Add emergency dump after an ICE
[official-gcc.git] / gcc / c / c-decl.c
blob190c00bd435e8e221912cf9c1b1a80855887418b
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2020 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 #define INCLUDE_UNIQUE_PTR
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
62 enum decl_context
63 { NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
69 /* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
72 deprecated items. */
74 enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
91 static location_t current_function_prototype_locus;
93 /* Whether this prototype was built-in. */
95 static bool current_function_prototype_built_in;
97 /* The argument type information of this prototype. */
99 static tree current_function_prototype_arg_types;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* A list of decls to be made automatically visible in each file scope. */
120 static GTY(()) tree visible_builtins;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
145 static bool undef_nested_function;
147 /* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149 int current_omp_declare_target_attribute;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 /* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
212 struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The binding oracle; see c-tree.h. */
224 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
226 /* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228 #define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
231 static inline struct c_binding* *
232 i_symbol_binding (tree node)
234 struct lang_identifier *lid
235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
237 if (lid->symbol_binding == NULL
238 && c_binding_oracle != NULL
239 && !I_SYMBOL_CHECKED (node))
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL, node);
247 return &lid->symbol_binding;
250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
252 #define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
255 /* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257 #define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
260 static inline struct c_binding **
261 i_tag_binding (tree node)
263 struct lang_identifier *lid
264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
266 if (lid->tag_binding == NULL
267 && c_binding_oracle != NULL
268 && !I_TAG_CHECKED (node))
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node) = 1;
273 c_binding_oracle (C_ORACLE_TAG, node);
276 return &lid->tag_binding;
279 #define I_TAG_BINDING(node) (*i_tag_binding (node))
281 #define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
284 /* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286 #define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
289 static inline struct c_binding **
290 i_label_binding (tree node)
292 struct lang_identifier *lid
293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
295 if (lid->label_binding == NULL
296 && c_binding_oracle != NULL
297 && !I_LABEL_CHECKED (node))
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node) = 1;
302 c_binding_oracle (C_ORACLE_LABEL, node);
305 return &lid->label_binding;
308 #define I_LABEL_BINDING(node) (*i_label_binding (node))
310 #define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
313 /* The resulting tree type. */
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
318 union tree_node GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
320 generic;
321 struct lang_identifier GTY ((tag ("1"))) identifier;
324 /* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
330 struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
350 /* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
354 struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
361 typedef struct c_goto_bindings *c_goto_bindings_p;
363 /* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
366 struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
383 /* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
388 Most declarations are recorded in the current scope.
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
409 undefined.
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
416 this scope.
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
422 list here.)
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
428 struct GTY((chain_next ("%h.outer"))) c_scope {
429 /* The scope containing this one. */
430 struct c_scope *outer;
432 /* The next outermost function scope. */
433 struct c_scope *outer_function;
435 /* All bindings in this scope. */
436 struct c_binding *bindings;
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
440 tree blocks;
441 tree blocks_last;
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth : 28;
447 /* True if we are currently filling this scope with parameter
448 declarations. */
449 BOOL_BITFIELD parm_flag : 1;
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec : 1;
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls : 1;
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body : 1;
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep : 1;
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64 : 1;
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings : 1;
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl : 1;
485 /* The scope currently in effect. */
487 static GTY(()) struct c_scope *current_scope;
489 /* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
493 static GTY(()) struct c_scope *current_function_scope;
495 /* The C file scope. This is reset for each input translation unit. */
497 static GTY(()) struct c_scope *file_scope;
499 /* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
502 static GTY(()) struct c_scope *external_scope;
504 /* A chain of c_scope structures awaiting reuse. */
506 static GTY((deletable)) struct c_scope *scope_freelist;
508 /* A chain of c_binding structures awaiting reuse. */
510 static GTY((deletable)) struct c_binding *binding_freelist;
512 /* Append VAR to LIST in scope SCOPE. */
513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
515 tree d_ = (decl); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
518 else \
519 s_->list = d_; \
520 s_->list##_last = d_; \
521 } while (0)
523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
527 if (t_->to##_last) \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
529 else \
530 t_->to = f_->from; \
531 t_->to##_last = f_->from##_last; \
532 } while (0)
534 /* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
539 struct GTY((chain_next ("%h.next"))) c_inline_static {
540 /* The location for a diagnostic. */
541 location_t location;
543 /* The function that may be an inline definition. */
544 tree function;
546 /* The object or function referenced. */
547 tree static_decl;
549 /* What sort of reference this is. */
550 enum c_inline_static_type type;
552 /* The next such structure or NULL. */
553 struct c_inline_static *next;
556 /* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558 static GTY(()) struct c_inline_static *c_inline_statics;
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
562 static bool keep_next_level_flag;
564 /* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
568 static bool next_is_function_body;
570 /* A vector of pointers to c_binding structures. */
572 typedef struct c_binding *c_binding_ptr;
574 /* Information that we keep for a struct or union while it is being
575 parsed. */
577 class c_struct_parse_info
579 public:
580 /* If warn_cxx_compat, a list of types defined within this
581 struct. */
582 auto_vec<tree> struct_types;
583 /* If warn_cxx_compat, a list of field names which have bindings,
584 and which are defined in this struct, but which are not defined
585 in any enclosing struct. This is used to clear the in_struct
586 field of the c_bindings structure. */
587 auto_vec<c_binding_ptr> fields;
588 /* If warn_cxx_compat, a list of typedef names used when defining
589 fields in this struct. */
590 auto_vec<tree> typedefs_seen;
593 /* Information for the struct or union currently being parsed, or
594 NULL if not parsing a struct or union. */
595 static class c_struct_parse_info *struct_parse_info;
597 /* Forward declarations. */
598 static tree lookup_name_in_scope (tree, struct c_scope *);
599 static tree c_make_fname_decl (location_t, tree, int);
600 static tree grokdeclarator (const struct c_declarator *,
601 struct c_declspecs *,
602 enum decl_context, bool, tree *, tree *, tree *,
603 bool *, enum deprecated_states);
604 static tree grokparms (struct c_arg_info *, bool);
605 static void layout_array_type (tree);
606 static void warn_defaults_to (location_t, int, const char *, ...)
607 ATTRIBUTE_GCC_DIAG(3,4);
608 static const char *header_for_builtin_fn (tree);
610 /* T is a statement. Add it to the statement-tree. This is the
611 C/ObjC version--C++ has a slightly different version of this
612 function. */
614 tree
615 add_stmt (tree t)
617 enum tree_code code = TREE_CODE (t);
619 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
621 if (!EXPR_HAS_LOCATION (t))
622 SET_EXPR_LOCATION (t, input_location);
625 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
626 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
628 /* Add T to the statement-tree. Non-side-effect statements need to be
629 recorded during statement expressions. */
630 if (!building_stmt_list_p ())
631 push_stmt_list ();
632 append_to_statement_list_force (t, &cur_stmt_list);
634 return t;
637 /* Build a pointer type using the default pointer mode. */
639 static tree
640 c_build_pointer_type (tree to_type)
642 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
643 : TYPE_ADDR_SPACE (to_type);
644 machine_mode pointer_mode;
646 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
647 pointer_mode = targetm.addr_space.pointer_mode (as);
648 else
649 pointer_mode = c_default_pointer_mode;
650 return build_pointer_type_for_mode (to_type, pointer_mode, false);
654 /* Return true if we will want to say something if a goto statement
655 crosses DECL. */
657 static bool
658 decl_jump_unsafe (tree decl)
660 if (error_operand_p (decl))
661 return false;
663 /* Don't warn for compound literals. If a goto statement crosses
664 their initialization, it should cross also all the places where
665 the complit is used or where the complit address might be saved
666 into some variable, so code after the label to which goto jumps
667 should not be able to refer to the compound literal. */
668 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
669 return false;
671 /* Always warn about crossing variably modified types. */
672 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
673 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
674 return true;
676 /* Otherwise, only warn if -Wgoto-misses-init and this is an
677 initialized automatic decl. */
678 if (warn_jump_misses_init
679 && VAR_P (decl)
680 && !TREE_STATIC (decl)
681 && DECL_INITIAL (decl) != NULL_TREE)
682 return true;
684 return false;
688 void
689 c_print_identifier (FILE *file, tree node, int indent)
691 void (*save) (enum c_oracle_request, tree identifier);
693 /* Temporarily hide any binding oracle. Without this, calls to
694 debug_tree from the debugger will end up calling into the oracle,
695 making for a confusing debug session. As the oracle isn't needed
696 here for normal operation, it's simplest to suppress it. */
697 save = c_binding_oracle;
698 c_binding_oracle = NULL;
700 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
701 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
702 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
703 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
705 tree rid = ridpointers[C_RID_CODE (node)];
706 indent_to (file, indent + 4);
707 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
708 (void *) rid, IDENTIFIER_POINTER (rid));
711 c_binding_oracle = save;
714 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
715 which may be any of several kinds of DECL or TYPE or error_mark_node,
716 in the scope SCOPE. */
717 static void
718 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
719 bool nested, location_t locus)
721 struct c_binding *b, **here;
723 if (binding_freelist)
725 b = binding_freelist;
726 binding_freelist = b->prev;
728 else
729 b = ggc_alloc<c_binding> ();
731 b->shadowed = 0;
732 b->decl = decl;
733 b->id = name;
734 b->depth = scope->depth;
735 b->invisible = invisible;
736 b->nested = nested;
737 b->inner_comp = 0;
738 b->in_struct = 0;
739 b->locus = locus;
741 b->u.type = NULL;
743 b->prev = scope->bindings;
744 scope->bindings = b;
746 if (decl_jump_unsafe (decl))
747 scope->has_jump_unsafe_decl = 1;
749 if (!name)
750 return;
752 switch (TREE_CODE (decl))
754 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
755 case ENUMERAL_TYPE:
756 case UNION_TYPE:
757 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
758 case VAR_DECL:
759 case FUNCTION_DECL:
760 case TYPE_DECL:
761 case CONST_DECL:
762 case PARM_DECL:
763 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
765 default:
766 gcc_unreachable ();
769 /* Locate the appropriate place in the chain of shadowed decls
770 to insert this binding. Normally, scope == current_scope and
771 this does nothing. */
772 while (*here && (*here)->depth > scope->depth)
773 here = &(*here)->shadowed;
775 b->shadowed = *here;
776 *here = b;
779 /* Clear the binding structure B, stick it on the binding_freelist,
780 and return the former value of b->prev. This is used by pop_scope
781 and get_parm_info to iterate destructively over all the bindings
782 from a given scope. */
783 static struct c_binding *
784 free_binding_and_advance (struct c_binding *b)
786 struct c_binding *prev = b->prev;
788 memset (b, 0, sizeof (struct c_binding));
789 b->prev = binding_freelist;
790 binding_freelist = b;
792 return prev;
795 /* Bind a label. Like bind, but skip fields which aren't used for
796 labels, and add the LABEL_VARS value. */
797 static void
798 bind_label (tree name, tree label, struct c_scope *scope,
799 struct c_label_vars *label_vars)
801 struct c_binding *b;
803 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
804 UNKNOWN_LOCATION);
806 scope->has_label_bindings = true;
808 b = scope->bindings;
809 gcc_assert (b->decl == label);
810 label_vars->shadowed = b->u.label;
811 b->u.label = label_vars;
814 /* Hook called at end of compilation to assume 1 elt
815 for a file-scope tentative array defn that wasn't complete before. */
817 void
818 c_finish_incomplete_decl (tree decl)
820 if (VAR_P (decl))
822 tree type = TREE_TYPE (decl);
823 if (type != error_mark_node
824 && TREE_CODE (type) == ARRAY_TYPE
825 && !DECL_EXTERNAL (decl)
826 && TYPE_DOMAIN (type) == NULL_TREE)
828 warning_at (DECL_SOURCE_LOCATION (decl),
829 0, "array %q+D assumed to have one element", decl);
831 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
833 relayout_decl (decl);
838 /* Record that inline function FUNC contains a reference (location
839 LOC) to static DECL (file-scope or function-local according to
840 TYPE). */
842 void
843 record_inline_static (location_t loc, tree func, tree decl,
844 enum c_inline_static_type type)
846 c_inline_static *csi = ggc_alloc<c_inline_static> ();
847 csi->location = loc;
848 csi->function = func;
849 csi->static_decl = decl;
850 csi->type = type;
851 csi->next = c_inline_statics;
852 c_inline_statics = csi;
855 /* Check for references to static declarations in inline functions at
856 the end of the translation unit and diagnose them if the functions
857 are still inline definitions. */
859 static void
860 check_inline_statics (void)
862 struct c_inline_static *csi;
863 for (csi = c_inline_statics; csi; csi = csi->next)
865 if (DECL_EXTERNAL (csi->function))
866 switch (csi->type)
868 case csi_internal:
869 pedwarn (csi->location, 0,
870 "%qD is static but used in inline function %qD "
871 "which is not static", csi->static_decl, csi->function);
872 break;
873 case csi_modifiable:
874 pedwarn (csi->location, 0,
875 "%q+D is static but declared in inline function %qD "
876 "which is not static", csi->static_decl, csi->function);
877 break;
878 default:
879 gcc_unreachable ();
882 c_inline_statics = NULL;
885 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
886 for the current state, otherwise set it to uninitialized. */
888 static void
889 set_spot_bindings (struct c_spot_bindings *p, bool defining)
891 if (defining)
893 p->scope = current_scope;
894 p->bindings_in_scope = current_scope->bindings;
896 else
898 p->scope = NULL;
899 p->bindings_in_scope = NULL;
901 p->stmt_exprs = 0;
902 p->left_stmt_expr = false;
905 /* Update spot bindings P as we pop out of SCOPE. Return true if we
906 should push decls for a label. */
908 static bool
909 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
911 if (p->scope != scope)
913 /* This label or goto is defined in some other scope, or it is a
914 label which is not yet defined. There is nothing to
915 update. */
916 return false;
919 /* Adjust the spot bindings to refer to the bindings already defined
920 in the enclosing scope. */
921 p->scope = scope->outer;
922 p->bindings_in_scope = p->scope->bindings;
924 return true;
927 /* The Objective-C front-end often needs to determine the current scope. */
929 void *
930 objc_get_current_scope (void)
932 return current_scope;
935 /* The following function is used only by Objective-C. It needs to live here
936 because it accesses the innards of c_scope. */
938 void
939 objc_mark_locals_volatile (void *enclosing_blk)
941 struct c_scope *scope;
942 struct c_binding *b;
944 for (scope = current_scope;
945 scope && scope != enclosing_blk;
946 scope = scope->outer)
948 for (b = scope->bindings; b; b = b->prev)
949 objc_volatilize_decl (b->decl);
951 /* Do not climb up past the current function. */
952 if (scope->function_body)
953 break;
957 /* Return true if we are in the global binding level. */
959 bool
960 global_bindings_p (void)
962 return current_scope == file_scope;
965 /* Return true if we're declaring parameters in an old-style function
966 declaration. */
968 bool
969 old_style_parameter_scope (void)
971 /* If processing parameters and there is no function statement list, we
972 * have an old-style function declaration. */
973 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
976 void
977 keep_next_level (void)
979 keep_next_level_flag = true;
982 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
984 void
985 set_float_const_decimal64 (void)
987 current_scope->float_const_decimal64 = true;
990 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
992 void
993 clear_float_const_decimal64 (void)
995 current_scope->float_const_decimal64 = false;
998 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1000 bool
1001 float_const_decimal64_p (void)
1003 return current_scope->float_const_decimal64;
1006 /* Identify this scope as currently being filled with parameters. */
1008 void
1009 declare_parm_level (void)
1011 current_scope->parm_flag = true;
1014 void
1015 push_scope (void)
1017 if (next_is_function_body)
1019 /* This is the transition from the parameters to the top level
1020 of the function body. These are the same scope
1021 (C99 6.2.1p4,6) so we do not push another scope structure.
1022 next_is_function_body is set only by store_parm_decls, which
1023 in turn is called when and only when we are about to
1024 encounter the opening curly brace for the function body.
1026 The outermost block of a function always gets a BLOCK node,
1027 because the debugging output routines expect that each
1028 function has at least one BLOCK. */
1029 current_scope->parm_flag = false;
1030 current_scope->function_body = true;
1031 current_scope->keep = true;
1032 current_scope->outer_function = current_function_scope;
1033 current_function_scope = current_scope;
1035 keep_next_level_flag = false;
1036 next_is_function_body = false;
1038 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1039 if (current_scope->outer)
1040 current_scope->float_const_decimal64
1041 = current_scope->outer->float_const_decimal64;
1042 else
1043 current_scope->float_const_decimal64 = false;
1045 else
1047 struct c_scope *scope;
1048 if (scope_freelist)
1050 scope = scope_freelist;
1051 scope_freelist = scope->outer;
1053 else
1054 scope = ggc_cleared_alloc<c_scope> ();
1056 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1057 if (current_scope)
1058 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1059 else
1060 scope->float_const_decimal64 = false;
1062 scope->keep = keep_next_level_flag;
1063 scope->outer = current_scope;
1064 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1066 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1067 possible. */
1068 if (current_scope && scope->depth == 0)
1070 scope->depth--;
1071 sorry ("GCC supports only %u nested scopes", scope->depth);
1074 current_scope = scope;
1075 keep_next_level_flag = false;
1079 /* This is called when we are leaving SCOPE. For each label defined
1080 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1081 These are the decls whose initialization will be skipped by a goto
1082 later in the function. */
1084 static void
1085 update_label_decls (struct c_scope *scope)
1087 struct c_scope *s;
1089 s = scope;
1090 while (s != NULL)
1092 if (s->has_label_bindings)
1094 struct c_binding *b;
1096 for (b = s->bindings; b != NULL; b = b->prev)
1098 struct c_label_vars *label_vars;
1099 struct c_binding *b1;
1100 bool hjud;
1101 unsigned int ix;
1102 struct c_goto_bindings *g;
1104 if (TREE_CODE (b->decl) != LABEL_DECL)
1105 continue;
1106 label_vars = b->u.label;
1108 b1 = label_vars->label_bindings.bindings_in_scope;
1109 if (label_vars->label_bindings.scope == NULL)
1110 hjud = false;
1111 else
1112 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1113 if (update_spot_bindings (scope, &label_vars->label_bindings))
1115 /* This label is defined in this scope. */
1116 if (hjud)
1118 for (; b1 != NULL; b1 = b1->prev)
1120 /* A goto from later in the function to this
1121 label will never see the initialization
1122 of B1, if any. Save it to issue a
1123 warning if needed. */
1124 if (decl_jump_unsafe (b1->decl))
1125 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1130 /* Update the bindings of any goto statements associated
1131 with this label. */
1132 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1133 update_spot_bindings (scope, &g->goto_bindings);
1137 /* Don't search beyond the current function. */
1138 if (s == current_function_scope)
1139 break;
1141 s = s->outer;
1145 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1147 static void
1148 set_type_context (tree type, tree context)
1150 for (type = TYPE_MAIN_VARIANT (type); type;
1151 type = TYPE_NEXT_VARIANT (type))
1152 TYPE_CONTEXT (type) = context;
1155 /* Exit a scope. Restore the state of the identifier-decl mappings
1156 that were in effect when this scope was entered. Return a BLOCK
1157 node containing all the DECLs in this scope that are of interest
1158 to debug info generation. */
1160 tree
1161 pop_scope (void)
1163 struct c_scope *scope = current_scope;
1164 tree block, context, p;
1165 struct c_binding *b;
1167 bool functionbody = scope->function_body;
1168 bool keep = functionbody || scope->keep || scope->bindings;
1170 update_label_decls (scope);
1172 /* If appropriate, create a BLOCK to record the decls for the life
1173 of this function. */
1174 block = NULL_TREE;
1175 if (keep)
1177 block = make_node (BLOCK);
1178 BLOCK_SUBBLOCKS (block) = scope->blocks;
1179 TREE_USED (block) = 1;
1181 /* In each subblock, record that this is its superior. */
1182 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1183 BLOCK_SUPERCONTEXT (p) = block;
1185 BLOCK_VARS (block) = NULL_TREE;
1188 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1189 scope must be set so that they point to the appropriate
1190 construct, i.e. either to the current FUNCTION_DECL node, or
1191 else to the BLOCK node we just constructed.
1193 Note that for tagged types whose scope is just the formal
1194 parameter list for some function type specification, we can't
1195 properly set their TYPE_CONTEXTs here, because we don't have a
1196 pointer to the appropriate FUNCTION_TYPE node readily available
1197 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1198 type nodes get set in `grokdeclarator' as soon as we have created
1199 the FUNCTION_TYPE node which will represent the "scope" for these
1200 "parameter list local" tagged types. */
1201 if (scope->function_body)
1202 context = current_function_decl;
1203 else if (scope == file_scope)
1205 tree file_decl
1206 = build_translation_unit_decl (get_identifier (main_input_filename));
1207 context = file_decl;
1208 debug_hooks->register_main_translation_unit (file_decl);
1210 else
1211 context = block;
1213 /* Clear all bindings in this scope. */
1214 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1216 p = b->decl;
1217 switch (TREE_CODE (p))
1219 case LABEL_DECL:
1220 /* Warnings for unused labels, errors for undefined labels. */
1221 if (TREE_USED (p) && !DECL_INITIAL (p))
1223 error ("label %q+D used but not defined", p);
1224 DECL_INITIAL (p) = error_mark_node;
1226 else
1227 warn_for_unused_label (p);
1229 /* Labels go in BLOCK_VARS. */
1230 DECL_CHAIN (p) = BLOCK_VARS (block);
1231 BLOCK_VARS (block) = p;
1232 gcc_assert (I_LABEL_BINDING (b->id) == b);
1233 I_LABEL_BINDING (b->id) = b->shadowed;
1235 /* Also pop back to the shadowed label_vars. */
1236 release_tree_vector (b->u.label->decls_in_scope);
1237 b->u.label = b->u.label->shadowed;
1238 break;
1240 case ENUMERAL_TYPE:
1241 case UNION_TYPE:
1242 case RECORD_TYPE:
1243 set_type_context (p, context);
1245 /* Types may not have tag-names, in which case the type
1246 appears in the bindings list with b->id NULL. */
1247 if (b->id)
1249 gcc_assert (I_TAG_BINDING (b->id) == b);
1250 I_TAG_BINDING (b->id) = b->shadowed;
1252 break;
1254 case FUNCTION_DECL:
1255 /* Propagate TREE_ADDRESSABLE from nested functions to their
1256 containing functions. */
1257 if (!TREE_ASM_WRITTEN (p)
1258 && DECL_INITIAL (p) != NULL_TREE
1259 && TREE_ADDRESSABLE (p)
1260 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1261 && DECL_ABSTRACT_ORIGIN (p) != p)
1262 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1263 if (!TREE_PUBLIC (p)
1264 && !DECL_INITIAL (p)
1265 && !b->nested
1266 && scope != file_scope
1267 && scope != external_scope)
1269 error ("nested function %q+D declared but never defined", p);
1270 undef_nested_function = true;
1272 else if (DECL_DECLARED_INLINE_P (p)
1273 && TREE_PUBLIC (p)
1274 && !DECL_INITIAL (p))
1276 /* C99 6.7.4p6: "a function with external linkage... declared
1277 with an inline function specifier ... shall also be defined
1278 in the same translation unit." */
1279 if (!flag_gnu89_inline
1280 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1281 && scope == external_scope)
1282 pedwarn (input_location, 0,
1283 "inline function %q+D declared but never defined", p);
1284 DECL_EXTERNAL (p) = 1;
1287 goto common_symbol;
1289 case VAR_DECL:
1290 /* Warnings for unused variables. */
1291 if ((!TREE_USED (p) || !DECL_READ_P (p))
1292 && !TREE_NO_WARNING (p)
1293 && !DECL_IN_SYSTEM_HEADER (p)
1294 && DECL_NAME (p)
1295 && !DECL_ARTIFICIAL (p)
1296 && scope != file_scope
1297 && scope != external_scope)
1299 if (!TREE_USED (p))
1300 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1301 else if (DECL_CONTEXT (p) == current_function_decl)
1302 warning_at (DECL_SOURCE_LOCATION (p),
1303 OPT_Wunused_but_set_variable,
1304 "variable %qD set but not used", p);
1307 if (b->inner_comp)
1309 error ("type of array %q+D completed incompatibly with"
1310 " implicit initialization", p);
1313 /* Fall through. */
1314 case TYPE_DECL:
1315 case CONST_DECL:
1316 common_symbol:
1317 /* All of these go in BLOCK_VARS, but only if this is the
1318 binding in the home scope. */
1319 if (!b->nested)
1321 DECL_CHAIN (p) = BLOCK_VARS (block);
1322 BLOCK_VARS (block) = p;
1324 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1326 /* For block local externs add a special
1327 DECL_EXTERNAL decl for debug info generation. */
1328 tree extp = copy_node (p);
1330 DECL_EXTERNAL (extp) = 1;
1331 TREE_STATIC (extp) = 0;
1332 TREE_PUBLIC (extp) = 1;
1333 DECL_INITIAL (extp) = NULL_TREE;
1334 DECL_LANG_SPECIFIC (extp) = NULL;
1335 DECL_CONTEXT (extp) = current_function_decl;
1336 if (TREE_CODE (p) == FUNCTION_DECL)
1338 DECL_RESULT (extp) = NULL_TREE;
1339 DECL_SAVED_TREE (extp) = NULL_TREE;
1340 DECL_STRUCT_FUNCTION (extp) = NULL;
1342 if (b->locus != UNKNOWN_LOCATION)
1343 DECL_SOURCE_LOCATION (extp) = b->locus;
1344 DECL_CHAIN (extp) = BLOCK_VARS (block);
1345 BLOCK_VARS (block) = extp;
1347 /* If this is the file scope set DECL_CONTEXT of each decl to
1348 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1349 work. */
1350 if (scope == file_scope)
1352 DECL_CONTEXT (p) = context;
1353 if (TREE_CODE (p) == TYPE_DECL
1354 && TREE_TYPE (p) != error_mark_node)
1355 set_type_context (TREE_TYPE (p), context);
1358 gcc_fallthrough ();
1359 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1360 already been put there by store_parm_decls. Unused-
1361 parameter warnings are handled by function.c.
1362 error_mark_node obviously does not go in BLOCK_VARS and
1363 does not get unused-variable warnings. */
1364 case PARM_DECL:
1365 case ERROR_MARK:
1366 /* It is possible for a decl not to have a name. We get
1367 here with b->id NULL in this case. */
1368 if (b->id)
1370 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1371 I_SYMBOL_BINDING (b->id) = b->shadowed;
1372 if (b->shadowed && b->shadowed->u.type)
1373 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1375 break;
1377 default:
1378 gcc_unreachable ();
1383 /* Dispose of the block that we just made inside some higher level. */
1384 if ((scope->function_body || scope == file_scope) && context)
1386 DECL_INITIAL (context) = block;
1387 BLOCK_SUPERCONTEXT (block) = context;
1389 else if (scope->outer)
1391 if (block)
1392 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1393 /* If we did not make a block for the scope just exited, any
1394 blocks made for inner scopes must be carried forward so they
1395 will later become subblocks of something else. */
1396 else if (scope->blocks)
1397 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1400 /* Pop the current scope, and free the structure for reuse. */
1401 current_scope = scope->outer;
1402 if (scope->function_body)
1403 current_function_scope = scope->outer_function;
1405 memset (scope, 0, sizeof (struct c_scope));
1406 scope->outer = scope_freelist;
1407 scope_freelist = scope;
1409 return block;
1412 void
1413 push_file_scope (void)
1415 tree decl;
1417 if (file_scope)
1418 return;
1420 push_scope ();
1421 file_scope = current_scope;
1423 start_fname_decls ();
1425 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1426 bind (DECL_NAME (decl), decl, file_scope,
1427 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1430 void
1431 pop_file_scope (void)
1433 /* In case there were missing closebraces, get us back to the global
1434 binding level. */
1435 while (current_scope != file_scope)
1436 pop_scope ();
1438 /* __FUNCTION__ is defined at file scope (""). This
1439 call may not be necessary as my tests indicate it
1440 still works without it. */
1441 finish_fname_decls ();
1443 check_inline_statics ();
1445 /* This is the point to write out a PCH if we're doing that.
1446 In that case we do not want to do anything else. */
1447 if (pch_file)
1449 c_common_write_pch ();
1450 /* Ensure even the callers don't try to finalize the CU. */
1451 flag_syntax_only = 1;
1452 return;
1455 /* Pop off the file scope and close this translation unit. */
1456 pop_scope ();
1457 file_scope = 0;
1459 maybe_apply_pending_pragma_weaks ();
1462 /* Adjust the bindings for the start of a statement expression. */
1464 void
1465 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1467 struct c_scope *scope;
1469 for (scope = current_scope; scope != NULL; scope = scope->outer)
1471 struct c_binding *b;
1473 if (!scope->has_label_bindings)
1474 continue;
1476 for (b = scope->bindings; b != NULL; b = b->prev)
1478 struct c_label_vars *label_vars;
1479 unsigned int ix;
1480 struct c_goto_bindings *g;
1482 if (TREE_CODE (b->decl) != LABEL_DECL)
1483 continue;
1484 label_vars = b->u.label;
1485 ++label_vars->label_bindings.stmt_exprs;
1486 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1487 ++g->goto_bindings.stmt_exprs;
1491 if (switch_bindings != NULL)
1492 ++switch_bindings->stmt_exprs;
1495 /* Adjust the bindings for the end of a statement expression. */
1497 void
1498 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1500 struct c_scope *scope;
1502 for (scope = current_scope; scope != NULL; scope = scope->outer)
1504 struct c_binding *b;
1506 if (!scope->has_label_bindings)
1507 continue;
1509 for (b = scope->bindings; b != NULL; b = b->prev)
1511 struct c_label_vars *label_vars;
1512 unsigned int ix;
1513 struct c_goto_bindings *g;
1515 if (TREE_CODE (b->decl) != LABEL_DECL)
1516 continue;
1517 label_vars = b->u.label;
1518 --label_vars->label_bindings.stmt_exprs;
1519 if (label_vars->label_bindings.stmt_exprs < 0)
1521 label_vars->label_bindings.left_stmt_expr = true;
1522 label_vars->label_bindings.stmt_exprs = 0;
1524 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1526 --g->goto_bindings.stmt_exprs;
1527 if (g->goto_bindings.stmt_exprs < 0)
1529 g->goto_bindings.left_stmt_expr = true;
1530 g->goto_bindings.stmt_exprs = 0;
1536 if (switch_bindings != NULL)
1538 --switch_bindings->stmt_exprs;
1539 gcc_assert (switch_bindings->stmt_exprs >= 0);
1543 /* Push a definition or a declaration of struct, union or enum tag "name".
1544 "type" should be the type node.
1545 We assume that the tag "name" is not already defined, and has a location
1546 of LOC.
1548 Note that the definition may really be just a forward reference.
1549 In that case, the TYPE_SIZE will be zero. */
1551 static void
1552 pushtag (location_t loc, tree name, tree type)
1554 /* Record the identifier as the type's name if it has none. */
1555 if (name && !TYPE_NAME (type))
1556 TYPE_NAME (type) = name;
1557 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1559 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1560 tagged type we just added to the current scope. This fake
1561 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1562 to output a representation of a tagged type, and it also gives
1563 us a convenient place to record the "scope start" address for the
1564 tagged type. */
1566 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1567 TYPE_DECL, NULL_TREE, type));
1569 /* An approximation for now, so we can tell this is a function-scope tag.
1570 This will be updated in pop_scope. */
1571 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1573 if (warn_cxx_compat && name != NULL_TREE)
1575 struct c_binding *b = I_SYMBOL_BINDING (name);
1577 if (b != NULL
1578 && b->decl != NULL_TREE
1579 && TREE_CODE (b->decl) == TYPE_DECL
1580 && (B_IN_CURRENT_SCOPE (b)
1581 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1582 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1583 != TYPE_MAIN_VARIANT (type)))
1585 auto_diagnostic_group d;
1586 if (warning_at (loc, OPT_Wc___compat,
1587 ("using %qD as both a typedef and a tag is "
1588 "invalid in C++"), b->decl)
1589 && b->locus != UNKNOWN_LOCATION)
1590 inform (b->locus, "originally defined here");
1595 /* An exported interface to pushtag. This is used by the gdb plugin's
1596 binding oracle to introduce a new tag binding. */
1598 void
1599 c_pushtag (location_t loc, tree name, tree type)
1601 pushtag (loc, name, type);
1604 /* An exported interface to bind a declaration. LOC is the location
1605 to use. DECL is the declaration to bind. The decl's name is used
1606 to determine how it is bound. If DECL is a VAR_DECL, then
1607 IS_GLOBAL determines whether the decl is put into the global (file
1608 and external) scope or the current function's scope; if DECL is not
1609 a VAR_DECL then it is always put into the file scope. */
1611 void
1612 c_bind (location_t loc, tree decl, bool is_global)
1614 struct c_scope *scope;
1615 bool nested = false;
1617 if (!VAR_P (decl) || current_function_scope == NULL)
1619 /* Types and functions are always considered to be global. */
1620 scope = file_scope;
1621 DECL_EXTERNAL (decl) = 1;
1622 TREE_PUBLIC (decl) = 1;
1624 else if (is_global)
1626 /* Also bind it into the external scope. */
1627 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1628 nested = true;
1629 scope = file_scope;
1630 DECL_EXTERNAL (decl) = 1;
1631 TREE_PUBLIC (decl) = 1;
1633 else
1635 DECL_CONTEXT (decl) = current_function_decl;
1636 TREE_PUBLIC (decl) = 0;
1637 scope = current_function_scope;
1640 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1644 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1645 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1646 to the builtin_structptr_types array. Subsequent declarations of such
1647 built-ins are expected to refer to it rather than to fileptr_type_node,
1648 etc. which is just void* (or to any other type).
1649 Used only by match_builtin_function_types. */
1651 static const unsigned builtin_structptr_type_count
1652 = sizeof builtin_structptr_types / sizeof builtin_structptr_types[0];
1654 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1656 /* Returns true if types T1 and T2 representing return types or types
1657 of function arguments are close enough to be considered interchangeable
1658 in redeclarations of built-in functions. */
1660 static bool
1661 types_close_enough_to_match (tree t1, tree t2)
1663 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1664 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1665 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1668 /* Subroutine of compare_decls. Allow harmless mismatches in return
1669 and argument types provided that the type modes match. Set *STRICT
1670 and *ARGNO to the expected argument type and number in case of
1671 an argument type mismatch or null and zero otherwise. Return
1672 a unified type given a suitable match, and 0 otherwise. */
1674 static tree
1675 match_builtin_function_types (tree newtype, tree oldtype,
1676 tree *strict, unsigned *argno)
1678 *argno = 0;
1679 *strict = NULL_TREE;
1681 /* Accept the return type of the new declaration if it has the same
1682 mode and if they're both pointers or if neither is. */
1683 tree oldrettype = TREE_TYPE (oldtype);
1684 tree newrettype = TREE_TYPE (newtype);
1686 if (!types_close_enough_to_match (oldrettype, newrettype))
1687 return NULL_TREE;
1689 /* Check that the return types are compatible but don't fail if they
1690 are not (e.g., int vs long in ILP32) and just let the caller know. */
1691 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1692 TYPE_MAIN_VARIANT (newrettype)))
1693 *strict = oldrettype;
1695 tree oldargs = TYPE_ARG_TYPES (oldtype);
1696 tree newargs = TYPE_ARG_TYPES (newtype);
1697 tree tryargs = newargs;
1699 const unsigned nlst
1700 = sizeof last_structptr_types / sizeof last_structptr_types[0];
1701 const unsigned nbst
1702 = sizeof builtin_structptr_types / sizeof builtin_structptr_types[0];
1704 gcc_checking_assert (nlst == nbst);
1706 for (unsigned i = 1; oldargs || newargs; ++i)
1708 if (!oldargs
1709 || !newargs
1710 || !TREE_VALUE (oldargs)
1711 || !TREE_VALUE (newargs))
1712 return NULL_TREE;
1714 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1715 tree newtype = TREE_VALUE (newargs);
1716 if (newtype == error_mark_node)
1717 return NULL_TREE;
1718 newtype = TYPE_MAIN_VARIANT (newtype);
1720 if (!types_close_enough_to_match (oldtype, newtype))
1721 return NULL_TREE;
1723 unsigned j = nbst;
1724 if (POINTER_TYPE_P (oldtype))
1725 /* Iterate over well-known struct types like FILE (whose types
1726 aren't known to us) and compare the pointer to each to
1727 the pointer argument. */
1728 for (j = 0; j < nbst; ++j)
1730 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1731 continue;
1732 /* Store the first FILE* etc. argument type (whatever it is), and
1733 expect any subsequent declarations of file I/O etc. built-ins
1734 to refer to it rather than to fileptr_type_node etc. which is
1735 just void* (or const void*). */
1736 if (last_structptr_types[j])
1738 if (!comptypes (last_structptr_types[j], newtype))
1740 *argno = i;
1741 *strict = last_structptr_types[j];
1744 else
1745 last_structptr_types[j] = newtype;
1746 break;
1749 if (j == nbst && !comptypes (oldtype, newtype))
1751 if (POINTER_TYPE_P (oldtype))
1753 /* For incompatible pointers, only reject differences in
1754 the unqualified variants of the referenced types but
1755 consider differences in qualifiers as benign (report
1756 those to caller via *STRICT below). */
1757 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1758 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1759 if (!comptypes (oldref, newref))
1760 return NULL_TREE;
1763 if (!*strict)
1765 *argno = i;
1766 *strict = oldtype;
1770 oldargs = TREE_CHAIN (oldargs);
1771 newargs = TREE_CHAIN (newargs);
1774 tree trytype = build_function_type (newrettype, tryargs);
1776 /* Allow declaration to change transaction_safe attribute. */
1777 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1778 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1779 tree newattrs = TYPE_ATTRIBUTES (newtype);
1780 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1781 if (oldtsafe && !newtsafe)
1782 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1783 else if (newtsafe && !oldtsafe)
1784 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1785 NULL_TREE, oldattrs);
1787 return build_type_attribute_variant (trytype, oldattrs);
1790 /* Subroutine of diagnose_mismatched_decls. Check for function type
1791 mismatch involving an empty arglist vs a nonempty one and give clearer
1792 diagnostics. */
1793 static void
1794 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1795 tree newtype, tree oldtype)
1797 tree t;
1799 if (TREE_CODE (olddecl) != FUNCTION_DECL
1800 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1801 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1802 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1803 return;
1805 t = TYPE_ARG_TYPES (oldtype);
1806 if (t == NULL_TREE)
1807 t = TYPE_ARG_TYPES (newtype);
1808 for (; t; t = TREE_CHAIN (t))
1810 tree type = TREE_VALUE (t);
1812 if (TREE_CHAIN (t) == NULL_TREE
1813 && TYPE_MAIN_VARIANT (type) != void_type_node)
1815 inform (input_location, "a parameter list with an ellipsis "
1816 "cannot match an empty parameter name list declaration");
1817 break;
1820 if (c_type_promotes_to (type) != type)
1822 inform (input_location, "an argument type that has a default "
1823 "promotion cannot match an empty parameter name list "
1824 "declaration");
1825 break;
1830 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1831 old-style function definition, NEWDECL is a prototype declaration.
1832 Diagnose inconsistencies in the argument list. Returns TRUE if
1833 the prototype is compatible, FALSE if not. */
1834 static bool
1835 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1837 tree newargs, oldargs;
1838 int i;
1840 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1842 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1843 newargs = TYPE_ARG_TYPES (newtype);
1844 i = 1;
1846 for (;;)
1848 tree oldargtype = TREE_VALUE (oldargs);
1849 tree newargtype = TREE_VALUE (newargs);
1851 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1852 return false;
1854 oldargtype = (TYPE_ATOMIC (oldargtype)
1855 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1856 TYPE_QUAL_ATOMIC)
1857 : TYPE_MAIN_VARIANT (oldargtype));
1858 newargtype = (TYPE_ATOMIC (newargtype)
1859 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1860 TYPE_QUAL_ATOMIC)
1861 : TYPE_MAIN_VARIANT (newargtype));
1863 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1864 break;
1866 /* Reaching the end of just one list means the two decls don't
1867 agree on the number of arguments. */
1868 if (END_OF_ARGLIST (oldargtype))
1870 error ("prototype for %q+D declares more arguments "
1871 "than previous old-style definition", newdecl);
1872 return false;
1874 else if (END_OF_ARGLIST (newargtype))
1876 error ("prototype for %q+D declares fewer arguments "
1877 "than previous old-style definition", newdecl);
1878 return false;
1881 /* Type for passing arg must be consistent with that declared
1882 for the arg. */
1883 else if (!comptypes (oldargtype, newargtype))
1885 error ("prototype for %q+D declares argument %d"
1886 " with incompatible type",
1887 newdecl, i);
1888 return false;
1891 oldargs = TREE_CHAIN (oldargs);
1892 newargs = TREE_CHAIN (newargs);
1893 i++;
1896 /* If we get here, no errors were found, but do issue a warning
1897 for this poor-style construct. */
1898 warning (0, "prototype for %q+D follows non-prototype definition",
1899 newdecl);
1900 return true;
1901 #undef END_OF_ARGLIST
1904 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1905 first in a pair of mismatched declarations, using the diagnostic
1906 function DIAG. */
1907 static void
1908 locate_old_decl (tree decl)
1910 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl)
1911 && !C_DECL_DECLARED_BUILTIN (decl))
1913 else if (DECL_INITIAL (decl))
1914 inform (input_location, "previous definition of %q+D was here", decl);
1915 else if (C_DECL_IMPLICIT (decl))
1916 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1917 else
1918 inform (input_location, "previous declaration of %q+D was here", decl);
1921 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1922 Returns true if the caller should proceed to merge the two, false
1923 if OLDDECL should simply be discarded. As a side effect, issues
1924 all necessary diagnostics for invalid or poor-style combinations.
1925 If it returns true, writes the types of NEWDECL and OLDDECL to
1926 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1927 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1929 static bool
1930 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1931 tree *newtypep, tree *oldtypep)
1933 tree newtype, oldtype;
1934 bool retval = true;
1936 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1937 && DECL_EXTERNAL (DECL))
1939 /* If we have error_mark_node for either decl or type, just discard
1940 the previous decl - we're in an error cascade already. */
1941 if (olddecl == error_mark_node || newdecl == error_mark_node)
1942 return false;
1943 *oldtypep = oldtype = TREE_TYPE (olddecl);
1944 *newtypep = newtype = TREE_TYPE (newdecl);
1945 if (oldtype == error_mark_node || newtype == error_mark_node)
1946 return false;
1948 /* Two different categories of symbol altogether. This is an error
1949 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1950 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1952 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1953 && fndecl_built_in_p (olddecl)
1954 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1956 auto_diagnostic_group d;
1957 error ("%q+D redeclared as different kind of symbol", newdecl);
1958 locate_old_decl (olddecl);
1960 else if (TREE_PUBLIC (newdecl))
1961 warning (OPT_Wbuiltin_declaration_mismatch,
1962 "built-in function %q+D declared as non-function",
1963 newdecl);
1964 else
1965 warning (OPT_Wshadow, "declaration of %q+D shadows "
1966 "a built-in function", newdecl);
1967 return false;
1970 /* Enumerators have no linkage, so may only be declared once in a
1971 given scope. */
1972 if (TREE_CODE (olddecl) == CONST_DECL)
1974 auto_diagnostic_group d;
1975 error ("redeclaration of enumerator %q+D", newdecl);
1976 locate_old_decl (olddecl);
1977 return false;
1980 bool pedwarned = false;
1981 bool warned = false;
1982 auto_diagnostic_group d;
1984 if (!comptypes (oldtype, newtype))
1986 if (TREE_CODE (olddecl) == FUNCTION_DECL
1987 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
1988 && !C_DECL_DECLARED_BUILTIN (olddecl))
1990 /* Accept "harmless" mismatches in function types such
1991 as missing qualifiers or int vs long when they're the same
1992 size. However, diagnose return and argument types that are
1993 incompatible according to language rules. */
1994 tree mismatch_expect;
1995 unsigned mismatch_argno;
1997 tree trytype = match_builtin_function_types (newtype, oldtype,
1998 &mismatch_expect,
1999 &mismatch_argno);
2001 if (trytype && comptypes (newtype, trytype))
2002 *oldtypep = oldtype = trytype;
2003 else
2005 /* If types don't match for a built-in, throw away the
2006 built-in. No point in calling locate_old_decl here, it
2007 won't print anything. */
2008 const char *header = header_for_builtin_fn (olddecl);
2009 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2010 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2011 "conflicting types for built-in function %q+D; "
2012 "expected %qT",
2013 newdecl, oldtype)
2014 && header)
2016 /* Suggest the right header to include as the preferred
2017 solution rather than the spelling of the declaration. */
2018 rich_location richloc (line_table, loc);
2019 maybe_add_include_fixit (&richloc, header, true);
2020 inform (&richloc,
2021 "%qD is declared in header %qs", olddecl, header);
2023 return false;
2026 if (mismatch_expect && extra_warnings)
2028 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2029 bool warned = false;
2030 if (mismatch_argno)
2031 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2032 "mismatch in argument %u type of built-in "
2033 "function %qD; expected %qT",
2034 mismatch_argno, newdecl, mismatch_expect);
2035 else
2036 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2037 "mismatch in return type of built-in "
2038 "function %qD; expected %qT",
2039 newdecl, mismatch_expect);
2040 const char *header = header_for_builtin_fn (olddecl);
2041 if (warned && header)
2043 rich_location richloc (line_table, newloc);
2044 maybe_add_include_fixit (&richloc, header, true);
2045 inform (&richloc,
2046 "%qD is declared in header %qs", olddecl, header);
2050 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2051 && DECL_IS_BUILTIN (olddecl))
2053 /* A conflicting function declaration for a predeclared
2054 function that isn't actually built in. Objective C uses
2055 these. The new declaration silently overrides everything
2056 but the volatility (i.e. noreturn) indication. See also
2057 below. FIXME: Make Objective C use normal builtins. */
2058 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2059 return false;
2061 /* Permit void foo (...) to match int foo (...) if the latter is
2062 the definition and implicit int was used. See
2063 c-torture/compile/920625-2.c. */
2064 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2065 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2066 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2067 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2069 pedwarned = pedwarn (input_location, 0,
2070 "conflicting types for %q+D", newdecl);
2071 /* Make sure we keep void as the return type. */
2072 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2073 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2075 /* Permit void foo (...) to match an earlier call to foo (...) with
2076 no declared type (thus, implicitly int). */
2077 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2078 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2079 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2080 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2082 pedwarned = pedwarn (input_location, 0,
2083 "conflicting types for %q+D", newdecl);
2084 /* Make sure we keep void as the return type. */
2085 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2087 else
2089 int new_quals = TYPE_QUALS (newtype);
2090 int old_quals = TYPE_QUALS (oldtype);
2092 if (new_quals != old_quals)
2094 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2095 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2096 if (new_addr != old_addr)
2098 if (ADDR_SPACE_GENERIC_P (new_addr))
2099 error ("conflicting named address spaces (generic vs %s) "
2100 "for %q+D",
2101 c_addr_space_name (old_addr), newdecl);
2102 else if (ADDR_SPACE_GENERIC_P (old_addr))
2103 error ("conflicting named address spaces (%s vs generic) "
2104 "for %q+D",
2105 c_addr_space_name (new_addr), newdecl);
2106 else
2107 error ("conflicting named address spaces (%s vs %s) "
2108 "for %q+D",
2109 c_addr_space_name (new_addr),
2110 c_addr_space_name (old_addr),
2111 newdecl);
2114 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2115 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2116 error ("conflicting type qualifiers for %q+D", newdecl);
2118 else
2119 error ("conflicting types for %q+D", newdecl);
2120 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2121 locate_old_decl (olddecl);
2122 return false;
2126 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2127 but silently ignore the redeclaration if either is in a system
2128 header. (Conflicting redeclarations were handled above.) This
2129 is allowed for C11 if the types are the same, not just
2130 compatible. */
2131 if (TREE_CODE (newdecl) == TYPE_DECL)
2133 bool types_different = false;
2134 int comptypes_result;
2136 comptypes_result
2137 = comptypes_check_different_types (oldtype, newtype, &types_different);
2139 if (comptypes_result != 1 || types_different)
2141 error ("redefinition of typedef %q+D with different type", newdecl);
2142 locate_old_decl (olddecl);
2143 return false;
2146 if (DECL_IN_SYSTEM_HEADER (newdecl)
2147 || DECL_IN_SYSTEM_HEADER (olddecl)
2148 || TREE_NO_WARNING (newdecl)
2149 || TREE_NO_WARNING (olddecl))
2150 return true; /* Allow OLDDECL to continue in use. */
2152 if (variably_modified_type_p (newtype, NULL))
2154 error ("redefinition of typedef %q+D with variably modified type",
2155 newdecl);
2156 locate_old_decl (olddecl);
2158 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2159 "redefinition of typedef %q+D", newdecl))
2160 locate_old_decl (olddecl);
2162 return true;
2165 /* Function declarations can either be 'static' or 'extern' (no
2166 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2167 can never conflict with each other on account of linkage
2168 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2169 gnu89 mode permits two definitions if one is 'extern inline' and
2170 one is not. The non- extern-inline definition supersedes the
2171 extern-inline definition. */
2173 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2175 /* If you declare a built-in function name as static, or
2176 define the built-in with an old-style definition (so we
2177 can't validate the argument list) the built-in definition is
2178 overridden, but optionally warn this was a bad choice of name. */
2179 if (fndecl_built_in_p (olddecl)
2180 && !C_DECL_DECLARED_BUILTIN (olddecl))
2182 if (!TREE_PUBLIC (newdecl)
2183 || (DECL_INITIAL (newdecl)
2184 && !prototype_p (TREE_TYPE (newdecl))))
2186 warning_at (DECL_SOURCE_LOCATION (newdecl),
2187 OPT_Wshadow, "declaration of %qD shadows "
2188 "a built-in function", newdecl);
2189 /* Discard the old built-in function. */
2190 return false;
2193 if (!prototype_p (TREE_TYPE (newdecl)))
2195 /* Set for built-ins that take no arguments. */
2196 bool func_void_args = false;
2197 if (tree at = TYPE_ARG_TYPES (oldtype))
2198 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2200 if (extra_warnings && !func_void_args)
2201 warning_at (DECL_SOURCE_LOCATION (newdecl),
2202 OPT_Wbuiltin_declaration_mismatch,
2203 "declaration of built-in function %qD without "
2204 "a prototype; expected %qT",
2205 newdecl, TREE_TYPE (olddecl));
2209 if (DECL_INITIAL (newdecl))
2211 if (DECL_INITIAL (olddecl))
2213 /* If both decls are in the same TU and the new declaration
2214 isn't overriding an extern inline reject the new decl.
2215 In c99, no overriding is allowed in the same translation
2216 unit. */
2217 if ((!DECL_EXTERN_INLINE (olddecl)
2218 || DECL_EXTERN_INLINE (newdecl)
2219 || (!flag_gnu89_inline
2220 && (!DECL_DECLARED_INLINE_P (olddecl)
2221 || !lookup_attribute ("gnu_inline",
2222 DECL_ATTRIBUTES (olddecl)))
2223 && (!DECL_DECLARED_INLINE_P (newdecl)
2224 || !lookup_attribute ("gnu_inline",
2225 DECL_ATTRIBUTES (newdecl))))
2227 && same_translation_unit_p (newdecl, olddecl))
2229 auto_diagnostic_group d;
2230 error ("redefinition of %q+D", newdecl);
2231 locate_old_decl (olddecl);
2232 return false;
2236 /* If we have a prototype after an old-style function definition,
2237 the argument types must be checked specially. */
2238 else if (DECL_INITIAL (olddecl)
2239 && !prototype_p (oldtype) && prototype_p (newtype)
2240 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2242 auto_diagnostic_group d;
2243 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2245 locate_old_decl (olddecl);
2246 return false;
2249 /* A non-static declaration (even an "extern") followed by a
2250 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2251 The same is true for a static forward declaration at block
2252 scope followed by a non-static declaration/definition at file
2253 scope. Static followed by non-static at the same scope is
2254 not undefined behavior, and is the most convenient way to get
2255 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2256 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2257 we do diagnose it if -Wtraditional. */
2258 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2260 /* Two exceptions to the rule. If olddecl is an extern
2261 inline, or a predeclared function that isn't actually
2262 built in, newdecl silently overrides olddecl. The latter
2263 occur only in Objective C; see also above. (FIXME: Make
2264 Objective C use normal builtins.) */
2265 if (!DECL_IS_BUILTIN (olddecl)
2266 && !DECL_EXTERN_INLINE (olddecl))
2268 auto_diagnostic_group d;
2269 error ("static declaration of %q+D follows "
2270 "non-static declaration", newdecl);
2271 locate_old_decl (olddecl);
2273 return false;
2275 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2277 if (DECL_CONTEXT (olddecl))
2279 auto_diagnostic_group d;
2280 error ("non-static declaration of %q+D follows "
2281 "static declaration", newdecl);
2282 locate_old_decl (olddecl);
2283 return false;
2285 else if (warn_traditional)
2287 warned |= warning (OPT_Wtraditional,
2288 "non-static declaration of %q+D "
2289 "follows static declaration", newdecl);
2293 /* Make sure gnu_inline attribute is either not present, or
2294 present on all inline decls. */
2295 if (DECL_DECLARED_INLINE_P (olddecl)
2296 && DECL_DECLARED_INLINE_P (newdecl))
2298 bool newa = lookup_attribute ("gnu_inline",
2299 DECL_ATTRIBUTES (newdecl)) != NULL;
2300 bool olda = lookup_attribute ("gnu_inline",
2301 DECL_ATTRIBUTES (olddecl)) != NULL;
2302 if (newa != olda)
2304 auto_diagnostic_group d;
2305 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2306 newa ? newdecl : olddecl);
2307 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2308 "but not here");
2312 else if (VAR_P (newdecl))
2314 /* Only variables can be thread-local, and all declarations must
2315 agree on this property. */
2316 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2318 /* Nothing to check. Since OLDDECL is marked threadprivate
2319 and NEWDECL does not have a thread-local attribute, we
2320 will merge the threadprivate attribute into NEWDECL. */
2323 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2325 auto_diagnostic_group d;
2326 if (DECL_THREAD_LOCAL_P (newdecl))
2327 error ("thread-local declaration of %q+D follows "
2328 "non-thread-local declaration", newdecl);
2329 else
2330 error ("non-thread-local declaration of %q+D follows "
2331 "thread-local declaration", newdecl);
2333 locate_old_decl (olddecl);
2334 return false;
2337 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2338 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2340 auto_diagnostic_group d;
2341 error ("redefinition of %q+D", newdecl);
2342 locate_old_decl (olddecl);
2343 return false;
2346 /* Objects declared at file scope: if the first declaration had
2347 external linkage (even if it was an external reference) the
2348 second must have external linkage as well, or the behavior is
2349 undefined. If the first declaration had internal linkage, then
2350 the second must too, or else be an external reference (in which
2351 case the composite declaration still has internal linkage).
2352 As for function declarations, we warn about the static-then-
2353 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2354 if (DECL_FILE_SCOPE_P (newdecl)
2355 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2357 if (DECL_EXTERNAL (newdecl))
2359 if (!DECL_FILE_SCOPE_P (olddecl))
2361 auto_diagnostic_group d;
2362 error ("extern declaration of %q+D follows "
2363 "declaration with no linkage", newdecl);
2364 locate_old_decl (olddecl);
2365 return false;
2367 else if (warn_traditional)
2369 warned |= warning (OPT_Wtraditional,
2370 "non-static declaration of %q+D "
2371 "follows static declaration", newdecl);
2374 else
2376 auto_diagnostic_group d;
2377 if (TREE_PUBLIC (newdecl))
2378 error ("non-static declaration of %q+D follows "
2379 "static declaration", newdecl);
2380 else
2381 error ("static declaration of %q+D follows "
2382 "non-static declaration", newdecl);
2384 locate_old_decl (olddecl);
2385 return false;
2388 /* Two objects with the same name declared at the same block
2389 scope must both be external references (6.7p3). */
2390 else if (!DECL_FILE_SCOPE_P (newdecl))
2392 if (DECL_EXTERNAL (newdecl))
2394 /* Extern with initializer at block scope, which will
2395 already have received an error. */
2397 else if (DECL_EXTERNAL (olddecl))
2399 auto_diagnostic_group d;
2400 error ("declaration of %q+D with no linkage follows "
2401 "extern declaration", newdecl);
2402 locate_old_decl (olddecl);
2404 else
2406 auto_diagnostic_group d;
2407 error ("redeclaration of %q+D with no linkage", newdecl);
2408 locate_old_decl (olddecl);
2411 return false;
2414 /* C++ does not permit a decl to appear multiple times at file
2415 scope. */
2416 if (warn_cxx_compat
2417 && DECL_FILE_SCOPE_P (newdecl)
2418 && !DECL_EXTERNAL (newdecl)
2419 && !DECL_EXTERNAL (olddecl))
2420 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2421 OPT_Wc___compat,
2422 ("duplicate declaration of %qD is "
2423 "invalid in C++"),
2424 newdecl);
2427 /* warnings */
2428 /* All decls must agree on a visibility. */
2429 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2430 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2431 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2433 warned |= warning (0, "redeclaration of %q+D with different visibility "
2434 "(old visibility preserved)", newdecl);
2437 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2438 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2439 else /* PARM_DECL, VAR_DECL */
2441 /* Redeclaration of a parameter is a constraint violation (this is
2442 not explicitly stated, but follows from C99 6.7p3 [no more than
2443 one declaration of the same identifier with no linkage in the
2444 same scope, except type tags] and 6.2.2p6 [parameters have no
2445 linkage]). We must check for a forward parameter declaration,
2446 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2447 an extension, the mandatory diagnostic for which is handled by
2448 mark_forward_parm_decls. */
2450 if (TREE_CODE (newdecl) == PARM_DECL
2451 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2453 auto_diagnostic_group d;
2454 error ("redefinition of parameter %q+D", newdecl);
2455 locate_old_decl (olddecl);
2456 return false;
2460 /* Optional warning for completely redundant decls. */
2461 if (!warned && !pedwarned
2462 && warn_redundant_decls
2463 /* Don't warn about a function declaration followed by a
2464 definition. */
2465 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2466 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2467 /* Don't warn about redundant redeclarations of builtins. */
2468 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2469 && !fndecl_built_in_p (newdecl)
2470 && fndecl_built_in_p (olddecl)
2471 && !C_DECL_DECLARED_BUILTIN (olddecl))
2472 /* Don't warn about an extern followed by a definition. */
2473 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2474 /* Don't warn about forward parameter decls. */
2475 && !(TREE_CODE (newdecl) == PARM_DECL
2476 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2477 /* Don't warn about a variable definition following a declaration. */
2478 && !(VAR_P (newdecl)
2479 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2481 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2482 newdecl);
2485 /* Report location of previous decl/defn. */
2486 if (warned || pedwarned)
2487 locate_old_decl (olddecl);
2489 #undef DECL_EXTERN_INLINE
2491 return retval;
2494 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2495 consistent with OLDDECL, but carries new information. Merge the
2496 new information into OLDDECL. This function issues no
2497 diagnostics. */
2499 static void
2500 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2502 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2503 && DECL_INITIAL (newdecl) != NULL_TREE);
2504 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2505 && prototype_p (TREE_TYPE (newdecl)));
2506 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2507 && prototype_p (TREE_TYPE (olddecl)));
2509 /* For real parm decl following a forward decl, rechain the old decl
2510 in its new location and clear TREE_ASM_WRITTEN (it's not a
2511 forward decl anymore). */
2512 if (TREE_CODE (newdecl) == PARM_DECL
2513 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2515 struct c_binding *b, **here;
2517 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2518 if ((*here)->decl == olddecl)
2519 goto found;
2520 gcc_unreachable ();
2522 found:
2523 b = *here;
2524 *here = b->prev;
2525 b->prev = current_scope->bindings;
2526 current_scope->bindings = b;
2528 TREE_ASM_WRITTEN (olddecl) = 0;
2531 DECL_ATTRIBUTES (newdecl)
2532 = targetm.merge_decl_attributes (olddecl, newdecl);
2534 /* For typedefs use the old type, as the new type's DECL_NAME points
2535 at newdecl, which will be ggc_freed. */
2536 if (TREE_CODE (newdecl) == TYPE_DECL)
2538 /* But NEWTYPE might have an attribute, honor that. */
2539 tree tem = newtype;
2540 newtype = oldtype;
2542 if (TYPE_USER_ALIGN (tem))
2544 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2545 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2546 TYPE_USER_ALIGN (newtype) = true;
2549 /* And remove the new type from the variants list. */
2550 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2552 tree remove = TREE_TYPE (newdecl);
2553 if (TYPE_MAIN_VARIANT (remove) == remove)
2555 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2556 /* If remove is the main variant, no need to remove that
2557 from the list. One of the DECL_ORIGINAL_TYPE
2558 variants, e.g. created for aligned attribute, might still
2559 refer to the newdecl TYPE_DECL though, so remove that one
2560 in that case. */
2561 if (DECL_ORIGINAL_TYPE (newdecl)
2562 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2563 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2564 t; t = TYPE_MAIN_VARIANT (t))
2565 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2567 TYPE_NEXT_VARIANT (t)
2568 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2569 break;
2572 else
2573 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2574 t = TYPE_NEXT_VARIANT (t))
2575 if (TYPE_NEXT_VARIANT (t) == remove)
2577 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2578 break;
2583 /* Merge the data types specified in the two decls. */
2584 TREE_TYPE (newdecl)
2585 = TREE_TYPE (olddecl)
2586 = composite_type (newtype, oldtype);
2588 /* Lay the type out, unless already done. */
2589 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2591 if (TREE_TYPE (newdecl) != error_mark_node)
2592 layout_type (TREE_TYPE (newdecl));
2593 if (TREE_CODE (newdecl) != FUNCTION_DECL
2594 && TREE_CODE (newdecl) != TYPE_DECL
2595 && TREE_CODE (newdecl) != CONST_DECL)
2596 layout_decl (newdecl, 0);
2598 else
2600 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2601 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2602 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2603 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2604 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2606 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2607 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2609 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2610 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2611 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2612 DECL_WARN_IF_NOT_ALIGN (olddecl));
2615 /* Keep the old rtl since we can safely use it. */
2616 if (HAS_RTL_P (olddecl))
2617 COPY_DECL_RTL (olddecl, newdecl);
2619 /* Merge the type qualifiers. */
2620 if (TREE_READONLY (newdecl))
2621 TREE_READONLY (olddecl) = 1;
2623 if (TREE_THIS_VOLATILE (newdecl))
2624 TREE_THIS_VOLATILE (olddecl) = 1;
2626 /* Merge deprecatedness. */
2627 if (TREE_DEPRECATED (newdecl))
2628 TREE_DEPRECATED (olddecl) = 1;
2630 /* If a decl is in a system header and the other isn't, keep the one on the
2631 system header. Otherwise, keep source location of definition rather than
2632 declaration and of prototype rather than non-prototype unless that
2633 prototype is built-in. */
2634 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2635 && DECL_IN_SYSTEM_HEADER (olddecl)
2636 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2637 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2638 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2639 && DECL_IN_SYSTEM_HEADER (newdecl)
2640 && !DECL_IN_SYSTEM_HEADER (olddecl))
2641 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2642 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2643 && DECL_INITIAL (olddecl) != NULL_TREE)
2644 || (old_is_prototype && !new_is_prototype
2645 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2646 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2648 /* Merge the initialization information. */
2649 if (DECL_INITIAL (newdecl) == NULL_TREE)
2650 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2652 /* Merge the threadprivate attribute. */
2653 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2654 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2656 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2658 /* Copy the assembler name.
2659 Currently, it can only be defined in the prototype. */
2660 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2662 /* Use visibility of whichever declaration had it specified */
2663 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2665 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2666 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2669 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2671 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2672 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2673 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2674 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2675 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2676 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2677 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2678 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2679 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2680 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2681 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2682 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2683 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2684 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2687 /* Merge the storage class information. */
2688 merge_weak (newdecl, olddecl);
2690 /* For functions, static overrides non-static. */
2691 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2693 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2694 /* This is since we don't automatically
2695 copy the attributes of NEWDECL into OLDDECL. */
2696 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2697 /* If this clears `static', clear it in the identifier too. */
2698 if (!TREE_PUBLIC (olddecl))
2699 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2703 /* In c99, 'extern' declaration before (or after) 'inline' means this
2704 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2705 is present. */
2706 if (TREE_CODE (newdecl) == FUNCTION_DECL
2707 && !flag_gnu89_inline
2708 && (DECL_DECLARED_INLINE_P (newdecl)
2709 || DECL_DECLARED_INLINE_P (olddecl))
2710 && (!DECL_DECLARED_INLINE_P (newdecl)
2711 || !DECL_DECLARED_INLINE_P (olddecl)
2712 || !DECL_EXTERNAL (olddecl))
2713 && DECL_EXTERNAL (newdecl)
2714 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2715 && !current_function_decl)
2716 DECL_EXTERNAL (newdecl) = 0;
2718 /* An inline definition following a static declaration is not
2719 DECL_EXTERNAL. */
2720 if (new_is_definition
2721 && (DECL_DECLARED_INLINE_P (newdecl)
2722 || DECL_DECLARED_INLINE_P (olddecl))
2723 && !TREE_PUBLIC (olddecl))
2724 DECL_EXTERNAL (newdecl) = 0;
2726 if (DECL_EXTERNAL (newdecl))
2728 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2729 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2731 /* An extern decl does not override previous storage class. */
2732 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2733 if (!DECL_EXTERNAL (newdecl))
2735 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2736 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2739 else
2741 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2742 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2745 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2747 /* If we're redefining a function previously defined as extern
2748 inline, make sure we emit debug info for the inline before we
2749 throw it away, in case it was inlined into a function that
2750 hasn't been written out yet. */
2751 if (new_is_definition && DECL_INITIAL (olddecl))
2752 /* The new defn must not be inline. */
2753 DECL_UNINLINABLE (newdecl) = 1;
2754 else
2756 /* If either decl says `inline', this fn is inline, unless
2757 its definition was passed already. */
2758 if (DECL_DECLARED_INLINE_P (newdecl)
2759 || DECL_DECLARED_INLINE_P (olddecl))
2760 DECL_DECLARED_INLINE_P (newdecl) = 1;
2762 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2763 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2765 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2766 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2767 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2768 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2771 if (fndecl_built_in_p (olddecl))
2773 /* If redeclaring a builtin function, it stays built in.
2774 But it gets tagged as having been declared. */
2775 copy_decl_built_in_function (newdecl, olddecl);
2776 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2777 if (new_is_prototype)
2779 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2780 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2782 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2783 switch (fncode)
2785 /* If a compatible prototype of these builtin functions
2786 is seen, assume the runtime implements it with the
2787 expected semantics. */
2788 case BUILT_IN_STPCPY:
2789 if (builtin_decl_explicit_p (fncode))
2790 set_builtin_decl_implicit_p (fncode, true);
2791 break;
2792 default:
2793 if (builtin_decl_explicit_p (fncode))
2794 set_builtin_decl_declared_p (fncode, true);
2795 break;
2798 copy_attributes_to_builtin (newdecl);
2801 else
2802 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2803 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2806 /* Preserve function specific target and optimization options */
2807 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2808 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2809 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2810 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2812 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2813 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2814 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2815 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2817 /* Also preserve various other info from the definition. */
2818 if (!new_is_definition)
2820 tree t;
2821 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2822 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2823 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2824 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2825 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2826 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2827 DECL_CONTEXT (t) = newdecl;
2829 /* See if we've got a function to instantiate from. */
2830 if (DECL_SAVED_TREE (olddecl))
2831 DECL_ABSTRACT_ORIGIN (newdecl)
2832 = DECL_ABSTRACT_ORIGIN (olddecl);
2836 /* Merge the USED information. */
2837 if (TREE_USED (olddecl))
2838 TREE_USED (newdecl) = 1;
2839 else if (TREE_USED (newdecl))
2840 TREE_USED (olddecl) = 1;
2841 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2842 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2843 if (DECL_PRESERVE_P (olddecl))
2844 DECL_PRESERVE_P (newdecl) = 1;
2845 else if (DECL_PRESERVE_P (newdecl))
2846 DECL_PRESERVE_P (olddecl) = 1;
2848 /* Merge DECL_COMMON */
2849 if (VAR_P (olddecl) && VAR_P (newdecl)
2850 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2851 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2852 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2854 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2855 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2856 DECL_ARGUMENTS (if appropriate). */
2858 unsigned olddecl_uid = DECL_UID (olddecl);
2859 tree olddecl_context = DECL_CONTEXT (olddecl);
2860 tree olddecl_arguments = NULL;
2861 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2862 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2864 memcpy ((char *) olddecl + sizeof (struct tree_common),
2865 (char *) newdecl + sizeof (struct tree_common),
2866 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2867 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2868 switch (TREE_CODE (olddecl))
2870 case FUNCTION_DECL:
2871 case VAR_DECL:
2873 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2875 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2876 (char *) newdecl + sizeof (struct tree_decl_common),
2877 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2878 olddecl->decl_with_vis.symtab_node = snode;
2880 if ((DECL_EXTERNAL (olddecl)
2881 || TREE_PUBLIC (olddecl)
2882 || TREE_STATIC (olddecl))
2883 && DECL_SECTION_NAME (newdecl) != NULL)
2884 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2886 /* This isn't quite correct for something like
2887 int __thread x attribute ((tls_model ("local-exec")));
2888 extern int __thread x;
2889 as we'll lose the "local-exec" model. */
2890 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2891 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2892 break;
2895 case FIELD_DECL:
2896 case PARM_DECL:
2897 case LABEL_DECL:
2898 case RESULT_DECL:
2899 case CONST_DECL:
2900 case TYPE_DECL:
2901 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2902 (char *) newdecl + sizeof (struct tree_decl_common),
2903 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2904 break;
2906 default:
2908 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2909 (char *) newdecl + sizeof (struct tree_decl_common),
2910 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2912 DECL_UID (olddecl) = olddecl_uid;
2913 DECL_CONTEXT (olddecl) = olddecl_context;
2914 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2915 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2918 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2919 so that encode_section_info has a chance to look at the new decl
2920 flags and attributes. */
2921 if (DECL_RTL_SET_P (olddecl)
2922 && (TREE_CODE (olddecl) == FUNCTION_DECL
2923 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2924 make_decl_rtl (olddecl);
2927 /* Handle when a new declaration NEWDECL has the same name as an old
2928 one OLDDECL in the same binding contour. Prints an error message
2929 if appropriate.
2931 If safely possible, alter OLDDECL to look like NEWDECL, and return
2932 true. Otherwise, return false. */
2934 static bool
2935 duplicate_decls (tree newdecl, tree olddecl)
2937 tree newtype = NULL, oldtype = NULL;
2939 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2941 /* Avoid `unused variable' and other warnings for OLDDECL. */
2942 TREE_NO_WARNING (olddecl) = 1;
2943 return false;
2946 merge_decls (newdecl, olddecl, newtype, oldtype);
2948 /* The NEWDECL will no longer be needed.
2950 Before releasing the node, be sure to remove function from symbol
2951 table that might have been inserted there to record comdat group.
2952 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2953 structure is shared in between NEWDECL and OLDECL. */
2954 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2955 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2956 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2958 struct symtab_node *snode = symtab_node::get (newdecl);
2959 if (snode)
2960 snode->remove ();
2962 ggc_free (newdecl);
2963 return true;
2967 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2968 static void
2969 warn_if_shadowing (tree new_decl)
2971 struct c_binding *b;
2973 /* Shadow warnings wanted? */
2974 if (!(warn_shadow
2975 || warn_shadow_local
2976 || warn_shadow_compatible_local)
2977 /* No shadow warnings for internally generated vars. */
2978 || DECL_IS_BUILTIN (new_decl))
2979 return;
2981 /* Is anything being shadowed? Invisible decls do not count. */
2982 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2983 if (b->decl && b->decl != new_decl && !b->invisible
2984 && (b->decl == error_mark_node
2985 || diagnostic_report_warnings_p (global_dc,
2986 DECL_SOURCE_LOCATION (b->decl))))
2988 tree old_decl = b->decl;
2990 if (old_decl == error_mark_node)
2992 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2993 "non-variable", new_decl);
2994 break;
2997 bool warned = false;
2998 auto_diagnostic_group d;
2999 if (TREE_CODE (old_decl) == PARM_DECL)
3001 enum opt_code warning_code;
3003 /* If '-Wshadow=compatible-local' is specified without other
3004 -Wshadow= flags, we will warn only when the types of the
3005 shadowing variable (i.e. new_decl) and the shadowed variable
3006 (old_decl) are compatible. */
3007 if (warn_shadow)
3008 warning_code = OPT_Wshadow;
3009 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3010 warning_code = OPT_Wshadow_compatible_local;
3011 else
3012 warning_code = OPT_Wshadow_local;
3013 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3014 "declaration of %qD shadows a parameter",
3015 new_decl);
3017 else if (DECL_FILE_SCOPE_P (old_decl))
3019 /* Do not warn if a variable shadows a function, unless
3020 the variable is a function or a pointer-to-function. */
3021 if (TREE_CODE (old_decl) == FUNCTION_DECL
3022 && TREE_CODE (new_decl) != FUNCTION_DECL
3023 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3024 continue;
3026 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3027 "declaration of %qD shadows a global "
3028 "declaration",
3029 new_decl);
3031 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3032 && fndecl_built_in_p (old_decl))
3034 warning (OPT_Wshadow, "declaration of %q+D shadows "
3035 "a built-in function", new_decl);
3036 break;
3038 else
3040 enum opt_code warning_code;
3042 /* If '-Wshadow=compatible-local' is specified without other
3043 -Wshadow= flags, we will warn only when the types of the
3044 shadowing variable (i.e. new_decl) and the shadowed variable
3045 (old_decl) are compatible. */
3046 if (warn_shadow)
3047 warning_code = OPT_Wshadow;
3048 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3049 warning_code = OPT_Wshadow_compatible_local;
3050 else
3051 warning_code = OPT_Wshadow_local;
3052 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3053 "declaration of %qD shadows a previous local",
3054 new_decl);
3057 if (warned)
3058 inform (DECL_SOURCE_LOCATION (old_decl),
3059 "shadowed declaration is here");
3061 break;
3065 /* Record a decl-node X as belonging to the current lexical scope.
3066 Check for errors (such as an incompatible declaration for the same
3067 name already seen in the same scope).
3069 Returns either X or an old decl for the same name.
3070 If an old decl is returned, it may have been smashed
3071 to agree with what X says. */
3073 tree
3074 pushdecl (tree x)
3076 tree name = DECL_NAME (x);
3077 struct c_scope *scope = current_scope;
3078 struct c_binding *b;
3079 bool nested = false;
3080 location_t locus = DECL_SOURCE_LOCATION (x);
3082 /* Must set DECL_CONTEXT for everything not at file scope or
3083 DECL_FILE_SCOPE_P won't work. Local externs don't count
3084 unless they have initializers (which generate code). */
3085 if (current_function_decl
3086 && (!VAR_OR_FUNCTION_DECL_P (x)
3087 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3088 DECL_CONTEXT (x) = current_function_decl;
3090 /* Anonymous decls are just inserted in the scope. */
3091 if (!name)
3093 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3094 locus);
3095 return x;
3098 /* First, see if there is another declaration with the same name in
3099 the current scope. If there is, duplicate_decls may do all the
3100 work for us. If duplicate_decls returns false, that indicates
3101 two incompatible decls in the same scope; we are to silently
3102 replace the old one (duplicate_decls has issued all appropriate
3103 diagnostics). In particular, we should not consider possible
3104 duplicates in the external scope, or shadowing. */
3105 b = I_SYMBOL_BINDING (name);
3106 if (b && B_IN_SCOPE (b, scope))
3108 struct c_binding *b_ext, *b_use;
3109 tree type = TREE_TYPE (x);
3110 tree visdecl = b->decl;
3111 tree vistype = TREE_TYPE (visdecl);
3112 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3113 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3114 b->inner_comp = false;
3115 b_use = b;
3116 b_ext = b;
3117 /* If this is an external linkage declaration, we should check
3118 for compatibility with the type in the external scope before
3119 setting the type at this scope based on the visible
3120 information only. */
3121 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3123 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3124 b_ext = b_ext->shadowed;
3125 if (b_ext)
3127 b_use = b_ext;
3128 if (b_use->u.type)
3129 TREE_TYPE (b_use->decl) = b_use->u.type;
3132 if (duplicate_decls (x, b_use->decl))
3134 if (b_use != b)
3136 /* Save the updated type in the external scope and
3137 restore the proper type for this scope. */
3138 tree thistype;
3139 if (comptypes (vistype, type))
3140 thistype = composite_type (vistype, type);
3141 else
3142 thistype = TREE_TYPE (b_use->decl);
3143 b_use->u.type = TREE_TYPE (b_use->decl);
3144 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3145 && fndecl_built_in_p (b_use->decl))
3146 thistype
3147 = build_type_attribute_variant (thistype,
3148 TYPE_ATTRIBUTES
3149 (b_use->u.type));
3150 TREE_TYPE (b_use->decl) = thistype;
3152 return b_use->decl;
3154 else
3155 goto skip_external_and_shadow_checks;
3158 /* All declarations with external linkage, and all external
3159 references, go in the external scope, no matter what scope is
3160 current. However, the binding in that scope is ignored for
3161 purposes of normal name lookup. A separate binding structure is
3162 created in the requested scope; this governs the normal
3163 visibility of the symbol.
3165 The binding in the externals scope is used exclusively for
3166 detecting duplicate declarations of the same object, no matter
3167 what scope they are in; this is what we do here. (C99 6.2.7p2:
3168 All declarations that refer to the same object or function shall
3169 have compatible type; otherwise, the behavior is undefined.)
3170 However, in Objective-C, we also want to detect declarations
3171 conflicting with those of the basic types. */
3172 if ((DECL_EXTERNAL (x) || scope == file_scope)
3173 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3175 tree type = TREE_TYPE (x);
3176 tree vistype = NULL_TREE;
3177 tree visdecl = NULL_TREE;
3178 bool type_saved = false;
3179 if (b && !B_IN_EXTERNAL_SCOPE (b)
3180 && VAR_OR_FUNCTION_DECL_P (b->decl)
3181 && DECL_FILE_SCOPE_P (b->decl))
3183 visdecl = b->decl;
3184 vistype = TREE_TYPE (visdecl);
3186 if (scope != file_scope
3187 && !DECL_IN_SYSTEM_HEADER (x))
3188 warning_at (locus, OPT_Wnested_externs,
3189 "nested extern declaration of %qD", x);
3191 while (b && !B_IN_EXTERNAL_SCOPE (b))
3193 /* If this decl might be modified, save its type. This is
3194 done here rather than when the decl is first bound
3195 because the type may change after first binding, through
3196 being completed or through attributes being added. If we
3197 encounter multiple such decls, only the first should have
3198 its type saved; the others will already have had their
3199 proper types saved and the types will not have changed as
3200 their scopes will not have been re-entered. */
3201 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3203 b->u.type = TREE_TYPE (b->decl);
3204 type_saved = true;
3206 if (B_IN_FILE_SCOPE (b)
3207 && VAR_P (b->decl)
3208 && TREE_STATIC (b->decl)
3209 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3210 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3211 && TREE_CODE (type) == ARRAY_TYPE
3212 && TYPE_DOMAIN (type)
3213 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3214 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3216 /* Array type completed in inner scope, which should be
3217 diagnosed if the completion does not have size 1 and
3218 it does not get completed in the file scope. */
3219 b->inner_comp = true;
3221 b = b->shadowed;
3224 /* If a matching external declaration has been found, set its
3225 type to the composite of all the types of that declaration.
3226 After the consistency checks, it will be reset to the
3227 composite of the visible types only. */
3228 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3229 && b->u.type)
3230 TREE_TYPE (b->decl) = b->u.type;
3232 /* The point of the same_translation_unit_p check here is,
3233 we want to detect a duplicate decl for a construct like
3234 foo() { extern bar(); } ... static bar(); but not if
3235 they are in different translation units. In any case,
3236 the static does not go in the externals scope. */
3237 if (b
3238 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3239 && duplicate_decls (x, b->decl))
3241 tree thistype;
3242 if (vistype)
3244 if (comptypes (vistype, type))
3245 thistype = composite_type (vistype, type);
3246 else
3247 thistype = TREE_TYPE (b->decl);
3249 else
3250 thistype = type;
3251 b->u.type = TREE_TYPE (b->decl);
3252 if (TREE_CODE (b->decl) == FUNCTION_DECL
3253 && fndecl_built_in_p (b->decl))
3254 thistype
3255 = build_type_attribute_variant (thistype,
3256 TYPE_ATTRIBUTES (b->u.type));
3257 TREE_TYPE (b->decl) = thistype;
3258 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3259 locus);
3260 return b->decl;
3262 else if (TREE_PUBLIC (x))
3264 if (visdecl && !b && duplicate_decls (x, visdecl))
3266 /* An external declaration at block scope referring to a
3267 visible entity with internal linkage. The composite
3268 type will already be correct for this scope, so we
3269 just need to fall through to make the declaration in
3270 this scope. */
3271 nested = true;
3272 x = visdecl;
3274 else
3276 bind (name, x, external_scope, /*invisible=*/true,
3277 /*nested=*/false, locus);
3278 nested = true;
3283 if (TREE_CODE (x) != PARM_DECL)
3284 warn_if_shadowing (x);
3286 skip_external_and_shadow_checks:
3287 if (TREE_CODE (x) == TYPE_DECL)
3289 /* So this is a typedef, set its underlying type. */
3290 set_underlying_type (x);
3292 /* If X is a typedef defined in the current function, record it
3293 for the purpose of implementing the -Wunused-local-typedefs
3294 warning. */
3295 record_locally_defined_typedef (x);
3298 bind (name, x, scope, /*invisible=*/false, nested, locus);
3300 /* If x's type is incomplete because it's based on a
3301 structure or union which has not yet been fully declared,
3302 attach it to that structure or union type, so we can go
3303 back and complete the variable declaration later, if the
3304 structure or union gets fully declared.
3306 If the input is erroneous, we can have error_mark in the type
3307 slot (e.g. "f(void a, ...)") - that doesn't count as an
3308 incomplete type. */
3309 if (TREE_TYPE (x) != error_mark_node
3310 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3312 tree element = TREE_TYPE (x);
3314 while (TREE_CODE (element) == ARRAY_TYPE)
3315 element = TREE_TYPE (element);
3316 element = TYPE_MAIN_VARIANT (element);
3318 if ((RECORD_OR_UNION_TYPE_P (element)
3319 || TREE_CODE (element) == ENUMERAL_TYPE)
3320 && (TREE_CODE (x) != TYPE_DECL
3321 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3322 && !COMPLETE_TYPE_P (element))
3323 C_TYPE_INCOMPLETE_VARS (element)
3324 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3326 return x;
3330 /* Issue a warning about implicit function declaration. ID is the function
3331 identifier, OLDDECL is a declaration of the function in a different scope,
3332 or NULL_TREE. */
3334 static void
3335 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3337 if (!warn_implicit_function_declaration)
3338 return;
3340 bool warned;
3341 auto_diagnostic_group d;
3342 name_hint hint;
3343 if (!olddecl)
3344 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3346 if (flag_isoc99)
3348 if (const char *suggestion = hint.suggestion ())
3350 gcc_rich_location richloc (loc);
3351 richloc.add_fixit_replace (suggestion);
3352 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3353 "implicit declaration of function %qE;"
3354 " did you mean %qs?",
3355 id, suggestion);
3357 else
3358 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3359 "implicit declaration of function %qE", id);
3361 else if (const char *suggestion = hint.suggestion ())
3363 gcc_rich_location richloc (loc);
3364 richloc.add_fixit_replace (suggestion);
3365 warned = warning_at
3366 (&richloc, OPT_Wimplicit_function_declaration,
3367 G_("implicit declaration of function %qE; did you mean %qs?"),
3368 id, suggestion);
3370 else
3371 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3372 G_("implicit declaration of function %qE"), id);
3374 if (warned)
3376 /* Whether the olddecl is an undeclared builtin function.
3377 locate_old_decl will not generate a diagnostic for those,
3378 so in that case we want to look elsewhere. */
3379 bool undeclared_builtin = (olddecl
3380 && TREE_CODE (olddecl) == FUNCTION_DECL
3381 && fndecl_built_in_p (olddecl)
3382 && !C_DECL_DECLARED_BUILTIN (olddecl));
3383 if (undeclared_builtin)
3385 const char *header = header_for_builtin_fn (olddecl);
3386 if (header)
3388 rich_location richloc (line_table, loc);
3389 maybe_add_include_fixit (&richloc, header, true);
3390 inform (&richloc,
3391 "include %qs or provide a declaration of %qE",
3392 header, id);
3395 else if (olddecl)
3396 locate_old_decl (olddecl);
3399 if (!warned)
3400 hint.suppress ();
3403 /* Return the name of the header file that declares built-in function
3404 FNDECL, or null if either we don't know or don't expect to see an
3405 explicit declaration. */
3407 static const char *
3408 header_for_builtin_fn (tree fndecl)
3410 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3411 return NULL;
3413 switch (DECL_FUNCTION_CODE (fndecl))
3415 CASE_FLT_FN (BUILT_IN_ACOS):
3416 CASE_FLT_FN (BUILT_IN_ACOSH):
3417 CASE_FLT_FN (BUILT_IN_ASIN):
3418 CASE_FLT_FN (BUILT_IN_ASINH):
3419 CASE_FLT_FN (BUILT_IN_ATAN):
3420 CASE_FLT_FN (BUILT_IN_ATANH):
3421 CASE_FLT_FN (BUILT_IN_ATAN2):
3422 CASE_FLT_FN (BUILT_IN_CBRT):
3423 CASE_FLT_FN (BUILT_IN_CEIL):
3424 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3425 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3426 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3427 CASE_FLT_FN (BUILT_IN_COS):
3428 CASE_FLT_FN (BUILT_IN_COSH):
3429 CASE_FLT_FN (BUILT_IN_ERF):
3430 CASE_FLT_FN (BUILT_IN_ERFC):
3431 CASE_FLT_FN (BUILT_IN_EXP):
3432 CASE_FLT_FN (BUILT_IN_EXP2):
3433 CASE_FLT_FN (BUILT_IN_EXPM1):
3434 CASE_FLT_FN (BUILT_IN_FABS):
3435 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3436 CASE_FLT_FN (BUILT_IN_FDIM):
3437 CASE_FLT_FN (BUILT_IN_FLOOR):
3438 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3439 CASE_FLT_FN (BUILT_IN_FMA):
3440 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3441 CASE_FLT_FN (BUILT_IN_FMAX):
3442 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3443 CASE_FLT_FN (BUILT_IN_FMIN):
3444 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3445 CASE_FLT_FN (BUILT_IN_FMOD):
3446 CASE_FLT_FN (BUILT_IN_FREXP):
3447 CASE_FLT_FN (BUILT_IN_HYPOT):
3448 CASE_FLT_FN (BUILT_IN_ILOGB):
3449 CASE_FLT_FN (BUILT_IN_LDEXP):
3450 CASE_FLT_FN (BUILT_IN_LGAMMA):
3451 CASE_FLT_FN (BUILT_IN_LLRINT):
3452 CASE_FLT_FN (BUILT_IN_LLROUND):
3453 CASE_FLT_FN (BUILT_IN_LOG):
3454 CASE_FLT_FN (BUILT_IN_LOG10):
3455 CASE_FLT_FN (BUILT_IN_LOG1P):
3456 CASE_FLT_FN (BUILT_IN_LOG2):
3457 CASE_FLT_FN (BUILT_IN_LOGB):
3458 CASE_FLT_FN (BUILT_IN_LRINT):
3459 CASE_FLT_FN (BUILT_IN_LROUND):
3460 CASE_FLT_FN (BUILT_IN_MODF):
3461 CASE_FLT_FN (BUILT_IN_NAN):
3462 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3463 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3464 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3465 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3466 CASE_FLT_FN (BUILT_IN_POW):
3467 CASE_FLT_FN (BUILT_IN_REMAINDER):
3468 CASE_FLT_FN (BUILT_IN_REMQUO):
3469 CASE_FLT_FN (BUILT_IN_RINT):
3470 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3471 CASE_FLT_FN (BUILT_IN_ROUND):
3472 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3473 CASE_FLT_FN (BUILT_IN_SCALBLN):
3474 CASE_FLT_FN (BUILT_IN_SCALBN):
3475 CASE_FLT_FN (BUILT_IN_SIN):
3476 CASE_FLT_FN (BUILT_IN_SINH):
3477 CASE_FLT_FN (BUILT_IN_SINCOS):
3478 CASE_FLT_FN (BUILT_IN_SQRT):
3479 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3480 CASE_FLT_FN (BUILT_IN_TAN):
3481 CASE_FLT_FN (BUILT_IN_TANH):
3482 CASE_FLT_FN (BUILT_IN_TGAMMA):
3483 CASE_FLT_FN (BUILT_IN_TRUNC):
3484 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3485 case BUILT_IN_ISINF:
3486 case BUILT_IN_ISNAN:
3487 return "<math.h>";
3488 CASE_FLT_FN (BUILT_IN_CABS):
3489 CASE_FLT_FN (BUILT_IN_CACOS):
3490 CASE_FLT_FN (BUILT_IN_CACOSH):
3491 CASE_FLT_FN (BUILT_IN_CARG):
3492 CASE_FLT_FN (BUILT_IN_CASIN):
3493 CASE_FLT_FN (BUILT_IN_CASINH):
3494 CASE_FLT_FN (BUILT_IN_CATAN):
3495 CASE_FLT_FN (BUILT_IN_CATANH):
3496 CASE_FLT_FN (BUILT_IN_CCOS):
3497 CASE_FLT_FN (BUILT_IN_CCOSH):
3498 CASE_FLT_FN (BUILT_IN_CEXP):
3499 CASE_FLT_FN (BUILT_IN_CIMAG):
3500 CASE_FLT_FN (BUILT_IN_CLOG):
3501 CASE_FLT_FN (BUILT_IN_CONJ):
3502 CASE_FLT_FN (BUILT_IN_CPOW):
3503 CASE_FLT_FN (BUILT_IN_CPROJ):
3504 CASE_FLT_FN (BUILT_IN_CREAL):
3505 CASE_FLT_FN (BUILT_IN_CSIN):
3506 CASE_FLT_FN (BUILT_IN_CSINH):
3507 CASE_FLT_FN (BUILT_IN_CSQRT):
3508 CASE_FLT_FN (BUILT_IN_CTAN):
3509 CASE_FLT_FN (BUILT_IN_CTANH):
3510 return "<complex.h>";
3511 case BUILT_IN_MEMCHR:
3512 case BUILT_IN_MEMCMP:
3513 case BUILT_IN_MEMCPY:
3514 case BUILT_IN_MEMMOVE:
3515 case BUILT_IN_MEMSET:
3516 case BUILT_IN_STRCAT:
3517 case BUILT_IN_STRCHR:
3518 case BUILT_IN_STRCMP:
3519 case BUILT_IN_STRCPY:
3520 case BUILT_IN_STRCSPN:
3521 case BUILT_IN_STRLEN:
3522 case BUILT_IN_STRNCAT:
3523 case BUILT_IN_STRNCMP:
3524 case BUILT_IN_STRNCPY:
3525 case BUILT_IN_STRPBRK:
3526 case BUILT_IN_STRRCHR:
3527 case BUILT_IN_STRSPN:
3528 case BUILT_IN_STRSTR:
3529 return "<string.h>";
3530 case BUILT_IN_FPRINTF:
3531 case BUILT_IN_PUTC:
3532 case BUILT_IN_FPUTC:
3533 case BUILT_IN_FPUTS:
3534 case BUILT_IN_FSCANF:
3535 case BUILT_IN_FWRITE:
3536 case BUILT_IN_PRINTF:
3537 case BUILT_IN_PUTCHAR:
3538 case BUILT_IN_PUTS:
3539 case BUILT_IN_SCANF:
3540 case BUILT_IN_SNPRINTF:
3541 case BUILT_IN_SPRINTF:
3542 case BUILT_IN_SSCANF:
3543 case BUILT_IN_VFPRINTF:
3544 case BUILT_IN_VFSCANF:
3545 case BUILT_IN_VPRINTF:
3546 case BUILT_IN_VSCANF:
3547 case BUILT_IN_VSNPRINTF:
3548 case BUILT_IN_VSPRINTF:
3549 case BUILT_IN_VSSCANF:
3550 return "<stdio.h>";
3551 case BUILT_IN_ISALNUM:
3552 case BUILT_IN_ISALPHA:
3553 case BUILT_IN_ISBLANK:
3554 case BUILT_IN_ISCNTRL:
3555 case BUILT_IN_ISDIGIT:
3556 case BUILT_IN_ISGRAPH:
3557 case BUILT_IN_ISLOWER:
3558 case BUILT_IN_ISPRINT:
3559 case BUILT_IN_ISPUNCT:
3560 case BUILT_IN_ISSPACE:
3561 case BUILT_IN_ISUPPER:
3562 case BUILT_IN_ISXDIGIT:
3563 case BUILT_IN_TOLOWER:
3564 case BUILT_IN_TOUPPER:
3565 return "<ctype.h>";
3566 case BUILT_IN_ISWALNUM:
3567 case BUILT_IN_ISWALPHA:
3568 case BUILT_IN_ISWBLANK:
3569 case BUILT_IN_ISWCNTRL:
3570 case BUILT_IN_ISWDIGIT:
3571 case BUILT_IN_ISWGRAPH:
3572 case BUILT_IN_ISWLOWER:
3573 case BUILT_IN_ISWPRINT:
3574 case BUILT_IN_ISWPUNCT:
3575 case BUILT_IN_ISWSPACE:
3576 case BUILT_IN_ISWUPPER:
3577 case BUILT_IN_ISWXDIGIT:
3578 case BUILT_IN_TOWLOWER:
3579 case BUILT_IN_TOWUPPER:
3580 return "<wctype.h>";
3581 case BUILT_IN_ABORT:
3582 case BUILT_IN_ABS:
3583 case BUILT_IN_CALLOC:
3584 case BUILT_IN_EXIT:
3585 case BUILT_IN_FREE:
3586 case BUILT_IN_LABS:
3587 case BUILT_IN_LLABS:
3588 case BUILT_IN_MALLOC:
3589 case BUILT_IN_REALLOC:
3590 case BUILT_IN__EXIT2:
3591 case BUILT_IN_ALIGNED_ALLOC:
3592 return "<stdlib.h>";
3593 case BUILT_IN_IMAXABS:
3594 return "<inttypes.h>";
3595 case BUILT_IN_STRFTIME:
3596 return "<time.h>";
3597 default:
3598 return NULL;
3602 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3603 function of type int (). */
3605 tree
3606 implicitly_declare (location_t loc, tree functionid)
3608 struct c_binding *b;
3609 tree decl = NULL_TREE;
3610 tree asmspec_tree;
3612 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3614 if (B_IN_SCOPE (b, external_scope))
3616 decl = b->decl;
3617 break;
3621 if (decl)
3623 if (TREE_CODE (decl) != FUNCTION_DECL)
3624 return decl;
3626 /* FIXME: Objective-C has weird not-really-builtin functions
3627 which are supposed to be visible automatically. They wind up
3628 in the external scope because they're pushed before the file
3629 scope gets created. Catch this here and rebind them into the
3630 file scope. */
3631 if (!fndecl_built_in_p (decl) && DECL_IS_BUILTIN (decl))
3633 bind (functionid, decl, file_scope,
3634 /*invisible=*/false, /*nested=*/true,
3635 DECL_SOURCE_LOCATION (decl));
3636 return decl;
3638 else
3640 tree newtype = default_function_type;
3641 if (b->u.type)
3642 TREE_TYPE (decl) = b->u.type;
3643 /* Implicit declaration of a function already declared
3644 (somehow) in a different scope, or as a built-in.
3645 If this is the first time this has happened, warn;
3646 then recycle the old declaration but with the new type. */
3647 if (!C_DECL_IMPLICIT (decl))
3649 implicit_decl_warning (loc, functionid, decl);
3650 C_DECL_IMPLICIT (decl) = 1;
3652 if (fndecl_built_in_p (decl))
3654 newtype = build_type_attribute_variant (newtype,
3655 TYPE_ATTRIBUTES
3656 (TREE_TYPE (decl)));
3657 if (!comptypes (newtype, TREE_TYPE (decl)))
3659 bool warned = warning_at (loc,
3660 OPT_Wbuiltin_declaration_mismatch,
3661 "incompatible implicit "
3662 "declaration of built-in "
3663 "function %qD", decl);
3664 /* See if we can hint which header to include. */
3665 const char *header = header_for_builtin_fn (decl);
3666 if (header != NULL && warned)
3668 rich_location richloc (line_table, loc);
3669 maybe_add_include_fixit (&richloc, header, true);
3670 inform (&richloc,
3671 "include %qs or provide a declaration of %qD",
3672 header, decl);
3674 newtype = TREE_TYPE (decl);
3677 else
3679 if (!comptypes (newtype, TREE_TYPE (decl)))
3681 auto_diagnostic_group d;
3682 error_at (loc, "incompatible implicit declaration of "
3683 "function %qD", decl);
3684 locate_old_decl (decl);
3687 b->u.type = TREE_TYPE (decl);
3688 TREE_TYPE (decl) = newtype;
3689 bind (functionid, decl, current_scope,
3690 /*invisible=*/false, /*nested=*/true,
3691 DECL_SOURCE_LOCATION (decl));
3692 return decl;
3696 /* Not seen before. */
3697 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3698 DECL_EXTERNAL (decl) = 1;
3699 TREE_PUBLIC (decl) = 1;
3700 C_DECL_IMPLICIT (decl) = 1;
3701 implicit_decl_warning (loc, functionid, 0);
3702 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3703 if (asmspec_tree)
3704 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3706 /* C89 says implicit declarations are in the innermost block.
3707 So we record the decl in the standard fashion. */
3708 decl = pushdecl (decl);
3710 /* No need to call objc_check_decl here - it's a function type. */
3711 rest_of_decl_compilation (decl, 0, 0);
3713 /* Write a record describing this implicit function declaration
3714 to the prototypes file (if requested). */
3715 gen_aux_info_record (decl, 0, 1, 0);
3717 /* Possibly apply some default attributes to this implicit declaration. */
3718 decl_attributes (&decl, NULL_TREE, 0);
3720 return decl;
3723 /* Issue an error message for a reference to an undeclared variable
3724 ID, including a reference to a builtin outside of function-call
3725 context. Establish a binding of the identifier to error_mark_node
3726 in an appropriate scope, which will suppress further errors for the
3727 same identifier. The error message should be given location LOC. */
3728 void
3729 undeclared_variable (location_t loc, tree id)
3731 static bool already = false;
3732 struct c_scope *scope;
3734 auto_diagnostic_group d;
3735 if (current_function_decl == NULL_TREE)
3737 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3738 if (const char *suggestion = guessed_id.suggestion ())
3740 gcc_rich_location richloc (loc);
3741 richloc.add_fixit_replace (suggestion);
3742 error_at (&richloc,
3743 "%qE undeclared here (not in a function);"
3744 " did you mean %qs?",
3745 id, suggestion);
3747 else
3748 error_at (loc, "%qE undeclared here (not in a function)", id);
3749 scope = current_scope;
3751 else
3753 if (!objc_diagnose_private_ivar (id))
3755 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3756 if (const char *suggestion = guessed_id.suggestion ())
3758 gcc_rich_location richloc (loc);
3759 richloc.add_fixit_replace (suggestion);
3760 error_at (&richloc,
3761 "%qE undeclared (first use in this function);"
3762 " did you mean %qs?",
3763 id, suggestion);
3765 else
3766 error_at (loc, "%qE undeclared (first use in this function)", id);
3768 if (!already)
3770 inform (loc, "each undeclared identifier is reported only"
3771 " once for each function it appears in");
3772 already = true;
3775 /* If we are parsing old-style parameter decls, current_function_decl
3776 will be nonnull but current_function_scope will be null. */
3777 scope = current_function_scope ? current_function_scope : current_scope;
3779 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3780 UNKNOWN_LOCATION);
3783 /* Subroutine of lookup_label, declare_label, define_label: construct a
3784 LABEL_DECL with all the proper frills. Also create a struct
3785 c_label_vars initialized for the current scope. */
3787 static tree
3788 make_label (location_t location, tree name, bool defining,
3789 struct c_label_vars **p_label_vars)
3791 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3792 DECL_CONTEXT (label) = current_function_decl;
3793 SET_DECL_MODE (label, VOIDmode);
3795 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3796 label_vars->shadowed = NULL;
3797 set_spot_bindings (&label_vars->label_bindings, defining);
3798 label_vars->decls_in_scope = make_tree_vector ();
3799 label_vars->gotos = NULL;
3800 *p_label_vars = label_vars;
3802 return label;
3805 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3806 Create one if none exists so far for the current function.
3807 This is called when a label is used in a goto expression or
3808 has its address taken. */
3810 tree
3811 lookup_label (tree name)
3813 tree label;
3814 struct c_label_vars *label_vars;
3816 if (current_function_scope == 0)
3818 error ("label %qE referenced outside of any function", name);
3819 return NULL_TREE;
3822 /* Use a label already defined or ref'd with this name, but not if
3823 it is inherited from a containing function and wasn't declared
3824 using __label__. */
3825 label = I_LABEL_DECL (name);
3826 if (label && (DECL_CONTEXT (label) == current_function_decl
3827 || C_DECLARED_LABEL_FLAG (label)))
3829 /* If the label has only been declared, update its apparent
3830 location to point here, for better diagnostics if it
3831 turns out not to have been defined. */
3832 if (DECL_INITIAL (label) == NULL_TREE)
3833 DECL_SOURCE_LOCATION (label) = input_location;
3834 return label;
3837 /* No label binding for that identifier; make one. */
3838 label = make_label (input_location, name, false, &label_vars);
3840 /* Ordinary labels go in the current function scope. */
3841 bind_label (name, label, current_function_scope, label_vars);
3843 return label;
3846 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3847 to LABEL. */
3849 static void
3850 warn_about_goto (location_t goto_loc, tree label, tree decl)
3852 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3853 error_at (goto_loc,
3854 "jump into scope of identifier with variably modified type");
3855 else
3856 warning_at (goto_loc, OPT_Wjump_misses_init,
3857 "jump skips variable initialization");
3858 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3859 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3862 /* Look up a label because of a goto statement. This is like
3863 lookup_label, but also issues any appropriate warnings. */
3865 tree
3866 lookup_label_for_goto (location_t loc, tree name)
3868 tree label;
3869 struct c_label_vars *label_vars;
3870 unsigned int ix;
3871 tree decl;
3873 label = lookup_label (name);
3874 if (label == NULL_TREE)
3875 return NULL_TREE;
3877 /* If we are jumping to a different function, we can't issue any
3878 useful warnings. */
3879 if (DECL_CONTEXT (label) != current_function_decl)
3881 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3882 return label;
3885 label_vars = I_LABEL_BINDING (name)->u.label;
3887 /* If the label has not yet been defined, then push this goto on a
3888 list for possible later warnings. */
3889 if (label_vars->label_bindings.scope == NULL)
3891 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3893 g->loc = loc;
3894 set_spot_bindings (&g->goto_bindings, true);
3895 vec_safe_push (label_vars->gotos, g);
3896 return label;
3899 /* If there are any decls in label_vars->decls_in_scope, then this
3900 goto has missed the declaration of the decl. This happens for a
3901 case like
3902 int i = 1;
3903 lab:
3905 goto lab;
3906 Issue a warning or error. */
3907 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3908 warn_about_goto (loc, label, decl);
3910 if (label_vars->label_bindings.left_stmt_expr)
3912 error_at (loc, "jump into statement expression");
3913 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3916 return label;
3919 /* Make a label named NAME in the current function, shadowing silently
3920 any that may be inherited from containing functions or containing
3921 scopes. This is called for __label__ declarations. */
3923 tree
3924 declare_label (tree name)
3926 struct c_binding *b = I_LABEL_BINDING (name);
3927 tree label;
3928 struct c_label_vars *label_vars;
3930 /* Check to make sure that the label hasn't already been declared
3931 at this scope */
3932 if (b && B_IN_CURRENT_SCOPE (b))
3934 auto_diagnostic_group d;
3935 error ("duplicate label declaration %qE", name);
3936 locate_old_decl (b->decl);
3938 /* Just use the previous declaration. */
3939 return b->decl;
3942 label = make_label (input_location, name, false, &label_vars);
3943 C_DECLARED_LABEL_FLAG (label) = 1;
3945 /* Declared labels go in the current scope. */
3946 bind_label (name, label, current_scope, label_vars);
3948 return label;
3951 /* When we define a label, issue any appropriate warnings if there are
3952 any gotos earlier in the function which jump to this label. */
3954 static void
3955 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3957 unsigned int ix;
3958 struct c_goto_bindings *g;
3960 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3962 struct c_binding *b;
3963 struct c_scope *scope;
3965 /* We have a goto to this label. The goto is going forward. In
3966 g->scope, the goto is going to skip any binding which was
3967 defined after g->bindings_in_scope. */
3968 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3970 for (b = g->goto_bindings.scope->bindings;
3971 b != g->goto_bindings.bindings_in_scope;
3972 b = b->prev)
3974 if (decl_jump_unsafe (b->decl))
3975 warn_about_goto (g->loc, label, b->decl);
3979 /* We also need to warn about decls defined in any scopes
3980 between the scope of the label and the scope of the goto. */
3981 for (scope = label_vars->label_bindings.scope;
3982 scope != g->goto_bindings.scope;
3983 scope = scope->outer)
3985 gcc_assert (scope != NULL);
3986 if (scope->has_jump_unsafe_decl)
3988 if (scope == label_vars->label_bindings.scope)
3989 b = label_vars->label_bindings.bindings_in_scope;
3990 else
3991 b = scope->bindings;
3992 for (; b != NULL; b = b->prev)
3994 if (decl_jump_unsafe (b->decl))
3995 warn_about_goto (g->loc, label, b->decl);
4000 if (g->goto_bindings.stmt_exprs > 0)
4002 error_at (g->loc, "jump into statement expression");
4003 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4004 label);
4008 /* Now that the label is defined, we will issue warnings about
4009 subsequent gotos to this label when we see them. */
4010 vec_safe_truncate (label_vars->gotos, 0);
4011 label_vars->gotos = NULL;
4014 /* Define a label, specifying the location in the source file.
4015 Return the LABEL_DECL node for the label, if the definition is valid.
4016 Otherwise return NULL_TREE. */
4018 tree
4019 define_label (location_t location, tree name)
4021 /* Find any preexisting label with this name. It is an error
4022 if that label has already been defined in this function, or
4023 if there is a containing function with a declared label with
4024 the same name. */
4025 tree label = I_LABEL_DECL (name);
4027 if (label
4028 && ((DECL_CONTEXT (label) == current_function_decl
4029 && DECL_INITIAL (label) != NULL_TREE)
4030 || (DECL_CONTEXT (label) != current_function_decl
4031 && C_DECLARED_LABEL_FLAG (label))))
4033 auto_diagnostic_group d;
4034 error_at (location, "duplicate label %qD", label);
4035 locate_old_decl (label);
4036 return NULL_TREE;
4038 else if (label && DECL_CONTEXT (label) == current_function_decl)
4040 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4042 /* The label has been used or declared already in this function,
4043 but not defined. Update its location to point to this
4044 definition. */
4045 DECL_SOURCE_LOCATION (label) = location;
4046 set_spot_bindings (&label_vars->label_bindings, true);
4048 /* Issue warnings as required about any goto statements from
4049 earlier in the function. */
4050 check_earlier_gotos (label, label_vars);
4052 else
4054 struct c_label_vars *label_vars;
4056 /* No label binding for that identifier; make one. */
4057 label = make_label (location, name, true, &label_vars);
4059 /* Ordinary labels go in the current function scope. */
4060 bind_label (name, label, current_function_scope, label_vars);
4063 if (!in_system_header_at (input_location) && lookup_name (name))
4064 warning_at (location, OPT_Wtraditional,
4065 "traditional C lacks a separate namespace "
4066 "for labels, identifier %qE conflicts", name);
4068 /* Mark label as having been defined. */
4069 DECL_INITIAL (label) = error_mark_node;
4070 return label;
4073 /* Get the bindings for a new switch statement. This is used to issue
4074 warnings as appropriate for jumps from the switch to case or
4075 default labels. */
4077 struct c_spot_bindings *
4078 c_get_switch_bindings (void)
4080 struct c_spot_bindings *switch_bindings;
4082 switch_bindings = XNEW (struct c_spot_bindings);
4083 set_spot_bindings (switch_bindings, true);
4084 return switch_bindings;
4087 void
4088 c_release_switch_bindings (struct c_spot_bindings *bindings)
4090 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4091 XDELETE (bindings);
4094 /* This is called at the point of a case or default label to issue
4095 warnings about decls as needed. It returns true if it found an
4096 error, not just a warning. */
4098 bool
4099 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4100 location_t switch_loc, location_t case_loc)
4102 bool saw_error;
4103 struct c_scope *scope;
4105 saw_error = false;
4106 for (scope = current_scope;
4107 scope != switch_bindings->scope;
4108 scope = scope->outer)
4110 struct c_binding *b;
4112 gcc_assert (scope != NULL);
4114 if (!scope->has_jump_unsafe_decl)
4115 continue;
4117 for (b = scope->bindings; b != NULL; b = b->prev)
4119 if (decl_jump_unsafe (b->decl))
4121 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
4123 saw_error = true;
4124 error_at (case_loc,
4125 ("switch jumps into scope of identifier with "
4126 "variably modified type"));
4128 else
4129 warning_at (case_loc, OPT_Wjump_misses_init,
4130 "switch jumps over variable initialization");
4131 inform (switch_loc, "switch starts here");
4132 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4133 b->decl);
4138 if (switch_bindings->stmt_exprs > 0)
4140 saw_error = true;
4141 error_at (case_loc, "switch jumps into statement expression");
4142 inform (switch_loc, "switch starts here");
4145 return saw_error;
4148 /* Given NAME, an IDENTIFIER_NODE,
4149 return the structure (or union or enum) definition for that name.
4150 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4151 CODE says which kind of type the caller wants;
4152 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4153 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4154 location where the tag was defined.
4155 If the wrong kind of type is found, an error is reported. */
4157 static tree
4158 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4159 location_t *ploc)
4161 struct c_binding *b = I_TAG_BINDING (name);
4162 bool thislevel = false;
4164 if (!b || !b->decl)
4165 return NULL_TREE;
4167 /* We only care about whether it's in this level if
4168 thislevel_only was set or it might be a type clash. */
4169 if (thislevel_only || TREE_CODE (b->decl) != code)
4171 /* For our purposes, a tag in the external scope is the same as
4172 a tag in the file scope. (Primarily relevant to Objective-C
4173 and its builtin structure tags, which get pushed before the
4174 file scope is created.) */
4175 if (B_IN_CURRENT_SCOPE (b)
4176 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4177 thislevel = true;
4180 if (thislevel_only && !thislevel)
4181 return NULL_TREE;
4183 if (TREE_CODE (b->decl) != code)
4185 /* Definition isn't the kind we were looking for. */
4186 pending_invalid_xref = name;
4187 pending_invalid_xref_location = input_location;
4189 /* If in the same binding level as a declaration as a tag
4190 of a different type, this must not be allowed to
4191 shadow that tag, so give the error immediately.
4192 (For example, "struct foo; union foo;" is invalid.) */
4193 if (thislevel)
4194 pending_xref_error ();
4197 if (ploc != NULL)
4198 *ploc = b->locus;
4200 return b->decl;
4203 /* Return true if a definition exists for NAME with code CODE. */
4205 bool
4206 tag_exists_p (enum tree_code code, tree name)
4208 struct c_binding *b = I_TAG_BINDING (name);
4210 if (b == NULL || b->decl == NULL_TREE)
4211 return false;
4212 return TREE_CODE (b->decl) == code;
4215 /* Print an error message now
4216 for a recent invalid struct, union or enum cross reference.
4217 We don't print them immediately because they are not invalid
4218 when used in the `struct foo;' construct for shadowing. */
4220 void
4221 pending_xref_error (void)
4223 if (pending_invalid_xref != NULL_TREE)
4224 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4225 pending_invalid_xref);
4226 pending_invalid_xref = NULL_TREE;
4230 /* Look up NAME in the current scope and its superiors
4231 in the namespace of variables, functions and typedefs.
4232 Return a ..._DECL node of some kind representing its definition,
4233 or return NULL_TREE if it is undefined. */
4235 tree
4236 lookup_name (tree name)
4238 struct c_binding *b = I_SYMBOL_BINDING (name);
4239 if (b && !b->invisible)
4241 maybe_record_typedef_use (b->decl);
4242 return b->decl;
4244 return NULL_TREE;
4247 /* Similar to `lookup_name' but look only at the indicated scope. */
4249 static tree
4250 lookup_name_in_scope (tree name, struct c_scope *scope)
4252 struct c_binding *b;
4254 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4255 if (B_IN_SCOPE (b, scope))
4256 return b->decl;
4257 return NULL_TREE;
4260 /* Look for the closest match for NAME within the currently valid
4261 scopes.
4263 This finds the identifier with the lowest Levenshtein distance to
4264 NAME. If there are multiple candidates with equal minimal distance,
4265 the first one found is returned. Scopes are searched from innermost
4266 outwards, and within a scope in reverse order of declaration, thus
4267 benefiting candidates "near" to the current scope.
4269 The function also looks for similar macro names to NAME, since a
4270 misspelled macro name will not be expanded, and hence looks like an
4271 identifier to the C frontend.
4273 It also looks for start_typename keywords, to detect "singed" vs "signed"
4274 typos.
4276 Use LOC for any deferred diagnostics. */
4278 name_hint
4279 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4281 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4283 /* First, try some well-known names in the C standard library, in case
4284 the user forgot a #include. */
4285 const char *header_hint
4286 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4288 if (header_hint)
4289 return name_hint (NULL,
4290 new suggest_missing_header (loc,
4291 IDENTIFIER_POINTER (name),
4292 header_hint));
4294 /* Only suggest names reserved for the implementation if NAME begins
4295 with an underscore. */
4296 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4298 best_match<tree, tree> bm (name);
4300 /* Look within currently valid scopes. */
4301 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4302 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4304 if (!binding->id || binding->invisible)
4305 continue;
4306 if (binding->decl == error_mark_node)
4307 continue;
4308 /* Don't use bindings from implicitly declared functions,
4309 as they were likely misspellings themselves. */
4310 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4311 if (C_DECL_IMPLICIT (binding->decl))
4312 continue;
4313 /* Don't suggest names that are reserved for use by the
4314 implementation, unless NAME began with an underscore. */
4315 if (!consider_implementation_names)
4317 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4318 if (name_reserved_for_implementation_p (suggestion_str))
4319 continue;
4321 switch (kind)
4323 case FUZZY_LOOKUP_TYPENAME:
4324 if (TREE_CODE (binding->decl) != TYPE_DECL)
4325 continue;
4326 break;
4328 case FUZZY_LOOKUP_FUNCTION_NAME:
4329 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4331 /* Allow function pointers. */
4332 if ((VAR_P (binding->decl)
4333 || TREE_CODE (binding->decl) == PARM_DECL)
4334 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4335 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4336 == FUNCTION_TYPE))
4337 break;
4338 continue;
4340 break;
4342 default:
4343 break;
4345 bm.consider (binding->id);
4348 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4350 x = SOME_OTHER_MACRO (y);
4351 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4352 as a misspelled identifier.
4354 Use the best distance so far so that a candidate is only set if
4355 a macro is better than anything so far. This allows early rejection
4356 (without calculating the edit distance) of macro names that must have
4357 distance >= bm.get_best_distance (), and means that we only get a
4358 non-NULL result for best_macro_match if it's better than any of
4359 the identifiers already checked, which avoids needless creation
4360 of identifiers for macro hashnodes. */
4361 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4362 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4363 /* If a macro is the closest so far to NAME, use it, creating an
4364 identifier tree node for it. */
4365 if (best_macro)
4367 const char *id = (const char *)best_macro->ident.str;
4368 tree macro_as_identifier
4369 = get_identifier_with_length (id, best_macro->ident.len);
4370 bm.set_best_so_far (macro_as_identifier,
4371 bmm.get_best_distance (),
4372 bmm.get_best_candidate_length ());
4375 /* Try the "start_typename" keywords to detect
4376 "singed" vs "signed" typos. */
4377 if (kind == FUZZY_LOOKUP_TYPENAME)
4379 for (unsigned i = 0; i < num_c_common_reswords; i++)
4381 const c_common_resword *resword = &c_common_reswords[i];
4382 if (!c_keyword_starts_typename (resword->rid))
4383 continue;
4384 tree resword_identifier = ridpointers [resword->rid];
4385 if (!resword_identifier)
4386 continue;
4387 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4388 bm.consider (resword_identifier);
4392 tree best = bm.get_best_meaningful_candidate ();
4393 if (best)
4394 return name_hint (IDENTIFIER_POINTER (best), NULL);
4395 else
4396 return name_hint (NULL, NULL);
4400 /* Table of supported standard (C2x) attributes. */
4401 const struct attribute_spec std_attribute_table[] =
4403 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4404 affects_type_identity, handler, exclude } */
4405 { "deprecated", 0, 1, false, false, false, false,
4406 handle_deprecated_attribute, NULL },
4407 { "fallthrough", 0, 0, false, false, false, false,
4408 handle_fallthrough_attribute, NULL },
4409 { "maybe_unused", 0, 0, false, false, false, false,
4410 handle_unused_attribute, NULL },
4411 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4414 /* Create the predefined scalar types of C,
4415 and some nodes representing standard constants (0, 1, (void *) 0).
4416 Initialize the global scope.
4417 Make definitions for built-in primitive functions. */
4419 void
4420 c_init_decl_processing (void)
4422 location_t save_loc = input_location;
4424 /* Initialize reserved words for parser. */
4425 c_parse_init ();
4427 register_scoped_attributes (std_attribute_table, NULL);
4429 current_function_decl = NULL_TREE;
4431 gcc_obstack_init (&parser_obstack);
4433 /* Make the externals scope. */
4434 push_scope ();
4435 external_scope = current_scope;
4437 /* Declarations from c_common_nodes_and_builtins must not be associated
4438 with this input file, lest we get differences between using and not
4439 using preprocessed headers. */
4440 input_location = BUILTINS_LOCATION;
4442 c_common_nodes_and_builtins ();
4444 /* In C, comparisons and TRUTH_* expressions have type int. */
4445 truthvalue_type_node = integer_type_node;
4446 truthvalue_true_node = integer_one_node;
4447 truthvalue_false_node = integer_zero_node;
4449 /* Even in C99, which has a real boolean type. */
4450 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4451 boolean_type_node));
4453 input_location = save_loc;
4455 make_fname_decl = c_make_fname_decl;
4456 start_fname_decls ();
4459 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4460 give the decl, NAME is the initialization string and TYPE_DEP
4461 indicates whether NAME depended on the type of the function. As we
4462 don't yet implement delayed emission of static data, we mark the
4463 decl as emitted so it is not placed in the output. Anything using
4464 it must therefore pull out the STRING_CST initializer directly.
4465 FIXME. */
4467 static tree
4468 c_make_fname_decl (location_t loc, tree id, int type_dep)
4470 const char *name = fname_as_string (type_dep);
4471 tree decl, type, init;
4472 size_t length = strlen (name);
4474 type = build_array_type (char_type_node,
4475 build_index_type (size_int (length)));
4476 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4478 decl = build_decl (loc, VAR_DECL, id, type);
4480 TREE_STATIC (decl) = 1;
4481 TREE_READONLY (decl) = 1;
4482 DECL_ARTIFICIAL (decl) = 1;
4484 init = build_string (length + 1, name);
4485 free (CONST_CAST (char *, name));
4486 TREE_TYPE (init) = type;
4487 DECL_INITIAL (decl) = init;
4489 TREE_USED (decl) = 1;
4491 if (current_function_decl
4492 /* For invalid programs like this:
4494 void foo()
4495 const char* p = __FUNCTION__;
4497 the __FUNCTION__ is believed to appear in K&R style function
4498 parameter declarator. In that case we still don't have
4499 function_scope. */
4500 && current_function_scope)
4502 DECL_CONTEXT (decl) = current_function_decl;
4503 bind (id, decl, current_function_scope,
4504 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4507 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4509 return decl;
4512 tree
4513 c_builtin_function (tree decl)
4515 tree type = TREE_TYPE (decl);
4516 tree id = DECL_NAME (decl);
4518 const char *name = IDENTIFIER_POINTER (id);
4519 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4521 /* Should never be called on a symbol with a preexisting meaning. */
4522 gcc_assert (!I_SYMBOL_BINDING (id));
4524 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4525 UNKNOWN_LOCATION);
4527 /* Builtins in the implementation namespace are made visible without
4528 needing to be explicitly declared. See push_file_scope. */
4529 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4531 DECL_CHAIN (decl) = visible_builtins;
4532 visible_builtins = decl;
4535 return decl;
4538 tree
4539 c_builtin_function_ext_scope (tree decl)
4541 tree type = TREE_TYPE (decl);
4542 tree id = DECL_NAME (decl);
4544 const char *name = IDENTIFIER_POINTER (id);
4545 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4547 if (external_scope)
4548 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4549 UNKNOWN_LOCATION);
4551 /* Builtins in the implementation namespace are made visible without
4552 needing to be explicitly declared. See push_file_scope. */
4553 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4555 DECL_CHAIN (decl) = visible_builtins;
4556 visible_builtins = decl;
4559 return decl;
4562 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4564 tree
4565 c_simulate_builtin_function_decl (tree decl)
4567 tree type = TREE_TYPE (decl);
4568 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4569 return pushdecl (decl);
4572 /* Warn about attributes in a context where they are unused
4573 (attribute-declarations, except for the "fallthrough" case, and
4574 attributes on statements). */
4576 void
4577 c_warn_unused_attributes (tree attrs)
4579 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4580 if (get_attribute_namespace (t) == NULL_TREE)
4581 /* The specifications of standard attributes mean this is a
4582 constraint violation. */
4583 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4584 get_attribute_name (t));
4585 else
4586 warning (OPT_Wattributes, "%qE attribute ignored",
4587 get_attribute_name (t));
4590 /* Warn for standard attributes being applied to a type that is not
4591 being defined, where that is a constraint violation, and return a
4592 list of attributes with them removed. */
4594 tree
4595 c_warn_type_attributes (tree attrs)
4597 tree *attr_ptr = &attrs;
4598 while (*attr_ptr)
4599 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4601 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4602 get_attribute_name (*attr_ptr));
4603 *attr_ptr = TREE_CHAIN (*attr_ptr);
4605 else
4606 attr_ptr = &TREE_CHAIN (*attr_ptr);
4607 return attrs;
4610 /* Called when a declaration is seen that contains no names to declare.
4611 If its type is a reference to a structure, union or enum inherited
4612 from a containing scope, shadow that tag name for the current scope
4613 with a forward reference.
4614 If its type defines a new named structure or union
4615 or defines an enum, it is valid but we need not do anything here.
4616 Otherwise, it is an error. */
4618 void
4619 shadow_tag (const struct c_declspecs *declspecs)
4621 shadow_tag_warned (declspecs, 0);
4624 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4625 but no pedwarn. */
4626 void
4627 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4629 bool found_tag = false;
4631 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4633 tree value = declspecs->type;
4634 enum tree_code code = TREE_CODE (value);
4636 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4637 /* Used to test also that TYPE_SIZE (value) != 0.
4638 That caused warning for `struct foo;' at top level in the file. */
4640 tree name = TYPE_NAME (value);
4641 tree t;
4643 found_tag = true;
4645 if (declspecs->restrict_p)
4647 error ("invalid use of %<restrict%>");
4648 warned = 1;
4651 if (name == NULL_TREE)
4653 if (warned != 1 && code != ENUMERAL_TYPE)
4654 /* Empty unnamed enum OK */
4656 pedwarn (input_location, 0,
4657 "unnamed struct/union that defines no instances");
4658 warned = 1;
4661 else if (declspecs->typespec_kind != ctsk_tagdef
4662 && declspecs->typespec_kind != ctsk_tagfirstref
4663 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4664 && declspecs->storage_class != csc_none)
4666 if (warned != 1)
4667 pedwarn (input_location, 0,
4668 "empty declaration with storage class specifier "
4669 "does not redeclare tag");
4670 warned = 1;
4671 pending_xref_error ();
4673 else if (declspecs->typespec_kind != ctsk_tagdef
4674 && declspecs->typespec_kind != ctsk_tagfirstref
4675 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4676 && (declspecs->const_p
4677 || declspecs->volatile_p
4678 || declspecs->atomic_p
4679 || declspecs->restrict_p
4680 || declspecs->address_space))
4682 if (warned != 1)
4683 pedwarn (input_location, 0,
4684 "empty declaration with type qualifier "
4685 "does not redeclare tag");
4686 warned = 1;
4687 pending_xref_error ();
4689 else if (declspecs->typespec_kind != ctsk_tagdef
4690 && declspecs->typespec_kind != ctsk_tagfirstref
4691 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4692 && declspecs->alignas_p)
4694 if (warned != 1)
4695 pedwarn (input_location, 0,
4696 "empty declaration with %<_Alignas%> "
4697 "does not redeclare tag");
4698 warned = 1;
4699 pending_xref_error ();
4701 else
4703 pending_invalid_xref = NULL_TREE;
4704 t = lookup_tag (code, name, true, NULL);
4706 if (t == NULL_TREE)
4708 t = make_node (code);
4709 pushtag (input_location, name, t);
4713 else
4715 if (warned != 1 && !in_system_header_at (input_location))
4717 pedwarn (input_location, 0,
4718 "useless type name in empty declaration");
4719 warned = 1;
4723 else if (warned != 1 && !in_system_header_at (input_location)
4724 && declspecs->typedef_p)
4726 pedwarn (input_location, 0, "useless type name in empty declaration");
4727 warned = 1;
4730 pending_invalid_xref = NULL_TREE;
4732 if (declspecs->inline_p)
4734 error ("%<inline%> in empty declaration");
4735 warned = 1;
4738 if (declspecs->noreturn_p)
4740 error ("%<_Noreturn%> in empty declaration");
4741 warned = 1;
4744 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4746 error ("%<auto%> in file-scope empty declaration");
4747 warned = 1;
4750 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4752 error ("%<register%> in file-scope empty declaration");
4753 warned = 1;
4756 if (!warned && !in_system_header_at (input_location)
4757 && declspecs->storage_class != csc_none)
4759 warning (0, "useless storage class specifier in empty declaration");
4760 warned = 2;
4763 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4765 warning (0, "useless %qs in empty declaration",
4766 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4767 warned = 2;
4770 if (!warned
4771 && !in_system_header_at (input_location)
4772 && (declspecs->const_p
4773 || declspecs->volatile_p
4774 || declspecs->atomic_p
4775 || declspecs->restrict_p
4776 || declspecs->address_space))
4778 warning (0, "useless type qualifier in empty declaration");
4779 warned = 2;
4782 if (!warned && !in_system_header_at (input_location)
4783 && declspecs->alignas_p)
4785 warning (0, "useless %<_Alignas%> in empty declaration");
4786 warned = 2;
4789 if (found_tag
4790 && warned == 2
4791 && (declspecs->typespec_kind == ctsk_tagref_attrs
4792 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
4794 /* Standard attributes after the "struct" or "union" keyword are
4795 only permitted when the contents of the type are defined, or
4796 in the form "struct-or-union attribute-specifier-sequence
4797 identifier;". If the ';' was not present, attributes were
4798 diagnosed in the parser. Here, ensure that any other useless
4799 elements of the declaration result in a pedwarn, not just a
4800 warning. Forward declarations of enum types are not part of
4801 standard C, but handle them the same. */
4802 pedwarn (input_location, 0,
4803 "invalid use of attributes in empty declaration");
4804 warned = 1;
4807 if (warned != 1)
4809 if (declspecs->declspecs_seen_p
4810 && !declspecs->non_std_attrs_seen_p)
4811 /* An attribute declaration (but not a fallthrough attribute
4812 declaration, which was handled separately); warn if there
4813 are any attributes being ignored (but not if the attributes
4814 were empty). */
4815 c_warn_unused_attributes (declspecs->attrs);
4816 else if (!found_tag)
4817 pedwarn (input_location, 0, "empty declaration");
4822 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4823 bits. SPECS represents declaration specifiers that the grammar
4824 only permits to contain type qualifiers and attributes. */
4827 quals_from_declspecs (const struct c_declspecs *specs)
4829 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4830 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4831 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4832 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4833 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4834 gcc_assert (!specs->type
4835 && !specs->decl_attr
4836 && specs->typespec_word == cts_none
4837 && specs->storage_class == csc_none
4838 && !specs->typedef_p
4839 && !specs->explicit_signed_p
4840 && !specs->deprecated_p
4841 && !specs->long_p
4842 && !specs->long_long_p
4843 && !specs->short_p
4844 && !specs->signed_p
4845 && !specs->unsigned_p
4846 && !specs->complex_p
4847 && !specs->inline_p
4848 && !specs->noreturn_p
4849 && !specs->thread_p);
4850 return quals;
4853 /* Construct an array declarator. LOC is the location of the
4854 beginning of the array (usually the opening brace). EXPR is the
4855 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4856 inside the [] (to be applied to the pointer to which a parameter
4857 array is converted). STATIC_P is true if "static" is inside the
4858 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4859 VLA of unspecified length which is nevertheless a complete type,
4860 false otherwise. The field for the contained declarator is left to
4861 be filled in by set_array_declarator_inner. */
4863 struct c_declarator *
4864 build_array_declarator (location_t loc,
4865 tree expr, struct c_declspecs *quals, bool static_p,
4866 bool vla_unspec_p)
4868 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4869 struct c_declarator);
4870 declarator->id_loc = loc;
4871 declarator->kind = cdk_array;
4872 declarator->declarator = 0;
4873 declarator->u.array.dimen = expr;
4874 if (quals)
4876 declarator->u.array.attrs = quals->attrs;
4877 declarator->u.array.quals = quals_from_declspecs (quals);
4879 else
4881 declarator->u.array.attrs = NULL_TREE;
4882 declarator->u.array.quals = 0;
4884 declarator->u.array.static_p = static_p;
4885 declarator->u.array.vla_unspec_p = vla_unspec_p;
4886 if (static_p || quals != NULL)
4887 pedwarn_c90 (loc, OPT_Wpedantic,
4888 "ISO C90 does not support %<static%> or type "
4889 "qualifiers in parameter array declarators");
4890 if (vla_unspec_p)
4891 pedwarn_c90 (loc, OPT_Wpedantic,
4892 "ISO C90 does not support %<[*]%> array declarators");
4893 if (vla_unspec_p)
4895 if (!current_scope->parm_flag)
4897 /* C99 6.7.5.2p4 */
4898 error_at (loc, "%<[*]%> not allowed in other than "
4899 "function prototype scope");
4900 declarator->u.array.vla_unspec_p = false;
4901 return NULL;
4903 current_scope->had_vla_unspec = true;
4905 return declarator;
4908 /* Set the contained declarator of an array declarator. DECL is the
4909 declarator, as constructed by build_array_declarator; INNER is what
4910 appears on the left of the []. */
4912 struct c_declarator *
4913 set_array_declarator_inner (struct c_declarator *decl,
4914 struct c_declarator *inner)
4916 decl->declarator = inner;
4917 return decl;
4920 /* INIT is a constructor that forms DECL's initializer. If the final
4921 element initializes a flexible array field, add the size of that
4922 initializer to DECL's size. */
4924 static void
4925 add_flexible_array_elts_to_size (tree decl, tree init)
4927 tree elt, type;
4929 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4930 return;
4932 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4933 type = TREE_TYPE (elt);
4934 if (TREE_CODE (type) == ARRAY_TYPE
4935 && TYPE_SIZE (type) == NULL_TREE
4936 && TYPE_DOMAIN (type) != NULL_TREE
4937 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4939 complete_array_type (&type, elt, false);
4940 DECL_SIZE (decl)
4941 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4942 DECL_SIZE_UNIT (decl)
4943 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4947 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4948 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4949 before the type name, and set *EXPR_CONST_OPERANDS, if
4950 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4951 appear in a constant expression. */
4953 tree
4954 groktypename (struct c_type_name *type_name, tree *expr,
4955 bool *expr_const_operands)
4957 tree type;
4958 tree attrs = type_name->specs->attrs;
4960 type_name->specs->attrs = NULL_TREE;
4962 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4963 false, NULL, &attrs, expr, expr_const_operands,
4964 DEPRECATED_NORMAL);
4966 /* Apply attributes. */
4967 attrs = c_warn_type_attributes (attrs);
4968 decl_attributes (&type, attrs, 0);
4970 return type;
4973 /* Wrapper for decl_attributes that adds some implicit attributes
4974 to VAR_DECLs or FUNCTION_DECLs. */
4976 static tree
4977 c_decl_attributes (tree *node, tree attributes, int flags)
4979 /* Add implicit "omp declare target" attribute if requested. */
4980 if (current_omp_declare_target_attribute
4981 && ((VAR_P (*node) && is_global_var (*node))
4982 || TREE_CODE (*node) == FUNCTION_DECL))
4984 if (VAR_P (*node)
4985 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4986 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4987 NULL_TREE, attributes);
4988 else
4990 attributes = tree_cons (get_identifier ("omp declare target"),
4991 NULL_TREE, attributes);
4992 attributes = tree_cons (get_identifier ("omp declare target block"),
4993 NULL_TREE, attributes);
4997 /* Look up the current declaration with all the attributes merged
4998 so far so that attributes on the current declaration that's
4999 about to be pushed that conflict with the former can be detected,
5000 diagnosed, and rejected as appropriate. */
5001 tree last_decl = lookup_name (DECL_NAME (*node));
5002 if (!last_decl)
5003 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
5005 return decl_attributes (node, attributes, flags, last_decl);
5009 /* Decode a declarator in an ordinary declaration or data definition.
5010 This is called as soon as the type information and variable name
5011 have been parsed, before parsing the initializer if any.
5012 Here we create the ..._DECL node, fill in its type,
5013 and put it on the list of decls for the current context.
5014 The ..._DECL node is returned as the value.
5016 Exception: for arrays where the length is not specified,
5017 the type is left null, to be filled in by `finish_decl'.
5019 Function definitions do not come here; they go to start_function
5020 instead. However, external and forward declarations of functions
5021 do go through here. Structure field declarations are done by
5022 grokfield and not through here. */
5024 tree
5025 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5026 bool initialized, tree attributes)
5028 tree decl;
5029 tree tem;
5030 tree expr = NULL_TREE;
5031 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5033 /* An object declared as __attribute__((deprecated)) suppresses
5034 warnings of uses of other deprecated items. */
5035 if (lookup_attribute ("deprecated", attributes))
5036 deprecated_state = DEPRECATED_SUPPRESS;
5038 decl = grokdeclarator (declarator, declspecs,
5039 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5040 deprecated_state);
5041 if (!decl || decl == error_mark_node)
5042 return NULL_TREE;
5044 if (expr)
5045 add_stmt (fold_convert (void_type_node, expr));
5047 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5048 && TREE_PUBLIC (decl))
5049 warning (OPT_Wmain, "%q+D is usually a function", decl);
5051 if (initialized)
5052 /* Is it valid for this decl to have an initializer at all?
5053 If not, set INITIALIZED to zero, which will indirectly
5054 tell 'finish_decl' to ignore the initializer once it is parsed. */
5055 switch (TREE_CODE (decl))
5057 case TYPE_DECL:
5058 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5059 initialized = false;
5060 break;
5062 case FUNCTION_DECL:
5063 error ("function %qD is initialized like a variable", decl);
5064 initialized = false;
5065 break;
5067 case PARM_DECL:
5068 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5069 error ("parameter %qD is initialized", decl);
5070 initialized = false;
5071 break;
5073 default:
5074 /* Don't allow initializations for incomplete types except for
5075 arrays which might be completed by the initialization. */
5077 /* This can happen if the array size is an undefined macro.
5078 We already gave a warning, so we don't need another one. */
5079 if (TREE_TYPE (decl) == error_mark_node)
5080 initialized = false;
5081 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5083 /* A complete type is ok if size is fixed. */
5085 if (!poly_int_tree_p (TYPE_SIZE (TREE_TYPE (decl)))
5086 || C_DECL_VARIABLE_SIZE (decl))
5088 error ("variable-sized object may not be initialized");
5089 initialized = false;
5092 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5094 error ("variable %qD has initializer but incomplete type", decl);
5095 initialized = false;
5097 else if (C_DECL_VARIABLE_SIZE (decl))
5099 /* Although C99 is unclear about whether incomplete arrays
5100 of VLAs themselves count as VLAs, it does not make
5101 sense to permit them to be initialized given that
5102 ordinary VLAs may not be initialized. */
5103 error ("variable-sized object may not be initialized");
5104 initialized = false;
5108 if (initialized)
5110 if (current_scope == file_scope)
5111 TREE_STATIC (decl) = 1;
5113 /* Tell 'pushdecl' this is an initialized decl
5114 even though we don't yet have the initializer expression.
5115 Also tell 'finish_decl' it may store the real initializer. */
5116 DECL_INITIAL (decl) = error_mark_node;
5119 /* If this is a function declaration, write a record describing it to the
5120 prototypes file (if requested). */
5122 if (TREE_CODE (decl) == FUNCTION_DECL)
5123 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5125 /* ANSI specifies that a tentative definition which is not merged with
5126 a non-tentative definition behaves exactly like a definition with an
5127 initializer equal to zero. (Section 3.7.2)
5129 -fno-common gives strict ANSI behavior, though this tends to break
5130 a large body of code that grew up without this rule.
5132 Thread-local variables are never common, since there's no entrenched
5133 body of code to break, and it allows more efficient variable references
5134 in the presence of dynamic linking. */
5136 if (VAR_P (decl)
5137 && !initialized
5138 && TREE_PUBLIC (decl)
5139 && !DECL_THREAD_LOCAL_P (decl)
5140 && !flag_no_common)
5141 DECL_COMMON (decl) = 1;
5143 /* Set attributes here so if duplicate decl, will have proper attributes. */
5144 c_decl_attributes (&decl, attributes, 0);
5146 /* Handle gnu_inline attribute. */
5147 if (declspecs->inline_p
5148 && !flag_gnu89_inline
5149 && TREE_CODE (decl) == FUNCTION_DECL
5150 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5151 || current_function_decl))
5153 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5155 else if (declspecs->storage_class != csc_static)
5156 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5159 if (TREE_CODE (decl) == FUNCTION_DECL
5160 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5162 struct c_declarator *ce = declarator;
5164 if (ce->kind == cdk_pointer)
5165 ce = declarator->declarator;
5166 if (ce->kind == cdk_function)
5168 tree args = ce->u.arg_info->parms;
5169 for (; args; args = DECL_CHAIN (args))
5171 tree type = TREE_TYPE (args);
5172 if (type && INTEGRAL_TYPE_P (type)
5173 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5174 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5179 if (TREE_CODE (decl) == FUNCTION_DECL
5180 && DECL_DECLARED_INLINE_P (decl)
5181 && DECL_UNINLINABLE (decl)
5182 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5183 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5184 decl, "noinline");
5186 /* C99 6.7.4p3: An inline definition of a function with external
5187 linkage shall not contain a definition of a modifiable object
5188 with static storage duration... */
5189 if (VAR_P (decl)
5190 && current_scope != file_scope
5191 && TREE_STATIC (decl)
5192 && !TREE_READONLY (decl)
5193 && DECL_DECLARED_INLINE_P (current_function_decl)
5194 && DECL_EXTERNAL (current_function_decl))
5195 record_inline_static (input_location, current_function_decl,
5196 decl, csi_modifiable);
5198 if (c_dialect_objc ()
5199 && VAR_OR_FUNCTION_DECL_P (decl))
5200 objc_check_global_decl (decl);
5202 /* Add this decl to the current scope.
5203 TEM may equal DECL or it may be a previous decl of the same name. */
5204 tem = pushdecl (decl);
5206 if (initialized && DECL_EXTERNAL (tem))
5208 DECL_EXTERNAL (tem) = 0;
5209 TREE_STATIC (tem) = 1;
5212 return tem;
5215 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5216 DECL or the non-array element type if DECL is an uninitialized array.
5217 If that type has a const member, diagnose this. */
5219 static void
5220 diagnose_uninitialized_cst_member (tree decl, tree type)
5222 tree field;
5223 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5225 tree field_type;
5226 if (TREE_CODE (field) != FIELD_DECL)
5227 continue;
5228 field_type = strip_array_types (TREE_TYPE (field));
5230 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5232 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5233 "uninitialized const member in %qT is invalid in C++",
5234 strip_array_types (TREE_TYPE (decl)));
5235 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5238 if (RECORD_OR_UNION_TYPE_P (field_type))
5239 diagnose_uninitialized_cst_member (decl, field_type);
5243 /* Finish processing of a declaration;
5244 install its initial value.
5245 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5246 If the length of an array type is not known before,
5247 it must be determined now, from the initial value, or it is an error.
5249 INIT_LOC is the location of the initial value. */
5251 void
5252 finish_decl (tree decl, location_t init_loc, tree init,
5253 tree origtype, tree asmspec_tree)
5255 tree type;
5256 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5257 const char *asmspec = 0;
5259 /* If a name was specified, get the string. */
5260 if (VAR_OR_FUNCTION_DECL_P (decl)
5261 && DECL_FILE_SCOPE_P (decl))
5262 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5263 if (asmspec_tree)
5264 asmspec = TREE_STRING_POINTER (asmspec_tree);
5266 if (VAR_P (decl)
5267 && TREE_STATIC (decl)
5268 && global_bindings_p ())
5269 /* So decl is a global variable. Record the types it uses
5270 so that we can decide later to emit debug info for them. */
5271 record_types_used_by_current_var_decl (decl);
5273 /* If `start_decl' didn't like having an initialization, ignore it now. */
5274 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5275 init = NULL_TREE;
5277 /* Don't crash if parm is initialized. */
5278 if (TREE_CODE (decl) == PARM_DECL)
5279 init = NULL_TREE;
5281 if (init)
5282 store_init_value (init_loc, decl, init, origtype);
5284 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5285 || TREE_CODE (decl) == FIELD_DECL))
5286 objc_check_decl (decl);
5288 type = TREE_TYPE (decl);
5290 /* Deduce size of array from initialization, if not already known.
5291 This is only needed for an initialization in the current scope;
5292 it must not be done for a file-scope initialization of a
5293 declaration with external linkage, redeclared in an inner scope
5294 with the outer declaration shadowed in an intermediate scope. */
5295 if (TREE_CODE (type) == ARRAY_TYPE
5296 && TYPE_DOMAIN (type) == NULL_TREE
5297 && TREE_CODE (decl) != TYPE_DECL
5298 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5300 bool do_default
5301 = (TREE_STATIC (decl)
5302 /* Even if pedantic, an external linkage array
5303 may have incomplete type at first. */
5304 ? pedantic && !TREE_PUBLIC (decl)
5305 : !DECL_EXTERNAL (decl));
5306 int failure
5307 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5308 do_default);
5310 /* Get the completed type made by complete_array_type. */
5311 type = TREE_TYPE (decl);
5313 switch (failure)
5315 case 1:
5316 error ("initializer fails to determine size of %q+D", decl);
5317 break;
5319 case 2:
5320 if (do_default)
5321 error ("array size missing in %q+D", decl);
5322 break;
5324 case 3:
5325 error ("zero or negative size array %q+D", decl);
5326 break;
5328 case 0:
5329 /* For global variables, update the copy of the type that
5330 exists in the binding. */
5331 if (TREE_PUBLIC (decl))
5333 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5334 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5335 b_ext = b_ext->shadowed;
5336 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5338 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5339 b_ext->u.type = composite_type (b_ext->u.type, type);
5340 else
5341 b_ext->u.type = type;
5344 break;
5346 default:
5347 gcc_unreachable ();
5350 if (DECL_INITIAL (decl))
5351 TREE_TYPE (DECL_INITIAL (decl)) = type;
5353 relayout_decl (decl);
5356 /* Look for braced array initializers for character arrays and
5357 recursively convert them into STRING_CSTs. */
5358 if (tree init = DECL_INITIAL (decl))
5359 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5361 if (VAR_P (decl))
5363 if (init && TREE_CODE (init) == CONSTRUCTOR)
5364 add_flexible_array_elts_to_size (decl, init);
5366 complete_flexible_array_elts (DECL_INITIAL (decl));
5368 if (is_global_var (decl))
5370 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5371 ? TCTX_THREAD_STORAGE
5372 : TCTX_STATIC_STORAGE);
5373 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5374 TREE_TYPE (decl) = error_mark_node;
5377 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5378 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5379 layout_decl (decl, 0);
5381 if (DECL_SIZE (decl) == NULL_TREE
5382 /* Don't give an error if we already gave one earlier. */
5383 && TREE_TYPE (decl) != error_mark_node
5384 && (TREE_STATIC (decl)
5385 /* A static variable with an incomplete type
5386 is an error if it is initialized.
5387 Also if it is not file scope.
5388 Otherwise, let it through, but if it is not `extern'
5389 then it may cause an error message later. */
5390 ? (DECL_INITIAL (decl) != NULL_TREE
5391 || !DECL_FILE_SCOPE_P (decl))
5392 /* An automatic variable with an incomplete type
5393 is an error. */
5394 : !DECL_EXTERNAL (decl)))
5396 error ("storage size of %q+D isn%'t known", decl);
5397 TREE_TYPE (decl) = error_mark_node;
5400 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5401 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5402 && DECL_SIZE (decl) == NULL_TREE
5403 && TREE_STATIC (decl))
5404 incomplete_record_decls.safe_push (decl);
5406 if (is_global_var (decl)
5407 && DECL_SIZE (decl) != NULL_TREE
5408 && TREE_TYPE (decl) != error_mark_node)
5410 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5411 constant_expression_warning (DECL_SIZE (decl));
5412 else
5414 error ("storage size of %q+D isn%'t constant", decl);
5415 TREE_TYPE (decl) = error_mark_node;
5419 if (TREE_USED (type))
5421 TREE_USED (decl) = 1;
5422 DECL_READ_P (decl) = 1;
5426 /* If this is a function and an assembler name is specified, reset DECL_RTL
5427 so we can give it its new name. Also, update builtin_decl if it
5428 was a normal built-in. */
5429 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5431 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5432 set_builtin_user_assembler_name (decl, asmspec);
5433 set_user_assembler_name (decl, asmspec);
5436 /* If #pragma weak was used, mark the decl weak now. */
5437 maybe_apply_pragma_weak (decl);
5439 /* Output the assembler code and/or RTL code for variables and functions,
5440 unless the type is an undefined structure or union.
5441 If not, it will get done when the type is completed. */
5443 if (VAR_OR_FUNCTION_DECL_P (decl))
5445 /* Determine the ELF visibility. */
5446 if (TREE_PUBLIC (decl))
5447 c_determine_visibility (decl);
5449 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5450 if (c_dialect_objc ())
5451 objc_check_decl (decl);
5453 if (asmspec)
5455 /* If this is not a static variable, issue a warning.
5456 It doesn't make any sense to give an ASMSPEC for an
5457 ordinary, non-register local variable. Historically,
5458 GCC has accepted -- but ignored -- the ASMSPEC in
5459 this case. */
5460 if (!DECL_FILE_SCOPE_P (decl)
5461 && VAR_P (decl)
5462 && !C_DECL_REGISTER (decl)
5463 && !TREE_STATIC (decl))
5464 warning (0, "ignoring %<asm%> specifier for non-static local "
5465 "variable %q+D", decl);
5466 else
5467 set_user_assembler_name (decl, asmspec);
5470 if (DECL_FILE_SCOPE_P (decl))
5472 if (DECL_INITIAL (decl) == NULL_TREE
5473 || DECL_INITIAL (decl) == error_mark_node)
5474 /* Don't output anything
5475 when a tentative file-scope definition is seen.
5476 But at end of compilation, do output code for them. */
5477 DECL_DEFER_OUTPUT (decl) = 1;
5478 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5479 DECL_HARD_REGISTER (decl) = 1;
5480 rest_of_decl_compilation (decl, true, 0);
5482 else
5484 /* In conjunction with an ASMSPEC, the `register'
5485 keyword indicates that we should place the variable
5486 in a particular register. */
5487 if (asmspec && C_DECL_REGISTER (decl))
5489 DECL_HARD_REGISTER (decl) = 1;
5490 /* This cannot be done for a structure with volatile
5491 fields, on which DECL_REGISTER will have been
5492 reset. */
5493 if (!DECL_REGISTER (decl))
5494 error ("cannot put object with volatile field into register");
5497 if (TREE_CODE (decl) != FUNCTION_DECL)
5499 /* If we're building a variable sized type, and we might be
5500 reachable other than via the top of the current binding
5501 level, then create a new BIND_EXPR so that we deallocate
5502 the object at the right time. */
5503 /* Note that DECL_SIZE can be null due to errors. */
5504 if (DECL_SIZE (decl)
5505 && !TREE_CONSTANT (DECL_SIZE (decl))
5506 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5508 tree bind;
5509 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5510 TREE_SIDE_EFFECTS (bind) = 1;
5511 add_stmt (bind);
5512 BIND_EXPR_BODY (bind) = push_stmt_list ();
5514 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5515 DECL_EXPR, decl));
5520 if (!DECL_FILE_SCOPE_P (decl))
5522 /* Recompute the RTL of a local array now
5523 if it used to be an incomplete type. */
5524 if (was_incomplete && !is_global_var (decl))
5526 /* If we used it already as memory, it must stay in memory. */
5527 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5528 /* If it's still incomplete now, no init will save it. */
5529 if (DECL_SIZE (decl) == NULL_TREE)
5530 DECL_INITIAL (decl) = NULL_TREE;
5535 if (TREE_CODE (decl) == TYPE_DECL)
5537 if (!DECL_FILE_SCOPE_P (decl)
5538 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5539 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5541 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5544 /* Install a cleanup (aka destructor) if one was given. */
5545 if (VAR_P (decl) && !TREE_STATIC (decl))
5547 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5548 if (attr)
5550 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5551 tree cleanup_decl = lookup_name (cleanup_id);
5552 tree cleanup;
5553 vec<tree, va_gc> *v;
5555 /* Build "cleanup(&decl)" for the destructor. */
5556 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5557 vec_alloc (v, 1);
5558 v->quick_push (cleanup);
5559 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5560 vNULL, cleanup_decl, v, NULL);
5561 vec_free (v);
5563 /* Don't warn about decl unused; the cleanup uses it. */
5564 TREE_USED (decl) = 1;
5565 TREE_USED (cleanup_decl) = 1;
5566 DECL_READ_P (decl) = 1;
5568 push_cleanup (decl, cleanup, false);
5572 if (warn_cxx_compat
5573 && VAR_P (decl)
5574 && !DECL_EXTERNAL (decl)
5575 && DECL_INITIAL (decl) == NULL_TREE)
5577 type = strip_array_types (type);
5578 if (TREE_READONLY (decl))
5579 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5580 "uninitialized %<const %D%> is invalid in C++", decl);
5581 else if (RECORD_OR_UNION_TYPE_P (type)
5582 && C_TYPE_FIELDS_READONLY (type))
5583 diagnose_uninitialized_cst_member (decl, type);
5586 if (flag_openmp
5587 && VAR_P (decl)
5588 && lookup_attribute ("omp declare target implicit",
5589 DECL_ATTRIBUTES (decl)))
5591 DECL_ATTRIBUTES (decl)
5592 = remove_attribute ("omp declare target implicit",
5593 DECL_ATTRIBUTES (decl));
5594 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5595 error ("%q+D in declare target directive does not have mappable type",
5596 decl);
5597 else if (!lookup_attribute ("omp declare target",
5598 DECL_ATTRIBUTES (decl))
5599 && !lookup_attribute ("omp declare target link",
5600 DECL_ATTRIBUTES (decl)))
5601 DECL_ATTRIBUTES (decl)
5602 = tree_cons (get_identifier ("omp declare target"),
5603 NULL_TREE, DECL_ATTRIBUTES (decl));
5606 /* This is the last point we can lower alignment so give the target the
5607 chance to do so. */
5608 if (VAR_P (decl)
5609 && !is_global_var (decl)
5610 && !DECL_HARD_REGISTER (decl))
5611 targetm.lower_local_decl_alignment (decl);
5613 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5616 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5617 EXPR is NULL or a pointer to an expression that needs to be
5618 evaluated for the side effects of array size expressions in the
5619 parameters. */
5621 tree
5622 grokparm (const struct c_parm *parm, tree *expr)
5624 tree attrs = parm->attrs;
5625 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5626 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5628 decl_attributes (&decl, attrs, 0);
5630 return decl;
5633 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5634 and push that on the current scope. EXPR is a pointer to an
5635 expression that needs to be evaluated for the side effects of array
5636 size expressions in the parameters. */
5638 void
5639 push_parm_decl (const struct c_parm *parm, tree *expr)
5641 tree attrs = parm->attrs;
5642 tree decl;
5644 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5645 &attrs, expr, NULL, DEPRECATED_NORMAL);
5646 if (decl && DECL_P (decl))
5647 DECL_SOURCE_LOCATION (decl) = parm->loc;
5648 decl_attributes (&decl, attrs, 0);
5650 decl = pushdecl (decl);
5652 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5655 /* Mark all the parameter declarations to date as forward decls.
5656 Also diagnose use of this extension. */
5658 void
5659 mark_forward_parm_decls (void)
5661 struct c_binding *b;
5663 if (pedantic && !current_scope->warned_forward_parm_decls)
5665 pedwarn (input_location, OPT_Wpedantic,
5666 "ISO C forbids forward parameter declarations");
5667 current_scope->warned_forward_parm_decls = true;
5670 for (b = current_scope->bindings; b; b = b->prev)
5671 if (TREE_CODE (b->decl) == PARM_DECL)
5672 TREE_ASM_WRITTEN (b->decl) = 1;
5675 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5676 literal, which may be an incomplete array type completed by the
5677 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5678 literal. NON_CONST is true if the initializers contain something
5679 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5680 it is the (valid) alignment for this compound literal, as specified
5681 with _Alignas. */
5683 tree
5684 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5685 unsigned int alignas_align)
5687 /* We do not use start_decl here because we have a type, not a declarator;
5688 and do not use finish_decl because the decl should be stored inside
5689 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5690 tree decl;
5691 tree complit;
5692 tree stmt;
5694 if (type == error_mark_node
5695 || init == error_mark_node)
5696 return error_mark_node;
5698 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5699 DECL_EXTERNAL (decl) = 0;
5700 TREE_PUBLIC (decl) = 0;
5701 TREE_STATIC (decl) = (current_scope == file_scope);
5702 DECL_CONTEXT (decl) = current_function_decl;
5703 TREE_USED (decl) = 1;
5704 DECL_READ_P (decl) = 1;
5705 DECL_ARTIFICIAL (decl) = 1;
5706 DECL_IGNORED_P (decl) = 1;
5707 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
5708 TREE_TYPE (decl) = type;
5709 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5710 if (alignas_align)
5712 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5713 DECL_USER_ALIGN (decl) = 1;
5715 store_init_value (loc, decl, init, NULL_TREE);
5717 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5719 int failure = complete_array_type (&TREE_TYPE (decl),
5720 DECL_INITIAL (decl), true);
5721 /* If complete_array_type returns 3, it means that the
5722 initial value of the compound literal is empty. Allow it. */
5723 gcc_assert (failure == 0 || failure == 3);
5725 type = TREE_TYPE (decl);
5726 TREE_TYPE (DECL_INITIAL (decl)) = type;
5729 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5731 c_incomplete_type_error (loc, NULL_TREE, type);
5732 return error_mark_node;
5735 if (TREE_STATIC (decl)
5736 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
5737 return error_mark_node;
5739 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5740 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5741 TREE_SIDE_EFFECTS (complit) = 1;
5743 layout_decl (decl, 0);
5745 if (TREE_STATIC (decl))
5747 /* This decl needs a name for the assembler output. */
5748 set_compound_literal_name (decl);
5749 DECL_DEFER_OUTPUT (decl) = 1;
5750 DECL_COMDAT (decl) = 1;
5751 pushdecl (decl);
5752 rest_of_decl_compilation (decl, 1, 0);
5754 else if (current_function_decl && !current_scope->parm_flag)
5755 pushdecl (decl);
5757 if (non_const)
5759 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5760 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5763 return complit;
5766 /* Check the type of a compound literal. Here we just check that it
5767 is valid for C++. */
5769 void
5770 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5772 if (warn_cxx_compat
5773 && (type_name->specs->typespec_kind == ctsk_tagdef
5774 || type_name->specs->typespec_kind == ctsk_tagfirstref
5775 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
5776 warning_at (loc, OPT_Wc___compat,
5777 "defining a type in a compound literal is invalid in C++");
5780 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5781 replacing with appropriate values if they are invalid. */
5783 static void
5784 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5785 tree orig_name)
5787 tree type_mv;
5788 unsigned int max_width;
5789 unsigned HOST_WIDE_INT w;
5790 const char *name = (orig_name
5791 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5792 : _("<anonymous>"));
5794 /* Detect and ignore out of range field width and process valid
5795 field widths. */
5796 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5798 error_at (loc, "bit-field %qs width not an integer constant", name);
5799 *width = integer_one_node;
5801 else
5803 if (TREE_CODE (*width) != INTEGER_CST)
5805 *width = c_fully_fold (*width, false, NULL);
5806 if (TREE_CODE (*width) == INTEGER_CST)
5807 pedwarn (loc, OPT_Wpedantic,
5808 "bit-field %qs width not an integer constant expression",
5809 name);
5811 if (TREE_CODE (*width) != INTEGER_CST)
5813 error_at (loc, "bit-field %qs width not an integer constant", name);
5814 *width = integer_one_node;
5816 constant_expression_warning (*width);
5817 if (tree_int_cst_sgn (*width) < 0)
5819 error_at (loc, "negative width in bit-field %qs", name);
5820 *width = integer_one_node;
5822 else if (integer_zerop (*width) && orig_name)
5824 error_at (loc, "zero width for bit-field %qs", name);
5825 *width = integer_one_node;
5829 /* Detect invalid bit-field type. */
5830 if (TREE_CODE (*type) != INTEGER_TYPE
5831 && TREE_CODE (*type) != BOOLEAN_TYPE
5832 && TREE_CODE (*type) != ENUMERAL_TYPE)
5834 error_at (loc, "bit-field %qs has invalid type", name);
5835 *type = unsigned_type_node;
5838 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5840 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5841 name);
5842 *type = unsigned_type_node;
5845 type_mv = TYPE_MAIN_VARIANT (*type);
5846 if (!in_system_header_at (input_location)
5847 && type_mv != integer_type_node
5848 && type_mv != unsigned_type_node
5849 && type_mv != boolean_type_node)
5850 pedwarn_c90 (loc, OPT_Wpedantic,
5851 "type of bit-field %qs is a GCC extension", name);
5853 max_width = TYPE_PRECISION (*type);
5855 if (compare_tree_int (*width, max_width) > 0)
5857 error_at (loc, "width of %qs exceeds its type", name);
5858 w = max_width;
5859 *width = build_int_cst (integer_type_node, w);
5861 else
5862 w = tree_to_uhwi (*width);
5864 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5866 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5867 if (!lt
5868 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5869 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5870 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5876 /* Print warning about variable length array if necessary. */
5878 static void
5879 warn_variable_length_array (tree name, tree size)
5881 if (TREE_CONSTANT (size))
5883 if (name)
5884 pedwarn_c90 (input_location, OPT_Wvla,
5885 "ISO C90 forbids array %qE whose size "
5886 "cannot be evaluated", name);
5887 else
5888 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5889 "whose size cannot be evaluated");
5891 else
5893 if (name)
5894 pedwarn_c90 (input_location, OPT_Wvla,
5895 "ISO C90 forbids variable length array %qE", name);
5896 else
5897 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5898 "length array");
5902 /* Print warning about defaulting to int if necessary. */
5904 static void
5905 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5907 diagnostic_info diagnostic;
5908 va_list ap;
5909 rich_location richloc (line_table, location);
5911 va_start (ap, gmsgid);
5912 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5913 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5914 diagnostic.option_index = opt;
5915 diagnostic_report_diagnostic (global_dc, &diagnostic);
5916 va_end (ap);
5919 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5920 considering only those c_declspec_words found in LIST, which
5921 must be terminated by cdw_number_of_elements. */
5923 static location_t
5924 smallest_type_quals_location (const location_t *locations,
5925 const c_declspec_word *list)
5927 location_t loc = UNKNOWN_LOCATION;
5928 while (*list != cdw_number_of_elements)
5930 location_t newloc = locations[*list];
5931 if (loc == UNKNOWN_LOCATION
5932 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5933 loc = newloc;
5934 list++;
5937 return loc;
5940 /* Given declspecs and a declarator,
5941 determine the name and type of the object declared
5942 and construct a ..._DECL node for it.
5943 (In one case we can return a ..._TYPE node instead.
5944 For invalid input we sometimes return NULL_TREE.)
5946 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5948 DECL_CONTEXT says which syntactic context this declaration is in:
5949 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5950 FUNCDEF for a function definition. Like NORMAL but a few different
5951 error messages in each case. Return value may be zero meaning
5952 this definition is too screwy to try to parse.
5953 PARM for a parameter declaration (either within a function prototype
5954 or before a function body). Make a PARM_DECL, or return void_type_node.
5955 TYPENAME if for a typename (in a cast or sizeof).
5956 Don't make a DECL node; just return the ..._TYPE node.
5957 FIELD for a struct or union field; make a FIELD_DECL.
5958 INITIALIZED is true if the decl has an initializer.
5959 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5960 representing the width of the bit-field.
5961 DECL_ATTRS points to the list of attributes that should be added to this
5962 decl. Any nested attributes that belong on the decl itself will be
5963 added to this list.
5964 If EXPR is not NULL, any expressions that need to be evaluated as
5965 part of evaluating variably modified types will be stored in *EXPR.
5966 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5967 set to indicate whether operands in *EXPR can be used in constant
5968 expressions.
5969 DEPRECATED_STATE is a deprecated_states value indicating whether
5970 deprecation warnings should be suppressed.
5972 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5973 It may also be so in the PARM case, for a prototype where the
5974 argument type is specified but not the name.
5976 This function is where the complicated C meanings of `static'
5977 and `extern' are interpreted. */
5979 static tree
5980 grokdeclarator (const struct c_declarator *declarator,
5981 struct c_declspecs *declspecs,
5982 enum decl_context decl_context, bool initialized, tree *width,
5983 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5984 enum deprecated_states deprecated_state)
5986 tree type = declspecs->type;
5987 bool threadp = declspecs->thread_p;
5988 enum c_storage_class storage_class = declspecs->storage_class;
5989 int constp;
5990 int restrictp;
5991 int volatilep;
5992 int atomicp;
5993 int type_quals = TYPE_UNQUALIFIED;
5994 tree name = NULL_TREE;
5995 bool funcdef_flag = false;
5996 bool funcdef_syntax = false;
5997 bool size_varies = false;
5998 tree decl_attr = declspecs->decl_attr;
5999 int array_ptr_quals = TYPE_UNQUALIFIED;
6000 tree array_ptr_attrs = NULL_TREE;
6001 bool array_parm_static = false;
6002 bool array_parm_vla_unspec_p = false;
6003 tree returned_attrs = NULL_TREE;
6004 tree decl_id_attrs = NULL_TREE;
6005 bool bitfield = width != NULL;
6006 tree element_type;
6007 tree orig_qual_type = NULL;
6008 size_t orig_qual_indirect = 0;
6009 struct c_arg_info *arg_info = 0;
6010 addr_space_t as1, as2, address_space;
6011 location_t loc = UNKNOWN_LOCATION;
6012 tree expr_dummy;
6013 bool expr_const_operands_dummy;
6014 enum c_declarator_kind first_non_attr_kind;
6015 unsigned int alignas_align = 0;
6017 if (TREE_CODE (type) == ERROR_MARK)
6018 return error_mark_node;
6019 if (expr == NULL)
6021 expr = &expr_dummy;
6022 expr_dummy = NULL_TREE;
6024 if (expr_const_operands == NULL)
6025 expr_const_operands = &expr_const_operands_dummy;
6027 if (declspecs->expr)
6029 if (*expr)
6030 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6031 declspecs->expr);
6032 else
6033 *expr = declspecs->expr;
6035 *expr_const_operands = declspecs->expr_const_operands;
6037 if (decl_context == FUNCDEF)
6038 funcdef_flag = true, decl_context = NORMAL;
6040 /* Look inside a declarator for the name being declared
6041 and get it as an IDENTIFIER_NODE, for an error message. */
6043 const struct c_declarator *decl = declarator;
6045 first_non_attr_kind = cdk_attrs;
6046 while (decl)
6047 switch (decl->kind)
6049 case cdk_array:
6050 loc = decl->id_loc;
6051 /* FALL THRU. */
6053 case cdk_function:
6054 case cdk_pointer:
6055 funcdef_syntax = (decl->kind == cdk_function);
6056 if (first_non_attr_kind == cdk_attrs)
6057 first_non_attr_kind = decl->kind;
6058 decl = decl->declarator;
6059 break;
6061 case cdk_attrs:
6062 decl = decl->declarator;
6063 break;
6065 case cdk_id:
6066 loc = decl->id_loc;
6067 if (decl->u.id.id)
6068 name = decl->u.id.id;
6069 decl_id_attrs = decl->u.id.attrs;
6070 if (first_non_attr_kind == cdk_attrs)
6071 first_non_attr_kind = decl->kind;
6072 decl = 0;
6073 break;
6075 default:
6076 gcc_unreachable ();
6078 if (name == NULL_TREE)
6080 gcc_assert (decl_context == PARM
6081 || decl_context == TYPENAME
6082 || (decl_context == FIELD
6083 && declarator->kind == cdk_id));
6084 gcc_assert (!initialized);
6088 /* A function definition's declarator must have the form of
6089 a function declarator. */
6091 if (funcdef_flag && !funcdef_syntax)
6092 return NULL_TREE;
6094 /* If this looks like a function definition, make it one,
6095 even if it occurs where parms are expected.
6096 Then store_parm_decls will reject it and not use it as a parm. */
6097 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6098 decl_context = PARM;
6100 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
6101 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6103 if ((decl_context == NORMAL || decl_context == FIELD)
6104 && current_scope == file_scope
6105 && variably_modified_type_p (type, NULL_TREE))
6107 if (name)
6108 error_at (loc, "variably modified %qE at file scope", name);
6109 else
6110 error_at (loc, "variably modified field at file scope");
6111 type = integer_type_node;
6114 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6116 /* Diagnose defaulting to "int". */
6118 if (declspecs->default_int_p && !in_system_header_at (input_location))
6120 /* Issue a warning if this is an ISO C 99 program or if
6121 -Wreturn-type and this is a function, or if -Wimplicit;
6122 prefer the former warning since it is more explicit. */
6123 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6124 && funcdef_flag)
6125 warn_about_return_type = 1;
6126 else
6128 if (name)
6129 warn_defaults_to (loc, OPT_Wimplicit_int,
6130 "type defaults to %<int%> in declaration "
6131 "of %qE", name);
6132 else
6133 warn_defaults_to (loc, OPT_Wimplicit_int,
6134 "type defaults to %<int%> in type name");
6138 /* Adjust the type if a bit-field is being declared,
6139 -funsigned-bitfields applied and the type is not explicitly
6140 "signed". */
6141 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6142 && TREE_CODE (type) == INTEGER_TYPE)
6143 type = unsigned_type_for (type);
6145 /* Figure out the type qualifiers for the declaration. There are
6146 two ways a declaration can become qualified. One is something
6147 like `const int i' where the `const' is explicit. Another is
6148 something like `typedef const int CI; CI i' where the type of the
6149 declaration contains the `const'. A third possibility is that
6150 there is a type qualifier on the element type of a typedefed
6151 array type, in which case we should extract that qualifier so
6152 that c_apply_type_quals_to_decl receives the full list of
6153 qualifiers to work with (C90 is not entirely clear about whether
6154 duplicate qualifiers should be diagnosed in this case, but it
6155 seems most appropriate to do so). */
6156 element_type = strip_array_types (type);
6157 constp = declspecs->const_p + TYPE_READONLY (element_type);
6158 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6159 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6160 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6161 as1 = declspecs->address_space;
6162 as2 = TYPE_ADDR_SPACE (element_type);
6163 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6165 if (constp > 1)
6166 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6167 if (restrictp > 1)
6168 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6169 if (volatilep > 1)
6170 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6171 if (atomicp > 1)
6172 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6174 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6175 error_at (loc, "conflicting named address spaces (%s vs %s)",
6176 c_addr_space_name (as1), c_addr_space_name (as2));
6178 if ((TREE_CODE (type) == ARRAY_TYPE
6179 || first_non_attr_kind == cdk_array)
6180 && TYPE_QUALS (element_type))
6182 orig_qual_type = type;
6183 type = TYPE_MAIN_VARIANT (type);
6185 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6186 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6187 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6188 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6189 | ENCODE_QUAL_ADDR_SPACE (address_space));
6190 if (type_quals != TYPE_QUALS (element_type))
6191 orig_qual_type = NULL_TREE;
6193 /* Applying the _Atomic qualifier to an array type (through the use
6194 of typedefs or typeof) must be detected here. If the qualifier
6195 is introduced later, any appearance of applying it to an array is
6196 actually applying it to an element of that array. */
6197 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6198 error_at (loc, "%<_Atomic%>-qualified array type");
6200 /* Warn about storage classes that are invalid for certain
6201 kinds of declarations (parameters, typenames, etc.). */
6203 if (funcdef_flag
6204 && (threadp
6205 || storage_class == csc_auto
6206 || storage_class == csc_register
6207 || storage_class == csc_typedef))
6209 if (storage_class == csc_auto)
6210 pedwarn (loc,
6211 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6212 "function definition declared %<auto%>");
6213 if (storage_class == csc_register)
6214 error_at (loc, "function definition declared %<register%>");
6215 if (storage_class == csc_typedef)
6216 error_at (loc, "function definition declared %<typedef%>");
6217 if (threadp)
6218 error_at (loc, "function definition declared %qs",
6219 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6220 threadp = false;
6221 if (storage_class == csc_auto
6222 || storage_class == csc_register
6223 || storage_class == csc_typedef)
6224 storage_class = csc_none;
6226 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6228 if (decl_context == PARM && storage_class == csc_register)
6230 else
6232 switch (decl_context)
6234 case FIELD:
6235 if (name)
6236 error_at (loc, "storage class specified for structure "
6237 "field %qE", name);
6238 else
6239 error_at (loc, "storage class specified for structure field");
6240 break;
6241 case PARM:
6242 if (name)
6243 error_at (loc, "storage class specified for parameter %qE",
6244 name);
6245 else
6246 error_at (loc, "storage class specified for unnamed parameter");
6247 break;
6248 default:
6249 error_at (loc, "storage class specified for typename");
6250 break;
6252 storage_class = csc_none;
6253 threadp = false;
6256 else if (storage_class == csc_extern
6257 && initialized
6258 && !funcdef_flag)
6260 /* 'extern' with initialization is invalid if not at file scope. */
6261 if (current_scope == file_scope)
6263 /* It is fine to have 'extern const' when compiling at C
6264 and C++ intersection. */
6265 if (!(warn_cxx_compat && constp))
6266 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6267 name);
6269 else
6270 error_at (loc, "%qE has both %<extern%> and initializer", name);
6272 else if (current_scope == file_scope)
6274 if (storage_class == csc_auto)
6275 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6276 name);
6277 if (pedantic && storage_class == csc_register)
6278 pedwarn (input_location, OPT_Wpedantic,
6279 "file-scope declaration of %qE specifies %<register%>", name);
6281 else
6283 if (storage_class == csc_extern && funcdef_flag)
6284 error_at (loc, "nested function %qE declared %<extern%>", name);
6285 else if (threadp && storage_class == csc_none)
6287 error_at (loc, "function-scope %qE implicitly auto and declared "
6288 "%qs", name,
6289 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6290 threadp = false;
6294 /* Now figure out the structure of the declarator proper.
6295 Descend through it, creating more complex types, until we reach
6296 the declared identifier (or NULL_TREE, in an absolute declarator).
6297 At each stage we maintain an unqualified version of the type
6298 together with any qualifiers that should be applied to it with
6299 c_build_qualified_type; this way, array types including
6300 multidimensional array types are first built up in unqualified
6301 form and then the qualified form is created with
6302 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6304 while (declarator && declarator->kind != cdk_id)
6306 if (type == error_mark_node)
6308 declarator = declarator->declarator;
6309 continue;
6312 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6313 a cdk_pointer (for *...),
6314 a cdk_function (for ...(...)),
6315 a cdk_attrs (for nested attributes),
6316 or a cdk_id (for the name being declared
6317 or the place in an absolute declarator
6318 where the name was omitted).
6319 For the last case, we have just exited the loop.
6321 At this point, TYPE is the type of elements of an array,
6322 or for a function to return, or for a pointer to point to.
6323 After this sequence of ifs, TYPE is the type of the
6324 array or function or pointer, and DECLARATOR has had its
6325 outermost layer removed. */
6327 if (array_ptr_quals != TYPE_UNQUALIFIED
6328 || array_ptr_attrs != NULL_TREE
6329 || array_parm_static)
6331 /* Only the innermost declarator (making a parameter be of
6332 array type which is converted to pointer type)
6333 may have static or type qualifiers. */
6334 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6335 array_ptr_quals = TYPE_UNQUALIFIED;
6336 array_ptr_attrs = NULL_TREE;
6337 array_parm_static = false;
6340 switch (declarator->kind)
6342 case cdk_attrs:
6344 /* A declarator with embedded attributes. */
6345 tree attrs = declarator->u.attrs;
6346 const struct c_declarator *inner_decl;
6347 int attr_flags = 0;
6348 declarator = declarator->declarator;
6349 /* Standard attribute syntax precisely defines what entity
6350 an attribute in each position appertains to, so only
6351 apply laxity about positioning to GNU attribute syntax.
6352 Standard attributes applied to a function or array
6353 declarator apply exactly to that type; standard
6354 attributes applied to the identifier apply to the
6355 declaration rather than to the type, and are specified
6356 using a cdk_id declarator rather than using
6357 cdk_attrs. */
6358 inner_decl = declarator;
6359 while (inner_decl->kind == cdk_attrs)
6360 inner_decl = inner_decl->declarator;
6361 if (!cxx11_attribute_p (attrs))
6363 if (inner_decl->kind == cdk_id)
6364 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6365 else if (inner_decl->kind == cdk_function)
6366 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6367 else if (inner_decl->kind == cdk_array)
6368 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6370 attrs = c_warn_type_attributes (attrs);
6371 returned_attrs = decl_attributes (&type,
6372 chainon (returned_attrs, attrs),
6373 attr_flags);
6374 break;
6376 case cdk_array:
6378 tree itype = NULL_TREE;
6379 tree size = declarator->u.array.dimen;
6380 /* The index is a signed object `sizetype' bits wide. */
6381 tree index_type = c_common_signed_type (sizetype);
6383 array_ptr_quals = declarator->u.array.quals;
6384 array_ptr_attrs = declarator->u.array.attrs;
6385 array_parm_static = declarator->u.array.static_p;
6386 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6388 declarator = declarator->declarator;
6390 /* Check for some types that there cannot be arrays of. */
6392 if (VOID_TYPE_P (type))
6394 if (name)
6395 error_at (loc, "declaration of %qE as array of voids", name);
6396 else
6397 error_at (loc, "declaration of type name as array of voids");
6398 type = error_mark_node;
6401 if (TREE_CODE (type) == FUNCTION_TYPE)
6403 if (name)
6404 error_at (loc, "declaration of %qE as array of functions",
6405 name);
6406 else
6407 error_at (loc, "declaration of type name as array of "
6408 "functions");
6409 type = error_mark_node;
6412 if (pedantic && !in_system_header_at (input_location)
6413 && flexible_array_type_p (type))
6414 pedwarn (loc, OPT_Wpedantic,
6415 "invalid use of structure with flexible array member");
6417 if (size == error_mark_node)
6418 type = error_mark_node;
6420 if (type == error_mark_node)
6421 continue;
6423 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6425 type = error_mark_node;
6426 continue;
6429 /* If size was specified, set ITYPE to a range-type for
6430 that size. Otherwise, ITYPE remains null. finish_decl
6431 may figure it out from an initial value. */
6433 if (size)
6435 bool size_maybe_const = true;
6436 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6437 && !TREE_OVERFLOW (size));
6438 bool this_size_varies = false;
6440 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6441 lvalue. */
6442 STRIP_TYPE_NOPS (size);
6444 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6446 if (name)
6447 error_at (loc, "size of array %qE has non-integer type",
6448 name);
6449 else
6450 error_at (loc,
6451 "size of unnamed array has non-integer type");
6452 size = integer_one_node;
6453 size_int_const = true;
6455 /* This can happen with enum forward declaration. */
6456 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6458 if (name)
6459 error_at (loc, "size of array %qE has incomplete type",
6460 name);
6461 else
6462 error_at (loc, "size of unnamed array has incomplete "
6463 "type");
6464 size = integer_one_node;
6465 size_int_const = true;
6468 size = c_fully_fold (size, false, &size_maybe_const);
6470 if (pedantic && size_maybe_const && integer_zerop (size))
6472 if (name)
6473 pedwarn (loc, OPT_Wpedantic,
6474 "ISO C forbids zero-size array %qE", name);
6475 else
6476 pedwarn (loc, OPT_Wpedantic,
6477 "ISO C forbids zero-size array");
6480 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6482 constant_expression_warning (size);
6483 if (tree_int_cst_sgn (size) < 0)
6485 if (name)
6486 error_at (loc, "size of array %qE is negative", name);
6487 else
6488 error_at (loc, "size of unnamed array is negative");
6489 size = integer_one_node;
6490 size_int_const = true;
6492 /* Handle a size folded to an integer constant but
6493 not an integer constant expression. */
6494 if (!size_int_const)
6496 /* If this is a file scope declaration of an
6497 ordinary identifier, this is invalid code;
6498 diagnosing it here and not subsequently
6499 treating the type as variable-length avoids
6500 more confusing diagnostics later. */
6501 if ((decl_context == NORMAL || decl_context == FIELD)
6502 && current_scope == file_scope)
6503 pedwarn (input_location, 0,
6504 "variably modified %qE at file scope",
6505 name);
6506 else
6507 this_size_varies = size_varies = true;
6508 warn_variable_length_array (name, size);
6511 else if ((decl_context == NORMAL || decl_context == FIELD)
6512 && current_scope == file_scope)
6514 error_at (loc, "variably modified %qE at file scope", name);
6515 size = integer_one_node;
6517 else
6519 /* Make sure the array size remains visibly
6520 nonconstant even if it is (eg) a const variable
6521 with known value. */
6522 this_size_varies = size_varies = true;
6523 warn_variable_length_array (name, size);
6524 if (sanitize_flags_p (SANITIZE_VLA)
6525 && current_function_decl != NULL_TREE
6526 && decl_context == NORMAL)
6528 /* Evaluate the array size only once. */
6529 size = save_expr (size);
6530 size = c_fully_fold (size, false, NULL);
6531 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6532 ubsan_instrument_vla (loc, size),
6533 size);
6537 if (integer_zerop (size) && !this_size_varies)
6539 /* A zero-length array cannot be represented with
6540 an unsigned index type, which is what we'll
6541 get with build_index_type. Create an
6542 open-ended range instead. */
6543 itype = build_range_type (sizetype, size, NULL_TREE);
6545 else
6547 /* Arrange for the SAVE_EXPR on the inside of the
6548 MINUS_EXPR, which allows the -1 to get folded
6549 with the +1 that happens when building TYPE_SIZE. */
6550 if (size_varies)
6551 size = save_expr (size);
6552 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6553 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6554 integer_zero_node, size);
6556 /* Compute the maximum valid index, that is, size
6557 - 1. Do the calculation in index_type, so that
6558 if it is a variable the computations will be
6559 done in the proper mode. */
6560 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6561 convert (index_type, size),
6562 convert (index_type,
6563 size_one_node));
6565 /* The above overflows when size does not fit
6566 in index_type.
6567 ??? While a size of INT_MAX+1 technically shouldn't
6568 cause an overflow (because we subtract 1), handling
6569 this case seems like an unnecessary complication. */
6570 if (TREE_CODE (size) == INTEGER_CST
6571 && !int_fits_type_p (size, index_type))
6573 if (name)
6574 error_at (loc, "size of array %qE is too large",
6575 name);
6576 else
6577 error_at (loc, "size of unnamed array is too large");
6578 type = error_mark_node;
6579 continue;
6582 itype = build_index_type (itype);
6584 if (this_size_varies)
6586 if (TREE_SIDE_EFFECTS (size))
6588 if (*expr)
6589 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6590 *expr, size);
6591 else
6592 *expr = size;
6594 *expr_const_operands &= size_maybe_const;
6597 else if (decl_context == FIELD)
6599 bool flexible_array_member = false;
6600 if (array_parm_vla_unspec_p)
6601 /* Field names can in fact have function prototype
6602 scope so [*] is disallowed here through making
6603 the field variably modified, not through being
6604 something other than a declaration with function
6605 prototype scope. */
6606 size_varies = true;
6607 else
6609 const struct c_declarator *t = declarator;
6610 while (t->kind == cdk_attrs)
6611 t = t->declarator;
6612 flexible_array_member = (t->kind == cdk_id);
6614 if (flexible_array_member
6615 && !in_system_header_at (input_location))
6616 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6617 "support flexible array members");
6619 /* ISO C99 Flexible array members are effectively
6620 identical to GCC's zero-length array extension. */
6621 if (flexible_array_member || array_parm_vla_unspec_p)
6622 itype = build_range_type (sizetype, size_zero_node,
6623 NULL_TREE);
6625 else if (decl_context == PARM)
6627 if (array_parm_vla_unspec_p)
6629 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6630 size_varies = true;
6633 else if (decl_context == TYPENAME)
6635 if (array_parm_vla_unspec_p)
6637 /* C99 6.7.5.2p4 */
6638 warning (0, "%<[*]%> not in a declaration");
6639 /* We use this to avoid messing up with incomplete
6640 array types of the same type, that would
6641 otherwise be modified below. */
6642 itype = build_range_type (sizetype, size_zero_node,
6643 NULL_TREE);
6644 size_varies = true;
6648 /* Complain about arrays of incomplete types. */
6649 if (!COMPLETE_TYPE_P (type))
6651 error_at (loc, "array type has incomplete element type %qT",
6652 type);
6653 /* See if we can be more helpful. */
6654 if (TREE_CODE (type) == ARRAY_TYPE)
6656 if (name)
6657 inform (loc, "declaration of %qE as multidimensional "
6658 "array must have bounds for all dimensions "
6659 "except the first", name);
6660 else
6661 inform (loc, "declaration of multidimensional array "
6662 "must have bounds for all dimensions except "
6663 "the first");
6665 type = error_mark_node;
6667 else
6668 /* When itype is NULL, a shared incomplete array type is
6669 returned for all array of a given type. Elsewhere we
6670 make sure we don't complete that type before copying
6671 it, but here we want to make sure we don't ever
6672 modify the shared type, so we gcc_assert (itype)
6673 below. */
6675 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6676 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6677 type = build_qualified_type (type,
6678 ENCODE_QUAL_ADDR_SPACE (as));
6680 type = build_array_type (type, itype);
6683 if (type != error_mark_node)
6685 if (size_varies)
6687 /* It is ok to modify type here even if itype is
6688 NULL: if size_varies, we're in a
6689 multi-dimensional array and the inner type has
6690 variable size, so the enclosing shared array type
6691 must too. */
6692 if (size && TREE_CODE (size) == INTEGER_CST)
6693 type
6694 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6695 C_TYPE_VARIABLE_SIZE (type) = 1;
6698 /* The GCC extension for zero-length arrays differs from
6699 ISO flexible array members in that sizeof yields
6700 zero. */
6701 if (size && integer_zerop (size))
6703 gcc_assert (itype);
6704 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6705 TYPE_SIZE (type) = bitsize_zero_node;
6706 TYPE_SIZE_UNIT (type) = size_zero_node;
6707 SET_TYPE_STRUCTURAL_EQUALITY (type);
6709 if (array_parm_vla_unspec_p)
6711 gcc_assert (itype);
6712 /* The type is complete. C99 6.7.5.2p4 */
6713 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6714 TYPE_SIZE (type) = bitsize_zero_node;
6715 TYPE_SIZE_UNIT (type) = size_zero_node;
6716 SET_TYPE_STRUCTURAL_EQUALITY (type);
6719 if (!valid_array_size_p (loc, type, name))
6720 type = error_mark_node;
6723 if (decl_context != PARM
6724 && (array_ptr_quals != TYPE_UNQUALIFIED
6725 || array_ptr_attrs != NULL_TREE
6726 || array_parm_static))
6728 error_at (loc, "static or type qualifiers in non-parameter "
6729 "array declarator");
6730 array_ptr_quals = TYPE_UNQUALIFIED;
6731 array_ptr_attrs = NULL_TREE;
6732 array_parm_static = false;
6734 orig_qual_indirect++;
6735 break;
6737 case cdk_function:
6739 /* Say it's a definition only for the declarator closest
6740 to the identifier, apart possibly from some
6741 attributes. */
6742 bool really_funcdef = false;
6743 tree arg_types;
6744 orig_qual_type = NULL_TREE;
6745 if (funcdef_flag)
6747 const struct c_declarator *t = declarator->declarator;
6748 while (t->kind == cdk_attrs)
6749 t = t->declarator;
6750 really_funcdef = (t->kind == cdk_id);
6753 /* Declaring a function type. Make sure we have a valid
6754 type for the function to return. */
6755 if (type == error_mark_node)
6756 continue;
6758 size_varies = false;
6760 /* Warn about some types functions can't return. */
6761 if (TREE_CODE (type) == FUNCTION_TYPE)
6763 if (name)
6764 error_at (loc, "%qE declared as function returning a "
6765 "function", name);
6766 else
6767 error_at (loc, "type name declared as function "
6768 "returning a function");
6769 type = integer_type_node;
6771 if (TREE_CODE (type) == ARRAY_TYPE)
6773 if (name)
6774 error_at (loc, "%qE declared as function returning an array",
6775 name);
6776 else
6777 error_at (loc, "type name declared as function returning "
6778 "an array");
6779 type = integer_type_node;
6782 /* Construct the function type and go to the next
6783 inner layer of declarator. */
6784 arg_info = declarator->u.arg_info;
6785 arg_types = grokparms (arg_info, really_funcdef);
6787 /* Type qualifiers before the return type of the function
6788 qualify the return type, not the function type. */
6789 if (type_quals)
6791 const enum c_declspec_word ignored_quals_list[] =
6793 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6794 cdw_atomic, cdw_number_of_elements
6796 location_t specs_loc
6797 = smallest_type_quals_location (declspecs->locations,
6798 ignored_quals_list);
6799 if (specs_loc == UNKNOWN_LOCATION)
6800 specs_loc = declspecs->locations[cdw_typedef];
6801 if (specs_loc == UNKNOWN_LOCATION)
6802 specs_loc = loc;
6804 /* Type qualifiers on a function return type are
6805 normally permitted by the standard but have no
6806 effect, so give a warning at -Wreturn-type.
6807 Qualifiers on a void return type are banned on
6808 function definitions in ISO C; GCC used to used
6809 them for noreturn functions. The resolution of C11
6810 DR#423 means qualifiers (other than _Atomic) are
6811 actually removed from the return type when
6812 determining the function type. */
6813 int quals_used = type_quals;
6814 if (flag_isoc11)
6815 quals_used &= TYPE_QUAL_ATOMIC;
6816 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6817 pedwarn (specs_loc, 0,
6818 "function definition has qualified void "
6819 "return type");
6820 else
6821 warning_at (specs_loc, OPT_Wignored_qualifiers,
6822 "type qualifiers ignored on function "
6823 "return type");
6825 /* Ensure an error for restrict on invalid types; the
6826 DR#423 resolution is not entirely clear about
6827 this. */
6828 if (flag_isoc11
6829 && (type_quals & TYPE_QUAL_RESTRICT)
6830 && (!POINTER_TYPE_P (type)
6831 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6832 error_at (loc, "invalid use of %<restrict%>");
6833 type = c_build_qualified_type (type, quals_used);
6835 type_quals = TYPE_UNQUALIFIED;
6837 type = build_function_type (type, arg_types);
6838 declarator = declarator->declarator;
6840 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6841 the formal parameter list of this FUNCTION_TYPE to point to
6842 the FUNCTION_TYPE node itself. */
6844 c_arg_tag *tag;
6845 unsigned ix;
6847 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6848 TYPE_CONTEXT (tag->type) = type;
6850 break;
6852 case cdk_pointer:
6854 /* Merge any constancy or volatility into the target type
6855 for the pointer. */
6856 if ((type_quals & TYPE_QUAL_ATOMIC)
6857 && TREE_CODE (type) == FUNCTION_TYPE)
6859 error_at (loc,
6860 "%<_Atomic%>-qualified function type");
6861 type_quals &= ~TYPE_QUAL_ATOMIC;
6863 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6864 && type_quals)
6865 pedwarn (loc, OPT_Wpedantic,
6866 "ISO C forbids qualified function types");
6867 if (type_quals)
6868 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6869 orig_qual_indirect);
6870 orig_qual_type = NULL_TREE;
6871 size_varies = false;
6873 /* When the pointed-to type involves components of variable size,
6874 care must be taken to ensure that the size evaluation code is
6875 emitted early enough to dominate all the possible later uses
6876 and late enough for the variables on which it depends to have
6877 been assigned.
6879 This is expected to happen automatically when the pointed-to
6880 type has a name/declaration of it's own, but special attention
6881 is required if the type is anonymous.
6883 We attach an artificial TYPE_DECL to such pointed-to type
6884 and arrange for it to be included in a DECL_EXPR. This
6885 forces the sizes evaluation at a safe point and ensures it
6886 is not deferred until e.g. within a deeper conditional context.
6888 PARM contexts have no enclosing statement list that
6889 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6890 instead, and add it to the list of expressions that
6891 need to be evaluated.
6893 TYPENAME contexts do have an enclosing statement list,
6894 but it would be incorrect to use it, as the size should
6895 only be evaluated if the containing expression is
6896 evaluated. We might also be in the middle of an
6897 expression with side effects on the pointed-to type size
6898 "arguments" prior to the pointer declaration point and
6899 the fake TYPE_DECL in the enclosing context would force
6900 the size evaluation prior to the side effects. We therefore
6901 use BIND_EXPRs in TYPENAME contexts too. */
6902 if (!TYPE_NAME (type)
6903 && variably_modified_type_p (type, NULL_TREE))
6905 tree bind = NULL_TREE;
6906 if (decl_context == TYPENAME || decl_context == PARM)
6908 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6909 NULL_TREE, NULL_TREE);
6910 TREE_SIDE_EFFECTS (bind) = 1;
6911 BIND_EXPR_BODY (bind) = push_stmt_list ();
6912 push_scope ();
6914 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6915 DECL_ARTIFICIAL (decl) = 1;
6916 pushdecl (decl);
6917 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6918 TYPE_NAME (type) = decl;
6919 if (bind)
6921 pop_scope ();
6922 BIND_EXPR_BODY (bind)
6923 = pop_stmt_list (BIND_EXPR_BODY (bind));
6924 if (*expr)
6925 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6926 bind);
6927 else
6928 *expr = bind;
6932 type = c_build_pointer_type (type);
6934 /* Process type qualifiers (such as const or volatile)
6935 that were given inside the `*'. */
6936 type_quals = declarator->u.pointer_quals;
6938 declarator = declarator->declarator;
6939 break;
6941 default:
6942 gcc_unreachable ();
6945 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6946 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
6948 /* Now TYPE has the actual type, apart from any qualifiers in
6949 TYPE_QUALS. */
6951 /* Warn about address space used for things other than static memory or
6952 pointers. */
6953 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6954 if (!ADDR_SPACE_GENERIC_P (address_space))
6956 if (decl_context == NORMAL)
6958 switch (storage_class)
6960 case csc_auto:
6961 error ("%qs combined with %<auto%> qualifier for %qE",
6962 c_addr_space_name (address_space), name);
6963 break;
6964 case csc_register:
6965 error ("%qs combined with %<register%> qualifier for %qE",
6966 c_addr_space_name (address_space), name);
6967 break;
6968 case csc_none:
6969 if (current_function_scope)
6971 error ("%qs specified for auto variable %qE",
6972 c_addr_space_name (address_space), name);
6973 break;
6975 break;
6976 case csc_static:
6977 case csc_extern:
6978 case csc_typedef:
6979 break;
6980 default:
6981 gcc_unreachable ();
6984 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6986 if (name)
6987 error ("%qs specified for parameter %qE",
6988 c_addr_space_name (address_space), name);
6989 else
6990 error ("%qs specified for unnamed parameter",
6991 c_addr_space_name (address_space));
6993 else if (decl_context == FIELD)
6995 if (name)
6996 error ("%qs specified for structure field %qE",
6997 c_addr_space_name (address_space), name);
6998 else
6999 error ("%qs specified for structure field",
7000 c_addr_space_name (address_space));
7004 /* Check the type and width of a bit-field. */
7005 if (bitfield)
7007 check_bitfield_type_and_width (loc, &type, width, name);
7008 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7009 atomic types are permitted for bit-fields; we have no code to
7010 make bit-field accesses atomic, so disallow them. */
7011 if (type_quals & TYPE_QUAL_ATOMIC)
7013 if (name)
7014 error_at (loc, "bit-field %qE has atomic type", name);
7015 else
7016 error_at (loc, "bit-field has atomic type");
7017 type_quals &= ~TYPE_QUAL_ATOMIC;
7021 /* Reject invalid uses of _Alignas. */
7022 if (declspecs->alignas_p)
7024 if (storage_class == csc_typedef)
7025 error_at (loc, "alignment specified for typedef %qE", name);
7026 else if (storage_class == csc_register)
7027 error_at (loc, "alignment specified for %<register%> object %qE",
7028 name);
7029 else if (decl_context == PARM)
7031 if (name)
7032 error_at (loc, "alignment specified for parameter %qE", name);
7033 else
7034 error_at (loc, "alignment specified for unnamed parameter");
7036 else if (bitfield)
7038 if (name)
7039 error_at (loc, "alignment specified for bit-field %qE", name);
7040 else
7041 error_at (loc, "alignment specified for unnamed bit-field");
7043 else if (TREE_CODE (type) == FUNCTION_TYPE)
7044 error_at (loc, "alignment specified for function %qE", name);
7045 else if (declspecs->align_log != -1 && TYPE_P (type))
7047 alignas_align = 1U << declspecs->align_log;
7048 if (alignas_align < min_align_of_type (type))
7050 if (name)
7051 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7052 "alignment of %qE", name);
7053 else
7054 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7055 "alignment of unnamed field");
7056 alignas_align = 0;
7061 /* If this is declaring a typedef name, return a TYPE_DECL. */
7063 if (storage_class == csc_typedef)
7065 tree decl;
7066 if ((type_quals & TYPE_QUAL_ATOMIC)
7067 && TREE_CODE (type) == FUNCTION_TYPE)
7069 error_at (loc,
7070 "%<_Atomic%>-qualified function type");
7071 type_quals &= ~TYPE_QUAL_ATOMIC;
7073 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7074 && type_quals)
7075 pedwarn (loc, OPT_Wpedantic,
7076 "ISO C forbids qualified function types");
7077 if (type_quals)
7078 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7079 orig_qual_indirect);
7080 decl = build_decl (declarator->id_loc,
7081 TYPE_DECL, declarator->u.id.id, type);
7082 if (declspecs->explicit_signed_p)
7083 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7084 if (declspecs->inline_p)
7085 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7086 if (declspecs->noreturn_p)
7087 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7089 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7091 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7093 if (b != NULL
7094 && b->decl != NULL_TREE
7095 && (B_IN_CURRENT_SCOPE (b)
7096 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7097 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7099 auto_diagnostic_group d;
7100 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7101 ("using %qD as both a typedef and a tag is "
7102 "invalid in C++"), decl)
7103 && b->locus != UNKNOWN_LOCATION)
7104 inform (b->locus, "originally defined here");
7108 return decl;
7111 /* If this is a type name (such as, in a cast or sizeof),
7112 compute the type and return it now. */
7114 if (decl_context == TYPENAME)
7116 /* Note that the grammar rejects storage classes in typenames
7117 and fields. */
7118 gcc_assert (storage_class == csc_none && !threadp
7119 && !declspecs->inline_p && !declspecs->noreturn_p);
7120 if ((type_quals & TYPE_QUAL_ATOMIC)
7121 && TREE_CODE (type) == FUNCTION_TYPE)
7123 error_at (loc,
7124 "%<_Atomic%>-qualified function type");
7125 type_quals &= ~TYPE_QUAL_ATOMIC;
7127 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7128 && type_quals)
7129 pedwarn (loc, OPT_Wpedantic,
7130 "ISO C forbids const or volatile function types");
7131 if (type_quals)
7132 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7133 orig_qual_indirect);
7134 return type;
7137 if (pedantic && decl_context == FIELD
7138 && variably_modified_type_p (type, NULL_TREE))
7140 /* C99 6.7.2.1p8 */
7141 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7142 "have a variably modified type");
7145 /* Aside from typedefs and type names (handle above),
7146 `void' at top level (not within pointer)
7147 is allowed only in public variables.
7148 We don't complain about parms either, but that is because
7149 a better error message can be made later. */
7151 if (VOID_TYPE_P (type) && decl_context != PARM
7152 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7153 && (storage_class == csc_extern
7154 || (current_scope == file_scope
7155 && !(storage_class == csc_static
7156 || storage_class == csc_register)))))
7158 error_at (loc, "variable or field %qE declared void", name);
7159 type = integer_type_node;
7162 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7163 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7166 tree decl;
7168 if (decl_context == PARM)
7170 tree promoted_type;
7171 bool array_parameter_p = false;
7173 /* A parameter declared as an array of T is really a pointer to T.
7174 One declared as a function is really a pointer to a function. */
7176 if (TREE_CODE (type) == ARRAY_TYPE)
7178 /* Transfer const-ness of array into that of type pointed to. */
7179 type = TREE_TYPE (type);
7180 if (orig_qual_type != NULL_TREE)
7182 if (orig_qual_indirect == 0)
7183 orig_qual_type = TREE_TYPE (orig_qual_type);
7184 else
7185 orig_qual_indirect--;
7187 if (type_quals)
7188 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7189 orig_qual_indirect);
7190 type = c_build_pointer_type (type);
7191 type_quals = array_ptr_quals;
7192 if (type_quals)
7193 type = c_build_qualified_type (type, type_quals);
7195 /* We don't yet implement attributes in this context. */
7196 if (array_ptr_attrs != NULL_TREE)
7197 warning_at (loc, OPT_Wattributes,
7198 "attributes in parameter array declarator ignored");
7200 size_varies = false;
7201 array_parameter_p = true;
7203 else if (TREE_CODE (type) == FUNCTION_TYPE)
7205 if (type_quals & TYPE_QUAL_ATOMIC)
7207 error_at (loc,
7208 "%<_Atomic%>-qualified function type");
7209 type_quals &= ~TYPE_QUAL_ATOMIC;
7211 else if (type_quals)
7212 pedwarn (loc, OPT_Wpedantic,
7213 "ISO C forbids qualified function types");
7214 if (type_quals)
7215 type = c_build_qualified_type (type, type_quals);
7216 type = c_build_pointer_type (type);
7217 type_quals = TYPE_UNQUALIFIED;
7219 else if (type_quals)
7220 type = c_build_qualified_type (type, type_quals);
7222 decl = build_decl (declarator->id_loc,
7223 PARM_DECL, declarator->u.id.id, type);
7224 if (size_varies)
7225 C_DECL_VARIABLE_SIZE (decl) = 1;
7226 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7228 /* Compute the type actually passed in the parmlist,
7229 for the case where there is no prototype.
7230 (For example, shorts and chars are passed as ints.)
7231 When there is a prototype, this is overridden later. */
7233 if (type == error_mark_node)
7234 promoted_type = type;
7235 else
7236 promoted_type = c_type_promotes_to (type);
7238 DECL_ARG_TYPE (decl) = promoted_type;
7239 if (declspecs->inline_p)
7240 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7241 if (declspecs->noreturn_p)
7242 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7244 else if (decl_context == FIELD)
7246 /* Note that the grammar rejects storage classes in typenames
7247 and fields. */
7248 gcc_assert (storage_class == csc_none && !threadp
7249 && !declspecs->inline_p && !declspecs->noreturn_p);
7251 /* Structure field. It may not be a function. */
7253 if (TREE_CODE (type) == FUNCTION_TYPE)
7255 error_at (loc, "field %qE declared as a function", name);
7256 type = build_pointer_type (type);
7258 else if (TREE_CODE (type) != ERROR_MARK
7259 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7261 if (name)
7262 error_at (loc, "field %qE has incomplete type", name);
7263 else
7264 error_at (loc, "unnamed field has incomplete type");
7265 type = error_mark_node;
7267 else if (TREE_CODE (type) == ARRAY_TYPE
7268 && TYPE_DOMAIN (type) == NULL_TREE)
7270 /* We have a flexible array member through a typedef.
7271 Set suitable range. Whether this is a correct position
7272 for a flexible array member will be determined elsewhere. */
7273 if (!in_system_header_at (input_location))
7274 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7275 "support flexible array members");
7276 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7277 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7278 NULL_TREE);
7279 if (orig_qual_indirect == 0)
7280 orig_qual_type = NULL_TREE;
7282 if (type != error_mark_node
7283 && !verify_type_context (loc, TCTX_FIELD, type))
7284 type = error_mark_node;
7286 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7287 orig_qual_indirect);
7288 decl = build_decl (declarator->id_loc,
7289 FIELD_DECL, declarator->u.id.id, type);
7290 DECL_NONADDRESSABLE_P (decl) = bitfield;
7291 if (bitfield && !declarator->u.id.id)
7293 TREE_NO_WARNING (decl) = 1;
7294 DECL_PADDING_P (decl) = 1;
7297 if (size_varies)
7298 C_DECL_VARIABLE_SIZE (decl) = 1;
7300 else if (TREE_CODE (type) == FUNCTION_TYPE)
7302 if (storage_class == csc_register || threadp)
7304 error_at (loc, "invalid storage class for function %qE", name);
7306 else if (current_scope != file_scope)
7308 /* Function declaration not at file scope. Storage
7309 classes other than `extern' are not allowed, C99
7310 6.7.1p5, and `extern' makes no difference. However,
7311 GCC allows 'auto', perhaps with 'inline', to support
7312 nested functions. */
7313 if (storage_class == csc_auto)
7314 pedwarn (loc, OPT_Wpedantic,
7315 "invalid storage class for function %qE", name);
7316 else if (storage_class == csc_static)
7318 error_at (loc, "invalid storage class for function %qE", name);
7319 if (funcdef_flag)
7320 storage_class = declspecs->storage_class = csc_none;
7321 else
7322 return NULL_TREE;
7326 decl = build_decl (declarator->id_loc,
7327 FUNCTION_DECL, declarator->u.id.id, type);
7328 decl = build_decl_attribute_variant (decl, decl_attr);
7330 if (type_quals & TYPE_QUAL_ATOMIC)
7332 error_at (loc,
7333 "%<_Atomic%>-qualified function type");
7334 type_quals &= ~TYPE_QUAL_ATOMIC;
7336 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7337 pedwarn (loc, OPT_Wpedantic,
7338 "ISO C forbids qualified function types");
7340 /* Every function declaration is an external reference
7341 (DECL_EXTERNAL) except for those which are not at file
7342 scope and are explicitly declared "auto". This is
7343 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7344 GCC to signify a forward declaration of a nested function. */
7345 if (storage_class == csc_auto && current_scope != file_scope)
7346 DECL_EXTERNAL (decl) = 0;
7347 /* In C99, a function which is declared 'inline' with 'extern'
7348 is not an external reference (which is confusing). It
7349 means that the later definition of the function must be output
7350 in this file, C99 6.7.4p6. In GNU C89, a function declared
7351 'extern inline' is an external reference. */
7352 else if (declspecs->inline_p && storage_class != csc_static)
7353 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7354 == flag_gnu89_inline);
7355 else
7356 DECL_EXTERNAL (decl) = !initialized;
7358 /* Record absence of global scope for `static' or `auto'. */
7359 TREE_PUBLIC (decl)
7360 = !(storage_class == csc_static || storage_class == csc_auto);
7362 /* For a function definition, record the argument information
7363 block where store_parm_decls will look for it. */
7364 if (funcdef_flag)
7365 current_function_arg_info = arg_info;
7367 if (declspecs->default_int_p)
7368 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7370 /* Record presence of `inline' and `_Noreturn', if it is
7371 reasonable. */
7372 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7374 if (declspecs->inline_p)
7375 pedwarn (loc, 0, "cannot inline function %<main%>");
7376 if (declspecs->noreturn_p)
7377 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7379 else
7381 if (declspecs->inline_p)
7382 /* Record that the function is declared `inline'. */
7383 DECL_DECLARED_INLINE_P (decl) = 1;
7384 if (declspecs->noreturn_p)
7386 if (flag_isoc99)
7387 pedwarn_c99 (loc, OPT_Wpedantic,
7388 "ISO C99 does not support %<_Noreturn%>");
7389 else
7390 pedwarn_c99 (loc, OPT_Wpedantic,
7391 "ISO C90 does not support %<_Noreturn%>");
7392 TREE_THIS_VOLATILE (decl) = 1;
7396 else
7398 /* It's a variable. */
7399 /* An uninitialized decl with `extern' is a reference. */
7400 int extern_ref = !initialized && storage_class == csc_extern;
7402 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7403 orig_qual_indirect);
7405 /* C99 6.2.2p7: It is invalid (compile-time undefined
7406 behavior) to create an 'extern' declaration for a
7407 variable if there is a global declaration that is
7408 'static' and the global declaration is not visible.
7409 (If the static declaration _is_ currently visible,
7410 the 'extern' declaration is taken to refer to that decl.) */
7411 if (extern_ref && current_scope != file_scope)
7413 tree global_decl = identifier_global_value (declarator->u.id.id);
7414 tree visible_decl = lookup_name (declarator->u.id.id);
7416 if (global_decl
7417 && global_decl != visible_decl
7418 && VAR_P (global_decl)
7419 && !TREE_PUBLIC (global_decl))
7420 error_at (loc, "variable previously declared %<static%> "
7421 "redeclared %<extern%>");
7424 decl = build_decl (declarator->id_loc,
7425 VAR_DECL, declarator->u.id.id, type);
7426 if (size_varies)
7427 C_DECL_VARIABLE_SIZE (decl) = 1;
7429 if (declspecs->inline_p)
7430 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7431 if (declspecs->noreturn_p)
7432 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7434 /* At file scope, an initialized extern declaration may follow
7435 a static declaration. In that case, DECL_EXTERNAL will be
7436 reset later in start_decl. */
7437 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7439 /* At file scope, the presence of a `static' or `register' storage
7440 class specifier, or the absence of all storage class specifiers
7441 makes this declaration a definition (perhaps tentative). Also,
7442 the absence of `static' makes it public. */
7443 if (current_scope == file_scope)
7445 TREE_PUBLIC (decl) = storage_class != csc_static;
7446 TREE_STATIC (decl) = !extern_ref;
7448 /* Not at file scope, only `static' makes a static definition. */
7449 else
7451 TREE_STATIC (decl) = (storage_class == csc_static);
7452 TREE_PUBLIC (decl) = extern_ref;
7455 if (threadp)
7456 set_decl_tls_model (decl, decl_default_tls_model (decl));
7459 if ((storage_class == csc_extern
7460 || (storage_class == csc_none
7461 && TREE_CODE (type) == FUNCTION_TYPE
7462 && !funcdef_flag))
7463 && variably_modified_type_p (type, NULL_TREE))
7465 /* C99 6.7.5.2p2 */
7466 if (TREE_CODE (type) == FUNCTION_TYPE)
7467 error_at (loc, "non-nested function with variably modified type");
7468 else
7469 error_at (loc, "object with variably modified type must have "
7470 "no linkage");
7473 /* For nested functions disqualify ones taking VLAs by value
7474 from inlining since the middle-end cannot deal with this.
7475 ??? We should arrange for those to be passed by reference
7476 with emitting the copy on the caller side in the frontend. */
7477 if (storage_class == csc_none
7478 && TREE_CODE (type) == FUNCTION_TYPE)
7479 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7481 tree arg = TREE_VALUE (al);
7482 if (arg != error_mark_node
7483 && C_TYPE_VARIABLE_SIZE (arg))
7485 DECL_UNINLINABLE (decl) = 1;
7486 break;
7490 /* Record `register' declaration for warnings on &
7491 and in case doing stupid register allocation. */
7493 if (storage_class == csc_register)
7495 C_DECL_REGISTER (decl) = 1;
7496 DECL_REGISTER (decl) = 1;
7499 /* Record constancy and volatility. */
7500 c_apply_type_quals_to_decl (type_quals, decl);
7502 /* Apply _Alignas specifiers. */
7503 if (alignas_align)
7505 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7506 DECL_USER_ALIGN (decl) = 1;
7509 /* If a type has volatile components, it should be stored in memory.
7510 Otherwise, the fact that those components are volatile
7511 will be ignored, and would even crash the compiler.
7512 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7513 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7514 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7515 || TREE_CODE (decl) == RESULT_DECL))
7517 /* It is not an error for a structure with volatile fields to
7518 be declared register, but reset DECL_REGISTER since it
7519 cannot actually go in a register. */
7520 int was_reg = C_DECL_REGISTER (decl);
7521 C_DECL_REGISTER (decl) = 0;
7522 DECL_REGISTER (decl) = 0;
7523 c_mark_addressable (decl);
7524 C_DECL_REGISTER (decl) = was_reg;
7527 /* This is the earliest point at which we might know the assembler
7528 name of a variable. Thus, if it's known before this, die horribly. */
7529 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7530 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7532 if (warn_cxx_compat
7533 && VAR_P (decl)
7534 && TREE_PUBLIC (decl)
7535 && TREE_STATIC (decl)
7536 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7537 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7538 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7539 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7540 ("non-local variable %qD with anonymous type is "
7541 "questionable in C++"),
7542 decl);
7544 return decl;
7548 /* Decode the parameter-list info for a function type or function definition.
7549 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7550 if there is an identifier list instead of a parameter decl list).
7551 These two functions are separate because when a function returns
7552 or receives functions then each is called multiple times but the order
7553 of calls is different. The last call to `grokparms' is always the one
7554 that contains the formal parameter names of a function definition.
7556 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7558 FUNCDEF_FLAG is true for a function definition, false for
7559 a mere declaration. A nonempty identifier-list gets an error message
7560 when FUNCDEF_FLAG is false. */
7562 static tree
7563 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7565 tree arg_types = arg_info->types;
7567 if (funcdef_flag && arg_info->had_vla_unspec)
7569 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7570 /* C99 6.7.5.2p4 */
7571 error ("%<[*]%> not allowed in other than function prototype scope");
7574 if (arg_types == NULL_TREE && !funcdef_flag
7575 && !in_system_header_at (input_location))
7576 warning (OPT_Wstrict_prototypes,
7577 "function declaration isn%'t a prototype");
7579 if (arg_types == error_mark_node)
7580 /* Don't set TYPE_ARG_TYPES in this case. */
7581 return NULL_TREE;
7583 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7585 if (!funcdef_flag)
7587 pedwarn (input_location, 0, "parameter names (without types) in "
7588 "function declaration");
7589 arg_info->parms = NULL_TREE;
7591 else
7592 arg_info->parms = arg_info->types;
7594 arg_info->types = NULL_TREE;
7595 return NULL_TREE;
7597 else
7599 tree parm, type, typelt;
7600 unsigned int parmno;
7602 /* In C2X, convert () in a function definition to (void). */
7603 if (flag_isoc2x
7604 && funcdef_flag
7605 && !arg_types
7606 && !arg_info->parms)
7607 arg_types = arg_info->types = void_list_node;
7609 /* If there is a parameter of incomplete type in a definition,
7610 this is an error. In a declaration this is valid, and a
7611 struct or union type may be completed later, before any calls
7612 or definition of the function. In the case where the tag was
7613 first declared within the parameter list, a warning has
7614 already been given. If a parameter has void type, then
7615 however the function cannot be defined or called, so
7616 warn. */
7618 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7619 parm;
7620 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7622 type = TREE_VALUE (typelt);
7623 if (type == error_mark_node)
7624 continue;
7626 if (!COMPLETE_TYPE_P (type))
7628 if (funcdef_flag)
7630 if (DECL_NAME (parm))
7631 error_at (input_location,
7632 "parameter %u (%q+D) has incomplete type",
7633 parmno, parm);
7634 else
7635 error_at (DECL_SOURCE_LOCATION (parm),
7636 "parameter %u has incomplete type",
7637 parmno);
7639 TREE_VALUE (typelt) = error_mark_node;
7640 TREE_TYPE (parm) = error_mark_node;
7641 arg_types = NULL_TREE;
7643 else if (VOID_TYPE_P (type))
7645 if (DECL_NAME (parm))
7646 warning_at (input_location, 0,
7647 "parameter %u (%q+D) has void type",
7648 parmno, parm);
7649 else
7650 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7651 "parameter %u has void type",
7652 parmno);
7656 if (DECL_NAME (parm) && TREE_USED (parm))
7657 warn_if_shadowing (parm);
7659 return arg_types;
7663 /* Allocate and initialize a c_arg_info structure from the parser's
7664 obstack. */
7666 struct c_arg_info *
7667 build_arg_info (void)
7669 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7670 ret->parms = NULL_TREE;
7671 ret->tags = NULL;
7672 ret->types = NULL_TREE;
7673 ret->others = NULL_TREE;
7674 ret->pending_sizes = NULL;
7675 ret->had_vla_unspec = 0;
7676 return ret;
7679 /* Take apart the current scope and return a c_arg_info structure with
7680 info on a parameter list just parsed.
7682 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7684 ELLIPSIS being true means the argument list ended in '...' so don't
7685 append a sentinel (void_list_node) to the end of the type-list.
7687 EXPR is NULL or an expression that needs to be evaluated for the
7688 side effects of array size expressions in the parameters. */
7690 struct c_arg_info *
7691 get_parm_info (bool ellipsis, tree expr)
7693 struct c_binding *b = current_scope->bindings;
7694 struct c_arg_info *arg_info = build_arg_info ();
7696 tree parms = NULL_TREE;
7697 vec<c_arg_tag, va_gc> *tags = NULL;
7698 tree types = NULL_TREE;
7699 tree others = NULL_TREE;
7701 bool gave_void_only_once_err = false;
7703 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7705 /* The bindings in this scope must not get put into a block.
7706 We will take care of deleting the binding nodes. */
7707 current_scope->bindings = 0;
7709 /* This function is only called if there was *something* on the
7710 parameter list. */
7711 gcc_assert (b);
7713 /* A parameter list consisting solely of 'void' indicates that the
7714 function takes no arguments. But if the 'void' is qualified
7715 (by 'const' or 'volatile'), or has a storage class specifier
7716 ('register'), then the behavior is undefined; issue an error.
7717 Typedefs for 'void' are OK (see DR#157). */
7718 if (b->prev == 0 /* one binding */
7719 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7720 && !DECL_NAME (b->decl) /* anonymous */
7721 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7723 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7724 || C_DECL_REGISTER (b->decl))
7725 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7727 /* There cannot be an ellipsis. */
7728 if (ellipsis)
7729 error_at (b->locus, "%<void%> must be the only parameter");
7731 arg_info->types = void_list_node;
7732 return arg_info;
7735 if (!ellipsis)
7736 types = void_list_node;
7738 /* Break up the bindings list into parms, tags, types, and others;
7739 apply sanity checks; purge the name-to-decl bindings. */
7740 while (b)
7742 tree decl = b->decl;
7743 tree type = TREE_TYPE (decl);
7744 c_arg_tag tag;
7745 const char *keyword;
7747 switch (TREE_CODE (decl))
7749 case PARM_DECL:
7750 if (b->id)
7752 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7753 I_SYMBOL_BINDING (b->id) = b->shadowed;
7756 /* Check for forward decls that never got their actual decl. */
7757 if (TREE_ASM_WRITTEN (decl))
7758 error_at (b->locus,
7759 "parameter %q+D has just a forward declaration", decl);
7760 /* Check for (..., void, ...) and issue an error. */
7761 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7763 if (!gave_void_only_once_err)
7765 error_at (b->locus, "%<void%> must be the only parameter");
7766 gave_void_only_once_err = true;
7769 else
7771 /* Valid parameter, add it to the list. */
7772 DECL_CHAIN (decl) = parms;
7773 parms = decl;
7775 /* Since there is a prototype, args are passed in their
7776 declared types. The back end may override this later. */
7777 DECL_ARG_TYPE (decl) = type;
7778 types = tree_cons (0, type, types);
7780 break;
7782 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7783 case UNION_TYPE: keyword = "union"; goto tag;
7784 case RECORD_TYPE: keyword = "struct"; goto tag;
7785 tag:
7786 /* Types may not have tag-names, in which case the type
7787 appears in the bindings list with b->id NULL. */
7788 if (b->id)
7790 gcc_assert (I_TAG_BINDING (b->id) == b);
7791 I_TAG_BINDING (b->id) = b->shadowed;
7794 /* Warn about any struct, union or enum tags defined in a
7795 parameter list. The scope of such types is limited to
7796 the parameter list, which is rarely if ever desirable
7797 (it's impossible to call such a function with type-
7798 correct arguments). An anonymous union parm type is
7799 meaningful as a GNU extension, so don't warn for that. */
7800 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7802 if (b->id)
7803 /* The %s will be one of 'struct', 'union', or 'enum'. */
7804 warning_at (b->locus, 0,
7805 "%<%s %E%> declared inside parameter list"
7806 " will not be visible outside of this definition or"
7807 " declaration", keyword, b->id);
7808 else
7809 /* The %s will be one of 'struct', 'union', or 'enum'. */
7810 warning_at (b->locus, 0,
7811 "anonymous %s declared inside parameter list"
7812 " will not be visible outside of this definition or"
7813 " declaration", keyword);
7816 tag.id = b->id;
7817 tag.type = decl;
7818 vec_safe_push (tags, tag);
7819 break;
7821 case FUNCTION_DECL:
7822 /* FUNCTION_DECLs appear when there is an implicit function
7823 declaration in the parameter list. */
7824 gcc_assert (b->nested || seen_error ());
7825 goto set_shadowed;
7827 case CONST_DECL:
7828 case TYPE_DECL:
7829 /* CONST_DECLs appear here when we have an embedded enum,
7830 and TYPE_DECLs appear here when we have an embedded struct
7831 or union. No warnings for this - we already warned about the
7832 type itself. */
7834 /* When we reinsert this decl in the function body, we need
7835 to reconstruct whether it was marked as nested. */
7836 gcc_assert (!b->nested);
7837 DECL_CHAIN (decl) = others;
7838 others = decl;
7839 /* fall through */
7841 case ERROR_MARK:
7842 set_shadowed:
7843 /* error_mark_node appears here when we have an undeclared
7844 variable. Just throw it away. */
7845 if (b->id)
7847 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7848 I_SYMBOL_BINDING (b->id) = b->shadowed;
7850 break;
7852 /* Other things that might be encountered. */
7853 case LABEL_DECL:
7854 case VAR_DECL:
7855 default:
7856 gcc_unreachable ();
7859 b = free_binding_and_advance (b);
7862 arg_info->parms = parms;
7863 arg_info->tags = tags;
7864 arg_info->types = types;
7865 arg_info->others = others;
7866 arg_info->pending_sizes = expr;
7867 return arg_info;
7870 /* Get the struct, enum or union (CODE says which) with tag NAME.
7871 Define the tag as a forward-reference with location LOC if it is
7872 not defined. HAVE_STD_ATTRS says whether any standard attributes
7873 were present after the struct, union or enum keyword; ATTRS are the
7874 standard attributes present there. Return a c_typespec structure
7875 for the type specifier. */
7877 struct c_typespec
7878 parser_xref_tag (location_t loc, enum tree_code code, tree name,
7879 bool have_std_attrs, tree attrs)
7881 struct c_typespec ret;
7882 tree ref;
7883 location_t refloc;
7885 ret.expr = NULL_TREE;
7886 ret.expr_const_operands = true;
7888 /* If a cross reference is requested, look up the type
7889 already defined for this tag and return it. */
7891 ref = lookup_tag (code, name, false, &refloc);
7892 /* If this is the right type of tag, return what we found.
7893 (This reference will be shadowed by shadow_tag later if appropriate.)
7894 If this is the wrong type of tag, do not return it. If it was the
7895 wrong type in the same scope, we will have had an error
7896 message already; if in a different scope and declaring
7897 a name, pending_xref_error will give an error message; but if in a
7898 different scope and not declaring a name, this tag should
7899 shadow the previous declaration of a different type of tag, and
7900 this would not work properly if we return the reference found.
7901 (For example, with "struct foo" in an outer scope, "union foo;"
7902 must shadow that tag with a new one of union type.) */
7903 ret.kind = (ref
7904 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
7905 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
7906 if (ref && TREE_CODE (ref) == code)
7908 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
7909 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7910 && loc != UNKNOWN_LOCATION
7911 && warn_cxx_compat)
7913 switch (code)
7915 case ENUMERAL_TYPE:
7916 warning_at (loc, OPT_Wc___compat,
7917 ("enum type defined in struct or union "
7918 "is not visible in C++"));
7919 inform (refloc, "enum type defined here");
7920 break;
7921 case RECORD_TYPE:
7922 warning_at (loc, OPT_Wc___compat,
7923 ("struct defined in struct or union "
7924 "is not visible in C++"));
7925 inform (refloc, "struct defined here");
7926 break;
7927 case UNION_TYPE:
7928 warning_at (loc, OPT_Wc___compat,
7929 ("union defined in struct or union "
7930 "is not visible in C++"));
7931 inform (refloc, "union defined here");
7932 break;
7933 default:
7934 gcc_unreachable();
7938 ret.spec = ref;
7939 return ret;
7942 /* If no such tag is yet defined, create a forward-reference node
7943 and record it as the "definition".
7944 When a real declaration of this type is found,
7945 the forward-reference will be altered into a real type. */
7947 ref = make_node (code);
7948 if (code == ENUMERAL_TYPE)
7950 /* Give the type a default layout like unsigned int
7951 to avoid crashing if it does not get defined. */
7952 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7953 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7954 TYPE_USER_ALIGN (ref) = 0;
7955 TYPE_UNSIGNED (ref) = 1;
7956 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7957 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7958 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7961 pushtag (loc, name, ref);
7962 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
7964 ret.spec = ref;
7965 return ret;
7968 /* Get the struct, enum or union (CODE says which) with tag NAME.
7969 Define the tag as a forward-reference if it is not defined.
7970 Return a tree for the type. */
7972 tree
7973 xref_tag (enum tree_code code, tree name)
7975 return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
7978 /* Make sure that the tag NAME is defined *in the current scope*
7979 at least as a forward reference.
7980 LOC is the location of the struct's definition.
7981 CODE says which kind of tag NAME ought to be.
7983 This stores the current value of the file static STRUCT_PARSE_INFO
7984 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7985 new c_struct_parse_info structure. The old value of
7986 STRUCT_PARSE_INFO is restored in finish_struct. */
7988 tree
7989 start_struct (location_t loc, enum tree_code code, tree name,
7990 class c_struct_parse_info **enclosing_struct_parse_info)
7992 /* If there is already a tag defined at this scope
7993 (as a forward reference), just return it. */
7995 tree ref = NULL_TREE;
7996 location_t refloc = UNKNOWN_LOCATION;
7998 if (name != NULL_TREE)
7999 ref = lookup_tag (code, name, true, &refloc);
8000 if (ref && TREE_CODE (ref) == code)
8002 if (TYPE_STUB_DECL (ref))
8003 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8005 if (TYPE_SIZE (ref))
8007 if (code == UNION_TYPE)
8008 error_at (loc, "redefinition of %<union %E%>", name);
8009 else
8010 error_at (loc, "redefinition of %<struct %E%>", name);
8011 if (refloc != UNKNOWN_LOCATION)
8012 inform (refloc, "originally defined here");
8013 /* Don't create structures using a name already in use. */
8014 ref = NULL_TREE;
8016 else if (C_TYPE_BEING_DEFINED (ref))
8018 if (code == UNION_TYPE)
8019 error_at (loc, "nested redefinition of %<union %E%>", name);
8020 else
8021 error_at (loc, "nested redefinition of %<struct %E%>", name);
8022 /* Don't bother to report "originally defined here" for a
8023 nested redefinition; the original definition should be
8024 obvious. */
8025 /* Don't create structures that contain themselves. */
8026 ref = NULL_TREE;
8030 /* Otherwise create a forward-reference just so the tag is in scope. */
8032 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8034 ref = make_node (code);
8035 pushtag (loc, name, ref);
8038 C_TYPE_BEING_DEFINED (ref) = 1;
8039 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8040 TYPE_PACKED (v) = flag_pack_struct;
8042 *enclosing_struct_parse_info = struct_parse_info;
8043 struct_parse_info = new c_struct_parse_info ();
8045 /* FIXME: This will issue a warning for a use of a type defined
8046 within a statement expr used within sizeof, et. al. This is not
8047 terribly serious as C++ doesn't permit statement exprs within
8048 sizeof anyhow. */
8049 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8050 warning_at (loc, OPT_Wc___compat,
8051 "defining type in %qs expression is invalid in C++",
8052 (in_sizeof
8053 ? "sizeof"
8054 : (in_typeof ? "typeof" : "alignof")));
8056 return ref;
8059 /* Process the specs, declarator and width (NULL if omitted)
8060 of a structure component, returning a FIELD_DECL node.
8061 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8062 DECL_ATTRS is as for grokdeclarator.
8064 LOC is the location of the structure component.
8066 This is done during the parsing of the struct declaration.
8067 The FIELD_DECL nodes are chained together and the lot of them
8068 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8070 tree
8071 grokfield (location_t loc,
8072 struct c_declarator *declarator, struct c_declspecs *declspecs,
8073 tree width, tree *decl_attrs)
8075 tree value;
8077 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8078 && width == NULL_TREE)
8080 /* This is an unnamed decl.
8082 If we have something of the form "union { list } ;" then this
8083 is the anonymous union extension. Similarly for struct.
8085 If this is something of the form "struct foo;", then
8086 If MS or Plan 9 extensions are enabled, this is handled as
8087 an anonymous struct.
8088 Otherwise this is a forward declaration of a structure tag.
8090 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8091 If foo names a structure or union without a tag, then this
8092 is an anonymous struct (this is permitted by C11).
8093 If MS or Plan 9 extensions are enabled and foo names a
8094 structure, then again this is an anonymous struct.
8095 Otherwise this is an error.
8097 Oh what a horrid tangled web we weave. I wonder if MS consciously
8098 took this from Plan 9 or if it was an accident of implementation
8099 that took root before someone noticed the bug... */
8101 tree type = declspecs->type;
8102 bool ok = false;
8104 if (RECORD_OR_UNION_TYPE_P (type)
8105 && (flag_ms_extensions
8106 || flag_plan9_extensions
8107 || !declspecs->typedef_p))
8109 if (flag_ms_extensions || flag_plan9_extensions)
8110 ok = true;
8111 else if (TYPE_NAME (type) == NULL)
8112 ok = true;
8113 else
8114 ok = false;
8116 if (!ok)
8118 pedwarn (loc, 0, "declaration does not declare anything");
8119 return NULL_TREE;
8121 if (flag_isoc99)
8122 pedwarn_c99 (loc, OPT_Wpedantic,
8123 "ISO C99 doesn%'t support unnamed structs/unions");
8124 else
8125 pedwarn_c99 (loc, OPT_Wpedantic,
8126 "ISO C90 doesn%'t support unnamed structs/unions");
8129 value = grokdeclarator (declarator, declspecs, FIELD, false,
8130 width ? &width : NULL, decl_attrs, NULL, NULL,
8131 DEPRECATED_NORMAL);
8133 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8134 DECL_INITIAL (value) = width;
8135 if (width)
8136 SET_DECL_C_BIT_FIELD (value);
8138 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8140 /* If we currently have a binding for this field, set the
8141 in_struct field in the binding, so that we warn about lookups
8142 which find it. */
8143 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8144 if (b != NULL)
8146 /* If the in_struct field is not yet set, push it on a list
8147 to be cleared when this struct is finished. */
8148 if (!b->in_struct)
8150 struct_parse_info->fields.safe_push (b);
8151 b->in_struct = 1;
8156 return value;
8159 /* Subroutine of detect_field_duplicates: return whether X and Y,
8160 which are both fields in the same struct, have duplicate field
8161 names. */
8163 static bool
8164 is_duplicate_field (tree x, tree y)
8166 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8167 return true;
8169 /* When using -fplan9-extensions, an anonymous field whose name is a
8170 typedef can duplicate a field name. */
8171 if (flag_plan9_extensions
8172 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8174 tree xt, xn, yt, yn;
8176 xt = TREE_TYPE (x);
8177 if (DECL_NAME (x) != NULL_TREE)
8178 xn = DECL_NAME (x);
8179 else if (RECORD_OR_UNION_TYPE_P (xt)
8180 && TYPE_NAME (xt) != NULL_TREE
8181 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8182 xn = DECL_NAME (TYPE_NAME (xt));
8183 else
8184 xn = NULL_TREE;
8186 yt = TREE_TYPE (y);
8187 if (DECL_NAME (y) != NULL_TREE)
8188 yn = DECL_NAME (y);
8189 else if (RECORD_OR_UNION_TYPE_P (yt)
8190 && TYPE_NAME (yt) != NULL_TREE
8191 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8192 yn = DECL_NAME (TYPE_NAME (yt));
8193 else
8194 yn = NULL_TREE;
8196 if (xn != NULL_TREE && xn == yn)
8197 return true;
8200 return false;
8203 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8204 to HTAB, giving errors for any duplicates. */
8206 static void
8207 detect_field_duplicates_hash (tree fieldlist,
8208 hash_table<nofree_ptr_hash <tree_node> > *htab)
8210 tree x, y;
8211 tree_node **slot;
8213 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8214 if ((y = DECL_NAME (x)) != NULL_TREE)
8216 slot = htab->find_slot (y, INSERT);
8217 if (*slot)
8219 error ("duplicate member %q+D", x);
8220 DECL_NAME (x) = NULL_TREE;
8222 *slot = y;
8224 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8226 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8228 /* When using -fplan9-extensions, an anonymous field whose
8229 name is a typedef can duplicate a field name. */
8230 if (flag_plan9_extensions
8231 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8232 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8234 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8235 slot = htab->find_slot (xn, INSERT);
8236 if (*slot)
8237 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8238 *slot = xn;
8243 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8244 the list such that this does not present a problem later. */
8246 static void
8247 detect_field_duplicates (tree fieldlist)
8249 tree x, y;
8250 int timeout = 10;
8252 /* If the struct is the list of instance variables of an Objective-C
8253 class, then we need to check all the instance variables of
8254 superclasses when checking for duplicates (since you can't have
8255 an instance variable in a subclass with the same name as an
8256 instance variable in a superclass). We pass on this job to the
8257 Objective-C compiler. objc_detect_field_duplicates() will return
8258 false if we are not checking the list of instance variables and
8259 the C frontend should proceed with the standard field duplicate
8260 checks. If we are checking the list of instance variables, the
8261 ObjC frontend will do the check, emit the errors if needed, and
8262 then return true. */
8263 if (c_dialect_objc ())
8264 if (objc_detect_field_duplicates (false))
8265 return;
8267 /* First, see if there are more than "a few" fields.
8268 This is trivially true if there are zero or one fields. */
8269 if (!fieldlist || !DECL_CHAIN (fieldlist))
8270 return;
8271 x = fieldlist;
8272 do {
8273 timeout--;
8274 if (DECL_NAME (x) == NULL_TREE
8275 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8276 timeout = 0;
8277 x = DECL_CHAIN (x);
8278 } while (timeout > 0 && x);
8280 /* If there were "few" fields and no anonymous structures or unions,
8281 avoid the overhead of allocating a hash table. Instead just do
8282 the nested traversal thing. */
8283 if (timeout > 0)
8285 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8286 /* When using -fplan9-extensions, we can have duplicates
8287 between typedef names and fields. */
8288 if (DECL_NAME (x)
8289 || (flag_plan9_extensions
8290 && DECL_NAME (x) == NULL_TREE
8291 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8292 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8293 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8295 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8296 if (is_duplicate_field (y, x))
8298 error ("duplicate member %q+D", x);
8299 DECL_NAME (x) = NULL_TREE;
8303 else
8305 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8306 detect_field_duplicates_hash (fieldlist, &htab);
8310 /* Finish up struct info used by -Wc++-compat. */
8312 static void
8313 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8314 location_t record_loc)
8316 unsigned int ix;
8317 tree x;
8318 struct c_binding *b;
8320 if (fieldlist == NULL_TREE)
8322 if (code == RECORD_TYPE)
8323 warning_at (record_loc, OPT_Wc___compat,
8324 "empty struct has size 0 in C, size 1 in C++");
8325 else
8326 warning_at (record_loc, OPT_Wc___compat,
8327 "empty union has size 0 in C, size 1 in C++");
8330 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8331 the current struct. We do this now at the end of the struct
8332 because the flag is used to issue visibility warnings, and we
8333 only want to issue those warnings if the type is referenced
8334 outside of the struct declaration. */
8335 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8336 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8338 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8339 typedefs used when declaring fields in this struct. If the name
8340 of any of the fields is also a typedef name then the struct would
8341 not parse in C++, because the C++ lookup rules say that the
8342 typedef name would be looked up in the context of the struct, and
8343 would thus be the field rather than the typedef. */
8344 if (!struct_parse_info->typedefs_seen.is_empty ()
8345 && fieldlist != NULL_TREE)
8347 /* Use a hash_set<tree> using the name of the typedef. We can use
8348 a hash_set<tree> because identifiers are interned. */
8349 hash_set<tree> tset;
8351 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8352 tset.add (DECL_NAME (x));
8354 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8356 if (DECL_NAME (x) != NULL_TREE
8357 && tset.contains (DECL_NAME (x)))
8359 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8360 ("using %qD as both field and typedef name is "
8361 "invalid in C++"),
8363 /* FIXME: It would be nice to report the location where
8364 the typedef name is used. */
8369 /* For each field which has a binding and which was not defined in
8370 an enclosing struct, clear the in_struct field. */
8371 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8372 b->in_struct = 0;
8375 /* Function to help qsort sort FIELD_DECLs by name order. */
8377 static int
8378 field_decl_cmp (const void *x_p, const void *y_p)
8380 const tree *const x = (const tree *) x_p;
8381 const tree *const y = (const tree *) y_p;
8383 if (DECL_NAME (*x) == DECL_NAME (*y))
8384 /* A nontype is "greater" than a type. */
8385 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8386 if (DECL_NAME (*x) == NULL_TREE)
8387 return -1;
8388 if (DECL_NAME (*y) == NULL_TREE)
8389 return 1;
8390 if (DECL_NAME (*x) < DECL_NAME (*y))
8391 return -1;
8392 return 1;
8395 /* If this structure or union completes the type of any previous
8396 variable declaration, lay it out and output its rtl. */
8397 static void
8398 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
8400 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
8402 tree decl = TREE_VALUE (x);
8403 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8404 layout_array_type (TREE_TYPE (decl));
8405 if (TREE_CODE (decl) != TYPE_DECL)
8407 relayout_decl (decl);
8408 if (c_dialect_objc ())
8409 objc_check_decl (decl);
8410 rest_of_decl_compilation (decl, toplevel, 0);
8415 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8416 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8417 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8418 ATTRIBUTES are attributes to be applied to the structure.
8420 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8421 the struct was started. */
8423 tree
8424 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8425 class c_struct_parse_info *enclosing_struct_parse_info)
8427 tree x;
8428 bool toplevel = file_scope == current_scope;
8430 /* If this type was previously laid out as a forward reference,
8431 make sure we lay it out again. */
8433 TYPE_SIZE (t) = NULL_TREE;
8435 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8437 if (pedantic)
8439 for (x = fieldlist; x; x = DECL_CHAIN (x))
8441 if (DECL_NAME (x) != NULL_TREE)
8442 break;
8443 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8444 break;
8447 if (x == NULL_TREE)
8449 if (TREE_CODE (t) == UNION_TYPE)
8451 if (fieldlist)
8452 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8453 else
8454 pedwarn (loc, OPT_Wpedantic, "union has no members");
8456 else
8458 if (fieldlist)
8459 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8460 else
8461 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8466 /* Install struct as DECL_CONTEXT of each field decl.
8467 Also process specified field sizes, found in the DECL_INITIAL,
8468 storing 0 there after the type has been changed to precision equal
8469 to its width, rather than the precision of the specified standard
8470 type. (Correct layout requires the original type to have been preserved
8471 until now.) */
8473 bool saw_named_field = false;
8474 for (x = fieldlist; x; x = DECL_CHAIN (x))
8476 if (TREE_TYPE (x) == error_mark_node)
8477 continue;
8479 DECL_CONTEXT (x) = t;
8481 /* If any field is const, the structure type is pseudo-const. */
8482 if (TREE_READONLY (x))
8483 C_TYPE_FIELDS_READONLY (t) = 1;
8484 else
8486 /* A field that is pseudo-const makes the structure likewise. */
8487 tree t1 = strip_array_types (TREE_TYPE (x));
8488 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8489 C_TYPE_FIELDS_READONLY (t) = 1;
8492 /* Any field that is volatile means variables of this type must be
8493 treated in some ways as volatile. */
8494 if (TREE_THIS_VOLATILE (x))
8495 C_TYPE_FIELDS_VOLATILE (t) = 1;
8497 /* Any field of nominal variable size implies structure is too. */
8498 if (C_DECL_VARIABLE_SIZE (x))
8499 C_TYPE_VARIABLE_SIZE (t) = 1;
8501 if (DECL_C_BIT_FIELD (x))
8503 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8504 DECL_SIZE (x) = bitsize_int (width);
8505 DECL_BIT_FIELD (x) = 1;
8508 if (TYPE_PACKED (t)
8509 && (DECL_BIT_FIELD (x)
8510 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8511 DECL_PACKED (x) = 1;
8513 /* Detect flexible array member in an invalid context. */
8514 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8515 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8516 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8517 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8519 if (TREE_CODE (t) == UNION_TYPE)
8521 error_at (DECL_SOURCE_LOCATION (x),
8522 "flexible array member in union");
8523 TREE_TYPE (x) = error_mark_node;
8525 else if (DECL_CHAIN (x) != NULL_TREE)
8527 error_at (DECL_SOURCE_LOCATION (x),
8528 "flexible array member not at end of struct");
8529 TREE_TYPE (x) = error_mark_node;
8531 else if (!saw_named_field)
8533 error_at (DECL_SOURCE_LOCATION (x),
8534 "flexible array member in a struct with no named "
8535 "members");
8536 TREE_TYPE (x) = error_mark_node;
8540 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8541 && flexible_array_type_p (TREE_TYPE (x)))
8542 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8543 "invalid use of structure with flexible array member");
8545 if (DECL_NAME (x)
8546 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8547 saw_named_field = true;
8550 detect_field_duplicates (fieldlist);
8552 /* Now we have the nearly final fieldlist. Record it,
8553 then lay out the structure or union (including the fields). */
8555 TYPE_FIELDS (t) = fieldlist;
8557 maybe_apply_pragma_scalar_storage_order (t);
8559 layout_type (t);
8561 if (TYPE_SIZE_UNIT (t)
8562 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8563 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8564 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8565 error ("type %qT is too large", t);
8567 /* Give bit-fields their proper types and rewrite the type of array fields
8568 with scalar component if the enclosing type has reverse storage order. */
8569 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8571 if (TREE_CODE (field) == FIELD_DECL
8572 && DECL_INITIAL (field)
8573 && TREE_TYPE (field) != error_mark_node)
8575 unsigned HOST_WIDE_INT width
8576 = tree_to_uhwi (DECL_INITIAL (field));
8577 tree type = TREE_TYPE (field);
8578 if (width != TYPE_PRECISION (type))
8580 TREE_TYPE (field)
8581 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8582 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8584 DECL_INITIAL (field) = NULL_TREE;
8586 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8587 && TREE_CODE (field) == FIELD_DECL
8588 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8590 tree ftype = TREE_TYPE (field);
8591 tree ctype = strip_array_types (ftype);
8592 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8594 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8595 tree *typep = &fmain_type;
8596 do {
8597 *typep = build_distinct_type_copy (*typep);
8598 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8599 typep = &TREE_TYPE (*typep);
8600 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8601 TREE_TYPE (field)
8602 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8607 /* Now we have the truly final field list.
8608 Store it in this type and in the variants. */
8610 TYPE_FIELDS (t) = fieldlist;
8612 /* If there are lots of fields, sort so we can look through them fast.
8613 We arbitrarily consider 16 or more elts to be "a lot". */
8616 int len = 0;
8618 for (x = fieldlist; x; x = DECL_CHAIN (x))
8620 if (len > 15 || DECL_NAME (x) == NULL)
8621 break;
8622 len += 1;
8625 if (len > 15)
8627 tree *field_array;
8628 struct lang_type *space;
8629 struct sorted_fields_type *space2;
8631 len += list_length (x);
8633 /* Use the same allocation policy here that make_node uses, to
8634 ensure that this lives as long as the rest of the struct decl.
8635 All decls in an inline function need to be saved. */
8637 space = ggc_cleared_alloc<struct lang_type> ();
8638 space2 = (sorted_fields_type *) ggc_internal_alloc
8639 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8641 len = 0;
8642 space->s = space2;
8643 field_array = &space2->elts[0];
8644 for (x = fieldlist; x; x = DECL_CHAIN (x))
8646 field_array[len++] = x;
8648 /* If there is anonymous struct or union, break out of the loop. */
8649 if (DECL_NAME (x) == NULL)
8650 break;
8652 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8653 if (x == NULL)
8655 TYPE_LANG_SPECIFIC (t) = space;
8656 TYPE_LANG_SPECIFIC (t)->s->len = len;
8657 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8658 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8663 /* If this was supposed to be a transparent union, but we can't
8664 make it one, warn and turn off the flag. */
8665 if (TREE_CODE (t) == UNION_TYPE
8666 && TYPE_TRANSPARENT_AGGR (t)
8667 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8669 TYPE_TRANSPARENT_AGGR (t) = 0;
8670 warning_at (loc, 0, "union cannot be made transparent");
8673 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8674 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8676 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8677 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8678 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
8679 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8680 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8681 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8682 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8685 /* Update type location to the one of the definition, instead of e.g.
8686 a forward declaration. */
8687 if (TYPE_STUB_DECL (t))
8688 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8690 /* Finish debugging output for this type. */
8691 rest_of_type_compilation (t, toplevel);
8693 finish_incomplete_vars (incomplete_vars, toplevel);
8695 /* If we're inside a function proper, i.e. not file-scope and not still
8696 parsing parameters, then arrange for the size of a variable sized type
8697 to be bound now. */
8698 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8699 add_stmt (build_stmt (loc,
8700 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8702 if (warn_cxx_compat)
8703 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8705 delete struct_parse_info;
8707 struct_parse_info = enclosing_struct_parse_info;
8709 /* If this struct is defined inside a struct, add it to
8710 struct_types. */
8711 if (warn_cxx_compat
8712 && struct_parse_info != NULL
8713 && !in_sizeof && !in_typeof && !in_alignof)
8714 struct_parse_info->struct_types.safe_push (t);
8716 return t;
8719 static struct {
8720 gt_pointer_operator new_value;
8721 void *cookie;
8722 } resort_data;
8724 /* This routine compares two fields like field_decl_cmp but using the
8725 pointer operator in resort_data. */
8727 static int
8728 resort_field_decl_cmp (const void *x_p, const void *y_p)
8730 const tree *const x = (const tree *) x_p;
8731 const tree *const y = (const tree *) y_p;
8733 if (DECL_NAME (*x) == DECL_NAME (*y))
8734 /* A nontype is "greater" than a type. */
8735 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8736 if (DECL_NAME (*x) == NULL_TREE)
8737 return -1;
8738 if (DECL_NAME (*y) == NULL_TREE)
8739 return 1;
8741 tree d1 = DECL_NAME (*x);
8742 tree d2 = DECL_NAME (*y);
8743 resort_data.new_value (&d1, resort_data.cookie);
8744 resort_data.new_value (&d2, resort_data.cookie);
8745 if (d1 < d2)
8746 return -1;
8748 return 1;
8751 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8753 void
8754 resort_sorted_fields (void *obj,
8755 void * ARG_UNUSED (orig_obj),
8756 gt_pointer_operator new_value,
8757 void *cookie)
8759 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8760 resort_data.new_value = new_value;
8761 resort_data.cookie = cookie;
8762 qsort (&sf->elts[0], sf->len, sizeof (tree),
8763 resort_field_decl_cmp);
8766 /* Lay out the type T, and its element type, and so on. */
8768 static void
8769 layout_array_type (tree t)
8771 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8772 layout_array_type (TREE_TYPE (t));
8773 layout_type (t);
8776 /* Begin compiling the definition of an enumeration type.
8777 NAME is its name (or null if anonymous).
8778 LOC is the enum's location.
8779 Returns the type object, as yet incomplete.
8780 Also records info about it so that build_enumerator
8781 may be used to declare the individual values as they are read. */
8783 tree
8784 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8786 tree enumtype = NULL_TREE;
8787 location_t enumloc = UNKNOWN_LOCATION;
8789 /* If this is the real definition for a previous forward reference,
8790 fill in the contents in the same object that used to be the
8791 forward reference. */
8793 if (name != NULL_TREE)
8794 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8796 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8798 enumtype = make_node (ENUMERAL_TYPE);
8799 pushtag (loc, name, enumtype);
8801 /* Update type location to the one of the definition, instead of e.g.
8802 a forward declaration. */
8803 else if (TYPE_STUB_DECL (enumtype))
8805 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8806 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8809 if (C_TYPE_BEING_DEFINED (enumtype))
8810 error_at (loc, "nested redefinition of %<enum %E%>", name);
8812 C_TYPE_BEING_DEFINED (enumtype) = 1;
8814 if (TYPE_VALUES (enumtype) != NULL_TREE)
8816 /* This enum is a named one that has been declared already. */
8817 error_at (loc, "redeclaration of %<enum %E%>", name);
8818 if (enumloc != UNKNOWN_LOCATION)
8819 inform (enumloc, "originally defined here");
8821 /* Completely replace its old definition.
8822 The old enumerators remain defined, however. */
8823 TYPE_VALUES (enumtype) = NULL_TREE;
8826 the_enum->enum_next_value = integer_zero_node;
8827 the_enum->enum_overflow = 0;
8829 if (flag_short_enums)
8830 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8831 TYPE_PACKED (v) = 1;
8833 /* FIXME: This will issue a warning for a use of a type defined
8834 within sizeof in a statement expr. This is not terribly serious
8835 as C++ doesn't permit statement exprs within sizeof anyhow. */
8836 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8837 warning_at (loc, OPT_Wc___compat,
8838 "defining type in %qs expression is invalid in C++",
8839 (in_sizeof
8840 ? "sizeof"
8841 : (in_typeof ? "typeof" : "alignof")));
8843 return enumtype;
8846 /* After processing and defining all the values of an enumeration type,
8847 install their decls in the enumeration type and finish it off.
8848 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8849 and ATTRIBUTES are the specified attributes.
8850 Returns ENUMTYPE. */
8852 tree
8853 finish_enum (tree enumtype, tree values, tree attributes)
8855 tree pair, tem;
8856 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8857 int precision;
8858 signop sign;
8859 bool toplevel = (file_scope == current_scope);
8860 struct lang_type *lt;
8862 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8864 /* Calculate the maximum value of any enumerator in this type. */
8866 if (values == error_mark_node)
8867 minnode = maxnode = integer_zero_node;
8868 else
8870 minnode = maxnode = TREE_VALUE (values);
8871 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8873 tree value = TREE_VALUE (pair);
8874 if (tree_int_cst_lt (maxnode, value))
8875 maxnode = value;
8876 if (tree_int_cst_lt (value, minnode))
8877 minnode = value;
8881 /* Construct the final type of this enumeration. It is the same
8882 as one of the integral types - the narrowest one that fits, except
8883 that normally we only go as narrow as int - and signed iff any of
8884 the values are negative. */
8885 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8886 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8887 tree_int_cst_min_precision (maxnode, sign));
8889 /* If the precision of the type was specified with an attribute and it
8890 was too small, give an error. Otherwise, use it. */
8891 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8893 if (precision > TYPE_PRECISION (enumtype))
8895 TYPE_PRECISION (enumtype) = 0;
8896 error ("specified mode too small for enumerated values");
8898 else
8899 precision = TYPE_PRECISION (enumtype);
8901 else
8902 TYPE_PRECISION (enumtype) = 0;
8904 if (TYPE_PACKED (enumtype)
8905 || precision > TYPE_PRECISION (integer_type_node)
8906 || TYPE_PRECISION (enumtype))
8908 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8909 if (tem == NULL)
8911 warning (0, "enumeration values exceed range of largest integer");
8912 tem = long_long_integer_type_node;
8915 else
8916 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8918 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8919 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8920 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8921 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8922 TYPE_SIZE (enumtype) = NULL_TREE;
8923 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8925 layout_type (enumtype);
8927 if (values != error_mark_node)
8929 /* Change the type of the enumerators to be the enum type. We
8930 need to do this irrespective of the size of the enum, for
8931 proper type checking. Replace the DECL_INITIALs of the
8932 enumerators, and the value slots of the list, with copies
8933 that have the enum type; they cannot be modified in place
8934 because they may be shared (e.g. integer_zero_node) Finally,
8935 change the purpose slots to point to the names of the decls. */
8936 for (pair = values; pair; pair = TREE_CHAIN (pair))
8938 tree enu = TREE_PURPOSE (pair);
8939 tree ini = DECL_INITIAL (enu);
8941 TREE_TYPE (enu) = enumtype;
8943 /* The ISO C Standard mandates enumerators to have type int,
8944 even though the underlying type of an enum type is
8945 unspecified. However, GCC allows enumerators of any
8946 integer type as an extensions. build_enumerator()
8947 converts any enumerators that fit in an int to type int,
8948 to avoid promotions to unsigned types when comparing
8949 integers with enumerators that fit in the int range.
8950 When -pedantic is given, build_enumerator() would have
8951 already warned about those that don't fit. Here we
8952 convert the rest to the enumerator type. */
8953 if (TREE_TYPE (ini) != integer_type_node)
8954 ini = convert (enumtype, ini);
8956 DECL_INITIAL (enu) = ini;
8957 TREE_PURPOSE (pair) = DECL_NAME (enu);
8958 TREE_VALUE (pair) = ini;
8961 TYPE_VALUES (enumtype) = values;
8964 /* Record the min/max values so that we can warn about bit-field
8965 enumerations that are too small for the values. */
8966 lt = ggc_cleared_alloc<struct lang_type> ();
8967 lt->enum_min = minnode;
8968 lt->enum_max = maxnode;
8969 TYPE_LANG_SPECIFIC (enumtype) = lt;
8971 /* Fix up all variant types of this enum type. */
8972 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
8973 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8975 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
8976 if (tem == enumtype)
8977 continue;
8978 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8979 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8980 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8981 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8982 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8983 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8984 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8985 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8986 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8987 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8988 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8991 /* Finish debugging output for this type. */
8992 rest_of_type_compilation (enumtype, toplevel);
8994 finish_incomplete_vars (incomplete_vars, toplevel);
8996 /* If this enum is defined inside a struct, add it to
8997 struct_types. */
8998 if (warn_cxx_compat
8999 && struct_parse_info != NULL
9000 && !in_sizeof && !in_typeof && !in_alignof)
9001 struct_parse_info->struct_types.safe_push (enumtype);
9003 C_TYPE_BEING_DEFINED (enumtype) = 0;
9005 return enumtype;
9008 /* Build and install a CONST_DECL for one value of the
9009 current enumeration type (one that was begun with start_enum).
9010 DECL_LOC is the location of the enumerator.
9011 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9012 Return a tree-list containing the CONST_DECL and its value.
9013 Assignment of sequential values by default is handled here. */
9015 tree
9016 build_enumerator (location_t decl_loc, location_t loc,
9017 struct c_enum_contents *the_enum, tree name, tree value)
9019 tree decl, type;
9021 /* Validate and default VALUE. */
9023 if (value != NULL_TREE)
9025 /* Don't issue more errors for error_mark_node (i.e. an
9026 undeclared identifier) - just ignore the value expression. */
9027 if (value == error_mark_node)
9028 value = NULL_TREE;
9029 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9031 error_at (loc, "enumerator value for %qE is not an integer constant",
9032 name);
9033 value = NULL_TREE;
9035 else
9037 if (TREE_CODE (value) != INTEGER_CST)
9039 value = c_fully_fold (value, false, NULL);
9040 if (TREE_CODE (value) == INTEGER_CST)
9041 pedwarn (loc, OPT_Wpedantic,
9042 "enumerator value for %qE is not an integer "
9043 "constant expression", name);
9045 if (TREE_CODE (value) != INTEGER_CST)
9047 error ("enumerator value for %qE is not an integer constant",
9048 name);
9049 value = NULL_TREE;
9051 else
9053 value = default_conversion (value);
9054 constant_expression_warning (value);
9059 /* Default based on previous value. */
9060 /* It should no longer be possible to have NON_LVALUE_EXPR
9061 in the default. */
9062 if (value == NULL_TREE)
9064 value = the_enum->enum_next_value;
9065 if (the_enum->enum_overflow)
9066 error_at (loc, "overflow in enumeration values");
9068 /* Even though the underlying type of an enum is unspecified, the
9069 type of enumeration constants is explicitly defined as int
9070 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
9071 an extension. */
9072 else if (!int_fits_type_p (value, integer_type_node))
9073 pedwarn (loc, OPT_Wpedantic,
9074 "ISO C restricts enumerator values to range of %<int%>");
9076 /* The ISO C Standard mandates enumerators to have type int, even
9077 though the underlying type of an enum type is unspecified.
9078 However, GCC allows enumerators of any integer type as an
9079 extensions. Here we convert any enumerators that fit in an int
9080 to type int, to avoid promotions to unsigned types when comparing
9081 integers with enumerators that fit in the int range. When
9082 -pedantic is given, we would have already warned about those that
9083 don't fit. We have to do this here rather than in finish_enum
9084 because this value may be used to define more enumerators. */
9085 if (int_fits_type_p (value, integer_type_node))
9086 value = convert (integer_type_node, value);
9088 /* Set basis for default for next value. */
9089 the_enum->enum_next_value
9090 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9091 PLUS_EXPR, value, integer_one_node, false);
9092 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9094 /* Now create a declaration for the enum value name. */
9096 type = TREE_TYPE (value);
9097 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
9098 TYPE_PRECISION (integer_type_node)),
9099 (TYPE_PRECISION (type)
9100 >= TYPE_PRECISION (integer_type_node)
9101 && TYPE_UNSIGNED (type)));
9103 decl = build_decl (decl_loc, CONST_DECL, name, type);
9104 DECL_INITIAL (decl) = convert (type, value);
9105 pushdecl (decl);
9107 return tree_cons (decl, value, NULL_TREE);
9110 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9112 tree
9113 c_simulate_enum_decl (location_t loc, const char *name,
9114 vec<string_int_pair> values)
9116 location_t saved_loc = input_location;
9117 input_location = loc;
9119 struct c_enum_contents the_enum;
9120 tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9122 tree value_chain = NULL_TREE;
9123 string_int_pair *value;
9124 unsigned int i;
9125 FOR_EACH_VEC_ELT (values, i, value)
9127 tree decl = build_enumerator (loc, loc, &the_enum,
9128 get_identifier (value->first),
9129 build_int_cst (integer_type_node,
9130 value->second));
9131 TREE_CHAIN (decl) = value_chain;
9132 value_chain = decl;
9135 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9137 input_location = saved_loc;
9138 return enumtype;
9141 /* Create the FUNCTION_DECL for a function definition.
9142 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9143 the declaration; they describe the function's name and the type it returns,
9144 but twisted together in a fashion that parallels the syntax of C.
9146 This function creates a binding context for the function body
9147 as well as setting up the FUNCTION_DECL in current_function_decl.
9149 Returns true on success. If the DECLARATOR is not suitable for a function
9150 (it defines a datum instead), we return false to report a parse error. */
9152 bool
9153 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9154 tree attributes)
9156 tree decl1, old_decl;
9157 tree restype, resdecl;
9158 location_t loc;
9160 current_function_returns_value = 0; /* Assume, until we see it does. */
9161 current_function_returns_null = 0;
9162 current_function_returns_abnormally = 0;
9163 warn_about_return_type = 0;
9164 c_switch_stack = NULL;
9166 /* Indicate no valid break/continue context by setting these variables
9167 to some non-null, non-label value. We'll notice and emit the proper
9168 error message in c_finish_bc_stmt. */
9169 c_break_label = c_cont_label = size_zero_node;
9171 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9172 &attributes, NULL, NULL, DEPRECATED_NORMAL);
9173 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9175 /* If the declarator is not suitable for a function definition,
9176 cause a syntax error. */
9177 if (decl1 == NULL_TREE
9178 || TREE_CODE (decl1) != FUNCTION_DECL)
9179 return false;
9181 loc = DECL_SOURCE_LOCATION (decl1);
9183 /* A nested function is not global. */
9184 if (current_function_decl != NULL_TREE)
9185 TREE_PUBLIC (decl1) = 0;
9187 c_decl_attributes (&decl1, attributes, 0);
9189 if (DECL_DECLARED_INLINE_P (decl1)
9190 && DECL_UNINLINABLE (decl1)
9191 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9192 warning_at (loc, OPT_Wattributes,
9193 "inline function %qD given attribute %qs",
9194 decl1, "noinline");
9196 /* Handle gnu_inline attribute. */
9197 if (declspecs->inline_p
9198 && !flag_gnu89_inline
9199 && TREE_CODE (decl1) == FUNCTION_DECL
9200 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9201 || current_function_decl))
9203 if (declspecs->storage_class != csc_static)
9204 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9207 announce_function (decl1);
9209 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9211 error_at (loc, "return type is an incomplete type");
9212 /* Make it return void instead. */
9213 TREE_TYPE (decl1)
9214 = build_function_type (void_type_node,
9215 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9218 if (warn_about_return_type)
9219 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9220 : (warn_return_type > 0 ? OPT_Wreturn_type
9221 : OPT_Wimplicit_int),
9222 "return type defaults to %<int%>");
9224 /* Make the init_value nonzero so pushdecl knows this is not tentative.
9225 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
9226 DECL_INITIAL (decl1) = error_mark_node;
9228 /* If this definition isn't a prototype and we had a prototype declaration
9229 before, copy the arg type info from that prototype. */
9230 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9231 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9232 old_decl = NULL_TREE;
9233 current_function_prototype_locus = UNKNOWN_LOCATION;
9234 current_function_prototype_built_in = false;
9235 current_function_prototype_arg_types = NULL_TREE;
9236 if (!prototype_p (TREE_TYPE (decl1)))
9238 if (old_decl != NULL_TREE
9239 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
9240 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9241 TREE_TYPE (TREE_TYPE (old_decl))))
9243 if (stdarg_p (TREE_TYPE (old_decl)))
9245 auto_diagnostic_group d;
9246 warning_at (loc, 0, "%q+D defined as variadic function "
9247 "without prototype", decl1);
9248 locate_old_decl (old_decl);
9250 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
9251 TREE_TYPE (decl1));
9252 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9253 current_function_prototype_built_in
9254 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9255 current_function_prototype_arg_types
9256 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
9258 if (TREE_PUBLIC (decl1))
9260 /* If there is an external prototype declaration of this
9261 function, record its location but do not copy information
9262 to this decl. This may be an invisible declaration
9263 (built-in or in a scope which has finished) or simply
9264 have more refined argument types than any declaration
9265 found above. */
9266 struct c_binding *b;
9267 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9268 if (B_IN_SCOPE (b, external_scope))
9269 break;
9270 if (b)
9272 tree ext_decl, ext_type;
9273 ext_decl = b->decl;
9274 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9275 if (TREE_CODE (ext_type) == FUNCTION_TYPE
9276 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9277 TREE_TYPE (ext_type)))
9279 current_function_prototype_locus
9280 = DECL_SOURCE_LOCATION (ext_decl);
9281 current_function_prototype_built_in
9282 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9283 current_function_prototype_arg_types
9284 = TYPE_ARG_TYPES (ext_type);
9290 /* Optionally warn of old-fashioned def with no previous prototype. */
9291 if (warn_strict_prototypes
9292 && old_decl != error_mark_node
9293 && !prototype_p (TREE_TYPE (decl1))
9294 && C_DECL_ISNT_PROTOTYPE (old_decl))
9295 warning_at (loc, OPT_Wstrict_prototypes,
9296 "function declaration isn%'t a prototype");
9297 /* Optionally warn of any global def with no previous prototype. */
9298 else if (warn_missing_prototypes
9299 && old_decl != error_mark_node
9300 && TREE_PUBLIC (decl1)
9301 && !MAIN_NAME_P (DECL_NAME (decl1))
9302 && C_DECL_ISNT_PROTOTYPE (old_decl)
9303 && !DECL_DECLARED_INLINE_P (decl1))
9304 warning_at (loc, OPT_Wmissing_prototypes,
9305 "no previous prototype for %qD", decl1);
9306 /* Optionally warn of any def with no previous prototype
9307 if the function has already been used. */
9308 else if (warn_missing_prototypes
9309 && old_decl != NULL_TREE
9310 && old_decl != error_mark_node
9311 && TREE_USED (old_decl)
9312 && !prototype_p (TREE_TYPE (old_decl)))
9313 warning_at (loc, OPT_Wmissing_prototypes,
9314 "%qD was used with no prototype before its definition", decl1);
9315 /* Optionally warn of any global def with no previous declaration. */
9316 else if (warn_missing_declarations
9317 && TREE_PUBLIC (decl1)
9318 && old_decl == NULL_TREE
9319 && !MAIN_NAME_P (DECL_NAME (decl1))
9320 && !DECL_DECLARED_INLINE_P (decl1))
9321 warning_at (loc, OPT_Wmissing_declarations,
9322 "no previous declaration for %qD",
9323 decl1);
9324 /* Optionally warn of any def with no previous declaration
9325 if the function has already been used. */
9326 else if (warn_missing_declarations
9327 && old_decl != NULL_TREE
9328 && old_decl != error_mark_node
9329 && TREE_USED (old_decl)
9330 && C_DECL_IMPLICIT (old_decl))
9331 warning_at (loc, OPT_Wmissing_declarations,
9332 "%qD was used with no declaration before its definition", decl1);
9334 /* This function exists in static storage.
9335 (This does not mean `static' in the C sense!) */
9336 TREE_STATIC (decl1) = 1;
9338 /* This is the earliest point at which we might know the assembler
9339 name of the function. Thus, if it's set before this, die horribly. */
9340 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9342 /* If #pragma weak was used, mark the decl weak now. */
9343 if (current_scope == file_scope)
9344 maybe_apply_pragma_weak (decl1);
9346 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9347 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9349 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9350 != integer_type_node)
9351 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9352 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9353 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9354 decl1);
9356 check_main_parameter_types (decl1);
9358 if (!TREE_PUBLIC (decl1))
9359 pedwarn (loc, OPT_Wmain,
9360 "%qD is normally a non-static function", decl1);
9363 /* Record the decl so that the function name is defined.
9364 If we already have a decl for this name, and it is a FUNCTION_DECL,
9365 use the old decl. */
9367 current_function_decl = pushdecl (decl1);
9369 push_scope ();
9370 declare_parm_level ();
9372 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9373 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9374 DECL_ARTIFICIAL (resdecl) = 1;
9375 DECL_IGNORED_P (resdecl) = 1;
9376 DECL_RESULT (current_function_decl) = resdecl;
9378 start_fname_decls ();
9380 return true;
9383 /* Subroutine of store_parm_decls which handles new-style function
9384 definitions (prototype format). The parms already have decls, so we
9385 need only record them as in effect and complain if any redundant
9386 old-style parm decls were written. */
9387 static void
9388 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9390 tree decl;
9391 c_arg_tag *tag;
9392 unsigned ix;
9394 if (current_scope->bindings)
9396 error_at (DECL_SOURCE_LOCATION (fndecl),
9397 "old-style parameter declarations in prototyped "
9398 "function definition");
9400 /* Get rid of the old-style declarations. */
9401 pop_scope ();
9402 push_scope ();
9404 /* Don't issue this warning for nested functions, and don't issue this
9405 warning if we got here because ARG_INFO_TYPES was error_mark_node
9406 (this happens when a function definition has just an ellipsis in
9407 its parameter list). */
9408 else if (!in_system_header_at (input_location)
9409 && !current_function_scope
9410 && arg_info->types != error_mark_node)
9411 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9412 "traditional C rejects ISO C style function definitions");
9414 /* Now make all the parameter declarations visible in the function body.
9415 We can bypass most of the grunt work of pushdecl. */
9416 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9418 DECL_CONTEXT (decl) = current_function_decl;
9419 if (DECL_NAME (decl))
9421 bind (DECL_NAME (decl), decl, current_scope,
9422 /*invisible=*/false, /*nested=*/false,
9423 UNKNOWN_LOCATION);
9424 if (!TREE_USED (decl))
9425 warn_if_shadowing (decl);
9427 else
9428 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9431 /* Record the parameter list in the function declaration. */
9432 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9434 /* Now make all the ancillary declarations visible, likewise. */
9435 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9437 DECL_CONTEXT (decl) = current_function_decl;
9438 if (DECL_NAME (decl))
9439 bind (DECL_NAME (decl), decl, current_scope,
9440 /*invisible=*/false,
9441 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9442 UNKNOWN_LOCATION);
9445 /* And all the tag declarations. */
9446 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9447 if (tag->id)
9448 bind (tag->id, tag->type, current_scope,
9449 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9452 /* Subroutine of store_parm_decls which handles old-style function
9453 definitions (separate parameter list and declarations). */
9455 static void
9456 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9458 struct c_binding *b;
9459 tree parm, decl, last;
9460 tree parmids = arg_info->parms;
9461 hash_set<tree> seen_args;
9463 if (!in_system_header_at (input_location))
9465 if (flag_isoc2x)
9466 pedwarn (DECL_SOURCE_LOCATION (fndecl),
9467 OPT_Wold_style_definition, "old-style function definition");
9468 else
9469 warning_at (DECL_SOURCE_LOCATION (fndecl),
9470 OPT_Wold_style_definition,
9471 "old-style function definition");
9474 if (current_scope->had_vla_unspec)
9475 error ("%<[*]%> not allowed in other than function prototype scope");
9477 /* Match each formal parameter name with its declaration. Save each
9478 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9479 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9481 if (TREE_VALUE (parm) == NULL_TREE)
9483 error_at (DECL_SOURCE_LOCATION (fndecl),
9484 "parameter name missing from parameter list");
9485 TREE_PURPOSE (parm) = NULL_TREE;
9486 continue;
9489 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9490 if (b && B_IN_CURRENT_SCOPE (b))
9492 decl = b->decl;
9493 /* Skip erroneous parameters. */
9494 if (decl == error_mark_node)
9495 continue;
9496 /* If we got something other than a PARM_DECL it is an error. */
9497 if (TREE_CODE (decl) != PARM_DECL)
9499 error_at (DECL_SOURCE_LOCATION (decl),
9500 "%qD declared as a non-parameter", decl);
9501 continue;
9503 /* If the declaration is already marked, we have a duplicate
9504 name. Complain and ignore the duplicate. */
9505 else if (seen_args.contains (decl))
9507 error_at (DECL_SOURCE_LOCATION (decl),
9508 "multiple parameters named %qD", decl);
9509 TREE_PURPOSE (parm) = NULL_TREE;
9510 continue;
9512 /* If the declaration says "void", complain and turn it into
9513 an int. */
9514 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9516 error_at (DECL_SOURCE_LOCATION (decl),
9517 "parameter %qD declared with void type", decl);
9518 TREE_TYPE (decl) = integer_type_node;
9519 DECL_ARG_TYPE (decl) = integer_type_node;
9520 layout_decl (decl, 0);
9522 warn_if_shadowing (decl);
9524 /* If no declaration found, default to int. */
9525 else
9527 /* FIXME diagnostics: This should be the location of the argument,
9528 not the FNDECL. E.g., for an old-style declaration
9530 int f10(v) { blah; }
9532 We should use the location of the V, not the F10.
9533 Unfortunately, the V is an IDENTIFIER_NODE which has no
9534 location. In the future we need locations for c_arg_info
9535 entries.
9537 See gcc.dg/Wshadow-3.c for an example of this problem. */
9538 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9539 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9540 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9541 pushdecl (decl);
9542 warn_if_shadowing (decl);
9544 if (flag_isoc99)
9545 pedwarn (DECL_SOURCE_LOCATION (decl),
9546 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9547 decl);
9548 else
9549 warning_at (DECL_SOURCE_LOCATION (decl),
9550 OPT_Wmissing_parameter_type,
9551 "type of %qD defaults to %<int%>", decl);
9554 TREE_PURPOSE (parm) = decl;
9555 seen_args.add (decl);
9558 /* Now examine the parms chain for incomplete declarations
9559 and declarations with no corresponding names. */
9561 for (b = current_scope->bindings; b; b = b->prev)
9563 parm = b->decl;
9564 if (TREE_CODE (parm) != PARM_DECL)
9565 continue;
9567 if (TREE_TYPE (parm) != error_mark_node
9568 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9570 error_at (DECL_SOURCE_LOCATION (parm),
9571 "parameter %qD has incomplete type", parm);
9572 TREE_TYPE (parm) = error_mark_node;
9575 if (!seen_args.contains (parm))
9577 error_at (DECL_SOURCE_LOCATION (parm),
9578 "declaration for parameter %qD but no such parameter",
9579 parm);
9581 /* Pretend the parameter was not missing.
9582 This gets us to a standard state and minimizes
9583 further error messages. */
9584 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9588 /* Chain the declarations together in the order of the list of
9589 names. Store that chain in the function decl, replacing the
9590 list of names. Update the current scope to match. */
9591 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9593 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9594 if (TREE_PURPOSE (parm))
9595 break;
9596 if (parm && TREE_PURPOSE (parm))
9598 last = TREE_PURPOSE (parm);
9599 DECL_ARGUMENTS (fndecl) = last;
9601 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9602 if (TREE_PURPOSE (parm))
9604 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9605 last = TREE_PURPOSE (parm);
9607 DECL_CHAIN (last) = NULL_TREE;
9610 /* If there was a previous prototype,
9611 set the DECL_ARG_TYPE of each argument according to
9612 the type previously specified, and report any mismatches. */
9614 if (current_function_prototype_arg_types)
9616 tree type;
9617 for (parm = DECL_ARGUMENTS (fndecl),
9618 type = current_function_prototype_arg_types;
9619 parm || (type != NULL_TREE
9620 && TREE_VALUE (type) != error_mark_node
9621 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9622 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9624 if (parm == NULL_TREE
9625 || type == NULL_TREE
9626 || (TREE_VALUE (type) != error_mark_node
9627 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9629 if (current_function_prototype_built_in)
9630 warning_at (DECL_SOURCE_LOCATION (fndecl),
9631 0, "number of arguments doesn%'t match "
9632 "built-in prototype");
9633 else
9635 /* FIXME diagnostics: This should be the location of
9636 FNDECL, but there is bug when a prototype is
9637 declared inside function context, but defined
9638 outside of it (e.g., gcc.dg/pr15698-2.c). In
9639 which case FNDECL gets the location of the
9640 prototype, not the definition. */
9641 error_at (input_location,
9642 "number of arguments doesn%'t match prototype");
9644 error_at (current_function_prototype_locus,
9645 "prototype declaration");
9647 break;
9649 /* Type for passing arg must be consistent with that
9650 declared for the arg. ISO C says we take the unqualified
9651 type for parameters declared with qualified type. */
9652 if (TREE_TYPE (parm) != error_mark_node
9653 && TREE_VALUE (type) != error_mark_node
9654 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9655 != TYPE_ATOMIC (TREE_VALUE (type)))
9656 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9657 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9659 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9660 == TYPE_ATOMIC (TREE_VALUE (type)))
9661 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9662 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9664 /* Adjust argument to match prototype. E.g. a previous
9665 `int foo(float);' prototype causes
9666 `int foo(x) float x; {...}' to be treated like
9667 `int foo(float x) {...}'. This is particularly
9668 useful for argument types like uid_t. */
9669 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9671 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9672 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9673 && (TYPE_PRECISION (TREE_TYPE (parm))
9674 < TYPE_PRECISION (integer_type_node)))
9675 DECL_ARG_TYPE (parm)
9676 = c_type_promotes_to (TREE_TYPE (parm));
9678 /* ??? Is it possible to get here with a
9679 built-in prototype or will it always have
9680 been diagnosed as conflicting with an
9681 old-style definition and discarded? */
9682 if (current_function_prototype_built_in)
9683 warning_at (DECL_SOURCE_LOCATION (parm),
9684 OPT_Wpedantic, "promoted argument %qD "
9685 "doesn%'t match built-in prototype", parm);
9686 else
9688 pedwarn (DECL_SOURCE_LOCATION (parm),
9689 OPT_Wpedantic, "promoted argument %qD "
9690 "doesn%'t match prototype", parm);
9691 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9692 "prototype declaration");
9695 else
9697 if (current_function_prototype_built_in)
9698 warning_at (DECL_SOURCE_LOCATION (parm),
9699 0, "argument %qD doesn%'t match "
9700 "built-in prototype", parm);
9701 else
9703 error_at (DECL_SOURCE_LOCATION (parm),
9704 "argument %qD doesn%'t match prototype", parm);
9705 error_at (current_function_prototype_locus,
9706 "prototype declaration");
9711 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9714 /* Otherwise, create a prototype that would match. */
9716 else
9718 tree actual = NULL_TREE, last = NULL_TREE, type;
9720 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9722 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9723 if (last)
9724 TREE_CHAIN (last) = type;
9725 else
9726 actual = type;
9727 last = type;
9729 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9730 if (last)
9731 TREE_CHAIN (last) = type;
9732 else
9733 actual = type;
9735 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9736 of the type of this function, but we need to avoid having this
9737 affect the types of other similarly-typed functions, so we must
9738 first force the generation of an identical (but separate) type
9739 node for the relevant function type. The new node we create
9740 will be a variant of the main variant of the original function
9741 type. */
9743 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9745 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9749 /* Store parameter declarations passed in ARG_INFO into the current
9750 function declaration. */
9752 void
9753 store_parm_decls_from (struct c_arg_info *arg_info)
9755 current_function_arg_info = arg_info;
9756 store_parm_decls ();
9759 /* Called by walk_tree to look for and update context-less labels
9760 or labels with context in the parent function. */
9762 static tree
9763 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9765 tree ctx = static_cast<tree>(data);
9766 if (TREE_CODE (*tp) == LABEL_EXPR
9767 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
9768 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
9770 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
9771 *walk_subtrees = 0;
9774 return NULL_TREE;
9777 /* Store the parameter declarations into the current function declaration.
9778 This is called after parsing the parameter declarations, before
9779 digesting the body of the function.
9781 For an old-style definition, construct a prototype out of the old-style
9782 parameter declarations and inject it into the function's type. */
9784 void
9785 store_parm_decls (void)
9787 tree fndecl = current_function_decl;
9788 bool proto;
9790 /* The argument information block for FNDECL. */
9791 struct c_arg_info *arg_info = current_function_arg_info;
9792 current_function_arg_info = 0;
9794 /* True if this definition is written with a prototype. In C2X, an
9795 empty argument list was converted to (void) in grokparms; in
9796 older C standard versions, it does not give the function a type
9797 with a prototype for future calls. */
9798 proto = arg_info->types != 0;
9800 if (proto)
9801 store_parm_decls_newstyle (fndecl, arg_info);
9802 else
9803 store_parm_decls_oldstyle (fndecl, arg_info);
9805 /* The next call to push_scope will be a function body. */
9807 next_is_function_body = true;
9809 /* Write a record describing this function definition to the prototypes
9810 file (if requested). */
9812 gen_aux_info_record (fndecl, 1, 0, proto);
9814 /* Initialize the RTL code for the function. */
9815 allocate_struct_function (fndecl, false);
9817 if (warn_unused_local_typedefs)
9818 cfun->language = ggc_cleared_alloc<language_function> ();
9820 /* Begin the statement tree for this function. */
9821 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9823 /* ??? Insert the contents of the pending sizes list into the function
9824 to be evaluated. The only reason left to have this is
9825 void foo(int n, int array[n++])
9826 because we throw away the array type in favor of a pointer type, and
9827 thus won't naturally see the SAVE_EXPR containing the increment. All
9828 other pending sizes would be handled by gimplify_parameters. */
9829 if (arg_info->pending_sizes)
9831 /* In very special circumstances, e.g. for code like
9832 _Atomic int i = 5;
9833 void f (int a[i += 2]) {}
9834 we need to execute the atomic assignment on function entry.
9835 But in this case, it is not just a straight store, it has the
9836 op= form, which means that build_atomic_assign has generated
9837 gotos, labels, etc. Because at that time the function decl
9838 for F has not been created yet, those labels do not have any
9839 function context. But we have the fndecl now, so update the
9840 labels accordingly. gimplify_expr would crash otherwise.
9841 Or with nested functions the labels could be created with parent
9842 function's context, while when the statement is emitted at the
9843 start of the nested function, it needs the nested function's
9844 context. */
9845 walk_tree_without_duplicates (&arg_info->pending_sizes,
9846 set_labels_context_r, fndecl);
9847 add_stmt (arg_info->pending_sizes);
9851 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9852 c_finish_omp_declare_simd for function prototypes. No diagnostics
9853 should be done. */
9855 void
9856 temp_store_parm_decls (tree fndecl, tree parms)
9858 push_scope ();
9859 for (tree p = parms; p; p = DECL_CHAIN (p))
9861 DECL_CONTEXT (p) = fndecl;
9862 if (DECL_NAME (p))
9863 bind (DECL_NAME (p), p, current_scope,
9864 /*invisible=*/false, /*nested=*/false,
9865 UNKNOWN_LOCATION);
9869 /* Undo what temp_store_parm_decls did. */
9871 void
9872 temp_pop_parm_decls (void)
9874 /* Clear all bindings in this temporary scope, so that
9875 pop_scope doesn't create a BLOCK. */
9876 struct c_binding *b = current_scope->bindings;
9877 current_scope->bindings = NULL;
9878 for (; b; b = free_binding_and_advance (b))
9880 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9881 || b->decl == error_mark_node);
9882 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9883 I_SYMBOL_BINDING (b->id) = b->shadowed;
9884 if (b->shadowed && b->shadowed->u.type)
9885 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9887 pop_scope ();
9891 /* Finish up a function declaration and compile that function
9892 all the way to assembler language output. Then free the storage
9893 for the function definition.
9895 This is called after parsing the body of the function definition. */
9897 void
9898 finish_function (location_t end_loc)
9900 tree fndecl = current_function_decl;
9902 if (c_dialect_objc ())
9903 objc_finish_function ();
9905 if (TREE_CODE (fndecl) == FUNCTION_DECL
9906 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9908 tree args = DECL_ARGUMENTS (fndecl);
9909 for (; args; args = DECL_CHAIN (args))
9911 tree type = TREE_TYPE (args);
9912 if (INTEGRAL_TYPE_P (type)
9913 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9914 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9918 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9919 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9921 /* Must mark the RESULT_DECL as being in this function. */
9923 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9924 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9926 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9927 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9928 == integer_type_node && flag_isoc99)
9930 /* Hack. We don't want the middle-end to warn that this return
9931 is unreachable, so we mark its location as special. Using
9932 UNKNOWN_LOCATION has the problem that it gets clobbered in
9933 annotate_one_with_locus. A cleaner solution might be to
9934 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9936 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9939 /* Tie off the statement tree for this function. */
9940 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9942 finish_fname_decls ();
9944 /* Complain if there's no return statement only if option specified on
9945 command line. */
9946 if (warn_return_type > 0
9947 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9948 && !current_function_returns_value && !current_function_returns_null
9949 /* Don't complain if we are no-return. */
9950 && !current_function_returns_abnormally
9951 /* Don't complain if we are declared noreturn. */
9952 && !TREE_THIS_VOLATILE (fndecl)
9953 /* Don't warn for main(). */
9954 && !MAIN_NAME_P (DECL_NAME (fndecl))
9955 /* Or if they didn't actually specify a return type. */
9956 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9957 /* Normally, with -Wreturn-type, flow will complain, but we might
9958 optimize out static functions. */
9959 && !TREE_PUBLIC (fndecl)
9960 && targetm.warn_func_return (fndecl)
9961 && warning (OPT_Wreturn_type,
9962 "no return statement in function returning non-void"))
9963 TREE_NO_WARNING (fndecl) = 1;
9965 /* Complain about parameters that are only set, but never otherwise used. */
9966 if (warn_unused_but_set_parameter)
9968 tree decl;
9970 for (decl = DECL_ARGUMENTS (fndecl);
9971 decl;
9972 decl = DECL_CHAIN (decl))
9973 if (TREE_USED (decl)
9974 && TREE_CODE (decl) == PARM_DECL
9975 && !DECL_READ_P (decl)
9976 && DECL_NAME (decl)
9977 && !DECL_ARTIFICIAL (decl)
9978 && !TREE_NO_WARNING (decl))
9979 warning_at (DECL_SOURCE_LOCATION (decl),
9980 OPT_Wunused_but_set_parameter,
9981 "parameter %qD set but not used", decl);
9984 /* Complain about locally defined typedefs that are not used in this
9985 function. */
9986 maybe_warn_unused_local_typedefs ();
9988 /* Possibly warn about unused parameters. */
9989 if (warn_unused_parameter)
9990 do_warn_unused_parameter (fndecl);
9992 /* Store the end of the function, so that we get good line number
9993 info for the epilogue. */
9994 cfun->function_end_locus = end_loc;
9996 /* Finalize the ELF visibility for the function. */
9997 c_determine_visibility (fndecl);
9999 /* For GNU C extern inline functions disregard inline limits. */
10000 if (DECL_EXTERNAL (fndecl)
10001 && DECL_DECLARED_INLINE_P (fndecl)
10002 && (flag_gnu89_inline
10003 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10004 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10006 /* Genericize before inlining. Delay genericizing nested functions
10007 until their parent function is genericized. Since finalizing
10008 requires GENERIC, delay that as well. */
10010 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10011 && !undef_nested_function)
10013 if (!decl_function_context (fndecl))
10015 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10016 c_genericize (fndecl);
10018 /* ??? Objc emits functions after finalizing the compilation unit.
10019 This should be cleaned up later and this conditional removed. */
10020 if (symtab->global_info_ready)
10022 cgraph_node::add_new_function (fndecl, false);
10023 return;
10025 cgraph_node::finalize_function (fndecl, false);
10027 else
10029 /* Register this function with cgraph just far enough to get it
10030 added to our parent's nested function list. Handy, since the
10031 C front end doesn't have such a list. */
10032 (void) cgraph_node::get_create (fndecl);
10036 if (!decl_function_context (fndecl))
10037 undef_nested_function = false;
10039 if (cfun->language != NULL)
10041 ggc_free (cfun->language);
10042 cfun->language = NULL;
10045 /* We're leaving the context of this function, so zap cfun.
10046 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10047 tree_rest_of_compilation. */
10048 set_cfun (NULL);
10049 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10050 current_function_decl = NULL;
10053 /* Check the declarations given in a for-loop for satisfying the C99
10054 constraints. If exactly one such decl is found, return it. LOC is
10055 the location of the opening parenthesis of the for loop. The last
10056 parameter allows you to control the "for loop initial declarations
10057 are only allowed in C99 mode". Normally, you should pass
10058 flag_isoc99 as that parameter. But in some cases (Objective-C
10059 foreach loop, for example) we want to run the checks in this
10060 function even if not in C99 mode, so we allow the caller to turn
10061 off the error about not being in C99 mode.
10064 tree
10065 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10067 struct c_binding *b;
10068 tree one_decl = NULL_TREE;
10069 int n_decls = 0;
10071 if (!turn_off_iso_c99_error)
10073 static bool hint = true;
10074 /* If we get here, declarations have been used in a for loop without
10075 the C99 for loop scope. This doesn't make much sense, so don't
10076 allow it. */
10077 error_at (loc, "%<for%> loop initial declarations "
10078 "are only allowed in C99 or C11 mode");
10079 if (hint)
10081 inform (loc,
10082 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
10083 "%<-std=gnu11%> to compile your code");
10084 hint = false;
10086 return NULL_TREE;
10088 else
10089 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10090 "initial declarations");
10092 /* C99 subclause 6.8.5 paragraph 3:
10094 [#3] The declaration part of a for statement shall only
10095 declare identifiers for objects having storage class auto or
10096 register.
10098 It isn't clear whether, in this sentence, "identifiers" binds to
10099 "shall only declare" or to "objects" - that is, whether all identifiers
10100 declared must be identifiers for objects, or whether the restriction
10101 only applies to those that are. (A question on this in comp.std.c
10102 in November 2000 received no answer.) We implement the strictest
10103 interpretation, to avoid creating an extension which later causes
10104 problems. */
10106 for (b = current_scope->bindings; b; b = b->prev)
10108 tree id = b->id;
10109 tree decl = b->decl;
10111 if (!id)
10112 continue;
10114 switch (TREE_CODE (decl))
10116 case VAR_DECL:
10118 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10119 if (TREE_STATIC (decl))
10120 error_at (decl_loc,
10121 "declaration of static variable %qD in %<for%> loop "
10122 "initial declaration", decl);
10123 else if (DECL_EXTERNAL (decl))
10124 error_at (decl_loc,
10125 "declaration of %<extern%> variable %qD in %<for%> loop "
10126 "initial declaration", decl);
10128 break;
10130 case RECORD_TYPE:
10131 error_at (loc,
10132 "%<struct %E%> declared in %<for%> loop initial "
10133 "declaration", id);
10134 break;
10135 case UNION_TYPE:
10136 error_at (loc,
10137 "%<union %E%> declared in %<for%> loop initial declaration",
10138 id);
10139 break;
10140 case ENUMERAL_TYPE:
10141 error_at (loc, "%<enum %E%> declared in %<for%> loop "
10142 "initial declaration", id);
10143 break;
10144 default:
10145 error_at (loc, "declaration of non-variable "
10146 "%qD in %<for%> loop initial declaration", decl);
10149 n_decls++;
10150 one_decl = decl;
10153 return n_decls == 1 ? one_decl : NULL_TREE;
10156 /* Save and reinitialize the variables
10157 used during compilation of a C function. */
10159 void
10160 c_push_function_context (void)
10162 struct language_function *p = cfun->language;
10163 /* cfun->language might have been already allocated by the use of
10164 -Wunused-local-typedefs. In that case, just re-use it. */
10165 if (p == NULL)
10166 cfun->language = p = ggc_cleared_alloc<language_function> ();
10168 p->base.x_stmt_tree = c_stmt_tree;
10169 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10170 p->x_break_label = c_break_label;
10171 p->x_cont_label = c_cont_label;
10172 p->x_switch_stack = c_switch_stack;
10173 p->arg_info = current_function_arg_info;
10174 p->returns_value = current_function_returns_value;
10175 p->returns_null = current_function_returns_null;
10176 p->returns_abnormally = current_function_returns_abnormally;
10177 p->warn_about_return_type = warn_about_return_type;
10179 push_function_context ();
10182 /* Restore the variables used during compilation of a C function. */
10184 void
10185 c_pop_function_context (void)
10187 struct language_function *p;
10189 pop_function_context ();
10190 p = cfun->language;
10192 /* When -Wunused-local-typedefs is in effect, cfun->languages is
10193 used to store data throughout the life time of the current cfun,
10194 So don't deallocate it. */
10195 if (!warn_unused_local_typedefs)
10196 cfun->language = NULL;
10198 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10199 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10201 /* Stop pointing to the local nodes about to be freed. */
10202 /* But DECL_INITIAL must remain nonzero so we know this
10203 was an actual function definition. */
10204 DECL_INITIAL (current_function_decl) = error_mark_node;
10205 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10208 c_stmt_tree = p->base.x_stmt_tree;
10209 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10210 c_break_label = p->x_break_label;
10211 c_cont_label = p->x_cont_label;
10212 c_switch_stack = p->x_switch_stack;
10213 current_function_arg_info = p->arg_info;
10214 current_function_returns_value = p->returns_value;
10215 current_function_returns_null = p->returns_null;
10216 current_function_returns_abnormally = p->returns_abnormally;
10217 warn_about_return_type = p->warn_about_return_type;
10220 /* The functions below are required for functionality of doing
10221 function at once processing in the C front end. Currently these
10222 functions are not called from anywhere in the C front end, but as
10223 these changes continue, that will change. */
10225 /* Returns the stmt_tree (if any) to which statements are currently
10226 being added. If there is no active statement-tree, NULL is
10227 returned. */
10229 stmt_tree
10230 current_stmt_tree (void)
10232 return &c_stmt_tree;
10235 /* Return the global value of T as a symbol. */
10237 tree
10238 identifier_global_value (tree t)
10240 struct c_binding *b;
10242 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10243 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10244 return b->decl;
10246 return NULL_TREE;
10249 /* Return the global value of tag T as a symbol. */
10251 tree
10252 identifier_global_tag (tree t)
10254 struct c_binding *b;
10256 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10257 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10258 return b->decl;
10260 return NULL_TREE;
10263 /* Returns true if NAME refers to a built-in function or function-like
10264 operator. */
10266 bool
10267 names_builtin_p (const char *name)
10269 tree id = get_identifier (name);
10270 if (tree decl = identifier_global_value (id))
10271 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_BUILTIN (decl);
10273 /* Also detect common reserved C words that aren't strictly built-in
10274 functions. */
10275 switch (C_RID_CODE (id))
10277 case RID_BUILTIN_CONVERTVECTOR:
10278 case RID_BUILTIN_HAS_ATTRIBUTE:
10279 case RID_BUILTIN_SHUFFLE:
10280 case RID_CHOOSE_EXPR:
10281 case RID_OFFSETOF:
10282 case RID_TYPES_COMPATIBLE_P:
10283 return true;
10284 default:
10285 break;
10288 return false;
10291 /* In C, the only C-linkage public declaration is at file scope. */
10293 tree
10294 c_linkage_bindings (tree name)
10296 return identifier_global_value (name);
10299 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
10300 otherwise the name is found in ridpointers from RID_INDEX. */
10302 void
10303 record_builtin_type (enum rid rid_index, const char *name, tree type)
10305 tree id, decl;
10306 if (name == 0)
10307 id = ridpointers[(int) rid_index];
10308 else
10309 id = get_identifier (name);
10310 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10311 pushdecl (decl);
10312 if (debug_hooks->type_decl)
10313 debug_hooks->type_decl (decl, false);
10316 /* Build the void_list_node (void_type_node having been created). */
10317 tree
10318 build_void_list_node (void)
10320 tree t = build_tree_list (NULL_TREE, void_type_node);
10321 return t;
10324 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10326 struct c_parm *
10327 build_c_parm (struct c_declspecs *specs, tree attrs,
10328 struct c_declarator *declarator,
10329 location_t loc)
10331 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10332 ret->specs = specs;
10333 ret->attrs = attrs;
10334 ret->declarator = declarator;
10335 ret->loc = loc;
10336 return ret;
10339 /* Return a declarator with nested attributes. TARGET is the inner
10340 declarator to which these attributes apply. ATTRS are the
10341 attributes. */
10343 struct c_declarator *
10344 build_attrs_declarator (tree attrs, struct c_declarator *target)
10346 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10347 ret->kind = cdk_attrs;
10348 ret->declarator = target;
10349 ret->u.attrs = attrs;
10350 return ret;
10353 /* Return a declarator for a function with arguments specified by ARGS
10354 and return type specified by TARGET. */
10356 struct c_declarator *
10357 build_function_declarator (struct c_arg_info *args,
10358 struct c_declarator *target)
10360 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10361 ret->kind = cdk_function;
10362 ret->declarator = target;
10363 ret->u.arg_info = args;
10364 return ret;
10367 /* Return a declarator for the identifier IDENT (which may be
10368 NULL_TREE for an abstract declarator). */
10370 struct c_declarator *
10371 build_id_declarator (tree ident)
10373 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10374 ret->kind = cdk_id;
10375 ret->declarator = 0;
10376 ret->u.id.id = ident;
10377 ret->u.id.attrs = NULL_TREE;
10378 /* Default value - may get reset to a more precise location. */
10379 ret->id_loc = input_location;
10380 return ret;
10383 /* Return something to represent absolute declarators containing a *.
10384 TARGET is the absolute declarator that the * contains.
10385 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10386 to apply to the pointer type. */
10388 struct c_declarator *
10389 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10390 struct c_declarator *target)
10392 tree attrs;
10393 int quals = 0;
10394 struct c_declarator *itarget = target;
10395 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10396 if (type_quals_attrs)
10398 attrs = type_quals_attrs->attrs;
10399 quals = quals_from_declspecs (type_quals_attrs);
10400 if (attrs != NULL_TREE)
10401 itarget = build_attrs_declarator (attrs, target);
10403 ret->kind = cdk_pointer;
10404 ret->declarator = itarget;
10405 ret->u.pointer_quals = quals;
10406 return ret;
10409 /* Return a pointer to a structure for an empty list of declaration
10410 specifiers. */
10412 struct c_declspecs *
10413 build_null_declspecs (void)
10415 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10416 memset (ret, 0, sizeof *ret);
10417 ret->align_log = -1;
10418 ret->typespec_word = cts_none;
10419 ret->storage_class = csc_none;
10420 ret->expr_const_operands = true;
10421 ret->typespec_kind = ctsk_none;
10422 ret->address_space = ADDR_SPACE_GENERIC;
10423 return ret;
10426 /* Add the address space ADDRSPACE to the declaration specifiers
10427 SPECS, returning SPECS. */
10429 struct c_declspecs *
10430 declspecs_add_addrspace (location_t location,
10431 struct c_declspecs *specs, addr_space_t as)
10433 specs->non_sc_seen_p = true;
10434 specs->declspecs_seen_p = true;
10435 specs->non_std_attrs_seen_p = true;
10437 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10438 && specs->address_space != as)
10439 error ("incompatible address space qualifiers %qs and %qs",
10440 c_addr_space_name (as),
10441 c_addr_space_name (specs->address_space));
10442 else
10444 specs->address_space = as;
10445 specs->locations[cdw_address_space] = location;
10447 return specs;
10450 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10451 returning SPECS. */
10453 struct c_declspecs *
10454 declspecs_add_qual (location_t loc,
10455 struct c_declspecs *specs, tree qual)
10457 enum rid i;
10458 bool dupe = false;
10459 specs->non_sc_seen_p = true;
10460 specs->declspecs_seen_p = true;
10461 specs->non_std_attrs_seen_p = true;
10462 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10463 && C_IS_RESERVED_WORD (qual));
10464 i = C_RID_CODE (qual);
10465 location_t prev_loc = UNKNOWN_LOCATION;
10466 switch (i)
10468 case RID_CONST:
10469 dupe = specs->const_p;
10470 specs->const_p = true;
10471 prev_loc = specs->locations[cdw_const];
10472 specs->locations[cdw_const] = loc;
10473 break;
10474 case RID_VOLATILE:
10475 dupe = specs->volatile_p;
10476 specs->volatile_p = true;
10477 prev_loc = specs->locations[cdw_volatile];
10478 specs->locations[cdw_volatile] = loc;
10479 break;
10480 case RID_RESTRICT:
10481 dupe = specs->restrict_p;
10482 specs->restrict_p = true;
10483 prev_loc = specs->locations[cdw_restrict];
10484 specs->locations[cdw_restrict] = loc;
10485 break;
10486 case RID_ATOMIC:
10487 dupe = specs->atomic_p;
10488 specs->atomic_p = true;
10489 prev_loc = specs->locations[cdw_atomic];
10490 specs->locations[cdw_atomic] = loc;
10491 break;
10492 default:
10493 gcc_unreachable ();
10495 if (dupe)
10497 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10498 "duplicate %qE declaration specifier", qual);
10499 if (!warned
10500 && warn_duplicate_decl_specifier
10501 && prev_loc >= RESERVED_LOCATION_COUNT
10502 && !from_macro_expansion_at (prev_loc)
10503 && !from_macro_expansion_at (loc))
10504 warning_at (loc, OPT_Wduplicate_decl_specifier,
10505 "duplicate %qE declaration specifier", qual);
10507 return specs;
10510 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10511 returning SPECS. */
10513 struct c_declspecs *
10514 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10515 struct c_typespec spec)
10517 tree type = spec.spec;
10518 specs->non_sc_seen_p = true;
10519 specs->declspecs_seen_p = true;
10520 specs->non_std_attrs_seen_p = true;
10521 specs->typespec_kind = spec.kind;
10522 if (TREE_DEPRECATED (type))
10523 specs->deprecated_p = true;
10525 /* Handle type specifier keywords. */
10526 if (TREE_CODE (type) == IDENTIFIER_NODE
10527 && C_IS_RESERVED_WORD (type)
10528 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10530 enum rid i = C_RID_CODE (type);
10531 if (specs->type)
10533 error_at (loc, "two or more data types in declaration specifiers");
10534 return specs;
10536 if ((int) i <= (int) RID_LAST_MODIFIER)
10538 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10539 bool dupe = false;
10540 switch (i)
10542 case RID_LONG:
10543 if (specs->long_long_p)
10545 error_at (loc, "%<long long long%> is too long for GCC");
10546 break;
10548 if (specs->long_p)
10550 if (specs->typespec_word == cts_double)
10552 error_at (loc,
10553 ("both %<long long%> and %<double%> in "
10554 "declaration specifiers"));
10555 break;
10557 pedwarn_c90 (loc, OPT_Wlong_long,
10558 "ISO C90 does not support %<long long%>");
10559 specs->long_long_p = 1;
10560 specs->locations[cdw_long_long] = loc;
10561 break;
10563 if (specs->short_p)
10564 error_at (loc,
10565 ("both %<long%> and %<short%> in "
10566 "declaration specifiers"));
10567 else if (specs->typespec_word == cts_auto_type)
10568 error_at (loc,
10569 ("both %<long%> and %<__auto_type%> in "
10570 "declaration specifiers"));
10571 else if (specs->typespec_word == cts_void)
10572 error_at (loc,
10573 ("both %<long%> and %<void%> in "
10574 "declaration specifiers"));
10575 else if (specs->typespec_word == cts_int_n)
10576 error_at (loc,
10577 ("both %<long%> and %<__int%d%> in "
10578 "declaration specifiers"),
10579 int_n_data[specs->int_n_idx].bitsize);
10580 else if (specs->typespec_word == cts_bool)
10581 error_at (loc,
10582 ("both %<long%> and %<_Bool%> in "
10583 "declaration specifiers"));
10584 else if (specs->typespec_word == cts_char)
10585 error_at (loc,
10586 ("both %<long%> and %<char%> in "
10587 "declaration specifiers"));
10588 else if (specs->typespec_word == cts_float)
10589 error_at (loc,
10590 ("both %<long%> and %<float%> in "
10591 "declaration specifiers"));
10592 else if (specs->typespec_word == cts_floatn_nx)
10593 error_at (loc,
10594 ("both %<long%> and %<_Float%d%s%> in "
10595 "declaration specifiers"),
10596 floatn_nx_types[specs->floatn_nx_idx].n,
10597 (floatn_nx_types[specs->floatn_nx_idx].extended
10598 ? "x"
10599 : ""));
10600 else if (specs->typespec_word == cts_dfloat32)
10601 error_at (loc,
10602 ("both %<long%> and %<_Decimal32%> in "
10603 "declaration specifiers"));
10604 else if (specs->typespec_word == cts_dfloat64)
10605 error_at (loc,
10606 ("both %<long%> and %<_Decimal64%> in "
10607 "declaration specifiers"));
10608 else if (specs->typespec_word == cts_dfloat128)
10609 error_at (loc,
10610 ("both %<long%> and %<_Decimal128%> in "
10611 "declaration specifiers"));
10612 else
10614 specs->long_p = true;
10615 specs->locations[cdw_long] = loc;
10617 break;
10618 case RID_SHORT:
10619 dupe = specs->short_p;
10620 if (specs->long_p)
10621 error_at (loc,
10622 ("both %<long%> and %<short%> in "
10623 "declaration specifiers"));
10624 else if (specs->typespec_word == cts_auto_type)
10625 error_at (loc,
10626 ("both %<short%> and %<__auto_type%> in "
10627 "declaration specifiers"));
10628 else if (specs->typespec_word == cts_void)
10629 error_at (loc,
10630 ("both %<short%> and %<void%> in "
10631 "declaration specifiers"));
10632 else if (specs->typespec_word == cts_int_n)
10633 error_at (loc,
10634 ("both %<short%> and %<__int%d%> in "
10635 "declaration specifiers"),
10636 int_n_data[specs->int_n_idx].bitsize);
10637 else if (specs->typespec_word == cts_bool)
10638 error_at (loc,
10639 ("both %<short%> and %<_Bool%> in "
10640 "declaration specifiers"));
10641 else if (specs->typespec_word == cts_char)
10642 error_at (loc,
10643 ("both %<short%> and %<char%> in "
10644 "declaration specifiers"));
10645 else if (specs->typespec_word == cts_float)
10646 error_at (loc,
10647 ("both %<short%> and %<float%> in "
10648 "declaration specifiers"));
10649 else if (specs->typespec_word == cts_double)
10650 error_at (loc,
10651 ("both %<short%> and %<double%> in "
10652 "declaration specifiers"));
10653 else if (specs->typespec_word == cts_floatn_nx)
10654 error_at (loc,
10655 ("both %<short%> and %<_Float%d%s%> in "
10656 "declaration specifiers"),
10657 floatn_nx_types[specs->floatn_nx_idx].n,
10658 (floatn_nx_types[specs->floatn_nx_idx].extended
10659 ? "x"
10660 : ""));
10661 else if (specs->typespec_word == cts_dfloat32)
10662 error_at (loc,
10663 ("both %<short%> and %<_Decimal32%> in "
10664 "declaration specifiers"));
10665 else if (specs->typespec_word == cts_dfloat64)
10666 error_at (loc,
10667 ("both %<short%> and %<_Decimal64%> in "
10668 "declaration specifiers"));
10669 else if (specs->typespec_word == cts_dfloat128)
10670 error_at (loc,
10671 ("both %<short%> and %<_Decimal128%> in "
10672 "declaration specifiers"));
10673 else
10675 specs->short_p = true;
10676 specs->locations[cdw_short] = loc;
10678 break;
10679 case RID_SIGNED:
10680 dupe = specs->signed_p;
10681 if (specs->unsigned_p)
10682 error_at (loc,
10683 ("both %<signed%> and %<unsigned%> in "
10684 "declaration specifiers"));
10685 else if (specs->typespec_word == cts_auto_type)
10686 error_at (loc,
10687 ("both %<signed%> and %<__auto_type%> in "
10688 "declaration specifiers"));
10689 else if (specs->typespec_word == cts_void)
10690 error_at (loc,
10691 ("both %<signed%> and %<void%> in "
10692 "declaration specifiers"));
10693 else if (specs->typespec_word == cts_bool)
10694 error_at (loc,
10695 ("both %<signed%> and %<_Bool%> in "
10696 "declaration specifiers"));
10697 else if (specs->typespec_word == cts_float)
10698 error_at (loc,
10699 ("both %<signed%> and %<float%> in "
10700 "declaration specifiers"));
10701 else if (specs->typespec_word == cts_double)
10702 error_at (loc,
10703 ("both %<signed%> and %<double%> in "
10704 "declaration specifiers"));
10705 else if (specs->typespec_word == cts_floatn_nx)
10706 error_at (loc,
10707 ("both %<signed%> and %<_Float%d%s%> in "
10708 "declaration specifiers"),
10709 floatn_nx_types[specs->floatn_nx_idx].n,
10710 (floatn_nx_types[specs->floatn_nx_idx].extended
10711 ? "x"
10712 : ""));
10713 else if (specs->typespec_word == cts_dfloat32)
10714 error_at (loc,
10715 ("both %<signed%> and %<_Decimal32%> in "
10716 "declaration specifiers"));
10717 else if (specs->typespec_word == cts_dfloat64)
10718 error_at (loc,
10719 ("both %<signed%> and %<_Decimal64%> in "
10720 "declaration specifiers"));
10721 else if (specs->typespec_word == cts_dfloat128)
10722 error_at (loc,
10723 ("both %<signed%> and %<_Decimal128%> in "
10724 "declaration specifiers"));
10725 else
10727 specs->signed_p = true;
10728 specs->locations[cdw_signed] = loc;
10730 break;
10731 case RID_UNSIGNED:
10732 dupe = specs->unsigned_p;
10733 if (specs->signed_p)
10734 error_at (loc,
10735 ("both %<signed%> and %<unsigned%> in "
10736 "declaration specifiers"));
10737 else if (specs->typespec_word == cts_auto_type)
10738 error_at (loc,
10739 ("both %<unsigned%> and %<__auto_type%> in "
10740 "declaration specifiers"));
10741 else if (specs->typespec_word == cts_void)
10742 error_at (loc,
10743 ("both %<unsigned%> and %<void%> in "
10744 "declaration specifiers"));
10745 else if (specs->typespec_word == cts_bool)
10746 error_at (loc,
10747 ("both %<unsigned%> and %<_Bool%> in "
10748 "declaration specifiers"));
10749 else if (specs->typespec_word == cts_float)
10750 error_at (loc,
10751 ("both %<unsigned%> and %<float%> in "
10752 "declaration specifiers"));
10753 else if (specs->typespec_word == cts_double)
10754 error_at (loc,
10755 ("both %<unsigned%> and %<double%> in "
10756 "declaration specifiers"));
10757 else if (specs->typespec_word == cts_floatn_nx)
10758 error_at (loc,
10759 ("both %<unsigned%> and %<_Float%d%s%> in "
10760 "declaration specifiers"),
10761 floatn_nx_types[specs->floatn_nx_idx].n,
10762 (floatn_nx_types[specs->floatn_nx_idx].extended
10763 ? "x"
10764 : ""));
10765 else if (specs->typespec_word == cts_dfloat32)
10766 error_at (loc,
10767 ("both %<unsigned%> and %<_Decimal32%> in "
10768 "declaration specifiers"));
10769 else if (specs->typespec_word == cts_dfloat64)
10770 error_at (loc,
10771 ("both %<unsigned%> and %<_Decimal64%> in "
10772 "declaration specifiers"));
10773 else if (specs->typespec_word == cts_dfloat128)
10774 error_at (loc,
10775 ("both %<unsigned%> and %<_Decimal128%> in "
10776 "declaration specifiers"));
10777 else
10779 specs->unsigned_p = true;
10780 specs->locations[cdw_unsigned] = loc;
10782 break;
10783 case RID_COMPLEX:
10784 dupe = specs->complex_p;
10785 if (!in_system_header_at (loc))
10786 pedwarn_c90 (loc, OPT_Wpedantic,
10787 "ISO C90 does not support complex types");
10788 if (specs->typespec_word == cts_auto_type)
10789 error_at (loc,
10790 ("both %<complex%> and %<__auto_type%> in "
10791 "declaration specifiers"));
10792 else if (specs->typespec_word == cts_void)
10793 error_at (loc,
10794 ("both %<complex%> and %<void%> in "
10795 "declaration specifiers"));
10796 else if (specs->typespec_word == cts_bool)
10797 error_at (loc,
10798 ("both %<complex%> and %<_Bool%> in "
10799 "declaration specifiers"));
10800 else if (specs->typespec_word == cts_dfloat32)
10801 error_at (loc,
10802 ("both %<complex%> and %<_Decimal32%> in "
10803 "declaration specifiers"));
10804 else if (specs->typespec_word == cts_dfloat64)
10805 error_at (loc,
10806 ("both %<complex%> and %<_Decimal64%> in "
10807 "declaration specifiers"));
10808 else if (specs->typespec_word == cts_dfloat128)
10809 error_at (loc,
10810 ("both %<complex%> and %<_Decimal128%> in "
10811 "declaration specifiers"));
10812 else if (specs->typespec_word == cts_fract)
10813 error_at (loc,
10814 ("both %<complex%> and %<_Fract%> in "
10815 "declaration specifiers"));
10816 else if (specs->typespec_word == cts_accum)
10817 error_at (loc,
10818 ("both %<complex%> and %<_Accum%> in "
10819 "declaration specifiers"));
10820 else if (specs->saturating_p)
10821 error_at (loc,
10822 ("both %<complex%> and %<_Sat%> in "
10823 "declaration specifiers"));
10824 else
10826 specs->complex_p = true;
10827 specs->locations[cdw_complex] = loc;
10829 break;
10830 case RID_SAT:
10831 dupe = specs->saturating_p;
10832 pedwarn (loc, OPT_Wpedantic,
10833 "ISO C does not support saturating types");
10834 if (specs->typespec_word == cts_int_n)
10836 error_at (loc,
10837 ("both %<_Sat%> and %<__int%d%> in "
10838 "declaration specifiers"),
10839 int_n_data[specs->int_n_idx].bitsize);
10841 else if (specs->typespec_word == cts_auto_type)
10842 error_at (loc,
10843 ("both %<_Sat%> and %<__auto_type%> in "
10844 "declaration specifiers"));
10845 else if (specs->typespec_word == cts_void)
10846 error_at (loc,
10847 ("both %<_Sat%> and %<void%> in "
10848 "declaration specifiers"));
10849 else if (specs->typespec_word == cts_bool)
10850 error_at (loc,
10851 ("both %<_Sat%> and %<_Bool%> in "
10852 "declaration specifiers"));
10853 else if (specs->typespec_word == cts_char)
10854 error_at (loc,
10855 ("both %<_Sat%> and %<char%> in "
10856 "declaration specifiers"));
10857 else if (specs->typespec_word == cts_int)
10858 error_at (loc,
10859 ("both %<_Sat%> and %<int%> in "
10860 "declaration specifiers"));
10861 else if (specs->typespec_word == cts_float)
10862 error_at (loc,
10863 ("both %<_Sat%> and %<float%> in "
10864 "declaration specifiers"));
10865 else if (specs->typespec_word == cts_double)
10866 error_at (loc,
10867 ("both %<_Sat%> and %<double%> in "
10868 "declaration specifiers"));
10869 else if (specs->typespec_word == cts_floatn_nx)
10870 error_at (loc,
10871 ("both %<_Sat%> and %<_Float%d%s%> in "
10872 "declaration specifiers"),
10873 floatn_nx_types[specs->floatn_nx_idx].n,
10874 (floatn_nx_types[specs->floatn_nx_idx].extended
10875 ? "x"
10876 : ""));
10877 else if (specs->typespec_word == cts_dfloat32)
10878 error_at (loc,
10879 ("both %<_Sat%> and %<_Decimal32%> in "
10880 "declaration specifiers"));
10881 else if (specs->typespec_word == cts_dfloat64)
10882 error_at (loc,
10883 ("both %<_Sat%> and %<_Decimal64%> in "
10884 "declaration specifiers"));
10885 else if (specs->typespec_word == cts_dfloat128)
10886 error_at (loc,
10887 ("both %<_Sat%> and %<_Decimal128%> in "
10888 "declaration specifiers"));
10889 else if (specs->complex_p)
10890 error_at (loc,
10891 ("both %<_Sat%> and %<complex%> in "
10892 "declaration specifiers"));
10893 else
10895 specs->saturating_p = true;
10896 specs->locations[cdw_saturating] = loc;
10898 break;
10899 default:
10900 gcc_unreachable ();
10903 if (dupe)
10904 error_at (loc, "duplicate %qE", type);
10906 return specs;
10908 else
10910 /* "void", "_Bool", "char", "int", "float", "double",
10911 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10912 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10913 "__auto_type". */
10914 if (specs->typespec_word != cts_none)
10916 error_at (loc,
10917 "two or more data types in declaration specifiers");
10918 return specs;
10920 switch (i)
10922 case RID_AUTO_TYPE:
10923 if (specs->long_p)
10924 error_at (loc,
10925 ("both %<long%> and %<__auto_type%> in "
10926 "declaration specifiers"));
10927 else if (specs->short_p)
10928 error_at (loc,
10929 ("both %<short%> and %<__auto_type%> in "
10930 "declaration specifiers"));
10931 else if (specs->signed_p)
10932 error_at (loc,
10933 ("both %<signed%> and %<__auto_type%> in "
10934 "declaration specifiers"));
10935 else if (specs->unsigned_p)
10936 error_at (loc,
10937 ("both %<unsigned%> and %<__auto_type%> in "
10938 "declaration specifiers"));
10939 else if (specs->complex_p)
10940 error_at (loc,
10941 ("both %<complex%> and %<__auto_type%> in "
10942 "declaration specifiers"));
10943 else if (specs->saturating_p)
10944 error_at (loc,
10945 ("both %<_Sat%> and %<__auto_type%> in "
10946 "declaration specifiers"));
10947 else
10949 specs->typespec_word = cts_auto_type;
10950 specs->locations[cdw_typespec] = loc;
10952 return specs;
10953 case RID_INT_N_0:
10954 case RID_INT_N_1:
10955 case RID_INT_N_2:
10956 case RID_INT_N_3:
10957 specs->int_n_idx = i - RID_INT_N_0;
10958 if (!in_system_header_at (input_location)
10959 /* If the INT_N type ends in "__", and so is of the format
10960 "__intN__", don't pedwarn. */
10961 && (strncmp (IDENTIFIER_POINTER (type)
10962 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
10963 pedwarn (loc, OPT_Wpedantic,
10964 "ISO C does not support %<__int%d%> types",
10965 int_n_data[specs->int_n_idx].bitsize);
10967 if (specs->long_p)
10968 error_at (loc,
10969 ("both %<__int%d%> and %<long%> in "
10970 "declaration specifiers"),
10971 int_n_data[specs->int_n_idx].bitsize);
10972 else if (specs->saturating_p)
10973 error_at (loc,
10974 ("both %<_Sat%> and %<__int%d%> in "
10975 "declaration specifiers"),
10976 int_n_data[specs->int_n_idx].bitsize);
10977 else if (specs->short_p)
10978 error_at (loc,
10979 ("both %<__int%d%> and %<short%> in "
10980 "declaration specifiers"),
10981 int_n_data[specs->int_n_idx].bitsize);
10982 else if (! int_n_enabled_p[specs->int_n_idx])
10984 specs->typespec_word = cts_int_n;
10985 error_at (loc,
10986 "%<__int%d%> is not supported on this target",
10987 int_n_data[specs->int_n_idx].bitsize);
10989 else
10991 specs->typespec_word = cts_int_n;
10992 specs->locations[cdw_typespec] = loc;
10994 return specs;
10995 case RID_VOID:
10996 if (specs->long_p)
10997 error_at (loc,
10998 ("both %<long%> and %<void%> in "
10999 "declaration specifiers"));
11000 else if (specs->short_p)
11001 error_at (loc,
11002 ("both %<short%> and %<void%> in "
11003 "declaration specifiers"));
11004 else if (specs->signed_p)
11005 error_at (loc,
11006 ("both %<signed%> and %<void%> in "
11007 "declaration specifiers"));
11008 else if (specs->unsigned_p)
11009 error_at (loc,
11010 ("both %<unsigned%> and %<void%> in "
11011 "declaration specifiers"));
11012 else if (specs->complex_p)
11013 error_at (loc,
11014 ("both %<complex%> and %<void%> in "
11015 "declaration specifiers"));
11016 else if (specs->saturating_p)
11017 error_at (loc,
11018 ("both %<_Sat%> and %<void%> in "
11019 "declaration specifiers"));
11020 else
11022 specs->typespec_word = cts_void;
11023 specs->locations[cdw_typespec] = loc;
11025 return specs;
11026 case RID_BOOL:
11027 if (!in_system_header_at (loc))
11028 pedwarn_c90 (loc, OPT_Wpedantic,
11029 "ISO C90 does not support boolean types");
11030 if (specs->long_p)
11031 error_at (loc,
11032 ("both %<long%> and %<_Bool%> in "
11033 "declaration specifiers"));
11034 else if (specs->short_p)
11035 error_at (loc,
11036 ("both %<short%> and %<_Bool%> in "
11037 "declaration specifiers"));
11038 else if (specs->signed_p)
11039 error_at (loc,
11040 ("both %<signed%> and %<_Bool%> in "
11041 "declaration specifiers"));
11042 else if (specs->unsigned_p)
11043 error_at (loc,
11044 ("both %<unsigned%> and %<_Bool%> in "
11045 "declaration specifiers"));
11046 else if (specs->complex_p)
11047 error_at (loc,
11048 ("both %<complex%> and %<_Bool%> in "
11049 "declaration specifiers"));
11050 else if (specs->saturating_p)
11051 error_at (loc,
11052 ("both %<_Sat%> and %<_Bool%> in "
11053 "declaration specifiers"));
11054 else
11056 specs->typespec_word = cts_bool;
11057 specs->locations[cdw_typespec] = loc;
11059 return specs;
11060 case RID_CHAR:
11061 if (specs->long_p)
11062 error_at (loc,
11063 ("both %<long%> and %<char%> in "
11064 "declaration specifiers"));
11065 else if (specs->short_p)
11066 error_at (loc,
11067 ("both %<short%> and %<char%> in "
11068 "declaration specifiers"));
11069 else if (specs->saturating_p)
11070 error_at (loc,
11071 ("both %<_Sat%> and %<char%> in "
11072 "declaration specifiers"));
11073 else
11075 specs->typespec_word = cts_char;
11076 specs->locations[cdw_typespec] = loc;
11078 return specs;
11079 case RID_INT:
11080 if (specs->saturating_p)
11081 error_at (loc,
11082 ("both %<_Sat%> and %<int%> in "
11083 "declaration specifiers"));
11084 else
11086 specs->typespec_word = cts_int;
11087 specs->locations[cdw_typespec] = loc;
11089 return specs;
11090 case RID_FLOAT:
11091 if (specs->long_p)
11092 error_at (loc,
11093 ("both %<long%> and %<float%> in "
11094 "declaration specifiers"));
11095 else if (specs->short_p)
11096 error_at (loc,
11097 ("both %<short%> and %<float%> in "
11098 "declaration specifiers"));
11099 else if (specs->signed_p)
11100 error_at (loc,
11101 ("both %<signed%> and %<float%> in "
11102 "declaration specifiers"));
11103 else if (specs->unsigned_p)
11104 error_at (loc,
11105 ("both %<unsigned%> and %<float%> in "
11106 "declaration specifiers"));
11107 else if (specs->saturating_p)
11108 error_at (loc,
11109 ("both %<_Sat%> and %<float%> in "
11110 "declaration specifiers"));
11111 else
11113 specs->typespec_word = cts_float;
11114 specs->locations[cdw_typespec] = loc;
11116 return specs;
11117 case RID_DOUBLE:
11118 if (specs->long_long_p)
11119 error_at (loc,
11120 ("both %<long long%> and %<double%> in "
11121 "declaration specifiers"));
11122 else if (specs->short_p)
11123 error_at (loc,
11124 ("both %<short%> and %<double%> in "
11125 "declaration specifiers"));
11126 else if (specs->signed_p)
11127 error_at (loc,
11128 ("both %<signed%> and %<double%> in "
11129 "declaration specifiers"));
11130 else if (specs->unsigned_p)
11131 error_at (loc,
11132 ("both %<unsigned%> and %<double%> in "
11133 "declaration specifiers"));
11134 else if (specs->saturating_p)
11135 error_at (loc,
11136 ("both %<_Sat%> and %<double%> in "
11137 "declaration specifiers"));
11138 else
11140 specs->typespec_word = cts_double;
11141 specs->locations[cdw_typespec] = loc;
11143 return specs;
11144 CASE_RID_FLOATN_NX:
11145 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11146 if (!in_system_header_at (input_location))
11147 pedwarn (loc, OPT_Wpedantic,
11148 "ISO C does not support the %<_Float%d%s%> type",
11149 floatn_nx_types[specs->floatn_nx_idx].n,
11150 (floatn_nx_types[specs->floatn_nx_idx].extended
11151 ? "x"
11152 : ""));
11154 if (specs->long_p)
11155 error_at (loc,
11156 ("both %<long%> and %<_Float%d%s%> in "
11157 "declaration specifiers"),
11158 floatn_nx_types[specs->floatn_nx_idx].n,
11159 (floatn_nx_types[specs->floatn_nx_idx].extended
11160 ? "x"
11161 : ""));
11162 else if (specs->short_p)
11163 error_at (loc,
11164 ("both %<short%> and %<_Float%d%s%> in "
11165 "declaration specifiers"),
11166 floatn_nx_types[specs->floatn_nx_idx].n,
11167 (floatn_nx_types[specs->floatn_nx_idx].extended
11168 ? "x"
11169 : ""));
11170 else if (specs->signed_p)
11171 error_at (loc,
11172 ("both %<signed%> and %<_Float%d%s%> in "
11173 "declaration specifiers"),
11174 floatn_nx_types[specs->floatn_nx_idx].n,
11175 (floatn_nx_types[specs->floatn_nx_idx].extended
11176 ? "x"
11177 : ""));
11178 else if (specs->unsigned_p)
11179 error_at (loc,
11180 ("both %<unsigned%> and %<_Float%d%s%> in "
11181 "declaration specifiers"),
11182 floatn_nx_types[specs->floatn_nx_idx].n,
11183 (floatn_nx_types[specs->floatn_nx_idx].extended
11184 ? "x"
11185 : ""));
11186 else if (specs->saturating_p)
11187 error_at (loc,
11188 ("both %<_Sat%> and %<_Float%d%s%> in "
11189 "declaration specifiers"),
11190 floatn_nx_types[specs->floatn_nx_idx].n,
11191 (floatn_nx_types[specs->floatn_nx_idx].extended
11192 ? "x"
11193 : ""));
11194 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11196 specs->typespec_word = cts_floatn_nx;
11197 error_at (loc,
11198 "%<_Float%d%s%> is not supported on this target",
11199 floatn_nx_types[specs->floatn_nx_idx].n,
11200 (floatn_nx_types[specs->floatn_nx_idx].extended
11201 ? "x"
11202 : ""));
11204 else
11206 specs->typespec_word = cts_floatn_nx;
11207 specs->locations[cdw_typespec] = loc;
11209 return specs;
11210 case RID_DFLOAT32:
11211 case RID_DFLOAT64:
11212 case RID_DFLOAT128:
11214 const char *str;
11215 if (i == RID_DFLOAT32)
11216 str = "_Decimal32";
11217 else if (i == RID_DFLOAT64)
11218 str = "_Decimal64";
11219 else
11220 str = "_Decimal128";
11221 if (specs->long_long_p)
11222 error_at (loc,
11223 ("both %<long long%> and %qs in "
11224 "declaration specifiers"),
11225 str);
11226 if (specs->long_p)
11227 error_at (loc,
11228 ("both %<long%> and %qs in "
11229 "declaration specifiers"),
11230 str);
11231 else if (specs->short_p)
11232 error_at (loc,
11233 ("both %<short%> and %qs in "
11234 "declaration specifiers"),
11235 str);
11236 else if (specs->signed_p)
11237 error_at (loc,
11238 ("both %<signed%> and %qs in "
11239 "declaration specifiers"),
11240 str);
11241 else if (specs->unsigned_p)
11242 error_at (loc,
11243 ("both %<unsigned%> and %qs in "
11244 "declaration specifiers"),
11245 str);
11246 else if (specs->complex_p)
11247 error_at (loc,
11248 ("both %<complex%> and %qs in "
11249 "declaration specifiers"),
11250 str);
11251 else if (specs->saturating_p)
11252 error_at (loc,
11253 ("both %<_Sat%> and %qs in "
11254 "declaration specifiers"),
11255 str);
11256 else if (i == RID_DFLOAT32)
11257 specs->typespec_word = cts_dfloat32;
11258 else if (i == RID_DFLOAT64)
11259 specs->typespec_word = cts_dfloat64;
11260 else
11261 specs->typespec_word = cts_dfloat128;
11262 specs->locations[cdw_typespec] = loc;
11264 if (!targetm.decimal_float_supported_p ())
11265 error_at (loc,
11266 ("decimal floating-point not supported "
11267 "for this target"));
11268 pedwarn_c11 (loc, OPT_Wpedantic,
11269 "ISO C does not support decimal floating-point "
11270 "before C2X");
11271 return specs;
11272 case RID_FRACT:
11273 case RID_ACCUM:
11275 const char *str;
11276 if (i == RID_FRACT)
11277 str = "_Fract";
11278 else
11279 str = "_Accum";
11280 if (specs->complex_p)
11281 error_at (loc,
11282 ("both %<complex%> and %qs in "
11283 "declaration specifiers"),
11284 str);
11285 else if (i == RID_FRACT)
11286 specs->typespec_word = cts_fract;
11287 else
11288 specs->typespec_word = cts_accum;
11289 specs->locations[cdw_typespec] = loc;
11291 if (!targetm.fixed_point_supported_p ())
11292 error_at (loc,
11293 "fixed-point types not supported for this target");
11294 pedwarn (loc, OPT_Wpedantic,
11295 "ISO C does not support fixed-point types");
11296 return specs;
11297 default:
11298 /* ObjC reserved word "id", handled below. */
11299 break;
11304 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11305 form of ObjC type, cases such as "int" and "long" being handled
11306 above), a TYPE (struct, union, enum and typeof specifiers) or an
11307 ERROR_MARK. In none of these cases may there have previously
11308 been any type specifiers. */
11309 if (specs->type || specs->typespec_word != cts_none
11310 || specs->long_p || specs->short_p || specs->signed_p
11311 || specs->unsigned_p || specs->complex_p)
11312 error_at (loc, "two or more data types in declaration specifiers");
11313 else if (TREE_CODE (type) == TYPE_DECL)
11315 if (TREE_TYPE (type) == error_mark_node)
11316 ; /* Allow the type to default to int to avoid cascading errors. */
11317 else
11319 specs->type = TREE_TYPE (type);
11320 specs->decl_attr = DECL_ATTRIBUTES (type);
11321 specs->typedef_p = true;
11322 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11323 specs->locations[cdw_typedef] = loc;
11325 /* If this typedef name is defined in a struct, then a C++
11326 lookup would return a different value. */
11327 if (warn_cxx_compat
11328 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11329 warning_at (loc, OPT_Wc___compat,
11330 "C++ lookup of %qD would return a field, not a type",
11331 type);
11333 /* If we are parsing a struct, record that a struct field
11334 used a typedef. */
11335 if (warn_cxx_compat && struct_parse_info != NULL)
11336 struct_parse_info->typedefs_seen.safe_push (type);
11339 else if (TREE_CODE (type) == IDENTIFIER_NODE)
11341 tree t = lookup_name (type);
11342 if (!t || TREE_CODE (t) != TYPE_DECL)
11343 error_at (loc, "%qE fails to be a typedef or built in type", type);
11344 else if (TREE_TYPE (t) == error_mark_node)
11346 else
11348 specs->type = TREE_TYPE (t);
11349 specs->locations[cdw_typespec] = loc;
11352 else
11354 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11356 specs->typedef_p = true;
11357 specs->locations[cdw_typedef] = loc;
11358 if (spec.expr)
11360 if (specs->expr)
11361 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11362 specs->expr, spec.expr);
11363 else
11364 specs->expr = spec.expr;
11365 specs->expr_const_operands &= spec.expr_const_operands;
11368 specs->type = type;
11371 return specs;
11374 /* Add the storage class specifier or function specifier SCSPEC to the
11375 declaration specifiers SPECS, returning SPECS. */
11377 struct c_declspecs *
11378 declspecs_add_scspec (location_t loc,
11379 struct c_declspecs *specs,
11380 tree scspec)
11382 enum rid i;
11383 enum c_storage_class n = csc_none;
11384 bool dupe = false;
11385 specs->declspecs_seen_p = true;
11386 specs->non_std_attrs_seen_p = true;
11387 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11388 && C_IS_RESERVED_WORD (scspec));
11389 i = C_RID_CODE (scspec);
11390 if (specs->non_sc_seen_p)
11391 warning (OPT_Wold_style_declaration,
11392 "%qE is not at beginning of declaration", scspec);
11393 switch (i)
11395 case RID_INLINE:
11396 /* C99 permits duplicate inline. Although of doubtful utility,
11397 it seems simplest to permit it in gnu89 mode as well, as
11398 there is also little utility in maintaining this as a
11399 difference between gnu89 and C99 inline. */
11400 dupe = false;
11401 specs->inline_p = true;
11402 specs->locations[cdw_inline] = loc;
11403 break;
11404 case RID_NORETURN:
11405 /* Duplicate _Noreturn is permitted. */
11406 dupe = false;
11407 specs->noreturn_p = true;
11408 specs->locations[cdw_noreturn] = loc;
11409 break;
11410 case RID_THREAD:
11411 dupe = specs->thread_p;
11412 if (specs->storage_class == csc_auto)
11413 error ("%qE used with %<auto%>", scspec);
11414 else if (specs->storage_class == csc_register)
11415 error ("%qE used with %<register%>", scspec);
11416 else if (specs->storage_class == csc_typedef)
11417 error ("%qE used with %<typedef%>", scspec);
11418 else
11420 specs->thread_p = true;
11421 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11422 "__thread") == 0);
11423 /* A diagnostic is not required for the use of this
11424 identifier in the implementation namespace; only diagnose
11425 it for the C11 spelling because of existing code using
11426 the other spelling. */
11427 if (!specs->thread_gnu_p)
11429 if (flag_isoc99)
11430 pedwarn_c99 (loc, OPT_Wpedantic,
11431 "ISO C99 does not support %qE", scspec);
11432 else
11433 pedwarn_c99 (loc, OPT_Wpedantic,
11434 "ISO C90 does not support %qE", scspec);
11436 specs->locations[cdw_thread] = loc;
11438 break;
11439 case RID_AUTO:
11440 n = csc_auto;
11441 break;
11442 case RID_EXTERN:
11443 n = csc_extern;
11444 /* Diagnose "__thread extern". */
11445 if (specs->thread_p && specs->thread_gnu_p)
11446 error ("%<__thread%> before %<extern%>");
11447 break;
11448 case RID_REGISTER:
11449 n = csc_register;
11450 break;
11451 case RID_STATIC:
11452 n = csc_static;
11453 /* Diagnose "__thread static". */
11454 if (specs->thread_p && specs->thread_gnu_p)
11455 error ("%<__thread%> before %<static%>");
11456 break;
11457 case RID_TYPEDEF:
11458 n = csc_typedef;
11459 break;
11460 default:
11461 gcc_unreachable ();
11463 if (n != csc_none && n == specs->storage_class)
11464 dupe = true;
11465 if (dupe)
11467 if (i == RID_THREAD)
11468 error ("duplicate %<_Thread_local%> or %<__thread%>");
11469 else
11470 error ("duplicate %qE", scspec);
11472 if (n != csc_none)
11474 if (specs->storage_class != csc_none && n != specs->storage_class)
11476 error ("multiple storage classes in declaration specifiers");
11478 else
11480 specs->storage_class = n;
11481 specs->locations[cdw_storage_class] = loc;
11482 if (n != csc_extern && n != csc_static && specs->thread_p)
11484 error ("%qs used with %qE",
11485 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11486 scspec);
11487 specs->thread_p = false;
11491 return specs;
11494 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11495 returning SPECS. */
11497 struct c_declspecs *
11498 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11500 specs->attrs = chainon (attrs, specs->attrs);
11501 specs->locations[cdw_attributes] = loc;
11502 specs->declspecs_seen_p = true;
11503 /* In the case of standard attributes at the start of the
11504 declaration, the caller will reset this. */
11505 specs->non_std_attrs_seen_p = true;
11506 return specs;
11509 /* Add an _Alignas specifier (expression ALIGN, or type whose
11510 alignment is ALIGN) to the declaration specifiers SPECS, returning
11511 SPECS. */
11512 struct c_declspecs *
11513 declspecs_add_alignas (location_t loc,
11514 struct c_declspecs *specs, tree align)
11516 specs->alignas_p = true;
11517 specs->locations[cdw_alignas] = loc;
11518 if (align == error_mark_node)
11519 return specs;
11521 /* Only accept the alignment if it's valid and greater than
11522 the current one. Zero is invalid but by C11 required to
11523 be silently ignored. */
11524 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11525 if (align_log > specs->align_log)
11526 specs->align_log = align_log;
11527 return specs;
11530 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11531 specifiers with any other type specifier to determine the resulting
11532 type. This is where ISO C checks on complex types are made, since
11533 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11534 double". Also apply postfix standard attributes to modify the type. */
11536 struct c_declspecs *
11537 finish_declspecs (struct c_declspecs *specs)
11539 /* If a type was specified as a whole, we have no modifiers and are
11540 done. */
11541 if (specs->type != NULL_TREE)
11543 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11544 && !specs->signed_p && !specs->unsigned_p
11545 && !specs->complex_p);
11547 /* Set a dummy type. */
11548 if (TREE_CODE (specs->type) == ERROR_MARK)
11549 specs->type = integer_type_node;
11550 goto handle_postfix_attrs;
11553 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11554 has been specified, treat it as "int" unless "_Complex" is
11555 present and there are no other specifiers. If we just have
11556 "_Complex", it is equivalent to "_Complex double", but e.g.
11557 "_Complex short" is equivalent to "_Complex short int". */
11558 if (specs->typespec_word == cts_none)
11560 if (specs->saturating_p)
11562 error_at (specs->locations[cdw_saturating],
11563 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11564 if (!targetm.fixed_point_supported_p ())
11565 error_at (specs->locations[cdw_saturating],
11566 "fixed-point types not supported for this target");
11567 specs->typespec_word = cts_fract;
11569 else if (specs->long_p || specs->short_p
11570 || specs->signed_p || specs->unsigned_p)
11572 specs->typespec_word = cts_int;
11574 else if (specs->complex_p)
11576 specs->typespec_word = cts_double;
11577 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11578 "ISO C does not support plain %<complex%> meaning "
11579 "%<double complex%>");
11581 else
11583 specs->typespec_word = cts_int;
11584 specs->default_int_p = true;
11585 /* We don't diagnose this here because grokdeclarator will
11586 give more specific diagnostics according to whether it is
11587 a function definition. */
11591 /* If "signed" was specified, record this to distinguish "int" and
11592 "signed int" in the case of a bit-field with
11593 -funsigned-bitfields. */
11594 specs->explicit_signed_p = specs->signed_p;
11596 /* Now compute the actual type. */
11597 switch (specs->typespec_word)
11599 case cts_auto_type:
11600 gcc_assert (!specs->long_p && !specs->short_p
11601 && !specs->signed_p && !specs->unsigned_p
11602 && !specs->complex_p);
11603 /* Type to be filled in later. */
11604 if (specs->postfix_attrs)
11605 error ("%<__auto_type%> followed by %<[[]]%> attributes");
11606 break;
11607 case cts_void:
11608 gcc_assert (!specs->long_p && !specs->short_p
11609 && !specs->signed_p && !specs->unsigned_p
11610 && !specs->complex_p);
11611 specs->type = void_type_node;
11612 break;
11613 case cts_bool:
11614 gcc_assert (!specs->long_p && !specs->short_p
11615 && !specs->signed_p && !specs->unsigned_p
11616 && !specs->complex_p);
11617 specs->type = boolean_type_node;
11618 break;
11619 case cts_char:
11620 gcc_assert (!specs->long_p && !specs->short_p);
11621 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11622 if (specs->signed_p)
11623 specs->type = signed_char_type_node;
11624 else if (specs->unsigned_p)
11625 specs->type = unsigned_char_type_node;
11626 else
11627 specs->type = char_type_node;
11628 if (specs->complex_p)
11630 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11631 "ISO C does not support complex integer types");
11632 specs->type = build_complex_type (specs->type);
11634 break;
11635 case cts_int_n:
11636 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11637 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11638 if (! int_n_enabled_p[specs->int_n_idx])
11639 specs->type = integer_type_node;
11640 else
11641 specs->type = (specs->unsigned_p
11642 ? int_n_trees[specs->int_n_idx].unsigned_type
11643 : int_n_trees[specs->int_n_idx].signed_type);
11644 if (specs->complex_p)
11646 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11647 "ISO C does not support complex integer types");
11648 specs->type = build_complex_type (specs->type);
11650 break;
11651 case cts_int:
11652 gcc_assert (!(specs->long_p && specs->short_p));
11653 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11654 if (specs->long_long_p)
11655 specs->type = (specs->unsigned_p
11656 ? long_long_unsigned_type_node
11657 : long_long_integer_type_node);
11658 else if (specs->long_p)
11659 specs->type = (specs->unsigned_p
11660 ? long_unsigned_type_node
11661 : long_integer_type_node);
11662 else if (specs->short_p)
11663 specs->type = (specs->unsigned_p
11664 ? short_unsigned_type_node
11665 : short_integer_type_node);
11666 else
11667 specs->type = (specs->unsigned_p
11668 ? unsigned_type_node
11669 : integer_type_node);
11670 if (specs->complex_p)
11672 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11673 "ISO C does not support complex integer types");
11674 specs->type = build_complex_type (specs->type);
11676 break;
11677 case cts_float:
11678 gcc_assert (!specs->long_p && !specs->short_p
11679 && !specs->signed_p && !specs->unsigned_p);
11680 specs->type = (specs->complex_p
11681 ? complex_float_type_node
11682 : float_type_node);
11683 break;
11684 case cts_double:
11685 gcc_assert (!specs->long_long_p && !specs->short_p
11686 && !specs->signed_p && !specs->unsigned_p);
11687 if (specs->long_p)
11689 specs->type = (specs->complex_p
11690 ? complex_long_double_type_node
11691 : long_double_type_node);
11693 else
11695 specs->type = (specs->complex_p
11696 ? complex_double_type_node
11697 : double_type_node);
11699 break;
11700 case cts_floatn_nx:
11701 gcc_assert (!specs->long_p && !specs->short_p
11702 && !specs->signed_p && !specs->unsigned_p);
11703 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11704 specs->type = integer_type_node;
11705 else if (specs->complex_p)
11706 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11707 else
11708 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11709 break;
11710 case cts_dfloat32:
11711 case cts_dfloat64:
11712 case cts_dfloat128:
11713 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11714 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11715 if (!targetm.decimal_float_supported_p ())
11716 specs->type = integer_type_node;
11717 else if (specs->typespec_word == cts_dfloat32)
11718 specs->type = dfloat32_type_node;
11719 else if (specs->typespec_word == cts_dfloat64)
11720 specs->type = dfloat64_type_node;
11721 else
11722 specs->type = dfloat128_type_node;
11723 break;
11724 case cts_fract:
11725 gcc_assert (!specs->complex_p);
11726 if (!targetm.fixed_point_supported_p ())
11727 specs->type = integer_type_node;
11728 else if (specs->saturating_p)
11730 if (specs->long_long_p)
11731 specs->type = specs->unsigned_p
11732 ? sat_unsigned_long_long_fract_type_node
11733 : sat_long_long_fract_type_node;
11734 else if (specs->long_p)
11735 specs->type = specs->unsigned_p
11736 ? sat_unsigned_long_fract_type_node
11737 : sat_long_fract_type_node;
11738 else if (specs->short_p)
11739 specs->type = specs->unsigned_p
11740 ? sat_unsigned_short_fract_type_node
11741 : sat_short_fract_type_node;
11742 else
11743 specs->type = specs->unsigned_p
11744 ? sat_unsigned_fract_type_node
11745 : sat_fract_type_node;
11747 else
11749 if (specs->long_long_p)
11750 specs->type = specs->unsigned_p
11751 ? unsigned_long_long_fract_type_node
11752 : long_long_fract_type_node;
11753 else if (specs->long_p)
11754 specs->type = specs->unsigned_p
11755 ? unsigned_long_fract_type_node
11756 : long_fract_type_node;
11757 else if (specs->short_p)
11758 specs->type = specs->unsigned_p
11759 ? unsigned_short_fract_type_node
11760 : short_fract_type_node;
11761 else
11762 specs->type = specs->unsigned_p
11763 ? unsigned_fract_type_node
11764 : fract_type_node;
11766 break;
11767 case cts_accum:
11768 gcc_assert (!specs->complex_p);
11769 if (!targetm.fixed_point_supported_p ())
11770 specs->type = integer_type_node;
11771 else if (specs->saturating_p)
11773 if (specs->long_long_p)
11774 specs->type = specs->unsigned_p
11775 ? sat_unsigned_long_long_accum_type_node
11776 : sat_long_long_accum_type_node;
11777 else if (specs->long_p)
11778 specs->type = specs->unsigned_p
11779 ? sat_unsigned_long_accum_type_node
11780 : sat_long_accum_type_node;
11781 else if (specs->short_p)
11782 specs->type = specs->unsigned_p
11783 ? sat_unsigned_short_accum_type_node
11784 : sat_short_accum_type_node;
11785 else
11786 specs->type = specs->unsigned_p
11787 ? sat_unsigned_accum_type_node
11788 : sat_accum_type_node;
11790 else
11792 if (specs->long_long_p)
11793 specs->type = specs->unsigned_p
11794 ? unsigned_long_long_accum_type_node
11795 : long_long_accum_type_node;
11796 else if (specs->long_p)
11797 specs->type = specs->unsigned_p
11798 ? unsigned_long_accum_type_node
11799 : long_accum_type_node;
11800 else if (specs->short_p)
11801 specs->type = specs->unsigned_p
11802 ? unsigned_short_accum_type_node
11803 : short_accum_type_node;
11804 else
11805 specs->type = specs->unsigned_p
11806 ? unsigned_accum_type_node
11807 : accum_type_node;
11809 break;
11810 default:
11811 gcc_unreachable ();
11813 handle_postfix_attrs:
11814 if (specs->type != NULL)
11816 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
11817 decl_attributes (&specs->type, specs->postfix_attrs, 0);
11818 specs->postfix_attrs = NULL_TREE;
11821 return specs;
11824 /* Perform final processing on one file scope's declarations (or the
11825 external scope's declarations), GLOBALS. */
11827 static void
11828 c_write_global_declarations_1 (tree globals)
11830 tree decl;
11831 bool reconsider;
11833 /* Process the decls in the order they were written. */
11834 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11836 /* Check for used but undefined static functions using the C
11837 standard's definition of "used", and set TREE_NO_WARNING so
11838 that check_global_declaration doesn't repeat the check. */
11839 if (TREE_CODE (decl) == FUNCTION_DECL
11840 && DECL_INITIAL (decl) == NULL_TREE
11841 && DECL_EXTERNAL (decl)
11842 && !TREE_PUBLIC (decl))
11844 if (C_DECL_USED (decl))
11846 if (pedwarn (input_location, 0, "%q+F used but never defined",
11847 decl))
11848 TREE_NO_WARNING (decl) = 1;
11850 /* For -Wunused-function warn about unused static prototypes. */
11851 else if (warn_unused_function
11852 && ! DECL_ARTIFICIAL (decl)
11853 && ! TREE_NO_WARNING (decl))
11855 if (warning (OPT_Wunused_function,
11856 "%q+F declared %<static%> but never defined",
11857 decl))
11858 TREE_NO_WARNING (decl) = 1;
11862 wrapup_global_declaration_1 (decl);
11867 reconsider = false;
11868 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11869 reconsider |= wrapup_global_declaration_2 (decl);
11871 while (reconsider);
11874 /* Preserve the external declarations scope across a garbage collect. */
11875 static GTY(()) tree ext_block;
11877 /* Collect all references relevant to SOURCE_FILE. */
11879 static void
11880 collect_all_refs (const char *source_file)
11882 tree t;
11883 unsigned i;
11885 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11886 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11888 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11891 /* Collect source file references at global level. */
11893 static void
11894 collect_source_refs (void)
11896 tree t;
11897 tree decls;
11898 tree decl;
11899 unsigned i;
11901 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11903 decls = DECL_INITIAL (t);
11904 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11905 if (!DECL_IS_BUILTIN (decl))
11906 collect_source_ref (DECL_SOURCE_FILE (decl));
11909 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11910 if (!DECL_IS_BUILTIN (decl))
11911 collect_source_ref (DECL_SOURCE_FILE (decl));
11914 /* Perform any final parser cleanups and generate initial debugging
11915 information. */
11917 void
11918 c_parse_final_cleanups (void)
11920 tree t;
11921 unsigned i;
11923 /* We don't want to do this if generating a PCH. */
11924 if (pch_file)
11925 return;
11927 timevar_stop (TV_PHASE_PARSING);
11928 timevar_start (TV_PHASE_DEFERRED);
11930 /* Do the Objective-C stuff. This is where all the Objective-C
11931 module stuff gets generated (symtab, class/protocol/selector
11932 lists etc). */
11933 if (c_dialect_objc ())
11934 objc_write_global_declarations ();
11936 /* Close the external scope. */
11937 ext_block = pop_scope ();
11938 external_scope = 0;
11939 gcc_assert (!current_scope);
11941 /* Handle -fdump-ada-spec[-slim]. */
11942 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11944 /* Build a table of files to generate specs for */
11945 collect_source_ref (main_input_filename);
11946 if (!flag_dump_ada_spec_slim)
11947 collect_source_refs ();
11949 dump_ada_specs (collect_all_refs, NULL);
11952 /* Process all file scopes in this compilation, and the external_scope,
11953 through wrapup_global_declarations. */
11954 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11955 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11956 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11958 timevar_stop (TV_PHASE_DEFERRED);
11959 timevar_start (TV_PHASE_PARSING);
11961 ext_block = NULL;
11964 /* Register reserved keyword WORD as qualifier for address space AS. */
11966 void
11967 c_register_addr_space (const char *word, addr_space_t as)
11969 int rid = RID_FIRST_ADDR_SPACE + as;
11970 tree id;
11972 /* Address space qualifiers are only supported
11973 in C with GNU extensions enabled. */
11974 if (c_dialect_objc () || flag_no_asm)
11975 return;
11977 id = get_identifier (word);
11978 C_SET_RID_CODE (id, rid);
11979 C_IS_RESERVED_WORD (id) = 1;
11980 ridpointers [rid] = id;
11983 /* Return identifier to look up for omp declare reduction. */
11985 tree
11986 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11988 const char *p = NULL;
11989 switch (reduction_code)
11991 case PLUS_EXPR: p = "+"; break;
11992 case MULT_EXPR: p = "*"; break;
11993 case MINUS_EXPR: p = "-"; break;
11994 case BIT_AND_EXPR: p = "&"; break;
11995 case BIT_XOR_EXPR: p = "^"; break;
11996 case BIT_IOR_EXPR: p = "|"; break;
11997 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11998 case TRUTH_ORIF_EXPR: p = "||"; break;
11999 case MIN_EXPR: p = "min"; break;
12000 case MAX_EXPR: p = "max"; break;
12001 default:
12002 break;
12005 if (p == NULL)
12007 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
12008 return error_mark_node;
12009 p = IDENTIFIER_POINTER (reduction_id);
12012 const char prefix[] = "omp declare reduction ";
12013 size_t lenp = sizeof (prefix);
12014 size_t len = strlen (p);
12015 char *name = XALLOCAVEC (char, lenp + len);
12016 memcpy (name, prefix, lenp - 1);
12017 memcpy (name + lenp - 1, p, len + 1);
12018 return get_identifier (name);
12021 /* Lookup REDUCTION_ID in the current scope, or create an artificial
12022 VAR_DECL, bind it into the current scope and return it. */
12024 tree
12025 c_omp_reduction_decl (tree reduction_id)
12027 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12028 if (b != NULL && B_IN_CURRENT_SCOPE (b))
12029 return b->decl;
12031 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
12032 reduction_id, integer_type_node);
12033 DECL_ARTIFICIAL (decl) = 1;
12034 DECL_EXTERNAL (decl) = 1;
12035 TREE_STATIC (decl) = 1;
12036 TREE_PUBLIC (decl) = 0;
12037 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
12038 return decl;
12041 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
12043 tree
12044 c_omp_reduction_lookup (tree reduction_id, tree type)
12046 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12047 while (b)
12049 tree t;
12050 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
12051 if (comptypes (TREE_PURPOSE (t), type))
12052 return TREE_VALUE (t);
12053 b = b->shadowed;
12055 return error_mark_node;
12058 /* Helper function called via walk_tree, to diagnose invalid
12059 #pragma omp declare reduction combiners or initializers. */
12061 tree
12062 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
12064 tree *vars = (tree *) data;
12065 if (SSA_VAR_P (*tp)
12066 && !DECL_ARTIFICIAL (*tp)
12067 && *tp != vars[0]
12068 && *tp != vars[1])
12070 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
12071 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
12072 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
12073 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
12074 *tp);
12075 else
12076 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
12077 "to variable %qD which is not %<omp_priv%> nor "
12078 "%<omp_orig%>",
12079 *tp);
12080 return *tp;
12082 return NULL_TREE;
12086 bool
12087 c_check_in_current_scope (tree decl)
12089 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
12090 return b != NULL && B_IN_CURRENT_SCOPE (b);
12093 #include "gt-c-c-decl.h"