1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2022 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
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
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. */
28 #define INCLUDE_STRING
29 #define INCLUDE_MEMORY
31 #include "coretypes.h"
36 #include "stringpool.h"
39 #include "print-tree.h"
40 #include "stor-layout.h"
45 #include "c-family/c-objc.h"
46 #include "c-family/c-pragma.h"
47 #include "c-family/c-ubsan.h"
49 #include "langhooks.h"
50 #include "tree-iterator.h"
53 #include "c-family/c-ada-spec.h"
55 #include "spellcheck-tree.h"
56 #include "gcc-rich-location.h"
58 #include "c-family/name-hint.h"
59 #include "c-family/known-headers.h"
60 #include "c-family/c-spellcheck.h"
61 #include "context.h" /* For 'g'. */
62 #include "omp-general.h"
63 #include "omp-offload.h" /* For offload_vars. */
65 #include "tree-pretty-print.h"
67 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
69 { NORMAL, /* Ordinary declaration */
70 FUNCDEF, /* Function definition */
71 PARM, /* Declaration of parm before function body */
72 FIELD, /* Declaration inside struct or union */
73 TYPENAME}; /* Typename (inside cast or sizeof) */
75 /* States indicating how grokdeclarator() should handle declspecs marked
76 with __attribute__((deprecated)) or __attribute__((unavailable)).
77 An object declared as __attribute__((unavailable)) should suppress
78 any reports of being declared with unavailable or deprecated items.
79 An object declared as __attribute__((deprecated)) should suppress
80 warnings of uses of other deprecated items. */
82 enum deprecated_states {
85 UNAVAILABLE_DEPRECATED_SUPPRESS
89 /* Nonzero if we have seen an invalid cross reference
90 to a struct, union, or enum, but not yet printed the message. */
91 tree pending_invalid_xref;
93 /* File and line to appear in the eventual error message. */
94 location_t pending_invalid_xref_location;
96 /* The file and line that the prototype came from if this is an
97 old-style definition; used for diagnostics in
98 store_parm_decls_oldstyle. */
100 static location_t current_function_prototype_locus;
102 /* Whether this prototype was built-in. */
104 static bool current_function_prototype_built_in;
106 /* The argument type information of this prototype. */
108 static tree current_function_prototype_arg_types;
110 /* The argument information structure for the function currently being
113 static struct c_arg_info *current_function_arg_info;
115 /* The obstack on which parser and related data structures, which are
116 not live beyond their top-level declaration or definition, are
118 struct obstack parser_obstack;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* Zero if we are not in an iteration or switch statement, otherwise
125 a bitmask. See bitmask definitions in c-tree.h. */
126 unsigned char in_statement;
128 /* A list of decls to be made automatically visible in each file scope. */
129 static GTY(()) tree visible_builtins;
131 /* Set to 0 at beginning of a function definition, set to 1 if
132 a return statement that specifies a return value is seen. */
134 int current_function_returns_value;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement with no argument is seen. */
139 int current_function_returns_null;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a call to a noreturn function is seen. */
144 int current_function_returns_abnormally;
146 /* Set to nonzero by `grokdeclarator' for a function
147 whose return type is defaulted, if warnings for this are desired. */
149 static int warn_about_return_type;
151 /* Nonzero when the current toplevel function contains a declaration
152 of a nested function which is never defined. */
154 static bool undef_nested_function;
156 /* Vector of implicit "omp declare target" attributes to be added into
157 the attribute lists. */
158 vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute;
160 /* If non-zero, we are inside of
161 #pragma omp begin assumes ... #pragma omp end assumes region. */
162 int current_omp_begin_assumes;
164 /* Each c_binding structure describes one binding of an identifier to
165 a decl. All the decls in a scope - irrespective of namespace - are
166 chained together by the ->prev field, which (as the name implies)
167 runs in reverse order. All the decls in a given namespace bound to
168 a given identifier are chained by the ->shadowed field, which runs
169 from inner to outer scopes.
171 The ->decl field usually points to a DECL node, but there are two
172 exceptions. In the namespace of type tags, the bound entity is a
173 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
174 identifier is encountered, it is bound to error_mark_node to
175 suppress further errors about that identifier in the current
178 The ->u.type field stores the type of the declaration in this scope;
179 if NULL, the type is the type of the ->decl field. This is only of
180 relevance for objects with external or internal linkage which may
181 be redeclared in inner scopes, forming composite types that only
182 persist for the duration of those scopes. In the external scope,
183 this stores the composite of all the types declared for this
184 object, visible or not. The ->inner_comp field (used only at file
185 scope) stores whether an incomplete array type at file scope was
186 completed at an inner scope to an array size other than 1.
188 The ->u.label field is used for labels. It points to a structure
189 which stores additional information used for warnings.
191 The depth field is copied from the scope structure that holds this
192 decl. It is used to preserve the proper ordering of the ->shadowed
193 field (see bind()) and also for a handful of special-case checks.
194 Finally, the invisible bit is true for a decl which should be
195 ignored for purposes of normal name lookup, and the nested bit is
196 true for a decl that's been bound a second time in an inner scope;
197 in all such cases, the binding in the outer scope will have its
198 invisible bit true. */
200 struct GTY((chain_next ("%h.prev"))) c_binding {
201 union GTY(()) { /* first so GTY desc can use decl */
202 tree GTY((tag ("0"))) type; /* the type in this scope */
203 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
204 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
205 tree decl; /* the decl bound */
206 tree id; /* the identifier it's bound to */
207 struct c_binding *prev; /* the previous decl in this scope */
208 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
209 unsigned int depth : 28; /* depth of this scope */
210 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
211 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
212 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
213 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
214 location_t locus; /* location for nested bindings */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 /* Each C symbol points to three linked lists of c_binding structures.
222 These describe the values of the identifier in the three different
223 namespaces defined by the language. */
225 struct GTY(()) lang_identifier {
226 struct c_common_identifier common_id;
227 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
228 struct c_binding *tag_binding; /* struct/union/enum tags */
229 struct c_binding *label_binding; /* labels */
232 /* Validate c-lang.cc's assumptions. */
233 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
234 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
236 /* The binding oracle; see c-tree.h. */
237 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
239 /* This flag is set on an identifier if we have previously asked the
240 binding oracle for this identifier's symbol binding. */
241 #define I_SYMBOL_CHECKED(node) \
242 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
244 static inline struct c_binding* *
245 i_symbol_binding (tree node)
247 struct lang_identifier *lid
248 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
250 if (lid->symbol_binding == NULL
251 && c_binding_oracle != NULL
252 && !I_SYMBOL_CHECKED (node))
254 /* Set the "checked" flag first, to avoid infinite recursion
255 when the binding oracle calls back into gcc. */
256 I_SYMBOL_CHECKED (node) = 1;
257 c_binding_oracle (C_ORACLE_SYMBOL, node);
260 return &lid->symbol_binding;
263 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
265 #define I_SYMBOL_DECL(node) \
266 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
268 /* This flag is set on an identifier if we have previously asked the
269 binding oracle for this identifier's tag binding. */
270 #define I_TAG_CHECKED(node) \
271 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
273 static inline struct c_binding **
274 i_tag_binding (tree node)
276 struct lang_identifier *lid
277 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
279 if (lid->tag_binding == NULL
280 && c_binding_oracle != NULL
281 && !I_TAG_CHECKED (node))
283 /* Set the "checked" flag first, to avoid infinite recursion
284 when the binding oracle calls back into gcc. */
285 I_TAG_CHECKED (node) = 1;
286 c_binding_oracle (C_ORACLE_TAG, node);
289 return &lid->tag_binding;
292 #define I_TAG_BINDING(node) (*i_tag_binding (node))
294 #define I_TAG_DECL(node) \
295 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
297 /* This flag is set on an identifier if we have previously asked the
298 binding oracle for this identifier's label binding. */
299 #define I_LABEL_CHECKED(node) \
300 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
302 static inline struct c_binding **
303 i_label_binding (tree node)
305 struct lang_identifier *lid
306 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
308 if (lid->label_binding == NULL
309 && c_binding_oracle != NULL
310 && !I_LABEL_CHECKED (node))
312 /* Set the "checked" flag first, to avoid infinite recursion
313 when the binding oracle calls back into gcc. */
314 I_LABEL_CHECKED (node) = 1;
315 c_binding_oracle (C_ORACLE_LABEL, node);
318 return &lid->label_binding;
321 #define I_LABEL_BINDING(node) (*i_label_binding (node))
323 #define I_LABEL_DECL(node) \
324 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
326 /* The resulting tree type. */
328 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
329 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
331 union tree_node GTY ((tag ("0"),
332 desc ("tree_node_structure (&%h)")))
334 struct lang_identifier GTY ((tag ("1"))) identifier;
337 /* Track bindings and other things that matter for goto warnings. For
338 efficiency, we do not gather all the decls at the point of
339 definition. Instead, we point into the bindings structure. As
340 scopes are popped, we update these structures and gather the decls
341 that matter at that time. */
343 struct GTY(()) c_spot_bindings {
344 /* The currently open scope which holds bindings defined when the
345 label was defined or the goto statement was found. */
346 struct c_scope *scope;
347 /* The bindings in the scope field which were defined at the point
348 of the label or goto. This lets us look at older or newer
349 bindings in the scope, as appropriate. */
350 struct c_binding *bindings_in_scope;
351 /* The number of statement expressions that have started since this
352 label or goto statement was defined. This is zero if we are at
353 the same statement expression level. It is positive if we are in
354 a statement expression started since this spot. It is negative
355 if this spot was in a statement expression and we have left
358 /* Whether we started in a statement expression but are no longer in
359 it. This is set to true if stmt_exprs ever goes negative. */
363 /* This structure is used to keep track of bindings seen when a goto
364 statement is defined. This is only used if we see the goto
365 statement before we see the label. */
367 struct GTY(()) c_goto_bindings {
368 /* The location of the goto statement. */
370 /* The bindings of the goto statement. */
371 struct c_spot_bindings goto_bindings;
374 typedef struct c_goto_bindings *c_goto_bindings_p;
376 /* The additional information we keep track of for a label binding.
377 These fields are updated as scopes are popped. */
379 struct GTY(()) c_label_vars {
380 /* The shadowed c_label_vars, when one label shadows another (which
381 can only happen using a __label__ declaration). */
382 struct c_label_vars *shadowed;
383 /* The bindings when the label was defined. */
384 struct c_spot_bindings label_bindings;
385 /* A list of decls that we care about: decls about which we should
386 warn if a goto branches to this label from later in the function.
387 Decls are added to this list as scopes are popped. We only add
388 the decls that matter. */
389 vec<tree, va_gc> *decls_in_scope;
390 /* A list of goto statements to this label. This is only used for
391 goto statements seen before the label was defined, so that we can
392 issue appropriate warnings for them. */
393 vec<c_goto_bindings_p, va_gc> *gotos;
396 /* Each c_scope structure describes the complete contents of one
397 scope. Four scopes are distinguished specially: the innermost or
398 current scope, the innermost function scope, the file scope (always
399 the second to outermost) and the outermost or external scope.
401 Most declarations are recorded in the current scope.
403 All normal label declarations are recorded in the innermost
404 function scope, as are bindings of undeclared identifiers to
405 error_mark_node. (GCC permits nested functions as an extension,
406 hence the 'innermost' qualifier.) Explicitly declared labels
407 (using the __label__ extension) appear in the current scope.
409 Being in the file scope (current_scope == file_scope) causes
410 special behavior in several places below. Also, under some
411 conditions the Objective-C front end records declarations in the
412 file scope even though that isn't the current scope.
414 All declarations with external linkage are recorded in the external
415 scope, even if they aren't visible there; this models the fact that
416 such declarations are visible to the entire program, and (with a
417 bit of cleverness, see pushdecl) allows diagnosis of some violations
418 of C99 6.2.2p7 and 6.2.7p2:
420 If, within the same translation unit, the same identifier appears
421 with both internal and external linkage, the behavior is
424 All declarations that refer to the same object or function shall
425 have compatible type; otherwise, the behavior is undefined.
427 Initially only the built-in declarations, which describe compiler
428 intrinsic functions plus a subset of the standard library, are in
431 The order of the blocks list matters, and it is frequently appended
432 to. To avoid having to walk all the way to the end of the list on
433 each insertion, or reverse the list later, we maintain a pointer to
434 the last list entry. (FIXME: It should be feasible to use a reversed
437 The bindings list is strictly in reverse order of declarations;
438 pop_scope relies on this. */
441 struct GTY((chain_next ("%h.outer"))) c_scope {
442 /* The scope containing this one. */
443 struct c_scope *outer;
445 /* The next outermost function scope. */
446 struct c_scope *outer_function;
448 /* All bindings in this scope. */
449 struct c_binding *bindings;
451 /* For each scope (except the global one), a chain of BLOCK nodes
452 for all the scopes that were entered and exited one level down. */
456 /* The depth of this scope. Used to keep the ->shadowed chain of
457 bindings sorted innermost to outermost. */
458 unsigned int depth : 28;
460 /* True if we are currently filling this scope with parameter
462 BOOL_BITFIELD parm_flag : 1;
464 /* True if we saw [*] in this scope. Used to give an error messages
465 if these appears in a function definition. */
466 BOOL_BITFIELD had_vla_unspec : 1;
468 /* True if we already complained about forward parameter decls
469 in this scope. This prevents double warnings on
470 foo (int a; int b; ...) */
471 BOOL_BITFIELD warned_forward_parm_decls : 1;
473 /* True if this is the outermost block scope of a function body.
474 This scope contains the parameters, the local variables declared
475 in the outermost block, and all the labels (except those in
476 nested functions, or declared at block scope with __label__). */
477 BOOL_BITFIELD function_body : 1;
479 /* True means make a BLOCK for this scope no matter what. */
480 BOOL_BITFIELD keep : 1;
482 /* True means that an unsuffixed float constant is _Decimal64. */
483 BOOL_BITFIELD float_const_decimal64 : 1;
485 /* True if this scope has any label bindings. This is used to speed
486 up searching for labels when popping scopes, particularly since
487 labels are normally only found at function scope. */
488 BOOL_BITFIELD has_label_bindings : 1;
490 /* True if we should issue a warning if a goto statement crosses any
491 of the bindings. We still need to check the list of bindings to
492 find the specific ones we need to warn about. This is true if
493 decl_jump_unsafe would return true for any of the bindings. This
494 is used to avoid looping over all the bindings unnecessarily. */
495 BOOL_BITFIELD has_jump_unsafe_decl : 1;
498 /* The scope currently in effect. */
500 static GTY(()) struct c_scope *current_scope;
502 /* The innermost function scope. Ordinary (not explicitly declared)
503 labels, bindings to error_mark_node, and the lazily-created
504 bindings of __func__ and its friends get this scope. */
506 static GTY(()) struct c_scope *current_function_scope;
508 /* The C file scope. This is reset for each input translation unit. */
510 static GTY(()) struct c_scope *file_scope;
512 /* The outermost scope. This is used for all declarations with
513 external linkage, and only these, hence the name. */
515 static GTY(()) struct c_scope *external_scope;
517 /* A chain of c_scope structures awaiting reuse. */
519 static GTY((deletable)) struct c_scope *scope_freelist;
521 /* A chain of c_binding structures awaiting reuse. */
523 static GTY((deletable)) struct c_binding *binding_freelist;
525 /* Append VAR to LIST in scope SCOPE. */
526 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
527 struct c_scope *s_ = (scope); \
529 if (s_->list##_last) \
530 BLOCK_CHAIN (s_->list##_last) = d_; \
533 s_->list##_last = d_; \
536 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
537 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
538 struct c_scope *t_ = (tscope); \
539 struct c_scope *f_ = (fscope); \
541 BLOCK_CHAIN (t_->to##_last) = f_->from; \
544 t_->to##_last = f_->from##_last; \
547 /* A c_inline_static structure stores details of a static identifier
548 referenced in a definition of a function that may be an inline
549 definition if no subsequent declaration of that function uses
550 "extern" or does not use "inline". */
552 struct GTY((chain_next ("%h.next"))) c_inline_static {
553 /* The location for a diagnostic. */
556 /* The function that may be an inline definition. */
559 /* The object or function referenced. */
562 /* What sort of reference this is. */
563 enum c_inline_static_type type;
565 /* The next such structure or NULL. */
566 struct c_inline_static *next;
569 /* List of static identifiers used or referenced in functions that may
570 be inline definitions. */
571 static GTY(()) struct c_inline_static *c_inline_statics;
573 /* True means unconditionally make a BLOCK for the next scope pushed. */
575 static bool keep_next_level_flag;
577 /* True means the next call to push_scope will be the outermost scope
578 of a function body, so do not push a new scope, merely cease
579 expecting parameter decls. */
581 static bool next_is_function_body;
583 /* A vector of pointers to c_binding structures. */
585 typedef struct c_binding *c_binding_ptr;
587 /* Information that we keep for a struct or union while it is being
590 class c_struct_parse_info
593 /* If warn_cxx_compat, a list of types defined within this
595 auto_vec<tree> struct_types;
596 /* If warn_cxx_compat, a list of field names which have bindings,
597 and which are defined in this struct, but which are not defined
598 in any enclosing struct. This is used to clear the in_struct
599 field of the c_bindings structure. */
600 auto_vec<c_binding_ptr> fields;
601 /* If warn_cxx_compat, a list of typedef names used when defining
602 fields in this struct. */
603 auto_vec<tree> typedefs_seen;
606 /* Information for the struct or union currently being parsed, or
607 NULL if not parsing a struct or union. */
608 static class c_struct_parse_info *struct_parse_info;
610 /* Forward declarations. */
611 static tree lookup_name_in_scope (tree, struct c_scope *);
612 static tree c_make_fname_decl (location_t, tree, int);
613 static tree grokdeclarator (const struct c_declarator *,
614 struct c_declspecs *,
615 enum decl_context, bool, tree *, tree *, tree *,
616 bool *, enum deprecated_states);
617 static tree grokparms (struct c_arg_info *, bool);
618 static void layout_array_type (tree);
619 static void warn_defaults_to (location_t, int, const char *, ...)
620 ATTRIBUTE_GCC_DIAG(3,4);
621 static const char *header_for_builtin_fn (tree);
623 /* T is a statement. Add it to the statement-tree. This is the
624 C/ObjC version--C++ has a slightly different version of this
630 enum tree_code code = TREE_CODE (t);
632 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
634 if (!EXPR_HAS_LOCATION (t))
635 SET_EXPR_LOCATION (t, input_location);
638 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
639 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
641 /* Add T to the statement-tree. Non-side-effect statements need to be
642 recorded during statement expressions. */
643 if (!building_stmt_list_p ())
645 append_to_statement_list_force (t, &cur_stmt_list);
650 /* Build a pointer type using the default pointer mode. */
653 c_build_pointer_type (tree to_type)
655 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
656 : TYPE_ADDR_SPACE (to_type);
657 machine_mode pointer_mode;
659 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
660 pointer_mode = targetm.addr_space.pointer_mode (as);
662 pointer_mode = c_default_pointer_mode;
663 return build_pointer_type_for_mode (to_type, pointer_mode, false);
667 /* Return true if we will want to say something if a goto statement
671 decl_jump_unsafe (tree decl)
673 if (error_operand_p (decl))
676 /* Don't warn for compound literals. If a goto statement crosses
677 their initialization, it should cross also all the places where
678 the complit is used or where the complit address might be saved
679 into some variable, so code after the label to which goto jumps
680 should not be able to refer to the compound literal. */
681 if (VAR_P (decl) && C_DECL_COMPOUND_LITERAL_P (decl))
684 /* Always warn about crossing variably modified types. */
685 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
686 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
689 /* Otherwise, only warn if -Wgoto-misses-init and this is an
690 initialized automatic decl. */
691 if (warn_jump_misses_init
693 && !TREE_STATIC (decl)
694 && DECL_INITIAL (decl) != NULL_TREE)
702 c_print_identifier (FILE *file, tree node, int indent)
704 void (*save) (enum c_oracle_request, tree identifier);
706 /* Temporarily hide any binding oracle. Without this, calls to
707 debug_tree from the debugger will end up calling into the oracle,
708 making for a confusing debug session. As the oracle isn't needed
709 here for normal operation, it's simplest to suppress it. */
710 save = c_binding_oracle;
711 c_binding_oracle = NULL;
713 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
714 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
715 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
716 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
718 tree rid = ridpointers[C_RID_CODE (node)];
719 indent_to (file, indent + 4);
720 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
721 (void *) rid, IDENTIFIER_POINTER (rid));
724 c_binding_oracle = save;
727 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
728 which may be any of several kinds of DECL or TYPE or error_mark_node,
729 in the scope SCOPE. */
731 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
732 bool nested, location_t locus)
734 struct c_binding *b, **here;
736 if (binding_freelist)
738 b = binding_freelist;
739 binding_freelist = b->prev;
742 b = ggc_alloc<c_binding> ();
747 b->depth = scope->depth;
748 b->invisible = invisible;
756 b->prev = scope->bindings;
759 if (decl_jump_unsafe (decl))
760 scope->has_jump_unsafe_decl = 1;
765 switch (TREE_CODE (decl))
767 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
770 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
776 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
782 /* Locate the appropriate place in the chain of shadowed decls
783 to insert this binding. Normally, scope == current_scope and
784 this does nothing. */
785 while (*here && (*here)->depth > scope->depth)
786 here = &(*here)->shadowed;
792 /* Clear the binding structure B, stick it on the binding_freelist,
793 and return the former value of b->prev. This is used by pop_scope
794 and get_parm_info to iterate destructively over all the bindings
795 from a given scope. */
796 static struct c_binding *
797 free_binding_and_advance (struct c_binding *b)
799 struct c_binding *prev = b->prev;
801 memset (b, 0, sizeof (struct c_binding));
802 b->prev = binding_freelist;
803 binding_freelist = b;
808 /* Bind a label. Like bind, but skip fields which aren't used for
809 labels, and add the LABEL_VARS value. */
811 bind_label (tree name, tree label, struct c_scope *scope,
812 struct c_label_vars *label_vars)
816 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
819 scope->has_label_bindings = true;
822 gcc_assert (b->decl == label);
823 label_vars->shadowed = b->u.label;
824 b->u.label = label_vars;
827 /* Hook called at end of compilation to assume 1 elt
828 for a file-scope tentative array defn that wasn't complete before. */
831 c_finish_incomplete_decl (tree decl)
835 tree type = TREE_TYPE (decl);
836 if (type != error_mark_node
837 && TREE_CODE (type) == ARRAY_TYPE
838 && !DECL_EXTERNAL (decl)
839 && TYPE_DOMAIN (type) == NULL_TREE)
841 warning_at (DECL_SOURCE_LOCATION (decl),
842 0, "array %q+D assumed to have one element", decl);
844 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
846 relayout_decl (decl);
851 /* Record that inline function FUNC contains a reference (location
852 LOC) to static DECL (file-scope or function-local according to
856 record_inline_static (location_t loc, tree func, tree decl,
857 enum c_inline_static_type type)
859 c_inline_static *csi = ggc_alloc<c_inline_static> ();
861 csi->function = func;
862 csi->static_decl = decl;
864 csi->next = c_inline_statics;
865 c_inline_statics = csi;
868 /* Check for references to static declarations in inline functions at
869 the end of the translation unit and diagnose them if the functions
870 are still inline definitions. */
873 check_inline_statics (void)
875 struct c_inline_static *csi;
876 for (csi = c_inline_statics; csi; csi = csi->next)
878 if (DECL_EXTERNAL (csi->function))
882 pedwarn (csi->location, 0,
883 "%qD is static but used in inline function %qD "
884 "which is not static", csi->static_decl, csi->function);
887 pedwarn (csi->location, 0,
888 "%q+D is static but declared in inline function %qD "
889 "which is not static", csi->static_decl, csi->function);
895 c_inline_statics = NULL;
898 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
899 for the current state, otherwise set it to uninitialized. */
902 set_spot_bindings (struct c_spot_bindings *p, bool defining)
906 p->scope = current_scope;
907 p->bindings_in_scope = current_scope->bindings;
912 p->bindings_in_scope = NULL;
915 p->left_stmt_expr = false;
918 /* Update spot bindings P as we pop out of SCOPE. Return true if we
919 should push decls for a label. */
922 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
924 if (p->scope != scope)
926 /* This label or goto is defined in some other scope, or it is a
927 label which is not yet defined. There is nothing to
932 /* Adjust the spot bindings to refer to the bindings already defined
933 in the enclosing scope. */
934 p->scope = scope->outer;
935 p->bindings_in_scope = p->scope->bindings;
940 /* The Objective-C front-end often needs to determine the current scope. */
943 objc_get_current_scope (void)
945 return current_scope;
948 /* The following function is used only by Objective-C. It needs to live here
949 because it accesses the innards of c_scope. */
952 objc_mark_locals_volatile (void *enclosing_blk)
954 struct c_scope *scope;
957 for (scope = current_scope;
958 scope && scope != enclosing_blk;
959 scope = scope->outer)
961 for (b = scope->bindings; b; b = b->prev)
962 objc_volatilize_decl (b->decl);
964 /* Do not climb up past the current function. */
965 if (scope->function_body)
970 /* Return true if we are in the global binding level. */
973 global_bindings_p (void)
975 return current_scope == file_scope;
978 /* Return true if we're declaring parameters in an old-style function
982 old_style_parameter_scope (void)
984 /* If processing parameters and there is no function statement list, we
985 * have an old-style function declaration. */
986 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
990 keep_next_level (void)
992 keep_next_level_flag = true;
995 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
998 set_float_const_decimal64 (void)
1000 current_scope->float_const_decimal64 = true;
1003 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1006 clear_float_const_decimal64 (void)
1008 current_scope->float_const_decimal64 = false;
1011 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1014 float_const_decimal64_p (void)
1016 return current_scope->float_const_decimal64;
1019 /* Identify this scope as currently being filled with parameters. */
1022 declare_parm_level (void)
1024 current_scope->parm_flag = true;
1030 if (next_is_function_body)
1032 /* This is the transition from the parameters to the top level
1033 of the function body. These are the same scope
1034 (C99 6.2.1p4,6) so we do not push another scope structure.
1035 next_is_function_body is set only by store_parm_decls, which
1036 in turn is called when and only when we are about to
1037 encounter the opening curly brace for the function body.
1039 The outermost block of a function always gets a BLOCK node,
1040 because the debugging output routines expect that each
1041 function has at least one BLOCK. */
1042 current_scope->parm_flag = false;
1043 current_scope->function_body = true;
1044 current_scope->keep = true;
1045 current_scope->outer_function = current_function_scope;
1046 current_function_scope = current_scope;
1048 keep_next_level_flag = false;
1049 next_is_function_body = false;
1051 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1052 if (current_scope->outer)
1053 current_scope->float_const_decimal64
1054 = current_scope->outer->float_const_decimal64;
1056 current_scope->float_const_decimal64 = false;
1060 struct c_scope *scope;
1063 scope = scope_freelist;
1064 scope_freelist = scope->outer;
1067 scope = ggc_cleared_alloc<c_scope> ();
1069 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1071 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1073 scope->float_const_decimal64 = false;
1075 scope->keep = keep_next_level_flag;
1076 scope->outer = current_scope;
1077 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1079 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1081 if (current_scope && scope->depth == 0)
1084 sorry ("GCC supports only %u nested scopes", scope->depth);
1087 current_scope = scope;
1088 keep_next_level_flag = false;
1092 /* This is called when we are leaving SCOPE. For each label defined
1093 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1094 These are the decls whose initialization will be skipped by a goto
1095 later in the function. */
1098 update_label_decls (struct c_scope *scope)
1105 if (s->has_label_bindings)
1107 struct c_binding *b;
1109 for (b = s->bindings; b != NULL; b = b->prev)
1111 struct c_label_vars *label_vars;
1112 struct c_binding *b1;
1115 struct c_goto_bindings *g;
1117 if (TREE_CODE (b->decl) != LABEL_DECL)
1119 label_vars = b->u.label;
1121 b1 = label_vars->label_bindings.bindings_in_scope;
1122 if (label_vars->label_bindings.scope == NULL)
1125 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1126 if (update_spot_bindings (scope, &label_vars->label_bindings))
1128 /* This label is defined in this scope. */
1131 for (; b1 != NULL; b1 = b1->prev)
1133 /* A goto from later in the function to this
1134 label will never see the initialization
1135 of B1, if any. Save it to issue a
1136 warning if needed. */
1137 if (decl_jump_unsafe (b1->decl))
1138 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1143 /* Update the bindings of any goto statements associated
1145 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1146 update_spot_bindings (scope, &g->goto_bindings);
1150 /* Don't search beyond the current function. */
1151 if (s == current_function_scope)
1158 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1161 set_type_context (tree type, tree context)
1163 for (type = TYPE_MAIN_VARIANT (type); type;
1164 type = TYPE_NEXT_VARIANT (type))
1165 TYPE_CONTEXT (type) = context;
1168 /* Exit a scope. Restore the state of the identifier-decl mappings
1169 that were in effect when this scope was entered. Return a BLOCK
1170 node containing all the DECLs in this scope that are of interest
1171 to debug info generation. */
1176 struct c_scope *scope = current_scope;
1177 tree block, context, p;
1178 struct c_binding *b;
1180 bool functionbody = scope->function_body;
1181 bool keep = functionbody || scope->keep || scope->bindings;
1183 update_label_decls (scope);
1185 /* If appropriate, create a BLOCK to record the decls for the life
1186 of this function. */
1190 block = make_node (BLOCK);
1191 BLOCK_SUBBLOCKS (block) = scope->blocks;
1192 TREE_USED (block) = 1;
1194 /* In each subblock, record that this is its superior. */
1195 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1196 BLOCK_SUPERCONTEXT (p) = block;
1198 BLOCK_VARS (block) = NULL_TREE;
1201 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1202 scope must be set so that they point to the appropriate
1203 construct, i.e. either to the current FUNCTION_DECL node, or
1204 else to the BLOCK node we just constructed.
1206 Note that for tagged types whose scope is just the formal
1207 parameter list for some function type specification, we can't
1208 properly set their TYPE_CONTEXTs here, because we don't have a
1209 pointer to the appropriate FUNCTION_TYPE node readily available
1210 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1211 type nodes get set in `grokdeclarator' as soon as we have created
1212 the FUNCTION_TYPE node which will represent the "scope" for these
1213 "parameter list local" tagged types. */
1214 if (scope->function_body)
1215 context = current_function_decl;
1216 else if (scope == file_scope)
1219 = build_translation_unit_decl (get_identifier (main_input_filename));
1220 context = file_decl;
1221 debug_hooks->register_main_translation_unit (file_decl);
1226 /* Clear all bindings in this scope. */
1227 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1230 switch (TREE_CODE (p))
1233 /* Warnings for unused labels, errors for undefined labels. */
1234 if (TREE_USED (p) && !DECL_INITIAL (p))
1236 error ("label %q+D used but not defined", p);
1237 DECL_INITIAL (p) = error_mark_node;
1240 warn_for_unused_label (p);
1242 /* Labels go in BLOCK_VARS. */
1243 DECL_CHAIN (p) = BLOCK_VARS (block);
1244 BLOCK_VARS (block) = p;
1245 gcc_assert (I_LABEL_BINDING (b->id) == b);
1246 I_LABEL_BINDING (b->id) = b->shadowed;
1248 /* Also pop back to the shadowed label_vars. */
1249 release_tree_vector (b->u.label->decls_in_scope);
1250 b->u.label = b->u.label->shadowed;
1256 set_type_context (p, context);
1258 /* Types may not have tag-names, in which case the type
1259 appears in the bindings list with b->id NULL. */
1262 gcc_assert (I_TAG_BINDING (b->id) == b);
1263 I_TAG_BINDING (b->id) = b->shadowed;
1268 /* Propagate TREE_ADDRESSABLE from nested functions to their
1269 containing functions. */
1270 if (!TREE_ASM_WRITTEN (p)
1271 && DECL_INITIAL (p) != NULL_TREE
1272 && TREE_ADDRESSABLE (p)
1273 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1274 && DECL_ABSTRACT_ORIGIN (p) != p)
1275 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1276 if (!TREE_PUBLIC (p)
1277 && !DECL_INITIAL (p)
1279 && scope != file_scope
1280 && scope != external_scope)
1282 error ("nested function %q+D declared but never defined", p);
1283 undef_nested_function = true;
1285 else if (DECL_DECLARED_INLINE_P (p)
1287 && !DECL_INITIAL (p))
1289 /* C99 6.7.4p6: "a function with external linkage... declared
1290 with an inline function specifier ... shall also be defined
1291 in the same translation unit." */
1292 if (!flag_gnu89_inline
1293 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1294 && scope == external_scope)
1295 pedwarn (input_location, 0,
1296 "inline function %q+D declared but never defined", p);
1297 DECL_EXTERNAL (p) = 1;
1303 /* Warnings for unused variables. */
1304 if ((!TREE_USED (p) || !DECL_READ_P (p))
1305 && !warning_suppressed_p (p, OPT_Wunused_but_set_variable)
1306 && !DECL_IN_SYSTEM_HEADER (p)
1308 && !DECL_ARTIFICIAL (p)
1309 && scope != file_scope
1310 && scope != external_scope)
1313 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1314 else if (DECL_CONTEXT (p) == current_function_decl)
1315 warning_at (DECL_SOURCE_LOCATION (p),
1316 OPT_Wunused_but_set_variable,
1317 "variable %qD set but not used", p);
1322 error ("type of array %q+D completed incompatibly with"
1323 " implicit initialization", p);
1330 /* All of these go in BLOCK_VARS, but only if this is the
1331 binding in the home scope. */
1334 DECL_CHAIN (p) = BLOCK_VARS (block);
1335 BLOCK_VARS (block) = p;
1337 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1339 /* For block local externs add a special
1340 DECL_EXTERNAL decl for debug info generation. */
1341 tree extp = copy_node (p);
1343 DECL_EXTERNAL (extp) = 1;
1344 TREE_STATIC (extp) = 0;
1345 TREE_PUBLIC (extp) = 1;
1346 DECL_INITIAL (extp) = NULL_TREE;
1347 DECL_LANG_SPECIFIC (extp) = NULL;
1348 DECL_CONTEXT (extp) = current_function_decl;
1349 if (TREE_CODE (p) == FUNCTION_DECL)
1351 DECL_RESULT (extp) = NULL_TREE;
1352 DECL_SAVED_TREE (extp) = NULL_TREE;
1353 DECL_STRUCT_FUNCTION (extp) = NULL;
1355 if (b->locus != UNKNOWN_LOCATION)
1356 DECL_SOURCE_LOCATION (extp) = b->locus;
1357 DECL_CHAIN (extp) = BLOCK_VARS (block);
1358 BLOCK_VARS (block) = extp;
1360 /* If this is the file scope set DECL_CONTEXT of each decl to
1361 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1363 if (scope == file_scope)
1365 DECL_CONTEXT (p) = context;
1366 if (TREE_CODE (p) == TYPE_DECL
1367 && TREE_TYPE (p) != error_mark_node)
1368 set_type_context (TREE_TYPE (p), context);
1372 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1373 already been put there by store_parm_decls. Unused-
1374 parameter warnings are handled by function.cc.
1375 error_mark_node obviously does not go in BLOCK_VARS and
1376 does not get unused-variable warnings. */
1379 /* It is possible for a decl not to have a name. We get
1380 here with b->id NULL in this case. */
1383 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1384 I_SYMBOL_BINDING (b->id) = b->shadowed;
1385 if (b->shadowed && b->shadowed->u.type)
1386 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1396 /* Dispose of the block that we just made inside some higher level. */
1397 if ((scope->function_body || scope == file_scope) && context)
1399 DECL_INITIAL (context) = block;
1400 BLOCK_SUPERCONTEXT (block) = context;
1402 else if (scope->outer)
1405 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1406 /* If we did not make a block for the scope just exited, any
1407 blocks made for inner scopes must be carried forward so they
1408 will later become subblocks of something else. */
1409 else if (scope->blocks)
1410 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1413 /* Pop the current scope, and free the structure for reuse. */
1414 current_scope = scope->outer;
1415 if (scope->function_body)
1416 current_function_scope = scope->outer_function;
1418 memset (scope, 0, sizeof (struct c_scope));
1419 scope->outer = scope_freelist;
1420 scope_freelist = scope;
1426 push_file_scope (void)
1434 file_scope = current_scope;
1436 start_fname_decls ();
1438 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1439 bind (DECL_NAME (decl), decl, file_scope,
1440 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1444 pop_file_scope (void)
1446 /* In case there were missing closebraces, get us back to the global
1448 while (current_scope != file_scope)
1451 /* __FUNCTION__ is defined at file scope (""). This
1452 call may not be necessary as my tests indicate it
1453 still works without it. */
1454 finish_fname_decls ();
1456 check_inline_statics ();
1458 /* This is the point to write out a PCH if we're doing that.
1459 In that case we do not want to do anything else. */
1462 c_common_write_pch ();
1463 /* Ensure even the callers don't try to finalize the CU. */
1464 flag_syntax_only = 1;
1468 /* Pop off the file scope and close this translation unit. */
1472 maybe_apply_pending_pragma_weaks ();
1475 /* Adjust the bindings for the start of a statement expression. */
1478 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1480 struct c_scope *scope;
1482 for (scope = current_scope; scope != NULL; scope = scope->outer)
1484 struct c_binding *b;
1486 if (!scope->has_label_bindings)
1489 for (b = scope->bindings; b != NULL; b = b->prev)
1491 struct c_label_vars *label_vars;
1493 struct c_goto_bindings *g;
1495 if (TREE_CODE (b->decl) != LABEL_DECL)
1497 label_vars = b->u.label;
1498 ++label_vars->label_bindings.stmt_exprs;
1499 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1500 ++g->goto_bindings.stmt_exprs;
1504 if (switch_bindings != NULL)
1505 ++switch_bindings->stmt_exprs;
1508 /* Adjust the bindings for the end of a statement expression. */
1511 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1513 struct c_scope *scope;
1515 for (scope = current_scope; scope != NULL; scope = scope->outer)
1517 struct c_binding *b;
1519 if (!scope->has_label_bindings)
1522 for (b = scope->bindings; b != NULL; b = b->prev)
1524 struct c_label_vars *label_vars;
1526 struct c_goto_bindings *g;
1528 if (TREE_CODE (b->decl) != LABEL_DECL)
1530 label_vars = b->u.label;
1531 --label_vars->label_bindings.stmt_exprs;
1532 if (label_vars->label_bindings.stmt_exprs < 0)
1534 label_vars->label_bindings.left_stmt_expr = true;
1535 label_vars->label_bindings.stmt_exprs = 0;
1537 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1539 --g->goto_bindings.stmt_exprs;
1540 if (g->goto_bindings.stmt_exprs < 0)
1542 g->goto_bindings.left_stmt_expr = true;
1543 g->goto_bindings.stmt_exprs = 0;
1549 if (switch_bindings != NULL)
1551 --switch_bindings->stmt_exprs;
1552 gcc_assert (switch_bindings->stmt_exprs >= 0);
1556 /* Push a definition or a declaration of struct, union or enum tag "name".
1557 "type" should be the type node.
1558 We assume that the tag "name" is not already defined, and has a location
1561 Note that the definition may really be just a forward reference.
1562 In that case, the TYPE_SIZE will be zero. */
1565 pushtag (location_t loc, tree name, tree type)
1567 /* Record the identifier as the type's name if it has none. */
1568 if (name && !TYPE_NAME (type))
1569 TYPE_NAME (type) = name;
1570 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1572 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1573 tagged type we just added to the current scope. This fake
1574 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1575 to output a representation of a tagged type, and it also gives
1576 us a convenient place to record the "scope start" address for the
1579 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1580 TYPE_DECL, NULL_TREE, type));
1582 /* An approximation for now, so we can tell this is a function-scope tag.
1583 This will be updated in pop_scope. */
1584 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1586 if (warn_cxx_compat && name != NULL_TREE)
1588 struct c_binding *b = I_SYMBOL_BINDING (name);
1591 && b->decl != NULL_TREE
1592 && TREE_CODE (b->decl) == TYPE_DECL
1593 && (B_IN_CURRENT_SCOPE (b)
1594 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1595 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1596 != TYPE_MAIN_VARIANT (type)))
1598 auto_diagnostic_group d;
1599 if (warning_at (loc, OPT_Wc___compat,
1600 ("using %qD as both a typedef and a tag is "
1601 "invalid in C++"), b->decl)
1602 && b->locus != UNKNOWN_LOCATION)
1603 inform (b->locus, "originally defined here");
1608 /* An exported interface to pushtag. This is used by the gdb plugin's
1609 binding oracle to introduce a new tag binding. */
1612 c_pushtag (location_t loc, tree name, tree type)
1614 pushtag (loc, name, type);
1617 /* An exported interface to bind a declaration. LOC is the location
1618 to use. DECL is the declaration to bind. The decl's name is used
1619 to determine how it is bound. If DECL is a VAR_DECL, then
1620 IS_GLOBAL determines whether the decl is put into the global (file
1621 and external) scope or the current function's scope; if DECL is not
1622 a VAR_DECL then it is always put into the file scope. */
1625 c_bind (location_t loc, tree decl, bool is_global)
1627 struct c_scope *scope;
1628 bool nested = false;
1630 if (!VAR_P (decl) || current_function_scope == NULL)
1632 /* Types and functions are always considered to be global. */
1634 DECL_EXTERNAL (decl) = 1;
1635 TREE_PUBLIC (decl) = 1;
1639 /* Also bind it into the external scope. */
1640 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1643 DECL_EXTERNAL (decl) = 1;
1644 TREE_PUBLIC (decl) = 1;
1648 DECL_CONTEXT (decl) = current_function_decl;
1649 TREE_PUBLIC (decl) = 0;
1650 scope = current_function_scope;
1653 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1657 /* Stores the first FILE*, const struct tm* etc. argument type (whatever
1658 it is) seen in a declaration of a file I/O etc. built-in, corresponding
1659 to the builtin_structptr_types array. Subsequent declarations of such
1660 built-ins are expected to refer to it rather than to fileptr_type_node,
1661 etc. which is just void* (or to any other type).
1662 Used only by match_builtin_function_types. */
1664 static const unsigned builtin_structptr_type_count
1665 = ARRAY_SIZE (builtin_structptr_types);
1667 static GTY(()) tree last_structptr_types[builtin_structptr_type_count];
1669 /* Returns true if types T1 and T2 representing return types or types
1670 of function arguments are close enough to be considered interchangeable
1671 in redeclarations of built-in functions. */
1674 types_close_enough_to_match (tree t1, tree t2)
1676 return (TYPE_MODE (t1) == TYPE_MODE (t2)
1677 && POINTER_TYPE_P (t1) == POINTER_TYPE_P (t2)
1678 && FUNCTION_POINTER_TYPE_P (t1) == FUNCTION_POINTER_TYPE_P (t2));
1681 /* Subroutine of compare_decls. Allow harmless mismatches in return
1682 and argument types provided that the type modes match. Set *STRICT
1683 and *ARGNO to the expected argument type and number in case of
1684 an argument type mismatch or null and zero otherwise. Return
1685 a unified type given a suitable match, and 0 otherwise. */
1688 match_builtin_function_types (tree newtype, tree oldtype,
1689 tree *strict, unsigned *argno)
1692 *strict = NULL_TREE;
1694 /* Accept the return type of the new declaration if it has the same
1695 mode and if they're both pointers or if neither is. */
1696 tree oldrettype = TREE_TYPE (oldtype);
1697 tree newrettype = TREE_TYPE (newtype);
1699 if (!types_close_enough_to_match (oldrettype, newrettype))
1702 /* Check that the return types are compatible but don't fail if they
1703 are not (e.g., int vs long in ILP32) and just let the caller know. */
1704 if (!comptypes (TYPE_MAIN_VARIANT (oldrettype),
1705 TYPE_MAIN_VARIANT (newrettype)))
1706 *strict = oldrettype;
1708 tree oldargs = TYPE_ARG_TYPES (oldtype);
1709 tree newargs = TYPE_ARG_TYPES (newtype);
1710 tree tryargs = newargs;
1712 const unsigned nlst = ARRAY_SIZE (last_structptr_types);
1713 const unsigned nbst = ARRAY_SIZE (builtin_structptr_types);
1715 gcc_checking_assert (nlst == nbst);
1717 for (unsigned i = 1; oldargs || newargs; ++i)
1721 || !TREE_VALUE (oldargs)
1722 || !TREE_VALUE (newargs))
1725 tree oldtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1726 tree newtype = TREE_VALUE (newargs);
1727 if (newtype == error_mark_node)
1729 newtype = TYPE_MAIN_VARIANT (newtype);
1731 if (!types_close_enough_to_match (oldtype, newtype))
1735 if (POINTER_TYPE_P (oldtype))
1736 /* Iterate over well-known struct types like FILE (whose types
1737 aren't known to us) and compare the pointer to each to
1738 the pointer argument. */
1739 for (j = 0; j < nbst; ++j)
1741 if (TREE_VALUE (oldargs) != builtin_structptr_types[j].node)
1743 /* Store the first FILE* etc. argument type (whatever it is), and
1744 expect any subsequent declarations of file I/O etc. built-ins
1745 to refer to it rather than to fileptr_type_node etc. which is
1746 just void* (or const void*). */
1747 if (last_structptr_types[j])
1749 if (!comptypes (last_structptr_types[j], newtype))
1752 *strict = last_structptr_types[j];
1756 last_structptr_types[j] = newtype;
1760 if (j == nbst && !comptypes (oldtype, newtype))
1762 if (POINTER_TYPE_P (oldtype))
1764 /* For incompatible pointers, only reject differences in
1765 the unqualified variants of the referenced types but
1766 consider differences in qualifiers as benign (report
1767 those to caller via *STRICT below). */
1768 tree oldref = TYPE_MAIN_VARIANT (TREE_TYPE (oldtype));
1769 tree newref = TYPE_MAIN_VARIANT (TREE_TYPE (newtype));
1770 if (!comptypes (oldref, newref))
1781 oldargs = TREE_CHAIN (oldargs);
1782 newargs = TREE_CHAIN (newargs);
1785 tree trytype = build_function_type (newrettype, tryargs);
1787 /* Allow declaration to change transaction_safe attribute. */
1788 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1789 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1790 tree newattrs = TYPE_ATTRIBUTES (newtype);
1791 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1792 if (oldtsafe && !newtsafe)
1793 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1794 else if (newtsafe && !oldtsafe)
1795 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1796 NULL_TREE, oldattrs);
1798 return build_type_attribute_variant (trytype, oldattrs);
1801 /* Subroutine of diagnose_mismatched_decls. Check for function type
1802 mismatch involving an empty arglist vs a nonempty one and give clearer
1805 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1806 tree newtype, tree oldtype)
1810 if (TREE_CODE (olddecl) != FUNCTION_DECL
1811 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1812 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1813 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1816 t = TYPE_ARG_TYPES (oldtype);
1818 t = TYPE_ARG_TYPES (newtype);
1819 for (; t; t = TREE_CHAIN (t))
1821 tree type = TREE_VALUE (t);
1823 if (TREE_CHAIN (t) == NULL_TREE
1824 && TYPE_MAIN_VARIANT (type) != void_type_node)
1826 inform (input_location, "a parameter list with an ellipsis "
1827 "cannot match an empty parameter name list declaration");
1831 if (c_type_promotes_to (type) != type)
1833 inform (input_location, "an argument type that has a default "
1834 "promotion cannot match an empty parameter name list "
1841 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1842 old-style function definition, NEWDECL is a prototype declaration.
1843 Diagnose inconsistencies in the argument list. Returns TRUE if
1844 the prototype is compatible, FALSE if not. */
1846 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1848 tree newargs, oldargs;
1851 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1853 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1854 newargs = TYPE_ARG_TYPES (newtype);
1859 tree oldargtype = TREE_VALUE (oldargs);
1860 tree newargtype = TREE_VALUE (newargs);
1862 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1865 oldargtype = (TYPE_ATOMIC (oldargtype)
1866 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1868 : TYPE_MAIN_VARIANT (oldargtype));
1869 newargtype = (TYPE_ATOMIC (newargtype)
1870 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1872 : TYPE_MAIN_VARIANT (newargtype));
1874 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1877 /* Reaching the end of just one list means the two decls don't
1878 agree on the number of arguments. */
1879 if (END_OF_ARGLIST (oldargtype))
1881 error ("prototype for %q+D declares more arguments "
1882 "than previous old-style definition", newdecl);
1885 else if (END_OF_ARGLIST (newargtype))
1887 error ("prototype for %q+D declares fewer arguments "
1888 "than previous old-style definition", newdecl);
1892 /* Type for passing arg must be consistent with that declared
1894 else if (!comptypes (oldargtype, newargtype))
1896 error ("prototype for %q+D declares argument %d"
1897 " with incompatible type",
1902 oldargs = TREE_CHAIN (oldargs);
1903 newargs = TREE_CHAIN (newargs);
1907 /* If we get here, no errors were found, but do issue a warning
1908 for this poor-style construct. */
1909 warning (0, "prototype for %q+D follows non-prototype definition",
1912 #undef END_OF_ARGLIST
1915 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1916 first in a pair of mismatched declarations, using the diagnostic
1919 locate_old_decl (tree decl)
1921 if (TREE_CODE (decl) == FUNCTION_DECL
1922 && fndecl_built_in_p (decl)
1923 && !C_DECL_DECLARED_BUILTIN (decl))
1925 else if (DECL_INITIAL (decl))
1926 inform (input_location,
1927 "previous definition of %q+D with type %qT",
1928 decl, TREE_TYPE (decl));
1929 else if (C_DECL_IMPLICIT (decl))
1930 inform (input_location,
1931 "previous implicit declaration of %q+D with type %qT",
1932 decl, TREE_TYPE (decl));
1934 inform (input_location,
1935 "previous declaration of %q+D with type %qT",
1936 decl, TREE_TYPE (decl));
1939 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1940 Returns true if the caller should proceed to merge the two, false
1941 if OLDDECL should simply be discarded. As a side effect, issues
1942 all necessary diagnostics for invalid or poor-style combinations.
1943 If it returns true, writes the types of NEWDECL and OLDDECL to
1944 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1945 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1948 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1949 tree *newtypep, tree *oldtypep)
1951 tree newtype, oldtype;
1954 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1955 && DECL_EXTERNAL (DECL))
1957 /* If we have error_mark_node for either decl or type, just discard
1958 the previous decl - we're in an error cascade already. */
1959 if (olddecl == error_mark_node || newdecl == error_mark_node)
1961 *oldtypep = oldtype = TREE_TYPE (olddecl);
1962 *newtypep = newtype = TREE_TYPE (newdecl);
1963 if (oldtype == error_mark_node || newtype == error_mark_node)
1966 /* Two different categories of symbol altogether. This is an error
1967 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1968 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1970 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1971 && fndecl_built_in_p (olddecl)
1972 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1974 auto_diagnostic_group d;
1975 error ("%q+D redeclared as different kind of symbol", newdecl);
1976 locate_old_decl (olddecl);
1978 else if (TREE_PUBLIC (newdecl))
1979 warning (OPT_Wbuiltin_declaration_mismatch,
1980 "built-in function %q+D declared as non-function",
1983 warning (OPT_Wshadow, "declaration of %q+D shadows "
1984 "a built-in function", newdecl);
1988 /* Enumerators have no linkage, so may only be declared once in a
1990 if (TREE_CODE (olddecl) == CONST_DECL)
1992 auto_diagnostic_group d;
1993 error ("redeclaration of enumerator %q+D", newdecl);
1994 locate_old_decl (olddecl);
1998 bool pedwarned = false;
1999 bool warned = false;
2000 bool enum_and_int_p = false;
2001 auto_diagnostic_group d;
2003 int comptypes_result = comptypes_check_enum_int (oldtype, newtype,
2005 if (!comptypes_result)
2007 if (TREE_CODE (olddecl) == FUNCTION_DECL
2008 && fndecl_built_in_p (olddecl, BUILT_IN_NORMAL)
2009 && !C_DECL_DECLARED_BUILTIN (olddecl))
2011 /* Accept "harmless" mismatches in function types such
2012 as missing qualifiers or int vs long when they're the same
2013 size. However, diagnose return and argument types that are
2014 incompatible according to language rules. */
2015 tree mismatch_expect;
2016 unsigned mismatch_argno;
2018 tree trytype = match_builtin_function_types (newtype, oldtype,
2022 if (trytype && comptypes (newtype, trytype))
2023 *oldtypep = oldtype = trytype;
2026 /* If types don't match for a built-in, throw away the
2027 built-in. No point in calling locate_old_decl here, it
2028 won't print anything. */
2029 const char *header = header_for_builtin_fn (olddecl);
2030 location_t loc = DECL_SOURCE_LOCATION (newdecl);
2031 if (warning_at (loc, OPT_Wbuiltin_declaration_mismatch,
2032 "conflicting types for built-in function %q+D; "
2037 /* Suggest the right header to include as the preferred
2038 solution rather than the spelling of the declaration. */
2039 rich_location richloc (line_table, loc);
2040 maybe_add_include_fixit (&richloc, header, true);
2042 "%qD is declared in header %qs", olddecl, header);
2047 if (mismatch_expect && extra_warnings)
2049 location_t newloc = DECL_SOURCE_LOCATION (newdecl);
2050 bool warned = false;
2052 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2053 "mismatch in argument %u type of built-in "
2054 "function %qD; expected %qT",
2055 mismatch_argno, newdecl, mismatch_expect);
2057 warned = warning_at (newloc, OPT_Wbuiltin_declaration_mismatch,
2058 "mismatch in return type of built-in "
2059 "function %qD; expected %qT",
2060 newdecl, mismatch_expect);
2061 const char *header = header_for_builtin_fn (olddecl);
2062 if (warned && header)
2064 rich_location richloc (line_table, newloc);
2065 maybe_add_include_fixit (&richloc, header, true);
2067 "%qD is declared in header %qs", olddecl, header);
2071 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2072 && DECL_IS_UNDECLARED_BUILTIN (olddecl))
2074 /* A conflicting function declaration for a predeclared
2075 function that isn't actually built in. Objective C uses
2076 these. The new declaration silently overrides everything
2077 but the volatility (i.e. noreturn) indication. See also
2078 below. FIXME: Make Objective C use normal builtins. */
2079 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2082 /* Permit void foo (...) to match int foo (...) if the latter is
2083 the definition and implicit int was used. See
2084 c-torture/compile/920625-2.c. */
2085 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
2086 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
2087 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
2088 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
2090 pedwarned = pedwarn (input_location, 0,
2091 "conflicting types for %q+D", newdecl);
2092 /* Make sure we keep void as the return type. */
2093 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
2094 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
2096 /* Permit void foo (...) to match an earlier call to foo (...) with
2097 no declared type (thus, implicitly int). */
2098 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2099 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
2100 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
2101 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
2103 pedwarned = pedwarn (input_location, 0,
2104 "conflicting types for %q+D; have %qT",
2106 /* Make sure we keep void as the return type. */
2107 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
2111 int new_quals = TYPE_QUALS (newtype);
2112 int old_quals = TYPE_QUALS (oldtype);
2114 if (new_quals != old_quals)
2116 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
2117 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
2118 if (new_addr != old_addr)
2120 if (ADDR_SPACE_GENERIC_P (new_addr))
2121 error ("conflicting named address spaces (generic vs %s) "
2123 c_addr_space_name (old_addr), newdecl);
2124 else if (ADDR_SPACE_GENERIC_P (old_addr))
2125 error ("conflicting named address spaces (%s vs generic) "
2127 c_addr_space_name (new_addr), newdecl);
2129 error ("conflicting named address spaces (%s vs %s) "
2131 c_addr_space_name (new_addr),
2132 c_addr_space_name (old_addr),
2136 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
2137 != CLEAR_QUAL_ADDR_SPACE (old_quals))
2138 error ("conflicting type qualifiers for %q+D", newdecl);
2141 error ("conflicting types for %q+D; have %qT", newdecl, newtype);
2142 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
2143 locate_old_decl (olddecl);
2147 /* Warn about enum/integer type mismatches. They are compatible types
2148 (C2X 6.7.2.2/5), but may pose portability problems. */
2149 else if (enum_and_int_p && TREE_CODE (newdecl) != TYPE_DECL)
2150 warned = warning_at (DECL_SOURCE_LOCATION (newdecl),
2151 OPT_Wenum_int_mismatch,
2152 "conflicting types for %q+D due to enum/integer "
2153 "mismatch; have %qT", newdecl, newtype);
2155 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
2156 but silently ignore the redeclaration if either is in a system
2157 header. (Conflicting redeclarations were handled above.) This
2158 is allowed for C11 if the types are the same, not just
2160 if (TREE_CODE (newdecl) == TYPE_DECL)
2162 bool types_different = false;
2165 = comptypes_check_different_types (oldtype, newtype, &types_different);
2167 if (comptypes_result != 1 || types_different)
2169 error ("redefinition of typedef %q+D with different type", newdecl);
2170 locate_old_decl (olddecl);
2174 if (DECL_IN_SYSTEM_HEADER (newdecl)
2175 || DECL_IN_SYSTEM_HEADER (olddecl)
2176 || warning_suppressed_p (newdecl, OPT_Wpedantic)
2177 || warning_suppressed_p (olddecl, OPT_Wpedantic))
2178 return true; /* Allow OLDDECL to continue in use. */
2180 if (variably_modified_type_p (newtype, NULL))
2182 error ("redefinition of typedef %q+D with variably modified type",
2184 locate_old_decl (olddecl);
2186 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2187 "redefinition of typedef %q+D", newdecl))
2188 locate_old_decl (olddecl);
2193 /* Function declarations can either be 'static' or 'extern' (no
2194 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2195 can never conflict with each other on account of linkage
2196 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2197 gnu89 mode permits two definitions if one is 'extern inline' and
2198 one is not. The non- extern-inline definition supersedes the
2199 extern-inline definition. */
2201 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2203 /* If you declare a built-in function name as static, or
2204 define the built-in with an old-style definition (so we
2205 can't validate the argument list) the built-in definition is
2206 overridden, but optionally warn this was a bad choice of name. */
2207 if (fndecl_built_in_p (olddecl)
2208 && !C_DECL_DECLARED_BUILTIN (olddecl))
2210 if (!TREE_PUBLIC (newdecl)
2211 || (DECL_INITIAL (newdecl)
2212 && !prototype_p (TREE_TYPE (newdecl))))
2214 warning_at (DECL_SOURCE_LOCATION (newdecl),
2215 OPT_Wshadow, "declaration of %qD shadows "
2216 "a built-in function", newdecl);
2217 /* Discard the old built-in function. */
2221 if (!prototype_p (TREE_TYPE (newdecl)))
2223 /* Set for built-ins that take no arguments. */
2224 bool func_void_args = false;
2225 if (tree at = TYPE_ARG_TYPES (oldtype))
2226 func_void_args = VOID_TYPE_P (TREE_VALUE (at));
2228 if (extra_warnings && !func_void_args)
2229 warning_at (DECL_SOURCE_LOCATION (newdecl),
2230 OPT_Wbuiltin_declaration_mismatch,
2231 "declaration of built-in function %qD without "
2232 "a prototype; expected %qT",
2233 newdecl, TREE_TYPE (olddecl));
2237 if (DECL_INITIAL (newdecl))
2239 if (DECL_INITIAL (olddecl))
2241 /* If both decls are in the same TU and the new declaration
2242 isn't overriding an extern inline reject the new decl.
2243 In c99, no overriding is allowed in the same translation
2245 if ((!DECL_EXTERN_INLINE (olddecl)
2246 || DECL_EXTERN_INLINE (newdecl)
2247 || (!flag_gnu89_inline
2248 && (!DECL_DECLARED_INLINE_P (olddecl)
2249 || !lookup_attribute ("gnu_inline",
2250 DECL_ATTRIBUTES (olddecl)))
2251 && (!DECL_DECLARED_INLINE_P (newdecl)
2252 || !lookup_attribute ("gnu_inline",
2253 DECL_ATTRIBUTES (newdecl))))
2255 && same_translation_unit_p (newdecl, olddecl))
2257 auto_diagnostic_group d;
2258 error ("redefinition of %q+D", newdecl);
2259 locate_old_decl (olddecl);
2264 /* If we have a prototype after an old-style function definition,
2265 the argument types must be checked specially. */
2266 else if (DECL_INITIAL (olddecl)
2267 && !prototype_p (oldtype) && prototype_p (newtype)
2268 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2270 auto_diagnostic_group d;
2271 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2273 locate_old_decl (olddecl);
2277 /* A non-static declaration (even an "extern") followed by a
2278 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2279 The same is true for a static forward declaration at block
2280 scope followed by a non-static declaration/definition at file
2281 scope. Static followed by non-static at the same scope is
2282 not undefined behavior, and is the most convenient way to get
2283 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2284 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2285 we do diagnose it if -Wtraditional. */
2286 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2288 /* Two exceptions to the rule. If olddecl is an extern
2289 inline, or a predeclared function that isn't actually
2290 built in, newdecl silently overrides olddecl. The latter
2291 occur only in Objective C; see also above. (FIXME: Make
2292 Objective C use normal builtins.) */
2293 if (!DECL_IS_UNDECLARED_BUILTIN (olddecl)
2294 && !DECL_EXTERN_INLINE (olddecl))
2296 auto_diagnostic_group d;
2297 error ("static declaration of %q+D follows "
2298 "non-static declaration", newdecl);
2299 locate_old_decl (olddecl);
2303 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2305 if (DECL_CONTEXT (olddecl))
2307 auto_diagnostic_group d;
2308 error ("non-static declaration of %q+D follows "
2309 "static declaration", newdecl);
2310 locate_old_decl (olddecl);
2313 else if (warn_traditional)
2315 warned |= warning (OPT_Wtraditional,
2316 "non-static declaration of %q+D "
2317 "follows static declaration", newdecl);
2321 /* Make sure gnu_inline attribute is either not present, or
2322 present on all inline decls. */
2323 if (DECL_DECLARED_INLINE_P (olddecl)
2324 && DECL_DECLARED_INLINE_P (newdecl))
2326 bool newa = lookup_attribute ("gnu_inline",
2327 DECL_ATTRIBUTES (newdecl)) != NULL;
2328 bool olda = lookup_attribute ("gnu_inline",
2329 DECL_ATTRIBUTES (olddecl)) != NULL;
2332 auto_diagnostic_group d;
2333 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2334 newa ? newdecl : olddecl);
2335 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2340 else if (VAR_P (newdecl))
2342 /* Only variables can be thread-local, and all declarations must
2343 agree on this property. */
2344 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2346 /* Nothing to check. Since OLDDECL is marked threadprivate
2347 and NEWDECL does not have a thread-local attribute, we
2348 will merge the threadprivate attribute into NEWDECL. */
2351 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2353 auto_diagnostic_group d;
2354 if (DECL_THREAD_LOCAL_P (newdecl))
2355 error ("thread-local declaration of %q+D follows "
2356 "non-thread-local declaration", newdecl);
2358 error ("non-thread-local declaration of %q+D follows "
2359 "thread-local declaration", newdecl);
2361 locate_old_decl (olddecl);
2365 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2366 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2368 auto_diagnostic_group d;
2369 error ("redefinition of %q+D", newdecl);
2370 locate_old_decl (olddecl);
2374 /* Objects declared at file scope: if the first declaration had
2375 external linkage (even if it was an external reference) the
2376 second must have external linkage as well, or the behavior is
2377 undefined. If the first declaration had internal linkage, then
2378 the second must too, or else be an external reference (in which
2379 case the composite declaration still has internal linkage).
2380 As for function declarations, we warn about the static-then-
2381 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2382 if (DECL_FILE_SCOPE_P (newdecl)
2383 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2385 if (DECL_EXTERNAL (newdecl))
2387 if (!DECL_FILE_SCOPE_P (olddecl))
2389 auto_diagnostic_group d;
2390 error ("extern declaration of %q+D follows "
2391 "declaration with no linkage", newdecl);
2392 locate_old_decl (olddecl);
2395 else if (warn_traditional)
2397 warned |= warning (OPT_Wtraditional,
2398 "non-static declaration of %q+D "
2399 "follows static declaration", newdecl);
2404 auto_diagnostic_group d;
2405 if (TREE_PUBLIC (newdecl))
2406 error ("non-static declaration of %q+D follows "
2407 "static declaration", newdecl);
2409 error ("static declaration of %q+D follows "
2410 "non-static declaration", newdecl);
2412 locate_old_decl (olddecl);
2416 /* Two objects with the same name declared at the same block
2417 scope must both be external references (6.7p3). */
2418 else if (!DECL_FILE_SCOPE_P (newdecl))
2420 if (DECL_EXTERNAL (newdecl))
2422 /* Extern with initializer at block scope, which will
2423 already have received an error. */
2425 else if (DECL_EXTERNAL (olddecl))
2427 auto_diagnostic_group d;
2428 error ("declaration of %q+D with no linkage follows "
2429 "extern declaration", newdecl);
2430 locate_old_decl (olddecl);
2434 auto_diagnostic_group d;
2435 error ("redeclaration of %q+D with no linkage", newdecl);
2436 locate_old_decl (olddecl);
2442 /* C++ does not permit a decl to appear multiple times at file
2445 && DECL_FILE_SCOPE_P (newdecl)
2446 && !DECL_EXTERNAL (newdecl)
2447 && !DECL_EXTERNAL (olddecl))
2448 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2450 ("duplicate declaration of %qD is "
2456 /* All decls must agree on a visibility. */
2457 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2458 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2459 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2461 warned |= warning (0, "redeclaration of %q+D with different visibility "
2462 "(old visibility preserved)", newdecl);
2465 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2466 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2467 else /* PARM_DECL, VAR_DECL */
2469 /* Redeclaration of a parameter is a constraint violation (this is
2470 not explicitly stated, but follows from C99 6.7p3 [no more than
2471 one declaration of the same identifier with no linkage in the
2472 same scope, except type tags] and 6.2.2p6 [parameters have no
2473 linkage]). We must check for a forward parameter declaration,
2474 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2475 an extension, the mandatory diagnostic for which is handled by
2476 mark_forward_parm_decls. */
2478 if (TREE_CODE (newdecl) == PARM_DECL
2479 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2481 auto_diagnostic_group d;
2482 error ("redefinition of parameter %q+D", newdecl);
2483 locate_old_decl (olddecl);
2488 /* Optional warning for completely redundant decls. */
2489 if (!warned && !pedwarned
2490 && warn_redundant_decls
2491 /* Don't warn about a function declaration followed by a
2493 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2494 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2495 /* Don't warn about redundant redeclarations of builtins. */
2496 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2497 && !fndecl_built_in_p (newdecl)
2498 && fndecl_built_in_p (olddecl)
2499 && !C_DECL_DECLARED_BUILTIN (olddecl))
2500 /* Don't warn about an extern followed by a definition. */
2501 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2502 /* Don't warn about forward parameter decls. */
2503 && !(TREE_CODE (newdecl) == PARM_DECL
2504 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2505 /* Don't warn about a variable definition following a declaration. */
2506 && !(VAR_P (newdecl)
2507 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2509 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2513 /* Report location of previous decl/defn. */
2514 if (warned || pedwarned)
2515 locate_old_decl (olddecl);
2517 #undef DECL_EXTERN_INLINE
2522 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2523 consistent with OLDDECL, but carries new information. Merge the
2524 new information into OLDDECL. This function issues no
2528 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2530 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2531 && DECL_INITIAL (newdecl) != NULL_TREE);
2532 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2533 && prototype_p (TREE_TYPE (newdecl)));
2534 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2535 && prototype_p (TREE_TYPE (olddecl)));
2537 /* For real parm decl following a forward decl, rechain the old decl
2538 in its new location and clear TREE_ASM_WRITTEN (it's not a
2539 forward decl anymore). */
2540 if (TREE_CODE (newdecl) == PARM_DECL
2541 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2543 struct c_binding *b, **here;
2545 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2546 if ((*here)->decl == olddecl)
2553 b->prev = current_scope->bindings;
2554 current_scope->bindings = b;
2556 TREE_ASM_WRITTEN (olddecl) = 0;
2559 DECL_ATTRIBUTES (newdecl)
2560 = targetm.merge_decl_attributes (olddecl, newdecl);
2562 /* For typedefs use the old type, as the new type's DECL_NAME points
2563 at newdecl, which will be ggc_freed. */
2564 if (TREE_CODE (newdecl) == TYPE_DECL)
2566 /* But NEWTYPE might have an attribute, honor that. */
2570 if (TYPE_USER_ALIGN (tem))
2572 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2573 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2574 TYPE_USER_ALIGN (newtype) = true;
2577 /* And remove the new type from the variants list. */
2578 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2580 tree remove = TREE_TYPE (newdecl);
2581 if (TYPE_MAIN_VARIANT (remove) == remove)
2583 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE);
2584 /* If remove is the main variant, no need to remove that
2585 from the list. One of the DECL_ORIGINAL_TYPE
2586 variants, e.g. created for aligned attribute, might still
2587 refer to the newdecl TYPE_DECL though, so remove that one
2589 if (DECL_ORIGINAL_TYPE (newdecl)
2590 && DECL_ORIGINAL_TYPE (newdecl) != remove)
2591 for (tree t = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (newdecl));
2592 t; t = TYPE_MAIN_VARIANT (t))
2593 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl)
2595 TYPE_NEXT_VARIANT (t)
2596 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t));
2601 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2602 t = TYPE_NEXT_VARIANT (t))
2603 if (TYPE_NEXT_VARIANT (t) == remove)
2605 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2611 /* Merge the data types specified in the two decls. */
2613 = TREE_TYPE (olddecl)
2614 = composite_type (newtype, oldtype);
2616 /* Lay the type out, unless already done. */
2617 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2619 if (TREE_TYPE (newdecl) != error_mark_node)
2620 layout_type (TREE_TYPE (newdecl));
2621 if (TREE_CODE (newdecl) != FUNCTION_DECL
2622 && TREE_CODE (newdecl) != TYPE_DECL
2623 && TREE_CODE (newdecl) != CONST_DECL)
2624 layout_decl (newdecl, 0);
2628 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2629 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2630 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2631 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2632 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2634 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2635 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2637 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl)
2638 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl))
2639 DECL_USER_ALIGN (newdecl) = 1;
2640 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2641 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2642 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2643 DECL_WARN_IF_NOT_ALIGN (olddecl));
2646 /* Keep the old rtl since we can safely use it. */
2647 if (HAS_RTL_P (olddecl))
2648 COPY_DECL_RTL (olddecl, newdecl);
2650 /* Merge the type qualifiers. */
2651 if (TREE_READONLY (newdecl))
2652 TREE_READONLY (olddecl) = 1;
2654 if (TREE_THIS_VOLATILE (newdecl))
2655 TREE_THIS_VOLATILE (olddecl) = 1;
2657 /* Merge deprecatedness. */
2658 if (TREE_DEPRECATED (newdecl))
2659 TREE_DEPRECATED (olddecl) = 1;
2661 /* Merge unavailability. */
2662 if (TREE_UNAVAILABLE (newdecl))
2663 TREE_UNAVAILABLE (olddecl) = 1;
2665 /* If a decl is in a system header and the other isn't, keep the one on the
2666 system header. Otherwise, keep source location of definition rather than
2667 declaration and of prototype rather than non-prototype unless that
2668 prototype is built-in. */
2669 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2670 && DECL_IN_SYSTEM_HEADER (olddecl)
2671 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2672 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2673 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2674 && DECL_IN_SYSTEM_HEADER (newdecl)
2675 && !DECL_IN_SYSTEM_HEADER (olddecl))
2676 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2677 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2678 && DECL_INITIAL (olddecl) != NULL_TREE)
2679 || (old_is_prototype && !new_is_prototype
2680 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2681 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2683 /* Merge the initialization information. */
2684 if (DECL_INITIAL (newdecl) == NULL_TREE)
2685 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2687 /* Merge the threadprivate attribute. */
2688 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2689 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2691 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2693 /* Copy the assembler name.
2694 Currently, it can only be defined in the prototype. */
2695 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2697 /* Use visibility of whichever declaration had it specified */
2698 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2700 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2701 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2704 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2706 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2707 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2708 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2709 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2710 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2711 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2712 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2713 if (DECL_IS_OPERATOR_NEW_P (olddecl))
2714 DECL_SET_IS_OPERATOR_NEW (newdecl, true);
2715 if (DECL_IS_OPERATOR_DELETE_P (olddecl))
2716 DECL_SET_IS_OPERATOR_DELETE (newdecl, true);
2717 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2718 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2719 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2722 /* Merge the storage class information. */
2723 merge_weak (newdecl, olddecl);
2725 /* For functions, static overrides non-static. */
2726 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2728 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2729 /* This is since we don't automatically
2730 copy the attributes of NEWDECL into OLDDECL. */
2731 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2732 /* If this clears `static', clear it in the identifier too. */
2733 if (!TREE_PUBLIC (olddecl))
2734 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2738 /* In c99, 'extern' declaration before (or after) 'inline' means this
2739 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2741 if (TREE_CODE (newdecl) == FUNCTION_DECL
2742 && !flag_gnu89_inline
2743 && (DECL_DECLARED_INLINE_P (newdecl)
2744 || DECL_DECLARED_INLINE_P (olddecl))
2745 && (!DECL_DECLARED_INLINE_P (newdecl)
2746 || !DECL_DECLARED_INLINE_P (olddecl)
2747 || !DECL_EXTERNAL (olddecl))
2748 && DECL_EXTERNAL (newdecl)
2749 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2750 && !current_function_decl)
2751 DECL_EXTERNAL (newdecl) = 0;
2753 /* An inline definition following a static declaration is not
2755 if (new_is_definition
2756 && (DECL_DECLARED_INLINE_P (newdecl)
2757 || DECL_DECLARED_INLINE_P (olddecl))
2758 && !TREE_PUBLIC (olddecl))
2759 DECL_EXTERNAL (newdecl) = 0;
2761 if (DECL_EXTERNAL (newdecl))
2763 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2764 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2766 /* An extern decl does not override previous storage class. */
2767 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2768 if (!DECL_EXTERNAL (newdecl))
2770 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2771 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2776 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2777 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2780 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2782 /* If we're redefining a function previously defined as extern
2783 inline, make sure we emit debug info for the inline before we
2784 throw it away, in case it was inlined into a function that
2785 hasn't been written out yet. */
2786 if (new_is_definition && DECL_INITIAL (olddecl))
2787 /* The new defn must not be inline. */
2788 DECL_UNINLINABLE (newdecl) = 1;
2791 /* If either decl says `inline', this fn is inline, unless
2792 its definition was passed already. */
2793 if (DECL_DECLARED_INLINE_P (newdecl)
2794 || DECL_DECLARED_INLINE_P (olddecl))
2795 DECL_DECLARED_INLINE_P (newdecl) = 1;
2797 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2798 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2800 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2801 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2802 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2803 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2806 if (fndecl_built_in_p (olddecl))
2808 /* If redeclaring a builtin function, it stays built in.
2809 But it gets tagged as having been declared. */
2810 copy_decl_built_in_function (newdecl, olddecl);
2811 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2812 if (new_is_prototype)
2814 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2815 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2817 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2820 /* If a compatible prototype of these builtin functions
2821 is seen, assume the runtime implements it with the
2822 expected semantics. */
2823 case BUILT_IN_STPCPY:
2824 if (builtin_decl_explicit_p (fncode))
2825 set_builtin_decl_implicit_p (fncode, true);
2828 if (builtin_decl_explicit_p (fncode))
2829 set_builtin_decl_declared_p (fncode, true);
2833 copy_attributes_to_builtin (newdecl);
2837 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2838 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2841 /* Preserve function specific target and optimization options */
2842 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2843 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2844 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2845 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2847 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2848 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2849 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2850 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2852 /* Also preserve various other info from the definition. */
2853 if (!new_is_definition)
2856 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2857 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2858 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2859 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2860 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2861 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2862 DECL_CONTEXT (t) = newdecl;
2864 /* See if we've got a function to instantiate from. */
2865 if (DECL_SAVED_TREE (olddecl))
2866 DECL_ABSTRACT_ORIGIN (newdecl)
2867 = DECL_ABSTRACT_ORIGIN (olddecl);
2871 /* Merge the USED information. */
2872 if (TREE_USED (olddecl))
2873 TREE_USED (newdecl) = 1;
2874 else if (TREE_USED (newdecl))
2875 TREE_USED (olddecl) = 1;
2876 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2877 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2878 if (DECL_PRESERVE_P (olddecl))
2879 DECL_PRESERVE_P (newdecl) = 1;
2880 else if (DECL_PRESERVE_P (newdecl))
2881 DECL_PRESERVE_P (olddecl) = 1;
2883 /* Merge DECL_COMMON */
2884 if (VAR_P (olddecl) && VAR_P (newdecl)
2885 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2886 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2887 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2889 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2890 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2891 DECL_ARGUMENTS (if appropriate). */
2893 unsigned olddecl_uid = DECL_UID (olddecl);
2894 tree olddecl_context = DECL_CONTEXT (olddecl);
2895 tree olddecl_arguments = NULL;
2896 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2897 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2899 memcpy ((char *) olddecl + sizeof (struct tree_common),
2900 (char *) newdecl + sizeof (struct tree_common),
2901 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2902 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2903 switch (TREE_CODE (olddecl))
2908 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2910 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2911 (char *) newdecl + sizeof (struct tree_decl_common),
2912 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2913 olddecl->decl_with_vis.symtab_node = snode;
2915 if ((DECL_EXTERNAL (olddecl)
2916 || TREE_PUBLIC (olddecl)
2917 || TREE_STATIC (olddecl))
2918 && DECL_SECTION_NAME (newdecl) != NULL)
2919 set_decl_section_name (olddecl, newdecl);
2921 /* This isn't quite correct for something like
2922 int __thread x attribute ((tls_model ("local-exec")));
2923 extern int __thread x;
2924 as we'll lose the "local-exec" model. */
2925 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2926 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2936 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2937 (char *) newdecl + sizeof (struct tree_decl_common),
2938 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2943 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2944 (char *) newdecl + sizeof (struct tree_decl_common),
2945 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2947 DECL_UID (olddecl) = olddecl_uid;
2948 DECL_CONTEXT (olddecl) = olddecl_context;
2949 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2950 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2953 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2954 so that encode_section_info has a chance to look at the new decl
2955 flags and attributes. */
2956 if (DECL_RTL_SET_P (olddecl)
2957 && (TREE_CODE (olddecl) == FUNCTION_DECL
2958 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2959 make_decl_rtl (olddecl);
2962 /* Handle when a new declaration NEWDECL has the same name as an old
2963 one OLDDECL in the same binding contour. Prints an error message
2966 If safely possible, alter OLDDECL to look like NEWDECL, and return
2967 true. Otherwise, return false. */
2970 duplicate_decls (tree newdecl, tree olddecl)
2972 tree newtype = NULL, oldtype = NULL;
2974 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2976 /* Avoid `unused variable' and other warnings for OLDDECL. */
2977 suppress_warning (olddecl, OPT_Wunused);
2978 /* If the types are completely different, poison them both with
2980 if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl))
2981 && olddecl != error_mark_node
2984 if (TREE_CODE (olddecl) != FUNCTION_DECL)
2985 TREE_TYPE (olddecl) = error_mark_node;
2986 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2987 TREE_TYPE (newdecl) = error_mark_node;
2992 merge_decls (newdecl, olddecl, newtype, oldtype);
2994 /* The NEWDECL will no longer be needed.
2996 Before releasing the node, be sure to remove function from symbol
2997 table that might have been inserted there to record comdat group.
2998 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2999 structure is shared in between NEWDECL and OLDECL. */
3000 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3001 DECL_STRUCT_FUNCTION (newdecl) = NULL;
3002 if (VAR_OR_FUNCTION_DECL_P (newdecl))
3004 struct symtab_node *snode = symtab_node::get (newdecl);
3013 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
3015 warn_if_shadowing (tree new_decl)
3017 struct c_binding *b;
3019 /* Shadow warnings wanted? */
3021 || warn_shadow_local
3022 || warn_shadow_compatible_local)
3023 /* No shadow warnings for internally generated vars. */
3024 || DECL_IS_UNDECLARED_BUILTIN (new_decl))
3027 /* Is anything being shadowed? Invisible decls do not count. */
3028 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
3029 if (b->decl && b->decl != new_decl && !b->invisible
3030 && (b->decl == error_mark_node
3031 || diagnostic_report_warnings_p (global_dc,
3032 DECL_SOURCE_LOCATION (b->decl))))
3034 tree old_decl = b->decl;
3036 if (old_decl == error_mark_node)
3038 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
3039 "non-variable", new_decl);
3043 bool warned = false;
3044 auto_diagnostic_group d;
3045 if (TREE_CODE (old_decl) == PARM_DECL)
3047 enum opt_code warning_code;
3049 /* If '-Wshadow=compatible-local' is specified without other
3050 -Wshadow= flags, we will warn only when the types of the
3051 shadowing variable (i.e. new_decl) and the shadowed variable
3052 (old_decl) are compatible. */
3054 warning_code = OPT_Wshadow;
3055 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3056 warning_code = OPT_Wshadow_compatible_local;
3058 warning_code = OPT_Wshadow_local;
3059 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3060 "declaration of %qD shadows a parameter",
3063 else if (DECL_FILE_SCOPE_P (old_decl))
3065 /* Do not warn if a variable shadows a function, unless
3066 the variable is a function or a pointer-to-function. */
3067 if (TREE_CODE (old_decl) == FUNCTION_DECL
3068 && TREE_CODE (new_decl) != FUNCTION_DECL
3069 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
3072 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
3073 "declaration of %qD shadows a global "
3077 else if (TREE_CODE (old_decl) == FUNCTION_DECL
3078 && fndecl_built_in_p (old_decl))
3080 warning (OPT_Wshadow, "declaration of %q+D shadows "
3081 "a built-in function", new_decl);
3086 enum opt_code warning_code;
3088 /* If '-Wshadow=compatible-local' is specified without other
3089 -Wshadow= flags, we will warn only when the types of the
3090 shadowing variable (i.e. new_decl) and the shadowed variable
3091 (old_decl) are compatible. */
3093 warning_code = OPT_Wshadow;
3094 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
3095 warning_code = OPT_Wshadow_compatible_local;
3097 warning_code = OPT_Wshadow_local;
3098 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
3099 "declaration of %qD shadows a previous local",
3104 inform (DECL_SOURCE_LOCATION (old_decl),
3105 "shadowed declaration is here");
3111 /* Record a decl-node X as belonging to the current lexical scope.
3112 Check for errors (such as an incompatible declaration for the same
3113 name already seen in the same scope).
3115 Returns either X or an old decl for the same name.
3116 If an old decl is returned, it may have been smashed
3117 to agree with what X says. */
3122 tree name = DECL_NAME (x);
3123 struct c_scope *scope = current_scope;
3124 struct c_binding *b;
3125 bool nested = false;
3126 location_t locus = DECL_SOURCE_LOCATION (x);
3128 /* Must set DECL_CONTEXT for everything not at file scope or
3129 DECL_FILE_SCOPE_P won't work. Local externs don't count
3130 unless they have initializers (which generate code). */
3131 if (current_function_decl
3132 && (!VAR_OR_FUNCTION_DECL_P (x)
3133 || DECL_INITIAL (x) || !TREE_PUBLIC (x)))
3134 DECL_CONTEXT (x) = current_function_decl;
3136 /* Anonymous decls are just inserted in the scope. */
3139 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
3144 /* First, see if there is another declaration with the same name in
3145 the current scope. If there is, duplicate_decls may do all the
3146 work for us. If duplicate_decls returns false, that indicates
3147 two incompatible decls in the same scope; we are to silently
3148 replace the old one (duplicate_decls has issued all appropriate
3149 diagnostics). In particular, we should not consider possible
3150 duplicates in the external scope, or shadowing. */
3151 b = I_SYMBOL_BINDING (name);
3152 if (b && B_IN_SCOPE (b, scope))
3154 struct c_binding *b_ext, *b_use;
3155 tree type = TREE_TYPE (x);
3156 tree visdecl = b->decl;
3157 tree vistype = TREE_TYPE (visdecl);
3158 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3159 && COMPLETE_TYPE_P (TREE_TYPE (x)))
3160 b->inner_comp = false;
3163 /* If this is an external linkage declaration, we should check
3164 for compatibility with the type in the external scope before
3165 setting the type at this scope based on the visible
3166 information only. */
3167 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
3169 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3170 b_ext = b_ext->shadowed;
3175 TREE_TYPE (b_use->decl) = b_use->u.type;
3178 if (duplicate_decls (x, b_use->decl))
3182 /* Save the updated type in the external scope and
3183 restore the proper type for this scope. */
3185 if (comptypes (vistype, type))
3186 thistype = composite_type (vistype, type);
3188 thistype = TREE_TYPE (b_use->decl);
3189 b_use->u.type = TREE_TYPE (b_use->decl);
3190 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
3191 && fndecl_built_in_p (b_use->decl))
3193 = build_type_attribute_variant (thistype,
3196 TREE_TYPE (b_use->decl) = thistype;
3201 goto skip_external_and_shadow_checks;
3204 /* All declarations with external linkage, and all external
3205 references, go in the external scope, no matter what scope is
3206 current. However, the binding in that scope is ignored for
3207 purposes of normal name lookup. A separate binding structure is
3208 created in the requested scope; this governs the normal
3209 visibility of the symbol.
3211 The binding in the externals scope is used exclusively for
3212 detecting duplicate declarations of the same object, no matter
3213 what scope they are in; this is what we do here. (C99 6.2.7p2:
3214 All declarations that refer to the same object or function shall
3215 have compatible type; otherwise, the behavior is undefined.)
3216 However, in Objective-C, we also want to detect declarations
3217 conflicting with those of the basic types. */
3218 if ((DECL_EXTERNAL (x) || scope == file_scope)
3219 && (VAR_OR_FUNCTION_DECL_P (x) || c_dialect_objc ()))
3221 tree type = TREE_TYPE (x);
3222 tree vistype = NULL_TREE;
3223 tree visdecl = NULL_TREE;
3224 bool type_saved = false;
3225 if (b && !B_IN_EXTERNAL_SCOPE (b)
3226 && VAR_OR_FUNCTION_DECL_P (b->decl)
3227 && DECL_FILE_SCOPE_P (b->decl))
3230 vistype = TREE_TYPE (visdecl);
3232 if (scope != file_scope
3233 && !DECL_IN_SYSTEM_HEADER (x))
3234 warning_at (locus, OPT_Wnested_externs,
3235 "nested extern declaration of %qD", x);
3237 while (b && !B_IN_EXTERNAL_SCOPE (b))
3239 /* If this decl might be modified, save its type. This is
3240 done here rather than when the decl is first bound
3241 because the type may change after first binding, through
3242 being completed or through attributes being added. If we
3243 encounter multiple such decls, only the first should have
3244 its type saved; the others will already have had their
3245 proper types saved and the types will not have changed as
3246 their scopes will not have been re-entered. */
3247 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3249 b->u.type = TREE_TYPE (b->decl);
3252 if (B_IN_FILE_SCOPE (b)
3254 && TREE_STATIC (b->decl)
3255 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3256 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3257 && TREE_CODE (type) == ARRAY_TYPE
3258 && TYPE_DOMAIN (type)
3259 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3260 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3262 /* Array type completed in inner scope, which should be
3263 diagnosed if the completion does not have size 1 and
3264 it does not get completed in the file scope. */
3265 b->inner_comp = true;
3270 /* If a matching external declaration has been found, set its
3271 type to the composite of all the types of that declaration.
3272 After the consistency checks, it will be reset to the
3273 composite of the visible types only. */
3274 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3276 TREE_TYPE (b->decl) = b->u.type;
3278 /* The point of the same_translation_unit_p check here is,
3279 we want to detect a duplicate decl for a construct like
3280 foo() { extern bar(); } ... static bar(); but not if
3281 they are in different translation units. In any case,
3282 the static does not go in the externals scope. */
3284 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3285 && duplicate_decls (x, b->decl))
3290 if (comptypes (vistype, type))
3291 thistype = composite_type (vistype, type);
3293 thistype = TREE_TYPE (b->decl);
3297 b->u.type = TREE_TYPE (b->decl);
3298 /* Propagate the type attributes to the decl. */
3300 = build_type_attribute_variant (thistype,
3301 TYPE_ATTRIBUTES (b->u.type));
3302 TREE_TYPE (b->decl) = thistype;
3303 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3307 else if (TREE_PUBLIC (x))
3309 if (visdecl && !b && duplicate_decls (x, visdecl))
3311 /* An external declaration at block scope referring to a
3312 visible entity with internal linkage. The composite
3313 type will already be correct for this scope, so we
3314 just need to fall through to make the declaration in
3321 bind (name, x, external_scope, /*invisible=*/true,
3322 /*nested=*/false, locus);
3328 if (TREE_CODE (x) != PARM_DECL)
3329 warn_if_shadowing (x);
3331 skip_external_and_shadow_checks:
3332 if (TREE_CODE (x) == TYPE_DECL)
3334 /* So this is a typedef, set its underlying type. */
3335 set_underlying_type (x);
3337 /* If X is a typedef defined in the current function, record it
3338 for the purpose of implementing the -Wunused-local-typedefs
3340 record_locally_defined_typedef (x);
3343 bind (name, x, scope, /*invisible=*/false, nested, locus);
3345 /* If x's type is incomplete because it's based on a
3346 structure or union which has not yet been fully declared,
3347 attach it to that structure or union type, so we can go
3348 back and complete the variable declaration later, if the
3349 structure or union gets fully declared.
3351 If the input is erroneous, we can have error_mark in the type
3352 slot (e.g. "f(void a, ...)") - that doesn't count as an
3354 if (TREE_TYPE (x) != error_mark_node
3355 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3357 tree element = TREE_TYPE (x);
3359 while (TREE_CODE (element) == ARRAY_TYPE)
3360 element = TREE_TYPE (element);
3361 element = TYPE_MAIN_VARIANT (element);
3363 if ((RECORD_OR_UNION_TYPE_P (element)
3364 || TREE_CODE (element) == ENUMERAL_TYPE)
3365 && (TREE_CODE (x) != TYPE_DECL
3366 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3367 && !COMPLETE_TYPE_P (element))
3368 C_TYPE_INCOMPLETE_VARS (element)
3369 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3375 /* Issue a warning about implicit function declaration. ID is the function
3376 identifier, OLDDECL is a declaration of the function in a different scope,
3380 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3382 if (!warn_implicit_function_declaration)
3386 auto_diagnostic_group d;
3389 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3393 if (const char *suggestion = hint.suggestion ())
3395 gcc_rich_location richloc (loc);
3396 richloc.add_fixit_replace (suggestion);
3397 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3398 "implicit declaration of function %qE;"
3399 " did you mean %qs?",
3403 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3404 "implicit declaration of function %qE", id);
3406 else if (const char *suggestion = hint.suggestion ())
3408 gcc_rich_location richloc (loc);
3409 richloc.add_fixit_replace (suggestion);
3411 (&richloc, OPT_Wimplicit_function_declaration,
3412 G_("implicit declaration of function %qE; did you mean %qs?"),
3416 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3417 G_("implicit declaration of function %qE"), id);
3421 /* Whether the olddecl is an undeclared builtin function.
3422 locate_old_decl will not generate a diagnostic for those,
3423 so in that case we want to look elsewhere. */
3424 bool undeclared_builtin = (olddecl
3425 && TREE_CODE (olddecl) == FUNCTION_DECL
3426 && fndecl_built_in_p (olddecl)
3427 && !C_DECL_DECLARED_BUILTIN (olddecl));
3428 if (undeclared_builtin)
3430 const char *header = header_for_builtin_fn (olddecl);
3433 rich_location richloc (line_table, loc);
3434 maybe_add_include_fixit (&richloc, header, true);
3436 "include %qs or provide a declaration of %qE",
3441 locate_old_decl (olddecl);
3448 /* Return the name of the header file that declares built-in function
3449 FNDECL, or null if either we don't know or don't expect to see an
3450 explicit declaration. */
3453 header_for_builtin_fn (tree fndecl)
3455 if (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3458 switch (DECL_FUNCTION_CODE (fndecl))
3460 CASE_FLT_FN (BUILT_IN_ACOS):
3461 CASE_FLT_FN (BUILT_IN_ACOSH):
3462 CASE_FLT_FN (BUILT_IN_ASIN):
3463 CASE_FLT_FN (BUILT_IN_ASINH):
3464 CASE_FLT_FN (BUILT_IN_ATAN):
3465 CASE_FLT_FN (BUILT_IN_ATANH):
3466 CASE_FLT_FN (BUILT_IN_ATAN2):
3467 CASE_FLT_FN (BUILT_IN_CBRT):
3468 CASE_FLT_FN (BUILT_IN_CEIL):
3469 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3470 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3471 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3472 CASE_FLT_FN (BUILT_IN_COS):
3473 CASE_FLT_FN (BUILT_IN_COSH):
3474 CASE_FLT_FN (BUILT_IN_ERF):
3475 CASE_FLT_FN (BUILT_IN_ERFC):
3476 CASE_FLT_FN (BUILT_IN_EXP):
3477 CASE_FLT_FN (BUILT_IN_EXP2):
3478 CASE_FLT_FN (BUILT_IN_EXPM1):
3479 CASE_FLT_FN (BUILT_IN_FABS):
3480 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3481 CASE_FLT_FN (BUILT_IN_FDIM):
3482 CASE_FLT_FN (BUILT_IN_FLOOR):
3483 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3484 CASE_FLT_FN (BUILT_IN_FMA):
3485 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3486 CASE_FLT_FN (BUILT_IN_FMAX):
3487 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3488 CASE_FLT_FN (BUILT_IN_FMIN):
3489 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3490 CASE_FLT_FN (BUILT_IN_FMOD):
3491 CASE_FLT_FN (BUILT_IN_FREXP):
3492 CASE_FLT_FN (BUILT_IN_HYPOT):
3493 CASE_FLT_FN (BUILT_IN_ILOGB):
3494 CASE_FLT_FN (BUILT_IN_LDEXP):
3495 CASE_FLT_FN (BUILT_IN_LGAMMA):
3496 CASE_FLT_FN (BUILT_IN_LLRINT):
3497 CASE_FLT_FN (BUILT_IN_LLROUND):
3498 CASE_FLT_FN (BUILT_IN_LOG):
3499 CASE_FLT_FN (BUILT_IN_LOG10):
3500 CASE_FLT_FN (BUILT_IN_LOG1P):
3501 CASE_FLT_FN (BUILT_IN_LOG2):
3502 CASE_FLT_FN (BUILT_IN_LOGB):
3503 CASE_FLT_FN (BUILT_IN_LRINT):
3504 CASE_FLT_FN (BUILT_IN_LROUND):
3505 CASE_FLT_FN (BUILT_IN_MODF):
3506 CASE_FLT_FN (BUILT_IN_NAN):
3507 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3508 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3509 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3510 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3511 CASE_FLT_FN (BUILT_IN_POW):
3512 CASE_FLT_FN (BUILT_IN_REMAINDER):
3513 CASE_FLT_FN (BUILT_IN_REMQUO):
3514 CASE_FLT_FN (BUILT_IN_RINT):
3515 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3516 CASE_FLT_FN (BUILT_IN_ROUND):
3517 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3518 CASE_FLT_FN (BUILT_IN_SCALBLN):
3519 CASE_FLT_FN (BUILT_IN_SCALBN):
3520 CASE_FLT_FN (BUILT_IN_SIN):
3521 CASE_FLT_FN (BUILT_IN_SINH):
3522 CASE_FLT_FN (BUILT_IN_SINCOS):
3523 CASE_FLT_FN (BUILT_IN_SQRT):
3524 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3525 CASE_FLT_FN (BUILT_IN_TAN):
3526 CASE_FLT_FN (BUILT_IN_TANH):
3527 CASE_FLT_FN (BUILT_IN_TGAMMA):
3528 CASE_FLT_FN (BUILT_IN_TRUNC):
3529 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3530 case BUILT_IN_ISINF:
3531 case BUILT_IN_ISNAN:
3533 CASE_FLT_FN (BUILT_IN_CABS):
3534 CASE_FLT_FN (BUILT_IN_CACOS):
3535 CASE_FLT_FN (BUILT_IN_CACOSH):
3536 CASE_FLT_FN (BUILT_IN_CARG):
3537 CASE_FLT_FN (BUILT_IN_CASIN):
3538 CASE_FLT_FN (BUILT_IN_CASINH):
3539 CASE_FLT_FN (BUILT_IN_CATAN):
3540 CASE_FLT_FN (BUILT_IN_CATANH):
3541 CASE_FLT_FN (BUILT_IN_CCOS):
3542 CASE_FLT_FN (BUILT_IN_CCOSH):
3543 CASE_FLT_FN (BUILT_IN_CEXP):
3544 CASE_FLT_FN (BUILT_IN_CIMAG):
3545 CASE_FLT_FN (BUILT_IN_CLOG):
3546 CASE_FLT_FN (BUILT_IN_CONJ):
3547 CASE_FLT_FN (BUILT_IN_CPOW):
3548 CASE_FLT_FN (BUILT_IN_CPROJ):
3549 CASE_FLT_FN (BUILT_IN_CREAL):
3550 CASE_FLT_FN (BUILT_IN_CSIN):
3551 CASE_FLT_FN (BUILT_IN_CSINH):
3552 CASE_FLT_FN (BUILT_IN_CSQRT):
3553 CASE_FLT_FN (BUILT_IN_CTAN):
3554 CASE_FLT_FN (BUILT_IN_CTANH):
3555 return "<complex.h>";
3556 case BUILT_IN_MEMCHR:
3557 case BUILT_IN_MEMCMP:
3558 case BUILT_IN_MEMCPY:
3559 case BUILT_IN_MEMMOVE:
3560 case BUILT_IN_MEMSET:
3561 case BUILT_IN_STRCAT:
3562 case BUILT_IN_STRCHR:
3563 case BUILT_IN_STRCMP:
3564 case BUILT_IN_STRCPY:
3565 case BUILT_IN_STRCSPN:
3566 case BUILT_IN_STRLEN:
3567 case BUILT_IN_STRNCAT:
3568 case BUILT_IN_STRNCMP:
3569 case BUILT_IN_STRNCPY:
3570 case BUILT_IN_STRPBRK:
3571 case BUILT_IN_STRRCHR:
3572 case BUILT_IN_STRSPN:
3573 case BUILT_IN_STRSTR:
3574 return "<string.h>";
3575 case BUILT_IN_FPRINTF:
3577 case BUILT_IN_FPUTC:
3578 case BUILT_IN_FPUTS:
3579 case BUILT_IN_FSCANF:
3580 case BUILT_IN_FWRITE:
3581 case BUILT_IN_PRINTF:
3582 case BUILT_IN_PUTCHAR:
3584 case BUILT_IN_SCANF:
3585 case BUILT_IN_SNPRINTF:
3586 case BUILT_IN_SPRINTF:
3587 case BUILT_IN_SSCANF:
3588 case BUILT_IN_VFPRINTF:
3589 case BUILT_IN_VFSCANF:
3590 case BUILT_IN_VPRINTF:
3591 case BUILT_IN_VSCANF:
3592 case BUILT_IN_VSNPRINTF:
3593 case BUILT_IN_VSPRINTF:
3594 case BUILT_IN_VSSCANF:
3596 case BUILT_IN_ISALNUM:
3597 case BUILT_IN_ISALPHA:
3598 case BUILT_IN_ISBLANK:
3599 case BUILT_IN_ISCNTRL:
3600 case BUILT_IN_ISDIGIT:
3601 case BUILT_IN_ISGRAPH:
3602 case BUILT_IN_ISLOWER:
3603 case BUILT_IN_ISPRINT:
3604 case BUILT_IN_ISPUNCT:
3605 case BUILT_IN_ISSPACE:
3606 case BUILT_IN_ISUPPER:
3607 case BUILT_IN_ISXDIGIT:
3608 case BUILT_IN_TOLOWER:
3609 case BUILT_IN_TOUPPER:
3611 case BUILT_IN_ISWALNUM:
3612 case BUILT_IN_ISWALPHA:
3613 case BUILT_IN_ISWBLANK:
3614 case BUILT_IN_ISWCNTRL:
3615 case BUILT_IN_ISWDIGIT:
3616 case BUILT_IN_ISWGRAPH:
3617 case BUILT_IN_ISWLOWER:
3618 case BUILT_IN_ISWPRINT:
3619 case BUILT_IN_ISWPUNCT:
3620 case BUILT_IN_ISWSPACE:
3621 case BUILT_IN_ISWUPPER:
3622 case BUILT_IN_ISWXDIGIT:
3623 case BUILT_IN_TOWLOWER:
3624 case BUILT_IN_TOWUPPER:
3625 return "<wctype.h>";
3626 case BUILT_IN_ABORT:
3628 case BUILT_IN_CALLOC:
3632 case BUILT_IN_LLABS:
3633 case BUILT_IN_MALLOC:
3634 case BUILT_IN_REALLOC:
3635 case BUILT_IN__EXIT2:
3636 case BUILT_IN_ALIGNED_ALLOC:
3637 return "<stdlib.h>";
3638 case BUILT_IN_IMAXABS:
3639 return "<inttypes.h>";
3640 case BUILT_IN_STRFTIME:
3647 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3648 function of type int (). */
3651 implicitly_declare (location_t loc, tree functionid)
3653 struct c_binding *b;
3654 tree decl = NULL_TREE;
3657 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3659 if (B_IN_SCOPE (b, external_scope))
3668 if (TREE_CODE (decl) != FUNCTION_DECL)
3671 /* FIXME: Objective-C has weird not-really-builtin functions
3672 which are supposed to be visible automatically. They wind up
3673 in the external scope because they're pushed before the file
3674 scope gets created. Catch this here and rebind them into the
3676 if (!fndecl_built_in_p (decl) && DECL_IS_UNDECLARED_BUILTIN (decl))
3678 bind (functionid, decl, file_scope,
3679 /*invisible=*/false, /*nested=*/true,
3680 DECL_SOURCE_LOCATION (decl));
3685 tree newtype = default_function_type;
3687 TREE_TYPE (decl) = b->u.type;
3688 /* Implicit declaration of a function already declared
3689 (somehow) in a different scope, or as a built-in.
3690 If this is the first time this has happened, warn;
3691 then recycle the old declaration but with the new type. */
3692 if (!C_DECL_IMPLICIT (decl))
3694 implicit_decl_warning (loc, functionid, decl);
3695 C_DECL_IMPLICIT (decl) = 1;
3697 if (fndecl_built_in_p (decl))
3699 newtype = build_type_attribute_variant (newtype,
3701 (TREE_TYPE (decl)));
3702 if (!comptypes (newtype, TREE_TYPE (decl)))
3704 auto_diagnostic_group d;
3705 bool warned = warning_at (loc,
3706 OPT_Wbuiltin_declaration_mismatch,
3707 "incompatible implicit "
3708 "declaration of built-in "
3709 "function %qD", decl);
3710 /* See if we can hint which header to include. */
3711 const char *header = header_for_builtin_fn (decl);
3712 if (header != NULL && warned)
3714 rich_location richloc (line_table, loc);
3715 maybe_add_include_fixit (&richloc, header, true);
3717 "include %qs or provide a declaration of %qD",
3720 newtype = TREE_TYPE (decl);
3725 if (!comptypes (newtype, TREE_TYPE (decl)))
3727 auto_diagnostic_group d;
3728 error_at (loc, "incompatible implicit declaration of "
3729 "function %qD", decl);
3730 locate_old_decl (decl);
3733 b->u.type = TREE_TYPE (decl);
3734 TREE_TYPE (decl) = newtype;
3735 bind (functionid, decl, current_scope,
3736 /*invisible=*/false, /*nested=*/true,
3737 DECL_SOURCE_LOCATION (decl));
3742 /* Not seen before. */
3743 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3744 DECL_EXTERNAL (decl) = 1;
3745 TREE_PUBLIC (decl) = 1;
3746 C_DECL_IMPLICIT (decl) = 1;
3747 implicit_decl_warning (loc, functionid, 0);
3748 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3750 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3752 /* C89 says implicit declarations are in the innermost block.
3753 So we record the decl in the standard fashion. */
3754 decl = pushdecl (decl);
3756 /* No need to call objc_check_decl here - it's a function type. */
3757 rest_of_decl_compilation (decl, 0, 0);
3759 /* Write a record describing this implicit function declaration
3760 to the prototypes file (if requested). */
3761 gen_aux_info_record (decl, 0, 1, 0);
3763 /* Possibly apply some default attributes to this implicit declaration. */
3764 decl_attributes (&decl, NULL_TREE, 0);
3769 /* Issue an error message for a reference to an undeclared variable
3770 ID, including a reference to a builtin outside of function-call
3771 context. Establish a binding of the identifier to error_mark_node
3772 in an appropriate scope, which will suppress further errors for the
3773 same identifier. The error message should be given location LOC. */
3775 undeclared_variable (location_t loc, tree id)
3777 static bool already = false;
3778 struct c_scope *scope;
3780 auto_diagnostic_group d;
3781 if (current_function_decl == NULL_TREE)
3783 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3784 if (const char *suggestion = guessed_id.suggestion ())
3786 gcc_rich_location richloc (loc);
3787 richloc.add_fixit_replace (suggestion);
3789 "%qE undeclared here (not in a function);"
3790 " did you mean %qs?",
3794 error_at (loc, "%qE undeclared here (not in a function)", id);
3795 scope = current_scope;
3799 if (!objc_diagnose_private_ivar (id))
3801 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3802 if (const char *suggestion = guessed_id.suggestion ())
3804 gcc_rich_location richloc (loc);
3805 richloc.add_fixit_replace (suggestion);
3807 "%qE undeclared (first use in this function);"
3808 " did you mean %qs?",
3812 error_at (loc, "%qE undeclared (first use in this function)", id);
3816 inform (loc, "each undeclared identifier is reported only"
3817 " once for each function it appears in");
3821 /* If we are parsing old-style parameter decls, current_function_decl
3822 will be nonnull but current_function_scope will be null. */
3823 scope = current_function_scope ? current_function_scope : current_scope;
3825 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3829 /* Subroutine of lookup_label, declare_label, define_label: construct a
3830 LABEL_DECL with all the proper frills. Also create a struct
3831 c_label_vars initialized for the current scope. */
3834 make_label (location_t location, tree name, bool defining,
3835 struct c_label_vars **p_label_vars)
3837 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3838 DECL_CONTEXT (label) = current_function_decl;
3839 SET_DECL_MODE (label, VOIDmode);
3841 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3842 label_vars->shadowed = NULL;
3843 set_spot_bindings (&label_vars->label_bindings, defining);
3844 label_vars->decls_in_scope = make_tree_vector ();
3845 label_vars->gotos = NULL;
3846 *p_label_vars = label_vars;
3851 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3852 Create one if none exists so far for the current function.
3853 This is called when a label is used in a goto expression or
3854 has its address taken. */
3857 lookup_label (tree name)
3860 struct c_label_vars *label_vars;
3862 if (current_function_scope == 0)
3864 error ("label %qE referenced outside of any function", name);
3868 /* Use a label already defined or ref'd with this name, but not if
3869 it is inherited from a containing function and wasn't declared
3871 label = I_LABEL_DECL (name);
3872 if (label && (DECL_CONTEXT (label) == current_function_decl
3873 || C_DECLARED_LABEL_FLAG (label)))
3875 /* If the label has only been declared, update its apparent
3876 location to point here, for better diagnostics if it
3877 turns out not to have been defined. */
3878 if (DECL_INITIAL (label) == NULL_TREE)
3879 DECL_SOURCE_LOCATION (label) = input_location;
3883 /* No label binding for that identifier; make one. */
3884 label = make_label (input_location, name, false, &label_vars);
3886 /* Ordinary labels go in the current function scope. */
3887 bind_label (name, label, current_function_scope, label_vars);
3892 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3896 warn_about_goto (location_t goto_loc, tree label, tree decl)
3898 auto_diagnostic_group d;
3899 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3901 "jump into scope of identifier with variably modified type");
3903 if (!warning_at (goto_loc, OPT_Wjump_misses_init,
3904 "jump skips variable initialization"))
3906 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3907 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3910 /* Look up a label because of a goto statement. This is like
3911 lookup_label, but also issues any appropriate warnings. */
3914 lookup_label_for_goto (location_t loc, tree name)
3917 struct c_label_vars *label_vars;
3921 label = lookup_label (name);
3922 if (label == NULL_TREE)
3925 /* If we are jumping to a different function, we can't issue any
3927 if (DECL_CONTEXT (label) != current_function_decl)
3929 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3933 label_vars = I_LABEL_BINDING (name)->u.label;
3935 /* If the label has not yet been defined, then push this goto on a
3936 list for possible later warnings. */
3937 if (label_vars->label_bindings.scope == NULL)
3939 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3942 set_spot_bindings (&g->goto_bindings, true);
3943 vec_safe_push (label_vars->gotos, g);
3947 /* If there are any decls in label_vars->decls_in_scope, then this
3948 goto has missed the declaration of the decl. This happens for a
3954 Issue a warning or error. */
3955 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3956 warn_about_goto (loc, label, decl);
3958 if (label_vars->label_bindings.left_stmt_expr)
3960 auto_diagnostic_group d;
3961 error_at (loc, "jump into statement expression");
3962 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3968 /* Make a label named NAME in the current function, shadowing silently
3969 any that may be inherited from containing functions or containing
3970 scopes. This is called for __label__ declarations. */
3973 declare_label (tree name)
3975 struct c_binding *b = I_LABEL_BINDING (name);
3977 struct c_label_vars *label_vars;
3979 /* Check to make sure that the label hasn't already been declared
3981 if (b && B_IN_CURRENT_SCOPE (b))
3983 auto_diagnostic_group d;
3984 error ("duplicate label declaration %qE", name);
3985 locate_old_decl (b->decl);
3987 /* Just use the previous declaration. */
3991 label = make_label (input_location, name, false, &label_vars);
3992 C_DECLARED_LABEL_FLAG (label) = 1;
3994 /* Declared labels go in the current scope. */
3995 bind_label (name, label, current_scope, label_vars);
4000 /* When we define a label, issue any appropriate warnings if there are
4001 any gotos earlier in the function which jump to this label. */
4004 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
4007 struct c_goto_bindings *g;
4009 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
4011 struct c_binding *b;
4012 struct c_scope *scope;
4014 /* We have a goto to this label. The goto is going forward. In
4015 g->scope, the goto is going to skip any binding which was
4016 defined after g->bindings_in_scope. */
4017 if (g->goto_bindings.scope->has_jump_unsafe_decl)
4019 for (b = g->goto_bindings.scope->bindings;
4020 b != g->goto_bindings.bindings_in_scope;
4023 if (decl_jump_unsafe (b->decl))
4024 warn_about_goto (g->loc, label, b->decl);
4028 /* We also need to warn about decls defined in any scopes
4029 between the scope of the label and the scope of the goto. */
4030 for (scope = label_vars->label_bindings.scope;
4031 scope != g->goto_bindings.scope;
4032 scope = scope->outer)
4034 gcc_assert (scope != NULL);
4035 if (scope->has_jump_unsafe_decl)
4037 if (scope == label_vars->label_bindings.scope)
4038 b = label_vars->label_bindings.bindings_in_scope;
4040 b = scope->bindings;
4041 for (; b != NULL; b = b->prev)
4043 if (decl_jump_unsafe (b->decl))
4044 warn_about_goto (g->loc, label, b->decl);
4049 if (g->goto_bindings.stmt_exprs > 0)
4051 auto_diagnostic_group d;
4052 error_at (g->loc, "jump into statement expression");
4053 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
4058 /* Now that the label is defined, we will issue warnings about
4059 subsequent gotos to this label when we see them. */
4060 vec_safe_truncate (label_vars->gotos, 0);
4061 label_vars->gotos = NULL;
4064 /* Define a label, specifying the location in the source file.
4065 Return the LABEL_DECL node for the label, if the definition is valid.
4066 Otherwise return NULL_TREE. */
4069 define_label (location_t location, tree name)
4071 /* Find any preexisting label with this name. It is an error
4072 if that label has already been defined in this function, or
4073 if there is a containing function with a declared label with
4075 tree label = I_LABEL_DECL (name);
4078 && ((DECL_CONTEXT (label) == current_function_decl
4079 && DECL_INITIAL (label) != NULL_TREE)
4080 || (DECL_CONTEXT (label) != current_function_decl
4081 && C_DECLARED_LABEL_FLAG (label))))
4083 auto_diagnostic_group d;
4084 error_at (location, "duplicate label %qD", label);
4085 locate_old_decl (label);
4088 else if (label && DECL_CONTEXT (label) == current_function_decl)
4090 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
4092 /* The label has been used or declared already in this function,
4093 but not defined. Update its location to point to this
4095 DECL_SOURCE_LOCATION (label) = location;
4096 set_spot_bindings (&label_vars->label_bindings, true);
4098 /* Issue warnings as required about any goto statements from
4099 earlier in the function. */
4100 check_earlier_gotos (label, label_vars);
4104 struct c_label_vars *label_vars;
4106 /* No label binding for that identifier; make one. */
4107 label = make_label (location, name, true, &label_vars);
4109 /* Ordinary labels go in the current function scope. */
4110 bind_label (name, label, current_function_scope, label_vars);
4113 if (!in_system_header_at (input_location) && lookup_name (name))
4114 warning_at (location, OPT_Wtraditional,
4115 "traditional C lacks a separate namespace "
4116 "for labels, identifier %qE conflicts", name);
4118 /* Mark label as having been defined. */
4119 DECL_INITIAL (label) = error_mark_node;
4123 /* Get the bindings for a new switch statement. This is used to issue
4124 warnings as appropriate for jumps from the switch to case or
4127 struct c_spot_bindings *
4128 c_get_switch_bindings (void)
4130 struct c_spot_bindings *switch_bindings;
4132 switch_bindings = XNEW (struct c_spot_bindings);
4133 set_spot_bindings (switch_bindings, true);
4134 return switch_bindings;
4138 c_release_switch_bindings (struct c_spot_bindings *bindings)
4140 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
4144 /* This is called at the point of a case or default label to issue
4145 warnings about decls as needed. It returns true if it found an
4146 error, not just a warning. */
4149 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
4150 location_t switch_loc, location_t case_loc)
4153 struct c_scope *scope;
4156 for (scope = current_scope;
4157 scope != switch_bindings->scope;
4158 scope = scope->outer)
4160 struct c_binding *b;
4162 gcc_assert (scope != NULL);
4164 if (!scope->has_jump_unsafe_decl)
4167 for (b = scope->bindings; b != NULL; b = b->prev)
4169 if (decl_jump_unsafe (b->decl))
4171 auto_diagnostic_group d;
4173 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
4177 ("switch jumps into scope of identifier with "
4178 "variably modified type"));
4183 = warning_at (case_loc, OPT_Wjump_misses_init,
4184 "switch jumps over variable initialization");
4187 inform (switch_loc, "switch starts here");
4188 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
4195 if (switch_bindings->stmt_exprs > 0)
4198 auto_diagnostic_group d;
4199 error_at (case_loc, "switch jumps into statement expression");
4200 inform (switch_loc, "switch starts here");
4206 /* Given NAME, an IDENTIFIER_NODE,
4207 return the structure (or union or enum) definition for that name.
4208 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
4209 CODE says which kind of type the caller wants;
4210 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4211 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
4212 location where the tag was defined.
4213 If the wrong kind of type is found, an error is reported. */
4216 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
4219 struct c_binding *b = I_TAG_BINDING (name);
4220 bool thislevel = false;
4225 /* We only care about whether it's in this level if
4226 thislevel_only was set or it might be a type clash. */
4227 if (thislevel_only || TREE_CODE (b->decl) != code)
4229 /* For our purposes, a tag in the external scope is the same as
4230 a tag in the file scope. (Primarily relevant to Objective-C
4231 and its builtin structure tags, which get pushed before the
4232 file scope is created.) */
4233 if (B_IN_CURRENT_SCOPE (b)
4234 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
4238 if (thislevel_only && !thislevel)
4241 if (TREE_CODE (b->decl) != code)
4243 /* Definition isn't the kind we were looking for. */
4244 pending_invalid_xref = name;
4245 pending_invalid_xref_location = input_location;
4247 /* If in the same binding level as a declaration as a tag
4248 of a different type, this must not be allowed to
4249 shadow that tag, so give the error immediately.
4250 (For example, "struct foo; union foo;" is invalid.) */
4252 pending_xref_error ();
4261 /* Return true if a definition exists for NAME with code CODE. */
4264 tag_exists_p (enum tree_code code, tree name)
4266 struct c_binding *b = I_TAG_BINDING (name);
4268 if (b == NULL || b->decl == NULL_TREE)
4270 return TREE_CODE (b->decl) == code;
4273 /* Print an error message now
4274 for a recent invalid struct, union or enum cross reference.
4275 We don't print them immediately because they are not invalid
4276 when used in the `struct foo;' construct for shadowing. */
4279 pending_xref_error (void)
4281 if (pending_invalid_xref != NULL_TREE)
4282 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4283 pending_invalid_xref);
4284 pending_invalid_xref = NULL_TREE;
4288 /* Look up NAME in the current scope and its superiors
4289 in the namespace of variables, functions and typedefs.
4290 Return a ..._DECL node of some kind representing its definition,
4291 or return NULL_TREE if it is undefined. */
4294 lookup_name (tree name)
4296 struct c_binding *b = I_SYMBOL_BINDING (name);
4297 if (b && !b->invisible)
4299 maybe_record_typedef_use (b->decl);
4305 /* Similar to `lookup_name' but look only at the indicated scope. */
4308 lookup_name_in_scope (tree name, struct c_scope *scope)
4310 struct c_binding *b;
4312 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4313 if (B_IN_SCOPE (b, scope))
4318 /* Look for the closest match for NAME within the currently valid
4321 This finds the identifier with the lowest Levenshtein distance to
4322 NAME. If there are multiple candidates with equal minimal distance,
4323 the first one found is returned. Scopes are searched from innermost
4324 outwards, and within a scope in reverse order of declaration, thus
4325 benefiting candidates "near" to the current scope.
4327 The function also looks for similar macro names to NAME, since a
4328 misspelled macro name will not be expanded, and hence looks like an
4329 identifier to the C frontend.
4331 It also looks for start_typename keywords, to detect "singed" vs "signed"
4334 Use LOC for any deferred diagnostics. */
4337 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4339 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4341 /* First, try some well-known names in the C standard library, in case
4342 the user forgot a #include. */
4343 const char *header_hint
4344 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4347 return name_hint (NULL,
4348 new suggest_missing_header (loc,
4349 IDENTIFIER_POINTER (name),
4352 /* Only suggest names reserved for the implementation if NAME begins
4353 with an underscore. */
4354 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4356 best_match<tree, tree> bm (name);
4358 /* Look within currently valid scopes. */
4359 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4360 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4362 if (!binding->id || binding->invisible)
4364 if (binding->decl == error_mark_node)
4366 /* Don't use bindings from implicitly declared functions,
4367 as they were likely misspellings themselves. */
4368 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4369 if (C_DECL_IMPLICIT (binding->decl))
4371 /* Don't suggest names that are reserved for use by the
4372 implementation, unless NAME began with an underscore. */
4373 if (!consider_implementation_names)
4375 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4376 if (name_reserved_for_implementation_p (suggestion_str))
4381 case FUZZY_LOOKUP_TYPENAME:
4382 if (TREE_CODE (binding->decl) != TYPE_DECL)
4386 case FUZZY_LOOKUP_FUNCTION_NAME:
4387 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4389 /* Allow function pointers. */
4390 if ((VAR_P (binding->decl)
4391 || TREE_CODE (binding->decl) == PARM_DECL)
4392 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4393 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4403 bm.consider (binding->id);
4406 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4408 x = SOME_OTHER_MACRO (y);
4409 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4410 as a misspelled identifier.
4412 Use the best distance so far so that a candidate is only set if
4413 a macro is better than anything so far. This allows early rejection
4414 (without calculating the edit distance) of macro names that must have
4415 distance >= bm.get_best_distance (), and means that we only get a
4416 non-NULL result for best_macro_match if it's better than any of
4417 the identifiers already checked, which avoids needless creation
4418 of identifiers for macro hashnodes. */
4419 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4420 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4421 /* If a macro is the closest so far to NAME, use it, creating an
4422 identifier tree node for it. */
4425 const char *id = (const char *)best_macro->ident.str;
4426 tree macro_as_identifier
4427 = get_identifier_with_length (id, best_macro->ident.len);
4428 bm.set_best_so_far (macro_as_identifier,
4429 bmm.get_best_distance (),
4430 bmm.get_best_candidate_length ());
4433 /* Try the "start_typename" keywords to detect
4434 "singed" vs "signed" typos. */
4435 if (kind == FUZZY_LOOKUP_TYPENAME)
4437 for (unsigned i = 0; i < num_c_common_reswords; i++)
4439 const c_common_resword *resword = &c_common_reswords[i];
4440 if (!c_keyword_starts_typename (resword->rid))
4442 tree resword_identifier = ridpointers [resword->rid];
4443 if (!resword_identifier)
4445 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4446 bm.consider (resword_identifier);
4450 tree best = bm.get_best_meaningful_candidate ();
4452 return name_hint (IDENTIFIER_POINTER (best), NULL);
4454 return name_hint (NULL, NULL);
4458 /* Handle the standard [[nodiscard]] attribute. */
4461 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4462 int /*flags*/, bool *no_add_attrs)
4464 if (TREE_CODE (*node) == FUNCTION_DECL)
4466 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4467 warning_at (DECL_SOURCE_LOCATION (*node),
4468 OPT_Wattributes, "%qE attribute applied to %qD with void "
4469 "return type", name, *node);
4471 else if (RECORD_OR_UNION_TYPE_P (*node)
4472 || TREE_CODE (*node) == ENUMERAL_TYPE)
4476 pedwarn (input_location,
4477 OPT_Wattributes, "%qE attribute can only be applied to "
4478 "functions or to structure, union or enumeration types", name);
4479 *no_add_attrs = true;
4484 /* Handle the standard [[noreturn]] attribute. */
4487 handle_std_noreturn_attribute (tree *node, tree name, tree args,
4488 int flags, bool *no_add_attrs)
4490 /* Unlike GNU __attribute__ ((noreturn)), the standard [[noreturn]]
4491 only applies to functions, not function pointers. */
4492 if (TREE_CODE (*node) == FUNCTION_DECL)
4493 return handle_noreturn_attribute (node, name, args, flags, no_add_attrs);
4496 pedwarn (input_location, OPT_Wattributes,
4497 "standard %qE attribute can only be applied to functions",
4499 *no_add_attrs = true;
4504 /* Table of supported standard (C2x) attributes. */
4505 const struct attribute_spec std_attribute_table[] =
4507 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4508 affects_type_identity, handler, exclude } */
4509 { "_Noreturn", 0, 0, false, false, false, false,
4510 handle_std_noreturn_attribute, NULL },
4511 { "deprecated", 0, 1, false, false, false, false,
4512 handle_deprecated_attribute, NULL },
4513 { "fallthrough", 0, 0, false, false, false, false,
4514 handle_fallthrough_attribute, NULL },
4515 { "maybe_unused", 0, 0, false, false, false, false,
4516 handle_unused_attribute, NULL },
4517 { "nodiscard", 0, 1, false, false, false, false,
4518 handle_nodiscard_attribute, NULL },
4519 { "noreturn", 0, 0, false, false, false, false,
4520 handle_std_noreturn_attribute, NULL },
4521 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4524 /* Create the predefined scalar types of C,
4525 and some nodes representing standard constants (0, 1, (void *) 0).
4526 Initialize the global scope.
4527 Make definitions for built-in primitive functions. */
4530 c_init_decl_processing (void)
4532 location_t save_loc = input_location;
4534 /* Initialize reserved words for parser. */
4537 register_scoped_attributes (std_attribute_table, NULL);
4539 current_function_decl = NULL_TREE;
4541 gcc_obstack_init (&parser_obstack);
4543 /* Make the externals scope. */
4545 external_scope = current_scope;
4547 /* Declarations from c_common_nodes_and_builtins must not be associated
4548 with this input file, lest we get differences between using and not
4549 using preprocessed headers. */
4550 input_location = BUILTINS_LOCATION;
4552 c_common_nodes_and_builtins ();
4554 /* In C, comparisons and TRUTH_* expressions have type int. */
4555 truthvalue_type_node = integer_type_node;
4556 truthvalue_true_node = integer_one_node;
4557 truthvalue_false_node = integer_zero_node;
4559 /* Even in C99, which has a real boolean type. */
4560 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4561 boolean_type_node));
4563 /* C-specific nullptr initialization. */
4564 record_builtin_type (RID_MAX, "nullptr_t", nullptr_type_node);
4565 /* The size and alignment of nullptr_t is the same as for a pointer to
4567 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode));
4569 input_location = save_loc;
4571 make_fname_decl = c_make_fname_decl;
4572 start_fname_decls ();
4575 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4576 give the decl, NAME is the initialization string and TYPE_DEP
4577 indicates whether NAME depended on the type of the function. As we
4578 don't yet implement delayed emission of static data, we mark the
4579 decl as emitted so it is not placed in the output. Anything using
4580 it must therefore pull out the STRING_CST initializer directly.
4584 c_make_fname_decl (location_t loc, tree id, int type_dep)
4586 const char *name = fname_as_string (type_dep);
4587 tree decl, type, init;
4588 size_t length = strlen (name);
4590 type = build_array_type (char_type_node,
4591 build_index_type (size_int (length)));
4592 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4594 decl = build_decl (loc, VAR_DECL, id, type);
4596 TREE_STATIC (decl) = 1;
4597 TREE_READONLY (decl) = 1;
4598 DECL_ARTIFICIAL (decl) = 1;
4600 init = build_string (length + 1, name);
4601 free (CONST_CAST (char *, name));
4602 TREE_TYPE (init) = type;
4603 DECL_INITIAL (decl) = init;
4605 TREE_USED (decl) = 1;
4607 if (current_function_decl
4608 /* For invalid programs like this:
4611 const char* p = __FUNCTION__;
4613 the __FUNCTION__ is believed to appear in K&R style function
4614 parameter declarator. In that case we still don't have
4616 && current_function_scope)
4618 DECL_CONTEXT (decl) = current_function_decl;
4619 bind (id, decl, current_function_scope,
4620 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4623 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4629 c_builtin_function (tree decl)
4631 tree type = TREE_TYPE (decl);
4632 tree id = DECL_NAME (decl);
4634 const char *name = IDENTIFIER_POINTER (id);
4635 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4637 /* Should never be called on a symbol with a preexisting meaning. */
4638 gcc_assert (!I_SYMBOL_BINDING (id));
4640 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4643 /* Builtins in the implementation namespace are made visible without
4644 needing to be explicitly declared. See push_file_scope. */
4645 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4647 DECL_CHAIN (decl) = visible_builtins;
4648 visible_builtins = decl;
4655 c_builtin_function_ext_scope (tree decl)
4657 tree type = TREE_TYPE (decl);
4658 tree id = DECL_NAME (decl);
4660 const char *name = IDENTIFIER_POINTER (id);
4661 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4664 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4667 /* Builtins in the implementation namespace are made visible without
4668 needing to be explicitly declared. See push_file_scope. */
4669 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4671 DECL_CHAIN (decl) = visible_builtins;
4672 visible_builtins = decl;
4678 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */
4681 c_simulate_builtin_function_decl (tree decl)
4683 tree type = TREE_TYPE (decl);
4684 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4685 return pushdecl (decl);
4688 /* Warn about attributes in a context where they are unused
4689 (attribute-declarations, except for the "fallthrough" case, and
4690 attributes on statements). */
4693 c_warn_unused_attributes (tree attrs)
4695 for (tree t = attrs; t != NULL_TREE; t = TREE_CHAIN (t))
4696 if (get_attribute_namespace (t) == NULL_TREE)
4697 /* The specifications of standard attributes mean this is a
4698 constraint violation. */
4699 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4700 get_attribute_name (t));
4701 else if (!attribute_ignored_p (t))
4702 warning (OPT_Wattributes, "%qE attribute ignored",
4703 get_attribute_name (t));
4706 /* Warn for standard attributes being applied to a type that is not
4707 being defined, where that is a constraint violation, and return a
4708 list of attributes with them removed. */
4711 c_warn_type_attributes (tree attrs)
4713 tree *attr_ptr = &attrs;
4715 if (get_attribute_namespace (*attr_ptr) == NULL_TREE)
4717 pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
4718 get_attribute_name (*attr_ptr));
4719 *attr_ptr = TREE_CHAIN (*attr_ptr);
4722 attr_ptr = &TREE_CHAIN (*attr_ptr);
4726 /* Called when a declaration is seen that contains no names to declare.
4727 If its type is a reference to a structure, union or enum inherited
4728 from a containing scope, shadow that tag name for the current scope
4729 with a forward reference.
4730 If its type defines a new named structure or union
4731 or defines an enum, it is valid but we need not do anything here.
4732 Otherwise, it is an error. */
4735 shadow_tag (const struct c_declspecs *declspecs)
4737 shadow_tag_warned (declspecs, 0);
4740 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4743 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4745 bool found_tag = false;
4747 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4749 tree value = declspecs->type;
4750 enum tree_code code = TREE_CODE (value);
4752 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4753 /* Used to test also that TYPE_SIZE (value) != 0.
4754 That caused warning for `struct foo;' at top level in the file. */
4756 tree name = TYPE_NAME (value);
4761 if (declspecs->restrict_p)
4763 error ("invalid use of %<restrict%>");
4767 if (name == NULL_TREE)
4769 if (warned != 1 && code != ENUMERAL_TYPE)
4770 /* Empty unnamed enum OK */
4772 pedwarn (input_location, 0,
4773 "unnamed struct/union that defines no instances");
4777 else if (declspecs->typespec_kind != ctsk_tagdef
4778 && declspecs->typespec_kind != ctsk_tagfirstref
4779 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4780 && declspecs->storage_class != csc_none)
4783 pedwarn (input_location, 0,
4784 "empty declaration with storage class specifier "
4785 "does not redeclare tag");
4787 pending_xref_error ();
4789 else if (declspecs->typespec_kind != ctsk_tagdef
4790 && declspecs->typespec_kind != ctsk_tagfirstref
4791 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4792 && (declspecs->const_p
4793 || declspecs->volatile_p
4794 || declspecs->atomic_p
4795 || declspecs->restrict_p
4796 || declspecs->address_space))
4799 pedwarn (input_location, 0,
4800 "empty declaration with type qualifier "
4801 "does not redeclare tag");
4803 pending_xref_error ();
4805 else if (declspecs->typespec_kind != ctsk_tagdef
4806 && declspecs->typespec_kind != ctsk_tagfirstref
4807 && declspecs->typespec_kind != ctsk_tagfirstref_attrs
4808 && declspecs->alignas_p)
4811 pedwarn (input_location, 0,
4812 "empty declaration with %<_Alignas%> "
4813 "does not redeclare tag");
4815 pending_xref_error ();
4819 pending_invalid_xref = NULL_TREE;
4820 t = lookup_tag (code, name, true, NULL);
4824 t = make_node (code);
4825 pushtag (input_location, name, t);
4831 if (warned != 1 && !in_system_header_at (input_location))
4833 pedwarn (input_location, 0,
4834 "useless type name in empty declaration");
4839 else if (warned != 1 && !in_system_header_at (input_location)
4840 && declspecs->typedef_p)
4842 pedwarn (input_location, 0, "useless type name in empty declaration");
4846 pending_invalid_xref = NULL_TREE;
4848 if (declspecs->inline_p)
4850 error ("%<inline%> in empty declaration");
4854 if (declspecs->noreturn_p)
4856 error ("%<_Noreturn%> in empty declaration");
4860 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4862 error ("%<auto%> in file-scope empty declaration");
4866 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4868 error ("%<register%> in file-scope empty declaration");
4872 if (!warned && !in_system_header_at (input_location)
4873 && declspecs->storage_class != csc_none)
4875 warning (0, "useless storage class specifier in empty declaration");
4879 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4881 warning (0, "useless %qs in empty declaration",
4882 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4887 && !in_system_header_at (input_location)
4888 && (declspecs->const_p
4889 || declspecs->volatile_p
4890 || declspecs->atomic_p
4891 || declspecs->restrict_p
4892 || declspecs->address_space))
4894 warning (0, "useless type qualifier in empty declaration");
4898 if (!warned && !in_system_header_at (input_location)
4899 && declspecs->alignas_p)
4901 warning (0, "useless %<_Alignas%> in empty declaration");
4907 && (declspecs->typespec_kind == ctsk_tagref_attrs
4908 || declspecs->typespec_kind == ctsk_tagfirstref_attrs))
4910 /* Standard attributes after the "struct" or "union" keyword are
4911 only permitted when the contents of the type are defined, or
4912 in the form "struct-or-union attribute-specifier-sequence
4913 identifier;". If the ';' was not present, attributes were
4914 diagnosed in the parser. Here, ensure that any other useless
4915 elements of the declaration result in a pedwarn, not just a
4916 warning. Forward declarations of enum types are not part of
4917 standard C, but handle them the same. */
4918 pedwarn (input_location, 0,
4919 "invalid use of attributes in empty declaration");
4925 if (declspecs->declspecs_seen_p
4926 && !declspecs->non_std_attrs_seen_p)
4927 /* An attribute declaration (but not a fallthrough attribute
4928 declaration, which was handled separately); warn if there
4929 are any attributes being ignored (but not if the attributes
4931 c_warn_unused_attributes (declspecs->attrs);
4932 else if (!found_tag)
4933 pedwarn (input_location, 0, "empty declaration");
4938 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4939 bits. SPECS represents declaration specifiers that the grammar
4940 only permits to contain type qualifiers and attributes. */
4943 quals_from_declspecs (const struct c_declspecs *specs)
4945 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4946 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4947 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4948 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4949 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4950 gcc_assert (!specs->type
4951 && !specs->decl_attr
4952 && specs->typespec_word == cts_none
4953 && specs->storage_class == csc_none
4954 && !specs->typedef_p
4955 && !specs->explicit_signed_p
4956 && !specs->deprecated_p
4957 && !specs->unavailable_p
4959 && !specs->long_long_p
4962 && !specs->unsigned_p
4963 && !specs->complex_p
4965 && !specs->noreturn_p
4966 && !specs->thread_p);
4970 /* Construct an array declarator. LOC is the location of the
4971 beginning of the array (usually the opening brace). EXPR is the
4972 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4973 inside the [] (to be applied to the pointer to which a parameter
4974 array is converted). STATIC_P is true if "static" is inside the
4975 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4976 VLA of unspecified length which is nevertheless a complete type,
4977 false otherwise. The field for the contained declarator is left to
4978 be filled in by set_array_declarator_inner. */
4980 struct c_declarator *
4981 build_array_declarator (location_t loc,
4982 tree expr, struct c_declspecs *quals, bool static_p,
4985 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4986 struct c_declarator);
4987 declarator->id_loc = loc;
4988 declarator->kind = cdk_array;
4989 declarator->declarator = 0;
4990 declarator->u.array.dimen = expr;
4993 declarator->u.array.attrs = quals->attrs;
4994 declarator->u.array.quals = quals_from_declspecs (quals);
4998 declarator->u.array.attrs = NULL_TREE;
4999 declarator->u.array.quals = 0;
5001 declarator->u.array.static_p = static_p;
5002 declarator->u.array.vla_unspec_p = vla_unspec_p;
5003 if (static_p || quals != NULL)
5004 pedwarn_c90 (loc, OPT_Wpedantic,
5005 "ISO C90 does not support %<static%> or type "
5006 "qualifiers in parameter array declarators");
5008 pedwarn_c90 (loc, OPT_Wpedantic,
5009 "ISO C90 does not support %<[*]%> array declarators");
5012 if (!current_scope->parm_flag)
5015 error_at (loc, "%<[*]%> not allowed in other than "
5016 "function prototype scope");
5017 declarator->u.array.vla_unspec_p = false;
5020 current_scope->had_vla_unspec = true;
5025 /* Set the contained declarator of an array declarator. DECL is the
5026 declarator, as constructed by build_array_declarator; INNER is what
5027 appears on the left of the []. */
5029 struct c_declarator *
5030 set_array_declarator_inner (struct c_declarator *decl,
5031 struct c_declarator *inner)
5033 decl->declarator = inner;
5037 /* Determine whether TYPE is a ISO C99 flexible array memeber type "[]". */
5039 flexible_array_member_type_p (const_tree type)
5041 if (TREE_CODE (type) == ARRAY_TYPE
5042 && TYPE_SIZE (type) == NULL_TREE
5043 && TYPE_DOMAIN (type) != NULL_TREE
5044 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5050 /* Determine whether TYPE is a one-element array type "[1]". */
5052 one_element_array_type_p (const_tree type)
5054 if (TREE_CODE (type) != ARRAY_TYPE)
5056 return integer_zerop (array_type_nelts (type));
5059 /* Determine whether TYPE is a zero-length array type "[0]". */
5061 zero_length_array_type_p (const_tree type)
5063 if (TREE_CODE (type) == ARRAY_TYPE)
5064 if (tree type_size = TYPE_SIZE_UNIT (type))
5065 if ((integer_zerop (type_size))
5066 && TYPE_DOMAIN (type) != NULL_TREE
5067 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5072 /* INIT is a constructor that forms DECL's initializer. If the final
5073 element initializes a flexible array field, add the size of that
5074 initializer to DECL's size. */
5077 add_flexible_array_elts_to_size (tree decl, tree init)
5081 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5084 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5085 type = TREE_TYPE (elt);
5086 if (flexible_array_member_type_p (type))
5088 complete_array_type (&type, elt, false);
5090 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5091 DECL_SIZE_UNIT (decl)
5092 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5096 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5097 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5098 before the type name, and set *EXPR_CONST_OPERANDS, if
5099 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5100 appear in a constant expression. */
5103 groktypename (struct c_type_name *type_name, tree *expr,
5104 bool *expr_const_operands)
5107 tree attrs = type_name->specs->attrs;
5109 type_name->specs->attrs = NULL_TREE;
5111 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5112 false, NULL, &attrs, expr, expr_const_operands,
5115 /* Apply attributes. */
5116 attrs = c_warn_type_attributes (attrs);
5117 decl_attributes (&type, attrs, 0);
5122 /* Looks up the most recent pushed declaration corresponding to DECL. */
5125 lookup_last_decl (tree decl)
5127 tree last_decl = lookup_name (DECL_NAME (decl));
5129 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5133 /* Wrapper for decl_attributes that adds some implicit attributes
5134 to VAR_DECLs or FUNCTION_DECLs. */
5137 c_decl_attributes (tree *node, tree attributes, int flags)
5139 /* Add implicit "omp declare target" attribute if requested. */
5140 if (vec_safe_length (current_omp_declare_target_attribute)
5141 && ((VAR_P (*node) && is_global_var (*node))
5142 || TREE_CODE (*node) == FUNCTION_DECL))
5144 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5145 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5146 NULL_TREE, attributes);
5149 attributes = tree_cons (get_identifier ("omp declare target"),
5150 NULL_TREE, attributes);
5151 attributes = tree_cons (get_identifier ("omp declare target block"),
5152 NULL_TREE, attributes);
5154 if (TREE_CODE (*node) == FUNCTION_DECL)
5157 = current_omp_declare_target_attribute->last ().device_type;
5158 device_type = MAX (device_type, 0);
5159 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0
5160 && !lookup_attribute ("omp declare target host", attributes))
5162 = tree_cons (get_identifier ("omp declare target host"),
5163 NULL_TREE, attributes);
5164 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0
5165 && !lookup_attribute ("omp declare target nohost", attributes))
5167 = tree_cons (get_identifier ("omp declare target nohost"),
5168 NULL_TREE, attributes);
5172 /* Look up the current declaration with all the attributes merged
5173 so far so that attributes on the current declaration that's
5174 about to be pushed that conflict with the former can be detected,
5175 diagnosed, and rejected as appropriate. */
5176 tree last_decl = lookup_last_decl (*node);
5177 return decl_attributes (node, attributes, flags, last_decl);
5181 /* Decode a declarator in an ordinary declaration or data definition.
5182 This is called as soon as the type information and variable name
5183 have been parsed, before parsing the initializer if any.
5184 Here we create the ..._DECL node, fill in its type,
5185 and put it on the list of decls for the current context.
5186 When nonnull, set *LASTLOC to the location of the prior declaration
5187 of the same entity if one exists.
5188 The ..._DECL node is returned as the value.
5190 Exception: for arrays where the length is not specified,
5191 the type is left null, to be filled in by `finish_decl'.
5193 Function definitions do not come here; they go to start_function
5194 instead. However, external and forward declarations of functions
5195 do go through here. Structure field declarations are done by
5196 grokfield and not through here. */
5199 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5200 bool initialized, tree attributes, location_t *lastloc /* = NULL */)
5204 tree expr = NULL_TREE;
5205 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5207 /* An object declared as __attribute__((unavailable)) suppresses
5208 warnings and errors from __attribute__((deprecated/unavailable))
5210 An object declared as __attribute__((deprecated)) suppresses
5211 warnings of uses of other deprecated items. */
5212 if (lookup_attribute ("unavailable", attributes))
5213 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5214 else if (lookup_attribute ("deprecated", attributes))
5215 deprecated_state = DEPRECATED_SUPPRESS;
5217 decl = grokdeclarator (declarator, declspecs,
5218 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5220 if (!decl || decl == error_mark_node)
5223 if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5224 if (lastdecl != error_mark_node)
5225 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5228 add_stmt (fold_convert (void_type_node, expr));
5230 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5231 && TREE_PUBLIC (decl))
5232 warning (OPT_Wmain, "%q+D is usually a function", decl);
5235 /* Is it valid for this decl to have an initializer at all?
5236 If not, set INITIALIZED to zero, which will indirectly
5237 tell 'finish_decl' to ignore the initializer once it is parsed. */
5238 switch (TREE_CODE (decl))
5241 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5242 initialized = false;
5246 error ("function %qD is initialized like a variable", decl);
5247 initialized = false;
5251 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5252 error ("parameter %qD is initialized", decl);
5253 initialized = false;
5257 /* Don't allow initializations for incomplete types except for
5258 arrays which might be completed by the initialization. */
5260 /* This can happen if the array size is an undefined macro.
5261 We already gave a warning, so we don't need another one. */
5262 if (TREE_TYPE (decl) == error_mark_node)
5263 initialized = false;
5264 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5266 /* A complete type is ok if size is fixed. If the size is
5267 variable, an empty initializer is OK and nonempty
5268 initializers will be diagnosed in the parser. */
5270 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5272 error ("variable %qD has initializer but incomplete type", decl);
5273 initialized = false;
5279 if (current_scope == file_scope)
5280 TREE_STATIC (decl) = 1;
5282 /* Tell 'pushdecl' this is an initialized decl
5283 even though we don't yet have the initializer expression.
5284 Also tell 'finish_decl' it may store the real initializer. */
5285 DECL_INITIAL (decl) = error_mark_node;
5288 /* If this is a function declaration, write a record describing it to the
5289 prototypes file (if requested). */
5291 if (TREE_CODE (decl) == FUNCTION_DECL)
5292 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5294 /* ANSI specifies that a tentative definition which is not merged with
5295 a non-tentative definition behaves exactly like a definition with an
5296 initializer equal to zero. (Section 3.7.2)
5298 -fno-common gives strict ANSI behavior, though this tends to break
5299 a large body of code that grew up without this rule.
5301 Thread-local variables are never common, since there's no entrenched
5302 body of code to break, and it allows more efficient variable references
5303 in the presence of dynamic linking. */
5307 && TREE_PUBLIC (decl)
5308 && !DECL_THREAD_LOCAL_P (decl)
5310 DECL_COMMON (decl) = 1;
5312 /* Set attributes here so if duplicate decl, will have proper attributes. */
5313 c_decl_attributes (&decl, attributes, 0);
5315 /* Handle gnu_inline attribute. */
5316 if (declspecs->inline_p
5317 && !flag_gnu89_inline
5318 && TREE_CODE (decl) == FUNCTION_DECL
5319 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5320 || current_function_decl))
5322 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5324 else if (declspecs->storage_class != csc_static)
5325 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5328 if (TREE_CODE (decl) == FUNCTION_DECL
5329 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5331 struct c_declarator *ce = declarator;
5333 if (ce->kind == cdk_pointer)
5334 ce = declarator->declarator;
5335 if (ce->kind == cdk_function)
5337 tree args = ce->u.arg_info->parms;
5338 for (; args; args = DECL_CHAIN (args))
5340 tree type = TREE_TYPE (args);
5341 if (type && INTEGRAL_TYPE_P (type)
5342 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5343 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5348 if (TREE_CODE (decl) == FUNCTION_DECL
5349 && DECL_DECLARED_INLINE_P (decl)
5350 && DECL_UNINLINABLE (decl)
5351 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5352 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5355 /* C99 6.7.4p3: An inline definition of a function with external
5356 linkage shall not contain a definition of a modifiable object
5357 with static storage duration... */
5359 && current_scope != file_scope
5360 && TREE_STATIC (decl)
5361 && !TREE_READONLY (decl)
5362 && DECL_DECLARED_INLINE_P (current_function_decl)
5363 && DECL_EXTERNAL (current_function_decl))
5364 record_inline_static (input_location, current_function_decl,
5365 decl, csi_modifiable);
5367 if (c_dialect_objc ()
5368 && VAR_OR_FUNCTION_DECL_P (decl))
5369 objc_check_global_decl (decl);
5371 /* Add this decl to the current scope.
5372 TEM may equal DECL or it may be a previous decl of the same name. */
5373 tem = pushdecl (decl);
5375 if (initialized && DECL_EXTERNAL (tem))
5377 DECL_EXTERNAL (tem) = 0;
5378 TREE_STATIC (tem) = 1;
5384 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5385 DECL or the non-array element type if DECL is an uninitialized array.
5386 If that type has a const member, diagnose this. */
5389 diagnose_uninitialized_cst_member (tree decl, tree type)
5392 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5395 if (TREE_CODE (field) != FIELD_DECL)
5397 field_type = strip_array_types (TREE_TYPE (field));
5399 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5401 auto_diagnostic_group d;
5402 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5403 "uninitialized const member in %qT is invalid in C++",
5404 strip_array_types (TREE_TYPE (decl))))
5405 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5408 if (RECORD_OR_UNION_TYPE_P (field_type))
5409 diagnose_uninitialized_cst_member (decl, field_type);
5413 /* Finish processing of a declaration;
5414 install its initial value.
5415 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5416 If the length of an array type is not known before,
5417 it must be determined now, from the initial value, or it is an error.
5419 INIT_LOC is the location of the initial value. */
5422 finish_decl (tree decl, location_t init_loc, tree init,
5423 tree origtype, tree asmspec_tree)
5426 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5427 const char *asmspec = 0;
5429 /* If a name was specified, get the string. */
5430 if (VAR_OR_FUNCTION_DECL_P (decl)
5431 && DECL_FILE_SCOPE_P (decl))
5432 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5434 asmspec = TREE_STRING_POINTER (asmspec_tree);
5437 && TREE_STATIC (decl)
5438 && global_bindings_p ())
5439 /* So decl is a global variable. Record the types it uses
5440 so that we can decide later to emit debug info for them. */
5441 record_types_used_by_current_var_decl (decl);
5443 /* If `start_decl' didn't like having an initialization, ignore it now. */
5444 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5447 /* Don't crash if parm is initialized. */
5448 if (TREE_CODE (decl) == PARM_DECL)
5452 store_init_value (init_loc, decl, init, origtype);
5454 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5455 || TREE_CODE (decl) == FIELD_DECL))
5456 objc_check_decl (decl);
5458 type = TREE_TYPE (decl);
5460 /* Deduce size of array from initialization, if not already known.
5461 This is only needed for an initialization in the current scope;
5462 it must not be done for a file-scope initialization of a
5463 declaration with external linkage, redeclared in an inner scope
5464 with the outer declaration shadowed in an intermediate scope. */
5465 if (TREE_CODE (type) == ARRAY_TYPE
5466 && TYPE_DOMAIN (type) == NULL_TREE
5467 && TREE_CODE (decl) != TYPE_DECL
5468 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5471 = (TREE_STATIC (decl)
5472 /* Even if pedantic, an external linkage array
5473 may have incomplete type at first. */
5474 ? pedantic && !TREE_PUBLIC (decl)
5475 : !DECL_EXTERNAL (decl));
5477 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5480 /* Get the completed type made by complete_array_type. */
5481 type = TREE_TYPE (decl);
5486 error ("initializer fails to determine size of %q+D", decl);
5491 error ("array size missing in %q+D", decl);
5495 error ("zero or negative size array %q+D", decl);
5499 /* For global variables, update the copy of the type that
5500 exists in the binding. */
5501 if (TREE_PUBLIC (decl))
5503 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5504 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5505 b_ext = b_ext->shadowed;
5506 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5508 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5509 b_ext->u.type = composite_type (b_ext->u.type, type);
5511 b_ext->u.type = type;
5520 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5521 TREE_TYPE (DECL_INITIAL (decl)) = type;
5523 relayout_decl (decl);
5526 /* Look for braced array initializers for character arrays and
5527 recursively convert them into STRING_CSTs. */
5528 if (tree init = DECL_INITIAL (decl))
5529 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5533 if (init && TREE_CODE (init) == CONSTRUCTOR)
5534 add_flexible_array_elts_to_size (decl, init);
5536 complete_flexible_array_elts (DECL_INITIAL (decl));
5538 if (is_global_var (decl))
5540 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5541 ? TCTX_THREAD_STORAGE
5542 : TCTX_STATIC_STORAGE);
5543 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5544 TREE_TYPE (decl) = error_mark_node;
5547 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5548 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5549 layout_decl (decl, 0);
5551 if (DECL_SIZE (decl) == NULL_TREE
5552 /* Don't give an error if we already gave one earlier. */
5553 && TREE_TYPE (decl) != error_mark_node
5554 && (TREE_STATIC (decl)
5555 /* A static variable with an incomplete type
5556 is an error if it is initialized.
5557 Also if it is not file scope.
5558 Otherwise, let it through, but if it is not `extern'
5559 then it may cause an error message later. */
5560 ? (DECL_INITIAL (decl) != NULL_TREE
5561 || !DECL_FILE_SCOPE_P (decl))
5562 /* An automatic variable with an incomplete type
5564 : !DECL_EXTERNAL (decl)))
5566 error ("storage size of %q+D isn%'t known", decl);
5567 TREE_TYPE (decl) = error_mark_node;
5570 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5571 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5572 && DECL_SIZE (decl) == NULL_TREE
5573 && TREE_STATIC (decl))
5574 incomplete_record_decls.safe_push (decl);
5576 if (is_global_var (decl)
5577 && DECL_SIZE (decl) != NULL_TREE
5578 && TREE_TYPE (decl) != error_mark_node)
5580 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5581 constant_expression_warning (DECL_SIZE (decl));
5584 error ("storage size of %q+D isn%'t constant", decl);
5585 TREE_TYPE (decl) = error_mark_node;
5589 if (TREE_USED (type))
5591 TREE_USED (decl) = 1;
5592 DECL_READ_P (decl) = 1;
5596 /* If this is a function and an assembler name is specified, reset DECL_RTL
5597 so we can give it its new name. Also, update builtin_decl if it
5598 was a normal built-in. */
5599 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5601 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5602 set_builtin_user_assembler_name (decl, asmspec);
5603 set_user_assembler_name (decl, asmspec);
5606 /* If #pragma weak was used, mark the decl weak now. */
5607 maybe_apply_pragma_weak (decl);
5609 /* Output the assembler code and/or RTL code for variables and functions,
5610 unless the type is an undefined structure or union.
5611 If not, it will get done when the type is completed. */
5613 if (VAR_OR_FUNCTION_DECL_P (decl))
5615 /* Determine the ELF visibility. */
5616 if (TREE_PUBLIC (decl))
5617 c_determine_visibility (decl);
5619 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5620 if (c_dialect_objc ())
5621 objc_check_decl (decl);
5625 /* If this is not a static variable, issue a warning.
5626 It doesn't make any sense to give an ASMSPEC for an
5627 ordinary, non-register local variable. Historically,
5628 GCC has accepted -- but ignored -- the ASMSPEC in
5630 if (!DECL_FILE_SCOPE_P (decl)
5632 && !C_DECL_REGISTER (decl)
5633 && !TREE_STATIC (decl))
5634 warning (0, "ignoring %<asm%> specifier for non-static local "
5635 "variable %q+D", decl);
5637 set_user_assembler_name (decl, asmspec);
5640 if (DECL_FILE_SCOPE_P (decl))
5642 if (DECL_INITIAL (decl) == NULL_TREE
5643 || DECL_INITIAL (decl) == error_mark_node)
5644 /* Don't output anything
5645 when a tentative file-scope definition is seen.
5646 But at end of compilation, do output code for them. */
5647 DECL_DEFER_OUTPUT (decl) = 1;
5648 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5649 DECL_HARD_REGISTER (decl) = 1;
5650 rest_of_decl_compilation (decl, true, 0);
5652 if (TREE_CODE (decl) == FUNCTION_DECL)
5654 tree parms = DECL_ARGUMENTS (decl);
5655 const bool builtin = fndecl_built_in_p (decl);
5656 if (tree access = build_attr_access_from_parms (parms, !builtin))
5657 decl_attributes (&decl, access, 0);
5662 /* In conjunction with an ASMSPEC, the `register'
5663 keyword indicates that we should place the variable
5664 in a particular register. */
5665 if (asmspec && C_DECL_REGISTER (decl))
5667 DECL_HARD_REGISTER (decl) = 1;
5668 /* This cannot be done for a structure with volatile
5669 fields, on which DECL_REGISTER will have been
5671 if (!DECL_REGISTER (decl))
5672 error ("cannot put object with volatile field into register");
5675 if (TREE_CODE (decl) != FUNCTION_DECL)
5677 /* If we're building a variable sized type, and we might be
5678 reachable other than via the top of the current binding
5679 level, then create a new BIND_EXPR so that we deallocate
5680 the object at the right time. */
5681 /* Note that DECL_SIZE can be null due to errors. */
5682 if (DECL_SIZE (decl)
5683 && !TREE_CONSTANT (DECL_SIZE (decl))
5684 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5687 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5688 TREE_SIDE_EFFECTS (bind) = 1;
5690 BIND_EXPR_BODY (bind) = push_stmt_list ();
5692 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5698 if (!DECL_FILE_SCOPE_P (decl))
5700 /* Recompute the RTL of a local array now
5701 if it used to be an incomplete type. */
5702 if (was_incomplete && !is_global_var (decl))
5704 /* If we used it already as memory, it must stay in memory. */
5705 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5706 /* If it's still incomplete now, no init will save it. */
5707 if (DECL_SIZE (decl) == NULL_TREE)
5708 DECL_INITIAL (decl) = NULL_TREE;
5713 if (TREE_CODE (decl) == TYPE_DECL)
5715 if (!DECL_FILE_SCOPE_P (decl)
5716 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5717 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5719 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5722 /* Install a cleanup (aka destructor) if one was given. */
5723 if (VAR_P (decl) && !TREE_STATIC (decl))
5725 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5728 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5729 tree cleanup_decl = lookup_name (cleanup_id);
5731 vec<tree, va_gc> *v;
5733 /* Build "cleanup(&decl)" for the destructor. */
5734 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5736 v->quick_push (cleanup);
5737 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5738 vNULL, cleanup_decl, v, NULL);
5741 /* Don't warn about decl unused; the cleanup uses it. */
5742 TREE_USED (decl) = 1;
5743 TREE_USED (cleanup_decl) = 1;
5744 DECL_READ_P (decl) = 1;
5746 push_cleanup (decl, cleanup, false);
5752 && !DECL_EXTERNAL (decl)
5753 && DECL_INITIAL (decl) == NULL_TREE)
5755 type = strip_array_types (type);
5756 if (TREE_READONLY (decl))
5757 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5758 "uninitialized %<const %D%> is invalid in C++", decl);
5759 else if (RECORD_OR_UNION_TYPE_P (type)
5760 && C_TYPE_FIELDS_READONLY (type))
5761 diagnose_uninitialized_cst_member (decl, type);
5766 && lookup_attribute ("omp declare target implicit",
5767 DECL_ATTRIBUTES (decl)))
5769 DECL_ATTRIBUTES (decl)
5770 = remove_attribute ("omp declare target implicit",
5771 DECL_ATTRIBUTES (decl));
5772 if (!omp_mappable_type (TREE_TYPE (decl)))
5773 error ("%q+D in declare target directive does not have mappable type",
5775 else if (!lookup_attribute ("omp declare target",
5776 DECL_ATTRIBUTES (decl))
5777 && !lookup_attribute ("omp declare target link",
5778 DECL_ATTRIBUTES (decl)))
5780 DECL_ATTRIBUTES (decl)
5781 = tree_cons (get_identifier ("omp declare target"),
5782 NULL_TREE, DECL_ATTRIBUTES (decl));
5783 symtab_node *node = symtab_node::get (decl);
5786 node->offloadable = 1;
5787 if (ENABLE_OFFLOADING)
5789 g->have_offload = true;
5790 if (is_a <varpool_node *> (node))
5791 vec_safe_push (offload_vars, decl);
5797 /* This is the last point we can lower alignment so give the target the
5800 && !is_global_var (decl)
5801 && !DECL_HARD_REGISTER (decl))
5802 targetm.lower_local_decl_alignment (decl);
5804 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5807 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5808 EXPR is NULL or a pointer to an expression that needs to be
5809 evaluated for the side effects of array size expressions in the
5813 grokparm (const struct c_parm *parm, tree *expr)
5815 tree attrs = parm->attrs;
5816 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5817 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5819 decl_attributes (&decl, attrs, 0);
5824 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5825 described by PARM, concatenated onto attributes ATTRS.
5826 The spec consists of one dollar symbol for each specified variable
5827 bound, one asterisk for each unspecified variable bound, followed
5828 by at most one specification of the most significant bound of
5829 an ordinary array parameter. For ordinary arrays the specification
5830 is either the constant bound itself, or the space character for
5831 an array with an unspecified bound (the [] form). Finally, a chain
5832 of specified variable bounds is appended to the spec, starting with
5833 the most significant bound. For example, the PARM T a[2][m][3][n]
5834 will produce __attribute__((arg spec ("[$$2]", m, n)).
5835 For T a typedef for an array with variable bounds, the bounds are
5836 included in the specification in the expected order.
5837 No "arg spec" is created for parameters of pointer types, making
5838 a distinction between T(*)[N] (or, equivalently, T[][N]) and
5839 the T[M][N] form, all of which have the same type and are represented
5840 the same, but only the last of which gets an "arg spec" describing
5841 the most significant bound M. */
5844 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5846 /* The attribute specification string, minor bound first. */
5849 /* A list of VLA variable bounds, major first, or null if unspecified
5851 tree vbchain = NULL_TREE;
5852 /* True for a pointer parameter. */
5853 bool pointer = false;
5854 /* True for an ordinary array with an unpecified bound. */
5855 bool nobound = false;
5857 /* Create a string representation for the bounds of the array/VLA. */
5858 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
5860 next = pd->declarator;
5861 while (next && next->kind == cdk_attrs)
5862 next = next->declarator;
5864 /* Remember if a pointer has been seen to avoid storing the constant
5866 if (pd->kind == cdk_pointer)
5869 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
5870 && (!next || next->kind == cdk_id))
5872 /* Do nothing for the common case of a pointer. The fact that
5873 the parameter is one can be deduced from the absence of
5874 an arg spec for it. */
5878 if (pd->kind == cdk_id)
5881 || !parm->specs->type
5882 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
5883 || !TYPE_DOMAIN (parm->specs->type)
5884 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
5887 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
5889 && TREE_CODE (max) == INTEGER_CST)
5891 /* Extract the upper bound from a parameter of an array type
5892 unless the parameter is an ordinary array of unspecified
5893 bound in which case a next iteration of the loop will
5895 if (spec.empty () || spec.end ()[-1] != ' ')
5897 if (!tree_fits_shwi_p (max))
5900 /* The upper bound is the value of the largest valid
5902 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
5904 sprintf (buf, "%lu", (unsigned long)n);
5910 /* For a VLA typedef, create a list of its variable bounds and
5911 append it in the expected order to VBCHAIN. */
5912 tree tpbnds = NULL_TREE;
5913 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
5914 type = TREE_TYPE (type))
5916 tree nelts = array_type_nelts (type);
5917 if (error_operand_p (nelts))
5919 if (TREE_CODE (nelts) != INTEGER_CST)
5921 /* Each variable VLA bound is represented by the dollar
5924 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
5927 tpbnds = nreverse (tpbnds);
5928 vbchain = chainon (vbchain, tpbnds);
5932 if (pd->kind != cdk_array)
5935 if (pd->u.array.vla_unspec_p)
5937 /* Each unspecified bound is represented by a star. There
5938 can be any number of these in a declaration (but none in
5944 tree nelts = pd->u.array.dimen;
5947 /* Ordinary array of unspecified size. There can be at most
5948 one for the most significant bound. Exit on the next
5949 iteration which determines whether or not PARM is declared
5950 as a pointer or an array. */
5955 if (pd->u.array.static_p)
5958 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
5959 /* Avoid invalid NELTS. */
5963 nelts = c_fully_fold (nelts, false, nullptr);
5964 if (TREE_CODE (nelts) == INTEGER_CST)
5966 /* Skip all constant bounds except the most significant one.
5967 The interior ones are included in the array type. */
5968 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
5971 if (!tree_fits_uhwi_p (nelts))
5972 /* Bail completely on invalid bounds. */
5976 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
5977 sprintf (buf, "%llu", (unsigned long long)n);
5982 /* Each variable VLA bound is represented by a dollar sign. */
5984 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
5987 if (spec.empty () && !nobound)
5990 spec.insert (0, "[");
5992 /* Ordinary array of unspecified bound is represented by a space.
5993 It must be last in the spec. */
5997 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
5998 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
5999 tree name = get_identifier ("arg spec");
6000 return tree_cons (name, args, attrs);
6003 /* Given a parsed parameter declaration, decode it into a PARM_DECL
6004 and push that on the current scope. EXPR is a pointer to an
6005 expression that needs to be evaluated for the side effects of array
6006 size expressions in the parameters. */
6009 push_parm_decl (const struct c_parm *parm, tree *expr)
6011 tree attrs = parm->attrs;
6012 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
6013 &attrs, expr, NULL, DEPRECATED_NORMAL);
6014 if (decl && DECL_P (decl))
6015 DECL_SOURCE_LOCATION (decl) = parm->loc;
6017 attrs = get_parm_array_spec (parm, attrs);
6018 decl_attributes (&decl, attrs, 0);
6020 decl = pushdecl (decl);
6022 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
6025 /* Mark all the parameter declarations to date as forward decls.
6026 Also diagnose use of this extension. */
6029 mark_forward_parm_decls (void)
6031 struct c_binding *b;
6033 if (pedantic && !current_scope->warned_forward_parm_decls)
6035 pedwarn (input_location, OPT_Wpedantic,
6036 "ISO C forbids forward parameter declarations");
6037 current_scope->warned_forward_parm_decls = true;
6040 for (b = current_scope->bindings; b; b = b->prev)
6041 if (TREE_CODE (b->decl) == PARM_DECL)
6042 TREE_ASM_WRITTEN (b->decl) = 1;
6045 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
6046 literal, which may be an incomplete array type completed by the
6047 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6048 literal. NON_CONST is true if the initializers contain something
6049 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6050 it is the (valid) alignment for this compound literal, as specified
6051 with _Alignas. SCSPECS are the storage class specifiers (C2x) from the
6052 compound literal. */
6055 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6056 unsigned int alignas_align,
6057 struct c_declspecs *scspecs)
6059 /* We do not use start_decl here because we have a type, not a declarator;
6060 and do not use finish_decl because the decl should be stored inside
6061 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6065 bool threadp = scspecs ? scspecs->thread_p : false;
6066 enum c_storage_class storage_class = (scspecs
6067 ? scspecs->storage_class
6070 if (type == error_mark_node
6071 || init == error_mark_node)
6072 return error_mark_node;
6074 if (current_scope == file_scope && storage_class == csc_register)
6076 error_at (loc, "file-scope compound literal specifies %<register%>");
6077 storage_class = csc_none;
6080 if (current_scope != file_scope && threadp && storage_class == csc_none)
6082 error_at (loc, "compound literal implicitly auto and declared %qs",
6083 scspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6087 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6088 DECL_EXTERNAL (decl) = 0;
6089 TREE_PUBLIC (decl) = 0;
6090 TREE_STATIC (decl) = (current_scope == file_scope
6091 || storage_class == csc_static);
6092 DECL_CONTEXT (decl) = current_function_decl;
6093 TREE_USED (decl) = 1;
6094 DECL_READ_P (decl) = 1;
6095 DECL_ARTIFICIAL (decl) = 1;
6096 DECL_IGNORED_P (decl) = 1;
6097 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6098 TREE_TYPE (decl) = type;
6100 set_decl_tls_model (decl, decl_default_tls_model (decl));
6101 if (storage_class == csc_register)
6103 C_DECL_REGISTER (decl) = 1;
6104 DECL_REGISTER (decl) = 1;
6106 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6109 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6110 DECL_USER_ALIGN (decl) = 1;
6112 store_init_value (loc, decl, init, NULL_TREE);
6114 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6116 int failure = complete_array_type (&TREE_TYPE (decl),
6117 DECL_INITIAL (decl), true);
6118 /* If complete_array_type returns 3, it means that the
6119 initial value of the compound literal is empty. Allow it. */
6120 gcc_assert (failure == 0 || failure == 3);
6122 type = TREE_TYPE (decl);
6123 TREE_TYPE (DECL_INITIAL (decl)) = type;
6126 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6128 c_incomplete_type_error (loc, NULL_TREE, type);
6129 return error_mark_node;
6132 if (TREE_STATIC (decl)
6133 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6134 return error_mark_node;
6136 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6137 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6138 TREE_SIDE_EFFECTS (complit) = 1;
6140 layout_decl (decl, 0);
6142 if (TREE_STATIC (decl))
6144 /* This decl needs a name for the assembler output. */
6145 set_compound_literal_name (decl);
6146 DECL_DEFER_OUTPUT (decl) = 1;
6147 DECL_COMDAT (decl) = 1;
6149 rest_of_decl_compilation (decl, 1, 0);
6151 else if (current_function_decl && !current_scope->parm_flag)
6156 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6157 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6163 /* Check the type of a compound literal. Here we just check that it
6164 is valid for C++. */
6167 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6170 && (type_name->specs->typespec_kind == ctsk_tagdef
6171 || type_name->specs->typespec_kind == ctsk_tagfirstref
6172 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6173 warning_at (loc, OPT_Wc___compat,
6174 "defining a type in a compound literal is invalid in C++");
6177 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6178 replacing with appropriate values if they are invalid. */
6181 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6185 unsigned int max_width;
6186 unsigned HOST_WIDE_INT w;
6187 const char *name = (orig_name
6188 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6189 : _("<anonymous>"));
6191 /* Detect and ignore out of range field width and process valid
6193 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6195 error_at (loc, "bit-field %qs width not an integer constant", name);
6196 *width = integer_one_node;
6200 if (TREE_CODE (*width) != INTEGER_CST)
6202 *width = c_fully_fold (*width, false, NULL);
6203 if (TREE_CODE (*width) == INTEGER_CST)
6204 pedwarn (loc, OPT_Wpedantic,
6205 "bit-field %qs width not an integer constant expression",
6208 if (TREE_CODE (*width) != INTEGER_CST)
6210 error_at (loc, "bit-field %qs width not an integer constant", name);
6211 *width = integer_one_node;
6213 constant_expression_warning (*width);
6214 if (tree_int_cst_sgn (*width) < 0)
6216 error_at (loc, "negative width in bit-field %qs", name);
6217 *width = integer_one_node;
6219 else if (integer_zerop (*width) && orig_name)
6221 error_at (loc, "zero width for bit-field %qs", name);
6222 *width = integer_one_node;
6226 /* Detect invalid bit-field type. */
6227 if (TREE_CODE (*type) != INTEGER_TYPE
6228 && TREE_CODE (*type) != BOOLEAN_TYPE
6229 && TREE_CODE (*type) != ENUMERAL_TYPE)
6231 error_at (loc, "bit-field %qs has invalid type", name);
6232 *type = unsigned_type_node;
6235 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6237 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6239 *type = unsigned_type_node;
6242 type_mv = TYPE_MAIN_VARIANT (*type);
6243 if (!in_system_header_at (input_location)
6244 && type_mv != integer_type_node
6245 && type_mv != unsigned_type_node
6246 && type_mv != boolean_type_node)
6247 pedwarn_c90 (loc, OPT_Wpedantic,
6248 "type of bit-field %qs is a GCC extension", name);
6250 max_width = TYPE_PRECISION (*type);
6252 if (compare_tree_int (*width, max_width) > 0)
6254 error_at (loc, "width of %qs exceeds its type", name);
6256 *width = build_int_cst (integer_type_node, w);
6259 w = tree_to_uhwi (*width);
6261 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6263 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6265 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6266 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6267 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6273 /* Print warning about variable length array if necessary. */
6276 warn_variable_length_array (tree name, tree size)
6278 if (TREE_CONSTANT (size))
6281 pedwarn_c90 (input_location, OPT_Wvla,
6282 "ISO C90 forbids array %qE whose size "
6283 "cannot be evaluated", name);
6285 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6286 "whose size cannot be evaluated");
6291 pedwarn_c90 (input_location, OPT_Wvla,
6292 "ISO C90 forbids variable length array %qE", name);
6294 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6299 /* Print warning about defaulting to int if necessary. */
6302 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6304 diagnostic_info diagnostic;
6306 rich_location richloc (line_table, location);
6308 va_start (ap, gmsgid);
6309 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6310 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6311 diagnostic.option_index = opt;
6312 diagnostic_report_diagnostic (global_dc, &diagnostic);
6316 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6317 considering only those c_declspec_words found in LIST, which
6318 must be terminated by cdw_number_of_elements. */
6321 smallest_type_quals_location (const location_t *locations,
6322 const c_declspec_word *list)
6324 location_t loc = UNKNOWN_LOCATION;
6325 while (*list != cdw_number_of_elements)
6327 location_t newloc = locations[*list];
6328 if (loc == UNKNOWN_LOCATION
6329 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6337 /* Given declspecs and a declarator,
6338 determine the name and type of the object declared
6339 and construct a ..._DECL node for it.
6340 (In one case we can return a ..._TYPE node instead.
6341 For invalid input we sometimes return NULL_TREE.)
6343 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6345 DECL_CONTEXT says which syntactic context this declaration is in:
6346 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6347 FUNCDEF for a function definition. Like NORMAL but a few different
6348 error messages in each case. Return value may be zero meaning
6349 this definition is too screwy to try to parse.
6350 PARM for a parameter declaration (either within a function prototype
6351 or before a function body). Make a PARM_DECL, or return void_type_node.
6352 TYPENAME if for a typename (in a cast or sizeof).
6353 Don't make a DECL node; just return the ..._TYPE node.
6354 FIELD for a struct or union field; make a FIELD_DECL.
6355 INITIALIZED is true if the decl has an initializer.
6356 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6357 representing the width of the bit-field.
6358 DECL_ATTRS points to the list of attributes that should be added to this
6359 decl. Any nested attributes that belong on the decl itself will be
6361 If EXPR is not NULL, any expressions that need to be evaluated as
6362 part of evaluating variably modified types will be stored in *EXPR.
6363 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6364 set to indicate whether operands in *EXPR can be used in constant
6366 DEPRECATED_STATE is a deprecated_states value indicating whether
6367 deprecation/unavailability warnings should be suppressed.
6369 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6370 It may also be so in the PARM case, for a prototype where the
6371 argument type is specified but not the name.
6373 This function is where the complicated C meanings of `static'
6374 and `extern' are interpreted. */
6377 grokdeclarator (const struct c_declarator *declarator,
6378 struct c_declspecs *declspecs,
6379 enum decl_context decl_context, bool initialized, tree *width,
6380 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6381 enum deprecated_states deprecated_state)
6383 tree type = declspecs->type;
6384 bool threadp = declspecs->thread_p;
6385 enum c_storage_class storage_class = declspecs->storage_class;
6390 int type_quals = TYPE_UNQUALIFIED;
6391 tree name = NULL_TREE;
6392 bool funcdef_flag = false;
6393 bool funcdef_syntax = false;
6394 bool size_varies = false;
6395 tree decl_attr = declspecs->decl_attr;
6396 int array_ptr_quals = TYPE_UNQUALIFIED;
6397 tree array_ptr_attrs = NULL_TREE;
6398 bool array_parm_static = false;
6399 bool array_parm_vla_unspec_p = false;
6400 tree returned_attrs = NULL_TREE;
6401 tree decl_id_attrs = NULL_TREE;
6402 bool bitfield = width != NULL;
6404 tree orig_qual_type = NULL;
6405 size_t orig_qual_indirect = 0;
6406 struct c_arg_info *arg_info = 0;
6407 addr_space_t as1, as2, address_space;
6408 location_t loc = UNKNOWN_LOCATION;
6410 bool expr_const_operands_dummy;
6411 enum c_declarator_kind first_non_attr_kind;
6412 unsigned int alignas_align = 0;
6414 if (TREE_CODE (type) == ERROR_MARK)
6415 return error_mark_node;
6419 expr_dummy = NULL_TREE;
6421 if (expr_const_operands == NULL)
6422 expr_const_operands = &expr_const_operands_dummy;
6424 if (declspecs->expr)
6427 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6430 *expr = declspecs->expr;
6432 *expr_const_operands = declspecs->expr_const_operands;
6434 if (decl_context == FUNCDEF)
6435 funcdef_flag = true, decl_context = NORMAL;
6437 /* Look inside a declarator for the name being declared
6438 and get it as an IDENTIFIER_NODE, for an error message. */
6440 const struct c_declarator *decl = declarator;
6442 first_non_attr_kind = cdk_attrs;
6452 funcdef_syntax = (decl->kind == cdk_function);
6453 if (first_non_attr_kind == cdk_attrs)
6454 first_non_attr_kind = decl->kind;
6455 decl = decl->declarator;
6459 decl = decl->declarator;
6465 name = decl->u.id.id;
6466 decl_id_attrs = decl->u.id.attrs;
6467 if (first_non_attr_kind == cdk_attrs)
6468 first_non_attr_kind = decl->kind;
6475 if (name == NULL_TREE)
6477 gcc_assert (decl_context == PARM
6478 || decl_context == TYPENAME
6479 || (decl_context == FIELD
6480 && declarator->kind == cdk_id));
6481 gcc_assert (!initialized);
6485 /* A function definition's declarator must have the form of
6486 a function declarator. */
6488 if (funcdef_flag && !funcdef_syntax)
6491 /* If this looks like a function definition, make it one,
6492 even if it occurs where parms are expected.
6493 Then store_parm_decls will reject it and not use it as a parm. */
6494 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6495 decl_context = PARM;
6497 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6499 if (declspecs->unavailable_p)
6500 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6501 else if (declspecs->deprecated_p
6502 && deprecated_state != DEPRECATED_SUPPRESS)
6503 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6506 if ((decl_context == NORMAL || decl_context == FIELD)
6507 && current_scope == file_scope
6508 && variably_modified_type_p (type, NULL_TREE))
6511 error_at (loc, "variably modified %qE at file scope", name);
6513 error_at (loc, "variably modified field at file scope");
6514 type = integer_type_node;
6517 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6519 /* Diagnose defaulting to "int". */
6521 if (declspecs->default_int_p && !in_system_header_at (input_location))
6523 /* Issue a warning if this is an ISO C 99 program or if
6524 -Wreturn-type and this is a function, or if -Wimplicit;
6525 prefer the former warning since it is more explicit. */
6526 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6528 warn_about_return_type = 1;
6532 warn_defaults_to (loc, OPT_Wimplicit_int,
6533 "type defaults to %<int%> in declaration "
6536 warn_defaults_to (loc, OPT_Wimplicit_int,
6537 "type defaults to %<int%> in type name");
6541 /* Adjust the type if a bit-field is being declared,
6542 -funsigned-bitfields applied and the type is not explicitly
6544 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6545 && TREE_CODE (type) == INTEGER_TYPE)
6546 type = unsigned_type_for (type);
6548 /* Figure out the type qualifiers for the declaration. There are
6549 two ways a declaration can become qualified. One is something
6550 like `const int i' where the `const' is explicit. Another is
6551 something like `typedef const int CI; CI i' where the type of the
6552 declaration contains the `const'. A third possibility is that
6553 there is a type qualifier on the element type of a typedefed
6554 array type, in which case we should extract that qualifier so
6555 that c_apply_type_quals_to_decl receives the full list of
6556 qualifiers to work with (C90 is not entirely clear about whether
6557 duplicate qualifiers should be diagnosed in this case, but it
6558 seems most appropriate to do so). */
6559 element_type = strip_array_types (type);
6560 constp = declspecs->const_p + TYPE_READONLY (element_type);
6561 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6562 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6563 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6564 as1 = declspecs->address_space;
6565 as2 = TYPE_ADDR_SPACE (element_type);
6566 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6569 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6571 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6573 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6575 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6577 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6578 error_at (loc, "conflicting named address spaces (%s vs %s)",
6579 c_addr_space_name (as1), c_addr_space_name (as2));
6581 if ((TREE_CODE (type) == ARRAY_TYPE
6582 || first_non_attr_kind == cdk_array)
6583 && TYPE_QUALS (element_type))
6585 orig_qual_type = type;
6586 type = TYPE_MAIN_VARIANT (type);
6588 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6589 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6590 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6591 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6592 | ENCODE_QUAL_ADDR_SPACE (address_space));
6593 if (type_quals != TYPE_QUALS (element_type))
6594 orig_qual_type = NULL_TREE;
6596 /* Applying the _Atomic qualifier to an array type (through the use
6597 of typedefs or typeof) must be detected here. If the qualifier
6598 is introduced later, any appearance of applying it to an array is
6599 actually applying it to an element of that array. */
6600 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6601 error_at (loc, "%<_Atomic%>-qualified array type");
6603 /* Warn about storage classes that are invalid for certain
6604 kinds of declarations (parameters, typenames, etc.). */
6608 || storage_class == csc_auto
6609 || storage_class == csc_register
6610 || storage_class == csc_typedef))
6612 if (storage_class == csc_auto)
6614 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6615 "function definition declared %<auto%>");
6616 if (storage_class == csc_register)
6617 error_at (loc, "function definition declared %<register%>");
6618 if (storage_class == csc_typedef)
6619 error_at (loc, "function definition declared %<typedef%>");
6621 error_at (loc, "function definition declared %qs",
6622 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6624 if (storage_class == csc_auto
6625 || storage_class == csc_register
6626 || storage_class == csc_typedef)
6627 storage_class = csc_none;
6629 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6631 if (decl_context == PARM && storage_class == csc_register)
6635 switch (decl_context)
6639 error_at (loc, "storage class specified for structure "
6642 error_at (loc, "storage class specified for structure field");
6646 error_at (loc, "storage class specified for parameter %qE",
6649 error_at (loc, "storage class specified for unnamed parameter");
6652 error_at (loc, "storage class specified for typename");
6655 storage_class = csc_none;
6659 else if (storage_class == csc_extern
6663 /* 'extern' with initialization is invalid if not at file scope. */
6664 if (current_scope == file_scope)
6666 /* It is fine to have 'extern const' when compiling at C
6667 and C++ intersection. */
6668 if (!(warn_cxx_compat && constp))
6669 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6673 error_at (loc, "%qE has both %<extern%> and initializer", name);
6675 else if (current_scope == file_scope)
6677 if (storage_class == csc_auto)
6678 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6680 if (pedantic && storage_class == csc_register)
6681 pedwarn (input_location, OPT_Wpedantic,
6682 "file-scope declaration of %qE specifies %<register%>", name);
6686 if (storage_class == csc_extern && funcdef_flag)
6687 error_at (loc, "nested function %qE declared %<extern%>", name);
6688 else if (threadp && storage_class == csc_none)
6690 error_at (loc, "function-scope %qE implicitly auto and declared "
6692 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6697 /* Now figure out the structure of the declarator proper.
6698 Descend through it, creating more complex types, until we reach
6699 the declared identifier (or NULL_TREE, in an absolute declarator).
6700 At each stage we maintain an unqualified version of the type
6701 together with any qualifiers that should be applied to it with
6702 c_build_qualified_type; this way, array types including
6703 multidimensional array types are first built up in unqualified
6704 form and then the qualified form is created with
6705 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6707 while (declarator && declarator->kind != cdk_id)
6709 if (type == error_mark_node)
6711 declarator = declarator->declarator;
6715 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6716 a cdk_pointer (for *...),
6717 a cdk_function (for ...(...)),
6718 a cdk_attrs (for nested attributes),
6719 or a cdk_id (for the name being declared
6720 or the place in an absolute declarator
6721 where the name was omitted).
6722 For the last case, we have just exited the loop.
6724 At this point, TYPE is the type of elements of an array,
6725 or for a function to return, or for a pointer to point to.
6726 After this sequence of ifs, TYPE is the type of the
6727 array or function or pointer, and DECLARATOR has had its
6728 outermost layer removed. */
6730 if (array_ptr_quals != TYPE_UNQUALIFIED
6731 || array_ptr_attrs != NULL_TREE
6732 || array_parm_static)
6734 /* Only the innermost declarator (making a parameter be of
6735 array type which is converted to pointer type)
6736 may have static or type qualifiers. */
6737 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6738 array_ptr_quals = TYPE_UNQUALIFIED;
6739 array_ptr_attrs = NULL_TREE;
6740 array_parm_static = false;
6743 switch (declarator->kind)
6747 /* A declarator with embedded attributes. */
6748 tree attrs = declarator->u.attrs;
6749 const struct c_declarator *inner_decl;
6751 declarator = declarator->declarator;
6752 /* Standard attribute syntax precisely defines what entity
6753 an attribute in each position appertains to, so only
6754 apply laxity about positioning to GNU attribute syntax.
6755 Standard attributes applied to a function or array
6756 declarator apply exactly to that type; standard
6757 attributes applied to the identifier apply to the
6758 declaration rather than to the type, and are specified
6759 using a cdk_id declarator rather than using
6761 inner_decl = declarator;
6762 while (inner_decl->kind == cdk_attrs)
6763 inner_decl = inner_decl->declarator;
6764 if (!cxx11_attribute_p (attrs))
6766 if (inner_decl->kind == cdk_id)
6767 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6768 else if (inner_decl->kind == cdk_function)
6769 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6770 else if (inner_decl->kind == cdk_array)
6771 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6773 attrs = c_warn_type_attributes (attrs);
6774 returned_attrs = decl_attributes (&type,
6775 chainon (returned_attrs, attrs),
6781 tree itype = NULL_TREE;
6782 tree size = declarator->u.array.dimen;
6783 /* The index is a signed object `sizetype' bits wide. */
6784 tree index_type = c_common_signed_type (sizetype);
6786 array_ptr_quals = declarator->u.array.quals;
6787 array_ptr_attrs = declarator->u.array.attrs;
6788 array_parm_static = declarator->u.array.static_p;
6789 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6791 declarator = declarator->declarator;
6793 /* Check for some types that there cannot be arrays of. */
6795 if (VOID_TYPE_P (type))
6798 error_at (loc, "declaration of %qE as array of voids", name);
6800 error_at (loc, "declaration of type name as array of voids");
6801 type = error_mark_node;
6804 if (TREE_CODE (type) == FUNCTION_TYPE)
6807 error_at (loc, "declaration of %qE as array of functions",
6810 error_at (loc, "declaration of type name as array of "
6812 type = error_mark_node;
6815 if (pedantic && !in_system_header_at (input_location)
6816 && flexible_array_type_p (type))
6817 pedwarn (loc, OPT_Wpedantic,
6818 "invalid use of structure with flexible array member");
6820 if (size == error_mark_node)
6821 type = error_mark_node;
6823 if (type == error_mark_node)
6826 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6828 type = error_mark_node;
6832 /* If size was specified, set ITYPE to a range-type for
6833 that size. Otherwise, ITYPE remains null. finish_decl
6834 may figure it out from an initial value. */
6838 bool size_maybe_const = true;
6839 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6840 && !TREE_OVERFLOW (size));
6841 bool this_size_varies = false;
6843 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6845 STRIP_TYPE_NOPS (size);
6847 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6850 error_at (loc, "size of array %qE has non-integer type",
6854 "size of unnamed array has non-integer type");
6855 size = integer_one_node;
6856 size_int_const = true;
6858 /* This can happen with enum forward declaration. */
6859 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6862 error_at (loc, "size of array %qE has incomplete type",
6865 error_at (loc, "size of unnamed array has incomplete "
6867 size = integer_one_node;
6868 size_int_const = true;
6871 size = c_fully_fold (size, false, &size_maybe_const);
6873 if (pedantic && size_maybe_const && integer_zerop (size))
6876 pedwarn (loc, OPT_Wpedantic,
6877 "ISO C forbids zero-size array %qE", name);
6879 pedwarn (loc, OPT_Wpedantic,
6880 "ISO C forbids zero-size array");
6883 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6885 constant_expression_warning (size);
6886 if (tree_int_cst_sgn (size) < 0)
6889 error_at (loc, "size of array %qE is negative", name);
6891 error_at (loc, "size of unnamed array is negative");
6892 size = integer_one_node;
6893 size_int_const = true;
6895 /* Handle a size folded to an integer constant but
6896 not an integer constant expression. */
6897 if (!size_int_const)
6899 /* If this is a file scope declaration of an
6900 ordinary identifier, this is invalid code;
6901 diagnosing it here and not subsequently
6902 treating the type as variable-length avoids
6903 more confusing diagnostics later. */
6904 if ((decl_context == NORMAL || decl_context == FIELD)
6905 && current_scope == file_scope)
6906 pedwarn (input_location, 0,
6907 "variably modified %qE at file scope",
6910 this_size_varies = size_varies = true;
6911 warn_variable_length_array (name, size);
6914 else if ((decl_context == NORMAL || decl_context == FIELD)
6915 && current_scope == file_scope)
6917 error_at (loc, "variably modified %qE at file scope", name);
6918 size = integer_one_node;
6922 /* Make sure the array size remains visibly
6923 nonconstant even if it is (eg) a const variable
6924 with known value. */
6925 this_size_varies = size_varies = true;
6926 warn_variable_length_array (name, size);
6927 if (sanitize_flags_p (SANITIZE_VLA)
6928 && current_function_decl != NULL_TREE
6929 && decl_context == NORMAL)
6931 /* Evaluate the array size only once. */
6932 size = save_expr (size);
6933 size = c_fully_fold (size, false, NULL);
6934 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6935 ubsan_instrument_vla (loc, size),
6940 if (integer_zerop (size) && !this_size_varies)
6942 /* A zero-length array cannot be represented with
6943 an unsigned index type, which is what we'll
6944 get with build_index_type. Create an
6945 open-ended range instead. */
6946 itype = build_range_type (sizetype, size, NULL_TREE);
6950 /* Arrange for the SAVE_EXPR on the inside of the
6951 MINUS_EXPR, which allows the -1 to get folded
6952 with the +1 that happens when building TYPE_SIZE. */
6954 size = save_expr (size);
6955 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6956 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6957 integer_zero_node, size);
6959 /* Compute the maximum valid index, that is, size
6960 - 1. Do the calculation in index_type, so that
6961 if it is a variable the computations will be
6962 done in the proper mode. */
6963 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6964 convert (index_type, size),
6965 convert (index_type,
6968 /* The above overflows when size does not fit
6970 ??? While a size of INT_MAX+1 technically shouldn't
6971 cause an overflow (because we subtract 1), handling
6972 this case seems like an unnecessary complication. */
6973 if (TREE_CODE (size) == INTEGER_CST
6974 && !int_fits_type_p (size, index_type))
6977 error_at (loc, "size of array %qE is too large",
6980 error_at (loc, "size of unnamed array is too large");
6981 type = error_mark_node;
6985 itype = build_index_type (itype);
6987 if (this_size_varies)
6989 if (TREE_SIDE_EFFECTS (size))
6992 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6997 *expr_const_operands &= size_maybe_const;
7000 else if (decl_context == FIELD)
7002 bool flexible_array_member = false;
7003 if (array_parm_vla_unspec_p)
7004 /* Field names can in fact have function prototype
7005 scope so [*] is disallowed here through making
7006 the field variably modified, not through being
7007 something other than a declaration with function
7012 const struct c_declarator *t = declarator;
7013 while (t->kind == cdk_attrs)
7015 flexible_array_member = (t->kind == cdk_id);
7017 if (flexible_array_member
7018 && !in_system_header_at (input_location))
7019 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7020 "support flexible array members");
7022 /* ISO C99 Flexible array members are effectively
7023 identical to GCC's zero-length array extension. */
7024 if (flexible_array_member || array_parm_vla_unspec_p)
7025 itype = build_range_type (sizetype, size_zero_node,
7028 else if (decl_context == PARM)
7030 if (array_parm_vla_unspec_p)
7032 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
7036 else if (decl_context == TYPENAME)
7038 if (array_parm_vla_unspec_p)
7041 warning (0, "%<[*]%> not in a declaration");
7042 /* We use this to avoid messing up with incomplete
7043 array types of the same type, that would
7044 otherwise be modified below. */
7045 itype = build_range_type (sizetype, size_zero_node,
7051 /* Complain about arrays of incomplete types. */
7052 if (!COMPLETE_TYPE_P (type))
7054 auto_diagnostic_group d;
7055 error_at (loc, "array type has incomplete element type %qT",
7057 /* See if we can be more helpful. */
7058 if (TREE_CODE (type) == ARRAY_TYPE)
7061 inform (loc, "declaration of %qE as multidimensional "
7062 "array must have bounds for all dimensions "
7063 "except the first", name);
7065 inform (loc, "declaration of multidimensional array "
7066 "must have bounds for all dimensions except "
7069 type = error_mark_node;
7072 /* When itype is NULL, a shared incomplete array type is
7073 returned for all array of a given type. Elsewhere we
7074 make sure we don't complete that type before copying
7075 it, but here we want to make sure we don't ever
7076 modify the shared type, so we gcc_assert (itype)
7079 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7080 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7081 type = build_qualified_type (type,
7082 ENCODE_QUAL_ADDR_SPACE (as));
7084 type = build_array_type (type, itype);
7087 if (type != error_mark_node)
7091 /* It is ok to modify type here even if itype is
7092 NULL: if size_varies, we're in a
7093 multi-dimensional array and the inner type has
7094 variable size, so the enclosing shared array type
7096 if (size && TREE_CODE (size) == INTEGER_CST)
7098 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7099 C_TYPE_VARIABLE_SIZE (type) = 1;
7102 /* The GCC extension for zero-length arrays differs from
7103 ISO flexible array members in that sizeof yields
7105 if (size && integer_zerop (size))
7108 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7109 TYPE_SIZE (type) = bitsize_zero_node;
7110 TYPE_SIZE_UNIT (type) = size_zero_node;
7111 SET_TYPE_STRUCTURAL_EQUALITY (type);
7113 if (array_parm_vla_unspec_p)
7116 /* The type is complete. C99 6.7.5.2p4 */
7117 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7118 TYPE_SIZE (type) = bitsize_zero_node;
7119 TYPE_SIZE_UNIT (type) = size_zero_node;
7120 SET_TYPE_STRUCTURAL_EQUALITY (type);
7123 if (!valid_array_size_p (loc, type, name))
7124 type = error_mark_node;
7127 if (decl_context != PARM
7128 && (array_ptr_quals != TYPE_UNQUALIFIED
7129 || array_ptr_attrs != NULL_TREE
7130 || array_parm_static))
7132 error_at (loc, "static or type qualifiers in non-parameter "
7133 "array declarator");
7134 array_ptr_quals = TYPE_UNQUALIFIED;
7135 array_ptr_attrs = NULL_TREE;
7136 array_parm_static = false;
7138 orig_qual_indirect++;
7143 /* Say it's a definition only for the declarator closest
7144 to the identifier, apart possibly from some
7146 bool really_funcdef = false;
7148 orig_qual_type = NULL_TREE;
7151 const struct c_declarator *t = declarator->declarator;
7152 while (t->kind == cdk_attrs)
7154 really_funcdef = (t->kind == cdk_id);
7157 /* Declaring a function type. Make sure we have a valid
7158 type for the function to return. */
7159 if (type == error_mark_node)
7162 size_varies = false;
7164 /* Warn about some types functions can't return. */
7165 if (TREE_CODE (type) == FUNCTION_TYPE)
7168 error_at (loc, "%qE declared as function returning a "
7171 error_at (loc, "type name declared as function "
7172 "returning a function");
7173 type = integer_type_node;
7175 if (TREE_CODE (type) == ARRAY_TYPE)
7178 error_at (loc, "%qE declared as function returning an array",
7181 error_at (loc, "type name declared as function returning "
7183 type = integer_type_node;
7186 /* Construct the function type and go to the next
7187 inner layer of declarator. */
7188 arg_info = declarator->u.arg_info;
7189 arg_types = grokparms (arg_info, really_funcdef);
7191 /* Type qualifiers before the return type of the function
7192 qualify the return type, not the function type. */
7195 const enum c_declspec_word ignored_quals_list[] =
7197 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7198 cdw_atomic, cdw_number_of_elements
7200 location_t specs_loc
7201 = smallest_type_quals_location (declspecs->locations,
7202 ignored_quals_list);
7203 if (specs_loc == UNKNOWN_LOCATION)
7204 specs_loc = declspecs->locations[cdw_typedef];
7205 if (specs_loc == UNKNOWN_LOCATION)
7208 /* Type qualifiers on a function return type are
7209 normally permitted by the standard but have no
7210 effect, so give a warning at -Wreturn-type.
7211 Qualifiers on a void return type are banned on
7212 function definitions in ISO C; GCC used to used
7213 them for noreturn functions. The resolution of C11
7214 DR#423 means qualifiers (other than _Atomic) are
7215 actually removed from the return type when
7216 determining the function type. */
7217 int quals_used = type_quals;
7219 quals_used &= TYPE_QUAL_ATOMIC;
7220 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7221 pedwarn (specs_loc, 0,
7222 "function definition has qualified void "
7225 warning_at (specs_loc, OPT_Wignored_qualifiers,
7226 "type qualifiers ignored on function "
7229 /* Ensure an error for restrict on invalid types; the
7230 DR#423 resolution is not entirely clear about
7233 && (type_quals & TYPE_QUAL_RESTRICT)
7234 && (!POINTER_TYPE_P (type)
7235 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7236 error_at (loc, "invalid use of %<restrict%>");
7237 type = c_build_qualified_type (type, quals_used);
7239 type_quals = TYPE_UNQUALIFIED;
7241 type = build_function_type (type, arg_types);
7242 declarator = declarator->declarator;
7244 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7245 the formal parameter list of this FUNCTION_TYPE to point to
7246 the FUNCTION_TYPE node itself. */
7251 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7252 TYPE_CONTEXT (tag->type) = type;
7258 /* Merge any constancy or volatility into the target type
7260 if ((type_quals & TYPE_QUAL_ATOMIC)
7261 && TREE_CODE (type) == FUNCTION_TYPE)
7264 "%<_Atomic%>-qualified function type");
7265 type_quals &= ~TYPE_QUAL_ATOMIC;
7267 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7269 pedwarn (loc, OPT_Wpedantic,
7270 "ISO C forbids qualified function types");
7272 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7273 orig_qual_indirect);
7274 orig_qual_type = NULL_TREE;
7275 size_varies = false;
7277 /* When the pointed-to type involves components of variable size,
7278 care must be taken to ensure that the size evaluation code is
7279 emitted early enough to dominate all the possible later uses
7280 and late enough for the variables on which it depends to have
7283 This is expected to happen automatically when the pointed-to
7284 type has a name/declaration of it's own, but special attention
7285 is required if the type is anonymous.
7287 We attach an artificial TYPE_DECL to such pointed-to type
7288 and arrange for it to be included in a DECL_EXPR. This
7289 forces the sizes evaluation at a safe point and ensures it
7290 is not deferred until e.g. within a deeper conditional context.
7292 PARM contexts have no enclosing statement list that
7293 can hold the DECL_EXPR, so we need to use a BIND_EXPR
7294 instead, and add it to the list of expressions that
7295 need to be evaluated.
7297 TYPENAME contexts do have an enclosing statement list,
7298 but it would be incorrect to use it, as the size should
7299 only be evaluated if the containing expression is
7300 evaluated. We might also be in the middle of an
7301 expression with side effects on the pointed-to type size
7302 "arguments" prior to the pointer declaration point and
7303 the fake TYPE_DECL in the enclosing context would force
7304 the size evaluation prior to the side effects. We therefore
7305 use BIND_EXPRs in TYPENAME contexts too. */
7306 if (!TYPE_NAME (type)
7307 && variably_modified_type_p (type, NULL_TREE))
7309 tree bind = NULL_TREE;
7310 if (decl_context == TYPENAME || decl_context == PARM)
7312 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7313 NULL_TREE, NULL_TREE);
7314 TREE_SIDE_EFFECTS (bind) = 1;
7315 BIND_EXPR_BODY (bind) = push_stmt_list ();
7318 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7319 DECL_ARTIFICIAL (decl) = 1;
7321 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7322 TYPE_NAME (type) = decl;
7326 BIND_EXPR_BODY (bind)
7327 = pop_stmt_list (BIND_EXPR_BODY (bind));
7329 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7336 type = c_build_pointer_type (type);
7338 /* Process type qualifiers (such as const or volatile)
7339 that were given inside the `*'. */
7340 type_quals = declarator->u.pointer_quals;
7342 declarator = declarator->declarator;
7349 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7350 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7352 /* Now TYPE has the actual type, apart from any qualifiers in
7355 /* Warn about address space used for things other than static memory or
7357 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7358 if (!ADDR_SPACE_GENERIC_P (address_space))
7360 if (decl_context == NORMAL)
7362 switch (storage_class)
7365 error ("%qs combined with %<auto%> qualifier for %qE",
7366 c_addr_space_name (address_space), name);
7369 error ("%qs combined with %<register%> qualifier for %qE",
7370 c_addr_space_name (address_space), name);
7373 if (current_function_scope)
7375 error ("%qs specified for auto variable %qE",
7376 c_addr_space_name (address_space), name);
7388 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7391 error ("%qs specified for parameter %qE",
7392 c_addr_space_name (address_space), name);
7394 error ("%qs specified for unnamed parameter",
7395 c_addr_space_name (address_space));
7397 else if (decl_context == FIELD)
7400 error ("%qs specified for structure field %qE",
7401 c_addr_space_name (address_space), name);
7403 error ("%qs specified for structure field",
7404 c_addr_space_name (address_space));
7408 /* Check the type and width of a bit-field. */
7411 check_bitfield_type_and_width (loc, &type, width, name);
7412 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7413 atomic types are permitted for bit-fields; we have no code to
7414 make bit-field accesses atomic, so disallow them. */
7415 if (type_quals & TYPE_QUAL_ATOMIC)
7418 error_at (loc, "bit-field %qE has atomic type", name);
7420 error_at (loc, "bit-field has atomic type");
7421 type_quals &= ~TYPE_QUAL_ATOMIC;
7425 /* Reject invalid uses of _Alignas. */
7426 if (declspecs->alignas_p)
7428 if (storage_class == csc_typedef)
7429 error_at (loc, "alignment specified for typedef %qE", name);
7430 else if (storage_class == csc_register)
7431 error_at (loc, "alignment specified for %<register%> object %qE",
7433 else if (decl_context == PARM)
7436 error_at (loc, "alignment specified for parameter %qE", name);
7438 error_at (loc, "alignment specified for unnamed parameter");
7443 error_at (loc, "alignment specified for bit-field %qE", name);
7445 error_at (loc, "alignment specified for unnamed bit-field");
7447 else if (TREE_CODE (type) == FUNCTION_TYPE)
7448 error_at (loc, "alignment specified for function %qE", name);
7449 else if (declspecs->align_log != -1 && TYPE_P (type))
7451 alignas_align = 1U << declspecs->align_log;
7452 if (alignas_align < min_align_of_type (type))
7455 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7456 "alignment of %qE", name);
7458 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7459 "alignment of unnamed field");
7465 /* If this is declaring a typedef name, return a TYPE_DECL. */
7467 if (storage_class == csc_typedef)
7470 if ((type_quals & TYPE_QUAL_ATOMIC)
7471 && TREE_CODE (type) == FUNCTION_TYPE)
7474 "%<_Atomic%>-qualified function type");
7475 type_quals &= ~TYPE_QUAL_ATOMIC;
7477 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7479 pedwarn (loc, OPT_Wpedantic,
7480 "ISO C forbids qualified function types");
7482 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7483 orig_qual_indirect);
7484 decl = build_decl (declarator->id_loc,
7485 TYPE_DECL, declarator->u.id.id, type);
7486 if (declspecs->explicit_signed_p)
7487 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7488 if (declspecs->inline_p)
7489 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7490 if (declspecs->noreturn_p)
7491 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7493 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7495 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7498 && b->decl != NULL_TREE
7499 && (B_IN_CURRENT_SCOPE (b)
7500 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7501 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7503 auto_diagnostic_group d;
7504 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7505 ("using %qD as both a typedef and a tag is "
7506 "invalid in C++"), decl)
7507 && b->locus != UNKNOWN_LOCATION)
7508 inform (b->locus, "originally defined here");
7515 /* If this is a type name (such as, in a cast or sizeof),
7516 compute the type and return it now. */
7518 if (decl_context == TYPENAME)
7520 /* Note that the grammar rejects storage classes in typenames
7522 gcc_assert (storage_class == csc_none && !threadp
7523 && !declspecs->inline_p && !declspecs->noreturn_p);
7524 if ((type_quals & TYPE_QUAL_ATOMIC)
7525 && TREE_CODE (type) == FUNCTION_TYPE)
7528 "%<_Atomic%>-qualified function type");
7529 type_quals &= ~TYPE_QUAL_ATOMIC;
7531 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7533 pedwarn (loc, OPT_Wpedantic,
7534 "ISO C forbids const or volatile function types");
7536 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7537 orig_qual_indirect);
7541 if (pedantic && decl_context == FIELD
7542 && variably_modified_type_p (type, NULL_TREE))
7545 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7546 "have a variably modified type");
7549 /* Aside from typedefs and type names (handle above),
7550 `void' at top level (not within pointer)
7551 is allowed only in public variables.
7552 We don't complain about parms either, but that is because
7553 a better error message can be made later. */
7555 if (VOID_TYPE_P (type) && decl_context != PARM
7556 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7557 && (storage_class == csc_extern
7558 || (current_scope == file_scope
7559 && !(storage_class == csc_static
7560 || storage_class == csc_register)))))
7562 error_at (loc, "variable or field %qE declared void", name);
7563 type = integer_type_node;
7566 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7567 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7572 if (decl_context == PARM)
7575 bool array_parameter_p = false;
7577 /* A parameter declared as an array of T is really a pointer to T.
7578 One declared as a function is really a pointer to a function. */
7580 if (TREE_CODE (type) == ARRAY_TYPE)
7582 /* Transfer const-ness of array into that of type pointed to. */
7583 type = TREE_TYPE (type);
7584 if (orig_qual_type != NULL_TREE)
7586 if (orig_qual_indirect == 0)
7587 orig_qual_type = TREE_TYPE (orig_qual_type);
7589 orig_qual_indirect--;
7592 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7593 orig_qual_indirect);
7594 type = c_build_pointer_type (type);
7595 type_quals = array_ptr_quals;
7597 type = c_build_qualified_type (type, type_quals);
7599 /* We don't yet implement attributes in this context. */
7600 if (array_ptr_attrs != NULL_TREE)
7601 warning_at (loc, OPT_Wattributes,
7602 "attributes in parameter array declarator ignored");
7604 size_varies = false;
7605 array_parameter_p = true;
7607 else if (TREE_CODE (type) == FUNCTION_TYPE)
7609 if (type_quals & TYPE_QUAL_ATOMIC)
7612 "%<_Atomic%>-qualified function type");
7613 type_quals &= ~TYPE_QUAL_ATOMIC;
7615 else if (type_quals)
7616 pedwarn (loc, OPT_Wpedantic,
7617 "ISO C forbids qualified function types");
7619 type = c_build_qualified_type (type, type_quals);
7620 type = c_build_pointer_type (type);
7621 type_quals = TYPE_UNQUALIFIED;
7623 else if (type_quals)
7624 type = c_build_qualified_type (type, type_quals);
7626 decl = build_decl (declarator->id_loc,
7627 PARM_DECL, declarator->u.id.id, type);
7629 C_DECL_VARIABLE_SIZE (decl) = 1;
7630 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7632 /* Compute the type actually passed in the parmlist,
7633 for the case where there is no prototype.
7634 (For example, shorts and chars are passed as ints.)
7635 When there is a prototype, this is overridden later. */
7637 if (type == error_mark_node)
7638 promoted_type = type;
7640 promoted_type = c_type_promotes_to (type);
7642 DECL_ARG_TYPE (decl) = promoted_type;
7643 if (declspecs->inline_p)
7644 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7645 if (declspecs->noreturn_p)
7646 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7648 else if (decl_context == FIELD)
7650 /* Note that the grammar rejects storage classes in typenames
7652 gcc_assert (storage_class == csc_none && !threadp
7653 && !declspecs->inline_p && !declspecs->noreturn_p);
7655 /* Structure field. It may not be a function. */
7657 if (TREE_CODE (type) == FUNCTION_TYPE)
7659 error_at (loc, "field %qE declared as a function", name);
7660 type = build_pointer_type (type);
7662 else if (TREE_CODE (type) != ERROR_MARK
7663 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7666 error_at (loc, "field %qE has incomplete type", name);
7668 error_at (loc, "unnamed field has incomplete type");
7669 type = error_mark_node;
7671 else if (TREE_CODE (type) == ARRAY_TYPE
7672 && TYPE_DOMAIN (type) == NULL_TREE)
7674 /* We have a flexible array member through a typedef.
7675 Set suitable range. Whether this is a correct position
7676 for a flexible array member will be determined elsewhere. */
7677 if (!in_system_header_at (input_location))
7678 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7679 "support flexible array members");
7680 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7681 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7683 if (orig_qual_indirect == 0)
7684 orig_qual_type = NULL_TREE;
7686 if (type != error_mark_node
7687 && !verify_type_context (loc, TCTX_FIELD, type))
7688 type = error_mark_node;
7690 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7691 orig_qual_indirect);
7692 decl = build_decl (declarator->id_loc,
7693 FIELD_DECL, declarator->u.id.id, type);
7694 DECL_NONADDRESSABLE_P (decl) = bitfield;
7695 if (bitfield && !declarator->u.id.id)
7696 DECL_PADDING_P (decl) = 1;
7699 C_DECL_VARIABLE_SIZE (decl) = 1;
7701 else if (TREE_CODE (type) == FUNCTION_TYPE)
7703 if (storage_class == csc_register || threadp)
7705 error_at (loc, "invalid storage class for function %qE", name);
7707 else if (current_scope != file_scope)
7709 /* Function declaration not at file scope. Storage
7710 classes other than `extern' are not allowed, C99
7711 6.7.1p5, and `extern' makes no difference. However,
7712 GCC allows 'auto', perhaps with 'inline', to support
7713 nested functions. */
7714 if (storage_class == csc_auto)
7715 pedwarn (loc, OPT_Wpedantic,
7716 "invalid storage class for function %qE", name);
7717 else if (storage_class == csc_static)
7719 error_at (loc, "invalid storage class for function %qE", name);
7721 storage_class = declspecs->storage_class = csc_none;
7727 decl = build_decl (declarator->id_loc,
7728 FUNCTION_DECL, declarator->u.id.id, type);
7729 decl = build_decl_attribute_variant (decl, decl_attr);
7731 if (type_quals & TYPE_QUAL_ATOMIC)
7734 "%<_Atomic%>-qualified function type");
7735 type_quals &= ~TYPE_QUAL_ATOMIC;
7737 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7738 pedwarn (loc, OPT_Wpedantic,
7739 "ISO C forbids qualified function types");
7741 /* Every function declaration is an external reference
7742 (DECL_EXTERNAL) except for those which are not at file
7743 scope and are explicitly declared "auto". This is
7744 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7745 GCC to signify a forward declaration of a nested function. */
7746 if (storage_class == csc_auto && current_scope != file_scope)
7747 DECL_EXTERNAL (decl) = 0;
7748 /* In C99, a function which is declared 'inline' with 'extern'
7749 is not an external reference (which is confusing). It
7750 means that the later definition of the function must be output
7751 in this file, C99 6.7.4p6. In GNU C89, a function declared
7752 'extern inline' is an external reference. */
7753 else if (declspecs->inline_p && storage_class != csc_static)
7754 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7755 == flag_gnu89_inline);
7757 DECL_EXTERNAL (decl) = !initialized;
7759 /* Record absence of global scope for `static' or `auto'. */
7761 = !(storage_class == csc_static || storage_class == csc_auto);
7763 /* For a function definition, record the argument information
7764 block where store_parm_decls will look for it. */
7766 current_function_arg_info = arg_info;
7768 if (declspecs->default_int_p)
7769 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7771 /* Record presence of `inline' and `_Noreturn', if it is
7773 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7775 if (declspecs->inline_p)
7776 pedwarn (loc, 0, "cannot inline function %<main%>");
7777 if (declspecs->noreturn_p)
7778 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7782 if (declspecs->inline_p)
7783 /* Record that the function is declared `inline'. */
7784 DECL_DECLARED_INLINE_P (decl) = 1;
7785 if (declspecs->noreturn_p)
7788 pedwarn_c99 (loc, OPT_Wpedantic,
7789 "ISO C99 does not support %<_Noreturn%>");
7791 pedwarn_c99 (loc, OPT_Wpedantic,
7792 "ISO C90 does not support %<_Noreturn%>");
7793 TREE_THIS_VOLATILE (decl) = 1;
7799 /* It's a variable. */
7800 /* An uninitialized decl with `extern' is a reference. */
7801 int extern_ref = !initialized && storage_class == csc_extern;
7803 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7804 orig_qual_indirect);
7806 /* C99 6.2.2p7: It is invalid (compile-time undefined
7807 behavior) to create an 'extern' declaration for a
7808 variable if there is a global declaration that is
7809 'static' and the global declaration is not visible.
7810 (If the static declaration _is_ currently visible,
7811 the 'extern' declaration is taken to refer to that decl.) */
7812 if (extern_ref && current_scope != file_scope)
7814 tree global_decl = identifier_global_value (declarator->u.id.id);
7815 tree visible_decl = lookup_name (declarator->u.id.id);
7818 && global_decl != visible_decl
7819 && VAR_P (global_decl)
7820 && !TREE_PUBLIC (global_decl))
7821 error_at (loc, "variable previously declared %<static%> "
7822 "redeclared %<extern%>");
7825 decl = build_decl (declarator->id_loc,
7826 VAR_DECL, declarator->u.id.id, type);
7828 C_DECL_VARIABLE_SIZE (decl) = 1;
7830 if (declspecs->inline_p)
7831 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7832 if (declspecs->noreturn_p)
7833 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7835 /* At file scope, an initialized extern declaration may follow
7836 a static declaration. In that case, DECL_EXTERNAL will be
7837 reset later in start_decl. */
7838 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7840 /* At file scope, the presence of a `static' or `register' storage
7841 class specifier, or the absence of all storage class specifiers
7842 makes this declaration a definition (perhaps tentative). Also,
7843 the absence of `static' makes it public. */
7844 if (current_scope == file_scope)
7846 TREE_PUBLIC (decl) = storage_class != csc_static;
7847 TREE_STATIC (decl) = !extern_ref;
7849 /* Not at file scope, only `static' makes a static definition. */
7852 TREE_STATIC (decl) = (storage_class == csc_static);
7853 TREE_PUBLIC (decl) = extern_ref;
7857 set_decl_tls_model (decl, decl_default_tls_model (decl));
7860 if ((storage_class == csc_extern
7861 || (storage_class == csc_none
7862 && TREE_CODE (type) == FUNCTION_TYPE
7864 && variably_modified_type_p (type, NULL_TREE))
7867 if (TREE_CODE (type) == FUNCTION_TYPE)
7868 error_at (loc, "non-nested function with variably modified type");
7870 error_at (loc, "object with variably modified type must have "
7874 /* For nested functions disqualify ones taking VLAs by value
7875 from inlining since the middle-end cannot deal with this.
7876 ??? We should arrange for those to be passed by reference
7877 with emitting the copy on the caller side in the frontend. */
7878 if (storage_class == csc_none
7879 && TREE_CODE (type) == FUNCTION_TYPE)
7880 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7882 tree arg = TREE_VALUE (al);
7883 if (arg != error_mark_node
7884 && C_TYPE_VARIABLE_SIZE (arg))
7886 DECL_UNINLINABLE (decl) = 1;
7891 /* Record `register' declaration for warnings on &
7892 and in case doing stupid register allocation. */
7894 if (storage_class == csc_register)
7896 C_DECL_REGISTER (decl) = 1;
7897 DECL_REGISTER (decl) = 1;
7900 /* Record constancy and volatility. */
7901 c_apply_type_quals_to_decl (type_quals, decl);
7903 /* Apply _Alignas specifiers. */
7906 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7907 DECL_USER_ALIGN (decl) = 1;
7910 /* If a type has volatile components, it should be stored in memory.
7911 Otherwise, the fact that those components are volatile
7912 will be ignored, and would even crash the compiler.
7913 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7914 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7915 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7916 || TREE_CODE (decl) == RESULT_DECL))
7918 /* It is not an error for a structure with volatile fields to
7919 be declared register, but reset DECL_REGISTER since it
7920 cannot actually go in a register. */
7921 int was_reg = C_DECL_REGISTER (decl);
7922 C_DECL_REGISTER (decl) = 0;
7923 DECL_REGISTER (decl) = 0;
7924 c_mark_addressable (decl);
7925 C_DECL_REGISTER (decl) = was_reg;
7928 /* This is the earliest point at which we might know the assembler
7929 name of a variable. Thus, if it's known before this, die horribly. */
7930 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7931 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7935 && TREE_PUBLIC (decl)
7936 && TREE_STATIC (decl)
7937 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7938 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7939 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7940 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7941 ("non-local variable %qD with anonymous type is "
7942 "questionable in C++"),
7949 /* Decode the parameter-list info for a function type or function definition.
7950 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7951 if there is an identifier list instead of a parameter decl list).
7952 These two functions are separate because when a function returns
7953 or receives functions then each is called multiple times but the order
7954 of calls is different. The last call to `grokparms' is always the one
7955 that contains the formal parameter names of a function definition.
7957 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7959 FUNCDEF_FLAG is true for a function definition, false for
7960 a mere declaration. A nonempty identifier-list gets an error message
7961 when FUNCDEF_FLAG is false. */
7964 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7966 tree arg_types = arg_info->types;
7968 if (funcdef_flag && arg_info->had_vla_unspec)
7970 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7972 error ("%<[*]%> not allowed in other than function prototype scope");
7975 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc2x
7976 && !in_system_header_at (input_location))
7977 warning (OPT_Wstrict_prototypes,
7978 "function declaration isn%'t a prototype");
7980 if (arg_types == error_mark_node)
7981 /* Don't set TYPE_ARG_TYPES in this case. */
7984 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7988 pedwarn (input_location, 0, "parameter names (without types) in "
7989 "function declaration");
7990 arg_info->parms = NULL_TREE;
7993 arg_info->parms = arg_info->types;
7995 arg_info->types = NULL_TREE;
8000 tree parm, type, typelt;
8001 unsigned int parmno;
8003 /* In C2X, convert () to (void). */
8006 && !arg_info->parms)
8007 arg_types = arg_info->types = void_list_node;
8009 /* If there is a parameter of incomplete type in a definition,
8010 this is an error. In a declaration this is valid, and a
8011 struct or union type may be completed later, before any calls
8012 or definition of the function. In the case where the tag was
8013 first declared within the parameter list, a warning has
8014 already been given. If a parameter has void type, then
8015 however the function cannot be defined or called, so
8018 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
8020 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
8022 type = TREE_VALUE (typelt);
8023 if (type == error_mark_node)
8026 if (!COMPLETE_TYPE_P (type))
8030 if (DECL_NAME (parm))
8031 error_at (input_location,
8032 "parameter %u (%q+D) has incomplete type",
8035 error_at (DECL_SOURCE_LOCATION (parm),
8036 "parameter %u has incomplete type",
8039 TREE_VALUE (typelt) = error_mark_node;
8040 TREE_TYPE (parm) = error_mark_node;
8041 arg_types = NULL_TREE;
8043 else if (VOID_TYPE_P (type))
8045 if (DECL_NAME (parm))
8046 warning_at (input_location, 0,
8047 "parameter %u (%q+D) has void type",
8050 warning_at (DECL_SOURCE_LOCATION (parm), 0,
8051 "parameter %u has void type",
8056 if (DECL_NAME (parm) && TREE_USED (parm))
8057 warn_if_shadowing (parm);
8063 /* Allocate and initialize a c_arg_info structure from the parser's
8067 build_arg_info (void)
8069 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
8070 ret->parms = NULL_TREE;
8072 ret->types = NULL_TREE;
8073 ret->others = NULL_TREE;
8074 ret->pending_sizes = NULL;
8075 ret->had_vla_unspec = 0;
8079 /* Take apart the current scope and return a c_arg_info structure with
8080 info on a parameter list just parsed.
8082 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8084 ELLIPSIS being true means the argument list ended in '...' so don't
8085 append a sentinel (void_list_node) to the end of the type-list.
8087 EXPR is NULL or an expression that needs to be evaluated for the
8088 side effects of array size expressions in the parameters. */
8091 get_parm_info (bool ellipsis, tree expr)
8093 struct c_binding *b = current_scope->bindings;
8094 struct c_arg_info *arg_info = build_arg_info ();
8096 tree parms = NULL_TREE;
8097 vec<c_arg_tag, va_gc> *tags = NULL;
8098 tree types = NULL_TREE;
8099 tree others = NULL_TREE;
8101 bool gave_void_only_once_err = false;
8103 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8105 /* The bindings in this scope must not get put into a block.
8106 We will take care of deleting the binding nodes. */
8107 current_scope->bindings = 0;
8109 /* This function is only called if there was *something* on the
8113 /* A parameter list consisting solely of 'void' indicates that the
8114 function takes no arguments. But if the 'void' is qualified
8115 (by 'const' or 'volatile'), or has a storage class specifier
8116 ('register'), then the behavior is undefined; issue an error.
8117 Typedefs for 'void' are OK (see DR#157). */
8118 if (b->prev == 0 /* one binding */
8119 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8120 && !DECL_NAME (b->decl) /* anonymous */
8121 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8123 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8124 || C_DECL_REGISTER (b->decl))
8125 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8127 /* There cannot be an ellipsis. */
8129 error_at (b->locus, "%<void%> must be the only parameter");
8131 arg_info->types = void_list_node;
8136 types = void_list_node;
8138 /* Break up the bindings list into parms, tags, types, and others;
8139 apply sanity checks; purge the name-to-decl bindings. */
8142 tree decl = b->decl;
8143 tree type = TREE_TYPE (decl);
8145 const char *keyword;
8147 switch (TREE_CODE (decl))
8152 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8153 I_SYMBOL_BINDING (b->id) = b->shadowed;
8156 /* Check for forward decls that never got their actual decl. */
8157 if (TREE_ASM_WRITTEN (decl))
8159 "parameter %q+D has just a forward declaration", decl);
8160 /* Check for (..., void, ...) and issue an error. */
8161 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8163 if (!gave_void_only_once_err)
8165 error_at (b->locus, "%<void%> must be the only parameter");
8166 gave_void_only_once_err = true;
8171 /* Valid parameter, add it to the list. */
8172 DECL_CHAIN (decl) = parms;
8175 /* Since there is a prototype, args are passed in their
8176 declared types. The back end may override this later. */
8177 DECL_ARG_TYPE (decl) = type;
8178 types = tree_cons (0, type, types);
8182 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8183 case UNION_TYPE: keyword = "union"; goto tag;
8184 case RECORD_TYPE: keyword = "struct"; goto tag;
8186 /* Types may not have tag-names, in which case the type
8187 appears in the bindings list with b->id NULL. */
8190 gcc_assert (I_TAG_BINDING (b->id) == b);
8191 I_TAG_BINDING (b->id) = b->shadowed;
8194 /* Warn about any struct, union or enum tags defined in a
8195 parameter list. The scope of such types is limited to
8196 the parameter list, which is rarely if ever desirable
8197 (it's impossible to call such a function with type-
8198 correct arguments). An anonymous union parm type is
8199 meaningful as a GNU extension, so don't warn for that. */
8200 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8203 /* The %s will be one of 'struct', 'union', or 'enum'. */
8204 warning_at (b->locus, 0,
8205 "%<%s %E%> declared inside parameter list"
8206 " will not be visible outside of this definition or"
8207 " declaration", keyword, b->id);
8209 /* The %s will be one of 'struct', 'union', or 'enum'. */
8210 warning_at (b->locus, 0,
8211 "anonymous %s declared inside parameter list"
8212 " will not be visible outside of this definition or"
8213 " declaration", keyword);
8218 vec_safe_push (tags, tag);
8222 /* FUNCTION_DECLs appear when there is an implicit function
8223 declaration in the parameter list. */
8224 gcc_assert (b->nested || seen_error ());
8229 /* CONST_DECLs appear here when we have an embedded enum,
8230 and TYPE_DECLs appear here when we have an embedded struct
8231 or union. No warnings for this - we already warned about the
8234 /* When we reinsert this decl in the function body, we need
8235 to reconstruct whether it was marked as nested. */
8236 gcc_assert (!b->nested);
8237 DECL_CHAIN (decl) = others;
8243 /* error_mark_node appears here when we have an undeclared
8244 variable. Just throw it away. */
8247 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8248 I_SYMBOL_BINDING (b->id) = b->shadowed;
8252 /* Other things that might be encountered. */
8259 b = free_binding_and_advance (b);
8262 arg_info->parms = parms;
8263 arg_info->tags = tags;
8264 arg_info->types = types;
8265 arg_info->others = others;
8266 arg_info->pending_sizes = expr;
8270 /* Get the struct, enum or union (CODE says which) with tag NAME.
8271 Define the tag as a forward-reference with location LOC if it is
8272 not defined. HAVE_STD_ATTRS says whether any standard attributes
8273 were present after the struct, union or enum keyword; ATTRS are the
8274 standard attributes present there. Return a c_typespec structure
8275 for the type specifier. */
8278 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8279 bool have_std_attrs, tree attrs)
8281 struct c_typespec ret;
8285 ret.expr = NULL_TREE;
8286 ret.expr_const_operands = true;
8288 /* If a cross reference is requested, look up the type
8289 already defined for this tag and return it. */
8291 ref = lookup_tag (code, name, false, &refloc);
8292 /* If this is the right type of tag, return what we found.
8293 (This reference will be shadowed by shadow_tag later if appropriate.)
8294 If this is the wrong type of tag, do not return it. If it was the
8295 wrong type in the same scope, we will have had an error
8296 message already; if in a different scope and declaring
8297 a name, pending_xref_error will give an error message; but if in a
8298 different scope and not declaring a name, this tag should
8299 shadow the previous declaration of a different type of tag, and
8300 this would not work properly if we return the reference found.
8301 (For example, with "struct foo" in an outer scope, "union foo;"
8302 must shadow that tag with a new one of union type.) */
8304 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8305 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8306 if (ref && TREE_CODE (ref) == code)
8308 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8309 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8310 && loc != UNKNOWN_LOCATION
8313 auto_diagnostic_group d;
8317 if (warning_at (loc, OPT_Wc___compat,
8318 ("enum type defined in struct or union "
8319 "is not visible in C++")))
8320 inform (refloc, "enum type defined here");
8323 if (warning_at (loc, OPT_Wc___compat,
8324 ("struct defined in struct or union "
8325 "is not visible in C++")))
8326 inform (refloc, "struct defined here");
8329 if (warning_at (loc, OPT_Wc___compat,
8330 ("union defined in struct or union "
8331 "is not visible in C++")))
8332 inform (refloc, "union defined here");
8343 /* If no such tag is yet defined, create a forward-reference node
8344 and record it as the "definition".
8345 When a real declaration of this type is found,
8346 the forward-reference will be altered into a real type. */
8348 ref = make_node (code);
8349 if (code == ENUMERAL_TYPE)
8351 /* Give the type a default layout like unsigned int
8352 to avoid crashing if it does not get defined. */
8353 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8354 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8355 TYPE_USER_ALIGN (ref) = 0;
8356 TYPE_UNSIGNED (ref) = 1;
8357 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8358 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8359 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8362 pushtag (loc, name, ref);
8363 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8369 /* Get the struct, enum or union (CODE says which) with tag NAME.
8370 Define the tag as a forward-reference if it is not defined.
8371 Return a tree for the type. */
8374 xref_tag (enum tree_code code, tree name)
8376 return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
8379 /* Make sure that the tag NAME is defined *in the current scope*
8380 at least as a forward reference.
8381 LOC is the location of the struct's definition.
8382 CODE says which kind of tag NAME ought to be.
8384 This stores the current value of the file static STRUCT_PARSE_INFO
8385 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8386 new c_struct_parse_info structure. The old value of
8387 STRUCT_PARSE_INFO is restored in finish_struct. */
8390 start_struct (location_t loc, enum tree_code code, tree name,
8391 class c_struct_parse_info **enclosing_struct_parse_info)
8393 /* If there is already a tag defined at this scope
8394 (as a forward reference), just return it. */
8396 tree ref = NULL_TREE;
8397 location_t refloc = UNKNOWN_LOCATION;
8399 if (name != NULL_TREE)
8400 ref = lookup_tag (code, name, true, &refloc);
8401 if (ref && TREE_CODE (ref) == code)
8403 if (TYPE_STUB_DECL (ref))
8404 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8406 if (TYPE_SIZE (ref))
8408 auto_diagnostic_group d;
8409 if (code == UNION_TYPE)
8410 error_at (loc, "redefinition of %<union %E%>", name);
8412 error_at (loc, "redefinition of %<struct %E%>", name);
8413 if (refloc != UNKNOWN_LOCATION)
8414 inform (refloc, "originally defined here");
8415 /* Don't create structures using a name already in use. */
8418 else if (C_TYPE_BEING_DEFINED (ref))
8420 if (code == UNION_TYPE)
8421 error_at (loc, "nested redefinition of %<union %E%>", name);
8423 error_at (loc, "nested redefinition of %<struct %E%>", name);
8424 /* Don't bother to report "originally defined here" for a
8425 nested redefinition; the original definition should be
8427 /* Don't create structures that contain themselves. */
8432 /* Otherwise create a forward-reference just so the tag is in scope. */
8434 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8436 ref = make_node (code);
8437 pushtag (loc, name, ref);
8440 C_TYPE_BEING_DEFINED (ref) = 1;
8441 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8442 TYPE_PACKED (v) = flag_pack_struct;
8444 *enclosing_struct_parse_info = struct_parse_info;
8445 struct_parse_info = new c_struct_parse_info ();
8447 /* FIXME: This will issue a warning for a use of a type defined
8448 within a statement expr used within sizeof, et. al. This is not
8449 terribly serious as C++ doesn't permit statement exprs within
8451 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8452 warning_at (loc, OPT_Wc___compat,
8453 "defining type in %qs expression is invalid in C++",
8456 : (in_typeof ? "typeof" : "alignof")));
8461 /* Process the specs, declarator and width (NULL if omitted)
8462 of a structure component, returning a FIELD_DECL node.
8463 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8464 DECL_ATTRS is as for grokdeclarator.
8466 LOC is the location of the structure component.
8468 This is done during the parsing of the struct declaration.
8469 The FIELD_DECL nodes are chained together and the lot of them
8470 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8473 grokfield (location_t loc,
8474 struct c_declarator *declarator, struct c_declspecs *declspecs,
8475 tree width, tree *decl_attrs)
8479 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8480 && width == NULL_TREE)
8482 /* This is an unnamed decl.
8484 If we have something of the form "union { list } ;" then this
8485 is the anonymous union extension. Similarly for struct.
8487 If this is something of the form "struct foo;", then
8488 If MS or Plan 9 extensions are enabled, this is handled as
8489 an anonymous struct.
8490 Otherwise this is a forward declaration of a structure tag.
8492 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8493 If foo names a structure or union without a tag, then this
8494 is an anonymous struct (this is permitted by C11).
8495 If MS or Plan 9 extensions are enabled and foo names a
8496 structure, then again this is an anonymous struct.
8497 Otherwise this is an error.
8499 Oh what a horrid tangled web we weave. I wonder if MS consciously
8500 took this from Plan 9 or if it was an accident of implementation
8501 that took root before someone noticed the bug... */
8503 tree type = declspecs->type;
8506 if (RECORD_OR_UNION_TYPE_P (type)
8507 && (flag_ms_extensions
8508 || flag_plan9_extensions
8509 || !declspecs->typedef_p))
8511 if (flag_ms_extensions || flag_plan9_extensions)
8513 else if (TYPE_NAME (type) == NULL)
8520 pedwarn (loc, 0, "declaration does not declare anything");
8524 pedwarn_c99 (loc, OPT_Wpedantic,
8525 "ISO C99 doesn%'t support unnamed structs/unions");
8527 pedwarn_c99 (loc, OPT_Wpedantic,
8528 "ISO C90 doesn%'t support unnamed structs/unions");
8531 value = grokdeclarator (declarator, declspecs, FIELD, false,
8532 width ? &width : NULL, decl_attrs, NULL, NULL,
8535 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8536 DECL_INITIAL (value) = width;
8538 SET_DECL_C_BIT_FIELD (value);
8540 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8542 /* If we currently have a binding for this field, set the
8543 in_struct field in the binding, so that we warn about lookups
8545 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8548 /* If the in_struct field is not yet set, push it on a list
8549 to be cleared when this struct is finished. */
8552 struct_parse_info->fields.safe_push (b);
8561 /* Subroutine of detect_field_duplicates: return whether X and Y,
8562 which are both fields in the same struct, have duplicate field
8566 is_duplicate_field (tree x, tree y)
8568 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8571 /* When using -fplan9-extensions, an anonymous field whose name is a
8572 typedef can duplicate a field name. */
8573 if (flag_plan9_extensions
8574 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8576 tree xt, xn, yt, yn;
8579 if (DECL_NAME (x) != NULL_TREE)
8581 else if (RECORD_OR_UNION_TYPE_P (xt)
8582 && TYPE_NAME (xt) != NULL_TREE
8583 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8584 xn = DECL_NAME (TYPE_NAME (xt));
8589 if (DECL_NAME (y) != NULL_TREE)
8591 else if (RECORD_OR_UNION_TYPE_P (yt)
8592 && TYPE_NAME (yt) != NULL_TREE
8593 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8594 yn = DECL_NAME (TYPE_NAME (yt));
8598 if (xn != NULL_TREE && xn == yn)
8605 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8606 to HTAB, giving errors for any duplicates. */
8609 detect_field_duplicates_hash (tree fieldlist,
8610 hash_table<nofree_ptr_hash <tree_node> > *htab)
8615 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8616 if ((y = DECL_NAME (x)) != NULL_TREE)
8618 slot = htab->find_slot (y, INSERT);
8621 error ("duplicate member %q+D", x);
8622 DECL_NAME (x) = NULL_TREE;
8626 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8628 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8630 /* When using -fplan9-extensions, an anonymous field whose
8631 name is a typedef can duplicate a field name. */
8632 if (flag_plan9_extensions
8633 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8634 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8636 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8637 slot = htab->find_slot (xn, INSERT);
8639 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8645 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8646 the list such that this does not present a problem later. */
8649 detect_field_duplicates (tree fieldlist)
8654 /* If the struct is the list of instance variables of an Objective-C
8655 class, then we need to check all the instance variables of
8656 superclasses when checking for duplicates (since you can't have
8657 an instance variable in a subclass with the same name as an
8658 instance variable in a superclass). We pass on this job to the
8659 Objective-C compiler. objc_detect_field_duplicates() will return
8660 false if we are not checking the list of instance variables and
8661 the C frontend should proceed with the standard field duplicate
8662 checks. If we are checking the list of instance variables, the
8663 ObjC frontend will do the check, emit the errors if needed, and
8664 then return true. */
8665 if (c_dialect_objc ())
8666 if (objc_detect_field_duplicates (false))
8669 /* First, see if there are more than "a few" fields.
8670 This is trivially true if there are zero or one fields. */
8671 if (!fieldlist || !DECL_CHAIN (fieldlist))
8676 if (DECL_NAME (x) == NULL_TREE
8677 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8680 } while (timeout > 0 && x);
8682 /* If there were "few" fields and no anonymous structures or unions,
8683 avoid the overhead of allocating a hash table. Instead just do
8684 the nested traversal thing. */
8687 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8688 /* When using -fplan9-extensions, we can have duplicates
8689 between typedef names and fields. */
8691 || (flag_plan9_extensions
8692 && DECL_NAME (x) == NULL_TREE
8693 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8694 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8695 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8697 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8698 if (is_duplicate_field (y, x))
8700 error ("duplicate member %q+D", x);
8701 DECL_NAME (x) = NULL_TREE;
8707 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8708 detect_field_duplicates_hash (fieldlist, &htab);
8712 /* Finish up struct info used by -Wc++-compat. */
8715 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8716 location_t record_loc)
8720 struct c_binding *b;
8722 if (fieldlist == NULL_TREE)
8724 if (code == RECORD_TYPE)
8725 warning_at (record_loc, OPT_Wc___compat,
8726 "empty struct has size 0 in C, size 1 in C++");
8728 warning_at (record_loc, OPT_Wc___compat,
8729 "empty union has size 0 in C, size 1 in C++");
8732 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8733 the current struct. We do this now at the end of the struct
8734 because the flag is used to issue visibility warnings, and we
8735 only want to issue those warnings if the type is referenced
8736 outside of the struct declaration. */
8737 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8738 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8740 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8741 typedefs used when declaring fields in this struct. If the name
8742 of any of the fields is also a typedef name then the struct would
8743 not parse in C++, because the C++ lookup rules say that the
8744 typedef name would be looked up in the context of the struct, and
8745 would thus be the field rather than the typedef. */
8746 if (!struct_parse_info->typedefs_seen.is_empty ()
8747 && fieldlist != NULL_TREE)
8749 /* Use a hash_set<tree> using the name of the typedef. We can use
8750 a hash_set<tree> because identifiers are interned. */
8751 hash_set<tree> tset;
8753 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8754 tset.add (DECL_NAME (x));
8756 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8758 if (DECL_NAME (x) != NULL_TREE
8759 && tset.contains (DECL_NAME (x)))
8761 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8762 ("using %qD as both field and typedef name is "
8765 /* FIXME: It would be nice to report the location where
8766 the typedef name is used. */
8771 /* For each field which has a binding and which was not defined in
8772 an enclosing struct, clear the in_struct field. */
8773 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8777 /* Function to help qsort sort FIELD_DECLs by name order. */
8780 field_decl_cmp (const void *x_p, const void *y_p)
8782 const tree *const x = (const tree *) x_p;
8783 const tree *const y = (const tree *) y_p;
8785 if (DECL_NAME (*x) == DECL_NAME (*y))
8786 /* A nontype is "greater" than a type. */
8787 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8788 if (DECL_NAME (*x) == NULL_TREE)
8790 if (DECL_NAME (*y) == NULL_TREE)
8792 if (DECL_NAME (*x) < DECL_NAME (*y))
8797 /* If this structure or union completes the type of any previous
8798 variable declaration, lay it out and output its rtl. */
8800 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
8802 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
8804 tree decl = TREE_VALUE (x);
8805 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8806 layout_array_type (TREE_TYPE (decl));
8807 if (TREE_CODE (decl) != TYPE_DECL)
8809 relayout_decl (decl);
8810 if (c_dialect_objc ())
8811 objc_check_decl (decl);
8812 rest_of_decl_compilation (decl, toplevel, 0);
8818 /* Determine whether the FIELD_DECL X is a flexible array member according to
8820 A. whether the FIELD_DECL X is the last field of the DECL_CONTEXT;
8821 B. whether the FIELD_DECL is an array that is declared as "[]", "[0]",
8823 C. flag_strict_flex_arrays;
8824 D. the attribute strict_flex_array that is attached to the field
8826 Return TRUE when it's a flexible array member, FALSE otherwise. */
8829 is_flexible_array_member_p (bool is_last_field,
8832 /* If not the last field, return false. */
8836 /* If not an array field, return false. */
8837 if (TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
8840 bool is_zero_length_array = zero_length_array_type_p (TREE_TYPE (x));
8841 bool is_one_element_array = one_element_array_type_p (TREE_TYPE (x));
8842 bool is_flexible_array = flexible_array_member_type_p (TREE_TYPE (x));
8844 unsigned int strict_flex_array_level = flag_strict_flex_arrays;
8846 tree attr_strict_flex_array = lookup_attribute ("strict_flex_array",
8847 DECL_ATTRIBUTES (x));
8848 /* If there is a strict_flex_array attribute attached to the field,
8849 override the flag_strict_flex_arrays. */
8850 if (attr_strict_flex_array)
8852 /* Get the value of the level first from the attribute. */
8853 unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
8854 gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
8855 attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
8856 gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
8857 attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
8858 gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
8859 attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
8861 /* The attribute has higher priority than flag_struct_flex_array. */
8862 strict_flex_array_level = attr_strict_flex_array_level;
8865 switch (strict_flex_array_level)
8868 /* Default, all trailing arrays are flexible array members. */
8871 /* Level 1: all "[1]", "[0]", and "[]" are flexible array members. */
8872 if (is_one_element_array)
8876 /* Level 2: all "[0]", and "[]" are flexible array members. */
8877 if (is_zero_length_array)
8881 /* Level 3: Only "[]" are flexible array members. */
8882 if (is_flexible_array)
8892 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8893 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8894 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8895 ATTRIBUTES are attributes to be applied to the structure.
8897 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8898 the struct was started. */
8901 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8902 class c_struct_parse_info *enclosing_struct_parse_info)
8905 bool toplevel = file_scope == current_scope;
8907 /* If this type was previously laid out as a forward reference,
8908 make sure we lay it out again. */
8910 TYPE_SIZE (t) = NULL_TREE;
8912 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8916 for (x = fieldlist; x; x = DECL_CHAIN (x))
8918 if (DECL_NAME (x) != NULL_TREE)
8920 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8926 if (TREE_CODE (t) == UNION_TYPE)
8929 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8931 pedwarn (loc, OPT_Wpedantic, "union has no members");
8936 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8938 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8943 /* Install struct as DECL_CONTEXT of each field decl.
8944 Also process specified field sizes, found in the DECL_INITIAL,
8945 storing 0 there after the type has been changed to precision equal
8946 to its width, rather than the precision of the specified standard
8947 type. (Correct layout requires the original type to have been preserved
8950 bool saw_named_field = false;
8951 for (x = fieldlist; x; x = DECL_CHAIN (x))
8953 /* Whether this field is the last field of the structure or union.
8954 for UNION, any field is the last field of it. */
8955 bool is_last_field = (DECL_CHAIN (x) == NULL_TREE)
8956 || (TREE_CODE (t) == UNION_TYPE);
8958 if (TREE_TYPE (x) == error_mark_node)
8961 DECL_CONTEXT (x) = t;
8963 /* If any field is const, the structure type is pseudo-const. */
8964 if (TREE_READONLY (x))
8965 C_TYPE_FIELDS_READONLY (t) = 1;
8968 /* A field that is pseudo-const makes the structure likewise. */
8969 tree t1 = strip_array_types (TREE_TYPE (x));
8970 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8971 C_TYPE_FIELDS_READONLY (t) = 1;
8974 /* Any field that is volatile means variables of this type must be
8975 treated in some ways as volatile. */
8976 if (TREE_THIS_VOLATILE (x))
8977 C_TYPE_FIELDS_VOLATILE (t) = 1;
8979 /* Any field of nominal variable size implies structure is too. */
8980 if (C_DECL_VARIABLE_SIZE (x))
8981 C_TYPE_VARIABLE_SIZE (t) = 1;
8983 if (DECL_C_BIT_FIELD (x))
8985 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8986 DECL_SIZE (x) = bitsize_int (width);
8987 DECL_BIT_FIELD (x) = 1;
8991 && (DECL_BIT_FIELD (x)
8992 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8993 DECL_PACKED (x) = 1;
8995 /* Detect flexible array member in an invalid context. */
8996 if (flexible_array_member_type_p (TREE_TYPE (x)))
8998 if (TREE_CODE (t) == UNION_TYPE)
9000 error_at (DECL_SOURCE_LOCATION (x),
9001 "flexible array member in union");
9002 TREE_TYPE (x) = error_mark_node;
9004 else if (!is_last_field)
9006 error_at (DECL_SOURCE_LOCATION (x),
9007 "flexible array member not at end of struct");
9008 TREE_TYPE (x) = error_mark_node;
9010 else if (!saw_named_field)
9012 error_at (DECL_SOURCE_LOCATION (x),
9013 "flexible array member in a struct with no named "
9015 TREE_TYPE (x) = error_mark_node;
9019 if (pedantic && TREE_CODE (t) == RECORD_TYPE
9020 && flexible_array_type_p (TREE_TYPE (x)))
9021 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
9022 "invalid use of structure with flexible array member");
9024 /* Set DECL_NOT_FLEXARRAY flag for FIELD_DECL x. */
9025 DECL_NOT_FLEXARRAY (x) = !is_flexible_array_member_p (is_last_field, x);
9028 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
9029 saw_named_field = true;
9032 detect_field_duplicates (fieldlist);
9034 /* Now we have the nearly final fieldlist. Record it,
9035 then lay out the structure or union (including the fields). */
9037 TYPE_FIELDS (t) = fieldlist;
9039 maybe_apply_pragma_scalar_storage_order (t);
9043 if (TYPE_SIZE_UNIT (t)
9044 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
9045 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
9046 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
9047 error ("type %qT is too large", t);
9049 /* Give bit-fields their proper types and rewrite the type of array fields
9050 with scalar component if the enclosing type has reverse storage order. */
9051 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
9053 if (TREE_CODE (field) == FIELD_DECL
9054 && DECL_INITIAL (field)
9055 && TREE_TYPE (field) != error_mark_node)
9057 unsigned HOST_WIDE_INT width
9058 = tree_to_uhwi (DECL_INITIAL (field));
9059 tree type = TREE_TYPE (field);
9060 if (width != TYPE_PRECISION (type))
9063 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
9064 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
9066 DECL_INITIAL (field) = NULL_TREE;
9068 else if (TYPE_REVERSE_STORAGE_ORDER (t)
9069 && TREE_CODE (field) == FIELD_DECL
9070 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
9072 tree ftype = TREE_TYPE (field);
9073 tree ctype = strip_array_types (ftype);
9074 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
9076 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
9077 tree *typep = &fmain_type;
9079 *typep = build_distinct_type_copy (*typep);
9080 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
9081 typep = &TREE_TYPE (*typep);
9082 } while (TREE_CODE (*typep) == ARRAY_TYPE);
9084 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
9088 /* Warn on problematic type punning for storage order purposes. */
9089 if (TREE_CODE (t) == UNION_TYPE
9090 && TREE_CODE (field) == FIELD_DECL
9091 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
9093 tree ftype = TREE_TYPE (field);
9094 if (TREE_CODE (ftype) == ARRAY_TYPE)
9095 ftype = strip_array_types (ftype);
9096 if (RECORD_OR_UNION_TYPE_P (ftype)
9097 && TYPE_REVERSE_STORAGE_ORDER (ftype)
9098 != TYPE_REVERSE_STORAGE_ORDER (t))
9099 warning_at (DECL_SOURCE_LOCATION (field),
9100 OPT_Wscalar_storage_order,
9101 "type punning toggles scalar storage order");
9105 /* Now we have the truly final field list.
9106 Store it in this type and in the variants. */
9108 TYPE_FIELDS (t) = fieldlist;
9110 /* If there are lots of fields, sort so we can look through them fast.
9111 We arbitrarily consider 16 or more elts to be "a lot". */
9116 for (x = fieldlist; x; x = DECL_CHAIN (x))
9118 if (len > 15 || DECL_NAME (x) == NULL)
9126 struct lang_type *space;
9127 struct sorted_fields_type *space2;
9129 len += list_length (x);
9131 /* Use the same allocation policy here that make_node uses, to
9132 ensure that this lives as long as the rest of the struct decl.
9133 All decls in an inline function need to be saved. */
9135 space = ggc_cleared_alloc<struct lang_type> ();
9136 space2 = (sorted_fields_type *) ggc_internal_alloc
9137 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
9141 field_array = &space2->elts[0];
9142 for (x = fieldlist; x; x = DECL_CHAIN (x))
9144 field_array[len++] = x;
9146 /* If there is anonymous struct or union, break out of the loop. */
9147 if (DECL_NAME (x) == NULL)
9150 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
9153 TYPE_LANG_SPECIFIC (t) = space;
9154 TYPE_LANG_SPECIFIC (t)->s->len = len;
9155 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9156 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9161 /* If this was supposed to be a transparent union, but we can't
9162 make it one, warn and turn off the flag. */
9163 if (TREE_CODE (t) == UNION_TYPE
9164 && TYPE_TRANSPARENT_AGGR (t)
9165 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9167 TYPE_TRANSPARENT_AGGR (t) = 0;
9168 warning_at (loc, 0, "union cannot be made transparent");
9171 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9172 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9174 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9175 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9176 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9177 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9178 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9179 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9180 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9183 /* Update type location to the one of the definition, instead of e.g.
9184 a forward declaration. */
9185 if (TYPE_STUB_DECL (t))
9186 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9188 /* Finish debugging output for this type. */
9189 rest_of_type_compilation (t, toplevel);
9191 finish_incomplete_vars (incomplete_vars, toplevel);
9193 /* If we're inside a function proper, i.e. not file-scope and not still
9194 parsing parameters, then arrange for the size of a variable sized type
9196 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
9197 add_stmt (build_stmt (loc,
9198 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
9200 if (warn_cxx_compat)
9201 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9203 delete struct_parse_info;
9205 struct_parse_info = enclosing_struct_parse_info;
9207 /* If this struct is defined inside a struct, add it to
9210 && struct_parse_info != NULL
9211 && !in_sizeof && !in_typeof && !in_alignof)
9212 struct_parse_info->struct_types.safe_push (t);
9218 gt_pointer_operator new_value;
9222 /* This routine compares two fields like field_decl_cmp but using the
9223 pointer operator in resort_data. */
9226 resort_field_decl_cmp (const void *x_p, const void *y_p)
9228 const tree *const x = (const tree *) x_p;
9229 const tree *const y = (const tree *) y_p;
9231 if (DECL_NAME (*x) == DECL_NAME (*y))
9232 /* A nontype is "greater" than a type. */
9233 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9234 if (DECL_NAME (*x) == NULL_TREE)
9236 if (DECL_NAME (*y) == NULL_TREE)
9239 tree d1 = DECL_NAME (*x);
9240 tree d2 = DECL_NAME (*y);
9241 resort_data.new_value (&d1, &d1, resort_data.cookie);
9242 resort_data.new_value (&d2, &d2, resort_data.cookie);
9249 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9252 resort_sorted_fields (void *obj,
9253 void * ARG_UNUSED (orig_obj),
9254 gt_pointer_operator new_value,
9257 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9258 resort_data.new_value = new_value;
9259 resort_data.cookie = cookie;
9260 qsort (&sf->elts[0], sf->len, sizeof (tree),
9261 resort_field_decl_cmp);
9264 /* Lay out the type T, and its element type, and so on. */
9267 layout_array_type (tree t)
9269 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9270 layout_array_type (TREE_TYPE (t));
9274 /* Begin compiling the definition of an enumeration type.
9275 NAME is its name (or null if anonymous).
9276 LOC is the enum's location.
9277 Returns the type object, as yet incomplete.
9278 Also records info about it so that build_enumerator
9279 may be used to declare the individual values as they are read. */
9282 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
9284 tree enumtype = NULL_TREE;
9285 location_t enumloc = UNKNOWN_LOCATION;
9287 /* If this is the real definition for a previous forward reference,
9288 fill in the contents in the same object that used to be the
9289 forward reference. */
9291 if (name != NULL_TREE)
9292 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9294 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9296 enumtype = make_node (ENUMERAL_TYPE);
9297 pushtag (loc, name, enumtype);
9299 /* Update type location to the one of the definition, instead of e.g.
9300 a forward declaration. */
9301 else if (TYPE_STUB_DECL (enumtype))
9303 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9304 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9307 if (C_TYPE_BEING_DEFINED (enumtype))
9308 error_at (loc, "nested redefinition of %<enum %E%>", name);
9310 C_TYPE_BEING_DEFINED (enumtype) = 1;
9312 if (TYPE_VALUES (enumtype) != NULL_TREE)
9314 /* This enum is a named one that has been declared already. */
9315 auto_diagnostic_group d;
9316 error_at (loc, "redeclaration of %<enum %E%>", name);
9317 if (enumloc != UNKNOWN_LOCATION)
9318 inform (enumloc, "originally defined here");
9320 /* Completely replace its old definition.
9321 The old enumerators remain defined, however. */
9322 TYPE_VALUES (enumtype) = NULL_TREE;
9325 the_enum->enum_next_value = integer_zero_node;
9326 the_enum->enum_overflow = 0;
9328 if (flag_short_enums)
9329 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9330 TYPE_PACKED (v) = 1;
9332 /* FIXME: This will issue a warning for a use of a type defined
9333 within sizeof in a statement expr. This is not terribly serious
9334 as C++ doesn't permit statement exprs within sizeof anyhow. */
9335 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9336 warning_at (loc, OPT_Wc___compat,
9337 "defining type in %qs expression is invalid in C++",
9340 : (in_typeof ? "typeof" : "alignof")));
9345 /* After processing and defining all the values of an enumeration type,
9346 install their decls in the enumeration type and finish it off.
9347 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9348 and ATTRIBUTES are the specified attributes.
9349 Returns ENUMTYPE. */
9352 finish_enum (tree enumtype, tree values, tree attributes)
9355 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9358 bool toplevel = (file_scope == current_scope);
9359 struct lang_type *lt;
9361 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9363 /* Calculate the maximum value of any enumerator in this type. */
9365 if (values == error_mark_node)
9366 minnode = maxnode = integer_zero_node;
9369 minnode = maxnode = TREE_VALUE (values);
9370 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9372 tree value = TREE_VALUE (pair);
9373 if (tree_int_cst_lt (maxnode, value))
9375 if (tree_int_cst_lt (value, minnode))
9380 /* Construct the final type of this enumeration. It is the same
9381 as one of the integral types - the narrowest one that fits, except
9382 that normally we only go as narrow as int - and signed iff any of
9383 the values are negative. */
9384 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9385 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9386 tree_int_cst_min_precision (maxnode, sign));
9388 bool wider_than_int =
9389 (tree_int_cst_lt (minnode, TYPE_MIN_VALUE (integer_type_node))
9390 || tree_int_cst_lt (TYPE_MAX_VALUE (integer_type_node), maxnode));
9392 /* If the precision of the type was specified with an attribute and it
9393 was too small, give an error. Otherwise, use it. */
9394 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9396 if (precision > TYPE_PRECISION (enumtype))
9398 TYPE_PRECISION (enumtype) = 0;
9399 error ("specified mode too small for enumerated values");
9402 precision = TYPE_PRECISION (enumtype);
9405 TYPE_PRECISION (enumtype) = 0;
9407 if (TYPE_PACKED (enumtype)
9408 || precision > TYPE_PRECISION (integer_type_node)
9409 || TYPE_PRECISION (enumtype))
9411 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9414 /* This should only occur when both signed and unsigned
9415 values of maximum precision occur among the
9417 pedwarn (input_location, 0,
9418 "enumeration values exceed range of largest integer");
9419 tem = widest_integer_literal_type_node;
9421 else if (precision > TYPE_PRECISION (intmax_type_node)
9422 && !tree_int_cst_lt (minnode, TYPE_MIN_VALUE (intmax_type_node))
9423 && !tree_int_cst_lt (TYPE_MAX_VALUE (uintmax_type_node),
9425 pedwarn (input_location, OPT_Wpedantic,
9426 "enumeration values exceed range of %qs",
9427 sign == UNSIGNED ? "uintmax_t" : "intmax_t");
9430 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9432 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9433 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9434 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9435 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9436 TYPE_SIZE (enumtype) = NULL_TREE;
9437 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9439 layout_type (enumtype);
9441 if (values != error_mark_node)
9443 /* Change the type of the enumerators to be the enum type. We
9444 need to do this irrespective of the size of the enum, for
9445 proper type checking. Replace the DECL_INITIALs of the
9446 enumerators, and the value slots of the list, with copies
9447 that have the enum type; they cannot be modified in place
9448 because they may be shared (e.g. integer_zero_node) Finally,
9449 change the purpose slots to point to the names of the decls. */
9450 for (pair = values; pair; pair = TREE_CHAIN (pair))
9452 tree enu = TREE_PURPOSE (pair);
9453 tree ini = DECL_INITIAL (enu);
9455 TREE_TYPE (enu) = enumtype;
9457 /* Before C2X, the ISO C Standard mandates enumerators to
9458 have type int, even though the underlying type of an enum
9459 type is unspecified. However, C2X allows enumerators of
9460 any integer type, and if an enumeration has any
9461 enumerators wider than int, all enumerators have the
9462 enumerated type after it is parsed. Any enumerators that
9463 fit in int are given type int in build_enumerator (which
9464 is the correct type while the enumeration is being
9465 parsed), so no conversions are needed here if all
9466 enumerators fit in int. */
9468 ini = convert (enumtype, ini);
9470 DECL_INITIAL (enu) = ini;
9471 TREE_PURPOSE (pair) = DECL_NAME (enu);
9472 /* To match the C++ FE, store the CONST_DECL rather than just its
9474 TREE_VALUE (pair) = enu;
9477 TYPE_VALUES (enumtype) = values;
9480 /* Record the min/max values so that we can warn about bit-field
9481 enumerations that are too small for the values. */
9482 lt = ggc_cleared_alloc<struct lang_type> ();
9483 lt->enum_min = minnode;
9484 lt->enum_max = maxnode;
9485 TYPE_LANG_SPECIFIC (enumtype) = lt;
9487 /* Fix up all variant types of this enum type. */
9488 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9489 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9491 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9492 if (tem == enumtype)
9494 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9495 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9496 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9497 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9498 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9499 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9500 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9501 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9502 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9503 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9504 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9507 /* Finish debugging output for this type. */
9508 rest_of_type_compilation (enumtype, toplevel);
9510 finish_incomplete_vars (incomplete_vars, toplevel);
9512 /* If this enum is defined inside a struct, add it to
9515 && struct_parse_info != NULL
9516 && !in_sizeof && !in_typeof && !in_alignof)
9517 struct_parse_info->struct_types.safe_push (enumtype);
9519 C_TYPE_BEING_DEFINED (enumtype) = 0;
9524 /* Build and install a CONST_DECL for one value of the
9525 current enumeration type (one that was begun with start_enum).
9526 DECL_LOC is the location of the enumerator.
9527 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9528 Return a tree-list containing the CONST_DECL and its value.
9529 Assignment of sequential values by default is handled here. */
9532 build_enumerator (location_t decl_loc, location_t loc,
9533 struct c_enum_contents *the_enum, tree name, tree value)
9537 /* Validate and default VALUE. */
9539 if (value != NULL_TREE)
9541 /* Don't issue more errors for error_mark_node (i.e. an
9542 undeclared identifier) - just ignore the value expression. */
9543 if (value == error_mark_node)
9545 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9547 error_at (loc, "enumerator value for %qE is not an integer constant",
9553 if (TREE_CODE (value) != INTEGER_CST)
9555 value = c_fully_fold (value, false, NULL);
9556 if (TREE_CODE (value) == INTEGER_CST)
9557 pedwarn (loc, OPT_Wpedantic,
9558 "enumerator value for %qE is not an integer "
9559 "constant expression", name);
9561 if (TREE_CODE (value) != INTEGER_CST)
9563 error ("enumerator value for %qE is not an integer constant",
9569 value = default_conversion (value);
9570 constant_expression_warning (value);
9575 /* Default based on previous value. */
9576 /* It should no longer be possible to have NON_LVALUE_EXPR
9578 if (value == NULL_TREE)
9580 value = the_enum->enum_next_value;
9581 if (the_enum->enum_overflow)
9582 error_at (loc, "overflow in enumeration values");
9584 /* Even though the underlying type of an enum is unspecified, the
9585 type of enumeration constants is explicitly defined as int
9586 (6.4.4.3/2 in the C99 Standard). C2X allows any integer type, and
9587 GCC allows such types for older standards as an extension. */
9588 bool warned_range = false;
9589 if (!int_fits_type_p (value,
9590 (TYPE_UNSIGNED (TREE_TYPE (value))
9592 : intmax_type_node)))
9593 /* GCC does not consider its types larger than intmax_t to be
9594 extended integer types (although C2X would permit such types to
9595 be considered extended integer types if all the features
9596 required by <stdint.h> and <inttypes.h> macros, such as support
9597 for integer constants and I/O, were present), so diagnose if
9598 such a wider type is used. (If the wider type arose from a
9599 constant of such a type, that will also have been diagnosed,
9600 but this is the only diagnostic in the case where it arises
9601 from choosing a wider type automatically when adding 1
9603 warned_range = pedwarn (loc, OPT_Wpedantic,
9604 "enumerator value outside the range of %qs",
9605 (TYPE_UNSIGNED (TREE_TYPE (value))
9608 if (!warned_range && !int_fits_type_p (value, integer_type_node))
9609 pedwarn_c11 (loc, OPT_Wpedantic,
9610 "ISO C restricts enumerator values to range of %<int%> "
9613 /* The ISO C Standard mandates enumerators to have type int before
9614 C2X, even though the underlying type of an enum type is
9615 unspecified. C2X allows enumerators of any integer type. During
9616 the parsing of the enumeration, C2X specifies that constants
9617 representable in int have type int, constants not representable
9618 in int have the type of the given expression if any, and
9619 constants not representable in int and derived by adding 1 to the
9620 previous constant have the type of that constant unless the
9621 addition would overflow or wraparound, in which case a wider type
9622 of the same signedness is chosen automatically; after the
9623 enumeration is parsed, all the constants have the type of the
9624 enumeration if any do not fit in int. */
9625 if (int_fits_type_p (value, integer_type_node))
9626 value = convert (integer_type_node, value);
9628 /* Set basis for default for next value. */
9629 the_enum->enum_next_value
9630 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9631 PLUS_EXPR, value, integer_one_node, false);
9632 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9633 if (the_enum->enum_overflow)
9635 /* Choose a wider type with the same signedness if
9637 int prec = TYPE_PRECISION (TREE_TYPE (value)) + 1;
9638 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (value));
9639 tree new_type = (unsignedp
9640 ? long_unsigned_type_node
9641 : long_integer_type_node);
9642 if (prec > TYPE_PRECISION (new_type))
9643 new_type = (unsignedp
9644 ? long_long_unsigned_type_node
9645 : long_long_integer_type_node);
9646 if (prec > TYPE_PRECISION (new_type))
9647 new_type = (unsignedp
9648 ? widest_unsigned_literal_type_node
9649 : widest_integer_literal_type_node);
9650 if (prec <= TYPE_PRECISION (new_type))
9652 the_enum->enum_overflow = false;
9653 the_enum->enum_next_value
9654 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9655 PLUS_EXPR, convert (new_type, value),
9656 integer_one_node, false);
9657 gcc_assert (!tree_int_cst_lt (the_enum->enum_next_value, value));
9661 /* Now create a declaration for the enum value name. */
9663 decl = build_decl (decl_loc, CONST_DECL, name, TREE_TYPE (value));
9664 DECL_INITIAL (decl) = value;
9667 return tree_cons (decl, value, NULL_TREE);
9670 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9673 c_simulate_enum_decl (location_t loc, const char *name,
9674 vec<string_int_pair> *values_ptr)
9676 location_t saved_loc = input_location;
9677 input_location = loc;
9679 struct c_enum_contents the_enum;
9680 tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9682 tree value_chain = NULL_TREE;
9683 string_int_pair *value;
9684 vec<string_int_pair> values = *values_ptr;
9686 FOR_EACH_VEC_ELT (values, i, value)
9688 tree decl = build_enumerator (loc, loc, &the_enum,
9689 get_identifier (value->first),
9690 build_int_cst (integer_type_node,
9692 TREE_CHAIN (decl) = value_chain;
9696 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9698 input_location = saved_loc;
9702 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
9705 c_simulate_record_decl (location_t loc, const char *name,
9706 array_slice<const tree> fields)
9708 location_t saved_loc = input_location;
9709 input_location = loc;
9711 class c_struct_parse_info *struct_info;
9712 tree ident = get_identifier (name);
9713 tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
9715 for (unsigned int i = 0; i < fields.size (); ++i)
9717 DECL_FIELD_CONTEXT (fields[i]) = type;
9719 DECL_CHAIN (fields[i - 1]) = fields[i];
9722 finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
9724 tree decl = build_decl (loc, TYPE_DECL, ident, type);
9725 set_underlying_type (decl);
9726 lang_hooks.decls.pushdecl (decl);
9728 input_location = saved_loc;
9732 /* Create the FUNCTION_DECL for a function definition.
9733 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9734 the declaration; they describe the function's name and the type it returns,
9735 but twisted together in a fashion that parallels the syntax of C.
9737 This function creates a binding context for the function body
9738 as well as setting up the FUNCTION_DECL in current_function_decl.
9740 Returns true on success. If the DECLARATOR is not suitable for a function
9741 (it defines a datum instead), we return false to report a parse error. */
9744 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9747 tree decl1, old_decl;
9748 tree restype, resdecl;
9751 current_function_returns_value = 0; /* Assume, until we see it does. */
9752 current_function_returns_null = 0;
9753 current_function_returns_abnormally = 0;
9754 warn_about_return_type = 0;
9755 c_switch_stack = NULL;
9757 /* Indicate no valid break/continue context. */
9760 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9761 &attributes, NULL, NULL, DEPRECATED_NORMAL);
9762 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9764 /* If the declarator is not suitable for a function definition,
9765 cause a syntax error. */
9766 if (decl1 == NULL_TREE
9767 || TREE_CODE (decl1) != FUNCTION_DECL)
9770 loc = DECL_SOURCE_LOCATION (decl1);
9772 /* A nested function is not global. */
9773 if (current_function_decl != NULL_TREE)
9774 TREE_PUBLIC (decl1) = 0;
9776 c_decl_attributes (&decl1, attributes, 0);
9778 if (DECL_DECLARED_INLINE_P (decl1)
9779 && DECL_UNINLINABLE (decl1)
9780 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9781 warning_at (loc, OPT_Wattributes,
9782 "inline function %qD given attribute %qs",
9785 /* Handle gnu_inline attribute. */
9786 if (declspecs->inline_p
9787 && !flag_gnu89_inline
9788 && TREE_CODE (decl1) == FUNCTION_DECL
9789 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9790 || current_function_decl))
9792 if (declspecs->storage_class != csc_static)
9793 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9796 announce_function (decl1);
9798 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9800 error_at (loc, "return type is an incomplete type");
9801 /* Make it return void instead. */
9803 = build_function_type (void_type_node,
9804 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9807 if (warn_about_return_type)
9808 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9809 : (warn_return_type > 0 ? OPT_Wreturn_type
9810 : OPT_Wimplicit_int),
9811 "return type defaults to %<int%>");
9813 /* Make the init_value nonzero so pushdecl knows this is not tentative.
9814 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
9815 DECL_INITIAL (decl1) = error_mark_node;
9817 /* If this definition isn't a prototype and we had a prototype declaration
9818 before, copy the arg type info from that prototype. */
9819 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9820 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9821 old_decl = NULL_TREE;
9823 current_function_prototype_locus = UNKNOWN_LOCATION;
9824 current_function_prototype_built_in = false;
9825 current_function_prototype_arg_types = NULL_TREE;
9826 tree newtype = TREE_TYPE (decl1);
9827 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
9828 if (!prototype_p (newtype))
9830 tree oldrt = TREE_TYPE (oldtype);
9831 tree newrt = TREE_TYPE (newtype);
9832 if (old_decl != NULL_TREE
9833 && TREE_CODE (oldtype) == FUNCTION_TYPE
9834 && comptypes (oldrt, newrt))
9836 if (stdarg_p (oldtype))
9838 auto_diagnostic_group d;
9839 warning_at (loc, 0, "%q+D defined as variadic function "
9840 "without prototype", decl1);
9841 locate_old_decl (old_decl);
9843 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
9844 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9845 current_function_prototype_built_in
9846 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9847 current_function_prototype_arg_types
9848 = TYPE_ARG_TYPES (newtype);
9850 if (TREE_PUBLIC (decl1))
9852 /* If there is an external prototype declaration of this
9853 function, record its location but do not copy information
9854 to this decl. This may be an invisible declaration
9855 (built-in or in a scope which has finished) or simply
9856 have more refined argument types than any declaration
9858 struct c_binding *b;
9859 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9860 if (B_IN_SCOPE (b, external_scope))
9864 tree ext_decl, ext_type;
9866 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9867 if (TREE_CODE (ext_type) == FUNCTION_TYPE
9868 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9869 TREE_TYPE (ext_type)))
9871 current_function_prototype_locus
9872 = DECL_SOURCE_LOCATION (ext_decl);
9873 current_function_prototype_built_in
9874 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9875 current_function_prototype_arg_types
9876 = TYPE_ARG_TYPES (ext_type);
9882 /* Optionally warn of old-fashioned def with no previous prototype. */
9883 if (warn_strict_prototypes
9884 && old_decl != error_mark_node
9885 && !prototype_p (TREE_TYPE (decl1))
9886 && C_DECL_ISNT_PROTOTYPE (old_decl))
9887 warning_at (loc, OPT_Wstrict_prototypes,
9888 "function declaration isn%'t a prototype");
9889 /* Optionally warn of any global def with no previous prototype. */
9890 else if (warn_missing_prototypes
9891 && old_decl != error_mark_node
9892 && TREE_PUBLIC (decl1)
9893 && !MAIN_NAME_P (DECL_NAME (decl1))
9894 && C_DECL_ISNT_PROTOTYPE (old_decl)
9895 && !DECL_DECLARED_INLINE_P (decl1))
9896 warning_at (loc, OPT_Wmissing_prototypes,
9897 "no previous prototype for %qD", decl1);
9898 /* Optionally warn of any def with no previous prototype
9899 if the function has already been used. */
9900 else if (warn_missing_prototypes
9901 && old_decl != NULL_TREE
9902 && old_decl != error_mark_node
9903 && TREE_USED (old_decl)
9904 && !prototype_p (TREE_TYPE (old_decl)))
9905 warning_at (loc, OPT_Wmissing_prototypes,
9906 "%qD was used with no prototype before its definition", decl1);
9907 /* Optionally warn of any global def with no previous declaration. */
9908 else if (warn_missing_declarations
9909 && TREE_PUBLIC (decl1)
9910 && old_decl == NULL_TREE
9911 && !MAIN_NAME_P (DECL_NAME (decl1))
9912 && !DECL_DECLARED_INLINE_P (decl1))
9913 warning_at (loc, OPT_Wmissing_declarations,
9914 "no previous declaration for %qD",
9916 /* Optionally warn of any def with no previous declaration
9917 if the function has already been used. */
9918 else if (warn_missing_declarations
9919 && old_decl != NULL_TREE
9920 && old_decl != error_mark_node
9921 && TREE_USED (old_decl)
9922 && C_DECL_IMPLICIT (old_decl))
9923 warning_at (loc, OPT_Wmissing_declarations,
9924 "%qD was used with no declaration before its definition", decl1);
9926 /* This function exists in static storage.
9927 (This does not mean `static' in the C sense!) */
9928 TREE_STATIC (decl1) = 1;
9930 /* This is the earliest point at which we might know the assembler
9931 name of the function. Thus, if it's set before this, die horribly. */
9932 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9934 /* If #pragma weak was used, mark the decl weak now. */
9935 if (current_scope == file_scope)
9936 maybe_apply_pragma_weak (decl1);
9938 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9939 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9941 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9942 != integer_type_node)
9943 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9944 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9945 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9948 check_main_parameter_types (decl1);
9950 if (!TREE_PUBLIC (decl1))
9951 pedwarn (loc, OPT_Wmain,
9952 "%qD is normally a non-static function", decl1);
9955 tree parms = current_function_arg_info->parms;
9958 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
9959 warn_parm_array_mismatch (origloc, old_decl, parms);
9962 /* Record the decl so that the function name is defined.
9963 If we already have a decl for this name, and it is a FUNCTION_DECL,
9964 use the old decl. */
9966 current_function_decl = pushdecl (decl1);
9968 if (tree access = build_attr_access_from_parms (parms, false))
9969 decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
9973 declare_parm_level ();
9975 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9976 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9977 DECL_ARTIFICIAL (resdecl) = 1;
9978 DECL_IGNORED_P (resdecl) = 1;
9979 DECL_RESULT (current_function_decl) = resdecl;
9981 start_fname_decls ();
9986 /* Subroutine of store_parm_decls which handles new-style function
9987 definitions (prototype format). The parms already have decls, so we
9988 need only record them as in effect and complain if any redundant
9989 old-style parm decls were written. */
9991 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9997 if (current_scope->bindings)
9999 error_at (DECL_SOURCE_LOCATION (fndecl),
10000 "old-style parameter declarations in prototyped "
10001 "function definition");
10003 /* Get rid of the old-style declarations. */
10007 /* Don't issue this warning for nested functions, and don't issue this
10008 warning if we got here because ARG_INFO_TYPES was error_mark_node
10009 (this happens when a function definition has just an ellipsis in
10010 its parameter list). */
10011 else if (!in_system_header_at (input_location)
10012 && !current_function_scope
10013 && arg_info->types != error_mark_node)
10014 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
10015 "traditional C rejects ISO C style function definitions");
10017 /* Now make all the parameter declarations visible in the function body.
10018 We can bypass most of the grunt work of pushdecl. */
10019 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
10021 DECL_CONTEXT (decl) = current_function_decl;
10022 if (DECL_NAME (decl))
10024 bind (DECL_NAME (decl), decl, current_scope,
10025 /*invisible=*/false, /*nested=*/false,
10027 if (!TREE_USED (decl))
10028 warn_if_shadowing (decl);
10031 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
10032 "ISO C does not support omitting parameter names in "
10033 "function definitions before C2X");
10036 /* Record the parameter list in the function declaration. */
10037 DECL_ARGUMENTS (fndecl) = arg_info->parms;
10039 /* Now make all the ancillary declarations visible, likewise. */
10040 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
10042 DECL_CONTEXT (decl) = current_function_decl;
10043 if (DECL_NAME (decl))
10044 bind (DECL_NAME (decl), decl, current_scope,
10045 /*invisible=*/false,
10046 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
10050 /* And all the tag declarations. */
10051 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
10053 bind (tag->id, tag->type, current_scope,
10054 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
10057 /* Subroutine of store_parm_decls which handles old-style function
10058 definitions (separate parameter list and declarations). */
10061 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
10063 struct c_binding *b;
10064 tree parm, decl, last;
10065 tree parmids = arg_info->parms;
10066 hash_set<tree> seen_args;
10068 if (!in_system_header_at (input_location))
10071 pedwarn (DECL_SOURCE_LOCATION (fndecl),
10072 OPT_Wold_style_definition, "old-style function definition");
10074 warning_at (DECL_SOURCE_LOCATION (fndecl),
10075 OPT_Wold_style_definition,
10076 "old-style function definition");
10079 if (current_scope->had_vla_unspec)
10080 error ("%<[*]%> not allowed in other than function prototype scope");
10082 /* Match each formal parameter name with its declaration. Save each
10083 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
10084 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10086 if (TREE_VALUE (parm) == NULL_TREE)
10088 error_at (DECL_SOURCE_LOCATION (fndecl),
10089 "parameter name missing from parameter list");
10090 TREE_PURPOSE (parm) = NULL_TREE;
10094 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
10095 if (b && B_IN_CURRENT_SCOPE (b))
10098 /* Skip erroneous parameters. */
10099 if (decl == error_mark_node)
10101 /* If we got something other than a PARM_DECL it is an error. */
10102 if (TREE_CODE (decl) != PARM_DECL)
10104 error_at (DECL_SOURCE_LOCATION (decl),
10105 "%qD declared as a non-parameter", decl);
10108 /* If the declaration is already marked, we have a duplicate
10109 name. Complain and ignore the duplicate. */
10110 else if (seen_args.contains (decl))
10112 error_at (DECL_SOURCE_LOCATION (decl),
10113 "multiple parameters named %qD", decl);
10114 TREE_PURPOSE (parm) = NULL_TREE;
10117 /* If the declaration says "void", complain and turn it into
10119 else if (VOID_TYPE_P (TREE_TYPE (decl)))
10121 error_at (DECL_SOURCE_LOCATION (decl),
10122 "parameter %qD declared with void type", decl);
10123 TREE_TYPE (decl) = integer_type_node;
10124 DECL_ARG_TYPE (decl) = integer_type_node;
10125 layout_decl (decl, 0);
10127 warn_if_shadowing (decl);
10129 /* If no declaration found, default to int. */
10132 /* FIXME diagnostics: This should be the location of the argument,
10133 not the FNDECL. E.g., for an old-style declaration
10135 int f10(v) { blah; }
10137 We should use the location of the V, not the F10.
10138 Unfortunately, the V is an IDENTIFIER_NODE which has no
10139 location. In the future we need locations for c_arg_info
10142 See gcc.dg/Wshadow-3.c for an example of this problem. */
10143 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
10144 PARM_DECL, TREE_VALUE (parm), integer_type_node);
10145 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
10147 warn_if_shadowing (decl);
10150 pedwarn (DECL_SOURCE_LOCATION (decl),
10151 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
10154 warning_at (DECL_SOURCE_LOCATION (decl),
10155 OPT_Wmissing_parameter_type,
10156 "type of %qD defaults to %<int%>", decl);
10159 TREE_PURPOSE (parm) = decl;
10160 seen_args.add (decl);
10163 /* Now examine the parms chain for incomplete declarations
10164 and declarations with no corresponding names. */
10166 for (b = current_scope->bindings; b; b = b->prev)
10169 if (TREE_CODE (parm) != PARM_DECL)
10172 if (TREE_TYPE (parm) != error_mark_node
10173 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
10175 error_at (DECL_SOURCE_LOCATION (parm),
10176 "parameter %qD has incomplete type", parm);
10177 TREE_TYPE (parm) = error_mark_node;
10180 if (!seen_args.contains (parm))
10182 error_at (DECL_SOURCE_LOCATION (parm),
10183 "declaration for parameter %qD but no such parameter",
10186 /* Pretend the parameter was not missing.
10187 This gets us to a standard state and minimizes
10188 further error messages. */
10189 parmids = chainon (parmids, tree_cons (parm, 0, 0));
10193 /* Chain the declarations together in the order of the list of
10194 names. Store that chain in the function decl, replacing the
10195 list of names. Update the current scope to match. */
10196 DECL_ARGUMENTS (fndecl) = NULL_TREE;
10198 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
10199 if (TREE_PURPOSE (parm))
10201 if (parm && TREE_PURPOSE (parm))
10203 last = TREE_PURPOSE (parm);
10204 DECL_ARGUMENTS (fndecl) = last;
10206 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
10207 if (TREE_PURPOSE (parm))
10209 DECL_CHAIN (last) = TREE_PURPOSE (parm);
10210 last = TREE_PURPOSE (parm);
10212 DECL_CHAIN (last) = NULL_TREE;
10215 /* If there was a previous prototype,
10216 set the DECL_ARG_TYPE of each argument according to
10217 the type previously specified, and report any mismatches. */
10219 if (current_function_prototype_arg_types)
10222 for (parm = DECL_ARGUMENTS (fndecl),
10223 type = current_function_prototype_arg_types;
10224 parm || (type != NULL_TREE
10225 && TREE_VALUE (type) != error_mark_node
10226 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10227 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10229 if (parm == NULL_TREE
10230 || type == NULL_TREE
10231 || (TREE_VALUE (type) != error_mark_node
10232 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10234 if (current_function_prototype_built_in)
10235 warning_at (DECL_SOURCE_LOCATION (fndecl),
10236 0, "number of arguments doesn%'t match "
10237 "built-in prototype");
10240 /* FIXME diagnostics: This should be the location of
10241 FNDECL, but there is bug when a prototype is
10242 declared inside function context, but defined
10243 outside of it (e.g., gcc.dg/pr15698-2.c). In
10244 which case FNDECL gets the location of the
10245 prototype, not the definition. */
10246 error_at (input_location,
10247 "number of arguments doesn%'t match prototype");
10249 error_at (current_function_prototype_locus,
10250 "prototype declaration");
10254 /* Type for passing arg must be consistent with that
10255 declared for the arg. ISO C says we take the unqualified
10256 type for parameters declared with qualified type. */
10257 if (TREE_TYPE (parm) != error_mark_node
10258 && TREE_VALUE (type) != error_mark_node
10259 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10260 != TYPE_ATOMIC (TREE_VALUE (type)))
10261 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10262 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10264 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10265 == TYPE_ATOMIC (TREE_VALUE (type)))
10266 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10267 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10269 /* Adjust argument to match prototype. E.g. a previous
10270 `int foo(float);' prototype causes
10271 `int foo(x) float x; {...}' to be treated like
10272 `int foo(float x) {...}'. This is particularly
10273 useful for argument types like uid_t. */
10274 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10276 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10277 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10278 && (TYPE_PRECISION (TREE_TYPE (parm))
10279 < TYPE_PRECISION (integer_type_node)))
10280 DECL_ARG_TYPE (parm)
10281 = c_type_promotes_to (TREE_TYPE (parm));
10283 /* ??? Is it possible to get here with a
10284 built-in prototype or will it always have
10285 been diagnosed as conflicting with an
10286 old-style definition and discarded? */
10287 if (current_function_prototype_built_in)
10288 warning_at (DECL_SOURCE_LOCATION (parm),
10289 OPT_Wpedantic, "promoted argument %qD "
10290 "doesn%'t match built-in prototype", parm);
10293 pedwarn (DECL_SOURCE_LOCATION (parm),
10294 OPT_Wpedantic, "promoted argument %qD "
10295 "doesn%'t match prototype", parm);
10296 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10297 "prototype declaration");
10302 if (current_function_prototype_built_in)
10303 warning_at (DECL_SOURCE_LOCATION (parm),
10304 0, "argument %qD doesn%'t match "
10305 "built-in prototype", parm);
10308 error_at (DECL_SOURCE_LOCATION (parm),
10309 "argument %qD doesn%'t match prototype", parm);
10310 error_at (current_function_prototype_locus,
10311 "prototype declaration");
10316 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10319 /* Otherwise, create a prototype that would match. */
10323 tree actual = NULL_TREE, last = NULL_TREE, type;
10325 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10327 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10329 TREE_CHAIN (last) = type;
10334 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10336 TREE_CHAIN (last) = type;
10340 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10341 of the type of this function, but we need to avoid having this
10342 affect the types of other similarly-typed functions, so we must
10343 first force the generation of an identical (but separate) type
10344 node for the relevant function type. The new node we create
10345 will be a variant of the main variant of the original function
10348 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10350 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10354 /* Store parameter declarations passed in ARG_INFO into the current
10355 function declaration. */
10358 store_parm_decls_from (struct c_arg_info *arg_info)
10360 current_function_arg_info = arg_info;
10361 store_parm_decls ();
10364 /* Called by walk_tree to look for and update context-less labels
10365 or labels with context in the parent function. */
10368 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10370 tree ctx = static_cast<tree>(data);
10371 if (TREE_CODE (*tp) == LABEL_EXPR
10372 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10373 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10375 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10376 *walk_subtrees = 0;
10382 /* Store the parameter declarations into the current function declaration.
10383 This is called after parsing the parameter declarations, before
10384 digesting the body of the function.
10386 For an old-style definition, construct a prototype out of the old-style
10387 parameter declarations and inject it into the function's type. */
10390 store_parm_decls (void)
10392 tree fndecl = current_function_decl;
10395 /* The argument information block for FNDECL. */
10396 struct c_arg_info *arg_info = current_function_arg_info;
10397 current_function_arg_info = 0;
10399 /* True if this definition is written with a prototype. In C2X, an
10400 empty argument list was converted to (void) in grokparms; in
10401 older C standard versions, it does not give the function a type
10402 with a prototype for future calls. */
10403 proto = arg_info->types != 0;
10406 store_parm_decls_newstyle (fndecl, arg_info);
10408 store_parm_decls_oldstyle (fndecl, arg_info);
10410 /* The next call to push_scope will be a function body. */
10412 next_is_function_body = true;
10414 /* Write a record describing this function definition to the prototypes
10415 file (if requested). */
10417 gen_aux_info_record (fndecl, 1, 0, proto);
10419 /* Initialize the RTL code for the function. */
10420 allocate_struct_function (fndecl, false);
10422 if (warn_unused_local_typedefs)
10423 cfun->language = ggc_cleared_alloc<language_function> ();
10425 /* Begin the statement tree for this function. */
10426 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10428 /* ??? Insert the contents of the pending sizes list into the function
10429 to be evaluated. The only reason left to have this is
10430 void foo(int n, int array[n++])
10431 because we throw away the array type in favor of a pointer type, and
10432 thus won't naturally see the SAVE_EXPR containing the increment. All
10433 other pending sizes would be handled by gimplify_parameters. */
10434 if (arg_info->pending_sizes)
10436 /* In very special circumstances, e.g. for code like
10438 void f (int a[i += 2]) {}
10439 we need to execute the atomic assignment on function entry.
10440 But in this case, it is not just a straight store, it has the
10441 op= form, which means that build_atomic_assign has generated
10442 gotos, labels, etc. Because at that time the function decl
10443 for F has not been created yet, those labels do not have any
10444 function context. But we have the fndecl now, so update the
10445 labels accordingly. gimplify_expr would crash otherwise.
10446 Or with nested functions the labels could be created with parent
10447 function's context, while when the statement is emitted at the
10448 start of the nested function, it needs the nested function's
10450 walk_tree_without_duplicates (&arg_info->pending_sizes,
10451 set_labels_context_r, fndecl);
10452 add_stmt (arg_info->pending_sizes);
10456 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
10457 c_finish_omp_declare_simd for function prototypes. No diagnostics
10461 temp_store_parm_decls (tree fndecl, tree parms)
10464 for (tree p = parms; p; p = DECL_CHAIN (p))
10466 DECL_CONTEXT (p) = fndecl;
10468 bind (DECL_NAME (p), p, current_scope,
10469 /*invisible=*/false, /*nested=*/false,
10474 /* Undo what temp_store_parm_decls did. */
10477 temp_pop_parm_decls (void)
10479 /* Clear all bindings in this temporary scope, so that
10480 pop_scope doesn't create a BLOCK. */
10481 struct c_binding *b = current_scope->bindings;
10482 current_scope->bindings = NULL;
10483 for (; b; b = free_binding_and_advance (b))
10485 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10486 || b->decl == error_mark_node);
10487 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10488 I_SYMBOL_BINDING (b->id) = b->shadowed;
10489 if (b->shadowed && b->shadowed->u.type)
10490 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10496 /* Finish up a function declaration and compile that function
10497 all the way to assembler language output. Then free the storage
10498 for the function definition.
10500 This is called after parsing the body of the function definition. */
10503 finish_function (location_t end_loc)
10505 tree fndecl = current_function_decl;
10507 if (c_dialect_objc ())
10508 objc_finish_function ();
10510 if (TREE_CODE (fndecl) == FUNCTION_DECL
10511 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10513 tree args = DECL_ARGUMENTS (fndecl);
10514 for (; args; args = DECL_CHAIN (args))
10516 tree type = TREE_TYPE (args);
10517 if (INTEGRAL_TYPE_P (type)
10518 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10519 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10523 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10524 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10526 /* Must mark the RESULT_DECL as being in this function. */
10528 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10529 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10531 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
10532 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10533 == integer_type_node && flag_isoc99)
10535 /* Hack. We don't want the middle-end to warn that this return
10536 is unreachable, so we mark its location as special. Using
10537 UNKNOWN_LOCATION has the problem that it gets clobbered in
10538 annotate_one_with_locus. A cleaner solution might be to
10539 ensure ! should_carry_locus_p (stmt), but that needs a flag.
10541 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10544 /* Tie off the statement tree for this function. */
10545 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10547 finish_fname_decls ();
10549 /* Complain if there's no return statement only if option specified on
10551 if (warn_return_type > 0
10552 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10553 && !current_function_returns_value && !current_function_returns_null
10554 /* Don't complain if we are no-return. */
10555 && !current_function_returns_abnormally
10556 /* Don't complain if we are declared noreturn. */
10557 && !TREE_THIS_VOLATILE (fndecl)
10558 /* Don't warn for main(). */
10559 && !MAIN_NAME_P (DECL_NAME (fndecl))
10560 /* Or if they didn't actually specify a return type. */
10561 && !C_FUNCTION_IMPLICIT_INT (fndecl)
10562 /* Normally, with -Wreturn-type, flow will complain, but we might
10563 optimize out static functions. */
10564 && !TREE_PUBLIC (fndecl)
10565 && targetm.warn_func_return (fndecl)
10566 && warning (OPT_Wreturn_type,
10567 "no return statement in function returning non-void"))
10568 suppress_warning (fndecl, OPT_Wreturn_type);
10570 /* Complain about parameters that are only set, but never otherwise used. */
10571 if (warn_unused_but_set_parameter)
10575 for (decl = DECL_ARGUMENTS (fndecl);
10577 decl = DECL_CHAIN (decl))
10578 if (TREE_USED (decl)
10579 && TREE_CODE (decl) == PARM_DECL
10580 && !DECL_READ_P (decl)
10581 && DECL_NAME (decl)
10582 && !DECL_ARTIFICIAL (decl)
10583 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
10584 warning_at (DECL_SOURCE_LOCATION (decl),
10585 OPT_Wunused_but_set_parameter,
10586 "parameter %qD set but not used", decl);
10589 /* Complain about locally defined typedefs that are not used in this
10591 maybe_warn_unused_local_typedefs ();
10593 /* Possibly warn about unused parameters. */
10594 if (warn_unused_parameter)
10595 do_warn_unused_parameter (fndecl);
10597 /* Store the end of the function, so that we get good line number
10598 info for the epilogue. */
10599 cfun->function_end_locus = end_loc;
10601 /* Finalize the ELF visibility for the function. */
10602 c_determine_visibility (fndecl);
10604 /* For GNU C extern inline functions disregard inline limits. */
10605 if (DECL_EXTERNAL (fndecl)
10606 && DECL_DECLARED_INLINE_P (fndecl)
10607 && (flag_gnu89_inline
10608 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10609 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10611 /* Genericize before inlining. Delay genericizing nested functions
10612 until their parent function is genericized. Since finalizing
10613 requires GENERIC, delay that as well. */
10615 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10616 && !undef_nested_function)
10618 if (!decl_function_context (fndecl))
10620 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10621 c_genericize (fndecl);
10623 /* ??? Objc emits functions after finalizing the compilation unit.
10624 This should be cleaned up later and this conditional removed. */
10625 if (symtab->global_info_ready)
10627 cgraph_node::add_new_function (fndecl, false);
10630 cgraph_node::finalize_function (fndecl, false);
10634 /* Register this function with cgraph just far enough to get it
10635 added to our parent's nested function list. Handy, since the
10636 C front end doesn't have such a list. */
10637 (void) cgraph_node::get_create (fndecl);
10641 if (!decl_function_context (fndecl))
10642 undef_nested_function = false;
10644 if (cfun->language != NULL)
10646 ggc_free (cfun->language);
10647 cfun->language = NULL;
10650 /* We're leaving the context of this function, so zap cfun.
10651 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10652 tree_rest_of_compilation. */
10654 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10655 current_function_decl = NULL;
10658 /* Check the declarations given in a for-loop for satisfying the C99
10659 constraints. If exactly one such decl is found, return it. LOC is
10660 the location of the opening parenthesis of the for loop. The last
10661 parameter allows you to control the "for loop initial declarations
10662 are only allowed in C99 mode". Normally, you should pass
10663 flag_isoc99 as that parameter. But in some cases (Objective-C
10664 foreach loop, for example) we want to run the checks in this
10665 function even if not in C99 mode, so we allow the caller to turn
10666 off the error about not being in C99 mode.
10670 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10672 struct c_binding *b;
10673 tree one_decl = NULL_TREE;
10676 if (!turn_off_iso_c99_error)
10678 static bool hint = true;
10679 /* If we get here, declarations have been used in a for loop without
10680 the C99 for loop scope. This doesn't make much sense, so don't
10682 auto_diagnostic_group d;
10683 error_at (loc, "%<for%> loop initial declarations "
10684 "are only allowed in C99 or C11 mode");
10688 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
10689 "%<-std=gnu11%> to compile your code");
10695 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10696 "initial declarations");
10698 /* C99 subclause 6.8.5 paragraph 3:
10700 [#3] The declaration part of a for statement shall only
10701 declare identifiers for objects having storage class auto or
10704 It isn't clear whether, in this sentence, "identifiers" binds to
10705 "shall only declare" or to "objects" - that is, whether all identifiers
10706 declared must be identifiers for objects, or whether the restriction
10707 only applies to those that are. (A question on this in comp.std.c
10708 in November 2000 received no answer.) We implement the strictest
10709 interpretation, to avoid creating an extension which later causes
10712 for (b = current_scope->bindings; b; b = b->prev)
10715 tree decl = b->decl;
10720 switch (TREE_CODE (decl))
10724 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10725 if (TREE_STATIC (decl))
10726 error_at (decl_loc,
10727 "declaration of static variable %qD in %<for%> loop "
10728 "initial declaration", decl);
10729 else if (DECL_EXTERNAL (decl))
10730 error_at (decl_loc,
10731 "declaration of %<extern%> variable %qD in %<for%> loop "
10732 "initial declaration", decl);
10738 "%<struct %E%> declared in %<for%> loop initial "
10739 "declaration", id);
10743 "%<union %E%> declared in %<for%> loop initial declaration",
10746 case ENUMERAL_TYPE:
10747 error_at (loc, "%<enum %E%> declared in %<for%> loop "
10748 "initial declaration", id);
10751 error_at (loc, "declaration of non-variable "
10752 "%qD in %<for%> loop initial declaration", decl);
10759 return n_decls == 1 ? one_decl : NULL_TREE;
10762 /* Save and reinitialize the variables
10763 used during compilation of a C function. */
10766 c_push_function_context (void)
10768 struct language_function *p = cfun->language;
10769 /* cfun->language might have been already allocated by the use of
10770 -Wunused-local-typedefs. In that case, just re-use it. */
10772 cfun->language = p = ggc_cleared_alloc<language_function> ();
10774 p->base.x_stmt_tree = c_stmt_tree;
10775 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10776 p->x_in_statement = in_statement;
10777 p->x_switch_stack = c_switch_stack;
10778 p->arg_info = current_function_arg_info;
10779 p->returns_value = current_function_returns_value;
10780 p->returns_null = current_function_returns_null;
10781 p->returns_abnormally = current_function_returns_abnormally;
10782 p->warn_about_return_type = warn_about_return_type;
10784 push_function_context ();
10787 /* Restore the variables used during compilation of a C function. */
10790 c_pop_function_context (void)
10792 struct language_function *p;
10794 pop_function_context ();
10795 p = cfun->language;
10797 /* When -Wunused-local-typedefs is in effect, cfun->languages is
10798 used to store data throughout the life time of the current cfun,
10799 So don't deallocate it. */
10800 if (!warn_unused_local_typedefs)
10801 cfun->language = NULL;
10803 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10804 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10806 /* Stop pointing to the local nodes about to be freed. */
10807 /* But DECL_INITIAL must remain nonzero so we know this
10808 was an actual function definition. */
10809 DECL_INITIAL (current_function_decl) = error_mark_node;
10810 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10813 c_stmt_tree = p->base.x_stmt_tree;
10814 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10815 in_statement = p->x_in_statement;
10816 c_switch_stack = p->x_switch_stack;
10817 current_function_arg_info = p->arg_info;
10818 current_function_returns_value = p->returns_value;
10819 current_function_returns_null = p->returns_null;
10820 current_function_returns_abnormally = p->returns_abnormally;
10821 warn_about_return_type = p->warn_about_return_type;
10824 /* The functions below are required for functionality of doing
10825 function at once processing in the C front end. Currently these
10826 functions are not called from anywhere in the C front end, but as
10827 these changes continue, that will change. */
10829 /* Returns the stmt_tree (if any) to which statements are currently
10830 being added. If there is no active statement-tree, NULL is
10834 current_stmt_tree (void)
10836 return &c_stmt_tree;
10839 /* Return the global value of T as a symbol. */
10842 identifier_global_value (tree t)
10844 struct c_binding *b;
10846 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10847 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10853 /* Return the global value of tag T as a symbol. */
10856 identifier_global_tag (tree t)
10858 struct c_binding *b;
10860 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10861 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10867 /* Returns true if NAME refers to a built-in function or function-like
10871 names_builtin_p (const char *name)
10873 tree id = get_identifier (name);
10874 if (tree decl = identifier_global_value (id))
10875 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
10877 /* Also detect common reserved C words that aren't strictly built-in
10879 switch (C_RID_CODE (id))
10881 case RID_BUILTIN_CONVERTVECTOR:
10882 case RID_BUILTIN_HAS_ATTRIBUTE:
10883 case RID_BUILTIN_SHUFFLE:
10884 case RID_BUILTIN_SHUFFLEVECTOR:
10885 case RID_BUILTIN_ASSOC_BARRIER:
10886 case RID_CHOOSE_EXPR:
10888 case RID_TYPES_COMPATIBLE_P:
10897 /* In C, the only C-linkage public declaration is at file scope. */
10900 c_linkage_bindings (tree name)
10902 return identifier_global_value (name);
10905 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
10906 otherwise the name is found in ridpointers from RID_INDEX. */
10909 record_builtin_type (enum rid rid_index, const char *name, tree type)
10913 id = ridpointers[(int) rid_index];
10915 id = get_identifier (name);
10916 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10918 if (debug_hooks->type_decl)
10919 debug_hooks->type_decl (decl, false);
10922 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10925 build_c_parm (struct c_declspecs *specs, tree attrs,
10926 struct c_declarator *declarator,
10929 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10930 ret->specs = specs;
10931 ret->attrs = attrs;
10932 ret->declarator = declarator;
10937 /* Return a declarator with nested attributes. TARGET is the inner
10938 declarator to which these attributes apply. ATTRS are the
10941 struct c_declarator *
10942 build_attrs_declarator (tree attrs, struct c_declarator *target)
10944 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10945 ret->kind = cdk_attrs;
10946 ret->declarator = target;
10947 ret->u.attrs = attrs;
10951 /* Return a declarator for a function with arguments specified by ARGS
10952 and return type specified by TARGET. */
10954 struct c_declarator *
10955 build_function_declarator (struct c_arg_info *args,
10956 struct c_declarator *target)
10958 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10959 ret->kind = cdk_function;
10960 ret->declarator = target;
10961 ret->u.arg_info = args;
10965 /* Return a declarator for the identifier IDENT (which may be
10966 NULL_TREE for an abstract declarator). */
10968 struct c_declarator *
10969 build_id_declarator (tree ident)
10971 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10972 ret->kind = cdk_id;
10973 ret->declarator = 0;
10974 ret->u.id.id = ident;
10975 ret->u.id.attrs = NULL_TREE;
10976 /* Default value - may get reset to a more precise location. */
10977 ret->id_loc = input_location;
10981 /* Return something to represent absolute declarators containing a *.
10982 TARGET is the absolute declarator that the * contains.
10983 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10984 to apply to the pointer type. */
10986 struct c_declarator *
10987 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10988 struct c_declarator *target)
10992 struct c_declarator *itarget = target;
10993 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10994 if (type_quals_attrs)
10996 attrs = type_quals_attrs->attrs;
10997 quals = quals_from_declspecs (type_quals_attrs);
10998 if (attrs != NULL_TREE)
10999 itarget = build_attrs_declarator (attrs, target);
11001 ret->kind = cdk_pointer;
11002 ret->declarator = itarget;
11003 ret->u.pointer_quals = quals;
11007 /* Return a pointer to a structure for an empty list of declaration
11010 struct c_declspecs *
11011 build_null_declspecs (void)
11013 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
11014 memset (ret, 0, sizeof *ret);
11015 ret->align_log = -1;
11016 ret->typespec_word = cts_none;
11017 ret->storage_class = csc_none;
11018 ret->expr_const_operands = true;
11019 ret->typespec_kind = ctsk_none;
11020 ret->address_space = ADDR_SPACE_GENERIC;
11024 /* Add the address space ADDRSPACE to the declaration specifiers
11025 SPECS, returning SPECS. */
11027 struct c_declspecs *
11028 declspecs_add_addrspace (location_t location,
11029 struct c_declspecs *specs, addr_space_t as)
11031 specs->non_sc_seen_p = true;
11032 specs->declspecs_seen_p = true;
11033 specs->non_std_attrs_seen_p = true;
11035 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
11036 && specs->address_space != as)
11037 error ("incompatible address space qualifiers %qs and %qs",
11038 c_addr_space_name (as),
11039 c_addr_space_name (specs->address_space));
11042 specs->address_space = as;
11043 specs->locations[cdw_address_space] = location;
11048 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
11049 returning SPECS. */
11051 struct c_declspecs *
11052 declspecs_add_qual (location_t loc,
11053 struct c_declspecs *specs, tree qual)
11057 specs->non_sc_seen_p = true;
11058 specs->declspecs_seen_p = true;
11059 specs->non_std_attrs_seen_p = true;
11060 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
11061 && C_IS_RESERVED_WORD (qual));
11062 i = C_RID_CODE (qual);
11063 location_t prev_loc = UNKNOWN_LOCATION;
11067 dupe = specs->const_p;
11068 specs->const_p = true;
11069 prev_loc = specs->locations[cdw_const];
11070 specs->locations[cdw_const] = loc;
11073 dupe = specs->volatile_p;
11074 specs->volatile_p = true;
11075 prev_loc = specs->locations[cdw_volatile];
11076 specs->locations[cdw_volatile] = loc;
11079 dupe = specs->restrict_p;
11080 specs->restrict_p = true;
11081 prev_loc = specs->locations[cdw_restrict];
11082 specs->locations[cdw_restrict] = loc;
11085 dupe = specs->atomic_p;
11086 specs->atomic_p = true;
11087 prev_loc = specs->locations[cdw_atomic];
11088 specs->locations[cdw_atomic] = loc;
11091 gcc_unreachable ();
11095 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
11096 "duplicate %qE declaration specifier", qual);
11098 && warn_duplicate_decl_specifier
11099 && prev_loc >= RESERVED_LOCATION_COUNT
11100 && !from_macro_expansion_at (prev_loc)
11101 && !from_macro_expansion_at (loc))
11102 warning_at (loc, OPT_Wduplicate_decl_specifier,
11103 "duplicate %qE declaration specifier", qual);
11108 /* Add the type specifier TYPE to the declaration specifiers SPECS,
11109 returning SPECS. */
11111 struct c_declspecs *
11112 declspecs_add_type (location_t loc, struct c_declspecs *specs,
11113 struct c_typespec spec)
11115 tree type = spec.spec;
11116 specs->non_sc_seen_p = true;
11117 specs->declspecs_seen_p = true;
11118 specs->non_std_attrs_seen_p = true;
11119 specs->typespec_kind = spec.kind;
11120 if (TREE_DEPRECATED (type))
11121 specs->deprecated_p = true;
11122 if (TREE_UNAVAILABLE (type))
11123 specs->unavailable_p = true;
11125 /* Handle type specifier keywords. */
11126 if (TREE_CODE (type) == IDENTIFIER_NODE
11127 && C_IS_RESERVED_WORD (type)
11128 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
11130 enum rid i = C_RID_CODE (type);
11133 error_at (loc, "two or more data types in declaration specifiers");
11136 if ((int) i <= (int) RID_LAST_MODIFIER)
11138 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
11143 if (specs->long_long_p)
11145 error_at (loc, "%<long long long%> is too long for GCC");
11150 if (specs->typespec_word == cts_double)
11153 ("both %<long long%> and %<double%> in "
11154 "declaration specifiers"));
11157 pedwarn_c90 (loc, OPT_Wlong_long,
11158 "ISO C90 does not support %<long long%>");
11159 specs->long_long_p = 1;
11160 specs->locations[cdw_long_long] = loc;
11163 if (specs->short_p)
11165 ("both %<long%> and %<short%> in "
11166 "declaration specifiers"));
11167 else if (specs->typespec_word == cts_auto_type)
11169 ("both %<long%> and %<__auto_type%> in "
11170 "declaration specifiers"));
11171 else if (specs->typespec_word == cts_void)
11173 ("both %<long%> and %<void%> in "
11174 "declaration specifiers"));
11175 else if (specs->typespec_word == cts_int_n)
11177 ("both %<long%> and %<__int%d%> in "
11178 "declaration specifiers"),
11179 int_n_data[specs->int_n_idx].bitsize);
11180 else if (specs->typespec_word == cts_bool)
11182 ("both %<long%> and %<_Bool%> in "
11183 "declaration specifiers"));
11184 else if (specs->typespec_word == cts_char)
11186 ("both %<long%> and %<char%> in "
11187 "declaration specifiers"));
11188 else if (specs->typespec_word == cts_float)
11190 ("both %<long%> and %<float%> in "
11191 "declaration specifiers"));
11192 else if (specs->typespec_word == cts_floatn_nx)
11194 ("both %<long%> and %<_Float%d%s%> in "
11195 "declaration specifiers"),
11196 floatn_nx_types[specs->floatn_nx_idx].n,
11197 (floatn_nx_types[specs->floatn_nx_idx].extended
11200 else if (specs->typespec_word == cts_dfloat32)
11202 ("both %<long%> and %<_Decimal32%> in "
11203 "declaration specifiers"));
11204 else if (specs->typespec_word == cts_dfloat64)
11206 ("both %<long%> and %<_Decimal64%> in "
11207 "declaration specifiers"));
11208 else if (specs->typespec_word == cts_dfloat128)
11210 ("both %<long%> and %<_Decimal128%> in "
11211 "declaration specifiers"));
11214 specs->long_p = true;
11215 specs->locations[cdw_long] = loc;
11219 dupe = specs->short_p;
11222 ("both %<long%> and %<short%> in "
11223 "declaration specifiers"));
11224 else if (specs->typespec_word == cts_auto_type)
11226 ("both %<short%> and %<__auto_type%> in "
11227 "declaration specifiers"));
11228 else if (specs->typespec_word == cts_void)
11230 ("both %<short%> and %<void%> in "
11231 "declaration specifiers"));
11232 else if (specs->typespec_word == cts_int_n)
11234 ("both %<short%> and %<__int%d%> in "
11235 "declaration specifiers"),
11236 int_n_data[specs->int_n_idx].bitsize);
11237 else if (specs->typespec_word == cts_bool)
11239 ("both %<short%> and %<_Bool%> in "
11240 "declaration specifiers"));
11241 else if (specs->typespec_word == cts_char)
11243 ("both %<short%> and %<char%> in "
11244 "declaration specifiers"));
11245 else if (specs->typespec_word == cts_float)
11247 ("both %<short%> and %<float%> in "
11248 "declaration specifiers"));
11249 else if (specs->typespec_word == cts_double)
11251 ("both %<short%> and %<double%> in "
11252 "declaration specifiers"));
11253 else if (specs->typespec_word == cts_floatn_nx)
11255 ("both %<short%> and %<_Float%d%s%> in "
11256 "declaration specifiers"),
11257 floatn_nx_types[specs->floatn_nx_idx].n,
11258 (floatn_nx_types[specs->floatn_nx_idx].extended
11261 else if (specs->typespec_word == cts_dfloat32)
11263 ("both %<short%> and %<_Decimal32%> in "
11264 "declaration specifiers"));
11265 else if (specs->typespec_word == cts_dfloat64)
11267 ("both %<short%> and %<_Decimal64%> in "
11268 "declaration specifiers"));
11269 else if (specs->typespec_word == cts_dfloat128)
11271 ("both %<short%> and %<_Decimal128%> in "
11272 "declaration specifiers"));
11275 specs->short_p = true;
11276 specs->locations[cdw_short] = loc;
11280 dupe = specs->signed_p;
11281 if (specs->unsigned_p)
11283 ("both %<signed%> and %<unsigned%> in "
11284 "declaration specifiers"));
11285 else if (specs->typespec_word == cts_auto_type)
11287 ("both %<signed%> and %<__auto_type%> in "
11288 "declaration specifiers"));
11289 else if (specs->typespec_word == cts_void)
11291 ("both %<signed%> and %<void%> in "
11292 "declaration specifiers"));
11293 else if (specs->typespec_word == cts_bool)
11295 ("both %<signed%> and %<_Bool%> in "
11296 "declaration specifiers"));
11297 else if (specs->typespec_word == cts_float)
11299 ("both %<signed%> and %<float%> in "
11300 "declaration specifiers"));
11301 else if (specs->typespec_word == cts_double)
11303 ("both %<signed%> and %<double%> in "
11304 "declaration specifiers"));
11305 else if (specs->typespec_word == cts_floatn_nx)
11307 ("both %<signed%> and %<_Float%d%s%> in "
11308 "declaration specifiers"),
11309 floatn_nx_types[specs->floatn_nx_idx].n,
11310 (floatn_nx_types[specs->floatn_nx_idx].extended
11313 else if (specs->typespec_word == cts_dfloat32)
11315 ("both %<signed%> and %<_Decimal32%> in "
11316 "declaration specifiers"));
11317 else if (specs->typespec_word == cts_dfloat64)
11319 ("both %<signed%> and %<_Decimal64%> in "
11320 "declaration specifiers"));
11321 else if (specs->typespec_word == cts_dfloat128)
11323 ("both %<signed%> and %<_Decimal128%> in "
11324 "declaration specifiers"));
11327 specs->signed_p = true;
11328 specs->locations[cdw_signed] = loc;
11332 dupe = specs->unsigned_p;
11333 if (specs->signed_p)
11335 ("both %<signed%> and %<unsigned%> in "
11336 "declaration specifiers"));
11337 else if (specs->typespec_word == cts_auto_type)
11339 ("both %<unsigned%> and %<__auto_type%> in "
11340 "declaration specifiers"));
11341 else if (specs->typespec_word == cts_void)
11343 ("both %<unsigned%> and %<void%> in "
11344 "declaration specifiers"));
11345 else if (specs->typespec_word == cts_bool)
11347 ("both %<unsigned%> and %<_Bool%> in "
11348 "declaration specifiers"));
11349 else if (specs->typespec_word == cts_float)
11351 ("both %<unsigned%> and %<float%> in "
11352 "declaration specifiers"));
11353 else if (specs->typespec_word == cts_double)
11355 ("both %<unsigned%> and %<double%> in "
11356 "declaration specifiers"));
11357 else if (specs->typespec_word == cts_floatn_nx)
11359 ("both %<unsigned%> and %<_Float%d%s%> in "
11360 "declaration specifiers"),
11361 floatn_nx_types[specs->floatn_nx_idx].n,
11362 (floatn_nx_types[specs->floatn_nx_idx].extended
11365 else if (specs->typespec_word == cts_dfloat32)
11367 ("both %<unsigned%> and %<_Decimal32%> in "
11368 "declaration specifiers"));
11369 else if (specs->typespec_word == cts_dfloat64)
11371 ("both %<unsigned%> and %<_Decimal64%> in "
11372 "declaration specifiers"));
11373 else if (specs->typespec_word == cts_dfloat128)
11375 ("both %<unsigned%> and %<_Decimal128%> in "
11376 "declaration specifiers"));
11379 specs->unsigned_p = true;
11380 specs->locations[cdw_unsigned] = loc;
11384 dupe = specs->complex_p;
11385 if (!in_system_header_at (loc))
11386 pedwarn_c90 (loc, OPT_Wpedantic,
11387 "ISO C90 does not support complex types");
11388 if (specs->typespec_word == cts_auto_type)
11390 ("both %<complex%> and %<__auto_type%> in "
11391 "declaration specifiers"));
11392 else if (specs->typespec_word == cts_void)
11394 ("both %<complex%> and %<void%> in "
11395 "declaration specifiers"));
11396 else if (specs->typespec_word == cts_bool)
11398 ("both %<complex%> and %<_Bool%> in "
11399 "declaration specifiers"));
11400 else if (specs->typespec_word == cts_dfloat32)
11402 ("both %<complex%> and %<_Decimal32%> in "
11403 "declaration specifiers"));
11404 else if (specs->typespec_word == cts_dfloat64)
11406 ("both %<complex%> and %<_Decimal64%> in "
11407 "declaration specifiers"));
11408 else if (specs->typespec_word == cts_dfloat128)
11410 ("both %<complex%> and %<_Decimal128%> in "
11411 "declaration specifiers"));
11412 else if (specs->typespec_word == cts_fract)
11414 ("both %<complex%> and %<_Fract%> in "
11415 "declaration specifiers"));
11416 else if (specs->typespec_word == cts_accum)
11418 ("both %<complex%> and %<_Accum%> in "
11419 "declaration specifiers"));
11420 else if (specs->saturating_p)
11422 ("both %<complex%> and %<_Sat%> in "
11423 "declaration specifiers"));
11426 specs->complex_p = true;
11427 specs->locations[cdw_complex] = loc;
11431 dupe = specs->saturating_p;
11432 pedwarn (loc, OPT_Wpedantic,
11433 "ISO C does not support saturating types");
11434 if (specs->typespec_word == cts_int_n)
11437 ("both %<_Sat%> and %<__int%d%> in "
11438 "declaration specifiers"),
11439 int_n_data[specs->int_n_idx].bitsize);
11441 else if (specs->typespec_word == cts_auto_type)
11443 ("both %<_Sat%> and %<__auto_type%> in "
11444 "declaration specifiers"));
11445 else if (specs->typespec_word == cts_void)
11447 ("both %<_Sat%> and %<void%> in "
11448 "declaration specifiers"));
11449 else if (specs->typespec_word == cts_bool)
11451 ("both %<_Sat%> and %<_Bool%> in "
11452 "declaration specifiers"));
11453 else if (specs->typespec_word == cts_char)
11455 ("both %<_Sat%> and %<char%> in "
11456 "declaration specifiers"));
11457 else if (specs->typespec_word == cts_int)
11459 ("both %<_Sat%> and %<int%> in "
11460 "declaration specifiers"));
11461 else if (specs->typespec_word == cts_float)
11463 ("both %<_Sat%> and %<float%> in "
11464 "declaration specifiers"));
11465 else if (specs->typespec_word == cts_double)
11467 ("both %<_Sat%> and %<double%> in "
11468 "declaration specifiers"));
11469 else if (specs->typespec_word == cts_floatn_nx)
11471 ("both %<_Sat%> and %<_Float%d%s%> in "
11472 "declaration specifiers"),
11473 floatn_nx_types[specs->floatn_nx_idx].n,
11474 (floatn_nx_types[specs->floatn_nx_idx].extended
11477 else if (specs->typespec_word == cts_dfloat32)
11479 ("both %<_Sat%> and %<_Decimal32%> in "
11480 "declaration specifiers"));
11481 else if (specs->typespec_word == cts_dfloat64)
11483 ("both %<_Sat%> and %<_Decimal64%> in "
11484 "declaration specifiers"));
11485 else if (specs->typespec_word == cts_dfloat128)
11487 ("both %<_Sat%> and %<_Decimal128%> in "
11488 "declaration specifiers"));
11489 else if (specs->complex_p)
11491 ("both %<_Sat%> and %<complex%> in "
11492 "declaration specifiers"));
11495 specs->saturating_p = true;
11496 specs->locations[cdw_saturating] = loc;
11500 gcc_unreachable ();
11504 error_at (loc, "duplicate %qE", type);
11510 /* "void", "_Bool", "char", "int", "float", "double",
11511 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11512 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11514 if (specs->typespec_word != cts_none)
11517 "two or more data types in declaration specifiers");
11522 case RID_AUTO_TYPE:
11525 ("both %<long%> and %<__auto_type%> in "
11526 "declaration specifiers"));
11527 else if (specs->short_p)
11529 ("both %<short%> and %<__auto_type%> in "
11530 "declaration specifiers"));
11531 else if (specs->signed_p)
11533 ("both %<signed%> and %<__auto_type%> in "
11534 "declaration specifiers"));
11535 else if (specs->unsigned_p)
11537 ("both %<unsigned%> and %<__auto_type%> in "
11538 "declaration specifiers"));
11539 else if (specs->complex_p)
11541 ("both %<complex%> and %<__auto_type%> in "
11542 "declaration specifiers"));
11543 else if (specs->saturating_p)
11545 ("both %<_Sat%> and %<__auto_type%> in "
11546 "declaration specifiers"));
11549 specs->typespec_word = cts_auto_type;
11550 specs->locations[cdw_typespec] = loc;
11557 specs->int_n_idx = i - RID_INT_N_0;
11558 if (!in_system_header_at (input_location)
11559 /* If the INT_N type ends in "__", and so is of the format
11560 "__intN__", don't pedwarn. */
11561 && (strncmp (IDENTIFIER_POINTER (type)
11562 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11563 pedwarn (loc, OPT_Wpedantic,
11564 "ISO C does not support %<__int%d%> types",
11565 int_n_data[specs->int_n_idx].bitsize);
11569 ("both %<__int%d%> and %<long%> in "
11570 "declaration specifiers"),
11571 int_n_data[specs->int_n_idx].bitsize);
11572 else if (specs->saturating_p)
11574 ("both %<_Sat%> and %<__int%d%> in "
11575 "declaration specifiers"),
11576 int_n_data[specs->int_n_idx].bitsize);
11577 else if (specs->short_p)
11579 ("both %<__int%d%> and %<short%> in "
11580 "declaration specifiers"),
11581 int_n_data[specs->int_n_idx].bitsize);
11582 else if (! int_n_enabled_p[specs->int_n_idx])
11584 specs->typespec_word = cts_int_n;
11586 "%<__int%d%> is not supported on this target",
11587 int_n_data[specs->int_n_idx].bitsize);
11591 specs->typespec_word = cts_int_n;
11592 specs->locations[cdw_typespec] = loc;
11598 ("both %<long%> and %<void%> in "
11599 "declaration specifiers"));
11600 else if (specs->short_p)
11602 ("both %<short%> and %<void%> in "
11603 "declaration specifiers"));
11604 else if (specs->signed_p)
11606 ("both %<signed%> and %<void%> in "
11607 "declaration specifiers"));
11608 else if (specs->unsigned_p)
11610 ("both %<unsigned%> and %<void%> in "
11611 "declaration specifiers"));
11612 else if (specs->complex_p)
11614 ("both %<complex%> and %<void%> in "
11615 "declaration specifiers"));
11616 else if (specs->saturating_p)
11618 ("both %<_Sat%> and %<void%> in "
11619 "declaration specifiers"));
11622 specs->typespec_word = cts_void;
11623 specs->locations[cdw_typespec] = loc;
11627 if (!in_system_header_at (loc))
11628 pedwarn_c90 (loc, OPT_Wpedantic,
11629 "ISO C90 does not support boolean types");
11632 ("both %<long%> and %<_Bool%> in "
11633 "declaration specifiers"));
11634 else if (specs->short_p)
11636 ("both %<short%> and %<_Bool%> in "
11637 "declaration specifiers"));
11638 else if (specs->signed_p)
11640 ("both %<signed%> and %<_Bool%> in "
11641 "declaration specifiers"));
11642 else if (specs->unsigned_p)
11644 ("both %<unsigned%> and %<_Bool%> in "
11645 "declaration specifiers"));
11646 else if (specs->complex_p)
11648 ("both %<complex%> and %<_Bool%> in "
11649 "declaration specifiers"));
11650 else if (specs->saturating_p)
11652 ("both %<_Sat%> and %<_Bool%> in "
11653 "declaration specifiers"));
11656 specs->typespec_word = cts_bool;
11657 specs->locations[cdw_typespec] = loc;
11663 ("both %<long%> and %<char%> in "
11664 "declaration specifiers"));
11665 else if (specs->short_p)
11667 ("both %<short%> and %<char%> in "
11668 "declaration specifiers"));
11669 else if (specs->saturating_p)
11671 ("both %<_Sat%> and %<char%> in "
11672 "declaration specifiers"));
11675 specs->typespec_word = cts_char;
11676 specs->locations[cdw_typespec] = loc;
11680 if (specs->saturating_p)
11682 ("both %<_Sat%> and %<int%> in "
11683 "declaration specifiers"));
11686 specs->typespec_word = cts_int;
11687 specs->locations[cdw_typespec] = loc;
11693 ("both %<long%> and %<float%> in "
11694 "declaration specifiers"));
11695 else if (specs->short_p)
11697 ("both %<short%> and %<float%> in "
11698 "declaration specifiers"));
11699 else if (specs->signed_p)
11701 ("both %<signed%> and %<float%> in "
11702 "declaration specifiers"));
11703 else if (specs->unsigned_p)
11705 ("both %<unsigned%> and %<float%> in "
11706 "declaration specifiers"));
11707 else if (specs->saturating_p)
11709 ("both %<_Sat%> and %<float%> in "
11710 "declaration specifiers"));
11713 specs->typespec_word = cts_float;
11714 specs->locations[cdw_typespec] = loc;
11718 if (specs->long_long_p)
11720 ("both %<long long%> and %<double%> in "
11721 "declaration specifiers"));
11722 else if (specs->short_p)
11724 ("both %<short%> and %<double%> in "
11725 "declaration specifiers"));
11726 else if (specs->signed_p)
11728 ("both %<signed%> and %<double%> in "
11729 "declaration specifiers"));
11730 else if (specs->unsigned_p)
11732 ("both %<unsigned%> and %<double%> in "
11733 "declaration specifiers"));
11734 else if (specs->saturating_p)
11736 ("both %<_Sat%> and %<double%> in "
11737 "declaration specifiers"));
11740 specs->typespec_word = cts_double;
11741 specs->locations[cdw_typespec] = loc;
11744 CASE_RID_FLOATN_NX:
11745 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11746 if (!in_system_header_at (input_location))
11747 pedwarn (loc, OPT_Wpedantic,
11748 "ISO C does not support the %<_Float%d%s%> type",
11749 floatn_nx_types[specs->floatn_nx_idx].n,
11750 (floatn_nx_types[specs->floatn_nx_idx].extended
11756 ("both %<long%> and %<_Float%d%s%> in "
11757 "declaration specifiers"),
11758 floatn_nx_types[specs->floatn_nx_idx].n,
11759 (floatn_nx_types[specs->floatn_nx_idx].extended
11762 else if (specs->short_p)
11764 ("both %<short%> and %<_Float%d%s%> in "
11765 "declaration specifiers"),
11766 floatn_nx_types[specs->floatn_nx_idx].n,
11767 (floatn_nx_types[specs->floatn_nx_idx].extended
11770 else if (specs->signed_p)
11772 ("both %<signed%> and %<_Float%d%s%> in "
11773 "declaration specifiers"),
11774 floatn_nx_types[specs->floatn_nx_idx].n,
11775 (floatn_nx_types[specs->floatn_nx_idx].extended
11778 else if (specs->unsigned_p)
11780 ("both %<unsigned%> and %<_Float%d%s%> in "
11781 "declaration specifiers"),
11782 floatn_nx_types[specs->floatn_nx_idx].n,
11783 (floatn_nx_types[specs->floatn_nx_idx].extended
11786 else if (specs->saturating_p)
11788 ("both %<_Sat%> and %<_Float%d%s%> in "
11789 "declaration specifiers"),
11790 floatn_nx_types[specs->floatn_nx_idx].n,
11791 (floatn_nx_types[specs->floatn_nx_idx].extended
11794 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11796 specs->typespec_word = cts_floatn_nx;
11798 "%<_Float%d%s%> is not supported on this target",
11799 floatn_nx_types[specs->floatn_nx_idx].n,
11800 (floatn_nx_types[specs->floatn_nx_idx].extended
11806 specs->typespec_word = cts_floatn_nx;
11807 specs->locations[cdw_typespec] = loc;
11812 case RID_DFLOAT128:
11815 if (i == RID_DFLOAT32)
11816 str = "_Decimal32";
11817 else if (i == RID_DFLOAT64)
11818 str = "_Decimal64";
11820 str = "_Decimal128";
11821 if (specs->long_long_p)
11823 ("both %<long long%> and %qs in "
11824 "declaration specifiers"),
11828 ("both %<long%> and %qs in "
11829 "declaration specifiers"),
11831 else if (specs->short_p)
11833 ("both %<short%> and %qs in "
11834 "declaration specifiers"),
11836 else if (specs->signed_p)
11838 ("both %<signed%> and %qs in "
11839 "declaration specifiers"),
11841 else if (specs->unsigned_p)
11843 ("both %<unsigned%> and %qs in "
11844 "declaration specifiers"),
11846 else if (specs->complex_p)
11848 ("both %<complex%> and %qs in "
11849 "declaration specifiers"),
11851 else if (specs->saturating_p)
11853 ("both %<_Sat%> and %qs in "
11854 "declaration specifiers"),
11856 else if (i == RID_DFLOAT32)
11857 specs->typespec_word = cts_dfloat32;
11858 else if (i == RID_DFLOAT64)
11859 specs->typespec_word = cts_dfloat64;
11861 specs->typespec_word = cts_dfloat128;
11862 specs->locations[cdw_typespec] = loc;
11864 if (!targetm.decimal_float_supported_p ())
11866 ("decimal floating-point not supported "
11867 "for this target"));
11868 pedwarn_c11 (loc, OPT_Wpedantic,
11869 "ISO C does not support decimal floating-point "
11876 if (i == RID_FRACT)
11880 if (specs->complex_p)
11882 ("both %<complex%> and %qs in "
11883 "declaration specifiers"),
11885 else if (i == RID_FRACT)
11886 specs->typespec_word = cts_fract;
11888 specs->typespec_word = cts_accum;
11889 specs->locations[cdw_typespec] = loc;
11891 if (!targetm.fixed_point_supported_p ())
11893 "fixed-point types not supported for this target");
11894 pedwarn (loc, OPT_Wpedantic,
11895 "ISO C does not support fixed-point types");
11898 /* ObjC reserved word "id", handled below. */
11904 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11905 form of ObjC type, cases such as "int" and "long" being handled
11906 above), a TYPE (struct, union, enum and typeof specifiers) or an
11907 ERROR_MARK. In none of these cases may there have previously
11908 been any type specifiers. */
11909 if (specs->type || specs->typespec_word != cts_none
11910 || specs->long_p || specs->short_p || specs->signed_p
11911 || specs->unsigned_p || specs->complex_p)
11912 error_at (loc, "two or more data types in declaration specifiers");
11913 else if (TREE_CODE (type) == TYPE_DECL)
11915 if (TREE_TYPE (type) == error_mark_node)
11916 ; /* Allow the type to default to int to avoid cascading errors. */
11919 specs->type = TREE_TYPE (type);
11920 specs->decl_attr = DECL_ATTRIBUTES (type);
11921 specs->typedef_p = true;
11922 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11923 specs->locations[cdw_typedef] = loc;
11925 /* If this typedef name is defined in a struct, then a C++
11926 lookup would return a different value. */
11927 if (warn_cxx_compat
11928 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11929 warning_at (loc, OPT_Wc___compat,
11930 "C++ lookup of %qD would return a field, not a type",
11933 /* If we are parsing a struct, record that a struct field
11935 if (warn_cxx_compat && struct_parse_info != NULL)
11936 struct_parse_info->typedefs_seen.safe_push (type);
11939 else if (TREE_CODE (type) == IDENTIFIER_NODE)
11941 tree t = lookup_name (type);
11942 if (!t || TREE_CODE (t) != TYPE_DECL)
11943 error_at (loc, "%qE fails to be a typedef or built in type", type);
11944 else if (TREE_TYPE (t) == error_mark_node)
11948 specs->type = TREE_TYPE (t);
11949 specs->locations[cdw_typespec] = loc;
11954 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11956 specs->typedef_p = true;
11957 specs->locations[cdw_typedef] = loc;
11961 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11962 specs->expr, spec.expr);
11964 specs->expr = spec.expr;
11965 specs->expr_const_operands &= spec.expr_const_operands;
11968 specs->type = type;
11974 /* Add the storage class specifier or function specifier SCSPEC to the
11975 declaration specifiers SPECS, returning SPECS. */
11977 struct c_declspecs *
11978 declspecs_add_scspec (location_t loc,
11979 struct c_declspecs *specs,
11983 enum c_storage_class n = csc_none;
11985 specs->declspecs_seen_p = true;
11986 specs->non_std_attrs_seen_p = true;
11987 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11988 && C_IS_RESERVED_WORD (scspec));
11989 i = C_RID_CODE (scspec);
11990 if (specs->non_sc_seen_p)
11991 warning (OPT_Wold_style_declaration,
11992 "%qE is not at beginning of declaration", scspec);
11996 /* C99 permits duplicate inline. Although of doubtful utility,
11997 it seems simplest to permit it in gnu89 mode as well, as
11998 there is also little utility in maintaining this as a
11999 difference between gnu89 and C99 inline. */
12001 specs->inline_p = true;
12002 specs->locations[cdw_inline] = loc;
12005 /* Duplicate _Noreturn is permitted. */
12007 specs->noreturn_p = true;
12008 specs->locations[cdw_noreturn] = loc;
12011 dupe = specs->thread_p;
12012 if (specs->storage_class == csc_auto)
12013 error ("%qE used with %<auto%>", scspec);
12014 else if (specs->storage_class == csc_register)
12015 error ("%qE used with %<register%>", scspec);
12016 else if (specs->storage_class == csc_typedef)
12017 error ("%qE used with %<typedef%>", scspec);
12020 specs->thread_p = true;
12021 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
12023 /* A diagnostic is not required for the use of this
12024 identifier in the implementation namespace; only diagnose
12025 it for the C11 spelling because of existing code using
12026 the other spelling. */
12027 if (!specs->thread_gnu_p)
12030 pedwarn_c99 (loc, OPT_Wpedantic,
12031 "ISO C99 does not support %qE", scspec);
12033 pedwarn_c99 (loc, OPT_Wpedantic,
12034 "ISO C90 does not support %qE", scspec);
12036 specs->locations[cdw_thread] = loc;
12044 /* Diagnose "__thread extern". */
12045 if (specs->thread_p && specs->thread_gnu_p)
12046 error ("%<__thread%> before %<extern%>");
12053 /* Diagnose "__thread static". */
12054 if (specs->thread_p && specs->thread_gnu_p)
12055 error ("%<__thread%> before %<static%>");
12061 gcc_unreachable ();
12063 if (n != csc_none && n == specs->storage_class)
12067 if (i == RID_THREAD)
12068 error ("duplicate %<_Thread_local%> or %<__thread%>");
12070 error ("duplicate %qE", scspec);
12074 if (specs->storage_class != csc_none && n != specs->storage_class)
12076 error ("multiple storage classes in declaration specifiers");
12080 specs->storage_class = n;
12081 specs->locations[cdw_storage_class] = loc;
12082 if (n != csc_extern && n != csc_static && specs->thread_p)
12084 error ("%qs used with %qE",
12085 specs->thread_gnu_p ? "__thread" : "_Thread_local",
12087 specs->thread_p = false;
12094 /* Add the attributes ATTRS to the declaration specifiers SPECS,
12095 returning SPECS. */
12097 struct c_declspecs *
12098 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
12100 specs->attrs = chainon (attrs, specs->attrs);
12101 specs->locations[cdw_attributes] = loc;
12102 specs->declspecs_seen_p = true;
12103 /* In the case of standard attributes at the start of the
12104 declaration, the caller will reset this. */
12105 specs->non_std_attrs_seen_p = true;
12109 /* Add an _Alignas specifier (expression ALIGN, or type whose
12110 alignment is ALIGN) to the declaration specifiers SPECS, returning
12112 struct c_declspecs *
12113 declspecs_add_alignas (location_t loc,
12114 struct c_declspecs *specs, tree align)
12116 specs->alignas_p = true;
12117 specs->locations[cdw_alignas] = loc;
12118 if (align == error_mark_node)
12121 /* Only accept the alignment if it's valid and greater than
12122 the current one. Zero is invalid but by C11 required to
12123 be silently ignored. */
12124 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
12125 if (align_log > specs->align_log)
12126 specs->align_log = align_log;
12130 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
12131 specifiers with any other type specifier to determine the resulting
12132 type. This is where ISO C checks on complex types are made, since
12133 "_Complex long" is a prefix of the valid ISO C type "_Complex long
12134 double". Also apply postfix standard attributes to modify the type. */
12136 struct c_declspecs *
12137 finish_declspecs (struct c_declspecs *specs)
12139 /* If a type was specified as a whole, we have no modifiers and are
12141 if (specs->type != NULL_TREE)
12143 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12144 && !specs->signed_p && !specs->unsigned_p
12145 && !specs->complex_p);
12147 /* Set a dummy type. */
12148 if (TREE_CODE (specs->type) == ERROR_MARK)
12149 specs->type = integer_type_node;
12150 goto handle_postfix_attrs;
12153 /* If none of "void", "_Bool", "char", "int", "float" or "double"
12154 has been specified, treat it as "int" unless "_Complex" is
12155 present and there are no other specifiers. If we just have
12156 "_Complex", it is equivalent to "_Complex double", but e.g.
12157 "_Complex short" is equivalent to "_Complex short int". */
12158 if (specs->typespec_word == cts_none)
12160 if (specs->saturating_p)
12162 error_at (specs->locations[cdw_saturating],
12163 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
12164 if (!targetm.fixed_point_supported_p ())
12165 error_at (specs->locations[cdw_saturating],
12166 "fixed-point types not supported for this target");
12167 specs->typespec_word = cts_fract;
12169 else if (specs->long_p || specs->short_p
12170 || specs->signed_p || specs->unsigned_p)
12172 specs->typespec_word = cts_int;
12174 else if (specs->complex_p)
12176 specs->typespec_word = cts_double;
12177 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12178 "ISO C does not support plain %<complex%> meaning "
12179 "%<double complex%>");
12183 specs->typespec_word = cts_int;
12184 specs->default_int_p = true;
12185 /* We don't diagnose this here because grokdeclarator will
12186 give more specific diagnostics according to whether it is
12187 a function definition. */
12191 /* If "signed" was specified, record this to distinguish "int" and
12192 "signed int" in the case of a bit-field with
12193 -funsigned-bitfields. */
12194 specs->explicit_signed_p = specs->signed_p;
12196 /* Now compute the actual type. */
12197 switch (specs->typespec_word)
12199 case cts_auto_type:
12200 gcc_assert (!specs->long_p && !specs->short_p
12201 && !specs->signed_p && !specs->unsigned_p
12202 && !specs->complex_p);
12203 /* Type to be filled in later. */
12204 if (specs->postfix_attrs)
12205 error ("%<__auto_type%> followed by %<[[]]%> attributes");
12208 gcc_assert (!specs->long_p && !specs->short_p
12209 && !specs->signed_p && !specs->unsigned_p
12210 && !specs->complex_p);
12211 specs->type = void_type_node;
12214 gcc_assert (!specs->long_p && !specs->short_p
12215 && !specs->signed_p && !specs->unsigned_p
12216 && !specs->complex_p);
12217 specs->type = boolean_type_node;
12220 gcc_assert (!specs->long_p && !specs->short_p);
12221 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12222 if (specs->signed_p)
12223 specs->type = signed_char_type_node;
12224 else if (specs->unsigned_p)
12225 specs->type = unsigned_char_type_node;
12227 specs->type = char_type_node;
12228 if (specs->complex_p)
12230 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12231 "ISO C does not support complex integer types");
12232 specs->type = build_complex_type (specs->type);
12236 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12237 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12238 if (! int_n_enabled_p[specs->int_n_idx])
12239 specs->type = integer_type_node;
12241 specs->type = (specs->unsigned_p
12242 ? int_n_trees[specs->int_n_idx].unsigned_type
12243 : int_n_trees[specs->int_n_idx].signed_type);
12244 if (specs->complex_p)
12246 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12247 "ISO C does not support complex integer types");
12248 specs->type = build_complex_type (specs->type);
12252 gcc_assert (!(specs->long_p && specs->short_p));
12253 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12254 if (specs->long_long_p)
12255 specs->type = (specs->unsigned_p
12256 ? long_long_unsigned_type_node
12257 : long_long_integer_type_node);
12258 else if (specs->long_p)
12259 specs->type = (specs->unsigned_p
12260 ? long_unsigned_type_node
12261 : long_integer_type_node);
12262 else if (specs->short_p)
12263 specs->type = (specs->unsigned_p
12264 ? short_unsigned_type_node
12265 : short_integer_type_node);
12267 specs->type = (specs->unsigned_p
12268 ? unsigned_type_node
12269 : integer_type_node);
12270 if (specs->complex_p)
12272 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12273 "ISO C does not support complex integer types");
12274 specs->type = build_complex_type (specs->type);
12278 gcc_assert (!specs->long_p && !specs->short_p
12279 && !specs->signed_p && !specs->unsigned_p);
12280 specs->type = (specs->complex_p
12281 ? complex_float_type_node
12282 : float_type_node);
12285 gcc_assert (!specs->long_long_p && !specs->short_p
12286 && !specs->signed_p && !specs->unsigned_p);
12289 specs->type = (specs->complex_p
12290 ? complex_long_double_type_node
12291 : long_double_type_node);
12295 specs->type = (specs->complex_p
12296 ? complex_double_type_node
12297 : double_type_node);
12300 case cts_floatn_nx:
12301 gcc_assert (!specs->long_p && !specs->short_p
12302 && !specs->signed_p && !specs->unsigned_p);
12303 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12304 specs->type = integer_type_node;
12305 else if (specs->complex_p)
12306 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12308 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12312 case cts_dfloat128:
12313 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12314 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12315 if (!targetm.decimal_float_supported_p ())
12316 specs->type = integer_type_node;
12317 else if (specs->typespec_word == cts_dfloat32)
12318 specs->type = dfloat32_type_node;
12319 else if (specs->typespec_word == cts_dfloat64)
12320 specs->type = dfloat64_type_node;
12322 specs->type = dfloat128_type_node;
12325 gcc_assert (!specs->complex_p);
12326 if (!targetm.fixed_point_supported_p ())
12327 specs->type = integer_type_node;
12328 else if (specs->saturating_p)
12330 if (specs->long_long_p)
12331 specs->type = specs->unsigned_p
12332 ? sat_unsigned_long_long_fract_type_node
12333 : sat_long_long_fract_type_node;
12334 else if (specs->long_p)
12335 specs->type = specs->unsigned_p
12336 ? sat_unsigned_long_fract_type_node
12337 : sat_long_fract_type_node;
12338 else if (specs->short_p)
12339 specs->type = specs->unsigned_p
12340 ? sat_unsigned_short_fract_type_node
12341 : sat_short_fract_type_node;
12343 specs->type = specs->unsigned_p
12344 ? sat_unsigned_fract_type_node
12345 : sat_fract_type_node;
12349 if (specs->long_long_p)
12350 specs->type = specs->unsigned_p
12351 ? unsigned_long_long_fract_type_node
12352 : long_long_fract_type_node;
12353 else if (specs->long_p)
12354 specs->type = specs->unsigned_p
12355 ? unsigned_long_fract_type_node
12356 : long_fract_type_node;
12357 else if (specs->short_p)
12358 specs->type = specs->unsigned_p
12359 ? unsigned_short_fract_type_node
12360 : short_fract_type_node;
12362 specs->type = specs->unsigned_p
12363 ? unsigned_fract_type_node
12368 gcc_assert (!specs->complex_p);
12369 if (!targetm.fixed_point_supported_p ())
12370 specs->type = integer_type_node;
12371 else if (specs->saturating_p)
12373 if (specs->long_long_p)
12374 specs->type = specs->unsigned_p
12375 ? sat_unsigned_long_long_accum_type_node
12376 : sat_long_long_accum_type_node;
12377 else if (specs->long_p)
12378 specs->type = specs->unsigned_p
12379 ? sat_unsigned_long_accum_type_node
12380 : sat_long_accum_type_node;
12381 else if (specs->short_p)
12382 specs->type = specs->unsigned_p
12383 ? sat_unsigned_short_accum_type_node
12384 : sat_short_accum_type_node;
12386 specs->type = specs->unsigned_p
12387 ? sat_unsigned_accum_type_node
12388 : sat_accum_type_node;
12392 if (specs->long_long_p)
12393 specs->type = specs->unsigned_p
12394 ? unsigned_long_long_accum_type_node
12395 : long_long_accum_type_node;
12396 else if (specs->long_p)
12397 specs->type = specs->unsigned_p
12398 ? unsigned_long_accum_type_node
12399 : long_accum_type_node;
12400 else if (specs->short_p)
12401 specs->type = specs->unsigned_p
12402 ? unsigned_short_accum_type_node
12403 : short_accum_type_node;
12405 specs->type = specs->unsigned_p
12406 ? unsigned_accum_type_node
12411 gcc_unreachable ();
12413 handle_postfix_attrs:
12414 if (specs->type != NULL)
12416 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12417 decl_attributes (&specs->type, specs->postfix_attrs, 0);
12418 specs->postfix_attrs = NULL_TREE;
12424 /* Perform final processing on one file scope's declarations (or the
12425 external scope's declarations), GLOBALS. */
12428 c_write_global_declarations_1 (tree globals)
12433 /* Process the decls in the order they were written. */
12434 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12436 /* Check for used but undefined static functions using the C
12437 standard's definition of "used", and set TREE_NO_WARNING so
12438 that check_global_declaration doesn't repeat the check. */
12439 if (TREE_CODE (decl) == FUNCTION_DECL
12440 && DECL_INITIAL (decl) == NULL_TREE
12441 && DECL_EXTERNAL (decl)
12442 && !TREE_PUBLIC (decl))
12444 if (C_DECL_USED (decl))
12446 /* TODO: Add OPT_Wundefined-inline. */
12447 if (pedwarn (input_location, 0, "%q+F used but never defined",
12449 suppress_warning (decl /* OPT_Wundefined-inline. */);
12451 /* For -Wunused-function warn about unused static prototypes. */
12452 else if (warn_unused_function
12453 && ! DECL_ARTIFICIAL (decl)
12454 && ! warning_suppressed_p (decl, OPT_Wunused_function))
12456 if (warning (OPT_Wunused_function,
12457 "%q+F declared %<static%> but never defined",
12459 suppress_warning (decl, OPT_Wunused_function);
12463 wrapup_global_declaration_1 (decl);
12468 reconsider = false;
12469 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12470 reconsider |= wrapup_global_declaration_2 (decl);
12472 while (reconsider);
12475 /* Preserve the external declarations scope across a garbage collect. */
12476 static GTY(()) tree ext_block;
12478 /* Collect all references relevant to SOURCE_FILE. */
12481 collect_all_refs (const char *source_file)
12486 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12487 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12489 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12492 /* Collect source file references at global level. */
12495 collect_source_refs (void)
12502 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12504 decls = DECL_INITIAL (t);
12505 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12506 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12507 collect_source_ref (DECL_SOURCE_FILE (decl));
12510 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12511 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12512 collect_source_ref (DECL_SOURCE_FILE (decl));
12515 /* Free attribute access data that are not needed by the middle end. */
12518 free_attr_access_data ()
12520 struct cgraph_node *n;
12522 /* Iterate over all functions declared in the translation unit. */
12523 FOR_EACH_FUNCTION (n)
12525 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12526 if (tree attrs = DECL_ATTRIBUTES (parm))
12527 attr_access::free_lang_data (attrs);
12529 tree fntype = TREE_TYPE (n->decl);
12530 if (!fntype || fntype == error_mark_node)
12532 tree attrs = TYPE_ATTRIBUTES (fntype);
12536 attr_access::free_lang_data (attrs);
12540 /* Perform any final parser cleanups and generate initial debugging
12544 c_parse_final_cleanups (void)
12549 /* We don't want to do this if generating a PCH. */
12553 timevar_stop (TV_PHASE_PARSING);
12554 timevar_start (TV_PHASE_DEFERRED);
12556 /* Do the Objective-C stuff. This is where all the Objective-C
12557 module stuff gets generated (symtab, class/protocol/selector
12559 if (c_dialect_objc ())
12560 objc_write_global_declarations ();
12562 /* Close the external scope. */
12563 ext_block = pop_scope ();
12564 external_scope = 0;
12565 gcc_assert (!current_scope);
12567 /* Handle -fdump-ada-spec[-slim]. */
12568 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12570 /* Build a table of files to generate specs for */
12571 collect_source_ref (main_input_filename);
12572 if (!flag_dump_ada_spec_slim)
12573 collect_source_refs ();
12575 dump_ada_specs (collect_all_refs, NULL);
12578 /* Process all file scopes in this compilation, and the external_scope,
12579 through wrapup_global_declarations. */
12580 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12581 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12582 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12585 free_attr_access_data ();
12587 timevar_stop (TV_PHASE_DEFERRED);
12588 timevar_start (TV_PHASE_PARSING);
12593 /* Register reserved keyword WORD as qualifier for address space AS. */
12596 c_register_addr_space (const char *word, addr_space_t as)
12598 int rid = RID_FIRST_ADDR_SPACE + as;
12601 /* Address space qualifiers are only supported
12602 in C with GNU extensions enabled. */
12603 if (c_dialect_objc () || flag_no_asm)
12606 id = get_identifier (word);
12607 C_SET_RID_CODE (id, rid);
12608 C_IS_RESERVED_WORD (id) = 1;
12609 ridpointers [rid] = id;
12612 /* Return identifier to look up for omp declare reduction. */
12615 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
12617 const char *p = NULL;
12618 switch (reduction_code)
12620 case PLUS_EXPR: p = "+"; break;
12621 case MULT_EXPR: p = "*"; break;
12622 case MINUS_EXPR: p = "-"; break;
12623 case BIT_AND_EXPR: p = "&"; break;
12624 case BIT_XOR_EXPR: p = "^"; break;
12625 case BIT_IOR_EXPR: p = "|"; break;
12626 case TRUTH_ANDIF_EXPR: p = "&&"; break;
12627 case TRUTH_ORIF_EXPR: p = "||"; break;
12628 case MIN_EXPR: p = "min"; break;
12629 case MAX_EXPR: p = "max"; break;
12636 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
12637 return error_mark_node;
12638 p = IDENTIFIER_POINTER (reduction_id);
12641 const char prefix[] = "omp declare reduction ";
12642 size_t lenp = sizeof (prefix);
12643 size_t len = strlen (p);
12644 char *name = XALLOCAVEC (char, lenp + len);
12645 memcpy (name, prefix, lenp - 1);
12646 memcpy (name + lenp - 1, p, len + 1);
12647 return get_identifier (name);
12650 /* Lookup REDUCTION_ID in the current scope, or create an artificial
12651 VAR_DECL, bind it into the current scope and return it. */
12654 c_omp_reduction_decl (tree reduction_id)
12656 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12657 if (b != NULL && B_IN_CURRENT_SCOPE (b))
12660 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
12661 reduction_id, integer_type_node);
12662 DECL_ARTIFICIAL (decl) = 1;
12663 DECL_EXTERNAL (decl) = 1;
12664 TREE_STATIC (decl) = 1;
12665 TREE_PUBLIC (decl) = 0;
12666 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
12670 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
12673 c_omp_reduction_lookup (tree reduction_id, tree type)
12675 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12679 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
12680 if (comptypes (TREE_PURPOSE (t), type))
12681 return TREE_VALUE (t);
12684 return error_mark_node;
12687 /* Helper function called via walk_tree, to diagnose invalid
12688 #pragma omp declare reduction combiners or initializers. */
12691 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
12693 tree *vars = (tree *) data;
12694 if (SSA_VAR_P (*tp)
12695 && !DECL_ARTIFICIAL (*tp)
12699 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
12700 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
12701 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
12702 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
12705 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
12706 "to variable %qD which is not %<omp_priv%> nor "
12716 c_check_in_current_scope (tree decl)
12718 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
12719 return b != NULL && B_IN_CURRENT_SCOPE (b);
12722 #include "gt-c-c-decl.h"