d: Add test for PR d/108167 to the testsuite [PR108167]
[official-gcc.git] / gcc / c / c-decl.cc
blob91599658be9650a887e88774a5dc0c5502bb2301
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))
1313 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1314 else if (DECL_CONTEXT (p) == current_function_decl)
1315 warning_at (DECL_SOURCE_LOCATION (p),
1316 OPT_Wunused_but_set_variable,
1317 "variable %qD set but not used", p);
1320 if (b->inner_comp)
1322 error ("type of array %q+D completed incompatibly with"
1323 " implicit initialization", p);
1326 /* Fall through. */
1327 case TYPE_DECL:
1328 case CONST_DECL:
1329 common_symbol:
1330 /* All of these go in BLOCK_VARS, but only if this is the
1331 binding in the home scope. */
1332 if (!b->nested)
1334 DECL_CHAIN (p) = BLOCK_VARS (block);
1335 BLOCK_VARS (block) = p;
1337 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1339 /* For block local externs add a special
1340 DECL_EXTERNAL decl for debug info generation. */
1341 tree extp = copy_node (p);
1343 DECL_EXTERNAL (extp) = 1;
1344 TREE_STATIC (extp) = 0;
1345 TREE_PUBLIC (extp) = 1;
1346 DECL_INITIAL (extp) = NULL_TREE;
1347 DECL_LANG_SPECIFIC (extp) = NULL;
1348 DECL_CONTEXT (extp) = current_function_decl;
1349 if (TREE_CODE (p) == FUNCTION_DECL)
1351 DECL_RESULT (extp) = NULL_TREE;
1352 DECL_SAVED_TREE (extp) = NULL_TREE;
1353 DECL_STRUCT_FUNCTION (extp) = NULL;
1355 if (b->locus != UNKNOWN_LOCATION)
1356 DECL_SOURCE_LOCATION (extp) = b->locus;
1357 DECL_CHAIN (extp) = BLOCK_VARS (block);
1358 BLOCK_VARS (block) = extp;
1360 /* If this is the file scope set DECL_CONTEXT of each decl to
1361 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1362 work. */
1363 if (scope == file_scope)
1365 DECL_CONTEXT (p) = context;
1366 if (TREE_CODE (p) == TYPE_DECL
1367 && TREE_TYPE (p) != error_mark_node)
1368 set_type_context (TREE_TYPE (p), context);
1371 gcc_fallthrough ();
1372 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1373 already been put there by store_parm_decls. Unused-
1374 parameter warnings are handled by function.cc.
1375 error_mark_node obviously does not go in BLOCK_VARS and
1376 does not get unused-variable warnings. */
1377 case PARM_DECL:
1378 case ERROR_MARK:
1379 /* It is possible for a decl not to have a name. We get
1380 here with b->id NULL in this case. */
1381 if (b->id)
1383 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1384 I_SYMBOL_BINDING (b->id) = b->shadowed;
1385 if (b->shadowed && b->shadowed->u.type)
1386 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1388 break;
1390 default:
1391 gcc_unreachable ();
1396 /* Dispose of the block that we just made inside some higher level. */
1397 if ((scope->function_body || scope == file_scope) && context)
1399 DECL_INITIAL (context) = block;
1400 BLOCK_SUPERCONTEXT (block) = context;
1402 else if (scope->outer)
1404 if (block)
1405 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1406 /* If we did not make a block for the scope just exited, any
1407 blocks made for inner scopes must be carried forward so they
1408 will later become subblocks of something else. */
1409 else if (scope->blocks)
1410 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1413 /* Pop the current scope, and free the structure for reuse. */
1414 current_scope = scope->outer;
1415 if (scope->function_body)
1416 current_function_scope = scope->outer_function;
1418 memset (scope, 0, sizeof (struct c_scope));
1419 scope->outer = scope_freelist;
1420 scope_freelist = scope;
1422 return block;
1425 void
1426 push_file_scope (void)
1428 tree decl;
1430 if (file_scope)
1431 return;
1433 push_scope ();
1434 file_scope = current_scope;
1436 start_fname_decls ();
1438 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1439 bind (DECL_NAME (decl), decl, file_scope,
1440 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1443 void
1444 pop_file_scope (void)
1446 /* In case there were missing closebraces, get us back to the global
1447 binding level. */
1448 while (current_scope != file_scope)
1449 pop_scope ();
1451 /* __FUNCTION__ is defined at file scope (""). This
1452 call may not be necessary as my tests indicate it
1453 still works without it. */
1454 finish_fname_decls ();
1456 check_inline_statics ();
1458 /* This is the point to write out a PCH if we're doing that.
1459 In that case we do not want to do anything else. */
1460 if (pch_file)
1462 c_common_write_pch ();
1463 /* Ensure even the callers don't try to finalize the CU. */
1464 flag_syntax_only = 1;
1465 return;
1468 /* Pop off the file scope and close this translation unit. */
1469 pop_scope ();
1470 file_scope = 0;
1472 maybe_apply_pending_pragma_weaks ();
1475 /* Whether we are curently inside the initializer for an
1476 underspecified object definition (C2x auto or constexpr). */
1477 static bool in_underspecified_init;
1479 /* Start an underspecified object definition for NAME at LOC. This
1480 means that NAME is shadowed inside its initializer, so neither the
1481 definition being initialized, nor any definition from an outer
1482 scope, may be referenced during that initializer. Return state to
1483 be passed to finish_underspecified_init. If NAME is NULL_TREE, the
1484 underspecified object is a (constexpr) compound literal; there is
1485 no shadowing in that case, but all the other restrictions on
1486 underspecified object definitions still apply. */
1487 unsigned int
1488 start_underspecified_init (location_t loc, tree name)
1490 bool prev = in_underspecified_init;
1491 bool ok;
1492 if (name == NULL_TREE)
1493 ok = true;
1494 else
1496 tree decl = build_decl (loc, VAR_DECL, name, error_mark_node);
1497 C_DECL_UNDERSPECIFIED (decl) = 1;
1498 struct c_scope *scope = current_scope;
1499 struct c_binding *b = I_SYMBOL_BINDING (name);
1500 if (b && B_IN_SCOPE (b, scope))
1502 error_at (loc, "underspecified declaration of %qE, which is already "
1503 "declared in this scope", name);
1504 ok = false;
1506 else
1508 bind (name, decl, scope, false, false, loc);
1509 ok = true;
1512 in_underspecified_init = true;
1513 return ok | (prev << 1);
1516 /* Finish an underspecified object definition for NAME, before that
1517 name is bound to the real declaration instead of a placeholder.
1518 PREV_STATE is the value returned by the call to
1519 start_underspecified_init. If NAME is NULL_TREE, this means a
1520 compound literal, as for start_underspecified_init. */
1521 void
1522 finish_underspecified_init (tree name, unsigned int prev_state)
1524 if (name != NULL_TREE && (prev_state & 1))
1526 /* A VAR_DECL was bound to the name to shadow any previous
1527 declarations for the name; remove that binding now. */
1528 struct c_scope *scope = current_scope;
1529 struct c_binding *b = I_SYMBOL_BINDING (name);
1530 gcc_assert (b);
1531 gcc_assert (B_IN_SCOPE (b, scope));
1532 gcc_assert (VAR_P (b->decl));
1533 gcc_assert (C_DECL_UNDERSPECIFIED (b->decl));
1534 I_SYMBOL_BINDING (name) = b->shadowed;
1535 /* In erroneous cases there may be other bindings added to this
1536 scope during the initializer. */
1537 struct c_binding **p = &scope->bindings;
1538 while (*p != b)
1539 p = &((*p)->prev);
1540 *p = free_binding_and_advance (*p);
1542 in_underspecified_init = (prev_state & (1u << 1)) >> 1;
1545 /* Adjust the bindings for the start of a statement expression. */
1547 void
1548 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1550 struct c_scope *scope;
1552 for (scope = current_scope; scope != NULL; scope = scope->outer)
1554 struct c_binding *b;
1556 if (!scope->has_label_bindings)
1557 continue;
1559 for (b = scope->bindings; b != NULL; b = b->prev)
1561 struct c_label_vars *label_vars;
1562 unsigned int ix;
1563 struct c_goto_bindings *g;
1565 if (TREE_CODE (b->decl) != LABEL_DECL)
1566 continue;
1567 label_vars = b->u.label;
1568 ++label_vars->label_bindings.stmt_exprs;
1569 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1570 ++g->goto_bindings.stmt_exprs;
1574 if (switch_bindings != NULL)
1575 ++switch_bindings->stmt_exprs;
1578 /* Adjust the bindings for the end of a statement expression. */
1580 void
1581 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1583 struct c_scope *scope;
1585 for (scope = current_scope; scope != NULL; scope = scope->outer)
1587 struct c_binding *b;
1589 if (!scope->has_label_bindings)
1590 continue;
1592 for (b = scope->bindings; b != NULL; b = b->prev)
1594 struct c_label_vars *label_vars;
1595 unsigned int ix;
1596 struct c_goto_bindings *g;
1598 if (TREE_CODE (b->decl) != LABEL_DECL)
1599 continue;
1600 label_vars = b->u.label;
1601 --label_vars->label_bindings.stmt_exprs;
1602 if (label_vars->label_bindings.stmt_exprs < 0)
1604 label_vars->label_bindings.left_stmt_expr = true;
1605 label_vars->label_bindings.stmt_exprs = 0;
1607 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1609 --g->goto_bindings.stmt_exprs;
1610 if (g->goto_bindings.stmt_exprs < 0)
1612 g->goto_bindings.left_stmt_expr = true;
1613 g->goto_bindings.stmt_exprs = 0;
1619 if (switch_bindings != NULL)
1621 --switch_bindings->stmt_exprs;
1622 gcc_assert (switch_bindings->stmt_exprs >= 0);
1626 /* Push a definition or a declaration of struct, union or enum tag "name".
1627 "type" should be the type node.
1628 We assume that the tag "name" is not already defined, and has a location
1629 of LOC.
1631 Note that the definition may really be just a forward reference.
1632 In that case, the TYPE_SIZE will be zero. */
1634 static void
1635 pushtag (location_t loc, tree name, tree type)
1637 /* Record the identifier as the type's name if it has none. */
1638 if (name && !TYPE_NAME (type))
1639 TYPE_NAME (type) = name;
1640 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1642 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1643 tagged type we just added to the current scope. This fake
1644 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1645 to output a representation of a tagged type, and it also gives
1646 us a convenient place to record the "scope start" address for the
1647 tagged type. */
1649 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1650 TYPE_DECL, NULL_TREE, type));
1652 /* An approximation for now, so we can tell this is a function-scope tag.
1653 This will be updated in pop_scope. */
1654 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1656 if (warn_cxx_compat && name != NULL_TREE)
1658 struct c_binding *b = I_SYMBOL_BINDING (name);
1660 if (b != NULL
1661 && b->decl != NULL_TREE
1662 && TREE_CODE (b->decl) == TYPE_DECL
1663 && (B_IN_CURRENT_SCOPE (b)
1664 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1665 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1666 != TYPE_MAIN_VARIANT (type)))
1668 auto_diagnostic_group d;
1669 if (warning_at (loc, OPT_Wc___compat,
1670 ("using %qD as both a typedef and a tag is "
1671 "invalid in C++"), b->decl)
1672 && b->locus != UNKNOWN_LOCATION)
1673 inform (b->locus, "originally defined here");
1678 /* An exported interface to pushtag. This is used by the gdb plugin's
1679 binding oracle to introduce a new tag binding. */
1681 void
1682 c_pushtag (location_t loc, tree name, tree type)
1684 pushtag (loc, name, type);
1687 /* An exported interface to bind a declaration. LOC is the location
1688 to use. DECL is the declaration to bind. The decl's name is used
1689 to determine how it is bound. If DECL is a VAR_DECL, then
1690 IS_GLOBAL determines whether the decl is put into the global (file
1691 and external) scope or the current function's scope; if DECL is not
1692 a VAR_DECL then it is always put into the file scope. */
1694 void
1695 c_bind (location_t loc, tree decl, bool is_global)
1697 struct c_scope *scope;
1698 bool nested = false;
1700 if (!VAR_P (decl) || current_function_scope == NULL)
1702 /* Types and functions are always considered to be global. */
1703 scope = file_scope;
1704 DECL_EXTERNAL (decl) = 1;
1705 TREE_PUBLIC (decl) = 1;
1707 else if (is_global)
1709 /* Also bind it into the external scope. */
1710 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1711 nested = true;
1712 scope = file_scope;
1713 DECL_EXTERNAL (decl) = 1;
1714 TREE_PUBLIC (decl) = 1;
1716 else
1718 DECL_CONTEXT (decl) = current_function_decl;
1719 TREE_PUBLIC (decl) = 0;
1720 scope = current_function_scope;
1723 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1727 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1728 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1729 to the builtin_structptr_types array. Subsequent declarations of such
1730 built-ins are expected to refer to it rather than to fileptr_type_node,
1731 etc. which is just void* (or to any other type).
1732 Used only by match_builtin_function_types. */
1734 static const unsigned builtin_structptr_type_count
1735 = ARRAY_SIZE (builtin_structptr_types);
1737 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1739 /* Returns true if types T1 and T2 representing return types or types
1740 of function arguments are close enough to be considered interchangeable
1741 in redeclarations of built-in functions. */
1743 static bool
1744 types_close_enough_to_match (tree t1, tree t2)
1746 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1747 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1748 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1751 /* Subroutine of compare_decls. Allow harmless mismatches in return
1752 and argument types provided that the type modes match. Set *STRICT
1753 and *ARGNO to the expected argument type and number in case of
1754 an argument type mismatch or null and zero otherwise. Return
1755 a unified type given a suitable match, and 0 otherwise. */
1757 static tree
1758 match_builtin_function_types (tree newtype, tree oldtype,
1759 tree *strict, unsigned *argno)
1761 *argno = 0;
1762 *strict = NULL_TREE;
1764 /* Accept the return type of the new declaration if it has the same
1765 mode and if they're both pointers or if neither is. */
1766 tree oldrettype = TREE_TYPE (oldtype);
1767 tree newrettype = TREE_TYPE (newtype);
1769 if (!types_close_enough_to_match (oldrettype, newrettype))
1770 return NULL_TREE;
1772 /* Check that the return types are compatible but don't fail if they
1773 are not (e.g., int vs long in ILP32) and just let the caller know. */
1774 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1775 TYPE_MAIN_VARIANT (newrettype)))
1776 *strict = oldrettype;
1778 tree oldargs = TYPE_ARG_TYPES (oldtype);
1779 tree newargs = TYPE_ARG_TYPES (newtype);
1780 tree tryargs = newargs;
1782 const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1783 const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1785 gcc_checking_assert (nlst == nbst);
1787 for (unsigned i = 1; oldargs || newargs; ++i)
1789 if (!oldargs
1790 || !newargs
1791 || !TREE_VALUE (oldargs)
1792 || !TREE_VALUE (newargs))
1793 return NULL_TREE;
1795 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1796 tree newtype = TREE_VALUE (newargs);
1797 if (newtype == error_mark_node)
1798 return NULL_TREE;
1799 newtype = TYPE_MAIN_VARIANT (newtype);
1801 if (!types_close_enough_to_match (oldtype, newtype))
1802 return NULL_TREE;
1804 unsigned j = nbst;
1805 if (POINTER_TYPE_P (oldtype))
1806 /* Iterate over well-known struct types like FILE (whose types
1807 aren't known to us) and compare the pointer to each to
1808 the pointer argument. */
1809 for (j = 0; j < nbst; ++j)
1811 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1812 continue;
1813 /* Store the first FILE* etc. argument type (whatever it is), and
1814 expect any subsequent declarations of file I/O etc. built-ins
1815 to refer to it rather than to fileptr_type_node etc. which is
1816 just void* (or const void*). */
1817 if (last_structptr_types[j])
1819 if (!comptypes (last_structptr_types[j], newtype))
1821 *argno = i;
1822 *strict = last_structptr_types[j];
1825 else
1826 last_structptr_types[j] = newtype;
1827 break;
1830 if (j == nbst && !comptypes (oldtype, newtype))
1832 if (POINTER_TYPE_P (oldtype))
1834 /* For incompatible pointers, only reject differences in
1835 the unqualified variants of the referenced types but
1836 consider differences in qualifiers as benign (report
1837 those to caller via *STRICT below). */
1838 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1839 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1840 if (!comptypes (oldref, newref))
1841 return NULL_TREE;
1844 if (!*strict)
1846 *argno = i;
1847 *strict = oldtype;
1851 oldargs = TREE_CHAIN (oldargs);
1852 newargs = TREE_CHAIN (newargs);
1855 tree trytype = build_function_type (newrettype, tryargs);
1857 /* Allow declaration to change transaction_safe attribute. */
1858 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1859 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1860 tree newattrs = TYPE_ATTRIBUTES (newtype);
1861 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1862 if (oldtsafe && !newtsafe)
1863 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1864 else if (newtsafe && !oldtsafe)
1865 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1866 NULL_TREE, oldattrs);
1868 return build_type_attribute_variant (trytype, oldattrs);
1871 /* Subroutine of diagnose_mismatched_decls. Check for function type
1872 mismatch involving an empty arglist vs a nonempty one and give clearer
1873 diagnostics. */
1874 static void
1875 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1876 tree newtype, tree oldtype)
1878 tree t;
1880 if (TREE_CODE (olddecl) != FUNCTION_DECL
1881 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1882 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1883 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1884 return;
1886 t = TYPE_ARG_TYPES (oldtype);
1887 if (t == NULL_TREE)
1888 t = TYPE_ARG_TYPES (newtype);
1889 for (; t; t = TREE_CHAIN (t))
1891 tree type = TREE_VALUE (t);
1893 if (TREE_CHAIN (t) == NULL_TREE
1894 && TYPE_MAIN_VARIANT (type) != void_type_node)
1896 inform (input_location, "a parameter list with an ellipsis "
1897 "cannot match an empty parameter name list declaration");
1898 break;
1901 if (c_type_promotes_to (type) != type)
1903 inform (input_location, "an argument type that has a default "
1904 "promotion cannot match an empty parameter name list "
1905 "declaration");
1906 break;
1911 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1912 old-style function definition, NEWDECL is a prototype declaration.
1913 Diagnose inconsistencies in the argument list. Returns TRUE if
1914 the prototype is compatible, FALSE if not. */
1915 static bool
1916 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1918 tree newargs, oldargs;
1919 int i;
1921 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1923 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1924 newargs = TYPE_ARG_TYPES (newtype);
1925 i = 1;
1927 for (;;)
1929 tree oldargtype = TREE_VALUE (oldargs);
1930 tree newargtype = TREE_VALUE (newargs);
1932 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1933 return false;
1935 oldargtype = (TYPE_ATOMIC (oldargtype)
1936 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1937 TYPE_QUAL_ATOMIC)
1938 : TYPE_MAIN_VARIANT (oldargtype));
1939 newargtype = (TYPE_ATOMIC (newargtype)
1940 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1941 TYPE_QUAL_ATOMIC)
1942 : TYPE_MAIN_VARIANT (newargtype));
1944 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1945 break;
1947 /* Reaching the end of just one list means the two decls don't
1948 agree on the number of arguments. */
1949 if (END_OF_ARGLIST (oldargtype))
1951 error ("prototype for %q+D declares more arguments "
1952 "than previous old-style definition", newdecl);
1953 return false;
1955 else if (END_OF_ARGLIST (newargtype))
1957 error ("prototype for %q+D declares fewer arguments "
1958 "than previous old-style definition", newdecl);
1959 return false;
1962 /* Type for passing arg must be consistent with that declared
1963 for the arg. */
1964 else if (!comptypes (oldargtype, newargtype))
1966 error ("prototype for %q+D declares argument %d"
1967 " with incompatible type",
1968 newdecl, i);
1969 return false;
1972 oldargs = TREE_CHAIN (oldargs);
1973 newargs = TREE_CHAIN (newargs);
1974 i++;
1977 /* If we get here, no errors were found, but do issue a warning
1978 for this poor-style construct. */
1979 warning (0, "prototype for %q+D follows non-prototype definition",
1980 newdecl);
1981 return true;
1982 #undef END_OF_ARGLIST
1985 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1986 first in a pair of mismatched declarations, using the diagnostic
1987 function DIAG. */
1988 static void
1989 locate_old_decl (tree decl)
1991 if (TREE_CODE (decl) == FUNCTION_DECL
1992 && fndecl_built_in_p (decl)
1993 && !C_DECL_DECLARED_BUILTIN (decl))
1995 else if (DECL_INITIAL (decl))
1996 inform (input_location,
1997 "previous definition of %q+D with type %qT",
1998 decl, TREE_TYPE (decl));
1999 else if (C_DECL_IMPLICIT (decl))
2000 inform (input_location,
2001 "previous implicit declaration of %q+D with type %qT",
2002 decl, TREE_TYPE (decl));
2003 else
2004 inform (input_location,
2005 "previous declaration of %q+D with type %qT",
2006 decl, TREE_TYPE (decl));
2009 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
2010 Returns true if the caller should proceed to merge the two, false
2011 if OLDDECL should simply be discarded. As a side effect, issues
2012 all necessary diagnostics for invalid or poor-style combinations.
2013 If it returns true, writes the types of NEWDECL and OLDDECL to
2014 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
2015 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
2017 static bool
2018 diagnose_mismatched_decls (tree newdecl, tree olddecl,
2019 tree *newtypep, tree *oldtypep)
2021 tree newtype, oldtype;
2022 bool retval = true;
2024 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
2025 && DECL_EXTERNAL (DECL))
2027 /* If we have error_mark_node for either decl or type, just discard
2028 the previous decl - we're in an error cascade already. */
2029 if (olddecl == error_mark_node || newdecl == error_mark_node)
2030 return false;
2031 *oldtypep = oldtype = TREE_TYPE (olddecl);
2032 *newtypep = newtype = TREE_TYPE (newdecl);
2033 if (oldtype == error_mark_node || newtype == error_mark_node)
2034 return false;
2036 /* Two different categories of symbol altogether. This is an error
2037 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
2038 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2040 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
2041 && fndecl_built_in_p (olddecl)
2042 && !C_DECL_DECLARED_BUILTIN (olddecl)))
2044 auto_diagnostic_group d;
2045 error ("%q+D redeclared as different kind of symbol", newdecl);
2046 locate_old_decl (olddecl);
2048 else if (TREE_PUBLIC (newdecl))
2049 warning (OPT_Wbuiltin_declaration_mismatch,
2050 "built-in function %q+D declared as non-function",
2051 newdecl);
2052 else
2053 warning (OPT_Wshadow, "declaration of %q+D shadows "
2054 "a built-in function", newdecl);
2055 return false;
2058 /* Enumerators have no linkage, so may only be declared once in a
2059 given scope. */
2060 if (TREE_CODE (olddecl) == CONST_DECL)
2062 auto_diagnostic_group d;
2063 error ("redeclaration of enumerator %q+D", newdecl);
2064 locate_old_decl (olddecl);
2065 return false;
2068 bool pedwarned = false;
2069 bool warned = false;
2070 bool enum_and_int_p = false;
2071 auto_diagnostic_group d;
2073 int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2074 &enum_and_int_p);
2075 if (!comptypes_result)
2077 if (TREE_CODE (olddecl) == FUNCTION_DECL
2078 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2079 && !C_DECL_DECLARED_BUILTIN (olddecl))
2081 /* Accept "harmless" mismatches in function types such
2082 as missing qualifiers or int vs long when they're the same
2083 size. However, diagnose return and argument types that are
2084 incompatible according to language rules. */
2085 tree mismatch_expect;
2086 unsigned mismatch_argno;
2088 tree trytype = match_builtin_function_types (newtype, oldtype,
2089 &mismatch_expect,
2090 &mismatch_argno);
2092 if (trytype && comptypes (newtype, trytype))
2093 *oldtypep = oldtype = trytype;
2094 else
2096 /* If types don't match for a built-in, throw away the
2097 built-in. No point in calling locate_old_decl here, it
2098 won't print anything. */
2099 const char *header = header_for_builtin_fn (olddecl);
2100 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2101 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2102 "conflicting types for built-in function %q+D; "
2103 "expected %qT",
2104 newdecl, oldtype)
2105 && header)
2107 /* Suggest the right header to include as the preferred
2108 solution rather than the spelling of the declaration. */
2109 rich_location richloc (line_table, loc);
2110 maybe_add_include_fixit (&richloc, header, true);
2111 inform (&richloc,
2112 "%qD is declared in header %qs", olddecl, header);
2114 return false;
2117 if (mismatch_expect && extra_warnings)
2119 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2120 bool warned = false;
2121 if (mismatch_argno)
2122 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2123 "mismatch in argument %u type of built-in "
2124 "function %qD; expected %qT",
2125 mismatch_argno, newdecl, mismatch_expect);
2126 else
2127 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2128 "mismatch in return type of built-in "
2129 "function %qD; expected %qT",
2130 newdecl, mismatch_expect);
2131 const char *header = header_for_builtin_fn (olddecl);
2132 if (warned && header)
2134 rich_location richloc (line_table, newloc);
2135 maybe_add_include_fixit (&richloc, header, true);
2136 inform (&richloc,
2137 "%qD is declared in header %qs", olddecl, header);
2141 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2142 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2144 /* A conflicting function declaration for a predeclared
2145 function that isn't actually built in. Objective C uses
2146 these. The new declaration silently overrides everything
2147 but the volatility (i.e. noreturn) indication. See also
2148 below. FIXME: Make Objective C use normal builtins. */
2149 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2150 return false;
2152 /* Permit void foo (...) to match int foo (...) if the latter is
2153 the definition and implicit int was used. See
2154 c-torture/compile/920625-2.c. */
2155 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2156 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2157 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2158 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2160 pedwarned = pedwarn (input_location, 0,
2161 "conflicting types for %q+D", newdecl);
2162 /* Make sure we keep void as the return type. */
2163 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2164 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2166 /* Permit void foo (...) to match an earlier call to foo (...) with
2167 no declared type (thus, implicitly int). */
2168 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2169 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2170 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2171 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2173 pedwarned = pedwarn (input_location, 0,
2174 "conflicting types for %q+D; have %qT",
2175 newdecl, newtype);
2176 /* Make sure we keep void as the return type. */
2177 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2179 else
2181 int new_quals = TYPE_QUALS (newtype);
2182 int old_quals = TYPE_QUALS (oldtype);
2184 if (new_quals != old_quals)
2186 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2187 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2188 if (new_addr != old_addr)
2190 if (ADDR_SPACE_GENERIC_P (new_addr))
2191 error ("conflicting named address spaces (generic vs %s) "
2192 "for %q+D",
2193 c_addr_space_name (old_addr), newdecl);
2194 else if (ADDR_SPACE_GENERIC_P (old_addr))
2195 error ("conflicting named address spaces (%s vs generic) "
2196 "for %q+D",
2197 c_addr_space_name (new_addr), newdecl);
2198 else
2199 error ("conflicting named address spaces (%s vs %s) "
2200 "for %q+D",
2201 c_addr_space_name (new_addr),
2202 c_addr_space_name (old_addr),
2203 newdecl);
2206 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2207 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2208 error ("conflicting type qualifiers for %q+D", newdecl);
2210 else
2211 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2212 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2213 locate_old_decl (olddecl);
2214 return false;
2217 /* Warn about enum/integer type mismatches. They are compatible types
2218 (C2X 6.7.2.2/5), but may pose portability problems. */
2219 else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
2220 warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2221 OPT_Wenum_int_mismatch,
2222 "conflicting types for %q+D due to enum/integer "
2223 "mismatch; have %qT", newdecl, newtype);
2225 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2226 but silently ignore the redeclaration if either is in a system
2227 header. (Conflicting redeclarations were handled above.) This
2228 is allowed for C11 if the types are the same, not just
2229 compatible. */
2230 if (TREE_CODE (newdecl) == TYPE_DECL)
2232 bool types_different = false;
2234 comptypes_result
2235 = comptypes_check_different_types (oldtype, newtype, &types_different);
2237 if (comptypes_result != 1 || types_different)
2239 error ("redefinition of typedef %q+D with different type", newdecl);
2240 locate_old_decl (olddecl);
2241 return false;
2244 if (DECL_IN_SYSTEM_HEADER (newdecl)
2245 || DECL_IN_SYSTEM_HEADER (olddecl)
2246 || warning_suppressed_p (newdecl, OPT_Wpedantic)
2247 || warning_suppressed_p (olddecl, OPT_Wpedantic))
2248 return true; /* Allow OLDDECL to continue in use. */
2250 if (c_type_variably_modified_p (newtype))
2252 error ("redefinition of typedef %q+D with variably modified type",
2253 newdecl);
2254 locate_old_decl (olddecl);
2256 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2257 "redefinition of typedef %q+D", newdecl))
2258 locate_old_decl (olddecl);
2260 return true;
2263 /* Function declarations can either be 'static' or 'extern' (no
2264 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2265 can never conflict with each other on account of linkage
2266 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2267 gnu89 mode permits two definitions if one is 'extern inline' and
2268 one is not. The non- extern-inline definition supersedes the
2269 extern-inline definition. */
2271 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2273 /* If you declare a built-in function name as static, or
2274 define the built-in with an old-style definition (so we
2275 can't validate the argument list) the built-in definition is
2276 overridden, but optionally warn this was a bad choice of name. */
2277 if (fndecl_built_in_p (olddecl)
2278 && !C_DECL_DECLARED_BUILTIN (olddecl))
2280 if (!TREE_PUBLIC (newdecl)
2281 || (DECL_INITIAL (newdecl)
2282 && !prototype_p (TREE_TYPE (newdecl))))
2284 warning_at (DECL_SOURCE_LOCATION (newdecl),
2285 OPT_Wshadow, "declaration of %qD shadows "
2286 "a built-in function", newdecl);
2287 /* Discard the old built-in function. */
2288 return false;
2291 if (!prototype_p (TREE_TYPE (newdecl)))
2293 /* Set for built-ins that take no arguments. */
2294 bool func_void_args = false;
2295 if (tree at = TYPE_ARG_TYPES (oldtype))
2296 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2298 if (extra_warnings && !func_void_args)
2299 warning_at (DECL_SOURCE_LOCATION (newdecl),
2300 OPT_Wbuiltin_declaration_mismatch,
2301 "declaration of built-in function %qD without "
2302 "a prototype; expected %qT",
2303 newdecl, TREE_TYPE (olddecl));
2307 if (DECL_INITIAL (newdecl))
2309 if (DECL_INITIAL (olddecl))
2311 /* If both decls are in the same TU and the new declaration
2312 isn't overriding an extern inline reject the new decl.
2313 In c99, no overriding is allowed in the same translation
2314 unit. */
2315 if ((!DECL_EXTERN_INLINE (olddecl)
2316 || DECL_EXTERN_INLINE (newdecl)
2317 || (!flag_gnu89_inline
2318 && (!DECL_DECLARED_INLINE_P (olddecl)
2319 || !lookup_attribute ("gnu_inline",
2320 DECL_ATTRIBUTES (olddecl)))
2321 && (!DECL_DECLARED_INLINE_P (newdecl)
2322 || !lookup_attribute ("gnu_inline",
2323 DECL_ATTRIBUTES (newdecl))))
2325 && same_translation_unit_p (newdecl, olddecl))
2327 auto_diagnostic_group d;
2328 error ("redefinition of %q+D", newdecl);
2329 locate_old_decl (olddecl);
2330 return false;
2334 /* If we have a prototype after an old-style function definition,
2335 the argument types must be checked specially. */
2336 else if (DECL_INITIAL (olddecl)
2337 && !prototype_p (oldtype) && prototype_p (newtype)
2338 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2340 auto_diagnostic_group d;
2341 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2343 locate_old_decl (olddecl);
2344 return false;
2347 /* A non-static declaration (even an "extern") followed by a
2348 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2349 The same is true for a static forward declaration at block
2350 scope followed by a non-static declaration/definition at file
2351 scope. Static followed by non-static at the same scope is
2352 not undefined behavior, and is the most convenient way to get
2353 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2354 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2355 we do diagnose it if -Wtraditional. */
2356 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2358 /* Two exceptions to the rule. If olddecl is an extern
2359 inline, or a predeclared function that isn't actually
2360 built in, newdecl silently overrides olddecl. The latter
2361 occur only in Objective C; see also above. (FIXME: Make
2362 Objective C use normal builtins.) */
2363 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2364 && !DECL_EXTERN_INLINE (olddecl))
2366 auto_diagnostic_group d;
2367 error ("static declaration of %q+D follows "
2368 "non-static declaration", newdecl);
2369 locate_old_decl (olddecl);
2371 return false;
2373 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2375 if (DECL_CONTEXT (olddecl))
2377 auto_diagnostic_group d;
2378 error ("non-static declaration of %q+D follows "
2379 "static declaration", newdecl);
2380 locate_old_decl (olddecl);
2381 return false;
2383 else if (warn_traditional)
2385 warned |= warning (OPT_Wtraditional,
2386 "non-static declaration of %q+D "
2387 "follows static declaration", newdecl);
2391 /* Make sure gnu_inline attribute is either not present, or
2392 present on all inline decls. */
2393 if (DECL_DECLARED_INLINE_P (olddecl)
2394 && DECL_DECLARED_INLINE_P (newdecl))
2396 bool newa = lookup_attribute ("gnu_inline",
2397 DECL_ATTRIBUTES (newdecl)) != NULL;
2398 bool olda = lookup_attribute ("gnu_inline",
2399 DECL_ATTRIBUTES (olddecl)) != NULL;
2400 if (newa != olda)
2402 auto_diagnostic_group d;
2403 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2404 newa ? newdecl : olddecl);
2405 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2406 "but not here");
2410 else if (VAR_P (newdecl))
2412 /* Only variables can be thread-local, and all declarations must
2413 agree on this property. */
2414 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2416 /* Nothing to check. Since OLDDECL is marked threadprivate
2417 and NEWDECL does not have a thread-local attribute, we
2418 will merge the threadprivate attribute into NEWDECL. */
2421 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2423 auto_diagnostic_group d;
2424 if (DECL_THREAD_LOCAL_P (newdecl))
2425 error ("thread-local declaration of %q+D follows "
2426 "non-thread-local declaration", newdecl);
2427 else
2428 error ("non-thread-local declaration of %q+D follows "
2429 "thread-local declaration", newdecl);
2431 locate_old_decl (olddecl);
2432 return false;
2435 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2436 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2438 auto_diagnostic_group d;
2439 error ("redefinition of %q+D", newdecl);
2440 locate_old_decl (olddecl);
2441 return false;
2444 /* Objects declared at file scope: if the first declaration had
2445 external linkage (even if it was an external reference) the
2446 second must have external linkage as well, or the behavior is
2447 undefined. If the first declaration had internal linkage, then
2448 the second must too, or else be an external reference (in which
2449 case the composite declaration still has internal linkage).
2450 As for function declarations, we warn about the static-then-
2451 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2452 if (DECL_FILE_SCOPE_P (newdecl)
2453 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2455 if (DECL_EXTERNAL (newdecl))
2457 if (!DECL_FILE_SCOPE_P (olddecl))
2459 auto_diagnostic_group d;
2460 error ("extern declaration of %q+D follows "
2461 "declaration with no linkage", newdecl);
2462 locate_old_decl (olddecl);
2463 return false;
2465 else if (warn_traditional)
2467 warned |= warning (OPT_Wtraditional,
2468 "non-static declaration of %q+D "
2469 "follows static declaration", newdecl);
2472 else
2474 auto_diagnostic_group d;
2475 if (TREE_PUBLIC (newdecl))
2476 error ("non-static declaration of %q+D follows "
2477 "static declaration", newdecl);
2478 else
2479 error ("static declaration of %q+D follows "
2480 "non-static declaration", newdecl);
2482 locate_old_decl (olddecl);
2483 return false;
2486 /* Two objects with the same name declared at the same block
2487 scope must both be external references (6.7p3). */
2488 else if (!DECL_FILE_SCOPE_P (newdecl))
2490 if (DECL_EXTERNAL (newdecl))
2492 /* Extern with initializer at block scope, which will
2493 already have received an error. */
2495 else if (DECL_EXTERNAL (olddecl))
2497 auto_diagnostic_group d;
2498 error ("declaration of %q+D with no linkage follows "
2499 "extern declaration", newdecl);
2500 locate_old_decl (olddecl);
2502 else
2504 auto_diagnostic_group d;
2505 error ("redeclaration of %q+D with no linkage", newdecl);
2506 locate_old_decl (olddecl);
2509 return false;
2512 /* C++ does not permit a decl to appear multiple times at file
2513 scope. */
2514 if (warn_cxx_compat
2515 && DECL_FILE_SCOPE_P (newdecl)
2516 && !DECL_EXTERNAL (newdecl)
2517 && !DECL_EXTERNAL (olddecl))
2518 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2519 OPT_Wc___compat,
2520 ("duplicate declaration of %qD is "
2521 "invalid in C++"),
2522 newdecl);
2525 /* warnings */
2526 /* All decls must agree on a visibility. */
2527 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2528 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2529 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2531 warned |= warning (0, "redeclaration of %q+D with different visibility "
2532 "(old visibility preserved)", newdecl);
2535 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2536 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2537 else /* PARM_DECL, VAR_DECL */
2539 /* Redeclaration of a parameter is a constraint violation (this is
2540 not explicitly stated, but follows from C99 6.7p3 [no more than
2541 one declaration of the same identifier with no linkage in the
2542 same scope, except type tags] and 6.2.2p6 [parameters have no
2543 linkage]). We must check for a forward parameter declaration,
2544 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2545 an extension, the mandatory diagnostic for which is handled by
2546 mark_forward_parm_decls. */
2548 if (TREE_CODE (newdecl) == PARM_DECL
2549 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2551 auto_diagnostic_group d;
2552 error ("redefinition of parameter %q+D", newdecl);
2553 locate_old_decl (olddecl);
2554 return false;
2558 /* Optional warning for completely redundant decls. */
2559 if (!warned && !pedwarned
2560 && warn_redundant_decls
2561 /* Don't warn about a function declaration followed by a
2562 definition. */
2563 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2564 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2565 /* Don't warn about redundant redeclarations of builtins. */
2566 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2567 && !fndecl_built_in_p (newdecl)
2568 && fndecl_built_in_p (olddecl)
2569 && !C_DECL_DECLARED_BUILTIN (olddecl))
2570 /* Don't warn about an extern followed by a definition. */
2571 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2572 /* Don't warn about forward parameter decls. */
2573 && !(TREE_CODE (newdecl) == PARM_DECL
2574 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2575 /* Don't warn about a variable definition following a declaration. */
2576 && !(VAR_P (newdecl)
2577 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2579 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2580 newdecl);
2583 /* Report location of previous decl/defn. */
2584 if (warned || pedwarned)
2585 locate_old_decl (olddecl);
2587 #undef DECL_EXTERN_INLINE
2589 return retval;
2592 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2593 consistent with OLDDECL, but carries new information. Merge the
2594 new information into OLDDECL. This function issues no
2595 diagnostics. */
2597 static void
2598 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2600 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2601 && DECL_INITIAL (newdecl) != NULL_TREE);
2602 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2603 && prototype_p (TREE_TYPE (newdecl)));
2604 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2605 && prototype_p (TREE_TYPE (olddecl)));
2607 /* For real parm decl following a forward decl, rechain the old decl
2608 in its new location and clear TREE_ASM_WRITTEN (it's not a
2609 forward decl anymore). */
2610 if (TREE_CODE (newdecl) == PARM_DECL
2611 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2613 struct c_binding *b, **here;
2615 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2616 if ((*here)->decl == olddecl)
2617 goto found;
2618 gcc_unreachable ();
2620 found:
2621 b = *here;
2622 *here = b->prev;
2623 b->prev = current_scope->bindings;
2624 current_scope->bindings = b;
2626 TREE_ASM_WRITTEN (olddecl) = 0;
2629 DECL_ATTRIBUTES (newdecl)
2630 = targetm.merge_decl_attributes (olddecl, newdecl);
2632 /* For typedefs use the old type, as the new type's DECL_NAME points
2633 at newdecl, which will be ggc_freed. */
2634 if (TREE_CODE (newdecl) == TYPE_DECL)
2636 /* But NEWTYPE might have an attribute, honor that. */
2637 tree tem = newtype;
2638 newtype = oldtype;
2640 if (TYPE_USER_ALIGN (tem))
2642 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2643 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2644 TYPE_USER_ALIGN (newtype) = true;
2647 /* And remove the new type from the variants list. */
2648 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2650 tree remove = TREE_TYPE (newdecl);
2651 if (TYPE_MAIN_VARIANT (remove) == remove)
2653 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2654 /* If remove is the main variant, no need to remove that
2655 from the list. One of the DECL_ORIGINAL_TYPE
2656 variants, e.g. created for aligned attribute, might still
2657 refer to the newdecl TYPE_DECL though, so remove that one
2658 in that case. */
2659 if (DECL_ORIGINAL_TYPE (newdecl)
2660 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2661 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2662 t; t = TYPE_MAIN_VARIANT (t))
2663 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2665 TYPE_NEXT_VARIANT (t)
2666 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2667 break;
2670 else
2671 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2672 t = TYPE_NEXT_VARIANT (t))
2673 if (TYPE_NEXT_VARIANT (t) == remove)
2675 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2676 break;
2681 /* Merge the data types specified in the two decls. */
2682 TREE_TYPE (newdecl)
2683 = TREE_TYPE (olddecl)
2684 = composite_type (newtype, oldtype);
2686 /* Lay the type out, unless already done. */
2687 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2689 if (TREE_TYPE (newdecl) != error_mark_node)
2690 layout_type (TREE_TYPE (newdecl));
2691 if (TREE_CODE (newdecl) != FUNCTION_DECL
2692 && TREE_CODE (newdecl) != TYPE_DECL
2693 && TREE_CODE (newdecl) != CONST_DECL)
2694 layout_decl (newdecl, 0);
2696 else
2698 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2699 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2700 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2701 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2702 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2704 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2705 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2707 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2708 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2709 DECL_USER_ALIGN (newdecl) = 1;
2710 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2711 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2712 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2713 DECL_WARN_IF_NOT_ALIGN (olddecl));
2716 /* Keep the old rtl since we can safely use it. */
2717 if (HAS_RTL_P (olddecl))
2718 COPY_DECL_RTL (olddecl, newdecl);
2720 /* Merge the type qualifiers. */
2721 if (TREE_READONLY (newdecl))
2722 TREE_READONLY (olddecl) = 1;
2724 if (TREE_THIS_VOLATILE (newdecl))
2725 TREE_THIS_VOLATILE (olddecl) = 1;
2727 /* Merge deprecatedness. */
2728 if (TREE_DEPRECATED (newdecl))
2729 TREE_DEPRECATED (olddecl) = 1;
2731 /* Merge unavailability. */
2732 if (TREE_UNAVAILABLE (newdecl))
2733 TREE_UNAVAILABLE (olddecl) = 1;
2735 /* If a decl is in a system header and the other isn't, keep the one on the
2736 system header. Otherwise, keep source location of definition rather than
2737 declaration and of prototype rather than non-prototype unless that
2738 prototype is built-in. */
2739 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2740 && DECL_IN_SYSTEM_HEADER (olddecl)
2741 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2742 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2743 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2744 && DECL_IN_SYSTEM_HEADER (newdecl)
2745 && !DECL_IN_SYSTEM_HEADER (olddecl))
2746 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2747 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2748 && DECL_INITIAL (olddecl) != NULL_TREE)
2749 || (old_is_prototype && !new_is_prototype
2750 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2751 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2753 /* Merge the initialization information. */
2754 if (DECL_INITIAL (newdecl) == NULL_TREE)
2755 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2757 /* Merge 'constexpr' information. */
2758 if (VAR_P (olddecl) && VAR_P (newdecl))
2760 if (C_DECL_DECLARED_CONSTEXPR (olddecl))
2761 C_DECL_DECLARED_CONSTEXPR (newdecl) = 1;
2762 else if (C_DECL_DECLARED_CONSTEXPR (newdecl))
2763 C_DECL_DECLARED_CONSTEXPR (olddecl) = 1;
2766 /* Merge the threadprivate attribute. */
2767 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2768 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2770 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2772 /* Copy the assembler name.
2773 Currently, it can only be defined in the prototype. */
2774 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2776 /* Use visibility of whichever declaration had it specified */
2777 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2779 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2780 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2783 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2785 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2786 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2787 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2788 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2789 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2790 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2791 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2792 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2793 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2794 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2795 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2796 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2797 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2798 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2801 /* Merge the storage class information. */
2802 merge_weak (newdecl, olddecl);
2804 /* For functions, static overrides non-static. */
2805 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2807 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2808 /* This is since we don't automatically
2809 copy the attributes of NEWDECL into OLDDECL. */
2810 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2811 /* If this clears `static', clear it in the identifier too. */
2812 if (!TREE_PUBLIC (olddecl))
2813 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2817 /* In c99, 'extern' declaration before (or after) 'inline' means this
2818 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2819 is present. */
2820 if (TREE_CODE (newdecl) == FUNCTION_DECL
2821 && !flag_gnu89_inline
2822 && (DECL_DECLARED_INLINE_P (newdecl)
2823 || DECL_DECLARED_INLINE_P (olddecl))
2824 && (!DECL_DECLARED_INLINE_P (newdecl)
2825 || !DECL_DECLARED_INLINE_P (olddecl)
2826 || !DECL_EXTERNAL (olddecl))
2827 && DECL_EXTERNAL (newdecl)
2828 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2829 && !current_function_decl)
2830 DECL_EXTERNAL (newdecl) = 0;
2832 /* An inline definition following a static declaration is not
2833 DECL_EXTERNAL. */
2834 if (new_is_definition
2835 && (DECL_DECLARED_INLINE_P (newdecl)
2836 || DECL_DECLARED_INLINE_P (olddecl))
2837 && !TREE_PUBLIC (olddecl))
2838 DECL_EXTERNAL (newdecl) = 0;
2840 if (DECL_EXTERNAL (newdecl))
2842 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2843 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2845 /* An extern decl does not override previous storage class. */
2846 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2847 if (!DECL_EXTERNAL (newdecl))
2849 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2850 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2853 else
2855 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2856 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2859 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2861 /* If we're redefining a function previously defined as extern
2862 inline, make sure we emit debug info for the inline before we
2863 throw it away, in case it was inlined into a function that
2864 hasn't been written out yet. */
2865 if (new_is_definition && DECL_INITIAL (olddecl))
2866 /* The new defn must not be inline. */
2867 DECL_UNINLINABLE (newdecl) = 1;
2868 else
2870 /* If either decl says `inline', this fn is inline, unless
2871 its definition was passed already. */
2872 if (DECL_DECLARED_INLINE_P (newdecl)
2873 || DECL_DECLARED_INLINE_P (olddecl))
2874 DECL_DECLARED_INLINE_P (newdecl) = 1;
2876 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2877 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2879 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2880 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2881 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2882 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2885 if (fndecl_built_in_p (olddecl))
2887 /* If redeclaring a builtin function, it stays built in.
2888 But it gets tagged as having been declared. */
2889 copy_decl_built_in_function (newdecl, olddecl);
2890 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2891 if (new_is_prototype)
2893 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2894 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2896 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2897 switch (fncode)
2899 /* If a compatible prototype of these builtin functions
2900 is seen, assume the runtime implements it with the
2901 expected semantics. */
2902 case BUILT_IN_STPCPY:
2903 if (builtin_decl_explicit_p (fncode))
2904 set_builtin_decl_implicit_p (fncode, true);
2905 break;
2906 default:
2907 if (builtin_decl_explicit_p (fncode))
2908 set_builtin_decl_declared_p (fncode, true);
2909 break;
2912 copy_attributes_to_builtin (newdecl);
2915 else
2916 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2917 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2920 /* Preserve function specific target and optimization options */
2921 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2922 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2923 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2924 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2926 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2927 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2928 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2929 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2931 /* Also preserve various other info from the definition. */
2932 if (!new_is_definition)
2934 tree t;
2935 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2936 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2937 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2938 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2939 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2940 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2941 DECL_CONTEXT (t) = newdecl;
2943 /* See if we've got a function to instantiate from. */
2944 if (DECL_SAVED_TREE (olddecl))
2945 DECL_ABSTRACT_ORIGIN (newdecl)
2946 = DECL_ABSTRACT_ORIGIN (olddecl);
2950 /* Merge the USED information. */
2951 if (TREE_USED (olddecl))
2952 TREE_USED (newdecl) = 1;
2953 else if (TREE_USED (newdecl))
2954 TREE_USED (olddecl) = 1;
2955 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2956 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2957 if (DECL_PRESERVE_P (olddecl))
2958 DECL_PRESERVE_P (newdecl) = 1;
2959 else if (DECL_PRESERVE_P (newdecl))
2960 DECL_PRESERVE_P (olddecl) = 1;
2962 /* Merge DECL_COMMON */
2963 if (VAR_P (olddecl) && VAR_P (newdecl)
2964 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2965 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2966 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2968 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2969 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2970 DECL_ARGUMENTS (if appropriate). */
2972 unsigned olddecl_uid = DECL_UID (olddecl);
2973 tree olddecl_context = DECL_CONTEXT (olddecl);
2974 tree olddecl_arguments = NULL;
2975 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2976 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2978 memcpy ((char *) olddecl + sizeof (struct tree_common),
2979 (char *) newdecl + sizeof (struct tree_common),
2980 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2981 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2982 switch (TREE_CODE (olddecl))
2984 case FUNCTION_DECL:
2985 case VAR_DECL:
2987 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2989 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2990 (char *) newdecl + sizeof (struct tree_decl_common),
2991 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2992 olddecl->decl_with_vis.symtab_node = snode;
2994 if ((DECL_EXTERNAL (olddecl)
2995 || TREE_PUBLIC (olddecl)
2996 || TREE_STATIC (olddecl))
2997 && DECL_SECTION_NAME (newdecl) != NULL)
2998 set_decl_section_name (olddecl, newdecl);
3000 /* This isn't quite correct for something like
3001 int __thread x attribute ((tls_model ("local-exec")));
3002 extern int __thread x;
3003 as we'll lose the "local-exec" model. */
3004 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
3005 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
3006 break;
3009 case FIELD_DECL:
3010 case PARM_DECL:
3011 case LABEL_DECL:
3012 case RESULT_DECL:
3013 case CONST_DECL:
3014 case TYPE_DECL:
3015 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3016 (char *) newdecl + sizeof (struct tree_decl_common),
3017 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
3018 break;
3020 default:
3022 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
3023 (char *) newdecl + sizeof (struct tree_decl_common),
3024 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
3026 DECL_UID (olddecl) = olddecl_uid;
3027 DECL_CONTEXT (olddecl) = olddecl_context;
3028 if (TREE_CODE (olddecl) == FUNCTION_DECL)
3029 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
3032 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3033 so that encode_section_info has a chance to look at the new decl
3034 flags and attributes. */
3035 if (DECL_RTL_SET_P (olddecl)
3036 && (TREE_CODE (olddecl) == FUNCTION_DECL
3037 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
3038 make_decl_rtl (olddecl);
3041 /* Handle when a new declaration NEWDECL has the same name as an old
3042 one OLDDECL in the same binding contour. Prints an error message
3043 if appropriate.
3045 If safely possible, alter OLDDECL to look like NEWDECL, and return
3046 true. Otherwise, return false. */
3048 static bool
3049 duplicate_decls (tree newdecl, tree olddecl)
3051 tree newtype = NULL, oldtype = NULL;
3053 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
3055 /* Avoid `unused variable' and other warnings for OLDDECL. */
3056 suppress_warning (olddecl, OPT_Wunused);
3057 /* If the types are completely different, poison them both with
3058 error_mark_node. */
3059 if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
3060 && olddecl != error_mark_node
3061 && seen_error ())
3063 if (TREE_CODE (olddecl) != FUNCTION_DECL)
3064 TREE_TYPE (olddecl) = error_mark_node;
3065 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3066 TREE_TYPE (newdecl) = error_mark_node;
3068 return false;
3071 merge_decls (newdecl, olddecl, newtype, oldtype);
3073 /* The NEWDECL will no longer be needed.
3075 Before releasing the node, be sure to remove function from symbol
3076 table that might have been inserted there to record comdat group.
3077 Be sure to however do not free DECL_STRUCT_FUNCTION because this
3078 structure is shared in between NEWDECL and OLDECL. */
3079 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3080 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3081 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3083 struct symtab_node *snode = symtab_node::get (newdecl);
3084 if (snode)
3085 snode->remove ();
3087 ggc_free (newdecl);
3088 return true;
3092 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3093 static void
3094 warn_if_shadowing (tree new_decl)
3096 struct c_binding *b;
3098 /* Shadow warnings wanted? */
3099 if (!(warn_shadow
3100 || warn_shadow_local
3101 || warn_shadow_compatible_local)
3102 /* No shadow warnings for internally generated vars. */
3103 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3104 return;
3106 /* Is anything being shadowed? Invisible decls do not count. */
3107 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3108 if (b->decl && b->decl != new_decl && !b->invisible
3109 && (b->decl == error_mark_node
3110 || diagnostic_report_warnings_p (global_dc,
3111 DECL_SOURCE_LOCATION (b->decl))))
3113 tree old_decl = b->decl;
3115 if (old_decl == error_mark_node)
3117 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3118 "non-variable", new_decl);
3119 break;
3122 bool warned = false;
3123 auto_diagnostic_group d;
3124 if (TREE_CODE (old_decl) == PARM_DECL)
3126 enum opt_code warning_code;
3128 /* If '-Wshadow=compatible-local' is specified without other
3129 -Wshadow= flags, we will warn only when the types of the
3130 shadowing variable (i.e. new_decl) and the shadowed variable
3131 (old_decl) are compatible. */
3132 if (warn_shadow)
3133 warning_code = OPT_Wshadow;
3134 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3135 warning_code = OPT_Wshadow_compatible_local;
3136 else
3137 warning_code = OPT_Wshadow_local;
3138 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3139 "declaration of %qD shadows a parameter",
3140 new_decl);
3142 else if (DECL_FILE_SCOPE_P (old_decl))
3144 /* Do not warn if a variable shadows a function, unless
3145 the variable is a function or a pointer-to-function. */
3146 if (TREE_CODE (old_decl) == FUNCTION_DECL
3147 && TREE_CODE (new_decl) != FUNCTION_DECL
3148 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3149 continue;
3151 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3152 "declaration of %qD shadows a global "
3153 "declaration",
3154 new_decl);
3156 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3157 && fndecl_built_in_p (old_decl))
3159 warning (OPT_Wshadow, "declaration of %q+D shadows "
3160 "a built-in function", new_decl);
3161 break;
3163 else
3165 enum opt_code warning_code;
3167 /* If '-Wshadow=compatible-local' is specified without other
3168 -Wshadow= flags, we will warn only when the types of the
3169 shadowing variable (i.e. new_decl) and the shadowed variable
3170 (old_decl) are compatible. */
3171 if (warn_shadow)
3172 warning_code = OPT_Wshadow;
3173 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3174 warning_code = OPT_Wshadow_compatible_local;
3175 else
3176 warning_code = OPT_Wshadow_local;
3177 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3178 "declaration of %qD shadows a previous local",
3179 new_decl);
3182 if (warned)
3183 inform (DECL_SOURCE_LOCATION (old_decl),
3184 "shadowed declaration is here");
3186 break;
3190 /* Record a decl-node X as belonging to the current lexical scope.
3191 Check for errors (such as an incompatible declaration for the same
3192 name already seen in the same scope).
3194 Returns either X or an old decl for the same name.
3195 If an old decl is returned, it may have been smashed
3196 to agree with what X says. */
3198 tree
3199 pushdecl (tree x)
3201 tree name = DECL_NAME (x);
3202 struct c_scope *scope = current_scope;
3203 struct c_binding *b;
3204 bool nested = false;
3205 location_t locus = DECL_SOURCE_LOCATION (x);
3207 /* Must set DECL_CONTEXT for everything not at file scope or
3208 DECL_FILE_SCOPE_P won't work. Local externs don't count
3209 unless they have initializers (which generate code). */
3210 if (current_function_decl
3211 && (!VAR_OR_FUNCTION_DECL_P (x)
3212 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3213 DECL_CONTEXT (x) = current_function_decl;
3215 /* Anonymous decls are just inserted in the scope. */
3216 if (!name)
3218 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3219 locus);
3220 return x;
3223 /* First, see if there is another declaration with the same name in
3224 the current scope. If there is, duplicate_decls may do all the
3225 work for us. If duplicate_decls returns false, that indicates
3226 two incompatible decls in the same scope; we are to silently
3227 replace the old one (duplicate_decls has issued all appropriate
3228 diagnostics). In particular, we should not consider possible
3229 duplicates in the external scope, or shadowing. */
3230 b = I_SYMBOL_BINDING (name);
3231 if (b && B_IN_SCOPE (b, scope))
3233 struct c_binding *b_ext, *b_use;
3234 tree type = TREE_TYPE (x);
3235 tree visdecl = b->decl;
3236 tree vistype = TREE_TYPE (visdecl);
3237 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3238 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3239 b->inner_comp = false;
3240 b_use = b;
3241 b_ext = b;
3242 /* If this is an external linkage declaration, we should check
3243 for compatibility with the type in the external scope before
3244 setting the type at this scope based on the visible
3245 information only. */
3246 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3248 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3249 b_ext = b_ext->shadowed;
3250 if (b_ext)
3252 b_use = b_ext;
3253 if (b_use->u.type)
3254 TREE_TYPE (b_use->decl) = b_use->u.type;
3257 if (duplicate_decls (x, b_use->decl))
3259 if (b_use != b)
3261 /* Save the updated type in the external scope and
3262 restore the proper type for this scope. */
3263 tree thistype;
3264 if (comptypes (vistype, type))
3265 thistype = composite_type (vistype, type);
3266 else
3267 thistype = TREE_TYPE (b_use->decl);
3268 b_use->u.type = TREE_TYPE (b_use->decl);
3269 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3270 && fndecl_built_in_p (b_use->decl))
3271 thistype
3272 = build_type_attribute_variant (thistype,
3273 TYPE_ATTRIBUTES
3274 (b_use->u.type));
3275 TREE_TYPE (b_use->decl) = thistype;
3277 return b_use->decl;
3279 else
3280 goto skip_external_and_shadow_checks;
3283 /* All declarations with external linkage, and all external
3284 references, go in the external scope, no matter what scope is
3285 current. However, the binding in that scope is ignored for
3286 purposes of normal name lookup. A separate binding structure is
3287 created in the requested scope; this governs the normal
3288 visibility of the symbol.
3290 The binding in the externals scope is used exclusively for
3291 detecting duplicate declarations of the same object, no matter
3292 what scope they are in; this is what we do here. (C99 6.2.7p2:
3293 All declarations that refer to the same object or function shall
3294 have compatible type; otherwise, the behavior is undefined.)
3295 However, in Objective-C, we also want to detect declarations
3296 conflicting with those of the basic types. */
3297 if ((DECL_EXTERNAL (x) || scope == file_scope)
3298 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3300 tree type = TREE_TYPE (x);
3301 tree vistype = NULL_TREE;
3302 tree visdecl = NULL_TREE;
3303 bool type_saved = false;
3304 if (b && !B_IN_EXTERNAL_SCOPE (b)
3305 && VAR_OR_FUNCTION_DECL_P (b->decl)
3306 && DECL_FILE_SCOPE_P (b->decl))
3308 visdecl = b->decl;
3309 vistype = TREE_TYPE (visdecl);
3311 if (scope != file_scope
3312 && !DECL_IN_SYSTEM_HEADER (x))
3313 warning_at (locus, OPT_Wnested_externs,
3314 "nested extern declaration of %qD", x);
3316 while (b && !B_IN_EXTERNAL_SCOPE (b))
3318 /* If this decl might be modified, save its type. This is
3319 done here rather than when the decl is first bound
3320 because the type may change after first binding, through
3321 being completed or through attributes being added. If we
3322 encounter multiple such decls, only the first should have
3323 its type saved; the others will already have had their
3324 proper types saved and the types will not have changed as
3325 their scopes will not have been re-entered. */
3326 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3328 b->u.type = TREE_TYPE (b->decl);
3329 type_saved = true;
3331 if (B_IN_FILE_SCOPE (b)
3332 && VAR_P (b->decl)
3333 && TREE_STATIC (b->decl)
3334 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3335 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3336 && TREE_CODE (type) == ARRAY_TYPE
3337 && TYPE_DOMAIN (type)
3338 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3339 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3341 /* Array type completed in inner scope, which should be
3342 diagnosed if the completion does not have size 1 and
3343 it does not get completed in the file scope. */
3344 b->inner_comp = true;
3346 b = b->shadowed;
3349 /* If a matching external declaration has been found, set its
3350 type to the composite of all the types of that declaration.
3351 After the consistency checks, it will be reset to the
3352 composite of the visible types only. */
3353 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3354 && b->u.type)
3355 TREE_TYPE (b->decl) = b->u.type;
3357 /* The point of the same_translation_unit_p check here is,
3358 we want to detect a duplicate decl for a construct like
3359 foo() { extern bar(); } ... static bar(); but not if
3360 they are in different translation units. In any case,
3361 the static does not go in the externals scope. */
3362 if (b
3363 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3364 && duplicate_decls (x, b->decl))
3366 tree thistype;
3367 if (vistype)
3369 if (comptypes (vistype, type))
3370 thistype = composite_type (vistype, type);
3371 else
3372 thistype = TREE_TYPE (b->decl);
3374 else
3375 thistype = type;
3376 b->u.type = TREE_TYPE (b->decl);
3377 /* Propagate the type attributes to the decl. */
3378 thistype
3379 = build_type_attribute_variant (thistype,
3380 TYPE_ATTRIBUTES (b->u.type));
3381 TREE_TYPE (b->decl) = thistype;
3382 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3383 locus);
3384 return b->decl;
3386 else if (TREE_PUBLIC (x))
3388 if (visdecl && !b && duplicate_decls (x, visdecl))
3390 /* An external declaration at block scope referring to a
3391 visible entity with internal linkage. The composite
3392 type will already be correct for this scope, so we
3393 just need to fall through to make the declaration in
3394 this scope. */
3395 nested = true;
3396 x = visdecl;
3398 else
3400 bind (name, x, external_scope, /*invisible=*/true,
3401 /*nested=*/false, locus);
3402 nested = true;
3407 if (TREE_CODE (x) != PARM_DECL)
3408 warn_if_shadowing (x);
3410 skip_external_and_shadow_checks:
3411 if (TREE_CODE (x) == TYPE_DECL)
3413 /* So this is a typedef, set its underlying type. */
3414 set_underlying_type (x);
3416 /* If X is a typedef defined in the current function, record it
3417 for the purpose of implementing the -Wunused-local-typedefs
3418 warning. */
3419 record_locally_defined_typedef (x);
3422 bind (name, x, scope, /*invisible=*/false, nested, locus);
3424 /* If x's type is incomplete because it's based on a
3425 structure or union which has not yet been fully declared,
3426 attach it to that structure or union type, so we can go
3427 back and complete the variable declaration later, if the
3428 structure or union gets fully declared.
3430 If the input is erroneous, we can have error_mark in the type
3431 slot (e.g. "f(void a, ...)") - that doesn't count as an
3432 incomplete type. */
3433 if (TREE_TYPE (x) != error_mark_node
3434 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3436 tree element = TREE_TYPE (x);
3438 while (TREE_CODE (element) == ARRAY_TYPE)
3439 element = TREE_TYPE (element);
3440 element = TYPE_MAIN_VARIANT (element);
3442 if ((RECORD_OR_UNION_TYPE_P (element)
3443 || TREE_CODE (element) == ENUMERAL_TYPE)
3444 && (TREE_CODE (x) != TYPE_DECL
3445 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3446 && !COMPLETE_TYPE_P (element))
3447 C_TYPE_INCOMPLETE_VARS (element)
3448 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3450 return x;
3454 /* Issue a warning about implicit function declaration. ID is the function
3455 identifier, OLDDECL is a declaration of the function in a different scope,
3456 or NULL_TREE. */
3458 static void
3459 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3461 if (!warn_implicit_function_declaration)
3462 return;
3464 bool warned;
3465 auto_diagnostic_group d;
3466 name_hint hint;
3467 if (!olddecl)
3468 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3470 if (flag_isoc99)
3472 if (const char *suggestion = hint.suggestion ())
3474 gcc_rich_location richloc (loc);
3475 richloc.add_fixit_replace (suggestion);
3476 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3477 "implicit declaration of function %qE;"
3478 " did you mean %qs?",
3479 id, suggestion);
3481 else
3482 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3483 "implicit declaration of function %qE", id);
3485 else if (const char *suggestion = hint.suggestion ())
3487 gcc_rich_location richloc (loc);
3488 richloc.add_fixit_replace (suggestion);
3489 warned = warning_at
3490 (&richloc, OPT_Wimplicit_function_declaration,
3491 G_("implicit declaration of function %qE; did you mean %qs?"),
3492 id, suggestion);
3494 else
3495 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3496 G_("implicit declaration of function %qE"), id);
3498 if (warned)
3500 /* Whether the olddecl is an undeclared builtin function.
3501 locate_old_decl will not generate a diagnostic for those,
3502 so in that case we want to look elsewhere. */
3503 bool undeclared_builtin = (olddecl
3504 && TREE_CODE (olddecl) == FUNCTION_DECL
3505 && fndecl_built_in_p (olddecl)
3506 && !C_DECL_DECLARED_BUILTIN (olddecl));
3507 if (undeclared_builtin)
3509 const char *header = header_for_builtin_fn (olddecl);
3510 if (header)
3512 rich_location richloc (line_table, loc);
3513 maybe_add_include_fixit (&richloc, header, true);
3514 inform (&richloc,
3515 "include %qs or provide a declaration of %qE",
3516 header, id);
3519 else if (olddecl)
3520 locate_old_decl (olddecl);
3523 if (!warned)
3524 hint.suppress ();
3527 /* Return the name of the header file that declares built-in function
3528 FNDECL, or null if either we don't know or don't expect to see an
3529 explicit declaration. */
3531 static const char *
3532 header_for_builtin_fn (tree fndecl)
3534 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3535 return NULL;
3537 switch (DECL_FUNCTION_CODE (fndecl))
3539 CASE_FLT_FN (BUILT_IN_ACOS):
3540 CASE_FLT_FN (BUILT_IN_ACOSH):
3541 CASE_FLT_FN (BUILT_IN_ASIN):
3542 CASE_FLT_FN (BUILT_IN_ASINH):
3543 CASE_FLT_FN (BUILT_IN_ATAN):
3544 CASE_FLT_FN (BUILT_IN_ATANH):
3545 CASE_FLT_FN (BUILT_IN_ATAN2):
3546 CASE_FLT_FN (BUILT_IN_CBRT):
3547 CASE_FLT_FN (BUILT_IN_CEIL):
3548 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3549 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3550 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3551 CASE_FLT_FN (BUILT_IN_COS):
3552 CASE_FLT_FN (BUILT_IN_COSH):
3553 CASE_FLT_FN (BUILT_IN_ERF):
3554 CASE_FLT_FN (BUILT_IN_ERFC):
3555 CASE_FLT_FN (BUILT_IN_EXP):
3556 CASE_FLT_FN (BUILT_IN_EXP2):
3557 CASE_FLT_FN (BUILT_IN_EXPM1):
3558 CASE_FLT_FN (BUILT_IN_FABS):
3559 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3560 CASE_FLT_FN (BUILT_IN_FDIM):
3561 CASE_FLT_FN (BUILT_IN_FLOOR):
3562 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3563 CASE_FLT_FN (BUILT_IN_FMA):
3564 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3565 CASE_FLT_FN (BUILT_IN_FMAX):
3566 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3567 CASE_FLT_FN (BUILT_IN_FMIN):
3568 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3569 CASE_FLT_FN (BUILT_IN_FMOD):
3570 CASE_FLT_FN (BUILT_IN_FREXP):
3571 CASE_FLT_FN (BUILT_IN_HYPOT):
3572 CASE_FLT_FN (BUILT_IN_ILOGB):
3573 CASE_FLT_FN (BUILT_IN_LDEXP):
3574 CASE_FLT_FN (BUILT_IN_LGAMMA):
3575 CASE_FLT_FN (BUILT_IN_LLRINT):
3576 CASE_FLT_FN (BUILT_IN_LLROUND):
3577 CASE_FLT_FN (BUILT_IN_LOG):
3578 CASE_FLT_FN (BUILT_IN_LOG10):
3579 CASE_FLT_FN (BUILT_IN_LOG1P):
3580 CASE_FLT_FN (BUILT_IN_LOG2):
3581 CASE_FLT_FN (BUILT_IN_LOGB):
3582 CASE_FLT_FN (BUILT_IN_LRINT):
3583 CASE_FLT_FN (BUILT_IN_LROUND):
3584 CASE_FLT_FN (BUILT_IN_MODF):
3585 CASE_FLT_FN (BUILT_IN_NAN):
3586 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3587 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3588 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3589 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3590 CASE_FLT_FN (BUILT_IN_POW):
3591 CASE_FLT_FN (BUILT_IN_REMAINDER):
3592 CASE_FLT_FN (BUILT_IN_REMQUO):
3593 CASE_FLT_FN (BUILT_IN_RINT):
3594 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3595 CASE_FLT_FN (BUILT_IN_ROUND):
3596 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3597 CASE_FLT_FN (BUILT_IN_SCALBLN):
3598 CASE_FLT_FN (BUILT_IN_SCALBN):
3599 CASE_FLT_FN (BUILT_IN_SIN):
3600 CASE_FLT_FN (BUILT_IN_SINH):
3601 CASE_FLT_FN (BUILT_IN_SINCOS):
3602 CASE_FLT_FN (BUILT_IN_SQRT):
3603 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3604 CASE_FLT_FN (BUILT_IN_TAN):
3605 CASE_FLT_FN (BUILT_IN_TANH):
3606 CASE_FLT_FN (BUILT_IN_TGAMMA):
3607 CASE_FLT_FN (BUILT_IN_TRUNC):
3608 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3609 case BUILT_IN_ISINF:
3610 case BUILT_IN_ISNAN:
3611 return "<math.h>";
3612 CASE_FLT_FN (BUILT_IN_CABS):
3613 CASE_FLT_FN (BUILT_IN_CACOS):
3614 CASE_FLT_FN (BUILT_IN_CACOSH):
3615 CASE_FLT_FN (BUILT_IN_CARG):
3616 CASE_FLT_FN (BUILT_IN_CASIN):
3617 CASE_FLT_FN (BUILT_IN_CASINH):
3618 CASE_FLT_FN (BUILT_IN_CATAN):
3619 CASE_FLT_FN (BUILT_IN_CATANH):
3620 CASE_FLT_FN (BUILT_IN_CCOS):
3621 CASE_FLT_FN (BUILT_IN_CCOSH):
3622 CASE_FLT_FN (BUILT_IN_CEXP):
3623 CASE_FLT_FN (BUILT_IN_CIMAG):
3624 CASE_FLT_FN (BUILT_IN_CLOG):
3625 CASE_FLT_FN (BUILT_IN_CONJ):
3626 CASE_FLT_FN (BUILT_IN_CPOW):
3627 CASE_FLT_FN (BUILT_IN_CPROJ):
3628 CASE_FLT_FN (BUILT_IN_CREAL):
3629 CASE_FLT_FN (BUILT_IN_CSIN):
3630 CASE_FLT_FN (BUILT_IN_CSINH):
3631 CASE_FLT_FN (BUILT_IN_CSQRT):
3632 CASE_FLT_FN (BUILT_IN_CTAN):
3633 CASE_FLT_FN (BUILT_IN_CTANH):
3634 return "<complex.h>";
3635 case BUILT_IN_MEMCHR:
3636 case BUILT_IN_MEMCMP:
3637 case BUILT_IN_MEMCPY:
3638 case BUILT_IN_MEMMOVE:
3639 case BUILT_IN_MEMSET:
3640 case BUILT_IN_STRCAT:
3641 case BUILT_IN_STRCHR:
3642 case BUILT_IN_STRCMP:
3643 case BUILT_IN_STRCPY:
3644 case BUILT_IN_STRCSPN:
3645 case BUILT_IN_STRLEN:
3646 case BUILT_IN_STRNCAT:
3647 case BUILT_IN_STRNCMP:
3648 case BUILT_IN_STRNCPY:
3649 case BUILT_IN_STRPBRK:
3650 case BUILT_IN_STRRCHR:
3651 case BUILT_IN_STRSPN:
3652 case BUILT_IN_STRSTR:
3653 return "<string.h>";
3654 case BUILT_IN_FPRINTF:
3655 case BUILT_IN_PUTC:
3656 case BUILT_IN_FPUTC:
3657 case BUILT_IN_FPUTS:
3658 case BUILT_IN_FSCANF:
3659 case BUILT_IN_FWRITE:
3660 case BUILT_IN_PRINTF:
3661 case BUILT_IN_PUTCHAR:
3662 case BUILT_IN_PUTS:
3663 case BUILT_IN_SCANF:
3664 case BUILT_IN_SNPRINTF:
3665 case BUILT_IN_SPRINTF:
3666 case BUILT_IN_SSCANF:
3667 case BUILT_IN_VFPRINTF:
3668 case BUILT_IN_VFSCANF:
3669 case BUILT_IN_VPRINTF:
3670 case BUILT_IN_VSCANF:
3671 case BUILT_IN_VSNPRINTF:
3672 case BUILT_IN_VSPRINTF:
3673 case BUILT_IN_VSSCANF:
3674 return "<stdio.h>";
3675 case BUILT_IN_ISALNUM:
3676 case BUILT_IN_ISALPHA:
3677 case BUILT_IN_ISBLANK:
3678 case BUILT_IN_ISCNTRL:
3679 case BUILT_IN_ISDIGIT:
3680 case BUILT_IN_ISGRAPH:
3681 case BUILT_IN_ISLOWER:
3682 case BUILT_IN_ISPRINT:
3683 case BUILT_IN_ISPUNCT:
3684 case BUILT_IN_ISSPACE:
3685 case BUILT_IN_ISUPPER:
3686 case BUILT_IN_ISXDIGIT:
3687 case BUILT_IN_TOLOWER:
3688 case BUILT_IN_TOUPPER:
3689 return "<ctype.h>";
3690 case BUILT_IN_ISWALNUM:
3691 case BUILT_IN_ISWALPHA:
3692 case BUILT_IN_ISWBLANK:
3693 case BUILT_IN_ISWCNTRL:
3694 case BUILT_IN_ISWDIGIT:
3695 case BUILT_IN_ISWGRAPH:
3696 case BUILT_IN_ISWLOWER:
3697 case BUILT_IN_ISWPRINT:
3698 case BUILT_IN_ISWPUNCT:
3699 case BUILT_IN_ISWSPACE:
3700 case BUILT_IN_ISWUPPER:
3701 case BUILT_IN_ISWXDIGIT:
3702 case BUILT_IN_TOWLOWER:
3703 case BUILT_IN_TOWUPPER:
3704 return "<wctype.h>";
3705 case BUILT_IN_ABORT:
3706 case BUILT_IN_ABS:
3707 case BUILT_IN_CALLOC:
3708 case BUILT_IN_EXIT:
3709 case BUILT_IN_FREE:
3710 case BUILT_IN_LABS:
3711 case BUILT_IN_LLABS:
3712 case BUILT_IN_MALLOC:
3713 case BUILT_IN_REALLOC:
3714 case BUILT_IN__EXIT2:
3715 case BUILT_IN_ALIGNED_ALLOC:
3716 return "<stdlib.h>";
3717 case BUILT_IN_IMAXABS:
3718 return "<inttypes.h>";
3719 case BUILT_IN_STRFTIME:
3720 return "<time.h>";
3721 default:
3722 return NULL;
3726 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3727 function of type int (). */
3729 tree
3730 implicitly_declare (location_t loc, tree functionid)
3732 struct c_binding *b;
3733 tree decl = NULL_TREE;
3734 tree asmspec_tree;
3736 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3738 if (B_IN_SCOPE (b, external_scope))
3740 decl = b->decl;
3741 break;
3745 if (decl)
3747 if (TREE_CODE (decl) != FUNCTION_DECL)
3748 return decl;
3750 /* FIXME: Objective-C has weird not-really-builtin functions
3751 which are supposed to be visible automatically. They wind up
3752 in the external scope because they're pushed before the file
3753 scope gets created. Catch this here and rebind them into the
3754 file scope. */
3755 if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3757 bind (functionid, decl, file_scope,
3758 /*invisible=*/false, /*nested=*/true,
3759 DECL_SOURCE_LOCATION (decl));
3760 return decl;
3762 else
3764 tree newtype = default_function_type;
3765 if (b->u.type)
3766 TREE_TYPE (decl) = b->u.type;
3767 /* Implicit declaration of a function already declared
3768 (somehow) in a different scope, or as a built-in.
3769 If this is the first time this has happened, warn;
3770 then recycle the old declaration but with the new type. */
3771 if (!C_DECL_IMPLICIT (decl))
3773 implicit_decl_warning (loc, functionid, decl);
3774 C_DECL_IMPLICIT (decl) = 1;
3776 if (fndecl_built_in_p (decl))
3778 newtype = build_type_attribute_variant (newtype,
3779 TYPE_ATTRIBUTES
3780 (TREE_TYPE (decl)));
3781 if (!comptypes (newtype, TREE_TYPE (decl)))
3783 auto_diagnostic_group d;
3784 bool warned = warning_at (loc,
3785 OPT_Wbuiltin_declaration_mismatch,
3786 "incompatible implicit "
3787 "declaration of built-in "
3788 "function %qD", decl);
3789 /* See if we can hint which header to include. */
3790 const char *header = header_for_builtin_fn (decl);
3791 if (header != NULL && warned)
3793 rich_location richloc (line_table, loc);
3794 maybe_add_include_fixit (&richloc, header, true);
3795 inform (&richloc,
3796 "include %qs or provide a declaration of %qD",
3797 header, decl);
3799 newtype = TREE_TYPE (decl);
3802 else
3804 if (!comptypes (newtype, TREE_TYPE (decl)))
3806 auto_diagnostic_group d;
3807 error_at (loc, "incompatible implicit declaration of "
3808 "function %qD", decl);
3809 locate_old_decl (decl);
3812 b->u.type = TREE_TYPE (decl);
3813 TREE_TYPE (decl) = newtype;
3814 bind (functionid, decl, current_scope,
3815 /*invisible=*/false, /*nested=*/true,
3816 DECL_SOURCE_LOCATION (decl));
3817 return decl;
3821 /* Not seen before. */
3822 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3823 DECL_EXTERNAL (decl) = 1;
3824 TREE_PUBLIC (decl) = 1;
3825 C_DECL_IMPLICIT (decl) = 1;
3826 implicit_decl_warning (loc, functionid, 0);
3827 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3828 if (asmspec_tree)
3829 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3831 /* C89 says implicit declarations are in the innermost block.
3832 So we record the decl in the standard fashion. */
3833 decl = pushdecl (decl);
3835 /* No need to call objc_check_decl here - it's a function type. */
3836 rest_of_decl_compilation (decl, 0, 0);
3838 /* Write a record describing this implicit function declaration
3839 to the prototypes file (if requested). */
3840 gen_aux_info_record (decl, 0, 1, 0);
3842 /* Possibly apply some default attributes to this implicit declaration. */
3843 decl_attributes (&decl, NULL_TREE, 0);
3845 return decl;
3848 /* Issue an error message for a reference to an undeclared variable
3849 ID, including a reference to a builtin outside of function-call
3850 context. Establish a binding of the identifier to error_mark_node
3851 in an appropriate scope, which will suppress further errors for the
3852 same identifier. The error message should be given location LOC. */
3853 void
3854 undeclared_variable (location_t loc, tree id)
3856 static bool already = false;
3857 struct c_scope *scope;
3859 auto_diagnostic_group d;
3860 if (current_function_decl == NULL_TREE)
3862 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3863 if (const char *suggestion = guessed_id.suggestion ())
3865 gcc_rich_location richloc (loc);
3866 richloc.add_fixit_replace (suggestion);
3867 error_at (&richloc,
3868 "%qE undeclared here (not in a function);"
3869 " did you mean %qs?",
3870 id, suggestion);
3872 else
3873 error_at (loc, "%qE undeclared here (not in a function)", id);
3874 scope = current_scope;
3876 else
3878 if (!objc_diagnose_private_ivar (id))
3880 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3881 if (const char *suggestion = guessed_id.suggestion ())
3883 gcc_rich_location richloc (loc);
3884 richloc.add_fixit_replace (suggestion);
3885 error_at (&richloc,
3886 "%qE undeclared (first use in this function);"
3887 " did you mean %qs?",
3888 id, suggestion);
3890 else
3891 error_at (loc, "%qE undeclared (first use in this function)", id);
3893 if (!already)
3895 inform (loc, "each undeclared identifier is reported only"
3896 " once for each function it appears in");
3897 already = true;
3900 /* If we are parsing old-style parameter decls, current_function_decl
3901 will be nonnull but current_function_scope will be null. */
3902 scope = current_function_scope ? current_function_scope : current_scope;
3904 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3905 UNKNOWN_LOCATION);
3908 /* Subroutine of lookup_label, declare_label, define_label: construct a
3909 LABEL_DECL with all the proper frills. Also create a struct
3910 c_label_vars initialized for the current scope. */
3912 static tree
3913 make_label (location_t location, tree name, bool defining,
3914 struct c_label_vars **p_label_vars)
3916 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3917 DECL_CONTEXT (label) = current_function_decl;
3918 SET_DECL_MODE (label, VOIDmode);
3920 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3921 label_vars->shadowed = NULL;
3922 set_spot_bindings (&label_vars->label_bindings, defining);
3923 label_vars->decls_in_scope = make_tree_vector ();
3924 label_vars->gotos = NULL;
3925 *p_label_vars = label_vars;
3927 return label;
3930 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3931 Create one if none exists so far for the current function.
3932 This is called when a label is used in a goto expression or
3933 has its address taken. */
3935 tree
3936 lookup_label (tree name)
3938 tree label;
3939 struct c_label_vars *label_vars;
3941 if (current_function_scope == 0)
3943 error ("label %qE referenced outside of any function", name);
3944 return NULL_TREE;
3947 /* Use a label already defined or ref'd with this name, but not if
3948 it is inherited from a containing function and wasn't declared
3949 using __label__. */
3950 label = I_LABEL_DECL (name);
3951 if (label && (DECL_CONTEXT (label) == current_function_decl
3952 || C_DECLARED_LABEL_FLAG (label)))
3954 /* If the label has only been declared, update its apparent
3955 location to point here, for better diagnostics if it
3956 turns out not to have been defined. */
3957 if (DECL_INITIAL (label) == NULL_TREE)
3958 DECL_SOURCE_LOCATION (label) = input_location;
3959 return label;
3962 /* No label binding for that identifier; make one. */
3963 label = make_label (input_location, name, false, &label_vars);
3965 /* Ordinary labels go in the current function scope. */
3966 bind_label (name, label, current_function_scope, label_vars);
3968 return label;
3971 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3972 to LABEL. */
3974 static void
3975 warn_about_goto (location_t goto_loc, tree label, tree decl)
3977 auto_diagnostic_group d;
3978 if (c_type_variably_modified_p (TREE_TYPE (decl)))
3979 error_at (goto_loc,
3980 "jump into scope of identifier with variably modified type");
3981 else
3982 if (!warning_at (goto_loc, OPT_Wjump_misses_init,
3983 "jump skips variable initialization"))
3984 return;
3985 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3986 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3989 /* Look up a label because of a goto statement. This is like
3990 lookup_label, but also issues any appropriate warnings. */
3992 tree
3993 lookup_label_for_goto (location_t loc, tree name)
3995 tree label;
3996 struct c_label_vars *label_vars;
3997 unsigned int ix;
3998 tree decl;
4000 label = lookup_label (name);
4001 if (label == NULL_TREE)
4002 return NULL_TREE;
4004 /* If we are jumping to a different function, we can't issue any
4005 useful warnings. */
4006 if (DECL_CONTEXT (label) != current_function_decl)
4008 gcc_assert (C_DECLARED_LABEL_FLAG (label));
4009 return label;
4012 label_vars = I_LABEL_BINDING (name)->u.label;
4014 /* If the label has not yet been defined, then push this goto on a
4015 list for possible later warnings. */
4016 if (label_vars->label_bindings.scope == NULL)
4018 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
4020 g->loc = loc;
4021 set_spot_bindings (&g->goto_bindings, true);
4022 vec_safe_push (label_vars->gotos, g);
4023 return label;
4026 /* If there are any decls in label_vars->decls_in_scope, then this
4027 goto has missed the declaration of the decl. This happens for a
4028 case like
4029 int i = 1;
4030 lab:
4032 goto lab;
4033 Issue a warning or error. */
4034 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
4035 warn_about_goto (loc, label, decl);
4037 if (label_vars->label_bindings.left_stmt_expr)
4039 auto_diagnostic_group d;
4040 error_at (loc, "jump into statement expression");
4041 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
4044 return label;
4047 /* Make a label named NAME in the current function, shadowing silently
4048 any that may be inherited from containing functions or containing
4049 scopes. This is called for __label__ declarations. */
4051 tree
4052 declare_label (tree name)
4054 struct c_binding *b = I_LABEL_BINDING (name);
4055 tree label;
4056 struct c_label_vars *label_vars;
4058 /* Check to make sure that the label hasn't already been declared
4059 at this scope */
4060 if (b && B_IN_CURRENT_SCOPE (b))
4062 auto_diagnostic_group d;
4063 error ("duplicate label declaration %qE", name);
4064 locate_old_decl (b->decl);
4066 /* Just use the previous declaration. */
4067 return b->decl;
4070 label = make_label (input_location, name, false, &label_vars);
4071 C_DECLARED_LABEL_FLAG (label) = 1;
4073 /* Declared labels go in the current scope. */
4074 bind_label (name, label, current_scope, label_vars);
4076 return label;
4079 /* When we define a label, issue any appropriate warnings if there are
4080 any gotos earlier in the function which jump to this label. */
4082 static void
4083 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4085 unsigned int ix;
4086 struct c_goto_bindings *g;
4088 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4090 struct c_binding *b;
4091 struct c_scope *scope;
4093 /* We have a goto to this label. The goto is going forward. In
4094 g->scope, the goto is going to skip any binding which was
4095 defined after g->bindings_in_scope. */
4096 if (g->goto_bindings.scope->has_jump_unsafe_decl)
4098 for (b = g->goto_bindings.scope->bindings;
4099 b != g->goto_bindings.bindings_in_scope;
4100 b = b->prev)
4102 if (decl_jump_unsafe (b->decl))
4103 warn_about_goto (g->loc, label, b->decl);
4107 /* We also need to warn about decls defined in any scopes
4108 between the scope of the label and the scope of the goto. */
4109 for (scope = label_vars->label_bindings.scope;
4110 scope != g->goto_bindings.scope;
4111 scope = scope->outer)
4113 gcc_assert (scope != NULL);
4114 if (scope->has_jump_unsafe_decl)
4116 if (scope == label_vars->label_bindings.scope)
4117 b = label_vars->label_bindings.bindings_in_scope;
4118 else
4119 b = scope->bindings;
4120 for (; b != NULL; b = b->prev)
4122 if (decl_jump_unsafe (b->decl))
4123 warn_about_goto (g->loc, label, b->decl);
4128 if (g->goto_bindings.stmt_exprs > 0)
4130 auto_diagnostic_group d;
4131 error_at (g->loc, "jump into statement expression");
4132 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4133 label);
4137 /* Now that the label is defined, we will issue warnings about
4138 subsequent gotos to this label when we see them. */
4139 vec_safe_truncate (label_vars->gotos, 0);
4140 label_vars->gotos = NULL;
4143 /* Define a label, specifying the location in the source file.
4144 Return the LABEL_DECL node for the label, if the definition is valid.
4145 Otherwise return NULL_TREE. */
4147 tree
4148 define_label (location_t location, tree name)
4150 /* Find any preexisting label with this name. It is an error
4151 if that label has already been defined in this function, or
4152 if there is a containing function with a declared label with
4153 the same name. */
4154 tree label = I_LABEL_DECL (name);
4156 if (label
4157 && ((DECL_CONTEXT (label) == current_function_decl
4158 && DECL_INITIAL (label) != NULL_TREE)
4159 || (DECL_CONTEXT (label) != current_function_decl
4160 && C_DECLARED_LABEL_FLAG (label))))
4162 auto_diagnostic_group d;
4163 error_at (location, "duplicate label %qD", label);
4164 locate_old_decl (label);
4165 return NULL_TREE;
4167 else if (label && DECL_CONTEXT (label) == current_function_decl)
4169 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4171 /* The label has been used or declared already in this function,
4172 but not defined. Update its location to point to this
4173 definition. */
4174 DECL_SOURCE_LOCATION (label) = location;
4175 set_spot_bindings (&label_vars->label_bindings, true);
4177 /* Issue warnings as required about any goto statements from
4178 earlier in the function. */
4179 check_earlier_gotos (label, label_vars);
4181 else
4183 struct c_label_vars *label_vars;
4185 /* No label binding for that identifier; make one. */
4186 label = make_label (location, name, true, &label_vars);
4188 /* Ordinary labels go in the current function scope. */
4189 bind_label (name, label, current_function_scope, label_vars);
4192 if (!in_system_header_at (input_location) && lookup_name (name))
4193 warning_at (location, OPT_Wtraditional,
4194 "traditional C lacks a separate namespace "
4195 "for labels, identifier %qE conflicts", name);
4197 /* Mark label as having been defined. */
4198 DECL_INITIAL (label) = error_mark_node;
4199 return label;
4202 /* Get the bindings for a new switch statement. This is used to issue
4203 warnings as appropriate for jumps from the switch to case or
4204 default labels. */
4206 struct c_spot_bindings *
4207 c_get_switch_bindings (void)
4209 struct c_spot_bindings *switch_bindings;
4211 switch_bindings = XNEW (struct c_spot_bindings);
4212 set_spot_bindings (switch_bindings, true);
4213 return switch_bindings;
4216 void
4217 c_release_switch_bindings (struct c_spot_bindings *bindings)
4219 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4220 XDELETE (bindings);
4223 /* This is called at the point of a case or default label to issue
4224 warnings about decls as needed. It returns true if it found an
4225 error, not just a warning. */
4227 bool
4228 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4229 location_t switch_loc, location_t case_loc)
4231 bool saw_error;
4232 struct c_scope *scope;
4234 saw_error = false;
4235 for (scope = current_scope;
4236 scope != switch_bindings->scope;
4237 scope = scope->outer)
4239 struct c_binding *b;
4241 gcc_assert (scope != NULL);
4243 if (!scope->has_jump_unsafe_decl)
4244 continue;
4246 for (b = scope->bindings; b != NULL; b = b->prev)
4248 if (decl_jump_unsafe (b->decl))
4250 auto_diagnostic_group d;
4251 bool emitted;
4252 if (c_type_variably_modified_p (TREE_TYPE (b->decl)))
4254 saw_error = true;
4255 error_at (case_loc,
4256 ("switch jumps into scope of identifier with "
4257 "variably modified type"));
4258 emitted = true;
4260 else
4261 emitted
4262 = warning_at (case_loc, OPT_Wjump_misses_init,
4263 "switch jumps over variable initialization");
4264 if (emitted)
4266 inform (switch_loc, "switch starts here");
4267 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4268 b->decl);
4274 if (switch_bindings->stmt_exprs > 0)
4276 saw_error = true;
4277 auto_diagnostic_group d;
4278 error_at (case_loc, "switch jumps into statement expression");
4279 inform (switch_loc, "switch starts here");
4282 return saw_error;
4285 /* Given NAME, an IDENTIFIER_NODE,
4286 return the structure (or union or enum) definition for that name.
4287 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4288 CODE says which kind of type the caller wants;
4289 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4290 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4291 location where the tag was defined.
4292 If the wrong kind of type is found, an error is reported. */
4294 static tree
4295 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4296 location_t *ploc)
4298 struct c_binding *b = I_TAG_BINDING (name);
4299 bool thislevel = false;
4301 if (!b || !b->decl)
4302 return NULL_TREE;
4304 /* We only care about whether it's in this level if
4305 thislevel_only was set or it might be a type clash. */
4306 if (thislevel_only || TREE_CODE (b->decl) != code)
4308 /* For our purposes, a tag in the external scope is the same as
4309 a tag in the file scope. (Primarily relevant to Objective-C
4310 and its builtin structure tags, which get pushed before the
4311 file scope is created.) */
4312 if (B_IN_CURRENT_SCOPE (b)
4313 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4314 thislevel = true;
4317 if (thislevel_only && !thislevel)
4318 return NULL_TREE;
4320 if (TREE_CODE (b->decl) != code)
4322 /* Definition isn't the kind we were looking for. */
4323 pending_invalid_xref = name;
4324 pending_invalid_xref_location = input_location;
4326 /* If in the same binding level as a declaration as a tag
4327 of a different type, this must not be allowed to
4328 shadow that tag, so give the error immediately.
4329 (For example, "struct foo; union foo;" is invalid.) */
4330 if (thislevel)
4331 pending_xref_error ();
4334 if (ploc != NULL)
4335 *ploc = b->locus;
4337 return b->decl;
4340 /* Return true if a definition exists for NAME with code CODE. */
4342 bool
4343 tag_exists_p (enum tree_code code, tree name)
4345 struct c_binding *b = I_TAG_BINDING (name);
4347 if (b == NULL || b->decl == NULL_TREE)
4348 return false;
4349 return TREE_CODE (b->decl) == code;
4352 /* Print an error message now
4353 for a recent invalid struct, union or enum cross reference.
4354 We don't print them immediately because they are not invalid
4355 when used in the `struct foo;' construct for shadowing. */
4357 void
4358 pending_xref_error (void)
4360 if (pending_invalid_xref != NULL_TREE)
4361 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4362 pending_invalid_xref);
4363 pending_invalid_xref = NULL_TREE;
4367 /* Look up NAME in the current scope and its superiors
4368 in the namespace of variables, functions and typedefs.
4369 Return a ..._DECL node of some kind representing its definition,
4370 or return NULL_TREE if it is undefined. */
4372 tree
4373 lookup_name (tree name)
4375 struct c_binding *b = I_SYMBOL_BINDING (name);
4376 if (b && !b->invisible)
4378 maybe_record_typedef_use (b->decl);
4379 return b->decl;
4381 return NULL_TREE;
4384 /* Similar to `lookup_name' but look only at the indicated scope. */
4386 static tree
4387 lookup_name_in_scope (tree name, struct c_scope *scope)
4389 struct c_binding *b;
4391 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4392 if (B_IN_SCOPE (b, scope))
4393 return b->decl;
4394 return NULL_TREE;
4397 /* Look for the closest match for NAME within the currently valid
4398 scopes.
4400 This finds the identifier with the lowest Levenshtein distance to
4401 NAME. If there are multiple candidates with equal minimal distance,
4402 the first one found is returned. Scopes are searched from innermost
4403 outwards, and within a scope in reverse order of declaration, thus
4404 benefiting candidates "near" to the current scope.
4406 The function also looks for similar macro names to NAME, since a
4407 misspelled macro name will not be expanded, and hence looks like an
4408 identifier to the C frontend.
4410 It also looks for start_typename keywords, to detect "singed" vs "signed"
4411 typos.
4413 Use LOC for any deferred diagnostics. */
4415 name_hint
4416 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4418 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4420 /* First, try some well-known names in the C standard library, in case
4421 the user forgot a #include. */
4422 const char *header_hint
4423 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4425 if (header_hint)
4426 return name_hint (NULL,
4427 new suggest_missing_header (loc,
4428 IDENTIFIER_POINTER (name),
4429 header_hint));
4431 /* Only suggest names reserved for the implementation if NAME begins
4432 with an underscore. */
4433 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4435 best_match<tree, tree> bm (name);
4437 /* Look within currently valid scopes. */
4438 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4439 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4441 if (!binding->id || binding->invisible)
4442 continue;
4443 if (binding->decl == error_mark_node)
4444 continue;
4445 /* Don't use bindings from implicitly declared functions,
4446 as they were likely misspellings themselves. */
4447 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4448 if (C_DECL_IMPLICIT (binding->decl))
4449 continue;
4450 /* Don't suggest names that are reserved for use by the
4451 implementation, unless NAME began with an underscore. */
4452 if (!consider_implementation_names)
4454 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4455 if (name_reserved_for_implementation_p (suggestion_str))
4456 continue;
4458 switch (kind)
4460 case FUZZY_LOOKUP_TYPENAME:
4461 if (TREE_CODE (binding->decl) != TYPE_DECL)
4462 continue;
4463 break;
4465 case FUZZY_LOOKUP_FUNCTION_NAME:
4466 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4468 /* Allow function pointers. */
4469 if ((VAR_P (binding->decl)
4470 || TREE_CODE (binding->decl) == PARM_DECL)
4471 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4472 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4473 == FUNCTION_TYPE))
4474 break;
4475 continue;
4477 break;
4479 default:
4480 break;
4482 bm.consider (binding->id);
4485 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4487 x = SOME_OTHER_MACRO (y);
4488 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4489 as a misspelled identifier.
4491 Use the best distance so far so that a candidate is only set if
4492 a macro is better than anything so far. This allows early rejection
4493 (without calculating the edit distance) of macro names that must have
4494 distance >= bm.get_best_distance (), and means that we only get a
4495 non-NULL result for best_macro_match if it's better than any of
4496 the identifiers already checked, which avoids needless creation
4497 of identifiers for macro hashnodes. */
4498 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4499 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4500 /* If a macro is the closest so far to NAME, use it, creating an
4501 identifier tree node for it. */
4502 if (best_macro)
4504 const char *id = (const char *)best_macro->ident.str;
4505 tree macro_as_identifier
4506 = get_identifier_with_length (id, best_macro->ident.len);
4507 bm.set_best_so_far (macro_as_identifier,
4508 bmm.get_best_distance (),
4509 bmm.get_best_candidate_length ());
4512 /* Try the "start_typename" keywords to detect
4513 "singed" vs "signed" typos. */
4514 if (kind == FUZZY_LOOKUP_TYPENAME)
4516 for (unsigned i = 0; i < num_c_common_reswords; i++)
4518 const c_common_resword *resword = &c_common_reswords[i];
4519 if (!c_keyword_starts_typename (resword->rid))
4520 continue;
4521 tree resword_identifier = ridpointers [resword->rid];
4522 if (!resword_identifier)
4523 continue;
4524 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4525 bm.consider (resword_identifier);
4529 tree best = bm.get_best_meaningful_candidate ();
4530 if (best)
4531 return name_hint (IDENTIFIER_POINTER (best), NULL);
4532 else
4533 return name_hint (NULL, NULL);
4537 /* Handle the standard [[nodiscard]] attribute. */
4539 static tree
4540 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4541 int /*flags*/, bool *no_add_attrs)
4543 if (TREE_CODE (*node) == FUNCTION_DECL)
4545 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4546 warning_at (DECL_SOURCE_LOCATION (*node),
4547 OPT_Wattributes, "%qE attribute applied to %qD with void "
4548 "return type", name, *node);
4550 else if (RECORD_OR_UNION_TYPE_P (*node)
4551 || TREE_CODE (*node) == ENUMERAL_TYPE)
4552 /* OK */;
4553 else
4555 pedwarn (input_location,
4556 OPT_Wattributes, "%qE attribute can only be applied to "
4557 "functions or to structure, union or enumeration types", name);
4558 *no_add_attrs = true;
4560 return NULL_TREE;
4563 /* Handle the standard [[noreturn]] attribute. */
4565 static tree
4566 handle_std_noreturn_attribute (tree *node, tree name, tree args,
4567 int flags, bool *no_add_attrs)
4569 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4570 only applies to functions, not function pointers. */
4571 if (TREE_CODE (*node) == FUNCTION_DECL)
4572 return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4573 else
4575 pedwarn (input_location, OPT_Wattributes,
4576 "standard %qE attribute can only be applied to functions",
4577 name);
4578 *no_add_attrs = true;
4579 return NULL_TREE;
4583 /* Table of supported standard (C2x) attributes. */
4584 const struct attribute_spec std_attribute_table[] =
4586 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4587 affects_type_identity, handler, exclude } */
4588 { "_Noreturn", 0, 0, false, false, false, false,
4589 handle_std_noreturn_attribute, NULL },
4590 { "deprecated", 0, 1, false, false, false, false,
4591 handle_deprecated_attribute, NULL },
4592 { "fallthrough", 0, 0, false, false, false, false,
4593 handle_fallthrough_attribute, NULL },
4594 { "maybe_unused", 0, 0, false, false, false, false,
4595 handle_unused_attribute, NULL },
4596 { "nodiscard", 0, 1, false, false, false, false,
4597 handle_nodiscard_attribute, NULL },
4598 { "noreturn", 0, 0, false, false, false, false,
4599 handle_std_noreturn_attribute, NULL },
4600 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4603 /* Create the predefined scalar types of C,
4604 and some nodes representing standard constants (0, 1, (void *) 0).
4605 Initialize the global scope.
4606 Make definitions for built-in primitive functions. */
4608 void
4609 c_init_decl_processing (void)
4611 location_t save_loc = input_location;
4613 /* Initialize reserved words for parser. */
4614 c_parse_init ();
4616 register_scoped_attributes (std_attribute_table, NULL);
4618 current_function_decl = NULL_TREE;
4620 gcc_obstack_init (&parser_obstack);
4622 /* Make the externals scope. */
4623 push_scope ();
4624 external_scope = current_scope;
4626 /* Declarations from c_common_nodes_and_builtins must not be associated
4627 with this input file, lest we get differences between using and not
4628 using preprocessed headers. */
4629 input_location = BUILTINS_LOCATION;
4631 c_common_nodes_and_builtins ();
4633 /* In C, comparisons and TRUTH_* expressions have type int. */
4634 truthvalue_type_node = integer_type_node;
4635 truthvalue_true_node = integer_one_node;
4636 truthvalue_false_node = integer_zero_node;
4638 /* Even in C99, which has a real boolean type. */
4639 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4640 boolean_type_node));
4642 /* C-specific nullptr initialization. */
4643 record_builtin_type (RID_MAX, "nullptr_t", nullptr_type_node);
4644 /* The size and alignment of nullptr_t is the same as for a pointer to
4645 character type. */
4646 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4648 input_location = save_loc;
4650 make_fname_decl = c_make_fname_decl;
4651 start_fname_decls ();
4654 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4655 give the decl, NAME is the initialization string and TYPE_DEP
4656 indicates whether NAME depended on the type of the function. As we
4657 don't yet implement delayed emission of static data, we mark the
4658 decl as emitted so it is not placed in the output. Anything using
4659 it must therefore pull out the STRING_CST initializer directly.
4660 FIXME. */
4662 static tree
4663 c_make_fname_decl (location_t loc, tree id, int type_dep)
4665 const char *name = fname_as_string (type_dep);
4666 tree decl, type, init;
4667 size_t length = strlen (name);
4669 type = build_array_type (char_type_node,
4670 build_index_type (size_int (length)));
4671 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4673 decl = build_decl (loc, VAR_DECL, id, type);
4675 TREE_STATIC (decl) = 1;
4676 TREE_READONLY (decl) = 1;
4677 DECL_ARTIFICIAL (decl) = 1;
4679 init = build_string (length + 1, name);
4680 free (CONST_CAST (char *, name));
4681 TREE_TYPE (init) = type;
4682 DECL_INITIAL (decl) = init;
4684 TREE_USED (decl) = 1;
4686 if (current_function_decl
4687 /* For invalid programs like this:
4689 void foo()
4690 const char* p = __FUNCTION__;
4692 the __FUNCTION__ is believed to appear in K&R style function
4693 parameter declarator. In that case we still don't have
4694 function_scope. */
4695 && current_function_scope)
4697 DECL_CONTEXT (decl) = current_function_decl;
4698 bind (id, decl, current_function_scope,
4699 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4702 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4704 return decl;
4707 tree
4708 c_builtin_function (tree decl)
4710 tree type = TREE_TYPE (decl);
4711 tree id = DECL_NAME (decl);
4713 const char *name = IDENTIFIER_POINTER (id);
4714 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4716 /* Should never be called on a symbol with a preexisting meaning. */
4717 gcc_assert (!I_SYMBOL_BINDING (id));
4719 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4720 UNKNOWN_LOCATION);
4722 /* Builtins in the implementation namespace are made visible without
4723 needing to be explicitly declared. See push_file_scope. */
4724 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4726 DECL_CHAIN (decl) = visible_builtins;
4727 visible_builtins = decl;
4730 return decl;
4733 tree
4734 c_builtin_function_ext_scope (tree decl)
4736 tree type = TREE_TYPE (decl);
4737 tree id = DECL_NAME (decl);
4739 const char *name = IDENTIFIER_POINTER (id);
4740 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4742 if (external_scope)
4743 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4744 UNKNOWN_LOCATION);
4746 /* Builtins in the implementation namespace are made visible without
4747 needing to be explicitly declared. See push_file_scope. */
4748 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4750 DECL_CHAIN (decl) = visible_builtins;
4751 visible_builtins = decl;
4754 return decl;
4757 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4759 tree
4760 c_simulate_builtin_function_decl (tree decl)
4762 tree type = TREE_TYPE (decl);
4763 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4764 return pushdecl (decl);
4767 /* Warn about attributes in a context where they are unused
4768 (attribute-declarations, except for the "fallthrough" case, and
4769 attributes on statements). */
4771 void
4772 c_warn_unused_attributes (tree attrs)
4774 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4775 if (get_attribute_namespace (t) == NULL_TREE)
4776 /* The specifications of standard attributes mean this is a
4777 constraint violation. */
4778 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4779 get_attribute_name (t));
4780 else if (!attribute_ignored_p (t))
4781 warning (OPT_Wattributes, "%qE attribute ignored",
4782 get_attribute_name (t));
4785 /* Warn for standard attributes being applied to a type that is not
4786 being defined, where that is a constraint violation, and return a
4787 list of attributes with them removed. */
4789 tree
4790 c_warn_type_attributes (tree attrs)
4792 tree *attr_ptr = &attrs;
4793 while (*attr_ptr)
4794 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4796 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4797 get_attribute_name (*attr_ptr));
4798 *attr_ptr = TREE_CHAIN (*attr_ptr);
4800 else
4801 attr_ptr = &TREE_CHAIN (*attr_ptr);
4802 return attrs;
4805 /* Called when a declaration is seen that contains no names to declare.
4806 If its type is a reference to a structure, union or enum inherited
4807 from a containing scope, shadow that tag name for the current scope
4808 with a forward reference.
4809 If its type defines a new named structure or union
4810 or defines an enum, it is valid but we need not do anything here.
4811 Otherwise, it is an error. */
4813 void
4814 shadow_tag (const struct c_declspecs *declspecs)
4816 shadow_tag_warned (declspecs, 0);
4819 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4820 but no pedwarn. */
4821 void
4822 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4824 bool found_tag = false;
4826 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4828 tree value = declspecs->type;
4829 enum tree_code code = TREE_CODE (value);
4831 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4832 /* Used to test also that TYPE_SIZE (value) != 0.
4833 That caused warning for `struct foo;' at top level in the file. */
4835 tree name = TYPE_NAME (value);
4836 tree t;
4838 found_tag = true;
4840 if (declspecs->restrict_p)
4842 error ("invalid use of %<restrict%>");
4843 warned = 1;
4846 if (in_underspecified_init)
4848 /* This can only occur with extensions such as statement
4849 expressions, but is still appropriate as an error to
4850 avoid types declared in such a context escaping to
4851 the type of an auto variable. */
4852 error ("%qT declared in underspecified object initializer",
4853 value);
4854 warned = 1;
4857 if (name == NULL_TREE)
4859 if (warned != 1 && code != ENUMERAL_TYPE)
4860 /* Empty unnamed enum OK */
4862 pedwarn (input_location, 0,
4863 "unnamed struct/union that defines no instances");
4864 warned = 1;
4867 else if (declspecs->typespec_kind != ctsk_tagdef
4868 && declspecs->typespec_kind != ctsk_tagfirstref
4869 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4870 && declspecs->storage_class != csc_none)
4872 if (warned != 1)
4873 pedwarn (input_location, 0,
4874 "empty declaration with storage class specifier "
4875 "does not redeclare tag");
4876 warned = 1;
4877 pending_xref_error ();
4879 else if (declspecs->typespec_kind != ctsk_tagdef
4880 && declspecs->typespec_kind != ctsk_tagfirstref
4881 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4882 && (declspecs->const_p
4883 || declspecs->volatile_p
4884 || declspecs->atomic_p
4885 || declspecs->restrict_p
4886 || declspecs->address_space))
4888 if (warned != 1)
4889 pedwarn (input_location, 0,
4890 "empty declaration with type qualifier "
4891 "does not redeclare tag");
4892 warned = 1;
4893 pending_xref_error ();
4895 else if (declspecs->typespec_kind != ctsk_tagdef
4896 && declspecs->typespec_kind != ctsk_tagfirstref
4897 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4898 && declspecs->alignas_p)
4900 if (warned != 1)
4901 pedwarn (input_location, 0,
4902 "empty declaration with %<_Alignas%> "
4903 "does not redeclare tag");
4904 warned = 1;
4905 pending_xref_error ();
4907 else if (declspecs->typespec_kind != ctsk_tagdef
4908 && declspecs->typespec_kind != ctsk_tagfirstref
4909 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4910 && code == ENUMERAL_TYPE
4911 && !declspecs->enum_type_specifier_ref_p)
4913 bool warned_enum = false;
4914 if (warned != 1)
4915 warned_enum = pedwarn (input_location, OPT_Wpedantic,
4916 "empty declaration of %<enum%> type "
4917 "does not redeclare tag");
4918 if (warned_enum)
4919 warned = 1;
4920 pending_xref_error ();
4922 else
4924 pending_invalid_xref = NULL_TREE;
4925 t = lookup_tag (code, name, true, NULL);
4927 if (t == NULL_TREE)
4929 t = make_node (code);
4930 pushtag (input_location, name, t);
4934 else
4936 if (warned != 1 && !in_system_header_at (input_location))
4938 pedwarn (input_location, 0,
4939 "useless type name in empty declaration");
4940 warned = 1;
4944 else if (warned != 1 && !in_system_header_at (input_location)
4945 && declspecs->typedef_p)
4947 pedwarn (input_location, 0, "useless type name in empty declaration");
4948 warned = 1;
4951 pending_invalid_xref = NULL_TREE;
4953 if (declspecs->inline_p)
4955 error ("%<inline%> in empty declaration");
4956 warned = 1;
4959 if (declspecs->noreturn_p)
4961 error ("%<_Noreturn%> in empty declaration");
4962 warned = 1;
4965 if (declspecs->constexpr_p)
4967 error ("%<constexpr%> in empty declaration");
4968 warned = 1;
4971 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4973 error ("%<auto%> in file-scope empty declaration");
4974 warned = 1;
4977 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4979 error ("%<register%> in file-scope empty declaration");
4980 warned = 1;
4983 if (declspecs->enum_type_specifier_ref_p && !warned)
4985 if (declspecs->storage_class != csc_none)
4987 error ("storage class specifier in empty declaration with %<enum%> "
4988 "underlying type");
4989 warned = 1;
4991 else if (declspecs->thread_p)
4993 error ("%qs in empty declaration with %<enum%> underlying type",
4994 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4995 warned = 1;
4997 else if (declspecs->const_p
4998 || declspecs->volatile_p
4999 || declspecs->atomic_p
5000 || declspecs->restrict_p
5001 || declspecs->address_space)
5003 error ("type qualifier in empty declaration with %<enum%> "
5004 "underlying type");
5005 warned = 1;
5007 else if (declspecs->alignas_p)
5009 error ("%<alignas%> in empty declaration with %<enum%> "
5010 "underlying type");
5011 warned = 1;
5015 if (!warned && !in_system_header_at (input_location)
5016 && declspecs->storage_class != csc_none)
5018 warning (0, "useless storage class specifier in empty declaration");
5019 warned = 2;
5022 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
5024 warning (0, "useless %qs in empty declaration",
5025 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5026 warned = 2;
5029 if (!warned
5030 && !in_system_header_at (input_location)
5031 && (declspecs->const_p
5032 || declspecs->volatile_p
5033 || declspecs->atomic_p
5034 || declspecs->restrict_p
5035 || declspecs->address_space))
5037 warning (0, "useless type qualifier in empty declaration");
5038 warned = 2;
5041 if (!warned && !in_system_header_at (input_location)
5042 && declspecs->alignas_p)
5044 warning (0, "useless %<_Alignas%> in empty declaration");
5045 warned = 2;
5048 if (found_tag
5049 && warned == 2
5050 && (declspecs->typespec_kind == ctsk_tagref_attrs
5051 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
5053 /* Standard attributes after the "struct" or "union" keyword are
5054 only permitted when the contents of the type are defined, or
5055 in the form "struct-or-union attribute-specifier-sequence
5056 identifier;". If the ';' was not present, attributes were
5057 diagnosed in the parser. Here, ensure that any other useless
5058 elements of the declaration result in a pedwarn, not just a
5059 warning. Forward declarations of enum types are not part of
5060 standard C, but handle them the same. */
5061 pedwarn (input_location, 0,
5062 "invalid use of attributes in empty declaration");
5063 warned = 1;
5066 if (warned != 1)
5068 if (declspecs->declspecs_seen_p
5069 && !declspecs->non_std_attrs_seen_p)
5070 /* An attribute declaration (but not a fallthrough attribute
5071 declaration, which was handled separately); warn if there
5072 are any attributes being ignored (but not if the attributes
5073 were empty). */
5074 c_warn_unused_attributes (declspecs->attrs);
5075 else if (!found_tag)
5076 pedwarn (input_location, 0, "empty declaration");
5081 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
5082 bits. SPECS represents declaration specifiers that the grammar
5083 only permits to contain type qualifiers and attributes. */
5086 quals_from_declspecs (const struct c_declspecs *specs)
5088 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
5089 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
5090 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
5091 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
5092 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
5093 gcc_assert (!specs->type
5094 && !specs->decl_attr
5095 && specs->typespec_word == cts_none
5096 && specs->storage_class == csc_none
5097 && !specs->typedef_p
5098 && !specs->explicit_signed_p
5099 && !specs->deprecated_p
5100 && !specs->unavailable_p
5101 && !specs->long_p
5102 && !specs->long_long_p
5103 && !specs->short_p
5104 && !specs->signed_p
5105 && !specs->unsigned_p
5106 && !specs->complex_p
5107 && !specs->inline_p
5108 && !specs->noreturn_p
5109 && !specs->thread_p);
5110 return quals;
5113 /* Construct an array declarator. LOC is the location of the
5114 beginning of the array (usually the opening brace). EXPR is the
5115 expression inside [], or NULL_TREE. QUALS are the type qualifiers
5116 inside the [] (to be applied to the pointer to which a parameter
5117 array is converted). STATIC_P is true if "static" is inside the
5118 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
5119 VLA of unspecified length which is nevertheless a complete type,
5120 false otherwise. The field for the contained declarator is left to
5121 be filled in by set_array_declarator_inner. */
5123 struct c_declarator *
5124 build_array_declarator (location_t loc,
5125 tree expr, struct c_declspecs *quals, bool static_p,
5126 bool vla_unspec_p)
5128 struct c_declarator *declarator = XOBNEW (&parser_obstack,
5129 struct c_declarator);
5130 declarator->id_loc = loc;
5131 declarator->kind = cdk_array;
5132 declarator->declarator = 0;
5133 declarator->u.array.dimen = expr;
5134 if (quals)
5136 declarator->u.array.attrs = quals->attrs;
5137 declarator->u.array.quals = quals_from_declspecs (quals);
5139 else
5141 declarator->u.array.attrs = NULL_TREE;
5142 declarator->u.array.quals = 0;
5144 declarator->u.array.static_p = static_p;
5145 declarator->u.array.vla_unspec_p = vla_unspec_p;
5146 if (static_p || quals != NULL)
5147 pedwarn_c90 (loc, OPT_Wpedantic,
5148 "ISO C90 does not support %<static%> or type "
5149 "qualifiers in parameter array declarators");
5150 if (vla_unspec_p)
5151 pedwarn_c90 (loc, OPT_Wpedantic,
5152 "ISO C90 does not support %<[*]%> array declarators");
5153 if (vla_unspec_p)
5155 if (!current_scope->parm_flag)
5157 /* C99 6.7.5.2p4 */
5158 error_at (loc, "%<[*]%> not allowed in other than "
5159 "function prototype scope");
5160 declarator->u.array.vla_unspec_p = false;
5161 return NULL;
5163 current_scope->had_vla_unspec = true;
5165 return declarator;
5168 /* Set the contained declarator of an array declarator. DECL is the
5169 declarator, as constructed by build_array_declarator; INNER is what
5170 appears on the left of the []. */
5172 struct c_declarator *
5173 set_array_declarator_inner (struct c_declarator *decl,
5174 struct c_declarator *inner)
5176 decl->declarator = inner;
5177 return decl;
5180 /* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5181 static bool
5182 flexible_array_member_type_p (const_tree type)
5184 if (TREE_CODE (type) == ARRAY_TYPE
5185 && TYPE_SIZE (type) == NULL_TREE
5186 && TYPE_DOMAIN (type) != NULL_TREE
5187 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5188 return true;
5190 return false;
5193 /* Determine whether TYPE is a one-element array type "[1]". */
5194 static bool
5195 one_element_array_type_p (const_tree type)
5197 if (TREE_CODE (type) != ARRAY_TYPE)
5198 return false;
5199 return integer_zerop (array_type_nelts (type));
5202 /* Determine whether TYPE is a zero-length array type "[0]". */
5203 static bool
5204 zero_length_array_type_p (const_tree type)
5206 if (TREE_CODE (type) == ARRAY_TYPE)
5207 if (tree type_size = TYPE_SIZE_UNIT (type))
5208 if ((integer_zerop (type_size))
5209 && TYPE_DOMAIN (type) != NULL_TREE
5210 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5211 return true;
5212 return false;
5215 /* INIT is a constructor that forms DECL's initializer. If the final
5216 element initializes a flexible array field, add the size of that
5217 initializer to DECL's size. */
5219 static void
5220 add_flexible_array_elts_to_size (tree decl, tree init)
5222 tree elt, type;
5224 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5225 return;
5227 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5228 type = TREE_TYPE (elt);
5229 if (flexible_array_member_type_p (type))
5231 complete_array_type (&type, elt, false);
5232 DECL_SIZE (decl)
5233 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5234 DECL_SIZE_UNIT (decl)
5235 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5239 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5240 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5241 before the type name, and set *EXPR_CONST_OPERANDS, if
5242 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5243 appear in a constant expression. */
5245 tree
5246 groktypename (struct c_type_name *type_name, tree *expr,
5247 bool *expr_const_operands)
5249 tree type;
5250 tree attrs = type_name->specs->attrs;
5252 type_name->specs->attrs = NULL_TREE;
5254 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5255 false, NULL, &attrs, expr, expr_const_operands,
5256 DEPRECATED_NORMAL);
5258 /* Apply attributes. */
5259 attrs = c_warn_type_attributes (attrs);
5260 decl_attributes (&type, attrs, 0);
5262 return type;
5265 /* Looks up the most recent pushed declaration corresponding to DECL. */
5267 static tree
5268 lookup_last_decl (tree decl)
5270 tree last_decl = lookup_name (DECL_NAME (decl));
5271 if (!last_decl)
5272 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5273 return last_decl;
5276 /* Wrapper for decl_attributes that adds some implicit attributes
5277 to VAR_DECLs or FUNCTION_DECLs. */
5279 static tree
5280 c_decl_attributes (tree *node, tree attributes, int flags)
5282 /* Add implicit "omp declare target" attribute if requested. */
5283 if (vec_safe_length (current_omp_declare_target_attribute)
5284 && ((VAR_P (*node) && is_global_var (*node))
5285 || TREE_CODE (*node) == FUNCTION_DECL))
5287 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5288 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5289 NULL_TREE, attributes);
5290 else
5292 attributes = tree_cons (get_identifier ("omp declare target"),
5293 NULL_TREE, attributes);
5294 attributes = tree_cons (get_identifier ("omp declare target block"),
5295 NULL_TREE, attributes);
5297 if (TREE_CODE (*node) == FUNCTION_DECL)
5299 int device_type
5300 = current_omp_declare_target_attribute->last ().device_type;
5301 device_type = MAX (device_type, 0);
5302 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5303 && !lookup_attribute ("omp declare target host", attributes))
5304 attributes
5305 = tree_cons (get_identifier ("omp declare target host"),
5306 NULL_TREE, attributes);
5307 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5308 && !lookup_attribute ("omp declare target nohost", attributes))
5309 attributes
5310 = tree_cons (get_identifier ("omp declare target nohost"),
5311 NULL_TREE, attributes);
5315 /* Look up the current declaration with all the attributes merged
5316 so far so that attributes on the current declaration that's
5317 about to be pushed that conflict with the former can be detected,
5318 diagnosed, and rejected as appropriate. */
5319 tree last_decl = lookup_last_decl (*node);
5320 return decl_attributes (node, attributes, flags, last_decl);
5324 /* Decode a declarator in an ordinary declaration or data definition.
5325 This is called as soon as the type information and variable name
5326 have been parsed, before parsing the initializer if any.
5327 Here we create the ..._DECL node, fill in its type,
5328 and (if DO_PUSH) put it on the list of decls for the current context.
5329 When nonnull, set *LASTLOC to the location of the prior declaration
5330 of the same entity if one exists.
5331 The ..._DECL node is returned as the value.
5333 Exception: for arrays where the length is not specified,
5334 the type is left null, to be filled in by `finish_decl'.
5336 Function definitions do not come here; they go to start_function
5337 instead. However, external and forward declarations of functions
5338 do go through here. Structure field declarations are done by
5339 grokfield and not through here. */
5341 tree
5342 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5343 bool initialized, tree attributes, bool do_push /* = true */,
5344 location_t *lastloc /* = NULL */)
5346 tree decl;
5347 tree tem;
5348 tree expr = NULL_TREE;
5349 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5351 /* An object declared as __attribute__((unavailable)) suppresses
5352 warnings and errors from __attribute__((deprecated/unavailable))
5353 components.
5354 An object declared as __attribute__((deprecated)) suppresses
5355 warnings of uses of other deprecated items. */
5356 if (lookup_attribute ("unavailable", attributes))
5357 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5358 else if (lookup_attribute ("deprecated", attributes))
5359 deprecated_state = DEPRECATED_SUPPRESS;
5361 decl = grokdeclarator (declarator, declspecs,
5362 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5363 deprecated_state);
5364 if (!decl || decl == error_mark_node)
5365 return NULL_TREE;
5367 if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5368 if (lastdecl != error_mark_node)
5369 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5371 if (expr)
5372 add_stmt (fold_convert (void_type_node, expr));
5374 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5375 && TREE_PUBLIC (decl))
5376 warning (OPT_Wmain, "%q+D is usually a function", decl);
5378 if (initialized)
5379 /* Is it valid for this decl to have an initializer at all?
5380 If not, set INITIALIZED to zero, which will indirectly
5381 tell 'finish_decl' to ignore the initializer once it is parsed. */
5382 switch (TREE_CODE (decl))
5384 case TYPE_DECL:
5385 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5386 initialized = false;
5387 break;
5389 case FUNCTION_DECL:
5390 error ("function %qD is initialized like a variable", decl);
5391 initialized = false;
5392 break;
5394 case PARM_DECL:
5395 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5396 error ("parameter %qD is initialized", decl);
5397 initialized = false;
5398 break;
5400 default:
5401 /* Don't allow initializations for incomplete types except for
5402 arrays which might be completed by the initialization. */
5404 /* This can happen if the array size is an undefined macro.
5405 We already gave a warning, so we don't need another one. */
5406 if (TREE_TYPE (decl) == error_mark_node)
5407 initialized = false;
5408 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5410 /* A complete type is ok if size is fixed. If the size is
5411 variable, an empty initializer is OK and nonempty
5412 initializers will be diagnosed in the parser. */
5414 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5416 error ("variable %qD has initializer but incomplete type", decl);
5417 initialized = false;
5421 if (initialized)
5423 if (current_scope == file_scope)
5424 TREE_STATIC (decl) = 1;
5426 /* Tell 'pushdecl' this is an initialized decl
5427 even though we don't yet have the initializer expression.
5428 Also tell 'finish_decl' it may store the real initializer. */
5429 DECL_INITIAL (decl) = error_mark_node;
5432 /* If this is a function declaration, write a record describing it to the
5433 prototypes file (if requested). */
5435 if (TREE_CODE (decl) == FUNCTION_DECL)
5436 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5438 /* ANSI specifies that a tentative definition which is not merged with
5439 a non-tentative definition behaves exactly like a definition with an
5440 initializer equal to zero. (Section 3.7.2)
5442 -fno-common gives strict ANSI behavior, though this tends to break
5443 a large body of code that grew up without this rule.
5445 Thread-local variables are never common, since there's no entrenched
5446 body of code to break, and it allows more efficient variable references
5447 in the presence of dynamic linking. */
5449 if (VAR_P (decl)
5450 && !initialized
5451 && TREE_PUBLIC (decl)
5452 && !DECL_THREAD_LOCAL_P (decl)
5453 && !flag_no_common)
5454 DECL_COMMON (decl) = 1;
5456 /* Set attributes here so if duplicate decl, will have proper attributes. */
5457 c_decl_attributes (&decl, attributes, 0);
5459 /* Handle gnu_inline attribute. */
5460 if (declspecs->inline_p
5461 && !flag_gnu89_inline
5462 && TREE_CODE (decl) == FUNCTION_DECL
5463 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5464 || current_function_decl))
5466 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5468 else if (declspecs->storage_class != csc_static)
5469 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5472 if (TREE_CODE (decl) == FUNCTION_DECL
5473 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5475 struct c_declarator *ce = declarator;
5477 if (ce->kind == cdk_pointer)
5478 ce = declarator->declarator;
5479 if (ce->kind == cdk_function)
5481 tree args = ce->u.arg_info->parms;
5482 for (; args; args = DECL_CHAIN (args))
5484 tree type = TREE_TYPE (args);
5485 if (type && INTEGRAL_TYPE_P (type)
5486 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5487 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5492 if (TREE_CODE (decl) == FUNCTION_DECL
5493 && DECL_DECLARED_INLINE_P (decl)
5494 && DECL_UNINLINABLE (decl)
5495 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5496 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5497 decl, "noinline");
5499 /* C99 6.7.4p3: An inline definition of a function with external
5500 linkage shall not contain a definition of a modifiable object
5501 with static storage duration... */
5502 if (VAR_P (decl)
5503 && current_scope != file_scope
5504 && TREE_STATIC (decl)
5505 && !TREE_READONLY (decl)
5506 && DECL_DECLARED_INLINE_P (current_function_decl)
5507 && DECL_EXTERNAL (current_function_decl))
5508 record_inline_static (input_location, current_function_decl,
5509 decl, csi_modifiable);
5511 if (c_dialect_objc ()
5512 && VAR_OR_FUNCTION_DECL_P (decl))
5513 objc_check_global_decl (decl);
5515 /* Add this decl to the current scope.
5516 TEM may equal DECL or it may be a previous decl of the same name. */
5517 if (do_push)
5519 tem = pushdecl (decl);
5521 if (initialized && DECL_EXTERNAL (tem))
5523 DECL_EXTERNAL (tem) = 0;
5524 TREE_STATIC (tem) = 1;
5527 return tem;
5529 else
5530 return decl;
5533 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5534 DECL or the non-array element type if DECL is an uninitialized array.
5535 If that type has a const member, diagnose this. */
5537 static void
5538 diagnose_uninitialized_cst_member (tree decl, tree type)
5540 tree field;
5541 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5543 tree field_type;
5544 if (TREE_CODE (field) != FIELD_DECL)
5545 continue;
5546 field_type = strip_array_types (TREE_TYPE (field));
5548 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5550 auto_diagnostic_group d;
5551 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5552 "uninitialized const member in %qT is invalid in C++",
5553 strip_array_types (TREE_TYPE (decl))))
5554 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5557 if (RECORD_OR_UNION_TYPE_P (field_type))
5558 diagnose_uninitialized_cst_member (decl, field_type);
5562 /* Finish processing of a declaration;
5563 install its initial value.
5564 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5565 If the length of an array type is not known before,
5566 it must be determined now, from the initial value, or it is an error.
5568 INIT_LOC is the location of the initial value. */
5570 void
5571 finish_decl (tree decl, location_t init_loc, tree init,
5572 tree origtype, tree asmspec_tree)
5574 tree type;
5575 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5576 const char *asmspec = 0;
5578 /* If a name was specified, get the string. */
5579 if (VAR_OR_FUNCTION_DECL_P (decl)
5580 && DECL_FILE_SCOPE_P (decl))
5581 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5582 if (asmspec_tree)
5583 asmspec = TREE_STRING_POINTER (asmspec_tree);
5585 if (VAR_P (decl)
5586 && TREE_STATIC (decl)
5587 && global_bindings_p ())
5588 /* So decl is a global variable. Record the types it uses
5589 so that we can decide later to emit debug info for them. */
5590 record_types_used_by_current_var_decl (decl);
5592 /* If `start_decl' didn't like having an initialization, ignore it now. */
5593 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5594 init = NULL_TREE;
5596 /* Don't crash if parm is initialized. */
5597 if (TREE_CODE (decl) == PARM_DECL)
5598 init = NULL_TREE;
5600 if (init)
5601 store_init_value (init_loc, decl, init, origtype);
5603 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5604 || TREE_CODE (decl) == FIELD_DECL))
5605 objc_check_decl (decl);
5607 type = TREE_TYPE (decl);
5609 /* Deduce size of array from initialization, if not already known.
5610 This is only needed for an initialization in the current scope;
5611 it must not be done for a file-scope initialization of a
5612 declaration with external linkage, redeclared in an inner scope
5613 with the outer declaration shadowed in an intermediate scope. */
5614 if (TREE_CODE (type) == ARRAY_TYPE
5615 && TYPE_DOMAIN (type) == NULL_TREE
5616 && TREE_CODE (decl) != TYPE_DECL
5617 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5619 bool do_default
5620 = (TREE_STATIC (decl)
5621 /* Even if pedantic, an external linkage array
5622 may have incomplete type at first. */
5623 ? pedantic && !TREE_PUBLIC (decl)
5624 : !DECL_EXTERNAL (decl));
5625 int failure
5626 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5627 do_default);
5629 /* Get the completed type made by complete_array_type. */
5630 type = TREE_TYPE (decl);
5632 switch (failure)
5634 case 1:
5635 error ("initializer fails to determine size of %q+D", decl);
5636 break;
5638 case 2:
5639 if (do_default)
5640 error ("array size missing in %q+D", decl);
5641 break;
5643 case 3:
5644 error ("zero or negative size array %q+D", decl);
5645 break;
5647 case 0:
5648 /* For global variables, update the copy of the type that
5649 exists in the binding. */
5650 if (TREE_PUBLIC (decl))
5652 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5653 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5654 b_ext = b_ext->shadowed;
5655 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5657 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5658 b_ext->u.type = composite_type (b_ext->u.type, type);
5659 else
5660 b_ext->u.type = type;
5663 break;
5665 default:
5666 gcc_unreachable ();
5669 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5670 TREE_TYPE (DECL_INITIAL (decl)) = type;
5672 relayout_decl (decl);
5675 /* Look for braced array initializers for character arrays and
5676 recursively convert them into STRING_CSTs. */
5677 if (tree init = DECL_INITIAL (decl))
5678 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5680 if (VAR_P (decl))
5682 if (init && TREE_CODE (init) == CONSTRUCTOR)
5683 add_flexible_array_elts_to_size (decl, init);
5685 complete_flexible_array_elts (DECL_INITIAL (decl));
5687 if (is_global_var (decl))
5689 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5690 ? TCTX_THREAD_STORAGE
5691 : TCTX_STATIC_STORAGE);
5692 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5693 TREE_TYPE (decl) = error_mark_node;
5696 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5697 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5698 layout_decl (decl, 0);
5700 if (DECL_SIZE (decl) == NULL_TREE
5701 /* Don't give an error if we already gave one earlier. */
5702 && TREE_TYPE (decl) != error_mark_node
5703 && (TREE_STATIC (decl)
5704 /* A static variable with an incomplete type
5705 is an error if it is initialized.
5706 Also if it is not file scope.
5707 Otherwise, let it through, but if it is not `extern'
5708 then it may cause an error message later. */
5709 ? (DECL_INITIAL (decl) != NULL_TREE
5710 || !DECL_FILE_SCOPE_P (decl))
5711 /* An automatic variable with an incomplete type
5712 is an error. */
5713 : !DECL_EXTERNAL (decl)))
5715 error ("storage size of %q+D isn%'t known", decl);
5716 TREE_TYPE (decl) = error_mark_node;
5719 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5720 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5721 && DECL_SIZE (decl) == NULL_TREE
5722 && TREE_STATIC (decl))
5723 incomplete_record_decls.safe_push (decl);
5725 if (is_global_var (decl)
5726 && DECL_SIZE (decl) != NULL_TREE
5727 && TREE_TYPE (decl) != error_mark_node)
5729 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5730 constant_expression_warning (DECL_SIZE (decl));
5731 else
5733 error ("storage size of %q+D isn%'t constant", decl);
5734 TREE_TYPE (decl) = error_mark_node;
5738 if (TREE_USED (type))
5740 TREE_USED (decl) = 1;
5741 DECL_READ_P (decl) = 1;
5745 /* If this is a function and an assembler name is specified, reset DECL_RTL
5746 so we can give it its new name. Also, update builtin_decl if it
5747 was a normal built-in. */
5748 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5750 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5751 set_builtin_user_assembler_name (decl, asmspec);
5752 set_user_assembler_name (decl, asmspec);
5755 /* If #pragma weak was used, mark the decl weak now. */
5756 maybe_apply_pragma_weak (decl);
5758 /* Output the assembler code and/or RTL code for variables and functions,
5759 unless the type is an undefined structure or union.
5760 If not, it will get done when the type is completed. */
5762 if (VAR_OR_FUNCTION_DECL_P (decl))
5764 /* Determine the ELF visibility. */
5765 if (TREE_PUBLIC (decl))
5766 c_determine_visibility (decl);
5768 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5769 if (c_dialect_objc ())
5770 objc_check_decl (decl);
5772 if (asmspec)
5774 /* If this is not a static variable, issue a warning.
5775 It doesn't make any sense to give an ASMSPEC for an
5776 ordinary, non-register local variable. Historically,
5777 GCC has accepted -- but ignored -- the ASMSPEC in
5778 this case. */
5779 if (!DECL_FILE_SCOPE_P (decl)
5780 && VAR_P (decl)
5781 && !C_DECL_REGISTER (decl)
5782 && !TREE_STATIC (decl))
5783 warning (0, "ignoring %<asm%> specifier for non-static local "
5784 "variable %q+D", decl);
5785 else
5786 set_user_assembler_name (decl, asmspec);
5789 if (DECL_FILE_SCOPE_P (decl))
5791 if (DECL_INITIAL (decl) == NULL_TREE
5792 || DECL_INITIAL (decl) == error_mark_node)
5793 /* Don't output anything
5794 when a tentative file-scope definition is seen.
5795 But at end of compilation, do output code for them. */
5796 DECL_DEFER_OUTPUT (decl) = 1;
5797 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5798 DECL_HARD_REGISTER (decl) = 1;
5799 rest_of_decl_compilation (decl, true, 0);
5801 if (TREE_CODE (decl) == FUNCTION_DECL)
5803 tree parms = DECL_ARGUMENTS (decl);
5804 const bool builtin = fndecl_built_in_p (decl);
5805 if (tree access = build_attr_access_from_parms (parms, !builtin))
5806 decl_attributes (&decl, access, 0);
5809 else
5811 /* In conjunction with an ASMSPEC, the `register'
5812 keyword indicates that we should place the variable
5813 in a particular register. */
5814 if (asmspec && C_DECL_REGISTER (decl))
5816 DECL_HARD_REGISTER (decl) = 1;
5817 /* This cannot be done for a structure with volatile
5818 fields, on which DECL_REGISTER will have been
5819 reset. */
5820 if (!DECL_REGISTER (decl))
5821 error ("cannot put object with volatile field into register");
5824 if (TREE_CODE (decl) != FUNCTION_DECL)
5826 /* If we're building a variable sized type, and we might be
5827 reachable other than via the top of the current binding
5828 level, then create a new BIND_EXPR so that we deallocate
5829 the object at the right time. */
5830 /* Note that DECL_SIZE can be null due to errors. */
5831 if (DECL_SIZE (decl)
5832 && !TREE_CONSTANT (DECL_SIZE (decl))
5833 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5835 tree bind;
5836 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5837 TREE_SIDE_EFFECTS (bind) = 1;
5838 add_stmt (bind);
5839 BIND_EXPR_BODY (bind) = push_stmt_list ();
5841 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5842 DECL_EXPR, decl));
5847 if (!DECL_FILE_SCOPE_P (decl))
5849 /* Recompute the RTL of a local array now
5850 if it used to be an incomplete type. */
5851 if (was_incomplete && !is_global_var (decl))
5853 /* If we used it already as memory, it must stay in memory. */
5854 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5855 /* If it's still incomplete now, no init will save it. */
5856 if (DECL_SIZE (decl) == NULL_TREE)
5857 DECL_INITIAL (decl) = NULL_TREE;
5862 if (TREE_CODE (decl) == TYPE_DECL)
5864 if (!DECL_FILE_SCOPE_P (decl)
5865 && c_type_variably_modified_p (TREE_TYPE (decl)))
5866 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5868 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5871 /* Install a cleanup (aka destructor) if one was given. */
5872 if (VAR_P (decl) && !TREE_STATIC (decl))
5874 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5875 if (attr)
5877 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5878 tree cleanup_decl = lookup_name (cleanup_id);
5879 tree cleanup;
5880 vec<tree, va_gc> *v;
5882 /* Build "cleanup(&decl)" for the destructor. */
5883 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5884 vec_alloc (v, 1);
5885 v->quick_push (cleanup);
5886 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5887 vNULL, cleanup_decl, v, NULL);
5888 vec_free (v);
5890 /* Don't warn about decl unused; the cleanup uses it. */
5891 TREE_USED (decl) = 1;
5892 TREE_USED (cleanup_decl) = 1;
5893 DECL_READ_P (decl) = 1;
5895 push_cleanup (decl, cleanup, false);
5899 if (warn_cxx_compat
5900 && VAR_P (decl)
5901 && !DECL_EXTERNAL (decl)
5902 && DECL_INITIAL (decl) == NULL_TREE)
5904 type = strip_array_types (type);
5905 if (TREE_READONLY (decl))
5906 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5907 "uninitialized %<const %D%> is invalid in C++", decl);
5908 else if (RECORD_OR_UNION_TYPE_P (type)
5909 && C_TYPE_FIELDS_READONLY (type))
5910 diagnose_uninitialized_cst_member (decl, type);
5913 if (flag_openmp
5914 && VAR_P (decl)
5915 && lookup_attribute ("omp declare target implicit",
5916 DECL_ATTRIBUTES (decl)))
5918 DECL_ATTRIBUTES (decl)
5919 = remove_attribute ("omp declare target implicit",
5920 DECL_ATTRIBUTES (decl));
5921 if (!omp_mappable_type (TREE_TYPE (decl)))
5922 error ("%q+D in declare target directive does not have mappable type",
5923 decl);
5924 else if (!lookup_attribute ("omp declare target",
5925 DECL_ATTRIBUTES (decl))
5926 && !lookup_attribute ("omp declare target link",
5927 DECL_ATTRIBUTES (decl)))
5929 DECL_ATTRIBUTES (decl)
5930 = tree_cons (get_identifier ("omp declare target"),
5931 NULL_TREE, DECL_ATTRIBUTES (decl));
5932 symtab_node *node = symtab_node::get (decl);
5933 if (node != NULL)
5935 node->offloadable = 1;
5936 if (ENABLE_OFFLOADING)
5938 g->have_offload = true;
5939 if (is_a <varpool_node *> (node))
5940 vec_safe_push (offload_vars, decl);
5946 /* This is the last point we can lower alignment so give the target the
5947 chance to do so. */
5948 if (VAR_P (decl)
5949 && !is_global_var (decl)
5950 && !DECL_HARD_REGISTER (decl))
5951 targetm.lower_local_decl_alignment (decl);
5953 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5956 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5957 EXPR is NULL or a pointer to an expression that needs to be
5958 evaluated for the side effects of array size expressions in the
5959 parameters. */
5961 tree
5962 grokparm (const struct c_parm *parm, tree *expr)
5964 tree attrs = parm->attrs;
5965 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5966 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5968 decl_attributes (&decl, attrs, 0);
5970 return decl;
5973 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5974 described by PARM, concatenated onto attributes ATTRS.
5975 The spec consists of one dollar symbol for each specified variable
5976 bound, one asterisk for each unspecified variable bound, followed
5977 by at most one specification of the most significant bound of
5978 an ordinary array parameter. For ordinary arrays the specification
5979 is either the constant bound itself, or the space character for
5980 an array with an unspecified bound (the [] form). Finally, a chain
5981 of specified variable bounds is appended to the spec, starting with
5982 the most significant bound. For example, the PARM T a[2][m][3][n]
5983 will produce __attribute__((arg spec ("[$$2]", m, n)).
5984 For T a typedef for an array with variable bounds, the bounds are
5985 included in the specification in the expected order.
5986 No "arg spec" is created for parameters of pointer types, making
5987 a distinction between T(*)[N] (or, equivalently, T[][N]) and
5988 the T[M][N] form, all of which have the same type and are represented
5989 the same, but only the last of which gets an "arg spec" describing
5990 the most significant bound M. */
5992 static tree
5993 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5995 /* The attribute specification string, minor bound first. */
5996 std::string spec;
5998 /* A list of VLA variable bounds, major first, or null if unspecified
5999 or not a VLA. */
6000 tree vbchain = NULL_TREE;
6001 /* True for a pointer parameter. */
6002 bool pointer = false;
6003 /* True for an ordinary array with an unpecified bound. */
6004 bool nobound = false;
6006 /* Create a string representation for the bounds of the array/VLA. */
6007 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
6009 next = pd->declarator;
6010 while (next && next->kind == cdk_attrs)
6011 next = next->declarator;
6013 /* Remember if a pointer has been seen to avoid storing the constant
6014 bound. */
6015 if (pd->kind == cdk_pointer)
6016 pointer = true;
6018 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
6019 && (!next || next->kind == cdk_id))
6021 /* Do nothing for the common case of a pointer. The fact that
6022 the parameter is one can be deduced from the absence of
6023 an arg spec for it. */
6024 return attrs;
6027 if (pd->kind == cdk_id)
6029 if (pointer
6030 || !parm->specs->type
6031 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
6032 || !TYPE_DOMAIN (parm->specs->type)
6033 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
6034 continue;
6036 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
6037 if (!vbchain
6038 && TREE_CODE (max) == INTEGER_CST)
6040 /* Extract the upper bound from a parameter of an array type
6041 unless the parameter is an ordinary array of unspecified
6042 bound in which case a next iteration of the loop will
6043 exit. */
6044 if (spec.empty () || spec.end ()[-1] != ' ')
6046 if (!tree_fits_shwi_p (max))
6047 continue;
6049 /* The upper bound is the value of the largest valid
6050 index. */
6051 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
6052 char buf[40];
6053 sprintf (buf, "%lu", (unsigned long)n);
6054 spec += buf;
6056 continue;
6059 /* For a VLA typedef, create a list of its variable bounds and
6060 append it in the expected order to VBCHAIN. */
6061 tree tpbnds = NULL_TREE;
6062 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
6063 type = TREE_TYPE (type))
6065 tree nelts = array_type_nelts (type);
6066 if (error_operand_p (nelts))
6067 return attrs;
6068 if (TREE_CODE (nelts) != INTEGER_CST)
6070 /* Each variable VLA bound is represented by the dollar
6071 sign. */
6072 spec += "$";
6073 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
6076 tpbnds = nreverse (tpbnds);
6077 vbchain = chainon (vbchain, tpbnds);
6078 continue;
6081 if (pd->kind != cdk_array)
6082 continue;
6084 if (pd->u.array.vla_unspec_p)
6086 /* Each unspecified bound is represented by a star. There
6087 can be any number of these in a declaration (but none in
6088 a definition). */
6089 spec += '*';
6090 continue;
6093 tree nelts = pd->u.array.dimen;
6094 if (!nelts)
6096 /* Ordinary array of unspecified size. There can be at most
6097 one for the most significant bound. Exit on the next
6098 iteration which determines whether or not PARM is declared
6099 as a pointer or an array. */
6100 nobound = true;
6101 continue;
6104 if (pd->u.array.static_p)
6105 spec += 's';
6107 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
6108 /* Avoid invalid NELTS. */
6109 return attrs;
6111 STRIP_NOPS (nelts);
6112 nelts = c_fully_fold (nelts, false, nullptr);
6113 if (TREE_CODE (nelts) == INTEGER_CST)
6115 /* Skip all constant bounds except the most significant one.
6116 The interior ones are included in the array type. */
6117 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
6118 continue;
6120 if (!tree_fits_uhwi_p (nelts))
6121 /* Bail completely on invalid bounds. */
6122 return attrs;
6124 char buf[40];
6125 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
6126 sprintf (buf, "%llu", (unsigned long long)n);
6127 spec += buf;
6128 break;
6131 /* Each variable VLA bound is represented by a dollar sign. */
6132 spec += "$";
6133 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
6136 if (spec.empty () && !nobound)
6137 return attrs;
6139 spec.insert (0, "[");
6140 if (nobound)
6141 /* Ordinary array of unspecified bound is represented by a space.
6142 It must be last in the spec. */
6143 spec += ' ';
6144 spec += ']';
6146 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
6147 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
6148 tree name = get_identifier ("arg spec");
6149 return tree_cons (name, args, attrs);
6152 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6153 and push that on the current scope. EXPR is a pointer to an
6154 expression that needs to be evaluated for the side effects of array
6155 size expressions in the parameters. */
6157 void
6158 push_parm_decl (const struct c_parm *parm, tree *expr)
6160 tree attrs = parm->attrs;
6161 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6162 &attrs, expr, NULL, DEPRECATED_NORMAL);
6163 if (decl && DECL_P (decl))
6164 DECL_SOURCE_LOCATION (decl) = parm->loc;
6166 attrs = get_parm_array_spec (parm, attrs);
6167 decl_attributes (&decl, attrs, 0);
6169 decl = pushdecl (decl);
6171 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6174 /* Mark all the parameter declarations to date as forward decls.
6175 Also diagnose use of this extension. */
6177 void
6178 mark_forward_parm_decls (void)
6180 struct c_binding *b;
6182 if (pedantic && !current_scope->warned_forward_parm_decls)
6184 pedwarn (input_location, OPT_Wpedantic,
6185 "ISO C forbids forward parameter declarations");
6186 current_scope->warned_forward_parm_decls = true;
6189 for (b = current_scope->bindings; b; b = b->prev)
6190 if (TREE_CODE (b->decl) == PARM_DECL)
6191 TREE_ASM_WRITTEN (b->decl) = 1;
6194 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6195 literal, which may be an incomplete array type completed by the
6196 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6197 literal. NON_CONST is true if the initializers contain something
6198 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6199 it is the (valid) alignment for this compound literal, as specified
6200 with _Alignas. SCSPECS are the storage class specifiers (C2x) from the
6201 compound literal. */
6203 tree
6204 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6205 unsigned int alignas_align,
6206 struct c_declspecs *scspecs)
6208 /* We do not use start_decl here because we have a type, not a declarator;
6209 and do not use finish_decl because the decl should be stored inside
6210 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6211 tree decl;
6212 tree complit;
6213 tree stmt;
6214 bool threadp = scspecs ? scspecs->thread_p : false;
6215 enum c_storage_class storage_class = (scspecs
6216 ? scspecs->storage_class
6217 : csc_none);
6219 if (type == error_mark_node
6220 || init == error_mark_node)
6221 return error_mark_node;
6223 if (current_scope == file_scope && storage_class == csc_register)
6225 error_at (loc, "file-scope compound literal specifies %<register%>");
6226 storage_class = csc_none;
6229 if (current_scope != file_scope && threadp && storage_class == csc_none)
6231 error_at (loc, "compound literal implicitly auto and declared %qs",
6232 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6233 threadp = false;
6236 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6237 DECL_EXTERNAL (decl) = 0;
6238 TREE_PUBLIC (decl) = 0;
6239 TREE_STATIC (decl) = (current_scope == file_scope
6240 || storage_class == csc_static);
6241 DECL_CONTEXT (decl) = current_function_decl;
6242 TREE_USED (decl) = 1;
6243 DECL_READ_P (decl) = 1;
6244 DECL_ARTIFICIAL (decl) = 1;
6245 DECL_IGNORED_P (decl) = 1;
6246 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6247 C_DECL_DECLARED_CONSTEXPR (decl) = scspecs && scspecs->constexpr_p;
6248 TREE_TYPE (decl) = type;
6249 if (threadp)
6250 set_decl_tls_model (decl, decl_default_tls_model (decl));
6251 if (storage_class == csc_register)
6253 C_DECL_REGISTER (decl) = 1;
6254 DECL_REGISTER (decl) = 1;
6256 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6257 if (alignas_align)
6259 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6260 DECL_USER_ALIGN (decl) = 1;
6262 store_init_value (loc, decl, init, NULL_TREE);
6263 if (current_scope != file_scope
6264 && TREE_STATIC (decl)
6265 && !TREE_READONLY (decl)
6266 && DECL_DECLARED_INLINE_P (current_function_decl)
6267 && DECL_EXTERNAL (current_function_decl))
6268 record_inline_static (input_location, current_function_decl,
6269 decl, csi_modifiable);
6271 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6273 int failure = complete_array_type (&TREE_TYPE (decl),
6274 DECL_INITIAL (decl), true);
6275 /* If complete_array_type returns 3, it means that the
6276 initial value of the compound literal is empty. Allow it. */
6277 gcc_assert (failure == 0 || failure == 3);
6279 type = TREE_TYPE (decl);
6280 TREE_TYPE (DECL_INITIAL (decl)) = type;
6283 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6285 c_incomplete_type_error (loc, NULL_TREE, type);
6286 return error_mark_node;
6289 if (TREE_STATIC (decl)
6290 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6291 return error_mark_node;
6293 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6294 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6295 TREE_SIDE_EFFECTS (complit) = 1;
6297 layout_decl (decl, 0);
6299 if (TREE_STATIC (decl))
6301 /* This decl needs a name for the assembler output. */
6302 set_compound_literal_name (decl);
6303 DECL_DEFER_OUTPUT (decl) = 1;
6304 DECL_COMDAT (decl) = 1;
6305 pushdecl (decl);
6306 rest_of_decl_compilation (decl, 1, 0);
6308 else if (current_function_decl && !current_scope->parm_flag)
6309 pushdecl (decl);
6311 if (non_const)
6313 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6314 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6317 return complit;
6320 /* Check the type of a compound literal. Here we just check that it
6321 is valid for C++. */
6323 void
6324 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6326 if (warn_cxx_compat
6327 && (type_name->specs->typespec_kind == ctsk_tagdef
6328 || type_name->specs->typespec_kind == ctsk_tagfirstref
6329 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6330 warning_at (loc, OPT_Wc___compat,
6331 "defining a type in a compound literal is invalid in C++");
6334 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6335 replacing with appropriate values if they are invalid. */
6337 static void
6338 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6339 tree orig_name)
6341 tree type_mv;
6342 unsigned int max_width;
6343 unsigned HOST_WIDE_INT w;
6344 const char *name = (orig_name
6345 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6346 : _("<anonymous>"));
6348 /* Detect and ignore out of range field width and process valid
6349 field widths. */
6350 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6352 error_at (loc, "bit-field %qs width not an integer constant", name);
6353 *width = integer_one_node;
6355 else
6357 if (TREE_CODE (*width) != INTEGER_CST)
6359 *width = c_fully_fold (*width, false, NULL);
6360 if (TREE_CODE (*width) == INTEGER_CST)
6361 pedwarn (loc, OPT_Wpedantic,
6362 "bit-field %qs width not an integer constant expression",
6363 name);
6365 if (TREE_CODE (*width) != INTEGER_CST)
6367 error_at (loc, "bit-field %qs width not an integer constant", name);
6368 *width = integer_one_node;
6370 constant_expression_warning (*width);
6371 if (tree_int_cst_sgn (*width) < 0)
6373 error_at (loc, "negative width in bit-field %qs", name);
6374 *width = integer_one_node;
6376 else if (integer_zerop (*width) && orig_name)
6378 error_at (loc, "zero width for bit-field %qs", name);
6379 *width = integer_one_node;
6383 /* Detect invalid bit-field type. */
6384 if (TREE_CODE (*type) != INTEGER_TYPE
6385 && TREE_CODE (*type) != BOOLEAN_TYPE
6386 && TREE_CODE (*type) != ENUMERAL_TYPE)
6388 error_at (loc, "bit-field %qs has invalid type", name);
6389 *type = unsigned_type_node;
6392 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6394 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6395 name);
6396 *type = unsigned_type_node;
6399 type_mv = TYPE_MAIN_VARIANT (*type);
6400 if (!in_system_header_at (input_location)
6401 && type_mv != integer_type_node
6402 && type_mv != unsigned_type_node
6403 && type_mv != boolean_type_node)
6404 pedwarn_c90 (loc, OPT_Wpedantic,
6405 "type of bit-field %qs is a GCC extension", name);
6407 max_width = TYPE_PRECISION (*type);
6409 if (compare_tree_int (*width, max_width) > 0)
6411 error_at (loc, "width of %qs exceeds its type", name);
6412 w = max_width;
6413 *width = build_int_cst (integer_type_node, w);
6415 else
6416 w = tree_to_uhwi (*width);
6418 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6420 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6421 if (!lt
6422 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6423 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6424 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6430 /* Print warning about variable length array if necessary. */
6432 static void
6433 warn_variable_length_array (tree name, tree size)
6435 if (TREE_CONSTANT (size))
6437 if (name)
6438 pedwarn_c90 (input_location, OPT_Wvla,
6439 "ISO C90 forbids array %qE whose size "
6440 "cannot be evaluated", name);
6441 else
6442 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6443 "whose size cannot be evaluated");
6445 else
6447 if (name)
6448 pedwarn_c90 (input_location, OPT_Wvla,
6449 "ISO C90 forbids variable length array %qE", name);
6450 else
6451 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6452 "length array");
6456 /* Print warning about defaulting to int if necessary. */
6458 static void
6459 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6461 diagnostic_info diagnostic;
6462 va_list ap;
6463 rich_location richloc (line_table, location);
6465 va_start (ap, gmsgid);
6466 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6467 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6468 diagnostic.option_index = opt;
6469 diagnostic_report_diagnostic (global_dc, &diagnostic);
6470 va_end (ap);
6473 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6474 considering only those c_declspec_words found in LIST, which
6475 must be terminated by cdw_number_of_elements. */
6477 static location_t
6478 smallest_type_quals_location (const location_t *locations,
6479 const c_declspec_word *list)
6481 location_t loc = UNKNOWN_LOCATION;
6482 while (*list != cdw_number_of_elements)
6484 location_t newloc = locations[*list];
6485 if (loc == UNKNOWN_LOCATION
6486 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6487 loc = newloc;
6488 list++;
6491 return loc;
6494 /* Given declspecs and a declarator,
6495 determine the name and type of the object declared
6496 and construct a ..._DECL node for it.
6497 (In one case we can return a ..._TYPE node instead.
6498 For invalid input we sometimes return NULL_TREE.)
6500 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6502 DECL_CONTEXT says which syntactic context this declaration is in:
6503 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6504 FUNCDEF for a function definition. Like NORMAL but a few different
6505 error messages in each case. Return value may be zero meaning
6506 this definition is too screwy to try to parse.
6507 PARM for a parameter declaration (either within a function prototype
6508 or before a function body). Make a PARM_DECL, or return void_type_node.
6509 TYPENAME if for a typename (in a cast or sizeof).
6510 Don't make a DECL node; just return the ..._TYPE node.
6511 FIELD for a struct or union field; make a FIELD_DECL.
6512 INITIALIZED is true if the decl has an initializer.
6513 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6514 representing the width of the bit-field.
6515 DECL_ATTRS points to the list of attributes that should be added to this
6516 decl. Any nested attributes that belong on the decl itself will be
6517 added to this list.
6518 If EXPR is not NULL, any expressions that need to be evaluated as
6519 part of evaluating variably modified types will be stored in *EXPR.
6520 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6521 set to indicate whether operands in *EXPR can be used in constant
6522 expressions.
6523 DEPRECATED_STATE is a deprecated_states value indicating whether
6524 deprecation/unavailability warnings should be suppressed.
6526 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6527 It may also be so in the PARM case, for a prototype where the
6528 argument type is specified but not the name.
6530 This function is where the complicated C meanings of `static'
6531 and `extern' are interpreted. */
6533 static tree
6534 grokdeclarator (const struct c_declarator *declarator,
6535 struct c_declspecs *declspecs,
6536 enum decl_context decl_context, bool initialized, tree *width,
6537 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6538 enum deprecated_states deprecated_state)
6540 tree type = declspecs->type;
6541 bool threadp = declspecs->thread_p;
6542 bool constexprp = declspecs->constexpr_p;
6543 enum c_storage_class storage_class = declspecs->storage_class;
6544 int constp;
6545 int restrictp;
6546 int volatilep;
6547 int atomicp;
6548 int type_quals = TYPE_UNQUALIFIED;
6549 tree name = NULL_TREE;
6550 bool funcdef_flag = false;
6551 bool funcdef_syntax = false;
6552 bool size_varies = false;
6553 tree decl_attr = declspecs->decl_attr;
6554 int array_ptr_quals = TYPE_UNQUALIFIED;
6555 tree array_ptr_attrs = NULL_TREE;
6556 bool array_parm_static = false;
6557 bool array_parm_vla_unspec_p = false;
6558 tree returned_attrs = NULL_TREE;
6559 tree decl_id_attrs = NULL_TREE;
6560 bool bitfield = width != NULL;
6561 tree element_type;
6562 tree orig_qual_type = NULL;
6563 size_t orig_qual_indirect = 0;
6564 struct c_arg_info *arg_info = 0;
6565 addr_space_t as1, as2, address_space;
6566 location_t loc = UNKNOWN_LOCATION;
6567 tree expr_dummy;
6568 bool expr_const_operands_dummy;
6569 enum c_declarator_kind first_non_attr_kind;
6570 unsigned int alignas_align = 0;
6572 if (type == NULL_TREE)
6574 /* This can occur for auto on a parameter in C2X mode. Set a
6575 dummy type here so subsequent code can give diagnostics for
6576 this case. */
6577 gcc_assert (declspecs->c2x_auto_p);
6578 gcc_assert (decl_context == PARM);
6579 type = declspecs->type = integer_type_node;
6581 if (TREE_CODE (type) == ERROR_MARK)
6582 return error_mark_node;
6583 if (expr == NULL)
6585 expr = &expr_dummy;
6586 expr_dummy = NULL_TREE;
6588 if (expr_const_operands == NULL)
6589 expr_const_operands = &expr_const_operands_dummy;
6591 if (declspecs->expr)
6593 if (*expr)
6594 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6595 declspecs->expr);
6596 else
6597 *expr = declspecs->expr;
6599 *expr_const_operands = declspecs->expr_const_operands;
6601 if (decl_context == FUNCDEF)
6602 funcdef_flag = true, decl_context = NORMAL;
6604 /* Look inside a declarator for the name being declared
6605 and get it as an IDENTIFIER_NODE, for an error message. */
6607 const struct c_declarator *decl = declarator;
6609 first_non_attr_kind = cdk_attrs;
6610 while (decl)
6611 switch (decl->kind)
6613 case cdk_array:
6614 loc = decl->id_loc;
6615 /* FALL THRU. */
6617 case cdk_function:
6618 case cdk_pointer:
6619 funcdef_syntax = (decl->kind == cdk_function);
6620 if (first_non_attr_kind == cdk_attrs)
6621 first_non_attr_kind = decl->kind;
6622 decl = decl->declarator;
6623 break;
6625 case cdk_attrs:
6626 decl = decl->declarator;
6627 break;
6629 case cdk_id:
6630 loc = decl->id_loc;
6631 if (decl->u.id.id)
6632 name = decl->u.id.id;
6633 decl_id_attrs = decl->u.id.attrs;
6634 if (first_non_attr_kind == cdk_attrs)
6635 first_non_attr_kind = decl->kind;
6636 decl = 0;
6637 break;
6639 default:
6640 gcc_unreachable ();
6642 if (name == NULL_TREE)
6644 gcc_assert (decl_context == PARM
6645 || decl_context == TYPENAME
6646 || (decl_context == FIELD
6647 && declarator->kind == cdk_id));
6648 gcc_assert (!initialized);
6652 /* An enum type specifier (": specifier-qualifier-list") may only be
6653 specified when the enum is being defined or in an empty
6654 declaration of the form "enum identifier enum-type-specifier;".
6655 Except for the case of an empty declaration that has additional
6656 declaration specifiers, all invalid contexts (declarations that
6657 aren't empty, type names, parameter declarations, member
6658 declarations) pass through grokdeclarator. */
6659 if (declspecs->enum_type_specifier_ref_p)
6660 error_at (loc, "%<enum%> underlying type may not be specified here");
6662 /* A function definition's declarator must have the form of
6663 a function declarator. */
6665 if (funcdef_flag && !funcdef_syntax)
6666 return NULL_TREE;
6668 /* If this looks like a function definition, make it one,
6669 even if it occurs where parms are expected.
6670 Then store_parm_decls will reject it and not use it as a parm. */
6671 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6672 decl_context = PARM;
6674 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6676 if (declspecs->unavailable_p)
6677 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6678 else if (declspecs->deprecated_p
6679 && deprecated_state != DEPRECATED_SUPPRESS)
6680 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6683 if ((decl_context == NORMAL || decl_context == FIELD)
6684 && current_scope == file_scope
6685 && c_type_variably_modified_p (type))
6687 if (name)
6688 error_at (loc, "variably modified %qE at file scope", name);
6689 else
6690 error_at (loc, "variably modified field at file scope");
6691 type = integer_type_node;
6694 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6696 /* Diagnose defaulting to "int". */
6698 if (declspecs->default_int_p && !in_system_header_at (input_location))
6700 /* Issue a warning if this is an ISO C 99 program or if
6701 -Wreturn-type and this is a function, or if -Wimplicit;
6702 prefer the former warning since it is more explicit. */
6703 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6704 && funcdef_flag)
6705 warn_about_return_type = 1;
6706 else
6708 if (name)
6709 warn_defaults_to (loc, OPT_Wimplicit_int,
6710 "type defaults to %<int%> in declaration "
6711 "of %qE", name);
6712 else
6713 warn_defaults_to (loc, OPT_Wimplicit_int,
6714 "type defaults to %<int%> in type name");
6718 /* Adjust the type if a bit-field is being declared,
6719 -funsigned-bitfields applied and the type is not explicitly
6720 "signed". */
6721 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6722 && TREE_CODE (type) == INTEGER_TYPE)
6723 type = unsigned_type_for (type);
6725 /* Figure out the type qualifiers for the declaration. There are
6726 two ways a declaration can become qualified. One is something
6727 like `const int i' where the `const' is explicit. Another is
6728 something like `typedef const int CI; CI i' where the type of the
6729 declaration contains the `const'. A third possibility is that
6730 there is a type qualifier on the element type of a typedefed
6731 array type, in which case we should extract that qualifier so
6732 that c_apply_type_quals_to_decl receives the full list of
6733 qualifiers to work with (C90 is not entirely clear about whether
6734 duplicate qualifiers should be diagnosed in this case, but it
6735 seems most appropriate to do so). */
6736 element_type = strip_array_types (type);
6737 constp = declspecs->const_p + TYPE_READONLY (element_type);
6738 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6739 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6740 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6741 as1 = declspecs->address_space;
6742 as2 = TYPE_ADDR_SPACE (element_type);
6743 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6745 if (constp > 1)
6746 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6747 if (restrictp > 1)
6748 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6749 if (volatilep > 1)
6750 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6751 if (atomicp > 1)
6752 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6754 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6755 error_at (loc, "conflicting named address spaces (%s vs %s)",
6756 c_addr_space_name (as1), c_addr_space_name (as2));
6758 if ((TREE_CODE (type) == ARRAY_TYPE
6759 || first_non_attr_kind == cdk_array)
6760 && TYPE_QUALS (element_type))
6762 orig_qual_type = type;
6763 type = TYPE_MAIN_VARIANT (type);
6765 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6766 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6767 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6768 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6769 | ENCODE_QUAL_ADDR_SPACE (address_space));
6770 if (type_quals != TYPE_QUALS (element_type))
6771 orig_qual_type = NULL_TREE;
6773 /* Applying the _Atomic qualifier to an array type (through the use
6774 of typedefs or typeof) must be detected here. If the qualifier
6775 is introduced later, any appearance of applying it to an array is
6776 actually applying it to an element of that array. */
6777 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6778 error_at (loc, "%<_Atomic%>-qualified array type");
6780 /* Warn about storage classes that are invalid for certain
6781 kinds of declarations (parameters, typenames, etc.). */
6783 if (funcdef_flag
6784 && (threadp
6785 || constexprp
6786 || storage_class == csc_auto
6787 || storage_class == csc_register
6788 || storage_class == csc_typedef))
6790 if (storage_class == csc_auto)
6791 pedwarn (loc,
6792 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6793 "function definition declared %<auto%>");
6794 if (storage_class == csc_register)
6795 error_at (loc, "function definition declared %<register%>");
6796 if (storage_class == csc_typedef)
6797 error_at (loc, "function definition declared %<typedef%>");
6798 if (threadp)
6799 error_at (loc, "function definition declared %qs",
6800 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6801 threadp = false;
6802 /* The parser ensures a constexpr function definition never
6803 reaches here. */
6804 gcc_assert (!constexprp);
6805 if (storage_class == csc_auto
6806 || storage_class == csc_register
6807 || storage_class == csc_typedef)
6808 storage_class = csc_none;
6810 else if (decl_context != NORMAL && (storage_class != csc_none
6811 || threadp
6812 || constexprp
6813 || declspecs->c2x_auto_p))
6815 if (decl_context == PARM
6816 && storage_class == csc_register
6817 && !constexprp
6818 && !declspecs->c2x_auto_p)
6820 else
6822 switch (decl_context)
6824 case FIELD:
6825 if (name)
6826 error_at (loc, "storage class specified for structure "
6827 "field %qE", name);
6828 else
6829 error_at (loc, "storage class specified for structure field");
6830 break;
6831 case PARM:
6832 if (name)
6833 error_at (loc, "storage class specified for parameter %qE",
6834 name);
6835 else
6836 error_at (loc, "storage class specified for unnamed parameter");
6837 break;
6838 default:
6839 error_at (loc, "storage class specified for typename");
6840 break;
6842 storage_class = csc_none;
6843 threadp = false;
6844 constexprp = false;
6847 else if (storage_class == csc_extern
6848 && initialized
6849 && !funcdef_flag)
6851 /* 'extern' with initialization is invalid if not at file scope. */
6852 if (current_scope == file_scope)
6854 /* It is fine to have 'extern const' when compiling at C
6855 and C++ intersection. */
6856 if (!(warn_cxx_compat && constp))
6857 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6858 name);
6860 else
6861 error_at (loc, "%qE has both %<extern%> and initializer", name);
6863 else if (current_scope == file_scope)
6865 if (storage_class == csc_auto)
6866 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6867 name);
6868 if (pedantic && storage_class == csc_register)
6869 pedwarn (input_location, OPT_Wpedantic,
6870 "file-scope declaration of %qE specifies %<register%>", name);
6872 else
6874 if (storage_class == csc_extern && funcdef_flag)
6875 error_at (loc, "nested function %qE declared %<extern%>", name);
6876 else if (threadp && storage_class == csc_none)
6878 error_at (loc, "function-scope %qE implicitly auto and declared "
6879 "%qs", name,
6880 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6881 threadp = false;
6885 /* Now figure out the structure of the declarator proper.
6886 Descend through it, creating more complex types, until we reach
6887 the declared identifier (or NULL_TREE, in an absolute declarator).
6888 At each stage we maintain an unqualified version of the type
6889 together with any qualifiers that should be applied to it with
6890 c_build_qualified_type; this way, array types including
6891 multidimensional array types are first built up in unqualified
6892 form and then the qualified form is created with
6893 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6895 while (declarator && declarator->kind != cdk_id)
6897 if (type == error_mark_node)
6899 declarator = declarator->declarator;
6900 continue;
6903 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6904 a cdk_pointer (for *...),
6905 a cdk_function (for ...(...)),
6906 a cdk_attrs (for nested attributes),
6907 or a cdk_id (for the name being declared
6908 or the place in an absolute declarator
6909 where the name was omitted).
6910 For the last case, we have just exited the loop.
6912 At this point, TYPE is the type of elements of an array,
6913 or for a function to return, or for a pointer to point to.
6914 After this sequence of ifs, TYPE is the type of the
6915 array or function or pointer, and DECLARATOR has had its
6916 outermost layer removed. */
6918 if (array_ptr_quals != TYPE_UNQUALIFIED
6919 || array_ptr_attrs != NULL_TREE
6920 || array_parm_static)
6922 /* Only the innermost declarator (making a parameter be of
6923 array type which is converted to pointer type)
6924 may have static or type qualifiers. */
6925 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6926 array_ptr_quals = TYPE_UNQUALIFIED;
6927 array_ptr_attrs = NULL_TREE;
6928 array_parm_static = false;
6931 bool varmod = C_TYPE_VARIABLY_MODIFIED (type);
6933 switch (declarator->kind)
6935 case cdk_attrs:
6937 /* A declarator with embedded attributes. */
6938 tree attrs = declarator->u.attrs;
6939 const struct c_declarator *inner_decl;
6940 int attr_flags = 0;
6941 declarator = declarator->declarator;
6942 /* Standard attribute syntax precisely defines what entity
6943 an attribute in each position appertains to, so only
6944 apply laxity about positioning to GNU attribute syntax.
6945 Standard attributes applied to a function or array
6946 declarator apply exactly to that type; standard
6947 attributes applied to the identifier apply to the
6948 declaration rather than to the type, and are specified
6949 using a cdk_id declarator rather than using
6950 cdk_attrs. */
6951 inner_decl = declarator;
6952 while (inner_decl->kind == cdk_attrs)
6953 inner_decl = inner_decl->declarator;
6954 if (!cxx11_attribute_p (attrs))
6956 if (inner_decl->kind == cdk_id)
6957 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6958 else if (inner_decl->kind == cdk_function)
6959 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6960 else if (inner_decl->kind == cdk_array)
6961 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6963 attrs = c_warn_type_attributes (attrs);
6964 returned_attrs = decl_attributes (&type,
6965 chainon (returned_attrs, attrs),
6966 attr_flags);
6967 break;
6969 case cdk_array:
6971 tree itype = NULL_TREE;
6972 tree size = declarator->u.array.dimen;
6973 /* The index is a signed object `sizetype' bits wide. */
6974 tree index_type = c_common_signed_type (sizetype);
6976 array_ptr_quals = declarator->u.array.quals;
6977 array_ptr_attrs = declarator->u.array.attrs;
6978 array_parm_static = declarator->u.array.static_p;
6979 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6981 declarator = declarator->declarator;
6983 /* Check for some types that there cannot be arrays of. */
6985 if (VOID_TYPE_P (type))
6987 if (name)
6988 error_at (loc, "declaration of %qE as array of voids", name);
6989 else
6990 error_at (loc, "declaration of type name as array of voids");
6991 type = error_mark_node;
6994 if (TREE_CODE (type) == FUNCTION_TYPE)
6996 if (name)
6997 error_at (loc, "declaration of %qE as array of functions",
6998 name);
6999 else
7000 error_at (loc, "declaration of type name as array of "
7001 "functions");
7002 type = error_mark_node;
7005 if (pedantic && !in_system_header_at (input_location)
7006 && flexible_array_type_p (type))
7007 pedwarn (loc, OPT_Wpedantic,
7008 "invalid use of structure with flexible array member");
7010 if (size == error_mark_node)
7011 type = error_mark_node;
7013 if (type == error_mark_node)
7014 continue;
7016 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
7018 type = error_mark_node;
7019 continue;
7022 /* If size was specified, set ITYPE to a range-type for
7023 that size. Otherwise, ITYPE remains null. finish_decl
7024 may figure it out from an initial value. */
7026 if (size)
7028 bool size_maybe_const = true;
7029 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
7030 && !TREE_OVERFLOW (size));
7031 bool this_size_varies = false;
7033 /* Strip NON_LVALUE_EXPRs since we aren't using as an
7034 lvalue. */
7035 STRIP_TYPE_NOPS (size);
7037 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
7039 if (name)
7040 error_at (loc, "size of array %qE has non-integer type",
7041 name);
7042 else
7043 error_at (loc,
7044 "size of unnamed array has non-integer type");
7045 size = integer_one_node;
7046 size_int_const = true;
7048 /* This can happen with enum forward declaration. */
7049 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
7051 if (name)
7052 error_at (loc, "size of array %qE has incomplete type",
7053 name);
7054 else
7055 error_at (loc, "size of unnamed array has incomplete "
7056 "type");
7057 size = integer_one_node;
7058 size_int_const = true;
7061 size = c_fully_fold (size, false, &size_maybe_const);
7063 if (pedantic && size_maybe_const && integer_zerop (size))
7065 if (name)
7066 pedwarn (loc, OPT_Wpedantic,
7067 "ISO C forbids zero-size array %qE", name);
7068 else
7069 pedwarn (loc, OPT_Wpedantic,
7070 "ISO C forbids zero-size array");
7073 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
7075 constant_expression_warning (size);
7076 if (tree_int_cst_sgn (size) < 0)
7078 if (name)
7079 error_at (loc, "size of array %qE is negative", name);
7080 else
7081 error_at (loc, "size of unnamed array is negative");
7082 size = integer_one_node;
7083 size_int_const = true;
7085 /* Handle a size folded to an integer constant but
7086 not an integer constant expression. */
7087 if (!size_int_const)
7089 /* If this is a file scope declaration of an
7090 ordinary identifier, this is invalid code;
7091 diagnosing it here and not subsequently
7092 treating the type as variable-length avoids
7093 more confusing diagnostics later. */
7094 if ((decl_context == NORMAL || decl_context == FIELD)
7095 && current_scope == file_scope)
7096 pedwarn (input_location, 0,
7097 "variably modified %qE at file scope",
7098 name);
7099 else
7100 this_size_varies = size_varies = true;
7101 warn_variable_length_array (name, size);
7104 else if ((decl_context == NORMAL || decl_context == FIELD)
7105 && current_scope == file_scope)
7107 error_at (loc, "variably modified %qE at file scope", name);
7108 size = integer_one_node;
7110 else
7112 /* Make sure the array size remains visibly
7113 nonconstant even if it is (eg) a const variable
7114 with known value. */
7115 this_size_varies = size_varies = true;
7116 warn_variable_length_array (name, size);
7117 if (sanitize_flags_p (SANITIZE_VLA)
7118 && current_function_decl != NULL_TREE
7119 && decl_context == NORMAL)
7121 /* Evaluate the array size only once. */
7122 size = save_expr (size);
7123 size = c_fully_fold (size, false, NULL);
7124 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
7125 ubsan_instrument_vla (loc, size),
7126 size);
7130 if (integer_zerop (size) && !this_size_varies)
7132 /* A zero-length array cannot be represented with
7133 an unsigned index type, which is what we'll
7134 get with build_index_type. Create an
7135 open-ended range instead. */
7136 itype = build_range_type (sizetype, size, NULL_TREE);
7138 else
7140 /* Arrange for the SAVE_EXPR on the inside of the
7141 MINUS_EXPR, which allows the -1 to get folded
7142 with the +1 that happens when building TYPE_SIZE. */
7143 if (size_varies)
7144 size = save_expr (size);
7145 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
7146 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7147 integer_zero_node, size);
7149 /* Compute the maximum valid index, that is, size
7150 - 1. Do the calculation in index_type, so that
7151 if it is a variable the computations will be
7152 done in the proper mode. */
7153 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
7154 convert (index_type, size),
7155 convert (index_type,
7156 size_one_node));
7158 /* The above overflows when size does not fit
7159 in index_type.
7160 ??? While a size of INT_MAX+1 technically shouldn't
7161 cause an overflow (because we subtract 1), handling
7162 this case seems like an unnecessary complication. */
7163 if (TREE_CODE (size) == INTEGER_CST
7164 && !int_fits_type_p (size, index_type))
7166 if (name)
7167 error_at (loc, "size of array %qE is too large",
7168 name);
7169 else
7170 error_at (loc, "size of unnamed array is too large");
7171 type = error_mark_node;
7172 continue;
7175 itype = build_index_type (itype);
7177 if (this_size_varies)
7179 if (TREE_SIDE_EFFECTS (size))
7181 if (*expr)
7182 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
7183 *expr, size);
7184 else
7185 *expr = size;
7187 *expr_const_operands &= size_maybe_const;
7190 else if (decl_context == FIELD)
7192 bool flexible_array_member = false;
7193 if (array_parm_vla_unspec_p)
7194 /* Field names can in fact have function prototype
7195 scope so [*] is disallowed here through making
7196 the field variably modified, not through being
7197 something other than a declaration with function
7198 prototype scope. */
7199 size_varies = true;
7200 else
7202 const struct c_declarator *t = declarator;
7203 while (t->kind == cdk_attrs)
7204 t = t->declarator;
7205 flexible_array_member = (t->kind == cdk_id);
7207 if (flexible_array_member
7208 && !in_system_header_at (input_location))
7209 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7210 "support flexible array members");
7212 /* ISO C99 Flexible array members are effectively
7213 identical to GCC's zero-length array extension. */
7214 if (flexible_array_member || array_parm_vla_unspec_p)
7215 itype = build_range_type (sizetype, size_zero_node,
7216 NULL_TREE);
7218 else if (decl_context == PARM)
7220 if (array_parm_vla_unspec_p)
7222 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7223 size_varies = true;
7226 else if (decl_context == TYPENAME)
7228 if (array_parm_vla_unspec_p)
7230 /* C99 6.7.5.2p4 */
7231 warning (0, "%<[*]%> not in a declaration");
7232 /* We use this to avoid messing up with incomplete
7233 array types of the same type, that would
7234 otherwise be modified below. */
7235 itype = build_range_type (sizetype, size_zero_node,
7236 NULL_TREE);
7237 size_varies = true;
7241 /* Complain about arrays of incomplete types. */
7242 if (!COMPLETE_TYPE_P (type))
7244 auto_diagnostic_group d;
7245 error_at (loc, "array type has incomplete element type %qT",
7246 type);
7247 /* See if we can be more helpful. */
7248 if (TREE_CODE (type) == ARRAY_TYPE)
7250 if (name)
7251 inform (loc, "declaration of %qE as multidimensional "
7252 "array must have bounds for all dimensions "
7253 "except the first", name);
7254 else
7255 inform (loc, "declaration of multidimensional array "
7256 "must have bounds for all dimensions except "
7257 "the first");
7259 type = error_mark_node;
7261 else
7262 /* When itype is NULL, a shared incomplete array type is
7263 returned for all array of a given type. Elsewhere we
7264 make sure we don't complete that type before copying
7265 it, but here we want to make sure we don't ever
7266 modify the shared type, so we gcc_assert (itype)
7267 below. */
7269 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7270 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7271 type = build_qualified_type (type,
7272 ENCODE_QUAL_ADDR_SPACE (as));
7274 type = build_array_type (type, itype);
7277 if (type != error_mark_node)
7279 if (size_varies)
7281 /* It is ok to modify type here even if itype is
7282 NULL: if size_varies, we're in a
7283 multi-dimensional array and the inner type has
7284 variable size, so the enclosing shared array type
7285 must too. */
7286 if (size && TREE_CODE (size) == INTEGER_CST)
7287 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7288 C_TYPE_VARIABLE_SIZE (type) = 1;
7291 /* The GCC extension for zero-length arrays differs from
7292 ISO flexible array members in that sizeof yields
7293 zero. */
7294 if (size && integer_zerop (size))
7296 gcc_assert (itype);
7297 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7298 TYPE_SIZE (type) = bitsize_zero_node;
7299 TYPE_SIZE_UNIT (type) = size_zero_node;
7300 SET_TYPE_STRUCTURAL_EQUALITY (type);
7302 if (array_parm_vla_unspec_p)
7304 gcc_assert (itype);
7305 /* The type is complete. C99 6.7.5.2p4 */
7306 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7307 TYPE_SIZE (type) = bitsize_zero_node;
7308 TYPE_SIZE_UNIT (type) = size_zero_node;
7309 SET_TYPE_STRUCTURAL_EQUALITY (type);
7312 if (!valid_array_size_p (loc, type, name))
7313 type = error_mark_node;
7316 if (decl_context != PARM
7317 && (array_ptr_quals != TYPE_UNQUALIFIED
7318 || array_ptr_attrs != NULL_TREE
7319 || array_parm_static))
7321 error_at (loc, "static or type qualifiers in non-parameter "
7322 "array declarator");
7323 array_ptr_quals = TYPE_UNQUALIFIED;
7324 array_ptr_attrs = NULL_TREE;
7325 array_parm_static = false;
7327 orig_qual_indirect++;
7328 break;
7330 case cdk_function:
7332 /* Say it's a definition only for the declarator closest
7333 to the identifier, apart possibly from some
7334 attributes. */
7335 bool really_funcdef = false;
7336 tree arg_types;
7337 orig_qual_type = NULL_TREE;
7338 if (funcdef_flag)
7340 const struct c_declarator *t = declarator->declarator;
7341 while (t->kind == cdk_attrs)
7342 t = t->declarator;
7343 really_funcdef = (t->kind == cdk_id);
7346 /* Declaring a function type. Make sure we have a valid
7347 type for the function to return. */
7348 if (type == error_mark_node)
7349 continue;
7351 size_varies = false;
7353 /* Warn about some types functions can't return. */
7354 if (TREE_CODE (type) == FUNCTION_TYPE)
7356 if (name)
7357 error_at (loc, "%qE declared as function returning a "
7358 "function", name);
7359 else
7360 error_at (loc, "type name declared as function "
7361 "returning a function");
7362 type = integer_type_node;
7364 if (TREE_CODE (type) == ARRAY_TYPE)
7366 if (name)
7367 error_at (loc, "%qE declared as function returning an array",
7368 name);
7369 else
7370 error_at (loc, "type name declared as function returning "
7371 "an array");
7372 type = integer_type_node;
7375 /* Construct the function type and go to the next
7376 inner layer of declarator. */
7377 arg_info = declarator->u.arg_info;
7378 arg_types = grokparms (arg_info, really_funcdef);
7380 /* Type qualifiers before the return type of the function
7381 qualify the return type, not the function type. */
7382 if (type_quals)
7384 const enum c_declspec_word ignored_quals_list[] =
7386 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7387 cdw_atomic, cdw_number_of_elements
7389 location_t specs_loc
7390 = smallest_type_quals_location (declspecs->locations,
7391 ignored_quals_list);
7392 if (specs_loc == UNKNOWN_LOCATION)
7393 specs_loc = declspecs->locations[cdw_typedef];
7394 if (specs_loc == UNKNOWN_LOCATION)
7395 specs_loc = loc;
7397 /* Type qualifiers on a function return type are
7398 normally permitted by the standard but have no
7399 effect, so give a warning at -Wreturn-type.
7400 Qualifiers on a void return type are banned on
7401 function definitions in ISO C; GCC used to used
7402 them for noreturn functions. The resolution of C11
7403 DR#423 means qualifiers (other than _Atomic) are
7404 actually removed from the return type when
7405 determining the function type. */
7406 int quals_used = type_quals;
7407 if (flag_isoc11)
7408 quals_used &= TYPE_QUAL_ATOMIC;
7409 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7410 pedwarn (specs_loc, 0,
7411 "function definition has qualified void "
7412 "return type");
7413 else
7414 warning_at (specs_loc, OPT_Wignored_qualifiers,
7415 "type qualifiers ignored on function "
7416 "return type");
7418 /* Ensure an error for restrict on invalid types; the
7419 DR#423 resolution is not entirely clear about
7420 this. */
7421 if (flag_isoc11
7422 && (type_quals & TYPE_QUAL_RESTRICT)
7423 && (!POINTER_TYPE_P (type)
7424 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7425 error_at (loc, "invalid use of %<restrict%>");
7426 type = c_build_qualified_type (type, quals_used);
7428 type_quals = TYPE_UNQUALIFIED;
7430 type = build_function_type (type, arg_types,
7431 arg_info->no_named_args_stdarg_p);
7432 declarator = declarator->declarator;
7434 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7435 the formal parameter list of this FUNCTION_TYPE to point to
7436 the FUNCTION_TYPE node itself. */
7438 c_arg_tag *tag;
7439 unsigned ix;
7441 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7442 TYPE_CONTEXT (tag->type) = type;
7444 break;
7446 case cdk_pointer:
7448 /* Merge any constancy or volatility into the target type
7449 for the pointer. */
7450 if ((type_quals & TYPE_QUAL_ATOMIC)
7451 && TREE_CODE (type) == FUNCTION_TYPE)
7453 error_at (loc,
7454 "%<_Atomic%>-qualified function type");
7455 type_quals &= ~TYPE_QUAL_ATOMIC;
7457 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7458 && type_quals)
7459 pedwarn (loc, OPT_Wpedantic,
7460 "ISO C forbids qualified function types");
7461 if (type_quals)
7462 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7463 orig_qual_indirect);
7464 orig_qual_type = NULL_TREE;
7465 size_varies = false;
7467 /* When the pointed-to type involves components of variable size,
7468 care must be taken to ensure that the size evaluation code is
7469 emitted early enough to dominate all the possible later uses
7470 and late enough for the variables on which it depends to have
7471 been assigned.
7473 This is expected to happen automatically when the pointed-to
7474 type has a name/declaration of it's own, but special attention
7475 is required if the type is anonymous.
7477 We attach an artificial TYPE_DECL to such pointed-to type
7478 and arrange for it to be included in a DECL_EXPR. This
7479 forces the sizes evaluation at a safe point and ensures it
7480 is not deferred until e.g. within a deeper conditional context.
7482 PARM contexts have no enclosing statement list that
7483 can hold the DECL_EXPR, so we need to use a BIND_EXPR
7484 instead, and add it to the list of expressions that
7485 need to be evaluated.
7487 TYPENAME contexts do have an enclosing statement list,
7488 but it would be incorrect to use it, as the size should
7489 only be evaluated if the containing expression is
7490 evaluated. We might also be in the middle of an
7491 expression with side effects on the pointed-to type size
7492 "arguments" prior to the pointer declaration point and
7493 the fake TYPE_DECL in the enclosing context would force
7494 the size evaluation prior to the side effects. We therefore
7495 use BIND_EXPRs in TYPENAME contexts too. */
7496 if (!TYPE_NAME (type)
7497 && c_type_variably_modified_p (type))
7499 tree bind = NULL_TREE;
7500 if (decl_context == TYPENAME || decl_context == PARM)
7502 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7503 NULL_TREE, NULL_TREE);
7504 TREE_SIDE_EFFECTS (bind) = 1;
7505 BIND_EXPR_BODY (bind) = push_stmt_list ();
7506 push_scope ();
7508 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7509 DECL_ARTIFICIAL (decl) = 1;
7510 pushdecl (decl);
7511 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7512 TYPE_NAME (type) = decl;
7513 if (bind)
7515 pop_scope ();
7516 BIND_EXPR_BODY (bind)
7517 = pop_stmt_list (BIND_EXPR_BODY (bind));
7518 if (*expr)
7519 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7520 bind);
7521 else
7522 *expr = bind;
7526 type = c_build_pointer_type (type);
7528 /* Process type qualifiers (such as const or volatile)
7529 that were given inside the `*'. */
7530 type_quals = declarator->u.pointer_quals;
7532 declarator = declarator->declarator;
7533 break;
7535 default:
7536 gcc_unreachable ();
7538 if (type != error_mark_node)
7539 C_TYPE_VARIABLY_MODIFIED (type) = varmod || size_varies;
7541 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7542 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7544 /* Now TYPE has the actual type, apart from any qualifiers in
7545 TYPE_QUALS. */
7547 /* Warn about address space used for things other than static memory or
7548 pointers. */
7549 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7550 if (!ADDR_SPACE_GENERIC_P (address_space))
7552 if (decl_context == NORMAL)
7554 switch (storage_class)
7556 case csc_auto:
7557 error ("%qs combined with %<auto%> qualifier for %qE",
7558 c_addr_space_name (address_space), name);
7559 break;
7560 case csc_register:
7561 error ("%qs combined with %<register%> qualifier for %qE",
7562 c_addr_space_name (address_space), name);
7563 break;
7564 case csc_none:
7565 if (current_function_scope)
7567 error ("%qs specified for auto variable %qE",
7568 c_addr_space_name (address_space), name);
7569 break;
7571 break;
7572 case csc_static:
7573 case csc_extern:
7574 case csc_typedef:
7575 break;
7576 default:
7577 gcc_unreachable ();
7580 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7582 if (name)
7583 error ("%qs specified for parameter %qE",
7584 c_addr_space_name (address_space), name);
7585 else
7586 error ("%qs specified for unnamed parameter",
7587 c_addr_space_name (address_space));
7589 else if (decl_context == FIELD)
7591 if (name)
7592 error ("%qs specified for structure field %qE",
7593 c_addr_space_name (address_space), name);
7594 else
7595 error ("%qs specified for structure field",
7596 c_addr_space_name (address_space));
7600 /* Check the type and width of a bit-field. */
7601 if (bitfield)
7603 check_bitfield_type_and_width (loc, &type, width, name);
7604 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7605 atomic types are permitted for bit-fields; we have no code to
7606 make bit-field accesses atomic, so disallow them. */
7607 if (type_quals & TYPE_QUAL_ATOMIC)
7609 if (name)
7610 error_at (loc, "bit-field %qE has atomic type", name);
7611 else
7612 error_at (loc, "bit-field has atomic type");
7613 type_quals &= ~TYPE_QUAL_ATOMIC;
7617 /* Reject invalid uses of _Alignas. */
7618 if (declspecs->alignas_p)
7620 if (storage_class == csc_typedef)
7621 error_at (loc, "alignment specified for typedef %qE", name);
7622 else if (storage_class == csc_register)
7623 error_at (loc, "alignment specified for %<register%> object %qE",
7624 name);
7625 else if (decl_context == PARM)
7627 if (name)
7628 error_at (loc, "alignment specified for parameter %qE", name);
7629 else
7630 error_at (loc, "alignment specified for unnamed parameter");
7632 else if (bitfield)
7634 if (name)
7635 error_at (loc, "alignment specified for bit-field %qE", name);
7636 else
7637 error_at (loc, "alignment specified for unnamed bit-field");
7639 else if (TREE_CODE (type) == FUNCTION_TYPE)
7640 error_at (loc, "alignment specified for function %qE", name);
7641 else if (declspecs->align_log != -1 && TYPE_P (type))
7643 alignas_align = 1U << declspecs->align_log;
7644 if (alignas_align < min_align_of_type (type))
7646 if (name)
7647 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7648 "alignment of %qE", name);
7649 else
7650 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7651 "alignment of unnamed field");
7652 alignas_align = 0;
7657 /* If this is declaring a typedef name, return a TYPE_DECL. */
7659 if (storage_class == csc_typedef)
7661 tree decl;
7662 if ((type_quals & TYPE_QUAL_ATOMIC)
7663 && TREE_CODE (type) == FUNCTION_TYPE)
7665 error_at (loc,
7666 "%<_Atomic%>-qualified function type");
7667 type_quals &= ~TYPE_QUAL_ATOMIC;
7669 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7670 && type_quals)
7671 pedwarn (loc, OPT_Wpedantic,
7672 "ISO C forbids qualified function types");
7673 if (type_quals)
7674 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7675 orig_qual_indirect);
7676 decl = build_decl (declarator->id_loc,
7677 TYPE_DECL, declarator->u.id.id, type);
7678 if (declspecs->explicit_signed_p)
7679 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7680 if (declspecs->inline_p)
7681 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7682 if (declspecs->noreturn_p)
7683 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7685 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7687 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7689 if (b != NULL
7690 && b->decl != NULL_TREE
7691 && (B_IN_CURRENT_SCOPE (b)
7692 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7693 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7695 auto_diagnostic_group d;
7696 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7697 ("using %qD as both a typedef and a tag is "
7698 "invalid in C++"), decl)
7699 && b->locus != UNKNOWN_LOCATION)
7700 inform (b->locus, "originally defined here");
7704 return decl;
7707 /* If this is a type name (such as, in a cast or sizeof),
7708 compute the type and return it now. */
7710 if (decl_context == TYPENAME)
7712 /* Note that the grammar rejects storage classes in typenames
7713 and fields. */
7714 gcc_assert (storage_class == csc_none && !threadp
7715 && !declspecs->inline_p && !declspecs->noreturn_p);
7716 if ((type_quals & TYPE_QUAL_ATOMIC)
7717 && TREE_CODE (type) == FUNCTION_TYPE)
7719 error_at (loc,
7720 "%<_Atomic%>-qualified function type");
7721 type_quals &= ~TYPE_QUAL_ATOMIC;
7723 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7724 && type_quals)
7725 pedwarn (loc, OPT_Wpedantic,
7726 "ISO C forbids const or volatile function types");
7727 if (type_quals)
7728 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7729 orig_qual_indirect);
7730 return type;
7733 if (pedantic && decl_context == FIELD
7734 && c_type_variably_modified_p (type))
7736 /* C99 6.7.2.1p8 */
7737 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7738 "have a variably modified type");
7741 /* Aside from typedefs and type names (handle above),
7742 `void' at top level (not within pointer)
7743 is allowed only in public variables.
7744 We don't complain about parms either, but that is because
7745 a better error message can be made later. */
7747 if (VOID_TYPE_P (type) && decl_context != PARM
7748 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7749 && (storage_class == csc_extern
7750 || (current_scope == file_scope
7751 && !(storage_class == csc_static
7752 || storage_class == csc_register)))))
7754 error_at (loc, "variable or field %qE declared void", name);
7755 type = integer_type_node;
7758 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7759 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7762 tree decl;
7764 if (decl_context == PARM)
7766 tree promoted_type;
7767 bool array_parameter_p = false;
7769 /* A parameter declared as an array of T is really a pointer to T.
7770 One declared as a function is really a pointer to a function. */
7772 if (TREE_CODE (type) == ARRAY_TYPE)
7774 /* Transfer const-ness of array into that of type pointed to. */
7775 type = TREE_TYPE (type);
7776 if (orig_qual_type != NULL_TREE)
7778 if (orig_qual_indirect == 0)
7779 orig_qual_type = TREE_TYPE (orig_qual_type);
7780 else
7781 orig_qual_indirect--;
7783 if (type_quals)
7784 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7785 orig_qual_indirect);
7786 type = c_build_pointer_type (type);
7787 type_quals = array_ptr_quals;
7788 if (type_quals)
7789 type = c_build_qualified_type (type, type_quals);
7791 /* We don't yet implement attributes in this context. */
7792 if (array_ptr_attrs != NULL_TREE)
7793 warning_at (loc, OPT_Wattributes,
7794 "attributes in parameter array declarator ignored");
7796 size_varies = false;
7797 array_parameter_p = true;
7799 else if (TREE_CODE (type) == FUNCTION_TYPE)
7801 if (type_quals & TYPE_QUAL_ATOMIC)
7803 error_at (loc,
7804 "%<_Atomic%>-qualified function type");
7805 type_quals &= ~TYPE_QUAL_ATOMIC;
7807 else if (type_quals)
7808 pedwarn (loc, OPT_Wpedantic,
7809 "ISO C forbids qualified function types");
7810 if (type_quals)
7811 type = c_build_qualified_type (type, type_quals);
7812 type = c_build_pointer_type (type);
7813 type_quals = TYPE_UNQUALIFIED;
7815 else if (type_quals)
7816 type = c_build_qualified_type (type, type_quals);
7818 decl = build_decl (declarator->id_loc,
7819 PARM_DECL, declarator->u.id.id, type);
7820 if (size_varies)
7821 C_DECL_VARIABLE_SIZE (decl) = 1;
7822 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7824 /* Compute the type actually passed in the parmlist,
7825 for the case where there is no prototype.
7826 (For example, shorts and chars are passed as ints.)
7827 When there is a prototype, this is overridden later. */
7829 if (type == error_mark_node)
7830 promoted_type = type;
7831 else
7832 promoted_type = c_type_promotes_to (type);
7834 DECL_ARG_TYPE (decl) = promoted_type;
7835 if (declspecs->inline_p)
7836 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7837 if (declspecs->noreturn_p)
7838 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7840 else if (decl_context == FIELD)
7842 /* Note that the grammar rejects storage classes in typenames
7843 and fields. */
7844 gcc_assert (storage_class == csc_none && !threadp
7845 && !declspecs->inline_p && !declspecs->noreturn_p);
7847 /* Structure field. It may not be a function. */
7849 if (TREE_CODE (type) == FUNCTION_TYPE)
7851 error_at (loc, "field %qE declared as a function", name);
7852 type = build_pointer_type (type);
7854 else if (TREE_CODE (type) != ERROR_MARK
7855 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7857 if (name)
7858 error_at (loc, "field %qE has incomplete type", name);
7859 else
7860 error_at (loc, "unnamed field has incomplete type");
7861 type = error_mark_node;
7863 else if (TREE_CODE (type) == ARRAY_TYPE
7864 && TYPE_DOMAIN (type) == NULL_TREE)
7866 /* We have a flexible array member through a typedef.
7867 Set suitable range. Whether this is a correct position
7868 for a flexible array member will be determined elsewhere. */
7869 if (!in_system_header_at (input_location))
7870 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7871 "support flexible array members");
7872 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7873 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7874 NULL_TREE);
7875 if (orig_qual_indirect == 0)
7876 orig_qual_type = NULL_TREE;
7878 if (type != error_mark_node
7879 && !verify_type_context (loc, TCTX_FIELD, type))
7880 type = error_mark_node;
7882 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7883 orig_qual_indirect);
7884 decl = build_decl (declarator->id_loc,
7885 FIELD_DECL, declarator->u.id.id, type);
7886 DECL_NONADDRESSABLE_P (decl) = bitfield;
7887 if (bitfield && !declarator->u.id.id)
7888 DECL_PADDING_P (decl) = 1;
7890 if (size_varies)
7891 C_DECL_VARIABLE_SIZE (decl) = 1;
7893 else if (TREE_CODE (type) == FUNCTION_TYPE)
7895 if (storage_class == csc_register || threadp || constexprp)
7897 error_at (loc, "invalid storage class for function %qE", name);
7899 else if (current_scope != file_scope)
7901 /* Function declaration not at file scope. Storage
7902 classes other than `extern' are not allowed, C99
7903 6.7.1p5, and `extern' makes no difference. However,
7904 GCC allows 'auto', perhaps with 'inline', to support
7905 nested functions. */
7906 if (storage_class == csc_auto)
7907 pedwarn (loc, OPT_Wpedantic,
7908 "invalid storage class for function %qE", name);
7909 else if (storage_class == csc_static)
7911 error_at (loc, "invalid storage class for function %qE", name);
7912 if (funcdef_flag)
7913 storage_class = declspecs->storage_class = csc_none;
7914 else
7915 return NULL_TREE;
7919 decl = build_decl (declarator->id_loc,
7920 FUNCTION_DECL, declarator->u.id.id, type);
7921 decl = build_decl_attribute_variant (decl, decl_attr);
7923 if (type_quals & TYPE_QUAL_ATOMIC)
7925 error_at (loc,
7926 "%<_Atomic%>-qualified function type");
7927 type_quals &= ~TYPE_QUAL_ATOMIC;
7929 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7930 pedwarn (loc, OPT_Wpedantic,
7931 "ISO C forbids qualified function types");
7933 /* Every function declaration is an external reference
7934 (DECL_EXTERNAL) except for those which are not at file
7935 scope and are explicitly declared "auto". This is
7936 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7937 GCC to signify a forward declaration of a nested function. */
7938 if (storage_class == csc_auto && current_scope != file_scope)
7939 DECL_EXTERNAL (decl) = 0;
7940 /* In C99, a function which is declared 'inline' with 'extern'
7941 is not an external reference (which is confusing). It
7942 means that the later definition of the function must be output
7943 in this file, C99 6.7.4p6. In GNU C89, a function declared
7944 'extern inline' is an external reference. */
7945 else if (declspecs->inline_p && storage_class != csc_static)
7946 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7947 == flag_gnu89_inline);
7948 else
7949 DECL_EXTERNAL (decl) = !initialized;
7951 /* Record absence of global scope for `static' or `auto'. */
7952 TREE_PUBLIC (decl)
7953 = !(storage_class == csc_static || storage_class == csc_auto);
7955 /* For a function definition, record the argument information
7956 block where store_parm_decls will look for it. */
7957 if (funcdef_flag)
7958 current_function_arg_info = arg_info;
7960 if (declspecs->default_int_p)
7961 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7963 /* Record presence of `inline' and `_Noreturn', if it is
7964 reasonable. */
7965 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7967 if (declspecs->inline_p)
7968 pedwarn (loc, 0, "cannot inline function %<main%>");
7969 if (declspecs->noreturn_p)
7970 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7972 else
7974 if (declspecs->inline_p)
7975 /* Record that the function is declared `inline'. */
7976 DECL_DECLARED_INLINE_P (decl) = 1;
7977 if (declspecs->noreturn_p)
7979 if (flag_isoc99)
7980 pedwarn_c99 (loc, OPT_Wpedantic,
7981 "ISO C99 does not support %<_Noreturn%>");
7982 else
7983 pedwarn_c99 (loc, OPT_Wpedantic,
7984 "ISO C90 does not support %<_Noreturn%>");
7985 TREE_THIS_VOLATILE (decl) = 1;
7989 else
7991 /* It's a variable. */
7992 /* An uninitialized decl with `extern' is a reference. */
7993 int extern_ref = !initialized && storage_class == csc_extern;
7995 if (constexprp)
7997 /* The type of a constexpr variable must not be variably
7998 modified, volatile, atomic or restrict qualified or
7999 have a member with such a qualifier. const
8000 qualification is implicitly added, and, at file scope,
8001 has internal linkage. */
8002 if (c_type_variably_modified_p (type))
8003 error_at (loc, "%<constexpr%> object has variably modified "
8004 "type");
8005 if (type_quals
8006 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
8007 error_at (loc, "invalid qualifiers for %<constexpr%> object");
8008 else
8010 tree type_no_array = strip_array_types (type);
8011 if (RECORD_OR_UNION_TYPE_P (type_no_array)
8012 && C_TYPE_FIELDS_NON_CONSTEXPR (type_no_array))
8013 error_at (loc, "invalid qualifiers for field of "
8014 "%<constexpr%> object");
8016 type_quals |= TYPE_QUAL_CONST;
8017 if (current_scope == file_scope)
8018 storage_class = csc_static;
8021 type = c_build_qualified_type (type, type_quals, orig_qual_type,
8022 orig_qual_indirect);
8024 /* C99 6.2.2p7: It is invalid (compile-time undefined
8025 behavior) to create an 'extern' declaration for a
8026 variable if there is a global declaration that is
8027 'static' and the global declaration is not visible.
8028 (If the static declaration _is_ currently visible,
8029 the 'extern' declaration is taken to refer to that decl.) */
8030 if (extern_ref && current_scope != file_scope)
8032 tree global_decl = identifier_global_value (declarator->u.id.id);
8033 tree visible_decl = lookup_name (declarator->u.id.id);
8035 if (global_decl
8036 && global_decl != visible_decl
8037 && VAR_P (global_decl)
8038 && !TREE_PUBLIC (global_decl))
8039 error_at (loc, "variable previously declared %<static%> "
8040 "redeclared %<extern%>");
8043 decl = build_decl (declarator->id_loc,
8044 VAR_DECL, declarator->u.id.id, type);
8045 if (size_varies)
8046 C_DECL_VARIABLE_SIZE (decl) = 1;
8047 if (constexprp)
8048 C_DECL_DECLARED_CONSTEXPR (decl) = 1;
8050 if (declspecs->inline_p)
8051 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
8052 if (declspecs->noreturn_p)
8053 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
8055 /* At file scope, an initialized extern declaration may follow
8056 a static declaration. In that case, DECL_EXTERNAL will be
8057 reset later in start_decl. */
8058 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
8060 /* At file scope, the presence of a `static' or `register' storage
8061 class specifier, or the absence of all storage class specifiers
8062 makes this declaration a definition (perhaps tentative). Also,
8063 the absence of `static' makes it public. */
8064 if (current_scope == file_scope)
8066 TREE_PUBLIC (decl) = storage_class != csc_static;
8067 TREE_STATIC (decl) = !extern_ref;
8069 /* Not at file scope, only `static' makes a static definition. */
8070 else
8072 TREE_STATIC (decl) = (storage_class == csc_static);
8073 TREE_PUBLIC (decl) = extern_ref;
8076 if (threadp)
8077 set_decl_tls_model (decl, decl_default_tls_model (decl));
8080 if ((storage_class == csc_extern
8081 || (storage_class == csc_none
8082 && TREE_CODE (type) == FUNCTION_TYPE
8083 && !funcdef_flag))
8084 && c_type_variably_modified_p (type))
8086 /* C99 6.7.5.2p2 */
8087 if (TREE_CODE (type) == FUNCTION_TYPE)
8088 error_at (loc, "non-nested function with variably modified type");
8089 else
8090 error_at (loc, "object with variably modified type must have "
8091 "no linkage");
8094 /* For nested functions disqualify ones taking VLAs by value
8095 from inlining since the middle-end cannot deal with this.
8096 ??? We should arrange for those to be passed by reference
8097 with emitting the copy on the caller side in the frontend. */
8098 if (storage_class == csc_none
8099 && TREE_CODE (type) == FUNCTION_TYPE)
8100 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
8102 tree arg = TREE_VALUE (al);
8103 if (arg != error_mark_node
8104 && C_TYPE_VARIABLE_SIZE (arg))
8106 DECL_UNINLINABLE (decl) = 1;
8107 break;
8111 /* Record `register' declaration for warnings on &
8112 and in case doing stupid register allocation. */
8114 if (storage_class == csc_register)
8116 C_DECL_REGISTER (decl) = 1;
8117 DECL_REGISTER (decl) = 1;
8120 /* Record constancy and volatility. */
8121 c_apply_type_quals_to_decl (type_quals, decl);
8123 /* Apply _Alignas specifiers. */
8124 if (alignas_align)
8126 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
8127 DECL_USER_ALIGN (decl) = 1;
8130 /* If a type has volatile components, it should be stored in memory.
8131 Otherwise, the fact that those components are volatile
8132 will be ignored, and would even crash the compiler.
8133 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
8134 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
8135 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
8136 || TREE_CODE (decl) == RESULT_DECL))
8138 /* It is not an error for a structure with volatile fields to
8139 be declared register, but reset DECL_REGISTER since it
8140 cannot actually go in a register. */
8141 int was_reg = C_DECL_REGISTER (decl);
8142 C_DECL_REGISTER (decl) = 0;
8143 DECL_REGISTER (decl) = 0;
8144 c_mark_addressable (decl);
8145 C_DECL_REGISTER (decl) = was_reg;
8148 /* This is the earliest point at which we might know the assembler
8149 name of a variable. Thus, if it's known before this, die horribly. */
8150 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
8151 || !DECL_ASSEMBLER_NAME_SET_P (decl));
8153 if (warn_cxx_compat
8154 && VAR_P (decl)
8155 && TREE_PUBLIC (decl)
8156 && TREE_STATIC (decl)
8157 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
8158 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8159 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
8160 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
8161 ("non-local variable %qD with anonymous type is "
8162 "questionable in C++"),
8163 decl);
8165 return decl;
8169 /* Decode the parameter-list info for a function type or function definition.
8170 The argument is the value returned by `get_parm_info' (or made in c-parse.c
8171 if there is an identifier list instead of a parameter decl list).
8172 These two functions are separate because when a function returns
8173 or receives functions then each is called multiple times but the order
8174 of calls is different. The last call to `grokparms' is always the one
8175 that contains the formal parameter names of a function definition.
8177 Return a list of arg types to use in the FUNCTION_TYPE for this function.
8179 FUNCDEF_FLAG is true for a function definition, false for
8180 a mere declaration. A nonempty identifier-list gets an error message
8181 when FUNCDEF_FLAG is false. */
8183 static tree
8184 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
8186 tree arg_types = arg_info->types;
8188 if (funcdef_flag && arg_info->had_vla_unspec)
8190 /* A function definition isn't function prototype scope C99 6.2.1p4. */
8191 /* C99 6.7.5.2p4 */
8192 error ("%<[*]%> not allowed in other than function prototype scope");
8195 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc2x
8196 && !in_system_header_at (input_location))
8197 warning (OPT_Wstrict_prototypes,
8198 "function declaration isn%'t a prototype");
8200 if (arg_types == error_mark_node)
8201 /* Don't set TYPE_ARG_TYPES in this case. */
8202 return NULL_TREE;
8204 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
8206 if (!funcdef_flag)
8208 pedwarn (input_location, 0, "parameter names (without types) in "
8209 "function declaration");
8210 arg_info->parms = NULL_TREE;
8212 else
8213 arg_info->parms = arg_info->types;
8215 arg_info->types = NULL_TREE;
8216 return NULL_TREE;
8218 else
8220 tree parm, type, typelt;
8221 unsigned int parmno;
8223 /* In C2X, convert () to (void). */
8224 if (flag_isoc2x
8225 && !arg_types
8226 && !arg_info->parms
8227 && !arg_info->no_named_args_stdarg_p)
8228 arg_types = arg_info->types = void_list_node;
8230 /* If there is a parameter of incomplete type in a definition,
8231 this is an error. In a declaration this is valid, and a
8232 struct or union type may be completed later, before any calls
8233 or definition of the function. In the case where the tag was
8234 first declared within the parameter list, a warning has
8235 already been given. If a parameter has void type, then
8236 however the function cannot be defined or called, so
8237 warn. */
8239 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8240 parm;
8241 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8243 type = TREE_VALUE (typelt);
8244 if (type == error_mark_node)
8245 continue;
8247 if (!COMPLETE_TYPE_P (type))
8249 if (funcdef_flag)
8251 if (DECL_NAME (parm))
8252 error_at (input_location,
8253 "parameter %u (%q+D) has incomplete type",
8254 parmno, parm);
8255 else
8256 error_at (DECL_SOURCE_LOCATION (parm),
8257 "parameter %u has incomplete type",
8258 parmno);
8260 TREE_VALUE (typelt) = error_mark_node;
8261 TREE_TYPE (parm) = error_mark_node;
8262 arg_types = NULL_TREE;
8264 else if (VOID_TYPE_P (type))
8266 if (DECL_NAME (parm))
8267 warning_at (input_location, 0,
8268 "parameter %u (%q+D) has void type",
8269 parmno, parm);
8270 else
8271 warning_at (DECL_SOURCE_LOCATION (parm), 0,
8272 "parameter %u has void type",
8273 parmno);
8277 if (DECL_NAME (parm) && TREE_USED (parm))
8278 warn_if_shadowing (parm);
8280 return arg_types;
8284 /* Allocate and initialize a c_arg_info structure from the parser's
8285 obstack. */
8287 struct c_arg_info *
8288 build_arg_info (void)
8290 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8291 ret->parms = NULL_TREE;
8292 ret->tags = NULL;
8293 ret->types = NULL_TREE;
8294 ret->others = NULL_TREE;
8295 ret->pending_sizes = NULL;
8296 ret->had_vla_unspec = 0;
8297 ret->no_named_args_stdarg_p = 0;
8298 return ret;
8301 /* Take apart the current scope and return a c_arg_info structure with
8302 info on a parameter list just parsed.
8304 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8306 ELLIPSIS being true means the argument list ended in '...' so don't
8307 append a sentinel (void_list_node) to the end of the type-list.
8309 EXPR is NULL or an expression that needs to be evaluated for the
8310 side effects of array size expressions in the parameters. */
8312 struct c_arg_info *
8313 get_parm_info (bool ellipsis, tree expr)
8315 struct c_binding *b = current_scope->bindings;
8316 struct c_arg_info *arg_info = build_arg_info ();
8318 tree parms = NULL_TREE;
8319 vec<c_arg_tag, va_gc> *tags = NULL;
8320 tree types = NULL_TREE;
8321 tree others = NULL_TREE;
8323 bool gave_void_only_once_err = false;
8325 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8327 /* The bindings in this scope must not get put into a block.
8328 We will take care of deleting the binding nodes. */
8329 current_scope->bindings = 0;
8331 /* This function is only called if there was *something* on the
8332 parameter list. */
8333 gcc_assert (b);
8335 /* A parameter list consisting solely of 'void' indicates that the
8336 function takes no arguments. But if the 'void' is qualified
8337 (by 'const' or 'volatile'), or has a storage class specifier
8338 ('register'), then the behavior is undefined; issue an error.
8339 Typedefs for 'void' are OK (see DR#157). */
8340 if (b->prev == 0 /* one binding */
8341 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8342 && !DECL_NAME (b->decl) /* anonymous */
8343 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8345 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8346 || C_DECL_REGISTER (b->decl))
8347 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8349 /* There cannot be an ellipsis. */
8350 if (ellipsis)
8351 error_at (b->locus, "%<void%> must be the only parameter");
8353 arg_info->types = void_list_node;
8354 return arg_info;
8357 if (!ellipsis)
8358 types = void_list_node;
8360 /* Break up the bindings list into parms, tags, types, and others;
8361 apply sanity checks; purge the name-to-decl bindings. */
8362 while (b)
8364 tree decl = b->decl;
8365 tree type = TREE_TYPE (decl);
8366 c_arg_tag tag;
8367 const char *keyword;
8369 switch (TREE_CODE (decl))
8371 case PARM_DECL:
8372 if (b->id)
8374 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8375 I_SYMBOL_BINDING (b->id) = b->shadowed;
8378 /* Check for forward decls that never got their actual decl. */
8379 if (TREE_ASM_WRITTEN (decl))
8380 error_at (b->locus,
8381 "parameter %q+D has just a forward declaration", decl);
8382 /* Check for (..., void, ...) and issue an error. */
8383 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8385 if (!gave_void_only_once_err)
8387 error_at (b->locus, "%<void%> must be the only parameter");
8388 gave_void_only_once_err = true;
8391 else
8393 /* Valid parameter, add it to the list. */
8394 DECL_CHAIN (decl) = parms;
8395 parms = decl;
8397 /* Since there is a prototype, args are passed in their
8398 declared types. The back end may override this later. */
8399 DECL_ARG_TYPE (decl) = type;
8400 types = tree_cons (0, type, types);
8402 break;
8404 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8405 case UNION_TYPE: keyword = "union"; goto tag;
8406 case RECORD_TYPE: keyword = "struct"; goto tag;
8407 tag:
8408 /* Types may not have tag-names, in which case the type
8409 appears in the bindings list with b->id NULL. */
8410 if (b->id)
8412 gcc_assert (I_TAG_BINDING (b->id) == b);
8413 I_TAG_BINDING (b->id) = b->shadowed;
8416 /* Warn about any struct, union or enum tags defined in a
8417 parameter list. The scope of such types is limited to
8418 the parameter list, which is rarely if ever desirable
8419 (it's impossible to call such a function with type-
8420 correct arguments). An anonymous union parm type is
8421 meaningful as a GNU extension, so don't warn for that. */
8422 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8424 if (b->id)
8425 /* The %s will be one of 'struct', 'union', or 'enum'. */
8426 warning_at (b->locus, 0,
8427 "%<%s %E%> declared inside parameter list"
8428 " will not be visible outside of this definition or"
8429 " declaration", keyword, b->id);
8430 else
8431 /* The %s will be one of 'struct', 'union', or 'enum'. */
8432 warning_at (b->locus, 0,
8433 "anonymous %s declared inside parameter list"
8434 " will not be visible outside of this definition or"
8435 " declaration", keyword);
8438 tag.id = b->id;
8439 tag.type = decl;
8440 vec_safe_push (tags, tag);
8441 break;
8443 case FUNCTION_DECL:
8444 /* FUNCTION_DECLs appear when there is an implicit function
8445 declaration in the parameter list. */
8446 gcc_assert (b->nested || seen_error ());
8447 goto set_shadowed;
8449 case CONST_DECL:
8450 case TYPE_DECL:
8451 /* CONST_DECLs appear here when we have an embedded enum,
8452 and TYPE_DECLs appear here when we have an embedded struct
8453 or union. No warnings for this - we already warned about the
8454 type itself. */
8456 /* When we reinsert this decl in the function body, we need
8457 to reconstruct whether it was marked as nested. */
8458 gcc_assert (!b->nested);
8459 DECL_CHAIN (decl) = others;
8460 others = decl;
8461 /* fall through */
8463 case ERROR_MARK:
8464 set_shadowed:
8465 /* error_mark_node appears here when we have an undeclared
8466 variable. Just throw it away. */
8467 if (b->id)
8469 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8470 I_SYMBOL_BINDING (b->id) = b->shadowed;
8472 break;
8474 /* Other things that might be encountered. */
8475 case LABEL_DECL:
8476 case VAR_DECL:
8477 default:
8478 gcc_unreachable ();
8481 b = free_binding_and_advance (b);
8484 arg_info->parms = parms;
8485 arg_info->tags = tags;
8486 arg_info->types = types;
8487 arg_info->others = others;
8488 arg_info->pending_sizes = expr;
8489 arg_info->no_named_args_stdarg_p = ellipsis && !types;
8490 return arg_info;
8493 /* Get the struct, enum or union (CODE says which) with tag NAME.
8494 Define the tag as a forward-reference with location LOC if it is
8495 not defined. HAVE_STD_ATTRS says whether any standard attributes
8496 were present after the struct, union or enum keyword; ATTRS are the
8497 standard attributes present there. HAS_ENUM_TYPE_SPECIFIER says
8498 whether an enum type specifier (": specifier-qualifier-list") is
8499 present; if so, this is called before that specifier is parsed, so
8500 that the tag is in scope for that specifier. Return a c_typespec
8501 structure for the type specifier. */
8503 struct c_typespec
8504 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8505 bool have_std_attrs, tree attrs, bool has_enum_type_specifier)
8507 struct c_typespec ret;
8508 tree ref;
8509 location_t refloc;
8511 ret.expr = NULL_TREE;
8512 ret.expr_const_operands = true;
8513 ret.has_enum_type_specifier = has_enum_type_specifier;
8515 /* If a cross reference is requested, look up the type already
8516 defined for this tag and return it. If an enum type specifier is
8517 present, only a definition in the current scope is relevant. */
8519 ref = lookup_tag (code, name, has_enum_type_specifier, &refloc);
8520 /* If this is the right type of tag, return what we found.
8521 (This reference will be shadowed by shadow_tag later if appropriate.)
8522 If this is the wrong type of tag, do not return it. If it was the
8523 wrong type in the same scope, we will have had an error
8524 message already; if in a different scope and declaring
8525 a name, pending_xref_error will give an error message; but if in a
8526 different scope and not declaring a name, this tag should
8527 shadow the previous declaration of a different type of tag, and
8528 this would not work properly if we return the reference found.
8529 (For example, with "struct foo" in an outer scope, "union foo;"
8530 must shadow that tag with a new one of union type.) */
8531 ret.kind = (ref
8532 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8533 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8534 if (ref && TREE_CODE (ref) == code)
8536 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8537 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8538 && loc != UNKNOWN_LOCATION
8539 && warn_cxx_compat)
8541 auto_diagnostic_group d;
8542 switch (code)
8544 case ENUMERAL_TYPE:
8545 if (warning_at (loc, OPT_Wc___compat,
8546 ("enum type defined in struct or union "
8547 "is not visible in C++")))
8548 inform (refloc, "enum type defined here");
8549 break;
8550 case RECORD_TYPE:
8551 if (warning_at (loc, OPT_Wc___compat,
8552 ("struct defined in struct or union "
8553 "is not visible in C++")))
8554 inform (refloc, "struct defined here");
8555 break;
8556 case UNION_TYPE:
8557 if (warning_at (loc, OPT_Wc___compat,
8558 ("union defined in struct or union "
8559 "is not visible in C++")))
8560 inform (refloc, "union defined here");
8561 break;
8562 default:
8563 gcc_unreachable();
8567 ret.spec = ref;
8568 return ret;
8571 /* If no such tag is yet defined, create a forward-reference node
8572 and record it as the "definition".
8573 When a real declaration of this type is found,
8574 the forward-reference will be altered into a real type. */
8576 ref = make_node (code);
8577 if (code == ENUMERAL_TYPE)
8579 /* Give the type a default layout like unsigned int
8580 to avoid crashing if it does not get defined. */
8581 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8582 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8583 TYPE_USER_ALIGN (ref) = 0;
8584 TYPE_UNSIGNED (ref) = 1;
8585 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8586 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8587 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8588 ENUM_FIXED_UNDERLYING_TYPE_P (ref) = has_enum_type_specifier;
8591 pushtag (loc, name, ref);
8592 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8593 if (in_underspecified_init)
8594 error_at (loc, "%qT declared in underspecified object initializer",
8595 ref);
8597 ret.spec = ref;
8598 return ret;
8601 /* Get the struct, enum or union (CODE says which) with tag NAME.
8602 Define the tag as a forward-reference if it is not defined.
8603 Return a tree for the type. */
8605 tree
8606 xref_tag (enum tree_code code, tree name)
8608 return parser_xref_tag (input_location, code, name, false, NULL_TREE,
8609 false).spec;
8612 /* Make sure that the tag NAME is defined *in the current scope*
8613 at least as a forward reference.
8614 LOC is the location of the struct's definition.
8615 CODE says which kind of tag NAME ought to be.
8617 This stores the current value of the file static STRUCT_PARSE_INFO
8618 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8619 new c_struct_parse_info structure. The old value of
8620 STRUCT_PARSE_INFO is restored in finish_struct. */
8622 tree
8623 start_struct (location_t loc, enum tree_code code, tree name,
8624 class c_struct_parse_info **enclosing_struct_parse_info)
8626 /* If there is already a tag defined at this scope
8627 (as a forward reference), just return it. */
8629 tree ref = NULL_TREE;
8630 location_t refloc = UNKNOWN_LOCATION;
8632 if (name != NULL_TREE)
8633 ref = lookup_tag (code, name, true, &refloc);
8634 if (ref && TREE_CODE (ref) == code)
8636 if (TYPE_STUB_DECL (ref))
8637 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8639 if (TYPE_SIZE (ref))
8641 auto_diagnostic_group d;
8642 if (code == UNION_TYPE)
8643 error_at (loc, "redefinition of %<union %E%>", name);
8644 else
8645 error_at (loc, "redefinition of %<struct %E%>", name);
8646 if (refloc != UNKNOWN_LOCATION)
8647 inform (refloc, "originally defined here");
8648 /* Don't create structures using a name already in use. */
8649 ref = NULL_TREE;
8651 else if (C_TYPE_BEING_DEFINED (ref))
8653 if (code == UNION_TYPE)
8654 error_at (loc, "nested redefinition of %<union %E%>", name);
8655 else
8656 error_at (loc, "nested redefinition of %<struct %E%>", name);
8657 /* Don't bother to report "originally defined here" for a
8658 nested redefinition; the original definition should be
8659 obvious. */
8660 /* Don't create structures that contain themselves. */
8661 ref = NULL_TREE;
8665 /* Otherwise create a forward-reference just so the tag is in scope. */
8667 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8669 ref = make_node (code);
8670 pushtag (loc, name, ref);
8673 C_TYPE_BEING_DEFINED (ref) = 1;
8674 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8675 TYPE_PACKED (v) = flag_pack_struct;
8677 *enclosing_struct_parse_info = struct_parse_info;
8678 struct_parse_info = new c_struct_parse_info ();
8680 /* FIXME: This will issue a warning for a use of a type defined
8681 within a statement expr used within sizeof, et. al. This is not
8682 terribly serious as C++ doesn't permit statement exprs within
8683 sizeof anyhow. */
8684 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8685 warning_at (loc, OPT_Wc___compat,
8686 "defining type in %qs expression is invalid in C++",
8687 (in_sizeof
8688 ? "sizeof"
8689 : (in_typeof ? "typeof" : "alignof")));
8691 if (in_underspecified_init)
8692 error_at (loc, "%qT defined in underspecified object initializer", ref);
8694 return ref;
8697 /* Process the specs, declarator and width (NULL if omitted)
8698 of a structure component, returning a FIELD_DECL node.
8699 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8700 DECL_ATTRS is as for grokdeclarator.
8702 LOC is the location of the structure component.
8704 This is done during the parsing of the struct declaration.
8705 The FIELD_DECL nodes are chained together and the lot of them
8706 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8708 tree
8709 grokfield (location_t loc,
8710 struct c_declarator *declarator, struct c_declspecs *declspecs,
8711 tree width, tree *decl_attrs)
8713 tree value;
8715 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8716 && width == NULL_TREE)
8718 /* This is an unnamed decl.
8720 If we have something of the form "union { list } ;" then this
8721 is the anonymous union extension. Similarly for struct.
8723 If this is something of the form "struct foo;", then
8724 If MS or Plan 9 extensions are enabled, this is handled as
8725 an anonymous struct.
8726 Otherwise this is a forward declaration of a structure tag.
8728 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8729 If foo names a structure or union without a tag, then this
8730 is an anonymous struct (this is permitted by C11).
8731 If MS or Plan 9 extensions are enabled and foo names a
8732 structure, then again this is an anonymous struct.
8733 Otherwise this is an error.
8735 Oh what a horrid tangled web we weave. I wonder if MS consciously
8736 took this from Plan 9 or if it was an accident of implementation
8737 that took root before someone noticed the bug... */
8739 tree type = declspecs->type;
8740 bool ok = false;
8742 if (RECORD_OR_UNION_TYPE_P (type)
8743 && (flag_ms_extensions
8744 || flag_plan9_extensions
8745 || !declspecs->typedef_p))
8747 if (flag_ms_extensions || flag_plan9_extensions)
8748 ok = true;
8749 else if (TYPE_NAME (type) == NULL)
8750 ok = true;
8751 else
8752 ok = false;
8754 if (!ok)
8756 pedwarn (loc, 0, "declaration does not declare anything");
8757 return NULL_TREE;
8759 if (flag_isoc99)
8760 pedwarn_c99 (loc, OPT_Wpedantic,
8761 "ISO C99 doesn%'t support unnamed structs/unions");
8762 else
8763 pedwarn_c99 (loc, OPT_Wpedantic,
8764 "ISO C90 doesn%'t support unnamed structs/unions");
8767 value = grokdeclarator (declarator, declspecs, FIELD, false,
8768 width ? &width : NULL, decl_attrs, NULL, NULL,
8769 DEPRECATED_NORMAL);
8771 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8772 DECL_INITIAL (value) = width;
8773 if (width)
8774 SET_DECL_C_BIT_FIELD (value);
8776 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8778 /* If we currently have a binding for this field, set the
8779 in_struct field in the binding, so that we warn about lookups
8780 which find it. */
8781 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8782 if (b != NULL)
8784 /* If the in_struct field is not yet set, push it on a list
8785 to be cleared when this struct is finished. */
8786 if (!b->in_struct)
8788 struct_parse_info->fields.safe_push (b);
8789 b->in_struct = 1;
8794 return value;
8797 /* Subroutine of detect_field_duplicates: return whether X and Y,
8798 which are both fields in the same struct, have duplicate field
8799 names. */
8801 static bool
8802 is_duplicate_field (tree x, tree y)
8804 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8805 return true;
8807 /* When using -fplan9-extensions, an anonymous field whose name is a
8808 typedef can duplicate a field name. */
8809 if (flag_plan9_extensions
8810 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8812 tree xt, xn, yt, yn;
8814 xt = TREE_TYPE (x);
8815 if (DECL_NAME (x) != NULL_TREE)
8816 xn = DECL_NAME (x);
8817 else if (RECORD_OR_UNION_TYPE_P (xt)
8818 && TYPE_NAME (xt) != NULL_TREE
8819 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8820 xn = DECL_NAME (TYPE_NAME (xt));
8821 else
8822 xn = NULL_TREE;
8824 yt = TREE_TYPE (y);
8825 if (DECL_NAME (y) != NULL_TREE)
8826 yn = DECL_NAME (y);
8827 else if (RECORD_OR_UNION_TYPE_P (yt)
8828 && TYPE_NAME (yt) != NULL_TREE
8829 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8830 yn = DECL_NAME (TYPE_NAME (yt));
8831 else
8832 yn = NULL_TREE;
8834 if (xn != NULL_TREE && xn == yn)
8835 return true;
8838 return false;
8841 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8842 to HTAB, giving errors for any duplicates. */
8844 static void
8845 detect_field_duplicates_hash (tree fieldlist,
8846 hash_table<nofree_ptr_hash <tree_node> > *htab)
8848 tree x, y;
8849 tree_node **slot;
8851 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8852 if ((y = DECL_NAME (x)) != NULL_TREE)
8854 slot = htab->find_slot (y, INSERT);
8855 if (*slot)
8857 error ("duplicate member %q+D", x);
8858 DECL_NAME (x) = NULL_TREE;
8860 *slot = y;
8862 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8864 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8866 /* When using -fplan9-extensions, an anonymous field whose
8867 name is a typedef can duplicate a field name. */
8868 if (flag_plan9_extensions
8869 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8870 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8872 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8873 slot = htab->find_slot (xn, INSERT);
8874 if (*slot)
8875 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8876 *slot = xn;
8881 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8882 the list such that this does not present a problem later. */
8884 static void
8885 detect_field_duplicates (tree fieldlist)
8887 tree x, y;
8888 int timeout = 10;
8890 /* If the struct is the list of instance variables of an Objective-C
8891 class, then we need to check all the instance variables of
8892 superclasses when checking for duplicates (since you can't have
8893 an instance variable in a subclass with the same name as an
8894 instance variable in a superclass). We pass on this job to the
8895 Objective-C compiler. objc_detect_field_duplicates() will return
8896 false if we are not checking the list of instance variables and
8897 the C frontend should proceed with the standard field duplicate
8898 checks. If we are checking the list of instance variables, the
8899 ObjC frontend will do the check, emit the errors if needed, and
8900 then return true. */
8901 if (c_dialect_objc ())
8902 if (objc_detect_field_duplicates (false))
8903 return;
8905 /* First, see if there are more than "a few" fields.
8906 This is trivially true if there are zero or one fields. */
8907 if (!fieldlist || !DECL_CHAIN (fieldlist))
8908 return;
8909 x = fieldlist;
8910 do {
8911 timeout--;
8912 if (DECL_NAME (x) == NULL_TREE
8913 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8914 timeout = 0;
8915 x = DECL_CHAIN (x);
8916 } while (timeout > 0 && x);
8918 /* If there were "few" fields and no anonymous structures or unions,
8919 avoid the overhead of allocating a hash table. Instead just do
8920 the nested traversal thing. */
8921 if (timeout > 0)
8923 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8924 /* When using -fplan9-extensions, we can have duplicates
8925 between typedef names and fields. */
8926 if (DECL_NAME (x)
8927 || (flag_plan9_extensions
8928 && DECL_NAME (x) == NULL_TREE
8929 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8930 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8931 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8933 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8934 if (is_duplicate_field (y, x))
8936 error ("duplicate member %q+D", x);
8937 DECL_NAME (x) = NULL_TREE;
8941 else
8943 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8944 detect_field_duplicates_hash (fieldlist, &htab);
8948 /* Finish up struct info used by -Wc++-compat. */
8950 static void
8951 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8952 location_t record_loc)
8954 unsigned int ix;
8955 tree x;
8956 struct c_binding *b;
8958 if (fieldlist == NULL_TREE)
8960 if (code == RECORD_TYPE)
8961 warning_at (record_loc, OPT_Wc___compat,
8962 "empty struct has size 0 in C, size 1 in C++");
8963 else
8964 warning_at (record_loc, OPT_Wc___compat,
8965 "empty union has size 0 in C, size 1 in C++");
8968 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8969 the current struct. We do this now at the end of the struct
8970 because the flag is used to issue visibility warnings, and we
8971 only want to issue those warnings if the type is referenced
8972 outside of the struct declaration. */
8973 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8974 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8976 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8977 typedefs used when declaring fields in this struct. If the name
8978 of any of the fields is also a typedef name then the struct would
8979 not parse in C++, because the C++ lookup rules say that the
8980 typedef name would be looked up in the context of the struct, and
8981 would thus be the field rather than the typedef. */
8982 if (!struct_parse_info->typedefs_seen.is_empty ()
8983 && fieldlist != NULL_TREE)
8985 /* Use a hash_set<tree> using the name of the typedef. We can use
8986 a hash_set<tree> because identifiers are interned. */
8987 hash_set<tree> tset;
8989 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8990 tset.add (DECL_NAME (x));
8992 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8994 if (DECL_NAME (x) != NULL_TREE
8995 && tset.contains (DECL_NAME (x)))
8997 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8998 ("using %qD as both field and typedef name is "
8999 "invalid in C++"),
9001 /* FIXME: It would be nice to report the location where
9002 the typedef name is used. */
9007 /* For each field which has a binding and which was not defined in
9008 an enclosing struct, clear the in_struct field. */
9009 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
9010 b->in_struct = 0;
9013 /* Function to help qsort sort FIELD_DECLs by name order. */
9015 static int
9016 field_decl_cmp (const void *x_p, const void *y_p)
9018 const tree *const x = (const tree *) x_p;
9019 const tree *const y = (const tree *) y_p;
9021 if (DECL_NAME (*x) == DECL_NAME (*y))
9022 /* A nontype is "greater" than a type. */
9023 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9024 if (DECL_NAME (*x) == NULL_TREE)
9025 return -1;
9026 if (DECL_NAME (*y) == NULL_TREE)
9027 return 1;
9028 if (DECL_NAME (*x) < DECL_NAME (*y))
9029 return -1;
9030 return 1;
9033 /* If this structure or union completes the type of any previous
9034 variable declaration, lay it out and output its rtl. */
9035 static void
9036 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
9038 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
9040 tree decl = TREE_VALUE (x);
9041 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
9042 layout_array_type (TREE_TYPE (decl));
9043 if (TREE_CODE (decl) != TYPE_DECL)
9045 relayout_decl (decl);
9046 if (c_dialect_objc ())
9047 objc_check_decl (decl);
9048 rest_of_decl_compilation (decl, toplevel, 0);
9053 /* Determine whether the FIELD_DECL X is a flexible array member according to
9054 the following info:
9055 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
9056 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
9057 or "[1]";
9058 C. flag_strict_flex_arrays;
9059 D. the attribute strict_flex_array that is attached to the field
9060 if presenting.
9061 Return TRUE when it's a flexible array member, FALSE otherwise. */
9063 static bool
9064 is_flexible_array_member_p (bool is_last_field,
9065 tree x)
9067 /* If not the last field, return false. */
9068 if (!is_last_field)
9069 return false;
9071 /* If not an array field, return false. */
9072 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9073 return false;
9075 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
9076 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
9077 bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
9079 unsigned int strict_flex_array_level = c_strict_flex_array_level_of (x);
9081 switch (strict_flex_array_level)
9083 case 0:
9084 /* Default, all trailing arrays are flexible array members. */
9085 return true;
9086 case 1:
9087 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
9088 if (is_one_element_array)
9089 return true;
9090 /* FALLTHROUGH. */
9091 case 2:
9092 /* Level 2: all "[0]", and "[]" are flexible array members. */
9093 if (is_zero_length_array)
9094 return true;
9095 /* FALLTHROUGH. */
9096 case 3:
9097 /* Level 3: Only "[]" are flexible array members. */
9098 if (is_flexible_array)
9099 return true;
9100 break;
9101 default:
9102 gcc_unreachable ();
9104 return false;
9108 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
9109 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
9110 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
9111 ATTRIBUTES are attributes to be applied to the structure.
9113 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
9114 the struct was started. */
9116 tree
9117 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
9118 class c_struct_parse_info *enclosing_struct_parse_info)
9120 tree x;
9121 bool toplevel = file_scope == current_scope;
9123 /* If this type was previously laid out as a forward reference,
9124 make sure we lay it out again. */
9126 TYPE_SIZE (t) = NULL_TREE;
9128 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9130 if (pedantic)
9132 for (x = fieldlist; x; x = DECL_CHAIN (x))
9134 if (DECL_NAME (x) != NULL_TREE)
9135 break;
9136 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9137 break;
9140 if (x == NULL_TREE)
9142 if (TREE_CODE (t) == UNION_TYPE)
9144 if (fieldlist)
9145 pedwarn (loc, OPT_Wpedantic, "union has no named members");
9146 else
9147 pedwarn (loc, OPT_Wpedantic, "union has no members");
9149 else
9151 if (fieldlist)
9152 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
9153 else
9154 pedwarn (loc, OPT_Wpedantic, "struct has no members");
9159 /* Install struct as DECL_CONTEXT of each field decl.
9160 Also process specified field sizes, found in the DECL_INITIAL,
9161 storing 0 there after the type has been changed to precision equal
9162 to its width, rather than the precision of the specified standard
9163 type. (Correct layout requires the original type to have been preserved
9164 until now.) */
9166 bool saw_named_field = false;
9167 for (x = fieldlist; x; x = DECL_CHAIN (x))
9169 /* Whether this field is the last field of the structure or union.
9170 for UNION, any field is the last field of it. */
9171 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
9172 || (TREE_CODE (t) == UNION_TYPE);
9174 if (TREE_TYPE (x) == error_mark_node)
9175 continue;
9177 DECL_CONTEXT (x) = t;
9179 tree t1 = strip_array_types (TREE_TYPE (x));
9180 /* If any field is const, the structure type is pseudo-const. */
9181 if (TREE_READONLY (x))
9182 C_TYPE_FIELDS_READONLY (t) = 1;
9183 else
9185 /* A field that is pseudo-const makes the structure likewise. */
9186 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
9187 C_TYPE_FIELDS_READONLY (t) = 1;
9190 /* Any field that is volatile means variables of this type must be
9191 treated in some ways as volatile. */
9192 if (TREE_THIS_VOLATILE (x))
9194 C_TYPE_FIELDS_VOLATILE (t) = 1;
9195 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9198 /* Any field that is volatile, restrict-qualified or atomic
9199 means the type cannot be used for a constexpr object. */
9200 if (TYPE_QUALS (t1)
9201 & (TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC))
9202 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9203 else if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_NON_CONSTEXPR (t1))
9204 C_TYPE_FIELDS_NON_CONSTEXPR (t) = 1;
9206 /* Any field of nominal variable size implies structure is too. */
9207 if (C_DECL_VARIABLE_SIZE (x))
9208 C_TYPE_VARIABLE_SIZE (t) = 1;
9210 /* If any field is variably modified, record this fact. */
9211 if (C_TYPE_VARIABLY_MODIFIED (TREE_TYPE (x)))
9212 C_TYPE_VARIABLY_MODIFIED (t) = 1;
9214 if (DECL_C_BIT_FIELD (x))
9216 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
9217 DECL_SIZE (x) = bitsize_int (width);
9218 DECL_BIT_FIELD (x) = 1;
9221 if (TYPE_PACKED (t)
9222 && (DECL_BIT_FIELD (x)
9223 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
9224 DECL_PACKED (x) = 1;
9226 /* Detect flexible array member in an invalid context. */
9227 if (flexible_array_member_type_p (TREE_TYPE (x)))
9229 if (TREE_CODE (t) == UNION_TYPE)
9231 error_at (DECL_SOURCE_LOCATION (x),
9232 "flexible array member in union");
9233 TREE_TYPE (x) = error_mark_node;
9235 else if (!is_last_field)
9237 error_at (DECL_SOURCE_LOCATION (x),
9238 "flexible array member not at end of struct");
9239 TREE_TYPE (x) = error_mark_node;
9241 else if (!saw_named_field)
9243 error_at (DECL_SOURCE_LOCATION (x),
9244 "flexible array member in a struct with no named "
9245 "members");
9246 TREE_TYPE (x) = error_mark_node;
9250 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9251 && flexible_array_type_p (TREE_TYPE (x)))
9252 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9253 "invalid use of structure with flexible array member");
9255 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9256 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9258 if (DECL_NAME (x)
9259 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9260 saw_named_field = true;
9263 detect_field_duplicates (fieldlist);
9265 /* Now we have the nearly final fieldlist. Record it,
9266 then lay out the structure or union (including the fields). */
9268 TYPE_FIELDS (t) = fieldlist;
9270 maybe_apply_pragma_scalar_storage_order (t);
9272 layout_type (t);
9274 if (TYPE_SIZE_UNIT (t)
9275 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9276 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9277 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9278 error ("type %qT is too large", t);
9280 /* Give bit-fields their proper types and rewrite the type of array fields
9281 with scalar component if the enclosing type has reverse storage order. */
9282 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9284 if (TREE_CODE (field) == FIELD_DECL
9285 && DECL_INITIAL (field)
9286 && TREE_TYPE (field) != error_mark_node)
9288 unsigned HOST_WIDE_INT width
9289 = tree_to_uhwi (DECL_INITIAL (field));
9290 tree type = TREE_TYPE (field);
9291 if (width != TYPE_PRECISION (type))
9293 TREE_TYPE (field)
9294 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
9295 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9297 DECL_INITIAL (field) = NULL_TREE;
9299 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9300 && TREE_CODE (field) == FIELD_DECL
9301 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9303 tree ftype = TREE_TYPE (field);
9304 tree ctype = strip_array_types (ftype);
9305 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9307 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9308 tree *typep = &fmain_type;
9309 do {
9310 *typep = build_distinct_type_copy (*typep);
9311 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9312 typep = &TREE_TYPE (*typep);
9313 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9314 TREE_TYPE (field)
9315 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9319 /* Warn on problematic type punning for storage order purposes. */
9320 if (TREE_CODE (t) == UNION_TYPE
9321 && TREE_CODE (field) == FIELD_DECL
9322 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9324 tree ftype = TREE_TYPE (field);
9325 if (TREE_CODE (ftype) == ARRAY_TYPE)
9326 ftype = strip_array_types (ftype);
9327 if (RECORD_OR_UNION_TYPE_P (ftype)
9328 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9329 != TYPE_REVERSE_STORAGE_ORDER (t))
9330 warning_at (DECL_SOURCE_LOCATION (field),
9331 OPT_Wscalar_storage_order,
9332 "type punning toggles scalar storage order");
9336 /* Now we have the truly final field list.
9337 Store it in this type and in the variants. */
9339 TYPE_FIELDS (t) = fieldlist;
9341 /* If there are lots of fields, sort so we can look through them fast.
9342 We arbitrarily consider 16 or more elts to be "a lot". */
9345 int len = 0;
9347 for (x = fieldlist; x; x = DECL_CHAIN (x))
9349 if (len > 15 || DECL_NAME (x) == NULL)
9350 break;
9351 len += 1;
9354 if (len > 15)
9356 tree *field_array;
9357 struct lang_type *space;
9358 struct sorted_fields_type *space2;
9360 len += list_length (x);
9362 /* Use the same allocation policy here that make_node uses, to
9363 ensure that this lives as long as the rest of the struct decl.
9364 All decls in an inline function need to be saved. */
9366 space = ggc_cleared_alloc<struct lang_type> ();
9367 space2 = (sorted_fields_type *) ggc_internal_alloc
9368 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
9370 len = 0;
9371 space->s = space2;
9372 field_array = &space2->elts[0];
9373 for (x = fieldlist; x; x = DECL_CHAIN (x))
9375 field_array[len++] = x;
9377 /* If there is anonymous struct or union, break out of the loop. */
9378 if (DECL_NAME (x) == NULL)
9379 break;
9381 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9382 if (x == NULL)
9384 TYPE_LANG_SPECIFIC (t) = space;
9385 TYPE_LANG_SPECIFIC (t)->s->len = len;
9386 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9387 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9392 /* If this was supposed to be a transparent union, but we can't
9393 make it one, warn and turn off the flag. */
9394 if (TREE_CODE (t) == UNION_TYPE
9395 && TYPE_TRANSPARENT_AGGR (t)
9396 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9398 TYPE_TRANSPARENT_AGGR (t) = 0;
9399 warning_at (loc, 0, "union cannot be made transparent");
9402 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9403 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9405 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9406 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9407 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9408 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9409 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9410 C_TYPE_FIELDS_NON_CONSTEXPR (x) = C_TYPE_FIELDS_NON_CONSTEXPR (t);
9411 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9412 C_TYPE_VARIABLY_MODIFIED (x) = C_TYPE_VARIABLY_MODIFIED (t);
9413 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9416 /* Update type location to the one of the definition, instead of e.g.
9417 a forward declaration. */
9418 if (TYPE_STUB_DECL (t))
9419 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9421 /* Finish debugging output for this type. */
9422 rest_of_type_compilation (t, toplevel);
9424 finish_incomplete_vars (incomplete_vars, toplevel);
9426 /* If we're inside a function proper, i.e. not file-scope and not still
9427 parsing parameters, then arrange for the size of a variable sized type
9428 to be bound now. */
9429 if (building_stmt_list_p () && c_type_variably_modified_p(t))
9430 add_stmt (build_stmt (loc,
9431 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
9433 if (warn_cxx_compat)
9434 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9436 delete struct_parse_info;
9438 struct_parse_info = enclosing_struct_parse_info;
9440 /* If this struct is defined inside a struct, add it to
9441 struct_types. */
9442 if (warn_cxx_compat
9443 && struct_parse_info != NULL
9444 && !in_sizeof && !in_typeof && !in_alignof)
9445 struct_parse_info->struct_types.safe_push (t);
9447 return t;
9450 static struct {
9451 gt_pointer_operator new_value;
9452 void *cookie;
9453 } resort_data;
9455 /* This routine compares two fields like field_decl_cmp but using the
9456 pointer operator in resort_data. */
9458 static int
9459 resort_field_decl_cmp (const void *x_p, const void *y_p)
9461 const tree *const x = (const tree *) x_p;
9462 const tree *const y = (const tree *) y_p;
9464 if (DECL_NAME (*x) == DECL_NAME (*y))
9465 /* A nontype is "greater" than a type. */
9466 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9467 if (DECL_NAME (*x) == NULL_TREE)
9468 return -1;
9469 if (DECL_NAME (*y) == NULL_TREE)
9470 return 1;
9472 tree d1 = DECL_NAME (*x);
9473 tree d2 = DECL_NAME (*y);
9474 resort_data.new_value (&d1, &d1, resort_data.cookie);
9475 resort_data.new_value (&d2, &d2, resort_data.cookie);
9476 if (d1 < d2)
9477 return -1;
9479 return 1;
9482 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9484 void
9485 resort_sorted_fields (void *obj,
9486 void * ARG_UNUSED (orig_obj),
9487 gt_pointer_operator new_value,
9488 void *cookie)
9490 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9491 resort_data.new_value = new_value;
9492 resort_data.cookie = cookie;
9493 qsort (&sf->elts[0], sf->len, sizeof (tree),
9494 resort_field_decl_cmp);
9497 /* Lay out the type T, and its element type, and so on. */
9499 static void
9500 layout_array_type (tree t)
9502 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9503 layout_array_type (TREE_TYPE (t));
9504 layout_type (t);
9507 /* Begin compiling the definition of an enumeration type.
9508 NAME is its name (or null if anonymous).
9509 LOC is the enum's location.
9510 FIXED_UNDERLYING_TYPE is the (C2x) underlying type specified in the
9511 definition.
9512 Returns the type object, as yet incomplete.
9513 Also records info about it so that build_enumerator
9514 may be used to declare the individual values as they are read. */
9516 tree
9517 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
9518 tree fixed_underlying_type)
9520 tree enumtype = NULL_TREE;
9521 location_t enumloc = UNKNOWN_LOCATION;
9523 /* If this is the real definition for a previous forward reference,
9524 fill in the contents in the same object that used to be the
9525 forward reference. */
9527 if (name != NULL_TREE)
9528 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9530 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9532 enumtype = make_node (ENUMERAL_TYPE);
9533 pushtag (loc, name, enumtype);
9534 if (fixed_underlying_type != NULL_TREE)
9536 /* For an enum definition with a fixed underlying type, the
9537 type is complete during the definition and the
9538 enumeration constants have that type. If there was a
9539 tag, the type was completed in c_parser_enum_specifier.
9540 If not, it must be completed here. */
9541 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = true;
9542 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (fixed_underlying_type);
9543 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (fixed_underlying_type);
9544 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (fixed_underlying_type);
9545 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (fixed_underlying_type));
9546 TYPE_SIZE (enumtype) = NULL_TREE;
9547 TYPE_PRECISION (enumtype) = TYPE_PRECISION (fixed_underlying_type);
9548 ENUM_UNDERLYING_TYPE (enumtype) = fixed_underlying_type;
9549 layout_type (enumtype);
9552 /* Update type location to the one of the definition, instead of e.g.
9553 a forward declaration. */
9554 else if (TYPE_STUB_DECL (enumtype))
9556 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9557 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9560 if (C_TYPE_BEING_DEFINED (enumtype))
9561 error_at (loc, "nested redefinition of %<enum %E%>", name);
9563 C_TYPE_BEING_DEFINED (enumtype) = 1;
9565 if (TYPE_VALUES (enumtype) != NULL_TREE)
9567 /* This enum is a named one that has been declared already. */
9568 auto_diagnostic_group d;
9569 error_at (loc, "redeclaration of %<enum %E%>", name);
9570 if (enumloc != UNKNOWN_LOCATION)
9571 inform (enumloc, "originally defined here");
9573 /* Completely replace its old definition.
9574 The old enumerators remain defined, however. */
9575 TYPE_VALUES (enumtype) = NULL_TREE;
9578 if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
9579 && fixed_underlying_type == NULL_TREE)
9580 error_at (loc, "%<enum%> declared with but defined without "
9581 "fixed underlying type");
9583 the_enum->enum_next_value = integer_zero_node;
9584 the_enum->enum_type = enumtype;
9585 the_enum->enum_overflow = 0;
9587 if (flag_short_enums && !ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9588 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9589 TYPE_PACKED (v) = 1;
9591 /* FIXME: This will issue a warning for a use of a type defined
9592 within sizeof in a statement expr. This is not terribly serious
9593 as C++ doesn't permit statement exprs within sizeof anyhow. */
9594 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9595 warning_at (loc, OPT_Wc___compat,
9596 "defining type in %qs expression is invalid in C++",
9597 (in_sizeof
9598 ? "sizeof"
9599 : (in_typeof ? "typeof" : "alignof")));
9601 if (in_underspecified_init)
9602 error_at (loc, "%qT defined in underspecified object initializer",
9603 enumtype);
9605 return enumtype;
9608 /* After processing and defining all the values of an enumeration type,
9609 install their decls in the enumeration type and finish it off.
9610 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9611 and ATTRIBUTES are the specified attributes.
9612 Returns ENUMTYPE. */
9614 tree
9615 finish_enum (tree enumtype, tree values, tree attributes)
9617 tree pair, tem;
9618 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9619 int precision;
9620 signop sign;
9621 bool toplevel = (file_scope == current_scope);
9622 struct lang_type *lt;
9624 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9626 /* Calculate the maximum value of any enumerator in this type. */
9628 if (values == error_mark_node)
9629 minnode = maxnode = integer_zero_node;
9630 else
9632 minnode = maxnode = TREE_VALUE (values);
9633 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9635 tree value = TREE_VALUE (pair);
9636 if (tree_int_cst_lt (maxnode, value))
9637 maxnode = value;
9638 if (tree_int_cst_lt (value, minnode))
9639 minnode = value;
9643 /* Construct the final type of this enumeration. It is the same
9644 as one of the integral types - the narrowest one that fits, except
9645 that normally we only go as narrow as int - and signed iff any of
9646 the values are negative. */
9647 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9648 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9649 tree_int_cst_min_precision (maxnode, sign));
9651 bool wider_than_int =
9652 (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
9653 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
9656 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype))
9658 /* If the precision of the type was specified with an attribute and it
9659 was too small, give an error. Otherwise, use it. */
9660 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9662 if (precision > TYPE_PRECISION (enumtype))
9664 TYPE_PRECISION (enumtype) = 0;
9665 error ("specified mode too small for enumerated values");
9667 else
9668 precision = TYPE_PRECISION (enumtype);
9670 else
9671 TYPE_PRECISION (enumtype) = 0;
9673 if (TYPE_PACKED (enumtype)
9674 || precision > TYPE_PRECISION (integer_type_node)
9675 || TYPE_PRECISION (enumtype))
9677 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9678 if (tem == NULL)
9680 /* This should only occur when both signed and unsigned
9681 values of maximum precision occur among the
9682 enumerators. */
9683 pedwarn (input_location, 0,
9684 "enumeration values exceed range of largest integer");
9685 tem = widest_integer_literal_type_node;
9687 else if (precision > TYPE_PRECISION (intmax_type_node)
9688 && !tree_int_cst_lt (minnode,
9689 TYPE_MIN_VALUE (intmax_type_node))
9690 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
9691 maxnode))
9692 pedwarn (input_location, OPT_Wpedantic,
9693 "enumeration values exceed range of %qs",
9694 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
9696 else
9697 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9699 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9700 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9701 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9702 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9703 TYPE_SIZE (enumtype) = NULL_TREE;
9704 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9705 ENUM_UNDERLYING_TYPE (enumtype) =
9706 c_common_type_for_size (TYPE_PRECISION (tem), TYPE_UNSIGNED (tem));
9708 layout_type (enumtype);
9711 if (values != error_mark_node)
9713 /* Change the type of the enumerators to be the enum type. We
9714 need to do this irrespective of the size of the enum, for
9715 proper type checking. Replace the DECL_INITIALs of the
9716 enumerators, and the value slots of the list, with copies
9717 that have the enum type; they cannot be modified in place
9718 because they may be shared (e.g. integer_zero_node) Finally,
9719 change the purpose slots to point to the names of the decls. */
9720 for (pair = values; pair; pair = TREE_CHAIN (pair))
9722 tree enu = TREE_PURPOSE (pair);
9723 tree ini = DECL_INITIAL (enu);
9725 TREE_TYPE (enu) = enumtype;
9727 /* Before C2X, the ISO C Standard mandates enumerators to
9728 have type int, even though the underlying type of an enum
9729 type is unspecified. However, C2X allows enumerators of
9730 any integer type, and if an enumeration has any
9731 enumerators wider than int, all enumerators have the
9732 enumerated type after it is parsed. Any enumerators that
9733 fit in int are given type int in build_enumerator (which
9734 is the correct type while the enumeration is being
9735 parsed), so no conversions are needed here if all
9736 enumerators fit in int. If the enum has a fixed
9737 underlying type, the correct type was also given in
9738 build_enumerator. */
9739 if (!ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) && wider_than_int)
9740 ini = convert (enumtype, ini);
9742 DECL_INITIAL (enu) = ini;
9743 TREE_PURPOSE (pair) = DECL_NAME (enu);
9744 /* To match the C++ FE, store the CONST_DECL rather than just its
9745 value. */
9746 TREE_VALUE (pair) = enu;
9749 TYPE_VALUES (enumtype) = values;
9752 /* Record the min/max values so that we can warn about bit-field
9753 enumerations that are too small for the values. */
9754 lt = ggc_cleared_alloc<struct lang_type> ();
9755 lt->enum_min = minnode;
9756 lt->enum_max = maxnode;
9757 TYPE_LANG_SPECIFIC (enumtype) = lt;
9759 /* Fix up all variant types of this enum type. */
9760 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9761 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9763 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9764 if (tem == enumtype)
9765 continue;
9766 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9767 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9768 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9769 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9770 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9771 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9772 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9773 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9774 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9775 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9776 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9777 ENUM_UNDERLYING_TYPE (tem) = ENUM_UNDERLYING_TYPE (enumtype);
9780 /* Finish debugging output for this type. */
9781 rest_of_type_compilation (enumtype, toplevel);
9783 finish_incomplete_vars (incomplete_vars, toplevel);
9785 /* If this enum is defined inside a struct, add it to
9786 struct_types. */
9787 if (warn_cxx_compat
9788 && struct_parse_info != NULL
9789 && !in_sizeof && !in_typeof && !in_alignof)
9790 struct_parse_info->struct_types.safe_push (enumtype);
9792 C_TYPE_BEING_DEFINED (enumtype) = 0;
9794 return enumtype;
9797 /* Build and install a CONST_DECL for one value of the
9798 current enumeration type (one that was begun with start_enum).
9799 DECL_LOC is the location of the enumerator.
9800 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9801 Return a tree-list containing the CONST_DECL and its value.
9802 Assignment of sequential values by default is handled here. */
9804 tree
9805 build_enumerator (location_t decl_loc, location_t loc,
9806 struct c_enum_contents *the_enum, tree name, tree value)
9808 tree decl;
9810 /* Validate and default VALUE. */
9812 if (value != NULL_TREE)
9814 /* Don't issue more errors for error_mark_node (i.e. an
9815 undeclared identifier) - just ignore the value expression. */
9816 if (value == error_mark_node)
9817 value = NULL_TREE;
9818 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9820 error_at (loc, "enumerator value for %qE is not an integer constant",
9821 name);
9822 value = NULL_TREE;
9824 else
9826 if (TREE_CODE (value) != INTEGER_CST)
9828 value = c_fully_fold (value, false, NULL);
9829 if (TREE_CODE (value) == INTEGER_CST)
9830 pedwarn (loc, OPT_Wpedantic,
9831 "enumerator value for %qE is not an integer "
9832 "constant expression", name);
9834 if (TREE_CODE (value) != INTEGER_CST)
9836 error ("enumerator value for %qE is not an integer constant",
9837 name);
9838 value = NULL_TREE;
9840 else
9842 value = default_conversion (value);
9843 constant_expression_warning (value);
9848 /* Default based on previous value. */
9849 /* It should no longer be possible to have NON_LVALUE_EXPR
9850 in the default. */
9851 if (value == NULL_TREE)
9853 value = the_enum->enum_next_value;
9854 if (the_enum->enum_overflow)
9855 error_at (loc, "overflow in enumeration values");
9857 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
9859 /* Enumeration constants must fit in the fixed underlying type. */
9860 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (the_enum->enum_type)))
9861 error_at (loc,
9862 "enumerator value outside the range of underlying type");
9863 /* Enumeration constants for an enum with fixed underlying type
9864 have the enum type, both inside and outside the
9865 definition. */
9866 value = convert (the_enum->enum_type, value);
9868 else
9870 /* Even though the underlying type of an enum is unspecified, the
9871 type of enumeration constants is explicitly defined as int
9872 (6.4.4.3/2 in the C99 Standard). C2X allows any integer type, and
9873 GCC allows such types for older standards as an extension. */
9874 bool warned_range = false;
9875 if (!int_fits_type_p (value,
9876 (TYPE_UNSIGNED (TREE_TYPE (value))
9877 ? uintmax_type_node
9878 : intmax_type_node)))
9879 /* GCC does not consider its types larger than intmax_t to be
9880 extended integer types (although C2X would permit such types to
9881 be considered extended integer types if all the features
9882 required by <stdint.h> and <inttypes.h> macros, such as support
9883 for integer constants and I/O, were present), so diagnose if
9884 such a wider type is used. (If the wider type arose from a
9885 constant of such a type, that will also have been diagnosed,
9886 but this is the only diagnostic in the case where it arises
9887 from choosing a wider type automatically when adding 1
9888 overflows.) */
9889 warned_range = pedwarn (loc, OPT_Wpedantic,
9890 "enumerator value outside the range of %qs",
9891 (TYPE_UNSIGNED (TREE_TYPE (value))
9892 ? "uintmax_t"
9893 : "intmax_t"));
9894 if (!warned_range && !int_fits_type_p (value, integer_type_node))
9895 pedwarn_c11 (loc, OPT_Wpedantic,
9896 "ISO C restricts enumerator values to range of %<int%> "
9897 "before C2X");
9899 /* The ISO C Standard mandates enumerators to have type int before
9900 C2X, even though the underlying type of an enum type is
9901 unspecified. C2X allows enumerators of any integer type. During
9902 the parsing of the enumeration, C2X specifies that constants
9903 representable in int have type int, constants not representable
9904 in int have the type of the given expression if any, and
9905 constants not representable in int and derived by adding 1 to the
9906 previous constant have the type of that constant unless the
9907 addition would overflow or wraparound, in which case a wider type
9908 of the same signedness is chosen automatically; after the
9909 enumeration is parsed, all the constants have the type of the
9910 enumeration if any do not fit in int. */
9911 if (int_fits_type_p (value, integer_type_node))
9912 value = convert (integer_type_node, value);
9915 /* Set basis for default for next value. */
9916 if (ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
9918 tree underlying_type = ENUM_UNDERLYING_TYPE (the_enum->enum_type);
9919 if (TREE_CODE (underlying_type) == BOOLEAN_TYPE)
9920 /* A value of 2 following a value of 1 overflows bool, but we
9921 cannot carry out addition directly on bool without
9922 promotion, and converting the result of arithmetic in a
9923 wider type back to bool would not produce the right result
9924 for this overflow check. */
9925 the_enum->enum_next_value = invert_truthvalue_loc (loc, value);
9926 else
9927 the_enum->enum_next_value
9928 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9929 PLUS_EXPR, convert (underlying_type, value),
9930 convert (underlying_type, integer_one_node),
9931 false);
9933 else
9934 the_enum->enum_next_value
9935 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9936 PLUS_EXPR, value, integer_one_node, false);
9937 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9938 if (the_enum->enum_overflow
9939 && !ENUM_FIXED_UNDERLYING_TYPE_P (the_enum->enum_type))
9941 /* Choose a wider type with the same signedness if
9942 available. */
9943 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
9944 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
9945 tree new_type = (unsignedp
9946 ? long_unsigned_type_node
9947 : long_integer_type_node);
9948 if (prec > TYPE_PRECISION (new_type))
9949 new_type = (unsignedp
9950 ? long_long_unsigned_type_node
9951 : long_long_integer_type_node);
9952 if (prec > TYPE_PRECISION (new_type))
9953 new_type = (unsignedp
9954 ? widest_unsigned_literal_type_node
9955 : widest_integer_literal_type_node);
9956 if (prec <= TYPE_PRECISION (new_type))
9958 the_enum->enum_overflow = false;
9959 the_enum->enum_next_value
9960 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9961 PLUS_EXPR, convert (new_type, value),
9962 integer_one_node, false);
9963 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
9967 /* Now create a declaration for the enum value name. */
9969 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
9970 DECL_INITIAL (decl) = value;
9971 pushdecl (decl);
9973 return tree_cons (decl, value, NULL_TREE);
9976 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9978 tree
9979 c_simulate_enum_decl (location_t loc, const char *name,
9980 vec<string_int_pair> *values_ptr)
9982 location_t saved_loc = input_location;
9983 input_location = loc;
9985 struct c_enum_contents the_enum;
9986 tree enumtype = start_enum (loc, &the_enum, get_identifier (name),
9987 NULL_TREE);
9989 tree value_chain = NULL_TREE;
9990 string_int_pair *value;
9991 vec<string_int_pair> values = *values_ptr;
9992 unsigned int i;
9993 FOR_EACH_VEC_ELT (values, i, value)
9995 tree decl = build_enumerator (loc, loc, &the_enum,
9996 get_identifier (value->first),
9997 build_int_cst (integer_type_node,
9998 value->second));
9999 TREE_CHAIN (decl) = value_chain;
10000 value_chain = decl;
10003 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
10005 input_location = saved_loc;
10006 return enumtype;
10009 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
10011 tree
10012 c_simulate_record_decl (location_t loc, const char *name,
10013 array_slice<const tree> fields)
10015 location_t saved_loc = input_location;
10016 input_location = loc;
10018 class c_struct_parse_info *struct_info;
10019 tree ident = get_identifier (name);
10020 tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
10022 for (unsigned int i = 0; i < fields.size (); ++i)
10024 DECL_FIELD_CONTEXT (fields[i]) = type;
10025 if (i > 0)
10026 DECL_CHAIN (fields[i - 1]) = fields[i];
10029 finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
10031 tree decl = build_decl (loc, TYPE_DECL, ident, type);
10032 set_underlying_type (decl);
10033 lang_hooks.decls.pushdecl (decl);
10035 input_location = saved_loc;
10036 return type;
10039 /* Create the FUNCTION_DECL for a function definition.
10040 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
10041 the declaration; they describe the function's name and the type it returns,
10042 but twisted together in a fashion that parallels the syntax of C.
10044 This function creates a binding context for the function body
10045 as well as setting up the FUNCTION_DECL in current_function_decl.
10047 Returns true on success. If the DECLARATOR is not suitable for a function
10048 (it defines a datum instead), we return false to report a parse error. */
10050 bool
10051 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
10052 tree attributes)
10054 tree decl1, old_decl;
10055 tree restype, resdecl;
10056 location_t loc;
10057 location_t result_loc;
10059 current_function_returns_value = 0; /* Assume, until we see it does. */
10060 current_function_returns_null = 0;
10061 current_function_returns_abnormally = 0;
10062 warn_about_return_type = 0;
10063 c_switch_stack = NULL;
10065 /* Indicate no valid break/continue context. */
10066 in_statement = 0;
10068 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
10069 &attributes, NULL, NULL, DEPRECATED_NORMAL);
10070 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
10072 /* If the declarator is not suitable for a function definition,
10073 cause a syntax error. */
10074 if (decl1 == NULL_TREE
10075 || TREE_CODE (decl1) != FUNCTION_DECL)
10076 return false;
10078 loc = DECL_SOURCE_LOCATION (decl1);
10080 /* A nested function is not global. */
10081 if (current_function_decl != NULL_TREE)
10082 TREE_PUBLIC (decl1) = 0;
10084 c_decl_attributes (&decl1, attributes, 0);
10086 if (DECL_DECLARED_INLINE_P (decl1)
10087 && DECL_UNINLINABLE (decl1)
10088 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
10089 warning_at (loc, OPT_Wattributes,
10090 "inline function %qD given attribute %qs",
10091 decl1, "noinline");
10093 /* Handle gnu_inline attribute. */
10094 if (declspecs->inline_p
10095 && !flag_gnu89_inline
10096 && TREE_CODE (decl1) == FUNCTION_DECL
10097 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
10098 || current_function_decl))
10100 if (declspecs->storage_class != csc_static)
10101 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
10104 announce_function (decl1);
10106 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
10108 error_at (loc, "return type is an incomplete type");
10109 /* Make it return void instead. */
10110 TREE_TYPE (decl1)
10111 = build_function_type (void_type_node,
10112 TYPE_ARG_TYPES (TREE_TYPE (decl1)),
10113 TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (decl1)));
10116 if (warn_about_return_type)
10117 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
10118 : (warn_return_type > 0 ? OPT_Wreturn_type
10119 : OPT_Wimplicit_int),
10120 "return type defaults to %<int%>");
10122 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10123 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
10124 DECL_INITIAL (decl1) = error_mark_node;
10126 /* If this definition isn't a prototype and we had a prototype declaration
10127 before, copy the arg type info from that prototype. */
10128 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
10129 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
10130 old_decl = NULL_TREE;
10132 current_function_prototype_locus = UNKNOWN_LOCATION;
10133 current_function_prototype_built_in = false;
10134 current_function_prototype_arg_types = NULL_TREE;
10135 tree newtype = TREE_TYPE (decl1);
10136 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
10137 if (!prototype_p (newtype))
10139 tree oldrt = TREE_TYPE (oldtype);
10140 tree newrt = TREE_TYPE (newtype);
10141 if (old_decl != NULL_TREE
10142 && TREE_CODE (oldtype) == FUNCTION_TYPE
10143 && comptypes (oldrt, newrt))
10145 if (stdarg_p (oldtype))
10147 auto_diagnostic_group d;
10148 warning_at (loc, 0, "%q+D defined as variadic function "
10149 "without prototype", decl1);
10150 locate_old_decl (old_decl);
10152 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
10153 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
10154 current_function_prototype_built_in
10155 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
10156 current_function_prototype_arg_types
10157 = TYPE_ARG_TYPES (newtype);
10159 if (TREE_PUBLIC (decl1))
10161 /* If there is an external prototype declaration of this
10162 function, record its location but do not copy information
10163 to this decl. This may be an invisible declaration
10164 (built-in or in a scope which has finished) or simply
10165 have more refined argument types than any declaration
10166 found above. */
10167 struct c_binding *b;
10168 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
10169 if (B_IN_SCOPE (b, external_scope))
10170 break;
10171 if (b)
10173 tree ext_decl, ext_type;
10174 ext_decl = b->decl;
10175 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
10176 if (TREE_CODE (ext_type) == FUNCTION_TYPE
10177 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
10178 TREE_TYPE (ext_type)))
10180 current_function_prototype_locus
10181 = DECL_SOURCE_LOCATION (ext_decl);
10182 current_function_prototype_built_in
10183 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
10184 current_function_prototype_arg_types
10185 = TYPE_ARG_TYPES (ext_type);
10191 /* Optionally warn of old-fashioned def with no previous prototype. */
10192 if (warn_strict_prototypes
10193 && old_decl != error_mark_node
10194 && !prototype_p (TREE_TYPE (decl1))
10195 && C_DECL_ISNT_PROTOTYPE (old_decl))
10196 warning_at (loc, OPT_Wstrict_prototypes,
10197 "function declaration isn%'t a prototype");
10198 /* Optionally warn of any global def with no previous prototype. */
10199 else if (warn_missing_prototypes
10200 && old_decl != error_mark_node
10201 && TREE_PUBLIC (decl1)
10202 && !MAIN_NAME_P (DECL_NAME (decl1))
10203 && C_DECL_ISNT_PROTOTYPE (old_decl)
10204 && !DECL_DECLARED_INLINE_P (decl1))
10205 warning_at (loc, OPT_Wmissing_prototypes,
10206 "no previous prototype for %qD", decl1);
10207 /* Optionally warn of any def with no previous prototype
10208 if the function has already been used. */
10209 else if (warn_missing_prototypes
10210 && old_decl != NULL_TREE
10211 && old_decl != error_mark_node
10212 && TREE_USED (old_decl)
10213 && !prototype_p (TREE_TYPE (old_decl)))
10214 warning_at (loc, OPT_Wmissing_prototypes,
10215 "%qD was used with no prototype before its definition", decl1);
10216 /* Optionally warn of any global def with no previous declaration. */
10217 else if (warn_missing_declarations
10218 && TREE_PUBLIC (decl1)
10219 && old_decl == NULL_TREE
10220 && !MAIN_NAME_P (DECL_NAME (decl1))
10221 && !DECL_DECLARED_INLINE_P (decl1))
10222 warning_at (loc, OPT_Wmissing_declarations,
10223 "no previous declaration for %qD",
10224 decl1);
10225 /* Optionally warn of any def with no previous declaration
10226 if the function has already been used. */
10227 else if (warn_missing_declarations
10228 && old_decl != NULL_TREE
10229 && old_decl != error_mark_node
10230 && TREE_USED (old_decl)
10231 && C_DECL_IMPLICIT (old_decl))
10232 warning_at (loc, OPT_Wmissing_declarations,
10233 "%qD was used with no declaration before its definition", decl1);
10235 /* This function exists in static storage.
10236 (This does not mean `static' in the C sense!) */
10237 TREE_STATIC (decl1) = 1;
10239 /* This is the earliest point at which we might know the assembler
10240 name of the function. Thus, if it's set before this, die horribly. */
10241 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
10243 /* If #pragma weak was used, mark the decl weak now. */
10244 if (current_scope == file_scope)
10245 maybe_apply_pragma_weak (decl1);
10247 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
10248 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
10250 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
10251 != integer_type_node)
10252 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
10253 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
10254 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
10255 decl1);
10257 check_main_parameter_types (decl1);
10259 if (!TREE_PUBLIC (decl1))
10260 pedwarn (loc, OPT_Wmain,
10261 "%qD is normally a non-static function", decl1);
10264 tree parms = current_function_arg_info->parms;
10265 if (old_decl)
10267 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
10268 warn_parm_array_mismatch (origloc, old_decl, parms);
10271 /* Record the decl so that the function name is defined.
10272 If we already have a decl for this name, and it is a FUNCTION_DECL,
10273 use the old decl. */
10275 current_function_decl = pushdecl (decl1);
10277 if (tree access = build_attr_access_from_parms (parms, false))
10278 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
10279 old_decl);
10281 push_scope ();
10282 declare_parm_level ();
10284 /* Set the result decl source location to the location of the typespec. */
10285 result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION
10286 ? loc : declspecs->locations[cdw_typespec]);
10287 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
10288 resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype);
10289 DECL_ARTIFICIAL (resdecl) = 1;
10290 DECL_IGNORED_P (resdecl) = 1;
10291 DECL_RESULT (current_function_decl) = resdecl;
10293 start_fname_decls ();
10295 return true;
10298 /* Subroutine of store_parm_decls which handles new-style function
10299 definitions (prototype format). The parms already have decls, so we
10300 need only record them as in effect and complain if any redundant
10301 old-style parm decls were written. */
10302 static void
10303 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
10305 tree decl;
10306 c_arg_tag *tag;
10307 unsigned ix;
10309 if (current_scope->bindings)
10311 error_at (DECL_SOURCE_LOCATION (fndecl),
10312 "old-style parameter declarations in prototyped "
10313 "function definition");
10315 /* Get rid of the old-style declarations. */
10316 pop_scope ();
10317 push_scope ();
10319 /* Don't issue this warning for nested functions, and don't issue this
10320 warning if we got here because ARG_INFO_TYPES was error_mark_node
10321 (this happens when a function definition has just an ellipsis in
10322 its parameter list). */
10323 else if (!in_system_header_at (input_location)
10324 && !current_function_scope
10325 && arg_info->types != error_mark_node)
10326 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10327 "traditional C rejects ISO C style function definitions");
10329 /* Now make all the parameter declarations visible in the function body.
10330 We can bypass most of the grunt work of pushdecl. */
10331 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10333 DECL_CONTEXT (decl) = current_function_decl;
10334 if (DECL_NAME (decl))
10336 bind (DECL_NAME (decl), decl, current_scope,
10337 /*invisible=*/false, /*nested=*/false,
10338 UNKNOWN_LOCATION);
10339 if (!TREE_USED (decl))
10340 warn_if_shadowing (decl);
10342 else
10343 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10344 "ISO C does not support omitting parameter names in "
10345 "function definitions before C2X");
10348 /* Record the parameter list in the function declaration. */
10349 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10351 /* Now make all the ancillary declarations visible, likewise. */
10352 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10354 DECL_CONTEXT (decl) = current_function_decl;
10355 if (DECL_NAME (decl))
10356 bind (DECL_NAME (decl), decl, current_scope,
10357 /*invisible=*/false,
10358 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10359 UNKNOWN_LOCATION);
10362 /* And all the tag declarations. */
10363 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10364 if (tag->id)
10365 bind (tag->id, tag->type, current_scope,
10366 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10369 /* Subroutine of store_parm_decls which handles old-style function
10370 definitions (separate parameter list and declarations). */
10372 static void
10373 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10375 struct c_binding *b;
10376 tree parm, decl, last;
10377 tree parmids = arg_info->parms;
10378 hash_set<tree> seen_args;
10380 if (!in_system_header_at (input_location))
10382 if (flag_isoc2x)
10383 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10384 OPT_Wold_style_definition, "old-style function definition");
10385 else
10386 warning_at (DECL_SOURCE_LOCATION (fndecl),
10387 OPT_Wold_style_definition,
10388 "old-style function definition");
10391 if (current_scope->had_vla_unspec)
10392 error ("%<[*]%> not allowed in other than function prototype scope");
10394 /* Match each formal parameter name with its declaration. Save each
10395 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10396 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10398 if (TREE_VALUE (parm) == NULL_TREE)
10400 error_at (DECL_SOURCE_LOCATION (fndecl),
10401 "parameter name missing from parameter list");
10402 TREE_PURPOSE (parm) = NULL_TREE;
10403 continue;
10406 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10407 if (b && B_IN_CURRENT_SCOPE (b))
10409 decl = b->decl;
10410 /* Skip erroneous parameters. */
10411 if (decl == error_mark_node)
10412 continue;
10413 /* If we got something other than a PARM_DECL it is an error. */
10414 if (TREE_CODE (decl) != PARM_DECL)
10416 error_at (DECL_SOURCE_LOCATION (decl),
10417 "%qD declared as a non-parameter", decl);
10418 continue;
10420 /* If the declaration is already marked, we have a duplicate
10421 name. Complain and ignore the duplicate. */
10422 else if (seen_args.contains (decl))
10424 error_at (DECL_SOURCE_LOCATION (decl),
10425 "multiple parameters named %qD", decl);
10426 TREE_PURPOSE (parm) = NULL_TREE;
10427 continue;
10429 /* If the declaration says "void", complain and turn it into
10430 an int. */
10431 else if (VOID_TYPE_P (TREE_TYPE (decl)))
10433 error_at (DECL_SOURCE_LOCATION (decl),
10434 "parameter %qD declared with void type", decl);
10435 TREE_TYPE (decl) = integer_type_node;
10436 DECL_ARG_TYPE (decl) = integer_type_node;
10437 layout_decl (decl, 0);
10439 warn_if_shadowing (decl);
10441 /* If no declaration found, default to int. */
10442 else
10444 /* FIXME diagnostics: This should be the location of the argument,
10445 not the FNDECL. E.g., for an old-style declaration
10447 int f10(v) { blah; }
10449 We should use the location of the V, not the F10.
10450 Unfortunately, the V is an IDENTIFIER_NODE which has no
10451 location. In the future we need locations for c_arg_info
10452 entries.
10454 See gcc.dg/Wshadow-3.c for an example of this problem. */
10455 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10456 PARM_DECL, TREE_VALUE (parm), integer_type_node);
10457 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10458 pushdecl (decl);
10459 warn_if_shadowing (decl);
10461 if (flag_isoc99)
10462 pedwarn (DECL_SOURCE_LOCATION (decl),
10463 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10464 decl);
10465 else
10466 warning_at (DECL_SOURCE_LOCATION (decl),
10467 OPT_Wmissing_parameter_type,
10468 "type of %qD defaults to %<int%>", decl);
10471 TREE_PURPOSE (parm) = decl;
10472 seen_args.add (decl);
10475 /* Now examine the parms chain for incomplete declarations
10476 and declarations with no corresponding names. */
10478 for (b = current_scope->bindings; b; b = b->prev)
10480 parm = b->decl;
10481 if (TREE_CODE (parm) != PARM_DECL)
10482 continue;
10484 if (TREE_TYPE (parm) != error_mark_node
10485 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10487 error_at (DECL_SOURCE_LOCATION (parm),
10488 "parameter %qD has incomplete type", parm);
10489 TREE_TYPE (parm) = error_mark_node;
10492 if (!seen_args.contains (parm))
10494 error_at (DECL_SOURCE_LOCATION (parm),
10495 "declaration for parameter %qD but no such parameter",
10496 parm);
10498 /* Pretend the parameter was not missing.
10499 This gets us to a standard state and minimizes
10500 further error messages. */
10501 parmids = chainon (parmids, tree_cons (parm, 0, 0));
10505 /* Chain the declarations together in the order of the list of
10506 names. Store that chain in the function decl, replacing the
10507 list of names. Update the current scope to match. */
10508 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10510 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10511 if (TREE_PURPOSE (parm))
10512 break;
10513 if (parm && TREE_PURPOSE (parm))
10515 last = TREE_PURPOSE (parm);
10516 DECL_ARGUMENTS (fndecl) = last;
10518 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10519 if (TREE_PURPOSE (parm))
10521 DECL_CHAIN (last) = TREE_PURPOSE (parm);
10522 last = TREE_PURPOSE (parm);
10524 DECL_CHAIN (last) = NULL_TREE;
10527 /* If there was a previous prototype,
10528 set the DECL_ARG_TYPE of each argument according to
10529 the type previously specified, and report any mismatches. */
10531 if (current_function_prototype_arg_types)
10533 tree type;
10534 for (parm = DECL_ARGUMENTS (fndecl),
10535 type = current_function_prototype_arg_types;
10536 parm || (type != NULL_TREE
10537 && TREE_VALUE (type) != error_mark_node
10538 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10539 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10541 if (parm == NULL_TREE
10542 || type == NULL_TREE
10543 || (TREE_VALUE (type) != error_mark_node
10544 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10546 if (current_function_prototype_built_in)
10547 warning_at (DECL_SOURCE_LOCATION (fndecl),
10548 0, "number of arguments doesn%'t match "
10549 "built-in prototype");
10550 else
10552 /* FIXME diagnostics: This should be the location of
10553 FNDECL, but there is bug when a prototype is
10554 declared inside function context, but defined
10555 outside of it (e.g., gcc.dg/pr15698-2.c). In
10556 which case FNDECL gets the location of the
10557 prototype, not the definition. */
10558 error_at (input_location,
10559 "number of arguments doesn%'t match prototype");
10561 error_at (current_function_prototype_locus,
10562 "prototype declaration");
10564 break;
10566 /* Type for passing arg must be consistent with that
10567 declared for the arg. ISO C says we take the unqualified
10568 type for parameters declared with qualified type. */
10569 if (TREE_TYPE (parm) != error_mark_node
10570 && TREE_VALUE (type) != error_mark_node
10571 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10572 != TYPE_ATOMIC (TREE_VALUE (type)))
10573 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10574 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10576 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10577 == TYPE_ATOMIC (TREE_VALUE (type)))
10578 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10579 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10581 /* Adjust argument to match prototype. E.g. a previous
10582 `int foo(float);' prototype causes
10583 `int foo(x) float x; {...}' to be treated like
10584 `int foo(float x) {...}'. This is particularly
10585 useful for argument types like uid_t. */
10586 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10588 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10589 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10590 && (TYPE_PRECISION (TREE_TYPE (parm))
10591 < TYPE_PRECISION (integer_type_node)))
10592 DECL_ARG_TYPE (parm)
10593 = c_type_promotes_to (TREE_TYPE (parm));
10595 /* ??? Is it possible to get here with a
10596 built-in prototype or will it always have
10597 been diagnosed as conflicting with an
10598 old-style definition and discarded? */
10599 if (current_function_prototype_built_in)
10600 warning_at (DECL_SOURCE_LOCATION (parm),
10601 OPT_Wpedantic, "promoted argument %qD "
10602 "doesn%'t match built-in prototype", parm);
10603 else
10605 pedwarn (DECL_SOURCE_LOCATION (parm),
10606 OPT_Wpedantic, "promoted argument %qD "
10607 "doesn%'t match prototype", parm);
10608 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10609 "prototype declaration");
10612 else
10614 if (current_function_prototype_built_in)
10615 warning_at (DECL_SOURCE_LOCATION (parm),
10616 0, "argument %qD doesn%'t match "
10617 "built-in prototype", parm);
10618 else
10620 error_at (DECL_SOURCE_LOCATION (parm),
10621 "argument %qD doesn%'t match prototype", parm);
10622 error_at (current_function_prototype_locus,
10623 "prototype declaration");
10628 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10631 /* Otherwise, create a prototype that would match. */
10633 else
10635 tree actual = NULL_TREE, last = NULL_TREE, type;
10637 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10639 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10640 if (last)
10641 TREE_CHAIN (last) = type;
10642 else
10643 actual = type;
10644 last = type;
10646 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10647 if (last)
10648 TREE_CHAIN (last) = type;
10649 else
10650 actual = type;
10652 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10653 of the type of this function, but we need to avoid having this
10654 affect the types of other similarly-typed functions, so we must
10655 first force the generation of an identical (but separate) type
10656 node for the relevant function type. The new node we create
10657 will be a variant of the main variant of the original function
10658 type. */
10660 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10662 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10666 /* Store parameter declarations passed in ARG_INFO into the current
10667 function declaration. */
10669 void
10670 store_parm_decls_from (struct c_arg_info *arg_info)
10672 current_function_arg_info = arg_info;
10673 store_parm_decls ();
10676 /* Called by walk_tree to look for and update context-less labels
10677 or labels with context in the parent function. */
10679 static tree
10680 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10682 tree ctx = static_cast<tree>(data);
10683 if (TREE_CODE (*tp) == LABEL_EXPR
10684 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10685 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10687 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10688 *walk_subtrees = 0;
10691 return NULL_TREE;
10694 /* Store the parameter declarations into the current function declaration.
10695 This is called after parsing the parameter declarations, before
10696 digesting the body of the function.
10698 For an old-style definition, construct a prototype out of the old-style
10699 parameter declarations and inject it into the function's type. */
10701 void
10702 store_parm_decls (void)
10704 tree fndecl = current_function_decl;
10705 bool proto;
10707 /* The argument information block for FNDECL. */
10708 struct c_arg_info *arg_info = current_function_arg_info;
10709 current_function_arg_info = 0;
10711 /* True if this definition is written with a prototype. In C2X, an
10712 empty argument list was converted to (void) in grokparms; in
10713 older C standard versions, it does not give the function a type
10714 with a prototype for future calls. */
10715 proto = arg_info->types != 0 || arg_info->no_named_args_stdarg_p;
10717 if (proto)
10718 store_parm_decls_newstyle (fndecl, arg_info);
10719 else
10720 store_parm_decls_oldstyle (fndecl, arg_info);
10722 /* The next call to push_scope will be a function body. */
10724 next_is_function_body = true;
10726 /* Write a record describing this function definition to the prototypes
10727 file (if requested). */
10729 gen_aux_info_record (fndecl, 1, 0, proto);
10731 /* Initialize the RTL code for the function. */
10732 allocate_struct_function (fndecl, false);
10734 if (warn_unused_local_typedefs)
10735 cfun->language = ggc_cleared_alloc<language_function> ();
10737 /* Begin the statement tree for this function. */
10738 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10740 /* ??? Insert the contents of the pending sizes list into the function
10741 to be evaluated. The only reason left to have this is
10742 void foo(int n, int array[n++])
10743 because we throw away the array type in favor of a pointer type, and
10744 thus won't naturally see the SAVE_EXPR containing the increment. All
10745 other pending sizes would be handled by gimplify_parameters. */
10746 if (arg_info->pending_sizes)
10748 /* In very special circumstances, e.g. for code like
10749 _Atomic int i = 5;
10750 void f (int a[i += 2]) {}
10751 we need to execute the atomic assignment on function entry.
10752 But in this case, it is not just a straight store, it has the
10753 op= form, which means that build_atomic_assign has generated
10754 gotos, labels, etc. Because at that time the function decl
10755 for F has not been created yet, those labels do not have any
10756 function context. But we have the fndecl now, so update the
10757 labels accordingly. gimplify_expr would crash otherwise.
10758 Or with nested functions the labels could be created with parent
10759 function's context, while when the statement is emitted at the
10760 start of the nested function, it needs the nested function's
10761 context. */
10762 walk_tree_without_duplicates (&arg_info->pending_sizes,
10763 set_labels_context_r, fndecl);
10764 add_stmt (arg_info->pending_sizes);
10768 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
10769 c_finish_omp_declare_simd for function prototypes. No diagnostics
10770 should be done. */
10772 void
10773 temp_store_parm_decls (tree fndecl, tree parms)
10775 push_scope ();
10776 for (tree p = parms; p; p = DECL_CHAIN (p))
10778 DECL_CONTEXT (p) = fndecl;
10779 if (DECL_NAME (p))
10780 bind (DECL_NAME (p), p, current_scope,
10781 /*invisible=*/false, /*nested=*/false,
10782 UNKNOWN_LOCATION);
10786 /* Undo what temp_store_parm_decls did. */
10788 void
10789 temp_pop_parm_decls (void)
10791 /* Clear all bindings in this temporary scope, so that
10792 pop_scope doesn't create a BLOCK. */
10793 struct c_binding *b = current_scope->bindings;
10794 current_scope->bindings = NULL;
10795 for (; b; b = free_binding_and_advance (b))
10797 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10798 || b->decl == error_mark_node);
10799 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10800 I_SYMBOL_BINDING (b->id) = b->shadowed;
10801 if (b->shadowed && b->shadowed->u.type)
10802 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10804 pop_scope ();
10808 /* Finish up a function declaration and compile that function
10809 all the way to assembler language output. Then free the storage
10810 for the function definition.
10812 This is called after parsing the body of the function definition. */
10814 void
10815 finish_function (location_t end_loc)
10817 tree fndecl = current_function_decl;
10819 if (c_dialect_objc ())
10820 objc_finish_function ();
10822 if (TREE_CODE (fndecl) == FUNCTION_DECL
10823 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10825 tree args = DECL_ARGUMENTS (fndecl);
10826 for (; args; args = DECL_CHAIN (args))
10828 tree type = TREE_TYPE (args);
10829 if (INTEGRAL_TYPE_P (type)
10830 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10831 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10835 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10836 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10838 /* Must mark the RESULT_DECL as being in this function. */
10840 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10841 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10843 if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl)
10844 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10845 == integer_type_node && flag_isoc99)
10847 /* Hack. We don't want the middle-end to warn that this return
10848 is unreachable, so we mark its location as special. Using
10849 UNKNOWN_LOCATION has the problem that it gets clobbered in
10850 annotate_one_with_locus. A cleaner solution might be to
10851 ensure ! should_carry_locus_p (stmt), but that needs a flag.
10853 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10856 /* Tie off the statement tree for this function. */
10857 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10859 finish_fname_decls ();
10861 /* Complain if there's no return statement only if option specified on
10862 command line. */
10863 if (warn_return_type > 0
10864 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10865 && !current_function_returns_value && !current_function_returns_null
10866 /* Don't complain if we are no-return. */
10867 && !current_function_returns_abnormally
10868 /* Don't complain if we are declared noreturn. */
10869 && !TREE_THIS_VOLATILE (fndecl)
10870 /* Don't warn for main(). */
10871 && !MAIN_NAME_P (DECL_NAME (fndecl))
10872 /* Or if they didn't actually specify a return type. */
10873 && !C_FUNCTION_IMPLICIT_INT (fndecl)
10874 /* Normally, with -Wreturn-type, flow will complain, but we might
10875 optimize out static functions. */
10876 && !TREE_PUBLIC (fndecl)
10877 && targetm.warn_func_return (fndecl)
10878 && warning (OPT_Wreturn_type,
10879 "no return statement in function returning non-void"))
10880 suppress_warning (fndecl, OPT_Wreturn_type);
10882 /* Complain about parameters that are only set, but never otherwise used. */
10883 if (warn_unused_but_set_parameter)
10885 tree decl;
10887 for (decl = DECL_ARGUMENTS (fndecl);
10888 decl;
10889 decl = DECL_CHAIN (decl))
10890 if (TREE_USED (decl)
10891 && TREE_CODE (decl) == PARM_DECL
10892 && !DECL_READ_P (decl)
10893 && DECL_NAME (decl)
10894 && !DECL_ARTIFICIAL (decl)
10895 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
10896 warning_at (DECL_SOURCE_LOCATION (decl),
10897 OPT_Wunused_but_set_parameter,
10898 "parameter %qD set but not used", decl);
10901 /* Complain about locally defined typedefs that are not used in this
10902 function. */
10903 maybe_warn_unused_local_typedefs ();
10905 /* Possibly warn about unused parameters. */
10906 if (warn_unused_parameter)
10907 do_warn_unused_parameter (fndecl);
10909 /* Store the end of the function, so that we get good line number
10910 info for the epilogue. */
10911 cfun->function_end_locus = end_loc;
10913 /* Finalize the ELF visibility for the function. */
10914 c_determine_visibility (fndecl);
10916 /* For GNU C extern inline functions disregard inline limits. */
10917 if (DECL_EXTERNAL (fndecl)
10918 && DECL_DECLARED_INLINE_P (fndecl)
10919 && (flag_gnu89_inline
10920 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10921 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10923 /* Genericize before inlining. Delay genericizing nested functions
10924 until their parent function is genericized. Since finalizing
10925 requires GENERIC, delay that as well. */
10927 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10928 && !undef_nested_function)
10930 if (!decl_function_context (fndecl))
10932 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10933 c_genericize (fndecl);
10935 /* ??? Objc emits functions after finalizing the compilation unit.
10936 This should be cleaned up later and this conditional removed. */
10937 if (symtab->global_info_ready)
10939 cgraph_node::add_new_function (fndecl, false);
10940 return;
10942 cgraph_node::finalize_function (fndecl, false);
10944 else
10946 /* Register this function with cgraph just far enough to get it
10947 added to our parent's nested function list. Handy, since the
10948 C front end doesn't have such a list. */
10949 (void) cgraph_node::get_create (fndecl);
10953 if (!decl_function_context (fndecl))
10954 undef_nested_function = false;
10956 if (cfun->language != NULL)
10958 ggc_free (cfun->language);
10959 cfun->language = NULL;
10962 /* We're leaving the context of this function, so zap cfun.
10963 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10964 tree_rest_of_compilation. */
10965 set_cfun (NULL);
10966 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10967 current_function_decl = NULL;
10970 /* Check the declarations given in a for-loop for satisfying the C99
10971 constraints. If exactly one such decl is found, return it. LOC is
10972 the location of the opening parenthesis of the for loop. The last
10973 parameter allows you to control the "for loop initial declarations
10974 are only allowed in C99 mode". Normally, you should pass
10975 flag_isoc99 as that parameter. But in some cases (Objective-C
10976 foreach loop, for example) we want to run the checks in this
10977 function even if not in C99 mode, so we allow the caller to turn
10978 off the error about not being in C99 mode.
10981 tree
10982 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10984 struct c_binding *b;
10985 tree one_decl = NULL_TREE;
10986 int n_decls = 0;
10988 if (!turn_off_iso_c99_error)
10990 static bool hint = true;
10991 /* If we get here, declarations have been used in a for loop without
10992 the C99 for loop scope. This doesn't make much sense, so don't
10993 allow it. */
10994 auto_diagnostic_group d;
10995 error_at (loc, "%<for%> loop initial declarations "
10996 "are only allowed in C99 or C11 mode");
10997 if (hint)
10999 inform (loc,
11000 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
11001 "%<-std=gnu11%> to compile your code");
11002 hint = false;
11004 return NULL_TREE;
11006 else
11007 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
11008 "initial declarations");
11010 /* C99 subclause 6.8.5 paragraph 3:
11012 [#3] The declaration part of a for statement shall only
11013 declare identifiers for objects having storage class auto or
11014 register.
11016 It isn't clear whether, in this sentence, "identifiers" binds to
11017 "shall only declare" or to "objects" - that is, whether all identifiers
11018 declared must be identifiers for objects, or whether the restriction
11019 only applies to those that are. (A question on this in comp.std.c
11020 in November 2000 received no answer.) We implement the strictest
11021 interpretation, to avoid creating an extension which later causes
11022 problems. */
11024 for (b = current_scope->bindings; b; b = b->prev)
11026 tree id = b->id;
11027 tree decl = b->decl;
11029 if (!id)
11030 continue;
11032 switch (TREE_CODE (decl))
11034 case VAR_DECL:
11036 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
11037 if (TREE_STATIC (decl))
11038 error_at (decl_loc,
11039 "declaration of static variable %qD in %<for%> loop "
11040 "initial declaration", decl);
11041 else if (DECL_EXTERNAL (decl))
11042 error_at (decl_loc,
11043 "declaration of %<extern%> variable %qD in %<for%> loop "
11044 "initial declaration", decl);
11046 break;
11048 case RECORD_TYPE:
11049 error_at (loc,
11050 "%<struct %E%> declared in %<for%> loop initial "
11051 "declaration", id);
11052 break;
11053 case UNION_TYPE:
11054 error_at (loc,
11055 "%<union %E%> declared in %<for%> loop initial declaration",
11056 id);
11057 break;
11058 case ENUMERAL_TYPE:
11059 error_at (loc, "%<enum %E%> declared in %<for%> loop "
11060 "initial declaration", id);
11061 break;
11062 default:
11063 error_at (loc, "declaration of non-variable "
11064 "%qD in %<for%> loop initial declaration", decl);
11067 n_decls++;
11068 one_decl = decl;
11071 return n_decls == 1 ? one_decl : NULL_TREE;
11074 /* Save and reinitialize the variables
11075 used during compilation of a C function. */
11077 void
11078 c_push_function_context (void)
11080 struct language_function *p = cfun->language;
11081 /* cfun->language might have been already allocated by the use of
11082 -Wunused-local-typedefs. In that case, just re-use it. */
11083 if (p == NULL)
11084 cfun->language = p = ggc_cleared_alloc<language_function> ();
11086 p->base.x_stmt_tree = c_stmt_tree;
11087 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
11088 p->x_in_statement = in_statement;
11089 p->x_switch_stack = c_switch_stack;
11090 p->arg_info = current_function_arg_info;
11091 p->returns_value = current_function_returns_value;
11092 p->returns_null = current_function_returns_null;
11093 p->returns_abnormally = current_function_returns_abnormally;
11094 p->warn_about_return_type = warn_about_return_type;
11096 push_function_context ();
11099 /* Restore the variables used during compilation of a C function. */
11101 void
11102 c_pop_function_context (void)
11104 struct language_function *p;
11106 pop_function_context ();
11107 p = cfun->language;
11109 /* When -Wunused-local-typedefs is in effect, cfun->languages is
11110 used to store data throughout the life time of the current cfun,
11111 So don't deallocate it. */
11112 if (!warn_unused_local_typedefs)
11113 cfun->language = NULL;
11115 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
11116 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
11118 /* Stop pointing to the local nodes about to be freed. */
11119 /* But DECL_INITIAL must remain nonzero so we know this
11120 was an actual function definition. */
11121 DECL_INITIAL (current_function_decl) = error_mark_node;
11122 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
11125 c_stmt_tree = p->base.x_stmt_tree;
11126 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
11127 in_statement = p->x_in_statement;
11128 c_switch_stack = p->x_switch_stack;
11129 current_function_arg_info = p->arg_info;
11130 current_function_returns_value = p->returns_value;
11131 current_function_returns_null = p->returns_null;
11132 current_function_returns_abnormally = p->returns_abnormally;
11133 warn_about_return_type = p->warn_about_return_type;
11136 /* The functions below are required for functionality of doing
11137 function at once processing in the C front end. Currently these
11138 functions are not called from anywhere in the C front end, but as
11139 these changes continue, that will change. */
11141 /* Returns the stmt_tree (if any) to which statements are currently
11142 being added. If there is no active statement-tree, NULL is
11143 returned. */
11145 stmt_tree
11146 current_stmt_tree (void)
11148 return &c_stmt_tree;
11151 /* Return the global value of T as a symbol. */
11153 tree
11154 identifier_global_value (tree t)
11156 struct c_binding *b;
11158 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
11159 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11160 return b->decl;
11162 return NULL_TREE;
11165 /* Return the global value of tag T as a symbol. */
11167 tree
11168 identifier_global_tag (tree t)
11170 struct c_binding *b;
11172 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
11173 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
11174 return b->decl;
11176 return NULL_TREE;
11179 /* Returns true if NAME refers to a built-in function or function-like
11180 operator. */
11182 bool
11183 names_builtin_p (const char *name)
11185 tree id = get_identifier (name);
11186 if (tree decl = identifier_global_value (id))
11187 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
11189 /* Also detect common reserved C words that aren't strictly built-in
11190 functions. */
11191 switch (C_RID_CODE (id))
11193 case RID_BUILTIN_CONVERTVECTOR:
11194 case RID_BUILTIN_HAS_ATTRIBUTE:
11195 case RID_BUILTIN_SHUFFLE:
11196 case RID_BUILTIN_SHUFFLEVECTOR:
11197 case RID_BUILTIN_ASSOC_BARRIER:
11198 case RID_CHOOSE_EXPR:
11199 case RID_OFFSETOF:
11200 case RID_TYPES_COMPATIBLE_P:
11201 return true;
11202 default:
11203 break;
11206 return false;
11209 /* In C, the only C-linkage public declaration is at file scope. */
11211 tree
11212 c_linkage_bindings (tree name)
11214 return identifier_global_value (name);
11217 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
11218 otherwise the name is found in ridpointers from RID_INDEX. */
11220 void
11221 record_builtin_type (enum rid rid_index, const char *name, tree type)
11223 tree id, decl;
11224 if (name == 0)
11225 id = ridpointers[(int) rid_index];
11226 else
11227 id = get_identifier (name);
11228 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
11229 pushdecl (decl);
11230 if (debug_hooks->type_decl)
11231 debug_hooks->type_decl (decl, false);
11234 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
11236 struct c_parm *
11237 build_c_parm (struct c_declspecs *specs, tree attrs,
11238 struct c_declarator *declarator,
11239 location_t loc)
11241 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
11242 ret->specs = specs;
11243 ret->attrs = attrs;
11244 ret->declarator = declarator;
11245 ret->loc = loc;
11246 return ret;
11249 /* Return a declarator with nested attributes. TARGET is the inner
11250 declarator to which these attributes apply. ATTRS are the
11251 attributes. */
11253 struct c_declarator *
11254 build_attrs_declarator (tree attrs, struct c_declarator *target)
11256 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11257 ret->kind = cdk_attrs;
11258 ret->declarator = target;
11259 ret->u.attrs = attrs;
11260 return ret;
11263 /* Return a declarator for a function with arguments specified by ARGS
11264 and return type specified by TARGET. */
11266 struct c_declarator *
11267 build_function_declarator (struct c_arg_info *args,
11268 struct c_declarator *target)
11270 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11271 ret->kind = cdk_function;
11272 ret->declarator = target;
11273 ret->u.arg_info = args;
11274 return ret;
11277 /* Return a declarator for the identifier IDENT (which may be
11278 NULL_TREE for an abstract declarator). */
11280 struct c_declarator *
11281 build_id_declarator (tree ident)
11283 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11284 ret->kind = cdk_id;
11285 ret->declarator = 0;
11286 ret->u.id.id = ident;
11287 ret->u.id.attrs = NULL_TREE;
11288 /* Default value - may get reset to a more precise location. */
11289 ret->id_loc = input_location;
11290 return ret;
11293 /* Return something to represent absolute declarators containing a *.
11294 TARGET is the absolute declarator that the * contains.
11295 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
11296 to apply to the pointer type. */
11298 struct c_declarator *
11299 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
11300 struct c_declarator *target)
11302 tree attrs;
11303 int quals = 0;
11304 struct c_declarator *itarget = target;
11305 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
11306 if (type_quals_attrs)
11308 attrs = type_quals_attrs->attrs;
11309 quals = quals_from_declspecs (type_quals_attrs);
11310 if (attrs != NULL_TREE)
11311 itarget = build_attrs_declarator (attrs, target);
11313 ret->kind = cdk_pointer;
11314 ret->declarator = itarget;
11315 ret->u.pointer_quals = quals;
11316 return ret;
11319 /* Return a pointer to a structure for an empty list of declaration
11320 specifiers. */
11322 struct c_declspecs *
11323 build_null_declspecs (void)
11325 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11326 memset (ret, 0, sizeof *ret);
11327 ret->align_log = -1;
11328 ret->typespec_word = cts_none;
11329 ret->storage_class = csc_none;
11330 ret->expr_const_operands = true;
11331 ret->typespec_kind = ctsk_none;
11332 ret->address_space = ADDR_SPACE_GENERIC;
11333 return ret;
11336 /* Add the address space ADDRSPACE to the declaration specifiers
11337 SPECS, returning SPECS. */
11339 struct c_declspecs *
11340 declspecs_add_addrspace (location_t location,
11341 struct c_declspecs *specs, addr_space_t as)
11343 specs->non_sc_seen_p = true;
11344 specs->declspecs_seen_p = true;
11345 specs->non_std_attrs_seen_p = true;
11347 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11348 && specs->address_space != as)
11349 error ("incompatible address space qualifiers %qs and %qs",
11350 c_addr_space_name (as),
11351 c_addr_space_name (specs->address_space));
11352 else
11354 specs->address_space = as;
11355 specs->locations[cdw_address_space] = location;
11357 return specs;
11360 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11361 returning SPECS. */
11363 struct c_declspecs *
11364 declspecs_add_qual (location_t loc,
11365 struct c_declspecs *specs, tree qual)
11367 enum rid i;
11368 bool dupe = false;
11369 specs->non_sc_seen_p = true;
11370 specs->declspecs_seen_p = true;
11371 specs->non_std_attrs_seen_p = true;
11372 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11373 && C_IS_RESERVED_WORD (qual));
11374 i = C_RID_CODE (qual);
11375 location_t prev_loc = UNKNOWN_LOCATION;
11376 switch (i)
11378 case RID_CONST:
11379 dupe = specs->const_p;
11380 specs->const_p = true;
11381 prev_loc = specs->locations[cdw_const];
11382 specs->locations[cdw_const] = loc;
11383 break;
11384 case RID_VOLATILE:
11385 dupe = specs->volatile_p;
11386 specs->volatile_p = true;
11387 prev_loc = specs->locations[cdw_volatile];
11388 specs->locations[cdw_volatile] = loc;
11389 break;
11390 case RID_RESTRICT:
11391 dupe = specs->restrict_p;
11392 specs->restrict_p = true;
11393 prev_loc = specs->locations[cdw_restrict];
11394 specs->locations[cdw_restrict] = loc;
11395 break;
11396 case RID_ATOMIC:
11397 dupe = specs->atomic_p;
11398 specs->atomic_p = true;
11399 prev_loc = specs->locations[cdw_atomic];
11400 specs->locations[cdw_atomic] = loc;
11401 break;
11402 default:
11403 gcc_unreachable ();
11405 if (dupe)
11407 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11408 "duplicate %qE declaration specifier", qual);
11409 if (!warned
11410 && warn_duplicate_decl_specifier
11411 && prev_loc >= RESERVED_LOCATION_COUNT
11412 && !from_macro_expansion_at (prev_loc)
11413 && !from_macro_expansion_at (loc))
11414 warning_at (loc, OPT_Wduplicate_decl_specifier,
11415 "duplicate %qE declaration specifier", qual);
11417 return specs;
11420 /* Add the type specifier TYPE to the declaration specifiers SPECS,
11421 returning SPECS. */
11423 struct c_declspecs *
11424 declspecs_add_type (location_t loc, struct c_declspecs *specs,
11425 struct c_typespec spec)
11427 tree type = spec.spec;
11428 specs->non_sc_seen_p = true;
11429 specs->declspecs_seen_p = true;
11430 specs->non_std_attrs_seen_p = true;
11431 specs->typespec_kind = spec.kind;
11432 if (TREE_DEPRECATED (type))
11433 specs->deprecated_p = true;
11434 if (TREE_UNAVAILABLE (type))
11435 specs->unavailable_p = true;
11437 /* As a type specifier is present, "auto" must be used as a storage
11438 class specifier, not for type deduction. */
11439 if (specs->c2x_auto_p)
11441 specs->c2x_auto_p = false;
11442 if (specs->storage_class != csc_none)
11443 error ("multiple storage classes in declaration specifiers");
11444 else if (specs->thread_p)
11445 error ("%qs used with %<auto%>",
11446 specs->thread_gnu_p ? "__thread" : "_Thread_local");
11447 else if (specs->constexpr_p)
11448 /* auto may only be used with another storage class specifier,
11449 such as constexpr, if the type is inferred. */
11450 error ("%<auto%> used with %<constexpr%>");
11451 else
11452 specs->storage_class = csc_auto;
11455 /* Handle type specifier keywords. */
11456 if (TREE_CODE (type) == IDENTIFIER_NODE
11457 && C_IS_RESERVED_WORD (type)
11458 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11460 enum rid i = C_RID_CODE (type);
11461 if (specs->type)
11463 error_at (loc, "two or more data types in declaration specifiers");
11464 return specs;
11466 if ((int) i <= (int) RID_LAST_MODIFIER)
11468 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11469 bool dupe = false;
11470 switch (i)
11472 case RID_LONG:
11473 if (specs->long_long_p)
11475 error_at (loc, "%<long long long%> is too long for GCC");
11476 break;
11478 if (specs->long_p)
11480 if (specs->typespec_word == cts_double)
11482 error_at (loc,
11483 ("both %<long long%> and %<double%> in "
11484 "declaration specifiers"));
11485 break;
11487 pedwarn_c90 (loc, OPT_Wlong_long,
11488 "ISO C90 does not support %<long long%>");
11489 specs->long_long_p = 1;
11490 specs->locations[cdw_long_long] = loc;
11491 break;
11493 if (specs->short_p)
11494 error_at (loc,
11495 ("both %<long%> and %<short%> in "
11496 "declaration specifiers"));
11497 else if (specs->typespec_word == cts_auto_type)
11498 error_at (loc,
11499 ("both %<long%> and %<__auto_type%> in "
11500 "declaration specifiers"));
11501 else if (specs->typespec_word == cts_void)
11502 error_at (loc,
11503 ("both %<long%> and %<void%> in "
11504 "declaration specifiers"));
11505 else if (specs->typespec_word == cts_int_n)
11506 error_at (loc,
11507 ("both %<long%> and %<__int%d%> in "
11508 "declaration specifiers"),
11509 int_n_data[specs->int_n_idx].bitsize);
11510 else if (specs->typespec_word == cts_bool)
11511 error_at (loc,
11512 ("both %<long%> and %<_Bool%> in "
11513 "declaration specifiers"));
11514 else if (specs->typespec_word == cts_char)
11515 error_at (loc,
11516 ("both %<long%> and %<char%> in "
11517 "declaration specifiers"));
11518 else if (specs->typespec_word == cts_float)
11519 error_at (loc,
11520 ("both %<long%> and %<float%> in "
11521 "declaration specifiers"));
11522 else if (specs->typespec_word == cts_floatn_nx)
11523 error_at (loc,
11524 ("both %<long%> and %<_Float%d%s%> in "
11525 "declaration specifiers"),
11526 floatn_nx_types[specs->floatn_nx_idx].n,
11527 (floatn_nx_types[specs->floatn_nx_idx].extended
11528 ? "x"
11529 : ""));
11530 else if (specs->typespec_word == cts_dfloat32)
11531 error_at (loc,
11532 ("both %<long%> and %<_Decimal32%> in "
11533 "declaration specifiers"));
11534 else if (specs->typespec_word == cts_dfloat64)
11535 error_at (loc,
11536 ("both %<long%> and %<_Decimal64%> in "
11537 "declaration specifiers"));
11538 else if (specs->typespec_word == cts_dfloat128)
11539 error_at (loc,
11540 ("both %<long%> and %<_Decimal128%> in "
11541 "declaration specifiers"));
11542 else
11544 specs->long_p = true;
11545 specs->locations[cdw_long] = loc;
11547 break;
11548 case RID_SHORT:
11549 dupe = specs->short_p;
11550 if (specs->long_p)
11551 error_at (loc,
11552 ("both %<long%> and %<short%> in "
11553 "declaration specifiers"));
11554 else if (specs->typespec_word == cts_auto_type)
11555 error_at (loc,
11556 ("both %<short%> and %<__auto_type%> in "
11557 "declaration specifiers"));
11558 else if (specs->typespec_word == cts_void)
11559 error_at (loc,
11560 ("both %<short%> and %<void%> in "
11561 "declaration specifiers"));
11562 else if (specs->typespec_word == cts_int_n)
11563 error_at (loc,
11564 ("both %<short%> and %<__int%d%> in "
11565 "declaration specifiers"),
11566 int_n_data[specs->int_n_idx].bitsize);
11567 else if (specs->typespec_word == cts_bool)
11568 error_at (loc,
11569 ("both %<short%> and %<_Bool%> in "
11570 "declaration specifiers"));
11571 else if (specs->typespec_word == cts_char)
11572 error_at (loc,
11573 ("both %<short%> and %<char%> in "
11574 "declaration specifiers"));
11575 else if (specs->typespec_word == cts_float)
11576 error_at (loc,
11577 ("both %<short%> and %<float%> in "
11578 "declaration specifiers"));
11579 else if (specs->typespec_word == cts_double)
11580 error_at (loc,
11581 ("both %<short%> and %<double%> in "
11582 "declaration specifiers"));
11583 else if (specs->typespec_word == cts_floatn_nx)
11584 error_at (loc,
11585 ("both %<short%> and %<_Float%d%s%> in "
11586 "declaration specifiers"),
11587 floatn_nx_types[specs->floatn_nx_idx].n,
11588 (floatn_nx_types[specs->floatn_nx_idx].extended
11589 ? "x"
11590 : ""));
11591 else if (specs->typespec_word == cts_dfloat32)
11592 error_at (loc,
11593 ("both %<short%> and %<_Decimal32%> in "
11594 "declaration specifiers"));
11595 else if (specs->typespec_word == cts_dfloat64)
11596 error_at (loc,
11597 ("both %<short%> and %<_Decimal64%> in "
11598 "declaration specifiers"));
11599 else if (specs->typespec_word == cts_dfloat128)
11600 error_at (loc,
11601 ("both %<short%> and %<_Decimal128%> in "
11602 "declaration specifiers"));
11603 else
11605 specs->short_p = true;
11606 specs->locations[cdw_short] = loc;
11608 break;
11609 case RID_SIGNED:
11610 dupe = specs->signed_p;
11611 if (specs->unsigned_p)
11612 error_at (loc,
11613 ("both %<signed%> and %<unsigned%> in "
11614 "declaration specifiers"));
11615 else if (specs->typespec_word == cts_auto_type)
11616 error_at (loc,
11617 ("both %<signed%> and %<__auto_type%> in "
11618 "declaration specifiers"));
11619 else if (specs->typespec_word == cts_void)
11620 error_at (loc,
11621 ("both %<signed%> and %<void%> in "
11622 "declaration specifiers"));
11623 else if (specs->typespec_word == cts_bool)
11624 error_at (loc,
11625 ("both %<signed%> and %<_Bool%> in "
11626 "declaration specifiers"));
11627 else if (specs->typespec_word == cts_float)
11628 error_at (loc,
11629 ("both %<signed%> and %<float%> in "
11630 "declaration specifiers"));
11631 else if (specs->typespec_word == cts_double)
11632 error_at (loc,
11633 ("both %<signed%> and %<double%> in "
11634 "declaration specifiers"));
11635 else if (specs->typespec_word == cts_floatn_nx)
11636 error_at (loc,
11637 ("both %<signed%> and %<_Float%d%s%> in "
11638 "declaration specifiers"),
11639 floatn_nx_types[specs->floatn_nx_idx].n,
11640 (floatn_nx_types[specs->floatn_nx_idx].extended
11641 ? "x"
11642 : ""));
11643 else if (specs->typespec_word == cts_dfloat32)
11644 error_at (loc,
11645 ("both %<signed%> and %<_Decimal32%> in "
11646 "declaration specifiers"));
11647 else if (specs->typespec_word == cts_dfloat64)
11648 error_at (loc,
11649 ("both %<signed%> and %<_Decimal64%> in "
11650 "declaration specifiers"));
11651 else if (specs->typespec_word == cts_dfloat128)
11652 error_at (loc,
11653 ("both %<signed%> and %<_Decimal128%> in "
11654 "declaration specifiers"));
11655 else
11657 specs->signed_p = true;
11658 specs->locations[cdw_signed] = loc;
11660 break;
11661 case RID_UNSIGNED:
11662 dupe = specs->unsigned_p;
11663 if (specs->signed_p)
11664 error_at (loc,
11665 ("both %<signed%> and %<unsigned%> in "
11666 "declaration specifiers"));
11667 else if (specs->typespec_word == cts_auto_type)
11668 error_at (loc,
11669 ("both %<unsigned%> and %<__auto_type%> in "
11670 "declaration specifiers"));
11671 else if (specs->typespec_word == cts_void)
11672 error_at (loc,
11673 ("both %<unsigned%> and %<void%> in "
11674 "declaration specifiers"));
11675 else if (specs->typespec_word == cts_bool)
11676 error_at (loc,
11677 ("both %<unsigned%> and %<_Bool%> in "
11678 "declaration specifiers"));
11679 else if (specs->typespec_word == cts_float)
11680 error_at (loc,
11681 ("both %<unsigned%> and %<float%> in "
11682 "declaration specifiers"));
11683 else if (specs->typespec_word == cts_double)
11684 error_at (loc,
11685 ("both %<unsigned%> and %<double%> in "
11686 "declaration specifiers"));
11687 else if (specs->typespec_word == cts_floatn_nx)
11688 error_at (loc,
11689 ("both %<unsigned%> and %<_Float%d%s%> in "
11690 "declaration specifiers"),
11691 floatn_nx_types[specs->floatn_nx_idx].n,
11692 (floatn_nx_types[specs->floatn_nx_idx].extended
11693 ? "x"
11694 : ""));
11695 else if (specs->typespec_word == cts_dfloat32)
11696 error_at (loc,
11697 ("both %<unsigned%> and %<_Decimal32%> in "
11698 "declaration specifiers"));
11699 else if (specs->typespec_word == cts_dfloat64)
11700 error_at (loc,
11701 ("both %<unsigned%> and %<_Decimal64%> in "
11702 "declaration specifiers"));
11703 else if (specs->typespec_word == cts_dfloat128)
11704 error_at (loc,
11705 ("both %<unsigned%> and %<_Decimal128%> in "
11706 "declaration specifiers"));
11707 else
11709 specs->unsigned_p = true;
11710 specs->locations[cdw_unsigned] = loc;
11712 break;
11713 case RID_COMPLEX:
11714 dupe = specs->complex_p;
11715 if (!in_system_header_at (loc))
11716 pedwarn_c90 (loc, OPT_Wpedantic,
11717 "ISO C90 does not support complex types");
11718 if (specs->typespec_word == cts_auto_type)
11719 error_at (loc,
11720 ("both %<complex%> and %<__auto_type%> in "
11721 "declaration specifiers"));
11722 else if (specs->typespec_word == cts_void)
11723 error_at (loc,
11724 ("both %<complex%> and %<void%> in "
11725 "declaration specifiers"));
11726 else if (specs->typespec_word == cts_bool)
11727 error_at (loc,
11728 ("both %<complex%> and %<_Bool%> in "
11729 "declaration specifiers"));
11730 else if (specs->typespec_word == cts_dfloat32)
11731 error_at (loc,
11732 ("both %<complex%> and %<_Decimal32%> in "
11733 "declaration specifiers"));
11734 else if (specs->typespec_word == cts_dfloat64)
11735 error_at (loc,
11736 ("both %<complex%> and %<_Decimal64%> in "
11737 "declaration specifiers"));
11738 else if (specs->typespec_word == cts_dfloat128)
11739 error_at (loc,
11740 ("both %<complex%> and %<_Decimal128%> in "
11741 "declaration specifiers"));
11742 else if (specs->typespec_word == cts_fract)
11743 error_at (loc,
11744 ("both %<complex%> and %<_Fract%> in "
11745 "declaration specifiers"));
11746 else if (specs->typespec_word == cts_accum)
11747 error_at (loc,
11748 ("both %<complex%> and %<_Accum%> in "
11749 "declaration specifiers"));
11750 else if (specs->saturating_p)
11751 error_at (loc,
11752 ("both %<complex%> and %<_Sat%> in "
11753 "declaration specifiers"));
11754 else
11756 specs->complex_p = true;
11757 specs->locations[cdw_complex] = loc;
11759 break;
11760 case RID_SAT:
11761 dupe = specs->saturating_p;
11762 pedwarn (loc, OPT_Wpedantic,
11763 "ISO C does not support saturating types");
11764 if (specs->typespec_word == cts_int_n)
11766 error_at (loc,
11767 ("both %<_Sat%> and %<__int%d%> in "
11768 "declaration specifiers"),
11769 int_n_data[specs->int_n_idx].bitsize);
11771 else if (specs->typespec_word == cts_auto_type)
11772 error_at (loc,
11773 ("both %<_Sat%> and %<__auto_type%> in "
11774 "declaration specifiers"));
11775 else if (specs->typespec_word == cts_void)
11776 error_at (loc,
11777 ("both %<_Sat%> and %<void%> in "
11778 "declaration specifiers"));
11779 else if (specs->typespec_word == cts_bool)
11780 error_at (loc,
11781 ("both %<_Sat%> and %<_Bool%> in "
11782 "declaration specifiers"));
11783 else if (specs->typespec_word == cts_char)
11784 error_at (loc,
11785 ("both %<_Sat%> and %<char%> in "
11786 "declaration specifiers"));
11787 else if (specs->typespec_word == cts_int)
11788 error_at (loc,
11789 ("both %<_Sat%> and %<int%> in "
11790 "declaration specifiers"));
11791 else if (specs->typespec_word == cts_float)
11792 error_at (loc,
11793 ("both %<_Sat%> and %<float%> in "
11794 "declaration specifiers"));
11795 else if (specs->typespec_word == cts_double)
11796 error_at (loc,
11797 ("both %<_Sat%> and %<double%> in "
11798 "declaration specifiers"));
11799 else if (specs->typespec_word == cts_floatn_nx)
11800 error_at (loc,
11801 ("both %<_Sat%> and %<_Float%d%s%> in "
11802 "declaration specifiers"),
11803 floatn_nx_types[specs->floatn_nx_idx].n,
11804 (floatn_nx_types[specs->floatn_nx_idx].extended
11805 ? "x"
11806 : ""));
11807 else if (specs->typespec_word == cts_dfloat32)
11808 error_at (loc,
11809 ("both %<_Sat%> and %<_Decimal32%> in "
11810 "declaration specifiers"));
11811 else if (specs->typespec_word == cts_dfloat64)
11812 error_at (loc,
11813 ("both %<_Sat%> and %<_Decimal64%> in "
11814 "declaration specifiers"));
11815 else if (specs->typespec_word == cts_dfloat128)
11816 error_at (loc,
11817 ("both %<_Sat%> and %<_Decimal128%> in "
11818 "declaration specifiers"));
11819 else if (specs->complex_p)
11820 error_at (loc,
11821 ("both %<_Sat%> and %<complex%> in "
11822 "declaration specifiers"));
11823 else
11825 specs->saturating_p = true;
11826 specs->locations[cdw_saturating] = loc;
11828 break;
11829 default:
11830 gcc_unreachable ();
11833 if (dupe)
11834 error_at (loc, "duplicate %qE", type);
11836 return specs;
11838 else
11840 /* "void", "_Bool", "char", "int", "float", "double",
11841 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11842 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11843 "__auto_type". */
11844 if (specs->typespec_word != cts_none)
11846 error_at (loc,
11847 "two or more data types in declaration specifiers");
11848 return specs;
11850 switch (i)
11852 case RID_AUTO_TYPE:
11853 if (specs->long_p)
11854 error_at (loc,
11855 ("both %<long%> and %<__auto_type%> in "
11856 "declaration specifiers"));
11857 else if (specs->short_p)
11858 error_at (loc,
11859 ("both %<short%> and %<__auto_type%> in "
11860 "declaration specifiers"));
11861 else if (specs->signed_p)
11862 error_at (loc,
11863 ("both %<signed%> and %<__auto_type%> in "
11864 "declaration specifiers"));
11865 else if (specs->unsigned_p)
11866 error_at (loc,
11867 ("both %<unsigned%> and %<__auto_type%> in "
11868 "declaration specifiers"));
11869 else if (specs->complex_p)
11870 error_at (loc,
11871 ("both %<complex%> and %<__auto_type%> in "
11872 "declaration specifiers"));
11873 else if (specs->saturating_p)
11874 error_at (loc,
11875 ("both %<_Sat%> and %<__auto_type%> in "
11876 "declaration specifiers"));
11877 else
11879 specs->typespec_word = cts_auto_type;
11880 specs->locations[cdw_typespec] = loc;
11882 return specs;
11883 case RID_INT_N_0:
11884 case RID_INT_N_1:
11885 case RID_INT_N_2:
11886 case RID_INT_N_3:
11887 specs->int_n_idx = i - RID_INT_N_0;
11888 if (!in_system_header_at (input_location)
11889 /* If the INT_N type ends in "__", and so is of the format
11890 "__intN__", don't pedwarn. */
11891 && (strncmp (IDENTIFIER_POINTER (type)
11892 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11893 pedwarn (loc, OPT_Wpedantic,
11894 "ISO C does not support %<__int%d%> types",
11895 int_n_data[specs->int_n_idx].bitsize);
11897 if (specs->long_p)
11898 error_at (loc,
11899 ("both %<__int%d%> and %<long%> in "
11900 "declaration specifiers"),
11901 int_n_data[specs->int_n_idx].bitsize);
11902 else if (specs->saturating_p)
11903 error_at (loc,
11904 ("both %<_Sat%> and %<__int%d%> in "
11905 "declaration specifiers"),
11906 int_n_data[specs->int_n_idx].bitsize);
11907 else if (specs->short_p)
11908 error_at (loc,
11909 ("both %<__int%d%> and %<short%> in "
11910 "declaration specifiers"),
11911 int_n_data[specs->int_n_idx].bitsize);
11912 else if (! int_n_enabled_p[specs->int_n_idx])
11914 specs->typespec_word = cts_int_n;
11915 error_at (loc,
11916 "%<__int%d%> is not supported on this target",
11917 int_n_data[specs->int_n_idx].bitsize);
11919 else
11921 specs->typespec_word = cts_int_n;
11922 specs->locations[cdw_typespec] = loc;
11924 return specs;
11925 case RID_VOID:
11926 if (specs->long_p)
11927 error_at (loc,
11928 ("both %<long%> and %<void%> in "
11929 "declaration specifiers"));
11930 else if (specs->short_p)
11931 error_at (loc,
11932 ("both %<short%> and %<void%> in "
11933 "declaration specifiers"));
11934 else if (specs->signed_p)
11935 error_at (loc,
11936 ("both %<signed%> and %<void%> in "
11937 "declaration specifiers"));
11938 else if (specs->unsigned_p)
11939 error_at (loc,
11940 ("both %<unsigned%> and %<void%> in "
11941 "declaration specifiers"));
11942 else if (specs->complex_p)
11943 error_at (loc,
11944 ("both %<complex%> and %<void%> in "
11945 "declaration specifiers"));
11946 else if (specs->saturating_p)
11947 error_at (loc,
11948 ("both %<_Sat%> and %<void%> in "
11949 "declaration specifiers"));
11950 else
11952 specs->typespec_word = cts_void;
11953 specs->locations[cdw_typespec] = loc;
11955 return specs;
11956 case RID_BOOL:
11957 if (!in_system_header_at (loc))
11958 pedwarn_c90 (loc, OPT_Wpedantic,
11959 "ISO C90 does not support boolean types");
11960 if (specs->long_p)
11961 error_at (loc,
11962 ("both %<long%> and %<_Bool%> in "
11963 "declaration specifiers"));
11964 else if (specs->short_p)
11965 error_at (loc,
11966 ("both %<short%> and %<_Bool%> in "
11967 "declaration specifiers"));
11968 else if (specs->signed_p)
11969 error_at (loc,
11970 ("both %<signed%> and %<_Bool%> in "
11971 "declaration specifiers"));
11972 else if (specs->unsigned_p)
11973 error_at (loc,
11974 ("both %<unsigned%> and %<_Bool%> in "
11975 "declaration specifiers"));
11976 else if (specs->complex_p)
11977 error_at (loc,
11978 ("both %<complex%> and %<_Bool%> in "
11979 "declaration specifiers"));
11980 else if (specs->saturating_p)
11981 error_at (loc,
11982 ("both %<_Sat%> and %<_Bool%> in "
11983 "declaration specifiers"));
11984 else
11986 specs->typespec_word = cts_bool;
11987 specs->locations[cdw_typespec] = loc;
11989 return specs;
11990 case RID_CHAR:
11991 if (specs->long_p)
11992 error_at (loc,
11993 ("both %<long%> and %<char%> in "
11994 "declaration specifiers"));
11995 else if (specs->short_p)
11996 error_at (loc,
11997 ("both %<short%> and %<char%> in "
11998 "declaration specifiers"));
11999 else if (specs->saturating_p)
12000 error_at (loc,
12001 ("both %<_Sat%> and %<char%> in "
12002 "declaration specifiers"));
12003 else
12005 specs->typespec_word = cts_char;
12006 specs->locations[cdw_typespec] = loc;
12008 return specs;
12009 case RID_INT:
12010 if (specs->saturating_p)
12011 error_at (loc,
12012 ("both %<_Sat%> and %<int%> in "
12013 "declaration specifiers"));
12014 else
12016 specs->typespec_word = cts_int;
12017 specs->locations[cdw_typespec] = loc;
12019 return specs;
12020 case RID_FLOAT:
12021 if (specs->long_p)
12022 error_at (loc,
12023 ("both %<long%> and %<float%> in "
12024 "declaration specifiers"));
12025 else if (specs->short_p)
12026 error_at (loc,
12027 ("both %<short%> and %<float%> in "
12028 "declaration specifiers"));
12029 else if (specs->signed_p)
12030 error_at (loc,
12031 ("both %<signed%> and %<float%> in "
12032 "declaration specifiers"));
12033 else if (specs->unsigned_p)
12034 error_at (loc,
12035 ("both %<unsigned%> and %<float%> in "
12036 "declaration specifiers"));
12037 else if (specs->saturating_p)
12038 error_at (loc,
12039 ("both %<_Sat%> and %<float%> in "
12040 "declaration specifiers"));
12041 else
12043 specs->typespec_word = cts_float;
12044 specs->locations[cdw_typespec] = loc;
12046 return specs;
12047 case RID_DOUBLE:
12048 if (specs->long_long_p)
12049 error_at (loc,
12050 ("both %<long long%> and %<double%> in "
12051 "declaration specifiers"));
12052 else if (specs->short_p)
12053 error_at (loc,
12054 ("both %<short%> and %<double%> in "
12055 "declaration specifiers"));
12056 else if (specs->signed_p)
12057 error_at (loc,
12058 ("both %<signed%> and %<double%> in "
12059 "declaration specifiers"));
12060 else if (specs->unsigned_p)
12061 error_at (loc,
12062 ("both %<unsigned%> and %<double%> in "
12063 "declaration specifiers"));
12064 else if (specs->saturating_p)
12065 error_at (loc,
12066 ("both %<_Sat%> and %<double%> in "
12067 "declaration specifiers"));
12068 else
12070 specs->typespec_word = cts_double;
12071 specs->locations[cdw_typespec] = loc;
12073 return specs;
12074 CASE_RID_FLOATN_NX:
12075 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
12076 if (!in_system_header_at (input_location))
12077 pedwarn (loc, OPT_Wpedantic,
12078 "ISO C does not support the %<_Float%d%s%> type",
12079 floatn_nx_types[specs->floatn_nx_idx].n,
12080 (floatn_nx_types[specs->floatn_nx_idx].extended
12081 ? "x"
12082 : ""));
12084 if (specs->long_p)
12085 error_at (loc,
12086 ("both %<long%> and %<_Float%d%s%> in "
12087 "declaration specifiers"),
12088 floatn_nx_types[specs->floatn_nx_idx].n,
12089 (floatn_nx_types[specs->floatn_nx_idx].extended
12090 ? "x"
12091 : ""));
12092 else if (specs->short_p)
12093 error_at (loc,
12094 ("both %<short%> and %<_Float%d%s%> in "
12095 "declaration specifiers"),
12096 floatn_nx_types[specs->floatn_nx_idx].n,
12097 (floatn_nx_types[specs->floatn_nx_idx].extended
12098 ? "x"
12099 : ""));
12100 else if (specs->signed_p)
12101 error_at (loc,
12102 ("both %<signed%> and %<_Float%d%s%> in "
12103 "declaration specifiers"),
12104 floatn_nx_types[specs->floatn_nx_idx].n,
12105 (floatn_nx_types[specs->floatn_nx_idx].extended
12106 ? "x"
12107 : ""));
12108 else if (specs->unsigned_p)
12109 error_at (loc,
12110 ("both %<unsigned%> and %<_Float%d%s%> in "
12111 "declaration specifiers"),
12112 floatn_nx_types[specs->floatn_nx_idx].n,
12113 (floatn_nx_types[specs->floatn_nx_idx].extended
12114 ? "x"
12115 : ""));
12116 else if (specs->saturating_p)
12117 error_at (loc,
12118 ("both %<_Sat%> and %<_Float%d%s%> in "
12119 "declaration specifiers"),
12120 floatn_nx_types[specs->floatn_nx_idx].n,
12121 (floatn_nx_types[specs->floatn_nx_idx].extended
12122 ? "x"
12123 : ""));
12124 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12126 specs->typespec_word = cts_floatn_nx;
12127 error_at (loc,
12128 "%<_Float%d%s%> is not supported on this target",
12129 floatn_nx_types[specs->floatn_nx_idx].n,
12130 (floatn_nx_types[specs->floatn_nx_idx].extended
12131 ? "x"
12132 : ""));
12134 else
12136 specs->typespec_word = cts_floatn_nx;
12137 specs->locations[cdw_typespec] = loc;
12139 return specs;
12140 case RID_DFLOAT32:
12141 case RID_DFLOAT64:
12142 case RID_DFLOAT128:
12144 const char *str;
12145 if (i == RID_DFLOAT32)
12146 str = "_Decimal32";
12147 else if (i == RID_DFLOAT64)
12148 str = "_Decimal64";
12149 else
12150 str = "_Decimal128";
12151 if (specs->long_long_p)
12152 error_at (loc,
12153 ("both %<long long%> and %qs in "
12154 "declaration specifiers"),
12155 str);
12156 if (specs->long_p)
12157 error_at (loc,
12158 ("both %<long%> and %qs in "
12159 "declaration specifiers"),
12160 str);
12161 else if (specs->short_p)
12162 error_at (loc,
12163 ("both %<short%> and %qs in "
12164 "declaration specifiers"),
12165 str);
12166 else if (specs->signed_p)
12167 error_at (loc,
12168 ("both %<signed%> and %qs in "
12169 "declaration specifiers"),
12170 str);
12171 else if (specs->unsigned_p)
12172 error_at (loc,
12173 ("both %<unsigned%> and %qs in "
12174 "declaration specifiers"),
12175 str);
12176 else if (specs->complex_p)
12177 error_at (loc,
12178 ("both %<complex%> and %qs in "
12179 "declaration specifiers"),
12180 str);
12181 else if (specs->saturating_p)
12182 error_at (loc,
12183 ("both %<_Sat%> and %qs in "
12184 "declaration specifiers"),
12185 str);
12186 else if (i == RID_DFLOAT32)
12187 specs->typespec_word = cts_dfloat32;
12188 else if (i == RID_DFLOAT64)
12189 specs->typespec_word = cts_dfloat64;
12190 else
12191 specs->typespec_word = cts_dfloat128;
12192 specs->locations[cdw_typespec] = loc;
12194 if (!targetm.decimal_float_supported_p ())
12195 error_at (loc,
12196 ("decimal floating-point not supported "
12197 "for this target"));
12198 pedwarn_c11 (loc, OPT_Wpedantic,
12199 "ISO C does not support decimal floating-point "
12200 "before C2X");
12201 return specs;
12202 case RID_FRACT:
12203 case RID_ACCUM:
12205 const char *str;
12206 if (i == RID_FRACT)
12207 str = "_Fract";
12208 else
12209 str = "_Accum";
12210 if (specs->complex_p)
12211 error_at (loc,
12212 ("both %<complex%> and %qs in "
12213 "declaration specifiers"),
12214 str);
12215 else if (i == RID_FRACT)
12216 specs->typespec_word = cts_fract;
12217 else
12218 specs->typespec_word = cts_accum;
12219 specs->locations[cdw_typespec] = loc;
12221 if (!targetm.fixed_point_supported_p ())
12222 error_at (loc,
12223 "fixed-point types not supported for this target");
12224 pedwarn (loc, OPT_Wpedantic,
12225 "ISO C does not support fixed-point types");
12226 return specs;
12227 default:
12228 /* ObjC reserved word "id", handled below. */
12229 break;
12234 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
12235 form of ObjC type, cases such as "int" and "long" being handled
12236 above), a TYPE (struct, union, enum and typeof specifiers) or an
12237 ERROR_MARK. In none of these cases may there have previously
12238 been any type specifiers. */
12239 if (specs->type || specs->typespec_word != cts_none
12240 || specs->long_p || specs->short_p || specs->signed_p
12241 || specs->unsigned_p || specs->complex_p)
12242 error_at (loc, "two or more data types in declaration specifiers");
12243 else if (TREE_CODE (type) == TYPE_DECL)
12245 specs->type = TREE_TYPE (type);
12246 if (TREE_TYPE (type) != error_mark_node)
12248 specs->decl_attr = DECL_ATTRIBUTES (type);
12249 specs->typedef_p = true;
12250 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
12251 specs->locations[cdw_typedef] = loc;
12253 /* If this typedef name is defined in a struct, then a C++
12254 lookup would return a different value. */
12255 if (warn_cxx_compat
12256 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
12257 warning_at (loc, OPT_Wc___compat,
12258 "C++ lookup of %qD would return a field, not a type",
12259 type);
12261 /* If we are parsing a struct, record that a struct field
12262 used a typedef. */
12263 if (warn_cxx_compat && struct_parse_info != NULL)
12264 struct_parse_info->typedefs_seen.safe_push (type);
12267 else if (TREE_CODE (type) == IDENTIFIER_NODE)
12269 tree t = lookup_name (type);
12270 if (!t || TREE_CODE (t) != TYPE_DECL)
12271 error_at (loc, "%qE fails to be a typedef or built in type", type);
12272 else if (TREE_TYPE (t) == error_mark_node)
12274 else
12276 specs->type = TREE_TYPE (t);
12277 specs->locations[cdw_typespec] = loc;
12280 else
12282 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
12284 specs->typedef_p = true;
12285 specs->locations[cdw_typedef] = loc;
12286 if (spec.expr)
12288 if (specs->expr)
12289 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
12290 specs->expr, spec.expr);
12291 else
12292 specs->expr = spec.expr;
12293 specs->expr_const_operands &= spec.expr_const_operands;
12296 specs->type = type;
12297 if (spec.has_enum_type_specifier
12298 && spec.kind != ctsk_tagdef)
12299 specs->enum_type_specifier_ref_p = true;
12302 return specs;
12305 /* Add the storage class specifier or function specifier SCSPEC to the
12306 declaration specifiers SPECS, returning SPECS. */
12308 struct c_declspecs *
12309 declspecs_add_scspec (location_t loc,
12310 struct c_declspecs *specs,
12311 tree scspec)
12313 enum rid i;
12314 enum c_storage_class n = csc_none;
12315 bool dupe = false;
12316 specs->declspecs_seen_p = true;
12317 specs->non_std_attrs_seen_p = true;
12318 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
12319 && C_IS_RESERVED_WORD (scspec));
12320 i = C_RID_CODE (scspec);
12321 if (specs->non_sc_seen_p)
12322 warning (OPT_Wold_style_declaration,
12323 "%qE is not at beginning of declaration", scspec);
12324 switch (i)
12326 case RID_INLINE:
12327 /* C99 permits duplicate inline. Although of doubtful utility,
12328 it seems simplest to permit it in gnu89 mode as well, as
12329 there is also little utility in maintaining this as a
12330 difference between gnu89 and C99 inline. */
12331 dupe = false;
12332 specs->inline_p = true;
12333 specs->locations[cdw_inline] = loc;
12334 break;
12335 case RID_NORETURN:
12336 /* Duplicate _Noreturn is permitted. */
12337 dupe = false;
12338 specs->noreturn_p = true;
12339 specs->locations[cdw_noreturn] = loc;
12340 break;
12341 case RID_THREAD:
12342 dupe = specs->thread_p;
12343 if (specs->storage_class == csc_auto)
12344 error ("%qE used with %<auto%>", scspec);
12345 else if (specs->storage_class == csc_register)
12346 error ("%qE used with %<register%>", scspec);
12347 else if (specs->storage_class == csc_typedef)
12348 error ("%qE used with %<typedef%>", scspec);
12349 else if (specs->constexpr_p)
12350 error ("%qE used with %<constexpr%>", scspec);
12351 else
12353 specs->thread_p = true;
12354 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12355 "__thread") == 0);
12356 /* A diagnostic is not required for the use of this
12357 identifier in the implementation namespace; only diagnose
12358 it for the C11 spelling because of existing code using
12359 the other spelling. */
12360 if (!specs->thread_gnu_p)
12362 if (flag_isoc99)
12363 pedwarn_c99 (loc, OPT_Wpedantic,
12364 "ISO C99 does not support %qE", scspec);
12365 else
12366 pedwarn_c99 (loc, OPT_Wpedantic,
12367 "ISO C90 does not support %qE", scspec);
12369 specs->locations[cdw_thread] = loc;
12371 break;
12372 case RID_AUTO:
12373 if (flag_isoc2x
12374 && specs->typespec_kind == ctsk_none
12375 && specs->storage_class != csc_typedef)
12377 /* "auto" potentially used for type deduction. */
12378 if (specs->c2x_auto_p)
12379 error ("duplicate %qE", scspec);
12380 specs->c2x_auto_p = true;
12381 return specs;
12383 n = csc_auto;
12384 /* auto may only be used with another storage class specifier,
12385 such as constexpr, if the type is inferred. */
12386 if (specs->constexpr_p)
12387 error ("%qE used with %<constexpr%>", scspec);
12388 break;
12389 case RID_EXTERN:
12390 n = csc_extern;
12391 /* Diagnose "__thread extern". */
12392 if (specs->thread_p && specs->thread_gnu_p)
12393 error ("%<__thread%> before %<extern%>");
12394 break;
12395 case RID_REGISTER:
12396 n = csc_register;
12397 break;
12398 case RID_STATIC:
12399 n = csc_static;
12400 /* Diagnose "__thread static". */
12401 if (specs->thread_p && specs->thread_gnu_p)
12402 error ("%<__thread%> before %<static%>");
12403 break;
12404 case RID_TYPEDEF:
12405 n = csc_typedef;
12406 if (specs->c2x_auto_p)
12408 error ("%<typedef%> used with %<auto%>");
12409 specs->c2x_auto_p = false;
12411 break;
12412 case RID_CONSTEXPR:
12413 dupe = specs->constexpr_p;
12414 if (specs->storage_class == csc_extern)
12415 error ("%qE used with %<extern%>", scspec);
12416 else if (specs->storage_class == csc_typedef)
12417 error ("%qE used with %<typedef%>", scspec);
12418 else if (specs->storage_class == csc_auto)
12419 /* auto may only be used with another storage class specifier,
12420 such as constexpr, if the type is inferred. */
12421 error ("%qE used with %<auto%>", scspec);
12422 else if (specs->thread_p)
12423 error ("%qE used with %qs", scspec,
12424 specs->thread_gnu_p ? "__thread" : "_Thread_local");
12425 else
12426 specs->constexpr_p = true;
12427 break;
12428 default:
12429 gcc_unreachable ();
12431 if (n != csc_none && n == specs->storage_class)
12432 dupe = true;
12433 if (dupe)
12435 if (i == RID_THREAD)
12436 error ("duplicate %<_Thread_local%> or %<__thread%>");
12437 else
12438 error ("duplicate %qE", scspec);
12440 if (n != csc_none)
12442 if (specs->storage_class != csc_none && n != specs->storage_class)
12444 error ("multiple storage classes in declaration specifiers");
12446 else
12448 specs->storage_class = n;
12449 specs->locations[cdw_storage_class] = loc;
12450 if (n != csc_extern && n != csc_static && specs->thread_p)
12452 error ("%qs used with %qE",
12453 specs->thread_gnu_p ? "__thread" : "_Thread_local",
12454 scspec);
12455 specs->thread_p = false;
12457 if (n != csc_auto && n != csc_register && n != csc_static
12458 && specs->constexpr_p)
12460 error ("%<constexpr%> used with %qE", scspec);
12461 specs->constexpr_p = false;
12465 return specs;
12468 /* Add the attributes ATTRS to the declaration specifiers SPECS,
12469 returning SPECS. */
12471 struct c_declspecs *
12472 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12474 specs->attrs = chainon (attrs, specs->attrs);
12475 specs->locations[cdw_attributes] = loc;
12476 specs->declspecs_seen_p = true;
12477 /* In the case of standard attributes at the start of the
12478 declaration, the caller will reset this. */
12479 specs->non_std_attrs_seen_p = true;
12480 return specs;
12483 /* Add an _Alignas specifier (expression ALIGN, or type whose
12484 alignment is ALIGN) to the declaration specifiers SPECS, returning
12485 SPECS. */
12486 struct c_declspecs *
12487 declspecs_add_alignas (location_t loc,
12488 struct c_declspecs *specs, tree align)
12490 specs->alignas_p = true;
12491 specs->locations[cdw_alignas] = loc;
12492 if (align == error_mark_node)
12493 return specs;
12495 /* Only accept the alignment if it's valid and greater than
12496 the current one. Zero is invalid but by C11 required to
12497 be silently ignored. */
12498 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12499 if (align_log > specs->align_log)
12500 specs->align_log = align_log;
12501 return specs;
12504 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12505 specifiers with any other type specifier to determine the resulting
12506 type. This is where ISO C checks on complex types are made, since
12507 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12508 double". Also apply postfix standard attributes to modify the type. */
12510 struct c_declspecs *
12511 finish_declspecs (struct c_declspecs *specs)
12513 /* If a type was specified as a whole, we have no modifiers and are
12514 done. */
12515 if (specs->type != NULL_TREE)
12517 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12518 && !specs->signed_p && !specs->unsigned_p
12519 && !specs->complex_p && !specs->c2x_auto_p);
12521 /* Set a dummy type. */
12522 if (TREE_CODE (specs->type) == ERROR_MARK)
12523 specs->type = integer_type_node;
12524 goto handle_postfix_attrs;
12527 /* If none of "void", "_Bool", "char", "int", "float" or "double"
12528 has been specified, treat it as "int" unless "_Complex" is
12529 present and there are no other specifiers. If we just have
12530 "_Complex", it is equivalent to "_Complex double", but e.g.
12531 "_Complex short" is equivalent to "_Complex short int". */
12532 if (specs->typespec_word == cts_none)
12534 if (specs->saturating_p)
12536 error_at (specs->locations[cdw_saturating],
12537 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12538 if (!targetm.fixed_point_supported_p ())
12539 error_at (specs->locations[cdw_saturating],
12540 "fixed-point types not supported for this target");
12541 specs->typespec_word = cts_fract;
12543 else if (specs->long_p || specs->short_p
12544 || specs->signed_p || specs->unsigned_p)
12546 specs->typespec_word = cts_int;
12548 else if (specs->complex_p)
12550 specs->typespec_word = cts_double;
12551 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12552 "ISO C does not support plain %<complex%> meaning "
12553 "%<double complex%>");
12555 else if (specs->c2x_auto_p)
12557 /* Type to be filled in later, including applying postfix
12558 attributes. This warning only actually appears for
12559 -Wc11-c2x-compat in C2X mode; in older modes, there may
12560 be a warning or pedwarn for implicit "int" instead, or
12561 other errors for use of auto at file scope. */
12562 pedwarn_c11 (input_location, OPT_Wpedantic,
12563 "ISO C does not support %<auto%> type deduction "
12564 "before C2X");
12565 return specs;
12567 else
12569 specs->typespec_word = cts_int;
12570 specs->default_int_p = true;
12571 /* We don't diagnose this here because grokdeclarator will
12572 give more specific diagnostics according to whether it is
12573 a function definition. */
12577 /* If "signed" was specified, record this to distinguish "int" and
12578 "signed int" in the case of a bit-field with
12579 -funsigned-bitfields. */
12580 specs->explicit_signed_p = specs->signed_p;
12582 /* Now compute the actual type. */
12583 gcc_assert (!specs->c2x_auto_p);
12584 switch (specs->typespec_word)
12586 case cts_auto_type:
12587 gcc_assert (!specs->long_p && !specs->short_p
12588 && !specs->signed_p && !specs->unsigned_p
12589 && !specs->complex_p);
12590 /* Type to be filled in later. */
12591 if (specs->postfix_attrs)
12592 error ("%<__auto_type%> followed by %<[[]]%> attributes");
12593 break;
12594 case cts_void:
12595 gcc_assert (!specs->long_p && !specs->short_p
12596 && !specs->signed_p && !specs->unsigned_p
12597 && !specs->complex_p);
12598 specs->type = void_type_node;
12599 break;
12600 case cts_bool:
12601 gcc_assert (!specs->long_p && !specs->short_p
12602 && !specs->signed_p && !specs->unsigned_p
12603 && !specs->complex_p);
12604 specs->type = boolean_type_node;
12605 break;
12606 case cts_char:
12607 gcc_assert (!specs->long_p && !specs->short_p);
12608 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12609 if (specs->signed_p)
12610 specs->type = signed_char_type_node;
12611 else if (specs->unsigned_p)
12612 specs->type = unsigned_char_type_node;
12613 else
12614 specs->type = char_type_node;
12615 if (specs->complex_p)
12617 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12618 "ISO C does not support complex integer types");
12619 specs->type = build_complex_type (specs->type);
12621 break;
12622 case cts_int_n:
12623 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12624 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12625 if (! int_n_enabled_p[specs->int_n_idx])
12626 specs->type = integer_type_node;
12627 else
12628 specs->type = (specs->unsigned_p
12629 ? int_n_trees[specs->int_n_idx].unsigned_type
12630 : int_n_trees[specs->int_n_idx].signed_type);
12631 if (specs->complex_p)
12633 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12634 "ISO C does not support complex integer types");
12635 specs->type = build_complex_type (specs->type);
12637 break;
12638 case cts_int:
12639 gcc_assert (!(specs->long_p && specs->short_p));
12640 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12641 if (specs->long_long_p)
12642 specs->type = (specs->unsigned_p
12643 ? long_long_unsigned_type_node
12644 : long_long_integer_type_node);
12645 else if (specs->long_p)
12646 specs->type = (specs->unsigned_p
12647 ? long_unsigned_type_node
12648 : long_integer_type_node);
12649 else if (specs->short_p)
12650 specs->type = (specs->unsigned_p
12651 ? short_unsigned_type_node
12652 : short_integer_type_node);
12653 else
12654 specs->type = (specs->unsigned_p
12655 ? unsigned_type_node
12656 : integer_type_node);
12657 if (specs->complex_p)
12659 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12660 "ISO C does not support complex integer types");
12661 specs->type = build_complex_type (specs->type);
12663 break;
12664 case cts_float:
12665 gcc_assert (!specs->long_p && !specs->short_p
12666 && !specs->signed_p && !specs->unsigned_p);
12667 specs->type = (specs->complex_p
12668 ? complex_float_type_node
12669 : float_type_node);
12670 break;
12671 case cts_double:
12672 gcc_assert (!specs->long_long_p && !specs->short_p
12673 && !specs->signed_p && !specs->unsigned_p);
12674 if (specs->long_p)
12676 specs->type = (specs->complex_p
12677 ? complex_long_double_type_node
12678 : long_double_type_node);
12680 else
12682 specs->type = (specs->complex_p
12683 ? complex_double_type_node
12684 : double_type_node);
12686 break;
12687 case cts_floatn_nx:
12688 gcc_assert (!specs->long_p && !specs->short_p
12689 && !specs->signed_p && !specs->unsigned_p);
12690 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12691 specs->type = integer_type_node;
12692 else if (specs->complex_p)
12693 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12694 else
12695 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12696 break;
12697 case cts_dfloat32:
12698 case cts_dfloat64:
12699 case cts_dfloat128:
12700 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12701 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12702 if (!targetm.decimal_float_supported_p ())
12703 specs->type = integer_type_node;
12704 else if (specs->typespec_word == cts_dfloat32)
12705 specs->type = dfloat32_type_node;
12706 else if (specs->typespec_word == cts_dfloat64)
12707 specs->type = dfloat64_type_node;
12708 else
12709 specs->type = dfloat128_type_node;
12710 break;
12711 case cts_fract:
12712 gcc_assert (!specs->complex_p);
12713 if (!targetm.fixed_point_supported_p ())
12714 specs->type = integer_type_node;
12715 else if (specs->saturating_p)
12717 if (specs->long_long_p)
12718 specs->type = specs->unsigned_p
12719 ? sat_unsigned_long_long_fract_type_node
12720 : sat_long_long_fract_type_node;
12721 else if (specs->long_p)
12722 specs->type = specs->unsigned_p
12723 ? sat_unsigned_long_fract_type_node
12724 : sat_long_fract_type_node;
12725 else if (specs->short_p)
12726 specs->type = specs->unsigned_p
12727 ? sat_unsigned_short_fract_type_node
12728 : sat_short_fract_type_node;
12729 else
12730 specs->type = specs->unsigned_p
12731 ? sat_unsigned_fract_type_node
12732 : sat_fract_type_node;
12734 else
12736 if (specs->long_long_p)
12737 specs->type = specs->unsigned_p
12738 ? unsigned_long_long_fract_type_node
12739 : long_long_fract_type_node;
12740 else if (specs->long_p)
12741 specs->type = specs->unsigned_p
12742 ? unsigned_long_fract_type_node
12743 : long_fract_type_node;
12744 else if (specs->short_p)
12745 specs->type = specs->unsigned_p
12746 ? unsigned_short_fract_type_node
12747 : short_fract_type_node;
12748 else
12749 specs->type = specs->unsigned_p
12750 ? unsigned_fract_type_node
12751 : fract_type_node;
12753 break;
12754 case cts_accum:
12755 gcc_assert (!specs->complex_p);
12756 if (!targetm.fixed_point_supported_p ())
12757 specs->type = integer_type_node;
12758 else if (specs->saturating_p)
12760 if (specs->long_long_p)
12761 specs->type = specs->unsigned_p
12762 ? sat_unsigned_long_long_accum_type_node
12763 : sat_long_long_accum_type_node;
12764 else if (specs->long_p)
12765 specs->type = specs->unsigned_p
12766 ? sat_unsigned_long_accum_type_node
12767 : sat_long_accum_type_node;
12768 else if (specs->short_p)
12769 specs->type = specs->unsigned_p
12770 ? sat_unsigned_short_accum_type_node
12771 : sat_short_accum_type_node;
12772 else
12773 specs->type = specs->unsigned_p
12774 ? sat_unsigned_accum_type_node
12775 : sat_accum_type_node;
12777 else
12779 if (specs->long_long_p)
12780 specs->type = specs->unsigned_p
12781 ? unsigned_long_long_accum_type_node
12782 : long_long_accum_type_node;
12783 else if (specs->long_p)
12784 specs->type = specs->unsigned_p
12785 ? unsigned_long_accum_type_node
12786 : long_accum_type_node;
12787 else if (specs->short_p)
12788 specs->type = specs->unsigned_p
12789 ? unsigned_short_accum_type_node
12790 : short_accum_type_node;
12791 else
12792 specs->type = specs->unsigned_p
12793 ? unsigned_accum_type_node
12794 : accum_type_node;
12796 break;
12797 default:
12798 gcc_unreachable ();
12800 handle_postfix_attrs:
12801 if (specs->type != NULL)
12803 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12804 decl_attributes (&specs->type, specs->postfix_attrs, 0);
12805 specs->postfix_attrs = NULL_TREE;
12808 return specs;
12811 /* Perform final processing on one file scope's declarations (or the
12812 external scope's declarations), GLOBALS. */
12814 static void
12815 c_write_global_declarations_1 (tree globals)
12817 tree decl;
12818 bool reconsider;
12820 /* Process the decls in the order they were written. */
12821 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12823 /* Check for used but undefined static functions using the C
12824 standard's definition of "used", and set TREE_NO_WARNING so
12825 that check_global_declaration doesn't repeat the check. */
12826 if (TREE_CODE (decl) == FUNCTION_DECL
12827 && DECL_INITIAL (decl) == NULL_TREE
12828 && DECL_EXTERNAL (decl)
12829 && !TREE_PUBLIC (decl))
12831 if (C_DECL_USED (decl))
12833 /* TODO: Add OPT_Wundefined-inline. */
12834 if (pedwarn (input_location, 0, "%q+F used but never defined",
12835 decl))
12836 suppress_warning (decl /* OPT_Wundefined-inline. */);
12838 /* For -Wunused-function warn about unused static prototypes. */
12839 else if (warn_unused_function
12840 && ! DECL_ARTIFICIAL (decl)
12841 && ! warning_suppressed_p (decl, OPT_Wunused_function))
12843 if (warning (OPT_Wunused_function,
12844 "%q+F declared %<static%> but never defined",
12845 decl))
12846 suppress_warning (decl, OPT_Wunused_function);
12850 wrapup_global_declaration_1 (decl);
12855 reconsider = false;
12856 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12857 reconsider |= wrapup_global_declaration_2 (decl);
12859 while (reconsider);
12862 /* Preserve the external declarations scope across a garbage collect. */
12863 static GTY(()) tree ext_block;
12865 /* Collect all references relevant to SOURCE_FILE. */
12867 static void
12868 collect_all_refs (const char *source_file)
12870 tree t;
12871 unsigned i;
12873 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12874 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12876 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12879 /* Collect source file references at global level. */
12881 static void
12882 collect_source_refs (void)
12884 tree t;
12885 tree decls;
12886 tree decl;
12887 unsigned i;
12889 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12891 decls = DECL_INITIAL (t);
12892 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12893 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12894 collect_source_ref (DECL_SOURCE_FILE (decl));
12897 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12898 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12899 collect_source_ref (DECL_SOURCE_FILE (decl));
12902 /* Free attribute access data that are not needed by the middle end. */
12904 static void
12905 free_attr_access_data ()
12907 struct cgraph_node *n;
12909 /* Iterate over all functions declared in the translation unit. */
12910 FOR_EACH_FUNCTION (n)
12912 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12913 if (tree attrs = DECL_ATTRIBUTES (parm))
12914 attr_access::free_lang_data (attrs);
12916 tree fntype = TREE_TYPE (n->decl);
12917 if (!fntype || fntype == error_mark_node)
12918 continue;
12919 tree attrs = TYPE_ATTRIBUTES (fntype);
12920 if (!attrs)
12921 continue;
12923 attr_access::free_lang_data (attrs);
12927 /* Perform any final parser cleanups and generate initial debugging
12928 information. */
12930 void
12931 c_parse_final_cleanups (void)
12933 tree t;
12934 unsigned i;
12936 /* We don't want to do this if generating a PCH. */
12937 if (pch_file)
12938 return;
12940 timevar_stop (TV_PHASE_PARSING);
12941 timevar_start (TV_PHASE_DEFERRED);
12943 /* Do the Objective-C stuff. This is where all the Objective-C
12944 module stuff gets generated (symtab, class/protocol/selector
12945 lists etc). */
12946 if (c_dialect_objc ())
12947 objc_write_global_declarations ();
12949 /* Close the external scope. */
12950 ext_block = pop_scope ();
12951 external_scope = 0;
12952 gcc_assert (!current_scope);
12954 /* Handle -fdump-ada-spec[-slim]. */
12955 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12957 /* Build a table of files to generate specs for */
12958 collect_source_ref (main_input_filename);
12959 if (!flag_dump_ada_spec_slim)
12960 collect_source_refs ();
12962 dump_ada_specs (collect_all_refs, NULL);
12965 /* Process all file scopes in this compilation, and the external_scope,
12966 through wrapup_global_declarations. */
12967 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12968 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12969 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12971 if (!in_lto_p)
12972 free_attr_access_data ();
12974 timevar_stop (TV_PHASE_DEFERRED);
12975 timevar_start (TV_PHASE_PARSING);
12977 ext_block = NULL;
12980 /* Register reserved keyword WORD as qualifier for address space AS. */
12982 void
12983 c_register_addr_space (const char *word, addr_space_t as)
12985 int rid = RID_FIRST_ADDR_SPACE + as;
12986 tree id;
12988 /* Address space qualifiers are only supported
12989 in C with GNU extensions enabled. */
12990 if (c_dialect_objc () || flag_no_asm)
12991 return;
12993 id = get_identifier (word);
12994 C_SET_RID_CODE (id, rid);
12995 C_IS_RESERVED_WORD (id) = 1;
12996 ridpointers [rid] = id;
12999 /* Return identifier to look up for omp declare reduction. */
13001 tree
13002 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
13004 const char *p = NULL;
13005 switch (reduction_code)
13007 case PLUS_EXPR: p = "+"; break;
13008 case MULT_EXPR: p = "*"; break;
13009 case MINUS_EXPR: p = "-"; break;
13010 case BIT_AND_EXPR: p = "&"; break;
13011 case BIT_XOR_EXPR: p = "^"; break;
13012 case BIT_IOR_EXPR: p = "|"; break;
13013 case TRUTH_ANDIF_EXPR: p = "&&"; break;
13014 case TRUTH_ORIF_EXPR: p = "||"; break;
13015 case MIN_EXPR: p = "min"; break;
13016 case MAX_EXPR: p = "max"; break;
13017 default:
13018 break;
13021 if (p == NULL)
13023 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
13024 return error_mark_node;
13025 p = IDENTIFIER_POINTER (reduction_id);
13028 const char prefix[] = "omp declare reduction ";
13029 size_t lenp = sizeof (prefix);
13030 size_t len = strlen (p);
13031 char *name = XALLOCAVEC (char, lenp + len);
13032 memcpy (name, prefix, lenp - 1);
13033 memcpy (name + lenp - 1, p, len + 1);
13034 return get_identifier (name);
13037 /* Lookup REDUCTION_ID in the current scope, or create an artificial
13038 VAR_DECL, bind it into the current scope and return it. */
13040 tree
13041 c_omp_reduction_decl (tree reduction_id)
13043 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13044 if (b != NULL && B_IN_CURRENT_SCOPE (b))
13045 return b->decl;
13047 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
13048 reduction_id, integer_type_node);
13049 DECL_ARTIFICIAL (decl) = 1;
13050 DECL_EXTERNAL (decl) = 1;
13051 TREE_STATIC (decl) = 1;
13052 TREE_PUBLIC (decl) = 0;
13053 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
13054 return decl;
13057 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
13059 tree
13060 c_omp_reduction_lookup (tree reduction_id, tree type)
13062 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
13063 while (b)
13065 tree t;
13066 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
13067 if (comptypes (TREE_PURPOSE (t), type))
13068 return TREE_VALUE (t);
13069 b = b->shadowed;
13071 return error_mark_node;
13074 /* Helper function called via walk_tree, to diagnose invalid
13075 #pragma omp declare reduction combiners or initializers. */
13077 tree
13078 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
13080 tree *vars = (tree *) data;
13081 if (SSA_VAR_P (*tp)
13082 && !DECL_ARTIFICIAL (*tp)
13083 && *tp != vars[0]
13084 && *tp != vars[1])
13086 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
13087 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
13088 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
13089 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
13090 *tp);
13091 else
13092 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
13093 "to variable %qD which is not %<omp_priv%> nor "
13094 "%<omp_orig%>",
13095 *tp);
13096 return *tp;
13098 return NULL_TREE;
13102 bool
13103 c_check_in_current_scope (tree decl)
13105 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
13106 return b != NULL && B_IN_CURRENT_SCOPE (b);
13109 #include "gt-c-c-decl.h"