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 /* $Id: idf.c,v 1.42 2001/07/03 08:55:16 ceriel Exp $ */
6 /* IDENTIFIER FIDDLING & SYMBOL TABLE HANDLING */
12 #include "botch_free.h"
33 extern char options[];
34 extern arith NewLocal();
35 extern char *symbol2str();
41 #include <idf_pkg.body>
46 /* A new idf is created out of nowhere, to serve as an
50 char *s = Malloc(strlen(dot.tk_file) + 50);
52 sprint(s, "#%d in %s, line %u",
53 ++name_cnt, dot.tk_file, dot.tk_line);
54 s = Realloc(s, strlen(s)+1);
62 return idf->id_text[0] == '#';
65 declare_idf(ds, dc, lvl)
67 struct declarator *dc;
69 /* The identifier inside dc is declared on the level lvl, with
70 properties deduced from the decspecs ds and the declarator
72 The level is given explicitly to be able to insert, e.g.,
73 labels on the outermost level inside the function.
74 This routine implements the rich semantics of C
77 register struct idf *idf = dc->dc_idf;
78 register int sc = ds->ds_sc;
79 /* This local copy is essential:
81 makes b GLOBAL and c AUTO.
83 register struct def *def = idf->id_def; /* may be NULL */
84 register struct type *type;
85 struct stack_level *stl = stack_level_of(lvl);
86 char formal_array = 0;
88 /* determine the present type */
89 if (ds->ds_type == 0) {
90 /* at the L_FORMAL1 level there is no type specified yet
92 ASSERT(lvl == L_FORMAL1);
93 type = int_type; /* may change at L_FORMAL2 */
96 /* combine the decspecs and the declarator into one type */
97 type = declare_type(ds->ds_type, dc);
98 if (type->tp_size <= (arith)0 &&
99 actual_declaration(sc, type)) {
100 if (type->tp_size == (arith) -1) {
101 /* the type is not yet known,
104 if (type->tp_fund != VOID) {
105 if (level != L_GLOBAL)
106 error("unknown %s-type",
107 symbol2str(type->tp_fund));
108 } else error("void is not a complete type");
110 else strict("%s has size 0", idf->id_text);
114 /* some additional work for formal definitions */
115 if (lvl == L_FORMAL2) {
116 switch (type->tp_fund) {
118 warning("%s is a function; cannot be formal",
120 type = construct_type(POINTER, type, 0, (arith)0,
123 case ARRAY: /* 3.7.1 */
124 type = construct_type(POINTER, type->tp_up, 0, (arith)0,
131 /* The conversion is done in formal_cvt(). It is
132 * not done when the type is float and there is a
138 /* The tests on types, postponed from do_decspecs(), can now
141 /* update the storage class */
142 if (type && type->tp_fund == FUNCTION) {
143 if (lvl != L_GLOBAL) { /* 3.5.1 */
146 else if (sc != EXTERN && sc != TYPEDEF) {
147 error("illegal storage class %s for function with block-scope"
149 ds->ds_sc = sc = EXTERN;
155 else /* non-FUNCTION */
157 sc = lvl == L_GLOBAL ? GLOBAL
158 : lvl == L_FORMAL1 || lvl == L_FORMAL2 ? FORMAL
162 check_hiding(idf, lvl, sc); /* of some idf by this idf */
164 if (def && lvl == L_LOCAL && def->df_level == L_FORMAL2) {
165 error("%s redeclared", idf->id_text);
169 ( def->df_level == lvl ||
170 ( lvl != L_GLOBAL && def->df_level > lvl ) ||
172 && def->df_level == L_PROTO
173 && def->next && def->next->df_level == L_GLOBAL)
175 /* There is already a declaration for idf on this
176 level, or even more inside.
177 The rules differ for different levels.
181 global_redecl(idf, sc, type);
182 def->df_file = idf->id_file;
183 def->df_line = idf->id_line;
185 case L_FORMAL1: /* formal declaration */
186 error("formal %s redeclared", idf->id_text);
188 case L_FORMAL2: /* formal definition */
190 if (sc != EXTERN) error("%s redeclared", idf->id_text);
194 else /* the idf is unknown on this level */
195 if (lvl == L_FORMAL2 && sc != ENUM && good_formal(def, idf)) {
196 /* formal declaration, update only */
198 def->df_formal_array = formal_array;
200 def->df_level = L_FORMAL2; /* CJ */
201 def->df_file = idf->id_file;
202 def->df_line = idf->id_line;
204 else { /* fill in the def block */
205 register struct def *newdef = new_def();
208 newdef->df_level = lvl;
209 newdef->df_type = type;
211 newdef->df_file = idf->id_file;
212 newdef->df_line = idf->id_line;
215 newdef->df_firstbrace = 0;
217 /* link it into the name list in the proper place */
218 idf->id_def = newdef;
221 /* We now calculate the address.
222 Globals have names and don't get addresses, they
223 get numbers instead (through data_label()).
224 Formals are handled by declare_formals().
225 So here we hand out local addresses only.
227 if (lvl >= L_LOCAL) {
232 if (type->tp_size == (arith)-1
233 && type->tp_fund != ARRAY) {
234 error("size of local %s unknown",
236 /** type = idf->id_def->df_type = int_type; **/
238 if (type->tp_size != (arith) -1) {
240 NewLocal(type->tp_size,
247 newdef->df_address = (arith) data_label();
254 actual_declaration(sc, tp)
258 /* An actual_declaration needs space, right here and now.
260 register int fund = tp->tp_fund;
262 if (sc == ENUM || sc == TYPEDEF) /* virtual declarations */
264 if (fund == FUNCTION || fund == ARRAY)
265 /* allocation solved in other ways */
267 if (sc == EXTERN && fund == VOID) {
268 /* strange, but should be accepted */
271 /* to be allocated */
275 global_redecl(idf, new_sc, tp)
276 register struct idf *idf;
279 /* A global identifier may be declared several times,
280 provided the declarations do not conflict; they might
281 conflict in type (or supplement each other in the case of
282 an array) or they might conflict or supplement each other
285 register struct def *def = idf->id_def;
287 while (def->df_level != L_GLOBAL) def = def->next;
288 if (!equal_type(tp, def->df_type, 0, 1)) {
289 error("redeclaration of %s with different type", idf->id_text);
291 } else update_proto(tp, def->df_type);
292 if (tp->tp_fund == ARRAY) {
293 /* Multiple array declaration; this may be interesting */
294 if (tp->tp_size < 0) { /* new decl has [] */
297 if (def->df_type->tp_size < 0) { /* old decl has [] */
300 } if (tp->tp_fund == FUNCTION && new_sc == GLOBAL) {
305 /* Now we may be able to update the storage class.
306 Clean out this mess as soon as we know all the possibilities
309 EXTERN: we have seen the word "extern"
310 GLOBAL: the item was declared on the outer
311 level, without either "extern" or
313 STATIC: we have seen the word "static"
316 switch (def->df_sc) { /* the old storage class */
318 switch (new_sc) { /* the new storage class */
320 warning("%s redeclared static", idf->id_text);
328 crash("bad storage class");
333 switch (new_sc) { /* the new storage class */
334 case STATIC: /* linkage disagreement */
335 warning("%s redeclared static", idf->id_text);
342 crash("bad storage class");
347 switch (new_sc) { /* the new storage class */
348 case GLOBAL: /* linkage disagreement */
350 warning("%s is already declared static", idf->id_text);
355 crash("bad storage class");
361 error("illegal redeclaration of %s", idf->id_text);
364 crash("bad storage class");
370 good_formal(def, idf)
371 register struct def *def;
372 register struct idf *idf;
374 /* Succeeds if def is a proper L_FORMAL1 definition and
375 gives an error message otherwise.
377 if (!def || def->df_level != L_FORMAL1) { /* not in parameter list */
378 if (!is_anon_idf(idf))
379 error("%s not in parameter list", idf->id_text);
382 ASSERT(def->df_sc == FORMAL); /* CJ */
387 struct declarator *dc;
389 /* Declares the formal parameters if they exist.
391 register struct formal *fm = dc->dc_formal;
394 declare_parameter(fm->fm_idf);
400 register struct idf *idf;
402 /* The topmost definition of idf is set to initialized.
404 register struct def *def = idf->id_def; /* the topmost */
406 while (def->df_level <= L_PROTO) def = def->next;
407 if (def->df_initialized)
408 error("multiple initialization of %s", idf->id_text);
409 if (def->df_sc == TYPEDEF) {
410 error("typedef cannot be initialized");
413 def->df_initialized = 1;
416 declare_parameter(idf)
419 /* idf is declared as a formal.
421 add_def(idf, FORMAL, int_type, level);
424 declare_enum(tp, idf, l)
429 /* idf is declared as an enum constant with value l.
431 add_def(idf, ENUM, tp, level);
432 idf->id_def->df_address = l;
435 check_formals(idf, dc)
437 struct declarator *dc;
439 register struct formal *fm = dc->dc_formal;
440 register struct proto *pl = idf->id_def->df_type->tp_proto;
441 register struct decl_unary *du = dc->dc_decl_unary;
443 if (!du) { /* error or typdef'ed function */
444 error("illegal definition of %s", idf->id_text);
449 && (du->du_fund != FUNCTION
450 || du->next != (struct decl_unary *) 0)) {
453 if (!du) return; /* terrible error, signalled earlier */
455 if (du->du_proto) return;
458 /* Don't give a warning about an old-style definition,
459 * since the arguments will be checked anyway.
461 if (pl->pl_flag & PL_ELLIPSIS) {
462 if (!(du->du_proto) && !(pl->pl_flag & PL_ERRGIVEN))
463 error("ellipsis terminator in previous declaration");
466 else if (pl->pl_flag & PL_VOID) {
467 pl = pl->next; /* should be 0 */
470 if (!equal_type(promoted_type(fm->fm_idf->id_def->df_type)
471 , pl->pl_type, -1, 1)) {
472 if (!(pl->pl_flag & PL_ERRGIVEN))
473 error("incorrect type for parameter %s"
474 , fm->fm_idf->id_text);
475 pl->pl_flag |= PL_ERRGIVEN;
481 error("incorrect number of parameters");
483 } else { /* make a pseudo-prototype */
484 register struct proto *lpl = new_proto();
487 warning("'%s' old-fashioned function definition"
488 , dc->dc_idf->id_text);
491 if (pl == 0) pl = lpl;
493 lpl->next = new_proto();
496 lpl->pl_flag = PL_FORMAL;
497 lpl->pl_idf = fm->fm_idf;
499 promoted_type(fm->fm_idf->id_def->df_type);
502 if (pl == 0) { /* make func(void) */
504 pl->pl_type = void_type;
505 pl->pl_flag = PL_FORMAL | PL_VOID;
507 idf->id_def->df_type->tp_pseudoproto = pl;
509 free_formals(dc->dc_formal);
513 declare_formals(idf, fp)
517 /* Declares those formals as int that haven't been declared
519 An address is assigned to each formal parameter.
520 The total size of the formals is returned in *fp;
522 register struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
523 arith f_offset = (arith)0;
524 register int nparams = 0;
526 struct def *df = idf->id_def;
528 /* When one of the formals has the same name as the function,
529 it hides the function def. Get it.
532 if (se->se_idf == idf) {
539 se = stack_level_of(L_FORMAL1)->sl_entry;
541 hasproto = df->df_type->tp_proto != 0;
545 dumpidftab("start declare_formals", 0);
547 if (is_struct_or_union(df->df_type->tp_up->tp_fund)) {
548 /* create space for address of return value */
549 f_offset = pointer_size;
552 df = se->se_idf->id_def;
554 /* this stacklevel may also contain tags. ignore them */
555 if (!df || df->df_level < L_FORMAL1 ) {
560 df->df_address = f_offset;
561 /* the alignment convention for parameters is: align on
562 word boundaries, i.e. take care that the following
563 parameter starts on a new word boundary.
566 && df->df_type->tp_fund == FLOAT
567 && df->df_type->tp_size != double_size) {
568 f_offset = align(f_offset + double_size, (int) word_size);
570 else f_offset = align(f_offset + df->df_type->tp_size, (int) word_size);
571 RegisterAccount(df->df_address, df->df_type->tp_size,
572 regtype(df->df_type),
574 /* cvt int to char or short and double to float, if necessary
576 formal_cvt(hasproto, df);
578 df->df_level = L_FORMAL2; /* CJ */
579 if (nparams++ >= STDC_NPARAMS)
580 strict("number of formal parameters exceeds ANSI limit");
583 stb_string(df, FORMAL, se->se_idf->id_text);
585 #endif /* DBSYMTAB */
595 switch(tp->tp_fund) {
609 add_def(idf, sc, tp, lvl)
615 /* The identifier idf is declared on level lvl with storage
616 class sc and type tp, through a faked C declaration.
617 This is probably the wrong way to structure the problem,
618 but it will have to do for the time being.
620 struct decspecs Ds; struct declarator Dc;
625 Dc = null_declarator;
627 declare_idf(&Ds, &Dc, lvl);
631 register struct idf *idf;
633 /* The tk_symb of the token ahead is updated in the light of new
634 information about the identifier idf.
636 register int tk_symb = AHEAD;
638 if ( (tk_symb == IDENTIFIER || tk_symb == TYPE_IDENTIFIER) &&
641 AHEAD = idf->id_def && idf->id_def->df_sc == TYPEDEF ?
642 TYPE_IDENTIFIER : IDENTIFIER;
646 register struct formal *fm;
649 struct formal *tmp = fm->next;