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