Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / idf.c
1 /*
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".
4  */
5 /* $Id: idf.c,v 1.42 2001/07/03 08:55:16 ceriel Exp $ */
6 /*      IDENTIFIER  FIDDLING & SYMBOL TABLE HANDLING    */
7
8 #include        "lint.h"
9 #include        <em_reg.h>
10 #include        "debug.h"
11 #include        "idfsize.h"
12 #include        "botch_free.h"
13 #include        "nopp.h"
14 #include        "nparams.h"
15 #include        <alloc.h>
16 #include        "arith.h"
17 #include        "align.h"
18 #include        "LLlex.h"
19 #include        "level.h"
20 #include        "stack.h"
21 #include        "idf.h"
22 #include        "label.h"
23 #include        "def.h"
24 #include        "type.h"
25 #include        "proto.h"
26 #include        "struct.h"
27 #include        "declar.h"
28 #include        "decspecs.h"
29 #include        "sizes.h"
30 #include        "Lpars.h"
31 #include        "assert.h"
32
33 extern char options[];
34 extern arith NewLocal();
35 extern char *symbol2str();
36
37 #ifdef DEBUG
38 #define IDF_DEBUG
39 #endif
40
41 #include <idf_pkg.body>
42
43 struct idf *
44 gen_idf()
45 {
46         /*      A new idf is created out of nowhere, to serve as an
47                 anonymous name.
48         */
49         static int name_cnt;
50         char *s = Malloc(strlen(dot.tk_file) + 50);
51
52         sprint(s, "#%d in %s, line %u",
53                         ++name_cnt, dot.tk_file, dot.tk_line);
54         s = Realloc(s, strlen(s)+1);
55         return str2idf(s, 0);
56 }
57
58 int
59 is_anon_idf(idf)
60         struct idf *idf;
61 {
62         return idf->id_text[0] == '#';
63 }
64
65 declare_idf(ds, dc, lvl)
66         struct decspecs *ds;
67         struct declarator *dc;
68 {
69         /*      The identifier inside dc is declared on the level lvl, with
70                 properties deduced from the decspecs ds and the declarator
71                 dc.
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
75                 declarations.
76         */
77         register struct idf *idf = dc->dc_idf;
78         register int sc = ds->ds_sc;
79                 /*      This local copy is essential:
80                                 char b(), c;
81                         makes b GLOBAL and c AUTO.
82                 */
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;
87         
88         /* determine the present type */
89         if (ds->ds_type == 0)   {
90                 /*      at the L_FORMAL1 level there is no type specified yet
91                 */
92                 ASSERT(lvl == L_FORMAL1);
93                 type = int_type;        /* may change at L_FORMAL2 */
94         }
95         else    {
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,
102                                    but it has to be:
103                                 */
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");
109                         }
110                         else strict("%s has size 0", idf->id_text);
111                 }
112         }
113
114         /* some additional work for formal definitions */
115         if (lvl == L_FORMAL2)   {
116                 switch (type->tp_fund)  {
117                 case FUNCTION:
118                         warning("%s is a function; cannot be formal",
119                                 idf->id_text);
120                         type = construct_type(POINTER, type, 0, (arith)0,
121                                               NO_PROTO);
122                         break;
123                 case ARRAY:     /* 3.7.1 */
124                         type = construct_type(POINTER, type->tp_up, 0, (arith)0,
125                                               NO_PROTO);
126                         formal_array = 1;
127                         break;
128                 case FLOAT:
129                 case CHAR:
130                 case SHORT:
131                         /* The conversion is done in formal_cvt(). It is
132                          * not done when the type is float and there is a
133                          * prototype.
134                          */
135                         break;
136                 }
137         }
138         /*      The tests on types, postponed from do_decspecs(), can now
139                 be performed.
140         */
141         /* update the storage class */
142         if (type && type->tp_fund == FUNCTION)  {
143                 if (lvl != L_GLOBAL)  {         /* 3.5.1 */
144                         if (sc == 0)
145                                 sc = GLOBAL;
146                         else if (sc != EXTERN && sc != TYPEDEF) {
147                                 error("illegal storage class %s for function with block-scope"
148                                         , symbol2str(sc));
149                                 ds->ds_sc = sc = EXTERN;
150                         }
151                 }
152                 else if (sc == 0)
153                         sc = GLOBAL;
154         }
155         else    /* non-FUNCTION */
156                 if (sc == 0)
157                         sc =    lvl == L_GLOBAL ? GLOBAL
158                                 : lvl == L_FORMAL1 || lvl == L_FORMAL2 ? FORMAL
159                                 : AUTO;
160
161 #ifdef  LINT
162         check_hiding(idf, lvl, sc);     /* of some idf by this idf */
163 #endif  /* LINT */
164         if (def && lvl == L_LOCAL && def->df_level == L_FORMAL2) {
165                 error("%s redeclared", idf->id_text);
166         }
167
168         if (def && 
169             ( def->df_level == lvl ||
170               ( lvl != L_GLOBAL && def->df_level > lvl ) ||
171               (lvl == L_GLOBAL
172                && def->df_level == L_PROTO
173                && def->next && def->next->df_level == L_GLOBAL)
174            ))   {
175                 /*      There is already a declaration for idf on this
176                         level, or even more inside.
177                         The rules differ for different levels.
178                 */
179                 switch (lvl)    {
180                 case L_GLOBAL:
181                         global_redecl(idf, sc, type);
182                         def->df_file = idf->id_file;
183                         def->df_line = idf->id_line;
184                         break;
185                 case L_FORMAL1: /* formal declaration */
186                         error("formal %s redeclared", idf->id_text);
187                         break;
188                 case L_FORMAL2: /* formal definition */
189                 default:        /* local */
190                         if (sc != EXTERN) error("%s redeclared", idf->id_text);
191                         break;
192                 }
193         }
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 */
197                 def->df_type = type;
198                 def->df_formal_array = formal_array;
199                 def->df_sc = sc;
200                 def->df_level = L_FORMAL2;      /* CJ */
201                 def->df_file = idf->id_file;
202                 def->df_line = idf->id_line;
203         }
204         else    { /* fill in the def block */
205                 register struct def *newdef = new_def();
206
207                 newdef->next = def;
208                 newdef->df_level = lvl;
209                 newdef->df_type = type;
210                 newdef->df_sc = sc;
211                 newdef->df_file = idf->id_file;
212                 newdef->df_line = idf->id_line;
213 #ifdef  LINT
214                 newdef->df_set = 0;
215                 newdef->df_firstbrace = 0;
216 #endif  /* LINT */
217                 /* link it into the name list in the proper place */
218                 idf->id_def = newdef;
219                 update_ahead(idf);
220                 stack_idf(idf, stl);
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.
226                 */
227                 if (lvl >= L_LOCAL)     {
228                         ASSERT(sc);
229                         switch (sc)     {
230                         case REGISTER:
231                         case AUTO:
232                                 if (type->tp_size == (arith)-1
233                                         && type->tp_fund != ARRAY) {
234                                         error("size of local %s unknown",
235                                                 idf->id_text);
236                                 /** type = idf->id_def->df_type = int_type; **/
237                                 }
238                                 if (type->tp_size != (arith) -1) {
239                                     newdef->df_address =
240                                         NewLocal(type->tp_size,
241                                                  type->tp_align,
242                                                  regtype(type),
243                                                  sc);
244                                 }
245                                 break;
246                         case STATIC:
247                                 newdef->df_address = (arith) data_label();
248                                 break;
249                         }
250                 }
251         }
252 }
253
254 actual_declaration(sc, tp)
255         int sc;
256         struct type *tp;
257 {
258         /*      An actual_declaration needs space, right here and now.
259         */
260         register int fund = tp->tp_fund;
261         
262         if (sc == ENUM || sc == TYPEDEF) /* virtual declarations */
263                 return 0;
264         if (fund == FUNCTION || fund == ARRAY)
265                 /* allocation solved in other ways */
266                 return 0;
267         if (sc == EXTERN && fund == VOID) {
268                 /* strange, but should be accepted */
269                 return 0;
270         }
271         /* to be allocated */
272         return 1;
273 }
274
275 global_redecl(idf, new_sc, tp)
276         register struct idf *idf;
277         struct type *tp;
278 {
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
283                 in storage class.
284         */
285         register struct def *def = idf->id_def;
286
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);
290                 return;
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 [] */
295                         /* nothing new */
296                 } else
297                 if (def->df_type->tp_size < 0)  {       /* old decl has [] */
298                         def->df_type = tp;
299                 }
300         } if (tp->tp_fund == FUNCTION && new_sc == GLOBAL) {
301                 /* see 3.1.2.2 */
302                 new_sc = EXTERN;
303         }
304
305         /*      Now we may be able to update the storage class.
306                 Clean out this mess as soon as we know all the possibilities
307                 for new_sc.
308                 For now we have:
309                         EXTERN:         we have seen the word "extern"
310                         GLOBAL:         the item was declared on the outer
311                                         level, without either "extern" or
312                                         "static".
313                         STATIC:         we have seen the word "static"
314         */
315
316         switch (def->df_sc)     {       /* the old storage class */
317         case EXTERN:
318                 switch (new_sc) {       /* the new storage class */
319                 case STATIC:
320                         warning("%s redeclared static", idf->id_text);
321                         /* fallthrough */
322                 case GLOBAL:
323                         def->df_sc = new_sc;
324                         /* fallthrough */
325                 case EXTERN:
326                         break;
327                 default:
328                         crash("bad storage class");
329                         /*NOTREACHED*/
330                 }
331                 break;
332         case GLOBAL:
333                 switch (new_sc) {       /* the new storage class */
334                 case STATIC:            /* linkage disagreement */
335                         warning("%s redeclared static", idf->id_text);
336                         def->df_sc = new_sc;
337                         /* fallthrough */
338                 case GLOBAL:
339                 case EXTERN:
340                         break;
341                 default:
342                         crash("bad storage class");
343                         /*NOTREACHED*/
344                 }
345                 break;
346         case STATIC:
347                 switch (new_sc) {       /* the new storage class */
348                 case GLOBAL:            /* linkage disagreement */
349                 case EXTERN:
350                         warning("%s is already declared static", idf->id_text);
351                         /* fallthrough */
352                 case STATIC:
353                         break;
354                 default:
355                         crash("bad storage class");
356                         /*NOTREACHED*/
357                 }
358                 break;
359         case ENUM:
360         case TYPEDEF:
361                 error("illegal redeclaration of %s", idf->id_text);
362                 break;
363         default:
364                 crash("bad storage class");
365                 /*NOTREACHED*/
366         }
367 }
368
369 int
370 good_formal(def, idf)
371         register struct def *def;
372         register struct idf *idf;
373 {
374         /*      Succeeds if def is a proper L_FORMAL1 definition and
375                 gives an error message otherwise.
376         */
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);
380                 return 0;
381         }
382         ASSERT(def->df_sc == FORMAL);   /* CJ */
383         return 1;
384 }
385
386 declare_params(dc)
387         struct declarator *dc;
388 {
389         /*      Declares the formal parameters if they exist.
390         */
391         register struct formal *fm = dc->dc_formal;
392
393         while (fm)      {
394                 declare_parameter(fm->fm_idf);
395                 fm = fm->next;
396         }
397 }
398
399 idf_initialized(idf)
400         register struct idf *idf;
401 {
402         /*      The topmost definition of idf is set to initialized.
403         */
404         register struct def *def = idf->id_def; /* the topmost */
405         
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");
411                 return;
412         }
413         def->df_initialized = 1;
414 }
415
416 declare_parameter(idf)
417         struct idf *idf;
418 {
419         /*      idf is declared as a formal.
420         */
421         add_def(idf, FORMAL, int_type, level);
422 }
423
424 declare_enum(tp, idf, l)
425         struct type *tp;
426         struct idf *idf;
427         arith l;
428 {
429         /*      idf is declared as an enum constant with value l.
430         */
431         add_def(idf, ENUM, tp, level);
432         idf->id_def->df_address = l;
433 }
434
435 check_formals(idf, dc)
436         struct idf *idf;
437         struct declarator *dc;
438 {
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;
442
443         if (!du) {      /* error or typdef'ed function */
444                 error("illegal definition of %s", idf->id_text);
445                 return;
446         }
447
448         while (du
449                 && (du->du_fund != FUNCTION
450                     || du->next != (struct decl_unary *) 0)) {
451                 du = du->next;
452         }
453         if (!du) return;        /* terrible error, signalled earlier */
454
455         if (du->du_proto) return;
456
457         if (pl) {
458                 /* Don't give a warning about an old-style definition,
459                  * since the arguments will be checked anyway.
460                  */
461                 if (pl->pl_flag & PL_ELLIPSIS) {
462                     if (!(du->du_proto) && !(pl->pl_flag & PL_ERRGIVEN))
463                         error("ellipsis terminator in previous declaration");
464                     pl = pl->next;
465                 }
466                 else if (pl->pl_flag & PL_VOID) {
467                     pl = pl->next;                      /* should be 0 */
468                 }
469                 while(fm && pl) {
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;
476                     }
477                     fm = fm->next;
478                     pl = pl->next;
479                 }
480                 if (pl || fm) {
481                         error("incorrect number of parameters");
482                 }
483         } else {                        /* make a pseudo-prototype */
484                 register struct proto *lpl = new_proto();
485
486                 if (!options['o'])
487                         warning("'%s' old-fashioned function definition"
488                                         , dc->dc_idf->id_text);
489
490                 while (fm) {
491                         if (pl == 0) pl = lpl;
492                         else {
493                                 lpl->next = new_proto();
494                                 lpl = lpl->next;
495                         }
496                         lpl->pl_flag = PL_FORMAL;
497                         lpl->pl_idf = fm->fm_idf;
498                         lpl->pl_type =
499                                     promoted_type(fm->fm_idf->id_def->df_type);
500                         fm = fm->next;
501                 }
502                 if (pl == 0) {          /* make func(void) */
503                         pl = lpl;
504                         pl->pl_type = void_type;
505                         pl->pl_flag = PL_FORMAL | PL_VOID;
506                 }
507                 idf->id_def->df_type->tp_pseudoproto = pl;
508         }
509         free_formals(dc->dc_formal);
510         dc->dc_formal = 0;
511 }
512
513 declare_formals(idf, fp)
514         struct idf *idf;
515         arith *fp;
516 {
517         /*      Declares those formals as int that haven't been declared
518                 by the user.
519                 An address is assigned to each formal parameter.
520                 The total size of the formals is returned in *fp;
521         */
522         register struct stack_entry *se = stack_level_of(L_FORMAL1)->sl_entry;
523         arith f_offset = (arith)0;
524         register int nparams = 0;
525         int hasproto;
526         struct def *df = idf->id_def;
527
528         /* When one of the formals has the same name as the function, 
529            it hides the function def. Get it.
530         */
531         while (se) {
532                 if (se->se_idf == idf) {
533                         df = df->next;
534                         break;
535                 }
536                 se = se->next;
537         }
538
539         se = stack_level_of(L_FORMAL1)->sl_entry;
540         
541         hasproto = df->df_type->tp_proto != 0;
542
543 #ifdef  DEBUG
544         if (options['t'])
545                 dumpidftab("start declare_formals", 0);
546 #endif  /* DEBUG */
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;
550         }
551         while (se)      {
552                 df = se->se_idf->id_def;
553                 
554                 /* this stacklevel may also contain tags. ignore them */
555                 if (!df || df->df_level < L_FORMAL1 ) {
556                         se = se->next;
557                         continue;
558                 }
559
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.
564                 */
565                 if (! hasproto 
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);
569                 }
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),
573                                 df->df_sc);
574                 /* cvt int to char or short and double to float, if necessary
575                  */
576                 formal_cvt(hasproto, df);
577
578                 df->df_level = L_FORMAL2;       /* CJ */
579                 if (nparams++ >= STDC_NPARAMS)
580                         strict("number of formal parameters exceeds ANSI limit");
581 #ifdef DBSYMTAB
582                 if (options['g']) {
583                         stb_string(df, FORMAL, se->se_idf->id_text);
584                 }
585 #endif /* DBSYMTAB */
586                 se = se->next;
587         }
588         *fp = f_offset;
589 }
590
591 int
592 regtype(tp)
593         struct type *tp;
594 {
595         switch(tp->tp_fund) {
596         case INT:
597         case LONG:
598                 return reg_any;
599         case FLOAT:
600         case DOUBLE:
601         case LNGDBL:
602                 return reg_float;
603         case POINTER:
604                 return reg_pointer;
605         }
606         return -1;
607 }
608
609 add_def(idf, sc, tp, lvl)
610         struct idf *idf;
611         struct type *tp;
612         int lvl;
613         int sc;
614 {
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.
619         */
620         struct decspecs Ds; struct declarator Dc;
621
622         Ds = null_decspecs;
623         Ds.ds_type = tp;
624         Ds.ds_sc = sc;
625         Dc = null_declarator;
626         Dc.dc_idf = idf;
627         declare_idf(&Ds, &Dc, lvl);
628 }
629
630 update_ahead(idf)
631         register struct idf *idf;
632 {
633         /*      The tk_symb of the token ahead is updated in the light of new
634                 information about the identifier idf.
635         */
636         register int tk_symb = AHEAD;
637
638         if (    (tk_symb == IDENTIFIER || tk_symb == TYPE_IDENTIFIER) &&
639                 ahead.tk_idf == idf
640         )
641                 AHEAD = idf->id_def && idf->id_def->df_sc == TYPEDEF ?
642                                 TYPE_IDENTIFIER : IDENTIFIER;
643 }
644
645 free_formals(fm)
646         register struct formal *fm;
647 {
648         while (fm)      {
649                 struct formal *tmp = fm->next;
650
651                 free_formal(fm);
652                 fm = tmp;
653         }
654 }