* stor-layout.c (finish_record_layout): Also propagate the
[official-gcc.git] / gcc / c / c-decl.c
bloba94d3f743fbbfa9dc78b395ae99a0cc0c4553d6d
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "input.h"
31 #include "tm.h"
32 #include "intl.h"
33 #include "symtab.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "tree.h"
37 #include "fold-const.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "stringpool.h"
43 #include "tree-inline.h"
44 #include "flags.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "c-tree.h"
48 #include "toplev.h"
49 #include "tm_p.h"
50 #include "cpplib.h"
51 #include "target.h"
52 #include "debug.h"
53 #include "opts.h"
54 #include "timevar.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "c-family/c-pragma.h"
58 #include "c-family/c-ubsan.h"
59 #include "c-lang.h"
60 #include "langhooks.h"
61 #include "tree-iterator.h"
62 #include "diagnostic-core.h"
63 #include "dumpfile.h"
64 #include "is-a.h"
65 #include "plugin-api.h"
66 #include "ipa-ref.h"
67 #include "cgraph.h"
68 #include "langhooks-def.h"
69 #include "plugin.h"
70 #include "c-family/c-ada-spec.h"
71 #include "cilk.h"
72 #include "builtins.h"
74 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
75 enum decl_context
76 { NORMAL, /* Ordinary declaration */
77 FUNCDEF, /* Function definition */
78 PARM, /* Declaration of parm before function body */
79 FIELD, /* Declaration inside struct or union */
80 TYPENAME}; /* Typename (inside cast or sizeof) */
82 /* States indicating how grokdeclarator() should handle declspecs marked
83 with __attribute__((deprecated)). An object declared as
84 __attribute__((deprecated)) suppresses warnings of uses of other
85 deprecated items. */
87 enum deprecated_states {
88 DEPRECATED_NORMAL,
89 DEPRECATED_SUPPRESS
93 /* Nonzero if we have seen an invalid cross reference
94 to a struct, union, or enum, but not yet printed the message. */
95 tree pending_invalid_xref;
97 /* File and line to appear in the eventual error message. */
98 location_t pending_invalid_xref_location;
100 /* The file and line that the prototype came from if this is an
101 old-style definition; used for diagnostics in
102 store_parm_decls_oldstyle. */
104 static location_t current_function_prototype_locus;
106 /* Whether this prototype was built-in. */
108 static bool current_function_prototype_built_in;
110 /* The argument type information of this prototype. */
112 static tree current_function_prototype_arg_types;
114 /* The argument information structure for the function currently being
115 defined. */
117 static struct c_arg_info *current_function_arg_info;
119 /* The obstack on which parser and related data structures, which are
120 not live beyond their top-level declaration or definition, are
121 allocated. */
122 struct obstack parser_obstack;
124 /* The current statement tree. */
126 static GTY(()) struct stmt_tree_s c_stmt_tree;
128 /* State saving variables. */
129 tree c_break_label;
130 tree c_cont_label;
132 /* A list of decls to be made automatically visible in each file scope. */
133 static GTY(()) tree visible_builtins;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a return statement that specifies a return value is seen. */
138 int current_function_returns_value;
140 /* Set to 0 at beginning of a function definition, set to 1 if
141 a return statement with no argument is seen. */
143 int current_function_returns_null;
145 /* Set to 0 at beginning of a function definition, set to 1 if
146 a call to a noreturn function is seen. */
148 int current_function_returns_abnormally;
150 /* Set to nonzero by `grokdeclarator' for a function
151 whose return type is defaulted, if warnings for this are desired. */
153 static int warn_about_return_type;
155 /* Nonzero when the current toplevel function contains a declaration
156 of a nested function which is never defined. */
158 static bool undef_nested_function;
160 /* If non-zero, implicit "omp declare target" attribute is added into the
161 attribute lists. */
162 int current_omp_declare_target_attribute;
164 /* Each c_binding structure describes one binding of an identifier to
165 a decl. All the decls in a scope - irrespective of namespace - are
166 chained together by the ->prev field, which (as the name implies)
167 runs in reverse order. All the decls in a given namespace bound to
168 a given identifier are chained by the ->shadowed field, which runs
169 from inner to outer scopes.
171 The ->decl field usually points to a DECL node, but there are two
172 exceptions. In the namespace of type tags, the bound entity is a
173 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
174 identifier is encountered, it is bound to error_mark_node to
175 suppress further errors about that identifier in the current
176 function.
178 The ->u.type field stores the type of the declaration in this scope;
179 if NULL, the type is the type of the ->decl field. This is only of
180 relevance for objects with external or internal linkage which may
181 be redeclared in inner scopes, forming composite types that only
182 persist for the duration of those scopes. In the external scope,
183 this stores the composite of all the types declared for this
184 object, visible or not. The ->inner_comp field (used only at file
185 scope) stores whether an incomplete array type at file scope was
186 completed at an inner scope to an array size other than 1.
188 The ->u.label field is used for labels. It points to a structure
189 which stores additional information used for warnings.
191 The depth field is copied from the scope structure that holds this
192 decl. It is used to preserve the proper ordering of the ->shadowed
193 field (see bind()) and also for a handful of special-case checks.
194 Finally, the invisible bit is true for a decl which should be
195 ignored for purposes of normal name lookup, and the nested bit is
196 true for a decl that's been bound a second time in an inner scope;
197 in all such cases, the binding in the outer scope will have its
198 invisible bit true. */
200 struct GTY((chain_next ("%h.prev"))) c_binding {
201 union GTY(()) { /* first so GTY desc can use decl */
202 tree GTY((tag ("0"))) type; /* the type in this scope */
203 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
204 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
205 tree decl; /* the decl bound */
206 tree id; /* the identifier it's bound to */
207 struct c_binding *prev; /* the previous decl in this scope */
208 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
209 unsigned int depth : 28; /* depth of this scope */
210 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
211 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
212 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
213 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
214 location_t locus; /* location for nested bindings */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 /* Each C symbol points to three linked lists of c_binding structures.
222 These describe the values of the identifier in the three different
223 namespaces defined by the language. */
225 struct GTY(()) lang_identifier {
226 struct c_common_identifier common_id;
227 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
228 struct c_binding *tag_binding; /* struct/union/enum tags */
229 struct c_binding *label_binding; /* labels */
232 /* Validate c-lang.c's assumptions. */
233 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
234 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
236 /* The binding oracle; see c-tree.h. */
237 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
239 /* This flag is set on an identifier if we have previously asked the
240 binding oracle for this identifier's symbol binding. */
241 #define I_SYMBOL_CHECKED(node) \
242 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
244 static inline struct c_binding* *
245 i_symbol_binding (tree node)
247 struct lang_identifier *lid
248 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
250 if (lid->symbol_binding == NULL
251 && c_binding_oracle != NULL
252 && !I_SYMBOL_CHECKED (node))
254 /* Set the "checked" flag first, to avoid infinite recursion
255 when the binding oracle calls back into gcc. */
256 I_SYMBOL_CHECKED (node) = 1;
257 c_binding_oracle (C_ORACLE_SYMBOL, node);
260 return &lid->symbol_binding;
263 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
265 #define I_SYMBOL_DECL(node) \
266 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
268 /* This flag is set on an identifier if we have previously asked the
269 binding oracle for this identifier's tag binding. */
270 #define I_TAG_CHECKED(node) \
271 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
273 static inline struct c_binding **
274 i_tag_binding (tree node)
276 struct lang_identifier *lid
277 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
279 if (lid->tag_binding == NULL
280 && c_binding_oracle != NULL
281 && !I_TAG_CHECKED (node))
283 /* Set the "checked" flag first, to avoid infinite recursion
284 when the binding oracle calls back into gcc. */
285 I_TAG_CHECKED (node) = 1;
286 c_binding_oracle (C_ORACLE_TAG, node);
289 return &lid->tag_binding;
292 #define I_TAG_BINDING(node) (*i_tag_binding (node))
294 #define I_TAG_DECL(node) \
295 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
297 /* This flag is set on an identifier if we have previously asked the
298 binding oracle for this identifier's label binding. */
299 #define I_LABEL_CHECKED(node) \
300 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
302 static inline struct c_binding **
303 i_label_binding (tree node)
305 struct lang_identifier *lid
306 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
308 if (lid->label_binding == NULL
309 && c_binding_oracle != NULL
310 && !I_LABEL_CHECKED (node))
312 /* Set the "checked" flag first, to avoid infinite recursion
313 when the binding oracle calls back into gcc. */
314 I_LABEL_CHECKED (node) = 1;
315 c_binding_oracle (C_ORACLE_LABEL, node);
318 return &lid->label_binding;
321 #define I_LABEL_BINDING(node) (*i_label_binding (node))
323 #define I_LABEL_DECL(node) \
324 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
326 /* The resulting tree type. */
328 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
329 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
331 union tree_node GTY ((tag ("0"),
332 desc ("tree_node_structure (&%h)")))
333 generic;
334 struct lang_identifier GTY ((tag ("1"))) identifier;
337 /* Track bindings and other things that matter for goto warnings. For
338 efficiency, we do not gather all the decls at the point of
339 definition. Instead, we point into the bindings structure. As
340 scopes are popped, we update these structures and gather the decls
341 that matter at that time. */
343 struct GTY(()) c_spot_bindings {
344 /* The currently open scope which holds bindings defined when the
345 label was defined or the goto statement was found. */
346 struct c_scope *scope;
347 /* The bindings in the scope field which were defined at the point
348 of the label or goto. This lets us look at older or newer
349 bindings in the scope, as appropriate. */
350 struct c_binding *bindings_in_scope;
351 /* The number of statement expressions that have started since this
352 label or goto statement was defined. This is zero if we are at
353 the same statement expression level. It is positive if we are in
354 a statement expression started since this spot. It is negative
355 if this spot was in a statement expression and we have left
356 it. */
357 int stmt_exprs;
358 /* Whether we started in a statement expression but are no longer in
359 it. This is set to true if stmt_exprs ever goes negative. */
360 bool left_stmt_expr;
363 /* This structure is used to keep track of bindings seen when a goto
364 statement is defined. This is only used if we see the goto
365 statement before we see the label. */
367 struct GTY(()) c_goto_bindings {
368 /* The location of the goto statement. */
369 location_t loc;
370 /* The bindings of the goto statement. */
371 struct c_spot_bindings goto_bindings;
374 typedef struct c_goto_bindings *c_goto_bindings_p;
376 /* The additional information we keep track of for a label binding.
377 These fields are updated as scopes are popped. */
379 struct GTY(()) c_label_vars {
380 /* The shadowed c_label_vars, when one label shadows another (which
381 can only happen using a __label__ declaration). */
382 struct c_label_vars *shadowed;
383 /* The bindings when the label was defined. */
384 struct c_spot_bindings label_bindings;
385 /* A list of decls that we care about: decls about which we should
386 warn if a goto branches to this label from later in the function.
387 Decls are added to this list as scopes are popped. We only add
388 the decls that matter. */
389 vec<tree, va_gc> *decls_in_scope;
390 /* A list of goto statements to this label. This is only used for
391 goto statements seen before the label was defined, so that we can
392 issue appropriate warnings for them. */
393 vec<c_goto_bindings_p, va_gc> *gotos;
396 /* Each c_scope structure describes the complete contents of one
397 scope. Four scopes are distinguished specially: the innermost or
398 current scope, the innermost function scope, the file scope (always
399 the second to outermost) and the outermost or external scope.
401 Most declarations are recorded in the current scope.
403 All normal label declarations are recorded in the innermost
404 function scope, as are bindings of undeclared identifiers to
405 error_mark_node. (GCC permits nested functions as an extension,
406 hence the 'innermost' qualifier.) Explicitly declared labels
407 (using the __label__ extension) appear in the current scope.
409 Being in the file scope (current_scope == file_scope) causes
410 special behavior in several places below. Also, under some
411 conditions the Objective-C front end records declarations in the
412 file scope even though that isn't the current scope.
414 All declarations with external linkage are recorded in the external
415 scope, even if they aren't visible there; this models the fact that
416 such declarations are visible to the entire program, and (with a
417 bit of cleverness, see pushdecl) allows diagnosis of some violations
418 of C99 6.2.2p7 and 6.2.7p2:
420 If, within the same translation unit, the same identifier appears
421 with both internal and external linkage, the behavior is
422 undefined.
424 All declarations that refer to the same object or function shall
425 have compatible type; otherwise, the behavior is undefined.
427 Initially only the built-in declarations, which describe compiler
428 intrinsic functions plus a subset of the standard library, are in
429 this scope.
431 The order of the blocks list matters, and it is frequently appended
432 to. To avoid having to walk all the way to the end of the list on
433 each insertion, or reverse the list later, we maintain a pointer to
434 the last list entry. (FIXME: It should be feasible to use a reversed
435 list here.)
437 The bindings list is strictly in reverse order of declarations;
438 pop_scope relies on this. */
441 struct GTY((chain_next ("%h.outer"))) c_scope {
442 /* The scope containing this one. */
443 struct c_scope *outer;
445 /* The next outermost function scope. */
446 struct c_scope *outer_function;
448 /* All bindings in this scope. */
449 struct c_binding *bindings;
451 /* For each scope (except the global one), a chain of BLOCK nodes
452 for all the scopes that were entered and exited one level down. */
453 tree blocks;
454 tree blocks_last;
456 /* The depth of this scope. Used to keep the ->shadowed chain of
457 bindings sorted innermost to outermost. */
458 unsigned int depth : 28;
460 /* True if we are currently filling this scope with parameter
461 declarations. */
462 BOOL_BITFIELD parm_flag : 1;
464 /* True if we saw [*] in this scope. Used to give an error messages
465 if these appears in a function definition. */
466 BOOL_BITFIELD had_vla_unspec : 1;
468 /* True if we already complained about forward parameter decls
469 in this scope. This prevents double warnings on
470 foo (int a; int b; ...) */
471 BOOL_BITFIELD warned_forward_parm_decls : 1;
473 /* True if this is the outermost block scope of a function body.
474 This scope contains the parameters, the local variables declared
475 in the outermost block, and all the labels (except those in
476 nested functions, or declared at block scope with __label__). */
477 BOOL_BITFIELD function_body : 1;
479 /* True means make a BLOCK for this scope no matter what. */
480 BOOL_BITFIELD keep : 1;
482 /* True means that an unsuffixed float constant is _Decimal64. */
483 BOOL_BITFIELD float_const_decimal64 : 1;
485 /* True if this scope has any label bindings. This is used to speed
486 up searching for labels when popping scopes, particularly since
487 labels are normally only found at function scope. */
488 BOOL_BITFIELD has_label_bindings : 1;
490 /* True if we should issue a warning if a goto statement crosses any
491 of the bindings. We still need to check the list of bindings to
492 find the specific ones we need to warn about. This is true if
493 decl_jump_unsafe would return true for any of the bindings. This
494 is used to avoid looping over all the bindings unnecessarily. */
495 BOOL_BITFIELD has_jump_unsafe_decl : 1;
498 /* The scope currently in effect. */
500 static GTY(()) struct c_scope *current_scope;
502 /* The innermost function scope. Ordinary (not explicitly declared)
503 labels, bindings to error_mark_node, and the lazily-created
504 bindings of __func__ and its friends get this scope. */
506 static GTY(()) struct c_scope *current_function_scope;
508 /* The C file scope. This is reset for each input translation unit. */
510 static GTY(()) struct c_scope *file_scope;
512 /* The outermost scope. This is used for all declarations with
513 external linkage, and only these, hence the name. */
515 static GTY(()) struct c_scope *external_scope;
517 /* A chain of c_scope structures awaiting reuse. */
519 static GTY((deletable)) struct c_scope *scope_freelist;
521 /* A chain of c_binding structures awaiting reuse. */
523 static GTY((deletable)) struct c_binding *binding_freelist;
525 /* Append VAR to LIST in scope SCOPE. */
526 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
527 struct c_scope *s_ = (scope); \
528 tree d_ = (decl); \
529 if (s_->list##_last) \
530 BLOCK_CHAIN (s_->list##_last) = d_; \
531 else \
532 s_->list = d_; \
533 s_->list##_last = d_; \
534 } while (0)
536 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
537 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
538 struct c_scope *t_ = (tscope); \
539 struct c_scope *f_ = (fscope); \
540 if (t_->to##_last) \
541 BLOCK_CHAIN (t_->to##_last) = f_->from; \
542 else \
543 t_->to = f_->from; \
544 t_->to##_last = f_->from##_last; \
545 } while (0)
547 /* A c_inline_static structure stores details of a static identifier
548 referenced in a definition of a function that may be an inline
549 definition if no subsequent declaration of that function uses
550 "extern" or does not use "inline". */
552 struct GTY((chain_next ("%h.next"))) c_inline_static {
553 /* The location for a diagnostic. */
554 location_t location;
556 /* The function that may be an inline definition. */
557 tree function;
559 /* The object or function referenced. */
560 tree static_decl;
562 /* What sort of reference this is. */
563 enum c_inline_static_type type;
565 /* The next such structure or NULL. */
566 struct c_inline_static *next;
569 /* List of static identifiers used or referenced in functions that may
570 be inline definitions. */
571 static GTY(()) struct c_inline_static *c_inline_statics;
573 /* True means unconditionally make a BLOCK for the next scope pushed. */
575 static bool keep_next_level_flag;
577 /* True means the next call to push_scope will be the outermost scope
578 of a function body, so do not push a new scope, merely cease
579 expecting parameter decls. */
581 static bool next_is_function_body;
583 /* A vector of pointers to c_binding structures. */
585 typedef struct c_binding *c_binding_ptr;
587 /* Information that we keep for a struct or union while it is being
588 parsed. */
590 struct c_struct_parse_info
592 /* If warn_cxx_compat, a list of types defined within this
593 struct. */
594 vec<tree> struct_types;
595 /* If warn_cxx_compat, a list of field names which have bindings,
596 and which are defined in this struct, but which are not defined
597 in any enclosing struct. This is used to clear the in_struct
598 field of the c_bindings structure. */
599 vec<c_binding_ptr> fields;
600 /* If warn_cxx_compat, a list of typedef names used when defining
601 fields in this struct. */
602 vec<tree> typedefs_seen;
605 /* Information for the struct or union currently being parsed, or
606 NULL if not parsing a struct or union. */
607 static struct c_struct_parse_info *struct_parse_info;
609 /* Forward declarations. */
610 static tree lookup_name_in_scope (tree, struct c_scope *);
611 static tree c_make_fname_decl (location_t, tree, int);
612 static tree grokdeclarator (const struct c_declarator *,
613 struct c_declspecs *,
614 enum decl_context, bool, tree *, tree *, tree *,
615 bool *, enum deprecated_states);
616 static tree grokparms (struct c_arg_info *, bool);
617 static void layout_array_type (tree);
618 static void warn_defaults_to (location_t, int, const char *, ...)
619 ATTRIBUTE_GCC_DIAG(3,4);
621 /* T is a statement. Add it to the statement-tree. This is the
622 C/ObjC version--C++ has a slightly different version of this
623 function. */
625 tree
626 add_stmt (tree t)
628 enum tree_code code = TREE_CODE (t);
630 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
632 if (!EXPR_HAS_LOCATION (t))
633 SET_EXPR_LOCATION (t, input_location);
636 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
637 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
639 /* Add T to the statement-tree. Non-side-effect statements need to be
640 recorded during statement expressions. */
641 if (!building_stmt_list_p ())
642 push_stmt_list ();
643 append_to_statement_list_force (t, &cur_stmt_list);
645 return t;
648 /* Build a pointer type using the default pointer mode. */
650 static tree
651 c_build_pointer_type (tree to_type)
653 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
654 : TYPE_ADDR_SPACE (to_type);
655 machine_mode pointer_mode;
657 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
658 pointer_mode = targetm.addr_space.pointer_mode (as);
659 else
660 pointer_mode = c_default_pointer_mode;
661 return build_pointer_type_for_mode (to_type, pointer_mode, false);
665 /* Return true if we will want to say something if a goto statement
666 crosses DECL. */
668 static bool
669 decl_jump_unsafe (tree decl)
671 if (error_operand_p (decl))
672 return false;
674 /* Always warn about crossing variably modified types. */
675 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
676 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
677 return true;
679 /* Otherwise, only warn if -Wgoto-misses-init and this is an
680 initialized automatic decl. */
681 if (warn_jump_misses_init
682 && TREE_CODE (decl) == VAR_DECL
683 && !TREE_STATIC (decl)
684 && DECL_INITIAL (decl) != NULL_TREE)
685 return true;
687 return false;
691 void
692 c_print_identifier (FILE *file, tree node, int indent)
694 void (*save) (enum c_oracle_request, tree identifier);
696 /* Temporarily hide any binding oracle. Without this, calls to
697 debug_tree from the debugger will end up calling into the oracle,
698 making for a confusing debug session. As the oracle isn't needed
699 here for normal operation, it's simplest to suppress it. */
700 save = c_binding_oracle;
701 c_binding_oracle = NULL;
703 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
704 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
705 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
706 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
708 tree rid = ridpointers[C_RID_CODE (node)];
709 indent_to (file, indent + 4);
710 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
711 (void *) rid, IDENTIFIER_POINTER (rid));
714 c_binding_oracle = save;
717 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
718 which may be any of several kinds of DECL or TYPE or error_mark_node,
719 in the scope SCOPE. */
720 static void
721 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
722 bool nested, location_t locus)
724 struct c_binding *b, **here;
726 if (binding_freelist)
728 b = binding_freelist;
729 binding_freelist = b->prev;
731 else
732 b = ggc_alloc<c_binding> ();
734 b->shadowed = 0;
735 b->decl = decl;
736 b->id = name;
737 b->depth = scope->depth;
738 b->invisible = invisible;
739 b->nested = nested;
740 b->inner_comp = 0;
741 b->in_struct = 0;
742 b->locus = locus;
744 b->u.type = NULL;
746 b->prev = scope->bindings;
747 scope->bindings = b;
749 if (decl_jump_unsafe (decl))
750 scope->has_jump_unsafe_decl = 1;
752 if (!name)
753 return;
755 switch (TREE_CODE (decl))
757 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
758 case ENUMERAL_TYPE:
759 case UNION_TYPE:
760 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
761 case VAR_DECL:
762 case FUNCTION_DECL:
763 case TYPE_DECL:
764 case CONST_DECL:
765 case PARM_DECL:
766 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
768 default:
769 gcc_unreachable ();
772 /* Locate the appropriate place in the chain of shadowed decls
773 to insert this binding. Normally, scope == current_scope and
774 this does nothing. */
775 while (*here && (*here)->depth > scope->depth)
776 here = &(*here)->shadowed;
778 b->shadowed = *here;
779 *here = b;
782 /* Clear the binding structure B, stick it on the binding_freelist,
783 and return the former value of b->prev. This is used by pop_scope
784 and get_parm_info to iterate destructively over all the bindings
785 from a given scope. */
786 static struct c_binding *
787 free_binding_and_advance (struct c_binding *b)
789 struct c_binding *prev = b->prev;
791 memset (b, 0, sizeof (struct c_binding));
792 b->prev = binding_freelist;
793 binding_freelist = b;
795 return prev;
798 /* Bind a label. Like bind, but skip fields which aren't used for
799 labels, and add the LABEL_VARS value. */
800 static void
801 bind_label (tree name, tree label, struct c_scope *scope,
802 struct c_label_vars *label_vars)
804 struct c_binding *b;
806 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
807 UNKNOWN_LOCATION);
809 scope->has_label_bindings = true;
811 b = scope->bindings;
812 gcc_assert (b->decl == label);
813 label_vars->shadowed = b->u.label;
814 b->u.label = label_vars;
817 /* Hook called at end of compilation to assume 1 elt
818 for a file-scope tentative array defn that wasn't complete before. */
820 void
821 c_finish_incomplete_decl (tree decl)
823 if (TREE_CODE (decl) == VAR_DECL)
825 tree type = TREE_TYPE (decl);
826 if (type != error_mark_node
827 && TREE_CODE (type) == ARRAY_TYPE
828 && !DECL_EXTERNAL (decl)
829 && TYPE_DOMAIN (type) == 0)
831 warning_at (DECL_SOURCE_LOCATION (decl),
832 0, "array %q+D assumed to have one element", decl);
834 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
836 relayout_decl (decl);
841 /* Record that inline function FUNC contains a reference (location
842 LOC) to static DECL (file-scope or function-local according to
843 TYPE). */
845 void
846 record_inline_static (location_t loc, tree func, tree decl,
847 enum c_inline_static_type type)
849 c_inline_static *csi = ggc_alloc<c_inline_static> ();
850 csi->location = loc;
851 csi->function = func;
852 csi->static_decl = decl;
853 csi->type = type;
854 csi->next = c_inline_statics;
855 c_inline_statics = csi;
858 /* Check for references to static declarations in inline functions at
859 the end of the translation unit and diagnose them if the functions
860 are still inline definitions. */
862 static void
863 check_inline_statics (void)
865 struct c_inline_static *csi;
866 for (csi = c_inline_statics; csi; csi = csi->next)
868 if (DECL_EXTERNAL (csi->function))
869 switch (csi->type)
871 case csi_internal:
872 pedwarn (csi->location, 0,
873 "%qD is static but used in inline function %qD "
874 "which is not static", csi->static_decl, csi->function);
875 break;
876 case csi_modifiable:
877 pedwarn (csi->location, 0,
878 "%q+D is static but declared in inline function %qD "
879 "which is not static", csi->static_decl, csi->function);
880 break;
881 default:
882 gcc_unreachable ();
885 c_inline_statics = NULL;
888 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
889 for the current state, otherwise set it to uninitialized. */
891 static void
892 set_spot_bindings (struct c_spot_bindings *p, bool defining)
894 if (defining)
896 p->scope = current_scope;
897 p->bindings_in_scope = current_scope->bindings;
899 else
901 p->scope = NULL;
902 p->bindings_in_scope = NULL;
904 p->stmt_exprs = 0;
905 p->left_stmt_expr = false;
908 /* Update spot bindings P as we pop out of SCOPE. Return true if we
909 should push decls for a label. */
911 static bool
912 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
914 if (p->scope != scope)
916 /* This label or goto is defined in some other scope, or it is a
917 label which is not yet defined. There is nothing to
918 update. */
919 return false;
922 /* Adjust the spot bindings to refer to the bindings already defined
923 in the enclosing scope. */
924 p->scope = scope->outer;
925 p->bindings_in_scope = p->scope->bindings;
927 return true;
930 /* The Objective-C front-end often needs to determine the current scope. */
932 void *
933 objc_get_current_scope (void)
935 return current_scope;
938 /* The following function is used only by Objective-C. It needs to live here
939 because it accesses the innards of c_scope. */
941 void
942 objc_mark_locals_volatile (void *enclosing_blk)
944 struct c_scope *scope;
945 struct c_binding *b;
947 for (scope = current_scope;
948 scope && scope != enclosing_blk;
949 scope = scope->outer)
951 for (b = scope->bindings; b; b = b->prev)
952 objc_volatilize_decl (b->decl);
954 /* Do not climb up past the current function. */
955 if (scope->function_body)
956 break;
960 /* Return true if we are in the global binding level. */
962 bool
963 global_bindings_p (void)
965 return current_scope == file_scope;
968 void
969 keep_next_level (void)
971 keep_next_level_flag = true;
974 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
976 void
977 set_float_const_decimal64 (void)
979 current_scope->float_const_decimal64 = true;
982 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
984 void
985 clear_float_const_decimal64 (void)
987 current_scope->float_const_decimal64 = false;
990 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
992 bool
993 float_const_decimal64_p (void)
995 return current_scope->float_const_decimal64;
998 /* Identify this scope as currently being filled with parameters. */
1000 void
1001 declare_parm_level (void)
1003 current_scope->parm_flag = true;
1006 void
1007 push_scope (void)
1009 if (next_is_function_body)
1011 /* This is the transition from the parameters to the top level
1012 of the function body. These are the same scope
1013 (C99 6.2.1p4,6) so we do not push another scope structure.
1014 next_is_function_body is set only by store_parm_decls, which
1015 in turn is called when and only when we are about to
1016 encounter the opening curly brace for the function body.
1018 The outermost block of a function always gets a BLOCK node,
1019 because the debugging output routines expect that each
1020 function has at least one BLOCK. */
1021 current_scope->parm_flag = false;
1022 current_scope->function_body = true;
1023 current_scope->keep = true;
1024 current_scope->outer_function = current_function_scope;
1025 current_function_scope = current_scope;
1027 keep_next_level_flag = false;
1028 next_is_function_body = false;
1030 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1031 if (current_scope->outer)
1032 current_scope->float_const_decimal64
1033 = current_scope->outer->float_const_decimal64;
1034 else
1035 current_scope->float_const_decimal64 = false;
1037 else
1039 struct c_scope *scope;
1040 if (scope_freelist)
1042 scope = scope_freelist;
1043 scope_freelist = scope->outer;
1045 else
1046 scope = ggc_cleared_alloc<c_scope> ();
1048 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1049 if (current_scope)
1050 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1051 else
1052 scope->float_const_decimal64 = false;
1054 scope->keep = keep_next_level_flag;
1055 scope->outer = current_scope;
1056 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1058 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1059 possible. */
1060 if (current_scope && scope->depth == 0)
1062 scope->depth--;
1063 sorry ("GCC supports only %u nested scopes", scope->depth);
1066 current_scope = scope;
1067 keep_next_level_flag = false;
1071 /* This is called when we are leaving SCOPE. For each label defined
1072 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1073 These are the decls whose initialization will be skipped by a goto
1074 later in the function. */
1076 static void
1077 update_label_decls (struct c_scope *scope)
1079 struct c_scope *s;
1081 s = scope;
1082 while (s != NULL)
1084 if (s->has_label_bindings)
1086 struct c_binding *b;
1088 for (b = s->bindings; b != NULL; b = b->prev)
1090 struct c_label_vars *label_vars;
1091 struct c_binding *b1;
1092 bool hjud;
1093 unsigned int ix;
1094 struct c_goto_bindings *g;
1096 if (TREE_CODE (b->decl) != LABEL_DECL)
1097 continue;
1098 label_vars = b->u.label;
1100 b1 = label_vars->label_bindings.bindings_in_scope;
1101 if (label_vars->label_bindings.scope == NULL)
1102 hjud = false;
1103 else
1104 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1105 if (update_spot_bindings (scope, &label_vars->label_bindings))
1107 /* This label is defined in this scope. */
1108 if (hjud)
1110 for (; b1 != NULL; b1 = b1->prev)
1112 /* A goto from later in the function to this
1113 label will never see the initialization
1114 of B1, if any. Save it to issue a
1115 warning if needed. */
1116 if (decl_jump_unsafe (b1->decl))
1117 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1122 /* Update the bindings of any goto statements associated
1123 with this label. */
1124 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1125 update_spot_bindings (scope, &g->goto_bindings);
1129 /* Don't search beyond the current function. */
1130 if (s == current_function_scope)
1131 break;
1133 s = s->outer;
1137 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1139 static void
1140 set_type_context (tree type, tree context)
1142 for (type = TYPE_MAIN_VARIANT (type); type;
1143 type = TYPE_NEXT_VARIANT (type))
1144 TYPE_CONTEXT (type) = context;
1147 /* Exit a scope. Restore the state of the identifier-decl mappings
1148 that were in effect when this scope was entered. Return a BLOCK
1149 node containing all the DECLs in this scope that are of interest
1150 to debug info generation. */
1152 tree
1153 pop_scope (void)
1155 struct c_scope *scope = current_scope;
1156 tree block, context, p;
1157 struct c_binding *b;
1159 bool functionbody = scope->function_body;
1160 bool keep = functionbody || scope->keep || scope->bindings;
1162 update_label_decls (scope);
1164 /* If appropriate, create a BLOCK to record the decls for the life
1165 of this function. */
1166 block = 0;
1167 if (keep)
1169 block = make_node (BLOCK);
1170 BLOCK_SUBBLOCKS (block) = scope->blocks;
1171 TREE_USED (block) = 1;
1173 /* In each subblock, record that this is its superior. */
1174 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1175 BLOCK_SUPERCONTEXT (p) = block;
1177 BLOCK_VARS (block) = 0;
1180 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1181 scope must be set so that they point to the appropriate
1182 construct, i.e. either to the current FUNCTION_DECL node, or
1183 else to the BLOCK node we just constructed.
1185 Note that for tagged types whose scope is just the formal
1186 parameter list for some function type specification, we can't
1187 properly set their TYPE_CONTEXTs here, because we don't have a
1188 pointer to the appropriate FUNCTION_TYPE node readily available
1189 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1190 type nodes get set in `grokdeclarator' as soon as we have created
1191 the FUNCTION_TYPE node which will represent the "scope" for these
1192 "parameter list local" tagged types. */
1193 if (scope->function_body)
1194 context = current_function_decl;
1195 else if (scope == file_scope)
1197 tree file_decl = build_translation_unit_decl (NULL_TREE);
1198 context = file_decl;
1199 debug_hooks->register_main_translation_unit (file_decl);
1201 else
1202 context = block;
1204 /* Clear all bindings in this scope. */
1205 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1207 p = b->decl;
1208 switch (TREE_CODE (p))
1210 case LABEL_DECL:
1211 /* Warnings for unused labels, errors for undefined labels. */
1212 if (TREE_USED (p) && !DECL_INITIAL (p))
1214 error ("label %q+D used but not defined", p);
1215 DECL_INITIAL (p) = error_mark_node;
1217 else
1218 warn_for_unused_label (p);
1220 /* Labels go in BLOCK_VARS. */
1221 DECL_CHAIN (p) = BLOCK_VARS (block);
1222 BLOCK_VARS (block) = p;
1223 gcc_assert (I_LABEL_BINDING (b->id) == b);
1224 I_LABEL_BINDING (b->id) = b->shadowed;
1226 /* Also pop back to the shadowed label_vars. */
1227 release_tree_vector (b->u.label->decls_in_scope);
1228 b->u.label = b->u.label->shadowed;
1229 break;
1231 case ENUMERAL_TYPE:
1232 case UNION_TYPE:
1233 case RECORD_TYPE:
1234 set_type_context (p, context);
1236 /* Types may not have tag-names, in which case the type
1237 appears in the bindings list with b->id NULL. */
1238 if (b->id)
1240 gcc_assert (I_TAG_BINDING (b->id) == b);
1241 I_TAG_BINDING (b->id) = b->shadowed;
1243 break;
1245 case FUNCTION_DECL:
1246 /* Propagate TREE_ADDRESSABLE from nested functions to their
1247 containing functions. */
1248 if (!TREE_ASM_WRITTEN (p)
1249 && DECL_INITIAL (p) != 0
1250 && TREE_ADDRESSABLE (p)
1251 && DECL_ABSTRACT_ORIGIN (p) != 0
1252 && DECL_ABSTRACT_ORIGIN (p) != p)
1253 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1254 if (!DECL_EXTERNAL (p)
1255 && !DECL_INITIAL (p)
1256 && scope != file_scope
1257 && scope != external_scope)
1259 error ("nested function %q+D declared but never defined", p);
1260 undef_nested_function = true;
1262 else if (DECL_DECLARED_INLINE_P (p)
1263 && TREE_PUBLIC (p)
1264 && !DECL_INITIAL (p))
1266 /* C99 6.7.4p6: "a function with external linkage... declared
1267 with an inline function specifier ... shall also be defined
1268 in the same translation unit." */
1269 if (!flag_gnu89_inline
1270 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1271 && scope != external_scope)
1272 pedwarn (input_location, 0,
1273 "inline function %q+D declared but never defined", p);
1274 DECL_EXTERNAL (p) = 1;
1277 goto common_symbol;
1279 case VAR_DECL:
1280 /* Warnings for unused variables. */
1281 if ((!TREE_USED (p) || !DECL_READ_P (p))
1282 && !TREE_NO_WARNING (p)
1283 && !DECL_IN_SYSTEM_HEADER (p)
1284 && DECL_NAME (p)
1285 && !DECL_ARTIFICIAL (p)
1286 && scope != file_scope
1287 && scope != external_scope)
1289 if (!TREE_USED (p))
1290 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1291 else if (DECL_CONTEXT (p) == current_function_decl)
1292 warning_at (DECL_SOURCE_LOCATION (p),
1293 OPT_Wunused_but_set_variable,
1294 "variable %qD set but not used", p);
1297 if (b->inner_comp)
1299 error ("type of array %q+D completed incompatibly with"
1300 " implicit initialization", p);
1303 /* Fall through. */
1304 case TYPE_DECL:
1305 case CONST_DECL:
1306 common_symbol:
1307 /* All of these go in BLOCK_VARS, but only if this is the
1308 binding in the home scope. */
1309 if (!b->nested)
1311 DECL_CHAIN (p) = BLOCK_VARS (block);
1312 BLOCK_VARS (block) = p;
1314 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1316 /* For block local externs add a special
1317 DECL_EXTERNAL decl for debug info generation. */
1318 tree extp = copy_node (p);
1320 DECL_EXTERNAL (extp) = 1;
1321 TREE_STATIC (extp) = 0;
1322 TREE_PUBLIC (extp) = 1;
1323 DECL_INITIAL (extp) = NULL_TREE;
1324 DECL_LANG_SPECIFIC (extp) = NULL;
1325 DECL_CONTEXT (extp) = current_function_decl;
1326 if (TREE_CODE (p) == FUNCTION_DECL)
1328 DECL_RESULT (extp) = NULL_TREE;
1329 DECL_SAVED_TREE (extp) = NULL_TREE;
1330 DECL_STRUCT_FUNCTION (extp) = NULL;
1332 if (b->locus != UNKNOWN_LOCATION)
1333 DECL_SOURCE_LOCATION (extp) = b->locus;
1334 DECL_CHAIN (extp) = BLOCK_VARS (block);
1335 BLOCK_VARS (block) = extp;
1337 /* If this is the file scope set DECL_CONTEXT of each decl to
1338 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1339 work. */
1340 if (scope == file_scope)
1342 DECL_CONTEXT (p) = context;
1343 if (TREE_CODE (p) == TYPE_DECL
1344 && TREE_TYPE (p) != error_mark_node)
1345 set_type_context (TREE_TYPE (p), context);
1348 /* Fall through. */
1349 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1350 already been put there by store_parm_decls. Unused-
1351 parameter warnings are handled by function.c.
1352 error_mark_node obviously does not go in BLOCK_VARS and
1353 does not get unused-variable warnings. */
1354 case PARM_DECL:
1355 case ERROR_MARK:
1356 /* It is possible for a decl not to have a name. We get
1357 here with b->id NULL in this case. */
1358 if (b->id)
1360 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1361 I_SYMBOL_BINDING (b->id) = b->shadowed;
1362 if (b->shadowed && b->shadowed->u.type)
1363 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1365 break;
1367 default:
1368 gcc_unreachable ();
1373 /* Dispose of the block that we just made inside some higher level. */
1374 if ((scope->function_body || scope == file_scope) && context)
1376 DECL_INITIAL (context) = block;
1377 BLOCK_SUPERCONTEXT (block) = context;
1379 else if (scope->outer)
1381 if (block)
1382 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1383 /* If we did not make a block for the scope just exited, any
1384 blocks made for inner scopes must be carried forward so they
1385 will later become subblocks of something else. */
1386 else if (scope->blocks)
1387 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1390 /* Pop the current scope, and free the structure for reuse. */
1391 current_scope = scope->outer;
1392 if (scope->function_body)
1393 current_function_scope = scope->outer_function;
1395 memset (scope, 0, sizeof (struct c_scope));
1396 scope->outer = scope_freelist;
1397 scope_freelist = scope;
1399 return block;
1402 void
1403 push_file_scope (void)
1405 tree decl;
1407 if (file_scope)
1408 return;
1410 push_scope ();
1411 file_scope = current_scope;
1413 start_fname_decls ();
1415 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1416 bind (DECL_NAME (decl), decl, file_scope,
1417 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1420 void
1421 pop_file_scope (void)
1423 /* In case there were missing closebraces, get us back to the global
1424 binding level. */
1425 while (current_scope != file_scope)
1426 pop_scope ();
1428 /* __FUNCTION__ is defined at file scope (""). This
1429 call may not be necessary as my tests indicate it
1430 still works without it. */
1431 finish_fname_decls ();
1433 check_inline_statics ();
1435 /* This is the point to write out a PCH if we're doing that.
1436 In that case we do not want to do anything else. */
1437 if (pch_file)
1439 c_common_write_pch ();
1440 return;
1443 /* Pop off the file scope and close this translation unit. */
1444 pop_scope ();
1445 file_scope = 0;
1447 maybe_apply_pending_pragma_weaks ();
1450 /* Adjust the bindings for the start of a statement expression. */
1452 void
1453 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1455 struct c_scope *scope;
1457 for (scope = current_scope; scope != NULL; scope = scope->outer)
1459 struct c_binding *b;
1461 if (!scope->has_label_bindings)
1462 continue;
1464 for (b = scope->bindings; b != NULL; b = b->prev)
1466 struct c_label_vars *label_vars;
1467 unsigned int ix;
1468 struct c_goto_bindings *g;
1470 if (TREE_CODE (b->decl) != LABEL_DECL)
1471 continue;
1472 label_vars = b->u.label;
1473 ++label_vars->label_bindings.stmt_exprs;
1474 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1475 ++g->goto_bindings.stmt_exprs;
1479 if (switch_bindings != NULL)
1480 ++switch_bindings->stmt_exprs;
1483 /* Adjust the bindings for the end of a statement expression. */
1485 void
1486 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1488 struct c_scope *scope;
1490 for (scope = current_scope; scope != NULL; scope = scope->outer)
1492 struct c_binding *b;
1494 if (!scope->has_label_bindings)
1495 continue;
1497 for (b = scope->bindings; b != NULL; b = b->prev)
1499 struct c_label_vars *label_vars;
1500 unsigned int ix;
1501 struct c_goto_bindings *g;
1503 if (TREE_CODE (b->decl) != LABEL_DECL)
1504 continue;
1505 label_vars = b->u.label;
1506 --label_vars->label_bindings.stmt_exprs;
1507 if (label_vars->label_bindings.stmt_exprs < 0)
1509 label_vars->label_bindings.left_stmt_expr = true;
1510 label_vars->label_bindings.stmt_exprs = 0;
1512 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1514 --g->goto_bindings.stmt_exprs;
1515 if (g->goto_bindings.stmt_exprs < 0)
1517 g->goto_bindings.left_stmt_expr = true;
1518 g->goto_bindings.stmt_exprs = 0;
1524 if (switch_bindings != NULL)
1526 --switch_bindings->stmt_exprs;
1527 gcc_assert (switch_bindings->stmt_exprs >= 0);
1531 /* Push a definition or a declaration of struct, union or enum tag "name".
1532 "type" should be the type node.
1533 We assume that the tag "name" is not already defined, and has a location
1534 of LOC.
1536 Note that the definition may really be just a forward reference.
1537 In that case, the TYPE_SIZE will be zero. */
1539 static void
1540 pushtag (location_t loc, tree name, tree type)
1542 /* Record the identifier as the type's name if it has none. */
1543 if (name && !TYPE_NAME (type))
1544 TYPE_NAME (type) = name;
1545 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1547 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1548 tagged type we just added to the current scope. This fake
1549 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1550 to output a representation of a tagged type, and it also gives
1551 us a convenient place to record the "scope start" address for the
1552 tagged type. */
1554 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1555 TYPE_DECL, NULL_TREE, type));
1557 /* An approximation for now, so we can tell this is a function-scope tag.
1558 This will be updated in pop_scope. */
1559 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1561 if (warn_cxx_compat && name != NULL_TREE)
1563 struct c_binding *b = I_SYMBOL_BINDING (name);
1565 if (b != NULL
1566 && b->decl != NULL_TREE
1567 && TREE_CODE (b->decl) == TYPE_DECL
1568 && (B_IN_CURRENT_SCOPE (b)
1569 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1570 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1571 != TYPE_MAIN_VARIANT (type)))
1573 warning_at (loc, OPT_Wc___compat,
1574 ("using %qD as both a typedef and a tag is "
1575 "invalid in C++"),
1576 b->decl);
1577 if (b->locus != UNKNOWN_LOCATION)
1578 inform (b->locus, "originally defined here");
1583 /* An exported interface to pushtag. This is used by the gdb plugin's
1584 binding oracle to introduce a new tag binding. */
1586 void
1587 c_pushtag (location_t loc, tree name, tree type)
1589 pushtag (loc, name, type);
1592 /* An exported interface to bind a declaration. LOC is the location
1593 to use. DECL is the declaration to bind. The decl's name is used
1594 to determine how it is bound. If DECL is a VAR_DECL, then
1595 IS_GLOBAL determines whether the decl is put into the global (file
1596 and external) scope or the current function's scope; if DECL is not
1597 a VAR_DECL then it is always put into the file scope. */
1599 void
1600 c_bind (location_t loc, tree decl, bool is_global)
1602 struct c_scope *scope;
1603 bool nested = false;
1605 if (TREE_CODE (decl) != VAR_DECL || current_function_scope == NULL)
1607 /* Types and functions are always considered to be global. */
1608 scope = file_scope;
1609 DECL_EXTERNAL (decl) = 1;
1610 TREE_PUBLIC (decl) = 1;
1612 else if (is_global)
1614 /* Also bind it into the external scope. */
1615 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1616 nested = true;
1617 scope = file_scope;
1618 DECL_EXTERNAL (decl) = 1;
1619 TREE_PUBLIC (decl) = 1;
1621 else
1623 DECL_CONTEXT (decl) = current_function_decl;
1624 TREE_PUBLIC (decl) = 0;
1625 scope = current_function_scope;
1628 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1631 /* Subroutine of compare_decls. Allow harmless mismatches in return
1632 and argument types provided that the type modes match. This function
1633 return a unified type given a suitable match, and 0 otherwise. */
1635 static tree
1636 match_builtin_function_types (tree newtype, tree oldtype)
1638 tree newrettype, oldrettype;
1639 tree newargs, oldargs;
1640 tree trytype, tryargs;
1642 /* Accept the return type of the new declaration if same modes. */
1643 oldrettype = TREE_TYPE (oldtype);
1644 newrettype = TREE_TYPE (newtype);
1646 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1647 return 0;
1649 oldargs = TYPE_ARG_TYPES (oldtype);
1650 newargs = TYPE_ARG_TYPES (newtype);
1651 tryargs = newargs;
1653 while (oldargs || newargs)
1655 if (!oldargs
1656 || !newargs
1657 || !TREE_VALUE (oldargs)
1658 || !TREE_VALUE (newargs)
1659 || TYPE_MODE (TREE_VALUE (oldargs))
1660 != TYPE_MODE (TREE_VALUE (newargs)))
1661 return 0;
1663 oldargs = TREE_CHAIN (oldargs);
1664 newargs = TREE_CHAIN (newargs);
1667 trytype = build_function_type (newrettype, tryargs);
1668 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1671 /* Subroutine of diagnose_mismatched_decls. Check for function type
1672 mismatch involving an empty arglist vs a nonempty one and give clearer
1673 diagnostics. */
1674 static void
1675 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1676 tree newtype, tree oldtype)
1678 tree t;
1680 if (TREE_CODE (olddecl) != FUNCTION_DECL
1681 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1682 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1683 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1684 return;
1686 t = TYPE_ARG_TYPES (oldtype);
1687 if (t == 0)
1688 t = TYPE_ARG_TYPES (newtype);
1689 for (; t; t = TREE_CHAIN (t))
1691 tree type = TREE_VALUE (t);
1693 if (TREE_CHAIN (t) == 0
1694 && TYPE_MAIN_VARIANT (type) != void_type_node)
1696 inform (input_location, "a parameter list with an ellipsis can%'t match "
1697 "an empty parameter name list declaration");
1698 break;
1701 if (c_type_promotes_to (type) != type)
1703 inform (input_location, "an argument type that has a default promotion can%'t match "
1704 "an empty parameter name list declaration");
1705 break;
1710 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1711 old-style function definition, NEWDECL is a prototype declaration.
1712 Diagnose inconsistencies in the argument list. Returns TRUE if
1713 the prototype is compatible, FALSE if not. */
1714 static bool
1715 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1717 tree newargs, oldargs;
1718 int i;
1720 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1722 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1723 newargs = TYPE_ARG_TYPES (newtype);
1724 i = 1;
1726 for (;;)
1728 tree oldargtype = TREE_VALUE (oldargs);
1729 tree newargtype = TREE_VALUE (newargs);
1731 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1732 return false;
1734 oldargtype = (TYPE_ATOMIC (oldargtype)
1735 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1736 TYPE_QUAL_ATOMIC)
1737 : TYPE_MAIN_VARIANT (oldargtype));
1738 newargtype = (TYPE_ATOMIC (newargtype)
1739 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1740 TYPE_QUAL_ATOMIC)
1741 : TYPE_MAIN_VARIANT (newargtype));
1743 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1744 break;
1746 /* Reaching the end of just one list means the two decls don't
1747 agree on the number of arguments. */
1748 if (END_OF_ARGLIST (oldargtype))
1750 error ("prototype for %q+D declares more arguments "
1751 "than previous old-style definition", newdecl);
1752 return false;
1754 else if (END_OF_ARGLIST (newargtype))
1756 error ("prototype for %q+D declares fewer arguments "
1757 "than previous old-style definition", newdecl);
1758 return false;
1761 /* Type for passing arg must be consistent with that declared
1762 for the arg. */
1763 else if (!comptypes (oldargtype, newargtype))
1765 error ("prototype for %q+D declares argument %d"
1766 " with incompatible type",
1767 newdecl, i);
1768 return false;
1771 oldargs = TREE_CHAIN (oldargs);
1772 newargs = TREE_CHAIN (newargs);
1773 i++;
1776 /* If we get here, no errors were found, but do issue a warning
1777 for this poor-style construct. */
1778 warning (0, "prototype for %q+D follows non-prototype definition",
1779 newdecl);
1780 return true;
1781 #undef END_OF_ARGLIST
1784 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1785 first in a pair of mismatched declarations, using the diagnostic
1786 function DIAG. */
1787 static void
1788 locate_old_decl (tree decl)
1790 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1791 && !C_DECL_DECLARED_BUILTIN (decl))
1793 else if (DECL_INITIAL (decl))
1794 inform (input_location, "previous definition of %q+D was here", decl);
1795 else if (C_DECL_IMPLICIT (decl))
1796 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1797 else
1798 inform (input_location, "previous declaration of %q+D was here", decl);
1801 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1802 Returns true if the caller should proceed to merge the two, false
1803 if OLDDECL should simply be discarded. As a side effect, issues
1804 all necessary diagnostics for invalid or poor-style combinations.
1805 If it returns true, writes the types of NEWDECL and OLDDECL to
1806 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1807 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1809 static bool
1810 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1811 tree *newtypep, tree *oldtypep)
1813 tree newtype, oldtype;
1814 bool pedwarned = false;
1815 bool warned = false;
1816 bool retval = true;
1818 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1819 && DECL_EXTERNAL (DECL))
1821 /* If we have error_mark_node for either decl or type, just discard
1822 the previous decl - we're in an error cascade already. */
1823 if (olddecl == error_mark_node || newdecl == error_mark_node)
1824 return false;
1825 *oldtypep = oldtype = TREE_TYPE (olddecl);
1826 *newtypep = newtype = TREE_TYPE (newdecl);
1827 if (oldtype == error_mark_node || newtype == error_mark_node)
1828 return false;
1830 /* Two different categories of symbol altogether. This is an error
1831 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1832 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1834 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1835 && DECL_BUILT_IN (olddecl)
1836 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1838 error ("%q+D redeclared as different kind of symbol", newdecl);
1839 locate_old_decl (olddecl);
1841 else if (TREE_PUBLIC (newdecl))
1842 warning (0, "built-in function %q+D declared as non-function",
1843 newdecl);
1844 else
1845 warning (OPT_Wshadow, "declaration of %q+D shadows "
1846 "a built-in function", newdecl);
1847 return false;
1850 /* Enumerators have no linkage, so may only be declared once in a
1851 given scope. */
1852 if (TREE_CODE (olddecl) == CONST_DECL)
1854 error ("redeclaration of enumerator %q+D", newdecl);
1855 locate_old_decl (olddecl);
1856 return false;
1859 if (!comptypes (oldtype, newtype))
1861 if (TREE_CODE (olddecl) == FUNCTION_DECL
1862 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1864 /* Accept harmless mismatch in function types.
1865 This is for the ffs and fprintf builtins. */
1866 tree trytype = match_builtin_function_types (newtype, oldtype);
1868 if (trytype && comptypes (newtype, trytype))
1869 *oldtypep = oldtype = trytype;
1870 else
1872 /* If types don't match for a built-in, throw away the
1873 built-in. No point in calling locate_old_decl here, it
1874 won't print anything. */
1875 warning (0, "conflicting types for built-in function %q+D",
1876 newdecl);
1877 return false;
1880 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1881 && DECL_IS_BUILTIN (olddecl))
1883 /* A conflicting function declaration for a predeclared
1884 function that isn't actually built in. Objective C uses
1885 these. The new declaration silently overrides everything
1886 but the volatility (i.e. noreturn) indication. See also
1887 below. FIXME: Make Objective C use normal builtins. */
1888 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1889 return false;
1891 /* Permit void foo (...) to match int foo (...) if the latter is
1892 the definition and implicit int was used. See
1893 c-torture/compile/920625-2.c. */
1894 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1895 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1896 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1897 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1899 pedwarned = pedwarn (input_location, 0,
1900 "conflicting types for %q+D", newdecl);
1901 /* Make sure we keep void as the return type. */
1902 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1903 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1905 /* Permit void foo (...) to match an earlier call to foo (...) with
1906 no declared type (thus, implicitly int). */
1907 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1908 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1909 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1910 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1912 pedwarned = pedwarn (input_location, 0,
1913 "conflicting types for %q+D", newdecl);
1914 /* Make sure we keep void as the return type. */
1915 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1917 else
1919 int new_quals = TYPE_QUALS (newtype);
1920 int old_quals = TYPE_QUALS (oldtype);
1922 if (new_quals != old_quals)
1924 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1925 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1926 if (new_addr != old_addr)
1928 if (ADDR_SPACE_GENERIC_P (new_addr))
1929 error ("conflicting named address spaces (generic vs %s) "
1930 "for %q+D",
1931 c_addr_space_name (old_addr), newdecl);
1932 else if (ADDR_SPACE_GENERIC_P (old_addr))
1933 error ("conflicting named address spaces (%s vs generic) "
1934 "for %q+D",
1935 c_addr_space_name (new_addr), newdecl);
1936 else
1937 error ("conflicting named address spaces (%s vs %s) "
1938 "for %q+D",
1939 c_addr_space_name (new_addr),
1940 c_addr_space_name (old_addr),
1941 newdecl);
1944 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1945 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1946 error ("conflicting type qualifiers for %q+D", newdecl);
1948 else
1949 error ("conflicting types for %q+D", newdecl);
1950 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1951 locate_old_decl (olddecl);
1952 return false;
1956 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1957 but silently ignore the redeclaration if either is in a system
1958 header. (Conflicting redeclarations were handled above.) This
1959 is allowed for C11 if the types are the same, not just
1960 compatible. */
1961 if (TREE_CODE (newdecl) == TYPE_DECL)
1963 bool types_different = false;
1964 int comptypes_result;
1966 comptypes_result
1967 = comptypes_check_different_types (oldtype, newtype, &types_different);
1969 if (comptypes_result != 1 || types_different)
1971 error ("redefinition of typedef %q+D with different type", newdecl);
1972 locate_old_decl (olddecl);
1973 return false;
1976 if (DECL_IN_SYSTEM_HEADER (newdecl)
1977 || DECL_IN_SYSTEM_HEADER (olddecl)
1978 || TREE_NO_WARNING (newdecl)
1979 || TREE_NO_WARNING (olddecl))
1980 return true; /* Allow OLDDECL to continue in use. */
1982 if (variably_modified_type_p (newtype, NULL))
1984 error ("redefinition of typedef %q+D with variably modified type",
1985 newdecl);
1986 locate_old_decl (olddecl);
1988 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1989 "redefinition of typedef %q+D", newdecl))
1990 locate_old_decl (olddecl);
1992 return true;
1995 /* Function declarations can either be 'static' or 'extern' (no
1996 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1997 can never conflict with each other on account of linkage
1998 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1999 gnu89 mode permits two definitions if one is 'extern inline' and
2000 one is not. The non- extern-inline definition supersedes the
2001 extern-inline definition. */
2003 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2005 /* If you declare a built-in function name as static, or
2006 define the built-in with an old-style definition (so we
2007 can't validate the argument list) the built-in definition is
2008 overridden, but optionally warn this was a bad choice of name. */
2009 if (DECL_BUILT_IN (olddecl)
2010 && !C_DECL_DECLARED_BUILTIN (olddecl)
2011 && (!TREE_PUBLIC (newdecl)
2012 || (DECL_INITIAL (newdecl)
2013 && !prototype_p (TREE_TYPE (newdecl)))))
2015 warning (OPT_Wshadow, "declaration of %q+D shadows "
2016 "a built-in function", newdecl);
2017 /* Discard the old built-in function. */
2018 return false;
2021 if (DECL_INITIAL (newdecl))
2023 if (DECL_INITIAL (olddecl))
2025 /* If both decls are in the same TU and the new declaration
2026 isn't overriding an extern inline reject the new decl.
2027 In c99, no overriding is allowed in the same translation
2028 unit. */
2029 if ((!DECL_EXTERN_INLINE (olddecl)
2030 || DECL_EXTERN_INLINE (newdecl)
2031 || (!flag_gnu89_inline
2032 && (!DECL_DECLARED_INLINE_P (olddecl)
2033 || !lookup_attribute ("gnu_inline",
2034 DECL_ATTRIBUTES (olddecl)))
2035 && (!DECL_DECLARED_INLINE_P (newdecl)
2036 || !lookup_attribute ("gnu_inline",
2037 DECL_ATTRIBUTES (newdecl))))
2039 && same_translation_unit_p (newdecl, olddecl))
2041 error ("redefinition of %q+D", newdecl);
2042 locate_old_decl (olddecl);
2043 return false;
2047 /* If we have a prototype after an old-style function definition,
2048 the argument types must be checked specially. */
2049 else if (DECL_INITIAL (olddecl)
2050 && !prototype_p (oldtype) && prototype_p (newtype)
2051 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2052 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2054 locate_old_decl (olddecl);
2055 return false;
2057 /* A non-static declaration (even an "extern") followed by a
2058 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2059 The same is true for a static forward declaration at block
2060 scope followed by a non-static declaration/definition at file
2061 scope. Static followed by non-static at the same scope is
2062 not undefined behavior, and is the most convenient way to get
2063 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2064 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2065 we do diagnose it if -Wtraditional. */
2066 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2068 /* Two exceptions to the rule. If olddecl is an extern
2069 inline, or a predeclared function that isn't actually
2070 built in, newdecl silently overrides olddecl. The latter
2071 occur only in Objective C; see also above. (FIXME: Make
2072 Objective C use normal builtins.) */
2073 if (!DECL_IS_BUILTIN (olddecl)
2074 && !DECL_EXTERN_INLINE (olddecl))
2076 error ("static declaration of %q+D follows "
2077 "non-static declaration", newdecl);
2078 locate_old_decl (olddecl);
2080 return false;
2082 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2084 if (DECL_CONTEXT (olddecl))
2086 error ("non-static declaration of %q+D follows "
2087 "static declaration", newdecl);
2088 locate_old_decl (olddecl);
2089 return false;
2091 else if (warn_traditional)
2093 warned |= warning (OPT_Wtraditional,
2094 "non-static declaration of %q+D "
2095 "follows static declaration", newdecl);
2099 /* Make sure gnu_inline attribute is either not present, or
2100 present on all inline decls. */
2101 if (DECL_DECLARED_INLINE_P (olddecl)
2102 && DECL_DECLARED_INLINE_P (newdecl))
2104 bool newa = lookup_attribute ("gnu_inline",
2105 DECL_ATTRIBUTES (newdecl)) != NULL;
2106 bool olda = lookup_attribute ("gnu_inline",
2107 DECL_ATTRIBUTES (olddecl)) != NULL;
2108 if (newa != olda)
2110 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2111 newa ? newdecl : olddecl);
2112 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2113 "but not here");
2117 else if (TREE_CODE (newdecl) == VAR_DECL)
2119 /* Only variables can be thread-local, and all declarations must
2120 agree on this property. */
2121 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2123 /* Nothing to check. Since OLDDECL is marked threadprivate
2124 and NEWDECL does not have a thread-local attribute, we
2125 will merge the threadprivate attribute into NEWDECL. */
2128 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2130 if (DECL_THREAD_LOCAL_P (newdecl))
2131 error ("thread-local declaration of %q+D follows "
2132 "non-thread-local declaration", newdecl);
2133 else
2134 error ("non-thread-local declaration of %q+D follows "
2135 "thread-local declaration", newdecl);
2137 locate_old_decl (olddecl);
2138 return false;
2141 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2142 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2144 error ("redefinition of %q+D", newdecl);
2145 locate_old_decl (olddecl);
2146 return false;
2149 /* Objects declared at file scope: if the first declaration had
2150 external linkage (even if it was an external reference) the
2151 second must have external linkage as well, or the behavior is
2152 undefined. If the first declaration had internal linkage, then
2153 the second must too, or else be an external reference (in which
2154 case the composite declaration still has internal linkage).
2155 As for function declarations, we warn about the static-then-
2156 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2157 if (DECL_FILE_SCOPE_P (newdecl)
2158 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2160 if (DECL_EXTERNAL (newdecl))
2162 if (!DECL_FILE_SCOPE_P (olddecl))
2164 error ("extern declaration of %q+D follows "
2165 "declaration with no linkage", newdecl);
2166 locate_old_decl (olddecl);
2167 return false;
2169 else if (warn_traditional)
2171 warned |= warning (OPT_Wtraditional,
2172 "non-static declaration of %q+D "
2173 "follows static declaration", newdecl);
2176 else
2178 if (TREE_PUBLIC (newdecl))
2179 error ("non-static declaration of %q+D follows "
2180 "static declaration", newdecl);
2181 else
2182 error ("static declaration of %q+D follows "
2183 "non-static declaration", newdecl);
2185 locate_old_decl (olddecl);
2186 return false;
2189 /* Two objects with the same name declared at the same block
2190 scope must both be external references (6.7p3). */
2191 else if (!DECL_FILE_SCOPE_P (newdecl))
2193 if (DECL_EXTERNAL (newdecl))
2195 /* Extern with initializer at block scope, which will
2196 already have received an error. */
2198 else if (DECL_EXTERNAL (olddecl))
2200 error ("declaration of %q+D with no linkage follows "
2201 "extern declaration", newdecl);
2202 locate_old_decl (olddecl);
2204 else
2206 error ("redeclaration of %q+D with no linkage", newdecl);
2207 locate_old_decl (olddecl);
2210 return false;
2213 /* C++ does not permit a decl to appear multiple times at file
2214 scope. */
2215 if (warn_cxx_compat
2216 && DECL_FILE_SCOPE_P (newdecl)
2217 && !DECL_EXTERNAL (newdecl)
2218 && !DECL_EXTERNAL (olddecl))
2219 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2220 OPT_Wc___compat,
2221 ("duplicate declaration of %qD is "
2222 "invalid in C++"),
2223 newdecl);
2226 /* warnings */
2227 /* All decls must agree on a visibility. */
2228 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2229 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2230 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2232 warned |= warning (0, "redeclaration of %q+D with different visibility "
2233 "(old visibility preserved)", newdecl);
2236 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2238 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2239 if (DECL_DECLARED_INLINE_P (newdecl)
2240 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2241 warned |= warning (OPT_Wattributes,
2242 "inline declaration of %qD follows "
2243 "declaration with attribute noinline", newdecl);
2244 else if (DECL_DECLARED_INLINE_P (olddecl)
2245 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2246 warned |= warning (OPT_Wattributes,
2247 "declaration of %q+D with attribute "
2248 "noinline follows inline declaration ", newdecl);
2249 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2250 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2251 warned |= warning (OPT_Wattributes,
2252 "declaration of %q+D with attribute "
2253 "%qs follows declaration with attribute %qs",
2254 newdecl, "noinline", "always_inline");
2255 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2256 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2257 warned |= warning (OPT_Wattributes,
2258 "declaration of %q+D with attribute "
2259 "%qs follows declaration with attribute %qs",
2260 newdecl, "always_inline", "noinline");
2261 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2262 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2263 warned |= warning (OPT_Wattributes,
2264 "declaration of %q+D with attribute %qs follows "
2265 "declaration with attribute %qs", newdecl, "cold",
2266 "hot");
2267 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2268 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2269 warned |= warning (OPT_Wattributes,
2270 "declaration of %q+D with attribute %qs follows "
2271 "declaration with attribute %qs", newdecl, "hot",
2272 "cold");
2274 else /* PARM_DECL, VAR_DECL */
2276 /* Redeclaration of a parameter is a constraint violation (this is
2277 not explicitly stated, but follows from C99 6.7p3 [no more than
2278 one declaration of the same identifier with no linkage in the
2279 same scope, except type tags] and 6.2.2p6 [parameters have no
2280 linkage]). We must check for a forward parameter declaration,
2281 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2282 an extension, the mandatory diagnostic for which is handled by
2283 mark_forward_parm_decls. */
2285 if (TREE_CODE (newdecl) == PARM_DECL
2286 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2288 error ("redefinition of parameter %q+D", newdecl);
2289 locate_old_decl (olddecl);
2290 return false;
2294 /* Optional warning for completely redundant decls. */
2295 if (!warned && !pedwarned
2296 && warn_redundant_decls
2297 /* Don't warn about a function declaration followed by a
2298 definition. */
2299 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2300 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2301 /* Don't warn about redundant redeclarations of builtins. */
2302 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2303 && !DECL_BUILT_IN (newdecl)
2304 && DECL_BUILT_IN (olddecl)
2305 && !C_DECL_DECLARED_BUILTIN (olddecl))
2306 /* Don't warn about an extern followed by a definition. */
2307 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2308 /* Don't warn about forward parameter decls. */
2309 && !(TREE_CODE (newdecl) == PARM_DECL
2310 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2311 /* Don't warn about a variable definition following a declaration. */
2312 && !(TREE_CODE (newdecl) == VAR_DECL
2313 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2315 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2316 newdecl);
2319 /* Report location of previous decl/defn. */
2320 if (warned || pedwarned)
2321 locate_old_decl (olddecl);
2323 #undef DECL_EXTERN_INLINE
2325 return retval;
2328 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2329 consistent with OLDDECL, but carries new information. Merge the
2330 new information into OLDDECL. This function issues no
2331 diagnostics. */
2333 static void
2334 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2336 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2337 && DECL_INITIAL (newdecl) != 0);
2338 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2339 && prototype_p (TREE_TYPE (newdecl)));
2340 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2341 && prototype_p (TREE_TYPE (olddecl)));
2343 /* For real parm decl following a forward decl, rechain the old decl
2344 in its new location and clear TREE_ASM_WRITTEN (it's not a
2345 forward decl anymore). */
2346 if (TREE_CODE (newdecl) == PARM_DECL
2347 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2349 struct c_binding *b, **here;
2351 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2352 if ((*here)->decl == olddecl)
2353 goto found;
2354 gcc_unreachable ();
2356 found:
2357 b = *here;
2358 *here = b->prev;
2359 b->prev = current_scope->bindings;
2360 current_scope->bindings = b;
2362 TREE_ASM_WRITTEN (olddecl) = 0;
2365 DECL_ATTRIBUTES (newdecl)
2366 = targetm.merge_decl_attributes (olddecl, newdecl);
2368 /* Merge the data types specified in the two decls. */
2369 TREE_TYPE (newdecl)
2370 = TREE_TYPE (olddecl)
2371 = composite_type (newtype, oldtype);
2373 /* Lay the type out, unless already done. */
2374 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2376 if (TREE_TYPE (newdecl) != error_mark_node)
2377 layout_type (TREE_TYPE (newdecl));
2378 if (TREE_CODE (newdecl) != FUNCTION_DECL
2379 && TREE_CODE (newdecl) != TYPE_DECL
2380 && TREE_CODE (newdecl) != CONST_DECL)
2381 layout_decl (newdecl, 0);
2383 else
2385 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2386 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2387 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2388 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2389 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2391 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2392 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2396 /* Keep the old rtl since we can safely use it. */
2397 if (HAS_RTL_P (olddecl))
2398 COPY_DECL_RTL (olddecl, newdecl);
2400 /* Merge the type qualifiers. */
2401 if (TREE_READONLY (newdecl))
2402 TREE_READONLY (olddecl) = 1;
2404 if (TREE_THIS_VOLATILE (newdecl))
2405 TREE_THIS_VOLATILE (olddecl) = 1;
2407 /* Merge deprecatedness. */
2408 if (TREE_DEPRECATED (newdecl))
2409 TREE_DEPRECATED (olddecl) = 1;
2411 /* If a decl is in a system header and the other isn't, keep the one on the
2412 system header. Otherwise, keep source location of definition rather than
2413 declaration and of prototype rather than non-prototype unless that
2414 prototype is built-in. */
2415 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2416 && DECL_IN_SYSTEM_HEADER (olddecl)
2417 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2418 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2419 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2420 && DECL_IN_SYSTEM_HEADER (newdecl)
2421 && !DECL_IN_SYSTEM_HEADER (olddecl))
2422 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2423 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2424 || (old_is_prototype && !new_is_prototype
2425 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2426 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2428 /* Merge the initialization information. */
2429 if (DECL_INITIAL (newdecl) == 0)
2430 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2432 /* Merge the threadprivate attribute. */
2433 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2434 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2436 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2438 /* Copy the assembler name.
2439 Currently, it can only be defined in the prototype. */
2440 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2442 /* Use visibility of whichever declaration had it specified */
2443 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2445 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2446 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2449 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2451 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2452 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2453 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2454 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2455 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2456 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2457 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2458 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2459 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2460 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2461 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2464 /* Merge the storage class information. */
2465 merge_weak (newdecl, olddecl);
2467 /* For functions, static overrides non-static. */
2468 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2470 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2471 /* This is since we don't automatically
2472 copy the attributes of NEWDECL into OLDDECL. */
2473 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2474 /* If this clears `static', clear it in the identifier too. */
2475 if (!TREE_PUBLIC (olddecl))
2476 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2480 /* In c99, 'extern' declaration before (or after) 'inline' means this
2481 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2482 is present. */
2483 if (TREE_CODE (newdecl) == FUNCTION_DECL
2484 && !flag_gnu89_inline
2485 && (DECL_DECLARED_INLINE_P (newdecl)
2486 || DECL_DECLARED_INLINE_P (olddecl))
2487 && (!DECL_DECLARED_INLINE_P (newdecl)
2488 || !DECL_DECLARED_INLINE_P (olddecl)
2489 || !DECL_EXTERNAL (olddecl))
2490 && DECL_EXTERNAL (newdecl)
2491 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2492 && !current_function_decl)
2493 DECL_EXTERNAL (newdecl) = 0;
2495 /* An inline definition following a static declaration is not
2496 DECL_EXTERNAL. */
2497 if (new_is_definition
2498 && (DECL_DECLARED_INLINE_P (newdecl)
2499 || DECL_DECLARED_INLINE_P (olddecl))
2500 && !TREE_PUBLIC (olddecl))
2501 DECL_EXTERNAL (newdecl) = 0;
2503 if (DECL_EXTERNAL (newdecl))
2505 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2506 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2508 /* An extern decl does not override previous storage class. */
2509 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2510 if (!DECL_EXTERNAL (newdecl))
2512 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2513 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2516 else
2518 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2519 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2522 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2524 /* If we're redefining a function previously defined as extern
2525 inline, make sure we emit debug info for the inline before we
2526 throw it away, in case it was inlined into a function that
2527 hasn't been written out yet. */
2528 if (new_is_definition && DECL_INITIAL (olddecl))
2529 /* The new defn must not be inline. */
2530 DECL_UNINLINABLE (newdecl) = 1;
2531 else
2533 /* If either decl says `inline', this fn is inline, unless
2534 its definition was passed already. */
2535 if (DECL_DECLARED_INLINE_P (newdecl)
2536 || DECL_DECLARED_INLINE_P (olddecl))
2537 DECL_DECLARED_INLINE_P (newdecl) = 1;
2539 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2540 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2542 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2543 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2544 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2545 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2548 if (DECL_BUILT_IN (olddecl))
2550 /* If redeclaring a builtin function, it stays built in.
2551 But it gets tagged as having been declared. */
2552 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2553 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2554 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2555 if (new_is_prototype)
2557 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2558 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2560 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2561 switch (fncode)
2563 /* If a compatible prototype of these builtin functions
2564 is seen, assume the runtime implements it with the
2565 expected semantics. */
2566 case BUILT_IN_STPCPY:
2567 if (builtin_decl_explicit_p (fncode))
2568 set_builtin_decl_implicit_p (fncode, true);
2569 break;
2570 default:
2571 if (builtin_decl_explicit_p (fncode))
2572 set_builtin_decl_declared_p (fncode, true);
2573 break;
2577 else
2578 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2579 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2582 /* Preserve function specific target and optimization options */
2583 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2584 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2585 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2586 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2588 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2589 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2590 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2591 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2593 /* Also preserve various other info from the definition. */
2594 if (!new_is_definition)
2596 tree t;
2597 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2598 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2599 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2600 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2601 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2602 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2603 DECL_CONTEXT (t) = newdecl;
2605 /* See if we've got a function to instantiate from. */
2606 if (DECL_SAVED_TREE (olddecl))
2607 DECL_ABSTRACT_ORIGIN (newdecl)
2608 = DECL_ABSTRACT_ORIGIN (olddecl);
2612 /* Merge the USED information. */
2613 if (TREE_USED (olddecl))
2614 TREE_USED (newdecl) = 1;
2615 else if (TREE_USED (newdecl))
2616 TREE_USED (olddecl) = 1;
2617 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2618 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2619 if (DECL_PRESERVE_P (olddecl))
2620 DECL_PRESERVE_P (newdecl) = 1;
2621 else if (DECL_PRESERVE_P (newdecl))
2622 DECL_PRESERVE_P (olddecl) = 1;
2624 /* Merge DECL_COMMON */
2625 if (VAR_P (olddecl) && VAR_P (newdecl)
2626 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2627 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2628 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2630 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2631 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2632 DECL_ARGUMENTS (if appropriate). */
2634 unsigned olddecl_uid = DECL_UID (olddecl);
2635 tree olddecl_context = DECL_CONTEXT (olddecl);
2636 tree olddecl_arguments = NULL;
2637 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2638 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2640 memcpy ((char *) olddecl + sizeof (struct tree_common),
2641 (char *) newdecl + sizeof (struct tree_common),
2642 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2643 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2644 switch (TREE_CODE (olddecl))
2646 case FUNCTION_DECL:
2647 case VAR_DECL:
2649 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2651 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2652 (char *) newdecl + sizeof (struct tree_decl_common),
2653 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2654 olddecl->decl_with_vis.symtab_node = snode;
2656 if ((DECL_EXTERNAL (olddecl)
2657 || TREE_PUBLIC (olddecl)
2658 || TREE_STATIC (olddecl))
2659 && DECL_SECTION_NAME (newdecl) != NULL)
2660 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2662 /* This isn't quite correct for something like
2663 int __thread x attribute ((tls_model ("local-exec")));
2664 extern int __thread x;
2665 as we'll lose the "local-exec" model. */
2666 if (TREE_CODE (olddecl) == VAR_DECL
2667 && DECL_THREAD_LOCAL_P (newdecl))
2668 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2669 break;
2672 case FIELD_DECL:
2673 case PARM_DECL:
2674 case LABEL_DECL:
2675 case RESULT_DECL:
2676 case CONST_DECL:
2677 case TYPE_DECL:
2678 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2679 (char *) newdecl + sizeof (struct tree_decl_common),
2680 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2681 break;
2683 default:
2685 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2686 (char *) newdecl + sizeof (struct tree_decl_common),
2687 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2689 DECL_UID (olddecl) = olddecl_uid;
2690 DECL_CONTEXT (olddecl) = olddecl_context;
2691 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2692 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2695 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2696 so that encode_section_info has a chance to look at the new decl
2697 flags and attributes. */
2698 if (DECL_RTL_SET_P (olddecl)
2699 && (TREE_CODE (olddecl) == FUNCTION_DECL
2700 || (TREE_CODE (olddecl) == VAR_DECL
2701 && TREE_STATIC (olddecl))))
2702 make_decl_rtl (olddecl);
2705 /* Handle when a new declaration NEWDECL has the same name as an old
2706 one OLDDECL in the same binding contour. Prints an error message
2707 if appropriate.
2709 If safely possible, alter OLDDECL to look like NEWDECL, and return
2710 true. Otherwise, return false. */
2712 static bool
2713 duplicate_decls (tree newdecl, tree olddecl)
2715 tree newtype = NULL, oldtype = NULL;
2717 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2719 /* Avoid `unused variable' and other warnings for OLDDECL. */
2720 TREE_NO_WARNING (olddecl) = 1;
2721 return false;
2724 merge_decls (newdecl, olddecl, newtype, oldtype);
2726 /* The NEWDECL will no longer be needed.
2728 Before releasing the node, be sure to remove function from symbol
2729 table that might have been inserted there to record comdat group.
2730 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2731 structure is shared in between NEWDECL and OLDECL. */
2732 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2733 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2734 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2736 struct symtab_node *snode = symtab_node::get (newdecl);
2737 if (snode)
2738 snode->remove ();
2740 ggc_free (newdecl);
2741 return true;
2745 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2746 static void
2747 warn_if_shadowing (tree new_decl)
2749 struct c_binding *b;
2751 /* Shadow warnings wanted? */
2752 if (!warn_shadow
2753 /* No shadow warnings for internally generated vars. */
2754 || DECL_IS_BUILTIN (new_decl)
2755 /* No shadow warnings for vars made for inlining. */
2756 || DECL_FROM_INLINE (new_decl))
2757 return;
2759 /* Is anything being shadowed? Invisible decls do not count. */
2760 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2761 if (b->decl && b->decl != new_decl && !b->invisible
2762 && (b->decl == error_mark_node
2763 || diagnostic_report_warnings_p (global_dc,
2764 DECL_SOURCE_LOCATION (b->decl))))
2766 tree old_decl = b->decl;
2767 bool warned = false;
2769 if (old_decl == error_mark_node)
2771 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2772 "non-variable", new_decl);
2773 break;
2775 else if (TREE_CODE (old_decl) == PARM_DECL)
2776 warned = warning (OPT_Wshadow,
2777 "declaration of %q+D shadows a parameter",
2778 new_decl);
2779 else if (DECL_FILE_SCOPE_P (old_decl))
2781 /* Do not warn if a variable shadows a function, unless
2782 the variable is a function or a pointer-to-function. */
2783 if (TREE_CODE (old_decl) == FUNCTION_DECL
2784 && TREE_CODE (new_decl) != FUNCTION_DECL
2785 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2786 continue;
2788 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2789 "declaration of %qD shadows a global "
2790 "declaration",
2791 new_decl);
2793 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2794 && DECL_BUILT_IN (old_decl))
2796 warning (OPT_Wshadow, "declaration of %q+D shadows "
2797 "a built-in function", new_decl);
2798 break;
2800 else
2801 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2802 "previous local", new_decl);
2804 if (warned)
2805 inform (DECL_SOURCE_LOCATION (old_decl),
2806 "shadowed declaration is here");
2808 break;
2812 /* Record a decl-node X as belonging to the current lexical scope.
2813 Check for errors (such as an incompatible declaration for the same
2814 name already seen in the same scope).
2816 Returns either X or an old decl for the same name.
2817 If an old decl is returned, it may have been smashed
2818 to agree with what X says. */
2820 tree
2821 pushdecl (tree x)
2823 tree name = DECL_NAME (x);
2824 struct c_scope *scope = current_scope;
2825 struct c_binding *b;
2826 bool nested = false;
2827 location_t locus = DECL_SOURCE_LOCATION (x);
2829 /* Must set DECL_CONTEXT for everything not at file scope or
2830 DECL_FILE_SCOPE_P won't work. Local externs don't count
2831 unless they have initializers (which generate code). */
2832 if (current_function_decl
2833 && (!VAR_OR_FUNCTION_DECL_P (x)
2834 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2835 DECL_CONTEXT (x) = current_function_decl;
2837 /* Anonymous decls are just inserted in the scope. */
2838 if (!name)
2840 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2841 locus);
2842 return x;
2845 /* First, see if there is another declaration with the same name in
2846 the current scope. If there is, duplicate_decls may do all the
2847 work for us. If duplicate_decls returns false, that indicates
2848 two incompatible decls in the same scope; we are to silently
2849 replace the old one (duplicate_decls has issued all appropriate
2850 diagnostics). In particular, we should not consider possible
2851 duplicates in the external scope, or shadowing. */
2852 b = I_SYMBOL_BINDING (name);
2853 if (b && B_IN_SCOPE (b, scope))
2855 struct c_binding *b_ext, *b_use;
2856 tree type = TREE_TYPE (x);
2857 tree visdecl = b->decl;
2858 tree vistype = TREE_TYPE (visdecl);
2859 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2860 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2861 b->inner_comp = false;
2862 b_use = b;
2863 b_ext = b;
2864 /* If this is an external linkage declaration, we should check
2865 for compatibility with the type in the external scope before
2866 setting the type at this scope based on the visible
2867 information only. */
2868 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2870 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2871 b_ext = b_ext->shadowed;
2872 if (b_ext)
2874 b_use = b_ext;
2875 if (b_use->u.type)
2876 TREE_TYPE (b_use->decl) = b_use->u.type;
2879 if (duplicate_decls (x, b_use->decl))
2881 if (b_use != b)
2883 /* Save the updated type in the external scope and
2884 restore the proper type for this scope. */
2885 tree thistype;
2886 if (comptypes (vistype, type))
2887 thistype = composite_type (vistype, type);
2888 else
2889 thistype = TREE_TYPE (b_use->decl);
2890 b_use->u.type = TREE_TYPE (b_use->decl);
2891 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2892 && DECL_BUILT_IN (b_use->decl))
2893 thistype
2894 = build_type_attribute_variant (thistype,
2895 TYPE_ATTRIBUTES
2896 (b_use->u.type));
2897 TREE_TYPE (b_use->decl) = thistype;
2899 return b_use->decl;
2901 else
2902 goto skip_external_and_shadow_checks;
2905 /* All declarations with external linkage, and all external
2906 references, go in the external scope, no matter what scope is
2907 current. However, the binding in that scope is ignored for
2908 purposes of normal name lookup. A separate binding structure is
2909 created in the requested scope; this governs the normal
2910 visibility of the symbol.
2912 The binding in the externals scope is used exclusively for
2913 detecting duplicate declarations of the same object, no matter
2914 what scope they are in; this is what we do here. (C99 6.2.7p2:
2915 All declarations that refer to the same object or function shall
2916 have compatible type; otherwise, the behavior is undefined.) */
2917 if (DECL_EXTERNAL (x) || scope == file_scope)
2919 tree type = TREE_TYPE (x);
2920 tree vistype = 0;
2921 tree visdecl = 0;
2922 bool type_saved = false;
2923 if (b && !B_IN_EXTERNAL_SCOPE (b)
2924 && VAR_OR_FUNCTION_DECL_P (b->decl)
2925 && DECL_FILE_SCOPE_P (b->decl))
2927 visdecl = b->decl;
2928 vistype = TREE_TYPE (visdecl);
2930 if (scope != file_scope
2931 && !DECL_IN_SYSTEM_HEADER (x))
2932 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2934 while (b && !B_IN_EXTERNAL_SCOPE (b))
2936 /* If this decl might be modified, save its type. This is
2937 done here rather than when the decl is first bound
2938 because the type may change after first binding, through
2939 being completed or through attributes being added. If we
2940 encounter multiple such decls, only the first should have
2941 its type saved; the others will already have had their
2942 proper types saved and the types will not have changed as
2943 their scopes will not have been re-entered. */
2944 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2946 b->u.type = TREE_TYPE (b->decl);
2947 type_saved = true;
2949 if (B_IN_FILE_SCOPE (b)
2950 && TREE_CODE (b->decl) == VAR_DECL
2951 && TREE_STATIC (b->decl)
2952 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2953 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2954 && TREE_CODE (type) == ARRAY_TYPE
2955 && TYPE_DOMAIN (type)
2956 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2957 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2959 /* Array type completed in inner scope, which should be
2960 diagnosed if the completion does not have size 1 and
2961 it does not get completed in the file scope. */
2962 b->inner_comp = true;
2964 b = b->shadowed;
2967 /* If a matching external declaration has been found, set its
2968 type to the composite of all the types of that declaration.
2969 After the consistency checks, it will be reset to the
2970 composite of the visible types only. */
2971 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2972 && b->u.type)
2973 TREE_TYPE (b->decl) = b->u.type;
2975 /* The point of the same_translation_unit_p check here is,
2976 we want to detect a duplicate decl for a construct like
2977 foo() { extern bar(); } ... static bar(); but not if
2978 they are in different translation units. In any case,
2979 the static does not go in the externals scope. */
2980 if (b
2981 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2982 && duplicate_decls (x, b->decl))
2984 tree thistype;
2985 if (vistype)
2987 if (comptypes (vistype, type))
2988 thistype = composite_type (vistype, type);
2989 else
2990 thistype = TREE_TYPE (b->decl);
2992 else
2993 thistype = type;
2994 b->u.type = TREE_TYPE (b->decl);
2995 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2996 thistype
2997 = build_type_attribute_variant (thistype,
2998 TYPE_ATTRIBUTES (b->u.type));
2999 TREE_TYPE (b->decl) = thistype;
3000 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3001 locus);
3002 return b->decl;
3004 else if (TREE_PUBLIC (x))
3006 if (visdecl && !b && duplicate_decls (x, visdecl))
3008 /* An external declaration at block scope referring to a
3009 visible entity with internal linkage. The composite
3010 type will already be correct for this scope, so we
3011 just need to fall through to make the declaration in
3012 this scope. */
3013 nested = true;
3014 x = visdecl;
3016 else
3018 bind (name, x, external_scope, /*invisible=*/true,
3019 /*nested=*/false, locus);
3020 nested = true;
3025 if (TREE_CODE (x) != PARM_DECL)
3026 warn_if_shadowing (x);
3028 skip_external_and_shadow_checks:
3029 if (TREE_CODE (x) == TYPE_DECL)
3031 /* So this is a typedef, set its underlying type. */
3032 set_underlying_type (x);
3034 /* If X is a typedef defined in the current function, record it
3035 for the purpose of implementing the -Wunused-local-typedefs
3036 warning. */
3037 record_locally_defined_typedef (x);
3040 bind (name, x, scope, /*invisible=*/false, nested, locus);
3042 /* If x's type is incomplete because it's based on a
3043 structure or union which has not yet been fully declared,
3044 attach it to that structure or union type, so we can go
3045 back and complete the variable declaration later, if the
3046 structure or union gets fully declared.
3048 If the input is erroneous, we can have error_mark in the type
3049 slot (e.g. "f(void a, ...)") - that doesn't count as an
3050 incomplete type. */
3051 if (TREE_TYPE (x) != error_mark_node
3052 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3054 tree element = TREE_TYPE (x);
3056 while (TREE_CODE (element) == ARRAY_TYPE)
3057 element = TREE_TYPE (element);
3058 element = TYPE_MAIN_VARIANT (element);
3060 if ((TREE_CODE (element) == RECORD_TYPE
3061 || TREE_CODE (element) == UNION_TYPE)
3062 && (TREE_CODE (x) != TYPE_DECL
3063 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3064 && !COMPLETE_TYPE_P (element))
3065 C_TYPE_INCOMPLETE_VARS (element)
3066 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3068 return x;
3071 /* Record X as belonging to file scope.
3072 This is used only internally by the Objective-C front end,
3073 and is limited to its needs. duplicate_decls is not called;
3074 if there is any preexisting decl for this identifier, it is an ICE. */
3076 tree
3077 pushdecl_top_level (tree x)
3079 tree name;
3080 bool nested = false;
3081 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
3083 name = DECL_NAME (x);
3085 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3087 if (TREE_PUBLIC (x))
3089 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3090 UNKNOWN_LOCATION);
3091 nested = true;
3093 if (file_scope)
3094 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3096 return x;
3099 static void
3100 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3102 if (warn_implicit_function_declaration)
3104 bool warned;
3106 if (flag_isoc99)
3107 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3108 "implicit declaration of function %qE", id);
3109 else
3110 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3111 G_("implicit declaration of function %qE"), id);
3112 if (olddecl && warned)
3113 locate_old_decl (olddecl);
3117 /* This function represents mapping of a function code FCODE
3118 to its respective header. */
3120 static const char *
3121 header_for_builtin_fn (enum built_in_function fcode)
3123 switch (fcode)
3125 CASE_FLT_FN (BUILT_IN_ACOS):
3126 CASE_FLT_FN (BUILT_IN_ACOSH):
3127 CASE_FLT_FN (BUILT_IN_ASIN):
3128 CASE_FLT_FN (BUILT_IN_ASINH):
3129 CASE_FLT_FN (BUILT_IN_ATAN):
3130 CASE_FLT_FN (BUILT_IN_ATANH):
3131 CASE_FLT_FN (BUILT_IN_ATAN2):
3132 CASE_FLT_FN (BUILT_IN_CBRT):
3133 CASE_FLT_FN (BUILT_IN_CEIL):
3134 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3135 CASE_FLT_FN (BUILT_IN_COS):
3136 CASE_FLT_FN (BUILT_IN_COSH):
3137 CASE_FLT_FN (BUILT_IN_ERF):
3138 CASE_FLT_FN (BUILT_IN_ERFC):
3139 CASE_FLT_FN (BUILT_IN_EXP):
3140 CASE_FLT_FN (BUILT_IN_EXP2):
3141 CASE_FLT_FN (BUILT_IN_EXPM1):
3142 CASE_FLT_FN (BUILT_IN_FABS):
3143 CASE_FLT_FN (BUILT_IN_FDIM):
3144 CASE_FLT_FN (BUILT_IN_FLOOR):
3145 CASE_FLT_FN (BUILT_IN_FMA):
3146 CASE_FLT_FN (BUILT_IN_FMAX):
3147 CASE_FLT_FN (BUILT_IN_FMIN):
3148 CASE_FLT_FN (BUILT_IN_FMOD):
3149 CASE_FLT_FN (BUILT_IN_FREXP):
3150 CASE_FLT_FN (BUILT_IN_HYPOT):
3151 CASE_FLT_FN (BUILT_IN_ILOGB):
3152 CASE_FLT_FN (BUILT_IN_LDEXP):
3153 CASE_FLT_FN (BUILT_IN_LGAMMA):
3154 CASE_FLT_FN (BUILT_IN_LLRINT):
3155 CASE_FLT_FN (BUILT_IN_LLROUND):
3156 CASE_FLT_FN (BUILT_IN_LOG):
3157 CASE_FLT_FN (BUILT_IN_LOG10):
3158 CASE_FLT_FN (BUILT_IN_LOG1P):
3159 CASE_FLT_FN (BUILT_IN_LOG2):
3160 CASE_FLT_FN (BUILT_IN_LOGB):
3161 CASE_FLT_FN (BUILT_IN_LRINT):
3162 CASE_FLT_FN (BUILT_IN_LROUND):
3163 CASE_FLT_FN (BUILT_IN_MODF):
3164 CASE_FLT_FN (BUILT_IN_NAN):
3165 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3166 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3167 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3168 CASE_FLT_FN (BUILT_IN_POW):
3169 CASE_FLT_FN (BUILT_IN_REMAINDER):
3170 CASE_FLT_FN (BUILT_IN_REMQUO):
3171 CASE_FLT_FN (BUILT_IN_RINT):
3172 CASE_FLT_FN (BUILT_IN_ROUND):
3173 CASE_FLT_FN (BUILT_IN_SCALBLN):
3174 CASE_FLT_FN (BUILT_IN_SCALBN):
3175 CASE_FLT_FN (BUILT_IN_SIN):
3176 CASE_FLT_FN (BUILT_IN_SINH):
3177 CASE_FLT_FN (BUILT_IN_SINCOS):
3178 CASE_FLT_FN (BUILT_IN_SQRT):
3179 CASE_FLT_FN (BUILT_IN_TAN):
3180 CASE_FLT_FN (BUILT_IN_TANH):
3181 CASE_FLT_FN (BUILT_IN_TGAMMA):
3182 CASE_FLT_FN (BUILT_IN_TRUNC):
3183 case BUILT_IN_ISINF:
3184 case BUILT_IN_ISNAN:
3185 return "<math.h>";
3186 CASE_FLT_FN (BUILT_IN_CABS):
3187 CASE_FLT_FN (BUILT_IN_CACOS):
3188 CASE_FLT_FN (BUILT_IN_CACOSH):
3189 CASE_FLT_FN (BUILT_IN_CARG):
3190 CASE_FLT_FN (BUILT_IN_CASIN):
3191 CASE_FLT_FN (BUILT_IN_CASINH):
3192 CASE_FLT_FN (BUILT_IN_CATAN):
3193 CASE_FLT_FN (BUILT_IN_CATANH):
3194 CASE_FLT_FN (BUILT_IN_CCOS):
3195 CASE_FLT_FN (BUILT_IN_CCOSH):
3196 CASE_FLT_FN (BUILT_IN_CEXP):
3197 CASE_FLT_FN (BUILT_IN_CIMAG):
3198 CASE_FLT_FN (BUILT_IN_CLOG):
3199 CASE_FLT_FN (BUILT_IN_CONJ):
3200 CASE_FLT_FN (BUILT_IN_CPOW):
3201 CASE_FLT_FN (BUILT_IN_CPROJ):
3202 CASE_FLT_FN (BUILT_IN_CREAL):
3203 CASE_FLT_FN (BUILT_IN_CSIN):
3204 CASE_FLT_FN (BUILT_IN_CSINH):
3205 CASE_FLT_FN (BUILT_IN_CSQRT):
3206 CASE_FLT_FN (BUILT_IN_CTAN):
3207 CASE_FLT_FN (BUILT_IN_CTANH):
3208 return "<complex.h>";
3209 case BUILT_IN_MEMCHR:
3210 case BUILT_IN_MEMCMP:
3211 case BUILT_IN_MEMCPY:
3212 case BUILT_IN_MEMMOVE:
3213 case BUILT_IN_MEMSET:
3214 case BUILT_IN_STRCAT:
3215 case BUILT_IN_STRCHR:
3216 case BUILT_IN_STRCMP:
3217 case BUILT_IN_STRCPY:
3218 case BUILT_IN_STRCSPN:
3219 case BUILT_IN_STRLEN:
3220 case BUILT_IN_STRNCAT:
3221 case BUILT_IN_STRNCMP:
3222 case BUILT_IN_STRNCPY:
3223 case BUILT_IN_STRPBRK:
3224 case BUILT_IN_STRRCHR:
3225 case BUILT_IN_STRSPN:
3226 case BUILT_IN_STRSTR:
3227 return "<string.h>";
3228 case BUILT_IN_FPRINTF:
3229 case BUILT_IN_PUTC:
3230 case BUILT_IN_FPUTC:
3231 case BUILT_IN_FPUTS:
3232 case BUILT_IN_FSCANF:
3233 case BUILT_IN_FWRITE:
3234 case BUILT_IN_PRINTF:
3235 case BUILT_IN_PUTCHAR:
3236 case BUILT_IN_PUTS:
3237 case BUILT_IN_SCANF:
3238 case BUILT_IN_SNPRINTF:
3239 case BUILT_IN_SPRINTF:
3240 case BUILT_IN_SSCANF:
3241 case BUILT_IN_VFPRINTF:
3242 case BUILT_IN_VFSCANF:
3243 case BUILT_IN_VPRINTF:
3244 case BUILT_IN_VSCANF:
3245 case BUILT_IN_VSNPRINTF:
3246 case BUILT_IN_VSPRINTF:
3247 case BUILT_IN_VSSCANF:
3248 return "<stdio.h>";
3249 case BUILT_IN_ISALNUM:
3250 case BUILT_IN_ISALPHA:
3251 case BUILT_IN_ISBLANK:
3252 case BUILT_IN_ISCNTRL:
3253 case BUILT_IN_ISDIGIT:
3254 case BUILT_IN_ISGRAPH:
3255 case BUILT_IN_ISLOWER:
3256 case BUILT_IN_ISPRINT:
3257 case BUILT_IN_ISPUNCT:
3258 case BUILT_IN_ISSPACE:
3259 case BUILT_IN_ISUPPER:
3260 case BUILT_IN_ISXDIGIT:
3261 case BUILT_IN_TOLOWER:
3262 case BUILT_IN_TOUPPER:
3263 return "<ctype.h>";
3264 case BUILT_IN_ISWALNUM:
3265 case BUILT_IN_ISWALPHA:
3266 case BUILT_IN_ISWBLANK:
3267 case BUILT_IN_ISWCNTRL:
3268 case BUILT_IN_ISWDIGIT:
3269 case BUILT_IN_ISWGRAPH:
3270 case BUILT_IN_ISWLOWER:
3271 case BUILT_IN_ISWPRINT:
3272 case BUILT_IN_ISWPUNCT:
3273 case BUILT_IN_ISWSPACE:
3274 case BUILT_IN_ISWUPPER:
3275 case BUILT_IN_ISWXDIGIT:
3276 case BUILT_IN_TOWLOWER:
3277 case BUILT_IN_TOWUPPER:
3278 return "<wctype.h>";
3279 case BUILT_IN_ABORT:
3280 case BUILT_IN_ABS:
3281 case BUILT_IN_CALLOC:
3282 case BUILT_IN_EXIT:
3283 case BUILT_IN_FREE:
3284 case BUILT_IN_LABS:
3285 case BUILT_IN_LLABS:
3286 case BUILT_IN_MALLOC:
3287 case BUILT_IN_REALLOC:
3288 case BUILT_IN__EXIT2:
3289 case BUILT_IN_ALIGNED_ALLOC:
3290 return "<stdlib.h>";
3291 case BUILT_IN_IMAXABS:
3292 return "<inttypes.h>";
3293 case BUILT_IN_STRFTIME:
3294 return "<time.h>";
3295 default:
3296 return NULL;
3300 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3301 function of type int (). */
3303 tree
3304 implicitly_declare (location_t loc, tree functionid)
3306 struct c_binding *b;
3307 tree decl = 0;
3308 tree asmspec_tree;
3310 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3312 if (B_IN_SCOPE (b, external_scope))
3314 decl = b->decl;
3315 break;
3319 if (decl)
3321 if (decl == error_mark_node)
3322 return decl;
3324 /* FIXME: Objective-C has weird not-really-builtin functions
3325 which are supposed to be visible automatically. They wind up
3326 in the external scope because they're pushed before the file
3327 scope gets created. Catch this here and rebind them into the
3328 file scope. */
3329 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3331 bind (functionid, decl, file_scope,
3332 /*invisible=*/false, /*nested=*/true,
3333 DECL_SOURCE_LOCATION (decl));
3334 return decl;
3336 else
3338 tree newtype = default_function_type;
3339 if (b->u.type)
3340 TREE_TYPE (decl) = b->u.type;
3341 /* Implicit declaration of a function already declared
3342 (somehow) in a different scope, or as a built-in.
3343 If this is the first time this has happened, warn;
3344 then recycle the old declaration but with the new type. */
3345 if (!C_DECL_IMPLICIT (decl))
3347 implicit_decl_warning (loc, functionid, decl);
3348 C_DECL_IMPLICIT (decl) = 1;
3350 if (DECL_BUILT_IN (decl))
3352 newtype = build_type_attribute_variant (newtype,
3353 TYPE_ATTRIBUTES
3354 (TREE_TYPE (decl)));
3355 if (!comptypes (newtype, TREE_TYPE (decl)))
3357 bool warned = warning_at (loc, 0, "incompatible implicit "
3358 "declaration of built-in "
3359 "function %qD", decl);
3360 /* See if we can hint which header to include. */
3361 const char *header
3362 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3363 if (header != NULL && warned)
3364 inform (loc, "include %qs or provide a declaration of %qD",
3365 header, decl);
3366 newtype = TREE_TYPE (decl);
3369 else
3371 if (!comptypes (newtype, TREE_TYPE (decl)))
3373 error_at (loc, "incompatible implicit declaration of "
3374 "function %qD", decl);
3375 locate_old_decl (decl);
3378 b->u.type = TREE_TYPE (decl);
3379 TREE_TYPE (decl) = newtype;
3380 bind (functionid, decl, current_scope,
3381 /*invisible=*/false, /*nested=*/true,
3382 DECL_SOURCE_LOCATION (decl));
3383 return decl;
3387 /* Not seen before. */
3388 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3389 DECL_EXTERNAL (decl) = 1;
3390 TREE_PUBLIC (decl) = 1;
3391 C_DECL_IMPLICIT (decl) = 1;
3392 implicit_decl_warning (loc, functionid, 0);
3393 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3394 if (asmspec_tree)
3395 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3397 /* C89 says implicit declarations are in the innermost block.
3398 So we record the decl in the standard fashion. */
3399 decl = pushdecl (decl);
3401 /* No need to call objc_check_decl here - it's a function type. */
3402 rest_of_decl_compilation (decl, 0, 0);
3404 /* Write a record describing this implicit function declaration
3405 to the prototypes file (if requested). */
3406 gen_aux_info_record (decl, 0, 1, 0);
3408 /* Possibly apply some default attributes to this implicit declaration. */
3409 decl_attributes (&decl, NULL_TREE, 0);
3411 return decl;
3414 /* Issue an error message for a reference to an undeclared variable
3415 ID, including a reference to a builtin outside of function-call
3416 context. Establish a binding of the identifier to error_mark_node
3417 in an appropriate scope, which will suppress further errors for the
3418 same identifier. The error message should be given location LOC. */
3419 void
3420 undeclared_variable (location_t loc, tree id)
3422 static bool already = false;
3423 struct c_scope *scope;
3425 if (current_function_decl == 0)
3427 error_at (loc, "%qE undeclared here (not in a function)", id);
3428 scope = current_scope;
3430 else
3432 if (!objc_diagnose_private_ivar (id))
3433 error_at (loc, "%qE undeclared (first use in this function)", id);
3434 if (!already)
3436 inform (loc, "each undeclared identifier is reported only"
3437 " once for each function it appears in");
3438 already = true;
3441 /* If we are parsing old-style parameter decls, current_function_decl
3442 will be nonnull but current_function_scope will be null. */
3443 scope = current_function_scope ? current_function_scope : current_scope;
3445 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3446 UNKNOWN_LOCATION);
3449 /* Subroutine of lookup_label, declare_label, define_label: construct a
3450 LABEL_DECL with all the proper frills. Also create a struct
3451 c_label_vars initialized for the current scope. */
3453 static tree
3454 make_label (location_t location, tree name, bool defining,
3455 struct c_label_vars **p_label_vars)
3457 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3458 DECL_CONTEXT (label) = current_function_decl;
3459 DECL_MODE (label) = VOIDmode;
3461 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3462 label_vars->shadowed = NULL;
3463 set_spot_bindings (&label_vars->label_bindings, defining);
3464 label_vars->decls_in_scope = make_tree_vector ();
3465 label_vars->gotos = NULL;
3466 *p_label_vars = label_vars;
3468 return label;
3471 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3472 Create one if none exists so far for the current function.
3473 This is called when a label is used in a goto expression or
3474 has its address taken. */
3476 tree
3477 lookup_label (tree name)
3479 tree label;
3480 struct c_label_vars *label_vars;
3482 if (current_function_scope == 0)
3484 error ("label %qE referenced outside of any function", name);
3485 return 0;
3488 /* Use a label already defined or ref'd with this name, but not if
3489 it is inherited from a containing function and wasn't declared
3490 using __label__. */
3491 label = I_LABEL_DECL (name);
3492 if (label && (DECL_CONTEXT (label) == current_function_decl
3493 || C_DECLARED_LABEL_FLAG (label)))
3495 /* If the label has only been declared, update its apparent
3496 location to point here, for better diagnostics if it
3497 turns out not to have been defined. */
3498 if (DECL_INITIAL (label) == NULL_TREE)
3499 DECL_SOURCE_LOCATION (label) = input_location;
3500 return label;
3503 /* No label binding for that identifier; make one. */
3504 label = make_label (input_location, name, false, &label_vars);
3506 /* Ordinary labels go in the current function scope. */
3507 bind_label (name, label, current_function_scope, label_vars);
3509 return label;
3512 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3513 to LABEL. */
3515 static void
3516 warn_about_goto (location_t goto_loc, tree label, tree decl)
3518 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3519 error_at (goto_loc,
3520 "jump into scope of identifier with variably modified type");
3521 else
3522 warning_at (goto_loc, OPT_Wjump_misses_init,
3523 "jump skips variable initialization");
3524 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3525 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3528 /* Look up a label because of a goto statement. This is like
3529 lookup_label, but also issues any appropriate warnings. */
3531 tree
3532 lookup_label_for_goto (location_t loc, tree name)
3534 tree label;
3535 struct c_label_vars *label_vars;
3536 unsigned int ix;
3537 tree decl;
3539 label = lookup_label (name);
3540 if (label == NULL_TREE)
3541 return NULL_TREE;
3543 /* If we are jumping to a different function, we can't issue any
3544 useful warnings. */
3545 if (DECL_CONTEXT (label) != current_function_decl)
3547 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3548 return label;
3551 label_vars = I_LABEL_BINDING (name)->u.label;
3553 /* If the label has not yet been defined, then push this goto on a
3554 list for possible later warnings. */
3555 if (label_vars->label_bindings.scope == NULL)
3557 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3559 g->loc = loc;
3560 set_spot_bindings (&g->goto_bindings, true);
3561 vec_safe_push (label_vars->gotos, g);
3562 return label;
3565 /* If there are any decls in label_vars->decls_in_scope, then this
3566 goto has missed the declaration of the decl. This happens for a
3567 case like
3568 int i = 1;
3569 lab:
3571 goto lab;
3572 Issue a warning or error. */
3573 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3574 warn_about_goto (loc, label, decl);
3576 if (label_vars->label_bindings.left_stmt_expr)
3578 error_at (loc, "jump into statement expression");
3579 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3582 return label;
3585 /* Make a label named NAME in the current function, shadowing silently
3586 any that may be inherited from containing functions or containing
3587 scopes. This is called for __label__ declarations. */
3589 tree
3590 declare_label (tree name)
3592 struct c_binding *b = I_LABEL_BINDING (name);
3593 tree label;
3594 struct c_label_vars *label_vars;
3596 /* Check to make sure that the label hasn't already been declared
3597 at this scope */
3598 if (b && B_IN_CURRENT_SCOPE (b))
3600 error ("duplicate label declaration %qE", name);
3601 locate_old_decl (b->decl);
3603 /* Just use the previous declaration. */
3604 return b->decl;
3607 label = make_label (input_location, name, false, &label_vars);
3608 C_DECLARED_LABEL_FLAG (label) = 1;
3610 /* Declared labels go in the current scope. */
3611 bind_label (name, label, current_scope, label_vars);
3613 return label;
3616 /* When we define a label, issue any appropriate warnings if there are
3617 any gotos earlier in the function which jump to this label. */
3619 static void
3620 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3622 unsigned int ix;
3623 struct c_goto_bindings *g;
3625 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3627 struct c_binding *b;
3628 struct c_scope *scope;
3630 /* We have a goto to this label. The goto is going forward. In
3631 g->scope, the goto is going to skip any binding which was
3632 defined after g->bindings_in_scope. */
3633 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3635 for (b = g->goto_bindings.scope->bindings;
3636 b != g->goto_bindings.bindings_in_scope;
3637 b = b->prev)
3639 if (decl_jump_unsafe (b->decl))
3640 warn_about_goto (g->loc, label, b->decl);
3644 /* We also need to warn about decls defined in any scopes
3645 between the scope of the label and the scope of the goto. */
3646 for (scope = label_vars->label_bindings.scope;
3647 scope != g->goto_bindings.scope;
3648 scope = scope->outer)
3650 gcc_assert (scope != NULL);
3651 if (scope->has_jump_unsafe_decl)
3653 if (scope == label_vars->label_bindings.scope)
3654 b = label_vars->label_bindings.bindings_in_scope;
3655 else
3656 b = scope->bindings;
3657 for (; b != NULL; b = b->prev)
3659 if (decl_jump_unsafe (b->decl))
3660 warn_about_goto (g->loc, label, b->decl);
3665 if (g->goto_bindings.stmt_exprs > 0)
3667 error_at (g->loc, "jump into statement expression");
3668 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3669 label);
3673 /* Now that the label is defined, we will issue warnings about
3674 subsequent gotos to this label when we see them. */
3675 vec_safe_truncate (label_vars->gotos, 0);
3676 label_vars->gotos = NULL;
3679 /* Define a label, specifying the location in the source file.
3680 Return the LABEL_DECL node for the label, if the definition is valid.
3681 Otherwise return 0. */
3683 tree
3684 define_label (location_t location, tree name)
3686 /* Find any preexisting label with this name. It is an error
3687 if that label has already been defined in this function, or
3688 if there is a containing function with a declared label with
3689 the same name. */
3690 tree label = I_LABEL_DECL (name);
3692 if (label
3693 && ((DECL_CONTEXT (label) == current_function_decl
3694 && DECL_INITIAL (label) != 0)
3695 || (DECL_CONTEXT (label) != current_function_decl
3696 && C_DECLARED_LABEL_FLAG (label))))
3698 error_at (location, "duplicate label %qD", label);
3699 locate_old_decl (label);
3700 return 0;
3702 else if (label && DECL_CONTEXT (label) == current_function_decl)
3704 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3706 /* The label has been used or declared already in this function,
3707 but not defined. Update its location to point to this
3708 definition. */
3709 DECL_SOURCE_LOCATION (label) = location;
3710 set_spot_bindings (&label_vars->label_bindings, true);
3712 /* Issue warnings as required about any goto statements from
3713 earlier in the function. */
3714 check_earlier_gotos (label, label_vars);
3716 else
3718 struct c_label_vars *label_vars;
3720 /* No label binding for that identifier; make one. */
3721 label = make_label (location, name, true, &label_vars);
3723 /* Ordinary labels go in the current function scope. */
3724 bind_label (name, label, current_function_scope, label_vars);
3727 if (!in_system_header_at (input_location) && lookup_name (name))
3728 warning_at (location, OPT_Wtraditional,
3729 "traditional C lacks a separate namespace "
3730 "for labels, identifier %qE conflicts", name);
3732 /* Mark label as having been defined. */
3733 DECL_INITIAL (label) = error_mark_node;
3734 return label;
3737 /* Get the bindings for a new switch statement. This is used to issue
3738 warnings as appropriate for jumps from the switch to case or
3739 default labels. */
3741 struct c_spot_bindings *
3742 c_get_switch_bindings (void)
3744 struct c_spot_bindings *switch_bindings;
3746 switch_bindings = XNEW (struct c_spot_bindings);
3747 set_spot_bindings (switch_bindings, true);
3748 return switch_bindings;
3751 void
3752 c_release_switch_bindings (struct c_spot_bindings *bindings)
3754 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3755 XDELETE (bindings);
3758 /* This is called at the point of a case or default label to issue
3759 warnings about decls as needed. It returns true if it found an
3760 error, not just a warning. */
3762 bool
3763 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3764 location_t switch_loc, location_t case_loc)
3766 bool saw_error;
3767 struct c_scope *scope;
3769 saw_error = false;
3770 for (scope = current_scope;
3771 scope != switch_bindings->scope;
3772 scope = scope->outer)
3774 struct c_binding *b;
3776 gcc_assert (scope != NULL);
3778 if (!scope->has_jump_unsafe_decl)
3779 continue;
3781 for (b = scope->bindings; b != NULL; b = b->prev)
3783 if (decl_jump_unsafe (b->decl))
3785 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3787 saw_error = true;
3788 error_at (case_loc,
3789 ("switch jumps into scope of identifier with "
3790 "variably modified type"));
3792 else
3793 warning_at (case_loc, OPT_Wjump_misses_init,
3794 "switch jumps over variable initialization");
3795 inform (switch_loc, "switch starts here");
3796 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3797 b->decl);
3802 if (switch_bindings->stmt_exprs > 0)
3804 saw_error = true;
3805 error_at (case_loc, "switch jumps into statement expression");
3806 inform (switch_loc, "switch starts here");
3809 return saw_error;
3812 /* Given NAME, an IDENTIFIER_NODE,
3813 return the structure (or union or enum) definition for that name.
3814 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3815 CODE says which kind of type the caller wants;
3816 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3817 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3818 location where the tag was defined.
3819 If the wrong kind of type is found, an error is reported. */
3821 static tree
3822 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3823 location_t *ploc)
3825 struct c_binding *b = I_TAG_BINDING (name);
3826 int thislevel = 0;
3828 if (!b || !b->decl)
3829 return 0;
3831 /* We only care about whether it's in this level if
3832 thislevel_only was set or it might be a type clash. */
3833 if (thislevel_only || TREE_CODE (b->decl) != code)
3835 /* For our purposes, a tag in the external scope is the same as
3836 a tag in the file scope. (Primarily relevant to Objective-C
3837 and its builtin structure tags, which get pushed before the
3838 file scope is created.) */
3839 if (B_IN_CURRENT_SCOPE (b)
3840 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3841 thislevel = 1;
3844 if (thislevel_only && !thislevel)
3845 return 0;
3847 if (TREE_CODE (b->decl) != code)
3849 /* Definition isn't the kind we were looking for. */
3850 pending_invalid_xref = name;
3851 pending_invalid_xref_location = input_location;
3853 /* If in the same binding level as a declaration as a tag
3854 of a different type, this must not be allowed to
3855 shadow that tag, so give the error immediately.
3856 (For example, "struct foo; union foo;" is invalid.) */
3857 if (thislevel)
3858 pending_xref_error ();
3861 if (ploc != NULL)
3862 *ploc = b->locus;
3864 return b->decl;
3867 /* Print an error message now
3868 for a recent invalid struct, union or enum cross reference.
3869 We don't print them immediately because they are not invalid
3870 when used in the `struct foo;' construct for shadowing. */
3872 void
3873 pending_xref_error (void)
3875 if (pending_invalid_xref != 0)
3876 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3877 pending_invalid_xref);
3878 pending_invalid_xref = 0;
3882 /* Look up NAME in the current scope and its superiors
3883 in the namespace of variables, functions and typedefs.
3884 Return a ..._DECL node of some kind representing its definition,
3885 or return 0 if it is undefined. */
3887 tree
3888 lookup_name (tree name)
3890 struct c_binding *b = I_SYMBOL_BINDING (name);
3891 if (b && !b->invisible)
3893 maybe_record_typedef_use (b->decl);
3894 return b->decl;
3896 return 0;
3899 /* Similar to `lookup_name' but look only at the indicated scope. */
3901 static tree
3902 lookup_name_in_scope (tree name, struct c_scope *scope)
3904 struct c_binding *b;
3906 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3907 if (B_IN_SCOPE (b, scope))
3908 return b->decl;
3909 return 0;
3912 /* Create the predefined scalar types of C,
3913 and some nodes representing standard constants (0, 1, (void *) 0).
3914 Initialize the global scope.
3915 Make definitions for built-in primitive functions. */
3917 void
3918 c_init_decl_processing (void)
3920 location_t save_loc = input_location;
3922 /* Initialize reserved words for parser. */
3923 c_parse_init ();
3925 current_function_decl = 0;
3927 gcc_obstack_init (&parser_obstack);
3929 /* Make the externals scope. */
3930 push_scope ();
3931 external_scope = current_scope;
3933 /* Declarations from c_common_nodes_and_builtins must not be associated
3934 with this input file, lest we get differences between using and not
3935 using preprocessed headers. */
3936 input_location = BUILTINS_LOCATION;
3938 c_common_nodes_and_builtins ();
3940 /* In C, comparisons and TRUTH_* expressions have type int. */
3941 truthvalue_type_node = integer_type_node;
3942 truthvalue_true_node = integer_one_node;
3943 truthvalue_false_node = integer_zero_node;
3945 /* Even in C99, which has a real boolean type. */
3946 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3947 boolean_type_node));
3949 input_location = save_loc;
3951 make_fname_decl = c_make_fname_decl;
3952 start_fname_decls ();
3955 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3956 give the decl, NAME is the initialization string and TYPE_DEP
3957 indicates whether NAME depended on the type of the function. As we
3958 don't yet implement delayed emission of static data, we mark the
3959 decl as emitted so it is not placed in the output. Anything using
3960 it must therefore pull out the STRING_CST initializer directly.
3961 FIXME. */
3963 static tree
3964 c_make_fname_decl (location_t loc, tree id, int type_dep)
3966 const char *name = fname_as_string (type_dep);
3967 tree decl, type, init;
3968 size_t length = strlen (name);
3970 type = build_array_type (char_type_node,
3971 build_index_type (size_int (length)));
3972 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3974 decl = build_decl (loc, VAR_DECL, id, type);
3976 TREE_STATIC (decl) = 1;
3977 TREE_READONLY (decl) = 1;
3978 DECL_ARTIFICIAL (decl) = 1;
3980 init = build_string (length + 1, name);
3981 free (CONST_CAST (char *, name));
3982 TREE_TYPE (init) = type;
3983 DECL_INITIAL (decl) = init;
3985 TREE_USED (decl) = 1;
3987 if (current_function_decl
3988 /* For invalid programs like this:
3990 void foo()
3991 const char* p = __FUNCTION__;
3993 the __FUNCTION__ is believed to appear in K&R style function
3994 parameter declarator. In that case we still don't have
3995 function_scope. */
3996 && (!seen_error () || current_function_scope))
3998 DECL_CONTEXT (decl) = current_function_decl;
3999 bind (id, decl, current_function_scope,
4000 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4003 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4005 return decl;
4008 tree
4009 c_builtin_function (tree decl)
4011 tree type = TREE_TYPE (decl);
4012 tree id = DECL_NAME (decl);
4014 const char *name = IDENTIFIER_POINTER (id);
4015 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4017 /* Should never be called on a symbol with a preexisting meaning. */
4018 gcc_assert (!I_SYMBOL_BINDING (id));
4020 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4021 UNKNOWN_LOCATION);
4023 /* Builtins in the implementation namespace are made visible without
4024 needing to be explicitly declared. See push_file_scope. */
4025 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4027 DECL_CHAIN (decl) = visible_builtins;
4028 visible_builtins = decl;
4031 return decl;
4034 tree
4035 c_builtin_function_ext_scope (tree decl)
4037 tree type = TREE_TYPE (decl);
4038 tree id = DECL_NAME (decl);
4040 const char *name = IDENTIFIER_POINTER (id);
4041 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4043 if (external_scope)
4044 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4045 UNKNOWN_LOCATION);
4047 /* Builtins in the implementation namespace are made visible without
4048 needing to be explicitly declared. See push_file_scope. */
4049 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4051 DECL_CHAIN (decl) = visible_builtins;
4052 visible_builtins = decl;
4055 return decl;
4058 /* Called when a declaration is seen that contains no names to declare.
4059 If its type is a reference to a structure, union or enum inherited
4060 from a containing scope, shadow that tag name for the current scope
4061 with a forward reference.
4062 If its type defines a new named structure or union
4063 or defines an enum, it is valid but we need not do anything here.
4064 Otherwise, it is an error. */
4066 void
4067 shadow_tag (const struct c_declspecs *declspecs)
4069 shadow_tag_warned (declspecs, 0);
4072 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4073 but no pedwarn. */
4074 void
4075 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4077 bool found_tag = false;
4079 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4081 tree value = declspecs->type;
4082 enum tree_code code = TREE_CODE (value);
4084 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4085 /* Used to test also that TYPE_SIZE (value) != 0.
4086 That caused warning for `struct foo;' at top level in the file. */
4088 tree name = TYPE_NAME (value);
4089 tree t;
4091 found_tag = true;
4093 if (declspecs->restrict_p)
4095 error ("invalid use of %<restrict%>");
4096 warned = 1;
4099 if (name == 0)
4101 if (warned != 1 && code != ENUMERAL_TYPE)
4102 /* Empty unnamed enum OK */
4104 pedwarn (input_location, 0,
4105 "unnamed struct/union that defines no instances");
4106 warned = 1;
4109 else if (declspecs->typespec_kind != ctsk_tagdef
4110 && declspecs->typespec_kind != ctsk_tagfirstref
4111 && declspecs->storage_class != csc_none)
4113 if (warned != 1)
4114 pedwarn (input_location, 0,
4115 "empty declaration with storage class specifier "
4116 "does not redeclare tag");
4117 warned = 1;
4118 pending_xref_error ();
4120 else if (declspecs->typespec_kind != ctsk_tagdef
4121 && declspecs->typespec_kind != ctsk_tagfirstref
4122 && (declspecs->const_p
4123 || declspecs->volatile_p
4124 || declspecs->atomic_p
4125 || declspecs->restrict_p
4126 || declspecs->address_space))
4128 if (warned != 1)
4129 pedwarn (input_location, 0,
4130 "empty declaration with type qualifier "
4131 "does not redeclare tag");
4132 warned = 1;
4133 pending_xref_error ();
4135 else if (declspecs->typespec_kind != ctsk_tagdef
4136 && declspecs->typespec_kind != ctsk_tagfirstref
4137 && declspecs->alignas_p)
4139 if (warned != 1)
4140 pedwarn (input_location, 0,
4141 "empty declaration with %<_Alignas%> "
4142 "does not redeclare tag");
4143 warned = 1;
4144 pending_xref_error ();
4146 else
4148 pending_invalid_xref = 0;
4149 t = lookup_tag (code, name, 1, NULL);
4151 if (t == 0)
4153 t = make_node (code);
4154 pushtag (input_location, name, t);
4158 else
4160 if (warned != 1 && !in_system_header_at (input_location))
4162 pedwarn (input_location, 0,
4163 "useless type name in empty declaration");
4164 warned = 1;
4168 else if (warned != 1 && !in_system_header_at (input_location)
4169 && declspecs->typedef_p)
4171 pedwarn (input_location, 0, "useless type name in empty declaration");
4172 warned = 1;
4175 pending_invalid_xref = 0;
4177 if (declspecs->inline_p)
4179 error ("%<inline%> in empty declaration");
4180 warned = 1;
4183 if (declspecs->noreturn_p)
4185 error ("%<_Noreturn%> in empty declaration");
4186 warned = 1;
4189 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4191 error ("%<auto%> in file-scope empty declaration");
4192 warned = 1;
4195 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4197 error ("%<register%> in file-scope empty declaration");
4198 warned = 1;
4201 if (!warned && !in_system_header_at (input_location)
4202 && declspecs->storage_class != csc_none)
4204 warning (0, "useless storage class specifier in empty declaration");
4205 warned = 2;
4208 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4210 warning (0, "useless %qs in empty declaration",
4211 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4212 warned = 2;
4215 if (!warned
4216 && !in_system_header_at (input_location)
4217 && (declspecs->const_p
4218 || declspecs->volatile_p
4219 || declspecs->atomic_p
4220 || declspecs->restrict_p
4221 || declspecs->address_space))
4223 warning (0, "useless type qualifier in empty declaration");
4224 warned = 2;
4227 if (!warned && !in_system_header_at (input_location)
4228 && declspecs->alignas_p)
4230 warning (0, "useless %<_Alignas%> in empty declaration");
4231 warned = 2;
4234 if (warned != 1)
4236 if (!found_tag)
4237 pedwarn (input_location, 0, "empty declaration");
4242 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4243 bits. SPECS represents declaration specifiers that the grammar
4244 only permits to contain type qualifiers and attributes. */
4247 quals_from_declspecs (const struct c_declspecs *specs)
4249 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4250 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4251 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4252 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4253 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4254 gcc_assert (!specs->type
4255 && !specs->decl_attr
4256 && specs->typespec_word == cts_none
4257 && specs->storage_class == csc_none
4258 && !specs->typedef_p
4259 && !specs->explicit_signed_p
4260 && !specs->deprecated_p
4261 && !specs->long_p
4262 && !specs->long_long_p
4263 && !specs->short_p
4264 && !specs->signed_p
4265 && !specs->unsigned_p
4266 && !specs->complex_p
4267 && !specs->inline_p
4268 && !specs->noreturn_p
4269 && !specs->thread_p);
4270 return quals;
4273 /* Construct an array declarator. LOC is the location of the
4274 beginning of the array (usually the opening brace). EXPR is the
4275 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4276 inside the [] (to be applied to the pointer to which a parameter
4277 array is converted). STATIC_P is true if "static" is inside the
4278 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4279 VLA of unspecified length which is nevertheless a complete type,
4280 false otherwise. The field for the contained declarator is left to
4281 be filled in by set_array_declarator_inner. */
4283 struct c_declarator *
4284 build_array_declarator (location_t loc,
4285 tree expr, struct c_declspecs *quals, bool static_p,
4286 bool vla_unspec_p)
4288 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4289 struct c_declarator);
4290 declarator->id_loc = loc;
4291 declarator->kind = cdk_array;
4292 declarator->declarator = 0;
4293 declarator->u.array.dimen = expr;
4294 if (quals)
4296 declarator->u.array.attrs = quals->attrs;
4297 declarator->u.array.quals = quals_from_declspecs (quals);
4299 else
4301 declarator->u.array.attrs = NULL_TREE;
4302 declarator->u.array.quals = 0;
4304 declarator->u.array.static_p = static_p;
4305 declarator->u.array.vla_unspec_p = vla_unspec_p;
4306 if (static_p || quals != NULL)
4307 pedwarn_c90 (loc, OPT_Wpedantic,
4308 "ISO C90 does not support %<static%> or type "
4309 "qualifiers in parameter array declarators");
4310 if (vla_unspec_p)
4311 pedwarn_c90 (loc, OPT_Wpedantic,
4312 "ISO C90 does not support %<[*]%> array declarators");
4313 if (vla_unspec_p)
4315 if (!current_scope->parm_flag)
4317 /* C99 6.7.5.2p4 */
4318 error_at (loc, "%<[*]%> not allowed in other than "
4319 "function prototype scope");
4320 declarator->u.array.vla_unspec_p = false;
4321 return NULL;
4323 current_scope->had_vla_unspec = true;
4325 return declarator;
4328 /* Set the contained declarator of an array declarator. DECL is the
4329 declarator, as constructed by build_array_declarator; INNER is what
4330 appears on the left of the []. */
4332 struct c_declarator *
4333 set_array_declarator_inner (struct c_declarator *decl,
4334 struct c_declarator *inner)
4336 decl->declarator = inner;
4337 return decl;
4340 /* INIT is a constructor that forms DECL's initializer. If the final
4341 element initializes a flexible array field, add the size of that
4342 initializer to DECL's size. */
4344 static void
4345 add_flexible_array_elts_to_size (tree decl, tree init)
4347 tree elt, type;
4349 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4350 return;
4352 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4353 type = TREE_TYPE (elt);
4354 if (TREE_CODE (type) == ARRAY_TYPE
4355 && TYPE_SIZE (type) == NULL_TREE
4356 && TYPE_DOMAIN (type) != NULL_TREE
4357 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4359 complete_array_type (&type, elt, false);
4360 DECL_SIZE (decl)
4361 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4362 DECL_SIZE_UNIT (decl)
4363 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4367 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4368 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4369 before the type name, and set *EXPR_CONST_OPERANDS, if
4370 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4371 appear in a constant expression. */
4373 tree
4374 groktypename (struct c_type_name *type_name, tree *expr,
4375 bool *expr_const_operands)
4377 tree type;
4378 tree attrs = type_name->specs->attrs;
4380 type_name->specs->attrs = NULL_TREE;
4382 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4383 false, NULL, &attrs, expr, expr_const_operands,
4384 DEPRECATED_NORMAL);
4386 /* Apply attributes. */
4387 decl_attributes (&type, attrs, 0);
4389 return type;
4392 /* Wrapper for decl_attributes that adds some implicit attributes
4393 to VAR_DECLs or FUNCTION_DECLs. */
4395 static tree
4396 c_decl_attributes (tree *node, tree attributes, int flags)
4398 /* Add implicit "omp declare target" attribute if requested. */
4399 if (current_omp_declare_target_attribute
4400 && ((TREE_CODE (*node) == VAR_DECL
4401 && (TREE_STATIC (*node) || DECL_EXTERNAL (*node)))
4402 || TREE_CODE (*node) == FUNCTION_DECL))
4404 if (TREE_CODE (*node) == VAR_DECL
4405 && ((DECL_CONTEXT (*node)
4406 && TREE_CODE (DECL_CONTEXT (*node)) == FUNCTION_DECL)
4407 || (current_function_decl && !DECL_EXTERNAL (*node))))
4408 error ("%q+D in block scope inside of declare target directive",
4409 *node);
4410 else if (TREE_CODE (*node) == VAR_DECL
4411 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4412 error ("%q+D in declare target directive does not have mappable type",
4413 *node);
4414 else
4415 attributes = tree_cons (get_identifier ("omp declare target"),
4416 NULL_TREE, attributes);
4418 return decl_attributes (node, attributes, flags);
4422 /* Decode a declarator in an ordinary declaration or data definition.
4423 This is called as soon as the type information and variable name
4424 have been parsed, before parsing the initializer if any.
4425 Here we create the ..._DECL node, fill in its type,
4426 and put it on the list of decls for the current context.
4427 The ..._DECL node is returned as the value.
4429 Exception: for arrays where the length is not specified,
4430 the type is left null, to be filled in by `finish_decl'.
4432 Function definitions do not come here; they go to start_function
4433 instead. However, external and forward declarations of functions
4434 do go through here. Structure field declarations are done by
4435 grokfield and not through here. */
4437 tree
4438 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4439 bool initialized, tree attributes)
4441 tree decl;
4442 tree tem;
4443 tree expr = NULL_TREE;
4444 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4446 /* An object declared as __attribute__((deprecated)) suppresses
4447 warnings of uses of other deprecated items. */
4448 if (lookup_attribute ("deprecated", attributes))
4449 deprecated_state = DEPRECATED_SUPPRESS;
4451 decl = grokdeclarator (declarator, declspecs,
4452 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4453 deprecated_state);
4454 if (!decl || decl == error_mark_node)
4455 return NULL_TREE;
4457 if (expr)
4458 add_stmt (fold_convert (void_type_node, expr));
4460 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4461 warning (OPT_Wmain, "%q+D is usually a function", decl);
4463 if (initialized)
4464 /* Is it valid for this decl to have an initializer at all?
4465 If not, set INITIALIZED to zero, which will indirectly
4466 tell 'finish_decl' to ignore the initializer once it is parsed. */
4467 switch (TREE_CODE (decl))
4469 case TYPE_DECL:
4470 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4471 initialized = 0;
4472 break;
4474 case FUNCTION_DECL:
4475 error ("function %qD is initialized like a variable", decl);
4476 initialized = 0;
4477 break;
4479 case PARM_DECL:
4480 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4481 error ("parameter %qD is initialized", decl);
4482 initialized = 0;
4483 break;
4485 default:
4486 /* Don't allow initializations for incomplete types except for
4487 arrays which might be completed by the initialization. */
4489 /* This can happen if the array size is an undefined macro.
4490 We already gave a warning, so we don't need another one. */
4491 if (TREE_TYPE (decl) == error_mark_node)
4492 initialized = 0;
4493 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4495 /* A complete type is ok if size is fixed. */
4497 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4498 || C_DECL_VARIABLE_SIZE (decl))
4500 error ("variable-sized object may not be initialized");
4501 initialized = 0;
4504 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4506 error ("variable %qD has initializer but incomplete type", decl);
4507 initialized = 0;
4509 else if (C_DECL_VARIABLE_SIZE (decl))
4511 /* Although C99 is unclear about whether incomplete arrays
4512 of VLAs themselves count as VLAs, it does not make
4513 sense to permit them to be initialized given that
4514 ordinary VLAs may not be initialized. */
4515 error ("variable-sized object may not be initialized");
4516 initialized = 0;
4520 if (initialized)
4522 if (current_scope == file_scope)
4523 TREE_STATIC (decl) = 1;
4525 /* Tell 'pushdecl' this is an initialized decl
4526 even though we don't yet have the initializer expression.
4527 Also tell 'finish_decl' it may store the real initializer. */
4528 DECL_INITIAL (decl) = error_mark_node;
4531 /* If this is a function declaration, write a record describing it to the
4532 prototypes file (if requested). */
4534 if (TREE_CODE (decl) == FUNCTION_DECL)
4535 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4537 /* ANSI specifies that a tentative definition which is not merged with
4538 a non-tentative definition behaves exactly like a definition with an
4539 initializer equal to zero. (Section 3.7.2)
4541 -fno-common gives strict ANSI behavior, though this tends to break
4542 a large body of code that grew up without this rule.
4544 Thread-local variables are never common, since there's no entrenched
4545 body of code to break, and it allows more efficient variable references
4546 in the presence of dynamic linking. */
4548 if (TREE_CODE (decl) == VAR_DECL
4549 && !initialized
4550 && TREE_PUBLIC (decl)
4551 && !DECL_THREAD_LOCAL_P (decl)
4552 && !flag_no_common)
4553 DECL_COMMON (decl) = 1;
4555 /* Set attributes here so if duplicate decl, will have proper attributes. */
4556 c_decl_attributes (&decl, attributes, 0);
4558 /* Handle gnu_inline attribute. */
4559 if (declspecs->inline_p
4560 && !flag_gnu89_inline
4561 && TREE_CODE (decl) == FUNCTION_DECL
4562 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4563 || current_function_decl))
4565 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4567 else if (declspecs->storage_class != csc_static)
4568 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4571 if (TREE_CODE (decl) == FUNCTION_DECL
4572 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4574 struct c_declarator *ce = declarator;
4576 if (ce->kind == cdk_pointer)
4577 ce = declarator->declarator;
4578 if (ce->kind == cdk_function)
4580 tree args = ce->u.arg_info->parms;
4581 for (; args; args = DECL_CHAIN (args))
4583 tree type = TREE_TYPE (args);
4584 if (type && INTEGRAL_TYPE_P (type)
4585 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4586 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4591 if (TREE_CODE (decl) == FUNCTION_DECL
4592 && DECL_DECLARED_INLINE_P (decl)
4593 && DECL_UNINLINABLE (decl)
4594 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4595 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4596 decl);
4598 /* C99 6.7.4p3: An inline definition of a function with external
4599 linkage shall not contain a definition of a modifiable object
4600 with static storage duration... */
4601 if (TREE_CODE (decl) == VAR_DECL
4602 && current_scope != file_scope
4603 && TREE_STATIC (decl)
4604 && !TREE_READONLY (decl)
4605 && DECL_DECLARED_INLINE_P (current_function_decl)
4606 && DECL_EXTERNAL (current_function_decl))
4607 record_inline_static (input_location, current_function_decl,
4608 decl, csi_modifiable);
4610 if (c_dialect_objc ()
4611 && VAR_OR_FUNCTION_DECL_P (decl))
4612 objc_check_global_decl (decl);
4614 /* Add this decl to the current scope.
4615 TEM may equal DECL or it may be a previous decl of the same name. */
4616 tem = pushdecl (decl);
4618 if (initialized && DECL_EXTERNAL (tem))
4620 DECL_EXTERNAL (tem) = 0;
4621 TREE_STATIC (tem) = 1;
4624 return tem;
4627 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4628 DECL or the non-array element type if DECL is an uninitialized array.
4629 If that type has a const member, diagnose this. */
4631 static void
4632 diagnose_uninitialized_cst_member (tree decl, tree type)
4634 tree field;
4635 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4637 tree field_type;
4638 if (TREE_CODE (field) != FIELD_DECL)
4639 continue;
4640 field_type = strip_array_types (TREE_TYPE (field));
4642 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4644 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4645 "uninitialized const member in %qT is invalid in C++",
4646 strip_array_types (TREE_TYPE (decl)));
4647 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4650 if (TREE_CODE (field_type) == RECORD_TYPE
4651 || TREE_CODE (field_type) == UNION_TYPE)
4652 diagnose_uninitialized_cst_member (decl, field_type);
4656 /* Finish processing of a declaration;
4657 install its initial value.
4658 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4659 If the length of an array type is not known before,
4660 it must be determined now, from the initial value, or it is an error.
4662 INIT_LOC is the location of the initial value. */
4664 void
4665 finish_decl (tree decl, location_t init_loc, tree init,
4666 tree origtype, tree asmspec_tree)
4668 tree type;
4669 bool was_incomplete = (DECL_SIZE (decl) == 0);
4670 const char *asmspec = 0;
4672 /* If a name was specified, get the string. */
4673 if (VAR_OR_FUNCTION_DECL_P (decl)
4674 && DECL_FILE_SCOPE_P (decl))
4675 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4676 if (asmspec_tree)
4677 asmspec = TREE_STRING_POINTER (asmspec_tree);
4679 if (TREE_CODE (decl) == VAR_DECL
4680 && TREE_STATIC (decl)
4681 && global_bindings_p ())
4682 /* So decl is a global variable. Record the types it uses
4683 so that we can decide later to emit debug info for them. */
4684 record_types_used_by_current_var_decl (decl);
4686 /* If `start_decl' didn't like having an initialization, ignore it now. */
4687 if (init != 0 && DECL_INITIAL (decl) == 0)
4688 init = 0;
4690 /* Don't crash if parm is initialized. */
4691 if (TREE_CODE (decl) == PARM_DECL)
4692 init = 0;
4694 if (init)
4695 store_init_value (init_loc, decl, init, origtype);
4697 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4698 || TREE_CODE (decl) == FIELD_DECL))
4699 objc_check_decl (decl);
4701 type = TREE_TYPE (decl);
4703 /* Deduce size of array from initialization, if not already known. */
4704 if (TREE_CODE (type) == ARRAY_TYPE
4705 && TYPE_DOMAIN (type) == 0
4706 && TREE_CODE (decl) != TYPE_DECL)
4708 bool do_default
4709 = (TREE_STATIC (decl)
4710 /* Even if pedantic, an external linkage array
4711 may have incomplete type at first. */
4712 ? pedantic && !TREE_PUBLIC (decl)
4713 : !DECL_EXTERNAL (decl));
4714 int failure
4715 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4716 do_default);
4718 /* Get the completed type made by complete_array_type. */
4719 type = TREE_TYPE (decl);
4721 switch (failure)
4723 case 1:
4724 error ("initializer fails to determine size of %q+D", decl);
4725 break;
4727 case 2:
4728 if (do_default)
4729 error ("array size missing in %q+D", decl);
4730 /* If a `static' var's size isn't known,
4731 make it extern as well as static, so it does not get
4732 allocated.
4733 If it is not `static', then do not mark extern;
4734 finish_incomplete_decl will give it a default size
4735 and it will get allocated. */
4736 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4737 DECL_EXTERNAL (decl) = 1;
4738 break;
4740 case 3:
4741 error ("zero or negative size array %q+D", decl);
4742 break;
4744 case 0:
4745 /* For global variables, update the copy of the type that
4746 exists in the binding. */
4747 if (TREE_PUBLIC (decl))
4749 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4750 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4751 b_ext = b_ext->shadowed;
4752 if (b_ext)
4754 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4755 b_ext->u.type = composite_type (b_ext->u.type, type);
4756 else
4757 b_ext->u.type = type;
4760 break;
4762 default:
4763 gcc_unreachable ();
4766 if (DECL_INITIAL (decl))
4767 TREE_TYPE (DECL_INITIAL (decl)) = type;
4769 relayout_decl (decl);
4772 if (TREE_CODE (decl) == VAR_DECL)
4774 if (init && TREE_CODE (init) == CONSTRUCTOR)
4775 add_flexible_array_elts_to_size (decl, init);
4777 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4778 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4779 layout_decl (decl, 0);
4781 if (DECL_SIZE (decl) == 0
4782 /* Don't give an error if we already gave one earlier. */
4783 && TREE_TYPE (decl) != error_mark_node
4784 && (TREE_STATIC (decl)
4785 /* A static variable with an incomplete type
4786 is an error if it is initialized.
4787 Also if it is not file scope.
4788 Otherwise, let it through, but if it is not `extern'
4789 then it may cause an error message later. */
4790 ? (DECL_INITIAL (decl) != 0
4791 || !DECL_FILE_SCOPE_P (decl))
4792 /* An automatic variable with an incomplete type
4793 is an error. */
4794 : !DECL_EXTERNAL (decl)))
4796 error ("storage size of %q+D isn%'t known", decl);
4797 TREE_TYPE (decl) = error_mark_node;
4800 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4801 && DECL_SIZE (decl) != 0)
4803 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4804 constant_expression_warning (DECL_SIZE (decl));
4805 else
4807 error ("storage size of %q+D isn%'t constant", decl);
4808 TREE_TYPE (decl) = error_mark_node;
4812 if (TREE_USED (type))
4814 TREE_USED (decl) = 1;
4815 DECL_READ_P (decl) = 1;
4819 /* If this is a function and an assembler name is specified, reset DECL_RTL
4820 so we can give it its new name. Also, update builtin_decl if it
4821 was a normal built-in. */
4822 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4824 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4825 set_builtin_user_assembler_name (decl, asmspec);
4826 set_user_assembler_name (decl, asmspec);
4829 /* If #pragma weak was used, mark the decl weak now. */
4830 maybe_apply_pragma_weak (decl);
4832 /* Output the assembler code and/or RTL code for variables and functions,
4833 unless the type is an undefined structure or union.
4834 If not, it will get done when the type is completed. */
4836 if (VAR_OR_FUNCTION_DECL_P (decl))
4838 /* Determine the ELF visibility. */
4839 if (TREE_PUBLIC (decl))
4840 c_determine_visibility (decl);
4842 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4843 if (c_dialect_objc ())
4844 objc_check_decl (decl);
4846 if (asmspec)
4848 /* If this is not a static variable, issue a warning.
4849 It doesn't make any sense to give an ASMSPEC for an
4850 ordinary, non-register local variable. Historically,
4851 GCC has accepted -- but ignored -- the ASMSPEC in
4852 this case. */
4853 if (!DECL_FILE_SCOPE_P (decl)
4854 && TREE_CODE (decl) == VAR_DECL
4855 && !C_DECL_REGISTER (decl)
4856 && !TREE_STATIC (decl))
4857 warning (0, "ignoring asm-specifier for non-static local "
4858 "variable %q+D", decl);
4859 else
4860 set_user_assembler_name (decl, asmspec);
4863 if (DECL_FILE_SCOPE_P (decl))
4865 if (DECL_INITIAL (decl) == NULL_TREE
4866 || DECL_INITIAL (decl) == error_mark_node)
4867 /* Don't output anything
4868 when a tentative file-scope definition is seen.
4869 But at end of compilation, do output code for them. */
4870 DECL_DEFER_OUTPUT (decl) = 1;
4871 if (asmspec && C_DECL_REGISTER (decl))
4872 DECL_HARD_REGISTER (decl) = 1;
4873 rest_of_decl_compilation (decl, true, 0);
4875 else
4877 /* In conjunction with an ASMSPEC, the `register'
4878 keyword indicates that we should place the variable
4879 in a particular register. */
4880 if (asmspec && C_DECL_REGISTER (decl))
4882 DECL_HARD_REGISTER (decl) = 1;
4883 /* This cannot be done for a structure with volatile
4884 fields, on which DECL_REGISTER will have been
4885 reset. */
4886 if (!DECL_REGISTER (decl))
4887 error ("cannot put object with volatile field into register");
4890 if (TREE_CODE (decl) != FUNCTION_DECL)
4892 /* If we're building a variable sized type, and we might be
4893 reachable other than via the top of the current binding
4894 level, then create a new BIND_EXPR so that we deallocate
4895 the object at the right time. */
4896 /* Note that DECL_SIZE can be null due to errors. */
4897 if (DECL_SIZE (decl)
4898 && !TREE_CONSTANT (DECL_SIZE (decl))
4899 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4901 tree bind;
4902 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4903 TREE_SIDE_EFFECTS (bind) = 1;
4904 add_stmt (bind);
4905 BIND_EXPR_BODY (bind) = push_stmt_list ();
4907 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4908 DECL_EXPR, decl));
4913 if (!DECL_FILE_SCOPE_P (decl))
4915 /* Recompute the RTL of a local array now
4916 if it used to be an incomplete type. */
4917 if (was_incomplete
4918 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4920 /* If we used it already as memory, it must stay in memory. */
4921 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4922 /* If it's still incomplete now, no init will save it. */
4923 if (DECL_SIZE (decl) == 0)
4924 DECL_INITIAL (decl) = 0;
4929 if (TREE_CODE (decl) == TYPE_DECL)
4931 if (!DECL_FILE_SCOPE_P (decl)
4932 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4933 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4935 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4938 /* Install a cleanup (aka destructor) if one was given. */
4939 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4941 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4942 if (attr)
4944 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4945 tree cleanup_decl = lookup_name (cleanup_id);
4946 tree cleanup;
4947 vec<tree, va_gc> *v;
4949 /* Build "cleanup(&decl)" for the destructor. */
4950 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4951 vec_alloc (v, 1);
4952 v->quick_push (cleanup);
4953 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4954 vNULL, cleanup_decl, v, NULL);
4955 vec_free (v);
4957 /* Don't warn about decl unused; the cleanup uses it. */
4958 TREE_USED (decl) = 1;
4959 TREE_USED (cleanup_decl) = 1;
4960 DECL_READ_P (decl) = 1;
4962 push_cleanup (decl, cleanup, false);
4966 if (warn_cxx_compat
4967 && TREE_CODE (decl) == VAR_DECL
4968 && !DECL_EXTERNAL (decl)
4969 && DECL_INITIAL (decl) == NULL_TREE)
4971 type = strip_array_types (type);
4972 if (TREE_READONLY (decl))
4973 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4974 "uninitialized const %qD is invalid in C++", decl);
4975 else if ((TREE_CODE (type) == RECORD_TYPE
4976 || TREE_CODE (type) == UNION_TYPE)
4977 && C_TYPE_FIELDS_READONLY (type))
4978 diagnose_uninitialized_cst_member (decl, type);
4981 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4984 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4985 EXPR is NULL or a pointer to an expression that needs to be
4986 evaluated for the side effects of array size expressions in the
4987 parameters. */
4989 tree
4990 grokparm (const struct c_parm *parm, tree *expr)
4992 tree attrs = parm->attrs;
4993 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4994 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4996 decl_attributes (&decl, attrs, 0);
4998 return decl;
5001 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5002 and push that on the current scope. EXPR is a pointer to an
5003 expression that needs to be evaluated for the side effects of array
5004 size expressions in the parameters. */
5006 void
5007 push_parm_decl (const struct c_parm *parm, tree *expr)
5009 tree attrs = parm->attrs;
5010 tree decl;
5012 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5013 &attrs, expr, NULL, DEPRECATED_NORMAL);
5014 decl_attributes (&decl, attrs, 0);
5016 decl = pushdecl (decl);
5018 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5021 /* Mark all the parameter declarations to date as forward decls.
5022 Also diagnose use of this extension. */
5024 void
5025 mark_forward_parm_decls (void)
5027 struct c_binding *b;
5029 if (pedantic && !current_scope->warned_forward_parm_decls)
5031 pedwarn (input_location, OPT_Wpedantic,
5032 "ISO C forbids forward parameter declarations");
5033 current_scope->warned_forward_parm_decls = true;
5036 for (b = current_scope->bindings; b; b = b->prev)
5037 if (TREE_CODE (b->decl) == PARM_DECL)
5038 TREE_ASM_WRITTEN (b->decl) = 1;
5041 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5042 literal, which may be an incomplete array type completed by the
5043 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5044 literal. NON_CONST is true if the initializers contain something
5045 that cannot occur in a constant expression. */
5047 tree
5048 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5050 /* We do not use start_decl here because we have a type, not a declarator;
5051 and do not use finish_decl because the decl should be stored inside
5052 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5053 tree decl;
5054 tree complit;
5055 tree stmt;
5057 if (type == error_mark_node
5058 || init == error_mark_node)
5059 return error_mark_node;
5061 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5062 DECL_EXTERNAL (decl) = 0;
5063 TREE_PUBLIC (decl) = 0;
5064 TREE_STATIC (decl) = (current_scope == file_scope);
5065 DECL_CONTEXT (decl) = current_function_decl;
5066 TREE_USED (decl) = 1;
5067 DECL_READ_P (decl) = 1;
5068 TREE_TYPE (decl) = type;
5069 TREE_READONLY (decl) = (TYPE_READONLY (type)
5070 || (TREE_CODE (type) == ARRAY_TYPE
5071 && TYPE_READONLY (TREE_TYPE (type))));
5072 store_init_value (loc, decl, init, NULL_TREE);
5074 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5076 int failure = complete_array_type (&TREE_TYPE (decl),
5077 DECL_INITIAL (decl), true);
5078 /* If complete_array_type returns 3, it means that the
5079 initial value of the compound literal is empty. Allow it. */
5080 gcc_assert (failure == 0 || failure == 3);
5082 type = TREE_TYPE (decl);
5083 TREE_TYPE (DECL_INITIAL (decl)) = type;
5086 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5088 c_incomplete_type_error (NULL_TREE, type);
5089 return error_mark_node;
5092 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5093 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5094 TREE_SIDE_EFFECTS (complit) = 1;
5096 layout_decl (decl, 0);
5098 if (TREE_STATIC (decl))
5100 /* This decl needs a name for the assembler output. */
5101 set_compound_literal_name (decl);
5102 DECL_DEFER_OUTPUT (decl) = 1;
5103 DECL_COMDAT (decl) = 1;
5104 DECL_ARTIFICIAL (decl) = 1;
5105 DECL_IGNORED_P (decl) = 1;
5106 pushdecl (decl);
5107 rest_of_decl_compilation (decl, 1, 0);
5110 if (non_const)
5112 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5113 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5116 return complit;
5119 /* Check the type of a compound literal. Here we just check that it
5120 is valid for C++. */
5122 void
5123 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5125 if (warn_cxx_compat
5126 && (type_name->specs->typespec_kind == ctsk_tagdef
5127 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5128 warning_at (loc, OPT_Wc___compat,
5129 "defining a type in a compound literal is invalid in C++");
5132 /* Determine whether TYPE is a structure with a flexible array member,
5133 or a union containing such a structure (possibly recursively). */
5135 static bool
5136 flexible_array_type_p (tree type)
5138 tree x;
5139 switch (TREE_CODE (type))
5141 case RECORD_TYPE:
5142 x = TYPE_FIELDS (type);
5143 if (x == NULL_TREE)
5144 return false;
5145 while (DECL_CHAIN (x) != NULL_TREE)
5146 x = DECL_CHAIN (x);
5147 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5148 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5149 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5150 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5151 return true;
5152 return false;
5153 case UNION_TYPE:
5154 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5156 if (flexible_array_type_p (TREE_TYPE (x)))
5157 return true;
5159 return false;
5160 default:
5161 return false;
5165 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5166 replacing with appropriate values if they are invalid. */
5167 static void
5168 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
5170 tree type_mv;
5171 unsigned int max_width;
5172 unsigned HOST_WIDE_INT w;
5173 const char *name = (orig_name
5174 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5175 : _("<anonymous>"));
5177 /* Detect and ignore out of range field width and process valid
5178 field widths. */
5179 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5181 error ("bit-field %qs width not an integer constant", name);
5182 *width = integer_one_node;
5184 else
5186 if (TREE_CODE (*width) != INTEGER_CST)
5188 *width = c_fully_fold (*width, false, NULL);
5189 if (TREE_CODE (*width) == INTEGER_CST)
5190 pedwarn (input_location, OPT_Wpedantic,
5191 "bit-field %qs width not an integer constant expression",
5192 name);
5194 if (TREE_CODE (*width) != INTEGER_CST)
5196 error ("bit-field %qs width not an integer constant", name);
5197 *width = integer_one_node;
5199 constant_expression_warning (*width);
5200 if (tree_int_cst_sgn (*width) < 0)
5202 error ("negative width in bit-field %qs", name);
5203 *width = integer_one_node;
5205 else if (integer_zerop (*width) && orig_name)
5207 error ("zero width for bit-field %qs", name);
5208 *width = integer_one_node;
5212 /* Detect invalid bit-field type. */
5213 if (TREE_CODE (*type) != INTEGER_TYPE
5214 && TREE_CODE (*type) != BOOLEAN_TYPE
5215 && TREE_CODE (*type) != ENUMERAL_TYPE)
5217 error ("bit-field %qs has invalid type", name);
5218 *type = unsigned_type_node;
5221 type_mv = TYPE_MAIN_VARIANT (*type);
5222 if (!in_system_header_at (input_location)
5223 && type_mv != integer_type_node
5224 && type_mv != unsigned_type_node
5225 && type_mv != boolean_type_node)
5226 pedwarn_c90 (input_location, OPT_Wpedantic,
5227 "type of bit-field %qs is a GCC extension", name);
5229 max_width = TYPE_PRECISION (*type);
5231 if (0 < compare_tree_int (*width, max_width))
5233 error ("width of %qs exceeds its type", name);
5234 w = max_width;
5235 *width = build_int_cst (integer_type_node, w);
5237 else
5238 w = tree_to_uhwi (*width);
5240 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5242 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5243 if (!lt
5244 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5245 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5246 warning (0, "%qs is narrower than values of its type", name);
5252 /* Print warning about variable length array if necessary. */
5254 static void
5255 warn_variable_length_array (tree name, tree size)
5257 if (TREE_CONSTANT (size))
5259 if (name)
5260 pedwarn_c90 (input_location, OPT_Wvla,
5261 "ISO C90 forbids array %qE whose size "
5262 "can%'t be evaluated", name);
5263 else
5264 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5265 "whose size can%'t be evaluated");
5267 else
5269 if (name)
5270 pedwarn_c90 (input_location, OPT_Wvla,
5271 "ISO C90 forbids variable length array %qE", name);
5272 else
5273 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5274 "length array");
5278 /* Print warning about defaulting to int if necessary. */
5280 static void
5281 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5283 diagnostic_info diagnostic;
5284 va_list ap;
5286 va_start (ap, gmsgid);
5287 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
5288 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5289 diagnostic.option_index = opt;
5290 report_diagnostic (&diagnostic);
5291 va_end (ap);
5294 /* Given declspecs and a declarator,
5295 determine the name and type of the object declared
5296 and construct a ..._DECL node for it.
5297 (In one case we can return a ..._TYPE node instead.
5298 For invalid input we sometimes return 0.)
5300 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5302 DECL_CONTEXT says which syntactic context this declaration is in:
5303 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5304 FUNCDEF for a function definition. Like NORMAL but a few different
5305 error messages in each case. Return value may be zero meaning
5306 this definition is too screwy to try to parse.
5307 PARM for a parameter declaration (either within a function prototype
5308 or before a function body). Make a PARM_DECL, or return void_type_node.
5309 TYPENAME if for a typename (in a cast or sizeof).
5310 Don't make a DECL node; just return the ..._TYPE node.
5311 FIELD for a struct or union field; make a FIELD_DECL.
5312 INITIALIZED is true if the decl has an initializer.
5313 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5314 representing the width of the bit-field.
5315 DECL_ATTRS points to the list of attributes that should be added to this
5316 decl. Any nested attributes that belong on the decl itself will be
5317 added to this list.
5318 If EXPR is not NULL, any expressions that need to be evaluated as
5319 part of evaluating variably modified types will be stored in *EXPR.
5320 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5321 set to indicate whether operands in *EXPR can be used in constant
5322 expressions.
5323 DEPRECATED_STATE is a deprecated_states value indicating whether
5324 deprecation warnings should be suppressed.
5326 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5327 It may also be so in the PARM case, for a prototype where the
5328 argument type is specified but not the name.
5330 This function is where the complicated C meanings of `static'
5331 and `extern' are interpreted. */
5333 static tree
5334 grokdeclarator (const struct c_declarator *declarator,
5335 struct c_declspecs *declspecs,
5336 enum decl_context decl_context, bool initialized, tree *width,
5337 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5338 enum deprecated_states deprecated_state)
5340 tree type = declspecs->type;
5341 bool threadp = declspecs->thread_p;
5342 enum c_storage_class storage_class = declspecs->storage_class;
5343 int constp;
5344 int restrictp;
5345 int volatilep;
5346 int atomicp;
5347 int type_quals = TYPE_UNQUALIFIED;
5348 tree name = NULL_TREE;
5349 bool funcdef_flag = false;
5350 bool funcdef_syntax = false;
5351 bool size_varies = false;
5352 tree decl_attr = declspecs->decl_attr;
5353 int array_ptr_quals = TYPE_UNQUALIFIED;
5354 tree array_ptr_attrs = NULL_TREE;
5355 int array_parm_static = 0;
5356 bool array_parm_vla_unspec_p = false;
5357 tree returned_attrs = NULL_TREE;
5358 bool bitfield = width != NULL;
5359 tree element_type;
5360 struct c_arg_info *arg_info = 0;
5361 addr_space_t as1, as2, address_space;
5362 location_t loc = UNKNOWN_LOCATION;
5363 const char *errmsg;
5364 tree expr_dummy;
5365 bool expr_const_operands_dummy;
5366 enum c_declarator_kind first_non_attr_kind;
5367 unsigned int alignas_align = 0;
5369 if (TREE_CODE (type) == ERROR_MARK)
5370 return error_mark_node;
5371 if (expr == NULL)
5372 expr = &expr_dummy;
5373 if (expr_const_operands == NULL)
5374 expr_const_operands = &expr_const_operands_dummy;
5376 *expr = declspecs->expr;
5377 *expr_const_operands = declspecs->expr_const_operands;
5379 if (decl_context == FUNCDEF)
5380 funcdef_flag = true, decl_context = NORMAL;
5382 /* Look inside a declarator for the name being declared
5383 and get it as an IDENTIFIER_NODE, for an error message. */
5385 const struct c_declarator *decl = declarator;
5387 first_non_attr_kind = cdk_attrs;
5388 while (decl)
5389 switch (decl->kind)
5391 case cdk_array:
5392 loc = decl->id_loc;
5393 /* FALL THRU. */
5395 case cdk_function:
5396 case cdk_pointer:
5397 funcdef_syntax = (decl->kind == cdk_function);
5398 decl = decl->declarator;
5399 if (first_non_attr_kind == cdk_attrs)
5400 first_non_attr_kind = decl->kind;
5401 break;
5403 case cdk_attrs:
5404 decl = decl->declarator;
5405 break;
5407 case cdk_id:
5408 loc = decl->id_loc;
5409 if (decl->u.id)
5410 name = decl->u.id;
5411 if (first_non_attr_kind == cdk_attrs)
5412 first_non_attr_kind = decl->kind;
5413 decl = 0;
5414 break;
5416 default:
5417 gcc_unreachable ();
5419 if (name == 0)
5421 gcc_assert (decl_context == PARM
5422 || decl_context == TYPENAME
5423 || (decl_context == FIELD
5424 && declarator->kind == cdk_id));
5425 gcc_assert (!initialized);
5429 /* A function definition's declarator must have the form of
5430 a function declarator. */
5432 if (funcdef_flag && !funcdef_syntax)
5433 return 0;
5435 /* If this looks like a function definition, make it one,
5436 even if it occurs where parms are expected.
5437 Then store_parm_decls will reject it and not use it as a parm. */
5438 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5439 decl_context = PARM;
5441 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5442 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5444 if ((decl_context == NORMAL || decl_context == FIELD)
5445 && current_scope == file_scope
5446 && variably_modified_type_p (type, NULL_TREE))
5448 if (name)
5449 error_at (loc, "variably modified %qE at file scope", name);
5450 else
5451 error_at (loc, "variably modified field at file scope");
5452 type = integer_type_node;
5455 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5457 /* Diagnose defaulting to "int". */
5459 if (declspecs->default_int_p && !in_system_header_at (input_location))
5461 /* Issue a warning if this is an ISO C 99 program or if
5462 -Wreturn-type and this is a function, or if -Wimplicit;
5463 prefer the former warning since it is more explicit. */
5464 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5465 && funcdef_flag)
5466 warn_about_return_type = 1;
5467 else
5469 if (name)
5470 warn_defaults_to (loc, OPT_Wimplicit_int,
5471 "type defaults to %<int%> in declaration "
5472 "of %qE", name);
5473 else
5474 warn_defaults_to (loc, OPT_Wimplicit_int,
5475 "type defaults to %<int%> in type name");
5479 /* Adjust the type if a bit-field is being declared,
5480 -funsigned-bitfields applied and the type is not explicitly
5481 "signed". */
5482 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5483 && TREE_CODE (type) == INTEGER_TYPE)
5484 type = unsigned_type_for (type);
5486 /* Figure out the type qualifiers for the declaration. There are
5487 two ways a declaration can become qualified. One is something
5488 like `const int i' where the `const' is explicit. Another is
5489 something like `typedef const int CI; CI i' where the type of the
5490 declaration contains the `const'. A third possibility is that
5491 there is a type qualifier on the element type of a typedefed
5492 array type, in which case we should extract that qualifier so
5493 that c_apply_type_quals_to_decl receives the full list of
5494 qualifiers to work with (C90 is not entirely clear about whether
5495 duplicate qualifiers should be diagnosed in this case, but it
5496 seems most appropriate to do so). */
5497 element_type = strip_array_types (type);
5498 constp = declspecs->const_p + TYPE_READONLY (element_type);
5499 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5500 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5501 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5502 as1 = declspecs->address_space;
5503 as2 = TYPE_ADDR_SPACE (element_type);
5504 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5506 if (constp > 1)
5507 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5508 if (restrictp > 1)
5509 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5510 if (volatilep > 1)
5511 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5512 if (atomicp > 1)
5513 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5515 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5516 error_at (loc, "conflicting named address spaces (%s vs %s)",
5517 c_addr_space_name (as1), c_addr_space_name (as2));
5519 if ((TREE_CODE (type) == ARRAY_TYPE
5520 || first_non_attr_kind == cdk_array)
5521 && TYPE_QUALS (element_type))
5522 type = TYPE_MAIN_VARIANT (type);
5523 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5524 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5525 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5526 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5527 | ENCODE_QUAL_ADDR_SPACE (address_space));
5529 /* Applying the _Atomic qualifier to an array type (through the use
5530 of typedefs or typeof) must be detected here. If the qualifier
5531 is introduced later, any appearance of applying it to an array is
5532 actually applying it to an element of that array. */
5533 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5534 error_at (loc, "%<_Atomic%>-qualified array type");
5536 /* Warn about storage classes that are invalid for certain
5537 kinds of declarations (parameters, typenames, etc.). */
5539 if (funcdef_flag
5540 && (threadp
5541 || storage_class == csc_auto
5542 || storage_class == csc_register
5543 || storage_class == csc_typedef))
5545 if (storage_class == csc_auto)
5546 pedwarn (loc,
5547 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5548 "function definition declared %<auto%>");
5549 if (storage_class == csc_register)
5550 error_at (loc, "function definition declared %<register%>");
5551 if (storage_class == csc_typedef)
5552 error_at (loc, "function definition declared %<typedef%>");
5553 if (threadp)
5554 error_at (loc, "function definition declared %qs",
5555 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5556 threadp = false;
5557 if (storage_class == csc_auto
5558 || storage_class == csc_register
5559 || storage_class == csc_typedef)
5560 storage_class = csc_none;
5562 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5564 if (decl_context == PARM && storage_class == csc_register)
5566 else
5568 switch (decl_context)
5570 case FIELD:
5571 if (name)
5572 error_at (loc, "storage class specified for structure "
5573 "field %qE", name);
5574 else
5575 error_at (loc, "storage class specified for structure field");
5576 break;
5577 case PARM:
5578 if (name)
5579 error_at (loc, "storage class specified for parameter %qE",
5580 name);
5581 else
5582 error_at (loc, "storage class specified for unnamed parameter");
5583 break;
5584 default:
5585 error_at (loc, "storage class specified for typename");
5586 break;
5588 storage_class = csc_none;
5589 threadp = false;
5592 else if (storage_class == csc_extern
5593 && initialized
5594 && !funcdef_flag)
5596 /* 'extern' with initialization is invalid if not at file scope. */
5597 if (current_scope == file_scope)
5599 /* It is fine to have 'extern const' when compiling at C
5600 and C++ intersection. */
5601 if (!(warn_cxx_compat && constp))
5602 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5603 name);
5605 else
5606 error_at (loc, "%qE has both %<extern%> and initializer", name);
5608 else if (current_scope == file_scope)
5610 if (storage_class == csc_auto)
5611 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5612 name);
5613 if (pedantic && storage_class == csc_register)
5614 pedwarn (input_location, OPT_Wpedantic,
5615 "file-scope declaration of %qE specifies %<register%>", name);
5617 else
5619 if (storage_class == csc_extern && funcdef_flag)
5620 error_at (loc, "nested function %qE declared %<extern%>", name);
5621 else if (threadp && storage_class == csc_none)
5623 error_at (loc, "function-scope %qE implicitly auto and declared "
5624 "%qs", name,
5625 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5626 threadp = false;
5630 /* Now figure out the structure of the declarator proper.
5631 Descend through it, creating more complex types, until we reach
5632 the declared identifier (or NULL_TREE, in an absolute declarator).
5633 At each stage we maintain an unqualified version of the type
5634 together with any qualifiers that should be applied to it with
5635 c_build_qualified_type; this way, array types including
5636 multidimensional array types are first built up in unqualified
5637 form and then the qualified form is created with
5638 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5640 while (declarator && declarator->kind != cdk_id)
5642 if (type == error_mark_node)
5644 declarator = declarator->declarator;
5645 continue;
5648 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5649 a cdk_pointer (for *...),
5650 a cdk_function (for ...(...)),
5651 a cdk_attrs (for nested attributes),
5652 or a cdk_id (for the name being declared
5653 or the place in an absolute declarator
5654 where the name was omitted).
5655 For the last case, we have just exited the loop.
5657 At this point, TYPE is the type of elements of an array,
5658 or for a function to return, or for a pointer to point to.
5659 After this sequence of ifs, TYPE is the type of the
5660 array or function or pointer, and DECLARATOR has had its
5661 outermost layer removed. */
5663 if (array_ptr_quals != TYPE_UNQUALIFIED
5664 || array_ptr_attrs != NULL_TREE
5665 || array_parm_static)
5667 /* Only the innermost declarator (making a parameter be of
5668 array type which is converted to pointer type)
5669 may have static or type qualifiers. */
5670 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5671 array_ptr_quals = TYPE_UNQUALIFIED;
5672 array_ptr_attrs = NULL_TREE;
5673 array_parm_static = 0;
5676 switch (declarator->kind)
5678 case cdk_attrs:
5680 /* A declarator with embedded attributes. */
5681 tree attrs = declarator->u.attrs;
5682 const struct c_declarator *inner_decl;
5683 int attr_flags = 0;
5684 declarator = declarator->declarator;
5685 inner_decl = declarator;
5686 while (inner_decl->kind == cdk_attrs)
5687 inner_decl = inner_decl->declarator;
5688 if (inner_decl->kind == cdk_id)
5689 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5690 else if (inner_decl->kind == cdk_function)
5691 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5692 else if (inner_decl->kind == cdk_array)
5693 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5694 returned_attrs = decl_attributes (&type,
5695 chainon (returned_attrs, attrs),
5696 attr_flags);
5697 break;
5699 case cdk_array:
5701 tree itype = NULL_TREE;
5702 tree size = declarator->u.array.dimen;
5703 /* The index is a signed object `sizetype' bits wide. */
5704 tree index_type = c_common_signed_type (sizetype);
5706 array_ptr_quals = declarator->u.array.quals;
5707 array_ptr_attrs = declarator->u.array.attrs;
5708 array_parm_static = declarator->u.array.static_p;
5709 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5711 declarator = declarator->declarator;
5713 /* Check for some types that there cannot be arrays of. */
5715 if (VOID_TYPE_P (type))
5717 if (name)
5718 error_at (loc, "declaration of %qE as array of voids", name);
5719 else
5720 error_at (loc, "declaration of type name as array of voids");
5721 type = error_mark_node;
5724 if (TREE_CODE (type) == FUNCTION_TYPE)
5726 if (name)
5727 error_at (loc, "declaration of %qE as array of functions",
5728 name);
5729 else
5730 error_at (loc, "declaration of type name as array of "
5731 "functions");
5732 type = error_mark_node;
5735 if (pedantic && !in_system_header_at (input_location)
5736 && flexible_array_type_p (type))
5737 pedwarn (loc, OPT_Wpedantic,
5738 "invalid use of structure with flexible array member");
5740 if (size == error_mark_node)
5741 type = error_mark_node;
5743 if (type == error_mark_node)
5744 continue;
5746 /* If size was specified, set ITYPE to a range-type for
5747 that size. Otherwise, ITYPE remains null. finish_decl
5748 may figure it out from an initial value. */
5750 if (size)
5752 bool size_maybe_const = true;
5753 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5754 && !TREE_OVERFLOW (size));
5755 bool this_size_varies = false;
5757 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5758 lvalue. */
5759 STRIP_TYPE_NOPS (size);
5761 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5763 if (name)
5764 error_at (loc, "size of array %qE has non-integer type",
5765 name);
5766 else
5767 error_at (loc,
5768 "size of unnamed array has non-integer type");
5769 size = integer_one_node;
5772 size = c_fully_fold (size, false, &size_maybe_const);
5774 if (pedantic && size_maybe_const && integer_zerop (size))
5776 if (name)
5777 pedwarn (loc, OPT_Wpedantic,
5778 "ISO C forbids zero-size array %qE", name);
5779 else
5780 pedwarn (loc, OPT_Wpedantic,
5781 "ISO C forbids zero-size array");
5784 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5786 constant_expression_warning (size);
5787 if (tree_int_cst_sgn (size) < 0)
5789 if (name)
5790 error_at (loc, "size of array %qE is negative", name);
5791 else
5792 error_at (loc, "size of unnamed array is negative");
5793 size = integer_one_node;
5795 /* Handle a size folded to an integer constant but
5796 not an integer constant expression. */
5797 if (!size_int_const)
5799 /* If this is a file scope declaration of an
5800 ordinary identifier, this is invalid code;
5801 diagnosing it here and not subsequently
5802 treating the type as variable-length avoids
5803 more confusing diagnostics later. */
5804 if ((decl_context == NORMAL || decl_context == FIELD)
5805 && current_scope == file_scope)
5806 pedwarn (input_location, 0,
5807 "variably modified %qE at file scope",
5808 name);
5809 else
5810 this_size_varies = size_varies = true;
5811 warn_variable_length_array (name, size);
5814 else if ((decl_context == NORMAL || decl_context == FIELD)
5815 && current_scope == file_scope)
5817 error_at (loc, "variably modified %qE at file scope", name);
5818 size = integer_one_node;
5820 else
5822 /* Make sure the array size remains visibly
5823 nonconstant even if it is (eg) a const variable
5824 with known value. */
5825 this_size_varies = size_varies = true;
5826 warn_variable_length_array (name, size);
5827 if (flag_sanitize & SANITIZE_VLA
5828 && decl_context == NORMAL
5829 && do_ubsan_in_current_function ())
5831 /* Evaluate the array size only once. */
5832 size = c_save_expr (size);
5833 size = c_fully_fold (size, false, NULL);
5834 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5835 ubsan_instrument_vla (loc, size),
5836 size);
5840 if (integer_zerop (size) && !this_size_varies)
5842 /* A zero-length array cannot be represented with
5843 an unsigned index type, which is what we'll
5844 get with build_index_type. Create an
5845 open-ended range instead. */
5846 itype = build_range_type (sizetype, size, NULL_TREE);
5848 else
5850 /* Arrange for the SAVE_EXPR on the inside of the
5851 MINUS_EXPR, which allows the -1 to get folded
5852 with the +1 that happens when building TYPE_SIZE. */
5853 if (size_varies)
5854 size = save_expr (size);
5855 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5856 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5857 integer_zero_node, size);
5859 /* Compute the maximum valid index, that is, size
5860 - 1. Do the calculation in index_type, so that
5861 if it is a variable the computations will be
5862 done in the proper mode. */
5863 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5864 convert (index_type, size),
5865 convert (index_type,
5866 size_one_node));
5868 /* The above overflows when size does not fit
5869 in index_type.
5870 ??? While a size of INT_MAX+1 technically shouldn't
5871 cause an overflow (because we subtract 1), handling
5872 this case seems like an unnecessary complication. */
5873 if (TREE_CODE (size) == INTEGER_CST
5874 && !int_fits_type_p (size, index_type))
5876 if (name)
5877 error_at (loc, "size of array %qE is too large",
5878 name);
5879 else
5880 error_at (loc, "size of unnamed array is too large");
5881 type = error_mark_node;
5882 continue;
5885 itype = build_index_type (itype);
5887 if (this_size_varies)
5889 if (*expr)
5890 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5891 *expr, size);
5892 else
5893 *expr = size;
5894 *expr_const_operands &= size_maybe_const;
5897 else if (decl_context == FIELD)
5899 bool flexible_array_member = false;
5900 if (array_parm_vla_unspec_p)
5901 /* Field names can in fact have function prototype
5902 scope so [*] is disallowed here through making
5903 the field variably modified, not through being
5904 something other than a declaration with function
5905 prototype scope. */
5906 size_varies = true;
5907 else
5909 const struct c_declarator *t = declarator;
5910 while (t->kind == cdk_attrs)
5911 t = t->declarator;
5912 flexible_array_member = (t->kind == cdk_id);
5914 if (flexible_array_member
5915 && !in_system_header_at (input_location))
5916 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
5917 "support flexible array members");
5919 /* ISO C99 Flexible array members are effectively
5920 identical to GCC's zero-length array extension. */
5921 if (flexible_array_member || array_parm_vla_unspec_p)
5922 itype = build_range_type (sizetype, size_zero_node,
5923 NULL_TREE);
5925 else if (decl_context == PARM)
5927 if (array_parm_vla_unspec_p)
5929 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5930 size_varies = true;
5933 else if (decl_context == TYPENAME)
5935 if (array_parm_vla_unspec_p)
5937 /* C99 6.7.5.2p4 */
5938 warning (0, "%<[*]%> not in a declaration");
5939 /* We use this to avoid messing up with incomplete
5940 array types of the same type, that would
5941 otherwise be modified below. */
5942 itype = build_range_type (sizetype, size_zero_node,
5943 NULL_TREE);
5944 size_varies = true;
5948 /* Complain about arrays of incomplete types. */
5949 if (!COMPLETE_TYPE_P (type))
5951 error_at (loc, "array type has incomplete element type %qT",
5952 type);
5953 type = error_mark_node;
5955 else
5956 /* When itype is NULL, a shared incomplete array type is
5957 returned for all array of a given type. Elsewhere we
5958 make sure we don't complete that type before copying
5959 it, but here we want to make sure we don't ever
5960 modify the shared type, so we gcc_assert (itype)
5961 below. */
5963 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5964 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5965 type = build_qualified_type (type,
5966 ENCODE_QUAL_ADDR_SPACE (as));
5968 type = build_array_type (type, itype);
5971 if (type != error_mark_node)
5973 if (size_varies)
5975 /* It is ok to modify type here even if itype is
5976 NULL: if size_varies, we're in a
5977 multi-dimensional array and the inner type has
5978 variable size, so the enclosing shared array type
5979 must too. */
5980 if (size && TREE_CODE (size) == INTEGER_CST)
5981 type
5982 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5983 C_TYPE_VARIABLE_SIZE (type) = 1;
5986 /* The GCC extension for zero-length arrays differs from
5987 ISO flexible array members in that sizeof yields
5988 zero. */
5989 if (size && integer_zerop (size))
5991 gcc_assert (itype);
5992 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5993 TYPE_SIZE (type) = bitsize_zero_node;
5994 TYPE_SIZE_UNIT (type) = size_zero_node;
5995 SET_TYPE_STRUCTURAL_EQUALITY (type);
5997 if (array_parm_vla_unspec_p)
5999 gcc_assert (itype);
6000 /* The type is complete. C99 6.7.5.2p4 */
6001 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6002 TYPE_SIZE (type) = bitsize_zero_node;
6003 TYPE_SIZE_UNIT (type) = size_zero_node;
6004 SET_TYPE_STRUCTURAL_EQUALITY (type);
6008 if (decl_context != PARM
6009 && (array_ptr_quals != TYPE_UNQUALIFIED
6010 || array_ptr_attrs != NULL_TREE
6011 || array_parm_static))
6013 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6014 array_ptr_quals = TYPE_UNQUALIFIED;
6015 array_ptr_attrs = NULL_TREE;
6016 array_parm_static = 0;
6018 break;
6020 case cdk_function:
6022 /* Say it's a definition only for the declarator closest
6023 to the identifier, apart possibly from some
6024 attributes. */
6025 bool really_funcdef = false;
6026 tree arg_types;
6027 if (funcdef_flag)
6029 const struct c_declarator *t = declarator->declarator;
6030 while (t->kind == cdk_attrs)
6031 t = t->declarator;
6032 really_funcdef = (t->kind == cdk_id);
6035 /* Declaring a function type. Make sure we have a valid
6036 type for the function to return. */
6037 if (type == error_mark_node)
6038 continue;
6040 size_varies = false;
6042 /* Warn about some types functions can't return. */
6043 if (TREE_CODE (type) == FUNCTION_TYPE)
6045 if (name)
6046 error_at (loc, "%qE declared as function returning a "
6047 "function", name);
6048 else
6049 error_at (loc, "type name declared as function "
6050 "returning a function");
6051 type = integer_type_node;
6053 if (TREE_CODE (type) == ARRAY_TYPE)
6055 if (name)
6056 error_at (loc, "%qE declared as function returning an array",
6057 name);
6058 else
6059 error_at (loc, "type name declared as function returning "
6060 "an array");
6061 type = integer_type_node;
6063 errmsg = targetm.invalid_return_type (type);
6064 if (errmsg)
6066 error (errmsg);
6067 type = integer_type_node;
6070 /* Construct the function type and go to the next
6071 inner layer of declarator. */
6072 arg_info = declarator->u.arg_info;
6073 arg_types = grokparms (arg_info, really_funcdef);
6075 /* Type qualifiers before the return type of the function
6076 qualify the return type, not the function type. */
6077 if (type_quals)
6079 /* Type qualifiers on a function return type are
6080 normally permitted by the standard but have no
6081 effect, so give a warning at -Wreturn-type.
6082 Qualifiers on a void return type are banned on
6083 function definitions in ISO C; GCC used to used
6084 them for noreturn functions. */
6085 if (VOID_TYPE_P (type) && really_funcdef)
6086 pedwarn (loc, 0,
6087 "function definition has qualified void return type");
6088 else
6089 warning_at (loc, OPT_Wignored_qualifiers,
6090 "type qualifiers ignored on function return type");
6092 type = c_build_qualified_type (type, type_quals);
6094 type_quals = TYPE_UNQUALIFIED;
6096 type = build_function_type (type, arg_types);
6097 declarator = declarator->declarator;
6099 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6100 the formal parameter list of this FUNCTION_TYPE to point to
6101 the FUNCTION_TYPE node itself. */
6103 c_arg_tag *tag;
6104 unsigned ix;
6106 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6107 TYPE_CONTEXT (tag->type) = type;
6109 break;
6111 case cdk_pointer:
6113 /* Merge any constancy or volatility into the target type
6114 for the pointer. */
6115 if ((type_quals & TYPE_QUAL_ATOMIC)
6116 && TREE_CODE (type) == FUNCTION_TYPE)
6118 error_at (loc,
6119 "%<_Atomic%>-qualified function type");
6120 type_quals &= ~TYPE_QUAL_ATOMIC;
6122 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6123 && type_quals)
6124 pedwarn (loc, OPT_Wpedantic,
6125 "ISO C forbids qualified function types");
6126 if (type_quals)
6127 type = c_build_qualified_type (type, type_quals);
6128 size_varies = false;
6130 /* When the pointed-to type involves components of variable size,
6131 care must be taken to ensure that the size evaluation code is
6132 emitted early enough to dominate all the possible later uses
6133 and late enough for the variables on which it depends to have
6134 been assigned.
6136 This is expected to happen automatically when the pointed-to
6137 type has a name/declaration of it's own, but special attention
6138 is required if the type is anonymous.
6140 We handle the NORMAL and FIELD contexts here by attaching an
6141 artificial TYPE_DECL to such pointed-to type. This forces the
6142 sizes evaluation at a safe point and ensures it is not deferred
6143 until e.g. within a deeper conditional context.
6145 We expect nothing to be needed here for PARM or TYPENAME.
6146 Pushing a TYPE_DECL at this point for TYPENAME would actually
6147 be incorrect, as we might be in the middle of an expression
6148 with side effects on the pointed-to type size "arguments" prior
6149 to the pointer declaration point and the fake TYPE_DECL in the
6150 enclosing context would force the size evaluation prior to the
6151 side effects. */
6153 if (!TYPE_NAME (type)
6154 && (decl_context == NORMAL || decl_context == FIELD)
6155 && variably_modified_type_p (type, NULL_TREE))
6157 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6158 DECL_ARTIFICIAL (decl) = 1;
6159 pushdecl (decl);
6160 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6161 TYPE_NAME (type) = decl;
6164 type = c_build_pointer_type (type);
6166 /* Process type qualifiers (such as const or volatile)
6167 that were given inside the `*'. */
6168 type_quals = declarator->u.pointer_quals;
6170 declarator = declarator->declarator;
6171 break;
6173 default:
6174 gcc_unreachable ();
6177 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6179 /* Now TYPE has the actual type, apart from any qualifiers in
6180 TYPE_QUALS. */
6182 /* Warn about address space used for things other than static memory or
6183 pointers. */
6184 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6185 if (!ADDR_SPACE_GENERIC_P (address_space))
6187 if (decl_context == NORMAL)
6189 switch (storage_class)
6191 case csc_auto:
6192 error ("%qs combined with %<auto%> qualifier for %qE",
6193 c_addr_space_name (address_space), name);
6194 break;
6195 case csc_register:
6196 error ("%qs combined with %<register%> qualifier for %qE",
6197 c_addr_space_name (address_space), name);
6198 break;
6199 case csc_none:
6200 if (current_function_scope)
6202 error ("%qs specified for auto variable %qE",
6203 c_addr_space_name (address_space), name);
6204 break;
6206 break;
6207 case csc_static:
6208 case csc_extern:
6209 case csc_typedef:
6210 break;
6211 default:
6212 gcc_unreachable ();
6215 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6217 if (name)
6218 error ("%qs specified for parameter %qE",
6219 c_addr_space_name (address_space), name);
6220 else
6221 error ("%qs specified for unnamed parameter",
6222 c_addr_space_name (address_space));
6224 else if (decl_context == FIELD)
6226 if (name)
6227 error ("%qs specified for structure field %qE",
6228 c_addr_space_name (address_space), name);
6229 else
6230 error ("%qs specified for structure field",
6231 c_addr_space_name (address_space));
6235 /* Check the type and width of a bit-field. */
6236 if (bitfield)
6238 check_bitfield_type_and_width (&type, width, name);
6239 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6240 atomic types are permitted for bit-fields; we have no code to
6241 make bit-field accesses atomic, so disallow them. */
6242 if (type_quals & TYPE_QUAL_ATOMIC)
6244 if (name)
6245 error ("bit-field %qE has atomic type", name);
6246 else
6247 error ("bit-field has atomic type");
6248 type_quals &= ~TYPE_QUAL_ATOMIC;
6252 /* Reject invalid uses of _Alignas. */
6253 if (declspecs->alignas_p)
6255 if (storage_class == csc_typedef)
6256 error_at (loc, "alignment specified for typedef %qE", name);
6257 else if (storage_class == csc_register)
6258 error_at (loc, "alignment specified for %<register%> object %qE",
6259 name);
6260 else if (decl_context == PARM)
6262 if (name)
6263 error_at (loc, "alignment specified for parameter %qE", name);
6264 else
6265 error_at (loc, "alignment specified for unnamed parameter");
6267 else if (bitfield)
6269 if (name)
6270 error_at (loc, "alignment specified for bit-field %qE", name);
6271 else
6272 error_at (loc, "alignment specified for unnamed bit-field");
6274 else if (TREE_CODE (type) == FUNCTION_TYPE)
6275 error_at (loc, "alignment specified for function %qE", name);
6276 else if (declspecs->align_log != -1)
6278 alignas_align = 1U << declspecs->align_log;
6279 if (alignas_align < min_align_of_type (type))
6281 if (name)
6282 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6283 "alignment of %qE", name);
6284 else
6285 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6286 "alignment of unnamed field");
6287 alignas_align = 0;
6292 /* Did array size calculations overflow or does the array cover more
6293 than half of the address-space? */
6294 if (TREE_CODE (type) == ARRAY_TYPE
6295 && COMPLETE_TYPE_P (type)
6296 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6297 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
6299 if (name)
6300 error_at (loc, "size of array %qE is too large", name);
6301 else
6302 error_at (loc, "size of unnamed array is too large");
6303 /* If we proceed with the array type as it is, we'll eventually
6304 crash in tree_to_[su]hwi(). */
6305 type = error_mark_node;
6308 /* If this is declaring a typedef name, return a TYPE_DECL. */
6310 if (storage_class == csc_typedef)
6312 tree decl;
6313 if ((type_quals & TYPE_QUAL_ATOMIC)
6314 && TREE_CODE (type) == FUNCTION_TYPE)
6316 error_at (loc,
6317 "%<_Atomic%>-qualified function type");
6318 type_quals &= ~TYPE_QUAL_ATOMIC;
6320 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6321 && type_quals)
6322 pedwarn (loc, OPT_Wpedantic,
6323 "ISO C forbids qualified function types");
6324 if (type_quals)
6325 type = c_build_qualified_type (type, type_quals);
6326 decl = build_decl (declarator->id_loc,
6327 TYPE_DECL, declarator->u.id, type);
6328 if (declspecs->explicit_signed_p)
6329 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6330 if (declspecs->inline_p)
6331 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6332 if (declspecs->noreturn_p)
6333 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6335 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6337 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6339 if (b != NULL
6340 && b->decl != NULL_TREE
6341 && (B_IN_CURRENT_SCOPE (b)
6342 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6343 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6345 warning_at (declarator->id_loc, OPT_Wc___compat,
6346 ("using %qD as both a typedef and a tag is "
6347 "invalid in C++"),
6348 decl);
6349 if (b->locus != UNKNOWN_LOCATION)
6350 inform (b->locus, "originally defined here");
6354 return decl;
6357 /* If this is a type name (such as, in a cast or sizeof),
6358 compute the type and return it now. */
6360 if (decl_context == TYPENAME)
6362 /* Note that the grammar rejects storage classes in typenames
6363 and fields. */
6364 gcc_assert (storage_class == csc_none && !threadp
6365 && !declspecs->inline_p && !declspecs->noreturn_p);
6366 if ((type_quals & TYPE_QUAL_ATOMIC)
6367 && TREE_CODE (type) == FUNCTION_TYPE)
6369 error_at (loc,
6370 "%<_Atomic%>-qualified function type");
6371 type_quals &= ~TYPE_QUAL_ATOMIC;
6373 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6374 && type_quals)
6375 pedwarn (loc, OPT_Wpedantic,
6376 "ISO C forbids const or volatile function types");
6377 if (type_quals)
6378 type = c_build_qualified_type (type, type_quals);
6379 return type;
6382 if (pedantic && decl_context == FIELD
6383 && variably_modified_type_p (type, NULL_TREE))
6385 /* C99 6.7.2.1p8 */
6386 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6387 "have a variably modified type");
6390 /* Aside from typedefs and type names (handle above),
6391 `void' at top level (not within pointer)
6392 is allowed only in public variables.
6393 We don't complain about parms either, but that is because
6394 a better error message can be made later. */
6396 if (VOID_TYPE_P (type) && decl_context != PARM
6397 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6398 && (storage_class == csc_extern
6399 || (current_scope == file_scope
6400 && !(storage_class == csc_static
6401 || storage_class == csc_register)))))
6403 error_at (loc, "variable or field %qE declared void", name);
6404 type = integer_type_node;
6407 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6408 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6411 tree decl;
6413 if (decl_context == PARM)
6415 tree promoted_type;
6416 bool array_parameter_p = false;
6418 /* A parameter declared as an array of T is really a pointer to T.
6419 One declared as a function is really a pointer to a function. */
6421 if (TREE_CODE (type) == ARRAY_TYPE)
6423 /* Transfer const-ness of array into that of type pointed to. */
6424 type = TREE_TYPE (type);
6425 if (type_quals)
6426 type = c_build_qualified_type (type, type_quals);
6427 type = c_build_pointer_type (type);
6428 type_quals = array_ptr_quals;
6429 if (type_quals)
6430 type = c_build_qualified_type (type, type_quals);
6432 /* We don't yet implement attributes in this context. */
6433 if (array_ptr_attrs != NULL_TREE)
6434 warning_at (loc, OPT_Wattributes,
6435 "attributes in parameter array declarator ignored");
6437 size_varies = false;
6438 array_parameter_p = true;
6440 else if (TREE_CODE (type) == FUNCTION_TYPE)
6442 if (type_quals & TYPE_QUAL_ATOMIC)
6444 error_at (loc,
6445 "%<_Atomic%>-qualified function type");
6446 type_quals &= ~TYPE_QUAL_ATOMIC;
6448 else if (type_quals)
6449 pedwarn (loc, OPT_Wpedantic,
6450 "ISO C forbids qualified function types");
6451 if (type_quals)
6452 type = c_build_qualified_type (type, type_quals);
6453 type = c_build_pointer_type (type);
6454 type_quals = TYPE_UNQUALIFIED;
6456 else if (type_quals)
6457 type = c_build_qualified_type (type, type_quals);
6459 decl = build_decl (declarator->id_loc,
6460 PARM_DECL, declarator->u.id, type);
6461 if (size_varies)
6462 C_DECL_VARIABLE_SIZE (decl) = 1;
6463 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6465 /* Compute the type actually passed in the parmlist,
6466 for the case where there is no prototype.
6467 (For example, shorts and chars are passed as ints.)
6468 When there is a prototype, this is overridden later. */
6470 if (type == error_mark_node)
6471 promoted_type = type;
6472 else
6473 promoted_type = c_type_promotes_to (type);
6475 DECL_ARG_TYPE (decl) = promoted_type;
6476 if (declspecs->inline_p)
6477 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6478 if (declspecs->noreturn_p)
6479 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6481 else if (decl_context == FIELD)
6483 /* Note that the grammar rejects storage classes in typenames
6484 and fields. */
6485 gcc_assert (storage_class == csc_none && !threadp
6486 && !declspecs->inline_p && !declspecs->noreturn_p);
6488 /* Structure field. It may not be a function. */
6490 if (TREE_CODE (type) == FUNCTION_TYPE)
6492 error_at (loc, "field %qE declared as a function", name);
6493 type = build_pointer_type (type);
6495 else if (TREE_CODE (type) != ERROR_MARK
6496 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6498 if (name)
6499 error_at (loc, "field %qE has incomplete type", name);
6500 else
6501 error_at (loc, "unnamed field has incomplete type");
6502 type = error_mark_node;
6504 else if (TREE_CODE (type) == ARRAY_TYPE
6505 && TYPE_DOMAIN (type) == NULL_TREE)
6507 /* We have a flexible array member through a typedef.
6508 Set suitable range. Whether this is a correct position
6509 for a flexible array member will be determined elsewhere. */
6510 if (!in_system_header_at (input_location))
6511 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6512 "support flexible array members");
6513 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6514 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6515 NULL_TREE);
6517 type = c_build_qualified_type (type, type_quals);
6518 decl = build_decl (declarator->id_loc,
6519 FIELD_DECL, declarator->u.id, type);
6520 DECL_NONADDRESSABLE_P (decl) = bitfield;
6521 if (bitfield && !declarator->u.id)
6522 TREE_NO_WARNING (decl) = 1;
6524 if (size_varies)
6525 C_DECL_VARIABLE_SIZE (decl) = 1;
6527 else if (TREE_CODE (type) == FUNCTION_TYPE)
6529 if (storage_class == csc_register || threadp)
6531 error_at (loc, "invalid storage class for function %qE", name);
6533 else if (current_scope != file_scope)
6535 /* Function declaration not at file scope. Storage
6536 classes other than `extern' are not allowed, C99
6537 6.7.1p5, and `extern' makes no difference. However,
6538 GCC allows 'auto', perhaps with 'inline', to support
6539 nested functions. */
6540 if (storage_class == csc_auto)
6541 pedwarn (loc, OPT_Wpedantic,
6542 "invalid storage class for function %qE", name);
6543 else if (storage_class == csc_static)
6545 error_at (loc, "invalid storage class for function %qE", name);
6546 if (funcdef_flag)
6547 storage_class = declspecs->storage_class = csc_none;
6548 else
6549 return 0;
6553 decl = build_decl (declarator->id_loc,
6554 FUNCTION_DECL, declarator->u.id, type);
6555 decl = build_decl_attribute_variant (decl, decl_attr);
6557 if (type_quals & TYPE_QUAL_ATOMIC)
6559 error_at (loc,
6560 "%<_Atomic%>-qualified function type");
6561 type_quals &= ~TYPE_QUAL_ATOMIC;
6563 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6564 pedwarn (loc, OPT_Wpedantic,
6565 "ISO C forbids qualified function types");
6567 /* Every function declaration is an external reference
6568 (DECL_EXTERNAL) except for those which are not at file
6569 scope and are explicitly declared "auto". This is
6570 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6571 GCC to signify a forward declaration of a nested function. */
6572 if (storage_class == csc_auto && current_scope != file_scope)
6573 DECL_EXTERNAL (decl) = 0;
6574 /* In C99, a function which is declared 'inline' with 'extern'
6575 is not an external reference (which is confusing). It
6576 means that the later definition of the function must be output
6577 in this file, C99 6.7.4p6. In GNU C89, a function declared
6578 'extern inline' is an external reference. */
6579 else if (declspecs->inline_p && storage_class != csc_static)
6580 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6581 == flag_gnu89_inline);
6582 else
6583 DECL_EXTERNAL (decl) = !initialized;
6585 /* Record absence of global scope for `static' or `auto'. */
6586 TREE_PUBLIC (decl)
6587 = !(storage_class == csc_static || storage_class == csc_auto);
6589 /* For a function definition, record the argument information
6590 block where store_parm_decls will look for it. */
6591 if (funcdef_flag)
6592 current_function_arg_info = arg_info;
6594 if (declspecs->default_int_p)
6595 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6597 /* Record presence of `inline' and `_Noreturn', if it is
6598 reasonable. */
6599 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6601 if (declspecs->inline_p)
6602 pedwarn (loc, 0, "cannot inline function %<main%>");
6603 if (declspecs->noreturn_p)
6604 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6606 else
6608 if (declspecs->inline_p)
6609 /* Record that the function is declared `inline'. */
6610 DECL_DECLARED_INLINE_P (decl) = 1;
6611 if (declspecs->noreturn_p)
6613 if (flag_isoc99)
6614 pedwarn_c99 (loc, OPT_Wpedantic,
6615 "ISO C99 does not support %<_Noreturn%>");
6616 else
6617 pedwarn_c99 (loc, OPT_Wpedantic,
6618 "ISO C90 does not support %<_Noreturn%>");
6619 TREE_THIS_VOLATILE (decl) = 1;
6623 else
6625 /* It's a variable. */
6626 /* An uninitialized decl with `extern' is a reference. */
6627 int extern_ref = !initialized && storage_class == csc_extern;
6629 type = c_build_qualified_type (type, type_quals);
6631 /* C99 6.2.2p7: It is invalid (compile-time undefined
6632 behavior) to create an 'extern' declaration for a
6633 variable if there is a global declaration that is
6634 'static' and the global declaration is not visible.
6635 (If the static declaration _is_ currently visible,
6636 the 'extern' declaration is taken to refer to that decl.) */
6637 if (extern_ref && current_scope != file_scope)
6639 tree global_decl = identifier_global_value (declarator->u.id);
6640 tree visible_decl = lookup_name (declarator->u.id);
6642 if (global_decl
6643 && global_decl != visible_decl
6644 && TREE_CODE (global_decl) == VAR_DECL
6645 && !TREE_PUBLIC (global_decl))
6646 error_at (loc, "variable previously declared %<static%> "
6647 "redeclared %<extern%>");
6650 decl = build_decl (declarator->id_loc,
6651 VAR_DECL, declarator->u.id, type);
6652 if (size_varies)
6653 C_DECL_VARIABLE_SIZE (decl) = 1;
6655 if (declspecs->inline_p)
6656 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6657 if (declspecs->noreturn_p)
6658 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6660 /* At file scope, an initialized extern declaration may follow
6661 a static declaration. In that case, DECL_EXTERNAL will be
6662 reset later in start_decl. */
6663 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6665 /* At file scope, the presence of a `static' or `register' storage
6666 class specifier, or the absence of all storage class specifiers
6667 makes this declaration a definition (perhaps tentative). Also,
6668 the absence of `static' makes it public. */
6669 if (current_scope == file_scope)
6671 TREE_PUBLIC (decl) = storage_class != csc_static;
6672 TREE_STATIC (decl) = !extern_ref;
6674 /* Not at file scope, only `static' makes a static definition. */
6675 else
6677 TREE_STATIC (decl) = (storage_class == csc_static);
6678 TREE_PUBLIC (decl) = extern_ref;
6681 if (threadp)
6682 set_decl_tls_model (decl, decl_default_tls_model (decl));
6685 if ((storage_class == csc_extern
6686 || (storage_class == csc_none
6687 && TREE_CODE (type) == FUNCTION_TYPE
6688 && !funcdef_flag))
6689 && variably_modified_type_p (type, NULL_TREE))
6691 /* C99 6.7.5.2p2 */
6692 if (TREE_CODE (type) == FUNCTION_TYPE)
6693 error_at (loc, "non-nested function with variably modified type");
6694 else
6695 error_at (loc, "object with variably modified type must have "
6696 "no linkage");
6699 /* Record `register' declaration for warnings on &
6700 and in case doing stupid register allocation. */
6702 if (storage_class == csc_register)
6704 C_DECL_REGISTER (decl) = 1;
6705 DECL_REGISTER (decl) = 1;
6708 /* Record constancy and volatility. */
6709 c_apply_type_quals_to_decl (type_quals, decl);
6711 /* Apply _Alignas specifiers. */
6712 if (alignas_align)
6714 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6715 DECL_USER_ALIGN (decl) = 1;
6718 /* If a type has volatile components, it should be stored in memory.
6719 Otherwise, the fact that those components are volatile
6720 will be ignored, and would even crash the compiler.
6721 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6722 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6723 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6724 || TREE_CODE (decl) == RESULT_DECL))
6726 /* It is not an error for a structure with volatile fields to
6727 be declared register, but reset DECL_REGISTER since it
6728 cannot actually go in a register. */
6729 int was_reg = C_DECL_REGISTER (decl);
6730 C_DECL_REGISTER (decl) = 0;
6731 DECL_REGISTER (decl) = 0;
6732 c_mark_addressable (decl);
6733 C_DECL_REGISTER (decl) = was_reg;
6736 /* This is the earliest point at which we might know the assembler
6737 name of a variable. Thus, if it's known before this, die horribly. */
6738 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6740 if (warn_cxx_compat
6741 && TREE_CODE (decl) == VAR_DECL
6742 && TREE_PUBLIC (decl)
6743 && TREE_STATIC (decl)
6744 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6745 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6746 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6747 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6748 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6749 ("non-local variable %qD with anonymous type is "
6750 "questionable in C++"),
6751 decl);
6753 return decl;
6757 /* Decode the parameter-list info for a function type or function definition.
6758 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6759 if there is an identifier list instead of a parameter decl list).
6760 These two functions are separate because when a function returns
6761 or receives functions then each is called multiple times but the order
6762 of calls is different. The last call to `grokparms' is always the one
6763 that contains the formal parameter names of a function definition.
6765 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6767 FUNCDEF_FLAG is true for a function definition, false for
6768 a mere declaration. A nonempty identifier-list gets an error message
6769 when FUNCDEF_FLAG is false. */
6771 static tree
6772 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6774 tree arg_types = arg_info->types;
6776 if (funcdef_flag && arg_info->had_vla_unspec)
6778 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6779 /* C99 6.7.5.2p4 */
6780 error ("%<[*]%> not allowed in other than function prototype scope");
6783 if (arg_types == 0 && !funcdef_flag
6784 && !in_system_header_at (input_location))
6785 warning (OPT_Wstrict_prototypes,
6786 "function declaration isn%'t a prototype");
6788 if (arg_types == error_mark_node)
6789 return 0; /* don't set TYPE_ARG_TYPES in this case */
6791 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6793 if (!funcdef_flag)
6795 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6796 arg_info->parms = NULL_TREE;
6798 else
6799 arg_info->parms = arg_info->types;
6801 arg_info->types = 0;
6802 return 0;
6804 else
6806 tree parm, type, typelt;
6807 unsigned int parmno;
6808 const char *errmsg;
6810 /* If there is a parameter of incomplete type in a definition,
6811 this is an error. In a declaration this is valid, and a
6812 struct or union type may be completed later, before any calls
6813 or definition of the function. In the case where the tag was
6814 first declared within the parameter list, a warning has
6815 already been given. If a parameter has void type, then
6816 however the function cannot be defined or called, so
6817 warn. */
6819 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6820 parm;
6821 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6823 type = TREE_VALUE (typelt);
6824 if (type == error_mark_node)
6825 continue;
6827 if (!COMPLETE_TYPE_P (type))
6829 if (funcdef_flag)
6831 if (DECL_NAME (parm))
6832 error_at (input_location,
6833 "parameter %u (%q+D) has incomplete type",
6834 parmno, parm);
6835 else
6836 error_at (DECL_SOURCE_LOCATION (parm),
6837 "parameter %u has incomplete type",
6838 parmno);
6840 TREE_VALUE (typelt) = error_mark_node;
6841 TREE_TYPE (parm) = error_mark_node;
6842 arg_types = NULL_TREE;
6844 else if (VOID_TYPE_P (type))
6846 if (DECL_NAME (parm))
6847 warning_at (input_location, 0,
6848 "parameter %u (%q+D) has void type",
6849 parmno, parm);
6850 else
6851 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6852 "parameter %u has void type",
6853 parmno);
6857 errmsg = targetm.invalid_parameter_type (type);
6858 if (errmsg)
6860 error (errmsg);
6861 TREE_VALUE (typelt) = error_mark_node;
6862 TREE_TYPE (parm) = error_mark_node;
6863 arg_types = NULL_TREE;
6866 if (DECL_NAME (parm) && TREE_USED (parm))
6867 warn_if_shadowing (parm);
6869 return arg_types;
6873 /* Allocate and initialize a c_arg_info structure from the parser's
6874 obstack. */
6876 struct c_arg_info *
6877 build_arg_info (void)
6879 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6880 ret->parms = NULL_TREE;
6881 ret->tags = NULL;
6882 ret->types = NULL_TREE;
6883 ret->others = NULL_TREE;
6884 ret->pending_sizes = NULL;
6885 ret->had_vla_unspec = 0;
6886 return ret;
6889 /* Take apart the current scope and return a c_arg_info structure with
6890 info on a parameter list just parsed.
6892 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6894 ELLIPSIS being true means the argument list ended in '...' so don't
6895 append a sentinel (void_list_node) to the end of the type-list.
6897 EXPR is NULL or an expression that needs to be evaluated for the
6898 side effects of array size expressions in the parameters. */
6900 struct c_arg_info *
6901 get_parm_info (bool ellipsis, tree expr)
6903 struct c_binding *b = current_scope->bindings;
6904 struct c_arg_info *arg_info = build_arg_info ();
6906 tree parms = 0;
6907 vec<c_arg_tag, va_gc> *tags = NULL;
6908 tree types = 0;
6909 tree others = 0;
6911 static bool explained_incomplete_types = false;
6912 bool gave_void_only_once_err = false;
6914 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6916 /* The bindings in this scope must not get put into a block.
6917 We will take care of deleting the binding nodes. */
6918 current_scope->bindings = 0;
6920 /* This function is only called if there was *something* on the
6921 parameter list. */
6922 gcc_assert (b);
6924 /* A parameter list consisting solely of 'void' indicates that the
6925 function takes no arguments. But if the 'void' is qualified
6926 (by 'const' or 'volatile'), or has a storage class specifier
6927 ('register'), then the behavior is undefined; issue an error.
6928 Typedefs for 'void' are OK (see DR#157). */
6929 if (b->prev == 0 /* one binding */
6930 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6931 && !DECL_NAME (b->decl) /* anonymous */
6932 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6934 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
6935 || C_DECL_REGISTER (b->decl))
6936 error ("%<void%> as only parameter may not be qualified");
6938 /* There cannot be an ellipsis. */
6939 if (ellipsis)
6940 error ("%<void%> must be the only parameter");
6942 arg_info->types = void_list_node;
6943 return arg_info;
6946 if (!ellipsis)
6947 types = void_list_node;
6949 /* Break up the bindings list into parms, tags, types, and others;
6950 apply sanity checks; purge the name-to-decl bindings. */
6951 while (b)
6953 tree decl = b->decl;
6954 tree type = TREE_TYPE (decl);
6955 c_arg_tag tag;
6956 const char *keyword;
6958 switch (TREE_CODE (decl))
6960 case PARM_DECL:
6961 if (b->id)
6963 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6964 I_SYMBOL_BINDING (b->id) = b->shadowed;
6967 /* Check for forward decls that never got their actual decl. */
6968 if (TREE_ASM_WRITTEN (decl))
6969 error ("parameter %q+D has just a forward declaration", decl);
6970 /* Check for (..., void, ...) and issue an error. */
6971 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6973 if (!gave_void_only_once_err)
6975 error ("%<void%> must be the only parameter");
6976 gave_void_only_once_err = true;
6979 else
6981 /* Valid parameter, add it to the list. */
6982 DECL_CHAIN (decl) = parms;
6983 parms = decl;
6985 /* Since there is a prototype, args are passed in their
6986 declared types. The back end may override this later. */
6987 DECL_ARG_TYPE (decl) = type;
6988 types = tree_cons (0, type, types);
6990 break;
6992 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6993 case UNION_TYPE: keyword = "union"; goto tag;
6994 case RECORD_TYPE: keyword = "struct"; goto tag;
6995 tag:
6996 /* Types may not have tag-names, in which case the type
6997 appears in the bindings list with b->id NULL. */
6998 if (b->id)
7000 gcc_assert (I_TAG_BINDING (b->id) == b);
7001 I_TAG_BINDING (b->id) = b->shadowed;
7004 /* Warn about any struct, union or enum tags defined in a
7005 parameter list. The scope of such types is limited to
7006 the parameter list, which is rarely if ever desirable
7007 (it's impossible to call such a function with type-
7008 correct arguments). An anonymous union parm type is
7009 meaningful as a GNU extension, so don't warn for that. */
7010 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7012 if (b->id)
7013 /* The %s will be one of 'struct', 'union', or 'enum'. */
7014 warning (0, "%<%s %E%> declared inside parameter list",
7015 keyword, b->id);
7016 else
7017 /* The %s will be one of 'struct', 'union', or 'enum'. */
7018 warning (0, "anonymous %s declared inside parameter list",
7019 keyword);
7021 if (!explained_incomplete_types)
7023 warning (0, "its scope is only this definition or declaration,"
7024 " which is probably not what you want");
7025 explained_incomplete_types = true;
7029 tag.id = b->id;
7030 tag.type = decl;
7031 vec_safe_push (tags, tag);
7032 break;
7034 case CONST_DECL:
7035 case TYPE_DECL:
7036 case FUNCTION_DECL:
7037 /* CONST_DECLs appear here when we have an embedded enum,
7038 and TYPE_DECLs appear here when we have an embedded struct
7039 or union. No warnings for this - we already warned about the
7040 type itself. FUNCTION_DECLs appear when there is an implicit
7041 function declaration in the parameter list. */
7043 /* When we reinsert this decl in the function body, we need
7044 to reconstruct whether it was marked as nested. */
7045 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7046 ? b->nested
7047 : !b->nested);
7048 DECL_CHAIN (decl) = others;
7049 others = decl;
7050 /* fall through */
7052 case ERROR_MARK:
7053 /* error_mark_node appears here when we have an undeclared
7054 variable. Just throw it away. */
7055 if (b->id)
7057 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7058 I_SYMBOL_BINDING (b->id) = b->shadowed;
7060 break;
7062 /* Other things that might be encountered. */
7063 case LABEL_DECL:
7064 case VAR_DECL:
7065 default:
7066 gcc_unreachable ();
7069 b = free_binding_and_advance (b);
7072 arg_info->parms = parms;
7073 arg_info->tags = tags;
7074 arg_info->types = types;
7075 arg_info->others = others;
7076 arg_info->pending_sizes = expr;
7077 return arg_info;
7080 /* Get the struct, enum or union (CODE says which) with tag NAME.
7081 Define the tag as a forward-reference with location LOC if it is
7082 not defined. Return a c_typespec structure for the type
7083 specifier. */
7085 struct c_typespec
7086 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7088 struct c_typespec ret;
7089 tree ref;
7090 location_t refloc;
7092 ret.expr = NULL_TREE;
7093 ret.expr_const_operands = true;
7095 /* If a cross reference is requested, look up the type
7096 already defined for this tag and return it. */
7098 ref = lookup_tag (code, name, 0, &refloc);
7099 /* If this is the right type of tag, return what we found.
7100 (This reference will be shadowed by shadow_tag later if appropriate.)
7101 If this is the wrong type of tag, do not return it. If it was the
7102 wrong type in the same scope, we will have had an error
7103 message already; if in a different scope and declaring
7104 a name, pending_xref_error will give an error message; but if in a
7105 different scope and not declaring a name, this tag should
7106 shadow the previous declaration of a different type of tag, and
7107 this would not work properly if we return the reference found.
7108 (For example, with "struct foo" in an outer scope, "union foo;"
7109 must shadow that tag with a new one of union type.) */
7110 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7111 if (ref && TREE_CODE (ref) == code)
7113 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7114 && loc != UNKNOWN_LOCATION
7115 && warn_cxx_compat)
7117 switch (code)
7119 case ENUMERAL_TYPE:
7120 warning_at (loc, OPT_Wc___compat,
7121 ("enum type defined in struct or union "
7122 "is not visible in C++"));
7123 inform (refloc, "enum type defined here");
7124 break;
7125 case RECORD_TYPE:
7126 warning_at (loc, OPT_Wc___compat,
7127 ("struct defined in struct or union "
7128 "is not visible in C++"));
7129 inform (refloc, "struct defined here");
7130 break;
7131 case UNION_TYPE:
7132 warning_at (loc, OPT_Wc___compat,
7133 ("union defined in struct or union "
7134 "is not visible in C++"));
7135 inform (refloc, "union defined here");
7136 break;
7137 default:
7138 gcc_unreachable();
7142 ret.spec = ref;
7143 return ret;
7146 /* If no such tag is yet defined, create a forward-reference node
7147 and record it as the "definition".
7148 When a real declaration of this type is found,
7149 the forward-reference will be altered into a real type. */
7151 ref = make_node (code);
7152 if (code == ENUMERAL_TYPE)
7154 /* Give the type a default layout like unsigned int
7155 to avoid crashing if it does not get defined. */
7156 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7157 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
7158 TYPE_USER_ALIGN (ref) = 0;
7159 TYPE_UNSIGNED (ref) = 1;
7160 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7161 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7162 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7165 pushtag (loc, name, ref);
7167 ret.spec = ref;
7168 return ret;
7171 /* Get the struct, enum or union (CODE says which) with tag NAME.
7172 Define the tag as a forward-reference if it is not defined.
7173 Return a tree for the type. */
7175 tree
7176 xref_tag (enum tree_code code, tree name)
7178 return parser_xref_tag (input_location, code, name).spec;
7181 /* Make sure that the tag NAME is defined *in the current scope*
7182 at least as a forward reference.
7183 LOC is the location of the struct's definition.
7184 CODE says which kind of tag NAME ought to be.
7186 This stores the current value of the file static STRUCT_PARSE_INFO
7187 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7188 new c_struct_parse_info structure. The old value of
7189 STRUCT_PARSE_INFO is restored in finish_struct. */
7191 tree
7192 start_struct (location_t loc, enum tree_code code, tree name,
7193 struct c_struct_parse_info **enclosing_struct_parse_info)
7195 /* If there is already a tag defined at this scope
7196 (as a forward reference), just return it. */
7198 tree ref = NULL_TREE;
7199 location_t refloc = UNKNOWN_LOCATION;
7201 if (name != NULL_TREE)
7202 ref = lookup_tag (code, name, 1, &refloc);
7203 if (ref && TREE_CODE (ref) == code)
7205 if (TYPE_SIZE (ref))
7207 if (code == UNION_TYPE)
7208 error_at (loc, "redefinition of %<union %E%>", name);
7209 else
7210 error_at (loc, "redefinition of %<struct %E%>", name);
7211 if (refloc != UNKNOWN_LOCATION)
7212 inform (refloc, "originally defined here");
7213 /* Don't create structures using a name already in use. */
7214 ref = NULL_TREE;
7216 else if (C_TYPE_BEING_DEFINED (ref))
7218 if (code == UNION_TYPE)
7219 error_at (loc, "nested redefinition of %<union %E%>", name);
7220 else
7221 error_at (loc, "nested redefinition of %<struct %E%>", name);
7222 /* Don't bother to report "originally defined here" for a
7223 nested redefinition; the original definition should be
7224 obvious. */
7225 /* Don't create structures that contain themselves. */
7226 ref = NULL_TREE;
7230 /* Otherwise create a forward-reference just so the tag is in scope. */
7232 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7234 ref = make_node (code);
7235 pushtag (loc, name, ref);
7238 C_TYPE_BEING_DEFINED (ref) = 1;
7239 TYPE_PACKED (ref) = flag_pack_struct;
7241 *enclosing_struct_parse_info = struct_parse_info;
7242 struct_parse_info = XNEW (struct c_struct_parse_info);
7243 struct_parse_info->struct_types.create (0);
7244 struct_parse_info->fields.create (0);
7245 struct_parse_info->typedefs_seen.create (0);
7247 /* FIXME: This will issue a warning for a use of a type defined
7248 within a statement expr used within sizeof, et. al. This is not
7249 terribly serious as C++ doesn't permit statement exprs within
7250 sizeof anyhow. */
7251 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7252 warning_at (loc, OPT_Wc___compat,
7253 "defining type in %qs expression is invalid in C++",
7254 (in_sizeof
7255 ? "sizeof"
7256 : (in_typeof ? "typeof" : "alignof")));
7258 return ref;
7261 /* Process the specs, declarator and width (NULL if omitted)
7262 of a structure component, returning a FIELD_DECL node.
7263 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7264 DECL_ATTRS is as for grokdeclarator.
7266 LOC is the location of the structure component.
7268 This is done during the parsing of the struct declaration.
7269 The FIELD_DECL nodes are chained together and the lot of them
7270 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7272 tree
7273 grokfield (location_t loc,
7274 struct c_declarator *declarator, struct c_declspecs *declspecs,
7275 tree width, tree *decl_attrs)
7277 tree value;
7279 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7280 && width == NULL_TREE)
7282 /* This is an unnamed decl.
7284 If we have something of the form "union { list } ;" then this
7285 is the anonymous union extension. Similarly for struct.
7287 If this is something of the form "struct foo;", then
7288 If MS or Plan 9 extensions are enabled, this is handled as
7289 an anonymous struct.
7290 Otherwise this is a forward declaration of a structure tag.
7292 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7293 If foo names a structure or union without a tag, then this
7294 is an anonymous struct (this is permitted by C11).
7295 If MS or Plan 9 extensions are enabled and foo names a
7296 structure, then again this is an anonymous struct.
7297 Otherwise this is an error.
7299 Oh what a horrid tangled web we weave. I wonder if MS consciously
7300 took this from Plan 9 or if it was an accident of implementation
7301 that took root before someone noticed the bug... */
7303 tree type = declspecs->type;
7304 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
7305 || TREE_CODE (type) == UNION_TYPE);
7306 bool ok = false;
7308 if (type_ok
7309 && (flag_ms_extensions
7310 || flag_plan9_extensions
7311 || !declspecs->typedef_p))
7313 if (flag_ms_extensions || flag_plan9_extensions)
7314 ok = true;
7315 else if (TYPE_NAME (type) == NULL)
7316 ok = true;
7317 else
7318 ok = false;
7320 if (!ok)
7322 pedwarn (loc, 0, "declaration does not declare anything");
7323 return NULL_TREE;
7325 if (flag_isoc99)
7326 pedwarn_c99 (loc, OPT_Wpedantic,
7327 "ISO C99 doesn%'t support unnamed structs/unions");
7328 else
7329 pedwarn_c99 (loc, OPT_Wpedantic,
7330 "ISO C90 doesn%'t support unnamed structs/unions");
7333 value = grokdeclarator (declarator, declspecs, FIELD, false,
7334 width ? &width : NULL, decl_attrs, NULL, NULL,
7335 DEPRECATED_NORMAL);
7337 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7338 DECL_INITIAL (value) = width;
7340 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7342 /* If we currently have a binding for this field, set the
7343 in_struct field in the binding, so that we warn about lookups
7344 which find it. */
7345 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7346 if (b != NULL)
7348 /* If the in_struct field is not yet set, push it on a list
7349 to be cleared when this struct is finished. */
7350 if (!b->in_struct)
7352 struct_parse_info->fields.safe_push (b);
7353 b->in_struct = 1;
7358 return value;
7361 /* Subroutine of detect_field_duplicates: return whether X and Y,
7362 which are both fields in the same struct, have duplicate field
7363 names. */
7365 static bool
7366 is_duplicate_field (tree x, tree y)
7368 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7369 return true;
7371 /* When using -fplan9-extensions, an anonymous field whose name is a
7372 typedef can duplicate a field name. */
7373 if (flag_plan9_extensions
7374 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7376 tree xt, xn, yt, yn;
7378 xt = TREE_TYPE (x);
7379 if (DECL_NAME (x) != NULL_TREE)
7380 xn = DECL_NAME (x);
7381 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
7382 && TYPE_NAME (xt) != NULL_TREE
7383 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7384 xn = DECL_NAME (TYPE_NAME (xt));
7385 else
7386 xn = NULL_TREE;
7388 yt = TREE_TYPE (y);
7389 if (DECL_NAME (y) != NULL_TREE)
7390 yn = DECL_NAME (y);
7391 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
7392 && TYPE_NAME (yt) != NULL_TREE
7393 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7394 yn = DECL_NAME (TYPE_NAME (yt));
7395 else
7396 yn = NULL_TREE;
7398 if (xn != NULL_TREE && xn == yn)
7399 return true;
7402 return false;
7405 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7406 to HTAB, giving errors for any duplicates. */
7408 static void
7409 detect_field_duplicates_hash (tree fieldlist,
7410 hash_table<pointer_hash <tree_node> > *htab)
7412 tree x, y;
7413 tree_node **slot;
7415 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7416 if ((y = DECL_NAME (x)) != 0)
7418 slot = htab->find_slot (y, INSERT);
7419 if (*slot)
7421 error ("duplicate member %q+D", x);
7422 DECL_NAME (x) = NULL_TREE;
7424 *slot = y;
7426 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7427 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7429 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7431 /* When using -fplan9-extensions, an anonymous field whose
7432 name is a typedef can duplicate a field name. */
7433 if (flag_plan9_extensions
7434 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7435 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7437 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7438 slot = htab->find_slot (xn, INSERT);
7439 if (*slot)
7440 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7441 *slot = xn;
7446 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7447 the list such that this does not present a problem later. */
7449 static void
7450 detect_field_duplicates (tree fieldlist)
7452 tree x, y;
7453 int timeout = 10;
7455 /* If the struct is the list of instance variables of an Objective-C
7456 class, then we need to check all the instance variables of
7457 superclasses when checking for duplicates (since you can't have
7458 an instance variable in a subclass with the same name as an
7459 instance variable in a superclass). We pass on this job to the
7460 Objective-C compiler. objc_detect_field_duplicates() will return
7461 false if we are not checking the list of instance variables and
7462 the C frontend should proceed with the standard field duplicate
7463 checks. If we are checking the list of instance variables, the
7464 ObjC frontend will do the check, emit the errors if needed, and
7465 then return true. */
7466 if (c_dialect_objc ())
7467 if (objc_detect_field_duplicates (false))
7468 return;
7470 /* First, see if there are more than "a few" fields.
7471 This is trivially true if there are zero or one fields. */
7472 if (!fieldlist || !DECL_CHAIN (fieldlist))
7473 return;
7474 x = fieldlist;
7475 do {
7476 timeout--;
7477 if (DECL_NAME (x) == NULL_TREE
7478 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7479 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7480 timeout = 0;
7481 x = DECL_CHAIN (x);
7482 } while (timeout > 0 && x);
7484 /* If there were "few" fields and no anonymous structures or unions,
7485 avoid the overhead of allocating a hash table. Instead just do
7486 the nested traversal thing. */
7487 if (timeout > 0)
7489 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7490 /* When using -fplan9-extensions, we can have duplicates
7491 between typedef names and fields. */
7492 if (DECL_NAME (x)
7493 || (flag_plan9_extensions
7494 && DECL_NAME (x) == NULL_TREE
7495 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7496 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7497 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7498 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7500 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7501 if (is_duplicate_field (y, x))
7503 error ("duplicate member %q+D", x);
7504 DECL_NAME (x) = NULL_TREE;
7508 else
7510 hash_table<pointer_hash <tree_node> > htab (37);
7511 detect_field_duplicates_hash (fieldlist, &htab);
7515 /* Finish up struct info used by -Wc++-compat. */
7517 static void
7518 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7519 location_t record_loc)
7521 unsigned int ix;
7522 tree x;
7523 struct c_binding *b;
7525 if (fieldlist == NULL_TREE)
7527 if (code == RECORD_TYPE)
7528 warning_at (record_loc, OPT_Wc___compat,
7529 "empty struct has size 0 in C, size 1 in C++");
7530 else
7531 warning_at (record_loc, OPT_Wc___compat,
7532 "empty union has size 0 in C, size 1 in C++");
7535 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7536 the current struct. We do this now at the end of the struct
7537 because the flag is used to issue visibility warnings, and we
7538 only want to issue those warnings if the type is referenced
7539 outside of the struct declaration. */
7540 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7541 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7543 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7544 typedefs used when declaring fields in this struct. If the name
7545 of any of the fields is also a typedef name then the struct would
7546 not parse in C++, because the C++ lookup rules say that the
7547 typedef name would be looked up in the context of the struct, and
7548 would thus be the field rather than the typedef. */
7549 if (!struct_parse_info->typedefs_seen.is_empty ()
7550 && fieldlist != NULL_TREE)
7552 /* Use a hash_set<tree> using the name of the typedef. We can use
7553 a hash_set<tree> because identifiers are interned. */
7554 hash_set<tree> tset;
7556 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7557 tset.add (DECL_NAME (x));
7559 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7561 if (DECL_NAME (x) != NULL_TREE
7562 && tset.contains (DECL_NAME (x)))
7564 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7565 ("using %qD as both field and typedef name is "
7566 "invalid in C++"),
7568 /* FIXME: It would be nice to report the location where
7569 the typedef name is used. */
7574 /* For each field which has a binding and which was not defined in
7575 an enclosing struct, clear the in_struct field. */
7576 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7577 b->in_struct = 0;
7580 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7581 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7582 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7583 ATTRIBUTES are attributes to be applied to the structure.
7585 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7586 the struct was started. */
7588 tree
7589 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7590 struct c_struct_parse_info *enclosing_struct_parse_info)
7592 tree x;
7593 bool toplevel = file_scope == current_scope;
7594 int saw_named_field;
7596 /* If this type was previously laid out as a forward reference,
7597 make sure we lay it out again. */
7599 TYPE_SIZE (t) = 0;
7601 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7603 if (pedantic)
7605 for (x = fieldlist; x; x = DECL_CHAIN (x))
7607 if (DECL_NAME (x) != 0)
7608 break;
7609 if (flag_isoc11
7610 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7611 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7612 break;
7615 if (x == 0)
7617 if (TREE_CODE (t) == UNION_TYPE)
7619 if (fieldlist)
7620 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7621 else
7622 pedwarn (loc, OPT_Wpedantic, "union has no members");
7624 else
7626 if (fieldlist)
7627 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7628 else
7629 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7634 /* Install struct as DECL_CONTEXT of each field decl.
7635 Also process specified field sizes, found in the DECL_INITIAL,
7636 storing 0 there after the type has been changed to precision equal
7637 to its width, rather than the precision of the specified standard
7638 type. (Correct layout requires the original type to have been preserved
7639 until now.) */
7641 saw_named_field = 0;
7642 for (x = fieldlist; x; x = DECL_CHAIN (x))
7644 if (TREE_TYPE (x) == error_mark_node)
7645 continue;
7647 DECL_CONTEXT (x) = t;
7649 /* If any field is const, the structure type is pseudo-const. */
7650 if (TREE_READONLY (x))
7651 C_TYPE_FIELDS_READONLY (t) = 1;
7652 else
7654 /* A field that is pseudo-const makes the structure likewise. */
7655 tree t1 = strip_array_types (TREE_TYPE (x));
7656 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7657 && C_TYPE_FIELDS_READONLY (t1))
7658 C_TYPE_FIELDS_READONLY (t) = 1;
7661 /* Any field that is volatile means variables of this type must be
7662 treated in some ways as volatile. */
7663 if (TREE_THIS_VOLATILE (x))
7664 C_TYPE_FIELDS_VOLATILE (t) = 1;
7666 /* Any field of nominal variable size implies structure is too. */
7667 if (C_DECL_VARIABLE_SIZE (x))
7668 C_TYPE_VARIABLE_SIZE (t) = 1;
7670 if (DECL_INITIAL (x))
7672 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7673 DECL_SIZE (x) = bitsize_int (width);
7674 DECL_BIT_FIELD (x) = 1;
7675 SET_DECL_C_BIT_FIELD (x);
7678 if (TYPE_PACKED (t)
7679 && (DECL_BIT_FIELD (x)
7680 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7681 DECL_PACKED (x) = 1;
7683 /* Detect flexible array member in an invalid context. */
7684 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7685 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7686 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7687 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7689 if (TREE_CODE (t) == UNION_TYPE)
7691 error_at (DECL_SOURCE_LOCATION (x),
7692 "flexible array member in union");
7693 TREE_TYPE (x) = error_mark_node;
7695 else if (DECL_CHAIN (x) != NULL_TREE)
7697 error_at (DECL_SOURCE_LOCATION (x),
7698 "flexible array member not at end of struct");
7699 TREE_TYPE (x) = error_mark_node;
7701 else if (!saw_named_field)
7703 error_at (DECL_SOURCE_LOCATION (x),
7704 "flexible array member in otherwise empty struct");
7705 TREE_TYPE (x) = error_mark_node;
7709 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7710 && flexible_array_type_p (TREE_TYPE (x)))
7711 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7712 "invalid use of structure with flexible array member");
7714 if (DECL_NAME (x)
7715 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7716 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7717 saw_named_field = 1;
7720 detect_field_duplicates (fieldlist);
7722 /* Now we have the nearly final fieldlist. Record it,
7723 then lay out the structure or union (including the fields). */
7725 TYPE_FIELDS (t) = fieldlist;
7727 maybe_apply_pragma_scalar_storage_order (t);
7729 layout_type (t);
7731 if (TYPE_SIZE_UNIT (t)
7732 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7733 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7734 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7735 error ("type %qT is too large", t);
7737 /* Give bit-fields their proper types and rewrite the type of array fields
7738 with scalar component if the enclosing type has reverse storage order. */
7739 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
7741 if (TREE_CODE (field) == FIELD_DECL
7742 && DECL_INITIAL (field)
7743 && TREE_TYPE (field) != error_mark_node)
7745 unsigned HOST_WIDE_INT width
7746 = tree_to_uhwi (DECL_INITIAL (field));
7747 tree type = TREE_TYPE (field);
7748 if (width != TYPE_PRECISION (type))
7750 TREE_TYPE (field)
7751 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7752 DECL_MODE (field) = TYPE_MODE (TREE_TYPE (field));
7754 DECL_INITIAL (field) = 0;
7756 else if (TYPE_REVERSE_STORAGE_ORDER (t)
7757 && TREE_CODE (field) == FIELD_DECL
7758 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
7760 tree ftype = TREE_TYPE (field);
7761 if (!RECORD_OR_UNION_TYPE_P (strip_array_types (ftype)))
7763 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
7764 tree *typep = &fmain_type;
7765 do {
7766 *typep = build_distinct_type_copy (*typep);
7767 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
7768 typep = &TREE_TYPE (*typep);
7769 } while (TREE_CODE (*typep) == ARRAY_TYPE);
7770 TREE_TYPE (field)
7771 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
7776 /* Now we have the truly final field list.
7777 Store it in this type and in the variants. */
7779 TYPE_FIELDS (t) = fieldlist;
7781 /* If there are lots of fields, sort so we can look through them fast.
7782 We arbitrarily consider 16 or more elts to be "a lot". */
7785 int len = 0;
7787 for (x = fieldlist; x; x = DECL_CHAIN (x))
7789 if (len > 15 || DECL_NAME (x) == NULL)
7790 break;
7791 len += 1;
7794 if (len > 15)
7796 tree *field_array;
7797 struct lang_type *space;
7798 struct sorted_fields_type *space2;
7800 len += list_length (x);
7802 /* Use the same allocation policy here that make_node uses, to
7803 ensure that this lives as long as the rest of the struct decl.
7804 All decls in an inline function need to be saved. */
7806 space = ggc_cleared_alloc<struct lang_type> ();
7807 space2 = (sorted_fields_type *) ggc_internal_alloc
7808 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7810 len = 0;
7811 space->s = space2;
7812 field_array = &space2->elts[0];
7813 for (x = fieldlist; x; x = DECL_CHAIN (x))
7815 field_array[len++] = x;
7817 /* If there is anonymous struct or union, break out of the loop. */
7818 if (DECL_NAME (x) == NULL)
7819 break;
7821 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7822 if (x == NULL)
7824 TYPE_LANG_SPECIFIC (t) = space;
7825 TYPE_LANG_SPECIFIC (t)->s->len = len;
7826 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7827 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7832 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7834 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7835 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7836 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7837 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7838 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7841 /* If this was supposed to be a transparent union, but we can't
7842 make it one, warn and turn off the flag. */
7843 if (TREE_CODE (t) == UNION_TYPE
7844 && TYPE_TRANSPARENT_AGGR (t)
7845 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7847 TYPE_TRANSPARENT_AGGR (t) = 0;
7848 warning_at (loc, 0, "union cannot be made transparent");
7851 /* If this structure or union completes the type of any previous
7852 variable declaration, lay it out and output its rtl.
7854 Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
7855 in dwarf2out via rest_of_decl_compilation below and means
7856 something totally different. Since we will be clearing
7857 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
7858 clear it ahead of time and avoid problems in dwarf2out. Ideally,
7859 C_TYPE_INCOMPLETE_VARS should use some language specific
7860 node. */
7861 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7862 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7863 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
7865 tree decl = TREE_VALUE (x);
7866 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7867 layout_array_type (TREE_TYPE (decl));
7868 if (TREE_CODE (decl) != TYPE_DECL)
7870 layout_decl (decl, 0);
7871 if (c_dialect_objc ())
7872 objc_check_decl (decl);
7873 rest_of_decl_compilation (decl, toplevel, 0);
7877 /* Update type location to the one of the definition, instead of e.g.
7878 a forward declaration. */
7879 if (TYPE_STUB_DECL (t))
7880 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7882 /* Finish debugging output for this type. */
7883 rest_of_type_compilation (t, toplevel);
7885 /* If we're inside a function proper, i.e. not file-scope and not still
7886 parsing parameters, then arrange for the size of a variable sized type
7887 to be bound now. */
7888 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7889 add_stmt (build_stmt (loc,
7890 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7892 if (warn_cxx_compat)
7893 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
7895 struct_parse_info->struct_types.release ();
7896 struct_parse_info->fields.release ();
7897 struct_parse_info->typedefs_seen.release ();
7898 XDELETE (struct_parse_info);
7900 struct_parse_info = enclosing_struct_parse_info;
7902 /* If this struct is defined inside a struct, add it to
7903 struct_types. */
7904 if (warn_cxx_compat
7905 && struct_parse_info != NULL
7906 && !in_sizeof && !in_typeof && !in_alignof)
7907 struct_parse_info->struct_types.safe_push (t);
7909 return t;
7912 /* Lay out the type T, and its element type, and so on. */
7914 static void
7915 layout_array_type (tree t)
7917 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7918 layout_array_type (TREE_TYPE (t));
7919 layout_type (t);
7922 /* Begin compiling the definition of an enumeration type.
7923 NAME is its name (or null if anonymous).
7924 LOC is the enum's location.
7925 Returns the type object, as yet incomplete.
7926 Also records info about it so that build_enumerator
7927 may be used to declare the individual values as they are read. */
7929 tree
7930 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7932 tree enumtype = NULL_TREE;
7933 location_t enumloc = UNKNOWN_LOCATION;
7935 /* If this is the real definition for a previous forward reference,
7936 fill in the contents in the same object that used to be the
7937 forward reference. */
7939 if (name != NULL_TREE)
7940 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7942 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7944 enumtype = make_node (ENUMERAL_TYPE);
7945 pushtag (loc, name, enumtype);
7948 if (C_TYPE_BEING_DEFINED (enumtype))
7949 error_at (loc, "nested redefinition of %<enum %E%>", name);
7951 C_TYPE_BEING_DEFINED (enumtype) = 1;
7953 if (TYPE_VALUES (enumtype) != 0)
7955 /* This enum is a named one that has been declared already. */
7956 error_at (loc, "redeclaration of %<enum %E%>", name);
7957 if (enumloc != UNKNOWN_LOCATION)
7958 inform (enumloc, "originally defined here");
7960 /* Completely replace its old definition.
7961 The old enumerators remain defined, however. */
7962 TYPE_VALUES (enumtype) = 0;
7965 the_enum->enum_next_value = integer_zero_node;
7966 the_enum->enum_overflow = 0;
7968 if (flag_short_enums)
7969 TYPE_PACKED (enumtype) = 1;
7971 /* FIXME: This will issue a warning for a use of a type defined
7972 within sizeof in a statement expr. This is not terribly serious
7973 as C++ doesn't permit statement exprs within sizeof anyhow. */
7974 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7975 warning_at (loc, OPT_Wc___compat,
7976 "defining type in %qs expression is invalid in C++",
7977 (in_sizeof
7978 ? "sizeof"
7979 : (in_typeof ? "typeof" : "alignof")));
7981 return enumtype;
7984 /* After processing and defining all the values of an enumeration type,
7985 install their decls in the enumeration type and finish it off.
7986 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7987 and ATTRIBUTES are the specified attributes.
7988 Returns ENUMTYPE. */
7990 tree
7991 finish_enum (tree enumtype, tree values, tree attributes)
7993 tree pair, tem;
7994 tree minnode = 0, maxnode = 0;
7995 int precision;
7996 signop sign;
7997 bool toplevel = (file_scope == current_scope);
7998 struct lang_type *lt;
8000 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8002 /* Calculate the maximum value of any enumerator in this type. */
8004 if (values == error_mark_node)
8005 minnode = maxnode = integer_zero_node;
8006 else
8008 minnode = maxnode = TREE_VALUE (values);
8009 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8011 tree value = TREE_VALUE (pair);
8012 if (tree_int_cst_lt (maxnode, value))
8013 maxnode = value;
8014 if (tree_int_cst_lt (value, minnode))
8015 minnode = value;
8019 /* Construct the final type of this enumeration. It is the same
8020 as one of the integral types - the narrowest one that fits, except
8021 that normally we only go as narrow as int - and signed iff any of
8022 the values are negative. */
8023 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8024 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8025 tree_int_cst_min_precision (maxnode, sign));
8027 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
8029 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8030 if (tem == NULL)
8032 warning (0, "enumeration values exceed range of largest integer");
8033 tem = long_long_integer_type_node;
8036 else
8037 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8039 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8040 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8041 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8042 TYPE_ALIGN (enumtype) = TYPE_ALIGN (tem);
8043 TYPE_SIZE (enumtype) = 0;
8045 /* If the precision of the type was specified with an attribute and it
8046 was too small, give an error. Otherwise, use it. */
8047 if (TYPE_PRECISION (enumtype)
8048 && lookup_attribute ("mode", attributes))
8050 if (precision > TYPE_PRECISION (enumtype))
8051 error ("specified mode too small for enumeral values");
8053 else
8054 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8056 layout_type (enumtype);
8058 if (values != error_mark_node)
8060 /* Change the type of the enumerators to be the enum type. We
8061 need to do this irrespective of the size of the enum, for
8062 proper type checking. Replace the DECL_INITIALs of the
8063 enumerators, and the value slots of the list, with copies
8064 that have the enum type; they cannot be modified in place
8065 because they may be shared (e.g. integer_zero_node) Finally,
8066 change the purpose slots to point to the names of the decls. */
8067 for (pair = values; pair; pair = TREE_CHAIN (pair))
8069 tree enu = TREE_PURPOSE (pair);
8070 tree ini = DECL_INITIAL (enu);
8072 TREE_TYPE (enu) = enumtype;
8074 /* The ISO C Standard mandates enumerators to have type int,
8075 even though the underlying type of an enum type is
8076 unspecified. However, GCC allows enumerators of any
8077 integer type as an extensions. build_enumerator()
8078 converts any enumerators that fit in an int to type int,
8079 to avoid promotions to unsigned types when comparing
8080 integers with enumerators that fit in the int range.
8081 When -pedantic is given, build_enumerator() would have
8082 already warned about those that don't fit. Here we
8083 convert the rest to the enumerator type. */
8084 if (TREE_TYPE (ini) != integer_type_node)
8085 ini = convert (enumtype, ini);
8087 DECL_INITIAL (enu) = ini;
8088 TREE_PURPOSE (pair) = DECL_NAME (enu);
8089 TREE_VALUE (pair) = ini;
8092 TYPE_VALUES (enumtype) = values;
8095 /* Record the min/max values so that we can warn about bit-field
8096 enumerations that are too small for the values. */
8097 lt = ggc_cleared_alloc<struct lang_type> ();
8098 lt->enum_min = minnode;
8099 lt->enum_max = maxnode;
8100 TYPE_LANG_SPECIFIC (enumtype) = lt;
8102 /* Fix up all variant types of this enum type. */
8103 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8105 if (tem == enumtype)
8106 continue;
8107 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8108 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8109 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8110 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8111 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8112 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8113 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8114 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
8115 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8116 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8117 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8120 /* Finish debugging output for this type. */
8121 rest_of_type_compilation (enumtype, toplevel);
8123 /* If this enum is defined inside a struct, add it to
8124 struct_types. */
8125 if (warn_cxx_compat
8126 && struct_parse_info != NULL
8127 && !in_sizeof && !in_typeof && !in_alignof)
8128 struct_parse_info->struct_types.safe_push (enumtype);
8130 return enumtype;
8133 /* Build and install a CONST_DECL for one value of the
8134 current enumeration type (one that was begun with start_enum).
8135 DECL_LOC is the location of the enumerator.
8136 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8137 Return a tree-list containing the CONST_DECL and its value.
8138 Assignment of sequential values by default is handled here. */
8140 tree
8141 build_enumerator (location_t decl_loc, location_t loc,
8142 struct c_enum_contents *the_enum, tree name, tree value)
8144 tree decl, type;
8146 /* Validate and default VALUE. */
8148 if (value != 0)
8150 /* Don't issue more errors for error_mark_node (i.e. an
8151 undeclared identifier) - just ignore the value expression. */
8152 if (value == error_mark_node)
8153 value = 0;
8154 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8156 error_at (loc, "enumerator value for %qE is not an integer constant",
8157 name);
8158 value = 0;
8160 else
8162 if (TREE_CODE (value) != INTEGER_CST)
8164 value = c_fully_fold (value, false, NULL);
8165 if (TREE_CODE (value) == INTEGER_CST)
8166 pedwarn (loc, OPT_Wpedantic,
8167 "enumerator value for %qE is not an integer "
8168 "constant expression", name);
8170 if (TREE_CODE (value) != INTEGER_CST)
8172 error ("enumerator value for %qE is not an integer constant",
8173 name);
8174 value = 0;
8176 else
8178 value = default_conversion (value);
8179 constant_expression_warning (value);
8184 /* Default based on previous value. */
8185 /* It should no longer be possible to have NON_LVALUE_EXPR
8186 in the default. */
8187 if (value == 0)
8189 value = the_enum->enum_next_value;
8190 if (the_enum->enum_overflow)
8191 error_at (loc, "overflow in enumeration values");
8193 /* Even though the underlying type of an enum is unspecified, the
8194 type of enumeration constants is explicitly defined as int
8195 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8196 an extension. */
8197 else if (!int_fits_type_p (value, integer_type_node))
8198 pedwarn (loc, OPT_Wpedantic,
8199 "ISO C restricts enumerator values to range of %<int%>");
8201 /* The ISO C Standard mandates enumerators to have type int, even
8202 though the underlying type of an enum type is unspecified.
8203 However, GCC allows enumerators of any integer type as an
8204 extensions. Here we convert any enumerators that fit in an int
8205 to type int, to avoid promotions to unsigned types when comparing
8206 integers with enumerators that fit in the int range. When
8207 -pedantic is given, we would have already warned about those that
8208 don't fit. We have to do this here rather than in finish_enum
8209 because this value may be used to define more enumerators. */
8210 if (int_fits_type_p (value, integer_type_node))
8211 value = convert (integer_type_node, value);
8213 /* Set basis for default for next value. */
8214 the_enum->enum_next_value
8215 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8216 PLUS_EXPR, value, integer_one_node, 0);
8217 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8219 /* Now create a declaration for the enum value name. */
8221 type = TREE_TYPE (value);
8222 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8223 TYPE_PRECISION (integer_type_node)),
8224 (TYPE_PRECISION (type)
8225 >= TYPE_PRECISION (integer_type_node)
8226 && TYPE_UNSIGNED (type)));
8228 decl = build_decl (decl_loc, CONST_DECL, name, type);
8229 DECL_INITIAL (decl) = convert (type, value);
8230 pushdecl (decl);
8232 return tree_cons (decl, value, NULL_TREE);
8236 /* Create the FUNCTION_DECL for a function definition.
8237 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8238 the declaration; they describe the function's name and the type it returns,
8239 but twisted together in a fashion that parallels the syntax of C.
8241 This function creates a binding context for the function body
8242 as well as setting up the FUNCTION_DECL in current_function_decl.
8244 Returns 1 on success. If the DECLARATOR is not suitable for a function
8245 (it defines a datum instead), we return 0, which tells
8246 yyparse to report a parse error. */
8249 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8250 tree attributes)
8252 tree decl1, old_decl;
8253 tree restype, resdecl;
8254 location_t loc;
8256 current_function_returns_value = 0; /* Assume, until we see it does. */
8257 current_function_returns_null = 0;
8258 current_function_returns_abnormally = 0;
8259 warn_about_return_type = 0;
8260 c_switch_stack = NULL;
8262 /* Indicate no valid break/continue context by setting these variables
8263 to some non-null, non-label value. We'll notice and emit the proper
8264 error message in c_finish_bc_stmt. */
8265 c_break_label = c_cont_label = size_zero_node;
8267 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8268 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8269 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8271 /* If the declarator is not suitable for a function definition,
8272 cause a syntax error. */
8273 if (decl1 == 0
8274 || TREE_CODE (decl1) != FUNCTION_DECL)
8275 return 0;
8277 loc = DECL_SOURCE_LOCATION (decl1);
8279 c_decl_attributes (&decl1, attributes, 0);
8281 if (DECL_DECLARED_INLINE_P (decl1)
8282 && DECL_UNINLINABLE (decl1)
8283 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8284 warning_at (loc, OPT_Wattributes,
8285 "inline function %qD given attribute noinline",
8286 decl1);
8288 /* Handle gnu_inline attribute. */
8289 if (declspecs->inline_p
8290 && !flag_gnu89_inline
8291 && TREE_CODE (decl1) == FUNCTION_DECL
8292 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8293 || current_function_decl))
8295 if (declspecs->storage_class != csc_static)
8296 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8299 announce_function (decl1);
8301 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8303 error_at (loc, "return type is an incomplete type");
8304 /* Make it return void instead. */
8305 TREE_TYPE (decl1)
8306 = build_function_type (void_type_node,
8307 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8310 if (warn_about_return_type)
8311 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8312 : (warn_return_type ? OPT_Wreturn_type
8313 : OPT_Wimplicit_int),
8314 "return type defaults to %<int%>");
8316 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8317 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8318 DECL_INITIAL (decl1) = error_mark_node;
8320 /* A nested function is not global. */
8321 if (current_function_decl != 0)
8322 TREE_PUBLIC (decl1) = 0;
8324 /* If this definition isn't a prototype and we had a prototype declaration
8325 before, copy the arg type info from that prototype. */
8326 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8327 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8328 old_decl = 0;
8329 current_function_prototype_locus = UNKNOWN_LOCATION;
8330 current_function_prototype_built_in = false;
8331 current_function_prototype_arg_types = NULL_TREE;
8332 if (!prototype_p (TREE_TYPE (decl1)))
8334 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8335 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8336 TREE_TYPE (TREE_TYPE (old_decl))))
8338 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8339 TREE_TYPE (decl1));
8340 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8341 current_function_prototype_built_in
8342 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8343 current_function_prototype_arg_types
8344 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8346 if (TREE_PUBLIC (decl1))
8348 /* If there is an external prototype declaration of this
8349 function, record its location but do not copy information
8350 to this decl. This may be an invisible declaration
8351 (built-in or in a scope which has finished) or simply
8352 have more refined argument types than any declaration
8353 found above. */
8354 struct c_binding *b;
8355 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8356 if (B_IN_SCOPE (b, external_scope))
8357 break;
8358 if (b)
8360 tree ext_decl, ext_type;
8361 ext_decl = b->decl;
8362 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8363 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8364 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8365 TREE_TYPE (ext_type)))
8367 current_function_prototype_locus
8368 = DECL_SOURCE_LOCATION (ext_decl);
8369 current_function_prototype_built_in
8370 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8371 current_function_prototype_arg_types
8372 = TYPE_ARG_TYPES (ext_type);
8378 /* Optionally warn of old-fashioned def with no previous prototype. */
8379 if (warn_strict_prototypes
8380 && old_decl != error_mark_node
8381 && !prototype_p (TREE_TYPE (decl1))
8382 && C_DECL_ISNT_PROTOTYPE (old_decl))
8383 warning_at (loc, OPT_Wstrict_prototypes,
8384 "function declaration isn%'t a prototype");
8385 /* Optionally warn of any global def with no previous prototype. */
8386 else if (warn_missing_prototypes
8387 && old_decl != error_mark_node
8388 && TREE_PUBLIC (decl1)
8389 && !MAIN_NAME_P (DECL_NAME (decl1))
8390 && C_DECL_ISNT_PROTOTYPE (old_decl)
8391 && !DECL_DECLARED_INLINE_P (decl1))
8392 warning_at (loc, OPT_Wmissing_prototypes,
8393 "no previous prototype for %qD", decl1);
8394 /* Optionally warn of any def with no previous prototype
8395 if the function has already been used. */
8396 else if (warn_missing_prototypes
8397 && old_decl != 0
8398 && old_decl != error_mark_node
8399 && TREE_USED (old_decl)
8400 && !prototype_p (TREE_TYPE (old_decl)))
8401 warning_at (loc, OPT_Wmissing_prototypes,
8402 "%qD was used with no prototype before its definition", decl1);
8403 /* Optionally warn of any global def with no previous declaration. */
8404 else if (warn_missing_declarations
8405 && TREE_PUBLIC (decl1)
8406 && old_decl == 0
8407 && !MAIN_NAME_P (DECL_NAME (decl1))
8408 && !DECL_DECLARED_INLINE_P (decl1))
8409 warning_at (loc, OPT_Wmissing_declarations,
8410 "no previous declaration for %qD",
8411 decl1);
8412 /* Optionally warn of any def with no previous declaration
8413 if the function has already been used. */
8414 else if (warn_missing_declarations
8415 && old_decl != 0
8416 && old_decl != error_mark_node
8417 && TREE_USED (old_decl)
8418 && C_DECL_IMPLICIT (old_decl))
8419 warning_at (loc, OPT_Wmissing_declarations,
8420 "%qD was used with no declaration before its definition", decl1);
8422 /* This function exists in static storage.
8423 (This does not mean `static' in the C sense!) */
8424 TREE_STATIC (decl1) = 1;
8426 /* This is the earliest point at which we might know the assembler
8427 name of the function. Thus, if it's set before this, die horribly. */
8428 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8430 /* If #pragma weak was used, mark the decl weak now. */
8431 if (current_scope == file_scope)
8432 maybe_apply_pragma_weak (decl1);
8434 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8435 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8437 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8438 != integer_type_node)
8439 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8440 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8441 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8442 decl1);
8444 check_main_parameter_types (decl1);
8446 if (!TREE_PUBLIC (decl1))
8447 pedwarn (loc, OPT_Wmain,
8448 "%qD is normally a non-static function", decl1);
8451 /* Record the decl so that the function name is defined.
8452 If we already have a decl for this name, and it is a FUNCTION_DECL,
8453 use the old decl. */
8455 current_function_decl = pushdecl (decl1);
8457 push_scope ();
8458 declare_parm_level ();
8460 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8461 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8462 DECL_ARTIFICIAL (resdecl) = 1;
8463 DECL_IGNORED_P (resdecl) = 1;
8464 DECL_RESULT (current_function_decl) = resdecl;
8466 start_fname_decls ();
8468 return 1;
8471 /* Subroutine of store_parm_decls which handles new-style function
8472 definitions (prototype format). The parms already have decls, so we
8473 need only record them as in effect and complain if any redundant
8474 old-style parm decls were written. */
8475 static void
8476 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8478 tree decl;
8479 c_arg_tag *tag;
8480 unsigned ix;
8482 if (current_scope->bindings)
8484 error_at (DECL_SOURCE_LOCATION (fndecl),
8485 "old-style parameter declarations in prototyped "
8486 "function definition");
8488 /* Get rid of the old-style declarations. */
8489 pop_scope ();
8490 push_scope ();
8492 /* Don't issue this warning for nested functions, and don't issue this
8493 warning if we got here because ARG_INFO_TYPES was error_mark_node
8494 (this happens when a function definition has just an ellipsis in
8495 its parameter list). */
8496 else if (!in_system_header_at (input_location)
8497 && !current_function_scope
8498 && arg_info->types != error_mark_node)
8499 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8500 "traditional C rejects ISO C style function definitions");
8502 /* Now make all the parameter declarations visible in the function body.
8503 We can bypass most of the grunt work of pushdecl. */
8504 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8506 DECL_CONTEXT (decl) = current_function_decl;
8507 if (DECL_NAME (decl))
8509 bind (DECL_NAME (decl), decl, current_scope,
8510 /*invisible=*/false, /*nested=*/false,
8511 UNKNOWN_LOCATION);
8512 if (!TREE_USED (decl))
8513 warn_if_shadowing (decl);
8515 else
8516 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8519 /* Record the parameter list in the function declaration. */
8520 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8522 /* Now make all the ancillary declarations visible, likewise. */
8523 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8525 DECL_CONTEXT (decl) = current_function_decl;
8526 if (DECL_NAME (decl))
8527 bind (DECL_NAME (decl), decl, current_scope,
8528 /*invisible=*/false,
8529 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8530 UNKNOWN_LOCATION);
8533 /* And all the tag declarations. */
8534 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8535 if (tag->id)
8536 bind (tag->id, tag->type, current_scope,
8537 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8540 /* Subroutine of store_parm_decls which handles old-style function
8541 definitions (separate parameter list and declarations). */
8543 static void
8544 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8546 struct c_binding *b;
8547 tree parm, decl, last;
8548 tree parmids = arg_info->parms;
8549 hash_set<tree> seen_args;
8551 if (!in_system_header_at (input_location))
8552 warning_at (DECL_SOURCE_LOCATION (fndecl),
8553 OPT_Wold_style_definition, "old-style function definition");
8555 /* Match each formal parameter name with its declaration. Save each
8556 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8557 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8559 if (TREE_VALUE (parm) == 0)
8561 error_at (DECL_SOURCE_LOCATION (fndecl),
8562 "parameter name missing from parameter list");
8563 TREE_PURPOSE (parm) = 0;
8564 continue;
8567 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8568 if (b && B_IN_CURRENT_SCOPE (b))
8570 decl = b->decl;
8571 /* Skip erroneous parameters. */
8572 if (decl == error_mark_node)
8573 continue;
8574 /* If we got something other than a PARM_DECL it is an error. */
8575 if (TREE_CODE (decl) != PARM_DECL)
8576 error_at (DECL_SOURCE_LOCATION (decl),
8577 "%qD declared as a non-parameter", decl);
8578 /* If the declaration is already marked, we have a duplicate
8579 name. Complain and ignore the duplicate. */
8580 else if (seen_args.contains (decl))
8582 error_at (DECL_SOURCE_LOCATION (decl),
8583 "multiple parameters named %qD", decl);
8584 TREE_PURPOSE (parm) = 0;
8585 continue;
8587 /* If the declaration says "void", complain and turn it into
8588 an int. */
8589 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8591 error_at (DECL_SOURCE_LOCATION (decl),
8592 "parameter %qD declared with void type", decl);
8593 TREE_TYPE (decl) = integer_type_node;
8594 DECL_ARG_TYPE (decl) = integer_type_node;
8595 layout_decl (decl, 0);
8597 warn_if_shadowing (decl);
8599 /* If no declaration found, default to int. */
8600 else
8602 /* FIXME diagnostics: This should be the location of the argument,
8603 not the FNDECL. E.g., for an old-style declaration
8605 int f10(v) { blah; }
8607 We should use the location of the V, not the F10.
8608 Unfortunately, the V is an IDENTIFIER_NODE which has no
8609 location. In the future we need locations for c_arg_info
8610 entries.
8612 See gcc.dg/Wshadow-3.c for an example of this problem. */
8613 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8614 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8615 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8616 pushdecl (decl);
8617 warn_if_shadowing (decl);
8619 if (flag_isoc99)
8620 pedwarn (DECL_SOURCE_LOCATION (decl),
8621 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8622 decl);
8623 else
8624 warning_at (DECL_SOURCE_LOCATION (decl),
8625 OPT_Wmissing_parameter_type,
8626 "type of %qD defaults to %<int%>", decl);
8629 TREE_PURPOSE (parm) = decl;
8630 seen_args.add (decl);
8633 /* Now examine the parms chain for incomplete declarations
8634 and declarations with no corresponding names. */
8636 for (b = current_scope->bindings; b; b = b->prev)
8638 parm = b->decl;
8639 if (TREE_CODE (parm) != PARM_DECL)
8640 continue;
8642 if (TREE_TYPE (parm) != error_mark_node
8643 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8645 error_at (DECL_SOURCE_LOCATION (parm),
8646 "parameter %qD has incomplete type", parm);
8647 TREE_TYPE (parm) = error_mark_node;
8650 if (!seen_args.contains (parm))
8652 error_at (DECL_SOURCE_LOCATION (parm),
8653 "declaration for parameter %qD but no such parameter",
8654 parm);
8656 /* Pretend the parameter was not missing.
8657 This gets us to a standard state and minimizes
8658 further error messages. */
8659 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8663 /* Chain the declarations together in the order of the list of
8664 names. Store that chain in the function decl, replacing the
8665 list of names. Update the current scope to match. */
8666 DECL_ARGUMENTS (fndecl) = 0;
8668 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8669 if (TREE_PURPOSE (parm))
8670 break;
8671 if (parm && TREE_PURPOSE (parm))
8673 last = TREE_PURPOSE (parm);
8674 DECL_ARGUMENTS (fndecl) = last;
8676 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8677 if (TREE_PURPOSE (parm))
8679 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8680 last = TREE_PURPOSE (parm);
8682 DECL_CHAIN (last) = 0;
8685 /* If there was a previous prototype,
8686 set the DECL_ARG_TYPE of each argument according to
8687 the type previously specified, and report any mismatches. */
8689 if (current_function_prototype_arg_types)
8691 tree type;
8692 for (parm = DECL_ARGUMENTS (fndecl),
8693 type = current_function_prototype_arg_types;
8694 parm || (type && TREE_VALUE (type) != error_mark_node
8695 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8696 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8698 if (parm == 0 || type == 0
8699 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8701 if (current_function_prototype_built_in)
8702 warning_at (DECL_SOURCE_LOCATION (fndecl),
8703 0, "number of arguments doesn%'t match "
8704 "built-in prototype");
8705 else
8707 /* FIXME diagnostics: This should be the location of
8708 FNDECL, but there is bug when a prototype is
8709 declared inside function context, but defined
8710 outside of it (e.g., gcc.dg/pr15698-2.c). In
8711 which case FNDECL gets the location of the
8712 prototype, not the definition. */
8713 error_at (input_location,
8714 "number of arguments doesn%'t match prototype");
8716 error_at (current_function_prototype_locus,
8717 "prototype declaration");
8719 break;
8721 /* Type for passing arg must be consistent with that
8722 declared for the arg. ISO C says we take the unqualified
8723 type for parameters declared with qualified type. */
8724 if (TREE_TYPE (parm) != error_mark_node
8725 && TREE_TYPE (type) != error_mark_node
8726 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8727 != TYPE_ATOMIC (TREE_VALUE (type)))
8728 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8729 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8731 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8732 == TYPE_ATOMIC (TREE_VALUE (type)))
8733 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8734 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8736 /* Adjust argument to match prototype. E.g. a previous
8737 `int foo(float);' prototype causes
8738 `int foo(x) float x; {...}' to be treated like
8739 `int foo(float x) {...}'. This is particularly
8740 useful for argument types like uid_t. */
8741 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8743 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8744 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8745 && TYPE_PRECISION (TREE_TYPE (parm))
8746 < TYPE_PRECISION (integer_type_node))
8747 DECL_ARG_TYPE (parm)
8748 = c_type_promotes_to (TREE_TYPE (parm));
8750 /* ??? Is it possible to get here with a
8751 built-in prototype or will it always have
8752 been diagnosed as conflicting with an
8753 old-style definition and discarded? */
8754 if (current_function_prototype_built_in)
8755 warning_at (DECL_SOURCE_LOCATION (parm),
8756 OPT_Wpedantic, "promoted argument %qD "
8757 "doesn%'t match built-in prototype", parm);
8758 else
8760 pedwarn (DECL_SOURCE_LOCATION (parm),
8761 OPT_Wpedantic, "promoted argument %qD "
8762 "doesn%'t match prototype", parm);
8763 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8764 "prototype declaration");
8767 else
8769 if (current_function_prototype_built_in)
8770 warning_at (DECL_SOURCE_LOCATION (parm),
8771 0, "argument %qD doesn%'t match "
8772 "built-in prototype", parm);
8773 else
8775 error_at (DECL_SOURCE_LOCATION (parm),
8776 "argument %qD doesn%'t match prototype", parm);
8777 error_at (current_function_prototype_locus,
8778 "prototype declaration");
8783 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8786 /* Otherwise, create a prototype that would match. */
8788 else
8790 tree actual = 0, last = 0, type;
8792 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8794 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8795 if (last)
8796 TREE_CHAIN (last) = type;
8797 else
8798 actual = type;
8799 last = type;
8801 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8802 if (last)
8803 TREE_CHAIN (last) = type;
8804 else
8805 actual = type;
8807 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8808 of the type of this function, but we need to avoid having this
8809 affect the types of other similarly-typed functions, so we must
8810 first force the generation of an identical (but separate) type
8811 node for the relevant function type. The new node we create
8812 will be a variant of the main variant of the original function
8813 type. */
8815 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8817 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8821 /* Store parameter declarations passed in ARG_INFO into the current
8822 function declaration. */
8824 void
8825 store_parm_decls_from (struct c_arg_info *arg_info)
8827 current_function_arg_info = arg_info;
8828 store_parm_decls ();
8831 /* Called by walk_tree to look for and update context-less labels. */
8833 static tree
8834 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
8836 if (TREE_CODE (*tp) == LABEL_EXPR
8837 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
8839 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
8840 *walk_subtrees = 0;
8843 return NULL_TREE;
8846 /* Store the parameter declarations into the current function declaration.
8847 This is called after parsing the parameter declarations, before
8848 digesting the body of the function.
8850 For an old-style definition, construct a prototype out of the old-style
8851 parameter declarations and inject it into the function's type. */
8853 void
8854 store_parm_decls (void)
8856 tree fndecl = current_function_decl;
8857 bool proto;
8859 /* The argument information block for FNDECL. */
8860 struct c_arg_info *arg_info = current_function_arg_info;
8861 current_function_arg_info = 0;
8863 /* True if this definition is written with a prototype. Note:
8864 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8865 list in a function definition as equivalent to (void) -- an
8866 empty argument list specifies the function has no parameters,
8867 but only (void) sets up a prototype for future calls. */
8868 proto = arg_info->types != 0;
8870 if (proto)
8871 store_parm_decls_newstyle (fndecl, arg_info);
8872 else
8873 store_parm_decls_oldstyle (fndecl, arg_info);
8875 /* The next call to push_scope will be a function body. */
8877 next_is_function_body = true;
8879 /* Write a record describing this function definition to the prototypes
8880 file (if requested). */
8882 gen_aux_info_record (fndecl, 1, 0, proto);
8884 /* Initialize the RTL code for the function. */
8885 allocate_struct_function (fndecl, false);
8887 if (warn_unused_local_typedefs)
8888 cfun->language = ggc_cleared_alloc<language_function> ();
8890 /* Begin the statement tree for this function. */
8891 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8893 /* ??? Insert the contents of the pending sizes list into the function
8894 to be evaluated. The only reason left to have this is
8895 void foo(int n, int array[n++])
8896 because we throw away the array type in favor of a pointer type, and
8897 thus won't naturally see the SAVE_EXPR containing the increment. All
8898 other pending sizes would be handled by gimplify_parameters. */
8899 if (arg_info->pending_sizes)
8901 /* In very special circumstances, e.g. for code like
8902 _Atomic int i = 5;
8903 void f (int a[i += 2]) {}
8904 we need to execute the atomic assignment on function entry.
8905 But in this case, it is not just a straight store, it has the
8906 op= form, which means that build_atomic_assign has generated
8907 gotos, labels, etc. Because at that time the function decl
8908 for F has not been created yet, those labels do not have any
8909 function context. But we have the fndecl now, so update the
8910 labels accordingly. gimplify_expr would crash otherwise. */
8911 walk_tree_without_duplicates (&arg_info->pending_sizes,
8912 set_labels_context_r, fndecl);
8913 add_stmt (arg_info->pending_sizes);
8917 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8918 c_finish_omp_declare_simd for function prototypes. No diagnostics
8919 should be done. */
8921 void
8922 temp_store_parm_decls (tree fndecl, tree parms)
8924 push_scope ();
8925 for (tree p = parms; p; p = DECL_CHAIN (p))
8927 DECL_CONTEXT (p) = fndecl;
8928 if (DECL_NAME (p))
8929 bind (DECL_NAME (p), p, current_scope,
8930 /*invisible=*/false, /*nested=*/false,
8931 UNKNOWN_LOCATION);
8935 /* Undo what temp_store_parm_decls did. */
8937 void
8938 temp_pop_parm_decls (void)
8940 /* Clear all bindings in this temporary scope, so that
8941 pop_scope doesn't create a BLOCK. */
8942 struct c_binding *b = current_scope->bindings;
8943 current_scope->bindings = NULL;
8944 for (; b; b = free_binding_and_advance (b))
8946 gcc_assert (TREE_CODE (b->decl) == PARM_DECL);
8947 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8948 I_SYMBOL_BINDING (b->id) = b->shadowed;
8949 if (b->shadowed && b->shadowed->u.type)
8950 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
8952 pop_scope ();
8956 /* Finish up a function declaration and compile that function
8957 all the way to assembler language output. Then free the storage
8958 for the function definition.
8960 This is called after parsing the body of the function definition. */
8962 void
8963 finish_function (void)
8965 tree fndecl = current_function_decl;
8967 if (c_dialect_objc ())
8968 objc_finish_function ();
8970 if (TREE_CODE (fndecl) == FUNCTION_DECL
8971 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8973 tree args = DECL_ARGUMENTS (fndecl);
8974 for (; args; args = DECL_CHAIN (args))
8976 tree type = TREE_TYPE (args);
8977 if (INTEGRAL_TYPE_P (type)
8978 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8979 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
8983 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8984 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8986 /* Must mark the RESULT_DECL as being in this function. */
8988 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8989 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8991 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8992 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8993 == integer_type_node && flag_isoc99)
8995 /* Hack. We don't want the middle-end to warn that this return
8996 is unreachable, so we mark its location as special. Using
8997 UNKNOWN_LOCATION has the problem that it gets clobbered in
8998 annotate_one_with_locus. A cleaner solution might be to
8999 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9001 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9004 /* Tie off the statement tree for this function. */
9005 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9007 /* If the function has _Cilk_spawn in front of a function call inside it
9008 i.e. it is a spawning function, then add the appropriate Cilk plus
9009 functions inside. */
9010 if (fn_contains_cilk_spawn_p (cfun))
9011 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9013 finish_fname_decls ();
9015 /* Complain if there's just no return statement. */
9016 if (warn_return_type
9017 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9018 && !current_function_returns_value && !current_function_returns_null
9019 /* Don't complain if we are no-return. */
9020 && !current_function_returns_abnormally
9021 /* Don't complain if we are declared noreturn. */
9022 && !TREE_THIS_VOLATILE (fndecl)
9023 /* Don't warn for main(). */
9024 && !MAIN_NAME_P (DECL_NAME (fndecl))
9025 /* Or if they didn't actually specify a return type. */
9026 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9027 /* Normally, with -Wreturn-type, flow will complain, but we might
9028 optimize out static functions. */
9029 && !TREE_PUBLIC (fndecl))
9031 warning (OPT_Wreturn_type,
9032 "no return statement in function returning non-void");
9033 TREE_NO_WARNING (fndecl) = 1;
9036 /* Complain about parameters that are only set, but never otherwise used. */
9037 if (warn_unused_but_set_parameter)
9039 tree decl;
9041 for (decl = DECL_ARGUMENTS (fndecl);
9042 decl;
9043 decl = DECL_CHAIN (decl))
9044 if (TREE_USED (decl)
9045 && TREE_CODE (decl) == PARM_DECL
9046 && !DECL_READ_P (decl)
9047 && DECL_NAME (decl)
9048 && !DECL_ARTIFICIAL (decl)
9049 && !TREE_NO_WARNING (decl))
9050 warning_at (DECL_SOURCE_LOCATION (decl),
9051 OPT_Wunused_but_set_parameter,
9052 "parameter %qD set but not used", decl);
9055 /* Complain about locally defined typedefs that are not used in this
9056 function. */
9057 maybe_warn_unused_local_typedefs ();
9059 /* Store the end of the function, so that we get good line number
9060 info for the epilogue. */
9061 cfun->function_end_locus = input_location;
9063 /* Finalize the ELF visibility for the function. */
9064 c_determine_visibility (fndecl);
9066 /* For GNU C extern inline functions disregard inline limits. */
9067 if (DECL_EXTERNAL (fndecl)
9068 && DECL_DECLARED_INLINE_P (fndecl))
9069 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9071 /* Genericize before inlining. Delay genericizing nested functions
9072 until their parent function is genericized. Since finalizing
9073 requires GENERIC, delay that as well. */
9075 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9076 && !undef_nested_function)
9078 if (!decl_function_context (fndecl))
9080 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9081 c_genericize (fndecl);
9083 /* ??? Objc emits functions after finalizing the compilation unit.
9084 This should be cleaned up later and this conditional removed. */
9085 if (symtab->global_info_ready)
9087 cgraph_node::add_new_function (fndecl, false);
9088 return;
9090 cgraph_node::finalize_function (fndecl, false);
9092 else
9094 /* Register this function with cgraph just far enough to get it
9095 added to our parent's nested function list. Handy, since the
9096 C front end doesn't have such a list. */
9097 (void) cgraph_node::get_create (fndecl);
9101 if (!decl_function_context (fndecl))
9102 undef_nested_function = false;
9104 if (cfun->language != NULL)
9106 ggc_free (cfun->language);
9107 cfun->language = NULL;
9110 /* We're leaving the context of this function, so zap cfun.
9111 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9112 tree_rest_of_compilation. */
9113 set_cfun (NULL);
9114 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9115 current_function_decl = NULL;
9118 /* Check the declarations given in a for-loop for satisfying the C99
9119 constraints. If exactly one such decl is found, return it. LOC is
9120 the location of the opening parenthesis of the for loop. The last
9121 parameter allows you to control the "for loop initial declarations
9122 are only allowed in C99 mode". Normally, you should pass
9123 flag_isoc99 as that parameter. But in some cases (Objective-C
9124 foreach loop, for example) we want to run the checks in this
9125 function even if not in C99 mode, so we allow the caller to turn
9126 off the error about not being in C99 mode.
9129 tree
9130 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9132 struct c_binding *b;
9133 tree one_decl = NULL_TREE;
9134 int n_decls = 0;
9136 if (!turn_off_iso_c99_error)
9138 static bool hint = true;
9139 /* If we get here, declarations have been used in a for loop without
9140 the C99 for loop scope. This doesn't make much sense, so don't
9141 allow it. */
9142 error_at (loc, "%<for%> loop initial declarations "
9143 "are only allowed in C99 or C11 mode");
9144 if (hint)
9146 inform (loc,
9147 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9148 "to compile your code");
9149 hint = false;
9151 return NULL_TREE;
9153 /* C99 subclause 6.8.5 paragraph 3:
9155 [#3] The declaration part of a for statement shall only
9156 declare identifiers for objects having storage class auto or
9157 register.
9159 It isn't clear whether, in this sentence, "identifiers" binds to
9160 "shall only declare" or to "objects" - that is, whether all identifiers
9161 declared must be identifiers for objects, or whether the restriction
9162 only applies to those that are. (A question on this in comp.std.c
9163 in November 2000 received no answer.) We implement the strictest
9164 interpretation, to avoid creating an extension which later causes
9165 problems. */
9167 for (b = current_scope->bindings; b; b = b->prev)
9169 tree id = b->id;
9170 tree decl = b->decl;
9172 if (!id)
9173 continue;
9175 switch (TREE_CODE (decl))
9177 case VAR_DECL:
9179 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9180 if (TREE_STATIC (decl))
9181 error_at (decl_loc,
9182 "declaration of static variable %qD in %<for%> loop "
9183 "initial declaration", decl);
9184 else if (DECL_EXTERNAL (decl))
9185 error_at (decl_loc,
9186 "declaration of %<extern%> variable %qD in %<for%> loop "
9187 "initial declaration", decl);
9189 break;
9191 case RECORD_TYPE:
9192 error_at (loc,
9193 "%<struct %E%> declared in %<for%> loop initial "
9194 "declaration", id);
9195 break;
9196 case UNION_TYPE:
9197 error_at (loc,
9198 "%<union %E%> declared in %<for%> loop initial declaration",
9199 id);
9200 break;
9201 case ENUMERAL_TYPE:
9202 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9203 "initial declaration", id);
9204 break;
9205 default:
9206 error_at (loc, "declaration of non-variable "
9207 "%qD in %<for%> loop initial declaration", decl);
9210 n_decls++;
9211 one_decl = decl;
9214 return n_decls == 1 ? one_decl : NULL_TREE;
9217 /* Save and reinitialize the variables
9218 used during compilation of a C function. */
9220 void
9221 c_push_function_context (void)
9223 struct language_function *p = cfun->language;
9224 /* cfun->language might have been already allocated by the use of
9225 -Wunused-local-typedefs. In that case, just re-use it. */
9226 if (p == NULL)
9227 cfun->language = p = ggc_cleared_alloc<language_function> ();
9229 p->base.x_stmt_tree = c_stmt_tree;
9230 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9231 p->x_break_label = c_break_label;
9232 p->x_cont_label = c_cont_label;
9233 p->x_switch_stack = c_switch_stack;
9234 p->arg_info = current_function_arg_info;
9235 p->returns_value = current_function_returns_value;
9236 p->returns_null = current_function_returns_null;
9237 p->returns_abnormally = current_function_returns_abnormally;
9238 p->warn_about_return_type = warn_about_return_type;
9240 push_function_context ();
9243 /* Restore the variables used during compilation of a C function. */
9245 void
9246 c_pop_function_context (void)
9248 struct language_function *p;
9250 pop_function_context ();
9251 p = cfun->language;
9253 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9254 used to store data throughout the life time of the current cfun,
9255 So don't deallocate it. */
9256 if (!warn_unused_local_typedefs)
9257 cfun->language = NULL;
9259 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9260 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9262 /* Stop pointing to the local nodes about to be freed. */
9263 /* But DECL_INITIAL must remain nonzero so we know this
9264 was an actual function definition. */
9265 DECL_INITIAL (current_function_decl) = error_mark_node;
9266 DECL_ARGUMENTS (current_function_decl) = 0;
9269 c_stmt_tree = p->base.x_stmt_tree;
9270 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9271 c_break_label = p->x_break_label;
9272 c_cont_label = p->x_cont_label;
9273 c_switch_stack = p->x_switch_stack;
9274 current_function_arg_info = p->arg_info;
9275 current_function_returns_value = p->returns_value;
9276 current_function_returns_null = p->returns_null;
9277 current_function_returns_abnormally = p->returns_abnormally;
9278 warn_about_return_type = p->warn_about_return_type;
9281 /* The functions below are required for functionality of doing
9282 function at once processing in the C front end. Currently these
9283 functions are not called from anywhere in the C front end, but as
9284 these changes continue, that will change. */
9286 /* Returns the stmt_tree (if any) to which statements are currently
9287 being added. If there is no active statement-tree, NULL is
9288 returned. */
9290 stmt_tree
9291 current_stmt_tree (void)
9293 return &c_stmt_tree;
9296 /* Return the global value of T as a symbol. */
9298 tree
9299 identifier_global_value (tree t)
9301 struct c_binding *b;
9303 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9304 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9305 return b->decl;
9307 return 0;
9310 /* In C, the only C-linkage public declaration is at file scope. */
9312 tree
9313 c_linkage_bindings (tree name)
9315 return identifier_global_value (name);
9318 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9319 otherwise the name is found in ridpointers from RID_INDEX. */
9321 void
9322 record_builtin_type (enum rid rid_index, const char *name, tree type)
9324 tree id, decl;
9325 if (name == 0)
9326 id = ridpointers[(int) rid_index];
9327 else
9328 id = get_identifier (name);
9329 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9330 pushdecl (decl);
9331 if (debug_hooks->type_decl)
9332 debug_hooks->type_decl (decl, false);
9335 /* Build the void_list_node (void_type_node having been created). */
9336 tree
9337 build_void_list_node (void)
9339 tree t = build_tree_list (NULL_TREE, void_type_node);
9340 return t;
9343 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9345 struct c_parm *
9346 build_c_parm (struct c_declspecs *specs, tree attrs,
9347 struct c_declarator *declarator)
9349 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9350 ret->specs = specs;
9351 ret->attrs = attrs;
9352 ret->declarator = declarator;
9353 return ret;
9356 /* Return a declarator with nested attributes. TARGET is the inner
9357 declarator to which these attributes apply. ATTRS are the
9358 attributes. */
9360 struct c_declarator *
9361 build_attrs_declarator (tree attrs, struct c_declarator *target)
9363 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9364 ret->kind = cdk_attrs;
9365 ret->declarator = target;
9366 ret->u.attrs = attrs;
9367 return ret;
9370 /* Return a declarator for a function with arguments specified by ARGS
9371 and return type specified by TARGET. */
9373 struct c_declarator *
9374 build_function_declarator (struct c_arg_info *args,
9375 struct c_declarator *target)
9377 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9378 ret->kind = cdk_function;
9379 ret->declarator = target;
9380 ret->u.arg_info = args;
9381 return ret;
9384 /* Return a declarator for the identifier IDENT (which may be
9385 NULL_TREE for an abstract declarator). */
9387 struct c_declarator *
9388 build_id_declarator (tree ident)
9390 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9391 ret->kind = cdk_id;
9392 ret->declarator = 0;
9393 ret->u.id = ident;
9394 /* Default value - may get reset to a more precise location. */
9395 ret->id_loc = input_location;
9396 return ret;
9399 /* Return something to represent absolute declarators containing a *.
9400 TARGET is the absolute declarator that the * contains.
9401 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9402 to apply to the pointer type. */
9404 struct c_declarator *
9405 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9406 struct c_declarator *target)
9408 tree attrs;
9409 int quals = 0;
9410 struct c_declarator *itarget = target;
9411 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9412 if (type_quals_attrs)
9414 attrs = type_quals_attrs->attrs;
9415 quals = quals_from_declspecs (type_quals_attrs);
9416 if (attrs != NULL_TREE)
9417 itarget = build_attrs_declarator (attrs, target);
9419 ret->kind = cdk_pointer;
9420 ret->declarator = itarget;
9421 ret->u.pointer_quals = quals;
9422 return ret;
9425 /* Return a pointer to a structure for an empty list of declaration
9426 specifiers. */
9428 struct c_declspecs *
9429 build_null_declspecs (void)
9431 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9432 memset (&ret->locations, 0, cdw_number_of_elements);
9433 ret->type = 0;
9434 ret->expr = 0;
9435 ret->decl_attr = 0;
9436 ret->attrs = 0;
9437 ret->align_log = -1;
9438 ret->typespec_word = cts_none;
9439 ret->storage_class = csc_none;
9440 ret->expr_const_operands = true;
9441 ret->declspecs_seen_p = false;
9442 ret->typespec_kind = ctsk_none;
9443 ret->non_sc_seen_p = false;
9444 ret->typedef_p = false;
9445 ret->explicit_signed_p = false;
9446 ret->deprecated_p = false;
9447 ret->default_int_p = false;
9448 ret->long_p = false;
9449 ret->long_long_p = false;
9450 ret->short_p = false;
9451 ret->signed_p = false;
9452 ret->unsigned_p = false;
9453 ret->complex_p = false;
9454 ret->inline_p = false;
9455 ret->noreturn_p = false;
9456 ret->thread_p = false;
9457 ret->thread_gnu_p = false;
9458 ret->const_p = false;
9459 ret->volatile_p = false;
9460 ret->atomic_p = false;
9461 ret->restrict_p = false;
9462 ret->saturating_p = false;
9463 ret->alignas_p = false;
9464 ret->address_space = ADDR_SPACE_GENERIC;
9465 return ret;
9468 /* Add the address space ADDRSPACE to the declaration specifiers
9469 SPECS, returning SPECS. */
9471 struct c_declspecs *
9472 declspecs_add_addrspace (source_location location,
9473 struct c_declspecs *specs, addr_space_t as)
9475 specs->non_sc_seen_p = true;
9476 specs->declspecs_seen_p = true;
9478 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9479 && specs->address_space != as)
9480 error ("incompatible address space qualifiers %qs and %qs",
9481 c_addr_space_name (as),
9482 c_addr_space_name (specs->address_space));
9483 else
9485 specs->address_space = as;
9486 specs->locations[cdw_address_space] = location;
9488 return specs;
9491 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9492 returning SPECS. */
9494 struct c_declspecs *
9495 declspecs_add_qual (source_location loc,
9496 struct c_declspecs *specs, tree qual)
9498 enum rid i;
9499 bool dupe = false;
9500 specs->non_sc_seen_p = true;
9501 specs->declspecs_seen_p = true;
9502 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9503 && C_IS_RESERVED_WORD (qual));
9504 i = C_RID_CODE (qual);
9505 switch (i)
9507 case RID_CONST:
9508 dupe = specs->const_p;
9509 specs->const_p = true;
9510 specs->locations[cdw_const] = loc;
9511 break;
9512 case RID_VOLATILE:
9513 dupe = specs->volatile_p;
9514 specs->volatile_p = true;
9515 specs->locations[cdw_volatile] = loc;
9516 break;
9517 case RID_RESTRICT:
9518 dupe = specs->restrict_p;
9519 specs->restrict_p = true;
9520 specs->locations[cdw_restrict] = loc;
9521 break;
9522 case RID_ATOMIC:
9523 dupe = specs->atomic_p;
9524 specs->atomic_p = true;
9525 break;
9526 default:
9527 gcc_unreachable ();
9529 if (dupe)
9530 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9531 return specs;
9534 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9535 returning SPECS. */
9537 struct c_declspecs *
9538 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9539 struct c_typespec spec)
9541 tree type = spec.spec;
9542 specs->non_sc_seen_p = true;
9543 specs->declspecs_seen_p = true;
9544 specs->typespec_kind = spec.kind;
9545 if (TREE_DEPRECATED (type))
9546 specs->deprecated_p = true;
9548 /* Handle type specifier keywords. */
9549 if (TREE_CODE (type) == IDENTIFIER_NODE
9550 && C_IS_RESERVED_WORD (type)
9551 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9553 enum rid i = C_RID_CODE (type);
9554 if (specs->type)
9556 error_at (loc, "two or more data types in declaration specifiers");
9557 return specs;
9559 if ((int) i <= (int) RID_LAST_MODIFIER)
9561 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9562 bool dupe = false;
9563 switch (i)
9565 case RID_LONG:
9566 if (specs->long_long_p)
9568 error_at (loc, "%<long long long%> is too long for GCC");
9569 break;
9571 if (specs->long_p)
9573 if (specs->typespec_word == cts_double)
9575 error_at (loc,
9576 ("both %<long long%> and %<double%> in "
9577 "declaration specifiers"));
9578 break;
9580 pedwarn_c90 (loc, OPT_Wlong_long,
9581 "ISO C90 does not support %<long long%>");
9582 specs->long_long_p = 1;
9583 specs->locations[cdw_long_long] = loc;
9584 break;
9586 if (specs->short_p)
9587 error_at (loc,
9588 ("both %<long%> and %<short%> in "
9589 "declaration specifiers"));
9590 else if (specs->typespec_word == cts_auto_type)
9591 error_at (loc,
9592 ("both %<long%> and %<__auto_type%> in "
9593 "declaration specifiers"));
9594 else if (specs->typespec_word == cts_void)
9595 error_at (loc,
9596 ("both %<long%> and %<void%> in "
9597 "declaration specifiers"));
9598 else if (specs->typespec_word == cts_int_n)
9599 error_at (loc,
9600 ("both %<long%> and %<__int%d%> in "
9601 "declaration specifiers"),
9602 int_n_data[specs->int_n_idx].bitsize);
9603 else if (specs->typespec_word == cts_bool)
9604 error_at (loc,
9605 ("both %<long%> and %<_Bool%> in "
9606 "declaration specifiers"));
9607 else if (specs->typespec_word == cts_char)
9608 error_at (loc,
9609 ("both %<long%> and %<char%> in "
9610 "declaration specifiers"));
9611 else if (specs->typespec_word == cts_float)
9612 error_at (loc,
9613 ("both %<long%> and %<float%> in "
9614 "declaration specifiers"));
9615 else if (specs->typespec_word == cts_dfloat32)
9616 error_at (loc,
9617 ("both %<long%> and %<_Decimal32%> in "
9618 "declaration specifiers"));
9619 else if (specs->typespec_word == cts_dfloat64)
9620 error_at (loc,
9621 ("both %<long%> and %<_Decimal64%> in "
9622 "declaration specifiers"));
9623 else if (specs->typespec_word == cts_dfloat128)
9624 error_at (loc,
9625 ("both %<long%> and %<_Decimal128%> in "
9626 "declaration specifiers"));
9627 else
9629 specs->long_p = true;
9630 specs->locations[cdw_long] = loc;
9632 break;
9633 case RID_SHORT:
9634 dupe = specs->short_p;
9635 if (specs->long_p)
9636 error_at (loc,
9637 ("both %<long%> and %<short%> in "
9638 "declaration specifiers"));
9639 else if (specs->typespec_word == cts_auto_type)
9640 error_at (loc,
9641 ("both %<short%> and %<__auto_type%> in "
9642 "declaration specifiers"));
9643 else if (specs->typespec_word == cts_void)
9644 error_at (loc,
9645 ("both %<short%> and %<void%> in "
9646 "declaration specifiers"));
9647 else if (specs->typespec_word == cts_int_n)
9648 error_at (loc,
9649 ("both %<short%> and %<__int%d%> in "
9650 "declaration specifiers"),
9651 int_n_data[specs->int_n_idx].bitsize);
9652 else if (specs->typespec_word == cts_bool)
9653 error_at (loc,
9654 ("both %<short%> and %<_Bool%> in "
9655 "declaration specifiers"));
9656 else if (specs->typespec_word == cts_char)
9657 error_at (loc,
9658 ("both %<short%> and %<char%> in "
9659 "declaration specifiers"));
9660 else if (specs->typespec_word == cts_float)
9661 error_at (loc,
9662 ("both %<short%> and %<float%> in "
9663 "declaration specifiers"));
9664 else if (specs->typespec_word == cts_double)
9665 error_at (loc,
9666 ("both %<short%> and %<double%> in "
9667 "declaration specifiers"));
9668 else if (specs->typespec_word == cts_dfloat32)
9669 error_at (loc,
9670 ("both %<short%> and %<_Decimal32%> in "
9671 "declaration specifiers"));
9672 else if (specs->typespec_word == cts_dfloat64)
9673 error_at (loc,
9674 ("both %<short%> and %<_Decimal64%> in "
9675 "declaration specifiers"));
9676 else if (specs->typespec_word == cts_dfloat128)
9677 error_at (loc,
9678 ("both %<short%> and %<_Decimal128%> in "
9679 "declaration specifiers"));
9680 else
9682 specs->short_p = true;
9683 specs->locations[cdw_short] = loc;
9685 break;
9686 case RID_SIGNED:
9687 dupe = specs->signed_p;
9688 if (specs->unsigned_p)
9689 error_at (loc,
9690 ("both %<signed%> and %<unsigned%> in "
9691 "declaration specifiers"));
9692 else if (specs->typespec_word == cts_auto_type)
9693 error_at (loc,
9694 ("both %<signed%> and %<__auto_type%> in "
9695 "declaration specifiers"));
9696 else if (specs->typespec_word == cts_void)
9697 error_at (loc,
9698 ("both %<signed%> and %<void%> in "
9699 "declaration specifiers"));
9700 else if (specs->typespec_word == cts_bool)
9701 error_at (loc,
9702 ("both %<signed%> and %<_Bool%> in "
9703 "declaration specifiers"));
9704 else if (specs->typespec_word == cts_float)
9705 error_at (loc,
9706 ("both %<signed%> and %<float%> in "
9707 "declaration specifiers"));
9708 else if (specs->typespec_word == cts_double)
9709 error_at (loc,
9710 ("both %<signed%> and %<double%> in "
9711 "declaration specifiers"));
9712 else if (specs->typespec_word == cts_dfloat32)
9713 error_at (loc,
9714 ("both %<signed%> and %<_Decimal32%> in "
9715 "declaration specifiers"));
9716 else if (specs->typespec_word == cts_dfloat64)
9717 error_at (loc,
9718 ("both %<signed%> and %<_Decimal64%> in "
9719 "declaration specifiers"));
9720 else if (specs->typespec_word == cts_dfloat128)
9721 error_at (loc,
9722 ("both %<signed%> and %<_Decimal128%> in "
9723 "declaration specifiers"));
9724 else
9726 specs->signed_p = true;
9727 specs->locations[cdw_signed] = loc;
9729 break;
9730 case RID_UNSIGNED:
9731 dupe = specs->unsigned_p;
9732 if (specs->signed_p)
9733 error_at (loc,
9734 ("both %<signed%> and %<unsigned%> in "
9735 "declaration specifiers"));
9736 else if (specs->typespec_word == cts_auto_type)
9737 error_at (loc,
9738 ("both %<unsigned%> and %<__auto_type%> in "
9739 "declaration specifiers"));
9740 else if (specs->typespec_word == cts_void)
9741 error_at (loc,
9742 ("both %<unsigned%> and %<void%> in "
9743 "declaration specifiers"));
9744 else if (specs->typespec_word == cts_bool)
9745 error_at (loc,
9746 ("both %<unsigned%> and %<_Bool%> in "
9747 "declaration specifiers"));
9748 else if (specs->typespec_word == cts_float)
9749 error_at (loc,
9750 ("both %<unsigned%> and %<float%> in "
9751 "declaration specifiers"));
9752 else if (specs->typespec_word == cts_double)
9753 error_at (loc,
9754 ("both %<unsigned%> and %<double%> in "
9755 "declaration specifiers"));
9756 else if (specs->typespec_word == cts_dfloat32)
9757 error_at (loc,
9758 ("both %<unsigned%> and %<_Decimal32%> in "
9759 "declaration specifiers"));
9760 else if (specs->typespec_word == cts_dfloat64)
9761 error_at (loc,
9762 ("both %<unsigned%> and %<_Decimal64%> in "
9763 "declaration specifiers"));
9764 else if (specs->typespec_word == cts_dfloat128)
9765 error_at (loc,
9766 ("both %<unsigned%> and %<_Decimal128%> in "
9767 "declaration specifiers"));
9768 else
9770 specs->unsigned_p = true;
9771 specs->locations[cdw_unsigned] = loc;
9773 break;
9774 case RID_COMPLEX:
9775 dupe = specs->complex_p;
9776 if (!in_system_header_at (loc))
9777 pedwarn_c90 (loc, OPT_Wpedantic,
9778 "ISO C90 does not support complex types");
9779 if (specs->typespec_word == cts_auto_type)
9780 error_at (loc,
9781 ("both %<complex%> and %<__auto_type%> in "
9782 "declaration specifiers"));
9783 else if (specs->typespec_word == cts_void)
9784 error_at (loc,
9785 ("both %<complex%> and %<void%> in "
9786 "declaration specifiers"));
9787 else if (specs->typespec_word == cts_bool)
9788 error_at (loc,
9789 ("both %<complex%> and %<_Bool%> in "
9790 "declaration specifiers"));
9791 else if (specs->typespec_word == cts_dfloat32)
9792 error_at (loc,
9793 ("both %<complex%> and %<_Decimal32%> in "
9794 "declaration specifiers"));
9795 else if (specs->typespec_word == cts_dfloat64)
9796 error_at (loc,
9797 ("both %<complex%> and %<_Decimal64%> in "
9798 "declaration specifiers"));
9799 else if (specs->typespec_word == cts_dfloat128)
9800 error_at (loc,
9801 ("both %<complex%> and %<_Decimal128%> in "
9802 "declaration specifiers"));
9803 else if (specs->typespec_word == cts_fract)
9804 error_at (loc,
9805 ("both %<complex%> and %<_Fract%> in "
9806 "declaration specifiers"));
9807 else if (specs->typespec_word == cts_accum)
9808 error_at (loc,
9809 ("both %<complex%> and %<_Accum%> in "
9810 "declaration specifiers"));
9811 else if (specs->saturating_p)
9812 error_at (loc,
9813 ("both %<complex%> and %<_Sat%> in "
9814 "declaration specifiers"));
9815 else
9817 specs->complex_p = true;
9818 specs->locations[cdw_complex] = loc;
9820 break;
9821 case RID_SAT:
9822 dupe = specs->saturating_p;
9823 pedwarn (loc, OPT_Wpedantic,
9824 "ISO C does not support saturating types");
9825 if (specs->typespec_word == cts_int_n)
9827 error_at (loc,
9828 ("both %<_Sat%> and %<__int%d%> in "
9829 "declaration specifiers"),
9830 int_n_data[specs->int_n_idx].bitsize);
9832 else if (specs->typespec_word == cts_auto_type)
9833 error_at (loc,
9834 ("both %<_Sat%> and %<__auto_type%> in "
9835 "declaration specifiers"));
9836 else if (specs->typespec_word == cts_void)
9837 error_at (loc,
9838 ("both %<_Sat%> and %<void%> in "
9839 "declaration specifiers"));
9840 else if (specs->typespec_word == cts_bool)
9841 error_at (loc,
9842 ("both %<_Sat%> and %<_Bool%> in "
9843 "declaration specifiers"));
9844 else if (specs->typespec_word == cts_char)
9845 error_at (loc,
9846 ("both %<_Sat%> and %<char%> in "
9847 "declaration specifiers"));
9848 else if (specs->typespec_word == cts_int)
9849 error_at (loc,
9850 ("both %<_Sat%> and %<int%> in "
9851 "declaration specifiers"));
9852 else if (specs->typespec_word == cts_float)
9853 error_at (loc,
9854 ("both %<_Sat%> and %<float%> in "
9855 "declaration specifiers"));
9856 else if (specs->typespec_word == cts_double)
9857 error_at (loc,
9858 ("both %<_Sat%> and %<double%> in "
9859 "declaration specifiers"));
9860 else if (specs->typespec_word == cts_dfloat32)
9861 error_at (loc,
9862 ("both %<_Sat%> and %<_Decimal32%> in "
9863 "declaration specifiers"));
9864 else if (specs->typespec_word == cts_dfloat64)
9865 error_at (loc,
9866 ("both %<_Sat%> and %<_Decimal64%> in "
9867 "declaration specifiers"));
9868 else if (specs->typespec_word == cts_dfloat128)
9869 error_at (loc,
9870 ("both %<_Sat%> and %<_Decimal128%> in "
9871 "declaration specifiers"));
9872 else if (specs->complex_p)
9873 error_at (loc,
9874 ("both %<_Sat%> and %<complex%> in "
9875 "declaration specifiers"));
9876 else
9878 specs->saturating_p = true;
9879 specs->locations[cdw_saturating] = loc;
9881 break;
9882 default:
9883 gcc_unreachable ();
9886 if (dupe)
9887 error_at (loc, "duplicate %qE", type);
9889 return specs;
9891 else
9893 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9894 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
9895 "__auto_type". */
9896 if (specs->typespec_word != cts_none)
9898 error_at (loc,
9899 "two or more data types in declaration specifiers");
9900 return specs;
9902 switch (i)
9904 case RID_AUTO_TYPE:
9905 if (specs->long_p)
9906 error_at (loc,
9907 ("both %<long%> and %<__auto_type%> in "
9908 "declaration specifiers"));
9909 else if (specs->short_p)
9910 error_at (loc,
9911 ("both %<short%> and %<__auto_type%> in "
9912 "declaration specifiers"));
9913 else if (specs->signed_p)
9914 error_at (loc,
9915 ("both %<signed%> and %<__auto_type%> in "
9916 "declaration specifiers"));
9917 else if (specs->unsigned_p)
9918 error_at (loc,
9919 ("both %<unsigned%> and %<__auto_type%> in "
9920 "declaration specifiers"));
9921 else if (specs->complex_p)
9922 error_at (loc,
9923 ("both %<complex%> and %<__auto_type%> in "
9924 "declaration specifiers"));
9925 else if (specs->saturating_p)
9926 error_at (loc,
9927 ("both %<_Sat%> and %<__auto_type%> in "
9928 "declaration specifiers"));
9929 else
9931 specs->typespec_word = cts_auto_type;
9932 specs->locations[cdw_typespec] = loc;
9934 return specs;
9935 case RID_INT_N_0:
9936 case RID_INT_N_1:
9937 case RID_INT_N_2:
9938 case RID_INT_N_3:
9939 specs->int_n_idx = i - RID_INT_N_0;
9940 if (!in_system_header_at (input_location))
9941 pedwarn (loc, OPT_Wpedantic,
9942 "ISO C does not support %<__int%d%> types",
9943 int_n_data[specs->int_n_idx].bitsize);
9945 if (specs->long_p)
9946 error_at (loc,
9947 ("both %<__int%d%> and %<long%> in "
9948 "declaration specifiers"),
9949 int_n_data[specs->int_n_idx].bitsize);
9950 else if (specs->saturating_p)
9951 error_at (loc,
9952 ("both %<_Sat%> and %<__int%d%> in "
9953 "declaration specifiers"),
9954 int_n_data[specs->int_n_idx].bitsize);
9955 else if (specs->short_p)
9956 error_at (loc,
9957 ("both %<__int%d%> and %<short%> in "
9958 "declaration specifiers"),
9959 int_n_data[specs->int_n_idx].bitsize);
9960 else if (! int_n_enabled_p [specs->int_n_idx])
9961 error_at (loc,
9962 "%<__int%d%> is not supported on this target",
9963 int_n_data[specs->int_n_idx].bitsize);
9964 else
9966 specs->typespec_word = cts_int_n;
9967 specs->locations[cdw_typespec] = loc;
9969 return specs;
9970 case RID_VOID:
9971 if (specs->long_p)
9972 error_at (loc,
9973 ("both %<long%> and %<void%> in "
9974 "declaration specifiers"));
9975 else if (specs->short_p)
9976 error_at (loc,
9977 ("both %<short%> and %<void%> in "
9978 "declaration specifiers"));
9979 else if (specs->signed_p)
9980 error_at (loc,
9981 ("both %<signed%> and %<void%> in "
9982 "declaration specifiers"));
9983 else if (specs->unsigned_p)
9984 error_at (loc,
9985 ("both %<unsigned%> and %<void%> in "
9986 "declaration specifiers"));
9987 else if (specs->complex_p)
9988 error_at (loc,
9989 ("both %<complex%> and %<void%> in "
9990 "declaration specifiers"));
9991 else if (specs->saturating_p)
9992 error_at (loc,
9993 ("both %<_Sat%> and %<void%> in "
9994 "declaration specifiers"));
9995 else
9997 specs->typespec_word = cts_void;
9998 specs->locations[cdw_typespec] = loc;
10000 return specs;
10001 case RID_BOOL:
10002 if (!in_system_header_at (loc))
10003 pedwarn_c90 (loc, OPT_Wpedantic,
10004 "ISO C90 does not support boolean types");
10005 if (specs->long_p)
10006 error_at (loc,
10007 ("both %<long%> and %<_Bool%> in "
10008 "declaration specifiers"));
10009 else if (specs->short_p)
10010 error_at (loc,
10011 ("both %<short%> and %<_Bool%> in "
10012 "declaration specifiers"));
10013 else if (specs->signed_p)
10014 error_at (loc,
10015 ("both %<signed%> and %<_Bool%> in "
10016 "declaration specifiers"));
10017 else if (specs->unsigned_p)
10018 error_at (loc,
10019 ("both %<unsigned%> and %<_Bool%> in "
10020 "declaration specifiers"));
10021 else if (specs->complex_p)
10022 error_at (loc,
10023 ("both %<complex%> and %<_Bool%> in "
10024 "declaration specifiers"));
10025 else if (specs->saturating_p)
10026 error_at (loc,
10027 ("both %<_Sat%> and %<_Bool%> in "
10028 "declaration specifiers"));
10029 else
10031 specs->typespec_word = cts_bool;
10032 specs->locations[cdw_typespec] = loc;
10034 return specs;
10035 case RID_CHAR:
10036 if (specs->long_p)
10037 error_at (loc,
10038 ("both %<long%> and %<char%> in "
10039 "declaration specifiers"));
10040 else if (specs->short_p)
10041 error_at (loc,
10042 ("both %<short%> and %<char%> in "
10043 "declaration specifiers"));
10044 else if (specs->saturating_p)
10045 error_at (loc,
10046 ("both %<_Sat%> and %<char%> in "
10047 "declaration specifiers"));
10048 else
10050 specs->typespec_word = cts_char;
10051 specs->locations[cdw_typespec] = loc;
10053 return specs;
10054 case RID_INT:
10055 if (specs->saturating_p)
10056 error_at (loc,
10057 ("both %<_Sat%> and %<int%> in "
10058 "declaration specifiers"));
10059 else
10061 specs->typespec_word = cts_int;
10062 specs->locations[cdw_typespec] = loc;
10064 return specs;
10065 case RID_FLOAT:
10066 if (specs->long_p)
10067 error_at (loc,
10068 ("both %<long%> and %<float%> in "
10069 "declaration specifiers"));
10070 else if (specs->short_p)
10071 error_at (loc,
10072 ("both %<short%> and %<float%> in "
10073 "declaration specifiers"));
10074 else if (specs->signed_p)
10075 error_at (loc,
10076 ("both %<signed%> and %<float%> in "
10077 "declaration specifiers"));
10078 else if (specs->unsigned_p)
10079 error_at (loc,
10080 ("both %<unsigned%> and %<float%> in "
10081 "declaration specifiers"));
10082 else if (specs->saturating_p)
10083 error_at (loc,
10084 ("both %<_Sat%> and %<float%> in "
10085 "declaration specifiers"));
10086 else
10088 specs->typespec_word = cts_float;
10089 specs->locations[cdw_typespec] = loc;
10091 return specs;
10092 case RID_DOUBLE:
10093 if (specs->long_long_p)
10094 error_at (loc,
10095 ("both %<long long%> and %<double%> in "
10096 "declaration specifiers"));
10097 else if (specs->short_p)
10098 error_at (loc,
10099 ("both %<short%> and %<double%> in "
10100 "declaration specifiers"));
10101 else if (specs->signed_p)
10102 error_at (loc,
10103 ("both %<signed%> and %<double%> in "
10104 "declaration specifiers"));
10105 else if (specs->unsigned_p)
10106 error_at (loc,
10107 ("both %<unsigned%> and %<double%> in "
10108 "declaration specifiers"));
10109 else if (specs->saturating_p)
10110 error_at (loc,
10111 ("both %<_Sat%> and %<double%> in "
10112 "declaration specifiers"));
10113 else
10115 specs->typespec_word = cts_double;
10116 specs->locations[cdw_typespec] = loc;
10118 return specs;
10119 case RID_DFLOAT32:
10120 case RID_DFLOAT64:
10121 case RID_DFLOAT128:
10123 const char *str;
10124 if (i == RID_DFLOAT32)
10125 str = "_Decimal32";
10126 else if (i == RID_DFLOAT64)
10127 str = "_Decimal64";
10128 else
10129 str = "_Decimal128";
10130 if (specs->long_long_p)
10131 error_at (loc,
10132 ("both %<long long%> and %<%s%> in "
10133 "declaration specifiers"),
10134 str);
10135 if (specs->long_p)
10136 error_at (loc,
10137 ("both %<long%> and %<%s%> in "
10138 "declaration specifiers"),
10139 str);
10140 else if (specs->short_p)
10141 error_at (loc,
10142 ("both %<short%> and %<%s%> in "
10143 "declaration specifiers"),
10144 str);
10145 else if (specs->signed_p)
10146 error_at (loc,
10147 ("both %<signed%> and %<%s%> in "
10148 "declaration specifiers"),
10149 str);
10150 else if (specs->unsigned_p)
10151 error_at (loc,
10152 ("both %<unsigned%> and %<%s%> in "
10153 "declaration specifiers"),
10154 str);
10155 else if (specs->complex_p)
10156 error_at (loc,
10157 ("both %<complex%> and %<%s%> in "
10158 "declaration specifiers"),
10159 str);
10160 else if (specs->saturating_p)
10161 error_at (loc,
10162 ("both %<_Sat%> and %<%s%> in "
10163 "declaration specifiers"),
10164 str);
10165 else if (i == RID_DFLOAT32)
10166 specs->typespec_word = cts_dfloat32;
10167 else if (i == RID_DFLOAT64)
10168 specs->typespec_word = cts_dfloat64;
10169 else
10170 specs->typespec_word = cts_dfloat128;
10171 specs->locations[cdw_typespec] = loc;
10173 if (!targetm.decimal_float_supported_p ())
10174 error_at (loc,
10175 ("decimal floating point not supported "
10176 "for this target"));
10177 pedwarn (loc, OPT_Wpedantic,
10178 "ISO C does not support decimal floating point");
10179 return specs;
10180 case RID_FRACT:
10181 case RID_ACCUM:
10183 const char *str;
10184 if (i == RID_FRACT)
10185 str = "_Fract";
10186 else
10187 str = "_Accum";
10188 if (specs->complex_p)
10189 error_at (loc,
10190 ("both %<complex%> and %<%s%> in "
10191 "declaration specifiers"),
10192 str);
10193 else if (i == RID_FRACT)
10194 specs->typespec_word = cts_fract;
10195 else
10196 specs->typespec_word = cts_accum;
10197 specs->locations[cdw_typespec] = loc;
10199 if (!targetm.fixed_point_supported_p ())
10200 error_at (loc,
10201 "fixed-point types not supported for this target");
10202 pedwarn (loc, OPT_Wpedantic,
10203 "ISO C does not support fixed-point types");
10204 return specs;
10205 default:
10206 /* ObjC reserved word "id", handled below. */
10207 break;
10212 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10213 form of ObjC type, cases such as "int" and "long" being handled
10214 above), a TYPE (struct, union, enum and typeof specifiers) or an
10215 ERROR_MARK. In none of these cases may there have previously
10216 been any type specifiers. */
10217 if (specs->type || specs->typespec_word != cts_none
10218 || specs->long_p || specs->short_p || specs->signed_p
10219 || specs->unsigned_p || specs->complex_p)
10220 error_at (loc, "two or more data types in declaration specifiers");
10221 else if (TREE_CODE (type) == TYPE_DECL)
10223 if (TREE_TYPE (type) == error_mark_node)
10224 ; /* Allow the type to default to int to avoid cascading errors. */
10225 else
10227 specs->type = TREE_TYPE (type);
10228 specs->decl_attr = DECL_ATTRIBUTES (type);
10229 specs->typedef_p = true;
10230 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10231 specs->locations[cdw_typedef] = loc;
10233 /* If this typedef name is defined in a struct, then a C++
10234 lookup would return a different value. */
10235 if (warn_cxx_compat
10236 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10237 warning_at (loc, OPT_Wc___compat,
10238 "C++ lookup of %qD would return a field, not a type",
10239 type);
10241 /* If we are parsing a struct, record that a struct field
10242 used a typedef. */
10243 if (warn_cxx_compat && struct_parse_info != NULL)
10244 struct_parse_info->typedefs_seen.safe_push (type);
10247 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10249 tree t = lookup_name (type);
10250 if (!t || TREE_CODE (t) != TYPE_DECL)
10251 error_at (loc, "%qE fails to be a typedef or built in type", type);
10252 else if (TREE_TYPE (t) == error_mark_node)
10254 else
10256 specs->type = TREE_TYPE (t);
10257 specs->locations[cdw_typespec] = loc;
10260 else
10262 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10264 specs->typedef_p = true;
10265 specs->locations[cdw_typedef] = loc;
10266 if (spec.expr)
10268 if (specs->expr)
10269 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10270 specs->expr, spec.expr);
10271 else
10272 specs->expr = spec.expr;
10273 specs->expr_const_operands &= spec.expr_const_operands;
10276 specs->type = type;
10279 return specs;
10282 /* Add the storage class specifier or function specifier SCSPEC to the
10283 declaration specifiers SPECS, returning SPECS. */
10285 struct c_declspecs *
10286 declspecs_add_scspec (source_location loc,
10287 struct c_declspecs *specs,
10288 tree scspec)
10290 enum rid i;
10291 enum c_storage_class n = csc_none;
10292 bool dupe = false;
10293 specs->declspecs_seen_p = true;
10294 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10295 && C_IS_RESERVED_WORD (scspec));
10296 i = C_RID_CODE (scspec);
10297 if (specs->non_sc_seen_p)
10298 warning (OPT_Wold_style_declaration,
10299 "%qE is not at beginning of declaration", scspec);
10300 switch (i)
10302 case RID_INLINE:
10303 /* C99 permits duplicate inline. Although of doubtful utility,
10304 it seems simplest to permit it in gnu89 mode as well, as
10305 there is also little utility in maintaining this as a
10306 difference between gnu89 and C99 inline. */
10307 dupe = false;
10308 specs->inline_p = true;
10309 specs->locations[cdw_inline] = loc;
10310 break;
10311 case RID_NORETURN:
10312 /* Duplicate _Noreturn is permitted. */
10313 dupe = false;
10314 specs->noreturn_p = true;
10315 specs->locations[cdw_noreturn] = loc;
10316 break;
10317 case RID_THREAD:
10318 dupe = specs->thread_p;
10319 if (specs->storage_class == csc_auto)
10320 error ("%qE used with %<auto%>", scspec);
10321 else if (specs->storage_class == csc_register)
10322 error ("%qE used with %<register%>", scspec);
10323 else if (specs->storage_class == csc_typedef)
10324 error ("%qE used with %<typedef%>", scspec);
10325 else
10327 specs->thread_p = true;
10328 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10329 "__thread") == 0);
10330 /* A diagnostic is not required for the use of this
10331 identifier in the implementation namespace; only diagnose
10332 it for the C11 spelling because of existing code using
10333 the other spelling. */
10334 if (!specs->thread_gnu_p)
10336 if (flag_isoc99)
10337 pedwarn_c99 (loc, OPT_Wpedantic,
10338 "ISO C99 does not support %qE", scspec);
10339 else
10340 pedwarn_c99 (loc, OPT_Wpedantic,
10341 "ISO C90 does not support %qE", scspec);
10343 specs->locations[cdw_thread] = loc;
10345 break;
10346 case RID_AUTO:
10347 n = csc_auto;
10348 break;
10349 case RID_EXTERN:
10350 n = csc_extern;
10351 /* Diagnose "__thread extern". */
10352 if (specs->thread_p && specs->thread_gnu_p)
10353 error ("%<__thread%> before %<extern%>");
10354 break;
10355 case RID_REGISTER:
10356 n = csc_register;
10357 break;
10358 case RID_STATIC:
10359 n = csc_static;
10360 /* Diagnose "__thread static". */
10361 if (specs->thread_p && specs->thread_gnu_p)
10362 error ("%<__thread%> before %<static%>");
10363 break;
10364 case RID_TYPEDEF:
10365 n = csc_typedef;
10366 break;
10367 default:
10368 gcc_unreachable ();
10370 if (n != csc_none && n == specs->storage_class)
10371 dupe = true;
10372 if (dupe)
10374 if (i == RID_THREAD)
10375 error ("duplicate %<_Thread_local%> or %<__thread%>");
10376 else
10377 error ("duplicate %qE", scspec);
10379 if (n != csc_none)
10381 if (specs->storage_class != csc_none && n != specs->storage_class)
10383 error ("multiple storage classes in declaration specifiers");
10385 else
10387 specs->storage_class = n;
10388 specs->locations[cdw_storage_class] = loc;
10389 if (n != csc_extern && n != csc_static && specs->thread_p)
10391 error ("%qs used with %qE",
10392 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10393 scspec);
10394 specs->thread_p = false;
10398 return specs;
10401 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10402 returning SPECS. */
10404 struct c_declspecs *
10405 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10407 specs->attrs = chainon (attrs, specs->attrs);
10408 specs->locations[cdw_attributes] = loc;
10409 specs->declspecs_seen_p = true;
10410 return specs;
10413 /* Add an _Alignas specifier (expression ALIGN, or type whose
10414 alignment is ALIGN) to the declaration specifiers SPECS, returning
10415 SPECS. */
10416 struct c_declspecs *
10417 declspecs_add_alignas (source_location loc,
10418 struct c_declspecs *specs, tree align)
10420 int align_log;
10421 specs->alignas_p = true;
10422 specs->locations[cdw_alignas] = loc;
10423 if (align == error_mark_node)
10424 return specs;
10425 align_log = check_user_alignment (align, true);
10426 if (align_log > specs->align_log)
10427 specs->align_log = align_log;
10428 return specs;
10431 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10432 specifiers with any other type specifier to determine the resulting
10433 type. This is where ISO C checks on complex types are made, since
10434 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10435 double". */
10437 struct c_declspecs *
10438 finish_declspecs (struct c_declspecs *specs)
10440 /* If a type was specified as a whole, we have no modifiers and are
10441 done. */
10442 if (specs->type != NULL_TREE)
10444 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10445 && !specs->signed_p && !specs->unsigned_p
10446 && !specs->complex_p);
10448 /* Set a dummy type. */
10449 if (TREE_CODE (specs->type) == ERROR_MARK)
10450 specs->type = integer_type_node;
10451 return specs;
10454 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10455 has been specified, treat it as "int" unless "_Complex" is
10456 present and there are no other specifiers. If we just have
10457 "_Complex", it is equivalent to "_Complex double", but e.g.
10458 "_Complex short" is equivalent to "_Complex short int". */
10459 if (specs->typespec_word == cts_none)
10461 if (specs->saturating_p)
10463 error_at (specs->locations[cdw_saturating],
10464 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10465 if (!targetm.fixed_point_supported_p ())
10466 error_at (specs->locations[cdw_saturating],
10467 "fixed-point types not supported for this target");
10468 specs->typespec_word = cts_fract;
10470 else if (specs->long_p || specs->short_p
10471 || specs->signed_p || specs->unsigned_p)
10473 specs->typespec_word = cts_int;
10475 else if (specs->complex_p)
10477 specs->typespec_word = cts_double;
10478 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10479 "ISO C does not support plain %<complex%> meaning "
10480 "%<double complex%>");
10482 else
10484 specs->typespec_word = cts_int;
10485 specs->default_int_p = true;
10486 /* We don't diagnose this here because grokdeclarator will
10487 give more specific diagnostics according to whether it is
10488 a function definition. */
10492 /* If "signed" was specified, record this to distinguish "int" and
10493 "signed int" in the case of a bit-field with
10494 -funsigned-bitfields. */
10495 specs->explicit_signed_p = specs->signed_p;
10497 /* Now compute the actual type. */
10498 switch (specs->typespec_word)
10500 case cts_auto_type:
10501 gcc_assert (!specs->long_p && !specs->short_p
10502 && !specs->signed_p && !specs->unsigned_p
10503 && !specs->complex_p);
10504 /* Type to be filled in later. */
10505 break;
10506 case cts_void:
10507 gcc_assert (!specs->long_p && !specs->short_p
10508 && !specs->signed_p && !specs->unsigned_p
10509 && !specs->complex_p);
10510 specs->type = void_type_node;
10511 break;
10512 case cts_bool:
10513 gcc_assert (!specs->long_p && !specs->short_p
10514 && !specs->signed_p && !specs->unsigned_p
10515 && !specs->complex_p);
10516 specs->type = boolean_type_node;
10517 break;
10518 case cts_char:
10519 gcc_assert (!specs->long_p && !specs->short_p);
10520 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10521 if (specs->signed_p)
10522 specs->type = signed_char_type_node;
10523 else if (specs->unsigned_p)
10524 specs->type = unsigned_char_type_node;
10525 else
10526 specs->type = char_type_node;
10527 if (specs->complex_p)
10529 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10530 "ISO C does not support complex integer types");
10531 specs->type = build_complex_type (specs->type);
10533 break;
10534 case cts_int_n:
10535 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10536 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10537 specs->type = (specs->unsigned_p
10538 ? int_n_trees[specs->int_n_idx].unsigned_type
10539 : int_n_trees[specs->int_n_idx].signed_type);
10540 if (specs->complex_p)
10542 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10543 "ISO C does not support complex integer types");
10544 specs->type = build_complex_type (specs->type);
10546 break;
10547 case cts_int:
10548 gcc_assert (!(specs->long_p && specs->short_p));
10549 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10550 if (specs->long_long_p)
10551 specs->type = (specs->unsigned_p
10552 ? long_long_unsigned_type_node
10553 : long_long_integer_type_node);
10554 else if (specs->long_p)
10555 specs->type = (specs->unsigned_p
10556 ? long_unsigned_type_node
10557 : long_integer_type_node);
10558 else if (specs->short_p)
10559 specs->type = (specs->unsigned_p
10560 ? short_unsigned_type_node
10561 : short_integer_type_node);
10562 else
10563 specs->type = (specs->unsigned_p
10564 ? unsigned_type_node
10565 : integer_type_node);
10566 if (specs->complex_p)
10568 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10569 "ISO C does not support complex integer types");
10570 specs->type = build_complex_type (specs->type);
10572 break;
10573 case cts_float:
10574 gcc_assert (!specs->long_p && !specs->short_p
10575 && !specs->signed_p && !specs->unsigned_p);
10576 specs->type = (specs->complex_p
10577 ? complex_float_type_node
10578 : float_type_node);
10579 break;
10580 case cts_double:
10581 gcc_assert (!specs->long_long_p && !specs->short_p
10582 && !specs->signed_p && !specs->unsigned_p);
10583 if (specs->long_p)
10585 specs->type = (specs->complex_p
10586 ? complex_long_double_type_node
10587 : long_double_type_node);
10589 else
10591 specs->type = (specs->complex_p
10592 ? complex_double_type_node
10593 : double_type_node);
10595 break;
10596 case cts_dfloat32:
10597 case cts_dfloat64:
10598 case cts_dfloat128:
10599 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10600 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10601 if (specs->typespec_word == cts_dfloat32)
10602 specs->type = dfloat32_type_node;
10603 else if (specs->typespec_word == cts_dfloat64)
10604 specs->type = dfloat64_type_node;
10605 else
10606 specs->type = dfloat128_type_node;
10607 break;
10608 case cts_fract:
10609 gcc_assert (!specs->complex_p);
10610 if (!targetm.fixed_point_supported_p ())
10611 specs->type = integer_type_node;
10612 else if (specs->saturating_p)
10614 if (specs->long_long_p)
10615 specs->type = specs->unsigned_p
10616 ? sat_unsigned_long_long_fract_type_node
10617 : sat_long_long_fract_type_node;
10618 else if (specs->long_p)
10619 specs->type = specs->unsigned_p
10620 ? sat_unsigned_long_fract_type_node
10621 : sat_long_fract_type_node;
10622 else if (specs->short_p)
10623 specs->type = specs->unsigned_p
10624 ? sat_unsigned_short_fract_type_node
10625 : sat_short_fract_type_node;
10626 else
10627 specs->type = specs->unsigned_p
10628 ? sat_unsigned_fract_type_node
10629 : sat_fract_type_node;
10631 else
10633 if (specs->long_long_p)
10634 specs->type = specs->unsigned_p
10635 ? unsigned_long_long_fract_type_node
10636 : long_long_fract_type_node;
10637 else if (specs->long_p)
10638 specs->type = specs->unsigned_p
10639 ? unsigned_long_fract_type_node
10640 : long_fract_type_node;
10641 else if (specs->short_p)
10642 specs->type = specs->unsigned_p
10643 ? unsigned_short_fract_type_node
10644 : short_fract_type_node;
10645 else
10646 specs->type = specs->unsigned_p
10647 ? unsigned_fract_type_node
10648 : fract_type_node;
10650 break;
10651 case cts_accum:
10652 gcc_assert (!specs->complex_p);
10653 if (!targetm.fixed_point_supported_p ())
10654 specs->type = integer_type_node;
10655 else if (specs->saturating_p)
10657 if (specs->long_long_p)
10658 specs->type = specs->unsigned_p
10659 ? sat_unsigned_long_long_accum_type_node
10660 : sat_long_long_accum_type_node;
10661 else if (specs->long_p)
10662 specs->type = specs->unsigned_p
10663 ? sat_unsigned_long_accum_type_node
10664 : sat_long_accum_type_node;
10665 else if (specs->short_p)
10666 specs->type = specs->unsigned_p
10667 ? sat_unsigned_short_accum_type_node
10668 : sat_short_accum_type_node;
10669 else
10670 specs->type = specs->unsigned_p
10671 ? sat_unsigned_accum_type_node
10672 : sat_accum_type_node;
10674 else
10676 if (specs->long_long_p)
10677 specs->type = specs->unsigned_p
10678 ? unsigned_long_long_accum_type_node
10679 : long_long_accum_type_node;
10680 else if (specs->long_p)
10681 specs->type = specs->unsigned_p
10682 ? unsigned_long_accum_type_node
10683 : long_accum_type_node;
10684 else if (specs->short_p)
10685 specs->type = specs->unsigned_p
10686 ? unsigned_short_accum_type_node
10687 : short_accum_type_node;
10688 else
10689 specs->type = specs->unsigned_p
10690 ? unsigned_accum_type_node
10691 : accum_type_node;
10693 break;
10694 default:
10695 gcc_unreachable ();
10698 return specs;
10701 /* Perform final processing on one file scope's declarations (or the
10702 external scope's declarations), GLOBALS. */
10704 static void
10705 c_write_global_declarations_1 (tree globals)
10707 tree decl;
10708 bool reconsider;
10710 /* Process the decls in the order they were written. */
10711 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10713 /* Check for used but undefined static functions using the C
10714 standard's definition of "used", and set TREE_NO_WARNING so
10715 that check_global_declaration doesn't repeat the check. */
10716 if (TREE_CODE (decl) == FUNCTION_DECL
10717 && DECL_INITIAL (decl) == 0
10718 && DECL_EXTERNAL (decl)
10719 && !TREE_PUBLIC (decl)
10720 && C_DECL_USED (decl))
10722 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10723 TREE_NO_WARNING (decl) = 1;
10726 wrapup_global_declaration_1 (decl);
10731 reconsider = false;
10732 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10733 reconsider |= wrapup_global_declaration_2 (decl);
10735 while (reconsider);
10738 /* Callback to collect a source_ref from a DECL. */
10740 static void
10741 collect_source_ref_cb (tree decl)
10743 if (!DECL_IS_BUILTIN (decl))
10744 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10747 /* Preserve the external declarations scope across a garbage collect. */
10748 static GTY(()) tree ext_block;
10750 /* Collect all references relevant to SOURCE_FILE. */
10752 static void
10753 collect_all_refs (const char *source_file)
10755 tree t;
10756 unsigned i;
10758 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10759 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10761 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10764 /* Iterate over all global declarations and call CALLBACK. */
10766 static void
10767 for_each_global_decl (void (*callback) (tree decl))
10769 tree t;
10770 tree decls;
10771 tree decl;
10772 unsigned i;
10774 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10776 decls = DECL_INITIAL (t);
10777 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10778 callback (decl);
10781 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10782 callback (decl);
10785 /* Perform any final parser cleanups and generate initial debugging
10786 information. */
10788 void
10789 c_parse_final_cleanups (void)
10791 tree t;
10792 unsigned i;
10794 /* We don't want to do this if generating a PCH. */
10795 if (pch_file)
10796 return;
10798 timevar_stop (TV_PHASE_PARSING);
10799 timevar_start (TV_PHASE_DEFERRED);
10801 /* Do the Objective-C stuff. This is where all the Objective-C
10802 module stuff gets generated (symtab, class/protocol/selector
10803 lists etc). */
10804 if (c_dialect_objc ())
10805 objc_write_global_declarations ();
10807 /* Close the external scope. */
10808 ext_block = pop_scope ();
10809 external_scope = 0;
10810 gcc_assert (!current_scope);
10812 /* Handle -fdump-ada-spec[-slim]. */
10813 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10815 /* Build a table of files to generate specs for */
10816 if (flag_dump_ada_spec_slim)
10817 collect_source_ref (main_input_filename);
10818 else
10819 for_each_global_decl (collect_source_ref_cb);
10821 dump_ada_specs (collect_all_refs, NULL);
10824 if (ext_block)
10826 tree tmp = BLOCK_VARS (ext_block);
10827 int flags;
10828 FILE * stream = dump_begin (TDI_tu, &flags);
10829 if (stream && tmp)
10831 dump_node (tmp, flags & ~TDF_SLIM, stream);
10832 dump_end (TDI_tu, stream);
10836 /* Process all file scopes in this compilation, and the external_scope,
10837 through wrapup_global_declarations. */
10838 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10839 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10840 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10842 timevar_stop (TV_PHASE_DEFERRED);
10843 timevar_start (TV_PHASE_PARSING);
10845 ext_block = NULL;
10848 /* Register reserved keyword WORD as qualifier for address space AS. */
10850 void
10851 c_register_addr_space (const char *word, addr_space_t as)
10853 int rid = RID_FIRST_ADDR_SPACE + as;
10854 tree id;
10856 /* Address space qualifiers are only supported
10857 in C with GNU extensions enabled. */
10858 if (c_dialect_objc () || flag_no_asm)
10859 return;
10861 id = get_identifier (word);
10862 C_SET_RID_CODE (id, rid);
10863 C_IS_RESERVED_WORD (id) = 1;
10864 ridpointers [rid] = id;
10867 /* Return identifier to look up for omp declare reduction. */
10869 tree
10870 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
10872 const char *p = NULL;
10873 switch (reduction_code)
10875 case PLUS_EXPR: p = "+"; break;
10876 case MULT_EXPR: p = "*"; break;
10877 case MINUS_EXPR: p = "-"; break;
10878 case BIT_AND_EXPR: p = "&"; break;
10879 case BIT_XOR_EXPR: p = "^"; break;
10880 case BIT_IOR_EXPR: p = "|"; break;
10881 case TRUTH_ANDIF_EXPR: p = "&&"; break;
10882 case TRUTH_ORIF_EXPR: p = "||"; break;
10883 case MIN_EXPR: p = "min"; break;
10884 case MAX_EXPR: p = "max"; break;
10885 default:
10886 break;
10889 if (p == NULL)
10891 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
10892 return error_mark_node;
10893 p = IDENTIFIER_POINTER (reduction_id);
10896 const char prefix[] = "omp declare reduction ";
10897 size_t lenp = sizeof (prefix);
10898 size_t len = strlen (p);
10899 char *name = XALLOCAVEC (char, lenp + len);
10900 memcpy (name, prefix, lenp - 1);
10901 memcpy (name + lenp - 1, p, len + 1);
10902 return get_identifier (name);
10905 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10906 VAR_DECL, bind it into the current scope and return it. */
10908 tree
10909 c_omp_reduction_decl (tree reduction_id)
10911 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10912 if (b != NULL && B_IN_CURRENT_SCOPE (b))
10913 return b->decl;
10915 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
10916 reduction_id, integer_type_node);
10917 DECL_ARTIFICIAL (decl) = 1;
10918 DECL_EXTERNAL (decl) = 1;
10919 TREE_STATIC (decl) = 1;
10920 TREE_PUBLIC (decl) = 0;
10921 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
10922 return decl;
10925 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10927 tree
10928 c_omp_reduction_lookup (tree reduction_id, tree type)
10930 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10931 while (b)
10933 tree t;
10934 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
10935 if (comptypes (TREE_PURPOSE (t), type))
10936 return TREE_VALUE (t);
10937 b = b->shadowed;
10939 return error_mark_node;
10942 /* Helper function called via walk_tree, to diagnose invalid
10943 #pragma omp declare reduction combiners or initializers. */
10945 tree
10946 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
10948 tree *vars = (tree *) data;
10949 if (SSA_VAR_P (*tp)
10950 && !DECL_ARTIFICIAL (*tp)
10951 && *tp != vars[0]
10952 && *tp != vars[1])
10954 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
10955 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
10956 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
10957 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10958 *tp);
10959 else
10960 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
10961 "to variable %qD which is not %<omp_priv%> nor "
10962 "%<omp_orig%>",
10963 *tp);
10964 return *tp;
10966 return NULL_TREE;
10969 #include "gt-c-c-decl.h"