CWG 616, 1213 - value category of subobject references.
[official-gcc.git] / gcc / c / c-decl.c
blob3c4b18edf562b18a108e48df20d3bec0713377ee
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 if (warning_at (loc, OPT_Wc___compat,
1575 ("using %qD as both a typedef and a tag is "
1576 "invalid in C++"), b->decl)
1577 && b->locus != UNKNOWN_LOCATION)
1578 inform (b->locus, "originally defined here");
1583 /* An exported interface to pushtag. This is used by the gdb plugin's
1584 binding oracle to introduce a new tag binding. */
1586 void
1587 c_pushtag (location_t loc, tree name, tree type)
1589 pushtag (loc, name, type);
1592 /* An exported interface to bind a declaration. LOC is the location
1593 to use. DECL is the declaration to bind. The decl's name is used
1594 to determine how it is bound. If DECL is a VAR_DECL, then
1595 IS_GLOBAL determines whether the decl is put into the global (file
1596 and external) scope or the current function's scope; if DECL is not
1597 a VAR_DECL then it is always put into the file scope. */
1599 void
1600 c_bind (location_t loc, tree decl, bool is_global)
1602 struct c_scope *scope;
1603 bool nested = false;
1605 if (!VAR_P (decl) || current_function_scope == NULL)
1607 /* Types and functions are always considered to be global. */
1608 scope = file_scope;
1609 DECL_EXTERNAL (decl) = 1;
1610 TREE_PUBLIC (decl) = 1;
1612 else if (is_global)
1614 /* Also bind it into the external scope. */
1615 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1616 nested = true;
1617 scope = file_scope;
1618 DECL_EXTERNAL (decl) = 1;
1619 TREE_PUBLIC (decl) = 1;
1621 else
1623 DECL_CONTEXT (decl) = current_function_decl;
1624 TREE_PUBLIC (decl) = 0;
1625 scope = current_function_scope;
1628 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1631 /* Subroutine of compare_decls. Allow harmless mismatches in return
1632 and argument types provided that the type modes match. This function
1633 return a unified type given a suitable match, and 0 otherwise. */
1635 static tree
1636 match_builtin_function_types (tree newtype, tree oldtype)
1638 tree newrettype, oldrettype;
1639 tree newargs, oldargs;
1640 tree trytype, tryargs;
1642 /* Accept the return type of the new declaration if same modes. */
1643 oldrettype = TREE_TYPE (oldtype);
1644 newrettype = TREE_TYPE (newtype);
1646 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1647 return NULL_TREE;
1649 oldargs = TYPE_ARG_TYPES (oldtype);
1650 newargs = TYPE_ARG_TYPES (newtype);
1651 tryargs = newargs;
1653 while (oldargs || newargs)
1655 if (!oldargs
1656 || !newargs
1657 || !TREE_VALUE (oldargs)
1658 || !TREE_VALUE (newargs)
1659 || TYPE_MODE (TREE_VALUE (oldargs))
1660 != TYPE_MODE (TREE_VALUE (newargs)))
1661 return NULL_TREE;
1663 oldargs = TREE_CHAIN (oldargs);
1664 newargs = TREE_CHAIN (newargs);
1667 trytype = build_function_type (newrettype, tryargs);
1669 /* Allow declaration to change transaction_safe attribute. */
1670 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1671 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1672 tree newattrs = TYPE_ATTRIBUTES (newtype);
1673 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1674 if (oldtsafe && !newtsafe)
1675 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1676 else if (newtsafe && !oldtsafe)
1677 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1678 NULL_TREE, oldattrs);
1680 return build_type_attribute_variant (trytype, oldattrs);
1683 /* Subroutine of diagnose_mismatched_decls. Check for function type
1684 mismatch involving an empty arglist vs a nonempty one and give clearer
1685 diagnostics. */
1686 static void
1687 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1688 tree newtype, tree oldtype)
1690 tree t;
1692 if (TREE_CODE (olddecl) != FUNCTION_DECL
1693 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1694 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1695 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1696 return;
1698 t = TYPE_ARG_TYPES (oldtype);
1699 if (t == NULL_TREE)
1700 t = TYPE_ARG_TYPES (newtype);
1701 for (; t; t = TREE_CHAIN (t))
1703 tree type = TREE_VALUE (t);
1705 if (TREE_CHAIN (t) == NULL_TREE
1706 && TYPE_MAIN_VARIANT (type) != void_type_node)
1708 inform (input_location, "a parameter list with an ellipsis can%'t match "
1709 "an empty parameter name list declaration");
1710 break;
1713 if (c_type_promotes_to (type) != type)
1715 inform (input_location, "an argument type that has a default promotion can%'t match "
1716 "an empty parameter name list declaration");
1717 break;
1722 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1723 old-style function definition, NEWDECL is a prototype declaration.
1724 Diagnose inconsistencies in the argument list. Returns TRUE if
1725 the prototype is compatible, FALSE if not. */
1726 static bool
1727 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1729 tree newargs, oldargs;
1730 int i;
1732 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1734 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1735 newargs = TYPE_ARG_TYPES (newtype);
1736 i = 1;
1738 for (;;)
1740 tree oldargtype = TREE_VALUE (oldargs);
1741 tree newargtype = TREE_VALUE (newargs);
1743 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1744 return false;
1746 oldargtype = (TYPE_ATOMIC (oldargtype)
1747 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1748 TYPE_QUAL_ATOMIC)
1749 : TYPE_MAIN_VARIANT (oldargtype));
1750 newargtype = (TYPE_ATOMIC (newargtype)
1751 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1752 TYPE_QUAL_ATOMIC)
1753 : TYPE_MAIN_VARIANT (newargtype));
1755 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1756 break;
1758 /* Reaching the end of just one list means the two decls don't
1759 agree on the number of arguments. */
1760 if (END_OF_ARGLIST (oldargtype))
1762 error ("prototype for %q+D declares more arguments "
1763 "than previous old-style definition", newdecl);
1764 return false;
1766 else if (END_OF_ARGLIST (newargtype))
1768 error ("prototype for %q+D declares fewer arguments "
1769 "than previous old-style definition", newdecl);
1770 return false;
1773 /* Type for passing arg must be consistent with that declared
1774 for the arg. */
1775 else if (!comptypes (oldargtype, newargtype))
1777 error ("prototype for %q+D declares argument %d"
1778 " with incompatible type",
1779 newdecl, i);
1780 return false;
1783 oldargs = TREE_CHAIN (oldargs);
1784 newargs = TREE_CHAIN (newargs);
1785 i++;
1788 /* If we get here, no errors were found, but do issue a warning
1789 for this poor-style construct. */
1790 warning (0, "prototype for %q+D follows non-prototype definition",
1791 newdecl);
1792 return true;
1793 #undef END_OF_ARGLIST
1796 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1797 first in a pair of mismatched declarations, using the diagnostic
1798 function DIAG. */
1799 static void
1800 locate_old_decl (tree decl)
1802 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1803 && !C_DECL_DECLARED_BUILTIN (decl))
1805 else if (DECL_INITIAL (decl))
1806 inform (input_location, "previous definition of %q+D was here", decl);
1807 else if (C_DECL_IMPLICIT (decl))
1808 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1809 else
1810 inform (input_location, "previous declaration of %q+D was here", decl);
1813 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1814 Returns true if the caller should proceed to merge the two, false
1815 if OLDDECL should simply be discarded. As a side effect, issues
1816 all necessary diagnostics for invalid or poor-style combinations.
1817 If it returns true, writes the types of NEWDECL and OLDDECL to
1818 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1819 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1821 static bool
1822 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1823 tree *newtypep, tree *oldtypep)
1825 tree newtype, oldtype;
1826 bool pedwarned = false;
1827 bool warned = false;
1828 bool retval = true;
1830 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1831 && DECL_EXTERNAL (DECL))
1833 /* If we have error_mark_node for either decl or type, just discard
1834 the previous decl - we're in an error cascade already. */
1835 if (olddecl == error_mark_node || newdecl == error_mark_node)
1836 return false;
1837 *oldtypep = oldtype = TREE_TYPE (olddecl);
1838 *newtypep = newtype = TREE_TYPE (newdecl);
1839 if (oldtype == error_mark_node || newtype == error_mark_node)
1840 return false;
1842 /* Two different categories of symbol altogether. This is an error
1843 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1844 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1846 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1847 && DECL_BUILT_IN (olddecl)
1848 && !C_DECL_DECLARED_BUILTIN (olddecl)))
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 error ("redeclaration of enumerator %q+D", newdecl);
1868 locate_old_decl (olddecl);
1869 return false;
1872 if (!comptypes (oldtype, newtype))
1874 if (TREE_CODE (olddecl) == FUNCTION_DECL
1875 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1877 /* Accept harmless mismatch in function types.
1878 This is for the ffs and fprintf builtins. */
1879 tree trytype = match_builtin_function_types (newtype, oldtype);
1881 if (trytype && comptypes (newtype, trytype))
1882 *oldtypep = oldtype = trytype;
1883 else
1885 /* If types don't match for a built-in, throw away the
1886 built-in. No point in calling locate_old_decl here, it
1887 won't print anything. */
1888 warning (OPT_Wbuiltin_declaration_mismatch,
1889 "conflicting types for built-in function %q+D",
1890 newdecl);
1891 return false;
1894 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1895 && DECL_IS_BUILTIN (olddecl))
1897 /* A conflicting function declaration for a predeclared
1898 function that isn't actually built in. Objective C uses
1899 these. The new declaration silently overrides everything
1900 but the volatility (i.e. noreturn) indication. See also
1901 below. FIXME: Make Objective C use normal builtins. */
1902 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1903 return false;
1905 /* Permit void foo (...) to match int foo (...) if the latter is
1906 the definition and implicit int was used. See
1907 c-torture/compile/920625-2.c. */
1908 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1909 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1910 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1911 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1913 pedwarned = pedwarn (input_location, 0,
1914 "conflicting types for %q+D", newdecl);
1915 /* Make sure we keep void as the return type. */
1916 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1917 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1919 /* Permit void foo (...) to match an earlier call to foo (...) with
1920 no declared type (thus, implicitly int). */
1921 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1922 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1923 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1924 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1926 pedwarned = pedwarn (input_location, 0,
1927 "conflicting types for %q+D", newdecl);
1928 /* Make sure we keep void as the return type. */
1929 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1931 else
1933 int new_quals = TYPE_QUALS (newtype);
1934 int old_quals = TYPE_QUALS (oldtype);
1936 if (new_quals != old_quals)
1938 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1939 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1940 if (new_addr != old_addr)
1942 if (ADDR_SPACE_GENERIC_P (new_addr))
1943 error ("conflicting named address spaces (generic vs %s) "
1944 "for %q+D",
1945 c_addr_space_name (old_addr), newdecl);
1946 else if (ADDR_SPACE_GENERIC_P (old_addr))
1947 error ("conflicting named address spaces (%s vs generic) "
1948 "for %q+D",
1949 c_addr_space_name (new_addr), newdecl);
1950 else
1951 error ("conflicting named address spaces (%s vs %s) "
1952 "for %q+D",
1953 c_addr_space_name (new_addr),
1954 c_addr_space_name (old_addr),
1955 newdecl);
1958 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1959 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1960 error ("conflicting type qualifiers for %q+D", newdecl);
1962 else
1963 error ("conflicting types for %q+D", newdecl);
1964 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1965 locate_old_decl (olddecl);
1966 return false;
1970 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1971 but silently ignore the redeclaration if either is in a system
1972 header. (Conflicting redeclarations were handled above.) This
1973 is allowed for C11 if the types are the same, not just
1974 compatible. */
1975 if (TREE_CODE (newdecl) == TYPE_DECL)
1977 bool types_different = false;
1978 int comptypes_result;
1980 comptypes_result
1981 = comptypes_check_different_types (oldtype, newtype, &types_different);
1983 if (comptypes_result != 1 || types_different)
1985 error ("redefinition of typedef %q+D with different type", newdecl);
1986 locate_old_decl (olddecl);
1987 return false;
1990 if (DECL_IN_SYSTEM_HEADER (newdecl)
1991 || DECL_IN_SYSTEM_HEADER (olddecl)
1992 || TREE_NO_WARNING (newdecl)
1993 || TREE_NO_WARNING (olddecl))
1994 return true; /* Allow OLDDECL to continue in use. */
1996 if (variably_modified_type_p (newtype, NULL))
1998 error ("redefinition of typedef %q+D with variably modified type",
1999 newdecl);
2000 locate_old_decl (olddecl);
2002 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2003 "redefinition of typedef %q+D", newdecl))
2004 locate_old_decl (olddecl);
2006 return true;
2009 /* Function declarations can either be 'static' or 'extern' (no
2010 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2011 can never conflict with each other on account of linkage
2012 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2013 gnu89 mode permits two definitions if one is 'extern inline' and
2014 one is not. The non- extern-inline definition supersedes the
2015 extern-inline definition. */
2017 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2019 /* If you declare a built-in function name as static, or
2020 define the built-in with an old-style definition (so we
2021 can't validate the argument list) the built-in definition is
2022 overridden, but optionally warn this was a bad choice of name. */
2023 if (DECL_BUILT_IN (olddecl)
2024 && !C_DECL_DECLARED_BUILTIN (olddecl)
2025 && (!TREE_PUBLIC (newdecl)
2026 || (DECL_INITIAL (newdecl)
2027 && !prototype_p (TREE_TYPE (newdecl)))))
2029 warning (OPT_Wshadow, "declaration of %q+D shadows "
2030 "a built-in function", newdecl);
2031 /* Discard the old built-in function. */
2032 return false;
2035 if (DECL_INITIAL (newdecl))
2037 if (DECL_INITIAL (olddecl))
2039 /* If both decls are in the same TU and the new declaration
2040 isn't overriding an extern inline reject the new decl.
2041 In c99, no overriding is allowed in the same translation
2042 unit. */
2043 if ((!DECL_EXTERN_INLINE (olddecl)
2044 || DECL_EXTERN_INLINE (newdecl)
2045 || (!flag_gnu89_inline
2046 && (!DECL_DECLARED_INLINE_P (olddecl)
2047 || !lookup_attribute ("gnu_inline",
2048 DECL_ATTRIBUTES (olddecl)))
2049 && (!DECL_DECLARED_INLINE_P (newdecl)
2050 || !lookup_attribute ("gnu_inline",
2051 DECL_ATTRIBUTES (newdecl))))
2053 && same_translation_unit_p (newdecl, olddecl))
2055 error ("redefinition of %q+D", newdecl);
2056 locate_old_decl (olddecl);
2057 return false;
2061 /* If we have a prototype after an old-style function definition,
2062 the argument types must be checked specially. */
2063 else if (DECL_INITIAL (olddecl)
2064 && !prototype_p (oldtype) && prototype_p (newtype)
2065 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2066 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2068 locate_old_decl (olddecl);
2069 return false;
2071 /* A non-static declaration (even an "extern") followed by a
2072 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2073 The same is true for a static forward declaration at block
2074 scope followed by a non-static declaration/definition at file
2075 scope. Static followed by non-static at the same scope is
2076 not undefined behavior, and is the most convenient way to get
2077 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2078 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2079 we do diagnose it if -Wtraditional. */
2080 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2082 /* Two exceptions to the rule. If olddecl is an extern
2083 inline, or a predeclared function that isn't actually
2084 built in, newdecl silently overrides olddecl. The latter
2085 occur only in Objective C; see also above. (FIXME: Make
2086 Objective C use normal builtins.) */
2087 if (!DECL_IS_BUILTIN (olddecl)
2088 && !DECL_EXTERN_INLINE (olddecl))
2090 error ("static declaration of %q+D follows "
2091 "non-static declaration", newdecl);
2092 locate_old_decl (olddecl);
2094 return false;
2096 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2098 if (DECL_CONTEXT (olddecl))
2100 error ("non-static declaration of %q+D follows "
2101 "static declaration", newdecl);
2102 locate_old_decl (olddecl);
2103 return false;
2105 else if (warn_traditional)
2107 warned |= warning (OPT_Wtraditional,
2108 "non-static declaration of %q+D "
2109 "follows static declaration", newdecl);
2113 /* Make sure gnu_inline attribute is either not present, or
2114 present on all inline decls. */
2115 if (DECL_DECLARED_INLINE_P (olddecl)
2116 && DECL_DECLARED_INLINE_P (newdecl))
2118 bool newa = lookup_attribute ("gnu_inline",
2119 DECL_ATTRIBUTES (newdecl)) != NULL;
2120 bool olda = lookup_attribute ("gnu_inline",
2121 DECL_ATTRIBUTES (olddecl)) != NULL;
2122 if (newa != olda)
2124 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2125 newa ? newdecl : olddecl);
2126 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2127 "but not here");
2131 else if (VAR_P (newdecl))
2133 /* Only variables can be thread-local, and all declarations must
2134 agree on this property. */
2135 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2137 /* Nothing to check. Since OLDDECL is marked threadprivate
2138 and NEWDECL does not have a thread-local attribute, we
2139 will merge the threadprivate attribute into NEWDECL. */
2142 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2144 if (DECL_THREAD_LOCAL_P (newdecl))
2145 error ("thread-local declaration of %q+D follows "
2146 "non-thread-local declaration", newdecl);
2147 else
2148 error ("non-thread-local declaration of %q+D follows "
2149 "thread-local declaration", newdecl);
2151 locate_old_decl (olddecl);
2152 return false;
2155 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2156 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2158 error ("redefinition of %q+D", newdecl);
2159 locate_old_decl (olddecl);
2160 return false;
2163 /* Objects declared at file scope: if the first declaration had
2164 external linkage (even if it was an external reference) the
2165 second must have external linkage as well, or the behavior is
2166 undefined. If the first declaration had internal linkage, then
2167 the second must too, or else be an external reference (in which
2168 case the composite declaration still has internal linkage).
2169 As for function declarations, we warn about the static-then-
2170 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2171 if (DECL_FILE_SCOPE_P (newdecl)
2172 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2174 if (DECL_EXTERNAL (newdecl))
2176 if (!DECL_FILE_SCOPE_P (olddecl))
2178 error ("extern declaration of %q+D follows "
2179 "declaration with no linkage", newdecl);
2180 locate_old_decl (olddecl);
2181 return false;
2183 else if (warn_traditional)
2185 warned |= warning (OPT_Wtraditional,
2186 "non-static declaration of %q+D "
2187 "follows static declaration", newdecl);
2190 else
2192 if (TREE_PUBLIC (newdecl))
2193 error ("non-static declaration of %q+D follows "
2194 "static declaration", newdecl);
2195 else
2196 error ("static declaration of %q+D follows "
2197 "non-static declaration", newdecl);
2199 locate_old_decl (olddecl);
2200 return false;
2203 /* Two objects with the same name declared at the same block
2204 scope must both be external references (6.7p3). */
2205 else if (!DECL_FILE_SCOPE_P (newdecl))
2207 if (DECL_EXTERNAL (newdecl))
2209 /* Extern with initializer at block scope, which will
2210 already have received an error. */
2212 else if (DECL_EXTERNAL (olddecl))
2214 error ("declaration of %q+D with no linkage follows "
2215 "extern declaration", newdecl);
2216 locate_old_decl (olddecl);
2218 else
2220 error ("redeclaration of %q+D with no linkage", newdecl);
2221 locate_old_decl (olddecl);
2224 return false;
2227 /* C++ does not permit a decl to appear multiple times at file
2228 scope. */
2229 if (warn_cxx_compat
2230 && DECL_FILE_SCOPE_P (newdecl)
2231 && !DECL_EXTERNAL (newdecl)
2232 && !DECL_EXTERNAL (olddecl))
2233 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2234 OPT_Wc___compat,
2235 ("duplicate declaration of %qD is "
2236 "invalid in C++"),
2237 newdecl);
2240 /* warnings */
2241 /* All decls must agree on a visibility. */
2242 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2243 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2244 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2246 warned |= warning (0, "redeclaration of %q+D with different visibility "
2247 "(old visibility preserved)", newdecl);
2250 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2251 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2252 else /* PARM_DECL, VAR_DECL */
2254 /* Redeclaration of a parameter is a constraint violation (this is
2255 not explicitly stated, but follows from C99 6.7p3 [no more than
2256 one declaration of the same identifier with no linkage in the
2257 same scope, except type tags] and 6.2.2p6 [parameters have no
2258 linkage]). We must check for a forward parameter declaration,
2259 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2260 an extension, the mandatory diagnostic for which is handled by
2261 mark_forward_parm_decls. */
2263 if (TREE_CODE (newdecl) == PARM_DECL
2264 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2266 error ("redefinition of parameter %q+D", newdecl);
2267 locate_old_decl (olddecl);
2268 return false;
2272 /* Optional warning for completely redundant decls. */
2273 if (!warned && !pedwarned
2274 && warn_redundant_decls
2275 /* Don't warn about a function declaration followed by a
2276 definition. */
2277 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2278 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2279 /* Don't warn about redundant redeclarations of builtins. */
2280 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2281 && !DECL_BUILT_IN (newdecl)
2282 && DECL_BUILT_IN (olddecl)
2283 && !C_DECL_DECLARED_BUILTIN (olddecl))
2284 /* Don't warn about an extern followed by a definition. */
2285 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2286 /* Don't warn about forward parameter decls. */
2287 && !(TREE_CODE (newdecl) == PARM_DECL
2288 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2289 /* Don't warn about a variable definition following a declaration. */
2290 && !(VAR_P (newdecl)
2291 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2293 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2294 newdecl);
2297 /* Report location of previous decl/defn. */
2298 if (warned || pedwarned)
2299 locate_old_decl (olddecl);
2301 #undef DECL_EXTERN_INLINE
2303 return retval;
2306 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2307 consistent with OLDDECL, but carries new information. Merge the
2308 new information into OLDDECL. This function issues no
2309 diagnostics. */
2311 static void
2312 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2314 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2315 && DECL_INITIAL (newdecl) != NULL_TREE);
2316 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2317 && prototype_p (TREE_TYPE (newdecl)));
2318 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2319 && prototype_p (TREE_TYPE (olddecl)));
2321 /* For real parm decl following a forward decl, rechain the old decl
2322 in its new location and clear TREE_ASM_WRITTEN (it's not a
2323 forward decl anymore). */
2324 if (TREE_CODE (newdecl) == PARM_DECL
2325 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2327 struct c_binding *b, **here;
2329 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2330 if ((*here)->decl == olddecl)
2331 goto found;
2332 gcc_unreachable ();
2334 found:
2335 b = *here;
2336 *here = b->prev;
2337 b->prev = current_scope->bindings;
2338 current_scope->bindings = b;
2340 TREE_ASM_WRITTEN (olddecl) = 0;
2343 DECL_ATTRIBUTES (newdecl)
2344 = targetm.merge_decl_attributes (olddecl, newdecl);
2346 /* For typedefs use the old type, as the new type's DECL_NAME points
2347 at newdecl, which will be ggc_freed. */
2348 if (TREE_CODE (newdecl) == TYPE_DECL)
2350 /* But NEWTYPE might have an attribute, honor that. */
2351 tree tem = newtype;
2352 newtype = oldtype;
2354 if (TYPE_USER_ALIGN (tem))
2356 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2357 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2358 TYPE_USER_ALIGN (newtype) = true;
2361 /* And remove the new type from the variants list. */
2362 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2364 tree remove = TREE_TYPE (newdecl);
2365 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2366 t = TYPE_NEXT_VARIANT (t))
2367 if (TYPE_NEXT_VARIANT (t) == remove)
2369 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2370 break;
2375 /* Merge the data types specified in the two decls. */
2376 TREE_TYPE (newdecl)
2377 = TREE_TYPE (olddecl)
2378 = composite_type (newtype, oldtype);
2380 /* Lay the type out, unless already done. */
2381 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2383 if (TREE_TYPE (newdecl) != error_mark_node)
2384 layout_type (TREE_TYPE (newdecl));
2385 if (TREE_CODE (newdecl) != FUNCTION_DECL
2386 && TREE_CODE (newdecl) != TYPE_DECL
2387 && TREE_CODE (newdecl) != CONST_DECL)
2388 layout_decl (newdecl, 0);
2390 else
2392 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2393 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2394 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2395 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2396 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2398 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2399 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2401 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2402 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2403 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2404 DECL_WARN_IF_NOT_ALIGN (olddecl));
2407 /* Keep the old rtl since we can safely use it. */
2408 if (HAS_RTL_P (olddecl))
2409 COPY_DECL_RTL (olddecl, newdecl);
2411 /* Merge the type qualifiers. */
2412 if (TREE_READONLY (newdecl))
2413 TREE_READONLY (olddecl) = 1;
2415 if (TREE_THIS_VOLATILE (newdecl))
2416 TREE_THIS_VOLATILE (olddecl) = 1;
2418 /* Merge deprecatedness. */
2419 if (TREE_DEPRECATED (newdecl))
2420 TREE_DEPRECATED (olddecl) = 1;
2422 /* If a decl is in a system header and the other isn't, keep the one on the
2423 system header. Otherwise, keep source location of definition rather than
2424 declaration and of prototype rather than non-prototype unless that
2425 prototype is built-in. */
2426 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2427 && DECL_IN_SYSTEM_HEADER (olddecl)
2428 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2429 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2430 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2431 && DECL_IN_SYSTEM_HEADER (newdecl)
2432 && !DECL_IN_SYSTEM_HEADER (olddecl))
2433 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2434 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2435 && DECL_INITIAL (olddecl) != NULL_TREE)
2436 || (old_is_prototype && !new_is_prototype
2437 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2438 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2440 /* Merge the initialization information. */
2441 if (DECL_INITIAL (newdecl) == NULL_TREE)
2442 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2444 /* Merge the threadprivate attribute. */
2445 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2446 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2448 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2450 /* Copy the assembler name.
2451 Currently, it can only be defined in the prototype. */
2452 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2454 /* Use visibility of whichever declaration had it specified */
2455 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2457 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2458 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2461 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2463 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2464 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2465 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2466 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2467 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2468 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2469 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2470 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2471 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2472 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2473 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2476 /* Merge the storage class information. */
2477 merge_weak (newdecl, olddecl);
2479 /* For functions, static overrides non-static. */
2480 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2482 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2483 /* This is since we don't automatically
2484 copy the attributes of NEWDECL into OLDDECL. */
2485 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2486 /* If this clears `static', clear it in the identifier too. */
2487 if (!TREE_PUBLIC (olddecl))
2488 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2492 /* In c99, 'extern' declaration before (or after) 'inline' means this
2493 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2494 is present. */
2495 if (TREE_CODE (newdecl) == FUNCTION_DECL
2496 && !flag_gnu89_inline
2497 && (DECL_DECLARED_INLINE_P (newdecl)
2498 || DECL_DECLARED_INLINE_P (olddecl))
2499 && (!DECL_DECLARED_INLINE_P (newdecl)
2500 || !DECL_DECLARED_INLINE_P (olddecl)
2501 || !DECL_EXTERNAL (olddecl))
2502 && DECL_EXTERNAL (newdecl)
2503 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2504 && !current_function_decl)
2505 DECL_EXTERNAL (newdecl) = 0;
2507 /* An inline definition following a static declaration is not
2508 DECL_EXTERNAL. */
2509 if (new_is_definition
2510 && (DECL_DECLARED_INLINE_P (newdecl)
2511 || DECL_DECLARED_INLINE_P (olddecl))
2512 && !TREE_PUBLIC (olddecl))
2513 DECL_EXTERNAL (newdecl) = 0;
2515 if (DECL_EXTERNAL (newdecl))
2517 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2518 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2520 /* An extern decl does not override previous storage class. */
2521 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2522 if (!DECL_EXTERNAL (newdecl))
2524 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2525 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2528 else
2530 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2531 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2534 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2536 /* If we're redefining a function previously defined as extern
2537 inline, make sure we emit debug info for the inline before we
2538 throw it away, in case it was inlined into a function that
2539 hasn't been written out yet. */
2540 if (new_is_definition && DECL_INITIAL (olddecl))
2541 /* The new defn must not be inline. */
2542 DECL_UNINLINABLE (newdecl) = 1;
2543 else
2545 /* If either decl says `inline', this fn is inline, unless
2546 its definition was passed already. */
2547 if (DECL_DECLARED_INLINE_P (newdecl)
2548 || DECL_DECLARED_INLINE_P (olddecl))
2549 DECL_DECLARED_INLINE_P (newdecl) = 1;
2551 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2552 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2554 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2555 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2556 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2557 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2560 if (DECL_BUILT_IN (olddecl))
2562 /* If redeclaring a builtin function, it stays built in.
2563 But it gets tagged as having been declared. */
2564 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2565 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2566 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2567 if (new_is_prototype)
2569 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2570 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2572 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2573 switch (fncode)
2575 /* If a compatible prototype of these builtin functions
2576 is seen, assume the runtime implements it with the
2577 expected semantics. */
2578 case BUILT_IN_STPCPY:
2579 if (builtin_decl_explicit_p (fncode))
2580 set_builtin_decl_implicit_p (fncode, true);
2581 break;
2582 default:
2583 if (builtin_decl_explicit_p (fncode))
2584 set_builtin_decl_declared_p (fncode, true);
2585 break;
2588 copy_attributes_to_builtin (newdecl);
2591 else
2592 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2593 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2596 /* Preserve function specific target and optimization options */
2597 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2598 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2599 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2600 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2602 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2603 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2604 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2605 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2607 /* Also preserve various other info from the definition. */
2608 if (!new_is_definition)
2610 tree t;
2611 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2612 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2613 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2614 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2615 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2616 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2617 DECL_CONTEXT (t) = newdecl;
2619 /* See if we've got a function to instantiate from. */
2620 if (DECL_SAVED_TREE (olddecl))
2621 DECL_ABSTRACT_ORIGIN (newdecl)
2622 = DECL_ABSTRACT_ORIGIN (olddecl);
2626 /* Merge the USED information. */
2627 if (TREE_USED (olddecl))
2628 TREE_USED (newdecl) = 1;
2629 else if (TREE_USED (newdecl))
2630 TREE_USED (olddecl) = 1;
2631 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2632 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2633 if (DECL_PRESERVE_P (olddecl))
2634 DECL_PRESERVE_P (newdecl) = 1;
2635 else if (DECL_PRESERVE_P (newdecl))
2636 DECL_PRESERVE_P (olddecl) = 1;
2638 /* Merge DECL_COMMON */
2639 if (VAR_P (olddecl) && VAR_P (newdecl)
2640 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2641 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2642 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2644 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2645 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2646 DECL_ARGUMENTS (if appropriate). */
2648 unsigned olddecl_uid = DECL_UID (olddecl);
2649 tree olddecl_context = DECL_CONTEXT (olddecl);
2650 tree olddecl_arguments = NULL;
2651 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2652 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2654 memcpy ((char *) olddecl + sizeof (struct tree_common),
2655 (char *) newdecl + sizeof (struct tree_common),
2656 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2657 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2658 switch (TREE_CODE (olddecl))
2660 case FUNCTION_DECL:
2661 case VAR_DECL:
2663 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2665 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2666 (char *) newdecl + sizeof (struct tree_decl_common),
2667 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2668 olddecl->decl_with_vis.symtab_node = snode;
2670 if ((DECL_EXTERNAL (olddecl)
2671 || TREE_PUBLIC (olddecl)
2672 || TREE_STATIC (olddecl))
2673 && DECL_SECTION_NAME (newdecl) != NULL)
2674 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2676 /* This isn't quite correct for something like
2677 int __thread x attribute ((tls_model ("local-exec")));
2678 extern int __thread x;
2679 as we'll lose the "local-exec" model. */
2680 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2681 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2682 break;
2685 case FIELD_DECL:
2686 case PARM_DECL:
2687 case LABEL_DECL:
2688 case RESULT_DECL:
2689 case CONST_DECL:
2690 case TYPE_DECL:
2691 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2692 (char *) newdecl + sizeof (struct tree_decl_common),
2693 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2694 break;
2696 default:
2698 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2699 (char *) newdecl + sizeof (struct tree_decl_common),
2700 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2702 DECL_UID (olddecl) = olddecl_uid;
2703 DECL_CONTEXT (olddecl) = olddecl_context;
2704 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2705 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2708 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2709 so that encode_section_info has a chance to look at the new decl
2710 flags and attributes. */
2711 if (DECL_RTL_SET_P (olddecl)
2712 && (TREE_CODE (olddecl) == FUNCTION_DECL
2713 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2714 make_decl_rtl (olddecl);
2717 /* Handle when a new declaration NEWDECL has the same name as an old
2718 one OLDDECL in the same binding contour. Prints an error message
2719 if appropriate.
2721 If safely possible, alter OLDDECL to look like NEWDECL, and return
2722 true. Otherwise, return false. */
2724 static bool
2725 duplicate_decls (tree newdecl, tree olddecl)
2727 tree newtype = NULL, oldtype = NULL;
2729 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2731 /* Avoid `unused variable' and other warnings for OLDDECL. */
2732 TREE_NO_WARNING (olddecl) = 1;
2733 return false;
2736 merge_decls (newdecl, olddecl, newtype, oldtype);
2738 /* The NEWDECL will no longer be needed.
2740 Before releasing the node, be sure to remove function from symbol
2741 table that might have been inserted there to record comdat group.
2742 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2743 structure is shared in between NEWDECL and OLDECL. */
2744 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2745 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2746 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2748 struct symtab_node *snode = symtab_node::get (newdecl);
2749 if (snode)
2750 snode->remove ();
2752 ggc_free (newdecl);
2753 return true;
2757 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2758 static void
2759 warn_if_shadowing (tree new_decl)
2761 struct c_binding *b;
2763 /* Shadow warnings wanted? */
2764 if (!(warn_shadow
2765 || warn_shadow_local
2766 || warn_shadow_compatible_local)
2767 /* No shadow warnings for internally generated vars. */
2768 || DECL_IS_BUILTIN (new_decl)
2769 /* No shadow warnings for vars made for inlining. */
2770 || DECL_FROM_INLINE (new_decl))
2771 return;
2773 /* Is anything being shadowed? Invisible decls do not count. */
2774 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2775 if (b->decl && b->decl != new_decl && !b->invisible
2776 && (b->decl == error_mark_node
2777 || diagnostic_report_warnings_p (global_dc,
2778 DECL_SOURCE_LOCATION (b->decl))))
2780 tree old_decl = b->decl;
2781 bool warned = false;
2783 if (old_decl == error_mark_node)
2785 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2786 "non-variable", new_decl);
2787 break;
2789 else if (TREE_CODE (old_decl) == PARM_DECL)
2791 enum opt_code warning_code;
2793 /* If '-Wshadow=compatible-local' is specified without other
2794 -Wshadow= flags, we will warn only when the types of the
2795 shadowing variable (i.e. new_decl) and the shadowed variable
2796 (old_decl) are compatible. */
2797 if (warn_shadow)
2798 warning_code = OPT_Wshadow;
2799 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2800 warning_code = OPT_Wshadow_compatible_local;
2801 else
2802 warning_code = OPT_Wshadow_local;
2803 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2804 "declaration of %qD shadows a parameter",
2805 new_decl);
2807 else if (DECL_FILE_SCOPE_P (old_decl))
2809 /* Do not warn if a variable shadows a function, unless
2810 the variable is a function or a pointer-to-function. */
2811 if (TREE_CODE (old_decl) == FUNCTION_DECL
2812 && TREE_CODE (new_decl) != FUNCTION_DECL
2813 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2814 continue;
2816 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2817 "declaration of %qD shadows a global "
2818 "declaration",
2819 new_decl);
2821 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2822 && DECL_BUILT_IN (old_decl))
2824 warning (OPT_Wshadow, "declaration of %q+D shadows "
2825 "a built-in function", new_decl);
2826 break;
2828 else
2830 enum opt_code warning_code;
2832 /* If '-Wshadow=compatible-local' is specified without other
2833 -Wshadow= flags, we will warn only when the types of the
2834 shadowing variable (i.e. new_decl) and the shadowed variable
2835 (old_decl) are compatible. */
2836 if (warn_shadow)
2837 warning_code = OPT_Wshadow;
2838 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2839 warning_code = OPT_Wshadow_compatible_local;
2840 else
2841 warning_code = OPT_Wshadow_local;
2842 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2843 "declaration of %qD shadows a previous local",
2844 new_decl);
2847 if (warned)
2848 inform (DECL_SOURCE_LOCATION (old_decl),
2849 "shadowed declaration is here");
2851 break;
2855 /* Record a decl-node X as belonging to the current lexical scope.
2856 Check for errors (such as an incompatible declaration for the same
2857 name already seen in the same scope).
2859 Returns either X or an old decl for the same name.
2860 If an old decl is returned, it may have been smashed
2861 to agree with what X says. */
2863 tree
2864 pushdecl (tree x)
2866 tree name = DECL_NAME (x);
2867 struct c_scope *scope = current_scope;
2868 struct c_binding *b;
2869 bool nested = false;
2870 location_t locus = DECL_SOURCE_LOCATION (x);
2872 /* Must set DECL_CONTEXT for everything not at file scope or
2873 DECL_FILE_SCOPE_P won't work. Local externs don't count
2874 unless they have initializers (which generate code). */
2875 if (current_function_decl
2876 && (!VAR_OR_FUNCTION_DECL_P (x)
2877 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2878 DECL_CONTEXT (x) = current_function_decl;
2880 /* Anonymous decls are just inserted in the scope. */
2881 if (!name)
2883 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2884 locus);
2885 return x;
2888 /* First, see if there is another declaration with the same name in
2889 the current scope. If there is, duplicate_decls may do all the
2890 work for us. If duplicate_decls returns false, that indicates
2891 two incompatible decls in the same scope; we are to silently
2892 replace the old one (duplicate_decls has issued all appropriate
2893 diagnostics). In particular, we should not consider possible
2894 duplicates in the external scope, or shadowing. */
2895 b = I_SYMBOL_BINDING (name);
2896 if (b && B_IN_SCOPE (b, scope))
2898 struct c_binding *b_ext, *b_use;
2899 tree type = TREE_TYPE (x);
2900 tree visdecl = b->decl;
2901 tree vistype = TREE_TYPE (visdecl);
2902 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2903 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2904 b->inner_comp = false;
2905 b_use = b;
2906 b_ext = b;
2907 /* If this is an external linkage declaration, we should check
2908 for compatibility with the type in the external scope before
2909 setting the type at this scope based on the visible
2910 information only. */
2911 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2913 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2914 b_ext = b_ext->shadowed;
2915 if (b_ext)
2917 b_use = b_ext;
2918 if (b_use->u.type)
2919 TREE_TYPE (b_use->decl) = b_use->u.type;
2922 if (duplicate_decls (x, b_use->decl))
2924 if (b_use != b)
2926 /* Save the updated type in the external scope and
2927 restore the proper type for this scope. */
2928 tree thistype;
2929 if (comptypes (vistype, type))
2930 thistype = composite_type (vistype, type);
2931 else
2932 thistype = TREE_TYPE (b_use->decl);
2933 b_use->u.type = TREE_TYPE (b_use->decl);
2934 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2935 && DECL_BUILT_IN (b_use->decl))
2936 thistype
2937 = build_type_attribute_variant (thistype,
2938 TYPE_ATTRIBUTES
2939 (b_use->u.type));
2940 TREE_TYPE (b_use->decl) = thistype;
2942 return b_use->decl;
2944 else
2945 goto skip_external_and_shadow_checks;
2948 /* All declarations with external linkage, and all external
2949 references, go in the external scope, no matter what scope is
2950 current. However, the binding in that scope is ignored for
2951 purposes of normal name lookup. A separate binding structure is
2952 created in the requested scope; this governs the normal
2953 visibility of the symbol.
2955 The binding in the externals scope is used exclusively for
2956 detecting duplicate declarations of the same object, no matter
2957 what scope they are in; this is what we do here. (C99 6.2.7p2:
2958 All declarations that refer to the same object or function shall
2959 have compatible type; otherwise, the behavior is undefined.) */
2960 if (DECL_EXTERNAL (x) || scope == file_scope)
2962 tree type = TREE_TYPE (x);
2963 tree vistype = NULL_TREE;
2964 tree visdecl = NULL_TREE;
2965 bool type_saved = false;
2966 if (b && !B_IN_EXTERNAL_SCOPE (b)
2967 && VAR_OR_FUNCTION_DECL_P (b->decl)
2968 && DECL_FILE_SCOPE_P (b->decl))
2970 visdecl = b->decl;
2971 vistype = TREE_TYPE (visdecl);
2973 if (scope != file_scope
2974 && !DECL_IN_SYSTEM_HEADER (x))
2975 warning_at (locus, OPT_Wnested_externs,
2976 "nested extern declaration of %qD", x);
2978 while (b && !B_IN_EXTERNAL_SCOPE (b))
2980 /* If this decl might be modified, save its type. This is
2981 done here rather than when the decl is first bound
2982 because the type may change after first binding, through
2983 being completed or through attributes being added. If we
2984 encounter multiple such decls, only the first should have
2985 its type saved; the others will already have had their
2986 proper types saved and the types will not have changed as
2987 their scopes will not have been re-entered. */
2988 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2990 b->u.type = TREE_TYPE (b->decl);
2991 type_saved = true;
2993 if (B_IN_FILE_SCOPE (b)
2994 && VAR_P (b->decl)
2995 && TREE_STATIC (b->decl)
2996 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2997 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2998 && TREE_CODE (type) == ARRAY_TYPE
2999 && TYPE_DOMAIN (type)
3000 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3001 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3003 /* Array type completed in inner scope, which should be
3004 diagnosed if the completion does not have size 1 and
3005 it does not get completed in the file scope. */
3006 b->inner_comp = true;
3008 b = b->shadowed;
3011 /* If a matching external declaration has been found, set its
3012 type to the composite of all the types of that declaration.
3013 After the consistency checks, it will be reset to the
3014 composite of the visible types only. */
3015 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3016 && b->u.type)
3017 TREE_TYPE (b->decl) = b->u.type;
3019 /* The point of the same_translation_unit_p check here is,
3020 we want to detect a duplicate decl for a construct like
3021 foo() { extern bar(); } ... static bar(); but not if
3022 they are in different translation units. In any case,
3023 the static does not go in the externals scope. */
3024 if (b
3025 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3026 && duplicate_decls (x, b->decl))
3028 tree thistype;
3029 if (vistype)
3031 if (comptypes (vistype, type))
3032 thistype = composite_type (vistype, type);
3033 else
3034 thistype = TREE_TYPE (b->decl);
3036 else
3037 thistype = type;
3038 b->u.type = TREE_TYPE (b->decl);
3039 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3040 thistype
3041 = build_type_attribute_variant (thistype,
3042 TYPE_ATTRIBUTES (b->u.type));
3043 TREE_TYPE (b->decl) = thistype;
3044 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3045 locus);
3046 return b->decl;
3048 else if (TREE_PUBLIC (x))
3050 if (visdecl && !b && duplicate_decls (x, visdecl))
3052 /* An external declaration at block scope referring to a
3053 visible entity with internal linkage. The composite
3054 type will already be correct for this scope, so we
3055 just need to fall through to make the declaration in
3056 this scope. */
3057 nested = true;
3058 x = visdecl;
3060 else
3062 bind (name, x, external_scope, /*invisible=*/true,
3063 /*nested=*/false, locus);
3064 nested = true;
3069 if (TREE_CODE (x) != PARM_DECL)
3070 warn_if_shadowing (x);
3072 skip_external_and_shadow_checks:
3073 if (TREE_CODE (x) == TYPE_DECL)
3075 /* So this is a typedef, set its underlying type. */
3076 set_underlying_type (x);
3078 /* If X is a typedef defined in the current function, record it
3079 for the purpose of implementing the -Wunused-local-typedefs
3080 warning. */
3081 record_locally_defined_typedef (x);
3084 bind (name, x, scope, /*invisible=*/false, nested, locus);
3086 /* If x's type is incomplete because it's based on a
3087 structure or union which has not yet been fully declared,
3088 attach it to that structure or union type, so we can go
3089 back and complete the variable declaration later, if the
3090 structure or union gets fully declared.
3092 If the input is erroneous, we can have error_mark in the type
3093 slot (e.g. "f(void a, ...)") - that doesn't count as an
3094 incomplete type. */
3095 if (TREE_TYPE (x) != error_mark_node
3096 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3098 tree element = TREE_TYPE (x);
3100 while (TREE_CODE (element) == ARRAY_TYPE)
3101 element = TREE_TYPE (element);
3102 element = TYPE_MAIN_VARIANT (element);
3104 if (RECORD_OR_UNION_TYPE_P (element)
3105 && (TREE_CODE (x) != TYPE_DECL
3106 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3107 && !COMPLETE_TYPE_P (element))
3108 C_TYPE_INCOMPLETE_VARS (element)
3109 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3111 return x;
3115 /* Issue a warning about implicit function declaration. ID is the function
3116 identifier, OLDDECL is a declaration of the function in a different scope,
3117 or NULL_TREE. */
3119 static void
3120 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3122 if (!warn_implicit_function_declaration)
3123 return;
3125 bool warned;
3126 name_hint hint;
3127 if (!olddecl)
3128 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3130 if (flag_isoc99)
3132 if (hint)
3134 gcc_rich_location richloc (loc);
3135 richloc.add_fixit_replace (hint.suggestion ());
3136 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3137 "implicit declaration of function %qE;"
3138 " did you mean %qs?",
3139 id, hint.suggestion ());
3141 else
3142 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3143 "implicit declaration of function %qE", id);
3145 else if (hint)
3147 gcc_rich_location richloc (loc);
3148 richloc.add_fixit_replace (hint.suggestion ());
3149 warned = warning_at
3150 (&richloc, OPT_Wimplicit_function_declaration,
3151 G_("implicit declaration of function %qE; did you mean %qs?"),
3152 id, hint.suggestion ());
3154 else
3155 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3156 G_("implicit declaration of function %qE"), id);
3158 if (olddecl && warned)
3159 locate_old_decl (olddecl);
3161 if (!warned)
3162 hint.suppress ();
3165 /* This function represents mapping of a function code FCODE
3166 to its respective header. */
3168 static const char *
3169 header_for_builtin_fn (enum built_in_function fcode)
3171 switch (fcode)
3173 CASE_FLT_FN (BUILT_IN_ACOS):
3174 CASE_FLT_FN (BUILT_IN_ACOSH):
3175 CASE_FLT_FN (BUILT_IN_ASIN):
3176 CASE_FLT_FN (BUILT_IN_ASINH):
3177 CASE_FLT_FN (BUILT_IN_ATAN):
3178 CASE_FLT_FN (BUILT_IN_ATANH):
3179 CASE_FLT_FN (BUILT_IN_ATAN2):
3180 CASE_FLT_FN (BUILT_IN_CBRT):
3181 CASE_FLT_FN (BUILT_IN_CEIL):
3182 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3183 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3184 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3185 CASE_FLT_FN (BUILT_IN_COS):
3186 CASE_FLT_FN (BUILT_IN_COSH):
3187 CASE_FLT_FN (BUILT_IN_ERF):
3188 CASE_FLT_FN (BUILT_IN_ERFC):
3189 CASE_FLT_FN (BUILT_IN_EXP):
3190 CASE_FLT_FN (BUILT_IN_EXP2):
3191 CASE_FLT_FN (BUILT_IN_EXPM1):
3192 CASE_FLT_FN (BUILT_IN_FABS):
3193 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3194 CASE_FLT_FN (BUILT_IN_FDIM):
3195 CASE_FLT_FN (BUILT_IN_FLOOR):
3196 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3197 CASE_FLT_FN (BUILT_IN_FMA):
3198 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3199 CASE_FLT_FN (BUILT_IN_FMAX):
3200 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3201 CASE_FLT_FN (BUILT_IN_FMIN):
3202 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3203 CASE_FLT_FN (BUILT_IN_FMOD):
3204 CASE_FLT_FN (BUILT_IN_FREXP):
3205 CASE_FLT_FN (BUILT_IN_HYPOT):
3206 CASE_FLT_FN (BUILT_IN_ILOGB):
3207 CASE_FLT_FN (BUILT_IN_LDEXP):
3208 CASE_FLT_FN (BUILT_IN_LGAMMA):
3209 CASE_FLT_FN (BUILT_IN_LLRINT):
3210 CASE_FLT_FN (BUILT_IN_LLROUND):
3211 CASE_FLT_FN (BUILT_IN_LOG):
3212 CASE_FLT_FN (BUILT_IN_LOG10):
3213 CASE_FLT_FN (BUILT_IN_LOG1P):
3214 CASE_FLT_FN (BUILT_IN_LOG2):
3215 CASE_FLT_FN (BUILT_IN_LOGB):
3216 CASE_FLT_FN (BUILT_IN_LRINT):
3217 CASE_FLT_FN (BUILT_IN_LROUND):
3218 CASE_FLT_FN (BUILT_IN_MODF):
3219 CASE_FLT_FN (BUILT_IN_NAN):
3220 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3221 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3222 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3223 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3224 CASE_FLT_FN (BUILT_IN_POW):
3225 CASE_FLT_FN (BUILT_IN_REMAINDER):
3226 CASE_FLT_FN (BUILT_IN_REMQUO):
3227 CASE_FLT_FN (BUILT_IN_RINT):
3228 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3229 CASE_FLT_FN (BUILT_IN_ROUND):
3230 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3231 CASE_FLT_FN (BUILT_IN_SCALBLN):
3232 CASE_FLT_FN (BUILT_IN_SCALBN):
3233 CASE_FLT_FN (BUILT_IN_SIN):
3234 CASE_FLT_FN (BUILT_IN_SINH):
3235 CASE_FLT_FN (BUILT_IN_SINCOS):
3236 CASE_FLT_FN (BUILT_IN_SQRT):
3237 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3238 CASE_FLT_FN (BUILT_IN_TAN):
3239 CASE_FLT_FN (BUILT_IN_TANH):
3240 CASE_FLT_FN (BUILT_IN_TGAMMA):
3241 CASE_FLT_FN (BUILT_IN_TRUNC):
3242 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3243 case BUILT_IN_ISINF:
3244 case BUILT_IN_ISNAN:
3245 return "<math.h>";
3246 CASE_FLT_FN (BUILT_IN_CABS):
3247 CASE_FLT_FN (BUILT_IN_CACOS):
3248 CASE_FLT_FN (BUILT_IN_CACOSH):
3249 CASE_FLT_FN (BUILT_IN_CARG):
3250 CASE_FLT_FN (BUILT_IN_CASIN):
3251 CASE_FLT_FN (BUILT_IN_CASINH):
3252 CASE_FLT_FN (BUILT_IN_CATAN):
3253 CASE_FLT_FN (BUILT_IN_CATANH):
3254 CASE_FLT_FN (BUILT_IN_CCOS):
3255 CASE_FLT_FN (BUILT_IN_CCOSH):
3256 CASE_FLT_FN (BUILT_IN_CEXP):
3257 CASE_FLT_FN (BUILT_IN_CIMAG):
3258 CASE_FLT_FN (BUILT_IN_CLOG):
3259 CASE_FLT_FN (BUILT_IN_CONJ):
3260 CASE_FLT_FN (BUILT_IN_CPOW):
3261 CASE_FLT_FN (BUILT_IN_CPROJ):
3262 CASE_FLT_FN (BUILT_IN_CREAL):
3263 CASE_FLT_FN (BUILT_IN_CSIN):
3264 CASE_FLT_FN (BUILT_IN_CSINH):
3265 CASE_FLT_FN (BUILT_IN_CSQRT):
3266 CASE_FLT_FN (BUILT_IN_CTAN):
3267 CASE_FLT_FN (BUILT_IN_CTANH):
3268 return "<complex.h>";
3269 case BUILT_IN_MEMCHR:
3270 case BUILT_IN_MEMCMP:
3271 case BUILT_IN_MEMCPY:
3272 case BUILT_IN_MEMMOVE:
3273 case BUILT_IN_MEMSET:
3274 case BUILT_IN_STRCAT:
3275 case BUILT_IN_STRCHR:
3276 case BUILT_IN_STRCMP:
3277 case BUILT_IN_STRCPY:
3278 case BUILT_IN_STRCSPN:
3279 case BUILT_IN_STRLEN:
3280 case BUILT_IN_STRNCAT:
3281 case BUILT_IN_STRNCMP:
3282 case BUILT_IN_STRNCPY:
3283 case BUILT_IN_STRPBRK:
3284 case BUILT_IN_STRRCHR:
3285 case BUILT_IN_STRSPN:
3286 case BUILT_IN_STRSTR:
3287 return "<string.h>";
3288 case BUILT_IN_FPRINTF:
3289 case BUILT_IN_PUTC:
3290 case BUILT_IN_FPUTC:
3291 case BUILT_IN_FPUTS:
3292 case BUILT_IN_FSCANF:
3293 case BUILT_IN_FWRITE:
3294 case BUILT_IN_PRINTF:
3295 case BUILT_IN_PUTCHAR:
3296 case BUILT_IN_PUTS:
3297 case BUILT_IN_SCANF:
3298 case BUILT_IN_SNPRINTF:
3299 case BUILT_IN_SPRINTF:
3300 case BUILT_IN_SSCANF:
3301 case BUILT_IN_VFPRINTF:
3302 case BUILT_IN_VFSCANF:
3303 case BUILT_IN_VPRINTF:
3304 case BUILT_IN_VSCANF:
3305 case BUILT_IN_VSNPRINTF:
3306 case BUILT_IN_VSPRINTF:
3307 case BUILT_IN_VSSCANF:
3308 return "<stdio.h>";
3309 case BUILT_IN_ISALNUM:
3310 case BUILT_IN_ISALPHA:
3311 case BUILT_IN_ISBLANK:
3312 case BUILT_IN_ISCNTRL:
3313 case BUILT_IN_ISDIGIT:
3314 case BUILT_IN_ISGRAPH:
3315 case BUILT_IN_ISLOWER:
3316 case BUILT_IN_ISPRINT:
3317 case BUILT_IN_ISPUNCT:
3318 case BUILT_IN_ISSPACE:
3319 case BUILT_IN_ISUPPER:
3320 case BUILT_IN_ISXDIGIT:
3321 case BUILT_IN_TOLOWER:
3322 case BUILT_IN_TOUPPER:
3323 return "<ctype.h>";
3324 case BUILT_IN_ISWALNUM:
3325 case BUILT_IN_ISWALPHA:
3326 case BUILT_IN_ISWBLANK:
3327 case BUILT_IN_ISWCNTRL:
3328 case BUILT_IN_ISWDIGIT:
3329 case BUILT_IN_ISWGRAPH:
3330 case BUILT_IN_ISWLOWER:
3331 case BUILT_IN_ISWPRINT:
3332 case BUILT_IN_ISWPUNCT:
3333 case BUILT_IN_ISWSPACE:
3334 case BUILT_IN_ISWUPPER:
3335 case BUILT_IN_ISWXDIGIT:
3336 case BUILT_IN_TOWLOWER:
3337 case BUILT_IN_TOWUPPER:
3338 return "<wctype.h>";
3339 case BUILT_IN_ABORT:
3340 case BUILT_IN_ABS:
3341 case BUILT_IN_CALLOC:
3342 case BUILT_IN_EXIT:
3343 case BUILT_IN_FREE:
3344 case BUILT_IN_LABS:
3345 case BUILT_IN_LLABS:
3346 case BUILT_IN_MALLOC:
3347 case BUILT_IN_REALLOC:
3348 case BUILT_IN__EXIT2:
3349 case BUILT_IN_ALIGNED_ALLOC:
3350 return "<stdlib.h>";
3351 case BUILT_IN_IMAXABS:
3352 return "<inttypes.h>";
3353 case BUILT_IN_STRFTIME:
3354 return "<time.h>";
3355 default:
3356 return NULL;
3360 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3361 function of type int (). */
3363 tree
3364 implicitly_declare (location_t loc, tree functionid)
3366 struct c_binding *b;
3367 tree decl = NULL_TREE;
3368 tree asmspec_tree;
3370 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3372 if (B_IN_SCOPE (b, external_scope))
3374 decl = b->decl;
3375 break;
3379 if (decl)
3381 if (TREE_CODE (decl) != FUNCTION_DECL)
3382 return decl;
3384 /* FIXME: Objective-C has weird not-really-builtin functions
3385 which are supposed to be visible automatically. They wind up
3386 in the external scope because they're pushed before the file
3387 scope gets created. Catch this here and rebind them into the
3388 file scope. */
3389 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3391 bind (functionid, decl, file_scope,
3392 /*invisible=*/false, /*nested=*/true,
3393 DECL_SOURCE_LOCATION (decl));
3394 return decl;
3396 else
3398 tree newtype = default_function_type;
3399 if (b->u.type)
3400 TREE_TYPE (decl) = b->u.type;
3401 /* Implicit declaration of a function already declared
3402 (somehow) in a different scope, or as a built-in.
3403 If this is the first time this has happened, warn;
3404 then recycle the old declaration but with the new type. */
3405 if (!C_DECL_IMPLICIT (decl))
3407 implicit_decl_warning (loc, functionid, decl);
3408 C_DECL_IMPLICIT (decl) = 1;
3410 if (DECL_BUILT_IN (decl))
3412 newtype = build_type_attribute_variant (newtype,
3413 TYPE_ATTRIBUTES
3414 (TREE_TYPE (decl)));
3415 if (!comptypes (newtype, TREE_TYPE (decl)))
3417 bool warned = warning_at (loc, 0, "incompatible implicit "
3418 "declaration of built-in "
3419 "function %qD", decl);
3420 /* See if we can hint which header to include. */
3421 const char *header
3422 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3423 if (header != NULL && warned)
3425 rich_location richloc (line_table, loc);
3426 maybe_add_include_fixit (&richloc, header);
3427 inform (&richloc,
3428 "include %qs or provide a declaration of %qD",
3429 header, decl);
3431 newtype = TREE_TYPE (decl);
3434 else
3436 if (!comptypes (newtype, TREE_TYPE (decl)))
3438 error_at (loc, "incompatible implicit declaration of "
3439 "function %qD", decl);
3440 locate_old_decl (decl);
3443 b->u.type = TREE_TYPE (decl);
3444 TREE_TYPE (decl) = newtype;
3445 bind (functionid, decl, current_scope,
3446 /*invisible=*/false, /*nested=*/true,
3447 DECL_SOURCE_LOCATION (decl));
3448 return decl;
3452 /* Not seen before. */
3453 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3454 DECL_EXTERNAL (decl) = 1;
3455 TREE_PUBLIC (decl) = 1;
3456 C_DECL_IMPLICIT (decl) = 1;
3457 implicit_decl_warning (loc, functionid, 0);
3458 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3459 if (asmspec_tree)
3460 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3462 /* C89 says implicit declarations are in the innermost block.
3463 So we record the decl in the standard fashion. */
3464 decl = pushdecl (decl);
3466 /* No need to call objc_check_decl here - it's a function type. */
3467 rest_of_decl_compilation (decl, 0, 0);
3469 /* Write a record describing this implicit function declaration
3470 to the prototypes file (if requested). */
3471 gen_aux_info_record (decl, 0, 1, 0);
3473 /* Possibly apply some default attributes to this implicit declaration. */
3474 decl_attributes (&decl, NULL_TREE, 0);
3476 return decl;
3479 /* Issue an error message for a reference to an undeclared variable
3480 ID, including a reference to a builtin outside of function-call
3481 context. Establish a binding of the identifier to error_mark_node
3482 in an appropriate scope, which will suppress further errors for the
3483 same identifier. The error message should be given location LOC. */
3484 void
3485 undeclared_variable (location_t loc, tree id)
3487 static bool already = false;
3488 struct c_scope *scope;
3490 if (current_function_decl == NULL_TREE)
3492 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3493 if (guessed_id)
3495 gcc_rich_location richloc (loc);
3496 richloc.add_fixit_replace (guessed_id.suggestion ());
3497 error_at (&richloc,
3498 "%qE undeclared here (not in a function);"
3499 " did you mean %qs?",
3500 id, guessed_id.suggestion ());
3502 else
3503 error_at (loc, "%qE undeclared here (not in a function)", id);
3504 scope = current_scope;
3506 else
3508 if (!objc_diagnose_private_ivar (id))
3510 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3511 if (guessed_id)
3513 gcc_rich_location richloc (loc);
3514 richloc.add_fixit_replace (guessed_id.suggestion ());
3515 error_at (&richloc,
3516 "%qE undeclared (first use in this function);"
3517 " did you mean %qs?",
3518 id, guessed_id.suggestion ());
3520 else
3521 error_at (loc, "%qE undeclared (first use in this function)", id);
3523 if (!already)
3525 inform (loc, "each undeclared identifier is reported only"
3526 " once for each function it appears in");
3527 already = true;
3530 /* If we are parsing old-style parameter decls, current_function_decl
3531 will be nonnull but current_function_scope will be null. */
3532 scope = current_function_scope ? current_function_scope : current_scope;
3534 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3535 UNKNOWN_LOCATION);
3538 /* Subroutine of lookup_label, declare_label, define_label: construct a
3539 LABEL_DECL with all the proper frills. Also create a struct
3540 c_label_vars initialized for the current scope. */
3542 static tree
3543 make_label (location_t location, tree name, bool defining,
3544 struct c_label_vars **p_label_vars)
3546 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3547 DECL_CONTEXT (label) = current_function_decl;
3548 SET_DECL_MODE (label, VOIDmode);
3550 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3551 label_vars->shadowed = NULL;
3552 set_spot_bindings (&label_vars->label_bindings, defining);
3553 label_vars->decls_in_scope = make_tree_vector ();
3554 label_vars->gotos = NULL;
3555 *p_label_vars = label_vars;
3557 return label;
3560 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3561 Create one if none exists so far for the current function.
3562 This is called when a label is used in a goto expression or
3563 has its address taken. */
3565 tree
3566 lookup_label (tree name)
3568 tree label;
3569 struct c_label_vars *label_vars;
3571 if (current_function_scope == 0)
3573 error ("label %qE referenced outside of any function", name);
3574 return NULL_TREE;
3577 /* Use a label already defined or ref'd with this name, but not if
3578 it is inherited from a containing function and wasn't declared
3579 using __label__. */
3580 label = I_LABEL_DECL (name);
3581 if (label && (DECL_CONTEXT (label) == current_function_decl
3582 || C_DECLARED_LABEL_FLAG (label)))
3584 /* If the label has only been declared, update its apparent
3585 location to point here, for better diagnostics if it
3586 turns out not to have been defined. */
3587 if (DECL_INITIAL (label) == NULL_TREE)
3588 DECL_SOURCE_LOCATION (label) = input_location;
3589 return label;
3592 /* No label binding for that identifier; make one. */
3593 label = make_label (input_location, name, false, &label_vars);
3595 /* Ordinary labels go in the current function scope. */
3596 bind_label (name, label, current_function_scope, label_vars);
3598 return label;
3601 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3602 to LABEL. */
3604 static void
3605 warn_about_goto (location_t goto_loc, tree label, tree decl)
3607 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3608 error_at (goto_loc,
3609 "jump into scope of identifier with variably modified type");
3610 else
3611 warning_at (goto_loc, OPT_Wjump_misses_init,
3612 "jump skips variable initialization");
3613 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3614 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3617 /* Look up a label because of a goto statement. This is like
3618 lookup_label, but also issues any appropriate warnings. */
3620 tree
3621 lookup_label_for_goto (location_t loc, tree name)
3623 tree label;
3624 struct c_label_vars *label_vars;
3625 unsigned int ix;
3626 tree decl;
3628 label = lookup_label (name);
3629 if (label == NULL_TREE)
3630 return NULL_TREE;
3632 /* If we are jumping to a different function, we can't issue any
3633 useful warnings. */
3634 if (DECL_CONTEXT (label) != current_function_decl)
3636 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3637 return label;
3640 label_vars = I_LABEL_BINDING (name)->u.label;
3642 /* If the label has not yet been defined, then push this goto on a
3643 list for possible later warnings. */
3644 if (label_vars->label_bindings.scope == NULL)
3646 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3648 g->loc = loc;
3649 set_spot_bindings (&g->goto_bindings, true);
3650 vec_safe_push (label_vars->gotos, g);
3651 return label;
3654 /* If there are any decls in label_vars->decls_in_scope, then this
3655 goto has missed the declaration of the decl. This happens for a
3656 case like
3657 int i = 1;
3658 lab:
3660 goto lab;
3661 Issue a warning or error. */
3662 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3663 warn_about_goto (loc, label, decl);
3665 if (label_vars->label_bindings.left_stmt_expr)
3667 error_at (loc, "jump into statement expression");
3668 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3671 return label;
3674 /* Make a label named NAME in the current function, shadowing silently
3675 any that may be inherited from containing functions or containing
3676 scopes. This is called for __label__ declarations. */
3678 tree
3679 declare_label (tree name)
3681 struct c_binding *b = I_LABEL_BINDING (name);
3682 tree label;
3683 struct c_label_vars *label_vars;
3685 /* Check to make sure that the label hasn't already been declared
3686 at this scope */
3687 if (b && B_IN_CURRENT_SCOPE (b))
3689 error ("duplicate label declaration %qE", name);
3690 locate_old_decl (b->decl);
3692 /* Just use the previous declaration. */
3693 return b->decl;
3696 label = make_label (input_location, name, false, &label_vars);
3697 C_DECLARED_LABEL_FLAG (label) = 1;
3699 /* Declared labels go in the current scope. */
3700 bind_label (name, label, current_scope, label_vars);
3702 return label;
3705 /* When we define a label, issue any appropriate warnings if there are
3706 any gotos earlier in the function which jump to this label. */
3708 static void
3709 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3711 unsigned int ix;
3712 struct c_goto_bindings *g;
3714 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3716 struct c_binding *b;
3717 struct c_scope *scope;
3719 /* We have a goto to this label. The goto is going forward. In
3720 g->scope, the goto is going to skip any binding which was
3721 defined after g->bindings_in_scope. */
3722 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3724 for (b = g->goto_bindings.scope->bindings;
3725 b != g->goto_bindings.bindings_in_scope;
3726 b = b->prev)
3728 if (decl_jump_unsafe (b->decl))
3729 warn_about_goto (g->loc, label, b->decl);
3733 /* We also need to warn about decls defined in any scopes
3734 between the scope of the label and the scope of the goto. */
3735 for (scope = label_vars->label_bindings.scope;
3736 scope != g->goto_bindings.scope;
3737 scope = scope->outer)
3739 gcc_assert (scope != NULL);
3740 if (scope->has_jump_unsafe_decl)
3742 if (scope == label_vars->label_bindings.scope)
3743 b = label_vars->label_bindings.bindings_in_scope;
3744 else
3745 b = scope->bindings;
3746 for (; b != NULL; b = b->prev)
3748 if (decl_jump_unsafe (b->decl))
3749 warn_about_goto (g->loc, label, b->decl);
3754 if (g->goto_bindings.stmt_exprs > 0)
3756 error_at (g->loc, "jump into statement expression");
3757 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3758 label);
3762 /* Now that the label is defined, we will issue warnings about
3763 subsequent gotos to this label when we see them. */
3764 vec_safe_truncate (label_vars->gotos, 0);
3765 label_vars->gotos = NULL;
3768 /* Define a label, specifying the location in the source file.
3769 Return the LABEL_DECL node for the label, if the definition is valid.
3770 Otherwise return NULL_TREE. */
3772 tree
3773 define_label (location_t location, tree name)
3775 /* Find any preexisting label with this name. It is an error
3776 if that label has already been defined in this function, or
3777 if there is a containing function with a declared label with
3778 the same name. */
3779 tree label = I_LABEL_DECL (name);
3781 if (label
3782 && ((DECL_CONTEXT (label) == current_function_decl
3783 && DECL_INITIAL (label) != NULL_TREE)
3784 || (DECL_CONTEXT (label) != current_function_decl
3785 && C_DECLARED_LABEL_FLAG (label))))
3787 error_at (location, "duplicate label %qD", label);
3788 locate_old_decl (label);
3789 return NULL_TREE;
3791 else if (label && DECL_CONTEXT (label) == current_function_decl)
3793 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3795 /* The label has been used or declared already in this function,
3796 but not defined. Update its location to point to this
3797 definition. */
3798 DECL_SOURCE_LOCATION (label) = location;
3799 set_spot_bindings (&label_vars->label_bindings, true);
3801 /* Issue warnings as required about any goto statements from
3802 earlier in the function. */
3803 check_earlier_gotos (label, label_vars);
3805 else
3807 struct c_label_vars *label_vars;
3809 /* No label binding for that identifier; make one. */
3810 label = make_label (location, name, true, &label_vars);
3812 /* Ordinary labels go in the current function scope. */
3813 bind_label (name, label, current_function_scope, label_vars);
3816 if (!in_system_header_at (input_location) && lookup_name (name))
3817 warning_at (location, OPT_Wtraditional,
3818 "traditional C lacks a separate namespace "
3819 "for labels, identifier %qE conflicts", name);
3821 /* Mark label as having been defined. */
3822 DECL_INITIAL (label) = error_mark_node;
3823 return label;
3826 /* Get the bindings for a new switch statement. This is used to issue
3827 warnings as appropriate for jumps from the switch to case or
3828 default labels. */
3830 struct c_spot_bindings *
3831 c_get_switch_bindings (void)
3833 struct c_spot_bindings *switch_bindings;
3835 switch_bindings = XNEW (struct c_spot_bindings);
3836 set_spot_bindings (switch_bindings, true);
3837 return switch_bindings;
3840 void
3841 c_release_switch_bindings (struct c_spot_bindings *bindings)
3843 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3844 XDELETE (bindings);
3847 /* This is called at the point of a case or default label to issue
3848 warnings about decls as needed. It returns true if it found an
3849 error, not just a warning. */
3851 bool
3852 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3853 location_t switch_loc, location_t case_loc)
3855 bool saw_error;
3856 struct c_scope *scope;
3858 saw_error = false;
3859 for (scope = current_scope;
3860 scope != switch_bindings->scope;
3861 scope = scope->outer)
3863 struct c_binding *b;
3865 gcc_assert (scope != NULL);
3867 if (!scope->has_jump_unsafe_decl)
3868 continue;
3870 for (b = scope->bindings; b != NULL; b = b->prev)
3872 if (decl_jump_unsafe (b->decl))
3874 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3876 saw_error = true;
3877 error_at (case_loc,
3878 ("switch jumps into scope of identifier with "
3879 "variably modified type"));
3881 else
3882 warning_at (case_loc, OPT_Wjump_misses_init,
3883 "switch jumps over variable initialization");
3884 inform (switch_loc, "switch starts here");
3885 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3886 b->decl);
3891 if (switch_bindings->stmt_exprs > 0)
3893 saw_error = true;
3894 error_at (case_loc, "switch jumps into statement expression");
3895 inform (switch_loc, "switch starts here");
3898 return saw_error;
3901 /* Given NAME, an IDENTIFIER_NODE,
3902 return the structure (or union or enum) definition for that name.
3903 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3904 CODE says which kind of type the caller wants;
3905 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3906 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3907 location where the tag was defined.
3908 If the wrong kind of type is found, an error is reported. */
3910 static tree
3911 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3912 location_t *ploc)
3914 struct c_binding *b = I_TAG_BINDING (name);
3915 bool thislevel = false;
3917 if (!b || !b->decl)
3918 return NULL_TREE;
3920 /* We only care about whether it's in this level if
3921 thislevel_only was set or it might be a type clash. */
3922 if (thislevel_only || TREE_CODE (b->decl) != code)
3924 /* For our purposes, a tag in the external scope is the same as
3925 a tag in the file scope. (Primarily relevant to Objective-C
3926 and its builtin structure tags, which get pushed before the
3927 file scope is created.) */
3928 if (B_IN_CURRENT_SCOPE (b)
3929 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3930 thislevel = true;
3933 if (thislevel_only && !thislevel)
3934 return NULL_TREE;
3936 if (TREE_CODE (b->decl) != code)
3938 /* Definition isn't the kind we were looking for. */
3939 pending_invalid_xref = name;
3940 pending_invalid_xref_location = input_location;
3942 /* If in the same binding level as a declaration as a tag
3943 of a different type, this must not be allowed to
3944 shadow that tag, so give the error immediately.
3945 (For example, "struct foo; union foo;" is invalid.) */
3946 if (thislevel)
3947 pending_xref_error ();
3950 if (ploc != NULL)
3951 *ploc = b->locus;
3953 return b->decl;
3956 /* Return true if a definition exists for NAME with code CODE. */
3958 bool
3959 tag_exists_p (enum tree_code code, tree name)
3961 struct c_binding *b = I_TAG_BINDING (name);
3963 if (b == NULL || b->decl == NULL_TREE)
3964 return false;
3965 return TREE_CODE (b->decl) == code;
3968 /* Print an error message now
3969 for a recent invalid struct, union or enum cross reference.
3970 We don't print them immediately because they are not invalid
3971 when used in the `struct foo;' construct for shadowing. */
3973 void
3974 pending_xref_error (void)
3976 if (pending_invalid_xref != NULL_TREE)
3977 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3978 pending_invalid_xref);
3979 pending_invalid_xref = NULL_TREE;
3983 /* Look up NAME in the current scope and its superiors
3984 in the namespace of variables, functions and typedefs.
3985 Return a ..._DECL node of some kind representing its definition,
3986 or return NULL_TREE if it is undefined. */
3988 tree
3989 lookup_name (tree name)
3991 struct c_binding *b = I_SYMBOL_BINDING (name);
3992 if (b && !b->invisible)
3994 maybe_record_typedef_use (b->decl);
3995 return b->decl;
3997 return NULL_TREE;
4000 /* Similar to `lookup_name' but look only at the indicated scope. */
4002 static tree
4003 lookup_name_in_scope (tree name, struct c_scope *scope)
4005 struct c_binding *b;
4007 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4008 if (B_IN_SCOPE (b, scope))
4009 return b->decl;
4010 return NULL_TREE;
4013 /* Look for the closest match for NAME within the currently valid
4014 scopes.
4016 This finds the identifier with the lowest Levenshtein distance to
4017 NAME. If there are multiple candidates with equal minimal distance,
4018 the first one found is returned. Scopes are searched from innermost
4019 outwards, and within a scope in reverse order of declaration, thus
4020 benefiting candidates "near" to the current scope.
4022 The function also looks for similar macro names to NAME, since a
4023 misspelled macro name will not be expanded, and hence looks like an
4024 identifier to the C frontend.
4026 It also looks for start_typename keywords, to detect "singed" vs "signed"
4027 typos.
4029 Use LOC for any deferred diagnostics. */
4031 name_hint
4032 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4034 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4036 /* First, try some well-known names in the C standard library, in case
4037 the user forgot a #include. */
4038 const char *header_hint
4039 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4041 if (header_hint)
4042 return name_hint (NULL,
4043 new suggest_missing_header (loc,
4044 IDENTIFIER_POINTER (name),
4045 header_hint));
4047 /* Only suggest names reserved for the implementation if NAME begins
4048 with an underscore. */
4049 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4051 best_match<tree, tree> bm (name);
4053 /* Look within currently valid scopes. */
4054 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4055 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4057 if (!binding->id || binding->invisible)
4058 continue;
4059 if (binding->decl == error_mark_node)
4060 continue;
4061 /* Don't use bindings from implicitly declared functions,
4062 as they were likely misspellings themselves. */
4063 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4064 if (C_DECL_IMPLICIT (binding->decl))
4065 continue;
4066 /* Don't suggest names that are reserved for use by the
4067 implementation, unless NAME began with an underscore. */
4068 if (!consider_implementation_names)
4070 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4071 if (name_reserved_for_implementation_p (suggestion_str))
4072 continue;
4074 switch (kind)
4076 case FUZZY_LOOKUP_TYPENAME:
4077 if (TREE_CODE (binding->decl) != TYPE_DECL)
4078 continue;
4079 break;
4081 case FUZZY_LOOKUP_FUNCTION_NAME:
4082 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4084 /* Allow function pointers. */
4085 if ((VAR_P (binding->decl)
4086 || TREE_CODE (binding->decl) == PARM_DECL)
4087 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4088 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4089 == FUNCTION_TYPE))
4090 break;
4091 continue;
4093 break;
4095 default:
4096 break;
4098 bm.consider (binding->id);
4101 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4103 x = SOME_OTHER_MACRO (y);
4104 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4105 as a misspelled identifier.
4107 Use the best distance so far so that a candidate is only set if
4108 a macro is better than anything so far. This allows early rejection
4109 (without calculating the edit distance) of macro names that must have
4110 distance >= bm.get_best_distance (), and means that we only get a
4111 non-NULL result for best_macro_match if it's better than any of
4112 the identifiers already checked, which avoids needless creation
4113 of identifiers for macro hashnodes. */
4114 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4115 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4116 /* If a macro is the closest so far to NAME, use it, creating an
4117 identifier tree node for it. */
4118 if (best_macro)
4120 const char *id = (const char *)best_macro->ident.str;
4121 tree macro_as_identifier
4122 = get_identifier_with_length (id, best_macro->ident.len);
4123 bm.set_best_so_far (macro_as_identifier,
4124 bmm.get_best_distance (),
4125 bmm.get_best_candidate_length ());
4128 /* Try the "start_typename" keywords to detect
4129 "singed" vs "signed" typos. */
4130 if (kind == FUZZY_LOOKUP_TYPENAME)
4132 for (unsigned i = 0; i < num_c_common_reswords; i++)
4134 const c_common_resword *resword = &c_common_reswords[i];
4135 if (!c_keyword_starts_typename (resword->rid))
4136 continue;
4137 tree resword_identifier = ridpointers [resword->rid];
4138 if (!resword_identifier)
4139 continue;
4140 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4141 bm.consider (resword_identifier);
4145 tree best = bm.get_best_meaningful_candidate ();
4146 if (best)
4147 return name_hint (IDENTIFIER_POINTER (best), NULL);
4148 else
4149 return name_hint (NULL, NULL);
4153 /* Create the predefined scalar types of C,
4154 and some nodes representing standard constants (0, 1, (void *) 0).
4155 Initialize the global scope.
4156 Make definitions for built-in primitive functions. */
4158 void
4159 c_init_decl_processing (void)
4161 location_t save_loc = input_location;
4163 /* Initialize reserved words for parser. */
4164 c_parse_init ();
4166 current_function_decl = NULL_TREE;
4168 gcc_obstack_init (&parser_obstack);
4170 /* Make the externals scope. */
4171 push_scope ();
4172 external_scope = current_scope;
4174 /* Declarations from c_common_nodes_and_builtins must not be associated
4175 with this input file, lest we get differences between using and not
4176 using preprocessed headers. */
4177 input_location = BUILTINS_LOCATION;
4179 c_common_nodes_and_builtins ();
4181 /* In C, comparisons and TRUTH_* expressions have type int. */
4182 truthvalue_type_node = integer_type_node;
4183 truthvalue_true_node = integer_one_node;
4184 truthvalue_false_node = integer_zero_node;
4186 /* Even in C99, which has a real boolean type. */
4187 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4188 boolean_type_node));
4190 input_location = save_loc;
4192 make_fname_decl = c_make_fname_decl;
4193 start_fname_decls ();
4196 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4197 give the decl, NAME is the initialization string and TYPE_DEP
4198 indicates whether NAME depended on the type of the function. As we
4199 don't yet implement delayed emission of static data, we mark the
4200 decl as emitted so it is not placed in the output. Anything using
4201 it must therefore pull out the STRING_CST initializer directly.
4202 FIXME. */
4204 static tree
4205 c_make_fname_decl (location_t loc, tree id, int type_dep)
4207 const char *name = fname_as_string (type_dep);
4208 tree decl, type, init;
4209 size_t length = strlen (name);
4211 type = build_array_type (char_type_node,
4212 build_index_type (size_int (length)));
4213 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4215 decl = build_decl (loc, VAR_DECL, id, type);
4217 TREE_STATIC (decl) = 1;
4218 TREE_READONLY (decl) = 1;
4219 DECL_ARTIFICIAL (decl) = 1;
4221 init = build_string (length + 1, name);
4222 free (CONST_CAST (char *, name));
4223 TREE_TYPE (init) = type;
4224 DECL_INITIAL (decl) = init;
4226 TREE_USED (decl) = 1;
4228 if (current_function_decl
4229 /* For invalid programs like this:
4231 void foo()
4232 const char* p = __FUNCTION__;
4234 the __FUNCTION__ is believed to appear in K&R style function
4235 parameter declarator. In that case we still don't have
4236 function_scope. */
4237 && current_function_scope)
4239 DECL_CONTEXT (decl) = current_function_decl;
4240 bind (id, decl, current_function_scope,
4241 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4244 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4246 return decl;
4249 tree
4250 c_builtin_function (tree decl)
4252 tree type = TREE_TYPE (decl);
4253 tree id = DECL_NAME (decl);
4255 const char *name = IDENTIFIER_POINTER (id);
4256 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4258 /* Should never be called on a symbol with a preexisting meaning. */
4259 gcc_assert (!I_SYMBOL_BINDING (id));
4261 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4262 UNKNOWN_LOCATION);
4264 /* Builtins in the implementation namespace are made visible without
4265 needing to be explicitly declared. See push_file_scope. */
4266 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4268 DECL_CHAIN (decl) = visible_builtins;
4269 visible_builtins = decl;
4272 return decl;
4275 tree
4276 c_builtin_function_ext_scope (tree decl)
4278 tree type = TREE_TYPE (decl);
4279 tree id = DECL_NAME (decl);
4281 const char *name = IDENTIFIER_POINTER (id);
4282 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4284 if (external_scope)
4285 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4286 UNKNOWN_LOCATION);
4288 /* Builtins in the implementation namespace are made visible without
4289 needing to be explicitly declared. See push_file_scope. */
4290 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4292 DECL_CHAIN (decl) = visible_builtins;
4293 visible_builtins = decl;
4296 return decl;
4299 /* Called when a declaration is seen that contains no names to declare.
4300 If its type is a reference to a structure, union or enum inherited
4301 from a containing scope, shadow that tag name for the current scope
4302 with a forward reference.
4303 If its type defines a new named structure or union
4304 or defines an enum, it is valid but we need not do anything here.
4305 Otherwise, it is an error. */
4307 void
4308 shadow_tag (const struct c_declspecs *declspecs)
4310 shadow_tag_warned (declspecs, 0);
4313 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4314 but no pedwarn. */
4315 void
4316 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4318 bool found_tag = false;
4320 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4322 tree value = declspecs->type;
4323 enum tree_code code = TREE_CODE (value);
4325 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4326 /* Used to test also that TYPE_SIZE (value) != 0.
4327 That caused warning for `struct foo;' at top level in the file. */
4329 tree name = TYPE_NAME (value);
4330 tree t;
4332 found_tag = true;
4334 if (declspecs->restrict_p)
4336 error ("invalid use of %<restrict%>");
4337 warned = 1;
4340 if (name == NULL_TREE)
4342 if (warned != 1 && code != ENUMERAL_TYPE)
4343 /* Empty unnamed enum OK */
4345 pedwarn (input_location, 0,
4346 "unnamed struct/union that defines no instances");
4347 warned = 1;
4350 else if (declspecs->typespec_kind != ctsk_tagdef
4351 && declspecs->typespec_kind != ctsk_tagfirstref
4352 && declspecs->storage_class != csc_none)
4354 if (warned != 1)
4355 pedwarn (input_location, 0,
4356 "empty declaration with storage class specifier "
4357 "does not redeclare tag");
4358 warned = 1;
4359 pending_xref_error ();
4361 else if (declspecs->typespec_kind != ctsk_tagdef
4362 && declspecs->typespec_kind != ctsk_tagfirstref
4363 && (declspecs->const_p
4364 || declspecs->volatile_p
4365 || declspecs->atomic_p
4366 || declspecs->restrict_p
4367 || declspecs->address_space))
4369 if (warned != 1)
4370 pedwarn (input_location, 0,
4371 "empty declaration with type qualifier "
4372 "does not redeclare tag");
4373 warned = 1;
4374 pending_xref_error ();
4376 else if (declspecs->typespec_kind != ctsk_tagdef
4377 && declspecs->typespec_kind != ctsk_tagfirstref
4378 && declspecs->alignas_p)
4380 if (warned != 1)
4381 pedwarn (input_location, 0,
4382 "empty declaration with %<_Alignas%> "
4383 "does not redeclare tag");
4384 warned = 1;
4385 pending_xref_error ();
4387 else
4389 pending_invalid_xref = NULL_TREE;
4390 t = lookup_tag (code, name, true, NULL);
4392 if (t == NULL_TREE)
4394 t = make_node (code);
4395 pushtag (input_location, name, t);
4399 else
4401 if (warned != 1 && !in_system_header_at (input_location))
4403 pedwarn (input_location, 0,
4404 "useless type name in empty declaration");
4405 warned = 1;
4409 else if (warned != 1 && !in_system_header_at (input_location)
4410 && declspecs->typedef_p)
4412 pedwarn (input_location, 0, "useless type name in empty declaration");
4413 warned = 1;
4416 pending_invalid_xref = NULL_TREE;
4418 if (declspecs->inline_p)
4420 error ("%<inline%> in empty declaration");
4421 warned = 1;
4424 if (declspecs->noreturn_p)
4426 error ("%<_Noreturn%> in empty declaration");
4427 warned = 1;
4430 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4432 error ("%<auto%> in file-scope empty declaration");
4433 warned = 1;
4436 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4438 error ("%<register%> in file-scope empty declaration");
4439 warned = 1;
4442 if (!warned && !in_system_header_at (input_location)
4443 && declspecs->storage_class != csc_none)
4445 warning (0, "useless storage class specifier in empty declaration");
4446 warned = 2;
4449 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4451 warning (0, "useless %qs in empty declaration",
4452 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4453 warned = 2;
4456 if (!warned
4457 && !in_system_header_at (input_location)
4458 && (declspecs->const_p
4459 || declspecs->volatile_p
4460 || declspecs->atomic_p
4461 || declspecs->restrict_p
4462 || declspecs->address_space))
4464 warning (0, "useless type qualifier in empty declaration");
4465 warned = 2;
4468 if (!warned && !in_system_header_at (input_location)
4469 && declspecs->alignas_p)
4471 warning (0, "useless %<_Alignas%> in empty declaration");
4472 warned = 2;
4475 if (warned != 1)
4477 if (!found_tag)
4478 pedwarn (input_location, 0, "empty declaration");
4483 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4484 bits. SPECS represents declaration specifiers that the grammar
4485 only permits to contain type qualifiers and attributes. */
4488 quals_from_declspecs (const struct c_declspecs *specs)
4490 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4491 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4492 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4493 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4494 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4495 gcc_assert (!specs->type
4496 && !specs->decl_attr
4497 && specs->typespec_word == cts_none
4498 && specs->storage_class == csc_none
4499 && !specs->typedef_p
4500 && !specs->explicit_signed_p
4501 && !specs->deprecated_p
4502 && !specs->long_p
4503 && !specs->long_long_p
4504 && !specs->short_p
4505 && !specs->signed_p
4506 && !specs->unsigned_p
4507 && !specs->complex_p
4508 && !specs->inline_p
4509 && !specs->noreturn_p
4510 && !specs->thread_p);
4511 return quals;
4514 /* Construct an array declarator. LOC is the location of the
4515 beginning of the array (usually the opening brace). EXPR is the
4516 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4517 inside the [] (to be applied to the pointer to which a parameter
4518 array is converted). STATIC_P is true if "static" is inside the
4519 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4520 VLA of unspecified length which is nevertheless a complete type,
4521 false otherwise. The field for the contained declarator is left to
4522 be filled in by set_array_declarator_inner. */
4524 struct c_declarator *
4525 build_array_declarator (location_t loc,
4526 tree expr, struct c_declspecs *quals, bool static_p,
4527 bool vla_unspec_p)
4529 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4530 struct c_declarator);
4531 declarator->id_loc = loc;
4532 declarator->kind = cdk_array;
4533 declarator->declarator = 0;
4534 declarator->u.array.dimen = expr;
4535 if (quals)
4537 declarator->u.array.attrs = quals->attrs;
4538 declarator->u.array.quals = quals_from_declspecs (quals);
4540 else
4542 declarator->u.array.attrs = NULL_TREE;
4543 declarator->u.array.quals = 0;
4545 declarator->u.array.static_p = static_p;
4546 declarator->u.array.vla_unspec_p = vla_unspec_p;
4547 if (static_p || quals != NULL)
4548 pedwarn_c90 (loc, OPT_Wpedantic,
4549 "ISO C90 does not support %<static%> or type "
4550 "qualifiers in parameter array declarators");
4551 if (vla_unspec_p)
4552 pedwarn_c90 (loc, OPT_Wpedantic,
4553 "ISO C90 does not support %<[*]%> array declarators");
4554 if (vla_unspec_p)
4556 if (!current_scope->parm_flag)
4558 /* C99 6.7.5.2p4 */
4559 error_at (loc, "%<[*]%> not allowed in other than "
4560 "function prototype scope");
4561 declarator->u.array.vla_unspec_p = false;
4562 return NULL;
4564 current_scope->had_vla_unspec = true;
4566 return declarator;
4569 /* Set the contained declarator of an array declarator. DECL is the
4570 declarator, as constructed by build_array_declarator; INNER is what
4571 appears on the left of the []. */
4573 struct c_declarator *
4574 set_array_declarator_inner (struct c_declarator *decl,
4575 struct c_declarator *inner)
4577 decl->declarator = inner;
4578 return decl;
4581 /* INIT is a constructor that forms DECL's initializer. If the final
4582 element initializes a flexible array field, add the size of that
4583 initializer to DECL's size. */
4585 static void
4586 add_flexible_array_elts_to_size (tree decl, tree init)
4588 tree elt, type;
4590 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4591 return;
4593 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4594 type = TREE_TYPE (elt);
4595 if (TREE_CODE (type) == ARRAY_TYPE
4596 && TYPE_SIZE (type) == NULL_TREE
4597 && TYPE_DOMAIN (type) != NULL_TREE
4598 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4600 complete_array_type (&type, elt, false);
4601 DECL_SIZE (decl)
4602 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4603 DECL_SIZE_UNIT (decl)
4604 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4608 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4609 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4610 before the type name, and set *EXPR_CONST_OPERANDS, if
4611 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4612 appear in a constant expression. */
4614 tree
4615 groktypename (struct c_type_name *type_name, tree *expr,
4616 bool *expr_const_operands)
4618 tree type;
4619 tree attrs = type_name->specs->attrs;
4621 type_name->specs->attrs = NULL_TREE;
4623 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4624 false, NULL, &attrs, expr, expr_const_operands,
4625 DEPRECATED_NORMAL);
4627 /* Apply attributes. */
4628 decl_attributes (&type, attrs, 0);
4630 return type;
4633 /* Wrapper for decl_attributes that adds some implicit attributes
4634 to VAR_DECLs or FUNCTION_DECLs. */
4636 static tree
4637 c_decl_attributes (tree *node, tree attributes, int flags)
4639 /* Add implicit "omp declare target" attribute if requested. */
4640 if (current_omp_declare_target_attribute
4641 && ((VAR_P (*node) && is_global_var (*node))
4642 || TREE_CODE (*node) == FUNCTION_DECL))
4644 if (VAR_P (*node)
4645 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4646 error ("%q+D in declare target directive does not have mappable type",
4647 *node);
4648 else
4649 attributes = tree_cons (get_identifier ("omp declare target"),
4650 NULL_TREE, attributes);
4653 /* Look up the current declaration with all the attributes merged
4654 so far so that attributes on the current declaration that's
4655 about to be pushed that conflict with the former can be detected,
4656 diagnosed, and rejected as appropriate. */
4657 tree last_decl = lookup_name (DECL_NAME (*node));
4658 if (!last_decl)
4659 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4661 return decl_attributes (node, attributes, flags, last_decl);
4665 /* Decode a declarator in an ordinary declaration or data definition.
4666 This is called as soon as the type information and variable name
4667 have been parsed, before parsing the initializer if any.
4668 Here we create the ..._DECL node, fill in its type,
4669 and put it on the list of decls for the current context.
4670 The ..._DECL node is returned as the value.
4672 Exception: for arrays where the length is not specified,
4673 the type is left null, to be filled in by `finish_decl'.
4675 Function definitions do not come here; they go to start_function
4676 instead. However, external and forward declarations of functions
4677 do go through here. Structure field declarations are done by
4678 grokfield and not through here. */
4680 tree
4681 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4682 bool initialized, tree attributes)
4684 tree decl;
4685 tree tem;
4686 tree expr = NULL_TREE;
4687 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4689 /* An object declared as __attribute__((deprecated)) suppresses
4690 warnings of uses of other deprecated items. */
4691 if (lookup_attribute ("deprecated", attributes))
4692 deprecated_state = DEPRECATED_SUPPRESS;
4694 decl = grokdeclarator (declarator, declspecs,
4695 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4696 deprecated_state);
4697 if (!decl || decl == error_mark_node)
4698 return NULL_TREE;
4700 if (expr)
4701 add_stmt (fold_convert (void_type_node, expr));
4703 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4704 warning (OPT_Wmain, "%q+D is usually a function", decl);
4706 if (initialized)
4707 /* Is it valid for this decl to have an initializer at all?
4708 If not, set INITIALIZED to zero, which will indirectly
4709 tell 'finish_decl' to ignore the initializer once it is parsed. */
4710 switch (TREE_CODE (decl))
4712 case TYPE_DECL:
4713 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4714 initialized = false;
4715 break;
4717 case FUNCTION_DECL:
4718 error ("function %qD is initialized like a variable", decl);
4719 initialized = false;
4720 break;
4722 case PARM_DECL:
4723 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4724 error ("parameter %qD is initialized", decl);
4725 initialized = false;
4726 break;
4728 default:
4729 /* Don't allow initializations for incomplete types except for
4730 arrays which might be completed by the initialization. */
4732 /* This can happen if the array size is an undefined macro.
4733 We already gave a warning, so we don't need another one. */
4734 if (TREE_TYPE (decl) == error_mark_node)
4735 initialized = false;
4736 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4738 /* A complete type is ok if size is fixed. */
4740 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4741 || C_DECL_VARIABLE_SIZE (decl))
4743 error ("variable-sized object may not be initialized");
4744 initialized = false;
4747 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4749 error ("variable %qD has initializer but incomplete type", decl);
4750 initialized = false;
4752 else if (C_DECL_VARIABLE_SIZE (decl))
4754 /* Although C99 is unclear about whether incomplete arrays
4755 of VLAs themselves count as VLAs, it does not make
4756 sense to permit them to be initialized given that
4757 ordinary VLAs may not be initialized. */
4758 error ("variable-sized object may not be initialized");
4759 initialized = false;
4763 if (initialized)
4765 if (current_scope == file_scope)
4766 TREE_STATIC (decl) = 1;
4768 /* Tell 'pushdecl' this is an initialized decl
4769 even though we don't yet have the initializer expression.
4770 Also tell 'finish_decl' it may store the real initializer. */
4771 DECL_INITIAL (decl) = error_mark_node;
4774 /* If this is a function declaration, write a record describing it to the
4775 prototypes file (if requested). */
4777 if (TREE_CODE (decl) == FUNCTION_DECL)
4778 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4780 /* ANSI specifies that a tentative definition which is not merged with
4781 a non-tentative definition behaves exactly like a definition with an
4782 initializer equal to zero. (Section 3.7.2)
4784 -fno-common gives strict ANSI behavior, though this tends to break
4785 a large body of code that grew up without this rule.
4787 Thread-local variables are never common, since there's no entrenched
4788 body of code to break, and it allows more efficient variable references
4789 in the presence of dynamic linking. */
4791 if (VAR_P (decl)
4792 && !initialized
4793 && TREE_PUBLIC (decl)
4794 && !DECL_THREAD_LOCAL_P (decl)
4795 && !flag_no_common)
4796 DECL_COMMON (decl) = 1;
4798 /* Set attributes here so if duplicate decl, will have proper attributes. */
4799 c_decl_attributes (&decl, attributes, 0);
4801 /* Handle gnu_inline attribute. */
4802 if (declspecs->inline_p
4803 && !flag_gnu89_inline
4804 && TREE_CODE (decl) == FUNCTION_DECL
4805 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4806 || current_function_decl))
4808 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4810 else if (declspecs->storage_class != csc_static)
4811 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4814 if (TREE_CODE (decl) == FUNCTION_DECL
4815 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4817 struct c_declarator *ce = declarator;
4819 if (ce->kind == cdk_pointer)
4820 ce = declarator->declarator;
4821 if (ce->kind == cdk_function)
4823 tree args = ce->u.arg_info->parms;
4824 for (; args; args = DECL_CHAIN (args))
4826 tree type = TREE_TYPE (args);
4827 if (type && INTEGRAL_TYPE_P (type)
4828 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4829 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4834 if (TREE_CODE (decl) == FUNCTION_DECL
4835 && DECL_DECLARED_INLINE_P (decl)
4836 && DECL_UNINLINABLE (decl)
4837 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4838 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4839 decl);
4841 /* C99 6.7.4p3: An inline definition of a function with external
4842 linkage shall not contain a definition of a modifiable object
4843 with static storage duration... */
4844 if (VAR_P (decl)
4845 && current_scope != file_scope
4846 && TREE_STATIC (decl)
4847 && !TREE_READONLY (decl)
4848 && DECL_DECLARED_INLINE_P (current_function_decl)
4849 && DECL_EXTERNAL (current_function_decl))
4850 record_inline_static (input_location, current_function_decl,
4851 decl, csi_modifiable);
4853 if (c_dialect_objc ()
4854 && VAR_OR_FUNCTION_DECL_P (decl))
4855 objc_check_global_decl (decl);
4857 /* Add this decl to the current scope.
4858 TEM may equal DECL or it may be a previous decl of the same name. */
4859 tem = pushdecl (decl);
4861 if (initialized && DECL_EXTERNAL (tem))
4863 DECL_EXTERNAL (tem) = 0;
4864 TREE_STATIC (tem) = 1;
4867 return tem;
4870 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4871 DECL or the non-array element type if DECL is an uninitialized array.
4872 If that type has a const member, diagnose this. */
4874 static void
4875 diagnose_uninitialized_cst_member (tree decl, tree type)
4877 tree field;
4878 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4880 tree field_type;
4881 if (TREE_CODE (field) != FIELD_DECL)
4882 continue;
4883 field_type = strip_array_types (TREE_TYPE (field));
4885 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4887 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4888 "uninitialized const member in %qT is invalid in C++",
4889 strip_array_types (TREE_TYPE (decl)));
4890 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4893 if (RECORD_OR_UNION_TYPE_P (field_type))
4894 diagnose_uninitialized_cst_member (decl, field_type);
4898 /* Finish processing of a declaration;
4899 install its initial value.
4900 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4901 If the length of an array type is not known before,
4902 it must be determined now, from the initial value, or it is an error.
4904 INIT_LOC is the location of the initial value. */
4906 void
4907 finish_decl (tree decl, location_t init_loc, tree init,
4908 tree origtype, tree asmspec_tree)
4910 tree type;
4911 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4912 const char *asmspec = 0;
4914 /* If a name was specified, get the string. */
4915 if (VAR_OR_FUNCTION_DECL_P (decl)
4916 && DECL_FILE_SCOPE_P (decl))
4917 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4918 if (asmspec_tree)
4919 asmspec = TREE_STRING_POINTER (asmspec_tree);
4921 if (VAR_P (decl)
4922 && TREE_STATIC (decl)
4923 && global_bindings_p ())
4924 /* So decl is a global variable. Record the types it uses
4925 so that we can decide later to emit debug info for them. */
4926 record_types_used_by_current_var_decl (decl);
4928 /* If `start_decl' didn't like having an initialization, ignore it now. */
4929 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4930 init = NULL_TREE;
4932 /* Don't crash if parm is initialized. */
4933 if (TREE_CODE (decl) == PARM_DECL)
4934 init = NULL_TREE;
4936 if (init)
4937 store_init_value (init_loc, decl, init, origtype);
4939 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4940 || TREE_CODE (decl) == FIELD_DECL))
4941 objc_check_decl (decl);
4943 type = TREE_TYPE (decl);
4945 /* Deduce size of array from initialization, if not already known. */
4946 if (TREE_CODE (type) == ARRAY_TYPE
4947 && TYPE_DOMAIN (type) == NULL_TREE
4948 && TREE_CODE (decl) != TYPE_DECL)
4950 bool do_default
4951 = (TREE_STATIC (decl)
4952 /* Even if pedantic, an external linkage array
4953 may have incomplete type at first. */
4954 ? pedantic && !TREE_PUBLIC (decl)
4955 : !DECL_EXTERNAL (decl));
4956 int failure
4957 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4958 do_default);
4960 /* Get the completed type made by complete_array_type. */
4961 type = TREE_TYPE (decl);
4963 switch (failure)
4965 case 1:
4966 error ("initializer fails to determine size of %q+D", decl);
4967 break;
4969 case 2:
4970 if (do_default)
4971 error ("array size missing in %q+D", decl);
4972 /* If a `static' var's size isn't known,
4973 make it extern as well as static, so it does not get
4974 allocated.
4975 If it is not `static', then do not mark extern;
4976 finish_incomplete_decl will give it a default size
4977 and it will get allocated. */
4978 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4979 DECL_EXTERNAL (decl) = 1;
4980 break;
4982 case 3:
4983 error ("zero or negative size array %q+D", decl);
4984 break;
4986 case 0:
4987 /* For global variables, update the copy of the type that
4988 exists in the binding. */
4989 if (TREE_PUBLIC (decl))
4991 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4992 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4993 b_ext = b_ext->shadowed;
4994 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4996 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4997 b_ext->u.type = composite_type (b_ext->u.type, type);
4998 else
4999 b_ext->u.type = type;
5002 break;
5004 default:
5005 gcc_unreachable ();
5008 if (DECL_INITIAL (decl))
5009 TREE_TYPE (DECL_INITIAL (decl)) = type;
5011 relayout_decl (decl);
5014 if (VAR_P (decl))
5016 if (init && TREE_CODE (init) == CONSTRUCTOR)
5017 add_flexible_array_elts_to_size (decl, init);
5019 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5020 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5021 layout_decl (decl, 0);
5023 if (DECL_SIZE (decl) == NULL_TREE
5024 /* Don't give an error if we already gave one earlier. */
5025 && TREE_TYPE (decl) != error_mark_node
5026 && (TREE_STATIC (decl)
5027 /* A static variable with an incomplete type
5028 is an error if it is initialized.
5029 Also if it is not file scope.
5030 Otherwise, let it through, but if it is not `extern'
5031 then it may cause an error message later. */
5032 ? (DECL_INITIAL (decl) != NULL_TREE
5033 || !DECL_FILE_SCOPE_P (decl))
5034 /* An automatic variable with an incomplete type
5035 is an error. */
5036 : !DECL_EXTERNAL (decl)))
5038 error ("storage size of %q+D isn%'t known", decl);
5039 TREE_TYPE (decl) = error_mark_node;
5042 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5043 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5044 && DECL_SIZE (decl) == NULL_TREE
5045 && TREE_STATIC (decl))
5046 incomplete_record_decls.safe_push (decl);
5048 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5050 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5051 constant_expression_warning (DECL_SIZE (decl));
5052 else
5054 error ("storage size of %q+D isn%'t constant", decl);
5055 TREE_TYPE (decl) = error_mark_node;
5059 if (TREE_USED (type))
5061 TREE_USED (decl) = 1;
5062 DECL_READ_P (decl) = 1;
5066 /* If this is a function and an assembler name is specified, reset DECL_RTL
5067 so we can give it its new name. Also, update builtin_decl if it
5068 was a normal built-in. */
5069 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5071 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5072 set_builtin_user_assembler_name (decl, asmspec);
5073 set_user_assembler_name (decl, asmspec);
5076 /* If #pragma weak was used, mark the decl weak now. */
5077 maybe_apply_pragma_weak (decl);
5079 /* Output the assembler code and/or RTL code for variables and functions,
5080 unless the type is an undefined structure or union.
5081 If not, it will get done when the type is completed. */
5083 if (VAR_OR_FUNCTION_DECL_P (decl))
5085 /* Determine the ELF visibility. */
5086 if (TREE_PUBLIC (decl))
5087 c_determine_visibility (decl);
5089 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5090 if (c_dialect_objc ())
5091 objc_check_decl (decl);
5093 if (asmspec)
5095 /* If this is not a static variable, issue a warning.
5096 It doesn't make any sense to give an ASMSPEC for an
5097 ordinary, non-register local variable. Historically,
5098 GCC has accepted -- but ignored -- the ASMSPEC in
5099 this case. */
5100 if (!DECL_FILE_SCOPE_P (decl)
5101 && VAR_P (decl)
5102 && !C_DECL_REGISTER (decl)
5103 && !TREE_STATIC (decl))
5104 warning (0, "ignoring asm-specifier for non-static local "
5105 "variable %q+D", decl);
5106 else
5107 set_user_assembler_name (decl, asmspec);
5110 if (DECL_FILE_SCOPE_P (decl))
5112 if (DECL_INITIAL (decl) == NULL_TREE
5113 || DECL_INITIAL (decl) == error_mark_node)
5114 /* Don't output anything
5115 when a tentative file-scope definition is seen.
5116 But at end of compilation, do output code for them. */
5117 DECL_DEFER_OUTPUT (decl) = 1;
5118 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5119 DECL_HARD_REGISTER (decl) = 1;
5120 rest_of_decl_compilation (decl, true, 0);
5122 else
5124 /* In conjunction with an ASMSPEC, the `register'
5125 keyword indicates that we should place the variable
5126 in a particular register. */
5127 if (asmspec && C_DECL_REGISTER (decl))
5129 DECL_HARD_REGISTER (decl) = 1;
5130 /* This cannot be done for a structure with volatile
5131 fields, on which DECL_REGISTER will have been
5132 reset. */
5133 if (!DECL_REGISTER (decl))
5134 error ("cannot put object with volatile field into register");
5137 if (TREE_CODE (decl) != FUNCTION_DECL)
5139 /* If we're building a variable sized type, and we might be
5140 reachable other than via the top of the current binding
5141 level, then create a new BIND_EXPR so that we deallocate
5142 the object at the right time. */
5143 /* Note that DECL_SIZE can be null due to errors. */
5144 if (DECL_SIZE (decl)
5145 && !TREE_CONSTANT (DECL_SIZE (decl))
5146 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5148 tree bind;
5149 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5150 TREE_SIDE_EFFECTS (bind) = 1;
5151 add_stmt (bind);
5152 BIND_EXPR_BODY (bind) = push_stmt_list ();
5154 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5155 DECL_EXPR, decl));
5160 if (!DECL_FILE_SCOPE_P (decl))
5162 /* Recompute the RTL of a local array now
5163 if it used to be an incomplete type. */
5164 if (was_incomplete && !is_global_var (decl))
5166 /* If we used it already as memory, it must stay in memory. */
5167 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5168 /* If it's still incomplete now, no init will save it. */
5169 if (DECL_SIZE (decl) == NULL_TREE)
5170 DECL_INITIAL (decl) = NULL_TREE;
5175 if (TREE_CODE (decl) == TYPE_DECL)
5177 if (!DECL_FILE_SCOPE_P (decl)
5178 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5179 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5181 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5184 /* Install a cleanup (aka destructor) if one was given. */
5185 if (VAR_P (decl) && !TREE_STATIC (decl))
5187 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5188 if (attr)
5190 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5191 tree cleanup_decl = lookup_name (cleanup_id);
5192 tree cleanup;
5193 vec<tree, va_gc> *v;
5195 /* Build "cleanup(&decl)" for the destructor. */
5196 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5197 vec_alloc (v, 1);
5198 v->quick_push (cleanup);
5199 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5200 vNULL, cleanup_decl, v, NULL);
5201 vec_free (v);
5203 /* Don't warn about decl unused; the cleanup uses it. */
5204 TREE_USED (decl) = 1;
5205 TREE_USED (cleanup_decl) = 1;
5206 DECL_READ_P (decl) = 1;
5208 push_cleanup (decl, cleanup, false);
5212 if (warn_cxx_compat
5213 && VAR_P (decl)
5214 && !DECL_EXTERNAL (decl)
5215 && DECL_INITIAL (decl) == NULL_TREE)
5217 type = strip_array_types (type);
5218 if (TREE_READONLY (decl))
5219 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5220 "uninitialized const %qD is invalid in C++", decl);
5221 else if (RECORD_OR_UNION_TYPE_P (type)
5222 && C_TYPE_FIELDS_READONLY (type))
5223 diagnose_uninitialized_cst_member (decl, type);
5226 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5229 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5230 EXPR is NULL or a pointer to an expression that needs to be
5231 evaluated for the side effects of array size expressions in the
5232 parameters. */
5234 tree
5235 grokparm (const struct c_parm *parm, tree *expr)
5237 tree attrs = parm->attrs;
5238 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5239 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5241 decl_attributes (&decl, attrs, 0);
5243 return decl;
5246 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5247 and push that on the current scope. EXPR is a pointer to an
5248 expression that needs to be evaluated for the side effects of array
5249 size expressions in the parameters. */
5251 void
5252 push_parm_decl (const struct c_parm *parm, tree *expr)
5254 tree attrs = parm->attrs;
5255 tree decl;
5257 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5258 &attrs, expr, NULL, DEPRECATED_NORMAL);
5259 if (decl && DECL_P (decl))
5260 DECL_SOURCE_LOCATION (decl) = parm->loc;
5261 decl_attributes (&decl, attrs, 0);
5263 decl = pushdecl (decl);
5265 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5268 /* Mark all the parameter declarations to date as forward decls.
5269 Also diagnose use of this extension. */
5271 void
5272 mark_forward_parm_decls (void)
5274 struct c_binding *b;
5276 if (pedantic && !current_scope->warned_forward_parm_decls)
5278 pedwarn (input_location, OPT_Wpedantic,
5279 "ISO C forbids forward parameter declarations");
5280 current_scope->warned_forward_parm_decls = true;
5283 for (b = current_scope->bindings; b; b = b->prev)
5284 if (TREE_CODE (b->decl) == PARM_DECL)
5285 TREE_ASM_WRITTEN (b->decl) = 1;
5288 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5289 literal, which may be an incomplete array type completed by the
5290 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5291 literal. NON_CONST is true if the initializers contain something
5292 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5293 it is the (valid) alignment for this compound literal, as specified
5294 with _Alignas. */
5296 tree
5297 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5298 unsigned int alignas_align)
5300 /* We do not use start_decl here because we have a type, not a declarator;
5301 and do not use finish_decl because the decl should be stored inside
5302 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5303 tree decl;
5304 tree complit;
5305 tree stmt;
5307 if (type == error_mark_node
5308 || init == error_mark_node)
5309 return error_mark_node;
5311 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5312 DECL_EXTERNAL (decl) = 0;
5313 TREE_PUBLIC (decl) = 0;
5314 TREE_STATIC (decl) = (current_scope == file_scope);
5315 DECL_CONTEXT (decl) = current_function_decl;
5316 TREE_USED (decl) = 1;
5317 DECL_READ_P (decl) = 1;
5318 DECL_ARTIFICIAL (decl) = 1;
5319 DECL_IGNORED_P (decl) = 1;
5320 TREE_TYPE (decl) = type;
5321 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5322 if (alignas_align)
5324 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5325 DECL_USER_ALIGN (decl) = 1;
5327 store_init_value (loc, decl, init, NULL_TREE);
5329 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5331 int failure = complete_array_type (&TREE_TYPE (decl),
5332 DECL_INITIAL (decl), true);
5333 /* If complete_array_type returns 3, it means that the
5334 initial value of the compound literal is empty. Allow it. */
5335 gcc_assert (failure == 0 || failure == 3);
5337 type = TREE_TYPE (decl);
5338 TREE_TYPE (DECL_INITIAL (decl)) = type;
5341 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5343 c_incomplete_type_error (loc, NULL_TREE, type);
5344 return error_mark_node;
5347 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5348 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5349 TREE_SIDE_EFFECTS (complit) = 1;
5351 layout_decl (decl, 0);
5353 if (TREE_STATIC (decl))
5355 /* This decl needs a name for the assembler output. */
5356 set_compound_literal_name (decl);
5357 DECL_DEFER_OUTPUT (decl) = 1;
5358 DECL_COMDAT (decl) = 1;
5359 pushdecl (decl);
5360 rest_of_decl_compilation (decl, 1, 0);
5362 else
5363 pushdecl (decl);
5365 if (non_const)
5367 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5368 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5371 return complit;
5374 /* Check the type of a compound literal. Here we just check that it
5375 is valid for C++. */
5377 void
5378 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5380 if (warn_cxx_compat
5381 && (type_name->specs->typespec_kind == ctsk_tagdef
5382 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5383 warning_at (loc, OPT_Wc___compat,
5384 "defining a type in a compound literal is invalid in C++");
5387 /* Determine whether TYPE is a structure with a flexible array member,
5388 or a union containing such a structure (possibly recursively). */
5390 static bool
5391 flexible_array_type_p (tree type)
5393 tree x;
5394 switch (TREE_CODE (type))
5396 case RECORD_TYPE:
5397 x = TYPE_FIELDS (type);
5398 if (x == NULL_TREE)
5399 return false;
5400 while (DECL_CHAIN (x) != NULL_TREE)
5401 x = DECL_CHAIN (x);
5402 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5403 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5404 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5405 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5406 return true;
5407 return false;
5408 case UNION_TYPE:
5409 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5411 if (flexible_array_type_p (TREE_TYPE (x)))
5412 return true;
5414 return false;
5415 default:
5416 return false;
5420 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5421 replacing with appropriate values if they are invalid. */
5423 static void
5424 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5425 tree orig_name)
5427 tree type_mv;
5428 unsigned int max_width;
5429 unsigned HOST_WIDE_INT w;
5430 const char *name = (orig_name
5431 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5432 : _("<anonymous>"));
5434 /* Detect and ignore out of range field width and process valid
5435 field widths. */
5436 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5438 error_at (loc, "bit-field %qs width not an integer constant", name);
5439 *width = integer_one_node;
5441 else
5443 if (TREE_CODE (*width) != INTEGER_CST)
5445 *width = c_fully_fold (*width, false, NULL);
5446 if (TREE_CODE (*width) == INTEGER_CST)
5447 pedwarn (loc, OPT_Wpedantic,
5448 "bit-field %qs width not an integer constant expression",
5449 name);
5451 if (TREE_CODE (*width) != INTEGER_CST)
5453 error_at (loc, "bit-field %qs width not an integer constant", name);
5454 *width = integer_one_node;
5456 constant_expression_warning (*width);
5457 if (tree_int_cst_sgn (*width) < 0)
5459 error_at (loc, "negative width in bit-field %qs", name);
5460 *width = integer_one_node;
5462 else if (integer_zerop (*width) && orig_name)
5464 error_at (loc, "zero width for bit-field %qs", name);
5465 *width = integer_one_node;
5469 /* Detect invalid bit-field type. */
5470 if (TREE_CODE (*type) != INTEGER_TYPE
5471 && TREE_CODE (*type) != BOOLEAN_TYPE
5472 && TREE_CODE (*type) != ENUMERAL_TYPE)
5474 error_at (loc, "bit-field %qs has invalid type", name);
5475 *type = unsigned_type_node;
5478 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5480 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5481 name);
5482 *type = unsigned_type_node;
5485 type_mv = TYPE_MAIN_VARIANT (*type);
5486 if (!in_system_header_at (input_location)
5487 && type_mv != integer_type_node
5488 && type_mv != unsigned_type_node
5489 && type_mv != boolean_type_node)
5490 pedwarn_c90 (loc, OPT_Wpedantic,
5491 "type of bit-field %qs is a GCC extension", name);
5493 max_width = TYPE_PRECISION (*type);
5495 if (compare_tree_int (*width, max_width) > 0)
5497 error_at (loc, "width of %qs exceeds its type", name);
5498 w = max_width;
5499 *width = build_int_cst (integer_type_node, w);
5501 else
5502 w = tree_to_uhwi (*width);
5504 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5506 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5507 if (!lt
5508 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5509 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5510 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5516 /* Print warning about variable length array if necessary. */
5518 static void
5519 warn_variable_length_array (tree name, tree size)
5521 if (TREE_CONSTANT (size))
5523 if (name)
5524 pedwarn_c90 (input_location, OPT_Wvla,
5525 "ISO C90 forbids array %qE whose size "
5526 "can%'t be evaluated", name);
5527 else
5528 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5529 "whose size can%'t be evaluated");
5531 else
5533 if (name)
5534 pedwarn_c90 (input_location, OPT_Wvla,
5535 "ISO C90 forbids variable length array %qE", name);
5536 else
5537 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5538 "length array");
5542 /* Print warning about defaulting to int if necessary. */
5544 static void
5545 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5547 diagnostic_info diagnostic;
5548 va_list ap;
5549 rich_location richloc (line_table, location);
5551 va_start (ap, gmsgid);
5552 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5553 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5554 diagnostic.option_index = opt;
5555 diagnostic_report_diagnostic (global_dc, &diagnostic);
5556 va_end (ap);
5559 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5560 considering only those c_declspec_words found in LIST, which
5561 must be terminated by cdw_number_of_elements. */
5563 static location_t
5564 smallest_type_quals_location (const location_t *locations,
5565 const c_declspec_word *list)
5567 location_t loc = UNKNOWN_LOCATION;
5568 while (*list != cdw_number_of_elements)
5570 location_t newloc = locations[*list];
5571 if (loc == UNKNOWN_LOCATION
5572 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5573 loc = newloc;
5574 list++;
5577 return loc;
5580 /* Given declspecs and a declarator,
5581 determine the name and type of the object declared
5582 and construct a ..._DECL node for it.
5583 (In one case we can return a ..._TYPE node instead.
5584 For invalid input we sometimes return NULL_TREE.)
5586 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5588 DECL_CONTEXT says which syntactic context this declaration is in:
5589 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5590 FUNCDEF for a function definition. Like NORMAL but a few different
5591 error messages in each case. Return value may be zero meaning
5592 this definition is too screwy to try to parse.
5593 PARM for a parameter declaration (either within a function prototype
5594 or before a function body). Make a PARM_DECL, or return void_type_node.
5595 TYPENAME if for a typename (in a cast or sizeof).
5596 Don't make a DECL node; just return the ..._TYPE node.
5597 FIELD for a struct or union field; make a FIELD_DECL.
5598 INITIALIZED is true if the decl has an initializer.
5599 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5600 representing the width of the bit-field.
5601 DECL_ATTRS points to the list of attributes that should be added to this
5602 decl. Any nested attributes that belong on the decl itself will be
5603 added to this list.
5604 If EXPR is not NULL, any expressions that need to be evaluated as
5605 part of evaluating variably modified types will be stored in *EXPR.
5606 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5607 set to indicate whether operands in *EXPR can be used in constant
5608 expressions.
5609 DEPRECATED_STATE is a deprecated_states value indicating whether
5610 deprecation warnings should be suppressed.
5612 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5613 It may also be so in the PARM case, for a prototype where the
5614 argument type is specified but not the name.
5616 This function is where the complicated C meanings of `static'
5617 and `extern' are interpreted. */
5619 static tree
5620 grokdeclarator (const struct c_declarator *declarator,
5621 struct c_declspecs *declspecs,
5622 enum decl_context decl_context, bool initialized, tree *width,
5623 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5624 enum deprecated_states deprecated_state)
5626 tree type = declspecs->type;
5627 bool threadp = declspecs->thread_p;
5628 enum c_storage_class storage_class = declspecs->storage_class;
5629 int constp;
5630 int restrictp;
5631 int volatilep;
5632 int atomicp;
5633 int type_quals = TYPE_UNQUALIFIED;
5634 tree name = NULL_TREE;
5635 bool funcdef_flag = false;
5636 bool funcdef_syntax = false;
5637 bool size_varies = false;
5638 tree decl_attr = declspecs->decl_attr;
5639 int array_ptr_quals = TYPE_UNQUALIFIED;
5640 tree array_ptr_attrs = NULL_TREE;
5641 bool array_parm_static = false;
5642 bool array_parm_vla_unspec_p = false;
5643 tree returned_attrs = NULL_TREE;
5644 bool bitfield = width != NULL;
5645 tree element_type;
5646 tree orig_qual_type = NULL;
5647 size_t orig_qual_indirect = 0;
5648 struct c_arg_info *arg_info = 0;
5649 addr_space_t as1, as2, address_space;
5650 location_t loc = UNKNOWN_LOCATION;
5651 tree expr_dummy;
5652 bool expr_const_operands_dummy;
5653 enum c_declarator_kind first_non_attr_kind;
5654 unsigned int alignas_align = 0;
5656 if (TREE_CODE (type) == ERROR_MARK)
5657 return error_mark_node;
5658 if (expr == NULL)
5660 expr = &expr_dummy;
5661 expr_dummy = NULL_TREE;
5663 if (expr_const_operands == NULL)
5664 expr_const_operands = &expr_const_operands_dummy;
5666 if (declspecs->expr)
5668 if (*expr)
5669 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5670 declspecs->expr);
5671 else
5672 *expr = declspecs->expr;
5674 *expr_const_operands = declspecs->expr_const_operands;
5676 if (decl_context == FUNCDEF)
5677 funcdef_flag = true, decl_context = NORMAL;
5679 /* Look inside a declarator for the name being declared
5680 and get it as an IDENTIFIER_NODE, for an error message. */
5682 const struct c_declarator *decl = declarator;
5684 first_non_attr_kind = cdk_attrs;
5685 while (decl)
5686 switch (decl->kind)
5688 case cdk_array:
5689 loc = decl->id_loc;
5690 /* FALL THRU. */
5692 case cdk_function:
5693 case cdk_pointer:
5694 funcdef_syntax = (decl->kind == cdk_function);
5695 if (first_non_attr_kind == cdk_attrs)
5696 first_non_attr_kind = decl->kind;
5697 decl = decl->declarator;
5698 break;
5700 case cdk_attrs:
5701 decl = decl->declarator;
5702 break;
5704 case cdk_id:
5705 loc = decl->id_loc;
5706 if (decl->u.id)
5707 name = decl->u.id;
5708 if (first_non_attr_kind == cdk_attrs)
5709 first_non_attr_kind = decl->kind;
5710 decl = 0;
5711 break;
5713 default:
5714 gcc_unreachable ();
5716 if (name == NULL_TREE)
5718 gcc_assert (decl_context == PARM
5719 || decl_context == TYPENAME
5720 || (decl_context == FIELD
5721 && declarator->kind == cdk_id));
5722 gcc_assert (!initialized);
5726 /* A function definition's declarator must have the form of
5727 a function declarator. */
5729 if (funcdef_flag && !funcdef_syntax)
5730 return NULL_TREE;
5732 /* If this looks like a function definition, make it one,
5733 even if it occurs where parms are expected.
5734 Then store_parm_decls will reject it and not use it as a parm. */
5735 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5736 decl_context = PARM;
5738 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5739 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5741 if ((decl_context == NORMAL || decl_context == FIELD)
5742 && current_scope == file_scope
5743 && variably_modified_type_p (type, NULL_TREE))
5745 if (name)
5746 error_at (loc, "variably modified %qE at file scope", name);
5747 else
5748 error_at (loc, "variably modified field at file scope");
5749 type = integer_type_node;
5752 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5754 /* Diagnose defaulting to "int". */
5756 if (declspecs->default_int_p && !in_system_header_at (input_location))
5758 /* Issue a warning if this is an ISO C 99 program or if
5759 -Wreturn-type and this is a function, or if -Wimplicit;
5760 prefer the former warning since it is more explicit. */
5761 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5762 && funcdef_flag)
5763 warn_about_return_type = 1;
5764 else
5766 if (name)
5767 warn_defaults_to (loc, OPT_Wimplicit_int,
5768 "type defaults to %<int%> in declaration "
5769 "of %qE", name);
5770 else
5771 warn_defaults_to (loc, OPT_Wimplicit_int,
5772 "type defaults to %<int%> in type name");
5776 /* Adjust the type if a bit-field is being declared,
5777 -funsigned-bitfields applied and the type is not explicitly
5778 "signed". */
5779 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5780 && TREE_CODE (type) == INTEGER_TYPE)
5781 type = unsigned_type_for (type);
5783 /* Figure out the type qualifiers for the declaration. There are
5784 two ways a declaration can become qualified. One is something
5785 like `const int i' where the `const' is explicit. Another is
5786 something like `typedef const int CI; CI i' where the type of the
5787 declaration contains the `const'. A third possibility is that
5788 there is a type qualifier on the element type of a typedefed
5789 array type, in which case we should extract that qualifier so
5790 that c_apply_type_quals_to_decl receives the full list of
5791 qualifiers to work with (C90 is not entirely clear about whether
5792 duplicate qualifiers should be diagnosed in this case, but it
5793 seems most appropriate to do so). */
5794 element_type = strip_array_types (type);
5795 constp = declspecs->const_p + TYPE_READONLY (element_type);
5796 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5797 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5798 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5799 as1 = declspecs->address_space;
5800 as2 = TYPE_ADDR_SPACE (element_type);
5801 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5803 if (constp > 1)
5804 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5805 if (restrictp > 1)
5806 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5807 if (volatilep > 1)
5808 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5809 if (atomicp > 1)
5810 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5812 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5813 error_at (loc, "conflicting named address spaces (%s vs %s)",
5814 c_addr_space_name (as1), c_addr_space_name (as2));
5816 if ((TREE_CODE (type) == ARRAY_TYPE
5817 || first_non_attr_kind == cdk_array)
5818 && TYPE_QUALS (element_type))
5820 orig_qual_type = type;
5821 type = TYPE_MAIN_VARIANT (type);
5823 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5824 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5825 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5826 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5827 | ENCODE_QUAL_ADDR_SPACE (address_space));
5828 if (type_quals != TYPE_QUALS (element_type))
5829 orig_qual_type = NULL_TREE;
5831 /* Applying the _Atomic qualifier to an array type (through the use
5832 of typedefs or typeof) must be detected here. If the qualifier
5833 is introduced later, any appearance of applying it to an array is
5834 actually applying it to an element of that array. */
5835 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5836 error_at (loc, "%<_Atomic%>-qualified array type");
5838 /* Warn about storage classes that are invalid for certain
5839 kinds of declarations (parameters, typenames, etc.). */
5841 if (funcdef_flag
5842 && (threadp
5843 || storage_class == csc_auto
5844 || storage_class == csc_register
5845 || storage_class == csc_typedef))
5847 if (storage_class == csc_auto)
5848 pedwarn (loc,
5849 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5850 "function definition declared %<auto%>");
5851 if (storage_class == csc_register)
5852 error_at (loc, "function definition declared %<register%>");
5853 if (storage_class == csc_typedef)
5854 error_at (loc, "function definition declared %<typedef%>");
5855 if (threadp)
5856 error_at (loc, "function definition declared %qs",
5857 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5858 threadp = false;
5859 if (storage_class == csc_auto
5860 || storage_class == csc_register
5861 || storage_class == csc_typedef)
5862 storage_class = csc_none;
5864 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5866 if (decl_context == PARM && storage_class == csc_register)
5868 else
5870 switch (decl_context)
5872 case FIELD:
5873 if (name)
5874 error_at (loc, "storage class specified for structure "
5875 "field %qE", name);
5876 else
5877 error_at (loc, "storage class specified for structure field");
5878 break;
5879 case PARM:
5880 if (name)
5881 error_at (loc, "storage class specified for parameter %qE",
5882 name);
5883 else
5884 error_at (loc, "storage class specified for unnamed parameter");
5885 break;
5886 default:
5887 error_at (loc, "storage class specified for typename");
5888 break;
5890 storage_class = csc_none;
5891 threadp = false;
5894 else if (storage_class == csc_extern
5895 && initialized
5896 && !funcdef_flag)
5898 /* 'extern' with initialization is invalid if not at file scope. */
5899 if (current_scope == file_scope)
5901 /* It is fine to have 'extern const' when compiling at C
5902 and C++ intersection. */
5903 if (!(warn_cxx_compat && constp))
5904 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5905 name);
5907 else
5908 error_at (loc, "%qE has both %<extern%> and initializer", name);
5910 else if (current_scope == file_scope)
5912 if (storage_class == csc_auto)
5913 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5914 name);
5915 if (pedantic && storage_class == csc_register)
5916 pedwarn (input_location, OPT_Wpedantic,
5917 "file-scope declaration of %qE specifies %<register%>", name);
5919 else
5921 if (storage_class == csc_extern && funcdef_flag)
5922 error_at (loc, "nested function %qE declared %<extern%>", name);
5923 else if (threadp && storage_class == csc_none)
5925 error_at (loc, "function-scope %qE implicitly auto and declared "
5926 "%qs", name,
5927 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5928 threadp = false;
5932 /* Now figure out the structure of the declarator proper.
5933 Descend through it, creating more complex types, until we reach
5934 the declared identifier (or NULL_TREE, in an absolute declarator).
5935 At each stage we maintain an unqualified version of the type
5936 together with any qualifiers that should be applied to it with
5937 c_build_qualified_type; this way, array types including
5938 multidimensional array types are first built up in unqualified
5939 form and then the qualified form is created with
5940 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5942 while (declarator && declarator->kind != cdk_id)
5944 if (type == error_mark_node)
5946 declarator = declarator->declarator;
5947 continue;
5950 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5951 a cdk_pointer (for *...),
5952 a cdk_function (for ...(...)),
5953 a cdk_attrs (for nested attributes),
5954 or a cdk_id (for the name being declared
5955 or the place in an absolute declarator
5956 where the name was omitted).
5957 For the last case, we have just exited the loop.
5959 At this point, TYPE is the type of elements of an array,
5960 or for a function to return, or for a pointer to point to.
5961 After this sequence of ifs, TYPE is the type of the
5962 array or function or pointer, and DECLARATOR has had its
5963 outermost layer removed. */
5965 if (array_ptr_quals != TYPE_UNQUALIFIED
5966 || array_ptr_attrs != NULL_TREE
5967 || array_parm_static)
5969 /* Only the innermost declarator (making a parameter be of
5970 array type which is converted to pointer type)
5971 may have static or type qualifiers. */
5972 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5973 array_ptr_quals = TYPE_UNQUALIFIED;
5974 array_ptr_attrs = NULL_TREE;
5975 array_parm_static = false;
5978 switch (declarator->kind)
5980 case cdk_attrs:
5982 /* A declarator with embedded attributes. */
5983 tree attrs = declarator->u.attrs;
5984 const struct c_declarator *inner_decl;
5985 int attr_flags = 0;
5986 declarator = declarator->declarator;
5987 inner_decl = declarator;
5988 while (inner_decl->kind == cdk_attrs)
5989 inner_decl = inner_decl->declarator;
5990 if (inner_decl->kind == cdk_id)
5991 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5992 else if (inner_decl->kind == cdk_function)
5993 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5994 else if (inner_decl->kind == cdk_array)
5995 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5996 returned_attrs = decl_attributes (&type,
5997 chainon (returned_attrs, attrs),
5998 attr_flags);
5999 break;
6001 case cdk_array:
6003 tree itype = NULL_TREE;
6004 tree size = declarator->u.array.dimen;
6005 /* The index is a signed object `sizetype' bits wide. */
6006 tree index_type = c_common_signed_type (sizetype);
6008 array_ptr_quals = declarator->u.array.quals;
6009 array_ptr_attrs = declarator->u.array.attrs;
6010 array_parm_static = declarator->u.array.static_p;
6011 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6013 declarator = declarator->declarator;
6015 /* Check for some types that there cannot be arrays of. */
6017 if (VOID_TYPE_P (type))
6019 if (name)
6020 error_at (loc, "declaration of %qE as array of voids", name);
6021 else
6022 error_at (loc, "declaration of type name as array of voids");
6023 type = error_mark_node;
6026 if (TREE_CODE (type) == FUNCTION_TYPE)
6028 if (name)
6029 error_at (loc, "declaration of %qE as array of functions",
6030 name);
6031 else
6032 error_at (loc, "declaration of type name as array of "
6033 "functions");
6034 type = error_mark_node;
6037 if (pedantic && !in_system_header_at (input_location)
6038 && flexible_array_type_p (type))
6039 pedwarn (loc, OPT_Wpedantic,
6040 "invalid use of structure with flexible array member");
6042 if (size == error_mark_node)
6043 type = error_mark_node;
6045 if (type == error_mark_node)
6046 continue;
6048 /* If size was specified, set ITYPE to a range-type for
6049 that size. Otherwise, ITYPE remains null. finish_decl
6050 may figure it out from an initial value. */
6052 if (size)
6054 bool size_maybe_const = true;
6055 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6056 && !TREE_OVERFLOW (size));
6057 bool this_size_varies = false;
6059 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6060 lvalue. */
6061 STRIP_TYPE_NOPS (size);
6063 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6065 if (name)
6066 error_at (loc, "size of array %qE has non-integer type",
6067 name);
6068 else
6069 error_at (loc,
6070 "size of unnamed array has non-integer type");
6071 size = integer_one_node;
6073 /* This can happen with enum forward declaration. */
6074 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6076 if (name)
6077 error_at (loc, "size of array %qE has incomplete type",
6078 name);
6079 else
6080 error_at (loc, "size of unnamed array has incomplete "
6081 "type");
6082 size = integer_one_node;
6085 size = c_fully_fold (size, false, &size_maybe_const);
6087 if (pedantic && size_maybe_const && integer_zerop (size))
6089 if (name)
6090 pedwarn (loc, OPT_Wpedantic,
6091 "ISO C forbids zero-size array %qE", name);
6092 else
6093 pedwarn (loc, OPT_Wpedantic,
6094 "ISO C forbids zero-size array");
6097 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6099 constant_expression_warning (size);
6100 if (tree_int_cst_sgn (size) < 0)
6102 if (name)
6103 error_at (loc, "size of array %qE is negative", name);
6104 else
6105 error_at (loc, "size of unnamed array is negative");
6106 size = integer_one_node;
6108 /* Handle a size folded to an integer constant but
6109 not an integer constant expression. */
6110 if (!size_int_const)
6112 /* If this is a file scope declaration of an
6113 ordinary identifier, this is invalid code;
6114 diagnosing it here and not subsequently
6115 treating the type as variable-length avoids
6116 more confusing diagnostics later. */
6117 if ((decl_context == NORMAL || decl_context == FIELD)
6118 && current_scope == file_scope)
6119 pedwarn (input_location, 0,
6120 "variably modified %qE at file scope",
6121 name);
6122 else
6123 this_size_varies = size_varies = true;
6124 warn_variable_length_array (name, size);
6127 else if ((decl_context == NORMAL || decl_context == FIELD)
6128 && current_scope == file_scope)
6130 error_at (loc, "variably modified %qE at file scope", name);
6131 size = integer_one_node;
6133 else
6135 /* Make sure the array size remains visibly
6136 nonconstant even if it is (eg) a const variable
6137 with known value. */
6138 this_size_varies = size_varies = true;
6139 warn_variable_length_array (name, size);
6140 if (sanitize_flags_p (SANITIZE_VLA)
6141 && current_function_decl != NULL_TREE
6142 && decl_context == NORMAL)
6144 /* Evaluate the array size only once. */
6145 size = save_expr (size);
6146 size = c_fully_fold (size, false, NULL);
6147 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6148 ubsan_instrument_vla (loc, size),
6149 size);
6153 if (integer_zerop (size) && !this_size_varies)
6155 /* A zero-length array cannot be represented with
6156 an unsigned index type, which is what we'll
6157 get with build_index_type. Create an
6158 open-ended range instead. */
6159 itype = build_range_type (sizetype, size, NULL_TREE);
6161 else
6163 /* Arrange for the SAVE_EXPR on the inside of the
6164 MINUS_EXPR, which allows the -1 to get folded
6165 with the +1 that happens when building TYPE_SIZE. */
6166 if (size_varies)
6167 size = save_expr (size);
6168 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6169 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6170 integer_zero_node, size);
6172 /* Compute the maximum valid index, that is, size
6173 - 1. Do the calculation in index_type, so that
6174 if it is a variable the computations will be
6175 done in the proper mode. */
6176 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6177 convert (index_type, size),
6178 convert (index_type,
6179 size_one_node));
6181 /* The above overflows when size does not fit
6182 in index_type.
6183 ??? While a size of INT_MAX+1 technically shouldn't
6184 cause an overflow (because we subtract 1), handling
6185 this case seems like an unnecessary complication. */
6186 if (TREE_CODE (size) == INTEGER_CST
6187 && !int_fits_type_p (size, index_type))
6189 if (name)
6190 error_at (loc, "size of array %qE is too large",
6191 name);
6192 else
6193 error_at (loc, "size of unnamed array is too large");
6194 type = error_mark_node;
6195 continue;
6198 itype = build_index_type (itype);
6200 if (this_size_varies)
6202 if (*expr)
6203 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6204 *expr, size);
6205 else
6206 *expr = size;
6207 *expr_const_operands &= size_maybe_const;
6210 else if (decl_context == FIELD)
6212 bool flexible_array_member = false;
6213 if (array_parm_vla_unspec_p)
6214 /* Field names can in fact have function prototype
6215 scope so [*] is disallowed here through making
6216 the field variably modified, not through being
6217 something other than a declaration with function
6218 prototype scope. */
6219 size_varies = true;
6220 else
6222 const struct c_declarator *t = declarator;
6223 while (t->kind == cdk_attrs)
6224 t = t->declarator;
6225 flexible_array_member = (t->kind == cdk_id);
6227 if (flexible_array_member
6228 && !in_system_header_at (input_location))
6229 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6230 "support flexible array members");
6232 /* ISO C99 Flexible array members are effectively
6233 identical to GCC's zero-length array extension. */
6234 if (flexible_array_member || array_parm_vla_unspec_p)
6235 itype = build_range_type (sizetype, size_zero_node,
6236 NULL_TREE);
6238 else if (decl_context == PARM)
6240 if (array_parm_vla_unspec_p)
6242 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6243 size_varies = true;
6246 else if (decl_context == TYPENAME)
6248 if (array_parm_vla_unspec_p)
6250 /* C99 6.7.5.2p4 */
6251 warning (0, "%<[*]%> not in a declaration");
6252 /* We use this to avoid messing up with incomplete
6253 array types of the same type, that would
6254 otherwise be modified below. */
6255 itype = build_range_type (sizetype, size_zero_node,
6256 NULL_TREE);
6257 size_varies = true;
6261 /* Complain about arrays of incomplete types. */
6262 if (!COMPLETE_TYPE_P (type))
6264 error_at (loc, "array type has incomplete element type %qT",
6265 type);
6266 /* See if we can be more helpful. */
6267 if (TREE_CODE (type) == ARRAY_TYPE)
6269 if (name)
6270 inform (loc, "declaration of %qE as multidimensional "
6271 "array must have bounds for all dimensions "
6272 "except the first", name);
6273 else
6274 inform (loc, "declaration of multidimensional array "
6275 "must have bounds for all dimensions except "
6276 "the first");
6278 type = error_mark_node;
6280 else
6281 /* When itype is NULL, a shared incomplete array type is
6282 returned for all array of a given type. Elsewhere we
6283 make sure we don't complete that type before copying
6284 it, but here we want to make sure we don't ever
6285 modify the shared type, so we gcc_assert (itype)
6286 below. */
6288 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6289 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6290 type = build_qualified_type (type,
6291 ENCODE_QUAL_ADDR_SPACE (as));
6293 type = build_array_type (type, itype);
6296 if (type != error_mark_node)
6298 if (size_varies)
6300 /* It is ok to modify type here even if itype is
6301 NULL: if size_varies, we're in a
6302 multi-dimensional array and the inner type has
6303 variable size, so the enclosing shared array type
6304 must too. */
6305 if (size && TREE_CODE (size) == INTEGER_CST)
6306 type
6307 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6308 C_TYPE_VARIABLE_SIZE (type) = 1;
6311 /* The GCC extension for zero-length arrays differs from
6312 ISO flexible array members in that sizeof yields
6313 zero. */
6314 if (size && integer_zerop (size))
6316 gcc_assert (itype);
6317 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6318 TYPE_SIZE (type) = bitsize_zero_node;
6319 TYPE_SIZE_UNIT (type) = size_zero_node;
6320 SET_TYPE_STRUCTURAL_EQUALITY (type);
6322 if (array_parm_vla_unspec_p)
6324 gcc_assert (itype);
6325 /* The type is complete. C99 6.7.5.2p4 */
6326 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6327 TYPE_SIZE (type) = bitsize_zero_node;
6328 TYPE_SIZE_UNIT (type) = size_zero_node;
6329 SET_TYPE_STRUCTURAL_EQUALITY (type);
6332 if (!valid_array_size_p (loc, type, name))
6333 type = error_mark_node;
6336 if (decl_context != PARM
6337 && (array_ptr_quals != TYPE_UNQUALIFIED
6338 || array_ptr_attrs != NULL_TREE
6339 || array_parm_static))
6341 error_at (loc, "static or type qualifiers in non-parameter "
6342 "array declarator");
6343 array_ptr_quals = TYPE_UNQUALIFIED;
6344 array_ptr_attrs = NULL_TREE;
6345 array_parm_static = false;
6347 orig_qual_indirect++;
6348 break;
6350 case cdk_function:
6352 /* Say it's a definition only for the declarator closest
6353 to the identifier, apart possibly from some
6354 attributes. */
6355 bool really_funcdef = false;
6356 tree arg_types;
6357 orig_qual_type = NULL_TREE;
6358 if (funcdef_flag)
6360 const struct c_declarator *t = declarator->declarator;
6361 while (t->kind == cdk_attrs)
6362 t = t->declarator;
6363 really_funcdef = (t->kind == cdk_id);
6366 /* Declaring a function type. Make sure we have a valid
6367 type for the function to return. */
6368 if (type == error_mark_node)
6369 continue;
6371 size_varies = false;
6373 /* Warn about some types functions can't return. */
6374 if (TREE_CODE (type) == FUNCTION_TYPE)
6376 if (name)
6377 error_at (loc, "%qE declared as function returning a "
6378 "function", name);
6379 else
6380 error_at (loc, "type name declared as function "
6381 "returning a function");
6382 type = integer_type_node;
6384 if (TREE_CODE (type) == ARRAY_TYPE)
6386 if (name)
6387 error_at (loc, "%qE declared as function returning an array",
6388 name);
6389 else
6390 error_at (loc, "type name declared as function returning "
6391 "an array");
6392 type = integer_type_node;
6395 /* Construct the function type and go to the next
6396 inner layer of declarator. */
6397 arg_info = declarator->u.arg_info;
6398 arg_types = grokparms (arg_info, really_funcdef);
6400 /* Type qualifiers before the return type of the function
6401 qualify the return type, not the function type. */
6402 if (type_quals)
6404 const enum c_declspec_word ignored_quals_list[] =
6406 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6407 cdw_atomic, cdw_number_of_elements
6409 location_t specs_loc
6410 = smallest_type_quals_location (declspecs->locations,
6411 ignored_quals_list);
6412 if (specs_loc == UNKNOWN_LOCATION)
6413 specs_loc = declspecs->locations[cdw_typedef];
6414 if (specs_loc == UNKNOWN_LOCATION)
6415 specs_loc = loc;
6417 /* Type qualifiers on a function return type are
6418 normally permitted by the standard but have no
6419 effect, so give a warning at -Wreturn-type.
6420 Qualifiers on a void return type are banned on
6421 function definitions in ISO C; GCC used to used
6422 them for noreturn functions. The resolution of C11
6423 DR#423 means qualifiers (other than _Atomic) are
6424 actually removed from the return type when
6425 determining the function type. */
6426 int quals_used = type_quals;
6427 if (flag_isoc11)
6428 quals_used &= TYPE_QUAL_ATOMIC;
6429 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6430 pedwarn (specs_loc, 0,
6431 "function definition has qualified void return type");
6432 else
6433 warning_at (specs_loc, OPT_Wignored_qualifiers,
6434 "type qualifiers ignored on function return type");
6436 /* Ensure an error for restrict on invalid types; the
6437 DR#423 resolution is not entirely clear about
6438 this. */
6439 if (flag_isoc11
6440 && (type_quals & TYPE_QUAL_RESTRICT)
6441 && (!POINTER_TYPE_P (type)
6442 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6443 error_at (loc, "invalid use of %<restrict%>");
6444 if (quals_used)
6445 type = c_build_qualified_type (type, quals_used);
6447 type_quals = TYPE_UNQUALIFIED;
6449 type = build_function_type (type, arg_types);
6450 declarator = declarator->declarator;
6452 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6453 the formal parameter list of this FUNCTION_TYPE to point to
6454 the FUNCTION_TYPE node itself. */
6456 c_arg_tag *tag;
6457 unsigned ix;
6459 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6460 TYPE_CONTEXT (tag->type) = type;
6462 break;
6464 case cdk_pointer:
6466 /* Merge any constancy or volatility into the target type
6467 for the pointer. */
6468 if ((type_quals & TYPE_QUAL_ATOMIC)
6469 && TREE_CODE (type) == FUNCTION_TYPE)
6471 error_at (loc,
6472 "%<_Atomic%>-qualified function type");
6473 type_quals &= ~TYPE_QUAL_ATOMIC;
6475 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6476 && type_quals)
6477 pedwarn (loc, OPT_Wpedantic,
6478 "ISO C forbids qualified function types");
6479 if (type_quals)
6480 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6481 orig_qual_indirect);
6482 orig_qual_type = NULL_TREE;
6483 size_varies = false;
6485 /* When the pointed-to type involves components of variable size,
6486 care must be taken to ensure that the size evaluation code is
6487 emitted early enough to dominate all the possible later uses
6488 and late enough for the variables on which it depends to have
6489 been assigned.
6491 This is expected to happen automatically when the pointed-to
6492 type has a name/declaration of it's own, but special attention
6493 is required if the type is anonymous.
6495 We attach an artificial TYPE_DECL to such pointed-to type
6496 and arrange for it to be included in a DECL_EXPR. This
6497 forces the sizes evaluation at a safe point and ensures it
6498 is not deferred until e.g. within a deeper conditional context.
6500 PARM contexts have no enclosing statement list that
6501 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6502 instead, and add it to the list of expressions that
6503 need to be evaluated.
6505 TYPENAME contexts do have an enclosing statement list,
6506 but it would be incorrect to use it, as the size should
6507 only be evaluated if the containing expression is
6508 evaluated. We might also be in the middle of an
6509 expression with side effects on the pointed-to type size
6510 "arguments" prior to the pointer declaration point and
6511 the fake TYPE_DECL in the enclosing context would force
6512 the size evaluation prior to the side effects. We therefore
6513 use BIND_EXPRs in TYPENAME contexts too. */
6514 if (!TYPE_NAME (type)
6515 && variably_modified_type_p (type, NULL_TREE))
6517 tree bind = NULL_TREE;
6518 if (decl_context == TYPENAME || decl_context == PARM)
6520 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6521 NULL_TREE, NULL_TREE);
6522 TREE_SIDE_EFFECTS (bind) = 1;
6523 BIND_EXPR_BODY (bind) = push_stmt_list ();
6524 push_scope ();
6526 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6527 DECL_ARTIFICIAL (decl) = 1;
6528 pushdecl (decl);
6529 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6530 TYPE_NAME (type) = decl;
6531 if (bind)
6533 pop_scope ();
6534 BIND_EXPR_BODY (bind)
6535 = pop_stmt_list (BIND_EXPR_BODY (bind));
6536 if (*expr)
6537 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6538 bind);
6539 else
6540 *expr = bind;
6544 type = c_build_pointer_type (type);
6546 /* Process type qualifiers (such as const or volatile)
6547 that were given inside the `*'. */
6548 type_quals = declarator->u.pointer_quals;
6550 declarator = declarator->declarator;
6551 break;
6553 default:
6554 gcc_unreachable ();
6557 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6559 /* Now TYPE has the actual type, apart from any qualifiers in
6560 TYPE_QUALS. */
6562 /* Warn about address space used for things other than static memory or
6563 pointers. */
6564 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6565 if (!ADDR_SPACE_GENERIC_P (address_space))
6567 if (decl_context == NORMAL)
6569 switch (storage_class)
6571 case csc_auto:
6572 error ("%qs combined with %<auto%> qualifier for %qE",
6573 c_addr_space_name (address_space), name);
6574 break;
6575 case csc_register:
6576 error ("%qs combined with %<register%> qualifier for %qE",
6577 c_addr_space_name (address_space), name);
6578 break;
6579 case csc_none:
6580 if (current_function_scope)
6582 error ("%qs specified for auto variable %qE",
6583 c_addr_space_name (address_space), name);
6584 break;
6586 break;
6587 case csc_static:
6588 case csc_extern:
6589 case csc_typedef:
6590 break;
6591 default:
6592 gcc_unreachable ();
6595 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6597 if (name)
6598 error ("%qs specified for parameter %qE",
6599 c_addr_space_name (address_space), name);
6600 else
6601 error ("%qs specified for unnamed parameter",
6602 c_addr_space_name (address_space));
6604 else if (decl_context == FIELD)
6606 if (name)
6607 error ("%qs specified for structure field %qE",
6608 c_addr_space_name (address_space), name);
6609 else
6610 error ("%qs specified for structure field",
6611 c_addr_space_name (address_space));
6615 /* Check the type and width of a bit-field. */
6616 if (bitfield)
6618 check_bitfield_type_and_width (loc, &type, width, name);
6619 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6620 atomic types are permitted for bit-fields; we have no code to
6621 make bit-field accesses atomic, so disallow them. */
6622 if (type_quals & TYPE_QUAL_ATOMIC)
6624 if (name)
6625 error_at (loc, "bit-field %qE has atomic type", name);
6626 else
6627 error_at (loc, "bit-field has atomic type");
6628 type_quals &= ~TYPE_QUAL_ATOMIC;
6632 /* Reject invalid uses of _Alignas. */
6633 if (declspecs->alignas_p)
6635 if (storage_class == csc_typedef)
6636 error_at (loc, "alignment specified for typedef %qE", name);
6637 else if (storage_class == csc_register)
6638 error_at (loc, "alignment specified for %<register%> object %qE",
6639 name);
6640 else if (decl_context == PARM)
6642 if (name)
6643 error_at (loc, "alignment specified for parameter %qE", name);
6644 else
6645 error_at (loc, "alignment specified for unnamed parameter");
6647 else if (bitfield)
6649 if (name)
6650 error_at (loc, "alignment specified for bit-field %qE", name);
6651 else
6652 error_at (loc, "alignment specified for unnamed bit-field");
6654 else if (TREE_CODE (type) == FUNCTION_TYPE)
6655 error_at (loc, "alignment specified for function %qE", name);
6656 else if (declspecs->align_log != -1 && TYPE_P (type))
6658 alignas_align = 1U << declspecs->align_log;
6659 if (alignas_align < min_align_of_type (type))
6661 if (name)
6662 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6663 "alignment of %qE", name);
6664 else
6665 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6666 "alignment of unnamed field");
6667 alignas_align = 0;
6672 /* If this is declaring a typedef name, return a TYPE_DECL. */
6674 if (storage_class == csc_typedef)
6676 tree decl;
6677 if ((type_quals & TYPE_QUAL_ATOMIC)
6678 && TREE_CODE (type) == FUNCTION_TYPE)
6680 error_at (loc,
6681 "%<_Atomic%>-qualified function type");
6682 type_quals &= ~TYPE_QUAL_ATOMIC;
6684 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6685 && type_quals)
6686 pedwarn (loc, OPT_Wpedantic,
6687 "ISO C forbids qualified function types");
6688 if (type_quals)
6689 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6690 orig_qual_indirect);
6691 decl = build_decl (declarator->id_loc,
6692 TYPE_DECL, declarator->u.id, type);
6693 if (declspecs->explicit_signed_p)
6694 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6695 if (declspecs->inline_p)
6696 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6697 if (declspecs->noreturn_p)
6698 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6700 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6702 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6704 if (b != NULL
6705 && b->decl != NULL_TREE
6706 && (B_IN_CURRENT_SCOPE (b)
6707 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6708 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6710 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6711 ("using %qD as both a typedef and a tag is "
6712 "invalid in C++"), decl)
6713 && b->locus != UNKNOWN_LOCATION)
6714 inform (b->locus, "originally defined here");
6718 return decl;
6721 /* If this is a type name (such as, in a cast or sizeof),
6722 compute the type and return it now. */
6724 if (decl_context == TYPENAME)
6726 /* Note that the grammar rejects storage classes in typenames
6727 and fields. */
6728 gcc_assert (storage_class == csc_none && !threadp
6729 && !declspecs->inline_p && !declspecs->noreturn_p);
6730 if ((type_quals & TYPE_QUAL_ATOMIC)
6731 && TREE_CODE (type) == FUNCTION_TYPE)
6733 error_at (loc,
6734 "%<_Atomic%>-qualified function type");
6735 type_quals &= ~TYPE_QUAL_ATOMIC;
6737 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6738 && type_quals)
6739 pedwarn (loc, OPT_Wpedantic,
6740 "ISO C forbids const or volatile function types");
6741 if (type_quals)
6742 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6743 orig_qual_indirect);
6744 return type;
6747 if (pedantic && decl_context == FIELD
6748 && variably_modified_type_p (type, NULL_TREE))
6750 /* C99 6.7.2.1p8 */
6751 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6752 "have a variably modified type");
6755 /* Aside from typedefs and type names (handle above),
6756 `void' at top level (not within pointer)
6757 is allowed only in public variables.
6758 We don't complain about parms either, but that is because
6759 a better error message can be made later. */
6761 if (VOID_TYPE_P (type) && decl_context != PARM
6762 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6763 && (storage_class == csc_extern
6764 || (current_scope == file_scope
6765 && !(storage_class == csc_static
6766 || storage_class == csc_register)))))
6768 error_at (loc, "variable or field %qE declared void", name);
6769 type = integer_type_node;
6772 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6773 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6776 tree decl;
6778 if (decl_context == PARM)
6780 tree promoted_type;
6781 bool array_parameter_p = false;
6783 /* A parameter declared as an array of T is really a pointer to T.
6784 One declared as a function is really a pointer to a function. */
6786 if (TREE_CODE (type) == ARRAY_TYPE)
6788 /* Transfer const-ness of array into that of type pointed to. */
6789 type = TREE_TYPE (type);
6790 if (orig_qual_type != NULL_TREE)
6792 if (orig_qual_indirect == 0)
6793 orig_qual_type = TREE_TYPE (orig_qual_type);
6794 else
6795 orig_qual_indirect--;
6797 if (type_quals)
6798 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6799 orig_qual_indirect);
6800 type = c_build_pointer_type (type);
6801 type_quals = array_ptr_quals;
6802 if (type_quals)
6803 type = c_build_qualified_type (type, type_quals);
6805 /* We don't yet implement attributes in this context. */
6806 if (array_ptr_attrs != NULL_TREE)
6807 warning_at (loc, OPT_Wattributes,
6808 "attributes in parameter array declarator ignored");
6810 size_varies = false;
6811 array_parameter_p = true;
6813 else if (TREE_CODE (type) == FUNCTION_TYPE)
6815 if (type_quals & TYPE_QUAL_ATOMIC)
6817 error_at (loc,
6818 "%<_Atomic%>-qualified function type");
6819 type_quals &= ~TYPE_QUAL_ATOMIC;
6821 else if (type_quals)
6822 pedwarn (loc, OPT_Wpedantic,
6823 "ISO C forbids qualified function types");
6824 if (type_quals)
6825 type = c_build_qualified_type (type, type_quals);
6826 type = c_build_pointer_type (type);
6827 type_quals = TYPE_UNQUALIFIED;
6829 else if (type_quals)
6830 type = c_build_qualified_type (type, type_quals);
6832 decl = build_decl (declarator->id_loc,
6833 PARM_DECL, declarator->u.id, type);
6834 if (size_varies)
6835 C_DECL_VARIABLE_SIZE (decl) = 1;
6836 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6838 /* Compute the type actually passed in the parmlist,
6839 for the case where there is no prototype.
6840 (For example, shorts and chars are passed as ints.)
6841 When there is a prototype, this is overridden later. */
6843 if (type == error_mark_node)
6844 promoted_type = type;
6845 else
6846 promoted_type = c_type_promotes_to (type);
6848 DECL_ARG_TYPE (decl) = promoted_type;
6849 if (declspecs->inline_p)
6850 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6851 if (declspecs->noreturn_p)
6852 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6854 else if (decl_context == FIELD)
6856 /* Note that the grammar rejects storage classes in typenames
6857 and fields. */
6858 gcc_assert (storage_class == csc_none && !threadp
6859 && !declspecs->inline_p && !declspecs->noreturn_p);
6861 /* Structure field. It may not be a function. */
6863 if (TREE_CODE (type) == FUNCTION_TYPE)
6865 error_at (loc, "field %qE declared as a function", name);
6866 type = build_pointer_type (type);
6868 else if (TREE_CODE (type) != ERROR_MARK
6869 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6871 if (name)
6872 error_at (loc, "field %qE has incomplete type", name);
6873 else
6874 error_at (loc, "unnamed field has incomplete type");
6875 type = error_mark_node;
6877 else if (TREE_CODE (type) == ARRAY_TYPE
6878 && TYPE_DOMAIN (type) == NULL_TREE)
6880 /* We have a flexible array member through a typedef.
6881 Set suitable range. Whether this is a correct position
6882 for a flexible array member will be determined elsewhere. */
6883 if (!in_system_header_at (input_location))
6884 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6885 "support flexible array members");
6886 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6887 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6888 NULL_TREE);
6889 if (orig_qual_indirect == 0)
6890 orig_qual_type = NULL_TREE;
6892 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6893 orig_qual_indirect);
6894 decl = build_decl (declarator->id_loc,
6895 FIELD_DECL, declarator->u.id, type);
6896 DECL_NONADDRESSABLE_P (decl) = bitfield;
6897 if (bitfield && !declarator->u.id)
6899 TREE_NO_WARNING (decl) = 1;
6900 DECL_PADDING_P (decl) = 1;
6903 if (size_varies)
6904 C_DECL_VARIABLE_SIZE (decl) = 1;
6906 else if (TREE_CODE (type) == FUNCTION_TYPE)
6908 if (storage_class == csc_register || threadp)
6910 error_at (loc, "invalid storage class for function %qE", name);
6912 else if (current_scope != file_scope)
6914 /* Function declaration not at file scope. Storage
6915 classes other than `extern' are not allowed, C99
6916 6.7.1p5, and `extern' makes no difference. However,
6917 GCC allows 'auto', perhaps with 'inline', to support
6918 nested functions. */
6919 if (storage_class == csc_auto)
6920 pedwarn (loc, OPT_Wpedantic,
6921 "invalid storage class for function %qE", name);
6922 else if (storage_class == csc_static)
6924 error_at (loc, "invalid storage class for function %qE", name);
6925 if (funcdef_flag)
6926 storage_class = declspecs->storage_class = csc_none;
6927 else
6928 return NULL_TREE;
6932 decl = build_decl (declarator->id_loc,
6933 FUNCTION_DECL, declarator->u.id, type);
6934 decl = build_decl_attribute_variant (decl, decl_attr);
6936 if (type_quals & TYPE_QUAL_ATOMIC)
6938 error_at (loc,
6939 "%<_Atomic%>-qualified function type");
6940 type_quals &= ~TYPE_QUAL_ATOMIC;
6942 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6943 pedwarn (loc, OPT_Wpedantic,
6944 "ISO C forbids qualified function types");
6946 /* Every function declaration is an external reference
6947 (DECL_EXTERNAL) except for those which are not at file
6948 scope and are explicitly declared "auto". This is
6949 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6950 GCC to signify a forward declaration of a nested function. */
6951 if (storage_class == csc_auto && current_scope != file_scope)
6952 DECL_EXTERNAL (decl) = 0;
6953 /* In C99, a function which is declared 'inline' with 'extern'
6954 is not an external reference (which is confusing). It
6955 means that the later definition of the function must be output
6956 in this file, C99 6.7.4p6. In GNU C89, a function declared
6957 'extern inline' is an external reference. */
6958 else if (declspecs->inline_p && storage_class != csc_static)
6959 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6960 == flag_gnu89_inline);
6961 else
6962 DECL_EXTERNAL (decl) = !initialized;
6964 /* Record absence of global scope for `static' or `auto'. */
6965 TREE_PUBLIC (decl)
6966 = !(storage_class == csc_static || storage_class == csc_auto);
6968 /* For a function definition, record the argument information
6969 block where store_parm_decls will look for it. */
6970 if (funcdef_flag)
6971 current_function_arg_info = arg_info;
6973 if (declspecs->default_int_p)
6974 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6976 /* Record presence of `inline' and `_Noreturn', if it is
6977 reasonable. */
6978 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6980 if (declspecs->inline_p)
6981 pedwarn (loc, 0, "cannot inline function %<main%>");
6982 if (declspecs->noreturn_p)
6983 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6985 else
6987 if (declspecs->inline_p)
6988 /* Record that the function is declared `inline'. */
6989 DECL_DECLARED_INLINE_P (decl) = 1;
6990 if (declspecs->noreturn_p)
6992 if (flag_isoc99)
6993 pedwarn_c99 (loc, OPT_Wpedantic,
6994 "ISO C99 does not support %<_Noreturn%>");
6995 else
6996 pedwarn_c99 (loc, OPT_Wpedantic,
6997 "ISO C90 does not support %<_Noreturn%>");
6998 TREE_THIS_VOLATILE (decl) = 1;
7002 else
7004 /* It's a variable. */
7005 /* An uninitialized decl with `extern' is a reference. */
7006 int extern_ref = !initialized && storage_class == csc_extern;
7008 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7009 orig_qual_indirect);
7011 /* C99 6.2.2p7: It is invalid (compile-time undefined
7012 behavior) to create an 'extern' declaration for a
7013 variable if there is a global declaration that is
7014 'static' and the global declaration is not visible.
7015 (If the static declaration _is_ currently visible,
7016 the 'extern' declaration is taken to refer to that decl.) */
7017 if (extern_ref && current_scope != file_scope)
7019 tree global_decl = identifier_global_value (declarator->u.id);
7020 tree visible_decl = lookup_name (declarator->u.id);
7022 if (global_decl
7023 && global_decl != visible_decl
7024 && VAR_P (global_decl)
7025 && !TREE_PUBLIC (global_decl))
7026 error_at (loc, "variable previously declared %<static%> "
7027 "redeclared %<extern%>");
7030 decl = build_decl (declarator->id_loc,
7031 VAR_DECL, declarator->u.id, type);
7032 if (size_varies)
7033 C_DECL_VARIABLE_SIZE (decl) = 1;
7035 if (declspecs->inline_p)
7036 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7037 if (declspecs->noreturn_p)
7038 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7040 /* At file scope, an initialized extern declaration may follow
7041 a static declaration. In that case, DECL_EXTERNAL will be
7042 reset later in start_decl. */
7043 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7045 /* At file scope, the presence of a `static' or `register' storage
7046 class specifier, or the absence of all storage class specifiers
7047 makes this declaration a definition (perhaps tentative). Also,
7048 the absence of `static' makes it public. */
7049 if (current_scope == file_scope)
7051 TREE_PUBLIC (decl) = storage_class != csc_static;
7052 TREE_STATIC (decl) = !extern_ref;
7054 /* Not at file scope, only `static' makes a static definition. */
7055 else
7057 TREE_STATIC (decl) = (storage_class == csc_static);
7058 TREE_PUBLIC (decl) = extern_ref;
7061 if (threadp)
7062 set_decl_tls_model (decl, decl_default_tls_model (decl));
7065 if ((storage_class == csc_extern
7066 || (storage_class == csc_none
7067 && TREE_CODE (type) == FUNCTION_TYPE
7068 && !funcdef_flag))
7069 && variably_modified_type_p (type, NULL_TREE))
7071 /* C99 6.7.5.2p2 */
7072 if (TREE_CODE (type) == FUNCTION_TYPE)
7073 error_at (loc, "non-nested function with variably modified type");
7074 else
7075 error_at (loc, "object with variably modified type must have "
7076 "no linkage");
7079 /* Record `register' declaration for warnings on &
7080 and in case doing stupid register allocation. */
7082 if (storage_class == csc_register)
7084 C_DECL_REGISTER (decl) = 1;
7085 DECL_REGISTER (decl) = 1;
7088 /* Record constancy and volatility. */
7089 c_apply_type_quals_to_decl (type_quals, decl);
7091 /* Apply _Alignas specifiers. */
7092 if (alignas_align)
7094 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7095 DECL_USER_ALIGN (decl) = 1;
7098 /* If a type has volatile components, it should be stored in memory.
7099 Otherwise, the fact that those components are volatile
7100 will be ignored, and would even crash the compiler.
7101 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7102 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7103 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7104 || TREE_CODE (decl) == RESULT_DECL))
7106 /* It is not an error for a structure with volatile fields to
7107 be declared register, but reset DECL_REGISTER since it
7108 cannot actually go in a register. */
7109 int was_reg = C_DECL_REGISTER (decl);
7110 C_DECL_REGISTER (decl) = 0;
7111 DECL_REGISTER (decl) = 0;
7112 c_mark_addressable (decl);
7113 C_DECL_REGISTER (decl) = was_reg;
7116 /* This is the earliest point at which we might know the assembler
7117 name of a variable. Thus, if it's known before this, die horribly. */
7118 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7119 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7121 if (warn_cxx_compat
7122 && VAR_P (decl)
7123 && TREE_PUBLIC (decl)
7124 && TREE_STATIC (decl)
7125 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7126 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7127 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7128 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7129 ("non-local variable %qD with anonymous type is "
7130 "questionable in C++"),
7131 decl);
7133 return decl;
7137 /* Decode the parameter-list info for a function type or function definition.
7138 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7139 if there is an identifier list instead of a parameter decl list).
7140 These two functions are separate because when a function returns
7141 or receives functions then each is called multiple times but the order
7142 of calls is different. The last call to `grokparms' is always the one
7143 that contains the formal parameter names of a function definition.
7145 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7147 FUNCDEF_FLAG is true for a function definition, false for
7148 a mere declaration. A nonempty identifier-list gets an error message
7149 when FUNCDEF_FLAG is false. */
7151 static tree
7152 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7154 tree arg_types = arg_info->types;
7156 if (funcdef_flag && arg_info->had_vla_unspec)
7158 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7159 /* C99 6.7.5.2p4 */
7160 error ("%<[*]%> not allowed in other than function prototype scope");
7163 if (arg_types == NULL_TREE && !funcdef_flag
7164 && !in_system_header_at (input_location))
7165 warning (OPT_Wstrict_prototypes,
7166 "function declaration isn%'t a prototype");
7168 if (arg_types == error_mark_node)
7169 /* Don't set TYPE_ARG_TYPES in this case. */
7170 return NULL_TREE;
7172 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7174 if (!funcdef_flag)
7176 pedwarn (input_location, 0, "parameter names (without types) in "
7177 "function declaration");
7178 arg_info->parms = NULL_TREE;
7180 else
7181 arg_info->parms = arg_info->types;
7183 arg_info->types = NULL_TREE;
7184 return NULL_TREE;
7186 else
7188 tree parm, type, typelt;
7189 unsigned int parmno;
7191 /* If there is a parameter of incomplete type in a definition,
7192 this is an error. In a declaration this is valid, and a
7193 struct or union type may be completed later, before any calls
7194 or definition of the function. In the case where the tag was
7195 first declared within the parameter list, a warning has
7196 already been given. If a parameter has void type, then
7197 however the function cannot be defined or called, so
7198 warn. */
7200 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7201 parm;
7202 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7204 type = TREE_VALUE (typelt);
7205 if (type == error_mark_node)
7206 continue;
7208 if (!COMPLETE_TYPE_P (type))
7210 if (funcdef_flag)
7212 if (DECL_NAME (parm))
7213 error_at (input_location,
7214 "parameter %u (%q+D) has incomplete type",
7215 parmno, parm);
7216 else
7217 error_at (DECL_SOURCE_LOCATION (parm),
7218 "parameter %u has incomplete type",
7219 parmno);
7221 TREE_VALUE (typelt) = error_mark_node;
7222 TREE_TYPE (parm) = error_mark_node;
7223 arg_types = NULL_TREE;
7225 else if (VOID_TYPE_P (type))
7227 if (DECL_NAME (parm))
7228 warning_at (input_location, 0,
7229 "parameter %u (%q+D) has void type",
7230 parmno, parm);
7231 else
7232 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7233 "parameter %u has void type",
7234 parmno);
7238 if (DECL_NAME (parm) && TREE_USED (parm))
7239 warn_if_shadowing (parm);
7241 return arg_types;
7245 /* Allocate and initialize a c_arg_info structure from the parser's
7246 obstack. */
7248 struct c_arg_info *
7249 build_arg_info (void)
7251 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7252 ret->parms = NULL_TREE;
7253 ret->tags = NULL;
7254 ret->types = NULL_TREE;
7255 ret->others = NULL_TREE;
7256 ret->pending_sizes = NULL;
7257 ret->had_vla_unspec = 0;
7258 return ret;
7261 /* Take apart the current scope and return a c_arg_info structure with
7262 info on a parameter list just parsed.
7264 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7266 ELLIPSIS being true means the argument list ended in '...' so don't
7267 append a sentinel (void_list_node) to the end of the type-list.
7269 EXPR is NULL or an expression that needs to be evaluated for the
7270 side effects of array size expressions in the parameters. */
7272 struct c_arg_info *
7273 get_parm_info (bool ellipsis, tree expr)
7275 struct c_binding *b = current_scope->bindings;
7276 struct c_arg_info *arg_info = build_arg_info ();
7278 tree parms = NULL_TREE;
7279 vec<c_arg_tag, va_gc> *tags = NULL;
7280 tree types = NULL_TREE;
7281 tree others = NULL_TREE;
7283 bool gave_void_only_once_err = false;
7285 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7287 /* The bindings in this scope must not get put into a block.
7288 We will take care of deleting the binding nodes. */
7289 current_scope->bindings = 0;
7291 /* This function is only called if there was *something* on the
7292 parameter list. */
7293 gcc_assert (b);
7295 /* A parameter list consisting solely of 'void' indicates that the
7296 function takes no arguments. But if the 'void' is qualified
7297 (by 'const' or 'volatile'), or has a storage class specifier
7298 ('register'), then the behavior is undefined; issue an error.
7299 Typedefs for 'void' are OK (see DR#157). */
7300 if (b->prev == 0 /* one binding */
7301 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7302 && !DECL_NAME (b->decl) /* anonymous */
7303 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7305 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7306 || C_DECL_REGISTER (b->decl))
7307 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7309 /* There cannot be an ellipsis. */
7310 if (ellipsis)
7311 error_at (b->locus, "%<void%> must be the only parameter");
7313 arg_info->types = void_list_node;
7314 return arg_info;
7317 if (!ellipsis)
7318 types = void_list_node;
7320 /* Break up the bindings list into parms, tags, types, and others;
7321 apply sanity checks; purge the name-to-decl bindings. */
7322 while (b)
7324 tree decl = b->decl;
7325 tree type = TREE_TYPE (decl);
7326 c_arg_tag tag;
7327 const char *keyword;
7329 switch (TREE_CODE (decl))
7331 case PARM_DECL:
7332 if (b->id)
7334 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7335 I_SYMBOL_BINDING (b->id) = b->shadowed;
7338 /* Check for forward decls that never got their actual decl. */
7339 if (TREE_ASM_WRITTEN (decl))
7340 error_at (b->locus,
7341 "parameter %q+D has just a forward declaration", decl);
7342 /* Check for (..., void, ...) and issue an error. */
7343 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7345 if (!gave_void_only_once_err)
7347 error_at (b->locus, "%<void%> must be the only parameter");
7348 gave_void_only_once_err = true;
7351 else
7353 /* Valid parameter, add it to the list. */
7354 DECL_CHAIN (decl) = parms;
7355 parms = decl;
7357 /* Since there is a prototype, args are passed in their
7358 declared types. The back end may override this later. */
7359 DECL_ARG_TYPE (decl) = type;
7360 types = tree_cons (0, type, types);
7362 break;
7364 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7365 case UNION_TYPE: keyword = "union"; goto tag;
7366 case RECORD_TYPE: keyword = "struct"; goto tag;
7367 tag:
7368 /* Types may not have tag-names, in which case the type
7369 appears in the bindings list with b->id NULL. */
7370 if (b->id)
7372 gcc_assert (I_TAG_BINDING (b->id) == b);
7373 I_TAG_BINDING (b->id) = b->shadowed;
7376 /* Warn about any struct, union or enum tags defined in a
7377 parameter list. The scope of such types is limited to
7378 the parameter list, which is rarely if ever desirable
7379 (it's impossible to call such a function with type-
7380 correct arguments). An anonymous union parm type is
7381 meaningful as a GNU extension, so don't warn for that. */
7382 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7384 if (b->id)
7385 /* The %s will be one of 'struct', 'union', or 'enum'. */
7386 warning_at (b->locus, 0,
7387 "%<%s %E%> declared inside parameter list"
7388 " will not be visible outside of this definition or"
7389 " declaration", keyword, b->id);
7390 else
7391 /* The %s will be one of 'struct', 'union', or 'enum'. */
7392 warning_at (b->locus, 0,
7393 "anonymous %s declared inside parameter list"
7394 " will not be visible outside of this definition or"
7395 " declaration", keyword);
7398 tag.id = b->id;
7399 tag.type = decl;
7400 vec_safe_push (tags, tag);
7401 break;
7403 case FUNCTION_DECL:
7404 /* FUNCTION_DECLs appear when there is an implicit function
7405 declaration in the parameter list. */
7406 gcc_assert (b->nested || seen_error ());
7407 goto set_shadowed;
7409 case CONST_DECL:
7410 case TYPE_DECL:
7411 /* CONST_DECLs appear here when we have an embedded enum,
7412 and TYPE_DECLs appear here when we have an embedded struct
7413 or union. No warnings for this - we already warned about the
7414 type itself. */
7416 /* When we reinsert this decl in the function body, we need
7417 to reconstruct whether it was marked as nested. */
7418 gcc_assert (!b->nested);
7419 DECL_CHAIN (decl) = others;
7420 others = decl;
7421 /* fall through */
7423 case ERROR_MARK:
7424 set_shadowed:
7425 /* error_mark_node appears here when we have an undeclared
7426 variable. Just throw it away. */
7427 if (b->id)
7429 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7430 I_SYMBOL_BINDING (b->id) = b->shadowed;
7432 break;
7434 /* Other things that might be encountered. */
7435 case LABEL_DECL:
7436 case VAR_DECL:
7437 default:
7438 gcc_unreachable ();
7441 b = free_binding_and_advance (b);
7444 arg_info->parms = parms;
7445 arg_info->tags = tags;
7446 arg_info->types = types;
7447 arg_info->others = others;
7448 arg_info->pending_sizes = expr;
7449 return arg_info;
7452 /* Get the struct, enum or union (CODE says which) with tag NAME.
7453 Define the tag as a forward-reference with location LOC if it is
7454 not defined. Return a c_typespec structure for the type
7455 specifier. */
7457 struct c_typespec
7458 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7460 struct c_typespec ret;
7461 tree ref;
7462 location_t refloc;
7464 ret.expr = NULL_TREE;
7465 ret.expr_const_operands = true;
7467 /* If a cross reference is requested, look up the type
7468 already defined for this tag and return it. */
7470 ref = lookup_tag (code, name, false, &refloc);
7471 /* If this is the right type of tag, return what we found.
7472 (This reference will be shadowed by shadow_tag later if appropriate.)
7473 If this is the wrong type of tag, do not return it. If it was the
7474 wrong type in the same scope, we will have had an error
7475 message already; if in a different scope and declaring
7476 a name, pending_xref_error will give an error message; but if in a
7477 different scope and not declaring a name, this tag should
7478 shadow the previous declaration of a different type of tag, and
7479 this would not work properly if we return the reference found.
7480 (For example, with "struct foo" in an outer scope, "union foo;"
7481 must shadow that tag with a new one of union type.) */
7482 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7483 if (ref && TREE_CODE (ref) == code)
7485 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7486 && loc != UNKNOWN_LOCATION
7487 && warn_cxx_compat)
7489 switch (code)
7491 case ENUMERAL_TYPE:
7492 warning_at (loc, OPT_Wc___compat,
7493 ("enum type defined in struct or union "
7494 "is not visible in C++"));
7495 inform (refloc, "enum type defined here");
7496 break;
7497 case RECORD_TYPE:
7498 warning_at (loc, OPT_Wc___compat,
7499 ("struct defined in struct or union "
7500 "is not visible in C++"));
7501 inform (refloc, "struct defined here");
7502 break;
7503 case UNION_TYPE:
7504 warning_at (loc, OPT_Wc___compat,
7505 ("union defined in struct or union "
7506 "is not visible in C++"));
7507 inform (refloc, "union defined here");
7508 break;
7509 default:
7510 gcc_unreachable();
7514 ret.spec = ref;
7515 return ret;
7518 /* If no such tag is yet defined, create a forward-reference node
7519 and record it as the "definition".
7520 When a real declaration of this type is found,
7521 the forward-reference will be altered into a real type. */
7523 ref = make_node (code);
7524 if (code == ENUMERAL_TYPE)
7526 /* Give the type a default layout like unsigned int
7527 to avoid crashing if it does not get defined. */
7528 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7529 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7530 TYPE_USER_ALIGN (ref) = 0;
7531 TYPE_UNSIGNED (ref) = 1;
7532 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7533 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7534 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7537 pushtag (loc, name, ref);
7539 ret.spec = ref;
7540 return ret;
7543 /* Get the struct, enum or union (CODE says which) with tag NAME.
7544 Define the tag as a forward-reference if it is not defined.
7545 Return a tree for the type. */
7547 tree
7548 xref_tag (enum tree_code code, tree name)
7550 return parser_xref_tag (input_location, code, name).spec;
7553 /* Make sure that the tag NAME is defined *in the current scope*
7554 at least as a forward reference.
7555 LOC is the location of the struct's definition.
7556 CODE says which kind of tag NAME ought to be.
7558 This stores the current value of the file static STRUCT_PARSE_INFO
7559 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7560 new c_struct_parse_info structure. The old value of
7561 STRUCT_PARSE_INFO is restored in finish_struct. */
7563 tree
7564 start_struct (location_t loc, enum tree_code code, tree name,
7565 struct c_struct_parse_info **enclosing_struct_parse_info)
7567 /* If there is already a tag defined at this scope
7568 (as a forward reference), just return it. */
7570 tree ref = NULL_TREE;
7571 location_t refloc = UNKNOWN_LOCATION;
7573 if (name != NULL_TREE)
7574 ref = lookup_tag (code, name, true, &refloc);
7575 if (ref && TREE_CODE (ref) == code)
7577 if (TYPE_STUB_DECL (ref))
7578 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7580 if (TYPE_SIZE (ref))
7582 if (code == UNION_TYPE)
7583 error_at (loc, "redefinition of %<union %E%>", name);
7584 else
7585 error_at (loc, "redefinition of %<struct %E%>", name);
7586 if (refloc != UNKNOWN_LOCATION)
7587 inform (refloc, "originally defined here");
7588 /* Don't create structures using a name already in use. */
7589 ref = NULL_TREE;
7591 else if (C_TYPE_BEING_DEFINED (ref))
7593 if (code == UNION_TYPE)
7594 error_at (loc, "nested redefinition of %<union %E%>", name);
7595 else
7596 error_at (loc, "nested redefinition of %<struct %E%>", name);
7597 /* Don't bother to report "originally defined here" for a
7598 nested redefinition; the original definition should be
7599 obvious. */
7600 /* Don't create structures that contain themselves. */
7601 ref = NULL_TREE;
7605 /* Otherwise create a forward-reference just so the tag is in scope. */
7607 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7609 ref = make_node (code);
7610 pushtag (loc, name, ref);
7613 C_TYPE_BEING_DEFINED (ref) = 1;
7614 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7615 TYPE_PACKED (v) = flag_pack_struct;
7617 *enclosing_struct_parse_info = struct_parse_info;
7618 struct_parse_info = new c_struct_parse_info ();
7620 /* FIXME: This will issue a warning for a use of a type defined
7621 within a statement expr used within sizeof, et. al. This is not
7622 terribly serious as C++ doesn't permit statement exprs within
7623 sizeof anyhow. */
7624 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7625 warning_at (loc, OPT_Wc___compat,
7626 "defining type in %qs expression is invalid in C++",
7627 (in_sizeof
7628 ? "sizeof"
7629 : (in_typeof ? "typeof" : "alignof")));
7631 return ref;
7634 /* Process the specs, declarator and width (NULL if omitted)
7635 of a structure component, returning a FIELD_DECL node.
7636 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7637 DECL_ATTRS is as for grokdeclarator.
7639 LOC is the location of the structure component.
7641 This is done during the parsing of the struct declaration.
7642 The FIELD_DECL nodes are chained together and the lot of them
7643 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7645 tree
7646 grokfield (location_t loc,
7647 struct c_declarator *declarator, struct c_declspecs *declspecs,
7648 tree width, tree *decl_attrs)
7650 tree value;
7652 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7653 && width == NULL_TREE)
7655 /* This is an unnamed decl.
7657 If we have something of the form "union { list } ;" then this
7658 is the anonymous union extension. Similarly for struct.
7660 If this is something of the form "struct foo;", then
7661 If MS or Plan 9 extensions are enabled, this is handled as
7662 an anonymous struct.
7663 Otherwise this is a forward declaration of a structure tag.
7665 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7666 If foo names a structure or union without a tag, then this
7667 is an anonymous struct (this is permitted by C11).
7668 If MS or Plan 9 extensions are enabled and foo names a
7669 structure, then again this is an anonymous struct.
7670 Otherwise this is an error.
7672 Oh what a horrid tangled web we weave. I wonder if MS consciously
7673 took this from Plan 9 or if it was an accident of implementation
7674 that took root before someone noticed the bug... */
7676 tree type = declspecs->type;
7677 bool ok = false;
7679 if (RECORD_OR_UNION_TYPE_P (type)
7680 && (flag_ms_extensions
7681 || flag_plan9_extensions
7682 || !declspecs->typedef_p))
7684 if (flag_ms_extensions || flag_plan9_extensions)
7685 ok = true;
7686 else if (TYPE_NAME (type) == NULL)
7687 ok = true;
7688 else
7689 ok = false;
7691 if (!ok)
7693 pedwarn (loc, 0, "declaration does not declare anything");
7694 return NULL_TREE;
7696 if (flag_isoc99)
7697 pedwarn_c99 (loc, OPT_Wpedantic,
7698 "ISO C99 doesn%'t support unnamed structs/unions");
7699 else
7700 pedwarn_c99 (loc, OPT_Wpedantic,
7701 "ISO C90 doesn%'t support unnamed structs/unions");
7704 value = grokdeclarator (declarator, declspecs, FIELD, false,
7705 width ? &width : NULL, decl_attrs, NULL, NULL,
7706 DEPRECATED_NORMAL);
7708 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7709 DECL_INITIAL (value) = width;
7710 if (width)
7711 SET_DECL_C_BIT_FIELD (value);
7713 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7715 /* If we currently have a binding for this field, set the
7716 in_struct field in the binding, so that we warn about lookups
7717 which find it. */
7718 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7719 if (b != NULL)
7721 /* If the in_struct field is not yet set, push it on a list
7722 to be cleared when this struct is finished. */
7723 if (!b->in_struct)
7725 struct_parse_info->fields.safe_push (b);
7726 b->in_struct = 1;
7731 return value;
7734 /* Subroutine of detect_field_duplicates: return whether X and Y,
7735 which are both fields in the same struct, have duplicate field
7736 names. */
7738 static bool
7739 is_duplicate_field (tree x, tree y)
7741 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7742 return true;
7744 /* When using -fplan9-extensions, an anonymous field whose name is a
7745 typedef can duplicate a field name. */
7746 if (flag_plan9_extensions
7747 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7749 tree xt, xn, yt, yn;
7751 xt = TREE_TYPE (x);
7752 if (DECL_NAME (x) != NULL_TREE)
7753 xn = DECL_NAME (x);
7754 else if (RECORD_OR_UNION_TYPE_P (xt)
7755 && TYPE_NAME (xt) != NULL_TREE
7756 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7757 xn = DECL_NAME (TYPE_NAME (xt));
7758 else
7759 xn = NULL_TREE;
7761 yt = TREE_TYPE (y);
7762 if (DECL_NAME (y) != NULL_TREE)
7763 yn = DECL_NAME (y);
7764 else if (RECORD_OR_UNION_TYPE_P (yt)
7765 && TYPE_NAME (yt) != NULL_TREE
7766 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7767 yn = DECL_NAME (TYPE_NAME (yt));
7768 else
7769 yn = NULL_TREE;
7771 if (xn != NULL_TREE && xn == yn)
7772 return true;
7775 return false;
7778 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7779 to HTAB, giving errors for any duplicates. */
7781 static void
7782 detect_field_duplicates_hash (tree fieldlist,
7783 hash_table<nofree_ptr_hash <tree_node> > *htab)
7785 tree x, y;
7786 tree_node **slot;
7788 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7789 if ((y = DECL_NAME (x)) != NULL_TREE)
7791 slot = htab->find_slot (y, INSERT);
7792 if (*slot)
7794 error ("duplicate member %q+D", x);
7795 DECL_NAME (x) = NULL_TREE;
7797 *slot = y;
7799 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7801 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7803 /* When using -fplan9-extensions, an anonymous field whose
7804 name is a typedef can duplicate a field name. */
7805 if (flag_plan9_extensions
7806 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7807 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7809 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7810 slot = htab->find_slot (xn, INSERT);
7811 if (*slot)
7812 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7813 *slot = xn;
7818 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7819 the list such that this does not present a problem later. */
7821 static void
7822 detect_field_duplicates (tree fieldlist)
7824 tree x, y;
7825 int timeout = 10;
7827 /* If the struct is the list of instance variables of an Objective-C
7828 class, then we need to check all the instance variables of
7829 superclasses when checking for duplicates (since you can't have
7830 an instance variable in a subclass with the same name as an
7831 instance variable in a superclass). We pass on this job to the
7832 Objective-C compiler. objc_detect_field_duplicates() will return
7833 false if we are not checking the list of instance variables and
7834 the C frontend should proceed with the standard field duplicate
7835 checks. If we are checking the list of instance variables, the
7836 ObjC frontend will do the check, emit the errors if needed, and
7837 then return true. */
7838 if (c_dialect_objc ())
7839 if (objc_detect_field_duplicates (false))
7840 return;
7842 /* First, see if there are more than "a few" fields.
7843 This is trivially true if there are zero or one fields. */
7844 if (!fieldlist || !DECL_CHAIN (fieldlist))
7845 return;
7846 x = fieldlist;
7847 do {
7848 timeout--;
7849 if (DECL_NAME (x) == NULL_TREE
7850 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7851 timeout = 0;
7852 x = DECL_CHAIN (x);
7853 } while (timeout > 0 && x);
7855 /* If there were "few" fields and no anonymous structures or unions,
7856 avoid the overhead of allocating a hash table. Instead just do
7857 the nested traversal thing. */
7858 if (timeout > 0)
7860 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7861 /* When using -fplan9-extensions, we can have duplicates
7862 between typedef names and fields. */
7863 if (DECL_NAME (x)
7864 || (flag_plan9_extensions
7865 && DECL_NAME (x) == NULL_TREE
7866 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7867 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7868 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7870 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7871 if (is_duplicate_field (y, x))
7873 error ("duplicate member %q+D", x);
7874 DECL_NAME (x) = NULL_TREE;
7878 else
7880 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7881 detect_field_duplicates_hash (fieldlist, &htab);
7885 /* Finish up struct info used by -Wc++-compat. */
7887 static void
7888 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7889 location_t record_loc)
7891 unsigned int ix;
7892 tree x;
7893 struct c_binding *b;
7895 if (fieldlist == NULL_TREE)
7897 if (code == RECORD_TYPE)
7898 warning_at (record_loc, OPT_Wc___compat,
7899 "empty struct has size 0 in C, size 1 in C++");
7900 else
7901 warning_at (record_loc, OPT_Wc___compat,
7902 "empty union has size 0 in C, size 1 in C++");
7905 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7906 the current struct. We do this now at the end of the struct
7907 because the flag is used to issue visibility warnings, and we
7908 only want to issue those warnings if the type is referenced
7909 outside of the struct declaration. */
7910 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7911 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7913 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7914 typedefs used when declaring fields in this struct. If the name
7915 of any of the fields is also a typedef name then the struct would
7916 not parse in C++, because the C++ lookup rules say that the
7917 typedef name would be looked up in the context of the struct, and
7918 would thus be the field rather than the typedef. */
7919 if (!struct_parse_info->typedefs_seen.is_empty ()
7920 && fieldlist != NULL_TREE)
7922 /* Use a hash_set<tree> using the name of the typedef. We can use
7923 a hash_set<tree> because identifiers are interned. */
7924 hash_set<tree> tset;
7926 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7927 tset.add (DECL_NAME (x));
7929 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7931 if (DECL_NAME (x) != NULL_TREE
7932 && tset.contains (DECL_NAME (x)))
7934 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7935 ("using %qD as both field and typedef name is "
7936 "invalid in C++"),
7938 /* FIXME: It would be nice to report the location where
7939 the typedef name is used. */
7944 /* For each field which has a binding and which was not defined in
7945 an enclosing struct, clear the in_struct field. */
7946 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7947 b->in_struct = 0;
7950 /* Function to help qsort sort FIELD_DECLs by name order. */
7952 static int
7953 field_decl_cmp (const void *x_p, const void *y_p)
7955 const tree *const x = (const tree *) x_p;
7956 const tree *const y = (const tree *) y_p;
7958 if (DECL_NAME (*x) == DECL_NAME (*y))
7959 /* A nontype is "greater" than a type. */
7960 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7961 if (DECL_NAME (*x) == NULL_TREE)
7962 return -1;
7963 if (DECL_NAME (*y) == NULL_TREE)
7964 return 1;
7965 if (DECL_NAME (*x) < DECL_NAME (*y))
7966 return -1;
7967 return 1;
7970 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7971 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7972 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7973 ATTRIBUTES are attributes to be applied to the structure.
7975 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7976 the struct was started. */
7978 tree
7979 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7980 struct c_struct_parse_info *enclosing_struct_parse_info)
7982 tree x;
7983 bool toplevel = file_scope == current_scope;
7985 /* If this type was previously laid out as a forward reference,
7986 make sure we lay it out again. */
7988 TYPE_SIZE (t) = NULL_TREE;
7990 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7992 if (pedantic)
7994 for (x = fieldlist; x; x = DECL_CHAIN (x))
7996 if (DECL_NAME (x) != NULL_TREE)
7997 break;
7998 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7999 break;
8002 if (x == NULL_TREE)
8004 if (TREE_CODE (t) == UNION_TYPE)
8006 if (fieldlist)
8007 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8008 else
8009 pedwarn (loc, OPT_Wpedantic, "union has no members");
8011 else
8013 if (fieldlist)
8014 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8015 else
8016 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8021 /* Install struct as DECL_CONTEXT of each field decl.
8022 Also process specified field sizes, found in the DECL_INITIAL,
8023 storing 0 there after the type has been changed to precision equal
8024 to its width, rather than the precision of the specified standard
8025 type. (Correct layout requires the original type to have been preserved
8026 until now.) */
8028 bool saw_named_field = false;
8029 for (x = fieldlist; x; x = DECL_CHAIN (x))
8031 if (TREE_TYPE (x) == error_mark_node)
8032 continue;
8034 DECL_CONTEXT (x) = t;
8036 /* If any field is const, the structure type is pseudo-const. */
8037 if (TREE_READONLY (x))
8038 C_TYPE_FIELDS_READONLY (t) = 1;
8039 else
8041 /* A field that is pseudo-const makes the structure likewise. */
8042 tree t1 = strip_array_types (TREE_TYPE (x));
8043 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8044 C_TYPE_FIELDS_READONLY (t) = 1;
8047 /* Any field that is volatile means variables of this type must be
8048 treated in some ways as volatile. */
8049 if (TREE_THIS_VOLATILE (x))
8050 C_TYPE_FIELDS_VOLATILE (t) = 1;
8052 /* Any field of nominal variable size implies structure is too. */
8053 if (C_DECL_VARIABLE_SIZE (x))
8054 C_TYPE_VARIABLE_SIZE (t) = 1;
8056 if (DECL_C_BIT_FIELD (x))
8058 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8059 DECL_SIZE (x) = bitsize_int (width);
8060 DECL_BIT_FIELD (x) = 1;
8063 if (TYPE_PACKED (t)
8064 && (DECL_BIT_FIELD (x)
8065 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8066 DECL_PACKED (x) = 1;
8068 /* Detect flexible array member in an invalid context. */
8069 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8070 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8071 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8072 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8074 if (TREE_CODE (t) == UNION_TYPE)
8076 error_at (DECL_SOURCE_LOCATION (x),
8077 "flexible array member in union");
8078 TREE_TYPE (x) = error_mark_node;
8080 else if (DECL_CHAIN (x) != NULL_TREE)
8082 error_at (DECL_SOURCE_LOCATION (x),
8083 "flexible array member not at end of struct");
8084 TREE_TYPE (x) = error_mark_node;
8086 else if (!saw_named_field)
8088 error_at (DECL_SOURCE_LOCATION (x),
8089 "flexible array member in a struct with no named "
8090 "members");
8091 TREE_TYPE (x) = error_mark_node;
8095 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8096 && flexible_array_type_p (TREE_TYPE (x)))
8097 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8098 "invalid use of structure with flexible array member");
8100 if (DECL_NAME (x)
8101 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8102 saw_named_field = true;
8105 detect_field_duplicates (fieldlist);
8107 /* Now we have the nearly final fieldlist. Record it,
8108 then lay out the structure or union (including the fields). */
8110 TYPE_FIELDS (t) = fieldlist;
8112 maybe_apply_pragma_scalar_storage_order (t);
8114 layout_type (t);
8116 if (TYPE_SIZE_UNIT (t)
8117 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8118 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8119 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8120 error ("type %qT is too large", t);
8122 /* Give bit-fields their proper types and rewrite the type of array fields
8123 with scalar component if the enclosing type has reverse storage order. */
8124 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8126 if (TREE_CODE (field) == FIELD_DECL
8127 && DECL_INITIAL (field)
8128 && TREE_TYPE (field) != error_mark_node)
8130 unsigned HOST_WIDE_INT width
8131 = tree_to_uhwi (DECL_INITIAL (field));
8132 tree type = TREE_TYPE (field);
8133 if (width != TYPE_PRECISION (type))
8135 TREE_TYPE (field)
8136 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8137 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8139 DECL_INITIAL (field) = NULL_TREE;
8141 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8142 && TREE_CODE (field) == FIELD_DECL
8143 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8145 tree ftype = TREE_TYPE (field);
8146 tree ctype = strip_array_types (ftype);
8147 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8149 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8150 tree *typep = &fmain_type;
8151 do {
8152 *typep = build_distinct_type_copy (*typep);
8153 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8154 typep = &TREE_TYPE (*typep);
8155 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8156 TREE_TYPE (field)
8157 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8162 /* Now we have the truly final field list.
8163 Store it in this type and in the variants. */
8165 TYPE_FIELDS (t) = fieldlist;
8167 /* If there are lots of fields, sort so we can look through them fast.
8168 We arbitrarily consider 16 or more elts to be "a lot". */
8171 int len = 0;
8173 for (x = fieldlist; x; x = DECL_CHAIN (x))
8175 if (len > 15 || DECL_NAME (x) == NULL)
8176 break;
8177 len += 1;
8180 if (len > 15)
8182 tree *field_array;
8183 struct lang_type *space;
8184 struct sorted_fields_type *space2;
8186 len += list_length (x);
8188 /* Use the same allocation policy here that make_node uses, to
8189 ensure that this lives as long as the rest of the struct decl.
8190 All decls in an inline function need to be saved. */
8192 space = ggc_cleared_alloc<struct lang_type> ();
8193 space2 = (sorted_fields_type *) ggc_internal_alloc
8194 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8196 len = 0;
8197 space->s = space2;
8198 field_array = &space2->elts[0];
8199 for (x = fieldlist; x; x = DECL_CHAIN (x))
8201 field_array[len++] = x;
8203 /* If there is anonymous struct or union, break out of the loop. */
8204 if (DECL_NAME (x) == NULL)
8205 break;
8207 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8208 if (x == NULL)
8210 TYPE_LANG_SPECIFIC (t) = space;
8211 TYPE_LANG_SPECIFIC (t)->s->len = len;
8212 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8213 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8218 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8219 in dwarf2out via rest_of_decl_compilation below and means
8220 something totally different. Since we will be clearing
8221 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8222 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8223 C_TYPE_INCOMPLETE_VARS should use some language specific
8224 node. */
8225 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8226 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8228 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8229 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8230 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8231 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8232 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8233 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8236 /* If this was supposed to be a transparent union, but we can't
8237 make it one, warn and turn off the flag. */
8238 if (TREE_CODE (t) == UNION_TYPE
8239 && TYPE_TRANSPARENT_AGGR (t)
8240 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8242 TYPE_TRANSPARENT_AGGR (t) = 0;
8243 warning_at (loc, 0, "union cannot be made transparent");
8246 /* Update type location to the one of the definition, instead of e.g.
8247 a forward declaration. */
8248 if (TYPE_STUB_DECL (t))
8249 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8251 /* Finish debugging output for this type. */
8252 rest_of_type_compilation (t, toplevel);
8254 /* If this structure or union completes the type of any previous
8255 variable declaration, lay it out and output its rtl. */
8256 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8258 tree decl = TREE_VALUE (x);
8259 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8260 layout_array_type (TREE_TYPE (decl));
8261 if (TREE_CODE (decl) != TYPE_DECL)
8263 layout_decl (decl, 0);
8264 if (c_dialect_objc ())
8265 objc_check_decl (decl);
8266 rest_of_decl_compilation (decl, toplevel, 0);
8270 /* If we're inside a function proper, i.e. not file-scope and not still
8271 parsing parameters, then arrange for the size of a variable sized type
8272 to be bound now. */
8273 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8274 add_stmt (build_stmt (loc,
8275 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8277 if (warn_cxx_compat)
8278 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8280 delete struct_parse_info;
8282 struct_parse_info = enclosing_struct_parse_info;
8284 /* If this struct is defined inside a struct, add it to
8285 struct_types. */
8286 if (warn_cxx_compat
8287 && struct_parse_info != NULL
8288 && !in_sizeof && !in_typeof && !in_alignof)
8289 struct_parse_info->struct_types.safe_push (t);
8291 return t;
8294 static struct {
8295 gt_pointer_operator new_value;
8296 void *cookie;
8297 } resort_data;
8299 /* This routine compares two fields like field_decl_cmp but using the
8300 pointer operator in resort_data. */
8302 static int
8303 resort_field_decl_cmp (const void *x_p, const void *y_p)
8305 const tree *const x = (const tree *) x_p;
8306 const tree *const y = (const tree *) y_p;
8308 if (DECL_NAME (*x) == DECL_NAME (*y))
8309 /* A nontype is "greater" than a type. */
8310 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8311 if (DECL_NAME (*x) == NULL_TREE)
8312 return -1;
8313 if (DECL_NAME (*y) == NULL_TREE)
8314 return 1;
8316 tree d1 = DECL_NAME (*x);
8317 tree d2 = DECL_NAME (*y);
8318 resort_data.new_value (&d1, resort_data.cookie);
8319 resort_data.new_value (&d2, resort_data.cookie);
8320 if (d1 < d2)
8321 return -1;
8323 return 1;
8326 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8328 void
8329 resort_sorted_fields (void *obj,
8330 void * ARG_UNUSED (orig_obj),
8331 gt_pointer_operator new_value,
8332 void *cookie)
8334 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8335 resort_data.new_value = new_value;
8336 resort_data.cookie = cookie;
8337 qsort (&sf->elts[0], sf->len, sizeof (tree),
8338 resort_field_decl_cmp);
8341 /* Lay out the type T, and its element type, and so on. */
8343 static void
8344 layout_array_type (tree t)
8346 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8347 layout_array_type (TREE_TYPE (t));
8348 layout_type (t);
8351 /* Begin compiling the definition of an enumeration type.
8352 NAME is its name (or null if anonymous).
8353 LOC is the enum's location.
8354 Returns the type object, as yet incomplete.
8355 Also records info about it so that build_enumerator
8356 may be used to declare the individual values as they are read. */
8358 tree
8359 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8361 tree enumtype = NULL_TREE;
8362 location_t enumloc = UNKNOWN_LOCATION;
8364 /* If this is the real definition for a previous forward reference,
8365 fill in the contents in the same object that used to be the
8366 forward reference. */
8368 if (name != NULL_TREE)
8369 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8371 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8373 enumtype = make_node (ENUMERAL_TYPE);
8374 pushtag (loc, name, enumtype);
8376 /* Update type location to the one of the definition, instead of e.g.
8377 a forward declaration. */
8378 else if (TYPE_STUB_DECL (enumtype))
8380 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8381 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8384 if (C_TYPE_BEING_DEFINED (enumtype))
8385 error_at (loc, "nested redefinition of %<enum %E%>", name);
8387 C_TYPE_BEING_DEFINED (enumtype) = 1;
8389 if (TYPE_VALUES (enumtype) != NULL_TREE)
8391 /* This enum is a named one that has been declared already. */
8392 error_at (loc, "redeclaration of %<enum %E%>", name);
8393 if (enumloc != UNKNOWN_LOCATION)
8394 inform (enumloc, "originally defined here");
8396 /* Completely replace its old definition.
8397 The old enumerators remain defined, however. */
8398 TYPE_VALUES (enumtype) = NULL_TREE;
8401 the_enum->enum_next_value = integer_zero_node;
8402 the_enum->enum_overflow = 0;
8404 if (flag_short_enums)
8405 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8406 TYPE_PACKED (v) = 1;
8408 /* FIXME: This will issue a warning for a use of a type defined
8409 within sizeof in a statement expr. This is not terribly serious
8410 as C++ doesn't permit statement exprs within sizeof anyhow. */
8411 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8412 warning_at (loc, OPT_Wc___compat,
8413 "defining type in %qs expression is invalid in C++",
8414 (in_sizeof
8415 ? "sizeof"
8416 : (in_typeof ? "typeof" : "alignof")));
8418 return enumtype;
8421 /* After processing and defining all the values of an enumeration type,
8422 install their decls in the enumeration type and finish it off.
8423 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8424 and ATTRIBUTES are the specified attributes.
8425 Returns ENUMTYPE. */
8427 tree
8428 finish_enum (tree enumtype, tree values, tree attributes)
8430 tree pair, tem;
8431 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8432 int precision;
8433 signop sign;
8434 bool toplevel = (file_scope == current_scope);
8435 struct lang_type *lt;
8437 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8439 /* Calculate the maximum value of any enumerator in this type. */
8441 if (values == error_mark_node)
8442 minnode = maxnode = integer_zero_node;
8443 else
8445 minnode = maxnode = TREE_VALUE (values);
8446 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8448 tree value = TREE_VALUE (pair);
8449 if (tree_int_cst_lt (maxnode, value))
8450 maxnode = value;
8451 if (tree_int_cst_lt (value, minnode))
8452 minnode = value;
8456 /* Construct the final type of this enumeration. It is the same
8457 as one of the integral types - the narrowest one that fits, except
8458 that normally we only go as narrow as int - and signed iff any of
8459 the values are negative. */
8460 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8461 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8462 tree_int_cst_min_precision (maxnode, sign));
8464 /* If the precision of the type was specified with an attribute and it
8465 was too small, give an error. Otherwise, use it. */
8466 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8468 if (precision > TYPE_PRECISION (enumtype))
8470 TYPE_PRECISION (enumtype) = 0;
8471 error ("specified mode too small for enumeral values");
8473 else
8474 precision = TYPE_PRECISION (enumtype);
8476 else
8477 TYPE_PRECISION (enumtype) = 0;
8479 if (TYPE_PACKED (enumtype)
8480 || precision > TYPE_PRECISION (integer_type_node)
8481 || TYPE_PRECISION (enumtype))
8483 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8484 if (tem == NULL)
8486 warning (0, "enumeration values exceed range of largest integer");
8487 tem = long_long_integer_type_node;
8490 else
8491 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8493 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8494 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8495 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8496 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8497 TYPE_SIZE (enumtype) = NULL_TREE;
8498 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8500 layout_type (enumtype);
8502 if (values != error_mark_node)
8504 /* Change the type of the enumerators to be the enum type. We
8505 need to do this irrespective of the size of the enum, for
8506 proper type checking. Replace the DECL_INITIALs of the
8507 enumerators, and the value slots of the list, with copies
8508 that have the enum type; they cannot be modified in place
8509 because they may be shared (e.g. integer_zero_node) Finally,
8510 change the purpose slots to point to the names of the decls. */
8511 for (pair = values; pair; pair = TREE_CHAIN (pair))
8513 tree enu = TREE_PURPOSE (pair);
8514 tree ini = DECL_INITIAL (enu);
8516 TREE_TYPE (enu) = enumtype;
8518 /* The ISO C Standard mandates enumerators to have type int,
8519 even though the underlying type of an enum type is
8520 unspecified. However, GCC allows enumerators of any
8521 integer type as an extensions. build_enumerator()
8522 converts any enumerators that fit in an int to type int,
8523 to avoid promotions to unsigned types when comparing
8524 integers with enumerators that fit in the int range.
8525 When -pedantic is given, build_enumerator() would have
8526 already warned about those that don't fit. Here we
8527 convert the rest to the enumerator type. */
8528 if (TREE_TYPE (ini) != integer_type_node)
8529 ini = convert (enumtype, ini);
8531 DECL_INITIAL (enu) = ini;
8532 TREE_PURPOSE (pair) = DECL_NAME (enu);
8533 TREE_VALUE (pair) = ini;
8536 TYPE_VALUES (enumtype) = values;
8539 /* Record the min/max values so that we can warn about bit-field
8540 enumerations that are too small for the values. */
8541 lt = ggc_cleared_alloc<struct lang_type> ();
8542 lt->enum_min = minnode;
8543 lt->enum_max = maxnode;
8544 TYPE_LANG_SPECIFIC (enumtype) = lt;
8546 /* Fix up all variant types of this enum type. */
8547 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8549 if (tem == enumtype)
8550 continue;
8551 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8552 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8553 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8554 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8555 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8556 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8557 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8558 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8559 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8560 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8561 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8564 /* Finish debugging output for this type. */
8565 rest_of_type_compilation (enumtype, toplevel);
8567 /* If this enum is defined inside a struct, add it to
8568 struct_types. */
8569 if (warn_cxx_compat
8570 && struct_parse_info != NULL
8571 && !in_sizeof && !in_typeof && !in_alignof)
8572 struct_parse_info->struct_types.safe_push (enumtype);
8574 return enumtype;
8577 /* Build and install a CONST_DECL for one value of the
8578 current enumeration type (one that was begun with start_enum).
8579 DECL_LOC is the location of the enumerator.
8580 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8581 Return a tree-list containing the CONST_DECL and its value.
8582 Assignment of sequential values by default is handled here. */
8584 tree
8585 build_enumerator (location_t decl_loc, location_t loc,
8586 struct c_enum_contents *the_enum, tree name, tree value)
8588 tree decl, type;
8590 /* Validate and default VALUE. */
8592 if (value != NULL_TREE)
8594 /* Don't issue more errors for error_mark_node (i.e. an
8595 undeclared identifier) - just ignore the value expression. */
8596 if (value == error_mark_node)
8597 value = NULL_TREE;
8598 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8600 error_at (loc, "enumerator value for %qE is not an integer constant",
8601 name);
8602 value = NULL_TREE;
8604 else
8606 if (TREE_CODE (value) != INTEGER_CST)
8608 value = c_fully_fold (value, false, NULL);
8609 if (TREE_CODE (value) == INTEGER_CST)
8610 pedwarn (loc, OPT_Wpedantic,
8611 "enumerator value for %qE is not an integer "
8612 "constant expression", name);
8614 if (TREE_CODE (value) != INTEGER_CST)
8616 error ("enumerator value for %qE is not an integer constant",
8617 name);
8618 value = NULL_TREE;
8620 else
8622 value = default_conversion (value);
8623 constant_expression_warning (value);
8628 /* Default based on previous value. */
8629 /* It should no longer be possible to have NON_LVALUE_EXPR
8630 in the default. */
8631 if (value == NULL_TREE)
8633 value = the_enum->enum_next_value;
8634 if (the_enum->enum_overflow)
8635 error_at (loc, "overflow in enumeration values");
8637 /* Even though the underlying type of an enum is unspecified, the
8638 type of enumeration constants is explicitly defined as int
8639 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8640 an extension. */
8641 else if (!int_fits_type_p (value, integer_type_node))
8642 pedwarn (loc, OPT_Wpedantic,
8643 "ISO C restricts enumerator values to range of %<int%>");
8645 /* The ISO C Standard mandates enumerators to have type int, even
8646 though the underlying type of an enum type is unspecified.
8647 However, GCC allows enumerators of any integer type as an
8648 extensions. Here we convert any enumerators that fit in an int
8649 to type int, to avoid promotions to unsigned types when comparing
8650 integers with enumerators that fit in the int range. When
8651 -pedantic is given, we would have already warned about those that
8652 don't fit. We have to do this here rather than in finish_enum
8653 because this value may be used to define more enumerators. */
8654 if (int_fits_type_p (value, integer_type_node))
8655 value = convert (integer_type_node, value);
8657 /* Set basis for default for next value. */
8658 the_enum->enum_next_value
8659 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8660 PLUS_EXPR, value, integer_one_node, false);
8661 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8663 /* Now create a declaration for the enum value name. */
8665 type = TREE_TYPE (value);
8666 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8667 TYPE_PRECISION (integer_type_node)),
8668 (TYPE_PRECISION (type)
8669 >= TYPE_PRECISION (integer_type_node)
8670 && TYPE_UNSIGNED (type)));
8672 decl = build_decl (decl_loc, CONST_DECL, name, type);
8673 DECL_INITIAL (decl) = convert (type, value);
8674 pushdecl (decl);
8676 return tree_cons (decl, value, NULL_TREE);
8680 /* Create the FUNCTION_DECL for a function definition.
8681 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8682 the declaration; they describe the function's name and the type it returns,
8683 but twisted together in a fashion that parallels the syntax of C.
8685 This function creates a binding context for the function body
8686 as well as setting up the FUNCTION_DECL in current_function_decl.
8688 Returns true on success. If the DECLARATOR is not suitable for a function
8689 (it defines a datum instead), we return false to report a parse error. */
8691 bool
8692 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8693 tree attributes)
8695 tree decl1, old_decl;
8696 tree restype, resdecl;
8697 location_t loc;
8699 current_function_returns_value = 0; /* Assume, until we see it does. */
8700 current_function_returns_null = 0;
8701 current_function_returns_abnormally = 0;
8702 warn_about_return_type = 0;
8703 c_switch_stack = NULL;
8705 /* Indicate no valid break/continue context by setting these variables
8706 to some non-null, non-label value. We'll notice and emit the proper
8707 error message in c_finish_bc_stmt. */
8708 c_break_label = c_cont_label = size_zero_node;
8710 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8711 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8712 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8714 /* If the declarator is not suitable for a function definition,
8715 cause a syntax error. */
8716 if (decl1 == NULL_TREE
8717 || TREE_CODE (decl1) != FUNCTION_DECL)
8718 return false;
8720 loc = DECL_SOURCE_LOCATION (decl1);
8722 c_decl_attributes (&decl1, attributes, 0);
8724 if (DECL_DECLARED_INLINE_P (decl1)
8725 && DECL_UNINLINABLE (decl1)
8726 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8727 warning_at (loc, OPT_Wattributes,
8728 "inline function %qD given attribute noinline",
8729 decl1);
8731 /* Handle gnu_inline attribute. */
8732 if (declspecs->inline_p
8733 && !flag_gnu89_inline
8734 && TREE_CODE (decl1) == FUNCTION_DECL
8735 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8736 || current_function_decl))
8738 if (declspecs->storage_class != csc_static)
8739 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8742 announce_function (decl1);
8744 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8746 error_at (loc, "return type is an incomplete type");
8747 /* Make it return void instead. */
8748 TREE_TYPE (decl1)
8749 = build_function_type (void_type_node,
8750 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8753 if (warn_about_return_type)
8754 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8755 : (warn_return_type ? OPT_Wreturn_type
8756 : OPT_Wimplicit_int),
8757 "return type defaults to %<int%>");
8759 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8760 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8761 DECL_INITIAL (decl1) = error_mark_node;
8763 /* A nested function is not global. */
8764 if (current_function_decl != NULL_TREE)
8765 TREE_PUBLIC (decl1) = 0;
8767 /* If this definition isn't a prototype and we had a prototype declaration
8768 before, copy the arg type info from that prototype. */
8769 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8770 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8771 old_decl = NULL_TREE;
8772 current_function_prototype_locus = UNKNOWN_LOCATION;
8773 current_function_prototype_built_in = false;
8774 current_function_prototype_arg_types = NULL_TREE;
8775 if (!prototype_p (TREE_TYPE (decl1)))
8777 if (old_decl != NULL_TREE
8778 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8779 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8780 TREE_TYPE (TREE_TYPE (old_decl))))
8782 if (stdarg_p (TREE_TYPE (old_decl)))
8784 warning_at (loc, 0, "%q+D defined as variadic function "
8785 "without prototype", decl1);
8786 locate_old_decl (old_decl);
8788 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8789 TREE_TYPE (decl1));
8790 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8791 current_function_prototype_built_in
8792 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8793 current_function_prototype_arg_types
8794 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8796 if (TREE_PUBLIC (decl1))
8798 /* If there is an external prototype declaration of this
8799 function, record its location but do not copy information
8800 to this decl. This may be an invisible declaration
8801 (built-in or in a scope which has finished) or simply
8802 have more refined argument types than any declaration
8803 found above. */
8804 struct c_binding *b;
8805 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8806 if (B_IN_SCOPE (b, external_scope))
8807 break;
8808 if (b)
8810 tree ext_decl, ext_type;
8811 ext_decl = b->decl;
8812 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8813 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8814 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8815 TREE_TYPE (ext_type)))
8817 current_function_prototype_locus
8818 = DECL_SOURCE_LOCATION (ext_decl);
8819 current_function_prototype_built_in
8820 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8821 current_function_prototype_arg_types
8822 = TYPE_ARG_TYPES (ext_type);
8828 /* Optionally warn of old-fashioned def with no previous prototype. */
8829 if (warn_strict_prototypes
8830 && old_decl != error_mark_node
8831 && !prototype_p (TREE_TYPE (decl1))
8832 && C_DECL_ISNT_PROTOTYPE (old_decl))
8833 warning_at (loc, OPT_Wstrict_prototypes,
8834 "function declaration isn%'t a prototype");
8835 /* Optionally warn of any global def with no previous prototype. */
8836 else if (warn_missing_prototypes
8837 && old_decl != error_mark_node
8838 && TREE_PUBLIC (decl1)
8839 && !MAIN_NAME_P (DECL_NAME (decl1))
8840 && C_DECL_ISNT_PROTOTYPE (old_decl)
8841 && !DECL_DECLARED_INLINE_P (decl1))
8842 warning_at (loc, OPT_Wmissing_prototypes,
8843 "no previous prototype for %qD", decl1);
8844 /* Optionally warn of any def with no previous prototype
8845 if the function has already been used. */
8846 else if (warn_missing_prototypes
8847 && old_decl != NULL_TREE
8848 && old_decl != error_mark_node
8849 && TREE_USED (old_decl)
8850 && !prototype_p (TREE_TYPE (old_decl)))
8851 warning_at (loc, OPT_Wmissing_prototypes,
8852 "%qD was used with no prototype before its definition", decl1);
8853 /* Optionally warn of any global def with no previous declaration. */
8854 else if (warn_missing_declarations
8855 && TREE_PUBLIC (decl1)
8856 && old_decl == NULL_TREE
8857 && !MAIN_NAME_P (DECL_NAME (decl1))
8858 && !DECL_DECLARED_INLINE_P (decl1))
8859 warning_at (loc, OPT_Wmissing_declarations,
8860 "no previous declaration for %qD",
8861 decl1);
8862 /* Optionally warn of any def with no previous declaration
8863 if the function has already been used. */
8864 else if (warn_missing_declarations
8865 && old_decl != NULL_TREE
8866 && old_decl != error_mark_node
8867 && TREE_USED (old_decl)
8868 && C_DECL_IMPLICIT (old_decl))
8869 warning_at (loc, OPT_Wmissing_declarations,
8870 "%qD was used with no declaration before its definition", decl1);
8872 /* This function exists in static storage.
8873 (This does not mean `static' in the C sense!) */
8874 TREE_STATIC (decl1) = 1;
8876 /* This is the earliest point at which we might know the assembler
8877 name of the function. Thus, if it's set before this, die horribly. */
8878 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8880 /* If #pragma weak was used, mark the decl weak now. */
8881 if (current_scope == file_scope)
8882 maybe_apply_pragma_weak (decl1);
8884 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8885 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8887 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8888 != integer_type_node)
8889 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8890 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8891 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8892 decl1);
8894 check_main_parameter_types (decl1);
8896 if (!TREE_PUBLIC (decl1))
8897 pedwarn (loc, OPT_Wmain,
8898 "%qD is normally a non-static function", decl1);
8901 /* Record the decl so that the function name is defined.
8902 If we already have a decl for this name, and it is a FUNCTION_DECL,
8903 use the old decl. */
8905 current_function_decl = pushdecl (decl1);
8907 push_scope ();
8908 declare_parm_level ();
8910 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8911 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8912 DECL_ARTIFICIAL (resdecl) = 1;
8913 DECL_IGNORED_P (resdecl) = 1;
8914 DECL_RESULT (current_function_decl) = resdecl;
8916 start_fname_decls ();
8918 return true;
8921 /* Subroutine of store_parm_decls which handles new-style function
8922 definitions (prototype format). The parms already have decls, so we
8923 need only record them as in effect and complain if any redundant
8924 old-style parm decls were written. */
8925 static void
8926 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8928 tree decl;
8929 c_arg_tag *tag;
8930 unsigned ix;
8932 if (current_scope->bindings)
8934 error_at (DECL_SOURCE_LOCATION (fndecl),
8935 "old-style parameter declarations in prototyped "
8936 "function definition");
8938 /* Get rid of the old-style declarations. */
8939 pop_scope ();
8940 push_scope ();
8942 /* Don't issue this warning for nested functions, and don't issue this
8943 warning if we got here because ARG_INFO_TYPES was error_mark_node
8944 (this happens when a function definition has just an ellipsis in
8945 its parameter list). */
8946 else if (!in_system_header_at (input_location)
8947 && !current_function_scope
8948 && arg_info->types != error_mark_node)
8949 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8950 "traditional C rejects ISO C style function definitions");
8952 /* Now make all the parameter declarations visible in the function body.
8953 We can bypass most of the grunt work of pushdecl. */
8954 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8956 DECL_CONTEXT (decl) = current_function_decl;
8957 if (DECL_NAME (decl))
8959 bind (DECL_NAME (decl), decl, current_scope,
8960 /*invisible=*/false, /*nested=*/false,
8961 UNKNOWN_LOCATION);
8962 if (!TREE_USED (decl))
8963 warn_if_shadowing (decl);
8965 else
8966 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8969 /* Record the parameter list in the function declaration. */
8970 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8972 /* Now make all the ancillary declarations visible, likewise. */
8973 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8975 DECL_CONTEXT (decl) = current_function_decl;
8976 if (DECL_NAME (decl))
8977 bind (DECL_NAME (decl), decl, current_scope,
8978 /*invisible=*/false,
8979 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8980 UNKNOWN_LOCATION);
8983 /* And all the tag declarations. */
8984 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8985 if (tag->id)
8986 bind (tag->id, tag->type, current_scope,
8987 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8990 /* Subroutine of store_parm_decls which handles old-style function
8991 definitions (separate parameter list and declarations). */
8993 static void
8994 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8996 struct c_binding *b;
8997 tree parm, decl, last;
8998 tree parmids = arg_info->parms;
8999 hash_set<tree> seen_args;
9001 if (!in_system_header_at (input_location))
9002 warning_at (DECL_SOURCE_LOCATION (fndecl),
9003 OPT_Wold_style_definition, "old-style function definition");
9005 /* Match each formal parameter name with its declaration. Save each
9006 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9007 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9009 if (TREE_VALUE (parm) == NULL_TREE)
9011 error_at (DECL_SOURCE_LOCATION (fndecl),
9012 "parameter name missing from parameter list");
9013 TREE_PURPOSE (parm) = NULL_TREE;
9014 continue;
9017 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9018 if (b && B_IN_CURRENT_SCOPE (b))
9020 decl = b->decl;
9021 /* Skip erroneous parameters. */
9022 if (decl == error_mark_node)
9023 continue;
9024 /* If we got something other than a PARM_DECL it is an error. */
9025 if (TREE_CODE (decl) != PARM_DECL)
9027 error_at (DECL_SOURCE_LOCATION (decl),
9028 "%qD declared as a non-parameter", decl);
9029 continue;
9031 /* If the declaration is already marked, we have a duplicate
9032 name. Complain and ignore the duplicate. */
9033 else if (seen_args.contains (decl))
9035 error_at (DECL_SOURCE_LOCATION (decl),
9036 "multiple parameters named %qD", decl);
9037 TREE_PURPOSE (parm) = NULL_TREE;
9038 continue;
9040 /* If the declaration says "void", complain and turn it into
9041 an int. */
9042 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9044 error_at (DECL_SOURCE_LOCATION (decl),
9045 "parameter %qD declared with void type", decl);
9046 TREE_TYPE (decl) = integer_type_node;
9047 DECL_ARG_TYPE (decl) = integer_type_node;
9048 layout_decl (decl, 0);
9050 warn_if_shadowing (decl);
9052 /* If no declaration found, default to int. */
9053 else
9055 /* FIXME diagnostics: This should be the location of the argument,
9056 not the FNDECL. E.g., for an old-style declaration
9058 int f10(v) { blah; }
9060 We should use the location of the V, not the F10.
9061 Unfortunately, the V is an IDENTIFIER_NODE which has no
9062 location. In the future we need locations for c_arg_info
9063 entries.
9065 See gcc.dg/Wshadow-3.c for an example of this problem. */
9066 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9067 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9068 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9069 pushdecl (decl);
9070 warn_if_shadowing (decl);
9072 if (flag_isoc99)
9073 pedwarn (DECL_SOURCE_LOCATION (decl),
9074 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9075 decl);
9076 else
9077 warning_at (DECL_SOURCE_LOCATION (decl),
9078 OPT_Wmissing_parameter_type,
9079 "type of %qD defaults to %<int%>", decl);
9082 TREE_PURPOSE (parm) = decl;
9083 seen_args.add (decl);
9086 /* Now examine the parms chain for incomplete declarations
9087 and declarations with no corresponding names. */
9089 for (b = current_scope->bindings; b; b = b->prev)
9091 parm = b->decl;
9092 if (TREE_CODE (parm) != PARM_DECL)
9093 continue;
9095 if (TREE_TYPE (parm) != error_mark_node
9096 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9098 error_at (DECL_SOURCE_LOCATION (parm),
9099 "parameter %qD has incomplete type", parm);
9100 TREE_TYPE (parm) = error_mark_node;
9103 if (!seen_args.contains (parm))
9105 error_at (DECL_SOURCE_LOCATION (parm),
9106 "declaration for parameter %qD but no such parameter",
9107 parm);
9109 /* Pretend the parameter was not missing.
9110 This gets us to a standard state and minimizes
9111 further error messages. */
9112 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9116 /* Chain the declarations together in the order of the list of
9117 names. Store that chain in the function decl, replacing the
9118 list of names. Update the current scope to match. */
9119 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9121 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9122 if (TREE_PURPOSE (parm))
9123 break;
9124 if (parm && TREE_PURPOSE (parm))
9126 last = TREE_PURPOSE (parm);
9127 DECL_ARGUMENTS (fndecl) = last;
9129 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9130 if (TREE_PURPOSE (parm))
9132 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9133 last = TREE_PURPOSE (parm);
9135 DECL_CHAIN (last) = NULL_TREE;
9138 /* If there was a previous prototype,
9139 set the DECL_ARG_TYPE of each argument according to
9140 the type previously specified, and report any mismatches. */
9142 if (current_function_prototype_arg_types)
9144 tree type;
9145 for (parm = DECL_ARGUMENTS (fndecl),
9146 type = current_function_prototype_arg_types;
9147 parm || (type != NULL_TREE
9148 && TREE_VALUE (type) != error_mark_node
9149 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9150 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9152 if (parm == NULL_TREE
9153 || type == NULL_TREE
9154 || (TREE_VALUE (type) != error_mark_node
9155 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9157 if (current_function_prototype_built_in)
9158 warning_at (DECL_SOURCE_LOCATION (fndecl),
9159 0, "number of arguments doesn%'t match "
9160 "built-in prototype");
9161 else
9163 /* FIXME diagnostics: This should be the location of
9164 FNDECL, but there is bug when a prototype is
9165 declared inside function context, but defined
9166 outside of it (e.g., gcc.dg/pr15698-2.c). In
9167 which case FNDECL gets the location of the
9168 prototype, not the definition. */
9169 error_at (input_location,
9170 "number of arguments doesn%'t match prototype");
9172 error_at (current_function_prototype_locus,
9173 "prototype declaration");
9175 break;
9177 /* Type for passing arg must be consistent with that
9178 declared for the arg. ISO C says we take the unqualified
9179 type for parameters declared with qualified type. */
9180 if (TREE_TYPE (parm) != error_mark_node
9181 && TREE_VALUE (type) != error_mark_node
9182 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9183 != TYPE_ATOMIC (TREE_VALUE (type)))
9184 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9185 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9187 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9188 == TYPE_ATOMIC (TREE_VALUE (type)))
9189 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9190 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9192 /* Adjust argument to match prototype. E.g. a previous
9193 `int foo(float);' prototype causes
9194 `int foo(x) float x; {...}' to be treated like
9195 `int foo(float x) {...}'. This is particularly
9196 useful for argument types like uid_t. */
9197 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9199 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9200 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9201 && (TYPE_PRECISION (TREE_TYPE (parm))
9202 < TYPE_PRECISION (integer_type_node)))
9203 DECL_ARG_TYPE (parm)
9204 = c_type_promotes_to (TREE_TYPE (parm));
9206 /* ??? Is it possible to get here with a
9207 built-in prototype or will it always have
9208 been diagnosed as conflicting with an
9209 old-style definition and discarded? */
9210 if (current_function_prototype_built_in)
9211 warning_at (DECL_SOURCE_LOCATION (parm),
9212 OPT_Wpedantic, "promoted argument %qD "
9213 "doesn%'t match built-in prototype", parm);
9214 else
9216 pedwarn (DECL_SOURCE_LOCATION (parm),
9217 OPT_Wpedantic, "promoted argument %qD "
9218 "doesn%'t match prototype", parm);
9219 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9220 "prototype declaration");
9223 else
9225 if (current_function_prototype_built_in)
9226 warning_at (DECL_SOURCE_LOCATION (parm),
9227 0, "argument %qD doesn%'t match "
9228 "built-in prototype", parm);
9229 else
9231 error_at (DECL_SOURCE_LOCATION (parm),
9232 "argument %qD doesn%'t match prototype", parm);
9233 error_at (current_function_prototype_locus,
9234 "prototype declaration");
9239 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9242 /* Otherwise, create a prototype that would match. */
9244 else
9246 tree actual = NULL_TREE, last = NULL_TREE, type;
9248 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9250 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9251 if (last)
9252 TREE_CHAIN (last) = type;
9253 else
9254 actual = type;
9255 last = type;
9257 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9258 if (last)
9259 TREE_CHAIN (last) = type;
9260 else
9261 actual = type;
9263 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9264 of the type of this function, but we need to avoid having this
9265 affect the types of other similarly-typed functions, so we must
9266 first force the generation of an identical (but separate) type
9267 node for the relevant function type. The new node we create
9268 will be a variant of the main variant of the original function
9269 type. */
9271 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9273 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9277 /* Store parameter declarations passed in ARG_INFO into the current
9278 function declaration. */
9280 void
9281 store_parm_decls_from (struct c_arg_info *arg_info)
9283 current_function_arg_info = arg_info;
9284 store_parm_decls ();
9287 /* Called by walk_tree to look for and update context-less labels. */
9289 static tree
9290 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9292 if (TREE_CODE (*tp) == LABEL_EXPR
9293 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9295 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9296 *walk_subtrees = 0;
9299 return NULL_TREE;
9302 /* Store the parameter declarations into the current function declaration.
9303 This is called after parsing the parameter declarations, before
9304 digesting the body of the function.
9306 For an old-style definition, construct a prototype out of the old-style
9307 parameter declarations and inject it into the function's type. */
9309 void
9310 store_parm_decls (void)
9312 tree fndecl = current_function_decl;
9313 bool proto;
9315 /* The argument information block for FNDECL. */
9316 struct c_arg_info *arg_info = current_function_arg_info;
9317 current_function_arg_info = 0;
9319 /* True if this definition is written with a prototype. Note:
9320 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9321 list in a function definition as equivalent to (void) -- an
9322 empty argument list specifies the function has no parameters,
9323 but only (void) sets up a prototype for future calls. */
9324 proto = arg_info->types != 0;
9326 if (proto)
9327 store_parm_decls_newstyle (fndecl, arg_info);
9328 else
9329 store_parm_decls_oldstyle (fndecl, arg_info);
9331 /* The next call to push_scope will be a function body. */
9333 next_is_function_body = true;
9335 /* Write a record describing this function definition to the prototypes
9336 file (if requested). */
9338 gen_aux_info_record (fndecl, 1, 0, proto);
9340 /* Initialize the RTL code for the function. */
9341 allocate_struct_function (fndecl, false);
9343 if (warn_unused_local_typedefs)
9344 cfun->language = ggc_cleared_alloc<language_function> ();
9346 /* Begin the statement tree for this function. */
9347 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9349 /* ??? Insert the contents of the pending sizes list into the function
9350 to be evaluated. The only reason left to have this is
9351 void foo(int n, int array[n++])
9352 because we throw away the array type in favor of a pointer type, and
9353 thus won't naturally see the SAVE_EXPR containing the increment. All
9354 other pending sizes would be handled by gimplify_parameters. */
9355 if (arg_info->pending_sizes)
9357 /* In very special circumstances, e.g. for code like
9358 _Atomic int i = 5;
9359 void f (int a[i += 2]) {}
9360 we need to execute the atomic assignment on function entry.
9361 But in this case, it is not just a straight store, it has the
9362 op= form, which means that build_atomic_assign has generated
9363 gotos, labels, etc. Because at that time the function decl
9364 for F has not been created yet, those labels do not have any
9365 function context. But we have the fndecl now, so update the
9366 labels accordingly. gimplify_expr would crash otherwise. */
9367 walk_tree_without_duplicates (&arg_info->pending_sizes,
9368 set_labels_context_r, fndecl);
9369 add_stmt (arg_info->pending_sizes);
9373 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9374 c_finish_omp_declare_simd for function prototypes. No diagnostics
9375 should be done. */
9377 void
9378 temp_store_parm_decls (tree fndecl, tree parms)
9380 push_scope ();
9381 for (tree p = parms; p; p = DECL_CHAIN (p))
9383 DECL_CONTEXT (p) = fndecl;
9384 if (DECL_NAME (p))
9385 bind (DECL_NAME (p), p, current_scope,
9386 /*invisible=*/false, /*nested=*/false,
9387 UNKNOWN_LOCATION);
9391 /* Undo what temp_store_parm_decls did. */
9393 void
9394 temp_pop_parm_decls (void)
9396 /* Clear all bindings in this temporary scope, so that
9397 pop_scope doesn't create a BLOCK. */
9398 struct c_binding *b = current_scope->bindings;
9399 current_scope->bindings = NULL;
9400 for (; b; b = free_binding_and_advance (b))
9402 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9403 || b->decl == error_mark_node);
9404 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9405 I_SYMBOL_BINDING (b->id) = b->shadowed;
9406 if (b->shadowed && b->shadowed->u.type)
9407 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9409 pop_scope ();
9413 /* Finish up a function declaration and compile that function
9414 all the way to assembler language output. Then free the storage
9415 for the function definition.
9417 This is called after parsing the body of the function definition. */
9419 void
9420 finish_function (void)
9422 tree fndecl = current_function_decl;
9424 if (c_dialect_objc ())
9425 objc_finish_function ();
9427 if (TREE_CODE (fndecl) == FUNCTION_DECL
9428 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9430 tree args = DECL_ARGUMENTS (fndecl);
9431 for (; args; args = DECL_CHAIN (args))
9433 tree type = TREE_TYPE (args);
9434 if (INTEGRAL_TYPE_P (type)
9435 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9436 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9440 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9441 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9443 /* Must mark the RESULT_DECL as being in this function. */
9445 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9446 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9448 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9449 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9450 == integer_type_node && flag_isoc99)
9452 /* Hack. We don't want the middle-end to warn that this return
9453 is unreachable, so we mark its location as special. Using
9454 UNKNOWN_LOCATION has the problem that it gets clobbered in
9455 annotate_one_with_locus. A cleaner solution might be to
9456 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9458 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9461 /* Tie off the statement tree for this function. */
9462 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9464 finish_fname_decls ();
9466 /* Complain if there's just no return statement. */
9467 if (warn_return_type
9468 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9469 && !current_function_returns_value && !current_function_returns_null
9470 /* Don't complain if we are no-return. */
9471 && !current_function_returns_abnormally
9472 /* Don't complain if we are declared noreturn. */
9473 && !TREE_THIS_VOLATILE (fndecl)
9474 /* Don't warn for main(). */
9475 && !MAIN_NAME_P (DECL_NAME (fndecl))
9476 /* Or if they didn't actually specify a return type. */
9477 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9478 /* Normally, with -Wreturn-type, flow will complain, but we might
9479 optimize out static functions. */
9480 && !TREE_PUBLIC (fndecl))
9482 warning (OPT_Wreturn_type,
9483 "no return statement in function returning non-void");
9484 TREE_NO_WARNING (fndecl) = 1;
9487 /* Complain about parameters that are only set, but never otherwise used. */
9488 if (warn_unused_but_set_parameter)
9490 tree decl;
9492 for (decl = DECL_ARGUMENTS (fndecl);
9493 decl;
9494 decl = DECL_CHAIN (decl))
9495 if (TREE_USED (decl)
9496 && TREE_CODE (decl) == PARM_DECL
9497 && !DECL_READ_P (decl)
9498 && DECL_NAME (decl)
9499 && !DECL_ARTIFICIAL (decl)
9500 && !TREE_NO_WARNING (decl))
9501 warning_at (DECL_SOURCE_LOCATION (decl),
9502 OPT_Wunused_but_set_parameter,
9503 "parameter %qD set but not used", decl);
9506 /* Complain about locally defined typedefs that are not used in this
9507 function. */
9508 maybe_warn_unused_local_typedefs ();
9510 /* Possibly warn about unused parameters. */
9511 if (warn_unused_parameter)
9512 do_warn_unused_parameter (fndecl);
9514 /* Store the end of the function, so that we get good line number
9515 info for the epilogue. */
9516 cfun->function_end_locus = input_location;
9518 /* Finalize the ELF visibility for the function. */
9519 c_determine_visibility (fndecl);
9521 /* For GNU C extern inline functions disregard inline limits. */
9522 if (DECL_EXTERNAL (fndecl)
9523 && DECL_DECLARED_INLINE_P (fndecl)
9524 && (flag_gnu89_inline
9525 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9526 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9528 /* Genericize before inlining. Delay genericizing nested functions
9529 until their parent function is genericized. Since finalizing
9530 requires GENERIC, delay that as well. */
9532 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9533 && !undef_nested_function)
9535 if (!decl_function_context (fndecl))
9537 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9538 c_genericize (fndecl);
9540 /* ??? Objc emits functions after finalizing the compilation unit.
9541 This should be cleaned up later and this conditional removed. */
9542 if (symtab->global_info_ready)
9544 cgraph_node::add_new_function (fndecl, false);
9545 return;
9547 cgraph_node::finalize_function (fndecl, false);
9549 else
9551 /* Register this function with cgraph just far enough to get it
9552 added to our parent's nested function list. Handy, since the
9553 C front end doesn't have such a list. */
9554 (void) cgraph_node::get_create (fndecl);
9558 if (!decl_function_context (fndecl))
9559 undef_nested_function = false;
9561 if (cfun->language != NULL)
9563 ggc_free (cfun->language);
9564 cfun->language = NULL;
9567 /* We're leaving the context of this function, so zap cfun.
9568 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9569 tree_rest_of_compilation. */
9570 set_cfun (NULL);
9571 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9572 current_function_decl = NULL;
9575 /* Check the declarations given in a for-loop for satisfying the C99
9576 constraints. If exactly one such decl is found, return it. LOC is
9577 the location of the opening parenthesis of the for loop. The last
9578 parameter allows you to control the "for loop initial declarations
9579 are only allowed in C99 mode". Normally, you should pass
9580 flag_isoc99 as that parameter. But in some cases (Objective-C
9581 foreach loop, for example) we want to run the checks in this
9582 function even if not in C99 mode, so we allow the caller to turn
9583 off the error about not being in C99 mode.
9586 tree
9587 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9589 struct c_binding *b;
9590 tree one_decl = NULL_TREE;
9591 int n_decls = 0;
9593 if (!turn_off_iso_c99_error)
9595 static bool hint = true;
9596 /* If we get here, declarations have been used in a for loop without
9597 the C99 for loop scope. This doesn't make much sense, so don't
9598 allow it. */
9599 error_at (loc, "%<for%> loop initial declarations "
9600 "are only allowed in C99 or C11 mode");
9601 if (hint)
9603 inform (loc,
9604 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9605 "to compile your code");
9606 hint = false;
9608 return NULL_TREE;
9610 /* C99 subclause 6.8.5 paragraph 3:
9612 [#3] The declaration part of a for statement shall only
9613 declare identifiers for objects having storage class auto or
9614 register.
9616 It isn't clear whether, in this sentence, "identifiers" binds to
9617 "shall only declare" or to "objects" - that is, whether all identifiers
9618 declared must be identifiers for objects, or whether the restriction
9619 only applies to those that are. (A question on this in comp.std.c
9620 in November 2000 received no answer.) We implement the strictest
9621 interpretation, to avoid creating an extension which later causes
9622 problems. */
9624 for (b = current_scope->bindings; b; b = b->prev)
9626 tree id = b->id;
9627 tree decl = b->decl;
9629 if (!id)
9630 continue;
9632 switch (TREE_CODE (decl))
9634 case VAR_DECL:
9636 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9637 if (TREE_STATIC (decl))
9638 error_at (decl_loc,
9639 "declaration of static variable %qD in %<for%> loop "
9640 "initial declaration", decl);
9641 else if (DECL_EXTERNAL (decl))
9642 error_at (decl_loc,
9643 "declaration of %<extern%> variable %qD in %<for%> loop "
9644 "initial declaration", decl);
9646 break;
9648 case RECORD_TYPE:
9649 error_at (loc,
9650 "%<struct %E%> declared in %<for%> loop initial "
9651 "declaration", id);
9652 break;
9653 case UNION_TYPE:
9654 error_at (loc,
9655 "%<union %E%> declared in %<for%> loop initial declaration",
9656 id);
9657 break;
9658 case ENUMERAL_TYPE:
9659 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9660 "initial declaration", id);
9661 break;
9662 default:
9663 error_at (loc, "declaration of non-variable "
9664 "%qD in %<for%> loop initial declaration", decl);
9667 n_decls++;
9668 one_decl = decl;
9671 return n_decls == 1 ? one_decl : NULL_TREE;
9674 /* Save and reinitialize the variables
9675 used during compilation of a C function. */
9677 void
9678 c_push_function_context (void)
9680 struct language_function *p = cfun->language;
9681 /* cfun->language might have been already allocated by the use of
9682 -Wunused-local-typedefs. In that case, just re-use it. */
9683 if (p == NULL)
9684 cfun->language = p = ggc_cleared_alloc<language_function> ();
9686 p->base.x_stmt_tree = c_stmt_tree;
9687 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9688 p->x_break_label = c_break_label;
9689 p->x_cont_label = c_cont_label;
9690 p->x_switch_stack = c_switch_stack;
9691 p->arg_info = current_function_arg_info;
9692 p->returns_value = current_function_returns_value;
9693 p->returns_null = current_function_returns_null;
9694 p->returns_abnormally = current_function_returns_abnormally;
9695 p->warn_about_return_type = warn_about_return_type;
9697 push_function_context ();
9700 /* Restore the variables used during compilation of a C function. */
9702 void
9703 c_pop_function_context (void)
9705 struct language_function *p;
9707 pop_function_context ();
9708 p = cfun->language;
9710 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9711 used to store data throughout the life time of the current cfun,
9712 So don't deallocate it. */
9713 if (!warn_unused_local_typedefs)
9714 cfun->language = NULL;
9716 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9717 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9719 /* Stop pointing to the local nodes about to be freed. */
9720 /* But DECL_INITIAL must remain nonzero so we know this
9721 was an actual function definition. */
9722 DECL_INITIAL (current_function_decl) = error_mark_node;
9723 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9726 c_stmt_tree = p->base.x_stmt_tree;
9727 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9728 c_break_label = p->x_break_label;
9729 c_cont_label = p->x_cont_label;
9730 c_switch_stack = p->x_switch_stack;
9731 current_function_arg_info = p->arg_info;
9732 current_function_returns_value = p->returns_value;
9733 current_function_returns_null = p->returns_null;
9734 current_function_returns_abnormally = p->returns_abnormally;
9735 warn_about_return_type = p->warn_about_return_type;
9738 /* The functions below are required for functionality of doing
9739 function at once processing in the C front end. Currently these
9740 functions are not called from anywhere in the C front end, but as
9741 these changes continue, that will change. */
9743 /* Returns the stmt_tree (if any) to which statements are currently
9744 being added. If there is no active statement-tree, NULL is
9745 returned. */
9747 stmt_tree
9748 current_stmt_tree (void)
9750 return &c_stmt_tree;
9753 /* Return the global value of T as a symbol. */
9755 tree
9756 identifier_global_value (tree t)
9758 struct c_binding *b;
9760 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9761 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9762 return b->decl;
9764 return NULL_TREE;
9767 /* In C, the only C-linkage public declaration is at file scope. */
9769 tree
9770 c_linkage_bindings (tree name)
9772 return identifier_global_value (name);
9775 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9776 otherwise the name is found in ridpointers from RID_INDEX. */
9778 void
9779 record_builtin_type (enum rid rid_index, const char *name, tree type)
9781 tree id, decl;
9782 if (name == 0)
9783 id = ridpointers[(int) rid_index];
9784 else
9785 id = get_identifier (name);
9786 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9787 pushdecl (decl);
9788 if (debug_hooks->type_decl)
9789 debug_hooks->type_decl (decl, false);
9792 /* Build the void_list_node (void_type_node having been created). */
9793 tree
9794 build_void_list_node (void)
9796 tree t = build_tree_list (NULL_TREE, void_type_node);
9797 return t;
9800 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9802 struct c_parm *
9803 build_c_parm (struct c_declspecs *specs, tree attrs,
9804 struct c_declarator *declarator,
9805 location_t loc)
9807 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9808 ret->specs = specs;
9809 ret->attrs = attrs;
9810 ret->declarator = declarator;
9811 ret->loc = loc;
9812 return ret;
9815 /* Return a declarator with nested attributes. TARGET is the inner
9816 declarator to which these attributes apply. ATTRS are the
9817 attributes. */
9819 struct c_declarator *
9820 build_attrs_declarator (tree attrs, struct c_declarator *target)
9822 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9823 ret->kind = cdk_attrs;
9824 ret->declarator = target;
9825 ret->u.attrs = attrs;
9826 return ret;
9829 /* Return a declarator for a function with arguments specified by ARGS
9830 and return type specified by TARGET. */
9832 struct c_declarator *
9833 build_function_declarator (struct c_arg_info *args,
9834 struct c_declarator *target)
9836 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9837 ret->kind = cdk_function;
9838 ret->declarator = target;
9839 ret->u.arg_info = args;
9840 return ret;
9843 /* Return a declarator for the identifier IDENT (which may be
9844 NULL_TREE for an abstract declarator). */
9846 struct c_declarator *
9847 build_id_declarator (tree ident)
9849 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9850 ret->kind = cdk_id;
9851 ret->declarator = 0;
9852 ret->u.id = ident;
9853 /* Default value - may get reset to a more precise location. */
9854 ret->id_loc = input_location;
9855 return ret;
9858 /* Return something to represent absolute declarators containing a *.
9859 TARGET is the absolute declarator that the * contains.
9860 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9861 to apply to the pointer type. */
9863 struct c_declarator *
9864 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9865 struct c_declarator *target)
9867 tree attrs;
9868 int quals = 0;
9869 struct c_declarator *itarget = target;
9870 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9871 if (type_quals_attrs)
9873 attrs = type_quals_attrs->attrs;
9874 quals = quals_from_declspecs (type_quals_attrs);
9875 if (attrs != NULL_TREE)
9876 itarget = build_attrs_declarator (attrs, target);
9878 ret->kind = cdk_pointer;
9879 ret->declarator = itarget;
9880 ret->u.pointer_quals = quals;
9881 return ret;
9884 /* Return a pointer to a structure for an empty list of declaration
9885 specifiers. */
9887 struct c_declspecs *
9888 build_null_declspecs (void)
9890 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9891 memset (ret, 0, sizeof *ret);
9892 ret->align_log = -1;
9893 ret->typespec_word = cts_none;
9894 ret->storage_class = csc_none;
9895 ret->expr_const_operands = true;
9896 ret->typespec_kind = ctsk_none;
9897 ret->address_space = ADDR_SPACE_GENERIC;
9898 return ret;
9901 /* Add the address space ADDRSPACE to the declaration specifiers
9902 SPECS, returning SPECS. */
9904 struct c_declspecs *
9905 declspecs_add_addrspace (source_location location,
9906 struct c_declspecs *specs, addr_space_t as)
9908 specs->non_sc_seen_p = true;
9909 specs->declspecs_seen_p = true;
9911 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9912 && specs->address_space != as)
9913 error ("incompatible address space qualifiers %qs and %qs",
9914 c_addr_space_name (as),
9915 c_addr_space_name (specs->address_space));
9916 else
9918 specs->address_space = as;
9919 specs->locations[cdw_address_space] = location;
9921 return specs;
9924 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9925 returning SPECS. */
9927 struct c_declspecs *
9928 declspecs_add_qual (source_location loc,
9929 struct c_declspecs *specs, tree qual)
9931 enum rid i;
9932 bool dupe = false;
9933 specs->non_sc_seen_p = true;
9934 specs->declspecs_seen_p = true;
9935 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9936 && C_IS_RESERVED_WORD (qual));
9937 i = C_RID_CODE (qual);
9938 location_t prev_loc = UNKNOWN_LOCATION;
9939 switch (i)
9941 case RID_CONST:
9942 dupe = specs->const_p;
9943 specs->const_p = true;
9944 prev_loc = specs->locations[cdw_const];
9945 specs->locations[cdw_const] = loc;
9946 break;
9947 case RID_VOLATILE:
9948 dupe = specs->volatile_p;
9949 specs->volatile_p = true;
9950 prev_loc = specs->locations[cdw_volatile];
9951 specs->locations[cdw_volatile] = loc;
9952 break;
9953 case RID_RESTRICT:
9954 dupe = specs->restrict_p;
9955 specs->restrict_p = true;
9956 prev_loc = specs->locations[cdw_restrict];
9957 specs->locations[cdw_restrict] = loc;
9958 break;
9959 case RID_ATOMIC:
9960 dupe = specs->atomic_p;
9961 specs->atomic_p = true;
9962 prev_loc = specs->locations[cdw_atomic];
9963 specs->locations[cdw_atomic] = loc;
9964 break;
9965 default:
9966 gcc_unreachable ();
9968 if (dupe)
9970 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9971 "duplicate %qE declaration specifier", qual);
9972 if (!warned
9973 && warn_duplicate_decl_specifier
9974 && prev_loc >= RESERVED_LOCATION_COUNT
9975 && !from_macro_expansion_at (prev_loc)
9976 && !from_macro_expansion_at (loc))
9977 warning_at (loc, OPT_Wduplicate_decl_specifier,
9978 "duplicate %qE declaration specifier", qual);
9980 return specs;
9983 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9984 returning SPECS. */
9986 struct c_declspecs *
9987 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9988 struct c_typespec spec)
9990 tree type = spec.spec;
9991 specs->non_sc_seen_p = true;
9992 specs->declspecs_seen_p = true;
9993 specs->typespec_kind = spec.kind;
9994 if (TREE_DEPRECATED (type))
9995 specs->deprecated_p = true;
9997 /* Handle type specifier keywords. */
9998 if (TREE_CODE (type) == IDENTIFIER_NODE
9999 && C_IS_RESERVED_WORD (type)
10000 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10002 enum rid i = C_RID_CODE (type);
10003 if (specs->type)
10005 error_at (loc, "two or more data types in declaration specifiers");
10006 return specs;
10008 if ((int) i <= (int) RID_LAST_MODIFIER)
10010 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10011 bool dupe = false;
10012 switch (i)
10014 case RID_LONG:
10015 if (specs->long_long_p)
10017 error_at (loc, "%<long long long%> is too long for GCC");
10018 break;
10020 if (specs->long_p)
10022 if (specs->typespec_word == cts_double)
10024 error_at (loc,
10025 ("both %<long long%> and %<double%> in "
10026 "declaration specifiers"));
10027 break;
10029 pedwarn_c90 (loc, OPT_Wlong_long,
10030 "ISO C90 does not support %<long long%>");
10031 specs->long_long_p = 1;
10032 specs->locations[cdw_long_long] = loc;
10033 break;
10035 if (specs->short_p)
10036 error_at (loc,
10037 ("both %<long%> and %<short%> in "
10038 "declaration specifiers"));
10039 else if (specs->typespec_word == cts_auto_type)
10040 error_at (loc,
10041 ("both %<long%> and %<__auto_type%> in "
10042 "declaration specifiers"));
10043 else if (specs->typespec_word == cts_void)
10044 error_at (loc,
10045 ("both %<long%> and %<void%> in "
10046 "declaration specifiers"));
10047 else if (specs->typespec_word == cts_int_n)
10048 error_at (loc,
10049 ("both %<long%> and %<__int%d%> in "
10050 "declaration specifiers"),
10051 int_n_data[specs->int_n_idx].bitsize);
10052 else if (specs->typespec_word == cts_bool)
10053 error_at (loc,
10054 ("both %<long%> and %<_Bool%> in "
10055 "declaration specifiers"));
10056 else if (specs->typespec_word == cts_char)
10057 error_at (loc,
10058 ("both %<long%> and %<char%> in "
10059 "declaration specifiers"));
10060 else if (specs->typespec_word == cts_float)
10061 error_at (loc,
10062 ("both %<long%> and %<float%> in "
10063 "declaration specifiers"));
10064 else if (specs->typespec_word == cts_floatn_nx)
10065 error_at (loc,
10066 ("both %<long%> and %<_Float%d%s%> in "
10067 "declaration specifiers"),
10068 floatn_nx_types[specs->floatn_nx_idx].n,
10069 (floatn_nx_types[specs->floatn_nx_idx].extended
10070 ? "x"
10071 : ""));
10072 else if (specs->typespec_word == cts_dfloat32)
10073 error_at (loc,
10074 ("both %<long%> and %<_Decimal32%> in "
10075 "declaration specifiers"));
10076 else if (specs->typespec_word == cts_dfloat64)
10077 error_at (loc,
10078 ("both %<long%> and %<_Decimal64%> in "
10079 "declaration specifiers"));
10080 else if (specs->typespec_word == cts_dfloat128)
10081 error_at (loc,
10082 ("both %<long%> and %<_Decimal128%> in "
10083 "declaration specifiers"));
10084 else
10086 specs->long_p = true;
10087 specs->locations[cdw_long] = loc;
10089 break;
10090 case RID_SHORT:
10091 dupe = specs->short_p;
10092 if (specs->long_p)
10093 error_at (loc,
10094 ("both %<long%> and %<short%> in "
10095 "declaration specifiers"));
10096 else if (specs->typespec_word == cts_auto_type)
10097 error_at (loc,
10098 ("both %<short%> and %<__auto_type%> in "
10099 "declaration specifiers"));
10100 else if (specs->typespec_word == cts_void)
10101 error_at (loc,
10102 ("both %<short%> and %<void%> in "
10103 "declaration specifiers"));
10104 else if (specs->typespec_word == cts_int_n)
10105 error_at (loc,
10106 ("both %<short%> and %<__int%d%> in "
10107 "declaration specifiers"),
10108 int_n_data[specs->int_n_idx].bitsize);
10109 else if (specs->typespec_word == cts_bool)
10110 error_at (loc,
10111 ("both %<short%> and %<_Bool%> in "
10112 "declaration specifiers"));
10113 else if (specs->typespec_word == cts_char)
10114 error_at (loc,
10115 ("both %<short%> and %<char%> in "
10116 "declaration specifiers"));
10117 else if (specs->typespec_word == cts_float)
10118 error_at (loc,
10119 ("both %<short%> and %<float%> in "
10120 "declaration specifiers"));
10121 else if (specs->typespec_word == cts_double)
10122 error_at (loc,
10123 ("both %<short%> and %<double%> in "
10124 "declaration specifiers"));
10125 else if (specs->typespec_word == cts_floatn_nx)
10126 error_at (loc,
10127 ("both %<short%> and %<_Float%d%s%> in "
10128 "declaration specifiers"),
10129 floatn_nx_types[specs->floatn_nx_idx].n,
10130 (floatn_nx_types[specs->floatn_nx_idx].extended
10131 ? "x"
10132 : ""));
10133 else if (specs->typespec_word == cts_dfloat32)
10134 error_at (loc,
10135 ("both %<short%> and %<_Decimal32%> in "
10136 "declaration specifiers"));
10137 else if (specs->typespec_word == cts_dfloat64)
10138 error_at (loc,
10139 ("both %<short%> and %<_Decimal64%> in "
10140 "declaration specifiers"));
10141 else if (specs->typespec_word == cts_dfloat128)
10142 error_at (loc,
10143 ("both %<short%> and %<_Decimal128%> in "
10144 "declaration specifiers"));
10145 else
10147 specs->short_p = true;
10148 specs->locations[cdw_short] = loc;
10150 break;
10151 case RID_SIGNED:
10152 dupe = specs->signed_p;
10153 if (specs->unsigned_p)
10154 error_at (loc,
10155 ("both %<signed%> and %<unsigned%> in "
10156 "declaration specifiers"));
10157 else if (specs->typespec_word == cts_auto_type)
10158 error_at (loc,
10159 ("both %<signed%> and %<__auto_type%> in "
10160 "declaration specifiers"));
10161 else if (specs->typespec_word == cts_void)
10162 error_at (loc,
10163 ("both %<signed%> and %<void%> in "
10164 "declaration specifiers"));
10165 else if (specs->typespec_word == cts_bool)
10166 error_at (loc,
10167 ("both %<signed%> and %<_Bool%> in "
10168 "declaration specifiers"));
10169 else if (specs->typespec_word == cts_float)
10170 error_at (loc,
10171 ("both %<signed%> and %<float%> in "
10172 "declaration specifiers"));
10173 else if (specs->typespec_word == cts_double)
10174 error_at (loc,
10175 ("both %<signed%> and %<double%> in "
10176 "declaration specifiers"));
10177 else if (specs->typespec_word == cts_floatn_nx)
10178 error_at (loc,
10179 ("both %<signed%> and %<_Float%d%s%> in "
10180 "declaration specifiers"),
10181 floatn_nx_types[specs->floatn_nx_idx].n,
10182 (floatn_nx_types[specs->floatn_nx_idx].extended
10183 ? "x"
10184 : ""));
10185 else if (specs->typespec_word == cts_dfloat32)
10186 error_at (loc,
10187 ("both %<signed%> and %<_Decimal32%> in "
10188 "declaration specifiers"));
10189 else if (specs->typespec_word == cts_dfloat64)
10190 error_at (loc,
10191 ("both %<signed%> and %<_Decimal64%> in "
10192 "declaration specifiers"));
10193 else if (specs->typespec_word == cts_dfloat128)
10194 error_at (loc,
10195 ("both %<signed%> and %<_Decimal128%> in "
10196 "declaration specifiers"));
10197 else
10199 specs->signed_p = true;
10200 specs->locations[cdw_signed] = loc;
10202 break;
10203 case RID_UNSIGNED:
10204 dupe = specs->unsigned_p;
10205 if (specs->signed_p)
10206 error_at (loc,
10207 ("both %<signed%> and %<unsigned%> in "
10208 "declaration specifiers"));
10209 else if (specs->typespec_word == cts_auto_type)
10210 error_at (loc,
10211 ("both %<unsigned%> and %<__auto_type%> in "
10212 "declaration specifiers"));
10213 else if (specs->typespec_word == cts_void)
10214 error_at (loc,
10215 ("both %<unsigned%> and %<void%> in "
10216 "declaration specifiers"));
10217 else if (specs->typespec_word == cts_bool)
10218 error_at (loc,
10219 ("both %<unsigned%> and %<_Bool%> in "
10220 "declaration specifiers"));
10221 else if (specs->typespec_word == cts_float)
10222 error_at (loc,
10223 ("both %<unsigned%> and %<float%> in "
10224 "declaration specifiers"));
10225 else if (specs->typespec_word == cts_double)
10226 error_at (loc,
10227 ("both %<unsigned%> and %<double%> in "
10228 "declaration specifiers"));
10229 else if (specs->typespec_word == cts_floatn_nx)
10230 error_at (loc,
10231 ("both %<unsigned%> and %<_Float%d%s%> in "
10232 "declaration specifiers"),
10233 floatn_nx_types[specs->floatn_nx_idx].n,
10234 (floatn_nx_types[specs->floatn_nx_idx].extended
10235 ? "x"
10236 : ""));
10237 else if (specs->typespec_word == cts_dfloat32)
10238 error_at (loc,
10239 ("both %<unsigned%> and %<_Decimal32%> in "
10240 "declaration specifiers"));
10241 else if (specs->typespec_word == cts_dfloat64)
10242 error_at (loc,
10243 ("both %<unsigned%> and %<_Decimal64%> in "
10244 "declaration specifiers"));
10245 else if (specs->typespec_word == cts_dfloat128)
10246 error_at (loc,
10247 ("both %<unsigned%> and %<_Decimal128%> in "
10248 "declaration specifiers"));
10249 else
10251 specs->unsigned_p = true;
10252 specs->locations[cdw_unsigned] = loc;
10254 break;
10255 case RID_COMPLEX:
10256 dupe = specs->complex_p;
10257 if (!in_system_header_at (loc))
10258 pedwarn_c90 (loc, OPT_Wpedantic,
10259 "ISO C90 does not support complex types");
10260 if (specs->typespec_word == cts_auto_type)
10261 error_at (loc,
10262 ("both %<complex%> and %<__auto_type%> in "
10263 "declaration specifiers"));
10264 else if (specs->typespec_word == cts_void)
10265 error_at (loc,
10266 ("both %<complex%> and %<void%> in "
10267 "declaration specifiers"));
10268 else if (specs->typespec_word == cts_bool)
10269 error_at (loc,
10270 ("both %<complex%> and %<_Bool%> in "
10271 "declaration specifiers"));
10272 else if (specs->typespec_word == cts_dfloat32)
10273 error_at (loc,
10274 ("both %<complex%> and %<_Decimal32%> in "
10275 "declaration specifiers"));
10276 else if (specs->typespec_word == cts_dfloat64)
10277 error_at (loc,
10278 ("both %<complex%> and %<_Decimal64%> in "
10279 "declaration specifiers"));
10280 else if (specs->typespec_word == cts_dfloat128)
10281 error_at (loc,
10282 ("both %<complex%> and %<_Decimal128%> in "
10283 "declaration specifiers"));
10284 else if (specs->typespec_word == cts_fract)
10285 error_at (loc,
10286 ("both %<complex%> and %<_Fract%> in "
10287 "declaration specifiers"));
10288 else if (specs->typespec_word == cts_accum)
10289 error_at (loc,
10290 ("both %<complex%> and %<_Accum%> in "
10291 "declaration specifiers"));
10292 else if (specs->saturating_p)
10293 error_at (loc,
10294 ("both %<complex%> and %<_Sat%> in "
10295 "declaration specifiers"));
10296 else
10298 specs->complex_p = true;
10299 specs->locations[cdw_complex] = loc;
10301 break;
10302 case RID_SAT:
10303 dupe = specs->saturating_p;
10304 pedwarn (loc, OPT_Wpedantic,
10305 "ISO C does not support saturating types");
10306 if (specs->typespec_word == cts_int_n)
10308 error_at (loc,
10309 ("both %<_Sat%> and %<__int%d%> in "
10310 "declaration specifiers"),
10311 int_n_data[specs->int_n_idx].bitsize);
10313 else if (specs->typespec_word == cts_auto_type)
10314 error_at (loc,
10315 ("both %<_Sat%> and %<__auto_type%> in "
10316 "declaration specifiers"));
10317 else if (specs->typespec_word == cts_void)
10318 error_at (loc,
10319 ("both %<_Sat%> and %<void%> in "
10320 "declaration specifiers"));
10321 else if (specs->typespec_word == cts_bool)
10322 error_at (loc,
10323 ("both %<_Sat%> and %<_Bool%> in "
10324 "declaration specifiers"));
10325 else if (specs->typespec_word == cts_char)
10326 error_at (loc,
10327 ("both %<_Sat%> and %<char%> in "
10328 "declaration specifiers"));
10329 else if (specs->typespec_word == cts_int)
10330 error_at (loc,
10331 ("both %<_Sat%> and %<int%> in "
10332 "declaration specifiers"));
10333 else if (specs->typespec_word == cts_float)
10334 error_at (loc,
10335 ("both %<_Sat%> and %<float%> in "
10336 "declaration specifiers"));
10337 else if (specs->typespec_word == cts_double)
10338 error_at (loc,
10339 ("both %<_Sat%> and %<double%> in "
10340 "declaration specifiers"));
10341 else if (specs->typespec_word == cts_floatn_nx)
10342 error_at (loc,
10343 ("both %<_Sat%> and %<_Float%d%s%> in "
10344 "declaration specifiers"),
10345 floatn_nx_types[specs->floatn_nx_idx].n,
10346 (floatn_nx_types[specs->floatn_nx_idx].extended
10347 ? "x"
10348 : ""));
10349 else if (specs->typespec_word == cts_dfloat32)
10350 error_at (loc,
10351 ("both %<_Sat%> and %<_Decimal32%> in "
10352 "declaration specifiers"));
10353 else if (specs->typespec_word == cts_dfloat64)
10354 error_at (loc,
10355 ("both %<_Sat%> and %<_Decimal64%> in "
10356 "declaration specifiers"));
10357 else if (specs->typespec_word == cts_dfloat128)
10358 error_at (loc,
10359 ("both %<_Sat%> and %<_Decimal128%> in "
10360 "declaration specifiers"));
10361 else if (specs->complex_p)
10362 error_at (loc,
10363 ("both %<_Sat%> and %<complex%> in "
10364 "declaration specifiers"));
10365 else
10367 specs->saturating_p = true;
10368 specs->locations[cdw_saturating] = loc;
10370 break;
10371 default:
10372 gcc_unreachable ();
10375 if (dupe)
10376 error_at (loc, "duplicate %qE", type);
10378 return specs;
10380 else
10382 /* "void", "_Bool", "char", "int", "float", "double",
10383 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10384 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10385 "__auto_type". */
10386 if (specs->typespec_word != cts_none)
10388 error_at (loc,
10389 "two or more data types in declaration specifiers");
10390 return specs;
10392 switch (i)
10394 case RID_AUTO_TYPE:
10395 if (specs->long_p)
10396 error_at (loc,
10397 ("both %<long%> and %<__auto_type%> in "
10398 "declaration specifiers"));
10399 else if (specs->short_p)
10400 error_at (loc,
10401 ("both %<short%> and %<__auto_type%> in "
10402 "declaration specifiers"));
10403 else if (specs->signed_p)
10404 error_at (loc,
10405 ("both %<signed%> and %<__auto_type%> in "
10406 "declaration specifiers"));
10407 else if (specs->unsigned_p)
10408 error_at (loc,
10409 ("both %<unsigned%> and %<__auto_type%> in "
10410 "declaration specifiers"));
10411 else if (specs->complex_p)
10412 error_at (loc,
10413 ("both %<complex%> and %<__auto_type%> in "
10414 "declaration specifiers"));
10415 else if (specs->saturating_p)
10416 error_at (loc,
10417 ("both %<_Sat%> and %<__auto_type%> in "
10418 "declaration specifiers"));
10419 else
10421 specs->typespec_word = cts_auto_type;
10422 specs->locations[cdw_typespec] = loc;
10424 return specs;
10425 case RID_INT_N_0:
10426 case RID_INT_N_1:
10427 case RID_INT_N_2:
10428 case RID_INT_N_3:
10429 specs->int_n_idx = i - RID_INT_N_0;
10430 if (!in_system_header_at (input_location))
10431 pedwarn (loc, OPT_Wpedantic,
10432 "ISO C does not support %<__int%d%> types",
10433 int_n_data[specs->int_n_idx].bitsize);
10435 if (specs->long_p)
10436 error_at (loc,
10437 ("both %<__int%d%> and %<long%> in "
10438 "declaration specifiers"),
10439 int_n_data[specs->int_n_idx].bitsize);
10440 else if (specs->saturating_p)
10441 error_at (loc,
10442 ("both %<_Sat%> and %<__int%d%> in "
10443 "declaration specifiers"),
10444 int_n_data[specs->int_n_idx].bitsize);
10445 else if (specs->short_p)
10446 error_at (loc,
10447 ("both %<__int%d%> and %<short%> in "
10448 "declaration specifiers"),
10449 int_n_data[specs->int_n_idx].bitsize);
10450 else if (! int_n_enabled_p[specs->int_n_idx])
10452 specs->typespec_word = cts_int_n;
10453 error_at (loc,
10454 "%<__int%d%> is not supported on this target",
10455 int_n_data[specs->int_n_idx].bitsize);
10457 else
10459 specs->typespec_word = cts_int_n;
10460 specs->locations[cdw_typespec] = loc;
10462 return specs;
10463 case RID_VOID:
10464 if (specs->long_p)
10465 error_at (loc,
10466 ("both %<long%> and %<void%> in "
10467 "declaration specifiers"));
10468 else if (specs->short_p)
10469 error_at (loc,
10470 ("both %<short%> and %<void%> in "
10471 "declaration specifiers"));
10472 else if (specs->signed_p)
10473 error_at (loc,
10474 ("both %<signed%> and %<void%> in "
10475 "declaration specifiers"));
10476 else if (specs->unsigned_p)
10477 error_at (loc,
10478 ("both %<unsigned%> and %<void%> in "
10479 "declaration specifiers"));
10480 else if (specs->complex_p)
10481 error_at (loc,
10482 ("both %<complex%> and %<void%> in "
10483 "declaration specifiers"));
10484 else if (specs->saturating_p)
10485 error_at (loc,
10486 ("both %<_Sat%> and %<void%> in "
10487 "declaration specifiers"));
10488 else
10490 specs->typespec_word = cts_void;
10491 specs->locations[cdw_typespec] = loc;
10493 return specs;
10494 case RID_BOOL:
10495 if (!in_system_header_at (loc))
10496 pedwarn_c90 (loc, OPT_Wpedantic,
10497 "ISO C90 does not support boolean types");
10498 if (specs->long_p)
10499 error_at (loc,
10500 ("both %<long%> and %<_Bool%> in "
10501 "declaration specifiers"));
10502 else if (specs->short_p)
10503 error_at (loc,
10504 ("both %<short%> and %<_Bool%> in "
10505 "declaration specifiers"));
10506 else if (specs->signed_p)
10507 error_at (loc,
10508 ("both %<signed%> and %<_Bool%> in "
10509 "declaration specifiers"));
10510 else if (specs->unsigned_p)
10511 error_at (loc,
10512 ("both %<unsigned%> and %<_Bool%> in "
10513 "declaration specifiers"));
10514 else if (specs->complex_p)
10515 error_at (loc,
10516 ("both %<complex%> and %<_Bool%> in "
10517 "declaration specifiers"));
10518 else if (specs->saturating_p)
10519 error_at (loc,
10520 ("both %<_Sat%> and %<_Bool%> in "
10521 "declaration specifiers"));
10522 else
10524 specs->typespec_word = cts_bool;
10525 specs->locations[cdw_typespec] = loc;
10527 return specs;
10528 case RID_CHAR:
10529 if (specs->long_p)
10530 error_at (loc,
10531 ("both %<long%> and %<char%> in "
10532 "declaration specifiers"));
10533 else if (specs->short_p)
10534 error_at (loc,
10535 ("both %<short%> and %<char%> in "
10536 "declaration specifiers"));
10537 else if (specs->saturating_p)
10538 error_at (loc,
10539 ("both %<_Sat%> and %<char%> in "
10540 "declaration specifiers"));
10541 else
10543 specs->typespec_word = cts_char;
10544 specs->locations[cdw_typespec] = loc;
10546 return specs;
10547 case RID_INT:
10548 if (specs->saturating_p)
10549 error_at (loc,
10550 ("both %<_Sat%> and %<int%> in "
10551 "declaration specifiers"));
10552 else
10554 specs->typespec_word = cts_int;
10555 specs->locations[cdw_typespec] = loc;
10557 return specs;
10558 case RID_FLOAT:
10559 if (specs->long_p)
10560 error_at (loc,
10561 ("both %<long%> and %<float%> in "
10562 "declaration specifiers"));
10563 else if (specs->short_p)
10564 error_at (loc,
10565 ("both %<short%> and %<float%> in "
10566 "declaration specifiers"));
10567 else if (specs->signed_p)
10568 error_at (loc,
10569 ("both %<signed%> and %<float%> in "
10570 "declaration specifiers"));
10571 else if (specs->unsigned_p)
10572 error_at (loc,
10573 ("both %<unsigned%> and %<float%> in "
10574 "declaration specifiers"));
10575 else if (specs->saturating_p)
10576 error_at (loc,
10577 ("both %<_Sat%> and %<float%> in "
10578 "declaration specifiers"));
10579 else
10581 specs->typespec_word = cts_float;
10582 specs->locations[cdw_typespec] = loc;
10584 return specs;
10585 case RID_DOUBLE:
10586 if (specs->long_long_p)
10587 error_at (loc,
10588 ("both %<long long%> and %<double%> in "
10589 "declaration specifiers"));
10590 else if (specs->short_p)
10591 error_at (loc,
10592 ("both %<short%> and %<double%> in "
10593 "declaration specifiers"));
10594 else if (specs->signed_p)
10595 error_at (loc,
10596 ("both %<signed%> and %<double%> in "
10597 "declaration specifiers"));
10598 else if (specs->unsigned_p)
10599 error_at (loc,
10600 ("both %<unsigned%> and %<double%> in "
10601 "declaration specifiers"));
10602 else if (specs->saturating_p)
10603 error_at (loc,
10604 ("both %<_Sat%> and %<double%> in "
10605 "declaration specifiers"));
10606 else
10608 specs->typespec_word = cts_double;
10609 specs->locations[cdw_typespec] = loc;
10611 return specs;
10612 CASE_RID_FLOATN_NX:
10613 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10614 if (!in_system_header_at (input_location))
10615 pedwarn (loc, OPT_Wpedantic,
10616 "ISO C does not support the %<_Float%d%s%> type",
10617 floatn_nx_types[specs->floatn_nx_idx].n,
10618 (floatn_nx_types[specs->floatn_nx_idx].extended
10619 ? "x"
10620 : ""));
10622 if (specs->long_p)
10623 error_at (loc,
10624 ("both %<long%> and %<_Float%d%s%> in "
10625 "declaration specifiers"),
10626 floatn_nx_types[specs->floatn_nx_idx].n,
10627 (floatn_nx_types[specs->floatn_nx_idx].extended
10628 ? "x"
10629 : ""));
10630 else if (specs->short_p)
10631 error_at (loc,
10632 ("both %<short%> and %<_Float%d%s%> in "
10633 "declaration specifiers"),
10634 floatn_nx_types[specs->floatn_nx_idx].n,
10635 (floatn_nx_types[specs->floatn_nx_idx].extended
10636 ? "x"
10637 : ""));
10638 else if (specs->signed_p)
10639 error_at (loc,
10640 ("both %<signed%> and %<_Float%d%s%> in "
10641 "declaration specifiers"),
10642 floatn_nx_types[specs->floatn_nx_idx].n,
10643 (floatn_nx_types[specs->floatn_nx_idx].extended
10644 ? "x"
10645 : ""));
10646 else if (specs->unsigned_p)
10647 error_at (loc,
10648 ("both %<unsigned%> and %<_Float%d%s%> in "
10649 "declaration specifiers"),
10650 floatn_nx_types[specs->floatn_nx_idx].n,
10651 (floatn_nx_types[specs->floatn_nx_idx].extended
10652 ? "x"
10653 : ""));
10654 else if (specs->saturating_p)
10655 error_at (loc,
10656 ("both %<_Sat%> and %<_Float%d%s%> in "
10657 "declaration specifiers"),
10658 floatn_nx_types[specs->floatn_nx_idx].n,
10659 (floatn_nx_types[specs->floatn_nx_idx].extended
10660 ? "x"
10661 : ""));
10662 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10664 specs->typespec_word = cts_floatn_nx;
10665 error_at (loc,
10666 "%<_Float%d%s%> is not supported on this target",
10667 floatn_nx_types[specs->floatn_nx_idx].n,
10668 (floatn_nx_types[specs->floatn_nx_idx].extended
10669 ? "x"
10670 : ""));
10672 else
10674 specs->typespec_word = cts_floatn_nx;
10675 specs->locations[cdw_typespec] = loc;
10677 return specs;
10678 case RID_DFLOAT32:
10679 case RID_DFLOAT64:
10680 case RID_DFLOAT128:
10682 const char *str;
10683 if (i == RID_DFLOAT32)
10684 str = "_Decimal32";
10685 else if (i == RID_DFLOAT64)
10686 str = "_Decimal64";
10687 else
10688 str = "_Decimal128";
10689 if (specs->long_long_p)
10690 error_at (loc,
10691 ("both %<long long%> and %qs in "
10692 "declaration specifiers"),
10693 str);
10694 if (specs->long_p)
10695 error_at (loc,
10696 ("both %<long%> and %qs in "
10697 "declaration specifiers"),
10698 str);
10699 else if (specs->short_p)
10700 error_at (loc,
10701 ("both %<short%> and %qs in "
10702 "declaration specifiers"),
10703 str);
10704 else if (specs->signed_p)
10705 error_at (loc,
10706 ("both %<signed%> and %qs in "
10707 "declaration specifiers"),
10708 str);
10709 else if (specs->unsigned_p)
10710 error_at (loc,
10711 ("both %<unsigned%> and %qs in "
10712 "declaration specifiers"),
10713 str);
10714 else if (specs->complex_p)
10715 error_at (loc,
10716 ("both %<complex%> and %qs in "
10717 "declaration specifiers"),
10718 str);
10719 else if (specs->saturating_p)
10720 error_at (loc,
10721 ("both %<_Sat%> and %qs in "
10722 "declaration specifiers"),
10723 str);
10724 else if (i == RID_DFLOAT32)
10725 specs->typespec_word = cts_dfloat32;
10726 else if (i == RID_DFLOAT64)
10727 specs->typespec_word = cts_dfloat64;
10728 else
10729 specs->typespec_word = cts_dfloat128;
10730 specs->locations[cdw_typespec] = loc;
10732 if (!targetm.decimal_float_supported_p ())
10733 error_at (loc,
10734 ("decimal floating point not supported "
10735 "for this target"));
10736 pedwarn (loc, OPT_Wpedantic,
10737 "ISO C does not support decimal floating point");
10738 return specs;
10739 case RID_FRACT:
10740 case RID_ACCUM:
10742 const char *str;
10743 if (i == RID_FRACT)
10744 str = "_Fract";
10745 else
10746 str = "_Accum";
10747 if (specs->complex_p)
10748 error_at (loc,
10749 ("both %<complex%> and %qs in "
10750 "declaration specifiers"),
10751 str);
10752 else if (i == RID_FRACT)
10753 specs->typespec_word = cts_fract;
10754 else
10755 specs->typespec_word = cts_accum;
10756 specs->locations[cdw_typespec] = loc;
10758 if (!targetm.fixed_point_supported_p ())
10759 error_at (loc,
10760 "fixed-point types not supported for this target");
10761 pedwarn (loc, OPT_Wpedantic,
10762 "ISO C does not support fixed-point types");
10763 return specs;
10764 default:
10765 /* ObjC reserved word "id", handled below. */
10766 break;
10771 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10772 form of ObjC type, cases such as "int" and "long" being handled
10773 above), a TYPE (struct, union, enum and typeof specifiers) or an
10774 ERROR_MARK. In none of these cases may there have previously
10775 been any type specifiers. */
10776 if (specs->type || specs->typespec_word != cts_none
10777 || specs->long_p || specs->short_p || specs->signed_p
10778 || specs->unsigned_p || specs->complex_p)
10779 error_at (loc, "two or more data types in declaration specifiers");
10780 else if (TREE_CODE (type) == TYPE_DECL)
10782 if (TREE_TYPE (type) == error_mark_node)
10783 ; /* Allow the type to default to int to avoid cascading errors. */
10784 else
10786 specs->type = TREE_TYPE (type);
10787 specs->decl_attr = DECL_ATTRIBUTES (type);
10788 specs->typedef_p = true;
10789 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10790 specs->locations[cdw_typedef] = loc;
10792 /* If this typedef name is defined in a struct, then a C++
10793 lookup would return a different value. */
10794 if (warn_cxx_compat
10795 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10796 warning_at (loc, OPT_Wc___compat,
10797 "C++ lookup of %qD would return a field, not a type",
10798 type);
10800 /* If we are parsing a struct, record that a struct field
10801 used a typedef. */
10802 if (warn_cxx_compat && struct_parse_info != NULL)
10803 struct_parse_info->typedefs_seen.safe_push (type);
10806 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10808 tree t = lookup_name (type);
10809 if (!t || TREE_CODE (t) != TYPE_DECL)
10810 error_at (loc, "%qE fails to be a typedef or built in type", type);
10811 else if (TREE_TYPE (t) == error_mark_node)
10813 else
10815 specs->type = TREE_TYPE (t);
10816 specs->locations[cdw_typespec] = loc;
10819 else
10821 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10823 specs->typedef_p = true;
10824 specs->locations[cdw_typedef] = loc;
10825 if (spec.expr)
10827 if (specs->expr)
10828 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10829 specs->expr, spec.expr);
10830 else
10831 specs->expr = spec.expr;
10832 specs->expr_const_operands &= spec.expr_const_operands;
10835 specs->type = type;
10838 return specs;
10841 /* Add the storage class specifier or function specifier SCSPEC to the
10842 declaration specifiers SPECS, returning SPECS. */
10844 struct c_declspecs *
10845 declspecs_add_scspec (source_location loc,
10846 struct c_declspecs *specs,
10847 tree scspec)
10849 enum rid i;
10850 enum c_storage_class n = csc_none;
10851 bool dupe = false;
10852 specs->declspecs_seen_p = true;
10853 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10854 && C_IS_RESERVED_WORD (scspec));
10855 i = C_RID_CODE (scspec);
10856 if (specs->non_sc_seen_p)
10857 warning (OPT_Wold_style_declaration,
10858 "%qE is not at beginning of declaration", scspec);
10859 switch (i)
10861 case RID_INLINE:
10862 /* C99 permits duplicate inline. Although of doubtful utility,
10863 it seems simplest to permit it in gnu89 mode as well, as
10864 there is also little utility in maintaining this as a
10865 difference between gnu89 and C99 inline. */
10866 dupe = false;
10867 specs->inline_p = true;
10868 specs->locations[cdw_inline] = loc;
10869 break;
10870 case RID_NORETURN:
10871 /* Duplicate _Noreturn is permitted. */
10872 dupe = false;
10873 specs->noreturn_p = true;
10874 specs->locations[cdw_noreturn] = loc;
10875 break;
10876 case RID_THREAD:
10877 dupe = specs->thread_p;
10878 if (specs->storage_class == csc_auto)
10879 error ("%qE used with %<auto%>", scspec);
10880 else if (specs->storage_class == csc_register)
10881 error ("%qE used with %<register%>", scspec);
10882 else if (specs->storage_class == csc_typedef)
10883 error ("%qE used with %<typedef%>", scspec);
10884 else
10886 specs->thread_p = true;
10887 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10888 "__thread") == 0);
10889 /* A diagnostic is not required for the use of this
10890 identifier in the implementation namespace; only diagnose
10891 it for the C11 spelling because of existing code using
10892 the other spelling. */
10893 if (!specs->thread_gnu_p)
10895 if (flag_isoc99)
10896 pedwarn_c99 (loc, OPT_Wpedantic,
10897 "ISO C99 does not support %qE", scspec);
10898 else
10899 pedwarn_c99 (loc, OPT_Wpedantic,
10900 "ISO C90 does not support %qE", scspec);
10902 specs->locations[cdw_thread] = loc;
10904 break;
10905 case RID_AUTO:
10906 n = csc_auto;
10907 break;
10908 case RID_EXTERN:
10909 n = csc_extern;
10910 /* Diagnose "__thread extern". */
10911 if (specs->thread_p && specs->thread_gnu_p)
10912 error ("%<__thread%> before %<extern%>");
10913 break;
10914 case RID_REGISTER:
10915 n = csc_register;
10916 break;
10917 case RID_STATIC:
10918 n = csc_static;
10919 /* Diagnose "__thread static". */
10920 if (specs->thread_p && specs->thread_gnu_p)
10921 error ("%<__thread%> before %<static%>");
10922 break;
10923 case RID_TYPEDEF:
10924 n = csc_typedef;
10925 break;
10926 default:
10927 gcc_unreachable ();
10929 if (n != csc_none && n == specs->storage_class)
10930 dupe = true;
10931 if (dupe)
10933 if (i == RID_THREAD)
10934 error ("duplicate %<_Thread_local%> or %<__thread%>");
10935 else
10936 error ("duplicate %qE", scspec);
10938 if (n != csc_none)
10940 if (specs->storage_class != csc_none && n != specs->storage_class)
10942 error ("multiple storage classes in declaration specifiers");
10944 else
10946 specs->storage_class = n;
10947 specs->locations[cdw_storage_class] = loc;
10948 if (n != csc_extern && n != csc_static && specs->thread_p)
10950 error ("%qs used with %qE",
10951 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10952 scspec);
10953 specs->thread_p = false;
10957 return specs;
10960 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10961 returning SPECS. */
10963 struct c_declspecs *
10964 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10966 specs->attrs = chainon (attrs, specs->attrs);
10967 specs->locations[cdw_attributes] = loc;
10968 specs->declspecs_seen_p = true;
10969 return specs;
10972 /* Add an _Alignas specifier (expression ALIGN, or type whose
10973 alignment is ALIGN) to the declaration specifiers SPECS, returning
10974 SPECS. */
10975 struct c_declspecs *
10976 declspecs_add_alignas (source_location loc,
10977 struct c_declspecs *specs, tree align)
10979 int align_log;
10980 specs->alignas_p = true;
10981 specs->locations[cdw_alignas] = loc;
10982 if (align == error_mark_node)
10983 return specs;
10984 align_log = check_user_alignment (align, true);
10985 if (align_log > specs->align_log)
10986 specs->align_log = align_log;
10987 return specs;
10990 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10991 specifiers with any other type specifier to determine the resulting
10992 type. This is where ISO C checks on complex types are made, since
10993 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10994 double". */
10996 struct c_declspecs *
10997 finish_declspecs (struct c_declspecs *specs)
10999 /* If a type was specified as a whole, we have no modifiers and are
11000 done. */
11001 if (specs->type != NULL_TREE)
11003 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11004 && !specs->signed_p && !specs->unsigned_p
11005 && !specs->complex_p);
11007 /* Set a dummy type. */
11008 if (TREE_CODE (specs->type) == ERROR_MARK)
11009 specs->type = integer_type_node;
11010 return specs;
11013 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11014 has been specified, treat it as "int" unless "_Complex" is
11015 present and there are no other specifiers. If we just have
11016 "_Complex", it is equivalent to "_Complex double", but e.g.
11017 "_Complex short" is equivalent to "_Complex short int". */
11018 if (specs->typespec_word == cts_none)
11020 if (specs->saturating_p)
11022 error_at (specs->locations[cdw_saturating],
11023 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11024 if (!targetm.fixed_point_supported_p ())
11025 error_at (specs->locations[cdw_saturating],
11026 "fixed-point types not supported for this target");
11027 specs->typespec_word = cts_fract;
11029 else if (specs->long_p || specs->short_p
11030 || specs->signed_p || specs->unsigned_p)
11032 specs->typespec_word = cts_int;
11034 else if (specs->complex_p)
11036 specs->typespec_word = cts_double;
11037 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11038 "ISO C does not support plain %<complex%> meaning "
11039 "%<double complex%>");
11041 else
11043 specs->typespec_word = cts_int;
11044 specs->default_int_p = true;
11045 /* We don't diagnose this here because grokdeclarator will
11046 give more specific diagnostics according to whether it is
11047 a function definition. */
11051 /* If "signed" was specified, record this to distinguish "int" and
11052 "signed int" in the case of a bit-field with
11053 -funsigned-bitfields. */
11054 specs->explicit_signed_p = specs->signed_p;
11056 /* Now compute the actual type. */
11057 switch (specs->typespec_word)
11059 case cts_auto_type:
11060 gcc_assert (!specs->long_p && !specs->short_p
11061 && !specs->signed_p && !specs->unsigned_p
11062 && !specs->complex_p);
11063 /* Type to be filled in later. */
11064 break;
11065 case cts_void:
11066 gcc_assert (!specs->long_p && !specs->short_p
11067 && !specs->signed_p && !specs->unsigned_p
11068 && !specs->complex_p);
11069 specs->type = void_type_node;
11070 break;
11071 case cts_bool:
11072 gcc_assert (!specs->long_p && !specs->short_p
11073 && !specs->signed_p && !specs->unsigned_p
11074 && !specs->complex_p);
11075 specs->type = boolean_type_node;
11076 break;
11077 case cts_char:
11078 gcc_assert (!specs->long_p && !specs->short_p);
11079 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11080 if (specs->signed_p)
11081 specs->type = signed_char_type_node;
11082 else if (specs->unsigned_p)
11083 specs->type = unsigned_char_type_node;
11084 else
11085 specs->type = char_type_node;
11086 if (specs->complex_p)
11088 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11089 "ISO C does not support complex integer types");
11090 specs->type = build_complex_type (specs->type);
11092 break;
11093 case cts_int_n:
11094 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11095 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11096 if (! int_n_enabled_p[specs->int_n_idx])
11097 specs->type = integer_type_node;
11098 else
11099 specs->type = (specs->unsigned_p
11100 ? int_n_trees[specs->int_n_idx].unsigned_type
11101 : int_n_trees[specs->int_n_idx].signed_type);
11102 if (specs->complex_p)
11104 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11105 "ISO C does not support complex integer types");
11106 specs->type = build_complex_type (specs->type);
11108 break;
11109 case cts_int:
11110 gcc_assert (!(specs->long_p && specs->short_p));
11111 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11112 if (specs->long_long_p)
11113 specs->type = (specs->unsigned_p
11114 ? long_long_unsigned_type_node
11115 : long_long_integer_type_node);
11116 else if (specs->long_p)
11117 specs->type = (specs->unsigned_p
11118 ? long_unsigned_type_node
11119 : long_integer_type_node);
11120 else if (specs->short_p)
11121 specs->type = (specs->unsigned_p
11122 ? short_unsigned_type_node
11123 : short_integer_type_node);
11124 else
11125 specs->type = (specs->unsigned_p
11126 ? unsigned_type_node
11127 : integer_type_node);
11128 if (specs->complex_p)
11130 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11131 "ISO C does not support complex integer types");
11132 specs->type = build_complex_type (specs->type);
11134 break;
11135 case cts_float:
11136 gcc_assert (!specs->long_p && !specs->short_p
11137 && !specs->signed_p && !specs->unsigned_p);
11138 specs->type = (specs->complex_p
11139 ? complex_float_type_node
11140 : float_type_node);
11141 break;
11142 case cts_double:
11143 gcc_assert (!specs->long_long_p && !specs->short_p
11144 && !specs->signed_p && !specs->unsigned_p);
11145 if (specs->long_p)
11147 specs->type = (specs->complex_p
11148 ? complex_long_double_type_node
11149 : long_double_type_node);
11151 else
11153 specs->type = (specs->complex_p
11154 ? complex_double_type_node
11155 : double_type_node);
11157 break;
11158 case cts_floatn_nx:
11159 gcc_assert (!specs->long_p && !specs->short_p
11160 && !specs->signed_p && !specs->unsigned_p);
11161 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11162 specs->type = integer_type_node;
11163 else if (specs->complex_p)
11164 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11165 else
11166 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11167 break;
11168 case cts_dfloat32:
11169 case cts_dfloat64:
11170 case cts_dfloat128:
11171 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11172 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11173 if (specs->typespec_word == cts_dfloat32)
11174 specs->type = dfloat32_type_node;
11175 else if (specs->typespec_word == cts_dfloat64)
11176 specs->type = dfloat64_type_node;
11177 else
11178 specs->type = dfloat128_type_node;
11179 break;
11180 case cts_fract:
11181 gcc_assert (!specs->complex_p);
11182 if (!targetm.fixed_point_supported_p ())
11183 specs->type = integer_type_node;
11184 else if (specs->saturating_p)
11186 if (specs->long_long_p)
11187 specs->type = specs->unsigned_p
11188 ? sat_unsigned_long_long_fract_type_node
11189 : sat_long_long_fract_type_node;
11190 else if (specs->long_p)
11191 specs->type = specs->unsigned_p
11192 ? sat_unsigned_long_fract_type_node
11193 : sat_long_fract_type_node;
11194 else if (specs->short_p)
11195 specs->type = specs->unsigned_p
11196 ? sat_unsigned_short_fract_type_node
11197 : sat_short_fract_type_node;
11198 else
11199 specs->type = specs->unsigned_p
11200 ? sat_unsigned_fract_type_node
11201 : sat_fract_type_node;
11203 else
11205 if (specs->long_long_p)
11206 specs->type = specs->unsigned_p
11207 ? unsigned_long_long_fract_type_node
11208 : long_long_fract_type_node;
11209 else if (specs->long_p)
11210 specs->type = specs->unsigned_p
11211 ? unsigned_long_fract_type_node
11212 : long_fract_type_node;
11213 else if (specs->short_p)
11214 specs->type = specs->unsigned_p
11215 ? unsigned_short_fract_type_node
11216 : short_fract_type_node;
11217 else
11218 specs->type = specs->unsigned_p
11219 ? unsigned_fract_type_node
11220 : fract_type_node;
11222 break;
11223 case cts_accum:
11224 gcc_assert (!specs->complex_p);
11225 if (!targetm.fixed_point_supported_p ())
11226 specs->type = integer_type_node;
11227 else if (specs->saturating_p)
11229 if (specs->long_long_p)
11230 specs->type = specs->unsigned_p
11231 ? sat_unsigned_long_long_accum_type_node
11232 : sat_long_long_accum_type_node;
11233 else if (specs->long_p)
11234 specs->type = specs->unsigned_p
11235 ? sat_unsigned_long_accum_type_node
11236 : sat_long_accum_type_node;
11237 else if (specs->short_p)
11238 specs->type = specs->unsigned_p
11239 ? sat_unsigned_short_accum_type_node
11240 : sat_short_accum_type_node;
11241 else
11242 specs->type = specs->unsigned_p
11243 ? sat_unsigned_accum_type_node
11244 : sat_accum_type_node;
11246 else
11248 if (specs->long_long_p)
11249 specs->type = specs->unsigned_p
11250 ? unsigned_long_long_accum_type_node
11251 : long_long_accum_type_node;
11252 else if (specs->long_p)
11253 specs->type = specs->unsigned_p
11254 ? unsigned_long_accum_type_node
11255 : long_accum_type_node;
11256 else if (specs->short_p)
11257 specs->type = specs->unsigned_p
11258 ? unsigned_short_accum_type_node
11259 : short_accum_type_node;
11260 else
11261 specs->type = specs->unsigned_p
11262 ? unsigned_accum_type_node
11263 : accum_type_node;
11265 break;
11266 default:
11267 gcc_unreachable ();
11270 return specs;
11273 /* Perform final processing on one file scope's declarations (or the
11274 external scope's declarations), GLOBALS. */
11276 static void
11277 c_write_global_declarations_1 (tree globals)
11279 tree decl;
11280 bool reconsider;
11282 /* Process the decls in the order they were written. */
11283 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11285 /* Check for used but undefined static functions using the C
11286 standard's definition of "used", and set TREE_NO_WARNING so
11287 that check_global_declaration doesn't repeat the check. */
11288 if (TREE_CODE (decl) == FUNCTION_DECL
11289 && DECL_INITIAL (decl) == NULL_TREE
11290 && DECL_EXTERNAL (decl)
11291 && !TREE_PUBLIC (decl))
11293 if (C_DECL_USED (decl))
11295 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11296 TREE_NO_WARNING (decl) = 1;
11298 /* For -Wunused-function warn about unused static prototypes. */
11299 else if (warn_unused_function
11300 && ! DECL_ARTIFICIAL (decl)
11301 && ! TREE_NO_WARNING (decl))
11303 warning (OPT_Wunused_function,
11304 "%q+F declared %<static%> but never defined", decl);
11305 TREE_NO_WARNING (decl) = 1;
11309 wrapup_global_declaration_1 (decl);
11314 reconsider = false;
11315 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11316 reconsider |= wrapup_global_declaration_2 (decl);
11318 while (reconsider);
11321 /* Callback to collect a source_ref from a DECL. */
11323 static void
11324 collect_source_ref_cb (tree decl)
11326 if (!DECL_IS_BUILTIN (decl))
11327 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11330 /* Preserve the external declarations scope across a garbage collect. */
11331 static GTY(()) tree ext_block;
11333 /* Collect all references relevant to SOURCE_FILE. */
11335 static void
11336 collect_all_refs (const char *source_file)
11338 tree t;
11339 unsigned i;
11341 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11342 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11344 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11347 /* Iterate over all global declarations and call CALLBACK. */
11349 static void
11350 for_each_global_decl (void (*callback) (tree decl))
11352 tree t;
11353 tree decls;
11354 tree decl;
11355 unsigned i;
11357 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11359 decls = DECL_INITIAL (t);
11360 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11361 callback (decl);
11364 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11365 callback (decl);
11368 /* Perform any final parser cleanups and generate initial debugging
11369 information. */
11371 void
11372 c_parse_final_cleanups (void)
11374 tree t;
11375 unsigned i;
11377 /* We don't want to do this if generating a PCH. */
11378 if (pch_file)
11379 return;
11381 timevar_stop (TV_PHASE_PARSING);
11382 timevar_start (TV_PHASE_DEFERRED);
11384 /* Do the Objective-C stuff. This is where all the Objective-C
11385 module stuff gets generated (symtab, class/protocol/selector
11386 lists etc). */
11387 if (c_dialect_objc ())
11388 objc_write_global_declarations ();
11390 /* Close the external scope. */
11391 ext_block = pop_scope ();
11392 external_scope = 0;
11393 gcc_assert (!current_scope);
11395 /* Handle -fdump-ada-spec[-slim]. */
11396 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11398 /* Build a table of files to generate specs for */
11399 if (flag_dump_ada_spec_slim)
11400 collect_source_ref (main_input_filename);
11401 else
11402 for_each_global_decl (collect_source_ref_cb);
11404 dump_ada_specs (collect_all_refs, NULL);
11407 /* Process all file scopes in this compilation, and the external_scope,
11408 through wrapup_global_declarations. */
11409 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11410 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11411 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11413 timevar_stop (TV_PHASE_DEFERRED);
11414 timevar_start (TV_PHASE_PARSING);
11416 ext_block = NULL;
11419 /* Register reserved keyword WORD as qualifier for address space AS. */
11421 void
11422 c_register_addr_space (const char *word, addr_space_t as)
11424 int rid = RID_FIRST_ADDR_SPACE + as;
11425 tree id;
11427 /* Address space qualifiers are only supported
11428 in C with GNU extensions enabled. */
11429 if (c_dialect_objc () || flag_no_asm)
11430 return;
11432 id = get_identifier (word);
11433 C_SET_RID_CODE (id, rid);
11434 C_IS_RESERVED_WORD (id) = 1;
11435 ridpointers [rid] = id;
11438 /* Return identifier to look up for omp declare reduction. */
11440 tree
11441 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11443 const char *p = NULL;
11444 switch (reduction_code)
11446 case PLUS_EXPR: p = "+"; break;
11447 case MULT_EXPR: p = "*"; break;
11448 case MINUS_EXPR: p = "-"; break;
11449 case BIT_AND_EXPR: p = "&"; break;
11450 case BIT_XOR_EXPR: p = "^"; break;
11451 case BIT_IOR_EXPR: p = "|"; break;
11452 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11453 case TRUTH_ORIF_EXPR: p = "||"; break;
11454 case MIN_EXPR: p = "min"; break;
11455 case MAX_EXPR: p = "max"; break;
11456 default:
11457 break;
11460 if (p == NULL)
11462 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11463 return error_mark_node;
11464 p = IDENTIFIER_POINTER (reduction_id);
11467 const char prefix[] = "omp declare reduction ";
11468 size_t lenp = sizeof (prefix);
11469 size_t len = strlen (p);
11470 char *name = XALLOCAVEC (char, lenp + len);
11471 memcpy (name, prefix, lenp - 1);
11472 memcpy (name + lenp - 1, p, len + 1);
11473 return get_identifier (name);
11476 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11477 VAR_DECL, bind it into the current scope and return it. */
11479 tree
11480 c_omp_reduction_decl (tree reduction_id)
11482 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11483 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11484 return b->decl;
11486 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11487 reduction_id, integer_type_node);
11488 DECL_ARTIFICIAL (decl) = 1;
11489 DECL_EXTERNAL (decl) = 1;
11490 TREE_STATIC (decl) = 1;
11491 TREE_PUBLIC (decl) = 0;
11492 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11493 return decl;
11496 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11498 tree
11499 c_omp_reduction_lookup (tree reduction_id, tree type)
11501 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11502 while (b)
11504 tree t;
11505 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11506 if (comptypes (TREE_PURPOSE (t), type))
11507 return TREE_VALUE (t);
11508 b = b->shadowed;
11510 return error_mark_node;
11513 /* Helper function called via walk_tree, to diagnose invalid
11514 #pragma omp declare reduction combiners or initializers. */
11516 tree
11517 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11519 tree *vars = (tree *) data;
11520 if (SSA_VAR_P (*tp)
11521 && !DECL_ARTIFICIAL (*tp)
11522 && *tp != vars[0]
11523 && *tp != vars[1])
11525 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11526 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11527 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11528 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11529 *tp);
11530 else
11531 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11532 "to variable %qD which is not %<omp_priv%> nor "
11533 "%<omp_orig%>",
11534 *tp);
11535 return *tp;
11537 return NULL_TREE;
11540 #include "gt-c-c-decl.h"