C++: simplify output from suggest_alternatives_for
[official-gcc.git] / gcc / c / c-decl.c
blobcbbf7eba51fd8c5c547a0fe9e91c53a00103b6ee
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #define INCLUDE_UNIQUE_PTR
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
62 enum decl_context
63 { NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
69 /* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
72 deprecated items. */
74 enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
91 static location_t current_function_prototype_locus;
93 /* Whether this prototype was built-in. */
95 static bool current_function_prototype_built_in;
97 /* The argument type information of this prototype. */
99 static tree current_function_prototype_arg_types;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* A list of decls to be made automatically visible in each file scope. */
120 static GTY(()) tree visible_builtins;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
145 static bool undef_nested_function;
147 /* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149 int current_omp_declare_target_attribute;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 /* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
212 struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The binding oracle; see c-tree.h. */
224 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
226 /* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228 #define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
231 static inline struct c_binding* *
232 i_symbol_binding (tree node)
234 struct lang_identifier *lid
235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
237 if (lid->symbol_binding == NULL
238 && c_binding_oracle != NULL
239 && !I_SYMBOL_CHECKED (node))
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL, node);
247 return &lid->symbol_binding;
250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
252 #define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
255 /* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257 #define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
260 static inline struct c_binding **
261 i_tag_binding (tree node)
263 struct lang_identifier *lid
264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
266 if (lid->tag_binding == NULL
267 && c_binding_oracle != NULL
268 && !I_TAG_CHECKED (node))
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node) = 1;
273 c_binding_oracle (C_ORACLE_TAG, node);
276 return &lid->tag_binding;
279 #define I_TAG_BINDING(node) (*i_tag_binding (node))
281 #define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
284 /* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286 #define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
289 static inline struct c_binding **
290 i_label_binding (tree node)
292 struct lang_identifier *lid
293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
295 if (lid->label_binding == NULL
296 && c_binding_oracle != NULL
297 && !I_LABEL_CHECKED (node))
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node) = 1;
302 c_binding_oracle (C_ORACLE_LABEL, node);
305 return &lid->label_binding;
308 #define I_LABEL_BINDING(node) (*i_label_binding (node))
310 #define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
313 /* The resulting tree type. */
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
318 union tree_node GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
320 generic;
321 struct lang_identifier GTY ((tag ("1"))) identifier;
324 /* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
330 struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
350 /* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
354 struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
361 typedef struct c_goto_bindings *c_goto_bindings_p;
363 /* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
366 struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
383 /* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
388 Most declarations are recorded in the current scope.
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
409 undefined.
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
416 this scope.
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
422 list here.)
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
428 struct GTY((chain_next ("%h.outer"))) c_scope {
429 /* The scope containing this one. */
430 struct c_scope *outer;
432 /* The next outermost function scope. */
433 struct c_scope *outer_function;
435 /* All bindings in this scope. */
436 struct c_binding *bindings;
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
440 tree blocks;
441 tree blocks_last;
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth : 28;
447 /* True if we are currently filling this scope with parameter
448 declarations. */
449 BOOL_BITFIELD parm_flag : 1;
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec : 1;
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls : 1;
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body : 1;
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep : 1;
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64 : 1;
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings : 1;
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl : 1;
485 /* The scope currently in effect. */
487 static GTY(()) struct c_scope *current_scope;
489 /* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
493 static GTY(()) struct c_scope *current_function_scope;
495 /* The C file scope. This is reset for each input translation unit. */
497 static GTY(()) struct c_scope *file_scope;
499 /* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
502 static GTY(()) struct c_scope *external_scope;
504 /* A chain of c_scope structures awaiting reuse. */
506 static GTY((deletable)) struct c_scope *scope_freelist;
508 /* A chain of c_binding structures awaiting reuse. */
510 static GTY((deletable)) struct c_binding *binding_freelist;
512 /* Append VAR to LIST in scope SCOPE. */
513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
515 tree d_ = (decl); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
518 else \
519 s_->list = d_; \
520 s_->list##_last = d_; \
521 } while (0)
523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
527 if (t_->to##_last) \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
529 else \
530 t_->to = f_->from; \
531 t_->to##_last = f_->from##_last; \
532 } while (0)
534 /* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
539 struct GTY((chain_next ("%h.next"))) c_inline_static {
540 /* The location for a diagnostic. */
541 location_t location;
543 /* The function that may be an inline definition. */
544 tree function;
546 /* The object or function referenced. */
547 tree static_decl;
549 /* What sort of reference this is. */
550 enum c_inline_static_type type;
552 /* The next such structure or NULL. */
553 struct c_inline_static *next;
556 /* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558 static GTY(()) struct c_inline_static *c_inline_statics;
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
562 static bool keep_next_level_flag;
564 /* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
568 static bool next_is_function_body;
570 /* A vector of pointers to c_binding structures. */
572 typedef struct c_binding *c_binding_ptr;
574 /* Information that we keep for a struct or union while it is being
575 parsed. */
577 struct c_struct_parse_info
579 /* If warn_cxx_compat, a list of types defined within this
580 struct. */
581 auto_vec<tree> struct_types;
582 /* If warn_cxx_compat, a list of field names which have bindings,
583 and which are defined in this struct, but which are not defined
584 in any enclosing struct. This is used to clear the in_struct
585 field of the c_bindings structure. */
586 auto_vec<c_binding_ptr> fields;
587 /* If warn_cxx_compat, a list of typedef names used when defining
588 fields in this struct. */
589 auto_vec<tree> typedefs_seen;
592 /* Information for the struct or union currently being parsed, or
593 NULL if not parsing a struct or union. */
594 static struct c_struct_parse_info *struct_parse_info;
596 /* Forward declarations. */
597 static tree lookup_name_in_scope (tree, struct c_scope *);
598 static tree c_make_fname_decl (location_t, tree, int);
599 static tree grokdeclarator (const struct c_declarator *,
600 struct c_declspecs *,
601 enum decl_context, bool, tree *, tree *, tree *,
602 bool *, enum deprecated_states);
603 static tree grokparms (struct c_arg_info *, bool);
604 static void layout_array_type (tree);
605 static void warn_defaults_to (location_t, int, const char *, ...)
606 ATTRIBUTE_GCC_DIAG(3,4);
608 /* T is a statement. Add it to the statement-tree. This is the
609 C/ObjC version--C++ has a slightly different version of this
610 function. */
612 tree
613 add_stmt (tree t)
615 enum tree_code code = TREE_CODE (t);
617 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
619 if (!EXPR_HAS_LOCATION (t))
620 SET_EXPR_LOCATION (t, input_location);
623 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
624 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
626 /* Add T to the statement-tree. Non-side-effect statements need to be
627 recorded during statement expressions. */
628 if (!building_stmt_list_p ())
629 push_stmt_list ();
630 append_to_statement_list_force (t, &cur_stmt_list);
632 return t;
635 /* Build a pointer type using the default pointer mode. */
637 static tree
638 c_build_pointer_type (tree to_type)
640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
641 : TYPE_ADDR_SPACE (to_type);
642 machine_mode pointer_mode;
644 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
645 pointer_mode = targetm.addr_space.pointer_mode (as);
646 else
647 pointer_mode = c_default_pointer_mode;
648 return build_pointer_type_for_mode (to_type, pointer_mode, false);
652 /* Return true if we will want to say something if a goto statement
653 crosses DECL. */
655 static bool
656 decl_jump_unsafe (tree decl)
658 if (error_operand_p (decl))
659 return false;
661 /* Always warn about crossing variably modified types. */
662 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
663 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
664 return true;
666 /* Otherwise, only warn if -Wgoto-misses-init and this is an
667 initialized automatic decl. */
668 if (warn_jump_misses_init
669 && VAR_P (decl)
670 && !TREE_STATIC (decl)
671 && DECL_INITIAL (decl) != NULL_TREE)
672 return true;
674 return false;
678 void
679 c_print_identifier (FILE *file, tree node, int indent)
681 void (*save) (enum c_oracle_request, tree identifier);
683 /* Temporarily hide any binding oracle. Without this, calls to
684 debug_tree from the debugger will end up calling into the oracle,
685 making for a confusing debug session. As the oracle isn't needed
686 here for normal operation, it's simplest to suppress it. */
687 save = c_binding_oracle;
688 c_binding_oracle = NULL;
690 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
691 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
692 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
693 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
695 tree rid = ridpointers[C_RID_CODE (node)];
696 indent_to (file, indent + 4);
697 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
698 (void *) rid, IDENTIFIER_POINTER (rid));
701 c_binding_oracle = save;
704 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
705 which may be any of several kinds of DECL or TYPE or error_mark_node,
706 in the scope SCOPE. */
707 static void
708 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
709 bool nested, location_t locus)
711 struct c_binding *b, **here;
713 if (binding_freelist)
715 b = binding_freelist;
716 binding_freelist = b->prev;
718 else
719 b = ggc_alloc<c_binding> ();
721 b->shadowed = 0;
722 b->decl = decl;
723 b->id = name;
724 b->depth = scope->depth;
725 b->invisible = invisible;
726 b->nested = nested;
727 b->inner_comp = 0;
728 b->in_struct = 0;
729 b->locus = locus;
731 b->u.type = NULL;
733 b->prev = scope->bindings;
734 scope->bindings = b;
736 if (decl_jump_unsafe (decl))
737 scope->has_jump_unsafe_decl = 1;
739 if (!name)
740 return;
742 switch (TREE_CODE (decl))
744 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
745 case ENUMERAL_TYPE:
746 case UNION_TYPE:
747 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
748 case VAR_DECL:
749 case FUNCTION_DECL:
750 case TYPE_DECL:
751 case CONST_DECL:
752 case PARM_DECL:
753 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
755 default:
756 gcc_unreachable ();
759 /* Locate the appropriate place in the chain of shadowed decls
760 to insert this binding. Normally, scope == current_scope and
761 this does nothing. */
762 while (*here && (*here)->depth > scope->depth)
763 here = &(*here)->shadowed;
765 b->shadowed = *here;
766 *here = b;
769 /* Clear the binding structure B, stick it on the binding_freelist,
770 and return the former value of b->prev. This is used by pop_scope
771 and get_parm_info to iterate destructively over all the bindings
772 from a given scope. */
773 static struct c_binding *
774 free_binding_and_advance (struct c_binding *b)
776 struct c_binding *prev = b->prev;
778 memset (b, 0, sizeof (struct c_binding));
779 b->prev = binding_freelist;
780 binding_freelist = b;
782 return prev;
785 /* Bind a label. Like bind, but skip fields which aren't used for
786 labels, and add the LABEL_VARS value. */
787 static void
788 bind_label (tree name, tree label, struct c_scope *scope,
789 struct c_label_vars *label_vars)
791 struct c_binding *b;
793 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
794 UNKNOWN_LOCATION);
796 scope->has_label_bindings = true;
798 b = scope->bindings;
799 gcc_assert (b->decl == label);
800 label_vars->shadowed = b->u.label;
801 b->u.label = label_vars;
804 /* Hook called at end of compilation to assume 1 elt
805 for a file-scope tentative array defn that wasn't complete before. */
807 void
808 c_finish_incomplete_decl (tree decl)
810 if (VAR_P (decl))
812 tree type = TREE_TYPE (decl);
813 if (type != error_mark_node
814 && TREE_CODE (type) == ARRAY_TYPE
815 && !DECL_EXTERNAL (decl)
816 && TYPE_DOMAIN (type) == NULL_TREE)
818 warning_at (DECL_SOURCE_LOCATION (decl),
819 0, "array %q+D assumed to have one element", decl);
821 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
823 relayout_decl (decl);
828 /* Record that inline function FUNC contains a reference (location
829 LOC) to static DECL (file-scope or function-local according to
830 TYPE). */
832 void
833 record_inline_static (location_t loc, tree func, tree decl,
834 enum c_inline_static_type type)
836 c_inline_static *csi = ggc_alloc<c_inline_static> ();
837 csi->location = loc;
838 csi->function = func;
839 csi->static_decl = decl;
840 csi->type = type;
841 csi->next = c_inline_statics;
842 c_inline_statics = csi;
845 /* Check for references to static declarations in inline functions at
846 the end of the translation unit and diagnose them if the functions
847 are still inline definitions. */
849 static void
850 check_inline_statics (void)
852 struct c_inline_static *csi;
853 for (csi = c_inline_statics; csi; csi = csi->next)
855 if (DECL_EXTERNAL (csi->function))
856 switch (csi->type)
858 case csi_internal:
859 pedwarn (csi->location, 0,
860 "%qD is static but used in inline function %qD "
861 "which is not static", csi->static_decl, csi->function);
862 break;
863 case csi_modifiable:
864 pedwarn (csi->location, 0,
865 "%q+D is static but declared in inline function %qD "
866 "which is not static", csi->static_decl, csi->function);
867 break;
868 default:
869 gcc_unreachable ();
872 c_inline_statics = NULL;
875 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
876 for the current state, otherwise set it to uninitialized. */
878 static void
879 set_spot_bindings (struct c_spot_bindings *p, bool defining)
881 if (defining)
883 p->scope = current_scope;
884 p->bindings_in_scope = current_scope->bindings;
886 else
888 p->scope = NULL;
889 p->bindings_in_scope = NULL;
891 p->stmt_exprs = 0;
892 p->left_stmt_expr = false;
895 /* Update spot bindings P as we pop out of SCOPE. Return true if we
896 should push decls for a label. */
898 static bool
899 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
901 if (p->scope != scope)
903 /* This label or goto is defined in some other scope, or it is a
904 label which is not yet defined. There is nothing to
905 update. */
906 return false;
909 /* Adjust the spot bindings to refer to the bindings already defined
910 in the enclosing scope. */
911 p->scope = scope->outer;
912 p->bindings_in_scope = p->scope->bindings;
914 return true;
917 /* The Objective-C front-end often needs to determine the current scope. */
919 void *
920 objc_get_current_scope (void)
922 return current_scope;
925 /* The following function is used only by Objective-C. It needs to live here
926 because it accesses the innards of c_scope. */
928 void
929 objc_mark_locals_volatile (void *enclosing_blk)
931 struct c_scope *scope;
932 struct c_binding *b;
934 for (scope = current_scope;
935 scope && scope != enclosing_blk;
936 scope = scope->outer)
938 for (b = scope->bindings; b; b = b->prev)
939 objc_volatilize_decl (b->decl);
941 /* Do not climb up past the current function. */
942 if (scope->function_body)
943 break;
947 /* Return true if we are in the global binding level. */
949 bool
950 global_bindings_p (void)
952 return current_scope == file_scope;
955 /* Return true if we're declaring parameters in an old-style function
956 declaration. */
958 bool
959 old_style_parameter_scope (void)
961 /* If processing parameters and there is no function statement list, we
962 * have an old-style function declaration. */
963 return (current_scope->parm_flag && !DECL_SAVED_TREE (current_function_decl));
966 void
967 keep_next_level (void)
969 keep_next_level_flag = true;
972 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
974 void
975 set_float_const_decimal64 (void)
977 current_scope->float_const_decimal64 = true;
980 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
982 void
983 clear_float_const_decimal64 (void)
985 current_scope->float_const_decimal64 = false;
988 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
990 bool
991 float_const_decimal64_p (void)
993 return current_scope->float_const_decimal64;
996 /* Identify this scope as currently being filled with parameters. */
998 void
999 declare_parm_level (void)
1001 current_scope->parm_flag = true;
1004 void
1005 push_scope (void)
1007 if (next_is_function_body)
1009 /* This is the transition from the parameters to the top level
1010 of the function body. These are the same scope
1011 (C99 6.2.1p4,6) so we do not push another scope structure.
1012 next_is_function_body is set only by store_parm_decls, which
1013 in turn is called when and only when we are about to
1014 encounter the opening curly brace for the function body.
1016 The outermost block of a function always gets a BLOCK node,
1017 because the debugging output routines expect that each
1018 function has at least one BLOCK. */
1019 current_scope->parm_flag = false;
1020 current_scope->function_body = true;
1021 current_scope->keep = true;
1022 current_scope->outer_function = current_function_scope;
1023 current_function_scope = current_scope;
1025 keep_next_level_flag = false;
1026 next_is_function_body = false;
1028 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1029 if (current_scope->outer)
1030 current_scope->float_const_decimal64
1031 = current_scope->outer->float_const_decimal64;
1032 else
1033 current_scope->float_const_decimal64 = false;
1035 else
1037 struct c_scope *scope;
1038 if (scope_freelist)
1040 scope = scope_freelist;
1041 scope_freelist = scope->outer;
1043 else
1044 scope = ggc_cleared_alloc<c_scope> ();
1046 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1047 if (current_scope)
1048 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1049 else
1050 scope->float_const_decimal64 = false;
1052 scope->keep = keep_next_level_flag;
1053 scope->outer = current_scope;
1054 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1056 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1057 possible. */
1058 if (current_scope && scope->depth == 0)
1060 scope->depth--;
1061 sorry ("GCC supports only %u nested scopes", scope->depth);
1064 current_scope = scope;
1065 keep_next_level_flag = false;
1069 /* This is called when we are leaving SCOPE. For each label defined
1070 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1071 These are the decls whose initialization will be skipped by a goto
1072 later in the function. */
1074 static void
1075 update_label_decls (struct c_scope *scope)
1077 struct c_scope *s;
1079 s = scope;
1080 while (s != NULL)
1082 if (s->has_label_bindings)
1084 struct c_binding *b;
1086 for (b = s->bindings; b != NULL; b = b->prev)
1088 struct c_label_vars *label_vars;
1089 struct c_binding *b1;
1090 bool hjud;
1091 unsigned int ix;
1092 struct c_goto_bindings *g;
1094 if (TREE_CODE (b->decl) != LABEL_DECL)
1095 continue;
1096 label_vars = b->u.label;
1098 b1 = label_vars->label_bindings.bindings_in_scope;
1099 if (label_vars->label_bindings.scope == NULL)
1100 hjud = false;
1101 else
1102 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1103 if (update_spot_bindings (scope, &label_vars->label_bindings))
1105 /* This label is defined in this scope. */
1106 if (hjud)
1108 for (; b1 != NULL; b1 = b1->prev)
1110 /* A goto from later in the function to this
1111 label will never see the initialization
1112 of B1, if any. Save it to issue a
1113 warning if needed. */
1114 if (decl_jump_unsafe (b1->decl))
1115 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1120 /* Update the bindings of any goto statements associated
1121 with this label. */
1122 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1123 update_spot_bindings (scope, &g->goto_bindings);
1127 /* Don't search beyond the current function. */
1128 if (s == current_function_scope)
1129 break;
1131 s = s->outer;
1135 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1137 static void
1138 set_type_context (tree type, tree context)
1140 for (type = TYPE_MAIN_VARIANT (type); type;
1141 type = TYPE_NEXT_VARIANT (type))
1142 TYPE_CONTEXT (type) = context;
1145 /* Exit a scope. Restore the state of the identifier-decl mappings
1146 that were in effect when this scope was entered. Return a BLOCK
1147 node containing all the DECLs in this scope that are of interest
1148 to debug info generation. */
1150 tree
1151 pop_scope (void)
1153 struct c_scope *scope = current_scope;
1154 tree block, context, p;
1155 struct c_binding *b;
1157 bool functionbody = scope->function_body;
1158 bool keep = functionbody || scope->keep || scope->bindings;
1160 update_label_decls (scope);
1162 /* If appropriate, create a BLOCK to record the decls for the life
1163 of this function. */
1164 block = NULL_TREE;
1165 if (keep)
1167 block = make_node (BLOCK);
1168 BLOCK_SUBBLOCKS (block) = scope->blocks;
1169 TREE_USED (block) = 1;
1171 /* In each subblock, record that this is its superior. */
1172 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1173 BLOCK_SUPERCONTEXT (p) = block;
1175 BLOCK_VARS (block) = NULL_TREE;
1178 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1179 scope must be set so that they point to the appropriate
1180 construct, i.e. either to the current FUNCTION_DECL node, or
1181 else to the BLOCK node we just constructed.
1183 Note that for tagged types whose scope is just the formal
1184 parameter list for some function type specification, we can't
1185 properly set their TYPE_CONTEXTs here, because we don't have a
1186 pointer to the appropriate FUNCTION_TYPE node readily available
1187 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1188 type nodes get set in `grokdeclarator' as soon as we have created
1189 the FUNCTION_TYPE node which will represent the "scope" for these
1190 "parameter list local" tagged types. */
1191 if (scope->function_body)
1192 context = current_function_decl;
1193 else if (scope == file_scope)
1195 tree file_decl
1196 = build_translation_unit_decl (get_identifier (main_input_filename));
1197 context = file_decl;
1198 debug_hooks->register_main_translation_unit (file_decl);
1200 else
1201 context = block;
1203 /* Clear all bindings in this scope. */
1204 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1206 p = b->decl;
1207 switch (TREE_CODE (p))
1209 case LABEL_DECL:
1210 /* Warnings for unused labels, errors for undefined labels. */
1211 if (TREE_USED (p) && !DECL_INITIAL (p))
1213 error ("label %q+D used but not defined", p);
1214 DECL_INITIAL (p) = error_mark_node;
1216 else
1217 warn_for_unused_label (p);
1219 /* Labels go in BLOCK_VARS. */
1220 DECL_CHAIN (p) = BLOCK_VARS (block);
1221 BLOCK_VARS (block) = p;
1222 gcc_assert (I_LABEL_BINDING (b->id) == b);
1223 I_LABEL_BINDING (b->id) = b->shadowed;
1225 /* Also pop back to the shadowed label_vars. */
1226 release_tree_vector (b->u.label->decls_in_scope);
1227 b->u.label = b->u.label->shadowed;
1228 break;
1230 case ENUMERAL_TYPE:
1231 case UNION_TYPE:
1232 case RECORD_TYPE:
1233 set_type_context (p, context);
1235 /* Types may not have tag-names, in which case the type
1236 appears in the bindings list with b->id NULL. */
1237 if (b->id)
1239 gcc_assert (I_TAG_BINDING (b->id) == b);
1240 I_TAG_BINDING (b->id) = b->shadowed;
1242 break;
1244 case FUNCTION_DECL:
1245 /* Propagate TREE_ADDRESSABLE from nested functions to their
1246 containing functions. */
1247 if (!TREE_ASM_WRITTEN (p)
1248 && DECL_INITIAL (p) != NULL_TREE
1249 && TREE_ADDRESSABLE (p)
1250 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1251 && DECL_ABSTRACT_ORIGIN (p) != p)
1252 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1253 if (!DECL_EXTERNAL (p)
1254 && !DECL_INITIAL (p)
1255 && scope != file_scope
1256 && scope != external_scope)
1258 error ("nested function %q+D declared but never defined", p);
1259 undef_nested_function = true;
1261 else if (DECL_DECLARED_INLINE_P (p)
1262 && TREE_PUBLIC (p)
1263 && !DECL_INITIAL (p))
1265 /* C99 6.7.4p6: "a function with external linkage... declared
1266 with an inline function specifier ... shall also be defined
1267 in the same translation unit." */
1268 if (!flag_gnu89_inline
1269 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1270 && scope != external_scope)
1271 pedwarn (input_location, 0,
1272 "inline function %q+D declared but never defined", p);
1273 DECL_EXTERNAL (p) = 1;
1276 goto common_symbol;
1278 case VAR_DECL:
1279 /* Warnings for unused variables. */
1280 if ((!TREE_USED (p) || !DECL_READ_P (p))
1281 && !TREE_NO_WARNING (p)
1282 && !DECL_IN_SYSTEM_HEADER (p)
1283 && DECL_NAME (p)
1284 && !DECL_ARTIFICIAL (p)
1285 && scope != file_scope
1286 && scope != external_scope)
1288 if (!TREE_USED (p))
1289 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1290 else if (DECL_CONTEXT (p) == current_function_decl)
1291 warning_at (DECL_SOURCE_LOCATION (p),
1292 OPT_Wunused_but_set_variable,
1293 "variable %qD set but not used", p);
1296 if (b->inner_comp)
1298 error ("type of array %q+D completed incompatibly with"
1299 " implicit initialization", p);
1302 /* Fall through. */
1303 case TYPE_DECL:
1304 case CONST_DECL:
1305 common_symbol:
1306 /* All of these go in BLOCK_VARS, but only if this is the
1307 binding in the home scope. */
1308 if (!b->nested)
1310 DECL_CHAIN (p) = BLOCK_VARS (block);
1311 BLOCK_VARS (block) = p;
1313 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1315 /* For block local externs add a special
1316 DECL_EXTERNAL decl for debug info generation. */
1317 tree extp = copy_node (p);
1319 DECL_EXTERNAL (extp) = 1;
1320 TREE_STATIC (extp) = 0;
1321 TREE_PUBLIC (extp) = 1;
1322 DECL_INITIAL (extp) = NULL_TREE;
1323 DECL_LANG_SPECIFIC (extp) = NULL;
1324 DECL_CONTEXT (extp) = current_function_decl;
1325 if (TREE_CODE (p) == FUNCTION_DECL)
1327 DECL_RESULT (extp) = NULL_TREE;
1328 DECL_SAVED_TREE (extp) = NULL_TREE;
1329 DECL_STRUCT_FUNCTION (extp) = NULL;
1331 if (b->locus != UNKNOWN_LOCATION)
1332 DECL_SOURCE_LOCATION (extp) = b->locus;
1333 DECL_CHAIN (extp) = BLOCK_VARS (block);
1334 BLOCK_VARS (block) = extp;
1336 /* If this is the file scope set DECL_CONTEXT of each decl to
1337 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1338 work. */
1339 if (scope == file_scope)
1341 DECL_CONTEXT (p) = context;
1342 if (TREE_CODE (p) == TYPE_DECL
1343 && TREE_TYPE (p) != error_mark_node)
1344 set_type_context (TREE_TYPE (p), context);
1347 gcc_fallthrough ();
1348 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1349 already been put there by store_parm_decls. Unused-
1350 parameter warnings are handled by function.c.
1351 error_mark_node obviously does not go in BLOCK_VARS and
1352 does not get unused-variable warnings. */
1353 case PARM_DECL:
1354 case ERROR_MARK:
1355 /* It is possible for a decl not to have a name. We get
1356 here with b->id NULL in this case. */
1357 if (b->id)
1359 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1360 I_SYMBOL_BINDING (b->id) = b->shadowed;
1361 if (b->shadowed && b->shadowed->u.type)
1362 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1364 break;
1366 default:
1367 gcc_unreachable ();
1372 /* Dispose of the block that we just made inside some higher level. */
1373 if ((scope->function_body || scope == file_scope) && context)
1375 DECL_INITIAL (context) = block;
1376 BLOCK_SUPERCONTEXT (block) = context;
1378 else if (scope->outer)
1380 if (block)
1381 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1382 /* If we did not make a block for the scope just exited, any
1383 blocks made for inner scopes must be carried forward so they
1384 will later become subblocks of something else. */
1385 else if (scope->blocks)
1386 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1389 /* Pop the current scope, and free the structure for reuse. */
1390 current_scope = scope->outer;
1391 if (scope->function_body)
1392 current_function_scope = scope->outer_function;
1394 memset (scope, 0, sizeof (struct c_scope));
1395 scope->outer = scope_freelist;
1396 scope_freelist = scope;
1398 return block;
1401 void
1402 push_file_scope (void)
1404 tree decl;
1406 if (file_scope)
1407 return;
1409 push_scope ();
1410 file_scope = current_scope;
1412 start_fname_decls ();
1414 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1415 bind (DECL_NAME (decl), decl, file_scope,
1416 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1419 void
1420 pop_file_scope (void)
1422 /* In case there were missing closebraces, get us back to the global
1423 binding level. */
1424 while (current_scope != file_scope)
1425 pop_scope ();
1427 /* __FUNCTION__ is defined at file scope (""). This
1428 call may not be necessary as my tests indicate it
1429 still works without it. */
1430 finish_fname_decls ();
1432 check_inline_statics ();
1434 /* This is the point to write out a PCH if we're doing that.
1435 In that case we do not want to do anything else. */
1436 if (pch_file)
1438 c_common_write_pch ();
1439 /* Ensure even the callers don't try to finalize the CU. */
1440 flag_syntax_only = 1;
1441 return;
1444 /* Pop off the file scope and close this translation unit. */
1445 pop_scope ();
1446 file_scope = 0;
1448 maybe_apply_pending_pragma_weaks ();
1451 /* Adjust the bindings for the start of a statement expression. */
1453 void
1454 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1456 struct c_scope *scope;
1458 for (scope = current_scope; scope != NULL; scope = scope->outer)
1460 struct c_binding *b;
1462 if (!scope->has_label_bindings)
1463 continue;
1465 for (b = scope->bindings; b != NULL; b = b->prev)
1467 struct c_label_vars *label_vars;
1468 unsigned int ix;
1469 struct c_goto_bindings *g;
1471 if (TREE_CODE (b->decl) != LABEL_DECL)
1472 continue;
1473 label_vars = b->u.label;
1474 ++label_vars->label_bindings.stmt_exprs;
1475 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1476 ++g->goto_bindings.stmt_exprs;
1480 if (switch_bindings != NULL)
1481 ++switch_bindings->stmt_exprs;
1484 /* Adjust the bindings for the end of a statement expression. */
1486 void
1487 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1489 struct c_scope *scope;
1491 for (scope = current_scope; scope != NULL; scope = scope->outer)
1493 struct c_binding *b;
1495 if (!scope->has_label_bindings)
1496 continue;
1498 for (b = scope->bindings; b != NULL; b = b->prev)
1500 struct c_label_vars *label_vars;
1501 unsigned int ix;
1502 struct c_goto_bindings *g;
1504 if (TREE_CODE (b->decl) != LABEL_DECL)
1505 continue;
1506 label_vars = b->u.label;
1507 --label_vars->label_bindings.stmt_exprs;
1508 if (label_vars->label_bindings.stmt_exprs < 0)
1510 label_vars->label_bindings.left_stmt_expr = true;
1511 label_vars->label_bindings.stmt_exprs = 0;
1513 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1515 --g->goto_bindings.stmt_exprs;
1516 if (g->goto_bindings.stmt_exprs < 0)
1518 g->goto_bindings.left_stmt_expr = true;
1519 g->goto_bindings.stmt_exprs = 0;
1525 if (switch_bindings != NULL)
1527 --switch_bindings->stmt_exprs;
1528 gcc_assert (switch_bindings->stmt_exprs >= 0);
1532 /* Push a definition or a declaration of struct, union or enum tag "name".
1533 "type" should be the type node.
1534 We assume that the tag "name" is not already defined, and has a location
1535 of LOC.
1537 Note that the definition may really be just a forward reference.
1538 In that case, the TYPE_SIZE will be zero. */
1540 static void
1541 pushtag (location_t loc, tree name, tree type)
1543 /* Record the identifier as the type's name if it has none. */
1544 if (name && !TYPE_NAME (type))
1545 TYPE_NAME (type) = name;
1546 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1548 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1549 tagged type we just added to the current scope. This fake
1550 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1551 to output a representation of a tagged type, and it also gives
1552 us a convenient place to record the "scope start" address for the
1553 tagged type. */
1555 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1556 TYPE_DECL, NULL_TREE, type));
1558 /* An approximation for now, so we can tell this is a function-scope tag.
1559 This will be updated in pop_scope. */
1560 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1562 if (warn_cxx_compat && name != NULL_TREE)
1564 struct c_binding *b = I_SYMBOL_BINDING (name);
1566 if (b != NULL
1567 && b->decl != NULL_TREE
1568 && TREE_CODE (b->decl) == TYPE_DECL
1569 && (B_IN_CURRENT_SCOPE (b)
1570 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1571 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1572 != TYPE_MAIN_VARIANT (type)))
1574 auto_diagnostic_group d;
1575 if (warning_at (loc, OPT_Wc___compat,
1576 ("using %qD as both a typedef and a tag is "
1577 "invalid in C++"), b->decl)
1578 && b->locus != UNKNOWN_LOCATION)
1579 inform (b->locus, "originally defined here");
1584 /* An exported interface to pushtag. This is used by the gdb plugin's
1585 binding oracle to introduce a new tag binding. */
1587 void
1588 c_pushtag (location_t loc, tree name, tree type)
1590 pushtag (loc, name, type);
1593 /* An exported interface to bind a declaration. LOC is the location
1594 to use. DECL is the declaration to bind. The decl's name is used
1595 to determine how it is bound. If DECL is a VAR_DECL, then
1596 IS_GLOBAL determines whether the decl is put into the global (file
1597 and external) scope or the current function's scope; if DECL is not
1598 a VAR_DECL then it is always put into the file scope. */
1600 void
1601 c_bind (location_t loc, tree decl, bool is_global)
1603 struct c_scope *scope;
1604 bool nested = false;
1606 if (!VAR_P (decl) || current_function_scope == NULL)
1608 /* Types and functions are always considered to be global. */
1609 scope = file_scope;
1610 DECL_EXTERNAL (decl) = 1;
1611 TREE_PUBLIC (decl) = 1;
1613 else if (is_global)
1615 /* Also bind it into the external scope. */
1616 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1617 nested = true;
1618 scope = file_scope;
1619 DECL_EXTERNAL (decl) = 1;
1620 TREE_PUBLIC (decl) = 1;
1622 else
1624 DECL_CONTEXT (decl) = current_function_decl;
1625 TREE_PUBLIC (decl) = 0;
1626 scope = current_function_scope;
1629 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1632 /* Subroutine of compare_decls. Allow harmless mismatches in return
1633 and argument types provided that the type modes match. This function
1634 return a unified type given a suitable match, and 0 otherwise. */
1636 static tree
1637 match_builtin_function_types (tree newtype, tree oldtype)
1639 tree newrettype, oldrettype;
1640 tree newargs, oldargs;
1641 tree trytype, tryargs;
1643 /* Accept the return type of the new declaration if same modes. */
1644 oldrettype = TREE_TYPE (oldtype);
1645 newrettype = TREE_TYPE (newtype);
1647 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1648 return NULL_TREE;
1650 oldargs = TYPE_ARG_TYPES (oldtype);
1651 newargs = TYPE_ARG_TYPES (newtype);
1652 tryargs = newargs;
1654 while (oldargs || newargs)
1656 if (!oldargs
1657 || !newargs
1658 || !TREE_VALUE (oldargs)
1659 || !TREE_VALUE (newargs)
1660 || TYPE_MODE (TREE_VALUE (oldargs))
1661 != TYPE_MODE (TREE_VALUE (newargs)))
1662 return NULL_TREE;
1664 oldargs = TREE_CHAIN (oldargs);
1665 newargs = TREE_CHAIN (newargs);
1668 trytype = build_function_type (newrettype, tryargs);
1670 /* Allow declaration to change transaction_safe attribute. */
1671 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1672 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1673 tree newattrs = TYPE_ATTRIBUTES (newtype);
1674 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1675 if (oldtsafe && !newtsafe)
1676 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1677 else if (newtsafe && !oldtsafe)
1678 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1679 NULL_TREE, oldattrs);
1681 return build_type_attribute_variant (trytype, oldattrs);
1684 /* Subroutine of diagnose_mismatched_decls. Check for function type
1685 mismatch involving an empty arglist vs a nonempty one and give clearer
1686 diagnostics. */
1687 static void
1688 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1689 tree newtype, tree oldtype)
1691 tree t;
1693 if (TREE_CODE (olddecl) != FUNCTION_DECL
1694 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1695 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1696 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1697 return;
1699 t = TYPE_ARG_TYPES (oldtype);
1700 if (t == NULL_TREE)
1701 t = TYPE_ARG_TYPES (newtype);
1702 for (; t; t = TREE_CHAIN (t))
1704 tree type = TREE_VALUE (t);
1706 if (TREE_CHAIN (t) == NULL_TREE
1707 && TYPE_MAIN_VARIANT (type) != void_type_node)
1709 inform (input_location, "a parameter list with an ellipsis can%'t match "
1710 "an empty parameter name list declaration");
1711 break;
1714 if (c_type_promotes_to (type) != type)
1716 inform (input_location, "an argument type that has a default promotion can%'t match "
1717 "an empty parameter name list declaration");
1718 break;
1723 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1724 old-style function definition, NEWDECL is a prototype declaration.
1725 Diagnose inconsistencies in the argument list. Returns TRUE if
1726 the prototype is compatible, FALSE if not. */
1727 static bool
1728 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1730 tree newargs, oldargs;
1731 int i;
1733 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1735 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1736 newargs = TYPE_ARG_TYPES (newtype);
1737 i = 1;
1739 for (;;)
1741 tree oldargtype = TREE_VALUE (oldargs);
1742 tree newargtype = TREE_VALUE (newargs);
1744 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1745 return false;
1747 oldargtype = (TYPE_ATOMIC (oldargtype)
1748 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1749 TYPE_QUAL_ATOMIC)
1750 : TYPE_MAIN_VARIANT (oldargtype));
1751 newargtype = (TYPE_ATOMIC (newargtype)
1752 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1753 TYPE_QUAL_ATOMIC)
1754 : TYPE_MAIN_VARIANT (newargtype));
1756 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1757 break;
1759 /* Reaching the end of just one list means the two decls don't
1760 agree on the number of arguments. */
1761 if (END_OF_ARGLIST (oldargtype))
1763 error ("prototype for %q+D declares more arguments "
1764 "than previous old-style definition", newdecl);
1765 return false;
1767 else if (END_OF_ARGLIST (newargtype))
1769 error ("prototype for %q+D declares fewer arguments "
1770 "than previous old-style definition", newdecl);
1771 return false;
1774 /* Type for passing arg must be consistent with that declared
1775 for the arg. */
1776 else if (!comptypes (oldargtype, newargtype))
1778 error ("prototype for %q+D declares argument %d"
1779 " with incompatible type",
1780 newdecl, i);
1781 return false;
1784 oldargs = TREE_CHAIN (oldargs);
1785 newargs = TREE_CHAIN (newargs);
1786 i++;
1789 /* If we get here, no errors were found, but do issue a warning
1790 for this poor-style construct. */
1791 warning (0, "prototype for %q+D follows non-prototype definition",
1792 newdecl);
1793 return true;
1794 #undef END_OF_ARGLIST
1797 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1798 first in a pair of mismatched declarations, using the diagnostic
1799 function DIAG. */
1800 static void
1801 locate_old_decl (tree decl)
1803 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl)
1804 && !C_DECL_DECLARED_BUILTIN (decl))
1806 else if (DECL_INITIAL (decl))
1807 inform (input_location, "previous definition of %q+D was here", decl);
1808 else if (C_DECL_IMPLICIT (decl))
1809 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1810 else
1811 inform (input_location, "previous declaration of %q+D was here", decl);
1814 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1815 Returns true if the caller should proceed to merge the two, false
1816 if OLDDECL should simply be discarded. As a side effect, issues
1817 all necessary diagnostics for invalid or poor-style combinations.
1818 If it returns true, writes the types of NEWDECL and OLDDECL to
1819 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1820 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1822 static bool
1823 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1824 tree *newtypep, tree *oldtypep)
1826 tree newtype, oldtype;
1827 bool retval = true;
1829 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1830 && DECL_EXTERNAL (DECL))
1832 /* If we have error_mark_node for either decl or type, just discard
1833 the previous decl - we're in an error cascade already. */
1834 if (olddecl == error_mark_node || newdecl == error_mark_node)
1835 return false;
1836 *oldtypep = oldtype = TREE_TYPE (olddecl);
1837 *newtypep = newtype = TREE_TYPE (newdecl);
1838 if (oldtype == error_mark_node || newtype == error_mark_node)
1839 return false;
1841 /* Two different categories of symbol altogether. This is an error
1842 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1843 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1845 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1846 && fndecl_built_in_p (olddecl)
1847 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1849 auto_diagnostic_group d;
1850 error ("%q+D redeclared as different kind of symbol", newdecl);
1851 locate_old_decl (olddecl);
1853 else if (TREE_PUBLIC (newdecl))
1854 warning (OPT_Wbuiltin_declaration_mismatch,
1855 "built-in function %q+D declared as non-function",
1856 newdecl);
1857 else
1858 warning (OPT_Wshadow, "declaration of %q+D shadows "
1859 "a built-in function", newdecl);
1860 return false;
1863 /* Enumerators have no linkage, so may only be declared once in a
1864 given scope. */
1865 if (TREE_CODE (olddecl) == CONST_DECL)
1867 auto_diagnostic_group d;
1868 error ("redeclaration of enumerator %q+D", newdecl);
1869 locate_old_decl (olddecl);
1870 return false;
1873 bool pedwarned = false;
1874 bool warned = false;
1875 auto_diagnostic_group d;
1877 if (!comptypes (oldtype, newtype))
1879 if (TREE_CODE (olddecl) == FUNCTION_DECL
1880 && fndecl_built_in_p (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1882 /* Accept harmless mismatch in function types.
1883 This is for the ffs and fprintf builtins. */
1884 tree trytype = match_builtin_function_types (newtype, oldtype);
1886 if (trytype && comptypes (newtype, trytype))
1887 *oldtypep = oldtype = trytype;
1888 else
1890 /* If types don't match for a built-in, throw away the
1891 built-in. No point in calling locate_old_decl here, it
1892 won't print anything. */
1893 warning (OPT_Wbuiltin_declaration_mismatch,
1894 "conflicting types for built-in function %q+D",
1895 newdecl);
1896 return false;
1899 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1900 && DECL_IS_BUILTIN (olddecl))
1902 /* A conflicting function declaration for a predeclared
1903 function that isn't actually built in. Objective C uses
1904 these. The new declaration silently overrides everything
1905 but the volatility (i.e. noreturn) indication. See also
1906 below. FIXME: Make Objective C use normal builtins. */
1907 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1908 return false;
1910 /* Permit void foo (...) to match int foo (...) if the latter is
1911 the definition and implicit int was used. See
1912 c-torture/compile/920625-2.c. */
1913 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1914 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1915 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1916 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1918 pedwarned = pedwarn (input_location, 0,
1919 "conflicting types for %q+D", newdecl);
1920 /* Make sure we keep void as the return type. */
1921 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1922 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1924 /* Permit void foo (...) to match an earlier call to foo (...) with
1925 no declared type (thus, implicitly int). */
1926 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1927 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1928 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1929 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1931 pedwarned = pedwarn (input_location, 0,
1932 "conflicting types for %q+D", newdecl);
1933 /* Make sure we keep void as the return type. */
1934 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1936 else
1938 int new_quals = TYPE_QUALS (newtype);
1939 int old_quals = TYPE_QUALS (oldtype);
1941 if (new_quals != old_quals)
1943 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1944 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1945 if (new_addr != old_addr)
1947 if (ADDR_SPACE_GENERIC_P (new_addr))
1948 error ("conflicting named address spaces (generic vs %s) "
1949 "for %q+D",
1950 c_addr_space_name (old_addr), newdecl);
1951 else if (ADDR_SPACE_GENERIC_P (old_addr))
1952 error ("conflicting named address spaces (%s vs generic) "
1953 "for %q+D",
1954 c_addr_space_name (new_addr), newdecl);
1955 else
1956 error ("conflicting named address spaces (%s vs %s) "
1957 "for %q+D",
1958 c_addr_space_name (new_addr),
1959 c_addr_space_name (old_addr),
1960 newdecl);
1963 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1964 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1965 error ("conflicting type qualifiers for %q+D", newdecl);
1967 else
1968 error ("conflicting types for %q+D", newdecl);
1969 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1970 locate_old_decl (olddecl);
1971 return false;
1975 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1976 but silently ignore the redeclaration if either is in a system
1977 header. (Conflicting redeclarations were handled above.) This
1978 is allowed for C11 if the types are the same, not just
1979 compatible. */
1980 if (TREE_CODE (newdecl) == TYPE_DECL)
1982 bool types_different = false;
1983 int comptypes_result;
1985 comptypes_result
1986 = comptypes_check_different_types (oldtype, newtype, &types_different);
1988 if (comptypes_result != 1 || types_different)
1990 error ("redefinition of typedef %q+D with different type", newdecl);
1991 locate_old_decl (olddecl);
1992 return false;
1995 if (DECL_IN_SYSTEM_HEADER (newdecl)
1996 || DECL_IN_SYSTEM_HEADER (olddecl)
1997 || TREE_NO_WARNING (newdecl)
1998 || TREE_NO_WARNING (olddecl))
1999 return true; /* Allow OLDDECL to continue in use. */
2001 if (variably_modified_type_p (newtype, NULL))
2003 error ("redefinition of typedef %q+D with variably modified type",
2004 newdecl);
2005 locate_old_decl (olddecl);
2007 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2008 "redefinition of typedef %q+D", newdecl))
2009 locate_old_decl (olddecl);
2011 return true;
2014 /* Function declarations can either be 'static' or 'extern' (no
2015 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2016 can never conflict with each other on account of linkage
2017 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2018 gnu89 mode permits two definitions if one is 'extern inline' and
2019 one is not. The non- extern-inline definition supersedes the
2020 extern-inline definition. */
2022 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2024 /* If you declare a built-in function name as static, or
2025 define the built-in with an old-style definition (so we
2026 can't validate the argument list) the built-in definition is
2027 overridden, but optionally warn this was a bad choice of name. */
2028 if (fndecl_built_in_p (olddecl)
2029 && !C_DECL_DECLARED_BUILTIN (olddecl)
2030 && (!TREE_PUBLIC (newdecl)
2031 || (DECL_INITIAL (newdecl)
2032 && !prototype_p (TREE_TYPE (newdecl)))))
2034 warning (OPT_Wshadow, "declaration of %q+D shadows "
2035 "a built-in function", newdecl);
2036 /* Discard the old built-in function. */
2037 return false;
2040 if (DECL_INITIAL (newdecl))
2042 if (DECL_INITIAL (olddecl))
2044 /* If both decls are in the same TU and the new declaration
2045 isn't overriding an extern inline reject the new decl.
2046 In c99, no overriding is allowed in the same translation
2047 unit. */
2048 if ((!DECL_EXTERN_INLINE (olddecl)
2049 || DECL_EXTERN_INLINE (newdecl)
2050 || (!flag_gnu89_inline
2051 && (!DECL_DECLARED_INLINE_P (olddecl)
2052 || !lookup_attribute ("gnu_inline",
2053 DECL_ATTRIBUTES (olddecl)))
2054 && (!DECL_DECLARED_INLINE_P (newdecl)
2055 || !lookup_attribute ("gnu_inline",
2056 DECL_ATTRIBUTES (newdecl))))
2058 && same_translation_unit_p (newdecl, olddecl))
2060 auto_diagnostic_group d;
2061 error ("redefinition of %q+D", newdecl);
2062 locate_old_decl (olddecl);
2063 return false;
2067 /* If we have a prototype after an old-style function definition,
2068 the argument types must be checked specially. */
2069 else if (DECL_INITIAL (olddecl)
2070 && !prototype_p (oldtype) && prototype_p (newtype)
2071 && TYPE_ACTUAL_ARG_TYPES (oldtype))
2073 auto_diagnostic_group d;
2074 if (!validate_proto_after_old_defn (newdecl, newtype, oldtype))
2076 locate_old_decl (olddecl);
2077 return false;
2080 /* A non-static declaration (even an "extern") followed by a
2081 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2082 The same is true for a static forward declaration at block
2083 scope followed by a non-static declaration/definition at file
2084 scope. Static followed by non-static at the same scope is
2085 not undefined behavior, and is the most convenient way to get
2086 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2087 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2088 we do diagnose it if -Wtraditional. */
2089 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2091 /* Two exceptions to the rule. If olddecl is an extern
2092 inline, or a predeclared function that isn't actually
2093 built in, newdecl silently overrides olddecl. The latter
2094 occur only in Objective C; see also above. (FIXME: Make
2095 Objective C use normal builtins.) */
2096 if (!DECL_IS_BUILTIN (olddecl)
2097 && !DECL_EXTERN_INLINE (olddecl))
2099 auto_diagnostic_group d;
2100 error ("static declaration of %q+D follows "
2101 "non-static declaration", newdecl);
2102 locate_old_decl (olddecl);
2104 return false;
2106 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2108 if (DECL_CONTEXT (olddecl))
2110 auto_diagnostic_group d;
2111 error ("non-static declaration of %q+D follows "
2112 "static declaration", newdecl);
2113 locate_old_decl (olddecl);
2114 return false;
2116 else if (warn_traditional)
2118 warned |= warning (OPT_Wtraditional,
2119 "non-static declaration of %q+D "
2120 "follows static declaration", newdecl);
2124 /* Make sure gnu_inline attribute is either not present, or
2125 present on all inline decls. */
2126 if (DECL_DECLARED_INLINE_P (olddecl)
2127 && DECL_DECLARED_INLINE_P (newdecl))
2129 bool newa = lookup_attribute ("gnu_inline",
2130 DECL_ATTRIBUTES (newdecl)) != NULL;
2131 bool olda = lookup_attribute ("gnu_inline",
2132 DECL_ATTRIBUTES (olddecl)) != NULL;
2133 if (newa != olda)
2135 auto_diagnostic_group d;
2136 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2137 newa ? newdecl : olddecl);
2138 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2139 "but not here");
2143 else if (VAR_P (newdecl))
2145 /* Only variables can be thread-local, and all declarations must
2146 agree on this property. */
2147 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2149 /* Nothing to check. Since OLDDECL is marked threadprivate
2150 and NEWDECL does not have a thread-local attribute, we
2151 will merge the threadprivate attribute into NEWDECL. */
2154 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2156 auto_diagnostic_group d;
2157 if (DECL_THREAD_LOCAL_P (newdecl))
2158 error ("thread-local declaration of %q+D follows "
2159 "non-thread-local declaration", newdecl);
2160 else
2161 error ("non-thread-local declaration of %q+D follows "
2162 "thread-local declaration", newdecl);
2164 locate_old_decl (olddecl);
2165 return false;
2168 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2169 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2171 auto_diagnostic_group d;
2172 error ("redefinition of %q+D", newdecl);
2173 locate_old_decl (olddecl);
2174 return false;
2177 /* Objects declared at file scope: if the first declaration had
2178 external linkage (even if it was an external reference) the
2179 second must have external linkage as well, or the behavior is
2180 undefined. If the first declaration had internal linkage, then
2181 the second must too, or else be an external reference (in which
2182 case the composite declaration still has internal linkage).
2183 As for function declarations, we warn about the static-then-
2184 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2185 if (DECL_FILE_SCOPE_P (newdecl)
2186 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2188 if (DECL_EXTERNAL (newdecl))
2190 if (!DECL_FILE_SCOPE_P (olddecl))
2192 auto_diagnostic_group d;
2193 error ("extern declaration of %q+D follows "
2194 "declaration with no linkage", newdecl);
2195 locate_old_decl (olddecl);
2196 return false;
2198 else if (warn_traditional)
2200 warned |= warning (OPT_Wtraditional,
2201 "non-static declaration of %q+D "
2202 "follows static declaration", newdecl);
2205 else
2207 auto_diagnostic_group d;
2208 if (TREE_PUBLIC (newdecl))
2209 error ("non-static declaration of %q+D follows "
2210 "static declaration", newdecl);
2211 else
2212 error ("static declaration of %q+D follows "
2213 "non-static declaration", newdecl);
2215 locate_old_decl (olddecl);
2216 return false;
2219 /* Two objects with the same name declared at the same block
2220 scope must both be external references (6.7p3). */
2221 else if (!DECL_FILE_SCOPE_P (newdecl))
2223 if (DECL_EXTERNAL (newdecl))
2225 /* Extern with initializer at block scope, which will
2226 already have received an error. */
2228 else if (DECL_EXTERNAL (olddecl))
2230 auto_diagnostic_group d;
2231 error ("declaration of %q+D with no linkage follows "
2232 "extern declaration", newdecl);
2233 locate_old_decl (olddecl);
2235 else
2237 auto_diagnostic_group d;
2238 error ("redeclaration of %q+D with no linkage", newdecl);
2239 locate_old_decl (olddecl);
2242 return false;
2245 /* C++ does not permit a decl to appear multiple times at file
2246 scope. */
2247 if (warn_cxx_compat
2248 && DECL_FILE_SCOPE_P (newdecl)
2249 && !DECL_EXTERNAL (newdecl)
2250 && !DECL_EXTERNAL (olddecl))
2251 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2252 OPT_Wc___compat,
2253 ("duplicate declaration of %qD is "
2254 "invalid in C++"),
2255 newdecl);
2258 /* warnings */
2259 /* All decls must agree on a visibility. */
2260 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2261 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2262 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2264 warned |= warning (0, "redeclaration of %q+D with different visibility "
2265 "(old visibility preserved)", newdecl);
2268 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2269 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2270 else /* PARM_DECL, VAR_DECL */
2272 /* Redeclaration of a parameter is a constraint violation (this is
2273 not explicitly stated, but follows from C99 6.7p3 [no more than
2274 one declaration of the same identifier with no linkage in the
2275 same scope, except type tags] and 6.2.2p6 [parameters have no
2276 linkage]). We must check for a forward parameter declaration,
2277 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2278 an extension, the mandatory diagnostic for which is handled by
2279 mark_forward_parm_decls. */
2281 if (TREE_CODE (newdecl) == PARM_DECL
2282 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2284 auto_diagnostic_group d;
2285 error ("redefinition of parameter %q+D", newdecl);
2286 locate_old_decl (olddecl);
2287 return false;
2291 /* Optional warning for completely redundant decls. */
2292 if (!warned && !pedwarned
2293 && warn_redundant_decls
2294 /* Don't warn about a function declaration followed by a
2295 definition. */
2296 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2297 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2298 /* Don't warn about redundant redeclarations of builtins. */
2299 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2300 && !fndecl_built_in_p (newdecl)
2301 && fndecl_built_in_p (olddecl)
2302 && !C_DECL_DECLARED_BUILTIN (olddecl))
2303 /* Don't warn about an extern followed by a definition. */
2304 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2305 /* Don't warn about forward parameter decls. */
2306 && !(TREE_CODE (newdecl) == PARM_DECL
2307 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2308 /* Don't warn about a variable definition following a declaration. */
2309 && !(VAR_P (newdecl)
2310 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2312 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2313 newdecl);
2316 /* Report location of previous decl/defn. */
2317 if (warned || pedwarned)
2318 locate_old_decl (olddecl);
2320 #undef DECL_EXTERN_INLINE
2322 return retval;
2325 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2326 consistent with OLDDECL, but carries new information. Merge the
2327 new information into OLDDECL. This function issues no
2328 diagnostics. */
2330 static void
2331 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2333 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2334 && DECL_INITIAL (newdecl) != NULL_TREE);
2335 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2336 && prototype_p (TREE_TYPE (newdecl)));
2337 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2338 && prototype_p (TREE_TYPE (olddecl)));
2340 /* For real parm decl following a forward decl, rechain the old decl
2341 in its new location and clear TREE_ASM_WRITTEN (it's not a
2342 forward decl anymore). */
2343 if (TREE_CODE (newdecl) == PARM_DECL
2344 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2346 struct c_binding *b, **here;
2348 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2349 if ((*here)->decl == olddecl)
2350 goto found;
2351 gcc_unreachable ();
2353 found:
2354 b = *here;
2355 *here = b->prev;
2356 b->prev = current_scope->bindings;
2357 current_scope->bindings = b;
2359 TREE_ASM_WRITTEN (olddecl) = 0;
2362 DECL_ATTRIBUTES (newdecl)
2363 = targetm.merge_decl_attributes (olddecl, newdecl);
2365 /* For typedefs use the old type, as the new type's DECL_NAME points
2366 at newdecl, which will be ggc_freed. */
2367 if (TREE_CODE (newdecl) == TYPE_DECL)
2369 /* But NEWTYPE might have an attribute, honor that. */
2370 tree tem = newtype;
2371 newtype = oldtype;
2373 if (TYPE_USER_ALIGN (tem))
2375 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2376 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2377 TYPE_USER_ALIGN (newtype) = true;
2380 /* And remove the new type from the variants list. */
2381 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2383 tree remove = TREE_TYPE (newdecl);
2384 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2385 t = TYPE_NEXT_VARIANT (t))
2386 if (TYPE_NEXT_VARIANT (t) == remove)
2388 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2389 break;
2394 /* Merge the data types specified in the two decls. */
2395 TREE_TYPE (newdecl)
2396 = TREE_TYPE (olddecl)
2397 = composite_type (newtype, oldtype);
2399 /* Lay the type out, unless already done. */
2400 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2402 if (TREE_TYPE (newdecl) != error_mark_node)
2403 layout_type (TREE_TYPE (newdecl));
2404 if (TREE_CODE (newdecl) != FUNCTION_DECL
2405 && TREE_CODE (newdecl) != TYPE_DECL
2406 && TREE_CODE (newdecl) != CONST_DECL)
2407 layout_decl (newdecl, 0);
2409 else
2411 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2412 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2413 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2414 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2415 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2417 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2418 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2420 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2421 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2422 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2423 DECL_WARN_IF_NOT_ALIGN (olddecl));
2426 /* Keep the old rtl since we can safely use it. */
2427 if (HAS_RTL_P (olddecl))
2428 COPY_DECL_RTL (olddecl, newdecl);
2430 /* Merge the type qualifiers. */
2431 if (TREE_READONLY (newdecl))
2432 TREE_READONLY (olddecl) = 1;
2434 if (TREE_THIS_VOLATILE (newdecl))
2435 TREE_THIS_VOLATILE (olddecl) = 1;
2437 /* Merge deprecatedness. */
2438 if (TREE_DEPRECATED (newdecl))
2439 TREE_DEPRECATED (olddecl) = 1;
2441 /* If a decl is in a system header and the other isn't, keep the one on the
2442 system header. Otherwise, keep source location of definition rather than
2443 declaration and of prototype rather than non-prototype unless that
2444 prototype is built-in. */
2445 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2446 && DECL_IN_SYSTEM_HEADER (olddecl)
2447 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2448 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2449 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2450 && DECL_IN_SYSTEM_HEADER (newdecl)
2451 && !DECL_IN_SYSTEM_HEADER (olddecl))
2452 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2453 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2454 && DECL_INITIAL (olddecl) != NULL_TREE)
2455 || (old_is_prototype && !new_is_prototype
2456 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2457 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2459 /* Merge the initialization information. */
2460 if (DECL_INITIAL (newdecl) == NULL_TREE)
2461 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2463 /* Merge the threadprivate attribute. */
2464 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2465 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2467 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2469 /* Copy the assembler name.
2470 Currently, it can only be defined in the prototype. */
2471 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2473 /* Use visibility of whichever declaration had it specified */
2474 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2476 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2477 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2480 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2482 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2483 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2484 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2485 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2486 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2487 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2488 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2489 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2490 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2491 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2492 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2495 /* Merge the storage class information. */
2496 merge_weak (newdecl, olddecl);
2498 /* For functions, static overrides non-static. */
2499 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2501 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2502 /* This is since we don't automatically
2503 copy the attributes of NEWDECL into OLDDECL. */
2504 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2505 /* If this clears `static', clear it in the identifier too. */
2506 if (!TREE_PUBLIC (olddecl))
2507 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2511 /* In c99, 'extern' declaration before (or after) 'inline' means this
2512 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2513 is present. */
2514 if (TREE_CODE (newdecl) == FUNCTION_DECL
2515 && !flag_gnu89_inline
2516 && (DECL_DECLARED_INLINE_P (newdecl)
2517 || DECL_DECLARED_INLINE_P (olddecl))
2518 && (!DECL_DECLARED_INLINE_P (newdecl)
2519 || !DECL_DECLARED_INLINE_P (olddecl)
2520 || !DECL_EXTERNAL (olddecl))
2521 && DECL_EXTERNAL (newdecl)
2522 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2523 && !current_function_decl)
2524 DECL_EXTERNAL (newdecl) = 0;
2526 /* An inline definition following a static declaration is not
2527 DECL_EXTERNAL. */
2528 if (new_is_definition
2529 && (DECL_DECLARED_INLINE_P (newdecl)
2530 || DECL_DECLARED_INLINE_P (olddecl))
2531 && !TREE_PUBLIC (olddecl))
2532 DECL_EXTERNAL (newdecl) = 0;
2534 if (DECL_EXTERNAL (newdecl))
2536 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2537 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2539 /* An extern decl does not override previous storage class. */
2540 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2541 if (!DECL_EXTERNAL (newdecl))
2543 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2544 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2547 else
2549 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2550 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2553 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2555 /* If we're redefining a function previously defined as extern
2556 inline, make sure we emit debug info for the inline before we
2557 throw it away, in case it was inlined into a function that
2558 hasn't been written out yet. */
2559 if (new_is_definition && DECL_INITIAL (olddecl))
2560 /* The new defn must not be inline. */
2561 DECL_UNINLINABLE (newdecl) = 1;
2562 else
2564 /* If either decl says `inline', this fn is inline, unless
2565 its definition was passed already. */
2566 if (DECL_DECLARED_INLINE_P (newdecl)
2567 || DECL_DECLARED_INLINE_P (olddecl))
2568 DECL_DECLARED_INLINE_P (newdecl) = 1;
2570 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2571 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2573 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2574 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2575 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2576 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2579 if (fndecl_built_in_p (olddecl))
2581 /* If redeclaring a builtin function, it stays built in.
2582 But it gets tagged as having been declared. */
2583 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2584 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2585 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2586 if (new_is_prototype)
2588 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2589 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2591 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2592 switch (fncode)
2594 /* If a compatible prototype of these builtin functions
2595 is seen, assume the runtime implements it with the
2596 expected semantics. */
2597 case BUILT_IN_STPCPY:
2598 if (builtin_decl_explicit_p (fncode))
2599 set_builtin_decl_implicit_p (fncode, true);
2600 break;
2601 default:
2602 if (builtin_decl_explicit_p (fncode))
2603 set_builtin_decl_declared_p (fncode, true);
2604 break;
2607 copy_attributes_to_builtin (newdecl);
2610 else
2611 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2612 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2615 /* Preserve function specific target and optimization options */
2616 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2617 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2618 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2619 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2621 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2622 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2623 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2624 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2626 /* Also preserve various other info from the definition. */
2627 if (!new_is_definition)
2629 tree t;
2630 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2631 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2632 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2633 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2634 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2635 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2636 DECL_CONTEXT (t) = newdecl;
2638 /* See if we've got a function to instantiate from. */
2639 if (DECL_SAVED_TREE (olddecl))
2640 DECL_ABSTRACT_ORIGIN (newdecl)
2641 = DECL_ABSTRACT_ORIGIN (olddecl);
2645 /* Merge the USED information. */
2646 if (TREE_USED (olddecl))
2647 TREE_USED (newdecl) = 1;
2648 else if (TREE_USED (newdecl))
2649 TREE_USED (olddecl) = 1;
2650 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2651 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2652 if (DECL_PRESERVE_P (olddecl))
2653 DECL_PRESERVE_P (newdecl) = 1;
2654 else if (DECL_PRESERVE_P (newdecl))
2655 DECL_PRESERVE_P (olddecl) = 1;
2657 /* Merge DECL_COMMON */
2658 if (VAR_P (olddecl) && VAR_P (newdecl)
2659 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2660 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2661 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2663 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2664 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2665 DECL_ARGUMENTS (if appropriate). */
2667 unsigned olddecl_uid = DECL_UID (olddecl);
2668 tree olddecl_context = DECL_CONTEXT (olddecl);
2669 tree olddecl_arguments = NULL;
2670 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2671 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2673 memcpy ((char *) olddecl + sizeof (struct tree_common),
2674 (char *) newdecl + sizeof (struct tree_common),
2675 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2676 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2677 switch (TREE_CODE (olddecl))
2679 case FUNCTION_DECL:
2680 case VAR_DECL:
2682 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2684 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2685 (char *) newdecl + sizeof (struct tree_decl_common),
2686 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2687 olddecl->decl_with_vis.symtab_node = snode;
2689 if ((DECL_EXTERNAL (olddecl)
2690 || TREE_PUBLIC (olddecl)
2691 || TREE_STATIC (olddecl))
2692 && DECL_SECTION_NAME (newdecl) != NULL)
2693 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2695 /* This isn't quite correct for something like
2696 int __thread x attribute ((tls_model ("local-exec")));
2697 extern int __thread x;
2698 as we'll lose the "local-exec" model. */
2699 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2700 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2701 break;
2704 case FIELD_DECL:
2705 case PARM_DECL:
2706 case LABEL_DECL:
2707 case RESULT_DECL:
2708 case CONST_DECL:
2709 case TYPE_DECL:
2710 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2711 (char *) newdecl + sizeof (struct tree_decl_common),
2712 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2713 break;
2715 default:
2717 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2718 (char *) newdecl + sizeof (struct tree_decl_common),
2719 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2721 DECL_UID (olddecl) = olddecl_uid;
2722 DECL_CONTEXT (olddecl) = olddecl_context;
2723 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2724 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2727 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2728 so that encode_section_info has a chance to look at the new decl
2729 flags and attributes. */
2730 if (DECL_RTL_SET_P (olddecl)
2731 && (TREE_CODE (olddecl) == FUNCTION_DECL
2732 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2733 make_decl_rtl (olddecl);
2736 /* Handle when a new declaration NEWDECL has the same name as an old
2737 one OLDDECL in the same binding contour. Prints an error message
2738 if appropriate.
2740 If safely possible, alter OLDDECL to look like NEWDECL, and return
2741 true. Otherwise, return false. */
2743 static bool
2744 duplicate_decls (tree newdecl, tree olddecl)
2746 tree newtype = NULL, oldtype = NULL;
2748 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2750 /* Avoid `unused variable' and other warnings for OLDDECL. */
2751 TREE_NO_WARNING (olddecl) = 1;
2752 return false;
2755 merge_decls (newdecl, olddecl, newtype, oldtype);
2757 /* The NEWDECL will no longer be needed.
2759 Before releasing the node, be sure to remove function from symbol
2760 table that might have been inserted there to record comdat group.
2761 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2762 structure is shared in between NEWDECL and OLDECL. */
2763 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2764 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2765 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2767 struct symtab_node *snode = symtab_node::get (newdecl);
2768 if (snode)
2769 snode->remove ();
2771 ggc_free (newdecl);
2772 return true;
2776 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2777 static void
2778 warn_if_shadowing (tree new_decl)
2780 struct c_binding *b;
2782 /* Shadow warnings wanted? */
2783 if (!(warn_shadow
2784 || warn_shadow_local
2785 || warn_shadow_compatible_local)
2786 /* No shadow warnings for internally generated vars. */
2787 || DECL_IS_BUILTIN (new_decl))
2788 return;
2790 /* Is anything being shadowed? Invisible decls do not count. */
2791 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2792 if (b->decl && b->decl != new_decl && !b->invisible
2793 && (b->decl == error_mark_node
2794 || diagnostic_report_warnings_p (global_dc,
2795 DECL_SOURCE_LOCATION (b->decl))))
2797 tree old_decl = b->decl;
2799 if (old_decl == error_mark_node)
2801 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2802 "non-variable", new_decl);
2803 break;
2806 bool warned = false;
2807 auto_diagnostic_group d;
2808 if (TREE_CODE (old_decl) == PARM_DECL)
2810 enum opt_code warning_code;
2812 /* If '-Wshadow=compatible-local' is specified without other
2813 -Wshadow= flags, we will warn only when the types of the
2814 shadowing variable (i.e. new_decl) and the shadowed variable
2815 (old_decl) are compatible. */
2816 if (warn_shadow)
2817 warning_code = OPT_Wshadow;
2818 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2819 warning_code = OPT_Wshadow_compatible_local;
2820 else
2821 warning_code = OPT_Wshadow_local;
2822 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2823 "declaration of %qD shadows a parameter",
2824 new_decl);
2826 else if (DECL_FILE_SCOPE_P (old_decl))
2828 /* Do not warn if a variable shadows a function, unless
2829 the variable is a function or a pointer-to-function. */
2830 if (TREE_CODE (old_decl) == FUNCTION_DECL
2831 && TREE_CODE (new_decl) != FUNCTION_DECL
2832 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2833 continue;
2835 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2836 "declaration of %qD shadows a global "
2837 "declaration",
2838 new_decl);
2840 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2841 && fndecl_built_in_p (old_decl))
2843 warning (OPT_Wshadow, "declaration of %q+D shadows "
2844 "a built-in function", new_decl);
2845 break;
2847 else
2849 enum opt_code warning_code;
2851 /* If '-Wshadow=compatible-local' is specified without other
2852 -Wshadow= flags, we will warn only when the types of the
2853 shadowing variable (i.e. new_decl) and the shadowed variable
2854 (old_decl) are compatible. */
2855 if (warn_shadow)
2856 warning_code = OPT_Wshadow;
2857 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2858 warning_code = OPT_Wshadow_compatible_local;
2859 else
2860 warning_code = OPT_Wshadow_local;
2861 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2862 "declaration of %qD shadows a previous local",
2863 new_decl);
2866 if (warned)
2867 inform (DECL_SOURCE_LOCATION (old_decl),
2868 "shadowed declaration is here");
2870 break;
2874 /* Record a decl-node X as belonging to the current lexical scope.
2875 Check for errors (such as an incompatible declaration for the same
2876 name already seen in the same scope).
2878 Returns either X or an old decl for the same name.
2879 If an old decl is returned, it may have been smashed
2880 to agree with what X says. */
2882 tree
2883 pushdecl (tree x)
2885 tree name = DECL_NAME (x);
2886 struct c_scope *scope = current_scope;
2887 struct c_binding *b;
2888 bool nested = false;
2889 location_t locus = DECL_SOURCE_LOCATION (x);
2891 /* Must set DECL_CONTEXT for everything not at file scope or
2892 DECL_FILE_SCOPE_P won't work. Local externs don't count
2893 unless they have initializers (which generate code). */
2894 if (current_function_decl
2895 && (!VAR_OR_FUNCTION_DECL_P (x)
2896 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2897 DECL_CONTEXT (x) = current_function_decl;
2899 /* Anonymous decls are just inserted in the scope. */
2900 if (!name)
2902 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2903 locus);
2904 return x;
2907 /* First, see if there is another declaration with the same name in
2908 the current scope. If there is, duplicate_decls may do all the
2909 work for us. If duplicate_decls returns false, that indicates
2910 two incompatible decls in the same scope; we are to silently
2911 replace the old one (duplicate_decls has issued all appropriate
2912 diagnostics). In particular, we should not consider possible
2913 duplicates in the external scope, or shadowing. */
2914 b = I_SYMBOL_BINDING (name);
2915 if (b && B_IN_SCOPE (b, scope))
2917 struct c_binding *b_ext, *b_use;
2918 tree type = TREE_TYPE (x);
2919 tree visdecl = b->decl;
2920 tree vistype = TREE_TYPE (visdecl);
2921 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2922 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2923 b->inner_comp = false;
2924 b_use = b;
2925 b_ext = b;
2926 /* If this is an external linkage declaration, we should check
2927 for compatibility with the type in the external scope before
2928 setting the type at this scope based on the visible
2929 information only. */
2930 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2932 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2933 b_ext = b_ext->shadowed;
2934 if (b_ext)
2936 b_use = b_ext;
2937 if (b_use->u.type)
2938 TREE_TYPE (b_use->decl) = b_use->u.type;
2941 if (duplicate_decls (x, b_use->decl))
2943 if (b_use != b)
2945 /* Save the updated type in the external scope and
2946 restore the proper type for this scope. */
2947 tree thistype;
2948 if (comptypes (vistype, type))
2949 thistype = composite_type (vistype, type);
2950 else
2951 thistype = TREE_TYPE (b_use->decl);
2952 b_use->u.type = TREE_TYPE (b_use->decl);
2953 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2954 && fndecl_built_in_p (b_use->decl))
2955 thistype
2956 = build_type_attribute_variant (thistype,
2957 TYPE_ATTRIBUTES
2958 (b_use->u.type));
2959 TREE_TYPE (b_use->decl) = thistype;
2961 return b_use->decl;
2963 else
2964 goto skip_external_and_shadow_checks;
2967 /* All declarations with external linkage, and all external
2968 references, go in the external scope, no matter what scope is
2969 current. However, the binding in that scope is ignored for
2970 purposes of normal name lookup. A separate binding structure is
2971 created in the requested scope; this governs the normal
2972 visibility of the symbol.
2974 The binding in the externals scope is used exclusively for
2975 detecting duplicate declarations of the same object, no matter
2976 what scope they are in; this is what we do here. (C99 6.2.7p2:
2977 All declarations that refer to the same object or function shall
2978 have compatible type; otherwise, the behavior is undefined.) */
2979 if (DECL_EXTERNAL (x) || scope == file_scope)
2981 tree type = TREE_TYPE (x);
2982 tree vistype = NULL_TREE;
2983 tree visdecl = NULL_TREE;
2984 bool type_saved = false;
2985 if (b && !B_IN_EXTERNAL_SCOPE (b)
2986 && VAR_OR_FUNCTION_DECL_P (b->decl)
2987 && DECL_FILE_SCOPE_P (b->decl))
2989 visdecl = b->decl;
2990 vistype = TREE_TYPE (visdecl);
2992 if (scope != file_scope
2993 && !DECL_IN_SYSTEM_HEADER (x))
2994 warning_at (locus, OPT_Wnested_externs,
2995 "nested extern declaration of %qD", x);
2997 while (b && !B_IN_EXTERNAL_SCOPE (b))
2999 /* If this decl might be modified, save its type. This is
3000 done here rather than when the decl is first bound
3001 because the type may change after first binding, through
3002 being completed or through attributes being added. If we
3003 encounter multiple such decls, only the first should have
3004 its type saved; the others will already have had their
3005 proper types saved and the types will not have changed as
3006 their scopes will not have been re-entered. */
3007 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3009 b->u.type = TREE_TYPE (b->decl);
3010 type_saved = true;
3012 if (B_IN_FILE_SCOPE (b)
3013 && VAR_P (b->decl)
3014 && TREE_STATIC (b->decl)
3015 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3016 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3017 && TREE_CODE (type) == ARRAY_TYPE
3018 && TYPE_DOMAIN (type)
3019 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3020 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3022 /* Array type completed in inner scope, which should be
3023 diagnosed if the completion does not have size 1 and
3024 it does not get completed in the file scope. */
3025 b->inner_comp = true;
3027 b = b->shadowed;
3030 /* If a matching external declaration has been found, set its
3031 type to the composite of all the types of that declaration.
3032 After the consistency checks, it will be reset to the
3033 composite of the visible types only. */
3034 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3035 && b->u.type)
3036 TREE_TYPE (b->decl) = b->u.type;
3038 /* The point of the same_translation_unit_p check here is,
3039 we want to detect a duplicate decl for a construct like
3040 foo() { extern bar(); } ... static bar(); but not if
3041 they are in different translation units. In any case,
3042 the static does not go in the externals scope. */
3043 if (b
3044 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3045 && duplicate_decls (x, b->decl))
3047 tree thistype;
3048 if (vistype)
3050 if (comptypes (vistype, type))
3051 thistype = composite_type (vistype, type);
3052 else
3053 thistype = TREE_TYPE (b->decl);
3055 else
3056 thistype = type;
3057 b->u.type = TREE_TYPE (b->decl);
3058 if (TREE_CODE (b->decl) == FUNCTION_DECL
3059 && fndecl_built_in_p (b->decl))
3060 thistype
3061 = build_type_attribute_variant (thistype,
3062 TYPE_ATTRIBUTES (b->u.type));
3063 TREE_TYPE (b->decl) = thistype;
3064 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3065 locus);
3066 return b->decl;
3068 else if (TREE_PUBLIC (x))
3070 if (visdecl && !b && duplicate_decls (x, visdecl))
3072 /* An external declaration at block scope referring to a
3073 visible entity with internal linkage. The composite
3074 type will already be correct for this scope, so we
3075 just need to fall through to make the declaration in
3076 this scope. */
3077 nested = true;
3078 x = visdecl;
3080 else
3082 bind (name, x, external_scope, /*invisible=*/true,
3083 /*nested=*/false, locus);
3084 nested = true;
3089 if (TREE_CODE (x) != PARM_DECL)
3090 warn_if_shadowing (x);
3092 skip_external_and_shadow_checks:
3093 if (TREE_CODE (x) == TYPE_DECL)
3095 /* So this is a typedef, set its underlying type. */
3096 set_underlying_type (x);
3098 /* If X is a typedef defined in the current function, record it
3099 for the purpose of implementing the -Wunused-local-typedefs
3100 warning. */
3101 record_locally_defined_typedef (x);
3104 bind (name, x, scope, /*invisible=*/false, nested, locus);
3106 /* If x's type is incomplete because it's based on a
3107 structure or union which has not yet been fully declared,
3108 attach it to that structure or union type, so we can go
3109 back and complete the variable declaration later, if the
3110 structure or union gets fully declared.
3112 If the input is erroneous, we can have error_mark in the type
3113 slot (e.g. "f(void a, ...)") - that doesn't count as an
3114 incomplete type. */
3115 if (TREE_TYPE (x) != error_mark_node
3116 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3118 tree element = TREE_TYPE (x);
3120 while (TREE_CODE (element) == ARRAY_TYPE)
3121 element = TREE_TYPE (element);
3122 element = TYPE_MAIN_VARIANT (element);
3124 if (RECORD_OR_UNION_TYPE_P (element)
3125 && (TREE_CODE (x) != TYPE_DECL
3126 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3127 && !COMPLETE_TYPE_P (element))
3128 C_TYPE_INCOMPLETE_VARS (element)
3129 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3131 return x;
3135 /* Issue a warning about implicit function declaration. ID is the function
3136 identifier, OLDDECL is a declaration of the function in a different scope,
3137 or NULL_TREE. */
3139 static void
3140 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3142 if (!warn_implicit_function_declaration)
3143 return;
3145 bool warned;
3146 auto_diagnostic_group d;
3147 name_hint hint;
3148 if (!olddecl)
3149 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3151 if (flag_isoc99)
3153 if (const char *suggestion = hint.suggestion ())
3155 gcc_rich_location richloc (loc);
3156 richloc.add_fixit_replace (suggestion);
3157 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3158 "implicit declaration of function %qE;"
3159 " did you mean %qs?",
3160 id, suggestion);
3162 else
3163 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3164 "implicit declaration of function %qE", id);
3166 else if (const char *suggestion = hint.suggestion ())
3168 gcc_rich_location richloc (loc);
3169 richloc.add_fixit_replace (suggestion);
3170 warned = warning_at
3171 (&richloc, OPT_Wimplicit_function_declaration,
3172 G_("implicit declaration of function %qE; did you mean %qs?"),
3173 id, suggestion);
3175 else
3176 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3177 G_("implicit declaration of function %qE"), id);
3179 if (olddecl && warned)
3180 locate_old_decl (olddecl);
3182 if (!warned)
3183 hint.suppress ();
3186 /* This function represents mapping of a function code FCODE
3187 to its respective header. */
3189 static const char *
3190 header_for_builtin_fn (enum built_in_function fcode)
3192 switch (fcode)
3194 CASE_FLT_FN (BUILT_IN_ACOS):
3195 CASE_FLT_FN (BUILT_IN_ACOSH):
3196 CASE_FLT_FN (BUILT_IN_ASIN):
3197 CASE_FLT_FN (BUILT_IN_ASINH):
3198 CASE_FLT_FN (BUILT_IN_ATAN):
3199 CASE_FLT_FN (BUILT_IN_ATANH):
3200 CASE_FLT_FN (BUILT_IN_ATAN2):
3201 CASE_FLT_FN (BUILT_IN_CBRT):
3202 CASE_FLT_FN (BUILT_IN_CEIL):
3203 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3204 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3205 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3206 CASE_FLT_FN (BUILT_IN_COS):
3207 CASE_FLT_FN (BUILT_IN_COSH):
3208 CASE_FLT_FN (BUILT_IN_ERF):
3209 CASE_FLT_FN (BUILT_IN_ERFC):
3210 CASE_FLT_FN (BUILT_IN_EXP):
3211 CASE_FLT_FN (BUILT_IN_EXP2):
3212 CASE_FLT_FN (BUILT_IN_EXPM1):
3213 CASE_FLT_FN (BUILT_IN_FABS):
3214 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3215 CASE_FLT_FN (BUILT_IN_FDIM):
3216 CASE_FLT_FN (BUILT_IN_FLOOR):
3217 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3218 CASE_FLT_FN (BUILT_IN_FMA):
3219 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3220 CASE_FLT_FN (BUILT_IN_FMAX):
3221 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3222 CASE_FLT_FN (BUILT_IN_FMIN):
3223 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3224 CASE_FLT_FN (BUILT_IN_FMOD):
3225 CASE_FLT_FN (BUILT_IN_FREXP):
3226 CASE_FLT_FN (BUILT_IN_HYPOT):
3227 CASE_FLT_FN (BUILT_IN_ILOGB):
3228 CASE_FLT_FN (BUILT_IN_LDEXP):
3229 CASE_FLT_FN (BUILT_IN_LGAMMA):
3230 CASE_FLT_FN (BUILT_IN_LLRINT):
3231 CASE_FLT_FN (BUILT_IN_LLROUND):
3232 CASE_FLT_FN (BUILT_IN_LOG):
3233 CASE_FLT_FN (BUILT_IN_LOG10):
3234 CASE_FLT_FN (BUILT_IN_LOG1P):
3235 CASE_FLT_FN (BUILT_IN_LOG2):
3236 CASE_FLT_FN (BUILT_IN_LOGB):
3237 CASE_FLT_FN (BUILT_IN_LRINT):
3238 CASE_FLT_FN (BUILT_IN_LROUND):
3239 CASE_FLT_FN (BUILT_IN_MODF):
3240 CASE_FLT_FN (BUILT_IN_NAN):
3241 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3242 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3243 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3244 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3245 CASE_FLT_FN (BUILT_IN_POW):
3246 CASE_FLT_FN (BUILT_IN_REMAINDER):
3247 CASE_FLT_FN (BUILT_IN_REMQUO):
3248 CASE_FLT_FN (BUILT_IN_RINT):
3249 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3250 CASE_FLT_FN (BUILT_IN_ROUND):
3251 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3252 CASE_FLT_FN (BUILT_IN_SCALBLN):
3253 CASE_FLT_FN (BUILT_IN_SCALBN):
3254 CASE_FLT_FN (BUILT_IN_SIN):
3255 CASE_FLT_FN (BUILT_IN_SINH):
3256 CASE_FLT_FN (BUILT_IN_SINCOS):
3257 CASE_FLT_FN (BUILT_IN_SQRT):
3258 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3259 CASE_FLT_FN (BUILT_IN_TAN):
3260 CASE_FLT_FN (BUILT_IN_TANH):
3261 CASE_FLT_FN (BUILT_IN_TGAMMA):
3262 CASE_FLT_FN (BUILT_IN_TRUNC):
3263 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3264 case BUILT_IN_ISINF:
3265 case BUILT_IN_ISNAN:
3266 return "<math.h>";
3267 CASE_FLT_FN (BUILT_IN_CABS):
3268 CASE_FLT_FN (BUILT_IN_CACOS):
3269 CASE_FLT_FN (BUILT_IN_CACOSH):
3270 CASE_FLT_FN (BUILT_IN_CARG):
3271 CASE_FLT_FN (BUILT_IN_CASIN):
3272 CASE_FLT_FN (BUILT_IN_CASINH):
3273 CASE_FLT_FN (BUILT_IN_CATAN):
3274 CASE_FLT_FN (BUILT_IN_CATANH):
3275 CASE_FLT_FN (BUILT_IN_CCOS):
3276 CASE_FLT_FN (BUILT_IN_CCOSH):
3277 CASE_FLT_FN (BUILT_IN_CEXP):
3278 CASE_FLT_FN (BUILT_IN_CIMAG):
3279 CASE_FLT_FN (BUILT_IN_CLOG):
3280 CASE_FLT_FN (BUILT_IN_CONJ):
3281 CASE_FLT_FN (BUILT_IN_CPOW):
3282 CASE_FLT_FN (BUILT_IN_CPROJ):
3283 CASE_FLT_FN (BUILT_IN_CREAL):
3284 CASE_FLT_FN (BUILT_IN_CSIN):
3285 CASE_FLT_FN (BUILT_IN_CSINH):
3286 CASE_FLT_FN (BUILT_IN_CSQRT):
3287 CASE_FLT_FN (BUILT_IN_CTAN):
3288 CASE_FLT_FN (BUILT_IN_CTANH):
3289 return "<complex.h>";
3290 case BUILT_IN_MEMCHR:
3291 case BUILT_IN_MEMCMP:
3292 case BUILT_IN_MEMCPY:
3293 case BUILT_IN_MEMMOVE:
3294 case BUILT_IN_MEMSET:
3295 case BUILT_IN_STRCAT:
3296 case BUILT_IN_STRCHR:
3297 case BUILT_IN_STRCMP:
3298 case BUILT_IN_STRCPY:
3299 case BUILT_IN_STRCSPN:
3300 case BUILT_IN_STRLEN:
3301 case BUILT_IN_STRNCAT:
3302 case BUILT_IN_STRNCMP:
3303 case BUILT_IN_STRNCPY:
3304 case BUILT_IN_STRPBRK:
3305 case BUILT_IN_STRRCHR:
3306 case BUILT_IN_STRSPN:
3307 case BUILT_IN_STRSTR:
3308 return "<string.h>";
3309 case BUILT_IN_FPRINTF:
3310 case BUILT_IN_PUTC:
3311 case BUILT_IN_FPUTC:
3312 case BUILT_IN_FPUTS:
3313 case BUILT_IN_FSCANF:
3314 case BUILT_IN_FWRITE:
3315 case BUILT_IN_PRINTF:
3316 case BUILT_IN_PUTCHAR:
3317 case BUILT_IN_PUTS:
3318 case BUILT_IN_SCANF:
3319 case BUILT_IN_SNPRINTF:
3320 case BUILT_IN_SPRINTF:
3321 case BUILT_IN_SSCANF:
3322 case BUILT_IN_VFPRINTF:
3323 case BUILT_IN_VFSCANF:
3324 case BUILT_IN_VPRINTF:
3325 case BUILT_IN_VSCANF:
3326 case BUILT_IN_VSNPRINTF:
3327 case BUILT_IN_VSPRINTF:
3328 case BUILT_IN_VSSCANF:
3329 return "<stdio.h>";
3330 case BUILT_IN_ISALNUM:
3331 case BUILT_IN_ISALPHA:
3332 case BUILT_IN_ISBLANK:
3333 case BUILT_IN_ISCNTRL:
3334 case BUILT_IN_ISDIGIT:
3335 case BUILT_IN_ISGRAPH:
3336 case BUILT_IN_ISLOWER:
3337 case BUILT_IN_ISPRINT:
3338 case BUILT_IN_ISPUNCT:
3339 case BUILT_IN_ISSPACE:
3340 case BUILT_IN_ISUPPER:
3341 case BUILT_IN_ISXDIGIT:
3342 case BUILT_IN_TOLOWER:
3343 case BUILT_IN_TOUPPER:
3344 return "<ctype.h>";
3345 case BUILT_IN_ISWALNUM:
3346 case BUILT_IN_ISWALPHA:
3347 case BUILT_IN_ISWBLANK:
3348 case BUILT_IN_ISWCNTRL:
3349 case BUILT_IN_ISWDIGIT:
3350 case BUILT_IN_ISWGRAPH:
3351 case BUILT_IN_ISWLOWER:
3352 case BUILT_IN_ISWPRINT:
3353 case BUILT_IN_ISWPUNCT:
3354 case BUILT_IN_ISWSPACE:
3355 case BUILT_IN_ISWUPPER:
3356 case BUILT_IN_ISWXDIGIT:
3357 case BUILT_IN_TOWLOWER:
3358 case BUILT_IN_TOWUPPER:
3359 return "<wctype.h>";
3360 case BUILT_IN_ABORT:
3361 case BUILT_IN_ABS:
3362 case BUILT_IN_CALLOC:
3363 case BUILT_IN_EXIT:
3364 case BUILT_IN_FREE:
3365 case BUILT_IN_LABS:
3366 case BUILT_IN_LLABS:
3367 case BUILT_IN_MALLOC:
3368 case BUILT_IN_REALLOC:
3369 case BUILT_IN__EXIT2:
3370 case BUILT_IN_ALIGNED_ALLOC:
3371 return "<stdlib.h>";
3372 case BUILT_IN_IMAXABS:
3373 return "<inttypes.h>";
3374 case BUILT_IN_STRFTIME:
3375 return "<time.h>";
3376 default:
3377 return NULL;
3381 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3382 function of type int (). */
3384 tree
3385 implicitly_declare (location_t loc, tree functionid)
3387 struct c_binding *b;
3388 tree decl = NULL_TREE;
3389 tree asmspec_tree;
3391 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3393 if (B_IN_SCOPE (b, external_scope))
3395 decl = b->decl;
3396 break;
3400 if (decl)
3402 if (TREE_CODE (decl) != FUNCTION_DECL)
3403 return decl;
3405 /* FIXME: Objective-C has weird not-really-builtin functions
3406 which are supposed to be visible automatically. They wind up
3407 in the external scope because they're pushed before the file
3408 scope gets created. Catch this here and rebind them into the
3409 file scope. */
3410 if (!fndecl_built_in_p (decl) && DECL_IS_BUILTIN (decl))
3412 bind (functionid, decl, file_scope,
3413 /*invisible=*/false, /*nested=*/true,
3414 DECL_SOURCE_LOCATION (decl));
3415 return decl;
3417 else
3419 tree newtype = default_function_type;
3420 if (b->u.type)
3421 TREE_TYPE (decl) = b->u.type;
3422 /* Implicit declaration of a function already declared
3423 (somehow) in a different scope, or as a built-in.
3424 If this is the first time this has happened, warn;
3425 then recycle the old declaration but with the new type. */
3426 if (!C_DECL_IMPLICIT (decl))
3428 implicit_decl_warning (loc, functionid, decl);
3429 C_DECL_IMPLICIT (decl) = 1;
3431 if (fndecl_built_in_p (decl))
3433 newtype = build_type_attribute_variant (newtype,
3434 TYPE_ATTRIBUTES
3435 (TREE_TYPE (decl)));
3436 if (!comptypes (newtype, TREE_TYPE (decl)))
3438 bool warned = warning_at (loc, 0, "incompatible implicit "
3439 "declaration of built-in "
3440 "function %qD", decl);
3441 /* See if we can hint which header to include. */
3442 const char *header
3443 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3444 if (header != NULL && warned)
3446 rich_location richloc (line_table, loc);
3447 maybe_add_include_fixit (&richloc, header, true);
3448 inform (&richloc,
3449 "include %qs or provide a declaration of %qD",
3450 header, decl);
3452 newtype = TREE_TYPE (decl);
3455 else
3457 if (!comptypes (newtype, TREE_TYPE (decl)))
3459 auto_diagnostic_group d;
3460 error_at (loc, "incompatible implicit declaration of "
3461 "function %qD", decl);
3462 locate_old_decl (decl);
3465 b->u.type = TREE_TYPE (decl);
3466 TREE_TYPE (decl) = newtype;
3467 bind (functionid, decl, current_scope,
3468 /*invisible=*/false, /*nested=*/true,
3469 DECL_SOURCE_LOCATION (decl));
3470 return decl;
3474 /* Not seen before. */
3475 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3476 DECL_EXTERNAL (decl) = 1;
3477 TREE_PUBLIC (decl) = 1;
3478 C_DECL_IMPLICIT (decl) = 1;
3479 implicit_decl_warning (loc, functionid, 0);
3480 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3481 if (asmspec_tree)
3482 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3484 /* C89 says implicit declarations are in the innermost block.
3485 So we record the decl in the standard fashion. */
3486 decl = pushdecl (decl);
3488 /* No need to call objc_check_decl here - it's a function type. */
3489 rest_of_decl_compilation (decl, 0, 0);
3491 /* Write a record describing this implicit function declaration
3492 to the prototypes file (if requested). */
3493 gen_aux_info_record (decl, 0, 1, 0);
3495 /* Possibly apply some default attributes to this implicit declaration. */
3496 decl_attributes (&decl, NULL_TREE, 0);
3498 return decl;
3501 /* Issue an error message for a reference to an undeclared variable
3502 ID, including a reference to a builtin outside of function-call
3503 context. Establish a binding of the identifier to error_mark_node
3504 in an appropriate scope, which will suppress further errors for the
3505 same identifier. The error message should be given location LOC. */
3506 void
3507 undeclared_variable (location_t loc, tree id)
3509 static bool already = false;
3510 struct c_scope *scope;
3512 auto_diagnostic_group d;
3513 if (current_function_decl == NULL_TREE)
3515 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3516 if (const char *suggestion = guessed_id.suggestion ())
3518 gcc_rich_location richloc (loc);
3519 richloc.add_fixit_replace (suggestion);
3520 error_at (&richloc,
3521 "%qE undeclared here (not in a function);"
3522 " did you mean %qs?",
3523 id, suggestion);
3525 else
3526 error_at (loc, "%qE undeclared here (not in a function)", id);
3527 scope = current_scope;
3529 else
3531 if (!objc_diagnose_private_ivar (id))
3533 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3534 if (const char *suggestion = guessed_id.suggestion ())
3536 gcc_rich_location richloc (loc);
3537 richloc.add_fixit_replace (suggestion);
3538 error_at (&richloc,
3539 "%qE undeclared (first use in this function);"
3540 " did you mean %qs?",
3541 id, suggestion);
3543 else
3544 error_at (loc, "%qE undeclared (first use in this function)", id);
3546 if (!already)
3548 inform (loc, "each undeclared identifier is reported only"
3549 " once for each function it appears in");
3550 already = true;
3553 /* If we are parsing old-style parameter decls, current_function_decl
3554 will be nonnull but current_function_scope will be null. */
3555 scope = current_function_scope ? current_function_scope : current_scope;
3557 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3558 UNKNOWN_LOCATION);
3561 /* Subroutine of lookup_label, declare_label, define_label: construct a
3562 LABEL_DECL with all the proper frills. Also create a struct
3563 c_label_vars initialized for the current scope. */
3565 static tree
3566 make_label (location_t location, tree name, bool defining,
3567 struct c_label_vars **p_label_vars)
3569 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3570 DECL_CONTEXT (label) = current_function_decl;
3571 SET_DECL_MODE (label, VOIDmode);
3573 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3574 label_vars->shadowed = NULL;
3575 set_spot_bindings (&label_vars->label_bindings, defining);
3576 label_vars->decls_in_scope = make_tree_vector ();
3577 label_vars->gotos = NULL;
3578 *p_label_vars = label_vars;
3580 return label;
3583 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3584 Create one if none exists so far for the current function.
3585 This is called when a label is used in a goto expression or
3586 has its address taken. */
3588 tree
3589 lookup_label (tree name)
3591 tree label;
3592 struct c_label_vars *label_vars;
3594 if (current_function_scope == 0)
3596 error ("label %qE referenced outside of any function", name);
3597 return NULL_TREE;
3600 /* Use a label already defined or ref'd with this name, but not if
3601 it is inherited from a containing function and wasn't declared
3602 using __label__. */
3603 label = I_LABEL_DECL (name);
3604 if (label && (DECL_CONTEXT (label) == current_function_decl
3605 || C_DECLARED_LABEL_FLAG (label)))
3607 /* If the label has only been declared, update its apparent
3608 location to point here, for better diagnostics if it
3609 turns out not to have been defined. */
3610 if (DECL_INITIAL (label) == NULL_TREE)
3611 DECL_SOURCE_LOCATION (label) = input_location;
3612 return label;
3615 /* No label binding for that identifier; make one. */
3616 label = make_label (input_location, name, false, &label_vars);
3618 /* Ordinary labels go in the current function scope. */
3619 bind_label (name, label, current_function_scope, label_vars);
3621 return label;
3624 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3625 to LABEL. */
3627 static void
3628 warn_about_goto (location_t goto_loc, tree label, tree decl)
3630 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3631 error_at (goto_loc,
3632 "jump into scope of identifier with variably modified type");
3633 else
3634 warning_at (goto_loc, OPT_Wjump_misses_init,
3635 "jump skips variable initialization");
3636 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3637 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3640 /* Look up a label because of a goto statement. This is like
3641 lookup_label, but also issues any appropriate warnings. */
3643 tree
3644 lookup_label_for_goto (location_t loc, tree name)
3646 tree label;
3647 struct c_label_vars *label_vars;
3648 unsigned int ix;
3649 tree decl;
3651 label = lookup_label (name);
3652 if (label == NULL_TREE)
3653 return NULL_TREE;
3655 /* If we are jumping to a different function, we can't issue any
3656 useful warnings. */
3657 if (DECL_CONTEXT (label) != current_function_decl)
3659 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3660 return label;
3663 label_vars = I_LABEL_BINDING (name)->u.label;
3665 /* If the label has not yet been defined, then push this goto on a
3666 list for possible later warnings. */
3667 if (label_vars->label_bindings.scope == NULL)
3669 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3671 g->loc = loc;
3672 set_spot_bindings (&g->goto_bindings, true);
3673 vec_safe_push (label_vars->gotos, g);
3674 return label;
3677 /* If there are any decls in label_vars->decls_in_scope, then this
3678 goto has missed the declaration of the decl. This happens for a
3679 case like
3680 int i = 1;
3681 lab:
3683 goto lab;
3684 Issue a warning or error. */
3685 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3686 warn_about_goto (loc, label, decl);
3688 if (label_vars->label_bindings.left_stmt_expr)
3690 error_at (loc, "jump into statement expression");
3691 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3694 return label;
3697 /* Make a label named NAME in the current function, shadowing silently
3698 any that may be inherited from containing functions or containing
3699 scopes. This is called for __label__ declarations. */
3701 tree
3702 declare_label (tree name)
3704 struct c_binding *b = I_LABEL_BINDING (name);
3705 tree label;
3706 struct c_label_vars *label_vars;
3708 /* Check to make sure that the label hasn't already been declared
3709 at this scope */
3710 if (b && B_IN_CURRENT_SCOPE (b))
3712 auto_diagnostic_group d;
3713 error ("duplicate label declaration %qE", name);
3714 locate_old_decl (b->decl);
3716 /* Just use the previous declaration. */
3717 return b->decl;
3720 label = make_label (input_location, name, false, &label_vars);
3721 C_DECLARED_LABEL_FLAG (label) = 1;
3723 /* Declared labels go in the current scope. */
3724 bind_label (name, label, current_scope, label_vars);
3726 return label;
3729 /* When we define a label, issue any appropriate warnings if there are
3730 any gotos earlier in the function which jump to this label. */
3732 static void
3733 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3735 unsigned int ix;
3736 struct c_goto_bindings *g;
3738 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3740 struct c_binding *b;
3741 struct c_scope *scope;
3743 /* We have a goto to this label. The goto is going forward. In
3744 g->scope, the goto is going to skip any binding which was
3745 defined after g->bindings_in_scope. */
3746 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3748 for (b = g->goto_bindings.scope->bindings;
3749 b != g->goto_bindings.bindings_in_scope;
3750 b = b->prev)
3752 if (decl_jump_unsafe (b->decl))
3753 warn_about_goto (g->loc, label, b->decl);
3757 /* We also need to warn about decls defined in any scopes
3758 between the scope of the label and the scope of the goto. */
3759 for (scope = label_vars->label_bindings.scope;
3760 scope != g->goto_bindings.scope;
3761 scope = scope->outer)
3763 gcc_assert (scope != NULL);
3764 if (scope->has_jump_unsafe_decl)
3766 if (scope == label_vars->label_bindings.scope)
3767 b = label_vars->label_bindings.bindings_in_scope;
3768 else
3769 b = scope->bindings;
3770 for (; b != NULL; b = b->prev)
3772 if (decl_jump_unsafe (b->decl))
3773 warn_about_goto (g->loc, label, b->decl);
3778 if (g->goto_bindings.stmt_exprs > 0)
3780 error_at (g->loc, "jump into statement expression");
3781 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3782 label);
3786 /* Now that the label is defined, we will issue warnings about
3787 subsequent gotos to this label when we see them. */
3788 vec_safe_truncate (label_vars->gotos, 0);
3789 label_vars->gotos = NULL;
3792 /* Define a label, specifying the location in the source file.
3793 Return the LABEL_DECL node for the label, if the definition is valid.
3794 Otherwise return NULL_TREE. */
3796 tree
3797 define_label (location_t location, tree name)
3799 /* Find any preexisting label with this name. It is an error
3800 if that label has already been defined in this function, or
3801 if there is a containing function with a declared label with
3802 the same name. */
3803 tree label = I_LABEL_DECL (name);
3805 if (label
3806 && ((DECL_CONTEXT (label) == current_function_decl
3807 && DECL_INITIAL (label) != NULL_TREE)
3808 || (DECL_CONTEXT (label) != current_function_decl
3809 && C_DECLARED_LABEL_FLAG (label))))
3811 auto_diagnostic_group d;
3812 error_at (location, "duplicate label %qD", label);
3813 locate_old_decl (label);
3814 return NULL_TREE;
3816 else if (label && DECL_CONTEXT (label) == current_function_decl)
3818 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3820 /* The label has been used or declared already in this function,
3821 but not defined. Update its location to point to this
3822 definition. */
3823 DECL_SOURCE_LOCATION (label) = location;
3824 set_spot_bindings (&label_vars->label_bindings, true);
3826 /* Issue warnings as required about any goto statements from
3827 earlier in the function. */
3828 check_earlier_gotos (label, label_vars);
3830 else
3832 struct c_label_vars *label_vars;
3834 /* No label binding for that identifier; make one. */
3835 label = make_label (location, name, true, &label_vars);
3837 /* Ordinary labels go in the current function scope. */
3838 bind_label (name, label, current_function_scope, label_vars);
3841 if (!in_system_header_at (input_location) && lookup_name (name))
3842 warning_at (location, OPT_Wtraditional,
3843 "traditional C lacks a separate namespace "
3844 "for labels, identifier %qE conflicts", name);
3846 /* Mark label as having been defined. */
3847 DECL_INITIAL (label) = error_mark_node;
3848 return label;
3851 /* Get the bindings for a new switch statement. This is used to issue
3852 warnings as appropriate for jumps from the switch to case or
3853 default labels. */
3855 struct c_spot_bindings *
3856 c_get_switch_bindings (void)
3858 struct c_spot_bindings *switch_bindings;
3860 switch_bindings = XNEW (struct c_spot_bindings);
3861 set_spot_bindings (switch_bindings, true);
3862 return switch_bindings;
3865 void
3866 c_release_switch_bindings (struct c_spot_bindings *bindings)
3868 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3869 XDELETE (bindings);
3872 /* This is called at the point of a case or default label to issue
3873 warnings about decls as needed. It returns true if it found an
3874 error, not just a warning. */
3876 bool
3877 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3878 location_t switch_loc, location_t case_loc)
3880 bool saw_error;
3881 struct c_scope *scope;
3883 saw_error = false;
3884 for (scope = current_scope;
3885 scope != switch_bindings->scope;
3886 scope = scope->outer)
3888 struct c_binding *b;
3890 gcc_assert (scope != NULL);
3892 if (!scope->has_jump_unsafe_decl)
3893 continue;
3895 for (b = scope->bindings; b != NULL; b = b->prev)
3897 if (decl_jump_unsafe (b->decl))
3899 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3901 saw_error = true;
3902 error_at (case_loc,
3903 ("switch jumps into scope of identifier with "
3904 "variably modified type"));
3906 else
3907 warning_at (case_loc, OPT_Wjump_misses_init,
3908 "switch jumps over variable initialization");
3909 inform (switch_loc, "switch starts here");
3910 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3911 b->decl);
3916 if (switch_bindings->stmt_exprs > 0)
3918 saw_error = true;
3919 error_at (case_loc, "switch jumps into statement expression");
3920 inform (switch_loc, "switch starts here");
3923 return saw_error;
3926 /* Given NAME, an IDENTIFIER_NODE,
3927 return the structure (or union or enum) definition for that name.
3928 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3929 CODE says which kind of type the caller wants;
3930 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3931 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3932 location where the tag was defined.
3933 If the wrong kind of type is found, an error is reported. */
3935 static tree
3936 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3937 location_t *ploc)
3939 struct c_binding *b = I_TAG_BINDING (name);
3940 bool thislevel = false;
3942 if (!b || !b->decl)
3943 return NULL_TREE;
3945 /* We only care about whether it's in this level if
3946 thislevel_only was set or it might be a type clash. */
3947 if (thislevel_only || TREE_CODE (b->decl) != code)
3949 /* For our purposes, a tag in the external scope is the same as
3950 a tag in the file scope. (Primarily relevant to Objective-C
3951 and its builtin structure tags, which get pushed before the
3952 file scope is created.) */
3953 if (B_IN_CURRENT_SCOPE (b)
3954 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3955 thislevel = true;
3958 if (thislevel_only && !thislevel)
3959 return NULL_TREE;
3961 if (TREE_CODE (b->decl) != code)
3963 /* Definition isn't the kind we were looking for. */
3964 pending_invalid_xref = name;
3965 pending_invalid_xref_location = input_location;
3967 /* If in the same binding level as a declaration as a tag
3968 of a different type, this must not be allowed to
3969 shadow that tag, so give the error immediately.
3970 (For example, "struct foo; union foo;" is invalid.) */
3971 if (thislevel)
3972 pending_xref_error ();
3975 if (ploc != NULL)
3976 *ploc = b->locus;
3978 return b->decl;
3981 /* Return true if a definition exists for NAME with code CODE. */
3983 bool
3984 tag_exists_p (enum tree_code code, tree name)
3986 struct c_binding *b = I_TAG_BINDING (name);
3988 if (b == NULL || b->decl == NULL_TREE)
3989 return false;
3990 return TREE_CODE (b->decl) == code;
3993 /* Print an error message now
3994 for a recent invalid struct, union or enum cross reference.
3995 We don't print them immediately because they are not invalid
3996 when used in the `struct foo;' construct for shadowing. */
3998 void
3999 pending_xref_error (void)
4001 if (pending_invalid_xref != NULL_TREE)
4002 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4003 pending_invalid_xref);
4004 pending_invalid_xref = NULL_TREE;
4008 /* Look up NAME in the current scope and its superiors
4009 in the namespace of variables, functions and typedefs.
4010 Return a ..._DECL node of some kind representing its definition,
4011 or return NULL_TREE if it is undefined. */
4013 tree
4014 lookup_name (tree name)
4016 struct c_binding *b = I_SYMBOL_BINDING (name);
4017 if (b && !b->invisible)
4019 maybe_record_typedef_use (b->decl);
4020 return b->decl;
4022 return NULL_TREE;
4025 /* Similar to `lookup_name' but look only at the indicated scope. */
4027 static tree
4028 lookup_name_in_scope (tree name, struct c_scope *scope)
4030 struct c_binding *b;
4032 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4033 if (B_IN_SCOPE (b, scope))
4034 return b->decl;
4035 return NULL_TREE;
4038 /* Look for the closest match for NAME within the currently valid
4039 scopes.
4041 This finds the identifier with the lowest Levenshtein distance to
4042 NAME. If there are multiple candidates with equal minimal distance,
4043 the first one found is returned. Scopes are searched from innermost
4044 outwards, and within a scope in reverse order of declaration, thus
4045 benefiting candidates "near" to the current scope.
4047 The function also looks for similar macro names to NAME, since a
4048 misspelled macro name will not be expanded, and hence looks like an
4049 identifier to the C frontend.
4051 It also looks for start_typename keywords, to detect "singed" vs "signed"
4052 typos.
4054 Use LOC for any deferred diagnostics. */
4056 name_hint
4057 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4059 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4061 /* First, try some well-known names in the C standard library, in case
4062 the user forgot a #include. */
4063 const char *header_hint
4064 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4066 if (header_hint)
4067 return name_hint (NULL,
4068 new suggest_missing_header (loc,
4069 IDENTIFIER_POINTER (name),
4070 header_hint));
4072 /* Only suggest names reserved for the implementation if NAME begins
4073 with an underscore. */
4074 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4076 best_match<tree, tree> bm (name);
4078 /* Look within currently valid scopes. */
4079 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4080 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4082 if (!binding->id || binding->invisible)
4083 continue;
4084 if (binding->decl == error_mark_node)
4085 continue;
4086 /* Don't use bindings from implicitly declared functions,
4087 as they were likely misspellings themselves. */
4088 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4089 if (C_DECL_IMPLICIT (binding->decl))
4090 continue;
4091 /* Don't suggest names that are reserved for use by the
4092 implementation, unless NAME began with an underscore. */
4093 if (!consider_implementation_names)
4095 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4096 if (name_reserved_for_implementation_p (suggestion_str))
4097 continue;
4099 switch (kind)
4101 case FUZZY_LOOKUP_TYPENAME:
4102 if (TREE_CODE (binding->decl) != TYPE_DECL)
4103 continue;
4104 break;
4106 case FUZZY_LOOKUP_FUNCTION_NAME:
4107 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4109 /* Allow function pointers. */
4110 if ((VAR_P (binding->decl)
4111 || TREE_CODE (binding->decl) == PARM_DECL)
4112 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4113 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4114 == FUNCTION_TYPE))
4115 break;
4116 continue;
4118 break;
4120 default:
4121 break;
4123 bm.consider (binding->id);
4126 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4128 x = SOME_OTHER_MACRO (y);
4129 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4130 as a misspelled identifier.
4132 Use the best distance so far so that a candidate is only set if
4133 a macro is better than anything so far. This allows early rejection
4134 (without calculating the edit distance) of macro names that must have
4135 distance >= bm.get_best_distance (), and means that we only get a
4136 non-NULL result for best_macro_match if it's better than any of
4137 the identifiers already checked, which avoids needless creation
4138 of identifiers for macro hashnodes. */
4139 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4140 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4141 /* If a macro is the closest so far to NAME, use it, creating an
4142 identifier tree node for it. */
4143 if (best_macro)
4145 const char *id = (const char *)best_macro->ident.str;
4146 tree macro_as_identifier
4147 = get_identifier_with_length (id, best_macro->ident.len);
4148 bm.set_best_so_far (macro_as_identifier,
4149 bmm.get_best_distance (),
4150 bmm.get_best_candidate_length ());
4153 /* Try the "start_typename" keywords to detect
4154 "singed" vs "signed" typos. */
4155 if (kind == FUZZY_LOOKUP_TYPENAME)
4157 for (unsigned i = 0; i < num_c_common_reswords; i++)
4159 const c_common_resword *resword = &c_common_reswords[i];
4160 if (!c_keyword_starts_typename (resword->rid))
4161 continue;
4162 tree resword_identifier = ridpointers [resword->rid];
4163 if (!resword_identifier)
4164 continue;
4165 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4166 bm.consider (resword_identifier);
4170 tree best = bm.get_best_meaningful_candidate ();
4171 if (best)
4172 return name_hint (IDENTIFIER_POINTER (best), NULL);
4173 else
4174 return name_hint (NULL, NULL);
4178 /* Create the predefined scalar types of C,
4179 and some nodes representing standard constants (0, 1, (void *) 0).
4180 Initialize the global scope.
4181 Make definitions for built-in primitive functions. */
4183 void
4184 c_init_decl_processing (void)
4186 location_t save_loc = input_location;
4188 /* Initialize reserved words for parser. */
4189 c_parse_init ();
4191 current_function_decl = NULL_TREE;
4193 gcc_obstack_init (&parser_obstack);
4195 /* Make the externals scope. */
4196 push_scope ();
4197 external_scope = current_scope;
4199 /* Declarations from c_common_nodes_and_builtins must not be associated
4200 with this input file, lest we get differences between using and not
4201 using preprocessed headers. */
4202 input_location = BUILTINS_LOCATION;
4204 c_common_nodes_and_builtins ();
4206 /* In C, comparisons and TRUTH_* expressions have type int. */
4207 truthvalue_type_node = integer_type_node;
4208 truthvalue_true_node = integer_one_node;
4209 truthvalue_false_node = integer_zero_node;
4211 /* Even in C99, which has a real boolean type. */
4212 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4213 boolean_type_node));
4215 input_location = save_loc;
4217 make_fname_decl = c_make_fname_decl;
4218 start_fname_decls ();
4221 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4222 give the decl, NAME is the initialization string and TYPE_DEP
4223 indicates whether NAME depended on the type of the function. As we
4224 don't yet implement delayed emission of static data, we mark the
4225 decl as emitted so it is not placed in the output. Anything using
4226 it must therefore pull out the STRING_CST initializer directly.
4227 FIXME. */
4229 static tree
4230 c_make_fname_decl (location_t loc, tree id, int type_dep)
4232 const char *name = fname_as_string (type_dep);
4233 tree decl, type, init;
4234 size_t length = strlen (name);
4236 type = build_array_type (char_type_node,
4237 build_index_type (size_int (length)));
4238 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4240 decl = build_decl (loc, VAR_DECL, id, type);
4242 TREE_STATIC (decl) = 1;
4243 TREE_READONLY (decl) = 1;
4244 DECL_ARTIFICIAL (decl) = 1;
4246 init = build_string (length + 1, name);
4247 free (CONST_CAST (char *, name));
4248 TREE_TYPE (init) = type;
4249 DECL_INITIAL (decl) = init;
4251 TREE_USED (decl) = 1;
4253 if (current_function_decl
4254 /* For invalid programs like this:
4256 void foo()
4257 const char* p = __FUNCTION__;
4259 the __FUNCTION__ is believed to appear in K&R style function
4260 parameter declarator. In that case we still don't have
4261 function_scope. */
4262 && current_function_scope)
4264 DECL_CONTEXT (decl) = current_function_decl;
4265 bind (id, decl, current_function_scope,
4266 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4269 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4271 return decl;
4274 tree
4275 c_builtin_function (tree decl)
4277 tree type = TREE_TYPE (decl);
4278 tree id = DECL_NAME (decl);
4280 const char *name = IDENTIFIER_POINTER (id);
4281 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4283 /* Should never be called on a symbol with a preexisting meaning. */
4284 gcc_assert (!I_SYMBOL_BINDING (id));
4286 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4287 UNKNOWN_LOCATION);
4289 /* Builtins in the implementation namespace are made visible without
4290 needing to be explicitly declared. See push_file_scope. */
4291 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4293 DECL_CHAIN (decl) = visible_builtins;
4294 visible_builtins = decl;
4297 return decl;
4300 tree
4301 c_builtin_function_ext_scope (tree decl)
4303 tree type = TREE_TYPE (decl);
4304 tree id = DECL_NAME (decl);
4306 const char *name = IDENTIFIER_POINTER (id);
4307 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4309 if (external_scope)
4310 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4311 UNKNOWN_LOCATION);
4313 /* Builtins in the implementation namespace are made visible without
4314 needing to be explicitly declared. See push_file_scope. */
4315 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4317 DECL_CHAIN (decl) = visible_builtins;
4318 visible_builtins = decl;
4321 return decl;
4324 /* Called when a declaration is seen that contains no names to declare.
4325 If its type is a reference to a structure, union or enum inherited
4326 from a containing scope, shadow that tag name for the current scope
4327 with a forward reference.
4328 If its type defines a new named structure or union
4329 or defines an enum, it is valid but we need not do anything here.
4330 Otherwise, it is an error. */
4332 void
4333 shadow_tag (const struct c_declspecs *declspecs)
4335 shadow_tag_warned (declspecs, 0);
4338 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4339 but no pedwarn. */
4340 void
4341 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4343 bool found_tag = false;
4345 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4347 tree value = declspecs->type;
4348 enum tree_code code = TREE_CODE (value);
4350 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4351 /* Used to test also that TYPE_SIZE (value) != 0.
4352 That caused warning for `struct foo;' at top level in the file. */
4354 tree name = TYPE_NAME (value);
4355 tree t;
4357 found_tag = true;
4359 if (declspecs->restrict_p)
4361 error ("invalid use of %<restrict%>");
4362 warned = 1;
4365 if (name == NULL_TREE)
4367 if (warned != 1 && code != ENUMERAL_TYPE)
4368 /* Empty unnamed enum OK */
4370 pedwarn (input_location, 0,
4371 "unnamed struct/union that defines no instances");
4372 warned = 1;
4375 else if (declspecs->typespec_kind != ctsk_tagdef
4376 && declspecs->typespec_kind != ctsk_tagfirstref
4377 && declspecs->storage_class != csc_none)
4379 if (warned != 1)
4380 pedwarn (input_location, 0,
4381 "empty declaration with storage class specifier "
4382 "does not redeclare tag");
4383 warned = 1;
4384 pending_xref_error ();
4386 else if (declspecs->typespec_kind != ctsk_tagdef
4387 && declspecs->typespec_kind != ctsk_tagfirstref
4388 && (declspecs->const_p
4389 || declspecs->volatile_p
4390 || declspecs->atomic_p
4391 || declspecs->restrict_p
4392 || declspecs->address_space))
4394 if (warned != 1)
4395 pedwarn (input_location, 0,
4396 "empty declaration with type qualifier "
4397 "does not redeclare tag");
4398 warned = 1;
4399 pending_xref_error ();
4401 else if (declspecs->typespec_kind != ctsk_tagdef
4402 && declspecs->typespec_kind != ctsk_tagfirstref
4403 && declspecs->alignas_p)
4405 if (warned != 1)
4406 pedwarn (input_location, 0,
4407 "empty declaration with %<_Alignas%> "
4408 "does not redeclare tag");
4409 warned = 1;
4410 pending_xref_error ();
4412 else
4414 pending_invalid_xref = NULL_TREE;
4415 t = lookup_tag (code, name, true, NULL);
4417 if (t == NULL_TREE)
4419 t = make_node (code);
4420 pushtag (input_location, name, t);
4424 else
4426 if (warned != 1 && !in_system_header_at (input_location))
4428 pedwarn (input_location, 0,
4429 "useless type name in empty declaration");
4430 warned = 1;
4434 else if (warned != 1 && !in_system_header_at (input_location)
4435 && declspecs->typedef_p)
4437 pedwarn (input_location, 0, "useless type name in empty declaration");
4438 warned = 1;
4441 pending_invalid_xref = NULL_TREE;
4443 if (declspecs->inline_p)
4445 error ("%<inline%> in empty declaration");
4446 warned = 1;
4449 if (declspecs->noreturn_p)
4451 error ("%<_Noreturn%> in empty declaration");
4452 warned = 1;
4455 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4457 error ("%<auto%> in file-scope empty declaration");
4458 warned = 1;
4461 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4463 error ("%<register%> in file-scope empty declaration");
4464 warned = 1;
4467 if (!warned && !in_system_header_at (input_location)
4468 && declspecs->storage_class != csc_none)
4470 warning (0, "useless storage class specifier in empty declaration");
4471 warned = 2;
4474 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4476 warning (0, "useless %qs in empty declaration",
4477 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4478 warned = 2;
4481 if (!warned
4482 && !in_system_header_at (input_location)
4483 && (declspecs->const_p
4484 || declspecs->volatile_p
4485 || declspecs->atomic_p
4486 || declspecs->restrict_p
4487 || declspecs->address_space))
4489 warning (0, "useless type qualifier in empty declaration");
4490 warned = 2;
4493 if (!warned && !in_system_header_at (input_location)
4494 && declspecs->alignas_p)
4496 warning (0, "useless %<_Alignas%> in empty declaration");
4497 warned = 2;
4500 if (warned != 1)
4502 if (!found_tag)
4503 pedwarn (input_location, 0, "empty declaration");
4508 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4509 bits. SPECS represents declaration specifiers that the grammar
4510 only permits to contain type qualifiers and attributes. */
4513 quals_from_declspecs (const struct c_declspecs *specs)
4515 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4516 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4517 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4518 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4519 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4520 gcc_assert (!specs->type
4521 && !specs->decl_attr
4522 && specs->typespec_word == cts_none
4523 && specs->storage_class == csc_none
4524 && !specs->typedef_p
4525 && !specs->explicit_signed_p
4526 && !specs->deprecated_p
4527 && !specs->long_p
4528 && !specs->long_long_p
4529 && !specs->short_p
4530 && !specs->signed_p
4531 && !specs->unsigned_p
4532 && !specs->complex_p
4533 && !specs->inline_p
4534 && !specs->noreturn_p
4535 && !specs->thread_p);
4536 return quals;
4539 /* Construct an array declarator. LOC is the location of the
4540 beginning of the array (usually the opening brace). EXPR is the
4541 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4542 inside the [] (to be applied to the pointer to which a parameter
4543 array is converted). STATIC_P is true if "static" is inside the
4544 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4545 VLA of unspecified length which is nevertheless a complete type,
4546 false otherwise. The field for the contained declarator is left to
4547 be filled in by set_array_declarator_inner. */
4549 struct c_declarator *
4550 build_array_declarator (location_t loc,
4551 tree expr, struct c_declspecs *quals, bool static_p,
4552 bool vla_unspec_p)
4554 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4555 struct c_declarator);
4556 declarator->id_loc = loc;
4557 declarator->kind = cdk_array;
4558 declarator->declarator = 0;
4559 declarator->u.array.dimen = expr;
4560 if (quals)
4562 declarator->u.array.attrs = quals->attrs;
4563 declarator->u.array.quals = quals_from_declspecs (quals);
4565 else
4567 declarator->u.array.attrs = NULL_TREE;
4568 declarator->u.array.quals = 0;
4570 declarator->u.array.static_p = static_p;
4571 declarator->u.array.vla_unspec_p = vla_unspec_p;
4572 if (static_p || quals != NULL)
4573 pedwarn_c90 (loc, OPT_Wpedantic,
4574 "ISO C90 does not support %<static%> or type "
4575 "qualifiers in parameter array declarators");
4576 if (vla_unspec_p)
4577 pedwarn_c90 (loc, OPT_Wpedantic,
4578 "ISO C90 does not support %<[*]%> array declarators");
4579 if (vla_unspec_p)
4581 if (!current_scope->parm_flag)
4583 /* C99 6.7.5.2p4 */
4584 error_at (loc, "%<[*]%> not allowed in other than "
4585 "function prototype scope");
4586 declarator->u.array.vla_unspec_p = false;
4587 return NULL;
4589 current_scope->had_vla_unspec = true;
4591 return declarator;
4594 /* Set the contained declarator of an array declarator. DECL is the
4595 declarator, as constructed by build_array_declarator; INNER is what
4596 appears on the left of the []. */
4598 struct c_declarator *
4599 set_array_declarator_inner (struct c_declarator *decl,
4600 struct c_declarator *inner)
4602 decl->declarator = inner;
4603 return decl;
4606 /* INIT is a constructor that forms DECL's initializer. If the final
4607 element initializes a flexible array field, add the size of that
4608 initializer to DECL's size. */
4610 static void
4611 add_flexible_array_elts_to_size (tree decl, tree init)
4613 tree elt, type;
4615 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4616 return;
4618 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4619 type = TREE_TYPE (elt);
4620 if (TREE_CODE (type) == ARRAY_TYPE
4621 && TYPE_SIZE (type) == NULL_TREE
4622 && TYPE_DOMAIN (type) != NULL_TREE
4623 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4625 complete_array_type (&type, elt, false);
4626 DECL_SIZE (decl)
4627 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4628 DECL_SIZE_UNIT (decl)
4629 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4633 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4634 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4635 before the type name, and set *EXPR_CONST_OPERANDS, if
4636 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4637 appear in a constant expression. */
4639 tree
4640 groktypename (struct c_type_name *type_name, tree *expr,
4641 bool *expr_const_operands)
4643 tree type;
4644 tree attrs = type_name->specs->attrs;
4646 type_name->specs->attrs = NULL_TREE;
4648 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4649 false, NULL, &attrs, expr, expr_const_operands,
4650 DEPRECATED_NORMAL);
4652 /* Apply attributes. */
4653 decl_attributes (&type, attrs, 0);
4655 return type;
4658 /* Wrapper for decl_attributes that adds some implicit attributes
4659 to VAR_DECLs or FUNCTION_DECLs. */
4661 static tree
4662 c_decl_attributes (tree *node, tree attributes, int flags)
4664 /* Add implicit "omp declare target" attribute if requested. */
4665 if (current_omp_declare_target_attribute
4666 && ((VAR_P (*node) && is_global_var (*node))
4667 || TREE_CODE (*node) == FUNCTION_DECL))
4669 if (VAR_P (*node)
4670 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4671 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4672 NULL_TREE, attributes);
4673 else
4674 attributes = tree_cons (get_identifier ("omp declare target"),
4675 NULL_TREE, attributes);
4678 /* Look up the current declaration with all the attributes merged
4679 so far so that attributes on the current declaration that's
4680 about to be pushed that conflict with the former can be detected,
4681 diagnosed, and rejected as appropriate. */
4682 tree last_decl = lookup_name (DECL_NAME (*node));
4683 if (!last_decl)
4684 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4686 return decl_attributes (node, attributes, flags, last_decl);
4690 /* Decode a declarator in an ordinary declaration or data definition.
4691 This is called as soon as the type information and variable name
4692 have been parsed, before parsing the initializer if any.
4693 Here we create the ..._DECL node, fill in its type,
4694 and put it on the list of decls for the current context.
4695 The ..._DECL node is returned as the value.
4697 Exception: for arrays where the length is not specified,
4698 the type is left null, to be filled in by `finish_decl'.
4700 Function definitions do not come here; they go to start_function
4701 instead. However, external and forward declarations of functions
4702 do go through here. Structure field declarations are done by
4703 grokfield and not through here. */
4705 tree
4706 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4707 bool initialized, tree attributes)
4709 tree decl;
4710 tree tem;
4711 tree expr = NULL_TREE;
4712 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4714 /* An object declared as __attribute__((deprecated)) suppresses
4715 warnings of uses of other deprecated items. */
4716 if (lookup_attribute ("deprecated", attributes))
4717 deprecated_state = DEPRECATED_SUPPRESS;
4719 decl = grokdeclarator (declarator, declspecs,
4720 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4721 deprecated_state);
4722 if (!decl || decl == error_mark_node)
4723 return NULL_TREE;
4725 if (expr)
4726 add_stmt (fold_convert (void_type_node, expr));
4728 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
4729 && TREE_PUBLIC (decl))
4730 warning (OPT_Wmain, "%q+D is usually a function", decl);
4732 if (initialized)
4733 /* Is it valid for this decl to have an initializer at all?
4734 If not, set INITIALIZED to zero, which will indirectly
4735 tell 'finish_decl' to ignore the initializer once it is parsed. */
4736 switch (TREE_CODE (decl))
4738 case TYPE_DECL:
4739 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4740 initialized = false;
4741 break;
4743 case FUNCTION_DECL:
4744 error ("function %qD is initialized like a variable", decl);
4745 initialized = false;
4746 break;
4748 case PARM_DECL:
4749 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4750 error ("parameter %qD is initialized", decl);
4751 initialized = false;
4752 break;
4754 default:
4755 /* Don't allow initializations for incomplete types except for
4756 arrays which might be completed by the initialization. */
4758 /* This can happen if the array size is an undefined macro.
4759 We already gave a warning, so we don't need another one. */
4760 if (TREE_TYPE (decl) == error_mark_node)
4761 initialized = false;
4762 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4764 /* A complete type is ok if size is fixed. */
4766 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4767 || C_DECL_VARIABLE_SIZE (decl))
4769 error ("variable-sized object may not be initialized");
4770 initialized = false;
4773 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4775 error ("variable %qD has initializer but incomplete type", decl);
4776 initialized = false;
4778 else if (C_DECL_VARIABLE_SIZE (decl))
4780 /* Although C99 is unclear about whether incomplete arrays
4781 of VLAs themselves count as VLAs, it does not make
4782 sense to permit them to be initialized given that
4783 ordinary VLAs may not be initialized. */
4784 error ("variable-sized object may not be initialized");
4785 initialized = false;
4789 if (initialized)
4791 if (current_scope == file_scope)
4792 TREE_STATIC (decl) = 1;
4794 /* Tell 'pushdecl' this is an initialized decl
4795 even though we don't yet have the initializer expression.
4796 Also tell 'finish_decl' it may store the real initializer. */
4797 DECL_INITIAL (decl) = error_mark_node;
4800 /* If this is a function declaration, write a record describing it to the
4801 prototypes file (if requested). */
4803 if (TREE_CODE (decl) == FUNCTION_DECL)
4804 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4806 /* ANSI specifies that a tentative definition which is not merged with
4807 a non-tentative definition behaves exactly like a definition with an
4808 initializer equal to zero. (Section 3.7.2)
4810 -fno-common gives strict ANSI behavior, though this tends to break
4811 a large body of code that grew up without this rule.
4813 Thread-local variables are never common, since there's no entrenched
4814 body of code to break, and it allows more efficient variable references
4815 in the presence of dynamic linking. */
4817 if (VAR_P (decl)
4818 && !initialized
4819 && TREE_PUBLIC (decl)
4820 && !DECL_THREAD_LOCAL_P (decl)
4821 && !flag_no_common)
4822 DECL_COMMON (decl) = 1;
4824 /* Set attributes here so if duplicate decl, will have proper attributes. */
4825 c_decl_attributes (&decl, attributes, 0);
4827 /* Handle gnu_inline attribute. */
4828 if (declspecs->inline_p
4829 && !flag_gnu89_inline
4830 && TREE_CODE (decl) == FUNCTION_DECL
4831 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4832 || current_function_decl))
4834 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4836 else if (declspecs->storage_class != csc_static)
4837 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4840 if (TREE_CODE (decl) == FUNCTION_DECL
4841 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4843 struct c_declarator *ce = declarator;
4845 if (ce->kind == cdk_pointer)
4846 ce = declarator->declarator;
4847 if (ce->kind == cdk_function)
4849 tree args = ce->u.arg_info->parms;
4850 for (; args; args = DECL_CHAIN (args))
4852 tree type = TREE_TYPE (args);
4853 if (type && INTEGRAL_TYPE_P (type)
4854 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4855 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4860 if (TREE_CODE (decl) == FUNCTION_DECL
4861 && DECL_DECLARED_INLINE_P (decl)
4862 && DECL_UNINLINABLE (decl)
4863 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4864 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4865 decl);
4867 /* C99 6.7.4p3: An inline definition of a function with external
4868 linkage shall not contain a definition of a modifiable object
4869 with static storage duration... */
4870 if (VAR_P (decl)
4871 && current_scope != file_scope
4872 && TREE_STATIC (decl)
4873 && !TREE_READONLY (decl)
4874 && DECL_DECLARED_INLINE_P (current_function_decl)
4875 && DECL_EXTERNAL (current_function_decl))
4876 record_inline_static (input_location, current_function_decl,
4877 decl, csi_modifiable);
4879 if (c_dialect_objc ()
4880 && VAR_OR_FUNCTION_DECL_P (decl))
4881 objc_check_global_decl (decl);
4883 /* Add this decl to the current scope.
4884 TEM may equal DECL or it may be a previous decl of the same name. */
4885 tem = pushdecl (decl);
4887 if (initialized && DECL_EXTERNAL (tem))
4889 DECL_EXTERNAL (tem) = 0;
4890 TREE_STATIC (tem) = 1;
4893 return tem;
4896 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4897 DECL or the non-array element type if DECL is an uninitialized array.
4898 If that type has a const member, diagnose this. */
4900 static void
4901 diagnose_uninitialized_cst_member (tree decl, tree type)
4903 tree field;
4904 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4906 tree field_type;
4907 if (TREE_CODE (field) != FIELD_DECL)
4908 continue;
4909 field_type = strip_array_types (TREE_TYPE (field));
4911 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4913 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4914 "uninitialized const member in %qT is invalid in C++",
4915 strip_array_types (TREE_TYPE (decl)));
4916 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4919 if (RECORD_OR_UNION_TYPE_P (field_type))
4920 diagnose_uninitialized_cst_member (decl, field_type);
4924 /* Finish processing of a declaration;
4925 install its initial value.
4926 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4927 If the length of an array type is not known before,
4928 it must be determined now, from the initial value, or it is an error.
4930 INIT_LOC is the location of the initial value. */
4932 void
4933 finish_decl (tree decl, location_t init_loc, tree init,
4934 tree origtype, tree asmspec_tree)
4936 tree type;
4937 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4938 const char *asmspec = 0;
4940 /* If a name was specified, get the string. */
4941 if (VAR_OR_FUNCTION_DECL_P (decl)
4942 && DECL_FILE_SCOPE_P (decl))
4943 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4944 if (asmspec_tree)
4945 asmspec = TREE_STRING_POINTER (asmspec_tree);
4947 if (VAR_P (decl)
4948 && TREE_STATIC (decl)
4949 && global_bindings_p ())
4950 /* So decl is a global variable. Record the types it uses
4951 so that we can decide later to emit debug info for them. */
4952 record_types_used_by_current_var_decl (decl);
4954 /* If `start_decl' didn't like having an initialization, ignore it now. */
4955 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4956 init = NULL_TREE;
4958 /* Don't crash if parm is initialized. */
4959 if (TREE_CODE (decl) == PARM_DECL)
4960 init = NULL_TREE;
4962 if (init)
4963 store_init_value (init_loc, decl, init, origtype);
4965 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4966 || TREE_CODE (decl) == FIELD_DECL))
4967 objc_check_decl (decl);
4969 type = TREE_TYPE (decl);
4971 /* Deduce size of array from initialization, if not already known. */
4972 if (TREE_CODE (type) == ARRAY_TYPE
4973 && TYPE_DOMAIN (type) == NULL_TREE
4974 && TREE_CODE (decl) != TYPE_DECL)
4976 bool do_default
4977 = (TREE_STATIC (decl)
4978 /* Even if pedantic, an external linkage array
4979 may have incomplete type at first. */
4980 ? pedantic && !TREE_PUBLIC (decl)
4981 : !DECL_EXTERNAL (decl));
4982 int failure
4983 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4984 do_default);
4986 /* Get the completed type made by complete_array_type. */
4987 type = TREE_TYPE (decl);
4989 switch (failure)
4991 case 1:
4992 error ("initializer fails to determine size of %q+D", decl);
4993 break;
4995 case 2:
4996 if (do_default)
4997 error ("array size missing in %q+D", decl);
4998 break;
5000 case 3:
5001 error ("zero or negative size array %q+D", decl);
5002 break;
5004 case 0:
5005 /* For global variables, update the copy of the type that
5006 exists in the binding. */
5007 if (TREE_PUBLIC (decl))
5009 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5010 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5011 b_ext = b_ext->shadowed;
5012 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5014 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5015 b_ext->u.type = composite_type (b_ext->u.type, type);
5016 else
5017 b_ext->u.type = type;
5020 break;
5022 default:
5023 gcc_unreachable ();
5026 if (DECL_INITIAL (decl))
5027 TREE_TYPE (DECL_INITIAL (decl)) = type;
5029 relayout_decl (decl);
5032 if (TREE_CODE (type) == ARRAY_TYPE
5033 && TYPE_STRING_FLAG (TREE_TYPE (type))
5034 && DECL_INITIAL (decl)
5035 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR)
5036 DECL_INITIAL (decl) = braced_list_to_string (type, DECL_INITIAL (decl));
5038 if (VAR_P (decl))
5040 if (init && TREE_CODE (init) == CONSTRUCTOR)
5041 add_flexible_array_elts_to_size (decl, init);
5043 complete_flexible_array_elts (DECL_INITIAL (decl));
5045 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5046 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5047 layout_decl (decl, 0);
5049 if (DECL_SIZE (decl) == NULL_TREE
5050 /* Don't give an error if we already gave one earlier. */
5051 && TREE_TYPE (decl) != error_mark_node
5052 && (TREE_STATIC (decl)
5053 /* A static variable with an incomplete type
5054 is an error if it is initialized.
5055 Also if it is not file scope.
5056 Otherwise, let it through, but if it is not `extern'
5057 then it may cause an error message later. */
5058 ? (DECL_INITIAL (decl) != NULL_TREE
5059 || !DECL_FILE_SCOPE_P (decl))
5060 /* An automatic variable with an incomplete type
5061 is an error. */
5062 : !DECL_EXTERNAL (decl)))
5064 error ("storage size of %q+D isn%'t known", decl);
5065 TREE_TYPE (decl) = error_mark_node;
5068 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5069 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5070 && DECL_SIZE (decl) == NULL_TREE
5071 && TREE_STATIC (decl))
5072 incomplete_record_decls.safe_push (decl);
5074 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5076 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5077 constant_expression_warning (DECL_SIZE (decl));
5078 else
5080 error ("storage size of %q+D isn%'t constant", decl);
5081 TREE_TYPE (decl) = error_mark_node;
5085 if (TREE_USED (type))
5087 TREE_USED (decl) = 1;
5088 DECL_READ_P (decl) = 1;
5092 /* If this is a function and an assembler name is specified, reset DECL_RTL
5093 so we can give it its new name. Also, update builtin_decl if it
5094 was a normal built-in. */
5095 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5097 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5098 set_builtin_user_assembler_name (decl, asmspec);
5099 set_user_assembler_name (decl, asmspec);
5102 /* If #pragma weak was used, mark the decl weak now. */
5103 maybe_apply_pragma_weak (decl);
5105 /* Output the assembler code and/or RTL code for variables and functions,
5106 unless the type is an undefined structure or union.
5107 If not, it will get done when the type is completed. */
5109 if (VAR_OR_FUNCTION_DECL_P (decl))
5111 /* Determine the ELF visibility. */
5112 if (TREE_PUBLIC (decl))
5113 c_determine_visibility (decl);
5115 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5116 if (c_dialect_objc ())
5117 objc_check_decl (decl);
5119 if (asmspec)
5121 /* If this is not a static variable, issue a warning.
5122 It doesn't make any sense to give an ASMSPEC for an
5123 ordinary, non-register local variable. Historically,
5124 GCC has accepted -- but ignored -- the ASMSPEC in
5125 this case. */
5126 if (!DECL_FILE_SCOPE_P (decl)
5127 && VAR_P (decl)
5128 && !C_DECL_REGISTER (decl)
5129 && !TREE_STATIC (decl))
5130 warning (0, "ignoring asm-specifier for non-static local "
5131 "variable %q+D", decl);
5132 else
5133 set_user_assembler_name (decl, asmspec);
5136 if (DECL_FILE_SCOPE_P (decl))
5138 if (DECL_INITIAL (decl) == NULL_TREE
5139 || DECL_INITIAL (decl) == error_mark_node)
5140 /* Don't output anything
5141 when a tentative file-scope definition is seen.
5142 But at end of compilation, do output code for them. */
5143 DECL_DEFER_OUTPUT (decl) = 1;
5144 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5145 DECL_HARD_REGISTER (decl) = 1;
5146 rest_of_decl_compilation (decl, true, 0);
5148 else
5150 /* In conjunction with an ASMSPEC, the `register'
5151 keyword indicates that we should place the variable
5152 in a particular register. */
5153 if (asmspec && C_DECL_REGISTER (decl))
5155 DECL_HARD_REGISTER (decl) = 1;
5156 /* This cannot be done for a structure with volatile
5157 fields, on which DECL_REGISTER will have been
5158 reset. */
5159 if (!DECL_REGISTER (decl))
5160 error ("cannot put object with volatile field into register");
5163 if (TREE_CODE (decl) != FUNCTION_DECL)
5165 /* If we're building a variable sized type, and we might be
5166 reachable other than via the top of the current binding
5167 level, then create a new BIND_EXPR so that we deallocate
5168 the object at the right time. */
5169 /* Note that DECL_SIZE can be null due to errors. */
5170 if (DECL_SIZE (decl)
5171 && !TREE_CONSTANT (DECL_SIZE (decl))
5172 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5174 tree bind;
5175 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5176 TREE_SIDE_EFFECTS (bind) = 1;
5177 add_stmt (bind);
5178 BIND_EXPR_BODY (bind) = push_stmt_list ();
5180 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5181 DECL_EXPR, decl));
5186 if (!DECL_FILE_SCOPE_P (decl))
5188 /* Recompute the RTL of a local array now
5189 if it used to be an incomplete type. */
5190 if (was_incomplete && !is_global_var (decl))
5192 /* If we used it already as memory, it must stay in memory. */
5193 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5194 /* If it's still incomplete now, no init will save it. */
5195 if (DECL_SIZE (decl) == NULL_TREE)
5196 DECL_INITIAL (decl) = NULL_TREE;
5201 if (TREE_CODE (decl) == TYPE_DECL)
5203 if (!DECL_FILE_SCOPE_P (decl)
5204 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5205 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5207 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5210 /* Install a cleanup (aka destructor) if one was given. */
5211 if (VAR_P (decl) && !TREE_STATIC (decl))
5213 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5214 if (attr)
5216 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5217 tree cleanup_decl = lookup_name (cleanup_id);
5218 tree cleanup;
5219 vec<tree, va_gc> *v;
5221 /* Build "cleanup(&decl)" for the destructor. */
5222 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5223 vec_alloc (v, 1);
5224 v->quick_push (cleanup);
5225 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5226 vNULL, cleanup_decl, v, NULL);
5227 vec_free (v);
5229 /* Don't warn about decl unused; the cleanup uses it. */
5230 TREE_USED (decl) = 1;
5231 TREE_USED (cleanup_decl) = 1;
5232 DECL_READ_P (decl) = 1;
5234 push_cleanup (decl, cleanup, false);
5238 if (warn_cxx_compat
5239 && VAR_P (decl)
5240 && !DECL_EXTERNAL (decl)
5241 && DECL_INITIAL (decl) == NULL_TREE)
5243 type = strip_array_types (type);
5244 if (TREE_READONLY (decl))
5245 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5246 "uninitialized const %qD is invalid in C++", decl);
5247 else if (RECORD_OR_UNION_TYPE_P (type)
5248 && C_TYPE_FIELDS_READONLY (type))
5249 diagnose_uninitialized_cst_member (decl, type);
5252 if (flag_openmp
5253 && VAR_P (decl)
5254 && lookup_attribute ("omp declare target implicit",
5255 DECL_ATTRIBUTES (decl)))
5257 DECL_ATTRIBUTES (decl)
5258 = remove_attribute ("omp declare target implicit",
5259 DECL_ATTRIBUTES (decl));
5260 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5261 error ("%q+D in declare target directive does not have mappable type",
5262 decl);
5263 else if (!lookup_attribute ("omp declare target",
5264 DECL_ATTRIBUTES (decl))
5265 && !lookup_attribute ("omp declare target link",
5266 DECL_ATTRIBUTES (decl)))
5267 DECL_ATTRIBUTES (decl)
5268 = tree_cons (get_identifier ("omp declare target"),
5269 NULL_TREE, DECL_ATTRIBUTES (decl));
5272 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5275 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5276 EXPR is NULL or a pointer to an expression that needs to be
5277 evaluated for the side effects of array size expressions in the
5278 parameters. */
5280 tree
5281 grokparm (const struct c_parm *parm, tree *expr)
5283 tree attrs = parm->attrs;
5284 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5285 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5287 decl_attributes (&decl, attrs, 0);
5289 return decl;
5292 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5293 and push that on the current scope. EXPR is a pointer to an
5294 expression that needs to be evaluated for the side effects of array
5295 size expressions in the parameters. */
5297 void
5298 push_parm_decl (const struct c_parm *parm, tree *expr)
5300 tree attrs = parm->attrs;
5301 tree decl;
5303 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5304 &attrs, expr, NULL, DEPRECATED_NORMAL);
5305 if (decl && DECL_P (decl))
5306 DECL_SOURCE_LOCATION (decl) = parm->loc;
5307 decl_attributes (&decl, attrs, 0);
5309 decl = pushdecl (decl);
5311 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5314 /* Mark all the parameter declarations to date as forward decls.
5315 Also diagnose use of this extension. */
5317 void
5318 mark_forward_parm_decls (void)
5320 struct c_binding *b;
5322 if (pedantic && !current_scope->warned_forward_parm_decls)
5324 pedwarn (input_location, OPT_Wpedantic,
5325 "ISO C forbids forward parameter declarations");
5326 current_scope->warned_forward_parm_decls = true;
5329 for (b = current_scope->bindings; b; b = b->prev)
5330 if (TREE_CODE (b->decl) == PARM_DECL)
5331 TREE_ASM_WRITTEN (b->decl) = 1;
5334 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5335 literal, which may be an incomplete array type completed by the
5336 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5337 literal. NON_CONST is true if the initializers contain something
5338 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5339 it is the (valid) alignment for this compound literal, as specified
5340 with _Alignas. */
5342 tree
5343 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5344 unsigned int alignas_align)
5346 /* We do not use start_decl here because we have a type, not a declarator;
5347 and do not use finish_decl because the decl should be stored inside
5348 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5349 tree decl;
5350 tree complit;
5351 tree stmt;
5353 if (type == error_mark_node
5354 || init == error_mark_node)
5355 return error_mark_node;
5357 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5358 DECL_EXTERNAL (decl) = 0;
5359 TREE_PUBLIC (decl) = 0;
5360 TREE_STATIC (decl) = (current_scope == file_scope);
5361 DECL_CONTEXT (decl) = current_function_decl;
5362 TREE_USED (decl) = 1;
5363 DECL_READ_P (decl) = 1;
5364 DECL_ARTIFICIAL (decl) = 1;
5365 DECL_IGNORED_P (decl) = 1;
5366 TREE_TYPE (decl) = type;
5367 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5368 if (alignas_align)
5370 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5371 DECL_USER_ALIGN (decl) = 1;
5373 store_init_value (loc, decl, init, NULL_TREE);
5375 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5377 int failure = complete_array_type (&TREE_TYPE (decl),
5378 DECL_INITIAL (decl), true);
5379 /* If complete_array_type returns 3, it means that the
5380 initial value of the compound literal is empty. Allow it. */
5381 gcc_assert (failure == 0 || failure == 3);
5383 type = TREE_TYPE (decl);
5384 TREE_TYPE (DECL_INITIAL (decl)) = type;
5387 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5389 c_incomplete_type_error (loc, NULL_TREE, type);
5390 return error_mark_node;
5393 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5394 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5395 TREE_SIDE_EFFECTS (complit) = 1;
5397 layout_decl (decl, 0);
5399 if (TREE_STATIC (decl))
5401 /* This decl needs a name for the assembler output. */
5402 set_compound_literal_name (decl);
5403 DECL_DEFER_OUTPUT (decl) = 1;
5404 DECL_COMDAT (decl) = 1;
5405 pushdecl (decl);
5406 rest_of_decl_compilation (decl, 1, 0);
5408 else
5409 pushdecl (decl);
5411 if (non_const)
5413 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5414 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5417 return complit;
5420 /* Check the type of a compound literal. Here we just check that it
5421 is valid for C++. */
5423 void
5424 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5426 if (warn_cxx_compat
5427 && (type_name->specs->typespec_kind == ctsk_tagdef
5428 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5429 warning_at (loc, OPT_Wc___compat,
5430 "defining a type in a compound literal is invalid in C++");
5433 /* Determine whether TYPE is a structure with a flexible array member,
5434 or a union containing such a structure (possibly recursively). */
5436 static bool
5437 flexible_array_type_p (tree type)
5439 tree x;
5440 switch (TREE_CODE (type))
5442 case RECORD_TYPE:
5443 x = TYPE_FIELDS (type);
5444 if (x == NULL_TREE)
5445 return false;
5446 while (DECL_CHAIN (x) != NULL_TREE)
5447 x = DECL_CHAIN (x);
5448 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5449 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5450 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5451 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5452 return true;
5453 return false;
5454 case UNION_TYPE:
5455 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5457 if (flexible_array_type_p (TREE_TYPE (x)))
5458 return true;
5460 return false;
5461 default:
5462 return false;
5466 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5467 replacing with appropriate values if they are invalid. */
5469 static void
5470 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5471 tree orig_name)
5473 tree type_mv;
5474 unsigned int max_width;
5475 unsigned HOST_WIDE_INT w;
5476 const char *name = (orig_name
5477 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5478 : _("<anonymous>"));
5480 /* Detect and ignore out of range field width and process valid
5481 field widths. */
5482 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5484 error_at (loc, "bit-field %qs width not an integer constant", name);
5485 *width = integer_one_node;
5487 else
5489 if (TREE_CODE (*width) != INTEGER_CST)
5491 *width = c_fully_fold (*width, false, NULL);
5492 if (TREE_CODE (*width) == INTEGER_CST)
5493 pedwarn (loc, OPT_Wpedantic,
5494 "bit-field %qs width not an integer constant expression",
5495 name);
5497 if (TREE_CODE (*width) != INTEGER_CST)
5499 error_at (loc, "bit-field %qs width not an integer constant", name);
5500 *width = integer_one_node;
5502 constant_expression_warning (*width);
5503 if (tree_int_cst_sgn (*width) < 0)
5505 error_at (loc, "negative width in bit-field %qs", name);
5506 *width = integer_one_node;
5508 else if (integer_zerop (*width) && orig_name)
5510 error_at (loc, "zero width for bit-field %qs", name);
5511 *width = integer_one_node;
5515 /* Detect invalid bit-field type. */
5516 if (TREE_CODE (*type) != INTEGER_TYPE
5517 && TREE_CODE (*type) != BOOLEAN_TYPE
5518 && TREE_CODE (*type) != ENUMERAL_TYPE)
5520 error_at (loc, "bit-field %qs has invalid type", name);
5521 *type = unsigned_type_node;
5524 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5526 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5527 name);
5528 *type = unsigned_type_node;
5531 type_mv = TYPE_MAIN_VARIANT (*type);
5532 if (!in_system_header_at (input_location)
5533 && type_mv != integer_type_node
5534 && type_mv != unsigned_type_node
5535 && type_mv != boolean_type_node)
5536 pedwarn_c90 (loc, OPT_Wpedantic,
5537 "type of bit-field %qs is a GCC extension", name);
5539 max_width = TYPE_PRECISION (*type);
5541 if (compare_tree_int (*width, max_width) > 0)
5543 error_at (loc, "width of %qs exceeds its type", name);
5544 w = max_width;
5545 *width = build_int_cst (integer_type_node, w);
5547 else
5548 w = tree_to_uhwi (*width);
5550 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5552 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5553 if (!lt
5554 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5555 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5556 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5562 /* Print warning about variable length array if necessary. */
5564 static void
5565 warn_variable_length_array (tree name, tree size)
5567 if (TREE_CONSTANT (size))
5569 if (name)
5570 pedwarn_c90 (input_location, OPT_Wvla,
5571 "ISO C90 forbids array %qE whose size "
5572 "can%'t be evaluated", name);
5573 else
5574 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5575 "whose size can%'t be evaluated");
5577 else
5579 if (name)
5580 pedwarn_c90 (input_location, OPT_Wvla,
5581 "ISO C90 forbids variable length array %qE", name);
5582 else
5583 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5584 "length array");
5588 /* Print warning about defaulting to int if necessary. */
5590 static void
5591 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5593 diagnostic_info diagnostic;
5594 va_list ap;
5595 rich_location richloc (line_table, location);
5597 va_start (ap, gmsgid);
5598 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5599 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5600 diagnostic.option_index = opt;
5601 diagnostic_report_diagnostic (global_dc, &diagnostic);
5602 va_end (ap);
5605 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5606 considering only those c_declspec_words found in LIST, which
5607 must be terminated by cdw_number_of_elements. */
5609 static location_t
5610 smallest_type_quals_location (const location_t *locations,
5611 const c_declspec_word *list)
5613 location_t loc = UNKNOWN_LOCATION;
5614 while (*list != cdw_number_of_elements)
5616 location_t newloc = locations[*list];
5617 if (loc == UNKNOWN_LOCATION
5618 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5619 loc = newloc;
5620 list++;
5623 return loc;
5626 /* Given declspecs and a declarator,
5627 determine the name and type of the object declared
5628 and construct a ..._DECL node for it.
5629 (In one case we can return a ..._TYPE node instead.
5630 For invalid input we sometimes return NULL_TREE.)
5632 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5634 DECL_CONTEXT says which syntactic context this declaration is in:
5635 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5636 FUNCDEF for a function definition. Like NORMAL but a few different
5637 error messages in each case. Return value may be zero meaning
5638 this definition is too screwy to try to parse.
5639 PARM for a parameter declaration (either within a function prototype
5640 or before a function body). Make a PARM_DECL, or return void_type_node.
5641 TYPENAME if for a typename (in a cast or sizeof).
5642 Don't make a DECL node; just return the ..._TYPE node.
5643 FIELD for a struct or union field; make a FIELD_DECL.
5644 INITIALIZED is true if the decl has an initializer.
5645 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5646 representing the width of the bit-field.
5647 DECL_ATTRS points to the list of attributes that should be added to this
5648 decl. Any nested attributes that belong on the decl itself will be
5649 added to this list.
5650 If EXPR is not NULL, any expressions that need to be evaluated as
5651 part of evaluating variably modified types will be stored in *EXPR.
5652 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5653 set to indicate whether operands in *EXPR can be used in constant
5654 expressions.
5655 DEPRECATED_STATE is a deprecated_states value indicating whether
5656 deprecation warnings should be suppressed.
5658 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5659 It may also be so in the PARM case, for a prototype where the
5660 argument type is specified but not the name.
5662 This function is where the complicated C meanings of `static'
5663 and `extern' are interpreted. */
5665 static tree
5666 grokdeclarator (const struct c_declarator *declarator,
5667 struct c_declspecs *declspecs,
5668 enum decl_context decl_context, bool initialized, tree *width,
5669 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5670 enum deprecated_states deprecated_state)
5672 tree type = declspecs->type;
5673 bool threadp = declspecs->thread_p;
5674 enum c_storage_class storage_class = declspecs->storage_class;
5675 int constp;
5676 int restrictp;
5677 int volatilep;
5678 int atomicp;
5679 int type_quals = TYPE_UNQUALIFIED;
5680 tree name = NULL_TREE;
5681 bool funcdef_flag = false;
5682 bool funcdef_syntax = false;
5683 bool size_varies = false;
5684 tree decl_attr = declspecs->decl_attr;
5685 int array_ptr_quals = TYPE_UNQUALIFIED;
5686 tree array_ptr_attrs = NULL_TREE;
5687 bool array_parm_static = false;
5688 bool array_parm_vla_unspec_p = false;
5689 tree returned_attrs = NULL_TREE;
5690 bool bitfield = width != NULL;
5691 tree element_type;
5692 tree orig_qual_type = NULL;
5693 size_t orig_qual_indirect = 0;
5694 struct c_arg_info *arg_info = 0;
5695 addr_space_t as1, as2, address_space;
5696 location_t loc = UNKNOWN_LOCATION;
5697 tree expr_dummy;
5698 bool expr_const_operands_dummy;
5699 enum c_declarator_kind first_non_attr_kind;
5700 unsigned int alignas_align = 0;
5702 if (TREE_CODE (type) == ERROR_MARK)
5703 return error_mark_node;
5704 if (expr == NULL)
5706 expr = &expr_dummy;
5707 expr_dummy = NULL_TREE;
5709 if (expr_const_operands == NULL)
5710 expr_const_operands = &expr_const_operands_dummy;
5712 if (declspecs->expr)
5714 if (*expr)
5715 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5716 declspecs->expr);
5717 else
5718 *expr = declspecs->expr;
5720 *expr_const_operands = declspecs->expr_const_operands;
5722 if (decl_context == FUNCDEF)
5723 funcdef_flag = true, decl_context = NORMAL;
5725 /* Look inside a declarator for the name being declared
5726 and get it as an IDENTIFIER_NODE, for an error message. */
5728 const struct c_declarator *decl = declarator;
5730 first_non_attr_kind = cdk_attrs;
5731 while (decl)
5732 switch (decl->kind)
5734 case cdk_array:
5735 loc = decl->id_loc;
5736 /* FALL THRU. */
5738 case cdk_function:
5739 case cdk_pointer:
5740 funcdef_syntax = (decl->kind == cdk_function);
5741 if (first_non_attr_kind == cdk_attrs)
5742 first_non_attr_kind = decl->kind;
5743 decl = decl->declarator;
5744 break;
5746 case cdk_attrs:
5747 decl = decl->declarator;
5748 break;
5750 case cdk_id:
5751 loc = decl->id_loc;
5752 if (decl->u.id)
5753 name = decl->u.id;
5754 if (first_non_attr_kind == cdk_attrs)
5755 first_non_attr_kind = decl->kind;
5756 decl = 0;
5757 break;
5759 default:
5760 gcc_unreachable ();
5762 if (name == NULL_TREE)
5764 gcc_assert (decl_context == PARM
5765 || decl_context == TYPENAME
5766 || (decl_context == FIELD
5767 && declarator->kind == cdk_id));
5768 gcc_assert (!initialized);
5772 /* A function definition's declarator must have the form of
5773 a function declarator. */
5775 if (funcdef_flag && !funcdef_syntax)
5776 return NULL_TREE;
5778 /* If this looks like a function definition, make it one,
5779 even if it occurs where parms are expected.
5780 Then store_parm_decls will reject it and not use it as a parm. */
5781 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5782 decl_context = PARM;
5784 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5785 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5787 if ((decl_context == NORMAL || decl_context == FIELD)
5788 && current_scope == file_scope
5789 && variably_modified_type_p (type, NULL_TREE))
5791 if (name)
5792 error_at (loc, "variably modified %qE at file scope", name);
5793 else
5794 error_at (loc, "variably modified field at file scope");
5795 type = integer_type_node;
5798 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5800 /* Diagnose defaulting to "int". */
5802 if (declspecs->default_int_p && !in_system_header_at (input_location))
5804 /* Issue a warning if this is an ISO C 99 program or if
5805 -Wreturn-type and this is a function, or if -Wimplicit;
5806 prefer the former warning since it is more explicit. */
5807 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
5808 && funcdef_flag)
5809 warn_about_return_type = 1;
5810 else
5812 if (name)
5813 warn_defaults_to (loc, OPT_Wimplicit_int,
5814 "type defaults to %<int%> in declaration "
5815 "of %qE", name);
5816 else
5817 warn_defaults_to (loc, OPT_Wimplicit_int,
5818 "type defaults to %<int%> in type name");
5822 /* Adjust the type if a bit-field is being declared,
5823 -funsigned-bitfields applied and the type is not explicitly
5824 "signed". */
5825 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5826 && TREE_CODE (type) == INTEGER_TYPE)
5827 type = unsigned_type_for (type);
5829 /* Figure out the type qualifiers for the declaration. There are
5830 two ways a declaration can become qualified. One is something
5831 like `const int i' where the `const' is explicit. Another is
5832 something like `typedef const int CI; CI i' where the type of the
5833 declaration contains the `const'. A third possibility is that
5834 there is a type qualifier on the element type of a typedefed
5835 array type, in which case we should extract that qualifier so
5836 that c_apply_type_quals_to_decl receives the full list of
5837 qualifiers to work with (C90 is not entirely clear about whether
5838 duplicate qualifiers should be diagnosed in this case, but it
5839 seems most appropriate to do so). */
5840 element_type = strip_array_types (type);
5841 constp = declspecs->const_p + TYPE_READONLY (element_type);
5842 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5843 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5844 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5845 as1 = declspecs->address_space;
5846 as2 = TYPE_ADDR_SPACE (element_type);
5847 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5849 if (constp > 1)
5850 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5851 if (restrictp > 1)
5852 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5853 if (volatilep > 1)
5854 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5855 if (atomicp > 1)
5856 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5858 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5859 error_at (loc, "conflicting named address spaces (%s vs %s)",
5860 c_addr_space_name (as1), c_addr_space_name (as2));
5862 if ((TREE_CODE (type) == ARRAY_TYPE
5863 || first_non_attr_kind == cdk_array)
5864 && TYPE_QUALS (element_type))
5866 orig_qual_type = type;
5867 type = TYPE_MAIN_VARIANT (type);
5869 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5870 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5871 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5872 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5873 | ENCODE_QUAL_ADDR_SPACE (address_space));
5874 if (type_quals != TYPE_QUALS (element_type))
5875 orig_qual_type = NULL_TREE;
5877 /* Applying the _Atomic qualifier to an array type (through the use
5878 of typedefs or typeof) must be detected here. If the qualifier
5879 is introduced later, any appearance of applying it to an array is
5880 actually applying it to an element of that array. */
5881 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5882 error_at (loc, "%<_Atomic%>-qualified array type");
5884 /* Warn about storage classes that are invalid for certain
5885 kinds of declarations (parameters, typenames, etc.). */
5887 if (funcdef_flag
5888 && (threadp
5889 || storage_class == csc_auto
5890 || storage_class == csc_register
5891 || storage_class == csc_typedef))
5893 if (storage_class == csc_auto)
5894 pedwarn (loc,
5895 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5896 "function definition declared %<auto%>");
5897 if (storage_class == csc_register)
5898 error_at (loc, "function definition declared %<register%>");
5899 if (storage_class == csc_typedef)
5900 error_at (loc, "function definition declared %<typedef%>");
5901 if (threadp)
5902 error_at (loc, "function definition declared %qs",
5903 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5904 threadp = false;
5905 if (storage_class == csc_auto
5906 || storage_class == csc_register
5907 || storage_class == csc_typedef)
5908 storage_class = csc_none;
5910 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5912 if (decl_context == PARM && storage_class == csc_register)
5914 else
5916 switch (decl_context)
5918 case FIELD:
5919 if (name)
5920 error_at (loc, "storage class specified for structure "
5921 "field %qE", name);
5922 else
5923 error_at (loc, "storage class specified for structure field");
5924 break;
5925 case PARM:
5926 if (name)
5927 error_at (loc, "storage class specified for parameter %qE",
5928 name);
5929 else
5930 error_at (loc, "storage class specified for unnamed parameter");
5931 break;
5932 default:
5933 error_at (loc, "storage class specified for typename");
5934 break;
5936 storage_class = csc_none;
5937 threadp = false;
5940 else if (storage_class == csc_extern
5941 && initialized
5942 && !funcdef_flag)
5944 /* 'extern' with initialization is invalid if not at file scope. */
5945 if (current_scope == file_scope)
5947 /* It is fine to have 'extern const' when compiling at C
5948 and C++ intersection. */
5949 if (!(warn_cxx_compat && constp))
5950 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5951 name);
5953 else
5954 error_at (loc, "%qE has both %<extern%> and initializer", name);
5956 else if (current_scope == file_scope)
5958 if (storage_class == csc_auto)
5959 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5960 name);
5961 if (pedantic && storage_class == csc_register)
5962 pedwarn (input_location, OPT_Wpedantic,
5963 "file-scope declaration of %qE specifies %<register%>", name);
5965 else
5967 if (storage_class == csc_extern && funcdef_flag)
5968 error_at (loc, "nested function %qE declared %<extern%>", name);
5969 else if (threadp && storage_class == csc_none)
5971 error_at (loc, "function-scope %qE implicitly auto and declared "
5972 "%qs", name,
5973 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5974 threadp = false;
5978 /* Now figure out the structure of the declarator proper.
5979 Descend through it, creating more complex types, until we reach
5980 the declared identifier (or NULL_TREE, in an absolute declarator).
5981 At each stage we maintain an unqualified version of the type
5982 together with any qualifiers that should be applied to it with
5983 c_build_qualified_type; this way, array types including
5984 multidimensional array types are first built up in unqualified
5985 form and then the qualified form is created with
5986 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5988 while (declarator && declarator->kind != cdk_id)
5990 if (type == error_mark_node)
5992 declarator = declarator->declarator;
5993 continue;
5996 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5997 a cdk_pointer (for *...),
5998 a cdk_function (for ...(...)),
5999 a cdk_attrs (for nested attributes),
6000 or a cdk_id (for the name being declared
6001 or the place in an absolute declarator
6002 where the name was omitted).
6003 For the last case, we have just exited the loop.
6005 At this point, TYPE is the type of elements of an array,
6006 or for a function to return, or for a pointer to point to.
6007 After this sequence of ifs, TYPE is the type of the
6008 array or function or pointer, and DECLARATOR has had its
6009 outermost layer removed. */
6011 if (array_ptr_quals != TYPE_UNQUALIFIED
6012 || array_ptr_attrs != NULL_TREE
6013 || array_parm_static)
6015 /* Only the innermost declarator (making a parameter be of
6016 array type which is converted to pointer type)
6017 may have static or type qualifiers. */
6018 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6019 array_ptr_quals = TYPE_UNQUALIFIED;
6020 array_ptr_attrs = NULL_TREE;
6021 array_parm_static = false;
6024 switch (declarator->kind)
6026 case cdk_attrs:
6028 /* A declarator with embedded attributes. */
6029 tree attrs = declarator->u.attrs;
6030 const struct c_declarator *inner_decl;
6031 int attr_flags = 0;
6032 declarator = declarator->declarator;
6033 inner_decl = declarator;
6034 while (inner_decl->kind == cdk_attrs)
6035 inner_decl = inner_decl->declarator;
6036 if (inner_decl->kind == cdk_id)
6037 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6038 else if (inner_decl->kind == cdk_function)
6039 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6040 else if (inner_decl->kind == cdk_array)
6041 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6042 returned_attrs = decl_attributes (&type,
6043 chainon (returned_attrs, attrs),
6044 attr_flags);
6045 break;
6047 case cdk_array:
6049 tree itype = NULL_TREE;
6050 tree size = declarator->u.array.dimen;
6051 /* The index is a signed object `sizetype' bits wide. */
6052 tree index_type = c_common_signed_type (sizetype);
6054 array_ptr_quals = declarator->u.array.quals;
6055 array_ptr_attrs = declarator->u.array.attrs;
6056 array_parm_static = declarator->u.array.static_p;
6057 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6059 declarator = declarator->declarator;
6061 /* Check for some types that there cannot be arrays of. */
6063 if (VOID_TYPE_P (type))
6065 if (name)
6066 error_at (loc, "declaration of %qE as array of voids", name);
6067 else
6068 error_at (loc, "declaration of type name as array of voids");
6069 type = error_mark_node;
6072 if (TREE_CODE (type) == FUNCTION_TYPE)
6074 if (name)
6075 error_at (loc, "declaration of %qE as array of functions",
6076 name);
6077 else
6078 error_at (loc, "declaration of type name as array of "
6079 "functions");
6080 type = error_mark_node;
6083 if (pedantic && !in_system_header_at (input_location)
6084 && flexible_array_type_p (type))
6085 pedwarn (loc, OPT_Wpedantic,
6086 "invalid use of structure with flexible array member");
6088 if (size == error_mark_node)
6089 type = error_mark_node;
6091 if (type == error_mark_node)
6092 continue;
6094 /* If size was specified, set ITYPE to a range-type for
6095 that size. Otherwise, ITYPE remains null. finish_decl
6096 may figure it out from an initial value. */
6098 if (size)
6100 bool size_maybe_const = true;
6101 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6102 && !TREE_OVERFLOW (size));
6103 bool this_size_varies = false;
6105 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6106 lvalue. */
6107 STRIP_TYPE_NOPS (size);
6109 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6111 if (name)
6112 error_at (loc, "size of array %qE has non-integer type",
6113 name);
6114 else
6115 error_at (loc,
6116 "size of unnamed array has non-integer type");
6117 size = integer_one_node;
6119 /* This can happen with enum forward declaration. */
6120 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6122 if (name)
6123 error_at (loc, "size of array %qE has incomplete type",
6124 name);
6125 else
6126 error_at (loc, "size of unnamed array has incomplete "
6127 "type");
6128 size = integer_one_node;
6131 size = c_fully_fold (size, false, &size_maybe_const);
6133 if (pedantic && size_maybe_const && integer_zerop (size))
6135 if (name)
6136 pedwarn (loc, OPT_Wpedantic,
6137 "ISO C forbids zero-size array %qE", name);
6138 else
6139 pedwarn (loc, OPT_Wpedantic,
6140 "ISO C forbids zero-size array");
6143 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6145 constant_expression_warning (size);
6146 if (tree_int_cst_sgn (size) < 0)
6148 if (name)
6149 error_at (loc, "size of array %qE is negative", name);
6150 else
6151 error_at (loc, "size of unnamed array is negative");
6152 size = integer_one_node;
6154 /* Handle a size folded to an integer constant but
6155 not an integer constant expression. */
6156 if (!size_int_const)
6158 /* If this is a file scope declaration of an
6159 ordinary identifier, this is invalid code;
6160 diagnosing it here and not subsequently
6161 treating the type as variable-length avoids
6162 more confusing diagnostics later. */
6163 if ((decl_context == NORMAL || decl_context == FIELD)
6164 && current_scope == file_scope)
6165 pedwarn (input_location, 0,
6166 "variably modified %qE at file scope",
6167 name);
6168 else
6169 this_size_varies = size_varies = true;
6170 warn_variable_length_array (name, size);
6173 else if ((decl_context == NORMAL || decl_context == FIELD)
6174 && current_scope == file_scope)
6176 error_at (loc, "variably modified %qE at file scope", name);
6177 size = integer_one_node;
6179 else
6181 /* Make sure the array size remains visibly
6182 nonconstant even if it is (eg) a const variable
6183 with known value. */
6184 this_size_varies = size_varies = true;
6185 warn_variable_length_array (name, size);
6186 if (sanitize_flags_p (SANITIZE_VLA)
6187 && current_function_decl != NULL_TREE
6188 && decl_context == NORMAL)
6190 /* Evaluate the array size only once. */
6191 size = save_expr (size);
6192 size = c_fully_fold (size, false, NULL);
6193 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6194 ubsan_instrument_vla (loc, size),
6195 size);
6199 if (integer_zerop (size) && !this_size_varies)
6201 /* A zero-length array cannot be represented with
6202 an unsigned index type, which is what we'll
6203 get with build_index_type. Create an
6204 open-ended range instead. */
6205 itype = build_range_type (sizetype, size, NULL_TREE);
6207 else
6209 /* Arrange for the SAVE_EXPR on the inside of the
6210 MINUS_EXPR, which allows the -1 to get folded
6211 with the +1 that happens when building TYPE_SIZE. */
6212 if (size_varies)
6213 size = save_expr (size);
6214 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6215 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6216 integer_zero_node, size);
6218 /* Compute the maximum valid index, that is, size
6219 - 1. Do the calculation in index_type, so that
6220 if it is a variable the computations will be
6221 done in the proper mode. */
6222 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6223 convert (index_type, size),
6224 convert (index_type,
6225 size_one_node));
6227 /* The above overflows when size does not fit
6228 in index_type.
6229 ??? While a size of INT_MAX+1 technically shouldn't
6230 cause an overflow (because we subtract 1), handling
6231 this case seems like an unnecessary complication. */
6232 if (TREE_CODE (size) == INTEGER_CST
6233 && !int_fits_type_p (size, index_type))
6235 if (name)
6236 error_at (loc, "size of array %qE is too large",
6237 name);
6238 else
6239 error_at (loc, "size of unnamed array is too large");
6240 type = error_mark_node;
6241 continue;
6244 itype = build_index_type (itype);
6246 if (this_size_varies)
6248 if (*expr)
6249 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6250 *expr, size);
6251 else
6252 *expr = size;
6253 *expr_const_operands &= size_maybe_const;
6256 else if (decl_context == FIELD)
6258 bool flexible_array_member = false;
6259 if (array_parm_vla_unspec_p)
6260 /* Field names can in fact have function prototype
6261 scope so [*] is disallowed here through making
6262 the field variably modified, not through being
6263 something other than a declaration with function
6264 prototype scope. */
6265 size_varies = true;
6266 else
6268 const struct c_declarator *t = declarator;
6269 while (t->kind == cdk_attrs)
6270 t = t->declarator;
6271 flexible_array_member = (t->kind == cdk_id);
6273 if (flexible_array_member
6274 && !in_system_header_at (input_location))
6275 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6276 "support flexible array members");
6278 /* ISO C99 Flexible array members are effectively
6279 identical to GCC's zero-length array extension. */
6280 if (flexible_array_member || array_parm_vla_unspec_p)
6281 itype = build_range_type (sizetype, size_zero_node,
6282 NULL_TREE);
6284 else if (decl_context == PARM)
6286 if (array_parm_vla_unspec_p)
6288 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6289 size_varies = true;
6292 else if (decl_context == TYPENAME)
6294 if (array_parm_vla_unspec_p)
6296 /* C99 6.7.5.2p4 */
6297 warning (0, "%<[*]%> not in a declaration");
6298 /* We use this to avoid messing up with incomplete
6299 array types of the same type, that would
6300 otherwise be modified below. */
6301 itype = build_range_type (sizetype, size_zero_node,
6302 NULL_TREE);
6303 size_varies = true;
6307 /* Complain about arrays of incomplete types. */
6308 if (!COMPLETE_TYPE_P (type))
6310 error_at (loc, "array type has incomplete element type %qT",
6311 type);
6312 /* See if we can be more helpful. */
6313 if (TREE_CODE (type) == ARRAY_TYPE)
6315 if (name)
6316 inform (loc, "declaration of %qE as multidimensional "
6317 "array must have bounds for all dimensions "
6318 "except the first", name);
6319 else
6320 inform (loc, "declaration of multidimensional array "
6321 "must have bounds for all dimensions except "
6322 "the first");
6324 type = error_mark_node;
6326 else
6327 /* When itype is NULL, a shared incomplete array type is
6328 returned for all array of a given type. Elsewhere we
6329 make sure we don't complete that type before copying
6330 it, but here we want to make sure we don't ever
6331 modify the shared type, so we gcc_assert (itype)
6332 below. */
6334 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6335 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6336 type = build_qualified_type (type,
6337 ENCODE_QUAL_ADDR_SPACE (as));
6339 type = build_array_type (type, itype);
6342 if (type != error_mark_node)
6344 if (size_varies)
6346 /* It is ok to modify type here even if itype is
6347 NULL: if size_varies, we're in a
6348 multi-dimensional array and the inner type has
6349 variable size, so the enclosing shared array type
6350 must too. */
6351 if (size && TREE_CODE (size) == INTEGER_CST)
6352 type
6353 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6354 C_TYPE_VARIABLE_SIZE (type) = 1;
6357 /* The GCC extension for zero-length arrays differs from
6358 ISO flexible array members in that sizeof yields
6359 zero. */
6360 if (size && integer_zerop (size))
6362 gcc_assert (itype);
6363 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6364 TYPE_SIZE (type) = bitsize_zero_node;
6365 TYPE_SIZE_UNIT (type) = size_zero_node;
6366 SET_TYPE_STRUCTURAL_EQUALITY (type);
6368 if (array_parm_vla_unspec_p)
6370 gcc_assert (itype);
6371 /* The type is complete. C99 6.7.5.2p4 */
6372 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6373 TYPE_SIZE (type) = bitsize_zero_node;
6374 TYPE_SIZE_UNIT (type) = size_zero_node;
6375 SET_TYPE_STRUCTURAL_EQUALITY (type);
6378 if (!valid_array_size_p (loc, type, name))
6379 type = error_mark_node;
6382 if (decl_context != PARM
6383 && (array_ptr_quals != TYPE_UNQUALIFIED
6384 || array_ptr_attrs != NULL_TREE
6385 || array_parm_static))
6387 error_at (loc, "static or type qualifiers in non-parameter "
6388 "array declarator");
6389 array_ptr_quals = TYPE_UNQUALIFIED;
6390 array_ptr_attrs = NULL_TREE;
6391 array_parm_static = false;
6393 orig_qual_indirect++;
6394 break;
6396 case cdk_function:
6398 /* Say it's a definition only for the declarator closest
6399 to the identifier, apart possibly from some
6400 attributes. */
6401 bool really_funcdef = false;
6402 tree arg_types;
6403 orig_qual_type = NULL_TREE;
6404 if (funcdef_flag)
6406 const struct c_declarator *t = declarator->declarator;
6407 while (t->kind == cdk_attrs)
6408 t = t->declarator;
6409 really_funcdef = (t->kind == cdk_id);
6412 /* Declaring a function type. Make sure we have a valid
6413 type for the function to return. */
6414 if (type == error_mark_node)
6415 continue;
6417 size_varies = false;
6419 /* Warn about some types functions can't return. */
6420 if (TREE_CODE (type) == FUNCTION_TYPE)
6422 if (name)
6423 error_at (loc, "%qE declared as function returning a "
6424 "function", name);
6425 else
6426 error_at (loc, "type name declared as function "
6427 "returning a function");
6428 type = integer_type_node;
6430 if (TREE_CODE (type) == ARRAY_TYPE)
6432 if (name)
6433 error_at (loc, "%qE declared as function returning an array",
6434 name);
6435 else
6436 error_at (loc, "type name declared as function returning "
6437 "an array");
6438 type = integer_type_node;
6441 /* Construct the function type and go to the next
6442 inner layer of declarator. */
6443 arg_info = declarator->u.arg_info;
6444 arg_types = grokparms (arg_info, really_funcdef);
6446 /* Type qualifiers before the return type of the function
6447 qualify the return type, not the function type. */
6448 if (type_quals)
6450 const enum c_declspec_word ignored_quals_list[] =
6452 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6453 cdw_atomic, cdw_number_of_elements
6455 location_t specs_loc
6456 = smallest_type_quals_location (declspecs->locations,
6457 ignored_quals_list);
6458 if (specs_loc == UNKNOWN_LOCATION)
6459 specs_loc = declspecs->locations[cdw_typedef];
6460 if (specs_loc == UNKNOWN_LOCATION)
6461 specs_loc = loc;
6463 /* Type qualifiers on a function return type are
6464 normally permitted by the standard but have no
6465 effect, so give a warning at -Wreturn-type.
6466 Qualifiers on a void return type are banned on
6467 function definitions in ISO C; GCC used to used
6468 them for noreturn functions. The resolution of C11
6469 DR#423 means qualifiers (other than _Atomic) are
6470 actually removed from the return type when
6471 determining the function type. */
6472 int quals_used = type_quals;
6473 if (flag_isoc11)
6474 quals_used &= TYPE_QUAL_ATOMIC;
6475 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6476 pedwarn (specs_loc, 0,
6477 "function definition has qualified void return type");
6478 else
6479 warning_at (specs_loc, OPT_Wignored_qualifiers,
6480 "type qualifiers ignored on function return type");
6482 /* Ensure an error for restrict on invalid types; the
6483 DR#423 resolution is not entirely clear about
6484 this. */
6485 if (flag_isoc11
6486 && (type_quals & TYPE_QUAL_RESTRICT)
6487 && (!POINTER_TYPE_P (type)
6488 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6489 error_at (loc, "invalid use of %<restrict%>");
6490 if (quals_used)
6491 type = c_build_qualified_type (type, quals_used);
6493 type_quals = TYPE_UNQUALIFIED;
6495 type = build_function_type (type, arg_types);
6496 declarator = declarator->declarator;
6498 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6499 the formal parameter list of this FUNCTION_TYPE to point to
6500 the FUNCTION_TYPE node itself. */
6502 c_arg_tag *tag;
6503 unsigned ix;
6505 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6506 TYPE_CONTEXT (tag->type) = type;
6508 break;
6510 case cdk_pointer:
6512 /* Merge any constancy or volatility into the target type
6513 for the pointer. */
6514 if ((type_quals & TYPE_QUAL_ATOMIC)
6515 && TREE_CODE (type) == FUNCTION_TYPE)
6517 error_at (loc,
6518 "%<_Atomic%>-qualified function type");
6519 type_quals &= ~TYPE_QUAL_ATOMIC;
6521 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6522 && type_quals)
6523 pedwarn (loc, OPT_Wpedantic,
6524 "ISO C forbids qualified function types");
6525 if (type_quals)
6526 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6527 orig_qual_indirect);
6528 orig_qual_type = NULL_TREE;
6529 size_varies = false;
6531 /* When the pointed-to type involves components of variable size,
6532 care must be taken to ensure that the size evaluation code is
6533 emitted early enough to dominate all the possible later uses
6534 and late enough for the variables on which it depends to have
6535 been assigned.
6537 This is expected to happen automatically when the pointed-to
6538 type has a name/declaration of it's own, but special attention
6539 is required if the type is anonymous.
6541 We attach an artificial TYPE_DECL to such pointed-to type
6542 and arrange for it to be included in a DECL_EXPR. This
6543 forces the sizes evaluation at a safe point and ensures it
6544 is not deferred until e.g. within a deeper conditional context.
6546 PARM contexts have no enclosing statement list that
6547 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6548 instead, and add it to the list of expressions that
6549 need to be evaluated.
6551 TYPENAME contexts do have an enclosing statement list,
6552 but it would be incorrect to use it, as the size should
6553 only be evaluated if the containing expression is
6554 evaluated. We might also be in the middle of an
6555 expression with side effects on the pointed-to type size
6556 "arguments" prior to the pointer declaration point and
6557 the fake TYPE_DECL in the enclosing context would force
6558 the size evaluation prior to the side effects. We therefore
6559 use BIND_EXPRs in TYPENAME contexts too. */
6560 if (!TYPE_NAME (type)
6561 && variably_modified_type_p (type, NULL_TREE))
6563 tree bind = NULL_TREE;
6564 if (decl_context == TYPENAME || decl_context == PARM)
6566 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6567 NULL_TREE, NULL_TREE);
6568 TREE_SIDE_EFFECTS (bind) = 1;
6569 BIND_EXPR_BODY (bind) = push_stmt_list ();
6570 push_scope ();
6572 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6573 DECL_ARTIFICIAL (decl) = 1;
6574 pushdecl (decl);
6575 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6576 TYPE_NAME (type) = decl;
6577 if (bind)
6579 pop_scope ();
6580 BIND_EXPR_BODY (bind)
6581 = pop_stmt_list (BIND_EXPR_BODY (bind));
6582 if (*expr)
6583 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6584 bind);
6585 else
6586 *expr = bind;
6590 type = c_build_pointer_type (type);
6592 /* Process type qualifiers (such as const or volatile)
6593 that were given inside the `*'. */
6594 type_quals = declarator->u.pointer_quals;
6596 declarator = declarator->declarator;
6597 break;
6599 default:
6600 gcc_unreachable ();
6603 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6605 /* Now TYPE has the actual type, apart from any qualifiers in
6606 TYPE_QUALS. */
6608 /* Warn about address space used for things other than static memory or
6609 pointers. */
6610 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6611 if (!ADDR_SPACE_GENERIC_P (address_space))
6613 if (decl_context == NORMAL)
6615 switch (storage_class)
6617 case csc_auto:
6618 error ("%qs combined with %<auto%> qualifier for %qE",
6619 c_addr_space_name (address_space), name);
6620 break;
6621 case csc_register:
6622 error ("%qs combined with %<register%> qualifier for %qE",
6623 c_addr_space_name (address_space), name);
6624 break;
6625 case csc_none:
6626 if (current_function_scope)
6628 error ("%qs specified for auto variable %qE",
6629 c_addr_space_name (address_space), name);
6630 break;
6632 break;
6633 case csc_static:
6634 case csc_extern:
6635 case csc_typedef:
6636 break;
6637 default:
6638 gcc_unreachable ();
6641 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6643 if (name)
6644 error ("%qs specified for parameter %qE",
6645 c_addr_space_name (address_space), name);
6646 else
6647 error ("%qs specified for unnamed parameter",
6648 c_addr_space_name (address_space));
6650 else if (decl_context == FIELD)
6652 if (name)
6653 error ("%qs specified for structure field %qE",
6654 c_addr_space_name (address_space), name);
6655 else
6656 error ("%qs specified for structure field",
6657 c_addr_space_name (address_space));
6661 /* Check the type and width of a bit-field. */
6662 if (bitfield)
6664 check_bitfield_type_and_width (loc, &type, width, name);
6665 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6666 atomic types are permitted for bit-fields; we have no code to
6667 make bit-field accesses atomic, so disallow them. */
6668 if (type_quals & TYPE_QUAL_ATOMIC)
6670 if (name)
6671 error_at (loc, "bit-field %qE has atomic type", name);
6672 else
6673 error_at (loc, "bit-field has atomic type");
6674 type_quals &= ~TYPE_QUAL_ATOMIC;
6678 /* Reject invalid uses of _Alignas. */
6679 if (declspecs->alignas_p)
6681 if (storage_class == csc_typedef)
6682 error_at (loc, "alignment specified for typedef %qE", name);
6683 else if (storage_class == csc_register)
6684 error_at (loc, "alignment specified for %<register%> object %qE",
6685 name);
6686 else if (decl_context == PARM)
6688 if (name)
6689 error_at (loc, "alignment specified for parameter %qE", name);
6690 else
6691 error_at (loc, "alignment specified for unnamed parameter");
6693 else if (bitfield)
6695 if (name)
6696 error_at (loc, "alignment specified for bit-field %qE", name);
6697 else
6698 error_at (loc, "alignment specified for unnamed bit-field");
6700 else if (TREE_CODE (type) == FUNCTION_TYPE)
6701 error_at (loc, "alignment specified for function %qE", name);
6702 else if (declspecs->align_log != -1 && TYPE_P (type))
6704 alignas_align = 1U << declspecs->align_log;
6705 if (alignas_align < min_align_of_type (type))
6707 if (name)
6708 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6709 "alignment of %qE", name);
6710 else
6711 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6712 "alignment of unnamed field");
6713 alignas_align = 0;
6718 /* If this is declaring a typedef name, return a TYPE_DECL. */
6720 if (storage_class == csc_typedef)
6722 tree decl;
6723 if ((type_quals & TYPE_QUAL_ATOMIC)
6724 && TREE_CODE (type) == FUNCTION_TYPE)
6726 error_at (loc,
6727 "%<_Atomic%>-qualified function type");
6728 type_quals &= ~TYPE_QUAL_ATOMIC;
6730 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6731 && type_quals)
6732 pedwarn (loc, OPT_Wpedantic,
6733 "ISO C forbids qualified function types");
6734 if (type_quals)
6735 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6736 orig_qual_indirect);
6737 decl = build_decl (declarator->id_loc,
6738 TYPE_DECL, declarator->u.id, type);
6739 if (declspecs->explicit_signed_p)
6740 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6741 if (declspecs->inline_p)
6742 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6743 if (declspecs->noreturn_p)
6744 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6746 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6748 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6750 if (b != NULL
6751 && b->decl != NULL_TREE
6752 && (B_IN_CURRENT_SCOPE (b)
6753 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6754 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6756 auto_diagnostic_group d;
6757 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6758 ("using %qD as both a typedef and a tag is "
6759 "invalid in C++"), decl)
6760 && b->locus != UNKNOWN_LOCATION)
6761 inform (b->locus, "originally defined here");
6765 return decl;
6768 /* If this is a type name (such as, in a cast or sizeof),
6769 compute the type and return it now. */
6771 if (decl_context == TYPENAME)
6773 /* Note that the grammar rejects storage classes in typenames
6774 and fields. */
6775 gcc_assert (storage_class == csc_none && !threadp
6776 && !declspecs->inline_p && !declspecs->noreturn_p);
6777 if ((type_quals & TYPE_QUAL_ATOMIC)
6778 && TREE_CODE (type) == FUNCTION_TYPE)
6780 error_at (loc,
6781 "%<_Atomic%>-qualified function type");
6782 type_quals &= ~TYPE_QUAL_ATOMIC;
6784 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6785 && type_quals)
6786 pedwarn (loc, OPT_Wpedantic,
6787 "ISO C forbids const or volatile function types");
6788 if (type_quals)
6789 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6790 orig_qual_indirect);
6791 return type;
6794 if (pedantic && decl_context == FIELD
6795 && variably_modified_type_p (type, NULL_TREE))
6797 /* C99 6.7.2.1p8 */
6798 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6799 "have a variably modified type");
6802 /* Aside from typedefs and type names (handle above),
6803 `void' at top level (not within pointer)
6804 is allowed only in public variables.
6805 We don't complain about parms either, but that is because
6806 a better error message can be made later. */
6808 if (VOID_TYPE_P (type) && decl_context != PARM
6809 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6810 && (storage_class == csc_extern
6811 || (current_scope == file_scope
6812 && !(storage_class == csc_static
6813 || storage_class == csc_register)))))
6815 error_at (loc, "variable or field %qE declared void", name);
6816 type = integer_type_node;
6819 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6820 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6823 tree decl;
6825 if (decl_context == PARM)
6827 tree promoted_type;
6828 bool array_parameter_p = false;
6830 /* A parameter declared as an array of T is really a pointer to T.
6831 One declared as a function is really a pointer to a function. */
6833 if (TREE_CODE (type) == ARRAY_TYPE)
6835 /* Transfer const-ness of array into that of type pointed to. */
6836 type = TREE_TYPE (type);
6837 if (orig_qual_type != NULL_TREE)
6839 if (orig_qual_indirect == 0)
6840 orig_qual_type = TREE_TYPE (orig_qual_type);
6841 else
6842 orig_qual_indirect--;
6844 if (type_quals)
6845 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6846 orig_qual_indirect);
6847 type = c_build_pointer_type (type);
6848 type_quals = array_ptr_quals;
6849 if (type_quals)
6850 type = c_build_qualified_type (type, type_quals);
6852 /* We don't yet implement attributes in this context. */
6853 if (array_ptr_attrs != NULL_TREE)
6854 warning_at (loc, OPT_Wattributes,
6855 "attributes in parameter array declarator ignored");
6857 size_varies = false;
6858 array_parameter_p = true;
6860 else if (TREE_CODE (type) == FUNCTION_TYPE)
6862 if (type_quals & TYPE_QUAL_ATOMIC)
6864 error_at (loc,
6865 "%<_Atomic%>-qualified function type");
6866 type_quals &= ~TYPE_QUAL_ATOMIC;
6868 else if (type_quals)
6869 pedwarn (loc, OPT_Wpedantic,
6870 "ISO C forbids qualified function types");
6871 if (type_quals)
6872 type = c_build_qualified_type (type, type_quals);
6873 type = c_build_pointer_type (type);
6874 type_quals = TYPE_UNQUALIFIED;
6876 else if (type_quals)
6877 type = c_build_qualified_type (type, type_quals);
6879 decl = build_decl (declarator->id_loc,
6880 PARM_DECL, declarator->u.id, type);
6881 if (size_varies)
6882 C_DECL_VARIABLE_SIZE (decl) = 1;
6883 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6885 /* Compute the type actually passed in the parmlist,
6886 for the case where there is no prototype.
6887 (For example, shorts and chars are passed as ints.)
6888 When there is a prototype, this is overridden later. */
6890 if (type == error_mark_node)
6891 promoted_type = type;
6892 else
6893 promoted_type = c_type_promotes_to (type);
6895 DECL_ARG_TYPE (decl) = promoted_type;
6896 if (declspecs->inline_p)
6897 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6898 if (declspecs->noreturn_p)
6899 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6901 else if (decl_context == FIELD)
6903 /* Note that the grammar rejects storage classes in typenames
6904 and fields. */
6905 gcc_assert (storage_class == csc_none && !threadp
6906 && !declspecs->inline_p && !declspecs->noreturn_p);
6908 /* Structure field. It may not be a function. */
6910 if (TREE_CODE (type) == FUNCTION_TYPE)
6912 error_at (loc, "field %qE declared as a function", name);
6913 type = build_pointer_type (type);
6915 else if (TREE_CODE (type) != ERROR_MARK
6916 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6918 if (name)
6919 error_at (loc, "field %qE has incomplete type", name);
6920 else
6921 error_at (loc, "unnamed field has incomplete type");
6922 type = error_mark_node;
6924 else if (TREE_CODE (type) == ARRAY_TYPE
6925 && TYPE_DOMAIN (type) == NULL_TREE)
6927 /* We have a flexible array member through a typedef.
6928 Set suitable range. Whether this is a correct position
6929 for a flexible array member will be determined elsewhere. */
6930 if (!in_system_header_at (input_location))
6931 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6932 "support flexible array members");
6933 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6934 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6935 NULL_TREE);
6936 if (orig_qual_indirect == 0)
6937 orig_qual_type = NULL_TREE;
6939 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6940 orig_qual_indirect);
6941 decl = build_decl (declarator->id_loc,
6942 FIELD_DECL, declarator->u.id, type);
6943 DECL_NONADDRESSABLE_P (decl) = bitfield;
6944 if (bitfield && !declarator->u.id)
6946 TREE_NO_WARNING (decl) = 1;
6947 DECL_PADDING_P (decl) = 1;
6950 if (size_varies)
6951 C_DECL_VARIABLE_SIZE (decl) = 1;
6953 else if (TREE_CODE (type) == FUNCTION_TYPE)
6955 if (storage_class == csc_register || threadp)
6957 error_at (loc, "invalid storage class for function %qE", name);
6959 else if (current_scope != file_scope)
6961 /* Function declaration not at file scope. Storage
6962 classes other than `extern' are not allowed, C99
6963 6.7.1p5, and `extern' makes no difference. However,
6964 GCC allows 'auto', perhaps with 'inline', to support
6965 nested functions. */
6966 if (storage_class == csc_auto)
6967 pedwarn (loc, OPT_Wpedantic,
6968 "invalid storage class for function %qE", name);
6969 else if (storage_class == csc_static)
6971 error_at (loc, "invalid storage class for function %qE", name);
6972 if (funcdef_flag)
6973 storage_class = declspecs->storage_class = csc_none;
6974 else
6975 return NULL_TREE;
6979 decl = build_decl (declarator->id_loc,
6980 FUNCTION_DECL, declarator->u.id, type);
6981 decl = build_decl_attribute_variant (decl, decl_attr);
6983 if (type_quals & TYPE_QUAL_ATOMIC)
6985 error_at (loc,
6986 "%<_Atomic%>-qualified function type");
6987 type_quals &= ~TYPE_QUAL_ATOMIC;
6989 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6990 pedwarn (loc, OPT_Wpedantic,
6991 "ISO C forbids qualified function types");
6993 /* Every function declaration is an external reference
6994 (DECL_EXTERNAL) except for those which are not at file
6995 scope and are explicitly declared "auto". This is
6996 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6997 GCC to signify a forward declaration of a nested function. */
6998 if (storage_class == csc_auto && current_scope != file_scope)
6999 DECL_EXTERNAL (decl) = 0;
7000 /* In C99, a function which is declared 'inline' with 'extern'
7001 is not an external reference (which is confusing). It
7002 means that the later definition of the function must be output
7003 in this file, C99 6.7.4p6. In GNU C89, a function declared
7004 'extern inline' is an external reference. */
7005 else if (declspecs->inline_p && storage_class != csc_static)
7006 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7007 == flag_gnu89_inline);
7008 else
7009 DECL_EXTERNAL (decl) = !initialized;
7011 /* Record absence of global scope for `static' or `auto'. */
7012 TREE_PUBLIC (decl)
7013 = !(storage_class == csc_static || storage_class == csc_auto);
7015 /* For a function definition, record the argument information
7016 block where store_parm_decls will look for it. */
7017 if (funcdef_flag)
7018 current_function_arg_info = arg_info;
7020 if (declspecs->default_int_p)
7021 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7023 /* Record presence of `inline' and `_Noreturn', if it is
7024 reasonable. */
7025 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
7027 if (declspecs->inline_p)
7028 pedwarn (loc, 0, "cannot inline function %<main%>");
7029 if (declspecs->noreturn_p)
7030 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7032 else
7034 if (declspecs->inline_p)
7035 /* Record that the function is declared `inline'. */
7036 DECL_DECLARED_INLINE_P (decl) = 1;
7037 if (declspecs->noreturn_p)
7039 if (flag_isoc99)
7040 pedwarn_c99 (loc, OPT_Wpedantic,
7041 "ISO C99 does not support %<_Noreturn%>");
7042 else
7043 pedwarn_c99 (loc, OPT_Wpedantic,
7044 "ISO C90 does not support %<_Noreturn%>");
7045 TREE_THIS_VOLATILE (decl) = 1;
7049 else
7051 /* It's a variable. */
7052 /* An uninitialized decl with `extern' is a reference. */
7053 int extern_ref = !initialized && storage_class == csc_extern;
7055 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7056 orig_qual_indirect);
7058 /* C99 6.2.2p7: It is invalid (compile-time undefined
7059 behavior) to create an 'extern' declaration for a
7060 variable if there is a global declaration that is
7061 'static' and the global declaration is not visible.
7062 (If the static declaration _is_ currently visible,
7063 the 'extern' declaration is taken to refer to that decl.) */
7064 if (extern_ref && current_scope != file_scope)
7066 tree global_decl = identifier_global_value (declarator->u.id);
7067 tree visible_decl = lookup_name (declarator->u.id);
7069 if (global_decl
7070 && global_decl != visible_decl
7071 && VAR_P (global_decl)
7072 && !TREE_PUBLIC (global_decl))
7073 error_at (loc, "variable previously declared %<static%> "
7074 "redeclared %<extern%>");
7077 decl = build_decl (declarator->id_loc,
7078 VAR_DECL, declarator->u.id, type);
7079 if (size_varies)
7080 C_DECL_VARIABLE_SIZE (decl) = 1;
7082 if (declspecs->inline_p)
7083 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7084 if (declspecs->noreturn_p)
7085 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7087 /* At file scope, an initialized extern declaration may follow
7088 a static declaration. In that case, DECL_EXTERNAL will be
7089 reset later in start_decl. */
7090 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7092 /* At file scope, the presence of a `static' or `register' storage
7093 class specifier, or the absence of all storage class specifiers
7094 makes this declaration a definition (perhaps tentative). Also,
7095 the absence of `static' makes it public. */
7096 if (current_scope == file_scope)
7098 TREE_PUBLIC (decl) = storage_class != csc_static;
7099 TREE_STATIC (decl) = !extern_ref;
7101 /* Not at file scope, only `static' makes a static definition. */
7102 else
7104 TREE_STATIC (decl) = (storage_class == csc_static);
7105 TREE_PUBLIC (decl) = extern_ref;
7108 if (threadp)
7109 set_decl_tls_model (decl, decl_default_tls_model (decl));
7112 if ((storage_class == csc_extern
7113 || (storage_class == csc_none
7114 && TREE_CODE (type) == FUNCTION_TYPE
7115 && !funcdef_flag))
7116 && variably_modified_type_p (type, NULL_TREE))
7118 /* C99 6.7.5.2p2 */
7119 if (TREE_CODE (type) == FUNCTION_TYPE)
7120 error_at (loc, "non-nested function with variably modified type");
7121 else
7122 error_at (loc, "object with variably modified type must have "
7123 "no linkage");
7126 /* Record `register' declaration for warnings on &
7127 and in case doing stupid register allocation. */
7129 if (storage_class == csc_register)
7131 C_DECL_REGISTER (decl) = 1;
7132 DECL_REGISTER (decl) = 1;
7135 /* Record constancy and volatility. */
7136 c_apply_type_quals_to_decl (type_quals, decl);
7138 /* Apply _Alignas specifiers. */
7139 if (alignas_align)
7141 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7142 DECL_USER_ALIGN (decl) = 1;
7145 /* If a type has volatile components, it should be stored in memory.
7146 Otherwise, the fact that those components are volatile
7147 will be ignored, and would even crash the compiler.
7148 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7149 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7150 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7151 || TREE_CODE (decl) == RESULT_DECL))
7153 /* It is not an error for a structure with volatile fields to
7154 be declared register, but reset DECL_REGISTER since it
7155 cannot actually go in a register. */
7156 int was_reg = C_DECL_REGISTER (decl);
7157 C_DECL_REGISTER (decl) = 0;
7158 DECL_REGISTER (decl) = 0;
7159 c_mark_addressable (decl);
7160 C_DECL_REGISTER (decl) = was_reg;
7163 /* This is the earliest point at which we might know the assembler
7164 name of a variable. Thus, if it's known before this, die horribly. */
7165 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7166 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7168 if (warn_cxx_compat
7169 && VAR_P (decl)
7170 && TREE_PUBLIC (decl)
7171 && TREE_STATIC (decl)
7172 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7173 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7174 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7175 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7176 ("non-local variable %qD with anonymous type is "
7177 "questionable in C++"),
7178 decl);
7180 return decl;
7184 /* Decode the parameter-list info for a function type or function definition.
7185 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7186 if there is an identifier list instead of a parameter decl list).
7187 These two functions are separate because when a function returns
7188 or receives functions then each is called multiple times but the order
7189 of calls is different. The last call to `grokparms' is always the one
7190 that contains the formal parameter names of a function definition.
7192 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7194 FUNCDEF_FLAG is true for a function definition, false for
7195 a mere declaration. A nonempty identifier-list gets an error message
7196 when FUNCDEF_FLAG is false. */
7198 static tree
7199 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7201 tree arg_types = arg_info->types;
7203 if (funcdef_flag && arg_info->had_vla_unspec)
7205 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7206 /* C99 6.7.5.2p4 */
7207 error ("%<[*]%> not allowed in other than function prototype scope");
7210 if (arg_types == NULL_TREE && !funcdef_flag
7211 && !in_system_header_at (input_location))
7212 warning (OPT_Wstrict_prototypes,
7213 "function declaration isn%'t a prototype");
7215 if (arg_types == error_mark_node)
7216 /* Don't set TYPE_ARG_TYPES in this case. */
7217 return NULL_TREE;
7219 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7221 if (!funcdef_flag)
7223 pedwarn (input_location, 0, "parameter names (without types) in "
7224 "function declaration");
7225 arg_info->parms = NULL_TREE;
7227 else
7228 arg_info->parms = arg_info->types;
7230 arg_info->types = NULL_TREE;
7231 return NULL_TREE;
7233 else
7235 tree parm, type, typelt;
7236 unsigned int parmno;
7238 /* If there is a parameter of incomplete type in a definition,
7239 this is an error. In a declaration this is valid, and a
7240 struct or union type may be completed later, before any calls
7241 or definition of the function. In the case where the tag was
7242 first declared within the parameter list, a warning has
7243 already been given. If a parameter has void type, then
7244 however the function cannot be defined or called, so
7245 warn. */
7247 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7248 parm;
7249 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7251 type = TREE_VALUE (typelt);
7252 if (type == error_mark_node)
7253 continue;
7255 if (!COMPLETE_TYPE_P (type))
7257 if (funcdef_flag)
7259 if (DECL_NAME (parm))
7260 error_at (input_location,
7261 "parameter %u (%q+D) has incomplete type",
7262 parmno, parm);
7263 else
7264 error_at (DECL_SOURCE_LOCATION (parm),
7265 "parameter %u has incomplete type",
7266 parmno);
7268 TREE_VALUE (typelt) = error_mark_node;
7269 TREE_TYPE (parm) = error_mark_node;
7270 arg_types = NULL_TREE;
7272 else if (VOID_TYPE_P (type))
7274 if (DECL_NAME (parm))
7275 warning_at (input_location, 0,
7276 "parameter %u (%q+D) has void type",
7277 parmno, parm);
7278 else
7279 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7280 "parameter %u has void type",
7281 parmno);
7285 if (DECL_NAME (parm) && TREE_USED (parm))
7286 warn_if_shadowing (parm);
7288 return arg_types;
7292 /* Allocate and initialize a c_arg_info structure from the parser's
7293 obstack. */
7295 struct c_arg_info *
7296 build_arg_info (void)
7298 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7299 ret->parms = NULL_TREE;
7300 ret->tags = NULL;
7301 ret->types = NULL_TREE;
7302 ret->others = NULL_TREE;
7303 ret->pending_sizes = NULL;
7304 ret->had_vla_unspec = 0;
7305 return ret;
7308 /* Take apart the current scope and return a c_arg_info structure with
7309 info on a parameter list just parsed.
7311 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7313 ELLIPSIS being true means the argument list ended in '...' so don't
7314 append a sentinel (void_list_node) to the end of the type-list.
7316 EXPR is NULL or an expression that needs to be evaluated for the
7317 side effects of array size expressions in the parameters. */
7319 struct c_arg_info *
7320 get_parm_info (bool ellipsis, tree expr)
7322 struct c_binding *b = current_scope->bindings;
7323 struct c_arg_info *arg_info = build_arg_info ();
7325 tree parms = NULL_TREE;
7326 vec<c_arg_tag, va_gc> *tags = NULL;
7327 tree types = NULL_TREE;
7328 tree others = NULL_TREE;
7330 bool gave_void_only_once_err = false;
7332 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7334 /* The bindings in this scope must not get put into a block.
7335 We will take care of deleting the binding nodes. */
7336 current_scope->bindings = 0;
7338 /* This function is only called if there was *something* on the
7339 parameter list. */
7340 gcc_assert (b);
7342 /* A parameter list consisting solely of 'void' indicates that the
7343 function takes no arguments. But if the 'void' is qualified
7344 (by 'const' or 'volatile'), or has a storage class specifier
7345 ('register'), then the behavior is undefined; issue an error.
7346 Typedefs for 'void' are OK (see DR#157). */
7347 if (b->prev == 0 /* one binding */
7348 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7349 && !DECL_NAME (b->decl) /* anonymous */
7350 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7352 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7353 || C_DECL_REGISTER (b->decl))
7354 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7356 /* There cannot be an ellipsis. */
7357 if (ellipsis)
7358 error_at (b->locus, "%<void%> must be the only parameter");
7360 arg_info->types = void_list_node;
7361 return arg_info;
7364 if (!ellipsis)
7365 types = void_list_node;
7367 /* Break up the bindings list into parms, tags, types, and others;
7368 apply sanity checks; purge the name-to-decl bindings. */
7369 while (b)
7371 tree decl = b->decl;
7372 tree type = TREE_TYPE (decl);
7373 c_arg_tag tag;
7374 const char *keyword;
7376 switch (TREE_CODE (decl))
7378 case PARM_DECL:
7379 if (b->id)
7381 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7382 I_SYMBOL_BINDING (b->id) = b->shadowed;
7385 /* Check for forward decls that never got their actual decl. */
7386 if (TREE_ASM_WRITTEN (decl))
7387 error_at (b->locus,
7388 "parameter %q+D has just a forward declaration", decl);
7389 /* Check for (..., void, ...) and issue an error. */
7390 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7392 if (!gave_void_only_once_err)
7394 error_at (b->locus, "%<void%> must be the only parameter");
7395 gave_void_only_once_err = true;
7398 else
7400 /* Valid parameter, add it to the list. */
7401 DECL_CHAIN (decl) = parms;
7402 parms = decl;
7404 /* Since there is a prototype, args are passed in their
7405 declared types. The back end may override this later. */
7406 DECL_ARG_TYPE (decl) = type;
7407 types = tree_cons (0, type, types);
7409 break;
7411 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7412 case UNION_TYPE: keyword = "union"; goto tag;
7413 case RECORD_TYPE: keyword = "struct"; goto tag;
7414 tag:
7415 /* Types may not have tag-names, in which case the type
7416 appears in the bindings list with b->id NULL. */
7417 if (b->id)
7419 gcc_assert (I_TAG_BINDING (b->id) == b);
7420 I_TAG_BINDING (b->id) = b->shadowed;
7423 /* Warn about any struct, union or enum tags defined in a
7424 parameter list. The scope of such types is limited to
7425 the parameter list, which is rarely if ever desirable
7426 (it's impossible to call such a function with type-
7427 correct arguments). An anonymous union parm type is
7428 meaningful as a GNU extension, so don't warn for that. */
7429 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7431 if (b->id)
7432 /* The %s will be one of 'struct', 'union', or 'enum'. */
7433 warning_at (b->locus, 0,
7434 "%<%s %E%> declared inside parameter list"
7435 " will not be visible outside of this definition or"
7436 " declaration", keyword, b->id);
7437 else
7438 /* The %s will be one of 'struct', 'union', or 'enum'. */
7439 warning_at (b->locus, 0,
7440 "anonymous %s declared inside parameter list"
7441 " will not be visible outside of this definition or"
7442 " declaration", keyword);
7445 tag.id = b->id;
7446 tag.type = decl;
7447 vec_safe_push (tags, tag);
7448 break;
7450 case FUNCTION_DECL:
7451 /* FUNCTION_DECLs appear when there is an implicit function
7452 declaration in the parameter list. */
7453 gcc_assert (b->nested || seen_error ());
7454 goto set_shadowed;
7456 case CONST_DECL:
7457 case TYPE_DECL:
7458 /* CONST_DECLs appear here when we have an embedded enum,
7459 and TYPE_DECLs appear here when we have an embedded struct
7460 or union. No warnings for this - we already warned about the
7461 type itself. */
7463 /* When we reinsert this decl in the function body, we need
7464 to reconstruct whether it was marked as nested. */
7465 gcc_assert (!b->nested);
7466 DECL_CHAIN (decl) = others;
7467 others = decl;
7468 /* fall through */
7470 case ERROR_MARK:
7471 set_shadowed:
7472 /* error_mark_node appears here when we have an undeclared
7473 variable. Just throw it away. */
7474 if (b->id)
7476 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7477 I_SYMBOL_BINDING (b->id) = b->shadowed;
7479 break;
7481 /* Other things that might be encountered. */
7482 case LABEL_DECL:
7483 case VAR_DECL:
7484 default:
7485 gcc_unreachable ();
7488 b = free_binding_and_advance (b);
7491 arg_info->parms = parms;
7492 arg_info->tags = tags;
7493 arg_info->types = types;
7494 arg_info->others = others;
7495 arg_info->pending_sizes = expr;
7496 return arg_info;
7499 /* Get the struct, enum or union (CODE says which) with tag NAME.
7500 Define the tag as a forward-reference with location LOC if it is
7501 not defined. Return a c_typespec structure for the type
7502 specifier. */
7504 struct c_typespec
7505 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7507 struct c_typespec ret;
7508 tree ref;
7509 location_t refloc;
7511 ret.expr = NULL_TREE;
7512 ret.expr_const_operands = true;
7514 /* If a cross reference is requested, look up the type
7515 already defined for this tag and return it. */
7517 ref = lookup_tag (code, name, false, &refloc);
7518 /* If this is the right type of tag, return what we found.
7519 (This reference will be shadowed by shadow_tag later if appropriate.)
7520 If this is the wrong type of tag, do not return it. If it was the
7521 wrong type in the same scope, we will have had an error
7522 message already; if in a different scope and declaring
7523 a name, pending_xref_error will give an error message; but if in a
7524 different scope and not declaring a name, this tag should
7525 shadow the previous declaration of a different type of tag, and
7526 this would not work properly if we return the reference found.
7527 (For example, with "struct foo" in an outer scope, "union foo;"
7528 must shadow that tag with a new one of union type.) */
7529 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7530 if (ref && TREE_CODE (ref) == code)
7532 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7533 && loc != UNKNOWN_LOCATION
7534 && warn_cxx_compat)
7536 switch (code)
7538 case ENUMERAL_TYPE:
7539 warning_at (loc, OPT_Wc___compat,
7540 ("enum type defined in struct or union "
7541 "is not visible in C++"));
7542 inform (refloc, "enum type defined here");
7543 break;
7544 case RECORD_TYPE:
7545 warning_at (loc, OPT_Wc___compat,
7546 ("struct defined in struct or union "
7547 "is not visible in C++"));
7548 inform (refloc, "struct defined here");
7549 break;
7550 case UNION_TYPE:
7551 warning_at (loc, OPT_Wc___compat,
7552 ("union defined in struct or union "
7553 "is not visible in C++"));
7554 inform (refloc, "union defined here");
7555 break;
7556 default:
7557 gcc_unreachable();
7561 ret.spec = ref;
7562 return ret;
7565 /* If no such tag is yet defined, create a forward-reference node
7566 and record it as the "definition".
7567 When a real declaration of this type is found,
7568 the forward-reference will be altered into a real type. */
7570 ref = make_node (code);
7571 if (code == ENUMERAL_TYPE)
7573 /* Give the type a default layout like unsigned int
7574 to avoid crashing if it does not get defined. */
7575 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7576 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7577 TYPE_USER_ALIGN (ref) = 0;
7578 TYPE_UNSIGNED (ref) = 1;
7579 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7580 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7581 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7584 pushtag (loc, name, ref);
7586 ret.spec = ref;
7587 return ret;
7590 /* Get the struct, enum or union (CODE says which) with tag NAME.
7591 Define the tag as a forward-reference if it is not defined.
7592 Return a tree for the type. */
7594 tree
7595 xref_tag (enum tree_code code, tree name)
7597 return parser_xref_tag (input_location, code, name).spec;
7600 /* Make sure that the tag NAME is defined *in the current scope*
7601 at least as a forward reference.
7602 LOC is the location of the struct's definition.
7603 CODE says which kind of tag NAME ought to be.
7605 This stores the current value of the file static STRUCT_PARSE_INFO
7606 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7607 new c_struct_parse_info structure. The old value of
7608 STRUCT_PARSE_INFO is restored in finish_struct. */
7610 tree
7611 start_struct (location_t loc, enum tree_code code, tree name,
7612 struct c_struct_parse_info **enclosing_struct_parse_info)
7614 /* If there is already a tag defined at this scope
7615 (as a forward reference), just return it. */
7617 tree ref = NULL_TREE;
7618 location_t refloc = UNKNOWN_LOCATION;
7620 if (name != NULL_TREE)
7621 ref = lookup_tag (code, name, true, &refloc);
7622 if (ref && TREE_CODE (ref) == code)
7624 if (TYPE_STUB_DECL (ref))
7625 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7627 if (TYPE_SIZE (ref))
7629 if (code == UNION_TYPE)
7630 error_at (loc, "redefinition of %<union %E%>", name);
7631 else
7632 error_at (loc, "redefinition of %<struct %E%>", name);
7633 if (refloc != UNKNOWN_LOCATION)
7634 inform (refloc, "originally defined here");
7635 /* Don't create structures using a name already in use. */
7636 ref = NULL_TREE;
7638 else if (C_TYPE_BEING_DEFINED (ref))
7640 if (code == UNION_TYPE)
7641 error_at (loc, "nested redefinition of %<union %E%>", name);
7642 else
7643 error_at (loc, "nested redefinition of %<struct %E%>", name);
7644 /* Don't bother to report "originally defined here" for a
7645 nested redefinition; the original definition should be
7646 obvious. */
7647 /* Don't create structures that contain themselves. */
7648 ref = NULL_TREE;
7652 /* Otherwise create a forward-reference just so the tag is in scope. */
7654 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7656 ref = make_node (code);
7657 pushtag (loc, name, ref);
7660 C_TYPE_BEING_DEFINED (ref) = 1;
7661 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7662 TYPE_PACKED (v) = flag_pack_struct;
7664 *enclosing_struct_parse_info = struct_parse_info;
7665 struct_parse_info = new c_struct_parse_info ();
7667 /* FIXME: This will issue a warning for a use of a type defined
7668 within a statement expr used within sizeof, et. al. This is not
7669 terribly serious as C++ doesn't permit statement exprs within
7670 sizeof anyhow. */
7671 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7672 warning_at (loc, OPT_Wc___compat,
7673 "defining type in %qs expression is invalid in C++",
7674 (in_sizeof
7675 ? "sizeof"
7676 : (in_typeof ? "typeof" : "alignof")));
7678 return ref;
7681 /* Process the specs, declarator and width (NULL if omitted)
7682 of a structure component, returning a FIELD_DECL node.
7683 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7684 DECL_ATTRS is as for grokdeclarator.
7686 LOC is the location of the structure component.
7688 This is done during the parsing of the struct declaration.
7689 The FIELD_DECL nodes are chained together and the lot of them
7690 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7692 tree
7693 grokfield (location_t loc,
7694 struct c_declarator *declarator, struct c_declspecs *declspecs,
7695 tree width, tree *decl_attrs)
7697 tree value;
7699 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7700 && width == NULL_TREE)
7702 /* This is an unnamed decl.
7704 If we have something of the form "union { list } ;" then this
7705 is the anonymous union extension. Similarly for struct.
7707 If this is something of the form "struct foo;", then
7708 If MS or Plan 9 extensions are enabled, this is handled as
7709 an anonymous struct.
7710 Otherwise this is a forward declaration of a structure tag.
7712 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7713 If foo names a structure or union without a tag, then this
7714 is an anonymous struct (this is permitted by C11).
7715 If MS or Plan 9 extensions are enabled and foo names a
7716 structure, then again this is an anonymous struct.
7717 Otherwise this is an error.
7719 Oh what a horrid tangled web we weave. I wonder if MS consciously
7720 took this from Plan 9 or if it was an accident of implementation
7721 that took root before someone noticed the bug... */
7723 tree type = declspecs->type;
7724 bool ok = false;
7726 if (RECORD_OR_UNION_TYPE_P (type)
7727 && (flag_ms_extensions
7728 || flag_plan9_extensions
7729 || !declspecs->typedef_p))
7731 if (flag_ms_extensions || flag_plan9_extensions)
7732 ok = true;
7733 else if (TYPE_NAME (type) == NULL)
7734 ok = true;
7735 else
7736 ok = false;
7738 if (!ok)
7740 pedwarn (loc, 0, "declaration does not declare anything");
7741 return NULL_TREE;
7743 if (flag_isoc99)
7744 pedwarn_c99 (loc, OPT_Wpedantic,
7745 "ISO C99 doesn%'t support unnamed structs/unions");
7746 else
7747 pedwarn_c99 (loc, OPT_Wpedantic,
7748 "ISO C90 doesn%'t support unnamed structs/unions");
7751 value = grokdeclarator (declarator, declspecs, FIELD, false,
7752 width ? &width : NULL, decl_attrs, NULL, NULL,
7753 DEPRECATED_NORMAL);
7755 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7756 DECL_INITIAL (value) = width;
7757 if (width)
7758 SET_DECL_C_BIT_FIELD (value);
7760 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7762 /* If we currently have a binding for this field, set the
7763 in_struct field in the binding, so that we warn about lookups
7764 which find it. */
7765 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7766 if (b != NULL)
7768 /* If the in_struct field is not yet set, push it on a list
7769 to be cleared when this struct is finished. */
7770 if (!b->in_struct)
7772 struct_parse_info->fields.safe_push (b);
7773 b->in_struct = 1;
7778 return value;
7781 /* Subroutine of detect_field_duplicates: return whether X and Y,
7782 which are both fields in the same struct, have duplicate field
7783 names. */
7785 static bool
7786 is_duplicate_field (tree x, tree y)
7788 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7789 return true;
7791 /* When using -fplan9-extensions, an anonymous field whose name is a
7792 typedef can duplicate a field name. */
7793 if (flag_plan9_extensions
7794 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7796 tree xt, xn, yt, yn;
7798 xt = TREE_TYPE (x);
7799 if (DECL_NAME (x) != NULL_TREE)
7800 xn = DECL_NAME (x);
7801 else if (RECORD_OR_UNION_TYPE_P (xt)
7802 && TYPE_NAME (xt) != NULL_TREE
7803 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7804 xn = DECL_NAME (TYPE_NAME (xt));
7805 else
7806 xn = NULL_TREE;
7808 yt = TREE_TYPE (y);
7809 if (DECL_NAME (y) != NULL_TREE)
7810 yn = DECL_NAME (y);
7811 else if (RECORD_OR_UNION_TYPE_P (yt)
7812 && TYPE_NAME (yt) != NULL_TREE
7813 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7814 yn = DECL_NAME (TYPE_NAME (yt));
7815 else
7816 yn = NULL_TREE;
7818 if (xn != NULL_TREE && xn == yn)
7819 return true;
7822 return false;
7825 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7826 to HTAB, giving errors for any duplicates. */
7828 static void
7829 detect_field_duplicates_hash (tree fieldlist,
7830 hash_table<nofree_ptr_hash <tree_node> > *htab)
7832 tree x, y;
7833 tree_node **slot;
7835 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7836 if ((y = DECL_NAME (x)) != NULL_TREE)
7838 slot = htab->find_slot (y, INSERT);
7839 if (*slot)
7841 error ("duplicate member %q+D", x);
7842 DECL_NAME (x) = NULL_TREE;
7844 *slot = y;
7846 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7848 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7850 /* When using -fplan9-extensions, an anonymous field whose
7851 name is a typedef can duplicate a field name. */
7852 if (flag_plan9_extensions
7853 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7854 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7856 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7857 slot = htab->find_slot (xn, INSERT);
7858 if (*slot)
7859 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7860 *slot = xn;
7865 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7866 the list such that this does not present a problem later. */
7868 static void
7869 detect_field_duplicates (tree fieldlist)
7871 tree x, y;
7872 int timeout = 10;
7874 /* If the struct is the list of instance variables of an Objective-C
7875 class, then we need to check all the instance variables of
7876 superclasses when checking for duplicates (since you can't have
7877 an instance variable in a subclass with the same name as an
7878 instance variable in a superclass). We pass on this job to the
7879 Objective-C compiler. objc_detect_field_duplicates() will return
7880 false if we are not checking the list of instance variables and
7881 the C frontend should proceed with the standard field duplicate
7882 checks. If we are checking the list of instance variables, the
7883 ObjC frontend will do the check, emit the errors if needed, and
7884 then return true. */
7885 if (c_dialect_objc ())
7886 if (objc_detect_field_duplicates (false))
7887 return;
7889 /* First, see if there are more than "a few" fields.
7890 This is trivially true if there are zero or one fields. */
7891 if (!fieldlist || !DECL_CHAIN (fieldlist))
7892 return;
7893 x = fieldlist;
7894 do {
7895 timeout--;
7896 if (DECL_NAME (x) == NULL_TREE
7897 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7898 timeout = 0;
7899 x = DECL_CHAIN (x);
7900 } while (timeout > 0 && x);
7902 /* If there were "few" fields and no anonymous structures or unions,
7903 avoid the overhead of allocating a hash table. Instead just do
7904 the nested traversal thing. */
7905 if (timeout > 0)
7907 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7908 /* When using -fplan9-extensions, we can have duplicates
7909 between typedef names and fields. */
7910 if (DECL_NAME (x)
7911 || (flag_plan9_extensions
7912 && DECL_NAME (x) == NULL_TREE
7913 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7914 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7915 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7917 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7918 if (is_duplicate_field (y, x))
7920 error ("duplicate member %q+D", x);
7921 DECL_NAME (x) = NULL_TREE;
7925 else
7927 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7928 detect_field_duplicates_hash (fieldlist, &htab);
7932 /* Finish up struct info used by -Wc++-compat. */
7934 static void
7935 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7936 location_t record_loc)
7938 unsigned int ix;
7939 tree x;
7940 struct c_binding *b;
7942 if (fieldlist == NULL_TREE)
7944 if (code == RECORD_TYPE)
7945 warning_at (record_loc, OPT_Wc___compat,
7946 "empty struct has size 0 in C, size 1 in C++");
7947 else
7948 warning_at (record_loc, OPT_Wc___compat,
7949 "empty union has size 0 in C, size 1 in C++");
7952 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7953 the current struct. We do this now at the end of the struct
7954 because the flag is used to issue visibility warnings, and we
7955 only want to issue those warnings if the type is referenced
7956 outside of the struct declaration. */
7957 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7958 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7960 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7961 typedefs used when declaring fields in this struct. If the name
7962 of any of the fields is also a typedef name then the struct would
7963 not parse in C++, because the C++ lookup rules say that the
7964 typedef name would be looked up in the context of the struct, and
7965 would thus be the field rather than the typedef. */
7966 if (!struct_parse_info->typedefs_seen.is_empty ()
7967 && fieldlist != NULL_TREE)
7969 /* Use a hash_set<tree> using the name of the typedef. We can use
7970 a hash_set<tree> because identifiers are interned. */
7971 hash_set<tree> tset;
7973 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7974 tset.add (DECL_NAME (x));
7976 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7978 if (DECL_NAME (x) != NULL_TREE
7979 && tset.contains (DECL_NAME (x)))
7981 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7982 ("using %qD as both field and typedef name is "
7983 "invalid in C++"),
7985 /* FIXME: It would be nice to report the location where
7986 the typedef name is used. */
7991 /* For each field which has a binding and which was not defined in
7992 an enclosing struct, clear the in_struct field. */
7993 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7994 b->in_struct = 0;
7997 /* Function to help qsort sort FIELD_DECLs by name order. */
7999 static int
8000 field_decl_cmp (const void *x_p, const void *y_p)
8002 const tree *const x = (const tree *) x_p;
8003 const tree *const y = (const tree *) y_p;
8005 if (DECL_NAME (*x) == DECL_NAME (*y))
8006 /* A nontype is "greater" than a type. */
8007 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8008 if (DECL_NAME (*x) == NULL_TREE)
8009 return -1;
8010 if (DECL_NAME (*y) == NULL_TREE)
8011 return 1;
8012 if (DECL_NAME (*x) < DECL_NAME (*y))
8013 return -1;
8014 return 1;
8017 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8018 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8019 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8020 ATTRIBUTES are attributes to be applied to the structure.
8022 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8023 the struct was started. */
8025 tree
8026 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8027 struct c_struct_parse_info *enclosing_struct_parse_info)
8029 tree x;
8030 bool toplevel = file_scope == current_scope;
8032 /* If this type was previously laid out as a forward reference,
8033 make sure we lay it out again. */
8035 TYPE_SIZE (t) = NULL_TREE;
8037 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8039 if (pedantic)
8041 for (x = fieldlist; x; x = DECL_CHAIN (x))
8043 if (DECL_NAME (x) != NULL_TREE)
8044 break;
8045 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8046 break;
8049 if (x == NULL_TREE)
8051 if (TREE_CODE (t) == UNION_TYPE)
8053 if (fieldlist)
8054 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8055 else
8056 pedwarn (loc, OPT_Wpedantic, "union has no members");
8058 else
8060 if (fieldlist)
8061 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8062 else
8063 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8068 /* Install struct as DECL_CONTEXT of each field decl.
8069 Also process specified field sizes, found in the DECL_INITIAL,
8070 storing 0 there after the type has been changed to precision equal
8071 to its width, rather than the precision of the specified standard
8072 type. (Correct layout requires the original type to have been preserved
8073 until now.) */
8075 bool saw_named_field = false;
8076 for (x = fieldlist; x; x = DECL_CHAIN (x))
8078 if (TREE_TYPE (x) == error_mark_node)
8079 continue;
8081 DECL_CONTEXT (x) = t;
8083 /* If any field is const, the structure type is pseudo-const. */
8084 if (TREE_READONLY (x))
8085 C_TYPE_FIELDS_READONLY (t) = 1;
8086 else
8088 /* A field that is pseudo-const makes the structure likewise. */
8089 tree t1 = strip_array_types (TREE_TYPE (x));
8090 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8091 C_TYPE_FIELDS_READONLY (t) = 1;
8094 /* Any field that is volatile means variables of this type must be
8095 treated in some ways as volatile. */
8096 if (TREE_THIS_VOLATILE (x))
8097 C_TYPE_FIELDS_VOLATILE (t) = 1;
8099 /* Any field of nominal variable size implies structure is too. */
8100 if (C_DECL_VARIABLE_SIZE (x))
8101 C_TYPE_VARIABLE_SIZE (t) = 1;
8103 if (DECL_C_BIT_FIELD (x))
8105 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8106 DECL_SIZE (x) = bitsize_int (width);
8107 DECL_BIT_FIELD (x) = 1;
8110 if (TYPE_PACKED (t)
8111 && (DECL_BIT_FIELD (x)
8112 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8113 DECL_PACKED (x) = 1;
8115 /* Detect flexible array member in an invalid context. */
8116 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8117 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8118 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8119 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8121 if (TREE_CODE (t) == UNION_TYPE)
8123 error_at (DECL_SOURCE_LOCATION (x),
8124 "flexible array member in union");
8125 TREE_TYPE (x) = error_mark_node;
8127 else if (DECL_CHAIN (x) != NULL_TREE)
8129 error_at (DECL_SOURCE_LOCATION (x),
8130 "flexible array member not at end of struct");
8131 TREE_TYPE (x) = error_mark_node;
8133 else if (!saw_named_field)
8135 error_at (DECL_SOURCE_LOCATION (x),
8136 "flexible array member in a struct with no named "
8137 "members");
8138 TREE_TYPE (x) = error_mark_node;
8142 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8143 && flexible_array_type_p (TREE_TYPE (x)))
8144 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8145 "invalid use of structure with flexible array member");
8147 if (DECL_NAME (x)
8148 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8149 saw_named_field = true;
8152 detect_field_duplicates (fieldlist);
8154 /* Now we have the nearly final fieldlist. Record it,
8155 then lay out the structure or union (including the fields). */
8157 TYPE_FIELDS (t) = fieldlist;
8159 maybe_apply_pragma_scalar_storage_order (t);
8161 layout_type (t);
8163 if (TYPE_SIZE_UNIT (t)
8164 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8165 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8166 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8167 error ("type %qT is too large", t);
8169 /* Give bit-fields their proper types and rewrite the type of array fields
8170 with scalar component if the enclosing type has reverse storage order. */
8171 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8173 if (TREE_CODE (field) == FIELD_DECL
8174 && DECL_INITIAL (field)
8175 && TREE_TYPE (field) != error_mark_node)
8177 unsigned HOST_WIDE_INT width
8178 = tree_to_uhwi (DECL_INITIAL (field));
8179 tree type = TREE_TYPE (field);
8180 if (width != TYPE_PRECISION (type))
8182 TREE_TYPE (field)
8183 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8184 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8186 DECL_INITIAL (field) = NULL_TREE;
8188 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8189 && TREE_CODE (field) == FIELD_DECL
8190 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8192 tree ftype = TREE_TYPE (field);
8193 tree ctype = strip_array_types (ftype);
8194 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8196 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8197 tree *typep = &fmain_type;
8198 do {
8199 *typep = build_distinct_type_copy (*typep);
8200 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8201 typep = &TREE_TYPE (*typep);
8202 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8203 TREE_TYPE (field)
8204 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8209 /* Now we have the truly final field list.
8210 Store it in this type and in the variants. */
8212 TYPE_FIELDS (t) = fieldlist;
8214 /* If there are lots of fields, sort so we can look through them fast.
8215 We arbitrarily consider 16 or more elts to be "a lot". */
8218 int len = 0;
8220 for (x = fieldlist; x; x = DECL_CHAIN (x))
8222 if (len > 15 || DECL_NAME (x) == NULL)
8223 break;
8224 len += 1;
8227 if (len > 15)
8229 tree *field_array;
8230 struct lang_type *space;
8231 struct sorted_fields_type *space2;
8233 len += list_length (x);
8235 /* Use the same allocation policy here that make_node uses, to
8236 ensure that this lives as long as the rest of the struct decl.
8237 All decls in an inline function need to be saved. */
8239 space = ggc_cleared_alloc<struct lang_type> ();
8240 space2 = (sorted_fields_type *) ggc_internal_alloc
8241 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8243 len = 0;
8244 space->s = space2;
8245 field_array = &space2->elts[0];
8246 for (x = fieldlist; x; x = DECL_CHAIN (x))
8248 field_array[len++] = x;
8250 /* If there is anonymous struct or union, break out of the loop. */
8251 if (DECL_NAME (x) == NULL)
8252 break;
8254 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8255 if (x == NULL)
8257 TYPE_LANG_SPECIFIC (t) = space;
8258 TYPE_LANG_SPECIFIC (t)->s->len = len;
8259 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8260 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8265 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8266 in dwarf2out via rest_of_decl_compilation below and means
8267 something totally different. Since we will be clearing
8268 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8269 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8270 C_TYPE_INCOMPLETE_VARS should use some language specific
8271 node. */
8272 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8273 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8275 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8276 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8277 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8278 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8279 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8280 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8283 /* If this was supposed to be a transparent union, but we can't
8284 make it one, warn and turn off the flag. */
8285 if (TREE_CODE (t) == UNION_TYPE
8286 && TYPE_TRANSPARENT_AGGR (t)
8287 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8289 TYPE_TRANSPARENT_AGGR (t) = 0;
8290 warning_at (loc, 0, "union cannot be made transparent");
8293 /* Update type location to the one of the definition, instead of e.g.
8294 a forward declaration. */
8295 if (TYPE_STUB_DECL (t))
8296 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8298 /* Finish debugging output for this type. */
8299 rest_of_type_compilation (t, toplevel);
8301 /* If this structure or union completes the type of any previous
8302 variable declaration, lay it out and output its rtl. */
8303 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8305 tree decl = TREE_VALUE (x);
8306 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8307 layout_array_type (TREE_TYPE (decl));
8308 if (TREE_CODE (decl) != TYPE_DECL)
8310 layout_decl (decl, 0);
8311 if (c_dialect_objc ())
8312 objc_check_decl (decl);
8313 rest_of_decl_compilation (decl, toplevel, 0);
8317 /* If we're inside a function proper, i.e. not file-scope and not still
8318 parsing parameters, then arrange for the size of a variable sized type
8319 to be bound now. */
8320 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8321 add_stmt (build_stmt (loc,
8322 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8324 if (warn_cxx_compat)
8325 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8327 delete struct_parse_info;
8329 struct_parse_info = enclosing_struct_parse_info;
8331 /* If this struct is defined inside a struct, add it to
8332 struct_types. */
8333 if (warn_cxx_compat
8334 && struct_parse_info != NULL
8335 && !in_sizeof && !in_typeof && !in_alignof)
8336 struct_parse_info->struct_types.safe_push (t);
8338 return t;
8341 static struct {
8342 gt_pointer_operator new_value;
8343 void *cookie;
8344 } resort_data;
8346 /* This routine compares two fields like field_decl_cmp but using the
8347 pointer operator in resort_data. */
8349 static int
8350 resort_field_decl_cmp (const void *x_p, const void *y_p)
8352 const tree *const x = (const tree *) x_p;
8353 const tree *const y = (const tree *) y_p;
8355 if (DECL_NAME (*x) == DECL_NAME (*y))
8356 /* A nontype is "greater" than a type. */
8357 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8358 if (DECL_NAME (*x) == NULL_TREE)
8359 return -1;
8360 if (DECL_NAME (*y) == NULL_TREE)
8361 return 1;
8363 tree d1 = DECL_NAME (*x);
8364 tree d2 = DECL_NAME (*y);
8365 resort_data.new_value (&d1, resort_data.cookie);
8366 resort_data.new_value (&d2, resort_data.cookie);
8367 if (d1 < d2)
8368 return -1;
8370 return 1;
8373 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8375 void
8376 resort_sorted_fields (void *obj,
8377 void * ARG_UNUSED (orig_obj),
8378 gt_pointer_operator new_value,
8379 void *cookie)
8381 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8382 resort_data.new_value = new_value;
8383 resort_data.cookie = cookie;
8384 qsort (&sf->elts[0], sf->len, sizeof (tree),
8385 resort_field_decl_cmp);
8388 /* Lay out the type T, and its element type, and so on. */
8390 static void
8391 layout_array_type (tree t)
8393 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8394 layout_array_type (TREE_TYPE (t));
8395 layout_type (t);
8398 /* Begin compiling the definition of an enumeration type.
8399 NAME is its name (or null if anonymous).
8400 LOC is the enum's location.
8401 Returns the type object, as yet incomplete.
8402 Also records info about it so that build_enumerator
8403 may be used to declare the individual values as they are read. */
8405 tree
8406 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8408 tree enumtype = NULL_TREE;
8409 location_t enumloc = UNKNOWN_LOCATION;
8411 /* If this is the real definition for a previous forward reference,
8412 fill in the contents in the same object that used to be the
8413 forward reference. */
8415 if (name != NULL_TREE)
8416 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8418 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8420 enumtype = make_node (ENUMERAL_TYPE);
8421 pushtag (loc, name, enumtype);
8423 /* Update type location to the one of the definition, instead of e.g.
8424 a forward declaration. */
8425 else if (TYPE_STUB_DECL (enumtype))
8427 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8428 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8431 if (C_TYPE_BEING_DEFINED (enumtype))
8432 error_at (loc, "nested redefinition of %<enum %E%>", name);
8434 C_TYPE_BEING_DEFINED (enumtype) = 1;
8436 if (TYPE_VALUES (enumtype) != NULL_TREE)
8438 /* This enum is a named one that has been declared already. */
8439 error_at (loc, "redeclaration of %<enum %E%>", name);
8440 if (enumloc != UNKNOWN_LOCATION)
8441 inform (enumloc, "originally defined here");
8443 /* Completely replace its old definition.
8444 The old enumerators remain defined, however. */
8445 TYPE_VALUES (enumtype) = NULL_TREE;
8448 the_enum->enum_next_value = integer_zero_node;
8449 the_enum->enum_overflow = 0;
8451 if (flag_short_enums)
8452 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8453 TYPE_PACKED (v) = 1;
8455 /* FIXME: This will issue a warning for a use of a type defined
8456 within sizeof in a statement expr. This is not terribly serious
8457 as C++ doesn't permit statement exprs within sizeof anyhow. */
8458 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8459 warning_at (loc, OPT_Wc___compat,
8460 "defining type in %qs expression is invalid in C++",
8461 (in_sizeof
8462 ? "sizeof"
8463 : (in_typeof ? "typeof" : "alignof")));
8465 return enumtype;
8468 /* After processing and defining all the values of an enumeration type,
8469 install their decls in the enumeration type and finish it off.
8470 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8471 and ATTRIBUTES are the specified attributes.
8472 Returns ENUMTYPE. */
8474 tree
8475 finish_enum (tree enumtype, tree values, tree attributes)
8477 tree pair, tem;
8478 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8479 int precision;
8480 signop sign;
8481 bool toplevel = (file_scope == current_scope);
8482 struct lang_type *lt;
8484 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8486 /* Calculate the maximum value of any enumerator in this type. */
8488 if (values == error_mark_node)
8489 minnode = maxnode = integer_zero_node;
8490 else
8492 minnode = maxnode = TREE_VALUE (values);
8493 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8495 tree value = TREE_VALUE (pair);
8496 if (tree_int_cst_lt (maxnode, value))
8497 maxnode = value;
8498 if (tree_int_cst_lt (value, minnode))
8499 minnode = value;
8503 /* Construct the final type of this enumeration. It is the same
8504 as one of the integral types - the narrowest one that fits, except
8505 that normally we only go as narrow as int - and signed iff any of
8506 the values are negative. */
8507 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8508 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8509 tree_int_cst_min_precision (maxnode, sign));
8511 /* If the precision of the type was specified with an attribute and it
8512 was too small, give an error. Otherwise, use it. */
8513 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8515 if (precision > TYPE_PRECISION (enumtype))
8517 TYPE_PRECISION (enumtype) = 0;
8518 error ("specified mode too small for enumeral values");
8520 else
8521 precision = TYPE_PRECISION (enumtype);
8523 else
8524 TYPE_PRECISION (enumtype) = 0;
8526 if (TYPE_PACKED (enumtype)
8527 || precision > TYPE_PRECISION (integer_type_node)
8528 || TYPE_PRECISION (enumtype))
8530 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8531 if (tem == NULL)
8533 warning (0, "enumeration values exceed range of largest integer");
8534 tem = long_long_integer_type_node;
8537 else
8538 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8540 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8541 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8542 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8543 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8544 TYPE_SIZE (enumtype) = NULL_TREE;
8545 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8547 layout_type (enumtype);
8549 if (values != error_mark_node)
8551 /* Change the type of the enumerators to be the enum type. We
8552 need to do this irrespective of the size of the enum, for
8553 proper type checking. Replace the DECL_INITIALs of the
8554 enumerators, and the value slots of the list, with copies
8555 that have the enum type; they cannot be modified in place
8556 because they may be shared (e.g. integer_zero_node) Finally,
8557 change the purpose slots to point to the names of the decls. */
8558 for (pair = values; pair; pair = TREE_CHAIN (pair))
8560 tree enu = TREE_PURPOSE (pair);
8561 tree ini = DECL_INITIAL (enu);
8563 TREE_TYPE (enu) = enumtype;
8565 /* The ISO C Standard mandates enumerators to have type int,
8566 even though the underlying type of an enum type is
8567 unspecified. However, GCC allows enumerators of any
8568 integer type as an extensions. build_enumerator()
8569 converts any enumerators that fit in an int to type int,
8570 to avoid promotions to unsigned types when comparing
8571 integers with enumerators that fit in the int range.
8572 When -pedantic is given, build_enumerator() would have
8573 already warned about those that don't fit. Here we
8574 convert the rest to the enumerator type. */
8575 if (TREE_TYPE (ini) != integer_type_node)
8576 ini = convert (enumtype, ini);
8578 DECL_INITIAL (enu) = ini;
8579 TREE_PURPOSE (pair) = DECL_NAME (enu);
8580 TREE_VALUE (pair) = ini;
8583 TYPE_VALUES (enumtype) = values;
8586 /* Record the min/max values so that we can warn about bit-field
8587 enumerations that are too small for the values. */
8588 lt = ggc_cleared_alloc<struct lang_type> ();
8589 lt->enum_min = minnode;
8590 lt->enum_max = maxnode;
8591 TYPE_LANG_SPECIFIC (enumtype) = lt;
8593 /* Fix up all variant types of this enum type. */
8594 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8596 if (tem == enumtype)
8597 continue;
8598 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8599 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8600 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8601 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8602 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8603 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8604 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8605 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8606 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8607 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8608 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8611 /* Finish debugging output for this type. */
8612 rest_of_type_compilation (enumtype, toplevel);
8614 /* If this enum is defined inside a struct, add it to
8615 struct_types. */
8616 if (warn_cxx_compat
8617 && struct_parse_info != NULL
8618 && !in_sizeof && !in_typeof && !in_alignof)
8619 struct_parse_info->struct_types.safe_push (enumtype);
8621 return enumtype;
8624 /* Build and install a CONST_DECL for one value of the
8625 current enumeration type (one that was begun with start_enum).
8626 DECL_LOC is the location of the enumerator.
8627 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8628 Return a tree-list containing the CONST_DECL and its value.
8629 Assignment of sequential values by default is handled here. */
8631 tree
8632 build_enumerator (location_t decl_loc, location_t loc,
8633 struct c_enum_contents *the_enum, tree name, tree value)
8635 tree decl, type;
8637 /* Validate and default VALUE. */
8639 if (value != NULL_TREE)
8641 /* Don't issue more errors for error_mark_node (i.e. an
8642 undeclared identifier) - just ignore the value expression. */
8643 if (value == error_mark_node)
8644 value = NULL_TREE;
8645 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8647 error_at (loc, "enumerator value for %qE is not an integer constant",
8648 name);
8649 value = NULL_TREE;
8651 else
8653 if (TREE_CODE (value) != INTEGER_CST)
8655 value = c_fully_fold (value, false, NULL);
8656 if (TREE_CODE (value) == INTEGER_CST)
8657 pedwarn (loc, OPT_Wpedantic,
8658 "enumerator value for %qE is not an integer "
8659 "constant expression", name);
8661 if (TREE_CODE (value) != INTEGER_CST)
8663 error ("enumerator value for %qE is not an integer constant",
8664 name);
8665 value = NULL_TREE;
8667 else
8669 value = default_conversion (value);
8670 constant_expression_warning (value);
8675 /* Default based on previous value. */
8676 /* It should no longer be possible to have NON_LVALUE_EXPR
8677 in the default. */
8678 if (value == NULL_TREE)
8680 value = the_enum->enum_next_value;
8681 if (the_enum->enum_overflow)
8682 error_at (loc, "overflow in enumeration values");
8684 /* Even though the underlying type of an enum is unspecified, the
8685 type of enumeration constants is explicitly defined as int
8686 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8687 an extension. */
8688 else if (!int_fits_type_p (value, integer_type_node))
8689 pedwarn (loc, OPT_Wpedantic,
8690 "ISO C restricts enumerator values to range of %<int%>");
8692 /* The ISO C Standard mandates enumerators to have type int, even
8693 though the underlying type of an enum type is unspecified.
8694 However, GCC allows enumerators of any integer type as an
8695 extensions. Here we convert any enumerators that fit in an int
8696 to type int, to avoid promotions to unsigned types when comparing
8697 integers with enumerators that fit in the int range. When
8698 -pedantic is given, we would have already warned about those that
8699 don't fit. We have to do this here rather than in finish_enum
8700 because this value may be used to define more enumerators. */
8701 if (int_fits_type_p (value, integer_type_node))
8702 value = convert (integer_type_node, value);
8704 /* Set basis for default for next value. */
8705 the_enum->enum_next_value
8706 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8707 PLUS_EXPR, value, integer_one_node, false);
8708 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8710 /* Now create a declaration for the enum value name. */
8712 type = TREE_TYPE (value);
8713 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8714 TYPE_PRECISION (integer_type_node)),
8715 (TYPE_PRECISION (type)
8716 >= TYPE_PRECISION (integer_type_node)
8717 && TYPE_UNSIGNED (type)));
8719 decl = build_decl (decl_loc, CONST_DECL, name, type);
8720 DECL_INITIAL (decl) = convert (type, value);
8721 pushdecl (decl);
8723 return tree_cons (decl, value, NULL_TREE);
8727 /* Create the FUNCTION_DECL for a function definition.
8728 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8729 the declaration; they describe the function's name and the type it returns,
8730 but twisted together in a fashion that parallels the syntax of C.
8732 This function creates a binding context for the function body
8733 as well as setting up the FUNCTION_DECL in current_function_decl.
8735 Returns true on success. If the DECLARATOR is not suitable for a function
8736 (it defines a datum instead), we return false to report a parse error. */
8738 bool
8739 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8740 tree attributes)
8742 tree decl1, old_decl;
8743 tree restype, resdecl;
8744 location_t loc;
8746 current_function_returns_value = 0; /* Assume, until we see it does. */
8747 current_function_returns_null = 0;
8748 current_function_returns_abnormally = 0;
8749 warn_about_return_type = 0;
8750 c_switch_stack = NULL;
8752 /* Indicate no valid break/continue context by setting these variables
8753 to some non-null, non-label value. We'll notice and emit the proper
8754 error message in c_finish_bc_stmt. */
8755 c_break_label = c_cont_label = size_zero_node;
8757 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8758 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8759 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8761 /* If the declarator is not suitable for a function definition,
8762 cause a syntax error. */
8763 if (decl1 == NULL_TREE
8764 || TREE_CODE (decl1) != FUNCTION_DECL)
8765 return false;
8767 loc = DECL_SOURCE_LOCATION (decl1);
8769 c_decl_attributes (&decl1, attributes, 0);
8771 if (DECL_DECLARED_INLINE_P (decl1)
8772 && DECL_UNINLINABLE (decl1)
8773 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8774 warning_at (loc, OPT_Wattributes,
8775 "inline function %qD given attribute noinline",
8776 decl1);
8778 /* Handle gnu_inline attribute. */
8779 if (declspecs->inline_p
8780 && !flag_gnu89_inline
8781 && TREE_CODE (decl1) == FUNCTION_DECL
8782 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8783 || current_function_decl))
8785 if (declspecs->storage_class != csc_static)
8786 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8789 announce_function (decl1);
8791 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8793 error_at (loc, "return type is an incomplete type");
8794 /* Make it return void instead. */
8795 TREE_TYPE (decl1)
8796 = build_function_type (void_type_node,
8797 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8800 if (warn_about_return_type)
8801 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8802 : (warn_return_type > 0 ? OPT_Wreturn_type
8803 : OPT_Wimplicit_int),
8804 "return type defaults to %<int%>");
8806 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8807 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8808 DECL_INITIAL (decl1) = error_mark_node;
8810 /* A nested function is not global. */
8811 if (current_function_decl != NULL_TREE)
8812 TREE_PUBLIC (decl1) = 0;
8814 /* If this definition isn't a prototype and we had a prototype declaration
8815 before, copy the arg type info from that prototype. */
8816 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8817 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8818 old_decl = NULL_TREE;
8819 current_function_prototype_locus = UNKNOWN_LOCATION;
8820 current_function_prototype_built_in = false;
8821 current_function_prototype_arg_types = NULL_TREE;
8822 if (!prototype_p (TREE_TYPE (decl1)))
8824 if (old_decl != NULL_TREE
8825 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8826 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8827 TREE_TYPE (TREE_TYPE (old_decl))))
8829 if (stdarg_p (TREE_TYPE (old_decl)))
8831 auto_diagnostic_group d;
8832 warning_at (loc, 0, "%q+D defined as variadic function "
8833 "without prototype", decl1);
8834 locate_old_decl (old_decl);
8836 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8837 TREE_TYPE (decl1));
8838 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8839 current_function_prototype_built_in
8840 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8841 current_function_prototype_arg_types
8842 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8844 if (TREE_PUBLIC (decl1))
8846 /* If there is an external prototype declaration of this
8847 function, record its location but do not copy information
8848 to this decl. This may be an invisible declaration
8849 (built-in or in a scope which has finished) or simply
8850 have more refined argument types than any declaration
8851 found above. */
8852 struct c_binding *b;
8853 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8854 if (B_IN_SCOPE (b, external_scope))
8855 break;
8856 if (b)
8858 tree ext_decl, ext_type;
8859 ext_decl = b->decl;
8860 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8861 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8862 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8863 TREE_TYPE (ext_type)))
8865 current_function_prototype_locus
8866 = DECL_SOURCE_LOCATION (ext_decl);
8867 current_function_prototype_built_in
8868 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8869 current_function_prototype_arg_types
8870 = TYPE_ARG_TYPES (ext_type);
8876 /* Optionally warn of old-fashioned def with no previous prototype. */
8877 if (warn_strict_prototypes
8878 && old_decl != error_mark_node
8879 && !prototype_p (TREE_TYPE (decl1))
8880 && C_DECL_ISNT_PROTOTYPE (old_decl))
8881 warning_at (loc, OPT_Wstrict_prototypes,
8882 "function declaration isn%'t a prototype");
8883 /* Optionally warn of any global def with no previous prototype. */
8884 else if (warn_missing_prototypes
8885 && old_decl != error_mark_node
8886 && TREE_PUBLIC (decl1)
8887 && !MAIN_NAME_P (DECL_NAME (decl1))
8888 && C_DECL_ISNT_PROTOTYPE (old_decl)
8889 && !DECL_DECLARED_INLINE_P (decl1))
8890 warning_at (loc, OPT_Wmissing_prototypes,
8891 "no previous prototype for %qD", decl1);
8892 /* Optionally warn of any def with no previous prototype
8893 if the function has already been used. */
8894 else if (warn_missing_prototypes
8895 && old_decl != NULL_TREE
8896 && old_decl != error_mark_node
8897 && TREE_USED (old_decl)
8898 && !prototype_p (TREE_TYPE (old_decl)))
8899 warning_at (loc, OPT_Wmissing_prototypes,
8900 "%qD was used with no prototype before its definition", decl1);
8901 /* Optionally warn of any global def with no previous declaration. */
8902 else if (warn_missing_declarations
8903 && TREE_PUBLIC (decl1)
8904 && old_decl == NULL_TREE
8905 && !MAIN_NAME_P (DECL_NAME (decl1))
8906 && !DECL_DECLARED_INLINE_P (decl1))
8907 warning_at (loc, OPT_Wmissing_declarations,
8908 "no previous declaration for %qD",
8909 decl1);
8910 /* Optionally warn of any def with no previous declaration
8911 if the function has already been used. */
8912 else if (warn_missing_declarations
8913 && old_decl != NULL_TREE
8914 && old_decl != error_mark_node
8915 && TREE_USED (old_decl)
8916 && C_DECL_IMPLICIT (old_decl))
8917 warning_at (loc, OPT_Wmissing_declarations,
8918 "%qD was used with no declaration before its definition", decl1);
8920 /* This function exists in static storage.
8921 (This does not mean `static' in the C sense!) */
8922 TREE_STATIC (decl1) = 1;
8924 /* This is the earliest point at which we might know the assembler
8925 name of the function. Thus, if it's set before this, die horribly. */
8926 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8928 /* If #pragma weak was used, mark the decl weak now. */
8929 if (current_scope == file_scope)
8930 maybe_apply_pragma_weak (decl1);
8932 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8933 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8935 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8936 != integer_type_node)
8937 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8938 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8939 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8940 decl1);
8942 check_main_parameter_types (decl1);
8944 if (!TREE_PUBLIC (decl1))
8945 pedwarn (loc, OPT_Wmain,
8946 "%qD is normally a non-static function", decl1);
8949 /* Record the decl so that the function name is defined.
8950 If we already have a decl for this name, and it is a FUNCTION_DECL,
8951 use the old decl. */
8953 current_function_decl = pushdecl (decl1);
8955 push_scope ();
8956 declare_parm_level ();
8958 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8959 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8960 DECL_ARTIFICIAL (resdecl) = 1;
8961 DECL_IGNORED_P (resdecl) = 1;
8962 DECL_RESULT (current_function_decl) = resdecl;
8964 start_fname_decls ();
8966 return true;
8969 /* Subroutine of store_parm_decls which handles new-style function
8970 definitions (prototype format). The parms already have decls, so we
8971 need only record them as in effect and complain if any redundant
8972 old-style parm decls were written. */
8973 static void
8974 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8976 tree decl;
8977 c_arg_tag *tag;
8978 unsigned ix;
8980 if (current_scope->bindings)
8982 error_at (DECL_SOURCE_LOCATION (fndecl),
8983 "old-style parameter declarations in prototyped "
8984 "function definition");
8986 /* Get rid of the old-style declarations. */
8987 pop_scope ();
8988 push_scope ();
8990 /* Don't issue this warning for nested functions, and don't issue this
8991 warning if we got here because ARG_INFO_TYPES was error_mark_node
8992 (this happens when a function definition has just an ellipsis in
8993 its parameter list). */
8994 else if (!in_system_header_at (input_location)
8995 && !current_function_scope
8996 && arg_info->types != error_mark_node)
8997 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8998 "traditional C rejects ISO C style function definitions");
9000 /* Now make all the parameter declarations visible in the function body.
9001 We can bypass most of the grunt work of pushdecl. */
9002 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
9004 DECL_CONTEXT (decl) = current_function_decl;
9005 if (DECL_NAME (decl))
9007 bind (DECL_NAME (decl), decl, current_scope,
9008 /*invisible=*/false, /*nested=*/false,
9009 UNKNOWN_LOCATION);
9010 if (!TREE_USED (decl))
9011 warn_if_shadowing (decl);
9013 else
9014 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9017 /* Record the parameter list in the function declaration. */
9018 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9020 /* Now make all the ancillary declarations visible, likewise. */
9021 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9023 DECL_CONTEXT (decl) = current_function_decl;
9024 if (DECL_NAME (decl))
9025 bind (DECL_NAME (decl), decl, current_scope,
9026 /*invisible=*/false,
9027 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9028 UNKNOWN_LOCATION);
9031 /* And all the tag declarations. */
9032 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9033 if (tag->id)
9034 bind (tag->id, tag->type, current_scope,
9035 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9038 /* Subroutine of store_parm_decls which handles old-style function
9039 definitions (separate parameter list and declarations). */
9041 static void
9042 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9044 struct c_binding *b;
9045 tree parm, decl, last;
9046 tree parmids = arg_info->parms;
9047 hash_set<tree> seen_args;
9049 if (!in_system_header_at (input_location))
9050 warning_at (DECL_SOURCE_LOCATION (fndecl),
9051 OPT_Wold_style_definition, "old-style function definition");
9053 /* Match each formal parameter name with its declaration. Save each
9054 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9055 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9057 if (TREE_VALUE (parm) == NULL_TREE)
9059 error_at (DECL_SOURCE_LOCATION (fndecl),
9060 "parameter name missing from parameter list");
9061 TREE_PURPOSE (parm) = NULL_TREE;
9062 continue;
9065 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9066 if (b && B_IN_CURRENT_SCOPE (b))
9068 decl = b->decl;
9069 /* Skip erroneous parameters. */
9070 if (decl == error_mark_node)
9071 continue;
9072 /* If we got something other than a PARM_DECL it is an error. */
9073 if (TREE_CODE (decl) != PARM_DECL)
9075 error_at (DECL_SOURCE_LOCATION (decl),
9076 "%qD declared as a non-parameter", decl);
9077 continue;
9079 /* If the declaration is already marked, we have a duplicate
9080 name. Complain and ignore the duplicate. */
9081 else if (seen_args.contains (decl))
9083 error_at (DECL_SOURCE_LOCATION (decl),
9084 "multiple parameters named %qD", decl);
9085 TREE_PURPOSE (parm) = NULL_TREE;
9086 continue;
9088 /* If the declaration says "void", complain and turn it into
9089 an int. */
9090 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9092 error_at (DECL_SOURCE_LOCATION (decl),
9093 "parameter %qD declared with void type", decl);
9094 TREE_TYPE (decl) = integer_type_node;
9095 DECL_ARG_TYPE (decl) = integer_type_node;
9096 layout_decl (decl, 0);
9098 warn_if_shadowing (decl);
9100 /* If no declaration found, default to int. */
9101 else
9103 /* FIXME diagnostics: This should be the location of the argument,
9104 not the FNDECL. E.g., for an old-style declaration
9106 int f10(v) { blah; }
9108 We should use the location of the V, not the F10.
9109 Unfortunately, the V is an IDENTIFIER_NODE which has no
9110 location. In the future we need locations for c_arg_info
9111 entries.
9113 See gcc.dg/Wshadow-3.c for an example of this problem. */
9114 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9115 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9116 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9117 pushdecl (decl);
9118 warn_if_shadowing (decl);
9120 if (flag_isoc99)
9121 pedwarn (DECL_SOURCE_LOCATION (decl),
9122 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9123 decl);
9124 else
9125 warning_at (DECL_SOURCE_LOCATION (decl),
9126 OPT_Wmissing_parameter_type,
9127 "type of %qD defaults to %<int%>", decl);
9130 TREE_PURPOSE (parm) = decl;
9131 seen_args.add (decl);
9134 /* Now examine the parms chain for incomplete declarations
9135 and declarations with no corresponding names. */
9137 for (b = current_scope->bindings; b; b = b->prev)
9139 parm = b->decl;
9140 if (TREE_CODE (parm) != PARM_DECL)
9141 continue;
9143 if (TREE_TYPE (parm) != error_mark_node
9144 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9146 error_at (DECL_SOURCE_LOCATION (parm),
9147 "parameter %qD has incomplete type", parm);
9148 TREE_TYPE (parm) = error_mark_node;
9151 if (!seen_args.contains (parm))
9153 error_at (DECL_SOURCE_LOCATION (parm),
9154 "declaration for parameter %qD but no such parameter",
9155 parm);
9157 /* Pretend the parameter was not missing.
9158 This gets us to a standard state and minimizes
9159 further error messages. */
9160 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9164 /* Chain the declarations together in the order of the list of
9165 names. Store that chain in the function decl, replacing the
9166 list of names. Update the current scope to match. */
9167 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9169 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9170 if (TREE_PURPOSE (parm))
9171 break;
9172 if (parm && TREE_PURPOSE (parm))
9174 last = TREE_PURPOSE (parm);
9175 DECL_ARGUMENTS (fndecl) = last;
9177 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9178 if (TREE_PURPOSE (parm))
9180 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9181 last = TREE_PURPOSE (parm);
9183 DECL_CHAIN (last) = NULL_TREE;
9186 /* If there was a previous prototype,
9187 set the DECL_ARG_TYPE of each argument according to
9188 the type previously specified, and report any mismatches. */
9190 if (current_function_prototype_arg_types)
9192 tree type;
9193 for (parm = DECL_ARGUMENTS (fndecl),
9194 type = current_function_prototype_arg_types;
9195 parm || (type != NULL_TREE
9196 && TREE_VALUE (type) != error_mark_node
9197 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9198 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9200 if (parm == NULL_TREE
9201 || type == NULL_TREE
9202 || (TREE_VALUE (type) != error_mark_node
9203 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9205 if (current_function_prototype_built_in)
9206 warning_at (DECL_SOURCE_LOCATION (fndecl),
9207 0, "number of arguments doesn%'t match "
9208 "built-in prototype");
9209 else
9211 /* FIXME diagnostics: This should be the location of
9212 FNDECL, but there is bug when a prototype is
9213 declared inside function context, but defined
9214 outside of it (e.g., gcc.dg/pr15698-2.c). In
9215 which case FNDECL gets the location of the
9216 prototype, not the definition. */
9217 error_at (input_location,
9218 "number of arguments doesn%'t match prototype");
9220 error_at (current_function_prototype_locus,
9221 "prototype declaration");
9223 break;
9225 /* Type for passing arg must be consistent with that
9226 declared for the arg. ISO C says we take the unqualified
9227 type for parameters declared with qualified type. */
9228 if (TREE_TYPE (parm) != error_mark_node
9229 && TREE_VALUE (type) != error_mark_node
9230 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9231 != TYPE_ATOMIC (TREE_VALUE (type)))
9232 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9233 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9235 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9236 == TYPE_ATOMIC (TREE_VALUE (type)))
9237 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9238 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9240 /* Adjust argument to match prototype. E.g. a previous
9241 `int foo(float);' prototype causes
9242 `int foo(x) float x; {...}' to be treated like
9243 `int foo(float x) {...}'. This is particularly
9244 useful for argument types like uid_t. */
9245 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9247 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9248 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9249 && (TYPE_PRECISION (TREE_TYPE (parm))
9250 < TYPE_PRECISION (integer_type_node)))
9251 DECL_ARG_TYPE (parm)
9252 = c_type_promotes_to (TREE_TYPE (parm));
9254 /* ??? Is it possible to get here with a
9255 built-in prototype or will it always have
9256 been diagnosed as conflicting with an
9257 old-style definition and discarded? */
9258 if (current_function_prototype_built_in)
9259 warning_at (DECL_SOURCE_LOCATION (parm),
9260 OPT_Wpedantic, "promoted argument %qD "
9261 "doesn%'t match built-in prototype", parm);
9262 else
9264 pedwarn (DECL_SOURCE_LOCATION (parm),
9265 OPT_Wpedantic, "promoted argument %qD "
9266 "doesn%'t match prototype", parm);
9267 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9268 "prototype declaration");
9271 else
9273 if (current_function_prototype_built_in)
9274 warning_at (DECL_SOURCE_LOCATION (parm),
9275 0, "argument %qD doesn%'t match "
9276 "built-in prototype", parm);
9277 else
9279 error_at (DECL_SOURCE_LOCATION (parm),
9280 "argument %qD doesn%'t match prototype", parm);
9281 error_at (current_function_prototype_locus,
9282 "prototype declaration");
9287 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9290 /* Otherwise, create a prototype that would match. */
9292 else
9294 tree actual = NULL_TREE, last = NULL_TREE, type;
9296 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9298 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9299 if (last)
9300 TREE_CHAIN (last) = type;
9301 else
9302 actual = type;
9303 last = type;
9305 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9306 if (last)
9307 TREE_CHAIN (last) = type;
9308 else
9309 actual = type;
9311 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9312 of the type of this function, but we need to avoid having this
9313 affect the types of other similarly-typed functions, so we must
9314 first force the generation of an identical (but separate) type
9315 node for the relevant function type. The new node we create
9316 will be a variant of the main variant of the original function
9317 type. */
9319 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9321 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9325 /* Store parameter declarations passed in ARG_INFO into the current
9326 function declaration. */
9328 void
9329 store_parm_decls_from (struct c_arg_info *arg_info)
9331 current_function_arg_info = arg_info;
9332 store_parm_decls ();
9335 /* Called by walk_tree to look for and update context-less labels. */
9337 static tree
9338 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9340 if (TREE_CODE (*tp) == LABEL_EXPR
9341 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9343 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9344 *walk_subtrees = 0;
9347 return NULL_TREE;
9350 /* Store the parameter declarations into the current function declaration.
9351 This is called after parsing the parameter declarations, before
9352 digesting the body of the function.
9354 For an old-style definition, construct a prototype out of the old-style
9355 parameter declarations and inject it into the function's type. */
9357 void
9358 store_parm_decls (void)
9360 tree fndecl = current_function_decl;
9361 bool proto;
9363 /* The argument information block for FNDECL. */
9364 struct c_arg_info *arg_info = current_function_arg_info;
9365 current_function_arg_info = 0;
9367 /* True if this definition is written with a prototype. Note:
9368 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9369 list in a function definition as equivalent to (void) -- an
9370 empty argument list specifies the function has no parameters,
9371 but only (void) sets up a prototype for future calls. */
9372 proto = arg_info->types != 0;
9374 if (proto)
9375 store_parm_decls_newstyle (fndecl, arg_info);
9376 else
9377 store_parm_decls_oldstyle (fndecl, arg_info);
9379 /* The next call to push_scope will be a function body. */
9381 next_is_function_body = true;
9383 /* Write a record describing this function definition to the prototypes
9384 file (if requested). */
9386 gen_aux_info_record (fndecl, 1, 0, proto);
9388 /* Initialize the RTL code for the function. */
9389 allocate_struct_function (fndecl, false);
9391 if (warn_unused_local_typedefs)
9392 cfun->language = ggc_cleared_alloc<language_function> ();
9394 /* Begin the statement tree for this function. */
9395 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9397 /* ??? Insert the contents of the pending sizes list into the function
9398 to be evaluated. The only reason left to have this is
9399 void foo(int n, int array[n++])
9400 because we throw away the array type in favor of a pointer type, and
9401 thus won't naturally see the SAVE_EXPR containing the increment. All
9402 other pending sizes would be handled by gimplify_parameters. */
9403 if (arg_info->pending_sizes)
9405 /* In very special circumstances, e.g. for code like
9406 _Atomic int i = 5;
9407 void f (int a[i += 2]) {}
9408 we need to execute the atomic assignment on function entry.
9409 But in this case, it is not just a straight store, it has the
9410 op= form, which means that build_atomic_assign has generated
9411 gotos, labels, etc. Because at that time the function decl
9412 for F has not been created yet, those labels do not have any
9413 function context. But we have the fndecl now, so update the
9414 labels accordingly. gimplify_expr would crash otherwise. */
9415 walk_tree_without_duplicates (&arg_info->pending_sizes,
9416 set_labels_context_r, fndecl);
9417 add_stmt (arg_info->pending_sizes);
9421 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9422 c_finish_omp_declare_simd for function prototypes. No diagnostics
9423 should be done. */
9425 void
9426 temp_store_parm_decls (tree fndecl, tree parms)
9428 push_scope ();
9429 for (tree p = parms; p; p = DECL_CHAIN (p))
9431 DECL_CONTEXT (p) = fndecl;
9432 if (DECL_NAME (p))
9433 bind (DECL_NAME (p), p, current_scope,
9434 /*invisible=*/false, /*nested=*/false,
9435 UNKNOWN_LOCATION);
9439 /* Undo what temp_store_parm_decls did. */
9441 void
9442 temp_pop_parm_decls (void)
9444 /* Clear all bindings in this temporary scope, so that
9445 pop_scope doesn't create a BLOCK. */
9446 struct c_binding *b = current_scope->bindings;
9447 current_scope->bindings = NULL;
9448 for (; b; b = free_binding_and_advance (b))
9450 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9451 || b->decl == error_mark_node);
9452 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9453 I_SYMBOL_BINDING (b->id) = b->shadowed;
9454 if (b->shadowed && b->shadowed->u.type)
9455 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9457 pop_scope ();
9461 /* Finish up a function declaration and compile that function
9462 all the way to assembler language output. Then free the storage
9463 for the function definition.
9465 This is called after parsing the body of the function definition. */
9467 void
9468 finish_function (void)
9470 tree fndecl = current_function_decl;
9472 if (c_dialect_objc ())
9473 objc_finish_function ();
9475 if (TREE_CODE (fndecl) == FUNCTION_DECL
9476 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9478 tree args = DECL_ARGUMENTS (fndecl);
9479 for (; args; args = DECL_CHAIN (args))
9481 tree type = TREE_TYPE (args);
9482 if (INTEGRAL_TYPE_P (type)
9483 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9484 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9488 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9489 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9491 /* Must mark the RESULT_DECL as being in this function. */
9493 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9494 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9496 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9497 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9498 == integer_type_node && flag_isoc99)
9500 /* Hack. We don't want the middle-end to warn that this return
9501 is unreachable, so we mark its location as special. Using
9502 UNKNOWN_LOCATION has the problem that it gets clobbered in
9503 annotate_one_with_locus. A cleaner solution might be to
9504 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9506 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9509 /* Tie off the statement tree for this function. */
9510 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9512 finish_fname_decls ();
9514 /* Complain if there's no return statement only if option specified on
9515 command line. */
9516 if (warn_return_type > 0
9517 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9518 && !current_function_returns_value && !current_function_returns_null
9519 /* Don't complain if we are no-return. */
9520 && !current_function_returns_abnormally
9521 /* Don't complain if we are declared noreturn. */
9522 && !TREE_THIS_VOLATILE (fndecl)
9523 /* Don't warn for main(). */
9524 && !MAIN_NAME_P (DECL_NAME (fndecl))
9525 /* Or if they didn't actually specify a return type. */
9526 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9527 /* Normally, with -Wreturn-type, flow will complain, but we might
9528 optimize out static functions. */
9529 && !TREE_PUBLIC (fndecl))
9531 warning (OPT_Wreturn_type,
9532 "no return statement in function returning non-void");
9533 TREE_NO_WARNING (fndecl) = 1;
9536 /* Complain about parameters that are only set, but never otherwise used. */
9537 if (warn_unused_but_set_parameter)
9539 tree decl;
9541 for (decl = DECL_ARGUMENTS (fndecl);
9542 decl;
9543 decl = DECL_CHAIN (decl))
9544 if (TREE_USED (decl)
9545 && TREE_CODE (decl) == PARM_DECL
9546 && !DECL_READ_P (decl)
9547 && DECL_NAME (decl)
9548 && !DECL_ARTIFICIAL (decl)
9549 && !TREE_NO_WARNING (decl))
9550 warning_at (DECL_SOURCE_LOCATION (decl),
9551 OPT_Wunused_but_set_parameter,
9552 "parameter %qD set but not used", decl);
9555 /* Complain about locally defined typedefs that are not used in this
9556 function. */
9557 maybe_warn_unused_local_typedefs ();
9559 /* Possibly warn about unused parameters. */
9560 if (warn_unused_parameter)
9561 do_warn_unused_parameter (fndecl);
9563 /* Store the end of the function, so that we get good line number
9564 info for the epilogue. */
9565 cfun->function_end_locus = input_location;
9567 /* Finalize the ELF visibility for the function. */
9568 c_determine_visibility (fndecl);
9570 /* For GNU C extern inline functions disregard inline limits. */
9571 if (DECL_EXTERNAL (fndecl)
9572 && DECL_DECLARED_INLINE_P (fndecl)
9573 && (flag_gnu89_inline
9574 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9575 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9577 /* Genericize before inlining. Delay genericizing nested functions
9578 until their parent function is genericized. Since finalizing
9579 requires GENERIC, delay that as well. */
9581 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9582 && !undef_nested_function)
9584 if (!decl_function_context (fndecl))
9586 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9587 c_genericize (fndecl);
9589 /* ??? Objc emits functions after finalizing the compilation unit.
9590 This should be cleaned up later and this conditional removed. */
9591 if (symtab->global_info_ready)
9593 cgraph_node::add_new_function (fndecl, false);
9594 return;
9596 cgraph_node::finalize_function (fndecl, false);
9598 else
9600 /* Register this function with cgraph just far enough to get it
9601 added to our parent's nested function list. Handy, since the
9602 C front end doesn't have such a list. */
9603 (void) cgraph_node::get_create (fndecl);
9607 if (!decl_function_context (fndecl))
9608 undef_nested_function = false;
9610 if (cfun->language != NULL)
9612 ggc_free (cfun->language);
9613 cfun->language = NULL;
9616 /* We're leaving the context of this function, so zap cfun.
9617 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9618 tree_rest_of_compilation. */
9619 set_cfun (NULL);
9620 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9621 current_function_decl = NULL;
9624 /* Check the declarations given in a for-loop for satisfying the C99
9625 constraints. If exactly one such decl is found, return it. LOC is
9626 the location of the opening parenthesis of the for loop. The last
9627 parameter allows you to control the "for loop initial declarations
9628 are only allowed in C99 mode". Normally, you should pass
9629 flag_isoc99 as that parameter. But in some cases (Objective-C
9630 foreach loop, for example) we want to run the checks in this
9631 function even if not in C99 mode, so we allow the caller to turn
9632 off the error about not being in C99 mode.
9635 tree
9636 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9638 struct c_binding *b;
9639 tree one_decl = NULL_TREE;
9640 int n_decls = 0;
9642 if (!turn_off_iso_c99_error)
9644 static bool hint = true;
9645 /* If we get here, declarations have been used in a for loop without
9646 the C99 for loop scope. This doesn't make much sense, so don't
9647 allow it. */
9648 error_at (loc, "%<for%> loop initial declarations "
9649 "are only allowed in C99 or C11 mode");
9650 if (hint)
9652 inform (loc,
9653 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9654 "to compile your code");
9655 hint = false;
9657 return NULL_TREE;
9659 else
9660 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
9661 "initial declarations");
9663 /* C99 subclause 6.8.5 paragraph 3:
9665 [#3] The declaration part of a for statement shall only
9666 declare identifiers for objects having storage class auto or
9667 register.
9669 It isn't clear whether, in this sentence, "identifiers" binds to
9670 "shall only declare" or to "objects" - that is, whether all identifiers
9671 declared must be identifiers for objects, or whether the restriction
9672 only applies to those that are. (A question on this in comp.std.c
9673 in November 2000 received no answer.) We implement the strictest
9674 interpretation, to avoid creating an extension which later causes
9675 problems. */
9677 for (b = current_scope->bindings; b; b = b->prev)
9679 tree id = b->id;
9680 tree decl = b->decl;
9682 if (!id)
9683 continue;
9685 switch (TREE_CODE (decl))
9687 case VAR_DECL:
9689 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9690 if (TREE_STATIC (decl))
9691 error_at (decl_loc,
9692 "declaration of static variable %qD in %<for%> loop "
9693 "initial declaration", decl);
9694 else if (DECL_EXTERNAL (decl))
9695 error_at (decl_loc,
9696 "declaration of %<extern%> variable %qD in %<for%> loop "
9697 "initial declaration", decl);
9699 break;
9701 case RECORD_TYPE:
9702 error_at (loc,
9703 "%<struct %E%> declared in %<for%> loop initial "
9704 "declaration", id);
9705 break;
9706 case UNION_TYPE:
9707 error_at (loc,
9708 "%<union %E%> declared in %<for%> loop initial declaration",
9709 id);
9710 break;
9711 case ENUMERAL_TYPE:
9712 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9713 "initial declaration", id);
9714 break;
9715 default:
9716 error_at (loc, "declaration of non-variable "
9717 "%qD in %<for%> loop initial declaration", decl);
9720 n_decls++;
9721 one_decl = decl;
9724 return n_decls == 1 ? one_decl : NULL_TREE;
9727 /* Save and reinitialize the variables
9728 used during compilation of a C function. */
9730 void
9731 c_push_function_context (void)
9733 struct language_function *p = cfun->language;
9734 /* cfun->language might have been already allocated by the use of
9735 -Wunused-local-typedefs. In that case, just re-use it. */
9736 if (p == NULL)
9737 cfun->language = p = ggc_cleared_alloc<language_function> ();
9739 p->base.x_stmt_tree = c_stmt_tree;
9740 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9741 p->x_break_label = c_break_label;
9742 p->x_cont_label = c_cont_label;
9743 p->x_switch_stack = c_switch_stack;
9744 p->arg_info = current_function_arg_info;
9745 p->returns_value = current_function_returns_value;
9746 p->returns_null = current_function_returns_null;
9747 p->returns_abnormally = current_function_returns_abnormally;
9748 p->warn_about_return_type = warn_about_return_type;
9750 push_function_context ();
9753 /* Restore the variables used during compilation of a C function. */
9755 void
9756 c_pop_function_context (void)
9758 struct language_function *p;
9760 pop_function_context ();
9761 p = cfun->language;
9763 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9764 used to store data throughout the life time of the current cfun,
9765 So don't deallocate it. */
9766 if (!warn_unused_local_typedefs)
9767 cfun->language = NULL;
9769 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9770 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9772 /* Stop pointing to the local nodes about to be freed. */
9773 /* But DECL_INITIAL must remain nonzero so we know this
9774 was an actual function definition. */
9775 DECL_INITIAL (current_function_decl) = error_mark_node;
9776 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9779 c_stmt_tree = p->base.x_stmt_tree;
9780 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9781 c_break_label = p->x_break_label;
9782 c_cont_label = p->x_cont_label;
9783 c_switch_stack = p->x_switch_stack;
9784 current_function_arg_info = p->arg_info;
9785 current_function_returns_value = p->returns_value;
9786 current_function_returns_null = p->returns_null;
9787 current_function_returns_abnormally = p->returns_abnormally;
9788 warn_about_return_type = p->warn_about_return_type;
9791 /* The functions below are required for functionality of doing
9792 function at once processing in the C front end. Currently these
9793 functions are not called from anywhere in the C front end, but as
9794 these changes continue, that will change. */
9796 /* Returns the stmt_tree (if any) to which statements are currently
9797 being added. If there is no active statement-tree, NULL is
9798 returned. */
9800 stmt_tree
9801 current_stmt_tree (void)
9803 return &c_stmt_tree;
9806 /* Return the global value of T as a symbol. */
9808 tree
9809 identifier_global_value (tree t)
9811 struct c_binding *b;
9813 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9814 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9815 return b->decl;
9817 return NULL_TREE;
9820 /* In C, the only C-linkage public declaration is at file scope. */
9822 tree
9823 c_linkage_bindings (tree name)
9825 return identifier_global_value (name);
9828 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9829 otherwise the name is found in ridpointers from RID_INDEX. */
9831 void
9832 record_builtin_type (enum rid rid_index, const char *name, tree type)
9834 tree id, decl;
9835 if (name == 0)
9836 id = ridpointers[(int) rid_index];
9837 else
9838 id = get_identifier (name);
9839 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9840 pushdecl (decl);
9841 if (debug_hooks->type_decl)
9842 debug_hooks->type_decl (decl, false);
9845 /* Build the void_list_node (void_type_node having been created). */
9846 tree
9847 build_void_list_node (void)
9849 tree t = build_tree_list (NULL_TREE, void_type_node);
9850 return t;
9853 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9855 struct c_parm *
9856 build_c_parm (struct c_declspecs *specs, tree attrs,
9857 struct c_declarator *declarator,
9858 location_t loc)
9860 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9861 ret->specs = specs;
9862 ret->attrs = attrs;
9863 ret->declarator = declarator;
9864 ret->loc = loc;
9865 return ret;
9868 /* Return a declarator with nested attributes. TARGET is the inner
9869 declarator to which these attributes apply. ATTRS are the
9870 attributes. */
9872 struct c_declarator *
9873 build_attrs_declarator (tree attrs, struct c_declarator *target)
9875 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9876 ret->kind = cdk_attrs;
9877 ret->declarator = target;
9878 ret->u.attrs = attrs;
9879 return ret;
9882 /* Return a declarator for a function with arguments specified by ARGS
9883 and return type specified by TARGET. */
9885 struct c_declarator *
9886 build_function_declarator (struct c_arg_info *args,
9887 struct c_declarator *target)
9889 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9890 ret->kind = cdk_function;
9891 ret->declarator = target;
9892 ret->u.arg_info = args;
9893 return ret;
9896 /* Return a declarator for the identifier IDENT (which may be
9897 NULL_TREE for an abstract declarator). */
9899 struct c_declarator *
9900 build_id_declarator (tree ident)
9902 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9903 ret->kind = cdk_id;
9904 ret->declarator = 0;
9905 ret->u.id = ident;
9906 /* Default value - may get reset to a more precise location. */
9907 ret->id_loc = input_location;
9908 return ret;
9911 /* Return something to represent absolute declarators containing a *.
9912 TARGET is the absolute declarator that the * contains.
9913 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9914 to apply to the pointer type. */
9916 struct c_declarator *
9917 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9918 struct c_declarator *target)
9920 tree attrs;
9921 int quals = 0;
9922 struct c_declarator *itarget = target;
9923 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9924 if (type_quals_attrs)
9926 attrs = type_quals_attrs->attrs;
9927 quals = quals_from_declspecs (type_quals_attrs);
9928 if (attrs != NULL_TREE)
9929 itarget = build_attrs_declarator (attrs, target);
9931 ret->kind = cdk_pointer;
9932 ret->declarator = itarget;
9933 ret->u.pointer_quals = quals;
9934 return ret;
9937 /* Return a pointer to a structure for an empty list of declaration
9938 specifiers. */
9940 struct c_declspecs *
9941 build_null_declspecs (void)
9943 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9944 memset (ret, 0, sizeof *ret);
9945 ret->align_log = -1;
9946 ret->typespec_word = cts_none;
9947 ret->storage_class = csc_none;
9948 ret->expr_const_operands = true;
9949 ret->typespec_kind = ctsk_none;
9950 ret->address_space = ADDR_SPACE_GENERIC;
9951 return ret;
9954 /* Add the address space ADDRSPACE to the declaration specifiers
9955 SPECS, returning SPECS. */
9957 struct c_declspecs *
9958 declspecs_add_addrspace (source_location location,
9959 struct c_declspecs *specs, addr_space_t as)
9961 specs->non_sc_seen_p = true;
9962 specs->declspecs_seen_p = true;
9964 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9965 && specs->address_space != as)
9966 error ("incompatible address space qualifiers %qs and %qs",
9967 c_addr_space_name (as),
9968 c_addr_space_name (specs->address_space));
9969 else
9971 specs->address_space = as;
9972 specs->locations[cdw_address_space] = location;
9974 return specs;
9977 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9978 returning SPECS. */
9980 struct c_declspecs *
9981 declspecs_add_qual (source_location loc,
9982 struct c_declspecs *specs, tree qual)
9984 enum rid i;
9985 bool dupe = false;
9986 specs->non_sc_seen_p = true;
9987 specs->declspecs_seen_p = true;
9988 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9989 && C_IS_RESERVED_WORD (qual));
9990 i = C_RID_CODE (qual);
9991 location_t prev_loc = UNKNOWN_LOCATION;
9992 switch (i)
9994 case RID_CONST:
9995 dupe = specs->const_p;
9996 specs->const_p = true;
9997 prev_loc = specs->locations[cdw_const];
9998 specs->locations[cdw_const] = loc;
9999 break;
10000 case RID_VOLATILE:
10001 dupe = specs->volatile_p;
10002 specs->volatile_p = true;
10003 prev_loc = specs->locations[cdw_volatile];
10004 specs->locations[cdw_volatile] = loc;
10005 break;
10006 case RID_RESTRICT:
10007 dupe = specs->restrict_p;
10008 specs->restrict_p = true;
10009 prev_loc = specs->locations[cdw_restrict];
10010 specs->locations[cdw_restrict] = loc;
10011 break;
10012 case RID_ATOMIC:
10013 dupe = specs->atomic_p;
10014 specs->atomic_p = true;
10015 prev_loc = specs->locations[cdw_atomic];
10016 specs->locations[cdw_atomic] = loc;
10017 break;
10018 default:
10019 gcc_unreachable ();
10021 if (dupe)
10023 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10024 "duplicate %qE declaration specifier", qual);
10025 if (!warned
10026 && warn_duplicate_decl_specifier
10027 && prev_loc >= RESERVED_LOCATION_COUNT
10028 && !from_macro_expansion_at (prev_loc)
10029 && !from_macro_expansion_at (loc))
10030 warning_at (loc, OPT_Wduplicate_decl_specifier,
10031 "duplicate %qE declaration specifier", qual);
10033 return specs;
10036 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10037 returning SPECS. */
10039 struct c_declspecs *
10040 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10041 struct c_typespec spec)
10043 tree type = spec.spec;
10044 specs->non_sc_seen_p = true;
10045 specs->declspecs_seen_p = true;
10046 specs->typespec_kind = spec.kind;
10047 if (TREE_DEPRECATED (type))
10048 specs->deprecated_p = true;
10050 /* Handle type specifier keywords. */
10051 if (TREE_CODE (type) == IDENTIFIER_NODE
10052 && C_IS_RESERVED_WORD (type)
10053 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10055 enum rid i = C_RID_CODE (type);
10056 if (specs->type)
10058 error_at (loc, "two or more data types in declaration specifiers");
10059 return specs;
10061 if ((int) i <= (int) RID_LAST_MODIFIER)
10063 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10064 bool dupe = false;
10065 switch (i)
10067 case RID_LONG:
10068 if (specs->long_long_p)
10070 error_at (loc, "%<long long long%> is too long for GCC");
10071 break;
10073 if (specs->long_p)
10075 if (specs->typespec_word == cts_double)
10077 error_at (loc,
10078 ("both %<long long%> and %<double%> in "
10079 "declaration specifiers"));
10080 break;
10082 pedwarn_c90 (loc, OPT_Wlong_long,
10083 "ISO C90 does not support %<long long%>");
10084 specs->long_long_p = 1;
10085 specs->locations[cdw_long_long] = loc;
10086 break;
10088 if (specs->short_p)
10089 error_at (loc,
10090 ("both %<long%> and %<short%> in "
10091 "declaration specifiers"));
10092 else if (specs->typespec_word == cts_auto_type)
10093 error_at (loc,
10094 ("both %<long%> and %<__auto_type%> in "
10095 "declaration specifiers"));
10096 else if (specs->typespec_word == cts_void)
10097 error_at (loc,
10098 ("both %<long%> and %<void%> in "
10099 "declaration specifiers"));
10100 else if (specs->typespec_word == cts_int_n)
10101 error_at (loc,
10102 ("both %<long%> and %<__int%d%> in "
10103 "declaration specifiers"),
10104 int_n_data[specs->int_n_idx].bitsize);
10105 else if (specs->typespec_word == cts_bool)
10106 error_at (loc,
10107 ("both %<long%> and %<_Bool%> in "
10108 "declaration specifiers"));
10109 else if (specs->typespec_word == cts_char)
10110 error_at (loc,
10111 ("both %<long%> and %<char%> in "
10112 "declaration specifiers"));
10113 else if (specs->typespec_word == cts_float)
10114 error_at (loc,
10115 ("both %<long%> and %<float%> in "
10116 "declaration specifiers"));
10117 else if (specs->typespec_word == cts_floatn_nx)
10118 error_at (loc,
10119 ("both %<long%> and %<_Float%d%s%> in "
10120 "declaration specifiers"),
10121 floatn_nx_types[specs->floatn_nx_idx].n,
10122 (floatn_nx_types[specs->floatn_nx_idx].extended
10123 ? "x"
10124 : ""));
10125 else if (specs->typespec_word == cts_dfloat32)
10126 error_at (loc,
10127 ("both %<long%> and %<_Decimal32%> in "
10128 "declaration specifiers"));
10129 else if (specs->typespec_word == cts_dfloat64)
10130 error_at (loc,
10131 ("both %<long%> and %<_Decimal64%> in "
10132 "declaration specifiers"));
10133 else if (specs->typespec_word == cts_dfloat128)
10134 error_at (loc,
10135 ("both %<long%> and %<_Decimal128%> in "
10136 "declaration specifiers"));
10137 else
10139 specs->long_p = true;
10140 specs->locations[cdw_long] = loc;
10142 break;
10143 case RID_SHORT:
10144 dupe = specs->short_p;
10145 if (specs->long_p)
10146 error_at (loc,
10147 ("both %<long%> and %<short%> in "
10148 "declaration specifiers"));
10149 else if (specs->typespec_word == cts_auto_type)
10150 error_at (loc,
10151 ("both %<short%> and %<__auto_type%> in "
10152 "declaration specifiers"));
10153 else if (specs->typespec_word == cts_void)
10154 error_at (loc,
10155 ("both %<short%> and %<void%> in "
10156 "declaration specifiers"));
10157 else if (specs->typespec_word == cts_int_n)
10158 error_at (loc,
10159 ("both %<short%> and %<__int%d%> in "
10160 "declaration specifiers"),
10161 int_n_data[specs->int_n_idx].bitsize);
10162 else if (specs->typespec_word == cts_bool)
10163 error_at (loc,
10164 ("both %<short%> and %<_Bool%> in "
10165 "declaration specifiers"));
10166 else if (specs->typespec_word == cts_char)
10167 error_at (loc,
10168 ("both %<short%> and %<char%> in "
10169 "declaration specifiers"));
10170 else if (specs->typespec_word == cts_float)
10171 error_at (loc,
10172 ("both %<short%> and %<float%> in "
10173 "declaration specifiers"));
10174 else if (specs->typespec_word == cts_double)
10175 error_at (loc,
10176 ("both %<short%> and %<double%> in "
10177 "declaration specifiers"));
10178 else if (specs->typespec_word == cts_floatn_nx)
10179 error_at (loc,
10180 ("both %<short%> and %<_Float%d%s%> in "
10181 "declaration specifiers"),
10182 floatn_nx_types[specs->floatn_nx_idx].n,
10183 (floatn_nx_types[specs->floatn_nx_idx].extended
10184 ? "x"
10185 : ""));
10186 else if (specs->typespec_word == cts_dfloat32)
10187 error_at (loc,
10188 ("both %<short%> and %<_Decimal32%> in "
10189 "declaration specifiers"));
10190 else if (specs->typespec_word == cts_dfloat64)
10191 error_at (loc,
10192 ("both %<short%> and %<_Decimal64%> in "
10193 "declaration specifiers"));
10194 else if (specs->typespec_word == cts_dfloat128)
10195 error_at (loc,
10196 ("both %<short%> and %<_Decimal128%> in "
10197 "declaration specifiers"));
10198 else
10200 specs->short_p = true;
10201 specs->locations[cdw_short] = loc;
10203 break;
10204 case RID_SIGNED:
10205 dupe = specs->signed_p;
10206 if (specs->unsigned_p)
10207 error_at (loc,
10208 ("both %<signed%> and %<unsigned%> in "
10209 "declaration specifiers"));
10210 else if (specs->typespec_word == cts_auto_type)
10211 error_at (loc,
10212 ("both %<signed%> and %<__auto_type%> in "
10213 "declaration specifiers"));
10214 else if (specs->typespec_word == cts_void)
10215 error_at (loc,
10216 ("both %<signed%> and %<void%> in "
10217 "declaration specifiers"));
10218 else if (specs->typespec_word == cts_bool)
10219 error_at (loc,
10220 ("both %<signed%> and %<_Bool%> in "
10221 "declaration specifiers"));
10222 else if (specs->typespec_word == cts_float)
10223 error_at (loc,
10224 ("both %<signed%> and %<float%> in "
10225 "declaration specifiers"));
10226 else if (specs->typespec_word == cts_double)
10227 error_at (loc,
10228 ("both %<signed%> and %<double%> in "
10229 "declaration specifiers"));
10230 else if (specs->typespec_word == cts_floatn_nx)
10231 error_at (loc,
10232 ("both %<signed%> and %<_Float%d%s%> in "
10233 "declaration specifiers"),
10234 floatn_nx_types[specs->floatn_nx_idx].n,
10235 (floatn_nx_types[specs->floatn_nx_idx].extended
10236 ? "x"
10237 : ""));
10238 else if (specs->typespec_word == cts_dfloat32)
10239 error_at (loc,
10240 ("both %<signed%> and %<_Decimal32%> in "
10241 "declaration specifiers"));
10242 else if (specs->typespec_word == cts_dfloat64)
10243 error_at (loc,
10244 ("both %<signed%> and %<_Decimal64%> in "
10245 "declaration specifiers"));
10246 else if (specs->typespec_word == cts_dfloat128)
10247 error_at (loc,
10248 ("both %<signed%> and %<_Decimal128%> in "
10249 "declaration specifiers"));
10250 else
10252 specs->signed_p = true;
10253 specs->locations[cdw_signed] = loc;
10255 break;
10256 case RID_UNSIGNED:
10257 dupe = specs->unsigned_p;
10258 if (specs->signed_p)
10259 error_at (loc,
10260 ("both %<signed%> and %<unsigned%> in "
10261 "declaration specifiers"));
10262 else if (specs->typespec_word == cts_auto_type)
10263 error_at (loc,
10264 ("both %<unsigned%> and %<__auto_type%> in "
10265 "declaration specifiers"));
10266 else if (specs->typespec_word == cts_void)
10267 error_at (loc,
10268 ("both %<unsigned%> and %<void%> in "
10269 "declaration specifiers"));
10270 else if (specs->typespec_word == cts_bool)
10271 error_at (loc,
10272 ("both %<unsigned%> and %<_Bool%> in "
10273 "declaration specifiers"));
10274 else if (specs->typespec_word == cts_float)
10275 error_at (loc,
10276 ("both %<unsigned%> and %<float%> in "
10277 "declaration specifiers"));
10278 else if (specs->typespec_word == cts_double)
10279 error_at (loc,
10280 ("both %<unsigned%> and %<double%> in "
10281 "declaration specifiers"));
10282 else if (specs->typespec_word == cts_floatn_nx)
10283 error_at (loc,
10284 ("both %<unsigned%> and %<_Float%d%s%> in "
10285 "declaration specifiers"),
10286 floatn_nx_types[specs->floatn_nx_idx].n,
10287 (floatn_nx_types[specs->floatn_nx_idx].extended
10288 ? "x"
10289 : ""));
10290 else if (specs->typespec_word == cts_dfloat32)
10291 error_at (loc,
10292 ("both %<unsigned%> and %<_Decimal32%> in "
10293 "declaration specifiers"));
10294 else if (specs->typespec_word == cts_dfloat64)
10295 error_at (loc,
10296 ("both %<unsigned%> and %<_Decimal64%> in "
10297 "declaration specifiers"));
10298 else if (specs->typespec_word == cts_dfloat128)
10299 error_at (loc,
10300 ("both %<unsigned%> and %<_Decimal128%> in "
10301 "declaration specifiers"));
10302 else
10304 specs->unsigned_p = true;
10305 specs->locations[cdw_unsigned] = loc;
10307 break;
10308 case RID_COMPLEX:
10309 dupe = specs->complex_p;
10310 if (!in_system_header_at (loc))
10311 pedwarn_c90 (loc, OPT_Wpedantic,
10312 "ISO C90 does not support complex types");
10313 if (specs->typespec_word == cts_auto_type)
10314 error_at (loc,
10315 ("both %<complex%> and %<__auto_type%> in "
10316 "declaration specifiers"));
10317 else if (specs->typespec_word == cts_void)
10318 error_at (loc,
10319 ("both %<complex%> and %<void%> in "
10320 "declaration specifiers"));
10321 else if (specs->typespec_word == cts_bool)
10322 error_at (loc,
10323 ("both %<complex%> and %<_Bool%> in "
10324 "declaration specifiers"));
10325 else if (specs->typespec_word == cts_dfloat32)
10326 error_at (loc,
10327 ("both %<complex%> and %<_Decimal32%> in "
10328 "declaration specifiers"));
10329 else if (specs->typespec_word == cts_dfloat64)
10330 error_at (loc,
10331 ("both %<complex%> and %<_Decimal64%> in "
10332 "declaration specifiers"));
10333 else if (specs->typespec_word == cts_dfloat128)
10334 error_at (loc,
10335 ("both %<complex%> and %<_Decimal128%> in "
10336 "declaration specifiers"));
10337 else if (specs->typespec_word == cts_fract)
10338 error_at (loc,
10339 ("both %<complex%> and %<_Fract%> in "
10340 "declaration specifiers"));
10341 else if (specs->typespec_word == cts_accum)
10342 error_at (loc,
10343 ("both %<complex%> and %<_Accum%> in "
10344 "declaration specifiers"));
10345 else if (specs->saturating_p)
10346 error_at (loc,
10347 ("both %<complex%> and %<_Sat%> in "
10348 "declaration specifiers"));
10349 else
10351 specs->complex_p = true;
10352 specs->locations[cdw_complex] = loc;
10354 break;
10355 case RID_SAT:
10356 dupe = specs->saturating_p;
10357 pedwarn (loc, OPT_Wpedantic,
10358 "ISO C does not support saturating types");
10359 if (specs->typespec_word == cts_int_n)
10361 error_at (loc,
10362 ("both %<_Sat%> and %<__int%d%> in "
10363 "declaration specifiers"),
10364 int_n_data[specs->int_n_idx].bitsize);
10366 else if (specs->typespec_word == cts_auto_type)
10367 error_at (loc,
10368 ("both %<_Sat%> and %<__auto_type%> in "
10369 "declaration specifiers"));
10370 else if (specs->typespec_word == cts_void)
10371 error_at (loc,
10372 ("both %<_Sat%> and %<void%> in "
10373 "declaration specifiers"));
10374 else if (specs->typespec_word == cts_bool)
10375 error_at (loc,
10376 ("both %<_Sat%> and %<_Bool%> in "
10377 "declaration specifiers"));
10378 else if (specs->typespec_word == cts_char)
10379 error_at (loc,
10380 ("both %<_Sat%> and %<char%> in "
10381 "declaration specifiers"));
10382 else if (specs->typespec_word == cts_int)
10383 error_at (loc,
10384 ("both %<_Sat%> and %<int%> in "
10385 "declaration specifiers"));
10386 else if (specs->typespec_word == cts_float)
10387 error_at (loc,
10388 ("both %<_Sat%> and %<float%> in "
10389 "declaration specifiers"));
10390 else if (specs->typespec_word == cts_double)
10391 error_at (loc,
10392 ("both %<_Sat%> and %<double%> in "
10393 "declaration specifiers"));
10394 else if (specs->typespec_word == cts_floatn_nx)
10395 error_at (loc,
10396 ("both %<_Sat%> and %<_Float%d%s%> in "
10397 "declaration specifiers"),
10398 floatn_nx_types[specs->floatn_nx_idx].n,
10399 (floatn_nx_types[specs->floatn_nx_idx].extended
10400 ? "x"
10401 : ""));
10402 else if (specs->typespec_word == cts_dfloat32)
10403 error_at (loc,
10404 ("both %<_Sat%> and %<_Decimal32%> in "
10405 "declaration specifiers"));
10406 else if (specs->typespec_word == cts_dfloat64)
10407 error_at (loc,
10408 ("both %<_Sat%> and %<_Decimal64%> in "
10409 "declaration specifiers"));
10410 else if (specs->typespec_word == cts_dfloat128)
10411 error_at (loc,
10412 ("both %<_Sat%> and %<_Decimal128%> in "
10413 "declaration specifiers"));
10414 else if (specs->complex_p)
10415 error_at (loc,
10416 ("both %<_Sat%> and %<complex%> in "
10417 "declaration specifiers"));
10418 else
10420 specs->saturating_p = true;
10421 specs->locations[cdw_saturating] = loc;
10423 break;
10424 default:
10425 gcc_unreachable ();
10428 if (dupe)
10429 error_at (loc, "duplicate %qE", type);
10431 return specs;
10433 else
10435 /* "void", "_Bool", "char", "int", "float", "double",
10436 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10437 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10438 "__auto_type". */
10439 if (specs->typespec_word != cts_none)
10441 error_at (loc,
10442 "two or more data types in declaration specifiers");
10443 return specs;
10445 switch (i)
10447 case RID_AUTO_TYPE:
10448 if (specs->long_p)
10449 error_at (loc,
10450 ("both %<long%> and %<__auto_type%> in "
10451 "declaration specifiers"));
10452 else if (specs->short_p)
10453 error_at (loc,
10454 ("both %<short%> and %<__auto_type%> in "
10455 "declaration specifiers"));
10456 else if (specs->signed_p)
10457 error_at (loc,
10458 ("both %<signed%> and %<__auto_type%> in "
10459 "declaration specifiers"));
10460 else if (specs->unsigned_p)
10461 error_at (loc,
10462 ("both %<unsigned%> and %<__auto_type%> in "
10463 "declaration specifiers"));
10464 else if (specs->complex_p)
10465 error_at (loc,
10466 ("both %<complex%> and %<__auto_type%> in "
10467 "declaration specifiers"));
10468 else if (specs->saturating_p)
10469 error_at (loc,
10470 ("both %<_Sat%> and %<__auto_type%> in "
10471 "declaration specifiers"));
10472 else
10474 specs->typespec_word = cts_auto_type;
10475 specs->locations[cdw_typespec] = loc;
10477 return specs;
10478 case RID_INT_N_0:
10479 case RID_INT_N_1:
10480 case RID_INT_N_2:
10481 case RID_INT_N_3:
10482 specs->int_n_idx = i - RID_INT_N_0;
10483 if (!in_system_header_at (input_location))
10484 pedwarn (loc, OPT_Wpedantic,
10485 "ISO C does not support %<__int%d%> types",
10486 int_n_data[specs->int_n_idx].bitsize);
10488 if (specs->long_p)
10489 error_at (loc,
10490 ("both %<__int%d%> and %<long%> in "
10491 "declaration specifiers"),
10492 int_n_data[specs->int_n_idx].bitsize);
10493 else if (specs->saturating_p)
10494 error_at (loc,
10495 ("both %<_Sat%> and %<__int%d%> in "
10496 "declaration specifiers"),
10497 int_n_data[specs->int_n_idx].bitsize);
10498 else if (specs->short_p)
10499 error_at (loc,
10500 ("both %<__int%d%> and %<short%> in "
10501 "declaration specifiers"),
10502 int_n_data[specs->int_n_idx].bitsize);
10503 else if (! int_n_enabled_p[specs->int_n_idx])
10505 specs->typespec_word = cts_int_n;
10506 error_at (loc,
10507 "%<__int%d%> is not supported on this target",
10508 int_n_data[specs->int_n_idx].bitsize);
10510 else
10512 specs->typespec_word = cts_int_n;
10513 specs->locations[cdw_typespec] = loc;
10515 return specs;
10516 case RID_VOID:
10517 if (specs->long_p)
10518 error_at (loc,
10519 ("both %<long%> and %<void%> in "
10520 "declaration specifiers"));
10521 else if (specs->short_p)
10522 error_at (loc,
10523 ("both %<short%> and %<void%> in "
10524 "declaration specifiers"));
10525 else if (specs->signed_p)
10526 error_at (loc,
10527 ("both %<signed%> and %<void%> in "
10528 "declaration specifiers"));
10529 else if (specs->unsigned_p)
10530 error_at (loc,
10531 ("both %<unsigned%> and %<void%> in "
10532 "declaration specifiers"));
10533 else if (specs->complex_p)
10534 error_at (loc,
10535 ("both %<complex%> and %<void%> in "
10536 "declaration specifiers"));
10537 else if (specs->saturating_p)
10538 error_at (loc,
10539 ("both %<_Sat%> and %<void%> in "
10540 "declaration specifiers"));
10541 else
10543 specs->typespec_word = cts_void;
10544 specs->locations[cdw_typespec] = loc;
10546 return specs;
10547 case RID_BOOL:
10548 if (!in_system_header_at (loc))
10549 pedwarn_c90 (loc, OPT_Wpedantic,
10550 "ISO C90 does not support boolean types");
10551 if (specs->long_p)
10552 error_at (loc,
10553 ("both %<long%> and %<_Bool%> in "
10554 "declaration specifiers"));
10555 else if (specs->short_p)
10556 error_at (loc,
10557 ("both %<short%> and %<_Bool%> in "
10558 "declaration specifiers"));
10559 else if (specs->signed_p)
10560 error_at (loc,
10561 ("both %<signed%> and %<_Bool%> in "
10562 "declaration specifiers"));
10563 else if (specs->unsigned_p)
10564 error_at (loc,
10565 ("both %<unsigned%> and %<_Bool%> in "
10566 "declaration specifiers"));
10567 else if (specs->complex_p)
10568 error_at (loc,
10569 ("both %<complex%> and %<_Bool%> in "
10570 "declaration specifiers"));
10571 else if (specs->saturating_p)
10572 error_at (loc,
10573 ("both %<_Sat%> and %<_Bool%> in "
10574 "declaration specifiers"));
10575 else
10577 specs->typespec_word = cts_bool;
10578 specs->locations[cdw_typespec] = loc;
10580 return specs;
10581 case RID_CHAR:
10582 if (specs->long_p)
10583 error_at (loc,
10584 ("both %<long%> and %<char%> in "
10585 "declaration specifiers"));
10586 else if (specs->short_p)
10587 error_at (loc,
10588 ("both %<short%> and %<char%> in "
10589 "declaration specifiers"));
10590 else if (specs->saturating_p)
10591 error_at (loc,
10592 ("both %<_Sat%> and %<char%> in "
10593 "declaration specifiers"));
10594 else
10596 specs->typespec_word = cts_char;
10597 specs->locations[cdw_typespec] = loc;
10599 return specs;
10600 case RID_INT:
10601 if (specs->saturating_p)
10602 error_at (loc,
10603 ("both %<_Sat%> and %<int%> in "
10604 "declaration specifiers"));
10605 else
10607 specs->typespec_word = cts_int;
10608 specs->locations[cdw_typespec] = loc;
10610 return specs;
10611 case RID_FLOAT:
10612 if (specs->long_p)
10613 error_at (loc,
10614 ("both %<long%> and %<float%> in "
10615 "declaration specifiers"));
10616 else if (specs->short_p)
10617 error_at (loc,
10618 ("both %<short%> and %<float%> in "
10619 "declaration specifiers"));
10620 else if (specs->signed_p)
10621 error_at (loc,
10622 ("both %<signed%> and %<float%> in "
10623 "declaration specifiers"));
10624 else if (specs->unsigned_p)
10625 error_at (loc,
10626 ("both %<unsigned%> and %<float%> in "
10627 "declaration specifiers"));
10628 else if (specs->saturating_p)
10629 error_at (loc,
10630 ("both %<_Sat%> and %<float%> in "
10631 "declaration specifiers"));
10632 else
10634 specs->typespec_word = cts_float;
10635 specs->locations[cdw_typespec] = loc;
10637 return specs;
10638 case RID_DOUBLE:
10639 if (specs->long_long_p)
10640 error_at (loc,
10641 ("both %<long long%> and %<double%> in "
10642 "declaration specifiers"));
10643 else if (specs->short_p)
10644 error_at (loc,
10645 ("both %<short%> and %<double%> in "
10646 "declaration specifiers"));
10647 else if (specs->signed_p)
10648 error_at (loc,
10649 ("both %<signed%> and %<double%> in "
10650 "declaration specifiers"));
10651 else if (specs->unsigned_p)
10652 error_at (loc,
10653 ("both %<unsigned%> and %<double%> in "
10654 "declaration specifiers"));
10655 else if (specs->saturating_p)
10656 error_at (loc,
10657 ("both %<_Sat%> and %<double%> in "
10658 "declaration specifiers"));
10659 else
10661 specs->typespec_word = cts_double;
10662 specs->locations[cdw_typespec] = loc;
10664 return specs;
10665 CASE_RID_FLOATN_NX:
10666 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10667 if (!in_system_header_at (input_location))
10668 pedwarn (loc, OPT_Wpedantic,
10669 "ISO C does not support the %<_Float%d%s%> type",
10670 floatn_nx_types[specs->floatn_nx_idx].n,
10671 (floatn_nx_types[specs->floatn_nx_idx].extended
10672 ? "x"
10673 : ""));
10675 if (specs->long_p)
10676 error_at (loc,
10677 ("both %<long%> and %<_Float%d%s%> in "
10678 "declaration specifiers"),
10679 floatn_nx_types[specs->floatn_nx_idx].n,
10680 (floatn_nx_types[specs->floatn_nx_idx].extended
10681 ? "x"
10682 : ""));
10683 else if (specs->short_p)
10684 error_at (loc,
10685 ("both %<short%> and %<_Float%d%s%> in "
10686 "declaration specifiers"),
10687 floatn_nx_types[specs->floatn_nx_idx].n,
10688 (floatn_nx_types[specs->floatn_nx_idx].extended
10689 ? "x"
10690 : ""));
10691 else if (specs->signed_p)
10692 error_at (loc,
10693 ("both %<signed%> and %<_Float%d%s%> in "
10694 "declaration specifiers"),
10695 floatn_nx_types[specs->floatn_nx_idx].n,
10696 (floatn_nx_types[specs->floatn_nx_idx].extended
10697 ? "x"
10698 : ""));
10699 else if (specs->unsigned_p)
10700 error_at (loc,
10701 ("both %<unsigned%> and %<_Float%d%s%> in "
10702 "declaration specifiers"),
10703 floatn_nx_types[specs->floatn_nx_idx].n,
10704 (floatn_nx_types[specs->floatn_nx_idx].extended
10705 ? "x"
10706 : ""));
10707 else if (specs->saturating_p)
10708 error_at (loc,
10709 ("both %<_Sat%> and %<_Float%d%s%> in "
10710 "declaration specifiers"),
10711 floatn_nx_types[specs->floatn_nx_idx].n,
10712 (floatn_nx_types[specs->floatn_nx_idx].extended
10713 ? "x"
10714 : ""));
10715 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10717 specs->typespec_word = cts_floatn_nx;
10718 error_at (loc,
10719 "%<_Float%d%s%> is not supported on this target",
10720 floatn_nx_types[specs->floatn_nx_idx].n,
10721 (floatn_nx_types[specs->floatn_nx_idx].extended
10722 ? "x"
10723 : ""));
10725 else
10727 specs->typespec_word = cts_floatn_nx;
10728 specs->locations[cdw_typespec] = loc;
10730 return specs;
10731 case RID_DFLOAT32:
10732 case RID_DFLOAT64:
10733 case RID_DFLOAT128:
10735 const char *str;
10736 if (i == RID_DFLOAT32)
10737 str = "_Decimal32";
10738 else if (i == RID_DFLOAT64)
10739 str = "_Decimal64";
10740 else
10741 str = "_Decimal128";
10742 if (specs->long_long_p)
10743 error_at (loc,
10744 ("both %<long long%> and %qs in "
10745 "declaration specifiers"),
10746 str);
10747 if (specs->long_p)
10748 error_at (loc,
10749 ("both %<long%> and %qs in "
10750 "declaration specifiers"),
10751 str);
10752 else if (specs->short_p)
10753 error_at (loc,
10754 ("both %<short%> and %qs in "
10755 "declaration specifiers"),
10756 str);
10757 else if (specs->signed_p)
10758 error_at (loc,
10759 ("both %<signed%> and %qs in "
10760 "declaration specifiers"),
10761 str);
10762 else if (specs->unsigned_p)
10763 error_at (loc,
10764 ("both %<unsigned%> and %qs in "
10765 "declaration specifiers"),
10766 str);
10767 else if (specs->complex_p)
10768 error_at (loc,
10769 ("both %<complex%> and %qs in "
10770 "declaration specifiers"),
10771 str);
10772 else if (specs->saturating_p)
10773 error_at (loc,
10774 ("both %<_Sat%> and %qs in "
10775 "declaration specifiers"),
10776 str);
10777 else if (i == RID_DFLOAT32)
10778 specs->typespec_word = cts_dfloat32;
10779 else if (i == RID_DFLOAT64)
10780 specs->typespec_word = cts_dfloat64;
10781 else
10782 specs->typespec_word = cts_dfloat128;
10783 specs->locations[cdw_typespec] = loc;
10785 if (!targetm.decimal_float_supported_p ())
10786 error_at (loc,
10787 ("decimal floating point not supported "
10788 "for this target"));
10789 pedwarn (loc, OPT_Wpedantic,
10790 "ISO C does not support decimal floating point");
10791 return specs;
10792 case RID_FRACT:
10793 case RID_ACCUM:
10795 const char *str;
10796 if (i == RID_FRACT)
10797 str = "_Fract";
10798 else
10799 str = "_Accum";
10800 if (specs->complex_p)
10801 error_at (loc,
10802 ("both %<complex%> and %qs in "
10803 "declaration specifiers"),
10804 str);
10805 else if (i == RID_FRACT)
10806 specs->typespec_word = cts_fract;
10807 else
10808 specs->typespec_word = cts_accum;
10809 specs->locations[cdw_typespec] = loc;
10811 if (!targetm.fixed_point_supported_p ())
10812 error_at (loc,
10813 "fixed-point types not supported for this target");
10814 pedwarn (loc, OPT_Wpedantic,
10815 "ISO C does not support fixed-point types");
10816 return specs;
10817 default:
10818 /* ObjC reserved word "id", handled below. */
10819 break;
10824 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10825 form of ObjC type, cases such as "int" and "long" being handled
10826 above), a TYPE (struct, union, enum and typeof specifiers) or an
10827 ERROR_MARK. In none of these cases may there have previously
10828 been any type specifiers. */
10829 if (specs->type || specs->typespec_word != cts_none
10830 || specs->long_p || specs->short_p || specs->signed_p
10831 || specs->unsigned_p || specs->complex_p)
10832 error_at (loc, "two or more data types in declaration specifiers");
10833 else if (TREE_CODE (type) == TYPE_DECL)
10835 if (TREE_TYPE (type) == error_mark_node)
10836 ; /* Allow the type to default to int to avoid cascading errors. */
10837 else
10839 specs->type = TREE_TYPE (type);
10840 specs->decl_attr = DECL_ATTRIBUTES (type);
10841 specs->typedef_p = true;
10842 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10843 specs->locations[cdw_typedef] = loc;
10845 /* If this typedef name is defined in a struct, then a C++
10846 lookup would return a different value. */
10847 if (warn_cxx_compat
10848 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10849 warning_at (loc, OPT_Wc___compat,
10850 "C++ lookup of %qD would return a field, not a type",
10851 type);
10853 /* If we are parsing a struct, record that a struct field
10854 used a typedef. */
10855 if (warn_cxx_compat && struct_parse_info != NULL)
10856 struct_parse_info->typedefs_seen.safe_push (type);
10859 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10861 tree t = lookup_name (type);
10862 if (!t || TREE_CODE (t) != TYPE_DECL)
10863 error_at (loc, "%qE fails to be a typedef or built in type", type);
10864 else if (TREE_TYPE (t) == error_mark_node)
10866 else
10868 specs->type = TREE_TYPE (t);
10869 specs->locations[cdw_typespec] = loc;
10872 else
10874 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10876 specs->typedef_p = true;
10877 specs->locations[cdw_typedef] = loc;
10878 if (spec.expr)
10880 if (specs->expr)
10881 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10882 specs->expr, spec.expr);
10883 else
10884 specs->expr = spec.expr;
10885 specs->expr_const_operands &= spec.expr_const_operands;
10888 specs->type = type;
10891 return specs;
10894 /* Add the storage class specifier or function specifier SCSPEC to the
10895 declaration specifiers SPECS, returning SPECS. */
10897 struct c_declspecs *
10898 declspecs_add_scspec (source_location loc,
10899 struct c_declspecs *specs,
10900 tree scspec)
10902 enum rid i;
10903 enum c_storage_class n = csc_none;
10904 bool dupe = false;
10905 specs->declspecs_seen_p = true;
10906 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10907 && C_IS_RESERVED_WORD (scspec));
10908 i = C_RID_CODE (scspec);
10909 if (specs->non_sc_seen_p)
10910 warning (OPT_Wold_style_declaration,
10911 "%qE is not at beginning of declaration", scspec);
10912 switch (i)
10914 case RID_INLINE:
10915 /* C99 permits duplicate inline. Although of doubtful utility,
10916 it seems simplest to permit it in gnu89 mode as well, as
10917 there is also little utility in maintaining this as a
10918 difference between gnu89 and C99 inline. */
10919 dupe = false;
10920 specs->inline_p = true;
10921 specs->locations[cdw_inline] = loc;
10922 break;
10923 case RID_NORETURN:
10924 /* Duplicate _Noreturn is permitted. */
10925 dupe = false;
10926 specs->noreturn_p = true;
10927 specs->locations[cdw_noreturn] = loc;
10928 break;
10929 case RID_THREAD:
10930 dupe = specs->thread_p;
10931 if (specs->storage_class == csc_auto)
10932 error ("%qE used with %<auto%>", scspec);
10933 else if (specs->storage_class == csc_register)
10934 error ("%qE used with %<register%>", scspec);
10935 else if (specs->storage_class == csc_typedef)
10936 error ("%qE used with %<typedef%>", scspec);
10937 else
10939 specs->thread_p = true;
10940 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10941 "__thread") == 0);
10942 /* A diagnostic is not required for the use of this
10943 identifier in the implementation namespace; only diagnose
10944 it for the C11 spelling because of existing code using
10945 the other spelling. */
10946 if (!specs->thread_gnu_p)
10948 if (flag_isoc99)
10949 pedwarn_c99 (loc, OPT_Wpedantic,
10950 "ISO C99 does not support %qE", scspec);
10951 else
10952 pedwarn_c99 (loc, OPT_Wpedantic,
10953 "ISO C90 does not support %qE", scspec);
10955 specs->locations[cdw_thread] = loc;
10957 break;
10958 case RID_AUTO:
10959 n = csc_auto;
10960 break;
10961 case RID_EXTERN:
10962 n = csc_extern;
10963 /* Diagnose "__thread extern". */
10964 if (specs->thread_p && specs->thread_gnu_p)
10965 error ("%<__thread%> before %<extern%>");
10966 break;
10967 case RID_REGISTER:
10968 n = csc_register;
10969 break;
10970 case RID_STATIC:
10971 n = csc_static;
10972 /* Diagnose "__thread static". */
10973 if (specs->thread_p && specs->thread_gnu_p)
10974 error ("%<__thread%> before %<static%>");
10975 break;
10976 case RID_TYPEDEF:
10977 n = csc_typedef;
10978 break;
10979 default:
10980 gcc_unreachable ();
10982 if (n != csc_none && n == specs->storage_class)
10983 dupe = true;
10984 if (dupe)
10986 if (i == RID_THREAD)
10987 error ("duplicate %<_Thread_local%> or %<__thread%>");
10988 else
10989 error ("duplicate %qE", scspec);
10991 if (n != csc_none)
10993 if (specs->storage_class != csc_none && n != specs->storage_class)
10995 error ("multiple storage classes in declaration specifiers");
10997 else
10999 specs->storage_class = n;
11000 specs->locations[cdw_storage_class] = loc;
11001 if (n != csc_extern && n != csc_static && specs->thread_p)
11003 error ("%qs used with %qE",
11004 specs->thread_gnu_p ? "__thread" : "_Thread_local",
11005 scspec);
11006 specs->thread_p = false;
11010 return specs;
11013 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11014 returning SPECS. */
11016 struct c_declspecs *
11017 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
11019 specs->attrs = chainon (attrs, specs->attrs);
11020 specs->locations[cdw_attributes] = loc;
11021 specs->declspecs_seen_p = true;
11022 return specs;
11025 /* Add an _Alignas specifier (expression ALIGN, or type whose
11026 alignment is ALIGN) to the declaration specifiers SPECS, returning
11027 SPECS. */
11028 struct c_declspecs *
11029 declspecs_add_alignas (source_location loc,
11030 struct c_declspecs *specs, tree align)
11032 int align_log;
11033 specs->alignas_p = true;
11034 specs->locations[cdw_alignas] = loc;
11035 if (align == error_mark_node)
11036 return specs;
11037 align_log = check_user_alignment (align, true);
11038 if (align_log > specs->align_log)
11039 specs->align_log = align_log;
11040 return specs;
11043 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11044 specifiers with any other type specifier to determine the resulting
11045 type. This is where ISO C checks on complex types are made, since
11046 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11047 double". */
11049 struct c_declspecs *
11050 finish_declspecs (struct c_declspecs *specs)
11052 /* If a type was specified as a whole, we have no modifiers and are
11053 done. */
11054 if (specs->type != NULL_TREE)
11056 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11057 && !specs->signed_p && !specs->unsigned_p
11058 && !specs->complex_p);
11060 /* Set a dummy type. */
11061 if (TREE_CODE (specs->type) == ERROR_MARK)
11062 specs->type = integer_type_node;
11063 return specs;
11066 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11067 has been specified, treat it as "int" unless "_Complex" is
11068 present and there are no other specifiers. If we just have
11069 "_Complex", it is equivalent to "_Complex double", but e.g.
11070 "_Complex short" is equivalent to "_Complex short int". */
11071 if (specs->typespec_word == cts_none)
11073 if (specs->saturating_p)
11075 error_at (specs->locations[cdw_saturating],
11076 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11077 if (!targetm.fixed_point_supported_p ())
11078 error_at (specs->locations[cdw_saturating],
11079 "fixed-point types not supported for this target");
11080 specs->typespec_word = cts_fract;
11082 else if (specs->long_p || specs->short_p
11083 || specs->signed_p || specs->unsigned_p)
11085 specs->typespec_word = cts_int;
11087 else if (specs->complex_p)
11089 specs->typespec_word = cts_double;
11090 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11091 "ISO C does not support plain %<complex%> meaning "
11092 "%<double complex%>");
11094 else
11096 specs->typespec_word = cts_int;
11097 specs->default_int_p = true;
11098 /* We don't diagnose this here because grokdeclarator will
11099 give more specific diagnostics according to whether it is
11100 a function definition. */
11104 /* If "signed" was specified, record this to distinguish "int" and
11105 "signed int" in the case of a bit-field with
11106 -funsigned-bitfields. */
11107 specs->explicit_signed_p = specs->signed_p;
11109 /* Now compute the actual type. */
11110 switch (specs->typespec_word)
11112 case cts_auto_type:
11113 gcc_assert (!specs->long_p && !specs->short_p
11114 && !specs->signed_p && !specs->unsigned_p
11115 && !specs->complex_p);
11116 /* Type to be filled in later. */
11117 break;
11118 case cts_void:
11119 gcc_assert (!specs->long_p && !specs->short_p
11120 && !specs->signed_p && !specs->unsigned_p
11121 && !specs->complex_p);
11122 specs->type = void_type_node;
11123 break;
11124 case cts_bool:
11125 gcc_assert (!specs->long_p && !specs->short_p
11126 && !specs->signed_p && !specs->unsigned_p
11127 && !specs->complex_p);
11128 specs->type = boolean_type_node;
11129 break;
11130 case cts_char:
11131 gcc_assert (!specs->long_p && !specs->short_p);
11132 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11133 if (specs->signed_p)
11134 specs->type = signed_char_type_node;
11135 else if (specs->unsigned_p)
11136 specs->type = unsigned_char_type_node;
11137 else
11138 specs->type = char_type_node;
11139 if (specs->complex_p)
11141 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11142 "ISO C does not support complex integer types");
11143 specs->type = build_complex_type (specs->type);
11145 break;
11146 case cts_int_n:
11147 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11148 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11149 if (! int_n_enabled_p[specs->int_n_idx])
11150 specs->type = integer_type_node;
11151 else
11152 specs->type = (specs->unsigned_p
11153 ? int_n_trees[specs->int_n_idx].unsigned_type
11154 : int_n_trees[specs->int_n_idx].signed_type);
11155 if (specs->complex_p)
11157 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11158 "ISO C does not support complex integer types");
11159 specs->type = build_complex_type (specs->type);
11161 break;
11162 case cts_int:
11163 gcc_assert (!(specs->long_p && specs->short_p));
11164 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11165 if (specs->long_long_p)
11166 specs->type = (specs->unsigned_p
11167 ? long_long_unsigned_type_node
11168 : long_long_integer_type_node);
11169 else if (specs->long_p)
11170 specs->type = (specs->unsigned_p
11171 ? long_unsigned_type_node
11172 : long_integer_type_node);
11173 else if (specs->short_p)
11174 specs->type = (specs->unsigned_p
11175 ? short_unsigned_type_node
11176 : short_integer_type_node);
11177 else
11178 specs->type = (specs->unsigned_p
11179 ? unsigned_type_node
11180 : integer_type_node);
11181 if (specs->complex_p)
11183 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11184 "ISO C does not support complex integer types");
11185 specs->type = build_complex_type (specs->type);
11187 break;
11188 case cts_float:
11189 gcc_assert (!specs->long_p && !specs->short_p
11190 && !specs->signed_p && !specs->unsigned_p);
11191 specs->type = (specs->complex_p
11192 ? complex_float_type_node
11193 : float_type_node);
11194 break;
11195 case cts_double:
11196 gcc_assert (!specs->long_long_p && !specs->short_p
11197 && !specs->signed_p && !specs->unsigned_p);
11198 if (specs->long_p)
11200 specs->type = (specs->complex_p
11201 ? complex_long_double_type_node
11202 : long_double_type_node);
11204 else
11206 specs->type = (specs->complex_p
11207 ? complex_double_type_node
11208 : double_type_node);
11210 break;
11211 case cts_floatn_nx:
11212 gcc_assert (!specs->long_p && !specs->short_p
11213 && !specs->signed_p && !specs->unsigned_p);
11214 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11215 specs->type = integer_type_node;
11216 else if (specs->complex_p)
11217 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11218 else
11219 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11220 break;
11221 case cts_dfloat32:
11222 case cts_dfloat64:
11223 case cts_dfloat128:
11224 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11225 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11226 if (specs->typespec_word == cts_dfloat32)
11227 specs->type = dfloat32_type_node;
11228 else if (specs->typespec_word == cts_dfloat64)
11229 specs->type = dfloat64_type_node;
11230 else
11231 specs->type = dfloat128_type_node;
11232 break;
11233 case cts_fract:
11234 gcc_assert (!specs->complex_p);
11235 if (!targetm.fixed_point_supported_p ())
11236 specs->type = integer_type_node;
11237 else if (specs->saturating_p)
11239 if (specs->long_long_p)
11240 specs->type = specs->unsigned_p
11241 ? sat_unsigned_long_long_fract_type_node
11242 : sat_long_long_fract_type_node;
11243 else if (specs->long_p)
11244 specs->type = specs->unsigned_p
11245 ? sat_unsigned_long_fract_type_node
11246 : sat_long_fract_type_node;
11247 else if (specs->short_p)
11248 specs->type = specs->unsigned_p
11249 ? sat_unsigned_short_fract_type_node
11250 : sat_short_fract_type_node;
11251 else
11252 specs->type = specs->unsigned_p
11253 ? sat_unsigned_fract_type_node
11254 : sat_fract_type_node;
11256 else
11258 if (specs->long_long_p)
11259 specs->type = specs->unsigned_p
11260 ? unsigned_long_long_fract_type_node
11261 : long_long_fract_type_node;
11262 else if (specs->long_p)
11263 specs->type = specs->unsigned_p
11264 ? unsigned_long_fract_type_node
11265 : long_fract_type_node;
11266 else if (specs->short_p)
11267 specs->type = specs->unsigned_p
11268 ? unsigned_short_fract_type_node
11269 : short_fract_type_node;
11270 else
11271 specs->type = specs->unsigned_p
11272 ? unsigned_fract_type_node
11273 : fract_type_node;
11275 break;
11276 case cts_accum:
11277 gcc_assert (!specs->complex_p);
11278 if (!targetm.fixed_point_supported_p ())
11279 specs->type = integer_type_node;
11280 else if (specs->saturating_p)
11282 if (specs->long_long_p)
11283 specs->type = specs->unsigned_p
11284 ? sat_unsigned_long_long_accum_type_node
11285 : sat_long_long_accum_type_node;
11286 else if (specs->long_p)
11287 specs->type = specs->unsigned_p
11288 ? sat_unsigned_long_accum_type_node
11289 : sat_long_accum_type_node;
11290 else if (specs->short_p)
11291 specs->type = specs->unsigned_p
11292 ? sat_unsigned_short_accum_type_node
11293 : sat_short_accum_type_node;
11294 else
11295 specs->type = specs->unsigned_p
11296 ? sat_unsigned_accum_type_node
11297 : sat_accum_type_node;
11299 else
11301 if (specs->long_long_p)
11302 specs->type = specs->unsigned_p
11303 ? unsigned_long_long_accum_type_node
11304 : long_long_accum_type_node;
11305 else if (specs->long_p)
11306 specs->type = specs->unsigned_p
11307 ? unsigned_long_accum_type_node
11308 : long_accum_type_node;
11309 else if (specs->short_p)
11310 specs->type = specs->unsigned_p
11311 ? unsigned_short_accum_type_node
11312 : short_accum_type_node;
11313 else
11314 specs->type = specs->unsigned_p
11315 ? unsigned_accum_type_node
11316 : accum_type_node;
11318 break;
11319 default:
11320 gcc_unreachable ();
11323 return specs;
11326 /* Perform final processing on one file scope's declarations (or the
11327 external scope's declarations), GLOBALS. */
11329 static void
11330 c_write_global_declarations_1 (tree globals)
11332 tree decl;
11333 bool reconsider;
11335 /* Process the decls in the order they were written. */
11336 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11338 /* Check for used but undefined static functions using the C
11339 standard's definition of "used", and set TREE_NO_WARNING so
11340 that check_global_declaration doesn't repeat the check. */
11341 if (TREE_CODE (decl) == FUNCTION_DECL
11342 && DECL_INITIAL (decl) == NULL_TREE
11343 && DECL_EXTERNAL (decl)
11344 && !TREE_PUBLIC (decl))
11346 if (C_DECL_USED (decl))
11348 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11349 TREE_NO_WARNING (decl) = 1;
11351 /* For -Wunused-function warn about unused static prototypes. */
11352 else if (warn_unused_function
11353 && ! DECL_ARTIFICIAL (decl)
11354 && ! TREE_NO_WARNING (decl))
11356 warning (OPT_Wunused_function,
11357 "%q+F declared %<static%> but never defined", decl);
11358 TREE_NO_WARNING (decl) = 1;
11362 wrapup_global_declaration_1 (decl);
11367 reconsider = false;
11368 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11369 reconsider |= wrapup_global_declaration_2 (decl);
11371 while (reconsider);
11374 /* Callback to collect a source_ref from a DECL. */
11376 static void
11377 collect_source_ref_cb (tree decl)
11379 if (!DECL_IS_BUILTIN (decl))
11380 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11383 /* Preserve the external declarations scope across a garbage collect. */
11384 static GTY(()) tree ext_block;
11386 /* Collect all references relevant to SOURCE_FILE. */
11388 static void
11389 collect_all_refs (const char *source_file)
11391 tree t;
11392 unsigned i;
11394 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11395 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11397 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11400 /* Iterate over all global declarations and call CALLBACK. */
11402 static void
11403 for_each_global_decl (void (*callback) (tree decl))
11405 tree t;
11406 tree decls;
11407 tree decl;
11408 unsigned i;
11410 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11412 decls = DECL_INITIAL (t);
11413 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11414 callback (decl);
11417 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11418 callback (decl);
11421 /* Perform any final parser cleanups and generate initial debugging
11422 information. */
11424 void
11425 c_parse_final_cleanups (void)
11427 tree t;
11428 unsigned i;
11430 /* We don't want to do this if generating a PCH. */
11431 if (pch_file)
11432 return;
11434 timevar_stop (TV_PHASE_PARSING);
11435 timevar_start (TV_PHASE_DEFERRED);
11437 /* Do the Objective-C stuff. This is where all the Objective-C
11438 module stuff gets generated (symtab, class/protocol/selector
11439 lists etc). */
11440 if (c_dialect_objc ())
11441 objc_write_global_declarations ();
11443 /* Close the external scope. */
11444 ext_block = pop_scope ();
11445 external_scope = 0;
11446 gcc_assert (!current_scope);
11448 /* Handle -fdump-ada-spec[-slim]. */
11449 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11451 /* Build a table of files to generate specs for */
11452 if (flag_dump_ada_spec_slim)
11453 collect_source_ref (main_input_filename);
11454 else
11455 for_each_global_decl (collect_source_ref_cb);
11457 dump_ada_specs (collect_all_refs, NULL);
11460 /* Process all file scopes in this compilation, and the external_scope,
11461 through wrapup_global_declarations. */
11462 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11463 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11464 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11466 timevar_stop (TV_PHASE_DEFERRED);
11467 timevar_start (TV_PHASE_PARSING);
11469 ext_block = NULL;
11472 /* Register reserved keyword WORD as qualifier for address space AS. */
11474 void
11475 c_register_addr_space (const char *word, addr_space_t as)
11477 int rid = RID_FIRST_ADDR_SPACE + as;
11478 tree id;
11480 /* Address space qualifiers are only supported
11481 in C with GNU extensions enabled. */
11482 if (c_dialect_objc () || flag_no_asm)
11483 return;
11485 id = get_identifier (word);
11486 C_SET_RID_CODE (id, rid);
11487 C_IS_RESERVED_WORD (id) = 1;
11488 ridpointers [rid] = id;
11491 /* Return identifier to look up for omp declare reduction. */
11493 tree
11494 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11496 const char *p = NULL;
11497 switch (reduction_code)
11499 case PLUS_EXPR: p = "+"; break;
11500 case MULT_EXPR: p = "*"; break;
11501 case MINUS_EXPR: p = "-"; break;
11502 case BIT_AND_EXPR: p = "&"; break;
11503 case BIT_XOR_EXPR: p = "^"; break;
11504 case BIT_IOR_EXPR: p = "|"; break;
11505 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11506 case TRUTH_ORIF_EXPR: p = "||"; break;
11507 case MIN_EXPR: p = "min"; break;
11508 case MAX_EXPR: p = "max"; break;
11509 default:
11510 break;
11513 if (p == NULL)
11515 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11516 return error_mark_node;
11517 p = IDENTIFIER_POINTER (reduction_id);
11520 const char prefix[] = "omp declare reduction ";
11521 size_t lenp = sizeof (prefix);
11522 size_t len = strlen (p);
11523 char *name = XALLOCAVEC (char, lenp + len);
11524 memcpy (name, prefix, lenp - 1);
11525 memcpy (name + lenp - 1, p, len + 1);
11526 return get_identifier (name);
11529 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11530 VAR_DECL, bind it into the current scope and return it. */
11532 tree
11533 c_omp_reduction_decl (tree reduction_id)
11535 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11536 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11537 return b->decl;
11539 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11540 reduction_id, integer_type_node);
11541 DECL_ARTIFICIAL (decl) = 1;
11542 DECL_EXTERNAL (decl) = 1;
11543 TREE_STATIC (decl) = 1;
11544 TREE_PUBLIC (decl) = 0;
11545 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11546 return decl;
11549 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11551 tree
11552 c_omp_reduction_lookup (tree reduction_id, tree type)
11554 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11555 while (b)
11557 tree t;
11558 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11559 if (comptypes (TREE_PURPOSE (t), type))
11560 return TREE_VALUE (t);
11561 b = b->shadowed;
11563 return error_mark_node;
11566 /* Helper function called via walk_tree, to diagnose invalid
11567 #pragma omp declare reduction combiners or initializers. */
11569 tree
11570 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11572 tree *vars = (tree *) data;
11573 if (SSA_VAR_P (*tp)
11574 && !DECL_ARTIFICIAL (*tp)
11575 && *tp != vars[0]
11576 && *tp != vars[1])
11578 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11579 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11580 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11581 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11582 *tp);
11583 else
11584 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11585 "to variable %qD which is not %<omp_priv%> nor "
11586 "%<omp_orig%>",
11587 *tp);
11588 return *tp;
11590 return NULL_TREE;
11593 #include "gt-c-c-decl.h"