1 /* D E F I N I T I O N M E C H A N I S M */
20 MkDef(id, scope, kind)
21 register struct idf *id;
22 register struct scope *scope;
25 /* Create a new definition structure in scope "scope", with
26 * id "id" and kind "kind".
28 register struct def *df = new_def();
33 df->df_type = error_type;
34 df->df_next = id->id_def;
37 /* enter the definition in the list of definitions in this scope
39 df->df_nextinscope = scope->sc_def;
45 define(id, scope, kind)
46 register struct idf *id;
47 register struct scope *scope;
50 /* Declare an identifier in a scope, but first check if it
51 already has been defined.
52 If so, then check for the cases in which this is legal,
53 and otherwise give an error message.
55 register struct def *df;
57 if( df = lookup(id, scope, 0L) ) {
58 if (df->df_kind == D_INUSE) {
59 if( kind != D_INUSE ) {
60 error("\"%s\" already used in this block",
63 return MkDef(id, scope, kind);
65 if (df->df_kind == D_ERROR ) {
66 /* used in forward references */
70 /* other cases fit in an int (assume at least 2 bytes) */
71 switch((int) df->df_kind ) {
74 /* generate error message somewhere else */
78 if( kind == D_VARIABLE )
79 /* program parameter declared as variable */
84 if( kind == D_FORWTYPE ) return df;
85 if( kind == D_TYPE ) {
86 /* forward reference resolved */
87 df->df_kind = D_FTYPE;
91 error("identifier \"%s\" must be a type",
96 if( kind == D_PROCEDURE ) return df;
97 error("procedure identification \"%s\" expected",
102 if( kind == D_FUNCTION ) return df;
103 error("function identification \"%s\" expected",
108 if( kind != D_ERROR )
109 /* avoid spurious error messages */
110 error("identifier \"%s\" already declared",id->id_text);
115 return MkDef(id, scope, kind);
118 DoDirective(directive, nd, tp, scl, function)
119 struct idf *directive;
122 struct scopelist *scl;
124 long kind; /* kind of directive */
125 int inp; /* internal or external name */
126 int ext = 0; /* directive = EXTERN */
127 struct def *df = lookup(directive, PervasiveScope, D_INUSE);
130 if( !is_anon_idf(directive) )
131 node_error(nd, "\"%s\" unknown directive",
136 if (df->df_kind == D_FORWARD) {
137 kind = function ? D_FWFUNCTION : D_FWPROCEDURE;
138 inp = (proclevel > 1);
140 else if (df->df_kind == D_EXTERN) {
141 kind = function ? D_FUNCTION : D_PROCEDURE;
146 node_error(nd, "\"%s\" unknown directive",
151 if( df = define(nd->nd_IDF, CurrentScope, kind) ) {
152 if( df->df_kind != kind ) {
153 /* identifier already forward declared */
154 node_error(nd, "\"%s\" already forward declared",
155 nd->nd_IDF->id_text);
161 df->prc_name = gen_proc_name(nd->nd_IDF, inp);
163 if (!(df->df_flags & D_EXTERNAL) && proclevel > 1)
164 tp->prc_nbpar -= pointer_size;
165 /* was added for static link which is not needed now.
166 But make sure this is done only once (look at the
169 df->df_flags |= D_EXTERNAL;
171 df->df_flags |= D_SET;
176 DeclProc(nd, tp, scl)
177 register struct node *nd;
179 register struct scopelist *scl;
181 register struct def *df;
183 if( df = define(nd->nd_IDF, CurrentScope, D_PROCEDURE) ) {
184 df->df_flags |= D_SET;
185 if( df->df_kind == D_FWPROCEDURE ) {
186 df->df_kind = D_PROCEDURE; /* identification */
188 /* Simulate a call to open_scope(), which has already
189 * been performed in the forward declaration.
191 CurrVis = df->prc_vis;
195 "\"%s\" already declared",
196 nd->nd_IDF->id_text);
198 else { /* normal declaration */
200 df->prc_name = gen_proc_name(nd->nd_IDF, (proclevel>1));
201 /* simulate open_scope() */
202 CurrVis = df->prc_vis = scl;
206 else CurrVis = scl; /* simulate open_scope() */
212 DeclFunc(nd, tp, scl)
213 register struct node *nd;
215 register struct scopelist *scl;
217 register struct def *df;
219 if( df = define(nd->nd_IDF, CurrentScope, D_FUNCTION) ) {
220 df->df_flags &= ~D_SET;
221 if( df->df_kind == D_FUNCTION ) { /* declaration */
223 node_error(nd, "\"%s\" illegal function declaration",
224 nd->nd_IDF->id_text);
225 tp = construct_type(T_FUNCTION, error_type);
227 /* simulate open_scope() */
228 CurrVis = df->prc_vis = scl;
230 df->prc_name = gen_proc_name(nd->nd_IDF, (proclevel > 1));
232 else { /* identification */
233 assert(df->df_kind == D_FWFUNCTION);
235 df->df_kind = D_FUNCTION;
236 CurrVis = df->prc_vis;
240 "\"%s\" already declared",
241 nd->nd_IDF->id_text);
246 else CurrVis = scl; /* simulate open_scope() */
252 register struct def *df;
254 /* assignment to functionname is illegal outside the functionblock */
257 /* Give the error about assignment as soon as possible. The
258 * |= assignment inhibits a warning in the main procedure.
260 if( !(df->df_flags & D_SET) ) {
261 error("function \"%s\" not assigned",df->df_idf->id_text);
262 df->df_flags |= D_SET;
267 register struct def *block_df;
269 register struct def *tmp_def = CurrentScope->sc_def;
270 register struct def *df;
274 /* The length of a usd_def chain is at most 1.
275 * The while is just defensive programming.
277 while( df->df_kind & D_INUSE )
280 if( !is_anon_idf(df->df_idf)
281 && (df->df_scope == CurrentScope) ) {
282 if( !(df->df_kind & (D_ENUM|D_LABEL|D_ERROR)) ) {
283 if( !(df->df_flags & D_USED) ) {
284 if( !(df->df_flags & D_SET) ) {
285 warning("\"%s\" neither set nor used in \"%s\"",
286 df->df_idf->id_text, block_df->df_idf->id_text);
289 warning("\"%s\" unused in \"%s\"",
290 df->df_idf->id_text, block_df->df_idf->id_text);
293 else if( !(df->df_flags & D_SET) ) {
294 if( !(df->df_flags & D_LOOPVAR) )
295 warning("\"%s\" not set in \"%s\"",
296 df->df_idf->id_text, block_df->df_idf->id_text);
301 tmp_def = tmp_def->df_nextinscope;