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 3.35 1994/06/24 12:03:59 ceriel Exp $ */
6 /* IDENTIFIER FIDDLING & SYMBOL TABLE HANDLING */
13 #include "botch_free.h"
31 #include "specials.h" /* registration of special identifiers */
32 #include "noRoption.h"
34 int idfsize = IDFSIZE;
35 extern char options[];
36 extern arith NewLocal();
38 char sp_occurred[SP_TOTAL+1]; /* indicate occurrence of special id */
40 struct idf *idf_hashtable[HASHSIZE];
41 /* All identifiers can in principle be reached through
42 idf_hashtable; idf_hashtable[hc] is the start of a chain of
43 idf's whose tags all hash to hc. Each idf is the start of
44 a chain of def's for that idf, sorted according to level,
45 with the most recent one on top.
46 Any identifier occurring on a level is entered into this
47 list, regardless of the nature of its declaration
48 (variable, selector, structure tag, etc.).
52 idf_hashed(tg, size, hc)
54 int size; /* includes the '\0' character */
57 /* The tag tg with length size and known hash value hc is
58 looked up in the identifier table; if not found, it is
59 entered. A pointer to it is returned.
60 The identifier has already been truncated to idfsize
63 register struct idf **hook = &idf_hashtable[hc], *notch;
65 while ((notch = *hook)) {
66 register char *s1 = tg;
67 register char *cp = notch->id_text;
70 while (!(cmp = (*s1 - *cp++))) {
79 /* suppose that special identifiers, as
80 "setjmp", are already inserted
82 sp_occurred[notch->id_special] = 1;
87 /* a new struct idf must be inserted at the hook */
90 *hook = notch; /* hooked in */
91 notch->id_text = Salloc(tg, (unsigned) size);
104 print("Hash table tally:\n");
105 for (i = 0; i < HASHSIZE; i++) {
106 register struct idf *notch = idf_hashtable[i];
113 print("%d %d\n", i, cnt);
115 print("End hash table tally\n");
124 /* str2idf() returns an entry in the symbol table for the
125 identifier tg. If necessary, an entry is created.
126 It is used where the text of the identifier is available
127 but its hash value is not; otherwise idf_hashed() is to
130 register char *cp = tg;
132 register int pos = -1;
134 char ntg[IDFSIZE + 1];
135 register char *ncp = ntg;
138 while (++pos < idfsize && (ch = *cp++)) {
140 hash = ENHASH(hash, ch, pos);
142 hash = STOPHASH(hash);
144 return idf_hashed(ntg, ncp - ntg, hash);
150 /* A new idf is created out of nowhere, to serve as an
155 char *s = Malloc(strlen(dot.tk_file)+50);
157 sprint(s, "#%d in %s, line %u",
158 ++name_cnt, dot.tk_file, dot.tk_line);
168 return idf->id_text[0] == '#';
171 declare_idf(ds, dc, lvl)
173 struct declarator *dc;
175 /* The identifier inside dc is declared on the level lvl, with
176 properties deduced from the decspecs ds and the declarator
178 The level is given explicitly to be able to insert, e.g.,
179 labels on the outermost level inside the function.
180 This routine implements the rich semantics of C
183 register struct idf *idf = dc->dc_idf;
184 register int sc = ds->ds_sc;
185 /* This local copy is essential:
187 makes b GLOBAL and c AUTO.
189 register struct def *def = idf->id_def; /* may be NULL */
190 register struct type *type;
191 struct stack_level *stl = stack_level_of(lvl);
192 char formal_array = 0;
194 /* determine the present type */
195 if (ds->ds_type == 0) {
196 /* at the L_FORMAL1 level there is no type specified yet
198 ASSERT(lvl == L_FORMAL1);
199 type = int_type; /* may change at L_FORMAL2 */
202 /* combine the decspecs and the declarator into one type */
203 type = declare_type(ds->ds_type, dc);
204 if (type->tp_size <= (arith)0 &&
205 actual_declaration(sc, type)) {
206 if (type->tp_size == (arith) -1) {
207 /* the type is not yet known,
210 extern char *symbol2str();
211 error("unknown %s-type",
212 symbol2str(type->tp_fund));
214 else if (type->tp_fund != LABEL) {
216 warning("%s has size 0", idf->id_text);
221 /* some additional work for formal definitions */
222 if (lvl == L_FORMAL2) {
223 switch (type->tp_fund) {
225 warning("%s is a function; cannot be formal",
227 type = construct_type(POINTER, type, (arith)0);
229 case ARRAY: /* RM 10.1 */
230 type = construct_type(POINTER, type->tp_up, (arith)0);
234 case FLOAT: /* RM 10.1 */
240 /* The RM is not clear about this: we must
241 convert the parameter from int (they have
242 been pushed as ints) to the specified type.
243 The conversion to type int or uint is not
249 /* The tests on types, postponed from do_decspecs(), can now
252 /* update the storage class */
253 if (type && type->tp_fund == FUNCTION) {
254 if (sc == 0 || (ds->ds_sc_given && sc == AUTO)) /* RM 8.1 */
257 if (sc == REGISTER) {
258 error("function storage class cannot be register");
259 ds->ds_sc = sc = GLOBAL;
262 else /* non-FUNCTION */
264 sc = lvl == L_GLOBAL ? GLOBAL
265 : lvl == L_FORMAL1 || lvl == L_FORMAL2 ? FORMAL
268 if (options['R']) { /* some special K & R tests */
269 /* is it also an enum? */
270 if (idf->id_enum && idf->id_enum->tg_level == level)
271 warning("%s is also an enum tag", idf->id_text);
272 /* is it a universal typedef? */
273 if (def && def->df_level == L_UNIVERSAL)
274 warning("redeclaring reserved word %s", idf->id_text);
279 check_hiding(idf, lvl, sc); /* of some idf by this idf */
283 ( def->df_level == lvl ||
284 ( lvl != L_GLOBAL && def->df_level > lvl )
287 /* There is already a declaration for idf on this
288 level, or even more inside.
289 The rules differ for different levels.
293 global_redecl(idf, sc, type);
294 def->df_file = idf->id_file;
295 def->df_line = idf->id_line;
297 case L_FORMAL1: /* formal declaration */
298 error("formal %s redeclared", idf->id_text);
300 case L_FORMAL2: /* formal definition */
302 error("%s redeclared", idf->id_text);
306 else /* the idf is unknown on this level */
307 if (lvl == L_FORMAL2 && sc != ENUM && good_formal(def, idf)) {
308 /* formal declaration, update only */
310 def->df_formal_array = formal_array;
312 def->df_level = L_FORMAL2; /* CJ */
313 def->df_file = idf->id_file;
314 def->df_line = idf->id_line;
317 if ( lvl >= L_LOCAL &&
318 (type->tp_fund == FUNCTION || sc == EXTERN)
320 /* extern declaration inside function is treated the
321 same way as global extern declaration
325 (sc == STATIC && type->tp_fund == FUNCTION)
327 if (!is_anon_idf(idf))
328 warning("non-global static function %s",
331 declare_idf(ds, dc, L_GLOBAL);
333 else { /* fill in the def block */
334 register struct def *newdef = new_def();
337 newdef->df_level = lvl;
338 newdef->df_type = type;
340 newdef->df_file = idf->id_file;
341 newdef->df_line = idf->id_line;
344 newdef->df_firstbrace = 0;
347 /* link it into the name list in the proper place */
348 idf->id_def = newdef;
351 /* We now calculate the address.
352 Globals have names and don't get addresses, they
353 get numbers instead (through data_label()).
354 Formals are handled by declare_formals().
355 So here we hand out local addresses only.
357 if (lvl >= L_LOCAL) {
362 if (type->tp_size == (arith)-1) {
363 error("size of local %s unknown",
365 /** type = idf->id_def->df_type = int_type; **/
368 NewLocal(type->tp_size,
374 newdef->df_address = (arith) data_label();
381 actual_declaration(sc, tp)
385 /* An actual_declaration needs space, right here and now.
387 register int fund = tp->tp_fund;
389 if (sc == ENUM || sc == TYPEDEF) /* virtual declarations */
391 if (fund == FUNCTION || fund == ARRAY)
392 /* allocation solved in other ways */
394 /* to be allocated */
398 global_redecl(idf, new_sc, tp)
399 register struct idf *idf;
400 register struct type *tp;
402 /* A global identifier may be declared several times,
403 provided the declarations do not conflict; they might
404 conflict in type (or supplement each other in the case of
405 an array) or they might conflict or supplement each other
408 register struct def *def = idf->id_def;
410 if (tp != def->df_type) {
411 register struct type *otp = def->df_type;
412 if ( tp->tp_fund != ARRAY || otp->tp_fund != ARRAY ||
413 tp->tp_up != otp->tp_up
415 error("redeclaration of %s with different type",
419 /* Multiple array declaration; this may be interesting */
420 if (tp->tp_size < 0) { /* new decl has [] */
424 if (otp->tp_size < 0) { /* old decl has [] */
428 if (tp->tp_size != otp->tp_size)
429 error("inconsistent size in redeclaration of array %s",
432 /* Now we may be able to update the storage class.
433 Clean out this mess as soon as we know all the possibilities
436 EXTERN: we have seen the word "extern"
437 GLOBAL: the item was declared on the outer
438 level, without either "extern" or
440 STATIC: we have seen the word "static"
441 IMPLICIT: function declaration inferred from
444 if (new_sc == IMPLICIT)
445 return; /* no new information */
446 switch (def->df_sc) { /* the old storage class */
448 switch (new_sc) { /* the new storage class */
453 if (def->df_initialized) {
454 error("cannot redeclare %s to static",
458 warning("%s redeclared to static",
464 crash("bad storage class");
469 switch (new_sc) { /* the new storage class */
476 if (def->df_initialized)
477 error("cannot redeclare %s to static",
481 /* warn unconditionally */
482 warning("%s redeclared to static",
486 /* warn conditionally */
488 warning("%s redeclared to static",
490 #endif /* NOROPTION */
496 crash("bad storage class");
501 switch (new_sc) { /* the new storage class */
503 if (def->df_initialized)
504 error("cannot redeclare %s to extern",
507 warning("%s redeclared to extern",
514 if (def->df_type->tp_fund != FUNCTION)
515 warning("%s was already static",
519 crash("bad storage class");
524 switch (new_sc) { /* the new storage class */
532 warning("%s was implicitly declared as extern",
538 crash("bad storage class");
544 error("illegal redeclaration of %s", idf->id_text);
547 crash("bad storage class");
553 good_formal(def, idf)
554 register struct def *def;
555 register struct idf *idf;
557 /* Succeeds if def is a proper L_FORMAL1 definition and
558 gives an error message otherwise.
560 if (!def || def->df_level != L_FORMAL1) { /* not in parameter list */
561 if (!is_anon_idf(idf))
562 error("%s not in parameter list", idf->id_text);
565 ASSERT(def->df_sc == FORMAL); /* CJ */
570 register struct declarator *dc;
572 /* Declares the formal parameters if they exist.
574 register struct formal *fm = dc->dc_formal;
577 declare_parameter(fm->fm_idf);
580 free_formals(dc->dc_formal);
585 register struct idf *idf;
587 /* The topmost definition of idf is set to initialized.
589 register struct def *def = idf->id_def; /* the topmost */
591 if (def->df_initialized)
592 error("multiple initialization of %s", idf->id_text);
593 if (def->df_sc == TYPEDEF) {
594 warning("typedef cannot be initialized");
595 def->df_sc = EXTERN; /* ??? *//* What else ? */
597 def->df_initialized = 1;
600 declare_parameter(idf)
603 /* idf is declared as a formal.
605 add_def(idf, FORMAL, int_type, level);
608 declare_enum(tp, idf, l)
613 /* idf is declared as an enum constant with value l.
615 add_def(idf, ENUM, tp, level);
616 idf->id_def->df_address = l;
622 /* Declares those formals as int that haven't been declared
624 An address is assigned to each formal parameter.
625 The total size of the formals is returned in *fp;
627 register struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
628 arith f_offset = (arith)0;
632 dumpidftab("start declare_formals", 0);
635 register struct def *def = se->se_idf->id_def;
637 def->df_address = f_offset;
638 /* the alignment convention for parameters is: align on
639 word boundaries, i.e. take care that the following
640 parameter starts on a new word boundary.
642 f_offset = align(f_offset + def->df_type->tp_size, (int) word_size);
643 formal_cvt(def); /* cvt int to char or short, if necessary */
644 def->df_level = L_FORMAL2; /* CJ */
645 RegisterAccount(def->df_address, def->df_type->tp_size,
646 regtype(def->df_type),
650 stb_string(def, FORMAL, se->se_idf->id_text);
652 #endif /* DBSYMTAB */
662 switch(tp->tp_fund) {
677 add_def(idf, sc, tp, lvl)
683 /* The identifier idf is declared on level lvl with storage
684 class sc and type tp, through a faked C declaration.
685 This is probably the wrong way to structure the problem,
686 but it will have to do for the time being.
688 struct decspecs Ds; struct declarator Dc;
693 Dc = null_declarator;
695 declare_idf(&Ds, &Dc, lvl);
699 register struct idf *idf;
701 /* The tk_symb of the token ahead is updated in the light of new
702 information about the identifier idf.
704 register int tk_symb = AHEAD;
706 if ( (tk_symb == IDENTIFIER || tk_symb == TYPE_IDENTIFIER) &&
709 AHEAD = idf->id_def && idf->id_def->df_sc == TYPEDEF ?
710 TYPE_IDENTIFIER : IDENTIFIER;
714 register struct formal *fm;
717 struct formal *tmp = fm->next;
728 /* A simple congruence random number generator, as
729 described in Knuth, vol 2.
731 register int h, rnd = HASH_X;
733 for (h = 0; h < IDFSIZE; h++) {
735 rnd = (HASH_A * rnd + HASH_C) & HASHMASK;