PR tree-optimization/86650 - -Warray-bounds missing inlining context
[official-gcc.git] / gcc / c / c-decl.c
blobed1dd28cac63c8fe2155b99cd74ef60419e97f4e
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 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4647 NULL_TREE, attributes);
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 if (flag_openmp
5227 && VAR_P (decl)
5228 && lookup_attribute ("omp declare target implicit",
5229 DECL_ATTRIBUTES (decl)))
5231 DECL_ATTRIBUTES (decl)
5232 = remove_attribute ("omp declare target implicit",
5233 DECL_ATTRIBUTES (decl));
5234 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5235 error ("%q+D in declare target directive does not have mappable type",
5236 decl);
5237 else if (!lookup_attribute ("omp declare target",
5238 DECL_ATTRIBUTES (decl))
5239 && !lookup_attribute ("omp declare target link",
5240 DECL_ATTRIBUTES (decl)))
5241 DECL_ATTRIBUTES (decl)
5242 = tree_cons (get_identifier ("omp declare target"),
5243 NULL_TREE, DECL_ATTRIBUTES (decl));
5246 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5249 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5250 EXPR is NULL or a pointer to an expression that needs to be
5251 evaluated for the side effects of array size expressions in the
5252 parameters. */
5254 tree
5255 grokparm (const struct c_parm *parm, tree *expr)
5257 tree attrs = parm->attrs;
5258 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5259 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5261 decl_attributes (&decl, attrs, 0);
5263 return decl;
5266 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5267 and push that on the current scope. EXPR is a pointer to an
5268 expression that needs to be evaluated for the side effects of array
5269 size expressions in the parameters. */
5271 void
5272 push_parm_decl (const struct c_parm *parm, tree *expr)
5274 tree attrs = parm->attrs;
5275 tree decl;
5277 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5278 &attrs, expr, NULL, DEPRECATED_NORMAL);
5279 if (decl && DECL_P (decl))
5280 DECL_SOURCE_LOCATION (decl) = parm->loc;
5281 decl_attributes (&decl, attrs, 0);
5283 decl = pushdecl (decl);
5285 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5288 /* Mark all the parameter declarations to date as forward decls.
5289 Also diagnose use of this extension. */
5291 void
5292 mark_forward_parm_decls (void)
5294 struct c_binding *b;
5296 if (pedantic && !current_scope->warned_forward_parm_decls)
5298 pedwarn (input_location, OPT_Wpedantic,
5299 "ISO C forbids forward parameter declarations");
5300 current_scope->warned_forward_parm_decls = true;
5303 for (b = current_scope->bindings; b; b = b->prev)
5304 if (TREE_CODE (b->decl) == PARM_DECL)
5305 TREE_ASM_WRITTEN (b->decl) = 1;
5308 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5309 literal, which may be an incomplete array type completed by the
5310 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5311 literal. NON_CONST is true if the initializers contain something
5312 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5313 it is the (valid) alignment for this compound literal, as specified
5314 with _Alignas. */
5316 tree
5317 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5318 unsigned int alignas_align)
5320 /* We do not use start_decl here because we have a type, not a declarator;
5321 and do not use finish_decl because the decl should be stored inside
5322 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5323 tree decl;
5324 tree complit;
5325 tree stmt;
5327 if (type == error_mark_node
5328 || init == error_mark_node)
5329 return error_mark_node;
5331 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5332 DECL_EXTERNAL (decl) = 0;
5333 TREE_PUBLIC (decl) = 0;
5334 TREE_STATIC (decl) = (current_scope == file_scope);
5335 DECL_CONTEXT (decl) = current_function_decl;
5336 TREE_USED (decl) = 1;
5337 DECL_READ_P (decl) = 1;
5338 DECL_ARTIFICIAL (decl) = 1;
5339 DECL_IGNORED_P (decl) = 1;
5340 TREE_TYPE (decl) = type;
5341 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5342 if (alignas_align)
5344 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5345 DECL_USER_ALIGN (decl) = 1;
5347 store_init_value (loc, decl, init, NULL_TREE);
5349 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5351 int failure = complete_array_type (&TREE_TYPE (decl),
5352 DECL_INITIAL (decl), true);
5353 /* If complete_array_type returns 3, it means that the
5354 initial value of the compound literal is empty. Allow it. */
5355 gcc_assert (failure == 0 || failure == 3);
5357 type = TREE_TYPE (decl);
5358 TREE_TYPE (DECL_INITIAL (decl)) = type;
5361 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5363 c_incomplete_type_error (loc, NULL_TREE, type);
5364 return error_mark_node;
5367 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5368 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5369 TREE_SIDE_EFFECTS (complit) = 1;
5371 layout_decl (decl, 0);
5373 if (TREE_STATIC (decl))
5375 /* This decl needs a name for the assembler output. */
5376 set_compound_literal_name (decl);
5377 DECL_DEFER_OUTPUT (decl) = 1;
5378 DECL_COMDAT (decl) = 1;
5379 pushdecl (decl);
5380 rest_of_decl_compilation (decl, 1, 0);
5382 else
5383 pushdecl (decl);
5385 if (non_const)
5387 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5388 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5391 return complit;
5394 /* Check the type of a compound literal. Here we just check that it
5395 is valid for C++. */
5397 void
5398 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5400 if (warn_cxx_compat
5401 && (type_name->specs->typespec_kind == ctsk_tagdef
5402 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5403 warning_at (loc, OPT_Wc___compat,
5404 "defining a type in a compound literal is invalid in C++");
5407 /* Determine whether TYPE is a structure with a flexible array member,
5408 or a union containing such a structure (possibly recursively). */
5410 static bool
5411 flexible_array_type_p (tree type)
5413 tree x;
5414 switch (TREE_CODE (type))
5416 case RECORD_TYPE:
5417 x = TYPE_FIELDS (type);
5418 if (x == NULL_TREE)
5419 return false;
5420 while (DECL_CHAIN (x) != NULL_TREE)
5421 x = DECL_CHAIN (x);
5422 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5423 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5424 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5425 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5426 return true;
5427 return false;
5428 case UNION_TYPE:
5429 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5431 if (flexible_array_type_p (TREE_TYPE (x)))
5432 return true;
5434 return false;
5435 default:
5436 return false;
5440 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5441 replacing with appropriate values if they are invalid. */
5443 static void
5444 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5445 tree orig_name)
5447 tree type_mv;
5448 unsigned int max_width;
5449 unsigned HOST_WIDE_INT w;
5450 const char *name = (orig_name
5451 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5452 : _("<anonymous>"));
5454 /* Detect and ignore out of range field width and process valid
5455 field widths. */
5456 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5458 error_at (loc, "bit-field %qs width not an integer constant", name);
5459 *width = integer_one_node;
5461 else
5463 if (TREE_CODE (*width) != INTEGER_CST)
5465 *width = c_fully_fold (*width, false, NULL);
5466 if (TREE_CODE (*width) == INTEGER_CST)
5467 pedwarn (loc, OPT_Wpedantic,
5468 "bit-field %qs width not an integer constant expression",
5469 name);
5471 if (TREE_CODE (*width) != INTEGER_CST)
5473 error_at (loc, "bit-field %qs width not an integer constant", name);
5474 *width = integer_one_node;
5476 constant_expression_warning (*width);
5477 if (tree_int_cst_sgn (*width) < 0)
5479 error_at (loc, "negative width in bit-field %qs", name);
5480 *width = integer_one_node;
5482 else if (integer_zerop (*width) && orig_name)
5484 error_at (loc, "zero width for bit-field %qs", name);
5485 *width = integer_one_node;
5489 /* Detect invalid bit-field type. */
5490 if (TREE_CODE (*type) != INTEGER_TYPE
5491 && TREE_CODE (*type) != BOOLEAN_TYPE
5492 && TREE_CODE (*type) != ENUMERAL_TYPE)
5494 error_at (loc, "bit-field %qs has invalid type", name);
5495 *type = unsigned_type_node;
5498 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5500 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5501 name);
5502 *type = unsigned_type_node;
5505 type_mv = TYPE_MAIN_VARIANT (*type);
5506 if (!in_system_header_at (input_location)
5507 && type_mv != integer_type_node
5508 && type_mv != unsigned_type_node
5509 && type_mv != boolean_type_node)
5510 pedwarn_c90 (loc, OPT_Wpedantic,
5511 "type of bit-field %qs is a GCC extension", name);
5513 max_width = TYPE_PRECISION (*type);
5515 if (compare_tree_int (*width, max_width) > 0)
5517 error_at (loc, "width of %qs exceeds its type", name);
5518 w = max_width;
5519 *width = build_int_cst (integer_type_node, w);
5521 else
5522 w = tree_to_uhwi (*width);
5524 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5526 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5527 if (!lt
5528 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5529 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5530 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5536 /* Print warning about variable length array if necessary. */
5538 static void
5539 warn_variable_length_array (tree name, tree size)
5541 if (TREE_CONSTANT (size))
5543 if (name)
5544 pedwarn_c90 (input_location, OPT_Wvla,
5545 "ISO C90 forbids array %qE whose size "
5546 "can%'t be evaluated", name);
5547 else
5548 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5549 "whose size can%'t be evaluated");
5551 else
5553 if (name)
5554 pedwarn_c90 (input_location, OPT_Wvla,
5555 "ISO C90 forbids variable length array %qE", name);
5556 else
5557 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5558 "length array");
5562 /* Print warning about defaulting to int if necessary. */
5564 static void
5565 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5567 diagnostic_info diagnostic;
5568 va_list ap;
5569 rich_location richloc (line_table, location);
5571 va_start (ap, gmsgid);
5572 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5573 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5574 diagnostic.option_index = opt;
5575 diagnostic_report_diagnostic (global_dc, &diagnostic);
5576 va_end (ap);
5579 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5580 considering only those c_declspec_words found in LIST, which
5581 must be terminated by cdw_number_of_elements. */
5583 static location_t
5584 smallest_type_quals_location (const location_t *locations,
5585 const c_declspec_word *list)
5587 location_t loc = UNKNOWN_LOCATION;
5588 while (*list != cdw_number_of_elements)
5590 location_t newloc = locations[*list];
5591 if (loc == UNKNOWN_LOCATION
5592 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5593 loc = newloc;
5594 list++;
5597 return loc;
5600 /* Given declspecs and a declarator,
5601 determine the name and type of the object declared
5602 and construct a ..._DECL node for it.
5603 (In one case we can return a ..._TYPE node instead.
5604 For invalid input we sometimes return NULL_TREE.)
5606 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5608 DECL_CONTEXT says which syntactic context this declaration is in:
5609 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5610 FUNCDEF for a function definition. Like NORMAL but a few different
5611 error messages in each case. Return value may be zero meaning
5612 this definition is too screwy to try to parse.
5613 PARM for a parameter declaration (either within a function prototype
5614 or before a function body). Make a PARM_DECL, or return void_type_node.
5615 TYPENAME if for a typename (in a cast or sizeof).
5616 Don't make a DECL node; just return the ..._TYPE node.
5617 FIELD for a struct or union field; make a FIELD_DECL.
5618 INITIALIZED is true if the decl has an initializer.
5619 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5620 representing the width of the bit-field.
5621 DECL_ATTRS points to the list of attributes that should be added to this
5622 decl. Any nested attributes that belong on the decl itself will be
5623 added to this list.
5624 If EXPR is not NULL, any expressions that need to be evaluated as
5625 part of evaluating variably modified types will be stored in *EXPR.
5626 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5627 set to indicate whether operands in *EXPR can be used in constant
5628 expressions.
5629 DEPRECATED_STATE is a deprecated_states value indicating whether
5630 deprecation warnings should be suppressed.
5632 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5633 It may also be so in the PARM case, for a prototype where the
5634 argument type is specified but not the name.
5636 This function is where the complicated C meanings of `static'
5637 and `extern' are interpreted. */
5639 static tree
5640 grokdeclarator (const struct c_declarator *declarator,
5641 struct c_declspecs *declspecs,
5642 enum decl_context decl_context, bool initialized, tree *width,
5643 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5644 enum deprecated_states deprecated_state)
5646 tree type = declspecs->type;
5647 bool threadp = declspecs->thread_p;
5648 enum c_storage_class storage_class = declspecs->storage_class;
5649 int constp;
5650 int restrictp;
5651 int volatilep;
5652 int atomicp;
5653 int type_quals = TYPE_UNQUALIFIED;
5654 tree name = NULL_TREE;
5655 bool funcdef_flag = false;
5656 bool funcdef_syntax = false;
5657 bool size_varies = false;
5658 tree decl_attr = declspecs->decl_attr;
5659 int array_ptr_quals = TYPE_UNQUALIFIED;
5660 tree array_ptr_attrs = NULL_TREE;
5661 bool array_parm_static = false;
5662 bool array_parm_vla_unspec_p = false;
5663 tree returned_attrs = NULL_TREE;
5664 bool bitfield = width != NULL;
5665 tree element_type;
5666 tree orig_qual_type = NULL;
5667 size_t orig_qual_indirect = 0;
5668 struct c_arg_info *arg_info = 0;
5669 addr_space_t as1, as2, address_space;
5670 location_t loc = UNKNOWN_LOCATION;
5671 tree expr_dummy;
5672 bool expr_const_operands_dummy;
5673 enum c_declarator_kind first_non_attr_kind;
5674 unsigned int alignas_align = 0;
5676 if (TREE_CODE (type) == ERROR_MARK)
5677 return error_mark_node;
5678 if (expr == NULL)
5680 expr = &expr_dummy;
5681 expr_dummy = NULL_TREE;
5683 if (expr_const_operands == NULL)
5684 expr_const_operands = &expr_const_operands_dummy;
5686 if (declspecs->expr)
5688 if (*expr)
5689 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5690 declspecs->expr);
5691 else
5692 *expr = declspecs->expr;
5694 *expr_const_operands = declspecs->expr_const_operands;
5696 if (decl_context == FUNCDEF)
5697 funcdef_flag = true, decl_context = NORMAL;
5699 /* Look inside a declarator for the name being declared
5700 and get it as an IDENTIFIER_NODE, for an error message. */
5702 const struct c_declarator *decl = declarator;
5704 first_non_attr_kind = cdk_attrs;
5705 while (decl)
5706 switch (decl->kind)
5708 case cdk_array:
5709 loc = decl->id_loc;
5710 /* FALL THRU. */
5712 case cdk_function:
5713 case cdk_pointer:
5714 funcdef_syntax = (decl->kind == cdk_function);
5715 if (first_non_attr_kind == cdk_attrs)
5716 first_non_attr_kind = decl->kind;
5717 decl = decl->declarator;
5718 break;
5720 case cdk_attrs:
5721 decl = decl->declarator;
5722 break;
5724 case cdk_id:
5725 loc = decl->id_loc;
5726 if (decl->u.id)
5727 name = decl->u.id;
5728 if (first_non_attr_kind == cdk_attrs)
5729 first_non_attr_kind = decl->kind;
5730 decl = 0;
5731 break;
5733 default:
5734 gcc_unreachable ();
5736 if (name == NULL_TREE)
5738 gcc_assert (decl_context == PARM
5739 || decl_context == TYPENAME
5740 || (decl_context == FIELD
5741 && declarator->kind == cdk_id));
5742 gcc_assert (!initialized);
5746 /* A function definition's declarator must have the form of
5747 a function declarator. */
5749 if (funcdef_flag && !funcdef_syntax)
5750 return NULL_TREE;
5752 /* If this looks like a function definition, make it one,
5753 even if it occurs where parms are expected.
5754 Then store_parm_decls will reject it and not use it as a parm. */
5755 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5756 decl_context = PARM;
5758 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5759 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5761 if ((decl_context == NORMAL || decl_context == FIELD)
5762 && current_scope == file_scope
5763 && variably_modified_type_p (type, NULL_TREE))
5765 if (name)
5766 error_at (loc, "variably modified %qE at file scope", name);
5767 else
5768 error_at (loc, "variably modified field at file scope");
5769 type = integer_type_node;
5772 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5774 /* Diagnose defaulting to "int". */
5776 if (declspecs->default_int_p && !in_system_header_at (input_location))
5778 /* Issue a warning if this is an ISO C 99 program or if
5779 -Wreturn-type and this is a function, or if -Wimplicit;
5780 prefer the former warning since it is more explicit. */
5781 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
5782 && funcdef_flag)
5783 warn_about_return_type = 1;
5784 else
5786 if (name)
5787 warn_defaults_to (loc, OPT_Wimplicit_int,
5788 "type defaults to %<int%> in declaration "
5789 "of %qE", name);
5790 else
5791 warn_defaults_to (loc, OPT_Wimplicit_int,
5792 "type defaults to %<int%> in type name");
5796 /* Adjust the type if a bit-field is being declared,
5797 -funsigned-bitfields applied and the type is not explicitly
5798 "signed". */
5799 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5800 && TREE_CODE (type) == INTEGER_TYPE)
5801 type = unsigned_type_for (type);
5803 /* Figure out the type qualifiers for the declaration. There are
5804 two ways a declaration can become qualified. One is something
5805 like `const int i' where the `const' is explicit. Another is
5806 something like `typedef const int CI; CI i' where the type of the
5807 declaration contains the `const'. A third possibility is that
5808 there is a type qualifier on the element type of a typedefed
5809 array type, in which case we should extract that qualifier so
5810 that c_apply_type_quals_to_decl receives the full list of
5811 qualifiers to work with (C90 is not entirely clear about whether
5812 duplicate qualifiers should be diagnosed in this case, but it
5813 seems most appropriate to do so). */
5814 element_type = strip_array_types (type);
5815 constp = declspecs->const_p + TYPE_READONLY (element_type);
5816 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5817 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5818 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5819 as1 = declspecs->address_space;
5820 as2 = TYPE_ADDR_SPACE (element_type);
5821 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5823 if (constp > 1)
5824 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5825 if (restrictp > 1)
5826 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5827 if (volatilep > 1)
5828 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5829 if (atomicp > 1)
5830 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5832 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5833 error_at (loc, "conflicting named address spaces (%s vs %s)",
5834 c_addr_space_name (as1), c_addr_space_name (as2));
5836 if ((TREE_CODE (type) == ARRAY_TYPE
5837 || first_non_attr_kind == cdk_array)
5838 && TYPE_QUALS (element_type))
5840 orig_qual_type = type;
5841 type = TYPE_MAIN_VARIANT (type);
5843 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5844 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5845 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5846 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5847 | ENCODE_QUAL_ADDR_SPACE (address_space));
5848 if (type_quals != TYPE_QUALS (element_type))
5849 orig_qual_type = NULL_TREE;
5851 /* Applying the _Atomic qualifier to an array type (through the use
5852 of typedefs or typeof) must be detected here. If the qualifier
5853 is introduced later, any appearance of applying it to an array is
5854 actually applying it to an element of that array. */
5855 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5856 error_at (loc, "%<_Atomic%>-qualified array type");
5858 /* Warn about storage classes that are invalid for certain
5859 kinds of declarations (parameters, typenames, etc.). */
5861 if (funcdef_flag
5862 && (threadp
5863 || storage_class == csc_auto
5864 || storage_class == csc_register
5865 || storage_class == csc_typedef))
5867 if (storage_class == csc_auto)
5868 pedwarn (loc,
5869 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5870 "function definition declared %<auto%>");
5871 if (storage_class == csc_register)
5872 error_at (loc, "function definition declared %<register%>");
5873 if (storage_class == csc_typedef)
5874 error_at (loc, "function definition declared %<typedef%>");
5875 if (threadp)
5876 error_at (loc, "function definition declared %qs",
5877 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5878 threadp = false;
5879 if (storage_class == csc_auto
5880 || storage_class == csc_register
5881 || storage_class == csc_typedef)
5882 storage_class = csc_none;
5884 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5886 if (decl_context == PARM && storage_class == csc_register)
5888 else
5890 switch (decl_context)
5892 case FIELD:
5893 if (name)
5894 error_at (loc, "storage class specified for structure "
5895 "field %qE", name);
5896 else
5897 error_at (loc, "storage class specified for structure field");
5898 break;
5899 case PARM:
5900 if (name)
5901 error_at (loc, "storage class specified for parameter %qE",
5902 name);
5903 else
5904 error_at (loc, "storage class specified for unnamed parameter");
5905 break;
5906 default:
5907 error_at (loc, "storage class specified for typename");
5908 break;
5910 storage_class = csc_none;
5911 threadp = false;
5914 else if (storage_class == csc_extern
5915 && initialized
5916 && !funcdef_flag)
5918 /* 'extern' with initialization is invalid if not at file scope. */
5919 if (current_scope == file_scope)
5921 /* It is fine to have 'extern const' when compiling at C
5922 and C++ intersection. */
5923 if (!(warn_cxx_compat && constp))
5924 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5925 name);
5927 else
5928 error_at (loc, "%qE has both %<extern%> and initializer", name);
5930 else if (current_scope == file_scope)
5932 if (storage_class == csc_auto)
5933 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5934 name);
5935 if (pedantic && storage_class == csc_register)
5936 pedwarn (input_location, OPT_Wpedantic,
5937 "file-scope declaration of %qE specifies %<register%>", name);
5939 else
5941 if (storage_class == csc_extern && funcdef_flag)
5942 error_at (loc, "nested function %qE declared %<extern%>", name);
5943 else if (threadp && storage_class == csc_none)
5945 error_at (loc, "function-scope %qE implicitly auto and declared "
5946 "%qs", name,
5947 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5948 threadp = false;
5952 /* Now figure out the structure of the declarator proper.
5953 Descend through it, creating more complex types, until we reach
5954 the declared identifier (or NULL_TREE, in an absolute declarator).
5955 At each stage we maintain an unqualified version of the type
5956 together with any qualifiers that should be applied to it with
5957 c_build_qualified_type; this way, array types including
5958 multidimensional array types are first built up in unqualified
5959 form and then the qualified form is created with
5960 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5962 while (declarator && declarator->kind != cdk_id)
5964 if (type == error_mark_node)
5966 declarator = declarator->declarator;
5967 continue;
5970 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5971 a cdk_pointer (for *...),
5972 a cdk_function (for ...(...)),
5973 a cdk_attrs (for nested attributes),
5974 or a cdk_id (for the name being declared
5975 or the place in an absolute declarator
5976 where the name was omitted).
5977 For the last case, we have just exited the loop.
5979 At this point, TYPE is the type of elements of an array,
5980 or for a function to return, or for a pointer to point to.
5981 After this sequence of ifs, TYPE is the type of the
5982 array or function or pointer, and DECLARATOR has had its
5983 outermost layer removed. */
5985 if (array_ptr_quals != TYPE_UNQUALIFIED
5986 || array_ptr_attrs != NULL_TREE
5987 || array_parm_static)
5989 /* Only the innermost declarator (making a parameter be of
5990 array type which is converted to pointer type)
5991 may have static or type qualifiers. */
5992 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5993 array_ptr_quals = TYPE_UNQUALIFIED;
5994 array_ptr_attrs = NULL_TREE;
5995 array_parm_static = false;
5998 switch (declarator->kind)
6000 case cdk_attrs:
6002 /* A declarator with embedded attributes. */
6003 tree attrs = declarator->u.attrs;
6004 const struct c_declarator *inner_decl;
6005 int attr_flags = 0;
6006 declarator = declarator->declarator;
6007 inner_decl = declarator;
6008 while (inner_decl->kind == cdk_attrs)
6009 inner_decl = inner_decl->declarator;
6010 if (inner_decl->kind == cdk_id)
6011 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6012 else if (inner_decl->kind == cdk_function)
6013 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6014 else if (inner_decl->kind == cdk_array)
6015 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6016 returned_attrs = decl_attributes (&type,
6017 chainon (returned_attrs, attrs),
6018 attr_flags);
6019 break;
6021 case cdk_array:
6023 tree itype = NULL_TREE;
6024 tree size = declarator->u.array.dimen;
6025 /* The index is a signed object `sizetype' bits wide. */
6026 tree index_type = c_common_signed_type (sizetype);
6028 array_ptr_quals = declarator->u.array.quals;
6029 array_ptr_attrs = declarator->u.array.attrs;
6030 array_parm_static = declarator->u.array.static_p;
6031 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6033 declarator = declarator->declarator;
6035 /* Check for some types that there cannot be arrays of. */
6037 if (VOID_TYPE_P (type))
6039 if (name)
6040 error_at (loc, "declaration of %qE as array of voids", name);
6041 else
6042 error_at (loc, "declaration of type name as array of voids");
6043 type = error_mark_node;
6046 if (TREE_CODE (type) == FUNCTION_TYPE)
6048 if (name)
6049 error_at (loc, "declaration of %qE as array of functions",
6050 name);
6051 else
6052 error_at (loc, "declaration of type name as array of "
6053 "functions");
6054 type = error_mark_node;
6057 if (pedantic && !in_system_header_at (input_location)
6058 && flexible_array_type_p (type))
6059 pedwarn (loc, OPT_Wpedantic,
6060 "invalid use of structure with flexible array member");
6062 if (size == error_mark_node)
6063 type = error_mark_node;
6065 if (type == error_mark_node)
6066 continue;
6068 /* If size was specified, set ITYPE to a range-type for
6069 that size. Otherwise, ITYPE remains null. finish_decl
6070 may figure it out from an initial value. */
6072 if (size)
6074 bool size_maybe_const = true;
6075 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6076 && !TREE_OVERFLOW (size));
6077 bool this_size_varies = false;
6079 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6080 lvalue. */
6081 STRIP_TYPE_NOPS (size);
6083 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6085 if (name)
6086 error_at (loc, "size of array %qE has non-integer type",
6087 name);
6088 else
6089 error_at (loc,
6090 "size of unnamed array has non-integer type");
6091 size = integer_one_node;
6093 /* This can happen with enum forward declaration. */
6094 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6096 if (name)
6097 error_at (loc, "size of array %qE has incomplete type",
6098 name);
6099 else
6100 error_at (loc, "size of unnamed array has incomplete "
6101 "type");
6102 size = integer_one_node;
6105 size = c_fully_fold (size, false, &size_maybe_const);
6107 if (pedantic && size_maybe_const && integer_zerop (size))
6109 if (name)
6110 pedwarn (loc, OPT_Wpedantic,
6111 "ISO C forbids zero-size array %qE", name);
6112 else
6113 pedwarn (loc, OPT_Wpedantic,
6114 "ISO C forbids zero-size array");
6117 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6119 constant_expression_warning (size);
6120 if (tree_int_cst_sgn (size) < 0)
6122 if (name)
6123 error_at (loc, "size of array %qE is negative", name);
6124 else
6125 error_at (loc, "size of unnamed array is negative");
6126 size = integer_one_node;
6128 /* Handle a size folded to an integer constant but
6129 not an integer constant expression. */
6130 if (!size_int_const)
6132 /* If this is a file scope declaration of an
6133 ordinary identifier, this is invalid code;
6134 diagnosing it here and not subsequently
6135 treating the type as variable-length avoids
6136 more confusing diagnostics later. */
6137 if ((decl_context == NORMAL || decl_context == FIELD)
6138 && current_scope == file_scope)
6139 pedwarn (input_location, 0,
6140 "variably modified %qE at file scope",
6141 name);
6142 else
6143 this_size_varies = size_varies = true;
6144 warn_variable_length_array (name, size);
6147 else if ((decl_context == NORMAL || decl_context == FIELD)
6148 && current_scope == file_scope)
6150 error_at (loc, "variably modified %qE at file scope", name);
6151 size = integer_one_node;
6153 else
6155 /* Make sure the array size remains visibly
6156 nonconstant even if it is (eg) a const variable
6157 with known value. */
6158 this_size_varies = size_varies = true;
6159 warn_variable_length_array (name, size);
6160 if (sanitize_flags_p (SANITIZE_VLA)
6161 && current_function_decl != NULL_TREE
6162 && decl_context == NORMAL)
6164 /* Evaluate the array size only once. */
6165 size = save_expr (size);
6166 size = c_fully_fold (size, false, NULL);
6167 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6168 ubsan_instrument_vla (loc, size),
6169 size);
6173 if (integer_zerop (size) && !this_size_varies)
6175 /* A zero-length array cannot be represented with
6176 an unsigned index type, which is what we'll
6177 get with build_index_type. Create an
6178 open-ended range instead. */
6179 itype = build_range_type (sizetype, size, NULL_TREE);
6181 else
6183 /* Arrange for the SAVE_EXPR on the inside of the
6184 MINUS_EXPR, which allows the -1 to get folded
6185 with the +1 that happens when building TYPE_SIZE. */
6186 if (size_varies)
6187 size = save_expr (size);
6188 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6189 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6190 integer_zero_node, size);
6192 /* Compute the maximum valid index, that is, size
6193 - 1. Do the calculation in index_type, so that
6194 if it is a variable the computations will be
6195 done in the proper mode. */
6196 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6197 convert (index_type, size),
6198 convert (index_type,
6199 size_one_node));
6201 /* The above overflows when size does not fit
6202 in index_type.
6203 ??? While a size of INT_MAX+1 technically shouldn't
6204 cause an overflow (because we subtract 1), handling
6205 this case seems like an unnecessary complication. */
6206 if (TREE_CODE (size) == INTEGER_CST
6207 && !int_fits_type_p (size, index_type))
6209 if (name)
6210 error_at (loc, "size of array %qE is too large",
6211 name);
6212 else
6213 error_at (loc, "size of unnamed array is too large");
6214 type = error_mark_node;
6215 continue;
6218 itype = build_index_type (itype);
6220 if (this_size_varies)
6222 if (*expr)
6223 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6224 *expr, size);
6225 else
6226 *expr = size;
6227 *expr_const_operands &= size_maybe_const;
6230 else if (decl_context == FIELD)
6232 bool flexible_array_member = false;
6233 if (array_parm_vla_unspec_p)
6234 /* Field names can in fact have function prototype
6235 scope so [*] is disallowed here through making
6236 the field variably modified, not through being
6237 something other than a declaration with function
6238 prototype scope. */
6239 size_varies = true;
6240 else
6242 const struct c_declarator *t = declarator;
6243 while (t->kind == cdk_attrs)
6244 t = t->declarator;
6245 flexible_array_member = (t->kind == cdk_id);
6247 if (flexible_array_member
6248 && !in_system_header_at (input_location))
6249 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6250 "support flexible array members");
6252 /* ISO C99 Flexible array members are effectively
6253 identical to GCC's zero-length array extension. */
6254 if (flexible_array_member || array_parm_vla_unspec_p)
6255 itype = build_range_type (sizetype, size_zero_node,
6256 NULL_TREE);
6258 else if (decl_context == PARM)
6260 if (array_parm_vla_unspec_p)
6262 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6263 size_varies = true;
6266 else if (decl_context == TYPENAME)
6268 if (array_parm_vla_unspec_p)
6270 /* C99 6.7.5.2p4 */
6271 warning (0, "%<[*]%> not in a declaration");
6272 /* We use this to avoid messing up with incomplete
6273 array types of the same type, that would
6274 otherwise be modified below. */
6275 itype = build_range_type (sizetype, size_zero_node,
6276 NULL_TREE);
6277 size_varies = true;
6281 /* Complain about arrays of incomplete types. */
6282 if (!COMPLETE_TYPE_P (type))
6284 error_at (loc, "array type has incomplete element type %qT",
6285 type);
6286 /* See if we can be more helpful. */
6287 if (TREE_CODE (type) == ARRAY_TYPE)
6289 if (name)
6290 inform (loc, "declaration of %qE as multidimensional "
6291 "array must have bounds for all dimensions "
6292 "except the first", name);
6293 else
6294 inform (loc, "declaration of multidimensional array "
6295 "must have bounds for all dimensions except "
6296 "the first");
6298 type = error_mark_node;
6300 else
6301 /* When itype is NULL, a shared incomplete array type is
6302 returned for all array of a given type. Elsewhere we
6303 make sure we don't complete that type before copying
6304 it, but here we want to make sure we don't ever
6305 modify the shared type, so we gcc_assert (itype)
6306 below. */
6308 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6309 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6310 type = build_qualified_type (type,
6311 ENCODE_QUAL_ADDR_SPACE (as));
6313 type = build_array_type (type, itype);
6316 if (type != error_mark_node)
6318 if (size_varies)
6320 /* It is ok to modify type here even if itype is
6321 NULL: if size_varies, we're in a
6322 multi-dimensional array and the inner type has
6323 variable size, so the enclosing shared array type
6324 must too. */
6325 if (size && TREE_CODE (size) == INTEGER_CST)
6326 type
6327 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6328 C_TYPE_VARIABLE_SIZE (type) = 1;
6331 /* The GCC extension for zero-length arrays differs from
6332 ISO flexible array members in that sizeof yields
6333 zero. */
6334 if (size && integer_zerop (size))
6336 gcc_assert (itype);
6337 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6338 TYPE_SIZE (type) = bitsize_zero_node;
6339 TYPE_SIZE_UNIT (type) = size_zero_node;
6340 SET_TYPE_STRUCTURAL_EQUALITY (type);
6342 if (array_parm_vla_unspec_p)
6344 gcc_assert (itype);
6345 /* The type is complete. C99 6.7.5.2p4 */
6346 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6347 TYPE_SIZE (type) = bitsize_zero_node;
6348 TYPE_SIZE_UNIT (type) = size_zero_node;
6349 SET_TYPE_STRUCTURAL_EQUALITY (type);
6352 if (!valid_array_size_p (loc, type, name))
6353 type = error_mark_node;
6356 if (decl_context != PARM
6357 && (array_ptr_quals != TYPE_UNQUALIFIED
6358 || array_ptr_attrs != NULL_TREE
6359 || array_parm_static))
6361 error_at (loc, "static or type qualifiers in non-parameter "
6362 "array declarator");
6363 array_ptr_quals = TYPE_UNQUALIFIED;
6364 array_ptr_attrs = NULL_TREE;
6365 array_parm_static = false;
6367 orig_qual_indirect++;
6368 break;
6370 case cdk_function:
6372 /* Say it's a definition only for the declarator closest
6373 to the identifier, apart possibly from some
6374 attributes. */
6375 bool really_funcdef = false;
6376 tree arg_types;
6377 orig_qual_type = NULL_TREE;
6378 if (funcdef_flag)
6380 const struct c_declarator *t = declarator->declarator;
6381 while (t->kind == cdk_attrs)
6382 t = t->declarator;
6383 really_funcdef = (t->kind == cdk_id);
6386 /* Declaring a function type. Make sure we have a valid
6387 type for the function to return. */
6388 if (type == error_mark_node)
6389 continue;
6391 size_varies = false;
6393 /* Warn about some types functions can't return. */
6394 if (TREE_CODE (type) == FUNCTION_TYPE)
6396 if (name)
6397 error_at (loc, "%qE declared as function returning a "
6398 "function", name);
6399 else
6400 error_at (loc, "type name declared as function "
6401 "returning a function");
6402 type = integer_type_node;
6404 if (TREE_CODE (type) == ARRAY_TYPE)
6406 if (name)
6407 error_at (loc, "%qE declared as function returning an array",
6408 name);
6409 else
6410 error_at (loc, "type name declared as function returning "
6411 "an array");
6412 type = integer_type_node;
6415 /* Construct the function type and go to the next
6416 inner layer of declarator. */
6417 arg_info = declarator->u.arg_info;
6418 arg_types = grokparms (arg_info, really_funcdef);
6420 /* Type qualifiers before the return type of the function
6421 qualify the return type, not the function type. */
6422 if (type_quals)
6424 const enum c_declspec_word ignored_quals_list[] =
6426 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6427 cdw_atomic, cdw_number_of_elements
6429 location_t specs_loc
6430 = smallest_type_quals_location (declspecs->locations,
6431 ignored_quals_list);
6432 if (specs_loc == UNKNOWN_LOCATION)
6433 specs_loc = declspecs->locations[cdw_typedef];
6434 if (specs_loc == UNKNOWN_LOCATION)
6435 specs_loc = loc;
6437 /* Type qualifiers on a function return type are
6438 normally permitted by the standard but have no
6439 effect, so give a warning at -Wreturn-type.
6440 Qualifiers on a void return type are banned on
6441 function definitions in ISO C; GCC used to used
6442 them for noreturn functions. The resolution of C11
6443 DR#423 means qualifiers (other than _Atomic) are
6444 actually removed from the return type when
6445 determining the function type. */
6446 int quals_used = type_quals;
6447 if (flag_isoc11)
6448 quals_used &= TYPE_QUAL_ATOMIC;
6449 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6450 pedwarn (specs_loc, 0,
6451 "function definition has qualified void return type");
6452 else
6453 warning_at (specs_loc, OPT_Wignored_qualifiers,
6454 "type qualifiers ignored on function return type");
6456 /* Ensure an error for restrict on invalid types; the
6457 DR#423 resolution is not entirely clear about
6458 this. */
6459 if (flag_isoc11
6460 && (type_quals & TYPE_QUAL_RESTRICT)
6461 && (!POINTER_TYPE_P (type)
6462 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6463 error_at (loc, "invalid use of %<restrict%>");
6464 if (quals_used)
6465 type = c_build_qualified_type (type, quals_used);
6467 type_quals = TYPE_UNQUALIFIED;
6469 type = build_function_type (type, arg_types);
6470 declarator = declarator->declarator;
6472 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6473 the formal parameter list of this FUNCTION_TYPE to point to
6474 the FUNCTION_TYPE node itself. */
6476 c_arg_tag *tag;
6477 unsigned ix;
6479 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6480 TYPE_CONTEXT (tag->type) = type;
6482 break;
6484 case cdk_pointer:
6486 /* Merge any constancy or volatility into the target type
6487 for the pointer. */
6488 if ((type_quals & TYPE_QUAL_ATOMIC)
6489 && TREE_CODE (type) == FUNCTION_TYPE)
6491 error_at (loc,
6492 "%<_Atomic%>-qualified function type");
6493 type_quals &= ~TYPE_QUAL_ATOMIC;
6495 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6496 && type_quals)
6497 pedwarn (loc, OPT_Wpedantic,
6498 "ISO C forbids qualified function types");
6499 if (type_quals)
6500 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6501 orig_qual_indirect);
6502 orig_qual_type = NULL_TREE;
6503 size_varies = false;
6505 /* When the pointed-to type involves components of variable size,
6506 care must be taken to ensure that the size evaluation code is
6507 emitted early enough to dominate all the possible later uses
6508 and late enough for the variables on which it depends to have
6509 been assigned.
6511 This is expected to happen automatically when the pointed-to
6512 type has a name/declaration of it's own, but special attention
6513 is required if the type is anonymous.
6515 We attach an artificial TYPE_DECL to such pointed-to type
6516 and arrange for it to be included in a DECL_EXPR. This
6517 forces the sizes evaluation at a safe point and ensures it
6518 is not deferred until e.g. within a deeper conditional context.
6520 PARM contexts have no enclosing statement list that
6521 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6522 instead, and add it to the list of expressions that
6523 need to be evaluated.
6525 TYPENAME contexts do have an enclosing statement list,
6526 but it would be incorrect to use it, as the size should
6527 only be evaluated if the containing expression is
6528 evaluated. We might also be in the middle of an
6529 expression with side effects on the pointed-to type size
6530 "arguments" prior to the pointer declaration point and
6531 the fake TYPE_DECL in the enclosing context would force
6532 the size evaluation prior to the side effects. We therefore
6533 use BIND_EXPRs in TYPENAME contexts too. */
6534 if (!TYPE_NAME (type)
6535 && variably_modified_type_p (type, NULL_TREE))
6537 tree bind = NULL_TREE;
6538 if (decl_context == TYPENAME || decl_context == PARM)
6540 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6541 NULL_TREE, NULL_TREE);
6542 TREE_SIDE_EFFECTS (bind) = 1;
6543 BIND_EXPR_BODY (bind) = push_stmt_list ();
6544 push_scope ();
6546 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6547 DECL_ARTIFICIAL (decl) = 1;
6548 pushdecl (decl);
6549 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6550 TYPE_NAME (type) = decl;
6551 if (bind)
6553 pop_scope ();
6554 BIND_EXPR_BODY (bind)
6555 = pop_stmt_list (BIND_EXPR_BODY (bind));
6556 if (*expr)
6557 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6558 bind);
6559 else
6560 *expr = bind;
6564 type = c_build_pointer_type (type);
6566 /* Process type qualifiers (such as const or volatile)
6567 that were given inside the `*'. */
6568 type_quals = declarator->u.pointer_quals;
6570 declarator = declarator->declarator;
6571 break;
6573 default:
6574 gcc_unreachable ();
6577 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6579 /* Now TYPE has the actual type, apart from any qualifiers in
6580 TYPE_QUALS. */
6582 /* Warn about address space used for things other than static memory or
6583 pointers. */
6584 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6585 if (!ADDR_SPACE_GENERIC_P (address_space))
6587 if (decl_context == NORMAL)
6589 switch (storage_class)
6591 case csc_auto:
6592 error ("%qs combined with %<auto%> qualifier for %qE",
6593 c_addr_space_name (address_space), name);
6594 break;
6595 case csc_register:
6596 error ("%qs combined with %<register%> qualifier for %qE",
6597 c_addr_space_name (address_space), name);
6598 break;
6599 case csc_none:
6600 if (current_function_scope)
6602 error ("%qs specified for auto variable %qE",
6603 c_addr_space_name (address_space), name);
6604 break;
6606 break;
6607 case csc_static:
6608 case csc_extern:
6609 case csc_typedef:
6610 break;
6611 default:
6612 gcc_unreachable ();
6615 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6617 if (name)
6618 error ("%qs specified for parameter %qE",
6619 c_addr_space_name (address_space), name);
6620 else
6621 error ("%qs specified for unnamed parameter",
6622 c_addr_space_name (address_space));
6624 else if (decl_context == FIELD)
6626 if (name)
6627 error ("%qs specified for structure field %qE",
6628 c_addr_space_name (address_space), name);
6629 else
6630 error ("%qs specified for structure field",
6631 c_addr_space_name (address_space));
6635 /* Check the type and width of a bit-field. */
6636 if (bitfield)
6638 check_bitfield_type_and_width (loc, &type, width, name);
6639 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6640 atomic types are permitted for bit-fields; we have no code to
6641 make bit-field accesses atomic, so disallow them. */
6642 if (type_quals & TYPE_QUAL_ATOMIC)
6644 if (name)
6645 error_at (loc, "bit-field %qE has atomic type", name);
6646 else
6647 error_at (loc, "bit-field has atomic type");
6648 type_quals &= ~TYPE_QUAL_ATOMIC;
6652 /* Reject invalid uses of _Alignas. */
6653 if (declspecs->alignas_p)
6655 if (storage_class == csc_typedef)
6656 error_at (loc, "alignment specified for typedef %qE", name);
6657 else if (storage_class == csc_register)
6658 error_at (loc, "alignment specified for %<register%> object %qE",
6659 name);
6660 else if (decl_context == PARM)
6662 if (name)
6663 error_at (loc, "alignment specified for parameter %qE", name);
6664 else
6665 error_at (loc, "alignment specified for unnamed parameter");
6667 else if (bitfield)
6669 if (name)
6670 error_at (loc, "alignment specified for bit-field %qE", name);
6671 else
6672 error_at (loc, "alignment specified for unnamed bit-field");
6674 else if (TREE_CODE (type) == FUNCTION_TYPE)
6675 error_at (loc, "alignment specified for function %qE", name);
6676 else if (declspecs->align_log != -1 && TYPE_P (type))
6678 alignas_align = 1U << declspecs->align_log;
6679 if (alignas_align < min_align_of_type (type))
6681 if (name)
6682 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6683 "alignment of %qE", name);
6684 else
6685 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6686 "alignment of unnamed field");
6687 alignas_align = 0;
6692 /* If this is declaring a typedef name, return a TYPE_DECL. */
6694 if (storage_class == csc_typedef)
6696 tree decl;
6697 if ((type_quals & TYPE_QUAL_ATOMIC)
6698 && TREE_CODE (type) == FUNCTION_TYPE)
6700 error_at (loc,
6701 "%<_Atomic%>-qualified function type");
6702 type_quals &= ~TYPE_QUAL_ATOMIC;
6704 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6705 && type_quals)
6706 pedwarn (loc, OPT_Wpedantic,
6707 "ISO C forbids qualified function types");
6708 if (type_quals)
6709 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6710 orig_qual_indirect);
6711 decl = build_decl (declarator->id_loc,
6712 TYPE_DECL, declarator->u.id, type);
6713 if (declspecs->explicit_signed_p)
6714 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6715 if (declspecs->inline_p)
6716 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6717 if (declspecs->noreturn_p)
6718 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6720 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6722 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6724 if (b != NULL
6725 && b->decl != NULL_TREE
6726 && (B_IN_CURRENT_SCOPE (b)
6727 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6728 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6730 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6731 ("using %qD as both a typedef and a tag is "
6732 "invalid in C++"), decl)
6733 && b->locus != UNKNOWN_LOCATION)
6734 inform (b->locus, "originally defined here");
6738 return decl;
6741 /* If this is a type name (such as, in a cast or sizeof),
6742 compute the type and return it now. */
6744 if (decl_context == TYPENAME)
6746 /* Note that the grammar rejects storage classes in typenames
6747 and fields. */
6748 gcc_assert (storage_class == csc_none && !threadp
6749 && !declspecs->inline_p && !declspecs->noreturn_p);
6750 if ((type_quals & TYPE_QUAL_ATOMIC)
6751 && TREE_CODE (type) == FUNCTION_TYPE)
6753 error_at (loc,
6754 "%<_Atomic%>-qualified function type");
6755 type_quals &= ~TYPE_QUAL_ATOMIC;
6757 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6758 && type_quals)
6759 pedwarn (loc, OPT_Wpedantic,
6760 "ISO C forbids const or volatile function types");
6761 if (type_quals)
6762 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6763 orig_qual_indirect);
6764 return type;
6767 if (pedantic && decl_context == FIELD
6768 && variably_modified_type_p (type, NULL_TREE))
6770 /* C99 6.7.2.1p8 */
6771 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6772 "have a variably modified type");
6775 /* Aside from typedefs and type names (handle above),
6776 `void' at top level (not within pointer)
6777 is allowed only in public variables.
6778 We don't complain about parms either, but that is because
6779 a better error message can be made later. */
6781 if (VOID_TYPE_P (type) && decl_context != PARM
6782 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6783 && (storage_class == csc_extern
6784 || (current_scope == file_scope
6785 && !(storage_class == csc_static
6786 || storage_class == csc_register)))))
6788 error_at (loc, "variable or field %qE declared void", name);
6789 type = integer_type_node;
6792 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6793 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6796 tree decl;
6798 if (decl_context == PARM)
6800 tree promoted_type;
6801 bool array_parameter_p = false;
6803 /* A parameter declared as an array of T is really a pointer to T.
6804 One declared as a function is really a pointer to a function. */
6806 if (TREE_CODE (type) == ARRAY_TYPE)
6808 /* Transfer const-ness of array into that of type pointed to. */
6809 type = TREE_TYPE (type);
6810 if (orig_qual_type != NULL_TREE)
6812 if (orig_qual_indirect == 0)
6813 orig_qual_type = TREE_TYPE (orig_qual_type);
6814 else
6815 orig_qual_indirect--;
6817 if (type_quals)
6818 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6819 orig_qual_indirect);
6820 type = c_build_pointer_type (type);
6821 type_quals = array_ptr_quals;
6822 if (type_quals)
6823 type = c_build_qualified_type (type, type_quals);
6825 /* We don't yet implement attributes in this context. */
6826 if (array_ptr_attrs != NULL_TREE)
6827 warning_at (loc, OPT_Wattributes,
6828 "attributes in parameter array declarator ignored");
6830 size_varies = false;
6831 array_parameter_p = true;
6833 else if (TREE_CODE (type) == FUNCTION_TYPE)
6835 if (type_quals & TYPE_QUAL_ATOMIC)
6837 error_at (loc,
6838 "%<_Atomic%>-qualified function type");
6839 type_quals &= ~TYPE_QUAL_ATOMIC;
6841 else if (type_quals)
6842 pedwarn (loc, OPT_Wpedantic,
6843 "ISO C forbids qualified function types");
6844 if (type_quals)
6845 type = c_build_qualified_type (type, type_quals);
6846 type = c_build_pointer_type (type);
6847 type_quals = TYPE_UNQUALIFIED;
6849 else if (type_quals)
6850 type = c_build_qualified_type (type, type_quals);
6852 decl = build_decl (declarator->id_loc,
6853 PARM_DECL, declarator->u.id, type);
6854 if (size_varies)
6855 C_DECL_VARIABLE_SIZE (decl) = 1;
6856 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6858 /* Compute the type actually passed in the parmlist,
6859 for the case where there is no prototype.
6860 (For example, shorts and chars are passed as ints.)
6861 When there is a prototype, this is overridden later. */
6863 if (type == error_mark_node)
6864 promoted_type = type;
6865 else
6866 promoted_type = c_type_promotes_to (type);
6868 DECL_ARG_TYPE (decl) = promoted_type;
6869 if (declspecs->inline_p)
6870 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6871 if (declspecs->noreturn_p)
6872 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6874 else if (decl_context == FIELD)
6876 /* Note that the grammar rejects storage classes in typenames
6877 and fields. */
6878 gcc_assert (storage_class == csc_none && !threadp
6879 && !declspecs->inline_p && !declspecs->noreturn_p);
6881 /* Structure field. It may not be a function. */
6883 if (TREE_CODE (type) == FUNCTION_TYPE)
6885 error_at (loc, "field %qE declared as a function", name);
6886 type = build_pointer_type (type);
6888 else if (TREE_CODE (type) != ERROR_MARK
6889 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6891 if (name)
6892 error_at (loc, "field %qE has incomplete type", name);
6893 else
6894 error_at (loc, "unnamed field has incomplete type");
6895 type = error_mark_node;
6897 else if (TREE_CODE (type) == ARRAY_TYPE
6898 && TYPE_DOMAIN (type) == NULL_TREE)
6900 /* We have a flexible array member through a typedef.
6901 Set suitable range. Whether this is a correct position
6902 for a flexible array member will be determined elsewhere. */
6903 if (!in_system_header_at (input_location))
6904 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6905 "support flexible array members");
6906 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6907 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6908 NULL_TREE);
6909 if (orig_qual_indirect == 0)
6910 orig_qual_type = NULL_TREE;
6912 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6913 orig_qual_indirect);
6914 decl = build_decl (declarator->id_loc,
6915 FIELD_DECL, declarator->u.id, type);
6916 DECL_NONADDRESSABLE_P (decl) = bitfield;
6917 if (bitfield && !declarator->u.id)
6919 TREE_NO_WARNING (decl) = 1;
6920 DECL_PADDING_P (decl) = 1;
6923 if (size_varies)
6924 C_DECL_VARIABLE_SIZE (decl) = 1;
6926 else if (TREE_CODE (type) == FUNCTION_TYPE)
6928 if (storage_class == csc_register || threadp)
6930 error_at (loc, "invalid storage class for function %qE", name);
6932 else if (current_scope != file_scope)
6934 /* Function declaration not at file scope. Storage
6935 classes other than `extern' are not allowed, C99
6936 6.7.1p5, and `extern' makes no difference. However,
6937 GCC allows 'auto', perhaps with 'inline', to support
6938 nested functions. */
6939 if (storage_class == csc_auto)
6940 pedwarn (loc, OPT_Wpedantic,
6941 "invalid storage class for function %qE", name);
6942 else if (storage_class == csc_static)
6944 error_at (loc, "invalid storage class for function %qE", name);
6945 if (funcdef_flag)
6946 storage_class = declspecs->storage_class = csc_none;
6947 else
6948 return NULL_TREE;
6952 decl = build_decl (declarator->id_loc,
6953 FUNCTION_DECL, declarator->u.id, type);
6954 decl = build_decl_attribute_variant (decl, decl_attr);
6956 if (type_quals & TYPE_QUAL_ATOMIC)
6958 error_at (loc,
6959 "%<_Atomic%>-qualified function type");
6960 type_quals &= ~TYPE_QUAL_ATOMIC;
6962 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6963 pedwarn (loc, OPT_Wpedantic,
6964 "ISO C forbids qualified function types");
6966 /* Every function declaration is an external reference
6967 (DECL_EXTERNAL) except for those which are not at file
6968 scope and are explicitly declared "auto". This is
6969 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6970 GCC to signify a forward declaration of a nested function. */
6971 if (storage_class == csc_auto && current_scope != file_scope)
6972 DECL_EXTERNAL (decl) = 0;
6973 /* In C99, a function which is declared 'inline' with 'extern'
6974 is not an external reference (which is confusing). It
6975 means that the later definition of the function must be output
6976 in this file, C99 6.7.4p6. In GNU C89, a function declared
6977 'extern inline' is an external reference. */
6978 else if (declspecs->inline_p && storage_class != csc_static)
6979 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6980 == flag_gnu89_inline);
6981 else
6982 DECL_EXTERNAL (decl) = !initialized;
6984 /* Record absence of global scope for `static' or `auto'. */
6985 TREE_PUBLIC (decl)
6986 = !(storage_class == csc_static || storage_class == csc_auto);
6988 /* For a function definition, record the argument information
6989 block where store_parm_decls will look for it. */
6990 if (funcdef_flag)
6991 current_function_arg_info = arg_info;
6993 if (declspecs->default_int_p)
6994 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6996 /* Record presence of `inline' and `_Noreturn', if it is
6997 reasonable. */
6998 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
7000 if (declspecs->inline_p)
7001 pedwarn (loc, 0, "cannot inline function %<main%>");
7002 if (declspecs->noreturn_p)
7003 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7005 else
7007 if (declspecs->inline_p)
7008 /* Record that the function is declared `inline'. */
7009 DECL_DECLARED_INLINE_P (decl) = 1;
7010 if (declspecs->noreturn_p)
7012 if (flag_isoc99)
7013 pedwarn_c99 (loc, OPT_Wpedantic,
7014 "ISO C99 does not support %<_Noreturn%>");
7015 else
7016 pedwarn_c99 (loc, OPT_Wpedantic,
7017 "ISO C90 does not support %<_Noreturn%>");
7018 TREE_THIS_VOLATILE (decl) = 1;
7022 else
7024 /* It's a variable. */
7025 /* An uninitialized decl with `extern' is a reference. */
7026 int extern_ref = !initialized && storage_class == csc_extern;
7028 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7029 orig_qual_indirect);
7031 /* C99 6.2.2p7: It is invalid (compile-time undefined
7032 behavior) to create an 'extern' declaration for a
7033 variable if there is a global declaration that is
7034 'static' and the global declaration is not visible.
7035 (If the static declaration _is_ currently visible,
7036 the 'extern' declaration is taken to refer to that decl.) */
7037 if (extern_ref && current_scope != file_scope)
7039 tree global_decl = identifier_global_value (declarator->u.id);
7040 tree visible_decl = lookup_name (declarator->u.id);
7042 if (global_decl
7043 && global_decl != visible_decl
7044 && VAR_P (global_decl)
7045 && !TREE_PUBLIC (global_decl))
7046 error_at (loc, "variable previously declared %<static%> "
7047 "redeclared %<extern%>");
7050 decl = build_decl (declarator->id_loc,
7051 VAR_DECL, declarator->u.id, type);
7052 if (size_varies)
7053 C_DECL_VARIABLE_SIZE (decl) = 1;
7055 if (declspecs->inline_p)
7056 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7057 if (declspecs->noreturn_p)
7058 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7060 /* At file scope, an initialized extern declaration may follow
7061 a static declaration. In that case, DECL_EXTERNAL will be
7062 reset later in start_decl. */
7063 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7065 /* At file scope, the presence of a `static' or `register' storage
7066 class specifier, or the absence of all storage class specifiers
7067 makes this declaration a definition (perhaps tentative). Also,
7068 the absence of `static' makes it public. */
7069 if (current_scope == file_scope)
7071 TREE_PUBLIC (decl) = storage_class != csc_static;
7072 TREE_STATIC (decl) = !extern_ref;
7074 /* Not at file scope, only `static' makes a static definition. */
7075 else
7077 TREE_STATIC (decl) = (storage_class == csc_static);
7078 TREE_PUBLIC (decl) = extern_ref;
7081 if (threadp)
7082 set_decl_tls_model (decl, decl_default_tls_model (decl));
7085 if ((storage_class == csc_extern
7086 || (storage_class == csc_none
7087 && TREE_CODE (type) == FUNCTION_TYPE
7088 && !funcdef_flag))
7089 && variably_modified_type_p (type, NULL_TREE))
7091 /* C99 6.7.5.2p2 */
7092 if (TREE_CODE (type) == FUNCTION_TYPE)
7093 error_at (loc, "non-nested function with variably modified type");
7094 else
7095 error_at (loc, "object with variably modified type must have "
7096 "no linkage");
7099 /* Record `register' declaration for warnings on &
7100 and in case doing stupid register allocation. */
7102 if (storage_class == csc_register)
7104 C_DECL_REGISTER (decl) = 1;
7105 DECL_REGISTER (decl) = 1;
7108 /* Record constancy and volatility. */
7109 c_apply_type_quals_to_decl (type_quals, decl);
7111 /* Apply _Alignas specifiers. */
7112 if (alignas_align)
7114 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7115 DECL_USER_ALIGN (decl) = 1;
7118 /* If a type has volatile components, it should be stored in memory.
7119 Otherwise, the fact that those components are volatile
7120 will be ignored, and would even crash the compiler.
7121 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7122 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7123 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7124 || TREE_CODE (decl) == RESULT_DECL))
7126 /* It is not an error for a structure with volatile fields to
7127 be declared register, but reset DECL_REGISTER since it
7128 cannot actually go in a register. */
7129 int was_reg = C_DECL_REGISTER (decl);
7130 C_DECL_REGISTER (decl) = 0;
7131 DECL_REGISTER (decl) = 0;
7132 c_mark_addressable (decl);
7133 C_DECL_REGISTER (decl) = was_reg;
7136 /* This is the earliest point at which we might know the assembler
7137 name of a variable. Thus, if it's known before this, die horribly. */
7138 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7139 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7141 if (warn_cxx_compat
7142 && VAR_P (decl)
7143 && TREE_PUBLIC (decl)
7144 && TREE_STATIC (decl)
7145 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7146 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7147 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7148 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7149 ("non-local variable %qD with anonymous type is "
7150 "questionable in C++"),
7151 decl);
7153 return decl;
7157 /* Decode the parameter-list info for a function type or function definition.
7158 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7159 if there is an identifier list instead of a parameter decl list).
7160 These two functions are separate because when a function returns
7161 or receives functions then each is called multiple times but the order
7162 of calls is different. The last call to `grokparms' is always the one
7163 that contains the formal parameter names of a function definition.
7165 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7167 FUNCDEF_FLAG is true for a function definition, false for
7168 a mere declaration. A nonempty identifier-list gets an error message
7169 when FUNCDEF_FLAG is false. */
7171 static tree
7172 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7174 tree arg_types = arg_info->types;
7176 if (funcdef_flag && arg_info->had_vla_unspec)
7178 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7179 /* C99 6.7.5.2p4 */
7180 error ("%<[*]%> not allowed in other than function prototype scope");
7183 if (arg_types == NULL_TREE && !funcdef_flag
7184 && !in_system_header_at (input_location))
7185 warning (OPT_Wstrict_prototypes,
7186 "function declaration isn%'t a prototype");
7188 if (arg_types == error_mark_node)
7189 /* Don't set TYPE_ARG_TYPES in this case. */
7190 return NULL_TREE;
7192 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7194 if (!funcdef_flag)
7196 pedwarn (input_location, 0, "parameter names (without types) in "
7197 "function declaration");
7198 arg_info->parms = NULL_TREE;
7200 else
7201 arg_info->parms = arg_info->types;
7203 arg_info->types = NULL_TREE;
7204 return NULL_TREE;
7206 else
7208 tree parm, type, typelt;
7209 unsigned int parmno;
7211 /* If there is a parameter of incomplete type in a definition,
7212 this is an error. In a declaration this is valid, and a
7213 struct or union type may be completed later, before any calls
7214 or definition of the function. In the case where the tag was
7215 first declared within the parameter list, a warning has
7216 already been given. If a parameter has void type, then
7217 however the function cannot be defined or called, so
7218 warn. */
7220 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7221 parm;
7222 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7224 type = TREE_VALUE (typelt);
7225 if (type == error_mark_node)
7226 continue;
7228 if (!COMPLETE_TYPE_P (type))
7230 if (funcdef_flag)
7232 if (DECL_NAME (parm))
7233 error_at (input_location,
7234 "parameter %u (%q+D) has incomplete type",
7235 parmno, parm);
7236 else
7237 error_at (DECL_SOURCE_LOCATION (parm),
7238 "parameter %u has incomplete type",
7239 parmno);
7241 TREE_VALUE (typelt) = error_mark_node;
7242 TREE_TYPE (parm) = error_mark_node;
7243 arg_types = NULL_TREE;
7245 else if (VOID_TYPE_P (type))
7247 if (DECL_NAME (parm))
7248 warning_at (input_location, 0,
7249 "parameter %u (%q+D) has void type",
7250 parmno, parm);
7251 else
7252 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7253 "parameter %u has void type",
7254 parmno);
7258 if (DECL_NAME (parm) && TREE_USED (parm))
7259 warn_if_shadowing (parm);
7261 return arg_types;
7265 /* Allocate and initialize a c_arg_info structure from the parser's
7266 obstack. */
7268 struct c_arg_info *
7269 build_arg_info (void)
7271 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7272 ret->parms = NULL_TREE;
7273 ret->tags = NULL;
7274 ret->types = NULL_TREE;
7275 ret->others = NULL_TREE;
7276 ret->pending_sizes = NULL;
7277 ret->had_vla_unspec = 0;
7278 return ret;
7281 /* Take apart the current scope and return a c_arg_info structure with
7282 info on a parameter list just parsed.
7284 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7286 ELLIPSIS being true means the argument list ended in '...' so don't
7287 append a sentinel (void_list_node) to the end of the type-list.
7289 EXPR is NULL or an expression that needs to be evaluated for the
7290 side effects of array size expressions in the parameters. */
7292 struct c_arg_info *
7293 get_parm_info (bool ellipsis, tree expr)
7295 struct c_binding *b = current_scope->bindings;
7296 struct c_arg_info *arg_info = build_arg_info ();
7298 tree parms = NULL_TREE;
7299 vec<c_arg_tag, va_gc> *tags = NULL;
7300 tree types = NULL_TREE;
7301 tree others = NULL_TREE;
7303 bool gave_void_only_once_err = false;
7305 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7307 /* The bindings in this scope must not get put into a block.
7308 We will take care of deleting the binding nodes. */
7309 current_scope->bindings = 0;
7311 /* This function is only called if there was *something* on the
7312 parameter list. */
7313 gcc_assert (b);
7315 /* A parameter list consisting solely of 'void' indicates that the
7316 function takes no arguments. But if the 'void' is qualified
7317 (by 'const' or 'volatile'), or has a storage class specifier
7318 ('register'), then the behavior is undefined; issue an error.
7319 Typedefs for 'void' are OK (see DR#157). */
7320 if (b->prev == 0 /* one binding */
7321 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7322 && !DECL_NAME (b->decl) /* anonymous */
7323 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7325 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7326 || C_DECL_REGISTER (b->decl))
7327 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7329 /* There cannot be an ellipsis. */
7330 if (ellipsis)
7331 error_at (b->locus, "%<void%> must be the only parameter");
7333 arg_info->types = void_list_node;
7334 return arg_info;
7337 if (!ellipsis)
7338 types = void_list_node;
7340 /* Break up the bindings list into parms, tags, types, and others;
7341 apply sanity checks; purge the name-to-decl bindings. */
7342 while (b)
7344 tree decl = b->decl;
7345 tree type = TREE_TYPE (decl);
7346 c_arg_tag tag;
7347 const char *keyword;
7349 switch (TREE_CODE (decl))
7351 case PARM_DECL:
7352 if (b->id)
7354 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7355 I_SYMBOL_BINDING (b->id) = b->shadowed;
7358 /* Check for forward decls that never got their actual decl. */
7359 if (TREE_ASM_WRITTEN (decl))
7360 error_at (b->locus,
7361 "parameter %q+D has just a forward declaration", decl);
7362 /* Check for (..., void, ...) and issue an error. */
7363 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7365 if (!gave_void_only_once_err)
7367 error_at (b->locus, "%<void%> must be the only parameter");
7368 gave_void_only_once_err = true;
7371 else
7373 /* Valid parameter, add it to the list. */
7374 DECL_CHAIN (decl) = parms;
7375 parms = decl;
7377 /* Since there is a prototype, args are passed in their
7378 declared types. The back end may override this later. */
7379 DECL_ARG_TYPE (decl) = type;
7380 types = tree_cons (0, type, types);
7382 break;
7384 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7385 case UNION_TYPE: keyword = "union"; goto tag;
7386 case RECORD_TYPE: keyword = "struct"; goto tag;
7387 tag:
7388 /* Types may not have tag-names, in which case the type
7389 appears in the bindings list with b->id NULL. */
7390 if (b->id)
7392 gcc_assert (I_TAG_BINDING (b->id) == b);
7393 I_TAG_BINDING (b->id) = b->shadowed;
7396 /* Warn about any struct, union or enum tags defined in a
7397 parameter list. The scope of such types is limited to
7398 the parameter list, which is rarely if ever desirable
7399 (it's impossible to call such a function with type-
7400 correct arguments). An anonymous union parm type is
7401 meaningful as a GNU extension, so don't warn for that. */
7402 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7404 if (b->id)
7405 /* The %s will be one of 'struct', 'union', or 'enum'. */
7406 warning_at (b->locus, 0,
7407 "%<%s %E%> declared inside parameter list"
7408 " will not be visible outside of this definition or"
7409 " declaration", keyword, b->id);
7410 else
7411 /* The %s will be one of 'struct', 'union', or 'enum'. */
7412 warning_at (b->locus, 0,
7413 "anonymous %s declared inside parameter list"
7414 " will not be visible outside of this definition or"
7415 " declaration", keyword);
7418 tag.id = b->id;
7419 tag.type = decl;
7420 vec_safe_push (tags, tag);
7421 break;
7423 case FUNCTION_DECL:
7424 /* FUNCTION_DECLs appear when there is an implicit function
7425 declaration in the parameter list. */
7426 gcc_assert (b->nested || seen_error ());
7427 goto set_shadowed;
7429 case CONST_DECL:
7430 case TYPE_DECL:
7431 /* CONST_DECLs appear here when we have an embedded enum,
7432 and TYPE_DECLs appear here when we have an embedded struct
7433 or union. No warnings for this - we already warned about the
7434 type itself. */
7436 /* When we reinsert this decl in the function body, we need
7437 to reconstruct whether it was marked as nested. */
7438 gcc_assert (!b->nested);
7439 DECL_CHAIN (decl) = others;
7440 others = decl;
7441 /* fall through */
7443 case ERROR_MARK:
7444 set_shadowed:
7445 /* error_mark_node appears here when we have an undeclared
7446 variable. Just throw it away. */
7447 if (b->id)
7449 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7450 I_SYMBOL_BINDING (b->id) = b->shadowed;
7452 break;
7454 /* Other things that might be encountered. */
7455 case LABEL_DECL:
7456 case VAR_DECL:
7457 default:
7458 gcc_unreachable ();
7461 b = free_binding_and_advance (b);
7464 arg_info->parms = parms;
7465 arg_info->tags = tags;
7466 arg_info->types = types;
7467 arg_info->others = others;
7468 arg_info->pending_sizes = expr;
7469 return arg_info;
7472 /* Get the struct, enum or union (CODE says which) with tag NAME.
7473 Define the tag as a forward-reference with location LOC if it is
7474 not defined. Return a c_typespec structure for the type
7475 specifier. */
7477 struct c_typespec
7478 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7480 struct c_typespec ret;
7481 tree ref;
7482 location_t refloc;
7484 ret.expr = NULL_TREE;
7485 ret.expr_const_operands = true;
7487 /* If a cross reference is requested, look up the type
7488 already defined for this tag and return it. */
7490 ref = lookup_tag (code, name, false, &refloc);
7491 /* If this is the right type of tag, return what we found.
7492 (This reference will be shadowed by shadow_tag later if appropriate.)
7493 If this is the wrong type of tag, do not return it. If it was the
7494 wrong type in the same scope, we will have had an error
7495 message already; if in a different scope and declaring
7496 a name, pending_xref_error will give an error message; but if in a
7497 different scope and not declaring a name, this tag should
7498 shadow the previous declaration of a different type of tag, and
7499 this would not work properly if we return the reference found.
7500 (For example, with "struct foo" in an outer scope, "union foo;"
7501 must shadow that tag with a new one of union type.) */
7502 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7503 if (ref && TREE_CODE (ref) == code)
7505 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7506 && loc != UNKNOWN_LOCATION
7507 && warn_cxx_compat)
7509 switch (code)
7511 case ENUMERAL_TYPE:
7512 warning_at (loc, OPT_Wc___compat,
7513 ("enum type defined in struct or union "
7514 "is not visible in C++"));
7515 inform (refloc, "enum type defined here");
7516 break;
7517 case RECORD_TYPE:
7518 warning_at (loc, OPT_Wc___compat,
7519 ("struct defined in struct or union "
7520 "is not visible in C++"));
7521 inform (refloc, "struct defined here");
7522 break;
7523 case UNION_TYPE:
7524 warning_at (loc, OPT_Wc___compat,
7525 ("union defined in struct or union "
7526 "is not visible in C++"));
7527 inform (refloc, "union defined here");
7528 break;
7529 default:
7530 gcc_unreachable();
7534 ret.spec = ref;
7535 return ret;
7538 /* If no such tag is yet defined, create a forward-reference node
7539 and record it as the "definition".
7540 When a real declaration of this type is found,
7541 the forward-reference will be altered into a real type. */
7543 ref = make_node (code);
7544 if (code == ENUMERAL_TYPE)
7546 /* Give the type a default layout like unsigned int
7547 to avoid crashing if it does not get defined. */
7548 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7549 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7550 TYPE_USER_ALIGN (ref) = 0;
7551 TYPE_UNSIGNED (ref) = 1;
7552 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7553 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7554 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7557 pushtag (loc, name, ref);
7559 ret.spec = ref;
7560 return ret;
7563 /* Get the struct, enum or union (CODE says which) with tag NAME.
7564 Define the tag as a forward-reference if it is not defined.
7565 Return a tree for the type. */
7567 tree
7568 xref_tag (enum tree_code code, tree name)
7570 return parser_xref_tag (input_location, code, name).spec;
7573 /* Make sure that the tag NAME is defined *in the current scope*
7574 at least as a forward reference.
7575 LOC is the location of the struct's definition.
7576 CODE says which kind of tag NAME ought to be.
7578 This stores the current value of the file static STRUCT_PARSE_INFO
7579 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7580 new c_struct_parse_info structure. The old value of
7581 STRUCT_PARSE_INFO is restored in finish_struct. */
7583 tree
7584 start_struct (location_t loc, enum tree_code code, tree name,
7585 struct c_struct_parse_info **enclosing_struct_parse_info)
7587 /* If there is already a tag defined at this scope
7588 (as a forward reference), just return it. */
7590 tree ref = NULL_TREE;
7591 location_t refloc = UNKNOWN_LOCATION;
7593 if (name != NULL_TREE)
7594 ref = lookup_tag (code, name, true, &refloc);
7595 if (ref && TREE_CODE (ref) == code)
7597 if (TYPE_STUB_DECL (ref))
7598 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7600 if (TYPE_SIZE (ref))
7602 if (code == UNION_TYPE)
7603 error_at (loc, "redefinition of %<union %E%>", name);
7604 else
7605 error_at (loc, "redefinition of %<struct %E%>", name);
7606 if (refloc != UNKNOWN_LOCATION)
7607 inform (refloc, "originally defined here");
7608 /* Don't create structures using a name already in use. */
7609 ref = NULL_TREE;
7611 else if (C_TYPE_BEING_DEFINED (ref))
7613 if (code == UNION_TYPE)
7614 error_at (loc, "nested redefinition of %<union %E%>", name);
7615 else
7616 error_at (loc, "nested redefinition of %<struct %E%>", name);
7617 /* Don't bother to report "originally defined here" for a
7618 nested redefinition; the original definition should be
7619 obvious. */
7620 /* Don't create structures that contain themselves. */
7621 ref = NULL_TREE;
7625 /* Otherwise create a forward-reference just so the tag is in scope. */
7627 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7629 ref = make_node (code);
7630 pushtag (loc, name, ref);
7633 C_TYPE_BEING_DEFINED (ref) = 1;
7634 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7635 TYPE_PACKED (v) = flag_pack_struct;
7637 *enclosing_struct_parse_info = struct_parse_info;
7638 struct_parse_info = new c_struct_parse_info ();
7640 /* FIXME: This will issue a warning for a use of a type defined
7641 within a statement expr used within sizeof, et. al. This is not
7642 terribly serious as C++ doesn't permit statement exprs within
7643 sizeof anyhow. */
7644 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7645 warning_at (loc, OPT_Wc___compat,
7646 "defining type in %qs expression is invalid in C++",
7647 (in_sizeof
7648 ? "sizeof"
7649 : (in_typeof ? "typeof" : "alignof")));
7651 return ref;
7654 /* Process the specs, declarator and width (NULL if omitted)
7655 of a structure component, returning a FIELD_DECL node.
7656 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7657 DECL_ATTRS is as for grokdeclarator.
7659 LOC is the location of the structure component.
7661 This is done during the parsing of the struct declaration.
7662 The FIELD_DECL nodes are chained together and the lot of them
7663 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7665 tree
7666 grokfield (location_t loc,
7667 struct c_declarator *declarator, struct c_declspecs *declspecs,
7668 tree width, tree *decl_attrs)
7670 tree value;
7672 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7673 && width == NULL_TREE)
7675 /* This is an unnamed decl.
7677 If we have something of the form "union { list } ;" then this
7678 is the anonymous union extension. Similarly for struct.
7680 If this is something of the form "struct foo;", then
7681 If MS or Plan 9 extensions are enabled, this is handled as
7682 an anonymous struct.
7683 Otherwise this is a forward declaration of a structure tag.
7685 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7686 If foo names a structure or union without a tag, then this
7687 is an anonymous struct (this is permitted by C11).
7688 If MS or Plan 9 extensions are enabled and foo names a
7689 structure, then again this is an anonymous struct.
7690 Otherwise this is an error.
7692 Oh what a horrid tangled web we weave. I wonder if MS consciously
7693 took this from Plan 9 or if it was an accident of implementation
7694 that took root before someone noticed the bug... */
7696 tree type = declspecs->type;
7697 bool ok = false;
7699 if (RECORD_OR_UNION_TYPE_P (type)
7700 && (flag_ms_extensions
7701 || flag_plan9_extensions
7702 || !declspecs->typedef_p))
7704 if (flag_ms_extensions || flag_plan9_extensions)
7705 ok = true;
7706 else if (TYPE_NAME (type) == NULL)
7707 ok = true;
7708 else
7709 ok = false;
7711 if (!ok)
7713 pedwarn (loc, 0, "declaration does not declare anything");
7714 return NULL_TREE;
7716 if (flag_isoc99)
7717 pedwarn_c99 (loc, OPT_Wpedantic,
7718 "ISO C99 doesn%'t support unnamed structs/unions");
7719 else
7720 pedwarn_c99 (loc, OPT_Wpedantic,
7721 "ISO C90 doesn%'t support unnamed structs/unions");
7724 value = grokdeclarator (declarator, declspecs, FIELD, false,
7725 width ? &width : NULL, decl_attrs, NULL, NULL,
7726 DEPRECATED_NORMAL);
7728 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7729 DECL_INITIAL (value) = width;
7730 if (width)
7731 SET_DECL_C_BIT_FIELD (value);
7733 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7735 /* If we currently have a binding for this field, set the
7736 in_struct field in the binding, so that we warn about lookups
7737 which find it. */
7738 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7739 if (b != NULL)
7741 /* If the in_struct field is not yet set, push it on a list
7742 to be cleared when this struct is finished. */
7743 if (!b->in_struct)
7745 struct_parse_info->fields.safe_push (b);
7746 b->in_struct = 1;
7751 return value;
7754 /* Subroutine of detect_field_duplicates: return whether X and Y,
7755 which are both fields in the same struct, have duplicate field
7756 names. */
7758 static bool
7759 is_duplicate_field (tree x, tree y)
7761 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7762 return true;
7764 /* When using -fplan9-extensions, an anonymous field whose name is a
7765 typedef can duplicate a field name. */
7766 if (flag_plan9_extensions
7767 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7769 tree xt, xn, yt, yn;
7771 xt = TREE_TYPE (x);
7772 if (DECL_NAME (x) != NULL_TREE)
7773 xn = DECL_NAME (x);
7774 else if (RECORD_OR_UNION_TYPE_P (xt)
7775 && TYPE_NAME (xt) != NULL_TREE
7776 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7777 xn = DECL_NAME (TYPE_NAME (xt));
7778 else
7779 xn = NULL_TREE;
7781 yt = TREE_TYPE (y);
7782 if (DECL_NAME (y) != NULL_TREE)
7783 yn = DECL_NAME (y);
7784 else if (RECORD_OR_UNION_TYPE_P (yt)
7785 && TYPE_NAME (yt) != NULL_TREE
7786 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7787 yn = DECL_NAME (TYPE_NAME (yt));
7788 else
7789 yn = NULL_TREE;
7791 if (xn != NULL_TREE && xn == yn)
7792 return true;
7795 return false;
7798 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7799 to HTAB, giving errors for any duplicates. */
7801 static void
7802 detect_field_duplicates_hash (tree fieldlist,
7803 hash_table<nofree_ptr_hash <tree_node> > *htab)
7805 tree x, y;
7806 tree_node **slot;
7808 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7809 if ((y = DECL_NAME (x)) != NULL_TREE)
7811 slot = htab->find_slot (y, INSERT);
7812 if (*slot)
7814 error ("duplicate member %q+D", x);
7815 DECL_NAME (x) = NULL_TREE;
7817 *slot = y;
7819 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7821 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7823 /* When using -fplan9-extensions, an anonymous field whose
7824 name is a typedef can duplicate a field name. */
7825 if (flag_plan9_extensions
7826 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7827 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7829 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7830 slot = htab->find_slot (xn, INSERT);
7831 if (*slot)
7832 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7833 *slot = xn;
7838 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7839 the list such that this does not present a problem later. */
7841 static void
7842 detect_field_duplicates (tree fieldlist)
7844 tree x, y;
7845 int timeout = 10;
7847 /* If the struct is the list of instance variables of an Objective-C
7848 class, then we need to check all the instance variables of
7849 superclasses when checking for duplicates (since you can't have
7850 an instance variable in a subclass with the same name as an
7851 instance variable in a superclass). We pass on this job to the
7852 Objective-C compiler. objc_detect_field_duplicates() will return
7853 false if we are not checking the list of instance variables and
7854 the C frontend should proceed with the standard field duplicate
7855 checks. If we are checking the list of instance variables, the
7856 ObjC frontend will do the check, emit the errors if needed, and
7857 then return true. */
7858 if (c_dialect_objc ())
7859 if (objc_detect_field_duplicates (false))
7860 return;
7862 /* First, see if there are more than "a few" fields.
7863 This is trivially true if there are zero or one fields. */
7864 if (!fieldlist || !DECL_CHAIN (fieldlist))
7865 return;
7866 x = fieldlist;
7867 do {
7868 timeout--;
7869 if (DECL_NAME (x) == NULL_TREE
7870 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7871 timeout = 0;
7872 x = DECL_CHAIN (x);
7873 } while (timeout > 0 && x);
7875 /* If there were "few" fields and no anonymous structures or unions,
7876 avoid the overhead of allocating a hash table. Instead just do
7877 the nested traversal thing. */
7878 if (timeout > 0)
7880 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7881 /* When using -fplan9-extensions, we can have duplicates
7882 between typedef names and fields. */
7883 if (DECL_NAME (x)
7884 || (flag_plan9_extensions
7885 && DECL_NAME (x) == NULL_TREE
7886 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7887 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7888 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7890 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7891 if (is_duplicate_field (y, x))
7893 error ("duplicate member %q+D", x);
7894 DECL_NAME (x) = NULL_TREE;
7898 else
7900 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7901 detect_field_duplicates_hash (fieldlist, &htab);
7905 /* Finish up struct info used by -Wc++-compat. */
7907 static void
7908 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7909 location_t record_loc)
7911 unsigned int ix;
7912 tree x;
7913 struct c_binding *b;
7915 if (fieldlist == NULL_TREE)
7917 if (code == RECORD_TYPE)
7918 warning_at (record_loc, OPT_Wc___compat,
7919 "empty struct has size 0 in C, size 1 in C++");
7920 else
7921 warning_at (record_loc, OPT_Wc___compat,
7922 "empty union has size 0 in C, size 1 in C++");
7925 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7926 the current struct. We do this now at the end of the struct
7927 because the flag is used to issue visibility warnings, and we
7928 only want to issue those warnings if the type is referenced
7929 outside of the struct declaration. */
7930 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7931 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7933 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7934 typedefs used when declaring fields in this struct. If the name
7935 of any of the fields is also a typedef name then the struct would
7936 not parse in C++, because the C++ lookup rules say that the
7937 typedef name would be looked up in the context of the struct, and
7938 would thus be the field rather than the typedef. */
7939 if (!struct_parse_info->typedefs_seen.is_empty ()
7940 && fieldlist != NULL_TREE)
7942 /* Use a hash_set<tree> using the name of the typedef. We can use
7943 a hash_set<tree> because identifiers are interned. */
7944 hash_set<tree> tset;
7946 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7947 tset.add (DECL_NAME (x));
7949 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7951 if (DECL_NAME (x) != NULL_TREE
7952 && tset.contains (DECL_NAME (x)))
7954 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7955 ("using %qD as both field and typedef name is "
7956 "invalid in C++"),
7958 /* FIXME: It would be nice to report the location where
7959 the typedef name is used. */
7964 /* For each field which has a binding and which was not defined in
7965 an enclosing struct, clear the in_struct field. */
7966 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7967 b->in_struct = 0;
7970 /* Function to help qsort sort FIELD_DECLs by name order. */
7972 static int
7973 field_decl_cmp (const void *x_p, const void *y_p)
7975 const tree *const x = (const tree *) x_p;
7976 const tree *const y = (const tree *) y_p;
7978 if (DECL_NAME (*x) == DECL_NAME (*y))
7979 /* A nontype is "greater" than a type. */
7980 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7981 if (DECL_NAME (*x) == NULL_TREE)
7982 return -1;
7983 if (DECL_NAME (*y) == NULL_TREE)
7984 return 1;
7985 if (DECL_NAME (*x) < DECL_NAME (*y))
7986 return -1;
7987 return 1;
7990 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7991 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7992 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7993 ATTRIBUTES are attributes to be applied to the structure.
7995 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7996 the struct was started. */
7998 tree
7999 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8000 struct c_struct_parse_info *enclosing_struct_parse_info)
8002 tree x;
8003 bool toplevel = file_scope == current_scope;
8005 /* If this type was previously laid out as a forward reference,
8006 make sure we lay it out again. */
8008 TYPE_SIZE (t) = NULL_TREE;
8010 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8012 if (pedantic)
8014 for (x = fieldlist; x; x = DECL_CHAIN (x))
8016 if (DECL_NAME (x) != NULL_TREE)
8017 break;
8018 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8019 break;
8022 if (x == NULL_TREE)
8024 if (TREE_CODE (t) == UNION_TYPE)
8026 if (fieldlist)
8027 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8028 else
8029 pedwarn (loc, OPT_Wpedantic, "union has no members");
8031 else
8033 if (fieldlist)
8034 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8035 else
8036 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8041 /* Install struct as DECL_CONTEXT of each field decl.
8042 Also process specified field sizes, found in the DECL_INITIAL,
8043 storing 0 there after the type has been changed to precision equal
8044 to its width, rather than the precision of the specified standard
8045 type. (Correct layout requires the original type to have been preserved
8046 until now.) */
8048 bool saw_named_field = false;
8049 for (x = fieldlist; x; x = DECL_CHAIN (x))
8051 if (TREE_TYPE (x) == error_mark_node)
8052 continue;
8054 DECL_CONTEXT (x) = t;
8056 /* If any field is const, the structure type is pseudo-const. */
8057 if (TREE_READONLY (x))
8058 C_TYPE_FIELDS_READONLY (t) = 1;
8059 else
8061 /* A field that is pseudo-const makes the structure likewise. */
8062 tree t1 = strip_array_types (TREE_TYPE (x));
8063 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8064 C_TYPE_FIELDS_READONLY (t) = 1;
8067 /* Any field that is volatile means variables of this type must be
8068 treated in some ways as volatile. */
8069 if (TREE_THIS_VOLATILE (x))
8070 C_TYPE_FIELDS_VOLATILE (t) = 1;
8072 /* Any field of nominal variable size implies structure is too. */
8073 if (C_DECL_VARIABLE_SIZE (x))
8074 C_TYPE_VARIABLE_SIZE (t) = 1;
8076 if (DECL_C_BIT_FIELD (x))
8078 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8079 DECL_SIZE (x) = bitsize_int (width);
8080 DECL_BIT_FIELD (x) = 1;
8083 if (TYPE_PACKED (t)
8084 && (DECL_BIT_FIELD (x)
8085 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8086 DECL_PACKED (x) = 1;
8088 /* Detect flexible array member in an invalid context. */
8089 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8090 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8091 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8092 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8094 if (TREE_CODE (t) == UNION_TYPE)
8096 error_at (DECL_SOURCE_LOCATION (x),
8097 "flexible array member in union");
8098 TREE_TYPE (x) = error_mark_node;
8100 else if (DECL_CHAIN (x) != NULL_TREE)
8102 error_at (DECL_SOURCE_LOCATION (x),
8103 "flexible array member not at end of struct");
8104 TREE_TYPE (x) = error_mark_node;
8106 else if (!saw_named_field)
8108 error_at (DECL_SOURCE_LOCATION (x),
8109 "flexible array member in a struct with no named "
8110 "members");
8111 TREE_TYPE (x) = error_mark_node;
8115 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8116 && flexible_array_type_p (TREE_TYPE (x)))
8117 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8118 "invalid use of structure with flexible array member");
8120 if (DECL_NAME (x)
8121 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8122 saw_named_field = true;
8125 detect_field_duplicates (fieldlist);
8127 /* Now we have the nearly final fieldlist. Record it,
8128 then lay out the structure or union (including the fields). */
8130 TYPE_FIELDS (t) = fieldlist;
8132 maybe_apply_pragma_scalar_storage_order (t);
8134 layout_type (t);
8136 if (TYPE_SIZE_UNIT (t)
8137 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8138 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8139 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8140 error ("type %qT is too large", t);
8142 /* Give bit-fields their proper types and rewrite the type of array fields
8143 with scalar component if the enclosing type has reverse storage order. */
8144 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8146 if (TREE_CODE (field) == FIELD_DECL
8147 && DECL_INITIAL (field)
8148 && TREE_TYPE (field) != error_mark_node)
8150 unsigned HOST_WIDE_INT width
8151 = tree_to_uhwi (DECL_INITIAL (field));
8152 tree type = TREE_TYPE (field);
8153 if (width != TYPE_PRECISION (type))
8155 TREE_TYPE (field)
8156 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8157 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8159 DECL_INITIAL (field) = NULL_TREE;
8161 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8162 && TREE_CODE (field) == FIELD_DECL
8163 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8165 tree ftype = TREE_TYPE (field);
8166 tree ctype = strip_array_types (ftype);
8167 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8169 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8170 tree *typep = &fmain_type;
8171 do {
8172 *typep = build_distinct_type_copy (*typep);
8173 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8174 typep = &TREE_TYPE (*typep);
8175 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8176 TREE_TYPE (field)
8177 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8182 /* Now we have the truly final field list.
8183 Store it in this type and in the variants. */
8185 TYPE_FIELDS (t) = fieldlist;
8187 /* If there are lots of fields, sort so we can look through them fast.
8188 We arbitrarily consider 16 or more elts to be "a lot". */
8191 int len = 0;
8193 for (x = fieldlist; x; x = DECL_CHAIN (x))
8195 if (len > 15 || DECL_NAME (x) == NULL)
8196 break;
8197 len += 1;
8200 if (len > 15)
8202 tree *field_array;
8203 struct lang_type *space;
8204 struct sorted_fields_type *space2;
8206 len += list_length (x);
8208 /* Use the same allocation policy here that make_node uses, to
8209 ensure that this lives as long as the rest of the struct decl.
8210 All decls in an inline function need to be saved. */
8212 space = ggc_cleared_alloc<struct lang_type> ();
8213 space2 = (sorted_fields_type *) ggc_internal_alloc
8214 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8216 len = 0;
8217 space->s = space2;
8218 field_array = &space2->elts[0];
8219 for (x = fieldlist; x; x = DECL_CHAIN (x))
8221 field_array[len++] = x;
8223 /* If there is anonymous struct or union, break out of the loop. */
8224 if (DECL_NAME (x) == NULL)
8225 break;
8227 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8228 if (x == NULL)
8230 TYPE_LANG_SPECIFIC (t) = space;
8231 TYPE_LANG_SPECIFIC (t)->s->len = len;
8232 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8233 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8238 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8239 in dwarf2out via rest_of_decl_compilation below and means
8240 something totally different. Since we will be clearing
8241 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8242 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8243 C_TYPE_INCOMPLETE_VARS should use some language specific
8244 node. */
8245 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8246 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8248 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8249 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8250 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8251 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8252 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8253 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8256 /* If this was supposed to be a transparent union, but we can't
8257 make it one, warn and turn off the flag. */
8258 if (TREE_CODE (t) == UNION_TYPE
8259 && TYPE_TRANSPARENT_AGGR (t)
8260 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8262 TYPE_TRANSPARENT_AGGR (t) = 0;
8263 warning_at (loc, 0, "union cannot be made transparent");
8266 /* Update type location to the one of the definition, instead of e.g.
8267 a forward declaration. */
8268 if (TYPE_STUB_DECL (t))
8269 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8271 /* Finish debugging output for this type. */
8272 rest_of_type_compilation (t, toplevel);
8274 /* If this structure or union completes the type of any previous
8275 variable declaration, lay it out and output its rtl. */
8276 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8278 tree decl = TREE_VALUE (x);
8279 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8280 layout_array_type (TREE_TYPE (decl));
8281 if (TREE_CODE (decl) != TYPE_DECL)
8283 layout_decl (decl, 0);
8284 if (c_dialect_objc ())
8285 objc_check_decl (decl);
8286 rest_of_decl_compilation (decl, toplevel, 0);
8290 /* If we're inside a function proper, i.e. not file-scope and not still
8291 parsing parameters, then arrange for the size of a variable sized type
8292 to be bound now. */
8293 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8294 add_stmt (build_stmt (loc,
8295 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8297 if (warn_cxx_compat)
8298 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8300 delete struct_parse_info;
8302 struct_parse_info = enclosing_struct_parse_info;
8304 /* If this struct is defined inside a struct, add it to
8305 struct_types. */
8306 if (warn_cxx_compat
8307 && struct_parse_info != NULL
8308 && !in_sizeof && !in_typeof && !in_alignof)
8309 struct_parse_info->struct_types.safe_push (t);
8311 return t;
8314 static struct {
8315 gt_pointer_operator new_value;
8316 void *cookie;
8317 } resort_data;
8319 /* This routine compares two fields like field_decl_cmp but using the
8320 pointer operator in resort_data. */
8322 static int
8323 resort_field_decl_cmp (const void *x_p, const void *y_p)
8325 const tree *const x = (const tree *) x_p;
8326 const tree *const y = (const tree *) y_p;
8328 if (DECL_NAME (*x) == DECL_NAME (*y))
8329 /* A nontype is "greater" than a type. */
8330 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8331 if (DECL_NAME (*x) == NULL_TREE)
8332 return -1;
8333 if (DECL_NAME (*y) == NULL_TREE)
8334 return 1;
8336 tree d1 = DECL_NAME (*x);
8337 tree d2 = DECL_NAME (*y);
8338 resort_data.new_value (&d1, resort_data.cookie);
8339 resort_data.new_value (&d2, resort_data.cookie);
8340 if (d1 < d2)
8341 return -1;
8343 return 1;
8346 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8348 void
8349 resort_sorted_fields (void *obj,
8350 void * ARG_UNUSED (orig_obj),
8351 gt_pointer_operator new_value,
8352 void *cookie)
8354 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8355 resort_data.new_value = new_value;
8356 resort_data.cookie = cookie;
8357 qsort (&sf->elts[0], sf->len, sizeof (tree),
8358 resort_field_decl_cmp);
8361 /* Lay out the type T, and its element type, and so on. */
8363 static void
8364 layout_array_type (tree t)
8366 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8367 layout_array_type (TREE_TYPE (t));
8368 layout_type (t);
8371 /* Begin compiling the definition of an enumeration type.
8372 NAME is its name (or null if anonymous).
8373 LOC is the enum's location.
8374 Returns the type object, as yet incomplete.
8375 Also records info about it so that build_enumerator
8376 may be used to declare the individual values as they are read. */
8378 tree
8379 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8381 tree enumtype = NULL_TREE;
8382 location_t enumloc = UNKNOWN_LOCATION;
8384 /* If this is the real definition for a previous forward reference,
8385 fill in the contents in the same object that used to be the
8386 forward reference. */
8388 if (name != NULL_TREE)
8389 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8391 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8393 enumtype = make_node (ENUMERAL_TYPE);
8394 pushtag (loc, name, enumtype);
8396 /* Update type location to the one of the definition, instead of e.g.
8397 a forward declaration. */
8398 else if (TYPE_STUB_DECL (enumtype))
8400 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8401 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8404 if (C_TYPE_BEING_DEFINED (enumtype))
8405 error_at (loc, "nested redefinition of %<enum %E%>", name);
8407 C_TYPE_BEING_DEFINED (enumtype) = 1;
8409 if (TYPE_VALUES (enumtype) != NULL_TREE)
8411 /* This enum is a named one that has been declared already. */
8412 error_at (loc, "redeclaration of %<enum %E%>", name);
8413 if (enumloc != UNKNOWN_LOCATION)
8414 inform (enumloc, "originally defined here");
8416 /* Completely replace its old definition.
8417 The old enumerators remain defined, however. */
8418 TYPE_VALUES (enumtype) = NULL_TREE;
8421 the_enum->enum_next_value = integer_zero_node;
8422 the_enum->enum_overflow = 0;
8424 if (flag_short_enums)
8425 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8426 TYPE_PACKED (v) = 1;
8428 /* FIXME: This will issue a warning for a use of a type defined
8429 within sizeof in a statement expr. This is not terribly serious
8430 as C++ doesn't permit statement exprs within sizeof anyhow. */
8431 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8432 warning_at (loc, OPT_Wc___compat,
8433 "defining type in %qs expression is invalid in C++",
8434 (in_sizeof
8435 ? "sizeof"
8436 : (in_typeof ? "typeof" : "alignof")));
8438 return enumtype;
8441 /* After processing and defining all the values of an enumeration type,
8442 install their decls in the enumeration type and finish it off.
8443 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8444 and ATTRIBUTES are the specified attributes.
8445 Returns ENUMTYPE. */
8447 tree
8448 finish_enum (tree enumtype, tree values, tree attributes)
8450 tree pair, tem;
8451 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8452 int precision;
8453 signop sign;
8454 bool toplevel = (file_scope == current_scope);
8455 struct lang_type *lt;
8457 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8459 /* Calculate the maximum value of any enumerator in this type. */
8461 if (values == error_mark_node)
8462 minnode = maxnode = integer_zero_node;
8463 else
8465 minnode = maxnode = TREE_VALUE (values);
8466 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8468 tree value = TREE_VALUE (pair);
8469 if (tree_int_cst_lt (maxnode, value))
8470 maxnode = value;
8471 if (tree_int_cst_lt (value, minnode))
8472 minnode = value;
8476 /* Construct the final type of this enumeration. It is the same
8477 as one of the integral types - the narrowest one that fits, except
8478 that normally we only go as narrow as int - and signed iff any of
8479 the values are negative. */
8480 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8481 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8482 tree_int_cst_min_precision (maxnode, sign));
8484 /* If the precision of the type was specified with an attribute and it
8485 was too small, give an error. Otherwise, use it. */
8486 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8488 if (precision > TYPE_PRECISION (enumtype))
8490 TYPE_PRECISION (enumtype) = 0;
8491 error ("specified mode too small for enumeral values");
8493 else
8494 precision = TYPE_PRECISION (enumtype);
8496 else
8497 TYPE_PRECISION (enumtype) = 0;
8499 if (TYPE_PACKED (enumtype)
8500 || precision > TYPE_PRECISION (integer_type_node)
8501 || TYPE_PRECISION (enumtype))
8503 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8504 if (tem == NULL)
8506 warning (0, "enumeration values exceed range of largest integer");
8507 tem = long_long_integer_type_node;
8510 else
8511 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8513 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8514 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8515 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8516 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8517 TYPE_SIZE (enumtype) = NULL_TREE;
8518 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8520 layout_type (enumtype);
8522 if (values != error_mark_node)
8524 /* Change the type of the enumerators to be the enum type. We
8525 need to do this irrespective of the size of the enum, for
8526 proper type checking. Replace the DECL_INITIALs of the
8527 enumerators, and the value slots of the list, with copies
8528 that have the enum type; they cannot be modified in place
8529 because they may be shared (e.g. integer_zero_node) Finally,
8530 change the purpose slots to point to the names of the decls. */
8531 for (pair = values; pair; pair = TREE_CHAIN (pair))
8533 tree enu = TREE_PURPOSE (pair);
8534 tree ini = DECL_INITIAL (enu);
8536 TREE_TYPE (enu) = enumtype;
8538 /* The ISO C Standard mandates enumerators to have type int,
8539 even though the underlying type of an enum type is
8540 unspecified. However, GCC allows enumerators of any
8541 integer type as an extensions. build_enumerator()
8542 converts any enumerators that fit in an int to type int,
8543 to avoid promotions to unsigned types when comparing
8544 integers with enumerators that fit in the int range.
8545 When -pedantic is given, build_enumerator() would have
8546 already warned about those that don't fit. Here we
8547 convert the rest to the enumerator type. */
8548 if (TREE_TYPE (ini) != integer_type_node)
8549 ini = convert (enumtype, ini);
8551 DECL_INITIAL (enu) = ini;
8552 TREE_PURPOSE (pair) = DECL_NAME (enu);
8553 TREE_VALUE (pair) = ini;
8556 TYPE_VALUES (enumtype) = values;
8559 /* Record the min/max values so that we can warn about bit-field
8560 enumerations that are too small for the values. */
8561 lt = ggc_cleared_alloc<struct lang_type> ();
8562 lt->enum_min = minnode;
8563 lt->enum_max = maxnode;
8564 TYPE_LANG_SPECIFIC (enumtype) = lt;
8566 /* Fix up all variant types of this enum type. */
8567 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8569 if (tem == enumtype)
8570 continue;
8571 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8572 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8573 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8574 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8575 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8576 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8577 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8578 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8579 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8580 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8581 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8584 /* Finish debugging output for this type. */
8585 rest_of_type_compilation (enumtype, toplevel);
8587 /* If this enum is defined inside a struct, add it to
8588 struct_types. */
8589 if (warn_cxx_compat
8590 && struct_parse_info != NULL
8591 && !in_sizeof && !in_typeof && !in_alignof)
8592 struct_parse_info->struct_types.safe_push (enumtype);
8594 return enumtype;
8597 /* Build and install a CONST_DECL for one value of the
8598 current enumeration type (one that was begun with start_enum).
8599 DECL_LOC is the location of the enumerator.
8600 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8601 Return a tree-list containing the CONST_DECL and its value.
8602 Assignment of sequential values by default is handled here. */
8604 tree
8605 build_enumerator (location_t decl_loc, location_t loc,
8606 struct c_enum_contents *the_enum, tree name, tree value)
8608 tree decl, type;
8610 /* Validate and default VALUE. */
8612 if (value != NULL_TREE)
8614 /* Don't issue more errors for error_mark_node (i.e. an
8615 undeclared identifier) - just ignore the value expression. */
8616 if (value == error_mark_node)
8617 value = NULL_TREE;
8618 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8620 error_at (loc, "enumerator value for %qE is not an integer constant",
8621 name);
8622 value = NULL_TREE;
8624 else
8626 if (TREE_CODE (value) != INTEGER_CST)
8628 value = c_fully_fold (value, false, NULL);
8629 if (TREE_CODE (value) == INTEGER_CST)
8630 pedwarn (loc, OPT_Wpedantic,
8631 "enumerator value for %qE is not an integer "
8632 "constant expression", name);
8634 if (TREE_CODE (value) != INTEGER_CST)
8636 error ("enumerator value for %qE is not an integer constant",
8637 name);
8638 value = NULL_TREE;
8640 else
8642 value = default_conversion (value);
8643 constant_expression_warning (value);
8648 /* Default based on previous value. */
8649 /* It should no longer be possible to have NON_LVALUE_EXPR
8650 in the default. */
8651 if (value == NULL_TREE)
8653 value = the_enum->enum_next_value;
8654 if (the_enum->enum_overflow)
8655 error_at (loc, "overflow in enumeration values");
8657 /* Even though the underlying type of an enum is unspecified, the
8658 type of enumeration constants is explicitly defined as int
8659 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8660 an extension. */
8661 else if (!int_fits_type_p (value, integer_type_node))
8662 pedwarn (loc, OPT_Wpedantic,
8663 "ISO C restricts enumerator values to range of %<int%>");
8665 /* The ISO C Standard mandates enumerators to have type int, even
8666 though the underlying type of an enum type is unspecified.
8667 However, GCC allows enumerators of any integer type as an
8668 extensions. Here we convert any enumerators that fit in an int
8669 to type int, to avoid promotions to unsigned types when comparing
8670 integers with enumerators that fit in the int range. When
8671 -pedantic is given, we would have already warned about those that
8672 don't fit. We have to do this here rather than in finish_enum
8673 because this value may be used to define more enumerators. */
8674 if (int_fits_type_p (value, integer_type_node))
8675 value = convert (integer_type_node, value);
8677 /* Set basis for default for next value. */
8678 the_enum->enum_next_value
8679 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8680 PLUS_EXPR, value, integer_one_node, false);
8681 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8683 /* Now create a declaration for the enum value name. */
8685 type = TREE_TYPE (value);
8686 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8687 TYPE_PRECISION (integer_type_node)),
8688 (TYPE_PRECISION (type)
8689 >= TYPE_PRECISION (integer_type_node)
8690 && TYPE_UNSIGNED (type)));
8692 decl = build_decl (decl_loc, CONST_DECL, name, type);
8693 DECL_INITIAL (decl) = convert (type, value);
8694 pushdecl (decl);
8696 return tree_cons (decl, value, NULL_TREE);
8700 /* Create the FUNCTION_DECL for a function definition.
8701 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8702 the declaration; they describe the function's name and the type it returns,
8703 but twisted together in a fashion that parallels the syntax of C.
8705 This function creates a binding context for the function body
8706 as well as setting up the FUNCTION_DECL in current_function_decl.
8708 Returns true on success. If the DECLARATOR is not suitable for a function
8709 (it defines a datum instead), we return false to report a parse error. */
8711 bool
8712 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8713 tree attributes)
8715 tree decl1, old_decl;
8716 tree restype, resdecl;
8717 location_t loc;
8719 current_function_returns_value = 0; /* Assume, until we see it does. */
8720 current_function_returns_null = 0;
8721 current_function_returns_abnormally = 0;
8722 warn_about_return_type = 0;
8723 c_switch_stack = NULL;
8725 /* Indicate no valid break/continue context by setting these variables
8726 to some non-null, non-label value. We'll notice and emit the proper
8727 error message in c_finish_bc_stmt. */
8728 c_break_label = c_cont_label = size_zero_node;
8730 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8731 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8732 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8734 /* If the declarator is not suitable for a function definition,
8735 cause a syntax error. */
8736 if (decl1 == NULL_TREE
8737 || TREE_CODE (decl1) != FUNCTION_DECL)
8738 return false;
8740 loc = DECL_SOURCE_LOCATION (decl1);
8742 c_decl_attributes (&decl1, attributes, 0);
8744 if (DECL_DECLARED_INLINE_P (decl1)
8745 && DECL_UNINLINABLE (decl1)
8746 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8747 warning_at (loc, OPT_Wattributes,
8748 "inline function %qD given attribute noinline",
8749 decl1);
8751 /* Handle gnu_inline attribute. */
8752 if (declspecs->inline_p
8753 && !flag_gnu89_inline
8754 && TREE_CODE (decl1) == FUNCTION_DECL
8755 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8756 || current_function_decl))
8758 if (declspecs->storage_class != csc_static)
8759 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8762 announce_function (decl1);
8764 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8766 error_at (loc, "return type is an incomplete type");
8767 /* Make it return void instead. */
8768 TREE_TYPE (decl1)
8769 = build_function_type (void_type_node,
8770 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8773 if (warn_about_return_type)
8774 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8775 : (warn_return_type > 0 ? OPT_Wreturn_type
8776 : OPT_Wimplicit_int),
8777 "return type defaults to %<int%>");
8779 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8780 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8781 DECL_INITIAL (decl1) = error_mark_node;
8783 /* A nested function is not global. */
8784 if (current_function_decl != NULL_TREE)
8785 TREE_PUBLIC (decl1) = 0;
8787 /* If this definition isn't a prototype and we had a prototype declaration
8788 before, copy the arg type info from that prototype. */
8789 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8790 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8791 old_decl = NULL_TREE;
8792 current_function_prototype_locus = UNKNOWN_LOCATION;
8793 current_function_prototype_built_in = false;
8794 current_function_prototype_arg_types = NULL_TREE;
8795 if (!prototype_p (TREE_TYPE (decl1)))
8797 if (old_decl != NULL_TREE
8798 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8799 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8800 TREE_TYPE (TREE_TYPE (old_decl))))
8802 if (stdarg_p (TREE_TYPE (old_decl)))
8804 warning_at (loc, 0, "%q+D defined as variadic function "
8805 "without prototype", decl1);
8806 locate_old_decl (old_decl);
8808 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8809 TREE_TYPE (decl1));
8810 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8811 current_function_prototype_built_in
8812 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8813 current_function_prototype_arg_types
8814 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8816 if (TREE_PUBLIC (decl1))
8818 /* If there is an external prototype declaration of this
8819 function, record its location but do not copy information
8820 to this decl. This may be an invisible declaration
8821 (built-in or in a scope which has finished) or simply
8822 have more refined argument types than any declaration
8823 found above. */
8824 struct c_binding *b;
8825 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8826 if (B_IN_SCOPE (b, external_scope))
8827 break;
8828 if (b)
8830 tree ext_decl, ext_type;
8831 ext_decl = b->decl;
8832 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8833 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8834 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8835 TREE_TYPE (ext_type)))
8837 current_function_prototype_locus
8838 = DECL_SOURCE_LOCATION (ext_decl);
8839 current_function_prototype_built_in
8840 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8841 current_function_prototype_arg_types
8842 = TYPE_ARG_TYPES (ext_type);
8848 /* Optionally warn of old-fashioned def with no previous prototype. */
8849 if (warn_strict_prototypes
8850 && old_decl != error_mark_node
8851 && !prototype_p (TREE_TYPE (decl1))
8852 && C_DECL_ISNT_PROTOTYPE (old_decl))
8853 warning_at (loc, OPT_Wstrict_prototypes,
8854 "function declaration isn%'t a prototype");
8855 /* Optionally warn of any global def with no previous prototype. */
8856 else if (warn_missing_prototypes
8857 && old_decl != error_mark_node
8858 && TREE_PUBLIC (decl1)
8859 && !MAIN_NAME_P (DECL_NAME (decl1))
8860 && C_DECL_ISNT_PROTOTYPE (old_decl)
8861 && !DECL_DECLARED_INLINE_P (decl1))
8862 warning_at (loc, OPT_Wmissing_prototypes,
8863 "no previous prototype for %qD", decl1);
8864 /* Optionally warn of any def with no previous prototype
8865 if the function has already been used. */
8866 else if (warn_missing_prototypes
8867 && old_decl != NULL_TREE
8868 && old_decl != error_mark_node
8869 && TREE_USED (old_decl)
8870 && !prototype_p (TREE_TYPE (old_decl)))
8871 warning_at (loc, OPT_Wmissing_prototypes,
8872 "%qD was used with no prototype before its definition", decl1);
8873 /* Optionally warn of any global def with no previous declaration. */
8874 else if (warn_missing_declarations
8875 && TREE_PUBLIC (decl1)
8876 && old_decl == NULL_TREE
8877 && !MAIN_NAME_P (DECL_NAME (decl1))
8878 && !DECL_DECLARED_INLINE_P (decl1))
8879 warning_at (loc, OPT_Wmissing_declarations,
8880 "no previous declaration for %qD",
8881 decl1);
8882 /* Optionally warn of any def with no previous declaration
8883 if the function has already been used. */
8884 else if (warn_missing_declarations
8885 && old_decl != NULL_TREE
8886 && old_decl != error_mark_node
8887 && TREE_USED (old_decl)
8888 && C_DECL_IMPLICIT (old_decl))
8889 warning_at (loc, OPT_Wmissing_declarations,
8890 "%qD was used with no declaration before its definition", decl1);
8892 /* This function exists in static storage.
8893 (This does not mean `static' in the C sense!) */
8894 TREE_STATIC (decl1) = 1;
8896 /* This is the earliest point at which we might know the assembler
8897 name of the function. Thus, if it's set before this, die horribly. */
8898 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8900 /* If #pragma weak was used, mark the decl weak now. */
8901 if (current_scope == file_scope)
8902 maybe_apply_pragma_weak (decl1);
8904 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8905 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8907 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8908 != integer_type_node)
8909 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8910 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8911 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8912 decl1);
8914 check_main_parameter_types (decl1);
8916 if (!TREE_PUBLIC (decl1))
8917 pedwarn (loc, OPT_Wmain,
8918 "%qD is normally a non-static function", decl1);
8921 /* Record the decl so that the function name is defined.
8922 If we already have a decl for this name, and it is a FUNCTION_DECL,
8923 use the old decl. */
8925 current_function_decl = pushdecl (decl1);
8927 push_scope ();
8928 declare_parm_level ();
8930 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8931 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8932 DECL_ARTIFICIAL (resdecl) = 1;
8933 DECL_IGNORED_P (resdecl) = 1;
8934 DECL_RESULT (current_function_decl) = resdecl;
8936 start_fname_decls ();
8938 return true;
8941 /* Subroutine of store_parm_decls which handles new-style function
8942 definitions (prototype format). The parms already have decls, so we
8943 need only record them as in effect and complain if any redundant
8944 old-style parm decls were written. */
8945 static void
8946 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8948 tree decl;
8949 c_arg_tag *tag;
8950 unsigned ix;
8952 if (current_scope->bindings)
8954 error_at (DECL_SOURCE_LOCATION (fndecl),
8955 "old-style parameter declarations in prototyped "
8956 "function definition");
8958 /* Get rid of the old-style declarations. */
8959 pop_scope ();
8960 push_scope ();
8962 /* Don't issue this warning for nested functions, and don't issue this
8963 warning if we got here because ARG_INFO_TYPES was error_mark_node
8964 (this happens when a function definition has just an ellipsis in
8965 its parameter list). */
8966 else if (!in_system_header_at (input_location)
8967 && !current_function_scope
8968 && arg_info->types != error_mark_node)
8969 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8970 "traditional C rejects ISO C style function definitions");
8972 /* Now make all the parameter declarations visible in the function body.
8973 We can bypass most of the grunt work of pushdecl. */
8974 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8976 DECL_CONTEXT (decl) = current_function_decl;
8977 if (DECL_NAME (decl))
8979 bind (DECL_NAME (decl), decl, current_scope,
8980 /*invisible=*/false, /*nested=*/false,
8981 UNKNOWN_LOCATION);
8982 if (!TREE_USED (decl))
8983 warn_if_shadowing (decl);
8985 else
8986 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8989 /* Record the parameter list in the function declaration. */
8990 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8992 /* Now make all the ancillary declarations visible, likewise. */
8993 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8995 DECL_CONTEXT (decl) = current_function_decl;
8996 if (DECL_NAME (decl))
8997 bind (DECL_NAME (decl), decl, current_scope,
8998 /*invisible=*/false,
8999 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9000 UNKNOWN_LOCATION);
9003 /* And all the tag declarations. */
9004 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9005 if (tag->id)
9006 bind (tag->id, tag->type, current_scope,
9007 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9010 /* Subroutine of store_parm_decls which handles old-style function
9011 definitions (separate parameter list and declarations). */
9013 static void
9014 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9016 struct c_binding *b;
9017 tree parm, decl, last;
9018 tree parmids = arg_info->parms;
9019 hash_set<tree> seen_args;
9021 if (!in_system_header_at (input_location))
9022 warning_at (DECL_SOURCE_LOCATION (fndecl),
9023 OPT_Wold_style_definition, "old-style function definition");
9025 /* Match each formal parameter name with its declaration. Save each
9026 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9027 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9029 if (TREE_VALUE (parm) == NULL_TREE)
9031 error_at (DECL_SOURCE_LOCATION (fndecl),
9032 "parameter name missing from parameter list");
9033 TREE_PURPOSE (parm) = NULL_TREE;
9034 continue;
9037 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9038 if (b && B_IN_CURRENT_SCOPE (b))
9040 decl = b->decl;
9041 /* Skip erroneous parameters. */
9042 if (decl == error_mark_node)
9043 continue;
9044 /* If we got something other than a PARM_DECL it is an error. */
9045 if (TREE_CODE (decl) != PARM_DECL)
9047 error_at (DECL_SOURCE_LOCATION (decl),
9048 "%qD declared as a non-parameter", decl);
9049 continue;
9051 /* If the declaration is already marked, we have a duplicate
9052 name. Complain and ignore the duplicate. */
9053 else if (seen_args.contains (decl))
9055 error_at (DECL_SOURCE_LOCATION (decl),
9056 "multiple parameters named %qD", decl);
9057 TREE_PURPOSE (parm) = NULL_TREE;
9058 continue;
9060 /* If the declaration says "void", complain and turn it into
9061 an int. */
9062 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9064 error_at (DECL_SOURCE_LOCATION (decl),
9065 "parameter %qD declared with void type", decl);
9066 TREE_TYPE (decl) = integer_type_node;
9067 DECL_ARG_TYPE (decl) = integer_type_node;
9068 layout_decl (decl, 0);
9070 warn_if_shadowing (decl);
9072 /* If no declaration found, default to int. */
9073 else
9075 /* FIXME diagnostics: This should be the location of the argument,
9076 not the FNDECL. E.g., for an old-style declaration
9078 int f10(v) { blah; }
9080 We should use the location of the V, not the F10.
9081 Unfortunately, the V is an IDENTIFIER_NODE which has no
9082 location. In the future we need locations for c_arg_info
9083 entries.
9085 See gcc.dg/Wshadow-3.c for an example of this problem. */
9086 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9087 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9088 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9089 pushdecl (decl);
9090 warn_if_shadowing (decl);
9092 if (flag_isoc99)
9093 pedwarn (DECL_SOURCE_LOCATION (decl),
9094 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9095 decl);
9096 else
9097 warning_at (DECL_SOURCE_LOCATION (decl),
9098 OPT_Wmissing_parameter_type,
9099 "type of %qD defaults to %<int%>", decl);
9102 TREE_PURPOSE (parm) = decl;
9103 seen_args.add (decl);
9106 /* Now examine the parms chain for incomplete declarations
9107 and declarations with no corresponding names. */
9109 for (b = current_scope->bindings; b; b = b->prev)
9111 parm = b->decl;
9112 if (TREE_CODE (parm) != PARM_DECL)
9113 continue;
9115 if (TREE_TYPE (parm) != error_mark_node
9116 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9118 error_at (DECL_SOURCE_LOCATION (parm),
9119 "parameter %qD has incomplete type", parm);
9120 TREE_TYPE (parm) = error_mark_node;
9123 if (!seen_args.contains (parm))
9125 error_at (DECL_SOURCE_LOCATION (parm),
9126 "declaration for parameter %qD but no such parameter",
9127 parm);
9129 /* Pretend the parameter was not missing.
9130 This gets us to a standard state and minimizes
9131 further error messages. */
9132 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9136 /* Chain the declarations together in the order of the list of
9137 names. Store that chain in the function decl, replacing the
9138 list of names. Update the current scope to match. */
9139 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9141 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9142 if (TREE_PURPOSE (parm))
9143 break;
9144 if (parm && TREE_PURPOSE (parm))
9146 last = TREE_PURPOSE (parm);
9147 DECL_ARGUMENTS (fndecl) = last;
9149 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9150 if (TREE_PURPOSE (parm))
9152 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9153 last = TREE_PURPOSE (parm);
9155 DECL_CHAIN (last) = NULL_TREE;
9158 /* If there was a previous prototype,
9159 set the DECL_ARG_TYPE of each argument according to
9160 the type previously specified, and report any mismatches. */
9162 if (current_function_prototype_arg_types)
9164 tree type;
9165 for (parm = DECL_ARGUMENTS (fndecl),
9166 type = current_function_prototype_arg_types;
9167 parm || (type != NULL_TREE
9168 && TREE_VALUE (type) != error_mark_node
9169 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9170 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9172 if (parm == NULL_TREE
9173 || type == NULL_TREE
9174 || (TREE_VALUE (type) != error_mark_node
9175 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9177 if (current_function_prototype_built_in)
9178 warning_at (DECL_SOURCE_LOCATION (fndecl),
9179 0, "number of arguments doesn%'t match "
9180 "built-in prototype");
9181 else
9183 /* FIXME diagnostics: This should be the location of
9184 FNDECL, but there is bug when a prototype is
9185 declared inside function context, but defined
9186 outside of it (e.g., gcc.dg/pr15698-2.c). In
9187 which case FNDECL gets the location of the
9188 prototype, not the definition. */
9189 error_at (input_location,
9190 "number of arguments doesn%'t match prototype");
9192 error_at (current_function_prototype_locus,
9193 "prototype declaration");
9195 break;
9197 /* Type for passing arg must be consistent with that
9198 declared for the arg. ISO C says we take the unqualified
9199 type for parameters declared with qualified type. */
9200 if (TREE_TYPE (parm) != error_mark_node
9201 && TREE_VALUE (type) != error_mark_node
9202 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9203 != TYPE_ATOMIC (TREE_VALUE (type)))
9204 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9205 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9207 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9208 == TYPE_ATOMIC (TREE_VALUE (type)))
9209 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9210 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9212 /* Adjust argument to match prototype. E.g. a previous
9213 `int foo(float);' prototype causes
9214 `int foo(x) float x; {...}' to be treated like
9215 `int foo(float x) {...}'. This is particularly
9216 useful for argument types like uid_t. */
9217 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9219 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9220 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9221 && (TYPE_PRECISION (TREE_TYPE (parm))
9222 < TYPE_PRECISION (integer_type_node)))
9223 DECL_ARG_TYPE (parm)
9224 = c_type_promotes_to (TREE_TYPE (parm));
9226 /* ??? Is it possible to get here with a
9227 built-in prototype or will it always have
9228 been diagnosed as conflicting with an
9229 old-style definition and discarded? */
9230 if (current_function_prototype_built_in)
9231 warning_at (DECL_SOURCE_LOCATION (parm),
9232 OPT_Wpedantic, "promoted argument %qD "
9233 "doesn%'t match built-in prototype", parm);
9234 else
9236 pedwarn (DECL_SOURCE_LOCATION (parm),
9237 OPT_Wpedantic, "promoted argument %qD "
9238 "doesn%'t match prototype", parm);
9239 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9240 "prototype declaration");
9243 else
9245 if (current_function_prototype_built_in)
9246 warning_at (DECL_SOURCE_LOCATION (parm),
9247 0, "argument %qD doesn%'t match "
9248 "built-in prototype", parm);
9249 else
9251 error_at (DECL_SOURCE_LOCATION (parm),
9252 "argument %qD doesn%'t match prototype", parm);
9253 error_at (current_function_prototype_locus,
9254 "prototype declaration");
9259 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9262 /* Otherwise, create a prototype that would match. */
9264 else
9266 tree actual = NULL_TREE, last = NULL_TREE, type;
9268 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9270 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9271 if (last)
9272 TREE_CHAIN (last) = type;
9273 else
9274 actual = type;
9275 last = type;
9277 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9278 if (last)
9279 TREE_CHAIN (last) = type;
9280 else
9281 actual = type;
9283 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9284 of the type of this function, but we need to avoid having this
9285 affect the types of other similarly-typed functions, so we must
9286 first force the generation of an identical (but separate) type
9287 node for the relevant function type. The new node we create
9288 will be a variant of the main variant of the original function
9289 type. */
9291 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9293 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9297 /* Store parameter declarations passed in ARG_INFO into the current
9298 function declaration. */
9300 void
9301 store_parm_decls_from (struct c_arg_info *arg_info)
9303 current_function_arg_info = arg_info;
9304 store_parm_decls ();
9307 /* Called by walk_tree to look for and update context-less labels. */
9309 static tree
9310 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9312 if (TREE_CODE (*tp) == LABEL_EXPR
9313 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9315 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9316 *walk_subtrees = 0;
9319 return NULL_TREE;
9322 /* Store the parameter declarations into the current function declaration.
9323 This is called after parsing the parameter declarations, before
9324 digesting the body of the function.
9326 For an old-style definition, construct a prototype out of the old-style
9327 parameter declarations and inject it into the function's type. */
9329 void
9330 store_parm_decls (void)
9332 tree fndecl = current_function_decl;
9333 bool proto;
9335 /* The argument information block for FNDECL. */
9336 struct c_arg_info *arg_info = current_function_arg_info;
9337 current_function_arg_info = 0;
9339 /* True if this definition is written with a prototype. Note:
9340 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9341 list in a function definition as equivalent to (void) -- an
9342 empty argument list specifies the function has no parameters,
9343 but only (void) sets up a prototype for future calls. */
9344 proto = arg_info->types != 0;
9346 if (proto)
9347 store_parm_decls_newstyle (fndecl, arg_info);
9348 else
9349 store_parm_decls_oldstyle (fndecl, arg_info);
9351 /* The next call to push_scope will be a function body. */
9353 next_is_function_body = true;
9355 /* Write a record describing this function definition to the prototypes
9356 file (if requested). */
9358 gen_aux_info_record (fndecl, 1, 0, proto);
9360 /* Initialize the RTL code for the function. */
9361 allocate_struct_function (fndecl, false);
9363 if (warn_unused_local_typedefs)
9364 cfun->language = ggc_cleared_alloc<language_function> ();
9366 /* Begin the statement tree for this function. */
9367 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9369 /* ??? Insert the contents of the pending sizes list into the function
9370 to be evaluated. The only reason left to have this is
9371 void foo(int n, int array[n++])
9372 because we throw away the array type in favor of a pointer type, and
9373 thus won't naturally see the SAVE_EXPR containing the increment. All
9374 other pending sizes would be handled by gimplify_parameters. */
9375 if (arg_info->pending_sizes)
9377 /* In very special circumstances, e.g. for code like
9378 _Atomic int i = 5;
9379 void f (int a[i += 2]) {}
9380 we need to execute the atomic assignment on function entry.
9381 But in this case, it is not just a straight store, it has the
9382 op= form, which means that build_atomic_assign has generated
9383 gotos, labels, etc. Because at that time the function decl
9384 for F has not been created yet, those labels do not have any
9385 function context. But we have the fndecl now, so update the
9386 labels accordingly. gimplify_expr would crash otherwise. */
9387 walk_tree_without_duplicates (&arg_info->pending_sizes,
9388 set_labels_context_r, fndecl);
9389 add_stmt (arg_info->pending_sizes);
9393 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9394 c_finish_omp_declare_simd for function prototypes. No diagnostics
9395 should be done. */
9397 void
9398 temp_store_parm_decls (tree fndecl, tree parms)
9400 push_scope ();
9401 for (tree p = parms; p; p = DECL_CHAIN (p))
9403 DECL_CONTEXT (p) = fndecl;
9404 if (DECL_NAME (p))
9405 bind (DECL_NAME (p), p, current_scope,
9406 /*invisible=*/false, /*nested=*/false,
9407 UNKNOWN_LOCATION);
9411 /* Undo what temp_store_parm_decls did. */
9413 void
9414 temp_pop_parm_decls (void)
9416 /* Clear all bindings in this temporary scope, so that
9417 pop_scope doesn't create a BLOCK. */
9418 struct c_binding *b = current_scope->bindings;
9419 current_scope->bindings = NULL;
9420 for (; b; b = free_binding_and_advance (b))
9422 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9423 || b->decl == error_mark_node);
9424 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9425 I_SYMBOL_BINDING (b->id) = b->shadowed;
9426 if (b->shadowed && b->shadowed->u.type)
9427 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9429 pop_scope ();
9433 /* Finish up a function declaration and compile that function
9434 all the way to assembler language output. Then free the storage
9435 for the function definition.
9437 This is called after parsing the body of the function definition. */
9439 void
9440 finish_function (void)
9442 tree fndecl = current_function_decl;
9444 if (c_dialect_objc ())
9445 objc_finish_function ();
9447 if (TREE_CODE (fndecl) == FUNCTION_DECL
9448 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9450 tree args = DECL_ARGUMENTS (fndecl);
9451 for (; args; args = DECL_CHAIN (args))
9453 tree type = TREE_TYPE (args);
9454 if (INTEGRAL_TYPE_P (type)
9455 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9456 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9460 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9461 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9463 /* Must mark the RESULT_DECL as being in this function. */
9465 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9466 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9468 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9469 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9470 == integer_type_node && flag_isoc99)
9472 /* Hack. We don't want the middle-end to warn that this return
9473 is unreachable, so we mark its location as special. Using
9474 UNKNOWN_LOCATION has the problem that it gets clobbered in
9475 annotate_one_with_locus. A cleaner solution might be to
9476 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9478 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9481 /* Tie off the statement tree for this function. */
9482 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9484 finish_fname_decls ();
9486 /* Complain if there's no return statement only if option specified on
9487 command line. */
9488 if (warn_return_type > 0
9489 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9490 && !current_function_returns_value && !current_function_returns_null
9491 /* Don't complain if we are no-return. */
9492 && !current_function_returns_abnormally
9493 /* Don't complain if we are declared noreturn. */
9494 && !TREE_THIS_VOLATILE (fndecl)
9495 /* Don't warn for main(). */
9496 && !MAIN_NAME_P (DECL_NAME (fndecl))
9497 /* Or if they didn't actually specify a return type. */
9498 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9499 /* Normally, with -Wreturn-type, flow will complain, but we might
9500 optimize out static functions. */
9501 && !TREE_PUBLIC (fndecl))
9503 warning (OPT_Wreturn_type,
9504 "no return statement in function returning non-void");
9505 TREE_NO_WARNING (fndecl) = 1;
9508 /* Complain about parameters that are only set, but never otherwise used. */
9509 if (warn_unused_but_set_parameter)
9511 tree decl;
9513 for (decl = DECL_ARGUMENTS (fndecl);
9514 decl;
9515 decl = DECL_CHAIN (decl))
9516 if (TREE_USED (decl)
9517 && TREE_CODE (decl) == PARM_DECL
9518 && !DECL_READ_P (decl)
9519 && DECL_NAME (decl)
9520 && !DECL_ARTIFICIAL (decl)
9521 && !TREE_NO_WARNING (decl))
9522 warning_at (DECL_SOURCE_LOCATION (decl),
9523 OPT_Wunused_but_set_parameter,
9524 "parameter %qD set but not used", decl);
9527 /* Complain about locally defined typedefs that are not used in this
9528 function. */
9529 maybe_warn_unused_local_typedefs ();
9531 /* Possibly warn about unused parameters. */
9532 if (warn_unused_parameter)
9533 do_warn_unused_parameter (fndecl);
9535 /* Store the end of the function, so that we get good line number
9536 info for the epilogue. */
9537 cfun->function_end_locus = input_location;
9539 /* Finalize the ELF visibility for the function. */
9540 c_determine_visibility (fndecl);
9542 /* For GNU C extern inline functions disregard inline limits. */
9543 if (DECL_EXTERNAL (fndecl)
9544 && DECL_DECLARED_INLINE_P (fndecl)
9545 && (flag_gnu89_inline
9546 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9547 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9549 /* Genericize before inlining. Delay genericizing nested functions
9550 until their parent function is genericized. Since finalizing
9551 requires GENERIC, delay that as well. */
9553 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9554 && !undef_nested_function)
9556 if (!decl_function_context (fndecl))
9558 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9559 c_genericize (fndecl);
9561 /* ??? Objc emits functions after finalizing the compilation unit.
9562 This should be cleaned up later and this conditional removed. */
9563 if (symtab->global_info_ready)
9565 cgraph_node::add_new_function (fndecl, false);
9566 return;
9568 cgraph_node::finalize_function (fndecl, false);
9570 else
9572 /* Register this function with cgraph just far enough to get it
9573 added to our parent's nested function list. Handy, since the
9574 C front end doesn't have such a list. */
9575 (void) cgraph_node::get_create (fndecl);
9579 if (!decl_function_context (fndecl))
9580 undef_nested_function = false;
9582 if (cfun->language != NULL)
9584 ggc_free (cfun->language);
9585 cfun->language = NULL;
9588 /* We're leaving the context of this function, so zap cfun.
9589 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9590 tree_rest_of_compilation. */
9591 set_cfun (NULL);
9592 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9593 current_function_decl = NULL;
9596 /* Check the declarations given in a for-loop for satisfying the C99
9597 constraints. If exactly one such decl is found, return it. LOC is
9598 the location of the opening parenthesis of the for loop. The last
9599 parameter allows you to control the "for loop initial declarations
9600 are only allowed in C99 mode". Normally, you should pass
9601 flag_isoc99 as that parameter. But in some cases (Objective-C
9602 foreach loop, for example) we want to run the checks in this
9603 function even if not in C99 mode, so we allow the caller to turn
9604 off the error about not being in C99 mode.
9607 tree
9608 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9610 struct c_binding *b;
9611 tree one_decl = NULL_TREE;
9612 int n_decls = 0;
9614 if (!turn_off_iso_c99_error)
9616 static bool hint = true;
9617 /* If we get here, declarations have been used in a for loop without
9618 the C99 for loop scope. This doesn't make much sense, so don't
9619 allow it. */
9620 error_at (loc, "%<for%> loop initial declarations "
9621 "are only allowed in C99 or C11 mode");
9622 if (hint)
9624 inform (loc,
9625 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9626 "to compile your code");
9627 hint = false;
9629 return NULL_TREE;
9631 else
9632 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
9633 "initial declarations");
9635 /* C99 subclause 6.8.5 paragraph 3:
9637 [#3] The declaration part of a for statement shall only
9638 declare identifiers for objects having storage class auto or
9639 register.
9641 It isn't clear whether, in this sentence, "identifiers" binds to
9642 "shall only declare" or to "objects" - that is, whether all identifiers
9643 declared must be identifiers for objects, or whether the restriction
9644 only applies to those that are. (A question on this in comp.std.c
9645 in November 2000 received no answer.) We implement the strictest
9646 interpretation, to avoid creating an extension which later causes
9647 problems. */
9649 for (b = current_scope->bindings; b; b = b->prev)
9651 tree id = b->id;
9652 tree decl = b->decl;
9654 if (!id)
9655 continue;
9657 switch (TREE_CODE (decl))
9659 case VAR_DECL:
9661 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9662 if (TREE_STATIC (decl))
9663 error_at (decl_loc,
9664 "declaration of static variable %qD in %<for%> loop "
9665 "initial declaration", decl);
9666 else if (DECL_EXTERNAL (decl))
9667 error_at (decl_loc,
9668 "declaration of %<extern%> variable %qD in %<for%> loop "
9669 "initial declaration", decl);
9671 break;
9673 case RECORD_TYPE:
9674 error_at (loc,
9675 "%<struct %E%> declared in %<for%> loop initial "
9676 "declaration", id);
9677 break;
9678 case UNION_TYPE:
9679 error_at (loc,
9680 "%<union %E%> declared in %<for%> loop initial declaration",
9681 id);
9682 break;
9683 case ENUMERAL_TYPE:
9684 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9685 "initial declaration", id);
9686 break;
9687 default:
9688 error_at (loc, "declaration of non-variable "
9689 "%qD in %<for%> loop initial declaration", decl);
9692 n_decls++;
9693 one_decl = decl;
9696 return n_decls == 1 ? one_decl : NULL_TREE;
9699 /* Save and reinitialize the variables
9700 used during compilation of a C function. */
9702 void
9703 c_push_function_context (void)
9705 struct language_function *p = cfun->language;
9706 /* cfun->language might have been already allocated by the use of
9707 -Wunused-local-typedefs. In that case, just re-use it. */
9708 if (p == NULL)
9709 cfun->language = p = ggc_cleared_alloc<language_function> ();
9711 p->base.x_stmt_tree = c_stmt_tree;
9712 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9713 p->x_break_label = c_break_label;
9714 p->x_cont_label = c_cont_label;
9715 p->x_switch_stack = c_switch_stack;
9716 p->arg_info = current_function_arg_info;
9717 p->returns_value = current_function_returns_value;
9718 p->returns_null = current_function_returns_null;
9719 p->returns_abnormally = current_function_returns_abnormally;
9720 p->warn_about_return_type = warn_about_return_type;
9722 push_function_context ();
9725 /* Restore the variables used during compilation of a C function. */
9727 void
9728 c_pop_function_context (void)
9730 struct language_function *p;
9732 pop_function_context ();
9733 p = cfun->language;
9735 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9736 used to store data throughout the life time of the current cfun,
9737 So don't deallocate it. */
9738 if (!warn_unused_local_typedefs)
9739 cfun->language = NULL;
9741 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9742 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9744 /* Stop pointing to the local nodes about to be freed. */
9745 /* But DECL_INITIAL must remain nonzero so we know this
9746 was an actual function definition. */
9747 DECL_INITIAL (current_function_decl) = error_mark_node;
9748 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9751 c_stmt_tree = p->base.x_stmt_tree;
9752 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9753 c_break_label = p->x_break_label;
9754 c_cont_label = p->x_cont_label;
9755 c_switch_stack = p->x_switch_stack;
9756 current_function_arg_info = p->arg_info;
9757 current_function_returns_value = p->returns_value;
9758 current_function_returns_null = p->returns_null;
9759 current_function_returns_abnormally = p->returns_abnormally;
9760 warn_about_return_type = p->warn_about_return_type;
9763 /* The functions below are required for functionality of doing
9764 function at once processing in the C front end. Currently these
9765 functions are not called from anywhere in the C front end, but as
9766 these changes continue, that will change. */
9768 /* Returns the stmt_tree (if any) to which statements are currently
9769 being added. If there is no active statement-tree, NULL is
9770 returned. */
9772 stmt_tree
9773 current_stmt_tree (void)
9775 return &c_stmt_tree;
9778 /* Return the global value of T as a symbol. */
9780 tree
9781 identifier_global_value (tree t)
9783 struct c_binding *b;
9785 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9786 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9787 return b->decl;
9789 return NULL_TREE;
9792 /* In C, the only C-linkage public declaration is at file scope. */
9794 tree
9795 c_linkage_bindings (tree name)
9797 return identifier_global_value (name);
9800 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9801 otherwise the name is found in ridpointers from RID_INDEX. */
9803 void
9804 record_builtin_type (enum rid rid_index, const char *name, tree type)
9806 tree id, decl;
9807 if (name == 0)
9808 id = ridpointers[(int) rid_index];
9809 else
9810 id = get_identifier (name);
9811 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9812 pushdecl (decl);
9813 if (debug_hooks->type_decl)
9814 debug_hooks->type_decl (decl, false);
9817 /* Build the void_list_node (void_type_node having been created). */
9818 tree
9819 build_void_list_node (void)
9821 tree t = build_tree_list (NULL_TREE, void_type_node);
9822 return t;
9825 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9827 struct c_parm *
9828 build_c_parm (struct c_declspecs *specs, tree attrs,
9829 struct c_declarator *declarator,
9830 location_t loc)
9832 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9833 ret->specs = specs;
9834 ret->attrs = attrs;
9835 ret->declarator = declarator;
9836 ret->loc = loc;
9837 return ret;
9840 /* Return a declarator with nested attributes. TARGET is the inner
9841 declarator to which these attributes apply. ATTRS are the
9842 attributes. */
9844 struct c_declarator *
9845 build_attrs_declarator (tree attrs, struct c_declarator *target)
9847 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9848 ret->kind = cdk_attrs;
9849 ret->declarator = target;
9850 ret->u.attrs = attrs;
9851 return ret;
9854 /* Return a declarator for a function with arguments specified by ARGS
9855 and return type specified by TARGET. */
9857 struct c_declarator *
9858 build_function_declarator (struct c_arg_info *args,
9859 struct c_declarator *target)
9861 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9862 ret->kind = cdk_function;
9863 ret->declarator = target;
9864 ret->u.arg_info = args;
9865 return ret;
9868 /* Return a declarator for the identifier IDENT (which may be
9869 NULL_TREE for an abstract declarator). */
9871 struct c_declarator *
9872 build_id_declarator (tree ident)
9874 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9875 ret->kind = cdk_id;
9876 ret->declarator = 0;
9877 ret->u.id = ident;
9878 /* Default value - may get reset to a more precise location. */
9879 ret->id_loc = input_location;
9880 return ret;
9883 /* Return something to represent absolute declarators containing a *.
9884 TARGET is the absolute declarator that the * contains.
9885 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9886 to apply to the pointer type. */
9888 struct c_declarator *
9889 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9890 struct c_declarator *target)
9892 tree attrs;
9893 int quals = 0;
9894 struct c_declarator *itarget = target;
9895 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9896 if (type_quals_attrs)
9898 attrs = type_quals_attrs->attrs;
9899 quals = quals_from_declspecs (type_quals_attrs);
9900 if (attrs != NULL_TREE)
9901 itarget = build_attrs_declarator (attrs, target);
9903 ret->kind = cdk_pointer;
9904 ret->declarator = itarget;
9905 ret->u.pointer_quals = quals;
9906 return ret;
9909 /* Return a pointer to a structure for an empty list of declaration
9910 specifiers. */
9912 struct c_declspecs *
9913 build_null_declspecs (void)
9915 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9916 memset (ret, 0, sizeof *ret);
9917 ret->align_log = -1;
9918 ret->typespec_word = cts_none;
9919 ret->storage_class = csc_none;
9920 ret->expr_const_operands = true;
9921 ret->typespec_kind = ctsk_none;
9922 ret->address_space = ADDR_SPACE_GENERIC;
9923 return ret;
9926 /* Add the address space ADDRSPACE to the declaration specifiers
9927 SPECS, returning SPECS. */
9929 struct c_declspecs *
9930 declspecs_add_addrspace (source_location location,
9931 struct c_declspecs *specs, addr_space_t as)
9933 specs->non_sc_seen_p = true;
9934 specs->declspecs_seen_p = true;
9936 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9937 && specs->address_space != as)
9938 error ("incompatible address space qualifiers %qs and %qs",
9939 c_addr_space_name (as),
9940 c_addr_space_name (specs->address_space));
9941 else
9943 specs->address_space = as;
9944 specs->locations[cdw_address_space] = location;
9946 return specs;
9949 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9950 returning SPECS. */
9952 struct c_declspecs *
9953 declspecs_add_qual (source_location loc,
9954 struct c_declspecs *specs, tree qual)
9956 enum rid i;
9957 bool dupe = false;
9958 specs->non_sc_seen_p = true;
9959 specs->declspecs_seen_p = true;
9960 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9961 && C_IS_RESERVED_WORD (qual));
9962 i = C_RID_CODE (qual);
9963 location_t prev_loc = UNKNOWN_LOCATION;
9964 switch (i)
9966 case RID_CONST:
9967 dupe = specs->const_p;
9968 specs->const_p = true;
9969 prev_loc = specs->locations[cdw_const];
9970 specs->locations[cdw_const] = loc;
9971 break;
9972 case RID_VOLATILE:
9973 dupe = specs->volatile_p;
9974 specs->volatile_p = true;
9975 prev_loc = specs->locations[cdw_volatile];
9976 specs->locations[cdw_volatile] = loc;
9977 break;
9978 case RID_RESTRICT:
9979 dupe = specs->restrict_p;
9980 specs->restrict_p = true;
9981 prev_loc = specs->locations[cdw_restrict];
9982 specs->locations[cdw_restrict] = loc;
9983 break;
9984 case RID_ATOMIC:
9985 dupe = specs->atomic_p;
9986 specs->atomic_p = true;
9987 prev_loc = specs->locations[cdw_atomic];
9988 specs->locations[cdw_atomic] = loc;
9989 break;
9990 default:
9991 gcc_unreachable ();
9993 if (dupe)
9995 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9996 "duplicate %qE declaration specifier", qual);
9997 if (!warned
9998 && warn_duplicate_decl_specifier
9999 && prev_loc >= RESERVED_LOCATION_COUNT
10000 && !from_macro_expansion_at (prev_loc)
10001 && !from_macro_expansion_at (loc))
10002 warning_at (loc, OPT_Wduplicate_decl_specifier,
10003 "duplicate %qE declaration specifier", qual);
10005 return specs;
10008 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10009 returning SPECS. */
10011 struct c_declspecs *
10012 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10013 struct c_typespec spec)
10015 tree type = spec.spec;
10016 specs->non_sc_seen_p = true;
10017 specs->declspecs_seen_p = true;
10018 specs->typespec_kind = spec.kind;
10019 if (TREE_DEPRECATED (type))
10020 specs->deprecated_p = true;
10022 /* Handle type specifier keywords. */
10023 if (TREE_CODE (type) == IDENTIFIER_NODE
10024 && C_IS_RESERVED_WORD (type)
10025 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10027 enum rid i = C_RID_CODE (type);
10028 if (specs->type)
10030 error_at (loc, "two or more data types in declaration specifiers");
10031 return specs;
10033 if ((int) i <= (int) RID_LAST_MODIFIER)
10035 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10036 bool dupe = false;
10037 switch (i)
10039 case RID_LONG:
10040 if (specs->long_long_p)
10042 error_at (loc, "%<long long long%> is too long for GCC");
10043 break;
10045 if (specs->long_p)
10047 if (specs->typespec_word == cts_double)
10049 error_at (loc,
10050 ("both %<long long%> and %<double%> in "
10051 "declaration specifiers"));
10052 break;
10054 pedwarn_c90 (loc, OPT_Wlong_long,
10055 "ISO C90 does not support %<long long%>");
10056 specs->long_long_p = 1;
10057 specs->locations[cdw_long_long] = loc;
10058 break;
10060 if (specs->short_p)
10061 error_at (loc,
10062 ("both %<long%> and %<short%> in "
10063 "declaration specifiers"));
10064 else if (specs->typespec_word == cts_auto_type)
10065 error_at (loc,
10066 ("both %<long%> and %<__auto_type%> in "
10067 "declaration specifiers"));
10068 else if (specs->typespec_word == cts_void)
10069 error_at (loc,
10070 ("both %<long%> and %<void%> in "
10071 "declaration specifiers"));
10072 else if (specs->typespec_word == cts_int_n)
10073 error_at (loc,
10074 ("both %<long%> and %<__int%d%> in "
10075 "declaration specifiers"),
10076 int_n_data[specs->int_n_idx].bitsize);
10077 else if (specs->typespec_word == cts_bool)
10078 error_at (loc,
10079 ("both %<long%> and %<_Bool%> in "
10080 "declaration specifiers"));
10081 else if (specs->typespec_word == cts_char)
10082 error_at (loc,
10083 ("both %<long%> and %<char%> in "
10084 "declaration specifiers"));
10085 else if (specs->typespec_word == cts_float)
10086 error_at (loc,
10087 ("both %<long%> and %<float%> in "
10088 "declaration specifiers"));
10089 else if (specs->typespec_word == cts_floatn_nx)
10090 error_at (loc,
10091 ("both %<long%> and %<_Float%d%s%> in "
10092 "declaration specifiers"),
10093 floatn_nx_types[specs->floatn_nx_idx].n,
10094 (floatn_nx_types[specs->floatn_nx_idx].extended
10095 ? "x"
10096 : ""));
10097 else if (specs->typespec_word == cts_dfloat32)
10098 error_at (loc,
10099 ("both %<long%> and %<_Decimal32%> in "
10100 "declaration specifiers"));
10101 else if (specs->typespec_word == cts_dfloat64)
10102 error_at (loc,
10103 ("both %<long%> and %<_Decimal64%> in "
10104 "declaration specifiers"));
10105 else if (specs->typespec_word == cts_dfloat128)
10106 error_at (loc,
10107 ("both %<long%> and %<_Decimal128%> in "
10108 "declaration specifiers"));
10109 else
10111 specs->long_p = true;
10112 specs->locations[cdw_long] = loc;
10114 break;
10115 case RID_SHORT:
10116 dupe = specs->short_p;
10117 if (specs->long_p)
10118 error_at (loc,
10119 ("both %<long%> and %<short%> in "
10120 "declaration specifiers"));
10121 else if (specs->typespec_word == cts_auto_type)
10122 error_at (loc,
10123 ("both %<short%> and %<__auto_type%> in "
10124 "declaration specifiers"));
10125 else if (specs->typespec_word == cts_void)
10126 error_at (loc,
10127 ("both %<short%> and %<void%> in "
10128 "declaration specifiers"));
10129 else if (specs->typespec_word == cts_int_n)
10130 error_at (loc,
10131 ("both %<short%> and %<__int%d%> in "
10132 "declaration specifiers"),
10133 int_n_data[specs->int_n_idx].bitsize);
10134 else if (specs->typespec_word == cts_bool)
10135 error_at (loc,
10136 ("both %<short%> and %<_Bool%> in "
10137 "declaration specifiers"));
10138 else if (specs->typespec_word == cts_char)
10139 error_at (loc,
10140 ("both %<short%> and %<char%> in "
10141 "declaration specifiers"));
10142 else if (specs->typespec_word == cts_float)
10143 error_at (loc,
10144 ("both %<short%> and %<float%> in "
10145 "declaration specifiers"));
10146 else if (specs->typespec_word == cts_double)
10147 error_at (loc,
10148 ("both %<short%> and %<double%> in "
10149 "declaration specifiers"));
10150 else if (specs->typespec_word == cts_floatn_nx)
10151 error_at (loc,
10152 ("both %<short%> and %<_Float%d%s%> in "
10153 "declaration specifiers"),
10154 floatn_nx_types[specs->floatn_nx_idx].n,
10155 (floatn_nx_types[specs->floatn_nx_idx].extended
10156 ? "x"
10157 : ""));
10158 else if (specs->typespec_word == cts_dfloat32)
10159 error_at (loc,
10160 ("both %<short%> and %<_Decimal32%> in "
10161 "declaration specifiers"));
10162 else if (specs->typespec_word == cts_dfloat64)
10163 error_at (loc,
10164 ("both %<short%> and %<_Decimal64%> in "
10165 "declaration specifiers"));
10166 else if (specs->typespec_word == cts_dfloat128)
10167 error_at (loc,
10168 ("both %<short%> and %<_Decimal128%> in "
10169 "declaration specifiers"));
10170 else
10172 specs->short_p = true;
10173 specs->locations[cdw_short] = loc;
10175 break;
10176 case RID_SIGNED:
10177 dupe = specs->signed_p;
10178 if (specs->unsigned_p)
10179 error_at (loc,
10180 ("both %<signed%> and %<unsigned%> in "
10181 "declaration specifiers"));
10182 else if (specs->typespec_word == cts_auto_type)
10183 error_at (loc,
10184 ("both %<signed%> and %<__auto_type%> in "
10185 "declaration specifiers"));
10186 else if (specs->typespec_word == cts_void)
10187 error_at (loc,
10188 ("both %<signed%> and %<void%> in "
10189 "declaration specifiers"));
10190 else if (specs->typespec_word == cts_bool)
10191 error_at (loc,
10192 ("both %<signed%> and %<_Bool%> in "
10193 "declaration specifiers"));
10194 else if (specs->typespec_word == cts_float)
10195 error_at (loc,
10196 ("both %<signed%> and %<float%> in "
10197 "declaration specifiers"));
10198 else if (specs->typespec_word == cts_double)
10199 error_at (loc,
10200 ("both %<signed%> and %<double%> in "
10201 "declaration specifiers"));
10202 else if (specs->typespec_word == cts_floatn_nx)
10203 error_at (loc,
10204 ("both %<signed%> and %<_Float%d%s%> in "
10205 "declaration specifiers"),
10206 floatn_nx_types[specs->floatn_nx_idx].n,
10207 (floatn_nx_types[specs->floatn_nx_idx].extended
10208 ? "x"
10209 : ""));
10210 else if (specs->typespec_word == cts_dfloat32)
10211 error_at (loc,
10212 ("both %<signed%> and %<_Decimal32%> in "
10213 "declaration specifiers"));
10214 else if (specs->typespec_word == cts_dfloat64)
10215 error_at (loc,
10216 ("both %<signed%> and %<_Decimal64%> in "
10217 "declaration specifiers"));
10218 else if (specs->typespec_word == cts_dfloat128)
10219 error_at (loc,
10220 ("both %<signed%> and %<_Decimal128%> in "
10221 "declaration specifiers"));
10222 else
10224 specs->signed_p = true;
10225 specs->locations[cdw_signed] = loc;
10227 break;
10228 case RID_UNSIGNED:
10229 dupe = specs->unsigned_p;
10230 if (specs->signed_p)
10231 error_at (loc,
10232 ("both %<signed%> and %<unsigned%> in "
10233 "declaration specifiers"));
10234 else if (specs->typespec_word == cts_auto_type)
10235 error_at (loc,
10236 ("both %<unsigned%> and %<__auto_type%> in "
10237 "declaration specifiers"));
10238 else if (specs->typespec_word == cts_void)
10239 error_at (loc,
10240 ("both %<unsigned%> and %<void%> in "
10241 "declaration specifiers"));
10242 else if (specs->typespec_word == cts_bool)
10243 error_at (loc,
10244 ("both %<unsigned%> and %<_Bool%> in "
10245 "declaration specifiers"));
10246 else if (specs->typespec_word == cts_float)
10247 error_at (loc,
10248 ("both %<unsigned%> and %<float%> in "
10249 "declaration specifiers"));
10250 else if (specs->typespec_word == cts_double)
10251 error_at (loc,
10252 ("both %<unsigned%> and %<double%> in "
10253 "declaration specifiers"));
10254 else if (specs->typespec_word == cts_floatn_nx)
10255 error_at (loc,
10256 ("both %<unsigned%> and %<_Float%d%s%> in "
10257 "declaration specifiers"),
10258 floatn_nx_types[specs->floatn_nx_idx].n,
10259 (floatn_nx_types[specs->floatn_nx_idx].extended
10260 ? "x"
10261 : ""));
10262 else if (specs->typespec_word == cts_dfloat32)
10263 error_at (loc,
10264 ("both %<unsigned%> and %<_Decimal32%> in "
10265 "declaration specifiers"));
10266 else if (specs->typespec_word == cts_dfloat64)
10267 error_at (loc,
10268 ("both %<unsigned%> and %<_Decimal64%> in "
10269 "declaration specifiers"));
10270 else if (specs->typespec_word == cts_dfloat128)
10271 error_at (loc,
10272 ("both %<unsigned%> and %<_Decimal128%> in "
10273 "declaration specifiers"));
10274 else
10276 specs->unsigned_p = true;
10277 specs->locations[cdw_unsigned] = loc;
10279 break;
10280 case RID_COMPLEX:
10281 dupe = specs->complex_p;
10282 if (!in_system_header_at (loc))
10283 pedwarn_c90 (loc, OPT_Wpedantic,
10284 "ISO C90 does not support complex types");
10285 if (specs->typespec_word == cts_auto_type)
10286 error_at (loc,
10287 ("both %<complex%> and %<__auto_type%> in "
10288 "declaration specifiers"));
10289 else if (specs->typespec_word == cts_void)
10290 error_at (loc,
10291 ("both %<complex%> and %<void%> in "
10292 "declaration specifiers"));
10293 else if (specs->typespec_word == cts_bool)
10294 error_at (loc,
10295 ("both %<complex%> and %<_Bool%> in "
10296 "declaration specifiers"));
10297 else if (specs->typespec_word == cts_dfloat32)
10298 error_at (loc,
10299 ("both %<complex%> and %<_Decimal32%> in "
10300 "declaration specifiers"));
10301 else if (specs->typespec_word == cts_dfloat64)
10302 error_at (loc,
10303 ("both %<complex%> and %<_Decimal64%> in "
10304 "declaration specifiers"));
10305 else if (specs->typespec_word == cts_dfloat128)
10306 error_at (loc,
10307 ("both %<complex%> and %<_Decimal128%> in "
10308 "declaration specifiers"));
10309 else if (specs->typespec_word == cts_fract)
10310 error_at (loc,
10311 ("both %<complex%> and %<_Fract%> in "
10312 "declaration specifiers"));
10313 else if (specs->typespec_word == cts_accum)
10314 error_at (loc,
10315 ("both %<complex%> and %<_Accum%> in "
10316 "declaration specifiers"));
10317 else if (specs->saturating_p)
10318 error_at (loc,
10319 ("both %<complex%> and %<_Sat%> in "
10320 "declaration specifiers"));
10321 else
10323 specs->complex_p = true;
10324 specs->locations[cdw_complex] = loc;
10326 break;
10327 case RID_SAT:
10328 dupe = specs->saturating_p;
10329 pedwarn (loc, OPT_Wpedantic,
10330 "ISO C does not support saturating types");
10331 if (specs->typespec_word == cts_int_n)
10333 error_at (loc,
10334 ("both %<_Sat%> and %<__int%d%> in "
10335 "declaration specifiers"),
10336 int_n_data[specs->int_n_idx].bitsize);
10338 else if (specs->typespec_word == cts_auto_type)
10339 error_at (loc,
10340 ("both %<_Sat%> and %<__auto_type%> in "
10341 "declaration specifiers"));
10342 else if (specs->typespec_word == cts_void)
10343 error_at (loc,
10344 ("both %<_Sat%> and %<void%> in "
10345 "declaration specifiers"));
10346 else if (specs->typespec_word == cts_bool)
10347 error_at (loc,
10348 ("both %<_Sat%> and %<_Bool%> in "
10349 "declaration specifiers"));
10350 else if (specs->typespec_word == cts_char)
10351 error_at (loc,
10352 ("both %<_Sat%> and %<char%> in "
10353 "declaration specifiers"));
10354 else if (specs->typespec_word == cts_int)
10355 error_at (loc,
10356 ("both %<_Sat%> and %<int%> in "
10357 "declaration specifiers"));
10358 else if (specs->typespec_word == cts_float)
10359 error_at (loc,
10360 ("both %<_Sat%> and %<float%> in "
10361 "declaration specifiers"));
10362 else if (specs->typespec_word == cts_double)
10363 error_at (loc,
10364 ("both %<_Sat%> and %<double%> in "
10365 "declaration specifiers"));
10366 else if (specs->typespec_word == cts_floatn_nx)
10367 error_at (loc,
10368 ("both %<_Sat%> and %<_Float%d%s%> in "
10369 "declaration specifiers"),
10370 floatn_nx_types[specs->floatn_nx_idx].n,
10371 (floatn_nx_types[specs->floatn_nx_idx].extended
10372 ? "x"
10373 : ""));
10374 else if (specs->typespec_word == cts_dfloat32)
10375 error_at (loc,
10376 ("both %<_Sat%> and %<_Decimal32%> in "
10377 "declaration specifiers"));
10378 else if (specs->typespec_word == cts_dfloat64)
10379 error_at (loc,
10380 ("both %<_Sat%> and %<_Decimal64%> in "
10381 "declaration specifiers"));
10382 else if (specs->typespec_word == cts_dfloat128)
10383 error_at (loc,
10384 ("both %<_Sat%> and %<_Decimal128%> in "
10385 "declaration specifiers"));
10386 else if (specs->complex_p)
10387 error_at (loc,
10388 ("both %<_Sat%> and %<complex%> in "
10389 "declaration specifiers"));
10390 else
10392 specs->saturating_p = true;
10393 specs->locations[cdw_saturating] = loc;
10395 break;
10396 default:
10397 gcc_unreachable ();
10400 if (dupe)
10401 error_at (loc, "duplicate %qE", type);
10403 return specs;
10405 else
10407 /* "void", "_Bool", "char", "int", "float", "double",
10408 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10409 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10410 "__auto_type". */
10411 if (specs->typespec_word != cts_none)
10413 error_at (loc,
10414 "two or more data types in declaration specifiers");
10415 return specs;
10417 switch (i)
10419 case RID_AUTO_TYPE:
10420 if (specs->long_p)
10421 error_at (loc,
10422 ("both %<long%> and %<__auto_type%> in "
10423 "declaration specifiers"));
10424 else if (specs->short_p)
10425 error_at (loc,
10426 ("both %<short%> and %<__auto_type%> in "
10427 "declaration specifiers"));
10428 else if (specs->signed_p)
10429 error_at (loc,
10430 ("both %<signed%> and %<__auto_type%> in "
10431 "declaration specifiers"));
10432 else if (specs->unsigned_p)
10433 error_at (loc,
10434 ("both %<unsigned%> and %<__auto_type%> in "
10435 "declaration specifiers"));
10436 else if (specs->complex_p)
10437 error_at (loc,
10438 ("both %<complex%> and %<__auto_type%> in "
10439 "declaration specifiers"));
10440 else if (specs->saturating_p)
10441 error_at (loc,
10442 ("both %<_Sat%> and %<__auto_type%> in "
10443 "declaration specifiers"));
10444 else
10446 specs->typespec_word = cts_auto_type;
10447 specs->locations[cdw_typespec] = loc;
10449 return specs;
10450 case RID_INT_N_0:
10451 case RID_INT_N_1:
10452 case RID_INT_N_2:
10453 case RID_INT_N_3:
10454 specs->int_n_idx = i - RID_INT_N_0;
10455 if (!in_system_header_at (input_location))
10456 pedwarn (loc, OPT_Wpedantic,
10457 "ISO C does not support %<__int%d%> types",
10458 int_n_data[specs->int_n_idx].bitsize);
10460 if (specs->long_p)
10461 error_at (loc,
10462 ("both %<__int%d%> and %<long%> in "
10463 "declaration specifiers"),
10464 int_n_data[specs->int_n_idx].bitsize);
10465 else if (specs->saturating_p)
10466 error_at (loc,
10467 ("both %<_Sat%> and %<__int%d%> in "
10468 "declaration specifiers"),
10469 int_n_data[specs->int_n_idx].bitsize);
10470 else if (specs->short_p)
10471 error_at (loc,
10472 ("both %<__int%d%> and %<short%> in "
10473 "declaration specifiers"),
10474 int_n_data[specs->int_n_idx].bitsize);
10475 else if (! int_n_enabled_p[specs->int_n_idx])
10477 specs->typespec_word = cts_int_n;
10478 error_at (loc,
10479 "%<__int%d%> is not supported on this target",
10480 int_n_data[specs->int_n_idx].bitsize);
10482 else
10484 specs->typespec_word = cts_int_n;
10485 specs->locations[cdw_typespec] = loc;
10487 return specs;
10488 case RID_VOID:
10489 if (specs->long_p)
10490 error_at (loc,
10491 ("both %<long%> and %<void%> in "
10492 "declaration specifiers"));
10493 else if (specs->short_p)
10494 error_at (loc,
10495 ("both %<short%> and %<void%> in "
10496 "declaration specifiers"));
10497 else if (specs->signed_p)
10498 error_at (loc,
10499 ("both %<signed%> and %<void%> in "
10500 "declaration specifiers"));
10501 else if (specs->unsigned_p)
10502 error_at (loc,
10503 ("both %<unsigned%> and %<void%> in "
10504 "declaration specifiers"));
10505 else if (specs->complex_p)
10506 error_at (loc,
10507 ("both %<complex%> and %<void%> in "
10508 "declaration specifiers"));
10509 else if (specs->saturating_p)
10510 error_at (loc,
10511 ("both %<_Sat%> and %<void%> in "
10512 "declaration specifiers"));
10513 else
10515 specs->typespec_word = cts_void;
10516 specs->locations[cdw_typespec] = loc;
10518 return specs;
10519 case RID_BOOL:
10520 if (!in_system_header_at (loc))
10521 pedwarn_c90 (loc, OPT_Wpedantic,
10522 "ISO C90 does not support boolean types");
10523 if (specs->long_p)
10524 error_at (loc,
10525 ("both %<long%> and %<_Bool%> in "
10526 "declaration specifiers"));
10527 else if (specs->short_p)
10528 error_at (loc,
10529 ("both %<short%> and %<_Bool%> in "
10530 "declaration specifiers"));
10531 else if (specs->signed_p)
10532 error_at (loc,
10533 ("both %<signed%> and %<_Bool%> in "
10534 "declaration specifiers"));
10535 else if (specs->unsigned_p)
10536 error_at (loc,
10537 ("both %<unsigned%> and %<_Bool%> in "
10538 "declaration specifiers"));
10539 else if (specs->complex_p)
10540 error_at (loc,
10541 ("both %<complex%> and %<_Bool%> in "
10542 "declaration specifiers"));
10543 else if (specs->saturating_p)
10544 error_at (loc,
10545 ("both %<_Sat%> and %<_Bool%> in "
10546 "declaration specifiers"));
10547 else
10549 specs->typespec_word = cts_bool;
10550 specs->locations[cdw_typespec] = loc;
10552 return specs;
10553 case RID_CHAR:
10554 if (specs->long_p)
10555 error_at (loc,
10556 ("both %<long%> and %<char%> in "
10557 "declaration specifiers"));
10558 else if (specs->short_p)
10559 error_at (loc,
10560 ("both %<short%> and %<char%> in "
10561 "declaration specifiers"));
10562 else if (specs->saturating_p)
10563 error_at (loc,
10564 ("both %<_Sat%> and %<char%> in "
10565 "declaration specifiers"));
10566 else
10568 specs->typespec_word = cts_char;
10569 specs->locations[cdw_typespec] = loc;
10571 return specs;
10572 case RID_INT:
10573 if (specs->saturating_p)
10574 error_at (loc,
10575 ("both %<_Sat%> and %<int%> in "
10576 "declaration specifiers"));
10577 else
10579 specs->typespec_word = cts_int;
10580 specs->locations[cdw_typespec] = loc;
10582 return specs;
10583 case RID_FLOAT:
10584 if (specs->long_p)
10585 error_at (loc,
10586 ("both %<long%> and %<float%> in "
10587 "declaration specifiers"));
10588 else if (specs->short_p)
10589 error_at (loc,
10590 ("both %<short%> and %<float%> in "
10591 "declaration specifiers"));
10592 else if (specs->signed_p)
10593 error_at (loc,
10594 ("both %<signed%> and %<float%> in "
10595 "declaration specifiers"));
10596 else if (specs->unsigned_p)
10597 error_at (loc,
10598 ("both %<unsigned%> and %<float%> in "
10599 "declaration specifiers"));
10600 else if (specs->saturating_p)
10601 error_at (loc,
10602 ("both %<_Sat%> and %<float%> in "
10603 "declaration specifiers"));
10604 else
10606 specs->typespec_word = cts_float;
10607 specs->locations[cdw_typespec] = loc;
10609 return specs;
10610 case RID_DOUBLE:
10611 if (specs->long_long_p)
10612 error_at (loc,
10613 ("both %<long long%> and %<double%> in "
10614 "declaration specifiers"));
10615 else if (specs->short_p)
10616 error_at (loc,
10617 ("both %<short%> and %<double%> in "
10618 "declaration specifiers"));
10619 else if (specs->signed_p)
10620 error_at (loc,
10621 ("both %<signed%> and %<double%> in "
10622 "declaration specifiers"));
10623 else if (specs->unsigned_p)
10624 error_at (loc,
10625 ("both %<unsigned%> and %<double%> in "
10626 "declaration specifiers"));
10627 else if (specs->saturating_p)
10628 error_at (loc,
10629 ("both %<_Sat%> and %<double%> in "
10630 "declaration specifiers"));
10631 else
10633 specs->typespec_word = cts_double;
10634 specs->locations[cdw_typespec] = loc;
10636 return specs;
10637 CASE_RID_FLOATN_NX:
10638 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10639 if (!in_system_header_at (input_location))
10640 pedwarn (loc, OPT_Wpedantic,
10641 "ISO C does not support the %<_Float%d%s%> type",
10642 floatn_nx_types[specs->floatn_nx_idx].n,
10643 (floatn_nx_types[specs->floatn_nx_idx].extended
10644 ? "x"
10645 : ""));
10647 if (specs->long_p)
10648 error_at (loc,
10649 ("both %<long%> and %<_Float%d%s%> in "
10650 "declaration specifiers"),
10651 floatn_nx_types[specs->floatn_nx_idx].n,
10652 (floatn_nx_types[specs->floatn_nx_idx].extended
10653 ? "x"
10654 : ""));
10655 else if (specs->short_p)
10656 error_at (loc,
10657 ("both %<short%> and %<_Float%d%s%> in "
10658 "declaration specifiers"),
10659 floatn_nx_types[specs->floatn_nx_idx].n,
10660 (floatn_nx_types[specs->floatn_nx_idx].extended
10661 ? "x"
10662 : ""));
10663 else if (specs->signed_p)
10664 error_at (loc,
10665 ("both %<signed%> and %<_Float%d%s%> in "
10666 "declaration specifiers"),
10667 floatn_nx_types[specs->floatn_nx_idx].n,
10668 (floatn_nx_types[specs->floatn_nx_idx].extended
10669 ? "x"
10670 : ""));
10671 else if (specs->unsigned_p)
10672 error_at (loc,
10673 ("both %<unsigned%> and %<_Float%d%s%> in "
10674 "declaration specifiers"),
10675 floatn_nx_types[specs->floatn_nx_idx].n,
10676 (floatn_nx_types[specs->floatn_nx_idx].extended
10677 ? "x"
10678 : ""));
10679 else if (specs->saturating_p)
10680 error_at (loc,
10681 ("both %<_Sat%> and %<_Float%d%s%> in "
10682 "declaration specifiers"),
10683 floatn_nx_types[specs->floatn_nx_idx].n,
10684 (floatn_nx_types[specs->floatn_nx_idx].extended
10685 ? "x"
10686 : ""));
10687 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10689 specs->typespec_word = cts_floatn_nx;
10690 error_at (loc,
10691 "%<_Float%d%s%> is not supported on this target",
10692 floatn_nx_types[specs->floatn_nx_idx].n,
10693 (floatn_nx_types[specs->floatn_nx_idx].extended
10694 ? "x"
10695 : ""));
10697 else
10699 specs->typespec_word = cts_floatn_nx;
10700 specs->locations[cdw_typespec] = loc;
10702 return specs;
10703 case RID_DFLOAT32:
10704 case RID_DFLOAT64:
10705 case RID_DFLOAT128:
10707 const char *str;
10708 if (i == RID_DFLOAT32)
10709 str = "_Decimal32";
10710 else if (i == RID_DFLOAT64)
10711 str = "_Decimal64";
10712 else
10713 str = "_Decimal128";
10714 if (specs->long_long_p)
10715 error_at (loc,
10716 ("both %<long long%> and %qs in "
10717 "declaration specifiers"),
10718 str);
10719 if (specs->long_p)
10720 error_at (loc,
10721 ("both %<long%> and %qs in "
10722 "declaration specifiers"),
10723 str);
10724 else if (specs->short_p)
10725 error_at (loc,
10726 ("both %<short%> and %qs in "
10727 "declaration specifiers"),
10728 str);
10729 else if (specs->signed_p)
10730 error_at (loc,
10731 ("both %<signed%> and %qs in "
10732 "declaration specifiers"),
10733 str);
10734 else if (specs->unsigned_p)
10735 error_at (loc,
10736 ("both %<unsigned%> and %qs in "
10737 "declaration specifiers"),
10738 str);
10739 else if (specs->complex_p)
10740 error_at (loc,
10741 ("both %<complex%> and %qs in "
10742 "declaration specifiers"),
10743 str);
10744 else if (specs->saturating_p)
10745 error_at (loc,
10746 ("both %<_Sat%> and %qs in "
10747 "declaration specifiers"),
10748 str);
10749 else if (i == RID_DFLOAT32)
10750 specs->typespec_word = cts_dfloat32;
10751 else if (i == RID_DFLOAT64)
10752 specs->typespec_word = cts_dfloat64;
10753 else
10754 specs->typespec_word = cts_dfloat128;
10755 specs->locations[cdw_typespec] = loc;
10757 if (!targetm.decimal_float_supported_p ())
10758 error_at (loc,
10759 ("decimal floating point not supported "
10760 "for this target"));
10761 pedwarn (loc, OPT_Wpedantic,
10762 "ISO C does not support decimal floating point");
10763 return specs;
10764 case RID_FRACT:
10765 case RID_ACCUM:
10767 const char *str;
10768 if (i == RID_FRACT)
10769 str = "_Fract";
10770 else
10771 str = "_Accum";
10772 if (specs->complex_p)
10773 error_at (loc,
10774 ("both %<complex%> and %qs in "
10775 "declaration specifiers"),
10776 str);
10777 else if (i == RID_FRACT)
10778 specs->typespec_word = cts_fract;
10779 else
10780 specs->typespec_word = cts_accum;
10781 specs->locations[cdw_typespec] = loc;
10783 if (!targetm.fixed_point_supported_p ())
10784 error_at (loc,
10785 "fixed-point types not supported for this target");
10786 pedwarn (loc, OPT_Wpedantic,
10787 "ISO C does not support fixed-point types");
10788 return specs;
10789 default:
10790 /* ObjC reserved word "id", handled below. */
10791 break;
10796 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10797 form of ObjC type, cases such as "int" and "long" being handled
10798 above), a TYPE (struct, union, enum and typeof specifiers) or an
10799 ERROR_MARK. In none of these cases may there have previously
10800 been any type specifiers. */
10801 if (specs->type || specs->typespec_word != cts_none
10802 || specs->long_p || specs->short_p || specs->signed_p
10803 || specs->unsigned_p || specs->complex_p)
10804 error_at (loc, "two or more data types in declaration specifiers");
10805 else if (TREE_CODE (type) == TYPE_DECL)
10807 if (TREE_TYPE (type) == error_mark_node)
10808 ; /* Allow the type to default to int to avoid cascading errors. */
10809 else
10811 specs->type = TREE_TYPE (type);
10812 specs->decl_attr = DECL_ATTRIBUTES (type);
10813 specs->typedef_p = true;
10814 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10815 specs->locations[cdw_typedef] = loc;
10817 /* If this typedef name is defined in a struct, then a C++
10818 lookup would return a different value. */
10819 if (warn_cxx_compat
10820 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10821 warning_at (loc, OPT_Wc___compat,
10822 "C++ lookup of %qD would return a field, not a type",
10823 type);
10825 /* If we are parsing a struct, record that a struct field
10826 used a typedef. */
10827 if (warn_cxx_compat && struct_parse_info != NULL)
10828 struct_parse_info->typedefs_seen.safe_push (type);
10831 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10833 tree t = lookup_name (type);
10834 if (!t || TREE_CODE (t) != TYPE_DECL)
10835 error_at (loc, "%qE fails to be a typedef or built in type", type);
10836 else if (TREE_TYPE (t) == error_mark_node)
10838 else
10840 specs->type = TREE_TYPE (t);
10841 specs->locations[cdw_typespec] = loc;
10844 else
10846 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10848 specs->typedef_p = true;
10849 specs->locations[cdw_typedef] = loc;
10850 if (spec.expr)
10852 if (specs->expr)
10853 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10854 specs->expr, spec.expr);
10855 else
10856 specs->expr = spec.expr;
10857 specs->expr_const_operands &= spec.expr_const_operands;
10860 specs->type = type;
10863 return specs;
10866 /* Add the storage class specifier or function specifier SCSPEC to the
10867 declaration specifiers SPECS, returning SPECS. */
10869 struct c_declspecs *
10870 declspecs_add_scspec (source_location loc,
10871 struct c_declspecs *specs,
10872 tree scspec)
10874 enum rid i;
10875 enum c_storage_class n = csc_none;
10876 bool dupe = false;
10877 specs->declspecs_seen_p = true;
10878 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10879 && C_IS_RESERVED_WORD (scspec));
10880 i = C_RID_CODE (scspec);
10881 if (specs->non_sc_seen_p)
10882 warning (OPT_Wold_style_declaration,
10883 "%qE is not at beginning of declaration", scspec);
10884 switch (i)
10886 case RID_INLINE:
10887 /* C99 permits duplicate inline. Although of doubtful utility,
10888 it seems simplest to permit it in gnu89 mode as well, as
10889 there is also little utility in maintaining this as a
10890 difference between gnu89 and C99 inline. */
10891 dupe = false;
10892 specs->inline_p = true;
10893 specs->locations[cdw_inline] = loc;
10894 break;
10895 case RID_NORETURN:
10896 /* Duplicate _Noreturn is permitted. */
10897 dupe = false;
10898 specs->noreturn_p = true;
10899 specs->locations[cdw_noreturn] = loc;
10900 break;
10901 case RID_THREAD:
10902 dupe = specs->thread_p;
10903 if (specs->storage_class == csc_auto)
10904 error ("%qE used with %<auto%>", scspec);
10905 else if (specs->storage_class == csc_register)
10906 error ("%qE used with %<register%>", scspec);
10907 else if (specs->storage_class == csc_typedef)
10908 error ("%qE used with %<typedef%>", scspec);
10909 else
10911 specs->thread_p = true;
10912 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10913 "__thread") == 0);
10914 /* A diagnostic is not required for the use of this
10915 identifier in the implementation namespace; only diagnose
10916 it for the C11 spelling because of existing code using
10917 the other spelling. */
10918 if (!specs->thread_gnu_p)
10920 if (flag_isoc99)
10921 pedwarn_c99 (loc, OPT_Wpedantic,
10922 "ISO C99 does not support %qE", scspec);
10923 else
10924 pedwarn_c99 (loc, OPT_Wpedantic,
10925 "ISO C90 does not support %qE", scspec);
10927 specs->locations[cdw_thread] = loc;
10929 break;
10930 case RID_AUTO:
10931 n = csc_auto;
10932 break;
10933 case RID_EXTERN:
10934 n = csc_extern;
10935 /* Diagnose "__thread extern". */
10936 if (specs->thread_p && specs->thread_gnu_p)
10937 error ("%<__thread%> before %<extern%>");
10938 break;
10939 case RID_REGISTER:
10940 n = csc_register;
10941 break;
10942 case RID_STATIC:
10943 n = csc_static;
10944 /* Diagnose "__thread static". */
10945 if (specs->thread_p && specs->thread_gnu_p)
10946 error ("%<__thread%> before %<static%>");
10947 break;
10948 case RID_TYPEDEF:
10949 n = csc_typedef;
10950 break;
10951 default:
10952 gcc_unreachable ();
10954 if (n != csc_none && n == specs->storage_class)
10955 dupe = true;
10956 if (dupe)
10958 if (i == RID_THREAD)
10959 error ("duplicate %<_Thread_local%> or %<__thread%>");
10960 else
10961 error ("duplicate %qE", scspec);
10963 if (n != csc_none)
10965 if (specs->storage_class != csc_none && n != specs->storage_class)
10967 error ("multiple storage classes in declaration specifiers");
10969 else
10971 specs->storage_class = n;
10972 specs->locations[cdw_storage_class] = loc;
10973 if (n != csc_extern && n != csc_static && specs->thread_p)
10975 error ("%qs used with %qE",
10976 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10977 scspec);
10978 specs->thread_p = false;
10982 return specs;
10985 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10986 returning SPECS. */
10988 struct c_declspecs *
10989 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10991 specs->attrs = chainon (attrs, specs->attrs);
10992 specs->locations[cdw_attributes] = loc;
10993 specs->declspecs_seen_p = true;
10994 return specs;
10997 /* Add an _Alignas specifier (expression ALIGN, or type whose
10998 alignment is ALIGN) to the declaration specifiers SPECS, returning
10999 SPECS. */
11000 struct c_declspecs *
11001 declspecs_add_alignas (source_location loc,
11002 struct c_declspecs *specs, tree align)
11004 int align_log;
11005 specs->alignas_p = true;
11006 specs->locations[cdw_alignas] = loc;
11007 if (align == error_mark_node)
11008 return specs;
11009 align_log = check_user_alignment (align, true);
11010 if (align_log > specs->align_log)
11011 specs->align_log = align_log;
11012 return specs;
11015 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11016 specifiers with any other type specifier to determine the resulting
11017 type. This is where ISO C checks on complex types are made, since
11018 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11019 double". */
11021 struct c_declspecs *
11022 finish_declspecs (struct c_declspecs *specs)
11024 /* If a type was specified as a whole, we have no modifiers and are
11025 done. */
11026 if (specs->type != NULL_TREE)
11028 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11029 && !specs->signed_p && !specs->unsigned_p
11030 && !specs->complex_p);
11032 /* Set a dummy type. */
11033 if (TREE_CODE (specs->type) == ERROR_MARK)
11034 specs->type = integer_type_node;
11035 return specs;
11038 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11039 has been specified, treat it as "int" unless "_Complex" is
11040 present and there are no other specifiers. If we just have
11041 "_Complex", it is equivalent to "_Complex double", but e.g.
11042 "_Complex short" is equivalent to "_Complex short int". */
11043 if (specs->typespec_word == cts_none)
11045 if (specs->saturating_p)
11047 error_at (specs->locations[cdw_saturating],
11048 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11049 if (!targetm.fixed_point_supported_p ())
11050 error_at (specs->locations[cdw_saturating],
11051 "fixed-point types not supported for this target");
11052 specs->typespec_word = cts_fract;
11054 else if (specs->long_p || specs->short_p
11055 || specs->signed_p || specs->unsigned_p)
11057 specs->typespec_word = cts_int;
11059 else if (specs->complex_p)
11061 specs->typespec_word = cts_double;
11062 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11063 "ISO C does not support plain %<complex%> meaning "
11064 "%<double complex%>");
11066 else
11068 specs->typespec_word = cts_int;
11069 specs->default_int_p = true;
11070 /* We don't diagnose this here because grokdeclarator will
11071 give more specific diagnostics according to whether it is
11072 a function definition. */
11076 /* If "signed" was specified, record this to distinguish "int" and
11077 "signed int" in the case of a bit-field with
11078 -funsigned-bitfields. */
11079 specs->explicit_signed_p = specs->signed_p;
11081 /* Now compute the actual type. */
11082 switch (specs->typespec_word)
11084 case cts_auto_type:
11085 gcc_assert (!specs->long_p && !specs->short_p
11086 && !specs->signed_p && !specs->unsigned_p
11087 && !specs->complex_p);
11088 /* Type to be filled in later. */
11089 break;
11090 case cts_void:
11091 gcc_assert (!specs->long_p && !specs->short_p
11092 && !specs->signed_p && !specs->unsigned_p
11093 && !specs->complex_p);
11094 specs->type = void_type_node;
11095 break;
11096 case cts_bool:
11097 gcc_assert (!specs->long_p && !specs->short_p
11098 && !specs->signed_p && !specs->unsigned_p
11099 && !specs->complex_p);
11100 specs->type = boolean_type_node;
11101 break;
11102 case cts_char:
11103 gcc_assert (!specs->long_p && !specs->short_p);
11104 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11105 if (specs->signed_p)
11106 specs->type = signed_char_type_node;
11107 else if (specs->unsigned_p)
11108 specs->type = unsigned_char_type_node;
11109 else
11110 specs->type = char_type_node;
11111 if (specs->complex_p)
11113 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11114 "ISO C does not support complex integer types");
11115 specs->type = build_complex_type (specs->type);
11117 break;
11118 case cts_int_n:
11119 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11120 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11121 if (! int_n_enabled_p[specs->int_n_idx])
11122 specs->type = integer_type_node;
11123 else
11124 specs->type = (specs->unsigned_p
11125 ? int_n_trees[specs->int_n_idx].unsigned_type
11126 : int_n_trees[specs->int_n_idx].signed_type);
11127 if (specs->complex_p)
11129 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11130 "ISO C does not support complex integer types");
11131 specs->type = build_complex_type (specs->type);
11133 break;
11134 case cts_int:
11135 gcc_assert (!(specs->long_p && specs->short_p));
11136 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11137 if (specs->long_long_p)
11138 specs->type = (specs->unsigned_p
11139 ? long_long_unsigned_type_node
11140 : long_long_integer_type_node);
11141 else if (specs->long_p)
11142 specs->type = (specs->unsigned_p
11143 ? long_unsigned_type_node
11144 : long_integer_type_node);
11145 else if (specs->short_p)
11146 specs->type = (specs->unsigned_p
11147 ? short_unsigned_type_node
11148 : short_integer_type_node);
11149 else
11150 specs->type = (specs->unsigned_p
11151 ? unsigned_type_node
11152 : integer_type_node);
11153 if (specs->complex_p)
11155 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11156 "ISO C does not support complex integer types");
11157 specs->type = build_complex_type (specs->type);
11159 break;
11160 case cts_float:
11161 gcc_assert (!specs->long_p && !specs->short_p
11162 && !specs->signed_p && !specs->unsigned_p);
11163 specs->type = (specs->complex_p
11164 ? complex_float_type_node
11165 : float_type_node);
11166 break;
11167 case cts_double:
11168 gcc_assert (!specs->long_long_p && !specs->short_p
11169 && !specs->signed_p && !specs->unsigned_p);
11170 if (specs->long_p)
11172 specs->type = (specs->complex_p
11173 ? complex_long_double_type_node
11174 : long_double_type_node);
11176 else
11178 specs->type = (specs->complex_p
11179 ? complex_double_type_node
11180 : double_type_node);
11182 break;
11183 case cts_floatn_nx:
11184 gcc_assert (!specs->long_p && !specs->short_p
11185 && !specs->signed_p && !specs->unsigned_p);
11186 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11187 specs->type = integer_type_node;
11188 else if (specs->complex_p)
11189 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11190 else
11191 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11192 break;
11193 case cts_dfloat32:
11194 case cts_dfloat64:
11195 case cts_dfloat128:
11196 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11197 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11198 if (specs->typespec_word == cts_dfloat32)
11199 specs->type = dfloat32_type_node;
11200 else if (specs->typespec_word == cts_dfloat64)
11201 specs->type = dfloat64_type_node;
11202 else
11203 specs->type = dfloat128_type_node;
11204 break;
11205 case cts_fract:
11206 gcc_assert (!specs->complex_p);
11207 if (!targetm.fixed_point_supported_p ())
11208 specs->type = integer_type_node;
11209 else if (specs->saturating_p)
11211 if (specs->long_long_p)
11212 specs->type = specs->unsigned_p
11213 ? sat_unsigned_long_long_fract_type_node
11214 : sat_long_long_fract_type_node;
11215 else if (specs->long_p)
11216 specs->type = specs->unsigned_p
11217 ? sat_unsigned_long_fract_type_node
11218 : sat_long_fract_type_node;
11219 else if (specs->short_p)
11220 specs->type = specs->unsigned_p
11221 ? sat_unsigned_short_fract_type_node
11222 : sat_short_fract_type_node;
11223 else
11224 specs->type = specs->unsigned_p
11225 ? sat_unsigned_fract_type_node
11226 : sat_fract_type_node;
11228 else
11230 if (specs->long_long_p)
11231 specs->type = specs->unsigned_p
11232 ? unsigned_long_long_fract_type_node
11233 : long_long_fract_type_node;
11234 else if (specs->long_p)
11235 specs->type = specs->unsigned_p
11236 ? unsigned_long_fract_type_node
11237 : long_fract_type_node;
11238 else if (specs->short_p)
11239 specs->type = specs->unsigned_p
11240 ? unsigned_short_fract_type_node
11241 : short_fract_type_node;
11242 else
11243 specs->type = specs->unsigned_p
11244 ? unsigned_fract_type_node
11245 : fract_type_node;
11247 break;
11248 case cts_accum:
11249 gcc_assert (!specs->complex_p);
11250 if (!targetm.fixed_point_supported_p ())
11251 specs->type = integer_type_node;
11252 else if (specs->saturating_p)
11254 if (specs->long_long_p)
11255 specs->type = specs->unsigned_p
11256 ? sat_unsigned_long_long_accum_type_node
11257 : sat_long_long_accum_type_node;
11258 else if (specs->long_p)
11259 specs->type = specs->unsigned_p
11260 ? sat_unsigned_long_accum_type_node
11261 : sat_long_accum_type_node;
11262 else if (specs->short_p)
11263 specs->type = specs->unsigned_p
11264 ? sat_unsigned_short_accum_type_node
11265 : sat_short_accum_type_node;
11266 else
11267 specs->type = specs->unsigned_p
11268 ? sat_unsigned_accum_type_node
11269 : sat_accum_type_node;
11271 else
11273 if (specs->long_long_p)
11274 specs->type = specs->unsigned_p
11275 ? unsigned_long_long_accum_type_node
11276 : long_long_accum_type_node;
11277 else if (specs->long_p)
11278 specs->type = specs->unsigned_p
11279 ? unsigned_long_accum_type_node
11280 : long_accum_type_node;
11281 else if (specs->short_p)
11282 specs->type = specs->unsigned_p
11283 ? unsigned_short_accum_type_node
11284 : short_accum_type_node;
11285 else
11286 specs->type = specs->unsigned_p
11287 ? unsigned_accum_type_node
11288 : accum_type_node;
11290 break;
11291 default:
11292 gcc_unreachable ();
11295 return specs;
11298 /* Perform final processing on one file scope's declarations (or the
11299 external scope's declarations), GLOBALS. */
11301 static void
11302 c_write_global_declarations_1 (tree globals)
11304 tree decl;
11305 bool reconsider;
11307 /* Process the decls in the order they were written. */
11308 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11310 /* Check for used but undefined static functions using the C
11311 standard's definition of "used", and set TREE_NO_WARNING so
11312 that check_global_declaration doesn't repeat the check. */
11313 if (TREE_CODE (decl) == FUNCTION_DECL
11314 && DECL_INITIAL (decl) == NULL_TREE
11315 && DECL_EXTERNAL (decl)
11316 && !TREE_PUBLIC (decl))
11318 if (C_DECL_USED (decl))
11320 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11321 TREE_NO_WARNING (decl) = 1;
11323 /* For -Wunused-function warn about unused static prototypes. */
11324 else if (warn_unused_function
11325 && ! DECL_ARTIFICIAL (decl)
11326 && ! TREE_NO_WARNING (decl))
11328 warning (OPT_Wunused_function,
11329 "%q+F declared %<static%> but never defined", decl);
11330 TREE_NO_WARNING (decl) = 1;
11334 wrapup_global_declaration_1 (decl);
11339 reconsider = false;
11340 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11341 reconsider |= wrapup_global_declaration_2 (decl);
11343 while (reconsider);
11346 /* Callback to collect a source_ref from a DECL. */
11348 static void
11349 collect_source_ref_cb (tree decl)
11351 if (!DECL_IS_BUILTIN (decl))
11352 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11355 /* Preserve the external declarations scope across a garbage collect. */
11356 static GTY(()) tree ext_block;
11358 /* Collect all references relevant to SOURCE_FILE. */
11360 static void
11361 collect_all_refs (const char *source_file)
11363 tree t;
11364 unsigned i;
11366 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11367 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11369 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11372 /* Iterate over all global declarations and call CALLBACK. */
11374 static void
11375 for_each_global_decl (void (*callback) (tree decl))
11377 tree t;
11378 tree decls;
11379 tree decl;
11380 unsigned i;
11382 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11384 decls = DECL_INITIAL (t);
11385 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11386 callback (decl);
11389 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11390 callback (decl);
11393 /* Perform any final parser cleanups and generate initial debugging
11394 information. */
11396 void
11397 c_parse_final_cleanups (void)
11399 tree t;
11400 unsigned i;
11402 /* We don't want to do this if generating a PCH. */
11403 if (pch_file)
11404 return;
11406 timevar_stop (TV_PHASE_PARSING);
11407 timevar_start (TV_PHASE_DEFERRED);
11409 /* Do the Objective-C stuff. This is where all the Objective-C
11410 module stuff gets generated (symtab, class/protocol/selector
11411 lists etc). */
11412 if (c_dialect_objc ())
11413 objc_write_global_declarations ();
11415 /* Close the external scope. */
11416 ext_block = pop_scope ();
11417 external_scope = 0;
11418 gcc_assert (!current_scope);
11420 /* Handle -fdump-ada-spec[-slim]. */
11421 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11423 /* Build a table of files to generate specs for */
11424 if (flag_dump_ada_spec_slim)
11425 collect_source_ref (main_input_filename);
11426 else
11427 for_each_global_decl (collect_source_ref_cb);
11429 dump_ada_specs (collect_all_refs, NULL);
11432 /* Process all file scopes in this compilation, and the external_scope,
11433 through wrapup_global_declarations. */
11434 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11435 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11436 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11438 timevar_stop (TV_PHASE_DEFERRED);
11439 timevar_start (TV_PHASE_PARSING);
11441 ext_block = NULL;
11444 /* Register reserved keyword WORD as qualifier for address space AS. */
11446 void
11447 c_register_addr_space (const char *word, addr_space_t as)
11449 int rid = RID_FIRST_ADDR_SPACE + as;
11450 tree id;
11452 /* Address space qualifiers are only supported
11453 in C with GNU extensions enabled. */
11454 if (c_dialect_objc () || flag_no_asm)
11455 return;
11457 id = get_identifier (word);
11458 C_SET_RID_CODE (id, rid);
11459 C_IS_RESERVED_WORD (id) = 1;
11460 ridpointers [rid] = id;
11463 /* Return identifier to look up for omp declare reduction. */
11465 tree
11466 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11468 const char *p = NULL;
11469 switch (reduction_code)
11471 case PLUS_EXPR: p = "+"; break;
11472 case MULT_EXPR: p = "*"; break;
11473 case MINUS_EXPR: p = "-"; break;
11474 case BIT_AND_EXPR: p = "&"; break;
11475 case BIT_XOR_EXPR: p = "^"; break;
11476 case BIT_IOR_EXPR: p = "|"; break;
11477 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11478 case TRUTH_ORIF_EXPR: p = "||"; break;
11479 case MIN_EXPR: p = "min"; break;
11480 case MAX_EXPR: p = "max"; break;
11481 default:
11482 break;
11485 if (p == NULL)
11487 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11488 return error_mark_node;
11489 p = IDENTIFIER_POINTER (reduction_id);
11492 const char prefix[] = "omp declare reduction ";
11493 size_t lenp = sizeof (prefix);
11494 size_t len = strlen (p);
11495 char *name = XALLOCAVEC (char, lenp + len);
11496 memcpy (name, prefix, lenp - 1);
11497 memcpy (name + lenp - 1, p, len + 1);
11498 return get_identifier (name);
11501 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11502 VAR_DECL, bind it into the current scope and return it. */
11504 tree
11505 c_omp_reduction_decl (tree reduction_id)
11507 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11508 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11509 return b->decl;
11511 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11512 reduction_id, integer_type_node);
11513 DECL_ARTIFICIAL (decl) = 1;
11514 DECL_EXTERNAL (decl) = 1;
11515 TREE_STATIC (decl) = 1;
11516 TREE_PUBLIC (decl) = 0;
11517 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11518 return decl;
11521 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11523 tree
11524 c_omp_reduction_lookup (tree reduction_id, tree type)
11526 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11527 while (b)
11529 tree t;
11530 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11531 if (comptypes (TREE_PURPOSE (t), type))
11532 return TREE_VALUE (t);
11533 b = b->shadowed;
11535 return error_mark_node;
11538 /* Helper function called via walk_tree, to diagnose invalid
11539 #pragma omp declare reduction combiners or initializers. */
11541 tree
11542 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11544 tree *vars = (tree *) data;
11545 if (SSA_VAR_P (*tp)
11546 && !DECL_ARTIFICIAL (*tp)
11547 && *tp != vars[0]
11548 && *tp != vars[1])
11550 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11551 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11552 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11553 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11554 *tp);
11555 else
11556 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11557 "to variable %qD which is not %<omp_priv%> nor "
11558 "%<omp_orig%>",
11559 *tp);
11560 return *tp;
11562 return NULL_TREE;
11565 #include "gt-c-c-decl.h"