Fix diagnostics for never-defined inline and nested functions (PR c/88720, PR c/88726).
[official-gcc.git] / gcc / c / c-decl.c
blob5c7232f3657a6edf99de753d9e1de22ec68f3ff4
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #define INCLUDE_UNIQUE_PTR
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
62 enum decl_context
63 { NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
69 /* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
72 deprecated items. */
74 enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
91 static location_t current_function_prototype_locus;
93 /* Whether this prototype was built-in. */
95 static bool current_function_prototype_built_in;
97 /* The argument type information of this prototype. */
99 static tree current_function_prototype_arg_types;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* A list of decls to be made automatically visible in each file scope. */
120 static GTY(()) tree visible_builtins;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
145 static bool undef_nested_function;
147 /* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149 int current_omp_declare_target_attribute;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 /* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
212 struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The binding oracle; see c-tree.h. */
224 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
226 /* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228 #define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
231 static inline struct c_binding* *
232 i_symbol_binding (tree node)
234 struct lang_identifier *lid
235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
237 if (lid->symbol_binding == NULL
238 && c_binding_oracle != NULL
239 && !I_SYMBOL_CHECKED (node))
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL, node);
247 return &lid->symbol_binding;
250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
252 #define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
255 /* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257 #define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
260 static inline struct c_binding **
261 i_tag_binding (tree node)
263 struct lang_identifier *lid
264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
266 if (lid->tag_binding == NULL
267 && c_binding_oracle != NULL
268 && !I_TAG_CHECKED (node))
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node) = 1;
273 c_binding_oracle (C_ORACLE_TAG, node);
276 return &lid->tag_binding;
279 #define I_TAG_BINDING(node) (*i_tag_binding (node))
281 #define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
284 /* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286 #define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
289 static inline struct c_binding **
290 i_label_binding (tree node)
292 struct lang_identifier *lid
293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
295 if (lid->label_binding == NULL
296 && c_binding_oracle != NULL
297 && !I_LABEL_CHECKED (node))
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node) = 1;
302 c_binding_oracle (C_ORACLE_LABEL, node);
305 return &lid->label_binding;
308 #define I_LABEL_BINDING(node) (*i_label_binding (node))
310 #define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
313 /* The resulting tree type. */
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
318 union tree_node GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
320 generic;
321 struct lang_identifier GTY ((tag ("1"))) identifier;
324 /* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
330 struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
350 /* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
354 struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
361 typedef struct c_goto_bindings *c_goto_bindings_p;
363 /* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
366 struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
383 /* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
388 Most declarations are recorded in the current scope.
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
409 undefined.
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
416 this scope.
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
422 list here.)
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
428 struct GTY((chain_next ("%h.outer"))) c_scope {
429 /* The scope containing this one. */
430 struct c_scope *outer;
432 /* The next outermost function scope. */
433 struct c_scope *outer_function;
435 /* All bindings in this scope. */
436 struct c_binding *bindings;
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
440 tree blocks;
441 tree blocks_last;
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth : 28;
447 /* True if we are currently filling this scope with parameter
448 declarations. */
449 BOOL_BITFIELD parm_flag : 1;
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec : 1;
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls : 1;
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body : 1;
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep : 1;
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64 : 1;
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings : 1;
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl : 1;
485 /* The scope currently in effect. */
487 static GTY(()) struct c_scope *current_scope;
489 /* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
493 static GTY(()) struct c_scope *current_function_scope;
495 /* The C file scope. This is reset for each input translation unit. */
497 static GTY(()) struct c_scope *file_scope;
499 /* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
502 static GTY(()) struct c_scope *external_scope;
504 /* A chain of c_scope structures awaiting reuse. */
506 static GTY((deletable)) struct c_scope *scope_freelist;
508 /* A chain of c_binding structures awaiting reuse. */
510 static GTY((deletable)) struct c_binding *binding_freelist;
512 /* Append VAR to LIST in scope SCOPE. */
513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
515 tree d_ = (decl); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
518 else \
519 s_->list = d_; \
520 s_->list##_last = d_; \
521 } while (0)
523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
527 if (t_->to##_last) \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
529 else \
530 t_->to = f_->from; \
531 t_->to##_last = f_->from##_last; \
532 } while (0)
534 /* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
539 struct GTY((chain_next ("%h.next"))) c_inline_static {
540 /* The location for a diagnostic. */
541 location_t location;
543 /* The function that may be an inline definition. */
544 tree function;
546 /* The object or function referenced. */
547 tree static_decl;
549 /* What sort of reference this is. */
550 enum c_inline_static_type type;
552 /* The next such structure or NULL. */
553 struct c_inline_static *next;
556 /* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558 static GTY(()) struct c_inline_static *c_inline_statics;
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
562 static bool keep_next_level_flag;
564 /* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
568 static bool next_is_function_body;
570 /* A vector of pointers to c_binding structures. */
572 typedef struct c_binding *c_binding_ptr;
574 /* Information that we keep for a struct or union while it is being
575 parsed. */
577 struct c_struct_parse_info
579 /* If warn_cxx_compat, a list of types defined within this
580 struct. */
581 auto_vec<tree> struct_types;
582 /* If warn_cxx_compat, a list of field names which have bindings,
583 and which are defined in this struct, but which are not defined
584 in any enclosing struct. This is used to clear the in_struct
585 field of the c_bindings structure. */
586 auto_vec<c_binding_ptr> fields;
587 /* If warn_cxx_compat, a list of typedef names used when defining
588 fields in this struct. */
589 auto_vec<tree> typedefs_seen;
592 /* Information for the struct or union currently being parsed, or
593 NULL if not parsing a struct or union. */
594 static struct c_struct_parse_info *struct_parse_info;
596 /* Forward declarations. */
597 static tree lookup_name_in_scope (tree, struct c_scope *);
598 static tree c_make_fname_decl (location_t, tree, int);
599 static tree grokdeclarator (const struct c_declarator *,
600 struct c_declspecs *,
601 enum decl_context, bool, tree *, tree *, tree *,
602 bool *, enum deprecated_states);
603 static tree grokparms (struct c_arg_info *, bool);
604 static void layout_array_type (tree);
605 static void warn_defaults_to (location_t, int, const char *, ...)
606 ATTRIBUTE_GCC_DIAG(3,4);
607 static const char *header_for_builtin_fn (enum built_in_function);
609 /* T is a statement. Add it to the statement-tree. This is the
610 C/ObjC version--C++ has a slightly different version of this
611 function. */
613 tree
614 add_stmt (tree t)
616 enum tree_code code = TREE_CODE (t);
618 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
620 if (!EXPR_HAS_LOCATION (t))
621 SET_EXPR_LOCATION (t, input_location);
624 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
625 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
627 /* Add T to the statement-tree. Non-side-effect statements need to be
628 recorded during statement expressions. */
629 if (!building_stmt_list_p ())
630 push_stmt_list ();
631 append_to_statement_list_force (t, &cur_stmt_list);
633 return t;
636 /* Build a pointer type using the default pointer mode. */
638 static tree
639 c_build_pointer_type (tree to_type)
641 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
642 : TYPE_ADDR_SPACE (to_type);
643 machine_mode pointer_mode;
645 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
646 pointer_mode = targetm.addr_space.pointer_mode (as);
647 else
648 pointer_mode = c_default_pointer_mode;
649 return build_pointer_type_for_mode (to_type, pointer_mode, false);
653 /* Return true if we will want to say something if a goto statement
654 crosses DECL. */
656 static bool
657 decl_jump_unsafe (tree decl)
659 if (error_operand_p (decl))
660 return false;
662 /* Always warn about crossing variably modified types. */
663 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
664 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
665 return true;
667 /* Otherwise, only warn if -Wgoto-misses-init and this is an
668 initialized automatic decl. */
669 if (warn_jump_misses_init
670 && VAR_P (decl)
671 && !TREE_STATIC (decl)
672 && DECL_INITIAL (decl) != NULL_TREE)
673 return true;
675 return false;
679 void
680 c_print_identifier (FILE *file, tree node, int indent)
682 void (*save) (enum c_oracle_request, tree identifier);
684 /* Temporarily hide any binding oracle. Without this, calls to
685 debug_tree from the debugger will end up calling into the oracle,
686 making for a confusing debug session. As the oracle isn't needed
687 here for normal operation, it's simplest to suppress it. */
688 save = c_binding_oracle;
689 c_binding_oracle = NULL;
691 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
692 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
693 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
694 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
696 tree rid = ridpointers[C_RID_CODE (node)];
697 indent_to (file, indent + 4);
698 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
699 (void *) rid, IDENTIFIER_POINTER (rid));
702 c_binding_oracle = save;
705 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
706 which may be any of several kinds of DECL or TYPE or error_mark_node,
707 in the scope SCOPE. */
708 static void
709 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
710 bool nested, location_t locus)
712 struct c_binding *b, **here;
714 if (binding_freelist)
716 b = binding_freelist;
717 binding_freelist = b->prev;
719 else
720 b = ggc_alloc<c_binding> ();
722 b->shadowed = 0;
723 b->decl = decl;
724 b->id = name;
725 b->depth = scope->depth;
726 b->invisible = invisible;
727 b->nested = nested;
728 b->inner_comp = 0;
729 b->in_struct = 0;
730 b->locus = locus;
732 b->u.type = NULL;
734 b->prev = scope->bindings;
735 scope->bindings = b;
737 if (decl_jump_unsafe (decl))
738 scope->has_jump_unsafe_decl = 1;
740 if (!name)
741 return;
743 switch (TREE_CODE (decl))
745 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
746 case ENUMERAL_TYPE:
747 case UNION_TYPE:
748 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
749 case VAR_DECL:
750 case FUNCTION_DECL:
751 case TYPE_DECL:
752 case CONST_DECL:
753 case PARM_DECL:
754 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
756 default:
757 gcc_unreachable ();
760 /* Locate the appropriate place in the chain of shadowed decls
761 to insert this binding. Normally, scope == current_scope and
762 this does nothing. */
763 while (*here && (*here)->depth > scope->depth)
764 here = &(*here)->shadowed;
766 b->shadowed = *here;
767 *here = b;
770 /* Clear the binding structure B, stick it on the binding_freelist,
771 and return the former value of b->prev. This is used by pop_scope
772 and get_parm_info to iterate destructively over all the bindings
773 from a given scope. */
774 static struct c_binding *
775 free_binding_and_advance (struct c_binding *b)
777 struct c_binding *prev = b->prev;
779 memset (b, 0, sizeof (struct c_binding));
780 b->prev = binding_freelist;
781 binding_freelist = b;
783 return prev;
786 /* Bind a label. Like bind, but skip fields which aren't used for
787 labels, and add the LABEL_VARS value. */
788 static void
789 bind_label (tree name, tree label, struct c_scope *scope,
790 struct c_label_vars *label_vars)
792 struct c_binding *b;
794 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
795 UNKNOWN_LOCATION);
797 scope->has_label_bindings = true;
799 b = scope->bindings;
800 gcc_assert (b->decl == label);
801 label_vars->shadowed = b->u.label;
802 b->u.label = label_vars;
805 /* Hook called at end of compilation to assume 1 elt
806 for a file-scope tentative array defn that wasn't complete before. */
808 void
809 c_finish_incomplete_decl (tree decl)
811 if (VAR_P (decl))
813 tree type = TREE_TYPE (decl);
814 if (type != error_mark_node
815 && TREE_CODE (type) == ARRAY_TYPE
816 && !DECL_EXTERNAL (decl)
817 && TYPE_DOMAIN (type) == NULL_TREE)
819 warning_at (DECL_SOURCE_LOCATION (decl),
820 0, "array %q+D assumed to have one element", decl);
822 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
824 relayout_decl (decl);
829 /* Record that inline function FUNC contains a reference (location
830 LOC) to static DECL (file-scope or function-local according to
831 TYPE). */
833 void
834 record_inline_static (location_t loc, tree func, tree decl,
835 enum c_inline_static_type type)
837 c_inline_static *csi = ggc_alloc<c_inline_static> ();
838 csi->location = loc;
839 csi->function = func;
840 csi->static_decl = decl;
841 csi->type = type;
842 csi->next = c_inline_statics;
843 c_inline_statics = csi;
846 /* Check for references to static declarations in inline functions at
847 the end of the translation unit and diagnose them if the functions
848 are still inline definitions. */
850 static void
851 check_inline_statics (void)
853 struct c_inline_static *csi;
854 for (csi = c_inline_statics; csi; csi = csi->next)
856 if (DECL_EXTERNAL (csi->function))
857 switch (csi->type)
859 case csi_internal:
860 pedwarn (csi->location, 0,
861 "%qD is static but used in inline function %qD "
862 "which is not static", csi->static_decl, csi->function);
863 break;
864 case csi_modifiable:
865 pedwarn (csi->location, 0,
866 "%q+D is static but declared in inline function %qD "
867 "which is not static", csi->static_decl, csi->function);
868 break;
869 default:
870 gcc_unreachable ();
873 c_inline_statics = NULL;
876 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
877 for the current state, otherwise set it to uninitialized. */
879 static void
880 set_spot_bindings (struct c_spot_bindings *p, bool defining)
882 if (defining)
884 p->scope = current_scope;
885 p->bindings_in_scope = current_scope->bindings;
887 else
889 p->scope = NULL;
890 p->bindings_in_scope = NULL;
892 p->stmt_exprs = 0;
893 p->left_stmt_expr = false;
896 /* Update spot bindings P as we pop out of SCOPE. Return true if we
897 should push decls for a label. */
899 static bool
900 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
902 if (p->scope != scope)
904 /* This label or goto is defined in some other scope, or it is a
905 label which is not yet defined. There is nothing to
906 update. */
907 return false;
910 /* Adjust the spot bindings to refer to the bindings already defined
911 in the enclosing scope. */
912 p->scope = scope->outer;
913 p->bindings_in_scope = p->scope->bindings;
915 return true;
918 /* The Objective-C front-end often needs to determine the current scope. */
920 void *
921 objc_get_current_scope (void)
923 return current_scope;
926 /* The following function is used only by Objective-C. It needs to live here
927 because it accesses the innards of c_scope. */
929 void
930 objc_mark_locals_volatile (void *enclosing_blk)
932 struct c_scope *scope;
933 struct c_binding *b;
935 for (scope = current_scope;
936 scope && scope != enclosing_blk;
937 scope = scope->outer)
939 for (b = scope->bindings; b; b = b->prev)
940 objc_volatilize_decl (b->decl);
942 /* Do not climb up past the current function. */
943 if (scope->function_body)
944 break;
948 /* Return true if we are in the global binding level. */
950 bool
951 global_bindings_p (void)
953 return current_scope == file_scope;
956 /* Return true if we're declaring parameters in an old-style function
957 declaration. */
959 bool
960 old_style_parameter_scope (void)
962 /* If processing parameters and there is no function statement list, we
963 * have an old-style function declaration. */
964 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
967 void
968 keep_next_level (void)
970 keep_next_level_flag = true;
973 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
975 void
976 set_float_const_decimal64 (void)
978 current_scope->float_const_decimal64 = true;
981 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
983 void
984 clear_float_const_decimal64 (void)
986 current_scope->float_const_decimal64 = false;
989 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
991 bool
992 float_const_decimal64_p (void)
994 return current_scope->float_const_decimal64;
997 /* Identify this scope as currently being filled with parameters. */
999 void
1000 declare_parm_level (void)
1002 current_scope->parm_flag = true;
1005 void
1006 push_scope (void)
1008 if (next_is_function_body)
1010 /* This is the transition from the parameters to the top level
1011 of the function body. These are the same scope
1012 (C99 6.2.1p4,6) so we do not push another scope structure.
1013 next_is_function_body is set only by store_parm_decls, which
1014 in turn is called when and only when we are about to
1015 encounter the opening curly brace for the function body.
1017 The outermost block of a function always gets a BLOCK node,
1018 because the debugging output routines expect that each
1019 function has at least one BLOCK. */
1020 current_scope->parm_flag = false;
1021 current_scope->function_body = true;
1022 current_scope->keep = true;
1023 current_scope->outer_function = current_function_scope;
1024 current_function_scope = current_scope;
1026 keep_next_level_flag = false;
1027 next_is_function_body = false;
1029 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1030 if (current_scope->outer)
1031 current_scope->float_const_decimal64
1032 = current_scope->outer->float_const_decimal64;
1033 else
1034 current_scope->float_const_decimal64 = false;
1036 else
1038 struct c_scope *scope;
1039 if (scope_freelist)
1041 scope = scope_freelist;
1042 scope_freelist = scope->outer;
1044 else
1045 scope = ggc_cleared_alloc<c_scope> ();
1047 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1048 if (current_scope)
1049 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1050 else
1051 scope->float_const_decimal64 = false;
1053 scope->keep = keep_next_level_flag;
1054 scope->outer = current_scope;
1055 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1057 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1058 possible. */
1059 if (current_scope && scope->depth == 0)
1061 scope->depth--;
1062 sorry ("GCC supports only %u nested scopes", scope->depth);
1065 current_scope = scope;
1066 keep_next_level_flag = false;
1070 /* This is called when we are leaving SCOPE. For each label defined
1071 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1072 These are the decls whose initialization will be skipped by a goto
1073 later in the function. */
1075 static void
1076 update_label_decls (struct c_scope *scope)
1078 struct c_scope *s;
1080 s = scope;
1081 while (s != NULL)
1083 if (s->has_label_bindings)
1085 struct c_binding *b;
1087 for (b = s->bindings; b != NULL; b = b->prev)
1089 struct c_label_vars *label_vars;
1090 struct c_binding *b1;
1091 bool hjud;
1092 unsigned int ix;
1093 struct c_goto_bindings *g;
1095 if (TREE_CODE (b->decl) != LABEL_DECL)
1096 continue;
1097 label_vars = b->u.label;
1099 b1 = label_vars->label_bindings.bindings_in_scope;
1100 if (label_vars->label_bindings.scope == NULL)
1101 hjud = false;
1102 else
1103 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1104 if (update_spot_bindings (scope, &label_vars->label_bindings))
1106 /* This label is defined in this scope. */
1107 if (hjud)
1109 for (; b1 != NULL; b1 = b1->prev)
1111 /* A goto from later in the function to this
1112 label will never see the initialization
1113 of B1, if any. Save it to issue a
1114 warning if needed. */
1115 if (decl_jump_unsafe (b1->decl))
1116 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1121 /* Update the bindings of any goto statements associated
1122 with this label. */
1123 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1124 update_spot_bindings (scope, &g->goto_bindings);
1128 /* Don't search beyond the current function. */
1129 if (s == current_function_scope)
1130 break;
1132 s = s->outer;
1136 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1138 static void
1139 set_type_context (tree type, tree context)
1141 for (type = TYPE_MAIN_VARIANT (type); type;
1142 type = TYPE_NEXT_VARIANT (type))
1143 TYPE_CONTEXT (type) = context;
1146 /* Exit a scope. Restore the state of the identifier-decl mappings
1147 that were in effect when this scope was entered. Return a BLOCK
1148 node containing all the DECLs in this scope that are of interest
1149 to debug info generation. */
1151 tree
1152 pop_scope (void)
1154 struct c_scope *scope = current_scope;
1155 tree block, context, p;
1156 struct c_binding *b;
1158 bool functionbody = scope->function_body;
1159 bool keep = functionbody || scope->keep || scope->bindings;
1161 update_label_decls (scope);
1163 /* If appropriate, create a BLOCK to record the decls for the life
1164 of this function. */
1165 block = NULL_TREE;
1166 if (keep)
1168 block = make_node (BLOCK);
1169 BLOCK_SUBBLOCKS (block) = scope->blocks;
1170 TREE_USED (block) = 1;
1172 /* In each subblock, record that this is its superior. */
1173 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1174 BLOCK_SUPERCONTEXT (p) = block;
1176 BLOCK_VARS (block) = NULL_TREE;
1179 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1180 scope must be set so that they point to the appropriate
1181 construct, i.e. either to the current FUNCTION_DECL node, or
1182 else to the BLOCK node we just constructed.
1184 Note that for tagged types whose scope is just the formal
1185 parameter list for some function type specification, we can't
1186 properly set their TYPE_CONTEXTs here, because we don't have a
1187 pointer to the appropriate FUNCTION_TYPE node readily available
1188 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1189 type nodes get set in `grokdeclarator' as soon as we have created
1190 the FUNCTION_TYPE node which will represent the "scope" for these
1191 "parameter list local" tagged types. */
1192 if (scope->function_body)
1193 context = current_function_decl;
1194 else if (scope == file_scope)
1196 tree file_decl
1197 = build_translation_unit_decl (get_identifier (main_input_filename));
1198 context = file_decl;
1199 debug_hooks->register_main_translation_unit (file_decl);
1201 else
1202 context = block;
1204 /* Clear all bindings in this scope. */
1205 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1207 p = b->decl;
1208 switch (TREE_CODE (p))
1210 case LABEL_DECL:
1211 /* Warnings for unused labels, errors for undefined labels. */
1212 if (TREE_USED (p) && !DECL_INITIAL (p))
1214 error ("label %q+D used but not defined", p);
1215 DECL_INITIAL (p) = error_mark_node;
1217 else
1218 warn_for_unused_label (p);
1220 /* Labels go in BLOCK_VARS. */
1221 DECL_CHAIN (p) = BLOCK_VARS (block);
1222 BLOCK_VARS (block) = p;
1223 gcc_assert (I_LABEL_BINDING (b->id) == b);
1224 I_LABEL_BINDING (b->id) = b->shadowed;
1226 /* Also pop back to the shadowed label_vars. */
1227 release_tree_vector (b->u.label->decls_in_scope);
1228 b->u.label = b->u.label->shadowed;
1229 break;
1231 case ENUMERAL_TYPE:
1232 case UNION_TYPE:
1233 case RECORD_TYPE:
1234 set_type_context (p, context);
1236 /* Types may not have tag-names, in which case the type
1237 appears in the bindings list with b->id NULL. */
1238 if (b->id)
1240 gcc_assert (I_TAG_BINDING (b->id) == b);
1241 I_TAG_BINDING (b->id) = b->shadowed;
1243 break;
1245 case FUNCTION_DECL:
1246 /* Propagate TREE_ADDRESSABLE from nested functions to their
1247 containing functions. */
1248 if (!TREE_ASM_WRITTEN (p)
1249 && DECL_INITIAL (p) != NULL_TREE
1250 && TREE_ADDRESSABLE (p)
1251 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1252 && DECL_ABSTRACT_ORIGIN (p) != p)
1253 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1254 if (!TREE_PUBLIC (p)
1255 && !DECL_INITIAL (p)
1256 && !b->nested
1257 && scope != file_scope
1258 && scope != external_scope)
1260 error ("nested function %q+D declared but never defined", p);
1261 undef_nested_function = true;
1263 else if (DECL_DECLARED_INLINE_P (p)
1264 && TREE_PUBLIC (p)
1265 && !DECL_INITIAL (p))
1267 /* C99 6.7.4p6: "a function with external linkage... declared
1268 with an inline function specifier ... shall also be defined
1269 in the same translation unit." */
1270 if (!flag_gnu89_inline
1271 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1272 && scope == external_scope)
1273 pedwarn (input_location, 0,
1274 "inline function %q+D declared but never defined", p);
1275 DECL_EXTERNAL (p) = 1;
1278 goto common_symbol;
1280 case VAR_DECL:
1281 /* Warnings for unused variables. */
1282 if ((!TREE_USED (p) || !DECL_READ_P (p))
1283 && !TREE_NO_WARNING (p)
1284 && !DECL_IN_SYSTEM_HEADER (p)
1285 && DECL_NAME (p)
1286 && !DECL_ARTIFICIAL (p)
1287 && scope != file_scope
1288 && scope != external_scope)
1290 if (!TREE_USED (p))
1291 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1292 else if (DECL_CONTEXT (p) == current_function_decl)
1293 warning_at (DECL_SOURCE_LOCATION (p),
1294 OPT_Wunused_but_set_variable,
1295 "variable %qD set but not used", p);
1298 if (b->inner_comp)
1300 error ("type of array %q+D completed incompatibly with"
1301 " implicit initialization", p);
1304 /* Fall through. */
1305 case TYPE_DECL:
1306 case CONST_DECL:
1307 common_symbol:
1308 /* All of these go in BLOCK_VARS, but only if this is the
1309 binding in the home scope. */
1310 if (!b->nested)
1312 DECL_CHAIN (p) = BLOCK_VARS (block);
1313 BLOCK_VARS (block) = p;
1315 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1317 /* For block local externs add a special
1318 DECL_EXTERNAL decl for debug info generation. */
1319 tree extp = copy_node (p);
1321 DECL_EXTERNAL (extp) = 1;
1322 TREE_STATIC (extp) = 0;
1323 TREE_PUBLIC (extp) = 1;
1324 DECL_INITIAL (extp) = NULL_TREE;
1325 DECL_LANG_SPECIFIC (extp) = NULL;
1326 DECL_CONTEXT (extp) = current_function_decl;
1327 if (TREE_CODE (p) == FUNCTION_DECL)
1329 DECL_RESULT (extp) = NULL_TREE;
1330 DECL_SAVED_TREE (extp) = NULL_TREE;
1331 DECL_STRUCT_FUNCTION (extp) = NULL;
1333 if (b->locus != UNKNOWN_LOCATION)
1334 DECL_SOURCE_LOCATION (extp) = b->locus;
1335 DECL_CHAIN (extp) = BLOCK_VARS (block);
1336 BLOCK_VARS (block) = extp;
1338 /* If this is the file scope set DECL_CONTEXT of each decl to
1339 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1340 work. */
1341 if (scope == file_scope)
1343 DECL_CONTEXT (p) = context;
1344 if (TREE_CODE (p) == TYPE_DECL
1345 && TREE_TYPE (p) != error_mark_node)
1346 set_type_context (TREE_TYPE (p), context);
1349 gcc_fallthrough ();
1350 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1351 already been put there by store_parm_decls. Unused-
1352 parameter warnings are handled by function.c.
1353 error_mark_node obviously does not go in BLOCK_VARS and
1354 does not get unused-variable warnings. */
1355 case PARM_DECL:
1356 case ERROR_MARK:
1357 /* It is possible for a decl not to have a name. We get
1358 here with b->id NULL in this case. */
1359 if (b->id)
1361 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1362 I_SYMBOL_BINDING (b->id) = b->shadowed;
1363 if (b->shadowed && b->shadowed->u.type)
1364 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1366 break;
1368 default:
1369 gcc_unreachable ();
1374 /* Dispose of the block that we just made inside some higher level. */
1375 if ((scope->function_body || scope == file_scope) && context)
1377 DECL_INITIAL (context) = block;
1378 BLOCK_SUPERCONTEXT (block) = context;
1380 else if (scope->outer)
1382 if (block)
1383 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1384 /* If we did not make a block for the scope just exited, any
1385 blocks made for inner scopes must be carried forward so they
1386 will later become subblocks of something else. */
1387 else if (scope->blocks)
1388 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1391 /* Pop the current scope, and free the structure for reuse. */
1392 current_scope = scope->outer;
1393 if (scope->function_body)
1394 current_function_scope = scope->outer_function;
1396 memset (scope, 0, sizeof (struct c_scope));
1397 scope->outer = scope_freelist;
1398 scope_freelist = scope;
1400 return block;
1403 void
1404 push_file_scope (void)
1406 tree decl;
1408 if (file_scope)
1409 return;
1411 push_scope ();
1412 file_scope = current_scope;
1414 start_fname_decls ();
1416 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1417 bind (DECL_NAME (decl), decl, file_scope,
1418 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1421 void
1422 pop_file_scope (void)
1424 /* In case there were missing closebraces, get us back to the global
1425 binding level. */
1426 while (current_scope != file_scope)
1427 pop_scope ();
1429 /* __FUNCTION__ is defined at file scope (""). This
1430 call may not be necessary as my tests indicate it
1431 still works without it. */
1432 finish_fname_decls ();
1434 check_inline_statics ();
1436 /* This is the point to write out a PCH if we're doing that.
1437 In that case we do not want to do anything else. */
1438 if (pch_file)
1440 c_common_write_pch ();
1441 /* Ensure even the callers don't try to finalize the CU. */
1442 flag_syntax_only = 1;
1443 return;
1446 /* Pop off the file scope and close this translation unit. */
1447 pop_scope ();
1448 file_scope = 0;
1450 maybe_apply_pending_pragma_weaks ();
1453 /* Adjust the bindings for the start of a statement expression. */
1455 void
1456 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1458 struct c_scope *scope;
1460 for (scope = current_scope; scope != NULL; scope = scope->outer)
1462 struct c_binding *b;
1464 if (!scope->has_label_bindings)
1465 continue;
1467 for (b = scope->bindings; b != NULL; b = b->prev)
1469 struct c_label_vars *label_vars;
1470 unsigned int ix;
1471 struct c_goto_bindings *g;
1473 if (TREE_CODE (b->decl) != LABEL_DECL)
1474 continue;
1475 label_vars = b->u.label;
1476 ++label_vars->label_bindings.stmt_exprs;
1477 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1478 ++g->goto_bindings.stmt_exprs;
1482 if (switch_bindings != NULL)
1483 ++switch_bindings->stmt_exprs;
1486 /* Adjust the bindings for the end of a statement expression. */
1488 void
1489 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1491 struct c_scope *scope;
1493 for (scope = current_scope; scope != NULL; scope = scope->outer)
1495 struct c_binding *b;
1497 if (!scope->has_label_bindings)
1498 continue;
1500 for (b = scope->bindings; b != NULL; b = b->prev)
1502 struct c_label_vars *label_vars;
1503 unsigned int ix;
1504 struct c_goto_bindings *g;
1506 if (TREE_CODE (b->decl) != LABEL_DECL)
1507 continue;
1508 label_vars = b->u.label;
1509 --label_vars->label_bindings.stmt_exprs;
1510 if (label_vars->label_bindings.stmt_exprs < 0)
1512 label_vars->label_bindings.left_stmt_expr = true;
1513 label_vars->label_bindings.stmt_exprs = 0;
1515 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1517 --g->goto_bindings.stmt_exprs;
1518 if (g->goto_bindings.stmt_exprs < 0)
1520 g->goto_bindings.left_stmt_expr = true;
1521 g->goto_bindings.stmt_exprs = 0;
1527 if (switch_bindings != NULL)
1529 --switch_bindings->stmt_exprs;
1530 gcc_assert (switch_bindings->stmt_exprs >= 0);
1534 /* Push a definition or a declaration of struct, union or enum tag "name".
1535 "type" should be the type node.
1536 We assume that the tag "name" is not already defined, and has a location
1537 of LOC.
1539 Note that the definition may really be just a forward reference.
1540 In that case, the TYPE_SIZE will be zero. */
1542 static void
1543 pushtag (location_t loc, tree name, tree type)
1545 /* Record the identifier as the type's name if it has none. */
1546 if (name && !TYPE_NAME (type))
1547 TYPE_NAME (type) = name;
1548 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1550 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1551 tagged type we just added to the current scope. This fake
1552 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1553 to output a representation of a tagged type, and it also gives
1554 us a convenient place to record the "scope start" address for the
1555 tagged type. */
1557 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1558 TYPE_DECL, NULL_TREE, type));
1560 /* An approximation for now, so we can tell this is a function-scope tag.
1561 This will be updated in pop_scope. */
1562 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1564 if (warn_cxx_compat && name != NULL_TREE)
1566 struct c_binding *b = I_SYMBOL_BINDING (name);
1568 if (b != NULL
1569 && b->decl != NULL_TREE
1570 && TREE_CODE (b->decl) == TYPE_DECL
1571 && (B_IN_CURRENT_SCOPE (b)
1572 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1573 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1574 != TYPE_MAIN_VARIANT (type)))
1576 auto_diagnostic_group d;
1577 if (warning_at (loc, OPT_Wc___compat,
1578 ("using %qD as both a typedef and a tag is "
1579 "invalid in C++"), b->decl)
1580 && b->locus != UNKNOWN_LOCATION)
1581 inform (b->locus, "originally defined here");
1586 /* An exported interface to pushtag. This is used by the gdb plugin's
1587 binding oracle to introduce a new tag binding. */
1589 void
1590 c_pushtag (location_t loc, tree name, tree type)
1592 pushtag (loc, name, type);
1595 /* An exported interface to bind a declaration. LOC is the location
1596 to use. DECL is the declaration to bind. The decl's name is used
1597 to determine how it is bound. If DECL is a VAR_DECL, then
1598 IS_GLOBAL determines whether the decl is put into the global (file
1599 and external) scope or the current function's scope; if DECL is not
1600 a VAR_DECL then it is always put into the file scope. */
1602 void
1603 c_bind (location_t loc, tree decl, bool is_global)
1605 struct c_scope *scope;
1606 bool nested = false;
1608 if (!VAR_P (decl) || current_function_scope == NULL)
1610 /* Types and functions are always considered to be global. */
1611 scope = file_scope;
1612 DECL_EXTERNAL (decl) = 1;
1613 TREE_PUBLIC (decl) = 1;
1615 else if (is_global)
1617 /* Also bind it into the external scope. */
1618 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1619 nested = true;
1620 scope = file_scope;
1621 DECL_EXTERNAL (decl) = 1;
1622 TREE_PUBLIC (decl) = 1;
1624 else
1626 DECL_CONTEXT (decl) = current_function_decl;
1627 TREE_PUBLIC (decl) = 0;
1628 scope = current_function_scope;
1631 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1634 /* Subroutine of compare_decls. Allow harmless mismatches in return
1635 and argument types provided that the type modes match. This function
1636 return a unified type given a suitable match, and 0 otherwise. */
1638 static tree
1639 match_builtin_function_types (tree newtype, tree oldtype)
1641 tree newrettype, oldrettype;
1642 tree newargs, oldargs;
1643 tree trytype, tryargs;
1645 /* Accept the return type of the new declaration if same modes. */
1646 oldrettype = TREE_TYPE (oldtype);
1647 newrettype = TREE_TYPE (newtype);
1649 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1650 return NULL_TREE;
1652 oldargs = TYPE_ARG_TYPES (oldtype);
1653 newargs = TYPE_ARG_TYPES (newtype);
1654 tryargs = newargs;
1656 while (oldargs || newargs)
1658 if (!oldargs
1659 || !newargs
1660 || !TREE_VALUE (oldargs)
1661 || !TREE_VALUE (newargs)
1662 || TYPE_MODE (TREE_VALUE (oldargs))
1663 != TYPE_MODE (TREE_VALUE (newargs)))
1664 return NULL_TREE;
1666 oldargs = TREE_CHAIN (oldargs);
1667 newargs = TREE_CHAIN (newargs);
1670 trytype = build_function_type (newrettype, tryargs);
1672 /* Allow declaration to change transaction_safe attribute. */
1673 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1674 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1675 tree newattrs = TYPE_ATTRIBUTES (newtype);
1676 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1677 if (oldtsafe && !newtsafe)
1678 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1679 else if (newtsafe && !oldtsafe)
1680 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1681 NULL_TREE, oldattrs);
1683 return build_type_attribute_variant (trytype, oldattrs);
1686 /* Subroutine of diagnose_mismatched_decls. Check for function type
1687 mismatch involving an empty arglist vs a nonempty one and give clearer
1688 diagnostics. */
1689 static void
1690 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1691 tree newtype, tree oldtype)
1693 tree t;
1695 if (TREE_CODE (olddecl) != FUNCTION_DECL
1696 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1697 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1698 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1699 return;
1701 t = TYPE_ARG_TYPES (oldtype);
1702 if (t == NULL_TREE)
1703 t = TYPE_ARG_TYPES (newtype);
1704 for (; t; t = TREE_CHAIN (t))
1706 tree type = TREE_VALUE (t);
1708 if (TREE_CHAIN (t) == NULL_TREE
1709 && TYPE_MAIN_VARIANT (type) != void_type_node)
1711 inform (input_location, "a parameter list with an ellipsis can%'t match "
1712 "an empty parameter name list declaration");
1713 break;
1716 if (c_type_promotes_to (type) != type)
1718 inform (input_location, "an argument type that has a default promotion can%'t match "
1719 "an empty parameter name list declaration");
1720 break;
1725 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1726 old-style function definition, NEWDECL is a prototype declaration.
1727 Diagnose inconsistencies in the argument list. Returns TRUE if
1728 the prototype is compatible, FALSE if not. */
1729 static bool
1730 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1732 tree newargs, oldargs;
1733 int i;
1735 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1737 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1738 newargs = TYPE_ARG_TYPES (newtype);
1739 i = 1;
1741 for (;;)
1743 tree oldargtype = TREE_VALUE (oldargs);
1744 tree newargtype = TREE_VALUE (newargs);
1746 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1747 return false;
1749 oldargtype = (TYPE_ATOMIC (oldargtype)
1750 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1751 TYPE_QUAL_ATOMIC)
1752 : TYPE_MAIN_VARIANT (oldargtype));
1753 newargtype = (TYPE_ATOMIC (newargtype)
1754 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1755 TYPE_QUAL_ATOMIC)
1756 : TYPE_MAIN_VARIANT (newargtype));
1758 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1759 break;
1761 /* Reaching the end of just one list means the two decls don't
1762 agree on the number of arguments. */
1763 if (END_OF_ARGLIST (oldargtype))
1765 error ("prototype for %q+D declares more arguments "
1766 "than previous old-style definition", newdecl);
1767 return false;
1769 else if (END_OF_ARGLIST (newargtype))
1771 error ("prototype for %q+D declares fewer arguments "
1772 "than previous old-style definition", newdecl);
1773 return false;
1776 /* Type for passing arg must be consistent with that declared
1777 for the arg. */
1778 else if (!comptypes (oldargtype, newargtype))
1780 error ("prototype for %q+D declares argument %d"
1781 " with incompatible type",
1782 newdecl, i);
1783 return false;
1786 oldargs = TREE_CHAIN (oldargs);
1787 newargs = TREE_CHAIN (newargs);
1788 i++;
1791 /* If we get here, no errors were found, but do issue a warning
1792 for this poor-style construct. */
1793 warning (0, "prototype for %q+D follows non-prototype definition",
1794 newdecl);
1795 return true;
1796 #undef END_OF_ARGLIST
1799 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1800 first in a pair of mismatched declarations, using the diagnostic
1801 function DIAG. */
1802 static void
1803 locate_old_decl (tree decl)
1805 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl)
1806 && !C_DECL_DECLARED_BUILTIN (decl))
1808 else if (DECL_INITIAL (decl))
1809 inform (input_location, "previous definition of %q+D was here", decl);
1810 else if (C_DECL_IMPLICIT (decl))
1811 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1812 else
1813 inform (input_location, "previous declaration of %q+D was here", decl);
1816 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1817 Returns true if the caller should proceed to merge the two, false
1818 if OLDDECL should simply be discarded. As a side effect, issues
1819 all necessary diagnostics for invalid or poor-style combinations.
1820 If it returns true, writes the types of NEWDECL and OLDDECL to
1821 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1822 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1824 static bool
1825 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1826 tree *newtypep, tree *oldtypep)
1828 tree newtype, oldtype;
1829 bool retval = true;
1831 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1832 && DECL_EXTERNAL (DECL))
1834 /* If we have error_mark_node for either decl or type, just discard
1835 the previous decl - we're in an error cascade already. */
1836 if (olddecl == error_mark_node || newdecl == error_mark_node)
1837 return false;
1838 *oldtypep = oldtype = TREE_TYPE (olddecl);
1839 *newtypep = newtype = TREE_TYPE (newdecl);
1840 if (oldtype == error_mark_node || newtype == error_mark_node)
1841 return false;
1843 /* Two different categories of symbol altogether. This is an error
1844 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1845 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1847 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1848 && fndecl_built_in_p (olddecl)
1849 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1851 auto_diagnostic_group d;
1852 error ("%q+D redeclared as different kind of symbol", newdecl);
1853 locate_old_decl (olddecl);
1855 else if (TREE_PUBLIC (newdecl))
1856 warning (OPT_Wbuiltin_declaration_mismatch,
1857 "built-in function %q+D declared as non-function",
1858 newdecl);
1859 else
1860 warning (OPT_Wshadow, "declaration of %q+D shadows "
1861 "a built-in function", newdecl);
1862 return false;
1865 /* Enumerators have no linkage, so may only be declared once in a
1866 given scope. */
1867 if (TREE_CODE (olddecl) == CONST_DECL)
1869 auto_diagnostic_group d;
1870 error ("redeclaration of enumerator %q+D", newdecl);
1871 locate_old_decl (olddecl);
1872 return false;
1875 bool pedwarned = false;
1876 bool warned = false;
1877 auto_diagnostic_group d;
1879 if (!comptypes (oldtype, newtype))
1881 if (TREE_CODE (olddecl) == FUNCTION_DECL
1882 && fndecl_built_in_p (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1884 /* Accept harmless mismatch in function types.
1885 This is for the ffs and fprintf builtins. */
1886 tree trytype = match_builtin_function_types (newtype, oldtype);
1888 if (trytype && comptypes (newtype, trytype))
1889 *oldtypep = oldtype = trytype;
1890 else
1892 const char *header
1893 = header_for_builtin_fn (DECL_FUNCTION_CODE (olddecl));
1894 location_t loc = DECL_SOURCE_LOCATION (newdecl);
1895 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
1896 "conflicting types for built-in function %q+D; "
1897 "expected %qT",
1898 newdecl, oldtype)
1899 && header)
1901 /* Suggest the right header to include as the preferred
1902 solution rather than the spelling of the declaration. */
1903 rich_location richloc (line_table, loc);
1904 maybe_add_include_fixit (&richloc, header, true);
1905 inform (&richloc,
1906 "%qD is declared in header %qs", olddecl, header);
1908 /* If types don't match for a built-in, throw away the
1909 built-in. No point in calling locate_old_decl here, it
1910 won't print anything. */
1911 return false;
1914 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1915 && DECL_IS_BUILTIN (olddecl))
1917 /* A conflicting function declaration for a predeclared
1918 function that isn't actually built in. Objective C uses
1919 these. The new declaration silently overrides everything
1920 but the volatility (i.e. noreturn) indication. See also
1921 below. FIXME: Make Objective C use normal builtins. */
1922 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1923 return false;
1925 /* Permit void foo (...) to match int foo (...) if the latter is
1926 the definition and implicit int was used. See
1927 c-torture/compile/920625-2.c. */
1928 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1929 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1930 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1931 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1933 pedwarned = pedwarn (input_location, 0,
1934 "conflicting types for %q+D", newdecl);
1935 /* Make sure we keep void as the return type. */
1936 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1937 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1939 /* Permit void foo (...) to match an earlier call to foo (...) with
1940 no declared type (thus, implicitly int). */
1941 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1942 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1943 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1944 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1946 pedwarned = pedwarn (input_location, 0,
1947 "conflicting types for %q+D", newdecl);
1948 /* Make sure we keep void as the return type. */
1949 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1951 else
1953 int new_quals = TYPE_QUALS (newtype);
1954 int old_quals = TYPE_QUALS (oldtype);
1956 if (new_quals != old_quals)
1958 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1959 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1960 if (new_addr != old_addr)
1962 if (ADDR_SPACE_GENERIC_P (new_addr))
1963 error ("conflicting named address spaces (generic vs %s) "
1964 "for %q+D",
1965 c_addr_space_name (old_addr), newdecl);
1966 else if (ADDR_SPACE_GENERIC_P (old_addr))
1967 error ("conflicting named address spaces (%s vs generic) "
1968 "for %q+D",
1969 c_addr_space_name (new_addr), newdecl);
1970 else
1971 error ("conflicting named address spaces (%s vs %s) "
1972 "for %q+D",
1973 c_addr_space_name (new_addr),
1974 c_addr_space_name (old_addr),
1975 newdecl);
1978 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1979 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1980 error ("conflicting type qualifiers for %q+D", newdecl);
1982 else
1983 error ("conflicting types for %q+D", newdecl);
1984 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1985 locate_old_decl (olddecl);
1986 return false;
1990 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1991 but silently ignore the redeclaration if either is in a system
1992 header. (Conflicting redeclarations were handled above.) This
1993 is allowed for C11 if the types are the same, not just
1994 compatible. */
1995 if (TREE_CODE (newdecl) == TYPE_DECL)
1997 bool types_different = false;
1998 int comptypes_result;
2000 comptypes_result
2001 = comptypes_check_different_types (oldtype, newtype, &types_different);
2003 if (comptypes_result != 1 || types_different)
2005 error ("redefinition of typedef %q+D with different type", newdecl);
2006 locate_old_decl (olddecl);
2007 return false;
2010 if (DECL_IN_SYSTEM_HEADER (newdecl)
2011 || DECL_IN_SYSTEM_HEADER (olddecl)
2012 || TREE_NO_WARNING (newdecl)
2013 || TREE_NO_WARNING (olddecl))
2014 return true; /* Allow OLDDECL to continue in use. */
2016 if (variably_modified_type_p (newtype, NULL))
2018 error ("redefinition of typedef %q+D with variably modified type",
2019 newdecl);
2020 locate_old_decl (olddecl);
2022 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2023 "redefinition of typedef %q+D", newdecl))
2024 locate_old_decl (olddecl);
2026 return true;
2029 /* Function declarations can either be 'static' or 'extern' (no
2030 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2031 can never conflict with each other on account of linkage
2032 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2033 gnu89 mode permits two definitions if one is 'extern inline' and
2034 one is not. The non- extern-inline definition supersedes the
2035 extern-inline definition. */
2037 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2039 /* If you declare a built-in function name as static, or
2040 define the built-in with an old-style definition (so we
2041 can't validate the argument list) the built-in definition is
2042 overridden, but optionally warn this was a bad choice of name. */
2043 if (fndecl_built_in_p (olddecl)
2044 && !C_DECL_DECLARED_BUILTIN (olddecl))
2046 if (!TREE_PUBLIC (newdecl)
2047 || (DECL_INITIAL (newdecl)
2048 && !prototype_p (TREE_TYPE (newdecl))))
2050 warning_at (DECL_SOURCE_LOCATION (newdecl),
2051 OPT_Wshadow, "declaration of %qD shadows "
2052 "a built-in function", newdecl);
2053 /* Discard the old built-in function. */
2054 return false;
2057 if (!prototype_p (TREE_TYPE (newdecl)))
2059 /* Set for built-ins that take no arguments. */
2060 bool func_void_args = false;
2061 if (tree at = TYPE_ARG_TYPES (oldtype))
2062 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2064 if (extra_warnings && !func_void_args)
2065 warning_at (DECL_SOURCE_LOCATION (newdecl),
2066 OPT_Wbuiltin_declaration_mismatch,
2067 "declaration of built-in function %qD without "
2068 "a prototype; expected %qT",
2069 newdecl, TREE_TYPE (olddecl));
2073 if (DECL_INITIAL (newdecl))
2075 if (DECL_INITIAL (olddecl))
2077 /* If both decls are in the same TU and the new declaration
2078 isn't overriding an extern inline reject the new decl.
2079 In c99, no overriding is allowed in the same translation
2080 unit. */
2081 if ((!DECL_EXTERN_INLINE (olddecl)
2082 || DECL_EXTERN_INLINE (newdecl)
2083 || (!flag_gnu89_inline
2084 && (!DECL_DECLARED_INLINE_P (olddecl)
2085 || !lookup_attribute ("gnu_inline",
2086 DECL_ATTRIBUTES (olddecl)))
2087 && (!DECL_DECLARED_INLINE_P (newdecl)
2088 || !lookup_attribute ("gnu_inline",
2089 DECL_ATTRIBUTES (newdecl))))
2091 && same_translation_unit_p (newdecl, olddecl))
2093 auto_diagnostic_group d;
2094 error ("redefinition of %q+D", newdecl);
2095 locate_old_decl (olddecl);
2096 return false;
2100 /* If we have a prototype after an old-style function definition,
2101 the argument types must be checked specially. */
2102 else if (DECL_INITIAL (olddecl)
2103 && !prototype_p (oldtype) && prototype_p (newtype)
2104 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2106 auto_diagnostic_group d;
2107 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2109 locate_old_decl (olddecl);
2110 return false;
2113 /* A non-static declaration (even an "extern") followed by a
2114 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2115 The same is true for a static forward declaration at block
2116 scope followed by a non-static declaration/definition at file
2117 scope. Static followed by non-static at the same scope is
2118 not undefined behavior, and is the most convenient way to get
2119 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2120 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2121 we do diagnose it if -Wtraditional. */
2122 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2124 /* Two exceptions to the rule. If olddecl is an extern
2125 inline, or a predeclared function that isn't actually
2126 built in, newdecl silently overrides olddecl. The latter
2127 occur only in Objective C; see also above. (FIXME: Make
2128 Objective C use normal builtins.) */
2129 if (!DECL_IS_BUILTIN (olddecl)
2130 && !DECL_EXTERN_INLINE (olddecl))
2132 auto_diagnostic_group d;
2133 error ("static declaration of %q+D follows "
2134 "non-static declaration", newdecl);
2135 locate_old_decl (olddecl);
2137 return false;
2139 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2141 if (DECL_CONTEXT (olddecl))
2143 auto_diagnostic_group d;
2144 error ("non-static declaration of %q+D follows "
2145 "static declaration", newdecl);
2146 locate_old_decl (olddecl);
2147 return false;
2149 else if (warn_traditional)
2151 warned |= warning (OPT_Wtraditional,
2152 "non-static declaration of %q+D "
2153 "follows static declaration", newdecl);
2157 /* Make sure gnu_inline attribute is either not present, or
2158 present on all inline decls. */
2159 if (DECL_DECLARED_INLINE_P (olddecl)
2160 && DECL_DECLARED_INLINE_P (newdecl))
2162 bool newa = lookup_attribute ("gnu_inline",
2163 DECL_ATTRIBUTES (newdecl)) != NULL;
2164 bool olda = lookup_attribute ("gnu_inline",
2165 DECL_ATTRIBUTES (olddecl)) != NULL;
2166 if (newa != olda)
2168 auto_diagnostic_group d;
2169 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2170 newa ? newdecl : olddecl);
2171 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2172 "but not here");
2176 else if (VAR_P (newdecl))
2178 /* Only variables can be thread-local, and all declarations must
2179 agree on this property. */
2180 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2182 /* Nothing to check. Since OLDDECL is marked threadprivate
2183 and NEWDECL does not have a thread-local attribute, we
2184 will merge the threadprivate attribute into NEWDECL. */
2187 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2189 auto_diagnostic_group d;
2190 if (DECL_THREAD_LOCAL_P (newdecl))
2191 error ("thread-local declaration of %q+D follows "
2192 "non-thread-local declaration", newdecl);
2193 else
2194 error ("non-thread-local declaration of %q+D follows "
2195 "thread-local declaration", newdecl);
2197 locate_old_decl (olddecl);
2198 return false;
2201 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2202 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2204 auto_diagnostic_group d;
2205 error ("redefinition of %q+D", newdecl);
2206 locate_old_decl (olddecl);
2207 return false;
2210 /* Objects declared at file scope: if the first declaration had
2211 external linkage (even if it was an external reference) the
2212 second must have external linkage as well, or the behavior is
2213 undefined. If the first declaration had internal linkage, then
2214 the second must too, or else be an external reference (in which
2215 case the composite declaration still has internal linkage).
2216 As for function declarations, we warn about the static-then-
2217 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2218 if (DECL_FILE_SCOPE_P (newdecl)
2219 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2221 if (DECL_EXTERNAL (newdecl))
2223 if (!DECL_FILE_SCOPE_P (olddecl))
2225 auto_diagnostic_group d;
2226 error ("extern declaration of %q+D follows "
2227 "declaration with no linkage", newdecl);
2228 locate_old_decl (olddecl);
2229 return false;
2231 else if (warn_traditional)
2233 warned |= warning (OPT_Wtraditional,
2234 "non-static declaration of %q+D "
2235 "follows static declaration", newdecl);
2238 else
2240 auto_diagnostic_group d;
2241 if (TREE_PUBLIC (newdecl))
2242 error ("non-static declaration of %q+D follows "
2243 "static declaration", newdecl);
2244 else
2245 error ("static declaration of %q+D follows "
2246 "non-static declaration", newdecl);
2248 locate_old_decl (olddecl);
2249 return false;
2252 /* Two objects with the same name declared at the same block
2253 scope must both be external references (6.7p3). */
2254 else if (!DECL_FILE_SCOPE_P (newdecl))
2256 if (DECL_EXTERNAL (newdecl))
2258 /* Extern with initializer at block scope, which will
2259 already have received an error. */
2261 else if (DECL_EXTERNAL (olddecl))
2263 auto_diagnostic_group d;
2264 error ("declaration of %q+D with no linkage follows "
2265 "extern declaration", newdecl);
2266 locate_old_decl (olddecl);
2268 else
2270 auto_diagnostic_group d;
2271 error ("redeclaration of %q+D with no linkage", newdecl);
2272 locate_old_decl (olddecl);
2275 return false;
2278 /* C++ does not permit a decl to appear multiple times at file
2279 scope. */
2280 if (warn_cxx_compat
2281 && DECL_FILE_SCOPE_P (newdecl)
2282 && !DECL_EXTERNAL (newdecl)
2283 && !DECL_EXTERNAL (olddecl))
2284 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2285 OPT_Wc___compat,
2286 ("duplicate declaration of %qD is "
2287 "invalid in C++"),
2288 newdecl);
2291 /* warnings */
2292 /* All decls must agree on a visibility. */
2293 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2294 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2295 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2297 warned |= warning (0, "redeclaration of %q+D with different visibility "
2298 "(old visibility preserved)", newdecl);
2301 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2302 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2303 else /* PARM_DECL, VAR_DECL */
2305 /* Redeclaration of a parameter is a constraint violation (this is
2306 not explicitly stated, but follows from C99 6.7p3 [no more than
2307 one declaration of the same identifier with no linkage in the
2308 same scope, except type tags] and 6.2.2p6 [parameters have no
2309 linkage]). We must check for a forward parameter declaration,
2310 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2311 an extension, the mandatory diagnostic for which is handled by
2312 mark_forward_parm_decls. */
2314 if (TREE_CODE (newdecl) == PARM_DECL
2315 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2317 auto_diagnostic_group d;
2318 error ("redefinition of parameter %q+D", newdecl);
2319 locate_old_decl (olddecl);
2320 return false;
2324 /* Optional warning for completely redundant decls. */
2325 if (!warned && !pedwarned
2326 && warn_redundant_decls
2327 /* Don't warn about a function declaration followed by a
2328 definition. */
2329 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2330 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2331 /* Don't warn about redundant redeclarations of builtins. */
2332 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2333 && !fndecl_built_in_p (newdecl)
2334 && fndecl_built_in_p (olddecl)
2335 && !C_DECL_DECLARED_BUILTIN (olddecl))
2336 /* Don't warn about an extern followed by a definition. */
2337 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2338 /* Don't warn about forward parameter decls. */
2339 && !(TREE_CODE (newdecl) == PARM_DECL
2340 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2341 /* Don't warn about a variable definition following a declaration. */
2342 && !(VAR_P (newdecl)
2343 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2345 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2346 newdecl);
2349 /* Report location of previous decl/defn. */
2350 if (warned || pedwarned)
2351 locate_old_decl (olddecl);
2353 #undef DECL_EXTERN_INLINE
2355 return retval;
2358 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2359 consistent with OLDDECL, but carries new information. Merge the
2360 new information into OLDDECL. This function issues no
2361 diagnostics. */
2363 static void
2364 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2366 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2367 && DECL_INITIAL (newdecl) != NULL_TREE);
2368 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2369 && prototype_p (TREE_TYPE (newdecl)));
2370 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2371 && prototype_p (TREE_TYPE (olddecl)));
2373 /* For real parm decl following a forward decl, rechain the old decl
2374 in its new location and clear TREE_ASM_WRITTEN (it's not a
2375 forward decl anymore). */
2376 if (TREE_CODE (newdecl) == PARM_DECL
2377 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2379 struct c_binding *b, **here;
2381 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2382 if ((*here)->decl == olddecl)
2383 goto found;
2384 gcc_unreachable ();
2386 found:
2387 b = *here;
2388 *here = b->prev;
2389 b->prev = current_scope->bindings;
2390 current_scope->bindings = b;
2392 TREE_ASM_WRITTEN (olddecl) = 0;
2395 DECL_ATTRIBUTES (newdecl)
2396 = targetm.merge_decl_attributes (olddecl, newdecl);
2398 /* For typedefs use the old type, as the new type's DECL_NAME points
2399 at newdecl, which will be ggc_freed. */
2400 if (TREE_CODE (newdecl) == TYPE_DECL)
2402 /* But NEWTYPE might have an attribute, honor that. */
2403 tree tem = newtype;
2404 newtype = oldtype;
2406 if (TYPE_USER_ALIGN (tem))
2408 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2409 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2410 TYPE_USER_ALIGN (newtype) = true;
2413 /* And remove the new type from the variants list. */
2414 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2416 tree remove = TREE_TYPE (newdecl);
2417 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2418 t = TYPE_NEXT_VARIANT (t))
2419 if (TYPE_NEXT_VARIANT (t) == remove)
2421 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2422 break;
2427 /* Merge the data types specified in the two decls. */
2428 TREE_TYPE (newdecl)
2429 = TREE_TYPE (olddecl)
2430 = composite_type (newtype, oldtype);
2432 /* Lay the type out, unless already done. */
2433 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2435 if (TREE_TYPE (newdecl) != error_mark_node)
2436 layout_type (TREE_TYPE (newdecl));
2437 if (TREE_CODE (newdecl) != FUNCTION_DECL
2438 && TREE_CODE (newdecl) != TYPE_DECL
2439 && TREE_CODE (newdecl) != CONST_DECL)
2440 layout_decl (newdecl, 0);
2442 else
2444 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2445 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2446 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2447 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2448 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2450 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2451 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2453 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2454 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2455 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2456 DECL_WARN_IF_NOT_ALIGN (olddecl));
2459 /* Keep the old rtl since we can safely use it. */
2460 if (HAS_RTL_P (olddecl))
2461 COPY_DECL_RTL (olddecl, newdecl);
2463 /* Merge the type qualifiers. */
2464 if (TREE_READONLY (newdecl))
2465 TREE_READONLY (olddecl) = 1;
2467 if (TREE_THIS_VOLATILE (newdecl))
2468 TREE_THIS_VOLATILE (olddecl) = 1;
2470 /* Merge deprecatedness. */
2471 if (TREE_DEPRECATED (newdecl))
2472 TREE_DEPRECATED (olddecl) = 1;
2474 /* If a decl is in a system header and the other isn't, keep the one on the
2475 system header. Otherwise, keep source location of definition rather than
2476 declaration and of prototype rather than non-prototype unless that
2477 prototype is built-in. */
2478 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2479 && DECL_IN_SYSTEM_HEADER (olddecl)
2480 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2481 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2482 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2483 && DECL_IN_SYSTEM_HEADER (newdecl)
2484 && !DECL_IN_SYSTEM_HEADER (olddecl))
2485 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2486 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2487 && DECL_INITIAL (olddecl) != NULL_TREE)
2488 || (old_is_prototype && !new_is_prototype
2489 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2490 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2492 /* Merge the initialization information. */
2493 if (DECL_INITIAL (newdecl) == NULL_TREE)
2494 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2496 /* Merge the threadprivate attribute. */
2497 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2498 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2500 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2502 /* Copy the assembler name.
2503 Currently, it can only be defined in the prototype. */
2504 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2506 /* Use visibility of whichever declaration had it specified */
2507 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2509 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2510 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2513 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2515 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2516 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2517 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2518 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2519 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2520 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2521 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2522 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2523 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2524 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2525 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2528 /* Merge the storage class information. */
2529 merge_weak (newdecl, olddecl);
2531 /* For functions, static overrides non-static. */
2532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2534 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2535 /* This is since we don't automatically
2536 copy the attributes of NEWDECL into OLDDECL. */
2537 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2538 /* If this clears `static', clear it in the identifier too. */
2539 if (!TREE_PUBLIC (olddecl))
2540 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2544 /* In c99, 'extern' declaration before (or after) 'inline' means this
2545 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2546 is present. */
2547 if (TREE_CODE (newdecl) == FUNCTION_DECL
2548 && !flag_gnu89_inline
2549 && (DECL_DECLARED_INLINE_P (newdecl)
2550 || DECL_DECLARED_INLINE_P (olddecl))
2551 && (!DECL_DECLARED_INLINE_P (newdecl)
2552 || !DECL_DECLARED_INLINE_P (olddecl)
2553 || !DECL_EXTERNAL (olddecl))
2554 && DECL_EXTERNAL (newdecl)
2555 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2556 && !current_function_decl)
2557 DECL_EXTERNAL (newdecl) = 0;
2559 /* An inline definition following a static declaration is not
2560 DECL_EXTERNAL. */
2561 if (new_is_definition
2562 && (DECL_DECLARED_INLINE_P (newdecl)
2563 || DECL_DECLARED_INLINE_P (olddecl))
2564 && !TREE_PUBLIC (olddecl))
2565 DECL_EXTERNAL (newdecl) = 0;
2567 if (DECL_EXTERNAL (newdecl))
2569 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2570 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2572 /* An extern decl does not override previous storage class. */
2573 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2574 if (!DECL_EXTERNAL (newdecl))
2576 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2577 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2580 else
2582 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2583 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2586 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2588 /* If we're redefining a function previously defined as extern
2589 inline, make sure we emit debug info for the inline before we
2590 throw it away, in case it was inlined into a function that
2591 hasn't been written out yet. */
2592 if (new_is_definition && DECL_INITIAL (olddecl))
2593 /* The new defn must not be inline. */
2594 DECL_UNINLINABLE (newdecl) = 1;
2595 else
2597 /* If either decl says `inline', this fn is inline, unless
2598 its definition was passed already. */
2599 if (DECL_DECLARED_INLINE_P (newdecl)
2600 || DECL_DECLARED_INLINE_P (olddecl))
2601 DECL_DECLARED_INLINE_P (newdecl) = 1;
2603 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2604 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2606 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2607 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2608 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2609 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2612 if (fndecl_built_in_p (olddecl))
2614 /* If redeclaring a builtin function, it stays built in.
2615 But it gets tagged as having been declared. */
2616 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2617 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2618 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2619 if (new_is_prototype)
2621 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2622 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2624 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2625 switch (fncode)
2627 /* If a compatible prototype of these builtin functions
2628 is seen, assume the runtime implements it with the
2629 expected semantics. */
2630 case BUILT_IN_STPCPY:
2631 if (builtin_decl_explicit_p (fncode))
2632 set_builtin_decl_implicit_p (fncode, true);
2633 break;
2634 default:
2635 if (builtin_decl_explicit_p (fncode))
2636 set_builtin_decl_declared_p (fncode, true);
2637 break;
2640 copy_attributes_to_builtin (newdecl);
2643 else
2644 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2645 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2648 /* Preserve function specific target and optimization options */
2649 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2650 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2651 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2652 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2654 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2655 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2656 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2657 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2659 /* Also preserve various other info from the definition. */
2660 if (!new_is_definition)
2662 tree t;
2663 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2664 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2665 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2666 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2667 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2668 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2669 DECL_CONTEXT (t) = newdecl;
2671 /* See if we've got a function to instantiate from. */
2672 if (DECL_SAVED_TREE (olddecl))
2673 DECL_ABSTRACT_ORIGIN (newdecl)
2674 = DECL_ABSTRACT_ORIGIN (olddecl);
2678 /* Merge the USED information. */
2679 if (TREE_USED (olddecl))
2680 TREE_USED (newdecl) = 1;
2681 else if (TREE_USED (newdecl))
2682 TREE_USED (olddecl) = 1;
2683 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2684 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2685 if (DECL_PRESERVE_P (olddecl))
2686 DECL_PRESERVE_P (newdecl) = 1;
2687 else if (DECL_PRESERVE_P (newdecl))
2688 DECL_PRESERVE_P (olddecl) = 1;
2690 /* Merge DECL_COMMON */
2691 if (VAR_P (olddecl) && VAR_P (newdecl)
2692 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2693 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2694 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2696 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2697 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2698 DECL_ARGUMENTS (if appropriate). */
2700 unsigned olddecl_uid = DECL_UID (olddecl);
2701 tree olddecl_context = DECL_CONTEXT (olddecl);
2702 tree olddecl_arguments = NULL;
2703 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2704 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2706 memcpy ((char *) olddecl + sizeof (struct tree_common),
2707 (char *) newdecl + sizeof (struct tree_common),
2708 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2709 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2710 switch (TREE_CODE (olddecl))
2712 case FUNCTION_DECL:
2713 case VAR_DECL:
2715 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2717 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2718 (char *) newdecl + sizeof (struct tree_decl_common),
2719 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2720 olddecl->decl_with_vis.symtab_node = snode;
2722 if ((DECL_EXTERNAL (olddecl)
2723 || TREE_PUBLIC (olddecl)
2724 || TREE_STATIC (olddecl))
2725 && DECL_SECTION_NAME (newdecl) != NULL)
2726 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2728 /* This isn't quite correct for something like
2729 int __thread x attribute ((tls_model ("local-exec")));
2730 extern int __thread x;
2731 as we'll lose the "local-exec" model. */
2732 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2733 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2734 break;
2737 case FIELD_DECL:
2738 case PARM_DECL:
2739 case LABEL_DECL:
2740 case RESULT_DECL:
2741 case CONST_DECL:
2742 case TYPE_DECL:
2743 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2744 (char *) newdecl + sizeof (struct tree_decl_common),
2745 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2746 break;
2748 default:
2750 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2751 (char *) newdecl + sizeof (struct tree_decl_common),
2752 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2754 DECL_UID (olddecl) = olddecl_uid;
2755 DECL_CONTEXT (olddecl) = olddecl_context;
2756 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2757 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2760 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2761 so that encode_section_info has a chance to look at the new decl
2762 flags and attributes. */
2763 if (DECL_RTL_SET_P (olddecl)
2764 && (TREE_CODE (olddecl) == FUNCTION_DECL
2765 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2766 make_decl_rtl (olddecl);
2769 /* Handle when a new declaration NEWDECL has the same name as an old
2770 one OLDDECL in the same binding contour. Prints an error message
2771 if appropriate.
2773 If safely possible, alter OLDDECL to look like NEWDECL, and return
2774 true. Otherwise, return false. */
2776 static bool
2777 duplicate_decls (tree newdecl, tree olddecl)
2779 tree newtype = NULL, oldtype = NULL;
2781 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2783 /* Avoid `unused variable' and other warnings for OLDDECL. */
2784 TREE_NO_WARNING (olddecl) = 1;
2785 return false;
2788 merge_decls (newdecl, olddecl, newtype, oldtype);
2790 /* The NEWDECL will no longer be needed.
2792 Before releasing the node, be sure to remove function from symbol
2793 table that might have been inserted there to record comdat group.
2794 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2795 structure is shared in between NEWDECL and OLDECL. */
2796 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2797 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2798 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2800 struct symtab_node *snode = symtab_node::get (newdecl);
2801 if (snode)
2802 snode->remove ();
2804 ggc_free (newdecl);
2805 return true;
2809 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2810 static void
2811 warn_if_shadowing (tree new_decl)
2813 struct c_binding *b;
2815 /* Shadow warnings wanted? */
2816 if (!(warn_shadow
2817 || warn_shadow_local
2818 || warn_shadow_compatible_local)
2819 /* No shadow warnings for internally generated vars. */
2820 || DECL_IS_BUILTIN (new_decl))
2821 return;
2823 /* Is anything being shadowed? Invisible decls do not count. */
2824 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2825 if (b->decl && b->decl != new_decl && !b->invisible
2826 && (b->decl == error_mark_node
2827 || diagnostic_report_warnings_p (global_dc,
2828 DECL_SOURCE_LOCATION (b->decl))))
2830 tree old_decl = b->decl;
2832 if (old_decl == error_mark_node)
2834 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2835 "non-variable", new_decl);
2836 break;
2839 bool warned = false;
2840 auto_diagnostic_group d;
2841 if (TREE_CODE (old_decl) == PARM_DECL)
2843 enum opt_code warning_code;
2845 /* If '-Wshadow=compatible-local' is specified without other
2846 -Wshadow= flags, we will warn only when the types of the
2847 shadowing variable (i.e. new_decl) and the shadowed variable
2848 (old_decl) are compatible. */
2849 if (warn_shadow)
2850 warning_code = OPT_Wshadow;
2851 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2852 warning_code = OPT_Wshadow_compatible_local;
2853 else
2854 warning_code = OPT_Wshadow_local;
2855 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2856 "declaration of %qD shadows a parameter",
2857 new_decl);
2859 else if (DECL_FILE_SCOPE_P (old_decl))
2861 /* Do not warn if a variable shadows a function, unless
2862 the variable is a function or a pointer-to-function. */
2863 if (TREE_CODE (old_decl) == FUNCTION_DECL
2864 && TREE_CODE (new_decl) != FUNCTION_DECL
2865 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2866 continue;
2868 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2869 "declaration of %qD shadows a global "
2870 "declaration",
2871 new_decl);
2873 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2874 && fndecl_built_in_p (old_decl))
2876 warning (OPT_Wshadow, "declaration of %q+D shadows "
2877 "a built-in function", new_decl);
2878 break;
2880 else
2882 enum opt_code warning_code;
2884 /* If '-Wshadow=compatible-local' is specified without other
2885 -Wshadow= flags, we will warn only when the types of the
2886 shadowing variable (i.e. new_decl) and the shadowed variable
2887 (old_decl) are compatible. */
2888 if (warn_shadow)
2889 warning_code = OPT_Wshadow;
2890 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2891 warning_code = OPT_Wshadow_compatible_local;
2892 else
2893 warning_code = OPT_Wshadow_local;
2894 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2895 "declaration of %qD shadows a previous local",
2896 new_decl);
2899 if (warned)
2900 inform (DECL_SOURCE_LOCATION (old_decl),
2901 "shadowed declaration is here");
2903 break;
2907 /* Record a decl-node X as belonging to the current lexical scope.
2908 Check for errors (such as an incompatible declaration for the same
2909 name already seen in the same scope).
2911 Returns either X or an old decl for the same name.
2912 If an old decl is returned, it may have been smashed
2913 to agree with what X says. */
2915 tree
2916 pushdecl (tree x)
2918 tree name = DECL_NAME (x);
2919 struct c_scope *scope = current_scope;
2920 struct c_binding *b;
2921 bool nested = false;
2922 location_t locus = DECL_SOURCE_LOCATION (x);
2924 /* Must set DECL_CONTEXT for everything not at file scope or
2925 DECL_FILE_SCOPE_P won't work. Local externs don't count
2926 unless they have initializers (which generate code). */
2927 if (current_function_decl
2928 && (!VAR_OR_FUNCTION_DECL_P (x)
2929 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2930 DECL_CONTEXT (x) = current_function_decl;
2932 /* Anonymous decls are just inserted in the scope. */
2933 if (!name)
2935 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2936 locus);
2937 return x;
2940 /* First, see if there is another declaration with the same name in
2941 the current scope. If there is, duplicate_decls may do all the
2942 work for us. If duplicate_decls returns false, that indicates
2943 two incompatible decls in the same scope; we are to silently
2944 replace the old one (duplicate_decls has issued all appropriate
2945 diagnostics). In particular, we should not consider possible
2946 duplicates in the external scope, or shadowing. */
2947 b = I_SYMBOL_BINDING (name);
2948 if (b && B_IN_SCOPE (b, scope))
2950 struct c_binding *b_ext, *b_use;
2951 tree type = TREE_TYPE (x);
2952 tree visdecl = b->decl;
2953 tree vistype = TREE_TYPE (visdecl);
2954 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2955 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2956 b->inner_comp = false;
2957 b_use = b;
2958 b_ext = b;
2959 /* If this is an external linkage declaration, we should check
2960 for compatibility with the type in the external scope before
2961 setting the type at this scope based on the visible
2962 information only. */
2963 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2965 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2966 b_ext = b_ext->shadowed;
2967 if (b_ext)
2969 b_use = b_ext;
2970 if (b_use->u.type)
2971 TREE_TYPE (b_use->decl) = b_use->u.type;
2974 if (duplicate_decls (x, b_use->decl))
2976 if (b_use != b)
2978 /* Save the updated type in the external scope and
2979 restore the proper type for this scope. */
2980 tree thistype;
2981 if (comptypes (vistype, type))
2982 thistype = composite_type (vistype, type);
2983 else
2984 thistype = TREE_TYPE (b_use->decl);
2985 b_use->u.type = TREE_TYPE (b_use->decl);
2986 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2987 && fndecl_built_in_p (b_use->decl))
2988 thistype
2989 = build_type_attribute_variant (thistype,
2990 TYPE_ATTRIBUTES
2991 (b_use->u.type));
2992 TREE_TYPE (b_use->decl) = thistype;
2994 return b_use->decl;
2996 else
2997 goto skip_external_and_shadow_checks;
3000 /* All declarations with external linkage, and all external
3001 references, go in the external scope, no matter what scope is
3002 current. However, the binding in that scope is ignored for
3003 purposes of normal name lookup. A separate binding structure is
3004 created in the requested scope; this governs the normal
3005 visibility of the symbol.
3007 The binding in the externals scope is used exclusively for
3008 detecting duplicate declarations of the same object, no matter
3009 what scope they are in; this is what we do here. (C99 6.2.7p2:
3010 All declarations that refer to the same object or function shall
3011 have compatible type; otherwise, the behavior is undefined.) */
3012 if (DECL_EXTERNAL (x) || scope == file_scope)
3014 tree type = TREE_TYPE (x);
3015 tree vistype = NULL_TREE;
3016 tree visdecl = NULL_TREE;
3017 bool type_saved = false;
3018 if (b && !B_IN_EXTERNAL_SCOPE (b)
3019 && VAR_OR_FUNCTION_DECL_P (b->decl)
3020 && DECL_FILE_SCOPE_P (b->decl))
3022 visdecl = b->decl;
3023 vistype = TREE_TYPE (visdecl);
3025 if (scope != file_scope
3026 && !DECL_IN_SYSTEM_HEADER (x))
3027 warning_at (locus, OPT_Wnested_externs,
3028 "nested extern declaration of %qD", x);
3030 while (b && !B_IN_EXTERNAL_SCOPE (b))
3032 /* If this decl might be modified, save its type. This is
3033 done here rather than when the decl is first bound
3034 because the type may change after first binding, through
3035 being completed or through attributes being added. If we
3036 encounter multiple such decls, only the first should have
3037 its type saved; the others will already have had their
3038 proper types saved and the types will not have changed as
3039 their scopes will not have been re-entered. */
3040 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3042 b->u.type = TREE_TYPE (b->decl);
3043 type_saved = true;
3045 if (B_IN_FILE_SCOPE (b)
3046 && VAR_P (b->decl)
3047 && TREE_STATIC (b->decl)
3048 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3049 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3050 && TREE_CODE (type) == ARRAY_TYPE
3051 && TYPE_DOMAIN (type)
3052 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3053 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3055 /* Array type completed in inner scope, which should be
3056 diagnosed if the completion does not have size 1 and
3057 it does not get completed in the file scope. */
3058 b->inner_comp = true;
3060 b = b->shadowed;
3063 /* If a matching external declaration has been found, set its
3064 type to the composite of all the types of that declaration.
3065 After the consistency checks, it will be reset to the
3066 composite of the visible types only. */
3067 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3068 && b->u.type)
3069 TREE_TYPE (b->decl) = b->u.type;
3071 /* The point of the same_translation_unit_p check here is,
3072 we want to detect a duplicate decl for a construct like
3073 foo() { extern bar(); } ... static bar(); but not if
3074 they are in different translation units. In any case,
3075 the static does not go in the externals scope. */
3076 if (b
3077 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3078 && duplicate_decls (x, b->decl))
3080 tree thistype;
3081 if (vistype)
3083 if (comptypes (vistype, type))
3084 thistype = composite_type (vistype, type);
3085 else
3086 thistype = TREE_TYPE (b->decl);
3088 else
3089 thistype = type;
3090 b->u.type = TREE_TYPE (b->decl);
3091 if (TREE_CODE (b->decl) == FUNCTION_DECL
3092 && fndecl_built_in_p (b->decl))
3093 thistype
3094 = build_type_attribute_variant (thistype,
3095 TYPE_ATTRIBUTES (b->u.type));
3096 TREE_TYPE (b->decl) = thistype;
3097 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3098 locus);
3099 return b->decl;
3101 else if (TREE_PUBLIC (x))
3103 if (visdecl && !b && duplicate_decls (x, visdecl))
3105 /* An external declaration at block scope referring to a
3106 visible entity with internal linkage. The composite
3107 type will already be correct for this scope, so we
3108 just need to fall through to make the declaration in
3109 this scope. */
3110 nested = true;
3111 x = visdecl;
3113 else
3115 bind (name, x, external_scope, /*invisible=*/true,
3116 /*nested=*/false, locus);
3117 nested = true;
3122 if (TREE_CODE (x) != PARM_DECL)
3123 warn_if_shadowing (x);
3125 skip_external_and_shadow_checks:
3126 if (TREE_CODE (x) == TYPE_DECL)
3128 /* So this is a typedef, set its underlying type. */
3129 set_underlying_type (x);
3131 /* If X is a typedef defined in the current function, record it
3132 for the purpose of implementing the -Wunused-local-typedefs
3133 warning. */
3134 record_locally_defined_typedef (x);
3137 bind (name, x, scope, /*invisible=*/false, nested, locus);
3139 /* If x's type is incomplete because it's based on a
3140 structure or union which has not yet been fully declared,
3141 attach it to that structure or union type, so we can go
3142 back and complete the variable declaration later, if the
3143 structure or union gets fully declared.
3145 If the input is erroneous, we can have error_mark in the type
3146 slot (e.g. "f(void a, ...)") - that doesn't count as an
3147 incomplete type. */
3148 if (TREE_TYPE (x) != error_mark_node
3149 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3151 tree element = TREE_TYPE (x);
3153 while (TREE_CODE (element) == ARRAY_TYPE)
3154 element = TREE_TYPE (element);
3155 element = TYPE_MAIN_VARIANT (element);
3157 if (RECORD_OR_UNION_TYPE_P (element)
3158 && (TREE_CODE (x) != TYPE_DECL
3159 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3160 && !COMPLETE_TYPE_P (element))
3161 C_TYPE_INCOMPLETE_VARS (element)
3162 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3164 return x;
3168 /* Issue a warning about implicit function declaration. ID is the function
3169 identifier, OLDDECL is a declaration of the function in a different scope,
3170 or NULL_TREE. */
3172 static void
3173 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3175 if (!warn_implicit_function_declaration)
3176 return;
3178 bool warned;
3179 auto_diagnostic_group d;
3180 name_hint hint;
3181 if (!olddecl)
3182 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3184 if (flag_isoc99)
3186 if (const char *suggestion = hint.suggestion ())
3188 gcc_rich_location richloc (loc);
3189 richloc.add_fixit_replace (suggestion);
3190 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3191 "implicit declaration of function %qE;"
3192 " did you mean %qs?",
3193 id, suggestion);
3195 else
3196 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3197 "implicit declaration of function %qE", id);
3199 else if (const char *suggestion = hint.suggestion ())
3201 gcc_rich_location richloc (loc);
3202 richloc.add_fixit_replace (suggestion);
3203 warned = warning_at
3204 (&richloc, OPT_Wimplicit_function_declaration,
3205 G_("implicit declaration of function %qE; did you mean %qs?"),
3206 id, suggestion);
3208 else
3209 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3210 G_("implicit declaration of function %qE"), id);
3212 if (olddecl && warned)
3213 locate_old_decl (olddecl);
3215 if (!warned)
3216 hint.suppress ();
3219 /* This function represents mapping of a function code FCODE
3220 to its respective header. */
3222 static const char *
3223 header_for_builtin_fn (enum built_in_function fcode)
3225 switch (fcode)
3227 CASE_FLT_FN (BUILT_IN_ACOS):
3228 CASE_FLT_FN (BUILT_IN_ACOSH):
3229 CASE_FLT_FN (BUILT_IN_ASIN):
3230 CASE_FLT_FN (BUILT_IN_ASINH):
3231 CASE_FLT_FN (BUILT_IN_ATAN):
3232 CASE_FLT_FN (BUILT_IN_ATANH):
3233 CASE_FLT_FN (BUILT_IN_ATAN2):
3234 CASE_FLT_FN (BUILT_IN_CBRT):
3235 CASE_FLT_FN (BUILT_IN_CEIL):
3236 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3237 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3238 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3239 CASE_FLT_FN (BUILT_IN_COS):
3240 CASE_FLT_FN (BUILT_IN_COSH):
3241 CASE_FLT_FN (BUILT_IN_ERF):
3242 CASE_FLT_FN (BUILT_IN_ERFC):
3243 CASE_FLT_FN (BUILT_IN_EXP):
3244 CASE_FLT_FN (BUILT_IN_EXP2):
3245 CASE_FLT_FN (BUILT_IN_EXPM1):
3246 CASE_FLT_FN (BUILT_IN_FABS):
3247 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3248 CASE_FLT_FN (BUILT_IN_FDIM):
3249 CASE_FLT_FN (BUILT_IN_FLOOR):
3250 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3251 CASE_FLT_FN (BUILT_IN_FMA):
3252 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3253 CASE_FLT_FN (BUILT_IN_FMAX):
3254 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3255 CASE_FLT_FN (BUILT_IN_FMIN):
3256 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3257 CASE_FLT_FN (BUILT_IN_FMOD):
3258 CASE_FLT_FN (BUILT_IN_FREXP):
3259 CASE_FLT_FN (BUILT_IN_HYPOT):
3260 CASE_FLT_FN (BUILT_IN_ILOGB):
3261 CASE_FLT_FN (BUILT_IN_LDEXP):
3262 CASE_FLT_FN (BUILT_IN_LGAMMA):
3263 CASE_FLT_FN (BUILT_IN_LLRINT):
3264 CASE_FLT_FN (BUILT_IN_LLROUND):
3265 CASE_FLT_FN (BUILT_IN_LOG):
3266 CASE_FLT_FN (BUILT_IN_LOG10):
3267 CASE_FLT_FN (BUILT_IN_LOG1P):
3268 CASE_FLT_FN (BUILT_IN_LOG2):
3269 CASE_FLT_FN (BUILT_IN_LOGB):
3270 CASE_FLT_FN (BUILT_IN_LRINT):
3271 CASE_FLT_FN (BUILT_IN_LROUND):
3272 CASE_FLT_FN (BUILT_IN_MODF):
3273 CASE_FLT_FN (BUILT_IN_NAN):
3274 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3275 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3276 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3277 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3278 CASE_FLT_FN (BUILT_IN_POW):
3279 CASE_FLT_FN (BUILT_IN_REMAINDER):
3280 CASE_FLT_FN (BUILT_IN_REMQUO):
3281 CASE_FLT_FN (BUILT_IN_RINT):
3282 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3283 CASE_FLT_FN (BUILT_IN_ROUND):
3284 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3285 CASE_FLT_FN (BUILT_IN_SCALBLN):
3286 CASE_FLT_FN (BUILT_IN_SCALBN):
3287 CASE_FLT_FN (BUILT_IN_SIN):
3288 CASE_FLT_FN (BUILT_IN_SINH):
3289 CASE_FLT_FN (BUILT_IN_SINCOS):
3290 CASE_FLT_FN (BUILT_IN_SQRT):
3291 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3292 CASE_FLT_FN (BUILT_IN_TAN):
3293 CASE_FLT_FN (BUILT_IN_TANH):
3294 CASE_FLT_FN (BUILT_IN_TGAMMA):
3295 CASE_FLT_FN (BUILT_IN_TRUNC):
3296 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3297 case BUILT_IN_ISINF:
3298 case BUILT_IN_ISNAN:
3299 return "<math.h>";
3300 CASE_FLT_FN (BUILT_IN_CABS):
3301 CASE_FLT_FN (BUILT_IN_CACOS):
3302 CASE_FLT_FN (BUILT_IN_CACOSH):
3303 CASE_FLT_FN (BUILT_IN_CARG):
3304 CASE_FLT_FN (BUILT_IN_CASIN):
3305 CASE_FLT_FN (BUILT_IN_CASINH):
3306 CASE_FLT_FN (BUILT_IN_CATAN):
3307 CASE_FLT_FN (BUILT_IN_CATANH):
3308 CASE_FLT_FN (BUILT_IN_CCOS):
3309 CASE_FLT_FN (BUILT_IN_CCOSH):
3310 CASE_FLT_FN (BUILT_IN_CEXP):
3311 CASE_FLT_FN (BUILT_IN_CIMAG):
3312 CASE_FLT_FN (BUILT_IN_CLOG):
3313 CASE_FLT_FN (BUILT_IN_CONJ):
3314 CASE_FLT_FN (BUILT_IN_CPOW):
3315 CASE_FLT_FN (BUILT_IN_CPROJ):
3316 CASE_FLT_FN (BUILT_IN_CREAL):
3317 CASE_FLT_FN (BUILT_IN_CSIN):
3318 CASE_FLT_FN (BUILT_IN_CSINH):
3319 CASE_FLT_FN (BUILT_IN_CSQRT):
3320 CASE_FLT_FN (BUILT_IN_CTAN):
3321 CASE_FLT_FN (BUILT_IN_CTANH):
3322 return "<complex.h>";
3323 case BUILT_IN_MEMCHR:
3324 case BUILT_IN_MEMCMP:
3325 case BUILT_IN_MEMCPY:
3326 case BUILT_IN_MEMMOVE:
3327 case BUILT_IN_MEMSET:
3328 case BUILT_IN_STRCAT:
3329 case BUILT_IN_STRCHR:
3330 case BUILT_IN_STRCMP:
3331 case BUILT_IN_STRCPY:
3332 case BUILT_IN_STRCSPN:
3333 case BUILT_IN_STRLEN:
3334 case BUILT_IN_STRNCAT:
3335 case BUILT_IN_STRNCMP:
3336 case BUILT_IN_STRNCPY:
3337 case BUILT_IN_STRPBRK:
3338 case BUILT_IN_STRRCHR:
3339 case BUILT_IN_STRSPN:
3340 case BUILT_IN_STRSTR:
3341 return "<string.h>";
3342 case BUILT_IN_FPRINTF:
3343 case BUILT_IN_PUTC:
3344 case BUILT_IN_FPUTC:
3345 case BUILT_IN_FPUTS:
3346 case BUILT_IN_FSCANF:
3347 case BUILT_IN_FWRITE:
3348 case BUILT_IN_PRINTF:
3349 case BUILT_IN_PUTCHAR:
3350 case BUILT_IN_PUTS:
3351 case BUILT_IN_SCANF:
3352 case BUILT_IN_SNPRINTF:
3353 case BUILT_IN_SPRINTF:
3354 case BUILT_IN_SSCANF:
3355 case BUILT_IN_VFPRINTF:
3356 case BUILT_IN_VFSCANF:
3357 case BUILT_IN_VPRINTF:
3358 case BUILT_IN_VSCANF:
3359 case BUILT_IN_VSNPRINTF:
3360 case BUILT_IN_VSPRINTF:
3361 case BUILT_IN_VSSCANF:
3362 return "<stdio.h>";
3363 case BUILT_IN_ISALNUM:
3364 case BUILT_IN_ISALPHA:
3365 case BUILT_IN_ISBLANK:
3366 case BUILT_IN_ISCNTRL:
3367 case BUILT_IN_ISDIGIT:
3368 case BUILT_IN_ISGRAPH:
3369 case BUILT_IN_ISLOWER:
3370 case BUILT_IN_ISPRINT:
3371 case BUILT_IN_ISPUNCT:
3372 case BUILT_IN_ISSPACE:
3373 case BUILT_IN_ISUPPER:
3374 case BUILT_IN_ISXDIGIT:
3375 case BUILT_IN_TOLOWER:
3376 case BUILT_IN_TOUPPER:
3377 return "<ctype.h>";
3378 case BUILT_IN_ISWALNUM:
3379 case BUILT_IN_ISWALPHA:
3380 case BUILT_IN_ISWBLANK:
3381 case BUILT_IN_ISWCNTRL:
3382 case BUILT_IN_ISWDIGIT:
3383 case BUILT_IN_ISWGRAPH:
3384 case BUILT_IN_ISWLOWER:
3385 case BUILT_IN_ISWPRINT:
3386 case BUILT_IN_ISWPUNCT:
3387 case BUILT_IN_ISWSPACE:
3388 case BUILT_IN_ISWUPPER:
3389 case BUILT_IN_ISWXDIGIT:
3390 case BUILT_IN_TOWLOWER:
3391 case BUILT_IN_TOWUPPER:
3392 return "<wctype.h>";
3393 case BUILT_IN_ABORT:
3394 case BUILT_IN_ABS:
3395 case BUILT_IN_CALLOC:
3396 case BUILT_IN_EXIT:
3397 case BUILT_IN_FREE:
3398 case BUILT_IN_LABS:
3399 case BUILT_IN_LLABS:
3400 case BUILT_IN_MALLOC:
3401 case BUILT_IN_REALLOC:
3402 case BUILT_IN__EXIT2:
3403 case BUILT_IN_ALIGNED_ALLOC:
3404 return "<stdlib.h>";
3405 case BUILT_IN_IMAXABS:
3406 return "<inttypes.h>";
3407 case BUILT_IN_STRFTIME:
3408 return "<time.h>";
3409 default:
3410 return NULL;
3414 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3415 function of type int (). */
3417 tree
3418 implicitly_declare (location_t loc, tree functionid)
3420 struct c_binding *b;
3421 tree decl = NULL_TREE;
3422 tree asmspec_tree;
3424 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3426 if (B_IN_SCOPE (b, external_scope))
3428 decl = b->decl;
3429 break;
3433 if (decl)
3435 if (TREE_CODE (decl) != FUNCTION_DECL)
3436 return decl;
3438 /* FIXME: Objective-C has weird not-really-builtin functions
3439 which are supposed to be visible automatically. They wind up
3440 in the external scope because they're pushed before the file
3441 scope gets created. Catch this here and rebind them into the
3442 file scope. */
3443 if (!fndecl_built_in_p (decl) && DECL_IS_BUILTIN (decl))
3445 bind (functionid, decl, file_scope,
3446 /*invisible=*/false, /*nested=*/true,
3447 DECL_SOURCE_LOCATION (decl));
3448 return decl;
3450 else
3452 tree newtype = default_function_type;
3453 if (b->u.type)
3454 TREE_TYPE (decl) = b->u.type;
3455 /* Implicit declaration of a function already declared
3456 (somehow) in a different scope, or as a built-in.
3457 If this is the first time this has happened, warn;
3458 then recycle the old declaration but with the new type. */
3459 if (!C_DECL_IMPLICIT (decl))
3461 implicit_decl_warning (loc, functionid, decl);
3462 C_DECL_IMPLICIT (decl) = 1;
3464 if (fndecl_built_in_p (decl))
3466 newtype = build_type_attribute_variant (newtype,
3467 TYPE_ATTRIBUTES
3468 (TREE_TYPE (decl)));
3469 if (!comptypes (newtype, TREE_TYPE (decl)))
3471 bool warned = warning_at (loc, 0, "incompatible implicit "
3472 "declaration of built-in "
3473 "function %qD", decl);
3474 /* See if we can hint which header to include. */
3475 const char *header
3476 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3477 if (header != NULL && warned)
3479 rich_location richloc (line_table, loc);
3480 maybe_add_include_fixit (&richloc, header, true);
3481 inform (&richloc,
3482 "include %qs or provide a declaration of %qD",
3483 header, decl);
3485 newtype = TREE_TYPE (decl);
3488 else
3490 if (!comptypes (newtype, TREE_TYPE (decl)))
3492 auto_diagnostic_group d;
3493 error_at (loc, "incompatible implicit declaration of "
3494 "function %qD", decl);
3495 locate_old_decl (decl);
3498 b->u.type = TREE_TYPE (decl);
3499 TREE_TYPE (decl) = newtype;
3500 bind (functionid, decl, current_scope,
3501 /*invisible=*/false, /*nested=*/true,
3502 DECL_SOURCE_LOCATION (decl));
3503 return decl;
3507 /* Not seen before. */
3508 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3509 DECL_EXTERNAL (decl) = 1;
3510 TREE_PUBLIC (decl) = 1;
3511 C_DECL_IMPLICIT (decl) = 1;
3512 implicit_decl_warning (loc, functionid, 0);
3513 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3514 if (asmspec_tree)
3515 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3517 /* C89 says implicit declarations are in the innermost block.
3518 So we record the decl in the standard fashion. */
3519 decl = pushdecl (decl);
3521 /* No need to call objc_check_decl here - it's a function type. */
3522 rest_of_decl_compilation (decl, 0, 0);
3524 /* Write a record describing this implicit function declaration
3525 to the prototypes file (if requested). */
3526 gen_aux_info_record (decl, 0, 1, 0);
3528 /* Possibly apply some default attributes to this implicit declaration. */
3529 decl_attributes (&decl, NULL_TREE, 0);
3531 return decl;
3534 /* Issue an error message for a reference to an undeclared variable
3535 ID, including a reference to a builtin outside of function-call
3536 context. Establish a binding of the identifier to error_mark_node
3537 in an appropriate scope, which will suppress further errors for the
3538 same identifier. The error message should be given location LOC. */
3539 void
3540 undeclared_variable (location_t loc, tree id)
3542 static bool already = false;
3543 struct c_scope *scope;
3545 auto_diagnostic_group d;
3546 if (current_function_decl == NULL_TREE)
3548 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3549 if (const char *suggestion = guessed_id.suggestion ())
3551 gcc_rich_location richloc (loc);
3552 richloc.add_fixit_replace (suggestion);
3553 error_at (&richloc,
3554 "%qE undeclared here (not in a function);"
3555 " did you mean %qs?",
3556 id, suggestion);
3558 else
3559 error_at (loc, "%qE undeclared here (not in a function)", id);
3560 scope = current_scope;
3562 else
3564 if (!objc_diagnose_private_ivar (id))
3566 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3567 if (const char *suggestion = guessed_id.suggestion ())
3569 gcc_rich_location richloc (loc);
3570 richloc.add_fixit_replace (suggestion);
3571 error_at (&richloc,
3572 "%qE undeclared (first use in this function);"
3573 " did you mean %qs?",
3574 id, suggestion);
3576 else
3577 error_at (loc, "%qE undeclared (first use in this function)", id);
3579 if (!already)
3581 inform (loc, "each undeclared identifier is reported only"
3582 " once for each function it appears in");
3583 already = true;
3586 /* If we are parsing old-style parameter decls, current_function_decl
3587 will be nonnull but current_function_scope will be null. */
3588 scope = current_function_scope ? current_function_scope : current_scope;
3590 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3591 UNKNOWN_LOCATION);
3594 /* Subroutine of lookup_label, declare_label, define_label: construct a
3595 LABEL_DECL with all the proper frills. Also create a struct
3596 c_label_vars initialized for the current scope. */
3598 static tree
3599 make_label (location_t location, tree name, bool defining,
3600 struct c_label_vars **p_label_vars)
3602 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3603 DECL_CONTEXT (label) = current_function_decl;
3604 SET_DECL_MODE (label, VOIDmode);
3606 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3607 label_vars->shadowed = NULL;
3608 set_spot_bindings (&label_vars->label_bindings, defining);
3609 label_vars->decls_in_scope = make_tree_vector ();
3610 label_vars->gotos = NULL;
3611 *p_label_vars = label_vars;
3613 return label;
3616 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3617 Create one if none exists so far for the current function.
3618 This is called when a label is used in a goto expression or
3619 has its address taken. */
3621 tree
3622 lookup_label (tree name)
3624 tree label;
3625 struct c_label_vars *label_vars;
3627 if (current_function_scope == 0)
3629 error ("label %qE referenced outside of any function", name);
3630 return NULL_TREE;
3633 /* Use a label already defined or ref'd with this name, but not if
3634 it is inherited from a containing function and wasn't declared
3635 using __label__. */
3636 label = I_LABEL_DECL (name);
3637 if (label && (DECL_CONTEXT (label) == current_function_decl
3638 || C_DECLARED_LABEL_FLAG (label)))
3640 /* If the label has only been declared, update its apparent
3641 location to point here, for better diagnostics if it
3642 turns out not to have been defined. */
3643 if (DECL_INITIAL (label) == NULL_TREE)
3644 DECL_SOURCE_LOCATION (label) = input_location;
3645 return label;
3648 /* No label binding for that identifier; make one. */
3649 label = make_label (input_location, name, false, &label_vars);
3651 /* Ordinary labels go in the current function scope. */
3652 bind_label (name, label, current_function_scope, label_vars);
3654 return label;
3657 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3658 to LABEL. */
3660 static void
3661 warn_about_goto (location_t goto_loc, tree label, tree decl)
3663 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3664 error_at (goto_loc,
3665 "jump into scope of identifier with variably modified type");
3666 else
3667 warning_at (goto_loc, OPT_Wjump_misses_init,
3668 "jump skips variable initialization");
3669 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3670 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3673 /* Look up a label because of a goto statement. This is like
3674 lookup_label, but also issues any appropriate warnings. */
3676 tree
3677 lookup_label_for_goto (location_t loc, tree name)
3679 tree label;
3680 struct c_label_vars *label_vars;
3681 unsigned int ix;
3682 tree decl;
3684 label = lookup_label (name);
3685 if (label == NULL_TREE)
3686 return NULL_TREE;
3688 /* If we are jumping to a different function, we can't issue any
3689 useful warnings. */
3690 if (DECL_CONTEXT (label) != current_function_decl)
3692 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3693 return label;
3696 label_vars = I_LABEL_BINDING (name)->u.label;
3698 /* If the label has not yet been defined, then push this goto on a
3699 list for possible later warnings. */
3700 if (label_vars->label_bindings.scope == NULL)
3702 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3704 g->loc = loc;
3705 set_spot_bindings (&g->goto_bindings, true);
3706 vec_safe_push (label_vars->gotos, g);
3707 return label;
3710 /* If there are any decls in label_vars->decls_in_scope, then this
3711 goto has missed the declaration of the decl. This happens for a
3712 case like
3713 int i = 1;
3714 lab:
3716 goto lab;
3717 Issue a warning or error. */
3718 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3719 warn_about_goto (loc, label, decl);
3721 if (label_vars->label_bindings.left_stmt_expr)
3723 error_at (loc, "jump into statement expression");
3724 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3727 return label;
3730 /* Make a label named NAME in the current function, shadowing silently
3731 any that may be inherited from containing functions or containing
3732 scopes. This is called for __label__ declarations. */
3734 tree
3735 declare_label (tree name)
3737 struct c_binding *b = I_LABEL_BINDING (name);
3738 tree label;
3739 struct c_label_vars *label_vars;
3741 /* Check to make sure that the label hasn't already been declared
3742 at this scope */
3743 if (b && B_IN_CURRENT_SCOPE (b))
3745 auto_diagnostic_group d;
3746 error ("duplicate label declaration %qE", name);
3747 locate_old_decl (b->decl);
3749 /* Just use the previous declaration. */
3750 return b->decl;
3753 label = make_label (input_location, name, false, &label_vars);
3754 C_DECLARED_LABEL_FLAG (label) = 1;
3756 /* Declared labels go in the current scope. */
3757 bind_label (name, label, current_scope, label_vars);
3759 return label;
3762 /* When we define a label, issue any appropriate warnings if there are
3763 any gotos earlier in the function which jump to this label. */
3765 static void
3766 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3768 unsigned int ix;
3769 struct c_goto_bindings *g;
3771 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3773 struct c_binding *b;
3774 struct c_scope *scope;
3776 /* We have a goto to this label. The goto is going forward. In
3777 g->scope, the goto is going to skip any binding which was
3778 defined after g->bindings_in_scope. */
3779 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3781 for (b = g->goto_bindings.scope->bindings;
3782 b != g->goto_bindings.bindings_in_scope;
3783 b = b->prev)
3785 if (decl_jump_unsafe (b->decl))
3786 warn_about_goto (g->loc, label, b->decl);
3790 /* We also need to warn about decls defined in any scopes
3791 between the scope of the label and the scope of the goto. */
3792 for (scope = label_vars->label_bindings.scope;
3793 scope != g->goto_bindings.scope;
3794 scope = scope->outer)
3796 gcc_assert (scope != NULL);
3797 if (scope->has_jump_unsafe_decl)
3799 if (scope == label_vars->label_bindings.scope)
3800 b = label_vars->label_bindings.bindings_in_scope;
3801 else
3802 b = scope->bindings;
3803 for (; b != NULL; b = b->prev)
3805 if (decl_jump_unsafe (b->decl))
3806 warn_about_goto (g->loc, label, b->decl);
3811 if (g->goto_bindings.stmt_exprs > 0)
3813 error_at (g->loc, "jump into statement expression");
3814 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3815 label);
3819 /* Now that the label is defined, we will issue warnings about
3820 subsequent gotos to this label when we see them. */
3821 vec_safe_truncate (label_vars->gotos, 0);
3822 label_vars->gotos = NULL;
3825 /* Define a label, specifying the location in the source file.
3826 Return the LABEL_DECL node for the label, if the definition is valid.
3827 Otherwise return NULL_TREE. */
3829 tree
3830 define_label (location_t location, tree name)
3832 /* Find any preexisting label with this name. It is an error
3833 if that label has already been defined in this function, or
3834 if there is a containing function with a declared label with
3835 the same name. */
3836 tree label = I_LABEL_DECL (name);
3838 if (label
3839 && ((DECL_CONTEXT (label) == current_function_decl
3840 && DECL_INITIAL (label) != NULL_TREE)
3841 || (DECL_CONTEXT (label) != current_function_decl
3842 && C_DECLARED_LABEL_FLAG (label))))
3844 auto_diagnostic_group d;
3845 error_at (location, "duplicate label %qD", label);
3846 locate_old_decl (label);
3847 return NULL_TREE;
3849 else if (label && DECL_CONTEXT (label) == current_function_decl)
3851 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3853 /* The label has been used or declared already in this function,
3854 but not defined. Update its location to point to this
3855 definition. */
3856 DECL_SOURCE_LOCATION (label) = location;
3857 set_spot_bindings (&label_vars->label_bindings, true);
3859 /* Issue warnings as required about any goto statements from
3860 earlier in the function. */
3861 check_earlier_gotos (label, label_vars);
3863 else
3865 struct c_label_vars *label_vars;
3867 /* No label binding for that identifier; make one. */
3868 label = make_label (location, name, true, &label_vars);
3870 /* Ordinary labels go in the current function scope. */
3871 bind_label (name, label, current_function_scope, label_vars);
3874 if (!in_system_header_at (input_location) && lookup_name (name))
3875 warning_at (location, OPT_Wtraditional,
3876 "traditional C lacks a separate namespace "
3877 "for labels, identifier %qE conflicts", name);
3879 /* Mark label as having been defined. */
3880 DECL_INITIAL (label) = error_mark_node;
3881 return label;
3884 /* Get the bindings for a new switch statement. This is used to issue
3885 warnings as appropriate for jumps from the switch to case or
3886 default labels. */
3888 struct c_spot_bindings *
3889 c_get_switch_bindings (void)
3891 struct c_spot_bindings *switch_bindings;
3893 switch_bindings = XNEW (struct c_spot_bindings);
3894 set_spot_bindings (switch_bindings, true);
3895 return switch_bindings;
3898 void
3899 c_release_switch_bindings (struct c_spot_bindings *bindings)
3901 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3902 XDELETE (bindings);
3905 /* This is called at the point of a case or default label to issue
3906 warnings about decls as needed. It returns true if it found an
3907 error, not just a warning. */
3909 bool
3910 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3911 location_t switch_loc, location_t case_loc)
3913 bool saw_error;
3914 struct c_scope *scope;
3916 saw_error = false;
3917 for (scope = current_scope;
3918 scope != switch_bindings->scope;
3919 scope = scope->outer)
3921 struct c_binding *b;
3923 gcc_assert (scope != NULL);
3925 if (!scope->has_jump_unsafe_decl)
3926 continue;
3928 for (b = scope->bindings; b != NULL; b = b->prev)
3930 if (decl_jump_unsafe (b->decl))
3932 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3934 saw_error = true;
3935 error_at (case_loc,
3936 ("switch jumps into scope of identifier with "
3937 "variably modified type"));
3939 else
3940 warning_at (case_loc, OPT_Wjump_misses_init,
3941 "switch jumps over variable initialization");
3942 inform (switch_loc, "switch starts here");
3943 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3944 b->decl);
3949 if (switch_bindings->stmt_exprs > 0)
3951 saw_error = true;
3952 error_at (case_loc, "switch jumps into statement expression");
3953 inform (switch_loc, "switch starts here");
3956 return saw_error;
3959 /* Given NAME, an IDENTIFIER_NODE,
3960 return the structure (or union or enum) definition for that name.
3961 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3962 CODE says which kind of type the caller wants;
3963 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3964 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3965 location where the tag was defined.
3966 If the wrong kind of type is found, an error is reported. */
3968 static tree
3969 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3970 location_t *ploc)
3972 struct c_binding *b = I_TAG_BINDING (name);
3973 bool thislevel = false;
3975 if (!b || !b->decl)
3976 return NULL_TREE;
3978 /* We only care about whether it's in this level if
3979 thislevel_only was set or it might be a type clash. */
3980 if (thislevel_only || TREE_CODE (b->decl) != code)
3982 /* For our purposes, a tag in the external scope is the same as
3983 a tag in the file scope. (Primarily relevant to Objective-C
3984 and its builtin structure tags, which get pushed before the
3985 file scope is created.) */
3986 if (B_IN_CURRENT_SCOPE (b)
3987 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3988 thislevel = true;
3991 if (thislevel_only && !thislevel)
3992 return NULL_TREE;
3994 if (TREE_CODE (b->decl) != code)
3996 /* Definition isn't the kind we were looking for. */
3997 pending_invalid_xref = name;
3998 pending_invalid_xref_location = input_location;
4000 /* If in the same binding level as a declaration as a tag
4001 of a different type, this must not be allowed to
4002 shadow that tag, so give the error immediately.
4003 (For example, "struct foo; union foo;" is invalid.) */
4004 if (thislevel)
4005 pending_xref_error ();
4008 if (ploc != NULL)
4009 *ploc = b->locus;
4011 return b->decl;
4014 /* Return true if a definition exists for NAME with code CODE. */
4016 bool
4017 tag_exists_p (enum tree_code code, tree name)
4019 struct c_binding *b = I_TAG_BINDING (name);
4021 if (b == NULL || b->decl == NULL_TREE)
4022 return false;
4023 return TREE_CODE (b->decl) == code;
4026 /* Print an error message now
4027 for a recent invalid struct, union or enum cross reference.
4028 We don't print them immediately because they are not invalid
4029 when used in the `struct foo;' construct for shadowing. */
4031 void
4032 pending_xref_error (void)
4034 if (pending_invalid_xref != NULL_TREE)
4035 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4036 pending_invalid_xref);
4037 pending_invalid_xref = NULL_TREE;
4041 /* Look up NAME in the current scope and its superiors
4042 in the namespace of variables, functions and typedefs.
4043 Return a ..._DECL node of some kind representing its definition,
4044 or return NULL_TREE if it is undefined. */
4046 tree
4047 lookup_name (tree name)
4049 struct c_binding *b = I_SYMBOL_BINDING (name);
4050 if (b && !b->invisible)
4052 maybe_record_typedef_use (b->decl);
4053 return b->decl;
4055 return NULL_TREE;
4058 /* Similar to `lookup_name' but look only at the indicated scope. */
4060 static tree
4061 lookup_name_in_scope (tree name, struct c_scope *scope)
4063 struct c_binding *b;
4065 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4066 if (B_IN_SCOPE (b, scope))
4067 return b->decl;
4068 return NULL_TREE;
4071 /* Look for the closest match for NAME within the currently valid
4072 scopes.
4074 This finds the identifier with the lowest Levenshtein distance to
4075 NAME. If there are multiple candidates with equal minimal distance,
4076 the first one found is returned. Scopes are searched from innermost
4077 outwards, and within a scope in reverse order of declaration, thus
4078 benefiting candidates "near" to the current scope.
4080 The function also looks for similar macro names to NAME, since a
4081 misspelled macro name will not be expanded, and hence looks like an
4082 identifier to the C frontend.
4084 It also looks for start_typename keywords, to detect "singed" vs "signed"
4085 typos.
4087 Use LOC for any deferred diagnostics. */
4089 name_hint
4090 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4092 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4094 /* First, try some well-known names in the C standard library, in case
4095 the user forgot a #include. */
4096 const char *header_hint
4097 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4099 if (header_hint)
4100 return name_hint (NULL,
4101 new suggest_missing_header (loc,
4102 IDENTIFIER_POINTER (name),
4103 header_hint));
4105 /* Only suggest names reserved for the implementation if NAME begins
4106 with an underscore. */
4107 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4109 best_match<tree, tree> bm (name);
4111 /* Look within currently valid scopes. */
4112 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4113 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4115 if (!binding->id || binding->invisible)
4116 continue;
4117 if (binding->decl == error_mark_node)
4118 continue;
4119 /* Don't use bindings from implicitly declared functions,
4120 as they were likely misspellings themselves. */
4121 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4122 if (C_DECL_IMPLICIT (binding->decl))
4123 continue;
4124 /* Don't suggest names that are reserved for use by the
4125 implementation, unless NAME began with an underscore. */
4126 if (!consider_implementation_names)
4128 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4129 if (name_reserved_for_implementation_p (suggestion_str))
4130 continue;
4132 switch (kind)
4134 case FUZZY_LOOKUP_TYPENAME:
4135 if (TREE_CODE (binding->decl) != TYPE_DECL)
4136 continue;
4137 break;
4139 case FUZZY_LOOKUP_FUNCTION_NAME:
4140 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4142 /* Allow function pointers. */
4143 if ((VAR_P (binding->decl)
4144 || TREE_CODE (binding->decl) == PARM_DECL)
4145 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4146 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4147 == FUNCTION_TYPE))
4148 break;
4149 continue;
4151 break;
4153 default:
4154 break;
4156 bm.consider (binding->id);
4159 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4161 x = SOME_OTHER_MACRO (y);
4162 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4163 as a misspelled identifier.
4165 Use the best distance so far so that a candidate is only set if
4166 a macro is better than anything so far. This allows early rejection
4167 (without calculating the edit distance) of macro names that must have
4168 distance >= bm.get_best_distance (), and means that we only get a
4169 non-NULL result for best_macro_match if it's better than any of
4170 the identifiers already checked, which avoids needless creation
4171 of identifiers for macro hashnodes. */
4172 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4173 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4174 /* If a macro is the closest so far to NAME, use it, creating an
4175 identifier tree node for it. */
4176 if (best_macro)
4178 const char *id = (const char *)best_macro->ident.str;
4179 tree macro_as_identifier
4180 = get_identifier_with_length (id, best_macro->ident.len);
4181 bm.set_best_so_far (macro_as_identifier,
4182 bmm.get_best_distance (),
4183 bmm.get_best_candidate_length ());
4186 /* Try the "start_typename" keywords to detect
4187 "singed" vs "signed" typos. */
4188 if (kind == FUZZY_LOOKUP_TYPENAME)
4190 for (unsigned i = 0; i < num_c_common_reswords; i++)
4192 const c_common_resword *resword = &c_common_reswords[i];
4193 if (!c_keyword_starts_typename (resword->rid))
4194 continue;
4195 tree resword_identifier = ridpointers [resword->rid];
4196 if (!resword_identifier)
4197 continue;
4198 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4199 bm.consider (resword_identifier);
4203 tree best = bm.get_best_meaningful_candidate ();
4204 if (best)
4205 return name_hint (IDENTIFIER_POINTER (best), NULL);
4206 else
4207 return name_hint (NULL, NULL);
4211 /* Create the predefined scalar types of C,
4212 and some nodes representing standard constants (0, 1, (void *) 0).
4213 Initialize the global scope.
4214 Make definitions for built-in primitive functions. */
4216 void
4217 c_init_decl_processing (void)
4219 location_t save_loc = input_location;
4221 /* Initialize reserved words for parser. */
4222 c_parse_init ();
4224 current_function_decl = NULL_TREE;
4226 gcc_obstack_init (&parser_obstack);
4228 /* Make the externals scope. */
4229 push_scope ();
4230 external_scope = current_scope;
4232 /* Declarations from c_common_nodes_and_builtins must not be associated
4233 with this input file, lest we get differences between using and not
4234 using preprocessed headers. */
4235 input_location = BUILTINS_LOCATION;
4237 c_common_nodes_and_builtins ();
4239 /* In C, comparisons and TRUTH_* expressions have type int. */
4240 truthvalue_type_node = integer_type_node;
4241 truthvalue_true_node = integer_one_node;
4242 truthvalue_false_node = integer_zero_node;
4244 /* Even in C99, which has a real boolean type. */
4245 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4246 boolean_type_node));
4248 input_location = save_loc;
4250 make_fname_decl = c_make_fname_decl;
4251 start_fname_decls ();
4254 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4255 give the decl, NAME is the initialization string and TYPE_DEP
4256 indicates whether NAME depended on the type of the function. As we
4257 don't yet implement delayed emission of static data, we mark the
4258 decl as emitted so it is not placed in the output. Anything using
4259 it must therefore pull out the STRING_CST initializer directly.
4260 FIXME. */
4262 static tree
4263 c_make_fname_decl (location_t loc, tree id, int type_dep)
4265 const char *name = fname_as_string (type_dep);
4266 tree decl, type, init;
4267 size_t length = strlen (name);
4269 type = build_array_type (char_type_node,
4270 build_index_type (size_int (length)));
4271 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4273 decl = build_decl (loc, VAR_DECL, id, type);
4275 TREE_STATIC (decl) = 1;
4276 TREE_READONLY (decl) = 1;
4277 DECL_ARTIFICIAL (decl) = 1;
4279 init = build_string (length + 1, name);
4280 free (CONST_CAST (char *, name));
4281 TREE_TYPE (init) = type;
4282 DECL_INITIAL (decl) = init;
4284 TREE_USED (decl) = 1;
4286 if (current_function_decl
4287 /* For invalid programs like this:
4289 void foo()
4290 const char* p = __FUNCTION__;
4292 the __FUNCTION__ is believed to appear in K&R style function
4293 parameter declarator. In that case we still don't have
4294 function_scope. */
4295 && current_function_scope)
4297 DECL_CONTEXT (decl) = current_function_decl;
4298 bind (id, decl, current_function_scope,
4299 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4302 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4304 return decl;
4307 tree
4308 c_builtin_function (tree decl)
4310 tree type = TREE_TYPE (decl);
4311 tree id = DECL_NAME (decl);
4313 const char *name = IDENTIFIER_POINTER (id);
4314 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4316 /* Should never be called on a symbol with a preexisting meaning. */
4317 gcc_assert (!I_SYMBOL_BINDING (id));
4319 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4320 UNKNOWN_LOCATION);
4322 /* Builtins in the implementation namespace are made visible without
4323 needing to be explicitly declared. See push_file_scope. */
4324 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4326 DECL_CHAIN (decl) = visible_builtins;
4327 visible_builtins = decl;
4330 return decl;
4333 tree
4334 c_builtin_function_ext_scope (tree decl)
4336 tree type = TREE_TYPE (decl);
4337 tree id = DECL_NAME (decl);
4339 const char *name = IDENTIFIER_POINTER (id);
4340 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4342 if (external_scope)
4343 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4344 UNKNOWN_LOCATION);
4346 /* Builtins in the implementation namespace are made visible without
4347 needing to be explicitly declared. See push_file_scope. */
4348 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4350 DECL_CHAIN (decl) = visible_builtins;
4351 visible_builtins = decl;
4354 return decl;
4357 /* Called when a declaration is seen that contains no names to declare.
4358 If its type is a reference to a structure, union or enum inherited
4359 from a containing scope, shadow that tag name for the current scope
4360 with a forward reference.
4361 If its type defines a new named structure or union
4362 or defines an enum, it is valid but we need not do anything here.
4363 Otherwise, it is an error. */
4365 void
4366 shadow_tag (const struct c_declspecs *declspecs)
4368 shadow_tag_warned (declspecs, 0);
4371 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4372 but no pedwarn. */
4373 void
4374 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4376 bool found_tag = false;
4378 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4380 tree value = declspecs->type;
4381 enum tree_code code = TREE_CODE (value);
4383 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4384 /* Used to test also that TYPE_SIZE (value) != 0.
4385 That caused warning for `struct foo;' at top level in the file. */
4387 tree name = TYPE_NAME (value);
4388 tree t;
4390 found_tag = true;
4392 if (declspecs->restrict_p)
4394 error ("invalid use of %<restrict%>");
4395 warned = 1;
4398 if (name == NULL_TREE)
4400 if (warned != 1 && code != ENUMERAL_TYPE)
4401 /* Empty unnamed enum OK */
4403 pedwarn (input_location, 0,
4404 "unnamed struct/union that defines no instances");
4405 warned = 1;
4408 else if (declspecs->typespec_kind != ctsk_tagdef
4409 && declspecs->typespec_kind != ctsk_tagfirstref
4410 && declspecs->storage_class != csc_none)
4412 if (warned != 1)
4413 pedwarn (input_location, 0,
4414 "empty declaration with storage class specifier "
4415 "does not redeclare tag");
4416 warned = 1;
4417 pending_xref_error ();
4419 else if (declspecs->typespec_kind != ctsk_tagdef
4420 && declspecs->typespec_kind != ctsk_tagfirstref
4421 && (declspecs->const_p
4422 || declspecs->volatile_p
4423 || declspecs->atomic_p
4424 || declspecs->restrict_p
4425 || declspecs->address_space))
4427 if (warned != 1)
4428 pedwarn (input_location, 0,
4429 "empty declaration with type qualifier "
4430 "does not redeclare tag");
4431 warned = 1;
4432 pending_xref_error ();
4434 else if (declspecs->typespec_kind != ctsk_tagdef
4435 && declspecs->typespec_kind != ctsk_tagfirstref
4436 && declspecs->alignas_p)
4438 if (warned != 1)
4439 pedwarn (input_location, 0,
4440 "empty declaration with %<_Alignas%> "
4441 "does not redeclare tag");
4442 warned = 1;
4443 pending_xref_error ();
4445 else
4447 pending_invalid_xref = NULL_TREE;
4448 t = lookup_tag (code, name, true, NULL);
4450 if (t == NULL_TREE)
4452 t = make_node (code);
4453 pushtag (input_location, name, t);
4457 else
4459 if (warned != 1 && !in_system_header_at (input_location))
4461 pedwarn (input_location, 0,
4462 "useless type name in empty declaration");
4463 warned = 1;
4467 else if (warned != 1 && !in_system_header_at (input_location)
4468 && declspecs->typedef_p)
4470 pedwarn (input_location, 0, "useless type name in empty declaration");
4471 warned = 1;
4474 pending_invalid_xref = NULL_TREE;
4476 if (declspecs->inline_p)
4478 error ("%<inline%> in empty declaration");
4479 warned = 1;
4482 if (declspecs->noreturn_p)
4484 error ("%<_Noreturn%> in empty declaration");
4485 warned = 1;
4488 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4490 error ("%<auto%> in file-scope empty declaration");
4491 warned = 1;
4494 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4496 error ("%<register%> in file-scope empty declaration");
4497 warned = 1;
4500 if (!warned && !in_system_header_at (input_location)
4501 && declspecs->storage_class != csc_none)
4503 warning (0, "useless storage class specifier in empty declaration");
4504 warned = 2;
4507 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4509 warning (0, "useless %qs in empty declaration",
4510 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4511 warned = 2;
4514 if (!warned
4515 && !in_system_header_at (input_location)
4516 && (declspecs->const_p
4517 || declspecs->volatile_p
4518 || declspecs->atomic_p
4519 || declspecs->restrict_p
4520 || declspecs->address_space))
4522 warning (0, "useless type qualifier in empty declaration");
4523 warned = 2;
4526 if (!warned && !in_system_header_at (input_location)
4527 && declspecs->alignas_p)
4529 warning (0, "useless %<_Alignas%> in empty declaration");
4530 warned = 2;
4533 if (warned != 1)
4535 if (!found_tag)
4536 pedwarn (input_location, 0, "empty declaration");
4541 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4542 bits. SPECS represents declaration specifiers that the grammar
4543 only permits to contain type qualifiers and attributes. */
4546 quals_from_declspecs (const struct c_declspecs *specs)
4548 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4549 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4550 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4551 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4552 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4553 gcc_assert (!specs->type
4554 && !specs->decl_attr
4555 && specs->typespec_word == cts_none
4556 && specs->storage_class == csc_none
4557 && !specs->typedef_p
4558 && !specs->explicit_signed_p
4559 && !specs->deprecated_p
4560 && !specs->long_p
4561 && !specs->long_long_p
4562 && !specs->short_p
4563 && !specs->signed_p
4564 && !specs->unsigned_p
4565 && !specs->complex_p
4566 && !specs->inline_p
4567 && !specs->noreturn_p
4568 && !specs->thread_p);
4569 return quals;
4572 /* Construct an array declarator. LOC is the location of the
4573 beginning of the array (usually the opening brace). EXPR is the
4574 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4575 inside the [] (to be applied to the pointer to which a parameter
4576 array is converted). STATIC_P is true if "static" is inside the
4577 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4578 VLA of unspecified length which is nevertheless a complete type,
4579 false otherwise. The field for the contained declarator is left to
4580 be filled in by set_array_declarator_inner. */
4582 struct c_declarator *
4583 build_array_declarator (location_t loc,
4584 tree expr, struct c_declspecs *quals, bool static_p,
4585 bool vla_unspec_p)
4587 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4588 struct c_declarator);
4589 declarator->id_loc = loc;
4590 declarator->kind = cdk_array;
4591 declarator->declarator = 0;
4592 declarator->u.array.dimen = expr;
4593 if (quals)
4595 declarator->u.array.attrs = quals->attrs;
4596 declarator->u.array.quals = quals_from_declspecs (quals);
4598 else
4600 declarator->u.array.attrs = NULL_TREE;
4601 declarator->u.array.quals = 0;
4603 declarator->u.array.static_p = static_p;
4604 declarator->u.array.vla_unspec_p = vla_unspec_p;
4605 if (static_p || quals != NULL)
4606 pedwarn_c90 (loc, OPT_Wpedantic,
4607 "ISO C90 does not support %<static%> or type "
4608 "qualifiers in parameter array declarators");
4609 if (vla_unspec_p)
4610 pedwarn_c90 (loc, OPT_Wpedantic,
4611 "ISO C90 does not support %<[*]%> array declarators");
4612 if (vla_unspec_p)
4614 if (!current_scope->parm_flag)
4616 /* C99 6.7.5.2p4 */
4617 error_at (loc, "%<[*]%> not allowed in other than "
4618 "function prototype scope");
4619 declarator->u.array.vla_unspec_p = false;
4620 return NULL;
4622 current_scope->had_vla_unspec = true;
4624 return declarator;
4627 /* Set the contained declarator of an array declarator. DECL is the
4628 declarator, as constructed by build_array_declarator; INNER is what
4629 appears on the left of the []. */
4631 struct c_declarator *
4632 set_array_declarator_inner (struct c_declarator *decl,
4633 struct c_declarator *inner)
4635 decl->declarator = inner;
4636 return decl;
4639 /* INIT is a constructor that forms DECL's initializer. If the final
4640 element initializes a flexible array field, add the size of that
4641 initializer to DECL's size. */
4643 static void
4644 add_flexible_array_elts_to_size (tree decl, tree init)
4646 tree elt, type;
4648 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4649 return;
4651 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4652 type = TREE_TYPE (elt);
4653 if (TREE_CODE (type) == ARRAY_TYPE
4654 && TYPE_SIZE (type) == NULL_TREE
4655 && TYPE_DOMAIN (type) != NULL_TREE
4656 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4658 complete_array_type (&type, elt, false);
4659 DECL_SIZE (decl)
4660 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4661 DECL_SIZE_UNIT (decl)
4662 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4666 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4667 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4668 before the type name, and set *EXPR_CONST_OPERANDS, if
4669 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4670 appear in a constant expression. */
4672 tree
4673 groktypename (struct c_type_name *type_name, tree *expr,
4674 bool *expr_const_operands)
4676 tree type;
4677 tree attrs = type_name->specs->attrs;
4679 type_name->specs->attrs = NULL_TREE;
4681 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4682 false, NULL, &attrs, expr, expr_const_operands,
4683 DEPRECATED_NORMAL);
4685 /* Apply attributes. */
4686 decl_attributes (&type, attrs, 0);
4688 return type;
4691 /* Wrapper for decl_attributes that adds some implicit attributes
4692 to VAR_DECLs or FUNCTION_DECLs. */
4694 static tree
4695 c_decl_attributes (tree *node, tree attributes, int flags)
4697 /* Add implicit "omp declare target" attribute if requested. */
4698 if (current_omp_declare_target_attribute
4699 && ((VAR_P (*node) && is_global_var (*node))
4700 || TREE_CODE (*node) == FUNCTION_DECL))
4702 if (VAR_P (*node)
4703 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4704 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4705 NULL_TREE, attributes);
4706 else
4707 attributes = tree_cons (get_identifier ("omp declare target"),
4708 NULL_TREE, attributes);
4711 /* Look up the current declaration with all the attributes merged
4712 so far so that attributes on the current declaration that's
4713 about to be pushed that conflict with the former can be detected,
4714 diagnosed, and rejected as appropriate. */
4715 tree last_decl = lookup_name (DECL_NAME (*node));
4716 if (!last_decl)
4717 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4719 return decl_attributes (node, attributes, flags, last_decl);
4723 /* Decode a declarator in an ordinary declaration or data definition.
4724 This is called as soon as the type information and variable name
4725 have been parsed, before parsing the initializer if any.
4726 Here we create the ..._DECL node, fill in its type,
4727 and put it on the list of decls for the current context.
4728 The ..._DECL node is returned as the value.
4730 Exception: for arrays where the length is not specified,
4731 the type is left null, to be filled in by `finish_decl'.
4733 Function definitions do not come here; they go to start_function
4734 instead. However, external and forward declarations of functions
4735 do go through here. Structure field declarations are done by
4736 grokfield and not through here. */
4738 tree
4739 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4740 bool initialized, tree attributes)
4742 tree decl;
4743 tree tem;
4744 tree expr = NULL_TREE;
4745 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4747 /* An object declared as __attribute__((deprecated)) suppresses
4748 warnings of uses of other deprecated items. */
4749 if (lookup_attribute ("deprecated", attributes))
4750 deprecated_state = DEPRECATED_SUPPRESS;
4752 decl = grokdeclarator (declarator, declspecs,
4753 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4754 deprecated_state);
4755 if (!decl || decl == error_mark_node)
4756 return NULL_TREE;
4758 if (expr)
4759 add_stmt (fold_convert (void_type_node, expr));
4761 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
4762 && TREE_PUBLIC (decl))
4763 warning (OPT_Wmain, "%q+D is usually a function", decl);
4765 if (initialized)
4766 /* Is it valid for this decl to have an initializer at all?
4767 If not, set INITIALIZED to zero, which will indirectly
4768 tell 'finish_decl' to ignore the initializer once it is parsed. */
4769 switch (TREE_CODE (decl))
4771 case TYPE_DECL:
4772 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4773 initialized = false;
4774 break;
4776 case FUNCTION_DECL:
4777 error ("function %qD is initialized like a variable", decl);
4778 initialized = false;
4779 break;
4781 case PARM_DECL:
4782 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4783 error ("parameter %qD is initialized", decl);
4784 initialized = false;
4785 break;
4787 default:
4788 /* Don't allow initializations for incomplete types except for
4789 arrays which might be completed by the initialization. */
4791 /* This can happen if the array size is an undefined macro.
4792 We already gave a warning, so we don't need another one. */
4793 if (TREE_TYPE (decl) == error_mark_node)
4794 initialized = false;
4795 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4797 /* A complete type is ok if size is fixed. */
4799 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4800 || C_DECL_VARIABLE_SIZE (decl))
4802 error ("variable-sized object may not be initialized");
4803 initialized = false;
4806 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4808 error ("variable %qD has initializer but incomplete type", decl);
4809 initialized = false;
4811 else if (C_DECL_VARIABLE_SIZE (decl))
4813 /* Although C99 is unclear about whether incomplete arrays
4814 of VLAs themselves count as VLAs, it does not make
4815 sense to permit them to be initialized given that
4816 ordinary VLAs may not be initialized. */
4817 error ("variable-sized object may not be initialized");
4818 initialized = false;
4822 if (initialized)
4824 if (current_scope == file_scope)
4825 TREE_STATIC (decl) = 1;
4827 /* Tell 'pushdecl' this is an initialized decl
4828 even though we don't yet have the initializer expression.
4829 Also tell 'finish_decl' it may store the real initializer. */
4830 DECL_INITIAL (decl) = error_mark_node;
4833 /* If this is a function declaration, write a record describing it to the
4834 prototypes file (if requested). */
4836 if (TREE_CODE (decl) == FUNCTION_DECL)
4837 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4839 /* ANSI specifies that a tentative definition which is not merged with
4840 a non-tentative definition behaves exactly like a definition with an
4841 initializer equal to zero. (Section 3.7.2)
4843 -fno-common gives strict ANSI behavior, though this tends to break
4844 a large body of code that grew up without this rule.
4846 Thread-local variables are never common, since there's no entrenched
4847 body of code to break, and it allows more efficient variable references
4848 in the presence of dynamic linking. */
4850 if (VAR_P (decl)
4851 && !initialized
4852 && TREE_PUBLIC (decl)
4853 && !DECL_THREAD_LOCAL_P (decl)
4854 && !flag_no_common)
4855 DECL_COMMON (decl) = 1;
4857 /* Set attributes here so if duplicate decl, will have proper attributes. */
4858 c_decl_attributes (&decl, attributes, 0);
4860 /* Handle gnu_inline attribute. */
4861 if (declspecs->inline_p
4862 && !flag_gnu89_inline
4863 && TREE_CODE (decl) == FUNCTION_DECL
4864 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4865 || current_function_decl))
4867 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4869 else if (declspecs->storage_class != csc_static)
4870 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4873 if (TREE_CODE (decl) == FUNCTION_DECL
4874 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4876 struct c_declarator *ce = declarator;
4878 if (ce->kind == cdk_pointer)
4879 ce = declarator->declarator;
4880 if (ce->kind == cdk_function)
4882 tree args = ce->u.arg_info->parms;
4883 for (; args; args = DECL_CHAIN (args))
4885 tree type = TREE_TYPE (args);
4886 if (type && INTEGRAL_TYPE_P (type)
4887 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4888 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4893 if (TREE_CODE (decl) == FUNCTION_DECL
4894 && DECL_DECLARED_INLINE_P (decl)
4895 && DECL_UNINLINABLE (decl)
4896 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4897 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4898 decl);
4900 /* C99 6.7.4p3: An inline definition of a function with external
4901 linkage shall not contain a definition of a modifiable object
4902 with static storage duration... */
4903 if (VAR_P (decl)
4904 && current_scope != file_scope
4905 && TREE_STATIC (decl)
4906 && !TREE_READONLY (decl)
4907 && DECL_DECLARED_INLINE_P (current_function_decl)
4908 && DECL_EXTERNAL (current_function_decl))
4909 record_inline_static (input_location, current_function_decl,
4910 decl, csi_modifiable);
4912 if (c_dialect_objc ()
4913 && VAR_OR_FUNCTION_DECL_P (decl))
4914 objc_check_global_decl (decl);
4916 /* Add this decl to the current scope.
4917 TEM may equal DECL or it may be a previous decl of the same name. */
4918 tem = pushdecl (decl);
4920 if (initialized && DECL_EXTERNAL (tem))
4922 DECL_EXTERNAL (tem) = 0;
4923 TREE_STATIC (tem) = 1;
4926 return tem;
4929 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4930 DECL or the non-array element type if DECL is an uninitialized array.
4931 If that type has a const member, diagnose this. */
4933 static void
4934 diagnose_uninitialized_cst_member (tree decl, tree type)
4936 tree field;
4937 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4939 tree field_type;
4940 if (TREE_CODE (field) != FIELD_DECL)
4941 continue;
4942 field_type = strip_array_types (TREE_TYPE (field));
4944 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4946 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4947 "uninitialized const member in %qT is invalid in C++",
4948 strip_array_types (TREE_TYPE (decl)));
4949 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4952 if (RECORD_OR_UNION_TYPE_P (field_type))
4953 diagnose_uninitialized_cst_member (decl, field_type);
4957 /* Finish processing of a declaration;
4958 install its initial value.
4959 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4960 If the length of an array type is not known before,
4961 it must be determined now, from the initial value, or it is an error.
4963 INIT_LOC is the location of the initial value. */
4965 void
4966 finish_decl (tree decl, location_t init_loc, tree init,
4967 tree origtype, tree asmspec_tree)
4969 tree type;
4970 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4971 const char *asmspec = 0;
4973 /* If a name was specified, get the string. */
4974 if (VAR_OR_FUNCTION_DECL_P (decl)
4975 && DECL_FILE_SCOPE_P (decl))
4976 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4977 if (asmspec_tree)
4978 asmspec = TREE_STRING_POINTER (asmspec_tree);
4980 if (VAR_P (decl)
4981 && TREE_STATIC (decl)
4982 && global_bindings_p ())
4983 /* So decl is a global variable. Record the types it uses
4984 so that we can decide later to emit debug info for them. */
4985 record_types_used_by_current_var_decl (decl);
4987 /* If `start_decl' didn't like having an initialization, ignore it now. */
4988 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4989 init = NULL_TREE;
4991 /* Don't crash if parm is initialized. */
4992 if (TREE_CODE (decl) == PARM_DECL)
4993 init = NULL_TREE;
4995 if (init)
4996 store_init_value (init_loc, decl, init, origtype);
4998 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4999 || TREE_CODE (decl) == FIELD_DECL))
5000 objc_check_decl (decl);
5002 type = TREE_TYPE (decl);
5004 /* Deduce size of array from initialization, if not already known. */
5005 if (TREE_CODE (type) == ARRAY_TYPE
5006 && TYPE_DOMAIN (type) == NULL_TREE
5007 && TREE_CODE (decl) != TYPE_DECL)
5009 bool do_default
5010 = (TREE_STATIC (decl)
5011 /* Even if pedantic, an external linkage array
5012 may have incomplete type at first. */
5013 ? pedantic && !TREE_PUBLIC (decl)
5014 : !DECL_EXTERNAL (decl));
5015 int failure
5016 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5017 do_default);
5019 /* Get the completed type made by complete_array_type. */
5020 type = TREE_TYPE (decl);
5022 switch (failure)
5024 case 1:
5025 error ("initializer fails to determine size of %q+D", decl);
5026 break;
5028 case 2:
5029 if (do_default)
5030 error ("array size missing in %q+D", decl);
5031 break;
5033 case 3:
5034 error ("zero or negative size array %q+D", decl);
5035 break;
5037 case 0:
5038 /* For global variables, update the copy of the type that
5039 exists in the binding. */
5040 if (TREE_PUBLIC (decl))
5042 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5043 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5044 b_ext = b_ext->shadowed;
5045 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5047 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5048 b_ext->u.type = composite_type (b_ext->u.type, type);
5049 else
5050 b_ext->u.type = type;
5053 break;
5055 default:
5056 gcc_unreachable ();
5059 if (DECL_INITIAL (decl))
5060 TREE_TYPE (DECL_INITIAL (decl)) = type;
5062 relayout_decl (decl);
5065 if (TREE_CODE (type) == ARRAY_TYPE
5066 && TYPE_STRING_FLAG (TREE_TYPE (type))
5067 && DECL_INITIAL (decl)
5068 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR)
5069 DECL_INITIAL (decl) = braced_list_to_string (type, DECL_INITIAL (decl));
5071 if (VAR_P (decl))
5073 if (init && TREE_CODE (init) == CONSTRUCTOR)
5074 add_flexible_array_elts_to_size (decl, init);
5076 complete_flexible_array_elts (DECL_INITIAL (decl));
5078 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5079 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5080 layout_decl (decl, 0);
5082 if (DECL_SIZE (decl) == NULL_TREE
5083 /* Don't give an error if we already gave one earlier. */
5084 && TREE_TYPE (decl) != error_mark_node
5085 && (TREE_STATIC (decl)
5086 /* A static variable with an incomplete type
5087 is an error if it is initialized.
5088 Also if it is not file scope.
5089 Otherwise, let it through, but if it is not `extern'
5090 then it may cause an error message later. */
5091 ? (DECL_INITIAL (decl) != NULL_TREE
5092 || !DECL_FILE_SCOPE_P (decl))
5093 /* An automatic variable with an incomplete type
5094 is an error. */
5095 : !DECL_EXTERNAL (decl)))
5097 error ("storage size of %q+D isn%'t known", decl);
5098 TREE_TYPE (decl) = error_mark_node;
5101 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5102 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5103 && DECL_SIZE (decl) == NULL_TREE
5104 && TREE_STATIC (decl))
5105 incomplete_record_decls.safe_push (decl);
5107 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5109 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5110 constant_expression_warning (DECL_SIZE (decl));
5111 else
5113 error ("storage size of %q+D isn%'t constant", decl);
5114 TREE_TYPE (decl) = error_mark_node;
5118 if (TREE_USED (type))
5120 TREE_USED (decl) = 1;
5121 DECL_READ_P (decl) = 1;
5125 /* If this is a function and an assembler name is specified, reset DECL_RTL
5126 so we can give it its new name. Also, update builtin_decl if it
5127 was a normal built-in. */
5128 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5130 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5131 set_builtin_user_assembler_name (decl, asmspec);
5132 set_user_assembler_name (decl, asmspec);
5135 /* If #pragma weak was used, mark the decl weak now. */
5136 maybe_apply_pragma_weak (decl);
5138 /* Output the assembler code and/or RTL code for variables and functions,
5139 unless the type is an undefined structure or union.
5140 If not, it will get done when the type is completed. */
5142 if (VAR_OR_FUNCTION_DECL_P (decl))
5144 /* Determine the ELF visibility. */
5145 if (TREE_PUBLIC (decl))
5146 c_determine_visibility (decl);
5148 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5149 if (c_dialect_objc ())
5150 objc_check_decl (decl);
5152 if (asmspec)
5154 /* If this is not a static variable, issue a warning.
5155 It doesn't make any sense to give an ASMSPEC for an
5156 ordinary, non-register local variable. Historically,
5157 GCC has accepted -- but ignored -- the ASMSPEC in
5158 this case. */
5159 if (!DECL_FILE_SCOPE_P (decl)
5160 && VAR_P (decl)
5161 && !C_DECL_REGISTER (decl)
5162 && !TREE_STATIC (decl))
5163 warning (0, "ignoring asm-specifier for non-static local "
5164 "variable %q+D", decl);
5165 else
5166 set_user_assembler_name (decl, asmspec);
5169 if (DECL_FILE_SCOPE_P (decl))
5171 if (DECL_INITIAL (decl) == NULL_TREE
5172 || DECL_INITIAL (decl) == error_mark_node)
5173 /* Don't output anything
5174 when a tentative file-scope definition is seen.
5175 But at end of compilation, do output code for them. */
5176 DECL_DEFER_OUTPUT (decl) = 1;
5177 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5178 DECL_HARD_REGISTER (decl) = 1;
5179 rest_of_decl_compilation (decl, true, 0);
5181 else
5183 /* In conjunction with an ASMSPEC, the `register'
5184 keyword indicates that we should place the variable
5185 in a particular register. */
5186 if (asmspec && C_DECL_REGISTER (decl))
5188 DECL_HARD_REGISTER (decl) = 1;
5189 /* This cannot be done for a structure with volatile
5190 fields, on which DECL_REGISTER will have been
5191 reset. */
5192 if (!DECL_REGISTER (decl))
5193 error ("cannot put object with volatile field into register");
5196 if (TREE_CODE (decl) != FUNCTION_DECL)
5198 /* If we're building a variable sized type, and we might be
5199 reachable other than via the top of the current binding
5200 level, then create a new BIND_EXPR so that we deallocate
5201 the object at the right time. */
5202 /* Note that DECL_SIZE can be null due to errors. */
5203 if (DECL_SIZE (decl)
5204 && !TREE_CONSTANT (DECL_SIZE (decl))
5205 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5207 tree bind;
5208 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5209 TREE_SIDE_EFFECTS (bind) = 1;
5210 add_stmt (bind);
5211 BIND_EXPR_BODY (bind) = push_stmt_list ();
5213 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5214 DECL_EXPR, decl));
5219 if (!DECL_FILE_SCOPE_P (decl))
5221 /* Recompute the RTL of a local array now
5222 if it used to be an incomplete type. */
5223 if (was_incomplete && !is_global_var (decl))
5225 /* If we used it already as memory, it must stay in memory. */
5226 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5227 /* If it's still incomplete now, no init will save it. */
5228 if (DECL_SIZE (decl) == NULL_TREE)
5229 DECL_INITIAL (decl) = NULL_TREE;
5234 if (TREE_CODE (decl) == TYPE_DECL)
5236 if (!DECL_FILE_SCOPE_P (decl)
5237 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5238 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5240 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5243 /* Install a cleanup (aka destructor) if one was given. */
5244 if (VAR_P (decl) && !TREE_STATIC (decl))
5246 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5247 if (attr)
5249 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5250 tree cleanup_decl = lookup_name (cleanup_id);
5251 tree cleanup;
5252 vec<tree, va_gc> *v;
5254 /* Build "cleanup(&decl)" for the destructor. */
5255 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5256 vec_alloc (v, 1);
5257 v->quick_push (cleanup);
5258 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5259 vNULL, cleanup_decl, v, NULL);
5260 vec_free (v);
5262 /* Don't warn about decl unused; the cleanup uses it. */
5263 TREE_USED (decl) = 1;
5264 TREE_USED (cleanup_decl) = 1;
5265 DECL_READ_P (decl) = 1;
5267 push_cleanup (decl, cleanup, false);
5271 if (warn_cxx_compat
5272 && VAR_P (decl)
5273 && !DECL_EXTERNAL (decl)
5274 && DECL_INITIAL (decl) == NULL_TREE)
5276 type = strip_array_types (type);
5277 if (TREE_READONLY (decl))
5278 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5279 "uninitialized const %qD is invalid in C++", decl);
5280 else if (RECORD_OR_UNION_TYPE_P (type)
5281 && C_TYPE_FIELDS_READONLY (type))
5282 diagnose_uninitialized_cst_member (decl, type);
5285 if (flag_openmp
5286 && VAR_P (decl)
5287 && lookup_attribute ("omp declare target implicit",
5288 DECL_ATTRIBUTES (decl)))
5290 DECL_ATTRIBUTES (decl)
5291 = remove_attribute ("omp declare target implicit",
5292 DECL_ATTRIBUTES (decl));
5293 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5294 error ("%q+D in declare target directive does not have mappable type",
5295 decl);
5296 else if (!lookup_attribute ("omp declare target",
5297 DECL_ATTRIBUTES (decl))
5298 && !lookup_attribute ("omp declare target link",
5299 DECL_ATTRIBUTES (decl)))
5300 DECL_ATTRIBUTES (decl)
5301 = tree_cons (get_identifier ("omp declare target"),
5302 NULL_TREE, DECL_ATTRIBUTES (decl));
5305 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5308 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5309 EXPR is NULL or a pointer to an expression that needs to be
5310 evaluated for the side effects of array size expressions in the
5311 parameters. */
5313 tree
5314 grokparm (const struct c_parm *parm, tree *expr)
5316 tree attrs = parm->attrs;
5317 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5318 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5320 decl_attributes (&decl, attrs, 0);
5322 return decl;
5325 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5326 and push that on the current scope. EXPR is a pointer to an
5327 expression that needs to be evaluated for the side effects of array
5328 size expressions in the parameters. */
5330 void
5331 push_parm_decl (const struct c_parm *parm, tree *expr)
5333 tree attrs = parm->attrs;
5334 tree decl;
5336 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5337 &attrs, expr, NULL, DEPRECATED_NORMAL);
5338 if (decl && DECL_P (decl))
5339 DECL_SOURCE_LOCATION (decl) = parm->loc;
5340 decl_attributes (&decl, attrs, 0);
5342 decl = pushdecl (decl);
5344 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5347 /* Mark all the parameter declarations to date as forward decls.
5348 Also diagnose use of this extension. */
5350 void
5351 mark_forward_parm_decls (void)
5353 struct c_binding *b;
5355 if (pedantic && !current_scope->warned_forward_parm_decls)
5357 pedwarn (input_location, OPT_Wpedantic,
5358 "ISO C forbids forward parameter declarations");
5359 current_scope->warned_forward_parm_decls = true;
5362 for (b = current_scope->bindings; b; b = b->prev)
5363 if (TREE_CODE (b->decl) == PARM_DECL)
5364 TREE_ASM_WRITTEN (b->decl) = 1;
5367 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5368 literal, which may be an incomplete array type completed by the
5369 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5370 literal. NON_CONST is true if the initializers contain something
5371 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5372 it is the (valid) alignment for this compound literal, as specified
5373 with _Alignas. */
5375 tree
5376 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5377 unsigned int alignas_align)
5379 /* We do not use start_decl here because we have a type, not a declarator;
5380 and do not use finish_decl because the decl should be stored inside
5381 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5382 tree decl;
5383 tree complit;
5384 tree stmt;
5386 if (type == error_mark_node
5387 || init == error_mark_node)
5388 return error_mark_node;
5390 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5391 DECL_EXTERNAL (decl) = 0;
5392 TREE_PUBLIC (decl) = 0;
5393 TREE_STATIC (decl) = (current_scope == file_scope);
5394 DECL_CONTEXT (decl) = current_function_decl;
5395 TREE_USED (decl) = 1;
5396 DECL_READ_P (decl) = 1;
5397 DECL_ARTIFICIAL (decl) = 1;
5398 DECL_IGNORED_P (decl) = 1;
5399 TREE_TYPE (decl) = type;
5400 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5401 if (alignas_align)
5403 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5404 DECL_USER_ALIGN (decl) = 1;
5406 store_init_value (loc, decl, init, NULL_TREE);
5408 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5410 int failure = complete_array_type (&TREE_TYPE (decl),
5411 DECL_INITIAL (decl), true);
5412 /* If complete_array_type returns 3, it means that the
5413 initial value of the compound literal is empty. Allow it. */
5414 gcc_assert (failure == 0 || failure == 3);
5416 type = TREE_TYPE (decl);
5417 TREE_TYPE (DECL_INITIAL (decl)) = type;
5420 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5422 c_incomplete_type_error (loc, NULL_TREE, type);
5423 return error_mark_node;
5426 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5427 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5428 TREE_SIDE_EFFECTS (complit) = 1;
5430 layout_decl (decl, 0);
5432 if (TREE_STATIC (decl))
5434 /* This decl needs a name for the assembler output. */
5435 set_compound_literal_name (decl);
5436 DECL_DEFER_OUTPUT (decl) = 1;
5437 DECL_COMDAT (decl) = 1;
5438 pushdecl (decl);
5439 rest_of_decl_compilation (decl, 1, 0);
5441 else
5442 pushdecl (decl);
5444 if (non_const)
5446 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5447 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5450 return complit;
5453 /* Check the type of a compound literal. Here we just check that it
5454 is valid for C++. */
5456 void
5457 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5459 if (warn_cxx_compat
5460 && (type_name->specs->typespec_kind == ctsk_tagdef
5461 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5462 warning_at (loc, OPT_Wc___compat,
5463 "defining a type in a compound literal is invalid in C++");
5466 /* Determine whether TYPE is a structure with a flexible array member,
5467 or a union containing such a structure (possibly recursively). */
5469 static bool
5470 flexible_array_type_p (tree type)
5472 tree x;
5473 switch (TREE_CODE (type))
5475 case RECORD_TYPE:
5476 x = TYPE_FIELDS (type);
5477 if (x == NULL_TREE)
5478 return false;
5479 while (DECL_CHAIN (x) != NULL_TREE)
5480 x = DECL_CHAIN (x);
5481 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5482 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5483 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5484 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5485 return true;
5486 return false;
5487 case UNION_TYPE:
5488 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5490 if (flexible_array_type_p (TREE_TYPE (x)))
5491 return true;
5493 return false;
5494 default:
5495 return false;
5499 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5500 replacing with appropriate values if they are invalid. */
5502 static void
5503 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5504 tree orig_name)
5506 tree type_mv;
5507 unsigned int max_width;
5508 unsigned HOST_WIDE_INT w;
5509 const char *name = (orig_name
5510 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5511 : _("<anonymous>"));
5513 /* Detect and ignore out of range field width and process valid
5514 field widths. */
5515 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5517 error_at (loc, "bit-field %qs width not an integer constant", name);
5518 *width = integer_one_node;
5520 else
5522 if (TREE_CODE (*width) != INTEGER_CST)
5524 *width = c_fully_fold (*width, false, NULL);
5525 if (TREE_CODE (*width) == INTEGER_CST)
5526 pedwarn (loc, OPT_Wpedantic,
5527 "bit-field %qs width not an integer constant expression",
5528 name);
5530 if (TREE_CODE (*width) != INTEGER_CST)
5532 error_at (loc, "bit-field %qs width not an integer constant", name);
5533 *width = integer_one_node;
5535 constant_expression_warning (*width);
5536 if (tree_int_cst_sgn (*width) < 0)
5538 error_at (loc, "negative width in bit-field %qs", name);
5539 *width = integer_one_node;
5541 else if (integer_zerop (*width) && orig_name)
5543 error_at (loc, "zero width for bit-field %qs", name);
5544 *width = integer_one_node;
5548 /* Detect invalid bit-field type. */
5549 if (TREE_CODE (*type) != INTEGER_TYPE
5550 && TREE_CODE (*type) != BOOLEAN_TYPE
5551 && TREE_CODE (*type) != ENUMERAL_TYPE)
5553 error_at (loc, "bit-field %qs has invalid type", name);
5554 *type = unsigned_type_node;
5557 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5559 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5560 name);
5561 *type = unsigned_type_node;
5564 type_mv = TYPE_MAIN_VARIANT (*type);
5565 if (!in_system_header_at (input_location)
5566 && type_mv != integer_type_node
5567 && type_mv != unsigned_type_node
5568 && type_mv != boolean_type_node)
5569 pedwarn_c90 (loc, OPT_Wpedantic,
5570 "type of bit-field %qs is a GCC extension", name);
5572 max_width = TYPE_PRECISION (*type);
5574 if (compare_tree_int (*width, max_width) > 0)
5576 error_at (loc, "width of %qs exceeds its type", name);
5577 w = max_width;
5578 *width = build_int_cst (integer_type_node, w);
5580 else
5581 w = tree_to_uhwi (*width);
5583 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5585 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5586 if (!lt
5587 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5588 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5589 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5595 /* Print warning about variable length array if necessary. */
5597 static void
5598 warn_variable_length_array (tree name, tree size)
5600 if (TREE_CONSTANT (size))
5602 if (name)
5603 pedwarn_c90 (input_location, OPT_Wvla,
5604 "ISO C90 forbids array %qE whose size "
5605 "can%'t be evaluated", name);
5606 else
5607 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5608 "whose size can%'t be evaluated");
5610 else
5612 if (name)
5613 pedwarn_c90 (input_location, OPT_Wvla,
5614 "ISO C90 forbids variable length array %qE", name);
5615 else
5616 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5617 "length array");
5621 /* Print warning about defaulting to int if necessary. */
5623 static void
5624 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5626 diagnostic_info diagnostic;
5627 va_list ap;
5628 rich_location richloc (line_table, location);
5630 va_start (ap, gmsgid);
5631 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5632 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5633 diagnostic.option_index = opt;
5634 diagnostic_report_diagnostic (global_dc, &diagnostic);
5635 va_end (ap);
5638 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5639 considering only those c_declspec_words found in LIST, which
5640 must be terminated by cdw_number_of_elements. */
5642 static location_t
5643 smallest_type_quals_location (const location_t *locations,
5644 const c_declspec_word *list)
5646 location_t loc = UNKNOWN_LOCATION;
5647 while (*list != cdw_number_of_elements)
5649 location_t newloc = locations[*list];
5650 if (loc == UNKNOWN_LOCATION
5651 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5652 loc = newloc;
5653 list++;
5656 return loc;
5659 /* Given declspecs and a declarator,
5660 determine the name and type of the object declared
5661 and construct a ..._DECL node for it.
5662 (In one case we can return a ..._TYPE node instead.
5663 For invalid input we sometimes return NULL_TREE.)
5665 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5667 DECL_CONTEXT says which syntactic context this declaration is in:
5668 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5669 FUNCDEF for a function definition. Like NORMAL but a few different
5670 error messages in each case. Return value may be zero meaning
5671 this definition is too screwy to try to parse.
5672 PARM for a parameter declaration (either within a function prototype
5673 or before a function body). Make a PARM_DECL, or return void_type_node.
5674 TYPENAME if for a typename (in a cast or sizeof).
5675 Don't make a DECL node; just return the ..._TYPE node.
5676 FIELD for a struct or union field; make a FIELD_DECL.
5677 INITIALIZED is true if the decl has an initializer.
5678 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5679 representing the width of the bit-field.
5680 DECL_ATTRS points to the list of attributes that should be added to this
5681 decl. Any nested attributes that belong on the decl itself will be
5682 added to this list.
5683 If EXPR is not NULL, any expressions that need to be evaluated as
5684 part of evaluating variably modified types will be stored in *EXPR.
5685 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5686 set to indicate whether operands in *EXPR can be used in constant
5687 expressions.
5688 DEPRECATED_STATE is a deprecated_states value indicating whether
5689 deprecation warnings should be suppressed.
5691 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5692 It may also be so in the PARM case, for a prototype where the
5693 argument type is specified but not the name.
5695 This function is where the complicated C meanings of `static'
5696 and `extern' are interpreted. */
5698 static tree
5699 grokdeclarator (const struct c_declarator *declarator,
5700 struct c_declspecs *declspecs,
5701 enum decl_context decl_context, bool initialized, tree *width,
5702 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5703 enum deprecated_states deprecated_state)
5705 tree type = declspecs->type;
5706 bool threadp = declspecs->thread_p;
5707 enum c_storage_class storage_class = declspecs->storage_class;
5708 int constp;
5709 int restrictp;
5710 int volatilep;
5711 int atomicp;
5712 int type_quals = TYPE_UNQUALIFIED;
5713 tree name = NULL_TREE;
5714 bool funcdef_flag = false;
5715 bool funcdef_syntax = false;
5716 bool size_varies = false;
5717 tree decl_attr = declspecs->decl_attr;
5718 int array_ptr_quals = TYPE_UNQUALIFIED;
5719 tree array_ptr_attrs = NULL_TREE;
5720 bool array_parm_static = false;
5721 bool array_parm_vla_unspec_p = false;
5722 tree returned_attrs = NULL_TREE;
5723 bool bitfield = width != NULL;
5724 tree element_type;
5725 tree orig_qual_type = NULL;
5726 size_t orig_qual_indirect = 0;
5727 struct c_arg_info *arg_info = 0;
5728 addr_space_t as1, as2, address_space;
5729 location_t loc = UNKNOWN_LOCATION;
5730 tree expr_dummy;
5731 bool expr_const_operands_dummy;
5732 enum c_declarator_kind first_non_attr_kind;
5733 unsigned int alignas_align = 0;
5735 if (TREE_CODE (type) == ERROR_MARK)
5736 return error_mark_node;
5737 if (expr == NULL)
5739 expr = &expr_dummy;
5740 expr_dummy = NULL_TREE;
5742 if (expr_const_operands == NULL)
5743 expr_const_operands = &expr_const_operands_dummy;
5745 if (declspecs->expr)
5747 if (*expr)
5748 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5749 declspecs->expr);
5750 else
5751 *expr = declspecs->expr;
5753 *expr_const_operands = declspecs->expr_const_operands;
5755 if (decl_context == FUNCDEF)
5756 funcdef_flag = true, decl_context = NORMAL;
5758 /* Look inside a declarator for the name being declared
5759 and get it as an IDENTIFIER_NODE, for an error message. */
5761 const struct c_declarator *decl = declarator;
5763 first_non_attr_kind = cdk_attrs;
5764 while (decl)
5765 switch (decl->kind)
5767 case cdk_array:
5768 loc = decl->id_loc;
5769 /* FALL THRU. */
5771 case cdk_function:
5772 case cdk_pointer:
5773 funcdef_syntax = (decl->kind == cdk_function);
5774 if (first_non_attr_kind == cdk_attrs)
5775 first_non_attr_kind = decl->kind;
5776 decl = decl->declarator;
5777 break;
5779 case cdk_attrs:
5780 decl = decl->declarator;
5781 break;
5783 case cdk_id:
5784 loc = decl->id_loc;
5785 if (decl->u.id)
5786 name = decl->u.id;
5787 if (first_non_attr_kind == cdk_attrs)
5788 first_non_attr_kind = decl->kind;
5789 decl = 0;
5790 break;
5792 default:
5793 gcc_unreachable ();
5795 if (name == NULL_TREE)
5797 gcc_assert (decl_context == PARM
5798 || decl_context == TYPENAME
5799 || (decl_context == FIELD
5800 && declarator->kind == cdk_id));
5801 gcc_assert (!initialized);
5805 /* A function definition's declarator must have the form of
5806 a function declarator. */
5808 if (funcdef_flag && !funcdef_syntax)
5809 return NULL_TREE;
5811 /* If this looks like a function definition, make it one,
5812 even if it occurs where parms are expected.
5813 Then store_parm_decls will reject it and not use it as a parm. */
5814 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5815 decl_context = PARM;
5817 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5818 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5820 if ((decl_context == NORMAL || decl_context == FIELD)
5821 && current_scope == file_scope
5822 && variably_modified_type_p (type, NULL_TREE))
5824 if (name)
5825 error_at (loc, "variably modified %qE at file scope", name);
5826 else
5827 error_at (loc, "variably modified field at file scope");
5828 type = integer_type_node;
5831 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5833 /* Diagnose defaulting to "int". */
5835 if (declspecs->default_int_p && !in_system_header_at (input_location))
5837 /* Issue a warning if this is an ISO C 99 program or if
5838 -Wreturn-type and this is a function, or if -Wimplicit;
5839 prefer the former warning since it is more explicit. */
5840 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
5841 && funcdef_flag)
5842 warn_about_return_type = 1;
5843 else
5845 if (name)
5846 warn_defaults_to (loc, OPT_Wimplicit_int,
5847 "type defaults to %<int%> in declaration "
5848 "of %qE", name);
5849 else
5850 warn_defaults_to (loc, OPT_Wimplicit_int,
5851 "type defaults to %<int%> in type name");
5855 /* Adjust the type if a bit-field is being declared,
5856 -funsigned-bitfields applied and the type is not explicitly
5857 "signed". */
5858 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5859 && TREE_CODE (type) == INTEGER_TYPE)
5860 type = unsigned_type_for (type);
5862 /* Figure out the type qualifiers for the declaration. There are
5863 two ways a declaration can become qualified. One is something
5864 like `const int i' where the `const' is explicit. Another is
5865 something like `typedef const int CI; CI i' where the type of the
5866 declaration contains the `const'. A third possibility is that
5867 there is a type qualifier on the element type of a typedefed
5868 array type, in which case we should extract that qualifier so
5869 that c_apply_type_quals_to_decl receives the full list of
5870 qualifiers to work with (C90 is not entirely clear about whether
5871 duplicate qualifiers should be diagnosed in this case, but it
5872 seems most appropriate to do so). */
5873 element_type = strip_array_types (type);
5874 constp = declspecs->const_p + TYPE_READONLY (element_type);
5875 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5876 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5877 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5878 as1 = declspecs->address_space;
5879 as2 = TYPE_ADDR_SPACE (element_type);
5880 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5882 if (constp > 1)
5883 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5884 if (restrictp > 1)
5885 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5886 if (volatilep > 1)
5887 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5888 if (atomicp > 1)
5889 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5891 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5892 error_at (loc, "conflicting named address spaces (%s vs %s)",
5893 c_addr_space_name (as1), c_addr_space_name (as2));
5895 if ((TREE_CODE (type) == ARRAY_TYPE
5896 || first_non_attr_kind == cdk_array)
5897 && TYPE_QUALS (element_type))
5899 orig_qual_type = type;
5900 type = TYPE_MAIN_VARIANT (type);
5902 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5903 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5904 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5905 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5906 | ENCODE_QUAL_ADDR_SPACE (address_space));
5907 if (type_quals != TYPE_QUALS (element_type))
5908 orig_qual_type = NULL_TREE;
5910 /* Applying the _Atomic qualifier to an array type (through the use
5911 of typedefs or typeof) must be detected here. If the qualifier
5912 is introduced later, any appearance of applying it to an array is
5913 actually applying it to an element of that array. */
5914 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5915 error_at (loc, "%<_Atomic%>-qualified array type");
5917 /* Warn about storage classes that are invalid for certain
5918 kinds of declarations (parameters, typenames, etc.). */
5920 if (funcdef_flag
5921 && (threadp
5922 || storage_class == csc_auto
5923 || storage_class == csc_register
5924 || storage_class == csc_typedef))
5926 if (storage_class == csc_auto)
5927 pedwarn (loc,
5928 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5929 "function definition declared %<auto%>");
5930 if (storage_class == csc_register)
5931 error_at (loc, "function definition declared %<register%>");
5932 if (storage_class == csc_typedef)
5933 error_at (loc, "function definition declared %<typedef%>");
5934 if (threadp)
5935 error_at (loc, "function definition declared %qs",
5936 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5937 threadp = false;
5938 if (storage_class == csc_auto
5939 || storage_class == csc_register
5940 || storage_class == csc_typedef)
5941 storage_class = csc_none;
5943 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5945 if (decl_context == PARM && storage_class == csc_register)
5947 else
5949 switch (decl_context)
5951 case FIELD:
5952 if (name)
5953 error_at (loc, "storage class specified for structure "
5954 "field %qE", name);
5955 else
5956 error_at (loc, "storage class specified for structure field");
5957 break;
5958 case PARM:
5959 if (name)
5960 error_at (loc, "storage class specified for parameter %qE",
5961 name);
5962 else
5963 error_at (loc, "storage class specified for unnamed parameter");
5964 break;
5965 default:
5966 error_at (loc, "storage class specified for typename");
5967 break;
5969 storage_class = csc_none;
5970 threadp = false;
5973 else if (storage_class == csc_extern
5974 && initialized
5975 && !funcdef_flag)
5977 /* 'extern' with initialization is invalid if not at file scope. */
5978 if (current_scope == file_scope)
5980 /* It is fine to have 'extern const' when compiling at C
5981 and C++ intersection. */
5982 if (!(warn_cxx_compat && constp))
5983 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5984 name);
5986 else
5987 error_at (loc, "%qE has both %<extern%> and initializer", name);
5989 else if (current_scope == file_scope)
5991 if (storage_class == csc_auto)
5992 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5993 name);
5994 if (pedantic && storage_class == csc_register)
5995 pedwarn (input_location, OPT_Wpedantic,
5996 "file-scope declaration of %qE specifies %<register%>", name);
5998 else
6000 if (storage_class == csc_extern && funcdef_flag)
6001 error_at (loc, "nested function %qE declared %<extern%>", name);
6002 else if (threadp && storage_class == csc_none)
6004 error_at (loc, "function-scope %qE implicitly auto and declared "
6005 "%qs", name,
6006 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6007 threadp = false;
6011 /* Now figure out the structure of the declarator proper.
6012 Descend through it, creating more complex types, until we reach
6013 the declared identifier (or NULL_TREE, in an absolute declarator).
6014 At each stage we maintain an unqualified version of the type
6015 together with any qualifiers that should be applied to it with
6016 c_build_qualified_type; this way, array types including
6017 multidimensional array types are first built up in unqualified
6018 form and then the qualified form is created with
6019 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6021 while (declarator && declarator->kind != cdk_id)
6023 if (type == error_mark_node)
6025 declarator = declarator->declarator;
6026 continue;
6029 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6030 a cdk_pointer (for *...),
6031 a cdk_function (for ...(...)),
6032 a cdk_attrs (for nested attributes),
6033 or a cdk_id (for the name being declared
6034 or the place in an absolute declarator
6035 where the name was omitted).
6036 For the last case, we have just exited the loop.
6038 At this point, TYPE is the type of elements of an array,
6039 or for a function to return, or for a pointer to point to.
6040 After this sequence of ifs, TYPE is the type of the
6041 array or function or pointer, and DECLARATOR has had its
6042 outermost layer removed. */
6044 if (array_ptr_quals != TYPE_UNQUALIFIED
6045 || array_ptr_attrs != NULL_TREE
6046 || array_parm_static)
6048 /* Only the innermost declarator (making a parameter be of
6049 array type which is converted to pointer type)
6050 may have static or type qualifiers. */
6051 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6052 array_ptr_quals = TYPE_UNQUALIFIED;
6053 array_ptr_attrs = NULL_TREE;
6054 array_parm_static = false;
6057 switch (declarator->kind)
6059 case cdk_attrs:
6061 /* A declarator with embedded attributes. */
6062 tree attrs = declarator->u.attrs;
6063 const struct c_declarator *inner_decl;
6064 int attr_flags = 0;
6065 declarator = declarator->declarator;
6066 inner_decl = declarator;
6067 while (inner_decl->kind == cdk_attrs)
6068 inner_decl = inner_decl->declarator;
6069 if (inner_decl->kind == cdk_id)
6070 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6071 else if (inner_decl->kind == cdk_function)
6072 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6073 else if (inner_decl->kind == cdk_array)
6074 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6075 returned_attrs = decl_attributes (&type,
6076 chainon (returned_attrs, attrs),
6077 attr_flags);
6078 break;
6080 case cdk_array:
6082 tree itype = NULL_TREE;
6083 tree size = declarator->u.array.dimen;
6084 /* The index is a signed object `sizetype' bits wide. */
6085 tree index_type = c_common_signed_type (sizetype);
6087 array_ptr_quals = declarator->u.array.quals;
6088 array_ptr_attrs = declarator->u.array.attrs;
6089 array_parm_static = declarator->u.array.static_p;
6090 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6092 declarator = declarator->declarator;
6094 /* Check for some types that there cannot be arrays of. */
6096 if (VOID_TYPE_P (type))
6098 if (name)
6099 error_at (loc, "declaration of %qE as array of voids", name);
6100 else
6101 error_at (loc, "declaration of type name as array of voids");
6102 type = error_mark_node;
6105 if (TREE_CODE (type) == FUNCTION_TYPE)
6107 if (name)
6108 error_at (loc, "declaration of %qE as array of functions",
6109 name);
6110 else
6111 error_at (loc, "declaration of type name as array of "
6112 "functions");
6113 type = error_mark_node;
6116 if (pedantic && !in_system_header_at (input_location)
6117 && flexible_array_type_p (type))
6118 pedwarn (loc, OPT_Wpedantic,
6119 "invalid use of structure with flexible array member");
6121 if (size == error_mark_node)
6122 type = error_mark_node;
6124 if (type == error_mark_node)
6125 continue;
6127 /* If size was specified, set ITYPE to a range-type for
6128 that size. Otherwise, ITYPE remains null. finish_decl
6129 may figure it out from an initial value. */
6131 if (size)
6133 bool size_maybe_const = true;
6134 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6135 && !TREE_OVERFLOW (size));
6136 bool this_size_varies = false;
6138 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6139 lvalue. */
6140 STRIP_TYPE_NOPS (size);
6142 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6144 if (name)
6145 error_at (loc, "size of array %qE has non-integer type",
6146 name);
6147 else
6148 error_at (loc,
6149 "size of unnamed array has non-integer type");
6150 size = integer_one_node;
6152 /* This can happen with enum forward declaration. */
6153 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6155 if (name)
6156 error_at (loc, "size of array %qE has incomplete type",
6157 name);
6158 else
6159 error_at (loc, "size of unnamed array has incomplete "
6160 "type");
6161 size = integer_one_node;
6164 size = c_fully_fold (size, false, &size_maybe_const);
6166 if (pedantic && size_maybe_const && integer_zerop (size))
6168 if (name)
6169 pedwarn (loc, OPT_Wpedantic,
6170 "ISO C forbids zero-size array %qE", name);
6171 else
6172 pedwarn (loc, OPT_Wpedantic,
6173 "ISO C forbids zero-size array");
6176 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6178 constant_expression_warning (size);
6179 if (tree_int_cst_sgn (size) < 0)
6181 if (name)
6182 error_at (loc, "size of array %qE is negative", name);
6183 else
6184 error_at (loc, "size of unnamed array is negative");
6185 size = integer_one_node;
6187 /* Handle a size folded to an integer constant but
6188 not an integer constant expression. */
6189 if (!size_int_const)
6191 /* If this is a file scope declaration of an
6192 ordinary identifier, this is invalid code;
6193 diagnosing it here and not subsequently
6194 treating the type as variable-length avoids
6195 more confusing diagnostics later. */
6196 if ((decl_context == NORMAL || decl_context == FIELD)
6197 && current_scope == file_scope)
6198 pedwarn (input_location, 0,
6199 "variably modified %qE at file scope",
6200 name);
6201 else
6202 this_size_varies = size_varies = true;
6203 warn_variable_length_array (name, size);
6206 else if ((decl_context == NORMAL || decl_context == FIELD)
6207 && current_scope == file_scope)
6209 error_at (loc, "variably modified %qE at file scope", name);
6210 size = integer_one_node;
6212 else
6214 /* Make sure the array size remains visibly
6215 nonconstant even if it is (eg) a const variable
6216 with known value. */
6217 this_size_varies = size_varies = true;
6218 warn_variable_length_array (name, size);
6219 if (sanitize_flags_p (SANITIZE_VLA)
6220 && current_function_decl != NULL_TREE
6221 && decl_context == NORMAL)
6223 /* Evaluate the array size only once. */
6224 size = save_expr (size);
6225 size = c_fully_fold (size, false, NULL);
6226 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6227 ubsan_instrument_vla (loc, size),
6228 size);
6232 if (integer_zerop (size) && !this_size_varies)
6234 /* A zero-length array cannot be represented with
6235 an unsigned index type, which is what we'll
6236 get with build_index_type. Create an
6237 open-ended range instead. */
6238 itype = build_range_type (sizetype, size, NULL_TREE);
6240 else
6242 /* Arrange for the SAVE_EXPR on the inside of the
6243 MINUS_EXPR, which allows the -1 to get folded
6244 with the +1 that happens when building TYPE_SIZE. */
6245 if (size_varies)
6246 size = save_expr (size);
6247 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6248 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6249 integer_zero_node, size);
6251 /* Compute the maximum valid index, that is, size
6252 - 1. Do the calculation in index_type, so that
6253 if it is a variable the computations will be
6254 done in the proper mode. */
6255 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6256 convert (index_type, size),
6257 convert (index_type,
6258 size_one_node));
6260 /* The above overflows when size does not fit
6261 in index_type.
6262 ??? While a size of INT_MAX+1 technically shouldn't
6263 cause an overflow (because we subtract 1), handling
6264 this case seems like an unnecessary complication. */
6265 if (TREE_CODE (size) == INTEGER_CST
6266 && !int_fits_type_p (size, index_type))
6268 if (name)
6269 error_at (loc, "size of array %qE is too large",
6270 name);
6271 else
6272 error_at (loc, "size of unnamed array is too large");
6273 type = error_mark_node;
6274 continue;
6277 itype = build_index_type (itype);
6279 if (this_size_varies)
6281 if (*expr)
6282 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6283 *expr, size);
6284 else
6285 *expr = size;
6286 *expr_const_operands &= size_maybe_const;
6289 else if (decl_context == FIELD)
6291 bool flexible_array_member = false;
6292 if (array_parm_vla_unspec_p)
6293 /* Field names can in fact have function prototype
6294 scope so [*] is disallowed here through making
6295 the field variably modified, not through being
6296 something other than a declaration with function
6297 prototype scope. */
6298 size_varies = true;
6299 else
6301 const struct c_declarator *t = declarator;
6302 while (t->kind == cdk_attrs)
6303 t = t->declarator;
6304 flexible_array_member = (t->kind == cdk_id);
6306 if (flexible_array_member
6307 && !in_system_header_at (input_location))
6308 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6309 "support flexible array members");
6311 /* ISO C99 Flexible array members are effectively
6312 identical to GCC's zero-length array extension. */
6313 if (flexible_array_member || array_parm_vla_unspec_p)
6314 itype = build_range_type (sizetype, size_zero_node,
6315 NULL_TREE);
6317 else if (decl_context == PARM)
6319 if (array_parm_vla_unspec_p)
6321 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6322 size_varies = true;
6325 else if (decl_context == TYPENAME)
6327 if (array_parm_vla_unspec_p)
6329 /* C99 6.7.5.2p4 */
6330 warning (0, "%<[*]%> not in a declaration");
6331 /* We use this to avoid messing up with incomplete
6332 array types of the same type, that would
6333 otherwise be modified below. */
6334 itype = build_range_type (sizetype, size_zero_node,
6335 NULL_TREE);
6336 size_varies = true;
6340 /* Complain about arrays of incomplete types. */
6341 if (!COMPLETE_TYPE_P (type))
6343 error_at (loc, "array type has incomplete element type %qT",
6344 type);
6345 /* See if we can be more helpful. */
6346 if (TREE_CODE (type) == ARRAY_TYPE)
6348 if (name)
6349 inform (loc, "declaration of %qE as multidimensional "
6350 "array must have bounds for all dimensions "
6351 "except the first", name);
6352 else
6353 inform (loc, "declaration of multidimensional array "
6354 "must have bounds for all dimensions except "
6355 "the first");
6357 type = error_mark_node;
6359 else
6360 /* When itype is NULL, a shared incomplete array type is
6361 returned for all array of a given type. Elsewhere we
6362 make sure we don't complete that type before copying
6363 it, but here we want to make sure we don't ever
6364 modify the shared type, so we gcc_assert (itype)
6365 below. */
6367 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6368 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6369 type = build_qualified_type (type,
6370 ENCODE_QUAL_ADDR_SPACE (as));
6372 type = build_array_type (type, itype);
6375 if (type != error_mark_node)
6377 if (size_varies)
6379 /* It is ok to modify type here even if itype is
6380 NULL: if size_varies, we're in a
6381 multi-dimensional array and the inner type has
6382 variable size, so the enclosing shared array type
6383 must too. */
6384 if (size && TREE_CODE (size) == INTEGER_CST)
6385 type
6386 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6387 C_TYPE_VARIABLE_SIZE (type) = 1;
6390 /* The GCC extension for zero-length arrays differs from
6391 ISO flexible array members in that sizeof yields
6392 zero. */
6393 if (size && integer_zerop (size))
6395 gcc_assert (itype);
6396 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6397 TYPE_SIZE (type) = bitsize_zero_node;
6398 TYPE_SIZE_UNIT (type) = size_zero_node;
6399 SET_TYPE_STRUCTURAL_EQUALITY (type);
6401 if (array_parm_vla_unspec_p)
6403 gcc_assert (itype);
6404 /* The type is complete. C99 6.7.5.2p4 */
6405 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6406 TYPE_SIZE (type) = bitsize_zero_node;
6407 TYPE_SIZE_UNIT (type) = size_zero_node;
6408 SET_TYPE_STRUCTURAL_EQUALITY (type);
6411 if (!valid_array_size_p (loc, type, name))
6412 type = error_mark_node;
6415 if (decl_context != PARM
6416 && (array_ptr_quals != TYPE_UNQUALIFIED
6417 || array_ptr_attrs != NULL_TREE
6418 || array_parm_static))
6420 error_at (loc, "static or type qualifiers in non-parameter "
6421 "array declarator");
6422 array_ptr_quals = TYPE_UNQUALIFIED;
6423 array_ptr_attrs = NULL_TREE;
6424 array_parm_static = false;
6426 orig_qual_indirect++;
6427 break;
6429 case cdk_function:
6431 /* Say it's a definition only for the declarator closest
6432 to the identifier, apart possibly from some
6433 attributes. */
6434 bool really_funcdef = false;
6435 tree arg_types;
6436 orig_qual_type = NULL_TREE;
6437 if (funcdef_flag)
6439 const struct c_declarator *t = declarator->declarator;
6440 while (t->kind == cdk_attrs)
6441 t = t->declarator;
6442 really_funcdef = (t->kind == cdk_id);
6445 /* Declaring a function type. Make sure we have a valid
6446 type for the function to return. */
6447 if (type == error_mark_node)
6448 continue;
6450 size_varies = false;
6452 /* Warn about some types functions can't return. */
6453 if (TREE_CODE (type) == FUNCTION_TYPE)
6455 if (name)
6456 error_at (loc, "%qE declared as function returning a "
6457 "function", name);
6458 else
6459 error_at (loc, "type name declared as function "
6460 "returning a function");
6461 type = integer_type_node;
6463 if (TREE_CODE (type) == ARRAY_TYPE)
6465 if (name)
6466 error_at (loc, "%qE declared as function returning an array",
6467 name);
6468 else
6469 error_at (loc, "type name declared as function returning "
6470 "an array");
6471 type = integer_type_node;
6474 /* Construct the function type and go to the next
6475 inner layer of declarator. */
6476 arg_info = declarator->u.arg_info;
6477 arg_types = grokparms (arg_info, really_funcdef);
6479 /* Type qualifiers before the return type of the function
6480 qualify the return type, not the function type. */
6481 if (type_quals)
6483 const enum c_declspec_word ignored_quals_list[] =
6485 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6486 cdw_atomic, cdw_number_of_elements
6488 location_t specs_loc
6489 = smallest_type_quals_location (declspecs->locations,
6490 ignored_quals_list);
6491 if (specs_loc == UNKNOWN_LOCATION)
6492 specs_loc = declspecs->locations[cdw_typedef];
6493 if (specs_loc == UNKNOWN_LOCATION)
6494 specs_loc = loc;
6496 /* Type qualifiers on a function return type are
6497 normally permitted by the standard but have no
6498 effect, so give a warning at -Wreturn-type.
6499 Qualifiers on a void return type are banned on
6500 function definitions in ISO C; GCC used to used
6501 them for noreturn functions. The resolution of C11
6502 DR#423 means qualifiers (other than _Atomic) are
6503 actually removed from the return type when
6504 determining the function type. */
6505 int quals_used = type_quals;
6506 if (flag_isoc11)
6507 quals_used &= TYPE_QUAL_ATOMIC;
6508 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6509 pedwarn (specs_loc, 0,
6510 "function definition has qualified void return type");
6511 else
6512 warning_at (specs_loc, OPT_Wignored_qualifiers,
6513 "type qualifiers ignored on function return type");
6515 /* Ensure an error for restrict on invalid types; the
6516 DR#423 resolution is not entirely clear about
6517 this. */
6518 if (flag_isoc11
6519 && (type_quals & TYPE_QUAL_RESTRICT)
6520 && (!POINTER_TYPE_P (type)
6521 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6522 error_at (loc, "invalid use of %<restrict%>");
6523 if (quals_used)
6524 type = c_build_qualified_type (type, quals_used);
6526 type_quals = TYPE_UNQUALIFIED;
6528 type = build_function_type (type, arg_types);
6529 declarator = declarator->declarator;
6531 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6532 the formal parameter list of this FUNCTION_TYPE to point to
6533 the FUNCTION_TYPE node itself. */
6535 c_arg_tag *tag;
6536 unsigned ix;
6538 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6539 TYPE_CONTEXT (tag->type) = type;
6541 break;
6543 case cdk_pointer:
6545 /* Merge any constancy or volatility into the target type
6546 for the pointer. */
6547 if ((type_quals & TYPE_QUAL_ATOMIC)
6548 && TREE_CODE (type) == FUNCTION_TYPE)
6550 error_at (loc,
6551 "%<_Atomic%>-qualified function type");
6552 type_quals &= ~TYPE_QUAL_ATOMIC;
6554 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6555 && type_quals)
6556 pedwarn (loc, OPT_Wpedantic,
6557 "ISO C forbids qualified function types");
6558 if (type_quals)
6559 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6560 orig_qual_indirect);
6561 orig_qual_type = NULL_TREE;
6562 size_varies = false;
6564 /* When the pointed-to type involves components of variable size,
6565 care must be taken to ensure that the size evaluation code is
6566 emitted early enough to dominate all the possible later uses
6567 and late enough for the variables on which it depends to have
6568 been assigned.
6570 This is expected to happen automatically when the pointed-to
6571 type has a name/declaration of it's own, but special attention
6572 is required if the type is anonymous.
6574 We attach an artificial TYPE_DECL to such pointed-to type
6575 and arrange for it to be included in a DECL_EXPR. This
6576 forces the sizes evaluation at a safe point and ensures it
6577 is not deferred until e.g. within a deeper conditional context.
6579 PARM contexts have no enclosing statement list that
6580 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6581 instead, and add it to the list of expressions that
6582 need to be evaluated.
6584 TYPENAME contexts do have an enclosing statement list,
6585 but it would be incorrect to use it, as the size should
6586 only be evaluated if the containing expression is
6587 evaluated. We might also be in the middle of an
6588 expression with side effects on the pointed-to type size
6589 "arguments" prior to the pointer declaration point and
6590 the fake TYPE_DECL in the enclosing context would force
6591 the size evaluation prior to the side effects. We therefore
6592 use BIND_EXPRs in TYPENAME contexts too. */
6593 if (!TYPE_NAME (type)
6594 && variably_modified_type_p (type, NULL_TREE))
6596 tree bind = NULL_TREE;
6597 if (decl_context == TYPENAME || decl_context == PARM)
6599 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6600 NULL_TREE, NULL_TREE);
6601 TREE_SIDE_EFFECTS (bind) = 1;
6602 BIND_EXPR_BODY (bind) = push_stmt_list ();
6603 push_scope ();
6605 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6606 DECL_ARTIFICIAL (decl) = 1;
6607 pushdecl (decl);
6608 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6609 TYPE_NAME (type) = decl;
6610 if (bind)
6612 pop_scope ();
6613 BIND_EXPR_BODY (bind)
6614 = pop_stmt_list (BIND_EXPR_BODY (bind));
6615 if (*expr)
6616 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6617 bind);
6618 else
6619 *expr = bind;
6623 type = c_build_pointer_type (type);
6625 /* Process type qualifiers (such as const or volatile)
6626 that were given inside the `*'. */
6627 type_quals = declarator->u.pointer_quals;
6629 declarator = declarator->declarator;
6630 break;
6632 default:
6633 gcc_unreachable ();
6636 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6638 /* Now TYPE has the actual type, apart from any qualifiers in
6639 TYPE_QUALS. */
6641 /* Warn about address space used for things other than static memory or
6642 pointers. */
6643 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6644 if (!ADDR_SPACE_GENERIC_P (address_space))
6646 if (decl_context == NORMAL)
6648 switch (storage_class)
6650 case csc_auto:
6651 error ("%qs combined with %<auto%> qualifier for %qE",
6652 c_addr_space_name (address_space), name);
6653 break;
6654 case csc_register:
6655 error ("%qs combined with %<register%> qualifier for %qE",
6656 c_addr_space_name (address_space), name);
6657 break;
6658 case csc_none:
6659 if (current_function_scope)
6661 error ("%qs specified for auto variable %qE",
6662 c_addr_space_name (address_space), name);
6663 break;
6665 break;
6666 case csc_static:
6667 case csc_extern:
6668 case csc_typedef:
6669 break;
6670 default:
6671 gcc_unreachable ();
6674 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6676 if (name)
6677 error ("%qs specified for parameter %qE",
6678 c_addr_space_name (address_space), name);
6679 else
6680 error ("%qs specified for unnamed parameter",
6681 c_addr_space_name (address_space));
6683 else if (decl_context == FIELD)
6685 if (name)
6686 error ("%qs specified for structure field %qE",
6687 c_addr_space_name (address_space), name);
6688 else
6689 error ("%qs specified for structure field",
6690 c_addr_space_name (address_space));
6694 /* Check the type and width of a bit-field. */
6695 if (bitfield)
6697 check_bitfield_type_and_width (loc, &type, width, name);
6698 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6699 atomic types are permitted for bit-fields; we have no code to
6700 make bit-field accesses atomic, so disallow them. */
6701 if (type_quals & TYPE_QUAL_ATOMIC)
6703 if (name)
6704 error_at (loc, "bit-field %qE has atomic type", name);
6705 else
6706 error_at (loc, "bit-field has atomic type");
6707 type_quals &= ~TYPE_QUAL_ATOMIC;
6711 /* Reject invalid uses of _Alignas. */
6712 if (declspecs->alignas_p)
6714 if (storage_class == csc_typedef)
6715 error_at (loc, "alignment specified for typedef %qE", name);
6716 else if (storage_class == csc_register)
6717 error_at (loc, "alignment specified for %<register%> object %qE",
6718 name);
6719 else if (decl_context == PARM)
6721 if (name)
6722 error_at (loc, "alignment specified for parameter %qE", name);
6723 else
6724 error_at (loc, "alignment specified for unnamed parameter");
6726 else if (bitfield)
6728 if (name)
6729 error_at (loc, "alignment specified for bit-field %qE", name);
6730 else
6731 error_at (loc, "alignment specified for unnamed bit-field");
6733 else if (TREE_CODE (type) == FUNCTION_TYPE)
6734 error_at (loc, "alignment specified for function %qE", name);
6735 else if (declspecs->align_log != -1 && TYPE_P (type))
6737 alignas_align = 1U << declspecs->align_log;
6738 if (alignas_align < min_align_of_type (type))
6740 if (name)
6741 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6742 "alignment of %qE", name);
6743 else
6744 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6745 "alignment of unnamed field");
6746 alignas_align = 0;
6751 /* If this is declaring a typedef name, return a TYPE_DECL. */
6753 if (storage_class == csc_typedef)
6755 tree decl;
6756 if ((type_quals & TYPE_QUAL_ATOMIC)
6757 && TREE_CODE (type) == FUNCTION_TYPE)
6759 error_at (loc,
6760 "%<_Atomic%>-qualified function type");
6761 type_quals &= ~TYPE_QUAL_ATOMIC;
6763 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6764 && type_quals)
6765 pedwarn (loc, OPT_Wpedantic,
6766 "ISO C forbids qualified function types");
6767 if (type_quals)
6768 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6769 orig_qual_indirect);
6770 decl = build_decl (declarator->id_loc,
6771 TYPE_DECL, declarator->u.id, type);
6772 if (declspecs->explicit_signed_p)
6773 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6774 if (declspecs->inline_p)
6775 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6776 if (declspecs->noreturn_p)
6777 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6779 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6781 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6783 if (b != NULL
6784 && b->decl != NULL_TREE
6785 && (B_IN_CURRENT_SCOPE (b)
6786 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6787 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6789 auto_diagnostic_group d;
6790 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6791 ("using %qD as both a typedef and a tag is "
6792 "invalid in C++"), decl)
6793 && b->locus != UNKNOWN_LOCATION)
6794 inform (b->locus, "originally defined here");
6798 return decl;
6801 /* If this is a type name (such as, in a cast or sizeof),
6802 compute the type and return it now. */
6804 if (decl_context == TYPENAME)
6806 /* Note that the grammar rejects storage classes in typenames
6807 and fields. */
6808 gcc_assert (storage_class == csc_none && !threadp
6809 && !declspecs->inline_p && !declspecs->noreturn_p);
6810 if ((type_quals & TYPE_QUAL_ATOMIC)
6811 && TREE_CODE (type) == FUNCTION_TYPE)
6813 error_at (loc,
6814 "%<_Atomic%>-qualified function type");
6815 type_quals &= ~TYPE_QUAL_ATOMIC;
6817 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6818 && type_quals)
6819 pedwarn (loc, OPT_Wpedantic,
6820 "ISO C forbids const or volatile function types");
6821 if (type_quals)
6822 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6823 orig_qual_indirect);
6824 return type;
6827 if (pedantic && decl_context == FIELD
6828 && variably_modified_type_p (type, NULL_TREE))
6830 /* C99 6.7.2.1p8 */
6831 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6832 "have a variably modified type");
6835 /* Aside from typedefs and type names (handle above),
6836 `void' at top level (not within pointer)
6837 is allowed only in public variables.
6838 We don't complain about parms either, but that is because
6839 a better error message can be made later. */
6841 if (VOID_TYPE_P (type) && decl_context != PARM
6842 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6843 && (storage_class == csc_extern
6844 || (current_scope == file_scope
6845 && !(storage_class == csc_static
6846 || storage_class == csc_register)))))
6848 error_at (loc, "variable or field %qE declared void", name);
6849 type = integer_type_node;
6852 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6853 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6856 tree decl;
6858 if (decl_context == PARM)
6860 tree promoted_type;
6861 bool array_parameter_p = false;
6863 /* A parameter declared as an array of T is really a pointer to T.
6864 One declared as a function is really a pointer to a function. */
6866 if (TREE_CODE (type) == ARRAY_TYPE)
6868 /* Transfer const-ness of array into that of type pointed to. */
6869 type = TREE_TYPE (type);
6870 if (orig_qual_type != NULL_TREE)
6872 if (orig_qual_indirect == 0)
6873 orig_qual_type = TREE_TYPE (orig_qual_type);
6874 else
6875 orig_qual_indirect--;
6877 if (type_quals)
6878 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6879 orig_qual_indirect);
6880 type = c_build_pointer_type (type);
6881 type_quals = array_ptr_quals;
6882 if (type_quals)
6883 type = c_build_qualified_type (type, type_quals);
6885 /* We don't yet implement attributes in this context. */
6886 if (array_ptr_attrs != NULL_TREE)
6887 warning_at (loc, OPT_Wattributes,
6888 "attributes in parameter array declarator ignored");
6890 size_varies = false;
6891 array_parameter_p = true;
6893 else if (TREE_CODE (type) == FUNCTION_TYPE)
6895 if (type_quals & TYPE_QUAL_ATOMIC)
6897 error_at (loc,
6898 "%<_Atomic%>-qualified function type");
6899 type_quals &= ~TYPE_QUAL_ATOMIC;
6901 else if (type_quals)
6902 pedwarn (loc, OPT_Wpedantic,
6903 "ISO C forbids qualified function types");
6904 if (type_quals)
6905 type = c_build_qualified_type (type, type_quals);
6906 type = c_build_pointer_type (type);
6907 type_quals = TYPE_UNQUALIFIED;
6909 else if (type_quals)
6910 type = c_build_qualified_type (type, type_quals);
6912 decl = build_decl (declarator->id_loc,
6913 PARM_DECL, declarator->u.id, type);
6914 if (size_varies)
6915 C_DECL_VARIABLE_SIZE (decl) = 1;
6916 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6918 /* Compute the type actually passed in the parmlist,
6919 for the case where there is no prototype.
6920 (For example, shorts and chars are passed as ints.)
6921 When there is a prototype, this is overridden later. */
6923 if (type == error_mark_node)
6924 promoted_type = type;
6925 else
6926 promoted_type = c_type_promotes_to (type);
6928 DECL_ARG_TYPE (decl) = promoted_type;
6929 if (declspecs->inline_p)
6930 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6931 if (declspecs->noreturn_p)
6932 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6934 else if (decl_context == FIELD)
6936 /* Note that the grammar rejects storage classes in typenames
6937 and fields. */
6938 gcc_assert (storage_class == csc_none && !threadp
6939 && !declspecs->inline_p && !declspecs->noreturn_p);
6941 /* Structure field. It may not be a function. */
6943 if (TREE_CODE (type) == FUNCTION_TYPE)
6945 error_at (loc, "field %qE declared as a function", name);
6946 type = build_pointer_type (type);
6948 else if (TREE_CODE (type) != ERROR_MARK
6949 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6951 if (name)
6952 error_at (loc, "field %qE has incomplete type", name);
6953 else
6954 error_at (loc, "unnamed field has incomplete type");
6955 type = error_mark_node;
6957 else if (TREE_CODE (type) == ARRAY_TYPE
6958 && TYPE_DOMAIN (type) == NULL_TREE)
6960 /* We have a flexible array member through a typedef.
6961 Set suitable range. Whether this is a correct position
6962 for a flexible array member will be determined elsewhere. */
6963 if (!in_system_header_at (input_location))
6964 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6965 "support flexible array members");
6966 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6967 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6968 NULL_TREE);
6969 if (orig_qual_indirect == 0)
6970 orig_qual_type = NULL_TREE;
6972 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6973 orig_qual_indirect);
6974 decl = build_decl (declarator->id_loc,
6975 FIELD_DECL, declarator->u.id, type);
6976 DECL_NONADDRESSABLE_P (decl) = bitfield;
6977 if (bitfield && !declarator->u.id)
6979 TREE_NO_WARNING (decl) = 1;
6980 DECL_PADDING_P (decl) = 1;
6983 if (size_varies)
6984 C_DECL_VARIABLE_SIZE (decl) = 1;
6986 else if (TREE_CODE (type) == FUNCTION_TYPE)
6988 if (storage_class == csc_register || threadp)
6990 error_at (loc, "invalid storage class for function %qE", name);
6992 else if (current_scope != file_scope)
6994 /* Function declaration not at file scope. Storage
6995 classes other than `extern' are not allowed, C99
6996 6.7.1p5, and `extern' makes no difference. However,
6997 GCC allows 'auto', perhaps with 'inline', to support
6998 nested functions. */
6999 if (storage_class == csc_auto)
7000 pedwarn (loc, OPT_Wpedantic,
7001 "invalid storage class for function %qE", name);
7002 else if (storage_class == csc_static)
7004 error_at (loc, "invalid storage class for function %qE", name);
7005 if (funcdef_flag)
7006 storage_class = declspecs->storage_class = csc_none;
7007 else
7008 return NULL_TREE;
7012 decl = build_decl (declarator->id_loc,
7013 FUNCTION_DECL, declarator->u.id, type);
7014 decl = build_decl_attribute_variant (decl, decl_attr);
7016 if (type_quals & TYPE_QUAL_ATOMIC)
7018 error_at (loc,
7019 "%<_Atomic%>-qualified function type");
7020 type_quals &= ~TYPE_QUAL_ATOMIC;
7022 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7023 pedwarn (loc, OPT_Wpedantic,
7024 "ISO C forbids qualified function types");
7026 /* Every function declaration is an external reference
7027 (DECL_EXTERNAL) except for those which are not at file
7028 scope and are explicitly declared "auto". This is
7029 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7030 GCC to signify a forward declaration of a nested function. */
7031 if (storage_class == csc_auto && current_scope != file_scope)
7032 DECL_EXTERNAL (decl) = 0;
7033 /* In C99, a function which is declared 'inline' with 'extern'
7034 is not an external reference (which is confusing). It
7035 means that the later definition of the function must be output
7036 in this file, C99 6.7.4p6. In GNU C89, a function declared
7037 'extern inline' is an external reference. */
7038 else if (declspecs->inline_p && storage_class != csc_static)
7039 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7040 == flag_gnu89_inline);
7041 else
7042 DECL_EXTERNAL (decl) = !initialized;
7044 /* Record absence of global scope for `static' or `auto'. */
7045 TREE_PUBLIC (decl)
7046 = !(storage_class == csc_static || storage_class == csc_auto);
7048 /* For a function definition, record the argument information
7049 block where store_parm_decls will look for it. */
7050 if (funcdef_flag)
7051 current_function_arg_info = arg_info;
7053 if (declspecs->default_int_p)
7054 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7056 /* Record presence of `inline' and `_Noreturn', if it is
7057 reasonable. */
7058 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
7060 if (declspecs->inline_p)
7061 pedwarn (loc, 0, "cannot inline function %<main%>");
7062 if (declspecs->noreturn_p)
7063 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7065 else
7067 if (declspecs->inline_p)
7068 /* Record that the function is declared `inline'. */
7069 DECL_DECLARED_INLINE_P (decl) = 1;
7070 if (declspecs->noreturn_p)
7072 if (flag_isoc99)
7073 pedwarn_c99 (loc, OPT_Wpedantic,
7074 "ISO C99 does not support %<_Noreturn%>");
7075 else
7076 pedwarn_c99 (loc, OPT_Wpedantic,
7077 "ISO C90 does not support %<_Noreturn%>");
7078 TREE_THIS_VOLATILE (decl) = 1;
7082 else
7084 /* It's a variable. */
7085 /* An uninitialized decl with `extern' is a reference. */
7086 int extern_ref = !initialized && storage_class == csc_extern;
7088 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7089 orig_qual_indirect);
7091 /* C99 6.2.2p7: It is invalid (compile-time undefined
7092 behavior) to create an 'extern' declaration for a
7093 variable if there is a global declaration that is
7094 'static' and the global declaration is not visible.
7095 (If the static declaration _is_ currently visible,
7096 the 'extern' declaration is taken to refer to that decl.) */
7097 if (extern_ref && current_scope != file_scope)
7099 tree global_decl = identifier_global_value (declarator->u.id);
7100 tree visible_decl = lookup_name (declarator->u.id);
7102 if (global_decl
7103 && global_decl != visible_decl
7104 && VAR_P (global_decl)
7105 && !TREE_PUBLIC (global_decl))
7106 error_at (loc, "variable previously declared %<static%> "
7107 "redeclared %<extern%>");
7110 decl = build_decl (declarator->id_loc,
7111 VAR_DECL, declarator->u.id, type);
7112 if (size_varies)
7113 C_DECL_VARIABLE_SIZE (decl) = 1;
7115 if (declspecs->inline_p)
7116 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7117 if (declspecs->noreturn_p)
7118 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7120 /* At file scope, an initialized extern declaration may follow
7121 a static declaration. In that case, DECL_EXTERNAL will be
7122 reset later in start_decl. */
7123 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7125 /* At file scope, the presence of a `static' or `register' storage
7126 class specifier, or the absence of all storage class specifiers
7127 makes this declaration a definition (perhaps tentative). Also,
7128 the absence of `static' makes it public. */
7129 if (current_scope == file_scope)
7131 TREE_PUBLIC (decl) = storage_class != csc_static;
7132 TREE_STATIC (decl) = !extern_ref;
7134 /* Not at file scope, only `static' makes a static definition. */
7135 else
7137 TREE_STATIC (decl) = (storage_class == csc_static);
7138 TREE_PUBLIC (decl) = extern_ref;
7141 if (threadp)
7142 set_decl_tls_model (decl, decl_default_tls_model (decl));
7145 if ((storage_class == csc_extern
7146 || (storage_class == csc_none
7147 && TREE_CODE (type) == FUNCTION_TYPE
7148 && !funcdef_flag))
7149 && variably_modified_type_p (type, NULL_TREE))
7151 /* C99 6.7.5.2p2 */
7152 if (TREE_CODE (type) == FUNCTION_TYPE)
7153 error_at (loc, "non-nested function with variably modified type");
7154 else
7155 error_at (loc, "object with variably modified type must have "
7156 "no linkage");
7159 /* Record `register' declaration for warnings on &
7160 and in case doing stupid register allocation. */
7162 if (storage_class == csc_register)
7164 C_DECL_REGISTER (decl) = 1;
7165 DECL_REGISTER (decl) = 1;
7168 /* Record constancy and volatility. */
7169 c_apply_type_quals_to_decl (type_quals, decl);
7171 /* Apply _Alignas specifiers. */
7172 if (alignas_align)
7174 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7175 DECL_USER_ALIGN (decl) = 1;
7178 /* If a type has volatile components, it should be stored in memory.
7179 Otherwise, the fact that those components are volatile
7180 will be ignored, and would even crash the compiler.
7181 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7182 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7183 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7184 || TREE_CODE (decl) == RESULT_DECL))
7186 /* It is not an error for a structure with volatile fields to
7187 be declared register, but reset DECL_REGISTER since it
7188 cannot actually go in a register. */
7189 int was_reg = C_DECL_REGISTER (decl);
7190 C_DECL_REGISTER (decl) = 0;
7191 DECL_REGISTER (decl) = 0;
7192 c_mark_addressable (decl);
7193 C_DECL_REGISTER (decl) = was_reg;
7196 /* This is the earliest point at which we might know the assembler
7197 name of a variable. Thus, if it's known before this, die horribly. */
7198 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7199 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7201 if (warn_cxx_compat
7202 && VAR_P (decl)
7203 && TREE_PUBLIC (decl)
7204 && TREE_STATIC (decl)
7205 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7206 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7207 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7208 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7209 ("non-local variable %qD with anonymous type is "
7210 "questionable in C++"),
7211 decl);
7213 return decl;
7217 /* Decode the parameter-list info for a function type or function definition.
7218 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7219 if there is an identifier list instead of a parameter decl list).
7220 These two functions are separate because when a function returns
7221 or receives functions then each is called multiple times but the order
7222 of calls is different. The last call to `grokparms' is always the one
7223 that contains the formal parameter names of a function definition.
7225 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7227 FUNCDEF_FLAG is true for a function definition, false for
7228 a mere declaration. A nonempty identifier-list gets an error message
7229 when FUNCDEF_FLAG is false. */
7231 static tree
7232 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7234 tree arg_types = arg_info->types;
7236 if (funcdef_flag && arg_info->had_vla_unspec)
7238 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7239 /* C99 6.7.5.2p4 */
7240 error ("%<[*]%> not allowed in other than function prototype scope");
7243 if (arg_types == NULL_TREE && !funcdef_flag
7244 && !in_system_header_at (input_location))
7245 warning (OPT_Wstrict_prototypes,
7246 "function declaration isn%'t a prototype");
7248 if (arg_types == error_mark_node)
7249 /* Don't set TYPE_ARG_TYPES in this case. */
7250 return NULL_TREE;
7252 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7254 if (!funcdef_flag)
7256 pedwarn (input_location, 0, "parameter names (without types) in "
7257 "function declaration");
7258 arg_info->parms = NULL_TREE;
7260 else
7261 arg_info->parms = arg_info->types;
7263 arg_info->types = NULL_TREE;
7264 return NULL_TREE;
7266 else
7268 tree parm, type, typelt;
7269 unsigned int parmno;
7271 /* If there is a parameter of incomplete type in a definition,
7272 this is an error. In a declaration this is valid, and a
7273 struct or union type may be completed later, before any calls
7274 or definition of the function. In the case where the tag was
7275 first declared within the parameter list, a warning has
7276 already been given. If a parameter has void type, then
7277 however the function cannot be defined or called, so
7278 warn. */
7280 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7281 parm;
7282 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7284 type = TREE_VALUE (typelt);
7285 if (type == error_mark_node)
7286 continue;
7288 if (!COMPLETE_TYPE_P (type))
7290 if (funcdef_flag)
7292 if (DECL_NAME (parm))
7293 error_at (input_location,
7294 "parameter %u (%q+D) has incomplete type",
7295 parmno, parm);
7296 else
7297 error_at (DECL_SOURCE_LOCATION (parm),
7298 "parameter %u has incomplete type",
7299 parmno);
7301 TREE_VALUE (typelt) = error_mark_node;
7302 TREE_TYPE (parm) = error_mark_node;
7303 arg_types = NULL_TREE;
7305 else if (VOID_TYPE_P (type))
7307 if (DECL_NAME (parm))
7308 warning_at (input_location, 0,
7309 "parameter %u (%q+D) has void type",
7310 parmno, parm);
7311 else
7312 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7313 "parameter %u has void type",
7314 parmno);
7318 if (DECL_NAME (parm) && TREE_USED (parm))
7319 warn_if_shadowing (parm);
7321 return arg_types;
7325 /* Allocate and initialize a c_arg_info structure from the parser's
7326 obstack. */
7328 struct c_arg_info *
7329 build_arg_info (void)
7331 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7332 ret->parms = NULL_TREE;
7333 ret->tags = NULL;
7334 ret->types = NULL_TREE;
7335 ret->others = NULL_TREE;
7336 ret->pending_sizes = NULL;
7337 ret->had_vla_unspec = 0;
7338 return ret;
7341 /* Take apart the current scope and return a c_arg_info structure with
7342 info on a parameter list just parsed.
7344 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7346 ELLIPSIS being true means the argument list ended in '...' so don't
7347 append a sentinel (void_list_node) to the end of the type-list.
7349 EXPR is NULL or an expression that needs to be evaluated for the
7350 side effects of array size expressions in the parameters. */
7352 struct c_arg_info *
7353 get_parm_info (bool ellipsis, tree expr)
7355 struct c_binding *b = current_scope->bindings;
7356 struct c_arg_info *arg_info = build_arg_info ();
7358 tree parms = NULL_TREE;
7359 vec<c_arg_tag, va_gc> *tags = NULL;
7360 tree types = NULL_TREE;
7361 tree others = NULL_TREE;
7363 bool gave_void_only_once_err = false;
7365 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7367 /* The bindings in this scope must not get put into a block.
7368 We will take care of deleting the binding nodes. */
7369 current_scope->bindings = 0;
7371 /* This function is only called if there was *something* on the
7372 parameter list. */
7373 gcc_assert (b);
7375 /* A parameter list consisting solely of 'void' indicates that the
7376 function takes no arguments. But if the 'void' is qualified
7377 (by 'const' or 'volatile'), or has a storage class specifier
7378 ('register'), then the behavior is undefined; issue an error.
7379 Typedefs for 'void' are OK (see DR#157). */
7380 if (b->prev == 0 /* one binding */
7381 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7382 && !DECL_NAME (b->decl) /* anonymous */
7383 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7385 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7386 || C_DECL_REGISTER (b->decl))
7387 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7389 /* There cannot be an ellipsis. */
7390 if (ellipsis)
7391 error_at (b->locus, "%<void%> must be the only parameter");
7393 arg_info->types = void_list_node;
7394 return arg_info;
7397 if (!ellipsis)
7398 types = void_list_node;
7400 /* Break up the bindings list into parms, tags, types, and others;
7401 apply sanity checks; purge the name-to-decl bindings. */
7402 while (b)
7404 tree decl = b->decl;
7405 tree type = TREE_TYPE (decl);
7406 c_arg_tag tag;
7407 const char *keyword;
7409 switch (TREE_CODE (decl))
7411 case PARM_DECL:
7412 if (b->id)
7414 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7415 I_SYMBOL_BINDING (b->id) = b->shadowed;
7418 /* Check for forward decls that never got their actual decl. */
7419 if (TREE_ASM_WRITTEN (decl))
7420 error_at (b->locus,
7421 "parameter %q+D has just a forward declaration", decl);
7422 /* Check for (..., void, ...) and issue an error. */
7423 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7425 if (!gave_void_only_once_err)
7427 error_at (b->locus, "%<void%> must be the only parameter");
7428 gave_void_only_once_err = true;
7431 else
7433 /* Valid parameter, add it to the list. */
7434 DECL_CHAIN (decl) = parms;
7435 parms = decl;
7437 /* Since there is a prototype, args are passed in their
7438 declared types. The back end may override this later. */
7439 DECL_ARG_TYPE (decl) = type;
7440 types = tree_cons (0, type, types);
7442 break;
7444 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7445 case UNION_TYPE: keyword = "union"; goto tag;
7446 case RECORD_TYPE: keyword = "struct"; goto tag;
7447 tag:
7448 /* Types may not have tag-names, in which case the type
7449 appears in the bindings list with b->id NULL. */
7450 if (b->id)
7452 gcc_assert (I_TAG_BINDING (b->id) == b);
7453 I_TAG_BINDING (b->id) = b->shadowed;
7456 /* Warn about any struct, union or enum tags defined in a
7457 parameter list. The scope of such types is limited to
7458 the parameter list, which is rarely if ever desirable
7459 (it's impossible to call such a function with type-
7460 correct arguments). An anonymous union parm type is
7461 meaningful as a GNU extension, so don't warn for that. */
7462 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7464 if (b->id)
7465 /* The %s will be one of 'struct', 'union', or 'enum'. */
7466 warning_at (b->locus, 0,
7467 "%<%s %E%> declared inside parameter list"
7468 " will not be visible outside of this definition or"
7469 " declaration", keyword, b->id);
7470 else
7471 /* The %s will be one of 'struct', 'union', or 'enum'. */
7472 warning_at (b->locus, 0,
7473 "anonymous %s declared inside parameter list"
7474 " will not be visible outside of this definition or"
7475 " declaration", keyword);
7478 tag.id = b->id;
7479 tag.type = decl;
7480 vec_safe_push (tags, tag);
7481 break;
7483 case FUNCTION_DECL:
7484 /* FUNCTION_DECLs appear when there is an implicit function
7485 declaration in the parameter list. */
7486 gcc_assert (b->nested || seen_error ());
7487 goto set_shadowed;
7489 case CONST_DECL:
7490 case TYPE_DECL:
7491 /* CONST_DECLs appear here when we have an embedded enum,
7492 and TYPE_DECLs appear here when we have an embedded struct
7493 or union. No warnings for this - we already warned about the
7494 type itself. */
7496 /* When we reinsert this decl in the function body, we need
7497 to reconstruct whether it was marked as nested. */
7498 gcc_assert (!b->nested);
7499 DECL_CHAIN (decl) = others;
7500 others = decl;
7501 /* fall through */
7503 case ERROR_MARK:
7504 set_shadowed:
7505 /* error_mark_node appears here when we have an undeclared
7506 variable. Just throw it away. */
7507 if (b->id)
7509 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7510 I_SYMBOL_BINDING (b->id) = b->shadowed;
7512 break;
7514 /* Other things that might be encountered. */
7515 case LABEL_DECL:
7516 case VAR_DECL:
7517 default:
7518 gcc_unreachable ();
7521 b = free_binding_and_advance (b);
7524 arg_info->parms = parms;
7525 arg_info->tags = tags;
7526 arg_info->types = types;
7527 arg_info->others = others;
7528 arg_info->pending_sizes = expr;
7529 return arg_info;
7532 /* Get the struct, enum or union (CODE says which) with tag NAME.
7533 Define the tag as a forward-reference with location LOC if it is
7534 not defined. Return a c_typespec structure for the type
7535 specifier. */
7537 struct c_typespec
7538 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7540 struct c_typespec ret;
7541 tree ref;
7542 location_t refloc;
7544 ret.expr = NULL_TREE;
7545 ret.expr_const_operands = true;
7547 /* If a cross reference is requested, look up the type
7548 already defined for this tag and return it. */
7550 ref = lookup_tag (code, name, false, &refloc);
7551 /* If this is the right type of tag, return what we found.
7552 (This reference will be shadowed by shadow_tag later if appropriate.)
7553 If this is the wrong type of tag, do not return it. If it was the
7554 wrong type in the same scope, we will have had an error
7555 message already; if in a different scope and declaring
7556 a name, pending_xref_error will give an error message; but if in a
7557 different scope and not declaring a name, this tag should
7558 shadow the previous declaration of a different type of tag, and
7559 this would not work properly if we return the reference found.
7560 (For example, with "struct foo" in an outer scope, "union foo;"
7561 must shadow that tag with a new one of union type.) */
7562 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7563 if (ref && TREE_CODE (ref) == code)
7565 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7566 && loc != UNKNOWN_LOCATION
7567 && warn_cxx_compat)
7569 switch (code)
7571 case ENUMERAL_TYPE:
7572 warning_at (loc, OPT_Wc___compat,
7573 ("enum type defined in struct or union "
7574 "is not visible in C++"));
7575 inform (refloc, "enum type defined here");
7576 break;
7577 case RECORD_TYPE:
7578 warning_at (loc, OPT_Wc___compat,
7579 ("struct defined in struct or union "
7580 "is not visible in C++"));
7581 inform (refloc, "struct defined here");
7582 break;
7583 case UNION_TYPE:
7584 warning_at (loc, OPT_Wc___compat,
7585 ("union defined in struct or union "
7586 "is not visible in C++"));
7587 inform (refloc, "union defined here");
7588 break;
7589 default:
7590 gcc_unreachable();
7594 ret.spec = ref;
7595 return ret;
7598 /* If no such tag is yet defined, create a forward-reference node
7599 and record it as the "definition".
7600 When a real declaration of this type is found,
7601 the forward-reference will be altered into a real type. */
7603 ref = make_node (code);
7604 if (code == ENUMERAL_TYPE)
7606 /* Give the type a default layout like unsigned int
7607 to avoid crashing if it does not get defined. */
7608 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7609 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7610 TYPE_USER_ALIGN (ref) = 0;
7611 TYPE_UNSIGNED (ref) = 1;
7612 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7613 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7614 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7617 pushtag (loc, name, ref);
7619 ret.spec = ref;
7620 return ret;
7623 /* Get the struct, enum or union (CODE says which) with tag NAME.
7624 Define the tag as a forward-reference if it is not defined.
7625 Return a tree for the type. */
7627 tree
7628 xref_tag (enum tree_code code, tree name)
7630 return parser_xref_tag (input_location, code, name).spec;
7633 /* Make sure that the tag NAME is defined *in the current scope*
7634 at least as a forward reference.
7635 LOC is the location of the struct's definition.
7636 CODE says which kind of tag NAME ought to be.
7638 This stores the current value of the file static STRUCT_PARSE_INFO
7639 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7640 new c_struct_parse_info structure. The old value of
7641 STRUCT_PARSE_INFO is restored in finish_struct. */
7643 tree
7644 start_struct (location_t loc, enum tree_code code, tree name,
7645 struct c_struct_parse_info **enclosing_struct_parse_info)
7647 /* If there is already a tag defined at this scope
7648 (as a forward reference), just return it. */
7650 tree ref = NULL_TREE;
7651 location_t refloc = UNKNOWN_LOCATION;
7653 if (name != NULL_TREE)
7654 ref = lookup_tag (code, name, true, &refloc);
7655 if (ref && TREE_CODE (ref) == code)
7657 if (TYPE_STUB_DECL (ref))
7658 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7660 if (TYPE_SIZE (ref))
7662 if (code == UNION_TYPE)
7663 error_at (loc, "redefinition of %<union %E%>", name);
7664 else
7665 error_at (loc, "redefinition of %<struct %E%>", name);
7666 if (refloc != UNKNOWN_LOCATION)
7667 inform (refloc, "originally defined here");
7668 /* Don't create structures using a name already in use. */
7669 ref = NULL_TREE;
7671 else if (C_TYPE_BEING_DEFINED (ref))
7673 if (code == UNION_TYPE)
7674 error_at (loc, "nested redefinition of %<union %E%>", name);
7675 else
7676 error_at (loc, "nested redefinition of %<struct %E%>", name);
7677 /* Don't bother to report "originally defined here" for a
7678 nested redefinition; the original definition should be
7679 obvious. */
7680 /* Don't create structures that contain themselves. */
7681 ref = NULL_TREE;
7685 /* Otherwise create a forward-reference just so the tag is in scope. */
7687 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7689 ref = make_node (code);
7690 pushtag (loc, name, ref);
7693 C_TYPE_BEING_DEFINED (ref) = 1;
7694 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7695 TYPE_PACKED (v) = flag_pack_struct;
7697 *enclosing_struct_parse_info = struct_parse_info;
7698 struct_parse_info = new c_struct_parse_info ();
7700 /* FIXME: This will issue a warning for a use of a type defined
7701 within a statement expr used within sizeof, et. al. This is not
7702 terribly serious as C++ doesn't permit statement exprs within
7703 sizeof anyhow. */
7704 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7705 warning_at (loc, OPT_Wc___compat,
7706 "defining type in %qs expression is invalid in C++",
7707 (in_sizeof
7708 ? "sizeof"
7709 : (in_typeof ? "typeof" : "alignof")));
7711 return ref;
7714 /* Process the specs, declarator and width (NULL if omitted)
7715 of a structure component, returning a FIELD_DECL node.
7716 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7717 DECL_ATTRS is as for grokdeclarator.
7719 LOC is the location of the structure component.
7721 This is done during the parsing of the struct declaration.
7722 The FIELD_DECL nodes are chained together and the lot of them
7723 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7725 tree
7726 grokfield (location_t loc,
7727 struct c_declarator *declarator, struct c_declspecs *declspecs,
7728 tree width, tree *decl_attrs)
7730 tree value;
7732 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7733 && width == NULL_TREE)
7735 /* This is an unnamed decl.
7737 If we have something of the form "union { list } ;" then this
7738 is the anonymous union extension. Similarly for struct.
7740 If this is something of the form "struct foo;", then
7741 If MS or Plan 9 extensions are enabled, this is handled as
7742 an anonymous struct.
7743 Otherwise this is a forward declaration of a structure tag.
7745 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7746 If foo names a structure or union without a tag, then this
7747 is an anonymous struct (this is permitted by C11).
7748 If MS or Plan 9 extensions are enabled and foo names a
7749 structure, then again this is an anonymous struct.
7750 Otherwise this is an error.
7752 Oh what a horrid tangled web we weave. I wonder if MS consciously
7753 took this from Plan 9 or if it was an accident of implementation
7754 that took root before someone noticed the bug... */
7756 tree type = declspecs->type;
7757 bool ok = false;
7759 if (RECORD_OR_UNION_TYPE_P (type)
7760 && (flag_ms_extensions
7761 || flag_plan9_extensions
7762 || !declspecs->typedef_p))
7764 if (flag_ms_extensions || flag_plan9_extensions)
7765 ok = true;
7766 else if (TYPE_NAME (type) == NULL)
7767 ok = true;
7768 else
7769 ok = false;
7771 if (!ok)
7773 pedwarn (loc, 0, "declaration does not declare anything");
7774 return NULL_TREE;
7776 if (flag_isoc99)
7777 pedwarn_c99 (loc, OPT_Wpedantic,
7778 "ISO C99 doesn%'t support unnamed structs/unions");
7779 else
7780 pedwarn_c99 (loc, OPT_Wpedantic,
7781 "ISO C90 doesn%'t support unnamed structs/unions");
7784 value = grokdeclarator (declarator, declspecs, FIELD, false,
7785 width ? &width : NULL, decl_attrs, NULL, NULL,
7786 DEPRECATED_NORMAL);
7788 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7789 DECL_INITIAL (value) = width;
7790 if (width)
7791 SET_DECL_C_BIT_FIELD (value);
7793 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7795 /* If we currently have a binding for this field, set the
7796 in_struct field in the binding, so that we warn about lookups
7797 which find it. */
7798 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7799 if (b != NULL)
7801 /* If the in_struct field is not yet set, push it on a list
7802 to be cleared when this struct is finished. */
7803 if (!b->in_struct)
7805 struct_parse_info->fields.safe_push (b);
7806 b->in_struct = 1;
7811 return value;
7814 /* Subroutine of detect_field_duplicates: return whether X and Y,
7815 which are both fields in the same struct, have duplicate field
7816 names. */
7818 static bool
7819 is_duplicate_field (tree x, tree y)
7821 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7822 return true;
7824 /* When using -fplan9-extensions, an anonymous field whose name is a
7825 typedef can duplicate a field name. */
7826 if (flag_plan9_extensions
7827 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7829 tree xt, xn, yt, yn;
7831 xt = TREE_TYPE (x);
7832 if (DECL_NAME (x) != NULL_TREE)
7833 xn = DECL_NAME (x);
7834 else if (RECORD_OR_UNION_TYPE_P (xt)
7835 && TYPE_NAME (xt) != NULL_TREE
7836 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7837 xn = DECL_NAME (TYPE_NAME (xt));
7838 else
7839 xn = NULL_TREE;
7841 yt = TREE_TYPE (y);
7842 if (DECL_NAME (y) != NULL_TREE)
7843 yn = DECL_NAME (y);
7844 else if (RECORD_OR_UNION_TYPE_P (yt)
7845 && TYPE_NAME (yt) != NULL_TREE
7846 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7847 yn = DECL_NAME (TYPE_NAME (yt));
7848 else
7849 yn = NULL_TREE;
7851 if (xn != NULL_TREE && xn == yn)
7852 return true;
7855 return false;
7858 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7859 to HTAB, giving errors for any duplicates. */
7861 static void
7862 detect_field_duplicates_hash (tree fieldlist,
7863 hash_table<nofree_ptr_hash <tree_node> > *htab)
7865 tree x, y;
7866 tree_node **slot;
7868 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7869 if ((y = DECL_NAME (x)) != NULL_TREE)
7871 slot = htab->find_slot (y, INSERT);
7872 if (*slot)
7874 error ("duplicate member %q+D", x);
7875 DECL_NAME (x) = NULL_TREE;
7877 *slot = y;
7879 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7881 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7883 /* When using -fplan9-extensions, an anonymous field whose
7884 name is a typedef can duplicate a field name. */
7885 if (flag_plan9_extensions
7886 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7887 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7889 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7890 slot = htab->find_slot (xn, INSERT);
7891 if (*slot)
7892 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7893 *slot = xn;
7898 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7899 the list such that this does not present a problem later. */
7901 static void
7902 detect_field_duplicates (tree fieldlist)
7904 tree x, y;
7905 int timeout = 10;
7907 /* If the struct is the list of instance variables of an Objective-C
7908 class, then we need to check all the instance variables of
7909 superclasses when checking for duplicates (since you can't have
7910 an instance variable in a subclass with the same name as an
7911 instance variable in a superclass). We pass on this job to the
7912 Objective-C compiler. objc_detect_field_duplicates() will return
7913 false if we are not checking the list of instance variables and
7914 the C frontend should proceed with the standard field duplicate
7915 checks. If we are checking the list of instance variables, the
7916 ObjC frontend will do the check, emit the errors if needed, and
7917 then return true. */
7918 if (c_dialect_objc ())
7919 if (objc_detect_field_duplicates (false))
7920 return;
7922 /* First, see if there are more than "a few" fields.
7923 This is trivially true if there are zero or one fields. */
7924 if (!fieldlist || !DECL_CHAIN (fieldlist))
7925 return;
7926 x = fieldlist;
7927 do {
7928 timeout--;
7929 if (DECL_NAME (x) == NULL_TREE
7930 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7931 timeout = 0;
7932 x = DECL_CHAIN (x);
7933 } while (timeout > 0 && x);
7935 /* If there were "few" fields and no anonymous structures or unions,
7936 avoid the overhead of allocating a hash table. Instead just do
7937 the nested traversal thing. */
7938 if (timeout > 0)
7940 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7941 /* When using -fplan9-extensions, we can have duplicates
7942 between typedef names and fields. */
7943 if (DECL_NAME (x)
7944 || (flag_plan9_extensions
7945 && DECL_NAME (x) == NULL_TREE
7946 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7947 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7948 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7950 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7951 if (is_duplicate_field (y, x))
7953 error ("duplicate member %q+D", x);
7954 DECL_NAME (x) = NULL_TREE;
7958 else
7960 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7961 detect_field_duplicates_hash (fieldlist, &htab);
7965 /* Finish up struct info used by -Wc++-compat. */
7967 static void
7968 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7969 location_t record_loc)
7971 unsigned int ix;
7972 tree x;
7973 struct c_binding *b;
7975 if (fieldlist == NULL_TREE)
7977 if (code == RECORD_TYPE)
7978 warning_at (record_loc, OPT_Wc___compat,
7979 "empty struct has size 0 in C, size 1 in C++");
7980 else
7981 warning_at (record_loc, OPT_Wc___compat,
7982 "empty union has size 0 in C, size 1 in C++");
7985 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7986 the current struct. We do this now at the end of the struct
7987 because the flag is used to issue visibility warnings, and we
7988 only want to issue those warnings if the type is referenced
7989 outside of the struct declaration. */
7990 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7991 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7993 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7994 typedefs used when declaring fields in this struct. If the name
7995 of any of the fields is also a typedef name then the struct would
7996 not parse in C++, because the C++ lookup rules say that the
7997 typedef name would be looked up in the context of the struct, and
7998 would thus be the field rather than the typedef. */
7999 if (!struct_parse_info->typedefs_seen.is_empty ()
8000 && fieldlist != NULL_TREE)
8002 /* Use a hash_set<tree> using the name of the typedef. We can use
8003 a hash_set<tree> because identifiers are interned. */
8004 hash_set<tree> tset;
8006 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8007 tset.add (DECL_NAME (x));
8009 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8011 if (DECL_NAME (x) != NULL_TREE
8012 && tset.contains (DECL_NAME (x)))
8014 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8015 ("using %qD as both field and typedef name is "
8016 "invalid in C++"),
8018 /* FIXME: It would be nice to report the location where
8019 the typedef name is used. */
8024 /* For each field which has a binding and which was not defined in
8025 an enclosing struct, clear the in_struct field. */
8026 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8027 b->in_struct = 0;
8030 /* Function to help qsort sort FIELD_DECLs by name order. */
8032 static int
8033 field_decl_cmp (const void *x_p, const void *y_p)
8035 const tree *const x = (const tree *) x_p;
8036 const tree *const y = (const tree *) y_p;
8038 if (DECL_NAME (*x) == DECL_NAME (*y))
8039 /* A nontype is "greater" than a type. */
8040 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8041 if (DECL_NAME (*x) == NULL_TREE)
8042 return -1;
8043 if (DECL_NAME (*y) == NULL_TREE)
8044 return 1;
8045 if (DECL_NAME (*x) < DECL_NAME (*y))
8046 return -1;
8047 return 1;
8050 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8051 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8052 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8053 ATTRIBUTES are attributes to be applied to the structure.
8055 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8056 the struct was started. */
8058 tree
8059 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8060 struct c_struct_parse_info *enclosing_struct_parse_info)
8062 tree x;
8063 bool toplevel = file_scope == current_scope;
8065 /* If this type was previously laid out as a forward reference,
8066 make sure we lay it out again. */
8068 TYPE_SIZE (t) = NULL_TREE;
8070 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8072 if (pedantic)
8074 for (x = fieldlist; x; x = DECL_CHAIN (x))
8076 if (DECL_NAME (x) != NULL_TREE)
8077 break;
8078 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8079 break;
8082 if (x == NULL_TREE)
8084 if (TREE_CODE (t) == UNION_TYPE)
8086 if (fieldlist)
8087 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8088 else
8089 pedwarn (loc, OPT_Wpedantic, "union has no members");
8091 else
8093 if (fieldlist)
8094 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8095 else
8096 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8101 /* Install struct as DECL_CONTEXT of each field decl.
8102 Also process specified field sizes, found in the DECL_INITIAL,
8103 storing 0 there after the type has been changed to precision equal
8104 to its width, rather than the precision of the specified standard
8105 type. (Correct layout requires the original type to have been preserved
8106 until now.) */
8108 bool saw_named_field = false;
8109 for (x = fieldlist; x; x = DECL_CHAIN (x))
8111 if (TREE_TYPE (x) == error_mark_node)
8112 continue;
8114 DECL_CONTEXT (x) = t;
8116 /* If any field is const, the structure type is pseudo-const. */
8117 if (TREE_READONLY (x))
8118 C_TYPE_FIELDS_READONLY (t) = 1;
8119 else
8121 /* A field that is pseudo-const makes the structure likewise. */
8122 tree t1 = strip_array_types (TREE_TYPE (x));
8123 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8124 C_TYPE_FIELDS_READONLY (t) = 1;
8127 /* Any field that is volatile means variables of this type must be
8128 treated in some ways as volatile. */
8129 if (TREE_THIS_VOLATILE (x))
8130 C_TYPE_FIELDS_VOLATILE (t) = 1;
8132 /* Any field of nominal variable size implies structure is too. */
8133 if (C_DECL_VARIABLE_SIZE (x))
8134 C_TYPE_VARIABLE_SIZE (t) = 1;
8136 if (DECL_C_BIT_FIELD (x))
8138 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8139 DECL_SIZE (x) = bitsize_int (width);
8140 DECL_BIT_FIELD (x) = 1;
8143 if (TYPE_PACKED (t)
8144 && (DECL_BIT_FIELD (x)
8145 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8146 DECL_PACKED (x) = 1;
8148 /* Detect flexible array member in an invalid context. */
8149 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8150 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8151 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8152 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8154 if (TREE_CODE (t) == UNION_TYPE)
8156 error_at (DECL_SOURCE_LOCATION (x),
8157 "flexible array member in union");
8158 TREE_TYPE (x) = error_mark_node;
8160 else if (DECL_CHAIN (x) != NULL_TREE)
8162 error_at (DECL_SOURCE_LOCATION (x),
8163 "flexible array member not at end of struct");
8164 TREE_TYPE (x) = error_mark_node;
8166 else if (!saw_named_field)
8168 error_at (DECL_SOURCE_LOCATION (x),
8169 "flexible array member in a struct with no named "
8170 "members");
8171 TREE_TYPE (x) = error_mark_node;
8175 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8176 && flexible_array_type_p (TREE_TYPE (x)))
8177 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8178 "invalid use of structure with flexible array member");
8180 if (DECL_NAME (x)
8181 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8182 saw_named_field = true;
8185 detect_field_duplicates (fieldlist);
8187 /* Now we have the nearly final fieldlist. Record it,
8188 then lay out the structure or union (including the fields). */
8190 TYPE_FIELDS (t) = fieldlist;
8192 maybe_apply_pragma_scalar_storage_order (t);
8194 layout_type (t);
8196 if (TYPE_SIZE_UNIT (t)
8197 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8198 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8199 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8200 error ("type %qT is too large", t);
8202 /* Give bit-fields their proper types and rewrite the type of array fields
8203 with scalar component if the enclosing type has reverse storage order. */
8204 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8206 if (TREE_CODE (field) == FIELD_DECL
8207 && DECL_INITIAL (field)
8208 && TREE_TYPE (field) != error_mark_node)
8210 unsigned HOST_WIDE_INT width
8211 = tree_to_uhwi (DECL_INITIAL (field));
8212 tree type = TREE_TYPE (field);
8213 if (width != TYPE_PRECISION (type))
8215 TREE_TYPE (field)
8216 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8217 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8219 DECL_INITIAL (field) = NULL_TREE;
8221 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8222 && TREE_CODE (field) == FIELD_DECL
8223 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8225 tree ftype = TREE_TYPE (field);
8226 tree ctype = strip_array_types (ftype);
8227 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8229 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8230 tree *typep = &fmain_type;
8231 do {
8232 *typep = build_distinct_type_copy (*typep);
8233 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8234 typep = &TREE_TYPE (*typep);
8235 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8236 TREE_TYPE (field)
8237 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8242 /* Now we have the truly final field list.
8243 Store it in this type and in the variants. */
8245 TYPE_FIELDS (t) = fieldlist;
8247 /* If there are lots of fields, sort so we can look through them fast.
8248 We arbitrarily consider 16 or more elts to be "a lot". */
8251 int len = 0;
8253 for (x = fieldlist; x; x = DECL_CHAIN (x))
8255 if (len > 15 || DECL_NAME (x) == NULL)
8256 break;
8257 len += 1;
8260 if (len > 15)
8262 tree *field_array;
8263 struct lang_type *space;
8264 struct sorted_fields_type *space2;
8266 len += list_length (x);
8268 /* Use the same allocation policy here that make_node uses, to
8269 ensure that this lives as long as the rest of the struct decl.
8270 All decls in an inline function need to be saved. */
8272 space = ggc_cleared_alloc<struct lang_type> ();
8273 space2 = (sorted_fields_type *) ggc_internal_alloc
8274 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8276 len = 0;
8277 space->s = space2;
8278 field_array = &space2->elts[0];
8279 for (x = fieldlist; x; x = DECL_CHAIN (x))
8281 field_array[len++] = x;
8283 /* If there is anonymous struct or union, break out of the loop. */
8284 if (DECL_NAME (x) == NULL)
8285 break;
8287 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8288 if (x == NULL)
8290 TYPE_LANG_SPECIFIC (t) = space;
8291 TYPE_LANG_SPECIFIC (t)->s->len = len;
8292 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8293 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8298 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8299 in dwarf2out via rest_of_decl_compilation below and means
8300 something totally different. Since we will be clearing
8301 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8302 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8303 C_TYPE_INCOMPLETE_VARS should use some language specific
8304 node. */
8305 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8306 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8308 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8309 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8310 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8311 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8312 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8313 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8316 /* If this was supposed to be a transparent union, but we can't
8317 make it one, warn and turn off the flag. */
8318 if (TREE_CODE (t) == UNION_TYPE
8319 && TYPE_TRANSPARENT_AGGR (t)
8320 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8322 TYPE_TRANSPARENT_AGGR (t) = 0;
8323 warning_at (loc, 0, "union cannot be made transparent");
8326 /* Update type location to the one of the definition, instead of e.g.
8327 a forward declaration. */
8328 if (TYPE_STUB_DECL (t))
8329 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8331 /* Finish debugging output for this type. */
8332 rest_of_type_compilation (t, toplevel);
8334 /* If this structure or union completes the type of any previous
8335 variable declaration, lay it out and output its rtl. */
8336 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8338 tree decl = TREE_VALUE (x);
8339 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8340 layout_array_type (TREE_TYPE (decl));
8341 if (TREE_CODE (decl) != TYPE_DECL)
8343 layout_decl (decl, 0);
8344 if (c_dialect_objc ())
8345 objc_check_decl (decl);
8346 rest_of_decl_compilation (decl, toplevel, 0);
8350 /* If we're inside a function proper, i.e. not file-scope and not still
8351 parsing parameters, then arrange for the size of a variable sized type
8352 to be bound now. */
8353 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8354 add_stmt (build_stmt (loc,
8355 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8357 if (warn_cxx_compat)
8358 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8360 delete struct_parse_info;
8362 struct_parse_info = enclosing_struct_parse_info;
8364 /* If this struct is defined inside a struct, add it to
8365 struct_types. */
8366 if (warn_cxx_compat
8367 && struct_parse_info != NULL
8368 && !in_sizeof && !in_typeof && !in_alignof)
8369 struct_parse_info->struct_types.safe_push (t);
8371 return t;
8374 static struct {
8375 gt_pointer_operator new_value;
8376 void *cookie;
8377 } resort_data;
8379 /* This routine compares two fields like field_decl_cmp but using the
8380 pointer operator in resort_data. */
8382 static int
8383 resort_field_decl_cmp (const void *x_p, const void *y_p)
8385 const tree *const x = (const tree *) x_p;
8386 const tree *const y = (const tree *) y_p;
8388 if (DECL_NAME (*x) == DECL_NAME (*y))
8389 /* A nontype is "greater" than a type. */
8390 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8391 if (DECL_NAME (*x) == NULL_TREE)
8392 return -1;
8393 if (DECL_NAME (*y) == NULL_TREE)
8394 return 1;
8396 tree d1 = DECL_NAME (*x);
8397 tree d2 = DECL_NAME (*y);
8398 resort_data.new_value (&d1, resort_data.cookie);
8399 resort_data.new_value (&d2, resort_data.cookie);
8400 if (d1 < d2)
8401 return -1;
8403 return 1;
8406 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8408 void
8409 resort_sorted_fields (void *obj,
8410 void * ARG_UNUSED (orig_obj),
8411 gt_pointer_operator new_value,
8412 void *cookie)
8414 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8415 resort_data.new_value = new_value;
8416 resort_data.cookie = cookie;
8417 qsort (&sf->elts[0], sf->len, sizeof (tree),
8418 resort_field_decl_cmp);
8421 /* Lay out the type T, and its element type, and so on. */
8423 static void
8424 layout_array_type (tree t)
8426 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8427 layout_array_type (TREE_TYPE (t));
8428 layout_type (t);
8431 /* Begin compiling the definition of an enumeration type.
8432 NAME is its name (or null if anonymous).
8433 LOC is the enum's location.
8434 Returns the type object, as yet incomplete.
8435 Also records info about it so that build_enumerator
8436 may be used to declare the individual values as they are read. */
8438 tree
8439 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8441 tree enumtype = NULL_TREE;
8442 location_t enumloc = UNKNOWN_LOCATION;
8444 /* If this is the real definition for a previous forward reference,
8445 fill in the contents in the same object that used to be the
8446 forward reference. */
8448 if (name != NULL_TREE)
8449 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8451 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8453 enumtype = make_node (ENUMERAL_TYPE);
8454 pushtag (loc, name, enumtype);
8456 /* Update type location to the one of the definition, instead of e.g.
8457 a forward declaration. */
8458 else if (TYPE_STUB_DECL (enumtype))
8460 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8461 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8464 if (C_TYPE_BEING_DEFINED (enumtype))
8465 error_at (loc, "nested redefinition of %<enum %E%>", name);
8467 C_TYPE_BEING_DEFINED (enumtype) = 1;
8469 if (TYPE_VALUES (enumtype) != NULL_TREE)
8471 /* This enum is a named one that has been declared already. */
8472 error_at (loc, "redeclaration of %<enum %E%>", name);
8473 if (enumloc != UNKNOWN_LOCATION)
8474 inform (enumloc, "originally defined here");
8476 /* Completely replace its old definition.
8477 The old enumerators remain defined, however. */
8478 TYPE_VALUES (enumtype) = NULL_TREE;
8481 the_enum->enum_next_value = integer_zero_node;
8482 the_enum->enum_overflow = 0;
8484 if (flag_short_enums)
8485 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8486 TYPE_PACKED (v) = 1;
8488 /* FIXME: This will issue a warning for a use of a type defined
8489 within sizeof in a statement expr. This is not terribly serious
8490 as C++ doesn't permit statement exprs within sizeof anyhow. */
8491 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8492 warning_at (loc, OPT_Wc___compat,
8493 "defining type in %qs expression is invalid in C++",
8494 (in_sizeof
8495 ? "sizeof"
8496 : (in_typeof ? "typeof" : "alignof")));
8498 return enumtype;
8501 /* After processing and defining all the values of an enumeration type,
8502 install their decls in the enumeration type and finish it off.
8503 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8504 and ATTRIBUTES are the specified attributes.
8505 Returns ENUMTYPE. */
8507 tree
8508 finish_enum (tree enumtype, tree values, tree attributes)
8510 tree pair, tem;
8511 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8512 int precision;
8513 signop sign;
8514 bool toplevel = (file_scope == current_scope);
8515 struct lang_type *lt;
8517 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8519 /* Calculate the maximum value of any enumerator in this type. */
8521 if (values == error_mark_node)
8522 minnode = maxnode = integer_zero_node;
8523 else
8525 minnode = maxnode = TREE_VALUE (values);
8526 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8528 tree value = TREE_VALUE (pair);
8529 if (tree_int_cst_lt (maxnode, value))
8530 maxnode = value;
8531 if (tree_int_cst_lt (value, minnode))
8532 minnode = value;
8536 /* Construct the final type of this enumeration. It is the same
8537 as one of the integral types - the narrowest one that fits, except
8538 that normally we only go as narrow as int - and signed iff any of
8539 the values are negative. */
8540 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8541 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8542 tree_int_cst_min_precision (maxnode, sign));
8544 /* If the precision of the type was specified with an attribute and it
8545 was too small, give an error. Otherwise, use it. */
8546 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8548 if (precision > TYPE_PRECISION (enumtype))
8550 TYPE_PRECISION (enumtype) = 0;
8551 error ("specified mode too small for enumeral values");
8553 else
8554 precision = TYPE_PRECISION (enumtype);
8556 else
8557 TYPE_PRECISION (enumtype) = 0;
8559 if (TYPE_PACKED (enumtype)
8560 || precision > TYPE_PRECISION (integer_type_node)
8561 || TYPE_PRECISION (enumtype))
8563 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8564 if (tem == NULL)
8566 warning (0, "enumeration values exceed range of largest integer");
8567 tem = long_long_integer_type_node;
8570 else
8571 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8573 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8574 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8575 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8576 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8577 TYPE_SIZE (enumtype) = NULL_TREE;
8578 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8580 layout_type (enumtype);
8582 if (values != error_mark_node)
8584 /* Change the type of the enumerators to be the enum type. We
8585 need to do this irrespective of the size of the enum, for
8586 proper type checking. Replace the DECL_INITIALs of the
8587 enumerators, and the value slots of the list, with copies
8588 that have the enum type; they cannot be modified in place
8589 because they may be shared (e.g. integer_zero_node) Finally,
8590 change the purpose slots to point to the names of the decls. */
8591 for (pair = values; pair; pair = TREE_CHAIN (pair))
8593 tree enu = TREE_PURPOSE (pair);
8594 tree ini = DECL_INITIAL (enu);
8596 TREE_TYPE (enu) = enumtype;
8598 /* The ISO C Standard mandates enumerators to have type int,
8599 even though the underlying type of an enum type is
8600 unspecified. However, GCC allows enumerators of any
8601 integer type as an extensions. build_enumerator()
8602 converts any enumerators that fit in an int to type int,
8603 to avoid promotions to unsigned types when comparing
8604 integers with enumerators that fit in the int range.
8605 When -pedantic is given, build_enumerator() would have
8606 already warned about those that don't fit. Here we
8607 convert the rest to the enumerator type. */
8608 if (TREE_TYPE (ini) != integer_type_node)
8609 ini = convert (enumtype, ini);
8611 DECL_INITIAL (enu) = ini;
8612 TREE_PURPOSE (pair) = DECL_NAME (enu);
8613 TREE_VALUE (pair) = ini;
8616 TYPE_VALUES (enumtype) = values;
8619 /* Record the min/max values so that we can warn about bit-field
8620 enumerations that are too small for the values. */
8621 lt = ggc_cleared_alloc<struct lang_type> ();
8622 lt->enum_min = minnode;
8623 lt->enum_max = maxnode;
8624 TYPE_LANG_SPECIFIC (enumtype) = lt;
8626 /* Fix up all variant types of this enum type. */
8627 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8629 if (tem == enumtype)
8630 continue;
8631 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8632 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8633 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8634 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8635 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8636 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8637 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8638 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8639 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8640 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8641 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8644 /* Finish debugging output for this type. */
8645 rest_of_type_compilation (enumtype, toplevel);
8647 /* If this enum is defined inside a struct, add it to
8648 struct_types. */
8649 if (warn_cxx_compat
8650 && struct_parse_info != NULL
8651 && !in_sizeof && !in_typeof && !in_alignof)
8652 struct_parse_info->struct_types.safe_push (enumtype);
8654 return enumtype;
8657 /* Build and install a CONST_DECL for one value of the
8658 current enumeration type (one that was begun with start_enum).
8659 DECL_LOC is the location of the enumerator.
8660 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8661 Return a tree-list containing the CONST_DECL and its value.
8662 Assignment of sequential values by default is handled here. */
8664 tree
8665 build_enumerator (location_t decl_loc, location_t loc,
8666 struct c_enum_contents *the_enum, tree name, tree value)
8668 tree decl, type;
8670 /* Validate and default VALUE. */
8672 if (value != NULL_TREE)
8674 /* Don't issue more errors for error_mark_node (i.e. an
8675 undeclared identifier) - just ignore the value expression. */
8676 if (value == error_mark_node)
8677 value = NULL_TREE;
8678 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8680 error_at (loc, "enumerator value for %qE is not an integer constant",
8681 name);
8682 value = NULL_TREE;
8684 else
8686 if (TREE_CODE (value) != INTEGER_CST)
8688 value = c_fully_fold (value, false, NULL);
8689 if (TREE_CODE (value) == INTEGER_CST)
8690 pedwarn (loc, OPT_Wpedantic,
8691 "enumerator value for %qE is not an integer "
8692 "constant expression", name);
8694 if (TREE_CODE (value) != INTEGER_CST)
8696 error ("enumerator value for %qE is not an integer constant",
8697 name);
8698 value = NULL_TREE;
8700 else
8702 value = default_conversion (value);
8703 constant_expression_warning (value);
8708 /* Default based on previous value. */
8709 /* It should no longer be possible to have NON_LVALUE_EXPR
8710 in the default. */
8711 if (value == NULL_TREE)
8713 value = the_enum->enum_next_value;
8714 if (the_enum->enum_overflow)
8715 error_at (loc, "overflow in enumeration values");
8717 /* Even though the underlying type of an enum is unspecified, the
8718 type of enumeration constants is explicitly defined as int
8719 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8720 an extension. */
8721 else if (!int_fits_type_p (value, integer_type_node))
8722 pedwarn (loc, OPT_Wpedantic,
8723 "ISO C restricts enumerator values to range of %<int%>");
8725 /* The ISO C Standard mandates enumerators to have type int, even
8726 though the underlying type of an enum type is unspecified.
8727 However, GCC allows enumerators of any integer type as an
8728 extensions. Here we convert any enumerators that fit in an int
8729 to type int, to avoid promotions to unsigned types when comparing
8730 integers with enumerators that fit in the int range. When
8731 -pedantic is given, we would have already warned about those that
8732 don't fit. We have to do this here rather than in finish_enum
8733 because this value may be used to define more enumerators. */
8734 if (int_fits_type_p (value, integer_type_node))
8735 value = convert (integer_type_node, value);
8737 /* Set basis for default for next value. */
8738 the_enum->enum_next_value
8739 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8740 PLUS_EXPR, value, integer_one_node, false);
8741 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8743 /* Now create a declaration for the enum value name. */
8745 type = TREE_TYPE (value);
8746 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8747 TYPE_PRECISION (integer_type_node)),
8748 (TYPE_PRECISION (type)
8749 >= TYPE_PRECISION (integer_type_node)
8750 && TYPE_UNSIGNED (type)));
8752 decl = build_decl (decl_loc, CONST_DECL, name, type);
8753 DECL_INITIAL (decl) = convert (type, value);
8754 pushdecl (decl);
8756 return tree_cons (decl, value, NULL_TREE);
8760 /* Create the FUNCTION_DECL for a function definition.
8761 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8762 the declaration; they describe the function's name and the type it returns,
8763 but twisted together in a fashion that parallels the syntax of C.
8765 This function creates a binding context for the function body
8766 as well as setting up the FUNCTION_DECL in current_function_decl.
8768 Returns true on success. If the DECLARATOR is not suitable for a function
8769 (it defines a datum instead), we return false to report a parse error. */
8771 bool
8772 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8773 tree attributes)
8775 tree decl1, old_decl;
8776 tree restype, resdecl;
8777 location_t loc;
8779 current_function_returns_value = 0; /* Assume, until we see it does. */
8780 current_function_returns_null = 0;
8781 current_function_returns_abnormally = 0;
8782 warn_about_return_type = 0;
8783 c_switch_stack = NULL;
8785 /* Indicate no valid break/continue context by setting these variables
8786 to some non-null, non-label value. We'll notice and emit the proper
8787 error message in c_finish_bc_stmt. */
8788 c_break_label = c_cont_label = size_zero_node;
8790 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8791 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8792 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8794 /* If the declarator is not suitable for a function definition,
8795 cause a syntax error. */
8796 if (decl1 == NULL_TREE
8797 || TREE_CODE (decl1) != FUNCTION_DECL)
8798 return false;
8800 loc = DECL_SOURCE_LOCATION (decl1);
8802 c_decl_attributes (&decl1, attributes, 0);
8804 if (DECL_DECLARED_INLINE_P (decl1)
8805 && DECL_UNINLINABLE (decl1)
8806 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8807 warning_at (loc, OPT_Wattributes,
8808 "inline function %qD given attribute noinline",
8809 decl1);
8811 /* Handle gnu_inline attribute. */
8812 if (declspecs->inline_p
8813 && !flag_gnu89_inline
8814 && TREE_CODE (decl1) == FUNCTION_DECL
8815 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8816 || current_function_decl))
8818 if (declspecs->storage_class != csc_static)
8819 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8822 announce_function (decl1);
8824 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8826 error_at (loc, "return type is an incomplete type");
8827 /* Make it return void instead. */
8828 TREE_TYPE (decl1)
8829 = build_function_type (void_type_node,
8830 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8833 if (warn_about_return_type)
8834 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8835 : (warn_return_type > 0 ? OPT_Wreturn_type
8836 : OPT_Wimplicit_int),
8837 "return type defaults to %<int%>");
8839 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8840 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8841 DECL_INITIAL (decl1) = error_mark_node;
8843 /* A nested function is not global. */
8844 if (current_function_decl != NULL_TREE)
8845 TREE_PUBLIC (decl1) = 0;
8847 /* If this definition isn't a prototype and we had a prototype declaration
8848 before, copy the arg type info from that prototype. */
8849 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8850 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8851 old_decl = NULL_TREE;
8852 current_function_prototype_locus = UNKNOWN_LOCATION;
8853 current_function_prototype_built_in = false;
8854 current_function_prototype_arg_types = NULL_TREE;
8855 if (!prototype_p (TREE_TYPE (decl1)))
8857 if (old_decl != NULL_TREE
8858 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8859 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8860 TREE_TYPE (TREE_TYPE (old_decl))))
8862 if (stdarg_p (TREE_TYPE (old_decl)))
8864 auto_diagnostic_group d;
8865 warning_at (loc, 0, "%q+D defined as variadic function "
8866 "without prototype", decl1);
8867 locate_old_decl (old_decl);
8869 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8870 TREE_TYPE (decl1));
8871 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8872 current_function_prototype_built_in
8873 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8874 current_function_prototype_arg_types
8875 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8877 if (TREE_PUBLIC (decl1))
8879 /* If there is an external prototype declaration of this
8880 function, record its location but do not copy information
8881 to this decl. This may be an invisible declaration
8882 (built-in or in a scope which has finished) or simply
8883 have more refined argument types than any declaration
8884 found above. */
8885 struct c_binding *b;
8886 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8887 if (B_IN_SCOPE (b, external_scope))
8888 break;
8889 if (b)
8891 tree ext_decl, ext_type;
8892 ext_decl = b->decl;
8893 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8894 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8895 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8896 TREE_TYPE (ext_type)))
8898 current_function_prototype_locus
8899 = DECL_SOURCE_LOCATION (ext_decl);
8900 current_function_prototype_built_in
8901 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8902 current_function_prototype_arg_types
8903 = TYPE_ARG_TYPES (ext_type);
8909 /* Optionally warn of old-fashioned def with no previous prototype. */
8910 if (warn_strict_prototypes
8911 && old_decl != error_mark_node
8912 && !prototype_p (TREE_TYPE (decl1))
8913 && C_DECL_ISNT_PROTOTYPE (old_decl))
8914 warning_at (loc, OPT_Wstrict_prototypes,
8915 "function declaration isn%'t a prototype");
8916 /* Optionally warn of any global def with no previous prototype. */
8917 else if (warn_missing_prototypes
8918 && old_decl != error_mark_node
8919 && TREE_PUBLIC (decl1)
8920 && !MAIN_NAME_P (DECL_NAME (decl1))
8921 && C_DECL_ISNT_PROTOTYPE (old_decl)
8922 && !DECL_DECLARED_INLINE_P (decl1))
8923 warning_at (loc, OPT_Wmissing_prototypes,
8924 "no previous prototype for %qD", decl1);
8925 /* Optionally warn of any def with no previous prototype
8926 if the function has already been used. */
8927 else if (warn_missing_prototypes
8928 && old_decl != NULL_TREE
8929 && old_decl != error_mark_node
8930 && TREE_USED (old_decl)
8931 && !prototype_p (TREE_TYPE (old_decl)))
8932 warning_at (loc, OPT_Wmissing_prototypes,
8933 "%qD was used with no prototype before its definition", decl1);
8934 /* Optionally warn of any global def with no previous declaration. */
8935 else if (warn_missing_declarations
8936 && TREE_PUBLIC (decl1)
8937 && old_decl == NULL_TREE
8938 && !MAIN_NAME_P (DECL_NAME (decl1))
8939 && !DECL_DECLARED_INLINE_P (decl1))
8940 warning_at (loc, OPT_Wmissing_declarations,
8941 "no previous declaration for %qD",
8942 decl1);
8943 /* Optionally warn of any def with no previous declaration
8944 if the function has already been used. */
8945 else if (warn_missing_declarations
8946 && old_decl != NULL_TREE
8947 && old_decl != error_mark_node
8948 && TREE_USED (old_decl)
8949 && C_DECL_IMPLICIT (old_decl))
8950 warning_at (loc, OPT_Wmissing_declarations,
8951 "%qD was used with no declaration before its definition", decl1);
8953 /* This function exists in static storage.
8954 (This does not mean `static' in the C sense!) */
8955 TREE_STATIC (decl1) = 1;
8957 /* This is the earliest point at which we might know the assembler
8958 name of the function. Thus, if it's set before this, die horribly. */
8959 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8961 /* If #pragma weak was used, mark the decl weak now. */
8962 if (current_scope == file_scope)
8963 maybe_apply_pragma_weak (decl1);
8965 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8966 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8968 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8969 != integer_type_node)
8970 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8971 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8972 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8973 decl1);
8975 check_main_parameter_types (decl1);
8977 if (!TREE_PUBLIC (decl1))
8978 pedwarn (loc, OPT_Wmain,
8979 "%qD is normally a non-static function", decl1);
8982 /* Record the decl so that the function name is defined.
8983 If we already have a decl for this name, and it is a FUNCTION_DECL,
8984 use the old decl. */
8986 current_function_decl = pushdecl (decl1);
8988 push_scope ();
8989 declare_parm_level ();
8991 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8992 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8993 DECL_ARTIFICIAL (resdecl) = 1;
8994 DECL_IGNORED_P (resdecl) = 1;
8995 DECL_RESULT (current_function_decl) = resdecl;
8997 start_fname_decls ();
8999 return true;
9002 /* Subroutine of store_parm_decls which handles new-style function
9003 definitions (prototype format). The parms already have decls, so we
9004 need only record them as in effect and complain if any redundant
9005 old-style parm decls were written. */
9006 static void
9007 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9009 tree decl;
9010 c_arg_tag *tag;
9011 unsigned ix;
9013 if (current_scope->bindings)
9015 error_at (DECL_SOURCE_LOCATION (fndecl),
9016 "old-style parameter declarations in prototyped "
9017 "function definition");
9019 /* Get rid of the old-style declarations. */
9020 pop_scope ();
9021 push_scope ();
9023 /* Don't issue this warning for nested functions, and don't issue this
9024 warning if we got here because ARG_INFO_TYPES was error_mark_node
9025 (this happens when a function definition has just an ellipsis in
9026 its parameter list). */
9027 else if (!in_system_header_at (input_location)
9028 && !current_function_scope
9029 && arg_info->types != error_mark_node)
9030 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9031 "traditional C rejects ISO C style function definitions");
9033 /* Now make all the parameter declarations visible in the function body.
9034 We can bypass most of the grunt work of pushdecl. */
9035 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9037 DECL_CONTEXT (decl) = current_function_decl;
9038 if (DECL_NAME (decl))
9040 bind (DECL_NAME (decl), decl, current_scope,
9041 /*invisible=*/false, /*nested=*/false,
9042 UNKNOWN_LOCATION);
9043 if (!TREE_USED (decl))
9044 warn_if_shadowing (decl);
9046 else
9047 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9050 /* Record the parameter list in the function declaration. */
9051 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9053 /* Now make all the ancillary declarations visible, likewise. */
9054 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9056 DECL_CONTEXT (decl) = current_function_decl;
9057 if (DECL_NAME (decl))
9058 bind (DECL_NAME (decl), decl, current_scope,
9059 /*invisible=*/false,
9060 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9061 UNKNOWN_LOCATION);
9064 /* And all the tag declarations. */
9065 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9066 if (tag->id)
9067 bind (tag->id, tag->type, current_scope,
9068 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9071 /* Subroutine of store_parm_decls which handles old-style function
9072 definitions (separate parameter list and declarations). */
9074 static void
9075 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9077 struct c_binding *b;
9078 tree parm, decl, last;
9079 tree parmids = arg_info->parms;
9080 hash_set<tree> seen_args;
9082 if (!in_system_header_at (input_location))
9083 warning_at (DECL_SOURCE_LOCATION (fndecl),
9084 OPT_Wold_style_definition, "old-style function definition");
9086 /* Match each formal parameter name with its declaration. Save each
9087 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9088 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9090 if (TREE_VALUE (parm) == NULL_TREE)
9092 error_at (DECL_SOURCE_LOCATION (fndecl),
9093 "parameter name missing from parameter list");
9094 TREE_PURPOSE (parm) = NULL_TREE;
9095 continue;
9098 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9099 if (b && B_IN_CURRENT_SCOPE (b))
9101 decl = b->decl;
9102 /* Skip erroneous parameters. */
9103 if (decl == error_mark_node)
9104 continue;
9105 /* If we got something other than a PARM_DECL it is an error. */
9106 if (TREE_CODE (decl) != PARM_DECL)
9108 error_at (DECL_SOURCE_LOCATION (decl),
9109 "%qD declared as a non-parameter", decl);
9110 continue;
9112 /* If the declaration is already marked, we have a duplicate
9113 name. Complain and ignore the duplicate. */
9114 else if (seen_args.contains (decl))
9116 error_at (DECL_SOURCE_LOCATION (decl),
9117 "multiple parameters named %qD", decl);
9118 TREE_PURPOSE (parm) = NULL_TREE;
9119 continue;
9121 /* If the declaration says "void", complain and turn it into
9122 an int. */
9123 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9125 error_at (DECL_SOURCE_LOCATION (decl),
9126 "parameter %qD declared with void type", decl);
9127 TREE_TYPE (decl) = integer_type_node;
9128 DECL_ARG_TYPE (decl) = integer_type_node;
9129 layout_decl (decl, 0);
9131 warn_if_shadowing (decl);
9133 /* If no declaration found, default to int. */
9134 else
9136 /* FIXME diagnostics: This should be the location of the argument,
9137 not the FNDECL. E.g., for an old-style declaration
9139 int f10(v) { blah; }
9141 We should use the location of the V, not the F10.
9142 Unfortunately, the V is an IDENTIFIER_NODE which has no
9143 location. In the future we need locations for c_arg_info
9144 entries.
9146 See gcc.dg/Wshadow-3.c for an example of this problem. */
9147 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9148 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9149 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9150 pushdecl (decl);
9151 warn_if_shadowing (decl);
9153 if (flag_isoc99)
9154 pedwarn (DECL_SOURCE_LOCATION (decl),
9155 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9156 decl);
9157 else
9158 warning_at (DECL_SOURCE_LOCATION (decl),
9159 OPT_Wmissing_parameter_type,
9160 "type of %qD defaults to %<int%>", decl);
9163 TREE_PURPOSE (parm) = decl;
9164 seen_args.add (decl);
9167 /* Now examine the parms chain for incomplete declarations
9168 and declarations with no corresponding names. */
9170 for (b = current_scope->bindings; b; b = b->prev)
9172 parm = b->decl;
9173 if (TREE_CODE (parm) != PARM_DECL)
9174 continue;
9176 if (TREE_TYPE (parm) != error_mark_node
9177 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9179 error_at (DECL_SOURCE_LOCATION (parm),
9180 "parameter %qD has incomplete type", parm);
9181 TREE_TYPE (parm) = error_mark_node;
9184 if (!seen_args.contains (parm))
9186 error_at (DECL_SOURCE_LOCATION (parm),
9187 "declaration for parameter %qD but no such parameter",
9188 parm);
9190 /* Pretend the parameter was not missing.
9191 This gets us to a standard state and minimizes
9192 further error messages. */
9193 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9197 /* Chain the declarations together in the order of the list of
9198 names. Store that chain in the function decl, replacing the
9199 list of names. Update the current scope to match. */
9200 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9202 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9203 if (TREE_PURPOSE (parm))
9204 break;
9205 if (parm && TREE_PURPOSE (parm))
9207 last = TREE_PURPOSE (parm);
9208 DECL_ARGUMENTS (fndecl) = last;
9210 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9211 if (TREE_PURPOSE (parm))
9213 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9214 last = TREE_PURPOSE (parm);
9216 DECL_CHAIN (last) = NULL_TREE;
9219 /* If there was a previous prototype,
9220 set the DECL_ARG_TYPE of each argument according to
9221 the type previously specified, and report any mismatches. */
9223 if (current_function_prototype_arg_types)
9225 tree type;
9226 for (parm = DECL_ARGUMENTS (fndecl),
9227 type = current_function_prototype_arg_types;
9228 parm || (type != NULL_TREE
9229 && TREE_VALUE (type) != error_mark_node
9230 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9231 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9233 if (parm == NULL_TREE
9234 || type == NULL_TREE
9235 || (TREE_VALUE (type) != error_mark_node
9236 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9238 if (current_function_prototype_built_in)
9239 warning_at (DECL_SOURCE_LOCATION (fndecl),
9240 0, "number of arguments doesn%'t match "
9241 "built-in prototype");
9242 else
9244 /* FIXME diagnostics: This should be the location of
9245 FNDECL, but there is bug when a prototype is
9246 declared inside function context, but defined
9247 outside of it (e.g., gcc.dg/pr15698-2.c). In
9248 which case FNDECL gets the location of the
9249 prototype, not the definition. */
9250 error_at (input_location,
9251 "number of arguments doesn%'t match prototype");
9253 error_at (current_function_prototype_locus,
9254 "prototype declaration");
9256 break;
9258 /* Type for passing arg must be consistent with that
9259 declared for the arg. ISO C says we take the unqualified
9260 type for parameters declared with qualified type. */
9261 if (TREE_TYPE (parm) != error_mark_node
9262 && TREE_VALUE (type) != error_mark_node
9263 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9264 != TYPE_ATOMIC (TREE_VALUE (type)))
9265 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9266 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9268 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9269 == TYPE_ATOMIC (TREE_VALUE (type)))
9270 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9271 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9273 /* Adjust argument to match prototype. E.g. a previous
9274 `int foo(float);' prototype causes
9275 `int foo(x) float x; {...}' to be treated like
9276 `int foo(float x) {...}'. This is particularly
9277 useful for argument types like uid_t. */
9278 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9280 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9281 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9282 && (TYPE_PRECISION (TREE_TYPE (parm))
9283 < TYPE_PRECISION (integer_type_node)))
9284 DECL_ARG_TYPE (parm)
9285 = c_type_promotes_to (TREE_TYPE (parm));
9287 /* ??? Is it possible to get here with a
9288 built-in prototype or will it always have
9289 been diagnosed as conflicting with an
9290 old-style definition and discarded? */
9291 if (current_function_prototype_built_in)
9292 warning_at (DECL_SOURCE_LOCATION (parm),
9293 OPT_Wpedantic, "promoted argument %qD "
9294 "doesn%'t match built-in prototype", parm);
9295 else
9297 pedwarn (DECL_SOURCE_LOCATION (parm),
9298 OPT_Wpedantic, "promoted argument %qD "
9299 "doesn%'t match prototype", parm);
9300 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9301 "prototype declaration");
9304 else
9306 if (current_function_prototype_built_in)
9307 warning_at (DECL_SOURCE_LOCATION (parm),
9308 0, "argument %qD doesn%'t match "
9309 "built-in prototype", parm);
9310 else
9312 error_at (DECL_SOURCE_LOCATION (parm),
9313 "argument %qD doesn%'t match prototype", parm);
9314 error_at (current_function_prototype_locus,
9315 "prototype declaration");
9320 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9323 /* Otherwise, create a prototype that would match. */
9325 else
9327 tree actual = NULL_TREE, last = NULL_TREE, type;
9329 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9331 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9332 if (last)
9333 TREE_CHAIN (last) = type;
9334 else
9335 actual = type;
9336 last = type;
9338 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9339 if (last)
9340 TREE_CHAIN (last) = type;
9341 else
9342 actual = type;
9344 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9345 of the type of this function, but we need to avoid having this
9346 affect the types of other similarly-typed functions, so we must
9347 first force the generation of an identical (but separate) type
9348 node for the relevant function type. The new node we create
9349 will be a variant of the main variant of the original function
9350 type. */
9352 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9354 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9358 /* Store parameter declarations passed in ARG_INFO into the current
9359 function declaration. */
9361 void
9362 store_parm_decls_from (struct c_arg_info *arg_info)
9364 current_function_arg_info = arg_info;
9365 store_parm_decls ();
9368 /* Called by walk_tree to look for and update context-less labels. */
9370 static tree
9371 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9373 if (TREE_CODE (*tp) == LABEL_EXPR
9374 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9376 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9377 *walk_subtrees = 0;
9380 return NULL_TREE;
9383 /* Store the parameter declarations into the current function declaration.
9384 This is called after parsing the parameter declarations, before
9385 digesting the body of the function.
9387 For an old-style definition, construct a prototype out of the old-style
9388 parameter declarations and inject it into the function's type. */
9390 void
9391 store_parm_decls (void)
9393 tree fndecl = current_function_decl;
9394 bool proto;
9396 /* The argument information block for FNDECL. */
9397 struct c_arg_info *arg_info = current_function_arg_info;
9398 current_function_arg_info = 0;
9400 /* True if this definition is written with a prototype. Note:
9401 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9402 list in a function definition as equivalent to (void) -- an
9403 empty argument list specifies the function has no parameters,
9404 but only (void) sets up a prototype for future calls. */
9405 proto = arg_info->types != 0;
9407 if (proto)
9408 store_parm_decls_newstyle (fndecl, arg_info);
9409 else
9410 store_parm_decls_oldstyle (fndecl, arg_info);
9412 /* The next call to push_scope will be a function body. */
9414 next_is_function_body = true;
9416 /* Write a record describing this function definition to the prototypes
9417 file (if requested). */
9419 gen_aux_info_record (fndecl, 1, 0, proto);
9421 /* Initialize the RTL code for the function. */
9422 allocate_struct_function (fndecl, false);
9424 if (warn_unused_local_typedefs)
9425 cfun->language = ggc_cleared_alloc<language_function> ();
9427 /* Begin the statement tree for this function. */
9428 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9430 /* ??? Insert the contents of the pending sizes list into the function
9431 to be evaluated. The only reason left to have this is
9432 void foo(int n, int array[n++])
9433 because we throw away the array type in favor of a pointer type, and
9434 thus won't naturally see the SAVE_EXPR containing the increment. All
9435 other pending sizes would be handled by gimplify_parameters. */
9436 if (arg_info->pending_sizes)
9438 /* In very special circumstances, e.g. for code like
9439 _Atomic int i = 5;
9440 void f (int a[i += 2]) {}
9441 we need to execute the atomic assignment on function entry.
9442 But in this case, it is not just a straight store, it has the
9443 op= form, which means that build_atomic_assign has generated
9444 gotos, labels, etc. Because at that time the function decl
9445 for F has not been created yet, those labels do not have any
9446 function context. But we have the fndecl now, so update the
9447 labels accordingly. gimplify_expr would crash otherwise. */
9448 walk_tree_without_duplicates (&arg_info->pending_sizes,
9449 set_labels_context_r, fndecl);
9450 add_stmt (arg_info->pending_sizes);
9454 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9455 c_finish_omp_declare_simd for function prototypes. No diagnostics
9456 should be done. */
9458 void
9459 temp_store_parm_decls (tree fndecl, tree parms)
9461 push_scope ();
9462 for (tree p = parms; p; p = DECL_CHAIN (p))
9464 DECL_CONTEXT (p) = fndecl;
9465 if (DECL_NAME (p))
9466 bind (DECL_NAME (p), p, current_scope,
9467 /*invisible=*/false, /*nested=*/false,
9468 UNKNOWN_LOCATION);
9472 /* Undo what temp_store_parm_decls did. */
9474 void
9475 temp_pop_parm_decls (void)
9477 /* Clear all bindings in this temporary scope, so that
9478 pop_scope doesn't create a BLOCK. */
9479 struct c_binding *b = current_scope->bindings;
9480 current_scope->bindings = NULL;
9481 for (; b; b = free_binding_and_advance (b))
9483 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9484 || b->decl == error_mark_node);
9485 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9486 I_SYMBOL_BINDING (b->id) = b->shadowed;
9487 if (b->shadowed && b->shadowed->u.type)
9488 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9490 pop_scope ();
9494 /* Finish up a function declaration and compile that function
9495 all the way to assembler language output. Then free the storage
9496 for the function definition.
9498 This is called after parsing the body of the function definition. */
9500 void
9501 finish_function (void)
9503 tree fndecl = current_function_decl;
9505 if (c_dialect_objc ())
9506 objc_finish_function ();
9508 if (TREE_CODE (fndecl) == FUNCTION_DECL
9509 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9511 tree args = DECL_ARGUMENTS (fndecl);
9512 for (; args; args = DECL_CHAIN (args))
9514 tree type = TREE_TYPE (args);
9515 if (INTEGRAL_TYPE_P (type)
9516 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9517 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9521 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9522 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9524 /* Must mark the RESULT_DECL as being in this function. */
9526 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9527 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9529 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9530 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9531 == integer_type_node && flag_isoc99)
9533 /* Hack. We don't want the middle-end to warn that this return
9534 is unreachable, so we mark its location as special. Using
9535 UNKNOWN_LOCATION has the problem that it gets clobbered in
9536 annotate_one_with_locus. A cleaner solution might be to
9537 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9539 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9542 /* Tie off the statement tree for this function. */
9543 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9545 finish_fname_decls ();
9547 /* Complain if there's no return statement only if option specified on
9548 command line. */
9549 if (warn_return_type > 0
9550 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9551 && !current_function_returns_value && !current_function_returns_null
9552 /* Don't complain if we are no-return. */
9553 && !current_function_returns_abnormally
9554 /* Don't complain if we are declared noreturn. */
9555 && !TREE_THIS_VOLATILE (fndecl)
9556 /* Don't warn for main(). */
9557 && !MAIN_NAME_P (DECL_NAME (fndecl))
9558 /* Or if they didn't actually specify a return type. */
9559 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9560 /* Normally, with -Wreturn-type, flow will complain, but we might
9561 optimize out static functions. */
9562 && !TREE_PUBLIC (fndecl))
9564 warning (OPT_Wreturn_type,
9565 "no return statement in function returning non-void");
9566 TREE_NO_WARNING (fndecl) = 1;
9569 /* Complain about parameters that are only set, but never otherwise used. */
9570 if (warn_unused_but_set_parameter)
9572 tree decl;
9574 for (decl = DECL_ARGUMENTS (fndecl);
9575 decl;
9576 decl = DECL_CHAIN (decl))
9577 if (TREE_USED (decl)
9578 && TREE_CODE (decl) == PARM_DECL
9579 && !DECL_READ_P (decl)
9580 && DECL_NAME (decl)
9581 && !DECL_ARTIFICIAL (decl)
9582 && !TREE_NO_WARNING (decl))
9583 warning_at (DECL_SOURCE_LOCATION (decl),
9584 OPT_Wunused_but_set_parameter,
9585 "parameter %qD set but not used", decl);
9588 /* Complain about locally defined typedefs that are not used in this
9589 function. */
9590 maybe_warn_unused_local_typedefs ();
9592 /* Possibly warn about unused parameters. */
9593 if (warn_unused_parameter)
9594 do_warn_unused_parameter (fndecl);
9596 /* Store the end of the function, so that we get good line number
9597 info for the epilogue. */
9598 cfun->function_end_locus = input_location;
9600 /* Finalize the ELF visibility for the function. */
9601 c_determine_visibility (fndecl);
9603 /* For GNU C extern inline functions disregard inline limits. */
9604 if (DECL_EXTERNAL (fndecl)
9605 && DECL_DECLARED_INLINE_P (fndecl)
9606 && (flag_gnu89_inline
9607 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9608 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9610 /* Genericize before inlining. Delay genericizing nested functions
9611 until their parent function is genericized. Since finalizing
9612 requires GENERIC, delay that as well. */
9614 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9615 && !undef_nested_function)
9617 if (!decl_function_context (fndecl))
9619 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9620 c_genericize (fndecl);
9622 /* ??? Objc emits functions after finalizing the compilation unit.
9623 This should be cleaned up later and this conditional removed. */
9624 if (symtab->global_info_ready)
9626 cgraph_node::add_new_function (fndecl, false);
9627 return;
9629 cgraph_node::finalize_function (fndecl, false);
9631 else
9633 /* Register this function with cgraph just far enough to get it
9634 added to our parent's nested function list. Handy, since the
9635 C front end doesn't have such a list. */
9636 (void) cgraph_node::get_create (fndecl);
9640 if (!decl_function_context (fndecl))
9641 undef_nested_function = false;
9643 if (cfun->language != NULL)
9645 ggc_free (cfun->language);
9646 cfun->language = NULL;
9649 /* We're leaving the context of this function, so zap cfun.
9650 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9651 tree_rest_of_compilation. */
9652 set_cfun (NULL);
9653 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9654 current_function_decl = NULL;
9657 /* Check the declarations given in a for-loop for satisfying the C99
9658 constraints. If exactly one such decl is found, return it. LOC is
9659 the location of the opening parenthesis of the for loop. The last
9660 parameter allows you to control the "for loop initial declarations
9661 are only allowed in C99 mode". Normally, you should pass
9662 flag_isoc99 as that parameter. But in some cases (Objective-C
9663 foreach loop, for example) we want to run the checks in this
9664 function even if not in C99 mode, so we allow the caller to turn
9665 off the error about not being in C99 mode.
9668 tree
9669 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9671 struct c_binding *b;
9672 tree one_decl = NULL_TREE;
9673 int n_decls = 0;
9675 if (!turn_off_iso_c99_error)
9677 static bool hint = true;
9678 /* If we get here, declarations have been used in a for loop without
9679 the C99 for loop scope. This doesn't make much sense, so don't
9680 allow it. */
9681 error_at (loc, "%<for%> loop initial declarations "
9682 "are only allowed in C99 or C11 mode");
9683 if (hint)
9685 inform (loc,
9686 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9687 "to compile your code");
9688 hint = false;
9690 return NULL_TREE;
9692 else
9693 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
9694 "initial declarations");
9696 /* C99 subclause 6.8.5 paragraph 3:
9698 [#3] The declaration part of a for statement shall only
9699 declare identifiers for objects having storage class auto or
9700 register.
9702 It isn't clear whether, in this sentence, "identifiers" binds to
9703 "shall only declare" or to "objects" - that is, whether all identifiers
9704 declared must be identifiers for objects, or whether the restriction
9705 only applies to those that are. (A question on this in comp.std.c
9706 in November 2000 received no answer.) We implement the strictest
9707 interpretation, to avoid creating an extension which later causes
9708 problems. */
9710 for (b = current_scope->bindings; b; b = b->prev)
9712 tree id = b->id;
9713 tree decl = b->decl;
9715 if (!id)
9716 continue;
9718 switch (TREE_CODE (decl))
9720 case VAR_DECL:
9722 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9723 if (TREE_STATIC (decl))
9724 error_at (decl_loc,
9725 "declaration of static variable %qD in %<for%> loop "
9726 "initial declaration", decl);
9727 else if (DECL_EXTERNAL (decl))
9728 error_at (decl_loc,
9729 "declaration of %<extern%> variable %qD in %<for%> loop "
9730 "initial declaration", decl);
9732 break;
9734 case RECORD_TYPE:
9735 error_at (loc,
9736 "%<struct %E%> declared in %<for%> loop initial "
9737 "declaration", id);
9738 break;
9739 case UNION_TYPE:
9740 error_at (loc,
9741 "%<union %E%> declared in %<for%> loop initial declaration",
9742 id);
9743 break;
9744 case ENUMERAL_TYPE:
9745 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9746 "initial declaration", id);
9747 break;
9748 default:
9749 error_at (loc, "declaration of non-variable "
9750 "%qD in %<for%> loop initial declaration", decl);
9753 n_decls++;
9754 one_decl = decl;
9757 return n_decls == 1 ? one_decl : NULL_TREE;
9760 /* Save and reinitialize the variables
9761 used during compilation of a C function. */
9763 void
9764 c_push_function_context (void)
9766 struct language_function *p = cfun->language;
9767 /* cfun->language might have been already allocated by the use of
9768 -Wunused-local-typedefs. In that case, just re-use it. */
9769 if (p == NULL)
9770 cfun->language = p = ggc_cleared_alloc<language_function> ();
9772 p->base.x_stmt_tree = c_stmt_tree;
9773 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9774 p->x_break_label = c_break_label;
9775 p->x_cont_label = c_cont_label;
9776 p->x_switch_stack = c_switch_stack;
9777 p->arg_info = current_function_arg_info;
9778 p->returns_value = current_function_returns_value;
9779 p->returns_null = current_function_returns_null;
9780 p->returns_abnormally = current_function_returns_abnormally;
9781 p->warn_about_return_type = warn_about_return_type;
9783 push_function_context ();
9786 /* Restore the variables used during compilation of a C function. */
9788 void
9789 c_pop_function_context (void)
9791 struct language_function *p;
9793 pop_function_context ();
9794 p = cfun->language;
9796 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9797 used to store data throughout the life time of the current cfun,
9798 So don't deallocate it. */
9799 if (!warn_unused_local_typedefs)
9800 cfun->language = NULL;
9802 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9803 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9805 /* Stop pointing to the local nodes about to be freed. */
9806 /* But DECL_INITIAL must remain nonzero so we know this
9807 was an actual function definition. */
9808 DECL_INITIAL (current_function_decl) = error_mark_node;
9809 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9812 c_stmt_tree = p->base.x_stmt_tree;
9813 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9814 c_break_label = p->x_break_label;
9815 c_cont_label = p->x_cont_label;
9816 c_switch_stack = p->x_switch_stack;
9817 current_function_arg_info = p->arg_info;
9818 current_function_returns_value = p->returns_value;
9819 current_function_returns_null = p->returns_null;
9820 current_function_returns_abnormally = p->returns_abnormally;
9821 warn_about_return_type = p->warn_about_return_type;
9824 /* The functions below are required for functionality of doing
9825 function at once processing in the C front end. Currently these
9826 functions are not called from anywhere in the C front end, but as
9827 these changes continue, that will change. */
9829 /* Returns the stmt_tree (if any) to which statements are currently
9830 being added. If there is no active statement-tree, NULL is
9831 returned. */
9833 stmt_tree
9834 current_stmt_tree (void)
9836 return &c_stmt_tree;
9839 /* Return the global value of T as a symbol. */
9841 tree
9842 identifier_global_value (tree t)
9844 struct c_binding *b;
9846 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9847 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9848 return b->decl;
9850 return NULL_TREE;
9853 /* In C, the only C-linkage public declaration is at file scope. */
9855 tree
9856 c_linkage_bindings (tree name)
9858 return identifier_global_value (name);
9861 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9862 otherwise the name is found in ridpointers from RID_INDEX. */
9864 void
9865 record_builtin_type (enum rid rid_index, const char *name, tree type)
9867 tree id, decl;
9868 if (name == 0)
9869 id = ridpointers[(int) rid_index];
9870 else
9871 id = get_identifier (name);
9872 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9873 pushdecl (decl);
9874 if (debug_hooks->type_decl)
9875 debug_hooks->type_decl (decl, false);
9878 /* Build the void_list_node (void_type_node having been created). */
9879 tree
9880 build_void_list_node (void)
9882 tree t = build_tree_list (NULL_TREE, void_type_node);
9883 return t;
9886 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9888 struct c_parm *
9889 build_c_parm (struct c_declspecs *specs, tree attrs,
9890 struct c_declarator *declarator,
9891 location_t loc)
9893 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9894 ret->specs = specs;
9895 ret->attrs = attrs;
9896 ret->declarator = declarator;
9897 ret->loc = loc;
9898 return ret;
9901 /* Return a declarator with nested attributes. TARGET is the inner
9902 declarator to which these attributes apply. ATTRS are the
9903 attributes. */
9905 struct c_declarator *
9906 build_attrs_declarator (tree attrs, struct c_declarator *target)
9908 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9909 ret->kind = cdk_attrs;
9910 ret->declarator = target;
9911 ret->u.attrs = attrs;
9912 return ret;
9915 /* Return a declarator for a function with arguments specified by ARGS
9916 and return type specified by TARGET. */
9918 struct c_declarator *
9919 build_function_declarator (struct c_arg_info *args,
9920 struct c_declarator *target)
9922 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9923 ret->kind = cdk_function;
9924 ret->declarator = target;
9925 ret->u.arg_info = args;
9926 return ret;
9929 /* Return a declarator for the identifier IDENT (which may be
9930 NULL_TREE for an abstract declarator). */
9932 struct c_declarator *
9933 build_id_declarator (tree ident)
9935 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9936 ret->kind = cdk_id;
9937 ret->declarator = 0;
9938 ret->u.id = ident;
9939 /* Default value - may get reset to a more precise location. */
9940 ret->id_loc = input_location;
9941 return ret;
9944 /* Return something to represent absolute declarators containing a *.
9945 TARGET is the absolute declarator that the * contains.
9946 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9947 to apply to the pointer type. */
9949 struct c_declarator *
9950 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9951 struct c_declarator *target)
9953 tree attrs;
9954 int quals = 0;
9955 struct c_declarator *itarget = target;
9956 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9957 if (type_quals_attrs)
9959 attrs = type_quals_attrs->attrs;
9960 quals = quals_from_declspecs (type_quals_attrs);
9961 if (attrs != NULL_TREE)
9962 itarget = build_attrs_declarator (attrs, target);
9964 ret->kind = cdk_pointer;
9965 ret->declarator = itarget;
9966 ret->u.pointer_quals = quals;
9967 return ret;
9970 /* Return a pointer to a structure for an empty list of declaration
9971 specifiers. */
9973 struct c_declspecs *
9974 build_null_declspecs (void)
9976 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9977 memset (ret, 0, sizeof *ret);
9978 ret->align_log = -1;
9979 ret->typespec_word = cts_none;
9980 ret->storage_class = csc_none;
9981 ret->expr_const_operands = true;
9982 ret->typespec_kind = ctsk_none;
9983 ret->address_space = ADDR_SPACE_GENERIC;
9984 return ret;
9987 /* Add the address space ADDRSPACE to the declaration specifiers
9988 SPECS, returning SPECS. */
9990 struct c_declspecs *
9991 declspecs_add_addrspace (location_t location,
9992 struct c_declspecs *specs, addr_space_t as)
9994 specs->non_sc_seen_p = true;
9995 specs->declspecs_seen_p = true;
9997 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9998 && specs->address_space != as)
9999 error ("incompatible address space qualifiers %qs and %qs",
10000 c_addr_space_name (as),
10001 c_addr_space_name (specs->address_space));
10002 else
10004 specs->address_space = as;
10005 specs->locations[cdw_address_space] = location;
10007 return specs;
10010 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10011 returning SPECS. */
10013 struct c_declspecs *
10014 declspecs_add_qual (location_t loc,
10015 struct c_declspecs *specs, tree qual)
10017 enum rid i;
10018 bool dupe = false;
10019 specs->non_sc_seen_p = true;
10020 specs->declspecs_seen_p = true;
10021 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10022 && C_IS_RESERVED_WORD (qual));
10023 i = C_RID_CODE (qual);
10024 location_t prev_loc = UNKNOWN_LOCATION;
10025 switch (i)
10027 case RID_CONST:
10028 dupe = specs->const_p;
10029 specs->const_p = true;
10030 prev_loc = specs->locations[cdw_const];
10031 specs->locations[cdw_const] = loc;
10032 break;
10033 case RID_VOLATILE:
10034 dupe = specs->volatile_p;
10035 specs->volatile_p = true;
10036 prev_loc = specs->locations[cdw_volatile];
10037 specs->locations[cdw_volatile] = loc;
10038 break;
10039 case RID_RESTRICT:
10040 dupe = specs->restrict_p;
10041 specs->restrict_p = true;
10042 prev_loc = specs->locations[cdw_restrict];
10043 specs->locations[cdw_restrict] = loc;
10044 break;
10045 case RID_ATOMIC:
10046 dupe = specs->atomic_p;
10047 specs->atomic_p = true;
10048 prev_loc = specs->locations[cdw_atomic];
10049 specs->locations[cdw_atomic] = loc;
10050 break;
10051 default:
10052 gcc_unreachable ();
10054 if (dupe)
10056 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10057 "duplicate %qE declaration specifier", qual);
10058 if (!warned
10059 && warn_duplicate_decl_specifier
10060 && prev_loc >= RESERVED_LOCATION_COUNT
10061 && !from_macro_expansion_at (prev_loc)
10062 && !from_macro_expansion_at (loc))
10063 warning_at (loc, OPT_Wduplicate_decl_specifier,
10064 "duplicate %qE declaration specifier", qual);
10066 return specs;
10069 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10070 returning SPECS. */
10072 struct c_declspecs *
10073 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10074 struct c_typespec spec)
10076 tree type = spec.spec;
10077 specs->non_sc_seen_p = true;
10078 specs->declspecs_seen_p = true;
10079 specs->typespec_kind = spec.kind;
10080 if (TREE_DEPRECATED (type))
10081 specs->deprecated_p = true;
10083 /* Handle type specifier keywords. */
10084 if (TREE_CODE (type) == IDENTIFIER_NODE
10085 && C_IS_RESERVED_WORD (type)
10086 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10088 enum rid i = C_RID_CODE (type);
10089 if (specs->type)
10091 error_at (loc, "two or more data types in declaration specifiers");
10092 return specs;
10094 if ((int) i <= (int) RID_LAST_MODIFIER)
10096 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10097 bool dupe = false;
10098 switch (i)
10100 case RID_LONG:
10101 if (specs->long_long_p)
10103 error_at (loc, "%<long long long%> is too long for GCC");
10104 break;
10106 if (specs->long_p)
10108 if (specs->typespec_word == cts_double)
10110 error_at (loc,
10111 ("both %<long long%> and %<double%> in "
10112 "declaration specifiers"));
10113 break;
10115 pedwarn_c90 (loc, OPT_Wlong_long,
10116 "ISO C90 does not support %<long long%>");
10117 specs->long_long_p = 1;
10118 specs->locations[cdw_long_long] = loc;
10119 break;
10121 if (specs->short_p)
10122 error_at (loc,
10123 ("both %<long%> and %<short%> in "
10124 "declaration specifiers"));
10125 else if (specs->typespec_word == cts_auto_type)
10126 error_at (loc,
10127 ("both %<long%> and %<__auto_type%> in "
10128 "declaration specifiers"));
10129 else if (specs->typespec_word == cts_void)
10130 error_at (loc,
10131 ("both %<long%> and %<void%> in "
10132 "declaration specifiers"));
10133 else if (specs->typespec_word == cts_int_n)
10134 error_at (loc,
10135 ("both %<long%> and %<__int%d%> in "
10136 "declaration specifiers"),
10137 int_n_data[specs->int_n_idx].bitsize);
10138 else if (specs->typespec_word == cts_bool)
10139 error_at (loc,
10140 ("both %<long%> and %<_Bool%> in "
10141 "declaration specifiers"));
10142 else if (specs->typespec_word == cts_char)
10143 error_at (loc,
10144 ("both %<long%> and %<char%> in "
10145 "declaration specifiers"));
10146 else if (specs->typespec_word == cts_float)
10147 error_at (loc,
10148 ("both %<long%> and %<float%> in "
10149 "declaration specifiers"));
10150 else if (specs->typespec_word == cts_floatn_nx)
10151 error_at (loc,
10152 ("both %<long%> and %<_Float%d%s%> in "
10153 "declaration specifiers"),
10154 floatn_nx_types[specs->floatn_nx_idx].n,
10155 (floatn_nx_types[specs->floatn_nx_idx].extended
10156 ? "x"
10157 : ""));
10158 else if (specs->typespec_word == cts_dfloat32)
10159 error_at (loc,
10160 ("both %<long%> and %<_Decimal32%> in "
10161 "declaration specifiers"));
10162 else if (specs->typespec_word == cts_dfloat64)
10163 error_at (loc,
10164 ("both %<long%> and %<_Decimal64%> in "
10165 "declaration specifiers"));
10166 else if (specs->typespec_word == cts_dfloat128)
10167 error_at (loc,
10168 ("both %<long%> and %<_Decimal128%> in "
10169 "declaration specifiers"));
10170 else
10172 specs->long_p = true;
10173 specs->locations[cdw_long] = loc;
10175 break;
10176 case RID_SHORT:
10177 dupe = specs->short_p;
10178 if (specs->long_p)
10179 error_at (loc,
10180 ("both %<long%> and %<short%> in "
10181 "declaration specifiers"));
10182 else if (specs->typespec_word == cts_auto_type)
10183 error_at (loc,
10184 ("both %<short%> and %<__auto_type%> in "
10185 "declaration specifiers"));
10186 else if (specs->typespec_word == cts_void)
10187 error_at (loc,
10188 ("both %<short%> and %<void%> in "
10189 "declaration specifiers"));
10190 else if (specs->typespec_word == cts_int_n)
10191 error_at (loc,
10192 ("both %<short%> and %<__int%d%> in "
10193 "declaration specifiers"),
10194 int_n_data[specs->int_n_idx].bitsize);
10195 else if (specs->typespec_word == cts_bool)
10196 error_at (loc,
10197 ("both %<short%> and %<_Bool%> in "
10198 "declaration specifiers"));
10199 else if (specs->typespec_word == cts_char)
10200 error_at (loc,
10201 ("both %<short%> and %<char%> in "
10202 "declaration specifiers"));
10203 else if (specs->typespec_word == cts_float)
10204 error_at (loc,
10205 ("both %<short%> and %<float%> in "
10206 "declaration specifiers"));
10207 else if (specs->typespec_word == cts_double)
10208 error_at (loc,
10209 ("both %<short%> and %<double%> in "
10210 "declaration specifiers"));
10211 else if (specs->typespec_word == cts_floatn_nx)
10212 error_at (loc,
10213 ("both %<short%> and %<_Float%d%s%> in "
10214 "declaration specifiers"),
10215 floatn_nx_types[specs->floatn_nx_idx].n,
10216 (floatn_nx_types[specs->floatn_nx_idx].extended
10217 ? "x"
10218 : ""));
10219 else if (specs->typespec_word == cts_dfloat32)
10220 error_at (loc,
10221 ("both %<short%> and %<_Decimal32%> in "
10222 "declaration specifiers"));
10223 else if (specs->typespec_word == cts_dfloat64)
10224 error_at (loc,
10225 ("both %<short%> and %<_Decimal64%> in "
10226 "declaration specifiers"));
10227 else if (specs->typespec_word == cts_dfloat128)
10228 error_at (loc,
10229 ("both %<short%> and %<_Decimal128%> in "
10230 "declaration specifiers"));
10231 else
10233 specs->short_p = true;
10234 specs->locations[cdw_short] = loc;
10236 break;
10237 case RID_SIGNED:
10238 dupe = specs->signed_p;
10239 if (specs->unsigned_p)
10240 error_at (loc,
10241 ("both %<signed%> and %<unsigned%> in "
10242 "declaration specifiers"));
10243 else if (specs->typespec_word == cts_auto_type)
10244 error_at (loc,
10245 ("both %<signed%> and %<__auto_type%> in "
10246 "declaration specifiers"));
10247 else if (specs->typespec_word == cts_void)
10248 error_at (loc,
10249 ("both %<signed%> and %<void%> in "
10250 "declaration specifiers"));
10251 else if (specs->typespec_word == cts_bool)
10252 error_at (loc,
10253 ("both %<signed%> and %<_Bool%> in "
10254 "declaration specifiers"));
10255 else if (specs->typespec_word == cts_float)
10256 error_at (loc,
10257 ("both %<signed%> and %<float%> in "
10258 "declaration specifiers"));
10259 else if (specs->typespec_word == cts_double)
10260 error_at (loc,
10261 ("both %<signed%> and %<double%> in "
10262 "declaration specifiers"));
10263 else if (specs->typespec_word == cts_floatn_nx)
10264 error_at (loc,
10265 ("both %<signed%> and %<_Float%d%s%> in "
10266 "declaration specifiers"),
10267 floatn_nx_types[specs->floatn_nx_idx].n,
10268 (floatn_nx_types[specs->floatn_nx_idx].extended
10269 ? "x"
10270 : ""));
10271 else if (specs->typespec_word == cts_dfloat32)
10272 error_at (loc,
10273 ("both %<signed%> and %<_Decimal32%> in "
10274 "declaration specifiers"));
10275 else if (specs->typespec_word == cts_dfloat64)
10276 error_at (loc,
10277 ("both %<signed%> and %<_Decimal64%> in "
10278 "declaration specifiers"));
10279 else if (specs->typespec_word == cts_dfloat128)
10280 error_at (loc,
10281 ("both %<signed%> and %<_Decimal128%> in "
10282 "declaration specifiers"));
10283 else
10285 specs->signed_p = true;
10286 specs->locations[cdw_signed] = loc;
10288 break;
10289 case RID_UNSIGNED:
10290 dupe = specs->unsigned_p;
10291 if (specs->signed_p)
10292 error_at (loc,
10293 ("both %<signed%> and %<unsigned%> in "
10294 "declaration specifiers"));
10295 else if (specs->typespec_word == cts_auto_type)
10296 error_at (loc,
10297 ("both %<unsigned%> and %<__auto_type%> in "
10298 "declaration specifiers"));
10299 else if (specs->typespec_word == cts_void)
10300 error_at (loc,
10301 ("both %<unsigned%> and %<void%> in "
10302 "declaration specifiers"));
10303 else if (specs->typespec_word == cts_bool)
10304 error_at (loc,
10305 ("both %<unsigned%> and %<_Bool%> in "
10306 "declaration specifiers"));
10307 else if (specs->typespec_word == cts_float)
10308 error_at (loc,
10309 ("both %<unsigned%> and %<float%> in "
10310 "declaration specifiers"));
10311 else if (specs->typespec_word == cts_double)
10312 error_at (loc,
10313 ("both %<unsigned%> and %<double%> in "
10314 "declaration specifiers"));
10315 else if (specs->typespec_word == cts_floatn_nx)
10316 error_at (loc,
10317 ("both %<unsigned%> and %<_Float%d%s%> in "
10318 "declaration specifiers"),
10319 floatn_nx_types[specs->floatn_nx_idx].n,
10320 (floatn_nx_types[specs->floatn_nx_idx].extended
10321 ? "x"
10322 : ""));
10323 else if (specs->typespec_word == cts_dfloat32)
10324 error_at (loc,
10325 ("both %<unsigned%> and %<_Decimal32%> in "
10326 "declaration specifiers"));
10327 else if (specs->typespec_word == cts_dfloat64)
10328 error_at (loc,
10329 ("both %<unsigned%> and %<_Decimal64%> in "
10330 "declaration specifiers"));
10331 else if (specs->typespec_word == cts_dfloat128)
10332 error_at (loc,
10333 ("both %<unsigned%> and %<_Decimal128%> in "
10334 "declaration specifiers"));
10335 else
10337 specs->unsigned_p = true;
10338 specs->locations[cdw_unsigned] = loc;
10340 break;
10341 case RID_COMPLEX:
10342 dupe = specs->complex_p;
10343 if (!in_system_header_at (loc))
10344 pedwarn_c90 (loc, OPT_Wpedantic,
10345 "ISO C90 does not support complex types");
10346 if (specs->typespec_word == cts_auto_type)
10347 error_at (loc,
10348 ("both %<complex%> and %<__auto_type%> in "
10349 "declaration specifiers"));
10350 else if (specs->typespec_word == cts_void)
10351 error_at (loc,
10352 ("both %<complex%> and %<void%> in "
10353 "declaration specifiers"));
10354 else if (specs->typespec_word == cts_bool)
10355 error_at (loc,
10356 ("both %<complex%> and %<_Bool%> in "
10357 "declaration specifiers"));
10358 else if (specs->typespec_word == cts_dfloat32)
10359 error_at (loc,
10360 ("both %<complex%> and %<_Decimal32%> in "
10361 "declaration specifiers"));
10362 else if (specs->typespec_word == cts_dfloat64)
10363 error_at (loc,
10364 ("both %<complex%> and %<_Decimal64%> in "
10365 "declaration specifiers"));
10366 else if (specs->typespec_word == cts_dfloat128)
10367 error_at (loc,
10368 ("both %<complex%> and %<_Decimal128%> in "
10369 "declaration specifiers"));
10370 else if (specs->typespec_word == cts_fract)
10371 error_at (loc,
10372 ("both %<complex%> and %<_Fract%> in "
10373 "declaration specifiers"));
10374 else if (specs->typespec_word == cts_accum)
10375 error_at (loc,
10376 ("both %<complex%> and %<_Accum%> in "
10377 "declaration specifiers"));
10378 else if (specs->saturating_p)
10379 error_at (loc,
10380 ("both %<complex%> and %<_Sat%> in "
10381 "declaration specifiers"));
10382 else
10384 specs->complex_p = true;
10385 specs->locations[cdw_complex] = loc;
10387 break;
10388 case RID_SAT:
10389 dupe = specs->saturating_p;
10390 pedwarn (loc, OPT_Wpedantic,
10391 "ISO C does not support saturating types");
10392 if (specs->typespec_word == cts_int_n)
10394 error_at (loc,
10395 ("both %<_Sat%> and %<__int%d%> in "
10396 "declaration specifiers"),
10397 int_n_data[specs->int_n_idx].bitsize);
10399 else if (specs->typespec_word == cts_auto_type)
10400 error_at (loc,
10401 ("both %<_Sat%> and %<__auto_type%> in "
10402 "declaration specifiers"));
10403 else if (specs->typespec_word == cts_void)
10404 error_at (loc,
10405 ("both %<_Sat%> and %<void%> in "
10406 "declaration specifiers"));
10407 else if (specs->typespec_word == cts_bool)
10408 error_at (loc,
10409 ("both %<_Sat%> and %<_Bool%> in "
10410 "declaration specifiers"));
10411 else if (specs->typespec_word == cts_char)
10412 error_at (loc,
10413 ("both %<_Sat%> and %<char%> in "
10414 "declaration specifiers"));
10415 else if (specs->typespec_word == cts_int)
10416 error_at (loc,
10417 ("both %<_Sat%> and %<int%> in "
10418 "declaration specifiers"));
10419 else if (specs->typespec_word == cts_float)
10420 error_at (loc,
10421 ("both %<_Sat%> and %<float%> in "
10422 "declaration specifiers"));
10423 else if (specs->typespec_word == cts_double)
10424 error_at (loc,
10425 ("both %<_Sat%> and %<double%> in "
10426 "declaration specifiers"));
10427 else if (specs->typespec_word == cts_floatn_nx)
10428 error_at (loc,
10429 ("both %<_Sat%> and %<_Float%d%s%> in "
10430 "declaration specifiers"),
10431 floatn_nx_types[specs->floatn_nx_idx].n,
10432 (floatn_nx_types[specs->floatn_nx_idx].extended
10433 ? "x"
10434 : ""));
10435 else if (specs->typespec_word == cts_dfloat32)
10436 error_at (loc,
10437 ("both %<_Sat%> and %<_Decimal32%> in "
10438 "declaration specifiers"));
10439 else if (specs->typespec_word == cts_dfloat64)
10440 error_at (loc,
10441 ("both %<_Sat%> and %<_Decimal64%> in "
10442 "declaration specifiers"));
10443 else if (specs->typespec_word == cts_dfloat128)
10444 error_at (loc,
10445 ("both %<_Sat%> and %<_Decimal128%> in "
10446 "declaration specifiers"));
10447 else if (specs->complex_p)
10448 error_at (loc,
10449 ("both %<_Sat%> and %<complex%> in "
10450 "declaration specifiers"));
10451 else
10453 specs->saturating_p = true;
10454 specs->locations[cdw_saturating] = loc;
10456 break;
10457 default:
10458 gcc_unreachable ();
10461 if (dupe)
10462 error_at (loc, "duplicate %qE", type);
10464 return specs;
10466 else
10468 /* "void", "_Bool", "char", "int", "float", "double",
10469 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10470 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10471 "__auto_type". */
10472 if (specs->typespec_word != cts_none)
10474 error_at (loc,
10475 "two or more data types in declaration specifiers");
10476 return specs;
10478 switch (i)
10480 case RID_AUTO_TYPE:
10481 if (specs->long_p)
10482 error_at (loc,
10483 ("both %<long%> and %<__auto_type%> in "
10484 "declaration specifiers"));
10485 else if (specs->short_p)
10486 error_at (loc,
10487 ("both %<short%> and %<__auto_type%> in "
10488 "declaration specifiers"));
10489 else if (specs->signed_p)
10490 error_at (loc,
10491 ("both %<signed%> and %<__auto_type%> in "
10492 "declaration specifiers"));
10493 else if (specs->unsigned_p)
10494 error_at (loc,
10495 ("both %<unsigned%> and %<__auto_type%> in "
10496 "declaration specifiers"));
10497 else if (specs->complex_p)
10498 error_at (loc,
10499 ("both %<complex%> and %<__auto_type%> in "
10500 "declaration specifiers"));
10501 else if (specs->saturating_p)
10502 error_at (loc,
10503 ("both %<_Sat%> and %<__auto_type%> in "
10504 "declaration specifiers"));
10505 else
10507 specs->typespec_word = cts_auto_type;
10508 specs->locations[cdw_typespec] = loc;
10510 return specs;
10511 case RID_INT_N_0:
10512 case RID_INT_N_1:
10513 case RID_INT_N_2:
10514 case RID_INT_N_3:
10515 specs->int_n_idx = i - RID_INT_N_0;
10516 if (!in_system_header_at (input_location))
10517 pedwarn (loc, OPT_Wpedantic,
10518 "ISO C does not support %<__int%d%> types",
10519 int_n_data[specs->int_n_idx].bitsize);
10521 if (specs->long_p)
10522 error_at (loc,
10523 ("both %<__int%d%> and %<long%> in "
10524 "declaration specifiers"),
10525 int_n_data[specs->int_n_idx].bitsize);
10526 else if (specs->saturating_p)
10527 error_at (loc,
10528 ("both %<_Sat%> and %<__int%d%> in "
10529 "declaration specifiers"),
10530 int_n_data[specs->int_n_idx].bitsize);
10531 else if (specs->short_p)
10532 error_at (loc,
10533 ("both %<__int%d%> and %<short%> in "
10534 "declaration specifiers"),
10535 int_n_data[specs->int_n_idx].bitsize);
10536 else if (! int_n_enabled_p[specs->int_n_idx])
10538 specs->typespec_word = cts_int_n;
10539 error_at (loc,
10540 "%<__int%d%> is not supported on this target",
10541 int_n_data[specs->int_n_idx].bitsize);
10543 else
10545 specs->typespec_word = cts_int_n;
10546 specs->locations[cdw_typespec] = loc;
10548 return specs;
10549 case RID_VOID:
10550 if (specs->long_p)
10551 error_at (loc,
10552 ("both %<long%> and %<void%> in "
10553 "declaration specifiers"));
10554 else if (specs->short_p)
10555 error_at (loc,
10556 ("both %<short%> and %<void%> in "
10557 "declaration specifiers"));
10558 else if (specs->signed_p)
10559 error_at (loc,
10560 ("both %<signed%> and %<void%> in "
10561 "declaration specifiers"));
10562 else if (specs->unsigned_p)
10563 error_at (loc,
10564 ("both %<unsigned%> and %<void%> in "
10565 "declaration specifiers"));
10566 else if (specs->complex_p)
10567 error_at (loc,
10568 ("both %<complex%> and %<void%> in "
10569 "declaration specifiers"));
10570 else if (specs->saturating_p)
10571 error_at (loc,
10572 ("both %<_Sat%> and %<void%> in "
10573 "declaration specifiers"));
10574 else
10576 specs->typespec_word = cts_void;
10577 specs->locations[cdw_typespec] = loc;
10579 return specs;
10580 case RID_BOOL:
10581 if (!in_system_header_at (loc))
10582 pedwarn_c90 (loc, OPT_Wpedantic,
10583 "ISO C90 does not support boolean types");
10584 if (specs->long_p)
10585 error_at (loc,
10586 ("both %<long%> and %<_Bool%> in "
10587 "declaration specifiers"));
10588 else if (specs->short_p)
10589 error_at (loc,
10590 ("both %<short%> and %<_Bool%> in "
10591 "declaration specifiers"));
10592 else if (specs->signed_p)
10593 error_at (loc,
10594 ("both %<signed%> and %<_Bool%> in "
10595 "declaration specifiers"));
10596 else if (specs->unsigned_p)
10597 error_at (loc,
10598 ("both %<unsigned%> and %<_Bool%> in "
10599 "declaration specifiers"));
10600 else if (specs->complex_p)
10601 error_at (loc,
10602 ("both %<complex%> and %<_Bool%> in "
10603 "declaration specifiers"));
10604 else if (specs->saturating_p)
10605 error_at (loc,
10606 ("both %<_Sat%> and %<_Bool%> in "
10607 "declaration specifiers"));
10608 else
10610 specs->typespec_word = cts_bool;
10611 specs->locations[cdw_typespec] = loc;
10613 return specs;
10614 case RID_CHAR:
10615 if (specs->long_p)
10616 error_at (loc,
10617 ("both %<long%> and %<char%> in "
10618 "declaration specifiers"));
10619 else if (specs->short_p)
10620 error_at (loc,
10621 ("both %<short%> and %<char%> in "
10622 "declaration specifiers"));
10623 else if (specs->saturating_p)
10624 error_at (loc,
10625 ("both %<_Sat%> and %<char%> in "
10626 "declaration specifiers"));
10627 else
10629 specs->typespec_word = cts_char;
10630 specs->locations[cdw_typespec] = loc;
10632 return specs;
10633 case RID_INT:
10634 if (specs->saturating_p)
10635 error_at (loc,
10636 ("both %<_Sat%> and %<int%> in "
10637 "declaration specifiers"));
10638 else
10640 specs->typespec_word = cts_int;
10641 specs->locations[cdw_typespec] = loc;
10643 return specs;
10644 case RID_FLOAT:
10645 if (specs->long_p)
10646 error_at (loc,
10647 ("both %<long%> and %<float%> in "
10648 "declaration specifiers"));
10649 else if (specs->short_p)
10650 error_at (loc,
10651 ("both %<short%> and %<float%> in "
10652 "declaration specifiers"));
10653 else if (specs->signed_p)
10654 error_at (loc,
10655 ("both %<signed%> and %<float%> in "
10656 "declaration specifiers"));
10657 else if (specs->unsigned_p)
10658 error_at (loc,
10659 ("both %<unsigned%> and %<float%> in "
10660 "declaration specifiers"));
10661 else if (specs->saturating_p)
10662 error_at (loc,
10663 ("both %<_Sat%> and %<float%> in "
10664 "declaration specifiers"));
10665 else
10667 specs->typespec_word = cts_float;
10668 specs->locations[cdw_typespec] = loc;
10670 return specs;
10671 case RID_DOUBLE:
10672 if (specs->long_long_p)
10673 error_at (loc,
10674 ("both %<long long%> and %<double%> in "
10675 "declaration specifiers"));
10676 else if (specs->short_p)
10677 error_at (loc,
10678 ("both %<short%> and %<double%> in "
10679 "declaration specifiers"));
10680 else if (specs->signed_p)
10681 error_at (loc,
10682 ("both %<signed%> and %<double%> in "
10683 "declaration specifiers"));
10684 else if (specs->unsigned_p)
10685 error_at (loc,
10686 ("both %<unsigned%> and %<double%> in "
10687 "declaration specifiers"));
10688 else if (specs->saturating_p)
10689 error_at (loc,
10690 ("both %<_Sat%> and %<double%> in "
10691 "declaration specifiers"));
10692 else
10694 specs->typespec_word = cts_double;
10695 specs->locations[cdw_typespec] = loc;
10697 return specs;
10698 CASE_RID_FLOATN_NX:
10699 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10700 if (!in_system_header_at (input_location))
10701 pedwarn (loc, OPT_Wpedantic,
10702 "ISO C does not support the %<_Float%d%s%> type",
10703 floatn_nx_types[specs->floatn_nx_idx].n,
10704 (floatn_nx_types[specs->floatn_nx_idx].extended
10705 ? "x"
10706 : ""));
10708 if (specs->long_p)
10709 error_at (loc,
10710 ("both %<long%> and %<_Float%d%s%> in "
10711 "declaration specifiers"),
10712 floatn_nx_types[specs->floatn_nx_idx].n,
10713 (floatn_nx_types[specs->floatn_nx_idx].extended
10714 ? "x"
10715 : ""));
10716 else if (specs->short_p)
10717 error_at (loc,
10718 ("both %<short%> and %<_Float%d%s%> in "
10719 "declaration specifiers"),
10720 floatn_nx_types[specs->floatn_nx_idx].n,
10721 (floatn_nx_types[specs->floatn_nx_idx].extended
10722 ? "x"
10723 : ""));
10724 else if (specs->signed_p)
10725 error_at (loc,
10726 ("both %<signed%> and %<_Float%d%s%> in "
10727 "declaration specifiers"),
10728 floatn_nx_types[specs->floatn_nx_idx].n,
10729 (floatn_nx_types[specs->floatn_nx_idx].extended
10730 ? "x"
10731 : ""));
10732 else if (specs->unsigned_p)
10733 error_at (loc,
10734 ("both %<unsigned%> and %<_Float%d%s%> in "
10735 "declaration specifiers"),
10736 floatn_nx_types[specs->floatn_nx_idx].n,
10737 (floatn_nx_types[specs->floatn_nx_idx].extended
10738 ? "x"
10739 : ""));
10740 else if (specs->saturating_p)
10741 error_at (loc,
10742 ("both %<_Sat%> and %<_Float%d%s%> in "
10743 "declaration specifiers"),
10744 floatn_nx_types[specs->floatn_nx_idx].n,
10745 (floatn_nx_types[specs->floatn_nx_idx].extended
10746 ? "x"
10747 : ""));
10748 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10750 specs->typespec_word = cts_floatn_nx;
10751 error_at (loc,
10752 "%<_Float%d%s%> is not supported on this target",
10753 floatn_nx_types[specs->floatn_nx_idx].n,
10754 (floatn_nx_types[specs->floatn_nx_idx].extended
10755 ? "x"
10756 : ""));
10758 else
10760 specs->typespec_word = cts_floatn_nx;
10761 specs->locations[cdw_typespec] = loc;
10763 return specs;
10764 case RID_DFLOAT32:
10765 case RID_DFLOAT64:
10766 case RID_DFLOAT128:
10768 const char *str;
10769 if (i == RID_DFLOAT32)
10770 str = "_Decimal32";
10771 else if (i == RID_DFLOAT64)
10772 str = "_Decimal64";
10773 else
10774 str = "_Decimal128";
10775 if (specs->long_long_p)
10776 error_at (loc,
10777 ("both %<long long%> and %qs in "
10778 "declaration specifiers"),
10779 str);
10780 if (specs->long_p)
10781 error_at (loc,
10782 ("both %<long%> and %qs in "
10783 "declaration specifiers"),
10784 str);
10785 else if (specs->short_p)
10786 error_at (loc,
10787 ("both %<short%> and %qs in "
10788 "declaration specifiers"),
10789 str);
10790 else if (specs->signed_p)
10791 error_at (loc,
10792 ("both %<signed%> and %qs in "
10793 "declaration specifiers"),
10794 str);
10795 else if (specs->unsigned_p)
10796 error_at (loc,
10797 ("both %<unsigned%> and %qs in "
10798 "declaration specifiers"),
10799 str);
10800 else if (specs->complex_p)
10801 error_at (loc,
10802 ("both %<complex%> and %qs in "
10803 "declaration specifiers"),
10804 str);
10805 else if (specs->saturating_p)
10806 error_at (loc,
10807 ("both %<_Sat%> and %qs in "
10808 "declaration specifiers"),
10809 str);
10810 else if (i == RID_DFLOAT32)
10811 specs->typespec_word = cts_dfloat32;
10812 else if (i == RID_DFLOAT64)
10813 specs->typespec_word = cts_dfloat64;
10814 else
10815 specs->typespec_word = cts_dfloat128;
10816 specs->locations[cdw_typespec] = loc;
10818 if (!targetm.decimal_float_supported_p ())
10819 error_at (loc,
10820 ("decimal floating point not supported "
10821 "for this target"));
10822 pedwarn (loc, OPT_Wpedantic,
10823 "ISO C does not support decimal floating point");
10824 return specs;
10825 case RID_FRACT:
10826 case RID_ACCUM:
10828 const char *str;
10829 if (i == RID_FRACT)
10830 str = "_Fract";
10831 else
10832 str = "_Accum";
10833 if (specs->complex_p)
10834 error_at (loc,
10835 ("both %<complex%> and %qs in "
10836 "declaration specifiers"),
10837 str);
10838 else if (i == RID_FRACT)
10839 specs->typespec_word = cts_fract;
10840 else
10841 specs->typespec_word = cts_accum;
10842 specs->locations[cdw_typespec] = loc;
10844 if (!targetm.fixed_point_supported_p ())
10845 error_at (loc,
10846 "fixed-point types not supported for this target");
10847 pedwarn (loc, OPT_Wpedantic,
10848 "ISO C does not support fixed-point types");
10849 return specs;
10850 default:
10851 /* ObjC reserved word "id", handled below. */
10852 break;
10857 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10858 form of ObjC type, cases such as "int" and "long" being handled
10859 above), a TYPE (struct, union, enum and typeof specifiers) or an
10860 ERROR_MARK. In none of these cases may there have previously
10861 been any type specifiers. */
10862 if (specs->type || specs->typespec_word != cts_none
10863 || specs->long_p || specs->short_p || specs->signed_p
10864 || specs->unsigned_p || specs->complex_p)
10865 error_at (loc, "two or more data types in declaration specifiers");
10866 else if (TREE_CODE (type) == TYPE_DECL)
10868 if (TREE_TYPE (type) == error_mark_node)
10869 ; /* Allow the type to default to int to avoid cascading errors. */
10870 else
10872 specs->type = TREE_TYPE (type);
10873 specs->decl_attr = DECL_ATTRIBUTES (type);
10874 specs->typedef_p = true;
10875 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10876 specs->locations[cdw_typedef] = loc;
10878 /* If this typedef name is defined in a struct, then a C++
10879 lookup would return a different value. */
10880 if (warn_cxx_compat
10881 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10882 warning_at (loc, OPT_Wc___compat,
10883 "C++ lookup of %qD would return a field, not a type",
10884 type);
10886 /* If we are parsing a struct, record that a struct field
10887 used a typedef. */
10888 if (warn_cxx_compat && struct_parse_info != NULL)
10889 struct_parse_info->typedefs_seen.safe_push (type);
10892 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10894 tree t = lookup_name (type);
10895 if (!t || TREE_CODE (t) != TYPE_DECL)
10896 error_at (loc, "%qE fails to be a typedef or built in type", type);
10897 else if (TREE_TYPE (t) == error_mark_node)
10899 else
10901 specs->type = TREE_TYPE (t);
10902 specs->locations[cdw_typespec] = loc;
10905 else
10907 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10909 specs->typedef_p = true;
10910 specs->locations[cdw_typedef] = loc;
10911 if (spec.expr)
10913 if (specs->expr)
10914 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10915 specs->expr, spec.expr);
10916 else
10917 specs->expr = spec.expr;
10918 specs->expr_const_operands &= spec.expr_const_operands;
10921 specs->type = type;
10924 return specs;
10927 /* Add the storage class specifier or function specifier SCSPEC to the
10928 declaration specifiers SPECS, returning SPECS. */
10930 struct c_declspecs *
10931 declspecs_add_scspec (location_t loc,
10932 struct c_declspecs *specs,
10933 tree scspec)
10935 enum rid i;
10936 enum c_storage_class n = csc_none;
10937 bool dupe = false;
10938 specs->declspecs_seen_p = true;
10939 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10940 && C_IS_RESERVED_WORD (scspec));
10941 i = C_RID_CODE (scspec);
10942 if (specs->non_sc_seen_p)
10943 warning (OPT_Wold_style_declaration,
10944 "%qE is not at beginning of declaration", scspec);
10945 switch (i)
10947 case RID_INLINE:
10948 /* C99 permits duplicate inline. Although of doubtful utility,
10949 it seems simplest to permit it in gnu89 mode as well, as
10950 there is also little utility in maintaining this as a
10951 difference between gnu89 and C99 inline. */
10952 dupe = false;
10953 specs->inline_p = true;
10954 specs->locations[cdw_inline] = loc;
10955 break;
10956 case RID_NORETURN:
10957 /* Duplicate _Noreturn is permitted. */
10958 dupe = false;
10959 specs->noreturn_p = true;
10960 specs->locations[cdw_noreturn] = loc;
10961 break;
10962 case RID_THREAD:
10963 dupe = specs->thread_p;
10964 if (specs->storage_class == csc_auto)
10965 error ("%qE used with %<auto%>", scspec);
10966 else if (specs->storage_class == csc_register)
10967 error ("%qE used with %<register%>", scspec);
10968 else if (specs->storage_class == csc_typedef)
10969 error ("%qE used with %<typedef%>", scspec);
10970 else
10972 specs->thread_p = true;
10973 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10974 "__thread") == 0);
10975 /* A diagnostic is not required for the use of this
10976 identifier in the implementation namespace; only diagnose
10977 it for the C11 spelling because of existing code using
10978 the other spelling. */
10979 if (!specs->thread_gnu_p)
10981 if (flag_isoc99)
10982 pedwarn_c99 (loc, OPT_Wpedantic,
10983 "ISO C99 does not support %qE", scspec);
10984 else
10985 pedwarn_c99 (loc, OPT_Wpedantic,
10986 "ISO C90 does not support %qE", scspec);
10988 specs->locations[cdw_thread] = loc;
10990 break;
10991 case RID_AUTO:
10992 n = csc_auto;
10993 break;
10994 case RID_EXTERN:
10995 n = csc_extern;
10996 /* Diagnose "__thread extern". */
10997 if (specs->thread_p && specs->thread_gnu_p)
10998 error ("%<__thread%> before %<extern%>");
10999 break;
11000 case RID_REGISTER:
11001 n = csc_register;
11002 break;
11003 case RID_STATIC:
11004 n = csc_static;
11005 /* Diagnose "__thread static". */
11006 if (specs->thread_p && specs->thread_gnu_p)
11007 error ("%<__thread%> before %<static%>");
11008 break;
11009 case RID_TYPEDEF:
11010 n = csc_typedef;
11011 break;
11012 default:
11013 gcc_unreachable ();
11015 if (n != csc_none && n == specs->storage_class)
11016 dupe = true;
11017 if (dupe)
11019 if (i == RID_THREAD)
11020 error ("duplicate %<_Thread_local%> or %<__thread%>");
11021 else
11022 error ("duplicate %qE", scspec);
11024 if (n != csc_none)
11026 if (specs->storage_class != csc_none && n != specs->storage_class)
11028 error ("multiple storage classes in declaration specifiers");
11030 else
11032 specs->storage_class = n;
11033 specs->locations[cdw_storage_class] = loc;
11034 if (n != csc_extern && n != csc_static && specs->thread_p)
11036 error ("%qs used with %qE",
11037 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11038 scspec);
11039 specs->thread_p = false;
11043 return specs;
11046 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11047 returning SPECS. */
11049 struct c_declspecs *
11050 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11052 specs->attrs = chainon (attrs, specs->attrs);
11053 specs->locations[cdw_attributes] = loc;
11054 specs->declspecs_seen_p = true;
11055 return specs;
11058 /* Add an _Alignas specifier (expression ALIGN, or type whose
11059 alignment is ALIGN) to the declaration specifiers SPECS, returning
11060 SPECS. */
11061 struct c_declspecs *
11062 declspecs_add_alignas (location_t loc,
11063 struct c_declspecs *specs, tree align)
11065 specs->alignas_p = true;
11066 specs->locations[cdw_alignas] = loc;
11067 if (align == error_mark_node)
11068 return specs;
11070 /* Only accept the alignment if it's valid and greater than
11071 the current one. Zero is invalid but by C11 required to
11072 be silently ignored. */
11073 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11074 if (align_log > specs->align_log)
11075 specs->align_log = align_log;
11076 return specs;
11079 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11080 specifiers with any other type specifier to determine the resulting
11081 type. This is where ISO C checks on complex types are made, since
11082 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11083 double". */
11085 struct c_declspecs *
11086 finish_declspecs (struct c_declspecs *specs)
11088 /* If a type was specified as a whole, we have no modifiers and are
11089 done. */
11090 if (specs->type != NULL_TREE)
11092 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11093 && !specs->signed_p && !specs->unsigned_p
11094 && !specs->complex_p);
11096 /* Set a dummy type. */
11097 if (TREE_CODE (specs->type) == ERROR_MARK)
11098 specs->type = integer_type_node;
11099 return specs;
11102 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11103 has been specified, treat it as "int" unless "_Complex" is
11104 present and there are no other specifiers. If we just have
11105 "_Complex", it is equivalent to "_Complex double", but e.g.
11106 "_Complex short" is equivalent to "_Complex short int". */
11107 if (specs->typespec_word == cts_none)
11109 if (specs->saturating_p)
11111 error_at (specs->locations[cdw_saturating],
11112 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11113 if (!targetm.fixed_point_supported_p ())
11114 error_at (specs->locations[cdw_saturating],
11115 "fixed-point types not supported for this target");
11116 specs->typespec_word = cts_fract;
11118 else if (specs->long_p || specs->short_p
11119 || specs->signed_p || specs->unsigned_p)
11121 specs->typespec_word = cts_int;
11123 else if (specs->complex_p)
11125 specs->typespec_word = cts_double;
11126 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11127 "ISO C does not support plain %<complex%> meaning "
11128 "%<double complex%>");
11130 else
11132 specs->typespec_word = cts_int;
11133 specs->default_int_p = true;
11134 /* We don't diagnose this here because grokdeclarator will
11135 give more specific diagnostics according to whether it is
11136 a function definition. */
11140 /* If "signed" was specified, record this to distinguish "int" and
11141 "signed int" in the case of a bit-field with
11142 -funsigned-bitfields. */
11143 specs->explicit_signed_p = specs->signed_p;
11145 /* Now compute the actual type. */
11146 switch (specs->typespec_word)
11148 case cts_auto_type:
11149 gcc_assert (!specs->long_p && !specs->short_p
11150 && !specs->signed_p && !specs->unsigned_p
11151 && !specs->complex_p);
11152 /* Type to be filled in later. */
11153 break;
11154 case cts_void:
11155 gcc_assert (!specs->long_p && !specs->short_p
11156 && !specs->signed_p && !specs->unsigned_p
11157 && !specs->complex_p);
11158 specs->type = void_type_node;
11159 break;
11160 case cts_bool:
11161 gcc_assert (!specs->long_p && !specs->short_p
11162 && !specs->signed_p && !specs->unsigned_p
11163 && !specs->complex_p);
11164 specs->type = boolean_type_node;
11165 break;
11166 case cts_char:
11167 gcc_assert (!specs->long_p && !specs->short_p);
11168 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11169 if (specs->signed_p)
11170 specs->type = signed_char_type_node;
11171 else if (specs->unsigned_p)
11172 specs->type = unsigned_char_type_node;
11173 else
11174 specs->type = char_type_node;
11175 if (specs->complex_p)
11177 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11178 "ISO C does not support complex integer types");
11179 specs->type = build_complex_type (specs->type);
11181 break;
11182 case cts_int_n:
11183 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11184 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11185 if (! int_n_enabled_p[specs->int_n_idx])
11186 specs->type = integer_type_node;
11187 else
11188 specs->type = (specs->unsigned_p
11189 ? int_n_trees[specs->int_n_idx].unsigned_type
11190 : int_n_trees[specs->int_n_idx].signed_type);
11191 if (specs->complex_p)
11193 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11194 "ISO C does not support complex integer types");
11195 specs->type = build_complex_type (specs->type);
11197 break;
11198 case cts_int:
11199 gcc_assert (!(specs->long_p && specs->short_p));
11200 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11201 if (specs->long_long_p)
11202 specs->type = (specs->unsigned_p
11203 ? long_long_unsigned_type_node
11204 : long_long_integer_type_node);
11205 else if (specs->long_p)
11206 specs->type = (specs->unsigned_p
11207 ? long_unsigned_type_node
11208 : long_integer_type_node);
11209 else if (specs->short_p)
11210 specs->type = (specs->unsigned_p
11211 ? short_unsigned_type_node
11212 : short_integer_type_node);
11213 else
11214 specs->type = (specs->unsigned_p
11215 ? unsigned_type_node
11216 : integer_type_node);
11217 if (specs->complex_p)
11219 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11220 "ISO C does not support complex integer types");
11221 specs->type = build_complex_type (specs->type);
11223 break;
11224 case cts_float:
11225 gcc_assert (!specs->long_p && !specs->short_p
11226 && !specs->signed_p && !specs->unsigned_p);
11227 specs->type = (specs->complex_p
11228 ? complex_float_type_node
11229 : float_type_node);
11230 break;
11231 case cts_double:
11232 gcc_assert (!specs->long_long_p && !specs->short_p
11233 && !specs->signed_p && !specs->unsigned_p);
11234 if (specs->long_p)
11236 specs->type = (specs->complex_p
11237 ? complex_long_double_type_node
11238 : long_double_type_node);
11240 else
11242 specs->type = (specs->complex_p
11243 ? complex_double_type_node
11244 : double_type_node);
11246 break;
11247 case cts_floatn_nx:
11248 gcc_assert (!specs->long_p && !specs->short_p
11249 && !specs->signed_p && !specs->unsigned_p);
11250 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11251 specs->type = integer_type_node;
11252 else if (specs->complex_p)
11253 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11254 else
11255 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11256 break;
11257 case cts_dfloat32:
11258 case cts_dfloat64:
11259 case cts_dfloat128:
11260 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11261 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11262 if (specs->typespec_word == cts_dfloat32)
11263 specs->type = dfloat32_type_node;
11264 else if (specs->typespec_word == cts_dfloat64)
11265 specs->type = dfloat64_type_node;
11266 else
11267 specs->type = dfloat128_type_node;
11268 break;
11269 case cts_fract:
11270 gcc_assert (!specs->complex_p);
11271 if (!targetm.fixed_point_supported_p ())
11272 specs->type = integer_type_node;
11273 else if (specs->saturating_p)
11275 if (specs->long_long_p)
11276 specs->type = specs->unsigned_p
11277 ? sat_unsigned_long_long_fract_type_node
11278 : sat_long_long_fract_type_node;
11279 else if (specs->long_p)
11280 specs->type = specs->unsigned_p
11281 ? sat_unsigned_long_fract_type_node
11282 : sat_long_fract_type_node;
11283 else if (specs->short_p)
11284 specs->type = specs->unsigned_p
11285 ? sat_unsigned_short_fract_type_node
11286 : sat_short_fract_type_node;
11287 else
11288 specs->type = specs->unsigned_p
11289 ? sat_unsigned_fract_type_node
11290 : sat_fract_type_node;
11292 else
11294 if (specs->long_long_p)
11295 specs->type = specs->unsigned_p
11296 ? unsigned_long_long_fract_type_node
11297 : long_long_fract_type_node;
11298 else if (specs->long_p)
11299 specs->type = specs->unsigned_p
11300 ? unsigned_long_fract_type_node
11301 : long_fract_type_node;
11302 else if (specs->short_p)
11303 specs->type = specs->unsigned_p
11304 ? unsigned_short_fract_type_node
11305 : short_fract_type_node;
11306 else
11307 specs->type = specs->unsigned_p
11308 ? unsigned_fract_type_node
11309 : fract_type_node;
11311 break;
11312 case cts_accum:
11313 gcc_assert (!specs->complex_p);
11314 if (!targetm.fixed_point_supported_p ())
11315 specs->type = integer_type_node;
11316 else if (specs->saturating_p)
11318 if (specs->long_long_p)
11319 specs->type = specs->unsigned_p
11320 ? sat_unsigned_long_long_accum_type_node
11321 : sat_long_long_accum_type_node;
11322 else if (specs->long_p)
11323 specs->type = specs->unsigned_p
11324 ? sat_unsigned_long_accum_type_node
11325 : sat_long_accum_type_node;
11326 else if (specs->short_p)
11327 specs->type = specs->unsigned_p
11328 ? sat_unsigned_short_accum_type_node
11329 : sat_short_accum_type_node;
11330 else
11331 specs->type = specs->unsigned_p
11332 ? sat_unsigned_accum_type_node
11333 : sat_accum_type_node;
11335 else
11337 if (specs->long_long_p)
11338 specs->type = specs->unsigned_p
11339 ? unsigned_long_long_accum_type_node
11340 : long_long_accum_type_node;
11341 else if (specs->long_p)
11342 specs->type = specs->unsigned_p
11343 ? unsigned_long_accum_type_node
11344 : long_accum_type_node;
11345 else if (specs->short_p)
11346 specs->type = specs->unsigned_p
11347 ? unsigned_short_accum_type_node
11348 : short_accum_type_node;
11349 else
11350 specs->type = specs->unsigned_p
11351 ? unsigned_accum_type_node
11352 : accum_type_node;
11354 break;
11355 default:
11356 gcc_unreachable ();
11359 return specs;
11362 /* Perform final processing on one file scope's declarations (or the
11363 external scope's declarations), GLOBALS. */
11365 static void
11366 c_write_global_declarations_1 (tree globals)
11368 tree decl;
11369 bool reconsider;
11371 /* Process the decls in the order they were written. */
11372 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11374 /* Check for used but undefined static functions using the C
11375 standard's definition of "used", and set TREE_NO_WARNING so
11376 that check_global_declaration doesn't repeat the check. */
11377 if (TREE_CODE (decl) == FUNCTION_DECL
11378 && DECL_INITIAL (decl) == NULL_TREE
11379 && DECL_EXTERNAL (decl)
11380 && !TREE_PUBLIC (decl))
11382 if (C_DECL_USED (decl))
11384 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11385 TREE_NO_WARNING (decl) = 1;
11387 /* For -Wunused-function warn about unused static prototypes. */
11388 else if (warn_unused_function
11389 && ! DECL_ARTIFICIAL (decl)
11390 && ! TREE_NO_WARNING (decl))
11392 warning (OPT_Wunused_function,
11393 "%q+F declared %<static%> but never defined", decl);
11394 TREE_NO_WARNING (decl) = 1;
11398 wrapup_global_declaration_1 (decl);
11403 reconsider = false;
11404 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11405 reconsider |= wrapup_global_declaration_2 (decl);
11407 while (reconsider);
11410 /* Callback to collect a source_ref from a DECL. */
11412 static void
11413 collect_source_ref_cb (tree decl)
11415 if (!DECL_IS_BUILTIN (decl))
11416 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11419 /* Preserve the external declarations scope across a garbage collect. */
11420 static GTY(()) tree ext_block;
11422 /* Collect all references relevant to SOURCE_FILE. */
11424 static void
11425 collect_all_refs (const char *source_file)
11427 tree t;
11428 unsigned i;
11430 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11431 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11433 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11436 /* Iterate over all global declarations and call CALLBACK. */
11438 static void
11439 for_each_global_decl (void (*callback) (tree decl))
11441 tree t;
11442 tree decls;
11443 tree decl;
11444 unsigned i;
11446 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11448 decls = DECL_INITIAL (t);
11449 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11450 callback (decl);
11453 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11454 callback (decl);
11457 /* Perform any final parser cleanups and generate initial debugging
11458 information. */
11460 void
11461 c_parse_final_cleanups (void)
11463 tree t;
11464 unsigned i;
11466 /* We don't want to do this if generating a PCH. */
11467 if (pch_file)
11468 return;
11470 timevar_stop (TV_PHASE_PARSING);
11471 timevar_start (TV_PHASE_DEFERRED);
11473 /* Do the Objective-C stuff. This is where all the Objective-C
11474 module stuff gets generated (symtab, class/protocol/selector
11475 lists etc). */
11476 if (c_dialect_objc ())
11477 objc_write_global_declarations ();
11479 /* Close the external scope. */
11480 ext_block = pop_scope ();
11481 external_scope = 0;
11482 gcc_assert (!current_scope);
11484 /* Handle -fdump-ada-spec[-slim]. */
11485 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11487 /* Build a table of files to generate specs for */
11488 if (flag_dump_ada_spec_slim)
11489 collect_source_ref (main_input_filename);
11490 else
11491 for_each_global_decl (collect_source_ref_cb);
11493 dump_ada_specs (collect_all_refs, NULL);
11496 /* Process all file scopes in this compilation, and the external_scope,
11497 through wrapup_global_declarations. */
11498 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11499 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11500 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11502 timevar_stop (TV_PHASE_DEFERRED);
11503 timevar_start (TV_PHASE_PARSING);
11505 ext_block = NULL;
11508 /* Register reserved keyword WORD as qualifier for address space AS. */
11510 void
11511 c_register_addr_space (const char *word, addr_space_t as)
11513 int rid = RID_FIRST_ADDR_SPACE + as;
11514 tree id;
11516 /* Address space qualifiers are only supported
11517 in C with GNU extensions enabled. */
11518 if (c_dialect_objc () || flag_no_asm)
11519 return;
11521 id = get_identifier (word);
11522 C_SET_RID_CODE (id, rid);
11523 C_IS_RESERVED_WORD (id) = 1;
11524 ridpointers [rid] = id;
11527 /* Return identifier to look up for omp declare reduction. */
11529 tree
11530 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11532 const char *p = NULL;
11533 switch (reduction_code)
11535 case PLUS_EXPR: p = "+"; break;
11536 case MULT_EXPR: p = "*"; break;
11537 case MINUS_EXPR: p = "-"; break;
11538 case BIT_AND_EXPR: p = "&"; break;
11539 case BIT_XOR_EXPR: p = "^"; break;
11540 case BIT_IOR_EXPR: p = "|"; break;
11541 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11542 case TRUTH_ORIF_EXPR: p = "||"; break;
11543 case MIN_EXPR: p = "min"; break;
11544 case MAX_EXPR: p = "max"; break;
11545 default:
11546 break;
11549 if (p == NULL)
11551 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11552 return error_mark_node;
11553 p = IDENTIFIER_POINTER (reduction_id);
11556 const char prefix[] = "omp declare reduction ";
11557 size_t lenp = sizeof (prefix);
11558 size_t len = strlen (p);
11559 char *name = XALLOCAVEC (char, lenp + len);
11560 memcpy (name, prefix, lenp - 1);
11561 memcpy (name + lenp - 1, p, len + 1);
11562 return get_identifier (name);
11565 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11566 VAR_DECL, bind it into the current scope and return it. */
11568 tree
11569 c_omp_reduction_decl (tree reduction_id)
11571 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11572 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11573 return b->decl;
11575 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11576 reduction_id, integer_type_node);
11577 DECL_ARTIFICIAL (decl) = 1;
11578 DECL_EXTERNAL (decl) = 1;
11579 TREE_STATIC (decl) = 1;
11580 TREE_PUBLIC (decl) = 0;
11581 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11582 return decl;
11585 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11587 tree
11588 c_omp_reduction_lookup (tree reduction_id, tree type)
11590 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11591 while (b)
11593 tree t;
11594 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11595 if (comptypes (TREE_PURPOSE (t), type))
11596 return TREE_VALUE (t);
11597 b = b->shadowed;
11599 return error_mark_node;
11602 /* Helper function called via walk_tree, to diagnose invalid
11603 #pragma omp declare reduction combiners or initializers. */
11605 tree
11606 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11608 tree *vars = (tree *) data;
11609 if (SSA_VAR_P (*tp)
11610 && !DECL_ARTIFICIAL (*tp)
11611 && *tp != vars[0]
11612 && *tp != vars[1])
11614 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11615 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11616 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11617 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11618 *tp);
11619 else
11620 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11621 "to variable %qD which is not %<omp_priv%> nor "
11622 "%<omp_orig%>",
11623 *tp);
11624 return *tp;
11626 return NULL_TREE;
11629 #include "gt-c-c-decl.h"