|
|
1.1 root 1: /* Process declarations and variables for C compiler.
2: Copyright (C) 1988, 1992 Free Software Foundation, Inc.
3:
4: This file is part of GNU CC.
5:
6: GNU CC is free software; you can redistribute it and/or modify
7: it under the terms of the GNU General Public License as published by
8: the Free Software Foundation; either version 2, or (at your option)
9: any later version.
10:
11: GNU CC is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with GNU CC; see the file COPYING. If not, write to
18: the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19:
20:
21: /* Process declarations and symbol lookup for C front end.
22: Also constructs types; the standard scalar types at initialization,
23: and structure, union, array and enum types when they are declared. */
24:
25: /* ??? not all decl nodes are given the most useful possible
26: line numbers. For example, the CONST_DECLs for enum values. */
27:
28: #include "config.h"
29: #include "tree.h"
30: #include "flags.h"
31: #include "c-tree.h"
32: #include "c-lex.h"
33: #include <stdio.h>
34:
35: /* In grokdeclarator, distinguish syntactic contexts of declarators. */
36: enum decl_context
37: { NORMAL, /* Ordinary declaration */
38: FUNCDEF, /* Function definition */
39: PARM, /* Declaration of parm before function body */
40: FIELD, /* Declaration inside struct or union */
41: BITFIELD, /* Likewise but with specified width */
42: TYPENAME}; /* Typename (inside cast or sizeof) */
43:
44: #undef NULL
45: #define NULL 0
46:
47: #ifndef CHAR_TYPE_SIZE
48: #define CHAR_TYPE_SIZE BITS_PER_UNIT
49: #endif
50:
51: #ifndef SHORT_TYPE_SIZE
52: #define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
53: #endif
54:
55: #ifndef INT_TYPE_SIZE
56: #define INT_TYPE_SIZE BITS_PER_WORD
57: #endif
58:
59: #ifndef LONG_TYPE_SIZE
60: #define LONG_TYPE_SIZE BITS_PER_WORD
61: #endif
62:
63: #ifndef LONG_LONG_TYPE_SIZE
64: #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
65: #endif
66:
67: #ifndef WCHAR_UNSIGNED
68: #define WCHAR_UNSIGNED 0
69: #endif
70:
71: #ifndef FLOAT_TYPE_SIZE
72: #define FLOAT_TYPE_SIZE BITS_PER_WORD
73: #endif
74:
75: #ifndef DOUBLE_TYPE_SIZE
76: #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
77: #endif
78:
79: #ifndef LONG_DOUBLE_TYPE_SIZE
80: #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
81: #endif
82:
83: /* We let tm.h override the types used here, to handle trivial differences
84: such as the choice of unsigned int or long unsigned int for size_t.
85: When machines start needing nontrivial differences in the size type,
86: it would be best to do something here to figure out automatically
87: from other information what type to use. */
88:
89: #ifndef SIZE_TYPE
90: #define SIZE_TYPE "long unsigned int"
91: #endif
92:
93: #ifndef PTRDIFF_TYPE
94: #define PTRDIFF_TYPE "long int"
95: #endif
96:
97: #ifndef WCHAR_TYPE
98: #define WCHAR_TYPE "int"
99: #endif
100:
101: /* a node which has tree code ERROR_MARK, and whose type is itself.
102: All erroneous expressions are replaced with this node. All functions
103: that accept nodes as arguments should avoid generating error messages
104: if this node is one of the arguments, since it is undesirable to get
105: multiple error messages from one error in the input. */
106:
107: tree error_mark_node;
108:
109: /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
110:
111: tree short_integer_type_node;
112: tree integer_type_node;
113: tree long_integer_type_node;
114: tree long_long_integer_type_node;
115:
116: tree short_unsigned_type_node;
117: tree unsigned_type_node;
118: tree long_unsigned_type_node;
119: tree long_long_unsigned_type_node;
120:
121: tree ptrdiff_type_node;
122:
123: tree unsigned_char_type_node;
124: tree signed_char_type_node;
125: tree char_type_node;
126: tree wchar_type_node;
127: tree signed_wchar_type_node;
128: tree unsigned_wchar_type_node;
129:
130: tree float_type_node;
131: tree double_type_node;
132: tree long_double_type_node;
133:
134: /* a VOID_TYPE node. */
135:
136: tree void_type_node;
137:
138: /* Nodes for types `void *' and `const void *'. */
139:
140: tree ptr_type_node, const_ptr_type_node;
141:
142: /* Nodes for types `char *' and `const char *'. */
143:
144: tree string_type_node, const_string_type_node;
145:
146: /* Type `char[256]' or something like it.
147: Used when an array of char is needed and the size is irrelevant. */
148:
149: tree char_array_type_node;
150:
151: /* Type `int[256]' or something like it.
152: Used when an array of int needed and the size is irrelevant. */
153:
154: tree int_array_type_node;
155:
156: /* Type `wchar_t[256]' or something like it.
157: Used when a wide string literal is created. */
158:
159: tree wchar_array_type_node;
160:
161: /* type `int ()' -- used for implicit declaration of functions. */
162:
163: tree default_function_type;
164:
165: /* function types `double (double)' and `double (double, double)', etc. */
166:
167: tree double_ftype_double, double_ftype_double_double;
168: tree int_ftype_int, long_ftype_long;
169:
170: /* Function type `void (void *, void *, int)' and similar ones */
171:
172: tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
173:
174: /* Function type `char *(char *, char *)' and similar ones */
175: tree string_ftype_ptr_ptr, int_ftype_string_string;
176:
177: /* Function type `int (const void *, const void *, size_t)' */
178: tree int_ftype_cptr_cptr_sizet;
179:
180: /* Two expressions that are constants with value zero.
181: The first is of type `int', the second of type `void *'. */
182:
183: tree integer_zero_node;
184: tree null_pointer_node;
185:
186: /* A node for the integer constant 1. */
187:
188: tree integer_one_node;
189:
190: /* Nonzero if we have seen an invalid cross reference
191: to a struct, union, or enum, but not yet printed the message. */
192:
193: tree pending_invalid_xref;
194: /* File and line to appear in the eventual error message. */
195: char *pending_invalid_xref_file;
196: int pending_invalid_xref_line;
197:
198: /* While defining an enum type, this is 1 plus the last enumerator
199: constant value. */
200:
201: static tree enum_next_value;
202:
1.1.1.3 ! root 203: /* Nonzero means that there was overflow computing enum_next_value. */
! 204:
! 205: static int enum_overflow;
! 206:
1.1 root 207: /* Parsing a function declarator leaves a list of parameter names
208: or a chain or parameter decls here. */
209:
210: static tree last_function_parms;
211:
212: /* Parsing a function declarator leaves here a chain of structure
213: and enum types declared in the parmlist. */
214:
215: static tree last_function_parm_tags;
216:
217: /* After parsing the declarator that starts a function definition,
218: `start_function' puts here the list of parameter names or chain of decls.
219: `store_parm_decls' finds it here. */
220:
221: static tree current_function_parms;
222:
223: /* Similar, for last_function_parm_tags. */
224: static tree current_function_parm_tags;
225:
226: /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
227: that have names. Here so we can clear out their names' definitions
228: at the end of the function. */
229:
230: tree named_labels;
231:
232: /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
233:
234: static tree shadowed_labels;
235:
236: /* Nonzero when store_parm_decls is called indicates a varargs function.
237: Value not meaningful after store_parm_decls. */
238:
239: static int c_function_varargs;
240:
241: /* The FUNCTION_DECL for the function currently being compiled,
242: or 0 if between functions. */
243: tree current_function_decl;
244:
245: /* Set to 0 at beginning of a function definition, set to 1 if
246: a return statement that specifies a return value is seen. */
247:
248: int current_function_returns_value;
249:
250: /* Set to 0 at beginning of a function definition, set to 1 if
251: a return statement with no argument is seen. */
252:
253: int current_function_returns_null;
254:
255: /* Set to nonzero by `grokdeclarator' for a function
256: whose return type is defaulted, if warnings for this are desired. */
257:
258: static int warn_about_return_type;
259:
1.1.1.2 root 260: /* Nonzero when starting a function declared `extern inline'. */
1.1 root 261:
262: static int current_extern_inline;
263:
264: /* For each binding contour we allocate a binding_level structure
265: * which records the names defined in that contour.
266: * Contours include:
267: * 0) the global one
268: * 1) one for each function definition,
269: * where internal declarations of the parameters appear.
270: * 2) one for each compound statement,
271: * to record its declarations.
272: *
273: * The current meaning of a name can be found by searching the levels from
274: * the current one out to the global one.
275: */
276:
277: /* Note that the information in the `names' component of the global contour
278: is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
279:
280: struct binding_level
281: {
282: /* A chain of _DECL nodes for all variables, constants, functions,
283: and typedef types. These are in the reverse of the order supplied.
284: */
285: tree names;
286:
287: /* A list of structure, union and enum definitions,
288: * for looking up tag names.
289: * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
290: * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
291: * or ENUMERAL_TYPE node.
292: */
293: tree tags;
294:
295: /* For each level, a list of shadowed outer-level local definitions
296: to be restored when this level is popped.
297: Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
298: whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
299: tree shadowed;
300:
301: /* For each level (except not the global one),
302: a chain of BLOCK nodes for all the levels
303: that were entered and exited one level down. */
304: tree blocks;
305:
306: /* The binding level which this one is contained in (inherits from). */
307: struct binding_level *level_chain;
308:
309: /* Nonzero for the level that holds the parameters of a function. */
310: /* 2 for a definition, 1 for a declaration. */
311: char parm_flag;
312:
313: /* Nonzero if this level "doesn't exist" for tags. */
314: char tag_transparent;
315:
316: /* Nonzero if sublevels of this level "don't exist" for tags.
317: This is set in the parm level of a function definition
318: while reading the function body, so that the outermost block
319: of the function body will be tag-transparent. */
320: char subblocks_tag_transparent;
321:
322: /* Nonzero means make a BLOCK for this level regardless of all else. */
323: char keep;
324:
325: /* Nonzero means make a BLOCK if this level has any subblocks. */
326: char keep_if_subblocks;
327:
328: /* Number of decls in `names' that have incomplete
329: structure or union types. */
330: int n_incomplete;
331:
332: /* A list of decls giving the (reversed) specified order of parms,
333: not including any forward-decls in the parmlist.
334: This is so we can put the parms in proper order for assign_parms. */
335: tree parm_order;
336: };
337:
338: #define NULL_BINDING_LEVEL (struct binding_level *) NULL
339:
340: /* The binding level currently in effect. */
341:
342: static struct binding_level *current_binding_level;
343:
344: /* A chain of binding_level structures awaiting reuse. */
345:
346: static struct binding_level *free_binding_level;
347:
348: /* The outermost binding level, for names of file scope.
349: This is created when the compiler is started and exists
350: through the entire run. */
351:
352: static struct binding_level *global_binding_level;
353:
354: /* Binding level structures are initialized by copying this one. */
355:
356: static struct binding_level clear_binding_level
357: = {NULL, NULL, NULL, NULL, NULL, 0, 0, 0};
358:
359: /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
360:
361: static int keep_next_level_flag;
362:
363: /* Nonzero means make a BLOCK for the next level pushed
364: if it has subblocks. */
365:
366: static int keep_next_if_subblocks;
367:
368: /* The chain of outer levels of label scopes.
369: This uses the same data structure used for binding levels,
370: but it works differently: each link in the chain records
371: saved values of named_labels and shadowed_labels for
372: a label binding level outside the current one. */
373:
374: static struct binding_level *label_level_chain;
375:
376: /* Forward declarations. */
377:
378: static tree grokparms (), grokdeclarator ();
379: tree pushdecl ();
1.1.1.2 root 380: tree builtin_function ();
1.1 root 381:
382: static tree lookup_tag ();
383: static tree lookup_tag_reverse ();
384: static tree lookup_name_current_level ();
385: static char *redeclaration_error_message ();
386: static void layout_array_type ();
387:
388: /* C-specific option variables. */
389:
390: /* Nonzero means allow type mismatches in conditional expressions;
391: just make their values `void'. */
392:
393: int flag_cond_mismatch;
394:
395: /* Nonzero means give `double' the same size as `float'. */
396:
397: int flag_short_double;
398:
399: /* Nonzero means don't recognize the keyword `asm'. */
400:
401: int flag_no_asm;
402:
1.1.1.3 ! root 403: /* Nonzero means don't recognize any builtin functions. */
1.1 root 404:
405: int flag_no_builtin;
406:
1.1.1.3 ! root 407: /* Nonzero means don't recognize the non-ANSI builtin functions.
! 408: -ansi sets this. */
! 409:
! 410: int flag_no_nonansi_builtin;
! 411:
1.1 root 412: /* Nonzero means do some things the same way PCC does. */
413:
414: int flag_traditional;
415:
416: /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
417:
418: int flag_signed_bitfields = 1;
419:
420: /* Nonzero means handle `#ident' directives. 0 means ignore them. */
421:
422: int flag_no_ident = 0;
423:
424: /* Nonzero means warn about implicit declarations. */
425:
426: int warn_implicit;
427:
428: /* Nonzero means give string constants the type `const char *'
429: to get extra warnings from them. These warnings will be too numerous
430: to be useful, except in thoroughly ANSIfied programs. */
431:
432: int warn_write_strings;
433:
434: /* Nonzero means warn about pointer casts that can drop a type qualifier
435: from the pointer target type. */
436:
437: int warn_cast_qual;
438:
439: /* Warn about traditional constructs whose meanings changed in ANSI C. */
440:
441: int warn_traditional;
442:
443: /* Nonzero means warn about sizeof(function) or addition/subtraction
444: of function pointers. */
445:
446: int warn_pointer_arith;
447:
448: /* Nonzero means warn for non-prototype function decls
449: or non-prototyped defs without previous prototype. */
450:
451: int warn_strict_prototypes;
452:
453: /* Nonzero means warn for any global function def
454: without separate previous prototype decl. */
455:
456: int warn_missing_prototypes;
457:
458: /* Nonzero means warn about multiple (redundant) decls for the same single
459: variable or function. */
460:
461: int warn_redundant_decls = 0;
462:
463: /* Nonzero means warn about extern declarations of objects not at
464: file-scope level and about *all* declarations of functions (whether
465: extern or static) not at file-scope level. Note that we exclude
466: implicit function declarations. To get warnings about those, use
467: -Wimplicit. */
468:
469: int warn_nested_externs = 0;
470:
471: /* Warn about *printf or *scanf format/argument anomalies. */
472:
473: int warn_format;
474:
475: /* Warn about a subscript that has type char. */
476:
477: int warn_char_subscripts = 0;
478:
479: /* Warn if a type conversion is done that might have confusing results. */
480:
481: int warn_conversion;
482:
483: /* Warn if adding () is suggested. */
484:
1.1.1.2 root 485: int warn_parentheses;
1.1 root 486:
487: /* Nonzero means `$' can be in an identifier.
488: See cccp.c for reasons why this breaks some obscure ANSI C programs. */
489:
490: #ifndef DOLLARS_IN_IDENTIFIERS
491: #define DOLLARS_IN_IDENTIFIERS 1
492: #endif
493: int dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 1;
494:
495: char *language_string = "GNU C";
496:
497: /* Decode the string P as a language-specific option for C.
498: Return 1 if it is recognized (and handle it);
499: return 0 if not recognized. */
500:
501: int
502: c_decode_option (p)
503: char *p;
504: {
505: if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
506: {
507: flag_traditional = 1;
508: flag_writable_strings = 1;
509: #if DOLLARS_IN_IDENTIFIERS > 0
510: dollars_in_ident = 1;
511: #endif
512: }
513: else if (!strcmp (p, "-fnotraditional"))
514: ;
515: else if (!strcmp (p, "-fsigned-char"))
516: flag_signed_char = 1;
517: else if (!strcmp (p, "-funsigned-char"))
518: flag_signed_char = 0;
519: else if (!strcmp (p, "-fno-signed-char"))
520: flag_signed_char = 0;
521: else if (!strcmp (p, "-fno-unsigned-char"))
522: flag_signed_char = 1;
523: else if (!strcmp (p, "-fsigned-bitfields"))
524: flag_signed_bitfields = 1;
525: else if (!strcmp (p, "-funsigned-bitfields"))
526: flag_signed_bitfields = 0;
527: else if (!strcmp (p, "-fno-signed-bitfields"))
528: flag_signed_bitfields = 0;
529: else if (!strcmp (p, "-fno-unsigned-bitfields"))
530: flag_signed_bitfields = 1;
531: else if (!strcmp (p, "-fshort-enums"))
532: flag_short_enums = 1;
533: else if (!strcmp (p, "-fno-short-enums"))
534: flag_short_enums = 0;
535: else if (!strcmp (p, "-fcond-mismatch"))
536: flag_cond_mismatch = 1;
537: else if (!strcmp (p, "-fno-cond-mismatch"))
538: flag_cond_mismatch = 0;
539: else if (!strcmp (p, "-fshort-double"))
540: flag_short_double = 1;
541: else if (!strcmp (p, "-fno-short-double"))
542: flag_short_double = 0;
543: else if (!strcmp (p, "-fasm"))
544: flag_no_asm = 0;
545: else if (!strcmp (p, "-fno-asm"))
546: flag_no_asm = 1;
547: else if (!strcmp (p, "-fbuiltin"))
548: flag_no_builtin = 0;
549: else if (!strcmp (p, "-fno-builtin"))
550: flag_no_builtin = 1;
551: else if (!strcmp (p, "-fno-ident"))
552: flag_no_ident = 1;
553: else if (!strcmp (p, "-fident"))
554: flag_no_ident = 0;
555: else if (!strcmp (p, "-ansi"))
1.1.1.3 ! root 556: flag_no_asm = 1, flag_no_nonansi_builtin = 1, dollars_in_ident = 0;
1.1 root 557: else if (!strcmp (p, "-Wimplicit"))
558: warn_implicit = 1;
559: else if (!strcmp (p, "-Wno-implicit"))
560: warn_implicit = 0;
561: else if (!strcmp (p, "-Wwrite-strings"))
562: warn_write_strings = 1;
563: else if (!strcmp (p, "-Wno-write-strings"))
564: warn_write_strings = 0;
565: else if (!strcmp (p, "-Wcast-qual"))
566: warn_cast_qual = 1;
567: else if (!strcmp (p, "-Wno-cast-qual"))
568: warn_cast_qual = 0;
569: else if (!strcmp (p, "-Wpointer-arith"))
570: warn_pointer_arith = 1;
571: else if (!strcmp (p, "-Wno-pointer-arith"))
572: warn_pointer_arith = 0;
573: else if (!strcmp (p, "-Wstrict-prototypes"))
574: warn_strict_prototypes = 1;
575: else if (!strcmp (p, "-Wno-strict-prototypes"))
576: warn_strict_prototypes = 0;
577: else if (!strcmp (p, "-Wmissing-prototypes"))
578: warn_missing_prototypes = 1;
579: else if (!strcmp (p, "-Wno-missing-prototypes"))
580: warn_missing_prototypes = 0;
581: else if (!strcmp (p, "-Wredundant-decls"))
582: warn_redundant_decls = 1;
1.1.1.3 ! root 583: else if (!strcmp (p, "-Wno-redundant-decls"))
1.1 root 584: warn_redundant_decls = 0;
585: else if (!strcmp (p, "-Wnested-externs"))
586: warn_nested_externs = 1;
587: else if (!strcmp (p, "-Wno-nested-externs"))
588: warn_nested_externs = 0;
589: else if (!strcmp (p, "-Wtraditional"))
590: warn_traditional = 1;
591: else if (!strcmp (p, "-Wno-traditional"))
592: warn_traditional = 0;
593: else if (!strcmp (p, "-Wformat"))
594: warn_format = 1;
595: else if (!strcmp (p, "-Wno-format"))
596: warn_format = 0;
597: else if (!strcmp (p, "-Wchar-subscripts"))
598: warn_char_subscripts = 1;
599: else if (!strcmp (p, "-Wno-char-subscripts"))
600: warn_char_subscripts = 0;
601: else if (!strcmp (p, "-Wconversion"))
602: warn_conversion = 1;
603: else if (!strcmp (p, "-Wno-conversion"))
604: warn_conversion = 0;
605: else if (!strcmp (p, "-Wparentheses"))
606: warn_parentheses = 1;
607: else if (!strcmp (p, "-Wno-parentheses"))
608: warn_parentheses = 0;
609: else if (!strcmp (p, "-Wcomment"))
610: ; /* cpp handles this one. */
611: else if (!strcmp (p, "-Wno-comment"))
612: ; /* cpp handles this one. */
613: else if (!strcmp (p, "-Wcomments"))
614: ; /* cpp handles this one. */
615: else if (!strcmp (p, "-Wno-comments"))
616: ; /* cpp handles this one. */
617: else if (!strcmp (p, "-Wtrigraphs"))
618: ; /* cpp handles this one. */
619: else if (!strcmp (p, "-Wno-trigraphs"))
620: ; /* cpp handles this one. */
1.1.1.3 ! root 621: else if (!strcmp (p, "-Wimport"))
! 622: ; /* cpp handles this one. */
! 623: else if (!strcmp (p, "-Wno-import"))
! 624: ; /* cpp handles this one. */
1.1 root 625: else if (!strcmp (p, "-Wall"))
626: {
627: extra_warnings = 1;
628: warn_uninitialized = 1;
629: warn_implicit = 1;
630: warn_return_type = 1;
631: warn_unused = 1;
632: warn_switch = 1;
633: warn_format = 1;
634: warn_char_subscripts = 1;
1.1.1.2 root 635: warn_parentheses = 1;
1.1 root 636: }
637: else
638: return 0;
639:
640: return 1;
641: }
642:
643: /* Hooks for print_node. */
644:
645: void
646: print_lang_decl ()
647: {
648: }
649:
650: void
651: print_lang_type ()
652: {
653: }
654:
655: void
656: print_lang_identifier (file, node, indent)
657: FILE *file;
658: tree node;
659: int indent;
660: {
661: print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4);
662: print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4);
663: print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
664: print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4);
665: print_node (file, "error locus", IDENTIFIER_ERROR_LOCUS (node), indent + 4);
666: }
667:
668: /* Create a new `struct binding_level'. */
669:
670: static
671: struct binding_level *
672: make_binding_level ()
673: {
674: /* NOSTRICT */
675: return (struct binding_level *) xmalloc (sizeof (struct binding_level));
676: }
677:
678: /* Nonzero if we are currently in the global binding level. */
679:
680: int
681: global_bindings_p ()
682: {
683: return current_binding_level == global_binding_level;
684: }
685:
686: void
687: keep_next_level ()
688: {
689: keep_next_level_flag = 1;
690: }
691:
692: /* Nonzero if the current level needs to have a BLOCK made. */
693:
694: int
695: kept_level_p ()
696: {
697: return ((current_binding_level->keep_if_subblocks
698: && current_binding_level->blocks != 0)
699: || current_binding_level->keep
700: || current_binding_level->names != 0
701: || (current_binding_level->tags != 0
702: && !current_binding_level->tag_transparent));
703: }
704:
705: /* Identify this binding level as a level of parameters.
706: DEFINITION_FLAG is 1 for a definition, 0 for a declaration. */
707:
708: void
709: declare_parm_level (definition_flag)
710: int definition_flag;
711: {
712: current_binding_level->parm_flag = 1 + definition_flag;
713: }
714:
715: /* Nonzero if currently making parm declarations. */
716:
717: int
718: in_parm_level_p ()
719: {
720: return current_binding_level->parm_flag;
721: }
722:
723: /* Enter a new binding level.
724: If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
725: not for that of tags. */
726:
727: void
728: pushlevel (tag_transparent)
729: int tag_transparent;
730: {
731: register struct binding_level *newlevel = NULL_BINDING_LEVEL;
732:
733: /* If this is the top level of a function,
734: just make sure that NAMED_LABELS is 0. */
735:
736: if (current_binding_level == global_binding_level)
737: {
738: named_labels = 0;
739: }
740:
741: /* Reuse or create a struct for this binding level. */
742:
743: if (free_binding_level)
744: {
745: newlevel = free_binding_level;
746: free_binding_level = free_binding_level->level_chain;
747: }
748: else
749: {
750: newlevel = make_binding_level ();
751: }
752:
753: /* Add this level to the front of the chain (stack) of levels that
754: are active. */
755:
756: *newlevel = clear_binding_level;
757: newlevel->tag_transparent
758: = (tag_transparent
759: || (current_binding_level
760: ? current_binding_level->subblocks_tag_transparent
761: : 0));
762: newlevel->level_chain = current_binding_level;
763: current_binding_level = newlevel;
764: newlevel->keep = keep_next_level_flag;
765: keep_next_level_flag = 0;
766: newlevel->keep_if_subblocks = keep_next_if_subblocks;
767: keep_next_if_subblocks = 0;
768: }
769:
770: /* Exit a binding level.
771: Pop the level off, and restore the state of the identifier-decl mappings
772: that were in effect when this level was entered.
773:
774: If KEEP is nonzero, this level had explicit declarations, so
775: and create a "block" (a BLOCK node) for the level
776: to record its declarations and subblocks for symbol table output.
777:
778: If FUNCTIONBODY is nonzero, this level is the body of a function,
779: so create a block as if KEEP were set and also clear out all
780: label names.
781:
782: If REVERSE is nonzero, reverse the order of decls before putting
783: them into the BLOCK. */
784:
785: tree
786: poplevel (keep, reverse, functionbody)
787: int keep;
788: int reverse;
789: int functionbody;
790: {
791: register tree link;
792: /* The chain of decls was accumulated in reverse order.
793: Put it into forward order, just for cleanliness. */
794: tree decls;
795: tree tags = current_binding_level->tags;
796: tree subblocks = current_binding_level->blocks;
797: tree block = 0;
798: tree decl;
799:
800: keep |= current_binding_level->keep;
801:
802: /* This warning is turned off because it causes warnings for
803: declarations like `extern struct foo *x'. */
804: #if 0
805: /* Warn about incomplete structure types in this level. */
806: for (link = tags; link; link = TREE_CHAIN (link))
807: if (TYPE_SIZE (TREE_VALUE (link)) == 0)
808: {
809: tree type = TREE_VALUE (link);
810: char *errmsg;
811: switch (TREE_CODE (type))
812: {
813: case RECORD_TYPE:
814: errmsg = "`struct %s' incomplete in scope ending here";
815: break;
816: case UNION_TYPE:
817: errmsg = "`union %s' incomplete in scope ending here";
818: break;
819: case ENUMERAL_TYPE:
820: errmsg = "`enum %s' incomplete in scope ending here";
821: break;
822: }
823: if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
824: error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
825: else
826: /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
827: error (errmsg, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
828: }
829: #endif /* 0 */
830:
831: /* Get the decls in the order they were written.
832: Usually current_binding_level->names is in reverse order.
833: But parameter decls were previously put in forward order. */
834:
835: if (reverse)
836: current_binding_level->names
837: = decls = nreverse (current_binding_level->names);
838: else
839: decls = current_binding_level->names;
840:
841: /* Output any nested inline functions within this block
842: if they weren't already output. */
843:
844: for (decl = decls; decl; decl = TREE_CHAIN (decl))
845: if (TREE_CODE (decl) == FUNCTION_DECL
846: && ! TREE_ASM_WRITTEN (decl)
847: && DECL_INITIAL (decl) != 0
848: && TREE_ADDRESSABLE (decl))
849: output_inline_function (decl);
850:
851: /* If there were any declarations or structure tags in that level,
852: or if this level is a function body,
853: create a BLOCK to record them for the life of this function. */
854:
855: if (keep || functionbody
856: || (current_binding_level->keep_if_subblocks && subblocks != 0))
857: block = build_block (keep ? decls : 0, keep ? tags : 0,
858: subblocks, 0, 0);
859:
860: /* In each subblock, record that this is its superior. */
861:
862: for (link = subblocks; link; link = TREE_CHAIN (link))
863: BLOCK_SUPERCONTEXT (link) = block;
864:
865: /* Clear out the meanings of the local variables of this level. */
866:
867: for (link = decls; link; link = TREE_CHAIN (link))
868: {
869: if (DECL_NAME (link) != 0)
870: {
871: /* If the ident. was used or addressed via a local extern decl,
872: don't forget that fact. */
873: if (TREE_EXTERNAL (link))
874: {
875: if (TREE_USED (link))
876: TREE_USED (DECL_NAME (link)) = 1;
877: if (TREE_ADDRESSABLE (link))
878: TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
879: }
880: IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
881: }
882: }
883:
884: /* Restore all name-meanings of the outer levels
885: that were shadowed by this level. */
886:
887: for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
888: IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
889:
890: /* If the level being exited is the top level of a function,
891: check over all the labels, and clear out the current
892: (function local) meanings of their names. */
893:
894: if (functionbody)
895: {
896: /* If this is the top level block of a function,
897: the vars are the function's parameters.
898: Don't leave them in the BLOCK because they are
899: found in the FUNCTION_DECL instead. */
900:
901: BLOCK_VARS (block) = 0;
902:
903: /* Clear out the definitions of all label names,
904: since their scopes end here,
905: and add them to BLOCK_VARS. */
906:
907: for (link = named_labels; link; link = TREE_CHAIN (link))
908: {
909: register tree label = TREE_VALUE (link);
910:
911: if (DECL_INITIAL (label) == 0)
912: {
913: error_with_decl (label, "label `%s' used but not defined");
914: /* Avoid crashing later. */
915: define_label (input_filename, lineno,
916: DECL_NAME (label));
917: }
918: else if (warn_unused && !TREE_USED (label))
919: warning_with_decl (label, "label `%s' defined but not used");
920: IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
921:
922: /* Put the labels into the "variables" of the
923: top-level block, so debugger can see them. */
924: TREE_CHAIN (label) = BLOCK_VARS (block);
925: BLOCK_VARS (block) = label;
926: }
927: }
928:
929: /* Pop the current level, and free the structure for reuse. */
930:
931: {
932: register struct binding_level *level = current_binding_level;
933: current_binding_level = current_binding_level->level_chain;
934:
935: level->level_chain = free_binding_level;
936: free_binding_level = level;
937: }
938:
939: /* Dispose of the block that we just made inside some higher level. */
940: if (functionbody)
941: DECL_INITIAL (current_function_decl) = block;
942: else if (block)
943: current_binding_level->blocks
944: = chainon (current_binding_level->blocks, block);
945: /* If we did not make a block for the level just exited,
946: any blocks made for inner levels
947: (since they cannot be recorded as subblocks in that level)
948: must be carried forward so they will later become subblocks
949: of something else. */
950: else if (subblocks)
951: current_binding_level->blocks
952: = chainon (current_binding_level->blocks, subblocks);
953:
954: /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
955: binding contour so that they point to the appropriate construct, i.e.
956: either to the current FUNCTION_DECL node, or else to the BLOCK node
957: we just constructed.
958:
959: Note that for tagged types whose scope is just the formal parameter
960: list for some function type specification, we can't properly set
961: their TYPE_CONTEXTs here, because we don't have a pointer to the
962: appropriate FUNCTION_TYPE node readily available to us. For those
963: cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
964: in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
965: node which will represent the "scope" for these "parameter list local"
966: tagged types.
967: */
968:
969: if (functionbody)
970: for (link = tags; link; link = TREE_CHAIN (link))
971: TYPE_CONTEXT (TREE_VALUE (link)) = current_function_decl;
972: else if (block)
973: for (link = tags; link; link = TREE_CHAIN (link))
974: TYPE_CONTEXT (TREE_VALUE (link)) = block;
975:
976: if (block)
977: TREE_USED (block) = 1;
978: return block;
979: }
980:
981: void
982: push_label_level ()
983: {
984: register struct binding_level *newlevel;
985:
986: /* Reuse or create a struct for this binding level. */
987:
988: if (free_binding_level)
989: {
990: newlevel = free_binding_level;
991: free_binding_level = free_binding_level->level_chain;
992: }
993: else
994: {
995: newlevel = make_binding_level ();
996: }
997:
998: /* Add this level to the front of the chain (stack) of label levels. */
999:
1000: newlevel->level_chain = label_level_chain;
1001: label_level_chain = newlevel;
1002:
1003: newlevel->names = named_labels;
1004: newlevel->shadowed = shadowed_labels;
1005: named_labels = 0;
1006: shadowed_labels = 0;
1007: }
1008:
1009: void
1010: pop_label_level ()
1011: {
1012: register struct binding_level *level = label_level_chain;
1013: tree link, prev;
1014:
1015: /* Clear out the definitions of the declared labels in this level.
1016: Leave in the list any ordinary, non-declared labels. */
1017: for (link = named_labels, prev = 0; link;)
1018: {
1019: if (C_DECLARED_LABEL_FLAG (TREE_VALUE (link)))
1020: {
1021: if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
1022: {
1023: error_with_decl ("label `%s' used but not defined",
1024: TREE_VALUE (link));
1025: /* Avoid crashing later. */
1026: define_label (input_filename, lineno,
1027: DECL_NAME (TREE_VALUE (link)));
1028: }
1029: else if (warn_unused && !TREE_USED (TREE_VALUE (link)))
1030: warning_with_decl (TREE_VALUE (link),
1031: "label `%s' defined but not used");
1032: IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
1033:
1034: /* Delete this element from the list. */
1035: link = TREE_CHAIN (link);
1036: if (prev)
1037: TREE_CHAIN (prev) = link;
1038: else
1039: named_labels = link;
1040: }
1041: else
1042: {
1043: prev = link;
1044: link = TREE_CHAIN (link);
1045: }
1046: }
1047:
1048: /* Bring back all the labels that were shadowed. */
1049: for (link = shadowed_labels; link; link = TREE_CHAIN (link))
1050: if (DECL_NAME (TREE_VALUE (link)) != 0)
1051: IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
1052: = TREE_VALUE (link);
1053:
1054: named_labels = chainon (named_labels, level->names);
1055: shadowed_labels = level->shadowed;
1056:
1057: /* Pop the current level, and free the structure for reuse. */
1058: label_level_chain = label_level_chain->level_chain;
1059: level->level_chain = free_binding_level;
1060: free_binding_level = level;
1061: }
1062:
1063: /* Push a definition or a declaration of struct, union or enum tag "name".
1064: "type" should be the type node.
1065: We assume that the tag "name" is not already defined.
1066:
1067: Note that the definition may really be just a forward reference.
1068: In that case, the TYPE_SIZE will be zero. */
1069:
1070: void
1071: pushtag (name, type)
1072: tree name, type;
1073: {
1074: register struct binding_level *b;
1075:
1076: /* Find the proper binding level for this type tag. */
1077:
1078: for (b = current_binding_level; b->tag_transparent; b = b->level_chain)
1079: continue;
1080:
1081: if (name)
1082: {
1083: /* Record the identifier as the type's name if it has none. */
1084:
1085: if (TYPE_NAME (type) == 0)
1086: TYPE_NAME (type) = name;
1087: }
1088:
1.1.1.2 root 1089: if (b == global_binding_level)
1090: b->tags = perm_tree_cons (name, type, b->tags);
1091: else
1092: b->tags = saveable_tree_cons (name, type, b->tags);
1093:
1.1 root 1094: /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1095: tagged type we just added to the current binding level. This fake
1096: NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1097: to output a a representation of a tagged type, and it also gives
1098: us a convenient place to record the "scope start" address for the
1099: tagged type. */
1100:
1101: TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL, type));
1102: }
1103:
1104: /* Handle when a new declaration NEWDECL
1105: has the same name as an old one OLDDECL
1106: in the same binding contour.
1107: Prints an error message if appropriate.
1108:
1109: If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
1110: Otherwise, return 0. */
1111:
1112: static int
1113: duplicate_decls (newdecl, olddecl)
1114: register tree newdecl, olddecl;
1115: {
1116: int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1117: int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1118: && DECL_INITIAL (newdecl) != 0);
1119:
1120: if (TREE_CODE (TREE_TYPE (newdecl)) == ERROR_MARK
1121: || TREE_CODE (TREE_TYPE (olddecl)) == ERROR_MARK)
1122: types_match = 0;
1123:
1124: /* New decl is completely inconsistent with the old one =>
1125: tell caller to replace the old one.
1126: This is always an error except in the case of shadowing a builtin. */
1127: if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1128: {
1129: if (TREE_CODE (olddecl) == FUNCTION_DECL
1130: && DECL_BUILT_IN (olddecl))
1131: {
1132: /* If you declare a built-in function name as static, the
1133: built-in definition is overridden,
1134: but optionally warn this was a bad choice of name. */
1135: if (!TREE_PUBLIC (newdecl))
1136: {
1137: if (warn_shadow)
1138: warning_with_decl (newdecl, "shadowing built-in function `%s'");
1139: }
1140: /* Likewise, if the built-in is not ansi, then programs can
1.1.1.2 root 1141: override it even globally without an error. */
1.1 root 1142: else if (DECL_BUILT_IN_NONANSI (olddecl))
1143: warning_with_decl (newdecl,
1144: "built-in function `%s' declared as non-function");
1145: else
1146: error_with_decl (newdecl,
1147: "built-in function `%s' declared as non-function");
1148: }
1149: else if (TREE_CODE (olddecl) == FUNCTION_DECL
1150: && DECL_BUILT_IN_NONANSI (olddecl))
1151: {
1152: /* If overriding decl is static,
1153: optionally warn this was a bad choice of name. */
1154: if (!TREE_PUBLIC (newdecl))
1155: {
1156: if (warn_shadow)
1157: warning_with_decl (newdecl, "shadowing library function `%s'");
1158: }
1159: /* Otherwise, always warn. */
1160: else
1161: warning_with_decl (newdecl,
1162: "library function `%s' declared as non-function");
1163: }
1164: else
1165: {
1166: error_with_decl (newdecl, "`%s' redeclared as different kind of symbol");
1167: error_with_decl (olddecl, "previous declaration of `%s'");
1168: }
1169:
1170: return 0;
1171: }
1172:
1173: /* For real parm decl following a forward decl,
1174: return 1 so old decl will be reused. */
1175: if (types_match && TREE_CODE (newdecl) == PARM_DECL
1176: && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1177: return 1;
1178:
1179: /* The new declaration is the same kind of object as the old one.
1180: The declarations may partially match. Print warnings if they don't
1181: match enough. Ultimately, copy most of the information from the new
1182: decl to the old one, and keep using the old one. */
1183:
1184: if (flag_traditional && TREE_CODE (newdecl) == FUNCTION_DECL
1185: && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (newdecl)) == olddecl
1186: && DECL_INITIAL (olddecl) == 0)
1187: /* If -traditional, avoid error for redeclaring fcn
1188: after implicit decl. */
1189: ;
1190: else if (TREE_CODE (olddecl) == FUNCTION_DECL
1191: && DECL_BUILT_IN (olddecl))
1192: {
1193: if (!TREE_PUBLIC (newdecl))
1194: {
1195: /* If you declare a built-in function name as static, the
1196: built-in definition is overridden,
1197: but optionally warn this was a bad choice of name. */
1198: if (warn_shadow)
1199: warning_with_decl (newdecl, "shadowing built-in function `%s'");
1200: /* Discard the old built-in function. */
1201: return 0;
1202: }
1203: else if (!types_match)
1204: warning_with_decl (newdecl, "conflicting types for built-in function `%s'");
1205: }
1206: else if (TREE_CODE (olddecl) == FUNCTION_DECL
1207: && DECL_BUILT_IN_NONANSI (olddecl))
1208: {
1209: if (!TREE_PUBLIC (newdecl))
1210: {
1211: /* If you declare a built-in function name as static, the
1212: built-in definition is overridden,
1213: but optionally warn this was a bad choice of name. */
1214: if (warn_shadow)
1215: warning_with_decl (newdecl, "shadowing library function `%s'");
1216: /* Discard the old built-in function. */
1217: return 0;
1218: }
1219: else if (!types_match)
1220: warning_with_decl (newdecl, "conflicting types for library function `%s'");
1221: }
1222: else if (!types_match
1223: /* Permit char *foo (int, ...); followed by char *foo ();
1224: if not pedantic. */
1225: && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1226: && ! pedantic
1227: /* Return types must still match. */
1228: && comptypes (TREE_TYPE (TREE_TYPE (olddecl)),
1229: TREE_TYPE (TREE_TYPE (newdecl)))
1230: && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) == 0))
1231: {
1232: error_with_decl (newdecl, "conflicting types for `%s'");
1233: /* Check for function type mismatch
1234: involving an empty arglist vs a nonempty one. */
1235: if (TREE_CODE (olddecl) == FUNCTION_DECL
1236: && comptypes (TREE_TYPE (TREE_TYPE (olddecl)),
1237: TREE_TYPE (TREE_TYPE (newdecl)))
1238: && ((TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == 0
1239: && DECL_INITIAL (olddecl) == 0)
1240: ||
1241: (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) == 0
1242: && DECL_INITIAL (newdecl) == 0)))
1243: {
1244: /* Classify the problem further. */
1245: register tree t = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1246: if (t == 0)
1247: t = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1248: for (; t; t = TREE_CHAIN (t))
1249: {
1250: register tree type = TREE_VALUE (t);
1251:
1252: if (TREE_CHAIN (t) == 0 && type != void_type_node)
1253: {
1254: error ("A parameter list with an ellipsis can't match");
1255: error ("an empty parameter name list declaration.");
1256: break;
1257: }
1258:
1259: if (type == float_type_node
1260: || (TREE_CODE (type) == INTEGER_TYPE
1261: && (TYPE_PRECISION (type)
1262: < TYPE_PRECISION (integer_type_node))))
1263: {
1264: error ("An argument type that has a default promotion");
1265: error ("can't match an empty parameter name list declaration.");
1266: break;
1267: }
1268: }
1269: }
1270: error_with_decl (olddecl, "previous declaration of `%s'");
1271: }
1272: else
1273: {
1274: char *errmsg = redeclaration_error_message (newdecl, olddecl);
1275: if (errmsg)
1276: {
1277: error_with_decl (newdecl, errmsg);
1278: error_with_decl (olddecl,
1279: "`%s' previously declared here");
1280: }
1281: else if (TREE_CODE (olddecl) == FUNCTION_DECL
1282: && DECL_INITIAL (olddecl) != 0
1283: && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == 0
1284: && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0)
1285: {
1286: register tree type, parm;
1287: register int nargs;
1288: /* Prototype decl follows defn w/o prototype. */
1289:
1290: for (parm = TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (olddecl)),
1291: type = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1292: nargs = 1;
1293: (TREE_VALUE (parm) != void_type_node
1294: || TREE_VALUE (type) != void_type_node);
1295: parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1296: {
1297: if (TREE_VALUE (parm) == void_type_node
1298: || TREE_VALUE (type) == void_type_node)
1299: {
1300: errmsg = "prototype for `%s' follows and number of arguments";
1301: break;
1302: }
1303: /* Type for passing arg must be consistent
1304: with that declared for the arg. */
1305: if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type))
1306: /* If -traditional, allow `unsigned int' instead of `int'
1307: in the prototype. */
1308: && (! (flag_traditional
1309: && TREE_VALUE (parm) == integer_type_node
1310: && TREE_VALUE (type) == unsigned_type_node)))
1311: {
1312: errmsg = "prototype for `%s' follows and argument %d";
1313: break;
1314: }
1315: }
1316: if (errmsg)
1317: {
1318: error_with_decl (newdecl, errmsg, nargs);
1319: error_with_decl (olddecl,
1320: "doesn't match non-prototype definition here");
1321: }
1322: else
1323: {
1324: warning_with_decl (newdecl, "prototype for `%s' follows");
1325: warning_with_decl (olddecl, "non-prototype definition here");
1326: }
1327: }
1.1.1.3 ! root 1328: /* Warn about mismatches in various flags. */
1.1 root 1329: else
1330: {
1.1.1.3 ! root 1331: /* Warn if function is now inline
! 1332: but was previously declared not inline and has been called. */
1.1 root 1333: if (TREE_CODE (olddecl) == FUNCTION_DECL
1334: && ! TREE_INLINE (olddecl) && TREE_INLINE (newdecl)
1335: && TREE_USED (olddecl))
1336: warning_with_decl (newdecl,
1337: "`%s' declared inline after being called");
1338: if (TREE_CODE (olddecl) == FUNCTION_DECL
1.1.1.3 ! root 1339: && TREE_INLINE (olddecl) != TREE_INLINE (newdecl)
! 1340: && ! (TREE_EXTERNAL (olddecl) && TREE_EXTERNAL (newdecl)))
1.1 root 1341: warning_with_decl (newdecl,
1342: "`%s' declarations disagree about `inline'");
1343: /* It is nice to warn when a function is declared
1344: global first and then static. */
1345: if (TREE_CODE (olddecl) == FUNCTION_DECL
1346: && TREE_PUBLIC (olddecl)
1347: && !TREE_PUBLIC (newdecl))
1348: warning_with_decl (newdecl, "static declaration for `%s' follows non-static");
1349:
1.1.1.3 ! root 1350: /* These bits are logically part of the type, for variables.
! 1351: But not for functions
! 1352: (where qualifiers are not valid ANSI anyway). */
! 1353: if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1.1 root 1354: && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1355: || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1356: pedwarn_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl");
1357: }
1358: }
1359:
1360: /* Copy all the DECL_... slots specified in the new decl
1361: except for any that we copy here from the old type. */
1362:
1363: if (types_match)
1364: {
1365: tree oldtype = TREE_TYPE (olddecl);
1366: /* Merge the data types specified in the two decls. */
1367: if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1368: TREE_TYPE (newdecl)
1369: = TREE_TYPE (olddecl)
1370: = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1371:
1372: /* Lay the type out, unless already done. */
1373: if (oldtype != TREE_TYPE (newdecl))
1374: {
1375: if (TREE_TYPE (newdecl) != error_mark_node)
1376: layout_type (TREE_TYPE (newdecl));
1377: if (TREE_CODE (newdecl) != FUNCTION_DECL
1378: && TREE_CODE (newdecl) != TYPE_DECL
1379: && TREE_CODE (newdecl) != CONST_DECL)
1380: layout_decl (newdecl, 0);
1381: }
1382: else
1383: {
1384: /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1385: DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1.1.1.3 ! root 1386: if (TREE_CODE (olddecl) != FUNCTION_DECL)
! 1387: if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
! 1388: DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1.1 root 1389: }
1390:
1391: /* Merge the type qualifiers. */
1392: if (DECL_BUILT_IN_NONANSI (olddecl) && TREE_THIS_VOLATILE (olddecl)
1393: && !TREE_THIS_VOLATILE (newdecl))
1394: TREE_THIS_VOLATILE (olddecl) = 0;
1395: if (TREE_READONLY (newdecl))
1396: TREE_READONLY (olddecl) = 1;
1397: if (TREE_THIS_VOLATILE (newdecl))
1398: TREE_THIS_VOLATILE (olddecl) = 1;
1399:
1400: /* Keep source location of definition rather than declaration. */
1401: if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1402: {
1403: DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1404: DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1405: }
1406:
1407: /* Merge the initialization information. */
1408: if (DECL_INITIAL (newdecl) == 0)
1409: DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1410: /* Keep the old rtl since we can safely use it. */
1411: DECL_RTL (newdecl) = DECL_RTL (olddecl);
1412: }
1413: /* If cannot merge, then use the new type and qualifiers,
1414: and don't preserve the old rtl. */
1415: else
1416: {
1417: TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1418: TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1419: TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1420: TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1421: }
1422:
1423: /* Merge the storage class information. */
1424: /* For functions, static overrides non-static. */
1425: if (TREE_CODE (newdecl) == FUNCTION_DECL)
1426: {
1427: TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1428: /* This is since we don't automatically
1429: copy the attributes of NEWDECL into OLDDECL. */
1430: TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1431: /* If this clears `static', clear it in the identifier too. */
1432: if (! TREE_PUBLIC (olddecl))
1433: TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1434: }
1435: if (TREE_EXTERNAL (newdecl))
1436: {
1437: TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1438: TREE_EXTERNAL (newdecl) = TREE_EXTERNAL (olddecl);
1439: /* An extern decl does not override previous storage class. */
1440: TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1441: }
1442: else
1443: {
1444: TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1445: TREE_EXTERNAL (olddecl) = 0;
1446: TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1447: }
1448: /* If either decl says `inline', this fn is inline,
1449: unless its definition was passed already. */
1450: if (TREE_INLINE (newdecl) && DECL_INITIAL (olddecl) == 0)
1451: TREE_INLINE (olddecl) = 1;
1452:
1453: /* Get rid of any built-in function if new arg types don't match it
1454: or if we have a function definition. */
1455: if (TREE_CODE (newdecl) == FUNCTION_DECL
1456: && DECL_BUILT_IN (olddecl)
1457: && (!types_match || new_is_definition))
1458: {
1459: TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1460: DECL_BUILT_IN (olddecl) = 0;
1461: }
1462:
1463: /* If redeclaring a builtin function, and not a definition,
1464: it stays built in.
1465: Also preserve various other info from the definition. */
1466: if (TREE_CODE (newdecl) == FUNCTION_DECL && !new_is_definition)
1467: {
1468: if (DECL_BUILT_IN (olddecl))
1469: {
1470: DECL_BUILT_IN (newdecl) = 1;
1471: DECL_SET_FUNCTION_CODE (newdecl, DECL_FUNCTION_CODE (olddecl));
1472: }
1473: else
1474: DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
1475:
1476: DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1477: DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1478: DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1479: DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1480: }
1481:
1482: bcopy ((char *) newdecl + sizeof (struct tree_common),
1483: (char *) olddecl + sizeof (struct tree_common),
1484: sizeof (struct tree_decl) - sizeof (struct tree_common));
1485:
1486: return 1;
1487: }
1488:
1489: /* Record a decl-node X as belonging to the current lexical scope.
1490: Check for errors (such as an incompatible declaration for the same
1491: name already seen in the same scope).
1492:
1493: Returns either X or an old decl for the same name.
1494: If an old decl is returned, it may have been smashed
1495: to agree with what X says. */
1496:
1497: tree
1498: pushdecl (x)
1499: tree x;
1500: {
1501: register tree t;
1502: register tree name = DECL_NAME (x);
1503: register struct binding_level *b = current_binding_level;
1504:
1505: DECL_CONTEXT (x) = current_function_decl;
1506: /* A local declaration for a function doesn't constitute nesting. */
1507: if (TREE_CODE (x) == FUNCTION_DECL && DECL_INITIAL (x) == 0)
1508: DECL_CONTEXT (x) = 0;
1509:
1510: if (warn_nested_externs && TREE_EXTERNAL (x) && b != global_binding_level
1511: && x != IDENTIFIER_IMPLICIT_DECL (name))
1512: warning ("nested extern declaration of `%s'", IDENTIFIER_POINTER (name));
1513:
1514: if (name)
1515: {
1516: char *file;
1517: int line;
1518:
1519: t = lookup_name_current_level (name);
1520: if (t != 0 && t == error_mark_node)
1521: /* error_mark_node is 0 for a while during initialization! */
1522: {
1523: t = 0;
1524: error_with_decl (x, "`%s' used prior to declaration");
1525: }
1526:
1527: if (t != 0)
1528: {
1529: file = DECL_SOURCE_FILE (t);
1530: line = DECL_SOURCE_LINE (t);
1531: }
1532:
1533: if (t != 0 && duplicate_decls (x, t))
1534: {
1535: if (TREE_CODE (t) == PARM_DECL)
1536: {
1537: /* Don't allow more than one "real" duplicate
1538: of a forward parm decl. */
1539: TREE_ASM_WRITTEN (t) = TREE_ASM_WRITTEN (x);
1540: return t;
1541: }
1542: /* If this decl is `static' and an implicit decl was seen previously,
1543: warn. But don't complain if -traditional,
1544: since traditional compilers don't complain. */
1545: if (!flag_traditional && TREE_PUBLIC (name)
1546: && ! TREE_PUBLIC (x) && ! TREE_EXTERNAL (x)
1547: /* We used to warn also for explicit extern followed by static,
1548: but sometimes you need to do it that way. */
1549: && IDENTIFIER_IMPLICIT_DECL (name) != 0)
1550: {
1551: pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1552: IDENTIFIER_POINTER (name));
1553: pedwarn_with_file_and_line (file, line,
1554: "previous declaration of `%s'",
1555: IDENTIFIER_POINTER (name));
1556: }
1557: if (warn_redundant_decls && line != 0)
1558: {
1559: warning ("redundant redeclaration of `%s' in same scope",
1560: IDENTIFIER_POINTER (name));
1561: warning_with_file_and_line (file, line,
1562: "previous declaration of `%s'",
1563: IDENTIFIER_POINTER (name));
1564: }
1565: return t;
1566: }
1567:
1.1.1.3 ! root 1568: /* If we are processing a typedef statement, generate a whole new
! 1569: ..._TYPE node (which will be just an variant of the existing
! 1570: ..._TYPE node with identical properties) and then install the
! 1571: TYPE_DECL node generated to represent the typedef name as the
! 1572: TYPE_NAME of this brand new (duplicate) ..._TYPE node.
! 1573:
! 1574: The whole point here is to end up with a situation where each
! 1575: and every ..._TYPE node the compiler creates will be uniquely
! 1576: associated with AT MOST one node representing a typedef name.
! 1577: This way, even though the compiler substitutes corresponding
! 1578: ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
! 1579: early on, later parts of the compiler can always do the reverse
! 1580: translation and get back the corresponding typedef name. For
! 1581: example, given:
! 1582:
! 1583: typedef struct S MY_TYPE;
! 1584: MY_TYPE object;
! 1585:
! 1586: Later parts of the compiler might only know that `object' was of
! 1587: type `struct S' if if were not for code just below. With this
! 1588: code however, later parts of the compiler see something like:
! 1589:
! 1590: struct S' == struct S
! 1591: typedef struct S' MY_TYPE;
! 1592: struct S' object;
! 1593:
! 1594: And they can then deduce (from the node for type struct S') that
! 1595: the original object declaration was:
! 1596:
! 1597: MY_TYPE object;
! 1598:
! 1599: Being able to do this is important for proper support of protoize,
! 1600: and also for generating precise symbolic debugging information
! 1601: which takes full account of the programmer's (typedef) vocabulary.
! 1602:
! 1603: Obviously, we don't want to generate a duplicate ..._TYPE node if
! 1604: the TYPE_DECL node that we are now processing really represents a
! 1605: standard built-in type.
! 1606:
1.1 root 1607: Since all standard types are effectively declared at line zero
1608: in the source file, we can easily check to see if we are working
1609: on a standard type by checking the current value of lineno. */
1610:
1611: if (TREE_CODE (x) == TYPE_DECL)
1612: {
1613: if (DECL_SOURCE_LINE (x) == 0 || !flag_gen_aux_info)
1614: {
1615: if (TYPE_NAME (TREE_TYPE (x)) == 0)
1616: TYPE_NAME (TREE_TYPE (x)) = x;
1617: }
1618: else
1619: {
1620: tree tt = TREE_TYPE (x);
1621:
1.1.1.3 ! root 1622: tt = build_type_copy (tt);
1.1 root 1623: TYPE_NAME (tt) = x;
1624: TREE_TYPE (x) = tt;
1625: }
1626: }
1627:
1628: /* Multiple external decls of the same identifier ought to match. */
1629:
1630: if (TREE_EXTERNAL (x) && IDENTIFIER_GLOBAL_VALUE (name) != 0
1631: && (TREE_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name))
1632: || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name)))
1633: /* We get warnings about inline functions where they are defined.
1634: Avoid duplicate warnings where they are used. */
1635: && !TREE_INLINE (x))
1636: {
1637: if (! comptypes (TREE_TYPE (x),
1638: TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
1639: {
1640: pedwarn_with_decl (x,
1641: "type mismatch with previous external decl");
1642: pedwarn_with_decl (IDENTIFIER_GLOBAL_VALUE (name),
1643: "previous external decl of `%s'");
1644: }
1645: }
1646:
1647: /* If a function has had an implicit declaration, and then is defined,
1648: make sure they are compatible. */
1649:
1650: if (IDENTIFIER_IMPLICIT_DECL (name) != 0
1651: && IDENTIFIER_GLOBAL_VALUE (name) == 0
1652: && TREE_CODE (x) == FUNCTION_DECL
1653: && ! comptypes (TREE_TYPE (x),
1654: TREE_TYPE (IDENTIFIER_IMPLICIT_DECL (name))))
1655: {
1656: warning_with_decl (x, "type mismatch with previous implicit declaration");
1.1.1.2 root 1657: warning_with_decl (IDENTIFIER_IMPLICIT_DECL (name),
1658: "previous implicit declaration of `%s'");
1.1 root 1659: }
1660:
1661: /* In PCC-compatibility mode, extern decls of vars with no current decl
1662: take effect at top level no matter where they are. */
1663: if (flag_traditional && TREE_EXTERNAL (x)
1664: && lookup_name (name) == 0)
1665: {
1666: tree type = TREE_TYPE (x);
1667:
1668: /* But don't do this if the type contains temporary nodes. */
1669: while (type)
1670: {
1671: if (type == error_mark_node)
1672: break;
1673: if (! TREE_PERMANENT (type))
1674: {
1675: warning_with_decl (x, "type of external `%s' is not global");
1676: /* By exiting the loop early, we leave TYPE nonzero,
1677: and thus prevent globalization of the decl. */
1678: break;
1679: }
1680: else if (TREE_CODE (type) == FUNCTION_TYPE
1681: && TYPE_ARG_TYPES (type) != 0)
1682: /* The types might not be truly local,
1683: but the list of arg types certainly is temporary.
1684: Since prototypes are nontraditional,
1685: ok not to do the traditional thing. */
1686: break;
1687: type = TREE_TYPE (type);
1688: }
1689:
1690: if (type == 0)
1691: b = global_binding_level;
1692: }
1693:
1694: /* This name is new in its binding level.
1695: Install the new declaration and return it. */
1696: if (b == global_binding_level)
1697: {
1698: /* Install a global value. */
1699:
1700: /* If the first global decl has external linkage,
1701: warn if we later see static one. */
1702: if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x))
1703: TREE_PUBLIC (name) = 1;
1704:
1705: IDENTIFIER_GLOBAL_VALUE (name) = x;
1706:
1707: /* Don't forget if the function was used via an implicit decl. */
1708: if (IDENTIFIER_IMPLICIT_DECL (name)
1709: && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
1710: TREE_USED (x) = 1, TREE_USED (name) = 1;
1711:
1712: /* Don't forget if its address was taken in that way. */
1713: if (IDENTIFIER_IMPLICIT_DECL (name)
1714: && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
1715: TREE_ADDRESSABLE (x) = 1;
1716:
1717: /* Warn about mismatches against previous implicit decl. */
1718: if (IDENTIFIER_IMPLICIT_DECL (name) != 0
1719: /* If this real decl matches the implicit, don't complain. */
1720: && ! (TREE_CODE (x) == FUNCTION_DECL
1721: && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
1722: pedwarn ("`%s' was previously implicitly declared to return `int'",
1723: IDENTIFIER_POINTER (name));
1724:
1725: /* If this decl is `static' and an `extern' was seen previously,
1726: that is erroneous. */
1727: if (TREE_PUBLIC (name)
1728: && ! TREE_PUBLIC (x) && ! TREE_EXTERNAL (x))
1729: {
1.1.1.2 root 1730: /* Okay to declare an ANSI built-in as inline static. */
1731: if (t != 0 && DECL_BUILT_IN (t)
1732: && TREE_INLINE (x))
1733: ;
1734: /* Okay to declare a non-ANSI built-in as anything. */
1735: else if (t != 0 && DECL_BUILT_IN_NONANSI (t))
1736: ;
1737: else if (IDENTIFIER_IMPLICIT_DECL (name))
1.1 root 1738: pedwarn ("`%s' was declared implicitly `extern' and later `static'",
1739: IDENTIFIER_POINTER (name));
1740: else
1741: pedwarn ("`%s' was declared `extern' and later `static'",
1742: IDENTIFIER_POINTER (name));
1743: }
1744: }
1745: else
1746: {
1747: /* Here to install a non-global value. */
1748: tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1749: tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
1750: IDENTIFIER_LOCAL_VALUE (name) = x;
1751:
1752: /* If this is an extern function declaration, see if we
1753: have a global definition for the function. */
1754: if (oldlocal == 0
1755: && TREE_EXTERNAL (x) && !TREE_INLINE (x)
1756: && oldglobal != 0
1757: && TREE_CODE (x) == FUNCTION_DECL
1758: && TREE_CODE (oldglobal) == FUNCTION_DECL)
1759: {
1760: /* We have one. Their types must agree. */
1761: if (! comptypes (TREE_TYPE (x),
1762: TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
1763: pedwarn_with_decl (x, "local declaration of `%s' doesn't match global one");
1764: /* If the global one is inline, make the local one inline. */
1765: else if (TREE_INLINE (oldglobal)
1766: || DECL_BUILT_IN (oldglobal)
1767: || (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0
1768: && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0))
1769: IDENTIFIER_LOCAL_VALUE (name) = oldglobal;
1770: }
1771:
1772: #if 0 /* This case is probably sometimes the right thing to do. */
1773: /* If we have a local external declaration,
1774: then any file-scope declaration should not
1775: have been static. */
1776: if (oldlocal == 0 && oldglobal != 0
1777: && !TREE_PUBLIC (oldglobal)
1778: && TREE_EXTERNAL (x) && TREE_PUBLIC (x))
1779: warning ("`%s' locally external but globally static",
1780: IDENTIFIER_POINTER (name));
1781: #endif
1782:
1783: /* If we have a local external declaration,
1784: and no file-scope declaration has yet been seen,
1785: then if we later have a file-scope decl it must not be static. */
1786: if (oldlocal == 0
1787: && oldglobal == 0
1788: && TREE_EXTERNAL (x)
1789: && TREE_PUBLIC (x))
1790: {
1791: TREE_PUBLIC (name) = 1;
1792: }
1793:
1794: /* Warn if shadowing an argument at the top level of the body. */
1795: if (oldlocal != 0 && !TREE_EXTERNAL (x)
1796: /* This warning doesn't apply to the parms of a nested fcn. */
1797: && ! current_binding_level->parm_flag
1798: /* Check that this is one level down from the parms. */
1799: && current_binding_level->level_chain->parm_flag
1800: /* Check that the decl being shadowed
1801: comes from the parm level, one level up. */
1802: && chain_member (oldlocal, current_binding_level->level_chain->names))
1803: {
1804: if (TREE_CODE (oldlocal) == PARM_DECL)
1805: pedwarn ("declaration of `%s' shadows a parameter",
1806: IDENTIFIER_POINTER (name));
1807: else
1808: pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1809: IDENTIFIER_POINTER (name));
1810: }
1811:
1812: /* Maybe warn if shadowing something else. */
1813: else if (warn_shadow && !TREE_EXTERNAL (x)
1.1.1.3 ! root 1814: /* No shadow warnings for internally generated vars. */
! 1815: && !DECL_IGNORED_P (x)
1.1 root 1816: /* No shadow warnings for vars made for inlining. */
1817: && ! DECL_FROM_INLINE (x))
1818: {
1819: char *warnstring = 0;
1820:
1821: if (TREE_CODE (x) == PARM_DECL
1822: && current_binding_level->parm_flag == 1)
1823: /* Don't warn about the parm names in a declaration. */
1824: ;
1825: else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1826: warnstring = "declaration of `%s' shadows a parameter";
1827: else if (oldlocal != 0)
1828: warnstring = "declaration of `%s' shadows previous local";
1829: else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1830: && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1831: warnstring = "declaration of `%s' shadows global declaration";
1832:
1833: if (warnstring)
1834: warning (warnstring, IDENTIFIER_POINTER (name));
1835: }
1836:
1837: /* If storing a local value, there may already be one (inherited).
1838: If so, record it for restoration when this binding level ends. */
1839: if (oldlocal != 0)
1840: b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1841: }
1842:
1843: /* Keep count of variables in this level with incomplete type. */
1844: if (TYPE_SIZE (TREE_TYPE (x)) == 0)
1845: ++b->n_incomplete;
1846: }
1847:
1848: /* Put decls on list in reverse order.
1849: We will reverse them later if necessary. */
1850: TREE_CHAIN (x) = b->names;
1851: b->names = x;
1852:
1853: return x;
1854: }
1855:
1856: /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1857:
1858: tree
1859: pushdecl_top_level (x)
1860: tree x;
1861: {
1862: register tree t;
1863: register struct binding_level *b = current_binding_level;
1864:
1865: current_binding_level = global_binding_level;
1866: t = pushdecl (x);
1867: current_binding_level = b;
1868: return t;
1869: }
1870:
1871: /* Generate an implicit declaration for identifier FUNCTIONID
1872: as a function of type int (). Print a warning if appropriate. */
1873:
1874: tree
1875: implicitly_declare (functionid)
1876: tree functionid;
1877: {
1878: register tree decl;
1879: int traditional_warning = 0;
1880: /* Only one "implicit declaration" warning per identifier. */
1881: int implicit_warning;
1882:
1883: /* Save the decl permanently so we can warn if definition follows. */
1884: push_obstacks_nochange ();
1885: end_temporary_allocation ();
1886:
1887: /* We used to reuse an old implicit decl here,
1888: but this loses with inline functions because it can clobber
1889: the saved decl chains. */
1890: /* if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
1891: decl = IDENTIFIER_IMPLICIT_DECL (functionid);
1892: else */
1893: decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1894:
1895: /* Warn of implicit decl following explicit local extern decl.
1896: This is probably a program designed for traditional C. */
1897: if (TREE_PUBLIC (functionid) && IDENTIFIER_GLOBAL_VALUE (functionid) == 0)
1898: traditional_warning = 1;
1899:
1900: /* Warn once of an implicit declaration. */
1901: implicit_warning = (IDENTIFIER_IMPLICIT_DECL (functionid) == 0);
1902:
1903: TREE_EXTERNAL (decl) = 1;
1904: TREE_PUBLIC (decl) = 1;
1905:
1906: /* Record that we have an implicit decl and this is it. */
1907: IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
1908:
1909: /* ANSI standard says implicit declarations are in the innermost block.
1910: So we record the decl in the standard fashion.
1911: If flag_traditional is set, pushdecl does it top-level. */
1912: pushdecl (decl);
1913:
1914: /* This is a no-op in c-lang.c or something real in objc-actions.c. */
1915: maybe_objc_check_decl (decl);
1916:
1917: rest_of_decl_compilation (decl, 0, 0, 0);
1918:
1919: if (warn_implicit && implicit_warning)
1920: warning ("implicit declaration of function `%s'",
1921: IDENTIFIER_POINTER (functionid));
1922: else if (warn_traditional && traditional_warning)
1923: warning ("function `%s' was previously declared within a block",
1924: IDENTIFIER_POINTER (functionid));
1925:
1926: /* Write a record describing this implicit function declaration to the
1927: prototypes file (if requested). */
1928:
1929: gen_aux_info_record (decl, 0, 1, 0);
1930:
1931: pop_obstacks ();
1932:
1933: return decl;
1934: }
1935:
1936: /* Return zero if the declaration NEWDECL is valid
1937: when the declaration OLDDECL (assumed to be for the same name)
1938: has already been seen.
1939: Otherwise return an error message format string with a %s
1940: where the identifier should go. */
1941:
1942: static char *
1943: redeclaration_error_message (newdecl, olddecl)
1944: tree newdecl, olddecl;
1945: {
1946: if (TREE_CODE (newdecl) == TYPE_DECL)
1947: {
1948: if (flag_traditional && TREE_TYPE (newdecl) == TREE_TYPE (olddecl))
1949: return 0;
1950: return "redefinition of `%s'";
1951: }
1952: else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1953: {
1954: /* Declarations of functions can insist on internal linkage
1955: but they can't be inconsistent with internal linkage,
1956: so there can be no error on that account.
1957: However defining the same name twice is no good. */
1958: if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1959: /* However, defining once as extern inline and a second
1960: time in another way is ok. */
1961: && !(TREE_INLINE (olddecl) && TREE_EXTERNAL (olddecl)
1962: && !(TREE_INLINE (newdecl) && TREE_EXTERNAL (newdecl))))
1963: return "redefinition of `%s'";
1964: return 0;
1965: }
1966: else if (current_binding_level == global_binding_level)
1967: {
1968: /* Objects declared at top level: */
1969: /* If at least one is a reference, it's ok. */
1970: if (TREE_EXTERNAL (newdecl) || TREE_EXTERNAL (olddecl))
1971: return 0;
1972: /* Reject two definitions. */
1973: if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1974: return "redefinition of `%s'";
1975: /* Now we have two tentative defs, or one tentative and one real def. */
1976: /* Insist that the linkage match. */
1977: if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1978: return "conflicting declarations of `%s'";
1979: return 0;
1980: }
1981: else if (current_binding_level->parm_flag
1982: && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1983: return 0;
1984: else
1985: {
1986: /* Objects declared with block scope: */
1987: /* Reject two definitions, and reject a definition
1988: together with an external reference. */
1989: if (!(TREE_EXTERNAL (newdecl) && TREE_EXTERNAL (olddecl)))
1990: return "redeclaration of `%s'";
1991: return 0;
1992: }
1993: }
1994:
1995: /* Get the LABEL_DECL corresponding to identifier ID as a label.
1996: Create one if none exists so far for the current function.
1997: This function is called for both label definitions and label references. */
1998:
1999: tree
2000: lookup_label (id)
2001: tree id;
2002: {
2003: register tree decl = IDENTIFIER_LABEL_VALUE (id);
2004:
2005: /* Use a label already defined or ref'd with this name. */
2006: if (decl != 0)
2007: {
2008: /* But not if it is inherited and wasn't declared to be inheritable. */
2009: if (DECL_CONTEXT (decl) != current_function_decl
2010: && ! C_DECLARED_LABEL_FLAG (decl))
2011: return shadow_label (id);
2012: return decl;
2013: }
2014:
2015: decl = build_decl (LABEL_DECL, id, void_type_node);
2016:
2017: /* A label not explicitly declared must be local to where it's ref'd. */
2018: DECL_CONTEXT (decl) = current_function_decl;
2019:
2020: DECL_MODE (decl) = VOIDmode;
2021:
2022: /* Say where one reference is to the label,
2023: for the sake of the error if it is not defined. */
2024: DECL_SOURCE_LINE (decl) = lineno;
2025: DECL_SOURCE_FILE (decl) = input_filename;
2026:
2027: IDENTIFIER_LABEL_VALUE (id) = decl;
2028:
2029: named_labels = tree_cons (NULL_TREE, decl, named_labels);
2030:
2031: return decl;
2032: }
2033:
2034: /* Make a label named NAME in the current function,
2035: shadowing silently any that may be inherited from containing functions
2036: or containing scopes.
2037:
2038: Note that valid use, if the label being shadowed
2039: comes from another scope in the same function,
2040: requires calling declare_nonlocal_label right away. */
2041:
2042: tree
2043: shadow_label (name)
2044: tree name;
2045: {
2046: register tree decl = IDENTIFIER_LABEL_VALUE (name);
2047:
2048: if (decl != 0)
2049: {
2050: shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2051: IDENTIFIER_LABEL_VALUE (name) = decl = 0;
2052: }
2053:
2054: return lookup_label (name);
2055: }
2056:
2057: /* Define a label, specifying the location in the source file.
2058: Return the LABEL_DECL node for the label, if the definition is valid.
2059: Otherwise return 0. */
2060:
2061: tree
2062: define_label (filename, line, name)
2063: char *filename;
2064: int line;
2065: tree name;
2066: {
2067: tree decl = lookup_label (name);
2068:
2069: /* If label with this name is known from an outer context, shadow it. */
2070: if (decl != 0 && DECL_CONTEXT (decl) != current_function_decl)
2071: {
2072: shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
2073: IDENTIFIER_LABEL_VALUE (name) = 0;
2074: decl = lookup_label (name);
2075: }
2076:
2077: if (DECL_INITIAL (decl) != 0)
2078: {
2079: error_with_decl (decl, "duplicate label `%s'");
2080: return 0;
2081: }
2082: else
2083: {
2084: /* Mark label as having been defined. */
2085: DECL_INITIAL (decl) = error_mark_node;
2086: /* Say where in the source. */
2087: DECL_SOURCE_FILE (decl) = filename;
2088: DECL_SOURCE_LINE (decl) = line;
2089: return decl;
2090: }
2091: }
2092:
2093: /* Return the list of declarations of the current level.
2094: Note that this list is in reverse order unless/until
2095: you nreverse it; and when you do nreverse it, you must
2096: store the result back using `storedecls' or you will lose. */
2097:
2098: tree
2099: getdecls ()
2100: {
2101: return current_binding_level->names;
2102: }
2103:
2104: /* Return the list of type-tags (for structs, etc) of the current level. */
2105:
2106: tree
2107: gettags ()
2108: {
2109: return current_binding_level->tags;
2110: }
2111:
2112: /* Store the list of declarations of the current level.
2113: This is done for the parameter declarations of a function being defined,
2114: after they are modified in the light of any missing parameters. */
2115:
2116: static void
2117: storedecls (decls)
2118: tree decls;
2119: {
2120: current_binding_level->names = decls;
2121: }
2122:
2123: /* Similarly, store the list of tags of the current level. */
2124:
2125: static void
2126: storetags (tags)
2127: tree tags;
2128: {
2129: current_binding_level->tags = tags;
2130: }
2131:
2132: /* Given NAME, an IDENTIFIER_NODE,
2133: return the structure (or union or enum) definition for that name.
2134: Searches binding levels from BINDING_LEVEL up to the global level.
2135: If THISLEVEL_ONLY is nonzero, searches only the specified context
2136: (but skips any tag-transparent contexts to find one that is
2137: meaningful for tags).
2138: CODE says which kind of type the caller wants;
2139: it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2140: If the wrong kind of type is found, an error is reported. */
2141:
2142: static tree
2143: lookup_tag (code, name, binding_level, thislevel_only)
2144: enum tree_code code;
2145: struct binding_level *binding_level;
2146: tree name;
2147: int thislevel_only;
2148: {
2149: register struct binding_level *level;
2150:
2151: for (level = binding_level; level; level = level->level_chain)
2152: {
2153: register tree tail;
2154: for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2155: {
2156: if (TREE_PURPOSE (tail) == name)
2157: {
2158: if (TREE_CODE (TREE_VALUE (tail)) != code)
2159: {
2160: /* Definition isn't the kind we were looking for. */
2161: pending_invalid_xref = name;
2162: pending_invalid_xref_file = input_filename;
2163: pending_invalid_xref_line = lineno;
2164: }
2165: return TREE_VALUE (tail);
2166: }
2167: }
2168: if (thislevel_only && ! level->tag_transparent)
2169: return NULL_TREE;
2170: }
2171: return NULL_TREE;
2172: }
2173:
2174: /* Print an error message now
2175: for a recent invalid struct, union or enum cross reference.
2176: We don't print them immediately because they are not invalid
2177: when used in the `struct foo;' construct for shadowing. */
2178:
2179: void
2180: pending_xref_error ()
2181: {
2182: if (pending_invalid_xref != 0)
2183: error_with_file_and_line (pending_invalid_xref_file,
2184: pending_invalid_xref_line,
2185: "`%s' defined as wrong kind of tag",
2186: IDENTIFIER_POINTER (pending_invalid_xref));
2187: pending_invalid_xref = 0;
2188: }
2189:
2190: /* Given a type, find the tag that was defined for it and return the tag name.
2191: Otherwise return 0. */
2192:
2193: static tree
2194: lookup_tag_reverse (type)
2195: tree type;
2196: {
2197: register struct binding_level *level;
2198:
2199: for (level = current_binding_level; level; level = level->level_chain)
2200: {
2201: register tree tail;
2202: for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
2203: {
2204: if (TREE_VALUE (tail) == type)
2205: return TREE_PURPOSE (tail);
2206: }
2207: }
2208: return NULL_TREE;
2209: }
2210:
2211: /* Look up NAME in the current binding level and its superiors
2212: in the namespace of variables, functions and typedefs.
2213: Return a ..._DECL node of some kind representing its definition,
2214: or return 0 if it is undefined. */
2215:
2216: tree
2217: lookup_name (name)
2218: tree name;
2219: {
2220: register tree val;
2221: if (current_binding_level != global_binding_level
2222: && IDENTIFIER_LOCAL_VALUE (name))
2223: val = IDENTIFIER_LOCAL_VALUE (name);
2224: else
2225: val = IDENTIFIER_GLOBAL_VALUE (name);
2226: return val;
2227: }
2228:
2229: /* Similar to `lookup_name' but look only at current binding level. */
2230:
2231: static tree
2232: lookup_name_current_level (name)
2233: tree name;
2234: {
2235: register tree t;
2236:
2237: if (current_binding_level == global_binding_level)
2238: return IDENTIFIER_GLOBAL_VALUE (name);
2239:
2240: if (IDENTIFIER_LOCAL_VALUE (name) == 0)
2241: return 0;
2242:
2243: for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
2244: if (DECL_NAME (t) == name)
2245: break;
2246:
2247: return t;
2248: }
2249:
2250: /* Create the predefined scalar types of C,
2251: and some nodes representing standard constants (0, 1, (void *)0).
2252: Initialize the global binding level.
2253: Make definitions for built-in primitive functions. */
2254:
2255: void
2256: init_decl_processing ()
2257: {
2258: register tree endlink;
2259: /* Either char* or void*. */
2260: tree traditional_ptr_type_node;
1.1.1.3 ! root 2261: /* Data types of memcpy and strlen. */
! 2262: tree memcpy_ftype, strlen_ftype;
! 2263: tree void_ftype_any;
1.1 root 2264: int wchar_type_size;
2265: tree temp;
2266:
2267: current_function_decl = NULL;
2268: named_labels = NULL;
2269: current_binding_level = NULL_BINDING_LEVEL;
2270: free_binding_level = NULL_BINDING_LEVEL;
2271: pushlevel (0); /* make the binding_level structure for global names */
2272: global_binding_level = current_binding_level;
2273:
2274: /* Define `int' and `char' first so that dbx will output them first. */
2275:
2276: integer_type_node = make_signed_type (INT_TYPE_SIZE);
2277: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
2278: integer_type_node));
2279:
2280: /* Define `char', which is like either `signed char' or `unsigned char'
2281: but not the same as either. */
2282:
2283: char_type_node =
2284: (flag_signed_char
2285: ? make_signed_type (CHAR_TYPE_SIZE)
2286: : make_unsigned_type (CHAR_TYPE_SIZE));
2287: pushdecl (build_decl (TYPE_DECL, get_identifier ("char"),
2288: char_type_node));
2289:
2290: long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
2291: pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
2292: long_integer_type_node));
2293:
2294: unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
2295: pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
2296: unsigned_type_node));
2297:
2298: long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
2299: pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
2300: long_unsigned_type_node));
2301:
2302: /* `unsigned long' is the standard type for sizeof.
2303: Traditionally, use a signed type.
2304: Note that stddef.h uses `unsigned long',
2305: and this must agree, even of long and int are the same size. */
2306: if (flag_traditional)
2307: sizetype = long_integer_type_node;
2308: else
2309: sizetype
2310: = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE)));
2311:
2312: ptrdiff_type_node
2313: = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
2314:
2315: TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype;
2316: TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype;
2317: TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype;
2318: TREE_TYPE (TYPE_SIZE (long_unsigned_type_node)) = sizetype;
2319: TREE_TYPE (TYPE_SIZE (long_integer_type_node)) = sizetype;
2320:
2321: error_mark_node = make_node (ERROR_MARK);
2322: TREE_TYPE (error_mark_node) = error_mark_node;
2323:
2324: short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
2325: pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
2326: short_integer_type_node));
2327:
2328: long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
2329: pushdecl (build_decl (TYPE_DECL, get_identifier ("long long int"),
2330: long_long_integer_type_node));
2331:
2332: short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
2333: pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
2334: short_unsigned_type_node));
2335:
2336: long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
2337: pushdecl (build_decl (TYPE_DECL, get_identifier ("long long unsigned int"),
2338: long_long_unsigned_type_node));
2339:
2340: /* Define both `signed char' and `unsigned char'. */
2341: signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
2342: pushdecl (build_decl (TYPE_DECL, get_identifier ("signed char"),
2343: signed_char_type_node));
2344:
2345: unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
2346: pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
2347: unsigned_char_type_node));
2348:
2349: float_type_node = make_node (REAL_TYPE);
2350: TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
2351: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
2352: float_type_node));
2353: layout_type (float_type_node);
2354:
2355: double_type_node = make_node (REAL_TYPE);
2356: if (flag_short_double)
2357: TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
2358: else
2359: TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
2360: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
2361: double_type_node));
2362: layout_type (double_type_node);
2363:
2364: long_double_type_node = make_node (REAL_TYPE);
2365: TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
2366: pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
2367: long_double_type_node));
2368: layout_type (long_double_type_node);
2369:
2370: wchar_type_node
2371: = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
2372: wchar_type_size = TYPE_PRECISION (wchar_type_node);
2373: signed_wchar_type_node = type_for_size (wchar_type_size, 0);
2374: unsigned_wchar_type_node = type_for_size (wchar_type_size, 1);
2375:
2376: integer_zero_node = build_int_2 (0, 0);
2377: TREE_TYPE (integer_zero_node) = integer_type_node;
2378: integer_one_node = build_int_2 (1, 0);
2379: TREE_TYPE (integer_one_node) = integer_type_node;
2380:
2381: size_zero_node = build_int_2 (0, 0);
2382: TREE_TYPE (size_zero_node) = sizetype;
2383: size_one_node = build_int_2 (1, 0);
2384: TREE_TYPE (size_one_node) = sizetype;
2385:
2386: void_type_node = make_node (VOID_TYPE);
2387: pushdecl (build_decl (TYPE_DECL,
2388: ridpointers[(int) RID_VOID], void_type_node));
2389: layout_type (void_type_node); /* Uses integer_zero_node */
2390: /* We are not going to have real types in C with less than byte alignment,
2391: so we might as well not have any types that claim to have it. */
2392: TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
2393:
2394: null_pointer_node = build_int_2 (0, 0);
2395: TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
2396: layout_type (TREE_TYPE (null_pointer_node));
2397:
2398: string_type_node = build_pointer_type (char_type_node);
2399: const_string_type_node
2400: = build_pointer_type (build_type_variant (char_type_node, 1, 0));
2401:
2402: /* make a type for arrays of 256 characters.
2403: 256 is picked randomly because we have a type for integers from 0 to 255.
2404: With luck nothing will ever really depend on the length of this
2405: array type. */
2406: char_array_type_node
2407: = build_array_type (char_type_node, unsigned_char_type_node);
2408: /* Likewise for arrays of ints. */
2409: int_array_type_node
2410: = build_array_type (integer_type_node, unsigned_char_type_node);
2411: /* This is for wide string constants. */
2412: wchar_array_type_node
2413: = build_array_type (wchar_type_node, unsigned_char_type_node);
2414:
2415: default_function_type
2416: = build_function_type (integer_type_node, NULL_TREE);
2417:
2418: ptr_type_node = build_pointer_type (void_type_node);
2419: const_ptr_type_node
2420: = build_pointer_type (build_type_variant (void_type_node, 1, 0));
2421:
2422: endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
2423:
1.1.1.3 ! root 2424: void_ftype_any
! 2425: = build_function_type (void_type_node, 0);
! 2426:
1.1 root 2427: double_ftype_double
2428: = build_function_type (double_type_node,
2429: tree_cons (NULL_TREE, double_type_node, endlink));
2430:
2431: double_ftype_double_double
2432: = build_function_type (double_type_node,
2433: tree_cons (NULL_TREE, double_type_node,
2434: tree_cons (NULL_TREE,
2435: double_type_node, endlink)));
2436:
2437: int_ftype_int
2438: = build_function_type (integer_type_node,
2439: tree_cons (NULL_TREE, integer_type_node, endlink));
2440:
2441: long_ftype_long
2442: = build_function_type (long_integer_type_node,
2443: tree_cons (NULL_TREE,
2444: long_integer_type_node, endlink));
2445:
2446: void_ftype_ptr_ptr_int
2447: = build_function_type (void_type_node,
2448: tree_cons (NULL_TREE, ptr_type_node,
2449: tree_cons (NULL_TREE, ptr_type_node,
2450: tree_cons (NULL_TREE,
2451: integer_type_node,
2452: endlink))));
2453:
2454: int_ftype_cptr_cptr_sizet
2455: = build_function_type (integer_type_node,
2456: tree_cons (NULL_TREE, const_ptr_type_node,
2457: tree_cons (NULL_TREE, const_ptr_type_node,
2458: tree_cons (NULL_TREE,
2459: sizetype,
2460: endlink))));
2461:
2462: void_ftype_ptr_int_int
2463: = build_function_type (void_type_node,
2464: tree_cons (NULL_TREE, ptr_type_node,
2465: tree_cons (NULL_TREE, integer_type_node,
2466: tree_cons (NULL_TREE,
2467: integer_type_node,
2468: endlink))));
2469:
2470: string_ftype_ptr_ptr /* strcpy prototype */
2471: = build_function_type (string_type_node,
2472: tree_cons (NULL_TREE, string_type_node,
2473: tree_cons (NULL_TREE,
2474: const_string_type_node,
2475: endlink)));
2476:
2477: int_ftype_string_string /* strcmp prototype */
2478: = build_function_type (integer_type_node,
2479: tree_cons (NULL_TREE, const_string_type_node,
2480: tree_cons (NULL_TREE,
2481: const_string_type_node,
2482: endlink)));
2483:
1.1.1.3 ! root 2484: strlen_ftype /* strlen prototype */
! 2485: = build_function_type (flag_traditional ? integer_type_node : sizetype,
1.1 root 2486: tree_cons (NULL_TREE, const_string_type_node,
2487: endlink));
2488:
2489: traditional_ptr_type_node
2490: = (flag_traditional ? string_type_node : ptr_type_node);
2491:
2492: memcpy_ftype /* memcpy prototype */
2493: = build_function_type (traditional_ptr_type_node,
2494: tree_cons (NULL_TREE, ptr_type_node,
2495: tree_cons (NULL_TREE, const_ptr_type_node,
2496: tree_cons (NULL_TREE,
2497: sizetype,
2498: endlink))));
2499:
1.1.1.2 root 2500: /* ``integer_tpe_node'' misspelling corrected: North-Keys 30 Mar 91 */
1.1 root 2501: builtin_function ("__builtin_constant_p",
2502: build_function_type (integer_type_node, endlink),
2503: BUILT_IN_CONSTANT_P, 0);
2504:
2505: builtin_function ("__builtin_return_address",
2506: build_function_type (integer_type_node,
2507: tree_cons (NULL_TREE,
2508: unsigned_type_node,
2509: endlink)),
2510: BUILT_IN_RETURN_ADDRESS, 0);
2511:
2512: builtin_function ("__builtin_frame_address",
2513: build_function_type (integer_type_node,
2514: tree_cons (NULL_TREE,
2515: unsigned_type_node,
2516: endlink)),
2517: BUILT_IN_FRAME_ADDRESS, 0);
2518:
2519: builtin_function ("__builtin_alloca",
2520: build_function_type (ptr_type_node,
2521: tree_cons (NULL_TREE,
2522: sizetype,
2523: endlink)),
2524: BUILT_IN_ALLOCA, "alloca");
1.1.1.3 ! root 2525: if (! flag_no_builtin && !flag_no_nonansi_builtin)
1.1 root 2526: {
2527: temp = builtin_function ("alloca",
2528: build_function_type (ptr_type_node,
2529: tree_cons (NULL_TREE,
2530: sizetype,
2531: endlink)),
2532: BUILT_IN_ALLOCA, 0);
2533: /* Suppress error if redefined as a non-function. */
2534: DECL_BUILT_IN_NONANSI (temp) = 1;
1.1.1.3 ! root 2535: temp = builtin_function ("_exit", void_ftype_any, NOT_BUILT_IN, 0);
1.1 root 2536: TREE_THIS_VOLATILE (temp) = 1;
2537: TREE_SIDE_EFFECTS (temp) = 1;
2538: /* Suppress error if redefined as a non-function. */
2539: DECL_BUILT_IN_NONANSI (temp) = 1;
2540: }
2541:
2542: builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, 0);
2543: builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS, 0);
2544: builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS, 0);
2545: builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, 0);
2546: builtin_function ("__builtin_saveregs", default_function_type,
2547: BUILT_IN_SAVEREGS, 0);
2548: /* EXPAND_BUILTIN_VARARGS is obsolete. */
2549: #if 0
2550: builtin_function ("__builtin_varargs",
2551: build_function_type (ptr_type_node,
2552: tree_cons (NULL_TREE,
2553: integer_type_node,
2554: endlink)),
2555: BUILT_IN_VARARGS, 0);
2556: #endif
2557: builtin_function ("__builtin_classify_type", default_function_type,
2558: BUILT_IN_CLASSIFY_TYPE, 0);
2559: builtin_function ("__builtin_next_arg",
2560: build_function_type (ptr_type_node, endlink),
2561: BUILT_IN_NEXT_ARG, 0);
2562: builtin_function ("__builtin_args_info",
2563: build_function_type (integer_type_node,
2564: tree_cons (NULL_TREE,
2565: integer_type_node,
2566: endlink)),
2567: BUILT_IN_ARGS_INFO, 0);
2568:
2569: /* Currently under experimentation. */
2570: builtin_function ("__builtin_memcpy", memcpy_ftype,
2571: BUILT_IN_MEMCPY, "memcpy");
2572: builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
2573: BUILT_IN_MEMCMP, "memcmp");
2574: builtin_function ("__builtin_strcmp", int_ftype_string_string,
2575: BUILT_IN_STRCMP, "strcmp");
2576: builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
2577: BUILT_IN_STRCPY, "strcpy");
1.1.1.3 ! root 2578: builtin_function ("__builtin_strlen", strlen_ftype,
1.1 root 2579: BUILT_IN_STRLEN, "strlen");
1.1.1.2 root 2580: builtin_function ("__builtin_fsqrt", double_ftype_double,
2581: BUILT_IN_FSQRT, "sqrt");
1.1 root 2582: /* In an ANSI C program, it is okay to supply built-in meanings
2583: for these functions, since applications cannot validly use them
2584: with any other meaning.
1.1.1.3 ! root 2585: However, honor the -fno-builtin option. */
! 2586: if (!flag_no_builtin)
1.1 root 2587: {
2588: builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, 0);
2589: builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, 0);
2590: builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, 0);
2591: builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, 0);
2592: builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP, 0);
2593: builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP, 0);
2594: builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY, 0);
1.1.1.3 ! root 2595: builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, 0);
1.1.1.2 root 2596: builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, 0);
1.1.1.3 ! root 2597:
! 2598: /* Declare these functions volatile
! 2599: to avoid spurious "control drops through" warnings. */
! 2600: /* Don't specify the argument types, to avoid errors
! 2601: from certain code which isn't valid in ANSI but which exists. */
! 2602: temp = builtin_function ("abort", void_ftype_any, NOT_BUILT_IN, 0);
! 2603: TREE_THIS_VOLATILE (temp) = 1;
! 2604: TREE_SIDE_EFFECTS (temp) = 1;
! 2605: temp = builtin_function ("exit", void_ftype_any, NOT_BUILT_IN, 0);
! 2606: TREE_THIS_VOLATILE (temp) = 1;
! 2607: TREE_SIDE_EFFECTS (temp) = 1;
1.1 root 2608: }
2609:
2610: #if 0
2611: /* Support for these has not been written in either expand_builtin
2612: or build_function_call. */
2613: builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, 0);
2614: builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, 0);
2615: builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR, 0);
2616: builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL, 0);
2617: builtin_function ("__builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD, 0);
2618: builtin_function ("__builtin_frem", double_ftype_double_double, BUILT_IN_FREM, 0);
2619: builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int, BUILT_IN_MEMSET, 0);
2620: builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP, 0);
2621: builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN, 0);
2622: #endif
2623:
1.1.1.3 ! root 2624: /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
! 2625: declare_function_name ();
! 2626:
1.1 root 2627: start_identifier_warnings ();
2628:
2629: init_format_info_table ();
2630: }
2631:
2632: /* Return a definition for a builtin function named NAME and whose data type
2633: is TYPE. TYPE should be a function type with argument types.
2634: FUNCTION_CODE tells later passes how to compile calls to this function.
2635: See tree.h for its possible values.
2636:
2637: If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2638: the name to be called if we can't opencode the function. */
2639:
1.1.1.2 root 2640: tree
1.1 root 2641: builtin_function (name, type, function_code, library_name)
2642: char *name;
2643: tree type;
2644: enum built_in_function function_code;
2645: char *library_name;
2646: {
2647: tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2648: TREE_EXTERNAL (decl) = 1;
2649: TREE_PUBLIC (decl) = 1;
1.1.1.3 ! root 2650: /* If -traditional, permit redefining a builtin function any way you like.
! 2651: (Though really, if the program redefines these functions,
! 2652: it probably won't work right unless compiled with -fno-builtin.) */
! 2653: if (flag_traditional && name[0] != '_')
! 2654: DECL_BUILT_IN_NONANSI (decl) = 1;
1.1 root 2655: if (library_name)
2656: DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
2657: make_decl_rtl (decl, 0, 1);
2658: pushdecl (decl);
2659: if (function_code != NOT_BUILT_IN)
2660: {
2661: DECL_BUILT_IN (decl) = 1;
2662: DECL_SET_FUNCTION_CODE (decl, function_code);
2663: }
2664:
2665: return decl;
2666: }
2667:
2668: /* Called when a declaration is seen that contains no names to declare.
2669: If its type is a reference to a structure, union or enum inherited
2670: from a containing scope, shadow that tag name for the current scope
2671: with a forward reference.
2672: If its type defines a new named structure or union
2673: or defines an enum, it is valid but we need not do anything here.
2674: Otherwise, it is an error. */
2675:
2676: void
2677: shadow_tag (declspecs)
2678: tree declspecs;
2679: {
2680: int found_tag = 0;
2681: int warned = 0;
2682: register tree link;
2683:
2684: pending_invalid_xref = 0;
2685:
2686: for (link = declspecs; link; link = TREE_CHAIN (link))
2687: {
2688: register tree value = TREE_VALUE (link);
2689: register enum tree_code code = TREE_CODE (value);
2690:
2691: if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2692: /* Used to test also that TYPE_SIZE (value) != 0.
2693: That caused warning for `struct foo;' at top level in the file. */
2694: {
2695: register tree name = lookup_tag_reverse (value);
2696: register tree t;
2697:
2698: found_tag++;
2699:
2700: if (name == 0)
2701: {
2702: if (code != ENUMERAL_TYPE) /* Empty unnamed enum OK */
2703: {
2704: pedwarn ("unnamed struct/union that defines no instances");
2705: warned = 1;
2706: }
2707: }
2708: else
2709: {
2710: t = lookup_tag (code, name, current_binding_level, 1);
2711:
2712: if (t == 0)
2713: {
2714: t = make_node (code);
2715: pushtag (name, t);
2716: }
2717: }
2718: }
2719: else
2720: {
2721: if (!warned)
2722: warning ("useless keyword or type name in empty declaration");
2723: warned = 1;
2724: }
2725: }
2726:
2727: if (!warned)
2728: {
2729: if (found_tag > 1)
2730: error ("two types specified in one empty declaration");
2731: if (found_tag == 0)
2732: pedwarn ("empty declaration");
2733: }
2734: }
2735:
2736: /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2737:
2738: tree
2739: groktypename (typename)
2740: tree typename;
2741: {
2742: if (TREE_CODE (typename) != TREE_LIST)
2743: return typename;
2744: return grokdeclarator (TREE_VALUE (typename),
2745: TREE_PURPOSE (typename),
2746: TYPENAME, 0);
2747: }
2748:
2749: /* Return a PARM_DECL node for a given pair of specs and declarator. */
2750:
2751: tree
2752: groktypename_in_parm_context (typename)
2753: tree typename;
2754: {
2755: if (TREE_CODE (typename) != TREE_LIST)
2756: return typename;
2757: return grokdeclarator (TREE_VALUE (typename),
2758: TREE_PURPOSE (typename),
2759: PARM, 0);
2760: }
2761:
2762: /* Decode a declarator in an ordinary declaration or data definition.
2763: This is called as soon as the type information and variable name
2764: have been parsed, before parsing the initializer if any.
2765: Here we create the ..._DECL node, fill in its type,
2766: and put it on the list of decls for the current context.
2767: The ..._DECL node is returned as the value.
2768:
2769: Exception: for arrays where the length is not specified,
2770: the type is left null, to be filled in by `finish_decl'.
2771:
2772: Function definitions do not come here; they go to start_function
2773: instead. However, external and forward declarations of functions
2774: do go through here. Structure field declarations are done by
2775: grokfield and not through here. */
2776:
2777: /* Set this to zero to debug not using the temporary obstack
2778: to parse initializers. */
2779: int debug_temp_inits = 1;
2780:
2781: tree
2782: start_decl (declarator, declspecs, initialized)
2783: tree declspecs, declarator;
2784: int initialized;
2785: {
2786: register tree decl = grokdeclarator (declarator, declspecs,
2787: NORMAL, initialized);
2788: register tree tem;
2789: int init_written = initialized;
2790:
2791: /* The corresponding pop_obstacks is in finish_decl. */
2792: push_obstacks_nochange ();
2793:
2794: if (initialized)
2795: /* Is it valid for this decl to have an initializer at all?
2796: If not, set INITIALIZED to zero, which will indirectly
2797: tell `finish_decl' to ignore the initializer once it is parsed. */
2798: switch (TREE_CODE (decl))
2799: {
2800: case TYPE_DECL:
2801: /* typedef foo = bar means give foo the same type as bar.
2802: We haven't parsed bar yet, so `finish_decl' will fix that up.
2803: Any other case of an initialization in a TYPE_DECL is an error. */
2804: if (pedantic || list_length (declspecs) > 1)
2805: {
2806: error ("typedef `%s' is initialized",
2807: IDENTIFIER_POINTER (DECL_NAME (decl)));
2808: initialized = 0;
2809: }
2810: break;
2811:
2812: case FUNCTION_DECL:
2813: error ("function `%s' is initialized like a variable",
2814: IDENTIFIER_POINTER (DECL_NAME (decl)));
2815: initialized = 0;
2816: break;
2817:
2818: case PARM_DECL:
2819: /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2820: error ("parameter `%s' is initialized",
2821: IDENTIFIER_POINTER (DECL_NAME (decl)));
2822: initialized = 0;
2823: break;
2824:
2825: default:
2826: /* Don't allow initializations for incomplete types
2827: except for arrays which might be completed by the initialization. */
2828: if (TYPE_SIZE (TREE_TYPE (decl)) != 0)
2829: {
2830: /* A complete type is ok if size is fixed. */
2831:
2832: if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2833: || C_DECL_VARIABLE_SIZE (decl))
2834: {
2835: error ("variable-sized object may not be initialized");
2836: initialized = 0;
2837: }
2838: }
2839: else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2840: {
2841: error ("variable `%s' has initializer but incomplete type",
2842: IDENTIFIER_POINTER (DECL_NAME (decl)));
2843: initialized = 0;
2844: }
2845: else if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))) == 0)
2846: {
2847: error ("elements of array `%s' have incomplete type",
2848: IDENTIFIER_POINTER (DECL_NAME (decl)));
2849: initialized = 0;
2850: }
2851: }
2852:
2853: if (initialized)
2854: {
2855: #if 0 /* Seems redundant with grokdeclarator. */
2856: if (current_binding_level != global_binding_level
2857: && TREE_EXTERNAL (decl)
2858: && TREE_CODE (decl) != FUNCTION_DECL)
2859: warning ("declaration of `%s' has `extern' and is initialized",
2860: IDENTIFIER_POINTER (DECL_NAME (decl)));
2861: #endif
2862: TREE_EXTERNAL (decl) = 0;
2863: if (current_binding_level == global_binding_level)
2864: TREE_STATIC (decl) = 1;
2865:
2866: /* Tell `pushdecl' this is an initialized decl
2867: even though we don't yet have the initializer expression.
2868: Also tell `finish_decl' it may store the real initializer. */
2869: DECL_INITIAL (decl) = error_mark_node;
2870: }
2871:
2872: /* If this is a function declaration, write a record describing it to the
2873: prototypes file (if requested). */
2874:
2875: if (TREE_CODE (decl) == FUNCTION_DECL)
2876: gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2877:
2878: /* Add this decl to the current binding level.
2879: TEM may equal DECL or it may be a previous decl of the same name. */
2880: tem = pushdecl (decl);
2881:
2882: /* For a local variable, define the RTL now. */
2883: if (current_binding_level != global_binding_level
2884: /* But not if this is a duplicate decl
2885: and we preserved the rtl from the previous one
2886: (which may or may not happen). */
2887: && DECL_RTL (tem) == 0)
2888: {
2889: if (TYPE_SIZE (TREE_TYPE (tem)) != 0)
2890: expand_decl (tem);
2891: else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2892: && DECL_INITIAL (tem) != 0)
2893: expand_decl (tem);
2894: }
2895:
2896: if (init_written)
2897: {
2898: /* When parsing and digesting the initializer,
2899: use temporary storage. Do this even if we will ignore the value. */
2900: if (current_binding_level == global_binding_level && debug_temp_inits)
2901: temporary_allocation ();
2902: }
2903:
2904: return tem;
2905: }
2906:
2907: /* Finish processing of a declaration;
2908: install its initial value.
2909: If the length of an array type is not known before,
2910: it must be determined now, from the initial value, or it is an error. */
2911:
2912: void
2913: finish_decl (decl, init, asmspec_tree)
2914: tree decl, init;
2915: tree asmspec_tree;
2916: {
2917: register tree type = TREE_TYPE (decl);
2918: int was_incomplete = (DECL_SIZE (decl) == 0);
2919: int temporary = allocation_temporary_p ();
2920: char *asmspec = 0;
2921:
2922: if (asmspec_tree)
2923: asmspec = TREE_STRING_POINTER (asmspec_tree);
2924:
2925: /* If `start_decl' didn't like having an initialization, ignore it now. */
2926:
2927: if (init != 0 && DECL_INITIAL (decl) == 0)
2928: init = 0;
2929: /* Don't crash if parm is initialized. */
2930: if (TREE_CODE (decl) == PARM_DECL)
2931: init = 0;
2932:
2933: if (init)
2934: {
2935: if (TREE_CODE (decl) != TYPE_DECL)
2936: store_init_value (decl, init);
2937: else
2938: {
2939: /* typedef foo = bar; store the type of bar as the type of foo. */
2940: TREE_TYPE (decl) = TREE_TYPE (init);
2941: DECL_INITIAL (decl) = init = 0;
2942: }
2943: }
2944:
2945: /* For top-level declaration, the initial value was read in
2946: the temporary obstack. MAXINDEX, rtl, etc. to be made below
2947: must go in the permanent obstack; but don't discard the
2948: temporary data yet. */
2949:
2950: if (current_binding_level == global_binding_level && temporary)
2951: end_temporary_allocation ();
2952:
2953: /* Deduce size of array from initialization, if not already known */
2954:
2955: if (TREE_CODE (type) == ARRAY_TYPE
2956: && TYPE_DOMAIN (type) == 0
2957: && TREE_CODE (decl) != TYPE_DECL)
2958: {
2959: int do_default
2960: = (TREE_STATIC (decl)
2961: /* Even if pedantic, an external linkage array
2962: may have incomplete type at first. */
2963: ? pedantic && !TREE_PUBLIC (decl)
2964: : !TREE_EXTERNAL (decl));
2965: int failure
2966: = complete_array_type (type, DECL_INITIAL (decl), do_default);
2967:
2968: /* Get the completed type made by complete_array_type. */
2969: type = TREE_TYPE (decl);
2970:
2971: if (failure == 1)
2972: error_with_decl (decl, "initializer fails to determine size of `%s'");
2973:
2974: if (failure == 2)
2975: {
2976: if (do_default)
2977: error_with_decl (decl, "array size missing in `%s'");
2978: else if (!pedantic && TREE_STATIC (decl))
2979: TREE_EXTERNAL (decl) = 1;
2980: }
2981:
2982: if (pedantic && TYPE_DOMAIN (type) != 0
2983: && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
2984: integer_zero_node))
2985: error_with_decl (decl, "zero-size array `%s'");
2986:
2987: layout_decl (decl, 0);
2988: }
2989:
2990: if (TREE_CODE (decl) == VAR_DECL)
2991: {
2992: if (TREE_STATIC (decl) && DECL_SIZE (decl) == 0)
2993: {
2994: /* A static variable with an incomplete type:
2995: that is an error if it is initialized or `static'.
2996: Otherwise, let it through, but if it is not `extern'
2997: then it may cause an error message later. */
2998: if (! (TREE_PUBLIC (decl) && DECL_INITIAL (decl) == 0))
2999: error_with_decl (decl, "storage size of `%s' isn't known");
3000: }
3001: else if (!TREE_EXTERNAL (decl) && DECL_SIZE (decl) == 0)
3002: {
3003: /* An automatic variable with an incomplete type:
3004: that is an error. */
3005: error_with_decl (decl, "storage size of `%s' isn't known");
3006: TREE_TYPE (decl) = error_mark_node;
3007: }
3008:
3009: if ((TREE_EXTERNAL (decl) || TREE_STATIC (decl))
3010: && DECL_SIZE (decl) != 0
3011: && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
3012: error_with_decl (decl, "storage size of `%s' isn't constant");
3013: }
3014:
3015: /* Output the assembler code and/or RTL code for variables and functions,
3016: unless the type is an undefined structure or union.
3017: If not, it will get done when the type is completed. */
3018:
3019: if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3020: {
3021: if (flag_traditional && allocation_temporary_p ())
3022: {
3023: push_obstacks_nochange ();
3024: end_temporary_allocation ();
3025: /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3026: maybe_objc_check_decl (decl);
3027: rest_of_decl_compilation (decl, asmspec,
3028: current_binding_level == global_binding_level,
3029: 0);
3030: pop_obstacks ();
3031: }
3032: else
3033: {
3034: /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3035: maybe_objc_check_decl (decl);
3036: rest_of_decl_compilation (decl, asmspec,
3037: current_binding_level == global_binding_level,
3038: 0);
3039: }
3040: if (current_binding_level != global_binding_level)
3041: {
3042: /* Recompute the RTL of a local array now
3043: if it used to be an incomplete type. */
3044: if (was_incomplete
3045: && ! TREE_STATIC (decl) && ! TREE_EXTERNAL (decl))
3046: {
3047: /* If we used it already as memory, it must stay in memory. */
3048: TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3049: /* If it's still incomplete now, no init will save it. */
3050: if (DECL_SIZE (decl) == 0)
3051: DECL_INITIAL (decl) = 0;
3052: expand_decl (decl);
3053: }
3054: /* Compute and store the initial value. */
3055: expand_decl_init (decl);
3056: }
3057: }
3058:
3059: if (TREE_CODE (decl) == TYPE_DECL)
3060: {
3061: /* This is a no-op in c-lang.c or something real in objc-actions.c. */
3062: maybe_objc_check_decl (decl);
3063: rest_of_decl_compilation (decl, 0,
3064: current_binding_level == global_binding_level,
3065: 0);
3066: }
3067:
3068: if (temporary && TREE_PERMANENT (decl))
3069: {
3070: /* We need to remember that this array HAD an initialization,
3071: but discard the actual temporary nodes,
3072: since we can't have a permanent node keep pointing to them. */
3073: if (DECL_INITIAL (decl) != 0)
3074: DECL_INITIAL (decl) = error_mark_node;
3075: }
3076:
3077: /* Resume permanent allocation, if not within a function. */
3078: /* The corresponding push_obstacks_nochange is in start_decl,
3079: and in push_parm_decl and in grokfield. */
3080: pop_obstacks ();
3081: if (current_binding_level == global_binding_level && temporary)
3082: /* Actually free the temporary space that we no longer need. */
3083: permanent_allocation ();
3084:
3085: /* At the end of a declaration, throw away any variable type sizes
3086: of types defined inside that declaration. There is no use
3087: computing them in the following function definition. */
3088: if (current_binding_level == global_binding_level)
3089: get_pending_sizes ();
3090: }
3091:
3092: /* If DECL has a cleanup, build and return that cleanup here.
3093: This is a callback called by expand_expr. */
3094:
3095: tree
3096: maybe_build_cleanup (decl)
3097: tree decl;
3098: {
3099: /* There are no cleanups in C. */
3100: return NULL_TREE;
3101: }
3102:
3103: /* Given a parsed parameter declaration,
3104: decode it into a PARM_DECL and push that on the current binding level.
3105: Also, for the sake of forward parm decls,
3106: record the given order of parms in `parm_order'. */
3107:
3108: void
3109: push_parm_decl (parm)
3110: tree parm;
3111: {
1.1.1.3 ! root 3112: tree decl, olddecl;
1.1.1.2 root 3113: int old_immediate_size_expand = immediate_size_expand;
3114: /* Don't try computing parm sizes now -- wait till fn is called. */
3115: immediate_size_expand = 0;
1.1 root 3116:
3117: /* The corresponding pop_obstacks is in finish_decl. */
3118: push_obstacks_nochange ();
3119:
3120: decl = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), PARM, 0);
1.1.1.3 ! root 3121: if (DECL_NAME (decl))
! 3122: {
! 3123: olddecl = lookup_name (DECL_NAME (decl));
! 3124: if (pedantic && olddecl != 0 && TREE_CODE (olddecl) == TYPE_DECL)
! 3125: pedwarn_with_decl (decl, "ANSI C forbids parameter `%s' shadowing typedef");
! 3126: }
1.1 root 3127: decl = pushdecl (decl);
3128:
1.1.1.2 root 3129: immediate_size_expand = old_immediate_size_expand;
3130:
1.1 root 3131: current_binding_level->parm_order
3132: = tree_cons (NULL_TREE, decl, current_binding_level->parm_order);
3133:
3134: /* Add this decl to the current binding level. */
3135: finish_decl (decl, NULL_TREE, NULL_TREE);
3136: }
3137:
3138: /* Clear the given order of parms in `parm_order'.
3139: Used at start of parm list,
3140: and also at semicolon terminating forward decls. */
3141:
3142: void
3143: clear_parm_order ()
3144: {
3145: current_binding_level->parm_order = NULL_TREE;
3146: }
3147:
3148: /* Make TYPE a complete type based on INITIAL_VALUE.
1.1.1.2 root 3149: Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
1.1 root 3150: 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3151:
3152: int
3153: complete_array_type (type, initial_value, do_default)
3154: tree type;
3155: tree initial_value;
3156: int do_default;
3157: {
3158: register tree maxindex = NULL_TREE;
3159: int value = 0;
3160:
3161: if (initial_value)
3162: {
3163: /* Note MAXINDEX is really the maximum index,
3164: one less than the size. */
3165: if (TREE_CODE (initial_value) == STRING_CST)
3166: {
3167: int eltsize
3168: = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3169: maxindex = build_int_2 (TREE_STRING_LENGTH (initial_value) / eltsize - 1, 0);
3170: }
3171: else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3172: {
3173: register int nelts
3174: = list_length (CONSTRUCTOR_ELTS (initial_value));
3175: maxindex = build_int_2 (nelts - 1, 0);
3176: }
3177: else
3178: {
3179: /* Make an error message unless that happened already. */
3180: if (initial_value != error_mark_node)
3181: value = 1;
3182:
3183: /* Prevent further error messages. */
3184: maxindex = build_int_2 (1, 0);
3185: }
3186: }
3187:
3188: if (!maxindex)
3189: {
3190: if (do_default)
3191: maxindex = build_int_2 (1, 0);
3192: value = 2;
3193: }
3194:
3195: if (maxindex)
3196: {
3197: TYPE_DOMAIN (type) = build_index_type (maxindex);
3198: if (!TREE_TYPE (maxindex))
3199: TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3200: }
3201:
3202: /* Lay out the type now that we can get the real answer. */
3203:
3204: layout_type (type);
3205:
3206: return value;
3207: }
3208:
3209: /* Given declspecs and a declarator,
3210: determine the name and type of the object declared
3211: and construct a ..._DECL node for it.
3212: (In one case we can return a ..._TYPE node instead.
3213: For invalid input we sometimes return 0.)
3214:
3215: DECLSPECS is a chain of tree_list nodes whose value fields
3216: are the storage classes and type specifiers.
3217:
3218: DECL_CONTEXT says which syntactic context this declaration is in:
3219: NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3220: FUNCDEF for a function definition. Like NORMAL but a few different
3221: error messages in each case. Return value may be zero meaning
3222: this definition is too screwy to try to parse.
3223: PARM for a parameter declaration (either within a function prototype
3224: or before a function body). Make a PARM_DECL, or return void_type_node.
3225: TYPENAME if for a typename (in a cast or sizeof).
3226: Don't make a DECL node; just return the ..._TYPE node.
3227: FIELD for a struct or union field; make a FIELD_DECL.
3228: BITFIELD for a field with specified width.
3229: INITIALIZED is 1 if the decl has an initializer.
3230:
3231: In the TYPENAME case, DECLARATOR is really an absolute declarator.
3232: It may also be so in the PARM case, for a prototype where the
3233: argument type is specified but not the name.
3234:
3235: This function is where the complicated C meanings of `static'
1.1.1.2 root 3236: and `extern' are interpreted. */
1.1 root 3237:
3238: static tree
3239: grokdeclarator (declarator, declspecs, decl_context, initialized)
3240: tree declspecs;
3241: tree declarator;
3242: enum decl_context decl_context;
3243: int initialized;
3244: {
3245: int specbits = 0;
3246: tree spec;
3247: tree type = NULL_TREE;
3248: int longlong = 0;
3249: int constp;
3250: int volatilep;
3251: int inlinep;
3252: int explicit_int = 0;
3253: int explicit_char = 0;
3254: tree typedef_decl = 0;
3255: char *name;
3256: tree typedef_type = 0;
3257: int funcdef_flag = 0;
3258: enum tree_code innermost_code = ERROR_MARK;
3259: int bitfield = 0;
1.1.1.2 root 3260: int size_varies = 0;
1.1 root 3261:
3262: if (decl_context == BITFIELD)
3263: bitfield = 1, decl_context = FIELD;
3264:
3265: if (decl_context == FUNCDEF)
3266: funcdef_flag = 1, decl_context = NORMAL;
3267:
3268: push_obstacks_nochange ();
3269:
3270: if (flag_traditional && allocation_temporary_p ())
3271: end_temporary_allocation ();
3272:
3273: /* Look inside a declarator for the name being declared
3274: and get it as a string, for an error message. */
3275: {
3276: register tree decl = declarator;
3277: name = 0;
3278:
3279: while (decl)
3280: switch (TREE_CODE (decl))
3281: {
3282: case ARRAY_REF:
3283: case INDIRECT_REF:
3284: case CALL_EXPR:
3285: innermost_code = TREE_CODE (decl);
3286: decl = TREE_OPERAND (decl, 0);
3287: break;
3288:
3289: case IDENTIFIER_NODE:
3290: name = IDENTIFIER_POINTER (decl);
3291: decl = 0;
3292: break;
3293:
3294: default:
3295: abort ();
3296: }
3297: if (name == 0)
3298: name = "type name";
3299: }
3300:
3301: /* A function definition's declarator must have the form of
3302: a function declarator. */
3303:
3304: if (funcdef_flag && innermost_code != CALL_EXPR)
3305: return 0;
3306:
3307: /* Anything declared one level down from the top level
3308: must be one of the parameters of a function
3309: (because the body is at least two levels down). */
3310:
3311: /* If this looks like a function definition, make it one,
3312: even if it occurs where parms are expected.
3313: Then store_parm_decls will reject it and not use it as a parm. */
3314: if (decl_context == NORMAL && !funcdef_flag
3315: && current_binding_level->level_chain == global_binding_level)
3316: decl_context = PARM;
3317:
3318: /* Look through the decl specs and record which ones appear.
3319: Some typespecs are defined as built-in typenames.
3320: Others, the ones that are modifiers of other types,
3321: are represented by bits in SPECBITS: set the bits for
3322: the modifiers that appear. Storage class keywords are also in SPECBITS.
3323:
3324: If there is a typedef name or a type, store the type in TYPE.
3325: This includes builtin typedefs such as `int'.
3326:
3327: Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3328: and did not come from a user typedef.
3329:
3330: Set LONGLONG if `long' is mentioned twice. */
3331:
3332: for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3333: {
3334: register int i;
3335: register tree id = TREE_VALUE (spec);
3336:
3337: if (id == ridpointers[(int) RID_INT])
3338: explicit_int = 1;
3339: if (id == ridpointers[(int) RID_CHAR])
3340: explicit_char = 1;
3341:
3342: if (TREE_CODE (id) == IDENTIFIER_NODE)
3343: for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
3344: {
3345: if (ridpointers[i] == id)
3346: {
3347: if (i == (int) RID_LONG && specbits & (1<<i))
3348: {
3349: if (pedantic)
3350: pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3351: else if (longlong)
3352: warning ("`long long long' is too long for GCC");
3353: else
3354: longlong = 1;
3355: }
3356: else if (specbits & (1 << i))
1.1.1.3 ! root 3357: pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
1.1 root 3358: specbits |= 1 << i;
3359: goto found;
3360: }
3361: }
3362: if (type)
3363: error ("two or more data types in declaration of `%s'", name);
3364: /* Actual typedefs come to us as TYPE_DECL nodes. */
3365: else if (TREE_CODE (id) == TYPE_DECL)
3366: {
3367: type = TREE_TYPE (id);
3368: typedef_decl = id;
3369: }
3370: /* Built-in types come as identifiers. */
3371: else if (TREE_CODE (id) == IDENTIFIER_NODE)
3372: {
3373: register tree t = lookup_name (id);
3374: if (TREE_TYPE (t) == error_mark_node)
3375: ;
3376: else if (!t || TREE_CODE (t) != TYPE_DECL)
3377: error ("`%s' fails to be a typedef or built in type",
3378: IDENTIFIER_POINTER (id));
3379: else
3380: {
3381: type = TREE_TYPE (t);
3382: typedef_decl = t;
3383: }
3384: }
3385: else if (TREE_CODE (id) != ERROR_MARK)
3386: type = id;
3387:
3388: found: {}
3389: }
3390:
3391: typedef_type = type;
3392: if (type)
1.1.1.2 root 3393: size_varies = C_TYPE_VARIABLE_SIZE (type);
1.1 root 3394:
3395: /* No type at all: default to `int', and set EXPLICIT_INT
3396: because it was not a user-defined typedef. */
3397:
3398: if (type == 0)
3399: {
3400: if (funcdef_flag && warn_return_type
3401: && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3402: | (1 << (int) RID_SIGNED) | (1 << (int) RID_UNSIGNED))))
3403: warn_about_return_type = 1;
3404: explicit_int = 1;
3405: type = integer_type_node;
3406: }
3407:
3408: /* Now process the modifiers that were specified
3409: and check for invalid combinations. */
3410:
3411: /* Long double is a special combination. */
3412:
3413: if ((specbits & 1 << (int) RID_LONG) && type == double_type_node)
3414: {
3415: specbits &= ~ (1 << (int) RID_LONG);
3416: type = long_double_type_node;
3417: }
3418:
3419: /* Check all other uses of type modifiers. */
3420:
3421: if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3422: | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3423: {
3424: int ok = 0;
3425:
3426: if (TREE_CODE (type) != INTEGER_TYPE)
3427: error ("long, short, signed or unsigned invalid for `%s'", name);
3428: else if ((specbits & 1 << (int) RID_LONG)
3429: && (specbits & 1 << (int) RID_SHORT))
3430: error ("long and short specified together for `%s'", name);
3431: else if (((specbits & 1 << (int) RID_LONG)
3432: || (specbits & 1 << (int) RID_SHORT))
3433: && explicit_char)
3434: error ("long or short specified with char for `%s'", name);
3435: else if (((specbits & 1 << (int) RID_LONG)
3436: || (specbits & 1 << (int) RID_SHORT))
3437: && TREE_CODE (type) == REAL_TYPE)
3438: error ("long or short specified with floating type for `%s'", name);
3439: else if ((specbits & 1 << (int) RID_SIGNED)
3440: && (specbits & 1 << (int) RID_UNSIGNED))
3441: error ("signed and unsigned given together for `%s'", name);
3442: else
3443: {
3444: ok = 1;
3445: if (!explicit_int && !explicit_char && pedantic)
3446: {
3447: pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
3448: name);
3449: if (flag_pedantic_errors)
3450: ok = 0;
3451: }
3452: }
3453:
3454: /* Discard the type modifiers if they are invalid. */
3455: if (! ok)
3456: {
3457: specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3458: | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3459: longlong = 0;
3460: }
3461: }
3462:
3463: /* Decide whether an integer type is signed or not.
3464: Optionally treat bitfields as signed by default. */
3465: if (specbits & 1 << (int) RID_UNSIGNED
3466: /* Traditionally, all bitfields are unsigned. */
3467: || (bitfield && flag_traditional)
3468: || (bitfield && ! flag_signed_bitfields
3469: && (explicit_int || explicit_char
3470: /* A typedef for plain `int' without `signed'
3471: can be controlled just like plain `int'. */
3472: || ! (typedef_decl != 0
3473: && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3474: && TREE_CODE (type) != ENUMERAL_TYPE
3475: && !(specbits & 1 << (int) RID_SIGNED)))
3476: {
3477: if (longlong)
3478: type = long_long_unsigned_type_node;
3479: else if (specbits & 1 << (int) RID_LONG)
3480: type = long_unsigned_type_node;
3481: else if (specbits & 1 << (int) RID_SHORT)
3482: type = short_unsigned_type_node;
3483: else if (type == char_type_node)
3484: type = unsigned_char_type_node;
3485: else if (typedef_decl)
3486: type = unsigned_type (type);
3487: else
3488: type = unsigned_type_node;
3489: }
3490: else if ((specbits & 1 << (int) RID_SIGNED)
3491: && type == char_type_node)
3492: type = signed_char_type_node;
3493: else if (longlong)
3494: type = long_long_integer_type_node;
3495: else if (specbits & 1 << (int) RID_LONG)
3496: type = long_integer_type_node;
3497: else if (specbits & 1 << (int) RID_SHORT)
3498: type = short_integer_type_node;
3499:
3500: /* Set CONSTP if this declaration is `const', whether by
3501: explicit specification or via a typedef.
3502: Likewise for VOLATILEP. */
3503:
3504: constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3505: volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3506: inlinep = !! (specbits & (1 << (int) RID_INLINE));
3507: if (constp > 1)
1.1.1.3 ! root 3508: pedwarn ("duplicate `const'");
1.1 root 3509: if (volatilep > 1)
1.1.1.3 ! root 3510: pedwarn ("duplicate `volatile'");
1.1 root 3511: if (! flag_gen_aux_info && (TYPE_READONLY (type) || TYPE_VOLATILE (type)))
3512: type = TYPE_MAIN_VARIANT (type);
3513:
3514: /* Warn if two storage classes are given. Default to `auto'. */
3515:
3516: {
3517: int nclasses = 0;
3518:
3519: if (specbits & 1 << (int) RID_AUTO) nclasses++;
3520: if (specbits & 1 << (int) RID_STATIC) nclasses++;
3521: if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3522: if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3523: if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3524:
3525: /* Warn about storage classes that are invalid for certain
3526: kinds of declarations (parameters, typenames, etc.). */
3527:
3528: if (nclasses > 1)
3529: error ("multiple storage classes in declaration of `%s'", name);
3530: else if (funcdef_flag
3531: && (specbits
3532: & ((1 << (int) RID_REGISTER)
3533: | (1 << (int) RID_AUTO)
3534: | (1 << (int) RID_TYPEDEF))))
3535: {
3536: if (specbits & 1 << (int) RID_AUTO
3537: && (pedantic || current_binding_level == global_binding_level))
3538: pedwarn ("function definition declared `auto'");
3539: if (specbits & 1 << (int) RID_REGISTER)
3540: error ("function definition declared `register'");
3541: if (specbits & 1 << (int) RID_TYPEDEF)
3542: error ("function definition declared `typedef'");
3543: specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3544: | (1 << (int) RID_AUTO));
3545: }
3546: else if (decl_context != NORMAL && nclasses > 0)
3547: {
3548: if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3549: ;
3550: else
3551: {
3552: error ((decl_context == FIELD
3553: ? "storage class specified for structure field `%s'"
3554: : (decl_context == PARM
3555: ? "storage class specified for parameter `%s'"
3556: : "storage class specified for typename")),
3557: name);
3558: specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3559: | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3560: | (1 << (int) RID_EXTERN));
3561: }
3562: }
3563: else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3564: {
3565: /* `extern' with initialization is invalid if not at top level. */
3566: if (current_binding_level == global_binding_level)
3567: warning ("`%s' initialized and declared `extern'", name);
3568: else
3569: error ("`%s' has both `extern' and initializer", name);
3570: }
3571: else if (specbits & 1 << (int) RID_EXTERN && funcdef_flag
3572: && current_binding_level != global_binding_level)
3573: error ("nested function `%s' declared `extern'", name);
3574: else if (current_binding_level == global_binding_level
3575: && specbits & (1 << (int) RID_AUTO))
3576: error ("top-level declaration of `%s' specifies `auto'", name);
3577: }
3578:
3579: /* Now figure out the structure of the declarator proper.
3580: Descend through it, creating more complex types, until we reach
3581: the declared identifier (or NULL_TREE, in an absolute declarator). */
3582:
3583: while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3584: {
3585: if (type == error_mark_node)
3586: {
3587: declarator = TREE_OPERAND (declarator, 0);
3588: continue;
3589: }
3590:
3591: /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3592: an INDIRECT_REF (for *...),
3593: a CALL_EXPR (for ...(...)),
3594: an identifier (for the name being declared)
3595: or a null pointer (for the place in an absolute declarator
3596: where the name was omitted).
3597: For the last two cases, we have just exited the loop.
3598:
3599: At this point, TYPE is the type of elements of an array,
3600: or for a function to return, or for a pointer to point to.
3601: After this sequence of ifs, TYPE is the type of the
3602: array or function or pointer, and DECLARATOR has had its
3603: outermost layer removed. */
3604:
3605: if (TREE_CODE (declarator) == ARRAY_REF)
3606: {
3607: register tree itype = NULL_TREE;
3608: register tree size = TREE_OPERAND (declarator, 1);
3609:
3610: declarator = TREE_OPERAND (declarator, 0);
3611:
3612: /* Check for some types that there cannot be arrays of. */
3613:
3614: if (type == void_type_node)
3615: {
3616: error ("declaration of `%s' as array of voids", name);
3617: type = error_mark_node;
3618: }
3619:
3620: if (TREE_CODE (type) == FUNCTION_TYPE)
3621: {
3622: error ("declaration of `%s' as array of functions", name);
3623: type = error_mark_node;
3624: }
3625:
3626: if (size == error_mark_node)
3627: type = error_mark_node;
3628:
3629: if (type == error_mark_node)
3630: continue;
3631:
3632: /* If size was specified, set ITYPE to a range-type for that size.
3633: Otherwise, ITYPE remains null. finish_decl may figure it out
3634: from an initial value. */
3635:
3636: if (size)
3637: {
3638: /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3639: while (TREE_CODE (size) == NON_LVALUE_EXPR)
3640: size = TREE_OPERAND (size, 0);
3641:
3642: if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
3643: && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
3644: {
3645: error ("size of array `%s' has non-integer type", name);
3646: size = integer_one_node;
3647: }
3648: if (pedantic && integer_zerop (size))
3649: pedwarn ("ANSI C forbids zero-size array `%s'", name);
3650: if (TREE_CODE (size) == INTEGER_CST)
3651: {
3652: if (INT_CST_LT (size, integer_zero_node))
3653: {
3654: error ("size of array `%s' is negative", name);
3655: size = integer_one_node;
3656: }
1.1.1.3 ! root 3657: itype = build_index_type (size_binop (MINUS_EXPR, size,
! 3658: size_one_node));
1.1 root 3659: }
3660: else
3661: {
3662: if (pedantic)
3663: pedwarn ("ANSI C forbids variable-size array `%s'", name);
3664: itype = build_binary_op (MINUS_EXPR, size, integer_one_node,
3665: 1);
3666: /* Make sure the array size remains visibly nonconstant
3667: even if it is (eg) a const variable with known value. */
1.1.1.2 root 3668: size_varies = 1;
3669: itype = variable_size (itype);
3670: itype = build_index_type (itype);
1.1 root 3671: }
3672: }
3673:
3674: #if 0 /* This had bad results for pointers to arrays, as in
3675: union incomplete (*foo)[4]; */
3676: /* Complain about arrays of incomplete types, except in typedefs. */
3677:
3678: if (TYPE_SIZE (type) == 0
3679: /* Avoid multiple warnings for nested array types. */
3680: && TREE_CODE (type) != ARRAY_TYPE
3681: && !(specbits & (1 << (int) RID_TYPEDEF))
3682: && !C_TYPE_BEING_DEFINED (type))
3683: warning ("array type has incomplete element type");
3684: #endif
3685:
3686: /* Build the array type itself.
3687: Merge any constancy or volatility into the target type. */
3688:
3689: #if 0 /* We shouldn't have a function type here at all!
3690: Functions aren't allowed as array elements. */
3691: if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3692: && (constp || volatilep))
3693: pedwarn ("ANSI C forbids const or volatile function types");
3694: #endif
3695: if (constp || volatilep)
3696: type = c_build_type_variant (type, constp, volatilep);
3697:
3698: #if 0 /* don't clear these; leave them set so that the array type
3699: or the variable is itself const or volatile. */
3700: constp = 0;
3701: volatilep = 0;
3702: #endif
3703:
3704: type = build_array_type (type, itype);
1.1.1.2 root 3705: if (size_varies)
1.1 root 3706: C_TYPE_VARIABLE_SIZE (type) = 1;
3707: }
3708: else if (TREE_CODE (declarator) == CALL_EXPR)
3709: {
3710: tree arg_types;
3711:
3712: /* Declaring a function type.
3713: Make sure we have a valid type for the function to return. */
3714: if (type == error_mark_node)
3715: continue;
3716:
1.1.1.2 root 3717: size_varies = 0;
1.1 root 3718:
3719: /* Warn about some types functions can't return. */
3720:
3721: if (TREE_CODE (type) == FUNCTION_TYPE)
3722: {
3723: error ("`%s' declared as function returning a function", name);
3724: type = integer_type_node;
3725: }
3726: if (TREE_CODE (type) == ARRAY_TYPE)
3727: {
3728: error ("`%s' declared as function returning an array", name);
3729: type = integer_type_node;
3730: }
3731:
3732: #ifndef TRADITIONAL_RETURN_FLOAT
3733: /* Traditionally, declaring return type float means double. */
3734:
3735: if (flag_traditional && type == float_type_node)
3736: type = double_type_node;
3737: #endif /* TRADITIONAL_RETURN_FLOAT */
3738:
3739: /* Construct the function type and go to the next
3740: inner layer of declarator. */
3741:
3742: arg_types = grokparms (TREE_OPERAND (declarator, 1),
3743: funcdef_flag
3744: /* Say it's a definition
3745: only for the CALL_EXPR
3746: closest to the identifier. */
3747: && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3748: #if 0 /* This seems to be false. We turn off temporary allocation
3749: above in this function if -traditional.
3750: And this code caused inconsistent results with prototypes:
3751: callers would ignore them, and pass arguments wrong. */
3752:
3753: /* Omit the arg types if -traditional, since the arg types
3754: and the list links might not be permanent. */
3755: type = build_function_type (type, flag_traditional ? 0 : arg_types);
3756: #endif
3757: type = build_function_type (type, arg_types);
3758: declarator = TREE_OPERAND (declarator, 0);
3759:
3760: /* Set the TYPE_CONTEXTs for each tagged type which is local to
3761: the formal parameter list of this FUNCTION_TYPE to point to
3762: the FUNCTION_TYPE node itself. */
3763:
3764: {
3765: register tree link;
3766:
3767: for (link = current_function_parm_tags;
3768: link;
3769: link = TREE_CHAIN (link))
3770: TYPE_CONTEXT (TREE_VALUE (link)) = type;
3771: }
3772: }
3773: else if (TREE_CODE (declarator) == INDIRECT_REF)
3774: {
3775: /* Merge any constancy or volatility into the target type
3776: for the pointer. */
3777:
3778: if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3779: && (constp || volatilep))
3780: pedwarn ("ANSI C forbids const or volatile function types");
3781: if (constp || volatilep)
3782: type = c_build_type_variant (type, constp, volatilep);
3783: constp = 0;
3784: volatilep = 0;
1.1.1.2 root 3785: size_varies = 0;
1.1 root 3786:
3787: type = build_pointer_type (type);
3788:
3789: /* Process a list of type modifier keywords
3790: (such as const or volatile) that were given inside the `*'. */
3791:
3792: if (TREE_TYPE (declarator))
3793: {
3794: register tree typemodlist;
3795: int erred = 0;
3796: for (typemodlist = TREE_TYPE (declarator); typemodlist;
3797: typemodlist = TREE_CHAIN (typemodlist))
3798: {
3799: if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
3800: constp++;
3801: else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
3802: volatilep++;
3803: else if (!erred)
3804: {
3805: erred = 1;
3806: error ("invalid type modifier within pointer declarator");
3807: }
3808: }
3809: if (constp > 1)
3810: warning ("duplicate `const'");
3811: if (volatilep > 1)
3812: warning ("duplicate `volatile'");
3813: }
3814:
3815: declarator = TREE_OPERAND (declarator, 0);
3816: }
3817: else
3818: abort ();
3819:
3820: }
3821:
3822: /* Now TYPE has the actual type. */
3823:
3824: /* If this is declaring a typedef name, return a TYPE_DECL. */
3825:
3826: if (specbits & (1 << (int) RID_TYPEDEF))
3827: {
3828: tree decl;
3829: /* Note that the grammar rejects storage classes
3830: in typenames, fields or parameters */
3831: if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3832: && (constp || volatilep))
3833: pedwarn ("ANSI C forbids const or volatile function types");
3834: if (constp || volatilep)
3835: type = c_build_type_variant (type, constp, volatilep);
3836: pop_obstacks ();
3837: decl = build_decl (TYPE_DECL, declarator, type);
3838: if ((specbits & (1 << (int) RID_SIGNED))
3839: || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3840: C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
3841: return decl;
3842: }
3843:
3844: /* Detect the case of an array type of unspecified size
3845: which came, as such, direct from a typedef name.
3846: We must copy the type, so that each identifier gets
3847: a distinct type, so that each identifier's size can be
3848: controlled separately by its own initializer. */
3849:
3850: if (type != 0 && typedef_type != 0
3851: && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)
3852: && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0)
3853: {
3854: type = build_array_type (TREE_TYPE (type), 0);
1.1.1.2 root 3855: if (size_varies)
1.1 root 3856: C_TYPE_VARIABLE_SIZE (type) = 1;
3857: }
3858:
3859: /* If this is a type name (such as, in a cast or sizeof),
3860: compute the type and return it now. */
3861:
3862: if (decl_context == TYPENAME)
3863: {
3864: /* Note that the grammar rejects storage classes
3865: in typenames, fields or parameters */
3866: if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
3867: && (constp || volatilep))
3868: pedwarn ("ANSI C forbids const or volatile function types");
3869: if (constp || volatilep)
3870: type = c_build_type_variant (type, constp, volatilep);
3871: pop_obstacks ();
3872: return type;
3873: }
3874:
3875: /* `void' at top level (not within pointer)
3876: is allowed only in typedefs or type names.
3877: We don't complain about parms either, but that is because
3878: a better error message can be made later. */
3879:
3880: if (type == void_type_node && decl_context != PARM)
3881: {
3882: error ("variable or field `%s' declared void",
3883: IDENTIFIER_POINTER (declarator));
3884: type = integer_type_node;
3885: }
3886:
3887: /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
3888: or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
3889:
3890: {
3891: register tree decl;
3892:
3893: if (decl_context == PARM)
3894: {
3895: tree type_as_written = type;
3896:
3897: /* A parameter declared as an array of T is really a pointer to T.
3898: One declared as a function is really a pointer to a function. */
3899:
3900: if (TREE_CODE (type) == ARRAY_TYPE)
3901: {
3902: /* Transfer const-ness of array into that of type pointed to. */
3903: type = build_pointer_type
3904: (c_build_type_variant (TREE_TYPE (type), constp, volatilep));
3905: volatilep = constp = 0;
1.1.1.2 root 3906: size_varies = 0;
1.1 root 3907: }
3908: else if (TREE_CODE (type) == FUNCTION_TYPE)
3909: {
3910: if (pedantic && (constp || volatilep))
3911: pedwarn ("ANSI C forbids const or volatile function types");
3912: type = build_pointer_type (c_build_type_variant (type, constp, volatilep));
3913: volatilep = constp = 0;
3914: }
3915:
3916: if (initialized)
3917: error ("parameter `%s' is initialized", name);
3918:
3919: decl = build_decl (PARM_DECL, declarator, type);
1.1.1.2 root 3920: if (size_varies)
1.1 root 3921: C_DECL_VARIABLE_SIZE (decl) = 1;
3922:
3923: /* Compute the type actually passed in the parmlist,
3924: for the case where there is no prototype.
3925: (For example, shorts and chars are passed as ints.)
3926: When there is a prototype, this is overridden later. */
3927:
3928: DECL_ARG_TYPE (decl) = type;
3929: if (type == float_type_node)
3930: DECL_ARG_TYPE (decl) = double_type_node;
1.1.1.3 ! root 3931: /* Don't use TYPE_PREISION to decide whether to promote,
! 3932: because we should convert short if it's the same size as int,
! 3933: but we should not convert long if it's the same size as int. */
! 3934: else if (type == char_type_node || type == signed_char_type_node
! 3935: || type == unsigned_char_type_node
! 3936: || type == short_integer_type_node
! 3937: || type == short_unsigned_type_node)
! 3938: {
! 3939: if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)
! 3940: && TREE_UNSIGNED (type))
! 3941: DECL_ARG_TYPE (decl) = unsigned_type_node;
! 3942: else
! 3943: DECL_ARG_TYPE (decl) = integer_type_node;
! 3944: }
1.1 root 3945:
3946: DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
3947: }
3948: else if (decl_context == FIELD)
3949: {
3950: /* Structure field. It may not be a function. */
3951:
3952: if (TREE_CODE (type) == FUNCTION_TYPE)
3953: {
3954: error ("field `%s' declared as a function",
3955: IDENTIFIER_POINTER (declarator));
3956: type = build_pointer_type (type);
3957: }
3958: else if (TREE_CODE (type) != ERROR_MARK && TYPE_SIZE (type) == 0)
3959: {
3960: error ("field `%s' has incomplete type",
3961: IDENTIFIER_POINTER (declarator));
3962: type = error_mark_node;
3963: }
3964: /* Move type qualifiers down to element of an array. */
3965: if (TREE_CODE (type) == ARRAY_TYPE && (constp || volatilep))
3966: {
3967: type = build_array_type (c_build_type_variant (TREE_TYPE (type),
3968: constp, volatilep),
3969: TYPE_DOMAIN (type));
3970: #if 0 /* Leave the field const or volatile as well. */
3971: constp = volatilep = 0;
3972: #endif
3973: }
3974: decl = build_decl (FIELD_DECL, declarator, type);
1.1.1.2 root 3975: if (size_varies)
1.1 root 3976: C_DECL_VARIABLE_SIZE (decl) = 1;
3977: }
3978: else if (TREE_CODE (type) == FUNCTION_TYPE)
3979: {
3980: if (specbits & (1 << (int) RID_AUTO)
3981: && (pedantic || current_binding_level == global_binding_level))
3982: pedwarn ("invalid storage class for function `%s'",
3983: IDENTIFIER_POINTER (declarator));
3984: if (specbits & (1 << (int) RID_REGISTER))
3985: error ("invalid storage class for function `%s'",
3986: IDENTIFIER_POINTER (declarator));
3987: /* Function declaration not at top level.
3988: Storage classes other than `extern' are not allowed
3989: and `extern' makes no difference. */
3990: if (current_binding_level != global_binding_level
3991: && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
3992: && pedantic)
3993: pedwarn ("invalid storage class for function `%s'",
3994: IDENTIFIER_POINTER (declarator));
3995: decl = build_decl (FUNCTION_DECL, declarator, type);
3996:
3997: if (pedantic && (constp || volatilep))
3998: pedwarn ("ANSI C forbids const or volatile functions");
3999:
4000: /* Every function declaration is "external"
4001: except for those which are inside a function body
4002: in which `auto' is used.
4003: That is a case not specified by ANSI C,
4004: and we use it for forward declarations for nested functions. */
4005: if (!(specbits & (1 << (int) RID_AUTO))
4006: || current_binding_level == global_binding_level)
4007: TREE_EXTERNAL (decl) = 1;
4008: /* Record absence of global scope for `static' or `auto'. */
4009: TREE_PUBLIC (decl)
4010: = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4011: /* Record presence of `inline', if it is reasonable. */
4012: if (inlinep)
4013: {
4014: tree last = tree_last (TYPE_ARG_TYPES (type));
4015:
4016: if (! strcmp (IDENTIFIER_POINTER (declarator), "main"))
4017: warning ("cannot inline function `main'");
4018: else if (last && TREE_VALUE (last) != void_type_node)
4019: warning ("inline declaration ignored for function with `...'");
4020: else
4021: /* Assume that otherwise the function can be inlined. */
4022: TREE_INLINE (decl) = 1;
4023:
4024: if (specbits & (1 << (int) RID_EXTERN))
4025: current_extern_inline = 1;
4026: }
4027: }
4028: else
4029: {
4030: /* It's a variable. */
4031:
4032: /* Move type qualifiers down to element of an array. */
4033: if (TREE_CODE (type) == ARRAY_TYPE && (constp || volatilep))
4034: {
4035: type = build_array_type (c_build_type_variant (TREE_TYPE (type),
4036: constp, volatilep),
4037: TYPE_DOMAIN (type));
4038: #if 0 /* Leave the variable const or volatile as well. */
4039: constp = volatilep = 0;
4040: #endif
4041: }
4042:
4043: decl = build_decl (VAR_DECL, declarator, type);
1.1.1.2 root 4044: if (size_varies)
1.1 root 4045: C_DECL_VARIABLE_SIZE (decl) = 1;
4046:
4047: if (inlinep)
4048: pedwarn_with_decl (decl, "variable `%s' declared `inline'");
4049:
4050: /* An uninitialized decl with `extern' is a reference. */
4051: TREE_EXTERNAL (decl)
4052: = !initialized && (specbits & (1 << (int) RID_EXTERN));
4053: /* At top level, either `static' or no s.c. makes a definition
4054: (perhaps tentative), and absence of `static' makes it public. */
4055: if (current_binding_level == global_binding_level)
4056: {
4057: TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC));
4058: TREE_STATIC (decl) = ! TREE_EXTERNAL (decl);
4059: }
4060: /* Not at top level, only `static' makes a static definition. */
4061: else
4062: {
4063: TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4064: TREE_PUBLIC (decl) = TREE_EXTERNAL (decl);
4065: }
4066: }
4067:
4068: /* Record `register' declaration for warnings on &
4069: and in case doing stupid register allocation. */
4070:
4071: if (specbits & (1 << (int) RID_REGISTER))
4072: TREE_REGDECL (decl) = 1;
4073:
4074: /* Record constancy and volatility. */
4075:
4076: if (constp)
4077: TREE_READONLY (decl) = 1;
4078: if (volatilep)
4079: {
4080: TREE_SIDE_EFFECTS (decl) = 1;
4081: TREE_THIS_VOLATILE (decl) = 1;
4082: }
4083: /* If a type has volatile components, it should be stored in memory.
4084: Otherwise, the fact that those components are volatile
4085: will be ignored, and would even crash the compiler. */
4086: if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4087: mark_addressable (decl);
4088:
4089: pop_obstacks ();
4090:
4091: return decl;
4092: }
4093: }
4094:
4095: /* Make a variant type in the proper way for C, propagating qualifiers
4096: down to the element type of an array. */
4097:
4098: tree
4099: c_build_type_variant (type, constp, volatilep)
4100: tree type;
4101: int constp, volatilep;
4102: {
4103: if (TREE_CODE (type) == ARRAY_TYPE)
4104: type = build_array_type (c_build_type_variant (TREE_TYPE (type),
4105: constp, volatilep),
4106: TYPE_DOMAIN (type));
4107: return build_type_variant (type, constp, volatilep);
4108: }
4109:
4110: /* Decode the parameter-list info for a function type or function definition.
4111: The argument is the value returned by `get_parm_info' (or made in parse.y
4112: if there is an identifier list instead of a parameter decl list).
4113: These two functions are separate because when a function returns
4114: or receives functions then each is called multiple times but the order
4115: of calls is different. The last call to `grokparms' is always the one
4116: that contains the formal parameter names of a function definition.
4117:
4118: Store in `last_function_parms' a chain of the decls of parms.
4119: Also store in `last_function_parm_tags' a chain of the struct, union,
4120: and enum tags declared among the parms.
4121:
4122: Return a list of arg types to use in the FUNCTION_TYPE for this function.
4123:
4124: FUNCDEF_FLAG is nonzero for a function definition, 0 for
4125: a mere declaration. A nonempty identifier-list gets an error message
4126: when FUNCDEF_FLAG is zero. */
4127:
4128: static tree
4129: grokparms (parms_info, funcdef_flag)
4130: tree parms_info;
4131: int funcdef_flag;
4132: {
4133: tree first_parm = TREE_CHAIN (parms_info);
4134:
4135: last_function_parms = TREE_PURPOSE (parms_info);
4136: last_function_parm_tags = TREE_VALUE (parms_info);
4137:
4138: if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag)
4139: warning ("function declaration isn't a prototype");
4140:
4141: if (first_parm != 0
4142: && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4143: {
4144: if (! funcdef_flag)
4145: pedwarn ("parameter names (without types) in function declaration");
4146:
4147: last_function_parms = first_parm;
4148: return 0;
4149: }
4150: else
4151: {
4152: tree parm;
4153: tree typelt;
4154: /* We no longer test FUNCDEF_FLAG.
4155: If the arg types are incomplete in a declaration,
4156: they must include undefined tags.
4157: These tags can never be defined in the scope of the declaration,
4158: so the types can never be completed,
4159: and no call can be compiled successfully. */
4160: #if 0
4161: /* In a fcn definition, arg types must be complete. */
4162: if (funcdef_flag)
4163: #endif
4164: for (parm = last_function_parms, typelt = first_parm;
4165: parm;
4166: parm = TREE_CHAIN (parm))
4167: /* Skip over any enumeration constants declared here. */
4168: if (TREE_CODE (parm) == PARM_DECL)
4169: {
4170: /* Barf if the parameter itself has an incomplete type. */
4171: tree type = TREE_VALUE (typelt);
4172: if (TYPE_SIZE (type) == 0)
4173: {
4174: if (funcdef_flag && DECL_NAME (parm) != 0)
4175: error ("parameter `%s' has incomplete type",
4176: IDENTIFIER_POINTER (DECL_NAME (parm)));
4177: else
4178: warning ("parameter has incomplete type");
4179: if (funcdef_flag)
4180: {
4181: TREE_VALUE (typelt) = error_mark_node;
4182: TREE_TYPE (parm) = error_mark_node;
4183: }
4184: }
4185: #if 0 /* This has been replaced by parm_tags_warning
4186: which uses a more accurate criterion for what to warn about. */
4187: else
4188: {
4189: /* Now warn if is a pointer to an incomplete type. */
4190: while (TREE_CODE (type) == POINTER_TYPE
4191: || TREE_CODE (type) == REFERENCE_TYPE)
4192: type = TREE_TYPE (type);
4193: type = TYPE_MAIN_VARIANT (type);
4194: if (TYPE_SIZE (type) == 0)
4195: {
4196: if (DECL_NAME (parm) != 0)
4197: warning ("parameter `%s' points to incomplete type",
4198: IDENTIFIER_POINTER (DECL_NAME (parm)));
4199: else
4200: warning ("parameter points to incomplete type");
4201: }
4202: }
4203: #endif
4204: typelt = TREE_CHAIN (typelt);
4205: }
4206:
4207: return first_parm;
4208: }
4209: }
4210:
4211:
4212: /* Return a tree_list node with info on a parameter list just parsed.
4213: The TREE_PURPOSE is a chain of decls of those parms.
4214: The TREE_VALUE is a list of structure, union and enum tags defined.
4215: The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4216: This tree_list node is later fed to `grokparms'.
4217:
4218: VOID_AT_END nonzero means append `void' to the end of the type-list.
4219: Zero means the parmlist ended with an ellipsis so don't append `void'. */
4220:
4221: tree
4222: get_parm_info (void_at_end)
4223: int void_at_end;
4224: {
4225: register tree decl, t;
4226: register tree types = 0;
4227: int erred = 0;
4228: tree tags = gettags ();
4229: tree parms = getdecls ();
4230: tree new_parms = 0;
4231: tree order = current_binding_level->parm_order;
4232:
4233: /* Just `void' (and no ellipsis) is special. There are really no parms. */
4234: if (void_at_end && parms != 0
4235: && TREE_CHAIN (parms) == 0
4236: && TREE_TYPE (parms) == void_type_node
4237: && DECL_NAME (parms) == 0)
4238: {
4239: parms = NULL_TREE;
4240: storedecls (NULL_TREE);
4241: return saveable_tree_cons (NULL_TREE, NULL_TREE,
4242: saveable_tree_cons (NULL_TREE, void_type_node, NULL_TREE));
4243: }
4244:
1.1.1.3 ! root 4245: /* Extract enumerator values and other non-parms declared with the parms.
! 4246: Likewise any forward parm decls that didn't have real parm decls. */
1.1 root 4247: for (decl = parms; decl; )
4248: {
4249: tree next = TREE_CHAIN (decl);
4250:
4251: if (TREE_CODE (decl) != PARM_DECL)
4252: {
4253: TREE_CHAIN (decl) = new_parms;
4254: new_parms = decl;
4255: }
1.1.1.3 ! root 4256: else if (TREE_ASM_WRITTEN (decl))
! 4257: {
! 4258: error_with_decl (decl, "parameter `%s' has just a forward declaration");
! 4259: TREE_CHAIN (decl) = new_parms;
! 4260: new_parms = decl;
! 4261: }
1.1 root 4262: decl = next;
4263: }
4264:
4265: /* Put the parm decls back in the order they were in in the parm list. */
4266: for (t = order; t; t = TREE_CHAIN (t))
4267: {
4268: if (TREE_CHAIN (t))
4269: TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (TREE_CHAIN (t));
4270: else
4271: TREE_CHAIN (TREE_VALUE (t)) = 0;
4272: }
4273:
4274: new_parms = chainon (order ? nreverse (TREE_VALUE (order)) : 0,
4275: new_parms);
4276:
4277: /* Store the parmlist in the binding level since the old one
4278: is no longer a valid list. (We have changed the chain pointers.) */
4279: storedecls (new_parms);
4280:
4281: for (decl = new_parms; decl; decl = TREE_CHAIN (decl))
4282: /* There may also be declarations for enumerators if an enumeration
4283: type is declared among the parms. Ignore them here. */
4284: if (TREE_CODE (decl) == PARM_DECL)
4285: {
4286: /* Since there is a prototype,
4287: args are passed in their declared types. */
4288: tree type = TREE_TYPE (decl);
4289: DECL_ARG_TYPE (decl) = type;
4290: #ifdef PROMOTE_PROTOTYPES
4291: if (TREE_CODE (type) == INTEGER_TYPE
4292: && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4293: DECL_ARG_TYPE (decl) = integer_type_node;
4294: #endif
4295:
4296: types = saveable_tree_cons (NULL_TREE, TREE_TYPE (decl), types);
4297: if (TREE_VALUE (types) == void_type_node && ! erred
4298: && DECL_NAME (decl) == 0)
4299: {
4300: error ("`void' in parameter list must be the entire list");
4301: erred = 1;
4302: }
4303: }
4304:
4305: if (void_at_end)
4306: return saveable_tree_cons (new_parms, tags,
4307: nreverse (saveable_tree_cons (NULL_TREE, void_type_node, types)));
4308:
4309: return saveable_tree_cons (new_parms, tags, nreverse (types));
4310: }
4311:
4312: /* At end of parameter list, warn about any struct, union or enum tags
4313: defined within. Do so because these types cannot ever become complete. */
4314:
4315: void
4316: parmlist_tags_warning ()
4317: {
4318: tree elt;
4319: static int already;
4320:
4321: for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt))
4322: {
4323: enum tree_code code = TREE_CODE (TREE_VALUE (elt));
1.1.1.3 ! root 4324: /* An anonymous union parm type is meaningful as a GNU extension.
! 4325: So don't warn for that. */
! 4326: if (code == UNION_TYPE && !pedantic)
! 4327: continue;
1.1.1.2 root 4328: if (TREE_PURPOSE (elt) != 0)
4329: warning ("`%s %s' declared inside parameter list",
4330: (code == RECORD_TYPE ? "struct"
4331: : code == UNION_TYPE ? "union"
4332: : "enum"),
4333: IDENTIFIER_POINTER (TREE_PURPOSE (elt)));
4334: else
4335: warning ("anonymous %s declared inside parameter list",
4336: (code == RECORD_TYPE ? "struct"
4337: : code == UNION_TYPE ? "union"
4338: : "enum"));
4339:
1.1 root 4340: if (! already)
4341: {
4342: warning ("its scope is only this definition or declaration,");
4343: warning ("which is probably not what you want.");
4344: already = 1;
4345: }
4346: }
4347: }
4348:
4349: /* Get the struct, enum or union (CODE says which) with tag NAME.
4350: Define the tag as a forward-reference if it is not defined. */
4351:
4352: tree
4353: xref_tag (code, name)
4354: enum tree_code code;
4355: tree name;
4356: {
4357: int temporary = allocation_temporary_p ();
4358:
4359: /* If a cross reference is requested, look up the type
4360: already defined for this tag and return it. */
4361:
4362: register tree ref = lookup_tag (code, name, current_binding_level, 0);
4363: /* Even if this is the wrong type of tag, return what we found.
4364: There will be an error message anyway, from pending_xref_error.
4365: If we create an empty xref just for an invalid use of the type,
1.1.1.2 root 4366: the main result is to create lots of superfluous error messages. */
1.1 root 4367: if (ref)
4368: return ref;
4369:
4370: push_obstacks_nochange ();
4371:
4372: if (current_binding_level == global_binding_level && temporary)
4373: end_temporary_allocation ();
4374:
4375: /* If no such tag is yet defined, create a forward-reference node
4376: and record it as the "definition".
4377: When a real declaration of this type is found,
4378: the forward-reference will be altered into a real type. */
4379:
4380: ref = make_node (code);
4381: if (code == ENUMERAL_TYPE)
4382: {
4383: /* (In ANSI, Enums can be referred to only if already defined.) */
4384: if (pedantic)
4385: pedwarn ("ANSI C forbids forward references to `enum' types");
4386: /* Give the type a default layout like unsigned int
4387: to avoid crashing if it does not get defined. */
4388: TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4389: TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4390: TREE_UNSIGNED (ref) = 1;
4391: TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4392: TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4393: TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4394: }
4395:
4396: pushtag (name, ref);
4397:
4398: pop_obstacks ();
4399:
4400: return ref;
4401: }
4402:
4403: /* Make sure that the tag NAME is defined *in the current binding level*
4404: at least as a forward reference.
4405: CODE says which kind of tag NAME ought to be. */
4406:
4407: tree
4408: start_struct (code, name)
4409: enum tree_code code;
4410: tree name;
4411: {
4412: /* If there is already a tag defined at this binding level
4413: (as a forward reference), just return it. */
4414:
4415: register tree ref = 0;
4416:
4417: if (name != 0)
4418: ref = lookup_tag (code, name, current_binding_level, 1);
4419: if (ref && TREE_CODE (ref) == code)
4420: {
4421: C_TYPE_BEING_DEFINED (ref) = 1;
4422: if (TYPE_FIELDS (ref))
4423: error ((code == UNION_TYPE ? "redefinition of `union %s'"
4424: : "redefinition of `struct %s'"),
4425: IDENTIFIER_POINTER (name));
4426:
4427: return ref;
4428: }
4429:
4430: /* Otherwise create a forward-reference just so the tag is in scope. */
4431:
4432: ref = make_node (code);
4433: pushtag (name, ref);
4434: C_TYPE_BEING_DEFINED (ref) = 1;
4435: return ref;
4436: }
4437:
4438: /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4439: of a structure component, returning a FIELD_DECL node.
4440: WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4441:
4442: This is done during the parsing of the struct declaration.
4443: The FIELD_DECL nodes are chained together and the lot of them
4444: are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4445:
4446: tree
4447: grokfield (filename, line, declarator, declspecs, width)
4448: char *filename;
4449: int line;
4450: tree declarator, declspecs, width;
4451: {
4452: tree value;
4453:
4454: /* The corresponding pop_obstacks is in finish_decl. */
4455: push_obstacks_nochange ();
4456:
4457: value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4458:
4459: finish_decl (value, NULL, NULL);
4460: DECL_INITIAL (value) = width;
4461:
4462: return value;
4463: }
4464:
4465: /* Function to help qsort sort FIELD_DECLs by name order. */
4466:
4467: static int
4468: field_decl_cmp (x, y)
4469: tree *x, *y;
4470: {
4471: return (long)DECL_NAME (*x) - (long)DECL_NAME (*y);
4472: }
4473:
4474: /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4475: FIELDLIST is a chain of FIELD_DECL nodes for the fields. */
4476:
4477: tree
4478: finish_struct (t, fieldlist)
4479: register tree t, fieldlist;
4480: {
4481: register tree x;
4482: int old_momentary;
4483: int toplevel = global_binding_level == current_binding_level;
4484:
4485: /* If this type was previously laid out as a forward reference,
4486: make sure we lay it out again. */
4487:
4488: TYPE_SIZE (t) = 0;
4489:
4490: /* Nameless union parm types are useful as GCC extension. */
4491: if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4492: /* Otherwise, warn about any struct or union def. in parmlist. */
4493: if (in_parm_level_p ())
4494: {
4495: if (pedantic)
4496: pedwarn ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms"
4497: : "structure defined inside parms"));
4498: else
4499: warning ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms"
4500: : "structure defined inside parms"));
4501: }
4502:
4503: old_momentary = suspend_momentary ();
4504:
4505: if (fieldlist == 0 && pedantic)
4506: pedwarn ((TREE_CODE (t) == UNION_TYPE ? "union has no members"
4507: : "structure has no members"));
4508:
4509: /* Install struct as DECL_CONTEXT of each field decl.
4510: Also process specified field sizes.
1.1.1.3 ! root 4511: Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
1.1 root 4512: The specified size is found in the DECL_INITIAL.
4513: Store 0 there, except for ": 0" fields (so we can find them
4514: and delete them, below). */
4515:
4516: for (x = fieldlist; x; x = TREE_CHAIN (x))
4517: {
4518: DECL_CONTEXT (x) = t;
1.1.1.3 ! root 4519: DECL_FIELD_SIZE (x) = 0;
1.1 root 4520:
4521: /* If any field is const, the structure type is pseudo-const. */
4522: if (TREE_READONLY (x))
4523: C_TYPE_FIELDS_READONLY (t) = 1;
4524: else
4525: {
4526: /* A field that is pseudo-const makes the structure likewise. */
4527: tree t1 = TREE_TYPE (x);
4528: while (TREE_CODE (t1) == ARRAY_TYPE)
4529: t1 = TREE_TYPE (t1);
4530: if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4531: && C_TYPE_FIELDS_READONLY (t1))
4532: C_TYPE_FIELDS_READONLY (t) = 1;
4533: }
4534:
4535: /* Any field that is volatile means variables of this type must be
4536: treated in some ways as volatile. */
4537: if (TREE_THIS_VOLATILE (x))
4538: C_TYPE_FIELDS_VOLATILE (t) = 1;
4539:
4540: /* Any field of nominal variable size implies structure is too. */
4541: if (C_DECL_VARIABLE_SIZE (x))
4542: C_TYPE_VARIABLE_SIZE (t) = 1;
4543:
4544: /* Detect invalid bit-field size. */
4545: while (DECL_INITIAL (x)
4546: && TREE_CODE (DECL_INITIAL (x)) == NON_LVALUE_EXPR)
4547: DECL_INITIAL (x) = TREE_OPERAND (DECL_INITIAL (x), 0);
4548: if (DECL_INITIAL (x) && TREE_CODE (DECL_INITIAL (x)) != INTEGER_CST)
4549: {
4550: error_with_decl (x, "bit-field `%s' width not an integer constant");
4551: DECL_INITIAL (x) = NULL;
4552: }
4553:
4554: /* Detect invalid bit-field type. */
4555: if (DECL_INITIAL (x)
4556: && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4557: && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4558: {
4559: error_with_decl (x, "bit-field `%s' has invalid type");
4560: DECL_INITIAL (x) = NULL;
4561: }
4562: if (DECL_INITIAL (x) && pedantic
4563: && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4564: && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node)
4565: pedwarn_with_decl (x, "bit-field `%s' type invalid in ANSI C");
4566:
4567: /* Detect and ignore out of range field width. */
4568: if (DECL_INITIAL (x))
4569: {
4570: register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
4571:
4572: if (width < 0)
4573: {
4574: DECL_INITIAL (x) = NULL;
4575: error_with_decl (x, "negative width in bit-field `%s'");
4576: }
4577: else if (width == 0 && DECL_NAME (x) != 0)
4578: {
4579: error_with_decl (x, "zero width for bit-field `%s'");
4580: DECL_INITIAL (x) = NULL;
4581: }
4582: else if (width > TYPE_PRECISION (TREE_TYPE (x)))
4583: {
4584: DECL_INITIAL (x) = NULL;
4585: pedwarn_with_decl (x, "width of `%s' exceeds its type");
4586: }
4587: }
4588:
4589: /* Process valid field width. */
4590: if (DECL_INITIAL (x))
4591: {
4592: register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
4593:
1.1.1.3 ! root 4594: DECL_FIELD_SIZE (x) = width;
1.1 root 4595: DECL_BIT_FIELD (x) = 1;
4596: DECL_INITIAL (x) = NULL;
4597:
4598: if (width == 0)
4599: {
4600: /* field size 0 => force desired amount of alignment. */
4601: #ifdef EMPTY_FIELD_BOUNDARY
4602: DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
4603: #endif
4604: #ifdef PCC_BITFIELD_TYPE_MATTERS
4605: DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
4606: TYPE_ALIGN (TREE_TYPE (x)));
4607: #endif
4608: }
4609: }
4610: else
1.1.1.3 ! root 4611: {
! 4612: int min_align = (DECL_PACKED (x) ? BITS_PER_UNIT
! 4613: : TYPE_ALIGN (TREE_TYPE (x)));
! 4614: /* Non-bit-fields are aligned for their type, except packed
! 4615: fields which require only BITS_PER_UNIT alignment. */
! 4616: DECL_ALIGN (x) = MAX (DECL_ALIGN (x), min_align);
! 4617: }
1.1 root 4618: }
4619:
4620: /* Now DECL_INITIAL is null on all members. */
4621:
4622: /* Delete all duplicate fields from the fieldlist */
4623: for (x = fieldlist; x && TREE_CHAIN (x);)
4624: /* Anonymous fields aren't duplicates. */
4625: if (DECL_NAME (TREE_CHAIN (x)) == 0)
4626: x = TREE_CHAIN (x);
4627: else
4628: {
4629: register tree y = fieldlist;
4630:
4631: while (1)
4632: {
4633: if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
4634: break;
4635: if (y == x)
4636: break;
4637: y = TREE_CHAIN (y);
4638: }
4639: if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
4640: {
4641: error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
4642: TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
4643: }
4644: else x = TREE_CHAIN (x);
4645: }
4646:
4647: /* Now we have the nearly final fieldlist. Record it,
4648: then lay out the structure or union (including the fields). */
4649:
4650: TYPE_FIELDS (t) = fieldlist;
4651:
4652: layout_type (t);
4653:
4654: /* Delete all zero-width bit-fields from the front of the fieldlist */
4655: while (fieldlist
4656: && DECL_INITIAL (fieldlist))
4657: fieldlist = TREE_CHAIN (fieldlist);
4658: /* Delete all such members from the rest of the fieldlist */
4659: for (x = fieldlist; x;)
4660: {
4661: if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x)))
4662: TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
4663: else x = TREE_CHAIN (x);
4664: }
4665:
4666: /* Now we have the truly final field list.
4667: Store it in this type and in the variants. */
4668:
4669: TYPE_FIELDS (t) = fieldlist;
4670:
4671: /* If there are lots of fields, sort so we can look through them fast.
4672: We arbitrarily consider 16 or more elts to be "a lot". */
4673: {
4674: int len = 0;
4675:
4676: for (x = fieldlist; x; x = TREE_CHAIN (x))
4677: {
4678: if (len > 15)
4679: break;
4680: len += 1;
4681: }
4682: if (len > 15)
4683: {
4684: tree *field_array;
4685: char *space;
4686:
4687: len += list_length (x);
4688: /* Use the same allocation policy here that make_node uses, to
4689: ensure that this lives as long as the rest of the struct decl.
4690: All decls in an inline function need to be saved. */
4691: if (allocation_temporary_p ())
4692: space = savealloc (sizeof (struct lang_type) + len * sizeof (tree));
4693: else
4694: space = oballoc (sizeof (struct lang_type) + len * sizeof (tree));
4695:
4696: TYPE_LANG_SPECIFIC (t) = (struct lang_type *) space;
4697: TYPE_LANG_SPECIFIC (t)->len = len;
4698:
4699: field_array = &TYPE_LANG_SPECIFIC (t)->elts[0];
4700: len = 0;
4701: for (x = fieldlist; x; x = TREE_CHAIN (x))
4702: field_array[len++] = x;
4703:
4704: qsort (field_array, len, sizeof (tree), field_decl_cmp);
4705: }
4706: }
4707:
4708: for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
4709: {
4710: TYPE_FIELDS (x) = TYPE_FIELDS (t);
4711: TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
4712: TYPE_ALIGN (x) = TYPE_ALIGN (t);
4713: }
4714:
4715: /* Promote each bit-field's type to int if it is narrower than that. */
4716: for (x = fieldlist; x; x = TREE_CHAIN (x))
4717: if (DECL_BIT_FIELD (x)
4718: && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
1.1.1.3 ! root 4719: && (TYPE_PRECISION (TREE_TYPE (x))
1.1 root 4720: < TYPE_PRECISION (integer_type_node)))
4721: TREE_TYPE (x) = integer_type_node;
4722:
4723: /* If this structure or union completes the type of any previous
4724: variable declaration, lay it out and output its rtl. */
4725:
4726: if (current_binding_level->n_incomplete != 0)
4727: {
4728: tree decl;
4729: for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
4730: {
4731: if (TREE_TYPE (decl) == t
4732: && TREE_CODE (decl) != TYPE_DECL)
4733: {
4734: layout_decl (decl, 0);
4735: /* This is a no-op in c-lang.c or something real in objc-actions.c. */
4736: maybe_objc_check_decl (decl);
4737: rest_of_decl_compilation (decl, 0, toplevel, 0);
4738: if (! toplevel)
4739: expand_decl (decl);
4740: --current_binding_level->n_incomplete;
4741: }
4742: else if (TYPE_SIZE (TREE_TYPE (decl)) == 0
4743: && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
4744: {
4745: tree element = TREE_TYPE (decl);
4746: while (TREE_CODE (element) == ARRAY_TYPE)
4747: element = TREE_TYPE (element);
4748: if (element == t)
4749: layout_array_type (TREE_TYPE (decl));
4750: }
4751: }
4752: }
4753:
4754: resume_momentary (old_momentary);
4755:
4756: /* Finish debugging output for this type. */
4757: rest_of_type_compilation (t, toplevel);
4758:
4759: return t;
4760: }
4761:
4762: /* Lay out the type T, and its element type, and so on. */
4763:
4764: static void
4765: layout_array_type (t)
4766: tree t;
4767: {
4768: if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4769: layout_array_type (TREE_TYPE (t));
4770: layout_type (t);
4771: }
4772:
4773: /* Begin compiling the definition of an enumeration type.
4774: NAME is its name (or null if anonymous).
4775: Returns the type object, as yet incomplete.
4776: Also records info about it so that build_enumerator
4777: may be used to declare the individual values as they are read. */
4778:
4779: tree
4780: start_enum (name)
4781: tree name;
4782: {
4783: register tree enumtype = 0;
4784:
4785: /* If this is the real definition for a previous forward reference,
4786: fill in the contents in the same object that used to be the
4787: forward reference. */
4788:
4789: if (name != 0)
4790: enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
4791:
4792: if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
4793: {
4794: enumtype = make_node (ENUMERAL_TYPE);
4795: pushtag (name, enumtype);
4796: }
4797:
4798: C_TYPE_BEING_DEFINED (enumtype) = 1;
4799:
4800: if (TYPE_VALUES (enumtype) != 0)
4801: {
4802: /* This enum is a named one that has been declared already. */
4803: error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
4804:
4805: /* Completely replace its old definition.
4806: The old enumerators remain defined, however. */
4807: TYPE_VALUES (enumtype) = 0;
4808: }
4809:
4810: enum_next_value = integer_zero_node;
1.1.1.3 ! root 4811: enum_overflow = 0;
1.1 root 4812:
4813: return enumtype;
4814: }
4815:
4816: /* After processing and defining all the values of an enumeration type,
4817: install their decls in the enumeration type and finish it off.
4818: ENUMTYPE is the type object and VALUES a list of decl-value pairs.
4819: Returns ENUMTYPE. */
4820:
4821: tree
4822: finish_enum (enumtype, values)
4823: register tree enumtype, values;
4824: {
4825: register tree pair;
4826: tree minnode = 0, maxnode = 0;
4827: register long maxvalue = 0;
4828: register long minvalue = 0;
4829: register int i;
4830: unsigned precision = 0;
4831: int toplevel = global_binding_level == current_binding_level;
4832:
4833: if (in_parm_level_p ())
4834: warning ("enum defined inside parms");
4835:
4836: /* Calculate the maximum value of any enumerator in this type. */
4837:
4838: for (pair = values; pair; pair = TREE_CHAIN (pair))
4839: {
4840: tree value = TREE_VALUE (pair);
4841: if (pair == values)
4842: minnode = maxnode = TREE_VALUE (pair);
4843: else
4844: {
4845: if (tree_int_cst_lt (maxnode, value))
4846: maxnode = value;
4847: if (tree_int_cst_lt (value, minnode))
4848: minnode = value;
4849: }
4850: }
4851:
4852: TYPE_MIN_VALUE (enumtype) = minnode;
4853: TYPE_MAX_VALUE (enumtype) = maxnode;
4854:
4855: /* Determine the precision this type needs. */
4856:
4857: if (TREE_INT_CST_HIGH (minnode) >= 0
4858: ? tree_int_cst_lt (TYPE_MAX_VALUE (unsigned_type_node), maxnode)
4859: : (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
4860: || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode)))
4861: precision = TYPE_PRECISION (long_long_integer_type_node);
4862: else
4863: {
4864: int maxvalue = TREE_INT_CST_LOW (maxnode);
4865: int minvalue = TREE_INT_CST_LOW (minnode);
4866:
4867: if (maxvalue > 0)
4868: precision = floor_log2 (maxvalue) + 1;
4869: if (minvalue < 0)
4870: {
4871: /* Compute number of bits to represent magnitude of a negative value.
4872: Add one to MINVALUE since range of negative numbers
4873: includes the power of two. */
4874: unsigned negprecision = floor_log2 (-minvalue - 1) + 1;
4875: if (negprecision > precision)
4876: precision = negprecision;
4877: precision += 1; /* room for sign bit */
4878: }
4879:
4880: if (!precision)
4881: precision = 1;
4882: }
4883:
4884: if (flag_short_enums || precision > TYPE_PRECISION (integer_type_node))
4885: /* Use the width of the narrowest normal C type which is wide enough. */
4886: TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size (precision, 1));
4887: else
4888: TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
4889:
4890: TYPE_SIZE (enumtype) = 0;
4891: layout_type (enumtype);
4892:
4893: /* An enum can have some negative values; then it is signed. */
4894: TREE_UNSIGNED (enumtype) = ! tree_int_cst_lt (minnode, integer_zero_node);
4895:
4896: /* If the enumerators might not fit in an int, change their type now. */
4897: /* It seems more useful in the debugger to leave these as int
4898: unless the enumerator is wider than int. */
4899: if (TYPE_PRECISION (enumtype) <= TYPE_PRECISION (integer_type_node))
4900: for (pair = values; pair; pair = TREE_CHAIN (pair))
4901: {
4902: TREE_TYPE (TREE_PURPOSE (pair)) = enumtype;
4903: DECL_SIZE (TREE_PURPOSE (pair)) = TYPE_SIZE (enumtype);
1.1.1.3 ! root 4904: if (TREE_CODE (TREE_PURPOSE (pair)) != FUNCTION_DECL)
! 4905: DECL_ALIGN (TREE_PURPOSE (pair)) = TYPE_ALIGN (enumtype);
1.1 root 4906: }
4907:
4908: /* Replace the decl nodes in VALUES with their names. */
4909: for (pair = values; pair; pair = TREE_CHAIN (pair))
4910: TREE_PURPOSE (pair) = DECL_NAME (TREE_PURPOSE (pair));
4911:
4912: TYPE_VALUES (enumtype) = values;
4913:
4914: /* Finish debugging output for this type. */
4915: rest_of_type_compilation (enumtype, toplevel);
4916:
4917: return enumtype;
4918: }
4919:
4920: /* Build and install a CONST_DECL for one value of the
4921: current enumeration type (one that was begun with start_enum).
4922: Return a tree-list containing the CONST_DECL and its value.
4923: Assignment of sequential values by default is handled here. */
4924:
4925: tree
4926: build_enumerator (name, value)
4927: tree name, value;
4928: {
4929: register tree decl;
4930:
4931: /* Validate and default VALUE. */
4932:
4933: /* Remove no-op casts from the value. */
4934: while (value != 0
4935: && (TREE_CODE (value) == NOP_EXPR
4936: || TREE_CODE (value) == NON_LVALUE_EXPR))
4937: value = TREE_OPERAND (value, 0);
4938:
4939: if (value != 0 && TREE_CODE (value) != INTEGER_CST)
4940: {
4941: error ("enumerator value for `%s' not integer constant",
4942: IDENTIFIER_POINTER (name));
4943: value = 0;
4944: }
4945:
4946: /* Default based on previous value. */
4947: /* It should no longer be possible to have NON_LVALUE_EXPR
4948: in the default. */
4949: if (value == 0)
1.1.1.3 ! root 4950: {
! 4951: value = enum_next_value;
! 4952: if (enum_overflow)
! 4953: error ("overflow in enumeration values");
! 4954: }
1.1 root 4955:
4956: if (pedantic && ! int_fits_type_p (value, integer_type_node))
4957: {
4958: pedwarn ("ANSI C restricts enumerator values to range of `int'");
4959: value = integer_zero_node;
4960: }
4961:
4962: /* Set basis for default for next value. */
4963: enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
1.1.1.3 ! root 4964: enum_overflow = tree_int_cst_lt (enum_next_value, value);
1.1 root 4965:
4966: /* Now create a declaration for the enum value name. */
4967:
4968: decl = build_decl (CONST_DECL, name, integer_type_node);
4969: DECL_INITIAL (decl) = value;
4970: TREE_TYPE (value) = integer_type_node;
4971: pushdecl (decl);
4972:
4973: return saveable_tree_cons (decl, value, NULL);
4974: }
4975:
4976: /* Create the FUNCTION_DECL for a function definition.
4977: DECLSPECS and DECLARATOR are the parts of the declaration;
4978: they describe the function's name and the type it returns,
4979: but twisted together in a fashion that parallels the syntax of C.
4980:
4981: This function creates a binding context for the function body
4982: as well as setting up the FUNCTION_DECL in current_function_decl.
4983:
4984: Returns 1 on success. If the DECLARATOR is not suitable for a function
4985: (it defines a datum instead), we return 0, which tells
4986: yyparse to report a parse error.
4987:
4988: NESTED is nonzero for a function nested within another function. */
4989:
4990: int
4991: start_function (declspecs, declarator, nested)
4992: tree declarator, declspecs;
4993: int nested;
4994: {
4995: tree decl1, old_decl;
4996: tree restype;
4997:
4998: current_function_returns_value = 0; /* Assume, until we see it does. */
4999: current_function_returns_null = 0;
5000: warn_about_return_type = 0;
5001: current_extern_inline = 0;
5002: c_function_varargs = 0;
5003: named_labels = 0;
5004: shadowed_labels = 0;
5005:
5006: decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5007:
5008: /* If the declarator is not suitable for a function definition,
5009: cause a syntax error. */
5010: if (decl1 == 0)
5011: return 0;
5012:
5013: announce_function (decl1);
5014:
5015: if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl1))) == 0)
5016: {
5017: error ("return-type is an incomplete type");
5018: /* Make it return void instead. */
5019: TREE_TYPE (decl1)
5020: = build_function_type (void_type_node,
5021: TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5022: }
5023:
5024: if (warn_about_return_type)
5025: warning ("return-type defaults to `int'");
5026:
5027: /* Save the parm names or decls from this function's declarator
5028: where store_parm_decls will find them. */
5029: current_function_parms = last_function_parms;
5030: current_function_parm_tags = last_function_parm_tags;
5031:
5032: /* Make the init_value nonzero so pushdecl knows this is not tentative.
5033: error_mark_node is replaced below (in poplevel) with the BLOCK. */
5034: DECL_INITIAL (decl1) = error_mark_node;
5035:
5036: /* If this definition isn't a prototype and we had a prototype declaration
5037: before, copy the arg type info from that prototype.
5038: But not if what we had before was a builtin function. */
5039: old_decl = lookup_name_current_level (DECL_NAME (decl1));
5040: if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5041: && !DECL_BUILT_IN (old_decl)
5042: && TREE_TYPE (TREE_TYPE (decl1)) == TREE_TYPE (TREE_TYPE (old_decl))
5043: && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5044: TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5045:
5046: /* Optionally warn of old-fashioned def with no previous prototype. */
5047: if (warn_strict_prototypes
5048: && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5049: && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0))
5050: warning ("function declaration isn't a prototype");
5051: /* Optionally warn of any global def with no previous prototype. */
5052: else if (warn_missing_prototypes
5053: && TREE_PUBLIC (decl1)
5054: && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0))
5055: warning_with_decl (decl1, "no previous prototype for `%s'");
5056: /* Optionally warn of any def with no previous prototype
5057: if the function has already been used. */
5058: else if (warn_missing_prototypes
5059: && old_decl != 0 && TREE_USED (old_decl)
5060: && !(old_decl != 0 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) != 0))
5061: warning_with_decl (decl1, "`%s' was used with no prototype before its definition");
5062:
5063: /* This is a definition, not a reference.
5064: So normally clear TREE_EXTERNAL.
5065: However, `extern inline' acts like a declaration
5066: except for defining how to inline. So set TREE_EXTERNAL in that case. */
5067: TREE_EXTERNAL (decl1) = current_extern_inline;
5068:
5069: /* This function exists in static storage.
5070: (This does not mean `static' in the C sense!) */
5071: TREE_STATIC (decl1) = 1;
5072:
5073: /* A nested function is not global. */
5074: if (current_function_decl != 0)
5075: TREE_PUBLIC (decl1) = 0;
5076:
5077: /* Record the decl so that the function name is defined.
5078: If we already have a decl for this name, and it is a FUNCTION_DECL,
5079: use the old decl. */
5080:
5081: current_function_decl = pushdecl (decl1);
5082:
5083: pushlevel (0);
5084: declare_parm_level (1);
5085: current_binding_level->subblocks_tag_transparent = 1;
5086:
5087: make_function_rtl (current_function_decl);
5088:
5089: restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5090: /* Promote the value to int before returning it. */
5091: if (TREE_CODE (restype) == INTEGER_TYPE
5092: && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
5093: restype = integer_type_node;
5094: DECL_RESULT (current_function_decl) = build_decl (RESULT_DECL, 0, restype);
5095:
5096: if (!nested)
5097: /* Allocate further tree nodes temporarily during compilation
5098: of this function only. */
5099: temporary_allocation ();
5100:
5101: /* If this fcn was already referenced via a block-scope `extern' decl
5102: (or an implicit decl), propagate certain information about the usage. */
5103: if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5104: TREE_ADDRESSABLE (current_function_decl) = 1;
5105:
5106: return 1;
5107: }
5108:
5109: /* Record that this function is going to be a varargs function.
5110: This is called before store_parm_decls, which is too early
5111: to call mark_varargs directly. */
5112:
5113: void
5114: c_mark_varargs ()
5115: {
5116: c_function_varargs = 1;
5117: }
5118:
5119: /* Store the parameter declarations into the current function declaration.
5120: This is called after parsing the parameter declarations, before
5121: digesting the body of the function.
5122:
5123: For an old-style definition, modify the function's type
5124: to specify at least the number of arguments. */
5125:
5126: void
5127: store_parm_decls ()
5128: {
5129: register tree fndecl = current_function_decl;
5130: register tree parm;
5131:
5132: /* This is either a chain of PARM_DECLs (if a prototype was used)
5133: or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
5134: tree specparms = current_function_parms;
5135:
5136: /* This is a list of types declared among parms in a prototype. */
5137: tree parmtags = current_function_parm_tags;
5138:
5139: /* This is a chain of PARM_DECLs from old-style parm declarations. */
5140: register tree parmdecls = getdecls ();
5141:
5142: /* This is a chain of any other decls that came in among the parm
5143: declarations. If a parm is declared with enum {foo, bar} x;
5144: then CONST_DECLs for foo and bar are put here. */
5145: tree nonparms = 0;
5146:
5147: /* Nonzero if this definition is written with a prototype. */
5148: int prototype = 0;
5149:
5150: if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST)
5151: {
5152: /* This case is when the function was defined with an ANSI prototype.
5153: The parms already have decls, so we need not do anything here
5154: except record them as in effect
5155: and complain if any redundant old-style parm decls were written. */
5156:
5157: register tree next;
5158: tree others = 0;
5159:
5160: prototype = 1;
5161:
5162: if (parmdecls != 0)
5163: error_with_decl (fndecl,
5164: "parm types given both in parmlist and separately");
5165:
5166: specparms = nreverse (specparms);
5167: for (parm = specparms; parm; parm = next)
5168: {
5169: next = TREE_CHAIN (parm);
5170: if (TREE_CODE (parm) == PARM_DECL)
5171: {
5172: if (DECL_NAME (parm) == 0)
5173: error_with_decl (parm, "parameter name omitted");
5174: else if (TREE_TYPE (parm) == void_type_node)
5175: error_with_decl (parm, "parameter `%s' declared void");
5176: pushdecl (parm);
5177: }
5178: else
5179: {
5180: /* If we find an enum constant or a type tag,
5181: put it aside for the moment. */
5182: TREE_CHAIN (parm) = 0;
5183: others = chainon (others, parm);
5184: }
5185: }
5186:
5187: /* Get the decls in their original chain order
5188: and record in the function. */
5189: DECL_ARGUMENTS (fndecl) = getdecls ();
5190:
5191: #if 0
5192: /* If this function takes a variable number of arguments,
5193: add a phony parameter to the end of the parm list,
5194: to represent the position of the first unnamed argument. */
5195: if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (fndecl))))
5196: != void_type_node)
5197: {
5198: tree dummy = build_decl (PARM_DECL, NULL_TREE, void_type_node);
5199: /* Let's hope the address of the unnamed parm
5200: won't depend on its type. */
5201: TREE_TYPE (dummy) = integer_type_node;
5202: DECL_ARG_TYPE (dummy) = integer_type_node;
5203: DECL_ARGUMENTS (fndecl)
5204: = chainon (DECL_ARGUMENTS (fndecl), dummy);
5205: }
5206: #endif
5207:
5208: /* Now pushdecl the enum constants. */
5209: for (parm = others; parm; parm = next)
5210: {
5211: next = TREE_CHAIN (parm);
5212: if (DECL_NAME (parm) == 0)
5213: ;
5214: else if (TREE_TYPE (parm) == void_type_node)
5215: ;
5216: else if (TREE_CODE (parm) != PARM_DECL)
5217: pushdecl (parm);
5218: }
5219:
5220: storetags (chainon (parmtags, gettags ()));
5221: }
5222: else
5223: {
5224: /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5225: each with a parm name as the TREE_VALUE.
5226:
5227: PARMDECLS is a chain of declarations for parameters.
5228: Warning! It can also contain CONST_DECLs which are not parameters
5229: but are names of enumerators of any enum types
5230: declared among the parameters.
5231:
5232: First match each formal parameter name with its declaration.
5233: Associate decls with the names and store the decls
5234: into the TREE_PURPOSE slots. */
5235:
5236: for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5237: DECL_RESULT (parm) = 0;
5238:
5239: for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5240: {
5241: register tree tail, found = NULL;
5242:
5243: if (TREE_VALUE (parm) == 0)
5244: {
5245: error_with_decl (fndecl, "parameter name missing from parameter list");
5246: TREE_PURPOSE (parm) = 0;
5247: continue;
5248: }
5249:
5250: /* See if any of the parmdecls specifies this parm by name.
5251: Ignore any enumerator decls. */
5252: for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5253: if (DECL_NAME (tail) == TREE_VALUE (parm)
5254: && TREE_CODE (tail) == PARM_DECL)
5255: {
5256: found = tail;
5257: break;
5258: }
5259:
5260: /* If declaration already marked, we have a duplicate name.
5261: Complain, and don't use this decl twice. */
5262: if (found && DECL_RESULT (found) != 0)
5263: {
5264: error_with_decl (found, "multiple parameters named `%s'");
5265: found = 0;
5266: }
5267:
5268: /* If the declaration says "void", complain and ignore it. */
5269: if (found && TREE_TYPE (found) == void_type_node)
5270: {
5271: error_with_decl (found, "parameter `%s' declared void");
5272: TREE_TYPE (found) = integer_type_node;
5273: DECL_ARG_TYPE (found) = integer_type_node;
5274: layout_decl (found, 0);
5275: }
5276:
5277: /* Traditionally, a parm declared float is actually a double. */
5278: if (found && flag_traditional
5279: && TREE_TYPE (found) == float_type_node)
5280: TREE_TYPE (found) = double_type_node;
5281:
5282: /* If no declaration found, default to int. */
5283: if (!found)
5284: {
5285: found = build_decl (PARM_DECL, TREE_VALUE (parm),
5286: integer_type_node);
5287: DECL_ARG_TYPE (found) = TREE_TYPE (found);
5288: DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
5289: DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
5290: if (extra_warnings)
5291: warning_with_decl (found, "type of `%s' defaults to `int'");
5292: pushdecl (found);
5293: }
5294:
5295: TREE_PURPOSE (parm) = found;
5296:
5297: /* Mark this decl as "already found" -- see test, above.
5298: It is safe to use DECL_RESULT for this
5299: since it is not used in PARM_DECLs or CONST_DECLs. */
5300: DECL_RESULT (found) = error_mark_node;
5301: }
5302:
5303: /* Put anything which is on the parmdecls chain and which is
5304: not a PARM_DECL onto the list NONPARMS. (The types of
5305: non-parm things which might appear on the list include
5306: enumerators and NULL-named TYPE_DECL nodes.) Complain about
5307: any actual PARM_DECLs not matched with any names. */
5308:
5309: nonparms = 0;
5310: for (parm = parmdecls; parm; )
5311: {
5312: tree next = TREE_CHAIN (parm);
5313: TREE_CHAIN (parm) = 0;
5314:
5315: if (TREE_CODE (parm) != PARM_DECL)
5316: nonparms = chainon (nonparms, parm);
5317: else
5318: {
5319: /* Complain about args with incomplete types. */
5320: if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
5321: {
5322: error_with_decl (parm, "parameter `%s' has incomplete type");
5323: TREE_TYPE (parm) = error_mark_node;
5324: }
5325:
5326: if (DECL_RESULT (parm) == 0)
5327: {
5328: error_with_decl (parm,
5329: "declaration for parameter `%s' but no such parameter");
5330: /* Pretend the parameter was not missing.
5331: This gets us to a standard state and minimizes
5332: further error messages. */
5333: specparms
5334: = chainon (specparms,
5335: tree_cons (parm, NULL_TREE, NULL_TREE));
5336: }
5337: }
5338:
5339: parm = next;
5340: }
5341:
5342: /* Chain the declarations together in the order of the list of names. */
5343: /* Store that chain in the function decl, replacing the list of names. */
5344: parm = specparms;
5345: DECL_ARGUMENTS (fndecl) = 0;
5346: {
5347: register tree last;
5348: for (last = 0; parm; parm = TREE_CHAIN (parm))
5349: if (TREE_PURPOSE (parm))
5350: {
5351: if (last == 0)
5352: DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
5353: else
5354: TREE_CHAIN (last) = TREE_PURPOSE (parm);
5355: last = TREE_PURPOSE (parm);
5356: TREE_CHAIN (last) = 0;
5357: }
5358: }
5359:
5360: /* If there was a previous prototype,
5361: set the DECL_ARG_TYPE of each argument according to
5362: the type previously specified, and report any mismatches. */
5363:
5364: if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5365: {
5366: register tree type;
5367: for (parm = DECL_ARGUMENTS (fndecl),
5368: type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5369: parm || (type && TREE_VALUE (type) != void_type_node);
5370: parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5371: {
5372: if (parm == 0 || type == 0
5373: || TREE_VALUE (type) == void_type_node)
5374: {
5375: error ("number of arguments doesn't match prototype");
5376: break;
5377: }
5378: /* Type for passing arg must be consistent
5379: with that declared for the arg. */
1.1.1.3 ! root 5380: if (! comptypes (DECL_ARG_TYPE (parm), TREE_VALUE (type)))
1.1 root 5381: {
1.1.1.3 ! root 5382: if (TREE_TYPE (parm) == TREE_VALUE (type))
1.1 root 5383: {
1.1.1.3 ! root 5384: /* Adjust argument to match prototype. E.g. a previous
! 5385: `int foo(float);' prototype causes
! 5386: `int foo(x) float x; {...}' to be treated like
! 5387: `int foo(float x) {...}'. This is particularly
! 5388: useful for argument types like uid_t. */
! 5389: DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
! 5390: #ifdef PROMOTE_PROTOTYPES
! 5391: if (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
! 5392: && TYPE_PRECISION (TREE_TYPE (parm))
! 5393: < TYPE_PRECISION (integer_type_node))
! 5394: DECL_ARG_TYPE (parm) = integer_type_node;
! 5395: #endif
! 5396: if (pedantic)
! 5397: warning ("promoted argument `%s' doesn't match prototype",
! 5398: IDENTIFIER_POINTER (DECL_NAME (parm)));
1.1 root 5399: }
1.1.1.3 ! root 5400: /* If -traditional, allow `int' argument to match
! 5401: `unsigned' prototype. */
! 5402: else if (! (flag_traditional
! 5403: && TREE_TYPE (parm) == integer_type_node
! 5404: && TREE_VALUE (type) == unsigned_type_node))
! 5405: error ("argument `%s' doesn't match prototype",
! 5406: IDENTIFIER_POINTER (DECL_NAME (parm)));
1.1 root 5407: }
5408: }
5409: TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5410: }
5411:
5412: /* Otherwise, create a prototype that would match. */
5413:
5414: else
5415: {
5416: register tree actual, type;
5417: register tree last = 0;
5418:
5419: for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5420: {
5421: type = perm_tree_cons (NULL, DECL_ARG_TYPE (parm), NULL);
5422: if (last)
5423: TREE_CHAIN (last) = type;
5424: else
5425: actual = type;
5426: last = type;
5427: }
5428: type = perm_tree_cons (NULL, void_type_node, NULL);
5429: if (last)
5430: TREE_CHAIN (last) = type;
5431: else
5432: actual = type;
5433:
1.1.1.2 root 5434: /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5435: of the type of this function, but we need to avoid having this
5436: affect the types of other similarly-typed functions, so we must
5437: first force the generation of an identical (but separate) type
5438: node for the relevant function type. The new node we create
5439: will be a variant of the main variant of the original function
5440: type. */
5441:
5442: TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5443:
1.1 root 5444: TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5445: }
5446:
5447: /* Now store the final chain of decls for the arguments
5448: as the decl-chain of the current lexical scope.
5449: Put the enumerators in as well, at the front so that
5450: DECL_ARGUMENTS is not modified. */
5451:
5452: storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
5453: }
5454:
5455: /* Make sure the binding level for the top of the function body
5456: gets a BLOCK if there are any in the function.
5457: Otherwise, the dbx output is wrong. */
5458:
5459: keep_next_if_subblocks = 1;
5460:
5461: /* ??? This might be an improvement,
5462: but needs to be thought about some more. */
5463: #if 0
5464: keep_next_level_flag = 1;
5465: #endif
5466:
5467: /* Write a record describing this function definition to the prototypes
5468: file (if requested). */
5469:
5470: gen_aux_info_record (fndecl, 1, 0, prototype);
5471:
5472: /* Initialize the RTL code for the function. */
5473:
5474: init_function_start (fndecl, input_filename, lineno);
5475:
5476: /* If this is a varargs function, inform function.c. */
5477:
5478: if (c_function_varargs)
5479: mark_varargs ();
5480:
1.1.1.3 ! root 5481: /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function. */
! 5482:
! 5483: declare_function_name ();
! 5484:
1.1 root 5485: /* Set up parameters and prepare for return, for the function. */
5486:
5487: expand_function_start (fndecl, 0);
5488:
5489: /* If this function is `main', emit a call to `__main'
5490: to run global initializers, etc. */
5491: if (DECL_NAME (fndecl)
5492: && strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main") == 0
5493: && DECL_CONTEXT (fndecl) == NULL_TREE)
5494: expand_main_function ();
5495: }
5496:
5497: /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
5498: each with a parm name as the TREE_VALUE. A null pointer as TREE_VALUE
5499: stands for an ellipsis in the identifier list.
5500:
5501: PARMLIST is the data returned by get_parm_info for the
5502: parmlist that follows the semicolon.
5503:
5504: We return a value of the same sort that get_parm_info returns,
5505: except that it describes the combination of identifiers and parmlist. */
5506:
5507: tree
5508: combine_parm_decls (specparms, parmlist, void_at_end)
5509: tree specparms, parmlist;
5510: int void_at_end;
5511: {
5512: register tree fndecl = current_function_decl;
5513: register tree parm;
5514:
5515: tree parmdecls = TREE_PURPOSE (parmlist);
5516:
5517: /* This is a chain of any other decls that came in among the parm
5518: declarations. They were separated already by get_parm_info,
5519: so we just need to keep them separate. */
5520: tree nonparms = TREE_VALUE (parmlist);
5521:
5522: tree types = 0;
5523:
5524: for (parm = parmdecls; parm; parm = TREE_CHAIN (parm))
5525: DECL_RESULT (parm) = 0;
5526:
5527: for (parm = specparms; parm; parm = TREE_CHAIN (parm))
5528: {
5529: register tree tail, found = NULL;
5530:
5531: /* See if any of the parmdecls specifies this parm by name. */
5532: for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
5533: if (DECL_NAME (tail) == TREE_VALUE (parm))
5534: {
5535: found = tail;
5536: break;
5537: }
5538:
5539: /* If declaration already marked, we have a duplicate name.
5540: Complain, and don't use this decl twice. */
5541: if (found && DECL_RESULT (found) != 0)
5542: {
5543: error_with_decl (found, "multiple parameters named `%s'");
5544: found = 0;
5545: }
5546:
5547: /* If the declaration says "void", complain and ignore it. */
5548: if (found && TREE_TYPE (found) == void_type_node)
5549: {
5550: error_with_decl (found, "parameter `%s' declared void");
5551: TREE_TYPE (found) = integer_type_node;
5552: DECL_ARG_TYPE (found) = integer_type_node;
5553: layout_decl (found, 0);
5554: }
5555:
5556: /* Traditionally, a parm declared float is actually a double. */
5557: if (found && flag_traditional
5558: && TREE_TYPE (found) == float_type_node)
5559: TREE_TYPE (found) = double_type_node;
5560:
5561: /* If no declaration found, default to int. */
5562: if (!found)
5563: {
5564: found = build_decl (PARM_DECL, TREE_VALUE (parm),
5565: integer_type_node);
5566: DECL_ARG_TYPE (found) = TREE_TYPE (found);
5567: DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
5568: DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
5569: error (found, "type of parameter `%s' is not declared");
5570: pushdecl (found);
5571: }
5572:
5573: TREE_PURPOSE (parm) = found;
5574:
5575: /* Mark this decl as "already found" -- see test, above.
5576: It is safe to use DECL_RESULT for this
5577: since it is not used in PARM_DECLs or CONST_DECLs. */
5578: DECL_RESULT (found) = error_mark_node;
5579: }
5580:
5581: /* Complain about any actual PARM_DECLs not matched with any names. */
5582:
5583: for (parm = parmdecls; parm; )
5584: {
5585: tree next = TREE_CHAIN (parm);
5586: TREE_CHAIN (parm) = 0;
5587:
5588: /* Complain about args with incomplete types. */
5589: if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
5590: {
5591: error_with_decl (parm, "parameter `%s' has incomplete type");
5592: TREE_TYPE (parm) = error_mark_node;
5593: }
5594:
5595: if (DECL_RESULT (parm) == 0)
5596: {
5597: error_with_decl (parm,
5598: "declaration for parameter `%s' but no such parameter");
5599: /* Pretend the parameter was not missing.
5600: This gets us to a standard state and minimizes
5601: further error messages. */
5602: specparms
5603: = chainon (specparms,
5604: tree_cons (parm, NULL_TREE, NULL_TREE));
5605: }
5606:
5607: parm = next;
5608: }
5609:
5610: /* Chain the declarations together in the order of the list of names.
5611: At the same time, build up a list of their types, in reverse order. */
5612:
5613: parm = specparms;
5614: parmdecls = 0;
5615: {
5616: register tree last;
5617: for (last = 0; parm; parm = TREE_CHAIN (parm))
5618: if (TREE_PURPOSE (parm))
5619: {
5620: if (last == 0)
5621: parmdecls = TREE_PURPOSE (parm);
5622: else
5623: TREE_CHAIN (last) = TREE_PURPOSE (parm);
5624: last = TREE_PURPOSE (parm);
5625: TREE_CHAIN (last) = 0;
5626:
5627: types = saveable_tree_cons (NULL_TREE, TREE_TYPE (parm), types);
5628: }
5629: }
5630:
5631: if (void_at_end)
5632: return saveable_tree_cons (parmdecls, nonparms,
5633: nreverse (saveable_tree_cons (NULL_TREE, void_type_node, types)));
5634:
5635: return saveable_tree_cons (parmdecls, nonparms, nreverse (types));
5636: }
5637:
5638: /* Finish up a function declaration and compile that function
5639: all the way to assembler language output. The free the storage
5640: for the function definition.
5641:
5642: This is called after parsing the body of the function definition.
5643:
5644: NESTED is nonzero if the function being finished is nested in another. */
5645:
5646: void
5647: finish_function (nested)
5648: int nested;
5649: {
5650: register tree fndecl = current_function_decl;
5651:
5652: /* TREE_READONLY (fndecl) = 1;
5653: This caused &foo to be of type ptr-to-const-function
5654: which then got a warning when stored in a ptr-to-function variable. */
5655:
5656: poplevel (1, 0, 1);
5657:
5658: /* Must mark the RESULT_DECL as being in this function. */
5659:
5660: DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
5661:
5662: /* Obey `register' declarations if `setjmp' is called in this fn. */
5663: if (flag_traditional && current_function_calls_setjmp)
5664: {
5665: setjmp_protect (DECL_INITIAL (fndecl));
5666: setjmp_protect_args ();
5667: }
5668:
5669: #ifdef DEFAULT_MAIN_RETURN
5670: if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main"))
5671: {
5672: /* Make it so that `main' always returns success by default. */
5673: DEFAULT_MAIN_RETURN;
5674: }
5675: #endif
5676:
5677: /* Generate rtl for function exit. */
5678: expand_function_end (input_filename, lineno);
5679:
5680: /* So we can tell if jump_optimize sets it to 1. */
5681: can_reach_end = 0;
5682:
5683: /* Run the optimizers and output the assembler code for this function. */
5684: rest_of_compilation (fndecl);
5685:
5686: current_function_returns_null |= can_reach_end;
5687:
5688: if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
5689: warning ("`volatile' function does return");
5690: else if (warn_return_type && current_function_returns_null
5691: && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) != void_type_node)
5692: /* If this function returns non-void and control can drop through,
5693: complain. */
5694: warning ("control reaches end of non-void function");
5695: /* With just -W, complain only if function returns both with
5696: and without a value. */
5697: else if (extra_warnings
5698: && current_function_returns_value && current_function_returns_null)
5699: warning ("this function may return with or without a value");
5700:
5701: /* Free all the tree nodes making up this function. */
5702: /* Switch back to allocating nodes permanently
5703: until we start another function. */
5704: if (! nested)
5705: permanent_allocation ();
5706:
5707: if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested)
5708: {
5709: /* Stop pointing to the local nodes about to be freed. */
5710: /* But DECL_INITIAL must remain nonzero so we know this
5711: was an actual function definition. */
5712: /* For a nested function, this is done in pop_c_function_context. */
5713: DECL_INITIAL (fndecl) = error_mark_node;
5714: DECL_ARGUMENTS (fndecl) = 0;
5715: }
5716:
5717: if (! nested)
5718: {
5719: /* Let the error reporting routines know that we're outside a
5720: function. For a nested function, this value is used in
5721: pop_c_function_context and then reset via pop_function_context. */
5722: current_function_decl = NULL;
5723: }
5724: }
5725:
5726: /* Save and restore the variables in this file and elsewhere
5727: that keep track of the progress of compilation of the current function.
5728: Used for nested functions. */
5729:
5730: struct c_function
5731: {
5732: struct c_function *next;
5733: tree enum_next_value;
5734: tree named_labels;
5735: tree shadowed_labels;
5736: int returns_value;
5737: int returns_null;
5738: int warn_about_return_type;
5739: int extern_inline;
5740: struct binding_level *binding_level;
5741: };
5742:
5743: struct c_function *c_function_chain;
5744:
5745: /* Save and reinitialize the variables
5746: used during compilation of a C function. */
5747:
5748: void
5749: push_c_function_context ()
5750: {
5751: struct c_function *p
5752: = (struct c_function *) xmalloc (sizeof (struct c_function));
5753:
1.1.1.3 ! root 5754: if (pedantic)
! 5755: pedwarn ("ANSI C forbids nested functions");
! 5756:
1.1 root 5757: push_function_context ();
5758:
5759: p->next = c_function_chain;
5760: c_function_chain = p;
5761:
5762: p->enum_next_value = enum_next_value;
5763: p->named_labels = named_labels;
5764: p->shadowed_labels = shadowed_labels;
5765: p->returns_value = current_function_returns_value;
5766: p->returns_null = current_function_returns_null;
5767: p->warn_about_return_type = warn_about_return_type;
5768: p->extern_inline = current_extern_inline;
5769: p->binding_level = current_binding_level;
5770: }
5771:
5772: /* Restore the variables used during compilation of a C function. */
5773:
5774: void
5775: pop_c_function_context ()
5776: {
5777: struct c_function *p = c_function_chain;
5778: tree link;
5779:
5780: /* Bring back all the labels that were shadowed. */
5781: for (link = shadowed_labels; link; link = TREE_CHAIN (link))
5782: if (DECL_NAME (TREE_VALUE (link)) != 0)
5783: IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)))
5784: = TREE_VALUE (link);
5785:
5786: if (DECL_SAVED_INSNS (current_function_decl) == 0)
5787: {
5788: /* Stop pointing to the local nodes about to be freed. */
5789: /* But DECL_INITIAL must remain nonzero so we know this
5790: was an actual function definition. */
5791: DECL_INITIAL (current_function_decl) = error_mark_node;
5792: DECL_ARGUMENTS (current_function_decl) = 0;
5793: }
5794:
5795: pop_function_context ();
5796:
5797: c_function_chain = p->next;
5798:
5799: enum_next_value = p->enum_next_value;
5800: named_labels = p->named_labels;
5801: shadowed_labels = p->shadowed_labels;
5802: current_function_returns_value = p->returns_value;
5803: current_function_returns_null = p->returns_null;
5804: warn_about_return_type = p->warn_about_return_type;
5805: current_extern_inline = p->extern_inline;
5806: current_binding_level = p->binding_level;
5807:
5808: free (p);
5809: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.