Fix thinko in new warning on type punning for storage order purposes
[official-gcc.git] / gcc / c / c-decl.c
bloba86792bbe06534dce40338302a99965ff7740be0
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2021 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_UNIQUE_PTR
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)). An object declared as
77 __attribute__((deprecated)) suppresses warnings of uses of other
78 deprecated items. */
80 enum deprecated_states {
81 DEPRECATED_NORMAL,
82 DEPRECATED_SUPPRESS
86 /* Nonzero if we have seen an invalid cross reference
87 to a struct, union, or enum, but not yet printed the message. */
88 tree pending_invalid_xref;
90 /* File and line to appear in the eventual error message. */
91 location_t pending_invalid_xref_location;
93 /* The file and line that the prototype came from if this is an
94 old-style definition; used for diagnostics in
95 store_parm_decls_oldstyle. */
97 static location_t current_function_prototype_locus;
99 /* Whether this prototype was built-in. */
101 static bool current_function_prototype_built_in;
103 /* The argument type information of this prototype. */
105 static tree current_function_prototype_arg_types;
107 /* The argument information structure for the function currently being
108 defined. */
110 static struct c_arg_info *current_function_arg_info;
112 /* The obstack on which parser and related data structures, which are
113 not live beyond their top-level declaration or definition, are
114 allocated. */
115 struct obstack parser_obstack;
117 /* The current statement tree. */
119 static GTY(()) struct stmt_tree_s c_stmt_tree;
121 /* Zero if we are not in an iteration or switch statement, otherwise
122 a bitmask. See bitmask definitions in c-tree.h. */
123 unsigned char in_statement;
125 /* A list of decls to be made automatically visible in each file scope. */
126 static GTY(()) tree visible_builtins;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a return statement that specifies a return value is seen. */
131 int current_function_returns_value;
133 /* Set to 0 at beginning of a function definition, set to 1 if
134 a return statement with no argument is seen. */
136 int current_function_returns_null;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a call to a noreturn function is seen. */
141 int current_function_returns_abnormally;
143 /* Set to nonzero by `grokdeclarator' for a function
144 whose return type is defaulted, if warnings for this are desired. */
146 static int warn_about_return_type;
148 /* Nonzero when the current toplevel function contains a declaration
149 of a nested function which is never defined. */
151 static bool undef_nested_function;
153 /* If non-zero, implicit "omp declare target" attribute is added into the
154 attribute lists. */
155 int current_omp_declare_target_attribute;
157 /* Each c_binding structure describes one binding of an identifier to
158 a decl. All the decls in a scope - irrespective of namespace - are
159 chained together by the ->prev field, which (as the name implies)
160 runs in reverse order. All the decls in a given namespace bound to
161 a given identifier are chained by the ->shadowed field, which runs
162 from inner to outer scopes.
164 The ->decl field usually points to a DECL node, but there are two
165 exceptions. In the namespace of type tags, the bound entity is a
166 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
167 identifier is encountered, it is bound to error_mark_node to
168 suppress further errors about that identifier in the current
169 function.
171 The ->u.type field stores the type of the declaration in this scope;
172 if NULL, the type is the type of the ->decl field. This is only of
173 relevance for objects with external or internal linkage which may
174 be redeclared in inner scopes, forming composite types that only
175 persist for the duration of those scopes. In the external scope,
176 this stores the composite of all the types declared for this
177 object, visible or not. The ->inner_comp field (used only at file
178 scope) stores whether an incomplete array type at file scope was
179 completed at an inner scope to an array size other than 1.
181 The ->u.label field is used for labels. It points to a structure
182 which stores additional information used for warnings.
184 The depth field is copied from the scope structure that holds this
185 decl. It is used to preserve the proper ordering of the ->shadowed
186 field (see bind()) and also for a handful of special-case checks.
187 Finally, the invisible bit is true for a decl which should be
188 ignored for purposes of normal name lookup, and the nested bit is
189 true for a decl that's been bound a second time in an inner scope;
190 in all such cases, the binding in the outer scope will have its
191 invisible bit true. */
193 struct GTY((chain_next ("%h.prev"))) c_binding {
194 union GTY(()) { /* first so GTY desc can use decl */
195 tree GTY((tag ("0"))) type; /* the type in this scope */
196 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
197 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
198 tree decl; /* the decl bound */
199 tree id; /* the identifier it's bound to */
200 struct c_binding *prev; /* the previous decl in this scope */
201 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
202 unsigned int depth : 28; /* depth of this scope */
203 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
204 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
205 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
206 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
207 location_t locus; /* location for nested bindings */
209 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
210 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
211 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
212 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
214 /* Each C symbol points to three linked lists of c_binding structures.
215 These describe the values of the identifier in the three different
216 namespaces defined by the language. */
218 struct GTY(()) lang_identifier {
219 struct c_common_identifier common_id;
220 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
221 struct c_binding *tag_binding; /* struct/union/enum tags */
222 struct c_binding *label_binding; /* labels */
225 /* Validate c-lang.c's assumptions. */
226 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
227 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
229 /* The binding oracle; see c-tree.h. */
230 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
232 /* This flag is set on an identifier if we have previously asked the
233 binding oracle for this identifier's symbol binding. */
234 #define I_SYMBOL_CHECKED(node) \
235 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
237 static inline struct c_binding* *
238 i_symbol_binding (tree node)
240 struct lang_identifier *lid
241 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
243 if (lid->symbol_binding == NULL
244 && c_binding_oracle != NULL
245 && !I_SYMBOL_CHECKED (node))
247 /* Set the "checked" flag first, to avoid infinite recursion
248 when the binding oracle calls back into gcc. */
249 I_SYMBOL_CHECKED (node) = 1;
250 c_binding_oracle (C_ORACLE_SYMBOL, node);
253 return &lid->symbol_binding;
256 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
258 #define I_SYMBOL_DECL(node) \
259 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
261 /* This flag is set on an identifier if we have previously asked the
262 binding oracle for this identifier's tag binding. */
263 #define I_TAG_CHECKED(node) \
264 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
266 static inline struct c_binding **
267 i_tag_binding (tree node)
269 struct lang_identifier *lid
270 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
272 if (lid->tag_binding == NULL
273 && c_binding_oracle != NULL
274 && !I_TAG_CHECKED (node))
276 /* Set the "checked" flag first, to avoid infinite recursion
277 when the binding oracle calls back into gcc. */
278 I_TAG_CHECKED (node) = 1;
279 c_binding_oracle (C_ORACLE_TAG, node);
282 return &lid->tag_binding;
285 #define I_TAG_BINDING(node) (*i_tag_binding (node))
287 #define I_TAG_DECL(node) \
288 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
290 /* This flag is set on an identifier if we have previously asked the
291 binding oracle for this identifier's label binding. */
292 #define I_LABEL_CHECKED(node) \
293 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
295 static inline struct c_binding **
296 i_label_binding (tree node)
298 struct lang_identifier *lid
299 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
301 if (lid->label_binding == NULL
302 && c_binding_oracle != NULL
303 && !I_LABEL_CHECKED (node))
305 /* Set the "checked" flag first, to avoid infinite recursion
306 when the binding oracle calls back into gcc. */
307 I_LABEL_CHECKED (node) = 1;
308 c_binding_oracle (C_ORACLE_LABEL, node);
311 return &lid->label_binding;
314 #define I_LABEL_BINDING(node) (*i_label_binding (node))
316 #define I_LABEL_DECL(node) \
317 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
319 /* The resulting tree type. */
321 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
322 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
324 union tree_node GTY ((tag ("0"),
325 desc ("tree_node_structure (&%h)")))
326 generic;
327 struct lang_identifier GTY ((tag ("1"))) identifier;
330 /* Track bindings and other things that matter for goto warnings. For
331 efficiency, we do not gather all the decls at the point of
332 definition. Instead, we point into the bindings structure. As
333 scopes are popped, we update these structures and gather the decls
334 that matter at that time. */
336 struct GTY(()) c_spot_bindings {
337 /* The currently open scope which holds bindings defined when the
338 label was defined or the goto statement was found. */
339 struct c_scope *scope;
340 /* The bindings in the scope field which were defined at the point
341 of the label or goto. This lets us look at older or newer
342 bindings in the scope, as appropriate. */
343 struct c_binding *bindings_in_scope;
344 /* The number of statement expressions that have started since this
345 label or goto statement was defined. This is zero if we are at
346 the same statement expression level. It is positive if we are in
347 a statement expression started since this spot. It is negative
348 if this spot was in a statement expression and we have left
349 it. */
350 int stmt_exprs;
351 /* Whether we started in a statement expression but are no longer in
352 it. This is set to true if stmt_exprs ever goes negative. */
353 bool left_stmt_expr;
356 /* This structure is used to keep track of bindings seen when a goto
357 statement is defined. This is only used if we see the goto
358 statement before we see the label. */
360 struct GTY(()) c_goto_bindings {
361 /* The location of the goto statement. */
362 location_t loc;
363 /* The bindings of the goto statement. */
364 struct c_spot_bindings goto_bindings;
367 typedef struct c_goto_bindings *c_goto_bindings_p;
369 /* The additional information we keep track of for a label binding.
370 These fields are updated as scopes are popped. */
372 struct GTY(()) c_label_vars {
373 /* The shadowed c_label_vars, when one label shadows another (which
374 can only happen using a __label__ declaration). */
375 struct c_label_vars *shadowed;
376 /* The bindings when the label was defined. */
377 struct c_spot_bindings label_bindings;
378 /* A list of decls that we care about: decls about which we should
379 warn if a goto branches to this label from later in the function.
380 Decls are added to this list as scopes are popped. We only add
381 the decls that matter. */
382 vec<tree, va_gc> *decls_in_scope;
383 /* A list of goto statements to this label. This is only used for
384 goto statements seen before the label was defined, so that we can
385 issue appropriate warnings for them. */
386 vec<c_goto_bindings_p, va_gc> *gotos;
389 /* Each c_scope structure describes the complete contents of one
390 scope. Four scopes are distinguished specially: the innermost or
391 current scope, the innermost function scope, the file scope (always
392 the second to outermost) and the outermost or external scope.
394 Most declarations are recorded in the current scope.
396 All normal label declarations are recorded in the innermost
397 function scope, as are bindings of undeclared identifiers to
398 error_mark_node. (GCC permits nested functions as an extension,
399 hence the 'innermost' qualifier.) Explicitly declared labels
400 (using the __label__ extension) appear in the current scope.
402 Being in the file scope (current_scope == file_scope) causes
403 special behavior in several places below. Also, under some
404 conditions the Objective-C front end records declarations in the
405 file scope even though that isn't the current scope.
407 All declarations with external linkage are recorded in the external
408 scope, even if they aren't visible there; this models the fact that
409 such declarations are visible to the entire program, and (with a
410 bit of cleverness, see pushdecl) allows diagnosis of some violations
411 of C99 6.2.2p7 and 6.2.7p2:
413 If, within the same translation unit, the same identifier appears
414 with both internal and external linkage, the behavior is
415 undefined.
417 All declarations that refer to the same object or function shall
418 have compatible type; otherwise, the behavior is undefined.
420 Initially only the built-in declarations, which describe compiler
421 intrinsic functions plus a subset of the standard library, are in
422 this scope.
424 The order of the blocks list matters, and it is frequently appended
425 to. To avoid having to walk all the way to the end of the list on
426 each insertion, or reverse the list later, we maintain a pointer to
427 the last list entry. (FIXME: It should be feasible to use a reversed
428 list here.)
430 The bindings list is strictly in reverse order of declarations;
431 pop_scope relies on this. */
434 struct GTY((chain_next ("%h.outer"))) c_scope {
435 /* The scope containing this one. */
436 struct c_scope *outer;
438 /* The next outermost function scope. */
439 struct c_scope *outer_function;
441 /* All bindings in this scope. */
442 struct c_binding *bindings;
444 /* For each scope (except the global one), a chain of BLOCK nodes
445 for all the scopes that were entered and exited one level down. */
446 tree blocks;
447 tree blocks_last;
449 /* The depth of this scope. Used to keep the ->shadowed chain of
450 bindings sorted innermost to outermost. */
451 unsigned int depth : 28;
453 /* True if we are currently filling this scope with parameter
454 declarations. */
455 BOOL_BITFIELD parm_flag : 1;
457 /* True if we saw [*] in this scope. Used to give an error messages
458 if these appears in a function definition. */
459 BOOL_BITFIELD had_vla_unspec : 1;
461 /* True if we already complained about forward parameter decls
462 in this scope. This prevents double warnings on
463 foo (int a; int b; ...) */
464 BOOL_BITFIELD warned_forward_parm_decls : 1;
466 /* True if this is the outermost block scope of a function body.
467 This scope contains the parameters, the local variables declared
468 in the outermost block, and all the labels (except those in
469 nested functions, or declared at block scope with __label__). */
470 BOOL_BITFIELD function_body : 1;
472 /* True means make a BLOCK for this scope no matter what. */
473 BOOL_BITFIELD keep : 1;
475 /* True means that an unsuffixed float constant is _Decimal64. */
476 BOOL_BITFIELD float_const_decimal64 : 1;
478 /* True if this scope has any label bindings. This is used to speed
479 up searching for labels when popping scopes, particularly since
480 labels are normally only found at function scope. */
481 BOOL_BITFIELD has_label_bindings : 1;
483 /* True if we should issue a warning if a goto statement crosses any
484 of the bindings. We still need to check the list of bindings to
485 find the specific ones we need to warn about. This is true if
486 decl_jump_unsafe would return true for any of the bindings. This
487 is used to avoid looping over all the bindings unnecessarily. */
488 BOOL_BITFIELD has_jump_unsafe_decl : 1;
491 /* The scope currently in effect. */
493 static GTY(()) struct c_scope *current_scope;
495 /* The innermost function scope. Ordinary (not explicitly declared)
496 labels, bindings to error_mark_node, and the lazily-created
497 bindings of __func__ and its friends get this scope. */
499 static GTY(()) struct c_scope *current_function_scope;
501 /* The C file scope. This is reset for each input translation unit. */
503 static GTY(()) struct c_scope *file_scope;
505 /* The outermost scope. This is used for all declarations with
506 external linkage, and only these, hence the name. */
508 static GTY(()) struct c_scope *external_scope;
510 /* A chain of c_scope structures awaiting reuse. */
512 static GTY((deletable)) struct c_scope *scope_freelist;
514 /* A chain of c_binding structures awaiting reuse. */
516 static GTY((deletable)) struct c_binding *binding_freelist;
518 /* Append VAR to LIST in scope SCOPE. */
519 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
520 struct c_scope *s_ = (scope); \
521 tree d_ = (decl); \
522 if (s_->list##_last) \
523 BLOCK_CHAIN (s_->list##_last) = d_; \
524 else \
525 s_->list = d_; \
526 s_->list##_last = d_; \
527 } while (0)
529 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
530 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
531 struct c_scope *t_ = (tscope); \
532 struct c_scope *f_ = (fscope); \
533 if (t_->to##_last) \
534 BLOCK_CHAIN (t_->to##_last) = f_->from; \
535 else \
536 t_->to = f_->from; \
537 t_->to##_last = f_->from##_last; \
538 } while (0)
540 /* A c_inline_static structure stores details of a static identifier
541 referenced in a definition of a function that may be an inline
542 definition if no subsequent declaration of that function uses
543 "extern" or does not use "inline". */
545 struct GTY((chain_next ("%h.next"))) c_inline_static {
546 /* The location for a diagnostic. */
547 location_t location;
549 /* The function that may be an inline definition. */
550 tree function;
552 /* The object or function referenced. */
553 tree static_decl;
555 /* What sort of reference this is. */
556 enum c_inline_static_type type;
558 /* The next such structure or NULL. */
559 struct c_inline_static *next;
562 /* List of static identifiers used or referenced in functions that may
563 be inline definitions. */
564 static GTY(()) struct c_inline_static *c_inline_statics;
566 /* True means unconditionally make a BLOCK for the next scope pushed. */
568 static bool keep_next_level_flag;
570 /* True means the next call to push_scope will be the outermost scope
571 of a function body, so do not push a new scope, merely cease
572 expecting parameter decls. */
574 static bool next_is_function_body;
576 /* A vector of pointers to c_binding structures. */
578 typedef struct c_binding *c_binding_ptr;
580 /* Information that we keep for a struct or union while it is being
581 parsed. */
583 class c_struct_parse_info
585 public:
586 /* If warn_cxx_compat, a list of types defined within this
587 struct. */
588 auto_vec<tree> struct_types;
589 /* If warn_cxx_compat, a list of field names which have bindings,
590 and which are defined in this struct, but which are not defined
591 in any enclosing struct. This is used to clear the in_struct
592 field of the c_bindings structure. */
593 auto_vec<c_binding_ptr> fields;
594 /* If warn_cxx_compat, a list of typedef names used when defining
595 fields in this struct. */
596 auto_vec<tree> typedefs_seen;
599 /* Information for the struct or union currently being parsed, or
600 NULL if not parsing a struct or union. */
601 static class c_struct_parse_info *struct_parse_info;
603 /* Forward declarations. */
604 static tree lookup_name_in_scope (tree, struct c_scope *);
605 static tree c_make_fname_decl (location_t, tree, int);
606 static tree grokdeclarator (const struct c_declarator *,
607 struct c_declspecs *,
608 enum decl_context, bool, tree *, tree *, tree *,
609 bool *, enum deprecated_states);
610 static tree grokparms (struct c_arg_info *, bool);
611 static void layout_array_type (tree);
612 static void warn_defaults_to (location_t, int, const char *, ...)
613 ATTRIBUTE_GCC_DIAG(3,4);
614 static const char *header_for_builtin_fn (tree);
616 /* T is a statement. Add it to the statement-tree. This is the
617 C/ObjC version--C++ has a slightly different version of this
618 function. */
620 tree
621 add_stmt (tree t)
623 enum tree_code code = TREE_CODE (t);
625 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
627 if (!EXPR_HAS_LOCATION (t))
628 SET_EXPR_LOCATION (t, input_location);
631 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
632 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
634 /* Add T to the statement-tree. Non-side-effect statements need to be
635 recorded during statement expressions. */
636 if (!building_stmt_list_p ())
637 push_stmt_list ();
638 append_to_statement_list_force (t, &cur_stmt_list);
640 return t;
643 /* Build a pointer type using the default pointer mode. */
645 static tree
646 c_build_pointer_type (tree to_type)
648 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
649 : TYPE_ADDR_SPACE (to_type);
650 machine_mode pointer_mode;
652 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
653 pointer_mode = targetm.addr_space.pointer_mode (as);
654 else
655 pointer_mode = c_default_pointer_mode;
656 return build_pointer_type_for_mode (to_type, pointer_mode, false);
660 /* Return true if we will want to say something if a goto statement
661 crosses DECL. */
663 static bool
664 decl_jump_unsafe (tree decl)
666 if (error_operand_p (decl))
667 return false;
669 /* Don't warn for compound literals. If a goto statement crosses
670 their initialization, it should cross also all the places where
671 the complit is used or where the complit address might be saved
672 into some variable, so code after the label to which goto jumps
673 should not be able to refer to the compound literal. */
674 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
675 return false;
677 /* Always warn about crossing variably modified types. */
678 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
679 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
680 return true;
682 /* Otherwise, only warn if -Wgoto-misses-init and this is an
683 initialized automatic decl. */
684 if (warn_jump_misses_init
685 && VAR_P (decl)
686 && !TREE_STATIC (decl)
687 && DECL_INITIAL (decl) != NULL_TREE)
688 return true;
690 return false;
694 void
695 c_print_identifier (FILE *file, tree node, int indent)
697 void (*save) (enum c_oracle_request, tree identifier);
699 /* Temporarily hide any binding oracle. Without this, calls to
700 debug_tree from the debugger will end up calling into the oracle,
701 making for a confusing debug session. As the oracle isn't needed
702 here for normal operation, it's simplest to suppress it. */
703 save = c_binding_oracle;
704 c_binding_oracle = NULL;
706 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
707 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
708 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
709 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
711 tree rid = ridpointers[C_RID_CODE (node)];
712 indent_to (file, indent + 4);
713 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
714 (void *) rid, IDENTIFIER_POINTER (rid));
717 c_binding_oracle = save;
720 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
721 which may be any of several kinds of DECL or TYPE or error_mark_node,
722 in the scope SCOPE. */
723 static void
724 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
725 bool nested, location_t locus)
727 struct c_binding *b, **here;
729 if (binding_freelist)
731 b = binding_freelist;
732 binding_freelist = b->prev;
734 else
735 b = ggc_alloc<c_binding> ();
737 b->shadowed = 0;
738 b->decl = decl;
739 b->id = name;
740 b->depth = scope->depth;
741 b->invisible = invisible;
742 b->nested = nested;
743 b->inner_comp = 0;
744 b->in_struct = 0;
745 b->locus = locus;
747 b->u.type = NULL;
749 b->prev = scope->bindings;
750 scope->bindings = b;
752 if (decl_jump_unsafe (decl))
753 scope->has_jump_unsafe_decl = 1;
755 if (!name)
756 return;
758 switch (TREE_CODE (decl))
760 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
761 case ENUMERAL_TYPE:
762 case UNION_TYPE:
763 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
764 case VAR_DECL:
765 case FUNCTION_DECL:
766 case TYPE_DECL:
767 case CONST_DECL:
768 case PARM_DECL:
769 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
771 default:
772 gcc_unreachable ();
775 /* Locate the appropriate place in the chain of shadowed decls
776 to insert this binding. Normally, scope == current_scope and
777 this does nothing. */
778 while (*here && (*here)->depth > scope->depth)
779 here = &(*here)->shadowed;
781 b->shadowed = *here;
782 *here = b;
785 /* Clear the binding structure B, stick it on the binding_freelist,
786 and return the former value of b->prev. This is used by pop_scope
787 and get_parm_info to iterate destructively over all the bindings
788 from a given scope. */
789 static struct c_binding *
790 free_binding_and_advance (struct c_binding *b)
792 struct c_binding *prev = b->prev;
794 memset (b, 0, sizeof (struct c_binding));
795 b->prev = binding_freelist;
796 binding_freelist = b;
798 return prev;
801 /* Bind a label. Like bind, but skip fields which aren't used for
802 labels, and add the LABEL_VARS value. */
803 static void
804 bind_label (tree name, tree label, struct c_scope *scope,
805 struct c_label_vars *label_vars)
807 struct c_binding *b;
809 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
810 UNKNOWN_LOCATION);
812 scope->has_label_bindings = true;
814 b = scope->bindings;
815 gcc_assert (b->decl == label);
816 label_vars->shadowed = b->u.label;
817 b->u.label = label_vars;
820 /* Hook called at end of compilation to assume 1 elt
821 for a file-scope tentative array defn that wasn't complete before. */
823 void
824 c_finish_incomplete_decl (tree decl)
826 if (VAR_P (decl))
828 tree type = TREE_TYPE (decl);
829 if (type != error_mark_node
830 && TREE_CODE (type) == ARRAY_TYPE
831 && !DECL_EXTERNAL (decl)
832 && TYPE_DOMAIN (type) == NULL_TREE)
834 warning_at (DECL_SOURCE_LOCATION (decl),
835 0, "array %q+D assumed to have one element", decl);
837 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
839 relayout_decl (decl);
844 /* Record that inline function FUNC contains a reference (location
845 LOC) to static DECL (file-scope or function-local according to
846 TYPE). */
848 void
849 record_inline_static (location_t loc, tree func, tree decl,
850 enum c_inline_static_type type)
852 c_inline_static *csi = ggc_alloc<c_inline_static> ();
853 csi->location = loc;
854 csi->function = func;
855 csi->static_decl = decl;
856 csi->type = type;
857 csi->next = c_inline_statics;
858 c_inline_statics = csi;
861 /* Check for references to static declarations in inline functions at
862 the end of the translation unit and diagnose them if the functions
863 are still inline definitions. */
865 static void
866 check_inline_statics (void)
868 struct c_inline_static *csi;
869 for (csi = c_inline_statics; csi; csi = csi->next)
871 if (DECL_EXTERNAL (csi->function))
872 switch (csi->type)
874 case csi_internal:
875 pedwarn (csi->location, 0,
876 "%qD is static but used in inline function %qD "
877 "which is not static", csi->static_decl, csi->function);
878 break;
879 case csi_modifiable:
880 pedwarn (csi->location, 0,
881 "%q+D is static but declared in inline function %qD "
882 "which is not static", csi->static_decl, csi->function);
883 break;
884 default:
885 gcc_unreachable ();
888 c_inline_statics = NULL;
891 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
892 for the current state, otherwise set it to uninitialized. */
894 static void
895 set_spot_bindings (struct c_spot_bindings *p, bool defining)
897 if (defining)
899 p->scope = current_scope;
900 p->bindings_in_scope = current_scope->bindings;
902 else
904 p->scope = NULL;
905 p->bindings_in_scope = NULL;
907 p->stmt_exprs = 0;
908 p->left_stmt_expr = false;
911 /* Update spot bindings P as we pop out of SCOPE. Return true if we
912 should push decls for a label. */
914 static bool
915 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
917 if (p->scope != scope)
919 /* This label or goto is defined in some other scope, or it is a
920 label which is not yet defined. There is nothing to
921 update. */
922 return false;
925 /* Adjust the spot bindings to refer to the bindings already defined
926 in the enclosing scope. */
927 p->scope = scope->outer;
928 p->bindings_in_scope = p->scope->bindings;
930 return true;
933 /* The Objective-C front-end often needs to determine the current scope. */
935 void *
936 objc_get_current_scope (void)
938 return current_scope;
941 /* The following function is used only by Objective-C. It needs to live here
942 because it accesses the innards of c_scope. */
944 void
945 objc_mark_locals_volatile (void *enclosing_blk)
947 struct c_scope *scope;
948 struct c_binding *b;
950 for (scope = current_scope;
951 scope && scope != enclosing_blk;
952 scope = scope->outer)
954 for (b = scope->bindings; b; b = b->prev)
955 objc_volatilize_decl (b->decl);
957 /* Do not climb up past the current function. */
958 if (scope->function_body)
959 break;
963 /* Return true if we are in the global binding level. */
965 bool
966 global_bindings_p (void)
968 return current_scope == file_scope;
971 /* Return true if we're declaring parameters in an old-style function
972 declaration. */
974 bool
975 old_style_parameter_scope (void)
977 /* If processing parameters and there is no function statement list, we
978 * have an old-style function declaration. */
979 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
982 void
983 keep_next_level (void)
985 keep_next_level_flag = true;
988 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
990 void
991 set_float_const_decimal64 (void)
993 current_scope->float_const_decimal64 = true;
996 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
998 void
999 clear_float_const_decimal64 (void)
1001 current_scope->float_const_decimal64 = false;
1004 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1006 bool
1007 float_const_decimal64_p (void)
1009 return current_scope->float_const_decimal64;
1012 /* Identify this scope as currently being filled with parameters. */
1014 void
1015 declare_parm_level (void)
1017 current_scope->parm_flag = true;
1020 void
1021 push_scope (void)
1023 if (next_is_function_body)
1025 /* This is the transition from the parameters to the top level
1026 of the function body. These are the same scope
1027 (C99 6.2.1p4,6) so we do not push another scope structure.
1028 next_is_function_body is set only by store_parm_decls, which
1029 in turn is called when and only when we are about to
1030 encounter the opening curly brace for the function body.
1032 The outermost block of a function always gets a BLOCK node,
1033 because the debugging output routines expect that each
1034 function has at least one BLOCK. */
1035 current_scope->parm_flag = false;
1036 current_scope->function_body = true;
1037 current_scope->keep = true;
1038 current_scope->outer_function = current_function_scope;
1039 current_function_scope = current_scope;
1041 keep_next_level_flag = false;
1042 next_is_function_body = false;
1044 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1045 if (current_scope->outer)
1046 current_scope->float_const_decimal64
1047 = current_scope->outer->float_const_decimal64;
1048 else
1049 current_scope->float_const_decimal64 = false;
1051 else
1053 struct c_scope *scope;
1054 if (scope_freelist)
1056 scope = scope_freelist;
1057 scope_freelist = scope->outer;
1059 else
1060 scope = ggc_cleared_alloc<c_scope> ();
1062 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1063 if (current_scope)
1064 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1065 else
1066 scope->float_const_decimal64 = false;
1068 scope->keep = keep_next_level_flag;
1069 scope->outer = current_scope;
1070 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1072 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1073 possible. */
1074 if (current_scope && scope->depth == 0)
1076 scope->depth--;
1077 sorry ("GCC supports only %u nested scopes", scope->depth);
1080 current_scope = scope;
1081 keep_next_level_flag = false;
1085 /* This is called when we are leaving SCOPE. For each label defined
1086 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1087 These are the decls whose initialization will be skipped by a goto
1088 later in the function. */
1090 static void
1091 update_label_decls (struct c_scope *scope)
1093 struct c_scope *s;
1095 s = scope;
1096 while (s != NULL)
1098 if (s->has_label_bindings)
1100 struct c_binding *b;
1102 for (b = s->bindings; b != NULL; b = b->prev)
1104 struct c_label_vars *label_vars;
1105 struct c_binding *b1;
1106 bool hjud;
1107 unsigned int ix;
1108 struct c_goto_bindings *g;
1110 if (TREE_CODE (b->decl) != LABEL_DECL)
1111 continue;
1112 label_vars = b->u.label;
1114 b1 = label_vars->label_bindings.bindings_in_scope;
1115 if (label_vars->label_bindings.scope == NULL)
1116 hjud = false;
1117 else
1118 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1119 if (update_spot_bindings (scope, &label_vars->label_bindings))
1121 /* This label is defined in this scope. */
1122 if (hjud)
1124 for (; b1 != NULL; b1 = b1->prev)
1126 /* A goto from later in the function to this
1127 label will never see the initialization
1128 of B1, if any. Save it to issue a
1129 warning if needed. */
1130 if (decl_jump_unsafe (b1->decl))
1131 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1136 /* Update the bindings of any goto statements associated
1137 with this label. */
1138 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1139 update_spot_bindings (scope, &g->goto_bindings);
1143 /* Don't search beyond the current function. */
1144 if (s == current_function_scope)
1145 break;
1147 s = s->outer;
1151 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1153 static void
1154 set_type_context (tree type, tree context)
1156 for (type = TYPE_MAIN_VARIANT (type); type;
1157 type = TYPE_NEXT_VARIANT (type))
1158 TYPE_CONTEXT (type) = context;
1161 /* Exit a scope. Restore the state of the identifier-decl mappings
1162 that were in effect when this scope was entered. Return a BLOCK
1163 node containing all the DECLs in this scope that are of interest
1164 to debug info generation. */
1166 tree
1167 pop_scope (void)
1169 struct c_scope *scope = current_scope;
1170 tree block, context, p;
1171 struct c_binding *b;
1173 bool functionbody = scope->function_body;
1174 bool keep = functionbody || scope->keep || scope->bindings;
1176 update_label_decls (scope);
1178 /* If appropriate, create a BLOCK to record the decls for the life
1179 of this function. */
1180 block = NULL_TREE;
1181 if (keep)
1183 block = make_node (BLOCK);
1184 BLOCK_SUBBLOCKS (block) = scope->blocks;
1185 TREE_USED (block) = 1;
1187 /* In each subblock, record that this is its superior. */
1188 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1189 BLOCK_SUPERCONTEXT (p) = block;
1191 BLOCK_VARS (block) = NULL_TREE;
1194 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1195 scope must be set so that they point to the appropriate
1196 construct, i.e. either to the current FUNCTION_DECL node, or
1197 else to the BLOCK node we just constructed.
1199 Note that for tagged types whose scope is just the formal
1200 parameter list for some function type specification, we can't
1201 properly set their TYPE_CONTEXTs here, because we don't have a
1202 pointer to the appropriate FUNCTION_TYPE node readily available
1203 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1204 type nodes get set in `grokdeclarator' as soon as we have created
1205 the FUNCTION_TYPE node which will represent the "scope" for these
1206 "parameter list local" tagged types. */
1207 if (scope->function_body)
1208 context = current_function_decl;
1209 else if (scope == file_scope)
1211 tree file_decl
1212 = build_translation_unit_decl (get_identifier (main_input_filename));
1213 context = file_decl;
1214 debug_hooks->register_main_translation_unit (file_decl);
1216 else
1217 context = block;
1219 /* Clear all bindings in this scope. */
1220 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1222 p = b->decl;
1223 switch (TREE_CODE (p))
1225 case LABEL_DECL:
1226 /* Warnings for unused labels, errors for undefined labels. */
1227 if (TREE_USED (p) && !DECL_INITIAL (p))
1229 error ("label %q+D used but not defined", p);
1230 DECL_INITIAL (p) = error_mark_node;
1232 else
1233 warn_for_unused_label (p);
1235 /* Labels go in BLOCK_VARS. */
1236 DECL_CHAIN (p) = BLOCK_VARS (block);
1237 BLOCK_VARS (block) = p;
1238 gcc_assert (I_LABEL_BINDING (b->id) == b);
1239 I_LABEL_BINDING (b->id) = b->shadowed;
1241 /* Also pop back to the shadowed label_vars. */
1242 release_tree_vector (b->u.label->decls_in_scope);
1243 b->u.label = b->u.label->shadowed;
1244 break;
1246 case ENUMERAL_TYPE:
1247 case UNION_TYPE:
1248 case RECORD_TYPE:
1249 set_type_context (p, context);
1251 /* Types may not have tag-names, in which case the type
1252 appears in the bindings list with b->id NULL. */
1253 if (b->id)
1255 gcc_assert (I_TAG_BINDING (b->id) == b);
1256 I_TAG_BINDING (b->id) = b->shadowed;
1258 break;
1260 case FUNCTION_DECL:
1261 /* Propagate TREE_ADDRESSABLE from nested functions to their
1262 containing functions. */
1263 if (!TREE_ASM_WRITTEN (p)
1264 && DECL_INITIAL (p) != NULL_TREE
1265 && TREE_ADDRESSABLE (p)
1266 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1267 && DECL_ABSTRACT_ORIGIN (p) != p)
1268 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1269 if (!TREE_PUBLIC (p)
1270 && !DECL_INITIAL (p)
1271 && !b->nested
1272 && scope != file_scope
1273 && scope != external_scope)
1275 error ("nested function %q+D declared but never defined", p);
1276 undef_nested_function = true;
1278 else if (DECL_DECLARED_INLINE_P (p)
1279 && TREE_PUBLIC (p)
1280 && !DECL_INITIAL (p))
1282 /* C99 6.7.4p6: "a function with external linkage... declared
1283 with an inline function specifier ... shall also be defined
1284 in the same translation unit." */
1285 if (!flag_gnu89_inline
1286 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1287 && scope == external_scope)
1288 pedwarn (input_location, 0,
1289 "inline function %q+D declared but never defined", p);
1290 DECL_EXTERNAL (p) = 1;
1293 goto common_symbol;
1295 case VAR_DECL:
1296 /* Warnings for unused variables. */
1297 if ((!TREE_USED (p) || !DECL_READ_P (p))
1298 && !TREE_NO_WARNING (p)
1299 && !DECL_IN_SYSTEM_HEADER (p)
1300 && DECL_NAME (p)
1301 && !DECL_ARTIFICIAL (p)
1302 && scope != file_scope
1303 && scope != external_scope)
1305 if (!TREE_USED (p))
1306 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1307 else if (DECL_CONTEXT (p) == current_function_decl)
1308 warning_at (DECL_SOURCE_LOCATION (p),
1309 OPT_Wunused_but_set_variable,
1310 "variable %qD set but not used", p);
1313 if (b->inner_comp)
1315 error ("type of array %q+D completed incompatibly with"
1316 " implicit initialization", p);
1319 /* Fall through. */
1320 case TYPE_DECL:
1321 case CONST_DECL:
1322 common_symbol:
1323 /* All of these go in BLOCK_VARS, but only if this is the
1324 binding in the home scope. */
1325 if (!b->nested)
1327 DECL_CHAIN (p) = BLOCK_VARS (block);
1328 BLOCK_VARS (block) = p;
1330 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1332 /* For block local externs add a special
1333 DECL_EXTERNAL decl for debug info generation. */
1334 tree extp = copy_node (p);
1336 DECL_EXTERNAL (extp) = 1;
1337 TREE_STATIC (extp) = 0;
1338 TREE_PUBLIC (extp) = 1;
1339 DECL_INITIAL (extp) = NULL_TREE;
1340 DECL_LANG_SPECIFIC (extp) = NULL;
1341 DECL_CONTEXT (extp) = current_function_decl;
1342 if (TREE_CODE (p) == FUNCTION_DECL)
1344 DECL_RESULT (extp) = NULL_TREE;
1345 DECL_SAVED_TREE (extp) = NULL_TREE;
1346 DECL_STRUCT_FUNCTION (extp) = NULL;
1348 if (b->locus != UNKNOWN_LOCATION)
1349 DECL_SOURCE_LOCATION (extp) = b->locus;
1350 DECL_CHAIN (extp) = BLOCK_VARS (block);
1351 BLOCK_VARS (block) = extp;
1353 /* If this is the file scope set DECL_CONTEXT of each decl to
1354 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1355 work. */
1356 if (scope == file_scope)
1358 DECL_CONTEXT (p) = context;
1359 if (TREE_CODE (p) == TYPE_DECL
1360 && TREE_TYPE (p) != error_mark_node)
1361 set_type_context (TREE_TYPE (p), context);
1364 gcc_fallthrough ();
1365 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1366 already been put there by store_parm_decls. Unused-
1367 parameter warnings are handled by function.c.
1368 error_mark_node obviously does not go in BLOCK_VARS and
1369 does not get unused-variable warnings. */
1370 case PARM_DECL:
1371 case ERROR_MARK:
1372 /* It is possible for a decl not to have a name. We get
1373 here with b->id NULL in this case. */
1374 if (b->id)
1376 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1377 I_SYMBOL_BINDING (b->id) = b->shadowed;
1378 if (b->shadowed && b->shadowed->u.type)
1379 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1381 break;
1383 default:
1384 gcc_unreachable ();
1389 /* Dispose of the block that we just made inside some higher level. */
1390 if ((scope->function_body || scope == file_scope) && context)
1392 DECL_INITIAL (context) = block;
1393 BLOCK_SUPERCONTEXT (block) = context;
1395 else if (scope->outer)
1397 if (block)
1398 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1399 /* If we did not make a block for the scope just exited, any
1400 blocks made for inner scopes must be carried forward so they
1401 will later become subblocks of something else. */
1402 else if (scope->blocks)
1403 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1406 /* Pop the current scope, and free the structure for reuse. */
1407 current_scope = scope->outer;
1408 if (scope->function_body)
1409 current_function_scope = scope->outer_function;
1411 memset (scope, 0, sizeof (struct c_scope));
1412 scope->outer = scope_freelist;
1413 scope_freelist = scope;
1415 return block;
1418 void
1419 push_file_scope (void)
1421 tree decl;
1423 if (file_scope)
1424 return;
1426 push_scope ();
1427 file_scope = current_scope;
1429 start_fname_decls ();
1431 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1432 bind (DECL_NAME (decl), decl, file_scope,
1433 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1436 void
1437 pop_file_scope (void)
1439 /* In case there were missing closebraces, get us back to the global
1440 binding level. */
1441 while (current_scope != file_scope)
1442 pop_scope ();
1444 /* __FUNCTION__ is defined at file scope (""). This
1445 call may not be necessary as my tests indicate it
1446 still works without it. */
1447 finish_fname_decls ();
1449 check_inline_statics ();
1451 /* This is the point to write out a PCH if we're doing that.
1452 In that case we do not want to do anything else. */
1453 if (pch_file)
1455 c_common_write_pch ();
1456 /* Ensure even the callers don't try to finalize the CU. */
1457 flag_syntax_only = 1;
1458 return;
1461 /* Pop off the file scope and close this translation unit. */
1462 pop_scope ();
1463 file_scope = 0;
1465 maybe_apply_pending_pragma_weaks ();
1468 /* Adjust the bindings for the start of a statement expression. */
1470 void
1471 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1473 struct c_scope *scope;
1475 for (scope = current_scope; scope != NULL; scope = scope->outer)
1477 struct c_binding *b;
1479 if (!scope->has_label_bindings)
1480 continue;
1482 for (b = scope->bindings; b != NULL; b = b->prev)
1484 struct c_label_vars *label_vars;
1485 unsigned int ix;
1486 struct c_goto_bindings *g;
1488 if (TREE_CODE (b->decl) != LABEL_DECL)
1489 continue;
1490 label_vars = b->u.label;
1491 ++label_vars->label_bindings.stmt_exprs;
1492 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1493 ++g->goto_bindings.stmt_exprs;
1497 if (switch_bindings != NULL)
1498 ++switch_bindings->stmt_exprs;
1501 /* Adjust the bindings for the end of a statement expression. */
1503 void
1504 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1506 struct c_scope *scope;
1508 for (scope = current_scope; scope != NULL; scope = scope->outer)
1510 struct c_binding *b;
1512 if (!scope->has_label_bindings)
1513 continue;
1515 for (b = scope->bindings; b != NULL; b = b->prev)
1517 struct c_label_vars *label_vars;
1518 unsigned int ix;
1519 struct c_goto_bindings *g;
1521 if (TREE_CODE (b->decl) != LABEL_DECL)
1522 continue;
1523 label_vars = b->u.label;
1524 --label_vars->label_bindings.stmt_exprs;
1525 if (label_vars->label_bindings.stmt_exprs < 0)
1527 label_vars->label_bindings.left_stmt_expr = true;
1528 label_vars->label_bindings.stmt_exprs = 0;
1530 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1532 --g->goto_bindings.stmt_exprs;
1533 if (g->goto_bindings.stmt_exprs < 0)
1535 g->goto_bindings.left_stmt_expr = true;
1536 g->goto_bindings.stmt_exprs = 0;
1542 if (switch_bindings != NULL)
1544 --switch_bindings->stmt_exprs;
1545 gcc_assert (switch_bindings->stmt_exprs >= 0);
1549 /* Push a definition or a declaration of struct, union or enum tag "name".
1550 "type" should be the type node.
1551 We assume that the tag "name" is not already defined, and has a location
1552 of LOC.
1554 Note that the definition may really be just a forward reference.
1555 In that case, the TYPE_SIZE will be zero. */
1557 static void
1558 pushtag (location_t loc, tree name, tree type)
1560 /* Record the identifier as the type's name if it has none. */
1561 if (name && !TYPE_NAME (type))
1562 TYPE_NAME (type) = name;
1563 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1565 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1566 tagged type we just added to the current scope. This fake
1567 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1568 to output a representation of a tagged type, and it also gives
1569 us a convenient place to record the "scope start" address for the
1570 tagged type. */
1572 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1573 TYPE_DECL, NULL_TREE, type));
1575 /* An approximation for now, so we can tell this is a function-scope tag.
1576 This will be updated in pop_scope. */
1577 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1579 if (warn_cxx_compat && name != NULL_TREE)
1581 struct c_binding *b = I_SYMBOL_BINDING (name);
1583 if (b != NULL
1584 && b->decl != NULL_TREE
1585 && TREE_CODE (b->decl) == TYPE_DECL
1586 && (B_IN_CURRENT_SCOPE (b)
1587 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1588 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1589 != TYPE_MAIN_VARIANT (type)))
1591 auto_diagnostic_group d;
1592 if (warning_at (loc, OPT_Wc___compat,
1593 ("using %qD as both a typedef and a tag is "
1594 "invalid in C++"), b->decl)
1595 && b->locus != UNKNOWN_LOCATION)
1596 inform (b->locus, "originally defined here");
1601 /* An exported interface to pushtag. This is used by the gdb plugin's
1602 binding oracle to introduce a new tag binding. */
1604 void
1605 c_pushtag (location_t loc, tree name, tree type)
1607 pushtag (loc, name, type);
1610 /* An exported interface to bind a declaration. LOC is the location
1611 to use. DECL is the declaration to bind. The decl's name is used
1612 to determine how it is bound. If DECL is a VAR_DECL, then
1613 IS_GLOBAL determines whether the decl is put into the global (file
1614 and external) scope or the current function's scope; if DECL is not
1615 a VAR_DECL then it is always put into the file scope. */
1617 void
1618 c_bind (location_t loc, tree decl, bool is_global)
1620 struct c_scope *scope;
1621 bool nested = false;
1623 if (!VAR_P (decl) || current_function_scope == NULL)
1625 /* Types and functions are always considered to be global. */
1626 scope = file_scope;
1627 DECL_EXTERNAL (decl) = 1;
1628 TREE_PUBLIC (decl) = 1;
1630 else if (is_global)
1632 /* Also bind it into the external scope. */
1633 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1634 nested = true;
1635 scope = file_scope;
1636 DECL_EXTERNAL (decl) = 1;
1637 TREE_PUBLIC (decl) = 1;
1639 else
1641 DECL_CONTEXT (decl) = current_function_decl;
1642 TREE_PUBLIC (decl) = 0;
1643 scope = current_function_scope;
1646 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1650 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1651 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1652 to the builtin_structptr_types array. Subsequent declarations of such
1653 built-ins are expected to refer to it rather than to fileptr_type_node,
1654 etc. which is just void* (or to any other type).
1655 Used only by match_builtin_function_types. */
1657 static const unsigned builtin_structptr_type_count
1658 = sizeof builtin_structptr_types / sizeof builtin_structptr_types[0];
1660 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1662 /* Returns true if types T1 and T2 representing return types or types
1663 of function arguments are close enough to be considered interchangeable
1664 in redeclarations of built-in functions. */
1666 static bool
1667 types_close_enough_to_match (tree t1, tree t2)
1669 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1670 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1671 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1674 /* Subroutine of compare_decls. Allow harmless mismatches in return
1675 and argument types provided that the type modes match. Set *STRICT
1676 and *ARGNO to the expected argument type and number in case of
1677 an argument type mismatch or null and zero otherwise. Return
1678 a unified type given a suitable match, and 0 otherwise. */
1680 static tree
1681 match_builtin_function_types (tree newtype, tree oldtype,
1682 tree *strict, unsigned *argno)
1684 *argno = 0;
1685 *strict = NULL_TREE;
1687 /* Accept the return type of the new declaration if it has the same
1688 mode and if they're both pointers or if neither is. */
1689 tree oldrettype = TREE_TYPE (oldtype);
1690 tree newrettype = TREE_TYPE (newtype);
1692 if (!types_close_enough_to_match (oldrettype, newrettype))
1693 return NULL_TREE;
1695 /* Check that the return types are compatible but don't fail if they
1696 are not (e.g., int vs long in ILP32) and just let the caller know. */
1697 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1698 TYPE_MAIN_VARIANT (newrettype)))
1699 *strict = oldrettype;
1701 tree oldargs = TYPE_ARG_TYPES (oldtype);
1702 tree newargs = TYPE_ARG_TYPES (newtype);
1703 tree tryargs = newargs;
1705 const unsigned nlst
1706 = sizeof last_structptr_types / sizeof last_structptr_types[0];
1707 const unsigned nbst
1708 = sizeof builtin_structptr_types / sizeof builtin_structptr_types[0];
1710 gcc_checking_assert (nlst == nbst);
1712 for (unsigned i = 1; oldargs || newargs; ++i)
1714 if (!oldargs
1715 || !newargs
1716 || !TREE_VALUE (oldargs)
1717 || !TREE_VALUE (newargs))
1718 return NULL_TREE;
1720 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1721 tree newtype = TREE_VALUE (newargs);
1722 if (newtype == error_mark_node)
1723 return NULL_TREE;
1724 newtype = TYPE_MAIN_VARIANT (newtype);
1726 if (!types_close_enough_to_match (oldtype, newtype))
1727 return NULL_TREE;
1729 unsigned j = nbst;
1730 if (POINTER_TYPE_P (oldtype))
1731 /* Iterate over well-known struct types like FILE (whose types
1732 aren't known to us) and compare the pointer to each to
1733 the pointer argument. */
1734 for (j = 0; j < nbst; ++j)
1736 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1737 continue;
1738 /* Store the first FILE* etc. argument type (whatever it is), and
1739 expect any subsequent declarations of file I/O etc. built-ins
1740 to refer to it rather than to fileptr_type_node etc. which is
1741 just void* (or const void*). */
1742 if (last_structptr_types[j])
1744 if (!comptypes (last_structptr_types[j], newtype))
1746 *argno = i;
1747 *strict = last_structptr_types[j];
1750 else
1751 last_structptr_types[j] = newtype;
1752 break;
1755 if (j == nbst && !comptypes (oldtype, newtype))
1757 if (POINTER_TYPE_P (oldtype))
1759 /* For incompatible pointers, only reject differences in
1760 the unqualified variants of the referenced types but
1761 consider differences in qualifiers as benign (report
1762 those to caller via *STRICT below). */
1763 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1764 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1765 if (!comptypes (oldref, newref))
1766 return NULL_TREE;
1769 if (!*strict)
1771 *argno = i;
1772 *strict = oldtype;
1776 oldargs = TREE_CHAIN (oldargs);
1777 newargs = TREE_CHAIN (newargs);
1780 tree trytype = build_function_type (newrettype, tryargs);
1782 /* Allow declaration to change transaction_safe attribute. */
1783 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1784 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1785 tree newattrs = TYPE_ATTRIBUTES (newtype);
1786 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1787 if (oldtsafe && !newtsafe)
1788 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1789 else if (newtsafe && !oldtsafe)
1790 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1791 NULL_TREE, oldattrs);
1793 return build_type_attribute_variant (trytype, oldattrs);
1796 /* Subroutine of diagnose_mismatched_decls. Check for function type
1797 mismatch involving an empty arglist vs a nonempty one and give clearer
1798 diagnostics. */
1799 static void
1800 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1801 tree newtype, tree oldtype)
1803 tree t;
1805 if (TREE_CODE (olddecl) != FUNCTION_DECL
1806 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1807 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1808 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1809 return;
1811 t = TYPE_ARG_TYPES (oldtype);
1812 if (t == NULL_TREE)
1813 t = TYPE_ARG_TYPES (newtype);
1814 for (; t; t = TREE_CHAIN (t))
1816 tree type = TREE_VALUE (t);
1818 if (TREE_CHAIN (t) == NULL_TREE
1819 && TYPE_MAIN_VARIANT (type) != void_type_node)
1821 inform (input_location, "a parameter list with an ellipsis "
1822 "cannot match an empty parameter name list declaration");
1823 break;
1826 if (c_type_promotes_to (type) != type)
1828 inform (input_location, "an argument type that has a default "
1829 "promotion cannot match an empty parameter name list "
1830 "declaration");
1831 break;
1836 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1837 old-style function definition, NEWDECL is a prototype declaration.
1838 Diagnose inconsistencies in the argument list. Returns TRUE if
1839 the prototype is compatible, FALSE if not. */
1840 static bool
1841 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1843 tree newargs, oldargs;
1844 int i;
1846 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1848 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1849 newargs = TYPE_ARG_TYPES (newtype);
1850 i = 1;
1852 for (;;)
1854 tree oldargtype = TREE_VALUE (oldargs);
1855 tree newargtype = TREE_VALUE (newargs);
1857 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1858 return false;
1860 oldargtype = (TYPE_ATOMIC (oldargtype)
1861 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1862 TYPE_QUAL_ATOMIC)
1863 : TYPE_MAIN_VARIANT (oldargtype));
1864 newargtype = (TYPE_ATOMIC (newargtype)
1865 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1866 TYPE_QUAL_ATOMIC)
1867 : TYPE_MAIN_VARIANT (newargtype));
1869 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1870 break;
1872 /* Reaching the end of just one list means the two decls don't
1873 agree on the number of arguments. */
1874 if (END_OF_ARGLIST (oldargtype))
1876 error ("prototype for %q+D declares more arguments "
1877 "than previous old-style definition", newdecl);
1878 return false;
1880 else if (END_OF_ARGLIST (newargtype))
1882 error ("prototype for %q+D declares fewer arguments "
1883 "than previous old-style definition", newdecl);
1884 return false;
1887 /* Type for passing arg must be consistent with that declared
1888 for the arg. */
1889 else if (!comptypes (oldargtype, newargtype))
1891 error ("prototype for %q+D declares argument %d"
1892 " with incompatible type",
1893 newdecl, i);
1894 return false;
1897 oldargs = TREE_CHAIN (oldargs);
1898 newargs = TREE_CHAIN (newargs);
1899 i++;
1902 /* If we get here, no errors were found, but do issue a warning
1903 for this poor-style construct. */
1904 warning (0, "prototype for %q+D follows non-prototype definition",
1905 newdecl);
1906 return true;
1907 #undef END_OF_ARGLIST
1910 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1911 first in a pair of mismatched declarations, using the diagnostic
1912 function DIAG. */
1913 static void
1914 locate_old_decl (tree decl)
1916 if (TREE_CODE (decl) == FUNCTION_DECL
1917 && fndecl_built_in_p (decl)
1918 && !C_DECL_DECLARED_BUILTIN (decl))
1920 else if (DECL_INITIAL (decl))
1921 inform (input_location,
1922 "previous definition of %q+D with type %qT",
1923 decl, TREE_TYPE (decl));
1924 else if (C_DECL_IMPLICIT (decl))
1925 inform (input_location,
1926 "previous implicit declaration of %q+D with type %qT",
1927 decl, TREE_TYPE (decl));
1928 else
1929 inform (input_location,
1930 "previous declaration of %q+D with type %qT",
1931 decl, TREE_TYPE (decl));
1934 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1935 Returns true if the caller should proceed to merge the two, false
1936 if OLDDECL should simply be discarded. As a side effect, issues
1937 all necessary diagnostics for invalid or poor-style combinations.
1938 If it returns true, writes the types of NEWDECL and OLDDECL to
1939 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1940 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1942 static bool
1943 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1944 tree *newtypep, tree *oldtypep)
1946 tree newtype, oldtype;
1947 bool retval = true;
1949 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1950 && DECL_EXTERNAL (DECL))
1952 /* If we have error_mark_node for either decl or type, just discard
1953 the previous decl - we're in an error cascade already. */
1954 if (olddecl == error_mark_node || newdecl == error_mark_node)
1955 return false;
1956 *oldtypep = oldtype = TREE_TYPE (olddecl);
1957 *newtypep = newtype = TREE_TYPE (newdecl);
1958 if (oldtype == error_mark_node || newtype == error_mark_node)
1959 return false;
1961 /* Two different categories of symbol altogether. This is an error
1962 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1963 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1965 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1966 && fndecl_built_in_p (olddecl)
1967 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1969 auto_diagnostic_group d;
1970 error ("%q+D redeclared as different kind of symbol", newdecl);
1971 locate_old_decl (olddecl);
1973 else if (TREE_PUBLIC (newdecl))
1974 warning (OPT_Wbuiltin_declaration_mismatch,
1975 "built-in function %q+D declared as non-function",
1976 newdecl);
1977 else
1978 warning (OPT_Wshadow, "declaration of %q+D shadows "
1979 "a built-in function", newdecl);
1980 return false;
1983 /* Enumerators have no linkage, so may only be declared once in a
1984 given scope. */
1985 if (TREE_CODE (olddecl) == CONST_DECL)
1987 auto_diagnostic_group d;
1988 error ("redeclaration of enumerator %q+D", newdecl);
1989 locate_old_decl (olddecl);
1990 return false;
1993 bool pedwarned = false;
1994 bool warned = false;
1995 auto_diagnostic_group d;
1997 if (!comptypes (oldtype, newtype))
1999 if (TREE_CODE (olddecl) == FUNCTION_DECL
2000 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2001 && !C_DECL_DECLARED_BUILTIN (olddecl))
2003 /* Accept "harmless" mismatches in function types such
2004 as missing qualifiers or int vs long when they're the same
2005 size. However, diagnose return and argument types that are
2006 incompatible according to language rules. */
2007 tree mismatch_expect;
2008 unsigned mismatch_argno;
2010 tree trytype = match_builtin_function_types (newtype, oldtype,
2011 &mismatch_expect,
2012 &mismatch_argno);
2014 if (trytype && comptypes (newtype, trytype))
2015 *oldtypep = oldtype = trytype;
2016 else
2018 /* If types don't match for a built-in, throw away the
2019 built-in. No point in calling locate_old_decl here, it
2020 won't print anything. */
2021 const char *header = header_for_builtin_fn (olddecl);
2022 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2023 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2024 "conflicting types for built-in function %q+D; "
2025 "expected %qT",
2026 newdecl, oldtype)
2027 && header)
2029 /* Suggest the right header to include as the preferred
2030 solution rather than the spelling of the declaration. */
2031 rich_location richloc (line_table, loc);
2032 maybe_add_include_fixit (&richloc, header, true);
2033 inform (&richloc,
2034 "%qD is declared in header %qs", olddecl, header);
2036 return false;
2039 if (mismatch_expect && extra_warnings)
2041 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2042 bool warned = false;
2043 if (mismatch_argno)
2044 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2045 "mismatch in argument %u type of built-in "
2046 "function %qD; expected %qT",
2047 mismatch_argno, newdecl, mismatch_expect);
2048 else
2049 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2050 "mismatch in return type of built-in "
2051 "function %qD; expected %qT",
2052 newdecl, mismatch_expect);
2053 const char *header = header_for_builtin_fn (olddecl);
2054 if (warned && header)
2056 rich_location richloc (line_table, newloc);
2057 maybe_add_include_fixit (&richloc, header, true);
2058 inform (&richloc,
2059 "%qD is declared in header %qs", olddecl, header);
2063 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2064 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2066 /* A conflicting function declaration for a predeclared
2067 function that isn't actually built in. Objective C uses
2068 these. The new declaration silently overrides everything
2069 but the volatility (i.e. noreturn) indication. See also
2070 below. FIXME: Make Objective C use normal builtins. */
2071 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2072 return false;
2074 /* Permit void foo (...) to match int foo (...) if the latter is
2075 the definition and implicit int was used. See
2076 c-torture/compile/920625-2.c. */
2077 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2078 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2079 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2080 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2082 pedwarned = pedwarn (input_location, 0,
2083 "conflicting types for %q+D", newdecl);
2084 /* Make sure we keep void as the return type. */
2085 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2086 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2088 /* Permit void foo (...) to match an earlier call to foo (...) with
2089 no declared type (thus, implicitly int). */
2090 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2091 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2092 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2093 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2095 pedwarned = pedwarn (input_location, 0,
2096 "conflicting types for %q+D; have %qT",
2097 newdecl, newtype);
2098 /* Make sure we keep void as the return type. */
2099 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2101 else
2103 int new_quals = TYPE_QUALS (newtype);
2104 int old_quals = TYPE_QUALS (oldtype);
2106 if (new_quals != old_quals)
2108 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2109 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2110 if (new_addr != old_addr)
2112 if (ADDR_SPACE_GENERIC_P (new_addr))
2113 error ("conflicting named address spaces (generic vs %s) "
2114 "for %q+D",
2115 c_addr_space_name (old_addr), newdecl);
2116 else if (ADDR_SPACE_GENERIC_P (old_addr))
2117 error ("conflicting named address spaces (%s vs generic) "
2118 "for %q+D",
2119 c_addr_space_name (new_addr), newdecl);
2120 else
2121 error ("conflicting named address spaces (%s vs %s) "
2122 "for %q+D",
2123 c_addr_space_name (new_addr),
2124 c_addr_space_name (old_addr),
2125 newdecl);
2128 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2129 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2130 error ("conflicting type qualifiers for %q+D", newdecl);
2132 else
2133 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2134 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2135 locate_old_decl (olddecl);
2136 return false;
2140 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2141 but silently ignore the redeclaration if either is in a system
2142 header. (Conflicting redeclarations were handled above.) This
2143 is allowed for C11 if the types are the same, not just
2144 compatible. */
2145 if (TREE_CODE (newdecl) == TYPE_DECL)
2147 bool types_different = false;
2148 int comptypes_result;
2150 comptypes_result
2151 = comptypes_check_different_types (oldtype, newtype, &types_different);
2153 if (comptypes_result != 1 || types_different)
2155 error ("redefinition of typedef %q+D with different type", newdecl);
2156 locate_old_decl (olddecl);
2157 return false;
2160 if (DECL_IN_SYSTEM_HEADER (newdecl)
2161 || DECL_IN_SYSTEM_HEADER (olddecl)
2162 || TREE_NO_WARNING (newdecl)
2163 || TREE_NO_WARNING (olddecl))
2164 return true; /* Allow OLDDECL to continue in use. */
2166 if (variably_modified_type_p (newtype, NULL))
2168 error ("redefinition of typedef %q+D with variably modified type",
2169 newdecl);
2170 locate_old_decl (olddecl);
2172 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2173 "redefinition of typedef %q+D", newdecl))
2174 locate_old_decl (olddecl);
2176 return true;
2179 /* Function declarations can either be 'static' or 'extern' (no
2180 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2181 can never conflict with each other on account of linkage
2182 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2183 gnu89 mode permits two definitions if one is 'extern inline' and
2184 one is not. The non- extern-inline definition supersedes the
2185 extern-inline definition. */
2187 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2189 /* If you declare a built-in function name as static, or
2190 define the built-in with an old-style definition (so we
2191 can't validate the argument list) the built-in definition is
2192 overridden, but optionally warn this was a bad choice of name. */
2193 if (fndecl_built_in_p (olddecl)
2194 && !C_DECL_DECLARED_BUILTIN (olddecl))
2196 if (!TREE_PUBLIC (newdecl)
2197 || (DECL_INITIAL (newdecl)
2198 && !prototype_p (TREE_TYPE (newdecl))))
2200 warning_at (DECL_SOURCE_LOCATION (newdecl),
2201 OPT_Wshadow, "declaration of %qD shadows "
2202 "a built-in function", newdecl);
2203 /* Discard the old built-in function. */
2204 return false;
2207 if (!prototype_p (TREE_TYPE (newdecl)))
2209 /* Set for built-ins that take no arguments. */
2210 bool func_void_args = false;
2211 if (tree at = TYPE_ARG_TYPES (oldtype))
2212 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2214 if (extra_warnings && !func_void_args)
2215 warning_at (DECL_SOURCE_LOCATION (newdecl),
2216 OPT_Wbuiltin_declaration_mismatch,
2217 "declaration of built-in function %qD without "
2218 "a prototype; expected %qT",
2219 newdecl, TREE_TYPE (olddecl));
2223 if (DECL_INITIAL (newdecl))
2225 if (DECL_INITIAL (olddecl))
2227 /* If both decls are in the same TU and the new declaration
2228 isn't overriding an extern inline reject the new decl.
2229 In c99, no overriding is allowed in the same translation
2230 unit. */
2231 if ((!DECL_EXTERN_INLINE (olddecl)
2232 || DECL_EXTERN_INLINE (newdecl)
2233 || (!flag_gnu89_inline
2234 && (!DECL_DECLARED_INLINE_P (olddecl)
2235 || !lookup_attribute ("gnu_inline",
2236 DECL_ATTRIBUTES (olddecl)))
2237 && (!DECL_DECLARED_INLINE_P (newdecl)
2238 || !lookup_attribute ("gnu_inline",
2239 DECL_ATTRIBUTES (newdecl))))
2241 && same_translation_unit_p (newdecl, olddecl))
2243 auto_diagnostic_group d;
2244 error ("redefinition of %q+D", newdecl);
2245 locate_old_decl (olddecl);
2246 return false;
2250 /* If we have a prototype after an old-style function definition,
2251 the argument types must be checked specially. */
2252 else if (DECL_INITIAL (olddecl)
2253 && !prototype_p (oldtype) && prototype_p (newtype)
2254 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2256 auto_diagnostic_group d;
2257 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2259 locate_old_decl (olddecl);
2260 return false;
2263 /* A non-static declaration (even an "extern") followed by a
2264 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2265 The same is true for a static forward declaration at block
2266 scope followed by a non-static declaration/definition at file
2267 scope. Static followed by non-static at the same scope is
2268 not undefined behavior, and is the most convenient way to get
2269 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2270 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2271 we do diagnose it if -Wtraditional. */
2272 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2274 /* Two exceptions to the rule. If olddecl is an extern
2275 inline, or a predeclared function that isn't actually
2276 built in, newdecl silently overrides olddecl. The latter
2277 occur only in Objective C; see also above. (FIXME: Make
2278 Objective C use normal builtins.) */
2279 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2280 && !DECL_EXTERN_INLINE (olddecl))
2282 auto_diagnostic_group d;
2283 error ("static declaration of %q+D follows "
2284 "non-static declaration", newdecl);
2285 locate_old_decl (olddecl);
2287 return false;
2289 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2291 if (DECL_CONTEXT (olddecl))
2293 auto_diagnostic_group d;
2294 error ("non-static declaration of %q+D follows "
2295 "static declaration", newdecl);
2296 locate_old_decl (olddecl);
2297 return false;
2299 else if (warn_traditional)
2301 warned |= warning (OPT_Wtraditional,
2302 "non-static declaration of %q+D "
2303 "follows static declaration", newdecl);
2307 /* Make sure gnu_inline attribute is either not present, or
2308 present on all inline decls. */
2309 if (DECL_DECLARED_INLINE_P (olddecl)
2310 && DECL_DECLARED_INLINE_P (newdecl))
2312 bool newa = lookup_attribute ("gnu_inline",
2313 DECL_ATTRIBUTES (newdecl)) != NULL;
2314 bool olda = lookup_attribute ("gnu_inline",
2315 DECL_ATTRIBUTES (olddecl)) != NULL;
2316 if (newa != olda)
2318 auto_diagnostic_group d;
2319 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2320 newa ? newdecl : olddecl);
2321 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2322 "but not here");
2326 else if (VAR_P (newdecl))
2328 /* Only variables can be thread-local, and all declarations must
2329 agree on this property. */
2330 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2332 /* Nothing to check. Since OLDDECL is marked threadprivate
2333 and NEWDECL does not have a thread-local attribute, we
2334 will merge the threadprivate attribute into NEWDECL. */
2337 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2339 auto_diagnostic_group d;
2340 if (DECL_THREAD_LOCAL_P (newdecl))
2341 error ("thread-local declaration of %q+D follows "
2342 "non-thread-local declaration", newdecl);
2343 else
2344 error ("non-thread-local declaration of %q+D follows "
2345 "thread-local declaration", newdecl);
2347 locate_old_decl (olddecl);
2348 return false;
2351 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2352 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2354 auto_diagnostic_group d;
2355 error ("redefinition of %q+D", newdecl);
2356 locate_old_decl (olddecl);
2357 return false;
2360 /* Objects declared at file scope: if the first declaration had
2361 external linkage (even if it was an external reference) the
2362 second must have external linkage as well, or the behavior is
2363 undefined. If the first declaration had internal linkage, then
2364 the second must too, or else be an external reference (in which
2365 case the composite declaration still has internal linkage).
2366 As for function declarations, we warn about the static-then-
2367 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2368 if (DECL_FILE_SCOPE_P (newdecl)
2369 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2371 if (DECL_EXTERNAL (newdecl))
2373 if (!DECL_FILE_SCOPE_P (olddecl))
2375 auto_diagnostic_group d;
2376 error ("extern declaration of %q+D follows "
2377 "declaration with no linkage", newdecl);
2378 locate_old_decl (olddecl);
2379 return false;
2381 else if (warn_traditional)
2383 warned |= warning (OPT_Wtraditional,
2384 "non-static declaration of %q+D "
2385 "follows static declaration", newdecl);
2388 else
2390 auto_diagnostic_group d;
2391 if (TREE_PUBLIC (newdecl))
2392 error ("non-static declaration of %q+D follows "
2393 "static declaration", newdecl);
2394 else
2395 error ("static declaration of %q+D follows "
2396 "non-static declaration", newdecl);
2398 locate_old_decl (olddecl);
2399 return false;
2402 /* Two objects with the same name declared at the same block
2403 scope must both be external references (6.7p3). */
2404 else if (!DECL_FILE_SCOPE_P (newdecl))
2406 if (DECL_EXTERNAL (newdecl))
2408 /* Extern with initializer at block scope, which will
2409 already have received an error. */
2411 else if (DECL_EXTERNAL (olddecl))
2413 auto_diagnostic_group d;
2414 error ("declaration of %q+D with no linkage follows "
2415 "extern declaration", newdecl);
2416 locate_old_decl (olddecl);
2418 else
2420 auto_diagnostic_group d;
2421 error ("redeclaration of %q+D with no linkage", newdecl);
2422 locate_old_decl (olddecl);
2425 return false;
2428 /* C++ does not permit a decl to appear multiple times at file
2429 scope. */
2430 if (warn_cxx_compat
2431 && DECL_FILE_SCOPE_P (newdecl)
2432 && !DECL_EXTERNAL (newdecl)
2433 && !DECL_EXTERNAL (olddecl))
2434 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2435 OPT_Wc___compat,
2436 ("duplicate declaration of %qD is "
2437 "invalid in C++"),
2438 newdecl);
2441 /* warnings */
2442 /* All decls must agree on a visibility. */
2443 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2444 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2445 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2447 warned |= warning (0, "redeclaration of %q+D with different visibility "
2448 "(old visibility preserved)", newdecl);
2451 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2452 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2453 else /* PARM_DECL, VAR_DECL */
2455 /* Redeclaration of a parameter is a constraint violation (this is
2456 not explicitly stated, but follows from C99 6.7p3 [no more than
2457 one declaration of the same identifier with no linkage in the
2458 same scope, except type tags] and 6.2.2p6 [parameters have no
2459 linkage]). We must check for a forward parameter declaration,
2460 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2461 an extension, the mandatory diagnostic for which is handled by
2462 mark_forward_parm_decls. */
2464 if (TREE_CODE (newdecl) == PARM_DECL
2465 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2467 auto_diagnostic_group d;
2468 error ("redefinition of parameter %q+D", newdecl);
2469 locate_old_decl (olddecl);
2470 return false;
2474 /* Optional warning for completely redundant decls. */
2475 if (!warned && !pedwarned
2476 && warn_redundant_decls
2477 /* Don't warn about a function declaration followed by a
2478 definition. */
2479 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2480 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2481 /* Don't warn about redundant redeclarations of builtins. */
2482 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2483 && !fndecl_built_in_p (newdecl)
2484 && fndecl_built_in_p (olddecl)
2485 && !C_DECL_DECLARED_BUILTIN (olddecl))
2486 /* Don't warn about an extern followed by a definition. */
2487 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2488 /* Don't warn about forward parameter decls. */
2489 && !(TREE_CODE (newdecl) == PARM_DECL
2490 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2491 /* Don't warn about a variable definition following a declaration. */
2492 && !(VAR_P (newdecl)
2493 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2495 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2496 newdecl);
2499 /* Report location of previous decl/defn. */
2500 if (warned || pedwarned)
2501 locate_old_decl (olddecl);
2503 #undef DECL_EXTERN_INLINE
2505 return retval;
2508 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2509 consistent with OLDDECL, but carries new information. Merge the
2510 new information into OLDDECL. This function issues no
2511 diagnostics. */
2513 static void
2514 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2516 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2517 && DECL_INITIAL (newdecl) != NULL_TREE);
2518 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2519 && prototype_p (TREE_TYPE (newdecl)));
2520 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2521 && prototype_p (TREE_TYPE (olddecl)));
2523 /* For real parm decl following a forward decl, rechain the old decl
2524 in its new location and clear TREE_ASM_WRITTEN (it's not a
2525 forward decl anymore). */
2526 if (TREE_CODE (newdecl) == PARM_DECL
2527 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2529 struct c_binding *b, **here;
2531 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2532 if ((*here)->decl == olddecl)
2533 goto found;
2534 gcc_unreachable ();
2536 found:
2537 b = *here;
2538 *here = b->prev;
2539 b->prev = current_scope->bindings;
2540 current_scope->bindings = b;
2542 TREE_ASM_WRITTEN (olddecl) = 0;
2545 DECL_ATTRIBUTES (newdecl)
2546 = targetm.merge_decl_attributes (olddecl, newdecl);
2548 /* For typedefs use the old type, as the new type's DECL_NAME points
2549 at newdecl, which will be ggc_freed. */
2550 if (TREE_CODE (newdecl) == TYPE_DECL)
2552 /* But NEWTYPE might have an attribute, honor that. */
2553 tree tem = newtype;
2554 newtype = oldtype;
2556 if (TYPE_USER_ALIGN (tem))
2558 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2559 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2560 TYPE_USER_ALIGN (newtype) = true;
2563 /* And remove the new type from the variants list. */
2564 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2566 tree remove = TREE_TYPE (newdecl);
2567 if (TYPE_MAIN_VARIANT (remove) == remove)
2569 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2570 /* If remove is the main variant, no need to remove that
2571 from the list. One of the DECL_ORIGINAL_TYPE
2572 variants, e.g. created for aligned attribute, might still
2573 refer to the newdecl TYPE_DECL though, so remove that one
2574 in that case. */
2575 if (DECL_ORIGINAL_TYPE (newdecl)
2576 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2577 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2578 t; t = TYPE_MAIN_VARIANT (t))
2579 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2581 TYPE_NEXT_VARIANT (t)
2582 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2583 break;
2586 else
2587 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2588 t = TYPE_NEXT_VARIANT (t))
2589 if (TYPE_NEXT_VARIANT (t) == remove)
2591 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2592 break;
2597 /* Merge the data types specified in the two decls. */
2598 TREE_TYPE (newdecl)
2599 = TREE_TYPE (olddecl)
2600 = composite_type (newtype, oldtype);
2602 /* Lay the type out, unless already done. */
2603 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2605 if (TREE_TYPE (newdecl) != error_mark_node)
2606 layout_type (TREE_TYPE (newdecl));
2607 if (TREE_CODE (newdecl) != FUNCTION_DECL
2608 && TREE_CODE (newdecl) != TYPE_DECL
2609 && TREE_CODE (newdecl) != CONST_DECL)
2610 layout_decl (newdecl, 0);
2612 else
2614 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2615 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2616 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2617 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2618 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2620 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2621 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2623 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2624 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2625 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2626 DECL_WARN_IF_NOT_ALIGN (olddecl));
2629 /* Keep the old rtl since we can safely use it. */
2630 if (HAS_RTL_P (olddecl))
2631 COPY_DECL_RTL (olddecl, newdecl);
2633 /* Merge the type qualifiers. */
2634 if (TREE_READONLY (newdecl))
2635 TREE_READONLY (olddecl) = 1;
2637 if (TREE_THIS_VOLATILE (newdecl))
2638 TREE_THIS_VOLATILE (olddecl) = 1;
2640 /* Merge deprecatedness. */
2641 if (TREE_DEPRECATED (newdecl))
2642 TREE_DEPRECATED (olddecl) = 1;
2644 /* If a decl is in a system header and the other isn't, keep the one on the
2645 system header. Otherwise, keep source location of definition rather than
2646 declaration and of prototype rather than non-prototype unless that
2647 prototype is built-in. */
2648 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2649 && DECL_IN_SYSTEM_HEADER (olddecl)
2650 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2651 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2652 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2653 && DECL_IN_SYSTEM_HEADER (newdecl)
2654 && !DECL_IN_SYSTEM_HEADER (olddecl))
2655 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2656 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2657 && DECL_INITIAL (olddecl) != NULL_TREE)
2658 || (old_is_prototype && !new_is_prototype
2659 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2660 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2662 /* Merge the initialization information. */
2663 if (DECL_INITIAL (newdecl) == NULL_TREE)
2664 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2666 /* Merge the threadprivate attribute. */
2667 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2668 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2670 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2672 /* Copy the assembler name.
2673 Currently, it can only be defined in the prototype. */
2674 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2676 /* Use visibility of whichever declaration had it specified */
2677 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2679 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2680 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2683 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2685 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2686 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2687 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2688 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2689 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2690 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2691 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2692 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2693 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2694 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2695 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2696 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2697 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2698 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2701 /* Merge the storage class information. */
2702 merge_weak (newdecl, olddecl);
2704 /* For functions, static overrides non-static. */
2705 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2707 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2708 /* This is since we don't automatically
2709 copy the attributes of NEWDECL into OLDDECL. */
2710 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2711 /* If this clears `static', clear it in the identifier too. */
2712 if (!TREE_PUBLIC (olddecl))
2713 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2717 /* In c99, 'extern' declaration before (or after) 'inline' means this
2718 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2719 is present. */
2720 if (TREE_CODE (newdecl) == FUNCTION_DECL
2721 && !flag_gnu89_inline
2722 && (DECL_DECLARED_INLINE_P (newdecl)
2723 || DECL_DECLARED_INLINE_P (olddecl))
2724 && (!DECL_DECLARED_INLINE_P (newdecl)
2725 || !DECL_DECLARED_INLINE_P (olddecl)
2726 || !DECL_EXTERNAL (olddecl))
2727 && DECL_EXTERNAL (newdecl)
2728 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2729 && !current_function_decl)
2730 DECL_EXTERNAL (newdecl) = 0;
2732 /* An inline definition following a static declaration is not
2733 DECL_EXTERNAL. */
2734 if (new_is_definition
2735 && (DECL_DECLARED_INLINE_P (newdecl)
2736 || DECL_DECLARED_INLINE_P (olddecl))
2737 && !TREE_PUBLIC (olddecl))
2738 DECL_EXTERNAL (newdecl) = 0;
2740 if (DECL_EXTERNAL (newdecl))
2742 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2743 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2745 /* An extern decl does not override previous storage class. */
2746 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2747 if (!DECL_EXTERNAL (newdecl))
2749 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2750 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2753 else
2755 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2756 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2759 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2761 /* If we're redefining a function previously defined as extern
2762 inline, make sure we emit debug info for the inline before we
2763 throw it away, in case it was inlined into a function that
2764 hasn't been written out yet. */
2765 if (new_is_definition && DECL_INITIAL (olddecl))
2766 /* The new defn must not be inline. */
2767 DECL_UNINLINABLE (newdecl) = 1;
2768 else
2770 /* If either decl says `inline', this fn is inline, unless
2771 its definition was passed already. */
2772 if (DECL_DECLARED_INLINE_P (newdecl)
2773 || DECL_DECLARED_INLINE_P (olddecl))
2774 DECL_DECLARED_INLINE_P (newdecl) = 1;
2776 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2777 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2779 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2780 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2781 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2782 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2785 if (fndecl_built_in_p (olddecl))
2787 /* If redeclaring a builtin function, it stays built in.
2788 But it gets tagged as having been declared. */
2789 copy_decl_built_in_function (newdecl, olddecl);
2790 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2791 if (new_is_prototype)
2793 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2794 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2796 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2797 switch (fncode)
2799 /* If a compatible prototype of these builtin functions
2800 is seen, assume the runtime implements it with the
2801 expected semantics. */
2802 case BUILT_IN_STPCPY:
2803 if (builtin_decl_explicit_p (fncode))
2804 set_builtin_decl_implicit_p (fncode, true);
2805 break;
2806 default:
2807 if (builtin_decl_explicit_p (fncode))
2808 set_builtin_decl_declared_p (fncode, true);
2809 break;
2812 copy_attributes_to_builtin (newdecl);
2815 else
2816 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2817 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2820 /* Preserve function specific target and optimization options */
2821 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2822 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2823 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2824 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2826 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2827 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2828 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2829 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2831 /* Also preserve various other info from the definition. */
2832 if (!new_is_definition)
2834 tree t;
2835 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2836 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2837 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2838 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2839 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2840 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2841 DECL_CONTEXT (t) = newdecl;
2843 /* See if we've got a function to instantiate from. */
2844 if (DECL_SAVED_TREE (olddecl))
2845 DECL_ABSTRACT_ORIGIN (newdecl)
2846 = DECL_ABSTRACT_ORIGIN (olddecl);
2850 /* Merge the USED information. */
2851 if (TREE_USED (olddecl))
2852 TREE_USED (newdecl) = 1;
2853 else if (TREE_USED (newdecl))
2854 TREE_USED (olddecl) = 1;
2855 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2856 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2857 if (DECL_PRESERVE_P (olddecl))
2858 DECL_PRESERVE_P (newdecl) = 1;
2859 else if (DECL_PRESERVE_P (newdecl))
2860 DECL_PRESERVE_P (olddecl) = 1;
2862 /* Merge DECL_COMMON */
2863 if (VAR_P (olddecl) && VAR_P (newdecl)
2864 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2865 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2866 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2868 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2869 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2870 DECL_ARGUMENTS (if appropriate). */
2872 unsigned olddecl_uid = DECL_UID (olddecl);
2873 tree olddecl_context = DECL_CONTEXT (olddecl);
2874 tree olddecl_arguments = NULL;
2875 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2876 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2878 memcpy ((char *) olddecl + sizeof (struct tree_common),
2879 (char *) newdecl + sizeof (struct tree_common),
2880 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2881 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2882 switch (TREE_CODE (olddecl))
2884 case FUNCTION_DECL:
2885 case VAR_DECL:
2887 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2889 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2890 (char *) newdecl + sizeof (struct tree_decl_common),
2891 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2892 olddecl->decl_with_vis.symtab_node = snode;
2894 if ((DECL_EXTERNAL (olddecl)
2895 || TREE_PUBLIC (olddecl)
2896 || TREE_STATIC (olddecl))
2897 && DECL_SECTION_NAME (newdecl) != NULL)
2898 set_decl_section_name (olddecl, newdecl);
2900 /* This isn't quite correct for something like
2901 int __thread x attribute ((tls_model ("local-exec")));
2902 extern int __thread x;
2903 as we'll lose the "local-exec" model. */
2904 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2905 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2906 break;
2909 case FIELD_DECL:
2910 case PARM_DECL:
2911 case LABEL_DECL:
2912 case RESULT_DECL:
2913 case CONST_DECL:
2914 case TYPE_DECL:
2915 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2916 (char *) newdecl + sizeof (struct tree_decl_common),
2917 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2918 break;
2920 default:
2922 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2923 (char *) newdecl + sizeof (struct tree_decl_common),
2924 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2926 DECL_UID (olddecl) = olddecl_uid;
2927 DECL_CONTEXT (olddecl) = olddecl_context;
2928 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2929 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2932 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2933 so that encode_section_info has a chance to look at the new decl
2934 flags and attributes. */
2935 if (DECL_RTL_SET_P (olddecl)
2936 && (TREE_CODE (olddecl) == FUNCTION_DECL
2937 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2938 make_decl_rtl (olddecl);
2941 /* Handle when a new declaration NEWDECL has the same name as an old
2942 one OLDDECL in the same binding contour. Prints an error message
2943 if appropriate.
2945 If safely possible, alter OLDDECL to look like NEWDECL, and return
2946 true. Otherwise, return false. */
2948 static bool
2949 duplicate_decls (tree newdecl, tree olddecl)
2951 tree newtype = NULL, oldtype = NULL;
2953 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2955 /* Avoid `unused variable' and other warnings for OLDDECL. */
2956 TREE_NO_WARNING (olddecl) = 1;
2957 return false;
2960 merge_decls (newdecl, olddecl, newtype, oldtype);
2962 /* The NEWDECL will no longer be needed.
2964 Before releasing the node, be sure to remove function from symbol
2965 table that might have been inserted there to record comdat group.
2966 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2967 structure is shared in between NEWDECL and OLDECL. */
2968 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2969 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2970 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2972 struct symtab_node *snode = symtab_node::get (newdecl);
2973 if (snode)
2974 snode->remove ();
2976 ggc_free (newdecl);
2977 return true;
2981 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2982 static void
2983 warn_if_shadowing (tree new_decl)
2985 struct c_binding *b;
2987 /* Shadow warnings wanted? */
2988 if (!(warn_shadow
2989 || warn_shadow_local
2990 || warn_shadow_compatible_local)
2991 /* No shadow warnings for internally generated vars. */
2992 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
2993 return;
2995 /* Is anything being shadowed? Invisible decls do not count. */
2996 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2997 if (b->decl && b->decl != new_decl && !b->invisible
2998 && (b->decl == error_mark_node
2999 || diagnostic_report_warnings_p (global_dc,
3000 DECL_SOURCE_LOCATION (b->decl))))
3002 tree old_decl = b->decl;
3004 if (old_decl == error_mark_node)
3006 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3007 "non-variable", new_decl);
3008 break;
3011 bool warned = false;
3012 auto_diagnostic_group d;
3013 if (TREE_CODE (old_decl) == PARM_DECL)
3015 enum opt_code warning_code;
3017 /* If '-Wshadow=compatible-local' is specified without other
3018 -Wshadow= flags, we will warn only when the types of the
3019 shadowing variable (i.e. new_decl) and the shadowed variable
3020 (old_decl) are compatible. */
3021 if (warn_shadow)
3022 warning_code = OPT_Wshadow;
3023 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3024 warning_code = OPT_Wshadow_compatible_local;
3025 else
3026 warning_code = OPT_Wshadow_local;
3027 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3028 "declaration of %qD shadows a parameter",
3029 new_decl);
3031 else if (DECL_FILE_SCOPE_P (old_decl))
3033 /* Do not warn if a variable shadows a function, unless
3034 the variable is a function or a pointer-to-function. */
3035 if (TREE_CODE (old_decl) == FUNCTION_DECL
3036 && TREE_CODE (new_decl) != FUNCTION_DECL
3037 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3038 continue;
3040 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3041 "declaration of %qD shadows a global "
3042 "declaration",
3043 new_decl);
3045 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3046 && fndecl_built_in_p (old_decl))
3048 warning (OPT_Wshadow, "declaration of %q+D shadows "
3049 "a built-in function", new_decl);
3050 break;
3052 else
3054 enum opt_code warning_code;
3056 /* If '-Wshadow=compatible-local' is specified without other
3057 -Wshadow= flags, we will warn only when the types of the
3058 shadowing variable (i.e. new_decl) and the shadowed variable
3059 (old_decl) are compatible. */
3060 if (warn_shadow)
3061 warning_code = OPT_Wshadow;
3062 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3063 warning_code = OPT_Wshadow_compatible_local;
3064 else
3065 warning_code = OPT_Wshadow_local;
3066 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3067 "declaration of %qD shadows a previous local",
3068 new_decl);
3071 if (warned)
3072 inform (DECL_SOURCE_LOCATION (old_decl),
3073 "shadowed declaration is here");
3075 break;
3079 /* Record a decl-node X as belonging to the current lexical scope.
3080 Check for errors (such as an incompatible declaration for the same
3081 name already seen in the same scope).
3083 Returns either X or an old decl for the same name.
3084 If an old decl is returned, it may have been smashed
3085 to agree with what X says. */
3087 tree
3088 pushdecl (tree x)
3090 tree name = DECL_NAME (x);
3091 struct c_scope *scope = current_scope;
3092 struct c_binding *b;
3093 bool nested = false;
3094 location_t locus = DECL_SOURCE_LOCATION (x);
3096 /* Must set DECL_CONTEXT for everything not at file scope or
3097 DECL_FILE_SCOPE_P won't work. Local externs don't count
3098 unless they have initializers (which generate code). */
3099 if (current_function_decl
3100 && (!VAR_OR_FUNCTION_DECL_P (x)
3101 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3102 DECL_CONTEXT (x) = current_function_decl;
3104 /* Anonymous decls are just inserted in the scope. */
3105 if (!name)
3107 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3108 locus);
3109 return x;
3112 /* First, see if there is another declaration with the same name in
3113 the current scope. If there is, duplicate_decls may do all the
3114 work for us. If duplicate_decls returns false, that indicates
3115 two incompatible decls in the same scope; we are to silently
3116 replace the old one (duplicate_decls has issued all appropriate
3117 diagnostics). In particular, we should not consider possible
3118 duplicates in the external scope, or shadowing. */
3119 b = I_SYMBOL_BINDING (name);
3120 if (b && B_IN_SCOPE (b, scope))
3122 struct c_binding *b_ext, *b_use;
3123 tree type = TREE_TYPE (x);
3124 tree visdecl = b->decl;
3125 tree vistype = TREE_TYPE (visdecl);
3126 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3127 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3128 b->inner_comp = false;
3129 b_use = b;
3130 b_ext = b;
3131 /* If this is an external linkage declaration, we should check
3132 for compatibility with the type in the external scope before
3133 setting the type at this scope based on the visible
3134 information only. */
3135 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3137 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3138 b_ext = b_ext->shadowed;
3139 if (b_ext)
3141 b_use = b_ext;
3142 if (b_use->u.type)
3143 TREE_TYPE (b_use->decl) = b_use->u.type;
3146 if (duplicate_decls (x, b_use->decl))
3148 if (b_use != b)
3150 /* Save the updated type in the external scope and
3151 restore the proper type for this scope. */
3152 tree thistype;
3153 if (comptypes (vistype, type))
3154 thistype = composite_type (vistype, type);
3155 else
3156 thistype = TREE_TYPE (b_use->decl);
3157 b_use->u.type = TREE_TYPE (b_use->decl);
3158 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3159 && fndecl_built_in_p (b_use->decl))
3160 thistype
3161 = build_type_attribute_variant (thistype,
3162 TYPE_ATTRIBUTES
3163 (b_use->u.type));
3164 TREE_TYPE (b_use->decl) = thistype;
3166 return b_use->decl;
3168 else
3169 goto skip_external_and_shadow_checks;
3172 /* All declarations with external linkage, and all external
3173 references, go in the external scope, no matter what scope is
3174 current. However, the binding in that scope is ignored for
3175 purposes of normal name lookup. A separate binding structure is
3176 created in the requested scope; this governs the normal
3177 visibility of the symbol.
3179 The binding in the externals scope is used exclusively for
3180 detecting duplicate declarations of the same object, no matter
3181 what scope they are in; this is what we do here. (C99 6.2.7p2:
3182 All declarations that refer to the same object or function shall
3183 have compatible type; otherwise, the behavior is undefined.)
3184 However, in Objective-C, we also want to detect declarations
3185 conflicting with those of the basic types. */
3186 if ((DECL_EXTERNAL (x) || scope == file_scope)
3187 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3189 tree type = TREE_TYPE (x);
3190 tree vistype = NULL_TREE;
3191 tree visdecl = NULL_TREE;
3192 bool type_saved = false;
3193 if (b && !B_IN_EXTERNAL_SCOPE (b)
3194 && VAR_OR_FUNCTION_DECL_P (b->decl)
3195 && DECL_FILE_SCOPE_P (b->decl))
3197 visdecl = b->decl;
3198 vistype = TREE_TYPE (visdecl);
3200 if (scope != file_scope
3201 && !DECL_IN_SYSTEM_HEADER (x))
3202 warning_at (locus, OPT_Wnested_externs,
3203 "nested extern declaration of %qD", x);
3205 while (b && !B_IN_EXTERNAL_SCOPE (b))
3207 /* If this decl might be modified, save its type. This is
3208 done here rather than when the decl is first bound
3209 because the type may change after first binding, through
3210 being completed or through attributes being added. If we
3211 encounter multiple such decls, only the first should have
3212 its type saved; the others will already have had their
3213 proper types saved and the types will not have changed as
3214 their scopes will not have been re-entered. */
3215 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3217 b->u.type = TREE_TYPE (b->decl);
3218 type_saved = true;
3220 if (B_IN_FILE_SCOPE (b)
3221 && VAR_P (b->decl)
3222 && TREE_STATIC (b->decl)
3223 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3224 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3225 && TREE_CODE (type) == ARRAY_TYPE
3226 && TYPE_DOMAIN (type)
3227 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3228 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3230 /* Array type completed in inner scope, which should be
3231 diagnosed if the completion does not have size 1 and
3232 it does not get completed in the file scope. */
3233 b->inner_comp = true;
3235 b = b->shadowed;
3238 /* If a matching external declaration has been found, set its
3239 type to the composite of all the types of that declaration.
3240 After the consistency checks, it will be reset to the
3241 composite of the visible types only. */
3242 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3243 && b->u.type)
3244 TREE_TYPE (b->decl) = b->u.type;
3246 /* The point of the same_translation_unit_p check here is,
3247 we want to detect a duplicate decl for a construct like
3248 foo() { extern bar(); } ... static bar(); but not if
3249 they are in different translation units. In any case,
3250 the static does not go in the externals scope. */
3251 if (b
3252 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3253 && duplicate_decls (x, b->decl))
3255 tree thistype;
3256 if (vistype)
3258 if (comptypes (vistype, type))
3259 thistype = composite_type (vistype, type);
3260 else
3261 thistype = TREE_TYPE (b->decl);
3263 else
3264 thistype = type;
3265 b->u.type = TREE_TYPE (b->decl);
3266 /* Propagate the type attributes to the decl. */
3267 thistype
3268 = build_type_attribute_variant (thistype,
3269 TYPE_ATTRIBUTES (b->u.type));
3270 TREE_TYPE (b->decl) = thistype;
3271 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3272 locus);
3273 return b->decl;
3275 else if (TREE_PUBLIC (x))
3277 if (visdecl && !b && duplicate_decls (x, visdecl))
3279 /* An external declaration at block scope referring to a
3280 visible entity with internal linkage. The composite
3281 type will already be correct for this scope, so we
3282 just need to fall through to make the declaration in
3283 this scope. */
3284 nested = true;
3285 x = visdecl;
3287 else
3289 bind (name, x, external_scope, /*invisible=*/true,
3290 /*nested=*/false, locus);
3291 nested = true;
3296 if (TREE_CODE (x) != PARM_DECL)
3297 warn_if_shadowing (x);
3299 skip_external_and_shadow_checks:
3300 if (TREE_CODE (x) == TYPE_DECL)
3302 /* So this is a typedef, set its underlying type. */
3303 set_underlying_type (x);
3305 /* If X is a typedef defined in the current function, record it
3306 for the purpose of implementing the -Wunused-local-typedefs
3307 warning. */
3308 record_locally_defined_typedef (x);
3311 bind (name, x, scope, /*invisible=*/false, nested, locus);
3313 /* If x's type is incomplete because it's based on a
3314 structure or union which has not yet been fully declared,
3315 attach it to that structure or union type, so we can go
3316 back and complete the variable declaration later, if the
3317 structure or union gets fully declared.
3319 If the input is erroneous, we can have error_mark in the type
3320 slot (e.g. "f(void a, ...)") - that doesn't count as an
3321 incomplete type. */
3322 if (TREE_TYPE (x) != error_mark_node
3323 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3325 tree element = TREE_TYPE (x);
3327 while (TREE_CODE (element) == ARRAY_TYPE)
3328 element = TREE_TYPE (element);
3329 element = TYPE_MAIN_VARIANT (element);
3331 if ((RECORD_OR_UNION_TYPE_P (element)
3332 || TREE_CODE (element) == ENUMERAL_TYPE)
3333 && (TREE_CODE (x) != TYPE_DECL
3334 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3335 && !COMPLETE_TYPE_P (element))
3336 C_TYPE_INCOMPLETE_VARS (element)
3337 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3339 return x;
3343 /* Issue a warning about implicit function declaration. ID is the function
3344 identifier, OLDDECL is a declaration of the function in a different scope,
3345 or NULL_TREE. */
3347 static void
3348 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3350 if (!warn_implicit_function_declaration)
3351 return;
3353 bool warned;
3354 auto_diagnostic_group d;
3355 name_hint hint;
3356 if (!olddecl)
3357 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3359 if (flag_isoc99)
3361 if (const char *suggestion = hint.suggestion ())
3363 gcc_rich_location richloc (loc);
3364 richloc.add_fixit_replace (suggestion);
3365 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3366 "implicit declaration of function %qE;"
3367 " did you mean %qs?",
3368 id, suggestion);
3370 else
3371 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3372 "implicit declaration of function %qE", id);
3374 else if (const char *suggestion = hint.suggestion ())
3376 gcc_rich_location richloc (loc);
3377 richloc.add_fixit_replace (suggestion);
3378 warned = warning_at
3379 (&richloc, OPT_Wimplicit_function_declaration,
3380 G_("implicit declaration of function %qE; did you mean %qs?"),
3381 id, suggestion);
3383 else
3384 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3385 G_("implicit declaration of function %qE"), id);
3387 if (warned)
3389 /* Whether the olddecl is an undeclared builtin function.
3390 locate_old_decl will not generate a diagnostic for those,
3391 so in that case we want to look elsewhere. */
3392 bool undeclared_builtin = (olddecl
3393 && TREE_CODE (olddecl) == FUNCTION_DECL
3394 && fndecl_built_in_p (olddecl)
3395 && !C_DECL_DECLARED_BUILTIN (olddecl));
3396 if (undeclared_builtin)
3398 const char *header = header_for_builtin_fn (olddecl);
3399 if (header)
3401 rich_location richloc (line_table, loc);
3402 maybe_add_include_fixit (&richloc, header, true);
3403 inform (&richloc,
3404 "include %qs or provide a declaration of %qE",
3405 header, id);
3408 else if (olddecl)
3409 locate_old_decl (olddecl);
3412 if (!warned)
3413 hint.suppress ();
3416 /* Return the name of the header file that declares built-in function
3417 FNDECL, or null if either we don't know or don't expect to see an
3418 explicit declaration. */
3420 static const char *
3421 header_for_builtin_fn (tree fndecl)
3423 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3424 return NULL;
3426 switch (DECL_FUNCTION_CODE (fndecl))
3428 CASE_FLT_FN (BUILT_IN_ACOS):
3429 CASE_FLT_FN (BUILT_IN_ACOSH):
3430 CASE_FLT_FN (BUILT_IN_ASIN):
3431 CASE_FLT_FN (BUILT_IN_ASINH):
3432 CASE_FLT_FN (BUILT_IN_ATAN):
3433 CASE_FLT_FN (BUILT_IN_ATANH):
3434 CASE_FLT_FN (BUILT_IN_ATAN2):
3435 CASE_FLT_FN (BUILT_IN_CBRT):
3436 CASE_FLT_FN (BUILT_IN_CEIL):
3437 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3438 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3439 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3440 CASE_FLT_FN (BUILT_IN_COS):
3441 CASE_FLT_FN (BUILT_IN_COSH):
3442 CASE_FLT_FN (BUILT_IN_ERF):
3443 CASE_FLT_FN (BUILT_IN_ERFC):
3444 CASE_FLT_FN (BUILT_IN_EXP):
3445 CASE_FLT_FN (BUILT_IN_EXP2):
3446 CASE_FLT_FN (BUILT_IN_EXPM1):
3447 CASE_FLT_FN (BUILT_IN_FABS):
3448 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3449 CASE_FLT_FN (BUILT_IN_FDIM):
3450 CASE_FLT_FN (BUILT_IN_FLOOR):
3451 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3452 CASE_FLT_FN (BUILT_IN_FMA):
3453 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3454 CASE_FLT_FN (BUILT_IN_FMAX):
3455 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3456 CASE_FLT_FN (BUILT_IN_FMIN):
3457 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3458 CASE_FLT_FN (BUILT_IN_FMOD):
3459 CASE_FLT_FN (BUILT_IN_FREXP):
3460 CASE_FLT_FN (BUILT_IN_HYPOT):
3461 CASE_FLT_FN (BUILT_IN_ILOGB):
3462 CASE_FLT_FN (BUILT_IN_LDEXP):
3463 CASE_FLT_FN (BUILT_IN_LGAMMA):
3464 CASE_FLT_FN (BUILT_IN_LLRINT):
3465 CASE_FLT_FN (BUILT_IN_LLROUND):
3466 CASE_FLT_FN (BUILT_IN_LOG):
3467 CASE_FLT_FN (BUILT_IN_LOG10):
3468 CASE_FLT_FN (BUILT_IN_LOG1P):
3469 CASE_FLT_FN (BUILT_IN_LOG2):
3470 CASE_FLT_FN (BUILT_IN_LOGB):
3471 CASE_FLT_FN (BUILT_IN_LRINT):
3472 CASE_FLT_FN (BUILT_IN_LROUND):
3473 CASE_FLT_FN (BUILT_IN_MODF):
3474 CASE_FLT_FN (BUILT_IN_NAN):
3475 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3476 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3477 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3478 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3479 CASE_FLT_FN (BUILT_IN_POW):
3480 CASE_FLT_FN (BUILT_IN_REMAINDER):
3481 CASE_FLT_FN (BUILT_IN_REMQUO):
3482 CASE_FLT_FN (BUILT_IN_RINT):
3483 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3484 CASE_FLT_FN (BUILT_IN_ROUND):
3485 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3486 CASE_FLT_FN (BUILT_IN_SCALBLN):
3487 CASE_FLT_FN (BUILT_IN_SCALBN):
3488 CASE_FLT_FN (BUILT_IN_SIN):
3489 CASE_FLT_FN (BUILT_IN_SINH):
3490 CASE_FLT_FN (BUILT_IN_SINCOS):
3491 CASE_FLT_FN (BUILT_IN_SQRT):
3492 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3493 CASE_FLT_FN (BUILT_IN_TAN):
3494 CASE_FLT_FN (BUILT_IN_TANH):
3495 CASE_FLT_FN (BUILT_IN_TGAMMA):
3496 CASE_FLT_FN (BUILT_IN_TRUNC):
3497 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3498 case BUILT_IN_ISINF:
3499 case BUILT_IN_ISNAN:
3500 return "<math.h>";
3501 CASE_FLT_FN (BUILT_IN_CABS):
3502 CASE_FLT_FN (BUILT_IN_CACOS):
3503 CASE_FLT_FN (BUILT_IN_CACOSH):
3504 CASE_FLT_FN (BUILT_IN_CARG):
3505 CASE_FLT_FN (BUILT_IN_CASIN):
3506 CASE_FLT_FN (BUILT_IN_CASINH):
3507 CASE_FLT_FN (BUILT_IN_CATAN):
3508 CASE_FLT_FN (BUILT_IN_CATANH):
3509 CASE_FLT_FN (BUILT_IN_CCOS):
3510 CASE_FLT_FN (BUILT_IN_CCOSH):
3511 CASE_FLT_FN (BUILT_IN_CEXP):
3512 CASE_FLT_FN (BUILT_IN_CIMAG):
3513 CASE_FLT_FN (BUILT_IN_CLOG):
3514 CASE_FLT_FN (BUILT_IN_CONJ):
3515 CASE_FLT_FN (BUILT_IN_CPOW):
3516 CASE_FLT_FN (BUILT_IN_CPROJ):
3517 CASE_FLT_FN (BUILT_IN_CREAL):
3518 CASE_FLT_FN (BUILT_IN_CSIN):
3519 CASE_FLT_FN (BUILT_IN_CSINH):
3520 CASE_FLT_FN (BUILT_IN_CSQRT):
3521 CASE_FLT_FN (BUILT_IN_CTAN):
3522 CASE_FLT_FN (BUILT_IN_CTANH):
3523 return "<complex.h>";
3524 case BUILT_IN_MEMCHR:
3525 case BUILT_IN_MEMCMP:
3526 case BUILT_IN_MEMCPY:
3527 case BUILT_IN_MEMMOVE:
3528 case BUILT_IN_MEMSET:
3529 case BUILT_IN_STRCAT:
3530 case BUILT_IN_STRCHR:
3531 case BUILT_IN_STRCMP:
3532 case BUILT_IN_STRCPY:
3533 case BUILT_IN_STRCSPN:
3534 case BUILT_IN_STRLEN:
3535 case BUILT_IN_STRNCAT:
3536 case BUILT_IN_STRNCMP:
3537 case BUILT_IN_STRNCPY:
3538 case BUILT_IN_STRPBRK:
3539 case BUILT_IN_STRRCHR:
3540 case BUILT_IN_STRSPN:
3541 case BUILT_IN_STRSTR:
3542 return "<string.h>";
3543 case BUILT_IN_FPRINTF:
3544 case BUILT_IN_PUTC:
3545 case BUILT_IN_FPUTC:
3546 case BUILT_IN_FPUTS:
3547 case BUILT_IN_FSCANF:
3548 case BUILT_IN_FWRITE:
3549 case BUILT_IN_PRINTF:
3550 case BUILT_IN_PUTCHAR:
3551 case BUILT_IN_PUTS:
3552 case BUILT_IN_SCANF:
3553 case BUILT_IN_SNPRINTF:
3554 case BUILT_IN_SPRINTF:
3555 case BUILT_IN_SSCANF:
3556 case BUILT_IN_VFPRINTF:
3557 case BUILT_IN_VFSCANF:
3558 case BUILT_IN_VPRINTF:
3559 case BUILT_IN_VSCANF:
3560 case BUILT_IN_VSNPRINTF:
3561 case BUILT_IN_VSPRINTF:
3562 case BUILT_IN_VSSCANF:
3563 return "<stdio.h>";
3564 case BUILT_IN_ISALNUM:
3565 case BUILT_IN_ISALPHA:
3566 case BUILT_IN_ISBLANK:
3567 case BUILT_IN_ISCNTRL:
3568 case BUILT_IN_ISDIGIT:
3569 case BUILT_IN_ISGRAPH:
3570 case BUILT_IN_ISLOWER:
3571 case BUILT_IN_ISPRINT:
3572 case BUILT_IN_ISPUNCT:
3573 case BUILT_IN_ISSPACE:
3574 case BUILT_IN_ISUPPER:
3575 case BUILT_IN_ISXDIGIT:
3576 case BUILT_IN_TOLOWER:
3577 case BUILT_IN_TOUPPER:
3578 return "<ctype.h>";
3579 case BUILT_IN_ISWALNUM:
3580 case BUILT_IN_ISWALPHA:
3581 case BUILT_IN_ISWBLANK:
3582 case BUILT_IN_ISWCNTRL:
3583 case BUILT_IN_ISWDIGIT:
3584 case BUILT_IN_ISWGRAPH:
3585 case BUILT_IN_ISWLOWER:
3586 case BUILT_IN_ISWPRINT:
3587 case BUILT_IN_ISWPUNCT:
3588 case BUILT_IN_ISWSPACE:
3589 case BUILT_IN_ISWUPPER:
3590 case BUILT_IN_ISWXDIGIT:
3591 case BUILT_IN_TOWLOWER:
3592 case BUILT_IN_TOWUPPER:
3593 return "<wctype.h>";
3594 case BUILT_IN_ABORT:
3595 case BUILT_IN_ABS:
3596 case BUILT_IN_CALLOC:
3597 case BUILT_IN_EXIT:
3598 case BUILT_IN_FREE:
3599 case BUILT_IN_LABS:
3600 case BUILT_IN_LLABS:
3601 case BUILT_IN_MALLOC:
3602 case BUILT_IN_REALLOC:
3603 case BUILT_IN__EXIT2:
3604 case BUILT_IN_ALIGNED_ALLOC:
3605 return "<stdlib.h>";
3606 case BUILT_IN_IMAXABS:
3607 return "<inttypes.h>";
3608 case BUILT_IN_STRFTIME:
3609 return "<time.h>";
3610 default:
3611 return NULL;
3615 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3616 function of type int (). */
3618 tree
3619 implicitly_declare (location_t loc, tree functionid)
3621 struct c_binding *b;
3622 tree decl = NULL_TREE;
3623 tree asmspec_tree;
3625 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3627 if (B_IN_SCOPE (b, external_scope))
3629 decl = b->decl;
3630 break;
3634 if (decl)
3636 if (TREE_CODE (decl) != FUNCTION_DECL)
3637 return decl;
3639 /* FIXME: Objective-C has weird not-really-builtin functions
3640 which are supposed to be visible automatically. They wind up
3641 in the external scope because they're pushed before the file
3642 scope gets created. Catch this here and rebind them into the
3643 file scope. */
3644 if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3646 bind (functionid, decl, file_scope,
3647 /*invisible=*/false, /*nested=*/true,
3648 DECL_SOURCE_LOCATION (decl));
3649 return decl;
3651 else
3653 tree newtype = default_function_type;
3654 if (b->u.type)
3655 TREE_TYPE (decl) = b->u.type;
3656 /* Implicit declaration of a function already declared
3657 (somehow) in a different scope, or as a built-in.
3658 If this is the first time this has happened, warn;
3659 then recycle the old declaration but with the new type. */
3660 if (!C_DECL_IMPLICIT (decl))
3662 implicit_decl_warning (loc, functionid, decl);
3663 C_DECL_IMPLICIT (decl) = 1;
3665 if (fndecl_built_in_p (decl))
3667 newtype = build_type_attribute_variant (newtype,
3668 TYPE_ATTRIBUTES
3669 (TREE_TYPE (decl)));
3670 if (!comptypes (newtype, TREE_TYPE (decl)))
3672 bool warned = warning_at (loc,
3673 OPT_Wbuiltin_declaration_mismatch,
3674 "incompatible implicit "
3675 "declaration of built-in "
3676 "function %qD", decl);
3677 /* See if we can hint which header to include. */
3678 const char *header = header_for_builtin_fn (decl);
3679 if (header != NULL && warned)
3681 rich_location richloc (line_table, loc);
3682 maybe_add_include_fixit (&richloc, header, true);
3683 inform (&richloc,
3684 "include %qs or provide a declaration of %qD",
3685 header, decl);
3687 newtype = TREE_TYPE (decl);
3690 else
3692 if (!comptypes (newtype, TREE_TYPE (decl)))
3694 auto_diagnostic_group d;
3695 error_at (loc, "incompatible implicit declaration of "
3696 "function %qD", decl);
3697 locate_old_decl (decl);
3700 b->u.type = TREE_TYPE (decl);
3701 TREE_TYPE (decl) = newtype;
3702 bind (functionid, decl, current_scope,
3703 /*invisible=*/false, /*nested=*/true,
3704 DECL_SOURCE_LOCATION (decl));
3705 return decl;
3709 /* Not seen before. */
3710 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3711 DECL_EXTERNAL (decl) = 1;
3712 TREE_PUBLIC (decl) = 1;
3713 C_DECL_IMPLICIT (decl) = 1;
3714 implicit_decl_warning (loc, functionid, 0);
3715 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3716 if (asmspec_tree)
3717 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3719 /* C89 says implicit declarations are in the innermost block.
3720 So we record the decl in the standard fashion. */
3721 decl = pushdecl (decl);
3723 /* No need to call objc_check_decl here - it's a function type. */
3724 rest_of_decl_compilation (decl, 0, 0);
3726 /* Write a record describing this implicit function declaration
3727 to the prototypes file (if requested). */
3728 gen_aux_info_record (decl, 0, 1, 0);
3730 /* Possibly apply some default attributes to this implicit declaration. */
3731 decl_attributes (&decl, NULL_TREE, 0);
3733 return decl;
3736 /* Issue an error message for a reference to an undeclared variable
3737 ID, including a reference to a builtin outside of function-call
3738 context. Establish a binding of the identifier to error_mark_node
3739 in an appropriate scope, which will suppress further errors for the
3740 same identifier. The error message should be given location LOC. */
3741 void
3742 undeclared_variable (location_t loc, tree id)
3744 static bool already = false;
3745 struct c_scope *scope;
3747 auto_diagnostic_group d;
3748 if (current_function_decl == NULL_TREE)
3750 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3751 if (const char *suggestion = guessed_id.suggestion ())
3753 gcc_rich_location richloc (loc);
3754 richloc.add_fixit_replace (suggestion);
3755 error_at (&richloc,
3756 "%qE undeclared here (not in a function);"
3757 " did you mean %qs?",
3758 id, suggestion);
3760 else
3761 error_at (loc, "%qE undeclared here (not in a function)", id);
3762 scope = current_scope;
3764 else
3766 if (!objc_diagnose_private_ivar (id))
3768 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3769 if (const char *suggestion = guessed_id.suggestion ())
3771 gcc_rich_location richloc (loc);
3772 richloc.add_fixit_replace (suggestion);
3773 error_at (&richloc,
3774 "%qE undeclared (first use in this function);"
3775 " did you mean %qs?",
3776 id, suggestion);
3778 else
3779 error_at (loc, "%qE undeclared (first use in this function)", id);
3781 if (!already)
3783 inform (loc, "each undeclared identifier is reported only"
3784 " once for each function it appears in");
3785 already = true;
3788 /* If we are parsing old-style parameter decls, current_function_decl
3789 will be nonnull but current_function_scope will be null. */
3790 scope = current_function_scope ? current_function_scope : current_scope;
3792 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3793 UNKNOWN_LOCATION);
3796 /* Subroutine of lookup_label, declare_label, define_label: construct a
3797 LABEL_DECL with all the proper frills. Also create a struct
3798 c_label_vars initialized for the current scope. */
3800 static tree
3801 make_label (location_t location, tree name, bool defining,
3802 struct c_label_vars **p_label_vars)
3804 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3805 DECL_CONTEXT (label) = current_function_decl;
3806 SET_DECL_MODE (label, VOIDmode);
3808 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3809 label_vars->shadowed = NULL;
3810 set_spot_bindings (&label_vars->label_bindings, defining);
3811 label_vars->decls_in_scope = make_tree_vector ();
3812 label_vars->gotos = NULL;
3813 *p_label_vars = label_vars;
3815 return label;
3818 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3819 Create one if none exists so far for the current function.
3820 This is called when a label is used in a goto expression or
3821 has its address taken. */
3823 tree
3824 lookup_label (tree name)
3826 tree label;
3827 struct c_label_vars *label_vars;
3829 if (current_function_scope == 0)
3831 error ("label %qE referenced outside of any function", name);
3832 return NULL_TREE;
3835 /* Use a label already defined or ref'd with this name, but not if
3836 it is inherited from a containing function and wasn't declared
3837 using __label__. */
3838 label = I_LABEL_DECL (name);
3839 if (label && (DECL_CONTEXT (label) == current_function_decl
3840 || C_DECLARED_LABEL_FLAG (label)))
3842 /* If the label has only been declared, update its apparent
3843 location to point here, for better diagnostics if it
3844 turns out not to have been defined. */
3845 if (DECL_INITIAL (label) == NULL_TREE)
3846 DECL_SOURCE_LOCATION (label) = input_location;
3847 return label;
3850 /* No label binding for that identifier; make one. */
3851 label = make_label (input_location, name, false, &label_vars);
3853 /* Ordinary labels go in the current function scope. */
3854 bind_label (name, label, current_function_scope, label_vars);
3856 return label;
3859 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3860 to LABEL. */
3862 static void
3863 warn_about_goto (location_t goto_loc, tree label, tree decl)
3865 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3866 error_at (goto_loc,
3867 "jump into scope of identifier with variably modified type");
3868 else
3869 warning_at (goto_loc, OPT_Wjump_misses_init,
3870 "jump skips variable initialization");
3871 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3872 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3875 /* Look up a label because of a goto statement. This is like
3876 lookup_label, but also issues any appropriate warnings. */
3878 tree
3879 lookup_label_for_goto (location_t loc, tree name)
3881 tree label;
3882 struct c_label_vars *label_vars;
3883 unsigned int ix;
3884 tree decl;
3886 label = lookup_label (name);
3887 if (label == NULL_TREE)
3888 return NULL_TREE;
3890 /* If we are jumping to a different function, we can't issue any
3891 useful warnings. */
3892 if (DECL_CONTEXT (label) != current_function_decl)
3894 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3895 return label;
3898 label_vars = I_LABEL_BINDING (name)->u.label;
3900 /* If the label has not yet been defined, then push this goto on a
3901 list for possible later warnings. */
3902 if (label_vars->label_bindings.scope == NULL)
3904 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3906 g->loc = loc;
3907 set_spot_bindings (&g->goto_bindings, true);
3908 vec_safe_push (label_vars->gotos, g);
3909 return label;
3912 /* If there are any decls in label_vars->decls_in_scope, then this
3913 goto has missed the declaration of the decl. This happens for a
3914 case like
3915 int i = 1;
3916 lab:
3918 goto lab;
3919 Issue a warning or error. */
3920 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3921 warn_about_goto (loc, label, decl);
3923 if (label_vars->label_bindings.left_stmt_expr)
3925 error_at (loc, "jump into statement expression");
3926 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3929 return label;
3932 /* Make a label named NAME in the current function, shadowing silently
3933 any that may be inherited from containing functions or containing
3934 scopes. This is called for __label__ declarations. */
3936 tree
3937 declare_label (tree name)
3939 struct c_binding *b = I_LABEL_BINDING (name);
3940 tree label;
3941 struct c_label_vars *label_vars;
3943 /* Check to make sure that the label hasn't already been declared
3944 at this scope */
3945 if (b && B_IN_CURRENT_SCOPE (b))
3947 auto_diagnostic_group d;
3948 error ("duplicate label declaration %qE", name);
3949 locate_old_decl (b->decl);
3951 /* Just use the previous declaration. */
3952 return b->decl;
3955 label = make_label (input_location, name, false, &label_vars);
3956 C_DECLARED_LABEL_FLAG (label) = 1;
3958 /* Declared labels go in the current scope. */
3959 bind_label (name, label, current_scope, label_vars);
3961 return label;
3964 /* When we define a label, issue any appropriate warnings if there are
3965 any gotos earlier in the function which jump to this label. */
3967 static void
3968 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3970 unsigned int ix;
3971 struct c_goto_bindings *g;
3973 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3975 struct c_binding *b;
3976 struct c_scope *scope;
3978 /* We have a goto to this label. The goto is going forward. In
3979 g->scope, the goto is going to skip any binding which was
3980 defined after g->bindings_in_scope. */
3981 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3983 for (b = g->goto_bindings.scope->bindings;
3984 b != g->goto_bindings.bindings_in_scope;
3985 b = b->prev)
3987 if (decl_jump_unsafe (b->decl))
3988 warn_about_goto (g->loc, label, b->decl);
3992 /* We also need to warn about decls defined in any scopes
3993 between the scope of the label and the scope of the goto. */
3994 for (scope = label_vars->label_bindings.scope;
3995 scope != g->goto_bindings.scope;
3996 scope = scope->outer)
3998 gcc_assert (scope != NULL);
3999 if (scope->has_jump_unsafe_decl)
4001 if (scope == label_vars->label_bindings.scope)
4002 b = label_vars->label_bindings.bindings_in_scope;
4003 else
4004 b = scope->bindings;
4005 for (; b != NULL; b = b->prev)
4007 if (decl_jump_unsafe (b->decl))
4008 warn_about_goto (g->loc, label, b->decl);
4013 if (g->goto_bindings.stmt_exprs > 0)
4015 error_at (g->loc, "jump into statement expression");
4016 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4017 label);
4021 /* Now that the label is defined, we will issue warnings about
4022 subsequent gotos to this label when we see them. */
4023 vec_safe_truncate (label_vars->gotos, 0);
4024 label_vars->gotos = NULL;
4027 /* Define a label, specifying the location in the source file.
4028 Return the LABEL_DECL node for the label, if the definition is valid.
4029 Otherwise return NULL_TREE. */
4031 tree
4032 define_label (location_t location, tree name)
4034 /* Find any preexisting label with this name. It is an error
4035 if that label has already been defined in this function, or
4036 if there is a containing function with a declared label with
4037 the same name. */
4038 tree label = I_LABEL_DECL (name);
4040 if (label
4041 && ((DECL_CONTEXT (label) == current_function_decl
4042 && DECL_INITIAL (label) != NULL_TREE)
4043 || (DECL_CONTEXT (label) != current_function_decl
4044 && C_DECLARED_LABEL_FLAG (label))))
4046 auto_diagnostic_group d;
4047 error_at (location, "duplicate label %qD", label);
4048 locate_old_decl (label);
4049 return NULL_TREE;
4051 else if (label && DECL_CONTEXT (label) == current_function_decl)
4053 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4055 /* The label has been used or declared already in this function,
4056 but not defined. Update its location to point to this
4057 definition. */
4058 DECL_SOURCE_LOCATION (label) = location;
4059 set_spot_bindings (&label_vars->label_bindings, true);
4061 /* Issue warnings as required about any goto statements from
4062 earlier in the function. */
4063 check_earlier_gotos (label, label_vars);
4065 else
4067 struct c_label_vars *label_vars;
4069 /* No label binding for that identifier; make one. */
4070 label = make_label (location, name, true, &label_vars);
4072 /* Ordinary labels go in the current function scope. */
4073 bind_label (name, label, current_function_scope, label_vars);
4076 if (!in_system_header_at (input_location) && lookup_name (name))
4077 warning_at (location, OPT_Wtraditional,
4078 "traditional C lacks a separate namespace "
4079 "for labels, identifier %qE conflicts", name);
4081 /* Mark label as having been defined. */
4082 DECL_INITIAL (label) = error_mark_node;
4083 return label;
4086 /* Get the bindings for a new switch statement. This is used to issue
4087 warnings as appropriate for jumps from the switch to case or
4088 default labels. */
4090 struct c_spot_bindings *
4091 c_get_switch_bindings (void)
4093 struct c_spot_bindings *switch_bindings;
4095 switch_bindings = XNEW (struct c_spot_bindings);
4096 set_spot_bindings (switch_bindings, true);
4097 return switch_bindings;
4100 void
4101 c_release_switch_bindings (struct c_spot_bindings *bindings)
4103 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4104 XDELETE (bindings);
4107 /* This is called at the point of a case or default label to issue
4108 warnings about decls as needed. It returns true if it found an
4109 error, not just a warning. */
4111 bool
4112 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4113 location_t switch_loc, location_t case_loc)
4115 bool saw_error;
4116 struct c_scope *scope;
4118 saw_error = false;
4119 for (scope = current_scope;
4120 scope != switch_bindings->scope;
4121 scope = scope->outer)
4123 struct c_binding *b;
4125 gcc_assert (scope != NULL);
4127 if (!scope->has_jump_unsafe_decl)
4128 continue;
4130 for (b = scope->bindings; b != NULL; b = b->prev)
4132 if (decl_jump_unsafe (b->decl))
4134 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
4136 saw_error = true;
4137 error_at (case_loc,
4138 ("switch jumps into scope of identifier with "
4139 "variably modified type"));
4141 else
4142 warning_at (case_loc, OPT_Wjump_misses_init,
4143 "switch jumps over variable initialization");
4144 inform (switch_loc, "switch starts here");
4145 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4146 b->decl);
4151 if (switch_bindings->stmt_exprs > 0)
4153 saw_error = true;
4154 error_at (case_loc, "switch jumps into statement expression");
4155 inform (switch_loc, "switch starts here");
4158 return saw_error;
4161 /* Given NAME, an IDENTIFIER_NODE,
4162 return the structure (or union or enum) definition for that name.
4163 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4164 CODE says which kind of type the caller wants;
4165 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4166 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4167 location where the tag was defined.
4168 If the wrong kind of type is found, an error is reported. */
4170 static tree
4171 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4172 location_t *ploc)
4174 struct c_binding *b = I_TAG_BINDING (name);
4175 bool thislevel = false;
4177 if (!b || !b->decl)
4178 return NULL_TREE;
4180 /* We only care about whether it's in this level if
4181 thislevel_only was set or it might be a type clash. */
4182 if (thislevel_only || TREE_CODE (b->decl) != code)
4184 /* For our purposes, a tag in the external scope is the same as
4185 a tag in the file scope. (Primarily relevant to Objective-C
4186 and its builtin structure tags, which get pushed before the
4187 file scope is created.) */
4188 if (B_IN_CURRENT_SCOPE (b)
4189 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4190 thislevel = true;
4193 if (thislevel_only && !thislevel)
4194 return NULL_TREE;
4196 if (TREE_CODE (b->decl) != code)
4198 /* Definition isn't the kind we were looking for. */
4199 pending_invalid_xref = name;
4200 pending_invalid_xref_location = input_location;
4202 /* If in the same binding level as a declaration as a tag
4203 of a different type, this must not be allowed to
4204 shadow that tag, so give the error immediately.
4205 (For example, "struct foo; union foo;" is invalid.) */
4206 if (thislevel)
4207 pending_xref_error ();
4210 if (ploc != NULL)
4211 *ploc = b->locus;
4213 return b->decl;
4216 /* Return true if a definition exists for NAME with code CODE. */
4218 bool
4219 tag_exists_p (enum tree_code code, tree name)
4221 struct c_binding *b = I_TAG_BINDING (name);
4223 if (b == NULL || b->decl == NULL_TREE)
4224 return false;
4225 return TREE_CODE (b->decl) == code;
4228 /* Print an error message now
4229 for a recent invalid struct, union or enum cross reference.
4230 We don't print them immediately because they are not invalid
4231 when used in the `struct foo;' construct for shadowing. */
4233 void
4234 pending_xref_error (void)
4236 if (pending_invalid_xref != NULL_TREE)
4237 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4238 pending_invalid_xref);
4239 pending_invalid_xref = NULL_TREE;
4243 /* Look up NAME in the current scope and its superiors
4244 in the namespace of variables, functions and typedefs.
4245 Return a ..._DECL node of some kind representing its definition,
4246 or return NULL_TREE if it is undefined. */
4248 tree
4249 lookup_name (tree name)
4251 struct c_binding *b = I_SYMBOL_BINDING (name);
4252 if (b && !b->invisible)
4254 maybe_record_typedef_use (b->decl);
4255 return b->decl;
4257 return NULL_TREE;
4260 /* Similar to `lookup_name' but look only at the indicated scope. */
4262 static tree
4263 lookup_name_in_scope (tree name, struct c_scope *scope)
4265 struct c_binding *b;
4267 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4268 if (B_IN_SCOPE (b, scope))
4269 return b->decl;
4270 return NULL_TREE;
4273 /* Look for the closest match for NAME within the currently valid
4274 scopes.
4276 This finds the identifier with the lowest Levenshtein distance to
4277 NAME. If there are multiple candidates with equal minimal distance,
4278 the first one found is returned. Scopes are searched from innermost
4279 outwards, and within a scope in reverse order of declaration, thus
4280 benefiting candidates "near" to the current scope.
4282 The function also looks for similar macro names to NAME, since a
4283 misspelled macro name will not be expanded, and hence looks like an
4284 identifier to the C frontend.
4286 It also looks for start_typename keywords, to detect "singed" vs "signed"
4287 typos.
4289 Use LOC for any deferred diagnostics. */
4291 name_hint
4292 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4294 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4296 /* First, try some well-known names in the C standard library, in case
4297 the user forgot a #include. */
4298 const char *header_hint
4299 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4301 if (header_hint)
4302 return name_hint (NULL,
4303 new suggest_missing_header (loc,
4304 IDENTIFIER_POINTER (name),
4305 header_hint));
4307 /* Only suggest names reserved for the implementation if NAME begins
4308 with an underscore. */
4309 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4311 best_match<tree, tree> bm (name);
4313 /* Look within currently valid scopes. */
4314 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4315 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4317 if (!binding->id || binding->invisible)
4318 continue;
4319 if (binding->decl == error_mark_node)
4320 continue;
4321 /* Don't use bindings from implicitly declared functions,
4322 as they were likely misspellings themselves. */
4323 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4324 if (C_DECL_IMPLICIT (binding->decl))
4325 continue;
4326 /* Don't suggest names that are reserved for use by the
4327 implementation, unless NAME began with an underscore. */
4328 if (!consider_implementation_names)
4330 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4331 if (name_reserved_for_implementation_p (suggestion_str))
4332 continue;
4334 switch (kind)
4336 case FUZZY_LOOKUP_TYPENAME:
4337 if (TREE_CODE (binding->decl) != TYPE_DECL)
4338 continue;
4339 break;
4341 case FUZZY_LOOKUP_FUNCTION_NAME:
4342 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4344 /* Allow function pointers. */
4345 if ((VAR_P (binding->decl)
4346 || TREE_CODE (binding->decl) == PARM_DECL)
4347 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4348 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4349 == FUNCTION_TYPE))
4350 break;
4351 continue;
4353 break;
4355 default:
4356 break;
4358 bm.consider (binding->id);
4361 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4363 x = SOME_OTHER_MACRO (y);
4364 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4365 as a misspelled identifier.
4367 Use the best distance so far so that a candidate is only set if
4368 a macro is better than anything so far. This allows early rejection
4369 (without calculating the edit distance) of macro names that must have
4370 distance >= bm.get_best_distance (), and means that we only get a
4371 non-NULL result for best_macro_match if it's better than any of
4372 the identifiers already checked, which avoids needless creation
4373 of identifiers for macro hashnodes. */
4374 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4375 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4376 /* If a macro is the closest so far to NAME, use it, creating an
4377 identifier tree node for it. */
4378 if (best_macro)
4380 const char *id = (const char *)best_macro->ident.str;
4381 tree macro_as_identifier
4382 = get_identifier_with_length (id, best_macro->ident.len);
4383 bm.set_best_so_far (macro_as_identifier,
4384 bmm.get_best_distance (),
4385 bmm.get_best_candidate_length ());
4388 /* Try the "start_typename" keywords to detect
4389 "singed" vs "signed" typos. */
4390 if (kind == FUZZY_LOOKUP_TYPENAME)
4392 for (unsigned i = 0; i < num_c_common_reswords; i++)
4394 const c_common_resword *resword = &c_common_reswords[i];
4395 if (!c_keyword_starts_typename (resword->rid))
4396 continue;
4397 tree resword_identifier = ridpointers [resword->rid];
4398 if (!resword_identifier)
4399 continue;
4400 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4401 bm.consider (resword_identifier);
4405 tree best = bm.get_best_meaningful_candidate ();
4406 if (best)
4407 return name_hint (IDENTIFIER_POINTER (best), NULL);
4408 else
4409 return name_hint (NULL, NULL);
4413 /* Handle the standard [[nodiscard]] attribute. */
4415 static tree
4416 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4417 int /*flags*/, bool *no_add_attrs)
4419 if (TREE_CODE (*node) == FUNCTION_DECL)
4421 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4422 warning_at (DECL_SOURCE_LOCATION (*node),
4423 OPT_Wattributes, "%qE attribute applied to %qD with void "
4424 "return type", name, *node);
4426 else if (RECORD_OR_UNION_TYPE_P (*node)
4427 || TREE_CODE (*node) == ENUMERAL_TYPE)
4428 /* OK */;
4429 else
4431 pedwarn (input_location,
4432 OPT_Wattributes, "%qE attribute can only be applied to "
4433 "functions or to structure, union or enumeration types", name);
4434 *no_add_attrs = true;
4436 return NULL_TREE;
4438 /* Table of supported standard (C2x) attributes. */
4439 const struct attribute_spec std_attribute_table[] =
4441 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4442 affects_type_identity, handler, exclude } */
4443 { "deprecated", 0, 1, false, false, false, false,
4444 handle_deprecated_attribute, NULL },
4445 { "fallthrough", 0, 0, false, false, false, false,
4446 handle_fallthrough_attribute, NULL },
4447 { "maybe_unused", 0, 0, false, false, false, false,
4448 handle_unused_attribute, NULL },
4449 { "nodiscard", 0, 1, false, false, false, false,
4450 handle_nodiscard_attribute, NULL },
4451 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4454 /* Create the predefined scalar types of C,
4455 and some nodes representing standard constants (0, 1, (void *) 0).
4456 Initialize the global scope.
4457 Make definitions for built-in primitive functions. */
4459 void
4460 c_init_decl_processing (void)
4462 location_t save_loc = input_location;
4464 /* Initialize reserved words for parser. */
4465 c_parse_init ();
4467 register_scoped_attributes (std_attribute_table, NULL);
4469 current_function_decl = NULL_TREE;
4471 gcc_obstack_init (&parser_obstack);
4473 /* Make the externals scope. */
4474 push_scope ();
4475 external_scope = current_scope;
4477 /* Declarations from c_common_nodes_and_builtins must not be associated
4478 with this input file, lest we get differences between using and not
4479 using preprocessed headers. */
4480 input_location = BUILTINS_LOCATION;
4482 c_common_nodes_and_builtins ();
4484 /* In C, comparisons and TRUTH_* expressions have type int. */
4485 truthvalue_type_node = integer_type_node;
4486 truthvalue_true_node = integer_one_node;
4487 truthvalue_false_node = integer_zero_node;
4489 /* Even in C99, which has a real boolean type. */
4490 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4491 boolean_type_node));
4493 input_location = save_loc;
4495 make_fname_decl = c_make_fname_decl;
4496 start_fname_decls ();
4499 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4500 give the decl, NAME is the initialization string and TYPE_DEP
4501 indicates whether NAME depended on the type of the function. As we
4502 don't yet implement delayed emission of static data, we mark the
4503 decl as emitted so it is not placed in the output. Anything using
4504 it must therefore pull out the STRING_CST initializer directly.
4505 FIXME. */
4507 static tree
4508 c_make_fname_decl (location_t loc, tree id, int type_dep)
4510 const char *name = fname_as_string (type_dep);
4511 tree decl, type, init;
4512 size_t length = strlen (name);
4514 type = build_array_type (char_type_node,
4515 build_index_type (size_int (length)));
4516 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4518 decl = build_decl (loc, VAR_DECL, id, type);
4520 TREE_STATIC (decl) = 1;
4521 TREE_READONLY (decl) = 1;
4522 DECL_ARTIFICIAL (decl) = 1;
4524 init = build_string (length + 1, name);
4525 free (CONST_CAST (char *, name));
4526 TREE_TYPE (init) = type;
4527 DECL_INITIAL (decl) = init;
4529 TREE_USED (decl) = 1;
4531 if (current_function_decl
4532 /* For invalid programs like this:
4534 void foo()
4535 const char* p = __FUNCTION__;
4537 the __FUNCTION__ is believed to appear in K&R style function
4538 parameter declarator. In that case we still don't have
4539 function_scope. */
4540 && current_function_scope)
4542 DECL_CONTEXT (decl) = current_function_decl;
4543 bind (id, decl, current_function_scope,
4544 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4547 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4549 return decl;
4552 tree
4553 c_builtin_function (tree decl)
4555 tree type = TREE_TYPE (decl);
4556 tree id = DECL_NAME (decl);
4558 const char *name = IDENTIFIER_POINTER (id);
4559 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4561 /* Should never be called on a symbol with a preexisting meaning. */
4562 gcc_assert (!I_SYMBOL_BINDING (id));
4564 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4565 UNKNOWN_LOCATION);
4567 /* Builtins in the implementation namespace are made visible without
4568 needing to be explicitly declared. See push_file_scope. */
4569 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4571 DECL_CHAIN (decl) = visible_builtins;
4572 visible_builtins = decl;
4575 return decl;
4578 tree
4579 c_builtin_function_ext_scope (tree decl)
4581 tree type = TREE_TYPE (decl);
4582 tree id = DECL_NAME (decl);
4584 const char *name = IDENTIFIER_POINTER (id);
4585 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4587 if (external_scope)
4588 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4589 UNKNOWN_LOCATION);
4591 /* Builtins in the implementation namespace are made visible without
4592 needing to be explicitly declared. See push_file_scope. */
4593 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4595 DECL_CHAIN (decl) = visible_builtins;
4596 visible_builtins = decl;
4599 return decl;
4602 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4604 tree
4605 c_simulate_builtin_function_decl (tree decl)
4607 tree type = TREE_TYPE (decl);
4608 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4609 return pushdecl (decl);
4612 /* Warn about attributes in a context where they are unused
4613 (attribute-declarations, except for the "fallthrough" case, and
4614 attributes on statements). */
4616 void
4617 c_warn_unused_attributes (tree attrs)
4619 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4620 if (get_attribute_namespace (t) == NULL_TREE)
4621 /* The specifications of standard attributes mean this is a
4622 constraint violation. */
4623 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4624 get_attribute_name (t));
4625 else
4626 warning (OPT_Wattributes, "%qE attribute ignored",
4627 get_attribute_name (t));
4630 /* Warn for standard attributes being applied to a type that is not
4631 being defined, where that is a constraint violation, and return a
4632 list of attributes with them removed. */
4634 tree
4635 c_warn_type_attributes (tree attrs)
4637 tree *attr_ptr = &attrs;
4638 while (*attr_ptr)
4639 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4641 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4642 get_attribute_name (*attr_ptr));
4643 *attr_ptr = TREE_CHAIN (*attr_ptr);
4645 else
4646 attr_ptr = &TREE_CHAIN (*attr_ptr);
4647 return attrs;
4650 /* Called when a declaration is seen that contains no names to declare.
4651 If its type is a reference to a structure, union or enum inherited
4652 from a containing scope, shadow that tag name for the current scope
4653 with a forward reference.
4654 If its type defines a new named structure or union
4655 or defines an enum, it is valid but we need not do anything here.
4656 Otherwise, it is an error. */
4658 void
4659 shadow_tag (const struct c_declspecs *declspecs)
4661 shadow_tag_warned (declspecs, 0);
4664 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4665 but no pedwarn. */
4666 void
4667 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4669 bool found_tag = false;
4671 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4673 tree value = declspecs->type;
4674 enum tree_code code = TREE_CODE (value);
4676 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4677 /* Used to test also that TYPE_SIZE (value) != 0.
4678 That caused warning for `struct foo;' at top level in the file. */
4680 tree name = TYPE_NAME (value);
4681 tree t;
4683 found_tag = true;
4685 if (declspecs->restrict_p)
4687 error ("invalid use of %<restrict%>");
4688 warned = 1;
4691 if (name == NULL_TREE)
4693 if (warned != 1 && code != ENUMERAL_TYPE)
4694 /* Empty unnamed enum OK */
4696 pedwarn (input_location, 0,
4697 "unnamed struct/union that defines no instances");
4698 warned = 1;
4701 else if (declspecs->typespec_kind != ctsk_tagdef
4702 && declspecs->typespec_kind != ctsk_tagfirstref
4703 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4704 && declspecs->storage_class != csc_none)
4706 if (warned != 1)
4707 pedwarn (input_location, 0,
4708 "empty declaration with storage class specifier "
4709 "does not redeclare tag");
4710 warned = 1;
4711 pending_xref_error ();
4713 else if (declspecs->typespec_kind != ctsk_tagdef
4714 && declspecs->typespec_kind != ctsk_tagfirstref
4715 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4716 && (declspecs->const_p
4717 || declspecs->volatile_p
4718 || declspecs->atomic_p
4719 || declspecs->restrict_p
4720 || declspecs->address_space))
4722 if (warned != 1)
4723 pedwarn (input_location, 0,
4724 "empty declaration with type qualifier "
4725 "does not redeclare tag");
4726 warned = 1;
4727 pending_xref_error ();
4729 else if (declspecs->typespec_kind != ctsk_tagdef
4730 && declspecs->typespec_kind != ctsk_tagfirstref
4731 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4732 && declspecs->alignas_p)
4734 if (warned != 1)
4735 pedwarn (input_location, 0,
4736 "empty declaration with %<_Alignas%> "
4737 "does not redeclare tag");
4738 warned = 1;
4739 pending_xref_error ();
4741 else
4743 pending_invalid_xref = NULL_TREE;
4744 t = lookup_tag (code, name, true, NULL);
4746 if (t == NULL_TREE)
4748 t = make_node (code);
4749 pushtag (input_location, name, t);
4753 else
4755 if (warned != 1 && !in_system_header_at (input_location))
4757 pedwarn (input_location, 0,
4758 "useless type name in empty declaration");
4759 warned = 1;
4763 else if (warned != 1 && !in_system_header_at (input_location)
4764 && declspecs->typedef_p)
4766 pedwarn (input_location, 0, "useless type name in empty declaration");
4767 warned = 1;
4770 pending_invalid_xref = NULL_TREE;
4772 if (declspecs->inline_p)
4774 error ("%<inline%> in empty declaration");
4775 warned = 1;
4778 if (declspecs->noreturn_p)
4780 error ("%<_Noreturn%> in empty declaration");
4781 warned = 1;
4784 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4786 error ("%<auto%> in file-scope empty declaration");
4787 warned = 1;
4790 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4792 error ("%<register%> in file-scope empty declaration");
4793 warned = 1;
4796 if (!warned && !in_system_header_at (input_location)
4797 && declspecs->storage_class != csc_none)
4799 warning (0, "useless storage class specifier in empty declaration");
4800 warned = 2;
4803 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4805 warning (0, "useless %qs in empty declaration",
4806 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4807 warned = 2;
4810 if (!warned
4811 && !in_system_header_at (input_location)
4812 && (declspecs->const_p
4813 || declspecs->volatile_p
4814 || declspecs->atomic_p
4815 || declspecs->restrict_p
4816 || declspecs->address_space))
4818 warning (0, "useless type qualifier in empty declaration");
4819 warned = 2;
4822 if (!warned && !in_system_header_at (input_location)
4823 && declspecs->alignas_p)
4825 warning (0, "useless %<_Alignas%> in empty declaration");
4826 warned = 2;
4829 if (found_tag
4830 && warned == 2
4831 && (declspecs->typespec_kind == ctsk_tagref_attrs
4832 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
4834 /* Standard attributes after the "struct" or "union" keyword are
4835 only permitted when the contents of the type are defined, or
4836 in the form "struct-or-union attribute-specifier-sequence
4837 identifier;". If the ';' was not present, attributes were
4838 diagnosed in the parser. Here, ensure that any other useless
4839 elements of the declaration result in a pedwarn, not just a
4840 warning. Forward declarations of enum types are not part of
4841 standard C, but handle them the same. */
4842 pedwarn (input_location, 0,
4843 "invalid use of attributes in empty declaration");
4844 warned = 1;
4847 if (warned != 1)
4849 if (declspecs->declspecs_seen_p
4850 && !declspecs->non_std_attrs_seen_p)
4851 /* An attribute declaration (but not a fallthrough attribute
4852 declaration, which was handled separately); warn if there
4853 are any attributes being ignored (but not if the attributes
4854 were empty). */
4855 c_warn_unused_attributes (declspecs->attrs);
4856 else if (!found_tag)
4857 pedwarn (input_location, 0, "empty declaration");
4862 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4863 bits. SPECS represents declaration specifiers that the grammar
4864 only permits to contain type qualifiers and attributes. */
4867 quals_from_declspecs (const struct c_declspecs *specs)
4869 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4870 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4871 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4872 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4873 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4874 gcc_assert (!specs->type
4875 && !specs->decl_attr
4876 && specs->typespec_word == cts_none
4877 && specs->storage_class == csc_none
4878 && !specs->typedef_p
4879 && !specs->explicit_signed_p
4880 && !specs->deprecated_p
4881 && !specs->long_p
4882 && !specs->long_long_p
4883 && !specs->short_p
4884 && !specs->signed_p
4885 && !specs->unsigned_p
4886 && !specs->complex_p
4887 && !specs->inline_p
4888 && !specs->noreturn_p
4889 && !specs->thread_p);
4890 return quals;
4893 /* Construct an array declarator. LOC is the location of the
4894 beginning of the array (usually the opening brace). EXPR is the
4895 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4896 inside the [] (to be applied to the pointer to which a parameter
4897 array is converted). STATIC_P is true if "static" is inside the
4898 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4899 VLA of unspecified length which is nevertheless a complete type,
4900 false otherwise. The field for the contained declarator is left to
4901 be filled in by set_array_declarator_inner. */
4903 struct c_declarator *
4904 build_array_declarator (location_t loc,
4905 tree expr, struct c_declspecs *quals, bool static_p,
4906 bool vla_unspec_p)
4908 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4909 struct c_declarator);
4910 declarator->id_loc = loc;
4911 declarator->kind = cdk_array;
4912 declarator->declarator = 0;
4913 declarator->u.array.dimen = expr;
4914 if (quals)
4916 declarator->u.array.attrs = quals->attrs;
4917 declarator->u.array.quals = quals_from_declspecs (quals);
4919 else
4921 declarator->u.array.attrs = NULL_TREE;
4922 declarator->u.array.quals = 0;
4924 declarator->u.array.static_p = static_p;
4925 declarator->u.array.vla_unspec_p = vla_unspec_p;
4926 if (static_p || quals != NULL)
4927 pedwarn_c90 (loc, OPT_Wpedantic,
4928 "ISO C90 does not support %<static%> or type "
4929 "qualifiers in parameter array declarators");
4930 if (vla_unspec_p)
4931 pedwarn_c90 (loc, OPT_Wpedantic,
4932 "ISO C90 does not support %<[*]%> array declarators");
4933 if (vla_unspec_p)
4935 if (!current_scope->parm_flag)
4937 /* C99 6.7.5.2p4 */
4938 error_at (loc, "%<[*]%> not allowed in other than "
4939 "function prototype scope");
4940 declarator->u.array.vla_unspec_p = false;
4941 return NULL;
4943 current_scope->had_vla_unspec = true;
4945 return declarator;
4948 /* Set the contained declarator of an array declarator. DECL is the
4949 declarator, as constructed by build_array_declarator; INNER is what
4950 appears on the left of the []. */
4952 struct c_declarator *
4953 set_array_declarator_inner (struct c_declarator *decl,
4954 struct c_declarator *inner)
4956 decl->declarator = inner;
4957 return decl;
4960 /* INIT is a constructor that forms DECL's initializer. If the final
4961 element initializes a flexible array field, add the size of that
4962 initializer to DECL's size. */
4964 static void
4965 add_flexible_array_elts_to_size (tree decl, tree init)
4967 tree elt, type;
4969 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4970 return;
4972 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4973 type = TREE_TYPE (elt);
4974 if (TREE_CODE (type) == ARRAY_TYPE
4975 && TYPE_SIZE (type) == NULL_TREE
4976 && TYPE_DOMAIN (type) != NULL_TREE
4977 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4979 complete_array_type (&type, elt, false);
4980 DECL_SIZE (decl)
4981 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4982 DECL_SIZE_UNIT (decl)
4983 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4987 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4988 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4989 before the type name, and set *EXPR_CONST_OPERANDS, if
4990 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4991 appear in a constant expression. */
4993 tree
4994 groktypename (struct c_type_name *type_name, tree *expr,
4995 bool *expr_const_operands)
4997 tree type;
4998 tree attrs = type_name->specs->attrs;
5000 type_name->specs->attrs = NULL_TREE;
5002 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5003 false, NULL, &attrs, expr, expr_const_operands,
5004 DEPRECATED_NORMAL);
5006 /* Apply attributes. */
5007 attrs = c_warn_type_attributes (attrs);
5008 decl_attributes (&type, attrs, 0);
5010 return type;
5013 /* Looks up the most recent pushed declaration corresponding to DECL. */
5015 static tree
5016 lookup_last_decl (tree decl)
5018 tree last_decl = lookup_name (DECL_NAME (decl));
5019 if (!last_decl)
5020 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5021 return last_decl;
5024 /* Wrapper for decl_attributes that adds some implicit attributes
5025 to VAR_DECLs or FUNCTION_DECLs. */
5027 static tree
5028 c_decl_attributes (tree *node, tree attributes, int flags)
5030 /* Add implicit "omp declare target" attribute if requested. */
5031 if (current_omp_declare_target_attribute
5032 && ((VAR_P (*node) && is_global_var (*node))
5033 || TREE_CODE (*node) == FUNCTION_DECL))
5035 if (VAR_P (*node)
5036 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
5037 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5038 NULL_TREE, attributes);
5039 else
5041 attributes = tree_cons (get_identifier ("omp declare target"),
5042 NULL_TREE, attributes);
5043 attributes = tree_cons (get_identifier ("omp declare target block"),
5044 NULL_TREE, attributes);
5048 /* Look up the current declaration with all the attributes merged
5049 so far so that attributes on the current declaration that's
5050 about to be pushed that conflict with the former can be detected,
5051 diagnosed, and rejected as appropriate. */
5052 tree last_decl = lookup_last_decl (*node);
5053 return decl_attributes (node, attributes, flags, last_decl);
5057 /* Decode a declarator in an ordinary declaration or data definition.
5058 This is called as soon as the type information and variable name
5059 have been parsed, before parsing the initializer if any.
5060 Here we create the ..._DECL node, fill in its type,
5061 and put it on the list of decls for the current context.
5062 When nonnull, set *LASTLOC to the location of the prior declaration
5063 of the same entity if one exists.
5064 The ..._DECL node is returned as the value.
5066 Exception: for arrays where the length is not specified,
5067 the type is left null, to be filled in by `finish_decl'.
5069 Function definitions do not come here; they go to start_function
5070 instead. However, external and forward declarations of functions
5071 do go through here. Structure field declarations are done by
5072 grokfield and not through here. */
5074 tree
5075 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5076 bool initialized, tree attributes, location_t *lastloc /* = NULL */)
5078 tree decl;
5079 tree tem;
5080 tree expr = NULL_TREE;
5081 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5083 /* An object declared as __attribute__((deprecated)) suppresses
5084 warnings of uses of other deprecated items. */
5085 if (lookup_attribute ("deprecated", attributes))
5086 deprecated_state = DEPRECATED_SUPPRESS;
5088 decl = grokdeclarator (declarator, declspecs,
5089 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5090 deprecated_state);
5091 if (!decl || decl == error_mark_node)
5092 return NULL_TREE;
5094 if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5095 if (lastdecl != error_mark_node)
5096 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5098 if (expr)
5099 add_stmt (fold_convert (void_type_node, expr));
5101 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5102 && TREE_PUBLIC (decl))
5103 warning (OPT_Wmain, "%q+D is usually a function", decl);
5105 if (initialized)
5106 /* Is it valid for this decl to have an initializer at all?
5107 If not, set INITIALIZED to zero, which will indirectly
5108 tell 'finish_decl' to ignore the initializer once it is parsed. */
5109 switch (TREE_CODE (decl))
5111 case TYPE_DECL:
5112 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5113 initialized = false;
5114 break;
5116 case FUNCTION_DECL:
5117 error ("function %qD is initialized like a variable", decl);
5118 initialized = false;
5119 break;
5121 case PARM_DECL:
5122 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5123 error ("parameter %qD is initialized", decl);
5124 initialized = false;
5125 break;
5127 default:
5128 /* Don't allow initializations for incomplete types except for
5129 arrays which might be completed by the initialization. */
5131 /* This can happen if the array size is an undefined macro.
5132 We already gave a warning, so we don't need another one. */
5133 if (TREE_TYPE (decl) == error_mark_node)
5134 initialized = false;
5135 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5137 /* A complete type is ok if size is fixed. */
5139 if (!poly_int_tree_p (TYPE_SIZE (TREE_TYPE (decl)))
5140 || C_DECL_VARIABLE_SIZE (decl))
5142 error ("variable-sized object may not be initialized");
5143 initialized = false;
5146 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5148 error ("variable %qD has initializer but incomplete type", decl);
5149 initialized = false;
5151 else if (C_DECL_VARIABLE_SIZE (decl))
5153 /* Although C99 is unclear about whether incomplete arrays
5154 of VLAs themselves count as VLAs, it does not make
5155 sense to permit them to be initialized given that
5156 ordinary VLAs may not be initialized. */
5157 error ("variable-sized object may not be initialized");
5158 initialized = false;
5162 if (initialized)
5164 if (current_scope == file_scope)
5165 TREE_STATIC (decl) = 1;
5167 /* Tell 'pushdecl' this is an initialized decl
5168 even though we don't yet have the initializer expression.
5169 Also tell 'finish_decl' it may store the real initializer. */
5170 DECL_INITIAL (decl) = error_mark_node;
5173 /* If this is a function declaration, write a record describing it to the
5174 prototypes file (if requested). */
5176 if (TREE_CODE (decl) == FUNCTION_DECL)
5177 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5179 /* ANSI specifies that a tentative definition which is not merged with
5180 a non-tentative definition behaves exactly like a definition with an
5181 initializer equal to zero. (Section 3.7.2)
5183 -fno-common gives strict ANSI behavior, though this tends to break
5184 a large body of code that grew up without this rule.
5186 Thread-local variables are never common, since there's no entrenched
5187 body of code to break, and it allows more efficient variable references
5188 in the presence of dynamic linking. */
5190 if (VAR_P (decl)
5191 && !initialized
5192 && TREE_PUBLIC (decl)
5193 && !DECL_THREAD_LOCAL_P (decl)
5194 && !flag_no_common)
5195 DECL_COMMON (decl) = 1;
5197 /* Set attributes here so if duplicate decl, will have proper attributes. */
5198 c_decl_attributes (&decl, attributes, 0);
5200 /* Handle gnu_inline attribute. */
5201 if (declspecs->inline_p
5202 && !flag_gnu89_inline
5203 && TREE_CODE (decl) == FUNCTION_DECL
5204 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5205 || current_function_decl))
5207 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5209 else if (declspecs->storage_class != csc_static)
5210 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5213 if (TREE_CODE (decl) == FUNCTION_DECL
5214 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5216 struct c_declarator *ce = declarator;
5218 if (ce->kind == cdk_pointer)
5219 ce = declarator->declarator;
5220 if (ce->kind == cdk_function)
5222 tree args = ce->u.arg_info->parms;
5223 for (; args; args = DECL_CHAIN (args))
5225 tree type = TREE_TYPE (args);
5226 if (type && INTEGRAL_TYPE_P (type)
5227 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5228 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5233 if (TREE_CODE (decl) == FUNCTION_DECL
5234 && DECL_DECLARED_INLINE_P (decl)
5235 && DECL_UNINLINABLE (decl)
5236 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5237 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5238 decl, "noinline");
5240 /* C99 6.7.4p3: An inline definition of a function with external
5241 linkage shall not contain a definition of a modifiable object
5242 with static storage duration... */
5243 if (VAR_P (decl)
5244 && current_scope != file_scope
5245 && TREE_STATIC (decl)
5246 && !TREE_READONLY (decl)
5247 && DECL_DECLARED_INLINE_P (current_function_decl)
5248 && DECL_EXTERNAL (current_function_decl))
5249 record_inline_static (input_location, current_function_decl,
5250 decl, csi_modifiable);
5252 if (c_dialect_objc ()
5253 && VAR_OR_FUNCTION_DECL_P (decl))
5254 objc_check_global_decl (decl);
5256 /* Add this decl to the current scope.
5257 TEM may equal DECL or it may be a previous decl of the same name. */
5258 tem = pushdecl (decl);
5260 if (initialized && DECL_EXTERNAL (tem))
5262 DECL_EXTERNAL (tem) = 0;
5263 TREE_STATIC (tem) = 1;
5266 return tem;
5269 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5270 DECL or the non-array element type if DECL is an uninitialized array.
5271 If that type has a const member, diagnose this. */
5273 static void
5274 diagnose_uninitialized_cst_member (tree decl, tree type)
5276 tree field;
5277 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5279 tree field_type;
5280 if (TREE_CODE (field) != FIELD_DECL)
5281 continue;
5282 field_type = strip_array_types (TREE_TYPE (field));
5284 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5286 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5287 "uninitialized const member in %qT is invalid in C++",
5288 strip_array_types (TREE_TYPE (decl)));
5289 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5292 if (RECORD_OR_UNION_TYPE_P (field_type))
5293 diagnose_uninitialized_cst_member (decl, field_type);
5297 /* Finish processing of a declaration;
5298 install its initial value.
5299 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5300 If the length of an array type is not known before,
5301 it must be determined now, from the initial value, or it is an error.
5303 INIT_LOC is the location of the initial value. */
5305 void
5306 finish_decl (tree decl, location_t init_loc, tree init,
5307 tree origtype, tree asmspec_tree)
5309 tree type;
5310 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5311 const char *asmspec = 0;
5313 /* If a name was specified, get the string. */
5314 if (VAR_OR_FUNCTION_DECL_P (decl)
5315 && DECL_FILE_SCOPE_P (decl))
5316 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5317 if (asmspec_tree)
5318 asmspec = TREE_STRING_POINTER (asmspec_tree);
5320 if (VAR_P (decl)
5321 && TREE_STATIC (decl)
5322 && global_bindings_p ())
5323 /* So decl is a global variable. Record the types it uses
5324 so that we can decide later to emit debug info for them. */
5325 record_types_used_by_current_var_decl (decl);
5327 /* If `start_decl' didn't like having an initialization, ignore it now. */
5328 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5329 init = NULL_TREE;
5331 /* Don't crash if parm is initialized. */
5332 if (TREE_CODE (decl) == PARM_DECL)
5333 init = NULL_TREE;
5335 if (init)
5336 store_init_value (init_loc, decl, init, origtype);
5338 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5339 || TREE_CODE (decl) == FIELD_DECL))
5340 objc_check_decl (decl);
5342 type = TREE_TYPE (decl);
5344 /* Deduce size of array from initialization, if not already known.
5345 This is only needed for an initialization in the current scope;
5346 it must not be done for a file-scope initialization of a
5347 declaration with external linkage, redeclared in an inner scope
5348 with the outer declaration shadowed in an intermediate scope. */
5349 if (TREE_CODE (type) == ARRAY_TYPE
5350 && TYPE_DOMAIN (type) == NULL_TREE
5351 && TREE_CODE (decl) != TYPE_DECL
5352 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5354 bool do_default
5355 = (TREE_STATIC (decl)
5356 /* Even if pedantic, an external linkage array
5357 may have incomplete type at first. */
5358 ? pedantic && !TREE_PUBLIC (decl)
5359 : !DECL_EXTERNAL (decl));
5360 int failure
5361 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5362 do_default);
5364 /* Get the completed type made by complete_array_type. */
5365 type = TREE_TYPE (decl);
5367 switch (failure)
5369 case 1:
5370 error ("initializer fails to determine size of %q+D", decl);
5371 break;
5373 case 2:
5374 if (do_default)
5375 error ("array size missing in %q+D", decl);
5376 break;
5378 case 3:
5379 error ("zero or negative size array %q+D", decl);
5380 break;
5382 case 0:
5383 /* For global variables, update the copy of the type that
5384 exists in the binding. */
5385 if (TREE_PUBLIC (decl))
5387 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5388 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5389 b_ext = b_ext->shadowed;
5390 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5392 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5393 b_ext->u.type = composite_type (b_ext->u.type, type);
5394 else
5395 b_ext->u.type = type;
5398 break;
5400 default:
5401 gcc_unreachable ();
5404 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5405 TREE_TYPE (DECL_INITIAL (decl)) = type;
5407 relayout_decl (decl);
5410 /* Look for braced array initializers for character arrays and
5411 recursively convert them into STRING_CSTs. */
5412 if (tree init = DECL_INITIAL (decl))
5413 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5415 if (VAR_P (decl))
5417 if (init && TREE_CODE (init) == CONSTRUCTOR)
5418 add_flexible_array_elts_to_size (decl, init);
5420 complete_flexible_array_elts (DECL_INITIAL (decl));
5422 if (is_global_var (decl))
5424 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5425 ? TCTX_THREAD_STORAGE
5426 : TCTX_STATIC_STORAGE);
5427 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5428 TREE_TYPE (decl) = error_mark_node;
5431 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5432 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5433 layout_decl (decl, 0);
5435 if (DECL_SIZE (decl) == NULL_TREE
5436 /* Don't give an error if we already gave one earlier. */
5437 && TREE_TYPE (decl) != error_mark_node
5438 && (TREE_STATIC (decl)
5439 /* A static variable with an incomplete type
5440 is an error if it is initialized.
5441 Also if it is not file scope.
5442 Otherwise, let it through, but if it is not `extern'
5443 then it may cause an error message later. */
5444 ? (DECL_INITIAL (decl) != NULL_TREE
5445 || !DECL_FILE_SCOPE_P (decl))
5446 /* An automatic variable with an incomplete type
5447 is an error. */
5448 : !DECL_EXTERNAL (decl)))
5450 error ("storage size of %q+D isn%'t known", decl);
5451 TREE_TYPE (decl) = error_mark_node;
5454 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5455 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5456 && DECL_SIZE (decl) == NULL_TREE
5457 && TREE_STATIC (decl))
5458 incomplete_record_decls.safe_push (decl);
5460 if (is_global_var (decl)
5461 && DECL_SIZE (decl) != NULL_TREE
5462 && TREE_TYPE (decl) != error_mark_node)
5464 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5465 constant_expression_warning (DECL_SIZE (decl));
5466 else
5468 error ("storage size of %q+D isn%'t constant", decl);
5469 TREE_TYPE (decl) = error_mark_node;
5473 if (TREE_USED (type))
5475 TREE_USED (decl) = 1;
5476 DECL_READ_P (decl) = 1;
5480 /* If this is a function and an assembler name is specified, reset DECL_RTL
5481 so we can give it its new name. Also, update builtin_decl if it
5482 was a normal built-in. */
5483 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5485 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5486 set_builtin_user_assembler_name (decl, asmspec);
5487 set_user_assembler_name (decl, asmspec);
5490 /* If #pragma weak was used, mark the decl weak now. */
5491 maybe_apply_pragma_weak (decl);
5493 /* Output the assembler code and/or RTL code for variables and functions,
5494 unless the type is an undefined structure or union.
5495 If not, it will get done when the type is completed. */
5497 if (VAR_OR_FUNCTION_DECL_P (decl))
5499 /* Determine the ELF visibility. */
5500 if (TREE_PUBLIC (decl))
5501 c_determine_visibility (decl);
5503 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5504 if (c_dialect_objc ())
5505 objc_check_decl (decl);
5507 if (asmspec)
5509 /* If this is not a static variable, issue a warning.
5510 It doesn't make any sense to give an ASMSPEC for an
5511 ordinary, non-register local variable. Historically,
5512 GCC has accepted -- but ignored -- the ASMSPEC in
5513 this case. */
5514 if (!DECL_FILE_SCOPE_P (decl)
5515 && VAR_P (decl)
5516 && !C_DECL_REGISTER (decl)
5517 && !TREE_STATIC (decl))
5518 warning (0, "ignoring %<asm%> specifier for non-static local "
5519 "variable %q+D", decl);
5520 else
5521 set_user_assembler_name (decl, asmspec);
5524 if (DECL_FILE_SCOPE_P (decl))
5526 if (DECL_INITIAL (decl) == NULL_TREE
5527 || DECL_INITIAL (decl) == error_mark_node)
5528 /* Don't output anything
5529 when a tentative file-scope definition is seen.
5530 But at end of compilation, do output code for them. */
5531 DECL_DEFER_OUTPUT (decl) = 1;
5532 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5533 DECL_HARD_REGISTER (decl) = 1;
5534 rest_of_decl_compilation (decl, true, 0);
5536 if (TREE_CODE (decl) == FUNCTION_DECL)
5538 tree parms = DECL_ARGUMENTS (decl);
5539 const bool builtin = fndecl_built_in_p (decl);
5540 if (tree access = build_attr_access_from_parms (parms, !builtin))
5541 decl_attributes (&decl, access, 0);
5544 else
5546 /* In conjunction with an ASMSPEC, the `register'
5547 keyword indicates that we should place the variable
5548 in a particular register. */
5549 if (asmspec && C_DECL_REGISTER (decl))
5551 DECL_HARD_REGISTER (decl) = 1;
5552 /* This cannot be done for a structure with volatile
5553 fields, on which DECL_REGISTER will have been
5554 reset. */
5555 if (!DECL_REGISTER (decl))
5556 error ("cannot put object with volatile field into register");
5559 if (TREE_CODE (decl) != FUNCTION_DECL)
5561 /* If we're building a variable sized type, and we might be
5562 reachable other than via the top of the current binding
5563 level, then create a new BIND_EXPR so that we deallocate
5564 the object at the right time. */
5565 /* Note that DECL_SIZE can be null due to errors. */
5566 if (DECL_SIZE (decl)
5567 && !TREE_CONSTANT (DECL_SIZE (decl))
5568 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5570 tree bind;
5571 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5572 TREE_SIDE_EFFECTS (bind) = 1;
5573 add_stmt (bind);
5574 BIND_EXPR_BODY (bind) = push_stmt_list ();
5576 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5577 DECL_EXPR, decl));
5582 if (!DECL_FILE_SCOPE_P (decl))
5584 /* Recompute the RTL of a local array now
5585 if it used to be an incomplete type. */
5586 if (was_incomplete && !is_global_var (decl))
5588 /* If we used it already as memory, it must stay in memory. */
5589 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5590 /* If it's still incomplete now, no init will save it. */
5591 if (DECL_SIZE (decl) == NULL_TREE)
5592 DECL_INITIAL (decl) = NULL_TREE;
5597 if (TREE_CODE (decl) == TYPE_DECL)
5599 if (!DECL_FILE_SCOPE_P (decl)
5600 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5601 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5603 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5606 /* Install a cleanup (aka destructor) if one was given. */
5607 if (VAR_P (decl) && !TREE_STATIC (decl))
5609 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5610 if (attr)
5612 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5613 tree cleanup_decl = lookup_name (cleanup_id);
5614 tree cleanup;
5615 vec<tree, va_gc> *v;
5617 /* Build "cleanup(&decl)" for the destructor. */
5618 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5619 vec_alloc (v, 1);
5620 v->quick_push (cleanup);
5621 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5622 vNULL, cleanup_decl, v, NULL);
5623 vec_free (v);
5625 /* Don't warn about decl unused; the cleanup uses it. */
5626 TREE_USED (decl) = 1;
5627 TREE_USED (cleanup_decl) = 1;
5628 DECL_READ_P (decl) = 1;
5630 push_cleanup (decl, cleanup, false);
5634 if (warn_cxx_compat
5635 && VAR_P (decl)
5636 && !DECL_EXTERNAL (decl)
5637 && DECL_INITIAL (decl) == NULL_TREE)
5639 type = strip_array_types (type);
5640 if (TREE_READONLY (decl))
5641 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5642 "uninitialized %<const %D%> is invalid in C++", decl);
5643 else if (RECORD_OR_UNION_TYPE_P (type)
5644 && C_TYPE_FIELDS_READONLY (type))
5645 diagnose_uninitialized_cst_member (decl, type);
5648 if (flag_openmp
5649 && VAR_P (decl)
5650 && lookup_attribute ("omp declare target implicit",
5651 DECL_ATTRIBUTES (decl)))
5653 DECL_ATTRIBUTES (decl)
5654 = remove_attribute ("omp declare target implicit",
5655 DECL_ATTRIBUTES (decl));
5656 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5657 error ("%q+D in declare target directive does not have mappable type",
5658 decl);
5659 else if (!lookup_attribute ("omp declare target",
5660 DECL_ATTRIBUTES (decl))
5661 && !lookup_attribute ("omp declare target link",
5662 DECL_ATTRIBUTES (decl)))
5664 DECL_ATTRIBUTES (decl)
5665 = tree_cons (get_identifier ("omp declare target"),
5666 NULL_TREE, DECL_ATTRIBUTES (decl));
5667 symtab_node *node = symtab_node::get (decl);
5668 if (node != NULL)
5670 node->offloadable = 1;
5671 if (ENABLE_OFFLOADING)
5673 g->have_offload = true;
5674 if (is_a <varpool_node *> (node))
5675 vec_safe_push (offload_vars, decl);
5681 /* This is the last point we can lower alignment so give the target the
5682 chance to do so. */
5683 if (VAR_P (decl)
5684 && !is_global_var (decl)
5685 && !DECL_HARD_REGISTER (decl))
5686 targetm.lower_local_decl_alignment (decl);
5688 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5691 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5692 EXPR is NULL or a pointer to an expression that needs to be
5693 evaluated for the side effects of array size expressions in the
5694 parameters. */
5696 tree
5697 grokparm (const struct c_parm *parm, tree *expr)
5699 tree attrs = parm->attrs;
5700 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5701 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5703 decl_attributes (&decl, attrs, 0);
5705 return decl;
5708 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5709 described by PARM, concatenated onto attributes ATTRS.
5710 The spec consists of one dollar symbol for each specified variable
5711 bound, one asterisk for each unspecified variable bound, followed
5712 by at most one specification of the most significant bound of
5713 an ordinary array parameter. For ordinary arrays the specification
5714 is either the constant bound itself, or the space character for
5715 an array with an unspecified bound (the [] form). Finally, a chain
5716 of specified variable bounds is appended to the spec, starting with
5717 the most significant bound. For example, the PARM T a[2][m][3][n]
5718 will produce __attribute__((arg spec ("[$$2]", m, n)).
5719 For T a typedef for an array with variable bounds, the bounds are
5720 included in the specification in the expected order.
5721 No "arg spec" is created for parameters of pointer types, making
5722 a distinction between T(*)[N] (or, equivalently, T[][N]) and
5723 the T[M][N] form, all of which have the same type and are represented
5724 the same, but only the last of which gets an "arg spec" describing
5725 the most significant bound M. */
5727 static tree
5728 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5730 /* The attribute specification string, minor bound first. */
5731 std::string spec;
5733 /* A list of VLA variable bounds, major first, or null if unspecified
5734 or not a VLA. */
5735 tree vbchain = NULL_TREE;
5736 /* True for a pointer parameter. */
5737 bool pointer = false;
5738 /* True for an ordinary array with an unpecified bound. */
5739 bool nobound = false;
5741 /* Create a string representation for the bounds of the array/VLA. */
5742 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
5744 next = pd->declarator;
5745 while (next && next->kind == cdk_attrs)
5746 next = next->declarator;
5748 /* Remember if a pointer has been seen to avoid storing the constant
5749 bound. */
5750 if (pd->kind == cdk_pointer)
5751 pointer = true;
5753 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
5754 && (!next || next->kind == cdk_id))
5756 /* Do nothing for the common case of a pointer. The fact that
5757 the parameter is one can be deduced from the absence of
5758 an arg spec for it. */
5759 return attrs;
5762 if (pd->kind == cdk_id)
5764 if (pointer
5765 || !parm->specs->type
5766 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
5767 || !TYPE_DOMAIN (parm->specs->type)
5768 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
5769 continue;
5771 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
5772 if (!vbchain
5773 && TREE_CODE (max) == INTEGER_CST)
5775 /* Extract the upper bound from a parameter of an array type
5776 unless the parameter is an ordinary array of unspecified
5777 bound in which case a next iteration of the loop will
5778 exit. */
5779 if (spec.empty () || spec.end ()[-1] != ' ')
5781 if (!tree_fits_shwi_p (max))
5782 continue;
5784 /* The upper bound is the value of the largest valid
5785 index. */
5786 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
5787 char buf[40];
5788 sprintf (buf, "%lu", (unsigned long)n);
5789 spec += buf;
5791 continue;
5794 /* For a VLA typedef, create a list of its variable bounds and
5795 append it in the expected order to VBCHAIN. */
5796 tree tpbnds = NULL_TREE;
5797 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
5798 type = TREE_TYPE (type))
5800 tree nelts = array_type_nelts (type);
5801 if (error_operand_p (nelts))
5802 return attrs;
5803 if (TREE_CODE (nelts) != INTEGER_CST)
5805 /* Each variable VLA bound is represented by the dollar
5806 sign. */
5807 spec += "$";
5808 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
5811 tpbnds = nreverse (tpbnds);
5812 vbchain = chainon (vbchain, tpbnds);
5813 continue;
5816 if (pd->kind != cdk_array)
5817 continue;
5819 if (pd->u.array.vla_unspec_p)
5821 /* Each unspecified bound is represented by a star. There
5822 can be any number of these in a declaration (but none in
5823 a definition). */
5824 spec += '*';
5825 continue;
5828 tree nelts = pd->u.array.dimen;
5829 if (!nelts)
5831 /* Ordinary array of unspecified size. There can be at most
5832 one for the most significant bound. Exit on the next
5833 iteration which determines whether or not PARM is declared
5834 as a pointer or an array. */
5835 nobound = true;
5836 continue;
5839 if (pd->u.array.static_p)
5840 spec += 's';
5842 if (TREE_CODE (nelts) == INTEGER_CST)
5844 /* Skip all constant bounds except the most significant one.
5845 The interior ones are included in the array type. */
5846 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
5847 continue;
5849 if (!tree_fits_uhwi_p (nelts))
5850 /* Bail completely on invalid bounds. */
5851 return attrs;
5853 char buf[40];
5854 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
5855 sprintf (buf, "%llu", (unsigned long long)n);
5856 spec += buf;
5857 break;
5859 else if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
5860 /* Avoid invalid NELTS. */
5861 return attrs;
5863 /* Each variable VLA bound is represented by a dollar sign. */
5864 spec += "$";
5865 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
5868 if (spec.empty () && !nobound)
5869 return attrs;
5871 spec.insert (0, "[");
5872 if (nobound)
5873 /* Ordinary array of unspecified bound is represented by a space.
5874 It must be last in the spec. */
5875 spec += ' ';
5876 spec += ']';
5878 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
5879 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
5880 tree name = get_identifier ("arg spec");
5881 return tree_cons (name, args, attrs);
5884 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5885 and push that on the current scope. EXPR is a pointer to an
5886 expression that needs to be evaluated for the side effects of array
5887 size expressions in the parameters. */
5889 void
5890 push_parm_decl (const struct c_parm *parm, tree *expr)
5892 tree attrs = parm->attrs;
5893 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5894 &attrs, expr, NULL, DEPRECATED_NORMAL);
5895 if (decl && DECL_P (decl))
5896 DECL_SOURCE_LOCATION (decl) = parm->loc;
5898 attrs = get_parm_array_spec (parm, attrs);
5899 decl_attributes (&decl, attrs, 0);
5901 decl = pushdecl (decl);
5903 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5906 /* Mark all the parameter declarations to date as forward decls.
5907 Also diagnose use of this extension. */
5909 void
5910 mark_forward_parm_decls (void)
5912 struct c_binding *b;
5914 if (pedantic && !current_scope->warned_forward_parm_decls)
5916 pedwarn (input_location, OPT_Wpedantic,
5917 "ISO C forbids forward parameter declarations");
5918 current_scope->warned_forward_parm_decls = true;
5921 for (b = current_scope->bindings; b; b = b->prev)
5922 if (TREE_CODE (b->decl) == PARM_DECL)
5923 TREE_ASM_WRITTEN (b->decl) = 1;
5926 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5927 literal, which may be an incomplete array type completed by the
5928 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5929 literal. NON_CONST is true if the initializers contain something
5930 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5931 it is the (valid) alignment for this compound literal, as specified
5932 with _Alignas. */
5934 tree
5935 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5936 unsigned int alignas_align)
5938 /* We do not use start_decl here because we have a type, not a declarator;
5939 and do not use finish_decl because the decl should be stored inside
5940 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5941 tree decl;
5942 tree complit;
5943 tree stmt;
5945 if (type == error_mark_node
5946 || init == error_mark_node)
5947 return error_mark_node;
5949 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5950 DECL_EXTERNAL (decl) = 0;
5951 TREE_PUBLIC (decl) = 0;
5952 TREE_STATIC (decl) = (current_scope == file_scope);
5953 DECL_CONTEXT (decl) = current_function_decl;
5954 TREE_USED (decl) = 1;
5955 DECL_READ_P (decl) = 1;
5956 DECL_ARTIFICIAL (decl) = 1;
5957 DECL_IGNORED_P (decl) = 1;
5958 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
5959 TREE_TYPE (decl) = type;
5960 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5961 if (alignas_align)
5963 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5964 DECL_USER_ALIGN (decl) = 1;
5966 store_init_value (loc, decl, init, NULL_TREE);
5968 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5970 int failure = complete_array_type (&TREE_TYPE (decl),
5971 DECL_INITIAL (decl), true);
5972 /* If complete_array_type returns 3, it means that the
5973 initial value of the compound literal is empty. Allow it. */
5974 gcc_assert (failure == 0 || failure == 3);
5976 type = TREE_TYPE (decl);
5977 TREE_TYPE (DECL_INITIAL (decl)) = type;
5980 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5982 c_incomplete_type_error (loc, NULL_TREE, type);
5983 return error_mark_node;
5986 if (TREE_STATIC (decl)
5987 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
5988 return error_mark_node;
5990 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5991 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5992 TREE_SIDE_EFFECTS (complit) = 1;
5994 layout_decl (decl, 0);
5996 if (TREE_STATIC (decl))
5998 /* This decl needs a name for the assembler output. */
5999 set_compound_literal_name (decl);
6000 DECL_DEFER_OUTPUT (decl) = 1;
6001 DECL_COMDAT (decl) = 1;
6002 pushdecl (decl);
6003 rest_of_decl_compilation (decl, 1, 0);
6005 else if (current_function_decl && !current_scope->parm_flag)
6006 pushdecl (decl);
6008 if (non_const)
6010 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6011 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6014 return complit;
6017 /* Check the type of a compound literal. Here we just check that it
6018 is valid for C++. */
6020 void
6021 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6023 if (warn_cxx_compat
6024 && (type_name->specs->typespec_kind == ctsk_tagdef
6025 || type_name->specs->typespec_kind == ctsk_tagfirstref
6026 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6027 warning_at (loc, OPT_Wc___compat,
6028 "defining a type in a compound literal is invalid in C++");
6031 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6032 replacing with appropriate values if they are invalid. */
6034 static void
6035 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6036 tree orig_name)
6038 tree type_mv;
6039 unsigned int max_width;
6040 unsigned HOST_WIDE_INT w;
6041 const char *name = (orig_name
6042 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6043 : _("<anonymous>"));
6045 /* Detect and ignore out of range field width and process valid
6046 field widths. */
6047 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6049 error_at (loc, "bit-field %qs width not an integer constant", name);
6050 *width = integer_one_node;
6052 else
6054 if (TREE_CODE (*width) != INTEGER_CST)
6056 *width = c_fully_fold (*width, false, NULL);
6057 if (TREE_CODE (*width) == INTEGER_CST)
6058 pedwarn (loc, OPT_Wpedantic,
6059 "bit-field %qs width not an integer constant expression",
6060 name);
6062 if (TREE_CODE (*width) != INTEGER_CST)
6064 error_at (loc, "bit-field %qs width not an integer constant", name);
6065 *width = integer_one_node;
6067 constant_expression_warning (*width);
6068 if (tree_int_cst_sgn (*width) < 0)
6070 error_at (loc, "negative width in bit-field %qs", name);
6071 *width = integer_one_node;
6073 else if (integer_zerop (*width) && orig_name)
6075 error_at (loc, "zero width for bit-field %qs", name);
6076 *width = integer_one_node;
6080 /* Detect invalid bit-field type. */
6081 if (TREE_CODE (*type) != INTEGER_TYPE
6082 && TREE_CODE (*type) != BOOLEAN_TYPE
6083 && TREE_CODE (*type) != ENUMERAL_TYPE)
6085 error_at (loc, "bit-field %qs has invalid type", name);
6086 *type = unsigned_type_node;
6089 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6091 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6092 name);
6093 *type = unsigned_type_node;
6096 type_mv = TYPE_MAIN_VARIANT (*type);
6097 if (!in_system_header_at (input_location)
6098 && type_mv != integer_type_node
6099 && type_mv != unsigned_type_node
6100 && type_mv != boolean_type_node)
6101 pedwarn_c90 (loc, OPT_Wpedantic,
6102 "type of bit-field %qs is a GCC extension", name);
6104 max_width = TYPE_PRECISION (*type);
6106 if (compare_tree_int (*width, max_width) > 0)
6108 error_at (loc, "width of %qs exceeds its type", name);
6109 w = max_width;
6110 *width = build_int_cst (integer_type_node, w);
6112 else
6113 w = tree_to_uhwi (*width);
6115 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6117 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6118 if (!lt
6119 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6120 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6121 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6127 /* Print warning about variable length array if necessary. */
6129 static void
6130 warn_variable_length_array (tree name, tree size)
6132 if (TREE_CONSTANT (size))
6134 if (name)
6135 pedwarn_c90 (input_location, OPT_Wvla,
6136 "ISO C90 forbids array %qE whose size "
6137 "cannot be evaluated", name);
6138 else
6139 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6140 "whose size cannot be evaluated");
6142 else
6144 if (name)
6145 pedwarn_c90 (input_location, OPT_Wvla,
6146 "ISO C90 forbids variable length array %qE", name);
6147 else
6148 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6149 "length array");
6153 /* Print warning about defaulting to int if necessary. */
6155 static void
6156 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6158 diagnostic_info diagnostic;
6159 va_list ap;
6160 rich_location richloc (line_table, location);
6162 va_start (ap, gmsgid);
6163 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6164 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6165 diagnostic.option_index = opt;
6166 diagnostic_report_diagnostic (global_dc, &diagnostic);
6167 va_end (ap);
6170 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6171 considering only those c_declspec_words found in LIST, which
6172 must be terminated by cdw_number_of_elements. */
6174 static location_t
6175 smallest_type_quals_location (const location_t *locations,
6176 const c_declspec_word *list)
6178 location_t loc = UNKNOWN_LOCATION;
6179 while (*list != cdw_number_of_elements)
6181 location_t newloc = locations[*list];
6182 if (loc == UNKNOWN_LOCATION
6183 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6184 loc = newloc;
6185 list++;
6188 return loc;
6191 /* Given declspecs and a declarator,
6192 determine the name and type of the object declared
6193 and construct a ..._DECL node for it.
6194 (In one case we can return a ..._TYPE node instead.
6195 For invalid input we sometimes return NULL_TREE.)
6197 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6199 DECL_CONTEXT says which syntactic context this declaration is in:
6200 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6201 FUNCDEF for a function definition. Like NORMAL but a few different
6202 error messages in each case. Return value may be zero meaning
6203 this definition is too screwy to try to parse.
6204 PARM for a parameter declaration (either within a function prototype
6205 or before a function body). Make a PARM_DECL, or return void_type_node.
6206 TYPENAME if for a typename (in a cast or sizeof).
6207 Don't make a DECL node; just return the ..._TYPE node.
6208 FIELD for a struct or union field; make a FIELD_DECL.
6209 INITIALIZED is true if the decl has an initializer.
6210 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6211 representing the width of the bit-field.
6212 DECL_ATTRS points to the list of attributes that should be added to this
6213 decl. Any nested attributes that belong on the decl itself will be
6214 added to this list.
6215 If EXPR is not NULL, any expressions that need to be evaluated as
6216 part of evaluating variably modified types will be stored in *EXPR.
6217 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6218 set to indicate whether operands in *EXPR can be used in constant
6219 expressions.
6220 DEPRECATED_STATE is a deprecated_states value indicating whether
6221 deprecation warnings should be suppressed.
6223 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6224 It may also be so in the PARM case, for a prototype where the
6225 argument type is specified but not the name.
6227 This function is where the complicated C meanings of `static'
6228 and `extern' are interpreted. */
6230 static tree
6231 grokdeclarator (const struct c_declarator *declarator,
6232 struct c_declspecs *declspecs,
6233 enum decl_context decl_context, bool initialized, tree *width,
6234 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6235 enum deprecated_states deprecated_state)
6237 tree type = declspecs->type;
6238 bool threadp = declspecs->thread_p;
6239 enum c_storage_class storage_class = declspecs->storage_class;
6240 int constp;
6241 int restrictp;
6242 int volatilep;
6243 int atomicp;
6244 int type_quals = TYPE_UNQUALIFIED;
6245 tree name = NULL_TREE;
6246 bool funcdef_flag = false;
6247 bool funcdef_syntax = false;
6248 bool size_varies = false;
6249 tree decl_attr = declspecs->decl_attr;
6250 int array_ptr_quals = TYPE_UNQUALIFIED;
6251 tree array_ptr_attrs = NULL_TREE;
6252 bool array_parm_static = false;
6253 bool array_parm_vla_unspec_p = false;
6254 tree returned_attrs = NULL_TREE;
6255 tree decl_id_attrs = NULL_TREE;
6256 bool bitfield = width != NULL;
6257 tree element_type;
6258 tree orig_qual_type = NULL;
6259 size_t orig_qual_indirect = 0;
6260 struct c_arg_info *arg_info = 0;
6261 addr_space_t as1, as2, address_space;
6262 location_t loc = UNKNOWN_LOCATION;
6263 tree expr_dummy;
6264 bool expr_const_operands_dummy;
6265 enum c_declarator_kind first_non_attr_kind;
6266 unsigned int alignas_align = 0;
6268 if (TREE_CODE (type) == ERROR_MARK)
6269 return error_mark_node;
6270 if (expr == NULL)
6272 expr = &expr_dummy;
6273 expr_dummy = NULL_TREE;
6275 if (expr_const_operands == NULL)
6276 expr_const_operands = &expr_const_operands_dummy;
6278 if (declspecs->expr)
6280 if (*expr)
6281 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6282 declspecs->expr);
6283 else
6284 *expr = declspecs->expr;
6286 *expr_const_operands = declspecs->expr_const_operands;
6288 if (decl_context == FUNCDEF)
6289 funcdef_flag = true, decl_context = NORMAL;
6291 /* Look inside a declarator for the name being declared
6292 and get it as an IDENTIFIER_NODE, for an error message. */
6294 const struct c_declarator *decl = declarator;
6296 first_non_attr_kind = cdk_attrs;
6297 while (decl)
6298 switch (decl->kind)
6300 case cdk_array:
6301 loc = decl->id_loc;
6302 /* FALL THRU. */
6304 case cdk_function:
6305 case cdk_pointer:
6306 funcdef_syntax = (decl->kind == cdk_function);
6307 if (first_non_attr_kind == cdk_attrs)
6308 first_non_attr_kind = decl->kind;
6309 decl = decl->declarator;
6310 break;
6312 case cdk_attrs:
6313 decl = decl->declarator;
6314 break;
6316 case cdk_id:
6317 loc = decl->id_loc;
6318 if (decl->u.id.id)
6319 name = decl->u.id.id;
6320 decl_id_attrs = decl->u.id.attrs;
6321 if (first_non_attr_kind == cdk_attrs)
6322 first_non_attr_kind = decl->kind;
6323 decl = 0;
6324 break;
6326 default:
6327 gcc_unreachable ();
6329 if (name == NULL_TREE)
6331 gcc_assert (decl_context == PARM
6332 || decl_context == TYPENAME
6333 || (decl_context == FIELD
6334 && declarator->kind == cdk_id));
6335 gcc_assert (!initialized);
6339 /* A function definition's declarator must have the form of
6340 a function declarator. */
6342 if (funcdef_flag && !funcdef_syntax)
6343 return NULL_TREE;
6345 /* If this looks like a function definition, make it one,
6346 even if it occurs where parms are expected.
6347 Then store_parm_decls will reject it and not use it as a parm. */
6348 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6349 decl_context = PARM;
6351 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
6352 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6354 if ((decl_context == NORMAL || decl_context == FIELD)
6355 && current_scope == file_scope
6356 && variably_modified_type_p (type, NULL_TREE))
6358 if (name)
6359 error_at (loc, "variably modified %qE at file scope", name);
6360 else
6361 error_at (loc, "variably modified field at file scope");
6362 type = integer_type_node;
6365 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6367 /* Diagnose defaulting to "int". */
6369 if (declspecs->default_int_p && !in_system_header_at (input_location))
6371 /* Issue a warning if this is an ISO C 99 program or if
6372 -Wreturn-type and this is a function, or if -Wimplicit;
6373 prefer the former warning since it is more explicit. */
6374 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6375 && funcdef_flag)
6376 warn_about_return_type = 1;
6377 else
6379 if (name)
6380 warn_defaults_to (loc, OPT_Wimplicit_int,
6381 "type defaults to %<int%> in declaration "
6382 "of %qE", name);
6383 else
6384 warn_defaults_to (loc, OPT_Wimplicit_int,
6385 "type defaults to %<int%> in type name");
6389 /* Adjust the type if a bit-field is being declared,
6390 -funsigned-bitfields applied and the type is not explicitly
6391 "signed". */
6392 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6393 && TREE_CODE (type) == INTEGER_TYPE)
6394 type = unsigned_type_for (type);
6396 /* Figure out the type qualifiers for the declaration. There are
6397 two ways a declaration can become qualified. One is something
6398 like `const int i' where the `const' is explicit. Another is
6399 something like `typedef const int CI; CI i' where the type of the
6400 declaration contains the `const'. A third possibility is that
6401 there is a type qualifier on the element type of a typedefed
6402 array type, in which case we should extract that qualifier so
6403 that c_apply_type_quals_to_decl receives the full list of
6404 qualifiers to work with (C90 is not entirely clear about whether
6405 duplicate qualifiers should be diagnosed in this case, but it
6406 seems most appropriate to do so). */
6407 element_type = strip_array_types (type);
6408 constp = declspecs->const_p + TYPE_READONLY (element_type);
6409 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6410 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6411 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6412 as1 = declspecs->address_space;
6413 as2 = TYPE_ADDR_SPACE (element_type);
6414 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6416 if (constp > 1)
6417 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6418 if (restrictp > 1)
6419 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6420 if (volatilep > 1)
6421 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6422 if (atomicp > 1)
6423 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6425 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6426 error_at (loc, "conflicting named address spaces (%s vs %s)",
6427 c_addr_space_name (as1), c_addr_space_name (as2));
6429 if ((TREE_CODE (type) == ARRAY_TYPE
6430 || first_non_attr_kind == cdk_array)
6431 && TYPE_QUALS (element_type))
6433 orig_qual_type = type;
6434 type = TYPE_MAIN_VARIANT (type);
6436 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6437 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6438 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6439 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6440 | ENCODE_QUAL_ADDR_SPACE (address_space));
6441 if (type_quals != TYPE_QUALS (element_type))
6442 orig_qual_type = NULL_TREE;
6444 /* Applying the _Atomic qualifier to an array type (through the use
6445 of typedefs or typeof) must be detected here. If the qualifier
6446 is introduced later, any appearance of applying it to an array is
6447 actually applying it to an element of that array. */
6448 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6449 error_at (loc, "%<_Atomic%>-qualified array type");
6451 /* Warn about storage classes that are invalid for certain
6452 kinds of declarations (parameters, typenames, etc.). */
6454 if (funcdef_flag
6455 && (threadp
6456 || storage_class == csc_auto
6457 || storage_class == csc_register
6458 || storage_class == csc_typedef))
6460 if (storage_class == csc_auto)
6461 pedwarn (loc,
6462 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6463 "function definition declared %<auto%>");
6464 if (storage_class == csc_register)
6465 error_at (loc, "function definition declared %<register%>");
6466 if (storage_class == csc_typedef)
6467 error_at (loc, "function definition declared %<typedef%>");
6468 if (threadp)
6469 error_at (loc, "function definition declared %qs",
6470 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6471 threadp = false;
6472 if (storage_class == csc_auto
6473 || storage_class == csc_register
6474 || storage_class == csc_typedef)
6475 storage_class = csc_none;
6477 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6479 if (decl_context == PARM && storage_class == csc_register)
6481 else
6483 switch (decl_context)
6485 case FIELD:
6486 if (name)
6487 error_at (loc, "storage class specified for structure "
6488 "field %qE", name);
6489 else
6490 error_at (loc, "storage class specified for structure field");
6491 break;
6492 case PARM:
6493 if (name)
6494 error_at (loc, "storage class specified for parameter %qE",
6495 name);
6496 else
6497 error_at (loc, "storage class specified for unnamed parameter");
6498 break;
6499 default:
6500 error_at (loc, "storage class specified for typename");
6501 break;
6503 storage_class = csc_none;
6504 threadp = false;
6507 else if (storage_class == csc_extern
6508 && initialized
6509 && !funcdef_flag)
6511 /* 'extern' with initialization is invalid if not at file scope. */
6512 if (current_scope == file_scope)
6514 /* It is fine to have 'extern const' when compiling at C
6515 and C++ intersection. */
6516 if (!(warn_cxx_compat && constp))
6517 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6518 name);
6520 else
6521 error_at (loc, "%qE has both %<extern%> and initializer", name);
6523 else if (current_scope == file_scope)
6525 if (storage_class == csc_auto)
6526 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6527 name);
6528 if (pedantic && storage_class == csc_register)
6529 pedwarn (input_location, OPT_Wpedantic,
6530 "file-scope declaration of %qE specifies %<register%>", name);
6532 else
6534 if (storage_class == csc_extern && funcdef_flag)
6535 error_at (loc, "nested function %qE declared %<extern%>", name);
6536 else if (threadp && storage_class == csc_none)
6538 error_at (loc, "function-scope %qE implicitly auto and declared "
6539 "%qs", name,
6540 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6541 threadp = false;
6545 /* Now figure out the structure of the declarator proper.
6546 Descend through it, creating more complex types, until we reach
6547 the declared identifier (or NULL_TREE, in an absolute declarator).
6548 At each stage we maintain an unqualified version of the type
6549 together with any qualifiers that should be applied to it with
6550 c_build_qualified_type; this way, array types including
6551 multidimensional array types are first built up in unqualified
6552 form and then the qualified form is created with
6553 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6555 while (declarator && declarator->kind != cdk_id)
6557 if (type == error_mark_node)
6559 declarator = declarator->declarator;
6560 continue;
6563 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6564 a cdk_pointer (for *...),
6565 a cdk_function (for ...(...)),
6566 a cdk_attrs (for nested attributes),
6567 or a cdk_id (for the name being declared
6568 or the place in an absolute declarator
6569 where the name was omitted).
6570 For the last case, we have just exited the loop.
6572 At this point, TYPE is the type of elements of an array,
6573 or for a function to return, or for a pointer to point to.
6574 After this sequence of ifs, TYPE is the type of the
6575 array or function or pointer, and DECLARATOR has had its
6576 outermost layer removed. */
6578 if (array_ptr_quals != TYPE_UNQUALIFIED
6579 || array_ptr_attrs != NULL_TREE
6580 || array_parm_static)
6582 /* Only the innermost declarator (making a parameter be of
6583 array type which is converted to pointer type)
6584 may have static or type qualifiers. */
6585 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6586 array_ptr_quals = TYPE_UNQUALIFIED;
6587 array_ptr_attrs = NULL_TREE;
6588 array_parm_static = false;
6591 switch (declarator->kind)
6593 case cdk_attrs:
6595 /* A declarator with embedded attributes. */
6596 tree attrs = declarator->u.attrs;
6597 const struct c_declarator *inner_decl;
6598 int attr_flags = 0;
6599 declarator = declarator->declarator;
6600 /* Standard attribute syntax precisely defines what entity
6601 an attribute in each position appertains to, so only
6602 apply laxity about positioning to GNU attribute syntax.
6603 Standard attributes applied to a function or array
6604 declarator apply exactly to that type; standard
6605 attributes applied to the identifier apply to the
6606 declaration rather than to the type, and are specified
6607 using a cdk_id declarator rather than using
6608 cdk_attrs. */
6609 inner_decl = declarator;
6610 while (inner_decl->kind == cdk_attrs)
6611 inner_decl = inner_decl->declarator;
6612 if (!cxx11_attribute_p (attrs))
6614 if (inner_decl->kind == cdk_id)
6615 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6616 else if (inner_decl->kind == cdk_function)
6617 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6618 else if (inner_decl->kind == cdk_array)
6619 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6621 attrs = c_warn_type_attributes (attrs);
6622 returned_attrs = decl_attributes (&type,
6623 chainon (returned_attrs, attrs),
6624 attr_flags);
6625 break;
6627 case cdk_array:
6629 tree itype = NULL_TREE;
6630 tree size = declarator->u.array.dimen;
6631 /* The index is a signed object `sizetype' bits wide. */
6632 tree index_type = c_common_signed_type (sizetype);
6634 array_ptr_quals = declarator->u.array.quals;
6635 array_ptr_attrs = declarator->u.array.attrs;
6636 array_parm_static = declarator->u.array.static_p;
6637 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6639 declarator = declarator->declarator;
6641 /* Check for some types that there cannot be arrays of. */
6643 if (VOID_TYPE_P (type))
6645 if (name)
6646 error_at (loc, "declaration of %qE as array of voids", name);
6647 else
6648 error_at (loc, "declaration of type name as array of voids");
6649 type = error_mark_node;
6652 if (TREE_CODE (type) == FUNCTION_TYPE)
6654 if (name)
6655 error_at (loc, "declaration of %qE as array of functions",
6656 name);
6657 else
6658 error_at (loc, "declaration of type name as array of "
6659 "functions");
6660 type = error_mark_node;
6663 if (pedantic && !in_system_header_at (input_location)
6664 && flexible_array_type_p (type))
6665 pedwarn (loc, OPT_Wpedantic,
6666 "invalid use of structure with flexible array member");
6668 if (size == error_mark_node)
6669 type = error_mark_node;
6671 if (type == error_mark_node)
6672 continue;
6674 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6676 type = error_mark_node;
6677 continue;
6680 /* If size was specified, set ITYPE to a range-type for
6681 that size. Otherwise, ITYPE remains null. finish_decl
6682 may figure it out from an initial value. */
6684 if (size)
6686 bool size_maybe_const = true;
6687 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6688 && !TREE_OVERFLOW (size));
6689 bool this_size_varies = false;
6691 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6692 lvalue. */
6693 STRIP_TYPE_NOPS (size);
6695 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6697 if (name)
6698 error_at (loc, "size of array %qE has non-integer type",
6699 name);
6700 else
6701 error_at (loc,
6702 "size of unnamed array has non-integer type");
6703 size = integer_one_node;
6704 size_int_const = true;
6706 /* This can happen with enum forward declaration. */
6707 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6709 if (name)
6710 error_at (loc, "size of array %qE has incomplete type",
6711 name);
6712 else
6713 error_at (loc, "size of unnamed array has incomplete "
6714 "type");
6715 size = integer_one_node;
6716 size_int_const = true;
6719 size = c_fully_fold (size, false, &size_maybe_const);
6721 if (pedantic && size_maybe_const && integer_zerop (size))
6723 if (name)
6724 pedwarn (loc, OPT_Wpedantic,
6725 "ISO C forbids zero-size array %qE", name);
6726 else
6727 pedwarn (loc, OPT_Wpedantic,
6728 "ISO C forbids zero-size array");
6731 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6733 constant_expression_warning (size);
6734 if (tree_int_cst_sgn (size) < 0)
6736 if (name)
6737 error_at (loc, "size of array %qE is negative", name);
6738 else
6739 error_at (loc, "size of unnamed array is negative");
6740 size = integer_one_node;
6741 size_int_const = true;
6743 /* Handle a size folded to an integer constant but
6744 not an integer constant expression. */
6745 if (!size_int_const)
6747 /* If this is a file scope declaration of an
6748 ordinary identifier, this is invalid code;
6749 diagnosing it here and not subsequently
6750 treating the type as variable-length avoids
6751 more confusing diagnostics later. */
6752 if ((decl_context == NORMAL || decl_context == FIELD)
6753 && current_scope == file_scope)
6754 pedwarn (input_location, 0,
6755 "variably modified %qE at file scope",
6756 name);
6757 else
6758 this_size_varies = size_varies = true;
6759 warn_variable_length_array (name, size);
6762 else if ((decl_context == NORMAL || decl_context == FIELD)
6763 && current_scope == file_scope)
6765 error_at (loc, "variably modified %qE at file scope", name);
6766 size = integer_one_node;
6768 else
6770 /* Make sure the array size remains visibly
6771 nonconstant even if it is (eg) a const variable
6772 with known value. */
6773 this_size_varies = size_varies = true;
6774 warn_variable_length_array (name, size);
6775 if (sanitize_flags_p (SANITIZE_VLA)
6776 && current_function_decl != NULL_TREE
6777 && decl_context == NORMAL)
6779 /* Evaluate the array size only once. */
6780 size = save_expr (size);
6781 size = c_fully_fold (size, false, NULL);
6782 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6783 ubsan_instrument_vla (loc, size),
6784 size);
6788 if (integer_zerop (size) && !this_size_varies)
6790 /* A zero-length array cannot be represented with
6791 an unsigned index type, which is what we'll
6792 get with build_index_type. Create an
6793 open-ended range instead. */
6794 itype = build_range_type (sizetype, size, NULL_TREE);
6796 else
6798 /* Arrange for the SAVE_EXPR on the inside of the
6799 MINUS_EXPR, which allows the -1 to get folded
6800 with the +1 that happens when building TYPE_SIZE. */
6801 if (size_varies)
6802 size = save_expr (size);
6803 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6804 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6805 integer_zero_node, size);
6807 /* Compute the maximum valid index, that is, size
6808 - 1. Do the calculation in index_type, so that
6809 if it is a variable the computations will be
6810 done in the proper mode. */
6811 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6812 convert (index_type, size),
6813 convert (index_type,
6814 size_one_node));
6816 /* The above overflows when size does not fit
6817 in index_type.
6818 ??? While a size of INT_MAX+1 technically shouldn't
6819 cause an overflow (because we subtract 1), handling
6820 this case seems like an unnecessary complication. */
6821 if (TREE_CODE (size) == INTEGER_CST
6822 && !int_fits_type_p (size, index_type))
6824 if (name)
6825 error_at (loc, "size of array %qE is too large",
6826 name);
6827 else
6828 error_at (loc, "size of unnamed array is too large");
6829 type = error_mark_node;
6830 continue;
6833 itype = build_index_type (itype);
6835 if (this_size_varies)
6837 if (TREE_SIDE_EFFECTS (size))
6839 if (*expr)
6840 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6841 *expr, size);
6842 else
6843 *expr = size;
6845 *expr_const_operands &= size_maybe_const;
6848 else if (decl_context == FIELD)
6850 bool flexible_array_member = false;
6851 if (array_parm_vla_unspec_p)
6852 /* Field names can in fact have function prototype
6853 scope so [*] is disallowed here through making
6854 the field variably modified, not through being
6855 something other than a declaration with function
6856 prototype scope. */
6857 size_varies = true;
6858 else
6860 const struct c_declarator *t = declarator;
6861 while (t->kind == cdk_attrs)
6862 t = t->declarator;
6863 flexible_array_member = (t->kind == cdk_id);
6865 if (flexible_array_member
6866 && !in_system_header_at (input_location))
6867 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6868 "support flexible array members");
6870 /* ISO C99 Flexible array members are effectively
6871 identical to GCC's zero-length array extension. */
6872 if (flexible_array_member || array_parm_vla_unspec_p)
6873 itype = build_range_type (sizetype, size_zero_node,
6874 NULL_TREE);
6876 else if (decl_context == PARM)
6878 if (array_parm_vla_unspec_p)
6880 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6881 size_varies = true;
6884 else if (decl_context == TYPENAME)
6886 if (array_parm_vla_unspec_p)
6888 /* C99 6.7.5.2p4 */
6889 warning (0, "%<[*]%> not in a declaration");
6890 /* We use this to avoid messing up with incomplete
6891 array types of the same type, that would
6892 otherwise be modified below. */
6893 itype = build_range_type (sizetype, size_zero_node,
6894 NULL_TREE);
6895 size_varies = true;
6899 /* Complain about arrays of incomplete types. */
6900 if (!COMPLETE_TYPE_P (type))
6902 error_at (loc, "array type has incomplete element type %qT",
6903 type);
6904 /* See if we can be more helpful. */
6905 if (TREE_CODE (type) == ARRAY_TYPE)
6907 if (name)
6908 inform (loc, "declaration of %qE as multidimensional "
6909 "array must have bounds for all dimensions "
6910 "except the first", name);
6911 else
6912 inform (loc, "declaration of multidimensional array "
6913 "must have bounds for all dimensions except "
6914 "the first");
6916 type = error_mark_node;
6918 else
6919 /* When itype is NULL, a shared incomplete array type is
6920 returned for all array of a given type. Elsewhere we
6921 make sure we don't complete that type before copying
6922 it, but here we want to make sure we don't ever
6923 modify the shared type, so we gcc_assert (itype)
6924 below. */
6926 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6927 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6928 type = build_qualified_type (type,
6929 ENCODE_QUAL_ADDR_SPACE (as));
6931 type = build_array_type (type, itype);
6934 if (type != error_mark_node)
6936 if (size_varies)
6938 /* It is ok to modify type here even if itype is
6939 NULL: if size_varies, we're in a
6940 multi-dimensional array and the inner type has
6941 variable size, so the enclosing shared array type
6942 must too. */
6943 if (size && TREE_CODE (size) == INTEGER_CST)
6944 type
6945 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6946 C_TYPE_VARIABLE_SIZE (type) = 1;
6949 /* The GCC extension for zero-length arrays differs from
6950 ISO flexible array members in that sizeof yields
6951 zero. */
6952 if (size && integer_zerop (size))
6954 gcc_assert (itype);
6955 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6956 TYPE_SIZE (type) = bitsize_zero_node;
6957 TYPE_SIZE_UNIT (type) = size_zero_node;
6958 SET_TYPE_STRUCTURAL_EQUALITY (type);
6960 if (array_parm_vla_unspec_p)
6962 gcc_assert (itype);
6963 /* The type is complete. C99 6.7.5.2p4 */
6964 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6965 TYPE_SIZE (type) = bitsize_zero_node;
6966 TYPE_SIZE_UNIT (type) = size_zero_node;
6967 SET_TYPE_STRUCTURAL_EQUALITY (type);
6970 if (!valid_array_size_p (loc, type, name))
6971 type = error_mark_node;
6974 if (decl_context != PARM
6975 && (array_ptr_quals != TYPE_UNQUALIFIED
6976 || array_ptr_attrs != NULL_TREE
6977 || array_parm_static))
6979 error_at (loc, "static or type qualifiers in non-parameter "
6980 "array declarator");
6981 array_ptr_quals = TYPE_UNQUALIFIED;
6982 array_ptr_attrs = NULL_TREE;
6983 array_parm_static = false;
6985 orig_qual_indirect++;
6986 break;
6988 case cdk_function:
6990 /* Say it's a definition only for the declarator closest
6991 to the identifier, apart possibly from some
6992 attributes. */
6993 bool really_funcdef = false;
6994 tree arg_types;
6995 orig_qual_type = NULL_TREE;
6996 if (funcdef_flag)
6998 const struct c_declarator *t = declarator->declarator;
6999 while (t->kind == cdk_attrs)
7000 t = t->declarator;
7001 really_funcdef = (t->kind == cdk_id);
7004 /* Declaring a function type. Make sure we have a valid
7005 type for the function to return. */
7006 if (type == error_mark_node)
7007 continue;
7009 size_varies = false;
7011 /* Warn about some types functions can't return. */
7012 if (TREE_CODE (type) == FUNCTION_TYPE)
7014 if (name)
7015 error_at (loc, "%qE declared as function returning a "
7016 "function", name);
7017 else
7018 error_at (loc, "type name declared as function "
7019 "returning a function");
7020 type = integer_type_node;
7022 if (TREE_CODE (type) == ARRAY_TYPE)
7024 if (name)
7025 error_at (loc, "%qE declared as function returning an array",
7026 name);
7027 else
7028 error_at (loc, "type name declared as function returning "
7029 "an array");
7030 type = integer_type_node;
7033 /* Construct the function type and go to the next
7034 inner layer of declarator. */
7035 arg_info = declarator->u.arg_info;
7036 arg_types = grokparms (arg_info, really_funcdef);
7038 /* Type qualifiers before the return type of the function
7039 qualify the return type, not the function type. */
7040 if (type_quals)
7042 const enum c_declspec_word ignored_quals_list[] =
7044 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7045 cdw_atomic, cdw_number_of_elements
7047 location_t specs_loc
7048 = smallest_type_quals_location (declspecs->locations,
7049 ignored_quals_list);
7050 if (specs_loc == UNKNOWN_LOCATION)
7051 specs_loc = declspecs->locations[cdw_typedef];
7052 if (specs_loc == UNKNOWN_LOCATION)
7053 specs_loc = loc;
7055 /* Type qualifiers on a function return type are
7056 normally permitted by the standard but have no
7057 effect, so give a warning at -Wreturn-type.
7058 Qualifiers on a void return type are banned on
7059 function definitions in ISO C; GCC used to used
7060 them for noreturn functions. The resolution of C11
7061 DR#423 means qualifiers (other than _Atomic) are
7062 actually removed from the return type when
7063 determining the function type. */
7064 int quals_used = type_quals;
7065 if (flag_isoc11)
7066 quals_used &= TYPE_QUAL_ATOMIC;
7067 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7068 pedwarn (specs_loc, 0,
7069 "function definition has qualified void "
7070 "return type");
7071 else
7072 warning_at (specs_loc, OPT_Wignored_qualifiers,
7073 "type qualifiers ignored on function "
7074 "return type");
7076 /* Ensure an error for restrict on invalid types; the
7077 DR#423 resolution is not entirely clear about
7078 this. */
7079 if (flag_isoc11
7080 && (type_quals & TYPE_QUAL_RESTRICT)
7081 && (!POINTER_TYPE_P (type)
7082 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7083 error_at (loc, "invalid use of %<restrict%>");
7084 type = c_build_qualified_type (type, quals_used);
7086 type_quals = TYPE_UNQUALIFIED;
7088 type = build_function_type (type, arg_types);
7089 declarator = declarator->declarator;
7091 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7092 the formal parameter list of this FUNCTION_TYPE to point to
7093 the FUNCTION_TYPE node itself. */
7095 c_arg_tag *tag;
7096 unsigned ix;
7098 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7099 TYPE_CONTEXT (tag->type) = type;
7101 break;
7103 case cdk_pointer:
7105 /* Merge any constancy or volatility into the target type
7106 for the pointer. */
7107 if ((type_quals & TYPE_QUAL_ATOMIC)
7108 && TREE_CODE (type) == FUNCTION_TYPE)
7110 error_at (loc,
7111 "%<_Atomic%>-qualified function type");
7112 type_quals &= ~TYPE_QUAL_ATOMIC;
7114 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7115 && type_quals)
7116 pedwarn (loc, OPT_Wpedantic,
7117 "ISO C forbids qualified function types");
7118 if (type_quals)
7119 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7120 orig_qual_indirect);
7121 orig_qual_type = NULL_TREE;
7122 size_varies = false;
7124 /* When the pointed-to type involves components of variable size,
7125 care must be taken to ensure that the size evaluation code is
7126 emitted early enough to dominate all the possible later uses
7127 and late enough for the variables on which it depends to have
7128 been assigned.
7130 This is expected to happen automatically when the pointed-to
7131 type has a name/declaration of it's own, but special attention
7132 is required if the type is anonymous.
7134 We attach an artificial TYPE_DECL to such pointed-to type
7135 and arrange for it to be included in a DECL_EXPR. This
7136 forces the sizes evaluation at a safe point and ensures it
7137 is not deferred until e.g. within a deeper conditional context.
7139 PARM contexts have no enclosing statement list that
7140 can hold the DECL_EXPR, so we need to use a BIND_EXPR
7141 instead, and add it to the list of expressions that
7142 need to be evaluated.
7144 TYPENAME contexts do have an enclosing statement list,
7145 but it would be incorrect to use it, as the size should
7146 only be evaluated if the containing expression is
7147 evaluated. We might also be in the middle of an
7148 expression with side effects on the pointed-to type size
7149 "arguments" prior to the pointer declaration point and
7150 the fake TYPE_DECL in the enclosing context would force
7151 the size evaluation prior to the side effects. We therefore
7152 use BIND_EXPRs in TYPENAME contexts too. */
7153 if (!TYPE_NAME (type)
7154 && variably_modified_type_p (type, NULL_TREE))
7156 tree bind = NULL_TREE;
7157 if (decl_context == TYPENAME || decl_context == PARM)
7159 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7160 NULL_TREE, NULL_TREE);
7161 TREE_SIDE_EFFECTS (bind) = 1;
7162 BIND_EXPR_BODY (bind) = push_stmt_list ();
7163 push_scope ();
7165 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7166 DECL_ARTIFICIAL (decl) = 1;
7167 pushdecl (decl);
7168 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7169 TYPE_NAME (type) = decl;
7170 if (bind)
7172 pop_scope ();
7173 BIND_EXPR_BODY (bind)
7174 = pop_stmt_list (BIND_EXPR_BODY (bind));
7175 if (*expr)
7176 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7177 bind);
7178 else
7179 *expr = bind;
7183 type = c_build_pointer_type (type);
7185 /* Process type qualifiers (such as const or volatile)
7186 that were given inside the `*'. */
7187 type_quals = declarator->u.pointer_quals;
7189 declarator = declarator->declarator;
7190 break;
7192 default:
7193 gcc_unreachable ();
7196 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7197 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7199 /* Now TYPE has the actual type, apart from any qualifiers in
7200 TYPE_QUALS. */
7202 /* Warn about address space used for things other than static memory or
7203 pointers. */
7204 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7205 if (!ADDR_SPACE_GENERIC_P (address_space))
7207 if (decl_context == NORMAL)
7209 switch (storage_class)
7211 case csc_auto:
7212 error ("%qs combined with %<auto%> qualifier for %qE",
7213 c_addr_space_name (address_space), name);
7214 break;
7215 case csc_register:
7216 error ("%qs combined with %<register%> qualifier for %qE",
7217 c_addr_space_name (address_space), name);
7218 break;
7219 case csc_none:
7220 if (current_function_scope)
7222 error ("%qs specified for auto variable %qE",
7223 c_addr_space_name (address_space), name);
7224 break;
7226 break;
7227 case csc_static:
7228 case csc_extern:
7229 case csc_typedef:
7230 break;
7231 default:
7232 gcc_unreachable ();
7235 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7237 if (name)
7238 error ("%qs specified for parameter %qE",
7239 c_addr_space_name (address_space), name);
7240 else
7241 error ("%qs specified for unnamed parameter",
7242 c_addr_space_name (address_space));
7244 else if (decl_context == FIELD)
7246 if (name)
7247 error ("%qs specified for structure field %qE",
7248 c_addr_space_name (address_space), name);
7249 else
7250 error ("%qs specified for structure field",
7251 c_addr_space_name (address_space));
7255 /* Check the type and width of a bit-field. */
7256 if (bitfield)
7258 check_bitfield_type_and_width (loc, &type, width, name);
7259 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7260 atomic types are permitted for bit-fields; we have no code to
7261 make bit-field accesses atomic, so disallow them. */
7262 if (type_quals & TYPE_QUAL_ATOMIC)
7264 if (name)
7265 error_at (loc, "bit-field %qE has atomic type", name);
7266 else
7267 error_at (loc, "bit-field has atomic type");
7268 type_quals &= ~TYPE_QUAL_ATOMIC;
7272 /* Reject invalid uses of _Alignas. */
7273 if (declspecs->alignas_p)
7275 if (storage_class == csc_typedef)
7276 error_at (loc, "alignment specified for typedef %qE", name);
7277 else if (storage_class == csc_register)
7278 error_at (loc, "alignment specified for %<register%> object %qE",
7279 name);
7280 else if (decl_context == PARM)
7282 if (name)
7283 error_at (loc, "alignment specified for parameter %qE", name);
7284 else
7285 error_at (loc, "alignment specified for unnamed parameter");
7287 else if (bitfield)
7289 if (name)
7290 error_at (loc, "alignment specified for bit-field %qE", name);
7291 else
7292 error_at (loc, "alignment specified for unnamed bit-field");
7294 else if (TREE_CODE (type) == FUNCTION_TYPE)
7295 error_at (loc, "alignment specified for function %qE", name);
7296 else if (declspecs->align_log != -1 && TYPE_P (type))
7298 alignas_align = 1U << declspecs->align_log;
7299 if (alignas_align < min_align_of_type (type))
7301 if (name)
7302 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7303 "alignment of %qE", name);
7304 else
7305 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7306 "alignment of unnamed field");
7307 alignas_align = 0;
7312 /* If this is declaring a typedef name, return a TYPE_DECL. */
7314 if (storage_class == csc_typedef)
7316 tree decl;
7317 if ((type_quals & TYPE_QUAL_ATOMIC)
7318 && TREE_CODE (type) == FUNCTION_TYPE)
7320 error_at (loc,
7321 "%<_Atomic%>-qualified function type");
7322 type_quals &= ~TYPE_QUAL_ATOMIC;
7324 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7325 && type_quals)
7326 pedwarn (loc, OPT_Wpedantic,
7327 "ISO C forbids qualified function types");
7328 if (type_quals)
7329 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7330 orig_qual_indirect);
7331 decl = build_decl (declarator->id_loc,
7332 TYPE_DECL, declarator->u.id.id, type);
7333 if (declspecs->explicit_signed_p)
7334 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7335 if (declspecs->inline_p)
7336 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7337 if (declspecs->noreturn_p)
7338 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7340 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7342 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7344 if (b != NULL
7345 && b->decl != NULL_TREE
7346 && (B_IN_CURRENT_SCOPE (b)
7347 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7348 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7350 auto_diagnostic_group d;
7351 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7352 ("using %qD as both a typedef and a tag is "
7353 "invalid in C++"), decl)
7354 && b->locus != UNKNOWN_LOCATION)
7355 inform (b->locus, "originally defined here");
7359 return decl;
7362 /* If this is a type name (such as, in a cast or sizeof),
7363 compute the type and return it now. */
7365 if (decl_context == TYPENAME)
7367 /* Note that the grammar rejects storage classes in typenames
7368 and fields. */
7369 gcc_assert (storage_class == csc_none && !threadp
7370 && !declspecs->inline_p && !declspecs->noreturn_p);
7371 if ((type_quals & TYPE_QUAL_ATOMIC)
7372 && TREE_CODE (type) == FUNCTION_TYPE)
7374 error_at (loc,
7375 "%<_Atomic%>-qualified function type");
7376 type_quals &= ~TYPE_QUAL_ATOMIC;
7378 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7379 && type_quals)
7380 pedwarn (loc, OPT_Wpedantic,
7381 "ISO C forbids const or volatile function types");
7382 if (type_quals)
7383 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7384 orig_qual_indirect);
7385 return type;
7388 if (pedantic && decl_context == FIELD
7389 && variably_modified_type_p (type, NULL_TREE))
7391 /* C99 6.7.2.1p8 */
7392 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7393 "have a variably modified type");
7396 /* Aside from typedefs and type names (handle above),
7397 `void' at top level (not within pointer)
7398 is allowed only in public variables.
7399 We don't complain about parms either, but that is because
7400 a better error message can be made later. */
7402 if (VOID_TYPE_P (type) && decl_context != PARM
7403 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7404 && (storage_class == csc_extern
7405 || (current_scope == file_scope
7406 && !(storage_class == csc_static
7407 || storage_class == csc_register)))))
7409 error_at (loc, "variable or field %qE declared void", name);
7410 type = integer_type_node;
7413 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7414 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7417 tree decl;
7419 if (decl_context == PARM)
7421 tree promoted_type;
7422 bool array_parameter_p = false;
7424 /* A parameter declared as an array of T is really a pointer to T.
7425 One declared as a function is really a pointer to a function. */
7427 if (TREE_CODE (type) == ARRAY_TYPE)
7429 /* Transfer const-ness of array into that of type pointed to. */
7430 type = TREE_TYPE (type);
7431 if (orig_qual_type != NULL_TREE)
7433 if (orig_qual_indirect == 0)
7434 orig_qual_type = TREE_TYPE (orig_qual_type);
7435 else
7436 orig_qual_indirect--;
7438 if (type_quals)
7439 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7440 orig_qual_indirect);
7441 type = c_build_pointer_type (type);
7442 type_quals = array_ptr_quals;
7443 if (type_quals)
7444 type = c_build_qualified_type (type, type_quals);
7446 /* We don't yet implement attributes in this context. */
7447 if (array_ptr_attrs != NULL_TREE)
7448 warning_at (loc, OPT_Wattributes,
7449 "attributes in parameter array declarator ignored");
7451 size_varies = false;
7452 array_parameter_p = true;
7454 else if (TREE_CODE (type) == FUNCTION_TYPE)
7456 if (type_quals & TYPE_QUAL_ATOMIC)
7458 error_at (loc,
7459 "%<_Atomic%>-qualified function type");
7460 type_quals &= ~TYPE_QUAL_ATOMIC;
7462 else if (type_quals)
7463 pedwarn (loc, OPT_Wpedantic,
7464 "ISO C forbids qualified function types");
7465 if (type_quals)
7466 type = c_build_qualified_type (type, type_quals);
7467 type = c_build_pointer_type (type);
7468 type_quals = TYPE_UNQUALIFIED;
7470 else if (type_quals)
7471 type = c_build_qualified_type (type, type_quals);
7473 decl = build_decl (declarator->id_loc,
7474 PARM_DECL, declarator->u.id.id, type);
7475 if (size_varies)
7476 C_DECL_VARIABLE_SIZE (decl) = 1;
7477 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7479 /* Compute the type actually passed in the parmlist,
7480 for the case where there is no prototype.
7481 (For example, shorts and chars are passed as ints.)
7482 When there is a prototype, this is overridden later. */
7484 if (type == error_mark_node)
7485 promoted_type = type;
7486 else
7487 promoted_type = c_type_promotes_to (type);
7489 DECL_ARG_TYPE (decl) = promoted_type;
7490 if (declspecs->inline_p)
7491 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7492 if (declspecs->noreturn_p)
7493 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7495 else if (decl_context == FIELD)
7497 /* Note that the grammar rejects storage classes in typenames
7498 and fields. */
7499 gcc_assert (storage_class == csc_none && !threadp
7500 && !declspecs->inline_p && !declspecs->noreturn_p);
7502 /* Structure field. It may not be a function. */
7504 if (TREE_CODE (type) == FUNCTION_TYPE)
7506 error_at (loc, "field %qE declared as a function", name);
7507 type = build_pointer_type (type);
7509 else if (TREE_CODE (type) != ERROR_MARK
7510 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7512 if (name)
7513 error_at (loc, "field %qE has incomplete type", name);
7514 else
7515 error_at (loc, "unnamed field has incomplete type");
7516 type = error_mark_node;
7518 else if (TREE_CODE (type) == ARRAY_TYPE
7519 && TYPE_DOMAIN (type) == NULL_TREE)
7521 /* We have a flexible array member through a typedef.
7522 Set suitable range. Whether this is a correct position
7523 for a flexible array member will be determined elsewhere. */
7524 if (!in_system_header_at (input_location))
7525 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7526 "support flexible array members");
7527 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7528 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7529 NULL_TREE);
7530 if (orig_qual_indirect == 0)
7531 orig_qual_type = NULL_TREE;
7533 if (type != error_mark_node
7534 && !verify_type_context (loc, TCTX_FIELD, type))
7535 type = error_mark_node;
7537 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7538 orig_qual_indirect);
7539 decl = build_decl (declarator->id_loc,
7540 FIELD_DECL, declarator->u.id.id, type);
7541 DECL_NONADDRESSABLE_P (decl) = bitfield;
7542 if (bitfield && !declarator->u.id.id)
7544 TREE_NO_WARNING (decl) = 1;
7545 DECL_PADDING_P (decl) = 1;
7548 if (size_varies)
7549 C_DECL_VARIABLE_SIZE (decl) = 1;
7551 else if (TREE_CODE (type) == FUNCTION_TYPE)
7553 if (storage_class == csc_register || threadp)
7555 error_at (loc, "invalid storage class for function %qE", name);
7557 else if (current_scope != file_scope)
7559 /* Function declaration not at file scope. Storage
7560 classes other than `extern' are not allowed, C99
7561 6.7.1p5, and `extern' makes no difference. However,
7562 GCC allows 'auto', perhaps with 'inline', to support
7563 nested functions. */
7564 if (storage_class == csc_auto)
7565 pedwarn (loc, OPT_Wpedantic,
7566 "invalid storage class for function %qE", name);
7567 else if (storage_class == csc_static)
7569 error_at (loc, "invalid storage class for function %qE", name);
7570 if (funcdef_flag)
7571 storage_class = declspecs->storage_class = csc_none;
7572 else
7573 return NULL_TREE;
7577 decl = build_decl (declarator->id_loc,
7578 FUNCTION_DECL, declarator->u.id.id, type);
7579 decl = build_decl_attribute_variant (decl, decl_attr);
7581 if (type_quals & TYPE_QUAL_ATOMIC)
7583 error_at (loc,
7584 "%<_Atomic%>-qualified function type");
7585 type_quals &= ~TYPE_QUAL_ATOMIC;
7587 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7588 pedwarn (loc, OPT_Wpedantic,
7589 "ISO C forbids qualified function types");
7591 /* Every function declaration is an external reference
7592 (DECL_EXTERNAL) except for those which are not at file
7593 scope and are explicitly declared "auto". This is
7594 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7595 GCC to signify a forward declaration of a nested function. */
7596 if (storage_class == csc_auto && current_scope != file_scope)
7597 DECL_EXTERNAL (decl) = 0;
7598 /* In C99, a function which is declared 'inline' with 'extern'
7599 is not an external reference (which is confusing). It
7600 means that the later definition of the function must be output
7601 in this file, C99 6.7.4p6. In GNU C89, a function declared
7602 'extern inline' is an external reference. */
7603 else if (declspecs->inline_p && storage_class != csc_static)
7604 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7605 == flag_gnu89_inline);
7606 else
7607 DECL_EXTERNAL (decl) = !initialized;
7609 /* Record absence of global scope for `static' or `auto'. */
7610 TREE_PUBLIC (decl)
7611 = !(storage_class == csc_static || storage_class == csc_auto);
7613 /* For a function definition, record the argument information
7614 block where store_parm_decls will look for it. */
7615 if (funcdef_flag)
7616 current_function_arg_info = arg_info;
7618 if (declspecs->default_int_p)
7619 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7621 /* Record presence of `inline' and `_Noreturn', if it is
7622 reasonable. */
7623 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7625 if (declspecs->inline_p)
7626 pedwarn (loc, 0, "cannot inline function %<main%>");
7627 if (declspecs->noreturn_p)
7628 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7630 else
7632 if (declspecs->inline_p)
7633 /* Record that the function is declared `inline'. */
7634 DECL_DECLARED_INLINE_P (decl) = 1;
7635 if (declspecs->noreturn_p)
7637 if (flag_isoc99)
7638 pedwarn_c99 (loc, OPT_Wpedantic,
7639 "ISO C99 does not support %<_Noreturn%>");
7640 else
7641 pedwarn_c99 (loc, OPT_Wpedantic,
7642 "ISO C90 does not support %<_Noreturn%>");
7643 TREE_THIS_VOLATILE (decl) = 1;
7647 else
7649 /* It's a variable. */
7650 /* An uninitialized decl with `extern' is a reference. */
7651 int extern_ref = !initialized && storage_class == csc_extern;
7653 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7654 orig_qual_indirect);
7656 /* C99 6.2.2p7: It is invalid (compile-time undefined
7657 behavior) to create an 'extern' declaration for a
7658 variable if there is a global declaration that is
7659 'static' and the global declaration is not visible.
7660 (If the static declaration _is_ currently visible,
7661 the 'extern' declaration is taken to refer to that decl.) */
7662 if (extern_ref && current_scope != file_scope)
7664 tree global_decl = identifier_global_value (declarator->u.id.id);
7665 tree visible_decl = lookup_name (declarator->u.id.id);
7667 if (global_decl
7668 && global_decl != visible_decl
7669 && VAR_P (global_decl)
7670 && !TREE_PUBLIC (global_decl))
7671 error_at (loc, "variable previously declared %<static%> "
7672 "redeclared %<extern%>");
7675 decl = build_decl (declarator->id_loc,
7676 VAR_DECL, declarator->u.id.id, type);
7677 if (size_varies)
7678 C_DECL_VARIABLE_SIZE (decl) = 1;
7680 if (declspecs->inline_p)
7681 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7682 if (declspecs->noreturn_p)
7683 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7685 /* At file scope, an initialized extern declaration may follow
7686 a static declaration. In that case, DECL_EXTERNAL will be
7687 reset later in start_decl. */
7688 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7690 /* At file scope, the presence of a `static' or `register' storage
7691 class specifier, or the absence of all storage class specifiers
7692 makes this declaration a definition (perhaps tentative). Also,
7693 the absence of `static' makes it public. */
7694 if (current_scope == file_scope)
7696 TREE_PUBLIC (decl) = storage_class != csc_static;
7697 TREE_STATIC (decl) = !extern_ref;
7699 /* Not at file scope, only `static' makes a static definition. */
7700 else
7702 TREE_STATIC (decl) = (storage_class == csc_static);
7703 TREE_PUBLIC (decl) = extern_ref;
7706 if (threadp)
7707 set_decl_tls_model (decl, decl_default_tls_model (decl));
7710 if ((storage_class == csc_extern
7711 || (storage_class == csc_none
7712 && TREE_CODE (type) == FUNCTION_TYPE
7713 && !funcdef_flag))
7714 && variably_modified_type_p (type, NULL_TREE))
7716 /* C99 6.7.5.2p2 */
7717 if (TREE_CODE (type) == FUNCTION_TYPE)
7718 error_at (loc, "non-nested function with variably modified type");
7719 else
7720 error_at (loc, "object with variably modified type must have "
7721 "no linkage");
7724 /* For nested functions disqualify ones taking VLAs by value
7725 from inlining since the middle-end cannot deal with this.
7726 ??? We should arrange for those to be passed by reference
7727 with emitting the copy on the caller side in the frontend. */
7728 if (storage_class == csc_none
7729 && TREE_CODE (type) == FUNCTION_TYPE)
7730 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7732 tree arg = TREE_VALUE (al);
7733 if (arg != error_mark_node
7734 && C_TYPE_VARIABLE_SIZE (arg))
7736 DECL_UNINLINABLE (decl) = 1;
7737 break;
7741 /* Record `register' declaration for warnings on &
7742 and in case doing stupid register allocation. */
7744 if (storage_class == csc_register)
7746 C_DECL_REGISTER (decl) = 1;
7747 DECL_REGISTER (decl) = 1;
7750 /* Record constancy and volatility. */
7751 c_apply_type_quals_to_decl (type_quals, decl);
7753 /* Apply _Alignas specifiers. */
7754 if (alignas_align)
7756 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7757 DECL_USER_ALIGN (decl) = 1;
7760 /* If a type has volatile components, it should be stored in memory.
7761 Otherwise, the fact that those components are volatile
7762 will be ignored, and would even crash the compiler.
7763 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7764 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7765 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7766 || TREE_CODE (decl) == RESULT_DECL))
7768 /* It is not an error for a structure with volatile fields to
7769 be declared register, but reset DECL_REGISTER since it
7770 cannot actually go in a register. */
7771 int was_reg = C_DECL_REGISTER (decl);
7772 C_DECL_REGISTER (decl) = 0;
7773 DECL_REGISTER (decl) = 0;
7774 c_mark_addressable (decl);
7775 C_DECL_REGISTER (decl) = was_reg;
7778 /* This is the earliest point at which we might know the assembler
7779 name of a variable. Thus, if it's known before this, die horribly. */
7780 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7781 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7783 if (warn_cxx_compat
7784 && VAR_P (decl)
7785 && TREE_PUBLIC (decl)
7786 && TREE_STATIC (decl)
7787 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7788 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7789 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7790 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7791 ("non-local variable %qD with anonymous type is "
7792 "questionable in C++"),
7793 decl);
7795 return decl;
7799 /* Decode the parameter-list info for a function type or function definition.
7800 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7801 if there is an identifier list instead of a parameter decl list).
7802 These two functions are separate because when a function returns
7803 or receives functions then each is called multiple times but the order
7804 of calls is different. The last call to `grokparms' is always the one
7805 that contains the formal parameter names of a function definition.
7807 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7809 FUNCDEF_FLAG is true for a function definition, false for
7810 a mere declaration. A nonempty identifier-list gets an error message
7811 when FUNCDEF_FLAG is false. */
7813 static tree
7814 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7816 tree arg_types = arg_info->types;
7818 if (funcdef_flag && arg_info->had_vla_unspec)
7820 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7821 /* C99 6.7.5.2p4 */
7822 error ("%<[*]%> not allowed in other than function prototype scope");
7825 if (arg_types == NULL_TREE && !funcdef_flag
7826 && !in_system_header_at (input_location))
7827 warning (OPT_Wstrict_prototypes,
7828 "function declaration isn%'t a prototype");
7830 if (arg_types == error_mark_node)
7831 /* Don't set TYPE_ARG_TYPES in this case. */
7832 return NULL_TREE;
7834 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7836 if (!funcdef_flag)
7838 pedwarn (input_location, 0, "parameter names (without types) in "
7839 "function declaration");
7840 arg_info->parms = NULL_TREE;
7842 else
7843 arg_info->parms = arg_info->types;
7845 arg_info->types = NULL_TREE;
7846 return NULL_TREE;
7848 else
7850 tree parm, type, typelt;
7851 unsigned int parmno;
7853 /* In C2X, convert () in a function definition to (void). */
7854 if (flag_isoc2x
7855 && funcdef_flag
7856 && !arg_types
7857 && !arg_info->parms)
7858 arg_types = arg_info->types = void_list_node;
7860 /* If there is a parameter of incomplete type in a definition,
7861 this is an error. In a declaration this is valid, and a
7862 struct or union type may be completed later, before any calls
7863 or definition of the function. In the case where the tag was
7864 first declared within the parameter list, a warning has
7865 already been given. If a parameter has void type, then
7866 however the function cannot be defined or called, so
7867 warn. */
7869 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7870 parm;
7871 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7873 type = TREE_VALUE (typelt);
7874 if (type == error_mark_node)
7875 continue;
7877 if (!COMPLETE_TYPE_P (type))
7879 if (funcdef_flag)
7881 if (DECL_NAME (parm))
7882 error_at (input_location,
7883 "parameter %u (%q+D) has incomplete type",
7884 parmno, parm);
7885 else
7886 error_at (DECL_SOURCE_LOCATION (parm),
7887 "parameter %u has incomplete type",
7888 parmno);
7890 TREE_VALUE (typelt) = error_mark_node;
7891 TREE_TYPE (parm) = error_mark_node;
7892 arg_types = NULL_TREE;
7894 else if (VOID_TYPE_P (type))
7896 if (DECL_NAME (parm))
7897 warning_at (input_location, 0,
7898 "parameter %u (%q+D) has void type",
7899 parmno, parm);
7900 else
7901 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7902 "parameter %u has void type",
7903 parmno);
7907 if (DECL_NAME (parm) && TREE_USED (parm))
7908 warn_if_shadowing (parm);
7910 return arg_types;
7914 /* Allocate and initialize a c_arg_info structure from the parser's
7915 obstack. */
7917 struct c_arg_info *
7918 build_arg_info (void)
7920 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7921 ret->parms = NULL_TREE;
7922 ret->tags = NULL;
7923 ret->types = NULL_TREE;
7924 ret->others = NULL_TREE;
7925 ret->pending_sizes = NULL;
7926 ret->had_vla_unspec = 0;
7927 return ret;
7930 /* Take apart the current scope and return a c_arg_info structure with
7931 info on a parameter list just parsed.
7933 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7935 ELLIPSIS being true means the argument list ended in '...' so don't
7936 append a sentinel (void_list_node) to the end of the type-list.
7938 EXPR is NULL or an expression that needs to be evaluated for the
7939 side effects of array size expressions in the parameters. */
7941 struct c_arg_info *
7942 get_parm_info (bool ellipsis, tree expr)
7944 struct c_binding *b = current_scope->bindings;
7945 struct c_arg_info *arg_info = build_arg_info ();
7947 tree parms = NULL_TREE;
7948 vec<c_arg_tag, va_gc> *tags = NULL;
7949 tree types = NULL_TREE;
7950 tree others = NULL_TREE;
7952 bool gave_void_only_once_err = false;
7954 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7956 /* The bindings in this scope must not get put into a block.
7957 We will take care of deleting the binding nodes. */
7958 current_scope->bindings = 0;
7960 /* This function is only called if there was *something* on the
7961 parameter list. */
7962 gcc_assert (b);
7964 /* A parameter list consisting solely of 'void' indicates that the
7965 function takes no arguments. But if the 'void' is qualified
7966 (by 'const' or 'volatile'), or has a storage class specifier
7967 ('register'), then the behavior is undefined; issue an error.
7968 Typedefs for 'void' are OK (see DR#157). */
7969 if (b->prev == 0 /* one binding */
7970 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7971 && !DECL_NAME (b->decl) /* anonymous */
7972 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7974 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7975 || C_DECL_REGISTER (b->decl))
7976 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7978 /* There cannot be an ellipsis. */
7979 if (ellipsis)
7980 error_at (b->locus, "%<void%> must be the only parameter");
7982 arg_info->types = void_list_node;
7983 return arg_info;
7986 if (!ellipsis)
7987 types = void_list_node;
7989 /* Break up the bindings list into parms, tags, types, and others;
7990 apply sanity checks; purge the name-to-decl bindings. */
7991 while (b)
7993 tree decl = b->decl;
7994 tree type = TREE_TYPE (decl);
7995 c_arg_tag tag;
7996 const char *keyword;
7998 switch (TREE_CODE (decl))
8000 case PARM_DECL:
8001 if (b->id)
8003 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8004 I_SYMBOL_BINDING (b->id) = b->shadowed;
8007 /* Check for forward decls that never got their actual decl. */
8008 if (TREE_ASM_WRITTEN (decl))
8009 error_at (b->locus,
8010 "parameter %q+D has just a forward declaration", decl);
8011 /* Check for (..., void, ...) and issue an error. */
8012 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8014 if (!gave_void_only_once_err)
8016 error_at (b->locus, "%<void%> must be the only parameter");
8017 gave_void_only_once_err = true;
8020 else
8022 /* Valid parameter, add it to the list. */
8023 DECL_CHAIN (decl) = parms;
8024 parms = decl;
8026 /* Since there is a prototype, args are passed in their
8027 declared types. The back end may override this later. */
8028 DECL_ARG_TYPE (decl) = type;
8029 types = tree_cons (0, type, types);
8031 break;
8033 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8034 case UNION_TYPE: keyword = "union"; goto tag;
8035 case RECORD_TYPE: keyword = "struct"; goto tag;
8036 tag:
8037 /* Types may not have tag-names, in which case the type
8038 appears in the bindings list with b->id NULL. */
8039 if (b->id)
8041 gcc_assert (I_TAG_BINDING (b->id) == b);
8042 I_TAG_BINDING (b->id) = b->shadowed;
8045 /* Warn about any struct, union or enum tags defined in a
8046 parameter list. The scope of such types is limited to
8047 the parameter list, which is rarely if ever desirable
8048 (it's impossible to call such a function with type-
8049 correct arguments). An anonymous union parm type is
8050 meaningful as a GNU extension, so don't warn for that. */
8051 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8053 if (b->id)
8054 /* The %s will be one of 'struct', 'union', or 'enum'. */
8055 warning_at (b->locus, 0,
8056 "%<%s %E%> declared inside parameter list"
8057 " will not be visible outside of this definition or"
8058 " declaration", keyword, b->id);
8059 else
8060 /* The %s will be one of 'struct', 'union', or 'enum'. */
8061 warning_at (b->locus, 0,
8062 "anonymous %s declared inside parameter list"
8063 " will not be visible outside of this definition or"
8064 " declaration", keyword);
8067 tag.id = b->id;
8068 tag.type = decl;
8069 vec_safe_push (tags, tag);
8070 break;
8072 case FUNCTION_DECL:
8073 /* FUNCTION_DECLs appear when there is an implicit function
8074 declaration in the parameter list. */
8075 gcc_assert (b->nested || seen_error ());
8076 goto set_shadowed;
8078 case CONST_DECL:
8079 case TYPE_DECL:
8080 /* CONST_DECLs appear here when we have an embedded enum,
8081 and TYPE_DECLs appear here when we have an embedded struct
8082 or union. No warnings for this - we already warned about the
8083 type itself. */
8085 /* When we reinsert this decl in the function body, we need
8086 to reconstruct whether it was marked as nested. */
8087 gcc_assert (!b->nested);
8088 DECL_CHAIN (decl) = others;
8089 others = decl;
8090 /* fall through */
8092 case ERROR_MARK:
8093 set_shadowed:
8094 /* error_mark_node appears here when we have an undeclared
8095 variable. Just throw it away. */
8096 if (b->id)
8098 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8099 I_SYMBOL_BINDING (b->id) = b->shadowed;
8101 break;
8103 /* Other things that might be encountered. */
8104 case LABEL_DECL:
8105 case VAR_DECL:
8106 default:
8107 gcc_unreachable ();
8110 b = free_binding_and_advance (b);
8113 arg_info->parms = parms;
8114 arg_info->tags = tags;
8115 arg_info->types = types;
8116 arg_info->others = others;
8117 arg_info->pending_sizes = expr;
8118 return arg_info;
8121 /* Get the struct, enum or union (CODE says which) with tag NAME.
8122 Define the tag as a forward-reference with location LOC if it is
8123 not defined. HAVE_STD_ATTRS says whether any standard attributes
8124 were present after the struct, union or enum keyword; ATTRS are the
8125 standard attributes present there. Return a c_typespec structure
8126 for the type specifier. */
8128 struct c_typespec
8129 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8130 bool have_std_attrs, tree attrs)
8132 struct c_typespec ret;
8133 tree ref;
8134 location_t refloc;
8136 ret.expr = NULL_TREE;
8137 ret.expr_const_operands = true;
8139 /* If a cross reference is requested, look up the type
8140 already defined for this tag and return it. */
8142 ref = lookup_tag (code, name, false, &refloc);
8143 /* If this is the right type of tag, return what we found.
8144 (This reference will be shadowed by shadow_tag later if appropriate.)
8145 If this is the wrong type of tag, do not return it. If it was the
8146 wrong type in the same scope, we will have had an error
8147 message already; if in a different scope and declaring
8148 a name, pending_xref_error will give an error message; but if in a
8149 different scope and not declaring a name, this tag should
8150 shadow the previous declaration of a different type of tag, and
8151 this would not work properly if we return the reference found.
8152 (For example, with "struct foo" in an outer scope, "union foo;"
8153 must shadow that tag with a new one of union type.) */
8154 ret.kind = (ref
8155 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8156 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8157 if (ref && TREE_CODE (ref) == code)
8159 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8160 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8161 && loc != UNKNOWN_LOCATION
8162 && warn_cxx_compat)
8164 switch (code)
8166 case ENUMERAL_TYPE:
8167 warning_at (loc, OPT_Wc___compat,
8168 ("enum type defined in struct or union "
8169 "is not visible in C++"));
8170 inform (refloc, "enum type defined here");
8171 break;
8172 case RECORD_TYPE:
8173 warning_at (loc, OPT_Wc___compat,
8174 ("struct defined in struct or union "
8175 "is not visible in C++"));
8176 inform (refloc, "struct defined here");
8177 break;
8178 case UNION_TYPE:
8179 warning_at (loc, OPT_Wc___compat,
8180 ("union defined in struct or union "
8181 "is not visible in C++"));
8182 inform (refloc, "union defined here");
8183 break;
8184 default:
8185 gcc_unreachable();
8189 ret.spec = ref;
8190 return ret;
8193 /* If no such tag is yet defined, create a forward-reference node
8194 and record it as the "definition".
8195 When a real declaration of this type is found,
8196 the forward-reference will be altered into a real type. */
8198 ref = make_node (code);
8199 if (code == ENUMERAL_TYPE)
8201 /* Give the type a default layout like unsigned int
8202 to avoid crashing if it does not get defined. */
8203 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8204 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8205 TYPE_USER_ALIGN (ref) = 0;
8206 TYPE_UNSIGNED (ref) = 1;
8207 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8208 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8209 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8212 pushtag (loc, name, ref);
8213 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8215 ret.spec = ref;
8216 return ret;
8219 /* Get the struct, enum or union (CODE says which) with tag NAME.
8220 Define the tag as a forward-reference if it is not defined.
8221 Return a tree for the type. */
8223 tree
8224 xref_tag (enum tree_code code, tree name)
8226 return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
8229 /* Make sure that the tag NAME is defined *in the current scope*
8230 at least as a forward reference.
8231 LOC is the location of the struct's definition.
8232 CODE says which kind of tag NAME ought to be.
8234 This stores the current value of the file static STRUCT_PARSE_INFO
8235 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8236 new c_struct_parse_info structure. The old value of
8237 STRUCT_PARSE_INFO is restored in finish_struct. */
8239 tree
8240 start_struct (location_t loc, enum tree_code code, tree name,
8241 class c_struct_parse_info **enclosing_struct_parse_info)
8243 /* If there is already a tag defined at this scope
8244 (as a forward reference), just return it. */
8246 tree ref = NULL_TREE;
8247 location_t refloc = UNKNOWN_LOCATION;
8249 if (name != NULL_TREE)
8250 ref = lookup_tag (code, name, true, &refloc);
8251 if (ref && TREE_CODE (ref) == code)
8253 if (TYPE_STUB_DECL (ref))
8254 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8256 if (TYPE_SIZE (ref))
8258 if (code == UNION_TYPE)
8259 error_at (loc, "redefinition of %<union %E%>", name);
8260 else
8261 error_at (loc, "redefinition of %<struct %E%>", name);
8262 if (refloc != UNKNOWN_LOCATION)
8263 inform (refloc, "originally defined here");
8264 /* Don't create structures using a name already in use. */
8265 ref = NULL_TREE;
8267 else if (C_TYPE_BEING_DEFINED (ref))
8269 if (code == UNION_TYPE)
8270 error_at (loc, "nested redefinition of %<union %E%>", name);
8271 else
8272 error_at (loc, "nested redefinition of %<struct %E%>", name);
8273 /* Don't bother to report "originally defined here" for a
8274 nested redefinition; the original definition should be
8275 obvious. */
8276 /* Don't create structures that contain themselves. */
8277 ref = NULL_TREE;
8281 /* Otherwise create a forward-reference just so the tag is in scope. */
8283 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8285 ref = make_node (code);
8286 pushtag (loc, name, ref);
8289 C_TYPE_BEING_DEFINED (ref) = 1;
8290 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8291 TYPE_PACKED (v) = flag_pack_struct;
8293 *enclosing_struct_parse_info = struct_parse_info;
8294 struct_parse_info = new c_struct_parse_info ();
8296 /* FIXME: This will issue a warning for a use of a type defined
8297 within a statement expr used within sizeof, et. al. This is not
8298 terribly serious as C++ doesn't permit statement exprs within
8299 sizeof anyhow. */
8300 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8301 warning_at (loc, OPT_Wc___compat,
8302 "defining type in %qs expression is invalid in C++",
8303 (in_sizeof
8304 ? "sizeof"
8305 : (in_typeof ? "typeof" : "alignof")));
8307 return ref;
8310 /* Process the specs, declarator and width (NULL if omitted)
8311 of a structure component, returning a FIELD_DECL node.
8312 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8313 DECL_ATTRS is as for grokdeclarator.
8315 LOC is the location of the structure component.
8317 This is done during the parsing of the struct declaration.
8318 The FIELD_DECL nodes are chained together and the lot of them
8319 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8321 tree
8322 grokfield (location_t loc,
8323 struct c_declarator *declarator, struct c_declspecs *declspecs,
8324 tree width, tree *decl_attrs)
8326 tree value;
8328 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8329 && width == NULL_TREE)
8331 /* This is an unnamed decl.
8333 If we have something of the form "union { list } ;" then this
8334 is the anonymous union extension. Similarly for struct.
8336 If this is something of the form "struct foo;", then
8337 If MS or Plan 9 extensions are enabled, this is handled as
8338 an anonymous struct.
8339 Otherwise this is a forward declaration of a structure tag.
8341 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8342 If foo names a structure or union without a tag, then this
8343 is an anonymous struct (this is permitted by C11).
8344 If MS or Plan 9 extensions are enabled and foo names a
8345 structure, then again this is an anonymous struct.
8346 Otherwise this is an error.
8348 Oh what a horrid tangled web we weave. I wonder if MS consciously
8349 took this from Plan 9 or if it was an accident of implementation
8350 that took root before someone noticed the bug... */
8352 tree type = declspecs->type;
8353 bool ok = false;
8355 if (RECORD_OR_UNION_TYPE_P (type)
8356 && (flag_ms_extensions
8357 || flag_plan9_extensions
8358 || !declspecs->typedef_p))
8360 if (flag_ms_extensions || flag_plan9_extensions)
8361 ok = true;
8362 else if (TYPE_NAME (type) == NULL)
8363 ok = true;
8364 else
8365 ok = false;
8367 if (!ok)
8369 pedwarn (loc, 0, "declaration does not declare anything");
8370 return NULL_TREE;
8372 if (flag_isoc99)
8373 pedwarn_c99 (loc, OPT_Wpedantic,
8374 "ISO C99 doesn%'t support unnamed structs/unions");
8375 else
8376 pedwarn_c99 (loc, OPT_Wpedantic,
8377 "ISO C90 doesn%'t support unnamed structs/unions");
8380 value = grokdeclarator (declarator, declspecs, FIELD, false,
8381 width ? &width : NULL, decl_attrs, NULL, NULL,
8382 DEPRECATED_NORMAL);
8384 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8385 DECL_INITIAL (value) = width;
8386 if (width)
8387 SET_DECL_C_BIT_FIELD (value);
8389 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8391 /* If we currently have a binding for this field, set the
8392 in_struct field in the binding, so that we warn about lookups
8393 which find it. */
8394 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8395 if (b != NULL)
8397 /* If the in_struct field is not yet set, push it on a list
8398 to be cleared when this struct is finished. */
8399 if (!b->in_struct)
8401 struct_parse_info->fields.safe_push (b);
8402 b->in_struct = 1;
8407 return value;
8410 /* Subroutine of detect_field_duplicates: return whether X and Y,
8411 which are both fields in the same struct, have duplicate field
8412 names. */
8414 static bool
8415 is_duplicate_field (tree x, tree y)
8417 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8418 return true;
8420 /* When using -fplan9-extensions, an anonymous field whose name is a
8421 typedef can duplicate a field name. */
8422 if (flag_plan9_extensions
8423 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8425 tree xt, xn, yt, yn;
8427 xt = TREE_TYPE (x);
8428 if (DECL_NAME (x) != NULL_TREE)
8429 xn = DECL_NAME (x);
8430 else if (RECORD_OR_UNION_TYPE_P (xt)
8431 && TYPE_NAME (xt) != NULL_TREE
8432 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8433 xn = DECL_NAME (TYPE_NAME (xt));
8434 else
8435 xn = NULL_TREE;
8437 yt = TREE_TYPE (y);
8438 if (DECL_NAME (y) != NULL_TREE)
8439 yn = DECL_NAME (y);
8440 else if (RECORD_OR_UNION_TYPE_P (yt)
8441 && TYPE_NAME (yt) != NULL_TREE
8442 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8443 yn = DECL_NAME (TYPE_NAME (yt));
8444 else
8445 yn = NULL_TREE;
8447 if (xn != NULL_TREE && xn == yn)
8448 return true;
8451 return false;
8454 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8455 to HTAB, giving errors for any duplicates. */
8457 static void
8458 detect_field_duplicates_hash (tree fieldlist,
8459 hash_table<nofree_ptr_hash <tree_node> > *htab)
8461 tree x, y;
8462 tree_node **slot;
8464 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8465 if ((y = DECL_NAME (x)) != NULL_TREE)
8467 slot = htab->find_slot (y, INSERT);
8468 if (*slot)
8470 error ("duplicate member %q+D", x);
8471 DECL_NAME (x) = NULL_TREE;
8473 *slot = y;
8475 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8477 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8479 /* When using -fplan9-extensions, an anonymous field whose
8480 name is a typedef can duplicate a field name. */
8481 if (flag_plan9_extensions
8482 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8483 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8485 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8486 slot = htab->find_slot (xn, INSERT);
8487 if (*slot)
8488 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8489 *slot = xn;
8494 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8495 the list such that this does not present a problem later. */
8497 static void
8498 detect_field_duplicates (tree fieldlist)
8500 tree x, y;
8501 int timeout = 10;
8503 /* If the struct is the list of instance variables of an Objective-C
8504 class, then we need to check all the instance variables of
8505 superclasses when checking for duplicates (since you can't have
8506 an instance variable in a subclass with the same name as an
8507 instance variable in a superclass). We pass on this job to the
8508 Objective-C compiler. objc_detect_field_duplicates() will return
8509 false if we are not checking the list of instance variables and
8510 the C frontend should proceed with the standard field duplicate
8511 checks. If we are checking the list of instance variables, the
8512 ObjC frontend will do the check, emit the errors if needed, and
8513 then return true. */
8514 if (c_dialect_objc ())
8515 if (objc_detect_field_duplicates (false))
8516 return;
8518 /* First, see if there are more than "a few" fields.
8519 This is trivially true if there are zero or one fields. */
8520 if (!fieldlist || !DECL_CHAIN (fieldlist))
8521 return;
8522 x = fieldlist;
8523 do {
8524 timeout--;
8525 if (DECL_NAME (x) == NULL_TREE
8526 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8527 timeout = 0;
8528 x = DECL_CHAIN (x);
8529 } while (timeout > 0 && x);
8531 /* If there were "few" fields and no anonymous structures or unions,
8532 avoid the overhead of allocating a hash table. Instead just do
8533 the nested traversal thing. */
8534 if (timeout > 0)
8536 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8537 /* When using -fplan9-extensions, we can have duplicates
8538 between typedef names and fields. */
8539 if (DECL_NAME (x)
8540 || (flag_plan9_extensions
8541 && DECL_NAME (x) == NULL_TREE
8542 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8543 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8544 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8546 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8547 if (is_duplicate_field (y, x))
8549 error ("duplicate member %q+D", x);
8550 DECL_NAME (x) = NULL_TREE;
8554 else
8556 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8557 detect_field_duplicates_hash (fieldlist, &htab);
8561 /* Finish up struct info used by -Wc++-compat. */
8563 static void
8564 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8565 location_t record_loc)
8567 unsigned int ix;
8568 tree x;
8569 struct c_binding *b;
8571 if (fieldlist == NULL_TREE)
8573 if (code == RECORD_TYPE)
8574 warning_at (record_loc, OPT_Wc___compat,
8575 "empty struct has size 0 in C, size 1 in C++");
8576 else
8577 warning_at (record_loc, OPT_Wc___compat,
8578 "empty union has size 0 in C, size 1 in C++");
8581 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8582 the current struct. We do this now at the end of the struct
8583 because the flag is used to issue visibility warnings, and we
8584 only want to issue those warnings if the type is referenced
8585 outside of the struct declaration. */
8586 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8587 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8589 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8590 typedefs used when declaring fields in this struct. If the name
8591 of any of the fields is also a typedef name then the struct would
8592 not parse in C++, because the C++ lookup rules say that the
8593 typedef name would be looked up in the context of the struct, and
8594 would thus be the field rather than the typedef. */
8595 if (!struct_parse_info->typedefs_seen.is_empty ()
8596 && fieldlist != NULL_TREE)
8598 /* Use a hash_set<tree> using the name of the typedef. We can use
8599 a hash_set<tree> because identifiers are interned. */
8600 hash_set<tree> tset;
8602 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8603 tset.add (DECL_NAME (x));
8605 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8607 if (DECL_NAME (x) != NULL_TREE
8608 && tset.contains (DECL_NAME (x)))
8610 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8611 ("using %qD as both field and typedef name is "
8612 "invalid in C++"),
8614 /* FIXME: It would be nice to report the location where
8615 the typedef name is used. */
8620 /* For each field which has a binding and which was not defined in
8621 an enclosing struct, clear the in_struct field. */
8622 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8623 b->in_struct = 0;
8626 /* Function to help qsort sort FIELD_DECLs by name order. */
8628 static int
8629 field_decl_cmp (const void *x_p, const void *y_p)
8631 const tree *const x = (const tree *) x_p;
8632 const tree *const y = (const tree *) y_p;
8634 if (DECL_NAME (*x) == DECL_NAME (*y))
8635 /* A nontype is "greater" than a type. */
8636 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8637 if (DECL_NAME (*x) == NULL_TREE)
8638 return -1;
8639 if (DECL_NAME (*y) == NULL_TREE)
8640 return 1;
8641 if (DECL_NAME (*x) < DECL_NAME (*y))
8642 return -1;
8643 return 1;
8646 /* If this structure or union completes the type of any previous
8647 variable declaration, lay it out and output its rtl. */
8648 static void
8649 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
8651 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
8653 tree decl = TREE_VALUE (x);
8654 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8655 layout_array_type (TREE_TYPE (decl));
8656 if (TREE_CODE (decl) != TYPE_DECL)
8658 relayout_decl (decl);
8659 if (c_dialect_objc ())
8660 objc_check_decl (decl);
8661 rest_of_decl_compilation (decl, toplevel, 0);
8666 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8667 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8668 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8669 ATTRIBUTES are attributes to be applied to the structure.
8671 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8672 the struct was started. */
8674 tree
8675 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8676 class c_struct_parse_info *enclosing_struct_parse_info)
8678 tree x;
8679 bool toplevel = file_scope == current_scope;
8681 /* If this type was previously laid out as a forward reference,
8682 make sure we lay it out again. */
8684 TYPE_SIZE (t) = NULL_TREE;
8686 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8688 if (pedantic)
8690 for (x = fieldlist; x; x = DECL_CHAIN (x))
8692 if (DECL_NAME (x) != NULL_TREE)
8693 break;
8694 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8695 break;
8698 if (x == NULL_TREE)
8700 if (TREE_CODE (t) == UNION_TYPE)
8702 if (fieldlist)
8703 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8704 else
8705 pedwarn (loc, OPT_Wpedantic, "union has no members");
8707 else
8709 if (fieldlist)
8710 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8711 else
8712 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8717 /* Install struct as DECL_CONTEXT of each field decl.
8718 Also process specified field sizes, found in the DECL_INITIAL,
8719 storing 0 there after the type has been changed to precision equal
8720 to its width, rather than the precision of the specified standard
8721 type. (Correct layout requires the original type to have been preserved
8722 until now.) */
8724 bool saw_named_field = false;
8725 for (x = fieldlist; x; x = DECL_CHAIN (x))
8727 if (TREE_TYPE (x) == error_mark_node)
8728 continue;
8730 DECL_CONTEXT (x) = t;
8732 /* If any field is const, the structure type is pseudo-const. */
8733 if (TREE_READONLY (x))
8734 C_TYPE_FIELDS_READONLY (t) = 1;
8735 else
8737 /* A field that is pseudo-const makes the structure likewise. */
8738 tree t1 = strip_array_types (TREE_TYPE (x));
8739 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8740 C_TYPE_FIELDS_READONLY (t) = 1;
8743 /* Any field that is volatile means variables of this type must be
8744 treated in some ways as volatile. */
8745 if (TREE_THIS_VOLATILE (x))
8746 C_TYPE_FIELDS_VOLATILE (t) = 1;
8748 /* Any field of nominal variable size implies structure is too. */
8749 if (C_DECL_VARIABLE_SIZE (x))
8750 C_TYPE_VARIABLE_SIZE (t) = 1;
8752 if (DECL_C_BIT_FIELD (x))
8754 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8755 DECL_SIZE (x) = bitsize_int (width);
8756 DECL_BIT_FIELD (x) = 1;
8759 if (TYPE_PACKED (t)
8760 && (DECL_BIT_FIELD (x)
8761 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8762 DECL_PACKED (x) = 1;
8764 /* Detect flexible array member in an invalid context. */
8765 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8766 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8767 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8768 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8770 if (TREE_CODE (t) == UNION_TYPE)
8772 error_at (DECL_SOURCE_LOCATION (x),
8773 "flexible array member in union");
8774 TREE_TYPE (x) = error_mark_node;
8776 else if (DECL_CHAIN (x) != NULL_TREE)
8778 error_at (DECL_SOURCE_LOCATION (x),
8779 "flexible array member not at end of struct");
8780 TREE_TYPE (x) = error_mark_node;
8782 else if (!saw_named_field)
8784 error_at (DECL_SOURCE_LOCATION (x),
8785 "flexible array member in a struct with no named "
8786 "members");
8787 TREE_TYPE (x) = error_mark_node;
8791 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8792 && flexible_array_type_p (TREE_TYPE (x)))
8793 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8794 "invalid use of structure with flexible array member");
8796 if (DECL_NAME (x)
8797 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8798 saw_named_field = true;
8801 detect_field_duplicates (fieldlist);
8803 /* Now we have the nearly final fieldlist. Record it,
8804 then lay out the structure or union (including the fields). */
8806 TYPE_FIELDS (t) = fieldlist;
8808 maybe_apply_pragma_scalar_storage_order (t);
8810 layout_type (t);
8812 if (TYPE_SIZE_UNIT (t)
8813 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8814 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8815 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8816 error ("type %qT is too large", t);
8818 /* Give bit-fields their proper types and rewrite the type of array fields
8819 with scalar component if the enclosing type has reverse storage order. */
8820 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8822 if (TREE_CODE (field) == FIELD_DECL
8823 && DECL_INITIAL (field)
8824 && TREE_TYPE (field) != error_mark_node)
8826 unsigned HOST_WIDE_INT width
8827 = tree_to_uhwi (DECL_INITIAL (field));
8828 tree type = TREE_TYPE (field);
8829 if (width != TYPE_PRECISION (type))
8831 TREE_TYPE (field)
8832 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8833 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8835 DECL_INITIAL (field) = NULL_TREE;
8837 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8838 && TREE_CODE (field) == FIELD_DECL
8839 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8841 tree ftype = TREE_TYPE (field);
8842 tree ctype = strip_array_types (ftype);
8843 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8845 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8846 tree *typep = &fmain_type;
8847 do {
8848 *typep = build_distinct_type_copy (*typep);
8849 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8850 typep = &TREE_TYPE (*typep);
8851 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8852 TREE_TYPE (field)
8853 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8857 /* Warn on problematic type punning for storage order purposes. */
8858 if (TREE_CODE (t) == UNION_TYPE
8859 && TREE_CODE (field) == FIELD_DECL
8860 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
8862 tree ftype = TREE_TYPE (field);
8863 if (TREE_CODE (ftype) == ARRAY_TYPE)
8864 ftype = strip_array_types (ftype);
8865 if (RECORD_OR_UNION_TYPE_P (ftype)
8866 && TYPE_REVERSE_STORAGE_ORDER (ftype)
8867 != TYPE_REVERSE_STORAGE_ORDER (t))
8868 warning_at (DECL_SOURCE_LOCATION (field),
8869 OPT_Wscalar_storage_order,
8870 "type punning toggles scalar storage order");
8874 /* Now we have the truly final field list.
8875 Store it in this type and in the variants. */
8877 TYPE_FIELDS (t) = fieldlist;
8879 /* If there are lots of fields, sort so we can look through them fast.
8880 We arbitrarily consider 16 or more elts to be "a lot". */
8883 int len = 0;
8885 for (x = fieldlist; x; x = DECL_CHAIN (x))
8887 if (len > 15 || DECL_NAME (x) == NULL)
8888 break;
8889 len += 1;
8892 if (len > 15)
8894 tree *field_array;
8895 struct lang_type *space;
8896 struct sorted_fields_type *space2;
8898 len += list_length (x);
8900 /* Use the same allocation policy here that make_node uses, to
8901 ensure that this lives as long as the rest of the struct decl.
8902 All decls in an inline function need to be saved. */
8904 space = ggc_cleared_alloc<struct lang_type> ();
8905 space2 = (sorted_fields_type *) ggc_internal_alloc
8906 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8908 len = 0;
8909 space->s = space2;
8910 field_array = &space2->elts[0];
8911 for (x = fieldlist; x; x = DECL_CHAIN (x))
8913 field_array[len++] = x;
8915 /* If there is anonymous struct or union, break out of the loop. */
8916 if (DECL_NAME (x) == NULL)
8917 break;
8919 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8920 if (x == NULL)
8922 TYPE_LANG_SPECIFIC (t) = space;
8923 TYPE_LANG_SPECIFIC (t)->s->len = len;
8924 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8925 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8930 /* If this was supposed to be a transparent union, but we can't
8931 make it one, warn and turn off the flag. */
8932 if (TREE_CODE (t) == UNION_TYPE
8933 && TYPE_TRANSPARENT_AGGR (t)
8934 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8936 TYPE_TRANSPARENT_AGGR (t) = 0;
8937 warning_at (loc, 0, "union cannot be made transparent");
8940 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8941 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8943 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8944 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8945 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
8946 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8947 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8948 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8949 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8952 /* Update type location to the one of the definition, instead of e.g.
8953 a forward declaration. */
8954 if (TYPE_STUB_DECL (t))
8955 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8957 /* Finish debugging output for this type. */
8958 rest_of_type_compilation (t, toplevel);
8960 finish_incomplete_vars (incomplete_vars, toplevel);
8962 /* If we're inside a function proper, i.e. not file-scope and not still
8963 parsing parameters, then arrange for the size of a variable sized type
8964 to be bound now. */
8965 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8966 add_stmt (build_stmt (loc,
8967 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8969 if (warn_cxx_compat)
8970 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8972 delete struct_parse_info;
8974 struct_parse_info = enclosing_struct_parse_info;
8976 /* If this struct is defined inside a struct, add it to
8977 struct_types. */
8978 if (warn_cxx_compat
8979 && struct_parse_info != NULL
8980 && !in_sizeof && !in_typeof && !in_alignof)
8981 struct_parse_info->struct_types.safe_push (t);
8983 return t;
8986 static struct {
8987 gt_pointer_operator new_value;
8988 void *cookie;
8989 } resort_data;
8991 /* This routine compares two fields like field_decl_cmp but using the
8992 pointer operator in resort_data. */
8994 static int
8995 resort_field_decl_cmp (const void *x_p, const void *y_p)
8997 const tree *const x = (const tree *) x_p;
8998 const tree *const y = (const tree *) y_p;
9000 if (DECL_NAME (*x) == DECL_NAME (*y))
9001 /* A nontype is "greater" than a type. */
9002 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9003 if (DECL_NAME (*x) == NULL_TREE)
9004 return -1;
9005 if (DECL_NAME (*y) == NULL_TREE)
9006 return 1;
9008 tree d1 = DECL_NAME (*x);
9009 tree d2 = DECL_NAME (*y);
9010 resort_data.new_value (&d1, resort_data.cookie);
9011 resort_data.new_value (&d2, resort_data.cookie);
9012 if (d1 < d2)
9013 return -1;
9015 return 1;
9018 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9020 void
9021 resort_sorted_fields (void *obj,
9022 void * ARG_UNUSED (orig_obj),
9023 gt_pointer_operator new_value,
9024 void *cookie)
9026 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9027 resort_data.new_value = new_value;
9028 resort_data.cookie = cookie;
9029 qsort (&sf->elts[0], sf->len, sizeof (tree),
9030 resort_field_decl_cmp);
9033 /* Lay out the type T, and its element type, and so on. */
9035 static void
9036 layout_array_type (tree t)
9038 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9039 layout_array_type (TREE_TYPE (t));
9040 layout_type (t);
9043 /* Begin compiling the definition of an enumeration type.
9044 NAME is its name (or null if anonymous).
9045 LOC is the enum's location.
9046 Returns the type object, as yet incomplete.
9047 Also records info about it so that build_enumerator
9048 may be used to declare the individual values as they are read. */
9050 tree
9051 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
9053 tree enumtype = NULL_TREE;
9054 location_t enumloc = UNKNOWN_LOCATION;
9056 /* If this is the real definition for a previous forward reference,
9057 fill in the contents in the same object that used to be the
9058 forward reference. */
9060 if (name != NULL_TREE)
9061 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9063 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9065 enumtype = make_node (ENUMERAL_TYPE);
9066 pushtag (loc, name, enumtype);
9068 /* Update type location to the one of the definition, instead of e.g.
9069 a forward declaration. */
9070 else if (TYPE_STUB_DECL (enumtype))
9072 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9073 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9076 if (C_TYPE_BEING_DEFINED (enumtype))
9077 error_at (loc, "nested redefinition of %<enum %E%>", name);
9079 C_TYPE_BEING_DEFINED (enumtype) = 1;
9081 if (TYPE_VALUES (enumtype) != NULL_TREE)
9083 /* This enum is a named one that has been declared already. */
9084 error_at (loc, "redeclaration of %<enum %E%>", name);
9085 if (enumloc != UNKNOWN_LOCATION)
9086 inform (enumloc, "originally defined here");
9088 /* Completely replace its old definition.
9089 The old enumerators remain defined, however. */
9090 TYPE_VALUES (enumtype) = NULL_TREE;
9093 the_enum->enum_next_value = integer_zero_node;
9094 the_enum->enum_overflow = 0;
9096 if (flag_short_enums)
9097 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9098 TYPE_PACKED (v) = 1;
9100 /* FIXME: This will issue a warning for a use of a type defined
9101 within sizeof in a statement expr. This is not terribly serious
9102 as C++ doesn't permit statement exprs within sizeof anyhow. */
9103 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9104 warning_at (loc, OPT_Wc___compat,
9105 "defining type in %qs expression is invalid in C++",
9106 (in_sizeof
9107 ? "sizeof"
9108 : (in_typeof ? "typeof" : "alignof")));
9110 return enumtype;
9113 /* After processing and defining all the values of an enumeration type,
9114 install their decls in the enumeration type and finish it off.
9115 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9116 and ATTRIBUTES are the specified attributes.
9117 Returns ENUMTYPE. */
9119 tree
9120 finish_enum (tree enumtype, tree values, tree attributes)
9122 tree pair, tem;
9123 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9124 int precision;
9125 signop sign;
9126 bool toplevel = (file_scope == current_scope);
9127 struct lang_type *lt;
9129 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9131 /* Calculate the maximum value of any enumerator in this type. */
9133 if (values == error_mark_node)
9134 minnode = maxnode = integer_zero_node;
9135 else
9137 minnode = maxnode = TREE_VALUE (values);
9138 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9140 tree value = TREE_VALUE (pair);
9141 if (tree_int_cst_lt (maxnode, value))
9142 maxnode = value;
9143 if (tree_int_cst_lt (value, minnode))
9144 minnode = value;
9148 /* Construct the final type of this enumeration. It is the same
9149 as one of the integral types - the narrowest one that fits, except
9150 that normally we only go as narrow as int - and signed iff any of
9151 the values are negative. */
9152 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9153 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9154 tree_int_cst_min_precision (maxnode, sign));
9156 /* If the precision of the type was specified with an attribute and it
9157 was too small, give an error. Otherwise, use it. */
9158 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9160 if (precision > TYPE_PRECISION (enumtype))
9162 TYPE_PRECISION (enumtype) = 0;
9163 error ("specified mode too small for enumerated values");
9165 else
9166 precision = TYPE_PRECISION (enumtype);
9168 else
9169 TYPE_PRECISION (enumtype) = 0;
9171 if (TYPE_PACKED (enumtype)
9172 || precision > TYPE_PRECISION (integer_type_node)
9173 || TYPE_PRECISION (enumtype))
9175 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9176 if (tem == NULL)
9178 warning (0, "enumeration values exceed range of largest integer");
9179 tem = long_long_integer_type_node;
9182 else
9183 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9185 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9186 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9187 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9188 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9189 TYPE_SIZE (enumtype) = NULL_TREE;
9190 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9192 layout_type (enumtype);
9194 if (values != error_mark_node)
9196 /* Change the type of the enumerators to be the enum type. We
9197 need to do this irrespective of the size of the enum, for
9198 proper type checking. Replace the DECL_INITIALs of the
9199 enumerators, and the value slots of the list, with copies
9200 that have the enum type; they cannot be modified in place
9201 because they may be shared (e.g. integer_zero_node) Finally,
9202 change the purpose slots to point to the names of the decls. */
9203 for (pair = values; pair; pair = TREE_CHAIN (pair))
9205 tree enu = TREE_PURPOSE (pair);
9206 tree ini = DECL_INITIAL (enu);
9208 TREE_TYPE (enu) = enumtype;
9210 /* The ISO C Standard mandates enumerators to have type int,
9211 even though the underlying type of an enum type is
9212 unspecified. However, GCC allows enumerators of any
9213 integer type as an extensions. build_enumerator()
9214 converts any enumerators that fit in an int to type int,
9215 to avoid promotions to unsigned types when comparing
9216 integers with enumerators that fit in the int range.
9217 When -pedantic is given, build_enumerator() would have
9218 already warned about those that don't fit. Here we
9219 convert the rest to the enumerator type. */
9220 if (TREE_TYPE (ini) != integer_type_node)
9221 ini = convert (enumtype, ini);
9223 DECL_INITIAL (enu) = ini;
9224 TREE_PURPOSE (pair) = DECL_NAME (enu);
9225 TREE_VALUE (pair) = ini;
9228 TYPE_VALUES (enumtype) = values;
9231 /* Record the min/max values so that we can warn about bit-field
9232 enumerations that are too small for the values. */
9233 lt = ggc_cleared_alloc<struct lang_type> ();
9234 lt->enum_min = minnode;
9235 lt->enum_max = maxnode;
9236 TYPE_LANG_SPECIFIC (enumtype) = lt;
9238 /* Fix up all variant types of this enum type. */
9239 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9240 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9242 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9243 if (tem == enumtype)
9244 continue;
9245 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9246 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9247 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9248 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9249 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9250 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9251 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9252 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9253 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9254 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9255 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9258 /* Finish debugging output for this type. */
9259 rest_of_type_compilation (enumtype, toplevel);
9261 finish_incomplete_vars (incomplete_vars, toplevel);
9263 /* If this enum is defined inside a struct, add it to
9264 struct_types. */
9265 if (warn_cxx_compat
9266 && struct_parse_info != NULL
9267 && !in_sizeof && !in_typeof && !in_alignof)
9268 struct_parse_info->struct_types.safe_push (enumtype);
9270 C_TYPE_BEING_DEFINED (enumtype) = 0;
9272 return enumtype;
9275 /* Build and install a CONST_DECL for one value of the
9276 current enumeration type (one that was begun with start_enum).
9277 DECL_LOC is the location of the enumerator.
9278 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9279 Return a tree-list containing the CONST_DECL and its value.
9280 Assignment of sequential values by default is handled here. */
9282 tree
9283 build_enumerator (location_t decl_loc, location_t loc,
9284 struct c_enum_contents *the_enum, tree name, tree value)
9286 tree decl, type;
9288 /* Validate and default VALUE. */
9290 if (value != NULL_TREE)
9292 /* Don't issue more errors for error_mark_node (i.e. an
9293 undeclared identifier) - just ignore the value expression. */
9294 if (value == error_mark_node)
9295 value = NULL_TREE;
9296 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9298 error_at (loc, "enumerator value for %qE is not an integer constant",
9299 name);
9300 value = NULL_TREE;
9302 else
9304 if (TREE_CODE (value) != INTEGER_CST)
9306 value = c_fully_fold (value, false, NULL);
9307 if (TREE_CODE (value) == INTEGER_CST)
9308 pedwarn (loc, OPT_Wpedantic,
9309 "enumerator value for %qE is not an integer "
9310 "constant expression", name);
9312 if (TREE_CODE (value) != INTEGER_CST)
9314 error ("enumerator value for %qE is not an integer constant",
9315 name);
9316 value = NULL_TREE;
9318 else
9320 value = default_conversion (value);
9321 constant_expression_warning (value);
9326 /* Default based on previous value. */
9327 /* It should no longer be possible to have NON_LVALUE_EXPR
9328 in the default. */
9329 if (value == NULL_TREE)
9331 value = the_enum->enum_next_value;
9332 if (the_enum->enum_overflow)
9333 error_at (loc, "overflow in enumeration values");
9335 /* Even though the underlying type of an enum is unspecified, the
9336 type of enumeration constants is explicitly defined as int
9337 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
9338 an extension. */
9339 else if (!int_fits_type_p (value, integer_type_node))
9340 pedwarn (loc, OPT_Wpedantic,
9341 "ISO C restricts enumerator values to range of %<int%>");
9343 /* The ISO C Standard mandates enumerators to have type int, even
9344 though the underlying type of an enum type is unspecified.
9345 However, GCC allows enumerators of any integer type as an
9346 extensions. Here we convert any enumerators that fit in an int
9347 to type int, to avoid promotions to unsigned types when comparing
9348 integers with enumerators that fit in the int range. When
9349 -pedantic is given, we would have already warned about those that
9350 don't fit. We have to do this here rather than in finish_enum
9351 because this value may be used to define more enumerators. */
9352 if (int_fits_type_p (value, integer_type_node))
9353 value = convert (integer_type_node, value);
9355 /* Set basis for default for next value. */
9356 the_enum->enum_next_value
9357 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9358 PLUS_EXPR, value, integer_one_node, false);
9359 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9361 /* Now create a declaration for the enum value name. */
9363 type = TREE_TYPE (value);
9364 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
9365 TYPE_PRECISION (integer_type_node)),
9366 (TYPE_PRECISION (type)
9367 >= TYPE_PRECISION (integer_type_node)
9368 && TYPE_UNSIGNED (type)));
9370 decl = build_decl (decl_loc, CONST_DECL, name, type);
9371 DECL_INITIAL (decl) = convert (type, value);
9372 pushdecl (decl);
9374 return tree_cons (decl, value, NULL_TREE);
9377 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9379 tree
9380 c_simulate_enum_decl (location_t loc, const char *name,
9381 vec<string_int_pair> values)
9383 location_t saved_loc = input_location;
9384 input_location = loc;
9386 struct c_enum_contents the_enum;
9387 tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9389 tree value_chain = NULL_TREE;
9390 string_int_pair *value;
9391 unsigned int i;
9392 FOR_EACH_VEC_ELT (values, i, value)
9394 tree decl = build_enumerator (loc, loc, &the_enum,
9395 get_identifier (value->first),
9396 build_int_cst (integer_type_node,
9397 value->second));
9398 TREE_CHAIN (decl) = value_chain;
9399 value_chain = decl;
9402 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9404 input_location = saved_loc;
9405 return enumtype;
9408 /* Create the FUNCTION_DECL for a function definition.
9409 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9410 the declaration; they describe the function's name and the type it returns,
9411 but twisted together in a fashion that parallels the syntax of C.
9413 This function creates a binding context for the function body
9414 as well as setting up the FUNCTION_DECL in current_function_decl.
9416 Returns true on success. If the DECLARATOR is not suitable for a function
9417 (it defines a datum instead), we return false to report a parse error. */
9419 bool
9420 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9421 tree attributes)
9423 tree decl1, old_decl;
9424 tree restype, resdecl;
9425 location_t loc;
9427 current_function_returns_value = 0; /* Assume, until we see it does. */
9428 current_function_returns_null = 0;
9429 current_function_returns_abnormally = 0;
9430 warn_about_return_type = 0;
9431 c_switch_stack = NULL;
9433 /* Indicate no valid break/continue context. */
9434 in_statement = 0;
9436 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9437 &attributes, NULL, NULL, DEPRECATED_NORMAL);
9438 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9440 /* If the declarator is not suitable for a function definition,
9441 cause a syntax error. */
9442 if (decl1 == NULL_TREE
9443 || TREE_CODE (decl1) != FUNCTION_DECL)
9444 return false;
9446 loc = DECL_SOURCE_LOCATION (decl1);
9448 /* A nested function is not global. */
9449 if (current_function_decl != NULL_TREE)
9450 TREE_PUBLIC (decl1) = 0;
9452 c_decl_attributes (&decl1, attributes, 0);
9454 if (DECL_DECLARED_INLINE_P (decl1)
9455 && DECL_UNINLINABLE (decl1)
9456 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9457 warning_at (loc, OPT_Wattributes,
9458 "inline function %qD given attribute %qs",
9459 decl1, "noinline");
9461 /* Handle gnu_inline attribute. */
9462 if (declspecs->inline_p
9463 && !flag_gnu89_inline
9464 && TREE_CODE (decl1) == FUNCTION_DECL
9465 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9466 || current_function_decl))
9468 if (declspecs->storage_class != csc_static)
9469 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9472 announce_function (decl1);
9474 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9476 error_at (loc, "return type is an incomplete type");
9477 /* Make it return void instead. */
9478 TREE_TYPE (decl1)
9479 = build_function_type (void_type_node,
9480 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9483 if (warn_about_return_type)
9484 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9485 : (warn_return_type > 0 ? OPT_Wreturn_type
9486 : OPT_Wimplicit_int),
9487 "return type defaults to %<int%>");
9489 /* Make the init_value nonzero so pushdecl knows this is not tentative.
9490 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
9491 DECL_INITIAL (decl1) = error_mark_node;
9493 /* If this definition isn't a prototype and we had a prototype declaration
9494 before, copy the arg type info from that prototype. */
9495 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9496 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9497 old_decl = NULL_TREE;
9499 current_function_prototype_locus = UNKNOWN_LOCATION;
9500 current_function_prototype_built_in = false;
9501 current_function_prototype_arg_types = NULL_TREE;
9502 tree newtype = TREE_TYPE (decl1);
9503 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
9504 if (!prototype_p (newtype))
9506 tree oldrt = TREE_TYPE (oldtype);
9507 tree newrt = TREE_TYPE (newtype);
9508 if (old_decl != NULL_TREE
9509 && TREE_CODE (oldtype) == FUNCTION_TYPE
9510 && comptypes (oldrt, newrt))
9512 if (stdarg_p (oldtype))
9514 auto_diagnostic_group d;
9515 warning_at (loc, 0, "%q+D defined as variadic function "
9516 "without prototype", decl1);
9517 locate_old_decl (old_decl);
9519 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
9520 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9521 current_function_prototype_built_in
9522 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9523 current_function_prototype_arg_types
9524 = TYPE_ARG_TYPES (newtype);
9526 if (TREE_PUBLIC (decl1))
9528 /* If there is an external prototype declaration of this
9529 function, record its location but do not copy information
9530 to this decl. This may be an invisible declaration
9531 (built-in or in a scope which has finished) or simply
9532 have more refined argument types than any declaration
9533 found above. */
9534 struct c_binding *b;
9535 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9536 if (B_IN_SCOPE (b, external_scope))
9537 break;
9538 if (b)
9540 tree ext_decl, ext_type;
9541 ext_decl = b->decl;
9542 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9543 if (TREE_CODE (ext_type) == FUNCTION_TYPE
9544 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9545 TREE_TYPE (ext_type)))
9547 current_function_prototype_locus
9548 = DECL_SOURCE_LOCATION (ext_decl);
9549 current_function_prototype_built_in
9550 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9551 current_function_prototype_arg_types
9552 = TYPE_ARG_TYPES (ext_type);
9558 /* Optionally warn of old-fashioned def with no previous prototype. */
9559 if (warn_strict_prototypes
9560 && old_decl != error_mark_node
9561 && !prototype_p (TREE_TYPE (decl1))
9562 && C_DECL_ISNT_PROTOTYPE (old_decl))
9563 warning_at (loc, OPT_Wstrict_prototypes,
9564 "function declaration isn%'t a prototype");
9565 /* Optionally warn of any global def with no previous prototype. */
9566 else if (warn_missing_prototypes
9567 && old_decl != error_mark_node
9568 && TREE_PUBLIC (decl1)
9569 && !MAIN_NAME_P (DECL_NAME (decl1))
9570 && C_DECL_ISNT_PROTOTYPE (old_decl)
9571 && !DECL_DECLARED_INLINE_P (decl1))
9572 warning_at (loc, OPT_Wmissing_prototypes,
9573 "no previous prototype for %qD", decl1);
9574 /* Optionally warn of any def with no previous prototype
9575 if the function has already been used. */
9576 else if (warn_missing_prototypes
9577 && old_decl != NULL_TREE
9578 && old_decl != error_mark_node
9579 && TREE_USED (old_decl)
9580 && !prototype_p (TREE_TYPE (old_decl)))
9581 warning_at (loc, OPT_Wmissing_prototypes,
9582 "%qD was used with no prototype before its definition", decl1);
9583 /* Optionally warn of any global def with no previous declaration. */
9584 else if (warn_missing_declarations
9585 && TREE_PUBLIC (decl1)
9586 && old_decl == NULL_TREE
9587 && !MAIN_NAME_P (DECL_NAME (decl1))
9588 && !DECL_DECLARED_INLINE_P (decl1))
9589 warning_at (loc, OPT_Wmissing_declarations,
9590 "no previous declaration for %qD",
9591 decl1);
9592 /* Optionally warn of any def with no previous declaration
9593 if the function has already been used. */
9594 else if (warn_missing_declarations
9595 && old_decl != NULL_TREE
9596 && old_decl != error_mark_node
9597 && TREE_USED (old_decl)
9598 && C_DECL_IMPLICIT (old_decl))
9599 warning_at (loc, OPT_Wmissing_declarations,
9600 "%qD was used with no declaration before its definition", decl1);
9602 /* This function exists in static storage.
9603 (This does not mean `static' in the C sense!) */
9604 TREE_STATIC (decl1) = 1;
9606 /* This is the earliest point at which we might know the assembler
9607 name of the function. Thus, if it's set before this, die horribly. */
9608 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9610 /* If #pragma weak was used, mark the decl weak now. */
9611 if (current_scope == file_scope)
9612 maybe_apply_pragma_weak (decl1);
9614 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9615 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9617 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9618 != integer_type_node)
9619 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9620 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9621 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9622 decl1);
9624 check_main_parameter_types (decl1);
9626 if (!TREE_PUBLIC (decl1))
9627 pedwarn (loc, OPT_Wmain,
9628 "%qD is normally a non-static function", decl1);
9631 tree parms = current_function_arg_info->parms;
9632 if (old_decl)
9634 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
9635 warn_parm_array_mismatch (origloc, old_decl, parms);
9638 /* Record the decl so that the function name is defined.
9639 If we already have a decl for this name, and it is a FUNCTION_DECL,
9640 use the old decl. */
9642 current_function_decl = pushdecl (decl1);
9644 if (tree access = build_attr_access_from_parms (parms, false))
9645 decl_attributes (&current_function_decl, access, ATTR_FLAG_INTERNAL,
9646 old_decl);
9648 push_scope ();
9649 declare_parm_level ();
9651 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9652 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9653 DECL_ARTIFICIAL (resdecl) = 1;
9654 DECL_IGNORED_P (resdecl) = 1;
9655 DECL_RESULT (current_function_decl) = resdecl;
9657 start_fname_decls ();
9659 return true;
9662 /* Subroutine of store_parm_decls which handles new-style function
9663 definitions (prototype format). The parms already have decls, so we
9664 need only record them as in effect and complain if any redundant
9665 old-style parm decls were written. */
9666 static void
9667 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9669 tree decl;
9670 c_arg_tag *tag;
9671 unsigned ix;
9673 if (current_scope->bindings)
9675 error_at (DECL_SOURCE_LOCATION (fndecl),
9676 "old-style parameter declarations in prototyped "
9677 "function definition");
9679 /* Get rid of the old-style declarations. */
9680 pop_scope ();
9681 push_scope ();
9683 /* Don't issue this warning for nested functions, and don't issue this
9684 warning if we got here because ARG_INFO_TYPES was error_mark_node
9685 (this happens when a function definition has just an ellipsis in
9686 its parameter list). */
9687 else if (!in_system_header_at (input_location)
9688 && !current_function_scope
9689 && arg_info->types != error_mark_node)
9690 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9691 "traditional C rejects ISO C style function definitions");
9693 /* Now make all the parameter declarations visible in the function body.
9694 We can bypass most of the grunt work of pushdecl. */
9695 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9697 DECL_CONTEXT (decl) = current_function_decl;
9698 if (DECL_NAME (decl))
9700 bind (DECL_NAME (decl), decl, current_scope,
9701 /*invisible=*/false, /*nested=*/false,
9702 UNKNOWN_LOCATION);
9703 if (!TREE_USED (decl))
9704 warn_if_shadowing (decl);
9706 else
9707 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9708 "ISO C does not support omitting parameter names in "
9709 "function definitions before C2X");
9712 /* Record the parameter list in the function declaration. */
9713 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9715 /* Now make all the ancillary declarations visible, likewise. */
9716 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9718 DECL_CONTEXT (decl) = current_function_decl;
9719 if (DECL_NAME (decl))
9720 bind (DECL_NAME (decl), decl, current_scope,
9721 /*invisible=*/false,
9722 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9723 UNKNOWN_LOCATION);
9726 /* And all the tag declarations. */
9727 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9728 if (tag->id)
9729 bind (tag->id, tag->type, current_scope,
9730 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9733 /* Subroutine of store_parm_decls which handles old-style function
9734 definitions (separate parameter list and declarations). */
9736 static void
9737 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9739 struct c_binding *b;
9740 tree parm, decl, last;
9741 tree parmids = arg_info->parms;
9742 hash_set<tree> seen_args;
9744 if (!in_system_header_at (input_location))
9746 if (flag_isoc2x)
9747 pedwarn (DECL_SOURCE_LOCATION (fndecl),
9748 OPT_Wold_style_definition, "old-style function definition");
9749 else
9750 warning_at (DECL_SOURCE_LOCATION (fndecl),
9751 OPT_Wold_style_definition,
9752 "old-style function definition");
9755 if (current_scope->had_vla_unspec)
9756 error ("%<[*]%> not allowed in other than function prototype scope");
9758 /* Match each formal parameter name with its declaration. Save each
9759 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9760 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9762 if (TREE_VALUE (parm) == NULL_TREE)
9764 error_at (DECL_SOURCE_LOCATION (fndecl),
9765 "parameter name missing from parameter list");
9766 TREE_PURPOSE (parm) = NULL_TREE;
9767 continue;
9770 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9771 if (b && B_IN_CURRENT_SCOPE (b))
9773 decl = b->decl;
9774 /* Skip erroneous parameters. */
9775 if (decl == error_mark_node)
9776 continue;
9777 /* If we got something other than a PARM_DECL it is an error. */
9778 if (TREE_CODE (decl) != PARM_DECL)
9780 error_at (DECL_SOURCE_LOCATION (decl),
9781 "%qD declared as a non-parameter", decl);
9782 continue;
9784 /* If the declaration is already marked, we have a duplicate
9785 name. Complain and ignore the duplicate. */
9786 else if (seen_args.contains (decl))
9788 error_at (DECL_SOURCE_LOCATION (decl),
9789 "multiple parameters named %qD", decl);
9790 TREE_PURPOSE (parm) = NULL_TREE;
9791 continue;
9793 /* If the declaration says "void", complain and turn it into
9794 an int. */
9795 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9797 error_at (DECL_SOURCE_LOCATION (decl),
9798 "parameter %qD declared with void type", decl);
9799 TREE_TYPE (decl) = integer_type_node;
9800 DECL_ARG_TYPE (decl) = integer_type_node;
9801 layout_decl (decl, 0);
9803 warn_if_shadowing (decl);
9805 /* If no declaration found, default to int. */
9806 else
9808 /* FIXME diagnostics: This should be the location of the argument,
9809 not the FNDECL. E.g., for an old-style declaration
9811 int f10(v) { blah; }
9813 We should use the location of the V, not the F10.
9814 Unfortunately, the V is an IDENTIFIER_NODE which has no
9815 location. In the future we need locations for c_arg_info
9816 entries.
9818 See gcc.dg/Wshadow-3.c for an example of this problem. */
9819 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9820 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9821 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9822 pushdecl (decl);
9823 warn_if_shadowing (decl);
9825 if (flag_isoc99)
9826 pedwarn (DECL_SOURCE_LOCATION (decl),
9827 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9828 decl);
9829 else
9830 warning_at (DECL_SOURCE_LOCATION (decl),
9831 OPT_Wmissing_parameter_type,
9832 "type of %qD defaults to %<int%>", decl);
9835 TREE_PURPOSE (parm) = decl;
9836 seen_args.add (decl);
9839 /* Now examine the parms chain for incomplete declarations
9840 and declarations with no corresponding names. */
9842 for (b = current_scope->bindings; b; b = b->prev)
9844 parm = b->decl;
9845 if (TREE_CODE (parm) != PARM_DECL)
9846 continue;
9848 if (TREE_TYPE (parm) != error_mark_node
9849 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9851 error_at (DECL_SOURCE_LOCATION (parm),
9852 "parameter %qD has incomplete type", parm);
9853 TREE_TYPE (parm) = error_mark_node;
9856 if (!seen_args.contains (parm))
9858 error_at (DECL_SOURCE_LOCATION (parm),
9859 "declaration for parameter %qD but no such parameter",
9860 parm);
9862 /* Pretend the parameter was not missing.
9863 This gets us to a standard state and minimizes
9864 further error messages. */
9865 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9869 /* Chain the declarations together in the order of the list of
9870 names. Store that chain in the function decl, replacing the
9871 list of names. Update the current scope to match. */
9872 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9874 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9875 if (TREE_PURPOSE (parm))
9876 break;
9877 if (parm && TREE_PURPOSE (parm))
9879 last = TREE_PURPOSE (parm);
9880 DECL_ARGUMENTS (fndecl) = last;
9882 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9883 if (TREE_PURPOSE (parm))
9885 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9886 last = TREE_PURPOSE (parm);
9888 DECL_CHAIN (last) = NULL_TREE;
9891 /* If there was a previous prototype,
9892 set the DECL_ARG_TYPE of each argument according to
9893 the type previously specified, and report any mismatches. */
9895 if (current_function_prototype_arg_types)
9897 tree type;
9898 for (parm = DECL_ARGUMENTS (fndecl),
9899 type = current_function_prototype_arg_types;
9900 parm || (type != NULL_TREE
9901 && TREE_VALUE (type) != error_mark_node
9902 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9903 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9905 if (parm == NULL_TREE
9906 || type == NULL_TREE
9907 || (TREE_VALUE (type) != error_mark_node
9908 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9910 if (current_function_prototype_built_in)
9911 warning_at (DECL_SOURCE_LOCATION (fndecl),
9912 0, "number of arguments doesn%'t match "
9913 "built-in prototype");
9914 else
9916 /* FIXME diagnostics: This should be the location of
9917 FNDECL, but there is bug when a prototype is
9918 declared inside function context, but defined
9919 outside of it (e.g., gcc.dg/pr15698-2.c). In
9920 which case FNDECL gets the location of the
9921 prototype, not the definition. */
9922 error_at (input_location,
9923 "number of arguments doesn%'t match prototype");
9925 error_at (current_function_prototype_locus,
9926 "prototype declaration");
9928 break;
9930 /* Type for passing arg must be consistent with that
9931 declared for the arg. ISO C says we take the unqualified
9932 type for parameters declared with qualified type. */
9933 if (TREE_TYPE (parm) != error_mark_node
9934 && TREE_VALUE (type) != error_mark_node
9935 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9936 != TYPE_ATOMIC (TREE_VALUE (type)))
9937 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9938 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9940 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9941 == TYPE_ATOMIC (TREE_VALUE (type)))
9942 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9943 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9945 /* Adjust argument to match prototype. E.g. a previous
9946 `int foo(float);' prototype causes
9947 `int foo(x) float x; {...}' to be treated like
9948 `int foo(float x) {...}'. This is particularly
9949 useful for argument types like uid_t. */
9950 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9952 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9953 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9954 && (TYPE_PRECISION (TREE_TYPE (parm))
9955 < TYPE_PRECISION (integer_type_node)))
9956 DECL_ARG_TYPE (parm)
9957 = c_type_promotes_to (TREE_TYPE (parm));
9959 /* ??? Is it possible to get here with a
9960 built-in prototype or will it always have
9961 been diagnosed as conflicting with an
9962 old-style definition and discarded? */
9963 if (current_function_prototype_built_in)
9964 warning_at (DECL_SOURCE_LOCATION (parm),
9965 OPT_Wpedantic, "promoted argument %qD "
9966 "doesn%'t match built-in prototype", parm);
9967 else
9969 pedwarn (DECL_SOURCE_LOCATION (parm),
9970 OPT_Wpedantic, "promoted argument %qD "
9971 "doesn%'t match prototype", parm);
9972 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9973 "prototype declaration");
9976 else
9978 if (current_function_prototype_built_in)
9979 warning_at (DECL_SOURCE_LOCATION (parm),
9980 0, "argument %qD doesn%'t match "
9981 "built-in prototype", parm);
9982 else
9984 error_at (DECL_SOURCE_LOCATION (parm),
9985 "argument %qD doesn%'t match prototype", parm);
9986 error_at (current_function_prototype_locus,
9987 "prototype declaration");
9992 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9995 /* Otherwise, create a prototype that would match. */
9997 else
9999 tree actual = NULL_TREE, last = NULL_TREE, type;
10001 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10003 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10004 if (last)
10005 TREE_CHAIN (last) = type;
10006 else
10007 actual = type;
10008 last = type;
10010 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10011 if (last)
10012 TREE_CHAIN (last) = type;
10013 else
10014 actual = type;
10016 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10017 of the type of this function, but we need to avoid having this
10018 affect the types of other similarly-typed functions, so we must
10019 first force the generation of an identical (but separate) type
10020 node for the relevant function type. The new node we create
10021 will be a variant of the main variant of the original function
10022 type. */
10024 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10026 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10030 /* Store parameter declarations passed in ARG_INFO into the current
10031 function declaration. */
10033 void
10034 store_parm_decls_from (struct c_arg_info *arg_info)
10036 current_function_arg_info = arg_info;
10037 store_parm_decls ();
10040 /* Called by walk_tree to look for and update context-less labels
10041 or labels with context in the parent function. */
10043 static tree
10044 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10046 tree ctx = static_cast<tree>(data);
10047 if (TREE_CODE (*tp) == LABEL_EXPR
10048 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10049 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10051 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10052 *walk_subtrees = 0;
10055 return NULL_TREE;
10058 /* Store the parameter declarations into the current function declaration.
10059 This is called after parsing the parameter declarations, before
10060 digesting the body of the function.
10062 For an old-style definition, construct a prototype out of the old-style
10063 parameter declarations and inject it into the function's type. */
10065 void
10066 store_parm_decls (void)
10068 tree fndecl = current_function_decl;
10069 bool proto;
10071 /* The argument information block for FNDECL. */
10072 struct c_arg_info *arg_info = current_function_arg_info;
10073 current_function_arg_info = 0;
10075 /* True if this definition is written with a prototype. In C2X, an
10076 empty argument list was converted to (void) in grokparms; in
10077 older C standard versions, it does not give the function a type
10078 with a prototype for future calls. */
10079 proto = arg_info->types != 0;
10081 if (proto)
10082 store_parm_decls_newstyle (fndecl, arg_info);
10083 else
10084 store_parm_decls_oldstyle (fndecl, arg_info);
10086 /* The next call to push_scope will be a function body. */
10088 next_is_function_body = true;
10090 /* Write a record describing this function definition to the prototypes
10091 file (if requested). */
10093 gen_aux_info_record (fndecl, 1, 0, proto);
10095 /* Initialize the RTL code for the function. */
10096 allocate_struct_function (fndecl, false);
10098 if (warn_unused_local_typedefs)
10099 cfun->language = ggc_cleared_alloc<language_function> ();
10101 /* Begin the statement tree for this function. */
10102 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10104 /* ??? Insert the contents of the pending sizes list into the function
10105 to be evaluated. The only reason left to have this is
10106 void foo(int n, int array[n++])
10107 because we throw away the array type in favor of a pointer type, and
10108 thus won't naturally see the SAVE_EXPR containing the increment. All
10109 other pending sizes would be handled by gimplify_parameters. */
10110 if (arg_info->pending_sizes)
10112 /* In very special circumstances, e.g. for code like
10113 _Atomic int i = 5;
10114 void f (int a[i += 2]) {}
10115 we need to execute the atomic assignment on function entry.
10116 But in this case, it is not just a straight store, it has the
10117 op= form, which means that build_atomic_assign has generated
10118 gotos, labels, etc. Because at that time the function decl
10119 for F has not been created yet, those labels do not have any
10120 function context. But we have the fndecl now, so update the
10121 labels accordingly. gimplify_expr would crash otherwise.
10122 Or with nested functions the labels could be created with parent
10123 function's context, while when the statement is emitted at the
10124 start of the nested function, it needs the nested function's
10125 context. */
10126 walk_tree_without_duplicates (&arg_info->pending_sizes,
10127 set_labels_context_r, fndecl);
10128 add_stmt (arg_info->pending_sizes);
10132 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
10133 c_finish_omp_declare_simd for function prototypes. No diagnostics
10134 should be done. */
10136 void
10137 temp_store_parm_decls (tree fndecl, tree parms)
10139 push_scope ();
10140 for (tree p = parms; p; p = DECL_CHAIN (p))
10142 DECL_CONTEXT (p) = fndecl;
10143 if (DECL_NAME (p))
10144 bind (DECL_NAME (p), p, current_scope,
10145 /*invisible=*/false, /*nested=*/false,
10146 UNKNOWN_LOCATION);
10150 /* Undo what temp_store_parm_decls did. */
10152 void
10153 temp_pop_parm_decls (void)
10155 /* Clear all bindings in this temporary scope, so that
10156 pop_scope doesn't create a BLOCK. */
10157 struct c_binding *b = current_scope->bindings;
10158 current_scope->bindings = NULL;
10159 for (; b; b = free_binding_and_advance (b))
10161 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10162 || b->decl == error_mark_node);
10163 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10164 I_SYMBOL_BINDING (b->id) = b->shadowed;
10165 if (b->shadowed && b->shadowed->u.type)
10166 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10168 pop_scope ();
10172 /* Finish up a function declaration and compile that function
10173 all the way to assembler language output. Then free the storage
10174 for the function definition.
10176 This is called after parsing the body of the function definition. */
10178 void
10179 finish_function (location_t end_loc)
10181 tree fndecl = current_function_decl;
10183 if (c_dialect_objc ())
10184 objc_finish_function ();
10186 if (TREE_CODE (fndecl) == FUNCTION_DECL
10187 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10189 tree args = DECL_ARGUMENTS (fndecl);
10190 for (; args; args = DECL_CHAIN (args))
10192 tree type = TREE_TYPE (args);
10193 if (INTEGRAL_TYPE_P (type)
10194 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10195 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10199 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10200 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10202 /* Must mark the RESULT_DECL as being in this function. */
10204 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10205 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10207 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
10208 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10209 == integer_type_node && flag_isoc99)
10211 /* Hack. We don't want the middle-end to warn that this return
10212 is unreachable, so we mark its location as special. Using
10213 UNKNOWN_LOCATION has the problem that it gets clobbered in
10214 annotate_one_with_locus. A cleaner solution might be to
10215 ensure ! should_carry_locus_p (stmt), but that needs a flag.
10217 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10220 /* Tie off the statement tree for this function. */
10221 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10223 finish_fname_decls ();
10225 /* Complain if there's no return statement only if option specified on
10226 command line. */
10227 if (warn_return_type > 0
10228 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10229 && !current_function_returns_value && !current_function_returns_null
10230 /* Don't complain if we are no-return. */
10231 && !current_function_returns_abnormally
10232 /* Don't complain if we are declared noreturn. */
10233 && !TREE_THIS_VOLATILE (fndecl)
10234 /* Don't warn for main(). */
10235 && !MAIN_NAME_P (DECL_NAME (fndecl))
10236 /* Or if they didn't actually specify a return type. */
10237 && !C_FUNCTION_IMPLICIT_INT (fndecl)
10238 /* Normally, with -Wreturn-type, flow will complain, but we might
10239 optimize out static functions. */
10240 && !TREE_PUBLIC (fndecl)
10241 && targetm.warn_func_return (fndecl)
10242 && warning (OPT_Wreturn_type,
10243 "no return statement in function returning non-void"))
10244 TREE_NO_WARNING (fndecl) = 1;
10246 /* Complain about parameters that are only set, but never otherwise used. */
10247 if (warn_unused_but_set_parameter)
10249 tree decl;
10251 for (decl = DECL_ARGUMENTS (fndecl);
10252 decl;
10253 decl = DECL_CHAIN (decl))
10254 if (TREE_USED (decl)
10255 && TREE_CODE (decl) == PARM_DECL
10256 && !DECL_READ_P (decl)
10257 && DECL_NAME (decl)
10258 && !DECL_ARTIFICIAL (decl)
10259 && !TREE_NO_WARNING (decl))
10260 warning_at (DECL_SOURCE_LOCATION (decl),
10261 OPT_Wunused_but_set_parameter,
10262 "parameter %qD set but not used", decl);
10265 /* Complain about locally defined typedefs that are not used in this
10266 function. */
10267 maybe_warn_unused_local_typedefs ();
10269 /* Possibly warn about unused parameters. */
10270 if (warn_unused_parameter)
10271 do_warn_unused_parameter (fndecl);
10273 /* Store the end of the function, so that we get good line number
10274 info for the epilogue. */
10275 cfun->function_end_locus = end_loc;
10277 /* Finalize the ELF visibility for the function. */
10278 c_determine_visibility (fndecl);
10280 /* For GNU C extern inline functions disregard inline limits. */
10281 if (DECL_EXTERNAL (fndecl)
10282 && DECL_DECLARED_INLINE_P (fndecl)
10283 && (flag_gnu89_inline
10284 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10285 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10287 /* Genericize before inlining. Delay genericizing nested functions
10288 until their parent function is genericized. Since finalizing
10289 requires GENERIC, delay that as well. */
10291 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10292 && !undef_nested_function)
10294 if (!decl_function_context (fndecl))
10296 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10297 c_genericize (fndecl);
10299 /* ??? Objc emits functions after finalizing the compilation unit.
10300 This should be cleaned up later and this conditional removed. */
10301 if (symtab->global_info_ready)
10303 cgraph_node::add_new_function (fndecl, false);
10304 return;
10306 cgraph_node::finalize_function (fndecl, false);
10308 else
10310 /* Register this function with cgraph just far enough to get it
10311 added to our parent's nested function list. Handy, since the
10312 C front end doesn't have such a list. */
10313 (void) cgraph_node::get_create (fndecl);
10317 if (!decl_function_context (fndecl))
10318 undef_nested_function = false;
10320 if (cfun->language != NULL)
10322 ggc_free (cfun->language);
10323 cfun->language = NULL;
10326 /* We're leaving the context of this function, so zap cfun.
10327 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10328 tree_rest_of_compilation. */
10329 set_cfun (NULL);
10330 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10331 current_function_decl = NULL;
10334 /* Check the declarations given in a for-loop for satisfying the C99
10335 constraints. If exactly one such decl is found, return it. LOC is
10336 the location of the opening parenthesis of the for loop. The last
10337 parameter allows you to control the "for loop initial declarations
10338 are only allowed in C99 mode". Normally, you should pass
10339 flag_isoc99 as that parameter. But in some cases (Objective-C
10340 foreach loop, for example) we want to run the checks in this
10341 function even if not in C99 mode, so we allow the caller to turn
10342 off the error about not being in C99 mode.
10345 tree
10346 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10348 struct c_binding *b;
10349 tree one_decl = NULL_TREE;
10350 int n_decls = 0;
10352 if (!turn_off_iso_c99_error)
10354 static bool hint = true;
10355 /* If we get here, declarations have been used in a for loop without
10356 the C99 for loop scope. This doesn't make much sense, so don't
10357 allow it. */
10358 error_at (loc, "%<for%> loop initial declarations "
10359 "are only allowed in C99 or C11 mode");
10360 if (hint)
10362 inform (loc,
10363 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
10364 "%<-std=gnu11%> to compile your code");
10365 hint = false;
10367 return NULL_TREE;
10369 else
10370 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10371 "initial declarations");
10373 /* C99 subclause 6.8.5 paragraph 3:
10375 [#3] The declaration part of a for statement shall only
10376 declare identifiers for objects having storage class auto or
10377 register.
10379 It isn't clear whether, in this sentence, "identifiers" binds to
10380 "shall only declare" or to "objects" - that is, whether all identifiers
10381 declared must be identifiers for objects, or whether the restriction
10382 only applies to those that are. (A question on this in comp.std.c
10383 in November 2000 received no answer.) We implement the strictest
10384 interpretation, to avoid creating an extension which later causes
10385 problems. */
10387 for (b = current_scope->bindings; b; b = b->prev)
10389 tree id = b->id;
10390 tree decl = b->decl;
10392 if (!id)
10393 continue;
10395 switch (TREE_CODE (decl))
10397 case VAR_DECL:
10399 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10400 if (TREE_STATIC (decl))
10401 error_at (decl_loc,
10402 "declaration of static variable %qD in %<for%> loop "
10403 "initial declaration", decl);
10404 else if (DECL_EXTERNAL (decl))
10405 error_at (decl_loc,
10406 "declaration of %<extern%> variable %qD in %<for%> loop "
10407 "initial declaration", decl);
10409 break;
10411 case RECORD_TYPE:
10412 error_at (loc,
10413 "%<struct %E%> declared in %<for%> loop initial "
10414 "declaration", id);
10415 break;
10416 case UNION_TYPE:
10417 error_at (loc,
10418 "%<union %E%> declared in %<for%> loop initial declaration",
10419 id);
10420 break;
10421 case ENUMERAL_TYPE:
10422 error_at (loc, "%<enum %E%> declared in %<for%> loop "
10423 "initial declaration", id);
10424 break;
10425 default:
10426 error_at (loc, "declaration of non-variable "
10427 "%qD in %<for%> loop initial declaration", decl);
10430 n_decls++;
10431 one_decl = decl;
10434 return n_decls == 1 ? one_decl : NULL_TREE;
10437 /* Save and reinitialize the variables
10438 used during compilation of a C function. */
10440 void
10441 c_push_function_context (void)
10443 struct language_function *p = cfun->language;
10444 /* cfun->language might have been already allocated by the use of
10445 -Wunused-local-typedefs. In that case, just re-use it. */
10446 if (p == NULL)
10447 cfun->language = p = ggc_cleared_alloc<language_function> ();
10449 p->base.x_stmt_tree = c_stmt_tree;
10450 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10451 p->x_in_statement = in_statement;
10452 p->x_switch_stack = c_switch_stack;
10453 p->arg_info = current_function_arg_info;
10454 p->returns_value = current_function_returns_value;
10455 p->returns_null = current_function_returns_null;
10456 p->returns_abnormally = current_function_returns_abnormally;
10457 p->warn_about_return_type = warn_about_return_type;
10459 push_function_context ();
10462 /* Restore the variables used during compilation of a C function. */
10464 void
10465 c_pop_function_context (void)
10467 struct language_function *p;
10469 pop_function_context ();
10470 p = cfun->language;
10472 /* When -Wunused-local-typedefs is in effect, cfun->languages is
10473 used to store data throughout the life time of the current cfun,
10474 So don't deallocate it. */
10475 if (!warn_unused_local_typedefs)
10476 cfun->language = NULL;
10478 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10479 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10481 /* Stop pointing to the local nodes about to be freed. */
10482 /* But DECL_INITIAL must remain nonzero so we know this
10483 was an actual function definition. */
10484 DECL_INITIAL (current_function_decl) = error_mark_node;
10485 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10488 c_stmt_tree = p->base.x_stmt_tree;
10489 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10490 in_statement = p->x_in_statement;
10491 c_switch_stack = p->x_switch_stack;
10492 current_function_arg_info = p->arg_info;
10493 current_function_returns_value = p->returns_value;
10494 current_function_returns_null = p->returns_null;
10495 current_function_returns_abnormally = p->returns_abnormally;
10496 warn_about_return_type = p->warn_about_return_type;
10499 /* The functions below are required for functionality of doing
10500 function at once processing in the C front end. Currently these
10501 functions are not called from anywhere in the C front end, but as
10502 these changes continue, that will change. */
10504 /* Returns the stmt_tree (if any) to which statements are currently
10505 being added. If there is no active statement-tree, NULL is
10506 returned. */
10508 stmt_tree
10509 current_stmt_tree (void)
10511 return &c_stmt_tree;
10514 /* Return the global value of T as a symbol. */
10516 tree
10517 identifier_global_value (tree t)
10519 struct c_binding *b;
10521 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10522 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10523 return b->decl;
10525 return NULL_TREE;
10528 /* Return the global value of tag T as a symbol. */
10530 tree
10531 identifier_global_tag (tree t)
10533 struct c_binding *b;
10535 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10536 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10537 return b->decl;
10539 return NULL_TREE;
10542 /* Returns true if NAME refers to a built-in function or function-like
10543 operator. */
10545 bool
10546 names_builtin_p (const char *name)
10548 tree id = get_identifier (name);
10549 if (tree decl = identifier_global_value (id))
10550 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
10552 /* Also detect common reserved C words that aren't strictly built-in
10553 functions. */
10554 switch (C_RID_CODE (id))
10556 case RID_BUILTIN_CONVERTVECTOR:
10557 case RID_BUILTIN_HAS_ATTRIBUTE:
10558 case RID_BUILTIN_SHUFFLE:
10559 case RID_BUILTIN_SHUFFLEVECTOR:
10560 case RID_CHOOSE_EXPR:
10561 case RID_OFFSETOF:
10562 case RID_TYPES_COMPATIBLE_P:
10563 return true;
10564 default:
10565 break;
10568 return false;
10571 /* In C, the only C-linkage public declaration is at file scope. */
10573 tree
10574 c_linkage_bindings (tree name)
10576 return identifier_global_value (name);
10579 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
10580 otherwise the name is found in ridpointers from RID_INDEX. */
10582 void
10583 record_builtin_type (enum rid rid_index, const char *name, tree type)
10585 tree id, decl;
10586 if (name == 0)
10587 id = ridpointers[(int) rid_index];
10588 else
10589 id = get_identifier (name);
10590 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10591 pushdecl (decl);
10592 if (debug_hooks->type_decl)
10593 debug_hooks->type_decl (decl, false);
10596 /* Build the void_list_node (void_type_node having been created). */
10597 tree
10598 build_void_list_node (void)
10600 tree t = build_tree_list (NULL_TREE, void_type_node);
10601 return t;
10604 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10606 struct c_parm *
10607 build_c_parm (struct c_declspecs *specs, tree attrs,
10608 struct c_declarator *declarator,
10609 location_t loc)
10611 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10612 ret->specs = specs;
10613 ret->attrs = attrs;
10614 ret->declarator = declarator;
10615 ret->loc = loc;
10616 return ret;
10619 /* Return a declarator with nested attributes. TARGET is the inner
10620 declarator to which these attributes apply. ATTRS are the
10621 attributes. */
10623 struct c_declarator *
10624 build_attrs_declarator (tree attrs, struct c_declarator *target)
10626 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10627 ret->kind = cdk_attrs;
10628 ret->declarator = target;
10629 ret->u.attrs = attrs;
10630 return ret;
10633 /* Return a declarator for a function with arguments specified by ARGS
10634 and return type specified by TARGET. */
10636 struct c_declarator *
10637 build_function_declarator (struct c_arg_info *args,
10638 struct c_declarator *target)
10640 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10641 ret->kind = cdk_function;
10642 ret->declarator = target;
10643 ret->u.arg_info = args;
10644 return ret;
10647 /* Return a declarator for the identifier IDENT (which may be
10648 NULL_TREE for an abstract declarator). */
10650 struct c_declarator *
10651 build_id_declarator (tree ident)
10653 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10654 ret->kind = cdk_id;
10655 ret->declarator = 0;
10656 ret->u.id.id = ident;
10657 ret->u.id.attrs = NULL_TREE;
10658 /* Default value - may get reset to a more precise location. */
10659 ret->id_loc = input_location;
10660 return ret;
10663 /* Return something to represent absolute declarators containing a *.
10664 TARGET is the absolute declarator that the * contains.
10665 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10666 to apply to the pointer type. */
10668 struct c_declarator *
10669 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10670 struct c_declarator *target)
10672 tree attrs;
10673 int quals = 0;
10674 struct c_declarator *itarget = target;
10675 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10676 if (type_quals_attrs)
10678 attrs = type_quals_attrs->attrs;
10679 quals = quals_from_declspecs (type_quals_attrs);
10680 if (attrs != NULL_TREE)
10681 itarget = build_attrs_declarator (attrs, target);
10683 ret->kind = cdk_pointer;
10684 ret->declarator = itarget;
10685 ret->u.pointer_quals = quals;
10686 return ret;
10689 /* Return a pointer to a structure for an empty list of declaration
10690 specifiers. */
10692 struct c_declspecs *
10693 build_null_declspecs (void)
10695 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10696 memset (ret, 0, sizeof *ret);
10697 ret->align_log = -1;
10698 ret->typespec_word = cts_none;
10699 ret->storage_class = csc_none;
10700 ret->expr_const_operands = true;
10701 ret->typespec_kind = ctsk_none;
10702 ret->address_space = ADDR_SPACE_GENERIC;
10703 return ret;
10706 /* Add the address space ADDRSPACE to the declaration specifiers
10707 SPECS, returning SPECS. */
10709 struct c_declspecs *
10710 declspecs_add_addrspace (location_t location,
10711 struct c_declspecs *specs, addr_space_t as)
10713 specs->non_sc_seen_p = true;
10714 specs->declspecs_seen_p = true;
10715 specs->non_std_attrs_seen_p = true;
10717 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10718 && specs->address_space != as)
10719 error ("incompatible address space qualifiers %qs and %qs",
10720 c_addr_space_name (as),
10721 c_addr_space_name (specs->address_space));
10722 else
10724 specs->address_space = as;
10725 specs->locations[cdw_address_space] = location;
10727 return specs;
10730 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10731 returning SPECS. */
10733 struct c_declspecs *
10734 declspecs_add_qual (location_t loc,
10735 struct c_declspecs *specs, tree qual)
10737 enum rid i;
10738 bool dupe = false;
10739 specs->non_sc_seen_p = true;
10740 specs->declspecs_seen_p = true;
10741 specs->non_std_attrs_seen_p = true;
10742 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10743 && C_IS_RESERVED_WORD (qual));
10744 i = C_RID_CODE (qual);
10745 location_t prev_loc = UNKNOWN_LOCATION;
10746 switch (i)
10748 case RID_CONST:
10749 dupe = specs->const_p;
10750 specs->const_p = true;
10751 prev_loc = specs->locations[cdw_const];
10752 specs->locations[cdw_const] = loc;
10753 break;
10754 case RID_VOLATILE:
10755 dupe = specs->volatile_p;
10756 specs->volatile_p = true;
10757 prev_loc = specs->locations[cdw_volatile];
10758 specs->locations[cdw_volatile] = loc;
10759 break;
10760 case RID_RESTRICT:
10761 dupe = specs->restrict_p;
10762 specs->restrict_p = true;
10763 prev_loc = specs->locations[cdw_restrict];
10764 specs->locations[cdw_restrict] = loc;
10765 break;
10766 case RID_ATOMIC:
10767 dupe = specs->atomic_p;
10768 specs->atomic_p = true;
10769 prev_loc = specs->locations[cdw_atomic];
10770 specs->locations[cdw_atomic] = loc;
10771 break;
10772 default:
10773 gcc_unreachable ();
10775 if (dupe)
10777 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10778 "duplicate %qE declaration specifier", qual);
10779 if (!warned
10780 && warn_duplicate_decl_specifier
10781 && prev_loc >= RESERVED_LOCATION_COUNT
10782 && !from_macro_expansion_at (prev_loc)
10783 && !from_macro_expansion_at (loc))
10784 warning_at (loc, OPT_Wduplicate_decl_specifier,
10785 "duplicate %qE declaration specifier", qual);
10787 return specs;
10790 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10791 returning SPECS. */
10793 struct c_declspecs *
10794 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10795 struct c_typespec spec)
10797 tree type = spec.spec;
10798 specs->non_sc_seen_p = true;
10799 specs->declspecs_seen_p = true;
10800 specs->non_std_attrs_seen_p = true;
10801 specs->typespec_kind = spec.kind;
10802 if (TREE_DEPRECATED (type))
10803 specs->deprecated_p = true;
10805 /* Handle type specifier keywords. */
10806 if (TREE_CODE (type) == IDENTIFIER_NODE
10807 && C_IS_RESERVED_WORD (type)
10808 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10810 enum rid i = C_RID_CODE (type);
10811 if (specs->type)
10813 error_at (loc, "two or more data types in declaration specifiers");
10814 return specs;
10816 if ((int) i <= (int) RID_LAST_MODIFIER)
10818 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10819 bool dupe = false;
10820 switch (i)
10822 case RID_LONG:
10823 if (specs->long_long_p)
10825 error_at (loc, "%<long long long%> is too long for GCC");
10826 break;
10828 if (specs->long_p)
10830 if (specs->typespec_word == cts_double)
10832 error_at (loc,
10833 ("both %<long long%> and %<double%> in "
10834 "declaration specifiers"));
10835 break;
10837 pedwarn_c90 (loc, OPT_Wlong_long,
10838 "ISO C90 does not support %<long long%>");
10839 specs->long_long_p = 1;
10840 specs->locations[cdw_long_long] = loc;
10841 break;
10843 if (specs->short_p)
10844 error_at (loc,
10845 ("both %<long%> and %<short%> in "
10846 "declaration specifiers"));
10847 else if (specs->typespec_word == cts_auto_type)
10848 error_at (loc,
10849 ("both %<long%> and %<__auto_type%> in "
10850 "declaration specifiers"));
10851 else if (specs->typespec_word == cts_void)
10852 error_at (loc,
10853 ("both %<long%> and %<void%> in "
10854 "declaration specifiers"));
10855 else if (specs->typespec_word == cts_int_n)
10856 error_at (loc,
10857 ("both %<long%> and %<__int%d%> in "
10858 "declaration specifiers"),
10859 int_n_data[specs->int_n_idx].bitsize);
10860 else if (specs->typespec_word == cts_bool)
10861 error_at (loc,
10862 ("both %<long%> and %<_Bool%> in "
10863 "declaration specifiers"));
10864 else if (specs->typespec_word == cts_char)
10865 error_at (loc,
10866 ("both %<long%> and %<char%> in "
10867 "declaration specifiers"));
10868 else if (specs->typespec_word == cts_float)
10869 error_at (loc,
10870 ("both %<long%> and %<float%> in "
10871 "declaration specifiers"));
10872 else if (specs->typespec_word == cts_floatn_nx)
10873 error_at (loc,
10874 ("both %<long%> and %<_Float%d%s%> in "
10875 "declaration specifiers"),
10876 floatn_nx_types[specs->floatn_nx_idx].n,
10877 (floatn_nx_types[specs->floatn_nx_idx].extended
10878 ? "x"
10879 : ""));
10880 else if (specs->typespec_word == cts_dfloat32)
10881 error_at (loc,
10882 ("both %<long%> and %<_Decimal32%> in "
10883 "declaration specifiers"));
10884 else if (specs->typespec_word == cts_dfloat64)
10885 error_at (loc,
10886 ("both %<long%> and %<_Decimal64%> in "
10887 "declaration specifiers"));
10888 else if (specs->typespec_word == cts_dfloat128)
10889 error_at (loc,
10890 ("both %<long%> and %<_Decimal128%> in "
10891 "declaration specifiers"));
10892 else
10894 specs->long_p = true;
10895 specs->locations[cdw_long] = loc;
10897 break;
10898 case RID_SHORT:
10899 dupe = specs->short_p;
10900 if (specs->long_p)
10901 error_at (loc,
10902 ("both %<long%> and %<short%> in "
10903 "declaration specifiers"));
10904 else if (specs->typespec_word == cts_auto_type)
10905 error_at (loc,
10906 ("both %<short%> and %<__auto_type%> in "
10907 "declaration specifiers"));
10908 else if (specs->typespec_word == cts_void)
10909 error_at (loc,
10910 ("both %<short%> and %<void%> in "
10911 "declaration specifiers"));
10912 else if (specs->typespec_word == cts_int_n)
10913 error_at (loc,
10914 ("both %<short%> and %<__int%d%> in "
10915 "declaration specifiers"),
10916 int_n_data[specs->int_n_idx].bitsize);
10917 else if (specs->typespec_word == cts_bool)
10918 error_at (loc,
10919 ("both %<short%> and %<_Bool%> in "
10920 "declaration specifiers"));
10921 else if (specs->typespec_word == cts_char)
10922 error_at (loc,
10923 ("both %<short%> and %<char%> in "
10924 "declaration specifiers"));
10925 else if (specs->typespec_word == cts_float)
10926 error_at (loc,
10927 ("both %<short%> and %<float%> in "
10928 "declaration specifiers"));
10929 else if (specs->typespec_word == cts_double)
10930 error_at (loc,
10931 ("both %<short%> and %<double%> in "
10932 "declaration specifiers"));
10933 else if (specs->typespec_word == cts_floatn_nx)
10934 error_at (loc,
10935 ("both %<short%> and %<_Float%d%s%> in "
10936 "declaration specifiers"),
10937 floatn_nx_types[specs->floatn_nx_idx].n,
10938 (floatn_nx_types[specs->floatn_nx_idx].extended
10939 ? "x"
10940 : ""));
10941 else if (specs->typespec_word == cts_dfloat32)
10942 error_at (loc,
10943 ("both %<short%> and %<_Decimal32%> in "
10944 "declaration specifiers"));
10945 else if (specs->typespec_word == cts_dfloat64)
10946 error_at (loc,
10947 ("both %<short%> and %<_Decimal64%> in "
10948 "declaration specifiers"));
10949 else if (specs->typespec_word == cts_dfloat128)
10950 error_at (loc,
10951 ("both %<short%> and %<_Decimal128%> in "
10952 "declaration specifiers"));
10953 else
10955 specs->short_p = true;
10956 specs->locations[cdw_short] = loc;
10958 break;
10959 case RID_SIGNED:
10960 dupe = specs->signed_p;
10961 if (specs->unsigned_p)
10962 error_at (loc,
10963 ("both %<signed%> and %<unsigned%> in "
10964 "declaration specifiers"));
10965 else if (specs->typespec_word == cts_auto_type)
10966 error_at (loc,
10967 ("both %<signed%> and %<__auto_type%> in "
10968 "declaration specifiers"));
10969 else if (specs->typespec_word == cts_void)
10970 error_at (loc,
10971 ("both %<signed%> and %<void%> in "
10972 "declaration specifiers"));
10973 else if (specs->typespec_word == cts_bool)
10974 error_at (loc,
10975 ("both %<signed%> and %<_Bool%> in "
10976 "declaration specifiers"));
10977 else if (specs->typespec_word == cts_float)
10978 error_at (loc,
10979 ("both %<signed%> and %<float%> in "
10980 "declaration specifiers"));
10981 else if (specs->typespec_word == cts_double)
10982 error_at (loc,
10983 ("both %<signed%> and %<double%> in "
10984 "declaration specifiers"));
10985 else if (specs->typespec_word == cts_floatn_nx)
10986 error_at (loc,
10987 ("both %<signed%> and %<_Float%d%s%> in "
10988 "declaration specifiers"),
10989 floatn_nx_types[specs->floatn_nx_idx].n,
10990 (floatn_nx_types[specs->floatn_nx_idx].extended
10991 ? "x"
10992 : ""));
10993 else if (specs->typespec_word == cts_dfloat32)
10994 error_at (loc,
10995 ("both %<signed%> and %<_Decimal32%> in "
10996 "declaration specifiers"));
10997 else if (specs->typespec_word == cts_dfloat64)
10998 error_at (loc,
10999 ("both %<signed%> and %<_Decimal64%> in "
11000 "declaration specifiers"));
11001 else if (specs->typespec_word == cts_dfloat128)
11002 error_at (loc,
11003 ("both %<signed%> and %<_Decimal128%> in "
11004 "declaration specifiers"));
11005 else
11007 specs->signed_p = true;
11008 specs->locations[cdw_signed] = loc;
11010 break;
11011 case RID_UNSIGNED:
11012 dupe = specs->unsigned_p;
11013 if (specs->signed_p)
11014 error_at (loc,
11015 ("both %<signed%> and %<unsigned%> in "
11016 "declaration specifiers"));
11017 else if (specs->typespec_word == cts_auto_type)
11018 error_at (loc,
11019 ("both %<unsigned%> and %<__auto_type%> in "
11020 "declaration specifiers"));
11021 else if (specs->typespec_word == cts_void)
11022 error_at (loc,
11023 ("both %<unsigned%> and %<void%> in "
11024 "declaration specifiers"));
11025 else if (specs->typespec_word == cts_bool)
11026 error_at (loc,
11027 ("both %<unsigned%> and %<_Bool%> in "
11028 "declaration specifiers"));
11029 else if (specs->typespec_word == cts_float)
11030 error_at (loc,
11031 ("both %<unsigned%> and %<float%> in "
11032 "declaration specifiers"));
11033 else if (specs->typespec_word == cts_double)
11034 error_at (loc,
11035 ("both %<unsigned%> and %<double%> in "
11036 "declaration specifiers"));
11037 else if (specs->typespec_word == cts_floatn_nx)
11038 error_at (loc,
11039 ("both %<unsigned%> and %<_Float%d%s%> in "
11040 "declaration specifiers"),
11041 floatn_nx_types[specs->floatn_nx_idx].n,
11042 (floatn_nx_types[specs->floatn_nx_idx].extended
11043 ? "x"
11044 : ""));
11045 else if (specs->typespec_word == cts_dfloat32)
11046 error_at (loc,
11047 ("both %<unsigned%> and %<_Decimal32%> in "
11048 "declaration specifiers"));
11049 else if (specs->typespec_word == cts_dfloat64)
11050 error_at (loc,
11051 ("both %<unsigned%> and %<_Decimal64%> in "
11052 "declaration specifiers"));
11053 else if (specs->typespec_word == cts_dfloat128)
11054 error_at (loc,
11055 ("both %<unsigned%> and %<_Decimal128%> in "
11056 "declaration specifiers"));
11057 else
11059 specs->unsigned_p = true;
11060 specs->locations[cdw_unsigned] = loc;
11062 break;
11063 case RID_COMPLEX:
11064 dupe = specs->complex_p;
11065 if (!in_system_header_at (loc))
11066 pedwarn_c90 (loc, OPT_Wpedantic,
11067 "ISO C90 does not support complex types");
11068 if (specs->typespec_word == cts_auto_type)
11069 error_at (loc,
11070 ("both %<complex%> and %<__auto_type%> in "
11071 "declaration specifiers"));
11072 else if (specs->typespec_word == cts_void)
11073 error_at (loc,
11074 ("both %<complex%> and %<void%> in "
11075 "declaration specifiers"));
11076 else if (specs->typespec_word == cts_bool)
11077 error_at (loc,
11078 ("both %<complex%> and %<_Bool%> in "
11079 "declaration specifiers"));
11080 else if (specs->typespec_word == cts_dfloat32)
11081 error_at (loc,
11082 ("both %<complex%> and %<_Decimal32%> in "
11083 "declaration specifiers"));
11084 else if (specs->typespec_word == cts_dfloat64)
11085 error_at (loc,
11086 ("both %<complex%> and %<_Decimal64%> in "
11087 "declaration specifiers"));
11088 else if (specs->typespec_word == cts_dfloat128)
11089 error_at (loc,
11090 ("both %<complex%> and %<_Decimal128%> in "
11091 "declaration specifiers"));
11092 else if (specs->typespec_word == cts_fract)
11093 error_at (loc,
11094 ("both %<complex%> and %<_Fract%> in "
11095 "declaration specifiers"));
11096 else if (specs->typespec_word == cts_accum)
11097 error_at (loc,
11098 ("both %<complex%> and %<_Accum%> in "
11099 "declaration specifiers"));
11100 else if (specs->saturating_p)
11101 error_at (loc,
11102 ("both %<complex%> and %<_Sat%> in "
11103 "declaration specifiers"));
11104 else
11106 specs->complex_p = true;
11107 specs->locations[cdw_complex] = loc;
11109 break;
11110 case RID_SAT:
11111 dupe = specs->saturating_p;
11112 pedwarn (loc, OPT_Wpedantic,
11113 "ISO C does not support saturating types");
11114 if (specs->typespec_word == cts_int_n)
11116 error_at (loc,
11117 ("both %<_Sat%> and %<__int%d%> in "
11118 "declaration specifiers"),
11119 int_n_data[specs->int_n_idx].bitsize);
11121 else if (specs->typespec_word == cts_auto_type)
11122 error_at (loc,
11123 ("both %<_Sat%> and %<__auto_type%> in "
11124 "declaration specifiers"));
11125 else if (specs->typespec_word == cts_void)
11126 error_at (loc,
11127 ("both %<_Sat%> and %<void%> in "
11128 "declaration specifiers"));
11129 else if (specs->typespec_word == cts_bool)
11130 error_at (loc,
11131 ("both %<_Sat%> and %<_Bool%> in "
11132 "declaration specifiers"));
11133 else if (specs->typespec_word == cts_char)
11134 error_at (loc,
11135 ("both %<_Sat%> and %<char%> in "
11136 "declaration specifiers"));
11137 else if (specs->typespec_word == cts_int)
11138 error_at (loc,
11139 ("both %<_Sat%> and %<int%> in "
11140 "declaration specifiers"));
11141 else if (specs->typespec_word == cts_float)
11142 error_at (loc,
11143 ("both %<_Sat%> and %<float%> in "
11144 "declaration specifiers"));
11145 else if (specs->typespec_word == cts_double)
11146 error_at (loc,
11147 ("both %<_Sat%> and %<double%> in "
11148 "declaration specifiers"));
11149 else if (specs->typespec_word == cts_floatn_nx)
11150 error_at (loc,
11151 ("both %<_Sat%> and %<_Float%d%s%> in "
11152 "declaration specifiers"),
11153 floatn_nx_types[specs->floatn_nx_idx].n,
11154 (floatn_nx_types[specs->floatn_nx_idx].extended
11155 ? "x"
11156 : ""));
11157 else if (specs->typespec_word == cts_dfloat32)
11158 error_at (loc,
11159 ("both %<_Sat%> and %<_Decimal32%> in "
11160 "declaration specifiers"));
11161 else if (specs->typespec_word == cts_dfloat64)
11162 error_at (loc,
11163 ("both %<_Sat%> and %<_Decimal64%> in "
11164 "declaration specifiers"));
11165 else if (specs->typespec_word == cts_dfloat128)
11166 error_at (loc,
11167 ("both %<_Sat%> and %<_Decimal128%> in "
11168 "declaration specifiers"));
11169 else if (specs->complex_p)
11170 error_at (loc,
11171 ("both %<_Sat%> and %<complex%> in "
11172 "declaration specifiers"));
11173 else
11175 specs->saturating_p = true;
11176 specs->locations[cdw_saturating] = loc;
11178 break;
11179 default:
11180 gcc_unreachable ();
11183 if (dupe)
11184 error_at (loc, "duplicate %qE", type);
11186 return specs;
11188 else
11190 /* "void", "_Bool", "char", "int", "float", "double",
11191 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11192 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11193 "__auto_type". */
11194 if (specs->typespec_word != cts_none)
11196 error_at (loc,
11197 "two or more data types in declaration specifiers");
11198 return specs;
11200 switch (i)
11202 case RID_AUTO_TYPE:
11203 if (specs->long_p)
11204 error_at (loc,
11205 ("both %<long%> and %<__auto_type%> in "
11206 "declaration specifiers"));
11207 else if (specs->short_p)
11208 error_at (loc,
11209 ("both %<short%> and %<__auto_type%> in "
11210 "declaration specifiers"));
11211 else if (specs->signed_p)
11212 error_at (loc,
11213 ("both %<signed%> and %<__auto_type%> in "
11214 "declaration specifiers"));
11215 else if (specs->unsigned_p)
11216 error_at (loc,
11217 ("both %<unsigned%> and %<__auto_type%> in "
11218 "declaration specifiers"));
11219 else if (specs->complex_p)
11220 error_at (loc,
11221 ("both %<complex%> and %<__auto_type%> in "
11222 "declaration specifiers"));
11223 else if (specs->saturating_p)
11224 error_at (loc,
11225 ("both %<_Sat%> and %<__auto_type%> in "
11226 "declaration specifiers"));
11227 else
11229 specs->typespec_word = cts_auto_type;
11230 specs->locations[cdw_typespec] = loc;
11232 return specs;
11233 case RID_INT_N_0:
11234 case RID_INT_N_1:
11235 case RID_INT_N_2:
11236 case RID_INT_N_3:
11237 specs->int_n_idx = i - RID_INT_N_0;
11238 if (!in_system_header_at (input_location)
11239 /* If the INT_N type ends in "__", and so is of the format
11240 "__intN__", don't pedwarn. */
11241 && (strncmp (IDENTIFIER_POINTER (type)
11242 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11243 pedwarn (loc, OPT_Wpedantic,
11244 "ISO C does not support %<__int%d%> types",
11245 int_n_data[specs->int_n_idx].bitsize);
11247 if (specs->long_p)
11248 error_at (loc,
11249 ("both %<__int%d%> and %<long%> in "
11250 "declaration specifiers"),
11251 int_n_data[specs->int_n_idx].bitsize);
11252 else if (specs->saturating_p)
11253 error_at (loc,
11254 ("both %<_Sat%> and %<__int%d%> in "
11255 "declaration specifiers"),
11256 int_n_data[specs->int_n_idx].bitsize);
11257 else if (specs->short_p)
11258 error_at (loc,
11259 ("both %<__int%d%> and %<short%> in "
11260 "declaration specifiers"),
11261 int_n_data[specs->int_n_idx].bitsize);
11262 else if (! int_n_enabled_p[specs->int_n_idx])
11264 specs->typespec_word = cts_int_n;
11265 error_at (loc,
11266 "%<__int%d%> is not supported on this target",
11267 int_n_data[specs->int_n_idx].bitsize);
11269 else
11271 specs->typespec_word = cts_int_n;
11272 specs->locations[cdw_typespec] = loc;
11274 return specs;
11275 case RID_VOID:
11276 if (specs->long_p)
11277 error_at (loc,
11278 ("both %<long%> and %<void%> in "
11279 "declaration specifiers"));
11280 else if (specs->short_p)
11281 error_at (loc,
11282 ("both %<short%> and %<void%> in "
11283 "declaration specifiers"));
11284 else if (specs->signed_p)
11285 error_at (loc,
11286 ("both %<signed%> and %<void%> in "
11287 "declaration specifiers"));
11288 else if (specs->unsigned_p)
11289 error_at (loc,
11290 ("both %<unsigned%> and %<void%> in "
11291 "declaration specifiers"));
11292 else if (specs->complex_p)
11293 error_at (loc,
11294 ("both %<complex%> and %<void%> in "
11295 "declaration specifiers"));
11296 else if (specs->saturating_p)
11297 error_at (loc,
11298 ("both %<_Sat%> and %<void%> in "
11299 "declaration specifiers"));
11300 else
11302 specs->typespec_word = cts_void;
11303 specs->locations[cdw_typespec] = loc;
11305 return specs;
11306 case RID_BOOL:
11307 if (!in_system_header_at (loc))
11308 pedwarn_c90 (loc, OPT_Wpedantic,
11309 "ISO C90 does not support boolean types");
11310 if (specs->long_p)
11311 error_at (loc,
11312 ("both %<long%> and %<_Bool%> in "
11313 "declaration specifiers"));
11314 else if (specs->short_p)
11315 error_at (loc,
11316 ("both %<short%> and %<_Bool%> in "
11317 "declaration specifiers"));
11318 else if (specs->signed_p)
11319 error_at (loc,
11320 ("both %<signed%> and %<_Bool%> in "
11321 "declaration specifiers"));
11322 else if (specs->unsigned_p)
11323 error_at (loc,
11324 ("both %<unsigned%> and %<_Bool%> in "
11325 "declaration specifiers"));
11326 else if (specs->complex_p)
11327 error_at (loc,
11328 ("both %<complex%> and %<_Bool%> in "
11329 "declaration specifiers"));
11330 else if (specs->saturating_p)
11331 error_at (loc,
11332 ("both %<_Sat%> and %<_Bool%> in "
11333 "declaration specifiers"));
11334 else
11336 specs->typespec_word = cts_bool;
11337 specs->locations[cdw_typespec] = loc;
11339 return specs;
11340 case RID_CHAR:
11341 if (specs->long_p)
11342 error_at (loc,
11343 ("both %<long%> and %<char%> in "
11344 "declaration specifiers"));
11345 else if (specs->short_p)
11346 error_at (loc,
11347 ("both %<short%> and %<char%> in "
11348 "declaration specifiers"));
11349 else if (specs->saturating_p)
11350 error_at (loc,
11351 ("both %<_Sat%> and %<char%> in "
11352 "declaration specifiers"));
11353 else
11355 specs->typespec_word = cts_char;
11356 specs->locations[cdw_typespec] = loc;
11358 return specs;
11359 case RID_INT:
11360 if (specs->saturating_p)
11361 error_at (loc,
11362 ("both %<_Sat%> and %<int%> in "
11363 "declaration specifiers"));
11364 else
11366 specs->typespec_word = cts_int;
11367 specs->locations[cdw_typespec] = loc;
11369 return specs;
11370 case RID_FLOAT:
11371 if (specs->long_p)
11372 error_at (loc,
11373 ("both %<long%> and %<float%> in "
11374 "declaration specifiers"));
11375 else if (specs->short_p)
11376 error_at (loc,
11377 ("both %<short%> and %<float%> in "
11378 "declaration specifiers"));
11379 else if (specs->signed_p)
11380 error_at (loc,
11381 ("both %<signed%> and %<float%> in "
11382 "declaration specifiers"));
11383 else if (specs->unsigned_p)
11384 error_at (loc,
11385 ("both %<unsigned%> and %<float%> in "
11386 "declaration specifiers"));
11387 else if (specs->saturating_p)
11388 error_at (loc,
11389 ("both %<_Sat%> and %<float%> in "
11390 "declaration specifiers"));
11391 else
11393 specs->typespec_word = cts_float;
11394 specs->locations[cdw_typespec] = loc;
11396 return specs;
11397 case RID_DOUBLE:
11398 if (specs->long_long_p)
11399 error_at (loc,
11400 ("both %<long long%> and %<double%> in "
11401 "declaration specifiers"));
11402 else if (specs->short_p)
11403 error_at (loc,
11404 ("both %<short%> and %<double%> in "
11405 "declaration specifiers"));
11406 else if (specs->signed_p)
11407 error_at (loc,
11408 ("both %<signed%> and %<double%> in "
11409 "declaration specifiers"));
11410 else if (specs->unsigned_p)
11411 error_at (loc,
11412 ("both %<unsigned%> and %<double%> in "
11413 "declaration specifiers"));
11414 else if (specs->saturating_p)
11415 error_at (loc,
11416 ("both %<_Sat%> and %<double%> in "
11417 "declaration specifiers"));
11418 else
11420 specs->typespec_word = cts_double;
11421 specs->locations[cdw_typespec] = loc;
11423 return specs;
11424 CASE_RID_FLOATN_NX:
11425 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11426 if (!in_system_header_at (input_location))
11427 pedwarn (loc, OPT_Wpedantic,
11428 "ISO C does not support the %<_Float%d%s%> type",
11429 floatn_nx_types[specs->floatn_nx_idx].n,
11430 (floatn_nx_types[specs->floatn_nx_idx].extended
11431 ? "x"
11432 : ""));
11434 if (specs->long_p)
11435 error_at (loc,
11436 ("both %<long%> and %<_Float%d%s%> in "
11437 "declaration specifiers"),
11438 floatn_nx_types[specs->floatn_nx_idx].n,
11439 (floatn_nx_types[specs->floatn_nx_idx].extended
11440 ? "x"
11441 : ""));
11442 else if (specs->short_p)
11443 error_at (loc,
11444 ("both %<short%> and %<_Float%d%s%> in "
11445 "declaration specifiers"),
11446 floatn_nx_types[specs->floatn_nx_idx].n,
11447 (floatn_nx_types[specs->floatn_nx_idx].extended
11448 ? "x"
11449 : ""));
11450 else if (specs->signed_p)
11451 error_at (loc,
11452 ("both %<signed%> and %<_Float%d%s%> in "
11453 "declaration specifiers"),
11454 floatn_nx_types[specs->floatn_nx_idx].n,
11455 (floatn_nx_types[specs->floatn_nx_idx].extended
11456 ? "x"
11457 : ""));
11458 else if (specs->unsigned_p)
11459 error_at (loc,
11460 ("both %<unsigned%> and %<_Float%d%s%> in "
11461 "declaration specifiers"),
11462 floatn_nx_types[specs->floatn_nx_idx].n,
11463 (floatn_nx_types[specs->floatn_nx_idx].extended
11464 ? "x"
11465 : ""));
11466 else if (specs->saturating_p)
11467 error_at (loc,
11468 ("both %<_Sat%> and %<_Float%d%s%> in "
11469 "declaration specifiers"),
11470 floatn_nx_types[specs->floatn_nx_idx].n,
11471 (floatn_nx_types[specs->floatn_nx_idx].extended
11472 ? "x"
11473 : ""));
11474 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11476 specs->typespec_word = cts_floatn_nx;
11477 error_at (loc,
11478 "%<_Float%d%s%> is not supported on this target",
11479 floatn_nx_types[specs->floatn_nx_idx].n,
11480 (floatn_nx_types[specs->floatn_nx_idx].extended
11481 ? "x"
11482 : ""));
11484 else
11486 specs->typespec_word = cts_floatn_nx;
11487 specs->locations[cdw_typespec] = loc;
11489 return specs;
11490 case RID_DFLOAT32:
11491 case RID_DFLOAT64:
11492 case RID_DFLOAT128:
11494 const char *str;
11495 if (i == RID_DFLOAT32)
11496 str = "_Decimal32";
11497 else if (i == RID_DFLOAT64)
11498 str = "_Decimal64";
11499 else
11500 str = "_Decimal128";
11501 if (specs->long_long_p)
11502 error_at (loc,
11503 ("both %<long long%> and %qs in "
11504 "declaration specifiers"),
11505 str);
11506 if (specs->long_p)
11507 error_at (loc,
11508 ("both %<long%> and %qs in "
11509 "declaration specifiers"),
11510 str);
11511 else if (specs->short_p)
11512 error_at (loc,
11513 ("both %<short%> and %qs in "
11514 "declaration specifiers"),
11515 str);
11516 else if (specs->signed_p)
11517 error_at (loc,
11518 ("both %<signed%> and %qs in "
11519 "declaration specifiers"),
11520 str);
11521 else if (specs->unsigned_p)
11522 error_at (loc,
11523 ("both %<unsigned%> and %qs in "
11524 "declaration specifiers"),
11525 str);
11526 else if (specs->complex_p)
11527 error_at (loc,
11528 ("both %<complex%> and %qs in "
11529 "declaration specifiers"),
11530 str);
11531 else if (specs->saturating_p)
11532 error_at (loc,
11533 ("both %<_Sat%> and %qs in "
11534 "declaration specifiers"),
11535 str);
11536 else if (i == RID_DFLOAT32)
11537 specs->typespec_word = cts_dfloat32;
11538 else if (i == RID_DFLOAT64)
11539 specs->typespec_word = cts_dfloat64;
11540 else
11541 specs->typespec_word = cts_dfloat128;
11542 specs->locations[cdw_typespec] = loc;
11544 if (!targetm.decimal_float_supported_p ())
11545 error_at (loc,
11546 ("decimal floating-point not supported "
11547 "for this target"));
11548 pedwarn_c11 (loc, OPT_Wpedantic,
11549 "ISO C does not support decimal floating-point "
11550 "before C2X");
11551 return specs;
11552 case RID_FRACT:
11553 case RID_ACCUM:
11555 const char *str;
11556 if (i == RID_FRACT)
11557 str = "_Fract";
11558 else
11559 str = "_Accum";
11560 if (specs->complex_p)
11561 error_at (loc,
11562 ("both %<complex%> and %qs in "
11563 "declaration specifiers"),
11564 str);
11565 else if (i == RID_FRACT)
11566 specs->typespec_word = cts_fract;
11567 else
11568 specs->typespec_word = cts_accum;
11569 specs->locations[cdw_typespec] = loc;
11571 if (!targetm.fixed_point_supported_p ())
11572 error_at (loc,
11573 "fixed-point types not supported for this target");
11574 pedwarn (loc, OPT_Wpedantic,
11575 "ISO C does not support fixed-point types");
11576 return specs;
11577 default:
11578 /* ObjC reserved word "id", handled below. */
11579 break;
11584 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11585 form of ObjC type, cases such as "int" and "long" being handled
11586 above), a TYPE (struct, union, enum and typeof specifiers) or an
11587 ERROR_MARK. In none of these cases may there have previously
11588 been any type specifiers. */
11589 if (specs->type || specs->typespec_word != cts_none
11590 || specs->long_p || specs->short_p || specs->signed_p
11591 || specs->unsigned_p || specs->complex_p)
11592 error_at (loc, "two or more data types in declaration specifiers");
11593 else if (TREE_CODE (type) == TYPE_DECL)
11595 if (TREE_TYPE (type) == error_mark_node)
11596 ; /* Allow the type to default to int to avoid cascading errors. */
11597 else
11599 specs->type = TREE_TYPE (type);
11600 specs->decl_attr = DECL_ATTRIBUTES (type);
11601 specs->typedef_p = true;
11602 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11603 specs->locations[cdw_typedef] = loc;
11605 /* If this typedef name is defined in a struct, then a C++
11606 lookup would return a different value. */
11607 if (warn_cxx_compat
11608 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11609 warning_at (loc, OPT_Wc___compat,
11610 "C++ lookup of %qD would return a field, not a type",
11611 type);
11613 /* If we are parsing a struct, record that a struct field
11614 used a typedef. */
11615 if (warn_cxx_compat && struct_parse_info != NULL)
11616 struct_parse_info->typedefs_seen.safe_push (type);
11619 else if (TREE_CODE (type) == IDENTIFIER_NODE)
11621 tree t = lookup_name (type);
11622 if (!t || TREE_CODE (t) != TYPE_DECL)
11623 error_at (loc, "%qE fails to be a typedef or built in type", type);
11624 else if (TREE_TYPE (t) == error_mark_node)
11626 else
11628 specs->type = TREE_TYPE (t);
11629 specs->locations[cdw_typespec] = loc;
11632 else
11634 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11636 specs->typedef_p = true;
11637 specs->locations[cdw_typedef] = loc;
11638 if (spec.expr)
11640 if (specs->expr)
11641 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11642 specs->expr, spec.expr);
11643 else
11644 specs->expr = spec.expr;
11645 specs->expr_const_operands &= spec.expr_const_operands;
11648 specs->type = type;
11651 return specs;
11654 /* Add the storage class specifier or function specifier SCSPEC to the
11655 declaration specifiers SPECS, returning SPECS. */
11657 struct c_declspecs *
11658 declspecs_add_scspec (location_t loc,
11659 struct c_declspecs *specs,
11660 tree scspec)
11662 enum rid i;
11663 enum c_storage_class n = csc_none;
11664 bool dupe = false;
11665 specs->declspecs_seen_p = true;
11666 specs->non_std_attrs_seen_p = true;
11667 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11668 && C_IS_RESERVED_WORD (scspec));
11669 i = C_RID_CODE (scspec);
11670 if (specs->non_sc_seen_p)
11671 warning (OPT_Wold_style_declaration,
11672 "%qE is not at beginning of declaration", scspec);
11673 switch (i)
11675 case RID_INLINE:
11676 /* C99 permits duplicate inline. Although of doubtful utility,
11677 it seems simplest to permit it in gnu89 mode as well, as
11678 there is also little utility in maintaining this as a
11679 difference between gnu89 and C99 inline. */
11680 dupe = false;
11681 specs->inline_p = true;
11682 specs->locations[cdw_inline] = loc;
11683 break;
11684 case RID_NORETURN:
11685 /* Duplicate _Noreturn is permitted. */
11686 dupe = false;
11687 specs->noreturn_p = true;
11688 specs->locations[cdw_noreturn] = loc;
11689 break;
11690 case RID_THREAD:
11691 dupe = specs->thread_p;
11692 if (specs->storage_class == csc_auto)
11693 error ("%qE used with %<auto%>", scspec);
11694 else if (specs->storage_class == csc_register)
11695 error ("%qE used with %<register%>", scspec);
11696 else if (specs->storage_class == csc_typedef)
11697 error ("%qE used with %<typedef%>", scspec);
11698 else
11700 specs->thread_p = true;
11701 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11702 "__thread") == 0);
11703 /* A diagnostic is not required for the use of this
11704 identifier in the implementation namespace; only diagnose
11705 it for the C11 spelling because of existing code using
11706 the other spelling. */
11707 if (!specs->thread_gnu_p)
11709 if (flag_isoc99)
11710 pedwarn_c99 (loc, OPT_Wpedantic,
11711 "ISO C99 does not support %qE", scspec);
11712 else
11713 pedwarn_c99 (loc, OPT_Wpedantic,
11714 "ISO C90 does not support %qE", scspec);
11716 specs->locations[cdw_thread] = loc;
11718 break;
11719 case RID_AUTO:
11720 n = csc_auto;
11721 break;
11722 case RID_EXTERN:
11723 n = csc_extern;
11724 /* Diagnose "__thread extern". */
11725 if (specs->thread_p && specs->thread_gnu_p)
11726 error ("%<__thread%> before %<extern%>");
11727 break;
11728 case RID_REGISTER:
11729 n = csc_register;
11730 break;
11731 case RID_STATIC:
11732 n = csc_static;
11733 /* Diagnose "__thread static". */
11734 if (specs->thread_p && specs->thread_gnu_p)
11735 error ("%<__thread%> before %<static%>");
11736 break;
11737 case RID_TYPEDEF:
11738 n = csc_typedef;
11739 break;
11740 default:
11741 gcc_unreachable ();
11743 if (n != csc_none && n == specs->storage_class)
11744 dupe = true;
11745 if (dupe)
11747 if (i == RID_THREAD)
11748 error ("duplicate %<_Thread_local%> or %<__thread%>");
11749 else
11750 error ("duplicate %qE", scspec);
11752 if (n != csc_none)
11754 if (specs->storage_class != csc_none && n != specs->storage_class)
11756 error ("multiple storage classes in declaration specifiers");
11758 else
11760 specs->storage_class = n;
11761 specs->locations[cdw_storage_class] = loc;
11762 if (n != csc_extern && n != csc_static && specs->thread_p)
11764 error ("%qs used with %qE",
11765 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11766 scspec);
11767 specs->thread_p = false;
11771 return specs;
11774 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11775 returning SPECS. */
11777 struct c_declspecs *
11778 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11780 specs->attrs = chainon (attrs, specs->attrs);
11781 specs->locations[cdw_attributes] = loc;
11782 specs->declspecs_seen_p = true;
11783 /* In the case of standard attributes at the start of the
11784 declaration, the caller will reset this. */
11785 specs->non_std_attrs_seen_p = true;
11786 return specs;
11789 /* Add an _Alignas specifier (expression ALIGN, or type whose
11790 alignment is ALIGN) to the declaration specifiers SPECS, returning
11791 SPECS. */
11792 struct c_declspecs *
11793 declspecs_add_alignas (location_t loc,
11794 struct c_declspecs *specs, tree align)
11796 specs->alignas_p = true;
11797 specs->locations[cdw_alignas] = loc;
11798 if (align == error_mark_node)
11799 return specs;
11801 /* Only accept the alignment if it's valid and greater than
11802 the current one. Zero is invalid but by C11 required to
11803 be silently ignored. */
11804 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11805 if (align_log > specs->align_log)
11806 specs->align_log = align_log;
11807 return specs;
11810 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11811 specifiers with any other type specifier to determine the resulting
11812 type. This is where ISO C checks on complex types are made, since
11813 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11814 double". Also apply postfix standard attributes to modify the type. */
11816 struct c_declspecs *
11817 finish_declspecs (struct c_declspecs *specs)
11819 /* If a type was specified as a whole, we have no modifiers and are
11820 done. */
11821 if (specs->type != NULL_TREE)
11823 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11824 && !specs->signed_p && !specs->unsigned_p
11825 && !specs->complex_p);
11827 /* Set a dummy type. */
11828 if (TREE_CODE (specs->type) == ERROR_MARK)
11829 specs->type = integer_type_node;
11830 goto handle_postfix_attrs;
11833 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11834 has been specified, treat it as "int" unless "_Complex" is
11835 present and there are no other specifiers. If we just have
11836 "_Complex", it is equivalent to "_Complex double", but e.g.
11837 "_Complex short" is equivalent to "_Complex short int". */
11838 if (specs->typespec_word == cts_none)
11840 if (specs->saturating_p)
11842 error_at (specs->locations[cdw_saturating],
11843 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11844 if (!targetm.fixed_point_supported_p ())
11845 error_at (specs->locations[cdw_saturating],
11846 "fixed-point types not supported for this target");
11847 specs->typespec_word = cts_fract;
11849 else if (specs->long_p || specs->short_p
11850 || specs->signed_p || specs->unsigned_p)
11852 specs->typespec_word = cts_int;
11854 else if (specs->complex_p)
11856 specs->typespec_word = cts_double;
11857 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11858 "ISO C does not support plain %<complex%> meaning "
11859 "%<double complex%>");
11861 else
11863 specs->typespec_word = cts_int;
11864 specs->default_int_p = true;
11865 /* We don't diagnose this here because grokdeclarator will
11866 give more specific diagnostics according to whether it is
11867 a function definition. */
11871 /* If "signed" was specified, record this to distinguish "int" and
11872 "signed int" in the case of a bit-field with
11873 -funsigned-bitfields. */
11874 specs->explicit_signed_p = specs->signed_p;
11876 /* Now compute the actual type. */
11877 switch (specs->typespec_word)
11879 case cts_auto_type:
11880 gcc_assert (!specs->long_p && !specs->short_p
11881 && !specs->signed_p && !specs->unsigned_p
11882 && !specs->complex_p);
11883 /* Type to be filled in later. */
11884 if (specs->postfix_attrs)
11885 error ("%<__auto_type%> followed by %<[[]]%> attributes");
11886 break;
11887 case cts_void:
11888 gcc_assert (!specs->long_p && !specs->short_p
11889 && !specs->signed_p && !specs->unsigned_p
11890 && !specs->complex_p);
11891 specs->type = void_type_node;
11892 break;
11893 case cts_bool:
11894 gcc_assert (!specs->long_p && !specs->short_p
11895 && !specs->signed_p && !specs->unsigned_p
11896 && !specs->complex_p);
11897 specs->type = boolean_type_node;
11898 break;
11899 case cts_char:
11900 gcc_assert (!specs->long_p && !specs->short_p);
11901 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11902 if (specs->signed_p)
11903 specs->type = signed_char_type_node;
11904 else if (specs->unsigned_p)
11905 specs->type = unsigned_char_type_node;
11906 else
11907 specs->type = char_type_node;
11908 if (specs->complex_p)
11910 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11911 "ISO C does not support complex integer types");
11912 specs->type = build_complex_type (specs->type);
11914 break;
11915 case cts_int_n:
11916 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11917 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11918 if (! int_n_enabled_p[specs->int_n_idx])
11919 specs->type = integer_type_node;
11920 else
11921 specs->type = (specs->unsigned_p
11922 ? int_n_trees[specs->int_n_idx].unsigned_type
11923 : int_n_trees[specs->int_n_idx].signed_type);
11924 if (specs->complex_p)
11926 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11927 "ISO C does not support complex integer types");
11928 specs->type = build_complex_type (specs->type);
11930 break;
11931 case cts_int:
11932 gcc_assert (!(specs->long_p && specs->short_p));
11933 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11934 if (specs->long_long_p)
11935 specs->type = (specs->unsigned_p
11936 ? long_long_unsigned_type_node
11937 : long_long_integer_type_node);
11938 else if (specs->long_p)
11939 specs->type = (specs->unsigned_p
11940 ? long_unsigned_type_node
11941 : long_integer_type_node);
11942 else if (specs->short_p)
11943 specs->type = (specs->unsigned_p
11944 ? short_unsigned_type_node
11945 : short_integer_type_node);
11946 else
11947 specs->type = (specs->unsigned_p
11948 ? unsigned_type_node
11949 : integer_type_node);
11950 if (specs->complex_p)
11952 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11953 "ISO C does not support complex integer types");
11954 specs->type = build_complex_type (specs->type);
11956 break;
11957 case cts_float:
11958 gcc_assert (!specs->long_p && !specs->short_p
11959 && !specs->signed_p && !specs->unsigned_p);
11960 specs->type = (specs->complex_p
11961 ? complex_float_type_node
11962 : float_type_node);
11963 break;
11964 case cts_double:
11965 gcc_assert (!specs->long_long_p && !specs->short_p
11966 && !specs->signed_p && !specs->unsigned_p);
11967 if (specs->long_p)
11969 specs->type = (specs->complex_p
11970 ? complex_long_double_type_node
11971 : long_double_type_node);
11973 else
11975 specs->type = (specs->complex_p
11976 ? complex_double_type_node
11977 : double_type_node);
11979 break;
11980 case cts_floatn_nx:
11981 gcc_assert (!specs->long_p && !specs->short_p
11982 && !specs->signed_p && !specs->unsigned_p);
11983 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11984 specs->type = integer_type_node;
11985 else if (specs->complex_p)
11986 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11987 else
11988 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11989 break;
11990 case cts_dfloat32:
11991 case cts_dfloat64:
11992 case cts_dfloat128:
11993 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11994 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11995 if (!targetm.decimal_float_supported_p ())
11996 specs->type = integer_type_node;
11997 else if (specs->typespec_word == cts_dfloat32)
11998 specs->type = dfloat32_type_node;
11999 else if (specs->typespec_word == cts_dfloat64)
12000 specs->type = dfloat64_type_node;
12001 else
12002 specs->type = dfloat128_type_node;
12003 break;
12004 case cts_fract:
12005 gcc_assert (!specs->complex_p);
12006 if (!targetm.fixed_point_supported_p ())
12007 specs->type = integer_type_node;
12008 else if (specs->saturating_p)
12010 if (specs->long_long_p)
12011 specs->type = specs->unsigned_p
12012 ? sat_unsigned_long_long_fract_type_node
12013 : sat_long_long_fract_type_node;
12014 else if (specs->long_p)
12015 specs->type = specs->unsigned_p
12016 ? sat_unsigned_long_fract_type_node
12017 : sat_long_fract_type_node;
12018 else if (specs->short_p)
12019 specs->type = specs->unsigned_p
12020 ? sat_unsigned_short_fract_type_node
12021 : sat_short_fract_type_node;
12022 else
12023 specs->type = specs->unsigned_p
12024 ? sat_unsigned_fract_type_node
12025 : sat_fract_type_node;
12027 else
12029 if (specs->long_long_p)
12030 specs->type = specs->unsigned_p
12031 ? unsigned_long_long_fract_type_node
12032 : long_long_fract_type_node;
12033 else if (specs->long_p)
12034 specs->type = specs->unsigned_p
12035 ? unsigned_long_fract_type_node
12036 : long_fract_type_node;
12037 else if (specs->short_p)
12038 specs->type = specs->unsigned_p
12039 ? unsigned_short_fract_type_node
12040 : short_fract_type_node;
12041 else
12042 specs->type = specs->unsigned_p
12043 ? unsigned_fract_type_node
12044 : fract_type_node;
12046 break;
12047 case cts_accum:
12048 gcc_assert (!specs->complex_p);
12049 if (!targetm.fixed_point_supported_p ())
12050 specs->type = integer_type_node;
12051 else if (specs->saturating_p)
12053 if (specs->long_long_p)
12054 specs->type = specs->unsigned_p
12055 ? sat_unsigned_long_long_accum_type_node
12056 : sat_long_long_accum_type_node;
12057 else if (specs->long_p)
12058 specs->type = specs->unsigned_p
12059 ? sat_unsigned_long_accum_type_node
12060 : sat_long_accum_type_node;
12061 else if (specs->short_p)
12062 specs->type = specs->unsigned_p
12063 ? sat_unsigned_short_accum_type_node
12064 : sat_short_accum_type_node;
12065 else
12066 specs->type = specs->unsigned_p
12067 ? sat_unsigned_accum_type_node
12068 : sat_accum_type_node;
12070 else
12072 if (specs->long_long_p)
12073 specs->type = specs->unsigned_p
12074 ? unsigned_long_long_accum_type_node
12075 : long_long_accum_type_node;
12076 else if (specs->long_p)
12077 specs->type = specs->unsigned_p
12078 ? unsigned_long_accum_type_node
12079 : long_accum_type_node;
12080 else if (specs->short_p)
12081 specs->type = specs->unsigned_p
12082 ? unsigned_short_accum_type_node
12083 : short_accum_type_node;
12084 else
12085 specs->type = specs->unsigned_p
12086 ? unsigned_accum_type_node
12087 : accum_type_node;
12089 break;
12090 default:
12091 gcc_unreachable ();
12093 handle_postfix_attrs:
12094 if (specs->type != NULL)
12096 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12097 decl_attributes (&specs->type, specs->postfix_attrs, 0);
12098 specs->postfix_attrs = NULL_TREE;
12101 return specs;
12104 /* Perform final processing on one file scope's declarations (or the
12105 external scope's declarations), GLOBALS. */
12107 static void
12108 c_write_global_declarations_1 (tree globals)
12110 tree decl;
12111 bool reconsider;
12113 /* Process the decls in the order they were written. */
12114 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12116 /* Check for used but undefined static functions using the C
12117 standard's definition of "used", and set TREE_NO_WARNING so
12118 that check_global_declaration doesn't repeat the check. */
12119 if (TREE_CODE (decl) == FUNCTION_DECL
12120 && DECL_INITIAL (decl) == NULL_TREE
12121 && DECL_EXTERNAL (decl)
12122 && !TREE_PUBLIC (decl))
12124 if (C_DECL_USED (decl))
12126 if (pedwarn (input_location, 0, "%q+F used but never defined",
12127 decl))
12128 TREE_NO_WARNING (decl) = 1;
12130 /* For -Wunused-function warn about unused static prototypes. */
12131 else if (warn_unused_function
12132 && ! DECL_ARTIFICIAL (decl)
12133 && ! TREE_NO_WARNING (decl))
12135 if (warning (OPT_Wunused_function,
12136 "%q+F declared %<static%> but never defined",
12137 decl))
12138 TREE_NO_WARNING (decl) = 1;
12142 wrapup_global_declaration_1 (decl);
12147 reconsider = false;
12148 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12149 reconsider |= wrapup_global_declaration_2 (decl);
12151 while (reconsider);
12154 /* Preserve the external declarations scope across a garbage collect. */
12155 static GTY(()) tree ext_block;
12157 /* Collect all references relevant to SOURCE_FILE. */
12159 static void
12160 collect_all_refs (const char *source_file)
12162 tree t;
12163 unsigned i;
12165 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12166 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12168 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12171 /* Collect source file references at global level. */
12173 static void
12174 collect_source_refs (void)
12176 tree t;
12177 tree decls;
12178 tree decl;
12179 unsigned i;
12181 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12183 decls = DECL_INITIAL (t);
12184 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12185 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12186 collect_source_ref (DECL_SOURCE_FILE (decl));
12189 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12190 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12191 collect_source_ref (DECL_SOURCE_FILE (decl));
12194 /* Free attribute access data that are not needed by the middle end. */
12196 static void
12197 free_attr_access_data ()
12199 struct cgraph_node *n;
12201 /* Iterate over all functions declared in the translation unit. */
12202 FOR_EACH_FUNCTION (n)
12204 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12205 if (tree attrs = DECL_ATTRIBUTES (parm))
12206 attr_access::free_lang_data (attrs);
12208 tree fntype = TREE_TYPE (n->decl);
12209 if (!fntype)
12210 continue;
12211 tree attrs = TYPE_ATTRIBUTES (fntype);
12212 if (!attrs)
12213 continue;
12215 attr_access::free_lang_data (attrs);
12219 /* Perform any final parser cleanups and generate initial debugging
12220 information. */
12222 void
12223 c_parse_final_cleanups (void)
12225 tree t;
12226 unsigned i;
12228 /* We don't want to do this if generating a PCH. */
12229 if (pch_file)
12230 return;
12232 timevar_stop (TV_PHASE_PARSING);
12233 timevar_start (TV_PHASE_DEFERRED);
12235 /* Do the Objective-C stuff. This is where all the Objective-C
12236 module stuff gets generated (symtab, class/protocol/selector
12237 lists etc). */
12238 if (c_dialect_objc ())
12239 objc_write_global_declarations ();
12241 /* Close the external scope. */
12242 ext_block = pop_scope ();
12243 external_scope = 0;
12244 gcc_assert (!current_scope);
12246 /* Handle -fdump-ada-spec[-slim]. */
12247 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12249 /* Build a table of files to generate specs for */
12250 collect_source_ref (main_input_filename);
12251 if (!flag_dump_ada_spec_slim)
12252 collect_source_refs ();
12254 dump_ada_specs (collect_all_refs, NULL);
12257 /* Process all file scopes in this compilation, and the external_scope,
12258 through wrapup_global_declarations. */
12259 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12260 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12261 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12263 if (!in_lto_p)
12264 free_attr_access_data ();
12266 timevar_stop (TV_PHASE_DEFERRED);
12267 timevar_start (TV_PHASE_PARSING);
12269 ext_block = NULL;
12272 /* Register reserved keyword WORD as qualifier for address space AS. */
12274 void
12275 c_register_addr_space (const char *word, addr_space_t as)
12277 int rid = RID_FIRST_ADDR_SPACE + as;
12278 tree id;
12280 /* Address space qualifiers are only supported
12281 in C with GNU extensions enabled. */
12282 if (c_dialect_objc () || flag_no_asm)
12283 return;
12285 id = get_identifier (word);
12286 C_SET_RID_CODE (id, rid);
12287 C_IS_RESERVED_WORD (id) = 1;
12288 ridpointers [rid] = id;
12291 /* Return identifier to look up for omp declare reduction. */
12293 tree
12294 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
12296 const char *p = NULL;
12297 switch (reduction_code)
12299 case PLUS_EXPR: p = "+"; break;
12300 case MULT_EXPR: p = "*"; break;
12301 case MINUS_EXPR: p = "-"; break;
12302 case BIT_AND_EXPR: p = "&"; break;
12303 case BIT_XOR_EXPR: p = "^"; break;
12304 case BIT_IOR_EXPR: p = "|"; break;
12305 case TRUTH_ANDIF_EXPR: p = "&&"; break;
12306 case TRUTH_ORIF_EXPR: p = "||"; break;
12307 case MIN_EXPR: p = "min"; break;
12308 case MAX_EXPR: p = "max"; break;
12309 default:
12310 break;
12313 if (p == NULL)
12315 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
12316 return error_mark_node;
12317 p = IDENTIFIER_POINTER (reduction_id);
12320 const char prefix[] = "omp declare reduction ";
12321 size_t lenp = sizeof (prefix);
12322 size_t len = strlen (p);
12323 char *name = XALLOCAVEC (char, lenp + len);
12324 memcpy (name, prefix, lenp - 1);
12325 memcpy (name + lenp - 1, p, len + 1);
12326 return get_identifier (name);
12329 /* Lookup REDUCTION_ID in the current scope, or create an artificial
12330 VAR_DECL, bind it into the current scope and return it. */
12332 tree
12333 c_omp_reduction_decl (tree reduction_id)
12335 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12336 if (b != NULL && B_IN_CURRENT_SCOPE (b))
12337 return b->decl;
12339 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
12340 reduction_id, integer_type_node);
12341 DECL_ARTIFICIAL (decl) = 1;
12342 DECL_EXTERNAL (decl) = 1;
12343 TREE_STATIC (decl) = 1;
12344 TREE_PUBLIC (decl) = 0;
12345 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
12346 return decl;
12349 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
12351 tree
12352 c_omp_reduction_lookup (tree reduction_id, tree type)
12354 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12355 while (b)
12357 tree t;
12358 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
12359 if (comptypes (TREE_PURPOSE (t), type))
12360 return TREE_VALUE (t);
12361 b = b->shadowed;
12363 return error_mark_node;
12366 /* Helper function called via walk_tree, to diagnose invalid
12367 #pragma omp declare reduction combiners or initializers. */
12369 tree
12370 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
12372 tree *vars = (tree *) data;
12373 if (SSA_VAR_P (*tp)
12374 && !DECL_ARTIFICIAL (*tp)
12375 && *tp != vars[0]
12376 && *tp != vars[1])
12378 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
12379 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
12380 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
12381 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
12382 *tp);
12383 else
12384 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
12385 "to variable %qD which is not %<omp_priv%> nor "
12386 "%<omp_orig%>",
12387 *tp);
12388 return *tp;
12390 return NULL_TREE;
12394 bool
12395 c_check_in_current_scope (tree decl)
12397 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
12398 return b != NULL && B_IN_CURRENT_SCOPE (b);
12401 #include "gt-c-c-decl.h"