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