2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 * Author: Ceriel J.H. Jacobs
8 /* D E F I N I T I O N M E C H A N I S M */
10 /* $Id: def.c,v 1.66 1995/04/26 13:54:56 ceriel Exp $ */
30 extern char *sprint();
46 /* Put definition "df" in front of the list of definitions
48 This is neccessary because in some cases the order in this
51 register t_def *df1 = df->df_scope->sc_def;
54 /* Definition "df" is not in front of the list
57 /* Find definition "df"
59 if (df1->df_nextinscope == df) {
60 /* It already was in the list. Remove it
62 df1->df_nextinscope = df->df_nextinscope;
65 df1 = df1->df_nextinscope;
68 /* Now put it in front
70 df->df_nextinscope = df->df_scope->sc_def;
71 df->df_scope->sc_def = df;
76 MkDef(id, scope, kind)
78 register t_scope *scope;
80 /* Create a new definition structure in scope "scope", with
81 id "id" and kind "kind".
89 df->df_next = id->id_def;
91 if (kind == D_ERROR || kind == D_FORWARD) df->df_type = error_type;
92 if (kind & (D_TYPE|D_PROCEDURE|D_CONST)) {
93 df->df_flags = D_DEFINED;
96 /* enter the definition in the list of definitions in this scope
98 df->df_nextinscope = scope->sc_def;
104 define(id, scope, kind)
106 register t_scope *scope;
109 /* Declare an identifier in a scope, but first check if it
110 already has been defined.
111 If so, then check for the cases in which this is legal,
112 and otherwise give an error message.
116 DO_DEBUG(options['S'], print("define %s, %x\n", id->id_text, kind));
117 df = lookup(id, scope, D_IMPORT, 0);
118 if ( /* Already in this scope */
121 switch(df->df_kind) {
123 if (kind != D_INUSE && kind != D_ERROR) {
124 error("identifier \"%s\" already used; may not be redefined in this scope", df->df_idf->id_text);
125 df->df_kind = D_ERROR;
131 /* An opaque type. We may now have found the
132 definition of this type.
134 if (kind == D_TYPE && df->df_scope == CurrentScope &&
136 df->df_kind = D_TYPE;
142 /* A forward reference to a module. We may have found
143 another one, or we may have found the definition
146 if (kind & (D_FORWMODULE|D_FORWARD)) {
150 if (kind == D_MODULE) {
151 FreeNode(df->for_node);
152 df->mod_vis = df->for_vis;
160 if (kind == D_FORWTYPE) return df;
163 if (kind & (D_FORWTYPE|D_TYPE)) return df;
164 error("identifier \"%s\" must be a type", id->id_text);
165 df->df_kind = D_ERROR;
168 /* A forward reference, for which we may now have
171 if (! (kind & (D_FORWARD | D_FORWMODULE))) {
172 FreeNode(df->for_node);
174 df->df_kind = D_ERROR; /* avoiding error message */
178 if (kind != D_ERROR && df->df_kind != D_ERROR) {
179 /* Avoid spurious error messages
181 error("identifier \"%s\" already declared",
184 if (df->df_scope == scope || df->df_kind == D_ERROR) {
186 if (kind & (D_TYPE|D_PROCEDURE|D_CONST)) {
187 df->df_flags = D_DEFINED;
194 return MkDef(id, scope, kind);
197 end_definition_list(pdf)
198 register t_def **pdf;
200 /* Remove all imports from a definition module. This is
201 neccesary because the implementation module might import
203 Also, mark all other definitions "QUALIFIED EXPORT".
208 if (df->df_kind & D_IMPORTED) {
209 if (! (df->df_flags & D_USED)) {
210 warning(W_ORDINARY, "identifier \"%s\" imported but not used", df->df_idf->id_text);
212 RemoveFromIdList(df);
213 *pdf = df->df_nextinscope;
217 df->df_flags |= D_QEXPORTED;
218 pdf = &(df->df_nextinscope);
226 /* Remove definition "df" from the definition list
228 register t_idf *id = df->df_idf;
231 if ((df1 = id->id_def) == df) id->id_def = df->df_next;
233 while (df1->df_next != df) {
234 assert(df1->df_next != 0);
237 df1->df_next = df->df_next;
245 /* A procedure is declared, either in a definition or a program
246 module. Create a def structure for it (if neccessary).
247 Also create a name for it.
250 register t_scope *scope;
254 assert(type & (D_PROCEDURE | D_PROCHEAD));
256 if (type == D_PROCHEAD) {
257 /* In a definition module
259 df = define(id, CurrentScope, type);
260 df->for_node = dot2leaf(Name);
261 df->df_flags |= D_USED | D_DEFINED;
262 if (CurrentScope->sc_definedby->df_flags & D_FOREIGN) {
263 df->prc_name = id->id_text;
266 sprint(buf,"%s_%s",CurrentScope->sc_name,id->id_text);
267 df->prc_name = Salloc(buf, (unsigned) (strlen(buf)+1));
269 if (CurrVis == Defined->mod_vis) {
270 /* The current module will define this routine.
271 make sure the name is exported.
277 df = lookup(id, CurrentScope, D_IMPORTED, 0);
278 if (df && df->df_kind == D_PROCHEAD) {
279 /* C_exp already generated when we saw the definition
280 in the definition module
285 df = define(id, CurrentScope, type);
286 sprint(buf,"_%d_%s",++nmcount,id->id_text);
287 df->prc_name = Salloc(buf, (unsigned)(strlen(buf)+1));
289 df->df_flags |= D_DEFINED;
291 open_scope(OPENSCOPE);
292 scope = CurrentScope;
293 scope->sc_name = df->prc_name;
294 scope->sc_definedby = df;
296 df->prc_vis = CurrVis;
305 /* The end of a procedure declaration.
306 Check that the closing identifier matches the name of the
307 procedure, close the scope, and check that a function
308 procedure has at least one RETURN statement.
310 extern int return_occurred;
312 match_id(id, df->df_idf);
313 close_scope(SC_CHKFORW|SC_REVERSE);
314 if (! return_occurred && ResultType(df->df_type)) {
315 error("function procedure %s does not return a value",
316 df->df_idf->id_text);
321 DefineLocalModule(id)
324 /* Create a definition for a local module. Also give it
325 a name to be used for code generation.
327 register t_def *df = define(id, CurrentScope, D_MODULE);
328 register t_scope *sc;
329 static int modulecount = 0;
331 extern int proclevel;
333 sprint(buf, "_%d%s_", ++modulecount, id->id_text);
336 /* We never saw the name of this module before. Create a
339 open_scope(CLOSEDSCOPE);
340 df->mod_vis = CurrVis;
343 CurrVis = df->mod_vis;
346 sc->sc_level = proclevel;
347 sc->sc_definedby = df;
348 sc->sc_name = Salloc(buf, (unsigned) (strlen(buf) + 1));
350 /* Create a type for it
352 df->df_type = standard_type(T_RECORD, 1, (arith) 0);
353 df->df_type->rec_scope = sc;
355 /* Generate code that indicates that the initialization procedure
356 for this module is local.
366 /* Check the header of a procedure declaration against a
367 possible earlier definition in the definition module.
370 if (df->df_kind == D_PROCHEAD &&
372 df->df_type != error_type) {
373 /* We already saw a definition of this type
374 in the definition module.
377 if (!TstProcEquiv(tp, df->df_type)) {
378 error("inconsistent procedure declaration for \"%s\"",
379 df->df_idf->id_text);
381 FreeType(df->df_type);
382 df->df_kind = D_PROCEDURE;
391 print("n: %s, k: %d\n", df->df_idf->id_text, df->df_kind);