re PR c/15698 (no error in presence of broken builtin fn + K&R declaration)
[gcc.git] / gcc / c-decl.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
63
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
71
72 \f
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
76
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
79
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
82
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
87 that case. */
88
89 static tree enum_next_value;
90
91 /* Nonzero means that there was overflow computing enum_next_value. */
92
93 static int enum_overflow;
94
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
98
99 static location_t current_function_prototype_locus;
100
101 /* Whether this prototype was built-in. */
102
103 static bool current_function_prototype_built_in;
104
105 /* The argument type information of this prototype. */
106
107 static tree current_function_prototype_arg_types;
108
109 /* The argument information structure for the function currently being
110 defined. */
111
112 static struct c_arg_info *current_function_arg_info;
113
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
118
119 /* The current statement tree. */
120
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
122
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
126
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
130
131 static GTY(()) tree all_translation_units;
132
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
135
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
138
139 int current_function_returns_value;
140
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
143
144 int current_function_returns_null;
145
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
148
149 int current_function_returns_abnormally;
150
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
153
154 static int warn_about_return_type;
155
156 /* Nonzero when starting a function declared `extern inline'. */
157
158 static int current_extern_inline;
159
160 /* Nonzero when the current toplevel function contains a declaration
161 of a nested function which is never defined. */
162
163 static bool undef_nested_function;
164
165 /* True means global_bindings_p should return false even if the scope stack
166 says we are in file scope. */
167 bool c_override_global_bindings_to_false;
168
169 \f
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
176
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
182 function.
183
184 The ->type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
193
194 The depth field is copied from the scope structure that holds this
195 decl. It is used to preserve the proper ordering of the ->shadowed
196 field (see bind()) and also for a handful of special-case checks.
197 Finally, the invisible bit is true for a decl which should be
198 ignored for purposes of normal name lookup, and the nested bit is
199 true for a decl that's been bound a second time in an inner scope;
200 in all such cases, the binding in the outer scope will have its
201 invisible bit true. */
202
203 struct c_binding GTY((chain_next ("%h.prev")))
204 {
205 tree decl; /* the decl bound */
206 tree type; /* the type in this scope */
207 tree id; /* the identifier it's bound to */
208 struct c_binding *prev; /* the previous decl in this scope */
209 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
210 unsigned int depth : 28; /* depth of this scope */
211 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
212 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
213 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
214 /* one free bit */
215 };
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
220
221 #define I_SYMBOL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
225
226 #define I_TAG_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
230
231 #define I_LABEL_BINDING(node) \
232 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
235
236 /* Each C symbol points to three linked lists of c_binding structures.
237 These describe the values of the identifier in the three different
238 namespaces defined by the language. */
239
240 struct lang_identifier GTY(())
241 {
242 struct c_common_identifier common_id;
243 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244 struct c_binding *tag_binding; /* struct/union/enum tags */
245 struct c_binding *label_binding; /* labels */
246 };
247
248 /* Validate c-lang.c's assumptions. */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
251
252 /* The resulting tree type. */
253
254 union lang_tree_node
255 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
257 {
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
260 generic;
261 struct lang_identifier GTY ((tag ("1"))) identifier;
262 };
263
264 /* Each c_scope structure describes the complete contents of one
265 scope. Four scopes are distinguished specially: the innermost or
266 current scope, the innermost function scope, the file scope (always
267 the second to outermost) and the outermost or external scope.
268
269 Most declarations are recorded in the current scope.
270
271 All normal label declarations are recorded in the innermost
272 function scope, as are bindings of undeclared identifiers to
273 error_mark_node. (GCC permits nested functions as an extension,
274 hence the 'innermost' qualifier.) Explicitly declared labels
275 (using the __label__ extension) appear in the current scope.
276
277 Being in the file scope (current_scope == file_scope) causes
278 special behavior in several places below. Also, under some
279 conditions the Objective-C front end records declarations in the
280 file scope even though that isn't the current scope.
281
282 All declarations with external linkage are recorded in the external
283 scope, even if they aren't visible there; this models the fact that
284 such declarations are visible to the entire program, and (with a
285 bit of cleverness, see pushdecl) allows diagnosis of some violations
286 of C99 6.2.2p7 and 6.2.7p2:
287
288 If, within the same translation unit, the same identifier appears
289 with both internal and external linkage, the behavior is
290 undefined.
291
292 All declarations that refer to the same object or function shall
293 have compatible type; otherwise, the behavior is undefined.
294
295 Initially only the built-in declarations, which describe compiler
296 intrinsic functions plus a subset of the standard library, are in
297 this scope.
298
299 The order of the blocks list matters, and it is frequently appended
300 to. To avoid having to walk all the way to the end of the list on
301 each insertion, or reverse the list later, we maintain a pointer to
302 the last list entry. (FIXME: It should be feasible to use a reversed
303 list here.)
304
305 The bindings list is strictly in reverse order of declarations;
306 pop_scope relies on this. */
307
308
309 struct c_scope GTY((chain_next ("%h.outer")))
310 {
311 /* The scope containing this one. */
312 struct c_scope *outer;
313
314 /* The next outermost function scope. */
315 struct c_scope *outer_function;
316
317 /* All bindings in this scope. */
318 struct c_binding *bindings;
319
320 /* For each scope (except the global one), a chain of BLOCK nodes
321 for all the scopes that were entered and exited one level down. */
322 tree blocks;
323 tree blocks_last;
324
325 /* The depth of this scope. Used to keep the ->shadowed chain of
326 bindings sorted innermost to outermost. */
327 unsigned int depth : 28;
328
329 /* True if we are currently filling this scope with parameter
330 declarations. */
331 BOOL_BITFIELD parm_flag : 1;
332
333 /* True if we already complained about forward parameter decls
334 in this scope. This prevents double warnings on
335 foo (int a; int b; ...) */
336 BOOL_BITFIELD warned_forward_parm_decls : 1;
337
338 /* True if this is the outermost block scope of a function body.
339 This scope contains the parameters, the local variables declared
340 in the outermost block, and all the labels (except those in
341 nested functions, or declared at block scope with __label__). */
342 BOOL_BITFIELD function_body : 1;
343
344 /* True means make a BLOCK for this scope no matter what. */
345 BOOL_BITFIELD keep : 1;
346 };
347
348 /* The scope currently in effect. */
349
350 static GTY(()) struct c_scope *current_scope;
351
352 /* The innermost function scope. Ordinary (not explicitly declared)
353 labels, bindings to error_mark_node, and the lazily-created
354 bindings of __func__ and its friends get this scope. */
355
356 static GTY(()) struct c_scope *current_function_scope;
357
358 /* The C file scope. This is reset for each input translation unit. */
359
360 static GTY(()) struct c_scope *file_scope;
361
362 /* The outermost scope. This is used for all declarations with
363 external linkage, and only these, hence the name. */
364
365 static GTY(()) struct c_scope *external_scope;
366
367 /* A chain of c_scope structures awaiting reuse. */
368
369 static GTY((deletable)) struct c_scope *scope_freelist;
370
371 /* A chain of c_binding structures awaiting reuse. */
372
373 static GTY((deletable)) struct c_binding *binding_freelist;
374
375 /* Append VAR to LIST in scope SCOPE. */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
377 struct c_scope *s_ = (scope); \
378 tree d_ = (decl); \
379 if (s_->list##_last) \
380 TREE_CHAIN (s_->list##_last) = d_; \
381 else \
382 s_->list = d_; \
383 s_->list##_last = d_; \
384 } while (0)
385
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
388 struct c_scope *t_ = (tscope); \
389 struct c_scope *f_ = (fscope); \
390 if (t_->to##_last) \
391 TREE_CHAIN (t_->to##_last) = f_->from; \
392 else \
393 t_->to = f_->from; \
394 t_->to##_last = f_->from##_last; \
395 } while (0)
396
397 /* True means unconditionally make a BLOCK for the next scope pushed. */
398
399 static bool keep_next_level_flag;
400
401 /* True means the next call to push_scope will be the outermost scope
402 of a function body, so do not push a new scope, merely cease
403 expecting parameter decls. */
404
405 static bool next_is_function_body;
406
407 /* Functions called automatically at the beginning and end of execution. */
408
409 static GTY(()) tree static_ctors;
410 static GTY(()) tree static_dtors;
411
412 /* Forward declarations. */
413 static tree lookup_name_in_scope (tree, struct c_scope *);
414 static tree c_make_fname_decl (tree, int);
415 static tree grokdeclarator (const struct c_declarator *,
416 struct c_declspecs *,
417 enum decl_context, bool, tree *);
418 static tree grokparms (struct c_arg_info *, bool);
419 static void layout_array_type (tree);
420 \f
421 /* States indicating how grokdeclarator() should handle declspecs marked
422 with __attribute__((deprecated)). An object declared as
423 __attribute__((deprecated)) suppresses warnings of uses of other
424 deprecated items. */
425
426 enum deprecated_states {
427 DEPRECATED_NORMAL,
428 DEPRECATED_SUPPRESS
429 };
430
431 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
432
433 void
434 c_print_identifier (FILE *file, tree node, int indent)
435 {
436 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
437 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
438 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
439 if (C_IS_RESERVED_WORD (node))
440 {
441 tree rid = ridpointers[C_RID_CODE (node)];
442 indent_to (file, indent + 4);
443 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
444 (void *) rid, IDENTIFIER_POINTER (rid));
445 }
446 }
447
448 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
449 which may be any of several kinds of DECL or TYPE or error_mark_node,
450 in the scope SCOPE. */
451 static void
452 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
453 {
454 struct c_binding *b, **here;
455
456 if (binding_freelist)
457 {
458 b = binding_freelist;
459 binding_freelist = b->prev;
460 }
461 else
462 b = GGC_NEW (struct c_binding);
463
464 b->shadowed = 0;
465 b->decl = decl;
466 b->id = name;
467 b->depth = scope->depth;
468 b->invisible = invisible;
469 b->nested = nested;
470 b->inner_comp = 0;
471
472 b->type = 0;
473
474 b->prev = scope->bindings;
475 scope->bindings = b;
476
477 if (!name)
478 return;
479
480 switch (TREE_CODE (decl))
481 {
482 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
483 case ENUMERAL_TYPE:
484 case UNION_TYPE:
485 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
486 case VAR_DECL:
487 case FUNCTION_DECL:
488 case TYPE_DECL:
489 case CONST_DECL:
490 case PARM_DECL:
491 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
492
493 default:
494 gcc_unreachable ();
495 }
496
497 /* Locate the appropriate place in the chain of shadowed decls
498 to insert this binding. Normally, scope == current_scope and
499 this does nothing. */
500 while (*here && (*here)->depth > scope->depth)
501 here = &(*here)->shadowed;
502
503 b->shadowed = *here;
504 *here = b;
505 }
506
507 /* Clear the binding structure B, stick it on the binding_freelist,
508 and return the former value of b->prev. This is used by pop_scope
509 and get_parm_info to iterate destructively over all the bindings
510 from a given scope. */
511 static struct c_binding *
512 free_binding_and_advance (struct c_binding *b)
513 {
514 struct c_binding *prev = b->prev;
515
516 memset (b, 0, sizeof (struct c_binding));
517 b->prev = binding_freelist;
518 binding_freelist = b;
519
520 return prev;
521 }
522
523 \f
524 /* Hook called at end of compilation to assume 1 elt
525 for a file-scope tentative array defn that wasn't complete before. */
526
527 void
528 c_finish_incomplete_decl (tree decl)
529 {
530 if (TREE_CODE (decl) == VAR_DECL)
531 {
532 tree type = TREE_TYPE (decl);
533 if (type != error_mark_node
534 && TREE_CODE (type) == ARRAY_TYPE
535 && !DECL_EXTERNAL (decl)
536 && TYPE_DOMAIN (type) == 0)
537 {
538 warning (0, "%Jarray %qD assumed to have one element", decl, decl);
539
540 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
541
542 layout_decl (decl, 0);
543 }
544 }
545 }
546 \f
547 /* The Objective-C front-end often needs to determine the current scope. */
548
549 void *
550 objc_get_current_scope (void)
551 {
552 return current_scope;
553 }
554
555 /* The following function is used only by Objective-C. It needs to live here
556 because it accesses the innards of c_scope. */
557
558 void
559 objc_mark_locals_volatile (void *enclosing_blk)
560 {
561 struct c_scope *scope;
562 struct c_binding *b;
563
564 for (scope = current_scope;
565 scope && scope != enclosing_blk;
566 scope = scope->outer)
567 {
568 for (b = scope->bindings; b; b = b->prev)
569 {
570 if (TREE_CODE (b->decl) == VAR_DECL
571 || TREE_CODE (b->decl) == PARM_DECL)
572 {
573 C_DECL_REGISTER (b->decl) = 0;
574 DECL_REGISTER (b->decl) = 0;
575 TREE_THIS_VOLATILE (b->decl) = 1;
576 }
577 }
578
579 /* Do not climb up past the current function. */
580 if (scope->function_body)
581 break;
582 }
583 }
584
585 /* Nonzero if we are currently in file scope. */
586
587 int
588 global_bindings_p (void)
589 {
590 return current_scope == file_scope && !c_override_global_bindings_to_false;
591 }
592
593 void
594 keep_next_level (void)
595 {
596 keep_next_level_flag = true;
597 }
598
599 /* Identify this scope as currently being filled with parameters. */
600
601 void
602 declare_parm_level (void)
603 {
604 current_scope->parm_flag = true;
605 }
606
607 void
608 push_scope (void)
609 {
610 if (next_is_function_body)
611 {
612 /* This is the transition from the parameters to the top level
613 of the function body. These are the same scope
614 (C99 6.2.1p4,6) so we do not push another scope structure.
615 next_is_function_body is set only by store_parm_decls, which
616 in turn is called when and only when we are about to
617 encounter the opening curly brace for the function body.
618
619 The outermost block of a function always gets a BLOCK node,
620 because the debugging output routines expect that each
621 function has at least one BLOCK. */
622 current_scope->parm_flag = false;
623 current_scope->function_body = true;
624 current_scope->keep = true;
625 current_scope->outer_function = current_function_scope;
626 current_function_scope = current_scope;
627
628 keep_next_level_flag = false;
629 next_is_function_body = false;
630 }
631 else
632 {
633 struct c_scope *scope;
634 if (scope_freelist)
635 {
636 scope = scope_freelist;
637 scope_freelist = scope->outer;
638 }
639 else
640 scope = GGC_CNEW (struct c_scope);
641
642 scope->keep = keep_next_level_flag;
643 scope->outer = current_scope;
644 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
645
646 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
647 possible. */
648 if (current_scope && scope->depth == 0)
649 {
650 scope->depth--;
651 sorry ("GCC supports only %u nested scopes", scope->depth);
652 }
653
654 current_scope = scope;
655 keep_next_level_flag = false;
656 }
657 }
658
659 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
660
661 static void
662 set_type_context (tree type, tree context)
663 {
664 for (type = TYPE_MAIN_VARIANT (type); type;
665 type = TYPE_NEXT_VARIANT (type))
666 TYPE_CONTEXT (type) = context;
667 }
668
669 /* Exit a scope. Restore the state of the identifier-decl mappings
670 that were in effect when this scope was entered. Return a BLOCK
671 node containing all the DECLs in this scope that are of interest
672 to debug info generation. */
673
674 tree
675 pop_scope (void)
676 {
677 struct c_scope *scope = current_scope;
678 tree block, context, p;
679 struct c_binding *b;
680
681 bool functionbody = scope->function_body;
682 bool keep = functionbody || scope->keep || scope->bindings;
683
684 c_end_vm_scope (scope->depth);
685
686 /* If appropriate, create a BLOCK to record the decls for the life
687 of this function. */
688 block = 0;
689 if (keep)
690 {
691 block = make_node (BLOCK);
692 BLOCK_SUBBLOCKS (block) = scope->blocks;
693 TREE_USED (block) = 1;
694
695 /* In each subblock, record that this is its superior. */
696 for (p = scope->blocks; p; p = TREE_CHAIN (p))
697 BLOCK_SUPERCONTEXT (p) = block;
698
699 BLOCK_VARS (block) = 0;
700 }
701
702 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
703 scope must be set so that they point to the appropriate
704 construct, i.e. either to the current FUNCTION_DECL node, or
705 else to the BLOCK node we just constructed.
706
707 Note that for tagged types whose scope is just the formal
708 parameter list for some function type specification, we can't
709 properly set their TYPE_CONTEXTs here, because we don't have a
710 pointer to the appropriate FUNCTION_TYPE node readily available
711 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
712 type nodes get set in `grokdeclarator' as soon as we have created
713 the FUNCTION_TYPE node which will represent the "scope" for these
714 "parameter list local" tagged types. */
715 if (scope->function_body)
716 context = current_function_decl;
717 else if (scope == file_scope)
718 {
719 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
720 TREE_CHAIN (file_decl) = all_translation_units;
721 all_translation_units = file_decl;
722 context = file_decl;
723 }
724 else
725 context = block;
726
727 /* Clear all bindings in this scope. */
728 for (b = scope->bindings; b; b = free_binding_and_advance (b))
729 {
730 p = b->decl;
731 switch (TREE_CODE (p))
732 {
733 case LABEL_DECL:
734 /* Warnings for unused labels, errors for undefined labels. */
735 if (TREE_USED (p) && !DECL_INITIAL (p))
736 {
737 error ("%Jlabel %qD used but not defined", p, p);
738 DECL_INITIAL (p) = error_mark_node;
739 }
740 else if (!TREE_USED (p) && warn_unused_label)
741 {
742 if (DECL_INITIAL (p))
743 warning (0, "%Jlabel %qD defined but not used", p, p);
744 else
745 warning (0, "%Jlabel %qD declared but not defined", p, p);
746 }
747 /* Labels go in BLOCK_VARS. */
748 TREE_CHAIN (p) = BLOCK_VARS (block);
749 BLOCK_VARS (block) = p;
750 gcc_assert (I_LABEL_BINDING (b->id) == b);
751 I_LABEL_BINDING (b->id) = b->shadowed;
752 break;
753
754 case ENUMERAL_TYPE:
755 case UNION_TYPE:
756 case RECORD_TYPE:
757 set_type_context (p, context);
758
759 /* Types may not have tag-names, in which case the type
760 appears in the bindings list with b->id NULL. */
761 if (b->id)
762 {
763 gcc_assert (I_TAG_BINDING (b->id) == b);
764 I_TAG_BINDING (b->id) = b->shadowed;
765 }
766 break;
767
768 case FUNCTION_DECL:
769 /* Propagate TREE_ADDRESSABLE from nested functions to their
770 containing functions. */
771 if (!TREE_ASM_WRITTEN (p)
772 && DECL_INITIAL (p) != 0
773 && TREE_ADDRESSABLE (p)
774 && DECL_ABSTRACT_ORIGIN (p) != 0
775 && DECL_ABSTRACT_ORIGIN (p) != p)
776 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
777 if (!DECL_EXTERNAL (p)
778 && DECL_INITIAL (p) == 0)
779 {
780 error ("%Jnested function %qD declared but never defined", p, p);
781 undef_nested_function = true;
782 }
783 goto common_symbol;
784
785 case VAR_DECL:
786 /* Warnings for unused variables. */
787 if (warn_unused_variable
788 && !TREE_USED (p)
789 && !DECL_IN_SYSTEM_HEADER (p)
790 && DECL_NAME (p)
791 && !DECL_ARTIFICIAL (p)
792 && scope != file_scope
793 && scope != external_scope)
794 warning (0, "%Junused variable %qD", p, p);
795
796 if (b->inner_comp)
797 {
798 error ("%Jtype of array %qD completed incompatibly with"
799 " implicit initialization", p, p);
800 }
801
802 /* Fall through. */
803 case TYPE_DECL:
804 case CONST_DECL:
805 common_symbol:
806 /* All of these go in BLOCK_VARS, but only if this is the
807 binding in the home scope. */
808 if (!b->nested)
809 {
810 TREE_CHAIN (p) = BLOCK_VARS (block);
811 BLOCK_VARS (block) = p;
812 }
813 /* If this is the file scope, and we are processing more
814 than one translation unit in this compilation, set
815 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
816 This makes same_translation_unit_p work, and causes
817 static declarations to be given disambiguating suffixes. */
818 if (scope == file_scope && num_in_fnames > 1)
819 {
820 DECL_CONTEXT (p) = context;
821 if (TREE_CODE (p) == TYPE_DECL)
822 set_type_context (TREE_TYPE (p), context);
823 }
824
825 /* Fall through. */
826 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
827 already been put there by store_parm_decls. Unused-
828 parameter warnings are handled by function.c.
829 error_mark_node obviously does not go in BLOCK_VARS and
830 does not get unused-variable warnings. */
831 case PARM_DECL:
832 case ERROR_MARK:
833 /* It is possible for a decl not to have a name. We get
834 here with b->id NULL in this case. */
835 if (b->id)
836 {
837 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
838 I_SYMBOL_BINDING (b->id) = b->shadowed;
839 if (b->shadowed && b->shadowed->type)
840 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
841 }
842 break;
843
844 default:
845 gcc_unreachable ();
846 }
847 }
848
849
850 /* Dispose of the block that we just made inside some higher level. */
851 if ((scope->function_body || scope == file_scope) && context)
852 {
853 DECL_INITIAL (context) = block;
854 BLOCK_SUPERCONTEXT (block) = context;
855 }
856 else if (scope->outer)
857 {
858 if (block)
859 SCOPE_LIST_APPEND (scope->outer, blocks, block);
860 /* If we did not make a block for the scope just exited, any
861 blocks made for inner scopes must be carried forward so they
862 will later become subblocks of something else. */
863 else if (scope->blocks)
864 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
865 }
866
867 /* Pop the current scope, and free the structure for reuse. */
868 current_scope = scope->outer;
869 if (scope->function_body)
870 current_function_scope = scope->outer_function;
871
872 memset (scope, 0, sizeof (struct c_scope));
873 scope->outer = scope_freelist;
874 scope_freelist = scope;
875
876 return block;
877 }
878
879 void
880 push_file_scope (void)
881 {
882 tree decl;
883
884 if (file_scope)
885 return;
886
887 push_scope ();
888 file_scope = current_scope;
889
890 start_fname_decls ();
891
892 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
893 bind (DECL_NAME (decl), decl, file_scope,
894 /*invisible=*/false, /*nested=*/true);
895 }
896
897 void
898 pop_file_scope (void)
899 {
900 /* In case there were missing closebraces, get us back to the global
901 binding level. */
902 while (current_scope != file_scope)
903 pop_scope ();
904
905 /* __FUNCTION__ is defined at file scope (""). This
906 call may not be necessary as my tests indicate it
907 still works without it. */
908 finish_fname_decls ();
909
910 /* This is the point to write out a PCH if we're doing that.
911 In that case we do not want to do anything else. */
912 if (pch_file)
913 {
914 c_common_write_pch ();
915 return;
916 }
917
918 /* Pop off the file scope and close this translation unit. */
919 pop_scope ();
920 file_scope = 0;
921
922 maybe_apply_pending_pragma_weaks ();
923 cgraph_finalize_compilation_unit ();
924 }
925
926 /* Insert BLOCK at the end of the list of subblocks of the current
927 scope. This is used when a BIND_EXPR is expanded, to handle the
928 BLOCK node inside the BIND_EXPR. */
929
930 void
931 insert_block (tree block)
932 {
933 TREE_USED (block) = 1;
934 SCOPE_LIST_APPEND (current_scope, blocks, block);
935 }
936 \f
937 /* Push a definition or a declaration of struct, union or enum tag "name".
938 "type" should be the type node.
939 We assume that the tag "name" is not already defined.
940
941 Note that the definition may really be just a forward reference.
942 In that case, the TYPE_SIZE will be zero. */
943
944 static void
945 pushtag (tree name, tree type)
946 {
947 /* Record the identifier as the type's name if it has none. */
948 if (name && !TYPE_NAME (type))
949 TYPE_NAME (type) = name;
950 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
951
952 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
953 tagged type we just added to the current scope. This fake
954 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
955 to output a representation of a tagged type, and it also gives
956 us a convenient place to record the "scope start" address for the
957 tagged type. */
958
959 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
960
961 /* An approximation for now, so we can tell this is a function-scope tag.
962 This will be updated in pop_scope. */
963 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
964 }
965 \f
966 /* Subroutine of compare_decls. Allow harmless mismatches in return
967 and argument types provided that the type modes match. This function
968 return a unified type given a suitable match, and 0 otherwise. */
969
970 static tree
971 match_builtin_function_types (tree newtype, tree oldtype)
972 {
973 tree newrettype, oldrettype;
974 tree newargs, oldargs;
975 tree trytype, tryargs;
976
977 /* Accept the return type of the new declaration if same modes. */
978 oldrettype = TREE_TYPE (oldtype);
979 newrettype = TREE_TYPE (newtype);
980
981 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
982 return 0;
983
984 oldargs = TYPE_ARG_TYPES (oldtype);
985 newargs = TYPE_ARG_TYPES (newtype);
986 tryargs = newargs;
987
988 while (oldargs || newargs)
989 {
990 if (!oldargs
991 || !newargs
992 || !TREE_VALUE (oldargs)
993 || !TREE_VALUE (newargs)
994 || TYPE_MODE (TREE_VALUE (oldargs))
995 != TYPE_MODE (TREE_VALUE (newargs)))
996 return 0;
997
998 oldargs = TREE_CHAIN (oldargs);
999 newargs = TREE_CHAIN (newargs);
1000 }
1001
1002 trytype = build_function_type (newrettype, tryargs);
1003 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1004 }
1005
1006 /* Subroutine of diagnose_mismatched_decls. Check for function type
1007 mismatch involving an empty arglist vs a nonempty one and give clearer
1008 diagnostics. */
1009 static void
1010 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1011 tree newtype, tree oldtype)
1012 {
1013 tree t;
1014
1015 if (TREE_CODE (olddecl) != FUNCTION_DECL
1016 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1017 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1018 ||
1019 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1020 return;
1021
1022 t = TYPE_ARG_TYPES (oldtype);
1023 if (t == 0)
1024 t = TYPE_ARG_TYPES (newtype);
1025 for (; t; t = TREE_CHAIN (t))
1026 {
1027 tree type = TREE_VALUE (t);
1028
1029 if (TREE_CHAIN (t) == 0
1030 && TYPE_MAIN_VARIANT (type) != void_type_node)
1031 {
1032 inform ("a parameter list with an ellipsis can%'t match "
1033 "an empty parameter name list declaration");
1034 break;
1035 }
1036
1037 if (c_type_promotes_to (type) != type)
1038 {
1039 inform ("an argument type that has a default promotion can%'t match "
1040 "an empty parameter name list declaration");
1041 break;
1042 }
1043 }
1044 }
1045
1046 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1047 old-style function definition, NEWDECL is a prototype declaration.
1048 Diagnose inconsistencies in the argument list. Returns TRUE if
1049 the prototype is compatible, FALSE if not. */
1050 static bool
1051 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1052 {
1053 tree newargs, oldargs;
1054 int i;
1055
1056 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1057
1058 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1059 newargs = TYPE_ARG_TYPES (newtype);
1060 i = 1;
1061
1062 for (;;)
1063 {
1064 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1065 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1066
1067 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1068 break;
1069
1070 /* Reaching the end of just one list means the two decls don't
1071 agree on the number of arguments. */
1072 if (END_OF_ARGLIST (oldargtype))
1073 {
1074 error ("%Jprototype for %qD declares more arguments "
1075 "than previous old-style definition", newdecl, newdecl);
1076 return false;
1077 }
1078 else if (END_OF_ARGLIST (newargtype))
1079 {
1080 error ("%Jprototype for %qD declares fewer arguments "
1081 "than previous old-style definition", newdecl, newdecl);
1082 return false;
1083 }
1084
1085 /* Type for passing arg must be consistent with that declared
1086 for the arg. */
1087 else if (!comptypes (oldargtype, newargtype))
1088 {
1089 error ("%Jprototype for %qD declares argument %d"
1090 " with incompatible type",
1091 newdecl, newdecl, i);
1092 return false;
1093 }
1094
1095 oldargs = TREE_CHAIN (oldargs);
1096 newargs = TREE_CHAIN (newargs);
1097 i++;
1098 }
1099
1100 /* If we get here, no errors were found, but do issue a warning
1101 for this poor-style construct. */
1102 warning (0, "%Jprototype for %qD follows non-prototype definition",
1103 newdecl, newdecl);
1104 return true;
1105 #undef END_OF_ARGLIST
1106 }
1107
1108 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1109 first in a pair of mismatched declarations, using the diagnostic
1110 function DIAG. */
1111 static void
1112 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1113 {
1114 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1115 ;
1116 else if (DECL_INITIAL (decl))
1117 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1118 else if (C_DECL_IMPLICIT (decl))
1119 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1120 else
1121 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1122 }
1123
1124 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1125 Returns true if the caller should proceed to merge the two, false
1126 if OLDDECL should simply be discarded. As a side effect, issues
1127 all necessary diagnostics for invalid or poor-style combinations.
1128 If it returns true, writes the types of NEWDECL and OLDDECL to
1129 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1130 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1131
1132 static bool
1133 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1134 tree *newtypep, tree *oldtypep)
1135 {
1136 tree newtype, oldtype;
1137 bool pedwarned = false;
1138 bool warned = false;
1139 bool retval = true;
1140
1141 /* If we have error_mark_node for either decl or type, just discard
1142 the previous decl - we're in an error cascade already. */
1143 if (olddecl == error_mark_node || newdecl == error_mark_node)
1144 return false;
1145 *oldtypep = oldtype = TREE_TYPE (olddecl);
1146 *newtypep = newtype = TREE_TYPE (newdecl);
1147 if (oldtype == error_mark_node || newtype == error_mark_node)
1148 return false;
1149
1150 /* Two different categories of symbol altogether. This is an error
1151 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1152 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1153 {
1154 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1155 && DECL_BUILT_IN (olddecl)
1156 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1157 {
1158 error ("%J%qD redeclared as different kind of symbol",
1159 newdecl, newdecl);
1160 locate_old_decl (olddecl, error);
1161 }
1162 else if (TREE_PUBLIC (newdecl))
1163 warning (0, "%Jbuilt-in function %qD declared as non-function",
1164 newdecl, newdecl);
1165 else if (warn_shadow)
1166 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1167 newdecl, newdecl);
1168 return false;
1169 }
1170
1171 /* Enumerators have no linkage, so may only be declared once in a
1172 given scope. */
1173 if (TREE_CODE (olddecl) == CONST_DECL)
1174 {
1175 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1176 locate_old_decl (olddecl, error);
1177 return false;
1178 }
1179
1180 if (!comptypes (oldtype, newtype))
1181 {
1182 if (TREE_CODE (olddecl) == FUNCTION_DECL
1183 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1184 {
1185 /* Accept harmless mismatch in function types.
1186 This is for the ffs and fprintf builtins. */
1187 tree trytype = match_builtin_function_types (newtype, oldtype);
1188
1189 if (trytype && comptypes (newtype, trytype))
1190 *oldtypep = oldtype = trytype;
1191 else
1192 {
1193 /* If types don't match for a built-in, throw away the
1194 built-in. No point in calling locate_old_decl here, it
1195 won't print anything. */
1196 warning (0, "%Jconflicting types for built-in function %qD",
1197 newdecl, newdecl);
1198 return false;
1199 }
1200 }
1201 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1202 && DECL_IS_BUILTIN (olddecl))
1203 {
1204 /* A conflicting function declaration for a predeclared
1205 function that isn't actually built in. Objective C uses
1206 these. The new declaration silently overrides everything
1207 but the volatility (i.e. noreturn) indication. See also
1208 below. FIXME: Make Objective C use normal builtins. */
1209 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1210 return false;
1211 }
1212 /* Permit void foo (...) to match int foo (...) if the latter is
1213 the definition and implicit int was used. See
1214 c-torture/compile/920625-2.c. */
1215 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1216 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1217 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1218 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1219 {
1220 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1221 /* Make sure we keep void as the return type. */
1222 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1223 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1224 pedwarned = true;
1225 }
1226 /* Permit void foo (...) to match an earlier call to foo (...) with
1227 no declared type (thus, implicitly int). */
1228 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1229 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1230 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1231 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1232 {
1233 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1234 /* Make sure we keep void as the return type. */
1235 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1236 pedwarned = true;
1237 }
1238 else
1239 {
1240 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1241 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1242 else
1243 error ("%Jconflicting types for %qD", newdecl, newdecl);
1244 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1245 locate_old_decl (olddecl, error);
1246 return false;
1247 }
1248 }
1249
1250 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1251 but silently ignore the redeclaration if either is in a system
1252 header. (Conflicting redeclarations were handled above.) */
1253 if (TREE_CODE (newdecl) == TYPE_DECL)
1254 {
1255 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1256 return true; /* Allow OLDDECL to continue in use. */
1257
1258 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1259 locate_old_decl (olddecl, error);
1260 return false;
1261 }
1262
1263 /* Function declarations can either be 'static' or 'extern' (no
1264 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1265 can never conflict with each other on account of linkage (6.2.2p4).
1266 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1267 two definitions if one is 'extern inline' and one is not. The non-
1268 extern-inline definition supersedes the extern-inline definition. */
1269 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1270 {
1271 /* If you declare a built-in function name as static, or
1272 define the built-in with an old-style definition (so we
1273 can't validate the argument list) the built-in definition is
1274 overridden, but optionally warn this was a bad choice of name. */
1275 if (DECL_BUILT_IN (olddecl)
1276 && !C_DECL_DECLARED_BUILTIN (olddecl)
1277 && (!TREE_PUBLIC (newdecl)
1278 || (DECL_INITIAL (newdecl)
1279 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1280 {
1281 if (warn_shadow)
1282 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1283 newdecl, newdecl);
1284 /* Discard the old built-in function. */
1285 return false;
1286 }
1287
1288 if (DECL_INITIAL (newdecl))
1289 {
1290 if (DECL_INITIAL (olddecl))
1291 {
1292 /* If both decls have extern inline and are in the same TU,
1293 reject the new decl. */
1294 if (DECL_DECLARED_INLINE_P (olddecl)
1295 && DECL_EXTERNAL (olddecl)
1296 && DECL_DECLARED_INLINE_P (newdecl)
1297 && DECL_EXTERNAL (newdecl)
1298 && same_translation_unit_p (newdecl, olddecl))
1299 {
1300 error ("%Jredefinition of %qD", newdecl, newdecl);
1301 locate_old_decl (olddecl, error);
1302 return false;
1303 }
1304 /* If both decls have not extern inline, reject the new decl. */
1305 if (!DECL_DECLARED_INLINE_P (olddecl)
1306 && !DECL_EXTERNAL (olddecl)
1307 && !DECL_DECLARED_INLINE_P (newdecl)
1308 && !DECL_EXTERNAL (newdecl))
1309 {
1310 error ("%Jredefinition of %qD", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1312 return false;
1313 }
1314 /* If the new decl is declared as extern inline, error if they are
1315 in the same TU, otherwise retain the old decl. */
1316 if (!DECL_DECLARED_INLINE_P (olddecl)
1317 && !DECL_EXTERNAL (olddecl)
1318 && DECL_DECLARED_INLINE_P (newdecl)
1319 && DECL_EXTERNAL (newdecl))
1320 {
1321 if (same_translation_unit_p (newdecl, olddecl))
1322 {
1323 error ("%Jredefinition of %qD", newdecl, newdecl);
1324 locate_old_decl (olddecl, error);
1325 return false;
1326 }
1327 else
1328 retval = false;
1329 }
1330 }
1331 }
1332 /* If we have a prototype after an old-style function definition,
1333 the argument types must be checked specially. */
1334 else if (DECL_INITIAL (olddecl)
1335 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1336 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1337 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1338 {
1339 locate_old_decl (olddecl, error);
1340 return false;
1341 }
1342 /* A non-static declaration (even an "extern") followed by a
1343 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1344 The same is true for a static forward declaration at block
1345 scope followed by a non-static declaration/definition at file
1346 scope. Static followed by non-static at the same scope is
1347 not undefined behavior, and is the most convenient way to get
1348 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1349 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1350 we do diagnose it if -Wtraditional. */
1351 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1352 {
1353 /* Two exceptions to the rule. If olddecl is an extern
1354 inline, or a predeclared function that isn't actually
1355 built in, newdecl silently overrides olddecl. The latter
1356 occur only in Objective C; see also above. (FIXME: Make
1357 Objective C use normal builtins.) */
1358 if (!DECL_IS_BUILTIN (olddecl)
1359 && !(DECL_EXTERNAL (olddecl)
1360 && DECL_DECLARED_INLINE_P (olddecl)))
1361 {
1362 error ("%Jstatic declaration of %qD follows "
1363 "non-static declaration", newdecl, newdecl);
1364 locate_old_decl (olddecl, error);
1365 }
1366 return false;
1367 }
1368 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1369 {
1370 if (DECL_CONTEXT (olddecl))
1371 {
1372 error ("%Jnon-static declaration of %qD follows "
1373 "static declaration", newdecl, newdecl);
1374 locate_old_decl (olddecl, error);
1375 return false;
1376 }
1377 else if (warn_traditional)
1378 {
1379 warning (0, "%Jnon-static declaration of %qD follows "
1380 "static declaration", newdecl, newdecl);
1381 warned = true;
1382 }
1383 }
1384 }
1385 else if (TREE_CODE (newdecl) == VAR_DECL)
1386 {
1387 /* Only variables can be thread-local, and all declarations must
1388 agree on this property. */
1389 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1390 {
1391 if (DECL_THREAD_LOCAL (newdecl))
1392 error ("%Jthread-local declaration of %qD follows "
1393 "non-thread-local declaration", newdecl, newdecl);
1394 else
1395 error ("%Jnon-thread-local declaration of %qD follows "
1396 "thread-local declaration", newdecl, newdecl);
1397
1398 locate_old_decl (olddecl, error);
1399 return false;
1400 }
1401
1402 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1403 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1404 {
1405 error ("%Jredefinition of %qD", newdecl, newdecl);
1406 locate_old_decl (olddecl, error);
1407 return false;
1408 }
1409
1410 /* Objects declared at file scope: if the first declaration had
1411 external linkage (even if it was an external reference) the
1412 second must have external linkage as well, or the behavior is
1413 undefined. If the first declaration had internal linkage, then
1414 the second must too, or else be an external reference (in which
1415 case the composite declaration still has internal linkage).
1416 As for function declarations, we warn about the static-then-
1417 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1418 if (DECL_FILE_SCOPE_P (newdecl)
1419 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1420 {
1421 if (DECL_EXTERNAL (newdecl))
1422 {
1423 if (!DECL_FILE_SCOPE_P (olddecl))
1424 {
1425 error ("%Jextern declaration of %qD follows "
1426 "declaration with no linkage", newdecl, newdecl);
1427 locate_old_decl (olddecl, error);
1428 return false;
1429 }
1430 else if (warn_traditional)
1431 {
1432 warning (0, "%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl, newdecl);
1434 warned = true;
1435 }
1436 }
1437 else
1438 {
1439 if (TREE_PUBLIC (newdecl))
1440 error ("%Jnon-static declaration of %qD follows "
1441 "static declaration", newdecl, newdecl);
1442 else
1443 error ("%Jstatic declaration of %qD follows "
1444 "non-static declaration", newdecl, newdecl);
1445
1446 locate_old_decl (olddecl, error);
1447 return false;
1448 }
1449 }
1450 /* Two objects with the same name declared at the same block
1451 scope must both be external references (6.7p3). */
1452 else if (!DECL_FILE_SCOPE_P (newdecl))
1453 {
1454 if (DECL_EXTERNAL (newdecl))
1455 {
1456 /* Extern with initializer at block scope, which will
1457 already have received an error. */
1458 }
1459 else if (DECL_EXTERNAL (olddecl))
1460 {
1461 error ("%Jdeclaration of %qD with no linkage follows "
1462 "extern declaration", newdecl, newdecl);
1463 locate_old_decl (olddecl, error);
1464 }
1465 else
1466 {
1467 error ("%Jredeclaration of %qD with no linkage",
1468 newdecl, newdecl);
1469 locate_old_decl (olddecl, error);
1470 }
1471
1472 return false;
1473 }
1474 }
1475
1476 /* warnings */
1477 /* All decls must agree on a visibility. */
1478 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1479 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1480 {
1481 warning (0, "%Jredeclaration of %qD with different visibility "
1482 "(old visibility preserved)", newdecl, newdecl);
1483 warned = true;
1484 }
1485
1486 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1487 {
1488 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1489 if (DECL_DECLARED_INLINE_P (newdecl)
1490 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1491 {
1492 warning (0, "%Jinline declaration of %qD follows "
1493 "declaration with attribute noinline", newdecl, newdecl);
1494 warned = true;
1495 }
1496 else if (DECL_DECLARED_INLINE_P (olddecl)
1497 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1498 {
1499 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1500 "inline declaration ", newdecl, newdecl);
1501 warned = true;
1502 }
1503
1504 /* Inline declaration after use or definition.
1505 ??? Should we still warn about this now we have unit-at-a-time
1506 mode and can get it right?
1507 Definitely don't complain if the decls are in different translation
1508 units. */
1509 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1510 && same_translation_unit_p (olddecl, newdecl))
1511 {
1512 if (TREE_USED (olddecl))
1513 {
1514 warning (0, "%J%qD declared inline after being called",
1515 olddecl, olddecl);
1516 warned = true;
1517 }
1518 else if (DECL_INITIAL (olddecl))
1519 {
1520 warning (0, "%J%qD declared inline after its definition",
1521 olddecl, olddecl);
1522 warned = true;
1523 }
1524 }
1525 }
1526 else /* PARM_DECL, VAR_DECL */
1527 {
1528 /* Redeclaration of a parameter is a constraint violation (this is
1529 not explicitly stated, but follows from C99 6.7p3 [no more than
1530 one declaration of the same identifier with no linkage in the
1531 same scope, except type tags] and 6.2.2p6 [parameters have no
1532 linkage]). We must check for a forward parameter declaration,
1533 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1534 an extension, the mandatory diagnostic for which is handled by
1535 mark_forward_parm_decls. */
1536
1537 if (TREE_CODE (newdecl) == PARM_DECL
1538 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1539 {
1540 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1541 locate_old_decl (olddecl, error);
1542 return false;
1543 }
1544 }
1545
1546 /* Optional warning for completely redundant decls. */
1547 if (!warned && !pedwarned
1548 && warn_redundant_decls
1549 /* Don't warn about a function declaration followed by a
1550 definition. */
1551 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1552 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1553 /* Don't warn about redundant redeclarations of builtins. */
1554 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1555 && !DECL_BUILT_IN (newdecl)
1556 && DECL_BUILT_IN (olddecl)
1557 && !C_DECL_DECLARED_BUILTIN (olddecl))
1558 /* Don't warn about an extern followed by a definition. */
1559 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1560 /* Don't warn about forward parameter decls. */
1561 && !(TREE_CODE (newdecl) == PARM_DECL
1562 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1563 {
1564 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1565 warned = true;
1566 }
1567
1568 /* Report location of previous decl/defn in a consistent manner. */
1569 if (warned || pedwarned)
1570 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1571
1572 return retval;
1573 }
1574
1575 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1576 consistent with OLDDECL, but carries new information. Merge the
1577 new information into OLDDECL. This function issues no
1578 diagnostics. */
1579
1580 static void
1581 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1582 {
1583 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1584 && DECL_INITIAL (newdecl) != 0);
1585 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1586 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1587 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1588 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1589
1590 /* For real parm decl following a forward decl, rechain the old decl
1591 in its new location and clear TREE_ASM_WRITTEN (it's not a
1592 forward decl anymore). */
1593 if (TREE_CODE (newdecl) == PARM_DECL
1594 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1595 {
1596 struct c_binding *b, **here;
1597
1598 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1599 if ((*here)->decl == olddecl)
1600 goto found;
1601 gcc_unreachable ();
1602
1603 found:
1604 b = *here;
1605 *here = b->prev;
1606 b->prev = current_scope->bindings;
1607 current_scope->bindings = b;
1608
1609 TREE_ASM_WRITTEN (olddecl) = 0;
1610 }
1611
1612 DECL_ATTRIBUTES (newdecl)
1613 = targetm.merge_decl_attributes (olddecl, newdecl);
1614
1615 /* Merge the data types specified in the two decls. */
1616 TREE_TYPE (newdecl)
1617 = TREE_TYPE (olddecl)
1618 = composite_type (newtype, oldtype);
1619
1620 /* Lay the type out, unless already done. */
1621 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1622 {
1623 if (TREE_TYPE (newdecl) != error_mark_node)
1624 layout_type (TREE_TYPE (newdecl));
1625 if (TREE_CODE (newdecl) != FUNCTION_DECL
1626 && TREE_CODE (newdecl) != TYPE_DECL
1627 && TREE_CODE (newdecl) != CONST_DECL)
1628 layout_decl (newdecl, 0);
1629 }
1630 else
1631 {
1632 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1633 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1634 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1635 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1636 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1637 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1638 {
1639 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1640 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1641 }
1642 }
1643
1644 /* Keep the old rtl since we can safely use it. */
1645 COPY_DECL_RTL (olddecl, newdecl);
1646
1647 /* Merge the type qualifiers. */
1648 if (TREE_READONLY (newdecl))
1649 TREE_READONLY (olddecl) = 1;
1650
1651 if (TREE_THIS_VOLATILE (newdecl))
1652 {
1653 TREE_THIS_VOLATILE (olddecl) = 1;
1654 if (TREE_CODE (newdecl) == VAR_DECL)
1655 make_var_volatile (newdecl);
1656 }
1657
1658 /* Merge deprecatedness. */
1659 if (TREE_DEPRECATED (newdecl))
1660 TREE_DEPRECATED (olddecl) = 1;
1661
1662 /* Keep source location of definition rather than declaration and of
1663 prototype rather than non-prototype unless that prototype is
1664 built-in. */
1665 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1666 || (old_is_prototype && !new_is_prototype
1667 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1668 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1669
1670 /* Merge the unused-warning information. */
1671 if (DECL_IN_SYSTEM_HEADER (olddecl))
1672 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1673 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1674 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1675
1676 /* Merge the initialization information. */
1677 if (DECL_INITIAL (newdecl) == 0)
1678 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1679
1680 /* Merge the section attribute.
1681 We want to issue an error if the sections conflict but that must be
1682 done later in decl_attributes since we are called before attributes
1683 are assigned. */
1684 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1685 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1686
1687 /* Copy the assembler name.
1688 Currently, it can only be defined in the prototype. */
1689 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1690
1691 /* Use visibility of whichever declaration had it specified */
1692 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1693 {
1694 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1695 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1696 }
1697
1698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1699 {
1700 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1701 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1702 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1703 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1704 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1705 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1706 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1707 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1708 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1709 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1710 }
1711
1712 /* Merge the storage class information. */
1713 merge_weak (newdecl, olddecl);
1714
1715 /* For functions, static overrides non-static. */
1716 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1717 {
1718 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1719 /* This is since we don't automatically
1720 copy the attributes of NEWDECL into OLDDECL. */
1721 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1722 /* If this clears `static', clear it in the identifier too. */
1723 if (!TREE_PUBLIC (olddecl))
1724 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1725 }
1726 if (DECL_EXTERNAL (newdecl))
1727 {
1728 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1729 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1730
1731 /* An extern decl does not override previous storage class. */
1732 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1733 if (!DECL_EXTERNAL (newdecl))
1734 {
1735 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1736 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1737 }
1738 }
1739 else
1740 {
1741 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1742 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1743 }
1744
1745 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1746 {
1747 /* If we're redefining a function previously defined as extern
1748 inline, make sure we emit debug info for the inline before we
1749 throw it away, in case it was inlined into a function that hasn't
1750 been written out yet. */
1751 if (new_is_definition && DECL_INITIAL (olddecl))
1752 {
1753 if (TREE_USED (olddecl)
1754 /* In unit-at-a-time mode we never inline re-defined extern
1755 inline functions. */
1756 && !flag_unit_at_a_time
1757 && cgraph_function_possibly_inlined_p (olddecl))
1758 (*debug_hooks->outlining_inline_function) (olddecl);
1759
1760 /* The new defn must not be inline. */
1761 DECL_INLINE (newdecl) = 0;
1762 DECL_UNINLINABLE (newdecl) = 1;
1763 }
1764 else
1765 {
1766 /* If either decl says `inline', this fn is inline,
1767 unless its definition was passed already. */
1768 if (DECL_DECLARED_INLINE_P (newdecl)
1769 || DECL_DECLARED_INLINE_P (olddecl))
1770 DECL_DECLARED_INLINE_P (newdecl) = 1;
1771
1772 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1773 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1774 }
1775
1776 if (DECL_BUILT_IN (olddecl))
1777 {
1778 /* If redeclaring a builtin function, it stays built in.
1779 But it gets tagged as having been declared. */
1780 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1781 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1782 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1783 if (new_is_prototype)
1784 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1785 else
1786 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1787 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1788 }
1789
1790 /* Also preserve various other info from the definition. */
1791 if (!new_is_definition)
1792 {
1793 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1794 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1795 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1796 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1797 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1798
1799 /* Set DECL_INLINE on the declaration if we've got a body
1800 from which to instantiate. */
1801 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1802 {
1803 DECL_INLINE (newdecl) = 1;
1804 DECL_ABSTRACT_ORIGIN (newdecl)
1805 = DECL_ABSTRACT_ORIGIN (olddecl);
1806 }
1807 }
1808 else
1809 {
1810 /* If a previous declaration said inline, mark the
1811 definition as inlinable. */
1812 if (DECL_DECLARED_INLINE_P (newdecl)
1813 && !DECL_UNINLINABLE (newdecl))
1814 DECL_INLINE (newdecl) = 1;
1815 }
1816 }
1817
1818 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1819 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1820 {
1821 unsigned olddecl_uid = DECL_UID (olddecl);
1822 tree olddecl_context = DECL_CONTEXT (olddecl);
1823
1824 memcpy ((char *) olddecl + sizeof (struct tree_common),
1825 (char *) newdecl + sizeof (struct tree_common),
1826 sizeof (struct tree_decl) - sizeof (struct tree_common));
1827 DECL_UID (olddecl) = olddecl_uid;
1828 DECL_CONTEXT (olddecl) = olddecl_context;
1829 }
1830
1831 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1832 so that encode_section_info has a chance to look at the new decl
1833 flags and attributes. */
1834 if (DECL_RTL_SET_P (olddecl)
1835 && (TREE_CODE (olddecl) == FUNCTION_DECL
1836 || (TREE_CODE (olddecl) == VAR_DECL
1837 && TREE_STATIC (olddecl))))
1838 make_decl_rtl (olddecl);
1839 }
1840
1841 /* Handle when a new declaration NEWDECL has the same name as an old
1842 one OLDDECL in the same binding contour. Prints an error message
1843 if appropriate.
1844
1845 If safely possible, alter OLDDECL to look like NEWDECL, and return
1846 true. Otherwise, return false. */
1847
1848 static bool
1849 duplicate_decls (tree newdecl, tree olddecl)
1850 {
1851 tree newtype = NULL, oldtype = NULL;
1852
1853 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1854 return false;
1855
1856 merge_decls (newdecl, olddecl, newtype, oldtype);
1857 return true;
1858 }
1859
1860 \f
1861 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1862 static void
1863 warn_if_shadowing (tree new_decl)
1864 {
1865 struct c_binding *b;
1866
1867 /* Shadow warnings wanted? */
1868 if (!warn_shadow
1869 /* No shadow warnings for internally generated vars. */
1870 || DECL_IS_BUILTIN (new_decl)
1871 /* No shadow warnings for vars made for inlining. */
1872 || DECL_FROM_INLINE (new_decl)
1873 /* Don't warn about the parm names in function declarator
1874 within a function declarator. It would be nice to avoid
1875 warning in any function declarator in a declaration, as
1876 opposed to a definition, but there is no way to tell
1877 it's not a definition at this point. */
1878 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1879 return;
1880
1881 /* Is anything being shadowed? Invisible decls do not count. */
1882 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1883 if (b->decl && b->decl != new_decl && !b->invisible)
1884 {
1885 tree old_decl = b->decl;
1886
1887 if (old_decl == error_mark_node)
1888 {
1889 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1890 new_decl, new_decl);
1891 break;
1892 }
1893 else if (TREE_CODE (old_decl) == PARM_DECL)
1894 warning (0, "%Jdeclaration of %qD shadows a parameter",
1895 new_decl, new_decl);
1896 else if (DECL_FILE_SCOPE_P (old_decl))
1897 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1898 new_decl, new_decl);
1899 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1900 && DECL_BUILT_IN (old_decl))
1901 {
1902 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1903 new_decl, new_decl);
1904 break;
1905 }
1906 else
1907 warning (0, "%Jdeclaration of %qD shadows a previous local",
1908 new_decl, new_decl);
1909
1910 warning (0, "%Jshadowed declaration is here", old_decl);
1911
1912 break;
1913 }
1914 }
1915
1916
1917 /* Subroutine of pushdecl.
1918
1919 X is a TYPE_DECL for a typedef statement. Create a brand new
1920 ..._TYPE node (which will be just a variant of the existing
1921 ..._TYPE node with identical properties) and then install X
1922 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1923
1924 The whole point here is to end up with a situation where each
1925 and every ..._TYPE node the compiler creates will be uniquely
1926 associated with AT MOST one node representing a typedef name.
1927 This way, even though the compiler substitutes corresponding
1928 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1929 early on, later parts of the compiler can always do the reverse
1930 translation and get back the corresponding typedef name. For
1931 example, given:
1932
1933 typedef struct S MY_TYPE;
1934 MY_TYPE object;
1935
1936 Later parts of the compiler might only know that `object' was of
1937 type `struct S' if it were not for code just below. With this
1938 code however, later parts of the compiler see something like:
1939
1940 struct S' == struct S
1941 typedef struct S' MY_TYPE;
1942 struct S' object;
1943
1944 And they can then deduce (from the node for type struct S') that
1945 the original object declaration was:
1946
1947 MY_TYPE object;
1948
1949 Being able to do this is important for proper support of protoize,
1950 and also for generating precise symbolic debugging information
1951 which takes full account of the programmer's (typedef) vocabulary.
1952
1953 Obviously, we don't want to generate a duplicate ..._TYPE node if
1954 the TYPE_DECL node that we are now processing really represents a
1955 standard built-in type.
1956
1957 Since all standard types are effectively declared at line zero
1958 in the source file, we can easily check to see if we are working
1959 on a standard type by checking the current value of lineno. */
1960
1961 static void
1962 clone_underlying_type (tree x)
1963 {
1964 if (DECL_IS_BUILTIN (x))
1965 {
1966 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1967 TYPE_NAME (TREE_TYPE (x)) = x;
1968 }
1969 else if (TREE_TYPE (x) != error_mark_node
1970 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1971 {
1972 tree tt = TREE_TYPE (x);
1973 DECL_ORIGINAL_TYPE (x) = tt;
1974 tt = build_variant_type_copy (tt);
1975 TYPE_NAME (tt) = x;
1976 TREE_USED (tt) = TREE_USED (x);
1977 TREE_TYPE (x) = tt;
1978 }
1979 }
1980
1981 /* Record a decl-node X as belonging to the current lexical scope.
1982 Check for errors (such as an incompatible declaration for the same
1983 name already seen in the same scope).
1984
1985 Returns either X or an old decl for the same name.
1986 If an old decl is returned, it may have been smashed
1987 to agree with what X says. */
1988
1989 tree
1990 pushdecl (tree x)
1991 {
1992 tree name = DECL_NAME (x);
1993 struct c_scope *scope = current_scope;
1994 struct c_binding *b;
1995 bool nested = false;
1996
1997 /* Functions need the lang_decl data. */
1998 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1999 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2000
2001 /* Must set DECL_CONTEXT for everything not at file scope or
2002 DECL_FILE_SCOPE_P won't work. Local externs don't count
2003 unless they have initializers (which generate code). */
2004 if (current_function_decl
2005 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2006 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2007 DECL_CONTEXT (x) = current_function_decl;
2008
2009 /* If this is of variably modified type, prevent jumping into its
2010 scope. */
2011 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2012 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2013 c_begin_vm_scope (scope->depth);
2014
2015 /* Anonymous decls are just inserted in the scope. */
2016 if (!name)
2017 {
2018 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2019 return x;
2020 }
2021
2022 /* First, see if there is another declaration with the same name in
2023 the current scope. If there is, duplicate_decls may do all the
2024 work for us. If duplicate_decls returns false, that indicates
2025 two incompatible decls in the same scope; we are to silently
2026 replace the old one (duplicate_decls has issued all appropriate
2027 diagnostics). In particular, we should not consider possible
2028 duplicates in the external scope, or shadowing. */
2029 b = I_SYMBOL_BINDING (name);
2030 if (b && B_IN_SCOPE (b, scope))
2031 {
2032 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2033 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2034 b->inner_comp = false;
2035 if (duplicate_decls (x, b->decl))
2036 return b->decl;
2037 else
2038 goto skip_external_and_shadow_checks;
2039 }
2040
2041 /* All declarations with external linkage, and all external
2042 references, go in the external scope, no matter what scope is
2043 current. However, the binding in that scope is ignored for
2044 purposes of normal name lookup. A separate binding structure is
2045 created in the requested scope; this governs the normal
2046 visibility of the symbol.
2047
2048 The binding in the externals scope is used exclusively for
2049 detecting duplicate declarations of the same object, no matter
2050 what scope they are in; this is what we do here. (C99 6.2.7p2:
2051 All declarations that refer to the same object or function shall
2052 have compatible type; otherwise, the behavior is undefined.) */
2053 if (DECL_EXTERNAL (x) || scope == file_scope)
2054 {
2055 tree type = TREE_TYPE (x);
2056 tree vistype = 0;
2057 tree visdecl = 0;
2058 bool type_saved = false;
2059 if (b && !B_IN_EXTERNAL_SCOPE (b)
2060 && (TREE_CODE (b->decl) == FUNCTION_DECL
2061 || TREE_CODE (b->decl) == VAR_DECL)
2062 && DECL_FILE_SCOPE_P (b->decl))
2063 {
2064 visdecl = b->decl;
2065 vistype = TREE_TYPE (visdecl);
2066 }
2067 if (warn_nested_externs
2068 && scope != file_scope
2069 && !DECL_IN_SYSTEM_HEADER (x))
2070 warning (0, "nested extern declaration of %qD", x);
2071
2072 while (b && !B_IN_EXTERNAL_SCOPE (b))
2073 {
2074 /* If this decl might be modified, save its type. This is
2075 done here rather than when the decl is first bound
2076 because the type may change after first binding, through
2077 being completed or through attributes being added. If we
2078 encounter multiple such decls, only the first should have
2079 its type saved; the others will already have had their
2080 proper types saved and the types will not have changed as
2081 their scopes will not have been re-entered. */
2082 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2083 {
2084 b->type = TREE_TYPE (b->decl);
2085 type_saved = true;
2086 }
2087 if (B_IN_FILE_SCOPE (b)
2088 && TREE_CODE (b->decl) == VAR_DECL
2089 && TREE_STATIC (b->decl)
2090 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2091 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2092 && TREE_CODE (type) == ARRAY_TYPE
2093 && TYPE_DOMAIN (type)
2094 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2095 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2096 {
2097 /* Array type completed in inner scope, which should be
2098 diagnosed if the completion does not have size 1 and
2099 it does not get completed in the file scope. */
2100 b->inner_comp = true;
2101 }
2102 b = b->shadowed;
2103 }
2104
2105 /* If a matching external declaration has been found, set its
2106 type to the composite of all the types of that declaration.
2107 After the consistency checks, it will be reset to the
2108 composite of the visible types only. */
2109 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2110 && b->type)
2111 TREE_TYPE (b->decl) = b->type;
2112
2113 /* The point of the same_translation_unit_p check here is,
2114 we want to detect a duplicate decl for a construct like
2115 foo() { extern bar(); } ... static bar(); but not if
2116 they are in different translation units. In any case,
2117 the static does not go in the externals scope. */
2118 if (b
2119 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2120 && duplicate_decls (x, b->decl))
2121 {
2122 tree thistype;
2123 thistype = (vistype ? composite_type (vistype, type) : type);
2124 b->type = TREE_TYPE (b->decl);
2125 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2126 thistype
2127 = build_type_attribute_variant (thistype,
2128 TYPE_ATTRIBUTES (b->type));
2129 TREE_TYPE (b->decl) = thistype;
2130 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2131 return b->decl;
2132 }
2133 else if (TREE_PUBLIC (x))
2134 {
2135 if (visdecl && !b && duplicate_decls (x, visdecl))
2136 {
2137 /* An external declaration at block scope referring to a
2138 visible entity with internal linkage. The composite
2139 type will already be correct for this scope, so we
2140 just need to fall through to make the declaration in
2141 this scope. */
2142 nested = true;
2143 x = visdecl;
2144 }
2145 else
2146 {
2147 bind (name, x, external_scope, /*invisible=*/true,
2148 /*nested=*/false);
2149 nested = true;
2150 }
2151 }
2152 }
2153
2154 warn_if_shadowing (x);
2155
2156 skip_external_and_shadow_checks:
2157 if (TREE_CODE (x) == TYPE_DECL)
2158 clone_underlying_type (x);
2159
2160 bind (name, x, scope, /*invisible=*/false, nested);
2161
2162 /* If x's type is incomplete because it's based on a
2163 structure or union which has not yet been fully declared,
2164 attach it to that structure or union type, so we can go
2165 back and complete the variable declaration later, if the
2166 structure or union gets fully declared.
2167
2168 If the input is erroneous, we can have error_mark in the type
2169 slot (e.g. "f(void a, ...)") - that doesn't count as an
2170 incomplete type. */
2171 if (TREE_TYPE (x) != error_mark_node
2172 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2173 {
2174 tree element = TREE_TYPE (x);
2175
2176 while (TREE_CODE (element) == ARRAY_TYPE)
2177 element = TREE_TYPE (element);
2178 element = TYPE_MAIN_VARIANT (element);
2179
2180 if ((TREE_CODE (element) == RECORD_TYPE
2181 || TREE_CODE (element) == UNION_TYPE)
2182 && (TREE_CODE (x) != TYPE_DECL
2183 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2184 && !COMPLETE_TYPE_P (element))
2185 C_TYPE_INCOMPLETE_VARS (element)
2186 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2187 }
2188 return x;
2189 }
2190
2191 /* Record X as belonging to file scope.
2192 This is used only internally by the Objective-C front end,
2193 and is limited to its needs. duplicate_decls is not called;
2194 if there is any preexisting decl for this identifier, it is an ICE. */
2195
2196 tree
2197 pushdecl_top_level (tree x)
2198 {
2199 tree name;
2200 bool nested = false;
2201 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2202
2203 name = DECL_NAME (x);
2204
2205 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2206
2207 if (TREE_PUBLIC (x))
2208 {
2209 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2210 nested = true;
2211 }
2212 if (file_scope)
2213 bind (name, x, file_scope, /*invisible=*/false, nested);
2214
2215 return x;
2216 }
2217 \f
2218 static void
2219 implicit_decl_warning (tree id, tree olddecl)
2220 {
2221 void (*diag) (const char *, ...);
2222 switch (mesg_implicit_function_declaration)
2223 {
2224 case 0: return;
2225 case 1: diag = warning0; break;
2226 case 2: diag = error; break;
2227 default: gcc_unreachable ();
2228 }
2229
2230 diag (N_("implicit declaration of function %qE"), id);
2231 if (olddecl)
2232 locate_old_decl (olddecl, diag);
2233 }
2234
2235 /* Generate an implicit declaration for identifier FUNCTIONID as a
2236 function of type int (). */
2237
2238 tree
2239 implicitly_declare (tree functionid)
2240 {
2241 struct c_binding *b;
2242 tree decl = 0;
2243 tree asmspec_tree;
2244
2245 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2246 {
2247 if (B_IN_SCOPE (b, external_scope))
2248 {
2249 decl = b->decl;
2250 break;
2251 }
2252 }
2253
2254 if (decl)
2255 {
2256 if (decl == error_mark_node)
2257 return decl;
2258
2259 /* FIXME: Objective-C has weird not-really-builtin functions
2260 which are supposed to be visible automatically. They wind up
2261 in the external scope because they're pushed before the file
2262 scope gets created. Catch this here and rebind them into the
2263 file scope. */
2264 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2265 {
2266 bind (functionid, decl, file_scope,
2267 /*invisible=*/false, /*nested=*/true);
2268 return decl;
2269 }
2270 else
2271 {
2272 tree newtype = default_function_type;
2273 if (b->type)
2274 TREE_TYPE (decl) = b->type;
2275 /* Implicit declaration of a function already declared
2276 (somehow) in a different scope, or as a built-in.
2277 If this is the first time this has happened, warn;
2278 then recycle the old declaration but with the new type. */
2279 if (!C_DECL_IMPLICIT (decl))
2280 {
2281 implicit_decl_warning (functionid, decl);
2282 C_DECL_IMPLICIT (decl) = 1;
2283 }
2284 if (DECL_BUILT_IN (decl))
2285 {
2286 newtype = build_type_attribute_variant (newtype,
2287 TYPE_ATTRIBUTES
2288 (TREE_TYPE (decl)));
2289 if (!comptypes (newtype, TREE_TYPE (decl)))
2290 {
2291 warning (0, "incompatible implicit declaration of built-in"
2292 " function %qD", decl);
2293 newtype = TREE_TYPE (decl);
2294 }
2295 }
2296 else
2297 {
2298 if (!comptypes (newtype, TREE_TYPE (decl)))
2299 {
2300 error ("incompatible implicit declaration of function %qD",
2301 decl);
2302 locate_old_decl (decl, error);
2303 }
2304 }
2305 b->type = TREE_TYPE (decl);
2306 TREE_TYPE (decl) = newtype;
2307 bind (functionid, decl, current_scope,
2308 /*invisible=*/false, /*nested=*/true);
2309 return decl;
2310 }
2311 }
2312
2313 /* Not seen before. */
2314 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2315 DECL_EXTERNAL (decl) = 1;
2316 TREE_PUBLIC (decl) = 1;
2317 C_DECL_IMPLICIT (decl) = 1;
2318 implicit_decl_warning (functionid, 0);
2319 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2320 if (asmspec_tree)
2321 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2322
2323 /* C89 says implicit declarations are in the innermost block.
2324 So we record the decl in the standard fashion. */
2325 decl = pushdecl (decl);
2326
2327 /* No need to call objc_check_decl here - it's a function type. */
2328 rest_of_decl_compilation (decl, 0, 0);
2329
2330 /* Write a record describing this implicit function declaration
2331 to the prototypes file (if requested). */
2332 gen_aux_info_record (decl, 0, 1, 0);
2333
2334 /* Possibly apply some default attributes to this implicit declaration. */
2335 decl_attributes (&decl, NULL_TREE, 0);
2336
2337 return decl;
2338 }
2339
2340 /* Issue an error message for a reference to an undeclared variable
2341 ID, including a reference to a builtin outside of function-call
2342 context. Establish a binding of the identifier to error_mark_node
2343 in an appropriate scope, which will suppress further errors for the
2344 same identifier. The error message should be given location LOC. */
2345 void
2346 undeclared_variable (tree id, location_t loc)
2347 {
2348 static bool already = false;
2349 struct c_scope *scope;
2350
2351 if (current_function_decl == 0)
2352 {
2353 error ("%H%qE undeclared here (not in a function)", &loc, id);
2354 scope = current_scope;
2355 }
2356 else
2357 {
2358 error ("%H%qE undeclared (first use in this function)", &loc, id);
2359
2360 if (!already)
2361 {
2362 error ("%H(Each undeclared identifier is reported only once", &loc);
2363 error ("%Hfor each function it appears in.)", &loc);
2364 already = true;
2365 }
2366
2367 /* If we are parsing old-style parameter decls, current_function_decl
2368 will be nonnull but current_function_scope will be null. */
2369 scope = current_function_scope ? current_function_scope : current_scope;
2370 }
2371 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2372 }
2373 \f
2374 /* Subroutine of lookup_label, declare_label, define_label: construct a
2375 LABEL_DECL with all the proper frills. */
2376
2377 static tree
2378 make_label (tree name, location_t location)
2379 {
2380 tree label = build_decl (LABEL_DECL, name, void_type_node);
2381
2382 DECL_CONTEXT (label) = current_function_decl;
2383 DECL_MODE (label) = VOIDmode;
2384 DECL_SOURCE_LOCATION (label) = location;
2385
2386 return label;
2387 }
2388
2389 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2390 Create one if none exists so far for the current function.
2391 This is called when a label is used in a goto expression or
2392 has its address taken. */
2393
2394 tree
2395 lookup_label (tree name)
2396 {
2397 tree label;
2398
2399 if (current_function_decl == 0)
2400 {
2401 error ("label %qE referenced outside of any function", name);
2402 return 0;
2403 }
2404
2405 /* Use a label already defined or ref'd with this name, but not if
2406 it is inherited from a containing function and wasn't declared
2407 using __label__. */
2408 label = I_LABEL_DECL (name);
2409 if (label && (DECL_CONTEXT (label) == current_function_decl
2410 || C_DECLARED_LABEL_FLAG (label)))
2411 {
2412 /* If the label has only been declared, update its apparent
2413 location to point here, for better diagnostics if it
2414 turns out not to have been defined. */
2415 if (!TREE_USED (label))
2416 DECL_SOURCE_LOCATION (label) = input_location;
2417 return label;
2418 }
2419
2420 /* No label binding for that identifier; make one. */
2421 label = make_label (name, input_location);
2422
2423 /* Ordinary labels go in the current function scope. */
2424 bind (name, label, current_function_scope,
2425 /*invisible=*/false, /*nested=*/false);
2426 return label;
2427 }
2428
2429 /* Make a label named NAME in the current function, shadowing silently
2430 any that may be inherited from containing functions or containing
2431 scopes. This is called for __label__ declarations. */
2432
2433 tree
2434 declare_label (tree name)
2435 {
2436 struct c_binding *b = I_LABEL_BINDING (name);
2437 tree label;
2438
2439 /* Check to make sure that the label hasn't already been declared
2440 at this scope */
2441 if (b && B_IN_CURRENT_SCOPE (b))
2442 {
2443 error ("duplicate label declaration %qE", name);
2444 locate_old_decl (b->decl, error);
2445
2446 /* Just use the previous declaration. */
2447 return b->decl;
2448 }
2449
2450 label = make_label (name, input_location);
2451 C_DECLARED_LABEL_FLAG (label) = 1;
2452
2453 /* Declared labels go in the current scope. */
2454 bind (name, label, current_scope,
2455 /*invisible=*/false, /*nested=*/false);
2456 return label;
2457 }
2458
2459 /* Define a label, specifying the location in the source file.
2460 Return the LABEL_DECL node for the label, if the definition is valid.
2461 Otherwise return 0. */
2462
2463 tree
2464 define_label (location_t location, tree name)
2465 {
2466 /* Find any preexisting label with this name. It is an error
2467 if that label has already been defined in this function, or
2468 if there is a containing function with a declared label with
2469 the same name. */
2470 tree label = I_LABEL_DECL (name);
2471 struct c_label_list *nlist_se, *nlist_vm;
2472
2473 if (label
2474 && ((DECL_CONTEXT (label) == current_function_decl
2475 && DECL_INITIAL (label) != 0)
2476 || (DECL_CONTEXT (label) != current_function_decl
2477 && C_DECLARED_LABEL_FLAG (label))))
2478 {
2479 error ("%Hduplicate label %qD", &location, label);
2480 locate_old_decl (label, error);
2481 return 0;
2482 }
2483 else if (label && DECL_CONTEXT (label) == current_function_decl)
2484 {
2485 /* The label has been used or declared already in this function,
2486 but not defined. Update its location to point to this
2487 definition. */
2488 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2489 error ("%Jjump into statement expression", label);
2490 if (C_DECL_UNDEFINABLE_VM (label))
2491 error ("%Jjump into scope of identifier with variably modified type",
2492 label);
2493 DECL_SOURCE_LOCATION (label) = location;
2494 }
2495 else
2496 {
2497 /* No label binding for that identifier; make one. */
2498 label = make_label (name, location);
2499
2500 /* Ordinary labels go in the current function scope. */
2501 bind (name, label, current_function_scope,
2502 /*invisible=*/false, /*nested=*/false);
2503 }
2504
2505 if (warn_traditional && !in_system_header && lookup_name (name))
2506 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2507 "identifier %qE conflicts", &location, name);
2508
2509 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2510 nlist_se->next = label_context_stack_se->labels_def;
2511 nlist_se->label = label;
2512 label_context_stack_se->labels_def = nlist_se;
2513
2514 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2515 nlist_vm->next = label_context_stack_vm->labels_def;
2516 nlist_vm->label = label;
2517 label_context_stack_vm->labels_def = nlist_vm;
2518
2519 /* Mark label as having been defined. */
2520 DECL_INITIAL (label) = error_mark_node;
2521 return label;
2522 }
2523 \f
2524 /* Given NAME, an IDENTIFIER_NODE,
2525 return the structure (or union or enum) definition for that name.
2526 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2527 CODE says which kind of type the caller wants;
2528 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2529 If the wrong kind of type is found, an error is reported. */
2530
2531 static tree
2532 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2533 {
2534 struct c_binding *b = I_TAG_BINDING (name);
2535 int thislevel = 0;
2536
2537 if (!b || !b->decl)
2538 return 0;
2539
2540 /* We only care about whether it's in this level if
2541 thislevel_only was set or it might be a type clash. */
2542 if (thislevel_only || TREE_CODE (b->decl) != code)
2543 {
2544 /* For our purposes, a tag in the external scope is the same as
2545 a tag in the file scope. (Primarily relevant to Objective-C
2546 and its builtin structure tags, which get pushed before the
2547 file scope is created.) */
2548 if (B_IN_CURRENT_SCOPE (b)
2549 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2550 thislevel = 1;
2551 }
2552
2553 if (thislevel_only && !thislevel)
2554 return 0;
2555
2556 if (TREE_CODE (b->decl) != code)
2557 {
2558 /* Definition isn't the kind we were looking for. */
2559 pending_invalid_xref = name;
2560 pending_invalid_xref_location = input_location;
2561
2562 /* If in the same binding level as a declaration as a tag
2563 of a different type, this must not be allowed to
2564 shadow that tag, so give the error immediately.
2565 (For example, "struct foo; union foo;" is invalid.) */
2566 if (thislevel)
2567 pending_xref_error ();
2568 }
2569 return b->decl;
2570 }
2571
2572 /* Print an error message now
2573 for a recent invalid struct, union or enum cross reference.
2574 We don't print them immediately because they are not invalid
2575 when used in the `struct foo;' construct for shadowing. */
2576
2577 void
2578 pending_xref_error (void)
2579 {
2580 if (pending_invalid_xref != 0)
2581 error ("%H%qE defined as wrong kind of tag",
2582 &pending_invalid_xref_location, pending_invalid_xref);
2583 pending_invalid_xref = 0;
2584 }
2585
2586 \f
2587 /* Look up NAME in the current scope and its superiors
2588 in the namespace of variables, functions and typedefs.
2589 Return a ..._DECL node of some kind representing its definition,
2590 or return 0 if it is undefined. */
2591
2592 tree
2593 lookup_name (tree name)
2594 {
2595 struct c_binding *b = I_SYMBOL_BINDING (name);
2596 if (b && !b->invisible)
2597 return b->decl;
2598 return 0;
2599 }
2600
2601 /* Similar to `lookup_name' but look only at the indicated scope. */
2602
2603 static tree
2604 lookup_name_in_scope (tree name, struct c_scope *scope)
2605 {
2606 struct c_binding *b;
2607
2608 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2609 if (B_IN_SCOPE (b, scope))
2610 return b->decl;
2611 return 0;
2612 }
2613 \f
2614 /* Create the predefined scalar types of C,
2615 and some nodes representing standard constants (0, 1, (void *) 0).
2616 Initialize the global scope.
2617 Make definitions for built-in primitive functions. */
2618
2619 void
2620 c_init_decl_processing (void)
2621 {
2622 location_t save_loc = input_location;
2623
2624 /* Initialize reserved words for parser. */
2625 c_parse_init ();
2626
2627 current_function_decl = 0;
2628
2629 gcc_obstack_init (&parser_obstack);
2630
2631 /* Make the externals scope. */
2632 push_scope ();
2633 external_scope = current_scope;
2634
2635 /* Declarations from c_common_nodes_and_builtins must not be associated
2636 with this input file, lest we get differences between using and not
2637 using preprocessed headers. */
2638 #ifdef USE_MAPPED_LOCATION
2639 input_location = BUILTINS_LOCATION;
2640 #else
2641 input_location.file = "<built-in>";
2642 input_location.line = 0;
2643 #endif
2644
2645 build_common_tree_nodes (flag_signed_char, false);
2646
2647 c_common_nodes_and_builtins ();
2648
2649 /* In C, comparisons and TRUTH_* expressions have type int. */
2650 truthvalue_type_node = integer_type_node;
2651 truthvalue_true_node = integer_one_node;
2652 truthvalue_false_node = integer_zero_node;
2653
2654 /* Even in C99, which has a real boolean type. */
2655 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2656 boolean_type_node));
2657
2658 input_location = save_loc;
2659
2660 pedantic_lvalues = true;
2661
2662 make_fname_decl = c_make_fname_decl;
2663 start_fname_decls ();
2664 }
2665
2666 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2667 decl, NAME is the initialization string and TYPE_DEP indicates whether
2668 NAME depended on the type of the function. As we don't yet implement
2669 delayed emission of static data, we mark the decl as emitted
2670 so it is not placed in the output. Anything using it must therefore pull
2671 out the STRING_CST initializer directly. FIXME. */
2672
2673 static tree
2674 c_make_fname_decl (tree id, int type_dep)
2675 {
2676 const char *name = fname_as_string (type_dep);
2677 tree decl, type, init;
2678 size_t length = strlen (name);
2679
2680 type = build_array_type (char_type_node,
2681 build_index_type (size_int (length)));
2682 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2683
2684 decl = build_decl (VAR_DECL, id, type);
2685
2686 TREE_STATIC (decl) = 1;
2687 TREE_READONLY (decl) = 1;
2688 DECL_ARTIFICIAL (decl) = 1;
2689
2690 init = build_string (length + 1, name);
2691 free ((char *) name);
2692 TREE_TYPE (init) = type;
2693 DECL_INITIAL (decl) = init;
2694
2695 TREE_USED (decl) = 1;
2696
2697 if (current_function_decl)
2698 {
2699 DECL_CONTEXT (decl) = current_function_decl;
2700 bind (id, decl, current_function_scope,
2701 /*invisible=*/false, /*nested=*/false);
2702 }
2703
2704 finish_decl (decl, init, NULL_TREE);
2705
2706 return decl;
2707 }
2708
2709 /* Return a definition for a builtin function named NAME and whose data type
2710 is TYPE. TYPE should be a function type with argument types.
2711 FUNCTION_CODE tells later passes how to compile calls to this function.
2712 See tree.h for its possible values.
2713
2714 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2715 the name to be called if we can't opencode the function. If
2716 ATTRS is nonzero, use that for the function's attribute list. */
2717
2718 tree
2719 builtin_function (const char *name, tree type, int function_code,
2720 enum built_in_class cl, const char *library_name,
2721 tree attrs)
2722 {
2723 tree id = get_identifier (name);
2724 tree decl = build_decl (FUNCTION_DECL, id, type);
2725 TREE_PUBLIC (decl) = 1;
2726 DECL_EXTERNAL (decl) = 1;
2727 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2728 DECL_BUILT_IN_CLASS (decl) = cl;
2729 DECL_FUNCTION_CODE (decl) = function_code;
2730 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2731 if (library_name)
2732 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2733
2734 /* Should never be called on a symbol with a preexisting meaning. */
2735 gcc_assert (!I_SYMBOL_BINDING (id));
2736
2737 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2738
2739 /* Builtins in the implementation namespace are made visible without
2740 needing to be explicitly declared. See push_file_scope. */
2741 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2742 {
2743 TREE_CHAIN (decl) = visible_builtins;
2744 visible_builtins = decl;
2745 }
2746
2747 /* Possibly apply some default attributes to this built-in function. */
2748 if (attrs)
2749 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2750 else
2751 decl_attributes (&decl, NULL_TREE, 0);
2752
2753 return decl;
2754 }
2755 \f
2756 /* Called when a declaration is seen that contains no names to declare.
2757 If its type is a reference to a structure, union or enum inherited
2758 from a containing scope, shadow that tag name for the current scope
2759 with a forward reference.
2760 If its type defines a new named structure or union
2761 or defines an enum, it is valid but we need not do anything here.
2762 Otherwise, it is an error. */
2763
2764 void
2765 shadow_tag (const struct c_declspecs *declspecs)
2766 {
2767 shadow_tag_warned (declspecs, 0);
2768 }
2769
2770 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2771 but no pedwarn. */
2772 void
2773 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2774 {
2775 bool found_tag = false;
2776
2777 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2778 {
2779 tree value = declspecs->type;
2780 enum tree_code code = TREE_CODE (value);
2781
2782 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2783 /* Used to test also that TYPE_SIZE (value) != 0.
2784 That caused warning for `struct foo;' at top level in the file. */
2785 {
2786 tree name = TYPE_NAME (value);
2787 tree t;
2788
2789 found_tag = true;
2790
2791 if (name == 0)
2792 {
2793 if (warned != 1 && code != ENUMERAL_TYPE)
2794 /* Empty unnamed enum OK */
2795 {
2796 pedwarn ("unnamed struct/union that defines no instances");
2797 warned = 1;
2798 }
2799 }
2800 else if (!declspecs->tag_defined_p
2801 && declspecs->storage_class != csc_none)
2802 {
2803 if (warned != 1)
2804 pedwarn ("empty declaration with storage class specifier "
2805 "does not redeclare tag");
2806 warned = 1;
2807 pending_xref_error ();
2808 }
2809 else if (!declspecs->tag_defined_p
2810 && (declspecs->const_p
2811 || declspecs->volatile_p
2812 || declspecs->restrict_p))
2813 {
2814 if (warned != 1)
2815 pedwarn ("empty declaration with type qualifier "
2816 "does not redeclare tag");
2817 warned = 1;
2818 pending_xref_error ();
2819 }
2820 else
2821 {
2822 pending_invalid_xref = 0;
2823 t = lookup_tag (code, name, 1);
2824
2825 if (t == 0)
2826 {
2827 t = make_node (code);
2828 pushtag (name, t);
2829 }
2830 }
2831 }
2832 else
2833 {
2834 if (warned != 1 && !in_system_header)
2835 {
2836 pedwarn ("useless type name in empty declaration");
2837 warned = 1;
2838 }
2839 }
2840 }
2841 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2842 {
2843 pedwarn ("useless type name in empty declaration");
2844 warned = 1;
2845 }
2846
2847 pending_invalid_xref = 0;
2848
2849 if (declspecs->inline_p)
2850 {
2851 error ("%<inline%> in empty declaration");
2852 warned = 1;
2853 }
2854
2855 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2856 {
2857 error ("%<auto%> in file-scope empty declaration");
2858 warned = 1;
2859 }
2860
2861 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2862 {
2863 error ("%<register%> in file-scope empty declaration");
2864 warned = 1;
2865 }
2866
2867 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2868 {
2869 warning (0, "useless storage class specifier in empty declaration");
2870 warned = 2;
2871 }
2872
2873 if (!warned && !in_system_header && declspecs->thread_p)
2874 {
2875 warning (0, "useless %<__thread%> in empty declaration");
2876 warned = 2;
2877 }
2878
2879 if (!warned && !in_system_header && (declspecs->const_p
2880 || declspecs->volatile_p
2881 || declspecs->restrict_p))
2882 {
2883 warning (0, "useless type qualifier in empty declaration");
2884 warned = 2;
2885 }
2886
2887 if (warned != 1)
2888 {
2889 if (!found_tag)
2890 pedwarn ("empty declaration");
2891 }
2892 }
2893 \f
2894
2895 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2896 bits. SPECS represents declaration specifiers that the grammar
2897 only permits to contain type qualifiers and attributes. */
2898
2899 int
2900 quals_from_declspecs (const struct c_declspecs *specs)
2901 {
2902 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2903 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2904 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2905 gcc_assert (!specs->type
2906 && !specs->decl_attr
2907 && specs->typespec_word == cts_none
2908 && specs->storage_class == csc_none
2909 && !specs->typedef_p
2910 && !specs->explicit_signed_p
2911 && !specs->deprecated_p
2912 && !specs->long_p
2913 && !specs->long_long_p
2914 && !specs->short_p
2915 && !specs->signed_p
2916 && !specs->unsigned_p
2917 && !specs->complex_p
2918 && !specs->inline_p
2919 && !specs->thread_p);
2920 return quals;
2921 }
2922
2923 /* Construct an array declarator. EXPR is the expression inside [], or
2924 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2925 to the pointer to which a parameter array is converted). STATIC_P is
2926 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2927 is true if the array is [*], a VLA of unspecified length which is
2928 nevertheless a complete type (not currently implemented by GCC),
2929 false otherwise. The field for the contained declarator is left to be
2930 filled in by set_array_declarator_inner. */
2931
2932 struct c_declarator *
2933 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2934 bool vla_unspec_p)
2935 {
2936 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2937 struct c_declarator);
2938 declarator->kind = cdk_array;
2939 declarator->declarator = 0;
2940 declarator->u.array.dimen = expr;
2941 if (quals)
2942 {
2943 declarator->u.array.attrs = quals->attrs;
2944 declarator->u.array.quals = quals_from_declspecs (quals);
2945 }
2946 else
2947 {
2948 declarator->u.array.attrs = NULL_TREE;
2949 declarator->u.array.quals = 0;
2950 }
2951 declarator->u.array.static_p = static_p;
2952 declarator->u.array.vla_unspec_p = vla_unspec_p;
2953 if (pedantic && !flag_isoc99)
2954 {
2955 if (static_p || quals != NULL)
2956 pedwarn ("ISO C90 does not support %<static%> or type "
2957 "qualifiers in parameter array declarators");
2958 if (vla_unspec_p)
2959 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2960 }
2961 if (vla_unspec_p)
2962 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
2963 return declarator;
2964 }
2965
2966 /* Set the contained declarator of an array declarator. DECL is the
2967 declarator, as constructed by build_array_declarator; INNER is what
2968 appears on the left of the []. ABSTRACT_P is true if it is an
2969 abstract declarator, false otherwise; this is used to reject static
2970 and type qualifiers in abstract declarators, where they are not in
2971 the C99 grammar (subject to possible change in DR#289). */
2972
2973 struct c_declarator *
2974 set_array_declarator_inner (struct c_declarator *decl,
2975 struct c_declarator *inner, bool abstract_p)
2976 {
2977 decl->declarator = inner;
2978 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
2979 || decl->u.array.attrs != NULL_TREE
2980 || decl->u.array.static_p))
2981 error ("static or type qualifiers in abstract declarator");
2982 return decl;
2983 }
2984 \f
2985 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2986
2987 tree
2988 groktypename (struct c_type_name *type_name)
2989 {
2990 tree type;
2991 tree attrs = type_name->specs->attrs;
2992
2993 type_name->specs->attrs = NULL_TREE;
2994
2995 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
2996 false, NULL);
2997
2998 /* Apply attributes. */
2999 decl_attributes (&type, attrs, 0);
3000
3001 return type;
3002 }
3003
3004 /* Decode a declarator in an ordinary declaration or data definition.
3005 This is called as soon as the type information and variable name
3006 have been parsed, before parsing the initializer if any.
3007 Here we create the ..._DECL node, fill in its type,
3008 and put it on the list of decls for the current context.
3009 The ..._DECL node is returned as the value.
3010
3011 Exception: for arrays where the length is not specified,
3012 the type is left null, to be filled in by `finish_decl'.
3013
3014 Function definitions do not come here; they go to start_function
3015 instead. However, external and forward declarations of functions
3016 do go through here. Structure field declarations are done by
3017 grokfield and not through here. */
3018
3019 tree
3020 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3021 bool initialized, tree attributes)
3022 {
3023 tree decl;
3024 tree tem;
3025
3026 /* An object declared as __attribute__((deprecated)) suppresses
3027 warnings of uses of other deprecated items. */
3028 if (lookup_attribute ("deprecated", attributes))
3029 deprecated_state = DEPRECATED_SUPPRESS;
3030
3031 decl = grokdeclarator (declarator, declspecs,
3032 NORMAL, initialized, NULL);
3033 if (!decl)
3034 return 0;
3035
3036 deprecated_state = DEPRECATED_NORMAL;
3037
3038 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3039 && MAIN_NAME_P (DECL_NAME (decl)))
3040 warning (0, "%J%qD is usually a function", decl, decl);
3041
3042 if (initialized)
3043 /* Is it valid for this decl to have an initializer at all?
3044 If not, set INITIALIZED to zero, which will indirectly
3045 tell 'finish_decl' to ignore the initializer once it is parsed. */
3046 switch (TREE_CODE (decl))
3047 {
3048 case TYPE_DECL:
3049 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3050 initialized = 0;
3051 break;
3052
3053 case FUNCTION_DECL:
3054 error ("function %qD is initialized like a variable", decl);
3055 initialized = 0;
3056 break;
3057
3058 case PARM_DECL:
3059 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3060 error ("parameter %qD is initialized", decl);
3061 initialized = 0;
3062 break;
3063
3064 default:
3065 /* Don't allow initializations for incomplete types except for
3066 arrays which might be completed by the initialization. */
3067
3068 /* This can happen if the array size is an undefined macro.
3069 We already gave a warning, so we don't need another one. */
3070 if (TREE_TYPE (decl) == error_mark_node)
3071 initialized = 0;
3072 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3073 {
3074 /* A complete type is ok if size is fixed. */
3075
3076 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3077 || C_DECL_VARIABLE_SIZE (decl))
3078 {
3079 error ("variable-sized object may not be initialized");
3080 initialized = 0;
3081 }
3082 }
3083 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3084 {
3085 error ("variable %qD has initializer but incomplete type", decl);
3086 initialized = 0;
3087 }
3088 else if (C_DECL_VARIABLE_SIZE (decl))
3089 {
3090 /* Although C99 is unclear about whether incomplete arrays
3091 of VLAs themselves count as VLAs, it does not make
3092 sense to permit them to be initialized given that
3093 ordinary VLAs may not be initialized. */
3094 error ("variable-sized object may not be initialized");
3095 initialized = 0;
3096 }
3097 }
3098
3099 if (initialized)
3100 {
3101 if (current_scope == file_scope)
3102 TREE_STATIC (decl) = 1;
3103
3104 /* Tell 'pushdecl' this is an initialized decl
3105 even though we don't yet have the initializer expression.
3106 Also tell 'finish_decl' it may store the real initializer. */
3107 DECL_INITIAL (decl) = error_mark_node;
3108 }
3109
3110 /* If this is a function declaration, write a record describing it to the
3111 prototypes file (if requested). */
3112
3113 if (TREE_CODE (decl) == FUNCTION_DECL)
3114 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3115
3116 /* ANSI specifies that a tentative definition which is not merged with
3117 a non-tentative definition behaves exactly like a definition with an
3118 initializer equal to zero. (Section 3.7.2)
3119
3120 -fno-common gives strict ANSI behavior, though this tends to break
3121 a large body of code that grew up without this rule.
3122
3123 Thread-local variables are never common, since there's no entrenched
3124 body of code to break, and it allows more efficient variable references
3125 in the presence of dynamic linking. */
3126
3127 if (TREE_CODE (decl) == VAR_DECL
3128 && !initialized
3129 && TREE_PUBLIC (decl)
3130 && !DECL_THREAD_LOCAL (decl)
3131 && !flag_no_common)
3132 DECL_COMMON (decl) = 1;
3133
3134 /* Set attributes here so if duplicate decl, will have proper attributes. */
3135 decl_attributes (&decl, attributes, 0);
3136
3137 if (TREE_CODE (decl) == FUNCTION_DECL
3138 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3139 {
3140 struct c_declarator *ce = declarator;
3141
3142 if (ce->kind == cdk_pointer)
3143 ce = declarator->declarator;
3144 if (ce->kind == cdk_function)
3145 {
3146 tree args = ce->u.arg_info->parms;
3147 for (; args; args = TREE_CHAIN (args))
3148 {
3149 tree type = TREE_TYPE (args);
3150 if (type && INTEGRAL_TYPE_P (type)
3151 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3152 DECL_ARG_TYPE (args) = integer_type_node;
3153 }
3154 }
3155 }
3156
3157 if (TREE_CODE (decl) == FUNCTION_DECL
3158 && DECL_DECLARED_INLINE_P (decl)
3159 && DECL_UNINLINABLE (decl)
3160 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3161 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3162
3163 /* Add this decl to the current scope.
3164 TEM may equal DECL or it may be a previous decl of the same name. */
3165 tem = pushdecl (decl);
3166
3167 if (initialized && DECL_EXTERNAL (tem))
3168 {
3169 DECL_EXTERNAL (tem) = 0;
3170 TREE_STATIC (tem) = 1;
3171 }
3172
3173 return tem;
3174 }
3175
3176 /* Finish processing of a declaration;
3177 install its initial value.
3178 If the length of an array type is not known before,
3179 it must be determined now, from the initial value, or it is an error. */
3180
3181 void
3182 finish_decl (tree decl, tree init, tree asmspec_tree)
3183 {
3184 tree type = TREE_TYPE (decl);
3185 int was_incomplete = (DECL_SIZE (decl) == 0);
3186 const char *asmspec = 0;
3187
3188 /* If a name was specified, get the string. */
3189 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3190 && DECL_FILE_SCOPE_P (decl))
3191 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3192 if (asmspec_tree)
3193 asmspec = TREE_STRING_POINTER (asmspec_tree);
3194
3195 /* If `start_decl' didn't like having an initialization, ignore it now. */
3196 if (init != 0 && DECL_INITIAL (decl) == 0)
3197 init = 0;
3198
3199 /* Don't crash if parm is initialized. */
3200 if (TREE_CODE (decl) == PARM_DECL)
3201 init = 0;
3202
3203 if (init)
3204 store_init_value (decl, init);
3205
3206 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3207 || TREE_CODE (decl) == FUNCTION_DECL
3208 || TREE_CODE (decl) == FIELD_DECL))
3209 objc_check_decl (decl);
3210
3211 /* Deduce size of array from initialization, if not already known. */
3212 if (TREE_CODE (type) == ARRAY_TYPE
3213 && TYPE_DOMAIN (type) == 0
3214 && TREE_CODE (decl) != TYPE_DECL)
3215 {
3216 bool do_default
3217 = (TREE_STATIC (decl)
3218 /* Even if pedantic, an external linkage array
3219 may have incomplete type at first. */
3220 ? pedantic && !TREE_PUBLIC (decl)
3221 : !DECL_EXTERNAL (decl));
3222 int failure
3223 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3224 do_default);
3225
3226 /* Get the completed type made by complete_array_type. */
3227 type = TREE_TYPE (decl);
3228
3229 if (failure == 1)
3230 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3231
3232 else if (failure == 2)
3233 {
3234 if (do_default)
3235 error ("%Jarray size missing in %qD", decl, decl);
3236 /* If a `static' var's size isn't known,
3237 make it extern as well as static, so it does not get
3238 allocated.
3239 If it is not `static', then do not mark extern;
3240 finish_incomplete_decl will give it a default size
3241 and it will get allocated. */
3242 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3243 DECL_EXTERNAL (decl) = 1;
3244 }
3245 else if (failure == 3)
3246 error ("%Jzero or negative size array %qD", decl, decl);
3247
3248 if (DECL_INITIAL (decl))
3249 TREE_TYPE (DECL_INITIAL (decl)) = type;
3250
3251 layout_decl (decl, 0);
3252 }
3253
3254 if (TREE_CODE (decl) == VAR_DECL)
3255 {
3256 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3257 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3258 layout_decl (decl, 0);
3259
3260 if (DECL_SIZE (decl) == 0
3261 /* Don't give an error if we already gave one earlier. */
3262 && TREE_TYPE (decl) != error_mark_node
3263 && (TREE_STATIC (decl)
3264 /* A static variable with an incomplete type
3265 is an error if it is initialized.
3266 Also if it is not file scope.
3267 Otherwise, let it through, but if it is not `extern'
3268 then it may cause an error message later. */
3269 ? (DECL_INITIAL (decl) != 0
3270 || !DECL_FILE_SCOPE_P (decl))
3271 /* An automatic variable with an incomplete type
3272 is an error. */
3273 : !DECL_EXTERNAL (decl)))
3274 {
3275 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3276 TREE_TYPE (decl) = error_mark_node;
3277 }
3278
3279 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3280 && DECL_SIZE (decl) != 0)
3281 {
3282 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3283 constant_expression_warning (DECL_SIZE (decl));
3284 else
3285 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3286 }
3287
3288 if (TREE_USED (type))
3289 TREE_USED (decl) = 1;
3290 }
3291
3292 /* If this is a function and an assembler name is specified, reset DECL_RTL
3293 so we can give it its new name. Also, update built_in_decls if it
3294 was a normal built-in. */
3295 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3296 {
3297 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3298 set_builtin_user_assembler_name (decl, asmspec);
3299 set_user_assembler_name (decl, asmspec);
3300 }
3301
3302 /* If #pragma weak was used, mark the decl weak now. */
3303 maybe_apply_pragma_weak (decl);
3304
3305 /* If this is a variable definition, determine its ELF visibility. */
3306 if (TREE_CODE (decl) == VAR_DECL
3307 && TREE_STATIC (decl)
3308 && !DECL_EXTERNAL (decl))
3309 c_determine_visibility (decl);
3310
3311 /* Output the assembler code and/or RTL code for variables and functions,
3312 unless the type is an undefined structure or union.
3313 If not, it will get done when the type is completed. */
3314
3315 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3316 {
3317 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3318 if (c_dialect_objc ())
3319 objc_check_decl (decl);
3320
3321 if (asmspec)
3322 {
3323 /* If this is not a static variable, issue a warning.
3324 It doesn't make any sense to give an ASMSPEC for an
3325 ordinary, non-register local variable. Historically,
3326 GCC has accepted -- but ignored -- the ASMSPEC in
3327 this case. */
3328 if (!DECL_FILE_SCOPE_P (decl)
3329 && TREE_CODE (decl) == VAR_DECL
3330 && !C_DECL_REGISTER (decl)
3331 && !TREE_STATIC (decl))
3332 warning (0, "%Jignoring asm-specifier for non-static local "
3333 "variable %qD", decl, decl);
3334 else if (C_DECL_REGISTER (decl))
3335 change_decl_assembler_name (decl, get_identifier (asmspec));
3336 else
3337 set_user_assembler_name (decl, asmspec);
3338 }
3339
3340 if (DECL_FILE_SCOPE_P (decl))
3341 {
3342 if (DECL_INITIAL (decl) == NULL_TREE
3343 || DECL_INITIAL (decl) == error_mark_node)
3344 /* Don't output anything
3345 when a tentative file-scope definition is seen.
3346 But at end of compilation, do output code for them. */
3347 DECL_DEFER_OUTPUT (decl) = 1;
3348 rest_of_decl_compilation (decl, true, 0);
3349 }
3350 else
3351 {
3352 /* In conjunction with an ASMSPEC, the `register'
3353 keyword indicates that we should place the variable
3354 in a particular register. */
3355 if (asmspec && C_DECL_REGISTER (decl))
3356 {
3357 DECL_HARD_REGISTER (decl) = 1;
3358 /* This cannot be done for a structure with volatile
3359 fields, on which DECL_REGISTER will have been
3360 reset. */
3361 if (!DECL_REGISTER (decl))
3362 error ("cannot put object with volatile field into register");
3363 }
3364
3365 if (TREE_CODE (decl) != FUNCTION_DECL)
3366 {
3367 /* If we're building a variable sized type, and we might be
3368 reachable other than via the top of the current binding
3369 level, then create a new BIND_EXPR so that we deallocate
3370 the object at the right time. */
3371 /* Note that DECL_SIZE can be null due to errors. */
3372 if (DECL_SIZE (decl)
3373 && !TREE_CONSTANT (DECL_SIZE (decl))
3374 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3375 {
3376 tree bind;
3377 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3378 TREE_SIDE_EFFECTS (bind) = 1;
3379 add_stmt (bind);
3380 BIND_EXPR_BODY (bind) = push_stmt_list ();
3381 }
3382 add_stmt (build_stmt (DECL_EXPR, decl));
3383 }
3384 }
3385
3386
3387 if (!DECL_FILE_SCOPE_P (decl))
3388 {
3389 /* Recompute the RTL of a local array now
3390 if it used to be an incomplete type. */
3391 if (was_incomplete
3392 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3393 {
3394 /* If we used it already as memory, it must stay in memory. */
3395 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3396 /* If it's still incomplete now, no init will save it. */
3397 if (DECL_SIZE (decl) == 0)
3398 DECL_INITIAL (decl) = 0;
3399 }
3400 }
3401 }
3402
3403 /* If this was marked 'used', be sure it will be output. */
3404 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3405 mark_decl_referenced (decl);
3406
3407 if (TREE_CODE (decl) == TYPE_DECL)
3408 {
3409 if (!DECL_FILE_SCOPE_P (decl)
3410 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3411 add_stmt (build_stmt (DECL_EXPR, decl));
3412
3413 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3414 }
3415
3416 /* At the end of a declaration, throw away any variable type sizes
3417 of types defined inside that declaration. There is no use
3418 computing them in the following function definition. */
3419 if (current_scope == file_scope)
3420 get_pending_sizes ();
3421
3422 /* Install a cleanup (aka destructor) if one was given. */
3423 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3424 {
3425 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3426 if (attr)
3427 {
3428 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3429 tree cleanup_decl = lookup_name (cleanup_id);
3430 tree cleanup;
3431
3432 /* Build "cleanup(&decl)" for the destructor. */
3433 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3434 cleanup = build_tree_list (NULL_TREE, cleanup);
3435 cleanup = build_function_call (cleanup_decl, cleanup);
3436
3437 /* Don't warn about decl unused; the cleanup uses it. */
3438 TREE_USED (decl) = 1;
3439 TREE_USED (cleanup_decl) = 1;
3440
3441 /* Initialize EH, if we've been told to do so. */
3442 if (flag_exceptions && !c_eh_initialized_p)
3443 {
3444 c_eh_initialized_p = true;
3445 eh_personality_libfunc
3446 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3447 ? "__gcc_personality_sj0"
3448 : "__gcc_personality_v0");
3449 using_eh_for_cleanups ();
3450 }
3451
3452 push_cleanup (decl, cleanup, false);
3453 }
3454 }
3455 }
3456
3457 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3458
3459 tree
3460 grokparm (const struct c_parm *parm)
3461 {
3462 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3463 NULL);
3464
3465 decl_attributes (&decl, parm->attrs, 0);
3466
3467 return decl;
3468 }
3469
3470 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3471 and push that on the current scope. */
3472
3473 void
3474 push_parm_decl (const struct c_parm *parm)
3475 {
3476 tree decl;
3477
3478 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3479 decl_attributes (&decl, parm->attrs, 0);
3480
3481 decl = pushdecl (decl);
3482
3483 finish_decl (decl, NULL_TREE, NULL_TREE);
3484 }
3485
3486 /* Mark all the parameter declarations to date as forward decls.
3487 Also diagnose use of this extension. */
3488
3489 void
3490 mark_forward_parm_decls (void)
3491 {
3492 struct c_binding *b;
3493
3494 if (pedantic && !current_scope->warned_forward_parm_decls)
3495 {
3496 pedwarn ("ISO C forbids forward parameter declarations");
3497 current_scope->warned_forward_parm_decls = true;
3498 }
3499
3500 for (b = current_scope->bindings; b; b = b->prev)
3501 if (TREE_CODE (b->decl) == PARM_DECL)
3502 TREE_ASM_WRITTEN (b->decl) = 1;
3503 }
3504 \f
3505 static GTY(()) int compound_literal_number;
3506
3507 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3508 literal, which may be an incomplete array type completed by the
3509 initializer; INIT is a CONSTRUCTOR that initializes the compound
3510 literal. */
3511
3512 tree
3513 build_compound_literal (tree type, tree init)
3514 {
3515 /* We do not use start_decl here because we have a type, not a declarator;
3516 and do not use finish_decl because the decl should be stored inside
3517 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3518 tree decl;
3519 tree complit;
3520 tree stmt;
3521
3522 if (type == error_mark_node)
3523 return error_mark_node;
3524
3525 decl = build_decl (VAR_DECL, NULL_TREE, type);
3526 DECL_EXTERNAL (decl) = 0;
3527 TREE_PUBLIC (decl) = 0;
3528 TREE_STATIC (decl) = (current_scope == file_scope);
3529 DECL_CONTEXT (decl) = current_function_decl;
3530 TREE_USED (decl) = 1;
3531 TREE_TYPE (decl) = type;
3532 TREE_READONLY (decl) = TYPE_READONLY (type);
3533 store_init_value (decl, init);
3534
3535 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3536 {
3537 int failure = complete_array_type (&TREE_TYPE (decl),
3538 DECL_INITIAL (decl), true);
3539 gcc_assert (!failure);
3540
3541 type = TREE_TYPE (decl);
3542 TREE_TYPE (DECL_INITIAL (decl)) = type;
3543 }
3544
3545 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3546 return error_mark_node;
3547
3548 stmt = build_stmt (DECL_EXPR, decl);
3549 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3550 TREE_SIDE_EFFECTS (complit) = 1;
3551
3552 layout_decl (decl, 0);
3553
3554 if (TREE_STATIC (decl))
3555 {
3556 /* This decl needs a name for the assembler output. We also need
3557 a unique suffix to be added to the name. */
3558 char *name;
3559
3560 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3561 compound_literal_number);
3562 compound_literal_number++;
3563 DECL_NAME (decl) = get_identifier (name);
3564 DECL_DEFER_OUTPUT (decl) = 1;
3565 DECL_COMDAT (decl) = 1;
3566 DECL_ARTIFICIAL (decl) = 1;
3567 DECL_IGNORED_P (decl) = 1;
3568 pushdecl (decl);
3569 rest_of_decl_compilation (decl, 1, 0);
3570 }
3571
3572 return complit;
3573 }
3574 \f
3575 /* Determine whether TYPE is a structure with a flexible array member,
3576 or a union containing such a structure (possibly recursively). */
3577
3578 static bool
3579 flexible_array_type_p (tree type)
3580 {
3581 tree x;
3582 switch (TREE_CODE (type))
3583 {
3584 case RECORD_TYPE:
3585 x = TYPE_FIELDS (type);
3586 if (x == NULL_TREE)
3587 return false;
3588 while (TREE_CHAIN (x) != NULL_TREE)
3589 x = TREE_CHAIN (x);
3590 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3591 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3592 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3593 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3594 return true;
3595 return false;
3596 case UNION_TYPE:
3597 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3598 {
3599 if (flexible_array_type_p (TREE_TYPE (x)))
3600 return true;
3601 }
3602 return false;
3603 default:
3604 return false;
3605 }
3606 }
3607 \f
3608 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3609 replacing with appropriate values if they are invalid. */
3610 static void
3611 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3612 {
3613 tree type_mv;
3614 unsigned int max_width;
3615 unsigned HOST_WIDE_INT w;
3616 const char *name = orig_name ? orig_name: _("<anonymous>");
3617
3618 /* Detect and ignore out of range field width and process valid
3619 field widths. */
3620 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3621 || TREE_CODE (*width) != INTEGER_CST)
3622 {
3623 error ("bit-field %qs width not an integer constant", name);
3624 *width = integer_one_node;
3625 }
3626 else
3627 {
3628 constant_expression_warning (*width);
3629 if (tree_int_cst_sgn (*width) < 0)
3630 {
3631 error ("negative width in bit-field %qs", name);
3632 *width = integer_one_node;
3633 }
3634 else if (integer_zerop (*width) && orig_name)
3635 {
3636 error ("zero width for bit-field %qs", name);
3637 *width = integer_one_node;
3638 }
3639 }
3640
3641 /* Detect invalid bit-field type. */
3642 if (TREE_CODE (*type) != INTEGER_TYPE
3643 && TREE_CODE (*type) != BOOLEAN_TYPE
3644 && TREE_CODE (*type) != ENUMERAL_TYPE)
3645 {
3646 error ("bit-field %qs has invalid type", name);
3647 *type = unsigned_type_node;
3648 }
3649
3650 type_mv = TYPE_MAIN_VARIANT (*type);
3651 if (pedantic
3652 && type_mv != integer_type_node
3653 && type_mv != unsigned_type_node
3654 && type_mv != boolean_type_node)
3655 pedwarn ("type of bit-field %qs is a GCC extension", name);
3656
3657 if (type_mv == boolean_type_node)
3658 max_width = CHAR_TYPE_SIZE;
3659 else
3660 max_width = TYPE_PRECISION (*type);
3661
3662 if (0 < compare_tree_int (*width, max_width))
3663 {
3664 error ("width of %qs exceeds its type", name);
3665 w = max_width;
3666 *width = build_int_cst (NULL_TREE, w);
3667 }
3668 else
3669 w = tree_low_cst (*width, 1);
3670
3671 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3672 {
3673 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3674 if (!lt
3675 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3676 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3677 warning (0, "%qs is narrower than values of its type", name);
3678 }
3679 }
3680 \f
3681 /* Given declspecs and a declarator,
3682 determine the name and type of the object declared
3683 and construct a ..._DECL node for it.
3684 (In one case we can return a ..._TYPE node instead.
3685 For invalid input we sometimes return 0.)
3686
3687 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3688
3689 DECL_CONTEXT says which syntactic context this declaration is in:
3690 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3691 FUNCDEF for a function definition. Like NORMAL but a few different
3692 error messages in each case. Return value may be zero meaning
3693 this definition is too screwy to try to parse.
3694 PARM for a parameter declaration (either within a function prototype
3695 or before a function body). Make a PARM_DECL, or return void_type_node.
3696 TYPENAME if for a typename (in a cast or sizeof).
3697 Don't make a DECL node; just return the ..._TYPE node.
3698 FIELD for a struct or union field; make a FIELD_DECL.
3699 INITIALIZED is true if the decl has an initializer.
3700 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3701 representing the width of the bit-field.
3702
3703 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3704 It may also be so in the PARM case, for a prototype where the
3705 argument type is specified but not the name.
3706
3707 This function is where the complicated C meanings of `static'
3708 and `extern' are interpreted. */
3709
3710 static tree
3711 grokdeclarator (const struct c_declarator *declarator,
3712 struct c_declspecs *declspecs,
3713 enum decl_context decl_context, bool initialized, tree *width)
3714 {
3715 tree type = declspecs->type;
3716 bool threadp = declspecs->thread_p;
3717 enum c_storage_class storage_class = declspecs->storage_class;
3718 int constp;
3719 int restrictp;
3720 int volatilep;
3721 int type_quals = TYPE_UNQUALIFIED;
3722 const char *name, *orig_name;
3723 tree typedef_type = 0;
3724 int funcdef_flag = 0;
3725 bool funcdef_syntax = false;
3726 int size_varies = 0;
3727 tree decl_attr = declspecs->decl_attr;
3728 int array_ptr_quals = TYPE_UNQUALIFIED;
3729 tree array_ptr_attrs = NULL_TREE;
3730 int array_parm_static = 0;
3731 tree returned_attrs = NULL_TREE;
3732 bool bitfield = width != NULL;
3733 tree element_type;
3734 struct c_arg_info *arg_info = 0;
3735
3736 if (decl_context == FUNCDEF)
3737 funcdef_flag = 1, decl_context = NORMAL;
3738
3739 /* Look inside a declarator for the name being declared
3740 and get it as a string, for an error message. */
3741 {
3742 const struct c_declarator *decl = declarator;
3743 name = 0;
3744
3745 while (decl)
3746 switch (decl->kind)
3747 {
3748 case cdk_function:
3749 case cdk_array:
3750 case cdk_pointer:
3751 funcdef_syntax = (decl->kind == cdk_function);
3752 decl = decl->declarator;
3753 break;
3754
3755 case cdk_attrs:
3756 decl = decl->declarator;
3757 break;
3758
3759 case cdk_id:
3760 if (decl->u.id)
3761 name = IDENTIFIER_POINTER (decl->u.id);
3762 decl = 0;
3763 break;
3764
3765 default:
3766 gcc_unreachable ();
3767 }
3768 orig_name = name;
3769 if (name == 0)
3770 name = "type name";
3771 }
3772
3773 /* A function definition's declarator must have the form of
3774 a function declarator. */
3775
3776 if (funcdef_flag && !funcdef_syntax)
3777 return 0;
3778
3779 /* If this looks like a function definition, make it one,
3780 even if it occurs where parms are expected.
3781 Then store_parm_decls will reject it and not use it as a parm. */
3782 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3783 decl_context = PARM;
3784
3785 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3786 warn_deprecated_use (declspecs->type);
3787
3788 typedef_type = type;
3789 size_varies = C_TYPE_VARIABLE_SIZE (type);
3790
3791 /* Diagnose defaulting to "int". */
3792
3793 if (declspecs->default_int_p && !in_system_header)
3794 {
3795 /* Issue a warning if this is an ISO C 99 program or if
3796 -Wreturn-type and this is a function, or if -Wimplicit;
3797 prefer the former warning since it is more explicit. */
3798 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3799 && funcdef_flag)
3800 warn_about_return_type = 1;
3801 else if (warn_implicit_int || flag_isoc99)
3802 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3803 }
3804
3805 /* Adjust the type if a bit-field is being declared,
3806 -funsigned-bitfields applied and the type is not explicitly
3807 "signed". */
3808 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3809 && TREE_CODE (type) == INTEGER_TYPE)
3810 type = c_common_unsigned_type (type);
3811
3812 /* Figure out the type qualifiers for the declaration. There are
3813 two ways a declaration can become qualified. One is something
3814 like `const int i' where the `const' is explicit. Another is
3815 something like `typedef const int CI; CI i' where the type of the
3816 declaration contains the `const'. A third possibility is that
3817 there is a type qualifier on the element type of a typedefed
3818 array type, in which case we should extract that qualifier so
3819 that c_apply_type_quals_to_decls receives the full list of
3820 qualifiers to work with (C90 is not entirely clear about whether
3821 duplicate qualifiers should be diagnosed in this case, but it
3822 seems most appropriate to do so). */
3823 element_type = strip_array_types (type);
3824 constp = declspecs->const_p + TYPE_READONLY (element_type);
3825 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3826 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3827 if (pedantic && !flag_isoc99)
3828 {
3829 if (constp > 1)
3830 pedwarn ("duplicate %<const%>");
3831 if (restrictp > 1)
3832 pedwarn ("duplicate %<restrict%>");
3833 if (volatilep > 1)
3834 pedwarn ("duplicate %<volatile%>");
3835 }
3836 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3837 type = TYPE_MAIN_VARIANT (type);
3838 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3839 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3840 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3841
3842 /* Warn about storage classes that are invalid for certain
3843 kinds of declarations (parameters, typenames, etc.). */
3844
3845 if (funcdef_flag
3846 && (threadp
3847 || storage_class == csc_auto
3848 || storage_class == csc_register
3849 || storage_class == csc_typedef))
3850 {
3851 if (storage_class == csc_auto
3852 && (pedantic || current_scope == file_scope))
3853 pedwarn ("function definition declared %<auto%>");
3854 if (storage_class == csc_register)
3855 error ("function definition declared %<register%>");
3856 if (storage_class == csc_typedef)
3857 error ("function definition declared %<typedef%>");
3858 if (threadp)
3859 error ("function definition declared %<__thread%>");
3860 threadp = false;
3861 if (storage_class == csc_auto
3862 || storage_class == csc_register
3863 || storage_class == csc_typedef)
3864 storage_class = csc_none;
3865 }
3866 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3867 {
3868 if (decl_context == PARM && storage_class == csc_register)
3869 ;
3870 else
3871 {
3872 switch (decl_context)
3873 {
3874 case FIELD:
3875 error ("storage class specified for structure field %qs",
3876 name);
3877 break;
3878 case PARM:
3879 error ("storage class specified for parameter %qs", name);
3880 break;
3881 default:
3882 error ("storage class specified for typename");
3883 break;
3884 }
3885 storage_class = csc_none;
3886 threadp = false;
3887 }
3888 }
3889 else if (storage_class == csc_extern
3890 && initialized
3891 && !funcdef_flag)
3892 {
3893 /* 'extern' with initialization is invalid if not at file scope. */
3894 if (current_scope == file_scope)
3895 warning (0, "%qs initialized and declared %<extern%>", name);
3896 else
3897 error ("%qs has both %<extern%> and initializer", name);
3898 }
3899 else if (current_scope == file_scope)
3900 {
3901 if (storage_class == csc_auto)
3902 error ("file-scope declaration of %qs specifies %<auto%>", name);
3903 if (pedantic && storage_class == csc_register)
3904 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3905 }
3906 else
3907 {
3908 if (storage_class == csc_extern && funcdef_flag)
3909 error ("nested function %qs declared %<extern%>", name);
3910 else if (threadp && storage_class == csc_none)
3911 {
3912 error ("function-scope %qs implicitly auto and declared "
3913 "%<__thread%>",
3914 name);
3915 threadp = false;
3916 }
3917 }
3918
3919 /* Now figure out the structure of the declarator proper.
3920 Descend through it, creating more complex types, until we reach
3921 the declared identifier (or NULL_TREE, in an absolute declarator).
3922 At each stage we maintain an unqualified version of the type
3923 together with any qualifiers that should be applied to it with
3924 c_build_qualified_type; this way, array types including
3925 multidimensional array types are first built up in unqualified
3926 form and then the qualified form is created with
3927 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3928
3929 while (declarator && declarator->kind != cdk_id)
3930 {
3931 if (type == error_mark_node)
3932 {
3933 declarator = declarator->declarator;
3934 continue;
3935 }
3936
3937 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3938 a cdk_pointer (for *...),
3939 a cdk_function (for ...(...)),
3940 a cdk_attrs (for nested attributes),
3941 or a cdk_id (for the name being declared
3942 or the place in an absolute declarator
3943 where the name was omitted).
3944 For the last case, we have just exited the loop.
3945
3946 At this point, TYPE is the type of elements of an array,
3947 or for a function to return, or for a pointer to point to.
3948 After this sequence of ifs, TYPE is the type of the
3949 array or function or pointer, and DECLARATOR has had its
3950 outermost layer removed. */
3951
3952 if (array_ptr_quals != TYPE_UNQUALIFIED
3953 || array_ptr_attrs != NULL_TREE
3954 || array_parm_static)
3955 {
3956 /* Only the innermost declarator (making a parameter be of
3957 array type which is converted to pointer type)
3958 may have static or type qualifiers. */
3959 error ("static or type qualifiers in non-parameter array declarator");
3960 array_ptr_quals = TYPE_UNQUALIFIED;
3961 array_ptr_attrs = NULL_TREE;
3962 array_parm_static = 0;
3963 }
3964
3965 switch (declarator->kind)
3966 {
3967 case cdk_attrs:
3968 {
3969 /* A declarator with embedded attributes. */
3970 tree attrs = declarator->u.attrs;
3971 const struct c_declarator *inner_decl;
3972 int attr_flags = 0;
3973 declarator = declarator->declarator;
3974 inner_decl = declarator;
3975 while (inner_decl->kind == cdk_attrs)
3976 inner_decl = inner_decl->declarator;
3977 if (inner_decl->kind == cdk_id)
3978 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3979 else if (inner_decl->kind == cdk_function)
3980 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3981 else if (inner_decl->kind == cdk_array)
3982 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3983 returned_attrs = decl_attributes (&type,
3984 chainon (returned_attrs, attrs),
3985 attr_flags);
3986 break;
3987 }
3988 case cdk_array:
3989 {
3990 tree itype = NULL_TREE;
3991 tree size = declarator->u.array.dimen;
3992 /* The index is a signed object `sizetype' bits wide. */
3993 tree index_type = c_common_signed_type (sizetype);
3994
3995 array_ptr_quals = declarator->u.array.quals;
3996 array_ptr_attrs = declarator->u.array.attrs;
3997 array_parm_static = declarator->u.array.static_p;
3998
3999 declarator = declarator->declarator;
4000
4001 /* Check for some types that there cannot be arrays of. */
4002
4003 if (VOID_TYPE_P (type))
4004 {
4005 error ("declaration of %qs as array of voids", name);
4006 type = error_mark_node;
4007 }
4008
4009 if (TREE_CODE (type) == FUNCTION_TYPE)
4010 {
4011 error ("declaration of %qs as array of functions", name);
4012 type = error_mark_node;
4013 }
4014
4015 if (pedantic && !in_system_header && flexible_array_type_p (type))
4016 pedwarn ("invalid use of structure with flexible array member");
4017
4018 if (size == error_mark_node)
4019 type = error_mark_node;
4020
4021 if (type == error_mark_node)
4022 continue;
4023
4024 /* If size was specified, set ITYPE to a range-type for
4025 that size. Otherwise, ITYPE remains null. finish_decl
4026 may figure it out from an initial value. */
4027
4028 if (size)
4029 {
4030 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4031 lvalue. */
4032 STRIP_TYPE_NOPS (size);
4033
4034 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4035 {
4036 error ("size of array %qs has non-integer type", name);
4037 size = integer_one_node;
4038 }
4039
4040 if (pedantic && integer_zerop (size))
4041 pedwarn ("ISO C forbids zero-size array %qs", name);
4042
4043 if (TREE_CODE (size) == INTEGER_CST)
4044 {
4045 constant_expression_warning (size);
4046 if (tree_int_cst_sgn (size) < 0)
4047 {
4048 error ("size of array %qs is negative", name);
4049 size = integer_one_node;
4050 }
4051 }
4052 else
4053 {
4054 /* Make sure the array size remains visibly
4055 nonconstant even if it is (eg) a const variable
4056 with known value. */
4057 size_varies = 1;
4058
4059 if (!flag_isoc99 && pedantic)
4060 {
4061 if (TREE_CONSTANT (size))
4062 pedwarn ("ISO C90 forbids array %qs whose size "
4063 "can%'t be evaluated",
4064 name);
4065 else
4066 pedwarn ("ISO C90 forbids variable-size array %qs",
4067 name);
4068 }
4069 }
4070
4071 if (integer_zerop (size))
4072 {
4073 /* A zero-length array cannot be represented with
4074 an unsigned index type, which is what we'll
4075 get with build_index_type. Create an
4076 open-ended range instead. */
4077 itype = build_range_type (sizetype, size, NULL_TREE);
4078 }
4079 else
4080 {
4081 /* Arrange for the SAVE_EXPR on the inside of the
4082 MINUS_EXPR, which allows the -1 to get folded
4083 with the +1 that happens when building TYPE_SIZE. */
4084 if (size_varies)
4085 size = variable_size (size);
4086
4087 /* Compute the maximum valid index, that is, size
4088 - 1. Do the calculation in index_type, so that
4089 if it is a variable the computations will be
4090 done in the proper mode. */
4091 itype = fold (build2 (MINUS_EXPR, index_type,
4092 convert (index_type, size),
4093 convert (index_type,
4094 size_one_node)));
4095
4096 /* If that overflowed, the array is too big. ???
4097 While a size of INT_MAX+1 technically shouldn't
4098 cause an overflow (because we subtract 1), the
4099 overflow is recorded during the conversion to
4100 index_type, before the subtraction. Handling
4101 this case seems like an unnecessary
4102 complication. */
4103 if (TREE_OVERFLOW (itype))
4104 {
4105 error ("size of array %qs is too large", name);
4106 type = error_mark_node;
4107 continue;
4108 }
4109
4110 itype = build_index_type (itype);
4111 }
4112 }
4113 else if (decl_context == FIELD)
4114 {
4115 if (pedantic && !flag_isoc99 && !in_system_header)
4116 pedwarn ("ISO C90 does not support flexible array members");
4117
4118 /* ISO C99 Flexible array members are effectively
4119 identical to GCC's zero-length array extension. */
4120 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4121 }
4122
4123 /* Complain about arrays of incomplete types. */
4124 if (!COMPLETE_TYPE_P (type))
4125 {
4126 error ("array type has incomplete element type");
4127 type = error_mark_node;
4128 }
4129 else
4130 type = build_array_type (type, itype);
4131
4132 if (size_varies)
4133 C_TYPE_VARIABLE_SIZE (type) = 1;
4134
4135 /* The GCC extension for zero-length arrays differs from
4136 ISO flexible array members in that sizeof yields
4137 zero. */
4138 if (size && integer_zerop (size))
4139 {
4140 TYPE_SIZE (type) = bitsize_zero_node;
4141 TYPE_SIZE_UNIT (type) = size_zero_node;
4142 }
4143
4144 if (decl_context != PARM
4145 && (array_ptr_quals != TYPE_UNQUALIFIED
4146 || array_ptr_attrs != NULL_TREE
4147 || array_parm_static))
4148 {
4149 error ("static or type qualifiers in non-parameter array declarator");
4150 array_ptr_quals = TYPE_UNQUALIFIED;
4151 array_ptr_attrs = NULL_TREE;
4152 array_parm_static = 0;
4153 }
4154 break;
4155 }
4156 case cdk_function:
4157 {
4158 /* Say it's a definition only for the declarator closest
4159 to the identifier, apart possibly from some
4160 attributes. */
4161 bool really_funcdef = false;
4162 tree arg_types;
4163 if (funcdef_flag)
4164 {
4165 const struct c_declarator *t = declarator->declarator;
4166 while (t->kind == cdk_attrs)
4167 t = t->declarator;
4168 really_funcdef = (t->kind == cdk_id);
4169 }
4170
4171 /* Declaring a function type. Make sure we have a valid
4172 type for the function to return. */
4173 if (type == error_mark_node)
4174 continue;
4175
4176 size_varies = 0;
4177
4178 /* Warn about some types functions can't return. */
4179 if (TREE_CODE (type) == FUNCTION_TYPE)
4180 {
4181 error ("%qs declared as function returning a function", name);
4182 type = integer_type_node;
4183 }
4184 if (TREE_CODE (type) == ARRAY_TYPE)
4185 {
4186 error ("%qs declared as function returning an array", name);
4187 type = integer_type_node;
4188 }
4189
4190 /* Construct the function type and go to the next
4191 inner layer of declarator. */
4192 arg_info = declarator->u.arg_info;
4193 arg_types = grokparms (arg_info, really_funcdef);
4194
4195 /* Type qualifiers before the return type of the function
4196 qualify the return type, not the function type. */
4197 if (type_quals)
4198 {
4199 /* Type qualifiers on a function return type are
4200 normally permitted by the standard but have no
4201 effect, so give a warning at -Wreturn-type.
4202 Qualifiers on a void return type are banned on
4203 function definitions in ISO C; GCC used to used
4204 them for noreturn functions. */
4205 if (VOID_TYPE_P (type) && really_funcdef)
4206 pedwarn ("function definition has qualified void return type");
4207 else if (warn_return_type)
4208 warning (0, "type qualifiers ignored on function return type");
4209
4210 type = c_build_qualified_type (type, type_quals);
4211 }
4212 type_quals = TYPE_UNQUALIFIED;
4213
4214 type = build_function_type (type, arg_types);
4215 declarator = declarator->declarator;
4216
4217 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4218 the formal parameter list of this FUNCTION_TYPE to point to
4219 the FUNCTION_TYPE node itself. */
4220 {
4221 tree link;
4222
4223 for (link = arg_info->tags;
4224 link;
4225 link = TREE_CHAIN (link))
4226 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4227 }
4228 break;
4229 }
4230 case cdk_pointer:
4231 {
4232 /* Merge any constancy or volatility into the target type
4233 for the pointer. */
4234
4235 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4236 && type_quals)
4237 pedwarn ("ISO C forbids qualified function types");
4238 if (type_quals)
4239 type = c_build_qualified_type (type, type_quals);
4240 size_varies = 0;
4241
4242 type = build_pointer_type (type);
4243
4244 /* Process type qualifiers (such as const or volatile)
4245 that were given inside the `*'. */
4246 type_quals = declarator->u.pointer_quals;
4247
4248 declarator = declarator->declarator;
4249 break;
4250 }
4251 default:
4252 gcc_unreachable ();
4253 }
4254 }
4255
4256 /* Now TYPE has the actual type, apart from any qualifiers in
4257 TYPE_QUALS. */
4258
4259 /* Check the type and width of a bit-field. */
4260 if (bitfield)
4261 check_bitfield_type_and_width (&type, width, orig_name);
4262
4263 /* Did array size calculations overflow? */
4264
4265 if (TREE_CODE (type) == ARRAY_TYPE
4266 && COMPLETE_TYPE_P (type)
4267 && TREE_OVERFLOW (TYPE_SIZE (type)))
4268 {
4269 error ("size of array %qs is too large", name);
4270 /* If we proceed with the array type as it is, we'll eventually
4271 crash in tree_low_cst(). */
4272 type = error_mark_node;
4273 }
4274
4275 /* If this is declaring a typedef name, return a TYPE_DECL. */
4276
4277 if (storage_class == csc_typedef)
4278 {
4279 tree decl;
4280 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4281 && type_quals)
4282 pedwarn ("ISO C forbids qualified function types");
4283 if (type_quals)
4284 type = c_build_qualified_type (type, type_quals);
4285 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4286 if (declspecs->explicit_signed_p)
4287 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4288 decl_attributes (&decl, returned_attrs, 0);
4289 if (declspecs->inline_p)
4290 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4291 return decl;
4292 }
4293
4294 /* Detect the case of an array type of unspecified size
4295 which came, as such, direct from a typedef name.
4296 We must copy the type, so that each identifier gets
4297 a distinct type, so that each identifier's size can be
4298 controlled separately by its own initializer. */
4299
4300 if (type != 0 && typedef_type != 0
4301 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4302 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4303 {
4304 type = build_array_type (TREE_TYPE (type), 0);
4305 if (size_varies)
4306 C_TYPE_VARIABLE_SIZE (type) = 1;
4307 }
4308
4309 /* If this is a type name (such as, in a cast or sizeof),
4310 compute the type and return it now. */
4311
4312 if (decl_context == TYPENAME)
4313 {
4314 /* Note that the grammar rejects storage classes in typenames
4315 and fields. */
4316 gcc_assert (storage_class == csc_none && !threadp
4317 && !declspecs->inline_p);
4318 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4319 && type_quals)
4320 pedwarn ("ISO C forbids const or volatile function types");
4321 if (type_quals)
4322 type = c_build_qualified_type (type, type_quals);
4323 decl_attributes (&type, returned_attrs, 0);
4324 return type;
4325 }
4326
4327 /* Aside from typedefs and type names (handle above),
4328 `void' at top level (not within pointer)
4329 is allowed only in public variables.
4330 We don't complain about parms either, but that is because
4331 a better error message can be made later. */
4332
4333 if (VOID_TYPE_P (type) && decl_context != PARM
4334 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4335 && (storage_class == csc_extern
4336 || (current_scope == file_scope
4337 && !(storage_class == csc_static
4338 || storage_class == csc_register)))))
4339 {
4340 error ("variable or field %qs declared void", name);
4341 type = integer_type_node;
4342 }
4343
4344 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4345 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4346
4347 {
4348 tree decl;
4349
4350 if (decl_context == PARM)
4351 {
4352 tree type_as_written;
4353 tree promoted_type;
4354
4355 /* A parameter declared as an array of T is really a pointer to T.
4356 One declared as a function is really a pointer to a function. */
4357
4358 if (TREE_CODE (type) == ARRAY_TYPE)
4359 {
4360 /* Transfer const-ness of array into that of type pointed to. */
4361 type = TREE_TYPE (type);
4362 if (type_quals)
4363 type = c_build_qualified_type (type, type_quals);
4364 type = build_pointer_type (type);
4365 type_quals = array_ptr_quals;
4366
4367 /* We don't yet implement attributes in this context. */
4368 if (array_ptr_attrs != NULL_TREE)
4369 warning (0, "attributes in parameter array declarator ignored");
4370
4371 size_varies = 0;
4372 }
4373 else if (TREE_CODE (type) == FUNCTION_TYPE)
4374 {
4375 if (pedantic && type_quals)
4376 pedwarn ("ISO C forbids qualified function types");
4377 if (type_quals)
4378 type = c_build_qualified_type (type, type_quals);
4379 type = build_pointer_type (type);
4380 type_quals = TYPE_UNQUALIFIED;
4381 }
4382 else if (type_quals)
4383 type = c_build_qualified_type (type, type_quals);
4384
4385 type_as_written = type;
4386
4387 decl = build_decl (PARM_DECL, declarator->u.id, type);
4388 if (size_varies)
4389 C_DECL_VARIABLE_SIZE (decl) = 1;
4390
4391 /* Compute the type actually passed in the parmlist,
4392 for the case where there is no prototype.
4393 (For example, shorts and chars are passed as ints.)
4394 When there is a prototype, this is overridden later. */
4395
4396 if (type == error_mark_node)
4397 promoted_type = type;
4398 else
4399 promoted_type = c_type_promotes_to (type);
4400
4401 DECL_ARG_TYPE (decl) = promoted_type;
4402 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4403 if (declspecs->inline_p)
4404 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4405 }
4406 else if (decl_context == FIELD)
4407 {
4408 /* Note that the grammar rejects storage classes in typenames
4409 and fields. */
4410 gcc_assert (storage_class == csc_none && !threadp
4411 && !declspecs->inline_p);
4412
4413 /* Structure field. It may not be a function. */
4414
4415 if (TREE_CODE (type) == FUNCTION_TYPE)
4416 {
4417 error ("field %qs declared as a function", name);
4418 type = build_pointer_type (type);
4419 }
4420 else if (TREE_CODE (type) != ERROR_MARK
4421 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4422 {
4423 error ("field %qs has incomplete type", name);
4424 type = error_mark_node;
4425 }
4426 type = c_build_qualified_type (type, type_quals);
4427 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4428 DECL_NONADDRESSABLE_P (decl) = bitfield;
4429
4430 if (size_varies)
4431 C_DECL_VARIABLE_SIZE (decl) = 1;
4432 }
4433 else if (TREE_CODE (type) == FUNCTION_TYPE)
4434 {
4435 if (storage_class == csc_register || threadp)
4436 {
4437 error ("invalid storage class for function %qs", name);
4438 }
4439 else if (current_scope != file_scope)
4440 {
4441 /* Function declaration not at file scope. Storage
4442 classes other than `extern' are not allowed, C99
4443 6.7.1p5, and `extern' makes no difference. However,
4444 GCC allows 'auto', perhaps with 'inline', to support
4445 nested functions. */
4446 if (storage_class == csc_auto)
4447 {
4448 if (pedantic)
4449 pedwarn ("invalid storage class for function %qs", name);
4450 }
4451 else if (storage_class == csc_static)
4452 {
4453 error ("invalid storage class for function %qs", name);
4454 if (funcdef_flag)
4455 storage_class = declspecs->storage_class = csc_none;
4456 else
4457 return 0;
4458 }
4459 }
4460
4461 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4462 decl = build_decl_attribute_variant (decl, decl_attr);
4463
4464 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4465
4466 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4467 pedwarn ("ISO C forbids qualified function types");
4468
4469 /* GNU C interprets a volatile-qualified function type to indicate
4470 that the function does not return. */
4471 if ((type_quals & TYPE_QUAL_VOLATILE)
4472 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4473 warning (0, "%<noreturn%> function returns non-void value");
4474
4475 /* Every function declaration is an external reference
4476 (DECL_EXTERNAL) except for those which are not at file
4477 scope and are explicitly declared "auto". This is
4478 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4479 GCC to signify a forward declaration of a nested function. */
4480 if (storage_class == csc_auto && current_scope != file_scope)
4481 DECL_EXTERNAL (decl) = 0;
4482 else
4483 DECL_EXTERNAL (decl) = 1;
4484
4485 /* Record absence of global scope for `static' or `auto'. */
4486 TREE_PUBLIC (decl)
4487 = !(storage_class == csc_static || storage_class == csc_auto);
4488
4489 /* For a function definition, record the argument information
4490 block where store_parm_decls will look for it. */
4491 if (funcdef_flag)
4492 current_function_arg_info = arg_info;
4493
4494 if (declspecs->default_int_p)
4495 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4496
4497 /* Record presence of `inline', if it is reasonable. */
4498 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4499 {
4500 if (declspecs->inline_p)
4501 pedwarn ("cannot inline function %<main%>");
4502 }
4503 else if (declspecs->inline_p)
4504 {
4505 /* Record that the function is declared `inline'. */
4506 DECL_DECLARED_INLINE_P (decl) = 1;
4507
4508 /* Do not mark bare declarations as DECL_INLINE. Doing so
4509 in the presence of multiple declarations can result in
4510 the abstract origin pointing between the declarations,
4511 which will confuse dwarf2out. */
4512 if (initialized)
4513 {
4514 DECL_INLINE (decl) = 1;
4515 if (storage_class == csc_extern)
4516 current_extern_inline = 1;
4517 }
4518 }
4519 /* If -finline-functions, assume it can be inlined. This does
4520 two things: let the function be deferred until it is actually
4521 needed, and let dwarf2 know that the function is inlinable. */
4522 else if (flag_inline_trees == 2 && initialized)
4523 DECL_INLINE (decl) = 1;
4524 }
4525 else
4526 {
4527 /* It's a variable. */
4528 /* An uninitialized decl with `extern' is a reference. */
4529 int extern_ref = !initialized && storage_class == csc_extern;
4530
4531 type = c_build_qualified_type (type, type_quals);
4532
4533 /* C99 6.2.2p7: It is invalid (compile-time undefined
4534 behavior) to create an 'extern' declaration for a
4535 variable if there is a global declaration that is
4536 'static' and the global declaration is not visible.
4537 (If the static declaration _is_ currently visible,
4538 the 'extern' declaration is taken to refer to that decl.) */
4539 if (extern_ref && current_scope != file_scope)
4540 {
4541 tree global_decl = identifier_global_value (declarator->u.id);
4542 tree visible_decl = lookup_name (declarator->u.id);
4543
4544 if (global_decl
4545 && global_decl != visible_decl
4546 && TREE_CODE (global_decl) == VAR_DECL
4547 && !TREE_PUBLIC (global_decl))
4548 error ("variable previously declared %<static%> redeclared "
4549 "%<extern%>");
4550 }
4551
4552 decl = build_decl (VAR_DECL, declarator->u.id, type);
4553 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4554 if (size_varies)
4555 C_DECL_VARIABLE_SIZE (decl) = 1;
4556
4557 if (declspecs->inline_p)
4558 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4559
4560 /* At file scope, an initialized extern declaration may follow
4561 a static declaration. In that case, DECL_EXTERNAL will be
4562 reset later in start_decl. */
4563 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4564
4565 /* At file scope, the presence of a `static' or `register' storage
4566 class specifier, or the absence of all storage class specifiers
4567 makes this declaration a definition (perhaps tentative). Also,
4568 the absence of both `static' and `register' makes it public. */
4569 if (current_scope == file_scope)
4570 {
4571 TREE_PUBLIC (decl) = !(storage_class == csc_static
4572 || storage_class == csc_register);
4573 TREE_STATIC (decl) = !extern_ref;
4574 }
4575 /* Not at file scope, only `static' makes a static definition. */
4576 else
4577 {
4578 TREE_STATIC (decl) = (storage_class == csc_static);
4579 TREE_PUBLIC (decl) = extern_ref;
4580 }
4581
4582 if (threadp)
4583 {
4584 if (targetm.have_tls)
4585 DECL_THREAD_LOCAL (decl) = 1;
4586 else
4587 /* A mere warning is sure to result in improper semantics
4588 at runtime. Don't bother to allow this to compile. */
4589 error ("thread-local storage not supported for this target");
4590 }
4591 }
4592
4593 /* Record `register' declaration for warnings on &
4594 and in case doing stupid register allocation. */
4595
4596 if (storage_class == csc_register)
4597 {
4598 C_DECL_REGISTER (decl) = 1;
4599 DECL_REGISTER (decl) = 1;
4600 }
4601
4602 /* Record constancy and volatility. */
4603 c_apply_type_quals_to_decl (type_quals, decl);
4604
4605 /* If a type has volatile components, it should be stored in memory.
4606 Otherwise, the fact that those components are volatile
4607 will be ignored, and would even crash the compiler. */
4608 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4609 {
4610 /* It is not an error for a structure with volatile fields to
4611 be declared register, but reset DECL_REGISTER since it
4612 cannot actually go in a register. */
4613 int was_reg = C_DECL_REGISTER (decl);
4614 C_DECL_REGISTER (decl) = 0;
4615 DECL_REGISTER (decl) = 0;
4616 c_mark_addressable (decl);
4617 C_DECL_REGISTER (decl) = was_reg;
4618 }
4619
4620 /* This is the earliest point at which we might know the assembler
4621 name of a variable. Thus, if it's known before this, die horribly. */
4622 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4623
4624 decl_attributes (&decl, returned_attrs, 0);
4625
4626 return decl;
4627 }
4628 }
4629 \f
4630 /* Decode the parameter-list info for a function type or function definition.
4631 The argument is the value returned by `get_parm_info' (or made in parse.y
4632 if there is an identifier list instead of a parameter decl list).
4633 These two functions are separate because when a function returns
4634 or receives functions then each is called multiple times but the order
4635 of calls is different. The last call to `grokparms' is always the one
4636 that contains the formal parameter names of a function definition.
4637
4638 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4639
4640 FUNCDEF_FLAG is true for a function definition, false for
4641 a mere declaration. A nonempty identifier-list gets an error message
4642 when FUNCDEF_FLAG is false. */
4643
4644 static tree
4645 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4646 {
4647 tree arg_types = arg_info->types;
4648
4649 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4650 && !in_system_header)
4651 warning (0, "function declaration isn%'t a prototype");
4652
4653 if (arg_types == error_mark_node)
4654 return 0; /* don't set TYPE_ARG_TYPES in this case */
4655
4656 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4657 {
4658 if (!funcdef_flag)
4659 pedwarn ("parameter names (without types) in function declaration");
4660
4661 arg_info->parms = arg_info->types;
4662 arg_info->types = 0;
4663 return 0;
4664 }
4665 else
4666 {
4667 tree parm, type, typelt;
4668 unsigned int parmno;
4669
4670 /* If there is a parameter of incomplete type in a definition,
4671 this is an error. In a declaration this is valid, and a
4672 struct or union type may be completed later, before any calls
4673 or definition of the function. In the case where the tag was
4674 first declared within the parameter list, a warning has
4675 already been given. If a parameter has void type, then
4676 however the function cannot be defined or called, so
4677 warn. */
4678
4679 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4680 parm;
4681 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4682 {
4683 type = TREE_VALUE (typelt);
4684 if (type == error_mark_node)
4685 continue;
4686
4687 if (!COMPLETE_TYPE_P (type))
4688 {
4689 if (funcdef_flag)
4690 {
4691 if (DECL_NAME (parm))
4692 error ("%Jparameter %u (%qD) has incomplete type",
4693 parm, parmno, parm);
4694 else
4695 error ("%Jparameter %u has incomplete type",
4696 parm, parmno);
4697
4698 TREE_VALUE (typelt) = error_mark_node;
4699 TREE_TYPE (parm) = error_mark_node;
4700 }
4701 else if (VOID_TYPE_P (type))
4702 {
4703 if (DECL_NAME (parm))
4704 warning (0, "%Jparameter %u (%qD) has void type",
4705 parm, parmno, parm);
4706 else
4707 warning (0, "%Jparameter %u has void type",
4708 parm, parmno);
4709 }
4710 }
4711 }
4712 return arg_types;
4713 }
4714 }
4715
4716 /* Take apart the current scope and return a c_arg_info structure with
4717 info on a parameter list just parsed.
4718
4719 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4720
4721 ELLIPSIS being true means the argument list ended in '...' so don't
4722 append a sentinel (void_list_node) to the end of the type-list. */
4723
4724 struct c_arg_info *
4725 get_parm_info (bool ellipsis)
4726 {
4727 struct c_binding *b = current_scope->bindings;
4728 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4729 struct c_arg_info);
4730 tree parms = 0;
4731 tree tags = 0;
4732 tree types = 0;
4733 tree others = 0;
4734
4735 static bool explained_incomplete_types = false;
4736 bool gave_void_only_once_err = false;
4737
4738 arg_info->parms = 0;
4739 arg_info->tags = 0;
4740 arg_info->types = 0;
4741 arg_info->others = 0;
4742
4743 /* The bindings in this scope must not get put into a block.
4744 We will take care of deleting the binding nodes. */
4745 current_scope->bindings = 0;
4746
4747 /* This function is only called if there was *something* on the
4748 parameter list. */
4749 gcc_assert (b);
4750
4751 /* A parameter list consisting solely of 'void' indicates that the
4752 function takes no arguments. But if the 'void' is qualified
4753 (by 'const' or 'volatile'), or has a storage class specifier
4754 ('register'), then the behavior is undefined; issue an error.
4755 Typedefs for 'void' are OK (see DR#157). */
4756 if (b->prev == 0 /* one binding */
4757 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4758 && !DECL_NAME (b->decl) /* anonymous */
4759 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4760 {
4761 if (TREE_THIS_VOLATILE (b->decl)
4762 || TREE_READONLY (b->decl)
4763 || C_DECL_REGISTER (b->decl))
4764 error ("%<void%> as only parameter may not be qualified");
4765
4766 /* There cannot be an ellipsis. */
4767 if (ellipsis)
4768 error ("%<void%> must be the only parameter");
4769
4770 arg_info->types = void_list_node;
4771 return arg_info;
4772 }
4773
4774 if (!ellipsis)
4775 types = void_list_node;
4776
4777 /* Break up the bindings list into parms, tags, types, and others;
4778 apply sanity checks; purge the name-to-decl bindings. */
4779 while (b)
4780 {
4781 tree decl = b->decl;
4782 tree type = TREE_TYPE (decl);
4783 const char *keyword;
4784
4785 switch (TREE_CODE (decl))
4786 {
4787 case PARM_DECL:
4788 if (b->id)
4789 {
4790 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4791 I_SYMBOL_BINDING (b->id) = b->shadowed;
4792 }
4793
4794 /* Check for forward decls that never got their actual decl. */
4795 if (TREE_ASM_WRITTEN (decl))
4796 error ("%Jparameter %qD has just a forward declaration",
4797 decl, decl);
4798 /* Check for (..., void, ...) and issue an error. */
4799 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4800 {
4801 if (!gave_void_only_once_err)
4802 {
4803 error ("%<void%> must be the only parameter");
4804 gave_void_only_once_err = true;
4805 }
4806 }
4807 else
4808 {
4809 /* Valid parameter, add it to the list. */
4810 TREE_CHAIN (decl) = parms;
4811 parms = decl;
4812
4813 /* Since there is a prototype, args are passed in their
4814 declared types. The back end may override this later. */
4815 DECL_ARG_TYPE (decl) = type;
4816 types = tree_cons (0, type, types);
4817 }
4818 break;
4819
4820 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4821 case UNION_TYPE: keyword = "union"; goto tag;
4822 case RECORD_TYPE: keyword = "struct"; goto tag;
4823 tag:
4824 /* Types may not have tag-names, in which case the type
4825 appears in the bindings list with b->id NULL. */
4826 if (b->id)
4827 {
4828 gcc_assert (I_TAG_BINDING (b->id) == b);
4829 I_TAG_BINDING (b->id) = b->shadowed;
4830 }
4831
4832 /* Warn about any struct, union or enum tags defined in a
4833 parameter list. The scope of such types is limited to
4834 the parameter list, which is rarely if ever desirable
4835 (it's impossible to call such a function with type-
4836 correct arguments). An anonymous union parm type is
4837 meaningful as a GNU extension, so don't warn for that. */
4838 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4839 {
4840 if (b->id)
4841 /* The %s will be one of 'struct', 'union', or 'enum'. */
4842 warning (0, "%<%s %E%> declared inside parameter list",
4843 keyword, b->id);
4844 else
4845 /* The %s will be one of 'struct', 'union', or 'enum'. */
4846 warning (0, "anonymous %s declared inside parameter list",
4847 keyword);
4848
4849 if (!explained_incomplete_types)
4850 {
4851 warning (0, "its scope is only this definition or declaration,"
4852 " which is probably not what you want");
4853 explained_incomplete_types = true;
4854 }
4855 }
4856
4857 tags = tree_cons (b->id, decl, tags);
4858 break;
4859
4860 case CONST_DECL:
4861 case TYPE_DECL:
4862 case FUNCTION_DECL:
4863 /* CONST_DECLs appear here when we have an embedded enum,
4864 and TYPE_DECLs appear here when we have an embedded struct
4865 or union. No warnings for this - we already warned about the
4866 type itself. FUNCTION_DECLs appear when there is an implicit
4867 function declaration in the parameter list. */
4868
4869 TREE_CHAIN (decl) = others;
4870 others = decl;
4871 /* fall through */
4872
4873 case ERROR_MARK:
4874 /* error_mark_node appears here when we have an undeclared
4875 variable. Just throw it away. */
4876 if (b->id)
4877 {
4878 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4879 I_SYMBOL_BINDING (b->id) = b->shadowed;
4880 }
4881 break;
4882
4883 /* Other things that might be encountered. */
4884 case LABEL_DECL:
4885 case VAR_DECL:
4886 default:
4887 gcc_unreachable ();
4888 }
4889
4890 b = free_binding_and_advance (b);
4891 }
4892
4893 arg_info->parms = parms;
4894 arg_info->tags = tags;
4895 arg_info->types = types;
4896 arg_info->others = others;
4897 return arg_info;
4898 }
4899 \f
4900 /* Get the struct, enum or union (CODE says which) with tag NAME.
4901 Define the tag as a forward-reference if it is not defined.
4902 Return a c_typespec structure for the type specifier. */
4903
4904 struct c_typespec
4905 parser_xref_tag (enum tree_code code, tree name)
4906 {
4907 struct c_typespec ret;
4908 /* If a cross reference is requested, look up the type
4909 already defined for this tag and return it. */
4910
4911 tree ref = lookup_tag (code, name, 0);
4912 /* If this is the right type of tag, return what we found.
4913 (This reference will be shadowed by shadow_tag later if appropriate.)
4914 If this is the wrong type of tag, do not return it. If it was the
4915 wrong type in the same scope, we will have had an error
4916 message already; if in a different scope and declaring
4917 a name, pending_xref_error will give an error message; but if in a
4918 different scope and not declaring a name, this tag should
4919 shadow the previous declaration of a different type of tag, and
4920 this would not work properly if we return the reference found.
4921 (For example, with "struct foo" in an outer scope, "union foo;"
4922 must shadow that tag with a new one of union type.) */
4923 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4924 if (ref && TREE_CODE (ref) == code)
4925 {
4926 ret.spec = ref;
4927 return ret;
4928 }
4929
4930 /* If no such tag is yet defined, create a forward-reference node
4931 and record it as the "definition".
4932 When a real declaration of this type is found,
4933 the forward-reference will be altered into a real type. */
4934
4935 ref = make_node (code);
4936 if (code == ENUMERAL_TYPE)
4937 {
4938 /* Give the type a default layout like unsigned int
4939 to avoid crashing if it does not get defined. */
4940 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4941 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4942 TYPE_USER_ALIGN (ref) = 0;
4943 TYPE_UNSIGNED (ref) = 1;
4944 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4945 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4946 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4947 }
4948
4949 pushtag (name, ref);
4950
4951 ret.spec = ref;
4952 return ret;
4953 }
4954
4955 /* Get the struct, enum or union (CODE says which) with tag NAME.
4956 Define the tag as a forward-reference if it is not defined.
4957 Return a tree for the type. */
4958
4959 tree
4960 xref_tag (enum tree_code code, tree name)
4961 {
4962 return parser_xref_tag (code, name).spec;
4963 }
4964 \f
4965 /* Make sure that the tag NAME is defined *in the current scope*
4966 at least as a forward reference.
4967 CODE says which kind of tag NAME ought to be. */
4968
4969 tree
4970 start_struct (enum tree_code code, tree name)
4971 {
4972 /* If there is already a tag defined at this scope
4973 (as a forward reference), just return it. */
4974
4975 tree ref = 0;
4976
4977 if (name != 0)
4978 ref = lookup_tag (code, name, 1);
4979 if (ref && TREE_CODE (ref) == code)
4980 {
4981 if (TYPE_SIZE (ref))
4982 {
4983 if (code == UNION_TYPE)
4984 error ("redefinition of %<union %E%>", name);
4985 else
4986 error ("redefinition of %<struct %E%>", name);
4987 }
4988 else if (C_TYPE_BEING_DEFINED (ref))
4989 {
4990 if (code == UNION_TYPE)
4991 error ("nested redefinition of %<union %E%>", name);
4992 else
4993 error ("nested redefinition of %<struct %E%>", name);
4994 }
4995 }
4996 else
4997 {
4998 /* Otherwise create a forward-reference just so the tag is in scope. */
4999
5000 ref = make_node (code);
5001 pushtag (name, ref);
5002 }
5003
5004 C_TYPE_BEING_DEFINED (ref) = 1;
5005 TYPE_PACKED (ref) = flag_pack_struct;
5006 return ref;
5007 }
5008
5009 /* Process the specs, declarator and width (NULL if omitted)
5010 of a structure component, returning a FIELD_DECL node.
5011 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5012
5013 This is done during the parsing of the struct declaration.
5014 The FIELD_DECL nodes are chained together and the lot of them
5015 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5016
5017 tree
5018 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5019 tree width)
5020 {
5021 tree value;
5022
5023 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5024 && width == NULL_TREE)
5025 {
5026 /* This is an unnamed decl.
5027
5028 If we have something of the form "union { list } ;" then this
5029 is the anonymous union extension. Similarly for struct.
5030
5031 If this is something of the form "struct foo;", then
5032 If MS extensions are enabled, this is handled as an
5033 anonymous struct.
5034 Otherwise this is a forward declaration of a structure tag.
5035
5036 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5037 If MS extensions are enabled and foo names a structure, then
5038 again this is an anonymous struct.
5039 Otherwise this is an error.
5040
5041 Oh what a horrid tangled web we weave. I wonder if MS consciously
5042 took this from Plan 9 or if it was an accident of implementation
5043 that took root before someone noticed the bug... */
5044
5045 tree type = declspecs->type;
5046 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5047 || TREE_CODE (type) == UNION_TYPE);
5048 bool ok = false;
5049
5050 if (type_ok
5051 && (flag_ms_extensions || !declspecs->typedef_p))
5052 {
5053 if (flag_ms_extensions)
5054 ok = true;
5055 else if (flag_iso)
5056 ok = false;
5057 else if (TYPE_NAME (type) == NULL)
5058 ok = true;
5059 else
5060 ok = false;
5061 }
5062 if (!ok)
5063 {
5064 pedwarn ("declaration does not declare anything");
5065 return NULL_TREE;
5066 }
5067 if (pedantic)
5068 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5069 }
5070
5071 value = grokdeclarator (declarator, declspecs, FIELD, false,
5072 width ? &width : NULL);
5073
5074 finish_decl (value, NULL_TREE, NULL_TREE);
5075 DECL_INITIAL (value) = width;
5076
5077 return value;
5078 }
5079 \f
5080 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5081 the list such that this does not present a problem later. */
5082
5083 static void
5084 detect_field_duplicates (tree fieldlist)
5085 {
5086 tree x, y;
5087 int timeout = 10;
5088
5089 /* First, see if there are more than "a few" fields.
5090 This is trivially true if there are zero or one fields. */
5091 if (!fieldlist)
5092 return;
5093 x = TREE_CHAIN (fieldlist);
5094 if (!x)
5095 return;
5096 do {
5097 timeout--;
5098 x = TREE_CHAIN (x);
5099 } while (timeout > 0 && x);
5100
5101 /* If there were "few" fields, avoid the overhead of allocating
5102 a hash table. Instead just do the nested traversal thing. */
5103 if (timeout > 0)
5104 {
5105 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5106 if (DECL_NAME (x))
5107 {
5108 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5109 if (DECL_NAME (y) == DECL_NAME (x))
5110 {
5111 error ("%Jduplicate member %qD", x, x);
5112 DECL_NAME (x) = NULL_TREE;
5113 }
5114 }
5115 }
5116 else
5117 {
5118 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5119 void **slot;
5120
5121 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5122 if ((y = DECL_NAME (x)) != 0)
5123 {
5124 slot = htab_find_slot (htab, y, INSERT);
5125 if (*slot)
5126 {
5127 error ("%Jduplicate member %qD", x, x);
5128 DECL_NAME (x) = NULL_TREE;
5129 }
5130 *slot = y;
5131 }
5132
5133 htab_delete (htab);
5134 }
5135 }
5136
5137 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5138 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5139 ATTRIBUTES are attributes to be applied to the structure. */
5140
5141 tree
5142 finish_struct (tree t, tree fieldlist, tree attributes)
5143 {
5144 tree x;
5145 bool toplevel = file_scope == current_scope;
5146 int saw_named_field;
5147
5148 /* If this type was previously laid out as a forward reference,
5149 make sure we lay it out again. */
5150
5151 TYPE_SIZE (t) = 0;
5152
5153 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5154
5155 if (pedantic)
5156 {
5157 for (x = fieldlist; x; x = TREE_CHAIN (x))
5158 if (DECL_NAME (x) != 0)
5159 break;
5160
5161 if (x == 0)
5162 {
5163 if (TREE_CODE (t) == UNION_TYPE)
5164 {
5165 if (fieldlist)
5166 pedwarn ("union has no named members");
5167 else
5168 pedwarn ("union has no members");
5169 }
5170 else
5171 {
5172 if (fieldlist)
5173 pedwarn ("struct has no named members");
5174 else
5175 pedwarn ("struct has no members");
5176 }
5177 }
5178 }
5179
5180 /* Install struct as DECL_CONTEXT of each field decl.
5181 Also process specified field sizes, found in the DECL_INITIAL,
5182 storing 0 there after the type has been changed to precision equal
5183 to its width, rather than the precision of the specified standard
5184 type. (Correct layout requires the original type to have been preserved
5185 until now.) */
5186
5187 saw_named_field = 0;
5188 for (x = fieldlist; x; x = TREE_CHAIN (x))
5189 {
5190 DECL_CONTEXT (x) = t;
5191 DECL_PACKED (x) |= TYPE_PACKED (t);
5192
5193 /* If any field is const, the structure type is pseudo-const. */
5194 if (TREE_READONLY (x))
5195 C_TYPE_FIELDS_READONLY (t) = 1;
5196 else
5197 {
5198 /* A field that is pseudo-const makes the structure likewise. */
5199 tree t1 = TREE_TYPE (x);
5200 while (TREE_CODE (t1) == ARRAY_TYPE)
5201 t1 = TREE_TYPE (t1);
5202 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5203 && C_TYPE_FIELDS_READONLY (t1))
5204 C_TYPE_FIELDS_READONLY (t) = 1;
5205 }
5206
5207 /* Any field that is volatile means variables of this type must be
5208 treated in some ways as volatile. */
5209 if (TREE_THIS_VOLATILE (x))
5210 C_TYPE_FIELDS_VOLATILE (t) = 1;
5211
5212 /* Any field of nominal variable size implies structure is too. */
5213 if (C_DECL_VARIABLE_SIZE (x))
5214 C_TYPE_VARIABLE_SIZE (t) = 1;
5215
5216 if (DECL_INITIAL (x))
5217 {
5218 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5219 DECL_SIZE (x) = bitsize_int (width);
5220 DECL_BIT_FIELD (x) = 1;
5221 SET_DECL_C_BIT_FIELD (x);
5222 }
5223
5224 /* Detect flexible array member in an invalid context. */
5225 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5226 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5227 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5228 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5229 {
5230 if (TREE_CODE (t) == UNION_TYPE)
5231 {
5232 error ("%Jflexible array member in union", x);
5233 TREE_TYPE (x) = error_mark_node;
5234 }
5235 else if (TREE_CHAIN (x) != NULL_TREE)
5236 {
5237 error ("%Jflexible array member not at end of struct", x);
5238 TREE_TYPE (x) = error_mark_node;
5239 }
5240 else if (!saw_named_field)
5241 {
5242 error ("%Jflexible array member in otherwise empty struct", x);
5243 TREE_TYPE (x) = error_mark_node;
5244 }
5245 }
5246
5247 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5248 && flexible_array_type_p (TREE_TYPE (x)))
5249 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5250
5251 if (DECL_NAME (x))
5252 saw_named_field = 1;
5253 }
5254
5255 detect_field_duplicates (fieldlist);
5256
5257 /* Now we have the nearly final fieldlist. Record it,
5258 then lay out the structure or union (including the fields). */
5259
5260 TYPE_FIELDS (t) = fieldlist;
5261
5262 layout_type (t);
5263
5264 /* Give bit-fields their proper types. */
5265 {
5266 tree *fieldlistp = &fieldlist;
5267 while (*fieldlistp)
5268 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5269 && TREE_TYPE (*fieldlistp) != error_mark_node)
5270 {
5271 unsigned HOST_WIDE_INT width
5272 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5273 tree type = TREE_TYPE (*fieldlistp);
5274 if (width != TYPE_PRECISION (type))
5275 {
5276 TREE_TYPE (*fieldlistp)
5277 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5278 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5279 }
5280 DECL_INITIAL (*fieldlistp) = 0;
5281 }
5282 else
5283 fieldlistp = &TREE_CHAIN (*fieldlistp);
5284 }
5285
5286 /* Now we have the truly final field list.
5287 Store it in this type and in the variants. */
5288
5289 TYPE_FIELDS (t) = fieldlist;
5290
5291 /* If there are lots of fields, sort so we can look through them fast.
5292 We arbitrarily consider 16 or more elts to be "a lot". */
5293
5294 {
5295 int len = 0;
5296
5297 for (x = fieldlist; x; x = TREE_CHAIN (x))
5298 {
5299 if (len > 15 || DECL_NAME (x) == NULL)
5300 break;
5301 len += 1;
5302 }
5303
5304 if (len > 15)
5305 {
5306 tree *field_array;
5307 struct lang_type *space;
5308 struct sorted_fields_type *space2;
5309
5310 len += list_length (x);
5311
5312 /* Use the same allocation policy here that make_node uses, to
5313 ensure that this lives as long as the rest of the struct decl.
5314 All decls in an inline function need to be saved. */
5315
5316 space = GGC_CNEW (struct lang_type);
5317 space2 = GGC_NEWVAR (struct sorted_fields_type,
5318 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5319
5320 len = 0;
5321 space->s = space2;
5322 field_array = &space2->elts[0];
5323 for (x = fieldlist; x; x = TREE_CHAIN (x))
5324 {
5325 field_array[len++] = x;
5326
5327 /* If there is anonymous struct or union, break out of the loop. */
5328 if (DECL_NAME (x) == NULL)
5329 break;
5330 }
5331 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5332 if (x == NULL)
5333 {
5334 TYPE_LANG_SPECIFIC (t) = space;
5335 TYPE_LANG_SPECIFIC (t)->s->len = len;
5336 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5337 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5338 }
5339 }
5340 }
5341
5342 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5343 {
5344 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5345 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5346 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5347 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5348 }
5349
5350 /* If this was supposed to be a transparent union, but we can't
5351 make it one, warn and turn off the flag. */
5352 if (TREE_CODE (t) == UNION_TYPE
5353 && TYPE_TRANSPARENT_UNION (t)
5354 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5355 {
5356 TYPE_TRANSPARENT_UNION (t) = 0;
5357 warning (0, "union cannot be made transparent");
5358 }
5359
5360 /* If this structure or union completes the type of any previous
5361 variable declaration, lay it out and output its rtl. */
5362 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5363 x;
5364 x = TREE_CHAIN (x))
5365 {
5366 tree decl = TREE_VALUE (x);
5367 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5368 layout_array_type (TREE_TYPE (decl));
5369 if (TREE_CODE (decl) != TYPE_DECL)
5370 {
5371 layout_decl (decl, 0);
5372 if (c_dialect_objc ())
5373 objc_check_decl (decl);
5374 rest_of_decl_compilation (decl, toplevel, 0);
5375 if (!toplevel)
5376 expand_decl (decl);
5377 }
5378 }
5379 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5380
5381 /* Finish debugging output for this type. */
5382 rest_of_type_compilation (t, toplevel);
5383
5384 /* If we're inside a function proper, i.e. not file-scope and not still
5385 parsing parameters, then arrange for the size of a variable sized type
5386 to be bound now. */
5387 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5388 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5389
5390 return t;
5391 }
5392
5393 /* Lay out the type T, and its element type, and so on. */
5394
5395 static void
5396 layout_array_type (tree t)
5397 {
5398 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5399 layout_array_type (TREE_TYPE (t));
5400 layout_type (t);
5401 }
5402 \f
5403 /* Begin compiling the definition of an enumeration type.
5404 NAME is its name (or null if anonymous).
5405 Returns the type object, as yet incomplete.
5406 Also records info about it so that build_enumerator
5407 may be used to declare the individual values as they are read. */
5408
5409 tree
5410 start_enum (tree name)
5411 {
5412 tree enumtype = 0;
5413
5414 /* If this is the real definition for a previous forward reference,
5415 fill in the contents in the same object that used to be the
5416 forward reference. */
5417
5418 if (name != 0)
5419 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5420
5421 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5422 {
5423 enumtype = make_node (ENUMERAL_TYPE);
5424 pushtag (name, enumtype);
5425 }
5426
5427 if (C_TYPE_BEING_DEFINED (enumtype))
5428 error ("nested redefinition of %<enum %E%>", name);
5429
5430 C_TYPE_BEING_DEFINED (enumtype) = 1;
5431
5432 if (TYPE_VALUES (enumtype) != 0)
5433 {
5434 /* This enum is a named one that has been declared already. */
5435 error ("redeclaration of %<enum %E%>", name);
5436
5437 /* Completely replace its old definition.
5438 The old enumerators remain defined, however. */
5439 TYPE_VALUES (enumtype) = 0;
5440 }
5441
5442 enum_next_value = integer_zero_node;
5443 enum_overflow = 0;
5444
5445 if (flag_short_enums)
5446 TYPE_PACKED (enumtype) = 1;
5447
5448 return enumtype;
5449 }
5450
5451 /* After processing and defining all the values of an enumeration type,
5452 install their decls in the enumeration type and finish it off.
5453 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5454 and ATTRIBUTES are the specified attributes.
5455 Returns ENUMTYPE. */
5456
5457 tree
5458 finish_enum (tree enumtype, tree values, tree attributes)
5459 {
5460 tree pair, tem;
5461 tree minnode = 0, maxnode = 0;
5462 int precision, unsign;
5463 bool toplevel = (file_scope == current_scope);
5464 struct lang_type *lt;
5465
5466 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5467
5468 /* Calculate the maximum value of any enumerator in this type. */
5469
5470 if (values == error_mark_node)
5471 minnode = maxnode = integer_zero_node;
5472 else
5473 {
5474 minnode = maxnode = TREE_VALUE (values);
5475 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5476 {
5477 tree value = TREE_VALUE (pair);
5478 if (tree_int_cst_lt (maxnode, value))
5479 maxnode = value;
5480 if (tree_int_cst_lt (value, minnode))
5481 minnode = value;
5482 }
5483 }
5484
5485 /* Construct the final type of this enumeration. It is the same
5486 as one of the integral types - the narrowest one that fits, except
5487 that normally we only go as narrow as int - and signed iff any of
5488 the values are negative. */
5489 unsign = (tree_int_cst_sgn (minnode) >= 0);
5490 precision = MAX (min_precision (minnode, unsign),
5491 min_precision (maxnode, unsign));
5492
5493 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5494 {
5495 tem = c_common_type_for_size (precision, unsign);
5496 if (tem == NULL)
5497 {
5498 warning (0, "enumeration values exceed range of largest integer");
5499 tem = long_long_integer_type_node;
5500 }
5501 }
5502 else
5503 tem = unsign ? unsigned_type_node : integer_type_node;
5504
5505 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5506 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5507 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5508 TYPE_SIZE (enumtype) = 0;
5509
5510 /* If the precision of the type was specific with an attribute and it
5511 was too small, give an error. Otherwise, use it. */
5512 if (TYPE_PRECISION (enumtype))
5513 {
5514 if (precision > TYPE_PRECISION (enumtype))
5515 error ("specified mode too small for enumeral values");
5516 }
5517 else
5518 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5519
5520 layout_type (enumtype);
5521
5522 if (values != error_mark_node)
5523 {
5524 /* Change the type of the enumerators to be the enum type. We
5525 need to do this irrespective of the size of the enum, for
5526 proper type checking. Replace the DECL_INITIALs of the
5527 enumerators, and the value slots of the list, with copies
5528 that have the enum type; they cannot be modified in place
5529 because they may be shared (e.g. integer_zero_node) Finally,
5530 change the purpose slots to point to the names of the decls. */
5531 for (pair = values; pair; pair = TREE_CHAIN (pair))
5532 {
5533 tree enu = TREE_PURPOSE (pair);
5534 tree ini = DECL_INITIAL (enu);
5535
5536 TREE_TYPE (enu) = enumtype;
5537
5538 /* The ISO C Standard mandates enumerators to have type int,
5539 even though the underlying type of an enum type is
5540 unspecified. Here we convert any enumerators that fit in
5541 an int to type int, to avoid promotions to unsigned types
5542 when comparing integers with enumerators that fit in the
5543 int range. When -pedantic is given, build_enumerator()
5544 would have already taken care of those that don't fit. */
5545 if (int_fits_type_p (ini, integer_type_node))
5546 tem = integer_type_node;
5547 else
5548 tem = enumtype;
5549 ini = convert (tem, ini);
5550
5551 DECL_INITIAL (enu) = ini;
5552 TREE_PURPOSE (pair) = DECL_NAME (enu);
5553 TREE_VALUE (pair) = ini;
5554 }
5555
5556 TYPE_VALUES (enumtype) = values;
5557 }
5558
5559 /* Record the min/max values so that we can warn about bit-field
5560 enumerations that are too small for the values. */
5561 lt = GGC_CNEW (struct lang_type);
5562 lt->enum_min = minnode;
5563 lt->enum_max = maxnode;
5564 TYPE_LANG_SPECIFIC (enumtype) = lt;
5565
5566 /* Fix up all variant types of this enum type. */
5567 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5568 {
5569 if (tem == enumtype)
5570 continue;
5571 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5572 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5573 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5574 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5575 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5576 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5577 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5578 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5579 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5580 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5581 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5582 }
5583
5584 /* Finish debugging output for this type. */
5585 rest_of_type_compilation (enumtype, toplevel);
5586
5587 return enumtype;
5588 }
5589
5590 /* Build and install a CONST_DECL for one value of the
5591 current enumeration type (one that was begun with start_enum).
5592 Return a tree-list containing the CONST_DECL and its value.
5593 Assignment of sequential values by default is handled here. */
5594
5595 tree
5596 build_enumerator (tree name, tree value)
5597 {
5598 tree decl, type;
5599
5600 /* Validate and default VALUE. */
5601
5602 if (value != 0)
5603 {
5604 /* Don't issue more errors for error_mark_node (i.e. an
5605 undeclared identifier) - just ignore the value expression. */
5606 if (value == error_mark_node)
5607 value = 0;
5608 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5609 || TREE_CODE (value) != INTEGER_CST)
5610 {
5611 error ("enumerator value for %qE is not an integer constant", name);
5612 value = 0;
5613 }
5614 else
5615 {
5616 value = default_conversion (value);
5617 constant_expression_warning (value);
5618 }
5619 }
5620
5621 /* Default based on previous value. */
5622 /* It should no longer be possible to have NON_LVALUE_EXPR
5623 in the default. */
5624 if (value == 0)
5625 {
5626 value = enum_next_value;
5627 if (enum_overflow)
5628 error ("overflow in enumeration values");
5629 }
5630
5631 if (pedantic && !int_fits_type_p (value, integer_type_node))
5632 {
5633 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5634 /* XXX This causes -pedantic to change the meaning of the program.
5635 Remove? -zw 2004-03-15 */
5636 value = convert (integer_type_node, value);
5637 }
5638
5639 /* Set basis for default for next value. */
5640 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5641 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5642
5643 /* Now create a declaration for the enum value name. */
5644
5645 type = TREE_TYPE (value);
5646 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5647 TYPE_PRECISION (integer_type_node)),
5648 (TYPE_PRECISION (type)
5649 >= TYPE_PRECISION (integer_type_node)
5650 && TYPE_UNSIGNED (type)));
5651
5652 decl = build_decl (CONST_DECL, name, type);
5653 DECL_INITIAL (decl) = convert (type, value);
5654 pushdecl (decl);
5655
5656 return tree_cons (decl, value, NULL_TREE);
5657 }
5658
5659 \f
5660 /* Create the FUNCTION_DECL for a function definition.
5661 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5662 the declaration; they describe the function's name and the type it returns,
5663 but twisted together in a fashion that parallels the syntax of C.
5664
5665 This function creates a binding context for the function body
5666 as well as setting up the FUNCTION_DECL in current_function_decl.
5667
5668 Returns 1 on success. If the DECLARATOR is not suitable for a function
5669 (it defines a datum instead), we return 0, which tells
5670 yyparse to report a parse error. */
5671
5672 int
5673 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5674 tree attributes)
5675 {
5676 tree decl1, old_decl;
5677 tree restype, resdecl;
5678 struct c_label_context_se *nstack_se;
5679 struct c_label_context_vm *nstack_vm;
5680
5681 current_function_returns_value = 0; /* Assume, until we see it does. */
5682 current_function_returns_null = 0;
5683 current_function_returns_abnormally = 0;
5684 warn_about_return_type = 0;
5685 current_extern_inline = 0;
5686 c_switch_stack = NULL;
5687
5688 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5689 nstack_se->labels_def = NULL;
5690 nstack_se->labels_used = NULL;
5691 nstack_se->next = label_context_stack_se;
5692 label_context_stack_se = nstack_se;
5693
5694 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5695 nstack_vm->labels_def = NULL;
5696 nstack_vm->labels_used = NULL;
5697 nstack_vm->scope = 0;
5698 nstack_vm->next = label_context_stack_vm;
5699 label_context_stack_vm = nstack_vm;
5700
5701 /* Indicate no valid break/continue context by setting these variables
5702 to some non-null, non-label value. We'll notice and emit the proper
5703 error message in c_finish_bc_stmt. */
5704 c_break_label = c_cont_label = size_zero_node;
5705
5706 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5707
5708 /* If the declarator is not suitable for a function definition,
5709 cause a syntax error. */
5710 if (decl1 == 0)
5711 return 0;
5712
5713 decl_attributes (&decl1, attributes, 0);
5714
5715 if (DECL_DECLARED_INLINE_P (decl1)
5716 && DECL_UNINLINABLE (decl1)
5717 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5718 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5719
5720 announce_function (decl1);
5721
5722 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5723 {
5724 error ("return type is an incomplete type");
5725 /* Make it return void instead. */
5726 TREE_TYPE (decl1)
5727 = build_function_type (void_type_node,
5728 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5729 }
5730
5731 if (warn_about_return_type)
5732 pedwarn_c99 ("return type defaults to %<int%>");
5733
5734 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5735 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5736 DECL_INITIAL (decl1) = error_mark_node;
5737
5738 /* If this definition isn't a prototype and we had a prototype declaration
5739 before, copy the arg type info from that prototype. */
5740 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5741 current_function_prototype_locus = UNKNOWN_LOCATION;
5742 current_function_prototype_built_in = false;
5743 current_function_prototype_arg_types = NULL_TREE;
5744 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5745 {
5746 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5747 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5748 TREE_TYPE (TREE_TYPE (old_decl))))
5749 {
5750 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5751 TREE_TYPE (decl1));
5752 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5753 current_function_prototype_built_in
5754 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5755 current_function_prototype_arg_types
5756 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5757 }
5758 if (TREE_PUBLIC (decl1))
5759 {
5760 /* If there is an external prototype declaration of this
5761 function, record its location but do not copy information
5762 to this decl. This may be an invisible declaration
5763 (built-in or in a scope which has finished) or simply
5764 have more refined argument types than any declaration
5765 found above. */
5766 struct c_binding *b;
5767 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5768 if (B_IN_SCOPE (b, external_scope))
5769 break;
5770 if (b)
5771 {
5772 tree ext_decl, ext_type;
5773 ext_decl = b->decl;
5774 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5775 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5776 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5777 TREE_TYPE (ext_type)))
5778 {
5779 current_function_prototype_locus
5780 = DECL_SOURCE_LOCATION (ext_decl);
5781 current_function_prototype_built_in
5782 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5783 current_function_prototype_arg_types
5784 = TYPE_ARG_TYPES (ext_type);
5785 }
5786 }
5787 }
5788 }
5789
5790 /* Optionally warn of old-fashioned def with no previous prototype. */
5791 if (warn_strict_prototypes
5792 && old_decl != error_mark_node
5793 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5794 && C_DECL_ISNT_PROTOTYPE (old_decl))
5795 warning (0, "function declaration isn%'t a prototype");
5796 /* Optionally warn of any global def with no previous prototype. */
5797 else if (warn_missing_prototypes
5798 && old_decl != error_mark_node
5799 && TREE_PUBLIC (decl1)
5800 && !MAIN_NAME_P (DECL_NAME (decl1))
5801 && C_DECL_ISNT_PROTOTYPE (old_decl))
5802 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5803 /* Optionally warn of any def with no previous prototype
5804 if the function has already been used. */
5805 else if (warn_missing_prototypes
5806 && old_decl != 0
5807 && old_decl != error_mark_node
5808 && TREE_USED (old_decl)
5809 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5810 warning (0, "%J%qD was used with no prototype before its definition",
5811 decl1, decl1);
5812 /* Optionally warn of any global def with no previous declaration. */
5813 else if (warn_missing_declarations
5814 && TREE_PUBLIC (decl1)
5815 && old_decl == 0
5816 && !MAIN_NAME_P (DECL_NAME (decl1)))
5817 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5818 /* Optionally warn of any def with no previous declaration
5819 if the function has already been used. */
5820 else if (warn_missing_declarations
5821 && old_decl != 0
5822 && old_decl != error_mark_node
5823 && TREE_USED (old_decl)
5824 && C_DECL_IMPLICIT (old_decl))
5825 warning (0, "%J%qD was used with no declaration before its definition",
5826 decl1, decl1);
5827
5828 /* This is a definition, not a reference.
5829 So normally clear DECL_EXTERNAL.
5830 However, `extern inline' acts like a declaration
5831 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5832 DECL_EXTERNAL (decl1) = current_extern_inline;
5833
5834 /* This function exists in static storage.
5835 (This does not mean `static' in the C sense!) */
5836 TREE_STATIC (decl1) = 1;
5837
5838 /* A nested function is not global. */
5839 if (current_function_decl != 0)
5840 TREE_PUBLIC (decl1) = 0;
5841
5842 /* This is the earliest point at which we might know the assembler
5843 name of the function. Thus, if it's set before this, die horribly. */
5844 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5845
5846 /* If #pragma weak was used, mark the decl weak now. */
5847 if (current_scope == file_scope)
5848 maybe_apply_pragma_weak (decl1);
5849
5850 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5851 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5852 {
5853 tree args;
5854 int argct = 0;
5855
5856 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5857 != integer_type_node)
5858 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5859
5860 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5861 args = TREE_CHAIN (args))
5862 {
5863 tree type = args ? TREE_VALUE (args) : 0;
5864
5865 if (type == void_type_node)
5866 break;
5867
5868 ++argct;
5869 switch (argct)
5870 {
5871 case 1:
5872 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5873 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5874 decl1, decl1);
5875 break;
5876
5877 case 2:
5878 if (TREE_CODE (type) != POINTER_TYPE
5879 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5880 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5881 != char_type_node))
5882 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5883 decl1, decl1);
5884 break;
5885
5886 case 3:
5887 if (TREE_CODE (type) != POINTER_TYPE
5888 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5889 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5890 != char_type_node))
5891 pedwarn ("%Jthird argument of %qD should probably be "
5892 "%<char **%>", decl1, decl1);
5893 break;
5894 }
5895 }
5896
5897 /* It is intentional that this message does not mention the third
5898 argument because it's only mentioned in an appendix of the
5899 standard. */
5900 if (argct > 0 && (argct < 2 || argct > 3))
5901 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5902
5903 if (!TREE_PUBLIC (decl1))
5904 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5905 }
5906
5907 /* Record the decl so that the function name is defined.
5908 If we already have a decl for this name, and it is a FUNCTION_DECL,
5909 use the old decl. */
5910
5911 current_function_decl = pushdecl (decl1);
5912
5913 push_scope ();
5914 declare_parm_level ();
5915
5916 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5917 /* Promote the value to int before returning it. */
5918 if (c_promoting_integer_type_p (restype))
5919 {
5920 /* It retains unsignedness if not really getting wider. */
5921 if (TYPE_UNSIGNED (restype)
5922 && (TYPE_PRECISION (restype)
5923 == TYPE_PRECISION (integer_type_node)))
5924 restype = unsigned_type_node;
5925 else
5926 restype = integer_type_node;
5927 }
5928
5929 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5930 DECL_ARTIFICIAL (resdecl) = 1;
5931 DECL_IGNORED_P (resdecl) = 1;
5932 DECL_RESULT (current_function_decl) = resdecl;
5933
5934 start_fname_decls ();
5935
5936 return 1;
5937 }
5938 \f
5939 /* Subroutine of store_parm_decls which handles new-style function
5940 definitions (prototype format). The parms already have decls, so we
5941 need only record them as in effect and complain if any redundant
5942 old-style parm decls were written. */
5943 static void
5944 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5945 {
5946 tree decl;
5947
5948 if (current_scope->bindings)
5949 {
5950 error ("%Jold-style parameter declarations in prototyped "
5951 "function definition", fndecl);
5952
5953 /* Get rid of the old-style declarations. */
5954 pop_scope ();
5955 push_scope ();
5956 }
5957 /* Don't issue this warning for nested functions, and don't issue this
5958 warning if we got here because ARG_INFO_TYPES was error_mark_node
5959 (this happens when a function definition has just an ellipsis in
5960 its parameter list). */
5961 else if (warn_traditional && !in_system_header && !current_function_scope
5962 && arg_info->types != error_mark_node)
5963 warning (0, "%Jtraditional C rejects ISO C style function definitions",
5964 fndecl);
5965
5966 /* Now make all the parameter declarations visible in the function body.
5967 We can bypass most of the grunt work of pushdecl. */
5968 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5969 {
5970 DECL_CONTEXT (decl) = current_function_decl;
5971 if (DECL_NAME (decl))
5972 bind (DECL_NAME (decl), decl, current_scope,
5973 /*invisible=*/false, /*nested=*/false);
5974 else
5975 error ("%Jparameter name omitted", decl);
5976 }
5977
5978 /* Record the parameter list in the function declaration. */
5979 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5980
5981 /* Now make all the ancillary declarations visible, likewise. */
5982 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5983 {
5984 DECL_CONTEXT (decl) = current_function_decl;
5985 if (DECL_NAME (decl))
5986 bind (DECL_NAME (decl), decl, current_scope,
5987 /*invisible=*/false, /*nested=*/false);
5988 }
5989
5990 /* And all the tag declarations. */
5991 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5992 if (TREE_PURPOSE (decl))
5993 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5994 /*invisible=*/false, /*nested=*/false);
5995 }
5996
5997 /* Subroutine of store_parm_decls which handles old-style function
5998 definitions (separate parameter list and declarations). */
5999
6000 static void
6001 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6002 {
6003 struct c_binding *b;
6004 tree parm, decl, last;
6005 tree parmids = arg_info->parms;
6006
6007 /* We use DECL_WEAK as a flag to show which parameters have been
6008 seen already, since it is not used on PARM_DECL. */
6009 #ifdef ENABLE_CHECKING
6010 for (b = current_scope->bindings; b; b = b->prev)
6011 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6012 #endif
6013
6014 if (warn_old_style_definition && !in_system_header)
6015 warning (0, "%Jold-style function definition", fndecl);
6016
6017 /* Match each formal parameter name with its declaration. Save each
6018 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6019 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6020 {
6021 if (TREE_VALUE (parm) == 0)
6022 {
6023 error ("%Jparameter name missing from parameter list", fndecl);
6024 TREE_PURPOSE (parm) = 0;
6025 continue;
6026 }
6027
6028 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6029 if (b && B_IN_CURRENT_SCOPE (b))
6030 {
6031 decl = b->decl;
6032 /* If we got something other than a PARM_DECL it is an error. */
6033 if (TREE_CODE (decl) != PARM_DECL)
6034 error ("%J%qD declared as a non-parameter", decl, decl);
6035 /* If the declaration is already marked, we have a duplicate
6036 name. Complain and ignore the duplicate. */
6037 else if (DECL_WEAK (decl))
6038 {
6039 error ("%Jmultiple parameters named %qD", decl, decl);
6040 TREE_PURPOSE (parm) = 0;
6041 continue;
6042 }
6043 /* If the declaration says "void", complain and turn it into
6044 an int. */
6045 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6046 {
6047 error ("%Jparameter %qD declared with void type", decl, decl);
6048 TREE_TYPE (decl) = integer_type_node;
6049 DECL_ARG_TYPE (decl) = integer_type_node;
6050 layout_decl (decl, 0);
6051 }
6052 }
6053 /* If no declaration found, default to int. */
6054 else
6055 {
6056 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6057 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6058 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6059 pushdecl (decl);
6060
6061 if (flag_isoc99)
6062 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6063 else if (extra_warnings)
6064 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6065 }
6066
6067 TREE_PURPOSE (parm) = decl;
6068 DECL_WEAK (decl) = 1;
6069 }
6070
6071 /* Now examine the parms chain for incomplete declarations
6072 and declarations with no corresponding names. */
6073
6074 for (b = current_scope->bindings; b; b = b->prev)
6075 {
6076 parm = b->decl;
6077 if (TREE_CODE (parm) != PARM_DECL)
6078 continue;
6079
6080 if (TREE_TYPE (parm) != error_mark_node
6081 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6082 {
6083 error ("%Jparameter %qD has incomplete type", parm, parm);
6084 TREE_TYPE (parm) = error_mark_node;
6085 }
6086
6087 if (!DECL_WEAK (parm))
6088 {
6089 error ("%Jdeclaration for parameter %qD but no such parameter",
6090 parm, parm);
6091
6092 /* Pretend the parameter was not missing.
6093 This gets us to a standard state and minimizes
6094 further error messages. */
6095 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6096 }
6097 }
6098
6099 /* Chain the declarations together in the order of the list of
6100 names. Store that chain in the function decl, replacing the
6101 list of names. Update the current scope to match. */
6102 DECL_ARGUMENTS (fndecl) = 0;
6103
6104 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6105 if (TREE_PURPOSE (parm))
6106 break;
6107 if (parm && TREE_PURPOSE (parm))
6108 {
6109 last = TREE_PURPOSE (parm);
6110 DECL_ARGUMENTS (fndecl) = last;
6111 DECL_WEAK (last) = 0;
6112
6113 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6114 if (TREE_PURPOSE (parm))
6115 {
6116 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6117 last = TREE_PURPOSE (parm);
6118 DECL_WEAK (last) = 0;
6119 }
6120 TREE_CHAIN (last) = 0;
6121 }
6122
6123 /* If there was a previous prototype,
6124 set the DECL_ARG_TYPE of each argument according to
6125 the type previously specified, and report any mismatches. */
6126
6127 if (current_function_prototype_arg_types)
6128 {
6129 tree type;
6130 for (parm = DECL_ARGUMENTS (fndecl),
6131 type = current_function_prototype_arg_types;
6132 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6133 != void_type_node));
6134 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6135 {
6136 if (parm == 0 || type == 0
6137 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6138 {
6139 if (current_function_prototype_built_in)
6140 warning (0, "number of arguments doesn%'t match "
6141 "built-in prototype");
6142 else
6143 {
6144 error ("number of arguments doesn%'t match prototype");
6145 error ("%Hprototype declaration",
6146 &current_function_prototype_locus);
6147 }
6148 break;
6149 }
6150 /* Type for passing arg must be consistent with that
6151 declared for the arg. ISO C says we take the unqualified
6152 type for parameters declared with qualified type. */
6153 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6154 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6155 {
6156 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6157 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6158 {
6159 /* Adjust argument to match prototype. E.g. a previous
6160 `int foo(float);' prototype causes
6161 `int foo(x) float x; {...}' to be treated like
6162 `int foo(float x) {...}'. This is particularly
6163 useful for argument types like uid_t. */
6164 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6165
6166 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6167 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6168 && TYPE_PRECISION (TREE_TYPE (parm))
6169 < TYPE_PRECISION (integer_type_node))
6170 DECL_ARG_TYPE (parm) = integer_type_node;
6171
6172 if (pedantic)
6173 {
6174 /* ??? Is it possible to get here with a
6175 built-in prototype or will it always have
6176 been diagnosed as conflicting with an
6177 old-style definition and discarded? */
6178 if (current_function_prototype_built_in)
6179 warning (0, "promoted argument %qD "
6180 "doesn%'t match built-in prototype", parm);
6181 else
6182 {
6183 pedwarn ("promoted argument %qD "
6184 "doesn%'t match prototype", parm);
6185 pedwarn ("%Hprototype declaration",
6186 &current_function_prototype_locus);
6187 }
6188 }
6189 }
6190 else
6191 {
6192 if (current_function_prototype_built_in)
6193 warning (0, "argument %qD doesn%'t match "
6194 "built-in prototype", parm);
6195 else
6196 {
6197 error ("argument %qD doesn%'t match prototype", parm);
6198 error ("%Hprototype declaration",
6199 &current_function_prototype_locus);
6200 }
6201 }
6202 }
6203 }
6204 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6205 }
6206
6207 /* Otherwise, create a prototype that would match. */
6208
6209 else
6210 {
6211 tree actual = 0, last = 0, type;
6212
6213 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6214 {
6215 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6216 if (last)
6217 TREE_CHAIN (last) = type;
6218 else
6219 actual = type;
6220 last = type;
6221 }
6222 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6223 if (last)
6224 TREE_CHAIN (last) = type;
6225 else
6226 actual = type;
6227
6228 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6229 of the type of this function, but we need to avoid having this
6230 affect the types of other similarly-typed functions, so we must
6231 first force the generation of an identical (but separate) type
6232 node for the relevant function type. The new node we create
6233 will be a variant of the main variant of the original function
6234 type. */
6235
6236 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6237
6238 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6239 }
6240 }
6241
6242 /* Store parameter declarations passed in ARG_INFO into the current
6243 function declaration. */
6244
6245 void
6246 store_parm_decls_from (struct c_arg_info *arg_info)
6247 {
6248 current_function_arg_info = arg_info;
6249 store_parm_decls ();
6250 }
6251
6252 /* Store the parameter declarations into the current function declaration.
6253 This is called after parsing the parameter declarations, before
6254 digesting the body of the function.
6255
6256 For an old-style definition, construct a prototype out of the old-style
6257 parameter declarations and inject it into the function's type. */
6258
6259 void
6260 store_parm_decls (void)
6261 {
6262 tree fndecl = current_function_decl;
6263 bool proto;
6264
6265 /* The argument information block for FNDECL. */
6266 struct c_arg_info *arg_info = current_function_arg_info;
6267 current_function_arg_info = 0;
6268
6269 /* True if this definition is written with a prototype. Note:
6270 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6271 list in a function definition as equivalent to (void) -- an
6272 empty argument list specifies the function has no parameters,
6273 but only (void) sets up a prototype for future calls. */
6274 proto = arg_info->types != 0;
6275
6276 if (proto)
6277 store_parm_decls_newstyle (fndecl, arg_info);
6278 else
6279 store_parm_decls_oldstyle (fndecl, arg_info);
6280
6281 /* The next call to push_scope will be a function body. */
6282
6283 next_is_function_body = true;
6284
6285 /* Write a record describing this function definition to the prototypes
6286 file (if requested). */
6287
6288 gen_aux_info_record (fndecl, 1, 0, proto);
6289
6290 /* Initialize the RTL code for the function. */
6291 allocate_struct_function (fndecl);
6292
6293 /* Begin the statement tree for this function. */
6294 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6295
6296 /* ??? Insert the contents of the pending sizes list into the function
6297 to be evaluated. The only reason left to have this is
6298 void foo(int n, int array[n++])
6299 because we throw away the array type in favor of a pointer type, and
6300 thus won't naturally see the SAVE_EXPR containing the increment. All
6301 other pending sizes would be handled by gimplify_parameters. */
6302 {
6303 tree t;
6304 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6305 add_stmt (TREE_VALUE (t));
6306 }
6307
6308 /* Even though we're inside a function body, we still don't want to
6309 call expand_expr to calculate the size of a variable-sized array.
6310 We haven't necessarily assigned RTL to all variables yet, so it's
6311 not safe to try to expand expressions involving them. */
6312 cfun->x_dont_save_pending_sizes_p = 1;
6313 }
6314 \f
6315 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6316 functions. */
6317
6318 static void
6319 c_warn_unused_result_recursively (tree fndecl)
6320 {
6321 struct cgraph_node *cgn;
6322
6323 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6324 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6325
6326 /* Finalize all nested functions now. */
6327 cgn = cgraph_node (fndecl);
6328 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6329 c_warn_unused_result_recursively (cgn->decl);
6330 }
6331
6332 /* Finish up a function declaration and compile that function
6333 all the way to assembler language output. The free the storage
6334 for the function definition.
6335
6336 This is called after parsing the body of the function definition. */
6337
6338 void
6339 finish_function (void)
6340 {
6341 tree fndecl = current_function_decl;
6342
6343 label_context_stack_se = label_context_stack_se->next;
6344 label_context_stack_vm = label_context_stack_vm->next;
6345
6346 if (TREE_CODE (fndecl) == FUNCTION_DECL
6347 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6348 {
6349 tree args = DECL_ARGUMENTS (fndecl);
6350 for (; args; args = TREE_CHAIN (args))
6351 {
6352 tree type = TREE_TYPE (args);
6353 if (INTEGRAL_TYPE_P (type)
6354 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6355 DECL_ARG_TYPE (args) = integer_type_node;
6356 }
6357 }
6358
6359 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6360 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6361
6362 /* Must mark the RESULT_DECL as being in this function. */
6363
6364 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6365 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6366
6367 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6368 {
6369 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6370 != integer_type_node)
6371 {
6372 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6373 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6374 if (!warn_main)
6375 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6376 }
6377 else
6378 {
6379 if (flag_isoc99)
6380 {
6381 tree stmt = c_finish_return (integer_zero_node);
6382 #ifdef USE_MAPPED_LOCATION
6383 /* Hack. We don't want the middle-end to warn that this return
6384 is unreachable, so we mark its location as special. Using
6385 UNKNOWN_LOCATION has the problem that it gets clobbered in
6386 annotate_one_with_locus. A cleaner solution might be to
6387 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6388 */
6389 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6390 #else
6391 /* Hack. We don't want the middle-end to warn that this
6392 return is unreachable, so put the statement on the
6393 special line 0. */
6394 annotate_with_file_line (stmt, input_filename, 0);
6395 #endif
6396 }
6397 }
6398 }
6399
6400 /* Tie off the statement tree for this function. */
6401 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6402
6403 finish_fname_decls ();
6404
6405 /* Complain if there's just no return statement. */
6406 if (warn_return_type
6407 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6408 && !current_function_returns_value && !current_function_returns_null
6409 /* Don't complain if we are no-return. */
6410 && !current_function_returns_abnormally
6411 /* Don't warn for main(). */
6412 && !MAIN_NAME_P (DECL_NAME (fndecl))
6413 /* Or if they didn't actually specify a return type. */
6414 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6415 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6416 inline function, as we might never be compiled separately. */
6417 && DECL_INLINE (fndecl))
6418 warning (0, "no return statement in function returning non-void");
6419
6420 /* With just -Wextra, complain only if function returns both with
6421 and without a value. */
6422 if (extra_warnings
6423 && current_function_returns_value
6424 && current_function_returns_null)
6425 warning (0, "this function may return with or without a value");
6426
6427 /* Store the end of the function, so that we get good line number
6428 info for the epilogue. */
6429 cfun->function_end_locus = input_location;
6430
6431 /* If we don't have ctors/dtors sections, and this is a static
6432 constructor or destructor, it must be recorded now. */
6433 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6434 && !targetm.have_ctors_dtors)
6435 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6436 if (DECL_STATIC_DESTRUCTOR (fndecl)
6437 && !targetm.have_ctors_dtors)
6438 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6439
6440 /* Finalize the ELF visibility for the function. */
6441 c_determine_visibility (fndecl);
6442
6443 /* Genericize before inlining. Delay genericizing nested functions
6444 until their parent function is genericized. Since finalizing
6445 requires GENERIC, delay that as well. */
6446
6447 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6448 && !undef_nested_function)
6449 {
6450 if (!decl_function_context (fndecl))
6451 {
6452 c_genericize (fndecl);
6453 c_warn_unused_result_recursively (fndecl);
6454
6455 /* ??? Objc emits functions after finalizing the compilation unit.
6456 This should be cleaned up later and this conditional removed. */
6457 if (cgraph_global_info_ready)
6458 {
6459 c_expand_body (fndecl);
6460 return;
6461 }
6462
6463 cgraph_finalize_function (fndecl, false);
6464 }
6465 else
6466 {
6467 /* Register this function with cgraph just far enough to get it
6468 added to our parent's nested function list. Handy, since the
6469 C front end doesn't have such a list. */
6470 (void) cgraph_node (fndecl);
6471 }
6472 }
6473
6474 if (!decl_function_context (fndecl))
6475 undef_nested_function = false;
6476
6477 /* We're leaving the context of this function, so zap cfun.
6478 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6479 tree_rest_of_compilation. */
6480 cfun = NULL;
6481 current_function_decl = NULL;
6482 }
6483
6484 /* Generate the RTL for the body of FNDECL. */
6485
6486 void
6487 c_expand_body (tree fndecl)
6488 {
6489
6490 if (!DECL_INITIAL (fndecl)
6491 || DECL_INITIAL (fndecl) == error_mark_node)
6492 return;
6493
6494 tree_rest_of_compilation (fndecl);
6495
6496 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6497 && targetm.have_ctors_dtors)
6498 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6499 DEFAULT_INIT_PRIORITY);
6500 if (DECL_STATIC_DESTRUCTOR (fndecl)
6501 && targetm.have_ctors_dtors)
6502 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6503 DEFAULT_INIT_PRIORITY);
6504 }
6505 \f
6506 /* Check the declarations given in a for-loop for satisfying the C99
6507 constraints. */
6508 void
6509 check_for_loop_decls (void)
6510 {
6511 struct c_binding *b;
6512
6513 if (!flag_isoc99)
6514 {
6515 /* If we get here, declarations have been used in a for loop without
6516 the C99 for loop scope. This doesn't make much sense, so don't
6517 allow it. */
6518 error ("%<for%> loop initial declaration used outside C99 mode");
6519 return;
6520 }
6521 /* C99 subclause 6.8.5 paragraph 3:
6522
6523 [#3] The declaration part of a for statement shall only
6524 declare identifiers for objects having storage class auto or
6525 register.
6526
6527 It isn't clear whether, in this sentence, "identifiers" binds to
6528 "shall only declare" or to "objects" - that is, whether all identifiers
6529 declared must be identifiers for objects, or whether the restriction
6530 only applies to those that are. (A question on this in comp.std.c
6531 in November 2000 received no answer.) We implement the strictest
6532 interpretation, to avoid creating an extension which later causes
6533 problems. */
6534
6535 for (b = current_scope->bindings; b; b = b->prev)
6536 {
6537 tree id = b->id;
6538 tree decl = b->decl;
6539
6540 if (!id)
6541 continue;
6542
6543 switch (TREE_CODE (decl))
6544 {
6545 case VAR_DECL:
6546 if (TREE_STATIC (decl))
6547 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6548 "initial declaration", decl, decl);
6549 else if (DECL_EXTERNAL (decl))
6550 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6551 "initial declaration", decl, decl);
6552 break;
6553
6554 case RECORD_TYPE:
6555 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6556 id);
6557 break;
6558 case UNION_TYPE:
6559 error ("%<union %E%> declared in %<for%> loop initial declaration",
6560 id);
6561 break;
6562 case ENUMERAL_TYPE:
6563 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6564 id);
6565 break;
6566 default:
6567 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6568 "initial declaration", decl, decl);
6569 }
6570 }
6571 }
6572 \f
6573 /* Save and reinitialize the variables
6574 used during compilation of a C function. */
6575
6576 void
6577 c_push_function_context (struct function *f)
6578 {
6579 struct language_function *p;
6580 p = GGC_NEW (struct language_function);
6581 f->language = p;
6582
6583 p->base.x_stmt_tree = c_stmt_tree;
6584 p->x_break_label = c_break_label;
6585 p->x_cont_label = c_cont_label;
6586 p->x_switch_stack = c_switch_stack;
6587 p->arg_info = current_function_arg_info;
6588 p->returns_value = current_function_returns_value;
6589 p->returns_null = current_function_returns_null;
6590 p->returns_abnormally = current_function_returns_abnormally;
6591 p->warn_about_return_type = warn_about_return_type;
6592 p->extern_inline = current_extern_inline;
6593 }
6594
6595 /* Restore the variables used during compilation of a C function. */
6596
6597 void
6598 c_pop_function_context (struct function *f)
6599 {
6600 struct language_function *p = f->language;
6601
6602 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6603 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6604 {
6605 /* Stop pointing to the local nodes about to be freed. */
6606 /* But DECL_INITIAL must remain nonzero so we know this
6607 was an actual function definition. */
6608 DECL_INITIAL (current_function_decl) = error_mark_node;
6609 DECL_ARGUMENTS (current_function_decl) = 0;
6610 }
6611
6612 c_stmt_tree = p->base.x_stmt_tree;
6613 c_break_label = p->x_break_label;
6614 c_cont_label = p->x_cont_label;
6615 c_switch_stack = p->x_switch_stack;
6616 current_function_arg_info = p->arg_info;
6617 current_function_returns_value = p->returns_value;
6618 current_function_returns_null = p->returns_null;
6619 current_function_returns_abnormally = p->returns_abnormally;
6620 warn_about_return_type = p->warn_about_return_type;
6621 current_extern_inline = p->extern_inline;
6622
6623 f->language = NULL;
6624 }
6625
6626 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6627
6628 void
6629 c_dup_lang_specific_decl (tree decl)
6630 {
6631 struct lang_decl *ld;
6632
6633 if (!DECL_LANG_SPECIFIC (decl))
6634 return;
6635
6636 ld = GGC_NEW (struct lang_decl);
6637 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6638 DECL_LANG_SPECIFIC (decl) = ld;
6639 }
6640
6641 /* The functions below are required for functionality of doing
6642 function at once processing in the C front end. Currently these
6643 functions are not called from anywhere in the C front end, but as
6644 these changes continue, that will change. */
6645
6646 /* Returns nonzero if the current statement is a full expression,
6647 i.e. temporaries created during that statement should be destroyed
6648 at the end of the statement. */
6649
6650 int
6651 stmts_are_full_exprs_p (void)
6652 {
6653 return 0;
6654 }
6655
6656 /* Returns the stmt_tree (if any) to which statements are currently
6657 being added. If there is no active statement-tree, NULL is
6658 returned. */
6659
6660 stmt_tree
6661 current_stmt_tree (void)
6662 {
6663 return &c_stmt_tree;
6664 }
6665
6666 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6667 C. */
6668
6669 int
6670 anon_aggr_type_p (tree ARG_UNUSED (node))
6671 {
6672 return 0;
6673 }
6674
6675 /* Return the global value of T as a symbol. */
6676
6677 tree
6678 identifier_global_value (tree t)
6679 {
6680 struct c_binding *b;
6681
6682 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6683 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6684 return b->decl;
6685
6686 return 0;
6687 }
6688
6689 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6690 otherwise the name is found in ridpointers from RID_INDEX. */
6691
6692 void
6693 record_builtin_type (enum rid rid_index, const char *name, tree type)
6694 {
6695 tree id, decl;
6696 if (name == 0)
6697 id = ridpointers[(int) rid_index];
6698 else
6699 id = get_identifier (name);
6700 decl = build_decl (TYPE_DECL, id, type);
6701 pushdecl (decl);
6702 if (debug_hooks->type_decl)
6703 debug_hooks->type_decl (decl, false);
6704 }
6705
6706 /* Build the void_list_node (void_type_node having been created). */
6707 tree
6708 build_void_list_node (void)
6709 {
6710 tree t = build_tree_list (NULL_TREE, void_type_node);
6711 return t;
6712 }
6713
6714 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6715
6716 struct c_parm *
6717 build_c_parm (struct c_declspecs *specs, tree attrs,
6718 struct c_declarator *declarator)
6719 {
6720 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6721 ret->specs = specs;
6722 ret->attrs = attrs;
6723 ret->declarator = declarator;
6724 return ret;
6725 }
6726
6727 /* Return a declarator with nested attributes. TARGET is the inner
6728 declarator to which these attributes apply. ATTRS are the
6729 attributes. */
6730
6731 struct c_declarator *
6732 build_attrs_declarator (tree attrs, struct c_declarator *target)
6733 {
6734 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6735 ret->kind = cdk_attrs;
6736 ret->declarator = target;
6737 ret->u.attrs = attrs;
6738 return ret;
6739 }
6740
6741 /* Return a declarator for a function with arguments specified by ARGS
6742 and return type specified by TARGET. */
6743
6744 struct c_declarator *
6745 build_function_declarator (struct c_arg_info *args,
6746 struct c_declarator *target)
6747 {
6748 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6749 ret->kind = cdk_function;
6750 ret->declarator = target;
6751 ret->u.arg_info = args;
6752 return ret;
6753 }
6754
6755 /* Return a declarator for the identifier IDENT (which may be
6756 NULL_TREE for an abstract declarator). */
6757
6758 struct c_declarator *
6759 build_id_declarator (tree ident)
6760 {
6761 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6762 ret->kind = cdk_id;
6763 ret->declarator = 0;
6764 ret->u.id = ident;
6765 /* Default value - may get reset to a more precise location. */
6766 ret->id_loc = input_location;
6767 return ret;
6768 }
6769
6770 /* Return something to represent absolute declarators containing a *.
6771 TARGET is the absolute declarator that the * contains.
6772 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6773 to apply to the pointer type. */
6774
6775 struct c_declarator *
6776 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6777 struct c_declarator *target)
6778 {
6779 tree attrs;
6780 int quals = 0;
6781 struct c_declarator *itarget = target;
6782 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6783 if (type_quals_attrs)
6784 {
6785 attrs = type_quals_attrs->attrs;
6786 quals = quals_from_declspecs (type_quals_attrs);
6787 if (attrs != NULL_TREE)
6788 itarget = build_attrs_declarator (attrs, target);
6789 }
6790 ret->kind = cdk_pointer;
6791 ret->declarator = itarget;
6792 ret->u.pointer_quals = quals;
6793 return ret;
6794 }
6795
6796 /* Return a pointer to a structure for an empty list of declaration
6797 specifiers. */
6798
6799 struct c_declspecs *
6800 build_null_declspecs (void)
6801 {
6802 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6803 ret->type = 0;
6804 ret->decl_attr = 0;
6805 ret->attrs = 0;
6806 ret->typespec_word = cts_none;
6807 ret->storage_class = csc_none;
6808 ret->declspecs_seen_p = false;
6809 ret->type_seen_p = false;
6810 ret->non_sc_seen_p = false;
6811 ret->typedef_p = false;
6812 ret->tag_defined_p = false;
6813 ret->explicit_signed_p = false;
6814 ret->deprecated_p = false;
6815 ret->default_int_p = false;
6816 ret->long_p = false;
6817 ret->long_long_p = false;
6818 ret->short_p = false;
6819 ret->signed_p = false;
6820 ret->unsigned_p = false;
6821 ret->complex_p = false;
6822 ret->inline_p = false;
6823 ret->thread_p = false;
6824 ret->const_p = false;
6825 ret->volatile_p = false;
6826 ret->restrict_p = false;
6827 return ret;
6828 }
6829
6830 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6831 returning SPECS. */
6832
6833 struct c_declspecs *
6834 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6835 {
6836 enum rid i;
6837 bool dupe = false;
6838 specs->non_sc_seen_p = true;
6839 specs->declspecs_seen_p = true;
6840 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6841 && C_IS_RESERVED_WORD (qual));
6842 i = C_RID_CODE (qual);
6843 switch (i)
6844 {
6845 case RID_CONST:
6846 dupe = specs->const_p;
6847 specs->const_p = true;
6848 break;
6849 case RID_VOLATILE:
6850 dupe = specs->volatile_p;
6851 specs->volatile_p = true;
6852 break;
6853 case RID_RESTRICT:
6854 dupe = specs->restrict_p;
6855 specs->restrict_p = true;
6856 break;
6857 default:
6858 gcc_unreachable ();
6859 }
6860 if (dupe && pedantic && !flag_isoc99)
6861 pedwarn ("duplicate %qE", qual);
6862 return specs;
6863 }
6864
6865 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6866 returning SPECS. */
6867
6868 struct c_declspecs *
6869 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6870 {
6871 tree type = spec.spec;
6872 specs->non_sc_seen_p = true;
6873 specs->declspecs_seen_p = true;
6874 specs->type_seen_p = true;
6875 if (TREE_DEPRECATED (type))
6876 specs->deprecated_p = true;
6877
6878 /* Handle type specifier keywords. */
6879 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6880 {
6881 enum rid i = C_RID_CODE (type);
6882 if (specs->type)
6883 {
6884 error ("two or more data types in declaration specifiers");
6885 return specs;
6886 }
6887 if ((int) i <= (int) RID_LAST_MODIFIER)
6888 {
6889 /* "long", "short", "signed", "unsigned" or "_Complex". */
6890 bool dupe = false;
6891 switch (i)
6892 {
6893 case RID_LONG:
6894 if (specs->long_long_p)
6895 {
6896 error ("%<long long long%> is too long for GCC");
6897 break;
6898 }
6899 if (specs->long_p)
6900 {
6901 if (specs->typespec_word == cts_double)
6902 {
6903 error ("both %<long long%> and %<double%> in "
6904 "declaration specifiers");
6905 break;
6906 }
6907 if (pedantic && !flag_isoc99 && !in_system_header
6908 && warn_long_long)
6909 pedwarn ("ISO C90 does not support %<long long%>");
6910 specs->long_long_p = 1;
6911 break;
6912 }
6913 if (specs->short_p)
6914 error ("both %<long%> and %<short%> in "
6915 "declaration specifiers");
6916 else if (specs->typespec_word == cts_void)
6917 error ("both %<long%> and %<void%> in "
6918 "declaration specifiers");
6919 else if (specs->typespec_word == cts_bool)
6920 error ("both %<long%> and %<_Bool%> in "
6921 "declaration specifiers");
6922 else if (specs->typespec_word == cts_char)
6923 error ("both %<long%> and %<char%> in "
6924 "declaration specifiers");
6925 else if (specs->typespec_word == cts_float)
6926 error ("both %<long%> and %<float%> in "
6927 "declaration specifiers");
6928 else
6929 specs->long_p = true;
6930 break;
6931 case RID_SHORT:
6932 dupe = specs->short_p;
6933 if (specs->long_p)
6934 error ("both %<long%> and %<short%> in "
6935 "declaration specifiers");
6936 else if (specs->typespec_word == cts_void)
6937 error ("both %<short%> and %<void%> in "
6938 "declaration specifiers");
6939 else if (specs->typespec_word == cts_bool)
6940 error ("both %<short%> and %<_Bool%> in "
6941 "declaration specifiers");
6942 else if (specs->typespec_word == cts_char)
6943 error ("both %<short%> and %<char%> in "
6944 "declaration specifiers");
6945 else if (specs->typespec_word == cts_float)
6946 error ("both %<short%> and %<float%> in "
6947 "declaration specifiers");
6948 else if (specs->typespec_word == cts_double)
6949 error ("both %<short%> and %<double%> in "
6950 "declaration specifiers");
6951 else
6952 specs->short_p = true;
6953 break;
6954 case RID_SIGNED:
6955 dupe = specs->signed_p;
6956 if (specs->unsigned_p)
6957 error ("both %<signed%> and %<unsigned%> in "
6958 "declaration specifiers");
6959 else if (specs->typespec_word == cts_void)
6960 error ("both %<signed%> and %<void%> in "
6961 "declaration specifiers");
6962 else if (specs->typespec_word == cts_bool)
6963 error ("both %<signed%> and %<_Bool%> in "
6964 "declaration specifiers");
6965 else if (specs->typespec_word == cts_float)
6966 error ("both %<signed%> and %<float%> in "
6967 "declaration specifiers");
6968 else if (specs->typespec_word == cts_double)
6969 error ("both %<signed%> and %<double%> in "
6970 "declaration specifiers");
6971 else
6972 specs->signed_p = true;
6973 break;
6974 case RID_UNSIGNED:
6975 dupe = specs->unsigned_p;
6976 if (specs->signed_p)
6977 error ("both %<signed%> and %<unsigned%> in "
6978 "declaration specifiers");
6979 else if (specs->typespec_word == cts_void)
6980 error ("both %<unsigned%> and %<void%> in "
6981 "declaration specifiers");
6982 else if (specs->typespec_word == cts_bool)
6983 error ("both %<unsigned%> and %<_Bool%> in "
6984 "declaration specifiers");
6985 else if (specs->typespec_word == cts_float)
6986 error ("both %<unsigned%> and %<float%> in "
6987 "declaration specifiers");
6988 else if (specs->typespec_word == cts_double)
6989 error ("both %<unsigned%> and %<double%> in "
6990 "declaration specifiers");
6991 else
6992 specs->unsigned_p = true;
6993 break;
6994 case RID_COMPLEX:
6995 dupe = specs->complex_p;
6996 if (pedantic && !flag_isoc99 && !in_system_header)
6997 pedwarn ("ISO C90 does not support complex types");
6998 if (specs->typespec_word == cts_void)
6999 error ("both %<complex%> and %<void%> in "
7000 "declaration specifiers");
7001 else if (specs->typespec_word == cts_bool)
7002 error ("both %<complex%> and %<_Bool%> in "
7003 "declaration specifiers");
7004 else
7005 specs->complex_p = true;
7006 break;
7007 default:
7008 gcc_unreachable ();
7009 }
7010
7011 if (dupe)
7012 error ("duplicate %qE", type);
7013
7014 return specs;
7015 }
7016 else
7017 {
7018 /* "void", "_Bool", "char", "int", "float" or "double". */
7019 if (specs->typespec_word != cts_none)
7020 {
7021 error ("two or more data types in declaration specifiers");
7022 return specs;
7023 }
7024 switch (i)
7025 {
7026 case RID_VOID:
7027 if (specs->long_p)
7028 error ("both %<long%> and %<void%> in "
7029 "declaration specifiers");
7030 else if (specs->short_p)
7031 error ("both %<short%> and %<void%> in "
7032 "declaration specifiers");
7033 else if (specs->signed_p)
7034 error ("both %<signed%> and %<void%> in "
7035 "declaration specifiers");
7036 else if (specs->unsigned_p)
7037 error ("both %<unsigned%> and %<void%> in "
7038 "declaration specifiers");
7039 else if (specs->complex_p)
7040 error ("both %<complex%> and %<void%> in "
7041 "declaration specifiers");
7042 else
7043 specs->typespec_word = cts_void;
7044 return specs;
7045 case RID_BOOL:
7046 if (specs->long_p)
7047 error ("both %<long%> and %<_Bool%> in "
7048 "declaration specifiers");
7049 else if (specs->short_p)
7050 error ("both %<short%> and %<_Bool%> in "
7051 "declaration specifiers");
7052 else if (specs->signed_p)
7053 error ("both %<signed%> and %<_Bool%> in "
7054 "declaration specifiers");
7055 else if (specs->unsigned_p)
7056 error ("both %<unsigned%> and %<_Bool%> in "
7057 "declaration specifiers");
7058 else if (specs->complex_p)
7059 error ("both %<complex%> and %<_Bool%> in "
7060 "declaration specifiers");
7061 else
7062 specs->typespec_word = cts_bool;
7063 return specs;
7064 case RID_CHAR:
7065 if (specs->long_p)
7066 error ("both %<long%> and %<char%> in "
7067 "declaration specifiers");
7068 else if (specs->short_p)
7069 error ("both %<short%> and %<char%> in "
7070 "declaration specifiers");
7071 else
7072 specs->typespec_word = cts_char;
7073 return specs;
7074 case RID_INT:
7075 specs->typespec_word = cts_int;
7076 return specs;
7077 case RID_FLOAT:
7078 if (specs->long_p)
7079 error ("both %<long%> and %<float%> in "
7080 "declaration specifiers");
7081 else if (specs->short_p)
7082 error ("both %<short%> and %<float%> in "
7083 "declaration specifiers");
7084 else if (specs->signed_p)
7085 error ("both %<signed%> and %<float%> in "
7086 "declaration specifiers");
7087 else if (specs->unsigned_p)
7088 error ("both %<unsigned%> and %<float%> in "
7089 "declaration specifiers");
7090 else
7091 specs->typespec_word = cts_float;
7092 return specs;
7093 case RID_DOUBLE:
7094 if (specs->long_long_p)
7095 error ("both %<long long%> and %<double%> in "
7096 "declaration specifiers");
7097 else if (specs->short_p)
7098 error ("both %<short%> and %<double%> in "
7099 "declaration specifiers");
7100 else if (specs->signed_p)
7101 error ("both %<signed%> and %<double%> in "
7102 "declaration specifiers");
7103 else if (specs->unsigned_p)
7104 error ("both %<unsigned%> and %<double%> in "
7105 "declaration specifiers");
7106 else
7107 specs->typespec_word = cts_double;
7108 return specs;
7109 default:
7110 /* ObjC reserved word "id", handled below. */
7111 break;
7112 }
7113 }
7114 }
7115
7116 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7117 form of ObjC type, cases such as "int" and "long" being handled
7118 above), a TYPE (struct, union, enum and typeof specifiers) or an
7119 ERROR_MARK. In none of these cases may there have previously
7120 been any type specifiers. */
7121 if (specs->type || specs->typespec_word != cts_none
7122 || specs->long_p || specs->short_p || specs->signed_p
7123 || specs->unsigned_p || specs->complex_p)
7124 error ("two or more data types in declaration specifiers");
7125 else if (TREE_CODE (type) == TYPE_DECL)
7126 {
7127 if (TREE_TYPE (type) == error_mark_node)
7128 ; /* Allow the type to default to int to avoid cascading errors. */
7129 else
7130 {
7131 specs->type = TREE_TYPE (type);
7132 specs->decl_attr = DECL_ATTRIBUTES (type);
7133 specs->typedef_p = true;
7134 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7135 }
7136 }
7137 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7138 {
7139 tree t = lookup_name (type);
7140 if (!t || TREE_CODE (t) != TYPE_DECL)
7141 error ("%qE fails to be a typedef or built in type", type);
7142 else if (TREE_TYPE (t) == error_mark_node)
7143 ;
7144 else
7145 specs->type = TREE_TYPE (t);
7146 }
7147 else if (TREE_CODE (type) != ERROR_MARK)
7148 {
7149 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7150 specs->tag_defined_p = true;
7151 if (spec.kind == ctsk_typeof)
7152 specs->typedef_p = true;
7153 specs->type = type;
7154 }
7155
7156 return specs;
7157 }
7158
7159 /* Add the storage class specifier or function specifier SCSPEC to the
7160 declaration specifiers SPECS, returning SPECS. */
7161
7162 struct c_declspecs *
7163 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7164 {
7165 enum rid i;
7166 enum c_storage_class n = csc_none;
7167 bool dupe = false;
7168 specs->declspecs_seen_p = true;
7169 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7170 && C_IS_RESERVED_WORD (scspec));
7171 i = C_RID_CODE (scspec);
7172 if (extra_warnings && specs->non_sc_seen_p)
7173 warning (0, "%qE is not at beginning of declaration", scspec);
7174 switch (i)
7175 {
7176 case RID_INLINE:
7177 /* C99 permits duplicate inline. Although of doubtful utility,
7178 it seems simplest to permit it in gnu89 mode as well, as
7179 there is also little utility in maintaining this as a
7180 difference between gnu89 and C99 inline. */
7181 dupe = false;
7182 specs->inline_p = true;
7183 break;
7184 case RID_THREAD:
7185 dupe = specs->thread_p;
7186 if (specs->storage_class == csc_auto)
7187 error ("%<__thread%> used with %<auto%>");
7188 else if (specs->storage_class == csc_register)
7189 error ("%<__thread%> used with %<register%>");
7190 else if (specs->storage_class == csc_typedef)
7191 error ("%<__thread%> used with %<typedef%>");
7192 else
7193 specs->thread_p = true;
7194 break;
7195 case RID_AUTO:
7196 n = csc_auto;
7197 break;
7198 case RID_EXTERN:
7199 n = csc_extern;
7200 /* Diagnose "__thread extern". */
7201 if (specs->thread_p)
7202 error ("%<__thread%> before %<extern%>");
7203 break;
7204 case RID_REGISTER:
7205 n = csc_register;
7206 break;
7207 case RID_STATIC:
7208 n = csc_static;
7209 /* Diagnose "__thread static". */
7210 if (specs->thread_p)
7211 error ("%<__thread%> before %<static%>");
7212 break;
7213 case RID_TYPEDEF:
7214 n = csc_typedef;
7215 break;
7216 default:
7217 gcc_unreachable ();
7218 }
7219 if (n != csc_none && n == specs->storage_class)
7220 dupe = true;
7221 if (dupe)
7222 error ("duplicate %qE", scspec);
7223 if (n != csc_none)
7224 {
7225 if (specs->storage_class != csc_none && n != specs->storage_class)
7226 {
7227 error ("multiple storage classes in declaration specifiers");
7228 }
7229 else
7230 {
7231 specs->storage_class = n;
7232 if (n != csc_extern && n != csc_static && specs->thread_p)
7233 {
7234 error ("%<__thread%> used with %qE", scspec);
7235 specs->thread_p = false;
7236 }
7237 }
7238 }
7239 return specs;
7240 }
7241
7242 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7243 returning SPECS. */
7244
7245 struct c_declspecs *
7246 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7247 {
7248 specs->attrs = chainon (attrs, specs->attrs);
7249 specs->declspecs_seen_p = true;
7250 return specs;
7251 }
7252
7253 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7254 specifiers with any other type specifier to determine the resulting
7255 type. This is where ISO C checks on complex types are made, since
7256 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7257 double". */
7258
7259 struct c_declspecs *
7260 finish_declspecs (struct c_declspecs *specs)
7261 {
7262 /* If a type was specified as a whole, we have no modifiers and are
7263 done. */
7264 if (specs->type != NULL_TREE)
7265 {
7266 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7267 && !specs->signed_p && !specs->unsigned_p
7268 && !specs->complex_p);
7269 return specs;
7270 }
7271
7272 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7273 has been specified, treat it as "int" unless "_Complex" is
7274 present and there are no other specifiers. If we just have
7275 "_Complex", it is equivalent to "_Complex double", but e.g.
7276 "_Complex short" is equivalent to "_Complex short int". */
7277 if (specs->typespec_word == cts_none)
7278 {
7279 if (specs->long_p || specs->short_p
7280 || specs->signed_p || specs->unsigned_p)
7281 {
7282 specs->typespec_word = cts_int;
7283 }
7284 else if (specs->complex_p)
7285 {
7286 specs->typespec_word = cts_double;
7287 if (pedantic)
7288 pedwarn ("ISO C does not support plain %<complex%> meaning "
7289 "%<double complex%>");
7290 }
7291 else
7292 {
7293 specs->typespec_word = cts_int;
7294 specs->default_int_p = true;
7295 /* We don't diagnose this here because grokdeclarator will
7296 give more specific diagnostics according to whether it is
7297 a function definition. */
7298 }
7299 }
7300
7301 /* If "signed" was specified, record this to distinguish "int" and
7302 "signed int" in the case of a bit-field with
7303 -funsigned-bitfields. */
7304 specs->explicit_signed_p = specs->signed_p;
7305
7306 /* Now compute the actual type. */
7307 switch (specs->typespec_word)
7308 {
7309 case cts_void:
7310 gcc_assert (!specs->long_p && !specs->short_p
7311 && !specs->signed_p && !specs->unsigned_p
7312 && !specs->complex_p);
7313 specs->type = void_type_node;
7314 break;
7315 case cts_bool:
7316 gcc_assert (!specs->long_p && !specs->short_p
7317 && !specs->signed_p && !specs->unsigned_p
7318 && !specs->complex_p);
7319 specs->type = boolean_type_node;
7320 break;
7321 case cts_char:
7322 gcc_assert (!specs->long_p && !specs->short_p);
7323 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7324 if (specs->signed_p)
7325 specs->type = signed_char_type_node;
7326 else if (specs->unsigned_p)
7327 specs->type = unsigned_char_type_node;
7328 else
7329 specs->type = char_type_node;
7330 if (specs->complex_p)
7331 {
7332 if (pedantic)
7333 pedwarn ("ISO C does not support complex integer types");
7334 specs->type = build_complex_type (specs->type);
7335 }
7336 break;
7337 case cts_int:
7338 gcc_assert (!(specs->long_p && specs->short_p));
7339 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7340 if (specs->long_long_p)
7341 specs->type = (specs->unsigned_p
7342 ? long_long_unsigned_type_node
7343 : long_long_integer_type_node);
7344 else if (specs->long_p)
7345 specs->type = (specs->unsigned_p
7346 ? long_unsigned_type_node
7347 : long_integer_type_node);
7348 else if (specs->short_p)
7349 specs->type = (specs->unsigned_p
7350 ? short_unsigned_type_node
7351 : short_integer_type_node);
7352 else
7353 specs->type = (specs->unsigned_p
7354 ? unsigned_type_node
7355 : integer_type_node);
7356 if (specs->complex_p)
7357 {
7358 if (pedantic)
7359 pedwarn ("ISO C does not support complex integer types");
7360 specs->type = build_complex_type (specs->type);
7361 }
7362 break;
7363 case cts_float:
7364 gcc_assert (!specs->long_p && !specs->short_p
7365 && !specs->signed_p && !specs->unsigned_p);
7366 specs->type = (specs->complex_p
7367 ? complex_float_type_node
7368 : float_type_node);
7369 break;
7370 case cts_double:
7371 gcc_assert (!specs->long_long_p && !specs->short_p
7372 && !specs->signed_p && !specs->unsigned_p);
7373 if (specs->long_p)
7374 {
7375 specs->type = (specs->complex_p
7376 ? complex_long_double_type_node
7377 : long_double_type_node);
7378 }
7379 else
7380 {
7381 specs->type = (specs->complex_p
7382 ? complex_double_type_node
7383 : double_type_node);
7384 }
7385 break;
7386 default:
7387 gcc_unreachable ();
7388 }
7389
7390 return specs;
7391 }
7392
7393 /* Synthesize a function which calls all the global ctors or global
7394 dtors in this file. This is only used for targets which do not
7395 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7396 static void
7397 build_cdtor (int method_type, tree cdtors)
7398 {
7399 tree body = 0;
7400
7401 if (!cdtors)
7402 return;
7403
7404 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7405 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7406 &body);
7407
7408 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7409 }
7410
7411 /* Perform final processing on one file scope's declarations (or the
7412 external scope's declarations), GLOBALS. */
7413 static void
7414 c_write_global_declarations_1 (tree globals)
7415 {
7416 size_t len = list_length (globals);
7417 tree *vec = XNEWVEC (tree, len);
7418 size_t i;
7419 tree decl;
7420
7421 /* Process the decls in the order they were written. */
7422 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7423 {
7424 vec[i] = decl;
7425 /* Check for used but undefined static functions using the C
7426 standard's definition of "used", and set TREE_NO_WARNING so
7427 that check_global_declarations doesn't repeat the check. */
7428 if (TREE_CODE (decl) == FUNCTION_DECL
7429 && DECL_INITIAL (decl) == 0
7430 && DECL_EXTERNAL (decl)
7431 && !TREE_PUBLIC (decl)
7432 && C_DECL_USED (decl))
7433 {
7434 pedwarn ("%J%qF used but never defined", decl, decl);
7435 TREE_NO_WARNING (decl) = 1;
7436 }
7437 }
7438
7439 wrapup_global_declarations (vec, len);
7440 check_global_declarations (vec, len);
7441
7442 free (vec);
7443 }
7444
7445 void
7446 c_write_global_declarations (void)
7447 {
7448 tree ext_block, t;
7449
7450 /* We don't want to do this if generating a PCH. */
7451 if (pch_file)
7452 return;
7453
7454 /* Don't waste time on further processing if -fsyntax-only or we've
7455 encountered errors. */
7456 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7457 return;
7458
7459 /* Close the external scope. */
7460 ext_block = pop_scope ();
7461 external_scope = 0;
7462 gcc_assert (!current_scope);
7463
7464 /* Process all file scopes in this compilation, and the external_scope,
7465 through wrapup_global_declarations and check_global_declarations. */
7466 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7467 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7468 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7469
7470 /* Generate functions to call static constructors and destructors
7471 for targets that do not support .ctors/.dtors sections. These
7472 functions have magic names which are detected by collect2. */
7473 build_cdtor ('I', static_ctors); static_ctors = 0;
7474 build_cdtor ('D', static_dtors); static_dtors = 0;
7475
7476 /* We're done parsing; proceed to optimize and emit assembly.
7477 FIXME: shouldn't be the front end's responsibility to call this. */
7478 cgraph_optimize ();
7479 }
7480
7481 #include "gt-c-decl.h"