1 /*
2 * Copyright (c) 2003 - 2010, Nils R. Weller
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Functions to create, populate and search scopes with and for
28 * structure and enum definitions and variable declarations
29 */
30 #include "scope.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include "token.h"
36 #include "decl.h"
37 #include "misc.h"
38 #include "expr.h"
39 #include "features.h"
40
41 #if XLATE_IMMEDIATELY
42 #include "backend.h"
43 #endif
44
45 #include "error.h"
46 #include "type.h"
47 #include "symlist.h"
48 #include "functions.h"
49 #include "cc1_main.h"
50 #include "debug.h"
51 #include "n_libc.h"
52
53 struct scope global_scope = {
54 0,
55 NULL,
56 0,
57 0,
58 { NULL, NULL, 0, 0 },
59 { 0, 0, 0 },
60 { 0, 0, 0 },
61 { 0, 0, 0 },
62 { 0, 0, 0 },
63 { 0, 0, 0 },
64 { 0, 0, 0 },
65 { 0, 0, 0 },
66 NULL, NULL,
67 { NULL, NULL, 0, 0 },
68 { NULL, NULL, 0, 0 },
69 NULL, NULL, NULL
70 };
71 struct scope *curscope;
72 static struct scope *scopelist_tail = &global_scope;
73 struct decl *static_init_vars;
74 struct decl *siv_tail;
75 struct decl *static_uninit_vars;
76 struct decl *siuv_tail;
77
78 /* 02/02/08: TLS variables */
79 struct decl *static_init_thread_vars;
80 struct decl *sitv_tail;
81 struct decl *static_uninit_thread_vars;
82 struct decl *siutv_tail;
83
84 struct decl *siv_checkpoint;
85
86 struct sym_entry *extern_vars;
87 struct sym_entry *extern_vars_tail;
88
89 /*
90 * Looks up structure definition (not instance!) with tag ``tag''. If
91 * ``nested'' is nonzero, the lookup will use scopes above the current
92 * scope. If it's not, the lookup will only consider the current scope
93 * On success, a pointer to the structure definition is returned, else a
94 * null pointer
95 */
96 struct ty_struct *
lookup_struct(struct scope * s,const char * tag,int nested)97 lookup_struct(struct scope *s, const char *tag, int nested) {
98 do {
99 struct ty_struct *ts;
100
101 for (ts = s->struct_defs.head; ts != NULL; ts = ts->next) {
102 if (ts->tag != NULL) {
103 if (strcmp(ts->tag, tag) == 0) {
104 return ts;
105 }
106 } else if (ts->dummytag != NULL) {
107 if (strcmp(ts->dummytag, tag) == 0) {
108 return ts;
109 }
110 }
111 }
112 if (nested == 0) {
113 break;
114 }
115 } while ((s = s->parent) != NULL);
116
117 return NULL;
118 }
119
120 struct ty_enum *
lookup_enum(struct scope * s,const char * tag,int nested)121 lookup_enum(struct scope *s, const char *tag, int nested) {
122 int i;
123 char *p;
124
125 do {
126 if (s->enum_defs.data) {
127 for (i = 0; i < s->enum_defs.ndecls; ++i) {
128 p = s->enum_defs.data[i]->tag;
129 if (p != NULL && strcmp(p, tag) == 0) {
130 return s->enum_defs.data[i];
131 }
132 }
133 }
134 if (nested == 0) {
135 break;
136 }
137 } while ((s = s->parent) != NULL);
138 return NULL;
139 }
140
141 struct type *
lookup_typedef(struct scope * s,const char * name,int nested,int flags)142 lookup_typedef(struct scope *s, const char *name, int nested, int flags) {
143 struct decl **d;
144 int i;
145 size_t namelen = strlen(name);
146
147 do {
148 /*
149 * 03/03/09: If there's a non-typedef of the same name
150 * in this scope, then it wins over the typedef. So if
151 * we have
152 *
153 * typedef int foo;
154 * {
155 * int foo;
156 * for (foo = 0; foo < 5; ++foo) {
157 *
158 * ... then the for loop shall only reference the inner
159 * declaration
160 *
161 * 03/03/09: This must be overridable for cases like:
162 *
163 * typedef int foo;
164 * struct x {
165 * int foo;
166 * foo bar;
167 * };
168 *
169 * ... otherwise the second declaration will not look
170 * up the typedef because the identifier wins.
171 */
172 if ((flags & LTD_IGNORE_IDENT) == 0) {
173 if (lookup_symbol(s, name, 0) != NULL) {
174 /*
175 * This scope has a non-typedef declaration which
176 * wins over potential outside typedefs
177 */
178 return NULL;
179 }
180 }
181
182 if (s->typedef_hash.used) {
183 struct sym_entry *se;
184
185 #if 0
186 se = lookup_hash(s->typedef_hash, s->n_typedef_slots,
187 name, namelen);
188 #endif
189 se = new_lookup_hash(&s->typedef_hash, name, namelen);
190 if (se != NULL) {
191 return se->dec->dtype;
192 }
193 } else {
194 #if FAST_SYMBOL_LOOKUP
195 struct sym_entry *se;
196
197 se = fast_lookup_symbol_se(s, name, nested, 1);
198 if (se != NULL) {
199 return se->dec->dtype;
200 } else {
201 return NULL;
202 }
203
204 #else
205 if (s->typedef_decls.data) {
206 d = s->typedef_decls.data;
207 for (i = 0; i < s->typedef_decls.ndecls; ++i) {
208 if (strcmp(d[i]->dtype->name, name)
209 == 0) {
210 return d[i]->dtype;
211 }
212 }
213 }
214 #endif
215 }
216 if (!nested) {
217 break;
218 }
219 } while ((s = s->parent) != NULL);
220 return NULL;
221 }
222
223
224 struct scope *
new_scope(int type)225 new_scope(int type) {
226 struct scope *ret;
227 static struct scope nullscope;
228 static unsigned long scopeno = 1; /* 0 = global scope */
229
230 #ifdef DEBUG2
231 puts("opening new scope");
232 #endif
233 ret = n_xmalloc(sizeof *ret);
234 *ret = nullscope;
235
236 ret->type = type;
237 ret->parent = curscope;
238
239
240 ret->scopeno = scopeno++;
241 scopelist_tail->next = ret;
242 scopelist_tail = ret;
243 curscope = ret;
244
245 return ret;
246 }
247
248 void
close_scope(void)249 close_scope(void) {
250 #if defined(DEBUG) || defined(DEBUG2) || defined(DEBUG3)
251 if ((curscope = curscope->parent) == NULL) {
252 fprintf(stderr,
253 "Fatal error: Attempting to close global scope\n");
254 ++errors; /* exit() handler checks this */
255 abort();
256 }
257 #else
258 if (curscope->parent == NULL) {
259 ++errors;
260 errorfl(NULL, "Attempt to close global scope!");
261 } else {
262 curscope = curscope->parent;
263 }
264 #endif
265 }
266
267
268 static void
do_store_decl(struct scope * s,struct dec_block * destdec,struct decl * d)269 do_store_decl(struct scope *s, struct dec_block *destdec, struct decl *d) {
270 struct statement *st;
271
272 if (destdec->ndecls >= destdec->nslots) {
273 if (destdec->nslots == 0) {
274 destdec->nslots = 8;
275 }
276 destdec->nslots *= 2;
277 destdec->data = n_xrealloc(destdec->data,
278 sizeof *destdec->data * destdec->nslots);
279 }
280 destdec->data[ destdec->ndecls++ ] = d;
281
282 if (s != NULL) {
283 st = alloc_statement();
284 st->type = ST_DECL;
285 st->data = d;
286 append_statement(&s->code, &s->code_tail, st);
287 }
288 }
289
290 void
update_array_size(struct decl * d,struct type * ty)291 update_array_size(struct decl *d, struct type *ty) {
292 if (ty->tlist != NULL && ty->tlist->type == TN_ARRAY_OF) {
293 /* May have to update array information */
294 #if REMOVE_ARRARG
295 if (!d->dtype->tlist->have_array_size) {
296 d->dtype->tlist->arrarg_const = ty->tlist->arrarg_const;
297 d->dtype->tlist->have_array_size = 1;
298 }
299 #else
300 if (d->dtype->tlist->arrarg->const_value == NULL) {
301 d->dtype->tlist->arrarg->const_value
302 ty->tlist->arrarg->const_value;
303 }
304 #endif
305 }
306 }
307
308 static int
check_redef(struct scope * s,struct decl * newdec,int * need_def)309 check_redef(struct scope *s, struct decl *newdec, int *need_def) {
310 struct decl *d;
311 struct type *ty = newdec->dtype;
312 struct sym_entry *se;
313
314 #if 0
315 if ((d = lookup_symbol(s, ty->name, 0)) == NULL) {
316 return 0;
317 }
318 #endif
319 if ((se = lookup_symbol_se(s, ty->name, 0)) == NULL) {
320 return 0;
321 }
322 d = se->dec;
323
324 /*
325 * 04/02/08: Also do redeclaration checking for local extern
326 * declarations and local function declarations instead of
327 * erroring for those immediately (GNU code loves to
328 * redeclare local function declarations)
329 */
330 if (s == &global_scope
331 || newdec->dtype->storage == TOK_KEY_EXTERN
332 || newdec->dtype->is_func) {
333 int compres;
334 int is_extern_inline = 0;
335 int is_static_inline = 0;
336
337 /*
338 * Declarations at file scope are tentative. They
339 * might be repeated as often as desired, as long
340 * as none of them differs
341 */
342 if ((compres = compare_types(d->dtype, ty,
343 CMPTY_SIGN|CMPTY_CONST|CMPTY_TENTDEC)) == -1
344 || (d->dtype->storage
345 && ty->storage
346 && d->dtype->storage != ty->storage)
347 /*
348 * 05/25/09: The case below covers
349 *
350 * static void foo();
351 * void foo() {}
352 *
353 * ... which would otherwise not end up
354 * here because the storage flag of the
355 * redeclaration is 0 (for now)
356 */
357 || (d->dtype->storage == TOK_KEY_STATIC
358 && ty->storage == 0
359 && newdec->dtype->is_func)) {
360
361 /*
362 * 09/30/07: gcc and tinycc allow arbitrary
363 * redeclarations of static/extern storage
364 * specifiers, e.g.
365 *
366 * static int foo;
367 * extern int foo;
368 *
369 * BitchX and probably other programs have
370 * come to depend on this, so we must support
371 * it too
372 *
373 * 03/29/08: Careful here, we only pay close
374 * attention to the redeclaration if we don't
375 * already have an initializer for the variable!
376 * Because if we have
377 *
378 * static int foo = 123;
379 * extern int foo;
380 *
381 * ... then the definition of ``foo'' will be
382 * name-mangled to something like ``_Static_foo0''
383 * and subsequent declarations must also use that
384 * name. So we cannot allow the extern declaration
385 * to override the name (externs are not mangled)
386 */
387 if (compres != -1) {
388 /*
389 * Yes, storage is only difference
390 *
391 * XXX symbol management is almost
392 * certainly wrong, and will cause
393 * problems for nasm, because the
394 * ``has_def'' stuff below is not
395 * handled
396 *
397 * 05/25/09: We're going from static to
398 * extern in this code path. This is now
399 * disallowed for functions! Even in
400 *
401 * static void foo();
402 * extern void foo() {}
403 *
404 * ... foo() remains static. The gcc
405 * source depends on this.
406 *
407 * XXX What about non-function symbols?
408 * Those just give an error later on. We
409 * should sort this mess out
410 */
411 if (!newdec->dtype->is_func) {
412 warningfl(d->tok, "Redeclaration of "
413 "`%s' with different storage "
414 "class, this may cause problems "
415 "with some assemblers",
416 ty->name);
417 if (d->init == NULL) {
418 d->dtype->storage = ty->storage;
419 }
420 } else {
421 /*
422 * 05/25/09: Static function
423 * redeclared as extern. We have to
424 * kludge the new type to be static
425 * as well, or we will still generate
426 * a global symbol.
427 *
428 * XXX Shouldn't the caller take over
429 * the old type?
430 */
431 newdec->dtype->storage = TOK_KEY_STATIC;
432 return 1;
433 }
434 } else {
435 errorfl(d->tok,
436 "Redefinition of `%s' with "
437 "conflicting type",
438 ty->name);
439 return -1;
440 }
441
442 if (d->init == NULL) {
443 se->dec->invalid = 1;
444 /*
445 * 03/30/08: Save reference count (code may already
446 * have accessed the previous declaration)
447 */
448 newdec->references = d->references;
449 newdec->real_references = d->real_references; /* 12/24/08 */
450 remove_symlist(s, se);
451 return 0;
452 } else {
453 *need_def = 0;
454 return 1;
455 }
456 }
457
458 if (d->dtype->is_func) {
459 /*
460 * Might have to replace old function entry
461 * with new one if this is a definition
462 * and the old one is a declaration. In
463 * either case, one can be deallocated
464 */
465 if (d->dtype->is_def && ty->is_def) {
466 /*if (IS_INLINE(ty->flags) && ty->storage == TOK_KEY_STATIC) {
467 ...discard declaration AND definition, if any ...
468 is_extern_inline = 1;
469 } else*/ {
470 errorfl(d->tok,
471 "Multiple definitions of function `%s'",
472 ty->name);
473 }
474 } else if (ty->is_def) {
475 if (d->dtype->tlist->tfunc->nargs == -1) {
476 ty->tlist->tfunc->was_just_declared = 1;
477 }
478
479 d->dtype = ty;
480 }
481 if (IS_INLINE(ty->flags) && ty->storage == TOK_KEY_EXTERN) {
482 /*
483 * 12/25/08: If we have an extern declaration
484 * which is later redeclared as ``extern
485 * inline'', the first declaration must be
486 * invalidated to make the function local to
487 * the module
488 */
489 is_extern_inline = 1;
490 } else if (IS_INLINE(ty->flags) && ty->storage == TOK_KEY_STATIC) {
491 /*
492 * 03/01/09: Handle static inline too
493 */
494 is_static_inline = 1;
495 }
496 }
497
498 *need_def = 0;
499 if (ty->storage != TOK_KEY_EXTERN && !ty->is_func) {
500 if (!d->has_def) {
501 /*
502 * This is a non-extern tentative object
503 * declaration, so we need one definition
504 * somewhere in the translation unit
505 */
506 *need_def = 1;
507 }
508 }
509
510 update_array_size(d, ty);
511
512 if ( (d->dtype->storage == TOK_KEY_EXTERN
513 && !d->was_not_extern
514 && newdec->dtype->storage != TOK_KEY_EXTERN
515 && newdec->init == NULL
516 && d->init == NULL /* for extern init */)
517
518 || is_extern_inline
519 || is_static_inline) {
520
521 /*
522 * 03/29/08: Also remove the declaration if it is
523 * first extern, then not extern (so it suddenly
524 * requires a definition)
525 */
526 se->dec->invalid = 1;
527 /*
528 * 03/30/08: Save reference count (code may already
529 * have accessed the previous declaration)
530 */
531 newdec->references = d->references;
532 newdec->real_references = d->real_references; /* 12/24/08 */
533 remove_symlist(s, se);
534
535 return 0;
536 }
537
538 if (newdec->init != NULL) {
539 /*
540 * 03/21/08: The new declaration has an initializer,
541 * so that must be used or rejected if we already
542 * have one
543 * int stuff, stuff = 123, stuff, stuff; // OK
544 * int stuff = 123, stuff = 123; // BAD
545 */
546 if (d->init != NULL) {
547 errorfl(d->tok,
548 "Redefinition of `%s' with "
549 "different initializer",
550 ty->name);
551 return -1;
552 }
553
554 /*
555 * OUCH... We now have to relocate this variable
556 * from the list of uninitialized to the one
557 * for initialized variables. So let's just
558 * remove the old declaration and pretend it
559 * didn't exist (so the caller adds it)
560 */
561 se->dec->invalid = 1;
562 /*
563 * 03/30/08: Save reference count (code may already
564 * have accessed the previous declaration)
565 */
566 newdec->references = d->references;
567 newdec->real_references = d->real_references; /* 12/24/08 */
568 remove_symlist(s, se);
569
570 return 0;
571 }
572
573 return 1; /* already has entry */
574 } else {
575 struct token *ttmp;
576 ttmp = errorfl_mk_tok(
577 ty->file, ty->line, NULL);
578
579 errorfl(ttmp,
580 "Multiple definitions of `%s'", ty->name);
581 return -1;
582 }
583
584 /* NOTREACHED */
585 return 0;
586 }
587
588
589 /*
590 * The stuff below handles a particularly ugly part of the C language;
591 * local external declarations, particularly for functions, are
592 * permitted! This sucks big time because some assemblers, such as
593 * nasm, always require an explicit symbol declaration to get access
594 * to an external object. This in turn means duplicate symbols will
595 * cause problems, such that we have to check for them. On a per-
596 * scope basis, multiple declarations have already been handled
597 * neatly for some time. However, e.g. local declarations overriden by
598 * subsequent global definitions in the same module, or just
599 * subsequent declarations, caused errors.
600 *
601 * 04/08/08: Reactivated and completed this
602 */
603 struct local_extern_decl {
604 struct decl *dec;
605 struct local_extern_decl *next;
606 };
607
608 #define LED_TABSIZE 32
609
610 static struct local_extern_decl *local_extern_decls_head[LED_TABSIZE];
611 static struct local_extern_decl *local_extern_decls_tail[LED_TABSIZE];
612
613
614 static void
mark_as_shadow_decl(struct decl * d)615 mark_as_shadow_decl(struct decl *d) {
616 d->invalid = 1;
617 /*
618 * Mark this as a ``shadow'' declaration, which
619 * causes no symbol declarations to be emitted,
620 * but which nonetheless is used for declaration
621 * lookup by lookup_symbol()!
622 *
623 * Example:
624 *
625 * int foo = 456;
626 * int main() {
627 * int foo = 123;
628 * { extern int foo; printf("%d\n", foo); }
629 * }
630 *
631 * ... here we must keep the inner extern decl
632 * as a ``shadow'' declaration which wins over
633 * the outer automatic variable
634 */
635 d->invalid |= 2;
636 }
637
638 int
is_shadow_decl(struct decl * d)639 is_shadow_decl(struct decl *d) {
640 return (d->invalid & 2) != 0;
641 }
642
643 /*
644 * Stores local extern declaration. If there already is another
645 * local extern declaration in a different local or in the
646 * global scope, this declaration is already marked invalid
647 */
648 static void
put_local_extern_decl(struct decl * d)649 put_local_extern_decl(struct decl *d) {
650 int key = generic_hash(d->dtype->name, LED_TABSIZE);
651 struct local_extern_decl *ledp;
652 struct decl *prevdec;
653
654 for (ledp = local_extern_decls_head[key];
655 ledp != NULL;
656 ledp = ledp->next) {
657 if (strcmp(ledp->dec->dtype->name, d->dtype->name) == 0) {
658 /* We already have an extern declaration */
659 /*
660 * XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
661 * XXXXXXXXXXXXXXXXXXXX we may have to do some
662 * type merging here for extenr bug
663 */
664 mark_as_shadow_decl(d);
665
666 /*
667 * 07/21/08: If the outer local declaration has
668 * array size information, and the inner one doesn't,
669 * then take over the outer data
670 *
671 * extern char bogus[128];
672 * {
673 * extern char bogus[];
674 * sizeof bogus;
675 */
676 if (ledp->dec->dtype->tlist != NULL
677 && ledp->dec->dtype->tlist->type == TN_ARRAY_OF
678 && ledp->dec->dtype->tlist->have_array_size
679
680 && d->dtype->tlist != NULL
681 && d->dtype->tlist->type == TN_ARRAY_OF
682 && !d->dtype->tlist->have_array_size) {
683
684 d->dtype->tlist->have_array_size = 1;
685 d->dtype->tlist->arrarg_const =
686 ledp->dec->dtype->tlist->arrarg_const;
687 }
688
689 return;
690 }
691 }
692
693 /*
694 * Now check whether a PRECEDING global declaration already
695 * renders this declaration obsolete/invalid
696 */
697 prevdec = lookup_symbol(&global_scope, d->dtype->name, 0);
698
699 if (prevdec != NULL
700 && (prevdec->dtype->storage == TOK_KEY_STATIC
701 || prevdec->was_not_extern
702 || prevdec->init != NULL
703 || (prevdec->dtype->is_func && prevdec->dtype->is_def))) {
704 /*
705 * One of the following cases is true, and therefore
706 * this redeclaration is already obsolete:
707 *
708 * static int foo; void f() { extern int foo; }
709 * int foo; void f() { extern int foo; }
710 * extern int foo = 123; void f() { extern int foo; }
711 * extern void f2() {} void f() { void f2(); f2(); }
712 *
713 * We don't have to store this declaration either;
714 * Subsequent local declarations will also see the
715 * global version
716 */
717 mark_as_shadow_decl(d);
718 return;
719 } else if (prevdec != NULL) {
720 /*
721 * 06/01/08: If there is no definition yet, we may still wish
722 * to keep a previous declaration if it is more complete (e.g.
723 * specifies array size)
724 */
725 if (compare_types(prevdec->dtype, d->dtype,
726 CMPTY_SIGN|CMPTY_CONST) == -1) {
727 errorfl(d->tok, "Redeclaration of extern `%s' "
728 "with conflicting type",
729 d->dtype->name);
730 return;
731 } else {
732 /*
733 * Types are compatible - but are they arrays?
734 */
735 if (prevdec->dtype->tlist != NULL
736 && prevdec->dtype->tlist->type
737 == TN_ARRAY_OF) {
738 if (prevdec->dtype->tlist->have_array_size) {
739 return;
740 }
741 }
742 }
743 }
744
745 /* New declaration */
746 ledp = n_xmalloc(sizeof *ledp); /* XXX zone allocator */
747 ledp->dec = d;
748 ledp->next = NULL;
749 if (local_extern_decls_head[key] == NULL) {
750 local_extern_decls_head[key] = local_extern_decls_tail[key] =
751 ledp;
752 } else {
753 local_extern_decls_tail[key]->next = ledp;
754 local_extern_decls_tail[key] = ledp;
755 }
756 }
757
758 /*
759 * Checks whether a new global DEFINITION renders previous extern
760 * declarations obsolete, and if so, marks them invalid
761 */
762 static void
check_local_extern_decl(struct decl * newdec)763 check_local_extern_decl(struct decl *newdec) {
764 int key = generic_hash(newdec->dtype->name, LED_TABSIZE);
765 struct local_extern_decl *ledp;
766
767 for (ledp = local_extern_decls_head[key];
768 ledp != NULL;
769 ledp = ledp->next) {
770
771 if (strcmp(ledp->dec->dtype->name, newdec->dtype->name) == 0) {
772 /*
773 * We have an extern declaration, and it must
774 * be invalidated because we now have a global
775 * definition too
776 */
777 mark_as_shadow_decl(ledp->dec);
778 return;
779 }
780 }
781 }
782
783
784 char *watchvar = NULL; /*"stuff";*/
785
786
787 /*
788 * Stores declaration referenced by ``d'' in current scope (curscope)
789 */
790 void
store_decl_scope(struct scope * s,struct decl ** dec)791 store_decl_scope(struct scope *s, struct decl **dec) {
792 struct type *t;
793 struct dec_block *destdec = NULL;
794 int i;
795
796 for (i = 0; dec[i] != NULL; ++i) {
797 t = dec[i]->dtype;
798
799 if (t->storage == TOK_KEY_TYPEDEF) {
800 /* Store dtype */
801 struct type *ot;
802
803 ot = lookup_typedef(s, t->name, 0, 0);
804 if (ot != NULL) {
805 /*
806 * 04/20/08: The declarations reorganization
807 * apparently caused typedef redeclarations to
808 * break. This brought up the topic that gcc
809 * allows these redeclarations ONLY if they
810 * are performed in different headers! E.g. on
811 * FreeBSD, there are va_list redeclarations,
812 * and gcc allows them because it looks at the
813 * preprocessor info and finds they are done
814 * in different headers. But if you put two
815 * declarations in one header, gcc gives an
816 * error! This SUCKS! We now always allow
817 * compatible redeclarations, but give one
818 * warning per file for it
819 */
820 if (compare_types(ot, t,
821 CMPTY_SIGN|CMPTY_CONST) == -1
822 || (ot->storage
823 && t->storage
824 && ot->storage != t->storage)) {
825
826 errorfl(dec[i]->tok, "Redefinition "
827 "of typedef "
828 "for `%s' with conflicting type",
829 t->name);
830 } else {
831 static int warned;
832
833 if (!warned) {
834 warningfl(dec[i]->tok,
835 "Redefinition "
836 "of typedef for `%s'",
837 t->name);
838 warned = 1;
839 }
840 }
841 return;
842 }
843
844 #if FAST_SYMBOL_LOOKUP
845 if (1) {
846 destdec = &s->typedef_decls;
847 put_fast_sym_hash(s, make_sym_entry(dec[i]), 1);
848 } else
849 #endif
850 if (s != &global_scope) {
851 destdec = &s->typedef_decls;
852 } else {
853 if (!s->typedef_hash.used) {
854 /* Global scope - need large table! */
855 new_make_hash_table(&s->typedef_hash,
856 SYM_HTAB_GLOBAL_SCOPE);
857 }
858 new_put_hash_table(&s->typedef_hash,
859 make_sym_entry(dec[i]));
860 }
861 #ifdef DEBUG2
862 printf("storing typedef %s\n",
863 t->name ? t->name : "unnamed typedef");
864 #endif
865 } else {
866 int debug_output = 0;
867
868 /* This is a ``real'' declaration */
869 if (t->name != NULL) {
870 /*
871 * Not anonymous - protect against
872 * redefinition
873 */
874 int need_def;
875 int rc = check_redef(s, dec[i], &need_def);
876
877 if (watchvar && strcmp(t->name, watchvar) == 0) {
878 debug_output = 1;
879 }
880 if (debug_output) {
881 printf("Storing decl %s (%p) to scope %p (global=%p)\n",
882 watchvar, dec[i], s, &global_scope);
883 printf("Storage = %s\n",
884 dec[i]->dtype->storage == TOK_KEY_EXTERN?
885 "extern":
886 dec[i]->dtype->storage == TOK_KEY_STATIC?
887 "static":
888 "none");
889 }
890
891 if (rc == 1) {
892 /*
893 * Multiple tentative definitions -
894 * OK
895 */
896 if (debug_output) {
897 printf(" %s already declared (but tentative) ...\n", watchvar);
898 }
899 #if XLATE_IMMEDIATELY
900 /*
901 * 03/25/08: nasm (at least old versions)
902 * accept
903 * symbol:
904 * ....
905 * global symbol
906 *
907 * ... but fail to create a global
908 * symbol table entry! So we have to
909 * perform the global declaration before
910 * defining the symbol. This condition
911 * was not fulfilled if there was a
912 * previous declaration preceding the
913 * definition;
914 *
915 * void func();
916 * void func() {}
917 *
918 * ... so now we handle this case here
919 */
920 if (dec[i]->dtype->is_func
921 && dec[i]->dtype->is_def) {
922
923 if (dec[i]->dtype->storage
924 == TOK_KEY_EXTERN) {
925 if (debug_output) {
926 printf(" %s Writing extern global decl? %s\n",
927 watchvar, dec[i]->has_symbol? "NO": "YES");
928 }
929 emit->global_extern_decls(&dec[i], 1);
930 } else {
931 if (debug_output) {
932 printf(" %s Writing static global decl? %s\n",
933 watchvar, dec[i]->has_symbol? "NO": "YES");
934 }
935 if (sysflag != OS_OSX) {
936 emit->global_static_decls(&dec[i], 1);
937 }
938 }
939 }
940 #endif
941 if (!need_def) {
942 /*
943 * Need not be allocated because
944 * it is extern or tentative
945 */
946 if (debug_output) {
947 printf(" %s NOT Writing definition! (extern or tentative)\n",
948 watchvar);
949 }
950 continue;
951 }
952
953 /*
954 * This is a redeclaration of a tentative
955 * declaration which also requires a
956 * definition.
957 */
958 if (dec[i]->init == NULL) {
959 continue;
960 }
961 dec[i]->has_def = 1;
962 } else if (rc == -1) {
963 /* Redefinition - bad */
964 return;
965 }
966
967 }
968
969
970 if (s->type == SCOPE_CODE
971 && (t->storage == TOK_KEY_EXTERN
972 || t->is_func
973 /* && t->is_def*/
974 /* && t->storage != TOK_KEY_STATIC
975 && t->tlist->type == TN_FUNCTION*/)) {
976 #if 0
977 /*
978 * XXX 07/22/07:
979 * Local extern declarations are terrible to
980 * deal with.. for now we always put them
981 * into the global scope to avoid
982 * redeclarations of symbols
983 *
984 * 08/22/07: This was missing the TN_FUNCTION
985 * check; without function pointers don't
986 * work
987 */
988 s = &global_scope;
989 #endif
990 /*
991 * 07/21/08: Always call put_local_extern_decl(),
992 * not just for nasm! The reason is that we must
993 * do type-checking. E.g. in
994 *
995 * extern char buf[128];
996 * void f() { extern char buf[]; }
997 *
998 * ... we are not allowed to discard the size
999 * information in f() such that sizeof() would
1000 * not work
1001 */
1002 if (/*emit->need_explicit_extern_decls*/ 1
1003 && s != &global_scope) {
1004 /*
1005 * 04/08/08: We have to revive the local
1006 * extern distinction for nasm and yasm to
1007 * avoid duplicate definitions
1008 */
1009 if (s != &global_scope) {
1010 put_local_extern_decl(dec[i]);
1011 } else {
1012 /* check_local_extern_decl(dec[i]);*/
1013 }
1014 }
1015 }
1016
1017 if (s == &global_scope) {
1018 int is_fdef = 0;
1019 /* Global or file scope declaration */
1020 #ifdef DEBUG2
1021 printf("Storing global declaration %s\n",
1022 t->name);
1023 #endif
1024 if (t->storage == TOK_KEY_AUTO) {
1025 errorfl(dec[i]->tok /*XXX*/,
1026 "Bogus storage class specifier in global variable declaration");
1027 return;
1028 } else if (t->storage == TOK_KEY_EXTERN
1029 || (t->is_func
1030 && !t->is_def
1031 && t->storage
1032 != TOK_KEY_STATIC)) {
1033 if (dec[i]->init != NULL) {
1034 /*
1035 * Initialized extern! Put
1036 * this into the static list
1037 */
1038 destdec = &s->static_decls;
1039 } else {
1040 if (IS_INLINE(t->flags)) {
1041 /*
1042 * 12/25/08: extern inline seems
1043 * to mean ``local function''
1044 */
1045 t->storage = TOK_KEY_STATIC;
1046 destdec = &s->static_decls;
1047 } else {
1048 t->storage = TOK_KEY_EXTERN;
1049 destdec = &s->extern_decls;
1050 }
1051 if (t->is_func && t->is_def) {
1052 is_fdef = 1;
1053 }
1054 }
1055 } else if (t->storage == 0) {
1056 /*
1057 * Need this to distinguish between
1058 * global and local static variables.
1059 */
1060 dec[i]->was_not_extern = 1;
1061 t->storage = TOK_KEY_EXTERN;
1062 destdec = &s->static_decls;
1063 } else if (t->storage == TOK_KEY_STATIC) {
1064 destdec = &s->static_decls;
1065 } else {
1066 errorfl(dec[i]->tok,
1067 "Invalid storage class for "
1068 "`%s'", t->name);
1069 return;
1070 }
1071
1072 if ((destdec == &s->static_decls || is_fdef)
1073 && emit->need_explicit_extern_decls) {
1074 /*
1075 * 04/08/08:
1076 * This is a (possibly tentative)
1077 * definition in global scope, so we
1078 * may have to check whether this
1079 * renders local extern declarations
1080 * obsolete, e.g. in
1081 *
1082 * int main() { void f(); f(); }
1083 * void f() {}
1084 *
1085 * ... we have to remove the local
1086 * extern declaration for f() because
1087 * otherwise it will clash with the
1088 * GLOBAL declaration later
1089 */
1090 check_local_extern_decl(dec[i]);
1091 }
1092 } else {
1093 /*
1094 * Local declaration (static or
1095 * automatic or register)
1096 */
1097 #ifdef DEBUG2
1098 printf("Storing local declaration %s\n",
1099 t->name? t->name:
1100 "unknown declaration");
1101 #endif
1102
1103 if (t->storage == TOK_KEY_EXTERN) {
1104 destdec = &s->extern_decls;
1105 } else if (t->storage == TOK_KEY_STATIC) {
1106 destdec = &s->static_decls;
1107 if (debug_output) {
1108 printf(" (%s Will append to static block)\n",
1109 watchvar);
1110 }
1111 } else if (t->storage == TOK_KEY_REGISTER) {
1112 /* destdec = &s->register_decls; */
1113 destdec = &s->automatic_decls;
1114 } else if (t->is_func && t->tlist->type == TN_FUNCTION) {
1115 /* XXX not working */
1116 t->storage = TOK_KEY_EXTERN;
1117 destdec = &s->extern_decls;
1118 if (debug_output) {
1119 printf(" (%s Will append to extern block)\n",
1120 watchvar);
1121 }
1122 } else {
1123 /*
1124 * 08/21/07: Removed because this is
1125 * never used
1126 */
1127 /* t->storage = TOK_KEY_AUTO;*/
1128 destdec = &s->automatic_decls;
1129 }
1130 if (t->is_func
1131 && t->tlist->type == TN_FUNCTION) {
1132 /* Record local function declaration */
1133 #if 0
1134 printf("putting local fdec `%s'\n", t->name);
1135 put_local_func_decl(dec[i]);
1136 #endif
1137 }
1138 }
1139
1140 if (destdec == &s->extern_decls) {
1141 if (debug_output) {
1142 printf(" %s Appending to extern vars list\n",
1143 watchvar);
1144 }
1145 append_symlist(NULL, &extern_vars,
1146 &extern_vars_tail,
1147 dec[i]);
1148 }
1149
1150 #if XLATE_IMMEDIATELY
1151 /*
1152 * We have to declare global symbols before they are
1153 * defined because otherwise nasm will accept our code
1154 * but, due to some bug, not create the requested
1155 * global symbol. See comment at check_redef() call too
1156 */
1157 if (dec[i]->dtype->is_func
1158 && dec[i]->dtype->is_def) {
1159 if (dec[i]->dtype->storage
1160 == TOK_KEY_EXTERN) {
1161 if (debug_output) {
1162 printf(" %s Function definition - writing "
1163 "extern global decl? %s\n",
1164 watchvar, dec[i]->has_symbol? "NO": "YES");
1165 }
1166 emit->global_extern_decls(&dec[i], 1);
1167 } else {
1168 if (debug_output) {
1169 printf(" %s Function definition - writing "
1170 "static global decl? %s\n",
1171 watchvar, dec[i]->has_symbol? "NO": "YES");
1172 }
1173 if (sysflag != OS_OSX)
1174 emit->global_static_decls(&dec[i], 1);
1175 }
1176 }
1177 #endif
1178 /*
1179 * 03/27/08: Mark non-extern declaration
1180 * as having a definition, so that
1181 * subsequent redeclarations are not
1182 * processed (to avoid multiple ``global''
1183 * declarations).
1184 *
1185 * This does not filter initialized
1186 * variables (which is correct) because
1187 * those always let check_redef() return
1188 * 0, so
1189 *
1190 * int foo;
1191 * int foo = 123;
1192 *
1193 * .. will use the correct definition
1194 * XXX this is probably still full of
1195 * bugs
1196 */
1197 if (dec[i]->dtype->storage != TOK_KEY_EXTERN
1198 && (!dec[i]->dtype->is_func
1199 || dec[i]->dtype->is_def)) {
1200 if (debug_output) {
1201 printf(" %s Setting has_def (subsequent decls "
1202 "should not emit anything)!\n", watchvar);
1203 }
1204 dec[i]->has_def = 1;
1205 } else if (dec[i]->dtype->storage == TOK_KEY_EXTERN
1206 && dec[i]->init != NULL) {
1207 /* 04/03/08: This was missing! */
1208 dec[i]->has_def = 1;
1209 }
1210
1211
1212 if (destdec == &s->static_decls
1213 && !dec[i]->dtype->is_func) {
1214
1215 #if XLATE_IMMEDIATELY
1216 /* ++dec[i]->references;*/
1217 #endif
1218 #if 0
1219 if (dec[i]->dtype->storage == TOK_KEY_EXTERN) {
1220 /* 03/21/08: New - make tentative
1221 * declarations work (whoops)
1222 */
1223 dec[i]->has_def = 1;
1224 }
1225 #endif
1226
1227
1228 assert(dec[i]->next == NULL);
1229 if (dec[i]->init != NULL) {
1230 if (IS_THREAD(dec[i]->dtype->flags)) {
1231 if (debug_output) {
1232 printf(" %s Appending to init thread list\n", watchvar);
1233 }
1234 append_decl(
1235 &static_init_thread_vars,
1236 &sitv_tail,
1237 dec[i]);
1238 #if 0 /*XLATE_IMMEDIATELY*/
1239 if (debug_output) {
1240 printf(" %s Writing init thread definition\n", watchvar);
1241 }
1242 emit->static_init_thread_vars(
1243 dec[i]);
1244 #endif
1245 } else {
1246 if (debug_output) {
1247 printf(" %s Appending to init list\n", watchvar);
1248 }
1249 append_decl(
1250 &static_init_vars,
1251 &siv_tail,
1252 dec[i]);
1253 #if 0 /*XLATE_IMMEDIATELY*/
1254 if (!IS_FUNCNAME(dec[i]->dtype->flags)) {
1255 if (debug_output) {
1256 printf(" %s Writing init definition\n", watchvar);
1257 }
1258 emit->static_init_vars(
1259 dec[i]);
1260 }
1261 #endif
1262 }
1263 } else {
1264 if (IS_THREAD(dec[i]->dtype->flags)) {
1265 if (debug_output) {
1266 printf(" %s Appending to uninit thread list\n", watchvar);
1267 }
1268 append_decl(
1269 &static_uninit_thread_vars,
1270 &siutv_tail,
1271 dec[i]);
1272 } else {
1273 if (debug_output) {
1274 printf(" %s Appending to uninit list\n", watchvar);
1275 }
1276 append_decl(
1277 &static_uninit_vars,
1278 &siuv_tail,
1279 dec[i]);
1280 }
1281 }
1282 }
1283
1284 /*
1285 * 03/23/08: Handle global declarations if already
1286 * possible
1287 */
1288 #if XLATE_IMMEDIATELY
1289 if (destdec == &s->extern_decls) {
1290 } else if (destdec == &s->static_decls
1291 && s == &global_scope
1292 && (dec[i]->init != NULL
1293 || dec[i]->was_not_extern)) {
1294 if (debug_output) {
1295 printf(" %s Writing static global decl? %s\n",
1296 watchvar, dec[i]->has_symbol? "NO": "YES");
1297 }
1298 if (sysflag != OS_OSX)
1299 emit->global_static_decls(&dec[i], 1);
1300 }
1301 #endif
1302 /*
1303 * NOTE: A declaration need not have a name to be
1304 * valid! (e.g. anonymous unions and bitfields)
1305 */
1306 if (debug_output) {
1307 printf(" %s Appending to symbol list of %p (global=%p)\n",
1308 watchvar, s, &global_scope);
1309 }
1310 if (0) { /*dec[i]->dtype->tbit != NULL
1311 && dec[i]->dtype->name == NULL) {*/
1312 /*
1313 * 07/17/08: Don't append unnamed bitfield
1314 * members to the symbol list! (Otherwise
1315 * they are also used to match initializers,
1316 * which is wrong
1317 *
1318 * 09/20/08: WRONG, we have to store them
1319 * anyway so that storage allocation works
1320 * as expected (otherwise unnamed delcarations
1321 * have no effect at all)
1322 */
1323 ;
1324 } else {
1325 append_symlist(s,
1326 &s->slist, &s->slist_tail, dec[i]);
1327 }
1328 }
1329
1330 if (destdec) {
1331 if (dec[i]->dtype->storage == TOK_KEY_STATIC
1332 && !dec[i]->dtype->is_func
1333 && dec[i]->asmname == NULL) {
1334 char *newname;
1335 size_t len = strlen(dec[i]->dtype->name);
1336
1337 /*
1338 * Change variable name to avoid name clashes
1339 * with other static variables. The old name
1340 * will be kept in the symbol list. And since
1341 * all lookups go through this list, the
1342 * identifier is still visible as what it was
1343 * originally called
1344 */
1345 len += sizeof "_Static_ " + 8;
1346 newname = n_xmalloc(len+sizeof "_Static_"+8);
1347 sprintf(newname, "_Static_%s%lu",
1348 dec[i]->dtype->name, s->scopeno);
1349 dec[i]->dtype->name = newname;
1350 } else if (dec[i]->dtype->storage == TOK_KEY_EXTERN
1351 || dec[i]->dtype->storage == TOK_KEY_STATIC
1352 || dec[i]->dtype->is_func) {
1353 if (dec[i]->asmname != NULL) {
1354 dec[i]->dtype->name = dec[i]->asmname;
1355 dec[i]->dtype->flags |= FLAGS_ASM_RENAMED;
1356 }
1357 } else if (dec[i]->dtype->storage == TOK_KEY_EXTERN) {
1358 /*
1359 * 02/14/09: For OSX: Symbols that are renamed using
1360 * __asm__ need to get this recorded so that symbol
1361 * exports do not export the extra underscore
1362 */
1363 if (dec[i]->asmname != NULL) {
1364 dec[i]->dtype->name = dec[i]->asmname;
1365 dec[i]->dtype->flags |= FLAGS_ASM_RENAMED;
1366 }
1367 }
1368 do_store_decl(s, destdec, dec[i]);
1369 }
1370 }
1371 }
1372
1373
1374 void
complete_type(struct ty_struct * oldts,struct ty_struct * ts)1375 complete_type(struct ty_struct *oldts, struct ty_struct *ts) {
1376 struct ty_struct *oldlink;
1377 int oldrefs;
1378
1379 oldlink = oldts->next;
1380 oldrefs = oldts->references;
1381 if (ts->incomplete) {
1382 return;
1383 }
1384 memcpy(oldts, ts, sizeof *ts);
1385 oldts->next = oldlink;
1386 oldts->incomplete = 0;
1387 oldts->references =
1388 oldrefs + ts->references;
1389 }
1390
1391 /*
1392 * Stores structure or enum definition (not instance!) in current scope.
1393 * If ts is not a null pointer, it will be stored. Otherwise, te will be
1394 * stored
1395 */
1396 void
store_def_scope(struct scope * sc,struct ty_struct * ts,struct ty_enum * te,struct token * tok)1397 store_def_scope(struct scope *sc,
1398 struct ty_struct *ts,
1399 struct ty_enum *te,
1400 struct token *tok) {
1401 int i;
1402 char *p;
1403 struct sd *s = NULL;
1404 struct ed *e = NULL;
1405
1406 if (ts != NULL) {
1407 /*
1408 * We must first check whether we are completing an incomplete
1409 * structure, i.e. one that was originally declared using e.g.
1410 * ``struct foo;'' or ``typedef struct foo bar;''
1411 */
1412 struct scope *destscope = curscope;
1413
1414 while (destscope->type == SCOPE_STRUCT) {
1415 destscope = destscope->parent;
1416 }
1417
1418 ts->parentscope = destscope;
1419 if (ts->tag != NULL) {
1420 struct ty_struct *oldts;
1421
1422 if ((oldts = lookup_struct(destscope, ts->tag, 1))
1423 != NULL) {
1424 if (oldts->incomplete) {
1425 complete_type(oldts, ts);
1426 return;
1427 } else if (destscope
1428 == oldts->parentscope) {
1429 if (!ts->incomplete) {
1430 errorfl(tok->prev,
1431 "Multiple definitions "
1432 "of structure or "
1433 "union `%s'", ts->tag);
1434 }
1435 return;
1436 }
1437 }
1438 } else {
1439 /*
1440 * 07/17/08: Structure has no tag! So we create a dummy
1441 * tag for it. This is needed for __builtin_offsetof();
1442 *
1443 * __builtin_offsetof(struct { int nonsense; } ...
1444 *
1445 * ... is allowed by gcc. But since we internally
1446 * rewrite this as
1447 *
1448 * &((struct type *)0)->....
1449 *
1450 * ... we need a struct tag! Thus use the dummy one
1451 */
1452 static unsigned long dummycount;
1453 char buf[128];
1454
1455 sprintf(buf, "__dummytag%lu", dummycount++);
1456 ts->dummytag = n_xstrdup(buf);
1457 }
1458
1459 /* Completely new structure type */
1460 s = &destscope->struct_defs;
1461 if (s->head == NULL) {
1462 s->head = s->tail = ts;
1463 } else {
1464 s->tail->next = ts;
1465 s->tail = s->tail->next;
1466 }
1467 ++s->ndecls;
1468 #ifdef DEBUG2
1469 printf("stored structure with tag %s\n", ts->tag);
1470 #endif
1471 } else if (te == NULL) {
1472 puts("Fatal error in store_def_scope - te = 0");
1473 abort();
1474 } else {
1475 struct scope *destscope = curscope;
1476
1477 while (destscope->type == SCOPE_STRUCT) {
1478 destscope = destscope->parent;
1479 }
1480
1481 e = & /*sc*/ destscope->enum_defs;
1482 if (te->tag != NULL) {
1483 /*
1484 * Check whether we have a multiple definition error.
1485 * It is not necessary to check for incomplete types,
1486 * as with structures, because enum forward references
1487 * are invalid (XXX might be implemented as extension
1488 * later)
1489 */
1490 for (i = 0; i < e->ndecls; ++i) {
1491 p = e->data[i]->tag;
1492 if (p && strcmp(p, te->tag) == 0) {
1493 errorfl(tok,
1494 "Multiple definitions of "
1495 "enum `%s'", p);
1496 return;
1497 }
1498 }
1499 if (lookup_struct(sc, te->tag, 0) != NULL) {
1500 errorfl(tok,
1501 "Multiple definitions of structure `%s'",
1502 te->tag);
1503 }
1504 }
1505
1506 if (e->ndecls >= e->nslots) {
1507 if (e->nslots == 0) {
1508 e->nslots = 1;
1509 }
1510 e->nslots *= 2;
1511 e->data = n_xrealloc(e->data,
1512 e->nslots * sizeof *e->data);
1513 }
1514 e->data[ e->ndecls++ ] = te;
1515 }
1516 }
1517
1518 struct decl *
lookup_symbol(struct scope * s,const char * name,int nested)1519 lookup_symbol(struct scope *s, const char *name, int nested) {
1520 struct sym_entry *se;
1521
1522 se = lookup_symbol_se(s, name, nested);
1523 if (se == NULL) {
1524 return NULL;
1525 }
1526 return se->dec;
1527 }
1528
1529 struct /*decl*/ sym_entry *
lookup_symbol_se(struct scope * s,const char * name,int nested)1530 lookup_symbol_se(struct scope *s, const char *name, int nested) {
1531 size_t len;
1532
1533 len = strlen(name);
1534 #if FAST_SYMBOL_LOOKUP
1535 if (s->type != SCOPE_STRUCT) {
1536 return fast_lookup_symbol_se(s, name, nested, 0);
1537 }
1538 #endif
1539
1540 do {
1541 struct sym_entry *se;
1542
1543 #if ! FAST_SYMBOL_LOOKUP
1544 if (!s->sym_hash.used)
1545 #endif
1546 {
1547 /* Linear scan */
1548 for (se = s->slist; se != NULL; se = se->next) {
1549 /* 04/08/08: Shadow declarations */
1550 if (se->dec->invalid
1551 && !is_shadow_decl(se->dec)) {
1552 continue;
1553 }
1554 if (!se->inactive && se->namelen == len) {
1555 if (se->name[0] == name[0]) {
1556 if (strcmp(&se->name[1],
1557 &name[1]) == 0) {
1558 return se /*->dec*/;
1559 }
1560 }
1561 }
1562 }
1563 }
1564 #if ! FAST_SYMBOL_LOOKUP
1565 else {
1566 /* Hash lookup (key is length) */
1567 #if 0
1568 se = lookup_hash(s->sym_hash,
1569 s->n_hash_slots, name, len);
1570 #endif
1571 se = new_lookup_hash(&s->sym_hash, name, len);
1572 if (se != NULL) {
1573 return se /*->dec*/;
1574 }
1575 }
1576 #endif
1577
1578 if (nested == 0) {
1579 break;
1580 }
1581 } while ((s = s->parent) != NULL);
1582
1583 return NULL;
1584 }
1585
1586
1587 struct decl *
access_symbol(struct scope * s,const char * name,int nested)1588 access_symbol(struct scope *s, const char *name, int nested) {
1589 struct decl *ret;
1590
1591 if ((ret = lookup_symbol(s, name, nested)) != NULL) {
1592 if (ret->is_alias) {
1593 ret = ret->is_alias;
1594 }
1595 ++ret->references;
1596 if (ret->dtype->tstruc != NULL) {
1597 ++ret->dtype->tstruc->references;
1598 }
1599 }
1600 return ret;
1601 }
1602
1603 /*
1604 * 12/25/08: Mark a (static) variable as really used, modulo
1605 * optimization. This is for architectures like PPC where we
1606 * aren't allowed to declare UNUSED extern declarations. For
1607 * example,
1608 *
1609 * extern int foo;
1610 * 0? foo: 123;
1611 *
1612 * ... is expected to work at least by glibc even if foo doesn't
1613 * exist. The access must be optimized away.
1614 *
1615 * On PPC this will cause a linker error even if we don't access
1616 * foo directly because we still create a TOC entry for the
1617 * undefined symbol. Instead of undoing reference increments
1618 * after having found that an access can be optimized away, we
1619 * now use a reference count of ACTUAL access in the emitter.
1620 */
1621 void
really_accessed(struct decl * d)1622 really_accessed(struct decl *d) {
1623 if (d->is_alias != NULL) {
1624 /*
1625 * This is an alias - increment actual item (e.g.
1626 * __func__ for __PRETTY_FUNCTION__
1627 */
1628 d = d->is_alias;
1629 }
1630 ++d->real_references;
1631 }
1632
1633
1634 struct decl *
put_implicit(const char * name)1635 put_implicit(const char *name) {
1636 struct decl *d[2];
1637 struct type *ty;
1638 struct type_node *tnode;
1639
1640 d[0] = alloc_decl();
1641 ty = alloc_type();
1642 ty->name = (char *)name;
1643 ty->code = TY_INT;
1644 ty->storage = TOK_KEY_EXTERN;
1645 ty->is_func = 1;
1646 ty->implicit = IMPLICIT_FDECL;
1647 ty->sign = TOK_KEY_SIGNED;
1648 tnode = alloc_type_node();
1649 tnode->type = TN_FUNCTION;
1650 tnode->tfunc = alloc_ty_func();
1651 tnode->tfunc->scope = NULL;
1652 tnode->tfunc->nargs = -1;
1653 tnode->tfunc->ret = NULL;
1654 tnode->tfunc->type = FDTYPE_ISO;
1655 ty->tlist = tnode;
1656
1657 d[0]->dtype = ty;
1658 d[0]->references = 1;
1659 d[0]->real_references = 0; /* 12/24/08 */
1660 d[1] = NULL;
1661
1662 store_decl_scope(&global_scope, d);
1663 return d[0];
1664 }
1665
1666