* c-typeck.c (digest_init): Shorten overlength strings.
[official-gcc.git] / gcc / c / c-decl.c
blobfdcfbde2d993b2ef01f563870e0aa23d13bc00da
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2018 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);
608 /* T is a statement. Add it to the statement-tree. This is the
609 C/ObjC version--C++ has a slightly different version of this
610 function. */
612 tree
613 add_stmt (tree t)
615 enum tree_code code = TREE_CODE (t);
617 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
619 if (!EXPR_HAS_LOCATION (t))
620 SET_EXPR_LOCATION (t, input_location);
623 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
624 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
626 /* Add T to the statement-tree. Non-side-effect statements need to be
627 recorded during statement expressions. */
628 if (!building_stmt_list_p ())
629 push_stmt_list ();
630 append_to_statement_list_force (t, &cur_stmt_list);
632 return t;
635 /* Build a pointer type using the default pointer mode. */
637 static tree
638 c_build_pointer_type (tree to_type)
640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
641 : TYPE_ADDR_SPACE (to_type);
642 machine_mode pointer_mode;
644 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
645 pointer_mode = targetm.addr_space.pointer_mode (as);
646 else
647 pointer_mode = c_default_pointer_mode;
648 return build_pointer_type_for_mode (to_type, pointer_mode, false);
652 /* Return true if we will want to say something if a goto statement
653 crosses DECL. */
655 static bool
656 decl_jump_unsafe (tree decl)
658 if (error_operand_p (decl))
659 return false;
661 /* Always warn about crossing variably modified types. */
662 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
663 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
664 return true;
666 /* Otherwise, only warn if -Wgoto-misses-init and this is an
667 initialized automatic decl. */
668 if (warn_jump_misses_init
669 && VAR_P (decl)
670 && !TREE_STATIC (decl)
671 && DECL_INITIAL (decl) != NULL_TREE)
672 return true;
674 return false;
678 void
679 c_print_identifier (FILE *file, tree node, int indent)
681 void (*save) (enum c_oracle_request, tree identifier);
683 /* Temporarily hide any binding oracle. Without this, calls to
684 debug_tree from the debugger will end up calling into the oracle,
685 making for a confusing debug session. As the oracle isn't needed
686 here for normal operation, it's simplest to suppress it. */
687 save = c_binding_oracle;
688 c_binding_oracle = NULL;
690 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
691 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
692 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
693 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
695 tree rid = ridpointers[C_RID_CODE (node)];
696 indent_to (file, indent + 4);
697 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
698 (void *) rid, IDENTIFIER_POINTER (rid));
701 c_binding_oracle = save;
704 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
705 which may be any of several kinds of DECL or TYPE or error_mark_node,
706 in the scope SCOPE. */
707 static void
708 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
709 bool nested, location_t locus)
711 struct c_binding *b, **here;
713 if (binding_freelist)
715 b = binding_freelist;
716 binding_freelist = b->prev;
718 else
719 b = ggc_alloc<c_binding> ();
721 b->shadowed = 0;
722 b->decl = decl;
723 b->id = name;
724 b->depth = scope->depth;
725 b->invisible = invisible;
726 b->nested = nested;
727 b->inner_comp = 0;
728 b->in_struct = 0;
729 b->locus = locus;
731 b->u.type = NULL;
733 b->prev = scope->bindings;
734 scope->bindings = b;
736 if (decl_jump_unsafe (decl))
737 scope->has_jump_unsafe_decl = 1;
739 if (!name)
740 return;
742 switch (TREE_CODE (decl))
744 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
745 case ENUMERAL_TYPE:
746 case UNION_TYPE:
747 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
748 case VAR_DECL:
749 case FUNCTION_DECL:
750 case TYPE_DECL:
751 case CONST_DECL:
752 case PARM_DECL:
753 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
755 default:
756 gcc_unreachable ();
759 /* Locate the appropriate place in the chain of shadowed decls
760 to insert this binding. Normally, scope == current_scope and
761 this does nothing. */
762 while (*here && (*here)->depth > scope->depth)
763 here = &(*here)->shadowed;
765 b->shadowed = *here;
766 *here = b;
769 /* Clear the binding structure B, stick it on the binding_freelist,
770 and return the former value of b->prev. This is used by pop_scope
771 and get_parm_info to iterate destructively over all the bindings
772 from a given scope. */
773 static struct c_binding *
774 free_binding_and_advance (struct c_binding *b)
776 struct c_binding *prev = b->prev;
778 memset (b, 0, sizeof (struct c_binding));
779 b->prev = binding_freelist;
780 binding_freelist = b;
782 return prev;
785 /* Bind a label. Like bind, but skip fields which aren't used for
786 labels, and add the LABEL_VARS value. */
787 static void
788 bind_label (tree name, tree label, struct c_scope *scope,
789 struct c_label_vars *label_vars)
791 struct c_binding *b;
793 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
794 UNKNOWN_LOCATION);
796 scope->has_label_bindings = true;
798 b = scope->bindings;
799 gcc_assert (b->decl == label);
800 label_vars->shadowed = b->u.label;
801 b->u.label = label_vars;
804 /* Hook called at end of compilation to assume 1 elt
805 for a file-scope tentative array defn that wasn't complete before. */
807 void
808 c_finish_incomplete_decl (tree decl)
810 if (VAR_P (decl))
812 tree type = TREE_TYPE (decl);
813 if (type != error_mark_node
814 && TREE_CODE (type) == ARRAY_TYPE
815 && !DECL_EXTERNAL (decl)
816 && TYPE_DOMAIN (type) == NULL_TREE)
818 warning_at (DECL_SOURCE_LOCATION (decl),
819 0, "array %q+D assumed to have one element", decl);
821 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
823 relayout_decl (decl);
828 /* Record that inline function FUNC contains a reference (location
829 LOC) to static DECL (file-scope or function-local according to
830 TYPE). */
832 void
833 record_inline_static (location_t loc, tree func, tree decl,
834 enum c_inline_static_type type)
836 c_inline_static *csi = ggc_alloc<c_inline_static> ();
837 csi->location = loc;
838 csi->function = func;
839 csi->static_decl = decl;
840 csi->type = type;
841 csi->next = c_inline_statics;
842 c_inline_statics = csi;
845 /* Check for references to static declarations in inline functions at
846 the end of the translation unit and diagnose them if the functions
847 are still inline definitions. */
849 static void
850 check_inline_statics (void)
852 struct c_inline_static *csi;
853 for (csi = c_inline_statics; csi; csi = csi->next)
855 if (DECL_EXTERNAL (csi->function))
856 switch (csi->type)
858 case csi_internal:
859 pedwarn (csi->location, 0,
860 "%qD is static but used in inline function %qD "
861 "which is not static", csi->static_decl, csi->function);
862 break;
863 case csi_modifiable:
864 pedwarn (csi->location, 0,
865 "%q+D is static but declared in inline function %qD "
866 "which is not static", csi->static_decl, csi->function);
867 break;
868 default:
869 gcc_unreachable ();
872 c_inline_statics = NULL;
875 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
876 for the current state, otherwise set it to uninitialized. */
878 static void
879 set_spot_bindings (struct c_spot_bindings *p, bool defining)
881 if (defining)
883 p->scope = current_scope;
884 p->bindings_in_scope = current_scope->bindings;
886 else
888 p->scope = NULL;
889 p->bindings_in_scope = NULL;
891 p->stmt_exprs = 0;
892 p->left_stmt_expr = false;
895 /* Update spot bindings P as we pop out of SCOPE. Return true if we
896 should push decls for a label. */
898 static bool
899 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
901 if (p->scope != scope)
903 /* This label or goto is defined in some other scope, or it is a
904 label which is not yet defined. There is nothing to
905 update. */
906 return false;
909 /* Adjust the spot bindings to refer to the bindings already defined
910 in the enclosing scope. */
911 p->scope = scope->outer;
912 p->bindings_in_scope = p->scope->bindings;
914 return true;
917 /* The Objective-C front-end often needs to determine the current scope. */
919 void *
920 objc_get_current_scope (void)
922 return current_scope;
925 /* The following function is used only by Objective-C. It needs to live here
926 because it accesses the innards of c_scope. */
928 void
929 objc_mark_locals_volatile (void *enclosing_blk)
931 struct c_scope *scope;
932 struct c_binding *b;
934 for (scope = current_scope;
935 scope && scope != enclosing_blk;
936 scope = scope->outer)
938 for (b = scope->bindings; b; b = b->prev)
939 objc_volatilize_decl (b->decl);
941 /* Do not climb up past the current function. */
942 if (scope->function_body)
943 break;
947 /* Return true if we are in the global binding level. */
949 bool
950 global_bindings_p (void)
952 return current_scope == file_scope;
955 /* Return true if we're declaring parameters in an old-style function
956 declaration. */
958 bool
959 old_style_parameter_scope (void)
961 /* If processing parameters and there is no function statement list, we
962 * have an old-style function declaration. */
963 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
966 void
967 keep_next_level (void)
969 keep_next_level_flag = true;
972 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
974 void
975 set_float_const_decimal64 (void)
977 current_scope->float_const_decimal64 = true;
980 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
982 void
983 clear_float_const_decimal64 (void)
985 current_scope->float_const_decimal64 = false;
988 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
990 bool
991 float_const_decimal64_p (void)
993 return current_scope->float_const_decimal64;
996 /* Identify this scope as currently being filled with parameters. */
998 void
999 declare_parm_level (void)
1001 current_scope->parm_flag = true;
1004 void
1005 push_scope (void)
1007 if (next_is_function_body)
1009 /* This is the transition from the parameters to the top level
1010 of the function body. These are the same scope
1011 (C99 6.2.1p4,6) so we do not push another scope structure.
1012 next_is_function_body is set only by store_parm_decls, which
1013 in turn is called when and only when we are about to
1014 encounter the opening curly brace for the function body.
1016 The outermost block of a function always gets a BLOCK node,
1017 because the debugging output routines expect that each
1018 function has at least one BLOCK. */
1019 current_scope->parm_flag = false;
1020 current_scope->function_body = true;
1021 current_scope->keep = true;
1022 current_scope->outer_function = current_function_scope;
1023 current_function_scope = current_scope;
1025 keep_next_level_flag = false;
1026 next_is_function_body = false;
1028 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1029 if (current_scope->outer)
1030 current_scope->float_const_decimal64
1031 = current_scope->outer->float_const_decimal64;
1032 else
1033 current_scope->float_const_decimal64 = false;
1035 else
1037 struct c_scope *scope;
1038 if (scope_freelist)
1040 scope = scope_freelist;
1041 scope_freelist = scope->outer;
1043 else
1044 scope = ggc_cleared_alloc<c_scope> ();
1046 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1047 if (current_scope)
1048 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1049 else
1050 scope->float_const_decimal64 = false;
1052 scope->keep = keep_next_level_flag;
1053 scope->outer = current_scope;
1054 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1056 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1057 possible. */
1058 if (current_scope && scope->depth == 0)
1060 scope->depth--;
1061 sorry ("GCC supports only %u nested scopes", scope->depth);
1064 current_scope = scope;
1065 keep_next_level_flag = false;
1069 /* This is called when we are leaving SCOPE. For each label defined
1070 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1071 These are the decls whose initialization will be skipped by a goto
1072 later in the function. */
1074 static void
1075 update_label_decls (struct c_scope *scope)
1077 struct c_scope *s;
1079 s = scope;
1080 while (s != NULL)
1082 if (s->has_label_bindings)
1084 struct c_binding *b;
1086 for (b = s->bindings; b != NULL; b = b->prev)
1088 struct c_label_vars *label_vars;
1089 struct c_binding *b1;
1090 bool hjud;
1091 unsigned int ix;
1092 struct c_goto_bindings *g;
1094 if (TREE_CODE (b->decl) != LABEL_DECL)
1095 continue;
1096 label_vars = b->u.label;
1098 b1 = label_vars->label_bindings.bindings_in_scope;
1099 if (label_vars->label_bindings.scope == NULL)
1100 hjud = false;
1101 else
1102 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1103 if (update_spot_bindings (scope, &label_vars->label_bindings))
1105 /* This label is defined in this scope. */
1106 if (hjud)
1108 for (; b1 != NULL; b1 = b1->prev)
1110 /* A goto from later in the function to this
1111 label will never see the initialization
1112 of B1, if any. Save it to issue a
1113 warning if needed. */
1114 if (decl_jump_unsafe (b1->decl))
1115 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1120 /* Update the bindings of any goto statements associated
1121 with this label. */
1122 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1123 update_spot_bindings (scope, &g->goto_bindings);
1127 /* Don't search beyond the current function. */
1128 if (s == current_function_scope)
1129 break;
1131 s = s->outer;
1135 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1137 static void
1138 set_type_context (tree type, tree context)
1140 for (type = TYPE_MAIN_VARIANT (type); type;
1141 type = TYPE_NEXT_VARIANT (type))
1142 TYPE_CONTEXT (type) = context;
1145 /* Exit a scope. Restore the state of the identifier-decl mappings
1146 that were in effect when this scope was entered. Return a BLOCK
1147 node containing all the DECLs in this scope that are of interest
1148 to debug info generation. */
1150 tree
1151 pop_scope (void)
1153 struct c_scope *scope = current_scope;
1154 tree block, context, p;
1155 struct c_binding *b;
1157 bool functionbody = scope->function_body;
1158 bool keep = functionbody || scope->keep || scope->bindings;
1160 update_label_decls (scope);
1162 /* If appropriate, create a BLOCK to record the decls for the life
1163 of this function. */
1164 block = NULL_TREE;
1165 if (keep)
1167 block = make_node (BLOCK);
1168 BLOCK_SUBBLOCKS (block) = scope->blocks;
1169 TREE_USED (block) = 1;
1171 /* In each subblock, record that this is its superior. */
1172 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1173 BLOCK_SUPERCONTEXT (p) = block;
1175 BLOCK_VARS (block) = NULL_TREE;
1178 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1179 scope must be set so that they point to the appropriate
1180 construct, i.e. either to the current FUNCTION_DECL node, or
1181 else to the BLOCK node we just constructed.
1183 Note that for tagged types whose scope is just the formal
1184 parameter list for some function type specification, we can't
1185 properly set their TYPE_CONTEXTs here, because we don't have a
1186 pointer to the appropriate FUNCTION_TYPE node readily available
1187 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1188 type nodes get set in `grokdeclarator' as soon as we have created
1189 the FUNCTION_TYPE node which will represent the "scope" for these
1190 "parameter list local" tagged types. */
1191 if (scope->function_body)
1192 context = current_function_decl;
1193 else if (scope == file_scope)
1195 tree file_decl
1196 = build_translation_unit_decl (get_identifier (main_input_filename));
1197 context = file_decl;
1198 debug_hooks->register_main_translation_unit (file_decl);
1200 else
1201 context = block;
1203 /* Clear all bindings in this scope. */
1204 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1206 p = b->decl;
1207 switch (TREE_CODE (p))
1209 case LABEL_DECL:
1210 /* Warnings for unused labels, errors for undefined labels. */
1211 if (TREE_USED (p) && !DECL_INITIAL (p))
1213 error ("label %q+D used but not defined", p);
1214 DECL_INITIAL (p) = error_mark_node;
1216 else
1217 warn_for_unused_label (p);
1219 /* Labels go in BLOCK_VARS. */
1220 DECL_CHAIN (p) = BLOCK_VARS (block);
1221 BLOCK_VARS (block) = p;
1222 gcc_assert (I_LABEL_BINDING (b->id) == b);
1223 I_LABEL_BINDING (b->id) = b->shadowed;
1225 /* Also pop back to the shadowed label_vars. */
1226 release_tree_vector (b->u.label->decls_in_scope);
1227 b->u.label = b->u.label->shadowed;
1228 break;
1230 case ENUMERAL_TYPE:
1231 case UNION_TYPE:
1232 case RECORD_TYPE:
1233 set_type_context (p, context);
1235 /* Types may not have tag-names, in which case the type
1236 appears in the bindings list with b->id NULL. */
1237 if (b->id)
1239 gcc_assert (I_TAG_BINDING (b->id) == b);
1240 I_TAG_BINDING (b->id) = b->shadowed;
1242 break;
1244 case FUNCTION_DECL:
1245 /* Propagate TREE_ADDRESSABLE from nested functions to their
1246 containing functions. */
1247 if (!TREE_ASM_WRITTEN (p)
1248 && DECL_INITIAL (p) != NULL_TREE
1249 && TREE_ADDRESSABLE (p)
1250 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1251 && DECL_ABSTRACT_ORIGIN (p) != p)
1252 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1253 if (!DECL_EXTERNAL (p)
1254 && !DECL_INITIAL (p)
1255 && scope != file_scope
1256 && scope != external_scope)
1258 error ("nested function %q+D declared but never defined", p);
1259 undef_nested_function = true;
1261 else if (DECL_DECLARED_INLINE_P (p)
1262 && TREE_PUBLIC (p)
1263 && !DECL_INITIAL (p))
1265 /* C99 6.7.4p6: "a function with external linkage... declared
1266 with an inline function specifier ... shall also be defined
1267 in the same translation unit." */
1268 if (!flag_gnu89_inline
1269 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1270 && scope != external_scope)
1271 pedwarn (input_location, 0,
1272 "inline function %q+D declared but never defined", p);
1273 DECL_EXTERNAL (p) = 1;
1276 goto common_symbol;
1278 case VAR_DECL:
1279 /* Warnings for unused variables. */
1280 if ((!TREE_USED (p) || !DECL_READ_P (p))
1281 && !TREE_NO_WARNING (p)
1282 && !DECL_IN_SYSTEM_HEADER (p)
1283 && DECL_NAME (p)
1284 && !DECL_ARTIFICIAL (p)
1285 && scope != file_scope
1286 && scope != external_scope)
1288 if (!TREE_USED (p))
1289 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1290 else if (DECL_CONTEXT (p) == current_function_decl)
1291 warning_at (DECL_SOURCE_LOCATION (p),
1292 OPT_Wunused_but_set_variable,
1293 "variable %qD set but not used", p);
1296 if (b->inner_comp)
1298 error ("type of array %q+D completed incompatibly with"
1299 " implicit initialization", p);
1302 /* Fall through. */
1303 case TYPE_DECL:
1304 case CONST_DECL:
1305 common_symbol:
1306 /* All of these go in BLOCK_VARS, but only if this is the
1307 binding in the home scope. */
1308 if (!b->nested)
1310 DECL_CHAIN (p) = BLOCK_VARS (block);
1311 BLOCK_VARS (block) = p;
1313 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1315 /* For block local externs add a special
1316 DECL_EXTERNAL decl for debug info generation. */
1317 tree extp = copy_node (p);
1319 DECL_EXTERNAL (extp) = 1;
1320 TREE_STATIC (extp) = 0;
1321 TREE_PUBLIC (extp) = 1;
1322 DECL_INITIAL (extp) = NULL_TREE;
1323 DECL_LANG_SPECIFIC (extp) = NULL;
1324 DECL_CONTEXT (extp) = current_function_decl;
1325 if (TREE_CODE (p) == FUNCTION_DECL)
1327 DECL_RESULT (extp) = NULL_TREE;
1328 DECL_SAVED_TREE (extp) = NULL_TREE;
1329 DECL_STRUCT_FUNCTION (extp) = NULL;
1331 if (b->locus != UNKNOWN_LOCATION)
1332 DECL_SOURCE_LOCATION (extp) = b->locus;
1333 DECL_CHAIN (extp) = BLOCK_VARS (block);
1334 BLOCK_VARS (block) = extp;
1336 /* If this is the file scope set DECL_CONTEXT of each decl to
1337 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1338 work. */
1339 if (scope == file_scope)
1341 DECL_CONTEXT (p) = context;
1342 if (TREE_CODE (p) == TYPE_DECL
1343 && TREE_TYPE (p) != error_mark_node)
1344 set_type_context (TREE_TYPE (p), context);
1347 gcc_fallthrough ();
1348 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1349 already been put there by store_parm_decls. Unused-
1350 parameter warnings are handled by function.c.
1351 error_mark_node obviously does not go in BLOCK_VARS and
1352 does not get unused-variable warnings. */
1353 case PARM_DECL:
1354 case ERROR_MARK:
1355 /* It is possible for a decl not to have a name. We get
1356 here with b->id NULL in this case. */
1357 if (b->id)
1359 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1360 I_SYMBOL_BINDING (b->id) = b->shadowed;
1361 if (b->shadowed && b->shadowed->u.type)
1362 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1364 break;
1366 default:
1367 gcc_unreachable ();
1372 /* Dispose of the block that we just made inside some higher level. */
1373 if ((scope->function_body || scope == file_scope) && context)
1375 DECL_INITIAL (context) = block;
1376 BLOCK_SUPERCONTEXT (block) = context;
1378 else if (scope->outer)
1380 if (block)
1381 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1382 /* If we did not make a block for the scope just exited, any
1383 blocks made for inner scopes must be carried forward so they
1384 will later become subblocks of something else. */
1385 else if (scope->blocks)
1386 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1389 /* Pop the current scope, and free the structure for reuse. */
1390 current_scope = scope->outer;
1391 if (scope->function_body)
1392 current_function_scope = scope->outer_function;
1394 memset (scope, 0, sizeof (struct c_scope));
1395 scope->outer = scope_freelist;
1396 scope_freelist = scope;
1398 return block;
1401 void
1402 push_file_scope (void)
1404 tree decl;
1406 if (file_scope)
1407 return;
1409 push_scope ();
1410 file_scope = current_scope;
1412 start_fname_decls ();
1414 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1415 bind (DECL_NAME (decl), decl, file_scope,
1416 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1419 void
1420 pop_file_scope (void)
1422 /* In case there were missing closebraces, get us back to the global
1423 binding level. */
1424 while (current_scope != file_scope)
1425 pop_scope ();
1427 /* __FUNCTION__ is defined at file scope (""). This
1428 call may not be necessary as my tests indicate it
1429 still works without it. */
1430 finish_fname_decls ();
1432 check_inline_statics ();
1434 /* This is the point to write out a PCH if we're doing that.
1435 In that case we do not want to do anything else. */
1436 if (pch_file)
1438 c_common_write_pch ();
1439 /* Ensure even the callers don't try to finalize the CU. */
1440 flag_syntax_only = 1;
1441 return;
1444 /* Pop off the file scope and close this translation unit. */
1445 pop_scope ();
1446 file_scope = 0;
1448 maybe_apply_pending_pragma_weaks ();
1451 /* Adjust the bindings for the start of a statement expression. */
1453 void
1454 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1456 struct c_scope *scope;
1458 for (scope = current_scope; scope != NULL; scope = scope->outer)
1460 struct c_binding *b;
1462 if (!scope->has_label_bindings)
1463 continue;
1465 for (b = scope->bindings; b != NULL; b = b->prev)
1467 struct c_label_vars *label_vars;
1468 unsigned int ix;
1469 struct c_goto_bindings *g;
1471 if (TREE_CODE (b->decl) != LABEL_DECL)
1472 continue;
1473 label_vars = b->u.label;
1474 ++label_vars->label_bindings.stmt_exprs;
1475 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1476 ++g->goto_bindings.stmt_exprs;
1480 if (switch_bindings != NULL)
1481 ++switch_bindings->stmt_exprs;
1484 /* Adjust the bindings for the end of a statement expression. */
1486 void
1487 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1489 struct c_scope *scope;
1491 for (scope = current_scope; scope != NULL; scope = scope->outer)
1493 struct c_binding *b;
1495 if (!scope->has_label_bindings)
1496 continue;
1498 for (b = scope->bindings; b != NULL; b = b->prev)
1500 struct c_label_vars *label_vars;
1501 unsigned int ix;
1502 struct c_goto_bindings *g;
1504 if (TREE_CODE (b->decl) != LABEL_DECL)
1505 continue;
1506 label_vars = b->u.label;
1507 --label_vars->label_bindings.stmt_exprs;
1508 if (label_vars->label_bindings.stmt_exprs < 0)
1510 label_vars->label_bindings.left_stmt_expr = true;
1511 label_vars->label_bindings.stmt_exprs = 0;
1513 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1515 --g->goto_bindings.stmt_exprs;
1516 if (g->goto_bindings.stmt_exprs < 0)
1518 g->goto_bindings.left_stmt_expr = true;
1519 g->goto_bindings.stmt_exprs = 0;
1525 if (switch_bindings != NULL)
1527 --switch_bindings->stmt_exprs;
1528 gcc_assert (switch_bindings->stmt_exprs >= 0);
1532 /* Push a definition or a declaration of struct, union or enum tag "name".
1533 "type" should be the type node.
1534 We assume that the tag "name" is not already defined, and has a location
1535 of LOC.
1537 Note that the definition may really be just a forward reference.
1538 In that case, the TYPE_SIZE will be zero. */
1540 static void
1541 pushtag (location_t loc, tree name, tree type)
1543 /* Record the identifier as the type's name if it has none. */
1544 if (name && !TYPE_NAME (type))
1545 TYPE_NAME (type) = name;
1546 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1548 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1549 tagged type we just added to the current scope. This fake
1550 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1551 to output a representation of a tagged type, and it also gives
1552 us a convenient place to record the "scope start" address for the
1553 tagged type. */
1555 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1556 TYPE_DECL, NULL_TREE, type));
1558 /* An approximation for now, so we can tell this is a function-scope tag.
1559 This will be updated in pop_scope. */
1560 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1562 if (warn_cxx_compat && name != NULL_TREE)
1564 struct c_binding *b = I_SYMBOL_BINDING (name);
1566 if (b != NULL
1567 && b->decl != NULL_TREE
1568 && TREE_CODE (b->decl) == TYPE_DECL
1569 && (B_IN_CURRENT_SCOPE (b)
1570 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1571 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1572 != TYPE_MAIN_VARIANT (type)))
1574 auto_diagnostic_group d;
1575 if (warning_at (loc, OPT_Wc___compat,
1576 ("using %qD as both a typedef and a tag is "
1577 "invalid in C++"), b->decl)
1578 && b->locus != UNKNOWN_LOCATION)
1579 inform (b->locus, "originally defined here");
1584 /* An exported interface to pushtag. This is used by the gdb plugin's
1585 binding oracle to introduce a new tag binding. */
1587 void
1588 c_pushtag (location_t loc, tree name, tree type)
1590 pushtag (loc, name, type);
1593 /* An exported interface to bind a declaration. LOC is the location
1594 to use. DECL is the declaration to bind. The decl's name is used
1595 to determine how it is bound. If DECL is a VAR_DECL, then
1596 IS_GLOBAL determines whether the decl is put into the global (file
1597 and external) scope or the current function's scope; if DECL is not
1598 a VAR_DECL then it is always put into the file scope. */
1600 void
1601 c_bind (location_t loc, tree decl, bool is_global)
1603 struct c_scope *scope;
1604 bool nested = false;
1606 if (!VAR_P (decl) || current_function_scope == NULL)
1608 /* Types and functions are always considered to be global. */
1609 scope = file_scope;
1610 DECL_EXTERNAL (decl) = 1;
1611 TREE_PUBLIC (decl) = 1;
1613 else if (is_global)
1615 /* Also bind it into the external scope. */
1616 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1617 nested = true;
1618 scope = file_scope;
1619 DECL_EXTERNAL (decl) = 1;
1620 TREE_PUBLIC (decl) = 1;
1622 else
1624 DECL_CONTEXT (decl) = current_function_decl;
1625 TREE_PUBLIC (decl) = 0;
1626 scope = current_function_scope;
1629 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1632 /* Subroutine of compare_decls. Allow harmless mismatches in return
1633 and argument types provided that the type modes match. This function
1634 return a unified type given a suitable match, and 0 otherwise. */
1636 static tree
1637 match_builtin_function_types (tree newtype, tree oldtype)
1639 tree newrettype, oldrettype;
1640 tree newargs, oldargs;
1641 tree trytype, tryargs;
1643 /* Accept the return type of the new declaration if same modes. */
1644 oldrettype = TREE_TYPE (oldtype);
1645 newrettype = TREE_TYPE (newtype);
1647 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1648 return NULL_TREE;
1650 oldargs = TYPE_ARG_TYPES (oldtype);
1651 newargs = TYPE_ARG_TYPES (newtype);
1652 tryargs = newargs;
1654 while (oldargs || newargs)
1656 if (!oldargs
1657 || !newargs
1658 || !TREE_VALUE (oldargs)
1659 || !TREE_VALUE (newargs)
1660 || TYPE_MODE (TREE_VALUE (oldargs))
1661 != TYPE_MODE (TREE_VALUE (newargs)))
1662 return NULL_TREE;
1664 oldargs = TREE_CHAIN (oldargs);
1665 newargs = TREE_CHAIN (newargs);
1668 trytype = build_function_type (newrettype, tryargs);
1670 /* Allow declaration to change transaction_safe attribute. */
1671 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1672 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1673 tree newattrs = TYPE_ATTRIBUTES (newtype);
1674 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1675 if (oldtsafe && !newtsafe)
1676 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1677 else if (newtsafe && !oldtsafe)
1678 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1679 NULL_TREE, oldattrs);
1681 return build_type_attribute_variant (trytype, oldattrs);
1684 /* Subroutine of diagnose_mismatched_decls. Check for function type
1685 mismatch involving an empty arglist vs a nonempty one and give clearer
1686 diagnostics. */
1687 static void
1688 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1689 tree newtype, tree oldtype)
1691 tree t;
1693 if (TREE_CODE (olddecl) != FUNCTION_DECL
1694 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1695 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1696 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1697 return;
1699 t = TYPE_ARG_TYPES (oldtype);
1700 if (t == NULL_TREE)
1701 t = TYPE_ARG_TYPES (newtype);
1702 for (; t; t = TREE_CHAIN (t))
1704 tree type = TREE_VALUE (t);
1706 if (TREE_CHAIN (t) == NULL_TREE
1707 && TYPE_MAIN_VARIANT (type) != void_type_node)
1709 inform (input_location, "a parameter list with an ellipsis can%'t match "
1710 "an empty parameter name list declaration");
1711 break;
1714 if (c_type_promotes_to (type) != type)
1716 inform (input_location, "an argument type that has a default promotion can%'t match "
1717 "an empty parameter name list declaration");
1718 break;
1723 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1724 old-style function definition, NEWDECL is a prototype declaration.
1725 Diagnose inconsistencies in the argument list. Returns TRUE if
1726 the prototype is compatible, FALSE if not. */
1727 static bool
1728 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1730 tree newargs, oldargs;
1731 int i;
1733 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1735 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1736 newargs = TYPE_ARG_TYPES (newtype);
1737 i = 1;
1739 for (;;)
1741 tree oldargtype = TREE_VALUE (oldargs);
1742 tree newargtype = TREE_VALUE (newargs);
1744 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1745 return false;
1747 oldargtype = (TYPE_ATOMIC (oldargtype)
1748 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1749 TYPE_QUAL_ATOMIC)
1750 : TYPE_MAIN_VARIANT (oldargtype));
1751 newargtype = (TYPE_ATOMIC (newargtype)
1752 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1753 TYPE_QUAL_ATOMIC)
1754 : TYPE_MAIN_VARIANT (newargtype));
1756 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1757 break;
1759 /* Reaching the end of just one list means the two decls don't
1760 agree on the number of arguments. */
1761 if (END_OF_ARGLIST (oldargtype))
1763 error ("prototype for %q+D declares more arguments "
1764 "than previous old-style definition", newdecl);
1765 return false;
1767 else if (END_OF_ARGLIST (newargtype))
1769 error ("prototype for %q+D declares fewer arguments "
1770 "than previous old-style definition", newdecl);
1771 return false;
1774 /* Type for passing arg must be consistent with that declared
1775 for the arg. */
1776 else if (!comptypes (oldargtype, newargtype))
1778 error ("prototype for %q+D declares argument %d"
1779 " with incompatible type",
1780 newdecl, i);
1781 return false;
1784 oldargs = TREE_CHAIN (oldargs);
1785 newargs = TREE_CHAIN (newargs);
1786 i++;
1789 /* If we get here, no errors were found, but do issue a warning
1790 for this poor-style construct. */
1791 warning (0, "prototype for %q+D follows non-prototype definition",
1792 newdecl);
1793 return true;
1794 #undef END_OF_ARGLIST
1797 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1798 first in a pair of mismatched declarations, using the diagnostic
1799 function DIAG. */
1800 static void
1801 locate_old_decl (tree decl)
1803 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl)
1804 && !C_DECL_DECLARED_BUILTIN (decl))
1806 else if (DECL_INITIAL (decl))
1807 inform (input_location, "previous definition of %q+D was here", decl);
1808 else if (C_DECL_IMPLICIT (decl))
1809 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1810 else
1811 inform (input_location, "previous declaration of %q+D was here", decl);
1814 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1815 Returns true if the caller should proceed to merge the two, false
1816 if OLDDECL should simply be discarded. As a side effect, issues
1817 all necessary diagnostics for invalid or poor-style combinations.
1818 If it returns true, writes the types of NEWDECL and OLDDECL to
1819 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1820 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1822 static bool
1823 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1824 tree *newtypep, tree *oldtypep)
1826 tree newtype, oldtype;
1827 bool retval = true;
1829 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1830 && DECL_EXTERNAL (DECL))
1832 /* If we have error_mark_node for either decl or type, just discard
1833 the previous decl - we're in an error cascade already. */
1834 if (olddecl == error_mark_node || newdecl == error_mark_node)
1835 return false;
1836 *oldtypep = oldtype = TREE_TYPE (olddecl);
1837 *newtypep = newtype = TREE_TYPE (newdecl);
1838 if (oldtype == error_mark_node || newtype == error_mark_node)
1839 return false;
1841 /* Two different categories of symbol altogether. This is an error
1842 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1843 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1845 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1846 && fndecl_built_in_p (olddecl)
1847 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1849 auto_diagnostic_group d;
1850 error ("%q+D redeclared as different kind of symbol", newdecl);
1851 locate_old_decl (olddecl);
1853 else if (TREE_PUBLIC (newdecl))
1854 warning (OPT_Wbuiltin_declaration_mismatch,
1855 "built-in function %q+D declared as non-function",
1856 newdecl);
1857 else
1858 warning (OPT_Wshadow, "declaration of %q+D shadows "
1859 "a built-in function", newdecl);
1860 return false;
1863 /* Enumerators have no linkage, so may only be declared once in a
1864 given scope. */
1865 if (TREE_CODE (olddecl) == CONST_DECL)
1867 auto_diagnostic_group d;
1868 error ("redeclaration of enumerator %q+D", newdecl);
1869 locate_old_decl (olddecl);
1870 return false;
1873 bool pedwarned = false;
1874 bool warned = false;
1875 auto_diagnostic_group d;
1877 if (!comptypes (oldtype, newtype))
1879 if (TREE_CODE (olddecl) == FUNCTION_DECL
1880 && fndecl_built_in_p (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1882 /* Accept harmless mismatch in function types.
1883 This is for the ffs and fprintf builtins. */
1884 tree trytype = match_builtin_function_types (newtype, oldtype);
1886 if (trytype && comptypes (newtype, trytype))
1887 *oldtypep = oldtype = trytype;
1888 else
1890 /* If types don't match for a built-in, throw away the
1891 built-in. No point in calling locate_old_decl here, it
1892 won't print anything. */
1893 warning (OPT_Wbuiltin_declaration_mismatch,
1894 "conflicting types for built-in function %q+D",
1895 newdecl);
1896 return false;
1899 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1900 && DECL_IS_BUILTIN (olddecl))
1902 /* A conflicting function declaration for a predeclared
1903 function that isn't actually built in. Objective C uses
1904 these. The new declaration silently overrides everything
1905 but the volatility (i.e. noreturn) indication. See also
1906 below. FIXME: Make Objective C use normal builtins. */
1907 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1908 return false;
1910 /* Permit void foo (...) to match int foo (...) if the latter is
1911 the definition and implicit int was used. See
1912 c-torture/compile/920625-2.c. */
1913 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1914 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1915 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1916 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1918 pedwarned = pedwarn (input_location, 0,
1919 "conflicting types for %q+D", newdecl);
1920 /* Make sure we keep void as the return type. */
1921 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1922 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1924 /* Permit void foo (...) to match an earlier call to foo (...) with
1925 no declared type (thus, implicitly int). */
1926 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1927 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1928 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1929 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1931 pedwarned = pedwarn (input_location, 0,
1932 "conflicting types for %q+D", newdecl);
1933 /* Make sure we keep void as the return type. */
1934 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1936 else
1938 int new_quals = TYPE_QUALS (newtype);
1939 int old_quals = TYPE_QUALS (oldtype);
1941 if (new_quals != old_quals)
1943 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1944 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1945 if (new_addr != old_addr)
1947 if (ADDR_SPACE_GENERIC_P (new_addr))
1948 error ("conflicting named address spaces (generic vs %s) "
1949 "for %q+D",
1950 c_addr_space_name (old_addr), newdecl);
1951 else if (ADDR_SPACE_GENERIC_P (old_addr))
1952 error ("conflicting named address spaces (%s vs generic) "
1953 "for %q+D",
1954 c_addr_space_name (new_addr), newdecl);
1955 else
1956 error ("conflicting named address spaces (%s vs %s) "
1957 "for %q+D",
1958 c_addr_space_name (new_addr),
1959 c_addr_space_name (old_addr),
1960 newdecl);
1963 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1964 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1965 error ("conflicting type qualifiers for %q+D", newdecl);
1967 else
1968 error ("conflicting types for %q+D", newdecl);
1969 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1970 locate_old_decl (olddecl);
1971 return false;
1975 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1976 but silently ignore the redeclaration if either is in a system
1977 header. (Conflicting redeclarations were handled above.) This
1978 is allowed for C11 if the types are the same, not just
1979 compatible. */
1980 if (TREE_CODE (newdecl) == TYPE_DECL)
1982 bool types_different = false;
1983 int comptypes_result;
1985 comptypes_result
1986 = comptypes_check_different_types (oldtype, newtype, &types_different);
1988 if (comptypes_result != 1 || types_different)
1990 error ("redefinition of typedef %q+D with different type", newdecl);
1991 locate_old_decl (olddecl);
1992 return false;
1995 if (DECL_IN_SYSTEM_HEADER (newdecl)
1996 || DECL_IN_SYSTEM_HEADER (olddecl)
1997 || TREE_NO_WARNING (newdecl)
1998 || TREE_NO_WARNING (olddecl))
1999 return true; /* Allow OLDDECL to continue in use. */
2001 if (variably_modified_type_p (newtype, NULL))
2003 error ("redefinition of typedef %q+D with variably modified type",
2004 newdecl);
2005 locate_old_decl (olddecl);
2007 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2008 "redefinition of typedef %q+D", newdecl))
2009 locate_old_decl (olddecl);
2011 return true;
2014 /* Function declarations can either be 'static' or 'extern' (no
2015 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2016 can never conflict with each other on account of linkage
2017 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2018 gnu89 mode permits two definitions if one is 'extern inline' and
2019 one is not. The non- extern-inline definition supersedes the
2020 extern-inline definition. */
2022 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2024 /* If you declare a built-in function name as static, or
2025 define the built-in with an old-style definition (so we
2026 can't validate the argument list) the built-in definition is
2027 overridden, but optionally warn this was a bad choice of name. */
2028 if (fndecl_built_in_p (olddecl)
2029 && !C_DECL_DECLARED_BUILTIN (olddecl)
2030 && (!TREE_PUBLIC (newdecl)
2031 || (DECL_INITIAL (newdecl)
2032 && !prototype_p (TREE_TYPE (newdecl)))))
2034 warning (OPT_Wshadow, "declaration of %q+D shadows "
2035 "a built-in function", newdecl);
2036 /* Discard the old built-in function. */
2037 return false;
2040 if (DECL_INITIAL (newdecl))
2042 if (DECL_INITIAL (olddecl))
2044 /* If both decls are in the same TU and the new declaration
2045 isn't overriding an extern inline reject the new decl.
2046 In c99, no overriding is allowed in the same translation
2047 unit. */
2048 if ((!DECL_EXTERN_INLINE (olddecl)
2049 || DECL_EXTERN_INLINE (newdecl)
2050 || (!flag_gnu89_inline
2051 && (!DECL_DECLARED_INLINE_P (olddecl)
2052 || !lookup_attribute ("gnu_inline",
2053 DECL_ATTRIBUTES (olddecl)))
2054 && (!DECL_DECLARED_INLINE_P (newdecl)
2055 || !lookup_attribute ("gnu_inline",
2056 DECL_ATTRIBUTES (newdecl))))
2058 && same_translation_unit_p (newdecl, olddecl))
2060 auto_diagnostic_group d;
2061 error ("redefinition of %q+D", newdecl);
2062 locate_old_decl (olddecl);
2063 return false;
2067 /* If we have a prototype after an old-style function definition,
2068 the argument types must be checked specially. */
2069 else if (DECL_INITIAL (olddecl)
2070 && !prototype_p (oldtype) && prototype_p (newtype)
2071 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2073 auto_diagnostic_group d;
2074 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2076 locate_old_decl (olddecl);
2077 return false;
2080 /* A non-static declaration (even an "extern") followed by a
2081 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2082 The same is true for a static forward declaration at block
2083 scope followed by a non-static declaration/definition at file
2084 scope. Static followed by non-static at the same scope is
2085 not undefined behavior, and is the most convenient way to get
2086 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2087 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2088 we do diagnose it if -Wtraditional. */
2089 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2091 /* Two exceptions to the rule. If olddecl is an extern
2092 inline, or a predeclared function that isn't actually
2093 built in, newdecl silently overrides olddecl. The latter
2094 occur only in Objective C; see also above. (FIXME: Make
2095 Objective C use normal builtins.) */
2096 if (!DECL_IS_BUILTIN (olddecl)
2097 && !DECL_EXTERN_INLINE (olddecl))
2099 auto_diagnostic_group d;
2100 error ("static declaration of %q+D follows "
2101 "non-static declaration", newdecl);
2102 locate_old_decl (olddecl);
2104 return false;
2106 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2108 if (DECL_CONTEXT (olddecl))
2110 auto_diagnostic_group d;
2111 error ("non-static declaration of %q+D follows "
2112 "static declaration", newdecl);
2113 locate_old_decl (olddecl);
2114 return false;
2116 else if (warn_traditional)
2118 warned |= warning (OPT_Wtraditional,
2119 "non-static declaration of %q+D "
2120 "follows static declaration", newdecl);
2124 /* Make sure gnu_inline attribute is either not present, or
2125 present on all inline decls. */
2126 if (DECL_DECLARED_INLINE_P (olddecl)
2127 && DECL_DECLARED_INLINE_P (newdecl))
2129 bool newa = lookup_attribute ("gnu_inline",
2130 DECL_ATTRIBUTES (newdecl)) != NULL;
2131 bool olda = lookup_attribute ("gnu_inline",
2132 DECL_ATTRIBUTES (olddecl)) != NULL;
2133 if (newa != olda)
2135 auto_diagnostic_group d;
2136 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2137 newa ? newdecl : olddecl);
2138 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2139 "but not here");
2143 else if (VAR_P (newdecl))
2145 /* Only variables can be thread-local, and all declarations must
2146 agree on this property. */
2147 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2149 /* Nothing to check. Since OLDDECL is marked threadprivate
2150 and NEWDECL does not have a thread-local attribute, we
2151 will merge the threadprivate attribute into NEWDECL. */
2154 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2156 auto_diagnostic_group d;
2157 if (DECL_THREAD_LOCAL_P (newdecl))
2158 error ("thread-local declaration of %q+D follows "
2159 "non-thread-local declaration", newdecl);
2160 else
2161 error ("non-thread-local declaration of %q+D follows "
2162 "thread-local declaration", newdecl);
2164 locate_old_decl (olddecl);
2165 return false;
2168 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2169 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2171 auto_diagnostic_group d;
2172 error ("redefinition of %q+D", newdecl);
2173 locate_old_decl (olddecl);
2174 return false;
2177 /* Objects declared at file scope: if the first declaration had
2178 external linkage (even if it was an external reference) the
2179 second must have external linkage as well, or the behavior is
2180 undefined. If the first declaration had internal linkage, then
2181 the second must too, or else be an external reference (in which
2182 case the composite declaration still has internal linkage).
2183 As for function declarations, we warn about the static-then-
2184 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2185 if (DECL_FILE_SCOPE_P (newdecl)
2186 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2188 if (DECL_EXTERNAL (newdecl))
2190 if (!DECL_FILE_SCOPE_P (olddecl))
2192 auto_diagnostic_group d;
2193 error ("extern declaration of %q+D follows "
2194 "declaration with no linkage", newdecl);
2195 locate_old_decl (olddecl);
2196 return false;
2198 else if (warn_traditional)
2200 warned |= warning (OPT_Wtraditional,
2201 "non-static declaration of %q+D "
2202 "follows static declaration", newdecl);
2205 else
2207 auto_diagnostic_group d;
2208 if (TREE_PUBLIC (newdecl))
2209 error ("non-static declaration of %q+D follows "
2210 "static declaration", newdecl);
2211 else
2212 error ("static declaration of %q+D follows "
2213 "non-static declaration", newdecl);
2215 locate_old_decl (olddecl);
2216 return false;
2219 /* Two objects with the same name declared at the same block
2220 scope must both be external references (6.7p3). */
2221 else if (!DECL_FILE_SCOPE_P (newdecl))
2223 if (DECL_EXTERNAL (newdecl))
2225 /* Extern with initializer at block scope, which will
2226 already have received an error. */
2228 else if (DECL_EXTERNAL (olddecl))
2230 auto_diagnostic_group d;
2231 error ("declaration of %q+D with no linkage follows "
2232 "extern declaration", newdecl);
2233 locate_old_decl (olddecl);
2235 else
2237 auto_diagnostic_group d;
2238 error ("redeclaration of %q+D with no linkage", newdecl);
2239 locate_old_decl (olddecl);
2242 return false;
2245 /* C++ does not permit a decl to appear multiple times at file
2246 scope. */
2247 if (warn_cxx_compat
2248 && DECL_FILE_SCOPE_P (newdecl)
2249 && !DECL_EXTERNAL (newdecl)
2250 && !DECL_EXTERNAL (olddecl))
2251 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2252 OPT_Wc___compat,
2253 ("duplicate declaration of %qD is "
2254 "invalid in C++"),
2255 newdecl);
2258 /* warnings */
2259 /* All decls must agree on a visibility. */
2260 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2261 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2262 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2264 warned |= warning (0, "redeclaration of %q+D with different visibility "
2265 "(old visibility preserved)", newdecl);
2268 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2269 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2270 else /* PARM_DECL, VAR_DECL */
2272 /* Redeclaration of a parameter is a constraint violation (this is
2273 not explicitly stated, but follows from C99 6.7p3 [no more than
2274 one declaration of the same identifier with no linkage in the
2275 same scope, except type tags] and 6.2.2p6 [parameters have no
2276 linkage]). We must check for a forward parameter declaration,
2277 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2278 an extension, the mandatory diagnostic for which is handled by
2279 mark_forward_parm_decls. */
2281 if (TREE_CODE (newdecl) == PARM_DECL
2282 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2284 auto_diagnostic_group d;
2285 error ("redefinition of parameter %q+D", newdecl);
2286 locate_old_decl (olddecl);
2287 return false;
2291 /* Optional warning for completely redundant decls. */
2292 if (!warned && !pedwarned
2293 && warn_redundant_decls
2294 /* Don't warn about a function declaration followed by a
2295 definition. */
2296 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2297 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2298 /* Don't warn about redundant redeclarations of builtins. */
2299 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2300 && !fndecl_built_in_p (newdecl)
2301 && fndecl_built_in_p (olddecl)
2302 && !C_DECL_DECLARED_BUILTIN (olddecl))
2303 /* Don't warn about an extern followed by a definition. */
2304 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2305 /* Don't warn about forward parameter decls. */
2306 && !(TREE_CODE (newdecl) == PARM_DECL
2307 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2308 /* Don't warn about a variable definition following a declaration. */
2309 && !(VAR_P (newdecl)
2310 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2312 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2313 newdecl);
2316 /* Report location of previous decl/defn. */
2317 if (warned || pedwarned)
2318 locate_old_decl (olddecl);
2320 #undef DECL_EXTERN_INLINE
2322 return retval;
2325 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2326 consistent with OLDDECL, but carries new information. Merge the
2327 new information into OLDDECL. This function issues no
2328 diagnostics. */
2330 static void
2331 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2333 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2334 && DECL_INITIAL (newdecl) != NULL_TREE);
2335 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2336 && prototype_p (TREE_TYPE (newdecl)));
2337 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2338 && prototype_p (TREE_TYPE (olddecl)));
2340 /* For real parm decl following a forward decl, rechain the old decl
2341 in its new location and clear TREE_ASM_WRITTEN (it's not a
2342 forward decl anymore). */
2343 if (TREE_CODE (newdecl) == PARM_DECL
2344 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2346 struct c_binding *b, **here;
2348 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2349 if ((*here)->decl == olddecl)
2350 goto found;
2351 gcc_unreachable ();
2353 found:
2354 b = *here;
2355 *here = b->prev;
2356 b->prev = current_scope->bindings;
2357 current_scope->bindings = b;
2359 TREE_ASM_WRITTEN (olddecl) = 0;
2362 DECL_ATTRIBUTES (newdecl)
2363 = targetm.merge_decl_attributes (olddecl, newdecl);
2365 /* For typedefs use the old type, as the new type's DECL_NAME points
2366 at newdecl, which will be ggc_freed. */
2367 if (TREE_CODE (newdecl) == TYPE_DECL)
2369 /* But NEWTYPE might have an attribute, honor that. */
2370 tree tem = newtype;
2371 newtype = oldtype;
2373 if (TYPE_USER_ALIGN (tem))
2375 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2376 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2377 TYPE_USER_ALIGN (newtype) = true;
2380 /* And remove the new type from the variants list. */
2381 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2383 tree remove = TREE_TYPE (newdecl);
2384 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2385 t = TYPE_NEXT_VARIANT (t))
2386 if (TYPE_NEXT_VARIANT (t) == remove)
2388 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2389 break;
2394 /* Merge the data types specified in the two decls. */
2395 TREE_TYPE (newdecl)
2396 = TREE_TYPE (olddecl)
2397 = composite_type (newtype, oldtype);
2399 /* Lay the type out, unless already done. */
2400 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2402 if (TREE_TYPE (newdecl) != error_mark_node)
2403 layout_type (TREE_TYPE (newdecl));
2404 if (TREE_CODE (newdecl) != FUNCTION_DECL
2405 && TREE_CODE (newdecl) != TYPE_DECL
2406 && TREE_CODE (newdecl) != CONST_DECL)
2407 layout_decl (newdecl, 0);
2409 else
2411 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2412 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2413 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2414 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2415 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2417 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2418 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2420 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2421 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2422 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2423 DECL_WARN_IF_NOT_ALIGN (olddecl));
2426 /* Keep the old rtl since we can safely use it. */
2427 if (HAS_RTL_P (olddecl))
2428 COPY_DECL_RTL (olddecl, newdecl);
2430 /* Merge the type qualifiers. */
2431 if (TREE_READONLY (newdecl))
2432 TREE_READONLY (olddecl) = 1;
2434 if (TREE_THIS_VOLATILE (newdecl))
2435 TREE_THIS_VOLATILE (olddecl) = 1;
2437 /* Merge deprecatedness. */
2438 if (TREE_DEPRECATED (newdecl))
2439 TREE_DEPRECATED (olddecl) = 1;
2441 /* If a decl is in a system header and the other isn't, keep the one on the
2442 system header. Otherwise, keep source location of definition rather than
2443 declaration and of prototype rather than non-prototype unless that
2444 prototype is built-in. */
2445 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2446 && DECL_IN_SYSTEM_HEADER (olddecl)
2447 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2448 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2449 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2450 && DECL_IN_SYSTEM_HEADER (newdecl)
2451 && !DECL_IN_SYSTEM_HEADER (olddecl))
2452 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2453 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2454 && DECL_INITIAL (olddecl) != NULL_TREE)
2455 || (old_is_prototype && !new_is_prototype
2456 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2457 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2459 /* Merge the initialization information. */
2460 if (DECL_INITIAL (newdecl) == NULL_TREE)
2461 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2463 /* Merge the threadprivate attribute. */
2464 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2465 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2467 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2469 /* Copy the assembler name.
2470 Currently, it can only be defined in the prototype. */
2471 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2473 /* Use visibility of whichever declaration had it specified */
2474 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2476 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2477 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2480 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2482 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2483 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2484 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2485 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2486 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2487 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2488 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2489 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2490 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2491 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2492 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2495 /* Merge the storage class information. */
2496 merge_weak (newdecl, olddecl);
2498 /* For functions, static overrides non-static. */
2499 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2501 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2502 /* This is since we don't automatically
2503 copy the attributes of NEWDECL into OLDDECL. */
2504 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2505 /* If this clears `static', clear it in the identifier too. */
2506 if (!TREE_PUBLIC (olddecl))
2507 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2511 /* In c99, 'extern' declaration before (or after) 'inline' means this
2512 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2513 is present. */
2514 if (TREE_CODE (newdecl) == FUNCTION_DECL
2515 && !flag_gnu89_inline
2516 && (DECL_DECLARED_INLINE_P (newdecl)
2517 || DECL_DECLARED_INLINE_P (olddecl))
2518 && (!DECL_DECLARED_INLINE_P (newdecl)
2519 || !DECL_DECLARED_INLINE_P (olddecl)
2520 || !DECL_EXTERNAL (olddecl))
2521 && DECL_EXTERNAL (newdecl)
2522 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2523 && !current_function_decl)
2524 DECL_EXTERNAL (newdecl) = 0;
2526 /* An inline definition following a static declaration is not
2527 DECL_EXTERNAL. */
2528 if (new_is_definition
2529 && (DECL_DECLARED_INLINE_P (newdecl)
2530 || DECL_DECLARED_INLINE_P (olddecl))
2531 && !TREE_PUBLIC (olddecl))
2532 DECL_EXTERNAL (newdecl) = 0;
2534 if (DECL_EXTERNAL (newdecl))
2536 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2537 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2539 /* An extern decl does not override previous storage class. */
2540 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2541 if (!DECL_EXTERNAL (newdecl))
2543 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2544 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2547 else
2549 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2550 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2553 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2555 /* If we're redefining a function previously defined as extern
2556 inline, make sure we emit debug info for the inline before we
2557 throw it away, in case it was inlined into a function that
2558 hasn't been written out yet. */
2559 if (new_is_definition && DECL_INITIAL (olddecl))
2560 /* The new defn must not be inline. */
2561 DECL_UNINLINABLE (newdecl) = 1;
2562 else
2564 /* If either decl says `inline', this fn is inline, unless
2565 its definition was passed already. */
2566 if (DECL_DECLARED_INLINE_P (newdecl)
2567 || DECL_DECLARED_INLINE_P (olddecl))
2568 DECL_DECLARED_INLINE_P (newdecl) = 1;
2570 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2571 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2573 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2574 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2575 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2576 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2579 if (fndecl_built_in_p (olddecl))
2581 /* If redeclaring a builtin function, it stays built in.
2582 But it gets tagged as having been declared. */
2583 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2584 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2585 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2586 if (new_is_prototype)
2588 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2589 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2591 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2592 switch (fncode)
2594 /* If a compatible prototype of these builtin functions
2595 is seen, assume the runtime implements it with the
2596 expected semantics. */
2597 case BUILT_IN_STPCPY:
2598 if (builtin_decl_explicit_p (fncode))
2599 set_builtin_decl_implicit_p (fncode, true);
2600 break;
2601 default:
2602 if (builtin_decl_explicit_p (fncode))
2603 set_builtin_decl_declared_p (fncode, true);
2604 break;
2607 copy_attributes_to_builtin (newdecl);
2610 else
2611 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2612 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2615 /* Preserve function specific target and optimization options */
2616 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2617 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2618 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2619 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2621 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2622 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2623 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2624 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2626 /* Also preserve various other info from the definition. */
2627 if (!new_is_definition)
2629 tree t;
2630 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2631 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2632 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2633 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2634 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2635 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2636 DECL_CONTEXT (t) = newdecl;
2638 /* See if we've got a function to instantiate from. */
2639 if (DECL_SAVED_TREE (olddecl))
2640 DECL_ABSTRACT_ORIGIN (newdecl)
2641 = DECL_ABSTRACT_ORIGIN (olddecl);
2645 /* Merge the USED information. */
2646 if (TREE_USED (olddecl))
2647 TREE_USED (newdecl) = 1;
2648 else if (TREE_USED (newdecl))
2649 TREE_USED (olddecl) = 1;
2650 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2651 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2652 if (DECL_PRESERVE_P (olddecl))
2653 DECL_PRESERVE_P (newdecl) = 1;
2654 else if (DECL_PRESERVE_P (newdecl))
2655 DECL_PRESERVE_P (olddecl) = 1;
2657 /* Merge DECL_COMMON */
2658 if (VAR_P (olddecl) && VAR_P (newdecl)
2659 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2660 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2661 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2663 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2664 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2665 DECL_ARGUMENTS (if appropriate). */
2667 unsigned olddecl_uid = DECL_UID (olddecl);
2668 tree olddecl_context = DECL_CONTEXT (olddecl);
2669 tree olddecl_arguments = NULL;
2670 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2671 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2673 memcpy ((char *) olddecl + sizeof (struct tree_common),
2674 (char *) newdecl + sizeof (struct tree_common),
2675 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2676 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2677 switch (TREE_CODE (olddecl))
2679 case FUNCTION_DECL:
2680 case VAR_DECL:
2682 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2684 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2685 (char *) newdecl + sizeof (struct tree_decl_common),
2686 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2687 olddecl->decl_with_vis.symtab_node = snode;
2689 if ((DECL_EXTERNAL (olddecl)
2690 || TREE_PUBLIC (olddecl)
2691 || TREE_STATIC (olddecl))
2692 && DECL_SECTION_NAME (newdecl) != NULL)
2693 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2695 /* This isn't quite correct for something like
2696 int __thread x attribute ((tls_model ("local-exec")));
2697 extern int __thread x;
2698 as we'll lose the "local-exec" model. */
2699 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2700 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2701 break;
2704 case FIELD_DECL:
2705 case PARM_DECL:
2706 case LABEL_DECL:
2707 case RESULT_DECL:
2708 case CONST_DECL:
2709 case TYPE_DECL:
2710 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2711 (char *) newdecl + sizeof (struct tree_decl_common),
2712 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2713 break;
2715 default:
2717 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2718 (char *) newdecl + sizeof (struct tree_decl_common),
2719 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2721 DECL_UID (olddecl) = olddecl_uid;
2722 DECL_CONTEXT (olddecl) = olddecl_context;
2723 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2724 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2727 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2728 so that encode_section_info has a chance to look at the new decl
2729 flags and attributes. */
2730 if (DECL_RTL_SET_P (olddecl)
2731 && (TREE_CODE (olddecl) == FUNCTION_DECL
2732 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2733 make_decl_rtl (olddecl);
2736 /* Handle when a new declaration NEWDECL has the same name as an old
2737 one OLDDECL in the same binding contour. Prints an error message
2738 if appropriate.
2740 If safely possible, alter OLDDECL to look like NEWDECL, and return
2741 true. Otherwise, return false. */
2743 static bool
2744 duplicate_decls (tree newdecl, tree olddecl)
2746 tree newtype = NULL, oldtype = NULL;
2748 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2750 /* Avoid `unused variable' and other warnings for OLDDECL. */
2751 TREE_NO_WARNING (olddecl) = 1;
2752 return false;
2755 merge_decls (newdecl, olddecl, newtype, oldtype);
2757 /* The NEWDECL will no longer be needed.
2759 Before releasing the node, be sure to remove function from symbol
2760 table that might have been inserted there to record comdat group.
2761 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2762 structure is shared in between NEWDECL and OLDECL. */
2763 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2764 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2765 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2767 struct symtab_node *snode = symtab_node::get (newdecl);
2768 if (snode)
2769 snode->remove ();
2771 ggc_free (newdecl);
2772 return true;
2776 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2777 static void
2778 warn_if_shadowing (tree new_decl)
2780 struct c_binding *b;
2782 /* Shadow warnings wanted? */
2783 if (!(warn_shadow
2784 || warn_shadow_local
2785 || warn_shadow_compatible_local)
2786 /* No shadow warnings for internally generated vars. */
2787 || DECL_IS_BUILTIN (new_decl)
2788 /* No shadow warnings for vars made for inlining. */
2789 || DECL_FROM_INLINE (new_decl))
2790 return;
2792 /* Is anything being shadowed? Invisible decls do not count. */
2793 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2794 if (b->decl && b->decl != new_decl && !b->invisible
2795 && (b->decl == error_mark_node
2796 || diagnostic_report_warnings_p (global_dc,
2797 DECL_SOURCE_LOCATION (b->decl))))
2799 tree old_decl = b->decl;
2801 if (old_decl == error_mark_node)
2803 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2804 "non-variable", new_decl);
2805 break;
2808 bool warned = false;
2809 auto_diagnostic_group d;
2810 if (TREE_CODE (old_decl) == PARM_DECL)
2812 enum opt_code warning_code;
2814 /* If '-Wshadow=compatible-local' is specified without other
2815 -Wshadow= flags, we will warn only when the types of the
2816 shadowing variable (i.e. new_decl) and the shadowed variable
2817 (old_decl) are compatible. */
2818 if (warn_shadow)
2819 warning_code = OPT_Wshadow;
2820 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2821 warning_code = OPT_Wshadow_compatible_local;
2822 else
2823 warning_code = OPT_Wshadow_local;
2824 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2825 "declaration of %qD shadows a parameter",
2826 new_decl);
2828 else if (DECL_FILE_SCOPE_P (old_decl))
2830 /* Do not warn if a variable shadows a function, unless
2831 the variable is a function or a pointer-to-function. */
2832 if (TREE_CODE (old_decl) == FUNCTION_DECL
2833 && TREE_CODE (new_decl) != FUNCTION_DECL
2834 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2835 continue;
2837 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2838 "declaration of %qD shadows a global "
2839 "declaration",
2840 new_decl);
2842 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2843 && fndecl_built_in_p (old_decl))
2845 warning (OPT_Wshadow, "declaration of %q+D shadows "
2846 "a built-in function", new_decl);
2847 break;
2849 else
2851 enum opt_code warning_code;
2853 /* If '-Wshadow=compatible-local' is specified without other
2854 -Wshadow= flags, we will warn only when the types of the
2855 shadowing variable (i.e. new_decl) and the shadowed variable
2856 (old_decl) are compatible. */
2857 if (warn_shadow)
2858 warning_code = OPT_Wshadow;
2859 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2860 warning_code = OPT_Wshadow_compatible_local;
2861 else
2862 warning_code = OPT_Wshadow_local;
2863 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2864 "declaration of %qD shadows a previous local",
2865 new_decl);
2868 if (warned)
2869 inform (DECL_SOURCE_LOCATION (old_decl),
2870 "shadowed declaration is here");
2872 break;
2876 /* Record a decl-node X as belonging to the current lexical scope.
2877 Check for errors (such as an incompatible declaration for the same
2878 name already seen in the same scope).
2880 Returns either X or an old decl for the same name.
2881 If an old decl is returned, it may have been smashed
2882 to agree with what X says. */
2884 tree
2885 pushdecl (tree x)
2887 tree name = DECL_NAME (x);
2888 struct c_scope *scope = current_scope;
2889 struct c_binding *b;
2890 bool nested = false;
2891 location_t locus = DECL_SOURCE_LOCATION (x);
2893 /* Must set DECL_CONTEXT for everything not at file scope or
2894 DECL_FILE_SCOPE_P won't work. Local externs don't count
2895 unless they have initializers (which generate code). */
2896 if (current_function_decl
2897 && (!VAR_OR_FUNCTION_DECL_P (x)
2898 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2899 DECL_CONTEXT (x) = current_function_decl;
2901 /* Anonymous decls are just inserted in the scope. */
2902 if (!name)
2904 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2905 locus);
2906 return x;
2909 /* First, see if there is another declaration with the same name in
2910 the current scope. If there is, duplicate_decls may do all the
2911 work for us. If duplicate_decls returns false, that indicates
2912 two incompatible decls in the same scope; we are to silently
2913 replace the old one (duplicate_decls has issued all appropriate
2914 diagnostics). In particular, we should not consider possible
2915 duplicates in the external scope, or shadowing. */
2916 b = I_SYMBOL_BINDING (name);
2917 if (b && B_IN_SCOPE (b, scope))
2919 struct c_binding *b_ext, *b_use;
2920 tree type = TREE_TYPE (x);
2921 tree visdecl = b->decl;
2922 tree vistype = TREE_TYPE (visdecl);
2923 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2924 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2925 b->inner_comp = false;
2926 b_use = b;
2927 b_ext = b;
2928 /* If this is an external linkage declaration, we should check
2929 for compatibility with the type in the external scope before
2930 setting the type at this scope based on the visible
2931 information only. */
2932 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2934 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2935 b_ext = b_ext->shadowed;
2936 if (b_ext)
2938 b_use = b_ext;
2939 if (b_use->u.type)
2940 TREE_TYPE (b_use->decl) = b_use->u.type;
2943 if (duplicate_decls (x, b_use->decl))
2945 if (b_use != b)
2947 /* Save the updated type in the external scope and
2948 restore the proper type for this scope. */
2949 tree thistype;
2950 if (comptypes (vistype, type))
2951 thistype = composite_type (vistype, type);
2952 else
2953 thistype = TREE_TYPE (b_use->decl);
2954 b_use->u.type = TREE_TYPE (b_use->decl);
2955 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2956 && fndecl_built_in_p (b_use->decl))
2957 thistype
2958 = build_type_attribute_variant (thistype,
2959 TYPE_ATTRIBUTES
2960 (b_use->u.type));
2961 TREE_TYPE (b_use->decl) = thistype;
2963 return b_use->decl;
2965 else
2966 goto skip_external_and_shadow_checks;
2969 /* All declarations with external linkage, and all external
2970 references, go in the external scope, no matter what scope is
2971 current. However, the binding in that scope is ignored for
2972 purposes of normal name lookup. A separate binding structure is
2973 created in the requested scope; this governs the normal
2974 visibility of the symbol.
2976 The binding in the externals scope is used exclusively for
2977 detecting duplicate declarations of the same object, no matter
2978 what scope they are in; this is what we do here. (C99 6.2.7p2:
2979 All declarations that refer to the same object or function shall
2980 have compatible type; otherwise, the behavior is undefined.) */
2981 if (DECL_EXTERNAL (x) || scope == file_scope)
2983 tree type = TREE_TYPE (x);
2984 tree vistype = NULL_TREE;
2985 tree visdecl = NULL_TREE;
2986 bool type_saved = false;
2987 if (b && !B_IN_EXTERNAL_SCOPE (b)
2988 && VAR_OR_FUNCTION_DECL_P (b->decl)
2989 && DECL_FILE_SCOPE_P (b->decl))
2991 visdecl = b->decl;
2992 vistype = TREE_TYPE (visdecl);
2994 if (scope != file_scope
2995 && !DECL_IN_SYSTEM_HEADER (x))
2996 warning_at (locus, OPT_Wnested_externs,
2997 "nested extern declaration of %qD", x);
2999 while (b && !B_IN_EXTERNAL_SCOPE (b))
3001 /* If this decl might be modified, save its type. This is
3002 done here rather than when the decl is first bound
3003 because the type may change after first binding, through
3004 being completed or through attributes being added. If we
3005 encounter multiple such decls, only the first should have
3006 its type saved; the others will already have had their
3007 proper types saved and the types will not have changed as
3008 their scopes will not have been re-entered. */
3009 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3011 b->u.type = TREE_TYPE (b->decl);
3012 type_saved = true;
3014 if (B_IN_FILE_SCOPE (b)
3015 && VAR_P (b->decl)
3016 && TREE_STATIC (b->decl)
3017 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3018 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3019 && TREE_CODE (type) == ARRAY_TYPE
3020 && TYPE_DOMAIN (type)
3021 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3022 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3024 /* Array type completed in inner scope, which should be
3025 diagnosed if the completion does not have size 1 and
3026 it does not get completed in the file scope. */
3027 b->inner_comp = true;
3029 b = b->shadowed;
3032 /* If a matching external declaration has been found, set its
3033 type to the composite of all the types of that declaration.
3034 After the consistency checks, it will be reset to the
3035 composite of the visible types only. */
3036 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3037 && b->u.type)
3038 TREE_TYPE (b->decl) = b->u.type;
3040 /* The point of the same_translation_unit_p check here is,
3041 we want to detect a duplicate decl for a construct like
3042 foo() { extern bar(); } ... static bar(); but not if
3043 they are in different translation units. In any case,
3044 the static does not go in the externals scope. */
3045 if (b
3046 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3047 && duplicate_decls (x, b->decl))
3049 tree thistype;
3050 if (vistype)
3052 if (comptypes (vistype, type))
3053 thistype = composite_type (vistype, type);
3054 else
3055 thistype = TREE_TYPE (b->decl);
3057 else
3058 thistype = type;
3059 b->u.type = TREE_TYPE (b->decl);
3060 if (TREE_CODE (b->decl) == FUNCTION_DECL
3061 && fndecl_built_in_p (b->decl))
3062 thistype
3063 = build_type_attribute_variant (thistype,
3064 TYPE_ATTRIBUTES (b->u.type));
3065 TREE_TYPE (b->decl) = thistype;
3066 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3067 locus);
3068 return b->decl;
3070 else if (TREE_PUBLIC (x))
3072 if (visdecl && !b && duplicate_decls (x, visdecl))
3074 /* An external declaration at block scope referring to a
3075 visible entity with internal linkage. The composite
3076 type will already be correct for this scope, so we
3077 just need to fall through to make the declaration in
3078 this scope. */
3079 nested = true;
3080 x = visdecl;
3082 else
3084 bind (name, x, external_scope, /*invisible=*/true,
3085 /*nested=*/false, locus);
3086 nested = true;
3091 if (TREE_CODE (x) != PARM_DECL)
3092 warn_if_shadowing (x);
3094 skip_external_and_shadow_checks:
3095 if (TREE_CODE (x) == TYPE_DECL)
3097 /* So this is a typedef, set its underlying type. */
3098 set_underlying_type (x);
3100 /* If X is a typedef defined in the current function, record it
3101 for the purpose of implementing the -Wunused-local-typedefs
3102 warning. */
3103 record_locally_defined_typedef (x);
3106 bind (name, x, scope, /*invisible=*/false, nested, locus);
3108 /* If x's type is incomplete because it's based on a
3109 structure or union which has not yet been fully declared,
3110 attach it to that structure or union type, so we can go
3111 back and complete the variable declaration later, if the
3112 structure or union gets fully declared.
3114 If the input is erroneous, we can have error_mark in the type
3115 slot (e.g. "f(void a, ...)") - that doesn't count as an
3116 incomplete type. */
3117 if (TREE_TYPE (x) != error_mark_node
3118 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3120 tree element = TREE_TYPE (x);
3122 while (TREE_CODE (element) == ARRAY_TYPE)
3123 element = TREE_TYPE (element);
3124 element = TYPE_MAIN_VARIANT (element);
3126 if (RECORD_OR_UNION_TYPE_P (element)
3127 && (TREE_CODE (x) != TYPE_DECL
3128 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3129 && !COMPLETE_TYPE_P (element))
3130 C_TYPE_INCOMPLETE_VARS (element)
3131 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3133 return x;
3137 /* Issue a warning about implicit function declaration. ID is the function
3138 identifier, OLDDECL is a declaration of the function in a different scope,
3139 or NULL_TREE. */
3141 static void
3142 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3144 if (!warn_implicit_function_declaration)
3145 return;
3147 bool warned;
3148 auto_diagnostic_group d;
3149 name_hint hint;
3150 if (!olddecl)
3151 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3153 if (flag_isoc99)
3155 if (hint)
3157 gcc_rich_location richloc (loc);
3158 richloc.add_fixit_replace (hint.suggestion ());
3159 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3160 "implicit declaration of function %qE;"
3161 " did you mean %qs?",
3162 id, hint.suggestion ());
3164 else
3165 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3166 "implicit declaration of function %qE", id);
3168 else if (hint)
3170 gcc_rich_location richloc (loc);
3171 richloc.add_fixit_replace (hint.suggestion ());
3172 warned = warning_at
3173 (&richloc, OPT_Wimplicit_function_declaration,
3174 G_("implicit declaration of function %qE; did you mean %qs?"),
3175 id, hint.suggestion ());
3177 else
3178 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3179 G_("implicit declaration of function %qE"), id);
3181 if (olddecl && warned)
3182 locate_old_decl (olddecl);
3184 if (!warned)
3185 hint.suppress ();
3188 /* This function represents mapping of a function code FCODE
3189 to its respective header. */
3191 static const char *
3192 header_for_builtin_fn (enum built_in_function fcode)
3194 switch (fcode)
3196 CASE_FLT_FN (BUILT_IN_ACOS):
3197 CASE_FLT_FN (BUILT_IN_ACOSH):
3198 CASE_FLT_FN (BUILT_IN_ASIN):
3199 CASE_FLT_FN (BUILT_IN_ASINH):
3200 CASE_FLT_FN (BUILT_IN_ATAN):
3201 CASE_FLT_FN (BUILT_IN_ATANH):
3202 CASE_FLT_FN (BUILT_IN_ATAN2):
3203 CASE_FLT_FN (BUILT_IN_CBRT):
3204 CASE_FLT_FN (BUILT_IN_CEIL):
3205 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3206 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3207 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3208 CASE_FLT_FN (BUILT_IN_COS):
3209 CASE_FLT_FN (BUILT_IN_COSH):
3210 CASE_FLT_FN (BUILT_IN_ERF):
3211 CASE_FLT_FN (BUILT_IN_ERFC):
3212 CASE_FLT_FN (BUILT_IN_EXP):
3213 CASE_FLT_FN (BUILT_IN_EXP2):
3214 CASE_FLT_FN (BUILT_IN_EXPM1):
3215 CASE_FLT_FN (BUILT_IN_FABS):
3216 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3217 CASE_FLT_FN (BUILT_IN_FDIM):
3218 CASE_FLT_FN (BUILT_IN_FLOOR):
3219 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3220 CASE_FLT_FN (BUILT_IN_FMA):
3221 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3222 CASE_FLT_FN (BUILT_IN_FMAX):
3223 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3224 CASE_FLT_FN (BUILT_IN_FMIN):
3225 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3226 CASE_FLT_FN (BUILT_IN_FMOD):
3227 CASE_FLT_FN (BUILT_IN_FREXP):
3228 CASE_FLT_FN (BUILT_IN_HYPOT):
3229 CASE_FLT_FN (BUILT_IN_ILOGB):
3230 CASE_FLT_FN (BUILT_IN_LDEXP):
3231 CASE_FLT_FN (BUILT_IN_LGAMMA):
3232 CASE_FLT_FN (BUILT_IN_LLRINT):
3233 CASE_FLT_FN (BUILT_IN_LLROUND):
3234 CASE_FLT_FN (BUILT_IN_LOG):
3235 CASE_FLT_FN (BUILT_IN_LOG10):
3236 CASE_FLT_FN (BUILT_IN_LOG1P):
3237 CASE_FLT_FN (BUILT_IN_LOG2):
3238 CASE_FLT_FN (BUILT_IN_LOGB):
3239 CASE_FLT_FN (BUILT_IN_LRINT):
3240 CASE_FLT_FN (BUILT_IN_LROUND):
3241 CASE_FLT_FN (BUILT_IN_MODF):
3242 CASE_FLT_FN (BUILT_IN_NAN):
3243 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3244 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3245 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3246 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3247 CASE_FLT_FN (BUILT_IN_POW):
3248 CASE_FLT_FN (BUILT_IN_REMAINDER):
3249 CASE_FLT_FN (BUILT_IN_REMQUO):
3250 CASE_FLT_FN (BUILT_IN_RINT):
3251 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3252 CASE_FLT_FN (BUILT_IN_ROUND):
3253 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3254 CASE_FLT_FN (BUILT_IN_SCALBLN):
3255 CASE_FLT_FN (BUILT_IN_SCALBN):
3256 CASE_FLT_FN (BUILT_IN_SIN):
3257 CASE_FLT_FN (BUILT_IN_SINH):
3258 CASE_FLT_FN (BUILT_IN_SINCOS):
3259 CASE_FLT_FN (BUILT_IN_SQRT):
3260 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3261 CASE_FLT_FN (BUILT_IN_TAN):
3262 CASE_FLT_FN (BUILT_IN_TANH):
3263 CASE_FLT_FN (BUILT_IN_TGAMMA):
3264 CASE_FLT_FN (BUILT_IN_TRUNC):
3265 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3266 case BUILT_IN_ISINF:
3267 case BUILT_IN_ISNAN:
3268 return "<math.h>";
3269 CASE_FLT_FN (BUILT_IN_CABS):
3270 CASE_FLT_FN (BUILT_IN_CACOS):
3271 CASE_FLT_FN (BUILT_IN_CACOSH):
3272 CASE_FLT_FN (BUILT_IN_CARG):
3273 CASE_FLT_FN (BUILT_IN_CASIN):
3274 CASE_FLT_FN (BUILT_IN_CASINH):
3275 CASE_FLT_FN (BUILT_IN_CATAN):
3276 CASE_FLT_FN (BUILT_IN_CATANH):
3277 CASE_FLT_FN (BUILT_IN_CCOS):
3278 CASE_FLT_FN (BUILT_IN_CCOSH):
3279 CASE_FLT_FN (BUILT_IN_CEXP):
3280 CASE_FLT_FN (BUILT_IN_CIMAG):
3281 CASE_FLT_FN (BUILT_IN_CLOG):
3282 CASE_FLT_FN (BUILT_IN_CONJ):
3283 CASE_FLT_FN (BUILT_IN_CPOW):
3284 CASE_FLT_FN (BUILT_IN_CPROJ):
3285 CASE_FLT_FN (BUILT_IN_CREAL):
3286 CASE_FLT_FN (BUILT_IN_CSIN):
3287 CASE_FLT_FN (BUILT_IN_CSINH):
3288 CASE_FLT_FN (BUILT_IN_CSQRT):
3289 CASE_FLT_FN (BUILT_IN_CTAN):
3290 CASE_FLT_FN (BUILT_IN_CTANH):
3291 return "<complex.h>";
3292 case BUILT_IN_MEMCHR:
3293 case BUILT_IN_MEMCMP:
3294 case BUILT_IN_MEMCPY:
3295 case BUILT_IN_MEMMOVE:
3296 case BUILT_IN_MEMSET:
3297 case BUILT_IN_STRCAT:
3298 case BUILT_IN_STRCHR:
3299 case BUILT_IN_STRCMP:
3300 case BUILT_IN_STRCPY:
3301 case BUILT_IN_STRCSPN:
3302 case BUILT_IN_STRLEN:
3303 case BUILT_IN_STRNCAT:
3304 case BUILT_IN_STRNCMP:
3305 case BUILT_IN_STRNCPY:
3306 case BUILT_IN_STRPBRK:
3307 case BUILT_IN_STRRCHR:
3308 case BUILT_IN_STRSPN:
3309 case BUILT_IN_STRSTR:
3310 return "<string.h>";
3311 case BUILT_IN_FPRINTF:
3312 case BUILT_IN_PUTC:
3313 case BUILT_IN_FPUTC:
3314 case BUILT_IN_FPUTS:
3315 case BUILT_IN_FSCANF:
3316 case BUILT_IN_FWRITE:
3317 case BUILT_IN_PRINTF:
3318 case BUILT_IN_PUTCHAR:
3319 case BUILT_IN_PUTS:
3320 case BUILT_IN_SCANF:
3321 case BUILT_IN_SNPRINTF:
3322 case BUILT_IN_SPRINTF:
3323 case BUILT_IN_SSCANF:
3324 case BUILT_IN_VFPRINTF:
3325 case BUILT_IN_VFSCANF:
3326 case BUILT_IN_VPRINTF:
3327 case BUILT_IN_VSCANF:
3328 case BUILT_IN_VSNPRINTF:
3329 case BUILT_IN_VSPRINTF:
3330 case BUILT_IN_VSSCANF:
3331 return "<stdio.h>";
3332 case BUILT_IN_ISALNUM:
3333 case BUILT_IN_ISALPHA:
3334 case BUILT_IN_ISBLANK:
3335 case BUILT_IN_ISCNTRL:
3336 case BUILT_IN_ISDIGIT:
3337 case BUILT_IN_ISGRAPH:
3338 case BUILT_IN_ISLOWER:
3339 case BUILT_IN_ISPRINT:
3340 case BUILT_IN_ISPUNCT:
3341 case BUILT_IN_ISSPACE:
3342 case BUILT_IN_ISUPPER:
3343 case BUILT_IN_ISXDIGIT:
3344 case BUILT_IN_TOLOWER:
3345 case BUILT_IN_TOUPPER:
3346 return "<ctype.h>";
3347 case BUILT_IN_ISWALNUM:
3348 case BUILT_IN_ISWALPHA:
3349 case BUILT_IN_ISWBLANK:
3350 case BUILT_IN_ISWCNTRL:
3351 case BUILT_IN_ISWDIGIT:
3352 case BUILT_IN_ISWGRAPH:
3353 case BUILT_IN_ISWLOWER:
3354 case BUILT_IN_ISWPRINT:
3355 case BUILT_IN_ISWPUNCT:
3356 case BUILT_IN_ISWSPACE:
3357 case BUILT_IN_ISWUPPER:
3358 case BUILT_IN_ISWXDIGIT:
3359 case BUILT_IN_TOWLOWER:
3360 case BUILT_IN_TOWUPPER:
3361 return "<wctype.h>";
3362 case BUILT_IN_ABORT:
3363 case BUILT_IN_ABS:
3364 case BUILT_IN_CALLOC:
3365 case BUILT_IN_EXIT:
3366 case BUILT_IN_FREE:
3367 case BUILT_IN_LABS:
3368 case BUILT_IN_LLABS:
3369 case BUILT_IN_MALLOC:
3370 case BUILT_IN_REALLOC:
3371 case BUILT_IN__EXIT2:
3372 case BUILT_IN_ALIGNED_ALLOC:
3373 return "<stdlib.h>";
3374 case BUILT_IN_IMAXABS:
3375 return "<inttypes.h>";
3376 case BUILT_IN_STRFTIME:
3377 return "<time.h>";
3378 default:
3379 return NULL;
3383 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3384 function of type int (). */
3386 tree
3387 implicitly_declare (location_t loc, tree functionid)
3389 struct c_binding *b;
3390 tree decl = NULL_TREE;
3391 tree asmspec_tree;
3393 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3395 if (B_IN_SCOPE (b, external_scope))
3397 decl = b->decl;
3398 break;
3402 if (decl)
3404 if (TREE_CODE (decl) != FUNCTION_DECL)
3405 return decl;
3407 /* FIXME: Objective-C has weird not-really-builtin functions
3408 which are supposed to be visible automatically. They wind up
3409 in the external scope because they're pushed before the file
3410 scope gets created. Catch this here and rebind them into the
3411 file scope. */
3412 if (!fndecl_built_in_p (decl) && DECL_IS_BUILTIN (decl))
3414 bind (functionid, decl, file_scope,
3415 /*invisible=*/false, /*nested=*/true,
3416 DECL_SOURCE_LOCATION (decl));
3417 return decl;
3419 else
3421 tree newtype = default_function_type;
3422 if (b->u.type)
3423 TREE_TYPE (decl) = b->u.type;
3424 /* Implicit declaration of a function already declared
3425 (somehow) in a different scope, or as a built-in.
3426 If this is the first time this has happened, warn;
3427 then recycle the old declaration but with the new type. */
3428 if (!C_DECL_IMPLICIT (decl))
3430 implicit_decl_warning (loc, functionid, decl);
3431 C_DECL_IMPLICIT (decl) = 1;
3433 if (fndecl_built_in_p (decl))
3435 newtype = build_type_attribute_variant (newtype,
3436 TYPE_ATTRIBUTES
3437 (TREE_TYPE (decl)));
3438 if (!comptypes (newtype, TREE_TYPE (decl)))
3440 bool warned = warning_at (loc, 0, "incompatible implicit "
3441 "declaration of built-in "
3442 "function %qD", decl);
3443 /* See if we can hint which header to include. */
3444 const char *header
3445 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3446 if (header != NULL && warned)
3448 rich_location richloc (line_table, loc);
3449 maybe_add_include_fixit (&richloc, header, true);
3450 inform (&richloc,
3451 "include %qs or provide a declaration of %qD",
3452 header, decl);
3454 newtype = TREE_TYPE (decl);
3457 else
3459 if (!comptypes (newtype, TREE_TYPE (decl)))
3461 auto_diagnostic_group d;
3462 error_at (loc, "incompatible implicit declaration of "
3463 "function %qD", decl);
3464 locate_old_decl (decl);
3467 b->u.type = TREE_TYPE (decl);
3468 TREE_TYPE (decl) = newtype;
3469 bind (functionid, decl, current_scope,
3470 /*invisible=*/false, /*nested=*/true,
3471 DECL_SOURCE_LOCATION (decl));
3472 return decl;
3476 /* Not seen before. */
3477 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3478 DECL_EXTERNAL (decl) = 1;
3479 TREE_PUBLIC (decl) = 1;
3480 C_DECL_IMPLICIT (decl) = 1;
3481 implicit_decl_warning (loc, functionid, 0);
3482 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3483 if (asmspec_tree)
3484 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3486 /* C89 says implicit declarations are in the innermost block.
3487 So we record the decl in the standard fashion. */
3488 decl = pushdecl (decl);
3490 /* No need to call objc_check_decl here - it's a function type. */
3491 rest_of_decl_compilation (decl, 0, 0);
3493 /* Write a record describing this implicit function declaration
3494 to the prototypes file (if requested). */
3495 gen_aux_info_record (decl, 0, 1, 0);
3497 /* Possibly apply some default attributes to this implicit declaration. */
3498 decl_attributes (&decl, NULL_TREE, 0);
3500 return decl;
3503 /* Issue an error message for a reference to an undeclared variable
3504 ID, including a reference to a builtin outside of function-call
3505 context. Establish a binding of the identifier to error_mark_node
3506 in an appropriate scope, which will suppress further errors for the
3507 same identifier. The error message should be given location LOC. */
3508 void
3509 undeclared_variable (location_t loc, tree id)
3511 static bool already = false;
3512 struct c_scope *scope;
3514 auto_diagnostic_group d;
3515 if (current_function_decl == NULL_TREE)
3517 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3518 if (guessed_id)
3520 gcc_rich_location richloc (loc);
3521 richloc.add_fixit_replace (guessed_id.suggestion ());
3522 error_at (&richloc,
3523 "%qE undeclared here (not in a function);"
3524 " did you mean %qs?",
3525 id, guessed_id.suggestion ());
3527 else
3528 error_at (loc, "%qE undeclared here (not in a function)", id);
3529 scope = current_scope;
3531 else
3533 if (!objc_diagnose_private_ivar (id))
3535 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3536 if (guessed_id)
3538 gcc_rich_location richloc (loc);
3539 richloc.add_fixit_replace (guessed_id.suggestion ());
3540 error_at (&richloc,
3541 "%qE undeclared (first use in this function);"
3542 " did you mean %qs?",
3543 id, guessed_id.suggestion ());
3545 else
3546 error_at (loc, "%qE undeclared (first use in this function)", id);
3548 if (!already)
3550 inform (loc, "each undeclared identifier is reported only"
3551 " once for each function it appears in");
3552 already = true;
3555 /* If we are parsing old-style parameter decls, current_function_decl
3556 will be nonnull but current_function_scope will be null. */
3557 scope = current_function_scope ? current_function_scope : current_scope;
3559 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3560 UNKNOWN_LOCATION);
3563 /* Subroutine of lookup_label, declare_label, define_label: construct a
3564 LABEL_DECL with all the proper frills. Also create a struct
3565 c_label_vars initialized for the current scope. */
3567 static tree
3568 make_label (location_t location, tree name, bool defining,
3569 struct c_label_vars **p_label_vars)
3571 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3572 DECL_CONTEXT (label) = current_function_decl;
3573 SET_DECL_MODE (label, VOIDmode);
3575 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3576 label_vars->shadowed = NULL;
3577 set_spot_bindings (&label_vars->label_bindings, defining);
3578 label_vars->decls_in_scope = make_tree_vector ();
3579 label_vars->gotos = NULL;
3580 *p_label_vars = label_vars;
3582 return label;
3585 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3586 Create one if none exists so far for the current function.
3587 This is called when a label is used in a goto expression or
3588 has its address taken. */
3590 tree
3591 lookup_label (tree name)
3593 tree label;
3594 struct c_label_vars *label_vars;
3596 if (current_function_scope == 0)
3598 error ("label %qE referenced outside of any function", name);
3599 return NULL_TREE;
3602 /* Use a label already defined or ref'd with this name, but not if
3603 it is inherited from a containing function and wasn't declared
3604 using __label__. */
3605 label = I_LABEL_DECL (name);
3606 if (label && (DECL_CONTEXT (label) == current_function_decl
3607 || C_DECLARED_LABEL_FLAG (label)))
3609 /* If the label has only been declared, update its apparent
3610 location to point here, for better diagnostics if it
3611 turns out not to have been defined. */
3612 if (DECL_INITIAL (label) == NULL_TREE)
3613 DECL_SOURCE_LOCATION (label) = input_location;
3614 return label;
3617 /* No label binding for that identifier; make one. */
3618 label = make_label (input_location, name, false, &label_vars);
3620 /* Ordinary labels go in the current function scope. */
3621 bind_label (name, label, current_function_scope, label_vars);
3623 return label;
3626 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3627 to LABEL. */
3629 static void
3630 warn_about_goto (location_t goto_loc, tree label, tree decl)
3632 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3633 error_at (goto_loc,
3634 "jump into scope of identifier with variably modified type");
3635 else
3636 warning_at (goto_loc, OPT_Wjump_misses_init,
3637 "jump skips variable initialization");
3638 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3639 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3642 /* Look up a label because of a goto statement. This is like
3643 lookup_label, but also issues any appropriate warnings. */
3645 tree
3646 lookup_label_for_goto (location_t loc, tree name)
3648 tree label;
3649 struct c_label_vars *label_vars;
3650 unsigned int ix;
3651 tree decl;
3653 label = lookup_label (name);
3654 if (label == NULL_TREE)
3655 return NULL_TREE;
3657 /* If we are jumping to a different function, we can't issue any
3658 useful warnings. */
3659 if (DECL_CONTEXT (label) != current_function_decl)
3661 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3662 return label;
3665 label_vars = I_LABEL_BINDING (name)->u.label;
3667 /* If the label has not yet been defined, then push this goto on a
3668 list for possible later warnings. */
3669 if (label_vars->label_bindings.scope == NULL)
3671 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3673 g->loc = loc;
3674 set_spot_bindings (&g->goto_bindings, true);
3675 vec_safe_push (label_vars->gotos, g);
3676 return label;
3679 /* If there are any decls in label_vars->decls_in_scope, then this
3680 goto has missed the declaration of the decl. This happens for a
3681 case like
3682 int i = 1;
3683 lab:
3685 goto lab;
3686 Issue a warning or error. */
3687 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3688 warn_about_goto (loc, label, decl);
3690 if (label_vars->label_bindings.left_stmt_expr)
3692 error_at (loc, "jump into statement expression");
3693 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3696 return label;
3699 /* Make a label named NAME in the current function, shadowing silently
3700 any that may be inherited from containing functions or containing
3701 scopes. This is called for __label__ declarations. */
3703 tree
3704 declare_label (tree name)
3706 struct c_binding *b = I_LABEL_BINDING (name);
3707 tree label;
3708 struct c_label_vars *label_vars;
3710 /* Check to make sure that the label hasn't already been declared
3711 at this scope */
3712 if (b && B_IN_CURRENT_SCOPE (b))
3714 auto_diagnostic_group d;
3715 error ("duplicate label declaration %qE", name);
3716 locate_old_decl (b->decl);
3718 /* Just use the previous declaration. */
3719 return b->decl;
3722 label = make_label (input_location, name, false, &label_vars);
3723 C_DECLARED_LABEL_FLAG (label) = 1;
3725 /* Declared labels go in the current scope. */
3726 bind_label (name, label, current_scope, label_vars);
3728 return label;
3731 /* When we define a label, issue any appropriate warnings if there are
3732 any gotos earlier in the function which jump to this label. */
3734 static void
3735 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3737 unsigned int ix;
3738 struct c_goto_bindings *g;
3740 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3742 struct c_binding *b;
3743 struct c_scope *scope;
3745 /* We have a goto to this label. The goto is going forward. In
3746 g->scope, the goto is going to skip any binding which was
3747 defined after g->bindings_in_scope. */
3748 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3750 for (b = g->goto_bindings.scope->bindings;
3751 b != g->goto_bindings.bindings_in_scope;
3752 b = b->prev)
3754 if (decl_jump_unsafe (b->decl))
3755 warn_about_goto (g->loc, label, b->decl);
3759 /* We also need to warn about decls defined in any scopes
3760 between the scope of the label and the scope of the goto. */
3761 for (scope = label_vars->label_bindings.scope;
3762 scope != g->goto_bindings.scope;
3763 scope = scope->outer)
3765 gcc_assert (scope != NULL);
3766 if (scope->has_jump_unsafe_decl)
3768 if (scope == label_vars->label_bindings.scope)
3769 b = label_vars->label_bindings.bindings_in_scope;
3770 else
3771 b = scope->bindings;
3772 for (; b != NULL; b = b->prev)
3774 if (decl_jump_unsafe (b->decl))
3775 warn_about_goto (g->loc, label, b->decl);
3780 if (g->goto_bindings.stmt_exprs > 0)
3782 error_at (g->loc, "jump into statement expression");
3783 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3784 label);
3788 /* Now that the label is defined, we will issue warnings about
3789 subsequent gotos to this label when we see them. */
3790 vec_safe_truncate (label_vars->gotos, 0);
3791 label_vars->gotos = NULL;
3794 /* Define a label, specifying the location in the source file.
3795 Return the LABEL_DECL node for the label, if the definition is valid.
3796 Otherwise return NULL_TREE. */
3798 tree
3799 define_label (location_t location, tree name)
3801 /* Find any preexisting label with this name. It is an error
3802 if that label has already been defined in this function, or
3803 if there is a containing function with a declared label with
3804 the same name. */
3805 tree label = I_LABEL_DECL (name);
3807 if (label
3808 && ((DECL_CONTEXT (label) == current_function_decl
3809 && DECL_INITIAL (label) != NULL_TREE)
3810 || (DECL_CONTEXT (label) != current_function_decl
3811 && C_DECLARED_LABEL_FLAG (label))))
3813 auto_diagnostic_group d;
3814 error_at (location, "duplicate label %qD", label);
3815 locate_old_decl (label);
3816 return NULL_TREE;
3818 else if (label && DECL_CONTEXT (label) == current_function_decl)
3820 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3822 /* The label has been used or declared already in this function,
3823 but not defined. Update its location to point to this
3824 definition. */
3825 DECL_SOURCE_LOCATION (label) = location;
3826 set_spot_bindings (&label_vars->label_bindings, true);
3828 /* Issue warnings as required about any goto statements from
3829 earlier in the function. */
3830 check_earlier_gotos (label, label_vars);
3832 else
3834 struct c_label_vars *label_vars;
3836 /* No label binding for that identifier; make one. */
3837 label = make_label (location, name, true, &label_vars);
3839 /* Ordinary labels go in the current function scope. */
3840 bind_label (name, label, current_function_scope, label_vars);
3843 if (!in_system_header_at (input_location) && lookup_name (name))
3844 warning_at (location, OPT_Wtraditional,
3845 "traditional C lacks a separate namespace "
3846 "for labels, identifier %qE conflicts", name);
3848 /* Mark label as having been defined. */
3849 DECL_INITIAL (label) = error_mark_node;
3850 return label;
3853 /* Get the bindings for a new switch statement. This is used to issue
3854 warnings as appropriate for jumps from the switch to case or
3855 default labels. */
3857 struct c_spot_bindings *
3858 c_get_switch_bindings (void)
3860 struct c_spot_bindings *switch_bindings;
3862 switch_bindings = XNEW (struct c_spot_bindings);
3863 set_spot_bindings (switch_bindings, true);
3864 return switch_bindings;
3867 void
3868 c_release_switch_bindings (struct c_spot_bindings *bindings)
3870 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3871 XDELETE (bindings);
3874 /* This is called at the point of a case or default label to issue
3875 warnings about decls as needed. It returns true if it found an
3876 error, not just a warning. */
3878 bool
3879 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3880 location_t switch_loc, location_t case_loc)
3882 bool saw_error;
3883 struct c_scope *scope;
3885 saw_error = false;
3886 for (scope = current_scope;
3887 scope != switch_bindings->scope;
3888 scope = scope->outer)
3890 struct c_binding *b;
3892 gcc_assert (scope != NULL);
3894 if (!scope->has_jump_unsafe_decl)
3895 continue;
3897 for (b = scope->bindings; b != NULL; b = b->prev)
3899 if (decl_jump_unsafe (b->decl))
3901 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3903 saw_error = true;
3904 error_at (case_loc,
3905 ("switch jumps into scope of identifier with "
3906 "variably modified type"));
3908 else
3909 warning_at (case_loc, OPT_Wjump_misses_init,
3910 "switch jumps over variable initialization");
3911 inform (switch_loc, "switch starts here");
3912 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3913 b->decl);
3918 if (switch_bindings->stmt_exprs > 0)
3920 saw_error = true;
3921 error_at (case_loc, "switch jumps into statement expression");
3922 inform (switch_loc, "switch starts here");
3925 return saw_error;
3928 /* Given NAME, an IDENTIFIER_NODE,
3929 return the structure (or union or enum) definition for that name.
3930 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3931 CODE says which kind of type the caller wants;
3932 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3933 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3934 location where the tag was defined.
3935 If the wrong kind of type is found, an error is reported. */
3937 static tree
3938 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3939 location_t *ploc)
3941 struct c_binding *b = I_TAG_BINDING (name);
3942 bool thislevel = false;
3944 if (!b || !b->decl)
3945 return NULL_TREE;
3947 /* We only care about whether it's in this level if
3948 thislevel_only was set or it might be a type clash. */
3949 if (thislevel_only || TREE_CODE (b->decl) != code)
3951 /* For our purposes, a tag in the external scope is the same as
3952 a tag in the file scope. (Primarily relevant to Objective-C
3953 and its builtin structure tags, which get pushed before the
3954 file scope is created.) */
3955 if (B_IN_CURRENT_SCOPE (b)
3956 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3957 thislevel = true;
3960 if (thislevel_only && !thislevel)
3961 return NULL_TREE;
3963 if (TREE_CODE (b->decl) != code)
3965 /* Definition isn't the kind we were looking for. */
3966 pending_invalid_xref = name;
3967 pending_invalid_xref_location = input_location;
3969 /* If in the same binding level as a declaration as a tag
3970 of a different type, this must not be allowed to
3971 shadow that tag, so give the error immediately.
3972 (For example, "struct foo; union foo;" is invalid.) */
3973 if (thislevel)
3974 pending_xref_error ();
3977 if (ploc != NULL)
3978 *ploc = b->locus;
3980 return b->decl;
3983 /* Return true if a definition exists for NAME with code CODE. */
3985 bool
3986 tag_exists_p (enum tree_code code, tree name)
3988 struct c_binding *b = I_TAG_BINDING (name);
3990 if (b == NULL || b->decl == NULL_TREE)
3991 return false;
3992 return TREE_CODE (b->decl) == code;
3995 /* Print an error message now
3996 for a recent invalid struct, union or enum cross reference.
3997 We don't print them immediately because they are not invalid
3998 when used in the `struct foo;' construct for shadowing. */
4000 void
4001 pending_xref_error (void)
4003 if (pending_invalid_xref != NULL_TREE)
4004 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4005 pending_invalid_xref);
4006 pending_invalid_xref = NULL_TREE;
4010 /* Look up NAME in the current scope and its superiors
4011 in the namespace of variables, functions and typedefs.
4012 Return a ..._DECL node of some kind representing its definition,
4013 or return NULL_TREE if it is undefined. */
4015 tree
4016 lookup_name (tree name)
4018 struct c_binding *b = I_SYMBOL_BINDING (name);
4019 if (b && !b->invisible)
4021 maybe_record_typedef_use (b->decl);
4022 return b->decl;
4024 return NULL_TREE;
4027 /* Similar to `lookup_name' but look only at the indicated scope. */
4029 static tree
4030 lookup_name_in_scope (tree name, struct c_scope *scope)
4032 struct c_binding *b;
4034 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4035 if (B_IN_SCOPE (b, scope))
4036 return b->decl;
4037 return NULL_TREE;
4040 /* Look for the closest match for NAME within the currently valid
4041 scopes.
4043 This finds the identifier with the lowest Levenshtein distance to
4044 NAME. If there are multiple candidates with equal minimal distance,
4045 the first one found is returned. Scopes are searched from innermost
4046 outwards, and within a scope in reverse order of declaration, thus
4047 benefiting candidates "near" to the current scope.
4049 The function also looks for similar macro names to NAME, since a
4050 misspelled macro name will not be expanded, and hence looks like an
4051 identifier to the C frontend.
4053 It also looks for start_typename keywords, to detect "singed" vs "signed"
4054 typos.
4056 Use LOC for any deferred diagnostics. */
4058 name_hint
4059 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4061 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4063 /* First, try some well-known names in the C standard library, in case
4064 the user forgot a #include. */
4065 const char *header_hint
4066 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4068 if (header_hint)
4069 return name_hint (NULL,
4070 new suggest_missing_header (loc,
4071 IDENTIFIER_POINTER (name),
4072 header_hint));
4074 /* Only suggest names reserved for the implementation if NAME begins
4075 with an underscore. */
4076 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4078 best_match<tree, tree> bm (name);
4080 /* Look within currently valid scopes. */
4081 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4082 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4084 if (!binding->id || binding->invisible)
4085 continue;
4086 if (binding->decl == error_mark_node)
4087 continue;
4088 /* Don't use bindings from implicitly declared functions,
4089 as they were likely misspellings themselves. */
4090 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4091 if (C_DECL_IMPLICIT (binding->decl))
4092 continue;
4093 /* Don't suggest names that are reserved for use by the
4094 implementation, unless NAME began with an underscore. */
4095 if (!consider_implementation_names)
4097 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4098 if (name_reserved_for_implementation_p (suggestion_str))
4099 continue;
4101 switch (kind)
4103 case FUZZY_LOOKUP_TYPENAME:
4104 if (TREE_CODE (binding->decl) != TYPE_DECL)
4105 continue;
4106 break;
4108 case FUZZY_LOOKUP_FUNCTION_NAME:
4109 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4111 /* Allow function pointers. */
4112 if ((VAR_P (binding->decl)
4113 || TREE_CODE (binding->decl) == PARM_DECL)
4114 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4115 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4116 == FUNCTION_TYPE))
4117 break;
4118 continue;
4120 break;
4122 default:
4123 break;
4125 bm.consider (binding->id);
4128 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4130 x = SOME_OTHER_MACRO (y);
4131 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4132 as a misspelled identifier.
4134 Use the best distance so far so that a candidate is only set if
4135 a macro is better than anything so far. This allows early rejection
4136 (without calculating the edit distance) of macro names that must have
4137 distance >= bm.get_best_distance (), and means that we only get a
4138 non-NULL result for best_macro_match if it's better than any of
4139 the identifiers already checked, which avoids needless creation
4140 of identifiers for macro hashnodes. */
4141 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4142 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4143 /* If a macro is the closest so far to NAME, use it, creating an
4144 identifier tree node for it. */
4145 if (best_macro)
4147 const char *id = (const char *)best_macro->ident.str;
4148 tree macro_as_identifier
4149 = get_identifier_with_length (id, best_macro->ident.len);
4150 bm.set_best_so_far (macro_as_identifier,
4151 bmm.get_best_distance (),
4152 bmm.get_best_candidate_length ());
4155 /* Try the "start_typename" keywords to detect
4156 "singed" vs "signed" typos. */
4157 if (kind == FUZZY_LOOKUP_TYPENAME)
4159 for (unsigned i = 0; i < num_c_common_reswords; i++)
4161 const c_common_resword *resword = &c_common_reswords[i];
4162 if (!c_keyword_starts_typename (resword->rid))
4163 continue;
4164 tree resword_identifier = ridpointers [resword->rid];
4165 if (!resword_identifier)
4166 continue;
4167 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4168 bm.consider (resword_identifier);
4172 tree best = bm.get_best_meaningful_candidate ();
4173 if (best)
4174 return name_hint (IDENTIFIER_POINTER (best), NULL);
4175 else
4176 return name_hint (NULL, NULL);
4180 /* Create the predefined scalar types of C,
4181 and some nodes representing standard constants (0, 1, (void *) 0).
4182 Initialize the global scope.
4183 Make definitions for built-in primitive functions. */
4185 void
4186 c_init_decl_processing (void)
4188 location_t save_loc = input_location;
4190 /* Initialize reserved words for parser. */
4191 c_parse_init ();
4193 current_function_decl = NULL_TREE;
4195 gcc_obstack_init (&parser_obstack);
4197 /* Make the externals scope. */
4198 push_scope ();
4199 external_scope = current_scope;
4201 /* Declarations from c_common_nodes_and_builtins must not be associated
4202 with this input file, lest we get differences between using and not
4203 using preprocessed headers. */
4204 input_location = BUILTINS_LOCATION;
4206 c_common_nodes_and_builtins ();
4208 /* In C, comparisons and TRUTH_* expressions have type int. */
4209 truthvalue_type_node = integer_type_node;
4210 truthvalue_true_node = integer_one_node;
4211 truthvalue_false_node = integer_zero_node;
4213 /* Even in C99, which has a real boolean type. */
4214 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4215 boolean_type_node));
4217 input_location = save_loc;
4219 make_fname_decl = c_make_fname_decl;
4220 start_fname_decls ();
4223 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4224 give the decl, NAME is the initialization string and TYPE_DEP
4225 indicates whether NAME depended on the type of the function. As we
4226 don't yet implement delayed emission of static data, we mark the
4227 decl as emitted so it is not placed in the output. Anything using
4228 it must therefore pull out the STRING_CST initializer directly.
4229 FIXME. */
4231 static tree
4232 c_make_fname_decl (location_t loc, tree id, int type_dep)
4234 const char *name = fname_as_string (type_dep);
4235 tree decl, type, init;
4236 size_t length = strlen (name);
4238 type = build_array_type (char_type_node,
4239 build_index_type (size_int (length)));
4240 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4242 decl = build_decl (loc, VAR_DECL, id, type);
4244 TREE_STATIC (decl) = 1;
4245 TREE_READONLY (decl) = 1;
4246 DECL_ARTIFICIAL (decl) = 1;
4248 init = build_string (length + 1, name);
4249 free (CONST_CAST (char *, name));
4250 TREE_TYPE (init) = type;
4251 DECL_INITIAL (decl) = init;
4253 TREE_USED (decl) = 1;
4255 if (current_function_decl
4256 /* For invalid programs like this:
4258 void foo()
4259 const char* p = __FUNCTION__;
4261 the __FUNCTION__ is believed to appear in K&R style function
4262 parameter declarator. In that case we still don't have
4263 function_scope. */
4264 && current_function_scope)
4266 DECL_CONTEXT (decl) = current_function_decl;
4267 bind (id, decl, current_function_scope,
4268 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4271 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4273 return decl;
4276 tree
4277 c_builtin_function (tree decl)
4279 tree type = TREE_TYPE (decl);
4280 tree id = DECL_NAME (decl);
4282 const char *name = IDENTIFIER_POINTER (id);
4283 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4285 /* Should never be called on a symbol with a preexisting meaning. */
4286 gcc_assert (!I_SYMBOL_BINDING (id));
4288 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4289 UNKNOWN_LOCATION);
4291 /* Builtins in the implementation namespace are made visible without
4292 needing to be explicitly declared. See push_file_scope. */
4293 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4295 DECL_CHAIN (decl) = visible_builtins;
4296 visible_builtins = decl;
4299 return decl;
4302 tree
4303 c_builtin_function_ext_scope (tree decl)
4305 tree type = TREE_TYPE (decl);
4306 tree id = DECL_NAME (decl);
4308 const char *name = IDENTIFIER_POINTER (id);
4309 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4311 if (external_scope)
4312 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4313 UNKNOWN_LOCATION);
4315 /* Builtins in the implementation namespace are made visible without
4316 needing to be explicitly declared. See push_file_scope. */
4317 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4319 DECL_CHAIN (decl) = visible_builtins;
4320 visible_builtins = decl;
4323 return decl;
4326 /* Called when a declaration is seen that contains no names to declare.
4327 If its type is a reference to a structure, union or enum inherited
4328 from a containing scope, shadow that tag name for the current scope
4329 with a forward reference.
4330 If its type defines a new named structure or union
4331 or defines an enum, it is valid but we need not do anything here.
4332 Otherwise, it is an error. */
4334 void
4335 shadow_tag (const struct c_declspecs *declspecs)
4337 shadow_tag_warned (declspecs, 0);
4340 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4341 but no pedwarn. */
4342 void
4343 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4345 bool found_tag = false;
4347 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4349 tree value = declspecs->type;
4350 enum tree_code code = TREE_CODE (value);
4352 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4353 /* Used to test also that TYPE_SIZE (value) != 0.
4354 That caused warning for `struct foo;' at top level in the file. */
4356 tree name = TYPE_NAME (value);
4357 tree t;
4359 found_tag = true;
4361 if (declspecs->restrict_p)
4363 error ("invalid use of %<restrict%>");
4364 warned = 1;
4367 if (name == NULL_TREE)
4369 if (warned != 1 && code != ENUMERAL_TYPE)
4370 /* Empty unnamed enum OK */
4372 pedwarn (input_location, 0,
4373 "unnamed struct/union that defines no instances");
4374 warned = 1;
4377 else if (declspecs->typespec_kind != ctsk_tagdef
4378 && declspecs->typespec_kind != ctsk_tagfirstref
4379 && declspecs->storage_class != csc_none)
4381 if (warned != 1)
4382 pedwarn (input_location, 0,
4383 "empty declaration with storage class specifier "
4384 "does not redeclare tag");
4385 warned = 1;
4386 pending_xref_error ();
4388 else if (declspecs->typespec_kind != ctsk_tagdef
4389 && declspecs->typespec_kind != ctsk_tagfirstref
4390 && (declspecs->const_p
4391 || declspecs->volatile_p
4392 || declspecs->atomic_p
4393 || declspecs->restrict_p
4394 || declspecs->address_space))
4396 if (warned != 1)
4397 pedwarn (input_location, 0,
4398 "empty declaration with type qualifier "
4399 "does not redeclare tag");
4400 warned = 1;
4401 pending_xref_error ();
4403 else if (declspecs->typespec_kind != ctsk_tagdef
4404 && declspecs->typespec_kind != ctsk_tagfirstref
4405 && declspecs->alignas_p)
4407 if (warned != 1)
4408 pedwarn (input_location, 0,
4409 "empty declaration with %<_Alignas%> "
4410 "does not redeclare tag");
4411 warned = 1;
4412 pending_xref_error ();
4414 else
4416 pending_invalid_xref = NULL_TREE;
4417 t = lookup_tag (code, name, true, NULL);
4419 if (t == NULL_TREE)
4421 t = make_node (code);
4422 pushtag (input_location, name, t);
4426 else
4428 if (warned != 1 && !in_system_header_at (input_location))
4430 pedwarn (input_location, 0,
4431 "useless type name in empty declaration");
4432 warned = 1;
4436 else if (warned != 1 && !in_system_header_at (input_location)
4437 && declspecs->typedef_p)
4439 pedwarn (input_location, 0, "useless type name in empty declaration");
4440 warned = 1;
4443 pending_invalid_xref = NULL_TREE;
4445 if (declspecs->inline_p)
4447 error ("%<inline%> in empty declaration");
4448 warned = 1;
4451 if (declspecs->noreturn_p)
4453 error ("%<_Noreturn%> in empty declaration");
4454 warned = 1;
4457 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4459 error ("%<auto%> in file-scope empty declaration");
4460 warned = 1;
4463 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4465 error ("%<register%> in file-scope empty declaration");
4466 warned = 1;
4469 if (!warned && !in_system_header_at (input_location)
4470 && declspecs->storage_class != csc_none)
4472 warning (0, "useless storage class specifier in empty declaration");
4473 warned = 2;
4476 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4478 warning (0, "useless %qs in empty declaration",
4479 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4480 warned = 2;
4483 if (!warned
4484 && !in_system_header_at (input_location)
4485 && (declspecs->const_p
4486 || declspecs->volatile_p
4487 || declspecs->atomic_p
4488 || declspecs->restrict_p
4489 || declspecs->address_space))
4491 warning (0, "useless type qualifier in empty declaration");
4492 warned = 2;
4495 if (!warned && !in_system_header_at (input_location)
4496 && declspecs->alignas_p)
4498 warning (0, "useless %<_Alignas%> in empty declaration");
4499 warned = 2;
4502 if (warned != 1)
4504 if (!found_tag)
4505 pedwarn (input_location, 0, "empty declaration");
4510 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4511 bits. SPECS represents declaration specifiers that the grammar
4512 only permits to contain type qualifiers and attributes. */
4515 quals_from_declspecs (const struct c_declspecs *specs)
4517 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4518 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4519 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4520 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4521 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4522 gcc_assert (!specs->type
4523 && !specs->decl_attr
4524 && specs->typespec_word == cts_none
4525 && specs->storage_class == csc_none
4526 && !specs->typedef_p
4527 && !specs->explicit_signed_p
4528 && !specs->deprecated_p
4529 && !specs->long_p
4530 && !specs->long_long_p
4531 && !specs->short_p
4532 && !specs->signed_p
4533 && !specs->unsigned_p
4534 && !specs->complex_p
4535 && !specs->inline_p
4536 && !specs->noreturn_p
4537 && !specs->thread_p);
4538 return quals;
4541 /* Construct an array declarator. LOC is the location of the
4542 beginning of the array (usually the opening brace). EXPR is the
4543 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4544 inside the [] (to be applied to the pointer to which a parameter
4545 array is converted). STATIC_P is true if "static" is inside the
4546 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4547 VLA of unspecified length which is nevertheless a complete type,
4548 false otherwise. The field for the contained declarator is left to
4549 be filled in by set_array_declarator_inner. */
4551 struct c_declarator *
4552 build_array_declarator (location_t loc,
4553 tree expr, struct c_declspecs *quals, bool static_p,
4554 bool vla_unspec_p)
4556 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4557 struct c_declarator);
4558 declarator->id_loc = loc;
4559 declarator->kind = cdk_array;
4560 declarator->declarator = 0;
4561 declarator->u.array.dimen = expr;
4562 if (quals)
4564 declarator->u.array.attrs = quals->attrs;
4565 declarator->u.array.quals = quals_from_declspecs (quals);
4567 else
4569 declarator->u.array.attrs = NULL_TREE;
4570 declarator->u.array.quals = 0;
4572 declarator->u.array.static_p = static_p;
4573 declarator->u.array.vla_unspec_p = vla_unspec_p;
4574 if (static_p || quals != NULL)
4575 pedwarn_c90 (loc, OPT_Wpedantic,
4576 "ISO C90 does not support %<static%> or type "
4577 "qualifiers in parameter array declarators");
4578 if (vla_unspec_p)
4579 pedwarn_c90 (loc, OPT_Wpedantic,
4580 "ISO C90 does not support %<[*]%> array declarators");
4581 if (vla_unspec_p)
4583 if (!current_scope->parm_flag)
4585 /* C99 6.7.5.2p4 */
4586 error_at (loc, "%<[*]%> not allowed in other than "
4587 "function prototype scope");
4588 declarator->u.array.vla_unspec_p = false;
4589 return NULL;
4591 current_scope->had_vla_unspec = true;
4593 return declarator;
4596 /* Set the contained declarator of an array declarator. DECL is the
4597 declarator, as constructed by build_array_declarator; INNER is what
4598 appears on the left of the []. */
4600 struct c_declarator *
4601 set_array_declarator_inner (struct c_declarator *decl,
4602 struct c_declarator *inner)
4604 decl->declarator = inner;
4605 return decl;
4608 /* INIT is a constructor that forms DECL's initializer. If the final
4609 element initializes a flexible array field, add the size of that
4610 initializer to DECL's size. */
4612 static void
4613 add_flexible_array_elts_to_size (tree decl, tree init)
4615 tree elt, type;
4617 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4618 return;
4620 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4621 type = TREE_TYPE (elt);
4622 if (TREE_CODE (type) == ARRAY_TYPE
4623 && TYPE_SIZE (type) == NULL_TREE
4624 && TYPE_DOMAIN (type) != NULL_TREE
4625 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4627 complete_array_type (&type, elt, false);
4628 DECL_SIZE (decl)
4629 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4630 DECL_SIZE_UNIT (decl)
4631 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4635 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4636 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4637 before the type name, and set *EXPR_CONST_OPERANDS, if
4638 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4639 appear in a constant expression. */
4641 tree
4642 groktypename (struct c_type_name *type_name, tree *expr,
4643 bool *expr_const_operands)
4645 tree type;
4646 tree attrs = type_name->specs->attrs;
4648 type_name->specs->attrs = NULL_TREE;
4650 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4651 false, NULL, &attrs, expr, expr_const_operands,
4652 DEPRECATED_NORMAL);
4654 /* Apply attributes. */
4655 decl_attributes (&type, attrs, 0);
4657 return type;
4660 /* Wrapper for decl_attributes that adds some implicit attributes
4661 to VAR_DECLs or FUNCTION_DECLs. */
4663 static tree
4664 c_decl_attributes (tree *node, tree attributes, int flags)
4666 /* Add implicit "omp declare target" attribute if requested. */
4667 if (current_omp_declare_target_attribute
4668 && ((VAR_P (*node) && is_global_var (*node))
4669 || TREE_CODE (*node) == FUNCTION_DECL))
4671 if (VAR_P (*node)
4672 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4673 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4674 NULL_TREE, attributes);
4675 else
4676 attributes = tree_cons (get_identifier ("omp declare target"),
4677 NULL_TREE, attributes);
4680 /* Look up the current declaration with all the attributes merged
4681 so far so that attributes on the current declaration that's
4682 about to be pushed that conflict with the former can be detected,
4683 diagnosed, and rejected as appropriate. */
4684 tree last_decl = lookup_name (DECL_NAME (*node));
4685 if (!last_decl)
4686 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4688 return decl_attributes (node, attributes, flags, last_decl);
4692 /* Decode a declarator in an ordinary declaration or data definition.
4693 This is called as soon as the type information and variable name
4694 have been parsed, before parsing the initializer if any.
4695 Here we create the ..._DECL node, fill in its type,
4696 and put it on the list of decls for the current context.
4697 The ..._DECL node is returned as the value.
4699 Exception: for arrays where the length is not specified,
4700 the type is left null, to be filled in by `finish_decl'.
4702 Function definitions do not come here; they go to start_function
4703 instead. However, external and forward declarations of functions
4704 do go through here. Structure field declarations are done by
4705 grokfield and not through here. */
4707 tree
4708 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4709 bool initialized, tree attributes)
4711 tree decl;
4712 tree tem;
4713 tree expr = NULL_TREE;
4714 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4716 /* An object declared as __attribute__((deprecated)) suppresses
4717 warnings of uses of other deprecated items. */
4718 if (lookup_attribute ("deprecated", attributes))
4719 deprecated_state = DEPRECATED_SUPPRESS;
4721 decl = grokdeclarator (declarator, declspecs,
4722 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4723 deprecated_state);
4724 if (!decl || decl == error_mark_node)
4725 return NULL_TREE;
4727 if (expr)
4728 add_stmt (fold_convert (void_type_node, expr));
4730 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
4731 && TREE_PUBLIC (decl))
4732 warning (OPT_Wmain, "%q+D is usually a function", decl);
4734 if (initialized)
4735 /* Is it valid for this decl to have an initializer at all?
4736 If not, set INITIALIZED to zero, which will indirectly
4737 tell 'finish_decl' to ignore the initializer once it is parsed. */
4738 switch (TREE_CODE (decl))
4740 case TYPE_DECL:
4741 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4742 initialized = false;
4743 break;
4745 case FUNCTION_DECL:
4746 error ("function %qD is initialized like a variable", decl);
4747 initialized = false;
4748 break;
4750 case PARM_DECL:
4751 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4752 error ("parameter %qD is initialized", decl);
4753 initialized = false;
4754 break;
4756 default:
4757 /* Don't allow initializations for incomplete types except for
4758 arrays which might be completed by the initialization. */
4760 /* This can happen if the array size is an undefined macro.
4761 We already gave a warning, so we don't need another one. */
4762 if (TREE_TYPE (decl) == error_mark_node)
4763 initialized = false;
4764 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4766 /* A complete type is ok if size is fixed. */
4768 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4769 || C_DECL_VARIABLE_SIZE (decl))
4771 error ("variable-sized object may not be initialized");
4772 initialized = false;
4775 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4777 error ("variable %qD has initializer but incomplete type", decl);
4778 initialized = false;
4780 else if (C_DECL_VARIABLE_SIZE (decl))
4782 /* Although C99 is unclear about whether incomplete arrays
4783 of VLAs themselves count as VLAs, it does not make
4784 sense to permit them to be initialized given that
4785 ordinary VLAs may not be initialized. */
4786 error ("variable-sized object may not be initialized");
4787 initialized = false;
4791 if (initialized)
4793 if (current_scope == file_scope)
4794 TREE_STATIC (decl) = 1;
4796 /* Tell 'pushdecl' this is an initialized decl
4797 even though we don't yet have the initializer expression.
4798 Also tell 'finish_decl' it may store the real initializer. */
4799 DECL_INITIAL (decl) = error_mark_node;
4802 /* If this is a function declaration, write a record describing it to the
4803 prototypes file (if requested). */
4805 if (TREE_CODE (decl) == FUNCTION_DECL)
4806 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4808 /* ANSI specifies that a tentative definition which is not merged with
4809 a non-tentative definition behaves exactly like a definition with an
4810 initializer equal to zero. (Section 3.7.2)
4812 -fno-common gives strict ANSI behavior, though this tends to break
4813 a large body of code that grew up without this rule.
4815 Thread-local variables are never common, since there's no entrenched
4816 body of code to break, and it allows more efficient variable references
4817 in the presence of dynamic linking. */
4819 if (VAR_P (decl)
4820 && !initialized
4821 && TREE_PUBLIC (decl)
4822 && !DECL_THREAD_LOCAL_P (decl)
4823 && !flag_no_common)
4824 DECL_COMMON (decl) = 1;
4826 /* Set attributes here so if duplicate decl, will have proper attributes. */
4827 c_decl_attributes (&decl, attributes, 0);
4829 /* Handle gnu_inline attribute. */
4830 if (declspecs->inline_p
4831 && !flag_gnu89_inline
4832 && TREE_CODE (decl) == FUNCTION_DECL
4833 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4834 || current_function_decl))
4836 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4838 else if (declspecs->storage_class != csc_static)
4839 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4842 if (TREE_CODE (decl) == FUNCTION_DECL
4843 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4845 struct c_declarator *ce = declarator;
4847 if (ce->kind == cdk_pointer)
4848 ce = declarator->declarator;
4849 if (ce->kind == cdk_function)
4851 tree args = ce->u.arg_info->parms;
4852 for (; args; args = DECL_CHAIN (args))
4854 tree type = TREE_TYPE (args);
4855 if (type && INTEGRAL_TYPE_P (type)
4856 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4857 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4862 if (TREE_CODE (decl) == FUNCTION_DECL
4863 && DECL_DECLARED_INLINE_P (decl)
4864 && DECL_UNINLINABLE (decl)
4865 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4866 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4867 decl);
4869 /* C99 6.7.4p3: An inline definition of a function with external
4870 linkage shall not contain a definition of a modifiable object
4871 with static storage duration... */
4872 if (VAR_P (decl)
4873 && current_scope != file_scope
4874 && TREE_STATIC (decl)
4875 && !TREE_READONLY (decl)
4876 && DECL_DECLARED_INLINE_P (current_function_decl)
4877 && DECL_EXTERNAL (current_function_decl))
4878 record_inline_static (input_location, current_function_decl,
4879 decl, csi_modifiable);
4881 if (c_dialect_objc ()
4882 && VAR_OR_FUNCTION_DECL_P (decl))
4883 objc_check_global_decl (decl);
4885 /* Add this decl to the current scope.
4886 TEM may equal DECL or it may be a previous decl of the same name. */
4887 tem = pushdecl (decl);
4889 if (initialized && DECL_EXTERNAL (tem))
4891 DECL_EXTERNAL (tem) = 0;
4892 TREE_STATIC (tem) = 1;
4895 return tem;
4898 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4899 DECL or the non-array element type if DECL is an uninitialized array.
4900 If that type has a const member, diagnose this. */
4902 static void
4903 diagnose_uninitialized_cst_member (tree decl, tree type)
4905 tree field;
4906 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4908 tree field_type;
4909 if (TREE_CODE (field) != FIELD_DECL)
4910 continue;
4911 field_type = strip_array_types (TREE_TYPE (field));
4913 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4915 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4916 "uninitialized const member in %qT is invalid in C++",
4917 strip_array_types (TREE_TYPE (decl)));
4918 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4921 if (RECORD_OR_UNION_TYPE_P (field_type))
4922 diagnose_uninitialized_cst_member (decl, field_type);
4926 /* Finish processing of a declaration;
4927 install its initial value.
4928 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4929 If the length of an array type is not known before,
4930 it must be determined now, from the initial value, or it is an error.
4932 INIT_LOC is the location of the initial value. */
4934 void
4935 finish_decl (tree decl, location_t init_loc, tree init,
4936 tree origtype, tree asmspec_tree)
4938 tree type;
4939 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4940 const char *asmspec = 0;
4942 /* If a name was specified, get the string. */
4943 if (VAR_OR_FUNCTION_DECL_P (decl)
4944 && DECL_FILE_SCOPE_P (decl))
4945 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4946 if (asmspec_tree)
4947 asmspec = TREE_STRING_POINTER (asmspec_tree);
4949 if (VAR_P (decl)
4950 && TREE_STATIC (decl)
4951 && global_bindings_p ())
4952 /* So decl is a global variable. Record the types it uses
4953 so that we can decide later to emit debug info for them. */
4954 record_types_used_by_current_var_decl (decl);
4956 /* If `start_decl' didn't like having an initialization, ignore it now. */
4957 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4958 init = NULL_TREE;
4960 /* Don't crash if parm is initialized. */
4961 if (TREE_CODE (decl) == PARM_DECL)
4962 init = NULL_TREE;
4964 if (init)
4965 store_init_value (init_loc, decl, init, origtype);
4967 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4968 || TREE_CODE (decl) == FIELD_DECL))
4969 objc_check_decl (decl);
4971 type = TREE_TYPE (decl);
4973 /* Deduce size of array from initialization, if not already known. */
4974 if (TREE_CODE (type) == ARRAY_TYPE
4975 && TYPE_DOMAIN (type) == NULL_TREE
4976 && TREE_CODE (decl) != TYPE_DECL)
4978 bool do_default
4979 = (TREE_STATIC (decl)
4980 /* Even if pedantic, an external linkage array
4981 may have incomplete type at first. */
4982 ? pedantic && !TREE_PUBLIC (decl)
4983 : !DECL_EXTERNAL (decl));
4984 int failure
4985 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4986 do_default);
4988 /* Get the completed type made by complete_array_type. */
4989 type = TREE_TYPE (decl);
4991 switch (failure)
4993 case 1:
4994 error ("initializer fails to determine size of %q+D", decl);
4995 break;
4997 case 2:
4998 if (do_default)
4999 error ("array size missing in %q+D", decl);
5000 break;
5002 case 3:
5003 error ("zero or negative size array %q+D", decl);
5004 break;
5006 case 0:
5007 /* For global variables, update the copy of the type that
5008 exists in the binding. */
5009 if (TREE_PUBLIC (decl))
5011 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5012 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5013 b_ext = b_ext->shadowed;
5014 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5016 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5017 b_ext->u.type = composite_type (b_ext->u.type, type);
5018 else
5019 b_ext->u.type = type;
5022 break;
5024 default:
5025 gcc_unreachable ();
5028 if (DECL_INITIAL (decl))
5029 TREE_TYPE (DECL_INITIAL (decl)) = type;
5031 relayout_decl (decl);
5034 if (TREE_CODE (type) == ARRAY_TYPE
5035 && TYPE_STRING_FLAG (TREE_TYPE (type))
5036 && DECL_INITIAL (decl)
5037 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR)
5038 DECL_INITIAL (decl) = braced_list_to_string (type, DECL_INITIAL (decl));
5040 if (VAR_P (decl))
5042 if (init && TREE_CODE (init) == CONSTRUCTOR)
5043 add_flexible_array_elts_to_size (decl, init);
5045 complete_flexible_array_elts (DECL_INITIAL (decl));
5047 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5048 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5049 layout_decl (decl, 0);
5051 if (DECL_SIZE (decl) == NULL_TREE
5052 /* Don't give an error if we already gave one earlier. */
5053 && TREE_TYPE (decl) != error_mark_node
5054 && (TREE_STATIC (decl)
5055 /* A static variable with an incomplete type
5056 is an error if it is initialized.
5057 Also if it is not file scope.
5058 Otherwise, let it through, but if it is not `extern'
5059 then it may cause an error message later. */
5060 ? (DECL_INITIAL (decl) != NULL_TREE
5061 || !DECL_FILE_SCOPE_P (decl))
5062 /* An automatic variable with an incomplete type
5063 is an error. */
5064 : !DECL_EXTERNAL (decl)))
5066 error ("storage size of %q+D isn%'t known", decl);
5067 TREE_TYPE (decl) = error_mark_node;
5070 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5071 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5072 && DECL_SIZE (decl) == NULL_TREE
5073 && TREE_STATIC (decl))
5074 incomplete_record_decls.safe_push (decl);
5076 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5078 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5079 constant_expression_warning (DECL_SIZE (decl));
5080 else
5082 error ("storage size of %q+D isn%'t constant", decl);
5083 TREE_TYPE (decl) = error_mark_node;
5087 if (TREE_USED (type))
5089 TREE_USED (decl) = 1;
5090 DECL_READ_P (decl) = 1;
5094 /* If this is a function and an assembler name is specified, reset DECL_RTL
5095 so we can give it its new name. Also, update builtin_decl if it
5096 was a normal built-in. */
5097 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5099 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5100 set_builtin_user_assembler_name (decl, asmspec);
5101 set_user_assembler_name (decl, asmspec);
5104 /* If #pragma weak was used, mark the decl weak now. */
5105 maybe_apply_pragma_weak (decl);
5107 /* Output the assembler code and/or RTL code for variables and functions,
5108 unless the type is an undefined structure or union.
5109 If not, it will get done when the type is completed. */
5111 if (VAR_OR_FUNCTION_DECL_P (decl))
5113 /* Determine the ELF visibility. */
5114 if (TREE_PUBLIC (decl))
5115 c_determine_visibility (decl);
5117 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5118 if (c_dialect_objc ())
5119 objc_check_decl (decl);
5121 if (asmspec)
5123 /* If this is not a static variable, issue a warning.
5124 It doesn't make any sense to give an ASMSPEC for an
5125 ordinary, non-register local variable. Historically,
5126 GCC has accepted -- but ignored -- the ASMSPEC in
5127 this case. */
5128 if (!DECL_FILE_SCOPE_P (decl)
5129 && VAR_P (decl)
5130 && !C_DECL_REGISTER (decl)
5131 && !TREE_STATIC (decl))
5132 warning (0, "ignoring asm-specifier for non-static local "
5133 "variable %q+D", decl);
5134 else
5135 set_user_assembler_name (decl, asmspec);
5138 if (DECL_FILE_SCOPE_P (decl))
5140 if (DECL_INITIAL (decl) == NULL_TREE
5141 || DECL_INITIAL (decl) == error_mark_node)
5142 /* Don't output anything
5143 when a tentative file-scope definition is seen.
5144 But at end of compilation, do output code for them. */
5145 DECL_DEFER_OUTPUT (decl) = 1;
5146 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5147 DECL_HARD_REGISTER (decl) = 1;
5148 rest_of_decl_compilation (decl, true, 0);
5150 else
5152 /* In conjunction with an ASMSPEC, the `register'
5153 keyword indicates that we should place the variable
5154 in a particular register. */
5155 if (asmspec && C_DECL_REGISTER (decl))
5157 DECL_HARD_REGISTER (decl) = 1;
5158 /* This cannot be done for a structure with volatile
5159 fields, on which DECL_REGISTER will have been
5160 reset. */
5161 if (!DECL_REGISTER (decl))
5162 error ("cannot put object with volatile field into register");
5165 if (TREE_CODE (decl) != FUNCTION_DECL)
5167 /* If we're building a variable sized type, and we might be
5168 reachable other than via the top of the current binding
5169 level, then create a new BIND_EXPR so that we deallocate
5170 the object at the right time. */
5171 /* Note that DECL_SIZE can be null due to errors. */
5172 if (DECL_SIZE (decl)
5173 && !TREE_CONSTANT (DECL_SIZE (decl))
5174 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5176 tree bind;
5177 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5178 TREE_SIDE_EFFECTS (bind) = 1;
5179 add_stmt (bind);
5180 BIND_EXPR_BODY (bind) = push_stmt_list ();
5182 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5183 DECL_EXPR, decl));
5188 if (!DECL_FILE_SCOPE_P (decl))
5190 /* Recompute the RTL of a local array now
5191 if it used to be an incomplete type. */
5192 if (was_incomplete && !is_global_var (decl))
5194 /* If we used it already as memory, it must stay in memory. */
5195 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5196 /* If it's still incomplete now, no init will save it. */
5197 if (DECL_SIZE (decl) == NULL_TREE)
5198 DECL_INITIAL (decl) = NULL_TREE;
5203 if (TREE_CODE (decl) == TYPE_DECL)
5205 if (!DECL_FILE_SCOPE_P (decl)
5206 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5207 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5209 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5212 /* Install a cleanup (aka destructor) if one was given. */
5213 if (VAR_P (decl) && !TREE_STATIC (decl))
5215 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5216 if (attr)
5218 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5219 tree cleanup_decl = lookup_name (cleanup_id);
5220 tree cleanup;
5221 vec<tree, va_gc> *v;
5223 /* Build "cleanup(&decl)" for the destructor. */
5224 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5225 vec_alloc (v, 1);
5226 v->quick_push (cleanup);
5227 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5228 vNULL, cleanup_decl, v, NULL);
5229 vec_free (v);
5231 /* Don't warn about decl unused; the cleanup uses it. */
5232 TREE_USED (decl) = 1;
5233 TREE_USED (cleanup_decl) = 1;
5234 DECL_READ_P (decl) = 1;
5236 push_cleanup (decl, cleanup, false);
5240 if (warn_cxx_compat
5241 && VAR_P (decl)
5242 && !DECL_EXTERNAL (decl)
5243 && DECL_INITIAL (decl) == NULL_TREE)
5245 type = strip_array_types (type);
5246 if (TREE_READONLY (decl))
5247 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5248 "uninitialized const %qD is invalid in C++", decl);
5249 else if (RECORD_OR_UNION_TYPE_P (type)
5250 && C_TYPE_FIELDS_READONLY (type))
5251 diagnose_uninitialized_cst_member (decl, type);
5254 if (flag_openmp
5255 && VAR_P (decl)
5256 && lookup_attribute ("omp declare target implicit",
5257 DECL_ATTRIBUTES (decl)))
5259 DECL_ATTRIBUTES (decl)
5260 = remove_attribute ("omp declare target implicit",
5261 DECL_ATTRIBUTES (decl));
5262 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5263 error ("%q+D in declare target directive does not have mappable type",
5264 decl);
5265 else if (!lookup_attribute ("omp declare target",
5266 DECL_ATTRIBUTES (decl))
5267 && !lookup_attribute ("omp declare target link",
5268 DECL_ATTRIBUTES (decl)))
5269 DECL_ATTRIBUTES (decl)
5270 = tree_cons (get_identifier ("omp declare target"),
5271 NULL_TREE, DECL_ATTRIBUTES (decl));
5274 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5277 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5278 EXPR is NULL or a pointer to an expression that needs to be
5279 evaluated for the side effects of array size expressions in the
5280 parameters. */
5282 tree
5283 grokparm (const struct c_parm *parm, tree *expr)
5285 tree attrs = parm->attrs;
5286 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5287 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5289 decl_attributes (&decl, attrs, 0);
5291 return decl;
5294 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5295 and push that on the current scope. EXPR is a pointer to an
5296 expression that needs to be evaluated for the side effects of array
5297 size expressions in the parameters. */
5299 void
5300 push_parm_decl (const struct c_parm *parm, tree *expr)
5302 tree attrs = parm->attrs;
5303 tree decl;
5305 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5306 &attrs, expr, NULL, DEPRECATED_NORMAL);
5307 if (decl && DECL_P (decl))
5308 DECL_SOURCE_LOCATION (decl) = parm->loc;
5309 decl_attributes (&decl, attrs, 0);
5311 decl = pushdecl (decl);
5313 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5316 /* Mark all the parameter declarations to date as forward decls.
5317 Also diagnose use of this extension. */
5319 void
5320 mark_forward_parm_decls (void)
5322 struct c_binding *b;
5324 if (pedantic && !current_scope->warned_forward_parm_decls)
5326 pedwarn (input_location, OPT_Wpedantic,
5327 "ISO C forbids forward parameter declarations");
5328 current_scope->warned_forward_parm_decls = true;
5331 for (b = current_scope->bindings; b; b = b->prev)
5332 if (TREE_CODE (b->decl) == PARM_DECL)
5333 TREE_ASM_WRITTEN (b->decl) = 1;
5336 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5337 literal, which may be an incomplete array type completed by the
5338 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5339 literal. NON_CONST is true if the initializers contain something
5340 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5341 it is the (valid) alignment for this compound literal, as specified
5342 with _Alignas. */
5344 tree
5345 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5346 unsigned int alignas_align)
5348 /* We do not use start_decl here because we have a type, not a declarator;
5349 and do not use finish_decl because the decl should be stored inside
5350 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5351 tree decl;
5352 tree complit;
5353 tree stmt;
5355 if (type == error_mark_node
5356 || init == error_mark_node)
5357 return error_mark_node;
5359 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5360 DECL_EXTERNAL (decl) = 0;
5361 TREE_PUBLIC (decl) = 0;
5362 TREE_STATIC (decl) = (current_scope == file_scope);
5363 DECL_CONTEXT (decl) = current_function_decl;
5364 TREE_USED (decl) = 1;
5365 DECL_READ_P (decl) = 1;
5366 DECL_ARTIFICIAL (decl) = 1;
5367 DECL_IGNORED_P (decl) = 1;
5368 TREE_TYPE (decl) = type;
5369 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5370 if (alignas_align)
5372 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5373 DECL_USER_ALIGN (decl) = 1;
5375 store_init_value (loc, decl, init, NULL_TREE);
5377 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5379 int failure = complete_array_type (&TREE_TYPE (decl),
5380 DECL_INITIAL (decl), true);
5381 /* If complete_array_type returns 3, it means that the
5382 initial value of the compound literal is empty. Allow it. */
5383 gcc_assert (failure == 0 || failure == 3);
5385 type = TREE_TYPE (decl);
5386 TREE_TYPE (DECL_INITIAL (decl)) = type;
5389 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5391 c_incomplete_type_error (loc, NULL_TREE, type);
5392 return error_mark_node;
5395 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5396 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5397 TREE_SIDE_EFFECTS (complit) = 1;
5399 layout_decl (decl, 0);
5401 if (TREE_STATIC (decl))
5403 /* This decl needs a name for the assembler output. */
5404 set_compound_literal_name (decl);
5405 DECL_DEFER_OUTPUT (decl) = 1;
5406 DECL_COMDAT (decl) = 1;
5407 pushdecl (decl);
5408 rest_of_decl_compilation (decl, 1, 0);
5410 else
5411 pushdecl (decl);
5413 if (non_const)
5415 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5416 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5419 return complit;
5422 /* Check the type of a compound literal. Here we just check that it
5423 is valid for C++. */
5425 void
5426 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5428 if (warn_cxx_compat
5429 && (type_name->specs->typespec_kind == ctsk_tagdef
5430 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5431 warning_at (loc, OPT_Wc___compat,
5432 "defining a type in a compound literal is invalid in C++");
5435 /* Determine whether TYPE is a structure with a flexible array member,
5436 or a union containing such a structure (possibly recursively). */
5438 static bool
5439 flexible_array_type_p (tree type)
5441 tree x;
5442 switch (TREE_CODE (type))
5444 case RECORD_TYPE:
5445 x = TYPE_FIELDS (type);
5446 if (x == NULL_TREE)
5447 return false;
5448 while (DECL_CHAIN (x) != NULL_TREE)
5449 x = DECL_CHAIN (x);
5450 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5451 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5452 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5453 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5454 return true;
5455 return false;
5456 case UNION_TYPE:
5457 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5459 if (flexible_array_type_p (TREE_TYPE (x)))
5460 return true;
5462 return false;
5463 default:
5464 return false;
5468 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5469 replacing with appropriate values if they are invalid. */
5471 static void
5472 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5473 tree orig_name)
5475 tree type_mv;
5476 unsigned int max_width;
5477 unsigned HOST_WIDE_INT w;
5478 const char *name = (orig_name
5479 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5480 : _("<anonymous>"));
5482 /* Detect and ignore out of range field width and process valid
5483 field widths. */
5484 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5486 error_at (loc, "bit-field %qs width not an integer constant", name);
5487 *width = integer_one_node;
5489 else
5491 if (TREE_CODE (*width) != INTEGER_CST)
5493 *width = c_fully_fold (*width, false, NULL);
5494 if (TREE_CODE (*width) == INTEGER_CST)
5495 pedwarn (loc, OPT_Wpedantic,
5496 "bit-field %qs width not an integer constant expression",
5497 name);
5499 if (TREE_CODE (*width) != INTEGER_CST)
5501 error_at (loc, "bit-field %qs width not an integer constant", name);
5502 *width = integer_one_node;
5504 constant_expression_warning (*width);
5505 if (tree_int_cst_sgn (*width) < 0)
5507 error_at (loc, "negative width in bit-field %qs", name);
5508 *width = integer_one_node;
5510 else if (integer_zerop (*width) && orig_name)
5512 error_at (loc, "zero width for bit-field %qs", name);
5513 *width = integer_one_node;
5517 /* Detect invalid bit-field type. */
5518 if (TREE_CODE (*type) != INTEGER_TYPE
5519 && TREE_CODE (*type) != BOOLEAN_TYPE
5520 && TREE_CODE (*type) != ENUMERAL_TYPE)
5522 error_at (loc, "bit-field %qs has invalid type", name);
5523 *type = unsigned_type_node;
5526 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5528 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5529 name);
5530 *type = unsigned_type_node;
5533 type_mv = TYPE_MAIN_VARIANT (*type);
5534 if (!in_system_header_at (input_location)
5535 && type_mv != integer_type_node
5536 && type_mv != unsigned_type_node
5537 && type_mv != boolean_type_node)
5538 pedwarn_c90 (loc, OPT_Wpedantic,
5539 "type of bit-field %qs is a GCC extension", name);
5541 max_width = TYPE_PRECISION (*type);
5543 if (compare_tree_int (*width, max_width) > 0)
5545 error_at (loc, "width of %qs exceeds its type", name);
5546 w = max_width;
5547 *width = build_int_cst (integer_type_node, w);
5549 else
5550 w = tree_to_uhwi (*width);
5552 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5554 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5555 if (!lt
5556 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5557 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5558 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5564 /* Print warning about variable length array if necessary. */
5566 static void
5567 warn_variable_length_array (tree name, tree size)
5569 if (TREE_CONSTANT (size))
5571 if (name)
5572 pedwarn_c90 (input_location, OPT_Wvla,
5573 "ISO C90 forbids array %qE whose size "
5574 "can%'t be evaluated", name);
5575 else
5576 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5577 "whose size can%'t be evaluated");
5579 else
5581 if (name)
5582 pedwarn_c90 (input_location, OPT_Wvla,
5583 "ISO C90 forbids variable length array %qE", name);
5584 else
5585 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5586 "length array");
5590 /* Print warning about defaulting to int if necessary. */
5592 static void
5593 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5595 diagnostic_info diagnostic;
5596 va_list ap;
5597 rich_location richloc (line_table, location);
5599 va_start (ap, gmsgid);
5600 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5601 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5602 diagnostic.option_index = opt;
5603 diagnostic_report_diagnostic (global_dc, &diagnostic);
5604 va_end (ap);
5607 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5608 considering only those c_declspec_words found in LIST, which
5609 must be terminated by cdw_number_of_elements. */
5611 static location_t
5612 smallest_type_quals_location (const location_t *locations,
5613 const c_declspec_word *list)
5615 location_t loc = UNKNOWN_LOCATION;
5616 while (*list != cdw_number_of_elements)
5618 location_t newloc = locations[*list];
5619 if (loc == UNKNOWN_LOCATION
5620 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5621 loc = newloc;
5622 list++;
5625 return loc;
5628 /* Given declspecs and a declarator,
5629 determine the name and type of the object declared
5630 and construct a ..._DECL node for it.
5631 (In one case we can return a ..._TYPE node instead.
5632 For invalid input we sometimes return NULL_TREE.)
5634 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5636 DECL_CONTEXT says which syntactic context this declaration is in:
5637 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5638 FUNCDEF for a function definition. Like NORMAL but a few different
5639 error messages in each case. Return value may be zero meaning
5640 this definition is too screwy to try to parse.
5641 PARM for a parameter declaration (either within a function prototype
5642 or before a function body). Make a PARM_DECL, or return void_type_node.
5643 TYPENAME if for a typename (in a cast or sizeof).
5644 Don't make a DECL node; just return the ..._TYPE node.
5645 FIELD for a struct or union field; make a FIELD_DECL.
5646 INITIALIZED is true if the decl has an initializer.
5647 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5648 representing the width of the bit-field.
5649 DECL_ATTRS points to the list of attributes that should be added to this
5650 decl. Any nested attributes that belong on the decl itself will be
5651 added to this list.
5652 If EXPR is not NULL, any expressions that need to be evaluated as
5653 part of evaluating variably modified types will be stored in *EXPR.
5654 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5655 set to indicate whether operands in *EXPR can be used in constant
5656 expressions.
5657 DEPRECATED_STATE is a deprecated_states value indicating whether
5658 deprecation warnings should be suppressed.
5660 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5661 It may also be so in the PARM case, for a prototype where the
5662 argument type is specified but not the name.
5664 This function is where the complicated C meanings of `static'
5665 and `extern' are interpreted. */
5667 static tree
5668 grokdeclarator (const struct c_declarator *declarator,
5669 struct c_declspecs *declspecs,
5670 enum decl_context decl_context, bool initialized, tree *width,
5671 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5672 enum deprecated_states deprecated_state)
5674 tree type = declspecs->type;
5675 bool threadp = declspecs->thread_p;
5676 enum c_storage_class storage_class = declspecs->storage_class;
5677 int constp;
5678 int restrictp;
5679 int volatilep;
5680 int atomicp;
5681 int type_quals = TYPE_UNQUALIFIED;
5682 tree name = NULL_TREE;
5683 bool funcdef_flag = false;
5684 bool funcdef_syntax = false;
5685 bool size_varies = false;
5686 tree decl_attr = declspecs->decl_attr;
5687 int array_ptr_quals = TYPE_UNQUALIFIED;
5688 tree array_ptr_attrs = NULL_TREE;
5689 bool array_parm_static = false;
5690 bool array_parm_vla_unspec_p = false;
5691 tree returned_attrs = NULL_TREE;
5692 bool bitfield = width != NULL;
5693 tree element_type;
5694 tree orig_qual_type = NULL;
5695 size_t orig_qual_indirect = 0;
5696 struct c_arg_info *arg_info = 0;
5697 addr_space_t as1, as2, address_space;
5698 location_t loc = UNKNOWN_LOCATION;
5699 tree expr_dummy;
5700 bool expr_const_operands_dummy;
5701 enum c_declarator_kind first_non_attr_kind;
5702 unsigned int alignas_align = 0;
5704 if (TREE_CODE (type) == ERROR_MARK)
5705 return error_mark_node;
5706 if (expr == NULL)
5708 expr = &expr_dummy;
5709 expr_dummy = NULL_TREE;
5711 if (expr_const_operands == NULL)
5712 expr_const_operands = &expr_const_operands_dummy;
5714 if (declspecs->expr)
5716 if (*expr)
5717 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5718 declspecs->expr);
5719 else
5720 *expr = declspecs->expr;
5722 *expr_const_operands = declspecs->expr_const_operands;
5724 if (decl_context == FUNCDEF)
5725 funcdef_flag = true, decl_context = NORMAL;
5727 /* Look inside a declarator for the name being declared
5728 and get it as an IDENTIFIER_NODE, for an error message. */
5730 const struct c_declarator *decl = declarator;
5732 first_non_attr_kind = cdk_attrs;
5733 while (decl)
5734 switch (decl->kind)
5736 case cdk_array:
5737 loc = decl->id_loc;
5738 /* FALL THRU. */
5740 case cdk_function:
5741 case cdk_pointer:
5742 funcdef_syntax = (decl->kind == cdk_function);
5743 if (first_non_attr_kind == cdk_attrs)
5744 first_non_attr_kind = decl->kind;
5745 decl = decl->declarator;
5746 break;
5748 case cdk_attrs:
5749 decl = decl->declarator;
5750 break;
5752 case cdk_id:
5753 loc = decl->id_loc;
5754 if (decl->u.id)
5755 name = decl->u.id;
5756 if (first_non_attr_kind == cdk_attrs)
5757 first_non_attr_kind = decl->kind;
5758 decl = 0;
5759 break;
5761 default:
5762 gcc_unreachable ();
5764 if (name == NULL_TREE)
5766 gcc_assert (decl_context == PARM
5767 || decl_context == TYPENAME
5768 || (decl_context == FIELD
5769 && declarator->kind == cdk_id));
5770 gcc_assert (!initialized);
5774 /* A function definition's declarator must have the form of
5775 a function declarator. */
5777 if (funcdef_flag && !funcdef_syntax)
5778 return NULL_TREE;
5780 /* If this looks like a function definition, make it one,
5781 even if it occurs where parms are expected.
5782 Then store_parm_decls will reject it and not use it as a parm. */
5783 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5784 decl_context = PARM;
5786 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5787 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5789 if ((decl_context == NORMAL || decl_context == FIELD)
5790 && current_scope == file_scope
5791 && variably_modified_type_p (type, NULL_TREE))
5793 if (name)
5794 error_at (loc, "variably modified %qE at file scope", name);
5795 else
5796 error_at (loc, "variably modified field at file scope");
5797 type = integer_type_node;
5800 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5802 /* Diagnose defaulting to "int". */
5804 if (declspecs->default_int_p && !in_system_header_at (input_location))
5806 /* Issue a warning if this is an ISO C 99 program or if
5807 -Wreturn-type and this is a function, or if -Wimplicit;
5808 prefer the former warning since it is more explicit. */
5809 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
5810 && funcdef_flag)
5811 warn_about_return_type = 1;
5812 else
5814 if (name)
5815 warn_defaults_to (loc, OPT_Wimplicit_int,
5816 "type defaults to %<int%> in declaration "
5817 "of %qE", name);
5818 else
5819 warn_defaults_to (loc, OPT_Wimplicit_int,
5820 "type defaults to %<int%> in type name");
5824 /* Adjust the type if a bit-field is being declared,
5825 -funsigned-bitfields applied and the type is not explicitly
5826 "signed". */
5827 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5828 && TREE_CODE (type) == INTEGER_TYPE)
5829 type = unsigned_type_for (type);
5831 /* Figure out the type qualifiers for the declaration. There are
5832 two ways a declaration can become qualified. One is something
5833 like `const int i' where the `const' is explicit. Another is
5834 something like `typedef const int CI; CI i' where the type of the
5835 declaration contains the `const'. A third possibility is that
5836 there is a type qualifier on the element type of a typedefed
5837 array type, in which case we should extract that qualifier so
5838 that c_apply_type_quals_to_decl receives the full list of
5839 qualifiers to work with (C90 is not entirely clear about whether
5840 duplicate qualifiers should be diagnosed in this case, but it
5841 seems most appropriate to do so). */
5842 element_type = strip_array_types (type);
5843 constp = declspecs->const_p + TYPE_READONLY (element_type);
5844 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5845 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5846 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5847 as1 = declspecs->address_space;
5848 as2 = TYPE_ADDR_SPACE (element_type);
5849 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5851 if (constp > 1)
5852 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5853 if (restrictp > 1)
5854 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5855 if (volatilep > 1)
5856 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5857 if (atomicp > 1)
5858 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5860 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5861 error_at (loc, "conflicting named address spaces (%s vs %s)",
5862 c_addr_space_name (as1), c_addr_space_name (as2));
5864 if ((TREE_CODE (type) == ARRAY_TYPE
5865 || first_non_attr_kind == cdk_array)
5866 && TYPE_QUALS (element_type))
5868 orig_qual_type = type;
5869 type = TYPE_MAIN_VARIANT (type);
5871 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5872 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5873 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5874 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5875 | ENCODE_QUAL_ADDR_SPACE (address_space));
5876 if (type_quals != TYPE_QUALS (element_type))
5877 orig_qual_type = NULL_TREE;
5879 /* Applying the _Atomic qualifier to an array type (through the use
5880 of typedefs or typeof) must be detected here. If the qualifier
5881 is introduced later, any appearance of applying it to an array is
5882 actually applying it to an element of that array. */
5883 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5884 error_at (loc, "%<_Atomic%>-qualified array type");
5886 /* Warn about storage classes that are invalid for certain
5887 kinds of declarations (parameters, typenames, etc.). */
5889 if (funcdef_flag
5890 && (threadp
5891 || storage_class == csc_auto
5892 || storage_class == csc_register
5893 || storage_class == csc_typedef))
5895 if (storage_class == csc_auto)
5896 pedwarn (loc,
5897 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5898 "function definition declared %<auto%>");
5899 if (storage_class == csc_register)
5900 error_at (loc, "function definition declared %<register%>");
5901 if (storage_class == csc_typedef)
5902 error_at (loc, "function definition declared %<typedef%>");
5903 if (threadp)
5904 error_at (loc, "function definition declared %qs",
5905 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5906 threadp = false;
5907 if (storage_class == csc_auto
5908 || storage_class == csc_register
5909 || storage_class == csc_typedef)
5910 storage_class = csc_none;
5912 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5914 if (decl_context == PARM && storage_class == csc_register)
5916 else
5918 switch (decl_context)
5920 case FIELD:
5921 if (name)
5922 error_at (loc, "storage class specified for structure "
5923 "field %qE", name);
5924 else
5925 error_at (loc, "storage class specified for structure field");
5926 break;
5927 case PARM:
5928 if (name)
5929 error_at (loc, "storage class specified for parameter %qE",
5930 name);
5931 else
5932 error_at (loc, "storage class specified for unnamed parameter");
5933 break;
5934 default:
5935 error_at (loc, "storage class specified for typename");
5936 break;
5938 storage_class = csc_none;
5939 threadp = false;
5942 else if (storage_class == csc_extern
5943 && initialized
5944 && !funcdef_flag)
5946 /* 'extern' with initialization is invalid if not at file scope. */
5947 if (current_scope == file_scope)
5949 /* It is fine to have 'extern const' when compiling at C
5950 and C++ intersection. */
5951 if (!(warn_cxx_compat && constp))
5952 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5953 name);
5955 else
5956 error_at (loc, "%qE has both %<extern%> and initializer", name);
5958 else if (current_scope == file_scope)
5960 if (storage_class == csc_auto)
5961 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5962 name);
5963 if (pedantic && storage_class == csc_register)
5964 pedwarn (input_location, OPT_Wpedantic,
5965 "file-scope declaration of %qE specifies %<register%>", name);
5967 else
5969 if (storage_class == csc_extern && funcdef_flag)
5970 error_at (loc, "nested function %qE declared %<extern%>", name);
5971 else if (threadp && storage_class == csc_none)
5973 error_at (loc, "function-scope %qE implicitly auto and declared "
5974 "%qs", name,
5975 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5976 threadp = false;
5980 /* Now figure out the structure of the declarator proper.
5981 Descend through it, creating more complex types, until we reach
5982 the declared identifier (or NULL_TREE, in an absolute declarator).
5983 At each stage we maintain an unqualified version of the type
5984 together with any qualifiers that should be applied to it with
5985 c_build_qualified_type; this way, array types including
5986 multidimensional array types are first built up in unqualified
5987 form and then the qualified form is created with
5988 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5990 while (declarator && declarator->kind != cdk_id)
5992 if (type == error_mark_node)
5994 declarator = declarator->declarator;
5995 continue;
5998 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5999 a cdk_pointer (for *...),
6000 a cdk_function (for ...(...)),
6001 a cdk_attrs (for nested attributes),
6002 or a cdk_id (for the name being declared
6003 or the place in an absolute declarator
6004 where the name was omitted).
6005 For the last case, we have just exited the loop.
6007 At this point, TYPE is the type of elements of an array,
6008 or for a function to return, or for a pointer to point to.
6009 After this sequence of ifs, TYPE is the type of the
6010 array or function or pointer, and DECLARATOR has had its
6011 outermost layer removed. */
6013 if (array_ptr_quals != TYPE_UNQUALIFIED
6014 || array_ptr_attrs != NULL_TREE
6015 || array_parm_static)
6017 /* Only the innermost declarator (making a parameter be of
6018 array type which is converted to pointer type)
6019 may have static or type qualifiers. */
6020 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6021 array_ptr_quals = TYPE_UNQUALIFIED;
6022 array_ptr_attrs = NULL_TREE;
6023 array_parm_static = false;
6026 switch (declarator->kind)
6028 case cdk_attrs:
6030 /* A declarator with embedded attributes. */
6031 tree attrs = declarator->u.attrs;
6032 const struct c_declarator *inner_decl;
6033 int attr_flags = 0;
6034 declarator = declarator->declarator;
6035 inner_decl = declarator;
6036 while (inner_decl->kind == cdk_attrs)
6037 inner_decl = inner_decl->declarator;
6038 if (inner_decl->kind == cdk_id)
6039 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6040 else if (inner_decl->kind == cdk_function)
6041 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6042 else if (inner_decl->kind == cdk_array)
6043 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6044 returned_attrs = decl_attributes (&type,
6045 chainon (returned_attrs, attrs),
6046 attr_flags);
6047 break;
6049 case cdk_array:
6051 tree itype = NULL_TREE;
6052 tree size = declarator->u.array.dimen;
6053 /* The index is a signed object `sizetype' bits wide. */
6054 tree index_type = c_common_signed_type (sizetype);
6056 array_ptr_quals = declarator->u.array.quals;
6057 array_ptr_attrs = declarator->u.array.attrs;
6058 array_parm_static = declarator->u.array.static_p;
6059 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6061 declarator = declarator->declarator;
6063 /* Check for some types that there cannot be arrays of. */
6065 if (VOID_TYPE_P (type))
6067 if (name)
6068 error_at (loc, "declaration of %qE as array of voids", name);
6069 else
6070 error_at (loc, "declaration of type name as array of voids");
6071 type = error_mark_node;
6074 if (TREE_CODE (type) == FUNCTION_TYPE)
6076 if (name)
6077 error_at (loc, "declaration of %qE as array of functions",
6078 name);
6079 else
6080 error_at (loc, "declaration of type name as array of "
6081 "functions");
6082 type = error_mark_node;
6085 if (pedantic && !in_system_header_at (input_location)
6086 && flexible_array_type_p (type))
6087 pedwarn (loc, OPT_Wpedantic,
6088 "invalid use of structure with flexible array member");
6090 if (size == error_mark_node)
6091 type = error_mark_node;
6093 if (type == error_mark_node)
6094 continue;
6096 /* If size was specified, set ITYPE to a range-type for
6097 that size. Otherwise, ITYPE remains null. finish_decl
6098 may figure it out from an initial value. */
6100 if (size)
6102 bool size_maybe_const = true;
6103 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6104 && !TREE_OVERFLOW (size));
6105 bool this_size_varies = false;
6107 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6108 lvalue. */
6109 STRIP_TYPE_NOPS (size);
6111 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6113 if (name)
6114 error_at (loc, "size of array %qE has non-integer type",
6115 name);
6116 else
6117 error_at (loc,
6118 "size of unnamed array has non-integer type");
6119 size = integer_one_node;
6121 /* This can happen with enum forward declaration. */
6122 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6124 if (name)
6125 error_at (loc, "size of array %qE has incomplete type",
6126 name);
6127 else
6128 error_at (loc, "size of unnamed array has incomplete "
6129 "type");
6130 size = integer_one_node;
6133 size = c_fully_fold (size, false, &size_maybe_const);
6135 if (pedantic && size_maybe_const && integer_zerop (size))
6137 if (name)
6138 pedwarn (loc, OPT_Wpedantic,
6139 "ISO C forbids zero-size array %qE", name);
6140 else
6141 pedwarn (loc, OPT_Wpedantic,
6142 "ISO C forbids zero-size array");
6145 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6147 constant_expression_warning (size);
6148 if (tree_int_cst_sgn (size) < 0)
6150 if (name)
6151 error_at (loc, "size of array %qE is negative", name);
6152 else
6153 error_at (loc, "size of unnamed array is negative");
6154 size = integer_one_node;
6156 /* Handle a size folded to an integer constant but
6157 not an integer constant expression. */
6158 if (!size_int_const)
6160 /* If this is a file scope declaration of an
6161 ordinary identifier, this is invalid code;
6162 diagnosing it here and not subsequently
6163 treating the type as variable-length avoids
6164 more confusing diagnostics later. */
6165 if ((decl_context == NORMAL || decl_context == FIELD)
6166 && current_scope == file_scope)
6167 pedwarn (input_location, 0,
6168 "variably modified %qE at file scope",
6169 name);
6170 else
6171 this_size_varies = size_varies = true;
6172 warn_variable_length_array (name, size);
6175 else if ((decl_context == NORMAL || decl_context == FIELD)
6176 && current_scope == file_scope)
6178 error_at (loc, "variably modified %qE at file scope", name);
6179 size = integer_one_node;
6181 else
6183 /* Make sure the array size remains visibly
6184 nonconstant even if it is (eg) a const variable
6185 with known value. */
6186 this_size_varies = size_varies = true;
6187 warn_variable_length_array (name, size);
6188 if (sanitize_flags_p (SANITIZE_VLA)
6189 && current_function_decl != NULL_TREE
6190 && decl_context == NORMAL)
6192 /* Evaluate the array size only once. */
6193 size = save_expr (size);
6194 size = c_fully_fold (size, false, NULL);
6195 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6196 ubsan_instrument_vla (loc, size),
6197 size);
6201 if (integer_zerop (size) && !this_size_varies)
6203 /* A zero-length array cannot be represented with
6204 an unsigned index type, which is what we'll
6205 get with build_index_type. Create an
6206 open-ended range instead. */
6207 itype = build_range_type (sizetype, size, NULL_TREE);
6209 else
6211 /* Arrange for the SAVE_EXPR on the inside of the
6212 MINUS_EXPR, which allows the -1 to get folded
6213 with the +1 that happens when building TYPE_SIZE. */
6214 if (size_varies)
6215 size = save_expr (size);
6216 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6217 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6218 integer_zero_node, size);
6220 /* Compute the maximum valid index, that is, size
6221 - 1. Do the calculation in index_type, so that
6222 if it is a variable the computations will be
6223 done in the proper mode. */
6224 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6225 convert (index_type, size),
6226 convert (index_type,
6227 size_one_node));
6229 /* The above overflows when size does not fit
6230 in index_type.
6231 ??? While a size of INT_MAX+1 technically shouldn't
6232 cause an overflow (because we subtract 1), handling
6233 this case seems like an unnecessary complication. */
6234 if (TREE_CODE (size) == INTEGER_CST
6235 && !int_fits_type_p (size, index_type))
6237 if (name)
6238 error_at (loc, "size of array %qE is too large",
6239 name);
6240 else
6241 error_at (loc, "size of unnamed array is too large");
6242 type = error_mark_node;
6243 continue;
6246 itype = build_index_type (itype);
6248 if (this_size_varies)
6250 if (*expr)
6251 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6252 *expr, size);
6253 else
6254 *expr = size;
6255 *expr_const_operands &= size_maybe_const;
6258 else if (decl_context == FIELD)
6260 bool flexible_array_member = false;
6261 if (array_parm_vla_unspec_p)
6262 /* Field names can in fact have function prototype
6263 scope so [*] is disallowed here through making
6264 the field variably modified, not through being
6265 something other than a declaration with function
6266 prototype scope. */
6267 size_varies = true;
6268 else
6270 const struct c_declarator *t = declarator;
6271 while (t->kind == cdk_attrs)
6272 t = t->declarator;
6273 flexible_array_member = (t->kind == cdk_id);
6275 if (flexible_array_member
6276 && !in_system_header_at (input_location))
6277 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6278 "support flexible array members");
6280 /* ISO C99 Flexible array members are effectively
6281 identical to GCC's zero-length array extension. */
6282 if (flexible_array_member || array_parm_vla_unspec_p)
6283 itype = build_range_type (sizetype, size_zero_node,
6284 NULL_TREE);
6286 else if (decl_context == PARM)
6288 if (array_parm_vla_unspec_p)
6290 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6291 size_varies = true;
6294 else if (decl_context == TYPENAME)
6296 if (array_parm_vla_unspec_p)
6298 /* C99 6.7.5.2p4 */
6299 warning (0, "%<[*]%> not in a declaration");
6300 /* We use this to avoid messing up with incomplete
6301 array types of the same type, that would
6302 otherwise be modified below. */
6303 itype = build_range_type (sizetype, size_zero_node,
6304 NULL_TREE);
6305 size_varies = true;
6309 /* Complain about arrays of incomplete types. */
6310 if (!COMPLETE_TYPE_P (type))
6312 error_at (loc, "array type has incomplete element type %qT",
6313 type);
6314 /* See if we can be more helpful. */
6315 if (TREE_CODE (type) == ARRAY_TYPE)
6317 if (name)
6318 inform (loc, "declaration of %qE as multidimensional "
6319 "array must have bounds for all dimensions "
6320 "except the first", name);
6321 else
6322 inform (loc, "declaration of multidimensional array "
6323 "must have bounds for all dimensions except "
6324 "the first");
6326 type = error_mark_node;
6328 else
6329 /* When itype is NULL, a shared incomplete array type is
6330 returned for all array of a given type. Elsewhere we
6331 make sure we don't complete that type before copying
6332 it, but here we want to make sure we don't ever
6333 modify the shared type, so we gcc_assert (itype)
6334 below. */
6336 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6337 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6338 type = build_qualified_type (type,
6339 ENCODE_QUAL_ADDR_SPACE (as));
6341 type = build_array_type (type, itype);
6344 if (type != error_mark_node)
6346 if (size_varies)
6348 /* It is ok to modify type here even if itype is
6349 NULL: if size_varies, we're in a
6350 multi-dimensional array and the inner type has
6351 variable size, so the enclosing shared array type
6352 must too. */
6353 if (size && TREE_CODE (size) == INTEGER_CST)
6354 type
6355 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6356 C_TYPE_VARIABLE_SIZE (type) = 1;
6359 /* The GCC extension for zero-length arrays differs from
6360 ISO flexible array members in that sizeof yields
6361 zero. */
6362 if (size && integer_zerop (size))
6364 gcc_assert (itype);
6365 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6366 TYPE_SIZE (type) = bitsize_zero_node;
6367 TYPE_SIZE_UNIT (type) = size_zero_node;
6368 SET_TYPE_STRUCTURAL_EQUALITY (type);
6370 if (array_parm_vla_unspec_p)
6372 gcc_assert (itype);
6373 /* The type is complete. C99 6.7.5.2p4 */
6374 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6375 TYPE_SIZE (type) = bitsize_zero_node;
6376 TYPE_SIZE_UNIT (type) = size_zero_node;
6377 SET_TYPE_STRUCTURAL_EQUALITY (type);
6380 if (!valid_array_size_p (loc, type, name))
6381 type = error_mark_node;
6384 if (decl_context != PARM
6385 && (array_ptr_quals != TYPE_UNQUALIFIED
6386 || array_ptr_attrs != NULL_TREE
6387 || array_parm_static))
6389 error_at (loc, "static or type qualifiers in non-parameter "
6390 "array declarator");
6391 array_ptr_quals = TYPE_UNQUALIFIED;
6392 array_ptr_attrs = NULL_TREE;
6393 array_parm_static = false;
6395 orig_qual_indirect++;
6396 break;
6398 case cdk_function:
6400 /* Say it's a definition only for the declarator closest
6401 to the identifier, apart possibly from some
6402 attributes. */
6403 bool really_funcdef = false;
6404 tree arg_types;
6405 orig_qual_type = NULL_TREE;
6406 if (funcdef_flag)
6408 const struct c_declarator *t = declarator->declarator;
6409 while (t->kind == cdk_attrs)
6410 t = t->declarator;
6411 really_funcdef = (t->kind == cdk_id);
6414 /* Declaring a function type. Make sure we have a valid
6415 type for the function to return. */
6416 if (type == error_mark_node)
6417 continue;
6419 size_varies = false;
6421 /* Warn about some types functions can't return. */
6422 if (TREE_CODE (type) == FUNCTION_TYPE)
6424 if (name)
6425 error_at (loc, "%qE declared as function returning a "
6426 "function", name);
6427 else
6428 error_at (loc, "type name declared as function "
6429 "returning a function");
6430 type = integer_type_node;
6432 if (TREE_CODE (type) == ARRAY_TYPE)
6434 if (name)
6435 error_at (loc, "%qE declared as function returning an array",
6436 name);
6437 else
6438 error_at (loc, "type name declared as function returning "
6439 "an array");
6440 type = integer_type_node;
6443 /* Construct the function type and go to the next
6444 inner layer of declarator. */
6445 arg_info = declarator->u.arg_info;
6446 arg_types = grokparms (arg_info, really_funcdef);
6448 /* Type qualifiers before the return type of the function
6449 qualify the return type, not the function type. */
6450 if (type_quals)
6452 const enum c_declspec_word ignored_quals_list[] =
6454 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6455 cdw_atomic, cdw_number_of_elements
6457 location_t specs_loc
6458 = smallest_type_quals_location (declspecs->locations,
6459 ignored_quals_list);
6460 if (specs_loc == UNKNOWN_LOCATION)
6461 specs_loc = declspecs->locations[cdw_typedef];
6462 if (specs_loc == UNKNOWN_LOCATION)
6463 specs_loc = loc;
6465 /* Type qualifiers on a function return type are
6466 normally permitted by the standard but have no
6467 effect, so give a warning at -Wreturn-type.
6468 Qualifiers on a void return type are banned on
6469 function definitions in ISO C; GCC used to used
6470 them for noreturn functions. The resolution of C11
6471 DR#423 means qualifiers (other than _Atomic) are
6472 actually removed from the return type when
6473 determining the function type. */
6474 int quals_used = type_quals;
6475 if (flag_isoc11)
6476 quals_used &= TYPE_QUAL_ATOMIC;
6477 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6478 pedwarn (specs_loc, 0,
6479 "function definition has qualified void return type");
6480 else
6481 warning_at (specs_loc, OPT_Wignored_qualifiers,
6482 "type qualifiers ignored on function return type");
6484 /* Ensure an error for restrict on invalid types; the
6485 DR#423 resolution is not entirely clear about
6486 this. */
6487 if (flag_isoc11
6488 && (type_quals & TYPE_QUAL_RESTRICT)
6489 && (!POINTER_TYPE_P (type)
6490 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6491 error_at (loc, "invalid use of %<restrict%>");
6492 if (quals_used)
6493 type = c_build_qualified_type (type, quals_used);
6495 type_quals = TYPE_UNQUALIFIED;
6497 type = build_function_type (type, arg_types);
6498 declarator = declarator->declarator;
6500 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6501 the formal parameter list of this FUNCTION_TYPE to point to
6502 the FUNCTION_TYPE node itself. */
6504 c_arg_tag *tag;
6505 unsigned ix;
6507 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6508 TYPE_CONTEXT (tag->type) = type;
6510 break;
6512 case cdk_pointer:
6514 /* Merge any constancy or volatility into the target type
6515 for the pointer. */
6516 if ((type_quals & TYPE_QUAL_ATOMIC)
6517 && TREE_CODE (type) == FUNCTION_TYPE)
6519 error_at (loc,
6520 "%<_Atomic%>-qualified function type");
6521 type_quals &= ~TYPE_QUAL_ATOMIC;
6523 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6524 && type_quals)
6525 pedwarn (loc, OPT_Wpedantic,
6526 "ISO C forbids qualified function types");
6527 if (type_quals)
6528 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6529 orig_qual_indirect);
6530 orig_qual_type = NULL_TREE;
6531 size_varies = false;
6533 /* When the pointed-to type involves components of variable size,
6534 care must be taken to ensure that the size evaluation code is
6535 emitted early enough to dominate all the possible later uses
6536 and late enough for the variables on which it depends to have
6537 been assigned.
6539 This is expected to happen automatically when the pointed-to
6540 type has a name/declaration of it's own, but special attention
6541 is required if the type is anonymous.
6543 We attach an artificial TYPE_DECL to such pointed-to type
6544 and arrange for it to be included in a DECL_EXPR. This
6545 forces the sizes evaluation at a safe point and ensures it
6546 is not deferred until e.g. within a deeper conditional context.
6548 PARM contexts have no enclosing statement list that
6549 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6550 instead, and add it to the list of expressions that
6551 need to be evaluated.
6553 TYPENAME contexts do have an enclosing statement list,
6554 but it would be incorrect to use it, as the size should
6555 only be evaluated if the containing expression is
6556 evaluated. We might also be in the middle of an
6557 expression with side effects on the pointed-to type size
6558 "arguments" prior to the pointer declaration point and
6559 the fake TYPE_DECL in the enclosing context would force
6560 the size evaluation prior to the side effects. We therefore
6561 use BIND_EXPRs in TYPENAME contexts too. */
6562 if (!TYPE_NAME (type)
6563 && variably_modified_type_p (type, NULL_TREE))
6565 tree bind = NULL_TREE;
6566 if (decl_context == TYPENAME || decl_context == PARM)
6568 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6569 NULL_TREE, NULL_TREE);
6570 TREE_SIDE_EFFECTS (bind) = 1;
6571 BIND_EXPR_BODY (bind) = push_stmt_list ();
6572 push_scope ();
6574 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6575 DECL_ARTIFICIAL (decl) = 1;
6576 pushdecl (decl);
6577 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6578 TYPE_NAME (type) = decl;
6579 if (bind)
6581 pop_scope ();
6582 BIND_EXPR_BODY (bind)
6583 = pop_stmt_list (BIND_EXPR_BODY (bind));
6584 if (*expr)
6585 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6586 bind);
6587 else
6588 *expr = bind;
6592 type = c_build_pointer_type (type);
6594 /* Process type qualifiers (such as const or volatile)
6595 that were given inside the `*'. */
6596 type_quals = declarator->u.pointer_quals;
6598 declarator = declarator->declarator;
6599 break;
6601 default:
6602 gcc_unreachable ();
6605 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6607 /* Now TYPE has the actual type, apart from any qualifiers in
6608 TYPE_QUALS. */
6610 /* Warn about address space used for things other than static memory or
6611 pointers. */
6612 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6613 if (!ADDR_SPACE_GENERIC_P (address_space))
6615 if (decl_context == NORMAL)
6617 switch (storage_class)
6619 case csc_auto:
6620 error ("%qs combined with %<auto%> qualifier for %qE",
6621 c_addr_space_name (address_space), name);
6622 break;
6623 case csc_register:
6624 error ("%qs combined with %<register%> qualifier for %qE",
6625 c_addr_space_name (address_space), name);
6626 break;
6627 case csc_none:
6628 if (current_function_scope)
6630 error ("%qs specified for auto variable %qE",
6631 c_addr_space_name (address_space), name);
6632 break;
6634 break;
6635 case csc_static:
6636 case csc_extern:
6637 case csc_typedef:
6638 break;
6639 default:
6640 gcc_unreachable ();
6643 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6645 if (name)
6646 error ("%qs specified for parameter %qE",
6647 c_addr_space_name (address_space), name);
6648 else
6649 error ("%qs specified for unnamed parameter",
6650 c_addr_space_name (address_space));
6652 else if (decl_context == FIELD)
6654 if (name)
6655 error ("%qs specified for structure field %qE",
6656 c_addr_space_name (address_space), name);
6657 else
6658 error ("%qs specified for structure field",
6659 c_addr_space_name (address_space));
6663 /* Check the type and width of a bit-field. */
6664 if (bitfield)
6666 check_bitfield_type_and_width (loc, &type, width, name);
6667 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6668 atomic types are permitted for bit-fields; we have no code to
6669 make bit-field accesses atomic, so disallow them. */
6670 if (type_quals & TYPE_QUAL_ATOMIC)
6672 if (name)
6673 error_at (loc, "bit-field %qE has atomic type", name);
6674 else
6675 error_at (loc, "bit-field has atomic type");
6676 type_quals &= ~TYPE_QUAL_ATOMIC;
6680 /* Reject invalid uses of _Alignas. */
6681 if (declspecs->alignas_p)
6683 if (storage_class == csc_typedef)
6684 error_at (loc, "alignment specified for typedef %qE", name);
6685 else if (storage_class == csc_register)
6686 error_at (loc, "alignment specified for %<register%> object %qE",
6687 name);
6688 else if (decl_context == PARM)
6690 if (name)
6691 error_at (loc, "alignment specified for parameter %qE", name);
6692 else
6693 error_at (loc, "alignment specified for unnamed parameter");
6695 else if (bitfield)
6697 if (name)
6698 error_at (loc, "alignment specified for bit-field %qE", name);
6699 else
6700 error_at (loc, "alignment specified for unnamed bit-field");
6702 else if (TREE_CODE (type) == FUNCTION_TYPE)
6703 error_at (loc, "alignment specified for function %qE", name);
6704 else if (declspecs->align_log != -1 && TYPE_P (type))
6706 alignas_align = 1U << declspecs->align_log;
6707 if (alignas_align < min_align_of_type (type))
6709 if (name)
6710 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6711 "alignment of %qE", name);
6712 else
6713 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6714 "alignment of unnamed field");
6715 alignas_align = 0;
6720 /* If this is declaring a typedef name, return a TYPE_DECL. */
6722 if (storage_class == csc_typedef)
6724 tree decl;
6725 if ((type_quals & TYPE_QUAL_ATOMIC)
6726 && TREE_CODE (type) == FUNCTION_TYPE)
6728 error_at (loc,
6729 "%<_Atomic%>-qualified function type");
6730 type_quals &= ~TYPE_QUAL_ATOMIC;
6732 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6733 && type_quals)
6734 pedwarn (loc, OPT_Wpedantic,
6735 "ISO C forbids qualified function types");
6736 if (type_quals)
6737 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6738 orig_qual_indirect);
6739 decl = build_decl (declarator->id_loc,
6740 TYPE_DECL, declarator->u.id, type);
6741 if (declspecs->explicit_signed_p)
6742 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6743 if (declspecs->inline_p)
6744 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6745 if (declspecs->noreturn_p)
6746 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6748 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6750 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6752 if (b != NULL
6753 && b->decl != NULL_TREE
6754 && (B_IN_CURRENT_SCOPE (b)
6755 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6756 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6758 auto_diagnostic_group d;
6759 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6760 ("using %qD as both a typedef and a tag is "
6761 "invalid in C++"), decl)
6762 && b->locus != UNKNOWN_LOCATION)
6763 inform (b->locus, "originally defined here");
6767 return decl;
6770 /* If this is a type name (such as, in a cast or sizeof),
6771 compute the type and return it now. */
6773 if (decl_context == TYPENAME)
6775 /* Note that the grammar rejects storage classes in typenames
6776 and fields. */
6777 gcc_assert (storage_class == csc_none && !threadp
6778 && !declspecs->inline_p && !declspecs->noreturn_p);
6779 if ((type_quals & TYPE_QUAL_ATOMIC)
6780 && TREE_CODE (type) == FUNCTION_TYPE)
6782 error_at (loc,
6783 "%<_Atomic%>-qualified function type");
6784 type_quals &= ~TYPE_QUAL_ATOMIC;
6786 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6787 && type_quals)
6788 pedwarn (loc, OPT_Wpedantic,
6789 "ISO C forbids const or volatile function types");
6790 if (type_quals)
6791 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6792 orig_qual_indirect);
6793 return type;
6796 if (pedantic && decl_context == FIELD
6797 && variably_modified_type_p (type, NULL_TREE))
6799 /* C99 6.7.2.1p8 */
6800 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6801 "have a variably modified type");
6804 /* Aside from typedefs and type names (handle above),
6805 `void' at top level (not within pointer)
6806 is allowed only in public variables.
6807 We don't complain about parms either, but that is because
6808 a better error message can be made later. */
6810 if (VOID_TYPE_P (type) && decl_context != PARM
6811 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6812 && (storage_class == csc_extern
6813 || (current_scope == file_scope
6814 && !(storage_class == csc_static
6815 || storage_class == csc_register)))))
6817 error_at (loc, "variable or field %qE declared void", name);
6818 type = integer_type_node;
6821 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6822 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6825 tree decl;
6827 if (decl_context == PARM)
6829 tree promoted_type;
6830 bool array_parameter_p = false;
6832 /* A parameter declared as an array of T is really a pointer to T.
6833 One declared as a function is really a pointer to a function. */
6835 if (TREE_CODE (type) == ARRAY_TYPE)
6837 /* Transfer const-ness of array into that of type pointed to. */
6838 type = TREE_TYPE (type);
6839 if (orig_qual_type != NULL_TREE)
6841 if (orig_qual_indirect == 0)
6842 orig_qual_type = TREE_TYPE (orig_qual_type);
6843 else
6844 orig_qual_indirect--;
6846 if (type_quals)
6847 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6848 orig_qual_indirect);
6849 type = c_build_pointer_type (type);
6850 type_quals = array_ptr_quals;
6851 if (type_quals)
6852 type = c_build_qualified_type (type, type_quals);
6854 /* We don't yet implement attributes in this context. */
6855 if (array_ptr_attrs != NULL_TREE)
6856 warning_at (loc, OPT_Wattributes,
6857 "attributes in parameter array declarator ignored");
6859 size_varies = false;
6860 array_parameter_p = true;
6862 else if (TREE_CODE (type) == FUNCTION_TYPE)
6864 if (type_quals & TYPE_QUAL_ATOMIC)
6866 error_at (loc,
6867 "%<_Atomic%>-qualified function type");
6868 type_quals &= ~TYPE_QUAL_ATOMIC;
6870 else if (type_quals)
6871 pedwarn (loc, OPT_Wpedantic,
6872 "ISO C forbids qualified function types");
6873 if (type_quals)
6874 type = c_build_qualified_type (type, type_quals);
6875 type = c_build_pointer_type (type);
6876 type_quals = TYPE_UNQUALIFIED;
6878 else if (type_quals)
6879 type = c_build_qualified_type (type, type_quals);
6881 decl = build_decl (declarator->id_loc,
6882 PARM_DECL, declarator->u.id, type);
6883 if (size_varies)
6884 C_DECL_VARIABLE_SIZE (decl) = 1;
6885 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6887 /* Compute the type actually passed in the parmlist,
6888 for the case where there is no prototype.
6889 (For example, shorts and chars are passed as ints.)
6890 When there is a prototype, this is overridden later. */
6892 if (type == error_mark_node)
6893 promoted_type = type;
6894 else
6895 promoted_type = c_type_promotes_to (type);
6897 DECL_ARG_TYPE (decl) = promoted_type;
6898 if (declspecs->inline_p)
6899 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6900 if (declspecs->noreturn_p)
6901 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6903 else if (decl_context == FIELD)
6905 /* Note that the grammar rejects storage classes in typenames
6906 and fields. */
6907 gcc_assert (storage_class == csc_none && !threadp
6908 && !declspecs->inline_p && !declspecs->noreturn_p);
6910 /* Structure field. It may not be a function. */
6912 if (TREE_CODE (type) == FUNCTION_TYPE)
6914 error_at (loc, "field %qE declared as a function", name);
6915 type = build_pointer_type (type);
6917 else if (TREE_CODE (type) != ERROR_MARK
6918 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6920 if (name)
6921 error_at (loc, "field %qE has incomplete type", name);
6922 else
6923 error_at (loc, "unnamed field has incomplete type");
6924 type = error_mark_node;
6926 else if (TREE_CODE (type) == ARRAY_TYPE
6927 && TYPE_DOMAIN (type) == NULL_TREE)
6929 /* We have a flexible array member through a typedef.
6930 Set suitable range. Whether this is a correct position
6931 for a flexible array member will be determined elsewhere. */
6932 if (!in_system_header_at (input_location))
6933 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6934 "support flexible array members");
6935 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6936 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6937 NULL_TREE);
6938 if (orig_qual_indirect == 0)
6939 orig_qual_type = NULL_TREE;
6941 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6942 orig_qual_indirect);
6943 decl = build_decl (declarator->id_loc,
6944 FIELD_DECL, declarator->u.id, type);
6945 DECL_NONADDRESSABLE_P (decl) = bitfield;
6946 if (bitfield && !declarator->u.id)
6948 TREE_NO_WARNING (decl) = 1;
6949 DECL_PADDING_P (decl) = 1;
6952 if (size_varies)
6953 C_DECL_VARIABLE_SIZE (decl) = 1;
6955 else if (TREE_CODE (type) == FUNCTION_TYPE)
6957 if (storage_class == csc_register || threadp)
6959 error_at (loc, "invalid storage class for function %qE", name);
6961 else if (current_scope != file_scope)
6963 /* Function declaration not at file scope. Storage
6964 classes other than `extern' are not allowed, C99
6965 6.7.1p5, and `extern' makes no difference. However,
6966 GCC allows 'auto', perhaps with 'inline', to support
6967 nested functions. */
6968 if (storage_class == csc_auto)
6969 pedwarn (loc, OPT_Wpedantic,
6970 "invalid storage class for function %qE", name);
6971 else if (storage_class == csc_static)
6973 error_at (loc, "invalid storage class for function %qE", name);
6974 if (funcdef_flag)
6975 storage_class = declspecs->storage_class = csc_none;
6976 else
6977 return NULL_TREE;
6981 decl = build_decl (declarator->id_loc,
6982 FUNCTION_DECL, declarator->u.id, type);
6983 decl = build_decl_attribute_variant (decl, decl_attr);
6985 if (type_quals & TYPE_QUAL_ATOMIC)
6987 error_at (loc,
6988 "%<_Atomic%>-qualified function type");
6989 type_quals &= ~TYPE_QUAL_ATOMIC;
6991 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6992 pedwarn (loc, OPT_Wpedantic,
6993 "ISO C forbids qualified function types");
6995 /* Every function declaration is an external reference
6996 (DECL_EXTERNAL) except for those which are not at file
6997 scope and are explicitly declared "auto". This is
6998 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6999 GCC to signify a forward declaration of a nested function. */
7000 if (storage_class == csc_auto && current_scope != file_scope)
7001 DECL_EXTERNAL (decl) = 0;
7002 /* In C99, a function which is declared 'inline' with 'extern'
7003 is not an external reference (which is confusing). It
7004 means that the later definition of the function must be output
7005 in this file, C99 6.7.4p6. In GNU C89, a function declared
7006 'extern inline' is an external reference. */
7007 else if (declspecs->inline_p && storage_class != csc_static)
7008 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7009 == flag_gnu89_inline);
7010 else
7011 DECL_EXTERNAL (decl) = !initialized;
7013 /* Record absence of global scope for `static' or `auto'. */
7014 TREE_PUBLIC (decl)
7015 = !(storage_class == csc_static || storage_class == csc_auto);
7017 /* For a function definition, record the argument information
7018 block where store_parm_decls will look for it. */
7019 if (funcdef_flag)
7020 current_function_arg_info = arg_info;
7022 if (declspecs->default_int_p)
7023 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7025 /* Record presence of `inline' and `_Noreturn', if it is
7026 reasonable. */
7027 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
7029 if (declspecs->inline_p)
7030 pedwarn (loc, 0, "cannot inline function %<main%>");
7031 if (declspecs->noreturn_p)
7032 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7034 else
7036 if (declspecs->inline_p)
7037 /* Record that the function is declared `inline'. */
7038 DECL_DECLARED_INLINE_P (decl) = 1;
7039 if (declspecs->noreturn_p)
7041 if (flag_isoc99)
7042 pedwarn_c99 (loc, OPT_Wpedantic,
7043 "ISO C99 does not support %<_Noreturn%>");
7044 else
7045 pedwarn_c99 (loc, OPT_Wpedantic,
7046 "ISO C90 does not support %<_Noreturn%>");
7047 TREE_THIS_VOLATILE (decl) = 1;
7051 else
7053 /* It's a variable. */
7054 /* An uninitialized decl with `extern' is a reference. */
7055 int extern_ref = !initialized && storage_class == csc_extern;
7057 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7058 orig_qual_indirect);
7060 /* C99 6.2.2p7: It is invalid (compile-time undefined
7061 behavior) to create an 'extern' declaration for a
7062 variable if there is a global declaration that is
7063 'static' and the global declaration is not visible.
7064 (If the static declaration _is_ currently visible,
7065 the 'extern' declaration is taken to refer to that decl.) */
7066 if (extern_ref && current_scope != file_scope)
7068 tree global_decl = identifier_global_value (declarator->u.id);
7069 tree visible_decl = lookup_name (declarator->u.id);
7071 if (global_decl
7072 && global_decl != visible_decl
7073 && VAR_P (global_decl)
7074 && !TREE_PUBLIC (global_decl))
7075 error_at (loc, "variable previously declared %<static%> "
7076 "redeclared %<extern%>");
7079 decl = build_decl (declarator->id_loc,
7080 VAR_DECL, declarator->u.id, type);
7081 if (size_varies)
7082 C_DECL_VARIABLE_SIZE (decl) = 1;
7084 if (declspecs->inline_p)
7085 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7086 if (declspecs->noreturn_p)
7087 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7089 /* At file scope, an initialized extern declaration may follow
7090 a static declaration. In that case, DECL_EXTERNAL will be
7091 reset later in start_decl. */
7092 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7094 /* At file scope, the presence of a `static' or `register' storage
7095 class specifier, or the absence of all storage class specifiers
7096 makes this declaration a definition (perhaps tentative). Also,
7097 the absence of `static' makes it public. */
7098 if (current_scope == file_scope)
7100 TREE_PUBLIC (decl) = storage_class != csc_static;
7101 TREE_STATIC (decl) = !extern_ref;
7103 /* Not at file scope, only `static' makes a static definition. */
7104 else
7106 TREE_STATIC (decl) = (storage_class == csc_static);
7107 TREE_PUBLIC (decl) = extern_ref;
7110 if (threadp)
7111 set_decl_tls_model (decl, decl_default_tls_model (decl));
7114 if ((storage_class == csc_extern
7115 || (storage_class == csc_none
7116 && TREE_CODE (type) == FUNCTION_TYPE
7117 && !funcdef_flag))
7118 && variably_modified_type_p (type, NULL_TREE))
7120 /* C99 6.7.5.2p2 */
7121 if (TREE_CODE (type) == FUNCTION_TYPE)
7122 error_at (loc, "non-nested function with variably modified type");
7123 else
7124 error_at (loc, "object with variably modified type must have "
7125 "no linkage");
7128 /* Record `register' declaration for warnings on &
7129 and in case doing stupid register allocation. */
7131 if (storage_class == csc_register)
7133 C_DECL_REGISTER (decl) = 1;
7134 DECL_REGISTER (decl) = 1;
7137 /* Record constancy and volatility. */
7138 c_apply_type_quals_to_decl (type_quals, decl);
7140 /* Apply _Alignas specifiers. */
7141 if (alignas_align)
7143 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7144 DECL_USER_ALIGN (decl) = 1;
7147 /* If a type has volatile components, it should be stored in memory.
7148 Otherwise, the fact that those components are volatile
7149 will be ignored, and would even crash the compiler.
7150 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7151 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7152 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7153 || TREE_CODE (decl) == RESULT_DECL))
7155 /* It is not an error for a structure with volatile fields to
7156 be declared register, but reset DECL_REGISTER since it
7157 cannot actually go in a register. */
7158 int was_reg = C_DECL_REGISTER (decl);
7159 C_DECL_REGISTER (decl) = 0;
7160 DECL_REGISTER (decl) = 0;
7161 c_mark_addressable (decl);
7162 C_DECL_REGISTER (decl) = was_reg;
7165 /* This is the earliest point at which we might know the assembler
7166 name of a variable. Thus, if it's known before this, die horribly. */
7167 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7168 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7170 if (warn_cxx_compat
7171 && VAR_P (decl)
7172 && TREE_PUBLIC (decl)
7173 && TREE_STATIC (decl)
7174 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7175 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7176 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7177 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7178 ("non-local variable %qD with anonymous type is "
7179 "questionable in C++"),
7180 decl);
7182 return decl;
7186 /* Decode the parameter-list info for a function type or function definition.
7187 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7188 if there is an identifier list instead of a parameter decl list).
7189 These two functions are separate because when a function returns
7190 or receives functions then each is called multiple times but the order
7191 of calls is different. The last call to `grokparms' is always the one
7192 that contains the formal parameter names of a function definition.
7194 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7196 FUNCDEF_FLAG is true for a function definition, false for
7197 a mere declaration. A nonempty identifier-list gets an error message
7198 when FUNCDEF_FLAG is false. */
7200 static tree
7201 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7203 tree arg_types = arg_info->types;
7205 if (funcdef_flag && arg_info->had_vla_unspec)
7207 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7208 /* C99 6.7.5.2p4 */
7209 error ("%<[*]%> not allowed in other than function prototype scope");
7212 if (arg_types == NULL_TREE && !funcdef_flag
7213 && !in_system_header_at (input_location))
7214 warning (OPT_Wstrict_prototypes,
7215 "function declaration isn%'t a prototype");
7217 if (arg_types == error_mark_node)
7218 /* Don't set TYPE_ARG_TYPES in this case. */
7219 return NULL_TREE;
7221 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7223 if (!funcdef_flag)
7225 pedwarn (input_location, 0, "parameter names (without types) in "
7226 "function declaration");
7227 arg_info->parms = NULL_TREE;
7229 else
7230 arg_info->parms = arg_info->types;
7232 arg_info->types = NULL_TREE;
7233 return NULL_TREE;
7235 else
7237 tree parm, type, typelt;
7238 unsigned int parmno;
7240 /* If there is a parameter of incomplete type in a definition,
7241 this is an error. In a declaration this is valid, and a
7242 struct or union type may be completed later, before any calls
7243 or definition of the function. In the case where the tag was
7244 first declared within the parameter list, a warning has
7245 already been given. If a parameter has void type, then
7246 however the function cannot be defined or called, so
7247 warn. */
7249 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7250 parm;
7251 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7253 type = TREE_VALUE (typelt);
7254 if (type == error_mark_node)
7255 continue;
7257 if (!COMPLETE_TYPE_P (type))
7259 if (funcdef_flag)
7261 if (DECL_NAME (parm))
7262 error_at (input_location,
7263 "parameter %u (%q+D) has incomplete type",
7264 parmno, parm);
7265 else
7266 error_at (DECL_SOURCE_LOCATION (parm),
7267 "parameter %u has incomplete type",
7268 parmno);
7270 TREE_VALUE (typelt) = error_mark_node;
7271 TREE_TYPE (parm) = error_mark_node;
7272 arg_types = NULL_TREE;
7274 else if (VOID_TYPE_P (type))
7276 if (DECL_NAME (parm))
7277 warning_at (input_location, 0,
7278 "parameter %u (%q+D) has void type",
7279 parmno, parm);
7280 else
7281 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7282 "parameter %u has void type",
7283 parmno);
7287 if (DECL_NAME (parm) && TREE_USED (parm))
7288 warn_if_shadowing (parm);
7290 return arg_types;
7294 /* Allocate and initialize a c_arg_info structure from the parser's
7295 obstack. */
7297 struct c_arg_info *
7298 build_arg_info (void)
7300 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7301 ret->parms = NULL_TREE;
7302 ret->tags = NULL;
7303 ret->types = NULL_TREE;
7304 ret->others = NULL_TREE;
7305 ret->pending_sizes = NULL;
7306 ret->had_vla_unspec = 0;
7307 return ret;
7310 /* Take apart the current scope and return a c_arg_info structure with
7311 info on a parameter list just parsed.
7313 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7315 ELLIPSIS being true means the argument list ended in '...' so don't
7316 append a sentinel (void_list_node) to the end of the type-list.
7318 EXPR is NULL or an expression that needs to be evaluated for the
7319 side effects of array size expressions in the parameters. */
7321 struct c_arg_info *
7322 get_parm_info (bool ellipsis, tree expr)
7324 struct c_binding *b = current_scope->bindings;
7325 struct c_arg_info *arg_info = build_arg_info ();
7327 tree parms = NULL_TREE;
7328 vec<c_arg_tag, va_gc> *tags = NULL;
7329 tree types = NULL_TREE;
7330 tree others = NULL_TREE;
7332 bool gave_void_only_once_err = false;
7334 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7336 /* The bindings in this scope must not get put into a block.
7337 We will take care of deleting the binding nodes. */
7338 current_scope->bindings = 0;
7340 /* This function is only called if there was *something* on the
7341 parameter list. */
7342 gcc_assert (b);
7344 /* A parameter list consisting solely of 'void' indicates that the
7345 function takes no arguments. But if the 'void' is qualified
7346 (by 'const' or 'volatile'), or has a storage class specifier
7347 ('register'), then the behavior is undefined; issue an error.
7348 Typedefs for 'void' are OK (see DR#157). */
7349 if (b->prev == 0 /* one binding */
7350 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7351 && !DECL_NAME (b->decl) /* anonymous */
7352 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7354 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7355 || C_DECL_REGISTER (b->decl))
7356 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7358 /* There cannot be an ellipsis. */
7359 if (ellipsis)
7360 error_at (b->locus, "%<void%> must be the only parameter");
7362 arg_info->types = void_list_node;
7363 return arg_info;
7366 if (!ellipsis)
7367 types = void_list_node;
7369 /* Break up the bindings list into parms, tags, types, and others;
7370 apply sanity checks; purge the name-to-decl bindings. */
7371 while (b)
7373 tree decl = b->decl;
7374 tree type = TREE_TYPE (decl);
7375 c_arg_tag tag;
7376 const char *keyword;
7378 switch (TREE_CODE (decl))
7380 case PARM_DECL:
7381 if (b->id)
7383 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7384 I_SYMBOL_BINDING (b->id) = b->shadowed;
7387 /* Check for forward decls that never got their actual decl. */
7388 if (TREE_ASM_WRITTEN (decl))
7389 error_at (b->locus,
7390 "parameter %q+D has just a forward declaration", decl);
7391 /* Check for (..., void, ...) and issue an error. */
7392 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7394 if (!gave_void_only_once_err)
7396 error_at (b->locus, "%<void%> must be the only parameter");
7397 gave_void_only_once_err = true;
7400 else
7402 /* Valid parameter, add it to the list. */
7403 DECL_CHAIN (decl) = parms;
7404 parms = decl;
7406 /* Since there is a prototype, args are passed in their
7407 declared types. The back end may override this later. */
7408 DECL_ARG_TYPE (decl) = type;
7409 types = tree_cons (0, type, types);
7411 break;
7413 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7414 case UNION_TYPE: keyword = "union"; goto tag;
7415 case RECORD_TYPE: keyword = "struct"; goto tag;
7416 tag:
7417 /* Types may not have tag-names, in which case the type
7418 appears in the bindings list with b->id NULL. */
7419 if (b->id)
7421 gcc_assert (I_TAG_BINDING (b->id) == b);
7422 I_TAG_BINDING (b->id) = b->shadowed;
7425 /* Warn about any struct, union or enum tags defined in a
7426 parameter list. The scope of such types is limited to
7427 the parameter list, which is rarely if ever desirable
7428 (it's impossible to call such a function with type-
7429 correct arguments). An anonymous union parm type is
7430 meaningful as a GNU extension, so don't warn for that. */
7431 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7433 if (b->id)
7434 /* The %s will be one of 'struct', 'union', or 'enum'. */
7435 warning_at (b->locus, 0,
7436 "%<%s %E%> declared inside parameter list"
7437 " will not be visible outside of this definition or"
7438 " declaration", keyword, b->id);
7439 else
7440 /* The %s will be one of 'struct', 'union', or 'enum'. */
7441 warning_at (b->locus, 0,
7442 "anonymous %s declared inside parameter list"
7443 " will not be visible outside of this definition or"
7444 " declaration", keyword);
7447 tag.id = b->id;
7448 tag.type = decl;
7449 vec_safe_push (tags, tag);
7450 break;
7452 case FUNCTION_DECL:
7453 /* FUNCTION_DECLs appear when there is an implicit function
7454 declaration in the parameter list. */
7455 gcc_assert (b->nested || seen_error ());
7456 goto set_shadowed;
7458 case CONST_DECL:
7459 case TYPE_DECL:
7460 /* CONST_DECLs appear here when we have an embedded enum,
7461 and TYPE_DECLs appear here when we have an embedded struct
7462 or union. No warnings for this - we already warned about the
7463 type itself. */
7465 /* When we reinsert this decl in the function body, we need
7466 to reconstruct whether it was marked as nested. */
7467 gcc_assert (!b->nested);
7468 DECL_CHAIN (decl) = others;
7469 others = decl;
7470 /* fall through */
7472 case ERROR_MARK:
7473 set_shadowed:
7474 /* error_mark_node appears here when we have an undeclared
7475 variable. Just throw it away. */
7476 if (b->id)
7478 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7479 I_SYMBOL_BINDING (b->id) = b->shadowed;
7481 break;
7483 /* Other things that might be encountered. */
7484 case LABEL_DECL:
7485 case VAR_DECL:
7486 default:
7487 gcc_unreachable ();
7490 b = free_binding_and_advance (b);
7493 arg_info->parms = parms;
7494 arg_info->tags = tags;
7495 arg_info->types = types;
7496 arg_info->others = others;
7497 arg_info->pending_sizes = expr;
7498 return arg_info;
7501 /* Get the struct, enum or union (CODE says which) with tag NAME.
7502 Define the tag as a forward-reference with location LOC if it is
7503 not defined. Return a c_typespec structure for the type
7504 specifier. */
7506 struct c_typespec
7507 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7509 struct c_typespec ret;
7510 tree ref;
7511 location_t refloc;
7513 ret.expr = NULL_TREE;
7514 ret.expr_const_operands = true;
7516 /* If a cross reference is requested, look up the type
7517 already defined for this tag and return it. */
7519 ref = lookup_tag (code, name, false, &refloc);
7520 /* If this is the right type of tag, return what we found.
7521 (This reference will be shadowed by shadow_tag later if appropriate.)
7522 If this is the wrong type of tag, do not return it. If it was the
7523 wrong type in the same scope, we will have had an error
7524 message already; if in a different scope and declaring
7525 a name, pending_xref_error will give an error message; but if in a
7526 different scope and not declaring a name, this tag should
7527 shadow the previous declaration of a different type of tag, and
7528 this would not work properly if we return the reference found.
7529 (For example, with "struct foo" in an outer scope, "union foo;"
7530 must shadow that tag with a new one of union type.) */
7531 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7532 if (ref && TREE_CODE (ref) == code)
7534 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7535 && loc != UNKNOWN_LOCATION
7536 && warn_cxx_compat)
7538 switch (code)
7540 case ENUMERAL_TYPE:
7541 warning_at (loc, OPT_Wc___compat,
7542 ("enum type defined in struct or union "
7543 "is not visible in C++"));
7544 inform (refloc, "enum type defined here");
7545 break;
7546 case RECORD_TYPE:
7547 warning_at (loc, OPT_Wc___compat,
7548 ("struct defined in struct or union "
7549 "is not visible in C++"));
7550 inform (refloc, "struct defined here");
7551 break;
7552 case UNION_TYPE:
7553 warning_at (loc, OPT_Wc___compat,
7554 ("union defined in struct or union "
7555 "is not visible in C++"));
7556 inform (refloc, "union defined here");
7557 break;
7558 default:
7559 gcc_unreachable();
7563 ret.spec = ref;
7564 return ret;
7567 /* If no such tag is yet defined, create a forward-reference node
7568 and record it as the "definition".
7569 When a real declaration of this type is found,
7570 the forward-reference will be altered into a real type. */
7572 ref = make_node (code);
7573 if (code == ENUMERAL_TYPE)
7575 /* Give the type a default layout like unsigned int
7576 to avoid crashing if it does not get defined. */
7577 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7578 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7579 TYPE_USER_ALIGN (ref) = 0;
7580 TYPE_UNSIGNED (ref) = 1;
7581 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7582 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7583 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7586 pushtag (loc, name, ref);
7588 ret.spec = ref;
7589 return ret;
7592 /* Get the struct, enum or union (CODE says which) with tag NAME.
7593 Define the tag as a forward-reference if it is not defined.
7594 Return a tree for the type. */
7596 tree
7597 xref_tag (enum tree_code code, tree name)
7599 return parser_xref_tag (input_location, code, name).spec;
7602 /* Make sure that the tag NAME is defined *in the current scope*
7603 at least as a forward reference.
7604 LOC is the location of the struct's definition.
7605 CODE says which kind of tag NAME ought to be.
7607 This stores the current value of the file static STRUCT_PARSE_INFO
7608 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7609 new c_struct_parse_info structure. The old value of
7610 STRUCT_PARSE_INFO is restored in finish_struct. */
7612 tree
7613 start_struct (location_t loc, enum tree_code code, tree name,
7614 struct c_struct_parse_info **enclosing_struct_parse_info)
7616 /* If there is already a tag defined at this scope
7617 (as a forward reference), just return it. */
7619 tree ref = NULL_TREE;
7620 location_t refloc = UNKNOWN_LOCATION;
7622 if (name != NULL_TREE)
7623 ref = lookup_tag (code, name, true, &refloc);
7624 if (ref && TREE_CODE (ref) == code)
7626 if (TYPE_STUB_DECL (ref))
7627 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7629 if (TYPE_SIZE (ref))
7631 if (code == UNION_TYPE)
7632 error_at (loc, "redefinition of %<union %E%>", name);
7633 else
7634 error_at (loc, "redefinition of %<struct %E%>", name);
7635 if (refloc != UNKNOWN_LOCATION)
7636 inform (refloc, "originally defined here");
7637 /* Don't create structures using a name already in use. */
7638 ref = NULL_TREE;
7640 else if (C_TYPE_BEING_DEFINED (ref))
7642 if (code == UNION_TYPE)
7643 error_at (loc, "nested redefinition of %<union %E%>", name);
7644 else
7645 error_at (loc, "nested redefinition of %<struct %E%>", name);
7646 /* Don't bother to report "originally defined here" for a
7647 nested redefinition; the original definition should be
7648 obvious. */
7649 /* Don't create structures that contain themselves. */
7650 ref = NULL_TREE;
7654 /* Otherwise create a forward-reference just so the tag is in scope. */
7656 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7658 ref = make_node (code);
7659 pushtag (loc, name, ref);
7662 C_TYPE_BEING_DEFINED (ref) = 1;
7663 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7664 TYPE_PACKED (v) = flag_pack_struct;
7666 *enclosing_struct_parse_info = struct_parse_info;
7667 struct_parse_info = new c_struct_parse_info ();
7669 /* FIXME: This will issue a warning for a use of a type defined
7670 within a statement expr used within sizeof, et. al. This is not
7671 terribly serious as C++ doesn't permit statement exprs within
7672 sizeof anyhow. */
7673 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7674 warning_at (loc, OPT_Wc___compat,
7675 "defining type in %qs expression is invalid in C++",
7676 (in_sizeof
7677 ? "sizeof"
7678 : (in_typeof ? "typeof" : "alignof")));
7680 return ref;
7683 /* Process the specs, declarator and width (NULL if omitted)
7684 of a structure component, returning a FIELD_DECL node.
7685 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7686 DECL_ATTRS is as for grokdeclarator.
7688 LOC is the location of the structure component.
7690 This is done during the parsing of the struct declaration.
7691 The FIELD_DECL nodes are chained together and the lot of them
7692 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7694 tree
7695 grokfield (location_t loc,
7696 struct c_declarator *declarator, struct c_declspecs *declspecs,
7697 tree width, tree *decl_attrs)
7699 tree value;
7701 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7702 && width == NULL_TREE)
7704 /* This is an unnamed decl.
7706 If we have something of the form "union { list } ;" then this
7707 is the anonymous union extension. Similarly for struct.
7709 If this is something of the form "struct foo;", then
7710 If MS or Plan 9 extensions are enabled, this is handled as
7711 an anonymous struct.
7712 Otherwise this is a forward declaration of a structure tag.
7714 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7715 If foo names a structure or union without a tag, then this
7716 is an anonymous struct (this is permitted by C11).
7717 If MS or Plan 9 extensions are enabled and foo names a
7718 structure, then again this is an anonymous struct.
7719 Otherwise this is an error.
7721 Oh what a horrid tangled web we weave. I wonder if MS consciously
7722 took this from Plan 9 or if it was an accident of implementation
7723 that took root before someone noticed the bug... */
7725 tree type = declspecs->type;
7726 bool ok = false;
7728 if (RECORD_OR_UNION_TYPE_P (type)
7729 && (flag_ms_extensions
7730 || flag_plan9_extensions
7731 || !declspecs->typedef_p))
7733 if (flag_ms_extensions || flag_plan9_extensions)
7734 ok = true;
7735 else if (TYPE_NAME (type) == NULL)
7736 ok = true;
7737 else
7738 ok = false;
7740 if (!ok)
7742 pedwarn (loc, 0, "declaration does not declare anything");
7743 return NULL_TREE;
7745 if (flag_isoc99)
7746 pedwarn_c99 (loc, OPT_Wpedantic,
7747 "ISO C99 doesn%'t support unnamed structs/unions");
7748 else
7749 pedwarn_c99 (loc, OPT_Wpedantic,
7750 "ISO C90 doesn%'t support unnamed structs/unions");
7753 value = grokdeclarator (declarator, declspecs, FIELD, false,
7754 width ? &width : NULL, decl_attrs, NULL, NULL,
7755 DEPRECATED_NORMAL);
7757 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7758 DECL_INITIAL (value) = width;
7759 if (width)
7760 SET_DECL_C_BIT_FIELD (value);
7762 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7764 /* If we currently have a binding for this field, set the
7765 in_struct field in the binding, so that we warn about lookups
7766 which find it. */
7767 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7768 if (b != NULL)
7770 /* If the in_struct field is not yet set, push it on a list
7771 to be cleared when this struct is finished. */
7772 if (!b->in_struct)
7774 struct_parse_info->fields.safe_push (b);
7775 b->in_struct = 1;
7780 return value;
7783 /* Subroutine of detect_field_duplicates: return whether X and Y,
7784 which are both fields in the same struct, have duplicate field
7785 names. */
7787 static bool
7788 is_duplicate_field (tree x, tree y)
7790 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7791 return true;
7793 /* When using -fplan9-extensions, an anonymous field whose name is a
7794 typedef can duplicate a field name. */
7795 if (flag_plan9_extensions
7796 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7798 tree xt, xn, yt, yn;
7800 xt = TREE_TYPE (x);
7801 if (DECL_NAME (x) != NULL_TREE)
7802 xn = DECL_NAME (x);
7803 else if (RECORD_OR_UNION_TYPE_P (xt)
7804 && TYPE_NAME (xt) != NULL_TREE
7805 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7806 xn = DECL_NAME (TYPE_NAME (xt));
7807 else
7808 xn = NULL_TREE;
7810 yt = TREE_TYPE (y);
7811 if (DECL_NAME (y) != NULL_TREE)
7812 yn = DECL_NAME (y);
7813 else if (RECORD_OR_UNION_TYPE_P (yt)
7814 && TYPE_NAME (yt) != NULL_TREE
7815 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7816 yn = DECL_NAME (TYPE_NAME (yt));
7817 else
7818 yn = NULL_TREE;
7820 if (xn != NULL_TREE && xn == yn)
7821 return true;
7824 return false;
7827 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7828 to HTAB, giving errors for any duplicates. */
7830 static void
7831 detect_field_duplicates_hash (tree fieldlist,
7832 hash_table<nofree_ptr_hash <tree_node> > *htab)
7834 tree x, y;
7835 tree_node **slot;
7837 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7838 if ((y = DECL_NAME (x)) != NULL_TREE)
7840 slot = htab->find_slot (y, INSERT);
7841 if (*slot)
7843 error ("duplicate member %q+D", x);
7844 DECL_NAME (x) = NULL_TREE;
7846 *slot = y;
7848 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7850 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7852 /* When using -fplan9-extensions, an anonymous field whose
7853 name is a typedef can duplicate a field name. */
7854 if (flag_plan9_extensions
7855 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7856 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7858 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7859 slot = htab->find_slot (xn, INSERT);
7860 if (*slot)
7861 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7862 *slot = xn;
7867 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7868 the list such that this does not present a problem later. */
7870 static void
7871 detect_field_duplicates (tree fieldlist)
7873 tree x, y;
7874 int timeout = 10;
7876 /* If the struct is the list of instance variables of an Objective-C
7877 class, then we need to check all the instance variables of
7878 superclasses when checking for duplicates (since you can't have
7879 an instance variable in a subclass with the same name as an
7880 instance variable in a superclass). We pass on this job to the
7881 Objective-C compiler. objc_detect_field_duplicates() will return
7882 false if we are not checking the list of instance variables and
7883 the C frontend should proceed with the standard field duplicate
7884 checks. If we are checking the list of instance variables, the
7885 ObjC frontend will do the check, emit the errors if needed, and
7886 then return true. */
7887 if (c_dialect_objc ())
7888 if (objc_detect_field_duplicates (false))
7889 return;
7891 /* First, see if there are more than "a few" fields.
7892 This is trivially true if there are zero or one fields. */
7893 if (!fieldlist || !DECL_CHAIN (fieldlist))
7894 return;
7895 x = fieldlist;
7896 do {
7897 timeout--;
7898 if (DECL_NAME (x) == NULL_TREE
7899 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7900 timeout = 0;
7901 x = DECL_CHAIN (x);
7902 } while (timeout > 0 && x);
7904 /* If there were "few" fields and no anonymous structures or unions,
7905 avoid the overhead of allocating a hash table. Instead just do
7906 the nested traversal thing. */
7907 if (timeout > 0)
7909 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7910 /* When using -fplan9-extensions, we can have duplicates
7911 between typedef names and fields. */
7912 if (DECL_NAME (x)
7913 || (flag_plan9_extensions
7914 && DECL_NAME (x) == NULL_TREE
7915 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7916 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7917 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7919 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7920 if (is_duplicate_field (y, x))
7922 error ("duplicate member %q+D", x);
7923 DECL_NAME (x) = NULL_TREE;
7927 else
7929 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7930 detect_field_duplicates_hash (fieldlist, &htab);
7934 /* Finish up struct info used by -Wc++-compat. */
7936 static void
7937 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7938 location_t record_loc)
7940 unsigned int ix;
7941 tree x;
7942 struct c_binding *b;
7944 if (fieldlist == NULL_TREE)
7946 if (code == RECORD_TYPE)
7947 warning_at (record_loc, OPT_Wc___compat,
7948 "empty struct has size 0 in C, size 1 in C++");
7949 else
7950 warning_at (record_loc, OPT_Wc___compat,
7951 "empty union has size 0 in C, size 1 in C++");
7954 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7955 the current struct. We do this now at the end of the struct
7956 because the flag is used to issue visibility warnings, and we
7957 only want to issue those warnings if the type is referenced
7958 outside of the struct declaration. */
7959 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7960 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7962 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7963 typedefs used when declaring fields in this struct. If the name
7964 of any of the fields is also a typedef name then the struct would
7965 not parse in C++, because the C++ lookup rules say that the
7966 typedef name would be looked up in the context of the struct, and
7967 would thus be the field rather than the typedef. */
7968 if (!struct_parse_info->typedefs_seen.is_empty ()
7969 && fieldlist != NULL_TREE)
7971 /* Use a hash_set<tree> using the name of the typedef. We can use
7972 a hash_set<tree> because identifiers are interned. */
7973 hash_set<tree> tset;
7975 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7976 tset.add (DECL_NAME (x));
7978 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7980 if (DECL_NAME (x) != NULL_TREE
7981 && tset.contains (DECL_NAME (x)))
7983 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7984 ("using %qD as both field and typedef name is "
7985 "invalid in C++"),
7987 /* FIXME: It would be nice to report the location where
7988 the typedef name is used. */
7993 /* For each field which has a binding and which was not defined in
7994 an enclosing struct, clear the in_struct field. */
7995 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7996 b->in_struct = 0;
7999 /* Function to help qsort sort FIELD_DECLs by name order. */
8001 static int
8002 field_decl_cmp (const void *x_p, const void *y_p)
8004 const tree *const x = (const tree *) x_p;
8005 const tree *const y = (const tree *) y_p;
8007 if (DECL_NAME (*x) == DECL_NAME (*y))
8008 /* A nontype is "greater" than a type. */
8009 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8010 if (DECL_NAME (*x) == NULL_TREE)
8011 return -1;
8012 if (DECL_NAME (*y) == NULL_TREE)
8013 return 1;
8014 if (DECL_NAME (*x) < DECL_NAME (*y))
8015 return -1;
8016 return 1;
8019 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8020 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8021 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8022 ATTRIBUTES are attributes to be applied to the structure.
8024 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8025 the struct was started. */
8027 tree
8028 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8029 struct c_struct_parse_info *enclosing_struct_parse_info)
8031 tree x;
8032 bool toplevel = file_scope == current_scope;
8034 /* If this type was previously laid out as a forward reference,
8035 make sure we lay it out again. */
8037 TYPE_SIZE (t) = NULL_TREE;
8039 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8041 if (pedantic)
8043 for (x = fieldlist; x; x = DECL_CHAIN (x))
8045 if (DECL_NAME (x) != NULL_TREE)
8046 break;
8047 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8048 break;
8051 if (x == NULL_TREE)
8053 if (TREE_CODE (t) == UNION_TYPE)
8055 if (fieldlist)
8056 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8057 else
8058 pedwarn (loc, OPT_Wpedantic, "union has no members");
8060 else
8062 if (fieldlist)
8063 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8064 else
8065 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8070 /* Install struct as DECL_CONTEXT of each field decl.
8071 Also process specified field sizes, found in the DECL_INITIAL,
8072 storing 0 there after the type has been changed to precision equal
8073 to its width, rather than the precision of the specified standard
8074 type. (Correct layout requires the original type to have been preserved
8075 until now.) */
8077 bool saw_named_field = false;
8078 for (x = fieldlist; x; x = DECL_CHAIN (x))
8080 if (TREE_TYPE (x) == error_mark_node)
8081 continue;
8083 DECL_CONTEXT (x) = t;
8085 /* If any field is const, the structure type is pseudo-const. */
8086 if (TREE_READONLY (x))
8087 C_TYPE_FIELDS_READONLY (t) = 1;
8088 else
8090 /* A field that is pseudo-const makes the structure likewise. */
8091 tree t1 = strip_array_types (TREE_TYPE (x));
8092 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8093 C_TYPE_FIELDS_READONLY (t) = 1;
8096 /* Any field that is volatile means variables of this type must be
8097 treated in some ways as volatile. */
8098 if (TREE_THIS_VOLATILE (x))
8099 C_TYPE_FIELDS_VOLATILE (t) = 1;
8101 /* Any field of nominal variable size implies structure is too. */
8102 if (C_DECL_VARIABLE_SIZE (x))
8103 C_TYPE_VARIABLE_SIZE (t) = 1;
8105 if (DECL_C_BIT_FIELD (x))
8107 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8108 DECL_SIZE (x) = bitsize_int (width);
8109 DECL_BIT_FIELD (x) = 1;
8112 if (TYPE_PACKED (t)
8113 && (DECL_BIT_FIELD (x)
8114 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8115 DECL_PACKED (x) = 1;
8117 /* Detect flexible array member in an invalid context. */
8118 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8119 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8120 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8121 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8123 if (TREE_CODE (t) == UNION_TYPE)
8125 error_at (DECL_SOURCE_LOCATION (x),
8126 "flexible array member in union");
8127 TREE_TYPE (x) = error_mark_node;
8129 else if (DECL_CHAIN (x) != NULL_TREE)
8131 error_at (DECL_SOURCE_LOCATION (x),
8132 "flexible array member not at end of struct");
8133 TREE_TYPE (x) = error_mark_node;
8135 else if (!saw_named_field)
8137 error_at (DECL_SOURCE_LOCATION (x),
8138 "flexible array member in a struct with no named "
8139 "members");
8140 TREE_TYPE (x) = error_mark_node;
8144 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8145 && flexible_array_type_p (TREE_TYPE (x)))
8146 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8147 "invalid use of structure with flexible array member");
8149 if (DECL_NAME (x)
8150 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8151 saw_named_field = true;
8154 detect_field_duplicates (fieldlist);
8156 /* Now we have the nearly final fieldlist. Record it,
8157 then lay out the structure or union (including the fields). */
8159 TYPE_FIELDS (t) = fieldlist;
8161 maybe_apply_pragma_scalar_storage_order (t);
8163 layout_type (t);
8165 if (TYPE_SIZE_UNIT (t)
8166 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8167 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8168 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8169 error ("type %qT is too large", t);
8171 /* Give bit-fields their proper types and rewrite the type of array fields
8172 with scalar component if the enclosing type has reverse storage order. */
8173 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8175 if (TREE_CODE (field) == FIELD_DECL
8176 && DECL_INITIAL (field)
8177 && TREE_TYPE (field) != error_mark_node)
8179 unsigned HOST_WIDE_INT width
8180 = tree_to_uhwi (DECL_INITIAL (field));
8181 tree type = TREE_TYPE (field);
8182 if (width != TYPE_PRECISION (type))
8184 TREE_TYPE (field)
8185 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8186 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8188 DECL_INITIAL (field) = NULL_TREE;
8190 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8191 && TREE_CODE (field) == FIELD_DECL
8192 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8194 tree ftype = TREE_TYPE (field);
8195 tree ctype = strip_array_types (ftype);
8196 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8198 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8199 tree *typep = &fmain_type;
8200 do {
8201 *typep = build_distinct_type_copy (*typep);
8202 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8203 typep = &TREE_TYPE (*typep);
8204 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8205 TREE_TYPE (field)
8206 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8211 /* Now we have the truly final field list.
8212 Store it in this type and in the variants. */
8214 TYPE_FIELDS (t) = fieldlist;
8216 /* If there are lots of fields, sort so we can look through them fast.
8217 We arbitrarily consider 16 or more elts to be "a lot". */
8220 int len = 0;
8222 for (x = fieldlist; x; x = DECL_CHAIN (x))
8224 if (len > 15 || DECL_NAME (x) == NULL)
8225 break;
8226 len += 1;
8229 if (len > 15)
8231 tree *field_array;
8232 struct lang_type *space;
8233 struct sorted_fields_type *space2;
8235 len += list_length (x);
8237 /* Use the same allocation policy here that make_node uses, to
8238 ensure that this lives as long as the rest of the struct decl.
8239 All decls in an inline function need to be saved. */
8241 space = ggc_cleared_alloc<struct lang_type> ();
8242 space2 = (sorted_fields_type *) ggc_internal_alloc
8243 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8245 len = 0;
8246 space->s = space2;
8247 field_array = &space2->elts[0];
8248 for (x = fieldlist; x; x = DECL_CHAIN (x))
8250 field_array[len++] = x;
8252 /* If there is anonymous struct or union, break out of the loop. */
8253 if (DECL_NAME (x) == NULL)
8254 break;
8256 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8257 if (x == NULL)
8259 TYPE_LANG_SPECIFIC (t) = space;
8260 TYPE_LANG_SPECIFIC (t)->s->len = len;
8261 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8262 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8267 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8268 in dwarf2out via rest_of_decl_compilation below and means
8269 something totally different. Since we will be clearing
8270 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8271 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8272 C_TYPE_INCOMPLETE_VARS should use some language specific
8273 node. */
8274 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8275 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8277 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8278 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8279 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8280 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8281 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8282 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8285 /* If this was supposed to be a transparent union, but we can't
8286 make it one, warn and turn off the flag. */
8287 if (TREE_CODE (t) == UNION_TYPE
8288 && TYPE_TRANSPARENT_AGGR (t)
8289 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8291 TYPE_TRANSPARENT_AGGR (t) = 0;
8292 warning_at (loc, 0, "union cannot be made transparent");
8295 /* Update type location to the one of the definition, instead of e.g.
8296 a forward declaration. */
8297 if (TYPE_STUB_DECL (t))
8298 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8300 /* Finish debugging output for this type. */
8301 rest_of_type_compilation (t, toplevel);
8303 /* If this structure or union completes the type of any previous
8304 variable declaration, lay it out and output its rtl. */
8305 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8307 tree decl = TREE_VALUE (x);
8308 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8309 layout_array_type (TREE_TYPE (decl));
8310 if (TREE_CODE (decl) != TYPE_DECL)
8312 layout_decl (decl, 0);
8313 if (c_dialect_objc ())
8314 objc_check_decl (decl);
8315 rest_of_decl_compilation (decl, toplevel, 0);
8319 /* If we're inside a function proper, i.e. not file-scope and not still
8320 parsing parameters, then arrange for the size of a variable sized type
8321 to be bound now. */
8322 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8323 add_stmt (build_stmt (loc,
8324 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8326 if (warn_cxx_compat)
8327 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8329 delete struct_parse_info;
8331 struct_parse_info = enclosing_struct_parse_info;
8333 /* If this struct is defined inside a struct, add it to
8334 struct_types. */
8335 if (warn_cxx_compat
8336 && struct_parse_info != NULL
8337 && !in_sizeof && !in_typeof && !in_alignof)
8338 struct_parse_info->struct_types.safe_push (t);
8340 return t;
8343 static struct {
8344 gt_pointer_operator new_value;
8345 void *cookie;
8346 } resort_data;
8348 /* This routine compares two fields like field_decl_cmp but using the
8349 pointer operator in resort_data. */
8351 static int
8352 resort_field_decl_cmp (const void *x_p, const void *y_p)
8354 const tree *const x = (const tree *) x_p;
8355 const tree *const y = (const tree *) y_p;
8357 if (DECL_NAME (*x) == DECL_NAME (*y))
8358 /* A nontype is "greater" than a type. */
8359 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8360 if (DECL_NAME (*x) == NULL_TREE)
8361 return -1;
8362 if (DECL_NAME (*y) == NULL_TREE)
8363 return 1;
8365 tree d1 = DECL_NAME (*x);
8366 tree d2 = DECL_NAME (*y);
8367 resort_data.new_value (&d1, resort_data.cookie);
8368 resort_data.new_value (&d2, resort_data.cookie);
8369 if (d1 < d2)
8370 return -1;
8372 return 1;
8375 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8377 void
8378 resort_sorted_fields (void *obj,
8379 void * ARG_UNUSED (orig_obj),
8380 gt_pointer_operator new_value,
8381 void *cookie)
8383 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8384 resort_data.new_value = new_value;
8385 resort_data.cookie = cookie;
8386 qsort (&sf->elts[0], sf->len, sizeof (tree),
8387 resort_field_decl_cmp);
8390 /* Lay out the type T, and its element type, and so on. */
8392 static void
8393 layout_array_type (tree t)
8395 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8396 layout_array_type (TREE_TYPE (t));
8397 layout_type (t);
8400 /* Begin compiling the definition of an enumeration type.
8401 NAME is its name (or null if anonymous).
8402 LOC is the enum's location.
8403 Returns the type object, as yet incomplete.
8404 Also records info about it so that build_enumerator
8405 may be used to declare the individual values as they are read. */
8407 tree
8408 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8410 tree enumtype = NULL_TREE;
8411 location_t enumloc = UNKNOWN_LOCATION;
8413 /* If this is the real definition for a previous forward reference,
8414 fill in the contents in the same object that used to be the
8415 forward reference. */
8417 if (name != NULL_TREE)
8418 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8420 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8422 enumtype = make_node (ENUMERAL_TYPE);
8423 pushtag (loc, name, enumtype);
8425 /* Update type location to the one of the definition, instead of e.g.
8426 a forward declaration. */
8427 else if (TYPE_STUB_DECL (enumtype))
8429 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8430 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8433 if (C_TYPE_BEING_DEFINED (enumtype))
8434 error_at (loc, "nested redefinition of %<enum %E%>", name);
8436 C_TYPE_BEING_DEFINED (enumtype) = 1;
8438 if (TYPE_VALUES (enumtype) != NULL_TREE)
8440 /* This enum is a named one that has been declared already. */
8441 error_at (loc, "redeclaration of %<enum %E%>", name);
8442 if (enumloc != UNKNOWN_LOCATION)
8443 inform (enumloc, "originally defined here");
8445 /* Completely replace its old definition.
8446 The old enumerators remain defined, however. */
8447 TYPE_VALUES (enumtype) = NULL_TREE;
8450 the_enum->enum_next_value = integer_zero_node;
8451 the_enum->enum_overflow = 0;
8453 if (flag_short_enums)
8454 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8455 TYPE_PACKED (v) = 1;
8457 /* FIXME: This will issue a warning for a use of a type defined
8458 within sizeof in a statement expr. This is not terribly serious
8459 as C++ doesn't permit statement exprs within sizeof anyhow. */
8460 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8461 warning_at (loc, OPT_Wc___compat,
8462 "defining type in %qs expression is invalid in C++",
8463 (in_sizeof
8464 ? "sizeof"
8465 : (in_typeof ? "typeof" : "alignof")));
8467 return enumtype;
8470 /* After processing and defining all the values of an enumeration type,
8471 install their decls in the enumeration type and finish it off.
8472 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8473 and ATTRIBUTES are the specified attributes.
8474 Returns ENUMTYPE. */
8476 tree
8477 finish_enum (tree enumtype, tree values, tree attributes)
8479 tree pair, tem;
8480 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8481 int precision;
8482 signop sign;
8483 bool toplevel = (file_scope == current_scope);
8484 struct lang_type *lt;
8486 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8488 /* Calculate the maximum value of any enumerator in this type. */
8490 if (values == error_mark_node)
8491 minnode = maxnode = integer_zero_node;
8492 else
8494 minnode = maxnode = TREE_VALUE (values);
8495 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8497 tree value = TREE_VALUE (pair);
8498 if (tree_int_cst_lt (maxnode, value))
8499 maxnode = value;
8500 if (tree_int_cst_lt (value, minnode))
8501 minnode = value;
8505 /* Construct the final type of this enumeration. It is the same
8506 as one of the integral types - the narrowest one that fits, except
8507 that normally we only go as narrow as int - and signed iff any of
8508 the values are negative. */
8509 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8510 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8511 tree_int_cst_min_precision (maxnode, sign));
8513 /* If the precision of the type was specified with an attribute and it
8514 was too small, give an error. Otherwise, use it. */
8515 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8517 if (precision > TYPE_PRECISION (enumtype))
8519 TYPE_PRECISION (enumtype) = 0;
8520 error ("specified mode too small for enumeral values");
8522 else
8523 precision = TYPE_PRECISION (enumtype);
8525 else
8526 TYPE_PRECISION (enumtype) = 0;
8528 if (TYPE_PACKED (enumtype)
8529 || precision > TYPE_PRECISION (integer_type_node)
8530 || TYPE_PRECISION (enumtype))
8532 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8533 if (tem == NULL)
8535 warning (0, "enumeration values exceed range of largest integer");
8536 tem = long_long_integer_type_node;
8539 else
8540 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8542 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8543 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8544 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8545 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8546 TYPE_SIZE (enumtype) = NULL_TREE;
8547 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8549 layout_type (enumtype);
8551 if (values != error_mark_node)
8553 /* Change the type of the enumerators to be the enum type. We
8554 need to do this irrespective of the size of the enum, for
8555 proper type checking. Replace the DECL_INITIALs of the
8556 enumerators, and the value slots of the list, with copies
8557 that have the enum type; they cannot be modified in place
8558 because they may be shared (e.g. integer_zero_node) Finally,
8559 change the purpose slots to point to the names of the decls. */
8560 for (pair = values; pair; pair = TREE_CHAIN (pair))
8562 tree enu = TREE_PURPOSE (pair);
8563 tree ini = DECL_INITIAL (enu);
8565 TREE_TYPE (enu) = enumtype;
8567 /* The ISO C Standard mandates enumerators to have type int,
8568 even though the underlying type of an enum type is
8569 unspecified. However, GCC allows enumerators of any
8570 integer type as an extensions. build_enumerator()
8571 converts any enumerators that fit in an int to type int,
8572 to avoid promotions to unsigned types when comparing
8573 integers with enumerators that fit in the int range.
8574 When -pedantic is given, build_enumerator() would have
8575 already warned about those that don't fit. Here we
8576 convert the rest to the enumerator type. */
8577 if (TREE_TYPE (ini) != integer_type_node)
8578 ini = convert (enumtype, ini);
8580 DECL_INITIAL (enu) = ini;
8581 TREE_PURPOSE (pair) = DECL_NAME (enu);
8582 TREE_VALUE (pair) = ini;
8585 TYPE_VALUES (enumtype) = values;
8588 /* Record the min/max values so that we can warn about bit-field
8589 enumerations that are too small for the values. */
8590 lt = ggc_cleared_alloc<struct lang_type> ();
8591 lt->enum_min = minnode;
8592 lt->enum_max = maxnode;
8593 TYPE_LANG_SPECIFIC (enumtype) = lt;
8595 /* Fix up all variant types of this enum type. */
8596 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8598 if (tem == enumtype)
8599 continue;
8600 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8601 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8602 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8603 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8604 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8605 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8606 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8607 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8608 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8609 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8610 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8613 /* Finish debugging output for this type. */
8614 rest_of_type_compilation (enumtype, toplevel);
8616 /* If this enum is defined inside a struct, add it to
8617 struct_types. */
8618 if (warn_cxx_compat
8619 && struct_parse_info != NULL
8620 && !in_sizeof && !in_typeof && !in_alignof)
8621 struct_parse_info->struct_types.safe_push (enumtype);
8623 return enumtype;
8626 /* Build and install a CONST_DECL for one value of the
8627 current enumeration type (one that was begun with start_enum).
8628 DECL_LOC is the location of the enumerator.
8629 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8630 Return a tree-list containing the CONST_DECL and its value.
8631 Assignment of sequential values by default is handled here. */
8633 tree
8634 build_enumerator (location_t decl_loc, location_t loc,
8635 struct c_enum_contents *the_enum, tree name, tree value)
8637 tree decl, type;
8639 /* Validate and default VALUE. */
8641 if (value != NULL_TREE)
8643 /* Don't issue more errors for error_mark_node (i.e. an
8644 undeclared identifier) - just ignore the value expression. */
8645 if (value == error_mark_node)
8646 value = NULL_TREE;
8647 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8649 error_at (loc, "enumerator value for %qE is not an integer constant",
8650 name);
8651 value = NULL_TREE;
8653 else
8655 if (TREE_CODE (value) != INTEGER_CST)
8657 value = c_fully_fold (value, false, NULL);
8658 if (TREE_CODE (value) == INTEGER_CST)
8659 pedwarn (loc, OPT_Wpedantic,
8660 "enumerator value for %qE is not an integer "
8661 "constant expression", name);
8663 if (TREE_CODE (value) != INTEGER_CST)
8665 error ("enumerator value for %qE is not an integer constant",
8666 name);
8667 value = NULL_TREE;
8669 else
8671 value = default_conversion (value);
8672 constant_expression_warning (value);
8677 /* Default based on previous value. */
8678 /* It should no longer be possible to have NON_LVALUE_EXPR
8679 in the default. */
8680 if (value == NULL_TREE)
8682 value = the_enum->enum_next_value;
8683 if (the_enum->enum_overflow)
8684 error_at (loc, "overflow in enumeration values");
8686 /* Even though the underlying type of an enum is unspecified, the
8687 type of enumeration constants is explicitly defined as int
8688 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8689 an extension. */
8690 else if (!int_fits_type_p (value, integer_type_node))
8691 pedwarn (loc, OPT_Wpedantic,
8692 "ISO C restricts enumerator values to range of %<int%>");
8694 /* The ISO C Standard mandates enumerators to have type int, even
8695 though the underlying type of an enum type is unspecified.
8696 However, GCC allows enumerators of any integer type as an
8697 extensions. Here we convert any enumerators that fit in an int
8698 to type int, to avoid promotions to unsigned types when comparing
8699 integers with enumerators that fit in the int range. When
8700 -pedantic is given, we would have already warned about those that
8701 don't fit. We have to do this here rather than in finish_enum
8702 because this value may be used to define more enumerators. */
8703 if (int_fits_type_p (value, integer_type_node))
8704 value = convert (integer_type_node, value);
8706 /* Set basis for default for next value. */
8707 the_enum->enum_next_value
8708 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8709 PLUS_EXPR, value, integer_one_node, false);
8710 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8712 /* Now create a declaration for the enum value name. */
8714 type = TREE_TYPE (value);
8715 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8716 TYPE_PRECISION (integer_type_node)),
8717 (TYPE_PRECISION (type)
8718 >= TYPE_PRECISION (integer_type_node)
8719 && TYPE_UNSIGNED (type)));
8721 decl = build_decl (decl_loc, CONST_DECL, name, type);
8722 DECL_INITIAL (decl) = convert (type, value);
8723 pushdecl (decl);
8725 return tree_cons (decl, value, NULL_TREE);
8729 /* Create the FUNCTION_DECL for a function definition.
8730 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8731 the declaration; they describe the function's name and the type it returns,
8732 but twisted together in a fashion that parallels the syntax of C.
8734 This function creates a binding context for the function body
8735 as well as setting up the FUNCTION_DECL in current_function_decl.
8737 Returns true on success. If the DECLARATOR is not suitable for a function
8738 (it defines a datum instead), we return false to report a parse error. */
8740 bool
8741 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8742 tree attributes)
8744 tree decl1, old_decl;
8745 tree restype, resdecl;
8746 location_t loc;
8748 current_function_returns_value = 0; /* Assume, until we see it does. */
8749 current_function_returns_null = 0;
8750 current_function_returns_abnormally = 0;
8751 warn_about_return_type = 0;
8752 c_switch_stack = NULL;
8754 /* Indicate no valid break/continue context by setting these variables
8755 to some non-null, non-label value. We'll notice and emit the proper
8756 error message in c_finish_bc_stmt. */
8757 c_break_label = c_cont_label = size_zero_node;
8759 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8760 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8761 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8763 /* If the declarator is not suitable for a function definition,
8764 cause a syntax error. */
8765 if (decl1 == NULL_TREE
8766 || TREE_CODE (decl1) != FUNCTION_DECL)
8767 return false;
8769 loc = DECL_SOURCE_LOCATION (decl1);
8771 c_decl_attributes (&decl1, attributes, 0);
8773 if (DECL_DECLARED_INLINE_P (decl1)
8774 && DECL_UNINLINABLE (decl1)
8775 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8776 warning_at (loc, OPT_Wattributes,
8777 "inline function %qD given attribute noinline",
8778 decl1);
8780 /* Handle gnu_inline attribute. */
8781 if (declspecs->inline_p
8782 && !flag_gnu89_inline
8783 && TREE_CODE (decl1) == FUNCTION_DECL
8784 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8785 || current_function_decl))
8787 if (declspecs->storage_class != csc_static)
8788 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8791 announce_function (decl1);
8793 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8795 error_at (loc, "return type is an incomplete type");
8796 /* Make it return void instead. */
8797 TREE_TYPE (decl1)
8798 = build_function_type (void_type_node,
8799 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8802 if (warn_about_return_type)
8803 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8804 : (warn_return_type > 0 ? OPT_Wreturn_type
8805 : OPT_Wimplicit_int),
8806 "return type defaults to %<int%>");
8808 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8809 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8810 DECL_INITIAL (decl1) = error_mark_node;
8812 /* A nested function is not global. */
8813 if (current_function_decl != NULL_TREE)
8814 TREE_PUBLIC (decl1) = 0;
8816 /* If this definition isn't a prototype and we had a prototype declaration
8817 before, copy the arg type info from that prototype. */
8818 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8819 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8820 old_decl = NULL_TREE;
8821 current_function_prototype_locus = UNKNOWN_LOCATION;
8822 current_function_prototype_built_in = false;
8823 current_function_prototype_arg_types = NULL_TREE;
8824 if (!prototype_p (TREE_TYPE (decl1)))
8826 if (old_decl != NULL_TREE
8827 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8828 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8829 TREE_TYPE (TREE_TYPE (old_decl))))
8831 if (stdarg_p (TREE_TYPE (old_decl)))
8833 auto_diagnostic_group d;
8834 warning_at (loc, 0, "%q+D defined as variadic function "
8835 "without prototype", decl1);
8836 locate_old_decl (old_decl);
8838 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8839 TREE_TYPE (decl1));
8840 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8841 current_function_prototype_built_in
8842 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8843 current_function_prototype_arg_types
8844 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8846 if (TREE_PUBLIC (decl1))
8848 /* If there is an external prototype declaration of this
8849 function, record its location but do not copy information
8850 to this decl. This may be an invisible declaration
8851 (built-in or in a scope which has finished) or simply
8852 have more refined argument types than any declaration
8853 found above. */
8854 struct c_binding *b;
8855 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8856 if (B_IN_SCOPE (b, external_scope))
8857 break;
8858 if (b)
8860 tree ext_decl, ext_type;
8861 ext_decl = b->decl;
8862 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8863 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8864 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8865 TREE_TYPE (ext_type)))
8867 current_function_prototype_locus
8868 = DECL_SOURCE_LOCATION (ext_decl);
8869 current_function_prototype_built_in
8870 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8871 current_function_prototype_arg_types
8872 = TYPE_ARG_TYPES (ext_type);
8878 /* Optionally warn of old-fashioned def with no previous prototype. */
8879 if (warn_strict_prototypes
8880 && old_decl != error_mark_node
8881 && !prototype_p (TREE_TYPE (decl1))
8882 && C_DECL_ISNT_PROTOTYPE (old_decl))
8883 warning_at (loc, OPT_Wstrict_prototypes,
8884 "function declaration isn%'t a prototype");
8885 /* Optionally warn of any global def with no previous prototype. */
8886 else if (warn_missing_prototypes
8887 && old_decl != error_mark_node
8888 && TREE_PUBLIC (decl1)
8889 && !MAIN_NAME_P (DECL_NAME (decl1))
8890 && C_DECL_ISNT_PROTOTYPE (old_decl)
8891 && !DECL_DECLARED_INLINE_P (decl1))
8892 warning_at (loc, OPT_Wmissing_prototypes,
8893 "no previous prototype for %qD", decl1);
8894 /* Optionally warn of any def with no previous prototype
8895 if the function has already been used. */
8896 else if (warn_missing_prototypes
8897 && old_decl != NULL_TREE
8898 && old_decl != error_mark_node
8899 && TREE_USED (old_decl)
8900 && !prototype_p (TREE_TYPE (old_decl)))
8901 warning_at (loc, OPT_Wmissing_prototypes,
8902 "%qD was used with no prototype before its definition", decl1);
8903 /* Optionally warn of any global def with no previous declaration. */
8904 else if (warn_missing_declarations
8905 && TREE_PUBLIC (decl1)
8906 && old_decl == NULL_TREE
8907 && !MAIN_NAME_P (DECL_NAME (decl1))
8908 && !DECL_DECLARED_INLINE_P (decl1))
8909 warning_at (loc, OPT_Wmissing_declarations,
8910 "no previous declaration for %qD",
8911 decl1);
8912 /* Optionally warn of any def with no previous declaration
8913 if the function has already been used. */
8914 else if (warn_missing_declarations
8915 && old_decl != NULL_TREE
8916 && old_decl != error_mark_node
8917 && TREE_USED (old_decl)
8918 && C_DECL_IMPLICIT (old_decl))
8919 warning_at (loc, OPT_Wmissing_declarations,
8920 "%qD was used with no declaration before its definition", decl1);
8922 /* This function exists in static storage.
8923 (This does not mean `static' in the C sense!) */
8924 TREE_STATIC (decl1) = 1;
8926 /* This is the earliest point at which we might know the assembler
8927 name of the function. Thus, if it's set before this, die horribly. */
8928 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8930 /* If #pragma weak was used, mark the decl weak now. */
8931 if (current_scope == file_scope)
8932 maybe_apply_pragma_weak (decl1);
8934 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8935 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8937 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8938 != integer_type_node)
8939 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8940 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8941 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8942 decl1);
8944 check_main_parameter_types (decl1);
8946 if (!TREE_PUBLIC (decl1))
8947 pedwarn (loc, OPT_Wmain,
8948 "%qD is normally a non-static function", decl1);
8951 /* Record the decl so that the function name is defined.
8952 If we already have a decl for this name, and it is a FUNCTION_DECL,
8953 use the old decl. */
8955 current_function_decl = pushdecl (decl1);
8957 push_scope ();
8958 declare_parm_level ();
8960 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8961 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8962 DECL_ARTIFICIAL (resdecl) = 1;
8963 DECL_IGNORED_P (resdecl) = 1;
8964 DECL_RESULT (current_function_decl) = resdecl;
8966 start_fname_decls ();
8968 return true;
8971 /* Subroutine of store_parm_decls which handles new-style function
8972 definitions (prototype format). The parms already have decls, so we
8973 need only record them as in effect and complain if any redundant
8974 old-style parm decls were written. */
8975 static void
8976 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8978 tree decl;
8979 c_arg_tag *tag;
8980 unsigned ix;
8982 if (current_scope->bindings)
8984 error_at (DECL_SOURCE_LOCATION (fndecl),
8985 "old-style parameter declarations in prototyped "
8986 "function definition");
8988 /* Get rid of the old-style declarations. */
8989 pop_scope ();
8990 push_scope ();
8992 /* Don't issue this warning for nested functions, and don't issue this
8993 warning if we got here because ARG_INFO_TYPES was error_mark_node
8994 (this happens when a function definition has just an ellipsis in
8995 its parameter list). */
8996 else if (!in_system_header_at (input_location)
8997 && !current_function_scope
8998 && arg_info->types != error_mark_node)
8999 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9000 "traditional C rejects ISO C style function definitions");
9002 /* Now make all the parameter declarations visible in the function body.
9003 We can bypass most of the grunt work of pushdecl. */
9004 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9006 DECL_CONTEXT (decl) = current_function_decl;
9007 if (DECL_NAME (decl))
9009 bind (DECL_NAME (decl), decl, current_scope,
9010 /*invisible=*/false, /*nested=*/false,
9011 UNKNOWN_LOCATION);
9012 if (!TREE_USED (decl))
9013 warn_if_shadowing (decl);
9015 else
9016 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9019 /* Record the parameter list in the function declaration. */
9020 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9022 /* Now make all the ancillary declarations visible, likewise. */
9023 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9025 DECL_CONTEXT (decl) = current_function_decl;
9026 if (DECL_NAME (decl))
9027 bind (DECL_NAME (decl), decl, current_scope,
9028 /*invisible=*/false,
9029 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9030 UNKNOWN_LOCATION);
9033 /* And all the tag declarations. */
9034 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9035 if (tag->id)
9036 bind (tag->id, tag->type, current_scope,
9037 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9040 /* Subroutine of store_parm_decls which handles old-style function
9041 definitions (separate parameter list and declarations). */
9043 static void
9044 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9046 struct c_binding *b;
9047 tree parm, decl, last;
9048 tree parmids = arg_info->parms;
9049 hash_set<tree> seen_args;
9051 if (!in_system_header_at (input_location))
9052 warning_at (DECL_SOURCE_LOCATION (fndecl),
9053 OPT_Wold_style_definition, "old-style function definition");
9055 /* Match each formal parameter name with its declaration. Save each
9056 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9057 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9059 if (TREE_VALUE (parm) == NULL_TREE)
9061 error_at (DECL_SOURCE_LOCATION (fndecl),
9062 "parameter name missing from parameter list");
9063 TREE_PURPOSE (parm) = NULL_TREE;
9064 continue;
9067 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9068 if (b && B_IN_CURRENT_SCOPE (b))
9070 decl = b->decl;
9071 /* Skip erroneous parameters. */
9072 if (decl == error_mark_node)
9073 continue;
9074 /* If we got something other than a PARM_DECL it is an error. */
9075 if (TREE_CODE (decl) != PARM_DECL)
9077 error_at (DECL_SOURCE_LOCATION (decl),
9078 "%qD declared as a non-parameter", decl);
9079 continue;
9081 /* If the declaration is already marked, we have a duplicate
9082 name. Complain and ignore the duplicate. */
9083 else if (seen_args.contains (decl))
9085 error_at (DECL_SOURCE_LOCATION (decl),
9086 "multiple parameters named %qD", decl);
9087 TREE_PURPOSE (parm) = NULL_TREE;
9088 continue;
9090 /* If the declaration says "void", complain and turn it into
9091 an int. */
9092 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9094 error_at (DECL_SOURCE_LOCATION (decl),
9095 "parameter %qD declared with void type", decl);
9096 TREE_TYPE (decl) = integer_type_node;
9097 DECL_ARG_TYPE (decl) = integer_type_node;
9098 layout_decl (decl, 0);
9100 warn_if_shadowing (decl);
9102 /* If no declaration found, default to int. */
9103 else
9105 /* FIXME diagnostics: This should be the location of the argument,
9106 not the FNDECL. E.g., for an old-style declaration
9108 int f10(v) { blah; }
9110 We should use the location of the V, not the F10.
9111 Unfortunately, the V is an IDENTIFIER_NODE which has no
9112 location. In the future we need locations for c_arg_info
9113 entries.
9115 See gcc.dg/Wshadow-3.c for an example of this problem. */
9116 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9117 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9118 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9119 pushdecl (decl);
9120 warn_if_shadowing (decl);
9122 if (flag_isoc99)
9123 pedwarn (DECL_SOURCE_LOCATION (decl),
9124 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9125 decl);
9126 else
9127 warning_at (DECL_SOURCE_LOCATION (decl),
9128 OPT_Wmissing_parameter_type,
9129 "type of %qD defaults to %<int%>", decl);
9132 TREE_PURPOSE (parm) = decl;
9133 seen_args.add (decl);
9136 /* Now examine the parms chain for incomplete declarations
9137 and declarations with no corresponding names. */
9139 for (b = current_scope->bindings; b; b = b->prev)
9141 parm = b->decl;
9142 if (TREE_CODE (parm) != PARM_DECL)
9143 continue;
9145 if (TREE_TYPE (parm) != error_mark_node
9146 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9148 error_at (DECL_SOURCE_LOCATION (parm),
9149 "parameter %qD has incomplete type", parm);
9150 TREE_TYPE (parm) = error_mark_node;
9153 if (!seen_args.contains (parm))
9155 error_at (DECL_SOURCE_LOCATION (parm),
9156 "declaration for parameter %qD but no such parameter",
9157 parm);
9159 /* Pretend the parameter was not missing.
9160 This gets us to a standard state and minimizes
9161 further error messages. */
9162 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9166 /* Chain the declarations together in the order of the list of
9167 names. Store that chain in the function decl, replacing the
9168 list of names. Update the current scope to match. */
9169 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9171 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9172 if (TREE_PURPOSE (parm))
9173 break;
9174 if (parm && TREE_PURPOSE (parm))
9176 last = TREE_PURPOSE (parm);
9177 DECL_ARGUMENTS (fndecl) = last;
9179 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9180 if (TREE_PURPOSE (parm))
9182 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9183 last = TREE_PURPOSE (parm);
9185 DECL_CHAIN (last) = NULL_TREE;
9188 /* If there was a previous prototype,
9189 set the DECL_ARG_TYPE of each argument according to
9190 the type previously specified, and report any mismatches. */
9192 if (current_function_prototype_arg_types)
9194 tree type;
9195 for (parm = DECL_ARGUMENTS (fndecl),
9196 type = current_function_prototype_arg_types;
9197 parm || (type != NULL_TREE
9198 && TREE_VALUE (type) != error_mark_node
9199 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9200 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9202 if (parm == NULL_TREE
9203 || type == NULL_TREE
9204 || (TREE_VALUE (type) != error_mark_node
9205 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9207 if (current_function_prototype_built_in)
9208 warning_at (DECL_SOURCE_LOCATION (fndecl),
9209 0, "number of arguments doesn%'t match "
9210 "built-in prototype");
9211 else
9213 /* FIXME diagnostics: This should be the location of
9214 FNDECL, but there is bug when a prototype is
9215 declared inside function context, but defined
9216 outside of it (e.g., gcc.dg/pr15698-2.c). In
9217 which case FNDECL gets the location of the
9218 prototype, not the definition. */
9219 error_at (input_location,
9220 "number of arguments doesn%'t match prototype");
9222 error_at (current_function_prototype_locus,
9223 "prototype declaration");
9225 break;
9227 /* Type for passing arg must be consistent with that
9228 declared for the arg. ISO C says we take the unqualified
9229 type for parameters declared with qualified type. */
9230 if (TREE_TYPE (parm) != error_mark_node
9231 && TREE_VALUE (type) != error_mark_node
9232 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9233 != TYPE_ATOMIC (TREE_VALUE (type)))
9234 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9235 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9237 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9238 == TYPE_ATOMIC (TREE_VALUE (type)))
9239 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9240 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9242 /* Adjust argument to match prototype. E.g. a previous
9243 `int foo(float);' prototype causes
9244 `int foo(x) float x; {...}' to be treated like
9245 `int foo(float x) {...}'. This is particularly
9246 useful for argument types like uid_t. */
9247 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9249 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9250 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9251 && (TYPE_PRECISION (TREE_TYPE (parm))
9252 < TYPE_PRECISION (integer_type_node)))
9253 DECL_ARG_TYPE (parm)
9254 = c_type_promotes_to (TREE_TYPE (parm));
9256 /* ??? Is it possible to get here with a
9257 built-in prototype or will it always have
9258 been diagnosed as conflicting with an
9259 old-style definition and discarded? */
9260 if (current_function_prototype_built_in)
9261 warning_at (DECL_SOURCE_LOCATION (parm),
9262 OPT_Wpedantic, "promoted argument %qD "
9263 "doesn%'t match built-in prototype", parm);
9264 else
9266 pedwarn (DECL_SOURCE_LOCATION (parm),
9267 OPT_Wpedantic, "promoted argument %qD "
9268 "doesn%'t match prototype", parm);
9269 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9270 "prototype declaration");
9273 else
9275 if (current_function_prototype_built_in)
9276 warning_at (DECL_SOURCE_LOCATION (parm),
9277 0, "argument %qD doesn%'t match "
9278 "built-in prototype", parm);
9279 else
9281 error_at (DECL_SOURCE_LOCATION (parm),
9282 "argument %qD doesn%'t match prototype", parm);
9283 error_at (current_function_prototype_locus,
9284 "prototype declaration");
9289 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9292 /* Otherwise, create a prototype that would match. */
9294 else
9296 tree actual = NULL_TREE, last = NULL_TREE, type;
9298 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9300 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9301 if (last)
9302 TREE_CHAIN (last) = type;
9303 else
9304 actual = type;
9305 last = type;
9307 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9308 if (last)
9309 TREE_CHAIN (last) = type;
9310 else
9311 actual = type;
9313 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9314 of the type of this function, but we need to avoid having this
9315 affect the types of other similarly-typed functions, so we must
9316 first force the generation of an identical (but separate) type
9317 node for the relevant function type. The new node we create
9318 will be a variant of the main variant of the original function
9319 type. */
9321 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9323 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9327 /* Store parameter declarations passed in ARG_INFO into the current
9328 function declaration. */
9330 void
9331 store_parm_decls_from (struct c_arg_info *arg_info)
9333 current_function_arg_info = arg_info;
9334 store_parm_decls ();
9337 /* Called by walk_tree to look for and update context-less labels. */
9339 static tree
9340 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9342 if (TREE_CODE (*tp) == LABEL_EXPR
9343 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9345 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9346 *walk_subtrees = 0;
9349 return NULL_TREE;
9352 /* Store the parameter declarations into the current function declaration.
9353 This is called after parsing the parameter declarations, before
9354 digesting the body of the function.
9356 For an old-style definition, construct a prototype out of the old-style
9357 parameter declarations and inject it into the function's type. */
9359 void
9360 store_parm_decls (void)
9362 tree fndecl = current_function_decl;
9363 bool proto;
9365 /* The argument information block for FNDECL. */
9366 struct c_arg_info *arg_info = current_function_arg_info;
9367 current_function_arg_info = 0;
9369 /* True if this definition is written with a prototype. Note:
9370 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9371 list in a function definition as equivalent to (void) -- an
9372 empty argument list specifies the function has no parameters,
9373 but only (void) sets up a prototype for future calls. */
9374 proto = arg_info->types != 0;
9376 if (proto)
9377 store_parm_decls_newstyle (fndecl, arg_info);
9378 else
9379 store_parm_decls_oldstyle (fndecl, arg_info);
9381 /* The next call to push_scope will be a function body. */
9383 next_is_function_body = true;
9385 /* Write a record describing this function definition to the prototypes
9386 file (if requested). */
9388 gen_aux_info_record (fndecl, 1, 0, proto);
9390 /* Initialize the RTL code for the function. */
9391 allocate_struct_function (fndecl, false);
9393 if (warn_unused_local_typedefs)
9394 cfun->language = ggc_cleared_alloc<language_function> ();
9396 /* Begin the statement tree for this function. */
9397 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9399 /* ??? Insert the contents of the pending sizes list into the function
9400 to be evaluated. The only reason left to have this is
9401 void foo(int n, int array[n++])
9402 because we throw away the array type in favor of a pointer type, and
9403 thus won't naturally see the SAVE_EXPR containing the increment. All
9404 other pending sizes would be handled by gimplify_parameters. */
9405 if (arg_info->pending_sizes)
9407 /* In very special circumstances, e.g. for code like
9408 _Atomic int i = 5;
9409 void f (int a[i += 2]) {}
9410 we need to execute the atomic assignment on function entry.
9411 But in this case, it is not just a straight store, it has the
9412 op= form, which means that build_atomic_assign has generated
9413 gotos, labels, etc. Because at that time the function decl
9414 for F has not been created yet, those labels do not have any
9415 function context. But we have the fndecl now, so update the
9416 labels accordingly. gimplify_expr would crash otherwise. */
9417 walk_tree_without_duplicates (&arg_info->pending_sizes,
9418 set_labels_context_r, fndecl);
9419 add_stmt (arg_info->pending_sizes);
9423 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9424 c_finish_omp_declare_simd for function prototypes. No diagnostics
9425 should be done. */
9427 void
9428 temp_store_parm_decls (tree fndecl, tree parms)
9430 push_scope ();
9431 for (tree p = parms; p; p = DECL_CHAIN (p))
9433 DECL_CONTEXT (p) = fndecl;
9434 if (DECL_NAME (p))
9435 bind (DECL_NAME (p), p, current_scope,
9436 /*invisible=*/false, /*nested=*/false,
9437 UNKNOWN_LOCATION);
9441 /* Undo what temp_store_parm_decls did. */
9443 void
9444 temp_pop_parm_decls (void)
9446 /* Clear all bindings in this temporary scope, so that
9447 pop_scope doesn't create a BLOCK. */
9448 struct c_binding *b = current_scope->bindings;
9449 current_scope->bindings = NULL;
9450 for (; b; b = free_binding_and_advance (b))
9452 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9453 || b->decl == error_mark_node);
9454 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9455 I_SYMBOL_BINDING (b->id) = b->shadowed;
9456 if (b->shadowed && b->shadowed->u.type)
9457 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9459 pop_scope ();
9463 /* Finish up a function declaration and compile that function
9464 all the way to assembler language output. Then free the storage
9465 for the function definition.
9467 This is called after parsing the body of the function definition. */
9469 void
9470 finish_function (void)
9472 tree fndecl = current_function_decl;
9474 if (c_dialect_objc ())
9475 objc_finish_function ();
9477 if (TREE_CODE (fndecl) == FUNCTION_DECL
9478 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9480 tree args = DECL_ARGUMENTS (fndecl);
9481 for (; args; args = DECL_CHAIN (args))
9483 tree type = TREE_TYPE (args);
9484 if (INTEGRAL_TYPE_P (type)
9485 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9486 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9490 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9491 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9493 /* Must mark the RESULT_DECL as being in this function. */
9495 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9496 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9498 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9499 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9500 == integer_type_node && flag_isoc99)
9502 /* Hack. We don't want the middle-end to warn that this return
9503 is unreachable, so we mark its location as special. Using
9504 UNKNOWN_LOCATION has the problem that it gets clobbered in
9505 annotate_one_with_locus. A cleaner solution might be to
9506 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9508 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9511 /* Tie off the statement tree for this function. */
9512 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9514 finish_fname_decls ();
9516 /* Complain if there's no return statement only if option specified on
9517 command line. */
9518 if (warn_return_type > 0
9519 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9520 && !current_function_returns_value && !current_function_returns_null
9521 /* Don't complain if we are no-return. */
9522 && !current_function_returns_abnormally
9523 /* Don't complain if we are declared noreturn. */
9524 && !TREE_THIS_VOLATILE (fndecl)
9525 /* Don't warn for main(). */
9526 && !MAIN_NAME_P (DECL_NAME (fndecl))
9527 /* Or if they didn't actually specify a return type. */
9528 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9529 /* Normally, with -Wreturn-type, flow will complain, but we might
9530 optimize out static functions. */
9531 && !TREE_PUBLIC (fndecl))
9533 warning (OPT_Wreturn_type,
9534 "no return statement in function returning non-void");
9535 TREE_NO_WARNING (fndecl) = 1;
9538 /* Complain about parameters that are only set, but never otherwise used. */
9539 if (warn_unused_but_set_parameter)
9541 tree decl;
9543 for (decl = DECL_ARGUMENTS (fndecl);
9544 decl;
9545 decl = DECL_CHAIN (decl))
9546 if (TREE_USED (decl)
9547 && TREE_CODE (decl) == PARM_DECL
9548 && !DECL_READ_P (decl)
9549 && DECL_NAME (decl)
9550 && !DECL_ARTIFICIAL (decl)
9551 && !TREE_NO_WARNING (decl))
9552 warning_at (DECL_SOURCE_LOCATION (decl),
9553 OPT_Wunused_but_set_parameter,
9554 "parameter %qD set but not used", decl);
9557 /* Complain about locally defined typedefs that are not used in this
9558 function. */
9559 maybe_warn_unused_local_typedefs ();
9561 /* Possibly warn about unused parameters. */
9562 if (warn_unused_parameter)
9563 do_warn_unused_parameter (fndecl);
9565 /* Store the end of the function, so that we get good line number
9566 info for the epilogue. */
9567 cfun->function_end_locus = input_location;
9569 /* Finalize the ELF visibility for the function. */
9570 c_determine_visibility (fndecl);
9572 /* For GNU C extern inline functions disregard inline limits. */
9573 if (DECL_EXTERNAL (fndecl)
9574 && DECL_DECLARED_INLINE_P (fndecl)
9575 && (flag_gnu89_inline
9576 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9577 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9579 /* Genericize before inlining. Delay genericizing nested functions
9580 until their parent function is genericized. Since finalizing
9581 requires GENERIC, delay that as well. */
9583 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9584 && !undef_nested_function)
9586 if (!decl_function_context (fndecl))
9588 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9589 c_genericize (fndecl);
9591 /* ??? Objc emits functions after finalizing the compilation unit.
9592 This should be cleaned up later and this conditional removed. */
9593 if (symtab->global_info_ready)
9595 cgraph_node::add_new_function (fndecl, false);
9596 return;
9598 cgraph_node::finalize_function (fndecl, false);
9600 else
9602 /* Register this function with cgraph just far enough to get it
9603 added to our parent's nested function list. Handy, since the
9604 C front end doesn't have such a list. */
9605 (void) cgraph_node::get_create (fndecl);
9609 if (!decl_function_context (fndecl))
9610 undef_nested_function = false;
9612 if (cfun->language != NULL)
9614 ggc_free (cfun->language);
9615 cfun->language = NULL;
9618 /* We're leaving the context of this function, so zap cfun.
9619 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9620 tree_rest_of_compilation. */
9621 set_cfun (NULL);
9622 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9623 current_function_decl = NULL;
9626 /* Check the declarations given in a for-loop for satisfying the C99
9627 constraints. If exactly one such decl is found, return it. LOC is
9628 the location of the opening parenthesis of the for loop. The last
9629 parameter allows you to control the "for loop initial declarations
9630 are only allowed in C99 mode". Normally, you should pass
9631 flag_isoc99 as that parameter. But in some cases (Objective-C
9632 foreach loop, for example) we want to run the checks in this
9633 function even if not in C99 mode, so we allow the caller to turn
9634 off the error about not being in C99 mode.
9637 tree
9638 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9640 struct c_binding *b;
9641 tree one_decl = NULL_TREE;
9642 int n_decls = 0;
9644 if (!turn_off_iso_c99_error)
9646 static bool hint = true;
9647 /* If we get here, declarations have been used in a for loop without
9648 the C99 for loop scope. This doesn't make much sense, so don't
9649 allow it. */
9650 error_at (loc, "%<for%> loop initial declarations "
9651 "are only allowed in C99 or C11 mode");
9652 if (hint)
9654 inform (loc,
9655 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9656 "to compile your code");
9657 hint = false;
9659 return NULL_TREE;
9661 else
9662 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
9663 "initial declarations");
9665 /* C99 subclause 6.8.5 paragraph 3:
9667 [#3] The declaration part of a for statement shall only
9668 declare identifiers for objects having storage class auto or
9669 register.
9671 It isn't clear whether, in this sentence, "identifiers" binds to
9672 "shall only declare" or to "objects" - that is, whether all identifiers
9673 declared must be identifiers for objects, or whether the restriction
9674 only applies to those that are. (A question on this in comp.std.c
9675 in November 2000 received no answer.) We implement the strictest
9676 interpretation, to avoid creating an extension which later causes
9677 problems. */
9679 for (b = current_scope->bindings; b; b = b->prev)
9681 tree id = b->id;
9682 tree decl = b->decl;
9684 if (!id)
9685 continue;
9687 switch (TREE_CODE (decl))
9689 case VAR_DECL:
9691 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9692 if (TREE_STATIC (decl))
9693 error_at (decl_loc,
9694 "declaration of static variable %qD in %<for%> loop "
9695 "initial declaration", decl);
9696 else if (DECL_EXTERNAL (decl))
9697 error_at (decl_loc,
9698 "declaration of %<extern%> variable %qD in %<for%> loop "
9699 "initial declaration", decl);
9701 break;
9703 case RECORD_TYPE:
9704 error_at (loc,
9705 "%<struct %E%> declared in %<for%> loop initial "
9706 "declaration", id);
9707 break;
9708 case UNION_TYPE:
9709 error_at (loc,
9710 "%<union %E%> declared in %<for%> loop initial declaration",
9711 id);
9712 break;
9713 case ENUMERAL_TYPE:
9714 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9715 "initial declaration", id);
9716 break;
9717 default:
9718 error_at (loc, "declaration of non-variable "
9719 "%qD in %<for%> loop initial declaration", decl);
9722 n_decls++;
9723 one_decl = decl;
9726 return n_decls == 1 ? one_decl : NULL_TREE;
9729 /* Save and reinitialize the variables
9730 used during compilation of a C function. */
9732 void
9733 c_push_function_context (void)
9735 struct language_function *p = cfun->language;
9736 /* cfun->language might have been already allocated by the use of
9737 -Wunused-local-typedefs. In that case, just re-use it. */
9738 if (p == NULL)
9739 cfun->language = p = ggc_cleared_alloc<language_function> ();
9741 p->base.x_stmt_tree = c_stmt_tree;
9742 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9743 p->x_break_label = c_break_label;
9744 p->x_cont_label = c_cont_label;
9745 p->x_switch_stack = c_switch_stack;
9746 p->arg_info = current_function_arg_info;
9747 p->returns_value = current_function_returns_value;
9748 p->returns_null = current_function_returns_null;
9749 p->returns_abnormally = current_function_returns_abnormally;
9750 p->warn_about_return_type = warn_about_return_type;
9752 push_function_context ();
9755 /* Restore the variables used during compilation of a C function. */
9757 void
9758 c_pop_function_context (void)
9760 struct language_function *p;
9762 pop_function_context ();
9763 p = cfun->language;
9765 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9766 used to store data throughout the life time of the current cfun,
9767 So don't deallocate it. */
9768 if (!warn_unused_local_typedefs)
9769 cfun->language = NULL;
9771 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9772 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9774 /* Stop pointing to the local nodes about to be freed. */
9775 /* But DECL_INITIAL must remain nonzero so we know this
9776 was an actual function definition. */
9777 DECL_INITIAL (current_function_decl) = error_mark_node;
9778 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9781 c_stmt_tree = p->base.x_stmt_tree;
9782 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9783 c_break_label = p->x_break_label;
9784 c_cont_label = p->x_cont_label;
9785 c_switch_stack = p->x_switch_stack;
9786 current_function_arg_info = p->arg_info;
9787 current_function_returns_value = p->returns_value;
9788 current_function_returns_null = p->returns_null;
9789 current_function_returns_abnormally = p->returns_abnormally;
9790 warn_about_return_type = p->warn_about_return_type;
9793 /* The functions below are required for functionality of doing
9794 function at once processing in the C front end. Currently these
9795 functions are not called from anywhere in the C front end, but as
9796 these changes continue, that will change. */
9798 /* Returns the stmt_tree (if any) to which statements are currently
9799 being added. If there is no active statement-tree, NULL is
9800 returned. */
9802 stmt_tree
9803 current_stmt_tree (void)
9805 return &c_stmt_tree;
9808 /* Return the global value of T as a symbol. */
9810 tree
9811 identifier_global_value (tree t)
9813 struct c_binding *b;
9815 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9816 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9817 return b->decl;
9819 return NULL_TREE;
9822 /* In C, the only C-linkage public declaration is at file scope. */
9824 tree
9825 c_linkage_bindings (tree name)
9827 return identifier_global_value (name);
9830 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9831 otherwise the name is found in ridpointers from RID_INDEX. */
9833 void
9834 record_builtin_type (enum rid rid_index, const char *name, tree type)
9836 tree id, decl;
9837 if (name == 0)
9838 id = ridpointers[(int) rid_index];
9839 else
9840 id = get_identifier (name);
9841 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9842 pushdecl (decl);
9843 if (debug_hooks->type_decl)
9844 debug_hooks->type_decl (decl, false);
9847 /* Build the void_list_node (void_type_node having been created). */
9848 tree
9849 build_void_list_node (void)
9851 tree t = build_tree_list (NULL_TREE, void_type_node);
9852 return t;
9855 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9857 struct c_parm *
9858 build_c_parm (struct c_declspecs *specs, tree attrs,
9859 struct c_declarator *declarator,
9860 location_t loc)
9862 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9863 ret->specs = specs;
9864 ret->attrs = attrs;
9865 ret->declarator = declarator;
9866 ret->loc = loc;
9867 return ret;
9870 /* Return a declarator with nested attributes. TARGET is the inner
9871 declarator to which these attributes apply. ATTRS are the
9872 attributes. */
9874 struct c_declarator *
9875 build_attrs_declarator (tree attrs, struct c_declarator *target)
9877 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9878 ret->kind = cdk_attrs;
9879 ret->declarator = target;
9880 ret->u.attrs = attrs;
9881 return ret;
9884 /* Return a declarator for a function with arguments specified by ARGS
9885 and return type specified by TARGET. */
9887 struct c_declarator *
9888 build_function_declarator (struct c_arg_info *args,
9889 struct c_declarator *target)
9891 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9892 ret->kind = cdk_function;
9893 ret->declarator = target;
9894 ret->u.arg_info = args;
9895 return ret;
9898 /* Return a declarator for the identifier IDENT (which may be
9899 NULL_TREE for an abstract declarator). */
9901 struct c_declarator *
9902 build_id_declarator (tree ident)
9904 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9905 ret->kind = cdk_id;
9906 ret->declarator = 0;
9907 ret->u.id = ident;
9908 /* Default value - may get reset to a more precise location. */
9909 ret->id_loc = input_location;
9910 return ret;
9913 /* Return something to represent absolute declarators containing a *.
9914 TARGET is the absolute declarator that the * contains.
9915 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9916 to apply to the pointer type. */
9918 struct c_declarator *
9919 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9920 struct c_declarator *target)
9922 tree attrs;
9923 int quals = 0;
9924 struct c_declarator *itarget = target;
9925 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9926 if (type_quals_attrs)
9928 attrs = type_quals_attrs->attrs;
9929 quals = quals_from_declspecs (type_quals_attrs);
9930 if (attrs != NULL_TREE)
9931 itarget = build_attrs_declarator (attrs, target);
9933 ret->kind = cdk_pointer;
9934 ret->declarator = itarget;
9935 ret->u.pointer_quals = quals;
9936 return ret;
9939 /* Return a pointer to a structure for an empty list of declaration
9940 specifiers. */
9942 struct c_declspecs *
9943 build_null_declspecs (void)
9945 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9946 memset (ret, 0, sizeof *ret);
9947 ret->align_log = -1;
9948 ret->typespec_word = cts_none;
9949 ret->storage_class = csc_none;
9950 ret->expr_const_operands = true;
9951 ret->typespec_kind = ctsk_none;
9952 ret->address_space = ADDR_SPACE_GENERIC;
9953 return ret;
9956 /* Add the address space ADDRSPACE to the declaration specifiers
9957 SPECS, returning SPECS. */
9959 struct c_declspecs *
9960 declspecs_add_addrspace (source_location location,
9961 struct c_declspecs *specs, addr_space_t as)
9963 specs->non_sc_seen_p = true;
9964 specs->declspecs_seen_p = true;
9966 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9967 && specs->address_space != as)
9968 error ("incompatible address space qualifiers %qs and %qs",
9969 c_addr_space_name (as),
9970 c_addr_space_name (specs->address_space));
9971 else
9973 specs->address_space = as;
9974 specs->locations[cdw_address_space] = location;
9976 return specs;
9979 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9980 returning SPECS. */
9982 struct c_declspecs *
9983 declspecs_add_qual (source_location loc,
9984 struct c_declspecs *specs, tree qual)
9986 enum rid i;
9987 bool dupe = false;
9988 specs->non_sc_seen_p = true;
9989 specs->declspecs_seen_p = true;
9990 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9991 && C_IS_RESERVED_WORD (qual));
9992 i = C_RID_CODE (qual);
9993 location_t prev_loc = UNKNOWN_LOCATION;
9994 switch (i)
9996 case RID_CONST:
9997 dupe = specs->const_p;
9998 specs->const_p = true;
9999 prev_loc = specs->locations[cdw_const];
10000 specs->locations[cdw_const] = loc;
10001 break;
10002 case RID_VOLATILE:
10003 dupe = specs->volatile_p;
10004 specs->volatile_p = true;
10005 prev_loc = specs->locations[cdw_volatile];
10006 specs->locations[cdw_volatile] = loc;
10007 break;
10008 case RID_RESTRICT:
10009 dupe = specs->restrict_p;
10010 specs->restrict_p = true;
10011 prev_loc = specs->locations[cdw_restrict];
10012 specs->locations[cdw_restrict] = loc;
10013 break;
10014 case RID_ATOMIC:
10015 dupe = specs->atomic_p;
10016 specs->atomic_p = true;
10017 prev_loc = specs->locations[cdw_atomic];
10018 specs->locations[cdw_atomic] = loc;
10019 break;
10020 default:
10021 gcc_unreachable ();
10023 if (dupe)
10025 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10026 "duplicate %qE declaration specifier", qual);
10027 if (!warned
10028 && warn_duplicate_decl_specifier
10029 && prev_loc >= RESERVED_LOCATION_COUNT
10030 && !from_macro_expansion_at (prev_loc)
10031 && !from_macro_expansion_at (loc))
10032 warning_at (loc, OPT_Wduplicate_decl_specifier,
10033 "duplicate %qE declaration specifier", qual);
10035 return specs;
10038 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10039 returning SPECS. */
10041 struct c_declspecs *
10042 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10043 struct c_typespec spec)
10045 tree type = spec.spec;
10046 specs->non_sc_seen_p = true;
10047 specs->declspecs_seen_p = true;
10048 specs->typespec_kind = spec.kind;
10049 if (TREE_DEPRECATED (type))
10050 specs->deprecated_p = true;
10052 /* Handle type specifier keywords. */
10053 if (TREE_CODE (type) == IDENTIFIER_NODE
10054 && C_IS_RESERVED_WORD (type)
10055 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10057 enum rid i = C_RID_CODE (type);
10058 if (specs->type)
10060 error_at (loc, "two or more data types in declaration specifiers");
10061 return specs;
10063 if ((int) i <= (int) RID_LAST_MODIFIER)
10065 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10066 bool dupe = false;
10067 switch (i)
10069 case RID_LONG:
10070 if (specs->long_long_p)
10072 error_at (loc, "%<long long long%> is too long for GCC");
10073 break;
10075 if (specs->long_p)
10077 if (specs->typespec_word == cts_double)
10079 error_at (loc,
10080 ("both %<long long%> and %<double%> in "
10081 "declaration specifiers"));
10082 break;
10084 pedwarn_c90 (loc, OPT_Wlong_long,
10085 "ISO C90 does not support %<long long%>");
10086 specs->long_long_p = 1;
10087 specs->locations[cdw_long_long] = loc;
10088 break;
10090 if (specs->short_p)
10091 error_at (loc,
10092 ("both %<long%> and %<short%> in "
10093 "declaration specifiers"));
10094 else if (specs->typespec_word == cts_auto_type)
10095 error_at (loc,
10096 ("both %<long%> and %<__auto_type%> in "
10097 "declaration specifiers"));
10098 else if (specs->typespec_word == cts_void)
10099 error_at (loc,
10100 ("both %<long%> and %<void%> in "
10101 "declaration specifiers"));
10102 else if (specs->typespec_word == cts_int_n)
10103 error_at (loc,
10104 ("both %<long%> and %<__int%d%> in "
10105 "declaration specifiers"),
10106 int_n_data[specs->int_n_idx].bitsize);
10107 else if (specs->typespec_word == cts_bool)
10108 error_at (loc,
10109 ("both %<long%> and %<_Bool%> in "
10110 "declaration specifiers"));
10111 else if (specs->typespec_word == cts_char)
10112 error_at (loc,
10113 ("both %<long%> and %<char%> in "
10114 "declaration specifiers"));
10115 else if (specs->typespec_word == cts_float)
10116 error_at (loc,
10117 ("both %<long%> and %<float%> in "
10118 "declaration specifiers"));
10119 else if (specs->typespec_word == cts_floatn_nx)
10120 error_at (loc,
10121 ("both %<long%> and %<_Float%d%s%> in "
10122 "declaration specifiers"),
10123 floatn_nx_types[specs->floatn_nx_idx].n,
10124 (floatn_nx_types[specs->floatn_nx_idx].extended
10125 ? "x"
10126 : ""));
10127 else if (specs->typespec_word == cts_dfloat32)
10128 error_at (loc,
10129 ("both %<long%> and %<_Decimal32%> in "
10130 "declaration specifiers"));
10131 else if (specs->typespec_word == cts_dfloat64)
10132 error_at (loc,
10133 ("both %<long%> and %<_Decimal64%> in "
10134 "declaration specifiers"));
10135 else if (specs->typespec_word == cts_dfloat128)
10136 error_at (loc,
10137 ("both %<long%> and %<_Decimal128%> in "
10138 "declaration specifiers"));
10139 else
10141 specs->long_p = true;
10142 specs->locations[cdw_long] = loc;
10144 break;
10145 case RID_SHORT:
10146 dupe = specs->short_p;
10147 if (specs->long_p)
10148 error_at (loc,
10149 ("both %<long%> and %<short%> in "
10150 "declaration specifiers"));
10151 else if (specs->typespec_word == cts_auto_type)
10152 error_at (loc,
10153 ("both %<short%> and %<__auto_type%> in "
10154 "declaration specifiers"));
10155 else if (specs->typespec_word == cts_void)
10156 error_at (loc,
10157 ("both %<short%> and %<void%> in "
10158 "declaration specifiers"));
10159 else if (specs->typespec_word == cts_int_n)
10160 error_at (loc,
10161 ("both %<short%> and %<__int%d%> in "
10162 "declaration specifiers"),
10163 int_n_data[specs->int_n_idx].bitsize);
10164 else if (specs->typespec_word == cts_bool)
10165 error_at (loc,
10166 ("both %<short%> and %<_Bool%> in "
10167 "declaration specifiers"));
10168 else if (specs->typespec_word == cts_char)
10169 error_at (loc,
10170 ("both %<short%> and %<char%> in "
10171 "declaration specifiers"));
10172 else if (specs->typespec_word == cts_float)
10173 error_at (loc,
10174 ("both %<short%> and %<float%> in "
10175 "declaration specifiers"));
10176 else if (specs->typespec_word == cts_double)
10177 error_at (loc,
10178 ("both %<short%> and %<double%> in "
10179 "declaration specifiers"));
10180 else if (specs->typespec_word == cts_floatn_nx)
10181 error_at (loc,
10182 ("both %<short%> and %<_Float%d%s%> in "
10183 "declaration specifiers"),
10184 floatn_nx_types[specs->floatn_nx_idx].n,
10185 (floatn_nx_types[specs->floatn_nx_idx].extended
10186 ? "x"
10187 : ""));
10188 else if (specs->typespec_word == cts_dfloat32)
10189 error_at (loc,
10190 ("both %<short%> and %<_Decimal32%> in "
10191 "declaration specifiers"));
10192 else if (specs->typespec_word == cts_dfloat64)
10193 error_at (loc,
10194 ("both %<short%> and %<_Decimal64%> in "
10195 "declaration specifiers"));
10196 else if (specs->typespec_word == cts_dfloat128)
10197 error_at (loc,
10198 ("both %<short%> and %<_Decimal128%> in "
10199 "declaration specifiers"));
10200 else
10202 specs->short_p = true;
10203 specs->locations[cdw_short] = loc;
10205 break;
10206 case RID_SIGNED:
10207 dupe = specs->signed_p;
10208 if (specs->unsigned_p)
10209 error_at (loc,
10210 ("both %<signed%> and %<unsigned%> in "
10211 "declaration specifiers"));
10212 else if (specs->typespec_word == cts_auto_type)
10213 error_at (loc,
10214 ("both %<signed%> and %<__auto_type%> in "
10215 "declaration specifiers"));
10216 else if (specs->typespec_word == cts_void)
10217 error_at (loc,
10218 ("both %<signed%> and %<void%> in "
10219 "declaration specifiers"));
10220 else if (specs->typespec_word == cts_bool)
10221 error_at (loc,
10222 ("both %<signed%> and %<_Bool%> in "
10223 "declaration specifiers"));
10224 else if (specs->typespec_word == cts_float)
10225 error_at (loc,
10226 ("both %<signed%> and %<float%> in "
10227 "declaration specifiers"));
10228 else if (specs->typespec_word == cts_double)
10229 error_at (loc,
10230 ("both %<signed%> and %<double%> in "
10231 "declaration specifiers"));
10232 else if (specs->typespec_word == cts_floatn_nx)
10233 error_at (loc,
10234 ("both %<signed%> and %<_Float%d%s%> in "
10235 "declaration specifiers"),
10236 floatn_nx_types[specs->floatn_nx_idx].n,
10237 (floatn_nx_types[specs->floatn_nx_idx].extended
10238 ? "x"
10239 : ""));
10240 else if (specs->typespec_word == cts_dfloat32)
10241 error_at (loc,
10242 ("both %<signed%> and %<_Decimal32%> in "
10243 "declaration specifiers"));
10244 else if (specs->typespec_word == cts_dfloat64)
10245 error_at (loc,
10246 ("both %<signed%> and %<_Decimal64%> in "
10247 "declaration specifiers"));
10248 else if (specs->typespec_word == cts_dfloat128)
10249 error_at (loc,
10250 ("both %<signed%> and %<_Decimal128%> in "
10251 "declaration specifiers"));
10252 else
10254 specs->signed_p = true;
10255 specs->locations[cdw_signed] = loc;
10257 break;
10258 case RID_UNSIGNED:
10259 dupe = specs->unsigned_p;
10260 if (specs->signed_p)
10261 error_at (loc,
10262 ("both %<signed%> and %<unsigned%> in "
10263 "declaration specifiers"));
10264 else if (specs->typespec_word == cts_auto_type)
10265 error_at (loc,
10266 ("both %<unsigned%> and %<__auto_type%> in "
10267 "declaration specifiers"));
10268 else if (specs->typespec_word == cts_void)
10269 error_at (loc,
10270 ("both %<unsigned%> and %<void%> in "
10271 "declaration specifiers"));
10272 else if (specs->typespec_word == cts_bool)
10273 error_at (loc,
10274 ("both %<unsigned%> and %<_Bool%> in "
10275 "declaration specifiers"));
10276 else if (specs->typespec_word == cts_float)
10277 error_at (loc,
10278 ("both %<unsigned%> and %<float%> in "
10279 "declaration specifiers"));
10280 else if (specs->typespec_word == cts_double)
10281 error_at (loc,
10282 ("both %<unsigned%> and %<double%> in "
10283 "declaration specifiers"));
10284 else if (specs->typespec_word == cts_floatn_nx)
10285 error_at (loc,
10286 ("both %<unsigned%> and %<_Float%d%s%> in "
10287 "declaration specifiers"),
10288 floatn_nx_types[specs->floatn_nx_idx].n,
10289 (floatn_nx_types[specs->floatn_nx_idx].extended
10290 ? "x"
10291 : ""));
10292 else if (specs->typespec_word == cts_dfloat32)
10293 error_at (loc,
10294 ("both %<unsigned%> and %<_Decimal32%> in "
10295 "declaration specifiers"));
10296 else if (specs->typespec_word == cts_dfloat64)
10297 error_at (loc,
10298 ("both %<unsigned%> and %<_Decimal64%> in "
10299 "declaration specifiers"));
10300 else if (specs->typespec_word == cts_dfloat128)
10301 error_at (loc,
10302 ("both %<unsigned%> and %<_Decimal128%> in "
10303 "declaration specifiers"));
10304 else
10306 specs->unsigned_p = true;
10307 specs->locations[cdw_unsigned] = loc;
10309 break;
10310 case RID_COMPLEX:
10311 dupe = specs->complex_p;
10312 if (!in_system_header_at (loc))
10313 pedwarn_c90 (loc, OPT_Wpedantic,
10314 "ISO C90 does not support complex types");
10315 if (specs->typespec_word == cts_auto_type)
10316 error_at (loc,
10317 ("both %<complex%> and %<__auto_type%> in "
10318 "declaration specifiers"));
10319 else if (specs->typespec_word == cts_void)
10320 error_at (loc,
10321 ("both %<complex%> and %<void%> in "
10322 "declaration specifiers"));
10323 else if (specs->typespec_word == cts_bool)
10324 error_at (loc,
10325 ("both %<complex%> and %<_Bool%> in "
10326 "declaration specifiers"));
10327 else if (specs->typespec_word == cts_dfloat32)
10328 error_at (loc,
10329 ("both %<complex%> and %<_Decimal32%> in "
10330 "declaration specifiers"));
10331 else if (specs->typespec_word == cts_dfloat64)
10332 error_at (loc,
10333 ("both %<complex%> and %<_Decimal64%> in "
10334 "declaration specifiers"));
10335 else if (specs->typespec_word == cts_dfloat128)
10336 error_at (loc,
10337 ("both %<complex%> and %<_Decimal128%> in "
10338 "declaration specifiers"));
10339 else if (specs->typespec_word == cts_fract)
10340 error_at (loc,
10341 ("both %<complex%> and %<_Fract%> in "
10342 "declaration specifiers"));
10343 else if (specs->typespec_word == cts_accum)
10344 error_at (loc,
10345 ("both %<complex%> and %<_Accum%> in "
10346 "declaration specifiers"));
10347 else if (specs->saturating_p)
10348 error_at (loc,
10349 ("both %<complex%> and %<_Sat%> in "
10350 "declaration specifiers"));
10351 else
10353 specs->complex_p = true;
10354 specs->locations[cdw_complex] = loc;
10356 break;
10357 case RID_SAT:
10358 dupe = specs->saturating_p;
10359 pedwarn (loc, OPT_Wpedantic,
10360 "ISO C does not support saturating types");
10361 if (specs->typespec_word == cts_int_n)
10363 error_at (loc,
10364 ("both %<_Sat%> and %<__int%d%> in "
10365 "declaration specifiers"),
10366 int_n_data[specs->int_n_idx].bitsize);
10368 else if (specs->typespec_word == cts_auto_type)
10369 error_at (loc,
10370 ("both %<_Sat%> and %<__auto_type%> in "
10371 "declaration specifiers"));
10372 else if (specs->typespec_word == cts_void)
10373 error_at (loc,
10374 ("both %<_Sat%> and %<void%> in "
10375 "declaration specifiers"));
10376 else if (specs->typespec_word == cts_bool)
10377 error_at (loc,
10378 ("both %<_Sat%> and %<_Bool%> in "
10379 "declaration specifiers"));
10380 else if (specs->typespec_word == cts_char)
10381 error_at (loc,
10382 ("both %<_Sat%> and %<char%> in "
10383 "declaration specifiers"));
10384 else if (specs->typespec_word == cts_int)
10385 error_at (loc,
10386 ("both %<_Sat%> and %<int%> in "
10387 "declaration specifiers"));
10388 else if (specs->typespec_word == cts_float)
10389 error_at (loc,
10390 ("both %<_Sat%> and %<float%> in "
10391 "declaration specifiers"));
10392 else if (specs->typespec_word == cts_double)
10393 error_at (loc,
10394 ("both %<_Sat%> and %<double%> in "
10395 "declaration specifiers"));
10396 else if (specs->typespec_word == cts_floatn_nx)
10397 error_at (loc,
10398 ("both %<_Sat%> and %<_Float%d%s%> in "
10399 "declaration specifiers"),
10400 floatn_nx_types[specs->floatn_nx_idx].n,
10401 (floatn_nx_types[specs->floatn_nx_idx].extended
10402 ? "x"
10403 : ""));
10404 else if (specs->typespec_word == cts_dfloat32)
10405 error_at (loc,
10406 ("both %<_Sat%> and %<_Decimal32%> in "
10407 "declaration specifiers"));
10408 else if (specs->typespec_word == cts_dfloat64)
10409 error_at (loc,
10410 ("both %<_Sat%> and %<_Decimal64%> in "
10411 "declaration specifiers"));
10412 else if (specs->typespec_word == cts_dfloat128)
10413 error_at (loc,
10414 ("both %<_Sat%> and %<_Decimal128%> in "
10415 "declaration specifiers"));
10416 else if (specs->complex_p)
10417 error_at (loc,
10418 ("both %<_Sat%> and %<complex%> in "
10419 "declaration specifiers"));
10420 else
10422 specs->saturating_p = true;
10423 specs->locations[cdw_saturating] = loc;
10425 break;
10426 default:
10427 gcc_unreachable ();
10430 if (dupe)
10431 error_at (loc, "duplicate %qE", type);
10433 return specs;
10435 else
10437 /* "void", "_Bool", "char", "int", "float", "double",
10438 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10439 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10440 "__auto_type". */
10441 if (specs->typespec_word != cts_none)
10443 error_at (loc,
10444 "two or more data types in declaration specifiers");
10445 return specs;
10447 switch (i)
10449 case RID_AUTO_TYPE:
10450 if (specs->long_p)
10451 error_at (loc,
10452 ("both %<long%> and %<__auto_type%> in "
10453 "declaration specifiers"));
10454 else if (specs->short_p)
10455 error_at (loc,
10456 ("both %<short%> and %<__auto_type%> in "
10457 "declaration specifiers"));
10458 else if (specs->signed_p)
10459 error_at (loc,
10460 ("both %<signed%> and %<__auto_type%> in "
10461 "declaration specifiers"));
10462 else if (specs->unsigned_p)
10463 error_at (loc,
10464 ("both %<unsigned%> and %<__auto_type%> in "
10465 "declaration specifiers"));
10466 else if (specs->complex_p)
10467 error_at (loc,
10468 ("both %<complex%> and %<__auto_type%> in "
10469 "declaration specifiers"));
10470 else if (specs->saturating_p)
10471 error_at (loc,
10472 ("both %<_Sat%> and %<__auto_type%> in "
10473 "declaration specifiers"));
10474 else
10476 specs->typespec_word = cts_auto_type;
10477 specs->locations[cdw_typespec] = loc;
10479 return specs;
10480 case RID_INT_N_0:
10481 case RID_INT_N_1:
10482 case RID_INT_N_2:
10483 case RID_INT_N_3:
10484 specs->int_n_idx = i - RID_INT_N_0;
10485 if (!in_system_header_at (input_location))
10486 pedwarn (loc, OPT_Wpedantic,
10487 "ISO C does not support %<__int%d%> types",
10488 int_n_data[specs->int_n_idx].bitsize);
10490 if (specs->long_p)
10491 error_at (loc,
10492 ("both %<__int%d%> and %<long%> in "
10493 "declaration specifiers"),
10494 int_n_data[specs->int_n_idx].bitsize);
10495 else if (specs->saturating_p)
10496 error_at (loc,
10497 ("both %<_Sat%> and %<__int%d%> in "
10498 "declaration specifiers"),
10499 int_n_data[specs->int_n_idx].bitsize);
10500 else if (specs->short_p)
10501 error_at (loc,
10502 ("both %<__int%d%> and %<short%> in "
10503 "declaration specifiers"),
10504 int_n_data[specs->int_n_idx].bitsize);
10505 else if (! int_n_enabled_p[specs->int_n_idx])
10507 specs->typespec_word = cts_int_n;
10508 error_at (loc,
10509 "%<__int%d%> is not supported on this target",
10510 int_n_data[specs->int_n_idx].bitsize);
10512 else
10514 specs->typespec_word = cts_int_n;
10515 specs->locations[cdw_typespec] = loc;
10517 return specs;
10518 case RID_VOID:
10519 if (specs->long_p)
10520 error_at (loc,
10521 ("both %<long%> and %<void%> in "
10522 "declaration specifiers"));
10523 else if (specs->short_p)
10524 error_at (loc,
10525 ("both %<short%> and %<void%> in "
10526 "declaration specifiers"));
10527 else if (specs->signed_p)
10528 error_at (loc,
10529 ("both %<signed%> and %<void%> in "
10530 "declaration specifiers"));
10531 else if (specs->unsigned_p)
10532 error_at (loc,
10533 ("both %<unsigned%> and %<void%> in "
10534 "declaration specifiers"));
10535 else if (specs->complex_p)
10536 error_at (loc,
10537 ("both %<complex%> and %<void%> in "
10538 "declaration specifiers"));
10539 else if (specs->saturating_p)
10540 error_at (loc,
10541 ("both %<_Sat%> and %<void%> in "
10542 "declaration specifiers"));
10543 else
10545 specs->typespec_word = cts_void;
10546 specs->locations[cdw_typespec] = loc;
10548 return specs;
10549 case RID_BOOL:
10550 if (!in_system_header_at (loc))
10551 pedwarn_c90 (loc, OPT_Wpedantic,
10552 "ISO C90 does not support boolean types");
10553 if (specs->long_p)
10554 error_at (loc,
10555 ("both %<long%> and %<_Bool%> in "
10556 "declaration specifiers"));
10557 else if (specs->short_p)
10558 error_at (loc,
10559 ("both %<short%> and %<_Bool%> in "
10560 "declaration specifiers"));
10561 else if (specs->signed_p)
10562 error_at (loc,
10563 ("both %<signed%> and %<_Bool%> in "
10564 "declaration specifiers"));
10565 else if (specs->unsigned_p)
10566 error_at (loc,
10567 ("both %<unsigned%> and %<_Bool%> in "
10568 "declaration specifiers"));
10569 else if (specs->complex_p)
10570 error_at (loc,
10571 ("both %<complex%> and %<_Bool%> in "
10572 "declaration specifiers"));
10573 else if (specs->saturating_p)
10574 error_at (loc,
10575 ("both %<_Sat%> and %<_Bool%> in "
10576 "declaration specifiers"));
10577 else
10579 specs->typespec_word = cts_bool;
10580 specs->locations[cdw_typespec] = loc;
10582 return specs;
10583 case RID_CHAR:
10584 if (specs->long_p)
10585 error_at (loc,
10586 ("both %<long%> and %<char%> in "
10587 "declaration specifiers"));
10588 else if (specs->short_p)
10589 error_at (loc,
10590 ("both %<short%> and %<char%> in "
10591 "declaration specifiers"));
10592 else if (specs->saturating_p)
10593 error_at (loc,
10594 ("both %<_Sat%> and %<char%> in "
10595 "declaration specifiers"));
10596 else
10598 specs->typespec_word = cts_char;
10599 specs->locations[cdw_typespec] = loc;
10601 return specs;
10602 case RID_INT:
10603 if (specs->saturating_p)
10604 error_at (loc,
10605 ("both %<_Sat%> and %<int%> in "
10606 "declaration specifiers"));
10607 else
10609 specs->typespec_word = cts_int;
10610 specs->locations[cdw_typespec] = loc;
10612 return specs;
10613 case RID_FLOAT:
10614 if (specs->long_p)
10615 error_at (loc,
10616 ("both %<long%> and %<float%> in "
10617 "declaration specifiers"));
10618 else if (specs->short_p)
10619 error_at (loc,
10620 ("both %<short%> and %<float%> in "
10621 "declaration specifiers"));
10622 else if (specs->signed_p)
10623 error_at (loc,
10624 ("both %<signed%> and %<float%> in "
10625 "declaration specifiers"));
10626 else if (specs->unsigned_p)
10627 error_at (loc,
10628 ("both %<unsigned%> and %<float%> in "
10629 "declaration specifiers"));
10630 else if (specs->saturating_p)
10631 error_at (loc,
10632 ("both %<_Sat%> and %<float%> in "
10633 "declaration specifiers"));
10634 else
10636 specs->typespec_word = cts_float;
10637 specs->locations[cdw_typespec] = loc;
10639 return specs;
10640 case RID_DOUBLE:
10641 if (specs->long_long_p)
10642 error_at (loc,
10643 ("both %<long long%> and %<double%> in "
10644 "declaration specifiers"));
10645 else if (specs->short_p)
10646 error_at (loc,
10647 ("both %<short%> and %<double%> in "
10648 "declaration specifiers"));
10649 else if (specs->signed_p)
10650 error_at (loc,
10651 ("both %<signed%> and %<double%> in "
10652 "declaration specifiers"));
10653 else if (specs->unsigned_p)
10654 error_at (loc,
10655 ("both %<unsigned%> and %<double%> in "
10656 "declaration specifiers"));
10657 else if (specs->saturating_p)
10658 error_at (loc,
10659 ("both %<_Sat%> and %<double%> in "
10660 "declaration specifiers"));
10661 else
10663 specs->typespec_word = cts_double;
10664 specs->locations[cdw_typespec] = loc;
10666 return specs;
10667 CASE_RID_FLOATN_NX:
10668 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10669 if (!in_system_header_at (input_location))
10670 pedwarn (loc, OPT_Wpedantic,
10671 "ISO C does not support the %<_Float%d%s%> type",
10672 floatn_nx_types[specs->floatn_nx_idx].n,
10673 (floatn_nx_types[specs->floatn_nx_idx].extended
10674 ? "x"
10675 : ""));
10677 if (specs->long_p)
10678 error_at (loc,
10679 ("both %<long%> and %<_Float%d%s%> in "
10680 "declaration specifiers"),
10681 floatn_nx_types[specs->floatn_nx_idx].n,
10682 (floatn_nx_types[specs->floatn_nx_idx].extended
10683 ? "x"
10684 : ""));
10685 else if (specs->short_p)
10686 error_at (loc,
10687 ("both %<short%> and %<_Float%d%s%> in "
10688 "declaration specifiers"),
10689 floatn_nx_types[specs->floatn_nx_idx].n,
10690 (floatn_nx_types[specs->floatn_nx_idx].extended
10691 ? "x"
10692 : ""));
10693 else if (specs->signed_p)
10694 error_at (loc,
10695 ("both %<signed%> and %<_Float%d%s%> in "
10696 "declaration specifiers"),
10697 floatn_nx_types[specs->floatn_nx_idx].n,
10698 (floatn_nx_types[specs->floatn_nx_idx].extended
10699 ? "x"
10700 : ""));
10701 else if (specs->unsigned_p)
10702 error_at (loc,
10703 ("both %<unsigned%> and %<_Float%d%s%> in "
10704 "declaration specifiers"),
10705 floatn_nx_types[specs->floatn_nx_idx].n,
10706 (floatn_nx_types[specs->floatn_nx_idx].extended
10707 ? "x"
10708 : ""));
10709 else if (specs->saturating_p)
10710 error_at (loc,
10711 ("both %<_Sat%> and %<_Float%d%s%> in "
10712 "declaration specifiers"),
10713 floatn_nx_types[specs->floatn_nx_idx].n,
10714 (floatn_nx_types[specs->floatn_nx_idx].extended
10715 ? "x"
10716 : ""));
10717 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10719 specs->typespec_word = cts_floatn_nx;
10720 error_at (loc,
10721 "%<_Float%d%s%> is not supported on this target",
10722 floatn_nx_types[specs->floatn_nx_idx].n,
10723 (floatn_nx_types[specs->floatn_nx_idx].extended
10724 ? "x"
10725 : ""));
10727 else
10729 specs->typespec_word = cts_floatn_nx;
10730 specs->locations[cdw_typespec] = loc;
10732 return specs;
10733 case RID_DFLOAT32:
10734 case RID_DFLOAT64:
10735 case RID_DFLOAT128:
10737 const char *str;
10738 if (i == RID_DFLOAT32)
10739 str = "_Decimal32";
10740 else if (i == RID_DFLOAT64)
10741 str = "_Decimal64";
10742 else
10743 str = "_Decimal128";
10744 if (specs->long_long_p)
10745 error_at (loc,
10746 ("both %<long long%> and %qs in "
10747 "declaration specifiers"),
10748 str);
10749 if (specs->long_p)
10750 error_at (loc,
10751 ("both %<long%> and %qs in "
10752 "declaration specifiers"),
10753 str);
10754 else if (specs->short_p)
10755 error_at (loc,
10756 ("both %<short%> and %qs in "
10757 "declaration specifiers"),
10758 str);
10759 else if (specs->signed_p)
10760 error_at (loc,
10761 ("both %<signed%> and %qs in "
10762 "declaration specifiers"),
10763 str);
10764 else if (specs->unsigned_p)
10765 error_at (loc,
10766 ("both %<unsigned%> and %qs in "
10767 "declaration specifiers"),
10768 str);
10769 else if (specs->complex_p)
10770 error_at (loc,
10771 ("both %<complex%> and %qs in "
10772 "declaration specifiers"),
10773 str);
10774 else if (specs->saturating_p)
10775 error_at (loc,
10776 ("both %<_Sat%> and %qs in "
10777 "declaration specifiers"),
10778 str);
10779 else if (i == RID_DFLOAT32)
10780 specs->typespec_word = cts_dfloat32;
10781 else if (i == RID_DFLOAT64)
10782 specs->typespec_word = cts_dfloat64;
10783 else
10784 specs->typespec_word = cts_dfloat128;
10785 specs->locations[cdw_typespec] = loc;
10787 if (!targetm.decimal_float_supported_p ())
10788 error_at (loc,
10789 ("decimal floating point not supported "
10790 "for this target"));
10791 pedwarn (loc, OPT_Wpedantic,
10792 "ISO C does not support decimal floating point");
10793 return specs;
10794 case RID_FRACT:
10795 case RID_ACCUM:
10797 const char *str;
10798 if (i == RID_FRACT)
10799 str = "_Fract";
10800 else
10801 str = "_Accum";
10802 if (specs->complex_p)
10803 error_at (loc,
10804 ("both %<complex%> and %qs in "
10805 "declaration specifiers"),
10806 str);
10807 else if (i == RID_FRACT)
10808 specs->typespec_word = cts_fract;
10809 else
10810 specs->typespec_word = cts_accum;
10811 specs->locations[cdw_typespec] = loc;
10813 if (!targetm.fixed_point_supported_p ())
10814 error_at (loc,
10815 "fixed-point types not supported for this target");
10816 pedwarn (loc, OPT_Wpedantic,
10817 "ISO C does not support fixed-point types");
10818 return specs;
10819 default:
10820 /* ObjC reserved word "id", handled below. */
10821 break;
10826 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10827 form of ObjC type, cases such as "int" and "long" being handled
10828 above), a TYPE (struct, union, enum and typeof specifiers) or an
10829 ERROR_MARK. In none of these cases may there have previously
10830 been any type specifiers. */
10831 if (specs->type || specs->typespec_word != cts_none
10832 || specs->long_p || specs->short_p || specs->signed_p
10833 || specs->unsigned_p || specs->complex_p)
10834 error_at (loc, "two or more data types in declaration specifiers");
10835 else if (TREE_CODE (type) == TYPE_DECL)
10837 if (TREE_TYPE (type) == error_mark_node)
10838 ; /* Allow the type to default to int to avoid cascading errors. */
10839 else
10841 specs->type = TREE_TYPE (type);
10842 specs->decl_attr = DECL_ATTRIBUTES (type);
10843 specs->typedef_p = true;
10844 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10845 specs->locations[cdw_typedef] = loc;
10847 /* If this typedef name is defined in a struct, then a C++
10848 lookup would return a different value. */
10849 if (warn_cxx_compat
10850 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10851 warning_at (loc, OPT_Wc___compat,
10852 "C++ lookup of %qD would return a field, not a type",
10853 type);
10855 /* If we are parsing a struct, record that a struct field
10856 used a typedef. */
10857 if (warn_cxx_compat && struct_parse_info != NULL)
10858 struct_parse_info->typedefs_seen.safe_push (type);
10861 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10863 tree t = lookup_name (type);
10864 if (!t || TREE_CODE (t) != TYPE_DECL)
10865 error_at (loc, "%qE fails to be a typedef or built in type", type);
10866 else if (TREE_TYPE (t) == error_mark_node)
10868 else
10870 specs->type = TREE_TYPE (t);
10871 specs->locations[cdw_typespec] = loc;
10874 else
10876 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10878 specs->typedef_p = true;
10879 specs->locations[cdw_typedef] = loc;
10880 if (spec.expr)
10882 if (specs->expr)
10883 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10884 specs->expr, spec.expr);
10885 else
10886 specs->expr = spec.expr;
10887 specs->expr_const_operands &= spec.expr_const_operands;
10890 specs->type = type;
10893 return specs;
10896 /* Add the storage class specifier or function specifier SCSPEC to the
10897 declaration specifiers SPECS, returning SPECS. */
10899 struct c_declspecs *
10900 declspecs_add_scspec (source_location loc,
10901 struct c_declspecs *specs,
10902 tree scspec)
10904 enum rid i;
10905 enum c_storage_class n = csc_none;
10906 bool dupe = false;
10907 specs->declspecs_seen_p = true;
10908 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10909 && C_IS_RESERVED_WORD (scspec));
10910 i = C_RID_CODE (scspec);
10911 if (specs->non_sc_seen_p)
10912 warning (OPT_Wold_style_declaration,
10913 "%qE is not at beginning of declaration", scspec);
10914 switch (i)
10916 case RID_INLINE:
10917 /* C99 permits duplicate inline. Although of doubtful utility,
10918 it seems simplest to permit it in gnu89 mode as well, as
10919 there is also little utility in maintaining this as a
10920 difference between gnu89 and C99 inline. */
10921 dupe = false;
10922 specs->inline_p = true;
10923 specs->locations[cdw_inline] = loc;
10924 break;
10925 case RID_NORETURN:
10926 /* Duplicate _Noreturn is permitted. */
10927 dupe = false;
10928 specs->noreturn_p = true;
10929 specs->locations[cdw_noreturn] = loc;
10930 break;
10931 case RID_THREAD:
10932 dupe = specs->thread_p;
10933 if (specs->storage_class == csc_auto)
10934 error ("%qE used with %<auto%>", scspec);
10935 else if (specs->storage_class == csc_register)
10936 error ("%qE used with %<register%>", scspec);
10937 else if (specs->storage_class == csc_typedef)
10938 error ("%qE used with %<typedef%>", scspec);
10939 else
10941 specs->thread_p = true;
10942 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10943 "__thread") == 0);
10944 /* A diagnostic is not required for the use of this
10945 identifier in the implementation namespace; only diagnose
10946 it for the C11 spelling because of existing code using
10947 the other spelling. */
10948 if (!specs->thread_gnu_p)
10950 if (flag_isoc99)
10951 pedwarn_c99 (loc, OPT_Wpedantic,
10952 "ISO C99 does not support %qE", scspec);
10953 else
10954 pedwarn_c99 (loc, OPT_Wpedantic,
10955 "ISO C90 does not support %qE", scspec);
10957 specs->locations[cdw_thread] = loc;
10959 break;
10960 case RID_AUTO:
10961 n = csc_auto;
10962 break;
10963 case RID_EXTERN:
10964 n = csc_extern;
10965 /* Diagnose "__thread extern". */
10966 if (specs->thread_p && specs->thread_gnu_p)
10967 error ("%<__thread%> before %<extern%>");
10968 break;
10969 case RID_REGISTER:
10970 n = csc_register;
10971 break;
10972 case RID_STATIC:
10973 n = csc_static;
10974 /* Diagnose "__thread static". */
10975 if (specs->thread_p && specs->thread_gnu_p)
10976 error ("%<__thread%> before %<static%>");
10977 break;
10978 case RID_TYPEDEF:
10979 n = csc_typedef;
10980 break;
10981 default:
10982 gcc_unreachable ();
10984 if (n != csc_none && n == specs->storage_class)
10985 dupe = true;
10986 if (dupe)
10988 if (i == RID_THREAD)
10989 error ("duplicate %<_Thread_local%> or %<__thread%>");
10990 else
10991 error ("duplicate %qE", scspec);
10993 if (n != csc_none)
10995 if (specs->storage_class != csc_none && n != specs->storage_class)
10997 error ("multiple storage classes in declaration specifiers");
10999 else
11001 specs->storage_class = n;
11002 specs->locations[cdw_storage_class] = loc;
11003 if (n != csc_extern && n != csc_static && specs->thread_p)
11005 error ("%qs used with %qE",
11006 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11007 scspec);
11008 specs->thread_p = false;
11012 return specs;
11015 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11016 returning SPECS. */
11018 struct c_declspecs *
11019 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
11021 specs->attrs = chainon (attrs, specs->attrs);
11022 specs->locations[cdw_attributes] = loc;
11023 specs->declspecs_seen_p = true;
11024 return specs;
11027 /* Add an _Alignas specifier (expression ALIGN, or type whose
11028 alignment is ALIGN) to the declaration specifiers SPECS, returning
11029 SPECS. */
11030 struct c_declspecs *
11031 declspecs_add_alignas (source_location loc,
11032 struct c_declspecs *specs, tree align)
11034 int align_log;
11035 specs->alignas_p = true;
11036 specs->locations[cdw_alignas] = loc;
11037 if (align == error_mark_node)
11038 return specs;
11039 align_log = check_user_alignment (align, true);
11040 if (align_log > specs->align_log)
11041 specs->align_log = align_log;
11042 return specs;
11045 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11046 specifiers with any other type specifier to determine the resulting
11047 type. This is where ISO C checks on complex types are made, since
11048 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11049 double". */
11051 struct c_declspecs *
11052 finish_declspecs (struct c_declspecs *specs)
11054 /* If a type was specified as a whole, we have no modifiers and are
11055 done. */
11056 if (specs->type != NULL_TREE)
11058 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11059 && !specs->signed_p && !specs->unsigned_p
11060 && !specs->complex_p);
11062 /* Set a dummy type. */
11063 if (TREE_CODE (specs->type) == ERROR_MARK)
11064 specs->type = integer_type_node;
11065 return specs;
11068 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11069 has been specified, treat it as "int" unless "_Complex" is
11070 present and there are no other specifiers. If we just have
11071 "_Complex", it is equivalent to "_Complex double", but e.g.
11072 "_Complex short" is equivalent to "_Complex short int". */
11073 if (specs->typespec_word == cts_none)
11075 if (specs->saturating_p)
11077 error_at (specs->locations[cdw_saturating],
11078 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11079 if (!targetm.fixed_point_supported_p ())
11080 error_at (specs->locations[cdw_saturating],
11081 "fixed-point types not supported for this target");
11082 specs->typespec_word = cts_fract;
11084 else if (specs->long_p || specs->short_p
11085 || specs->signed_p || specs->unsigned_p)
11087 specs->typespec_word = cts_int;
11089 else if (specs->complex_p)
11091 specs->typespec_word = cts_double;
11092 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11093 "ISO C does not support plain %<complex%> meaning "
11094 "%<double complex%>");
11096 else
11098 specs->typespec_word = cts_int;
11099 specs->default_int_p = true;
11100 /* We don't diagnose this here because grokdeclarator will
11101 give more specific diagnostics according to whether it is
11102 a function definition. */
11106 /* If "signed" was specified, record this to distinguish "int" and
11107 "signed int" in the case of a bit-field with
11108 -funsigned-bitfields. */
11109 specs->explicit_signed_p = specs->signed_p;
11111 /* Now compute the actual type. */
11112 switch (specs->typespec_word)
11114 case cts_auto_type:
11115 gcc_assert (!specs->long_p && !specs->short_p
11116 && !specs->signed_p && !specs->unsigned_p
11117 && !specs->complex_p);
11118 /* Type to be filled in later. */
11119 break;
11120 case cts_void:
11121 gcc_assert (!specs->long_p && !specs->short_p
11122 && !specs->signed_p && !specs->unsigned_p
11123 && !specs->complex_p);
11124 specs->type = void_type_node;
11125 break;
11126 case cts_bool:
11127 gcc_assert (!specs->long_p && !specs->short_p
11128 && !specs->signed_p && !specs->unsigned_p
11129 && !specs->complex_p);
11130 specs->type = boolean_type_node;
11131 break;
11132 case cts_char:
11133 gcc_assert (!specs->long_p && !specs->short_p);
11134 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11135 if (specs->signed_p)
11136 specs->type = signed_char_type_node;
11137 else if (specs->unsigned_p)
11138 specs->type = unsigned_char_type_node;
11139 else
11140 specs->type = char_type_node;
11141 if (specs->complex_p)
11143 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11144 "ISO C does not support complex integer types");
11145 specs->type = build_complex_type (specs->type);
11147 break;
11148 case cts_int_n:
11149 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11150 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11151 if (! int_n_enabled_p[specs->int_n_idx])
11152 specs->type = integer_type_node;
11153 else
11154 specs->type = (specs->unsigned_p
11155 ? int_n_trees[specs->int_n_idx].unsigned_type
11156 : int_n_trees[specs->int_n_idx].signed_type);
11157 if (specs->complex_p)
11159 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11160 "ISO C does not support complex integer types");
11161 specs->type = build_complex_type (specs->type);
11163 break;
11164 case cts_int:
11165 gcc_assert (!(specs->long_p && specs->short_p));
11166 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11167 if (specs->long_long_p)
11168 specs->type = (specs->unsigned_p
11169 ? long_long_unsigned_type_node
11170 : long_long_integer_type_node);
11171 else if (specs->long_p)
11172 specs->type = (specs->unsigned_p
11173 ? long_unsigned_type_node
11174 : long_integer_type_node);
11175 else if (specs->short_p)
11176 specs->type = (specs->unsigned_p
11177 ? short_unsigned_type_node
11178 : short_integer_type_node);
11179 else
11180 specs->type = (specs->unsigned_p
11181 ? unsigned_type_node
11182 : integer_type_node);
11183 if (specs->complex_p)
11185 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11186 "ISO C does not support complex integer types");
11187 specs->type = build_complex_type (specs->type);
11189 break;
11190 case cts_float:
11191 gcc_assert (!specs->long_p && !specs->short_p
11192 && !specs->signed_p && !specs->unsigned_p);
11193 specs->type = (specs->complex_p
11194 ? complex_float_type_node
11195 : float_type_node);
11196 break;
11197 case cts_double:
11198 gcc_assert (!specs->long_long_p && !specs->short_p
11199 && !specs->signed_p && !specs->unsigned_p);
11200 if (specs->long_p)
11202 specs->type = (specs->complex_p
11203 ? complex_long_double_type_node
11204 : long_double_type_node);
11206 else
11208 specs->type = (specs->complex_p
11209 ? complex_double_type_node
11210 : double_type_node);
11212 break;
11213 case cts_floatn_nx:
11214 gcc_assert (!specs->long_p && !specs->short_p
11215 && !specs->signed_p && !specs->unsigned_p);
11216 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11217 specs->type = integer_type_node;
11218 else if (specs->complex_p)
11219 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11220 else
11221 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11222 break;
11223 case cts_dfloat32:
11224 case cts_dfloat64:
11225 case cts_dfloat128:
11226 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11227 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11228 if (specs->typespec_word == cts_dfloat32)
11229 specs->type = dfloat32_type_node;
11230 else if (specs->typespec_word == cts_dfloat64)
11231 specs->type = dfloat64_type_node;
11232 else
11233 specs->type = dfloat128_type_node;
11234 break;
11235 case cts_fract:
11236 gcc_assert (!specs->complex_p);
11237 if (!targetm.fixed_point_supported_p ())
11238 specs->type = integer_type_node;
11239 else if (specs->saturating_p)
11241 if (specs->long_long_p)
11242 specs->type = specs->unsigned_p
11243 ? sat_unsigned_long_long_fract_type_node
11244 : sat_long_long_fract_type_node;
11245 else if (specs->long_p)
11246 specs->type = specs->unsigned_p
11247 ? sat_unsigned_long_fract_type_node
11248 : sat_long_fract_type_node;
11249 else if (specs->short_p)
11250 specs->type = specs->unsigned_p
11251 ? sat_unsigned_short_fract_type_node
11252 : sat_short_fract_type_node;
11253 else
11254 specs->type = specs->unsigned_p
11255 ? sat_unsigned_fract_type_node
11256 : sat_fract_type_node;
11258 else
11260 if (specs->long_long_p)
11261 specs->type = specs->unsigned_p
11262 ? unsigned_long_long_fract_type_node
11263 : long_long_fract_type_node;
11264 else if (specs->long_p)
11265 specs->type = specs->unsigned_p
11266 ? unsigned_long_fract_type_node
11267 : long_fract_type_node;
11268 else if (specs->short_p)
11269 specs->type = specs->unsigned_p
11270 ? unsigned_short_fract_type_node
11271 : short_fract_type_node;
11272 else
11273 specs->type = specs->unsigned_p
11274 ? unsigned_fract_type_node
11275 : fract_type_node;
11277 break;
11278 case cts_accum:
11279 gcc_assert (!specs->complex_p);
11280 if (!targetm.fixed_point_supported_p ())
11281 specs->type = integer_type_node;
11282 else if (specs->saturating_p)
11284 if (specs->long_long_p)
11285 specs->type = specs->unsigned_p
11286 ? sat_unsigned_long_long_accum_type_node
11287 : sat_long_long_accum_type_node;
11288 else if (specs->long_p)
11289 specs->type = specs->unsigned_p
11290 ? sat_unsigned_long_accum_type_node
11291 : sat_long_accum_type_node;
11292 else if (specs->short_p)
11293 specs->type = specs->unsigned_p
11294 ? sat_unsigned_short_accum_type_node
11295 : sat_short_accum_type_node;
11296 else
11297 specs->type = specs->unsigned_p
11298 ? sat_unsigned_accum_type_node
11299 : sat_accum_type_node;
11301 else
11303 if (specs->long_long_p)
11304 specs->type = specs->unsigned_p
11305 ? unsigned_long_long_accum_type_node
11306 : long_long_accum_type_node;
11307 else if (specs->long_p)
11308 specs->type = specs->unsigned_p
11309 ? unsigned_long_accum_type_node
11310 : long_accum_type_node;
11311 else if (specs->short_p)
11312 specs->type = specs->unsigned_p
11313 ? unsigned_short_accum_type_node
11314 : short_accum_type_node;
11315 else
11316 specs->type = specs->unsigned_p
11317 ? unsigned_accum_type_node
11318 : accum_type_node;
11320 break;
11321 default:
11322 gcc_unreachable ();
11325 return specs;
11328 /* Perform final processing on one file scope's declarations (or the
11329 external scope's declarations), GLOBALS. */
11331 static void
11332 c_write_global_declarations_1 (tree globals)
11334 tree decl;
11335 bool reconsider;
11337 /* Process the decls in the order they were written. */
11338 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11340 /* Check for used but undefined static functions using the C
11341 standard's definition of "used", and set TREE_NO_WARNING so
11342 that check_global_declaration doesn't repeat the check. */
11343 if (TREE_CODE (decl) == FUNCTION_DECL
11344 && DECL_INITIAL (decl) == NULL_TREE
11345 && DECL_EXTERNAL (decl)
11346 && !TREE_PUBLIC (decl))
11348 if (C_DECL_USED (decl))
11350 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11351 TREE_NO_WARNING (decl) = 1;
11353 /* For -Wunused-function warn about unused static prototypes. */
11354 else if (warn_unused_function
11355 && ! DECL_ARTIFICIAL (decl)
11356 && ! TREE_NO_WARNING (decl))
11358 warning (OPT_Wunused_function,
11359 "%q+F declared %<static%> but never defined", decl);
11360 TREE_NO_WARNING (decl) = 1;
11364 wrapup_global_declaration_1 (decl);
11369 reconsider = false;
11370 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11371 reconsider |= wrapup_global_declaration_2 (decl);
11373 while (reconsider);
11376 /* Callback to collect a source_ref from a DECL. */
11378 static void
11379 collect_source_ref_cb (tree decl)
11381 if (!DECL_IS_BUILTIN (decl))
11382 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11385 /* Preserve the external declarations scope across a garbage collect. */
11386 static GTY(()) tree ext_block;
11388 /* Collect all references relevant to SOURCE_FILE. */
11390 static void
11391 collect_all_refs (const char *source_file)
11393 tree t;
11394 unsigned i;
11396 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11397 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11399 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11402 /* Iterate over all global declarations and call CALLBACK. */
11404 static void
11405 for_each_global_decl (void (*callback) (tree decl))
11407 tree t;
11408 tree decls;
11409 tree decl;
11410 unsigned i;
11412 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11414 decls = DECL_INITIAL (t);
11415 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11416 callback (decl);
11419 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11420 callback (decl);
11423 /* Perform any final parser cleanups and generate initial debugging
11424 information. */
11426 void
11427 c_parse_final_cleanups (void)
11429 tree t;
11430 unsigned i;
11432 /* We don't want to do this if generating a PCH. */
11433 if (pch_file)
11434 return;
11436 timevar_stop (TV_PHASE_PARSING);
11437 timevar_start (TV_PHASE_DEFERRED);
11439 /* Do the Objective-C stuff. This is where all the Objective-C
11440 module stuff gets generated (symtab, class/protocol/selector
11441 lists etc). */
11442 if (c_dialect_objc ())
11443 objc_write_global_declarations ();
11445 /* Close the external scope. */
11446 ext_block = pop_scope ();
11447 external_scope = 0;
11448 gcc_assert (!current_scope);
11450 /* Handle -fdump-ada-spec[-slim]. */
11451 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11453 /* Build a table of files to generate specs for */
11454 if (flag_dump_ada_spec_slim)
11455 collect_source_ref (main_input_filename);
11456 else
11457 for_each_global_decl (collect_source_ref_cb);
11459 dump_ada_specs (collect_all_refs, NULL);
11462 /* Process all file scopes in this compilation, and the external_scope,
11463 through wrapup_global_declarations. */
11464 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11465 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11466 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11468 timevar_stop (TV_PHASE_DEFERRED);
11469 timevar_start (TV_PHASE_PARSING);
11471 ext_block = NULL;
11474 /* Register reserved keyword WORD as qualifier for address space AS. */
11476 void
11477 c_register_addr_space (const char *word, addr_space_t as)
11479 int rid = RID_FIRST_ADDR_SPACE + as;
11480 tree id;
11482 /* Address space qualifiers are only supported
11483 in C with GNU extensions enabled. */
11484 if (c_dialect_objc () || flag_no_asm)
11485 return;
11487 id = get_identifier (word);
11488 C_SET_RID_CODE (id, rid);
11489 C_IS_RESERVED_WORD (id) = 1;
11490 ridpointers [rid] = id;
11493 /* Return identifier to look up for omp declare reduction. */
11495 tree
11496 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11498 const char *p = NULL;
11499 switch (reduction_code)
11501 case PLUS_EXPR: p = "+"; break;
11502 case MULT_EXPR: p = "*"; break;
11503 case MINUS_EXPR: p = "-"; break;
11504 case BIT_AND_EXPR: p = "&"; break;
11505 case BIT_XOR_EXPR: p = "^"; break;
11506 case BIT_IOR_EXPR: p = "|"; break;
11507 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11508 case TRUTH_ORIF_EXPR: p = "||"; break;
11509 case MIN_EXPR: p = "min"; break;
11510 case MAX_EXPR: p = "max"; break;
11511 default:
11512 break;
11515 if (p == NULL)
11517 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11518 return error_mark_node;
11519 p = IDENTIFIER_POINTER (reduction_id);
11522 const char prefix[] = "omp declare reduction ";
11523 size_t lenp = sizeof (prefix);
11524 size_t len = strlen (p);
11525 char *name = XALLOCAVEC (char, lenp + len);
11526 memcpy (name, prefix, lenp - 1);
11527 memcpy (name + lenp - 1, p, len + 1);
11528 return get_identifier (name);
11531 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11532 VAR_DECL, bind it into the current scope and return it. */
11534 tree
11535 c_omp_reduction_decl (tree reduction_id)
11537 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11538 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11539 return b->decl;
11541 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11542 reduction_id, integer_type_node);
11543 DECL_ARTIFICIAL (decl) = 1;
11544 DECL_EXTERNAL (decl) = 1;
11545 TREE_STATIC (decl) = 1;
11546 TREE_PUBLIC (decl) = 0;
11547 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11548 return decl;
11551 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11553 tree
11554 c_omp_reduction_lookup (tree reduction_id, tree type)
11556 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11557 while (b)
11559 tree t;
11560 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11561 if (comptypes (TREE_PURPOSE (t), type))
11562 return TREE_VALUE (t);
11563 b = b->shadowed;
11565 return error_mark_node;
11568 /* Helper function called via walk_tree, to diagnose invalid
11569 #pragma omp declare reduction combiners or initializers. */
11571 tree
11572 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11574 tree *vars = (tree *) data;
11575 if (SSA_VAR_P (*tp)
11576 && !DECL_ARTIFICIAL (*tp)
11577 && *tp != vars[0]
11578 && *tp != vars[1])
11580 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11581 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11582 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11583 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11584 *tp);
11585 else
11586 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11587 "to variable %qD which is not %<omp_priv%> nor "
11588 "%<omp_orig%>",
11589 *tp);
11590 return *tp;
11592 return NULL_TREE;
11595 #include "gt-c-c-decl.h"