home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / z30.c < prev    next >
Text File  |  1994-01-23  |  9KB  |  198 lines

  1. /*@z30.c:Symbol uses:InsertUses()@********************************************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z30.c                                                      */
  26. /*  MODULE:       Symbol Uses                                                */
  27. /*  EXTERNS:      InsertUses(), FlattenUses(), SearchUses(),                 */
  28. /*                FirstExternTarget(), NextExternTarget()                    */
  29. /*                                                                           */
  30. /*****************************************************************************/
  31. #include "externs"
  32.  
  33.  
  34. /*****************************************************************************/
  35. /*                                                                           */
  36. /*  InsertUses(x, y)                                                         */
  37. /*                                                                           */
  38. /*  Record the fact that symbol x uses symbol y, by linking them.            */
  39. /*  Increment count of the number of times y is used, if y is a parameter.   */
  40. /*                                                                           */
  41. /*****************************************************************************/
  42.  
  43. InsertUses(x, y)
  44. OBJECT x, y;
  45. { OBJECT tmp;
  46.   debug2(DSU, D, "InsertUses( %s, %s )", SymName(x), SymName(y));
  47.   if( type(x) == LOCAL && type(y) == LOCAL && !predefined(y) )
  48.   { tmp = GetMem(USES_SIZE, no_fpos);  item(tmp) = y;
  49.     if( base_uses(x) == nil )  next(tmp) = tmp;
  50.     else next(tmp) = next(base_uses(x)), next(base_uses(x)) = tmp;
  51.     base_uses(x) = tmp;
  52.   }
  53.   if( is_par(type(y)) )
  54.   { uses_count(y) += (enclosing(y) == x ? 1 : 2);
  55.     if( dirty(y) || uses_count(y) > 1 )  dirty(enclosing(y)) = TRUE;
  56.   }
  57.   else if( sym_body(y) == nil || dirty(y) )  dirty(x) = TRUE;
  58.   debug5(DSU, D, "InsertUses returning ( %s %s; %s %s, count = %d )",
  59.     SymName(x), (dirty(x) ? "dirty" : "clean"),
  60.     SymName(y), (dirty(y) ? "dirty" : "clean"), uses_count(y));
  61. } /* end InsertUses */
  62.  
  63.  
  64. /*@::GatherUses(), GatherAllUses(), FlattenUses()@****************************/
  65. /*                                                                           */
  66. /*  static GatherUses(x, sym)                                                */
  67. /*  static GatherAllUses(x)                                                  */
  68. /*                                                                           */
  69. /*  GatherUses adds all the unmarked descendants of x to the uses relation   */
  70. /*  of sym;  GatherAllUses applies gather_uses to all descendants of x.      */
  71. /*                                                                           */
  72. /*****************************************************************************/
  73.  
  74. static GatherUses(x, sym)
  75. OBJECT x, sym;
  76. { OBJECT link, y, tmp;
  77.   if( base_uses(x) != nil )
  78.   { link = next(base_uses(x));
  79.     do
  80.     { y = item(link);
  81.       if( marker(y) != sym )
  82.       {    if( y != sym )
  83.     { marker(y) = sym;
  84.       tmp = GetMem(USES_SIZE, no_fpos);  item(tmp) = y;
  85.       if( uses(sym) == nil )  next(tmp) = tmp;
  86.       else next(tmp) = next(uses(sym)), next(uses(sym)) = tmp;
  87.       uses(sym) = tmp;
  88.       if( indefinite(y) )  indefinite(sym) = TRUE;
  89.       if( uses_extern_target(y) )  uses_extern_target(sym) = TRUE;
  90.       GatherUses(y, sym);
  91.     }
  92.     else recursive(sym) = TRUE;
  93.       }
  94.       link = next(link);
  95.     } while( link != next(base_uses(x)) );
  96.   }
  97. } /* end GatherUses */
  98.  
  99.  
  100. static GatherAllUses(x)
  101. OBJECT x;
  102. { OBJECT link, y;
  103.   for( link = Down(x);  link != x;  link = NextDown(link) )
  104.   { Child(y, link);
  105.     if( type(y) == LOCAL )  GatherUses(y, y);
  106.     GatherAllUses(y);
  107.   }
  108. } /* end GatherAllUses */
  109.  
  110.  
  111. /*****************************************************************************/
  112. /*                                                                           */
  113. /*  FlattenUses()                                                            */
  114. /*                                                                           */
  115. /*  Traverse the directed graph assembled by InsertUses, finding its         */
  116. /*  transitive closure and storing this explicitly in uses(x) for all x.     */
  117. /*                                                                           */
  118. /*****************************************************************************/
  119.  
  120. FlattenUses()
  121. { GatherAllUses(StartSym);
  122. } /* end FlattenUses */
  123.  
  124.  
  125. /*@::SearchUses(), FirstExternTarget(), NextExternTarget()@*******************/
  126. /*                                                                           */
  127. /*  BOOLEAN SearchUses(x, y)                                                 */
  128. /*                                                                           */
  129. /*  Discover whether symbol x uses symbol y by searching the uses list of x. */
  130. /*                                                                           */
  131. /*****************************************************************************/
  132.  
  133. BOOLEAN SearchUses(x, y)
  134. OBJECT x, y;
  135. { OBJECT p;
  136.   debug3(DSU, DD, "SearchUses(%s, %s) uses: %d", SymName(x),SymName(y),uses(x));
  137.   if( x == y )  return TRUE;
  138.   if( uses(x) != nil )
  139.   { p = next(uses(x));
  140.     do
  141.     { debug1(DSU, DDD, "  checking %s", SymName(item(p)));
  142.       if( item(p) == y )  return TRUE;
  143.       p = next(p);
  144.     } while( p != next(uses(x)) );
  145.   }
  146.   return FALSE;
  147. } /* end SearchUses */
  148.  
  149.  
  150. /*****************************************************************************/
  151. /*                                                                           */
  152. /*  OBJECT FirstExternTarget(sym, cont)                                      */
  153. /*  OBJECT NextExternTarget(sym, cont)                                       */
  154. /*                                                                           */
  155. /*  Together these two procedures return all symbols which are both used by  */
  156. /*  sym and a target for at least one external galley.  Return nil at end.   */
  157. /*                                                                           */
  158. /*****************************************************************************/
  159.  
  160. OBJECT FirstExternTarget(sym, cont)
  161. OBJECT sym, *cont;
  162. { OBJECT res;
  163.   debug1(DSU, D, "FirstExternTarget( %s )", SymName(sym));
  164.   res = nil;  *cont = nil;
  165.   if( is_extern_target(sym) )  res = sym;
  166.   else if( uses(sym) != nil )
  167.   { *cont = next(uses(sym));
  168.     do
  169.     { if( is_extern_target(item(*cont)) )
  170.       {    res = item(*cont);
  171.     break;
  172.       }
  173.       *cont = next(*cont);
  174.     } while( *cont != next(uses(sym)) );
  175.   }
  176.   debug1(DSU, D, "FirstExternTarget returning %s", SymName(res));
  177.   return res;
  178. } /* end FirstExternTarget */
  179.  
  180. OBJECT NextExternTarget(sym, cont)
  181. OBJECT sym, *cont;
  182. { OBJECT res;
  183.   debug1(DSU, D, "NextExternTarget( %s )", SymName(sym));
  184.   res = nil;
  185.   if( *cont != nil )
  186.   { *cont = next(*cont);
  187.     while( *cont != next(uses(sym)) )
  188.     { if( is_extern_target(item(*cont)) )
  189.       {    res = item(*cont);
  190.     break;
  191.       }
  192.       *cont = next(*cont);
  193.     }
  194.   }
  195.   debug1(DSU, D, "NextExternTarget returning %s", SymName(res));
  196.   return res;
  197. } /* end NextExternTarget */
  198.