aboutsummaryrefslogtreecommitdiffstats
path: root/z30.c
blob: 56e8f0e28ea81577047586416b1660151ce6c589 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*@z30.c:Symbol uses:InsertUses()@********************************************/
/*                                                                           */
/*  THE LOUT DOCUMENT FORMATTING SYSTEM (VERSION 3.39)                       */
/*  COPYRIGHT (C) 1991, 2008 Jeffrey H. Kingston                             */
/*                                                                           */
/*  Jeffrey H. Kingston (jeff@it.usyd.edu.au)                                */
/*  School of Information Technologies                                       */
/*  The University of Sydney 2006                                            */
/*  AUSTRALIA                                                                */
/*                                                                           */
/*  This program is free software; you can redistribute it and/or modify     */
/*  it under the terms of the GNU General Public License as published by     */
/*  the Free Software Foundation; either Version 3, or (at your option)      */
/*  any later version.                                                       */
/*                                                                           */
/*  This program is distributed in the hope that it will be useful,          */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
/*  GNU General Public License for more details.                             */
/*                                                                           */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program; if not, write to the Free Software              */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA   */
/*                                                                           */
/*  FILE:         z30.c                                                      */
/*  MODULE:       Symbol Uses                                                */
/*  EXTERNS:      InsertUses(), FlattenUses(), SearchUses(),                 */
/*                FirstExternTarget(), NextExternTarget()                    */
/*                                                                           */
/*****************************************************************************/
#include "externs.h"


/*****************************************************************************/
/*                                                                           */
/*  InsertUses(x, y)                                                         */
/*                                                                           */
/*  Record the fact that symbol x uses symbol y, by linking them.            */
/*  Increment count of the number of times y is used, if y is a parameter.   */
/*                                                                           */
/*****************************************************************************/

void InsertUses(OBJECT x, OBJECT y)
{ OBJECT tmp;
  debug2(DSU, D, "InsertUses( %s, %s )", SymName(x), SymName(y));
  if( type(x) == LOCAL && type(y) == LOCAL && !predefined(y) )
  { GetMem(tmp, USES_SIZE, no_fpos);  item(tmp) = y;
    if( base_uses(x) == nilobj )  next(tmp) = tmp;
    else next(tmp) = next(base_uses(x)), next(base_uses(x)) = tmp;
    base_uses(x) = tmp;
  }
  if( is_par(type(y)) )
  {
    uses_count(y) += (enclosing(y) == x ? 1 : 2);
    if( dirty(y) || uses_count(y) > 1 )  dirty(enclosing(y)) = TRUE;
  }
  else if( sym_body(y) == nilobj || dirty(y) )  dirty(x) = TRUE;
  debug5(DSU, D, "InsertUses returning ( %s %s; %s %s, count = %d )",
    SymName(x), (dirty(x) ? "dirty" : "clean"),
    SymName(y), (dirty(y) ? "dirty" : "clean"), uses_count(y));
} /* end InsertUses */


/*@::GatherUses(), GatherAllUses(), FlattenUses()@****************************/
/*                                                                           */
/*  static GatherUses(x, sym)                                                */
/*  static GatherAllUses(x)                                                  */
/*                                                                           */
/*  GatherUses adds all the unmarked descendants of x to the uses relation   */
/*  of sym;  GatherAllUses applies gather_uses to all descendants of x.      */
/*                                                                           */
/*****************************************************************************/

static void GatherUses(OBJECT x, OBJECT sym)
{ OBJECT link, y, tmp;
  if( base_uses(x) != nilobj )
  { link = next(base_uses(x));
    do
    { y = item(link);
      if( marker(y) != sym )
      {	if( y != sym )
	{ marker(y) = sym;
	  GetMem(tmp, USES_SIZE, no_fpos);  item(tmp) = y;
	  if( uses(sym) == nilobj )  next(tmp) = tmp;
	  else next(tmp) = next(uses(sym)), next(uses(sym)) = tmp;
	  uses(sym) = tmp;
	  if( indefinite(y) )  indefinite(sym) = TRUE;
	  if( uses_extern_target(y) )  uses_extern_target(sym) = TRUE;
	  GatherUses(y, sym);
	}
	else recursive(sym) = TRUE;
      }
      link = next(link);
    } while( link != next(base_uses(x)) );
  }
} /* end GatherUses */


static void GatherAllUses(OBJECT x)
{ OBJECT link, y;
  for( link = Down(x);  link != x;  link = NextDown(link) )
  { Child(y, link);
    if( type(y) == LOCAL )  GatherUses(y, y);
    GatherAllUses(y);
  }
} /* end GatherAllUses */


/*****************************************************************************/
/*                                                                           */
/*  FlattenUses()                                                            */
/*                                                                           */
/*  Traverse the directed graph assembled by InsertUses, finding its         */
/*  transitive closure and storing this explicitly in uses(x) for all x.     */
/*                                                                           */
/*****************************************************************************/

void FlattenUses(void)
{ GatherAllUses(StartSym);
} /* end FlattenUses */


/*@::SearchUses(), FirstExternTarget(), NextExternTarget()@*******************/
/*                                                                           */
/*  BOOLEAN SearchUses(x, y)                                                 */
/*                                                                           */
/*  Discover whether symbol x uses symbol y by searching the uses list of x. */
/*                                                                           */
/*****************************************************************************/

BOOLEAN SearchUses(OBJECT x, OBJECT y)
{ OBJECT p;
  debug3(DSU, DD, "SearchUses(%s, %s) uses: %d", SymName(x),SymName(y),uses(x));
  if( x == y )  return TRUE;
  if( uses(x) != nilobj )
  { p = next(uses(x));
    do
    { debug1(DSU, DDD, "  checking %s", SymName(item(p)));
      if( item(p) == y )  return TRUE;
      p = next(p);
    } while( p != next(uses(x)) );
  }
  return FALSE;
} /* end SearchUses */


/*****************************************************************************/
/*                                                                           */
/*  OBJECT FirstExternTarget(sym, cont)                                      */
/*  OBJECT NextExternTarget(sym, cont)                                       */
/*                                                                           */
/*  Together these two procedures return all symbols which are both used by  */
/*  sym and a target for at least one external galley.  Return nilobj at end.*/
/*                                                                           */
/*****************************************************************************/

OBJECT FirstExternTarget(OBJECT sym, OBJECT *cont)
{ OBJECT res;
  debug1(DSU, D, "FirstExternTarget( %s )", SymName(sym));
  res = nilobj;  *cont = nilobj;
  if( is_extern_target(sym) )  res = sym;
  else if( uses(sym) != nilobj )
  { *cont = next(uses(sym));
    do
    { if( is_extern_target(item(*cont)) )
      {	res = item(*cont);
	break;
      }
      *cont = next(*cont);
    } while( *cont != next(uses(sym)) );
  }
  debug1(DSU, D, "FirstExternTarget returning %s", SymName(res));
  return res;
} /* end FirstExternTarget */

OBJECT NextExternTarget(OBJECT sym, OBJECT *cont)
{ OBJECT res;
  debug1(DSU, D, "NextExternTarget( %s )", SymName(sym));
  res = nilobj;
  if( *cont != nilobj )
  { *cont = next(*cont);
    while( *cont != next(uses(sym)) )
    { if( is_extern_target(item(*cont)) )
      {	res = item(*cont);
	break;
      }
      *cont = next(*cont);
    }
  }
  debug1(DSU, D, "NextExternTarget returning %s", SymName(res));
  return res;
} /* end NextExternTarget */