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 /* If non-zero, implicit "omp declare target" attribute is added into the
158 int 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 /* INIT is a constructor that forms DECL's initializer. If the final
5038 element initializes a flexible array field, add the size of that
5039 initializer to DECL's size. */
5042 add_flexible_array_elts_to_size (tree decl, tree init)
5046 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
5049 elt = CONSTRUCTOR_ELTS (init)->last ().value;
5050 type = TREE_TYPE (elt);
5051 if (TREE_CODE (type) == ARRAY_TYPE
5052 && TYPE_SIZE (type) == NULL_TREE
5053 && TYPE_DOMAIN (type) != NULL_TREE
5054 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
5056 complete_array_type (&type, elt, false);
5058 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
5059 DECL_SIZE_UNIT (decl)
5060 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
5064 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
5065 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
5066 before the type name, and set *EXPR_CONST_OPERANDS, if
5067 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
5068 appear in a constant expression. */
5071 groktypename (struct c_type_name *type_name, tree *expr,
5072 bool *expr_const_operands)
5075 tree attrs = type_name->specs->attrs;
5077 type_name->specs->attrs = NULL_TREE;
5079 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
5080 false, NULL, &attrs, expr, expr_const_operands,
5083 /* Apply attributes. */
5084 attrs = c_warn_type_attributes (attrs);
5085 decl_attributes (&type, attrs, 0);
5090 /* Looks up the most recent pushed declaration corresponding to DECL. */
5093 lookup_last_decl (tree decl)
5095 tree last_decl = lookup_name (DECL_NAME (decl));
5097 last_decl = lookup_name_in_scope (DECL_NAME (decl), external_scope);
5101 /* Wrapper for decl_attributes that adds some implicit attributes
5102 to VAR_DECLs or FUNCTION_DECLs. */
5105 c_decl_attributes (tree *node, tree attributes, int flags)
5107 /* Add implicit "omp declare target" attribute if requested. */
5108 if (current_omp_declare_target_attribute
5109 && ((VAR_P (*node) && is_global_var (*node))
5110 || TREE_CODE (*node) == FUNCTION_DECL))
5112 if (VAR_P (*node) && !omp_mappable_type (TREE_TYPE (*node)))
5113 attributes = tree_cons (get_identifier ("omp declare target implicit"),
5114 NULL_TREE, attributes);
5117 attributes = tree_cons (get_identifier ("omp declare target"),
5118 NULL_TREE, attributes);
5119 attributes = tree_cons (get_identifier ("omp declare target block"),
5120 NULL_TREE, attributes);
5124 /* Look up the current declaration with all the attributes merged
5125 so far so that attributes on the current declaration that's
5126 about to be pushed that conflict with the former can be detected,
5127 diagnosed, and rejected as appropriate. */
5128 tree last_decl = lookup_last_decl (*node);
5129 return decl_attributes (node, attributes, flags, last_decl);
5133 /* Decode a declarator in an ordinary declaration or data definition.
5134 This is called as soon as the type information and variable name
5135 have been parsed, before parsing the initializer if any.
5136 Here we create the ..._DECL node, fill in its type,
5137 and put it on the list of decls for the current context.
5138 When nonnull, set *LASTLOC to the location of the prior declaration
5139 of the same entity if one exists.
5140 The ..._DECL node is returned as the value.
5142 Exception: for arrays where the length is not specified,
5143 the type is left null, to be filled in by `finish_decl'.
5145 Function definitions do not come here; they go to start_function
5146 instead. However, external and forward declarations of functions
5147 do go through here. Structure field declarations are done by
5148 grokfield and not through here. */
5151 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
5152 bool initialized, tree attributes, location_t *lastloc /* = NULL */)
5156 tree expr = NULL_TREE;
5157 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
5159 /* An object declared as __attribute__((unavailable)) suppresses
5160 warnings and errors from __attribute__((deprecated/unavailable))
5162 An object declared as __attribute__((deprecated)) suppresses
5163 warnings of uses of other deprecated items. */
5164 if (lookup_attribute ("unavailable", attributes))
5165 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
5166 else if (lookup_attribute ("deprecated", attributes))
5167 deprecated_state = DEPRECATED_SUPPRESS;
5169 decl = grokdeclarator (declarator, declspecs,
5170 NORMAL, initialized, NULL, &attributes, &expr, NULL,
5172 if (!decl || decl == error_mark_node)
5175 if (tree lastdecl = lastloc ? lookup_last_decl (decl) : NULL_TREE)
5176 if (lastdecl != error_mark_node)
5177 *lastloc = DECL_SOURCE_LOCATION (lastdecl);
5180 add_stmt (fold_convert (void_type_node, expr));
5182 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
5183 && TREE_PUBLIC (decl))
5184 warning (OPT_Wmain, "%q+D is usually a function", decl);
5187 /* Is it valid for this decl to have an initializer at all?
5188 If not, set INITIALIZED to zero, which will indirectly
5189 tell 'finish_decl' to ignore the initializer once it is parsed. */
5190 switch (TREE_CODE (decl))
5193 error ("typedef %qD is initialized (use %<__typeof__%> instead)", decl);
5194 initialized = false;
5198 error ("function %qD is initialized like a variable", decl);
5199 initialized = false;
5203 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
5204 error ("parameter %qD is initialized", decl);
5205 initialized = false;
5209 /* Don't allow initializations for incomplete types except for
5210 arrays which might be completed by the initialization. */
5212 /* This can happen if the array size is an undefined macro.
5213 We already gave a warning, so we don't need another one. */
5214 if (TREE_TYPE (decl) == error_mark_node)
5215 initialized = false;
5216 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
5218 /* A complete type is ok if size is fixed. If the size is
5219 variable, an empty initializer is OK and nonempty
5220 initializers will be diagnosed in the parser. */
5222 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
5224 error ("variable %qD has initializer but incomplete type", decl);
5225 initialized = false;
5231 if (current_scope == file_scope)
5232 TREE_STATIC (decl) = 1;
5234 /* Tell 'pushdecl' this is an initialized decl
5235 even though we don't yet have the initializer expression.
5236 Also tell 'finish_decl' it may store the real initializer. */
5237 DECL_INITIAL (decl) = error_mark_node;
5240 /* If this is a function declaration, write a record describing it to the
5241 prototypes file (if requested). */
5243 if (TREE_CODE (decl) == FUNCTION_DECL)
5244 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
5246 /* ANSI specifies that a tentative definition which is not merged with
5247 a non-tentative definition behaves exactly like a definition with an
5248 initializer equal to zero. (Section 3.7.2)
5250 -fno-common gives strict ANSI behavior, though this tends to break
5251 a large body of code that grew up without this rule.
5253 Thread-local variables are never common, since there's no entrenched
5254 body of code to break, and it allows more efficient variable references
5255 in the presence of dynamic linking. */
5259 && TREE_PUBLIC (decl)
5260 && !DECL_THREAD_LOCAL_P (decl)
5262 DECL_COMMON (decl) = 1;
5264 /* Set attributes here so if duplicate decl, will have proper attributes. */
5265 c_decl_attributes (&decl, attributes, 0);
5267 /* Handle gnu_inline attribute. */
5268 if (declspecs->inline_p
5269 && !flag_gnu89_inline
5270 && TREE_CODE (decl) == FUNCTION_DECL
5271 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
5272 || current_function_decl))
5274 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
5276 else if (declspecs->storage_class != csc_static)
5277 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
5280 if (TREE_CODE (decl) == FUNCTION_DECL
5281 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
5283 struct c_declarator *ce = declarator;
5285 if (ce->kind == cdk_pointer)
5286 ce = declarator->declarator;
5287 if (ce->kind == cdk_function)
5289 tree args = ce->u.arg_info->parms;
5290 for (; args; args = DECL_CHAIN (args))
5292 tree type = TREE_TYPE (args);
5293 if (type && INTEGRAL_TYPE_P (type)
5294 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5295 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
5300 if (TREE_CODE (decl) == FUNCTION_DECL
5301 && DECL_DECLARED_INLINE_P (decl)
5302 && DECL_UNINLINABLE (decl)
5303 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
5304 warning (OPT_Wattributes, "inline function %q+D given attribute %qs",
5307 /* C99 6.7.4p3: An inline definition of a function with external
5308 linkage shall not contain a definition of a modifiable object
5309 with static storage duration... */
5311 && current_scope != file_scope
5312 && TREE_STATIC (decl)
5313 && !TREE_READONLY (decl)
5314 && DECL_DECLARED_INLINE_P (current_function_decl)
5315 && DECL_EXTERNAL (current_function_decl))
5316 record_inline_static (input_location, current_function_decl,
5317 decl, csi_modifiable);
5319 if (c_dialect_objc ()
5320 && VAR_OR_FUNCTION_DECL_P (decl))
5321 objc_check_global_decl (decl);
5323 /* Add this decl to the current scope.
5324 TEM may equal DECL or it may be a previous decl of the same name. */
5325 tem = pushdecl (decl);
5327 if (initialized && DECL_EXTERNAL (tem))
5329 DECL_EXTERNAL (tem) = 0;
5330 TREE_STATIC (tem) = 1;
5336 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
5337 DECL or the non-array element type if DECL is an uninitialized array.
5338 If that type has a const member, diagnose this. */
5341 diagnose_uninitialized_cst_member (tree decl, tree type)
5344 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5347 if (TREE_CODE (field) != FIELD_DECL)
5349 field_type = strip_array_types (TREE_TYPE (field));
5351 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
5353 auto_diagnostic_group d;
5354 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5355 "uninitialized const member in %qT is invalid in C++",
5356 strip_array_types (TREE_TYPE (decl))))
5357 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
5360 if (RECORD_OR_UNION_TYPE_P (field_type))
5361 diagnose_uninitialized_cst_member (decl, field_type);
5365 /* Finish processing of a declaration;
5366 install its initial value.
5367 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
5368 If the length of an array type is not known before,
5369 it must be determined now, from the initial value, or it is an error.
5371 INIT_LOC is the location of the initial value. */
5374 finish_decl (tree decl, location_t init_loc, tree init,
5375 tree origtype, tree asmspec_tree)
5378 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
5379 const char *asmspec = 0;
5381 /* If a name was specified, get the string. */
5382 if (VAR_OR_FUNCTION_DECL_P (decl)
5383 && DECL_FILE_SCOPE_P (decl))
5384 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5386 asmspec = TREE_STRING_POINTER (asmspec_tree);
5389 && TREE_STATIC (decl)
5390 && global_bindings_p ())
5391 /* So decl is a global variable. Record the types it uses
5392 so that we can decide later to emit debug info for them. */
5393 record_types_used_by_current_var_decl (decl);
5395 /* If `start_decl' didn't like having an initialization, ignore it now. */
5396 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
5399 /* Don't crash if parm is initialized. */
5400 if (TREE_CODE (decl) == PARM_DECL)
5404 store_init_value (init_loc, decl, init, origtype);
5406 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
5407 || TREE_CODE (decl) == FIELD_DECL))
5408 objc_check_decl (decl);
5410 type = TREE_TYPE (decl);
5412 /* Deduce size of array from initialization, if not already known.
5413 This is only needed for an initialization in the current scope;
5414 it must not be done for a file-scope initialization of a
5415 declaration with external linkage, redeclared in an inner scope
5416 with the outer declaration shadowed in an intermediate scope. */
5417 if (TREE_CODE (type) == ARRAY_TYPE
5418 && TYPE_DOMAIN (type) == NULL_TREE
5419 && TREE_CODE (decl) != TYPE_DECL
5420 && !(TREE_PUBLIC (decl) && current_scope != file_scope))
5423 = (TREE_STATIC (decl)
5424 /* Even if pedantic, an external linkage array
5425 may have incomplete type at first. */
5426 ? pedantic && !TREE_PUBLIC (decl)
5427 : !DECL_EXTERNAL (decl));
5429 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
5432 /* Get the completed type made by complete_array_type. */
5433 type = TREE_TYPE (decl);
5438 error ("initializer fails to determine size of %q+D", decl);
5443 error ("array size missing in %q+D", decl);
5447 error ("zero or negative size array %q+D", decl);
5451 /* For global variables, update the copy of the type that
5452 exists in the binding. */
5453 if (TREE_PUBLIC (decl))
5455 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5456 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5457 b_ext = b_ext->shadowed;
5458 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5460 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5461 b_ext->u.type = composite_type (b_ext->u.type, type);
5463 b_ext->u.type = type;
5472 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
5473 TREE_TYPE (DECL_INITIAL (decl)) = type;
5475 relayout_decl (decl);
5478 /* Look for braced array initializers for character arrays and
5479 recursively convert them into STRING_CSTs. */
5480 if (tree init = DECL_INITIAL (decl))
5481 DECL_INITIAL (decl) = braced_lists_to_strings (type, init);
5485 if (init && TREE_CODE (init) == CONSTRUCTOR)
5486 add_flexible_array_elts_to_size (decl, init);
5488 complete_flexible_array_elts (DECL_INITIAL (decl));
5490 if (is_global_var (decl))
5492 type_context_kind context = (DECL_THREAD_LOCAL_P (decl)
5493 ? TCTX_THREAD_STORAGE
5494 : TCTX_STATIC_STORAGE);
5495 if (!verify_type_context (input_location, context, TREE_TYPE (decl)))
5496 TREE_TYPE (decl) = error_mark_node;
5499 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5500 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5501 layout_decl (decl, 0);
5503 if (DECL_SIZE (decl) == NULL_TREE
5504 /* Don't give an error if we already gave one earlier. */
5505 && TREE_TYPE (decl) != error_mark_node
5506 && (TREE_STATIC (decl)
5507 /* A static variable with an incomplete type
5508 is an error if it is initialized.
5509 Also if it is not file scope.
5510 Otherwise, let it through, but if it is not `extern'
5511 then it may cause an error message later. */
5512 ? (DECL_INITIAL (decl) != NULL_TREE
5513 || !DECL_FILE_SCOPE_P (decl))
5514 /* An automatic variable with an incomplete type
5516 : !DECL_EXTERNAL (decl)))
5518 error ("storage size of %q+D isn%'t known", decl);
5519 TREE_TYPE (decl) = error_mark_node;
5522 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5523 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5524 && DECL_SIZE (decl) == NULL_TREE
5525 && TREE_STATIC (decl))
5526 incomplete_record_decls.safe_push (decl);
5528 if (is_global_var (decl)
5529 && DECL_SIZE (decl) != NULL_TREE
5530 && TREE_TYPE (decl) != error_mark_node)
5532 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5533 constant_expression_warning (DECL_SIZE (decl));
5536 error ("storage size of %q+D isn%'t constant", decl);
5537 TREE_TYPE (decl) = error_mark_node;
5541 if (TREE_USED (type))
5543 TREE_USED (decl) = 1;
5544 DECL_READ_P (decl) = 1;
5548 /* If this is a function and an assembler name is specified, reset DECL_RTL
5549 so we can give it its new name. Also, update builtin_decl if it
5550 was a normal built-in. */
5551 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5553 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5554 set_builtin_user_assembler_name (decl, asmspec);
5555 set_user_assembler_name (decl, asmspec);
5558 /* If #pragma weak was used, mark the decl weak now. */
5559 maybe_apply_pragma_weak (decl);
5561 /* Output the assembler code and/or RTL code for variables and functions,
5562 unless the type is an undefined structure or union.
5563 If not, it will get done when the type is completed. */
5565 if (VAR_OR_FUNCTION_DECL_P (decl))
5567 /* Determine the ELF visibility. */
5568 if (TREE_PUBLIC (decl))
5569 c_determine_visibility (decl);
5571 /* This is a no-op in c-lang.cc or something real in objc-act.cc. */
5572 if (c_dialect_objc ())
5573 objc_check_decl (decl);
5577 /* If this is not a static variable, issue a warning.
5578 It doesn't make any sense to give an ASMSPEC for an
5579 ordinary, non-register local variable. Historically,
5580 GCC has accepted -- but ignored -- the ASMSPEC in
5582 if (!DECL_FILE_SCOPE_P (decl)
5584 && !C_DECL_REGISTER (decl)
5585 && !TREE_STATIC (decl))
5586 warning (0, "ignoring %<asm%> specifier for non-static local "
5587 "variable %q+D", decl);
5589 set_user_assembler_name (decl, asmspec);
5592 if (DECL_FILE_SCOPE_P (decl))
5594 if (DECL_INITIAL (decl) == NULL_TREE
5595 || DECL_INITIAL (decl) == error_mark_node)
5596 /* Don't output anything
5597 when a tentative file-scope definition is seen.
5598 But at end of compilation, do output code for them. */
5599 DECL_DEFER_OUTPUT (decl) = 1;
5600 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5601 DECL_HARD_REGISTER (decl) = 1;
5602 rest_of_decl_compilation (decl, true, 0);
5604 if (TREE_CODE (decl) == FUNCTION_DECL)
5606 tree parms = DECL_ARGUMENTS (decl);
5607 const bool builtin = fndecl_built_in_p (decl);
5608 if (tree access = build_attr_access_from_parms (parms, !builtin))
5609 decl_attributes (&decl, access, 0);
5614 /* In conjunction with an ASMSPEC, the `register'
5615 keyword indicates that we should place the variable
5616 in a particular register. */
5617 if (asmspec && C_DECL_REGISTER (decl))
5619 DECL_HARD_REGISTER (decl) = 1;
5620 /* This cannot be done for a structure with volatile
5621 fields, on which DECL_REGISTER will have been
5623 if (!DECL_REGISTER (decl))
5624 error ("cannot put object with volatile field into register");
5627 if (TREE_CODE (decl) != FUNCTION_DECL)
5629 /* If we're building a variable sized type, and we might be
5630 reachable other than via the top of the current binding
5631 level, then create a new BIND_EXPR so that we deallocate
5632 the object at the right time. */
5633 /* Note that DECL_SIZE can be null due to errors. */
5634 if (DECL_SIZE (decl)
5635 && !TREE_CONSTANT (DECL_SIZE (decl))
5636 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5639 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5640 TREE_SIDE_EFFECTS (bind) = 1;
5642 BIND_EXPR_BODY (bind) = push_stmt_list ();
5644 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5650 if (!DECL_FILE_SCOPE_P (decl))
5652 /* Recompute the RTL of a local array now
5653 if it used to be an incomplete type. */
5654 if (was_incomplete && !is_global_var (decl))
5656 /* If we used it already as memory, it must stay in memory. */
5657 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5658 /* If it's still incomplete now, no init will save it. */
5659 if (DECL_SIZE (decl) == NULL_TREE)
5660 DECL_INITIAL (decl) = NULL_TREE;
5665 if (TREE_CODE (decl) == TYPE_DECL)
5667 if (!DECL_FILE_SCOPE_P (decl)
5668 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5669 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5671 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5674 /* Install a cleanup (aka destructor) if one was given. */
5675 if (VAR_P (decl) && !TREE_STATIC (decl))
5677 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5680 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5681 tree cleanup_decl = lookup_name (cleanup_id);
5683 vec<tree, va_gc> *v;
5685 /* Build "cleanup(&decl)" for the destructor. */
5686 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5688 v->quick_push (cleanup);
5689 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5690 vNULL, cleanup_decl, v, NULL);
5693 /* Don't warn about decl unused; the cleanup uses it. */
5694 TREE_USED (decl) = 1;
5695 TREE_USED (cleanup_decl) = 1;
5696 DECL_READ_P (decl) = 1;
5698 push_cleanup (decl, cleanup, false);
5704 && !DECL_EXTERNAL (decl)
5705 && DECL_INITIAL (decl) == NULL_TREE)
5707 type = strip_array_types (type);
5708 if (TREE_READONLY (decl))
5709 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5710 "uninitialized %<const %D%> is invalid in C++", decl);
5711 else if (RECORD_OR_UNION_TYPE_P (type)
5712 && C_TYPE_FIELDS_READONLY (type))
5713 diagnose_uninitialized_cst_member (decl, type);
5718 && lookup_attribute ("omp declare target implicit",
5719 DECL_ATTRIBUTES (decl)))
5721 DECL_ATTRIBUTES (decl)
5722 = remove_attribute ("omp declare target implicit",
5723 DECL_ATTRIBUTES (decl));
5724 if (!omp_mappable_type (TREE_TYPE (decl)))
5725 error ("%q+D in declare target directive does not have mappable type",
5727 else if (!lookup_attribute ("omp declare target",
5728 DECL_ATTRIBUTES (decl))
5729 && !lookup_attribute ("omp declare target link",
5730 DECL_ATTRIBUTES (decl)))
5732 DECL_ATTRIBUTES (decl)
5733 = tree_cons (get_identifier ("omp declare target"),
5734 NULL_TREE, DECL_ATTRIBUTES (decl));
5735 symtab_node *node = symtab_node::get (decl);
5738 node->offloadable = 1;
5739 if (ENABLE_OFFLOADING)
5741 g->have_offload = true;
5742 if (is_a <varpool_node *> (node))
5743 vec_safe_push (offload_vars, decl);
5749 /* This is the last point we can lower alignment so give the target the
5752 && !is_global_var (decl)
5753 && !DECL_HARD_REGISTER (decl))
5754 targetm.lower_local_decl_alignment (decl);
5756 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5759 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5760 EXPR is NULL or a pointer to an expression that needs to be
5761 evaluated for the side effects of array size expressions in the
5765 grokparm (const struct c_parm *parm, tree *expr)
5767 tree attrs = parm->attrs;
5768 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5769 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5771 decl_attributes (&decl, attrs, 0);
5776 /* Return attribute "arg spec" corresponding to an array/VLA parameter
5777 described by PARM, concatenated onto attributes ATTRS.
5778 The spec consists of one dollar symbol for each specified variable
5779 bound, one asterisk for each unspecified variable bound, followed
5780 by at most one specification of the most significant bound of
5781 an ordinary array parameter. For ordinary arrays the specification
5782 is either the constant bound itself, or the space character for
5783 an array with an unspecified bound (the [] form). Finally, a chain
5784 of specified variable bounds is appended to the spec, starting with
5785 the most significant bound. For example, the PARM T a[2][m][3][n]
5786 will produce __attribute__((arg spec ("[$$2]", m, n)).
5787 For T a typedef for an array with variable bounds, the bounds are
5788 included in the specification in the expected order.
5789 No "arg spec" is created for parameters of pointer types, making
5790 a distinction between T(*)[N] (or, equivalently, T[][N]) and
5791 the T[M][N] form, all of which have the same type and are represented
5792 the same, but only the last of which gets an "arg spec" describing
5793 the most significant bound M. */
5796 get_parm_array_spec (const struct c_parm *parm, tree attrs)
5798 /* The attribute specification string, minor bound first. */
5801 /* A list of VLA variable bounds, major first, or null if unspecified
5803 tree vbchain = NULL_TREE;
5804 /* True for a pointer parameter. */
5805 bool pointer = false;
5806 /* True for an ordinary array with an unpecified bound. */
5807 bool nobound = false;
5809 /* Create a string representation for the bounds of the array/VLA. */
5810 for (c_declarator *pd = parm->declarator, *next; pd; pd = next)
5812 next = pd->declarator;
5813 while (next && next->kind == cdk_attrs)
5814 next = next->declarator;
5816 /* Remember if a pointer has been seen to avoid storing the constant
5818 if (pd->kind == cdk_pointer)
5821 if ((pd->kind == cdk_pointer || pd->kind == cdk_function)
5822 && (!next || next->kind == cdk_id))
5824 /* Do nothing for the common case of a pointer. The fact that
5825 the parameter is one can be deduced from the absence of
5826 an arg spec for it. */
5830 if (pd->kind == cdk_id)
5833 || !parm->specs->type
5834 || TREE_CODE (parm->specs->type) != ARRAY_TYPE
5835 || !TYPE_DOMAIN (parm->specs->type)
5836 || !TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type)))
5839 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm->specs->type));
5841 && TREE_CODE (max) == INTEGER_CST)
5843 /* Extract the upper bound from a parameter of an array type
5844 unless the parameter is an ordinary array of unspecified
5845 bound in which case a next iteration of the loop will
5847 if (spec.empty () || spec.end ()[-1] != ' ')
5849 if (!tree_fits_shwi_p (max))
5852 /* The upper bound is the value of the largest valid
5854 HOST_WIDE_INT n = tree_to_shwi (max) + 1;
5856 sprintf (buf, "%lu", (unsigned long)n);
5862 /* For a VLA typedef, create a list of its variable bounds and
5863 append it in the expected order to VBCHAIN. */
5864 tree tpbnds = NULL_TREE;
5865 for (tree type = parm->specs->type; TREE_CODE (type) == ARRAY_TYPE;
5866 type = TREE_TYPE (type))
5868 tree nelts = array_type_nelts (type);
5869 if (error_operand_p (nelts))
5871 if (TREE_CODE (nelts) != INTEGER_CST)
5873 /* Each variable VLA bound is represented by the dollar
5876 tpbnds = tree_cons (NULL_TREE, nelts, tpbnds);
5879 tpbnds = nreverse (tpbnds);
5880 vbchain = chainon (vbchain, tpbnds);
5884 if (pd->kind != cdk_array)
5887 if (pd->u.array.vla_unspec_p)
5889 /* Each unspecified bound is represented by a star. There
5890 can be any number of these in a declaration (but none in
5896 tree nelts = pd->u.array.dimen;
5899 /* Ordinary array of unspecified size. There can be at most
5900 one for the most significant bound. Exit on the next
5901 iteration which determines whether or not PARM is declared
5902 as a pointer or an array. */
5907 if (pd->u.array.static_p)
5910 if (!INTEGRAL_TYPE_P (TREE_TYPE (nelts)))
5911 /* Avoid invalid NELTS. */
5915 nelts = c_fully_fold (nelts, false, nullptr);
5916 if (TREE_CODE (nelts) == INTEGER_CST)
5918 /* Skip all constant bounds except the most significant one.
5919 The interior ones are included in the array type. */
5920 if (next && (next->kind == cdk_array || next->kind == cdk_pointer))
5923 if (!tree_fits_uhwi_p (nelts))
5924 /* Bail completely on invalid bounds. */
5928 unsigned HOST_WIDE_INT n = tree_to_uhwi (nelts);
5929 sprintf (buf, "%llu", (unsigned long long)n);
5934 /* Each variable VLA bound is represented by a dollar sign. */
5936 vbchain = tree_cons (NULL_TREE, nelts, vbchain);
5939 if (spec.empty () && !nobound)
5942 spec.insert (0, "[");
5944 /* Ordinary array of unspecified bound is represented by a space.
5945 It must be last in the spec. */
5949 tree acsstr = build_string (spec.length () + 1, spec.c_str ());
5950 tree args = tree_cons (NULL_TREE, acsstr, vbchain);
5951 tree name = get_identifier ("arg spec");
5952 return tree_cons (name, args, attrs);
5955 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5956 and push that on the current scope. EXPR is a pointer to an
5957 expression that needs to be evaluated for the side effects of array
5958 size expressions in the parameters. */
5961 push_parm_decl (const struct c_parm *parm, tree *expr)
5963 tree attrs = parm->attrs;
5964 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5965 &attrs, expr, NULL, DEPRECATED_NORMAL);
5966 if (decl && DECL_P (decl))
5967 DECL_SOURCE_LOCATION (decl) = parm->loc;
5969 attrs = get_parm_array_spec (parm, attrs);
5970 decl_attributes (&decl, attrs, 0);
5972 decl = pushdecl (decl);
5974 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5977 /* Mark all the parameter declarations to date as forward decls.
5978 Also diagnose use of this extension. */
5981 mark_forward_parm_decls (void)
5983 struct c_binding *b;
5985 if (pedantic && !current_scope->warned_forward_parm_decls)
5987 pedwarn (input_location, OPT_Wpedantic,
5988 "ISO C forbids forward parameter declarations");
5989 current_scope->warned_forward_parm_decls = true;
5992 for (b = current_scope->bindings; b; b = b->prev)
5993 if (TREE_CODE (b->decl) == PARM_DECL)
5994 TREE_ASM_WRITTEN (b->decl) = 1;
5997 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5998 literal, which may be an incomplete array type completed by the
5999 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
6000 literal. NON_CONST is true if the initializers contain something
6001 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
6002 it is the (valid) alignment for this compound literal, as specified
6006 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
6007 unsigned int alignas_align)
6009 /* We do not use start_decl here because we have a type, not a declarator;
6010 and do not use finish_decl because the decl should be stored inside
6011 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
6016 if (type == error_mark_node
6017 || init == error_mark_node)
6018 return error_mark_node;
6020 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
6021 DECL_EXTERNAL (decl) = 0;
6022 TREE_PUBLIC (decl) = 0;
6023 TREE_STATIC (decl) = (current_scope == file_scope);
6024 DECL_CONTEXT (decl) = current_function_decl;
6025 TREE_USED (decl) = 1;
6026 DECL_READ_P (decl) = 1;
6027 DECL_ARTIFICIAL (decl) = 1;
6028 DECL_IGNORED_P (decl) = 1;
6029 C_DECL_COMPOUND_LITERAL_P (decl) = 1;
6030 TREE_TYPE (decl) = type;
6031 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
6034 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6035 DECL_USER_ALIGN (decl) = 1;
6037 store_init_value (loc, decl, init, NULL_TREE);
6039 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
6041 int failure = complete_array_type (&TREE_TYPE (decl),
6042 DECL_INITIAL (decl), true);
6043 /* If complete_array_type returns 3, it means that the
6044 initial value of the compound literal is empty. Allow it. */
6045 gcc_assert (failure == 0 || failure == 3);
6047 type = TREE_TYPE (decl);
6048 TREE_TYPE (DECL_INITIAL (decl)) = type;
6051 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
6053 c_incomplete_type_error (loc, NULL_TREE, type);
6054 return error_mark_node;
6057 if (TREE_STATIC (decl)
6058 && !verify_type_context (loc, TCTX_STATIC_STORAGE, type))
6059 return error_mark_node;
6061 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
6062 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
6063 TREE_SIDE_EFFECTS (complit) = 1;
6065 layout_decl (decl, 0);
6067 if (TREE_STATIC (decl))
6069 /* This decl needs a name for the assembler output. */
6070 set_compound_literal_name (decl);
6071 DECL_DEFER_OUTPUT (decl) = 1;
6072 DECL_COMDAT (decl) = 1;
6074 rest_of_decl_compilation (decl, 1, 0);
6076 else if (current_function_decl && !current_scope->parm_flag)
6081 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
6082 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
6088 /* Check the type of a compound literal. Here we just check that it
6089 is valid for C++. */
6092 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
6095 && (type_name->specs->typespec_kind == ctsk_tagdef
6096 || type_name->specs->typespec_kind == ctsk_tagfirstref
6097 || type_name->specs->typespec_kind == ctsk_tagfirstref_attrs))
6098 warning_at (loc, OPT_Wc___compat,
6099 "defining a type in a compound literal is invalid in C++");
6102 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
6103 replacing with appropriate values if they are invalid. */
6106 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
6110 unsigned int max_width;
6111 unsigned HOST_WIDE_INT w;
6112 const char *name = (orig_name
6113 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
6114 : _("<anonymous>"));
6116 /* Detect and ignore out of range field width and process valid
6118 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
6120 error_at (loc, "bit-field %qs width not an integer constant", name);
6121 *width = integer_one_node;
6125 if (TREE_CODE (*width) != INTEGER_CST)
6127 *width = c_fully_fold (*width, false, NULL);
6128 if (TREE_CODE (*width) == INTEGER_CST)
6129 pedwarn (loc, OPT_Wpedantic,
6130 "bit-field %qs width not an integer constant expression",
6133 if (TREE_CODE (*width) != INTEGER_CST)
6135 error_at (loc, "bit-field %qs width not an integer constant", name);
6136 *width = integer_one_node;
6138 constant_expression_warning (*width);
6139 if (tree_int_cst_sgn (*width) < 0)
6141 error_at (loc, "negative width in bit-field %qs", name);
6142 *width = integer_one_node;
6144 else if (integer_zerop (*width) && orig_name)
6146 error_at (loc, "zero width for bit-field %qs", name);
6147 *width = integer_one_node;
6151 /* Detect invalid bit-field type. */
6152 if (TREE_CODE (*type) != INTEGER_TYPE
6153 && TREE_CODE (*type) != BOOLEAN_TYPE
6154 && TREE_CODE (*type) != ENUMERAL_TYPE)
6156 error_at (loc, "bit-field %qs has invalid type", name);
6157 *type = unsigned_type_node;
6160 if (TYPE_WARN_IF_NOT_ALIGN (*type))
6162 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
6164 *type = unsigned_type_node;
6167 type_mv = TYPE_MAIN_VARIANT (*type);
6168 if (!in_system_header_at (input_location)
6169 && type_mv != integer_type_node
6170 && type_mv != unsigned_type_node
6171 && type_mv != boolean_type_node)
6172 pedwarn_c90 (loc, OPT_Wpedantic,
6173 "type of bit-field %qs is a GCC extension", name);
6175 max_width = TYPE_PRECISION (*type);
6177 if (compare_tree_int (*width, max_width) > 0)
6179 error_at (loc, "width of %qs exceeds its type", name);
6181 *width = build_int_cst (integer_type_node, w);
6184 w = tree_to_uhwi (*width);
6186 if (TREE_CODE (*type) == ENUMERAL_TYPE)
6188 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
6190 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
6191 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
6192 warning_at (loc, 0, "%qs is narrower than values of its type", name);
6198 /* Print warning about variable length array if necessary. */
6201 warn_variable_length_array (tree name, tree size)
6203 if (TREE_CONSTANT (size))
6206 pedwarn_c90 (input_location, OPT_Wvla,
6207 "ISO C90 forbids array %qE whose size "
6208 "cannot be evaluated", name);
6210 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
6211 "whose size cannot be evaluated");
6216 pedwarn_c90 (input_location, OPT_Wvla,
6217 "ISO C90 forbids variable length array %qE", name);
6219 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
6224 /* Print warning about defaulting to int if necessary. */
6227 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
6229 diagnostic_info diagnostic;
6231 rich_location richloc (line_table, location);
6233 va_start (ap, gmsgid);
6234 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
6235 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
6236 diagnostic.option_index = opt;
6237 diagnostic_report_diagnostic (global_dc, &diagnostic);
6241 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
6242 considering only those c_declspec_words found in LIST, which
6243 must be terminated by cdw_number_of_elements. */
6246 smallest_type_quals_location (const location_t *locations,
6247 const c_declspec_word *list)
6249 location_t loc = UNKNOWN_LOCATION;
6250 while (*list != cdw_number_of_elements)
6252 location_t newloc = locations[*list];
6253 if (loc == UNKNOWN_LOCATION
6254 || (newloc != UNKNOWN_LOCATION && newloc < loc))
6262 /* Given declspecs and a declarator,
6263 determine the name and type of the object declared
6264 and construct a ..._DECL node for it.
6265 (In one case we can return a ..._TYPE node instead.
6266 For invalid input we sometimes return NULL_TREE.)
6268 DECLSPECS is a c_declspecs structure for the declaration specifiers.
6270 DECL_CONTEXT says which syntactic context this declaration is in:
6271 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6272 FUNCDEF for a function definition. Like NORMAL but a few different
6273 error messages in each case. Return value may be zero meaning
6274 this definition is too screwy to try to parse.
6275 PARM for a parameter declaration (either within a function prototype
6276 or before a function body). Make a PARM_DECL, or return void_type_node.
6277 TYPENAME if for a typename (in a cast or sizeof).
6278 Don't make a DECL node; just return the ..._TYPE node.
6279 FIELD for a struct or union field; make a FIELD_DECL.
6280 INITIALIZED is true if the decl has an initializer.
6281 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
6282 representing the width of the bit-field.
6283 DECL_ATTRS points to the list of attributes that should be added to this
6284 decl. Any nested attributes that belong on the decl itself will be
6286 If EXPR is not NULL, any expressions that need to be evaluated as
6287 part of evaluating variably modified types will be stored in *EXPR.
6288 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
6289 set to indicate whether operands in *EXPR can be used in constant
6291 DEPRECATED_STATE is a deprecated_states value indicating whether
6292 deprecation/unavailability warnings should be suppressed.
6294 In the TYPENAME case, DECLARATOR is really an absolute declarator.
6295 It may also be so in the PARM case, for a prototype where the
6296 argument type is specified but not the name.
6298 This function is where the complicated C meanings of `static'
6299 and `extern' are interpreted. */
6302 grokdeclarator (const struct c_declarator *declarator,
6303 struct c_declspecs *declspecs,
6304 enum decl_context decl_context, bool initialized, tree *width,
6305 tree *decl_attrs, tree *expr, bool *expr_const_operands,
6306 enum deprecated_states deprecated_state)
6308 tree type = declspecs->type;
6309 bool threadp = declspecs->thread_p;
6310 enum c_storage_class storage_class = declspecs->storage_class;
6315 int type_quals = TYPE_UNQUALIFIED;
6316 tree name = NULL_TREE;
6317 bool funcdef_flag = false;
6318 bool funcdef_syntax = false;
6319 bool size_varies = false;
6320 tree decl_attr = declspecs->decl_attr;
6321 int array_ptr_quals = TYPE_UNQUALIFIED;
6322 tree array_ptr_attrs = NULL_TREE;
6323 bool array_parm_static = false;
6324 bool array_parm_vla_unspec_p = false;
6325 tree returned_attrs = NULL_TREE;
6326 tree decl_id_attrs = NULL_TREE;
6327 bool bitfield = width != NULL;
6329 tree orig_qual_type = NULL;
6330 size_t orig_qual_indirect = 0;
6331 struct c_arg_info *arg_info = 0;
6332 addr_space_t as1, as2, address_space;
6333 location_t loc = UNKNOWN_LOCATION;
6335 bool expr_const_operands_dummy;
6336 enum c_declarator_kind first_non_attr_kind;
6337 unsigned int alignas_align = 0;
6339 if (TREE_CODE (type) == ERROR_MARK)
6340 return error_mark_node;
6344 expr_dummy = NULL_TREE;
6346 if (expr_const_operands == NULL)
6347 expr_const_operands = &expr_const_operands_dummy;
6349 if (declspecs->expr)
6352 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
6355 *expr = declspecs->expr;
6357 *expr_const_operands = declspecs->expr_const_operands;
6359 if (decl_context == FUNCDEF)
6360 funcdef_flag = true, decl_context = NORMAL;
6362 /* Look inside a declarator for the name being declared
6363 and get it as an IDENTIFIER_NODE, for an error message. */
6365 const struct c_declarator *decl = declarator;
6367 first_non_attr_kind = cdk_attrs;
6377 funcdef_syntax = (decl->kind == cdk_function);
6378 if (first_non_attr_kind == cdk_attrs)
6379 first_non_attr_kind = decl->kind;
6380 decl = decl->declarator;
6384 decl = decl->declarator;
6390 name = decl->u.id.id;
6391 decl_id_attrs = decl->u.id.attrs;
6392 if (first_non_attr_kind == cdk_attrs)
6393 first_non_attr_kind = decl->kind;
6400 if (name == NULL_TREE)
6402 gcc_assert (decl_context == PARM
6403 || decl_context == TYPENAME
6404 || (decl_context == FIELD
6405 && declarator->kind == cdk_id));
6406 gcc_assert (!initialized);
6410 /* A function definition's declarator must have the form of
6411 a function declarator. */
6413 if (funcdef_flag && !funcdef_syntax)
6416 /* If this looks like a function definition, make it one,
6417 even if it occurs where parms are expected.
6418 Then store_parm_decls will reject it and not use it as a parm. */
6419 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
6420 decl_context = PARM;
6422 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
6424 if (declspecs->unavailable_p)
6425 error_unavailable_use (declspecs->type, declspecs->decl_attr);
6426 else if (declspecs->deprecated_p
6427 && deprecated_state != DEPRECATED_SUPPRESS)
6428 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
6431 if ((decl_context == NORMAL || decl_context == FIELD)
6432 && current_scope == file_scope
6433 && variably_modified_type_p (type, NULL_TREE))
6436 error_at (loc, "variably modified %qE at file scope", name);
6438 error_at (loc, "variably modified field at file scope");
6439 type = integer_type_node;
6442 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
6444 /* Diagnose defaulting to "int". */
6446 if (declspecs->default_int_p && !in_system_header_at (input_location))
6448 /* Issue a warning if this is an ISO C 99 program or if
6449 -Wreturn-type and this is a function, or if -Wimplicit;
6450 prefer the former warning since it is more explicit. */
6451 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
6453 warn_about_return_type = 1;
6457 warn_defaults_to (loc, OPT_Wimplicit_int,
6458 "type defaults to %<int%> in declaration "
6461 warn_defaults_to (loc, OPT_Wimplicit_int,
6462 "type defaults to %<int%> in type name");
6466 /* Adjust the type if a bit-field is being declared,
6467 -funsigned-bitfields applied and the type is not explicitly
6469 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
6470 && TREE_CODE (type) == INTEGER_TYPE)
6471 type = unsigned_type_for (type);
6473 /* Figure out the type qualifiers for the declaration. There are
6474 two ways a declaration can become qualified. One is something
6475 like `const int i' where the `const' is explicit. Another is
6476 something like `typedef const int CI; CI i' where the type of the
6477 declaration contains the `const'. A third possibility is that
6478 there is a type qualifier on the element type of a typedefed
6479 array type, in which case we should extract that qualifier so
6480 that c_apply_type_quals_to_decl receives the full list of
6481 qualifiers to work with (C90 is not entirely clear about whether
6482 duplicate qualifiers should be diagnosed in this case, but it
6483 seems most appropriate to do so). */
6484 element_type = strip_array_types (type);
6485 constp = declspecs->const_p + TYPE_READONLY (element_type);
6486 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
6487 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
6488 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
6489 as1 = declspecs->address_space;
6490 as2 = TYPE_ADDR_SPACE (element_type);
6491 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
6494 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
6496 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
6498 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
6500 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
6502 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
6503 error_at (loc, "conflicting named address spaces (%s vs %s)",
6504 c_addr_space_name (as1), c_addr_space_name (as2));
6506 if ((TREE_CODE (type) == ARRAY_TYPE
6507 || first_non_attr_kind == cdk_array)
6508 && TYPE_QUALS (element_type))
6510 orig_qual_type = type;
6511 type = TYPE_MAIN_VARIANT (type);
6513 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
6514 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
6515 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
6516 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
6517 | ENCODE_QUAL_ADDR_SPACE (address_space));
6518 if (type_quals != TYPE_QUALS (element_type))
6519 orig_qual_type = NULL_TREE;
6521 /* Applying the _Atomic qualifier to an array type (through the use
6522 of typedefs or typeof) must be detected here. If the qualifier
6523 is introduced later, any appearance of applying it to an array is
6524 actually applying it to an element of that array. */
6525 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
6526 error_at (loc, "%<_Atomic%>-qualified array type");
6528 /* Warn about storage classes that are invalid for certain
6529 kinds of declarations (parameters, typenames, etc.). */
6533 || storage_class == csc_auto
6534 || storage_class == csc_register
6535 || storage_class == csc_typedef))
6537 if (storage_class == csc_auto)
6539 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
6540 "function definition declared %<auto%>");
6541 if (storage_class == csc_register)
6542 error_at (loc, "function definition declared %<register%>");
6543 if (storage_class == csc_typedef)
6544 error_at (loc, "function definition declared %<typedef%>");
6546 error_at (loc, "function definition declared %qs",
6547 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6549 if (storage_class == csc_auto
6550 || storage_class == csc_register
6551 || storage_class == csc_typedef)
6552 storage_class = csc_none;
6554 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
6556 if (decl_context == PARM && storage_class == csc_register)
6560 switch (decl_context)
6564 error_at (loc, "storage class specified for structure "
6567 error_at (loc, "storage class specified for structure field");
6571 error_at (loc, "storage class specified for parameter %qE",
6574 error_at (loc, "storage class specified for unnamed parameter");
6577 error_at (loc, "storage class specified for typename");
6580 storage_class = csc_none;
6584 else if (storage_class == csc_extern
6588 /* 'extern' with initialization is invalid if not at file scope. */
6589 if (current_scope == file_scope)
6591 /* It is fine to have 'extern const' when compiling at C
6592 and C++ intersection. */
6593 if (!(warn_cxx_compat && constp))
6594 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
6598 error_at (loc, "%qE has both %<extern%> and initializer", name);
6600 else if (current_scope == file_scope)
6602 if (storage_class == csc_auto)
6603 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
6605 if (pedantic && storage_class == csc_register)
6606 pedwarn (input_location, OPT_Wpedantic,
6607 "file-scope declaration of %qE specifies %<register%>", name);
6611 if (storage_class == csc_extern && funcdef_flag)
6612 error_at (loc, "nested function %qE declared %<extern%>", name);
6613 else if (threadp && storage_class == csc_none)
6615 error_at (loc, "function-scope %qE implicitly auto and declared "
6617 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
6622 /* Now figure out the structure of the declarator proper.
6623 Descend through it, creating more complex types, until we reach
6624 the declared identifier (or NULL_TREE, in an absolute declarator).
6625 At each stage we maintain an unqualified version of the type
6626 together with any qualifiers that should be applied to it with
6627 c_build_qualified_type; this way, array types including
6628 multidimensional array types are first built up in unqualified
6629 form and then the qualified form is created with
6630 TYPE_MAIN_VARIANT pointing to the unqualified form. */
6632 while (declarator && declarator->kind != cdk_id)
6634 if (type == error_mark_node)
6636 declarator = declarator->declarator;
6640 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
6641 a cdk_pointer (for *...),
6642 a cdk_function (for ...(...)),
6643 a cdk_attrs (for nested attributes),
6644 or a cdk_id (for the name being declared
6645 or the place in an absolute declarator
6646 where the name was omitted).
6647 For the last case, we have just exited the loop.
6649 At this point, TYPE is the type of elements of an array,
6650 or for a function to return, or for a pointer to point to.
6651 After this sequence of ifs, TYPE is the type of the
6652 array or function or pointer, and DECLARATOR has had its
6653 outermost layer removed. */
6655 if (array_ptr_quals != TYPE_UNQUALIFIED
6656 || array_ptr_attrs != NULL_TREE
6657 || array_parm_static)
6659 /* Only the innermost declarator (making a parameter be of
6660 array type which is converted to pointer type)
6661 may have static or type qualifiers. */
6662 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6663 array_ptr_quals = TYPE_UNQUALIFIED;
6664 array_ptr_attrs = NULL_TREE;
6665 array_parm_static = false;
6668 switch (declarator->kind)
6672 /* A declarator with embedded attributes. */
6673 tree attrs = declarator->u.attrs;
6674 const struct c_declarator *inner_decl;
6676 declarator = declarator->declarator;
6677 /* Standard attribute syntax precisely defines what entity
6678 an attribute in each position appertains to, so only
6679 apply laxity about positioning to GNU attribute syntax.
6680 Standard attributes applied to a function or array
6681 declarator apply exactly to that type; standard
6682 attributes applied to the identifier apply to the
6683 declaration rather than to the type, and are specified
6684 using a cdk_id declarator rather than using
6686 inner_decl = declarator;
6687 while (inner_decl->kind == cdk_attrs)
6688 inner_decl = inner_decl->declarator;
6689 if (!cxx11_attribute_p (attrs))
6691 if (inner_decl->kind == cdk_id)
6692 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6693 else if (inner_decl->kind == cdk_function)
6694 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6695 else if (inner_decl->kind == cdk_array)
6696 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6698 attrs = c_warn_type_attributes (attrs);
6699 returned_attrs = decl_attributes (&type,
6700 chainon (returned_attrs, attrs),
6706 tree itype = NULL_TREE;
6707 tree size = declarator->u.array.dimen;
6708 /* The index is a signed object `sizetype' bits wide. */
6709 tree index_type = c_common_signed_type (sizetype);
6711 array_ptr_quals = declarator->u.array.quals;
6712 array_ptr_attrs = declarator->u.array.attrs;
6713 array_parm_static = declarator->u.array.static_p;
6714 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6716 declarator = declarator->declarator;
6718 /* Check for some types that there cannot be arrays of. */
6720 if (VOID_TYPE_P (type))
6723 error_at (loc, "declaration of %qE as array of voids", name);
6725 error_at (loc, "declaration of type name as array of voids");
6726 type = error_mark_node;
6729 if (TREE_CODE (type) == FUNCTION_TYPE)
6732 error_at (loc, "declaration of %qE as array of functions",
6735 error_at (loc, "declaration of type name as array of "
6737 type = error_mark_node;
6740 if (pedantic && !in_system_header_at (input_location)
6741 && flexible_array_type_p (type))
6742 pedwarn (loc, OPT_Wpedantic,
6743 "invalid use of structure with flexible array member");
6745 if (size == error_mark_node)
6746 type = error_mark_node;
6748 if (type == error_mark_node)
6751 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, type))
6753 type = error_mark_node;
6757 /* If size was specified, set ITYPE to a range-type for
6758 that size. Otherwise, ITYPE remains null. finish_decl
6759 may figure it out from an initial value. */
6763 bool size_maybe_const = true;
6764 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6765 && !TREE_OVERFLOW (size));
6766 bool this_size_varies = false;
6768 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6770 STRIP_TYPE_NOPS (size);
6772 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6775 error_at (loc, "size of array %qE has non-integer type",
6779 "size of unnamed array has non-integer type");
6780 size = integer_one_node;
6781 size_int_const = true;
6783 /* This can happen with enum forward declaration. */
6784 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6787 error_at (loc, "size of array %qE has incomplete type",
6790 error_at (loc, "size of unnamed array has incomplete "
6792 size = integer_one_node;
6793 size_int_const = true;
6796 size = c_fully_fold (size, false, &size_maybe_const);
6798 if (pedantic && size_maybe_const && integer_zerop (size))
6801 pedwarn (loc, OPT_Wpedantic,
6802 "ISO C forbids zero-size array %qE", name);
6804 pedwarn (loc, OPT_Wpedantic,
6805 "ISO C forbids zero-size array");
6808 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6810 constant_expression_warning (size);
6811 if (tree_int_cst_sgn (size) < 0)
6814 error_at (loc, "size of array %qE is negative", name);
6816 error_at (loc, "size of unnamed array is negative");
6817 size = integer_one_node;
6818 size_int_const = true;
6820 /* Handle a size folded to an integer constant but
6821 not an integer constant expression. */
6822 if (!size_int_const)
6824 /* If this is a file scope declaration of an
6825 ordinary identifier, this is invalid code;
6826 diagnosing it here and not subsequently
6827 treating the type as variable-length avoids
6828 more confusing diagnostics later. */
6829 if ((decl_context == NORMAL || decl_context == FIELD)
6830 && current_scope == file_scope)
6831 pedwarn (input_location, 0,
6832 "variably modified %qE at file scope",
6835 this_size_varies = size_varies = true;
6836 warn_variable_length_array (name, size);
6839 else if ((decl_context == NORMAL || decl_context == FIELD)
6840 && current_scope == file_scope)
6842 error_at (loc, "variably modified %qE at file scope", name);
6843 size = integer_one_node;
6847 /* Make sure the array size remains visibly
6848 nonconstant even if it is (eg) a const variable
6849 with known value. */
6850 this_size_varies = size_varies = true;
6851 warn_variable_length_array (name, size);
6852 if (sanitize_flags_p (SANITIZE_VLA)
6853 && current_function_decl != NULL_TREE
6854 && decl_context == NORMAL)
6856 /* Evaluate the array size only once. */
6857 size = save_expr (size);
6858 size = c_fully_fold (size, false, NULL);
6859 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6860 ubsan_instrument_vla (loc, size),
6865 if (integer_zerop (size) && !this_size_varies)
6867 /* A zero-length array cannot be represented with
6868 an unsigned index type, which is what we'll
6869 get with build_index_type. Create an
6870 open-ended range instead. */
6871 itype = build_range_type (sizetype, size, NULL_TREE);
6875 /* Arrange for the SAVE_EXPR on the inside of the
6876 MINUS_EXPR, which allows the -1 to get folded
6877 with the +1 that happens when building TYPE_SIZE. */
6879 size = save_expr (size);
6880 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6881 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6882 integer_zero_node, size);
6884 /* Compute the maximum valid index, that is, size
6885 - 1. Do the calculation in index_type, so that
6886 if it is a variable the computations will be
6887 done in the proper mode. */
6888 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6889 convert (index_type, size),
6890 convert (index_type,
6893 /* The above overflows when size does not fit
6895 ??? While a size of INT_MAX+1 technically shouldn't
6896 cause an overflow (because we subtract 1), handling
6897 this case seems like an unnecessary complication. */
6898 if (TREE_CODE (size) == INTEGER_CST
6899 && !int_fits_type_p (size, index_type))
6902 error_at (loc, "size of array %qE is too large",
6905 error_at (loc, "size of unnamed array is too large");
6906 type = error_mark_node;
6910 itype = build_index_type (itype);
6912 if (this_size_varies)
6914 if (TREE_SIDE_EFFECTS (size))
6917 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6922 *expr_const_operands &= size_maybe_const;
6925 else if (decl_context == FIELD)
6927 bool flexible_array_member = false;
6928 if (array_parm_vla_unspec_p)
6929 /* Field names can in fact have function prototype
6930 scope so [*] is disallowed here through making
6931 the field variably modified, not through being
6932 something other than a declaration with function
6937 const struct c_declarator *t = declarator;
6938 while (t->kind == cdk_attrs)
6940 flexible_array_member = (t->kind == cdk_id);
6942 if (flexible_array_member
6943 && !in_system_header_at (input_location))
6944 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6945 "support flexible array members");
6947 /* ISO C99 Flexible array members are effectively
6948 identical to GCC's zero-length array extension. */
6949 if (flexible_array_member || array_parm_vla_unspec_p)
6950 itype = build_range_type (sizetype, size_zero_node,
6953 else if (decl_context == PARM)
6955 if (array_parm_vla_unspec_p)
6957 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6961 else if (decl_context == TYPENAME)
6963 if (array_parm_vla_unspec_p)
6966 warning (0, "%<[*]%> not in a declaration");
6967 /* We use this to avoid messing up with incomplete
6968 array types of the same type, that would
6969 otherwise be modified below. */
6970 itype = build_range_type (sizetype, size_zero_node,
6976 /* Complain about arrays of incomplete types. */
6977 if (!COMPLETE_TYPE_P (type))
6979 auto_diagnostic_group d;
6980 error_at (loc, "array type has incomplete element type %qT",
6982 /* See if we can be more helpful. */
6983 if (TREE_CODE (type) == ARRAY_TYPE)
6986 inform (loc, "declaration of %qE as multidimensional "
6987 "array must have bounds for all dimensions "
6988 "except the first", name);
6990 inform (loc, "declaration of multidimensional array "
6991 "must have bounds for all dimensions except "
6994 type = error_mark_node;
6997 /* When itype is NULL, a shared incomplete array type is
6998 returned for all array of a given type. Elsewhere we
6999 make sure we don't complete that type before copying
7000 it, but here we want to make sure we don't ever
7001 modify the shared type, so we gcc_assert (itype)
7004 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
7005 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
7006 type = build_qualified_type (type,
7007 ENCODE_QUAL_ADDR_SPACE (as));
7009 type = build_array_type (type, itype);
7012 if (type != error_mark_node)
7016 /* It is ok to modify type here even if itype is
7017 NULL: if size_varies, we're in a
7018 multi-dimensional array and the inner type has
7019 variable size, so the enclosing shared array type
7021 if (size && TREE_CODE (size) == INTEGER_CST)
7023 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7024 C_TYPE_VARIABLE_SIZE (type) = 1;
7027 /* The GCC extension for zero-length arrays differs from
7028 ISO flexible array members in that sizeof yields
7030 if (size && integer_zerop (size))
7033 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7034 TYPE_SIZE (type) = bitsize_zero_node;
7035 TYPE_SIZE_UNIT (type) = size_zero_node;
7036 SET_TYPE_STRUCTURAL_EQUALITY (type);
7038 if (array_parm_vla_unspec_p)
7041 /* The type is complete. C99 6.7.5.2p4 */
7042 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7043 TYPE_SIZE (type) = bitsize_zero_node;
7044 TYPE_SIZE_UNIT (type) = size_zero_node;
7045 SET_TYPE_STRUCTURAL_EQUALITY (type);
7048 if (!valid_array_size_p (loc, type, name))
7049 type = error_mark_node;
7052 if (decl_context != PARM
7053 && (array_ptr_quals != TYPE_UNQUALIFIED
7054 || array_ptr_attrs != NULL_TREE
7055 || array_parm_static))
7057 error_at (loc, "static or type qualifiers in non-parameter "
7058 "array declarator");
7059 array_ptr_quals = TYPE_UNQUALIFIED;
7060 array_ptr_attrs = NULL_TREE;
7061 array_parm_static = false;
7063 orig_qual_indirect++;
7068 /* Say it's a definition only for the declarator closest
7069 to the identifier, apart possibly from some
7071 bool really_funcdef = false;
7073 orig_qual_type = NULL_TREE;
7076 const struct c_declarator *t = declarator->declarator;
7077 while (t->kind == cdk_attrs)
7079 really_funcdef = (t->kind == cdk_id);
7082 /* Declaring a function type. Make sure we have a valid
7083 type for the function to return. */
7084 if (type == error_mark_node)
7087 size_varies = false;
7089 /* Warn about some types functions can't return. */
7090 if (TREE_CODE (type) == FUNCTION_TYPE)
7093 error_at (loc, "%qE declared as function returning a "
7096 error_at (loc, "type name declared as function "
7097 "returning a function");
7098 type = integer_type_node;
7100 if (TREE_CODE (type) == ARRAY_TYPE)
7103 error_at (loc, "%qE declared as function returning an array",
7106 error_at (loc, "type name declared as function returning "
7108 type = integer_type_node;
7111 /* Construct the function type and go to the next
7112 inner layer of declarator. */
7113 arg_info = declarator->u.arg_info;
7114 arg_types = grokparms (arg_info, really_funcdef);
7116 /* Type qualifiers before the return type of the function
7117 qualify the return type, not the function type. */
7120 const enum c_declspec_word ignored_quals_list[] =
7122 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
7123 cdw_atomic, cdw_number_of_elements
7125 location_t specs_loc
7126 = smallest_type_quals_location (declspecs->locations,
7127 ignored_quals_list);
7128 if (specs_loc == UNKNOWN_LOCATION)
7129 specs_loc = declspecs->locations[cdw_typedef];
7130 if (specs_loc == UNKNOWN_LOCATION)
7133 /* Type qualifiers on a function return type are
7134 normally permitted by the standard but have no
7135 effect, so give a warning at -Wreturn-type.
7136 Qualifiers on a void return type are banned on
7137 function definitions in ISO C; GCC used to used
7138 them for noreturn functions. The resolution of C11
7139 DR#423 means qualifiers (other than _Atomic) are
7140 actually removed from the return type when
7141 determining the function type. */
7142 int quals_used = type_quals;
7144 quals_used &= TYPE_QUAL_ATOMIC;
7145 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
7146 pedwarn (specs_loc, 0,
7147 "function definition has qualified void "
7150 warning_at (specs_loc, OPT_Wignored_qualifiers,
7151 "type qualifiers ignored on function "
7154 /* Ensure an error for restrict on invalid types; the
7155 DR#423 resolution is not entirely clear about
7158 && (type_quals & TYPE_QUAL_RESTRICT)
7159 && (!POINTER_TYPE_P (type)
7160 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
7161 error_at (loc, "invalid use of %<restrict%>");
7162 type = c_build_qualified_type (type, quals_used);
7164 type_quals = TYPE_UNQUALIFIED;
7166 type = build_function_type (type, arg_types);
7167 declarator = declarator->declarator;
7169 /* Set the TYPE_CONTEXTs for each tagged type which is local to
7170 the formal parameter list of this FUNCTION_TYPE to point to
7171 the FUNCTION_TYPE node itself. */
7176 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
7177 TYPE_CONTEXT (tag->type) = type;
7183 /* Merge any constancy or volatility into the target type
7185 if ((type_quals & TYPE_QUAL_ATOMIC)
7186 && TREE_CODE (type) == FUNCTION_TYPE)
7189 "%<_Atomic%>-qualified function type");
7190 type_quals &= ~TYPE_QUAL_ATOMIC;
7192 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7194 pedwarn (loc, OPT_Wpedantic,
7195 "ISO C forbids qualified function types");
7197 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7198 orig_qual_indirect);
7199 orig_qual_type = NULL_TREE;
7200 size_varies = false;
7202 /* When the pointed-to type involves components of variable size,
7203 care must be taken to ensure that the size evaluation code is
7204 emitted early enough to dominate all the possible later uses
7205 and late enough for the variables on which it depends to have
7208 This is expected to happen automatically when the pointed-to
7209 type has a name/declaration of it's own, but special attention
7210 is required if the type is anonymous.
7212 We attach an artificial TYPE_DECL to such pointed-to type
7213 and arrange for it to be included in a DECL_EXPR. This
7214 forces the sizes evaluation at a safe point and ensures it
7215 is not deferred until e.g. within a deeper conditional context.
7217 PARM contexts have no enclosing statement list that
7218 can hold the DECL_EXPR, so we need to use a BIND_EXPR
7219 instead, and add it to the list of expressions that
7220 need to be evaluated.
7222 TYPENAME contexts do have an enclosing statement list,
7223 but it would be incorrect to use it, as the size should
7224 only be evaluated if the containing expression is
7225 evaluated. We might also be in the middle of an
7226 expression with side effects on the pointed-to type size
7227 "arguments" prior to the pointer declaration point and
7228 the fake TYPE_DECL in the enclosing context would force
7229 the size evaluation prior to the side effects. We therefore
7230 use BIND_EXPRs in TYPENAME contexts too. */
7231 if (!TYPE_NAME (type)
7232 && variably_modified_type_p (type, NULL_TREE))
7234 tree bind = NULL_TREE;
7235 if (decl_context == TYPENAME || decl_context == PARM)
7237 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
7238 NULL_TREE, NULL_TREE);
7239 TREE_SIDE_EFFECTS (bind) = 1;
7240 BIND_EXPR_BODY (bind) = push_stmt_list ();
7243 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
7244 DECL_ARTIFICIAL (decl) = 1;
7246 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7247 TYPE_NAME (type) = decl;
7251 BIND_EXPR_BODY (bind)
7252 = pop_stmt_list (BIND_EXPR_BODY (bind));
7254 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
7261 type = c_build_pointer_type (type);
7263 /* Process type qualifiers (such as const or volatile)
7264 that were given inside the `*'. */
7265 type_quals = declarator->u.pointer_quals;
7267 declarator = declarator->declarator;
7274 *decl_attrs = chainon (returned_attrs, *decl_attrs);
7275 *decl_attrs = chainon (decl_id_attrs, *decl_attrs);
7277 /* Now TYPE has the actual type, apart from any qualifiers in
7280 /* Warn about address space used for things other than static memory or
7282 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
7283 if (!ADDR_SPACE_GENERIC_P (address_space))
7285 if (decl_context == NORMAL)
7287 switch (storage_class)
7290 error ("%qs combined with %<auto%> qualifier for %qE",
7291 c_addr_space_name (address_space), name);
7294 error ("%qs combined with %<register%> qualifier for %qE",
7295 c_addr_space_name (address_space), name);
7298 if (current_function_scope)
7300 error ("%qs specified for auto variable %qE",
7301 c_addr_space_name (address_space), name);
7313 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
7316 error ("%qs specified for parameter %qE",
7317 c_addr_space_name (address_space), name);
7319 error ("%qs specified for unnamed parameter",
7320 c_addr_space_name (address_space));
7322 else if (decl_context == FIELD)
7325 error ("%qs specified for structure field %qE",
7326 c_addr_space_name (address_space), name);
7328 error ("%qs specified for structure field",
7329 c_addr_space_name (address_space));
7333 /* Check the type and width of a bit-field. */
7336 check_bitfield_type_and_width (loc, &type, width, name);
7337 /* C11 makes it implementation-defined (6.7.2.1#5) whether
7338 atomic types are permitted for bit-fields; we have no code to
7339 make bit-field accesses atomic, so disallow them. */
7340 if (type_quals & TYPE_QUAL_ATOMIC)
7343 error_at (loc, "bit-field %qE has atomic type", name);
7345 error_at (loc, "bit-field has atomic type");
7346 type_quals &= ~TYPE_QUAL_ATOMIC;
7350 /* Reject invalid uses of _Alignas. */
7351 if (declspecs->alignas_p)
7353 if (storage_class == csc_typedef)
7354 error_at (loc, "alignment specified for typedef %qE", name);
7355 else if (storage_class == csc_register)
7356 error_at (loc, "alignment specified for %<register%> object %qE",
7358 else if (decl_context == PARM)
7361 error_at (loc, "alignment specified for parameter %qE", name);
7363 error_at (loc, "alignment specified for unnamed parameter");
7368 error_at (loc, "alignment specified for bit-field %qE", name);
7370 error_at (loc, "alignment specified for unnamed bit-field");
7372 else if (TREE_CODE (type) == FUNCTION_TYPE)
7373 error_at (loc, "alignment specified for function %qE", name);
7374 else if (declspecs->align_log != -1 && TYPE_P (type))
7376 alignas_align = 1U << declspecs->align_log;
7377 if (alignas_align < min_align_of_type (type))
7380 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7381 "alignment of %qE", name);
7383 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
7384 "alignment of unnamed field");
7390 /* If this is declaring a typedef name, return a TYPE_DECL. */
7392 if (storage_class == csc_typedef)
7395 if ((type_quals & TYPE_QUAL_ATOMIC)
7396 && TREE_CODE (type) == FUNCTION_TYPE)
7399 "%<_Atomic%>-qualified function type");
7400 type_quals &= ~TYPE_QUAL_ATOMIC;
7402 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7404 pedwarn (loc, OPT_Wpedantic,
7405 "ISO C forbids qualified function types");
7407 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7408 orig_qual_indirect);
7409 decl = build_decl (declarator->id_loc,
7410 TYPE_DECL, declarator->u.id.id, type);
7411 if (declspecs->explicit_signed_p)
7412 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
7413 if (declspecs->inline_p)
7414 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
7415 if (declspecs->noreturn_p)
7416 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
7418 if (warn_cxx_compat && declarator->u.id.id != NULL_TREE)
7420 struct c_binding *b = I_TAG_BINDING (declarator->u.id.id);
7423 && b->decl != NULL_TREE
7424 && (B_IN_CURRENT_SCOPE (b)
7425 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
7426 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
7428 auto_diagnostic_group d;
7429 if (warning_at (declarator->id_loc, OPT_Wc___compat,
7430 ("using %qD as both a typedef and a tag is "
7431 "invalid in C++"), decl)
7432 && b->locus != UNKNOWN_LOCATION)
7433 inform (b->locus, "originally defined here");
7440 /* If this is a type name (such as, in a cast or sizeof),
7441 compute the type and return it now. */
7443 if (decl_context == TYPENAME)
7445 /* Note that the grammar rejects storage classes in typenames
7447 gcc_assert (storage_class == csc_none && !threadp
7448 && !declspecs->inline_p && !declspecs->noreturn_p);
7449 if ((type_quals & TYPE_QUAL_ATOMIC)
7450 && TREE_CODE (type) == FUNCTION_TYPE)
7453 "%<_Atomic%>-qualified function type");
7454 type_quals &= ~TYPE_QUAL_ATOMIC;
7456 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
7458 pedwarn (loc, OPT_Wpedantic,
7459 "ISO C forbids const or volatile function types");
7461 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7462 orig_qual_indirect);
7466 if (pedantic && decl_context == FIELD
7467 && variably_modified_type_p (type, NULL_TREE))
7470 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
7471 "have a variably modified type");
7474 /* Aside from typedefs and type names (handle above),
7475 `void' at top level (not within pointer)
7476 is allowed only in public variables.
7477 We don't complain about parms either, but that is because
7478 a better error message can be made later. */
7480 if (VOID_TYPE_P (type) && decl_context != PARM
7481 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
7482 && (storage_class == csc_extern
7483 || (current_scope == file_scope
7484 && !(storage_class == csc_static
7485 || storage_class == csc_register)))))
7487 error_at (loc, "variable or field %qE declared void", name);
7488 type = integer_type_node;
7491 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
7492 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
7497 if (decl_context == PARM)
7500 bool array_parameter_p = false;
7502 /* A parameter declared as an array of T is really a pointer to T.
7503 One declared as a function is really a pointer to a function. */
7505 if (TREE_CODE (type) == ARRAY_TYPE)
7507 /* Transfer const-ness of array into that of type pointed to. */
7508 type = TREE_TYPE (type);
7509 if (orig_qual_type != NULL_TREE)
7511 if (orig_qual_indirect == 0)
7512 orig_qual_type = TREE_TYPE (orig_qual_type);
7514 orig_qual_indirect--;
7517 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7518 orig_qual_indirect);
7519 type = c_build_pointer_type (type);
7520 type_quals = array_ptr_quals;
7522 type = c_build_qualified_type (type, type_quals);
7524 /* We don't yet implement attributes in this context. */
7525 if (array_ptr_attrs != NULL_TREE)
7526 warning_at (loc, OPT_Wattributes,
7527 "attributes in parameter array declarator ignored");
7529 size_varies = false;
7530 array_parameter_p = true;
7532 else if (TREE_CODE (type) == FUNCTION_TYPE)
7534 if (type_quals & TYPE_QUAL_ATOMIC)
7537 "%<_Atomic%>-qualified function type");
7538 type_quals &= ~TYPE_QUAL_ATOMIC;
7540 else if (type_quals)
7541 pedwarn (loc, OPT_Wpedantic,
7542 "ISO C forbids qualified function types");
7544 type = c_build_qualified_type (type, type_quals);
7545 type = c_build_pointer_type (type);
7546 type_quals = TYPE_UNQUALIFIED;
7548 else if (type_quals)
7549 type = c_build_qualified_type (type, type_quals);
7551 decl = build_decl (declarator->id_loc,
7552 PARM_DECL, declarator->u.id.id, type);
7554 C_DECL_VARIABLE_SIZE (decl) = 1;
7555 C_ARRAY_PARAMETER (decl) = array_parameter_p;
7557 /* Compute the type actually passed in the parmlist,
7558 for the case where there is no prototype.
7559 (For example, shorts and chars are passed as ints.)
7560 When there is a prototype, this is overridden later. */
7562 if (type == error_mark_node)
7563 promoted_type = type;
7565 promoted_type = c_type_promotes_to (type);
7567 DECL_ARG_TYPE (decl) = promoted_type;
7568 if (declspecs->inline_p)
7569 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
7570 if (declspecs->noreturn_p)
7571 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
7573 else if (decl_context == FIELD)
7575 /* Note that the grammar rejects storage classes in typenames
7577 gcc_assert (storage_class == csc_none && !threadp
7578 && !declspecs->inline_p && !declspecs->noreturn_p);
7580 /* Structure field. It may not be a function. */
7582 if (TREE_CODE (type) == FUNCTION_TYPE)
7584 error_at (loc, "field %qE declared as a function", name);
7585 type = build_pointer_type (type);
7587 else if (TREE_CODE (type) != ERROR_MARK
7588 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
7591 error_at (loc, "field %qE has incomplete type", name);
7593 error_at (loc, "unnamed field has incomplete type");
7594 type = error_mark_node;
7596 else if (TREE_CODE (type) == ARRAY_TYPE
7597 && TYPE_DOMAIN (type) == NULL_TREE)
7599 /* We have a flexible array member through a typedef.
7600 Set suitable range. Whether this is a correct position
7601 for a flexible array member will be determined elsewhere. */
7602 if (!in_system_header_at (input_location))
7603 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
7604 "support flexible array members");
7605 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7606 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
7608 if (orig_qual_indirect == 0)
7609 orig_qual_type = NULL_TREE;
7611 if (type != error_mark_node
7612 && !verify_type_context (loc, TCTX_FIELD, type))
7613 type = error_mark_node;
7615 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7616 orig_qual_indirect);
7617 decl = build_decl (declarator->id_loc,
7618 FIELD_DECL, declarator->u.id.id, type);
7619 DECL_NONADDRESSABLE_P (decl) = bitfield;
7620 if (bitfield && !declarator->u.id.id)
7621 DECL_PADDING_P (decl) = 1;
7624 C_DECL_VARIABLE_SIZE (decl) = 1;
7626 else if (TREE_CODE (type) == FUNCTION_TYPE)
7628 if (storage_class == csc_register || threadp)
7630 error_at (loc, "invalid storage class for function %qE", name);
7632 else if (current_scope != file_scope)
7634 /* Function declaration not at file scope. Storage
7635 classes other than `extern' are not allowed, C99
7636 6.7.1p5, and `extern' makes no difference. However,
7637 GCC allows 'auto', perhaps with 'inline', to support
7638 nested functions. */
7639 if (storage_class == csc_auto)
7640 pedwarn (loc, OPT_Wpedantic,
7641 "invalid storage class for function %qE", name);
7642 else if (storage_class == csc_static)
7644 error_at (loc, "invalid storage class for function %qE", name);
7646 storage_class = declspecs->storage_class = csc_none;
7652 decl = build_decl (declarator->id_loc,
7653 FUNCTION_DECL, declarator->u.id.id, type);
7654 decl = build_decl_attribute_variant (decl, decl_attr);
7656 if (type_quals & TYPE_QUAL_ATOMIC)
7659 "%<_Atomic%>-qualified function type");
7660 type_quals &= ~TYPE_QUAL_ATOMIC;
7662 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
7663 pedwarn (loc, OPT_Wpedantic,
7664 "ISO C forbids qualified function types");
7666 /* Every function declaration is an external reference
7667 (DECL_EXTERNAL) except for those which are not at file
7668 scope and are explicitly declared "auto". This is
7669 forbidden by standard C (C99 6.7.1p5) and is interpreted by
7670 GCC to signify a forward declaration of a nested function. */
7671 if (storage_class == csc_auto && current_scope != file_scope)
7672 DECL_EXTERNAL (decl) = 0;
7673 /* In C99, a function which is declared 'inline' with 'extern'
7674 is not an external reference (which is confusing). It
7675 means that the later definition of the function must be output
7676 in this file, C99 6.7.4p6. In GNU C89, a function declared
7677 'extern inline' is an external reference. */
7678 else if (declspecs->inline_p && storage_class != csc_static)
7679 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7680 == flag_gnu89_inline);
7682 DECL_EXTERNAL (decl) = !initialized;
7684 /* Record absence of global scope for `static' or `auto'. */
7686 = !(storage_class == csc_static || storage_class == csc_auto);
7688 /* For a function definition, record the argument information
7689 block where store_parm_decls will look for it. */
7691 current_function_arg_info = arg_info;
7693 if (declspecs->default_int_p)
7694 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7696 /* Record presence of `inline' and `_Noreturn', if it is
7698 if (flag_hosted && MAIN_NAME_P (declarator->u.id.id))
7700 if (declspecs->inline_p)
7701 pedwarn (loc, 0, "cannot inline function %<main%>");
7702 if (declspecs->noreturn_p)
7703 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7707 if (declspecs->inline_p)
7708 /* Record that the function is declared `inline'. */
7709 DECL_DECLARED_INLINE_P (decl) = 1;
7710 if (declspecs->noreturn_p)
7713 pedwarn_c99 (loc, OPT_Wpedantic,
7714 "ISO C99 does not support %<_Noreturn%>");
7716 pedwarn_c99 (loc, OPT_Wpedantic,
7717 "ISO C90 does not support %<_Noreturn%>");
7718 TREE_THIS_VOLATILE (decl) = 1;
7724 /* It's a variable. */
7725 /* An uninitialized decl with `extern' is a reference. */
7726 int extern_ref = !initialized && storage_class == csc_extern;
7728 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7729 orig_qual_indirect);
7731 /* C99 6.2.2p7: It is invalid (compile-time undefined
7732 behavior) to create an 'extern' declaration for a
7733 variable if there is a global declaration that is
7734 'static' and the global declaration is not visible.
7735 (If the static declaration _is_ currently visible,
7736 the 'extern' declaration is taken to refer to that decl.) */
7737 if (extern_ref && current_scope != file_scope)
7739 tree global_decl = identifier_global_value (declarator->u.id.id);
7740 tree visible_decl = lookup_name (declarator->u.id.id);
7743 && global_decl != visible_decl
7744 && VAR_P (global_decl)
7745 && !TREE_PUBLIC (global_decl))
7746 error_at (loc, "variable previously declared %<static%> "
7747 "redeclared %<extern%>");
7750 decl = build_decl (declarator->id_loc,
7751 VAR_DECL, declarator->u.id.id, type);
7753 C_DECL_VARIABLE_SIZE (decl) = 1;
7755 if (declspecs->inline_p)
7756 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7757 if (declspecs->noreturn_p)
7758 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7760 /* At file scope, an initialized extern declaration may follow
7761 a static declaration. In that case, DECL_EXTERNAL will be
7762 reset later in start_decl. */
7763 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7765 /* At file scope, the presence of a `static' or `register' storage
7766 class specifier, or the absence of all storage class specifiers
7767 makes this declaration a definition (perhaps tentative). Also,
7768 the absence of `static' makes it public. */
7769 if (current_scope == file_scope)
7771 TREE_PUBLIC (decl) = storage_class != csc_static;
7772 TREE_STATIC (decl) = !extern_ref;
7774 /* Not at file scope, only `static' makes a static definition. */
7777 TREE_STATIC (decl) = (storage_class == csc_static);
7778 TREE_PUBLIC (decl) = extern_ref;
7782 set_decl_tls_model (decl, decl_default_tls_model (decl));
7785 if ((storage_class == csc_extern
7786 || (storage_class == csc_none
7787 && TREE_CODE (type) == FUNCTION_TYPE
7789 && variably_modified_type_p (type, NULL_TREE))
7792 if (TREE_CODE (type) == FUNCTION_TYPE)
7793 error_at (loc, "non-nested function with variably modified type");
7795 error_at (loc, "object with variably modified type must have "
7799 /* For nested functions disqualify ones taking VLAs by value
7800 from inlining since the middle-end cannot deal with this.
7801 ??? We should arrange for those to be passed by reference
7802 with emitting the copy on the caller side in the frontend. */
7803 if (storage_class == csc_none
7804 && TREE_CODE (type) == FUNCTION_TYPE)
7805 for (tree al = TYPE_ARG_TYPES (type); al; al = TREE_CHAIN (al))
7807 tree arg = TREE_VALUE (al);
7808 if (arg != error_mark_node
7809 && C_TYPE_VARIABLE_SIZE (arg))
7811 DECL_UNINLINABLE (decl) = 1;
7816 /* Record `register' declaration for warnings on &
7817 and in case doing stupid register allocation. */
7819 if (storage_class == csc_register)
7821 C_DECL_REGISTER (decl) = 1;
7822 DECL_REGISTER (decl) = 1;
7825 /* Record constancy and volatility. */
7826 c_apply_type_quals_to_decl (type_quals, decl);
7828 /* Apply _Alignas specifiers. */
7831 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7832 DECL_USER_ALIGN (decl) = 1;
7835 /* If a type has volatile components, it should be stored in memory.
7836 Otherwise, the fact that those components are volatile
7837 will be ignored, and would even crash the compiler.
7838 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7839 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7840 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7841 || TREE_CODE (decl) == RESULT_DECL))
7843 /* It is not an error for a structure with volatile fields to
7844 be declared register, but reset DECL_REGISTER since it
7845 cannot actually go in a register. */
7846 int was_reg = C_DECL_REGISTER (decl);
7847 C_DECL_REGISTER (decl) = 0;
7848 DECL_REGISTER (decl) = 0;
7849 c_mark_addressable (decl);
7850 C_DECL_REGISTER (decl) = was_reg;
7853 /* This is the earliest point at which we might know the assembler
7854 name of a variable. Thus, if it's known before this, die horribly. */
7855 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7856 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7860 && TREE_PUBLIC (decl)
7861 && TREE_STATIC (decl)
7862 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7863 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7864 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7865 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7866 ("non-local variable %qD with anonymous type is "
7867 "questionable in C++"),
7874 /* Decode the parameter-list info for a function type or function definition.
7875 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7876 if there is an identifier list instead of a parameter decl list).
7877 These two functions are separate because when a function returns
7878 or receives functions then each is called multiple times but the order
7879 of calls is different. The last call to `grokparms' is always the one
7880 that contains the formal parameter names of a function definition.
7882 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7884 FUNCDEF_FLAG is true for a function definition, false for
7885 a mere declaration. A nonempty identifier-list gets an error message
7886 when FUNCDEF_FLAG is false. */
7889 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7891 tree arg_types = arg_info->types;
7893 if (funcdef_flag && arg_info->had_vla_unspec)
7895 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7897 error ("%<[*]%> not allowed in other than function prototype scope");
7900 if (arg_types == NULL_TREE && !funcdef_flag && !flag_isoc2x
7901 && !in_system_header_at (input_location))
7902 warning (OPT_Wstrict_prototypes,
7903 "function declaration isn%'t a prototype");
7905 if (arg_types == error_mark_node)
7906 /* Don't set TYPE_ARG_TYPES in this case. */
7909 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7913 pedwarn (input_location, 0, "parameter names (without types) in "
7914 "function declaration");
7915 arg_info->parms = NULL_TREE;
7918 arg_info->parms = arg_info->types;
7920 arg_info->types = NULL_TREE;
7925 tree parm, type, typelt;
7926 unsigned int parmno;
7928 /* In C2X, convert () to (void). */
7931 && !arg_info->parms)
7932 arg_types = arg_info->types = void_list_node;
7934 /* If there is a parameter of incomplete type in a definition,
7935 this is an error. In a declaration this is valid, and a
7936 struct or union type may be completed later, before any calls
7937 or definition of the function. In the case where the tag was
7938 first declared within the parameter list, a warning has
7939 already been given. If a parameter has void type, then
7940 however the function cannot be defined or called, so
7943 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7945 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7947 type = TREE_VALUE (typelt);
7948 if (type == error_mark_node)
7951 if (!COMPLETE_TYPE_P (type))
7955 if (DECL_NAME (parm))
7956 error_at (input_location,
7957 "parameter %u (%q+D) has incomplete type",
7960 error_at (DECL_SOURCE_LOCATION (parm),
7961 "parameter %u has incomplete type",
7964 TREE_VALUE (typelt) = error_mark_node;
7965 TREE_TYPE (parm) = error_mark_node;
7966 arg_types = NULL_TREE;
7968 else if (VOID_TYPE_P (type))
7970 if (DECL_NAME (parm))
7971 warning_at (input_location, 0,
7972 "parameter %u (%q+D) has void type",
7975 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7976 "parameter %u has void type",
7981 if (DECL_NAME (parm) && TREE_USED (parm))
7982 warn_if_shadowing (parm);
7988 /* Allocate and initialize a c_arg_info structure from the parser's
7992 build_arg_info (void)
7994 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7995 ret->parms = NULL_TREE;
7997 ret->types = NULL_TREE;
7998 ret->others = NULL_TREE;
7999 ret->pending_sizes = NULL;
8000 ret->had_vla_unspec = 0;
8004 /* Take apart the current scope and return a c_arg_info structure with
8005 info on a parameter list just parsed.
8007 This structure is later fed to 'grokparms' and 'store_parm_decls'.
8009 ELLIPSIS being true means the argument list ended in '...' so don't
8010 append a sentinel (void_list_node) to the end of the type-list.
8012 EXPR is NULL or an expression that needs to be evaluated for the
8013 side effects of array size expressions in the parameters. */
8016 get_parm_info (bool ellipsis, tree expr)
8018 struct c_binding *b = current_scope->bindings;
8019 struct c_arg_info *arg_info = build_arg_info ();
8021 tree parms = NULL_TREE;
8022 vec<c_arg_tag, va_gc> *tags = NULL;
8023 tree types = NULL_TREE;
8024 tree others = NULL_TREE;
8026 bool gave_void_only_once_err = false;
8028 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
8030 /* The bindings in this scope must not get put into a block.
8031 We will take care of deleting the binding nodes. */
8032 current_scope->bindings = 0;
8034 /* This function is only called if there was *something* on the
8038 /* A parameter list consisting solely of 'void' indicates that the
8039 function takes no arguments. But if the 'void' is qualified
8040 (by 'const' or 'volatile'), or has a storage class specifier
8041 ('register'), then the behavior is undefined; issue an error.
8042 Typedefs for 'void' are OK (see DR#157). */
8043 if (b->prev == 0 /* one binding */
8044 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
8045 && !DECL_NAME (b->decl) /* anonymous */
8046 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
8048 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
8049 || C_DECL_REGISTER (b->decl))
8050 error_at (b->locus, "%<void%> as only parameter may not be qualified");
8052 /* There cannot be an ellipsis. */
8054 error_at (b->locus, "%<void%> must be the only parameter");
8056 arg_info->types = void_list_node;
8061 types = void_list_node;
8063 /* Break up the bindings list into parms, tags, types, and others;
8064 apply sanity checks; purge the name-to-decl bindings. */
8067 tree decl = b->decl;
8068 tree type = TREE_TYPE (decl);
8070 const char *keyword;
8072 switch (TREE_CODE (decl))
8077 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8078 I_SYMBOL_BINDING (b->id) = b->shadowed;
8081 /* Check for forward decls that never got their actual decl. */
8082 if (TREE_ASM_WRITTEN (decl))
8084 "parameter %q+D has just a forward declaration", decl);
8085 /* Check for (..., void, ...) and issue an error. */
8086 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
8088 if (!gave_void_only_once_err)
8090 error_at (b->locus, "%<void%> must be the only parameter");
8091 gave_void_only_once_err = true;
8096 /* Valid parameter, add it to the list. */
8097 DECL_CHAIN (decl) = parms;
8100 /* Since there is a prototype, args are passed in their
8101 declared types. The back end may override this later. */
8102 DECL_ARG_TYPE (decl) = type;
8103 types = tree_cons (0, type, types);
8107 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
8108 case UNION_TYPE: keyword = "union"; goto tag;
8109 case RECORD_TYPE: keyword = "struct"; goto tag;
8111 /* Types may not have tag-names, in which case the type
8112 appears in the bindings list with b->id NULL. */
8115 gcc_assert (I_TAG_BINDING (b->id) == b);
8116 I_TAG_BINDING (b->id) = b->shadowed;
8119 /* Warn about any struct, union or enum tags defined in a
8120 parameter list. The scope of such types is limited to
8121 the parameter list, which is rarely if ever desirable
8122 (it's impossible to call such a function with type-
8123 correct arguments). An anonymous union parm type is
8124 meaningful as a GNU extension, so don't warn for that. */
8125 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
8128 /* The %s will be one of 'struct', 'union', or 'enum'. */
8129 warning_at (b->locus, 0,
8130 "%<%s %E%> declared inside parameter list"
8131 " will not be visible outside of this definition or"
8132 " declaration", keyword, b->id);
8134 /* The %s will be one of 'struct', 'union', or 'enum'. */
8135 warning_at (b->locus, 0,
8136 "anonymous %s declared inside parameter list"
8137 " will not be visible outside of this definition or"
8138 " declaration", keyword);
8143 vec_safe_push (tags, tag);
8147 /* FUNCTION_DECLs appear when there is an implicit function
8148 declaration in the parameter list. */
8149 gcc_assert (b->nested || seen_error ());
8154 /* CONST_DECLs appear here when we have an embedded enum,
8155 and TYPE_DECLs appear here when we have an embedded struct
8156 or union. No warnings for this - we already warned about the
8159 /* When we reinsert this decl in the function body, we need
8160 to reconstruct whether it was marked as nested. */
8161 gcc_assert (!b->nested);
8162 DECL_CHAIN (decl) = others;
8168 /* error_mark_node appears here when we have an undeclared
8169 variable. Just throw it away. */
8172 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8173 I_SYMBOL_BINDING (b->id) = b->shadowed;
8177 /* Other things that might be encountered. */
8184 b = free_binding_and_advance (b);
8187 arg_info->parms = parms;
8188 arg_info->tags = tags;
8189 arg_info->types = types;
8190 arg_info->others = others;
8191 arg_info->pending_sizes = expr;
8195 /* Get the struct, enum or union (CODE says which) with tag NAME.
8196 Define the tag as a forward-reference with location LOC if it is
8197 not defined. HAVE_STD_ATTRS says whether any standard attributes
8198 were present after the struct, union or enum keyword; ATTRS are the
8199 standard attributes present there. Return a c_typespec structure
8200 for the type specifier. */
8203 parser_xref_tag (location_t loc, enum tree_code code, tree name,
8204 bool have_std_attrs, tree attrs)
8206 struct c_typespec ret;
8210 ret.expr = NULL_TREE;
8211 ret.expr_const_operands = true;
8213 /* If a cross reference is requested, look up the type
8214 already defined for this tag and return it. */
8216 ref = lookup_tag (code, name, false, &refloc);
8217 /* If this is the right type of tag, return what we found.
8218 (This reference will be shadowed by shadow_tag later if appropriate.)
8219 If this is the wrong type of tag, do not return it. If it was the
8220 wrong type in the same scope, we will have had an error
8221 message already; if in a different scope and declaring
8222 a name, pending_xref_error will give an error message; but if in a
8223 different scope and not declaring a name, this tag should
8224 shadow the previous declaration of a different type of tag, and
8225 this would not work properly if we return the reference found.
8226 (For example, with "struct foo" in an outer scope, "union foo;"
8227 must shadow that tag with a new one of union type.) */
8229 ? (have_std_attrs ? ctsk_tagref_attrs : ctsk_tagref)
8230 : (have_std_attrs ? ctsk_tagfirstref_attrs : ctsk_tagfirstref));
8231 if (ref && TREE_CODE (ref) == code)
8233 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8234 if (C_TYPE_DEFINED_IN_STRUCT (ref)
8235 && loc != UNKNOWN_LOCATION
8238 auto_diagnostic_group d;
8242 if (warning_at (loc, OPT_Wc___compat,
8243 ("enum type defined in struct or union "
8244 "is not visible in C++")))
8245 inform (refloc, "enum type defined here");
8248 if (warning_at (loc, OPT_Wc___compat,
8249 ("struct defined in struct or union "
8250 "is not visible in C++")))
8251 inform (refloc, "struct defined here");
8254 if (warning_at (loc, OPT_Wc___compat,
8255 ("union defined in struct or union "
8256 "is not visible in C++")))
8257 inform (refloc, "union defined here");
8268 /* If no such tag is yet defined, create a forward-reference node
8269 and record it as the "definition".
8270 When a real declaration of this type is found,
8271 the forward-reference will be altered into a real type. */
8273 ref = make_node (code);
8274 if (code == ENUMERAL_TYPE)
8276 /* Give the type a default layout like unsigned int
8277 to avoid crashing if it does not get defined. */
8278 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
8279 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
8280 TYPE_USER_ALIGN (ref) = 0;
8281 TYPE_UNSIGNED (ref) = 1;
8282 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
8283 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
8284 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
8287 pushtag (loc, name, ref);
8288 decl_attributes (&ref, attrs, (int) ATTR_FLAG_TYPE_IN_PLACE);
8294 /* Get the struct, enum or union (CODE says which) with tag NAME.
8295 Define the tag as a forward-reference if it is not defined.
8296 Return a tree for the type. */
8299 xref_tag (enum tree_code code, tree name)
8301 return parser_xref_tag (input_location, code, name, false, NULL_TREE).spec;
8304 /* Make sure that the tag NAME is defined *in the current scope*
8305 at least as a forward reference.
8306 LOC is the location of the struct's definition.
8307 CODE says which kind of tag NAME ought to be.
8309 This stores the current value of the file static STRUCT_PARSE_INFO
8310 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
8311 new c_struct_parse_info structure. The old value of
8312 STRUCT_PARSE_INFO is restored in finish_struct. */
8315 start_struct (location_t loc, enum tree_code code, tree name,
8316 class c_struct_parse_info **enclosing_struct_parse_info)
8318 /* If there is already a tag defined at this scope
8319 (as a forward reference), just return it. */
8321 tree ref = NULL_TREE;
8322 location_t refloc = UNKNOWN_LOCATION;
8324 if (name != NULL_TREE)
8325 ref = lookup_tag (code, name, true, &refloc);
8326 if (ref && TREE_CODE (ref) == code)
8328 if (TYPE_STUB_DECL (ref))
8329 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
8331 if (TYPE_SIZE (ref))
8333 auto_diagnostic_group d;
8334 if (code == UNION_TYPE)
8335 error_at (loc, "redefinition of %<union %E%>", name);
8337 error_at (loc, "redefinition of %<struct %E%>", name);
8338 if (refloc != UNKNOWN_LOCATION)
8339 inform (refloc, "originally defined here");
8340 /* Don't create structures using a name already in use. */
8343 else if (C_TYPE_BEING_DEFINED (ref))
8345 if (code == UNION_TYPE)
8346 error_at (loc, "nested redefinition of %<union %E%>", name);
8348 error_at (loc, "nested redefinition of %<struct %E%>", name);
8349 /* Don't bother to report "originally defined here" for a
8350 nested redefinition; the original definition should be
8352 /* Don't create structures that contain themselves. */
8357 /* Otherwise create a forward-reference just so the tag is in scope. */
8359 if (ref == NULL_TREE || TREE_CODE (ref) != code)
8361 ref = make_node (code);
8362 pushtag (loc, name, ref);
8365 C_TYPE_BEING_DEFINED (ref) = 1;
8366 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
8367 TYPE_PACKED (v) = flag_pack_struct;
8369 *enclosing_struct_parse_info = struct_parse_info;
8370 struct_parse_info = new c_struct_parse_info ();
8372 /* FIXME: This will issue a warning for a use of a type defined
8373 within a statement expr used within sizeof, et. al. This is not
8374 terribly serious as C++ doesn't permit statement exprs within
8376 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8377 warning_at (loc, OPT_Wc___compat,
8378 "defining type in %qs expression is invalid in C++",
8381 : (in_typeof ? "typeof" : "alignof")));
8386 /* Process the specs, declarator and width (NULL if omitted)
8387 of a structure component, returning a FIELD_DECL node.
8388 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
8389 DECL_ATTRS is as for grokdeclarator.
8391 LOC is the location of the structure component.
8393 This is done during the parsing of the struct declaration.
8394 The FIELD_DECL nodes are chained together and the lot of them
8395 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
8398 grokfield (location_t loc,
8399 struct c_declarator *declarator, struct c_declspecs *declspecs,
8400 tree width, tree *decl_attrs)
8404 if (declarator->kind == cdk_id && declarator->u.id.id == NULL_TREE
8405 && width == NULL_TREE)
8407 /* This is an unnamed decl.
8409 If we have something of the form "union { list } ;" then this
8410 is the anonymous union extension. Similarly for struct.
8412 If this is something of the form "struct foo;", then
8413 If MS or Plan 9 extensions are enabled, this is handled as
8414 an anonymous struct.
8415 Otherwise this is a forward declaration of a structure tag.
8417 If this is something of the form "foo;" and foo is a TYPE_DECL, then
8418 If foo names a structure or union without a tag, then this
8419 is an anonymous struct (this is permitted by C11).
8420 If MS or Plan 9 extensions are enabled and foo names a
8421 structure, then again this is an anonymous struct.
8422 Otherwise this is an error.
8424 Oh what a horrid tangled web we weave. I wonder if MS consciously
8425 took this from Plan 9 or if it was an accident of implementation
8426 that took root before someone noticed the bug... */
8428 tree type = declspecs->type;
8431 if (RECORD_OR_UNION_TYPE_P (type)
8432 && (flag_ms_extensions
8433 || flag_plan9_extensions
8434 || !declspecs->typedef_p))
8436 if (flag_ms_extensions || flag_plan9_extensions)
8438 else if (TYPE_NAME (type) == NULL)
8445 pedwarn (loc, 0, "declaration does not declare anything");
8449 pedwarn_c99 (loc, OPT_Wpedantic,
8450 "ISO C99 doesn%'t support unnamed structs/unions");
8452 pedwarn_c99 (loc, OPT_Wpedantic,
8453 "ISO C90 doesn%'t support unnamed structs/unions");
8456 value = grokdeclarator (declarator, declspecs, FIELD, false,
8457 width ? &width : NULL, decl_attrs, NULL, NULL,
8460 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
8461 DECL_INITIAL (value) = width;
8463 SET_DECL_C_BIT_FIELD (value);
8465 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
8467 /* If we currently have a binding for this field, set the
8468 in_struct field in the binding, so that we warn about lookups
8470 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
8473 /* If the in_struct field is not yet set, push it on a list
8474 to be cleared when this struct is finished. */
8477 struct_parse_info->fields.safe_push (b);
8486 /* Subroutine of detect_field_duplicates: return whether X and Y,
8487 which are both fields in the same struct, have duplicate field
8491 is_duplicate_field (tree x, tree y)
8493 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
8496 /* When using -fplan9-extensions, an anonymous field whose name is a
8497 typedef can duplicate a field name. */
8498 if (flag_plan9_extensions
8499 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
8501 tree xt, xn, yt, yn;
8504 if (DECL_NAME (x) != NULL_TREE)
8506 else if (RECORD_OR_UNION_TYPE_P (xt)
8507 && TYPE_NAME (xt) != NULL_TREE
8508 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
8509 xn = DECL_NAME (TYPE_NAME (xt));
8514 if (DECL_NAME (y) != NULL_TREE)
8516 else if (RECORD_OR_UNION_TYPE_P (yt)
8517 && TYPE_NAME (yt) != NULL_TREE
8518 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
8519 yn = DECL_NAME (TYPE_NAME (yt));
8523 if (xn != NULL_TREE && xn == yn)
8530 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
8531 to HTAB, giving errors for any duplicates. */
8534 detect_field_duplicates_hash (tree fieldlist,
8535 hash_table<nofree_ptr_hash <tree_node> > *htab)
8540 for (x = fieldlist; x ; x = DECL_CHAIN (x))
8541 if ((y = DECL_NAME (x)) != NULL_TREE)
8543 slot = htab->find_slot (y, INSERT);
8546 error ("duplicate member %q+D", x);
8547 DECL_NAME (x) = NULL_TREE;
8551 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8553 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
8555 /* When using -fplan9-extensions, an anonymous field whose
8556 name is a typedef can duplicate a field name. */
8557 if (flag_plan9_extensions
8558 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8559 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
8561 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
8562 slot = htab->find_slot (xn, INSERT);
8564 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
8570 /* Generate an error for any duplicate field names in FIELDLIST. Munge
8571 the list such that this does not present a problem later. */
8574 detect_field_duplicates (tree fieldlist)
8579 /* If the struct is the list of instance variables of an Objective-C
8580 class, then we need to check all the instance variables of
8581 superclasses when checking for duplicates (since you can't have
8582 an instance variable in a subclass with the same name as an
8583 instance variable in a superclass). We pass on this job to the
8584 Objective-C compiler. objc_detect_field_duplicates() will return
8585 false if we are not checking the list of instance variables and
8586 the C frontend should proceed with the standard field duplicate
8587 checks. If we are checking the list of instance variables, the
8588 ObjC frontend will do the check, emit the errors if needed, and
8589 then return true. */
8590 if (c_dialect_objc ())
8591 if (objc_detect_field_duplicates (false))
8594 /* First, see if there are more than "a few" fields.
8595 This is trivially true if there are zero or one fields. */
8596 if (!fieldlist || !DECL_CHAIN (fieldlist))
8601 if (DECL_NAME (x) == NULL_TREE
8602 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8605 } while (timeout > 0 && x);
8607 /* If there were "few" fields and no anonymous structures or unions,
8608 avoid the overhead of allocating a hash table. Instead just do
8609 the nested traversal thing. */
8612 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
8613 /* When using -fplan9-extensions, we can have duplicates
8614 between typedef names and fields. */
8616 || (flag_plan9_extensions
8617 && DECL_NAME (x) == NULL_TREE
8618 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
8619 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
8620 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
8622 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
8623 if (is_duplicate_field (y, x))
8625 error ("duplicate member %q+D", x);
8626 DECL_NAME (x) = NULL_TREE;
8632 hash_table<nofree_ptr_hash <tree_node> > htab (37);
8633 detect_field_duplicates_hash (fieldlist, &htab);
8637 /* Finish up struct info used by -Wc++-compat. */
8640 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
8641 location_t record_loc)
8645 struct c_binding *b;
8647 if (fieldlist == NULL_TREE)
8649 if (code == RECORD_TYPE)
8650 warning_at (record_loc, OPT_Wc___compat,
8651 "empty struct has size 0 in C, size 1 in C++");
8653 warning_at (record_loc, OPT_Wc___compat,
8654 "empty union has size 0 in C, size 1 in C++");
8657 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
8658 the current struct. We do this now at the end of the struct
8659 because the flag is used to issue visibility warnings, and we
8660 only want to issue those warnings if the type is referenced
8661 outside of the struct declaration. */
8662 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
8663 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
8665 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
8666 typedefs used when declaring fields in this struct. If the name
8667 of any of the fields is also a typedef name then the struct would
8668 not parse in C++, because the C++ lookup rules say that the
8669 typedef name would be looked up in the context of the struct, and
8670 would thus be the field rather than the typedef. */
8671 if (!struct_parse_info->typedefs_seen.is_empty ()
8672 && fieldlist != NULL_TREE)
8674 /* Use a hash_set<tree> using the name of the typedef. We can use
8675 a hash_set<tree> because identifiers are interned. */
8676 hash_set<tree> tset;
8678 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
8679 tset.add (DECL_NAME (x));
8681 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
8683 if (DECL_NAME (x) != NULL_TREE
8684 && tset.contains (DECL_NAME (x)))
8686 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
8687 ("using %qD as both field and typedef name is "
8690 /* FIXME: It would be nice to report the location where
8691 the typedef name is used. */
8696 /* For each field which has a binding and which was not defined in
8697 an enclosing struct, clear the in_struct field. */
8698 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
8702 /* Function to help qsort sort FIELD_DECLs by name order. */
8705 field_decl_cmp (const void *x_p, const void *y_p)
8707 const tree *const x = (const tree *) x_p;
8708 const tree *const y = (const tree *) y_p;
8710 if (DECL_NAME (*x) == DECL_NAME (*y))
8711 /* A nontype is "greater" than a type. */
8712 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8713 if (DECL_NAME (*x) == NULL_TREE)
8715 if (DECL_NAME (*y) == NULL_TREE)
8717 if (DECL_NAME (*x) < DECL_NAME (*y))
8722 /* If this structure or union completes the type of any previous
8723 variable declaration, lay it out and output its rtl. */
8725 finish_incomplete_vars (tree incomplete_vars, bool toplevel)
8727 for (tree x = incomplete_vars; x; x = TREE_CHAIN (x))
8729 tree decl = TREE_VALUE (x);
8730 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8731 layout_array_type (TREE_TYPE (decl));
8732 if (TREE_CODE (decl) != TYPE_DECL)
8734 relayout_decl (decl);
8735 if (c_dialect_objc ())
8736 objc_check_decl (decl);
8737 rest_of_decl_compilation (decl, toplevel, 0);
8742 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8743 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8744 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8745 ATTRIBUTES are attributes to be applied to the structure.
8747 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8748 the struct was started. */
8751 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8752 class c_struct_parse_info *enclosing_struct_parse_info)
8755 bool toplevel = file_scope == current_scope;
8757 /* If this type was previously laid out as a forward reference,
8758 make sure we lay it out again. */
8760 TYPE_SIZE (t) = NULL_TREE;
8762 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8766 for (x = fieldlist; x; x = DECL_CHAIN (x))
8768 if (DECL_NAME (x) != NULL_TREE)
8770 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8776 if (TREE_CODE (t) == UNION_TYPE)
8779 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8781 pedwarn (loc, OPT_Wpedantic, "union has no members");
8786 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8788 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8793 /* Install struct as DECL_CONTEXT of each field decl.
8794 Also process specified field sizes, found in the DECL_INITIAL,
8795 storing 0 there after the type has been changed to precision equal
8796 to its width, rather than the precision of the specified standard
8797 type. (Correct layout requires the original type to have been preserved
8800 bool saw_named_field = false;
8801 for (x = fieldlist; x; x = DECL_CHAIN (x))
8803 if (TREE_TYPE (x) == error_mark_node)
8806 DECL_CONTEXT (x) = t;
8808 /* If any field is const, the structure type is pseudo-const. */
8809 if (TREE_READONLY (x))
8810 C_TYPE_FIELDS_READONLY (t) = 1;
8813 /* A field that is pseudo-const makes the structure likewise. */
8814 tree t1 = strip_array_types (TREE_TYPE (x));
8815 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8816 C_TYPE_FIELDS_READONLY (t) = 1;
8819 /* Any field that is volatile means variables of this type must be
8820 treated in some ways as volatile. */
8821 if (TREE_THIS_VOLATILE (x))
8822 C_TYPE_FIELDS_VOLATILE (t) = 1;
8824 /* Any field of nominal variable size implies structure is too. */
8825 if (C_DECL_VARIABLE_SIZE (x))
8826 C_TYPE_VARIABLE_SIZE (t) = 1;
8828 if (DECL_C_BIT_FIELD (x))
8830 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8831 DECL_SIZE (x) = bitsize_int (width);
8832 DECL_BIT_FIELD (x) = 1;
8836 && (DECL_BIT_FIELD (x)
8837 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8838 DECL_PACKED (x) = 1;
8840 /* Detect flexible array member in an invalid context. */
8841 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8842 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8843 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8844 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8846 if (TREE_CODE (t) == UNION_TYPE)
8848 error_at (DECL_SOURCE_LOCATION (x),
8849 "flexible array member in union");
8850 TREE_TYPE (x) = error_mark_node;
8852 else if (DECL_CHAIN (x) != NULL_TREE)
8854 error_at (DECL_SOURCE_LOCATION (x),
8855 "flexible array member not at end of struct");
8856 TREE_TYPE (x) = error_mark_node;
8858 else if (!saw_named_field)
8860 error_at (DECL_SOURCE_LOCATION (x),
8861 "flexible array member in a struct with no named "
8863 TREE_TYPE (x) = error_mark_node;
8867 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8868 && flexible_array_type_p (TREE_TYPE (x)))
8869 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8870 "invalid use of structure with flexible array member");
8873 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8874 saw_named_field = true;
8877 detect_field_duplicates (fieldlist);
8879 /* Now we have the nearly final fieldlist. Record it,
8880 then lay out the structure or union (including the fields). */
8882 TYPE_FIELDS (t) = fieldlist;
8884 maybe_apply_pragma_scalar_storage_order (t);
8888 if (TYPE_SIZE_UNIT (t)
8889 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8890 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8891 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8892 error ("type %qT is too large", t);
8894 /* Give bit-fields their proper types and rewrite the type of array fields
8895 with scalar component if the enclosing type has reverse storage order. */
8896 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8898 if (TREE_CODE (field) == FIELD_DECL
8899 && DECL_INITIAL (field)
8900 && TREE_TYPE (field) != error_mark_node)
8902 unsigned HOST_WIDE_INT width
8903 = tree_to_uhwi (DECL_INITIAL (field));
8904 tree type = TREE_TYPE (field);
8905 if (width != TYPE_PRECISION (type))
8908 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8909 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8911 DECL_INITIAL (field) = NULL_TREE;
8913 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8914 && TREE_CODE (field) == FIELD_DECL
8915 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8917 tree ftype = TREE_TYPE (field);
8918 tree ctype = strip_array_types (ftype);
8919 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8921 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8922 tree *typep = &fmain_type;
8924 *typep = build_distinct_type_copy (*typep);
8925 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8926 typep = &TREE_TYPE (*typep);
8927 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8929 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8933 /* Warn on problematic type punning for storage order purposes. */
8934 if (TREE_CODE (t) == UNION_TYPE
8935 && TREE_CODE (field) == FIELD_DECL
8936 && AGGREGATE_TYPE_P (TREE_TYPE (field)))
8938 tree ftype = TREE_TYPE (field);
8939 if (TREE_CODE (ftype) == ARRAY_TYPE)
8940 ftype = strip_array_types (ftype);
8941 if (RECORD_OR_UNION_TYPE_P (ftype)
8942 && TYPE_REVERSE_STORAGE_ORDER (ftype)
8943 != TYPE_REVERSE_STORAGE_ORDER (t))
8944 warning_at (DECL_SOURCE_LOCATION (field),
8945 OPT_Wscalar_storage_order,
8946 "type punning toggles scalar storage order");
8950 /* Now we have the truly final field list.
8951 Store it in this type and in the variants. */
8953 TYPE_FIELDS (t) = fieldlist;
8955 /* If there are lots of fields, sort so we can look through them fast.
8956 We arbitrarily consider 16 or more elts to be "a lot". */
8961 for (x = fieldlist; x; x = DECL_CHAIN (x))
8963 if (len > 15 || DECL_NAME (x) == NULL)
8971 struct lang_type *space;
8972 struct sorted_fields_type *space2;
8974 len += list_length (x);
8976 /* Use the same allocation policy here that make_node uses, to
8977 ensure that this lives as long as the rest of the struct decl.
8978 All decls in an inline function need to be saved. */
8980 space = ggc_cleared_alloc<struct lang_type> ();
8981 space2 = (sorted_fields_type *) ggc_internal_alloc
8982 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8986 field_array = &space2->elts[0];
8987 for (x = fieldlist; x; x = DECL_CHAIN (x))
8989 field_array[len++] = x;
8991 /* If there is anonymous struct or union, break out of the loop. */
8992 if (DECL_NAME (x) == NULL)
8995 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8998 TYPE_LANG_SPECIFIC (t) = space;
8999 TYPE_LANG_SPECIFIC (t)->s->len = len;
9000 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
9001 qsort (field_array, len, sizeof (tree), field_decl_cmp);
9006 /* If this was supposed to be a transparent union, but we can't
9007 make it one, warn and turn off the flag. */
9008 if (TREE_CODE (t) == UNION_TYPE
9009 && TYPE_TRANSPARENT_AGGR (t)
9010 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
9012 TYPE_TRANSPARENT_AGGR (t) = 0;
9013 warning_at (loc, 0, "union cannot be made transparent");
9016 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
9017 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
9019 TYPE_FIELDS (x) = TYPE_FIELDS (t);
9020 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
9021 TYPE_TRANSPARENT_AGGR (x) = TYPE_TRANSPARENT_AGGR (t);
9022 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
9023 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
9024 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
9025 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
9028 /* Update type location to the one of the definition, instead of e.g.
9029 a forward declaration. */
9030 if (TYPE_STUB_DECL (t))
9031 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
9033 /* Finish debugging output for this type. */
9034 rest_of_type_compilation (t, toplevel);
9036 finish_incomplete_vars (incomplete_vars, toplevel);
9038 /* If we're inside a function proper, i.e. not file-scope and not still
9039 parsing parameters, then arrange for the size of a variable sized type
9041 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
9042 add_stmt (build_stmt (loc,
9043 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
9045 if (warn_cxx_compat)
9046 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
9048 delete struct_parse_info;
9050 struct_parse_info = enclosing_struct_parse_info;
9052 /* If this struct is defined inside a struct, add it to
9055 && struct_parse_info != NULL
9056 && !in_sizeof && !in_typeof && !in_alignof)
9057 struct_parse_info->struct_types.safe_push (t);
9063 gt_pointer_operator new_value;
9067 /* This routine compares two fields like field_decl_cmp but using the
9068 pointer operator in resort_data. */
9071 resort_field_decl_cmp (const void *x_p, const void *y_p)
9073 const tree *const x = (const tree *) x_p;
9074 const tree *const y = (const tree *) y_p;
9076 if (DECL_NAME (*x) == DECL_NAME (*y))
9077 /* A nontype is "greater" than a type. */
9078 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9079 if (DECL_NAME (*x) == NULL_TREE)
9081 if (DECL_NAME (*y) == NULL_TREE)
9084 tree d1 = DECL_NAME (*x);
9085 tree d2 = DECL_NAME (*y);
9086 resort_data.new_value (&d1, &d1, resort_data.cookie);
9087 resort_data.new_value (&d2, &d2, resort_data.cookie);
9094 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
9097 resort_sorted_fields (void *obj,
9098 void * ARG_UNUSED (orig_obj),
9099 gt_pointer_operator new_value,
9102 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
9103 resort_data.new_value = new_value;
9104 resort_data.cookie = cookie;
9105 qsort (&sf->elts[0], sf->len, sizeof (tree),
9106 resort_field_decl_cmp);
9109 /* Lay out the type T, and its element type, and so on. */
9112 layout_array_type (tree t)
9114 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
9115 layout_array_type (TREE_TYPE (t));
9119 /* Begin compiling the definition of an enumeration type.
9120 NAME is its name (or null if anonymous).
9121 LOC is the enum's location.
9122 Returns the type object, as yet incomplete.
9123 Also records info about it so that build_enumerator
9124 may be used to declare the individual values as they are read. */
9127 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
9129 tree enumtype = NULL_TREE;
9130 location_t enumloc = UNKNOWN_LOCATION;
9132 /* If this is the real definition for a previous forward reference,
9133 fill in the contents in the same object that used to be the
9134 forward reference. */
9136 if (name != NULL_TREE)
9137 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
9139 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
9141 enumtype = make_node (ENUMERAL_TYPE);
9142 pushtag (loc, name, enumtype);
9144 /* Update type location to the one of the definition, instead of e.g.
9145 a forward declaration. */
9146 else if (TYPE_STUB_DECL (enumtype))
9148 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
9149 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
9152 if (C_TYPE_BEING_DEFINED (enumtype))
9153 error_at (loc, "nested redefinition of %<enum %E%>", name);
9155 C_TYPE_BEING_DEFINED (enumtype) = 1;
9157 if (TYPE_VALUES (enumtype) != NULL_TREE)
9159 /* This enum is a named one that has been declared already. */
9160 auto_diagnostic_group d;
9161 error_at (loc, "redeclaration of %<enum %E%>", name);
9162 if (enumloc != UNKNOWN_LOCATION)
9163 inform (enumloc, "originally defined here");
9165 /* Completely replace its old definition.
9166 The old enumerators remain defined, however. */
9167 TYPE_VALUES (enumtype) = NULL_TREE;
9170 the_enum->enum_next_value = integer_zero_node;
9171 the_enum->enum_overflow = 0;
9173 if (flag_short_enums)
9174 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
9175 TYPE_PACKED (v) = 1;
9177 /* FIXME: This will issue a warning for a use of a type defined
9178 within sizeof in a statement expr. This is not terribly serious
9179 as C++ doesn't permit statement exprs within sizeof anyhow. */
9180 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
9181 warning_at (loc, OPT_Wc___compat,
9182 "defining type in %qs expression is invalid in C++",
9185 : (in_typeof ? "typeof" : "alignof")));
9190 /* After processing and defining all the values of an enumeration type,
9191 install their decls in the enumeration type and finish it off.
9192 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
9193 and ATTRIBUTES are the specified attributes.
9194 Returns ENUMTYPE. */
9197 finish_enum (tree enumtype, tree values, tree attributes)
9200 tree minnode = NULL_TREE, maxnode = NULL_TREE;
9203 bool toplevel = (file_scope == current_scope);
9204 struct lang_type *lt;
9206 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
9208 /* Calculate the maximum value of any enumerator in this type. */
9210 if (values == error_mark_node)
9211 minnode = maxnode = integer_zero_node;
9214 minnode = maxnode = TREE_VALUE (values);
9215 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
9217 tree value = TREE_VALUE (pair);
9218 if (tree_int_cst_lt (maxnode, value))
9220 if (tree_int_cst_lt (value, minnode))
9225 /* Construct the final type of this enumeration. It is the same
9226 as one of the integral types - the narrowest one that fits, except
9227 that normally we only go as narrow as int - and signed iff any of
9228 the values are negative. */
9229 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
9230 precision = MAX (tree_int_cst_min_precision (minnode, sign),
9231 tree_int_cst_min_precision (maxnode, sign));
9233 /* If the precision of the type was specified with an attribute and it
9234 was too small, give an error. Otherwise, use it. */
9235 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
9237 if (precision > TYPE_PRECISION (enumtype))
9239 TYPE_PRECISION (enumtype) = 0;
9240 error ("specified mode too small for enumerated values");
9243 precision = TYPE_PRECISION (enumtype);
9246 TYPE_PRECISION (enumtype) = 0;
9248 if (TYPE_PACKED (enumtype)
9249 || precision > TYPE_PRECISION (integer_type_node)
9250 || TYPE_PRECISION (enumtype))
9252 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
9255 warning (0, "enumeration values exceed range of largest integer");
9256 tem = long_long_integer_type_node;
9260 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
9262 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
9263 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
9264 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
9265 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
9266 TYPE_SIZE (enumtype) = NULL_TREE;
9267 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
9269 layout_type (enumtype);
9271 if (values != error_mark_node)
9273 /* Change the type of the enumerators to be the enum type. We
9274 need to do this irrespective of the size of the enum, for
9275 proper type checking. Replace the DECL_INITIALs of the
9276 enumerators, and the value slots of the list, with copies
9277 that have the enum type; they cannot be modified in place
9278 because they may be shared (e.g. integer_zero_node) Finally,
9279 change the purpose slots to point to the names of the decls. */
9280 for (pair = values; pair; pair = TREE_CHAIN (pair))
9282 tree enu = TREE_PURPOSE (pair);
9283 tree ini = DECL_INITIAL (enu);
9285 TREE_TYPE (enu) = enumtype;
9287 /* The ISO C Standard mandates enumerators to have type int,
9288 even though the underlying type of an enum type is
9289 unspecified. However, GCC allows enumerators of any
9290 integer type as an extensions. build_enumerator()
9291 converts any enumerators that fit in an int to type int,
9292 to avoid promotions to unsigned types when comparing
9293 integers with enumerators that fit in the int range.
9294 When -pedantic is given, build_enumerator() would have
9295 already warned about those that don't fit. Here we
9296 convert the rest to the enumerator type. */
9297 if (TREE_TYPE (ini) != integer_type_node)
9298 ini = convert (enumtype, ini);
9300 DECL_INITIAL (enu) = ini;
9301 TREE_PURPOSE (pair) = DECL_NAME (enu);
9302 /* To match the C++ FE, store the CONST_DECL rather than just its
9304 TREE_VALUE (pair) = enu;
9307 TYPE_VALUES (enumtype) = values;
9310 /* Record the min/max values so that we can warn about bit-field
9311 enumerations that are too small for the values. */
9312 lt = ggc_cleared_alloc<struct lang_type> ();
9313 lt->enum_min = minnode;
9314 lt->enum_max = maxnode;
9315 TYPE_LANG_SPECIFIC (enumtype) = lt;
9317 /* Fix up all variant types of this enum type. */
9318 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (enumtype));
9319 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
9321 C_TYPE_INCOMPLETE_VARS (tem) = NULL_TREE;
9322 if (tem == enumtype)
9324 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
9325 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
9326 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
9327 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
9328 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
9329 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
9330 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
9331 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
9332 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
9333 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
9334 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
9337 /* Finish debugging output for this type. */
9338 rest_of_type_compilation (enumtype, toplevel);
9340 finish_incomplete_vars (incomplete_vars, toplevel);
9342 /* If this enum is defined inside a struct, add it to
9345 && struct_parse_info != NULL
9346 && !in_sizeof && !in_typeof && !in_alignof)
9347 struct_parse_info->struct_types.safe_push (enumtype);
9349 C_TYPE_BEING_DEFINED (enumtype) = 0;
9354 /* Build and install a CONST_DECL for one value of the
9355 current enumeration type (one that was begun with start_enum).
9356 DECL_LOC is the location of the enumerator.
9357 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
9358 Return a tree-list containing the CONST_DECL and its value.
9359 Assignment of sequential values by default is handled here. */
9362 build_enumerator (location_t decl_loc, location_t loc,
9363 struct c_enum_contents *the_enum, tree name, tree value)
9367 /* Validate and default VALUE. */
9369 if (value != NULL_TREE)
9371 /* Don't issue more errors for error_mark_node (i.e. an
9372 undeclared identifier) - just ignore the value expression. */
9373 if (value == error_mark_node)
9375 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
9377 error_at (loc, "enumerator value for %qE is not an integer constant",
9383 if (TREE_CODE (value) != INTEGER_CST)
9385 value = c_fully_fold (value, false, NULL);
9386 if (TREE_CODE (value) == INTEGER_CST)
9387 pedwarn (loc, OPT_Wpedantic,
9388 "enumerator value for %qE is not an integer "
9389 "constant expression", name);
9391 if (TREE_CODE (value) != INTEGER_CST)
9393 error ("enumerator value for %qE is not an integer constant",
9399 value = default_conversion (value);
9400 constant_expression_warning (value);
9405 /* Default based on previous value. */
9406 /* It should no longer be possible to have NON_LVALUE_EXPR
9408 if (value == NULL_TREE)
9410 value = the_enum->enum_next_value;
9411 if (the_enum->enum_overflow)
9412 error_at (loc, "overflow in enumeration values");
9414 /* Even though the underlying type of an enum is unspecified, the
9415 type of enumeration constants is explicitly defined as int
9416 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
9418 else if (!int_fits_type_p (value, integer_type_node))
9419 pedwarn (loc, OPT_Wpedantic,
9420 "ISO C restricts enumerator values to range of %<int%>");
9422 /* The ISO C Standard mandates enumerators to have type int, even
9423 though the underlying type of an enum type is unspecified.
9424 However, GCC allows enumerators of any integer type as an
9425 extensions. Here we convert any enumerators that fit in an int
9426 to type int, to avoid promotions to unsigned types when comparing
9427 integers with enumerators that fit in the int range. When
9428 -pedantic is given, we would have already warned about those that
9429 don't fit. We have to do this here rather than in finish_enum
9430 because this value may be used to define more enumerators. */
9431 if (int_fits_type_p (value, integer_type_node))
9432 value = convert (integer_type_node, value);
9434 /* Set basis for default for next value. */
9435 the_enum->enum_next_value
9436 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
9437 PLUS_EXPR, value, integer_one_node, false);
9438 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
9440 /* Now create a declaration for the enum value name. */
9442 type = TREE_TYPE (value);
9443 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
9444 TYPE_PRECISION (integer_type_node)),
9445 (TYPE_PRECISION (type)
9446 >= TYPE_PRECISION (integer_type_node)
9447 && TYPE_UNSIGNED (type)));
9449 decl = build_decl (decl_loc, CONST_DECL, name, type);
9450 DECL_INITIAL (decl) = convert (type, value);
9453 return tree_cons (decl, value, NULL_TREE);
9456 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */
9459 c_simulate_enum_decl (location_t loc, const char *name,
9460 vec<string_int_pair> *values_ptr)
9462 location_t saved_loc = input_location;
9463 input_location = loc;
9465 struct c_enum_contents the_enum;
9466 tree enumtype = start_enum (loc, &the_enum, get_identifier (name));
9468 tree value_chain = NULL_TREE;
9469 string_int_pair *value;
9470 vec<string_int_pair> values = *values_ptr;
9472 FOR_EACH_VEC_ELT (values, i, value)
9474 tree decl = build_enumerator (loc, loc, &the_enum,
9475 get_identifier (value->first),
9476 build_int_cst (integer_type_node,
9478 TREE_CHAIN (decl) = value_chain;
9482 finish_enum (enumtype, nreverse (value_chain), NULL_TREE);
9484 input_location = saved_loc;
9488 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */
9491 c_simulate_record_decl (location_t loc, const char *name,
9492 array_slice<const tree> fields)
9494 location_t saved_loc = input_location;
9495 input_location = loc;
9497 class c_struct_parse_info *struct_info;
9498 tree ident = get_identifier (name);
9499 tree type = start_struct (loc, RECORD_TYPE, ident, &struct_info);
9501 for (unsigned int i = 0; i < fields.size (); ++i)
9503 DECL_FIELD_CONTEXT (fields[i]) = type;
9505 DECL_CHAIN (fields[i - 1]) = fields[i];
9508 finish_struct (loc, type, fields[0], NULL_TREE, struct_info);
9510 tree decl = build_decl (loc, TYPE_DECL, ident, type);
9511 set_underlying_type (decl);
9512 lang_hooks.decls.pushdecl (decl);
9514 input_location = saved_loc;
9518 /* Create the FUNCTION_DECL for a function definition.
9519 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
9520 the declaration; they describe the function's name and the type it returns,
9521 but twisted together in a fashion that parallels the syntax of C.
9523 This function creates a binding context for the function body
9524 as well as setting up the FUNCTION_DECL in current_function_decl.
9526 Returns true on success. If the DECLARATOR is not suitable for a function
9527 (it defines a datum instead), we return false to report a parse error. */
9530 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
9533 tree decl1, old_decl;
9534 tree restype, resdecl;
9537 current_function_returns_value = 0; /* Assume, until we see it does. */
9538 current_function_returns_null = 0;
9539 current_function_returns_abnormally = 0;
9540 warn_about_return_type = 0;
9541 c_switch_stack = NULL;
9543 /* Indicate no valid break/continue context. */
9546 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
9547 &attributes, NULL, NULL, DEPRECATED_NORMAL);
9548 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
9550 /* If the declarator is not suitable for a function definition,
9551 cause a syntax error. */
9552 if (decl1 == NULL_TREE
9553 || TREE_CODE (decl1) != FUNCTION_DECL)
9556 loc = DECL_SOURCE_LOCATION (decl1);
9558 /* A nested function is not global. */
9559 if (current_function_decl != NULL_TREE)
9560 TREE_PUBLIC (decl1) = 0;
9562 c_decl_attributes (&decl1, attributes, 0);
9564 if (DECL_DECLARED_INLINE_P (decl1)
9565 && DECL_UNINLINABLE (decl1)
9566 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
9567 warning_at (loc, OPT_Wattributes,
9568 "inline function %qD given attribute %qs",
9571 /* Handle gnu_inline attribute. */
9572 if (declspecs->inline_p
9573 && !flag_gnu89_inline
9574 && TREE_CODE (decl1) == FUNCTION_DECL
9575 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
9576 || current_function_decl))
9578 if (declspecs->storage_class != csc_static)
9579 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
9582 announce_function (decl1);
9584 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
9586 error_at (loc, "return type is an incomplete type");
9587 /* Make it return void instead. */
9589 = build_function_type (void_type_node,
9590 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
9593 if (warn_about_return_type)
9594 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
9595 : (warn_return_type > 0 ? OPT_Wreturn_type
9596 : OPT_Wimplicit_int),
9597 "return type defaults to %<int%>");
9599 /* Make the init_value nonzero so pushdecl knows this is not tentative.
9600 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
9601 DECL_INITIAL (decl1) = error_mark_node;
9603 /* If this definition isn't a prototype and we had a prototype declaration
9604 before, copy the arg type info from that prototype. */
9605 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
9606 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
9607 old_decl = NULL_TREE;
9609 current_function_prototype_locus = UNKNOWN_LOCATION;
9610 current_function_prototype_built_in = false;
9611 current_function_prototype_arg_types = NULL_TREE;
9612 tree newtype = TREE_TYPE (decl1);
9613 tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype;
9614 if (!prototype_p (newtype))
9616 tree oldrt = TREE_TYPE (oldtype);
9617 tree newrt = TREE_TYPE (newtype);
9618 if (old_decl != NULL_TREE
9619 && TREE_CODE (oldtype) == FUNCTION_TYPE
9620 && comptypes (oldrt, newrt))
9622 if (stdarg_p (oldtype))
9624 auto_diagnostic_group d;
9625 warning_at (loc, 0, "%q+D defined as variadic function "
9626 "without prototype", decl1);
9627 locate_old_decl (old_decl);
9629 TREE_TYPE (decl1) = composite_type (oldtype, newtype);
9630 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
9631 current_function_prototype_built_in
9632 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
9633 current_function_prototype_arg_types
9634 = TYPE_ARG_TYPES (newtype);
9636 if (TREE_PUBLIC (decl1))
9638 /* If there is an external prototype declaration of this
9639 function, record its location but do not copy information
9640 to this decl. This may be an invisible declaration
9641 (built-in or in a scope which has finished) or simply
9642 have more refined argument types than any declaration
9644 struct c_binding *b;
9645 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
9646 if (B_IN_SCOPE (b, external_scope))
9650 tree ext_decl, ext_type;
9652 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
9653 if (TREE_CODE (ext_type) == FUNCTION_TYPE
9654 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
9655 TREE_TYPE (ext_type)))
9657 current_function_prototype_locus
9658 = DECL_SOURCE_LOCATION (ext_decl);
9659 current_function_prototype_built_in
9660 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
9661 current_function_prototype_arg_types
9662 = TYPE_ARG_TYPES (ext_type);
9668 /* Optionally warn of old-fashioned def with no previous prototype. */
9669 if (warn_strict_prototypes
9670 && old_decl != error_mark_node
9671 && !prototype_p (TREE_TYPE (decl1))
9672 && C_DECL_ISNT_PROTOTYPE (old_decl))
9673 warning_at (loc, OPT_Wstrict_prototypes,
9674 "function declaration isn%'t a prototype");
9675 /* Optionally warn of any global def with no previous prototype. */
9676 else if (warn_missing_prototypes
9677 && old_decl != error_mark_node
9678 && TREE_PUBLIC (decl1)
9679 && !MAIN_NAME_P (DECL_NAME (decl1))
9680 && C_DECL_ISNT_PROTOTYPE (old_decl)
9681 && !DECL_DECLARED_INLINE_P (decl1))
9682 warning_at (loc, OPT_Wmissing_prototypes,
9683 "no previous prototype for %qD", decl1);
9684 /* Optionally warn of any def with no previous prototype
9685 if the function has already been used. */
9686 else if (warn_missing_prototypes
9687 && old_decl != NULL_TREE
9688 && old_decl != error_mark_node
9689 && TREE_USED (old_decl)
9690 && !prototype_p (TREE_TYPE (old_decl)))
9691 warning_at (loc, OPT_Wmissing_prototypes,
9692 "%qD was used with no prototype before its definition", decl1);
9693 /* Optionally warn of any global def with no previous declaration. */
9694 else if (warn_missing_declarations
9695 && TREE_PUBLIC (decl1)
9696 && old_decl == NULL_TREE
9697 && !MAIN_NAME_P (DECL_NAME (decl1))
9698 && !DECL_DECLARED_INLINE_P (decl1))
9699 warning_at (loc, OPT_Wmissing_declarations,
9700 "no previous declaration for %qD",
9702 /* Optionally warn of any def with no previous declaration
9703 if the function has already been used. */
9704 else if (warn_missing_declarations
9705 && old_decl != NULL_TREE
9706 && old_decl != error_mark_node
9707 && TREE_USED (old_decl)
9708 && C_DECL_IMPLICIT (old_decl))
9709 warning_at (loc, OPT_Wmissing_declarations,
9710 "%qD was used with no declaration before its definition", decl1);
9712 /* This function exists in static storage.
9713 (This does not mean `static' in the C sense!) */
9714 TREE_STATIC (decl1) = 1;
9716 /* This is the earliest point at which we might know the assembler
9717 name of the function. Thus, if it's set before this, die horribly. */
9718 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
9720 /* If #pragma weak was used, mark the decl weak now. */
9721 if (current_scope == file_scope)
9722 maybe_apply_pragma_weak (decl1);
9724 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
9725 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
9727 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
9728 != integer_type_node)
9729 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
9730 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
9731 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
9734 check_main_parameter_types (decl1);
9736 if (!TREE_PUBLIC (decl1))
9737 pedwarn (loc, OPT_Wmain,
9738 "%qD is normally a non-static function", decl1);
9741 tree parms = current_function_arg_info->parms;
9744 location_t origloc = DECL_SOURCE_LOCATION (old_decl);
9745 warn_parm_array_mismatch (origloc, old_decl, parms);
9748 /* Record the decl so that the function name is defined.
9749 If we already have a decl for this name, and it is a FUNCTION_DECL,
9750 use the old decl. */
9752 current_function_decl = pushdecl (decl1);
9754 if (tree access = build_attr_access_from_parms (parms, false))
9755 decl_attributes (¤t_function_decl, access, ATTR_FLAG_INTERNAL,
9759 declare_parm_level ();
9761 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
9762 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
9763 DECL_ARTIFICIAL (resdecl) = 1;
9764 DECL_IGNORED_P (resdecl) = 1;
9765 DECL_RESULT (current_function_decl) = resdecl;
9767 start_fname_decls ();
9772 /* Subroutine of store_parm_decls which handles new-style function
9773 definitions (prototype format). The parms already have decls, so we
9774 need only record them as in effect and complain if any redundant
9775 old-style parm decls were written. */
9777 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
9783 if (current_scope->bindings)
9785 error_at (DECL_SOURCE_LOCATION (fndecl),
9786 "old-style parameter declarations in prototyped "
9787 "function definition");
9789 /* Get rid of the old-style declarations. */
9793 /* Don't issue this warning for nested functions, and don't issue this
9794 warning if we got here because ARG_INFO_TYPES was error_mark_node
9795 (this happens when a function definition has just an ellipsis in
9796 its parameter list). */
9797 else if (!in_system_header_at (input_location)
9798 && !current_function_scope
9799 && arg_info->types != error_mark_node)
9800 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
9801 "traditional C rejects ISO C style function definitions");
9803 /* Now make all the parameter declarations visible in the function body.
9804 We can bypass most of the grunt work of pushdecl. */
9805 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9807 DECL_CONTEXT (decl) = current_function_decl;
9808 if (DECL_NAME (decl))
9810 bind (DECL_NAME (decl), decl, current_scope,
9811 /*invisible=*/false, /*nested=*/false,
9813 if (!TREE_USED (decl))
9814 warn_if_shadowing (decl);
9817 pedwarn_c11 (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
9818 "ISO C does not support omitting parameter names in "
9819 "function definitions before C2X");
9822 /* Record the parameter list in the function declaration. */
9823 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9825 /* Now make all the ancillary declarations visible, likewise. */
9826 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9828 DECL_CONTEXT (decl) = current_function_decl;
9829 if (DECL_NAME (decl))
9830 bind (DECL_NAME (decl), decl, current_scope,
9831 /*invisible=*/false,
9832 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9836 /* And all the tag declarations. */
9837 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9839 bind (tag->id, tag->type, current_scope,
9840 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9843 /* Subroutine of store_parm_decls which handles old-style function
9844 definitions (separate parameter list and declarations). */
9847 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9849 struct c_binding *b;
9850 tree parm, decl, last;
9851 tree parmids = arg_info->parms;
9852 hash_set<tree> seen_args;
9854 if (!in_system_header_at (input_location))
9857 pedwarn (DECL_SOURCE_LOCATION (fndecl),
9858 OPT_Wold_style_definition, "old-style function definition");
9860 warning_at (DECL_SOURCE_LOCATION (fndecl),
9861 OPT_Wold_style_definition,
9862 "old-style function definition");
9865 if (current_scope->had_vla_unspec)
9866 error ("%<[*]%> not allowed in other than function prototype scope");
9868 /* Match each formal parameter name with its declaration. Save each
9869 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9870 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9872 if (TREE_VALUE (parm) == NULL_TREE)
9874 error_at (DECL_SOURCE_LOCATION (fndecl),
9875 "parameter name missing from parameter list");
9876 TREE_PURPOSE (parm) = NULL_TREE;
9880 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9881 if (b && B_IN_CURRENT_SCOPE (b))
9884 /* Skip erroneous parameters. */
9885 if (decl == error_mark_node)
9887 /* If we got something other than a PARM_DECL it is an error. */
9888 if (TREE_CODE (decl) != PARM_DECL)
9890 error_at (DECL_SOURCE_LOCATION (decl),
9891 "%qD declared as a non-parameter", decl);
9894 /* If the declaration is already marked, we have a duplicate
9895 name. Complain and ignore the duplicate. */
9896 else if (seen_args.contains (decl))
9898 error_at (DECL_SOURCE_LOCATION (decl),
9899 "multiple parameters named %qD", decl);
9900 TREE_PURPOSE (parm) = NULL_TREE;
9903 /* If the declaration says "void", complain and turn it into
9905 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9907 error_at (DECL_SOURCE_LOCATION (decl),
9908 "parameter %qD declared with void type", decl);
9909 TREE_TYPE (decl) = integer_type_node;
9910 DECL_ARG_TYPE (decl) = integer_type_node;
9911 layout_decl (decl, 0);
9913 warn_if_shadowing (decl);
9915 /* If no declaration found, default to int. */
9918 /* FIXME diagnostics: This should be the location of the argument,
9919 not the FNDECL. E.g., for an old-style declaration
9921 int f10(v) { blah; }
9923 We should use the location of the V, not the F10.
9924 Unfortunately, the V is an IDENTIFIER_NODE which has no
9925 location. In the future we need locations for c_arg_info
9928 See gcc.dg/Wshadow-3.c for an example of this problem. */
9929 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9930 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9931 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9933 warn_if_shadowing (decl);
9936 pedwarn (DECL_SOURCE_LOCATION (decl),
9937 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9940 warning_at (DECL_SOURCE_LOCATION (decl),
9941 OPT_Wmissing_parameter_type,
9942 "type of %qD defaults to %<int%>", decl);
9945 TREE_PURPOSE (parm) = decl;
9946 seen_args.add (decl);
9949 /* Now examine the parms chain for incomplete declarations
9950 and declarations with no corresponding names. */
9952 for (b = current_scope->bindings; b; b = b->prev)
9955 if (TREE_CODE (parm) != PARM_DECL)
9958 if (TREE_TYPE (parm) != error_mark_node
9959 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9961 error_at (DECL_SOURCE_LOCATION (parm),
9962 "parameter %qD has incomplete type", parm);
9963 TREE_TYPE (parm) = error_mark_node;
9966 if (!seen_args.contains (parm))
9968 error_at (DECL_SOURCE_LOCATION (parm),
9969 "declaration for parameter %qD but no such parameter",
9972 /* Pretend the parameter was not missing.
9973 This gets us to a standard state and minimizes
9974 further error messages. */
9975 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9979 /* Chain the declarations together in the order of the list of
9980 names. Store that chain in the function decl, replacing the
9981 list of names. Update the current scope to match. */
9982 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9984 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9985 if (TREE_PURPOSE (parm))
9987 if (parm && TREE_PURPOSE (parm))
9989 last = TREE_PURPOSE (parm);
9990 DECL_ARGUMENTS (fndecl) = last;
9992 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9993 if (TREE_PURPOSE (parm))
9995 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9996 last = TREE_PURPOSE (parm);
9998 DECL_CHAIN (last) = NULL_TREE;
10001 /* If there was a previous prototype,
10002 set the DECL_ARG_TYPE of each argument according to
10003 the type previously specified, and report any mismatches. */
10005 if (current_function_prototype_arg_types)
10008 for (parm = DECL_ARGUMENTS (fndecl),
10009 type = current_function_prototype_arg_types;
10010 parm || (type != NULL_TREE
10011 && TREE_VALUE (type) != error_mark_node
10012 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
10013 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
10015 if (parm == NULL_TREE
10016 || type == NULL_TREE
10017 || (TREE_VALUE (type) != error_mark_node
10018 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
10020 if (current_function_prototype_built_in)
10021 warning_at (DECL_SOURCE_LOCATION (fndecl),
10022 0, "number of arguments doesn%'t match "
10023 "built-in prototype");
10026 /* FIXME diagnostics: This should be the location of
10027 FNDECL, but there is bug when a prototype is
10028 declared inside function context, but defined
10029 outside of it (e.g., gcc.dg/pr15698-2.c). In
10030 which case FNDECL gets the location of the
10031 prototype, not the definition. */
10032 error_at (input_location,
10033 "number of arguments doesn%'t match prototype");
10035 error_at (current_function_prototype_locus,
10036 "prototype declaration");
10040 /* Type for passing arg must be consistent with that
10041 declared for the arg. ISO C says we take the unqualified
10042 type for parameters declared with qualified type. */
10043 if (TREE_TYPE (parm) != error_mark_node
10044 && TREE_VALUE (type) != error_mark_node
10045 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10046 != TYPE_ATOMIC (TREE_VALUE (type)))
10047 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
10048 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
10050 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
10051 == TYPE_ATOMIC (TREE_VALUE (type)))
10052 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
10053 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
10055 /* Adjust argument to match prototype. E.g. a previous
10056 `int foo(float);' prototype causes
10057 `int foo(x) float x; {...}' to be treated like
10058 `int foo(float x) {...}'. This is particularly
10059 useful for argument types like uid_t. */
10060 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
10062 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
10063 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
10064 && (TYPE_PRECISION (TREE_TYPE (parm))
10065 < TYPE_PRECISION (integer_type_node)))
10066 DECL_ARG_TYPE (parm)
10067 = c_type_promotes_to (TREE_TYPE (parm));
10069 /* ??? Is it possible to get here with a
10070 built-in prototype or will it always have
10071 been diagnosed as conflicting with an
10072 old-style definition and discarded? */
10073 if (current_function_prototype_built_in)
10074 warning_at (DECL_SOURCE_LOCATION (parm),
10075 OPT_Wpedantic, "promoted argument %qD "
10076 "doesn%'t match built-in prototype", parm);
10079 pedwarn (DECL_SOURCE_LOCATION (parm),
10080 OPT_Wpedantic, "promoted argument %qD "
10081 "doesn%'t match prototype", parm);
10082 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
10083 "prototype declaration");
10088 if (current_function_prototype_built_in)
10089 warning_at (DECL_SOURCE_LOCATION (parm),
10090 0, "argument %qD doesn%'t match "
10091 "built-in prototype", parm);
10094 error_at (DECL_SOURCE_LOCATION (parm),
10095 "argument %qD doesn%'t match prototype", parm);
10096 error_at (current_function_prototype_locus,
10097 "prototype declaration");
10102 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
10105 /* Otherwise, create a prototype that would match. */
10109 tree actual = NULL_TREE, last = NULL_TREE, type;
10111 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
10113 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
10115 TREE_CHAIN (last) = type;
10120 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
10122 TREE_CHAIN (last) = type;
10126 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
10127 of the type of this function, but we need to avoid having this
10128 affect the types of other similarly-typed functions, so we must
10129 first force the generation of an identical (but separate) type
10130 node for the relevant function type. The new node we create
10131 will be a variant of the main variant of the original function
10134 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
10136 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
10140 /* Store parameter declarations passed in ARG_INFO into the current
10141 function declaration. */
10144 store_parm_decls_from (struct c_arg_info *arg_info)
10146 current_function_arg_info = arg_info;
10147 store_parm_decls ();
10150 /* Called by walk_tree to look for and update context-less labels
10151 or labels with context in the parent function. */
10154 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
10156 tree ctx = static_cast<tree>(data);
10157 if (TREE_CODE (*tp) == LABEL_EXPR
10158 && (DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE
10159 || DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == DECL_CONTEXT (ctx)))
10161 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = ctx;
10162 *walk_subtrees = 0;
10168 /* Store the parameter declarations into the current function declaration.
10169 This is called after parsing the parameter declarations, before
10170 digesting the body of the function.
10172 For an old-style definition, construct a prototype out of the old-style
10173 parameter declarations and inject it into the function's type. */
10176 store_parm_decls (void)
10178 tree fndecl = current_function_decl;
10181 /* The argument information block for FNDECL. */
10182 struct c_arg_info *arg_info = current_function_arg_info;
10183 current_function_arg_info = 0;
10185 /* True if this definition is written with a prototype. In C2X, an
10186 empty argument list was converted to (void) in grokparms; in
10187 older C standard versions, it does not give the function a type
10188 with a prototype for future calls. */
10189 proto = arg_info->types != 0;
10192 store_parm_decls_newstyle (fndecl, arg_info);
10194 store_parm_decls_oldstyle (fndecl, arg_info);
10196 /* The next call to push_scope will be a function body. */
10198 next_is_function_body = true;
10200 /* Write a record describing this function definition to the prototypes
10201 file (if requested). */
10203 gen_aux_info_record (fndecl, 1, 0, proto);
10205 /* Initialize the RTL code for the function. */
10206 allocate_struct_function (fndecl, false);
10208 if (warn_unused_local_typedefs)
10209 cfun->language = ggc_cleared_alloc<language_function> ();
10211 /* Begin the statement tree for this function. */
10212 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
10214 /* ??? Insert the contents of the pending sizes list into the function
10215 to be evaluated. The only reason left to have this is
10216 void foo(int n, int array[n++])
10217 because we throw away the array type in favor of a pointer type, and
10218 thus won't naturally see the SAVE_EXPR containing the increment. All
10219 other pending sizes would be handled by gimplify_parameters. */
10220 if (arg_info->pending_sizes)
10222 /* In very special circumstances, e.g. for code like
10224 void f (int a[i += 2]) {}
10225 we need to execute the atomic assignment on function entry.
10226 But in this case, it is not just a straight store, it has the
10227 op= form, which means that build_atomic_assign has generated
10228 gotos, labels, etc. Because at that time the function decl
10229 for F has not been created yet, those labels do not have any
10230 function context. But we have the fndecl now, so update the
10231 labels accordingly. gimplify_expr would crash otherwise.
10232 Or with nested functions the labels could be created with parent
10233 function's context, while when the statement is emitted at the
10234 start of the nested function, it needs the nested function's
10236 walk_tree_without_duplicates (&arg_info->pending_sizes,
10237 set_labels_context_r, fndecl);
10238 add_stmt (arg_info->pending_sizes);
10242 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
10243 c_finish_omp_declare_simd for function prototypes. No diagnostics
10247 temp_store_parm_decls (tree fndecl, tree parms)
10250 for (tree p = parms; p; p = DECL_CHAIN (p))
10252 DECL_CONTEXT (p) = fndecl;
10254 bind (DECL_NAME (p), p, current_scope,
10255 /*invisible=*/false, /*nested=*/false,
10260 /* Undo what temp_store_parm_decls did. */
10263 temp_pop_parm_decls (void)
10265 /* Clear all bindings in this temporary scope, so that
10266 pop_scope doesn't create a BLOCK. */
10267 struct c_binding *b = current_scope->bindings;
10268 current_scope->bindings = NULL;
10269 for (; b; b = free_binding_and_advance (b))
10271 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
10272 || b->decl == error_mark_node);
10273 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
10274 I_SYMBOL_BINDING (b->id) = b->shadowed;
10275 if (b->shadowed && b->shadowed->u.type)
10276 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
10282 /* Finish up a function declaration and compile that function
10283 all the way to assembler language output. Then free the storage
10284 for the function definition.
10286 This is called after parsing the body of the function definition. */
10289 finish_function (location_t end_loc)
10291 tree fndecl = current_function_decl;
10293 if (c_dialect_objc ())
10294 objc_finish_function ();
10296 if (TREE_CODE (fndecl) == FUNCTION_DECL
10297 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
10299 tree args = DECL_ARGUMENTS (fndecl);
10300 for (; args; args = DECL_CHAIN (args))
10302 tree type = TREE_TYPE (args);
10303 if (INTEGRAL_TYPE_P (type)
10304 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
10305 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
10309 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
10310 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
10312 /* Must mark the RESULT_DECL as being in this function. */
10314 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
10315 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
10317 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
10318 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
10319 == integer_type_node && flag_isoc99)
10321 /* Hack. We don't want the middle-end to warn that this return
10322 is unreachable, so we mark its location as special. Using
10323 UNKNOWN_LOCATION has the problem that it gets clobbered in
10324 annotate_one_with_locus. A cleaner solution might be to
10325 ensure ! should_carry_locus_p (stmt), but that needs a flag.
10327 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
10330 /* Tie off the statement tree for this function. */
10331 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
10333 finish_fname_decls ();
10335 /* Complain if there's no return statement only if option specified on
10337 if (warn_return_type > 0
10338 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
10339 && !current_function_returns_value && !current_function_returns_null
10340 /* Don't complain if we are no-return. */
10341 && !current_function_returns_abnormally
10342 /* Don't complain if we are declared noreturn. */
10343 && !TREE_THIS_VOLATILE (fndecl)
10344 /* Don't warn for main(). */
10345 && !MAIN_NAME_P (DECL_NAME (fndecl))
10346 /* Or if they didn't actually specify a return type. */
10347 && !C_FUNCTION_IMPLICIT_INT (fndecl)
10348 /* Normally, with -Wreturn-type, flow will complain, but we might
10349 optimize out static functions. */
10350 && !TREE_PUBLIC (fndecl)
10351 && targetm.warn_func_return (fndecl)
10352 && warning (OPT_Wreturn_type,
10353 "no return statement in function returning non-void"))
10354 suppress_warning (fndecl, OPT_Wreturn_type);
10356 /* Complain about parameters that are only set, but never otherwise used. */
10357 if (warn_unused_but_set_parameter)
10361 for (decl = DECL_ARGUMENTS (fndecl);
10363 decl = DECL_CHAIN (decl))
10364 if (TREE_USED (decl)
10365 && TREE_CODE (decl) == PARM_DECL
10366 && !DECL_READ_P (decl)
10367 && DECL_NAME (decl)
10368 && !DECL_ARTIFICIAL (decl)
10369 && !warning_suppressed_p (decl, OPT_Wunused_but_set_parameter))
10370 warning_at (DECL_SOURCE_LOCATION (decl),
10371 OPT_Wunused_but_set_parameter,
10372 "parameter %qD set but not used", decl);
10375 /* Complain about locally defined typedefs that are not used in this
10377 maybe_warn_unused_local_typedefs ();
10379 /* Possibly warn about unused parameters. */
10380 if (warn_unused_parameter)
10381 do_warn_unused_parameter (fndecl);
10383 /* Store the end of the function, so that we get good line number
10384 info for the epilogue. */
10385 cfun->function_end_locus = end_loc;
10387 /* Finalize the ELF visibility for the function. */
10388 c_determine_visibility (fndecl);
10390 /* For GNU C extern inline functions disregard inline limits. */
10391 if (DECL_EXTERNAL (fndecl)
10392 && DECL_DECLARED_INLINE_P (fndecl)
10393 && (flag_gnu89_inline
10394 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
10395 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
10397 /* Genericize before inlining. Delay genericizing nested functions
10398 until their parent function is genericized. Since finalizing
10399 requires GENERIC, delay that as well. */
10401 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
10402 && !undef_nested_function)
10404 if (!decl_function_context (fndecl))
10406 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
10407 c_genericize (fndecl);
10409 /* ??? Objc emits functions after finalizing the compilation unit.
10410 This should be cleaned up later and this conditional removed. */
10411 if (symtab->global_info_ready)
10413 cgraph_node::add_new_function (fndecl, false);
10416 cgraph_node::finalize_function (fndecl, false);
10420 /* Register this function with cgraph just far enough to get it
10421 added to our parent's nested function list. Handy, since the
10422 C front end doesn't have such a list. */
10423 (void) cgraph_node::get_create (fndecl);
10427 if (!decl_function_context (fndecl))
10428 undef_nested_function = false;
10430 if (cfun->language != NULL)
10432 ggc_free (cfun->language);
10433 cfun->language = NULL;
10436 /* We're leaving the context of this function, so zap cfun.
10437 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
10438 tree_rest_of_compilation. */
10440 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
10441 current_function_decl = NULL;
10444 /* Check the declarations given in a for-loop for satisfying the C99
10445 constraints. If exactly one such decl is found, return it. LOC is
10446 the location of the opening parenthesis of the for loop. The last
10447 parameter allows you to control the "for loop initial declarations
10448 are only allowed in C99 mode". Normally, you should pass
10449 flag_isoc99 as that parameter. But in some cases (Objective-C
10450 foreach loop, for example) we want to run the checks in this
10451 function even if not in C99 mode, so we allow the caller to turn
10452 off the error about not being in C99 mode.
10456 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
10458 struct c_binding *b;
10459 tree one_decl = NULL_TREE;
10462 if (!turn_off_iso_c99_error)
10464 static bool hint = true;
10465 /* If we get here, declarations have been used in a for loop without
10466 the C99 for loop scope. This doesn't make much sense, so don't
10468 auto_diagnostic_group d;
10469 error_at (loc, "%<for%> loop initial declarations "
10470 "are only allowed in C99 or C11 mode");
10474 "use option %<-std=c99%>, %<-std=gnu99%>, %<-std=c11%> or "
10475 "%<-std=gnu11%> to compile your code");
10481 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
10482 "initial declarations");
10484 /* C99 subclause 6.8.5 paragraph 3:
10486 [#3] The declaration part of a for statement shall only
10487 declare identifiers for objects having storage class auto or
10490 It isn't clear whether, in this sentence, "identifiers" binds to
10491 "shall only declare" or to "objects" - that is, whether all identifiers
10492 declared must be identifiers for objects, or whether the restriction
10493 only applies to those that are. (A question on this in comp.std.c
10494 in November 2000 received no answer.) We implement the strictest
10495 interpretation, to avoid creating an extension which later causes
10498 for (b = current_scope->bindings; b; b = b->prev)
10501 tree decl = b->decl;
10506 switch (TREE_CODE (decl))
10510 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
10511 if (TREE_STATIC (decl))
10512 error_at (decl_loc,
10513 "declaration of static variable %qD in %<for%> loop "
10514 "initial declaration", decl);
10515 else if (DECL_EXTERNAL (decl))
10516 error_at (decl_loc,
10517 "declaration of %<extern%> variable %qD in %<for%> loop "
10518 "initial declaration", decl);
10524 "%<struct %E%> declared in %<for%> loop initial "
10525 "declaration", id);
10529 "%<union %E%> declared in %<for%> loop initial declaration",
10532 case ENUMERAL_TYPE:
10533 error_at (loc, "%<enum %E%> declared in %<for%> loop "
10534 "initial declaration", id);
10537 error_at (loc, "declaration of non-variable "
10538 "%qD in %<for%> loop initial declaration", decl);
10545 return n_decls == 1 ? one_decl : NULL_TREE;
10548 /* Save and reinitialize the variables
10549 used during compilation of a C function. */
10552 c_push_function_context (void)
10554 struct language_function *p = cfun->language;
10555 /* cfun->language might have been already allocated by the use of
10556 -Wunused-local-typedefs. In that case, just re-use it. */
10558 cfun->language = p = ggc_cleared_alloc<language_function> ();
10560 p->base.x_stmt_tree = c_stmt_tree;
10561 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
10562 p->x_in_statement = in_statement;
10563 p->x_switch_stack = c_switch_stack;
10564 p->arg_info = current_function_arg_info;
10565 p->returns_value = current_function_returns_value;
10566 p->returns_null = current_function_returns_null;
10567 p->returns_abnormally = current_function_returns_abnormally;
10568 p->warn_about_return_type = warn_about_return_type;
10570 push_function_context ();
10573 /* Restore the variables used during compilation of a C function. */
10576 c_pop_function_context (void)
10578 struct language_function *p;
10580 pop_function_context ();
10581 p = cfun->language;
10583 /* When -Wunused-local-typedefs is in effect, cfun->languages is
10584 used to store data throughout the life time of the current cfun,
10585 So don't deallocate it. */
10586 if (!warn_unused_local_typedefs)
10587 cfun->language = NULL;
10589 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
10590 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
10592 /* Stop pointing to the local nodes about to be freed. */
10593 /* But DECL_INITIAL must remain nonzero so we know this
10594 was an actual function definition. */
10595 DECL_INITIAL (current_function_decl) = error_mark_node;
10596 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
10599 c_stmt_tree = p->base.x_stmt_tree;
10600 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
10601 in_statement = p->x_in_statement;
10602 c_switch_stack = p->x_switch_stack;
10603 current_function_arg_info = p->arg_info;
10604 current_function_returns_value = p->returns_value;
10605 current_function_returns_null = p->returns_null;
10606 current_function_returns_abnormally = p->returns_abnormally;
10607 warn_about_return_type = p->warn_about_return_type;
10610 /* The functions below are required for functionality of doing
10611 function at once processing in the C front end. Currently these
10612 functions are not called from anywhere in the C front end, but as
10613 these changes continue, that will change. */
10615 /* Returns the stmt_tree (if any) to which statements are currently
10616 being added. If there is no active statement-tree, NULL is
10620 current_stmt_tree (void)
10622 return &c_stmt_tree;
10625 /* Return the global value of T as a symbol. */
10628 identifier_global_value (tree t)
10630 struct c_binding *b;
10632 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
10633 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10639 /* Return the global value of tag T as a symbol. */
10642 identifier_global_tag (tree t)
10644 struct c_binding *b;
10646 for (b = I_TAG_BINDING (t); b; b = b->shadowed)
10647 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
10653 /* Returns true if NAME refers to a built-in function or function-like
10657 names_builtin_p (const char *name)
10659 tree id = get_identifier (name);
10660 if (tree decl = identifier_global_value (id))
10661 return TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_UNDECLARED_BUILTIN (decl);
10663 /* Also detect common reserved C words that aren't strictly built-in
10665 switch (C_RID_CODE (id))
10667 case RID_BUILTIN_CONVERTVECTOR:
10668 case RID_BUILTIN_HAS_ATTRIBUTE:
10669 case RID_BUILTIN_SHUFFLE:
10670 case RID_BUILTIN_SHUFFLEVECTOR:
10671 case RID_BUILTIN_ASSOC_BARRIER:
10672 case RID_CHOOSE_EXPR:
10674 case RID_TYPES_COMPATIBLE_P:
10683 /* In C, the only C-linkage public declaration is at file scope. */
10686 c_linkage_bindings (tree name)
10688 return identifier_global_value (name);
10691 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
10692 otherwise the name is found in ridpointers from RID_INDEX. */
10695 record_builtin_type (enum rid rid_index, const char *name, tree type)
10699 id = ridpointers[(int) rid_index];
10701 id = get_identifier (name);
10702 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
10704 if (debug_hooks->type_decl)
10705 debug_hooks->type_decl (decl, false);
10708 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
10711 build_c_parm (struct c_declspecs *specs, tree attrs,
10712 struct c_declarator *declarator,
10715 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
10716 ret->specs = specs;
10717 ret->attrs = attrs;
10718 ret->declarator = declarator;
10723 /* Return a declarator with nested attributes. TARGET is the inner
10724 declarator to which these attributes apply. ATTRS are the
10727 struct c_declarator *
10728 build_attrs_declarator (tree attrs, struct c_declarator *target)
10730 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10731 ret->kind = cdk_attrs;
10732 ret->declarator = target;
10733 ret->u.attrs = attrs;
10737 /* Return a declarator for a function with arguments specified by ARGS
10738 and return type specified by TARGET. */
10740 struct c_declarator *
10741 build_function_declarator (struct c_arg_info *args,
10742 struct c_declarator *target)
10744 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10745 ret->kind = cdk_function;
10746 ret->declarator = target;
10747 ret->u.arg_info = args;
10751 /* Return a declarator for the identifier IDENT (which may be
10752 NULL_TREE for an abstract declarator). */
10754 struct c_declarator *
10755 build_id_declarator (tree ident)
10757 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10758 ret->kind = cdk_id;
10759 ret->declarator = 0;
10760 ret->u.id.id = ident;
10761 ret->u.id.attrs = NULL_TREE;
10762 /* Default value - may get reset to a more precise location. */
10763 ret->id_loc = input_location;
10767 /* Return something to represent absolute declarators containing a *.
10768 TARGET is the absolute declarator that the * contains.
10769 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
10770 to apply to the pointer type. */
10772 struct c_declarator *
10773 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
10774 struct c_declarator *target)
10778 struct c_declarator *itarget = target;
10779 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
10780 if (type_quals_attrs)
10782 attrs = type_quals_attrs->attrs;
10783 quals = quals_from_declspecs (type_quals_attrs);
10784 if (attrs != NULL_TREE)
10785 itarget = build_attrs_declarator (attrs, target);
10787 ret->kind = cdk_pointer;
10788 ret->declarator = itarget;
10789 ret->u.pointer_quals = quals;
10793 /* Return a pointer to a structure for an empty list of declaration
10796 struct c_declspecs *
10797 build_null_declspecs (void)
10799 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
10800 memset (ret, 0, sizeof *ret);
10801 ret->align_log = -1;
10802 ret->typespec_word = cts_none;
10803 ret->storage_class = csc_none;
10804 ret->expr_const_operands = true;
10805 ret->typespec_kind = ctsk_none;
10806 ret->address_space = ADDR_SPACE_GENERIC;
10810 /* Add the address space ADDRSPACE to the declaration specifiers
10811 SPECS, returning SPECS. */
10813 struct c_declspecs *
10814 declspecs_add_addrspace (location_t location,
10815 struct c_declspecs *specs, addr_space_t as)
10817 specs->non_sc_seen_p = true;
10818 specs->declspecs_seen_p = true;
10819 specs->non_std_attrs_seen_p = true;
10821 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
10822 && specs->address_space != as)
10823 error ("incompatible address space qualifiers %qs and %qs",
10824 c_addr_space_name (as),
10825 c_addr_space_name (specs->address_space));
10828 specs->address_space = as;
10829 specs->locations[cdw_address_space] = location;
10834 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
10835 returning SPECS. */
10837 struct c_declspecs *
10838 declspecs_add_qual (location_t loc,
10839 struct c_declspecs *specs, tree qual)
10843 specs->non_sc_seen_p = true;
10844 specs->declspecs_seen_p = true;
10845 specs->non_std_attrs_seen_p = true;
10846 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
10847 && C_IS_RESERVED_WORD (qual));
10848 i = C_RID_CODE (qual);
10849 location_t prev_loc = UNKNOWN_LOCATION;
10853 dupe = specs->const_p;
10854 specs->const_p = true;
10855 prev_loc = specs->locations[cdw_const];
10856 specs->locations[cdw_const] = loc;
10859 dupe = specs->volatile_p;
10860 specs->volatile_p = true;
10861 prev_loc = specs->locations[cdw_volatile];
10862 specs->locations[cdw_volatile] = loc;
10865 dupe = specs->restrict_p;
10866 specs->restrict_p = true;
10867 prev_loc = specs->locations[cdw_restrict];
10868 specs->locations[cdw_restrict] = loc;
10871 dupe = specs->atomic_p;
10872 specs->atomic_p = true;
10873 prev_loc = specs->locations[cdw_atomic];
10874 specs->locations[cdw_atomic] = loc;
10877 gcc_unreachable ();
10881 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10882 "duplicate %qE declaration specifier", qual);
10884 && warn_duplicate_decl_specifier
10885 && prev_loc >= RESERVED_LOCATION_COUNT
10886 && !from_macro_expansion_at (prev_loc)
10887 && !from_macro_expansion_at (loc))
10888 warning_at (loc, OPT_Wduplicate_decl_specifier,
10889 "duplicate %qE declaration specifier", qual);
10894 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10895 returning SPECS. */
10897 struct c_declspecs *
10898 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10899 struct c_typespec spec)
10901 tree type = spec.spec;
10902 specs->non_sc_seen_p = true;
10903 specs->declspecs_seen_p = true;
10904 specs->non_std_attrs_seen_p = true;
10905 specs->typespec_kind = spec.kind;
10906 if (TREE_DEPRECATED (type))
10907 specs->deprecated_p = true;
10908 if (TREE_UNAVAILABLE (type))
10909 specs->unavailable_p = true;
10911 /* Handle type specifier keywords. */
10912 if (TREE_CODE (type) == IDENTIFIER_NODE
10913 && C_IS_RESERVED_WORD (type)
10914 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10916 enum rid i = C_RID_CODE (type);
10919 error_at (loc, "two or more data types in declaration specifiers");
10922 if ((int) i <= (int) RID_LAST_MODIFIER)
10924 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10929 if (specs->long_long_p)
10931 error_at (loc, "%<long long long%> is too long for GCC");
10936 if (specs->typespec_word == cts_double)
10939 ("both %<long long%> and %<double%> in "
10940 "declaration specifiers"));
10943 pedwarn_c90 (loc, OPT_Wlong_long,
10944 "ISO C90 does not support %<long long%>");
10945 specs->long_long_p = 1;
10946 specs->locations[cdw_long_long] = loc;
10949 if (specs->short_p)
10951 ("both %<long%> and %<short%> in "
10952 "declaration specifiers"));
10953 else if (specs->typespec_word == cts_auto_type)
10955 ("both %<long%> and %<__auto_type%> in "
10956 "declaration specifiers"));
10957 else if (specs->typespec_word == cts_void)
10959 ("both %<long%> and %<void%> in "
10960 "declaration specifiers"));
10961 else if (specs->typespec_word == cts_int_n)
10963 ("both %<long%> and %<__int%d%> in "
10964 "declaration specifiers"),
10965 int_n_data[specs->int_n_idx].bitsize);
10966 else if (specs->typespec_word == cts_bool)
10968 ("both %<long%> and %<_Bool%> in "
10969 "declaration specifiers"));
10970 else if (specs->typespec_word == cts_char)
10972 ("both %<long%> and %<char%> in "
10973 "declaration specifiers"));
10974 else if (specs->typespec_word == cts_float)
10976 ("both %<long%> and %<float%> in "
10977 "declaration specifiers"));
10978 else if (specs->typespec_word == cts_floatn_nx)
10980 ("both %<long%> and %<_Float%d%s%> in "
10981 "declaration specifiers"),
10982 floatn_nx_types[specs->floatn_nx_idx].n,
10983 (floatn_nx_types[specs->floatn_nx_idx].extended
10986 else if (specs->typespec_word == cts_dfloat32)
10988 ("both %<long%> and %<_Decimal32%> in "
10989 "declaration specifiers"));
10990 else if (specs->typespec_word == cts_dfloat64)
10992 ("both %<long%> and %<_Decimal64%> in "
10993 "declaration specifiers"));
10994 else if (specs->typespec_word == cts_dfloat128)
10996 ("both %<long%> and %<_Decimal128%> in "
10997 "declaration specifiers"));
11000 specs->long_p = true;
11001 specs->locations[cdw_long] = loc;
11005 dupe = specs->short_p;
11008 ("both %<long%> and %<short%> in "
11009 "declaration specifiers"));
11010 else if (specs->typespec_word == cts_auto_type)
11012 ("both %<short%> and %<__auto_type%> in "
11013 "declaration specifiers"));
11014 else if (specs->typespec_word == cts_void)
11016 ("both %<short%> and %<void%> in "
11017 "declaration specifiers"));
11018 else if (specs->typespec_word == cts_int_n)
11020 ("both %<short%> and %<__int%d%> in "
11021 "declaration specifiers"),
11022 int_n_data[specs->int_n_idx].bitsize);
11023 else if (specs->typespec_word == cts_bool)
11025 ("both %<short%> and %<_Bool%> in "
11026 "declaration specifiers"));
11027 else if (specs->typespec_word == cts_char)
11029 ("both %<short%> and %<char%> in "
11030 "declaration specifiers"));
11031 else if (specs->typespec_word == cts_float)
11033 ("both %<short%> and %<float%> in "
11034 "declaration specifiers"));
11035 else if (specs->typespec_word == cts_double)
11037 ("both %<short%> and %<double%> in "
11038 "declaration specifiers"));
11039 else if (specs->typespec_word == cts_floatn_nx)
11041 ("both %<short%> and %<_Float%d%s%> in "
11042 "declaration specifiers"),
11043 floatn_nx_types[specs->floatn_nx_idx].n,
11044 (floatn_nx_types[specs->floatn_nx_idx].extended
11047 else if (specs->typespec_word == cts_dfloat32)
11049 ("both %<short%> and %<_Decimal32%> in "
11050 "declaration specifiers"));
11051 else if (specs->typespec_word == cts_dfloat64)
11053 ("both %<short%> and %<_Decimal64%> in "
11054 "declaration specifiers"));
11055 else if (specs->typespec_word == cts_dfloat128)
11057 ("both %<short%> and %<_Decimal128%> in "
11058 "declaration specifiers"));
11061 specs->short_p = true;
11062 specs->locations[cdw_short] = loc;
11066 dupe = specs->signed_p;
11067 if (specs->unsigned_p)
11069 ("both %<signed%> and %<unsigned%> in "
11070 "declaration specifiers"));
11071 else if (specs->typespec_word == cts_auto_type)
11073 ("both %<signed%> and %<__auto_type%> in "
11074 "declaration specifiers"));
11075 else if (specs->typespec_word == cts_void)
11077 ("both %<signed%> and %<void%> in "
11078 "declaration specifiers"));
11079 else if (specs->typespec_word == cts_bool)
11081 ("both %<signed%> and %<_Bool%> in "
11082 "declaration specifiers"));
11083 else if (specs->typespec_word == cts_float)
11085 ("both %<signed%> and %<float%> in "
11086 "declaration specifiers"));
11087 else if (specs->typespec_word == cts_double)
11089 ("both %<signed%> and %<double%> in "
11090 "declaration specifiers"));
11091 else if (specs->typespec_word == cts_floatn_nx)
11093 ("both %<signed%> and %<_Float%d%s%> in "
11094 "declaration specifiers"),
11095 floatn_nx_types[specs->floatn_nx_idx].n,
11096 (floatn_nx_types[specs->floatn_nx_idx].extended
11099 else if (specs->typespec_word == cts_dfloat32)
11101 ("both %<signed%> and %<_Decimal32%> in "
11102 "declaration specifiers"));
11103 else if (specs->typespec_word == cts_dfloat64)
11105 ("both %<signed%> and %<_Decimal64%> in "
11106 "declaration specifiers"));
11107 else if (specs->typespec_word == cts_dfloat128)
11109 ("both %<signed%> and %<_Decimal128%> in "
11110 "declaration specifiers"));
11113 specs->signed_p = true;
11114 specs->locations[cdw_signed] = loc;
11118 dupe = specs->unsigned_p;
11119 if (specs->signed_p)
11121 ("both %<signed%> and %<unsigned%> in "
11122 "declaration specifiers"));
11123 else if (specs->typespec_word == cts_auto_type)
11125 ("both %<unsigned%> and %<__auto_type%> in "
11126 "declaration specifiers"));
11127 else if (specs->typespec_word == cts_void)
11129 ("both %<unsigned%> and %<void%> in "
11130 "declaration specifiers"));
11131 else if (specs->typespec_word == cts_bool)
11133 ("both %<unsigned%> and %<_Bool%> in "
11134 "declaration specifiers"));
11135 else if (specs->typespec_word == cts_float)
11137 ("both %<unsigned%> and %<float%> in "
11138 "declaration specifiers"));
11139 else if (specs->typespec_word == cts_double)
11141 ("both %<unsigned%> and %<double%> in "
11142 "declaration specifiers"));
11143 else if (specs->typespec_word == cts_floatn_nx)
11145 ("both %<unsigned%> and %<_Float%d%s%> in "
11146 "declaration specifiers"),
11147 floatn_nx_types[specs->floatn_nx_idx].n,
11148 (floatn_nx_types[specs->floatn_nx_idx].extended
11151 else if (specs->typespec_word == cts_dfloat32)
11153 ("both %<unsigned%> and %<_Decimal32%> in "
11154 "declaration specifiers"));
11155 else if (specs->typespec_word == cts_dfloat64)
11157 ("both %<unsigned%> and %<_Decimal64%> in "
11158 "declaration specifiers"));
11159 else if (specs->typespec_word == cts_dfloat128)
11161 ("both %<unsigned%> and %<_Decimal128%> in "
11162 "declaration specifiers"));
11165 specs->unsigned_p = true;
11166 specs->locations[cdw_unsigned] = loc;
11170 dupe = specs->complex_p;
11171 if (!in_system_header_at (loc))
11172 pedwarn_c90 (loc, OPT_Wpedantic,
11173 "ISO C90 does not support complex types");
11174 if (specs->typespec_word == cts_auto_type)
11176 ("both %<complex%> and %<__auto_type%> in "
11177 "declaration specifiers"));
11178 else if (specs->typespec_word == cts_void)
11180 ("both %<complex%> and %<void%> in "
11181 "declaration specifiers"));
11182 else if (specs->typespec_word == cts_bool)
11184 ("both %<complex%> and %<_Bool%> in "
11185 "declaration specifiers"));
11186 else if (specs->typespec_word == cts_dfloat32)
11188 ("both %<complex%> and %<_Decimal32%> in "
11189 "declaration specifiers"));
11190 else if (specs->typespec_word == cts_dfloat64)
11192 ("both %<complex%> and %<_Decimal64%> in "
11193 "declaration specifiers"));
11194 else if (specs->typespec_word == cts_dfloat128)
11196 ("both %<complex%> and %<_Decimal128%> in "
11197 "declaration specifiers"));
11198 else if (specs->typespec_word == cts_fract)
11200 ("both %<complex%> and %<_Fract%> in "
11201 "declaration specifiers"));
11202 else if (specs->typespec_word == cts_accum)
11204 ("both %<complex%> and %<_Accum%> in "
11205 "declaration specifiers"));
11206 else if (specs->saturating_p)
11208 ("both %<complex%> and %<_Sat%> in "
11209 "declaration specifiers"));
11212 specs->complex_p = true;
11213 specs->locations[cdw_complex] = loc;
11217 dupe = specs->saturating_p;
11218 pedwarn (loc, OPT_Wpedantic,
11219 "ISO C does not support saturating types");
11220 if (specs->typespec_word == cts_int_n)
11223 ("both %<_Sat%> and %<__int%d%> in "
11224 "declaration specifiers"),
11225 int_n_data[specs->int_n_idx].bitsize);
11227 else if (specs->typespec_word == cts_auto_type)
11229 ("both %<_Sat%> and %<__auto_type%> in "
11230 "declaration specifiers"));
11231 else if (specs->typespec_word == cts_void)
11233 ("both %<_Sat%> and %<void%> in "
11234 "declaration specifiers"));
11235 else if (specs->typespec_word == cts_bool)
11237 ("both %<_Sat%> and %<_Bool%> in "
11238 "declaration specifiers"));
11239 else if (specs->typespec_word == cts_char)
11241 ("both %<_Sat%> and %<char%> in "
11242 "declaration specifiers"));
11243 else if (specs->typespec_word == cts_int)
11245 ("both %<_Sat%> and %<int%> in "
11246 "declaration specifiers"));
11247 else if (specs->typespec_word == cts_float)
11249 ("both %<_Sat%> and %<float%> in "
11250 "declaration specifiers"));
11251 else if (specs->typespec_word == cts_double)
11253 ("both %<_Sat%> and %<double%> in "
11254 "declaration specifiers"));
11255 else if (specs->typespec_word == cts_floatn_nx)
11257 ("both %<_Sat%> and %<_Float%d%s%> in "
11258 "declaration specifiers"),
11259 floatn_nx_types[specs->floatn_nx_idx].n,
11260 (floatn_nx_types[specs->floatn_nx_idx].extended
11263 else if (specs->typespec_word == cts_dfloat32)
11265 ("both %<_Sat%> and %<_Decimal32%> in "
11266 "declaration specifiers"));
11267 else if (specs->typespec_word == cts_dfloat64)
11269 ("both %<_Sat%> and %<_Decimal64%> in "
11270 "declaration specifiers"));
11271 else if (specs->typespec_word == cts_dfloat128)
11273 ("both %<_Sat%> and %<_Decimal128%> in "
11274 "declaration specifiers"));
11275 else if (specs->complex_p)
11277 ("both %<_Sat%> and %<complex%> in "
11278 "declaration specifiers"));
11281 specs->saturating_p = true;
11282 specs->locations[cdw_saturating] = loc;
11286 gcc_unreachable ();
11290 error_at (loc, "duplicate %qE", type);
11296 /* "void", "_Bool", "char", "int", "float", "double",
11297 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
11298 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
11300 if (specs->typespec_word != cts_none)
11303 "two or more data types in declaration specifiers");
11308 case RID_AUTO_TYPE:
11311 ("both %<long%> and %<__auto_type%> in "
11312 "declaration specifiers"));
11313 else if (specs->short_p)
11315 ("both %<short%> and %<__auto_type%> in "
11316 "declaration specifiers"));
11317 else if (specs->signed_p)
11319 ("both %<signed%> and %<__auto_type%> in "
11320 "declaration specifiers"));
11321 else if (specs->unsigned_p)
11323 ("both %<unsigned%> and %<__auto_type%> in "
11324 "declaration specifiers"));
11325 else if (specs->complex_p)
11327 ("both %<complex%> and %<__auto_type%> in "
11328 "declaration specifiers"));
11329 else if (specs->saturating_p)
11331 ("both %<_Sat%> and %<__auto_type%> in "
11332 "declaration specifiers"));
11335 specs->typespec_word = cts_auto_type;
11336 specs->locations[cdw_typespec] = loc;
11343 specs->int_n_idx = i - RID_INT_N_0;
11344 if (!in_system_header_at (input_location)
11345 /* If the INT_N type ends in "__", and so is of the format
11346 "__intN__", don't pedwarn. */
11347 && (strncmp (IDENTIFIER_POINTER (type)
11348 + (IDENTIFIER_LENGTH (type) - 2), "__", 2) != 0))
11349 pedwarn (loc, OPT_Wpedantic,
11350 "ISO C does not support %<__int%d%> types",
11351 int_n_data[specs->int_n_idx].bitsize);
11355 ("both %<__int%d%> and %<long%> in "
11356 "declaration specifiers"),
11357 int_n_data[specs->int_n_idx].bitsize);
11358 else if (specs->saturating_p)
11360 ("both %<_Sat%> and %<__int%d%> in "
11361 "declaration specifiers"),
11362 int_n_data[specs->int_n_idx].bitsize);
11363 else if (specs->short_p)
11365 ("both %<__int%d%> and %<short%> in "
11366 "declaration specifiers"),
11367 int_n_data[specs->int_n_idx].bitsize);
11368 else if (! int_n_enabled_p[specs->int_n_idx])
11370 specs->typespec_word = cts_int_n;
11372 "%<__int%d%> is not supported on this target",
11373 int_n_data[specs->int_n_idx].bitsize);
11377 specs->typespec_word = cts_int_n;
11378 specs->locations[cdw_typespec] = loc;
11384 ("both %<long%> and %<void%> in "
11385 "declaration specifiers"));
11386 else if (specs->short_p)
11388 ("both %<short%> and %<void%> in "
11389 "declaration specifiers"));
11390 else if (specs->signed_p)
11392 ("both %<signed%> and %<void%> in "
11393 "declaration specifiers"));
11394 else if (specs->unsigned_p)
11396 ("both %<unsigned%> and %<void%> in "
11397 "declaration specifiers"));
11398 else if (specs->complex_p)
11400 ("both %<complex%> and %<void%> in "
11401 "declaration specifiers"));
11402 else if (specs->saturating_p)
11404 ("both %<_Sat%> and %<void%> in "
11405 "declaration specifiers"));
11408 specs->typespec_word = cts_void;
11409 specs->locations[cdw_typespec] = loc;
11413 if (!in_system_header_at (loc))
11414 pedwarn_c90 (loc, OPT_Wpedantic,
11415 "ISO C90 does not support boolean types");
11418 ("both %<long%> and %<_Bool%> in "
11419 "declaration specifiers"));
11420 else if (specs->short_p)
11422 ("both %<short%> and %<_Bool%> in "
11423 "declaration specifiers"));
11424 else if (specs->signed_p)
11426 ("both %<signed%> and %<_Bool%> in "
11427 "declaration specifiers"));
11428 else if (specs->unsigned_p)
11430 ("both %<unsigned%> and %<_Bool%> in "
11431 "declaration specifiers"));
11432 else if (specs->complex_p)
11434 ("both %<complex%> and %<_Bool%> in "
11435 "declaration specifiers"));
11436 else if (specs->saturating_p)
11438 ("both %<_Sat%> and %<_Bool%> in "
11439 "declaration specifiers"));
11442 specs->typespec_word = cts_bool;
11443 specs->locations[cdw_typespec] = loc;
11449 ("both %<long%> and %<char%> in "
11450 "declaration specifiers"));
11451 else if (specs->short_p)
11453 ("both %<short%> and %<char%> in "
11454 "declaration specifiers"));
11455 else if (specs->saturating_p)
11457 ("both %<_Sat%> and %<char%> in "
11458 "declaration specifiers"));
11461 specs->typespec_word = cts_char;
11462 specs->locations[cdw_typespec] = loc;
11466 if (specs->saturating_p)
11468 ("both %<_Sat%> and %<int%> in "
11469 "declaration specifiers"));
11472 specs->typespec_word = cts_int;
11473 specs->locations[cdw_typespec] = loc;
11479 ("both %<long%> and %<float%> in "
11480 "declaration specifiers"));
11481 else if (specs->short_p)
11483 ("both %<short%> and %<float%> in "
11484 "declaration specifiers"));
11485 else if (specs->signed_p)
11487 ("both %<signed%> and %<float%> in "
11488 "declaration specifiers"));
11489 else if (specs->unsigned_p)
11491 ("both %<unsigned%> and %<float%> in "
11492 "declaration specifiers"));
11493 else if (specs->saturating_p)
11495 ("both %<_Sat%> and %<float%> in "
11496 "declaration specifiers"));
11499 specs->typespec_word = cts_float;
11500 specs->locations[cdw_typespec] = loc;
11504 if (specs->long_long_p)
11506 ("both %<long long%> and %<double%> in "
11507 "declaration specifiers"));
11508 else if (specs->short_p)
11510 ("both %<short%> and %<double%> in "
11511 "declaration specifiers"));
11512 else if (specs->signed_p)
11514 ("both %<signed%> and %<double%> in "
11515 "declaration specifiers"));
11516 else if (specs->unsigned_p)
11518 ("both %<unsigned%> and %<double%> in "
11519 "declaration specifiers"));
11520 else if (specs->saturating_p)
11522 ("both %<_Sat%> and %<double%> in "
11523 "declaration specifiers"));
11526 specs->typespec_word = cts_double;
11527 specs->locations[cdw_typespec] = loc;
11530 CASE_RID_FLOATN_NX:
11531 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
11532 if (!in_system_header_at (input_location))
11533 pedwarn (loc, OPT_Wpedantic,
11534 "ISO C does not support the %<_Float%d%s%> type",
11535 floatn_nx_types[specs->floatn_nx_idx].n,
11536 (floatn_nx_types[specs->floatn_nx_idx].extended
11542 ("both %<long%> and %<_Float%d%s%> in "
11543 "declaration specifiers"),
11544 floatn_nx_types[specs->floatn_nx_idx].n,
11545 (floatn_nx_types[specs->floatn_nx_idx].extended
11548 else if (specs->short_p)
11550 ("both %<short%> and %<_Float%d%s%> in "
11551 "declaration specifiers"),
11552 floatn_nx_types[specs->floatn_nx_idx].n,
11553 (floatn_nx_types[specs->floatn_nx_idx].extended
11556 else if (specs->signed_p)
11558 ("both %<signed%> and %<_Float%d%s%> in "
11559 "declaration specifiers"),
11560 floatn_nx_types[specs->floatn_nx_idx].n,
11561 (floatn_nx_types[specs->floatn_nx_idx].extended
11564 else if (specs->unsigned_p)
11566 ("both %<unsigned%> and %<_Float%d%s%> in "
11567 "declaration specifiers"),
11568 floatn_nx_types[specs->floatn_nx_idx].n,
11569 (floatn_nx_types[specs->floatn_nx_idx].extended
11572 else if (specs->saturating_p)
11574 ("both %<_Sat%> and %<_Float%d%s%> in "
11575 "declaration specifiers"),
11576 floatn_nx_types[specs->floatn_nx_idx].n,
11577 (floatn_nx_types[specs->floatn_nx_idx].extended
11580 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11582 specs->typespec_word = cts_floatn_nx;
11584 "%<_Float%d%s%> is not supported on this target",
11585 floatn_nx_types[specs->floatn_nx_idx].n,
11586 (floatn_nx_types[specs->floatn_nx_idx].extended
11592 specs->typespec_word = cts_floatn_nx;
11593 specs->locations[cdw_typespec] = loc;
11598 case RID_DFLOAT128:
11601 if (i == RID_DFLOAT32)
11602 str = "_Decimal32";
11603 else if (i == RID_DFLOAT64)
11604 str = "_Decimal64";
11606 str = "_Decimal128";
11607 if (specs->long_long_p)
11609 ("both %<long long%> and %qs in "
11610 "declaration specifiers"),
11614 ("both %<long%> and %qs in "
11615 "declaration specifiers"),
11617 else if (specs->short_p)
11619 ("both %<short%> and %qs in "
11620 "declaration specifiers"),
11622 else if (specs->signed_p)
11624 ("both %<signed%> and %qs in "
11625 "declaration specifiers"),
11627 else if (specs->unsigned_p)
11629 ("both %<unsigned%> and %qs in "
11630 "declaration specifiers"),
11632 else if (specs->complex_p)
11634 ("both %<complex%> and %qs in "
11635 "declaration specifiers"),
11637 else if (specs->saturating_p)
11639 ("both %<_Sat%> and %qs in "
11640 "declaration specifiers"),
11642 else if (i == RID_DFLOAT32)
11643 specs->typespec_word = cts_dfloat32;
11644 else if (i == RID_DFLOAT64)
11645 specs->typespec_word = cts_dfloat64;
11647 specs->typespec_word = cts_dfloat128;
11648 specs->locations[cdw_typespec] = loc;
11650 if (!targetm.decimal_float_supported_p ())
11652 ("decimal floating-point not supported "
11653 "for this target"));
11654 pedwarn_c11 (loc, OPT_Wpedantic,
11655 "ISO C does not support decimal floating-point "
11662 if (i == RID_FRACT)
11666 if (specs->complex_p)
11668 ("both %<complex%> and %qs in "
11669 "declaration specifiers"),
11671 else if (i == RID_FRACT)
11672 specs->typespec_word = cts_fract;
11674 specs->typespec_word = cts_accum;
11675 specs->locations[cdw_typespec] = loc;
11677 if (!targetm.fixed_point_supported_p ())
11679 "fixed-point types not supported for this target");
11680 pedwarn (loc, OPT_Wpedantic,
11681 "ISO C does not support fixed-point types");
11684 /* ObjC reserved word "id", handled below. */
11690 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
11691 form of ObjC type, cases such as "int" and "long" being handled
11692 above), a TYPE (struct, union, enum and typeof specifiers) or an
11693 ERROR_MARK. In none of these cases may there have previously
11694 been any type specifiers. */
11695 if (specs->type || specs->typespec_word != cts_none
11696 || specs->long_p || specs->short_p || specs->signed_p
11697 || specs->unsigned_p || specs->complex_p)
11698 error_at (loc, "two or more data types in declaration specifiers");
11699 else if (TREE_CODE (type) == TYPE_DECL)
11701 if (TREE_TYPE (type) == error_mark_node)
11702 ; /* Allow the type to default to int to avoid cascading errors. */
11705 specs->type = TREE_TYPE (type);
11706 specs->decl_attr = DECL_ATTRIBUTES (type);
11707 specs->typedef_p = true;
11708 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
11709 specs->locations[cdw_typedef] = loc;
11711 /* If this typedef name is defined in a struct, then a C++
11712 lookup would return a different value. */
11713 if (warn_cxx_compat
11714 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
11715 warning_at (loc, OPT_Wc___compat,
11716 "C++ lookup of %qD would return a field, not a type",
11719 /* If we are parsing a struct, record that a struct field
11721 if (warn_cxx_compat && struct_parse_info != NULL)
11722 struct_parse_info->typedefs_seen.safe_push (type);
11725 else if (TREE_CODE (type) == IDENTIFIER_NODE)
11727 tree t = lookup_name (type);
11728 if (!t || TREE_CODE (t) != TYPE_DECL)
11729 error_at (loc, "%qE fails to be a typedef or built in type", type);
11730 else if (TREE_TYPE (t) == error_mark_node)
11734 specs->type = TREE_TYPE (t);
11735 specs->locations[cdw_typespec] = loc;
11740 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
11742 specs->typedef_p = true;
11743 specs->locations[cdw_typedef] = loc;
11747 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
11748 specs->expr, spec.expr);
11750 specs->expr = spec.expr;
11751 specs->expr_const_operands &= spec.expr_const_operands;
11754 specs->type = type;
11760 /* Add the storage class specifier or function specifier SCSPEC to the
11761 declaration specifiers SPECS, returning SPECS. */
11763 struct c_declspecs *
11764 declspecs_add_scspec (location_t loc,
11765 struct c_declspecs *specs,
11769 enum c_storage_class n = csc_none;
11771 specs->declspecs_seen_p = true;
11772 specs->non_std_attrs_seen_p = true;
11773 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
11774 && C_IS_RESERVED_WORD (scspec));
11775 i = C_RID_CODE (scspec);
11776 if (specs->non_sc_seen_p)
11777 warning (OPT_Wold_style_declaration,
11778 "%qE is not at beginning of declaration", scspec);
11782 /* C99 permits duplicate inline. Although of doubtful utility,
11783 it seems simplest to permit it in gnu89 mode as well, as
11784 there is also little utility in maintaining this as a
11785 difference between gnu89 and C99 inline. */
11787 specs->inline_p = true;
11788 specs->locations[cdw_inline] = loc;
11791 /* Duplicate _Noreturn is permitted. */
11793 specs->noreturn_p = true;
11794 specs->locations[cdw_noreturn] = loc;
11797 dupe = specs->thread_p;
11798 if (specs->storage_class == csc_auto)
11799 error ("%qE used with %<auto%>", scspec);
11800 else if (specs->storage_class == csc_register)
11801 error ("%qE used with %<register%>", scspec);
11802 else if (specs->storage_class == csc_typedef)
11803 error ("%qE used with %<typedef%>", scspec);
11806 specs->thread_p = true;
11807 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
11809 /* A diagnostic is not required for the use of this
11810 identifier in the implementation namespace; only diagnose
11811 it for the C11 spelling because of existing code using
11812 the other spelling. */
11813 if (!specs->thread_gnu_p)
11816 pedwarn_c99 (loc, OPT_Wpedantic,
11817 "ISO C99 does not support %qE", scspec);
11819 pedwarn_c99 (loc, OPT_Wpedantic,
11820 "ISO C90 does not support %qE", scspec);
11822 specs->locations[cdw_thread] = loc;
11830 /* Diagnose "__thread extern". */
11831 if (specs->thread_p && specs->thread_gnu_p)
11832 error ("%<__thread%> before %<extern%>");
11839 /* Diagnose "__thread static". */
11840 if (specs->thread_p && specs->thread_gnu_p)
11841 error ("%<__thread%> before %<static%>");
11847 gcc_unreachable ();
11849 if (n != csc_none && n == specs->storage_class)
11853 if (i == RID_THREAD)
11854 error ("duplicate %<_Thread_local%> or %<__thread%>");
11856 error ("duplicate %qE", scspec);
11860 if (specs->storage_class != csc_none && n != specs->storage_class)
11862 error ("multiple storage classes in declaration specifiers");
11866 specs->storage_class = n;
11867 specs->locations[cdw_storage_class] = loc;
11868 if (n != csc_extern && n != csc_static && specs->thread_p)
11870 error ("%qs used with %qE",
11871 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11873 specs->thread_p = false;
11880 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11881 returning SPECS. */
11883 struct c_declspecs *
11884 declspecs_add_attrs (location_t loc, struct c_declspecs *specs, tree attrs)
11886 specs->attrs = chainon (attrs, specs->attrs);
11887 specs->locations[cdw_attributes] = loc;
11888 specs->declspecs_seen_p = true;
11889 /* In the case of standard attributes at the start of the
11890 declaration, the caller will reset this. */
11891 specs->non_std_attrs_seen_p = true;
11895 /* Add an _Alignas specifier (expression ALIGN, or type whose
11896 alignment is ALIGN) to the declaration specifiers SPECS, returning
11898 struct c_declspecs *
11899 declspecs_add_alignas (location_t loc,
11900 struct c_declspecs *specs, tree align)
11902 specs->alignas_p = true;
11903 specs->locations[cdw_alignas] = loc;
11904 if (align == error_mark_node)
11907 /* Only accept the alignment if it's valid and greater than
11908 the current one. Zero is invalid but by C11 required to
11909 be silently ignored. */
11910 int align_log = check_user_alignment (align, false, /* warn_zero = */false);
11911 if (align_log > specs->align_log)
11912 specs->align_log = align_log;
11916 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11917 specifiers with any other type specifier to determine the resulting
11918 type. This is where ISO C checks on complex types are made, since
11919 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11920 double". Also apply postfix standard attributes to modify the type. */
11922 struct c_declspecs *
11923 finish_declspecs (struct c_declspecs *specs)
11925 /* If a type was specified as a whole, we have no modifiers and are
11927 if (specs->type != NULL_TREE)
11929 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11930 && !specs->signed_p && !specs->unsigned_p
11931 && !specs->complex_p);
11933 /* Set a dummy type. */
11934 if (TREE_CODE (specs->type) == ERROR_MARK)
11935 specs->type = integer_type_node;
11936 goto handle_postfix_attrs;
11939 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11940 has been specified, treat it as "int" unless "_Complex" is
11941 present and there are no other specifiers. If we just have
11942 "_Complex", it is equivalent to "_Complex double", but e.g.
11943 "_Complex short" is equivalent to "_Complex short int". */
11944 if (specs->typespec_word == cts_none)
11946 if (specs->saturating_p)
11948 error_at (specs->locations[cdw_saturating],
11949 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11950 if (!targetm.fixed_point_supported_p ())
11951 error_at (specs->locations[cdw_saturating],
11952 "fixed-point types not supported for this target");
11953 specs->typespec_word = cts_fract;
11955 else if (specs->long_p || specs->short_p
11956 || specs->signed_p || specs->unsigned_p)
11958 specs->typespec_word = cts_int;
11960 else if (specs->complex_p)
11962 specs->typespec_word = cts_double;
11963 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11964 "ISO C does not support plain %<complex%> meaning "
11965 "%<double complex%>");
11969 specs->typespec_word = cts_int;
11970 specs->default_int_p = true;
11971 /* We don't diagnose this here because grokdeclarator will
11972 give more specific diagnostics according to whether it is
11973 a function definition. */
11977 /* If "signed" was specified, record this to distinguish "int" and
11978 "signed int" in the case of a bit-field with
11979 -funsigned-bitfields. */
11980 specs->explicit_signed_p = specs->signed_p;
11982 /* Now compute the actual type. */
11983 switch (specs->typespec_word)
11985 case cts_auto_type:
11986 gcc_assert (!specs->long_p && !specs->short_p
11987 && !specs->signed_p && !specs->unsigned_p
11988 && !specs->complex_p);
11989 /* Type to be filled in later. */
11990 if (specs->postfix_attrs)
11991 error ("%<__auto_type%> followed by %<[[]]%> attributes");
11994 gcc_assert (!specs->long_p && !specs->short_p
11995 && !specs->signed_p && !specs->unsigned_p
11996 && !specs->complex_p);
11997 specs->type = void_type_node;
12000 gcc_assert (!specs->long_p && !specs->short_p
12001 && !specs->signed_p && !specs->unsigned_p
12002 && !specs->complex_p);
12003 specs->type = boolean_type_node;
12006 gcc_assert (!specs->long_p && !specs->short_p);
12007 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12008 if (specs->signed_p)
12009 specs->type = signed_char_type_node;
12010 else if (specs->unsigned_p)
12011 specs->type = unsigned_char_type_node;
12013 specs->type = char_type_node;
12014 if (specs->complex_p)
12016 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12017 "ISO C does not support complex integer types");
12018 specs->type = build_complex_type (specs->type);
12022 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
12023 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12024 if (! int_n_enabled_p[specs->int_n_idx])
12025 specs->type = integer_type_node;
12027 specs->type = (specs->unsigned_p
12028 ? int_n_trees[specs->int_n_idx].unsigned_type
12029 : int_n_trees[specs->int_n_idx].signed_type);
12030 if (specs->complex_p)
12032 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12033 "ISO C does not support complex integer types");
12034 specs->type = build_complex_type (specs->type);
12038 gcc_assert (!(specs->long_p && specs->short_p));
12039 gcc_assert (!(specs->signed_p && specs->unsigned_p));
12040 if (specs->long_long_p)
12041 specs->type = (specs->unsigned_p
12042 ? long_long_unsigned_type_node
12043 : long_long_integer_type_node);
12044 else if (specs->long_p)
12045 specs->type = (specs->unsigned_p
12046 ? long_unsigned_type_node
12047 : long_integer_type_node);
12048 else if (specs->short_p)
12049 specs->type = (specs->unsigned_p
12050 ? short_unsigned_type_node
12051 : short_integer_type_node);
12053 specs->type = (specs->unsigned_p
12054 ? unsigned_type_node
12055 : integer_type_node);
12056 if (specs->complex_p)
12058 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
12059 "ISO C does not support complex integer types");
12060 specs->type = build_complex_type (specs->type);
12064 gcc_assert (!specs->long_p && !specs->short_p
12065 && !specs->signed_p && !specs->unsigned_p);
12066 specs->type = (specs->complex_p
12067 ? complex_float_type_node
12068 : float_type_node);
12071 gcc_assert (!specs->long_long_p && !specs->short_p
12072 && !specs->signed_p && !specs->unsigned_p);
12075 specs->type = (specs->complex_p
12076 ? complex_long_double_type_node
12077 : long_double_type_node);
12081 specs->type = (specs->complex_p
12082 ? complex_double_type_node
12083 : double_type_node);
12086 case cts_floatn_nx:
12087 gcc_assert (!specs->long_p && !specs->short_p
12088 && !specs->signed_p && !specs->unsigned_p);
12089 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
12090 specs->type = integer_type_node;
12091 else if (specs->complex_p)
12092 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12094 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
12098 case cts_dfloat128:
12099 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
12100 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
12101 if (!targetm.decimal_float_supported_p ())
12102 specs->type = integer_type_node;
12103 else if (specs->typespec_word == cts_dfloat32)
12104 specs->type = dfloat32_type_node;
12105 else if (specs->typespec_word == cts_dfloat64)
12106 specs->type = dfloat64_type_node;
12108 specs->type = dfloat128_type_node;
12111 gcc_assert (!specs->complex_p);
12112 if (!targetm.fixed_point_supported_p ())
12113 specs->type = integer_type_node;
12114 else if (specs->saturating_p)
12116 if (specs->long_long_p)
12117 specs->type = specs->unsigned_p
12118 ? sat_unsigned_long_long_fract_type_node
12119 : sat_long_long_fract_type_node;
12120 else if (specs->long_p)
12121 specs->type = specs->unsigned_p
12122 ? sat_unsigned_long_fract_type_node
12123 : sat_long_fract_type_node;
12124 else if (specs->short_p)
12125 specs->type = specs->unsigned_p
12126 ? sat_unsigned_short_fract_type_node
12127 : sat_short_fract_type_node;
12129 specs->type = specs->unsigned_p
12130 ? sat_unsigned_fract_type_node
12131 : sat_fract_type_node;
12135 if (specs->long_long_p)
12136 specs->type = specs->unsigned_p
12137 ? unsigned_long_long_fract_type_node
12138 : long_long_fract_type_node;
12139 else if (specs->long_p)
12140 specs->type = specs->unsigned_p
12141 ? unsigned_long_fract_type_node
12142 : long_fract_type_node;
12143 else if (specs->short_p)
12144 specs->type = specs->unsigned_p
12145 ? unsigned_short_fract_type_node
12146 : short_fract_type_node;
12148 specs->type = specs->unsigned_p
12149 ? unsigned_fract_type_node
12154 gcc_assert (!specs->complex_p);
12155 if (!targetm.fixed_point_supported_p ())
12156 specs->type = integer_type_node;
12157 else if (specs->saturating_p)
12159 if (specs->long_long_p)
12160 specs->type = specs->unsigned_p
12161 ? sat_unsigned_long_long_accum_type_node
12162 : sat_long_long_accum_type_node;
12163 else if (specs->long_p)
12164 specs->type = specs->unsigned_p
12165 ? sat_unsigned_long_accum_type_node
12166 : sat_long_accum_type_node;
12167 else if (specs->short_p)
12168 specs->type = specs->unsigned_p
12169 ? sat_unsigned_short_accum_type_node
12170 : sat_short_accum_type_node;
12172 specs->type = specs->unsigned_p
12173 ? sat_unsigned_accum_type_node
12174 : sat_accum_type_node;
12178 if (specs->long_long_p)
12179 specs->type = specs->unsigned_p
12180 ? unsigned_long_long_accum_type_node
12181 : long_long_accum_type_node;
12182 else if (specs->long_p)
12183 specs->type = specs->unsigned_p
12184 ? unsigned_long_accum_type_node
12185 : long_accum_type_node;
12186 else if (specs->short_p)
12187 specs->type = specs->unsigned_p
12188 ? unsigned_short_accum_type_node
12189 : short_accum_type_node;
12191 specs->type = specs->unsigned_p
12192 ? unsigned_accum_type_node
12197 gcc_unreachable ();
12199 handle_postfix_attrs:
12200 if (specs->type != NULL)
12202 specs->postfix_attrs = c_warn_type_attributes (specs->postfix_attrs);
12203 decl_attributes (&specs->type, specs->postfix_attrs, 0);
12204 specs->postfix_attrs = NULL_TREE;
12210 /* Perform final processing on one file scope's declarations (or the
12211 external scope's declarations), GLOBALS. */
12214 c_write_global_declarations_1 (tree globals)
12219 /* Process the decls in the order they were written. */
12220 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12222 /* Check for used but undefined static functions using the C
12223 standard's definition of "used", and set TREE_NO_WARNING so
12224 that check_global_declaration doesn't repeat the check. */
12225 if (TREE_CODE (decl) == FUNCTION_DECL
12226 && DECL_INITIAL (decl) == NULL_TREE
12227 && DECL_EXTERNAL (decl)
12228 && !TREE_PUBLIC (decl))
12230 if (C_DECL_USED (decl))
12232 /* TODO: Add OPT_Wundefined-inline. */
12233 if (pedwarn (input_location, 0, "%q+F used but never defined",
12235 suppress_warning (decl /* OPT_Wundefined-inline. */);
12237 /* For -Wunused-function warn about unused static prototypes. */
12238 else if (warn_unused_function
12239 && ! DECL_ARTIFICIAL (decl)
12240 && ! warning_suppressed_p (decl, OPT_Wunused_function))
12242 if (warning (OPT_Wunused_function,
12243 "%q+F declared %<static%> but never defined",
12245 suppress_warning (decl, OPT_Wunused_function);
12249 wrapup_global_declaration_1 (decl);
12254 reconsider = false;
12255 for (decl = globals; decl; decl = DECL_CHAIN (decl))
12256 reconsider |= wrapup_global_declaration_2 (decl);
12258 while (reconsider);
12261 /* Preserve the external declarations scope across a garbage collect. */
12262 static GTY(()) tree ext_block;
12264 /* Collect all references relevant to SOURCE_FILE. */
12267 collect_all_refs (const char *source_file)
12272 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12273 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
12275 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
12278 /* Collect source file references at global level. */
12281 collect_source_refs (void)
12288 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12290 decls = DECL_INITIAL (t);
12291 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
12292 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12293 collect_source_ref (DECL_SOURCE_FILE (decl));
12296 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
12297 if (!DECL_IS_UNDECLARED_BUILTIN (decl))
12298 collect_source_ref (DECL_SOURCE_FILE (decl));
12301 /* Free attribute access data that are not needed by the middle end. */
12304 free_attr_access_data ()
12306 struct cgraph_node *n;
12308 /* Iterate over all functions declared in the translation unit. */
12309 FOR_EACH_FUNCTION (n)
12311 for (tree parm = DECL_ARGUMENTS (n->decl); parm; parm = TREE_CHAIN (parm))
12312 if (tree attrs = DECL_ATTRIBUTES (parm))
12313 attr_access::free_lang_data (attrs);
12315 tree fntype = TREE_TYPE (n->decl);
12316 if (!fntype || fntype == error_mark_node)
12318 tree attrs = TYPE_ATTRIBUTES (fntype);
12322 attr_access::free_lang_data (attrs);
12326 /* Perform any final parser cleanups and generate initial debugging
12330 c_parse_final_cleanups (void)
12335 /* We don't want to do this if generating a PCH. */
12339 timevar_stop (TV_PHASE_PARSING);
12340 timevar_start (TV_PHASE_DEFERRED);
12342 /* Do the Objective-C stuff. This is where all the Objective-C
12343 module stuff gets generated (symtab, class/protocol/selector
12345 if (c_dialect_objc ())
12346 objc_write_global_declarations ();
12348 /* Close the external scope. */
12349 ext_block = pop_scope ();
12350 external_scope = 0;
12351 gcc_assert (!current_scope);
12353 /* Handle -fdump-ada-spec[-slim]. */
12354 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
12356 /* Build a table of files to generate specs for */
12357 collect_source_ref (main_input_filename);
12358 if (!flag_dump_ada_spec_slim)
12359 collect_source_refs ();
12361 dump_ada_specs (collect_all_refs, NULL);
12364 /* Process all file scopes in this compilation, and the external_scope,
12365 through wrapup_global_declarations. */
12366 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
12367 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
12368 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
12371 free_attr_access_data ();
12373 timevar_stop (TV_PHASE_DEFERRED);
12374 timevar_start (TV_PHASE_PARSING);
12379 /* Register reserved keyword WORD as qualifier for address space AS. */
12382 c_register_addr_space (const char *word, addr_space_t as)
12384 int rid = RID_FIRST_ADDR_SPACE + as;
12387 /* Address space qualifiers are only supported
12388 in C with GNU extensions enabled. */
12389 if (c_dialect_objc () || flag_no_asm)
12392 id = get_identifier (word);
12393 C_SET_RID_CODE (id, rid);
12394 C_IS_RESERVED_WORD (id) = 1;
12395 ridpointers [rid] = id;
12398 /* Return identifier to look up for omp declare reduction. */
12401 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
12403 const char *p = NULL;
12404 switch (reduction_code)
12406 case PLUS_EXPR: p = "+"; break;
12407 case MULT_EXPR: p = "*"; break;
12408 case MINUS_EXPR: p = "-"; break;
12409 case BIT_AND_EXPR: p = "&"; break;
12410 case BIT_XOR_EXPR: p = "^"; break;
12411 case BIT_IOR_EXPR: p = "|"; break;
12412 case TRUTH_ANDIF_EXPR: p = "&&"; break;
12413 case TRUTH_ORIF_EXPR: p = "||"; break;
12414 case MIN_EXPR: p = "min"; break;
12415 case MAX_EXPR: p = "max"; break;
12422 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
12423 return error_mark_node;
12424 p = IDENTIFIER_POINTER (reduction_id);
12427 const char prefix[] = "omp declare reduction ";
12428 size_t lenp = sizeof (prefix);
12429 size_t len = strlen (p);
12430 char *name = XALLOCAVEC (char, lenp + len);
12431 memcpy (name, prefix, lenp - 1);
12432 memcpy (name + lenp - 1, p, len + 1);
12433 return get_identifier (name);
12436 /* Lookup REDUCTION_ID in the current scope, or create an artificial
12437 VAR_DECL, bind it into the current scope and return it. */
12440 c_omp_reduction_decl (tree reduction_id)
12442 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12443 if (b != NULL && B_IN_CURRENT_SCOPE (b))
12446 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
12447 reduction_id, integer_type_node);
12448 DECL_ARTIFICIAL (decl) = 1;
12449 DECL_EXTERNAL (decl) = 1;
12450 TREE_STATIC (decl) = 1;
12451 TREE_PUBLIC (decl) = 0;
12452 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
12456 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
12459 c_omp_reduction_lookup (tree reduction_id, tree type)
12461 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
12465 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
12466 if (comptypes (TREE_PURPOSE (t), type))
12467 return TREE_VALUE (t);
12470 return error_mark_node;
12473 /* Helper function called via walk_tree, to diagnose invalid
12474 #pragma omp declare reduction combiners or initializers. */
12477 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
12479 tree *vars = (tree *) data;
12480 if (SSA_VAR_P (*tp)
12481 && !DECL_ARTIFICIAL (*tp)
12485 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
12486 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
12487 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
12488 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
12491 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
12492 "to variable %qD which is not %<omp_priv%> nor "
12502 c_check_in_current_scope (tree decl)
12504 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (decl));
12505 return b != NULL && B_IN_CURRENT_SCOPE (b);
12508 #include "gt-c-c-decl.h"