Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / gcc / c / c-decl.cc
blobe537d33f3986083c039f671bcf5eb4c6f0408300
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2023 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_STRING
29 #define INCLUDE_MEMORY
30 #include "system.h"
31 #include "coretypes.h"
32 #include "target.h"
33 #include "function.h"
34 #include "c-tree.h"
35 #include "timevar.h"
36 #include "stringpool.h"
37 #include "cgraph.h"
38 #include "intl.h"
39 #include "print-tree.h"
40 #include "stor-layout.h"
41 #include "varasm.h"
42 #include "attribs.h"
43 #include "toplev.h"
44 #include "debug.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "c-family/c-ubsan.h"
48 #include "c-lang.h"
49 #include "langhooks.h"
50 #include "tree-iterator.h"
51 #include "dumpfile.h"
52 #include "plugin.h"
53 #include "c-family/c-ada-spec.h"
54 #include "builtins.h"
55 #include "spellcheck-tree.h"
56 #include "gcc-rich-location.h"
57 #include "asan.h"
58 #include "c-family/name-hint.h"
59 #include "c-family/known-headers.h"
60 #include "c-family/c-spellcheck.h"
61 #include "context.h" /* For 'g'. */
62 #include "omp-general.h"
63 #include "omp-offload.h" /* For offload_vars. */
65 #include "tree-pretty-print.h"
67 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
68 enum decl_context
69 { NORMAL, /* Ordinary declaration */
70 FUNCDEF, /* Function definition */
71 PARM, /* Declaration of parm before function body */
72 FIELD, /* Declaration inside struct or union */
73 TYPENAME}; /* Typename (inside cast or sizeof) */
75 /* States indicating how grokdeclarator() should handle declspecs marked
76 with __attribute__((deprecated)) or __attribute__((unavailable)).
77 An object declared as __attribute__((unavailable)) should suppress
78 any reports of being declared with unavailable or deprecated items.
79 An object declared as __attribute__((deprecated)) should suppress
80 warnings of uses of other deprecated items. */
82 enum deprecated_states {
83 DEPRECATED_NORMAL,
84 DEPRECATED_SUPPRESS,
85 UNAVAILABLE_DEPRECATED_SUPPRESS
89 /* Nonzero if we have seen an invalid cross reference
90 to a struct, union, or enum, but not yet printed the message. */
91 tree pending_invalid_xref;
93 /* File and line to appear in the eventual error message. */
94 location_t pending_invalid_xref_location;
96 /* The file and line that the prototype came from if this is an
97 old-style definition; used for diagnostics in
98 store_parm_decls_oldstyle. */
100 static location_t current_function_prototype_locus;
102 /* Whether this prototype was built-in. */
104 static bool current_function_prototype_built_in;
106 /* The argument type information of this prototype. */
108 static tree current_function_prototype_arg_types;
110 /* The argument information structure for the function currently being
111 defined. */
113 static struct c_arg_info *current_function_arg_info;
115 /* The obstack on which parser and related data structures, which are
116 not live beyond their top-level declaration or definition, are
117 allocated. */
118 struct obstack parser_obstack;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* Zero if we are not in an iteration or switch statement, otherwise
125 a bitmask. See bitmask definitions in c-tree.h. */
126 unsigned char in_statement;
128 /* A list of decls to be made automatically visible in each file scope. */
129 static GTY(()) tree visible_builtins;
131 /* Set to 0 at beginning of a function definition, set to 1 if
132 a return statement that specifies a return value is seen. */
134 int current_function_returns_value;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement with no argument is seen. */
139 int current_function_returns_null;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a call to a noreturn function is seen. */
144 int current_function_returns_abnormally;
146 /* Set to nonzero by `grokdeclarator' for a function
147 whose return type is defaulted, if warnings for this are desired. */
149 static int warn_about_return_type;
151 /* Nonzero when the current toplevel function contains a declaration
152 of a nested function which is never defined. */
154 static bool undef_nested_function;
156 /* Vector of implicit "omp declare target" attributes to be added into
157 the attribute lists. */
158 vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
160 /* If non-zero, we are inside of
161 #pragma omp begin assumes ... #pragma omp end assumes region. */
162 int current_omp_begin_assumes;
164 /* Each c_binding structure describes one binding of an identifier to
165 a decl. All the decls in a scope - irrespective of namespace - are
166 chained together by the ->prev field, which (as the name implies)
167 runs in reverse order. All the decls in a given namespace bound to
168 a given identifier are chained by the ->shadowed field, which runs
169 from inner to outer scopes.
171 The ->decl field usually points to a DECL node, but there are two
172 exceptions. In the namespace of type tags, the bound entity is a
173 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
174 identifier is encountered, it is bound to error_mark_node to
175 suppress further errors about that identifier in the current
176 function.
178 The ->u.type field stores the type of the declaration in this scope;
179 if NULL, the type is the type of the ->decl field. This is only of
180 relevance for objects with external or internal linkage which may
181 be redeclared in inner scopes, forming composite types that only
182 persist for the duration of those scopes. In the external scope,
183 this stores the composite of all the types declared for this
184 object, visible or not. The ->inner_comp field (used only at file
185 scope) stores whether an incomplete array type at file scope was
186 completed at an inner scope to an array size other than 1.
188 The ->u.label field is used for labels. It points to a structure
189 which stores additional information used for warnings.
191 The depth field is copied from the scope structure that holds this
192 decl. It is used to preserve the proper ordering of the ->shadowed
193 field (see bind()) and also for a handful of special-case checks.
194 Finally, the invisible bit is true for a decl which should be
195 ignored for purposes of normal name lookup, and the nested bit is
196 true for a decl that's been bound a second time in an inner scope;
197 in all such cases, the binding in the outer scope will have its
198 invisible bit true. */
200 struct GTY((chain_next ("%h.prev"))) c_binding {
201 union GTY(()) { /* first so GTY desc can use decl */
202 tree GTY((tag ("0"))) type; /* the type in this scope */
203 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
204 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
205 tree decl; /* the decl bound */
206 tree id; /* the identifier it's bound to */
207 struct c_binding *prev; /* the previous decl in this scope */
208 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
209 unsigned int depth : 28; /* depth of this scope */
210 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
211 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
212 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
213 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
214 location_t locus; /* location for nested bindings */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 /* Each C symbol points to three linked lists of c_binding structures.
222 These describe the values of the identifier in the three different
223 namespaces defined by the language. */
225 struct GTY(()) lang_identifier {
226 struct c_common_identifier common_id;
227 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
228 struct c_binding *tag_binding; /* struct/union/enum tags */
229 struct c_binding *label_binding; /* labels */
232 /* Validate c-lang.cc's assumptions. */
233 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
234 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
236 /* The binding oracle; see c-tree.h. */
237 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
239 /* This flag is set on an identifier if we have previously asked the
240 binding oracle for this identifier's symbol binding. */
241 #define I_SYMBOL_CHECKED(node) \
242 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
244 static inline struct c_binding* *
245 i_symbol_binding (tree node)
247 struct lang_identifier *lid
248 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
250 if (lid->symbol_binding == NULL
251 && c_binding_oracle != NULL
252 && !I_SYMBOL_CHECKED (node))
254 /* Set the "checked" flag first, to avoid infinite recursion
255 when the binding oracle calls back into gcc. */
256 I_SYMBOL_CHECKED (node) = 1;
257 c_binding_oracle (C_ORACLE_SYMBOL, node);
260 return &lid->symbol_binding;
263 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
265 #define I_SYMBOL_DECL(node) \
266 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
268 /* This flag is set on an identifier if we have previously asked the
269 binding oracle for this identifier's tag binding. */
270 #define I_TAG_CHECKED(node) \
271 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
273 static inline struct c_binding **
274 i_tag_binding (tree node)
276 struct lang_identifier *lid
277 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
279 if (lid->tag_binding == NULL
280 && c_binding_oracle != NULL
281 && !I_TAG_CHECKED (node))
283 /* Set the "checked" flag first, to avoid infinite recursion
284 when the binding oracle calls back into gcc. */
285 I_TAG_CHECKED (node) = 1;
286 c_binding_oracle (C_ORACLE_TAG, node);
289 return &lid->tag_binding;
292 #define I_TAG_BINDING(node) (*i_tag_binding (node))
294 #define I_TAG_DECL(node) \
295 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
297 /* This flag is set on an identifier if we have previously asked the
298 binding oracle for this identifier's label binding. */
299 #define I_LABEL_CHECKED(node) \
300 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
302 static inline struct c_binding **
303 i_label_binding (tree node)
305 struct lang_identifier *lid
306 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
308 if (lid->label_binding == NULL
309 && c_binding_oracle != NULL
310 && !I_LABEL_CHECKED (node))
312 /* Set the "checked" flag first, to avoid infinite recursion
313 when the binding oracle calls back into gcc. */
314 I_LABEL_CHECKED (node) = 1;
315 c_binding_oracle (C_ORACLE_LABEL, node);
318 return &lid->label_binding;
321 #define I_LABEL_BINDING(node) (*i_label_binding (node))
323 #define I_LABEL_DECL(node) \
324 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
326 /* The resulting tree type. */
328 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
329 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
331 union tree_node GTY ((tag ("0"),
332 desc ("tree_node_structure (&%h)")))
333 generic;
334 struct lang_identifier GTY ((tag ("1"))) identifier;
337 /* Track bindings and other things that matter for goto warnings. For
338 efficiency, we do not gather all the decls at the point of
339 definition. Instead, we point into the bindings structure. As
340 scopes are popped, we update these structures and gather the decls
341 that matter at that time. */
343 struct GTY(()) c_spot_bindings {
344 /* The currently open scope which holds bindings defined when the
345 label was defined or the goto statement was found. */
346 struct c_scope *scope;
347 /* The bindings in the scope field which were defined at the point
348 of the label or goto. This lets us look at older or newer
349 bindings in the scope, as appropriate. */
350 struct c_binding *bindings_in_scope;
351 /* The number of statement expressions that have started since this
352 label or goto statement was defined. This is zero if we are at
353 the same statement expression level. It is positive if we are in
354 a statement expression started since this spot. It is negative
355 if this spot was in a statement expression and we have left
356 it. */
357 int stmt_exprs;
358 /* Whether we started in a statement expression but are no longer in
359 it. This is set to true if stmt_exprs ever goes negative. */
360 bool left_stmt_expr;
363 /* This structure is used to keep track of bindings seen when a goto
364 statement is defined. This is only used if we see the goto
365 statement before we see the label. */
367 struct GTY(()) c_goto_bindings {
368 /* The location of the goto statement. */
369 location_t loc;
370 /* The bindings of the goto statement. */
371 struct c_spot_bindings goto_bindings;
374 typedef struct c_goto_bindings *c_goto_bindings_p;
376 /* The additional information we keep track of for a label binding.
377 These fields are updated as scopes are popped. */
379 struct GTY(()) c_label_vars {
380 /* The shadowed c_label_vars, when one label shadows another (which
381 can only happen using a __label__ declaration). */
382 struct c_label_vars *shadowed;
383 /* The bindings when the label was defined. */
384 struct c_spot_bindings label_bindings;
385 /* A list of decls that we care about: decls about which we should
386 warn if a goto branches to this label from later in the function.
387 Decls are added to this list as scopes are popped. We only add
388 the decls that matter. */
389 vec<tree, va_gc> *decls_in_scope;
390 /* A list of goto statements to this label. This is only used for
391 goto statements seen before the label was defined, so that we can
392 issue appropriate warnings for them. */
393 vec<c_goto_bindings_p, va_gc> *gotos;
396 /* Each c_scope structure describes the complete contents of one
397 scope. Four scopes are distinguished specially: the innermost or
398 current scope, the innermost function scope, the file scope (always
399 the second to outermost) and the outermost or external scope.
401 Most declarations are recorded in the current scope.
403 All normal label declarations are recorded in the innermost
404 function scope, as are bindings of undeclared identifiers to
405 error_mark_node. (GCC permits nested functions as an extension,
406 hence the 'innermost' qualifier.) Explicitly declared labels
407 (using the __label__ extension) appear in the current scope.
409 Being in the file scope (current_scope == file_scope) causes
410 special behavior in several places below. Also, under some
411 conditions the Objective-C front end records declarations in the
412 file scope even though that isn't the current scope.
414 All declarations with external linkage are recorded in the external
415 scope, even if they aren't visible there; this models the fact that
416 such declarations are visible to the entire program, and (with a
417 bit of cleverness, see pushdecl) allows diagnosis of some violations
418 of C99 6.2.2p7 and 6.2.7p2:
420 If, within the same translation unit, the same identifier appears
421 with both internal and external linkage, the behavior is
422 undefined.
424 All declarations that refer to the same object or function shall
425 have compatible type; otherwise, the behavior is undefined.
427 Initially only the built-in declarations, which describe compiler
428 intrinsic functions plus a subset of the standard library, are in
429 this scope.
431 The order of the blocks list matters, and it is frequently appended
432 to. To avoid having to walk all the way to the end of the list on
433 each insertion, or reverse the list later, we maintain a pointer to
434 the last list entry. (FIXME: It should be feasible to use a reversed
435 list here.)
437 The bindings list is strictly in reverse order of declarations;
438 pop_scope relies on this. */
441 struct GTY((chain_next ("%h.outer"))) c_scope {
442 /* The scope containing this one. */
443 struct c_scope *outer;
445 /* The next outermost function scope. */
446 struct c_scope *outer_function;
448 /* All bindings in this scope. */
449 struct c_binding *bindings;
451 /* For each scope (except the global one), a chain of BLOCK nodes
452 for all the scopes that were entered and exited one level down. */
453 tree blocks;
454 tree blocks_last;
456 /* The depth of this scope. Used to keep the ->shadowed chain of
457 bindings sorted innermost to outermost. */
458 unsigned int depth : 28;
460 /* True if we are currently filling this scope with parameter
461 declarations. */
462 BOOL_BITFIELD parm_flag : 1;
464 /* True if we saw [*] in this scope. Used to give an error messages
465 if these appears in a function definition. */
466 BOOL_BITFIELD had_vla_unspec : 1;
468 /* True if we already complained about forward parameter decls
469 in this scope. This prevents double warnings on
470 foo (int a; int b; ...) */
471 BOOL_BITFIELD warned_forward_parm_decls : 1;
473 /* True if this is the outermost block scope of a function body.
474 This scope contains the parameters, the local variables declared
475 in the outermost block, and all the labels (except those in
476 nested functions, or declared at block scope with __label__). */
477 BOOL_BITFIELD function_body : 1;
479 /* True means make a BLOCK for this scope no matter what. */
480 BOOL_BITFIELD keep : 1;
482 /* True means that an unsuffixed float constant is _Decimal64. */
483 BOOL_BITFIELD float_const_decimal64 : 1;
485 /* True if this scope has any label bindings. This is used to speed
486 up searching for labels when popping scopes, particularly since
487 labels are normally only found at function scope. */
488 BOOL_BITFIELD has_label_bindings : 1;
490 /* True if we should issue a warning if a goto statement crosses any
491 of the bindings. We still need to check the list of bindings to
492 find the specific ones we need to warn about. This is true if
493 decl_jump_unsafe would return true for any of the bindings. This
494 is used to avoid looping over all the bindings unnecessarily. */
495 BOOL_BITFIELD has_jump_unsafe_decl : 1;
498 /* The scope currently in effect. */
500 static GTY(()) struct c_scope *current_scope;
502 /* The innermost function scope. Ordinary (not explicitly declared)
503 labels, bindings to error_mark_node, and the lazily-created
504 bindings of __func__ and its friends get this scope. */
506 static GTY(()) struct c_scope *current_function_scope;
508 /* The C file scope. This is reset for each input translation unit. */
510 static GTY(()) struct c_scope *file_scope;
512 /* The outermost scope. This is used for all declarations with
513 external linkage, and only these, hence the name. */
515 static GTY(()) struct c_scope *external_scope;
517 /* A chain of c_scope structures awaiting reuse. */
519 static GTY((deletable)) struct c_scope *scope_freelist;
521 /* A chain of c_binding structures awaiting reuse. */
523 static GTY((deletable)) struct c_binding *binding_freelist;
525 /* Append VAR to LIST in scope SCOPE. */
526 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
527 struct c_scope *s_ = (scope); \
528 tree d_ = (decl); \
529 if (s_->list##_last) \
530 BLOCK_CHAIN (s_->list##_last) = d_; \
531 else \
532 s_->list = d_; \
533 s_->list##_last = d_; \
534 } while (0)
536 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
537 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
538 struct c_scope *t_ = (tscope); \
539 struct c_scope *f_ = (fscope); \
540 if (t_->to##_last) \
541 BLOCK_CHAIN (t_->to##_last) = f_->from; \
542 else \
543 t_->to = f_->from; \
544 t_->to##_last = f_->from##_last; \
545 } while (0)
547 /* A c_inline_static structure stores details of a static identifier
548 referenced in a definition of a function that may be an inline
549 definition if no subsequent declaration of that function uses
550 "extern" or does not use "inline". */
552 struct GTY((chain_next ("%h.next"))) c_inline_static {
553 /* The location for a diagnostic. */
554 location_t location;
556 /* The function that may be an inline definition. */
557 tree function;
559 /* The object or function referenced. */
560 tree static_decl;
562 /* What sort of reference this is. */
563 enum c_inline_static_type type;
565 /* The next such structure or NULL. */
566 struct c_inline_static *next;
569 /* List of static identifiers used or referenced in functions that may
570 be inline definitions. */
571 static GTY(()) struct c_inline_static *c_inline_statics;
573 /* True means unconditionally make a BLOCK for the next scope pushed. */
575 static bool keep_next_level_flag;
577 /* True means the next call to push_scope will be the outermost scope
578 of a function body, so do not push a new scope, merely cease
579 expecting parameter decls. */
581 static bool next_is_function_body;
583 /* A vector of pointers to c_binding structures. */
585 typedef struct c_binding *c_binding_ptr;
587 /* Information that we keep for a struct or union while it is being
588 parsed. */
590 class c_struct_parse_info
592 public:
593 /* If warn_cxx_compat, a list of types defined within this
594 struct. */
595 auto_vec<tree> struct_types;
596 /* If warn_cxx_compat, a list of field names which have bindings,
597 and which are defined in this struct, but which are not defined
598 in any enclosing struct. This is used to clear the in_struct
599 field of the c_bindings structure. */
600 auto_vec<c_binding_ptr> fields;
601 /* If warn_cxx_compat, a list of typedef names used when defining
602 fields in this struct. */
603 auto_vec<tree> typedefs_seen;
606 /* Information for the struct or union currently being parsed, or
607 NULL if not parsing a struct or union. */
608 static class c_struct_parse_info *struct_parse_info;
610 /* Forward declarations. */
611 static tree lookup_name_in_scope (tree, struct c_scope *);
612 static tree c_make_fname_decl (location_t, tree, int);
613 static tree grokdeclarator (const struct c_declarator *,
614 struct c_declspecs *,
615 enum decl_context, bool, tree *, tree *, tree *,
616 bool *, enum deprecated_states);
617 static tree grokparms (struct c_arg_info *, bool);
618 static void layout_array_type (tree);
619 static void warn_defaults_to (location_t, int, const char *, ...)
620 ATTRIBUTE_GCC_DIAG(3,4);
621 static const char *header_for_builtin_fn (tree);
623 /* T is a statement. Add it to the statement-tree. This is the
624 C/ObjC version--C++ has a slightly different version of this
625 function. */
627 tree
628 add_stmt (tree t)
630 enum tree_code code = TREE_CODE (t);
632 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
634 if (!EXPR_HAS_LOCATION (t))
635 SET_EXPR_LOCATION (t, input_location);
638 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
639 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
641 /* Add T to the statement-tree. Non-side-effect statements need to be
642 recorded during statement expressions. */
643 if (!building_stmt_list_p ())
644 push_stmt_list ();
645 append_to_statement_list_force (t, &cur_stmt_list);
647 return t;
650 /* Build a pointer type using the default pointer mode. */
652 static tree
653 c_build_pointer_type (tree to_type)
655 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
656 : TYPE_ADDR_SPACE (to_type);
657 machine_mode pointer_mode;
659 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
660 pointer_mode = targetm.addr_space.pointer_mode (as);
661 else
662 pointer_mode = c_default_pointer_mode;
663 return build_pointer_type_for_mode (to_type, pointer_mode, false);
667 /* Return true if we will want to say something if a goto statement
668 crosses DECL. */
670 static bool
671 decl_jump_unsafe (tree decl)
673 if (error_operand_p (decl))
674 return false;
676 /* Don't warn for compound literals. If a goto statement crosses
677 their initialization, it should cross also all the places where
678 the complit is used or where the complit address might be saved
679 into some variable, so code after the label to which goto jumps
680 should not be able to refer to the compound literal. */
681 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
682 return false;
684 /* Always warn about crossing variably modified types. */
685 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
686 && c_type_variably_modified_p (TREE_TYPE (decl)))
687 return true;
689 /* Otherwise, only warn if -Wgoto-misses-init and this is an
690 initialized automatic decl. */
691 if (warn_jump_misses_init
692 && VAR_P (decl)
693 && !TREE_STATIC (decl)
694 && DECL_INITIAL (decl) != NULL_TREE)
695 return true;
697 return false;
701 void
702 c_print_identifier (FILE *file, tree node, int indent)
704 void (*save) (enum c_oracle_request, tree identifier);
706 /* Temporarily hide any binding oracle. Without this, calls to
707 debug_tree from the debugger will end up calling into the oracle,
708 making for a confusing debug session. As the oracle isn't needed
709 here for normal operation, it's simplest to suppress it. */
710 save = c_binding_oracle;
711 c_binding_oracle = NULL;
713 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
714 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
715 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
716 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
718 tree rid = ridpointers[C_RID_CODE (node)];
719 indent_to (file, indent + 4);
720 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
721 (void *) rid, IDENTIFIER_POINTER (rid));
724 c_binding_oracle = save;
727 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
728 which may be any of several kinds of DECL or TYPE or error_mark_node,
729 in the scope SCOPE. */
730 static void
731 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
732 bool nested, location_t locus)
734 struct c_binding *b, **here;
736 if (binding_freelist)
738 b = binding_freelist;
739 binding_freelist = b->prev;
741 else
742 b = ggc_alloc<c_binding> ();
744 b->shadowed = 0;
745 b->decl = decl;
746 b->id = name;
747 b->depth = scope->depth;
748 b->invisible = invisible;
749 b->nested = nested;
750 b->inner_comp = 0;
751 b->in_struct = 0;
752 b->locus = locus;
754 b->u.type = NULL;
756 b->prev = scope->bindings;
757 scope->bindings = b;
759 if (decl_jump_unsafe (decl))
760 scope->has_jump_unsafe_decl = 1;
762 if (!name)
763 return;
765 switch (TREE_CODE (decl))
767 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
768 case ENUMERAL_TYPE:
769 case UNION_TYPE:
770 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
771 case VAR_DECL:
772 case FUNCTION_DECL:
773 case TYPE_DECL:
774 case CONST_DECL:
775 case PARM_DECL:
776 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
778 default:
779 gcc_unreachable ();
782 /* Locate the appropriate place in the chain of shadowed decls
783 to insert this binding. Normally, scope == current_scope and
784 this does nothing. */
785 while (*here && (*here)->depth > scope->depth)
786 here = &(*here)->shadowed;
788 b->shadowed = *here;
789 *here = b;
792 /* Clear the binding structure B, stick it on the binding_freelist,
793 and return the former value of b->prev. This is used by pop_scope
794 and get_parm_info to iterate destructively over all the bindings
795 from a given scope. */
796 static struct c_binding *
797 free_binding_and_advance (struct c_binding *b)
799 struct c_binding *prev = b->prev;
801 memset (b, 0, sizeof (struct c_binding));
802 b->prev = binding_freelist;
803 binding_freelist = b;
805 return prev;
808 /* Bind a label. Like bind, but skip fields which aren't used for
809 labels, and add the LABEL_VARS value. */
810 static void
811 bind_label (tree name, tree label, struct c_scope *scope,
812 struct c_label_vars *label_vars)
814 struct c_binding *b;
816 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
817 UNKNOWN_LOCATION);
819 scope->has_label_bindings = true;
821 b = scope->bindings;
822 gcc_assert (b->decl == label);
823 label_vars->shadowed = b->u.label;
824 b->u.label = label_vars;
827 /* Hook called at end of compilation to assume 1 elt
828 for a file-scope tentative array defn that wasn't complete before. */
830 void
831 c_finish_incomplete_decl (tree decl)
833 if (VAR_P (decl))
835 tree type = TREE_TYPE (decl);
836 if (type != error_mark_node
837 && TREE_CODE (type) == ARRAY_TYPE
838 && !DECL_EXTERNAL (decl)
839 && TYPE_DOMAIN (type) == NULL_TREE)
841 warning_at (DECL_SOURCE_LOCATION (decl),
842 0, "array %q+D assumed to have one element", decl);
844 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
846 relayout_decl (decl);
851 /* Record that inline function FUNC contains a reference (location
852 LOC) to static DECL (file-scope or function-local according to
853 TYPE). */
855 void
856 record_inline_static (location_t loc, tree func, tree decl,
857 enum c_inline_static_type type)
859 c_inline_static *csi = ggc_alloc<c_inline_static> ();
860 csi->location = loc;
861 csi->function = func;
862 csi->static_decl = decl;
863 csi->type = type;
864 csi->next = c_inline_statics;
865 c_inline_statics = csi;
868 /* Check for references to static declarations in inline functions at
869 the end of the translation unit and diagnose them if the functions
870 are still inline definitions. */
872 static void
873 check_inline_statics (void)
875 struct c_inline_static *csi;
876 for (csi = c_inline_statics; csi; csi = csi->next)
878 if (DECL_EXTERNAL (csi->function))
879 switch (csi->type)
881 case csi_internal:
882 pedwarn (csi->location, 0,
883 "%qD is static but used in inline function %qD "
884 "which is not static", csi->static_decl, csi->function);
885 break;
886 case csi_modifiable:
887 pedwarn (csi->location, 0,
888 "%q+D is static but declared in inline function %qD "
889 "which is not static", csi->static_decl, csi->function);
890 break;
891 default:
892 gcc_unreachable ();
895 c_inline_statics = NULL;
898 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
899 for the current state, otherwise set it to uninitialized. */
901 static void
902 set_spot_bindings (struct c_spot_bindings *p, bool defining)
904 if (defining)
906 p->scope = current_scope;
907 p->bindings_in_scope = current_scope->bindings;
909 else
911 p->scope = NULL;
912 p->bindings_in_scope = NULL;
914 p->stmt_exprs = 0;
915 p->left_stmt_expr = false;
918 /* Update spot bindings P as we pop out of SCOPE. Return true if we
919 should push decls for a label. */
921 static bool
922 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
924 if (p->scope != scope)
926 /* This label or goto is defined in some other scope, or it is a
927 label which is not yet defined. There is nothing to
928 update. */
929 return false;
932 /* Adjust the spot bindings to refer to the bindings already defined
933 in the enclosing scope. */
934 p->scope = scope->outer;
935 p->bindings_in_scope = p->scope->bindings;
937 return true;
940 /* The Objective-C front-end often needs to determine the current scope. */
942 void *
943 objc_get_current_scope (void)
945 return current_scope;
948 /* The following function is used only by Objective-C. It needs to live here
949 because it accesses the innards of c_scope. */
951 void
952 objc_mark_locals_volatile (void *enclosing_blk)
954 struct c_scope *scope;
955 struct c_binding *b;
957 for (scope = current_scope;
958 scope && scope != enclosing_blk;
959 scope = scope->outer)
961 for (b = scope->bindings; b; b = b->prev)
962 objc_volatilize_decl (b->decl);
964 /* Do not climb up past the current function. */
965 if (scope->function_body)
966 break;
970 /* Return true if we are in the global binding level. */
972 bool
973 global_bindings_p (void)
975 return current_scope == file_scope;
978 /* Return true if we're declaring parameters in an old-style function
979 declaration. */
981 bool
982 old_style_parameter_scope (void)
984 /* If processing parameters and there is no function statement list, we
985 * have an old-style function declaration. */
986 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
989 void
990 keep_next_level (void)
992 keep_next_level_flag = true;
995 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
997 void
998 set_float_const_decimal64 (void)
1000 current_scope->float_const_decimal64 = true;
1003 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1005 void
1006 clear_float_const_decimal64 (void)
1008 current_scope->float_const_decimal64 = false;
1011 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1013 bool
1014 float_const_decimal64_p (void)
1016 return current_scope->float_const_decimal64;
1019 /* Identify this scope as currently being filled with parameters. */
1021 void
1022 declare_parm_level (void)
1024 current_scope->parm_flag = true;
1027 void
1028 push_scope (void)
1030 if (next_is_function_body)
1032 /* This is the transition from the parameters to the top level
1033 of the function body. These are the same scope
1034 (C99 6.2.1p4,6) so we do not push another scope structure.
1035 next_is_function_body is set only by store_parm_decls, which
1036 in turn is called when and only when we are about to
1037 encounter the opening curly brace for the function body.
1039 The outermost block of a function always gets a BLOCK node,
1040 because the debugging output routines expect that each
1041 function has at least one BLOCK. */
1042 current_scope->parm_flag = false;
1043 current_scope->function_body = true;
1044 current_scope->keep = true;
1045 current_scope->outer_function = current_function_scope;
1046 current_function_scope = current_scope;
1048 keep_next_level_flag = false;
1049 next_is_function_body = false;
1051 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1052 if (current_scope->outer)
1053 current_scope->float_const_decimal64
1054 = current_scope->outer->float_const_decimal64;
1055 else
1056 current_scope->float_const_decimal64 = false;
1058 else
1060 struct c_scope *scope;
1061 if (scope_freelist)
1063 scope = scope_freelist;
1064 scope_freelist = scope->outer;
1066 else
1067 scope = ggc_cleared_alloc<c_scope> ();
1069 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1070 if (current_scope)
1071 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1072 else
1073 scope->float_const_decimal64 = false;
1075 scope->keep = keep_next_level_flag;
1076 scope->outer = current_scope;
1077 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1079 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1080 possible. */
1081 if (current_scope && scope->depth == 0)
1083 scope->depth--;
1084 sorry ("GCC supports only %u nested scopes", scope->depth);
1087 current_scope = scope;
1088 keep_next_level_flag = false;
1092 /* This is called when we are leaving SCOPE. For each label defined
1093 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1094 These are the decls whose initialization will be skipped by a goto
1095 later in the function. */
1097 static void
1098 update_label_decls (struct c_scope *scope)
1100 struct c_scope *s;
1102 s = scope;
1103 while (s != NULL)
1105 if (s->has_label_bindings)
1107 struct c_binding *b;
1109 for (b = s->bindings; b != NULL; b = b->prev)
1111 struct c_label_vars *label_vars;
1112 struct c_binding *b1;
1113 bool hjud;
1114 unsigned int ix;
1115 struct c_goto_bindings *g;
1117 if (TREE_CODE (b->decl) != LABEL_DECL)
1118 continue;
1119 label_vars = b->u.label;
1121 b1 = label_vars->label_bindings.bindings_in_scope;
1122 if (label_vars->label_bindings.scope == NULL)
1123 hjud = false;
1124 else
1125 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1126 if (update_spot_bindings (scope, &label_vars->label_bindings))
1128 /* This label is defined in this scope. */
1129 if (hjud)
1131 for (; b1 != NULL; b1 = b1->prev)
1133 /* A goto from later in the function to this
1134 label will never see the initialization
1135 of B1, if any. Save it to issue a
1136 warning if needed. */
1137 if (decl_jump_unsafe (b1->decl))
1138 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1143 /* Update the bindings of any goto statements associated
1144 with this label. */
1145 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1146 update_spot_bindings (scope, &g->goto_bindings);
1150 /* Don't search beyond the current function. */
1151 if (s == current_function_scope)
1152 break;
1154 s = s->outer;
1158 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1160 static void
1161 set_type_context (tree type, tree context)
1163 for (type = TYPE_MAIN_VARIANT (type); type;
1164 type = TYPE_NEXT_VARIANT (type))
1165 TYPE_CONTEXT (type) = context;
1168 /* Exit a scope. Restore the state of the identifier-decl mappings
1169 that were in effect when this scope was entered. Return a BLOCK
1170 node containing all the DECLs in this scope that are of interest
1171 to debug info generation. */
1173 tree
1174 pop_scope (void)
1176 struct c_scope *scope = current_scope;
1177 tree block, context, p;
1178 struct c_binding *b;
1180 bool functionbody = scope->function_body;
1181 bool keep = functionbody || scope->keep || scope->bindings;
1183 update_label_decls (scope);
1185 /* If appropriate, create a BLOCK to record the decls for the life
1186 of this function. */
1187 block = NULL_TREE;
1188 if (keep)
1190 block = make_node (BLOCK);
1191 BLOCK_SUBBLOCKS (block) = scope->blocks;
1192 TREE_USED (block) = 1;
1194 /* In each subblock, record that this is its superior. */
1195 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1196 BLOCK_SUPERCONTEXT (p) = block;
1198 BLOCK_VARS (block) = NULL_TREE;
1201 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1202 scope must be set so that they point to the appropriate
1203 construct, i.e. either to the current FUNCTION_DECL node, or
1204 else to the BLOCK node we just constructed.
1206 Note that for tagged types whose scope is just the formal
1207 parameter list for some function type specification, we can't
1208 properly set their TYPE_CONTEXTs here, because we don't have a
1209 pointer to the appropriate FUNCTION_TYPE node readily available
1210 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1211 type nodes get set in `grokdeclarator' as soon as we have created
1212 the FUNCTION_TYPE node which will represent the "scope" for these
1213 "parameter list local" tagged types. */
1214 if (scope->function_body)
1215 context = current_function_decl;
1216 else if (scope == file_scope)
1218 tree file_decl
1219 = build_translation_unit_decl (get_identifier (main_input_filename));
1220 context = file_decl;
1221 debug_hooks->register_main_translation_unit (file_decl);
1223 else
1224 context = block;
1226 /* Clear all bindings in this scope. */
1227 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1229 p = b->decl;
1230 switch (TREE_CODE (p))
1232 case LABEL_DECL:
1233 /* Warnings for unused labels, errors for undefined labels. */
1234 if (TREE_USED (p) && !DECL_INITIAL (p))
1236 error ("label %q+D used but not defined", p);
1237 DECL_INITIAL (p) = error_mark_node;
1239 else
1240 warn_for_unused_label (p);
1242 /* Labels go in BLOCK_VARS. */
1243 DECL_CHAIN (p) = BLOCK_VARS (block);
1244 BLOCK_VARS (block) = p;
1245 gcc_assert (I_LABEL_BINDING (b->id) == b);
1246 I_LABEL_BINDING (b->id) = b->shadowed;
1248 /* Also pop back to the shadowed label_vars. */
1249 release_tree_vector (b->u.label->decls_in_scope);
1250 b->u.label = b->u.label->shadowed;
1251 break;
1253 case ENUMERAL_TYPE:
1254 case UNION_TYPE:
1255 case RECORD_TYPE:
1256 set_type_context (p, context);
1258 /* Types may not have tag-names, in which case the type
1259 appears in the bindings list with b->id NULL. */
1260 if (b->id)
1262 gcc_assert (I_TAG_BINDING (b->id) == b);
1263 I_TAG_BINDING (b->id) = b->shadowed;
1265 break;
1267 case FUNCTION_DECL:
1268 /* Propagate TREE_ADDRESSABLE from nested functions to their
1269 containing functions. */
1270 if (!TREE_ASM_WRITTEN (p)
1271 && DECL_INITIAL (p) != NULL_TREE
1272 && TREE_ADDRESSABLE (p)
1273 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1274 && DECL_ABSTRACT_ORIGIN (p) != p)
1275 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1276 if (!TREE_PUBLIC (p)
1277 && !DECL_INITIAL (p)
1278 && !b->nested
1279 && scope != file_scope
1280 && scope != external_scope)
1282 error ("nested function %q+D declared but never defined", p);
1283 undef_nested_function = true;
1285 else if (DECL_DECLARED_INLINE_P (p)
1286 && TREE_PUBLIC (p)
1287 && !DECL_INITIAL (p))
1289 /* C99 6.7.4p6: "a function with external linkage... declared
1290 with an inline function specifier ... shall also be defined
1291 in the same translation unit." */
1292 if (!flag_gnu89_inline
1293 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1294 && scope == external_scope)
1295 pedwarn (input_location, 0,
1296 "inline function %q+D declared but never defined", p);
1297 DECL_EXTERNAL (p) = 1;
1300 goto common_symbol;
1302 case VAR_DECL:
1303 /* Warnings for unused variables. */
1304 if ((!TREE_USED (p) || !DECL_READ_P (p))
1305 && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1306 && !DECL_IN_SYSTEM_HEADER (p)
1307 && DECL_NAME (p)
1308 && !DECL_ARTIFICIAL (p)
1309 && scope != file_scope
1310 && scope != external_scope)
1312 if (!TREE_USED (p))
1314 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1315 suppress_warning (p, OPT_Wunused_variable);
1317 else if (DECL_CONTEXT (p) == current_function_decl)
1318 warning_at (DECL_SOURCE_LOCATION (p),
1319 OPT_Wunused_but_set_variable,
1320 "variable %qD set but not used", p);
1323 if (b->inner_comp)
1325 error ("type of array %q+D completed incompatibly with"
1326 " implicit initialization", p);
1329 /* Fall through. */
1330 case TYPE_DECL:
1331 case CONST_DECL:
1332 common_symbol:
1333 /* All of these go in BLOCK_VARS, but only if this is the
1334 binding in the home scope. */
1335 if (!b->nested)
1337 DECL_CHAIN (p) = BLOCK_VARS (block);
1338 BLOCK_VARS (block) = p;
1340 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1342 /* For block local externs add a special
1343 DECL_EXTERNAL decl for debug info generation. */
1344 tree extp = copy_node (p);
1346 DECL_EXTERNAL (extp) = 1;
1347 TREE_STATIC (extp) = 0;
1348 TREE_PUBLIC (extp) = 1;
1349 DECL_INITIAL (extp) = NULL_TREE;
1350 DECL_LANG_SPECIFIC (extp) = NULL;
1351 DECL_CONTEXT (extp) = current_function_decl;
1352 if (TREE_CODE (p) == FUNCTION_DECL)
1354 DECL_RESULT (extp) = NULL_TREE;
1355 DECL_SAVED_TREE (extp) = NULL_TREE;
1356 DECL_STRUCT_FUNCTION (extp) = NULL;
1358 if (b->locus != UNKNOWN_LOCATION)
1359 DECL_SOURCE_LOCATION (extp) = b->locus;
1360 DECL_CHAIN (extp) = BLOCK_VARS (block);
1361 BLOCK_VARS (block) = extp;
1363 /* If this is the file scope set DECL_CONTEXT of each decl to
1364 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1365 work. */
1366 if (scope == file_scope)
1368 DECL_CONTEXT (p) = context;
1369 if (TREE_CODE (p) == TYPE_DECL
1370 && TREE_TYPE (p) != error_mark_node)
1371 set_type_context (TREE_TYPE (p), context);
1374 gcc_fallthrough ();
1375 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1376 already been put there by store_parm_decls. Unused-
1377 parameter warnings are handled by function.cc.
1378 error_mark_node obviously does not go in BLOCK_VARS and
1379 does not get unused-variable warnings. */
1380 case PARM_DECL:
1381 case ERROR_MARK:
1382 /* It is possible for a decl not to have a name. We get
1383 here with b->id NULL in this case. */
1384 if (b->id)
1386 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1387 I_SYMBOL_BINDING (b->id) = b->shadowed;
1388 if (b->shadowed && b->shadowed->u.type)
1389 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1391 break;
1393 default:
1394 gcc_unreachable ();
1399 /* Dispose of the block that we just made inside some higher level. */
1400 if ((scope->function_body || scope == file_scope) && context)
1402 DECL_INITIAL (context) = block;
1403 BLOCK_SUPERCONTEXT (block) = context;
1405 else if (scope->outer)
1407 if (block)
1408 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1409 /* If we did not make a block for the scope just exited, any
1410 blocks made for inner scopes must be carried forward so they
1411 will later become subblocks of something else. */
1412 else if (scope->blocks)
1413 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1416 /* Pop the current scope, and free the structure for reuse. */
1417 current_scope = scope->outer;
1418 if (scope->function_body)
1419 current_function_scope = scope->outer_function;
1421 memset (scope, 0, sizeof (struct c_scope));
1422 scope->outer = scope_freelist;
1423 scope_freelist = scope;
1425 return block;
1428 void
1429 push_file_scope (void)
1431 tree decl;
1433 if (file_scope)
1434 return;
1436 push_scope ();
1437 file_scope = current_scope;
1439 start_fname_decls ();
1441 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1442 bind (DECL_NAME (decl), decl, file_scope,
1443 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1446 void
1447 pop_file_scope (void)
1449 /* In case there were missing closebraces, get us back to the global
1450 binding level. */
1451 while (current_scope != file_scope)
1452 pop_scope ();
1454 /* __FUNCTION__ is defined at file scope (""). This
1455 call may not be necessary as my tests indicate it
1456 still works without it. */
1457 finish_fname_decls ();
1459 check_inline_statics ();
1461 /* This is the point to write out a PCH if we're doing that.
1462 In that case we do not want to do anything else. */
1463 if (pch_file)
1465 c_common_write_pch ();
1466 /* Ensure even the callers don't try to finalize the CU. */
1467 flag_syntax_only = 1;
1468 return;
1471 /* Pop off the file scope and close this translation unit. */
1472 pop_scope ();
1473 file_scope = 0;
1475 maybe_apply_pending_pragma_weaks ();
1478 /* Whether we are curently inside the initializer for an
1479 underspecified object definition (C2x auto or constexpr). */
1480 static bool in_underspecified_init;
1482 /* Start an underspecified object definition for NAME at LOC. This
1483 means that NAME is shadowed inside its initializer, so neither the
1484 definition being initialized, nor any definition from an outer
1485 scope, may be referenced during that initializer. Return state to
1486 be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1487 underspecified object is a (constexpr) compound literal; there is
1488 no shadowing in that case, but all the other restrictions on
1489 underspecified object definitions still apply. */
1490 unsigned int
1491 start_underspecified_init (location_t loc, tree name)
1493 bool prev = in_underspecified_init;
1494 bool ok;
1495 if (name == NULL_TREE)
1496 ok = true;
1497 else
1499 tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1500 C_DECL_UNDERSPECIFIED (decl) = 1;
1501 struct c_scope *scope = current_scope;
1502 struct c_binding *b = I_SYMBOL_BINDING (name);
1503 if (b && B_IN_SCOPE (b, scope))
1505 error_at (loc, "underspecified declaration of %qE, which is already "
1506 "declared in this scope", name);
1507 ok = false;
1509 else
1511 bind (name, decl, scope, false, false, loc);
1512 ok = true;
1515 in_underspecified_init = true;
1516 return ok | (prev << 1);
1519 /* Finish an underspecified object definition for NAME, before that
1520 name is bound to the real declaration instead of a placeholder.
1521 PREV_STATE is the value returned by the call to
1522 start_underspecified_init. If NAME is NULL_TREE, this means a
1523 compound literal, as for start_underspecified_init. */
1524 void
1525 finish_underspecified_init (tree name, unsigned int prev_state)
1527 if (name != NULL_TREE && (prev_state & 1))
1529 /* A VAR_DECL was bound to the name to shadow any previous
1530 declarations for the name; remove that binding now. */
1531 struct c_scope *scope = current_scope;
1532 struct c_binding *b = I_SYMBOL_BINDING (name);
1533 gcc_assert (b);
1534 gcc_assert (B_IN_SCOPE (b, scope));
1535 gcc_assert (VAR_P (b->decl));
1536 gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1537 I_SYMBOL_BINDING (name) = b->shadowed;
1538 /* In erroneous cases there may be other bindings added to this
1539 scope during the initializer. */
1540 struct c_binding **p = &scope->bindings;
1541 while (*p != b)
1542 p = &((*p)->prev);
1543 *p = free_binding_and_advance (*p);
1545 in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1548 /* Adjust the bindings for the start of a statement expression. */
1550 void
1551 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1553 struct c_scope *scope;
1555 for (scope = current_scope; scope != NULL; scope = scope->outer)
1557 struct c_binding *b;
1559 if (!scope->has_label_bindings)
1560 continue;
1562 for (b = scope->bindings; b != NULL; b = b->prev)
1564 struct c_label_vars *label_vars;
1565 unsigned int ix;
1566 struct c_goto_bindings *g;
1568 if (TREE_CODE (b->decl) != LABEL_DECL)
1569 continue;
1570 label_vars = b->u.label;
1571 ++label_vars->label_bindings.stmt_exprs;
1572 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1573 ++g->goto_bindings.stmt_exprs;
1577 if (switch_bindings != NULL)
1578 ++switch_bindings->stmt_exprs;
1581 /* Adjust the bindings for the end of a statement expression. */
1583 void
1584 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1586 struct c_scope *scope;
1588 for (scope = current_scope; scope != NULL; scope = scope->outer)
1590 struct c_binding *b;
1592 if (!scope->has_label_bindings)
1593 continue;
1595 for (b = scope->bindings; b != NULL; b = b->prev)
1597 struct c_label_vars *label_vars;
1598 unsigned int ix;
1599 struct c_goto_bindings *g;
1601 if (TREE_CODE (b->decl) != LABEL_DECL)
1602 continue;
1603 label_vars = b->u.label;
1604 --label_vars->label_bindings.stmt_exprs;
1605 if (label_vars->label_bindings.stmt_exprs < 0)
1607 label_vars->label_bindings.left_stmt_expr = true;
1608 label_vars->label_bindings.stmt_exprs = 0;
1610 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1612 --g->goto_bindings.stmt_exprs;
1613 if (g->goto_bindings.stmt_exprs < 0)
1615 g->goto_bindings.left_stmt_expr = true;
1616 g->goto_bindings.stmt_exprs = 0;
1622 if (switch_bindings != NULL)
1624 --switch_bindings->stmt_exprs;
1625 gcc_assert (switch_bindings->stmt_exprs >= 0);
1629 /* Push a definition or a declaration of struct, union or enum tag "name".
1630 "type" should be the type node.
1631 We assume that the tag "name" is not already defined, and has a location
1632 of LOC.
1634 Note that the definition may really be just a forward reference.
1635 In that case, the TYPE_SIZE will be zero. */
1637 static void
1638 pushtag (location_t loc, tree name, tree type)
1640 /* Record the identifier as the type's name if it has none. */
1641 if (name && !TYPE_NAME (type))
1642 TYPE_NAME (type) = name;
1643 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1645 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1646 tagged type we just added to the current scope. This fake
1647 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1648 to output a representation of a tagged type, and it also gives
1649 us a convenient place to record the "scope start" address for the
1650 tagged type. */
1652 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1653 TYPE_DECL, NULL_TREE, type));
1655 /* An approximation for now, so we can tell this is a function-scope tag.
1656 This will be updated in pop_scope. */
1657 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1659 if (warn_cxx_compat && name != NULL_TREE)
1661 struct c_binding *b = I_SYMBOL_BINDING (name);
1663 if (b != NULL
1664 && b->decl != NULL_TREE
1665 && TREE_CODE (b->decl) == TYPE_DECL
1666 && (B_IN_CURRENT_SCOPE (b)
1667 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1668 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1669 != TYPE_MAIN_VARIANT (type)))
1671 auto_diagnostic_group d;
1672 if (warning_at (loc, OPT_Wc___compat,
1673 ("using %qD as both a typedef and a tag is "
1674 "invalid in C++"), b->decl)
1675 && b->locus != UNKNOWN_LOCATION)
1676 inform (b->locus, "originally defined here");
1681 /* An exported interface to pushtag. This is used by the gdb plugin's
1682 binding oracle to introduce a new tag binding. */
1684 void
1685 c_pushtag (location_t loc, tree name, tree type)
1687 pushtag (loc, name, type);
1690 /* An exported interface to bind a declaration. LOC is the location
1691 to use. DECL is the declaration to bind. The decl's name is used
1692 to determine how it is bound. If DECL is a VAR_DECL, then
1693 IS_GLOBAL determines whether the decl is put into the global (file
1694 and external) scope or the current function's scope; if DECL is not
1695 a VAR_DECL then it is always put into the file scope. */
1697 void
1698 c_bind (location_t loc, tree decl, bool is_global)
1700 struct c_scope *scope;
1701 bool nested = false;
1703 if (!VAR_P (decl) || current_function_scope == NULL)
1705 /* Types and functions are always considered to be global. */
1706 scope = file_scope;
1707 DECL_EXTERNAL (decl) = 1;
1708 TREE_PUBLIC (decl) = 1;
1710 else if (is_global)
1712 /* Also bind it into the external scope. */
1713 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1714 nested = true;
1715 scope = file_scope;
1716 DECL_EXTERNAL (decl) = 1;
1717 TREE_PUBLIC (decl) = 1;
1719 else
1721 DECL_CONTEXT (decl) = current_function_decl;
1722 TREE_PUBLIC (decl) = 0;
1723 scope = current_function_scope;
1726 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1730 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1731 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1732 to the builtin_structptr_types array. Subsequent declarations of such
1733 built-ins are expected to refer to it rather than to fileptr_type_node,
1734 etc. which is just void* (or to any other type).
1735 Used only by match_builtin_function_types. */
1737 static const unsigned builtin_structptr_type_count
1738 = ARRAY_SIZE (builtin_structptr_types);
1740 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1742 /* Returns true if types T1 and T2 representing return types or types
1743 of function arguments are close enough to be considered interchangeable
1744 in redeclarations of built-in functions. */
1746 static bool
1747 types_close_enough_to_match (tree t1, tree t2)
1749 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1750 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1751 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1754 /* Subroutine of compare_decls. Allow harmless mismatches in return
1755 and argument types provided that the type modes match. Set *STRICT
1756 and *ARGNO to the expected argument type and number in case of
1757 an argument type mismatch or null and zero otherwise. Return
1758 a unified type given a suitable match, and 0 otherwise. */
1760 static tree
1761 match_builtin_function_types (tree newtype, tree oldtype,
1762 tree *strict, unsigned *argno)
1764 *argno = 0;
1765 *strict = NULL_TREE;
1767 /* Accept the return type of the new declaration if it has the same
1768 mode and if they're both pointers or if neither is. */
1769 tree oldrettype = TREE_TYPE (oldtype);
1770 tree newrettype = TREE_TYPE (newtype);
1772 if (!types_close_enough_to_match (oldrettype, newrettype))
1773 return NULL_TREE;
1775 /* Check that the return types are compatible but don't fail if they
1776 are not (e.g., int vs long in ILP32) and just let the caller know. */
1777 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1778 TYPE_MAIN_VARIANT (newrettype)))
1779 *strict = oldrettype;
1781 tree oldargs = TYPE_ARG_TYPES (oldtype);
1782 tree newargs = TYPE_ARG_TYPES (newtype);
1783 tree tryargs = newargs;
1785 const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1786 const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1788 gcc_checking_assert (nlst == nbst);
1790 for (unsigned i = 1; oldargs || newargs; ++i)
1792 if (!oldargs
1793 || !newargs
1794 || !TREE_VALUE (oldargs)
1795 || !TREE_VALUE (newargs))
1796 return NULL_TREE;
1798 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1799 tree newtype = TREE_VALUE (newargs);
1800 if (newtype == error_mark_node)
1801 return NULL_TREE;
1802 newtype = TYPE_MAIN_VARIANT (newtype);
1804 if (!types_close_enough_to_match (oldtype, newtype))
1805 return NULL_TREE;
1807 unsigned j = nbst;
1808 if (POINTER_TYPE_P (oldtype))
1809 /* Iterate over well-known struct types like FILE (whose types
1810 aren't known to us) and compare the pointer to each to
1811 the pointer argument. */
1812 for (j = 0; j < nbst; ++j)
1814 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1815 continue;
1816 /* Store the first FILE* etc. argument type (whatever it is), and
1817 expect any subsequent declarations of file I/O etc. built-ins
1818 to refer to it rather than to fileptr_type_node etc. which is
1819 just void* (or const void*). */
1820 if (last_structptr_types[j])
1822 if (!comptypes (last_structptr_types[j], newtype))
1824 *argno = i;
1825 *strict = last_structptr_types[j];
1828 else
1829 last_structptr_types[j] = newtype;
1830 break;
1833 if (j == nbst && !comptypes (oldtype, newtype))
1835 if (POINTER_TYPE_P (oldtype))
1837 /* For incompatible pointers, only reject differences in
1838 the unqualified variants of the referenced types but
1839 consider differences in qualifiers as benign (report
1840 those to caller via *STRICT below). */
1841 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1842 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1843 if (!comptypes (oldref, newref))
1844 return NULL_TREE;
1847 if (!*strict)
1849 *argno = i;
1850 *strict = oldtype;
1854 oldargs = TREE_CHAIN (oldargs);
1855 newargs = TREE_CHAIN (newargs);
1858 tree trytype = build_function_type (newrettype, tryargs);
1860 /* Allow declaration to change transaction_safe attribute. */
1861 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1862 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1863 tree newattrs = TYPE_ATTRIBUTES (newtype);
1864 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1865 if (oldtsafe && !newtsafe)
1866 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1867 else if (newtsafe && !oldtsafe)
1868 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1869 NULL_TREE, oldattrs);
1871 return build_type_attribute_variant (trytype, oldattrs);
1874 /* Subroutine of diagnose_mismatched_decls. Check for function type
1875 mismatch involving an empty arglist vs a nonempty one and give clearer
1876 diagnostics. */
1877 static void
1878 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1879 tree newtype, tree oldtype)
1881 tree t;
1883 if (TREE_CODE (olddecl) != FUNCTION_DECL
1884 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1885 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1886 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1887 return;
1889 t = TYPE_ARG_TYPES (oldtype);
1890 if (t == NULL_TREE)
1891 t = TYPE_ARG_TYPES (newtype);
1892 for (; t; t = TREE_CHAIN (t))
1894 tree type = TREE_VALUE (t);
1896 if (TREE_CHAIN (t) == NULL_TREE
1897 && TYPE_MAIN_VARIANT (type) != void_type_node)
1899 inform (input_location, "a parameter list with an ellipsis "
1900 "cannot match an empty parameter name list declaration");
1901 break;
1904 if (c_type_promotes_to (type) != type)
1906 inform (input_location, "an argument type that has a default "
1907 "promotion cannot match an empty parameter name list "
1908 "declaration");
1909 break;
1914 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1915 old-style function definition, NEWDECL is a prototype declaration.
1916 Diagnose inconsistencies in the argument list. Returns TRUE if
1917 the prototype is compatible, FALSE if not. */
1918 static bool
1919 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1921 tree newargs, oldargs;
1922 int i;
1924 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1926 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1927 newargs = TYPE_ARG_TYPES (newtype);
1928 i = 1;
1930 for (;;)
1932 tree oldargtype = TREE_VALUE (oldargs);
1933 tree newargtype = TREE_VALUE (newargs);
1935 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1936 return false;
1938 oldargtype = (TYPE_ATOMIC (oldargtype)
1939 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1940 TYPE_QUAL_ATOMIC)
1941 : TYPE_MAIN_VARIANT (oldargtype));
1942 newargtype = (TYPE_ATOMIC (newargtype)
1943 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1944 TYPE_QUAL_ATOMIC)
1945 : TYPE_MAIN_VARIANT (newargtype));
1947 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1948 break;
1950 /* Reaching the end of just one list means the two decls don't
1951 agree on the number of arguments. */
1952 if (END_OF_ARGLIST (oldargtype))
1954 error ("prototype for %q+D declares more arguments "
1955 "than previous old-style definition", newdecl);
1956 return false;
1958 else if (END_OF_ARGLIST (newargtype))
1960 error ("prototype for %q+D declares fewer arguments "
1961 "than previous old-style definition", newdecl);
1962 return false;
1965 /* Type for passing arg must be consistent with that declared
1966 for the arg. */
1967 else if (!comptypes (oldargtype, newargtype))
1969 error ("prototype for %q+D declares argument %d"
1970 " with incompatible type",
1971 newdecl, i);
1972 return false;
1975 oldargs = TREE_CHAIN (oldargs);
1976 newargs = TREE_CHAIN (newargs);
1977 i++;
1980 /* If we get here, no errors were found, but do issue a warning
1981 for this poor-style construct. */
1982 warning (0, "prototype for %q+D follows non-prototype definition",
1983 newdecl);
1984 return true;
1985 #undef END_OF_ARGLIST
1988 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1989 first in a pair of mismatched declarations, using the diagnostic
1990 function DIAG. */
1991 static void
1992 locate_old_decl (tree decl)
1994 if (TREE_CODE (decl) == FUNCTION_DECL
1995 && fndecl_built_in_p (decl)
1996 && !C_DECL_DECLARED_BUILTIN (decl))
1998 else if (DECL_INITIAL (decl))
1999 inform (input_location,
2000 "previous definition of %q+D with type %qT",
2001 decl, TREE_TYPE (decl));
2002 else if (C_DECL_IMPLICIT (decl))
2003 inform (input_location,
2004 "previous implicit declaration of %q+D with type %qT",
2005 decl, TREE_TYPE (decl));
2006 else
2007 inform (input_location,
2008 "previous declaration of %q+D with type %qT",
2009 decl, TREE_TYPE (decl));
2012 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2013 Returns true if the caller should proceed to merge the two, false
2014 if OLDDECL should simply be discarded. As a side effect, issues
2015 all necessary diagnostics for invalid or poor-style combinations.
2016 If it returns true, writes the types of NEWDECL and OLDDECL to
2017 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2018 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2020 static bool
2021 diagnose_mismatched_decls (tree newdecl, tree olddecl,
2022 tree *newtypep, tree *oldtypep)
2024 tree newtype, oldtype;
2025 bool retval = true;
2027 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2028 && DECL_EXTERNAL (DECL))
2030 /* If we have error_mark_node for either decl or type, just discard
2031 the previous decl - we're in an error cascade already. */
2032 if (olddecl == error_mark_node || newdecl == error_mark_node)
2033 return false;
2034 *oldtypep = oldtype = TREE_TYPE (olddecl);
2035 *newtypep = newtype = TREE_TYPE (newdecl);
2036 if (oldtype == error_mark_node || newtype == error_mark_node)
2037 return false;
2039 /* Two different categories of symbol altogether. This is an error
2040 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2041 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2043 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2044 && fndecl_built_in_p (olddecl)
2045 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2047 auto_diagnostic_group d;
2048 error ("%q+D redeclared as different kind of symbol", newdecl);
2049 locate_old_decl (olddecl);
2051 else if (TREE_PUBLIC (newdecl))
2052 warning (OPT_Wbuiltin_declaration_mismatch,
2053 "built-in function %q+D declared as non-function",
2054 newdecl);
2055 else
2056 warning (OPT_Wshadow, "declaration of %q+D shadows "
2057 "a built-in function", newdecl);
2058 return false;
2061 /* Enumerators have no linkage, so may only be declared once in a
2062 given scope. */
2063 if (TREE_CODE (olddecl) == CONST_DECL)
2065 auto_diagnostic_group d;
2066 error ("redeclaration of enumerator %q+D", newdecl);
2067 locate_old_decl (olddecl);
2068 return false;
2071 bool pedwarned = false;
2072 bool warned = false;
2073 bool enum_and_int_p = false;
2074 auto_diagnostic_group d;
2076 int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2077 &enum_and_int_p);
2078 if (!comptypes_result)
2080 if (TREE_CODE (olddecl) == FUNCTION_DECL
2081 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2082 && !C_DECL_DECLARED_BUILTIN (olddecl))
2084 /* Accept "harmless" mismatches in function types such
2085 as missing qualifiers or int vs long when they're the same
2086 size. However, diagnose return and argument types that are
2087 incompatible according to language rules. */
2088 tree mismatch_expect;
2089 unsigned mismatch_argno;
2091 tree trytype = match_builtin_function_types (newtype, oldtype,
2092 &mismatch_expect,
2093 &mismatch_argno);
2095 if (trytype && comptypes (newtype, trytype))
2096 *oldtypep = oldtype = trytype;
2097 else
2099 /* If types don't match for a built-in, throw away the
2100 built-in. No point in calling locate_old_decl here, it
2101 won't print anything. */
2102 const char *header = header_for_builtin_fn (olddecl);
2103 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2104 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2105 "conflicting types for built-in function %q+D; "
2106 "expected %qT",
2107 newdecl, oldtype)
2108 && header)
2110 /* Suggest the right header to include as the preferred
2111 solution rather than the spelling of the declaration. */
2112 rich_location richloc (line_table, loc);
2113 maybe_add_include_fixit (&richloc, header, true);
2114 inform (&richloc,
2115 "%qD is declared in header %qs", olddecl, header);
2117 return false;
2120 if (mismatch_expect && extra_warnings)
2122 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2123 bool warned = false;
2124 if (mismatch_argno)
2125 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2126 "mismatch in argument %u type of built-in "
2127 "function %qD; expected %qT",
2128 mismatch_argno, newdecl, mismatch_expect);
2129 else
2130 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2131 "mismatch in return type of built-in "
2132 "function %qD; expected %qT",
2133 newdecl, mismatch_expect);
2134 const char *header = header_for_builtin_fn (olddecl);
2135 if (warned && header)
2137 rich_location richloc (line_table, newloc);
2138 maybe_add_include_fixit (&richloc, header, true);
2139 inform (&richloc,
2140 "%qD is declared in header %qs", olddecl, header);
2144 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2145 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2147 /* A conflicting function declaration for a predeclared
2148 function that isn't actually built in. Objective C uses
2149 these. The new declaration silently overrides everything
2150 but the volatility (i.e. noreturn) indication. See also
2151 below. FIXME: Make Objective C use normal builtins. */
2152 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2153 return false;
2155 /* Permit void foo (...) to match int foo (...) if the latter is
2156 the definition and implicit int was used. See
2157 c-torture/compile/920625-2.c. */
2158 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2159 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2160 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2161 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2163 pedwarned = pedwarn (input_location, 0,
2164 "conflicting types for %q+D", newdecl);
2165 /* Make sure we keep void as the return type. */
2166 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2167 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2169 /* Permit void foo (...) to match an earlier call to foo (...) with
2170 no declared type (thus, implicitly int). */
2171 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2172 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2173 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2174 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2176 pedwarned = pedwarn (input_location, 0,
2177 "conflicting types for %q+D; have %qT",
2178 newdecl, newtype);
2179 /* Make sure we keep void as the return type. */
2180 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2182 else
2184 int new_quals = TYPE_QUALS (newtype);
2185 int old_quals = TYPE_QUALS (oldtype);
2187 if (new_quals != old_quals)
2189 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2190 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2191 if (new_addr != old_addr)
2193 if (ADDR_SPACE_GENERIC_P (new_addr))
2194 error ("conflicting named address spaces (generic vs %s) "
2195 "for %q+D",
2196 c_addr_space_name (old_addr), newdecl);
2197 else if (ADDR_SPACE_GENERIC_P (old_addr))
2198 error ("conflicting named address spaces (%s vs generic) "
2199 "for %q+D",
2200 c_addr_space_name (new_addr), newdecl);
2201 else
2202 error ("conflicting named address spaces (%s vs %s) "
2203 "for %q+D",
2204 c_addr_space_name (new_addr),
2205 c_addr_space_name (old_addr),
2206 newdecl);
2209 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2210 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2211 error ("conflicting type qualifiers for %q+D", newdecl);
2213 else
2214 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2215 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2216 locate_old_decl (olddecl);
2217 return false;
2220 /* Warn about enum/integer type mismatches. They are compatible types
2221 (C2X 6.7.2.2/5), but may pose portability problems. */
2222 else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
2223 warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2224 OPT_Wenum_int_mismatch,
2225 "conflicting types for %q+D due to enum/integer "
2226 "mismatch; have %qT", newdecl, newtype);
2228 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2229 but silently ignore the redeclaration if either is in a system
2230 header. (Conflicting redeclarations were handled above.) This
2231 is allowed for C11 if the types are the same, not just
2232 compatible. */
2233 if (TREE_CODE (newdecl) == TYPE_DECL)
2235 bool types_different = false;
2237 comptypes_result
2238 = comptypes_check_different_types (oldtype, newtype, &types_different);
2240 if (comptypes_result != 1 || types_different)
2242 error ("redefinition of typedef %q+D with different type", newdecl);
2243 locate_old_decl (olddecl);
2244 return false;
2247 if (DECL_IN_SYSTEM_HEADER (newdecl)
2248 || DECL_IN_SYSTEM_HEADER (olddecl)
2249 || warning_suppressed_p (newdecl, OPT_Wpedantic)
2250 || warning_suppressed_p (olddecl, OPT_Wpedantic))
2251 return true; /* Allow OLDDECL to continue in use. */
2253 if (c_type_variably_modified_p (newtype))
2255 error ("redefinition of typedef %q+D with variably modified type",
2256 newdecl);
2257 locate_old_decl (olddecl);
2259 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2260 "redefinition of typedef %q+D", newdecl))
2261 locate_old_decl (olddecl);
2263 return true;
2266 /* Function declarations can either be 'static' or 'extern' (no
2267 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2268 can never conflict with each other on account of linkage
2269 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2270 gnu89 mode permits two definitions if one is 'extern inline' and
2271 one is not. The non- extern-inline definition supersedes the
2272 extern-inline definition. */
2274 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2276 /* If you declare a built-in function name as static, or
2277 define the built-in with an old-style definition (so we
2278 can't validate the argument list) the built-in definition is
2279 overridden, but optionally warn this was a bad choice of name. */
2280 if (fndecl_built_in_p (olddecl)
2281 && !C_DECL_DECLARED_BUILTIN (olddecl))
2283 if (!TREE_PUBLIC (newdecl)
2284 || (DECL_INITIAL (newdecl)
2285 && !prototype_p (TREE_TYPE (newdecl))))
2287 warning_at (DECL_SOURCE_LOCATION (newdecl),
2288 OPT_Wshadow, "declaration of %qD shadows "
2289 "a built-in function", newdecl);
2290 /* Discard the old built-in function. */
2291 return false;
2294 if (!prototype_p (TREE_TYPE (newdecl)))
2296 /* Set for built-ins that take no arguments. */
2297 bool func_void_args = false;
2298 if (tree at = TYPE_ARG_TYPES (oldtype))
2299 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2301 if (extra_warnings && !func_void_args)
2302 warning_at (DECL_SOURCE_LOCATION (newdecl),
2303 OPT_Wbuiltin_declaration_mismatch,
2304 "declaration of built-in function %qD without "
2305 "a prototype; expected %qT",
2306 newdecl, TREE_TYPE (olddecl));
2310 if (DECL_INITIAL (newdecl))
2312 if (DECL_INITIAL (olddecl))
2314 /* If both decls are in the same TU and the new declaration
2315 isn't overriding an extern inline reject the new decl.
2316 In c99, no overriding is allowed in the same translation
2317 unit. */
2318 if ((!DECL_EXTERN_INLINE (olddecl)
2319 || DECL_EXTERN_INLINE (newdecl)
2320 || (!flag_gnu89_inline
2321 && (!DECL_DECLARED_INLINE_P (olddecl)
2322 || !lookup_attribute ("gnu_inline",
2323 DECL_ATTRIBUTES (olddecl)))
2324 && (!DECL_DECLARED_INLINE_P (newdecl)
2325 || !lookup_attribute ("gnu_inline",
2326 DECL_ATTRIBUTES (newdecl))))
2328 && same_translation_unit_p (newdecl, olddecl))
2330 auto_diagnostic_group d;
2331 error ("redefinition of %q+D", newdecl);
2332 locate_old_decl (olddecl);
2333 return false;
2337 /* If we have a prototype after an old-style function definition,
2338 the argument types must be checked specially. */
2339 else if (DECL_INITIAL (olddecl)
2340 && !prototype_p (oldtype) && prototype_p (newtype)
2341 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2343 auto_diagnostic_group d;
2344 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2346 locate_old_decl (olddecl);
2347 return false;
2350 /* A non-static declaration (even an "extern") followed by a
2351 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2352 The same is true for a static forward declaration at block
2353 scope followed by a non-static declaration/definition at file
2354 scope. Static followed by non-static at the same scope is
2355 not undefined behavior, and is the most convenient way to get
2356 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2357 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2358 we do diagnose it if -Wtraditional. */
2359 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2361 /* Two exceptions to the rule. If olddecl is an extern
2362 inline, or a predeclared function that isn't actually
2363 built in, newdecl silently overrides olddecl. The latter
2364 occur only in Objective C; see also above. (FIXME: Make
2365 Objective C use normal builtins.) */
2366 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2367 && !DECL_EXTERN_INLINE (olddecl))
2369 auto_diagnostic_group d;
2370 error ("static declaration of %q+D follows "
2371 "non-static declaration", newdecl);
2372 locate_old_decl (olddecl);
2374 return false;
2376 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2378 if (DECL_CONTEXT (olddecl))
2380 auto_diagnostic_group d;
2381 error ("non-static declaration of %q+D follows "
2382 "static declaration", newdecl);
2383 locate_old_decl (olddecl);
2384 return false;
2386 else if (warn_traditional)
2388 warned |= warning (OPT_Wtraditional,
2389 "non-static declaration of %q+D "
2390 "follows static declaration", newdecl);
2394 /* Make sure gnu_inline attribute is either not present, or
2395 present on all inline decls. */
2396 if (DECL_DECLARED_INLINE_P (olddecl)
2397 && DECL_DECLARED_INLINE_P (newdecl))
2399 bool newa = lookup_attribute ("gnu_inline",
2400 DECL_ATTRIBUTES (newdecl)) != NULL;
2401 bool olda = lookup_attribute ("gnu_inline",
2402 DECL_ATTRIBUTES (olddecl)) != NULL;
2403 if (newa != olda)
2405 auto_diagnostic_group d;
2406 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2407 newa ? newdecl : olddecl);
2408 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2409 "but not here");
2413 else if (VAR_P (newdecl))
2415 /* Only variables can be thread-local, and all declarations must
2416 agree on this property. */
2417 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2419 /* Nothing to check. Since OLDDECL is marked threadprivate
2420 and NEWDECL does not have a thread-local attribute, we
2421 will merge the threadprivate attribute into NEWDECL. */
2424 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2426 auto_diagnostic_group d;
2427 if (DECL_THREAD_LOCAL_P (newdecl))
2428 error ("thread-local declaration of %q+D follows "
2429 "non-thread-local declaration", newdecl);
2430 else
2431 error ("non-thread-local declaration of %q+D follows "
2432 "thread-local declaration", newdecl);
2434 locate_old_decl (olddecl);
2435 return false;
2438 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2439 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2441 auto_diagnostic_group d;
2442 error ("redefinition of %q+D", newdecl);
2443 locate_old_decl (olddecl);
2444 return false;
2447 /* Objects declared at file scope: if the first declaration had
2448 external linkage (even if it was an external reference) the
2449 second must have external linkage as well, or the behavior is
2450 undefined. If the first declaration had internal linkage, then
2451 the second must too, or else be an external reference (in which
2452 case the composite declaration still has internal linkage).
2453 As for function declarations, we warn about the static-then-
2454 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2455 if (DECL_FILE_SCOPE_P (newdecl)
2456 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2458 if (DECL_EXTERNAL (newdecl))
2460 if (!DECL_FILE_SCOPE_P (olddecl))
2462 auto_diagnostic_group d;
2463 error ("extern declaration of %q+D follows "
2464 "declaration with no linkage", newdecl);
2465 locate_old_decl (olddecl);
2466 return false;
2468 else if (warn_traditional)
2470 warned |= warning (OPT_Wtraditional,
2471 "non-static declaration of %q+D "
2472 "follows static declaration", newdecl);
2475 else
2477 auto_diagnostic_group d;
2478 if (TREE_PUBLIC (newdecl))
2479 error ("non-static declaration of %q+D follows "
2480 "static declaration", newdecl);
2481 else
2482 error ("static declaration of %q+D follows "
2483 "non-static declaration", newdecl);
2485 locate_old_decl (olddecl);
2486 return false;
2489 /* Two objects with the same name declared at the same block
2490 scope must both be external references (6.7p3). */
2491 else if (!DECL_FILE_SCOPE_P (newdecl))
2493 if (DECL_EXTERNAL (newdecl))
2495 /* Extern with initializer at block scope, which will
2496 already have received an error. */
2498 else if (DECL_EXTERNAL (olddecl))
2500 auto_diagnostic_group d;
2501 error ("declaration of %q+D with no linkage follows "
2502 "extern declaration", newdecl);
2503 locate_old_decl (olddecl);
2505 else
2507 auto_diagnostic_group d;
2508 error ("redeclaration of %q+D with no linkage", newdecl);
2509 locate_old_decl (olddecl);
2512 return false;
2515 /* C++ does not permit a decl to appear multiple times at file
2516 scope. */
2517 if (warn_cxx_compat
2518 && DECL_FILE_SCOPE_P (newdecl)
2519 && !DECL_EXTERNAL (newdecl)
2520 && !DECL_EXTERNAL (olddecl))
2521 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2522 OPT_Wc___compat,
2523 ("duplicate declaration of %qD is "
2524 "invalid in C++"),
2525 newdecl);
2528 /* warnings */
2529 /* All decls must agree on a visibility. */
2530 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2531 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2532 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2534 warned |= warning (0, "redeclaration of %q+D with different visibility "
2535 "(old visibility preserved)", newdecl);
2538 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2539 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2540 else /* PARM_DECL, VAR_DECL */
2542 /* Redeclaration of a parameter is a constraint violation (this is
2543 not explicitly stated, but follows from C99 6.7p3 [no more than
2544 one declaration of the same identifier with no linkage in the
2545 same scope, except type tags] and 6.2.2p6 [parameters have no
2546 linkage]). We must check for a forward parameter declaration,
2547 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2548 an extension, the mandatory diagnostic for which is handled by
2549 mark_forward_parm_decls. */
2551 if (TREE_CODE (newdecl) == PARM_DECL
2552 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2554 auto_diagnostic_group d;
2555 error ("redefinition of parameter %q+D", newdecl);
2556 locate_old_decl (olddecl);
2557 return false;
2561 /* Optional warning for completely redundant decls. */
2562 if (!warned && !pedwarned
2563 && warn_redundant_decls
2564 /* Don't warn about a function declaration followed by a
2565 definition. */
2566 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2567 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2568 /* Don't warn about redundant redeclarations of builtins. */
2569 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2570 && !fndecl_built_in_p (newdecl)
2571 && fndecl_built_in_p (olddecl)
2572 && !C_DECL_DECLARED_BUILTIN (olddecl))
2573 /* Don't warn about an extern followed by a definition. */
2574 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2575 /* Don't warn about forward parameter decls. */
2576 && !(TREE_CODE (newdecl) == PARM_DECL
2577 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2578 /* Don't warn about a variable definition following a declaration. */
2579 && !(VAR_P (newdecl)
2580 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2582 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2583 newdecl);
2586 /* Report location of previous decl/defn. */
2587 if (warned || pedwarned)
2588 locate_old_decl (olddecl);
2590 #undef DECL_EXTERN_INLINE
2592 return retval;
2595 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2596 consistent with OLDDECL, but carries new information. Merge the
2597 new information into OLDDECL. This function issues no
2598 diagnostics. */
2600 static void
2601 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2603 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2604 && DECL_INITIAL (newdecl) != NULL_TREE);
2605 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2606 && prototype_p (TREE_TYPE (newdecl)));
2607 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2608 && prototype_p (TREE_TYPE (olddecl)));
2610 /* For real parm decl following a forward decl, rechain the old decl
2611 in its new location and clear TREE_ASM_WRITTEN (it's not a
2612 forward decl anymore). */
2613 if (TREE_CODE (newdecl) == PARM_DECL
2614 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2616 struct c_binding *b, **here;
2618 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2619 if ((*here)->decl == olddecl)
2620 goto found;
2621 gcc_unreachable ();
2623 found:
2624 b = *here;
2625 *here = b->prev;
2626 b->prev = current_scope->bindings;
2627 current_scope->bindings = b;
2629 TREE_ASM_WRITTEN (olddecl) = 0;
2632 DECL_ATTRIBUTES (newdecl)
2633 = targetm.merge_decl_attributes (olddecl, newdecl);
2635 /* For typedefs use the old type, as the new type's DECL_NAME points
2636 at newdecl, which will be ggc_freed. */
2637 if (TREE_CODE (newdecl) == TYPE_DECL)
2639 /* But NEWTYPE might have an attribute, honor that. */
2640 tree tem = newtype;
2641 newtype = oldtype;
2643 if (TYPE_USER_ALIGN (tem))
2645 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2646 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2647 TYPE_USER_ALIGN (newtype) = true;
2650 /* And remove the new type from the variants list. */
2651 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2653 tree remove = TREE_TYPE (newdecl);
2654 if (TYPE_MAIN_VARIANT (remove) == remove)
2656 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2657 /* If remove is the main variant, no need to remove that
2658 from the list. One of the DECL_ORIGINAL_TYPE
2659 variants, e.g. created for aligned attribute, might still
2660 refer to the newdecl TYPE_DECL though, so remove that one
2661 in that case. */
2662 if (DECL_ORIGINAL_TYPE (newdecl)
2663 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2664 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2665 t; t = TYPE_MAIN_VARIANT (t))
2666 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2668 TYPE_NEXT_VARIANT (t)
2669 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2670 break;
2673 else
2674 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2675 t = TYPE_NEXT_VARIANT (t))
2676 if (TYPE_NEXT_VARIANT (t) == remove)
2678 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2679 break;
2684 /* Merge the data types specified in the two decls. */
2685 TREE_TYPE (newdecl)
2686 = TREE_TYPE (olddecl)
2687 = composite_type (newtype, oldtype);
2689 /* Lay the type out, unless already done. */
2690 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2692 if (TREE_TYPE (newdecl) != error_mark_node)
2693 layout_type (TREE_TYPE (newdecl));
2694 if (TREE_CODE (newdecl) != FUNCTION_DECL
2695 && TREE_CODE (newdecl) != TYPE_DECL
2696 && TREE_CODE (newdecl) != CONST_DECL)
2697 layout_decl (newdecl, 0);
2699 else
2701 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2702 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2703 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2704 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2705 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2707 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2708 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2710 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2711 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2712 DECL_USER_ALIGN (newdecl) = 1;
2713 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2714 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2715 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2716 DECL_WARN_IF_NOT_ALIGN (olddecl));
2719 /* Keep the old rtl since we can safely use it. */
2720 if (HAS_RTL_P (olddecl))
2721 COPY_DECL_RTL (olddecl, newdecl);
2723 /* Merge the type qualifiers. */
2724 if (TREE_READONLY (newdecl))
2725 TREE_READONLY (olddecl) = 1;
2727 if (TREE_THIS_VOLATILE (newdecl))
2728 TREE_THIS_VOLATILE (olddecl) = 1;
2730 /* Merge deprecatedness. */
2731 if (TREE_DEPRECATED (newdecl))
2732 TREE_DEPRECATED (olddecl) = 1;
2734 /* Merge unavailability. */
2735 if (TREE_UNAVAILABLE (newdecl))
2736 TREE_UNAVAILABLE (olddecl) = 1;
2738 /* If a decl is in a system header and the other isn't, keep the one on the
2739 system header. Otherwise, keep source location of definition rather than
2740 declaration and of prototype rather than non-prototype unless that
2741 prototype is built-in. */
2742 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2743 && DECL_IN_SYSTEM_HEADER (olddecl)
2744 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2745 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2746 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2747 && DECL_IN_SYSTEM_HEADER (newdecl)
2748 && !DECL_IN_SYSTEM_HEADER (olddecl))
2749 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2750 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2751 && DECL_INITIAL (olddecl) != NULL_TREE)
2752 || (old_is_prototype && !new_is_prototype
2753 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2754 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2756 /* Merge the initialization information. */
2757 if (DECL_INITIAL (newdecl) == NULL_TREE)
2758 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2760 /* Merge 'constexpr' information. */
2761 if (VAR_P (olddecl) && VAR_P (newdecl))
2763 if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2764 C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2765 else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2766 C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2769 /* Merge the threadprivate attribute. */
2770 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2771 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2773 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2775 /* Copy the assembler name.
2776 Currently, it can only be defined in the prototype. */
2777 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2779 /* Use visibility of whichever declaration had it specified */
2780 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2782 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2783 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2786 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2788 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2789 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2790 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2791 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2792 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2793 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2794 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2795 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2796 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2797 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2798 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2799 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2800 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2801 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2804 /* Merge the storage class information. */
2805 merge_weak (newdecl, olddecl);
2807 /* For functions, static overrides non-static. */
2808 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2810 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2811 /* This is since we don't automatically
2812 copy the attributes of NEWDECL into OLDDECL. */
2813 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2814 /* If this clears `static', clear it in the identifier too. */
2815 if (!TREE_PUBLIC (olddecl))
2816 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2820 /* In c99, 'extern' declaration before (or after) 'inline' means this
2821 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2822 is present. */
2823 if (TREE_CODE (newdecl) == FUNCTION_DECL
2824 && !flag_gnu89_inline
2825 && (DECL_DECLARED_INLINE_P (newdecl)
2826 || DECL_DECLARED_INLINE_P (olddecl))
2827 && (!DECL_DECLARED_INLINE_P (newdecl)
2828 || !DECL_DECLARED_INLINE_P (olddecl)
2829 || !DECL_EXTERNAL (olddecl))
2830 && DECL_EXTERNAL (newdecl)
2831 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2832 && !current_function_decl)
2833 DECL_EXTERNAL (newdecl) = 0;
2835 /* An inline definition following a static declaration is not
2836 DECL_EXTERNAL. */
2837 if (new_is_definition
2838 && (DECL_DECLARED_INLINE_P (newdecl)
2839 || DECL_DECLARED_INLINE_P (olddecl))
2840 && !TREE_PUBLIC (olddecl))
2841 DECL_EXTERNAL (newdecl) = 0;
2843 if (DECL_EXTERNAL (newdecl))
2845 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2846 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2848 /* An extern decl does not override previous storage class. */
2849 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2850 if (!DECL_EXTERNAL (newdecl))
2852 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2853 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2856 else
2858 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2859 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2862 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2864 /* If we're redefining a function previously defined as extern
2865 inline, make sure we emit debug info for the inline before we
2866 throw it away, in case it was inlined into a function that
2867 hasn't been written out yet. */
2868 if (new_is_definition && DECL_INITIAL (olddecl))
2869 /* The new defn must not be inline. */
2870 DECL_UNINLINABLE (newdecl) = 1;
2871 else
2873 /* If either decl says `inline', this fn is inline, unless
2874 its definition was passed already. */
2875 if (DECL_DECLARED_INLINE_P (newdecl)
2876 || DECL_DECLARED_INLINE_P (olddecl))
2877 DECL_DECLARED_INLINE_P (newdecl) = 1;
2879 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2880 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2882 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2883 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2884 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2885 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2888 if (fndecl_built_in_p (olddecl))
2890 /* If redeclaring a builtin function, it stays built in.
2891 But it gets tagged as having been declared. */
2892 copy_decl_built_in_function (newdecl, olddecl);
2893 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2894 if (new_is_prototype)
2896 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2897 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2899 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2900 switch (fncode)
2902 /* If a compatible prototype of these builtin functions
2903 is seen, assume the runtime implements it with the
2904 expected semantics. */
2905 case BUILT_IN_STPCPY:
2906 if (builtin_decl_explicit_p (fncode))
2907 set_builtin_decl_implicit_p (fncode, true);
2908 break;
2909 default:
2910 if (builtin_decl_explicit_p (fncode))
2911 set_builtin_decl_declared_p (fncode, true);
2912 break;
2915 copy_attributes_to_builtin (newdecl);
2918 else
2919 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2920 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2923 /* Preserve function specific target and optimization options */
2924 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2925 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2926 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2927 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2929 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2930 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2931 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2932 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2934 /* Also preserve various other info from the definition. */
2935 if (!new_is_definition)
2937 tree t;
2938 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2939 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2940 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2941 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2942 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2943 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2944 DECL_CONTEXT (t) = newdecl;
2946 /* See if we've got a function to instantiate from. */
2947 if (DECL_SAVED_TREE (olddecl))
2948 DECL_ABSTRACT_ORIGIN (newdecl)
2949 = DECL_ABSTRACT_ORIGIN (olddecl);
2953 /* Merge the USED information. */
2954 if (TREE_USED (olddecl))
2955 TREE_USED (newdecl) = 1;
2956 else if (TREE_USED (newdecl))
2957 TREE_USED (olddecl) = 1;
2958 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2959 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2960 if (DECL_PRESERVE_P (olddecl))
2961 DECL_PRESERVE_P (newdecl) = 1;
2962 else if (DECL_PRESERVE_P (newdecl))
2963 DECL_PRESERVE_P (olddecl) = 1;
2965 /* Merge DECL_COMMON */
2966 if (VAR_P (olddecl) && VAR_P (newdecl)
2967 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2968 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2969 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2971 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2972 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2973 DECL_ARGUMENTS (if appropriate). */
2975 unsigned olddecl_uid = DECL_UID (olddecl);
2976 tree olddecl_context = DECL_CONTEXT (olddecl);
2977 tree olddecl_arguments = NULL;
2978 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2979 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2981 memcpy ((char *) olddecl + sizeof (struct tree_common),
2982 (char *) newdecl + sizeof (struct tree_common),
2983 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2984 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2985 switch (TREE_CODE (olddecl))
2987 case FUNCTION_DECL:
2988 case VAR_DECL:
2990 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2992 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2993 (char *) newdecl + sizeof (struct tree_decl_common),
2994 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2995 olddecl->decl_with_vis.symtab_node = snode;
2997 if ((DECL_EXTERNAL (olddecl)
2998 || TREE_PUBLIC (olddecl)
2999 || TREE_STATIC (olddecl))
3000 && DECL_SECTION_NAME (newdecl) != NULL)
3001 set_decl_section_name (olddecl, newdecl);
3003 /* This isn't quite correct for something like
3004 int __thread x attribute ((tls_model ("local-exec")));
3005 extern int __thread x;
3006 as we'll lose the "local-exec" model. */
3007 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3008 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3009 break;
3012 case FIELD_DECL:
3013 case PARM_DECL:
3014 case LABEL_DECL:
3015 case RESULT_DECL:
3016 case CONST_DECL:
3017 case TYPE_DECL:
3018 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3019 (char *) newdecl + sizeof (struct tree_decl_common),
3020 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3021 break;
3023 default:
3025 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3026 (char *) newdecl + sizeof (struct tree_decl_common),
3027 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3029 DECL_UID (olddecl) = olddecl_uid;
3030 DECL_CONTEXT (olddecl) = olddecl_context;
3031 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3032 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3035 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3036 so that encode_section_info has a chance to look at the new decl
3037 flags and attributes. */
3038 if (DECL_RTL_SET_P (olddecl)
3039 && (TREE_CODE (olddecl) == FUNCTION_DECL
3040 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3041 make_decl_rtl (olddecl);
3044 /* Handle when a new declaration NEWDECL has the same name as an old
3045 one OLDDECL in the same binding contour. Prints an error message
3046 if appropriate.
3048 If safely possible, alter OLDDECL to look like NEWDECL, and return
3049 true. Otherwise, return false. */
3051 static bool
3052 duplicate_decls (tree newdecl, tree olddecl)
3054 tree newtype = NULL, oldtype = NULL;
3056 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3058 /* Avoid `unused variable' and other warnings for OLDDECL. */
3059 suppress_warning (olddecl, OPT_Wunused);
3060 /* If the types are completely different, poison them both with
3061 error_mark_node. */
3062 if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3063 && olddecl != error_mark_node
3064 && seen_error ())
3066 if (TREE_CODE (olddecl) != FUNCTION_DECL)
3067 TREE_TYPE (olddecl) = error_mark_node;
3068 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3069 TREE_TYPE (newdecl) = error_mark_node;
3071 return false;
3074 merge_decls (newdecl, olddecl, newtype, oldtype);
3076 /* The NEWDECL will no longer be needed.
3078 Before releasing the node, be sure to remove function from symbol
3079 table that might have been inserted there to record comdat group.
3080 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3081 structure is shared in between NEWDECL and OLDECL. */
3082 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3083 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3084 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3086 struct symtab_node *snode = symtab_node::get (newdecl);
3087 if (snode)
3088 snode->remove ();
3090 ggc_free (newdecl);
3091 return true;
3095 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3096 static void
3097 warn_if_shadowing (tree new_decl)
3099 struct c_binding *b;
3101 /* Shadow warnings wanted? */
3102 if (!(warn_shadow
3103 || warn_shadow_local
3104 || warn_shadow_compatible_local)
3105 /* No shadow warnings for internally generated vars. */
3106 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3107 return;
3109 /* Is anything being shadowed? Invisible decls do not count. */
3110 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3111 if (b->decl && b->decl != new_decl && !b->invisible
3112 && (b->decl == error_mark_node
3113 || diagnostic_report_warnings_p (global_dc,
3114 DECL_SOURCE_LOCATION (b->decl))))
3116 tree old_decl = b->decl;
3118 if (old_decl == error_mark_node)
3120 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3121 "non-variable", new_decl);
3122 break;
3125 bool warned = false;
3126 auto_diagnostic_group d;
3127 if (TREE_CODE (old_decl) == PARM_DECL)
3129 enum opt_code warning_code;
3131 /* If '-Wshadow=compatible-local' is specified without other
3132 -Wshadow= flags, we will warn only when the types of the
3133 shadowing variable (i.e. new_decl) and the shadowed variable
3134 (old_decl) are compatible. */
3135 if (warn_shadow)
3136 warning_code = OPT_Wshadow;
3137 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3138 warning_code = OPT_Wshadow_compatible_local;
3139 else
3140 warning_code = OPT_Wshadow_local;
3141 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3142 "declaration of %qD shadows a parameter",
3143 new_decl);
3145 else if (DECL_FILE_SCOPE_P (old_decl))
3147 /* Do not warn if a variable shadows a function, unless
3148 the variable is a function or a pointer-to-function. */
3149 if (TREE_CODE (old_decl) == FUNCTION_DECL
3150 && TREE_CODE (new_decl) != FUNCTION_DECL
3151 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3152 continue;
3154 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3155 "declaration of %qD shadows a global "
3156 "declaration",
3157 new_decl);
3159 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3160 && fndecl_built_in_p (old_decl))
3162 warning (OPT_Wshadow, "declaration of %q+D shadows "
3163 "a built-in function", new_decl);
3164 break;
3166 else
3168 enum opt_code warning_code;
3170 /* If '-Wshadow=compatible-local' is specified without other
3171 -Wshadow= flags, we will warn only when the types of the
3172 shadowing variable (i.e. new_decl) and the shadowed variable
3173 (old_decl) are compatible. */
3174 if (warn_shadow)
3175 warning_code = OPT_Wshadow;
3176 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3177 warning_code = OPT_Wshadow_compatible_local;
3178 else
3179 warning_code = OPT_Wshadow_local;
3180 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3181 "declaration of %qD shadows a previous local",
3182 new_decl);
3185 if (warned)
3186 inform (DECL_SOURCE_LOCATION (old_decl),
3187 "shadowed declaration is here");
3189 break;
3193 /* Record a decl-node X as belonging to the current lexical scope.
3194 Check for errors (such as an incompatible declaration for the same
3195 name already seen in the same scope).
3197 Returns either X or an old decl for the same name.
3198 If an old decl is returned, it may have been smashed
3199 to agree with what X says. */
3201 tree
3202 pushdecl (tree x)
3204 tree name = DECL_NAME (x);
3205 struct c_scope *scope = current_scope;
3206 struct c_binding *b;
3207 bool nested = false;
3208 location_t locus = DECL_SOURCE_LOCATION (x);
3210 /* Must set DECL_CONTEXT for everything not at file scope or
3211 DECL_FILE_SCOPE_P won't work. Local externs don't count
3212 unless they have initializers (which generate code). */
3213 if (current_function_decl
3214 && (!VAR_OR_FUNCTION_DECL_P (x)
3215 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3216 DECL_CONTEXT (x) = current_function_decl;
3218 /* Anonymous decls are just inserted in the scope. */
3219 if (!name)
3221 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3222 locus);
3223 return x;
3226 /* First, see if there is another declaration with the same name in
3227 the current scope. If there is, duplicate_decls may do all the
3228 work for us. If duplicate_decls returns false, that indicates
3229 two incompatible decls in the same scope; we are to silently
3230 replace the old one (duplicate_decls has issued all appropriate
3231 diagnostics). In particular, we should not consider possible
3232 duplicates in the external scope, or shadowing. */
3233 b = I_SYMBOL_BINDING (name);
3234 if (b && B_IN_SCOPE (b, scope))
3236 struct c_binding *b_ext, *b_use;
3237 tree type = TREE_TYPE (x);
3238 tree visdecl = b->decl;
3239 tree vistype = TREE_TYPE (visdecl);
3240 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3241 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3242 b->inner_comp = false;
3243 b_use = b;
3244 b_ext = b;
3245 /* If this is an external linkage declaration, we should check
3246 for compatibility with the type in the external scope before
3247 setting the type at this scope based on the visible
3248 information only. */
3249 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3251 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3252 b_ext = b_ext->shadowed;
3253 if (b_ext)
3255 b_use = b_ext;
3256 if (b_use->u.type)
3257 TREE_TYPE (b_use->decl) = b_use->u.type;
3260 if (duplicate_decls (x, b_use->decl))
3262 if (b_use != b)
3264 /* Save the updated type in the external scope and
3265 restore the proper type for this scope. */
3266 tree thistype;
3267 if (comptypes (vistype, type))
3268 thistype = composite_type (vistype, type);
3269 else
3270 thistype = TREE_TYPE (b_use->decl);
3271 b_use->u.type = TREE_TYPE (b_use->decl);
3272 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3273 && fndecl_built_in_p (b_use->decl))
3274 thistype
3275 = build_type_attribute_variant (thistype,
3276 TYPE_ATTRIBUTES
3277 (b_use->u.type));
3278 TREE_TYPE (b_use->decl) = thistype;
3280 return b_use->decl;
3282 else
3283 goto skip_external_and_shadow_checks;
3286 /* All declarations with external linkage, and all external
3287 references, go in the external scope, no matter what scope is
3288 current. However, the binding in that scope is ignored for
3289 purposes of normal name lookup. A separate binding structure is
3290 created in the requested scope; this governs the normal
3291 visibility of the symbol.
3293 The binding in the externals scope is used exclusively for
3294 detecting duplicate declarations of the same object, no matter
3295 what scope they are in; this is what we do here. (C99 6.2.7p2:
3296 All declarations that refer to the same object or function shall
3297 have compatible type; otherwise, the behavior is undefined.)
3298 However, in Objective-C, we also want to detect declarations
3299 conflicting with those of the basic types. */
3300 if ((DECL_EXTERNAL (x) || scope == file_scope)
3301 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3303 tree type = TREE_TYPE (x);
3304 tree vistype = NULL_TREE;
3305 tree visdecl = NULL_TREE;
3306 bool type_saved = false;
3307 if (b && !B_IN_EXTERNAL_SCOPE (b)
3308 && VAR_OR_FUNCTION_DECL_P (b->decl)
3309 && DECL_FILE_SCOPE_P (b->decl))
3311 visdecl = b->decl;
3312 vistype = TREE_TYPE (visdecl);
3314 if (scope != file_scope
3315 && !DECL_IN_SYSTEM_HEADER (x))
3316 warning_at (locus, OPT_Wnested_externs,
3317 "nested extern declaration of %qD", x);
3319 while (b && !B_IN_EXTERNAL_SCOPE (b))
3321 /* If this decl might be modified, save its type. This is
3322 done here rather than when the decl is first bound
3323 because the type may change after first binding, through
3324 being completed or through attributes being added. If we
3325 encounter multiple such decls, only the first should have
3326 its type saved; the others will already have had their
3327 proper types saved and the types will not have changed as
3328 their scopes will not have been re-entered. */
3329 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3331 b->u.type = TREE_TYPE (b->decl);
3332 type_saved = true;
3334 if (B_IN_FILE_SCOPE (b)
3335 && VAR_P (b->decl)
3336 && TREE_STATIC (b->decl)
3337 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3338 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3339 && TREE_CODE (type) == ARRAY_TYPE
3340 && TYPE_DOMAIN (type)
3341 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3342 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3344 /* Array type completed in inner scope, which should be
3345 diagnosed if the completion does not have size 1 and
3346 it does not get completed in the file scope. */
3347 b->inner_comp = true;
3349 b = b->shadowed;
3352 /* If a matching external declaration has been found, set its
3353 type to the composite of all the types of that declaration.
3354 After the consistency checks, it will be reset to the
3355 composite of the visible types only. */
3356 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3357 && b->u.type)
3358 TREE_TYPE (b->decl) = b->u.type;
3360 /* The point of the same_translation_unit_p check here is,
3361 we want to detect a duplicate decl for a construct like
3362 foo() { extern bar(); } ... static bar(); but not if
3363 they are in different translation units. In any case,
3364 the static does not go in the externals scope. */
3365 if (b
3366 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3367 && duplicate_decls (x, b->decl))
3369 tree thistype;
3370 if (vistype)
3372 if (comptypes (vistype, type))
3373 thistype = composite_type (vistype, type);
3374 else
3375 thistype = TREE_TYPE (b->decl);
3377 else
3378 thistype = type;
3379 b->u.type = TREE_TYPE (b->decl);
3380 /* Propagate the type attributes to the decl. */
3381 thistype
3382 = build_type_attribute_variant (thistype,
3383 TYPE_ATTRIBUTES (b->u.type));
3384 TREE_TYPE (b->decl) = thistype;
3385 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3386 locus);
3387 return b->decl;
3389 else if (TREE_PUBLIC (x))
3391 if (visdecl && !b && duplicate_decls (x, visdecl))
3393 /* An external declaration at block scope referring to a
3394 visible entity with internal linkage. The composite
3395 type will already be correct for this scope, so we
3396 just need to fall through to make the declaration in
3397 this scope. */
3398 nested = true;
3399 x = visdecl;
3401 else
3403 bind (name, x, external_scope, /*invisible=*/true,
3404 /*nested=*/false, locus);
3405 nested = true;
3410 if (TREE_CODE (x) != PARM_DECL)
3411 warn_if_shadowing (x);
3413 skip_external_and_shadow_checks:
3414 if (TREE_CODE (x) == TYPE_DECL)
3416 /* So this is a typedef, set its underlying type. */
3417 set_underlying_type (x);
3419 /* If X is a typedef defined in the current function, record it
3420 for the purpose of implementing the -Wunused-local-typedefs
3421 warning. */
3422 record_locally_defined_typedef (x);
3425 bind (name, x, scope, /*invisible=*/false, nested, locus);
3427 /* If x's type is incomplete because it's based on a
3428 structure or union which has not yet been fully declared,
3429 attach it to that structure or union type, so we can go
3430 back and complete the variable declaration later, if the
3431 structure or union gets fully declared.
3433 If the input is erroneous, we can have error_mark in the type
3434 slot (e.g. "f(void a, ...)") - that doesn't count as an
3435 incomplete type. */
3436 if (TREE_TYPE (x) != error_mark_node
3437 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3439 tree element = TREE_TYPE (x);
3441 while (TREE_CODE (element) == ARRAY_TYPE)
3442 element = TREE_TYPE (element);
3443 element = TYPE_MAIN_VARIANT (element);
3445 if ((RECORD_OR_UNION_TYPE_P (element)
3446 || TREE_CODE (element) == ENUMERAL_TYPE)
3447 && (TREE_CODE (x) != TYPE_DECL
3448 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3449 && !COMPLETE_TYPE_P (element))
3450 C_TYPE_INCOMPLETE_VARS (element)
3451 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3453 return x;
3457 /* Issue a warning about implicit function declaration. ID is the function
3458 identifier, OLDDECL is a declaration of the function in a different scope,
3459 or NULL_TREE. */
3461 static void
3462 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3464 if (!warn_implicit_function_declaration)
3465 return;
3467 bool warned;
3468 auto_diagnostic_group d;
3469 name_hint hint;
3470 if (!olddecl)
3471 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3473 if (flag_isoc99)
3475 if (const char *suggestion = hint.suggestion ())
3477 gcc_rich_location richloc (loc);
3478 richloc.add_fixit_replace (suggestion);
3479 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3480 "implicit declaration of function %qE;"
3481 " did you mean %qs?",
3482 id, suggestion);
3484 else
3485 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3486 "implicit declaration of function %qE", id);
3488 else if (const char *suggestion = hint.suggestion ())
3490 gcc_rich_location richloc (loc);
3491 richloc.add_fixit_replace (suggestion);
3492 warned = warning_at
3493 (&richloc, OPT_Wimplicit_function_declaration,
3494 G_("implicit declaration of function %qE; did you mean %qs?"),
3495 id, suggestion);
3497 else
3498 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3499 G_("implicit declaration of function %qE"), id);
3501 if (warned)
3503 /* Whether the olddecl is an undeclared builtin function.
3504 locate_old_decl will not generate a diagnostic for those,
3505 so in that case we want to look elsewhere. */
3506 bool undeclared_builtin = (olddecl
3507 && TREE_CODE (olddecl) == FUNCTION_DECL
3508 && fndecl_built_in_p (olddecl)
3509 && !C_DECL_DECLARED_BUILTIN (olddecl));
3510 if (undeclared_builtin)
3512 const char *header = header_for_builtin_fn (olddecl);
3513 if (header)
3515 rich_location richloc (line_table, loc);
3516 maybe_add_include_fixit (&richloc, header, true);
3517 inform (&richloc,
3518 "include %qs or provide a declaration of %qE",
3519 header, id);
3522 else if (olddecl)
3523 locate_old_decl (olddecl);
3526 if (!warned)
3527 hint.suppress ();
3530 /* Return the name of the header file that declares built-in function
3531 FNDECL, or null if either we don't know or don't expect to see an
3532 explicit declaration. */
3534 static const char *
3535 header_for_builtin_fn (tree fndecl)
3537 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3538 return NULL;
3540 switch (DECL_FUNCTION_CODE (fndecl))
3542 CASE_FLT_FN (BUILT_IN_ACOS):
3543 CASE_FLT_FN (BUILT_IN_ACOSH):
3544 CASE_FLT_FN (BUILT_IN_ASIN):
3545 CASE_FLT_FN (BUILT_IN_ASINH):
3546 CASE_FLT_FN (BUILT_IN_ATAN):
3547 CASE_FLT_FN (BUILT_IN_ATANH):
3548 CASE_FLT_FN (BUILT_IN_ATAN2):
3549 CASE_FLT_FN (BUILT_IN_CBRT):
3550 CASE_FLT_FN (BUILT_IN_CEIL):
3551 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3552 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3553 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3554 CASE_FLT_FN (BUILT_IN_COS):
3555 CASE_FLT_FN (BUILT_IN_COSH):
3556 CASE_FLT_FN (BUILT_IN_ERF):
3557 CASE_FLT_FN (BUILT_IN_ERFC):
3558 CASE_FLT_FN (BUILT_IN_EXP):
3559 CASE_FLT_FN (BUILT_IN_EXP2):
3560 CASE_FLT_FN (BUILT_IN_EXPM1):
3561 CASE_FLT_FN (BUILT_IN_FABS):
3562 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3563 CASE_FLT_FN (BUILT_IN_FDIM):
3564 CASE_FLT_FN (BUILT_IN_FLOOR):
3565 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3566 CASE_FLT_FN (BUILT_IN_FMA):
3567 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3568 CASE_FLT_FN (BUILT_IN_FMAX):
3569 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3570 CASE_FLT_FN (BUILT_IN_FMIN):
3571 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3572 CASE_FLT_FN (BUILT_IN_FMOD):
3573 CASE_FLT_FN (BUILT_IN_FREXP):
3574 CASE_FLT_FN (BUILT_IN_HYPOT):
3575 CASE_FLT_FN (BUILT_IN_ILOGB):
3576 CASE_FLT_FN (BUILT_IN_LDEXP):
3577 CASE_FLT_FN (BUILT_IN_LGAMMA):
3578 CASE_FLT_FN (BUILT_IN_LLRINT):
3579 CASE_FLT_FN (BUILT_IN_LLROUND):
3580 CASE_FLT_FN (BUILT_IN_LOG):
3581 CASE_FLT_FN (BUILT_IN_LOG10):
3582 CASE_FLT_FN (BUILT_IN_LOG1P):
3583 CASE_FLT_FN (BUILT_IN_LOG2):
3584 CASE_FLT_FN (BUILT_IN_LOGB):
3585 CASE_FLT_FN (BUILT_IN_LRINT):
3586 CASE_FLT_FN (BUILT_IN_LROUND):
3587 CASE_FLT_FN (BUILT_IN_MODF):
3588 CASE_FLT_FN (BUILT_IN_NAN):
3589 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3590 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3591 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3592 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3593 CASE_FLT_FN (BUILT_IN_POW):
3594 CASE_FLT_FN (BUILT_IN_REMAINDER):
3595 CASE_FLT_FN (BUILT_IN_REMQUO):
3596 CASE_FLT_FN (BUILT_IN_RINT):
3597 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3598 CASE_FLT_FN (BUILT_IN_ROUND):
3599 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3600 CASE_FLT_FN (BUILT_IN_SCALBLN):
3601 CASE_FLT_FN (BUILT_IN_SCALBN):
3602 CASE_FLT_FN (BUILT_IN_SIN):
3603 CASE_FLT_FN (BUILT_IN_SINH):
3604 CASE_FLT_FN (BUILT_IN_SINCOS):
3605 CASE_FLT_FN (BUILT_IN_SQRT):
3606 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3607 CASE_FLT_FN (BUILT_IN_TAN):
3608 CASE_FLT_FN (BUILT_IN_TANH):
3609 CASE_FLT_FN (BUILT_IN_TGAMMA):
3610 CASE_FLT_FN (BUILT_IN_TRUNC):
3611 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3612 case BUILT_IN_ISINF:
3613 case BUILT_IN_ISNAN:
3614 return "<math.h>";
3615 CASE_FLT_FN (BUILT_IN_CABS):
3616 CASE_FLT_FN (BUILT_IN_CACOS):
3617 CASE_FLT_FN (BUILT_IN_CACOSH):
3618 CASE_FLT_FN (BUILT_IN_CARG):
3619 CASE_FLT_FN (BUILT_IN_CASIN):
3620 CASE_FLT_FN (BUILT_IN_CASINH):
3621 CASE_FLT_FN (BUILT_IN_CATAN):
3622 CASE_FLT_FN (BUILT_IN_CATANH):
3623 CASE_FLT_FN (BUILT_IN_CCOS):
3624 CASE_FLT_FN (BUILT_IN_CCOSH):
3625 CASE_FLT_FN (BUILT_IN_CEXP):
3626 CASE_FLT_FN (BUILT_IN_CIMAG):
3627 CASE_FLT_FN (BUILT_IN_CLOG):
3628 CASE_FLT_FN (BUILT_IN_CONJ):
3629 CASE_FLT_FN (BUILT_IN_CPOW):
3630 CASE_FLT_FN (BUILT_IN_CPROJ):
3631 CASE_FLT_FN (BUILT_IN_CREAL):
3632 CASE_FLT_FN (BUILT_IN_CSIN):
3633 CASE_FLT_FN (BUILT_IN_CSINH):
3634 CASE_FLT_FN (BUILT_IN_CSQRT):
3635 CASE_FLT_FN (BUILT_IN_CTAN):
3636 CASE_FLT_FN (BUILT_IN_CTANH):
3637 return "<complex.h>";
3638 case BUILT_IN_MEMCHR:
3639 case BUILT_IN_MEMCMP:
3640 case BUILT_IN_MEMCPY:
3641 case BUILT_IN_MEMMOVE:
3642 case BUILT_IN_MEMSET:
3643 case BUILT_IN_STRCAT:
3644 case BUILT_IN_STRCHR:
3645 case BUILT_IN_STRCMP:
3646 case BUILT_IN_STRCPY:
3647 case BUILT_IN_STRCSPN:
3648 case BUILT_IN_STRLEN:
3649 case BUILT_IN_STRNCAT:
3650 case BUILT_IN_STRNCMP:
3651 case BUILT_IN_STRNCPY:
3652 case BUILT_IN_STRPBRK:
3653 case BUILT_IN_STRRCHR:
3654 case BUILT_IN_STRSPN:
3655 case BUILT_IN_STRSTR:
3656 return "<string.h>";
3657 case BUILT_IN_FPRINTF:
3658 case BUILT_IN_PUTC:
3659 case BUILT_IN_FPUTC:
3660 case BUILT_IN_FPUTS:
3661 case BUILT_IN_FSCANF:
3662 case BUILT_IN_FWRITE:
3663 case BUILT_IN_PRINTF:
3664 case BUILT_IN_PUTCHAR:
3665 case BUILT_IN_PUTS:
3666 case BUILT_IN_SCANF:
3667 case BUILT_IN_SNPRINTF:
3668 case BUILT_IN_SPRINTF:
3669 case BUILT_IN_SSCANF:
3670 case BUILT_IN_VFPRINTF:
3671 case BUILT_IN_VFSCANF:
3672 case BUILT_IN_VPRINTF:
3673 case BUILT_IN_VSCANF:
3674 case BUILT_IN_VSNPRINTF:
3675 case BUILT_IN_VSPRINTF:
3676 case BUILT_IN_VSSCANF:
3677 return "<stdio.h>";
3678 case BUILT_IN_ISALNUM:
3679 case BUILT_IN_ISALPHA:
3680 case BUILT_IN_ISBLANK:
3681 case BUILT_IN_ISCNTRL:
3682 case BUILT_IN_ISDIGIT:
3683 case BUILT_IN_ISGRAPH:
3684 case BUILT_IN_ISLOWER:
3685 case BUILT_IN_ISPRINT:
3686 case BUILT_IN_ISPUNCT:
3687 case BUILT_IN_ISSPACE:
3688 case BUILT_IN_ISUPPER:
3689 case BUILT_IN_ISXDIGIT:
3690 case BUILT_IN_TOLOWER:
3691 case BUILT_IN_TOUPPER:
3692 return "<ctype.h>";
3693 case BUILT_IN_ISWALNUM:
3694 case BUILT_IN_ISWALPHA:
3695 case BUILT_IN_ISWBLANK:
3696 case BUILT_IN_ISWCNTRL:
3697 case BUILT_IN_ISWDIGIT:
3698 case BUILT_IN_ISWGRAPH:
3699 case BUILT_IN_ISWLOWER:
3700 case BUILT_IN_ISWPRINT:
3701 case BUILT_IN_ISWPUNCT:
3702 case BUILT_IN_ISWSPACE:
3703 case BUILT_IN_ISWUPPER:
3704 case BUILT_IN_ISWXDIGIT:
3705 case BUILT_IN_TOWLOWER:
3706 case BUILT_IN_TOWUPPER:
3707 return "<wctype.h>";
3708 case BUILT_IN_ABORT:
3709 case BUILT_IN_ABS:
3710 case BUILT_IN_CALLOC:
3711 case BUILT_IN_EXIT:
3712 case BUILT_IN_FREE:
3713 case BUILT_IN_LABS:
3714 case BUILT_IN_LLABS:
3715 case BUILT_IN_MALLOC:
3716 case BUILT_IN_REALLOC:
3717 case BUILT_IN__EXIT2:
3718 case BUILT_IN_ALIGNED_ALLOC:
3719 return "<stdlib.h>";
3720 case BUILT_IN_IMAXABS:
3721 return "<inttypes.h>";
3722 case BUILT_IN_STRFTIME:
3723 return "<time.h>";
3724 default:
3725 return NULL;
3729 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3730 function of type int (). */
3732 tree
3733 implicitly_declare (location_t loc, tree functionid)
3735 struct c_binding *b;
3736 tree decl = NULL_TREE;
3737 tree asmspec_tree;
3739 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3741 if (B_IN_SCOPE (b, external_scope))
3743 decl = b->decl;
3744 break;
3748 if (decl)
3750 if (TREE_CODE (decl) != FUNCTION_DECL)
3751 return decl;
3753 /* FIXME: Objective-C has weird not-really-builtin functions
3754 which are supposed to be visible automatically. They wind up
3755 in the external scope because they're pushed before the file
3756 scope gets created. Catch this here and rebind them into the
3757 file scope. */
3758 if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3760 bind (functionid, decl, file_scope,
3761 /*invisible=*/false, /*nested=*/true,
3762 DECL_SOURCE_LOCATION (decl));
3763 return decl;
3765 else
3767 tree newtype = default_function_type;
3768 if (b->u.type)
3769 TREE_TYPE (decl) = b->u.type;
3770 /* Implicit declaration of a function already declared
3771 (somehow) in a different scope, or as a built-in.
3772 If this is the first time this has happened, warn;
3773 then recycle the old declaration but with the new type. */
3774 if (!C_DECL_IMPLICIT (decl))
3776 implicit_decl_warning (loc, functionid, decl);
3777 C_DECL_IMPLICIT (decl) = 1;
3779 if (fndecl_built_in_p (decl))
3781 newtype = build_type_attribute_variant (newtype,
3782 TYPE_ATTRIBUTES
3783 (TREE_TYPE (decl)));
3784 if (!comptypes (newtype, TREE_TYPE (decl)))
3786 auto_diagnostic_group d;
3787 bool warned = warning_at (loc,
3788 OPT_Wbuiltin_declaration_mismatch,
3789 "incompatible implicit "
3790 "declaration of built-in "
3791 "function %qD", decl);
3792 /* See if we can hint which header to include. */
3793 const char *header = header_for_builtin_fn (decl);
3794 if (header != NULL && warned)
3796 rich_location richloc (line_table, loc);
3797 maybe_add_include_fixit (&richloc, header, true);
3798 inform (&richloc,
3799 "include %qs or provide a declaration of %qD",
3800 header, decl);
3802 newtype = TREE_TYPE (decl);
3805 else
3807 if (!comptypes (newtype, TREE_TYPE (decl)))
3809 auto_diagnostic_group d;
3810 error_at (loc, "incompatible implicit declaration of "
3811 "function %qD", decl);
3812 locate_old_decl (decl);
3815 b->u.type = TREE_TYPE (decl);
3816 TREE_TYPE (decl) = newtype;
3817 bind (functionid, decl, current_scope,
3818 /*invisible=*/false, /*nested=*/true,
3819 DECL_SOURCE_LOCATION (decl));
3820 return decl;
3824 /* Not seen before. */
3825 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3826 DECL_EXTERNAL (decl) = 1;
3827 TREE_PUBLIC (decl) = 1;
3828 C_DECL_IMPLICIT (decl) = 1;
3829 implicit_decl_warning (loc, functionid, 0);
3830 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3831 if (asmspec_tree)
3832 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3834 /* C89 says implicit declarations are in the innermost block.
3835 So we record the decl in the standard fashion. */
3836 decl = pushdecl (decl);
3838 /* No need to call objc_check_decl here - it's a function type. */
3839 rest_of_decl_compilation (decl, 0, 0);
3841 /* Write a record describing this implicit function declaration
3842 to the prototypes file (if requested). */
3843 gen_aux_info_record (decl, 0, 1, 0);
3845 /* Possibly apply some default attributes to this implicit declaration. */
3846 decl_attributes (&decl, NULL_TREE, 0);
3848 return decl;
3851 /* Issue an error message for a reference to an undeclared variable
3852 ID, including a reference to a builtin outside of function-call
3853 context. Establish a binding of the identifier to error_mark_node
3854 in an appropriate scope, which will suppress further errors for the
3855 same identifier. The error message should be given location LOC. */
3856 void
3857 undeclared_variable (location_t loc, tree id)
3859 static bool already = false;
3860 struct c_scope *scope;
3862 auto_diagnostic_group d;
3863 if (current_function_decl == NULL_TREE)
3865 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3866 if (const char *suggestion = guessed_id.suggestion ())
3868 gcc_rich_location richloc (loc);
3869 richloc.add_fixit_replace (suggestion);
3870 error_at (&richloc,
3871 "%qE undeclared here (not in a function);"
3872 " did you mean %qs?",
3873 id, suggestion);
3875 else
3876 error_at (loc, "%qE undeclared here (not in a function)", id);
3877 scope = current_scope;
3879 else
3881 if (!objc_diagnose_private_ivar (id))
3883 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3884 if (const char *suggestion = guessed_id.suggestion ())
3886 gcc_rich_location richloc (loc);
3887 richloc.add_fixit_replace (suggestion);
3888 error_at (&richloc,
3889 "%qE undeclared (first use in this function);"
3890 " did you mean %qs?",
3891 id, suggestion);
3893 else
3894 error_at (loc, "%qE undeclared (first use in this function)", id);
3896 if (!already)
3898 inform (loc, "each undeclared identifier is reported only"
3899 " once for each function it appears in");
3900 already = true;
3903 /* If we are parsing old-style parameter decls, current_function_decl
3904 will be nonnull but current_function_scope will be null. */
3905 scope = current_function_scope ? current_function_scope : current_scope;
3907 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3908 UNKNOWN_LOCATION);
3911 /* Subroutine of lookup_label, declare_label, define_label: construct a
3912 LABEL_DECL with all the proper frills. Also create a struct
3913 c_label_vars initialized for the current scope. */
3915 static tree
3916 make_label (location_t location, tree name, bool defining,
3917 struct c_label_vars **p_label_vars)
3919 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3920 DECL_CONTEXT (label) = current_function_decl;
3921 SET_DECL_MODE (label, VOIDmode);
3923 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3924 label_vars->shadowed = NULL;
3925 set_spot_bindings (&label_vars->label_bindings, defining);
3926 label_vars->decls_in_scope = make_tree_vector ();
3927 label_vars->gotos = NULL;
3928 *p_label_vars = label_vars;
3930 return label;
3933 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3934 Create one if none exists so far for the current function.
3935 This is called when a label is used in a goto expression or
3936 has its address taken. */
3938 tree
3939 lookup_label (tree name)
3941 tree label;
3942 struct c_label_vars *label_vars;
3944 if (current_function_scope == 0)
3946 error ("label %qE referenced outside of any function", name);
3947 return NULL_TREE;
3950 /* Use a label already defined or ref'd with this name, but not if
3951 it is inherited from a containing function and wasn't declared
3952 using __label__. */
3953 label = I_LABEL_DECL (name);
3954 if (label && (DECL_CONTEXT (label) == current_function_decl
3955 || C_DECLARED_LABEL_FLAG (label)))
3957 /* If the label has only been declared, update its apparent
3958 location to point here, for better diagnostics if it
3959 turns out not to have been defined. */
3960 if (DECL_INITIAL (label) == NULL_TREE)
3961 DECL_SOURCE_LOCATION (label) = input_location;
3962 return label;
3965 /* No label binding for that identifier; make one. */
3966 label = make_label (input_location, name, false, &label_vars);
3968 /* Ordinary labels go in the current function scope. */
3969 bind_label (name, label, current_function_scope, label_vars);
3971 return label;
3974 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3975 to LABEL. */
3977 static void
3978 warn_about_goto (location_t goto_loc, tree label, tree decl)
3980 auto_diagnostic_group d;
3981 if (c_type_variably_modified_p (TREE_TYPE (decl)))
3982 error_at (goto_loc,
3983 "jump into scope of identifier with variably modified type");
3984 else
3985 if (!warning_at (goto_loc, OPT_Wjump_misses_init,
3986 "jump skips variable initialization"))
3987 return;
3988 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3989 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3992 /* Look up a label because of a goto statement. This is like
3993 lookup_label, but also issues any appropriate warnings. */
3995 tree
3996 lookup_label_for_goto (location_t loc, tree name)
3998 tree label;
3999 struct c_label_vars *label_vars;
4000 unsigned int ix;
4001 tree decl;
4003 label = lookup_label (name);
4004 if (label == NULL_TREE)
4005 return NULL_TREE;
4007 /* If we are jumping to a different function, we can't issue any
4008 useful warnings. */
4009 if (DECL_CONTEXT (label) != current_function_decl)
4011 gcc_assert (C_DECLARED_LABEL_FLAG (label));
4012 return label;
4015 label_vars = I_LABEL_BINDING (name)->u.label;
4017 /* If the label has not yet been defined, then push this goto on a
4018 list for possible later warnings. */
4019 if (label_vars->label_bindings.scope == NULL)
4021 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4023 g->loc = loc;
4024 set_spot_bindings (&g->goto_bindings, true);
4025 vec_safe_push (label_vars->gotos, g);
4026 return label;
4029 /* If there are any decls in label_vars->decls_in_scope, then this
4030 goto has missed the declaration of the decl. This happens for a
4031 case like
4032 int i = 1;
4033 lab:
4035 goto lab;
4036 Issue a warning or error. */
4037 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4038 warn_about_goto (loc, label, decl);
4040 if (label_vars->label_bindings.left_stmt_expr)
4042 auto_diagnostic_group d;
4043 error_at (loc, "jump into statement expression");
4044 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4047 return label;
4050 /* Make a label named NAME in the current function, shadowing silently
4051 any that may be inherited from containing functions or containing
4052 scopes. This is called for __label__ declarations. */
4054 tree
4055 declare_label (tree name)
4057 struct c_binding *b = I_LABEL_BINDING (name);
4058 tree label;
4059 struct c_label_vars *label_vars;
4061 /* Check to make sure that the label hasn't already been declared
4062 at this scope */
4063 if (b && B_IN_CURRENT_SCOPE (b))
4065 auto_diagnostic_group d;
4066 error ("duplicate label declaration %qE", name);
4067 locate_old_decl (b->decl);
4069 /* Just use the previous declaration. */
4070 return b->decl;
4073 label = make_label (input_location, name, false, &label_vars);
4074 C_DECLARED_LABEL_FLAG (label) = 1;
4076 /* Declared labels go in the current scope. */
4077 bind_label (name, label, current_scope, label_vars);
4079 return label;
4082 /* When we define a label, issue any appropriate warnings if there are
4083 any gotos earlier in the function which jump to this label. */
4085 static void
4086 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4088 unsigned int ix;
4089 struct c_goto_bindings *g;
4091 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4093 struct c_binding *b;
4094 struct c_scope *scope;
4096 /* We have a goto to this label. The goto is going forward. In
4097 g->scope, the goto is going to skip any binding which was
4098 defined after g->bindings_in_scope. */
4099 if (g->goto_bindings.scope->has_jump_unsafe_decl)
4101 for (b = g->goto_bindings.scope->bindings;
4102 b != g->goto_bindings.bindings_in_scope;
4103 b = b->prev)
4105 if (decl_jump_unsafe (b->decl))
4106 warn_about_goto (g->loc, label, b->decl);
4110 /* We also need to warn about decls defined in any scopes
4111 between the scope of the label and the scope of the goto. */
4112 for (scope = label_vars->label_bindings.scope;
4113 scope != g->goto_bindings.scope;
4114 scope = scope->outer)
4116 gcc_assert (scope != NULL);
4117 if (scope->has_jump_unsafe_decl)
4119 if (scope == label_vars->label_bindings.scope)
4120 b = label_vars->label_bindings.bindings_in_scope;
4121 else
4122 b = scope->bindings;
4123 for (; b != NULL; b = b->prev)
4125 if (decl_jump_unsafe (b->decl))
4126 warn_about_goto (g->loc, label, b->decl);
4131 if (g->goto_bindings.stmt_exprs > 0)
4133 auto_diagnostic_group d;
4134 error_at (g->loc, "jump into statement expression");
4135 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4136 label);
4140 /* Now that the label is defined, we will issue warnings about
4141 subsequent gotos to this label when we see them. */
4142 vec_safe_truncate (label_vars->gotos, 0);
4143 label_vars->gotos = NULL;
4146 /* Define a label, specifying the location in the source file.
4147 Return the LABEL_DECL node for the label, if the definition is valid.
4148 Otherwise return NULL_TREE. */
4150 tree
4151 define_label (location_t location, tree name)
4153 /* Find any preexisting label with this name. It is an error
4154 if that label has already been defined in this function, or
4155 if there is a containing function with a declared label with
4156 the same name. */
4157 tree label = I_LABEL_DECL (name);
4159 if (label
4160 && ((DECL_CONTEXT (label) == current_function_decl
4161 && DECL_INITIAL (label) != NULL_TREE)
4162 || (DECL_CONTEXT (label) != current_function_decl
4163 && C_DECLARED_LABEL_FLAG (label))))
4165 auto_diagnostic_group d;
4166 error_at (location, "duplicate label %qD", label);
4167 locate_old_decl (label);
4168 return NULL_TREE;
4170 else if (label && DECL_CONTEXT (label) == current_function_decl)
4172 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4174 /* The label has been used or declared already in this function,
4175 but not defined. Update its location to point to this
4176 definition. */
4177 DECL_SOURCE_LOCATION (label) = location;
4178 set_spot_bindings (&label_vars->label_bindings, true);
4180 /* Issue warnings as required about any goto statements from
4181 earlier in the function. */
4182 check_earlier_gotos (label, label_vars);
4184 else
4186 struct c_label_vars *label_vars;
4188 /* No label binding for that identifier; make one. */
4189 label = make_label (location, name, true, &label_vars);
4191 /* Ordinary labels go in the current function scope. */
4192 bind_label (name, label, current_function_scope, label_vars);
4195 if (!in_system_header_at (input_location) && lookup_name (name))
4196 warning_at (location, OPT_Wtraditional,
4197 "traditional C lacks a separate namespace "
4198 "for labels, identifier %qE conflicts", name);
4200 /* Mark label as having been defined. */
4201 DECL_INITIAL (label) = error_mark_node;
4202 return label;
4205 /* Get the bindings for a new switch statement. This is used to issue
4206 warnings as appropriate for jumps from the switch to case or
4207 default labels. */
4209 struct c_spot_bindings *
4210 c_get_switch_bindings (void)
4212 struct c_spot_bindings *switch_bindings;
4214 switch_bindings = XNEW (struct c_spot_bindings);
4215 set_spot_bindings (switch_bindings, true);
4216 return switch_bindings;
4219 void
4220 c_release_switch_bindings (struct c_spot_bindings *bindings)
4222 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4223 XDELETE (bindings);
4226 /* This is called at the point of a case or default label to issue
4227 warnings about decls as needed. It returns true if it found an
4228 error, not just a warning. */
4230 bool
4231 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4232 location_t switch_loc, location_t case_loc)
4234 bool saw_error;
4235 struct c_scope *scope;
4237 saw_error = false;
4238 for (scope = current_scope;
4239 scope != switch_bindings->scope;
4240 scope = scope->outer)
4242 struct c_binding *b;
4244 gcc_assert (scope != NULL);
4246 if (!scope->has_jump_unsafe_decl)
4247 continue;
4249 for (b = scope->bindings; b != NULL; b = b->prev)
4251 if (decl_jump_unsafe (b->decl))
4253 auto_diagnostic_group d;
4254 bool emitted;
4255 if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4257 saw_error = true;
4258 error_at (case_loc,
4259 ("switch jumps into scope of identifier with "
4260 "variably modified type"));
4261 emitted = true;
4263 else
4264 emitted
4265 = warning_at (case_loc, OPT_Wjump_misses_init,
4266 "switch jumps over variable initialization");
4267 if (emitted)
4269 inform (switch_loc, "switch starts here");
4270 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4271 b->decl);
4277 if (switch_bindings->stmt_exprs > 0)
4279 saw_error = true;
4280 auto_diagnostic_group d;
4281 error_at (case_loc, "switch jumps into statement expression");
4282 inform (switch_loc, "switch starts here");
4285 return saw_error;
4288 /* Given NAME, an IDENTIFIER_NODE,
4289 return the structure (or union or enum) definition for that name.
4290 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4291 CODE says which kind of type the caller wants;
4292 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4293 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4294 location where the tag was defined.
4295 If the wrong kind of type is found, an error is reported. */
4297 static tree
4298 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4299 location_t *ploc)
4301 struct c_binding *b = I_TAG_BINDING (name);
4302 bool thislevel = false;
4304 if (!b || !b->decl)
4305 return NULL_TREE;
4307 /* We only care about whether it's in this level if
4308 thislevel_only was set or it might be a type clash. */
4309 if (thislevel_only || TREE_CODE (b->decl) != code)
4311 /* For our purposes, a tag in the external scope is the same as
4312 a tag in the file scope. (Primarily relevant to Objective-C
4313 and its builtin structure tags, which get pushed before the
4314 file scope is created.) */
4315 if (B_IN_CURRENT_SCOPE (b)
4316 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4317 thislevel = true;
4320 if (thislevel_only && !thislevel)
4321 return NULL_TREE;
4323 if (TREE_CODE (b->decl) != code)
4325 /* Definition isn't the kind we were looking for. */
4326 pending_invalid_xref = name;
4327 pending_invalid_xref_location = input_location;
4329 /* If in the same binding level as a declaration as a tag
4330 of a different type, this must not be allowed to
4331 shadow that tag, so give the error immediately.
4332 (For example, "struct foo; union foo;" is invalid.) */
4333 if (thislevel)
4334 pending_xref_error ();
4337 if (ploc != NULL)
4338 *ploc = b->locus;
4340 return b->decl;
4343 /* Return true if a definition exists for NAME with code CODE. */
4345 bool
4346 tag_exists_p (enum tree_code code, tree name)
4348 struct c_binding *b = I_TAG_BINDING (name);
4350 if (b == NULL || b->decl == NULL_TREE)
4351 return false;
4352 return TREE_CODE (b->decl) == code;
4355 /* Print an error message now
4356 for a recent invalid struct, union or enum cross reference.
4357 We don't print them immediately because they are not invalid
4358 when used in the `struct foo;' construct for shadowing. */
4360 void
4361 pending_xref_error (void)
4363 if (pending_invalid_xref != NULL_TREE)
4364 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4365 pending_invalid_xref);
4366 pending_invalid_xref = NULL_TREE;
4370 /* Look up NAME in the current scope and its superiors
4371 in the namespace of variables, functions and typedefs.
4372 Return a ..._DECL node of some kind representing its definition,
4373 or return NULL_TREE if it is undefined. */
4375 tree
4376 lookup_name (tree name)
4378 struct c_binding *b = I_SYMBOL_BINDING (name);
4379 if (b && !b->invisible)
4381 maybe_record_typedef_use (b->decl);
4382 return b->decl;
4384 return NULL_TREE;
4387 /* Similar to `lookup_name' but look only at the indicated scope. */
4389 static tree
4390 lookup_name_in_scope (tree name, struct c_scope *scope)
4392 struct c_binding *b;
4394 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4395 if (B_IN_SCOPE (b, scope))
4396 return b->decl;
4397 return NULL_TREE;
4400 /* Look for the closest match for NAME within the currently valid
4401 scopes.
4403 This finds the identifier with the lowest Levenshtein distance to
4404 NAME. If there are multiple candidates with equal minimal distance,
4405 the first one found is returned. Scopes are searched from innermost
4406 outwards, and within a scope in reverse order of declaration, thus
4407 benefiting candidates "near" to the current scope.
4409 The function also looks for similar macro names to NAME, since a
4410 misspelled macro name will not be expanded, and hence looks like an
4411 identifier to the C frontend.
4413 It also looks for start_typename keywords, to detect "singed" vs "signed"
4414 typos.
4416 Use LOC for any deferred diagnostics. */
4418 name_hint
4419 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4421 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4423 /* First, try some well-known names in the C standard library, in case
4424 the user forgot a #include. */
4425 const char *header_hint
4426 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4428 if (header_hint)
4429 return name_hint (NULL,
4430 new suggest_missing_header (loc,
4431 IDENTIFIER_POINTER (name),
4432 header_hint));
4434 /* Only suggest names reserved for the implementation if NAME begins
4435 with an underscore. */
4436 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4438 best_match<tree, tree> bm (name);
4440 /* Look within currently valid scopes. */
4441 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4442 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4444 if (!binding->id || binding->invisible)
4445 continue;
4446 if (binding->decl == error_mark_node)
4447 continue;
4448 /* Don't use bindings from implicitly declared functions,
4449 as they were likely misspellings themselves. */
4450 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4451 if (C_DECL_IMPLICIT (binding->decl))
4452 continue;
4453 /* Don't suggest names that are reserved for use by the
4454 implementation, unless NAME began with an underscore. */
4455 if (!consider_implementation_names)
4457 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4458 if (name_reserved_for_implementation_p (suggestion_str))
4459 continue;
4461 switch (kind)
4463 case FUZZY_LOOKUP_TYPENAME:
4464 if (TREE_CODE (binding->decl) != TYPE_DECL)
4465 continue;
4466 break;
4468 case FUZZY_LOOKUP_FUNCTION_NAME:
4469 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4471 /* Allow function pointers. */
4472 if ((VAR_P (binding->decl)
4473 || TREE_CODE (binding->decl) == PARM_DECL)
4474 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4475 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4476 == FUNCTION_TYPE))
4477 break;
4478 continue;
4480 break;
4482 default:
4483 break;
4485 bm.consider (binding->id);
4488 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4490 x = SOME_OTHER_MACRO (y);
4491 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4492 as a misspelled identifier.
4494 Use the best distance so far so that a candidate is only set if
4495 a macro is better than anything so far. This allows early rejection
4496 (without calculating the edit distance) of macro names that must have
4497 distance >= bm.get_best_distance (), and means that we only get a
4498 non-NULL result for best_macro_match if it's better than any of
4499 the identifiers already checked, which avoids needless creation
4500 of identifiers for macro hashnodes. */
4501 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4502 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4503 /* If a macro is the closest so far to NAME, use it, creating an
4504 identifier tree node for it. */
4505 if (best_macro)
4507 const char *id = (const char *)best_macro->ident.str;
4508 tree macro_as_identifier
4509 = get_identifier_with_length (id, best_macro->ident.len);
4510 bm.set_best_so_far (macro_as_identifier,
4511 bmm.get_best_distance (),
4512 bmm.get_best_candidate_length ());
4515 /* Try the "start_typename" keywords to detect
4516 "singed" vs "signed" typos. */
4517 if (kind == FUZZY_LOOKUP_TYPENAME)
4519 for (unsigned i = 0; i < num_c_common_reswords; i++)
4521 const c_common_resword *resword = &c_common_reswords[i];
4522 if (!c_keyword_starts_typename (resword->rid))
4523 continue;
4524 tree resword_identifier = ridpointers [resword->rid];
4525 if (!resword_identifier)
4526 continue;
4527 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4528 bm.consider (resword_identifier);
4532 tree best = bm.get_best_meaningful_candidate ();
4533 if (best)
4534 return name_hint (IDENTIFIER_POINTER (best), NULL);
4535 else
4536 return name_hint (NULL, NULL);
4540 /* Handle the standard [[nodiscard]] attribute. */
4542 static tree
4543 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4544 int /*flags*/, bool *no_add_attrs)
4546 if (TREE_CODE (*node) == FUNCTION_DECL)
4548 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4549 warning_at (DECL_SOURCE_LOCATION (*node),
4550 OPT_Wattributes, "%qE attribute applied to %qD with void "
4551 "return type", name, *node);
4553 else if (RECORD_OR_UNION_TYPE_P (*node)
4554 || TREE_CODE (*node) == ENUMERAL_TYPE)
4555 /* OK */;
4556 else
4558 pedwarn (input_location,
4559 OPT_Wattributes, "%qE attribute can only be applied to "
4560 "functions or to structure, union or enumeration types", name);
4561 *no_add_attrs = true;
4563 return NULL_TREE;
4566 /* Handle the standard [[noreturn]] attribute. */
4568 static tree
4569 handle_std_noreturn_attribute (tree *node, tree name, tree args,
4570 int flags, bool *no_add_attrs)
4572 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4573 only applies to functions, not function pointers. */
4574 if (TREE_CODE (*node) == FUNCTION_DECL)
4575 return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4576 else
4578 pedwarn (input_location, OPT_Wattributes,
4579 "standard %qE attribute can only be applied to functions",
4580 name);
4581 *no_add_attrs = true;
4582 return NULL_TREE;
4586 /* Table of supported standard (C2x) attributes. */
4587 const struct attribute_spec std_attribute_table[] =
4589 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4590 affects_type_identity, handler, exclude } */
4591 { "_Noreturn", 0, 0, false, false, false, false,
4592 handle_std_noreturn_attribute, NULL },
4593 { "deprecated", 0, 1, false, false, false, false,
4594 handle_deprecated_attribute, NULL },
4595 { "fallthrough", 0, 0, false, false, false, false,
4596 handle_fallthrough_attribute, NULL },
4597 { "maybe_unused", 0, 0, false, false, false, false,
4598 handle_unused_attribute, NULL },
4599 { "nodiscard", 0, 1, false, false, false, false,
4600 handle_nodiscard_attribute, NULL },
4601 { "noreturn", 0, 0, false, false, false, false,
4602 handle_std_noreturn_attribute, NULL },
4603 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4606 /* Create the predefined scalar types of C,
4607 and some nodes representing standard constants (0, 1, (void *) 0).
4608 Initialize the global scope.
4609 Make definitions for built-in primitive functions. */
4611 void
4612 c_init_decl_processing (void)
4614 location_t save_loc = input_location;
4616 /* Initialize reserved words for parser. */
4617 c_parse_init ();
4619 register_scoped_attributes (std_attribute_table, NULL);
4621 current_function_decl = NULL_TREE;
4623 gcc_obstack_init (&parser_obstack);
4625 /* Make the externals scope. */
4626 push_scope ();
4627 external_scope = current_scope;
4629 /* Declarations from c_common_nodes_and_builtins must not be associated
4630 with this input file, lest we get differences between using and not
4631 using preprocessed headers. */
4632 input_location = BUILTINS_LOCATION;
4634 c_common_nodes_and_builtins ();
4636 /* In C, comparisons and TRUTH_* expressions have type int. */
4637 truthvalue_type_node = integer_type_node;
4638 truthvalue_true_node = integer_one_node;
4639 truthvalue_false_node = integer_zero_node;
4641 /* Even in C99, which has a real boolean type. */
4642 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4643 boolean_type_node));
4645 /* C-specific nullptr initialization. */
4646 record_builtin_type (RID_MAX, "nullptr_t", nullptr_type_node);
4647 /* The size and alignment of nullptr_t is the same as for a pointer to
4648 character type. */
4649 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4651 input_location = save_loc;
4653 make_fname_decl = c_make_fname_decl;
4654 start_fname_decls ();
4657 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4658 give the decl, NAME is the initialization string and TYPE_DEP
4659 indicates whether NAME depended on the type of the function. As we
4660 don't yet implement delayed emission of static data, we mark the
4661 decl as emitted so it is not placed in the output. Anything using
4662 it must therefore pull out the STRING_CST initializer directly.
4663 FIXME. */
4665 static tree
4666 c_make_fname_decl (location_t loc, tree id, int type_dep)
4668 const char *name = fname_as_string (type_dep);
4669 tree decl, type, init;
4670 size_t length = strlen (name);
4672 type = build_array_type (char_type_node,
4673 build_index_type (size_int (length)));
4674 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4676 decl = build_decl (loc, VAR_DECL, id, type);
4678 TREE_STATIC (decl) = 1;
4679 TREE_READONLY (decl) = 1;
4680 DECL_ARTIFICIAL (decl) = 1;
4682 init = build_string (length + 1, name);
4683 free (CONST_CAST (char *, name));
4684 TREE_TYPE (init) = type;
4685 DECL_INITIAL (decl) = init;
4687 TREE_USED (decl) = 1;
4689 if (current_function_decl
4690 /* For invalid programs like this:
4692 void foo()
4693 const char* p = __FUNCTION__;
4695 the __FUNCTION__ is believed to appear in K&R style function
4696 parameter declarator. In that case we still don't have
4697 function_scope. */
4698 && current_function_scope)
4700 DECL_CONTEXT (decl) = current_function_decl;
4701 bind (id, decl, current_function_scope,
4702 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4705 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4707 return decl;
4710 tree
4711 c_builtin_function (tree decl)
4713 tree type = TREE_TYPE (decl);
4714 tree id = DECL_NAME (decl);
4716 const char *name = IDENTIFIER_POINTER (id);
4717 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4719 /* Should never be called on a symbol with a preexisting meaning. */
4720 gcc_assert (!I_SYMBOL_BINDING (id));
4722 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4723 UNKNOWN_LOCATION);
4725 /* Builtins in the implementation namespace are made visible without
4726 needing to be explicitly declared. See push_file_scope. */
4727 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4729 DECL_CHAIN (decl) = visible_builtins;
4730 visible_builtins = decl;
4733 return decl;
4736 tree
4737 c_builtin_function_ext_scope (tree decl)
4739 tree type = TREE_TYPE (decl);
4740 tree id = DECL_NAME (decl);
4742 const char *name = IDENTIFIER_POINTER (id);
4743 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4745 if (external_scope)
4746 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4747 UNKNOWN_LOCATION);
4749 /* Builtins in the implementation namespace are made visible without
4750 needing to be explicitly declared. See push_file_scope. */
4751 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4753 DECL_CHAIN (decl) = visible_builtins;
4754 visible_builtins = decl;
4757 return decl;
4760 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4762 tree
4763 c_simulate_builtin_function_decl (tree decl)
4765 tree type = TREE_TYPE (decl);
4766 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4767 return pushdecl (decl);
4770 /* Warn about attributes in a context where they are unused
4771 (attribute-declarations, except for the "fallthrough" case, and
4772 attributes on statements). */
4774 void
4775 c_warn_unused_attributes (tree attrs)
4777 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4778 if (get_attribute_namespace (t) == NULL_TREE)
4779 /* The specifications of standard attributes mean this is a
4780 constraint violation. */
4781 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4782 get_attribute_name (t));
4783 else if (!attribute_ignored_p (t))
4784 warning (OPT_Wattributes, "%qE attribute ignored",
4785 get_attribute_name (t));
4788 /* Warn for standard attributes being applied to a type that is not
4789 being defined, where that is a constraint violation, and return a
4790 list of attributes with them removed. */
4792 tree
4793 c_warn_type_attributes (tree attrs)
4795 tree *attr_ptr = &attrs;
4796 while (*attr_ptr)
4797 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4799 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4800 get_attribute_name (*attr_ptr));
4801 *attr_ptr = TREE_CHAIN (*attr_ptr);
4803 else
4804 attr_ptr = &TREE_CHAIN (*attr_ptr);
4805 return attrs;
4808 /* Called when a declaration is seen that contains no names to declare.
4809 If its type is a reference to a structure, union or enum inherited
4810 from a containing scope, shadow that tag name for the current scope
4811 with a forward reference.
4812 If its type defines a new named structure or union
4813 or defines an enum, it is valid but we need not do anything here.
4814 Otherwise, it is an error. */
4816 void
4817 shadow_tag (const struct c_declspecs *declspecs)
4819 shadow_tag_warned (declspecs, 0);
4822 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4823 but no pedwarn. */
4824 void
4825 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4827 bool found_tag = false;
4829 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4831 tree value = declspecs->type;
4832 enum tree_code code = TREE_CODE (value);
4834 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4835 /* Used to test also that TYPE_SIZE (value) != 0.
4836 That caused warning for `struct foo;' at top level in the file. */
4838 tree name = TYPE_NAME (value);
4839 tree t;
4841 found_tag = true;
4843 if (declspecs->restrict_p)
4845 error ("invalid use of %<restrict%>");
4846 warned = 1;
4849 if (in_underspecified_init)
4851 /* This can only occur with extensions such as statement
4852 expressions, but is still appropriate as an error to
4853 avoid types declared in such a context escaping to
4854 the type of an auto variable. */
4855 error ("%qT declared in underspecified object initializer",
4856 value);
4857 warned = 1;
4860 if (name == NULL_TREE)
4862 if (warned != 1 && code != ENUMERAL_TYPE)
4863 /* Empty unnamed enum OK */
4865 pedwarn (input_location, 0,
4866 "unnamed struct/union that defines no instances");
4867 warned = 1;
4870 else if (declspecs->typespec_kind != ctsk_tagdef
4871 && declspecs->typespec_kind != ctsk_tagfirstref
4872 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4873 && declspecs->storage_class != csc_none)
4875 if (warned != 1)
4876 pedwarn (input_location, 0,
4877 "empty declaration with storage class specifier "
4878 "does not redeclare tag");
4879 warned = 1;
4880 pending_xref_error ();
4882 else if (declspecs->typespec_kind != ctsk_tagdef
4883 && declspecs->typespec_kind != ctsk_tagfirstref
4884 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4885 && (declspecs->const_p
4886 || declspecs->volatile_p
4887 || declspecs->atomic_p
4888 || declspecs->restrict_p
4889 || declspecs->address_space))
4891 if (warned != 1)
4892 pedwarn (input_location, 0,
4893 "empty declaration with type qualifier "
4894 "does not redeclare tag");
4895 warned = 1;
4896 pending_xref_error ();
4898 else if (declspecs->typespec_kind != ctsk_tagdef
4899 && declspecs->typespec_kind != ctsk_tagfirstref
4900 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4901 && declspecs->alignas_p)
4903 if (warned != 1)
4904 pedwarn (input_location, 0,
4905 "empty declaration with %<_Alignas%> "
4906 "does not redeclare tag");
4907 warned = 1;
4908 pending_xref_error ();
4910 else if (declspecs->typespec_kind != ctsk_tagdef
4911 && declspecs->typespec_kind != ctsk_tagfirstref
4912 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4913 && code == ENUMERAL_TYPE
4914 && !declspecs->enum_type_specifier_ref_p)
4916 bool warned_enum = false;
4917 if (warned != 1)
4918 warned_enum = pedwarn (input_location, OPT_Wpedantic,
4919 "empty declaration of %<enum%> type "
4920 "does not redeclare tag");
4921 if (warned_enum)
4922 warned = 1;
4923 pending_xref_error ();
4925 else
4927 pending_invalid_xref = NULL_TREE;
4928 t = lookup_tag (code, name, true, NULL);
4930 if (t == NULL_TREE)
4932 t = make_node (code);
4933 pushtag (input_location, name, t);
4937 else
4939 if (warned != 1 && !in_system_header_at (input_location))
4941 pedwarn (input_location, 0,
4942 "useless type name in empty declaration");
4943 warned = 1;
4947 else if (warned != 1 && !in_system_header_at (input_location)
4948 && declspecs->typedef_p)
4950 pedwarn (input_location, 0, "useless type name in empty declaration");
4951 warned = 1;
4954 pending_invalid_xref = NULL_TREE;
4956 if (declspecs->inline_p)
4958 error ("%<inline%> in empty declaration");
4959 warned = 1;
4962 if (declspecs->noreturn_p)
4964 error ("%<_Noreturn%> in empty declaration");
4965 warned = 1;
4968 if (declspecs->constexpr_p)
4970 error ("%<constexpr%> in empty declaration");
4971 warned = 1;
4974 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4976 error ("%<auto%> in file-scope empty declaration");
4977 warned = 1;
4980 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4982 error ("%<register%> in file-scope empty declaration");
4983 warned = 1;
4986 if (declspecs->enum_type_specifier_ref_p && !warned)
4988 if (declspecs->storage_class != csc_none)
4990 error ("storage class specifier in empty declaration with %<enum%> "
4991 "underlying type");
4992 warned = 1;
4994 else if (declspecs->thread_p)
4996 error ("%qs in empty declaration with %<enum%> underlying type",
4997 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4998 warned = 1;
5000 else if (declspecs->const_p
5001 || declspecs->volatile_p
5002 || declspecs->atomic_p
5003 || declspecs->restrict_p
5004 || declspecs->address_space)
5006 error ("type qualifier in empty declaration with %<enum%> "
5007 "underlying type");
5008 warned = 1;
5010 else if (declspecs->alignas_p)
5012 error ("%<alignas%> in empty declaration with %<enum%> "
5013 "underlying type");
5014 warned = 1;
5018 if (!warned && !in_system_header_at (input_location)
5019 && declspecs->storage_class != csc_none)
5021 warning (0, "useless storage class specifier in empty declaration");
5022 warned = 2;
5025 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5027 warning (0, "useless %qs in empty declaration",
5028 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5029 warned = 2;
5032 if (!warned
5033 && !in_system_header_at (input_location)
5034 && (declspecs->const_p
5035 || declspecs->volatile_p
5036 || declspecs->atomic_p
5037 || declspecs->restrict_p
5038 || declspecs->address_space))
5040 warning (0, "useless type qualifier in empty declaration");
5041 warned = 2;
5044 if (!warned && !in_system_header_at (input_location)
5045 && declspecs->alignas_p)
5047 warning (0, "useless %<_Alignas%> in empty declaration");
5048 warned = 2;
5051 if (found_tag
5052 && warned == 2
5053 && (declspecs->typespec_kind == ctsk_tagref_attrs
5054 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5056 /* Standard attributes after the "struct" or "union" keyword are
5057 only permitted when the contents of the type are defined, or
5058 in the form "struct-or-union attribute-specifier-sequence
5059 identifier;". If the ';' was not present, attributes were
5060 diagnosed in the parser. Here, ensure that any other useless
5061 elements of the declaration result in a pedwarn, not just a
5062 warning. Forward declarations of enum types are not part of
5063 standard C, but handle them the same. */
5064 pedwarn (input_location, 0,
5065 "invalid use of attributes in empty declaration");
5066 warned = 1;
5069 if (warned != 1)
5071 if (declspecs->declspecs_seen_p
5072 && !declspecs->non_std_attrs_seen_p)
5073 /* An attribute declaration (but not a fallthrough attribute
5074 declaration, which was handled separately); warn if there
5075 are any attributes being ignored (but not if the attributes
5076 were empty). */
5077 c_warn_unused_attributes (declspecs->attrs);
5078 else if (!found_tag)
5079 pedwarn (input_location, 0, "empty declaration");
5084 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5085 bits. SPECS represents declaration specifiers that the grammar
5086 only permits to contain type qualifiers and attributes. */
5089 quals_from_declspecs (const struct c_declspecs *specs)
5091 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5092 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5093 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5094 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5095 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5096 gcc_assert (!specs->type
5097 && !specs->decl_attr
5098 && specs->typespec_word == cts_none
5099 && specs->storage_class == csc_none
5100 && !specs->typedef_p
5101 && !specs->explicit_signed_p
5102 && !specs->deprecated_p
5103 && !specs->unavailable_p
5104 && !specs->long_p
5105 && !specs->long_long_p
5106 && !specs->short_p
5107 && !specs->signed_p
5108 && !specs->unsigned_p
5109 && !specs->complex_p
5110 && !specs->inline_p
5111 && !specs->noreturn_p
5112 && !specs->thread_p);
5113 return quals;
5116 /* Construct an array declarator. LOC is the location of the
5117 beginning of the array (usually the opening brace). EXPR is the
5118 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5119 inside the [] (to be applied to the pointer to which a parameter
5120 array is converted). STATIC_P is true if "static" is inside the
5121 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5122 VLA of unspecified length which is nevertheless a complete type,
5123 false otherwise. The field for the contained declarator is left to
5124 be filled in by set_array_declarator_inner. */
5126 struct c_declarator *
5127 build_array_declarator (location_t loc,
5128 tree expr, struct c_declspecs *quals, bool static_p,
5129 bool vla_unspec_p)
5131 struct c_declarator *declarator = XOBNEW (&parser_obstack,
5132 struct c_declarator);
5133 declarator->id_loc = loc;
5134 declarator->kind = cdk_array;
5135 declarator->declarator = 0;
5136 declarator->u.array.dimen = expr;
5137 if (quals)
5139 declarator->u.array.attrs = quals->attrs;
5140 declarator->u.array.quals = quals_from_declspecs (quals);
5142 else
5144 declarator->u.array.attrs = NULL_TREE;
5145 declarator->u.array.quals = 0;
5147 declarator->u.array.static_p = static_p;
5148 declarator->u.array.vla_unspec_p = vla_unspec_p;
5149 if (static_p || quals != NULL)
5150 pedwarn_c90 (loc, OPT_Wpedantic,
5151 "ISO C90 does not support %<static%> or type "
5152 "qualifiers in parameter array declarators");
5153 if (vla_unspec_p)
5154 pedwarn_c90 (loc, OPT_Wpedantic,
5155 "ISO C90 does not support %<[*]%> array declarators");
5156 if (vla_unspec_p)
5158 if (!current_scope->parm_flag)
5160 /* C99 6.7.5.2p4 */
5161 error_at (loc, "%<[*]%> not allowed in other than "
5162 "function prototype scope");
5163 declarator->u.array.vla_unspec_p = false;
5164 return NULL;
5166 current_scope->had_vla_unspec = true;
5168 return declarator;
5171 /* Set the contained declarator of an array declarator. DECL is the
5172 declarator, as constructed by build_array_declarator; INNER is what
5173 appears on the left of the []. */
5175 struct c_declarator *
5176 set_array_declarator_inner (struct c_declarator *decl,
5177 struct c_declarator *inner)
5179 decl->declarator = inner;
5180 return decl;
5183 /* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5184 static bool
5185 flexible_array_member_type_p (const_tree type)
5187 if (TREE_CODE (type) == ARRAY_TYPE
5188 && TYPE_SIZE (type) == NULL_TREE
5189 && TYPE_DOMAIN (type) != NULL_TREE
5190 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5191 return true;
5193 return false;
5196 /* Determine whether TYPE is a one-element array type "[1]". */
5197 static bool
5198 one_element_array_type_p (const_tree type)
5200 if (TREE_CODE (type) != ARRAY_TYPE)
5201 return false;
5202 return integer_zerop (array_type_nelts (type));
5205 /* Determine whether TYPE is a zero-length array type "[0]". */
5206 static bool
5207 zero_length_array_type_p (const_tree type)
5209 if (TREE_CODE (type) == ARRAY_TYPE)
5210 if (tree type_size = TYPE_SIZE_UNIT (type))
5211 if ((integer_zerop (type_size))
5212 && TYPE_DOMAIN (type) != NULL_TREE
5213 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5214 return true;
5215 return false;
5218 /* INIT is a constructor that forms DECL's initializer. If the final
5219 element initializes a flexible array field, add the size of that
5220 initializer to DECL's size. */
5222 static void
5223 add_flexible_array_elts_to_size (tree decl, tree init)
5225 tree elt, type;
5227 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5228 return;
5230 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5231 type = TREE_TYPE (elt);
5232 if (flexible_array_member_type_p (type))
5234 complete_array_type (&type, elt, false);
5235 DECL_SIZE (decl)
5236 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5237 DECL_SIZE_UNIT (decl)
5238 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5242 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5243 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5244 before the type name, and set *EXPR_CONST_OPERANDS, if
5245 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5246 appear in a constant expression. */
5248 tree
5249 groktypename (struct c_type_name *type_name, tree *expr,
5250 bool *expr_const_operands)
5252 tree type;
5253 tree attrs = type_name->specs->attrs;
5255 type_name->specs->attrs = NULL_TREE;
5257 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5258 false, NULL, &attrs, expr, expr_const_operands,
5259 DEPRECATED_NORMAL);
5261 /* Apply attributes. */
5262 attrs = c_warn_type_attributes (attrs);
5263 decl_attributes (&type, attrs, 0);
5265 return type;
5268 /* Looks up the most recent pushed declaration corresponding to DECL. */
5270 static tree
5271 lookup_last_decl (tree decl)
5273 tree last_decl = lookup_name (DECL_NAME (decl));
5274 if (!last_decl)
5275 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5276 return last_decl;
5279 /* Wrapper for decl_attributes that adds some implicit attributes
5280 to VAR_DECLs or FUNCTION_DECLs. */
5282 static tree
5283 c_decl_attributes (tree *node, tree attributes, int flags)
5285 /* Add implicit "omp declare target" attribute if requested. */
5286 if (vec_safe_length (current_omp_declare_target_attribute)
5287 && ((VAR_P (*node) && is_global_var (*node))
5288 || TREE_CODE (*node) == FUNCTION_DECL))
5290 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5291 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5292 NULL_TREE, attributes);
5293 else
5295 attributes = tree_cons (get_identifier ("omp declare target"),
5296 NULL_TREE, attributes);
5297 attributes = tree_cons (get_identifier ("omp declare target block"),
5298 NULL_TREE, attributes);
5300 if (TREE_CODE (*node) == FUNCTION_DECL)
5302 int device_type
5303 = current_omp_declare_target_attribute->last ().device_type;
5304 device_type = MAX (device_type, 0);
5305 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5306 && !lookup_attribute ("omp declare target host", attributes))
5307 attributes
5308 = tree_cons (get_identifier ("omp declare target host"),
5309 NULL_TREE, attributes);
5310 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5311 && !lookup_attribute ("omp declare target nohost", attributes))
5312 attributes
5313 = tree_cons (get_identifier ("omp declare target nohost"),
5314 NULL_TREE, attributes);
5318 /* Look up the current declaration with all the attributes merged
5319 so far so that attributes on the current declaration that's
5320 about to be pushed that conflict with the former can be detected,
5321 diagnosed, and rejected as appropriate. */
5322 tree last_decl = lookup_last_decl (*node);
5323 return decl_attributes (node, attributes, flags, last_decl);
5327 /* Decode a declarator in an ordinary declaration or data definition.
5328 This is called as soon as the type information and variable name
5329 have been parsed, before parsing the initializer if any.
5330 Here we create the ..._DECL node, fill in its type,
5331 and (if DO_PUSH) put it on the list of decls for the current context.
5332 When nonnull, set *LASTLOC to the location of the prior declaration
5333 of the same entity if one exists.
5334 The ..._DECL node is returned as the value.
5336 Exception: for arrays where the length is not specified,
5337 the type is left null, to be filled in by `finish_decl'.
5339 Function definitions do not come here; they go to start_function
5340 instead. However, external and forward declarations of functions
5341 do go through here. Structure field declarations are done by
5342 grokfield and not through here. */
5344 tree
5345 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5346 bool initialized, tree attributes, bool do_push /* = true */,
5347 location_t *lastloc /* = NULL */)
5349 tree decl;
5350 tree tem;
5351 tree expr = NULL_TREE;
5352 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5354 /* An object declared as __attribute__((unavailable)) suppresses
5355 warnings and errors from __attribute__((deprecated/unavailable))
5356 components.
5357 An object declared as __attribute__((deprecated)) suppresses
5358 warnings of uses of other deprecated items. */
5359 if (lookup_attribute ("unavailable", attributes))
5360 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5361 else if (lookup_attribute ("deprecated", attributes))
5362 deprecated_state = DEPRECATED_SUPPRESS;
5364 decl = grokdeclarator (declarator, declspecs,
5365 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5366 deprecated_state);
5367 if (!decl || decl == error_mark_node)
5368 return NULL_TREE;
5370 if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5371 if (lastdecl != error_mark_node)
5372 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5374 if (expr)
5375 add_stmt (fold_convert (void_type_node, expr));
5377 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5378 && TREE_PUBLIC (decl))
5379 warning (OPT_Wmain, "%q+D is usually a function", decl);
5381 if (initialized)
5382 /* Is it valid for this decl to have an initializer at all?
5383 If not, set INITIALIZED to zero, which will indirectly
5384 tell 'finish_decl' to ignore the initializer once it is parsed. */
5385 switch (TREE_CODE (decl))
5387 case TYPE_DECL:
5388 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5389 initialized = false;
5390 break;
5392 case FUNCTION_DECL:
5393 error ("function %qD is initialized like a variable", decl);
5394 initialized = false;
5395 break;
5397 case PARM_DECL:
5398 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5399 error ("parameter %qD is initialized", decl);
5400 initialized = false;
5401 break;
5403 default:
5404 /* Don't allow initializations for incomplete types except for
5405 arrays which might be completed by the initialization. */
5407 /* This can happen if the array size is an undefined macro.
5408 We already gave a warning, so we don't need another one. */
5409 if (TREE_TYPE (decl) == error_mark_node)
5410 initialized = false;
5411 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5413 /* A complete type is ok if size is fixed. If the size is
5414 variable, an empty initializer is OK and nonempty
5415 initializers will be diagnosed in the parser. */
5417 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5419 error ("variable %qD has initializer but incomplete type", decl);
5420 initialized = false;
5424 if (initialized)
5426 if (current_scope == file_scope)
5427 TREE_STATIC (decl) = 1;
5429 /* Tell 'pushdecl' this is an initialized decl
5430 even though we don't yet have the initializer expression.
5431 Also tell 'finish_decl' it may store the real initializer. */
5432 DECL_INITIAL (decl) = error_mark_node;
5435 /* If this is a function declaration, write a record describing it to the
5436 prototypes file (if requested). */
5438 if (TREE_CODE (decl) == FUNCTION_DECL)
5439 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5441 /* ANSI specifies that a tentative definition which is not merged with
5442 a non-tentative definition behaves exactly like a definition with an
5443 initializer equal to zero. (Section 3.7.2)
5445 -fno-common gives strict ANSI behavior, though this tends to break
5446 a large body of code that grew up without this rule.
5448 Thread-local variables are never common, since there's no entrenched
5449 body of code to break, and it allows more efficient variable references
5450 in the presence of dynamic linking. */
5452 if (VAR_P (decl)
5453 && !initialized
5454 && TREE_PUBLIC (decl)
5455 && !DECL_THREAD_LOCAL_P (decl)
5456 && !flag_no_common)
5457 DECL_COMMON (decl) = 1;
5459 /* Set attributes here so if duplicate decl, will have proper attributes. */
5460 c_decl_attributes (&decl, attributes, 0);
5462 /* Handle gnu_inline attribute. */
5463 if (declspecs->inline_p
5464 && !flag_gnu89_inline
5465 && TREE_CODE (decl) == FUNCTION_DECL
5466 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5467 || current_function_decl))
5469 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5471 else if (declspecs->storage_class != csc_static)
5472 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5475 if (TREE_CODE (decl) == FUNCTION_DECL
5476 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5478 struct c_declarator *ce = declarator;
5480 if (ce->kind == cdk_pointer)
5481 ce = declarator->declarator;
5482 if (ce->kind == cdk_function)
5484 tree args = ce->u.arg_info->parms;
5485 for (; args; args = DECL_CHAIN (args))
5487 tree type = TREE_TYPE (args);
5488 if (type && INTEGRAL_TYPE_P (type)
5489 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5490 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5495 if (TREE_CODE (decl) == FUNCTION_DECL
5496 && DECL_DECLARED_INLINE_P (decl)
5497 && DECL_UNINLINABLE (decl)
5498 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5499 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5500 decl, "noinline");
5502 /* C99 6.7.4p3: An inline definition of a function with external
5503 linkage shall not contain a definition of a modifiable object
5504 with static storage duration... */
5505 if (VAR_P (decl)
5506 && current_scope != file_scope
5507 && TREE_STATIC (decl)
5508 && !TREE_READONLY (decl)
5509 && DECL_DECLARED_INLINE_P (current_function_decl)
5510 && DECL_EXTERNAL (current_function_decl))
5511 record_inline_static (input_location, current_function_decl,
5512 decl, csi_modifiable);
5514 if (c_dialect_objc ()
5515 && VAR_OR_FUNCTION_DECL_P (decl))
5516 objc_check_global_decl (decl);
5518 /* Add this decl to the current scope.
5519 TEM may equal DECL or it may be a previous decl of the same name. */
5520 if (do_push)
5522 tem = pushdecl (decl);
5524 if (initialized && DECL_EXTERNAL (tem))
5526 DECL_EXTERNAL (tem) = 0;
5527 TREE_STATIC (tem) = 1;
5530 return tem;
5532 else
5533 return decl;
5536 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5537 DECL or the non-array element type if DECL is an uninitialized array.
5538 If that type has a const member, diagnose this. */
5540 static void
5541 diagnose_uninitialized_cst_member (tree decl, tree type)
5543 tree field;
5544 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5546 tree field_type;
5547 if (TREE_CODE (field) != FIELD_DECL)
5548 continue;
5549 field_type = strip_array_types (TREE_TYPE (field));
5551 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5553 auto_diagnostic_group d;
5554 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5555 "uninitialized const member in %qT is invalid in C++",
5556 strip_array_types (TREE_TYPE (decl))))
5557 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5560 if (RECORD_OR_UNION_TYPE_P (field_type))
5561 diagnose_uninitialized_cst_member (decl, field_type);
5565 /* Finish processing of a declaration;
5566 install its initial value.
5567 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5568 If the length of an array type is not known before,
5569 it must be determined now, from the initial value, or it is an error.
5571 INIT_LOC is the location of the initial value. */
5573 void
5574 finish_decl (tree decl, location_t init_loc, tree init,
5575 tree origtype, tree asmspec_tree)
5577 tree type;
5578 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5579 const char *asmspec = 0;
5581 /* If a name was specified, get the string. */
5582 if (VAR_OR_FUNCTION_DECL_P (decl)
5583 && DECL_FILE_SCOPE_P (decl))
5584 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5585 if (asmspec_tree)
5586 asmspec = TREE_STRING_POINTER (asmspec_tree);
5588 if (VAR_P (decl)
5589 && TREE_STATIC (decl)
5590 && global_bindings_p ())
5591 /* So decl is a global variable. Record the types it uses
5592 so that we can decide later to emit debug info for them. */
5593 record_types_used_by_current_var_decl (decl);
5595 /* If `start_decl' didn't like having an initialization, ignore it now. */
5596 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5597 init = NULL_TREE;
5599 /* Don't crash if parm is initialized. */
5600 if (TREE_CODE (decl) == PARM_DECL)
5601 init = NULL_TREE;
5603 if (init)
5604 store_init_value (init_loc, decl, init, origtype);
5606 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5607 || TREE_CODE (decl) == FIELD_DECL))
5608 objc_check_decl (decl);
5610 type = TREE_TYPE (decl);
5612 /* Deduce size of array from initialization, if not already known.
5613 This is only needed for an initialization in the current scope;
5614 it must not be done for a file-scope initialization of a
5615 declaration with external linkage, redeclared in an inner scope
5616 with the outer declaration shadowed in an intermediate scope. */
5617 if (TREE_CODE (type) == ARRAY_TYPE
5618 && TYPE_DOMAIN (type) == NULL_TREE
5619 && TREE_CODE (decl) != TYPE_DECL
5620 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5622 bool do_default
5623 = (TREE_STATIC (decl)
5624 /* Even if pedantic, an external linkage array
5625 may have incomplete type at first. */
5626 ? pedantic && !TREE_PUBLIC (decl)
5627 : !DECL_EXTERNAL (decl));
5628 int failure
5629 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5630 do_default);
5632 /* Get the completed type made by complete_array_type. */
5633 type = TREE_TYPE (decl);
5635 switch (failure)
5637 case 1:
5638 error ("initializer fails to determine size of %q+D", decl);
5639 break;
5641 case 2:
5642 if (do_default)
5643 error ("array size missing in %q+D", decl);
5644 break;
5646 case 3:
5647 error ("zero or negative size array %q+D", decl);
5648 break;
5650 case 0:
5651 /* For global variables, update the copy of the type that
5652 exists in the binding. */
5653 if (TREE_PUBLIC (decl))
5655 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5656 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5657 b_ext = b_ext->shadowed;
5658 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5660 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5661 b_ext->u.type = composite_type (b_ext->u.type, type);
5662 else
5663 b_ext->u.type = type;
5666 break;
5668 default:
5669 gcc_unreachable ();
5672 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5673 TREE_TYPE (DECL_INITIAL (decl)) = type;
5675 relayout_decl (decl);
5678 /* Look for braced array initializers for character arrays and
5679 recursively convert them into STRING_CSTs. */
5680 if (tree init = DECL_INITIAL (decl))
5681 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5683 if (VAR_P (decl))
5685 if (init && TREE_CODE (init) == CONSTRUCTOR)
5686 add_flexible_array_elts_to_size (decl, init);
5688 complete_flexible_array_elts (DECL_INITIAL (decl));
5690 if (is_global_var (decl))
5692 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5693 ? TCTX_THREAD_STORAGE
5694 : TCTX_STATIC_STORAGE);
5695 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5696 TREE_TYPE (decl) = error_mark_node;
5699 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5700 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5701 layout_decl (decl, 0);
5703 if (DECL_SIZE (decl) == NULL_TREE
5704 /* Don't give an error if we already gave one earlier. */
5705 && TREE_TYPE (decl) != error_mark_node
5706 && (TREE_STATIC (decl)
5707 /* A static variable with an incomplete type
5708 is an error if it is initialized.
5709 Also if it is not file scope.
5710 Otherwise, let it through, but if it is not `extern'
5711 then it may cause an error message later. */
5712 ? (DECL_INITIAL (decl) != NULL_TREE
5713 || !DECL_FILE_SCOPE_P (decl))
5714 /* An automatic variable with an incomplete type
5715 is an error. */
5716 : !DECL_EXTERNAL (decl)))
5718 error ("storage size of %q+D isn%'t known", decl);
5719 TREE_TYPE (decl) = error_mark_node;
5722 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5723 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5724 && DECL_SIZE (decl) == NULL_TREE
5725 && TREE_STATIC (decl))
5726 incomplete_record_decls.safe_push (decl);
5728 if (is_global_var (decl)
5729 && DECL_SIZE (decl) != NULL_TREE
5730 && TREE_TYPE (decl) != error_mark_node)
5732 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5733 constant_expression_warning (DECL_SIZE (decl));
5734 else
5736 error ("storage size of %q+D isn%'t constant", decl);
5737 TREE_TYPE (decl) = error_mark_node;
5741 if (TREE_USED (type))
5743 TREE_USED (decl) = 1;
5744 DECL_READ_P (decl) = 1;
5748 /* If this is a function and an assembler name is specified, reset DECL_RTL
5749 so we can give it its new name. Also, update builtin_decl if it
5750 was a normal built-in. */
5751 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5753 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5754 set_builtin_user_assembler_name (decl, asmspec);
5755 set_user_assembler_name (decl, asmspec);
5758 /* If #pragma weak was used, mark the decl weak now. */
5759 maybe_apply_pragma_weak (decl);
5761 /* Output the assembler code and/or RTL code for variables and functions,
5762 unless the type is an undefined structure or union.
5763 If not, it will get done when the type is completed. */
5765 if (VAR_OR_FUNCTION_DECL_P (decl))
5767 /* Determine the ELF visibility. */
5768 if (TREE_PUBLIC (decl))
5769 c_determine_visibility (decl);
5771 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5772 if (c_dialect_objc ())
5773 objc_check_decl (decl);
5775 if (asmspec)
5777 /* If this is not a static variable, issue a warning.
5778 It doesn't make any sense to give an ASMSPEC for an
5779 ordinary, non-register local variable. Historically,
5780 GCC has accepted -- but ignored -- the ASMSPEC in
5781 this case. */
5782 if (!DECL_FILE_SCOPE_P (decl)
5783 && VAR_P (decl)
5784 && !C_DECL_REGISTER (decl)
5785 && !TREE_STATIC (decl))
5786 warning (0, "ignoring %<asm%> specifier for non-static local "
5787 "variable %q+D", decl);
5788 else
5789 set_user_assembler_name (decl, asmspec);
5792 if (DECL_FILE_SCOPE_P (decl))
5794 if (DECL_INITIAL (decl) == NULL_TREE
5795 || DECL_INITIAL (decl) == error_mark_node)
5796 /* Don't output anything
5797 when a tentative file-scope definition is seen.
5798 But at end of compilation, do output code for them. */
5799 DECL_DEFER_OUTPUT (decl) = 1;
5800 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5801 DECL_HARD_REGISTER (decl) = 1;
5802 rest_of_decl_compilation (decl, true, 0);
5804 if (TREE_CODE (decl) == FUNCTION_DECL)
5806 tree parms = DECL_ARGUMENTS (decl);
5807 const bool builtin = fndecl_built_in_p (decl);
5808 if (tree access = build_attr_access_from_parms (parms, !builtin))
5809 decl_attributes (&decl, access, 0);
5812 else
5814 /* In conjunction with an ASMSPEC, the `register'
5815 keyword indicates that we should place the variable
5816 in a particular register. */
5817 if (asmspec && C_DECL_REGISTER (decl))
5819 DECL_HARD_REGISTER (decl) = 1;
5820 /* This cannot be done for a structure with volatile
5821 fields, on which DECL_REGISTER will have been
5822 reset. */
5823 if (!DECL_REGISTER (decl))
5824 error ("cannot put object with volatile field into register");
5827 if (TREE_CODE (decl) != FUNCTION_DECL)
5829 /* If we're building a variable sized type, and we might be
5830 reachable other than via the top of the current binding
5831 level, then create a new BIND_EXPR so that we deallocate
5832 the object at the right time. */
5833 /* Note that DECL_SIZE can be null due to errors. */
5834 if (DECL_SIZE (decl)
5835 && !TREE_CONSTANT (DECL_SIZE (decl))
5836 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5838 tree bind;
5839 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5840 TREE_SIDE_EFFECTS (bind) = 1;
5841 add_stmt (bind);
5842 BIND_EXPR_BODY (bind) = push_stmt_list ();
5844 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5845 DECL_EXPR, decl));
5850 if (!DECL_FILE_SCOPE_P (decl))
5852 /* Recompute the RTL of a local array now
5853 if it used to be an incomplete type. */
5854 if (was_incomplete && !is_global_var (decl))
5856 /* If we used it already as memory, it must stay in memory. */
5857 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5858 /* If it's still incomplete now, no init will save it. */
5859 if (DECL_SIZE (decl) == NULL_TREE)
5860 DECL_INITIAL (decl) = NULL_TREE;
5865 if (TREE_CODE (decl) == TYPE_DECL)
5867 if (!DECL_FILE_SCOPE_P (decl)
5868 && c_type_variably_modified_p (TREE_TYPE (decl)))
5869 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5871 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5874 /* Install a cleanup (aka destructor) if one was given. */
5875 if (VAR_P (decl) && !TREE_STATIC (decl))
5877 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5878 if (attr)
5880 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5881 tree cleanup_decl = lookup_name (cleanup_id);
5882 tree cleanup;
5883 vec<tree, va_gc> *v;
5885 /* Build "cleanup(&decl)" for the destructor. */
5886 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5887 vec_alloc (v, 1);
5888 v->quick_push (cleanup);
5889 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5890 vNULL, cleanup_decl, v, NULL);
5891 vec_free (v);
5893 /* Don't warn about decl unused; the cleanup uses it. */
5894 TREE_USED (decl) = 1;
5895 TREE_USED (cleanup_decl) = 1;
5896 DECL_READ_P (decl) = 1;
5898 push_cleanup (decl, cleanup, false);
5902 if (warn_cxx_compat
5903 && VAR_P (decl)
5904 && !DECL_EXTERNAL (decl)
5905 && DECL_INITIAL (decl) == NULL_TREE)
5907 type = strip_array_types (type);
5908 if (TREE_READONLY (decl))
5909 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5910 "uninitialized %<const %D%> is invalid in C++", decl);
5911 else if (RECORD_OR_UNION_TYPE_P (type)
5912 && C_TYPE_FIELDS_READONLY (type))
5913 diagnose_uninitialized_cst_member (decl, type);
5916 if (flag_openmp
5917 && VAR_P (decl)
5918 && lookup_attribute ("omp declare target implicit",
5919 DECL_ATTRIBUTES (decl)))
5921 DECL_ATTRIBUTES (decl)
5922 = remove_attribute ("omp declare target implicit",
5923 DECL_ATTRIBUTES (decl));
5924 if (!omp_mappable_type (TREE_TYPE (decl)))
5925 error ("%q+D in declare target directive does not have mappable type",
5926 decl);
5927 else if (!lookup_attribute ("omp declare target",
5928 DECL_ATTRIBUTES (decl))
5929 && !lookup_attribute ("omp declare target link",
5930 DECL_ATTRIBUTES (decl)))
5932 DECL_ATTRIBUTES (decl)
5933 = tree_cons (get_identifier ("omp declare target"),
5934 NULL_TREE, DECL_ATTRIBUTES (decl));
5935 symtab_node *node = symtab_node::get (decl);
5936 if (node != NULL)
5938 node->offloadable = 1;
5939 if (ENABLE_OFFLOADING)
5941 g->have_offload = true;
5942 if (is_a <varpool_node *> (node))
5943 vec_safe_push (offload_vars, decl);
5949 /* This is the last point we can lower alignment so give the target the
5950 chance to do so. */
5951 if (VAR_P (decl)
5952 && !is_global_var (decl)
5953 && !DECL_HARD_REGISTER (decl))
5954 targetm.lower_local_decl_alignment (decl);
5956 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5959 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5960 EXPR is NULL or a pointer to an expression that needs to be
5961 evaluated for the side effects of array size expressions in the
5962 parameters. */
5964 tree
5965 grokparm (const struct c_parm *parm, tree *expr)
5967 tree attrs = parm->attrs;
5968 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5969 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5971 decl_attributes (&decl, attrs, 0);
5973 return decl;
5976 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5977 described by PARM, concatenated onto attributes ATTRS.
5978 The spec consists of one dollar symbol for each specified variable
5979 bound, one asterisk for each unspecified variable bound, followed
5980 by at most one specification of the most significant bound of
5981 an ordinary array parameter. For ordinary arrays the specification
5982 is either the constant bound itself, or the space character for
5983 an array with an unspecified bound (the [] form). Finally, a chain
5984 of specified variable bounds is appended to the spec, starting with
5985 the most significant bound. For example, the PARM T a[2][m][3][n]
5986 will produce __attribute__((arg spec ("[$$2]", m, n)).
5987 For T a typedef for an array with variable bounds, the bounds are
5988 included in the specification in the expected order.
5989 No "arg spec" is created for parameters of pointer types, making
5990 a distinction between T(*)[N] (or, equivalently, T[][N]) and
5991 the T[M][N] form, all of which have the same type and are represented
5992 the same, but only the last of which gets an "arg spec" describing
5993 the most significant bound M. */
5995 static tree
5996 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5998 /* The attribute specification string, minor bound first. */
5999 std::string spec;
6001 /* A list of VLA variable bounds, major first, or null if unspecified
6002 or not a VLA. */
6003 tree vbchain = NULL_TREE;
6004 /* True for a pointer parameter. */
6005 bool pointer = false;
6006 /* True for an ordinary array with an unpecified bound. */
6007 bool nobound = false;
6009 /* Create a string representation for the bounds of the array/VLA. */
6010 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6012 next = pd->declarator;
6013 while (next && next->kind == cdk_attrs)
6014 next = next->declarator;
6016 /* Remember if a pointer has been seen to avoid storing the constant
6017 bound. */
6018 if (pd->kind == cdk_pointer)
6019 pointer = true;
6021 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6022 && (!next || next->kind == cdk_id))
6024 /* Do nothing for the common case of a pointer. The fact that
6025 the parameter is one can be deduced from the absence of
6026 an arg spec for it. */
6027 return attrs;
6030 if (pd->kind == cdk_id)
6032 if (pointer
6033 || !parm->specs->type
6034 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6035 || !TYPE_DOMAIN (parm->specs->type)
6036 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6037 continue;
6039 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6040 if (!vbchain
6041 && TREE_CODE (max) == INTEGER_CST)
6043 /* Extract the upper bound from a parameter of an array type
6044 unless the parameter is an ordinary array of unspecified
6045 bound in which case a next iteration of the loop will
6046 exit. */
6047 if (spec.empty () || spec.end ()[-1] != ' ')
6049 if (!tree_fits_shwi_p (max))
6050 continue;
6052 /* The upper bound is the value of the largest valid
6053 index. */
6054 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6055 char buf[40];
6056 sprintf (buf, "%lu", (unsigned long)n);
6057 spec += buf;
6059 continue;
6062 /* For a VLA typedef, create a list of its variable bounds and
6063 append it in the expected order to VBCHAIN. */
6064 tree tpbnds = NULL_TREE;
6065 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6066 type = TREE_TYPE (type))
6068 tree nelts = array_type_nelts (type);
6069 if (error_operand_p (nelts))
6070 return attrs;
6071 if (TREE_CODE (nelts) != INTEGER_CST)
6073 /* Each variable VLA bound is represented by the dollar
6074 sign. */
6075 spec += "$";
6076 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
6079 tpbnds = nreverse (tpbnds);
6080 vbchain = chainon (vbchain, tpbnds);
6081 continue;
6084 if (pd->kind != cdk_array)
6085 continue;
6087 if (pd->u.array.vla_unspec_p)
6089 /* Each unspecified bound is represented by a star. There
6090 can be any number of these in a declaration (but none in
6091 a definition). */
6092 spec += '*';
6093 continue;
6096 tree nelts = pd->u.array.dimen;
6097 if (!nelts)
6099 /* Ordinary array of unspecified size. There can be at most
6100 one for the most significant bound. Exit on the next
6101 iteration which determines whether or not PARM is declared
6102 as a pointer or an array. */
6103 nobound = true;
6104 continue;
6107 if (pd->u.array.static_p)
6108 spec += 's';
6110 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6111 /* Avoid invalid NELTS. */
6112 return attrs;
6114 STRIP_NOPS (nelts);
6115 nelts = c_fully_fold (nelts, false, nullptr);
6116 if (TREE_CODE (nelts) == INTEGER_CST)
6118 /* Skip all constant bounds except the most significant one.
6119 The interior ones are included in the array type. */
6120 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6121 continue;
6123 if (!tree_fits_uhwi_p (nelts))
6124 /* Bail completely on invalid bounds. */
6125 return attrs;
6127 char buf[40];
6128 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6129 sprintf (buf, "%llu", (unsigned long long)n);
6130 spec += buf;
6131 break;
6134 /* Each variable VLA bound is represented by a dollar sign. */
6135 spec += "$";
6136 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6139 if (spec.empty () && !nobound)
6140 return attrs;
6142 spec.insert (0, "[");
6143 if (nobound)
6144 /* Ordinary array of unspecified bound is represented by a space.
6145 It must be last in the spec. */
6146 spec += ' ';
6147 spec += ']';
6149 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6150 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6151 tree name = get_identifier ("arg spec");
6152 return tree_cons (name, args, attrs);
6155 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6156 and push that on the current scope. EXPR is a pointer to an
6157 expression that needs to be evaluated for the side effects of array
6158 size expressions in the parameters. */
6160 void
6161 push_parm_decl (const struct c_parm *parm, tree *expr)
6163 tree attrs = parm->attrs;
6164 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6165 &attrs, expr, NULL, DEPRECATED_NORMAL);
6166 if (decl && DECL_P (decl))
6167 DECL_SOURCE_LOCATION (decl) = parm->loc;
6169 attrs = get_parm_array_spec (parm, attrs);
6170 decl_attributes (&decl, attrs, 0);
6172 decl = pushdecl (decl);
6174 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6177 /* Mark all the parameter declarations to date as forward decls.
6178 Also diagnose use of this extension. */
6180 void
6181 mark_forward_parm_decls (void)
6183 struct c_binding *b;
6185 if (pedantic && !current_scope->warned_forward_parm_decls)
6187 pedwarn (input_location, OPT_Wpedantic,
6188 "ISO C forbids forward parameter declarations");
6189 current_scope->warned_forward_parm_decls = true;
6192 for (b = current_scope->bindings; b; b = b->prev)
6193 if (TREE_CODE (b->decl) == PARM_DECL)
6194 TREE_ASM_WRITTEN (b->decl) = 1;
6197 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6198 literal, which may be an incomplete array type completed by the
6199 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6200 literal. NON_CONST is true if the initializers contain something
6201 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6202 it is the (valid) alignment for this compound literal, as specified
6203 with _Alignas. SCSPECS are the storage class specifiers (C2x) from the
6204 compound literal. */
6206 tree
6207 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6208 unsigned int alignas_align,
6209 struct c_declspecs *scspecs)
6211 /* We do not use start_decl here because we have a type, not a declarator;
6212 and do not use finish_decl because the decl should be stored inside
6213 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6214 tree decl;
6215 tree complit;
6216 tree stmt;
6217 bool threadp = scspecs ? scspecs->thread_p : false;
6218 enum c_storage_class storage_class = (scspecs
6219 ? scspecs->storage_class
6220 : csc_none);
6222 if (type == error_mark_node
6223 || init == error_mark_node)
6224 return error_mark_node;
6226 if (current_scope == file_scope && storage_class == csc_register)
6228 error_at (loc, "file-scope compound literal specifies %<register%>");
6229 storage_class = csc_none;
6232 if (current_scope != file_scope && threadp && storage_class == csc_none)
6234 error_at (loc, "compound literal implicitly auto and declared %qs",
6235 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6236 threadp = false;
6239 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6240 DECL_EXTERNAL (decl) = 0;
6241 TREE_PUBLIC (decl) = 0;
6242 TREE_STATIC (decl) = (current_scope == file_scope
6243 || storage_class == csc_static);
6244 DECL_CONTEXT (decl) = current_function_decl;
6245 TREE_USED (decl) = 1;
6246 DECL_READ_P (decl) = 1;
6247 DECL_ARTIFICIAL (decl) = 1;
6248 DECL_IGNORED_P (decl) = 1;
6249 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6250 C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6251 TREE_TYPE (decl) = type;
6252 if (threadp)
6253 set_decl_tls_model (decl, decl_default_tls_model (decl));
6254 if (storage_class == csc_register)
6256 C_DECL_REGISTER (decl) = 1;
6257 DECL_REGISTER (decl) = 1;
6259 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6260 if (alignas_align)
6262 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6263 DECL_USER_ALIGN (decl) = 1;
6265 store_init_value (loc, decl, init, NULL_TREE);
6266 if (current_scope != file_scope
6267 && TREE_STATIC (decl)
6268 && !TREE_READONLY (decl)
6269 && DECL_DECLARED_INLINE_P (current_function_decl)
6270 && DECL_EXTERNAL (current_function_decl))
6271 record_inline_static (input_location, current_function_decl,
6272 decl, csi_modifiable);
6274 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6276 int failure = complete_array_type (&TREE_TYPE (decl),
6277 DECL_INITIAL (decl), true);
6278 /* If complete_array_type returns 3, it means that the
6279 initial value of the compound literal is empty. Allow it. */
6280 gcc_assert (failure == 0 || failure == 3);
6282 type = TREE_TYPE (decl);
6283 TREE_TYPE (DECL_INITIAL (decl)) = type;
6286 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6288 c_incomplete_type_error (loc, NULL_TREE, type);
6289 return error_mark_node;
6292 if (TREE_STATIC (decl)
6293 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6294 return error_mark_node;
6296 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6297 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6298 TREE_SIDE_EFFECTS (complit) = 1;
6300 layout_decl (decl, 0);
6302 if (TREE_STATIC (decl))
6304 /* This decl needs a name for the assembler output. */
6305 set_compound_literal_name (decl);
6306 DECL_DEFER_OUTPUT (decl) = 1;
6307 DECL_COMDAT (decl) = 1;
6308 pushdecl (decl);
6309 rest_of_decl_compilation (decl, 1, 0);
6311 else if (current_function_decl && !current_scope->parm_flag)
6312 pushdecl (decl);
6314 if (non_const)
6316 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6317 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6320 return complit;
6323 /* Check the type of a compound literal. Here we just check that it
6324 is valid for C++. */
6326 void
6327 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6329 if (warn_cxx_compat
6330 && (type_name->specs->typespec_kind == ctsk_tagdef
6331 || type_name->specs->typespec_kind == ctsk_tagfirstref
6332 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6333 warning_at (loc, OPT_Wc___compat,
6334 "defining a type in a compound literal is invalid in C++");
6337 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6338 replacing with appropriate values if they are invalid. */
6340 static void
6341 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6342 tree orig_name)
6344 tree type_mv;
6345 unsigned int max_width;
6346 unsigned HOST_WIDE_INT w;
6347 const char *name = (orig_name
6348 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6349 : _("<anonymous>"));
6351 /* Detect and ignore out of range field width and process valid
6352 field widths. */
6353 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6355 error_at (loc, "bit-field %qs width not an integer constant", name);
6356 *width = integer_one_node;
6358 else
6360 if (TREE_CODE (*width) != INTEGER_CST)
6362 *width = c_fully_fold (*width, false, NULL);
6363 if (TREE_CODE (*width) == INTEGER_CST)
6364 pedwarn (loc, OPT_Wpedantic,
6365 "bit-field %qs width not an integer constant expression",
6366 name);
6368 if (TREE_CODE (*width) != INTEGER_CST)
6370 error_at (loc, "bit-field %qs width not an integer constant", name);
6371 *width = integer_one_node;
6373 constant_expression_warning (*width);
6374 if (tree_int_cst_sgn (*width) < 0)
6376 error_at (loc, "negative width in bit-field %qs", name);
6377 *width = integer_one_node;
6379 else if (integer_zerop (*width) && orig_name)
6381 error_at (loc, "zero width for bit-field %qs", name);
6382 *width = integer_one_node;
6386 /* Detect invalid bit-field type. */
6387 if (TREE_CODE (*type) != INTEGER_TYPE
6388 && TREE_CODE (*type) != BOOLEAN_TYPE
6389 && TREE_CODE (*type) != ENUMERAL_TYPE)
6391 error_at (loc, "bit-field %qs has invalid type", name);
6392 *type = unsigned_type_node;
6395 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6397 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6398 name);
6399 *type = unsigned_type_node;
6402 type_mv = TYPE_MAIN_VARIANT (*type);
6403 if (!in_system_header_at (input_location)
6404 && type_mv != integer_type_node
6405 && type_mv != unsigned_type_node
6406 && type_mv != boolean_type_node)
6407 pedwarn_c90 (loc, OPT_Wpedantic,
6408 "type of bit-field %qs is a GCC extension", name);
6410 max_width = TYPE_PRECISION (*type);
6412 if (compare_tree_int (*width, max_width) > 0)
6414 error_at (loc, "width of %qs exceeds its type", name);
6415 w = max_width;
6416 *width = build_int_cst (integer_type_node, w);
6418 else
6419 w = tree_to_uhwi (*width);
6421 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6423 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6424 if (!lt
6425 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6426 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6427 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6433 /* Print warning about variable length array if necessary. */
6435 static void
6436 warn_variable_length_array (tree name, tree size)
6438 if (TREE_CONSTANT (size))
6440 if (name)
6441 pedwarn_c90 (input_location, OPT_Wvla,
6442 "ISO C90 forbids array %qE whose size "
6443 "cannot be evaluated", name);
6444 else
6445 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6446 "whose size cannot be evaluated");
6448 else
6450 if (name)
6451 pedwarn_c90 (input_location, OPT_Wvla,
6452 "ISO C90 forbids variable length array %qE", name);
6453 else
6454 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6455 "length array");
6459 /* Print warning about defaulting to int if necessary. */
6461 static void
6462 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6464 diagnostic_info diagnostic;
6465 va_list ap;
6466 rich_location richloc (line_table, location);
6468 va_start (ap, gmsgid);
6469 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6470 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6471 diagnostic.option_index = opt;
6472 diagnostic_report_diagnostic (global_dc, &diagnostic);
6473 va_end (ap);
6476 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6477 considering only those c_declspec_words found in LIST, which
6478 must be terminated by cdw_number_of_elements. */
6480 static location_t
6481 smallest_type_quals_location (const location_t *locations,
6482 const c_declspec_word *list)
6484 location_t loc = UNKNOWN_LOCATION;
6485 while (*list != cdw_number_of_elements)
6487 location_t newloc = locations[*list];
6488 if (loc == UNKNOWN_LOCATION
6489 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6490 loc = newloc;
6491 list++;
6494 return loc;
6497 /* Given declspecs and a declarator,
6498 determine the name and type of the object declared
6499 and construct a ..._DECL node for it.
6500 (In one case we can return a ..._TYPE node instead.
6501 For invalid input we sometimes return NULL_TREE.)
6503 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6505 DECL_CONTEXT says which syntactic context this declaration is in:
6506 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6507 FUNCDEF for a function definition. Like NORMAL but a few different
6508 error messages in each case. Return value may be zero meaning
6509 this definition is too screwy to try to parse.
6510 PARM for a parameter declaration (either within a function prototype
6511 or before a function body). Make a PARM_DECL, or return void_type_node.
6512 TYPENAME if for a typename (in a cast or sizeof).
6513 Don't make a DECL node; just return the ..._TYPE node.
6514 FIELD for a struct or union field; make a FIELD_DECL.
6515 INITIALIZED is true if the decl has an initializer.
6516 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6517 representing the width of the bit-field.
6518 DECL_ATTRS points to the list of attributes that should be added to this
6519 decl. Any nested attributes that belong on the decl itself will be
6520 added to this list.
6521 If EXPR is not NULL, any expressions that need to be evaluated as
6522 part of evaluating variably modified types will be stored in *EXPR.
6523 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6524 set to indicate whether operands in *EXPR can be used in constant
6525 expressions.
6526 DEPRECATED_STATE is a deprecated_states value indicating whether
6527 deprecation/unavailability warnings should be suppressed.
6529 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6530 It may also be so in the PARM case, for a prototype where the
6531 argument type is specified but not the name.
6533 This function is where the complicated C meanings of `static'
6534 and `extern' are interpreted. */
6536 static tree
6537 grokdeclarator (const struct c_declarator *declarator,
6538 struct c_declspecs *declspecs,
6539 enum decl_context decl_context, bool initialized, tree *width,
6540 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6541 enum deprecated_states deprecated_state)
6543 tree type = declspecs->type;
6544 bool threadp = declspecs->thread_p;
6545 bool constexprp = declspecs->constexpr_p;
6546 enum c_storage_class storage_class = declspecs->storage_class;
6547 int constp;
6548 int restrictp;
6549 int volatilep;
6550 int atomicp;
6551 int type_quals = TYPE_UNQUALIFIED;
6552 tree name = NULL_TREE;
6553 bool funcdef_flag = false;
6554 bool funcdef_syntax = false;
6555 bool size_varies = false;
6556 tree decl_attr = declspecs->decl_attr;
6557 int array_ptr_quals = TYPE_UNQUALIFIED;
6558 tree array_ptr_attrs = NULL_TREE;
6559 bool array_parm_static = false;
6560 bool array_parm_vla_unspec_p = false;
6561 tree returned_attrs = NULL_TREE;
6562 tree decl_id_attrs = NULL_TREE;
6563 bool bitfield = width != NULL;
6564 tree element_type;
6565 tree orig_qual_type = NULL;
6566 size_t orig_qual_indirect = 0;
6567 struct c_arg_info *arg_info = 0;
6568 addr_space_t as1, as2, address_space;
6569 location_t loc = UNKNOWN_LOCATION;
6570 tree expr_dummy;
6571 bool expr_const_operands_dummy;
6572 enum c_declarator_kind first_non_attr_kind;
6573 unsigned int alignas_align = 0;
6575 if (type == NULL_TREE)
6577 /* This can occur for auto on a parameter in C2X mode. Set a
6578 dummy type here so subsequent code can give diagnostics for
6579 this case. */
6580 gcc_assert (declspecs->c2x_auto_p);
6581 gcc_assert (decl_context == PARM);
6582 type = declspecs->type = integer_type_node;
6584 if (TREE_CODE (type) == ERROR_MARK)
6585 return error_mark_node;
6586 if (expr == NULL)
6588 expr = &expr_dummy;
6589 expr_dummy = NULL_TREE;
6591 if (expr_const_operands == NULL)
6592 expr_const_operands = &expr_const_operands_dummy;
6594 if (declspecs->expr)
6596 if (*expr)
6597 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6598 declspecs->expr);
6599 else
6600 *expr = declspecs->expr;
6602 *expr_const_operands = declspecs->expr_const_operands;
6604 if (decl_context == FUNCDEF)
6605 funcdef_flag = true, decl_context = NORMAL;
6607 /* Look inside a declarator for the name being declared
6608 and get it as an IDENTIFIER_NODE, for an error message. */
6610 const struct c_declarator *decl = declarator;
6612 first_non_attr_kind = cdk_attrs;
6613 while (decl)
6614 switch (decl->kind)
6616 case cdk_array:
6617 loc = decl->id_loc;
6618 /* FALL THRU. */
6620 case cdk_function:
6621 case cdk_pointer:
6622 funcdef_syntax = (decl->kind == cdk_function);
6623 if (first_non_attr_kind == cdk_attrs)
6624 first_non_attr_kind = decl->kind;
6625 decl = decl->declarator;
6626 break;
6628 case cdk_attrs:
6629 decl = decl->declarator;
6630 break;
6632 case cdk_id:
6633 loc = decl->id_loc;
6634 if (decl->u.id.id)
6635 name = decl->u.id.id;
6636 decl_id_attrs = decl->u.id.attrs;
6637 if (first_non_attr_kind == cdk_attrs)
6638 first_non_attr_kind = decl->kind;
6639 decl = 0;
6640 break;
6642 default:
6643 gcc_unreachable ();
6645 if (name == NULL_TREE)
6647 gcc_assert (decl_context == PARM
6648 || decl_context == TYPENAME
6649 || (decl_context == FIELD
6650 && declarator->kind == cdk_id));
6651 gcc_assert (!initialized);
6655 /* An enum type specifier (": specifier-qualifier-list") may only be
6656 specified when the enum is being defined or in an empty
6657 declaration of the form "enum identifier enum-type-specifier;".
6658 Except for the case of an empty declaration that has additional
6659 declaration specifiers, all invalid contexts (declarations that
6660 aren't empty, type names, parameter declarations, member
6661 declarations) pass through grokdeclarator. */
6662 if (declspecs->enum_type_specifier_ref_p)
6663 error_at (loc, "%<enum%> underlying type may not be specified here");
6665 /* A function definition's declarator must have the form of
6666 a function declarator. */
6668 if (funcdef_flag && !funcdef_syntax)
6669 return NULL_TREE;
6671 /* If this looks like a function definition, make it one,
6672 even if it occurs where parms are expected.
6673 Then store_parm_decls will reject it and not use it as a parm. */
6674 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6675 decl_context = PARM;
6677 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6679 if (declspecs->unavailable_p)
6680 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6681 else if (declspecs->deprecated_p
6682 && deprecated_state != DEPRECATED_SUPPRESS)
6683 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6686 if ((decl_context == NORMAL || decl_context == FIELD)
6687 && current_scope == file_scope
6688 && c_type_variably_modified_p (type))
6690 if (name)
6691 error_at (loc, "variably modified %qE at file scope", name);
6692 else
6693 error_at (loc, "variably modified field at file scope");
6694 type = integer_type_node;
6697 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6699 /* Diagnose defaulting to "int". */
6701 if (declspecs->default_int_p && !in_system_header_at (input_location))
6703 /* Issue a warning if this is an ISO C 99 program or if
6704 -Wreturn-type and this is a function, or if -Wimplicit;
6705 prefer the former warning since it is more explicit. */
6706 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6707 && funcdef_flag)
6708 warn_about_return_type = 1;
6709 else
6711 if (name)
6712 warn_defaults_to (loc, OPT_Wimplicit_int,
6713 "type defaults to %<int%> in declaration "
6714 "of %qE", name);
6715 else
6716 warn_defaults_to (loc, OPT_Wimplicit_int,
6717 "type defaults to %<int%> in type name");
6721 /* Adjust the type if a bit-field is being declared,
6722 -funsigned-bitfields applied and the type is not explicitly
6723 "signed". */
6724 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6725 && TREE_CODE (type) == INTEGER_TYPE)
6726 type = unsigned_type_for (type);
6728 /* Figure out the type qualifiers for the declaration. There are
6729 two ways a declaration can become qualified. One is something
6730 like `const int i' where the `const' is explicit. Another is
6731 something like `typedef const int CI; CI i' where the type of the
6732 declaration contains the `const'. A third possibility is that
6733 there is a type qualifier on the element type of a typedefed
6734 array type, in which case we should extract that qualifier so
6735 that c_apply_type_quals_to_decl receives the full list of
6736 qualifiers to work with (C90 is not entirely clear about whether
6737 duplicate qualifiers should be diagnosed in this case, but it
6738 seems most appropriate to do so). */
6739 element_type = strip_array_types (type);
6740 constp = declspecs->const_p + TYPE_READONLY (element_type);
6741 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6742 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6743 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6744 as1 = declspecs->address_space;
6745 as2 = TYPE_ADDR_SPACE (element_type);
6746 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6748 if (constp > 1)
6749 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6750 if (restrictp > 1)
6751 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6752 if (volatilep > 1)
6753 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6754 if (atomicp > 1)
6755 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6757 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6758 error_at (loc, "conflicting named address spaces (%s vs %s)",
6759 c_addr_space_name (as1), c_addr_space_name (as2));
6761 if ((TREE_CODE (type) == ARRAY_TYPE
6762 || first_non_attr_kind == cdk_array)
6763 && TYPE_QUALS (element_type))
6765 orig_qual_type = type;
6766 type = TYPE_MAIN_VARIANT (type);
6768 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6769 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6770 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6771 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6772 | ENCODE_QUAL_ADDR_SPACE (address_space));
6773 if (type_quals != TYPE_QUALS (element_type))
6774 orig_qual_type = NULL_TREE;
6776 /* Applying the _Atomic qualifier to an array type (through the use
6777 of typedefs or typeof) must be detected here. If the qualifier
6778 is introduced later, any appearance of applying it to an array is
6779 actually applying it to an element of that array. */
6780 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6781 error_at (loc, "%<_Atomic%>-qualified array type");
6783 /* Warn about storage classes that are invalid for certain
6784 kinds of declarations (parameters, typenames, etc.). */
6786 if (funcdef_flag
6787 && (threadp
6788 || constexprp
6789 || storage_class == csc_auto
6790 || storage_class == csc_register
6791 || storage_class == csc_typedef))
6793 if (storage_class == csc_auto)
6794 pedwarn (loc,
6795 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6796 "function definition declared %<auto%>");
6797 if (storage_class == csc_register)
6798 error_at (loc, "function definition declared %<register%>");
6799 if (storage_class == csc_typedef)
6800 error_at (loc, "function definition declared %<typedef%>");
6801 if (threadp)
6802 error_at (loc, "function definition declared %qs",
6803 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6804 threadp = false;
6805 /* The parser ensures a constexpr function definition never
6806 reaches here. */
6807 gcc_assert (!constexprp);
6808 if (storage_class == csc_auto
6809 || storage_class == csc_register
6810 || storage_class == csc_typedef)
6811 storage_class = csc_none;
6813 else if (decl_context != NORMAL && (storage_class != csc_none
6814 || threadp
6815 || constexprp
6816 || declspecs->c2x_auto_p))
6818 if (decl_context == PARM
6819 && storage_class == csc_register
6820 && !constexprp
6821 && !declspecs->c2x_auto_p)
6823 else
6825 switch (decl_context)
6827 case FIELD:
6828 if (name)
6829 error_at (loc, "storage class specified for structure "
6830 "field %qE", name);
6831 else
6832 error_at (loc, "storage class specified for structure field");
6833 break;
6834 case PARM:
6835 if (name)
6836 error_at (loc, "storage class specified for parameter %qE",
6837 name);
6838 else
6839 error_at (loc, "storage class specified for unnamed parameter");
6840 break;
6841 default:
6842 error_at (loc, "storage class specified for typename");
6843 break;
6845 storage_class = csc_none;
6846 threadp = false;
6847 constexprp = false;
6850 else if (storage_class == csc_extern
6851 && initialized
6852 && !funcdef_flag)
6854 /* 'extern' with initialization is invalid if not at file scope. */
6855 if (current_scope == file_scope)
6857 /* It is fine to have 'extern const' when compiling at C
6858 and C++ intersection. */
6859 if (!(warn_cxx_compat && constp))
6860 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6861 name);
6863 else
6864 error_at (loc, "%qE has both %<extern%> and initializer", name);
6866 else if (current_scope == file_scope)
6868 if (storage_class == csc_auto)
6869 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6870 name);
6871 if (pedantic && storage_class == csc_register)
6872 pedwarn (input_location, OPT_Wpedantic,
6873 "file-scope declaration of %qE specifies %<register%>", name);
6875 else
6877 if (storage_class == csc_extern && funcdef_flag)
6878 error_at (loc, "nested function %qE declared %<extern%>", name);
6879 else if (threadp && storage_class == csc_none)
6881 error_at (loc, "function-scope %qE implicitly auto and declared "
6882 "%qs", name,
6883 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6884 threadp = false;
6888 /* Now figure out the structure of the declarator proper.
6889 Descend through it, creating more complex types, until we reach
6890 the declared identifier (or NULL_TREE, in an absolute declarator).
6891 At each stage we maintain an unqualified version of the type
6892 together with any qualifiers that should be applied to it with
6893 c_build_qualified_type; this way, array types including
6894 multidimensional array types are first built up in unqualified
6895 form and then the qualified form is created with
6896 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6898 while (declarator && declarator->kind != cdk_id)
6900 if (type == error_mark_node)
6902 declarator = declarator->declarator;
6903 continue;
6906 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6907 a cdk_pointer (for *...),
6908 a cdk_function (for ...(...)),
6909 a cdk_attrs (for nested attributes),
6910 or a cdk_id (for the name being declared
6911 or the place in an absolute declarator
6912 where the name was omitted).
6913 For the last case, we have just exited the loop.
6915 At this point, TYPE is the type of elements of an array,
6916 or for a function to return, or for a pointer to point to.
6917 After this sequence of ifs, TYPE is the type of the
6918 array or function or pointer, and DECLARATOR has had its
6919 outermost layer removed. */
6921 if (array_ptr_quals != TYPE_UNQUALIFIED
6922 || array_ptr_attrs != NULL_TREE
6923 || array_parm_static)
6925 /* Only the innermost declarator (making a parameter be of
6926 array type which is converted to pointer type)
6927 may have static or type qualifiers. */
6928 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6929 array_ptr_quals = TYPE_UNQUALIFIED;
6930 array_ptr_attrs = NULL_TREE;
6931 array_parm_static = false;
6934 bool varmod = C_TYPE_VARIABLY_MODIFIED (type);
6936 switch (declarator->kind)
6938 case cdk_attrs:
6940 /* A declarator with embedded attributes. */
6941 tree attrs = declarator->u.attrs;
6942 const struct c_declarator *inner_decl;
6943 int attr_flags = 0;
6944 declarator = declarator->declarator;
6945 /* Standard attribute syntax precisely defines what entity
6946 an attribute in each position appertains to, so only
6947 apply laxity about positioning to GNU attribute syntax.
6948 Standard attributes applied to a function or array
6949 declarator apply exactly to that type; standard
6950 attributes applied to the identifier apply to the
6951 declaration rather than to the type, and are specified
6952 using a cdk_id declarator rather than using
6953 cdk_attrs. */
6954 inner_decl = declarator;
6955 while (inner_decl->kind == cdk_attrs)
6956 inner_decl = inner_decl->declarator;
6957 if (!cxx11_attribute_p (attrs))
6959 if (inner_decl->kind == cdk_id)
6960 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6961 else if (inner_decl->kind == cdk_function)
6962 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6963 else if (inner_decl->kind == cdk_array)
6964 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6966 attrs = c_warn_type_attributes (attrs);
6967 returned_attrs = decl_attributes (&type,
6968 chainon (returned_attrs, attrs),
6969 attr_flags);
6970 break;
6972 case cdk_array:
6974 tree itype = NULL_TREE;
6975 tree size = declarator->u.array.dimen;
6976 /* The index is a signed object `sizetype' bits wide. */
6977 tree index_type = c_common_signed_type (sizetype);
6979 array_ptr_quals = declarator->u.array.quals;
6980 array_ptr_attrs = declarator->u.array.attrs;
6981 array_parm_static = declarator->u.array.static_p;
6982 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6984 declarator = declarator->declarator;
6986 /* Check for some types that there cannot be arrays of. */
6988 if (VOID_TYPE_P (type))
6990 if (name)
6991 error_at (loc, "declaration of %qE as array of voids", name);
6992 else
6993 error_at (loc, "declaration of type name as array of voids");
6994 type = error_mark_node;
6997 if (TREE_CODE (type) == FUNCTION_TYPE)
6999 if (name)
7000 error_at (loc, "declaration of %qE as array of functions",
7001 name);
7002 else
7003 error_at (loc, "declaration of type name as array of "
7004 "functions");
7005 type = error_mark_node;
7008 if (pedantic && !in_system_header_at (input_location)
7009 && flexible_array_type_p (type))
7010 pedwarn (loc, OPT_Wpedantic,
7011 "invalid use of structure with flexible array member");
7013 if (size == error_mark_node)
7014 type = error_mark_node;
7016 if (type == error_mark_node)
7017 continue;
7019 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7021 type = error_mark_node;
7022 continue;
7025 /* If size was specified, set ITYPE to a range-type for
7026 that size. Otherwise, ITYPE remains null. finish_decl
7027 may figure it out from an initial value. */
7029 if (size)
7031 bool size_maybe_const = true;
7032 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7033 && !TREE_OVERFLOW (size));
7034 bool this_size_varies = false;
7036 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7037 lvalue. */
7038 STRIP_TYPE_NOPS (size);
7040 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7042 if (name)
7043 error_at (loc, "size of array %qE has non-integer type",
7044 name);
7045 else
7046 error_at (loc,
7047 "size of unnamed array has non-integer type");
7048 size = integer_one_node;
7049 size_int_const = true;
7051 /* This can happen with enum forward declaration. */
7052 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7054 if (name)
7055 error_at (loc, "size of array %qE has incomplete type",
7056 name);
7057 else
7058 error_at (loc, "size of unnamed array has incomplete "
7059 "type");
7060 size = integer_one_node;
7061 size_int_const = true;
7064 size = c_fully_fold (size, false, &size_maybe_const);
7066 if (pedantic && size_maybe_const && integer_zerop (size))
7068 if (name)
7069 pedwarn (loc, OPT_Wpedantic,
7070 "ISO C forbids zero-size array %qE", name);
7071 else
7072 pedwarn (loc, OPT_Wpedantic,
7073 "ISO C forbids zero-size array");
7076 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7078 constant_expression_warning (size);
7079 if (tree_int_cst_sgn (size) < 0)
7081 if (name)
7082 error_at (loc, "size of array %qE is negative", name);
7083 else
7084 error_at (loc, "size of unnamed array is negative");
7085 size = integer_one_node;
7086 size_int_const = true;
7088 /* Handle a size folded to an integer constant but
7089 not an integer constant expression. */
7090 if (!size_int_const)
7092 /* If this is a file scope declaration of an
7093 ordinary identifier, this is invalid code;
7094 diagnosing it here and not subsequently
7095 treating the type as variable-length avoids
7096 more confusing diagnostics later. */
7097 if ((decl_context == NORMAL || decl_context == FIELD)
7098 && current_scope == file_scope)
7099 pedwarn (input_location, 0,
7100 "variably modified %qE at file scope",
7101 name);
7102 else
7103 this_size_varies = size_varies = true;
7104 warn_variable_length_array (name, size);
7107 else if ((decl_context == NORMAL || decl_context == FIELD)
7108 && current_scope == file_scope)
7110 error_at (loc, "variably modified %qE at file scope", name);
7111 size = integer_one_node;
7113 else
7115 /* Make sure the array size remains visibly
7116 nonconstant even if it is (eg) a const variable
7117 with known value. */
7118 this_size_varies = size_varies = true;
7119 warn_variable_length_array (name, size);
7120 if (sanitize_flags_p (SANITIZE_VLA)
7121 && current_function_decl != NULL_TREE
7122 && decl_context == NORMAL)
7124 /* Evaluate the array size only once. */
7125 size = save_expr (size);
7126 size = c_fully_fold (size, false, NULL);
7127 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7128 ubsan_instrument_vla (loc, size),
7129 size);
7133 if (integer_zerop (size) && !this_size_varies)
7135 /* A zero-length array cannot be represented with
7136 an unsigned index type, which is what we'll
7137 get with build_index_type. Create an
7138 open-ended range instead. */
7139 itype = build_range_type (sizetype, size, NULL_TREE);
7141 else
7143 /* Arrange for the SAVE_EXPR on the inside of the
7144 MINUS_EXPR, which allows the -1 to get folded
7145 with the +1 that happens when building TYPE_SIZE. */
7146 if (size_varies)
7147 size = save_expr (size);
7148 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7149 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7150 integer_zero_node, size);
7152 /* Compute the maximum valid index, that is, size
7153 - 1. Do the calculation in index_type, so that
7154 if it is a variable the computations will be
7155 done in the proper mode. */
7156 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7157 convert (index_type, size),
7158 convert (index_type,
7159 size_one_node));
7161 /* The above overflows when size does not fit
7162 in index_type.
7163 ??? While a size of INT_MAX+1 technically shouldn't
7164 cause an overflow (because we subtract 1), handling
7165 this case seems like an unnecessary complication. */
7166 if (TREE_CODE (size) == INTEGER_CST
7167 && !int_fits_type_p (size, index_type))
7169 if (name)
7170 error_at (loc, "size of array %qE is too large",
7171 name);
7172 else
7173 error_at (loc, "size of unnamed array is too large");
7174 type = error_mark_node;
7175 continue;
7178 itype = build_index_type (itype);
7180 if (this_size_varies)
7182 if (TREE_SIDE_EFFECTS (size))
7184 if (*expr)
7185 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7186 *expr, size);
7187 else
7188 *expr = size;
7190 *expr_const_operands &= size_maybe_const;
7193 else if (decl_context == FIELD)
7195 bool flexible_array_member = false;
7196 if (array_parm_vla_unspec_p)
7197 /* Field names can in fact have function prototype
7198 scope so [*] is disallowed here through making
7199 the field variably modified, not through being
7200 something other than a declaration with function
7201 prototype scope. */
7202 size_varies = true;
7203 else
7205 const struct c_declarator *t = declarator;
7206 while (t->kind == cdk_attrs)
7207 t = t->declarator;
7208 flexible_array_member = (t->kind == cdk_id);
7210 if (flexible_array_member
7211 && !in_system_header_at (input_location))
7212 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7213 "support flexible array members");
7215 /* ISO C99 Flexible array members are effectively
7216 identical to GCC's zero-length array extension. */
7217 if (flexible_array_member || array_parm_vla_unspec_p)
7218 itype = build_range_type (sizetype, size_zero_node,
7219 NULL_TREE);
7221 else if (decl_context == PARM)
7223 if (array_parm_vla_unspec_p)
7225 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7226 size_varies = true;
7229 else if (decl_context == TYPENAME)
7231 if (array_parm_vla_unspec_p)
7233 /* C99 6.7.5.2p4 */
7234 warning (0, "%<[*]%> not in a declaration");
7235 /* We use this to avoid messing up with incomplete
7236 array types of the same type, that would
7237 otherwise be modified below. */
7238 itype = build_range_type (sizetype, size_zero_node,
7239 NULL_TREE);
7240 size_varies = true;
7244 /* Complain about arrays of incomplete types. */
7245 if (!COMPLETE_TYPE_P (type))
7247 auto_diagnostic_group d;
7248 error_at (loc, "array type has incomplete element type %qT",
7249 type);
7250 /* See if we can be more helpful. */
7251 if (TREE_CODE (type) == ARRAY_TYPE)
7253 if (name)
7254 inform (loc, "declaration of %qE as multidimensional "
7255 "array must have bounds for all dimensions "
7256 "except the first", name);
7257 else
7258 inform (loc, "declaration of multidimensional array "
7259 "must have bounds for all dimensions except "
7260 "the first");
7262 type = error_mark_node;
7264 else
7265 /* When itype is NULL, a shared incomplete array type is
7266 returned for all array of a given type. Elsewhere we
7267 make sure we don't complete that type before copying
7268 it, but here we want to make sure we don't ever
7269 modify the shared type, so we gcc_assert (itype)
7270 below. */
7272 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7273 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7274 type = build_qualified_type (type,
7275 ENCODE_QUAL_ADDR_SPACE (as));
7277 type = build_array_type (type, itype);
7280 if (type != error_mark_node)
7282 if (size_varies)
7284 /* It is ok to modify type here even if itype is
7285 NULL: if size_varies, we're in a
7286 multi-dimensional array and the inner type has
7287 variable size, so the enclosing shared array type
7288 must too. */
7289 if (size && TREE_CODE (size) == INTEGER_CST)
7290 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7291 C_TYPE_VARIABLE_SIZE (type) = 1;
7294 /* The GCC extension for zero-length arrays differs from
7295 ISO flexible array members in that sizeof yields
7296 zero. */
7297 if (size && integer_zerop (size))
7299 gcc_assert (itype);
7300 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7301 TYPE_SIZE (type) = bitsize_zero_node;
7302 TYPE_SIZE_UNIT (type) = size_zero_node;
7303 SET_TYPE_STRUCTURAL_EQUALITY (type);
7305 if (array_parm_vla_unspec_p)
7307 gcc_assert (itype);
7308 /* The type is complete. C99 6.7.5.2p4 */
7309 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7310 TYPE_SIZE (type) = bitsize_zero_node;
7311 TYPE_SIZE_UNIT (type) = size_zero_node;
7312 SET_TYPE_STRUCTURAL_EQUALITY (type);
7315 if (!valid_array_size_p (loc, type, name))
7316 type = error_mark_node;
7319 if (decl_context != PARM
7320 && (array_ptr_quals != TYPE_UNQUALIFIED
7321 || array_ptr_attrs != NULL_TREE
7322 || array_parm_static))
7324 error_at (loc, "static or type qualifiers in non-parameter "
7325 "array declarator");
7326 array_ptr_quals = TYPE_UNQUALIFIED;
7327 array_ptr_attrs = NULL_TREE;
7328 array_parm_static = false;
7330 orig_qual_indirect++;
7331 break;
7333 case cdk_function:
7335 /* Say it's a definition only for the declarator closest
7336 to the identifier, apart possibly from some
7337 attributes. */
7338 bool really_funcdef = false;
7339 tree arg_types;
7340 orig_qual_type = NULL_TREE;
7341 if (funcdef_flag)
7343 const struct c_declarator *t = declarator->declarator;
7344 while (t->kind == cdk_attrs)
7345 t = t->declarator;
7346 really_funcdef = (t->kind == cdk_id);
7349 /* Declaring a function type. Make sure we have a valid
7350 type for the function to return. */
7351 if (type == error_mark_node)
7352 continue;
7354 size_varies = false;
7356 /* Warn about some types functions can't return. */
7357 if (TREE_CODE (type) == FUNCTION_TYPE)
7359 if (name)
7360 error_at (loc, "%qE declared as function returning a "
7361 "function", name);
7362 else
7363 error_at (loc, "type name declared as function "
7364 "returning a function");
7365 type = integer_type_node;
7367 if (TREE_CODE (type) == ARRAY_TYPE)
7369 if (name)
7370 error_at (loc, "%qE declared as function returning an array",
7371 name);
7372 else
7373 error_at (loc, "type name declared as function returning "
7374 "an array");
7375 type = integer_type_node;
7378 /* Construct the function type and go to the next
7379 inner layer of declarator. */
7380 arg_info = declarator->u.arg_info;
7381 arg_types = grokparms (arg_info, really_funcdef);
7383 /* Type qualifiers before the return type of the function
7384 qualify the return type, not the function type. */
7385 if (type_quals)
7387 const enum c_declspec_word ignored_quals_list[] =
7389 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7390 cdw_atomic, cdw_number_of_elements
7392 location_t specs_loc
7393 = smallest_type_quals_location (declspecs->locations,
7394 ignored_quals_list);
7395 if (specs_loc == UNKNOWN_LOCATION)
7396 specs_loc = declspecs->locations[cdw_typedef];
7397 if (specs_loc == UNKNOWN_LOCATION)
7398 specs_loc = loc;
7400 /* Type qualifiers on a function return type are
7401 normally permitted by the standard but have no
7402 effect, so give a warning at -Wreturn-type.
7403 Qualifiers on a void return type are banned on
7404 function definitions in ISO C; GCC used to used
7405 them for noreturn functions. The resolution of C11
7406 DR#423 means qualifiers (other than _Atomic) are
7407 actually removed from the return type when
7408 determining the function type. */
7409 int quals_used = type_quals;
7410 if (flag_isoc11)
7411 quals_used &= TYPE_QUAL_ATOMIC;
7412 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7413 pedwarn (specs_loc, 0,
7414 "function definition has qualified void "
7415 "return type");
7416 else
7417 warning_at (specs_loc, OPT_Wignored_qualifiers,
7418 "type qualifiers ignored on function "
7419 "return type");
7421 /* Ensure an error for restrict on invalid types; the
7422 DR#423 resolution is not entirely clear about
7423 this. */
7424 if (flag_isoc11
7425 && (type_quals & TYPE_QUAL_RESTRICT)
7426 && (!POINTER_TYPE_P (type)
7427 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7428 error_at (loc, "invalid use of %<restrict%>");
7429 type = c_build_qualified_type (type, quals_used);
7431 type_quals = TYPE_UNQUALIFIED;
7433 type = build_function_type (type, arg_types,
7434 arg_info->no_named_args_stdarg_p);
7435 declarator = declarator->declarator;
7437 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7438 the formal parameter list of this FUNCTION_TYPE to point to
7439 the FUNCTION_TYPE node itself. */
7441 c_arg_tag *tag;
7442 unsigned ix;
7444 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7445 TYPE_CONTEXT (tag->type) = type;
7447 break;
7449 case cdk_pointer:
7451 /* Merge any constancy or volatility into the target type
7452 for the pointer. */
7453 if ((type_quals & TYPE_QUAL_ATOMIC)
7454 && TREE_CODE (type) == FUNCTION_TYPE)
7456 error_at (loc,
7457 "%<_Atomic%>-qualified function type");
7458 type_quals &= ~TYPE_QUAL_ATOMIC;
7460 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7461 && type_quals)
7462 pedwarn (loc, OPT_Wpedantic,
7463 "ISO C forbids qualified function types");
7464 if (type_quals)
7465 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7466 orig_qual_indirect);
7467 orig_qual_type = NULL_TREE;
7468 size_varies = false;
7470 /* When the pointed-to type involves components of variable size,
7471 care must be taken to ensure that the size evaluation code is
7472 emitted early enough to dominate all the possible later uses
7473 and late enough for the variables on which it depends to have
7474 been assigned.
7476 This is expected to happen automatically when the pointed-to
7477 type has a name/declaration of it's own, but special attention
7478 is required if the type is anonymous.
7480 We attach an artificial TYPE_DECL to such pointed-to type
7481 and arrange for it to be included in a DECL_EXPR. This
7482 forces the sizes evaluation at a safe point and ensures it
7483 is not deferred until e.g. within a deeper conditional context.
7485 PARM contexts have no enclosing statement list that
7486 can hold the DECL_EXPR, so we need to use a BIND_EXPR
7487 instead, and add it to the list of expressions that
7488 need to be evaluated.
7490 TYPENAME contexts do have an enclosing statement list,
7491 but it would be incorrect to use it, as the size should
7492 only be evaluated if the containing expression is
7493 evaluated. We might also be in the middle of an
7494 expression with side effects on the pointed-to type size
7495 "arguments" prior to the pointer declaration point and
7496 the fake TYPE_DECL in the enclosing context would force
7497 the size evaluation prior to the side effects. We therefore
7498 use BIND_EXPRs in TYPENAME contexts too. */
7499 if (!TYPE_NAME (type)
7500 && c_type_variably_modified_p (type))
7502 tree bind = NULL_TREE;
7503 if (decl_context == TYPENAME || decl_context == PARM)
7505 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7506 NULL_TREE, NULL_TREE);
7507 TREE_SIDE_EFFECTS (bind) = 1;
7508 BIND_EXPR_BODY (bind) = push_stmt_list ();
7509 push_scope ();
7511 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7512 DECL_ARTIFICIAL (decl) = 1;
7513 pushdecl (decl);
7514 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7515 TYPE_NAME (type) = decl;
7516 if (bind)
7518 pop_scope ();
7519 BIND_EXPR_BODY (bind)
7520 = pop_stmt_list (BIND_EXPR_BODY (bind));
7521 if (*expr)
7522 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7523 bind);
7524 else
7525 *expr = bind;
7529 type = c_build_pointer_type (type);
7531 /* Process type qualifiers (such as const or volatile)
7532 that were given inside the `*'. */
7533 type_quals = declarator->u.pointer_quals;
7535 declarator = declarator->declarator;
7536 break;
7538 default:
7539 gcc_unreachable ();
7541 if (type != error_mark_node)
7542 C_TYPE_VARIABLY_MODIFIED (type) = varmod || size_varies;
7544 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7545 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7547 /* Now TYPE has the actual type, apart from any qualifiers in
7548 TYPE_QUALS. */
7550 /* Warn about address space used for things other than static memory or
7551 pointers. */
7552 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7553 if (!ADDR_SPACE_GENERIC_P (address_space))
7555 if (decl_context == NORMAL)
7557 switch (storage_class)
7559 case csc_auto:
7560 error ("%qs combined with %<auto%> qualifier for %qE",
7561 c_addr_space_name (address_space), name);
7562 break;
7563 case csc_register:
7564 error ("%qs combined with %<register%> qualifier for %qE",
7565 c_addr_space_name (address_space), name);
7566 break;
7567 case csc_none:
7568 if (current_function_scope)
7570 error ("%qs specified for auto variable %qE",
7571 c_addr_space_name (address_space), name);
7572 break;
7574 break;
7575 case csc_static:
7576 case csc_extern:
7577 case csc_typedef:
7578 break;
7579 default:
7580 gcc_unreachable ();
7583 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7585 if (name)
7586 error ("%qs specified for parameter %qE",
7587 c_addr_space_name (address_space), name);
7588 else
7589 error ("%qs specified for unnamed parameter",
7590 c_addr_space_name (address_space));
7592 else if (decl_context == FIELD)
7594 if (name)
7595 error ("%qs specified for structure field %qE",
7596 c_addr_space_name (address_space), name);
7597 else
7598 error ("%qs specified for structure field",
7599 c_addr_space_name (address_space));
7603 /* Check the type and width of a bit-field. */
7604 if (bitfield)
7606 check_bitfield_type_and_width (loc, &type, width, name);
7607 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7608 atomic types are permitted for bit-fields; we have no code to
7609 make bit-field accesses atomic, so disallow them. */
7610 if (type_quals & TYPE_QUAL_ATOMIC)
7612 if (name)
7613 error_at (loc, "bit-field %qE has atomic type", name);
7614 else
7615 error_at (loc, "bit-field has atomic type");
7616 type_quals &= ~TYPE_QUAL_ATOMIC;
7620 /* Reject invalid uses of _Alignas. */
7621 if (declspecs->alignas_p)
7623 if (storage_class == csc_typedef)
7624 error_at (loc, "alignment specified for typedef %qE", name);
7625 else if (storage_class == csc_register)
7626 error_at (loc, "alignment specified for %<register%> object %qE",
7627 name);
7628 else if (decl_context == PARM)
7630 if (name)
7631 error_at (loc, "alignment specified for parameter %qE", name);
7632 else
7633 error_at (loc, "alignment specified for unnamed parameter");
7635 else if (bitfield)
7637 if (name)
7638 error_at (loc, "alignment specified for bit-field %qE", name);
7639 else
7640 error_at (loc, "alignment specified for unnamed bit-field");
7642 else if (TREE_CODE (type) == FUNCTION_TYPE)
7643 error_at (loc, "alignment specified for function %qE", name);
7644 else if (declspecs->align_log != -1 && TYPE_P (type))
7646 alignas_align = 1U << declspecs->align_log;
7647 if (alignas_align < min_align_of_type (type))
7649 if (name)
7650 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7651 "alignment of %qE", name);
7652 else
7653 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7654 "alignment of unnamed field");
7655 alignas_align = 0;
7660 /* If this is declaring a typedef name, return a TYPE_DECL. */
7662 if (storage_class == csc_typedef)
7664 tree decl;
7665 if ((type_quals & TYPE_QUAL_ATOMIC)
7666 && TREE_CODE (type) == FUNCTION_TYPE)
7668 error_at (loc,
7669 "%<_Atomic%>-qualified function type");
7670 type_quals &= ~TYPE_QUAL_ATOMIC;
7672 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7673 && type_quals)
7674 pedwarn (loc, OPT_Wpedantic,
7675 "ISO C forbids qualified function types");
7676 if (type_quals)
7677 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7678 orig_qual_indirect);
7679 decl = build_decl (declarator->id_loc,
7680 TYPE_DECL, declarator->u.id.id, type);
7681 if (declspecs->explicit_signed_p)
7682 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7683 if (declspecs->inline_p)
7684 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7685 if (declspecs->noreturn_p)
7686 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7688 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7690 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7692 if (b != NULL
7693 && b->decl != NULL_TREE
7694 && (B_IN_CURRENT_SCOPE (b)
7695 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7696 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7698 auto_diagnostic_group d;
7699 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7700 ("using %qD as both a typedef and a tag is "
7701 "invalid in C++"), decl)
7702 && b->locus != UNKNOWN_LOCATION)
7703 inform (b->locus, "originally defined here");
7707 return decl;
7710 /* If this is a type name (such as, in a cast or sizeof),
7711 compute the type and return it now. */
7713 if (decl_context == TYPENAME)
7715 /* Note that the grammar rejects storage classes in typenames
7716 and fields. */
7717 gcc_assert (storage_class == csc_none && !threadp
7718 && !declspecs->inline_p && !declspecs->noreturn_p);
7719 if ((type_quals & TYPE_QUAL_ATOMIC)
7720 && TREE_CODE (type) == FUNCTION_TYPE)
7722 error_at (loc,
7723 "%<_Atomic%>-qualified function type");
7724 type_quals &= ~TYPE_QUAL_ATOMIC;
7726 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7727 && type_quals)
7728 pedwarn (loc, OPT_Wpedantic,
7729 "ISO C forbids const or volatile function types");
7730 if (type_quals)
7731 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7732 orig_qual_indirect);
7733 return type;
7736 if (pedantic && decl_context == FIELD
7737 && c_type_variably_modified_p (type))
7739 /* C99 6.7.2.1p8 */
7740 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7741 "have a variably modified type");
7744 /* Aside from typedefs and type names (handle above),
7745 `void' at top level (not within pointer)
7746 is allowed only in public variables.
7747 We don't complain about parms either, but that is because
7748 a better error message can be made later. */
7750 if (VOID_TYPE_P (type) && decl_context != PARM
7751 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7752 && (storage_class == csc_extern
7753 || (current_scope == file_scope
7754 && !(storage_class == csc_static
7755 || storage_class == csc_register)))))
7757 error_at (loc, "variable or field %qE declared void", name);
7758 type = integer_type_node;
7761 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7762 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7765 tree decl;
7767 if (decl_context == PARM)
7769 tree promoted_type;
7770 bool array_parameter_p = false;
7772 /* A parameter declared as an array of T is really a pointer to T.
7773 One declared as a function is really a pointer to a function. */
7775 if (TREE_CODE (type) == ARRAY_TYPE)
7777 /* Transfer const-ness of array into that of type pointed to. */
7778 type = TREE_TYPE (type);
7779 if (orig_qual_type != NULL_TREE)
7781 if (orig_qual_indirect == 0)
7782 orig_qual_type = TREE_TYPE (orig_qual_type);
7783 else
7784 orig_qual_indirect--;
7786 if (type_quals)
7787 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7788 orig_qual_indirect);
7789 type = c_build_pointer_type (type);
7790 type_quals = array_ptr_quals;
7791 if (type_quals)
7792 type = c_build_qualified_type (type, type_quals);
7794 /* We don't yet implement attributes in this context. */
7795 if (array_ptr_attrs != NULL_TREE)
7796 warning_at (loc, OPT_Wattributes,
7797 "attributes in parameter array declarator ignored");
7799 size_varies = false;
7800 array_parameter_p = true;
7802 else if (TREE_CODE (type) == FUNCTION_TYPE)
7804 if (type_quals & TYPE_QUAL_ATOMIC)
7806 error_at (loc,
7807 "%<_Atomic%>-qualified function type");
7808 type_quals &= ~TYPE_QUAL_ATOMIC;
7810 else if (type_quals)
7811 pedwarn (loc, OPT_Wpedantic,
7812 "ISO C forbids qualified function types");
7813 if (type_quals)
7814 type = c_build_qualified_type (type, type_quals);
7815 type = c_build_pointer_type (type);
7816 type_quals = TYPE_UNQUALIFIED;
7818 else if (type_quals)
7819 type = c_build_qualified_type (type, type_quals);
7821 decl = build_decl (declarator->id_loc,
7822 PARM_DECL, declarator->u.id.id, type);
7823 if (size_varies)
7824 C_DECL_VARIABLE_SIZE (decl) = 1;
7825 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7827 /* Compute the type actually passed in the parmlist,
7828 for the case where there is no prototype.
7829 (For example, shorts and chars are passed as ints.)
7830 When there is a prototype, this is overridden later. */
7832 if (type == error_mark_node)
7833 promoted_type = type;
7834 else
7835 promoted_type = c_type_promotes_to (type);
7837 DECL_ARG_TYPE (decl) = promoted_type;
7838 if (declspecs->inline_p)
7839 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7840 if (declspecs->noreturn_p)
7841 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7843 else if (decl_context == FIELD)
7845 /* Note that the grammar rejects storage classes in typenames
7846 and fields. */
7847 gcc_assert (storage_class == csc_none && !threadp
7848 && !declspecs->inline_p && !declspecs->noreturn_p);
7850 /* Structure field. It may not be a function. */
7852 if (TREE_CODE (type) == FUNCTION_TYPE)
7854 error_at (loc, "field %qE declared as a function", name);
7855 type = build_pointer_type (type);
7857 else if (TREE_CODE (type) != ERROR_MARK
7858 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7860 if (name)
7861 error_at (loc, "field %qE has incomplete type", name);
7862 else
7863 error_at (loc, "unnamed field has incomplete type");
7864 type = error_mark_node;
7866 else if (TREE_CODE (type) == ARRAY_TYPE
7867 && TYPE_DOMAIN (type) == NULL_TREE)
7869 /* We have a flexible array member through a typedef.
7870 Set suitable range. Whether this is a correct position
7871 for a flexible array member will be determined elsewhere. */
7872 if (!in_system_header_at (input_location))
7873 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7874 "support flexible array members");
7875 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7876 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7877 NULL_TREE);
7878 if (orig_qual_indirect == 0)
7879 orig_qual_type = NULL_TREE;
7881 if (type != error_mark_node
7882 && !verify_type_context (loc, TCTX_FIELD, type))
7883 type = error_mark_node;
7885 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7886 orig_qual_indirect);
7887 decl = build_decl (declarator->id_loc,
7888 FIELD_DECL, declarator->u.id.id, type);
7889 DECL_NONADDRESSABLE_P (decl) = bitfield;
7890 if (bitfield && !declarator->u.id.id)
7891 DECL_PADDING_P (decl) = 1;
7893 if (size_varies)
7894 C_DECL_VARIABLE_SIZE (decl) = 1;
7896 else if (TREE_CODE (type) == FUNCTION_TYPE)
7898 if (storage_class == csc_register || threadp || constexprp)
7900 error_at (loc, "invalid storage class for function %qE", name);
7902 else if (current_scope != file_scope)
7904 /* Function declaration not at file scope. Storage
7905 classes other than `extern' are not allowed, C99
7906 6.7.1p5, and `extern' makes no difference. However,
7907 GCC allows 'auto', perhaps with 'inline', to support
7908 nested functions. */
7909 if (storage_class == csc_auto)
7910 pedwarn (loc, OPT_Wpedantic,
7911 "invalid storage class for function %qE", name);
7912 else if (storage_class == csc_static)
7914 error_at (loc, "invalid storage class for function %qE", name);
7915 if (funcdef_flag)
7916 storage_class = declspecs->storage_class = csc_none;
7917 else
7918 return NULL_TREE;
7922 decl = build_decl (declarator->id_loc,
7923 FUNCTION_DECL, declarator->u.id.id, type);
7924 decl = build_decl_attribute_variant (decl, decl_attr);
7926 if (type_quals & TYPE_QUAL_ATOMIC)
7928 error_at (loc,
7929 "%<_Atomic%>-qualified function type");
7930 type_quals &= ~TYPE_QUAL_ATOMIC;
7932 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7933 pedwarn (loc, OPT_Wpedantic,
7934 "ISO C forbids qualified function types");
7936 /* Every function declaration is an external reference
7937 (DECL_EXTERNAL) except for those which are not at file
7938 scope and are explicitly declared "auto". This is
7939 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7940 GCC to signify a forward declaration of a nested function. */
7941 if (storage_class == csc_auto && current_scope != file_scope)
7942 DECL_EXTERNAL (decl) = 0;
7943 /* In C99, a function which is declared 'inline' with 'extern'
7944 is not an external reference (which is confusing). It
7945 means that the later definition of the function must be output
7946 in this file, C99 6.7.4p6. In GNU C89, a function declared
7947 'extern inline' is an external reference. */
7948 else if (declspecs->inline_p && storage_class != csc_static)
7949 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7950 == flag_gnu89_inline);
7951 else
7952 DECL_EXTERNAL (decl) = !initialized;
7954 /* Record absence of global scope for `static' or `auto'. */
7955 TREE_PUBLIC (decl)
7956 = !(storage_class == csc_static || storage_class == csc_auto);
7958 /* For a function definition, record the argument information
7959 block where store_parm_decls will look for it. */
7960 if (funcdef_flag)
7961 current_function_arg_info = arg_info;
7963 if (declspecs->default_int_p)
7964 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7966 /* Record presence of `inline' and `_Noreturn', if it is
7967 reasonable. */
7968 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7970 if (declspecs->inline_p)
7971 pedwarn (loc, 0, "cannot inline function %<main%>");
7972 if (declspecs->noreturn_p)
7973 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7975 else
7977 if (declspecs->inline_p)
7978 /* Record that the function is declared `inline'. */
7979 DECL_DECLARED_INLINE_P (decl) = 1;
7980 if (declspecs->noreturn_p)
7982 if (flag_isoc99)
7983 pedwarn_c99 (loc, OPT_Wpedantic,
7984 "ISO C99 does not support %<_Noreturn%>");
7985 else
7986 pedwarn_c99 (loc, OPT_Wpedantic,
7987 "ISO C90 does not support %<_Noreturn%>");
7988 TREE_THIS_VOLATILE (decl) = 1;
7992 else
7994 /* It's a variable. */
7995 /* An uninitialized decl with `extern' is a reference. */
7996 int extern_ref = !initialized && storage_class == csc_extern;
7998 if (constexprp)
8000 /* The type of a constexpr variable must not be variably
8001 modified, volatile, atomic or restrict qualified or
8002 have a member with such a qualifier. const
8003 qualification is implicitly added, and, at file scope,
8004 has internal linkage. */
8005 if (c_type_variably_modified_p (type))
8006 error_at (loc, "%<constexpr%> object has variably modified "
8007 "type");
8008 if (type_quals
8009 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8010 error_at (loc, "invalid qualifiers for %<constexpr%> object");
8011 else
8013 tree type_no_array = strip_array_types (type);
8014 if (RECORD_OR_UNION_TYPE_P (type_no_array)
8015 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8016 error_at (loc, "invalid qualifiers for field of "
8017 "%<constexpr%> object");
8019 type_quals |= TYPE_QUAL_CONST;
8020 if (current_scope == file_scope)
8021 storage_class = csc_static;
8024 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8025 orig_qual_indirect);
8027 /* C99 6.2.2p7: It is invalid (compile-time undefined
8028 behavior) to create an 'extern' declaration for a
8029 variable if there is a global declaration that is
8030 'static' and the global declaration is not visible.
8031 (If the static declaration _is_ currently visible,
8032 the 'extern' declaration is taken to refer to that decl.) */
8033 if (extern_ref && current_scope != file_scope)
8035 tree global_decl = identifier_global_value (declarator->u.id.id);
8036 tree visible_decl = lookup_name (declarator->u.id.id);
8038 if (global_decl
8039 && global_decl != visible_decl
8040 && VAR_P (global_decl)
8041 && !TREE_PUBLIC (global_decl))
8042 error_at (loc, "variable previously declared %<static%> "
8043 "redeclared %<extern%>");
8046 decl = build_decl (declarator->id_loc,
8047 VAR_DECL, declarator->u.id.id, type);
8048 if (size_varies)
8049 C_DECL_VARIABLE_SIZE (decl) = 1;
8050 if (constexprp)
8051 C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8053 if (declspecs->inline_p)
8054 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8055 if (declspecs->noreturn_p)
8056 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8058 /* At file scope, an initialized extern declaration may follow
8059 a static declaration. In that case, DECL_EXTERNAL will be
8060 reset later in start_decl. */
8061 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8063 /* At file scope, the presence of a `static' or `register' storage
8064 class specifier, or the absence of all storage class specifiers
8065 makes this declaration a definition (perhaps tentative). Also,
8066 the absence of `static' makes it public. */
8067 if (current_scope == file_scope)
8069 TREE_PUBLIC (decl) = storage_class != csc_static;
8070 TREE_STATIC (decl) = !extern_ref;
8072 /* Not at file scope, only `static' makes a static definition. */
8073 else
8075 TREE_STATIC (decl) = (storage_class == csc_static);
8076 TREE_PUBLIC (decl) = extern_ref;
8079 if (threadp)
8080 set_decl_tls_model (decl, decl_default_tls_model (decl));
8083 if ((storage_class == csc_extern
8084 || (storage_class == csc_none
8085 && TREE_CODE (type) == FUNCTION_TYPE
8086 && !funcdef_flag))
8087 && c_type_variably_modified_p (type))
8089 /* C99 6.7.5.2p2 */
8090 if (TREE_CODE (type) == FUNCTION_TYPE)
8091 error_at (loc, "non-nested function with variably modified type");
8092 else
8093 error_at (loc, "object with variably modified type must have "
8094 "no linkage");
8097 /* For nested functions disqualify ones taking VLAs by value
8098 from inlining since the middle-end cannot deal with this.
8099 ??? We should arrange for those to be passed by reference
8100 with emitting the copy on the caller side in the frontend. */
8101 if (storage_class == csc_none
8102 && TREE_CODE (type) == FUNCTION_TYPE)
8103 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8105 tree arg = TREE_VALUE (al);
8106 if (arg != error_mark_node
8107 && C_TYPE_VARIABLE_SIZE (arg))
8109 DECL_UNINLINABLE (decl) = 1;
8110 break;
8114 /* Record `register' declaration for warnings on &
8115 and in case doing stupid register allocation. */
8117 if (storage_class == csc_register)
8119 C_DECL_REGISTER (decl) = 1;
8120 DECL_REGISTER (decl) = 1;
8123 /* Record constancy and volatility. */
8124 c_apply_type_quals_to_decl (type_quals, decl);
8126 /* Apply _Alignas specifiers. */
8127 if (alignas_align)
8129 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8130 DECL_USER_ALIGN (decl) = 1;
8133 /* If a type has volatile components, it should be stored in memory.
8134 Otherwise, the fact that those components are volatile
8135 will be ignored, and would even crash the compiler.
8136 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8137 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8138 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8139 || TREE_CODE (decl) == RESULT_DECL))
8141 /* It is not an error for a structure with volatile fields to
8142 be declared register, but reset DECL_REGISTER since it
8143 cannot actually go in a register. */
8144 int was_reg = C_DECL_REGISTER (decl);
8145 C_DECL_REGISTER (decl) = 0;
8146 DECL_REGISTER (decl) = 0;
8147 c_mark_addressable (decl);
8148 C_DECL_REGISTER (decl) = was_reg;
8151 /* This is the earliest point at which we might know the assembler
8152 name of a variable. Thus, if it's known before this, die horribly. */
8153 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8154 || !DECL_ASSEMBLER_NAME_SET_P (decl));
8156 if (warn_cxx_compat
8157 && VAR_P (decl)
8158 && TREE_PUBLIC (decl)
8159 && TREE_STATIC (decl)
8160 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8161 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8162 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8163 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8164 ("non-local variable %qD with anonymous type is "
8165 "questionable in C++"),
8166 decl);
8168 return decl;
8172 /* Decode the parameter-list info for a function type or function definition.
8173 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8174 if there is an identifier list instead of a parameter decl list).
8175 These two functions are separate because when a function returns
8176 or receives functions then each is called multiple times but the order
8177 of calls is different. The last call to `grokparms' is always the one
8178 that contains the formal parameter names of a function definition.
8180 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8182 FUNCDEF_FLAG is true for a function definition, false for
8183 a mere declaration. A nonempty identifier-list gets an error message
8184 when FUNCDEF_FLAG is false. */
8186 static tree
8187 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8189 tree arg_types = arg_info->types;
8191 if (funcdef_flag && arg_info->had_vla_unspec)
8193 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8194 /* C99 6.7.5.2p4 */
8195 error ("%<[*]%> not allowed in other than function prototype scope");
8198 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc2x
8199 && !in_system_header_at (input_location))
8200 warning (OPT_Wstrict_prototypes,
8201 "function declaration isn%'t a prototype");
8203 if (arg_types == error_mark_node)
8204 /* Don't set TYPE_ARG_TYPES in this case. */
8205 return NULL_TREE;
8207 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8209 if (!funcdef_flag)
8211 pedwarn (input_location, 0, "parameter names (without types) in "
8212 "function declaration");
8213 arg_info->parms = NULL_TREE;
8215 else
8216 arg_info->parms = arg_info->types;
8218 arg_info->types = NULL_TREE;
8219 return NULL_TREE;
8221 else
8223 tree parm, type, typelt;
8224 unsigned int parmno;
8226 /* In C2X, convert () to (void). */
8227 if (flag_isoc2x
8228 && !arg_types
8229 && !arg_info->parms
8230 && !arg_info->no_named_args_stdarg_p)
8231 arg_types = arg_info->types = void_list_node;
8233 /* If there is a parameter of incomplete type in a definition,
8234 this is an error. In a declaration this is valid, and a
8235 struct or union type may be completed later, before any calls
8236 or definition of the function. In the case where the tag was
8237 first declared within the parameter list, a warning has
8238 already been given. If a parameter has void type, then
8239 however the function cannot be defined or called, so
8240 warn. */
8242 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8243 parm;
8244 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8246 type = TREE_VALUE (typelt);
8247 if (type == error_mark_node)
8248 continue;
8250 if (!COMPLETE_TYPE_P (type))
8252 if (funcdef_flag)
8254 if (DECL_NAME (parm))
8255 error_at (input_location,
8256 "parameter %u (%q+D) has incomplete type",
8257 parmno, parm);
8258 else
8259 error_at (DECL_SOURCE_LOCATION (parm),
8260 "parameter %u has incomplete type",
8261 parmno);
8263 TREE_VALUE (typelt) = error_mark_node;
8264 TREE_TYPE (parm) = error_mark_node;
8265 arg_types = NULL_TREE;
8267 else if (VOID_TYPE_P (type))
8269 if (DECL_NAME (parm))
8270 warning_at (input_location, 0,
8271 "parameter %u (%q+D) has void type",
8272 parmno, parm);
8273 else
8274 warning_at (DECL_SOURCE_LOCATION (parm), 0,
8275 "parameter %u has void type",
8276 parmno);
8280 if (DECL_NAME (parm) && TREE_USED (parm))
8281 warn_if_shadowing (parm);
8283 return arg_types;
8287 /* Allocate and initialize a c_arg_info structure from the parser's
8288 obstack. */
8290 struct c_arg_info *
8291 build_arg_info (void)
8293 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8294 ret->parms = NULL_TREE;
8295 ret->tags = NULL;
8296 ret->types = NULL_TREE;
8297 ret->others = NULL_TREE;
8298 ret->pending_sizes = NULL;
8299 ret->had_vla_unspec = 0;
8300 ret->no_named_args_stdarg_p = 0;
8301 return ret;
8304 /* Take apart the current scope and return a c_arg_info structure with
8305 info on a parameter list just parsed.
8307 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8309 ELLIPSIS being true means the argument list ended in '...' so don't
8310 append a sentinel (void_list_node) to the end of the type-list.
8312 EXPR is NULL or an expression that needs to be evaluated for the
8313 side effects of array size expressions in the parameters. */
8315 struct c_arg_info *
8316 get_parm_info (bool ellipsis, tree expr)
8318 struct c_binding *b = current_scope->bindings;
8319 struct c_arg_info *arg_info = build_arg_info ();
8321 tree parms = NULL_TREE;
8322 vec<c_arg_tag, va_gc> *tags = NULL;
8323 tree types = NULL_TREE;
8324 tree others = NULL_TREE;
8326 bool gave_void_only_once_err = false;
8328 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8330 /* The bindings in this scope must not get put into a block.
8331 We will take care of deleting the binding nodes. */
8332 current_scope->bindings = 0;
8334 /* This function is only called if there was *something* on the
8335 parameter list. */
8336 gcc_assert (b);
8338 /* A parameter list consisting solely of 'void' indicates that the
8339 function takes no arguments. But if the 'void' is qualified
8340 (by 'const' or 'volatile'), or has a storage class specifier
8341 ('register'), then the behavior is undefined; issue an error.
8342 Typedefs for 'void' are OK (see DR#157). */
8343 if (b->prev == 0 /* one binding */
8344 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8345 && !DECL_NAME (b->decl) /* anonymous */
8346 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8348 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8349 || C_DECL_REGISTER (b->decl))
8350 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8352 /* There cannot be an ellipsis. */
8353 if (ellipsis)
8354 error_at (b->locus, "%<void%> must be the only parameter");
8356 arg_info->types = void_list_node;
8357 return arg_info;
8360 if (!ellipsis)
8361 types = void_list_node;
8363 /* Break up the bindings list into parms, tags, types, and others;
8364 apply sanity checks; purge the name-to-decl bindings. */
8365 while (b)
8367 tree decl = b->decl;
8368 tree type = TREE_TYPE (decl);
8369 c_arg_tag tag;
8370 const char *keyword;
8372 switch (TREE_CODE (decl))
8374 case PARM_DECL:
8375 if (b->id)
8377 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8378 I_SYMBOL_BINDING (b->id) = b->shadowed;
8381 /* Check for forward decls that never got their actual decl. */
8382 if (TREE_ASM_WRITTEN (decl))
8383 error_at (b->locus,
8384 "parameter %q+D has just a forward declaration", decl);
8385 /* Check for (..., void, ...) and issue an error. */
8386 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8388 if (!gave_void_only_once_err)
8390 error_at (b->locus, "%<void%> must be the only parameter");
8391 gave_void_only_once_err = true;
8394 else
8396 /* Valid parameter, add it to the list. */
8397 DECL_CHAIN (decl) = parms;
8398 parms = decl;
8400 /* Since there is a prototype, args are passed in their
8401 declared types. The back end may override this later. */
8402 DECL_ARG_TYPE (decl) = type;
8403 types = tree_cons (0, type, types);
8405 break;
8407 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8408 case UNION_TYPE: keyword = "union"; goto tag;
8409 case RECORD_TYPE: keyword = "struct"; goto tag;
8410 tag:
8411 /* Types may not have tag-names, in which case the type
8412 appears in the bindings list with b->id NULL. */
8413 if (b->id)
8415 gcc_assert (I_TAG_BINDING (b->id) == b);
8416 I_TAG_BINDING (b->id) = b->shadowed;
8419 /* Warn about any struct, union or enum tags defined in a
8420 parameter list. The scope of such types is limited to
8421 the parameter list, which is rarely if ever desirable
8422 (it's impossible to call such a function with type-
8423 correct arguments). An anonymous union parm type is
8424 meaningful as a GNU extension, so don't warn for that. */
8425 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8427 if (b->id)
8428 /* The %s will be one of 'struct', 'union', or 'enum'. */
8429 warning_at (b->locus, 0,
8430 "%<%s %E%> declared inside parameter list"
8431 " will not be visible outside of this definition or"
8432 " declaration", keyword, b->id);
8433 else
8434 /* The %s will be one of 'struct', 'union', or 'enum'. */
8435 warning_at (b->locus, 0,
8436 "anonymous %s declared inside parameter list"
8437 " will not be visible outside of this definition or"
8438 " declaration", keyword);
8441 tag.id = b->id;
8442 tag.type = decl;
8443 vec_safe_push (tags, tag);
8444 break;
8446 case FUNCTION_DECL:
8447 /* FUNCTION_DECLs appear when there is an implicit function
8448 declaration in the parameter list. */
8449 gcc_assert (b->nested || seen_error ());
8450 goto set_shadowed;
8452 case CONST_DECL:
8453 case TYPE_DECL:
8454 /* CONST_DECLs appear here when we have an embedded enum,
8455 and TYPE_DECLs appear here when we have an embedded struct
8456 or union. No warnings for this - we already warned about the
8457 type itself. */
8459 /* When we reinsert this decl in the function body, we need
8460 to reconstruct whether it was marked as nested. */
8461 gcc_assert (!b->nested);
8462 DECL_CHAIN (decl) = others;
8463 others = decl;
8464 /* fall through */
8466 case ERROR_MARK:
8467 set_shadowed:
8468 /* error_mark_node appears here when we have an undeclared
8469 variable. Just throw it away. */
8470 if (b->id)
8472 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8473 I_SYMBOL_BINDING (b->id) = b->shadowed;
8475 break;
8477 /* Other things that might be encountered. */
8478 case LABEL_DECL:
8479 case VAR_DECL:
8480 default:
8481 gcc_unreachable ();
8484 b = free_binding_and_advance (b);
8487 arg_info->parms = parms;
8488 arg_info->tags = tags;
8489 arg_info->types = types;
8490 arg_info->others = others;
8491 arg_info->pending_sizes = expr;
8492 arg_info->no_named_args_stdarg_p = ellipsis && !types;
8493 return arg_info;
8496 /* Get the struct, enum or union (CODE says which) with tag NAME.
8497 Define the tag as a forward-reference with location LOC if it is
8498 not defined. HAVE_STD_ATTRS says whether any standard attributes
8499 were present after the struct, union or enum keyword; ATTRS are the
8500 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8501 whether an enum type specifier (": specifier-qualifier-list") is
8502 present; if so, this is called before that specifier is parsed, so
8503 that the tag is in scope for that specifier. Return a c_typespec
8504 structure for the type specifier. */
8506 struct c_typespec
8507 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8508 bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8510 struct c_typespec ret;
8511 tree ref;
8512 location_t refloc;
8514 ret.expr = NULL_TREE;
8515 ret.expr_const_operands = true;
8516 ret.has_enum_type_specifier = has_enum_type_specifier;
8518 /* If a cross reference is requested, look up the type already
8519 defined for this tag and return it. If an enum type specifier is
8520 present, only a definition in the current scope is relevant. */
8522 ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8523 /* If this is the right type of tag, return what we found.
8524 (This reference will be shadowed by shadow_tag later if appropriate.)
8525 If this is the wrong type of tag, do not return it. If it was the
8526 wrong type in the same scope, we will have had an error
8527 message already; if in a different scope and declaring
8528 a name, pending_xref_error will give an error message; but if in a
8529 different scope and not declaring a name, this tag should
8530 shadow the previous declaration of a different type of tag, and
8531 this would not work properly if we return the reference found.
8532 (For example, with "struct foo" in an outer scope, "union foo;"
8533 must shadow that tag with a new one of union type.) */
8534 ret.kind = (ref
8535 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8536 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8537 if (ref && TREE_CODE (ref) == code)
8539 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8540 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8541 && loc != UNKNOWN_LOCATION
8542 && warn_cxx_compat)
8544 auto_diagnostic_group d;
8545 switch (code)
8547 case ENUMERAL_TYPE:
8548 if (warning_at (loc, OPT_Wc___compat,
8549 ("enum type defined in struct or union "
8550 "is not visible in C++")))
8551 inform (refloc, "enum type defined here");
8552 break;
8553 case RECORD_TYPE:
8554 if (warning_at (loc, OPT_Wc___compat,
8555 ("struct defined in struct or union "
8556 "is not visible in C++")))
8557 inform (refloc, "struct defined here");
8558 break;
8559 case UNION_TYPE:
8560 if (warning_at (loc, OPT_Wc___compat,
8561 ("union defined in struct or union "
8562 "is not visible in C++")))
8563 inform (refloc, "union defined here");
8564 break;
8565 default:
8566 gcc_unreachable();
8570 ret.spec = ref;
8571 return ret;
8574 /* If no such tag is yet defined, create a forward-reference node
8575 and record it as the "definition".
8576 When a real declaration of this type is found,
8577 the forward-reference will be altered into a real type. */
8579 ref = make_node (code);
8580 if (code == ENUMERAL_TYPE)
8582 /* Give the type a default layout like unsigned int
8583 to avoid crashing if it does not get defined. */
8584 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8585 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8586 TYPE_USER_ALIGN (ref) = 0;
8587 TYPE_UNSIGNED (ref) = 1;
8588 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8589 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8590 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8591 ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8594 pushtag (loc, name, ref);
8595 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8596 if (in_underspecified_init)
8597 error_at (loc, "%qT declared in underspecified object initializer",
8598 ref);
8600 ret.spec = ref;
8601 return ret;
8604 /* Get the struct, enum or union (CODE says which) with tag NAME.
8605 Define the tag as a forward-reference if it is not defined.
8606 Return a tree for the type. */
8608 tree
8609 xref_tag (enum tree_code code, tree name)
8611 return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8612 false).spec;
8615 /* Make sure that the tag NAME is defined *in the current scope*
8616 at least as a forward reference.
8617 LOC is the location of the struct's definition.
8618 CODE says which kind of tag NAME ought to be.
8620 This stores the current value of the file static STRUCT_PARSE_INFO
8621 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8622 new c_struct_parse_info structure. The old value of
8623 STRUCT_PARSE_INFO is restored in finish_struct. */
8625 tree
8626 start_struct (location_t loc, enum tree_code code, tree name,
8627 class c_struct_parse_info **enclosing_struct_parse_info)
8629 /* If there is already a tag defined at this scope
8630 (as a forward reference), just return it. */
8632 tree ref = NULL_TREE;
8633 location_t refloc = UNKNOWN_LOCATION;
8635 if (name != NULL_TREE)
8636 ref = lookup_tag (code, name, true, &refloc);
8637 if (ref && TREE_CODE (ref) == code)
8639 if (TYPE_STUB_DECL (ref))
8640 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8642 if (TYPE_SIZE (ref))
8644 auto_diagnostic_group d;
8645 if (code == UNION_TYPE)
8646 error_at (loc, "redefinition of %<union %E%>", name);
8647 else
8648 error_at (loc, "redefinition of %<struct %E%>", name);
8649 if (refloc != UNKNOWN_LOCATION)
8650 inform (refloc, "originally defined here");
8651 /* Don't create structures using a name already in use. */
8652 ref = NULL_TREE;
8654 else if (C_TYPE_BEING_DEFINED (ref))
8656 if (code == UNION_TYPE)
8657 error_at (loc, "nested redefinition of %<union %E%>", name);
8658 else
8659 error_at (loc, "nested redefinition of %<struct %E%>", name);
8660 /* Don't bother to report "originally defined here" for a
8661 nested redefinition; the original definition should be
8662 obvious. */
8663 /* Don't create structures that contain themselves. */
8664 ref = NULL_TREE;
8668 /* Otherwise create a forward-reference just so the tag is in scope. */
8670 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8672 ref = make_node (code);
8673 pushtag (loc, name, ref);
8676 C_TYPE_BEING_DEFINED (ref) = 1;
8677 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8678 TYPE_PACKED (v) = flag_pack_struct;
8680 *enclosing_struct_parse_info = struct_parse_info;
8681 struct_parse_info = new c_struct_parse_info ();
8683 /* FIXME: This will issue a warning for a use of a type defined
8684 within a statement expr used within sizeof, et. al. This is not
8685 terribly serious as C++ doesn't permit statement exprs within
8686 sizeof anyhow. */
8687 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8688 warning_at (loc, OPT_Wc___compat,
8689 "defining type in %qs expression is invalid in C++",
8690 (in_sizeof
8691 ? "sizeof"
8692 : (in_typeof ? "typeof" : "alignof")));
8694 if (in_underspecified_init)
8695 error_at (loc, "%qT defined in underspecified object initializer", ref);
8697 return ref;
8700 /* Process the specs, declarator and width (NULL if omitted)
8701 of a structure component, returning a FIELD_DECL node.
8702 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8703 DECL_ATTRS is as for grokdeclarator.
8705 LOC is the location of the structure component.
8707 This is done during the parsing of the struct declaration.
8708 The FIELD_DECL nodes are chained together and the lot of them
8709 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8711 tree
8712 grokfield (location_t loc,
8713 struct c_declarator *declarator, struct c_declspecs *declspecs,
8714 tree width, tree *decl_attrs)
8716 tree value;
8718 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8719 && width == NULL_TREE)
8721 /* This is an unnamed decl.
8723 If we have something of the form "union { list } ;" then this
8724 is the anonymous union extension. Similarly for struct.
8726 If this is something of the form "struct foo;", then
8727 If MS or Plan 9 extensions are enabled, this is handled as
8728 an anonymous struct.
8729 Otherwise this is a forward declaration of a structure tag.
8731 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8732 If foo names a structure or union without a tag, then this
8733 is an anonymous struct (this is permitted by C11).
8734 If MS or Plan 9 extensions are enabled and foo names a
8735 structure, then again this is an anonymous struct.
8736 Otherwise this is an error.
8738 Oh what a horrid tangled web we weave. I wonder if MS consciously
8739 took this from Plan 9 or if it was an accident of implementation
8740 that took root before someone noticed the bug... */
8742 tree type = declspecs->type;
8743 bool ok = false;
8745 if (RECORD_OR_UNION_TYPE_P (type)
8746 && (flag_ms_extensions
8747 || flag_plan9_extensions
8748 || !declspecs->typedef_p))
8750 if (flag_ms_extensions || flag_plan9_extensions)
8751 ok = true;
8752 else if (TYPE_NAME (type) == NULL)
8753 ok = true;
8754 else
8755 ok = false;
8757 if (!ok)
8759 pedwarn (loc, 0, "declaration does not declare anything");
8760 return NULL_TREE;
8762 if (flag_isoc99)
8763 pedwarn_c99 (loc, OPT_Wpedantic,
8764 "ISO C99 doesn%'t support unnamed structs/unions");
8765 else
8766 pedwarn_c99 (loc, OPT_Wpedantic,
8767 "ISO C90 doesn%'t support unnamed structs/unions");
8770 value = grokdeclarator (declarator, declspecs, FIELD, false,
8771 width ? &width : NULL, decl_attrs, NULL, NULL,
8772 DEPRECATED_NORMAL);
8774 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8775 DECL_INITIAL (value) = width;
8776 if (width)
8777 SET_DECL_C_BIT_FIELD (value);
8779 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8781 /* If we currently have a binding for this field, set the
8782 in_struct field in the binding, so that we warn about lookups
8783 which find it. */
8784 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8785 if (b != NULL)
8787 /* If the in_struct field is not yet set, push it on a list
8788 to be cleared when this struct is finished. */
8789 if (!b->in_struct)
8791 struct_parse_info->fields.safe_push (b);
8792 b->in_struct = 1;
8797 return value;
8800 /* Subroutine of detect_field_duplicates: return whether X and Y,
8801 which are both fields in the same struct, have duplicate field
8802 names. */
8804 static bool
8805 is_duplicate_field (tree x, tree y)
8807 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8808 return true;
8810 /* When using -fplan9-extensions, an anonymous field whose name is a
8811 typedef can duplicate a field name. */
8812 if (flag_plan9_extensions
8813 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8815 tree xt, xn, yt, yn;
8817 xt = TREE_TYPE (x);
8818 if (DECL_NAME (x) != NULL_TREE)
8819 xn = DECL_NAME (x);
8820 else if (RECORD_OR_UNION_TYPE_P (xt)
8821 && TYPE_NAME (xt) != NULL_TREE
8822 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8823 xn = DECL_NAME (TYPE_NAME (xt));
8824 else
8825 xn = NULL_TREE;
8827 yt = TREE_TYPE (y);
8828 if (DECL_NAME (y) != NULL_TREE)
8829 yn = DECL_NAME (y);
8830 else if (RECORD_OR_UNION_TYPE_P (yt)
8831 && TYPE_NAME (yt) != NULL_TREE
8832 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8833 yn = DECL_NAME (TYPE_NAME (yt));
8834 else
8835 yn = NULL_TREE;
8837 if (xn != NULL_TREE && xn == yn)
8838 return true;
8841 return false;
8844 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8845 to HTAB, giving errors for any duplicates. */
8847 static void
8848 detect_field_duplicates_hash (tree fieldlist,
8849 hash_table<nofree_ptr_hash <tree_node> > *htab)
8851 tree x, y;
8852 tree_node **slot;
8854 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8855 if ((y = DECL_NAME (x)) != NULL_TREE)
8857 slot = htab->find_slot (y, INSERT);
8858 if (*slot)
8860 error ("duplicate member %q+D", x);
8861 DECL_NAME (x) = NULL_TREE;
8863 *slot = y;
8865 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8867 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8869 /* When using -fplan9-extensions, an anonymous field whose
8870 name is a typedef can duplicate a field name. */
8871 if (flag_plan9_extensions
8872 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8873 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8875 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8876 slot = htab->find_slot (xn, INSERT);
8877 if (*slot)
8878 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8879 *slot = xn;
8884 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8885 the list such that this does not present a problem later. */
8887 static void
8888 detect_field_duplicates (tree fieldlist)
8890 tree x, y;
8891 int timeout = 10;
8893 /* If the struct is the list of instance variables of an Objective-C
8894 class, then we need to check all the instance variables of
8895 superclasses when checking for duplicates (since you can't have
8896 an instance variable in a subclass with the same name as an
8897 instance variable in a superclass). We pass on this job to the
8898 Objective-C compiler. objc_detect_field_duplicates() will return
8899 false if we are not checking the list of instance variables and
8900 the C frontend should proceed with the standard field duplicate
8901 checks. If we are checking the list of instance variables, the
8902 ObjC frontend will do the check, emit the errors if needed, and
8903 then return true. */
8904 if (c_dialect_objc ())
8905 if (objc_detect_field_duplicates (false))
8906 return;
8908 /* First, see if there are more than "a few" fields.
8909 This is trivially true if there are zero or one fields. */
8910 if (!fieldlist || !DECL_CHAIN (fieldlist))
8911 return;
8912 x = fieldlist;
8913 do {
8914 timeout--;
8915 if (DECL_NAME (x) == NULL_TREE
8916 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8917 timeout = 0;
8918 x = DECL_CHAIN (x);
8919 } while (timeout > 0 && x);
8921 /* If there were "few" fields and no anonymous structures or unions,
8922 avoid the overhead of allocating a hash table. Instead just do
8923 the nested traversal thing. */
8924 if (timeout > 0)
8926 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8927 /* When using -fplan9-extensions, we can have duplicates
8928 between typedef names and fields. */
8929 if (DECL_NAME (x)
8930 || (flag_plan9_extensions
8931 && DECL_NAME (x) == NULL_TREE
8932 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8933 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8934 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8936 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8937 if (is_duplicate_field (y, x))
8939 error ("duplicate member %q+D", x);
8940 DECL_NAME (x) = NULL_TREE;
8944 else
8946 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8947 detect_field_duplicates_hash (fieldlist, &htab);
8951 /* Finish up struct info used by -Wc++-compat. */
8953 static void
8954 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8955 location_t record_loc)
8957 unsigned int ix;
8958 tree x;
8959 struct c_binding *b;
8961 if (fieldlist == NULL_TREE)
8963 if (code == RECORD_TYPE)
8964 warning_at (record_loc, OPT_Wc___compat,
8965 "empty struct has size 0 in C, size 1 in C++");
8966 else
8967 warning_at (record_loc, OPT_Wc___compat,
8968 "empty union has size 0 in C, size 1 in C++");
8971 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8972 the current struct. We do this now at the end of the struct
8973 because the flag is used to issue visibility warnings, and we
8974 only want to issue those warnings if the type is referenced
8975 outside of the struct declaration. */
8976 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8977 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8979 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8980 typedefs used when declaring fields in this struct. If the name
8981 of any of the fields is also a typedef name then the struct would
8982 not parse in C++, because the C++ lookup rules say that the
8983 typedef name would be looked up in the context of the struct, and
8984 would thus be the field rather than the typedef. */
8985 if (!struct_parse_info->typedefs_seen.is_empty ()
8986 && fieldlist != NULL_TREE)
8988 /* Use a hash_set<tree> using the name of the typedef. We can use
8989 a hash_set<tree> because identifiers are interned. */
8990 hash_set<tree> tset;
8992 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8993 tset.add (DECL_NAME (x));
8995 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8997 if (DECL_NAME (x) != NULL_TREE
8998 && tset.contains (DECL_NAME (x)))
9000 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
9001 ("using %qD as both field and typedef name is "
9002 "invalid in C++"),
9004 /* FIXME: It would be nice to report the location where
9005 the typedef name is used. */
9010 /* For each field which has a binding and which was not defined in
9011 an enclosing struct, clear the in_struct field. */
9012 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9013 b->in_struct = 0;
9016 /* Function to help qsort sort FIELD_DECLs by name order. */
9018 static int
9019 field_decl_cmp (const void *x_p, const void *y_p)
9021 const tree *const x = (const tree *) x_p;
9022 const tree *const y = (const tree *) y_p;
9024 if (DECL_NAME (*x) == DECL_NAME (*y))
9025 /* A nontype is "greater" than a type. */
9026 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9027 if (DECL_NAME (*x) == NULL_TREE)
9028 return -1;
9029 if (DECL_NAME (*y) == NULL_TREE)
9030 return 1;
9031 if (DECL_NAME (*x) < DECL_NAME (*y))
9032 return -1;
9033 return 1;
9036 /* If this structure or union completes the type of any previous
9037 variable declaration, lay it out and output its rtl. */
9038 static void
9039 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9041 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9043 tree decl = TREE_VALUE (x);
9044 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9045 layout_array_type (TREE_TYPE (decl));
9046 if (TREE_CODE (decl) != TYPE_DECL)
9048 relayout_decl (decl);
9049 if (c_dialect_objc ())
9050 objc_check_decl (decl);
9051 rest_of_decl_compilation (decl, toplevel, 0);
9056 /* Determine whether the FIELD_DECL X is a flexible array member according to
9057 the following info:
9058 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9059 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9060 or "[1]";
9061 C. flag_strict_flex_arrays;
9062 D. the attribute strict_flex_array that is attached to the field
9063 if presenting.
9064 Return TRUE when it's a flexible array member, FALSE otherwise. */
9066 static bool
9067 is_flexible_array_member_p (bool is_last_field,
9068 tree x)
9070 /* If not the last field, return false. */
9071 if (!is_last_field)
9072 return false;
9074 /* If not an array field, return false. */
9075 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9076 return false;
9078 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9079 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9080 bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
9082 unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9084 switch (strict_flex_array_level)
9086 case 0:
9087 /* Default, all trailing arrays are flexible array members. */
9088 return true;
9089 case 1:
9090 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9091 if (is_one_element_array)
9092 return true;
9093 /* FALLTHROUGH. */
9094 case 2:
9095 /* Level 2: all "[0]", and "[]" are flexible array members. */
9096 if (is_zero_length_array)
9097 return true;
9098 /* FALLTHROUGH. */
9099 case 3:
9100 /* Level 3: Only "[]" are flexible array members. */
9101 if (is_flexible_array)
9102 return true;
9103 break;
9104 default:
9105 gcc_unreachable ();
9107 return false;
9111 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9112 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9113 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9114 ATTRIBUTES are attributes to be applied to the structure.
9116 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9117 the struct was started. */
9119 tree
9120 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9121 class c_struct_parse_info *enclosing_struct_parse_info)
9123 tree x;
9124 bool toplevel = file_scope == current_scope;
9126 /* If this type was previously laid out as a forward reference,
9127 make sure we lay it out again. */
9129 TYPE_SIZE (t) = NULL_TREE;
9131 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9133 if (pedantic)
9135 for (x = fieldlist; x; x = DECL_CHAIN (x))
9137 if (DECL_NAME (x) != NULL_TREE)
9138 break;
9139 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9140 break;
9143 if (x == NULL_TREE)
9145 if (TREE_CODE (t) == UNION_TYPE)
9147 if (fieldlist)
9148 pedwarn (loc, OPT_Wpedantic, "union has no named members");
9149 else
9150 pedwarn (loc, OPT_Wpedantic, "union has no members");
9152 else
9154 if (fieldlist)
9155 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9156 else
9157 pedwarn (loc, OPT_Wpedantic, "struct has no members");
9162 /* Install struct as DECL_CONTEXT of each field decl.
9163 Also process specified field sizes, found in the DECL_INITIAL,
9164 storing 0 there after the type has been changed to precision equal
9165 to its width, rather than the precision of the specified standard
9166 type. (Correct layout requires the original type to have been preserved
9167 until now.) */
9169 bool saw_named_field = false;
9170 for (x = fieldlist; x; x = DECL_CHAIN (x))
9172 /* Whether this field is the last field of the structure or union.
9173 for UNION, any field is the last field of it. */
9174 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9175 || (TREE_CODE (t) == UNION_TYPE);
9177 if (TREE_TYPE (x) == error_mark_node)
9178 continue;
9180 DECL_CONTEXT (x) = t;
9182 tree t1 = strip_array_types (TREE_TYPE (x));
9183 /* If any field is const, the structure type is pseudo-const. */
9184 if (TREE_READONLY (x))
9185 C_TYPE_FIELDS_READONLY (t) = 1;
9186 else
9188 /* A field that is pseudo-const makes the structure likewise. */
9189 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9190 C_TYPE_FIELDS_READONLY (t) = 1;
9193 /* Any field that is volatile means variables of this type must be
9194 treated in some ways as volatile. */
9195 if (TREE_THIS_VOLATILE (x))
9197 C_TYPE_FIELDS_VOLATILE (t) = 1;
9198 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9201 /* Any field that is volatile, restrict-qualified or atomic
9202 means the type cannot be used for a constexpr object. */
9203 if (TYPE_QUALS (t1)
9204 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9205 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9206 else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9207 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9209 /* Any field of nominal variable size implies structure is too. */
9210 if (C_DECL_VARIABLE_SIZE (x))
9211 C_TYPE_VARIABLE_SIZE (t) = 1;
9213 /* If any field is variably modified, record this fact. */
9214 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9215 C_TYPE_VARIABLY_MODIFIED (t) = 1;
9217 if (DECL_C_BIT_FIELD (x))
9219 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9220 DECL_SIZE (x) = bitsize_int (width);
9221 DECL_BIT_FIELD (x) = 1;
9224 if (TYPE_PACKED (t)
9225 && (DECL_BIT_FIELD (x)
9226 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9227 DECL_PACKED (x) = 1;
9229 /* Detect flexible array member in an invalid context. */
9230 if (flexible_array_member_type_p (TREE_TYPE (x)))
9232 if (TREE_CODE (t) == UNION_TYPE)
9234 error_at (DECL_SOURCE_LOCATION (x),
9235 "flexible array member in union");
9236 TREE_TYPE (x) = error_mark_node;
9238 else if (!is_last_field)
9240 error_at (DECL_SOURCE_LOCATION (x),
9241 "flexible array member not at end of struct");
9242 TREE_TYPE (x) = error_mark_node;
9244 else if (!saw_named_field)
9246 error_at (DECL_SOURCE_LOCATION (x),
9247 "flexible array member in a struct with no named "
9248 "members");
9249 TREE_TYPE (x) = error_mark_node;
9253 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9254 && flexible_array_type_p (TREE_TYPE (x)))
9255 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9256 "invalid use of structure with flexible array member");
9258 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9259 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9261 if (DECL_NAME (x)
9262 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9263 saw_named_field = true;
9266 detect_field_duplicates (fieldlist);
9268 /* Now we have the nearly final fieldlist. Record it,
9269 then lay out the structure or union (including the fields). */
9271 TYPE_FIELDS (t) = fieldlist;
9273 maybe_apply_pragma_scalar_storage_order (t);
9275 layout_type (t);
9277 if (TYPE_SIZE_UNIT (t)
9278 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9279 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9280 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9281 error ("type %qT is too large", t);
9283 /* Give bit-fields their proper types and rewrite the type of array fields
9284 with scalar component if the enclosing type has reverse storage order. */
9285 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9287 if (TREE_CODE (field) == FIELD_DECL
9288 && DECL_INITIAL (field)
9289 && TREE_TYPE (field) != error_mark_node)
9291 unsigned HOST_WIDE_INT width
9292 = tree_to_uhwi (DECL_INITIAL (field));
9293 tree type = TREE_TYPE (field);
9294 if (width != TYPE_PRECISION (type))
9296 TREE_TYPE (field)
9297 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
9298 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9300 DECL_INITIAL (field) = NULL_TREE;
9302 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9303 && TREE_CODE (field) == FIELD_DECL
9304 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9306 tree ftype = TREE_TYPE (field);
9307 tree ctype = strip_array_types (ftype);
9308 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9310 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9311 tree *typep = &fmain_type;
9312 do {
9313 *typep = build_distinct_type_copy (*typep);
9314 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9315 typep = &TREE_TYPE (*typep);
9316 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9317 TREE_TYPE (field)
9318 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9322 /* Warn on problematic type punning for storage order purposes. */
9323 if (TREE_CODE (t) == UNION_TYPE
9324 && TREE_CODE (field) == FIELD_DECL
9325 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9327 tree ftype = TREE_TYPE (field);
9328 if (TREE_CODE (ftype) == ARRAY_TYPE)
9329 ftype = strip_array_types (ftype);
9330 if (RECORD_OR_UNION_TYPE_P (ftype)
9331 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9332 != TYPE_REVERSE_STORAGE_ORDER (t))
9333 warning_at (DECL_SOURCE_LOCATION (field),
9334 OPT_Wscalar_storage_order,
9335 "type punning toggles scalar storage order");
9339 /* Now we have the truly final field list.
9340 Store it in this type and in the variants. */
9342 TYPE_FIELDS (t) = fieldlist;
9344 /* If there are lots of fields, sort so we can look through them fast.
9345 We arbitrarily consider 16 or more elts to be "a lot". */
9348 int len = 0;
9350 for (x = fieldlist; x; x = DECL_CHAIN (x))
9352 if (len > 15 || DECL_NAME (x) == NULL)
9353 break;
9354 len += 1;
9357 if (len > 15)
9359 tree *field_array;
9360 struct lang_type *space;
9361 struct sorted_fields_type *space2;
9363 len += list_length (x);
9365 /* Use the same allocation policy here that make_node uses, to
9366 ensure that this lives as long as the rest of the struct decl.
9367 All decls in an inline function need to be saved. */
9369 space = ggc_cleared_alloc<struct lang_type> ();
9370 space2 = (sorted_fields_type *) ggc_internal_alloc
9371 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
9373 len = 0;
9374 space->s = space2;
9375 field_array = &space2->elts[0];
9376 for (x = fieldlist; x; x = DECL_CHAIN (x))
9378 field_array[len++] = x;
9380 /* If there is anonymous struct or union, break out of the loop. */
9381 if (DECL_NAME (x) == NULL)
9382 break;
9384 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9385 if (x == NULL)
9387 TYPE_LANG_SPECIFIC (t) = space;
9388 TYPE_LANG_SPECIFIC (t)->s->len = len;
9389 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9390 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9395 /* If this was supposed to be a transparent union, but we can't
9396 make it one, warn and turn off the flag. */
9397 if (TREE_CODE (t) == UNION_TYPE
9398 && TYPE_TRANSPARENT_AGGR (t)
9399 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9401 TYPE_TRANSPARENT_AGGR (t) = 0;
9402 warning_at (loc, 0, "union cannot be made transparent");
9405 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9406 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9408 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9409 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9410 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9411 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9412 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9413 C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9414 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9415 C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9416 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9419 /* Update type location to the one of the definition, instead of e.g.
9420 a forward declaration. */
9421 if (TYPE_STUB_DECL (t))
9422 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9424 /* Finish debugging output for this type. */
9425 rest_of_type_compilation (t, toplevel);
9427 finish_incomplete_vars (incomplete_vars, toplevel);
9429 /* If we're inside a function proper, i.e. not file-scope and not still
9430 parsing parameters, then arrange for the size of a variable sized type
9431 to be bound now. */
9432 if (building_stmt_list_p () && c_type_variably_modified_p(t))
9433 add_stmt (build_stmt (loc,
9434 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
9436 if (warn_cxx_compat)
9437 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9439 delete struct_parse_info;
9441 struct_parse_info = enclosing_struct_parse_info;
9443 /* If this struct is defined inside a struct, add it to
9444 struct_types. */
9445 if (warn_cxx_compat
9446 && struct_parse_info != NULL
9447 && !in_sizeof && !in_typeof && !in_alignof)
9448 struct_parse_info->struct_types.safe_push (t);
9450 return t;
9453 static struct {
9454 gt_pointer_operator new_value;
9455 void *cookie;
9456 } resort_data;
9458 /* This routine compares two fields like field_decl_cmp but using the
9459 pointer operator in resort_data. */
9461 static int
9462 resort_field_decl_cmp (const void *x_p, const void *y_p)
9464 const tree *const x = (const tree *) x_p;
9465 const tree *const y = (const tree *) y_p;
9467 if (DECL_NAME (*x) == DECL_NAME (*y))
9468 /* A nontype is "greater" than a type. */
9469 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9470 if (DECL_NAME (*x) == NULL_TREE)
9471 return -1;
9472 if (DECL_NAME (*y) == NULL_TREE)
9473 return 1;
9475 tree d1 = DECL_NAME (*x);
9476 tree d2 = DECL_NAME (*y);
9477 resort_data.new_value (&d1, &d1, resort_data.cookie);
9478 resort_data.new_value (&d2, &d2, resort_data.cookie);
9479 if (d1 < d2)
9480 return -1;
9482 return 1;
9485 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9487 void
9488 resort_sorted_fields (void *obj,
9489 void * ARG_UNUSED (orig_obj),
9490 gt_pointer_operator new_value,
9491 void *cookie)
9493 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9494 resort_data.new_value = new_value;
9495 resort_data.cookie = cookie;
9496 qsort (&sf->elts[0], sf->len, sizeof (tree),
9497 resort_field_decl_cmp);
9500 /* Lay out the type T, and its element type, and so on. */
9502 static void
9503 layout_array_type (tree t)
9505 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9506 layout_array_type (TREE_TYPE (t));
9507 layout_type (t);
9510 /* Begin compiling the definition of an enumeration type.
9511 NAME is its name (or null if anonymous).
9512 LOC is the enum's location.
9513 FIXED_UNDERLYING_TYPE is the (C2x) underlying type specified in the
9514 definition.
9515 Returns the type object, as yet incomplete.
9516 Also records info about it so that build_enumerator
9517 may be used to declare the individual values as they are read. */
9519 tree
9520 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9521 tree fixed_underlying_type)
9523 tree enumtype = NULL_TREE;
9524 location_t enumloc = UNKNOWN_LOCATION;
9526 /* If this is the real definition for a previous forward reference,
9527 fill in the contents in the same object that used to be the
9528 forward reference. */
9530 if (name != NULL_TREE)
9531 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9533 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9535 enumtype = make_node (ENUMERAL_TYPE);
9536 pushtag (loc, name, enumtype);
9537 if (fixed_underlying_type != NULL_TREE)
9539 /* For an enum definition with a fixed underlying type, the
9540 type is complete during the definition and the
9541 enumeration constants have that type. If there was a
9542 tag, the type was completed in c_parser_enum_specifier.
9543 If not, it must be completed here. */
9544 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9545 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9546 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9547 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9548 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9549 TYPE_SIZE (enumtype) = NULL_TREE;
9550 TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9551 ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9552 layout_type (enumtype);
9555 /* Update type location to the one of the definition, instead of e.g.
9556 a forward declaration. */
9557 else if (TYPE_STUB_DECL (enumtype))
9559 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9560 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9563 if (C_TYPE_BEING_DEFINED (enumtype))
9564 error_at (loc, "nested redefinition of %<enum %E%>", name);
9566 C_TYPE_BEING_DEFINED (enumtype) = 1;
9568 if (TYPE_VALUES (enumtype) != NULL_TREE)
9570 /* This enum is a named one that has been declared already. */
9571 auto_diagnostic_group d;
9572 error_at (loc, "redeclaration of %<enum %E%>", name);
9573 if (enumloc != UNKNOWN_LOCATION)
9574 inform (enumloc, "originally defined here");
9576 /* Completely replace its old definition.
9577 The old enumerators remain defined, however. */
9578 TYPE_VALUES (enumtype) = NULL_TREE;
9581 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9582 && fixed_underlying_type == NULL_TREE)
9583 error_at (loc, "%<enum%> declared with but defined without "
9584 "fixed underlying type");
9586 the_enum->enum_next_value = integer_zero_node;
9587 the_enum->enum_type = enumtype;
9588 the_enum->enum_overflow = 0;
9590 if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9591 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9592 TYPE_PACKED (v) = 1;
9594 /* FIXME: This will issue a warning for a use of a type defined
9595 within sizeof in a statement expr. This is not terribly serious
9596 as C++ doesn't permit statement exprs within sizeof anyhow. */
9597 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9598 warning_at (loc, OPT_Wc___compat,
9599 "defining type in %qs expression is invalid in C++",
9600 (in_sizeof
9601 ? "sizeof"
9602 : (in_typeof ? "typeof" : "alignof")));
9604 if (in_underspecified_init)
9605 error_at (loc, "%qT defined in underspecified object initializer",
9606 enumtype);
9608 return enumtype;
9611 /* After processing and defining all the values of an enumeration type,
9612 install their decls in the enumeration type and finish it off.
9613 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9614 and ATTRIBUTES are the specified attributes.
9615 Returns ENUMTYPE. */
9617 tree
9618 finish_enum (tree enumtype, tree values, tree attributes)
9620 tree pair, tem;
9621 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9622 int precision;
9623 signop sign;
9624 bool toplevel = (file_scope == current_scope);
9625 struct lang_type *lt;
9627 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9629 /* Calculate the maximum value of any enumerator in this type. */
9631 if (values == error_mark_node)
9632 minnode = maxnode = integer_zero_node;
9633 else
9635 minnode = maxnode = TREE_VALUE (values);
9636 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9638 tree value = TREE_VALUE (pair);
9639 if (tree_int_cst_lt (maxnode, value))
9640 maxnode = value;
9641 if (tree_int_cst_lt (value, minnode))
9642 minnode = value;
9646 /* Construct the final type of this enumeration. It is the same
9647 as one of the integral types - the narrowest one that fits, except
9648 that normally we only go as narrow as int - and signed iff any of
9649 the values are negative. */
9650 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9651 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9652 tree_int_cst_min_precision (maxnode, sign));
9654 bool wider_than_int =
9655 (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
9656 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
9659 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9661 /* If the precision of the type was specified with an attribute and it
9662 was too small, give an error. Otherwise, use it. */
9663 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9665 if (precision > TYPE_PRECISION (enumtype))
9667 TYPE_PRECISION (enumtype) = 0;
9668 error ("specified mode too small for enumerated values");
9670 else
9671 precision = TYPE_PRECISION (enumtype);
9673 else
9674 TYPE_PRECISION (enumtype) = 0;
9676 if (TYPE_PACKED (enumtype)
9677 || precision > TYPE_PRECISION (integer_type_node)
9678 || TYPE_PRECISION (enumtype))
9680 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9681 if (tem == NULL)
9683 /* This should only occur when both signed and unsigned
9684 values of maximum precision occur among the
9685 enumerators. */
9686 pedwarn (input_location, 0,
9687 "enumeration values exceed range of largest integer");
9688 tem = widest_integer_literal_type_node;
9690 else if (precision > TYPE_PRECISION (intmax_type_node)
9691 && !tree_int_cst_lt (minnode,
9692 TYPE_MIN_VALUE (intmax_type_node))
9693 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
9694 maxnode))
9695 pedwarn (input_location, OPT_Wpedantic,
9696 "enumeration values exceed range of %qs",
9697 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
9699 else
9700 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9702 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9703 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9704 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9705 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9706 TYPE_SIZE (enumtype) = NULL_TREE;
9707 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9708 ENUM_UNDERLYING_TYPE (enumtype) =
9709 c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
9711 layout_type (enumtype);
9714 if (values != error_mark_node)
9716 /* Change the type of the enumerators to be the enum type. We
9717 need to do this irrespective of the size of the enum, for
9718 proper type checking. Replace the DECL_INITIALs of the
9719 enumerators, and the value slots of the list, with copies
9720 that have the enum type; they cannot be modified in place
9721 because they may be shared (e.g. integer_zero_node) Finally,
9722 change the purpose slots to point to the names of the decls. */
9723 for (pair = values; pair; pair = TREE_CHAIN (pair))
9725 tree enu = TREE_PURPOSE (pair);
9726 tree ini = DECL_INITIAL (enu);
9728 TREE_TYPE (enu) = enumtype;
9730 /* Before C2X, the ISO C Standard mandates enumerators to
9731 have type int, even though the underlying type of an enum
9732 type is unspecified. However, C2X allows enumerators of
9733 any integer type, and if an enumeration has any
9734 enumerators wider than int, all enumerators have the
9735 enumerated type after it is parsed. Any enumerators that
9736 fit in int are given type int in build_enumerator (which
9737 is the correct type while the enumeration is being
9738 parsed), so no conversions are needed here if all
9739 enumerators fit in int. If the enum has a fixed
9740 underlying type, the correct type was also given in
9741 build_enumerator. */
9742 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
9743 ini = convert (enumtype, ini);
9745 DECL_INITIAL (enu) = ini;
9746 TREE_PURPOSE (pair) = DECL_NAME (enu);
9747 /* To match the C++ FE, store the CONST_DECL rather than just its
9748 value. */
9749 TREE_VALUE (pair) = enu;
9752 TYPE_VALUES (enumtype) = values;
9755 /* Record the min/max values so that we can warn about bit-field
9756 enumerations that are too small for the values. */
9757 lt = ggc_cleared_alloc<struct lang_type> ();
9758 lt->enum_min = minnode;
9759 lt->enum_max = maxnode;
9760 TYPE_LANG_SPECIFIC (enumtype) = lt;
9762 /* Fix up all variant types of this enum type. */
9763 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9764 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9766 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9767 if (tem == enumtype)
9768 continue;
9769 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9770 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9771 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9772 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9773 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9774 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9775 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9776 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9777 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9778 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9779 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9780 ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
9783 /* Finish debugging output for this type. */
9784 rest_of_type_compilation (enumtype, toplevel);
9786 finish_incomplete_vars (incomplete_vars, toplevel);
9788 /* If this enum is defined inside a struct, add it to
9789 struct_types. */
9790 if (warn_cxx_compat
9791 && struct_parse_info != NULL
9792 && !in_sizeof && !in_typeof && !in_alignof)
9793 struct_parse_info->struct_types.safe_push (enumtype);
9795 C_TYPE_BEING_DEFINED (enumtype) = 0;
9797 return enumtype;
9800 /* Build and install a CONST_DECL for one value of the
9801 current enumeration type (one that was begun with start_enum).
9802 DECL_LOC is the location of the enumerator.
9803 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9804 Return a tree-list containing the CONST_DECL and its value.
9805 Assignment of sequential values by default is handled here. */
9807 tree
9808 build_enumerator (location_t decl_loc, location_t loc,
9809 struct c_enum_contents *the_enum, tree name, tree value)
9811 tree decl;
9813 /* Validate and default VALUE. */
9815 if (value != NULL_TREE)
9817 /* Don't issue more errors for error_mark_node (i.e. an
9818 undeclared identifier) - just ignore the value expression. */
9819 if (value == error_mark_node)
9820 value = NULL_TREE;
9821 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9823 error_at (loc, "enumerator value for %qE is not an integer constant",
9824 name);
9825 value = NULL_TREE;
9827 else
9829 if (TREE_CODE (value) != INTEGER_CST)
9831 value = c_fully_fold (value, false, NULL);
9832 if (TREE_CODE (value) == INTEGER_CST)
9833 pedwarn (loc, OPT_Wpedantic,
9834 "enumerator value for %qE is not an integer "
9835 "constant expression", name);
9837 if (TREE_CODE (value) != INTEGER_CST)
9839 error ("enumerator value for %qE is not an integer constant",
9840 name);
9841 value = NULL_TREE;
9843 else
9845 value = default_conversion (value);
9846 constant_expression_warning (value);
9851 /* Default based on previous value. */
9852 /* It should no longer be possible to have NON_LVALUE_EXPR
9853 in the default. */
9854 if (value == NULL_TREE)
9856 value = the_enum->enum_next_value;
9857 if (the_enum->enum_overflow)
9858 error_at (loc, "overflow in enumeration values");
9860 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
9862 /* Enumeration constants must fit in the fixed underlying type. */
9863 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
9864 error_at (loc,
9865 "enumerator value outside the range of underlying type");
9866 /* Enumeration constants for an enum with fixed underlying type
9867 have the enum type, both inside and outside the
9868 definition. */
9869 value = convert (the_enum->enum_type, value);
9871 else
9873 /* Even though the underlying type of an enum is unspecified, the
9874 type of enumeration constants is explicitly defined as int
9875 (6.4.4.3/2 in the C99 Standard). C2X allows any integer type, and
9876 GCC allows such types for older standards as an extension. */
9877 bool warned_range = false;
9878 if (!int_fits_type_p (value,
9879 (TYPE_UNSIGNED (TREE_TYPE (value))
9880 ? uintmax_type_node
9881 : intmax_type_node)))
9882 /* GCC does not consider its types larger than intmax_t to be
9883 extended integer types (although C2X would permit such types to
9884 be considered extended integer types if all the features
9885 required by <stdint.h> and <inttypes.h> macros, such as support
9886 for integer constants and I/O, were present), so diagnose if
9887 such a wider type is used. (If the wider type arose from a
9888 constant of such a type, that will also have been diagnosed,
9889 but this is the only diagnostic in the case where it arises
9890 from choosing a wider type automatically when adding 1
9891 overflows.) */
9892 warned_range = pedwarn (loc, OPT_Wpedantic,
9893 "enumerator value outside the range of %qs",
9894 (TYPE_UNSIGNED (TREE_TYPE (value))
9895 ? "uintmax_t"
9896 : "intmax_t"));
9897 if (!warned_range && !int_fits_type_p (value, integer_type_node))
9898 pedwarn_c11 (loc, OPT_Wpedantic,
9899 "ISO C restricts enumerator values to range of %<int%> "
9900 "before C2X");
9902 /* The ISO C Standard mandates enumerators to have type int before
9903 C2X, even though the underlying type of an enum type is
9904 unspecified. C2X allows enumerators of any integer type. During
9905 the parsing of the enumeration, C2X specifies that constants
9906 representable in int have type int, constants not representable
9907 in int have the type of the given expression if any, and
9908 constants not representable in int and derived by adding 1 to the
9909 previous constant have the type of that constant unless the
9910 addition would overflow or wraparound, in which case a wider type
9911 of the same signedness is chosen automatically; after the
9912 enumeration is parsed, all the constants have the type of the
9913 enumeration if any do not fit in int. */
9914 if (int_fits_type_p (value, integer_type_node))
9915 value = convert (integer_type_node, value);
9918 /* Set basis for default for next value. */
9919 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
9921 tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
9922 if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
9923 /* A value of 2 following a value of 1 overflows bool, but we
9924 cannot carry out addition directly on bool without
9925 promotion, and converting the result of arithmetic in a
9926 wider type back to bool would not produce the right result
9927 for this overflow check. */
9928 the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
9929 else
9930 the_enum->enum_next_value
9931 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9932 PLUS_EXPR, convert (underlying_type, value),
9933 convert (underlying_type, integer_one_node),
9934 false);
9936 else
9937 the_enum->enum_next_value
9938 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9939 PLUS_EXPR, value, integer_one_node, false);
9940 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9941 if (the_enum->enum_overflow
9942 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
9944 /* Choose a wider type with the same signedness if
9945 available. */
9946 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
9947 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
9948 tree new_type = (unsignedp
9949 ? long_unsigned_type_node
9950 : long_integer_type_node);
9951 if (prec > TYPE_PRECISION (new_type))
9952 new_type = (unsignedp
9953 ? long_long_unsigned_type_node
9954 : long_long_integer_type_node);
9955 if (prec > TYPE_PRECISION (new_type))
9956 new_type = (unsignedp
9957 ? widest_unsigned_literal_type_node
9958 : widest_integer_literal_type_node);
9959 if (prec <= TYPE_PRECISION (new_type))
9961 the_enum->enum_overflow = false;
9962 the_enum->enum_next_value
9963 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9964 PLUS_EXPR, convert (new_type, value),
9965 integer_one_node, false);
9966 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
9970 /* Now create a declaration for the enum value name. */
9972 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
9973 DECL_INITIAL (decl) = value;
9974 pushdecl (decl);
9976 return tree_cons (decl, value, NULL_TREE);
9979 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9981 tree
9982 c_simulate_enum_decl (location_t loc, const char *name,
9983 vec<string_int_pair> *values_ptr)
9985 location_t saved_loc = input_location;
9986 input_location = loc;
9988 struct c_enum_contents the_enum;
9989 tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
9990 NULL_TREE);
9992 tree value_chain = NULL_TREE;
9993 string_int_pair *value;
9994 vec<string_int_pair> values = *values_ptr;
9995 unsigned int i;
9996 FOR_EACH_VEC_ELT (values, i, value)
9998 tree decl = build_enumerator (loc, loc, &the_enum,
9999 get_identifier (value->first),
10000 build_int_cst (integer_type_node,
10001 value->second));
10002 TREE_CHAIN (decl) = value_chain;
10003 value_chain = decl;
10006 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10008 input_location = saved_loc;
10009 return enumtype;
10012 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10014 tree
10015 c_simulate_record_decl (location_t loc, const char *name,
10016 array_slice<const tree> fields)
10018 location_t saved_loc = input_location;
10019 input_location = loc;
10021 class c_struct_parse_info *struct_info;
10022 tree ident = get_identifier (name);
10023 tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10025 for (unsigned int i = 0; i < fields.size (); ++i)
10027 DECL_FIELD_CONTEXT (fields[i]) = type;
10028 if (i > 0)
10029 DECL_CHAIN (fields[i - 1]) = fields[i];
10032 finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10034 tree decl = build_decl (loc, TYPE_DECL, ident, type);
10035 set_underlying_type (decl);
10036 lang_hooks.decls.pushdecl (decl);
10038 input_location = saved_loc;
10039 return type;
10042 /* Create the FUNCTION_DECL for a function definition.
10043 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10044 the declaration; they describe the function's name and the type it returns,
10045 but twisted together in a fashion that parallels the syntax of C.
10047 This function creates a binding context for the function body
10048 as well as setting up the FUNCTION_DECL in current_function_decl.
10050 Returns true on success. If the DECLARATOR is not suitable for a function
10051 (it defines a datum instead), we return false to report a parse error. */
10053 bool
10054 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10055 tree attributes)
10057 tree decl1, old_decl;
10058 tree restype, resdecl;
10059 location_t loc;
10060 location_t result_loc;
10062 current_function_returns_value = 0; /* Assume, until we see it does. */
10063 current_function_returns_null = 0;
10064 current_function_returns_abnormally = 0;
10065 warn_about_return_type = 0;
10066 c_switch_stack = NULL;
10068 /* Indicate no valid break/continue context. */
10069 in_statement = 0;
10071 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10072 &attributes, NULL, NULL, DEPRECATED_NORMAL);
10073 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10075 /* If the declarator is not suitable for a function definition,
10076 cause a syntax error. */
10077 if (decl1 == NULL_TREE
10078 || TREE_CODE (decl1) != FUNCTION_DECL)
10079 return false;
10081 loc = DECL_SOURCE_LOCATION (decl1);
10083 /* A nested function is not global. */
10084 if (current_function_decl != NULL_TREE)
10085 TREE_PUBLIC (decl1) = 0;
10087 c_decl_attributes (&decl1, attributes, 0);
10089 if (DECL_DECLARED_INLINE_P (decl1)
10090 && DECL_UNINLINABLE (decl1)
10091 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10092 warning_at (loc, OPT_Wattributes,
10093 "inline function %qD given attribute %qs",
10094 decl1, "noinline");
10096 /* Handle gnu_inline attribute. */
10097 if (declspecs->inline_p
10098 && !flag_gnu89_inline
10099 && TREE_CODE (decl1) == FUNCTION_DECL
10100 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10101 || current_function_decl))
10103 if (declspecs->storage_class != csc_static)
10104 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10107 announce_function (decl1);
10109 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10111 error_at (loc, "return type is an incomplete type");
10112 /* Make it return void instead. */
10113 TREE_TYPE (decl1)
10114 = build_function_type (void_type_node,
10115 TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10116 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10119 if (warn_about_return_type)
10120 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
10121 : (warn_return_type > 0 ? OPT_Wreturn_type
10122 : OPT_Wimplicit_int),
10123 "return type defaults to %<int%>");
10125 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10126 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10127 DECL_INITIAL (decl1) = error_mark_node;
10129 /* If this definition isn't a prototype and we had a prototype declaration
10130 before, copy the arg type info from that prototype. */
10131 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10132 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10133 old_decl = NULL_TREE;
10135 current_function_prototype_locus = UNKNOWN_LOCATION;
10136 current_function_prototype_built_in = false;
10137 current_function_prototype_arg_types = NULL_TREE;
10138 tree newtype = TREE_TYPE (decl1);
10139 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10140 if (!prototype_p (newtype))
10142 tree oldrt = TREE_TYPE (oldtype);
10143 tree newrt = TREE_TYPE (newtype);
10144 if (old_decl != NULL_TREE
10145 && TREE_CODE (oldtype) == FUNCTION_TYPE
10146 && comptypes (oldrt, newrt))
10148 if (stdarg_p (oldtype))
10150 auto_diagnostic_group d;
10151 warning_at (loc, 0, "%q+D defined as variadic function "
10152 "without prototype", decl1);
10153 locate_old_decl (old_decl);
10155 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10156 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10157 current_function_prototype_built_in
10158 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10159 current_function_prototype_arg_types
10160 = TYPE_ARG_TYPES (newtype);
10162 if (TREE_PUBLIC (decl1))
10164 /* If there is an external prototype declaration of this
10165 function, record its location but do not copy information
10166 to this decl. This may be an invisible declaration
10167 (built-in or in a scope which has finished) or simply
10168 have more refined argument types than any declaration
10169 found above. */
10170 struct c_binding *b;
10171 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10172 if (B_IN_SCOPE (b, external_scope))
10173 break;
10174 if (b)
10176 tree ext_decl, ext_type;
10177 ext_decl = b->decl;
10178 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10179 if (TREE_CODE (ext_type) == FUNCTION_TYPE
10180 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10181 TREE_TYPE (ext_type)))
10183 current_function_prototype_locus
10184 = DECL_SOURCE_LOCATION (ext_decl);
10185 current_function_prototype_built_in
10186 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10187 current_function_prototype_arg_types
10188 = TYPE_ARG_TYPES (ext_type);
10194 /* Optionally warn of old-fashioned def with no previous prototype. */
10195 if (warn_strict_prototypes
10196 && old_decl != error_mark_node
10197 && !prototype_p (TREE_TYPE (decl1))
10198 && C_DECL_ISNT_PROTOTYPE (old_decl))
10199 warning_at (loc, OPT_Wstrict_prototypes,
10200 "function declaration isn%'t a prototype");
10201 /* Optionally warn of any global def with no previous prototype. */
10202 else if (warn_missing_prototypes
10203 && old_decl != error_mark_node
10204 && TREE_PUBLIC (decl1)
10205 && !MAIN_NAME_P (DECL_NAME (decl1))
10206 && C_DECL_ISNT_PROTOTYPE (old_decl)
10207 && !DECL_DECLARED_INLINE_P (decl1))
10208 warning_at (loc, OPT_Wmissing_prototypes,
10209 "no previous prototype for %qD", decl1);
10210 /* Optionally warn of any def with no previous prototype
10211 if the function has already been used. */
10212 else if (warn_missing_prototypes
10213 && old_decl != NULL_TREE
10214 && old_decl != error_mark_node
10215 && TREE_USED (old_decl)
10216 && !prototype_p (TREE_TYPE (old_decl)))
10217 warning_at (loc, OPT_Wmissing_prototypes,
10218 "%qD was used with no prototype before its definition", decl1);
10219 /* Optionally warn of any global def with no previous declaration. */
10220 else if (warn_missing_declarations
10221 && TREE_PUBLIC (decl1)
10222 && old_decl == NULL_TREE
10223 && !MAIN_NAME_P (DECL_NAME (decl1))
10224 && !DECL_DECLARED_INLINE_P (decl1))
10225 warning_at (loc, OPT_Wmissing_declarations,
10226 "no previous declaration for %qD",
10227 decl1);
10228 /* Optionally warn of any def with no previous declaration
10229 if the function has already been used. */
10230 else if (warn_missing_declarations
10231 && old_decl != NULL_TREE
10232 && old_decl != error_mark_node
10233 && TREE_USED (old_decl)
10234 && C_DECL_IMPLICIT (old_decl))
10235 warning_at (loc, OPT_Wmissing_declarations,
10236 "%qD was used with no declaration before its definition", decl1);
10238 /* This function exists in static storage.
10239 (This does not mean `static' in the C sense!) */
10240 TREE_STATIC (decl1) = 1;
10242 /* This is the earliest point at which we might know the assembler
10243 name of the function. Thus, if it's set before this, die horribly. */
10244 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10246 /* If #pragma weak was used, mark the decl weak now. */
10247 if (current_scope == file_scope)
10248 maybe_apply_pragma_weak (decl1);
10250 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10251 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10253 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10254 != integer_type_node)
10255 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10256 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10257 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10258 decl1);
10260 check_main_parameter_types (decl1);
10262 if (!TREE_PUBLIC (decl1))
10263 pedwarn (loc, OPT_Wmain,
10264 "%qD is normally a non-static function", decl1);
10267 tree parms = current_function_arg_info->parms;
10268 if (old_decl)
10270 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10271 warn_parm_array_mismatch (origloc, old_decl, parms);
10274 /* Record the decl so that the function name is defined.
10275 If we already have a decl for this name, and it is a FUNCTION_DECL,
10276 use the old decl. */
10278 current_function_decl = pushdecl (decl1);
10280 if (tree access = build_attr_access_from_parms (parms, false))
10281 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
10282 old_decl);
10284 push_scope ();
10285 declare_parm_level ();
10287 /* Set the result decl source location to the location of the typespec. */
10288 result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10289 ? loc : declspecs->locations[cdw_typespec]);
10290 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10291 resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10292 DECL_ARTIFICIAL (resdecl) = 1;
10293 DECL_IGNORED_P (resdecl) = 1;
10294 DECL_RESULT (current_function_decl) = resdecl;
10296 start_fname_decls ();
10298 return true;
10301 /* Subroutine of store_parm_decls which handles new-style function
10302 definitions (prototype format). The parms already have decls, so we
10303 need only record them as in effect and complain if any redundant
10304 old-style parm decls were written. */
10305 static void
10306 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10308 tree decl;
10309 c_arg_tag *tag;
10310 unsigned ix;
10312 if (current_scope->bindings)
10314 error_at (DECL_SOURCE_LOCATION (fndecl),
10315 "old-style parameter declarations in prototyped "
10316 "function definition");
10318 /* Get rid of the old-style declarations. */
10319 pop_scope ();
10320 push_scope ();
10322 /* Don't issue this warning for nested functions, and don't issue this
10323 warning if we got here because ARG_INFO_TYPES was error_mark_node
10324 (this happens when a function definition has just an ellipsis in
10325 its parameter list). */
10326 else if (!in_system_header_at (input_location)
10327 && !current_function_scope
10328 && arg_info->types != error_mark_node)
10329 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10330 "traditional C rejects ISO C style function definitions");
10332 /* Now make all the parameter declarations visible in the function body.
10333 We can bypass most of the grunt work of pushdecl. */
10334 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10336 DECL_CONTEXT (decl) = current_function_decl;
10337 if (DECL_NAME (decl))
10339 bind (DECL_NAME (decl), decl, current_scope,
10340 /*invisible=*/false, /*nested=*/false,
10341 UNKNOWN_LOCATION);
10342 if (!TREE_USED (decl))
10343 warn_if_shadowing (decl);
10345 else
10346 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10347 "ISO C does not support omitting parameter names in "
10348 "function definitions before C2X");
10351 /* Record the parameter list in the function declaration. */
10352 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10354 /* Now make all the ancillary declarations visible, likewise. */
10355 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10357 DECL_CONTEXT (decl) = current_function_decl;
10358 if (DECL_NAME (decl))
10359 bind (DECL_NAME (decl), decl, current_scope,
10360 /*invisible=*/false,
10361 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10362 UNKNOWN_LOCATION);
10365 /* And all the tag declarations. */
10366 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10367 if (tag->id)
10368 bind (tag->id, tag->type, current_scope,
10369 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10372 /* Subroutine of store_parm_decls which handles old-style function
10373 definitions (separate parameter list and declarations). */
10375 static void
10376 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10378 struct c_binding *b;
10379 tree parm, decl, last;
10380 tree parmids = arg_info->parms;
10381 hash_set<tree> seen_args;
10383 if (!in_system_header_at (input_location))
10385 if (flag_isoc2x)
10386 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10387 OPT_Wold_style_definition, "old-style function definition");
10388 else
10389 warning_at (DECL_SOURCE_LOCATION (fndecl),
10390 OPT_Wold_style_definition,
10391 "old-style function definition");
10394 if (current_scope->had_vla_unspec)
10395 error ("%<[*]%> not allowed in other than function prototype scope");
10397 /* Match each formal parameter name with its declaration. Save each
10398 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10399 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10401 if (TREE_VALUE (parm) == NULL_TREE)
10403 error_at (DECL_SOURCE_LOCATION (fndecl),
10404 "parameter name missing from parameter list");
10405 TREE_PURPOSE (parm) = NULL_TREE;
10406 continue;
10409 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10410 if (b && B_IN_CURRENT_SCOPE (b))
10412 decl = b->decl;
10413 /* Skip erroneous parameters. */
10414 if (decl == error_mark_node)
10415 continue;
10416 /* If we got something other than a PARM_DECL it is an error. */
10417 if (TREE_CODE (decl) != PARM_DECL)
10419 error_at (DECL_SOURCE_LOCATION (decl),
10420 "%qD declared as a non-parameter", decl);
10421 continue;
10423 /* If the declaration is already marked, we have a duplicate
10424 name. Complain and ignore the duplicate. */
10425 else if (seen_args.contains (decl))
10427 error_at (DECL_SOURCE_LOCATION (decl),
10428 "multiple parameters named %qD", decl);
10429 TREE_PURPOSE (parm) = NULL_TREE;
10430 continue;
10432 /* If the declaration says "void", complain and turn it into
10433 an int. */
10434 else if (VOID_TYPE_P (TREE_TYPE (decl)))
10436 error_at (DECL_SOURCE_LOCATION (decl),
10437 "parameter %qD declared with void type", decl);
10438 TREE_TYPE (decl) = integer_type_node;
10439 DECL_ARG_TYPE (decl) = integer_type_node;
10440 layout_decl (decl, 0);
10442 warn_if_shadowing (decl);
10444 /* If no declaration found, default to int. */
10445 else
10447 /* FIXME diagnostics: This should be the location of the argument,
10448 not the FNDECL. E.g., for an old-style declaration
10450 int f10(v) { blah; }
10452 We should use the location of the V, not the F10.
10453 Unfortunately, the V is an IDENTIFIER_NODE which has no
10454 location. In the future we need locations for c_arg_info
10455 entries.
10457 See gcc.dg/Wshadow-3.c for an example of this problem. */
10458 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10459 PARM_DECL, TREE_VALUE (parm), integer_type_node);
10460 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10461 pushdecl (decl);
10462 warn_if_shadowing (decl);
10464 if (flag_isoc99)
10465 pedwarn (DECL_SOURCE_LOCATION (decl),
10466 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10467 decl);
10468 else
10469 warning_at (DECL_SOURCE_LOCATION (decl),
10470 OPT_Wmissing_parameter_type,
10471 "type of %qD defaults to %<int%>", decl);
10474 TREE_PURPOSE (parm) = decl;
10475 seen_args.add (decl);
10478 /* Now examine the parms chain for incomplete declarations
10479 and declarations with no corresponding names. */
10481 for (b = current_scope->bindings; b; b = b->prev)
10483 parm = b->decl;
10484 if (TREE_CODE (parm) != PARM_DECL)
10485 continue;
10487 if (TREE_TYPE (parm) != error_mark_node
10488 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10490 error_at (DECL_SOURCE_LOCATION (parm),
10491 "parameter %qD has incomplete type", parm);
10492 TREE_TYPE (parm) = error_mark_node;
10495 if (!seen_args.contains (parm))
10497 error_at (DECL_SOURCE_LOCATION (parm),
10498 "declaration for parameter %qD but no such parameter",
10499 parm);
10501 /* Pretend the parameter was not missing.
10502 This gets us to a standard state and minimizes
10503 further error messages. */
10504 parmids = chainon (parmids, tree_cons (parm, 0, 0));
10508 /* Chain the declarations together in the order of the list of
10509 names. Store that chain in the function decl, replacing the
10510 list of names. Update the current scope to match. */
10511 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10513 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10514 if (TREE_PURPOSE (parm))
10515 break;
10516 if (parm && TREE_PURPOSE (parm))
10518 last = TREE_PURPOSE (parm);
10519 DECL_ARGUMENTS (fndecl) = last;
10521 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10522 if (TREE_PURPOSE (parm))
10524 DECL_CHAIN (last) = TREE_PURPOSE (parm);
10525 last = TREE_PURPOSE (parm);
10527 DECL_CHAIN (last) = NULL_TREE;
10530 /* If there was a previous prototype,
10531 set the DECL_ARG_TYPE of each argument according to
10532 the type previously specified, and report any mismatches. */
10534 if (current_function_prototype_arg_types)
10536 tree type;
10537 for (parm = DECL_ARGUMENTS (fndecl),
10538 type = current_function_prototype_arg_types;
10539 parm || (type != NULL_TREE
10540 && TREE_VALUE (type) != error_mark_node
10541 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10542 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10544 if (parm == NULL_TREE
10545 || type == NULL_TREE
10546 || (TREE_VALUE (type) != error_mark_node
10547 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10549 if (current_function_prototype_built_in)
10550 warning_at (DECL_SOURCE_LOCATION (fndecl),
10551 0, "number of arguments doesn%'t match "
10552 "built-in prototype");
10553 else
10555 /* FIXME diagnostics: This should be the location of
10556 FNDECL, but there is bug when a prototype is
10557 declared inside function context, but defined
10558 outside of it (e.g., gcc.dg/pr15698-2.c). In
10559 which case FNDECL gets the location of the
10560 prototype, not the definition. */
10561 error_at (input_location,
10562 "number of arguments doesn%'t match prototype");
10564 error_at (current_function_prototype_locus,
10565 "prototype declaration");
10567 break;
10569 /* Type for passing arg must be consistent with that
10570 declared for the arg. ISO C says we take the unqualified
10571 type for parameters declared with qualified type. */
10572 if (TREE_TYPE (parm) != error_mark_node
10573 && TREE_VALUE (type) != error_mark_node
10574 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10575 != TYPE_ATOMIC (TREE_VALUE (type)))
10576 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10577 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10579 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10580 == TYPE_ATOMIC (TREE_VALUE (type)))
10581 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10582 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10584 /* Adjust argument to match prototype. E.g. a previous
10585 `int foo(float);' prototype causes
10586 `int foo(x) float x; {...}' to be treated like
10587 `int foo(float x) {...}'. This is particularly
10588 useful for argument types like uid_t. */
10589 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10591 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10592 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10593 && (TYPE_PRECISION (TREE_TYPE (parm))
10594 < TYPE_PRECISION (integer_type_node)))
10595 DECL_ARG_TYPE (parm)
10596 = c_type_promotes_to (TREE_TYPE (parm));
10598 /* ??? Is it possible to get here with a
10599 built-in prototype or will it always have
10600 been diagnosed as conflicting with an
10601 old-style definition and discarded? */
10602 if (current_function_prototype_built_in)
10603 warning_at (DECL_SOURCE_LOCATION (parm),
10604 OPT_Wpedantic, "promoted argument %qD "
10605 "doesn%'t match built-in prototype", parm);
10606 else
10608 pedwarn (DECL_SOURCE_LOCATION (parm),
10609 OPT_Wpedantic, "promoted argument %qD "
10610 "doesn%'t match prototype", parm);
10611 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10612 "prototype declaration");
10615 else
10617 if (current_function_prototype_built_in)
10618 warning_at (DECL_SOURCE_LOCATION (parm),
10619 0, "argument %qD doesn%'t match "
10620 "built-in prototype", parm);
10621 else
10623 error_at (DECL_SOURCE_LOCATION (parm),
10624 "argument %qD doesn%'t match prototype", parm);
10625 error_at (current_function_prototype_locus,
10626 "prototype declaration");
10631 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10634 /* Otherwise, create a prototype that would match. */
10636 else
10638 tree actual = NULL_TREE, last = NULL_TREE, type;
10640 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10642 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10643 if (last)
10644 TREE_CHAIN (last) = type;
10645 else
10646 actual = type;
10647 last = type;
10649 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10650 if (last)
10651 TREE_CHAIN (last) = type;
10652 else
10653 actual = type;
10655 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10656 of the type of this function, but we need to avoid having this
10657 affect the types of other similarly-typed functions, so we must
10658 first force the generation of an identical (but separate) type
10659 node for the relevant function type. The new node we create
10660 will be a variant of the main variant of the original function
10661 type. */
10663 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10665 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10669 /* Store parameter declarations passed in ARG_INFO into the current
10670 function declaration. */
10672 void
10673 store_parm_decls_from (struct c_arg_info *arg_info)
10675 current_function_arg_info = arg_info;
10676 store_parm_decls ();
10679 /* Called by walk_tree to look for and update context-less labels
10680 or labels with context in the parent function. */
10682 static tree
10683 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10685 tree ctx = static_cast<tree>(data);
10686 if (TREE_CODE (*tp) == LABEL_EXPR
10687 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10688 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10690 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10691 *walk_subtrees = 0;
10694 return NULL_TREE;
10697 /* Store the parameter declarations into the current function declaration.
10698 This is called after parsing the parameter declarations, before
10699 digesting the body of the function.
10701 For an old-style definition, construct a prototype out of the old-style
10702 parameter declarations and inject it into the function's type. */
10704 void
10705 store_parm_decls (void)
10707 tree fndecl = current_function_decl;
10708 bool proto;
10710 /* The argument information block for FNDECL. */
10711 struct c_arg_info *arg_info = current_function_arg_info;
10712 current_function_arg_info = 0;
10714 /* True if this definition is written with a prototype. In C2X, an
10715 empty argument list was converted to (void) in grokparms; in
10716 older C standard versions, it does not give the function a type
10717 with a prototype for future calls. */
10718 proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
10720 if (proto)
10721 store_parm_decls_newstyle (fndecl, arg_info);
10722 else
10723 store_parm_decls_oldstyle (fndecl, arg_info);
10725 /* The next call to push_scope will be a function body. */
10727 next_is_function_body = true;
10729 /* Write a record describing this function definition to the prototypes
10730 file (if requested). */
10732 gen_aux_info_record (fndecl, 1, 0, proto);
10734 /* Initialize the RTL code for the function. */
10735 allocate_struct_function (fndecl, false);
10737 if (warn_unused_local_typedefs)
10738 cfun->language = ggc_cleared_alloc<language_function> ();
10740 /* Begin the statement tree for this function. */
10741 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10743 /* ??? Insert the contents of the pending sizes list into the function
10744 to be evaluated. The only reason left to have this is
10745 void foo(int n, int array[n++])
10746 because we throw away the array type in favor of a pointer type, and
10747 thus won't naturally see the SAVE_EXPR containing the increment. All
10748 other pending sizes would be handled by gimplify_parameters. */
10749 if (arg_info->pending_sizes)
10751 /* In very special circumstances, e.g. for code like
10752 _Atomic int i = 5;
10753 void f (int a[i += 2]) {}
10754 we need to execute the atomic assignment on function entry.
10755 But in this case, it is not just a straight store, it has the
10756 op= form, which means that build_atomic_assign has generated
10757 gotos, labels, etc. Because at that time the function decl
10758 for F has not been created yet, those labels do not have any
10759 function context. But we have the fndecl now, so update the
10760 labels accordingly. gimplify_expr would crash otherwise.
10761 Or with nested functions the labels could be created with parent
10762 function's context, while when the statement is emitted at the
10763 start of the nested function, it needs the nested function's
10764 context. */
10765 walk_tree_without_duplicates (&arg_info->pending_sizes,
10766 set_labels_context_r, fndecl);
10767 add_stmt (arg_info->pending_sizes);
10771 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
10772 c_finish_omp_declare_simd for function prototypes. No diagnostics
10773 should be done. */
10775 void
10776 temp_store_parm_decls (tree fndecl, tree parms)
10778 push_scope ();
10779 for (tree p = parms; p; p = DECL_CHAIN (p))
10781 DECL_CONTEXT (p) = fndecl;
10782 if (DECL_NAME (p))
10783 bind (DECL_NAME (p), p, current_scope,
10784 /*invisible=*/false, /*nested=*/false,
10785 UNKNOWN_LOCATION);
10789 /* Undo what temp_store_parm_decls did. */
10791 void
10792 temp_pop_parm_decls (void)
10794 /* Clear all bindings in this temporary scope, so that
10795 pop_scope doesn't create a BLOCK. */
10796 struct c_binding *b = current_scope->bindings;
10797 current_scope->bindings = NULL;
10798 for (; b; b = free_binding_and_advance (b))
10800 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10801 || b->decl == error_mark_node);
10802 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10803 I_SYMBOL_BINDING (b->id) = b->shadowed;
10804 if (b->shadowed && b->shadowed->u.type)
10805 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10807 pop_scope ();
10811 /* Finish up a function declaration and compile that function
10812 all the way to assembler language output. Then free the storage
10813 for the function definition.
10815 This is called after parsing the body of the function definition. */
10817 void
10818 finish_function (location_t end_loc)
10820 tree fndecl = current_function_decl;
10822 if (c_dialect_objc ())
10823 objc_finish_function ();
10825 if (TREE_CODE (fndecl) == FUNCTION_DECL
10826 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10828 tree args = DECL_ARGUMENTS (fndecl);
10829 for (; args; args = DECL_CHAIN (args))
10831 tree type = TREE_TYPE (args);
10832 if (INTEGRAL_TYPE_P (type)
10833 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10834 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10838 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10839 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10841 /* Must mark the RESULT_DECL as being in this function. */
10843 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10844 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10846 if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
10847 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10848 == integer_type_node && flag_isoc99)
10850 /* Hack. We don't want the middle-end to warn that this return
10851 is unreachable, so we mark its location as special. Using
10852 UNKNOWN_LOCATION has the problem that it gets clobbered in
10853 annotate_one_with_locus. A cleaner solution might be to
10854 ensure ! should_carry_locus_p (stmt), but that needs a flag.
10856 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10859 /* Tie off the statement tree for this function. */
10860 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10862 finish_fname_decls ();
10864 /* Complain if there's no return statement only if option specified on
10865 command line. */
10866 if (warn_return_type > 0
10867 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10868 && !current_function_returns_value && !current_function_returns_null
10869 /* Don't complain if we are no-return. */
10870 && !current_function_returns_abnormally
10871 /* Don't complain if we are declared noreturn. */
10872 && !TREE_THIS_VOLATILE (fndecl)
10873 /* Don't warn for main(). */
10874 && !MAIN_NAME_P (DECL_NAME (fndecl))
10875 /* Or if they didn't actually specify a return type. */
10876 && !C_FUNCTION_IMPLICIT_INT (fndecl)
10877 /* Normally, with -Wreturn-type, flow will complain, but we might
10878 optimize out static functions. */
10879 && !TREE_PUBLIC (fndecl)
10880 && targetm.warn_func_return (fndecl)
10881 && warning (OPT_Wreturn_type,
10882 "no return statement in function returning non-void"))
10883 suppress_warning (fndecl, OPT_Wreturn_type);
10885 /* Complain about parameters that are only set, but never otherwise used. */
10886 if (warn_unused_but_set_parameter)
10888 tree decl;
10890 for (decl = DECL_ARGUMENTS (fndecl);
10891 decl;
10892 decl = DECL_CHAIN (decl))
10893 if (TREE_USED (decl)
10894 && TREE_CODE (decl) == PARM_DECL
10895 && !DECL_READ_P (decl)
10896 && DECL_NAME (decl)
10897 && !DECL_ARTIFICIAL (decl)
10898 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
10899 warning_at (DECL_SOURCE_LOCATION (decl),
10900 OPT_Wunused_but_set_parameter,
10901 "parameter %qD set but not used", decl);
10904 /* Complain about locally defined typedefs that are not used in this
10905 function. */
10906 maybe_warn_unused_local_typedefs ();
10908 /* Possibly warn about unused parameters. */
10909 if (warn_unused_parameter)
10910 do_warn_unused_parameter (fndecl);
10912 /* Store the end of the function, so that we get good line number
10913 info for the epilogue. */
10914 cfun->function_end_locus = end_loc;
10916 /* Finalize the ELF visibility for the function. */
10917 c_determine_visibility (fndecl);
10919 /* For GNU C extern inline functions disregard inline limits. */
10920 if (DECL_EXTERNAL (fndecl)
10921 && DECL_DECLARED_INLINE_P (fndecl)
10922 && (flag_gnu89_inline
10923 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10924 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10926 /* Genericize before inlining. Delay genericizing nested functions
10927 until their parent function is genericized. Since finalizing
10928 requires GENERIC, delay that as well. */
10930 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10931 && !undef_nested_function)
10933 if (!decl_function_context (fndecl))
10935 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10936 c_genericize (fndecl);
10938 /* ??? Objc emits functions after finalizing the compilation unit.
10939 This should be cleaned up later and this conditional removed. */
10940 if (symtab->global_info_ready)
10942 cgraph_node::add_new_function (fndecl, false);
10943 return;
10945 cgraph_node::finalize_function (fndecl, false);
10947 else
10949 /* Register this function with cgraph just far enough to get it
10950 added to our parent's nested function list. Handy, since the
10951 C front end doesn't have such a list. */
10952 (void) cgraph_node::get_create (fndecl);
10956 if (!decl_function_context (fndecl))
10957 undef_nested_function = false;
10959 if (cfun->language != NULL)
10961 ggc_free (cfun->language);
10962 cfun->language = NULL;
10965 /* We're leaving the context of this function, so zap cfun.
10966 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10967 tree_rest_of_compilation. */
10968 set_cfun (NULL);
10969 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10970 current_function_decl = NULL;
10973 /* Check the declarations given in a for-loop for satisfying the C99
10974 constraints. If exactly one such decl is found, return it. LOC is
10975 the location of the opening parenthesis of the for loop. The last
10976 parameter allows you to control the "for loop initial declarations
10977 are only allowed in C99 mode". Normally, you should pass
10978 flag_isoc99 as that parameter. But in some cases (Objective-C
10979 foreach loop, for example) we want to run the checks in this
10980 function even if not in C99 mode, so we allow the caller to turn
10981 off the error about not being in C99 mode.
10984 tree
10985 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10987 struct c_binding *b;
10988 tree one_decl = NULL_TREE;
10989 int n_decls = 0;
10991 if (!turn_off_iso_c99_error)
10993 static bool hint = true;
10994 /* If we get here, declarations have been used in a for loop without
10995 the C99 for loop scope. This doesn't make much sense, so don't
10996 allow it. */
10997 auto_diagnostic_group d;
10998 error_at (loc, "%<for%> loop initial declarations "
10999 "are only allowed in C99 or C11 mode");
11000 if (hint)
11002 inform (loc,
11003 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11004 "%<-std=gnu11%> to compile your code");
11005 hint = false;
11007 return NULL_TREE;
11009 else
11010 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11011 "initial declarations");
11013 /* C99 subclause 6.8.5 paragraph 3:
11015 [#3] The declaration part of a for statement shall only
11016 declare identifiers for objects having storage class auto or
11017 register.
11019 It isn't clear whether, in this sentence, "identifiers" binds to
11020 "shall only declare" or to "objects" - that is, whether all identifiers
11021 declared must be identifiers for objects, or whether the restriction
11022 only applies to those that are. (A question on this in comp.std.c
11023 in November 2000 received no answer.) We implement the strictest
11024 interpretation, to avoid creating an extension which later causes
11025 problems. */
11027 for (b = current_scope->bindings; b; b = b->prev)
11029 tree id = b->id;
11030 tree decl = b->decl;
11032 if (!id)
11033 continue;
11035 switch (TREE_CODE (decl))
11037 case VAR_DECL:
11039 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11040 if (TREE_STATIC (decl))
11041 error_at (decl_loc,
11042 "declaration of static variable %qD in %<for%> loop "
11043 "initial declaration", decl);
11044 else if (DECL_EXTERNAL (decl))
11045 error_at (decl_loc,
11046 "declaration of %<extern%> variable %qD in %<for%> loop "
11047 "initial declaration", decl);
11049 break;
11051 case RECORD_TYPE:
11052 error_at (loc,
11053 "%<struct %E%> declared in %<for%> loop initial "
11054 "declaration", id);
11055 break;
11056 case UNION_TYPE:
11057 error_at (loc,
11058 "%<union %E%> declared in %<for%> loop initial declaration",
11059 id);
11060 break;
11061 case ENUMERAL_TYPE:
11062 error_at (loc, "%<enum %E%> declared in %<for%> loop "
11063 "initial declaration", id);
11064 break;
11065 default:
11066 error_at (loc, "declaration of non-variable "
11067 "%qD in %<for%> loop initial declaration", decl);
11070 n_decls++;
11071 one_decl = decl;
11074 return n_decls == 1 ? one_decl : NULL_TREE;
11077 /* Save and reinitialize the variables
11078 used during compilation of a C function. */
11080 void
11081 c_push_function_context (void)
11083 struct language_function *p = cfun->language;
11084 /* cfun->language might have been already allocated by the use of
11085 -Wunused-local-typedefs. In that case, just re-use it. */
11086 if (p == NULL)
11087 cfun->language = p = ggc_cleared_alloc<language_function> ();
11089 p->base.x_stmt_tree = c_stmt_tree;
11090 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11091 p->x_in_statement = in_statement;
11092 p->x_switch_stack = c_switch_stack;
11093 p->arg_info = current_function_arg_info;
11094 p->returns_value = current_function_returns_value;
11095 p->returns_null = current_function_returns_null;
11096 p->returns_abnormally = current_function_returns_abnormally;
11097 p->warn_about_return_type = warn_about_return_type;
11099 push_function_context ();
11102 /* Restore the variables used during compilation of a C function. */
11104 void
11105 c_pop_function_context (void)
11107 struct language_function *p;
11109 pop_function_context ();
11110 p = cfun->language;
11112 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11113 used to store data throughout the life time of the current cfun,
11114 So don't deallocate it. */
11115 if (!warn_unused_local_typedefs)
11116 cfun->language = NULL;
11118 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11119 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11121 /* Stop pointing to the local nodes about to be freed. */
11122 /* But DECL_INITIAL must remain nonzero so we know this
11123 was an actual function definition. */
11124 DECL_INITIAL (current_function_decl) = error_mark_node;
11125 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11128 c_stmt_tree = p->base.x_stmt_tree;
11129 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11130 in_statement = p->x_in_statement;
11131 c_switch_stack = p->x_switch_stack;
11132 current_function_arg_info = p->arg_info;
11133 current_function_returns_value = p->returns_value;
11134 current_function_returns_null = p->returns_null;
11135 current_function_returns_abnormally = p->returns_abnormally;
11136 warn_about_return_type = p->warn_about_return_type;
11139 /* The functions below are required for functionality of doing
11140 function at once processing in the C front end. Currently these
11141 functions are not called from anywhere in the C front end, but as
11142 these changes continue, that will change. */
11144 /* Returns the stmt_tree (if any) to which statements are currently
11145 being added. If there is no active statement-tree, NULL is
11146 returned. */
11148 stmt_tree
11149 current_stmt_tree (void)
11151 return &c_stmt_tree;
11154 /* Return the global value of T as a symbol. */
11156 tree
11157 identifier_global_value (tree t)
11159 struct c_binding *b;
11161 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11162 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11163 return b->decl;
11165 return NULL_TREE;
11168 /* Return the global value of tag T as a symbol. */
11170 tree
11171 identifier_global_tag (tree t)
11173 struct c_binding *b;
11175 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11176 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11177 return b->decl;
11179 return NULL_TREE;
11182 /* Returns true if NAME refers to a built-in function or function-like
11183 operator. */
11185 bool
11186 names_builtin_p (const char *name)
11188 tree id = get_identifier (name);
11189 if (tree decl = identifier_global_value (id))
11190 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11192 /* Also detect common reserved C words that aren't strictly built-in
11193 functions. */
11194 switch (C_RID_CODE (id))
11196 case RID_BUILTIN_CONVERTVECTOR:
11197 case RID_BUILTIN_HAS_ATTRIBUTE:
11198 case RID_BUILTIN_SHUFFLE:
11199 case RID_BUILTIN_SHUFFLEVECTOR:
11200 case RID_BUILTIN_ASSOC_BARRIER:
11201 case RID_CHOOSE_EXPR:
11202 case RID_OFFSETOF:
11203 case RID_TYPES_COMPATIBLE_P:
11204 return true;
11205 default:
11206 break;
11209 return false;
11212 /* In C, the only C-linkage public declaration is at file scope. */
11214 tree
11215 c_linkage_bindings (tree name)
11217 return identifier_global_value (name);
11220 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11221 otherwise the name is found in ridpointers from RID_INDEX. */
11223 void
11224 record_builtin_type (enum rid rid_index, const char *name, tree type)
11226 tree id, decl;
11227 if (name == 0)
11228 id = ridpointers[(int) rid_index];
11229 else
11230 id = get_identifier (name);
11231 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11232 pushdecl (decl);
11233 if (debug_hooks->type_decl)
11234 debug_hooks->type_decl (decl, false);
11237 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11239 struct c_parm *
11240 build_c_parm (struct c_declspecs *specs, tree attrs,
11241 struct c_declarator *declarator,
11242 location_t loc)
11244 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11245 ret->specs = specs;
11246 ret->attrs = attrs;
11247 ret->declarator = declarator;
11248 ret->loc = loc;
11249 return ret;
11252 /* Return a declarator with nested attributes. TARGET is the inner
11253 declarator to which these attributes apply. ATTRS are the
11254 attributes. */
11256 struct c_declarator *
11257 build_attrs_declarator (tree attrs, struct c_declarator *target)
11259 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11260 ret->kind = cdk_attrs;
11261 ret->declarator = target;
11262 ret->u.attrs = attrs;
11263 return ret;
11266 /* Return a declarator for a function with arguments specified by ARGS
11267 and return type specified by TARGET. */
11269 struct c_declarator *
11270 build_function_declarator (struct c_arg_info *args,
11271 struct c_declarator *target)
11273 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11274 ret->kind = cdk_function;
11275 ret->declarator = target;
11276 ret->u.arg_info = args;
11277 return ret;
11280 /* Return a declarator for the identifier IDENT (which may be
11281 NULL_TREE for an abstract declarator). */
11283 struct c_declarator *
11284 build_id_declarator (tree ident)
11286 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11287 ret->kind = cdk_id;
11288 ret->declarator = 0;
11289 ret->u.id.id = ident;
11290 ret->u.id.attrs = NULL_TREE;
11291 /* Default value - may get reset to a more precise location. */
11292 ret->id_loc = input_location;
11293 return ret;
11296 /* Return something to represent absolute declarators containing a *.
11297 TARGET is the absolute declarator that the * contains.
11298 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11299 to apply to the pointer type. */
11301 struct c_declarator *
11302 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11303 struct c_declarator *target)
11305 tree attrs;
11306 int quals = 0;
11307 struct c_declarator *itarget = target;
11308 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11309 if (type_quals_attrs)
11311 attrs = type_quals_attrs->attrs;
11312 quals = quals_from_declspecs (type_quals_attrs);
11313 if (attrs != NULL_TREE)
11314 itarget = build_attrs_declarator (attrs, target);
11316 ret->kind = cdk_pointer;
11317 ret->declarator = itarget;
11318 ret->u.pointer_quals = quals;
11319 return ret;
11322 /* Return a pointer to a structure for an empty list of declaration
11323 specifiers. */
11325 struct c_declspecs *
11326 build_null_declspecs (void)
11328 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11329 memset (ret, 0, sizeof *ret);
11330 ret->align_log = -1;
11331 ret->typespec_word = cts_none;
11332 ret->storage_class = csc_none;
11333 ret->expr_const_operands = true;
11334 ret->typespec_kind = ctsk_none;
11335 ret->address_space = ADDR_SPACE_GENERIC;
11336 return ret;
11339 /* Add the address space ADDRSPACE to the declaration specifiers
11340 SPECS, returning SPECS. */
11342 struct c_declspecs *
11343 declspecs_add_addrspace (location_t location,
11344 struct c_declspecs *specs, addr_space_t as)
11346 specs->non_sc_seen_p = true;
11347 specs->declspecs_seen_p = true;
11348 specs->non_std_attrs_seen_p = true;
11350 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11351 && specs->address_space != as)
11352 error ("incompatible address space qualifiers %qs and %qs",
11353 c_addr_space_name (as),
11354 c_addr_space_name (specs->address_space));
11355 else
11357 specs->address_space = as;
11358 specs->locations[cdw_address_space] = location;
11360 return specs;
11363 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11364 returning SPECS. */
11366 struct c_declspecs *
11367 declspecs_add_qual (location_t loc,
11368 struct c_declspecs *specs, tree qual)
11370 enum rid i;
11371 bool dupe = false;
11372 specs->non_sc_seen_p = true;
11373 specs->declspecs_seen_p = true;
11374 specs->non_std_attrs_seen_p = true;
11375 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11376 && C_IS_RESERVED_WORD (qual));
11377 i = C_RID_CODE (qual);
11378 location_t prev_loc = UNKNOWN_LOCATION;
11379 switch (i)
11381 case RID_CONST:
11382 dupe = specs->const_p;
11383 specs->const_p = true;
11384 prev_loc = specs->locations[cdw_const];
11385 specs->locations[cdw_const] = loc;
11386 break;
11387 case RID_VOLATILE:
11388 dupe = specs->volatile_p;
11389 specs->volatile_p = true;
11390 prev_loc = specs->locations[cdw_volatile];
11391 specs->locations[cdw_volatile] = loc;
11392 break;
11393 case RID_RESTRICT:
11394 dupe = specs->restrict_p;
11395 specs->restrict_p = true;
11396 prev_loc = specs->locations[cdw_restrict];
11397 specs->locations[cdw_restrict] = loc;
11398 break;
11399 case RID_ATOMIC:
11400 dupe = specs->atomic_p;
11401 specs->atomic_p = true;
11402 prev_loc = specs->locations[cdw_atomic];
11403 specs->locations[cdw_atomic] = loc;
11404 break;
11405 default:
11406 gcc_unreachable ();
11408 if (dupe)
11410 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11411 "duplicate %qE declaration specifier", qual);
11412 if (!warned
11413 && warn_duplicate_decl_specifier
11414 && prev_loc >= RESERVED_LOCATION_COUNT
11415 && !from_macro_expansion_at (prev_loc)
11416 && !from_macro_expansion_at (loc))
11417 warning_at (loc, OPT_Wduplicate_decl_specifier,
11418 "duplicate %qE declaration specifier", qual);
11420 return specs;
11423 /* Add the type specifier TYPE to the declaration specifiers SPECS,
11424 returning SPECS. */
11426 struct c_declspecs *
11427 declspecs_add_type (location_t loc, struct c_declspecs *specs,
11428 struct c_typespec spec)
11430 tree type = spec.spec;
11431 specs->non_sc_seen_p = true;
11432 specs->declspecs_seen_p = true;
11433 specs->non_std_attrs_seen_p = true;
11434 specs->typespec_kind = spec.kind;
11435 if (TREE_DEPRECATED (type))
11436 specs->deprecated_p = true;
11437 if (TREE_UNAVAILABLE (type))
11438 specs->unavailable_p = true;
11440 /* As a type specifier is present, "auto" must be used as a storage
11441 class specifier, not for type deduction. */
11442 if (specs->c2x_auto_p)
11444 specs->c2x_auto_p = false;
11445 if (specs->storage_class != csc_none)
11446 error ("multiple storage classes in declaration specifiers");
11447 else if (specs->thread_p)
11448 error ("%qs used with %<auto%>",
11449 specs->thread_gnu_p ? "__thread" : "_Thread_local");
11450 else if (specs->constexpr_p)
11451 /* auto may only be used with another storage class specifier,
11452 such as constexpr, if the type is inferred. */
11453 error ("%<auto%> used with %<constexpr%>");
11454 else
11455 specs->storage_class = csc_auto;
11458 /* Handle type specifier keywords. */
11459 if (TREE_CODE (type) == IDENTIFIER_NODE
11460 && C_IS_RESERVED_WORD (type)
11461 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11463 enum rid i = C_RID_CODE (type);
11464 if (specs->type)
11466 error_at (loc, "two or more data types in declaration specifiers");
11467 return specs;
11469 if ((int) i <= (int) RID_LAST_MODIFIER)
11471 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11472 bool dupe = false;
11473 switch (i)
11475 case RID_LONG:
11476 if (specs->long_long_p)
11478 error_at (loc, "%<long long long%> is too long for GCC");
11479 break;
11481 if (specs->long_p)
11483 if (specs->typespec_word == cts_double)
11485 error_at (loc,
11486 ("both %<long long%> and %<double%> in "
11487 "declaration specifiers"));
11488 break;
11490 pedwarn_c90 (loc, OPT_Wlong_long,
11491 "ISO C90 does not support %<long long%>");
11492 specs->long_long_p = 1;
11493 specs->locations[cdw_long_long] = loc;
11494 break;
11496 if (specs->short_p)
11497 error_at (loc,
11498 ("both %<long%> and %<short%> in "
11499 "declaration specifiers"));
11500 else if (specs->typespec_word == cts_auto_type)
11501 error_at (loc,
11502 ("both %<long%> and %<__auto_type%> in "
11503 "declaration specifiers"));
11504 else if (specs->typespec_word == cts_void)
11505 error_at (loc,
11506 ("both %<long%> and %<void%> in "
11507 "declaration specifiers"));
11508 else if (specs->typespec_word == cts_int_n)
11509 error_at (loc,
11510 ("both %<long%> and %<__int%d%> in "
11511 "declaration specifiers"),
11512 int_n_data[specs->int_n_idx].bitsize);
11513 else if (specs->typespec_word == cts_bool)
11514 error_at (loc,
11515 ("both %<long%> and %<_Bool%> in "
11516 "declaration specifiers"));
11517 else if (specs->typespec_word == cts_char)
11518 error_at (loc,
11519 ("both %<long%> and %<char%> in "
11520 "declaration specifiers"));
11521 else if (specs->typespec_word == cts_float)
11522 error_at (loc,
11523 ("both %<long%> and %<float%> in "
11524 "declaration specifiers"));
11525 else if (specs->typespec_word == cts_floatn_nx)
11526 error_at (loc,
11527 ("both %<long%> and %<_Float%d%s%> in "
11528 "declaration specifiers"),
11529 floatn_nx_types[specs->floatn_nx_idx].n,
11530 (floatn_nx_types[specs->floatn_nx_idx].extended
11531 ? "x"
11532 : ""));
11533 else if (specs->typespec_word == cts_dfloat32)
11534 error_at (loc,
11535 ("both %<long%> and %<_Decimal32%> in "
11536 "declaration specifiers"));
11537 else if (specs->typespec_word == cts_dfloat64)
11538 error_at (loc,
11539 ("both %<long%> and %<_Decimal64%> in "
11540 "declaration specifiers"));
11541 else if (specs->typespec_word == cts_dfloat128)
11542 error_at (loc,
11543 ("both %<long%> and %<_Decimal128%> in "
11544 "declaration specifiers"));
11545 else
11547 specs->long_p = true;
11548 specs->locations[cdw_long] = loc;
11550 break;
11551 case RID_SHORT:
11552 dupe = specs->short_p;
11553 if (specs->long_p)
11554 error_at (loc,
11555 ("both %<long%> and %<short%> in "
11556 "declaration specifiers"));
11557 else if (specs->typespec_word == cts_auto_type)
11558 error_at (loc,
11559 ("both %<short%> and %<__auto_type%> in "
11560 "declaration specifiers"));
11561 else if (specs->typespec_word == cts_void)
11562 error_at (loc,
11563 ("both %<short%> and %<void%> in "
11564 "declaration specifiers"));
11565 else if (specs->typespec_word == cts_int_n)
11566 error_at (loc,
11567 ("both %<short%> and %<__int%d%> in "
11568 "declaration specifiers"),
11569 int_n_data[specs->int_n_idx].bitsize);
11570 else if (specs->typespec_word == cts_bool)
11571 error_at (loc,
11572 ("both %<short%> and %<_Bool%> in "
11573 "declaration specifiers"));
11574 else if (specs->typespec_word == cts_char)
11575 error_at (loc,
11576 ("both %<short%> and %<char%> in "
11577 "declaration specifiers"));
11578 else if (specs->typespec_word == cts_float)
11579 error_at (loc,
11580 ("both %<short%> and %<float%> in "
11581 "declaration specifiers"));
11582 else if (specs->typespec_word == cts_double)
11583 error_at (loc,
11584 ("both %<short%> and %<double%> in "
11585 "declaration specifiers"));
11586 else if (specs->typespec_word == cts_floatn_nx)
11587 error_at (loc,
11588 ("both %<short%> and %<_Float%d%s%> in "
11589 "declaration specifiers"),
11590 floatn_nx_types[specs->floatn_nx_idx].n,
11591 (floatn_nx_types[specs->floatn_nx_idx].extended
11592 ? "x"
11593 : ""));
11594 else if (specs->typespec_word == cts_dfloat32)
11595 error_at (loc,
11596 ("both %<short%> and %<_Decimal32%> in "
11597 "declaration specifiers"));
11598 else if (specs->typespec_word == cts_dfloat64)
11599 error_at (loc,
11600 ("both %<short%> and %<_Decimal64%> in "
11601 "declaration specifiers"));
11602 else if (specs->typespec_word == cts_dfloat128)
11603 error_at (loc,
11604 ("both %<short%> and %<_Decimal128%> in "
11605 "declaration specifiers"));
11606 else
11608 specs->short_p = true;
11609 specs->locations[cdw_short] = loc;
11611 break;
11612 case RID_SIGNED:
11613 dupe = specs->signed_p;
11614 if (specs->unsigned_p)
11615 error_at (loc,
11616 ("both %<signed%> and %<unsigned%> in "
11617 "declaration specifiers"));
11618 else if (specs->typespec_word == cts_auto_type)
11619 error_at (loc,
11620 ("both %<signed%> and %<__auto_type%> in "
11621 "declaration specifiers"));
11622 else if (specs->typespec_word == cts_void)
11623 error_at (loc,
11624 ("both %<signed%> and %<void%> in "
11625 "declaration specifiers"));
11626 else if (specs->typespec_word == cts_bool)
11627 error_at (loc,
11628 ("both %<signed%> and %<_Bool%> in "
11629 "declaration specifiers"));
11630 else if (specs->typespec_word == cts_float)
11631 error_at (loc,
11632 ("both %<signed%> and %<float%> in "
11633 "declaration specifiers"));
11634 else if (specs->typespec_word == cts_double)
11635 error_at (loc,
11636 ("both %<signed%> and %<double%> in "
11637 "declaration specifiers"));
11638 else if (specs->typespec_word == cts_floatn_nx)
11639 error_at (loc,
11640 ("both %<signed%> and %<_Float%d%s%> in "
11641 "declaration specifiers"),
11642 floatn_nx_types[specs->floatn_nx_idx].n,
11643 (floatn_nx_types[specs->floatn_nx_idx].extended
11644 ? "x"
11645 : ""));
11646 else if (specs->typespec_word == cts_dfloat32)
11647 error_at (loc,
11648 ("both %<signed%> and %<_Decimal32%> in "
11649 "declaration specifiers"));
11650 else if (specs->typespec_word == cts_dfloat64)
11651 error_at (loc,
11652 ("both %<signed%> and %<_Decimal64%> in "
11653 "declaration specifiers"));
11654 else if (specs->typespec_word == cts_dfloat128)
11655 error_at (loc,
11656 ("both %<signed%> and %<_Decimal128%> in "
11657 "declaration specifiers"));
11658 else
11660 specs->signed_p = true;
11661 specs->locations[cdw_signed] = loc;
11663 break;
11664 case RID_UNSIGNED:
11665 dupe = specs->unsigned_p;
11666 if (specs->signed_p)
11667 error_at (loc,
11668 ("both %<signed%> and %<unsigned%> in "
11669 "declaration specifiers"));
11670 else if (specs->typespec_word == cts_auto_type)
11671 error_at (loc,
11672 ("both %<unsigned%> and %<__auto_type%> in "
11673 "declaration specifiers"));
11674 else if (specs->typespec_word == cts_void)
11675 error_at (loc,
11676 ("both %<unsigned%> and %<void%> in "
11677 "declaration specifiers"));
11678 else if (specs->typespec_word == cts_bool)
11679 error_at (loc,
11680 ("both %<unsigned%> and %<_Bool%> in "
11681 "declaration specifiers"));
11682 else if (specs->typespec_word == cts_float)
11683 error_at (loc,
11684 ("both %<unsigned%> and %<float%> in "
11685 "declaration specifiers"));
11686 else if (specs->typespec_word == cts_double)
11687 error_at (loc,
11688 ("both %<unsigned%> and %<double%> in "
11689 "declaration specifiers"));
11690 else if (specs->typespec_word == cts_floatn_nx)
11691 error_at (loc,
11692 ("both %<unsigned%> and %<_Float%d%s%> in "
11693 "declaration specifiers"),
11694 floatn_nx_types[specs->floatn_nx_idx].n,
11695 (floatn_nx_types[specs->floatn_nx_idx].extended
11696 ? "x"
11697 : ""));
11698 else if (specs->typespec_word == cts_dfloat32)
11699 error_at (loc,
11700 ("both %<unsigned%> and %<_Decimal32%> in "
11701 "declaration specifiers"));
11702 else if (specs->typespec_word == cts_dfloat64)
11703 error_at (loc,
11704 ("both %<unsigned%> and %<_Decimal64%> in "
11705 "declaration specifiers"));
11706 else if (specs->typespec_word == cts_dfloat128)
11707 error_at (loc,
11708 ("both %<unsigned%> and %<_Decimal128%> in "
11709 "declaration specifiers"));
11710 else
11712 specs->unsigned_p = true;
11713 specs->locations[cdw_unsigned] = loc;
11715 break;
11716 case RID_COMPLEX:
11717 dupe = specs->complex_p;
11718 if (!in_system_header_at (loc))
11719 pedwarn_c90 (loc, OPT_Wpedantic,
11720 "ISO C90 does not support complex types");
11721 if (specs->typespec_word == cts_auto_type)
11722 error_at (loc,
11723 ("both %<complex%> and %<__auto_type%> in "
11724 "declaration specifiers"));
11725 else if (specs->typespec_word == cts_void)
11726 error_at (loc,
11727 ("both %<complex%> and %<void%> in "
11728 "declaration specifiers"));
11729 else if (specs->typespec_word == cts_bool)
11730 error_at (loc,
11731 ("both %<complex%> and %<_Bool%> in "
11732 "declaration specifiers"));
11733 else if (specs->typespec_word == cts_dfloat32)
11734 error_at (loc,
11735 ("both %<complex%> and %<_Decimal32%> in "
11736 "declaration specifiers"));
11737 else if (specs->typespec_word == cts_dfloat64)
11738 error_at (loc,
11739 ("both %<complex%> and %<_Decimal64%> in "
11740 "declaration specifiers"));
11741 else if (specs->typespec_word == cts_dfloat128)
11742 error_at (loc,
11743 ("both %<complex%> and %<_Decimal128%> in "
11744 "declaration specifiers"));
11745 else if (specs->typespec_word == cts_fract)
11746 error_at (loc,
11747 ("both %<complex%> and %<_Fract%> in "
11748 "declaration specifiers"));
11749 else if (specs->typespec_word == cts_accum)
11750 error_at (loc,
11751 ("both %<complex%> and %<_Accum%> in "
11752 "declaration specifiers"));
11753 else if (specs->saturating_p)
11754 error_at (loc,
11755 ("both %<complex%> and %<_Sat%> in "
11756 "declaration specifiers"));
11757 else
11759 specs->complex_p = true;
11760 specs->locations[cdw_complex] = loc;
11762 break;
11763 case RID_SAT:
11764 dupe = specs->saturating_p;
11765 pedwarn (loc, OPT_Wpedantic,
11766 "ISO C does not support saturating types");
11767 if (specs->typespec_word == cts_int_n)
11769 error_at (loc,
11770 ("both %<_Sat%> and %<__int%d%> in "
11771 "declaration specifiers"),
11772 int_n_data[specs->int_n_idx].bitsize);
11774 else if (specs->typespec_word == cts_auto_type)
11775 error_at (loc,
11776 ("both %<_Sat%> and %<__auto_type%> in "
11777 "declaration specifiers"));
11778 else if (specs->typespec_word == cts_void)
11779 error_at (loc,
11780 ("both %<_Sat%> and %<void%> in "
11781 "declaration specifiers"));
11782 else if (specs->typespec_word == cts_bool)
11783 error_at (loc,
11784 ("both %<_Sat%> and %<_Bool%> in "
11785 "declaration specifiers"));
11786 else if (specs->typespec_word == cts_char)
11787 error_at (loc,
11788 ("both %<_Sat%> and %<char%> in "
11789 "declaration specifiers"));
11790 else if (specs->typespec_word == cts_int)
11791 error_at (loc,
11792 ("both %<_Sat%> and %<int%> in "
11793 "declaration specifiers"));
11794 else if (specs->typespec_word == cts_float)
11795 error_at (loc,
11796 ("both %<_Sat%> and %<float%> in "
11797 "declaration specifiers"));
11798 else if (specs->typespec_word == cts_double)
11799 error_at (loc,
11800 ("both %<_Sat%> and %<double%> in "
11801 "declaration specifiers"));
11802 else if (specs->typespec_word == cts_floatn_nx)
11803 error_at (loc,
11804 ("both %<_Sat%> and %<_Float%d%s%> in "
11805 "declaration specifiers"),
11806 floatn_nx_types[specs->floatn_nx_idx].n,
11807 (floatn_nx_types[specs->floatn_nx_idx].extended
11808 ? "x"
11809 : ""));
11810 else if (specs->typespec_word == cts_dfloat32)
11811 error_at (loc,
11812 ("both %<_Sat%> and %<_Decimal32%> in "
11813 "declaration specifiers"));
11814 else if (specs->typespec_word == cts_dfloat64)
11815 error_at (loc,
11816 ("both %<_Sat%> and %<_Decimal64%> in "
11817 "declaration specifiers"));
11818 else if (specs->typespec_word == cts_dfloat128)
11819 error_at (loc,
11820 ("both %<_Sat%> and %<_Decimal128%> in "
11821 "declaration specifiers"));
11822 else if (specs->complex_p)
11823 error_at (loc,
11824 ("both %<_Sat%> and %<complex%> in "
11825 "declaration specifiers"));
11826 else
11828 specs->saturating_p = true;
11829 specs->locations[cdw_saturating] = loc;
11831 break;
11832 default:
11833 gcc_unreachable ();
11836 if (dupe)
11837 error_at (loc, "duplicate %qE", type);
11839 return specs;
11841 else
11843 /* "void", "_Bool", "char", "int", "float", "double",
11844 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11845 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11846 "__auto_type". */
11847 if (specs->typespec_word != cts_none)
11849 error_at (loc,
11850 "two or more data types in declaration specifiers");
11851 return specs;
11853 switch (i)
11855 case RID_AUTO_TYPE:
11856 if (specs->long_p)
11857 error_at (loc,
11858 ("both %<long%> and %<__auto_type%> in "
11859 "declaration specifiers"));
11860 else if (specs->short_p)
11861 error_at (loc,
11862 ("both %<short%> and %<__auto_type%> in "
11863 "declaration specifiers"));
11864 else if (specs->signed_p)
11865 error_at (loc,
11866 ("both %<signed%> and %<__auto_type%> in "
11867 "declaration specifiers"));
11868 else if (specs->unsigned_p)
11869 error_at (loc,
11870 ("both %<unsigned%> and %<__auto_type%> in "
11871 "declaration specifiers"));
11872 else if (specs->complex_p)
11873 error_at (loc,
11874 ("both %<complex%> and %<__auto_type%> in "
11875 "declaration specifiers"));
11876 else if (specs->saturating_p)
11877 error_at (loc,
11878 ("both %<_Sat%> and %<__auto_type%> in "
11879 "declaration specifiers"));
11880 else
11882 specs->typespec_word = cts_auto_type;
11883 specs->locations[cdw_typespec] = loc;
11885 return specs;
11886 case RID_INT_N_0:
11887 case RID_INT_N_1:
11888 case RID_INT_N_2:
11889 case RID_INT_N_3:
11890 specs->int_n_idx = i - RID_INT_N_0;
11891 if (!in_system_header_at (input_location)
11892 /* If the INT_N type ends in "__", and so is of the format
11893 "__intN__", don't pedwarn. */
11894 && (strncmp (IDENTIFIER_POINTER (type)
11895 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11896 pedwarn (loc, OPT_Wpedantic,
11897 "ISO C does not support %<__int%d%> types",
11898 int_n_data[specs->int_n_idx].bitsize);
11900 if (specs->long_p)
11901 error_at (loc,
11902 ("both %<__int%d%> and %<long%> in "
11903 "declaration specifiers"),
11904 int_n_data[specs->int_n_idx].bitsize);
11905 else if (specs->saturating_p)
11906 error_at (loc,
11907 ("both %<_Sat%> and %<__int%d%> in "
11908 "declaration specifiers"),
11909 int_n_data[specs->int_n_idx].bitsize);
11910 else if (specs->short_p)
11911 error_at (loc,
11912 ("both %<__int%d%> and %<short%> in "
11913 "declaration specifiers"),
11914 int_n_data[specs->int_n_idx].bitsize);
11915 else if (! int_n_enabled_p[specs->int_n_idx])
11917 specs->typespec_word = cts_int_n;
11918 error_at (loc,
11919 "%<__int%d%> is not supported on this target",
11920 int_n_data[specs->int_n_idx].bitsize);
11922 else
11924 specs->typespec_word = cts_int_n;
11925 specs->locations[cdw_typespec] = loc;
11927 return specs;
11928 case RID_VOID:
11929 if (specs->long_p)
11930 error_at (loc,
11931 ("both %<long%> and %<void%> in "
11932 "declaration specifiers"));
11933 else if (specs->short_p)
11934 error_at (loc,
11935 ("both %<short%> and %<void%> in "
11936 "declaration specifiers"));
11937 else if (specs->signed_p)
11938 error_at (loc,
11939 ("both %<signed%> and %<void%> in "
11940 "declaration specifiers"));
11941 else if (specs->unsigned_p)
11942 error_at (loc,
11943 ("both %<unsigned%> and %<void%> in "
11944 "declaration specifiers"));
11945 else if (specs->complex_p)
11946 error_at (loc,
11947 ("both %<complex%> and %<void%> in "
11948 "declaration specifiers"));
11949 else if (specs->saturating_p)
11950 error_at (loc,
11951 ("both %<_Sat%> and %<void%> in "
11952 "declaration specifiers"));
11953 else
11955 specs->typespec_word = cts_void;
11956 specs->locations[cdw_typespec] = loc;
11958 return specs;
11959 case RID_BOOL:
11960 if (!in_system_header_at (loc))
11961 pedwarn_c90 (loc, OPT_Wpedantic,
11962 "ISO C90 does not support boolean types");
11963 if (specs->long_p)
11964 error_at (loc,
11965 ("both %<long%> and %<_Bool%> in "
11966 "declaration specifiers"));
11967 else if (specs->short_p)
11968 error_at (loc,
11969 ("both %<short%> and %<_Bool%> in "
11970 "declaration specifiers"));
11971 else if (specs->signed_p)
11972 error_at (loc,
11973 ("both %<signed%> and %<_Bool%> in "
11974 "declaration specifiers"));
11975 else if (specs->unsigned_p)
11976 error_at (loc,
11977 ("both %<unsigned%> and %<_Bool%> in "
11978 "declaration specifiers"));
11979 else if (specs->complex_p)
11980 error_at (loc,
11981 ("both %<complex%> and %<_Bool%> in "
11982 "declaration specifiers"));
11983 else if (specs->saturating_p)
11984 error_at (loc,
11985 ("both %<_Sat%> and %<_Bool%> in "
11986 "declaration specifiers"));
11987 else
11989 specs->typespec_word = cts_bool;
11990 specs->locations[cdw_typespec] = loc;
11992 return specs;
11993 case RID_CHAR:
11994 if (specs->long_p)
11995 error_at (loc,
11996 ("both %<long%> and %<char%> in "
11997 "declaration specifiers"));
11998 else if (specs->short_p)
11999 error_at (loc,
12000 ("both %<short%> and %<char%> in "
12001 "declaration specifiers"));
12002 else if (specs->saturating_p)
12003 error_at (loc,
12004 ("both %<_Sat%> and %<char%> in "
12005 "declaration specifiers"));
12006 else
12008 specs->typespec_word = cts_char;
12009 specs->locations[cdw_typespec] = loc;
12011 return specs;
12012 case RID_INT:
12013 if (specs->saturating_p)
12014 error_at (loc,
12015 ("both %<_Sat%> and %<int%> in "
12016 "declaration specifiers"));
12017 else
12019 specs->typespec_word = cts_int;
12020 specs->locations[cdw_typespec] = loc;
12022 return specs;
12023 case RID_FLOAT:
12024 if (specs->long_p)
12025 error_at (loc,
12026 ("both %<long%> and %<float%> in "
12027 "declaration specifiers"));
12028 else if (specs->short_p)
12029 error_at (loc,
12030 ("both %<short%> and %<float%> in "
12031 "declaration specifiers"));
12032 else if (specs->signed_p)
12033 error_at (loc,
12034 ("both %<signed%> and %<float%> in "
12035 "declaration specifiers"));
12036 else if (specs->unsigned_p)
12037 error_at (loc,
12038 ("both %<unsigned%> and %<float%> in "
12039 "declaration specifiers"));
12040 else if (specs->saturating_p)
12041 error_at (loc,
12042 ("both %<_Sat%> and %<float%> in "
12043 "declaration specifiers"));
12044 else
12046 specs->typespec_word = cts_float;
12047 specs->locations[cdw_typespec] = loc;
12049 return specs;
12050 case RID_DOUBLE:
12051 if (specs->long_long_p)
12052 error_at (loc,
12053 ("both %<long long%> and %<double%> in "
12054 "declaration specifiers"));
12055 else if (specs->short_p)
12056 error_at (loc,
12057 ("both %<short%> and %<double%> in "
12058 "declaration specifiers"));
12059 else if (specs->signed_p)
12060 error_at (loc,
12061 ("both %<signed%> and %<double%> in "
12062 "declaration specifiers"));
12063 else if (specs->unsigned_p)
12064 error_at (loc,
12065 ("both %<unsigned%> and %<double%> in "
12066 "declaration specifiers"));
12067 else if (specs->saturating_p)
12068 error_at (loc,
12069 ("both %<_Sat%> and %<double%> in "
12070 "declaration specifiers"));
12071 else
12073 specs->typespec_word = cts_double;
12074 specs->locations[cdw_typespec] = loc;
12076 return specs;
12077 CASE_RID_FLOATN_NX:
12078 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12079 if (!in_system_header_at (input_location))
12080 pedwarn (loc, OPT_Wpedantic,
12081 "ISO C does not support the %<_Float%d%s%> type",
12082 floatn_nx_types[specs->floatn_nx_idx].n,
12083 (floatn_nx_types[specs->floatn_nx_idx].extended
12084 ? "x"
12085 : ""));
12087 if (specs->long_p)
12088 error_at (loc,
12089 ("both %<long%> and %<_Float%d%s%> in "
12090 "declaration specifiers"),
12091 floatn_nx_types[specs->floatn_nx_idx].n,
12092 (floatn_nx_types[specs->floatn_nx_idx].extended
12093 ? "x"
12094 : ""));
12095 else if (specs->short_p)
12096 error_at (loc,
12097 ("both %<short%> and %<_Float%d%s%> in "
12098 "declaration specifiers"),
12099 floatn_nx_types[specs->floatn_nx_idx].n,
12100 (floatn_nx_types[specs->floatn_nx_idx].extended
12101 ? "x"
12102 : ""));
12103 else if (specs->signed_p)
12104 error_at (loc,
12105 ("both %<signed%> and %<_Float%d%s%> in "
12106 "declaration specifiers"),
12107 floatn_nx_types[specs->floatn_nx_idx].n,
12108 (floatn_nx_types[specs->floatn_nx_idx].extended
12109 ? "x"
12110 : ""));
12111 else if (specs->unsigned_p)
12112 error_at (loc,
12113 ("both %<unsigned%> and %<_Float%d%s%> in "
12114 "declaration specifiers"),
12115 floatn_nx_types[specs->floatn_nx_idx].n,
12116 (floatn_nx_types[specs->floatn_nx_idx].extended
12117 ? "x"
12118 : ""));
12119 else if (specs->saturating_p)
12120 error_at (loc,
12121 ("both %<_Sat%> and %<_Float%d%s%> in "
12122 "declaration specifiers"),
12123 floatn_nx_types[specs->floatn_nx_idx].n,
12124 (floatn_nx_types[specs->floatn_nx_idx].extended
12125 ? "x"
12126 : ""));
12127 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12129 specs->typespec_word = cts_floatn_nx;
12130 error_at (loc,
12131 "%<_Float%d%s%> is not supported on this target",
12132 floatn_nx_types[specs->floatn_nx_idx].n,
12133 (floatn_nx_types[specs->floatn_nx_idx].extended
12134 ? "x"
12135 : ""));
12137 else
12139 specs->typespec_word = cts_floatn_nx;
12140 specs->locations[cdw_typespec] = loc;
12142 return specs;
12143 case RID_DFLOAT32:
12144 case RID_DFLOAT64:
12145 case RID_DFLOAT128:
12147 const char *str;
12148 if (i == RID_DFLOAT32)
12149 str = "_Decimal32";
12150 else if (i == RID_DFLOAT64)
12151 str = "_Decimal64";
12152 else
12153 str = "_Decimal128";
12154 if (specs->long_long_p)
12155 error_at (loc,
12156 ("both %<long long%> and %qs in "
12157 "declaration specifiers"),
12158 str);
12159 if (specs->long_p)
12160 error_at (loc,
12161 ("both %<long%> and %qs in "
12162 "declaration specifiers"),
12163 str);
12164 else if (specs->short_p)
12165 error_at (loc,
12166 ("both %<short%> and %qs in "
12167 "declaration specifiers"),
12168 str);
12169 else if (specs->signed_p)
12170 error_at (loc,
12171 ("both %<signed%> and %qs in "
12172 "declaration specifiers"),
12173 str);
12174 else if (specs->unsigned_p)
12175 error_at (loc,
12176 ("both %<unsigned%> and %qs in "
12177 "declaration specifiers"),
12178 str);
12179 else if (specs->complex_p)
12180 error_at (loc,
12181 ("both %<complex%> and %qs in "
12182 "declaration specifiers"),
12183 str);
12184 else if (specs->saturating_p)
12185 error_at (loc,
12186 ("both %<_Sat%> and %qs in "
12187 "declaration specifiers"),
12188 str);
12189 else if (i == RID_DFLOAT32)
12190 specs->typespec_word = cts_dfloat32;
12191 else if (i == RID_DFLOAT64)
12192 specs->typespec_word = cts_dfloat64;
12193 else
12194 specs->typespec_word = cts_dfloat128;
12195 specs->locations[cdw_typespec] = loc;
12197 if (!targetm.decimal_float_supported_p ())
12198 error_at (loc,
12199 ("decimal floating-point not supported "
12200 "for this target"));
12201 pedwarn_c11 (loc, OPT_Wpedantic,
12202 "ISO C does not support decimal floating-point "
12203 "before C2X");
12204 return specs;
12205 case RID_FRACT:
12206 case RID_ACCUM:
12208 const char *str;
12209 if (i == RID_FRACT)
12210 str = "_Fract";
12211 else
12212 str = "_Accum";
12213 if (specs->complex_p)
12214 error_at (loc,
12215 ("both %<complex%> and %qs in "
12216 "declaration specifiers"),
12217 str);
12218 else if (i == RID_FRACT)
12219 specs->typespec_word = cts_fract;
12220 else
12221 specs->typespec_word = cts_accum;
12222 specs->locations[cdw_typespec] = loc;
12224 if (!targetm.fixed_point_supported_p ())
12225 error_at (loc,
12226 "fixed-point types not supported for this target");
12227 pedwarn (loc, OPT_Wpedantic,
12228 "ISO C does not support fixed-point types");
12229 return specs;
12230 default:
12231 /* ObjC reserved word "id", handled below. */
12232 break;
12237 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12238 form of ObjC type, cases such as "int" and "long" being handled
12239 above), a TYPE (struct, union, enum and typeof specifiers) or an
12240 ERROR_MARK. In none of these cases may there have previously
12241 been any type specifiers. */
12242 if (specs->type || specs->typespec_word != cts_none
12243 || specs->long_p || specs->short_p || specs->signed_p
12244 || specs->unsigned_p || specs->complex_p)
12245 error_at (loc, "two or more data types in declaration specifiers");
12246 else if (TREE_CODE (type) == TYPE_DECL)
12248 specs->type = TREE_TYPE (type);
12249 if (TREE_TYPE (type) != error_mark_node)
12251 specs->decl_attr = DECL_ATTRIBUTES (type);
12252 specs->typedef_p = true;
12253 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12254 specs->locations[cdw_typedef] = loc;
12256 /* If this typedef name is defined in a struct, then a C++
12257 lookup would return a different value. */
12258 if (warn_cxx_compat
12259 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12260 warning_at (loc, OPT_Wc___compat,
12261 "C++ lookup of %qD would return a field, not a type",
12262 type);
12264 /* If we are parsing a struct, record that a struct field
12265 used a typedef. */
12266 if (warn_cxx_compat && struct_parse_info != NULL)
12267 struct_parse_info->typedefs_seen.safe_push (type);
12270 else if (TREE_CODE (type) == IDENTIFIER_NODE)
12272 tree t = lookup_name (type);
12273 if (!t || TREE_CODE (t) != TYPE_DECL)
12274 error_at (loc, "%qE fails to be a typedef or built in type", type);
12275 else if (TREE_TYPE (t) == error_mark_node)
12277 else
12279 specs->type = TREE_TYPE (t);
12280 specs->locations[cdw_typespec] = loc;
12283 else
12285 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
12287 specs->typedef_p = true;
12288 specs->locations[cdw_typedef] = loc;
12289 if (spec.expr)
12291 if (specs->expr)
12292 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12293 specs->expr, spec.expr);
12294 else
12295 specs->expr = spec.expr;
12296 specs->expr_const_operands &= spec.expr_const_operands;
12299 specs->type = type;
12300 if (spec.has_enum_type_specifier
12301 && spec.kind != ctsk_tagdef)
12302 specs->enum_type_specifier_ref_p = true;
12305 return specs;
12308 /* Add the storage class specifier or function specifier SCSPEC to the
12309 declaration specifiers SPECS, returning SPECS. */
12311 struct c_declspecs *
12312 declspecs_add_scspec (location_t loc,
12313 struct c_declspecs *specs,
12314 tree scspec)
12316 enum rid i;
12317 enum c_storage_class n = csc_none;
12318 bool dupe = false;
12319 specs->declspecs_seen_p = true;
12320 specs->non_std_attrs_seen_p = true;
12321 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12322 && C_IS_RESERVED_WORD (scspec));
12323 i = C_RID_CODE (scspec);
12324 if (specs->non_sc_seen_p)
12325 warning (OPT_Wold_style_declaration,
12326 "%qE is not at beginning of declaration", scspec);
12327 switch (i)
12329 case RID_INLINE:
12330 /* C99 permits duplicate inline. Although of doubtful utility,
12331 it seems simplest to permit it in gnu89 mode as well, as
12332 there is also little utility in maintaining this as a
12333 difference between gnu89 and C99 inline. */
12334 dupe = false;
12335 specs->inline_p = true;
12336 specs->locations[cdw_inline] = loc;
12337 break;
12338 case RID_NORETURN:
12339 /* Duplicate _Noreturn is permitted. */
12340 dupe = false;
12341 specs->noreturn_p = true;
12342 specs->locations[cdw_noreturn] = loc;
12343 break;
12344 case RID_THREAD:
12345 dupe = specs->thread_p;
12346 if (specs->storage_class == csc_auto)
12347 error ("%qE used with %<auto%>", scspec);
12348 else if (specs->storage_class == csc_register)
12349 error ("%qE used with %<register%>", scspec);
12350 else if (specs->storage_class == csc_typedef)
12351 error ("%qE used with %<typedef%>", scspec);
12352 else if (specs->constexpr_p)
12353 error ("%qE used with %<constexpr%>", scspec);
12354 else
12356 specs->thread_p = true;
12357 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12358 "__thread") == 0);
12359 /* A diagnostic is not required for the use of this
12360 identifier in the implementation namespace; only diagnose
12361 it for the C11 spelling because of existing code using
12362 the other spelling. */
12363 if (!specs->thread_gnu_p)
12365 if (flag_isoc99)
12366 pedwarn_c99 (loc, OPT_Wpedantic,
12367 "ISO C99 does not support %qE", scspec);
12368 else
12369 pedwarn_c99 (loc, OPT_Wpedantic,
12370 "ISO C90 does not support %qE", scspec);
12372 specs->locations[cdw_thread] = loc;
12374 break;
12375 case RID_AUTO:
12376 if (flag_isoc2x
12377 && specs->typespec_kind == ctsk_none
12378 && specs->storage_class != csc_typedef)
12380 /* "auto" potentially used for type deduction. */
12381 if (specs->c2x_auto_p)
12382 error ("duplicate %qE", scspec);
12383 specs->c2x_auto_p = true;
12384 return specs;
12386 n = csc_auto;
12387 /* auto may only be used with another storage class specifier,
12388 such as constexpr, if the type is inferred. */
12389 if (specs->constexpr_p)
12390 error ("%qE used with %<constexpr%>", scspec);
12391 break;
12392 case RID_EXTERN:
12393 n = csc_extern;
12394 /* Diagnose "__thread extern". */
12395 if (specs->thread_p && specs->thread_gnu_p)
12396 error ("%<__thread%> before %<extern%>");
12397 break;
12398 case RID_REGISTER:
12399 n = csc_register;
12400 break;
12401 case RID_STATIC:
12402 n = csc_static;
12403 /* Diagnose "__thread static". */
12404 if (specs->thread_p && specs->thread_gnu_p)
12405 error ("%<__thread%> before %<static%>");
12406 break;
12407 case RID_TYPEDEF:
12408 n = csc_typedef;
12409 if (specs->c2x_auto_p)
12411 error ("%<typedef%> used with %<auto%>");
12412 specs->c2x_auto_p = false;
12414 break;
12415 case RID_CONSTEXPR:
12416 dupe = specs->constexpr_p;
12417 if (specs->storage_class == csc_extern)
12418 error ("%qE used with %<extern%>", scspec);
12419 else if (specs->storage_class == csc_typedef)
12420 error ("%qE used with %<typedef%>", scspec);
12421 else if (specs->storage_class == csc_auto)
12422 /* auto may only be used with another storage class specifier,
12423 such as constexpr, if the type is inferred. */
12424 error ("%qE used with %<auto%>", scspec);
12425 else if (specs->thread_p)
12426 error ("%qE used with %qs", scspec,
12427 specs->thread_gnu_p ? "__thread" : "_Thread_local");
12428 else
12429 specs->constexpr_p = true;
12430 break;
12431 default:
12432 gcc_unreachable ();
12434 if (n != csc_none && n == specs->storage_class)
12435 dupe = true;
12436 if (dupe)
12438 if (i == RID_THREAD)
12439 error ("duplicate %<_Thread_local%> or %<__thread%>");
12440 else
12441 error ("duplicate %qE", scspec);
12443 if (n != csc_none)
12445 if (specs->storage_class != csc_none && n != specs->storage_class)
12447 error ("multiple storage classes in declaration specifiers");
12449 else
12451 specs->storage_class = n;
12452 specs->locations[cdw_storage_class] = loc;
12453 if (n != csc_extern && n != csc_static && specs->thread_p)
12455 error ("%qs used with %qE",
12456 specs->thread_gnu_p ? "__thread" : "_Thread_local",
12457 scspec);
12458 specs->thread_p = false;
12460 if (n != csc_auto && n != csc_register && n != csc_static
12461 && specs->constexpr_p)
12463 error ("%<constexpr%> used with %qE", scspec);
12464 specs->constexpr_p = false;
12468 return specs;
12471 /* Add the attributes ATTRS to the declaration specifiers SPECS,
12472 returning SPECS. */
12474 struct c_declspecs *
12475 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12477 specs->attrs = chainon (attrs, specs->attrs);
12478 specs->locations[cdw_attributes] = loc;
12479 specs->declspecs_seen_p = true;
12480 /* In the case of standard attributes at the start of the
12481 declaration, the caller will reset this. */
12482 specs->non_std_attrs_seen_p = true;
12483 return specs;
12486 /* Add an _Alignas specifier (expression ALIGN, or type whose
12487 alignment is ALIGN) to the declaration specifiers SPECS, returning
12488 SPECS. */
12489 struct c_declspecs *
12490 declspecs_add_alignas (location_t loc,
12491 struct c_declspecs *specs, tree align)
12493 specs->alignas_p = true;
12494 specs->locations[cdw_alignas] = loc;
12495 if (align == error_mark_node)
12496 return specs;
12498 /* Only accept the alignment if it's valid and greater than
12499 the current one. Zero is invalid but by C11 required to
12500 be silently ignored. */
12501 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12502 if (align_log > specs->align_log)
12503 specs->align_log = align_log;
12504 return specs;
12507 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12508 specifiers with any other type specifier to determine the resulting
12509 type. This is where ISO C checks on complex types are made, since
12510 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12511 double". Also apply postfix standard attributes to modify the type. */
12513 struct c_declspecs *
12514 finish_declspecs (struct c_declspecs *specs)
12516 /* If a type was specified as a whole, we have no modifiers and are
12517 done. */
12518 if (specs->type != NULL_TREE)
12520 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12521 && !specs->signed_p && !specs->unsigned_p
12522 && !specs->complex_p && !specs->c2x_auto_p);
12524 /* Set a dummy type. */
12525 if (TREE_CODE (specs->type) == ERROR_MARK)
12526 specs->type = integer_type_node;
12527 goto handle_postfix_attrs;
12530 /* If none of "void", "_Bool", "char", "int", "float" or "double"
12531 has been specified, treat it as "int" unless "_Complex" is
12532 present and there are no other specifiers. If we just have
12533 "_Complex", it is equivalent to "_Complex double", but e.g.
12534 "_Complex short" is equivalent to "_Complex short int". */
12535 if (specs->typespec_word == cts_none)
12537 if (specs->saturating_p)
12539 error_at (specs->locations[cdw_saturating],
12540 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12541 if (!targetm.fixed_point_supported_p ())
12542 error_at (specs->locations[cdw_saturating],
12543 "fixed-point types not supported for this target");
12544 specs->typespec_word = cts_fract;
12546 else if (specs->long_p || specs->short_p
12547 || specs->signed_p || specs->unsigned_p)
12549 specs->typespec_word = cts_int;
12551 else if (specs->complex_p)
12553 specs->typespec_word = cts_double;
12554 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12555 "ISO C does not support plain %<complex%> meaning "
12556 "%<double complex%>");
12558 else if (specs->c2x_auto_p)
12560 /* Type to be filled in later, including applying postfix
12561 attributes. This warning only actually appears for
12562 -Wc11-c2x-compat in C2X mode; in older modes, there may
12563 be a warning or pedwarn for implicit "int" instead, or
12564 other errors for use of auto at file scope. */
12565 pedwarn_c11 (input_location, OPT_Wpedantic,
12566 "ISO C does not support %<auto%> type deduction "
12567 "before C2X");
12568 return specs;
12570 else
12572 specs->typespec_word = cts_int;
12573 specs->default_int_p = true;
12574 /* We don't diagnose this here because grokdeclarator will
12575 give more specific diagnostics according to whether it is
12576 a function definition. */
12580 /* If "signed" was specified, record this to distinguish "int" and
12581 "signed int" in the case of a bit-field with
12582 -funsigned-bitfields. */
12583 specs->explicit_signed_p = specs->signed_p;
12585 /* Now compute the actual type. */
12586 gcc_assert (!specs->c2x_auto_p);
12587 switch (specs->typespec_word)
12589 case cts_auto_type:
12590 gcc_assert (!specs->long_p && !specs->short_p
12591 && !specs->signed_p && !specs->unsigned_p
12592 && !specs->complex_p);
12593 /* Type to be filled in later. */
12594 if (specs->postfix_attrs)
12595 error ("%<__auto_type%> followed by %<[[]]%> attributes");
12596 break;
12597 case cts_void:
12598 gcc_assert (!specs->long_p && !specs->short_p
12599 && !specs->signed_p && !specs->unsigned_p
12600 && !specs->complex_p);
12601 specs->type = void_type_node;
12602 break;
12603 case cts_bool:
12604 gcc_assert (!specs->long_p && !specs->short_p
12605 && !specs->signed_p && !specs->unsigned_p
12606 && !specs->complex_p);
12607 specs->type = boolean_type_node;
12608 break;
12609 case cts_char:
12610 gcc_assert (!specs->long_p && !specs->short_p);
12611 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12612 if (specs->signed_p)
12613 specs->type = signed_char_type_node;
12614 else if (specs->unsigned_p)
12615 specs->type = unsigned_char_type_node;
12616 else
12617 specs->type = char_type_node;
12618 if (specs->complex_p)
12620 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12621 "ISO C does not support complex integer types");
12622 specs->type = build_complex_type (specs->type);
12624 break;
12625 case cts_int_n:
12626 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12627 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12628 if (! int_n_enabled_p[specs->int_n_idx])
12629 specs->type = integer_type_node;
12630 else
12631 specs->type = (specs->unsigned_p
12632 ? int_n_trees[specs->int_n_idx].unsigned_type
12633 : int_n_trees[specs->int_n_idx].signed_type);
12634 if (specs->complex_p)
12636 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12637 "ISO C does not support complex integer types");
12638 specs->type = build_complex_type (specs->type);
12640 break;
12641 case cts_int:
12642 gcc_assert (!(specs->long_p && specs->short_p));
12643 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12644 if (specs->long_long_p)
12645 specs->type = (specs->unsigned_p
12646 ? long_long_unsigned_type_node
12647 : long_long_integer_type_node);
12648 else if (specs->long_p)
12649 specs->type = (specs->unsigned_p
12650 ? long_unsigned_type_node
12651 : long_integer_type_node);
12652 else if (specs->short_p)
12653 specs->type = (specs->unsigned_p
12654 ? short_unsigned_type_node
12655 : short_integer_type_node);
12656 else
12657 specs->type = (specs->unsigned_p
12658 ? unsigned_type_node
12659 : integer_type_node);
12660 if (specs->complex_p)
12662 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12663 "ISO C does not support complex integer types");
12664 specs->type = build_complex_type (specs->type);
12666 break;
12667 case cts_float:
12668 gcc_assert (!specs->long_p && !specs->short_p
12669 && !specs->signed_p && !specs->unsigned_p);
12670 specs->type = (specs->complex_p
12671 ? complex_float_type_node
12672 : float_type_node);
12673 break;
12674 case cts_double:
12675 gcc_assert (!specs->long_long_p && !specs->short_p
12676 && !specs->signed_p && !specs->unsigned_p);
12677 if (specs->long_p)
12679 specs->type = (specs->complex_p
12680 ? complex_long_double_type_node
12681 : long_double_type_node);
12683 else
12685 specs->type = (specs->complex_p
12686 ? complex_double_type_node
12687 : double_type_node);
12689 break;
12690 case cts_floatn_nx:
12691 gcc_assert (!specs->long_p && !specs->short_p
12692 && !specs->signed_p && !specs->unsigned_p);
12693 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12694 specs->type = integer_type_node;
12695 else if (specs->complex_p)
12696 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12697 else
12698 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12699 break;
12700 case cts_dfloat32:
12701 case cts_dfloat64:
12702 case cts_dfloat128:
12703 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12704 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12705 if (!targetm.decimal_float_supported_p ())
12706 specs->type = integer_type_node;
12707 else if (specs->typespec_word == cts_dfloat32)
12708 specs->type = dfloat32_type_node;
12709 else if (specs->typespec_word == cts_dfloat64)
12710 specs->type = dfloat64_type_node;
12711 else
12712 specs->type = dfloat128_type_node;
12713 break;
12714 case cts_fract:
12715 gcc_assert (!specs->complex_p);
12716 if (!targetm.fixed_point_supported_p ())
12717 specs->type = integer_type_node;
12718 else if (specs->saturating_p)
12720 if (specs->long_long_p)
12721 specs->type = specs->unsigned_p
12722 ? sat_unsigned_long_long_fract_type_node
12723 : sat_long_long_fract_type_node;
12724 else if (specs->long_p)
12725 specs->type = specs->unsigned_p
12726 ? sat_unsigned_long_fract_type_node
12727 : sat_long_fract_type_node;
12728 else if (specs->short_p)
12729 specs->type = specs->unsigned_p
12730 ? sat_unsigned_short_fract_type_node
12731 : sat_short_fract_type_node;
12732 else
12733 specs->type = specs->unsigned_p
12734 ? sat_unsigned_fract_type_node
12735 : sat_fract_type_node;
12737 else
12739 if (specs->long_long_p)
12740 specs->type = specs->unsigned_p
12741 ? unsigned_long_long_fract_type_node
12742 : long_long_fract_type_node;
12743 else if (specs->long_p)
12744 specs->type = specs->unsigned_p
12745 ? unsigned_long_fract_type_node
12746 : long_fract_type_node;
12747 else if (specs->short_p)
12748 specs->type = specs->unsigned_p
12749 ? unsigned_short_fract_type_node
12750 : short_fract_type_node;
12751 else
12752 specs->type = specs->unsigned_p
12753 ? unsigned_fract_type_node
12754 : fract_type_node;
12756 break;
12757 case cts_accum:
12758 gcc_assert (!specs->complex_p);
12759 if (!targetm.fixed_point_supported_p ())
12760 specs->type = integer_type_node;
12761 else if (specs->saturating_p)
12763 if (specs->long_long_p)
12764 specs->type = specs->unsigned_p
12765 ? sat_unsigned_long_long_accum_type_node
12766 : sat_long_long_accum_type_node;
12767 else if (specs->long_p)
12768 specs->type = specs->unsigned_p
12769 ? sat_unsigned_long_accum_type_node
12770 : sat_long_accum_type_node;
12771 else if (specs->short_p)
12772 specs->type = specs->unsigned_p
12773 ? sat_unsigned_short_accum_type_node
12774 : sat_short_accum_type_node;
12775 else
12776 specs->type = specs->unsigned_p
12777 ? sat_unsigned_accum_type_node
12778 : sat_accum_type_node;
12780 else
12782 if (specs->long_long_p)
12783 specs->type = specs->unsigned_p
12784 ? unsigned_long_long_accum_type_node
12785 : long_long_accum_type_node;
12786 else if (specs->long_p)
12787 specs->type = specs->unsigned_p
12788 ? unsigned_long_accum_type_node
12789 : long_accum_type_node;
12790 else if (specs->short_p)
12791 specs->type = specs->unsigned_p
12792 ? unsigned_short_accum_type_node
12793 : short_accum_type_node;
12794 else
12795 specs->type = specs->unsigned_p
12796 ? unsigned_accum_type_node
12797 : accum_type_node;
12799 break;
12800 default:
12801 gcc_unreachable ();
12803 handle_postfix_attrs:
12804 if (specs->type != NULL)
12806 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12807 decl_attributes (&specs->type, specs->postfix_attrs, 0);
12808 specs->postfix_attrs = NULL_TREE;
12811 return specs;
12814 /* Perform final processing on one file scope's declarations (or the
12815 external scope's declarations), GLOBALS. */
12817 static void
12818 c_write_global_declarations_1 (tree globals)
12820 tree decl;
12821 bool reconsider;
12823 /* Process the decls in the order they were written. */
12824 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12826 /* Check for used but undefined static functions using the C
12827 standard's definition of "used", and set TREE_NO_WARNING so
12828 that check_global_declaration doesn't repeat the check. */
12829 if (TREE_CODE (decl) == FUNCTION_DECL
12830 && DECL_INITIAL (decl) == NULL_TREE
12831 && DECL_EXTERNAL (decl)
12832 && !TREE_PUBLIC (decl))
12834 if (C_DECL_USED (decl))
12836 /* TODO: Add OPT_Wundefined-inline. */
12837 if (pedwarn (input_location, 0, "%q+F used but never defined",
12838 decl))
12839 suppress_warning (decl /* OPT_Wundefined-inline. */);
12841 /* For -Wunused-function warn about unused static prototypes. */
12842 else if (warn_unused_function
12843 && ! DECL_ARTIFICIAL (decl)
12844 && ! warning_suppressed_p (decl, OPT_Wunused_function))
12846 if (warning (OPT_Wunused_function,
12847 "%q+F declared %<static%> but never defined",
12848 decl))
12849 suppress_warning (decl, OPT_Wunused_function);
12853 wrapup_global_declaration_1 (decl);
12858 reconsider = false;
12859 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12860 reconsider |= wrapup_global_declaration_2 (decl);
12862 while (reconsider);
12865 /* Preserve the external declarations scope across a garbage collect. */
12866 static GTY(()) tree ext_block;
12868 /* Collect all references relevant to SOURCE_FILE. */
12870 static void
12871 collect_all_refs (const char *source_file)
12873 tree t;
12874 unsigned i;
12876 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12877 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12879 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12882 /* Collect source file references at global level. */
12884 static void
12885 collect_source_refs (void)
12887 tree t;
12888 tree decls;
12889 tree decl;
12890 unsigned i;
12892 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12894 decls = DECL_INITIAL (t);
12895 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12896 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12897 collect_source_ref (DECL_SOURCE_FILE (decl));
12900 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12901 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12902 collect_source_ref (DECL_SOURCE_FILE (decl));
12905 /* Free attribute access data that are not needed by the middle end. */
12907 static void
12908 free_attr_access_data ()
12910 struct cgraph_node *n;
12912 /* Iterate over all functions declared in the translation unit. */
12913 FOR_EACH_FUNCTION (n)
12915 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12916 if (tree attrs = DECL_ATTRIBUTES (parm))
12917 attr_access::free_lang_data (attrs);
12919 tree fntype = TREE_TYPE (n->decl);
12920 if (!fntype || fntype == error_mark_node)
12921 continue;
12922 tree attrs = TYPE_ATTRIBUTES (fntype);
12923 if (!attrs)
12924 continue;
12926 attr_access::free_lang_data (attrs);
12930 /* Perform any final parser cleanups and generate initial debugging
12931 information. */
12933 void
12934 c_parse_final_cleanups (void)
12936 tree t;
12937 unsigned i;
12939 /* We don't want to do this if generating a PCH. */
12940 if (pch_file)
12941 return;
12943 timevar_stop (TV_PHASE_PARSING);
12944 timevar_start (TV_PHASE_DEFERRED);
12946 /* Do the Objective-C stuff. This is where all the Objective-C
12947 module stuff gets generated (symtab, class/protocol/selector
12948 lists etc). */
12949 if (c_dialect_objc ())
12950 objc_write_global_declarations ();
12952 /* Close the external scope. */
12953 ext_block = pop_scope ();
12954 external_scope = 0;
12955 gcc_assert (!current_scope);
12957 /* Handle -fdump-ada-spec[-slim]. */
12958 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12960 /* Build a table of files to generate specs for */
12961 collect_source_ref (main_input_filename);
12962 if (!flag_dump_ada_spec_slim)
12963 collect_source_refs ();
12965 dump_ada_specs (collect_all_refs, NULL);
12968 /* Process all file scopes in this compilation, and the external_scope,
12969 through wrapup_global_declarations. */
12970 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12971 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12972 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12974 if (!in_lto_p)
12975 free_attr_access_data ();
12977 timevar_stop (TV_PHASE_DEFERRED);
12978 timevar_start (TV_PHASE_PARSING);
12980 ext_block = NULL;
12983 /* Register reserved keyword WORD as qualifier for address space AS. */
12985 void
12986 c_register_addr_space (const char *word, addr_space_t as)
12988 int rid = RID_FIRST_ADDR_SPACE + as;
12989 tree id;
12991 /* Address space qualifiers are only supported
12992 in C with GNU extensions enabled. */
12993 if (c_dialect_objc () || flag_no_asm)
12994 return;
12996 id = get_identifier (word);
12997 C_SET_RID_CODE (id, rid);
12998 C_IS_RESERVED_WORD (id) = 1;
12999 ridpointers [rid] = id;
13002 /* Return identifier to look up for omp declare reduction. */
13004 tree
13005 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13007 const char *p = NULL;
13008 switch (reduction_code)
13010 case PLUS_EXPR: p = "+"; break;
13011 case MULT_EXPR: p = "*"; break;
13012 case MINUS_EXPR: p = "-"; break;
13013 case BIT_AND_EXPR: p = "&"; break;
13014 case BIT_XOR_EXPR: p = "^"; break;
13015 case BIT_IOR_EXPR: p = "|"; break;
13016 case TRUTH_ANDIF_EXPR: p = "&&"; break;
13017 case TRUTH_ORIF_EXPR: p = "||"; break;
13018 case MIN_EXPR: p = "min"; break;
13019 case MAX_EXPR: p = "max"; break;
13020 default:
13021 break;
13024 if (p == NULL)
13026 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13027 return error_mark_node;
13028 p = IDENTIFIER_POINTER (reduction_id);
13031 const char prefix[] = "omp declare reduction ";
13032 size_t lenp = sizeof (prefix);
13033 size_t len = strlen (p);
13034 char *name = XALLOCAVEC (char, lenp + len);
13035 memcpy (name, prefix, lenp - 1);
13036 memcpy (name + lenp - 1, p, len + 1);
13037 return get_identifier (name);
13040 /* Lookup REDUCTION_ID in the current scope, or create an artificial
13041 VAR_DECL, bind it into the current scope and return it. */
13043 tree
13044 c_omp_reduction_decl (tree reduction_id)
13046 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13047 if (b != NULL && B_IN_CURRENT_SCOPE (b))
13048 return b->decl;
13050 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13051 reduction_id, integer_type_node);
13052 DECL_ARTIFICIAL (decl) = 1;
13053 DECL_EXTERNAL (decl) = 1;
13054 TREE_STATIC (decl) = 1;
13055 TREE_PUBLIC (decl) = 0;
13056 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13057 return decl;
13060 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13062 tree
13063 c_omp_reduction_lookup (tree reduction_id, tree type)
13065 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13066 while (b)
13068 tree t;
13069 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13070 if (comptypes (TREE_PURPOSE (t), type))
13071 return TREE_VALUE (t);
13072 b = b->shadowed;
13074 return error_mark_node;
13077 /* Helper function called via walk_tree, to diagnose invalid
13078 #pragma omp declare reduction combiners or initializers. */
13080 tree
13081 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13083 tree *vars = (tree *) data;
13084 if (SSA_VAR_P (*tp)
13085 && !DECL_ARTIFICIAL (*tp)
13086 && *tp != vars[0]
13087 && *tp != vars[1])
13089 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13090 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13091 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13092 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13093 *tp);
13094 else
13095 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13096 "to variable %qD which is not %<omp_priv%> nor "
13097 "%<omp_orig%>",
13098 *tp);
13099 return *tp;
13101 return NULL_TREE;
13105 bool
13106 c_check_in_current_scope (tree decl)
13108 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13109 return b != NULL && B_IN_CURRENT_SCOPE (b);
13112 #include "gt-c-c-decl.h"