Add support for grouping of related diagnostics (PR other/84889)
[official-gcc.git] / gcc / c / c-decl.c
blob95249779e3c8350c2286f0ead1858d20f2b374ef
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 && DECL_BUILT_IN (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 && DECL_BUILT_IN (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 && DECL_BUILT_IN (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 (DECL_BUILT_IN (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 && !DECL_BUILT_IN (newdecl)
2301 && DECL_BUILT_IN (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 (DECL_BUILT_IN (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 /* No shadow warnings for vars made for inlining. */
2789 || DECL_FROM_INLINE (new_decl))
2790 return;
2792 /* Is anything being shadowed? Invisible decls do not count. */
2793 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2794 if (b->decl && b->decl != new_decl && !b->invisible
2795 && (b->decl == error_mark_node
2796 || diagnostic_report_warnings_p (global_dc,
2797 DECL_SOURCE_LOCATION (b->decl))))
2799 tree old_decl = b->decl;
2801 if (old_decl == error_mark_node)
2803 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2804 "non-variable", new_decl);
2805 break;
2808 bool warned = false;
2809 auto_diagnostic_group d;
2810 if (TREE_CODE (old_decl) == PARM_DECL)
2812 enum opt_code warning_code;
2814 /* If '-Wshadow=compatible-local' is specified without other
2815 -Wshadow= flags, we will warn only when the types of the
2816 shadowing variable (i.e. new_decl) and the shadowed variable
2817 (old_decl) are compatible. */
2818 if (warn_shadow)
2819 warning_code = OPT_Wshadow;
2820 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2821 warning_code = OPT_Wshadow_compatible_local;
2822 else
2823 warning_code = OPT_Wshadow_local;
2824 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2825 "declaration of %qD shadows a parameter",
2826 new_decl);
2828 else if (DECL_FILE_SCOPE_P (old_decl))
2830 /* Do not warn if a variable shadows a function, unless
2831 the variable is a function or a pointer-to-function. */
2832 if (TREE_CODE (old_decl) == FUNCTION_DECL
2833 && TREE_CODE (new_decl) != FUNCTION_DECL
2834 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2835 continue;
2837 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2838 "declaration of %qD shadows a global "
2839 "declaration",
2840 new_decl);
2842 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2843 && DECL_BUILT_IN (old_decl))
2845 warning (OPT_Wshadow, "declaration of %q+D shadows "
2846 "a built-in function", new_decl);
2847 break;
2849 else
2851 enum opt_code warning_code;
2853 /* If '-Wshadow=compatible-local' is specified without other
2854 -Wshadow= flags, we will warn only when the types of the
2855 shadowing variable (i.e. new_decl) and the shadowed variable
2856 (old_decl) are compatible. */
2857 if (warn_shadow)
2858 warning_code = OPT_Wshadow;
2859 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2860 warning_code = OPT_Wshadow_compatible_local;
2861 else
2862 warning_code = OPT_Wshadow_local;
2863 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2864 "declaration of %qD shadows a previous local",
2865 new_decl);
2868 if (warned)
2869 inform (DECL_SOURCE_LOCATION (old_decl),
2870 "shadowed declaration is here");
2872 break;
2876 /* Record a decl-node X as belonging to the current lexical scope.
2877 Check for errors (such as an incompatible declaration for the same
2878 name already seen in the same scope).
2880 Returns either X or an old decl for the same name.
2881 If an old decl is returned, it may have been smashed
2882 to agree with what X says. */
2884 tree
2885 pushdecl (tree x)
2887 tree name = DECL_NAME (x);
2888 struct c_scope *scope = current_scope;
2889 struct c_binding *b;
2890 bool nested = false;
2891 location_t locus = DECL_SOURCE_LOCATION (x);
2893 /* Must set DECL_CONTEXT for everything not at file scope or
2894 DECL_FILE_SCOPE_P won't work. Local externs don't count
2895 unless they have initializers (which generate code). */
2896 if (current_function_decl
2897 && (!VAR_OR_FUNCTION_DECL_P (x)
2898 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2899 DECL_CONTEXT (x) = current_function_decl;
2901 /* Anonymous decls are just inserted in the scope. */
2902 if (!name)
2904 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2905 locus);
2906 return x;
2909 /* First, see if there is another declaration with the same name in
2910 the current scope. If there is, duplicate_decls may do all the
2911 work for us. If duplicate_decls returns false, that indicates
2912 two incompatible decls in the same scope; we are to silently
2913 replace the old one (duplicate_decls has issued all appropriate
2914 diagnostics). In particular, we should not consider possible
2915 duplicates in the external scope, or shadowing. */
2916 b = I_SYMBOL_BINDING (name);
2917 if (b && B_IN_SCOPE (b, scope))
2919 struct c_binding *b_ext, *b_use;
2920 tree type = TREE_TYPE (x);
2921 tree visdecl = b->decl;
2922 tree vistype = TREE_TYPE (visdecl);
2923 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2924 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2925 b->inner_comp = false;
2926 b_use = b;
2927 b_ext = b;
2928 /* If this is an external linkage declaration, we should check
2929 for compatibility with the type in the external scope before
2930 setting the type at this scope based on the visible
2931 information only. */
2932 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2934 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2935 b_ext = b_ext->shadowed;
2936 if (b_ext)
2938 b_use = b_ext;
2939 if (b_use->u.type)
2940 TREE_TYPE (b_use->decl) = b_use->u.type;
2943 if (duplicate_decls (x, b_use->decl))
2945 if (b_use != b)
2947 /* Save the updated type in the external scope and
2948 restore the proper type for this scope. */
2949 tree thistype;
2950 if (comptypes (vistype, type))
2951 thistype = composite_type (vistype, type);
2952 else
2953 thistype = TREE_TYPE (b_use->decl);
2954 b_use->u.type = TREE_TYPE (b_use->decl);
2955 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2956 && DECL_BUILT_IN (b_use->decl))
2957 thistype
2958 = build_type_attribute_variant (thistype,
2959 TYPE_ATTRIBUTES
2960 (b_use->u.type));
2961 TREE_TYPE (b_use->decl) = thistype;
2963 return b_use->decl;
2965 else
2966 goto skip_external_and_shadow_checks;
2969 /* All declarations with external linkage, and all external
2970 references, go in the external scope, no matter what scope is
2971 current. However, the binding in that scope is ignored for
2972 purposes of normal name lookup. A separate binding structure is
2973 created in the requested scope; this governs the normal
2974 visibility of the symbol.
2976 The binding in the externals scope is used exclusively for
2977 detecting duplicate declarations of the same object, no matter
2978 what scope they are in; this is what we do here. (C99 6.2.7p2:
2979 All declarations that refer to the same object or function shall
2980 have compatible type; otherwise, the behavior is undefined.) */
2981 if (DECL_EXTERNAL (x) || scope == file_scope)
2983 tree type = TREE_TYPE (x);
2984 tree vistype = NULL_TREE;
2985 tree visdecl = NULL_TREE;
2986 bool type_saved = false;
2987 if (b && !B_IN_EXTERNAL_SCOPE (b)
2988 && VAR_OR_FUNCTION_DECL_P (b->decl)
2989 && DECL_FILE_SCOPE_P (b->decl))
2991 visdecl = b->decl;
2992 vistype = TREE_TYPE (visdecl);
2994 if (scope != file_scope
2995 && !DECL_IN_SYSTEM_HEADER (x))
2996 warning_at (locus, OPT_Wnested_externs,
2997 "nested extern declaration of %qD", x);
2999 while (b && !B_IN_EXTERNAL_SCOPE (b))
3001 /* If this decl might be modified, save its type. This is
3002 done here rather than when the decl is first bound
3003 because the type may change after first binding, through
3004 being completed or through attributes being added. If we
3005 encounter multiple such decls, only the first should have
3006 its type saved; the others will already have had their
3007 proper types saved and the types will not have changed as
3008 their scopes will not have been re-entered. */
3009 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
3011 b->u.type = TREE_TYPE (b->decl);
3012 type_saved = true;
3014 if (B_IN_FILE_SCOPE (b)
3015 && VAR_P (b->decl)
3016 && TREE_STATIC (b->decl)
3017 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
3018 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
3019 && TREE_CODE (type) == ARRAY_TYPE
3020 && TYPE_DOMAIN (type)
3021 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
3022 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
3024 /* Array type completed in inner scope, which should be
3025 diagnosed if the completion does not have size 1 and
3026 it does not get completed in the file scope. */
3027 b->inner_comp = true;
3029 b = b->shadowed;
3032 /* If a matching external declaration has been found, set its
3033 type to the composite of all the types of that declaration.
3034 After the consistency checks, it will be reset to the
3035 composite of the visible types only. */
3036 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3037 && b->u.type)
3038 TREE_TYPE (b->decl) = b->u.type;
3040 /* The point of the same_translation_unit_p check here is,
3041 we want to detect a duplicate decl for a construct like
3042 foo() { extern bar(); } ... static bar(); but not if
3043 they are in different translation units. In any case,
3044 the static does not go in the externals scope. */
3045 if (b
3046 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3047 && duplicate_decls (x, b->decl))
3049 tree thistype;
3050 if (vistype)
3052 if (comptypes (vistype, type))
3053 thistype = composite_type (vistype, type);
3054 else
3055 thistype = TREE_TYPE (b->decl);
3057 else
3058 thistype = type;
3059 b->u.type = TREE_TYPE (b->decl);
3060 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3061 thistype
3062 = build_type_attribute_variant (thistype,
3063 TYPE_ATTRIBUTES (b->u.type));
3064 TREE_TYPE (b->decl) = thistype;
3065 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3066 locus);
3067 return b->decl;
3069 else if (TREE_PUBLIC (x))
3071 if (visdecl && !b && duplicate_decls (x, visdecl))
3073 /* An external declaration at block scope referring to a
3074 visible entity with internal linkage. The composite
3075 type will already be correct for this scope, so we
3076 just need to fall through to make the declaration in
3077 this scope. */
3078 nested = true;
3079 x = visdecl;
3081 else
3083 bind (name, x, external_scope, /*invisible=*/true,
3084 /*nested=*/false, locus);
3085 nested = true;
3090 if (TREE_CODE (x) != PARM_DECL)
3091 warn_if_shadowing (x);
3093 skip_external_and_shadow_checks:
3094 if (TREE_CODE (x) == TYPE_DECL)
3096 /* So this is a typedef, set its underlying type. */
3097 set_underlying_type (x);
3099 /* If X is a typedef defined in the current function, record it
3100 for the purpose of implementing the -Wunused-local-typedefs
3101 warning. */
3102 record_locally_defined_typedef (x);
3105 bind (name, x, scope, /*invisible=*/false, nested, locus);
3107 /* If x's type is incomplete because it's based on a
3108 structure or union which has not yet been fully declared,
3109 attach it to that structure or union type, so we can go
3110 back and complete the variable declaration later, if the
3111 structure or union gets fully declared.
3113 If the input is erroneous, we can have error_mark in the type
3114 slot (e.g. "f(void a, ...)") - that doesn't count as an
3115 incomplete type. */
3116 if (TREE_TYPE (x) != error_mark_node
3117 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3119 tree element = TREE_TYPE (x);
3121 while (TREE_CODE (element) == ARRAY_TYPE)
3122 element = TREE_TYPE (element);
3123 element = TYPE_MAIN_VARIANT (element);
3125 if (RECORD_OR_UNION_TYPE_P (element)
3126 && (TREE_CODE (x) != TYPE_DECL
3127 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3128 && !COMPLETE_TYPE_P (element))
3129 C_TYPE_INCOMPLETE_VARS (element)
3130 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3132 return x;
3136 /* Issue a warning about implicit function declaration. ID is the function
3137 identifier, OLDDECL is a declaration of the function in a different scope,
3138 or NULL_TREE. */
3140 static void
3141 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3143 if (!warn_implicit_function_declaration)
3144 return;
3146 bool warned;
3147 auto_diagnostic_group d;
3148 name_hint hint;
3149 if (!olddecl)
3150 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3152 if (flag_isoc99)
3154 if (hint)
3156 gcc_rich_location richloc (loc);
3157 richloc.add_fixit_replace (hint.suggestion ());
3158 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3159 "implicit declaration of function %qE;"
3160 " did you mean %qs?",
3161 id, hint.suggestion ());
3163 else
3164 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3165 "implicit declaration of function %qE", id);
3167 else if (hint)
3169 gcc_rich_location richloc (loc);
3170 richloc.add_fixit_replace (hint.suggestion ());
3171 warned = warning_at
3172 (&richloc, OPT_Wimplicit_function_declaration,
3173 G_("implicit declaration of function %qE; did you mean %qs?"),
3174 id, hint.suggestion ());
3176 else
3177 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3178 G_("implicit declaration of function %qE"), id);
3180 if (olddecl && warned)
3181 locate_old_decl (olddecl);
3183 if (!warned)
3184 hint.suppress ();
3187 /* This function represents mapping of a function code FCODE
3188 to its respective header. */
3190 static const char *
3191 header_for_builtin_fn (enum built_in_function fcode)
3193 switch (fcode)
3195 CASE_FLT_FN (BUILT_IN_ACOS):
3196 CASE_FLT_FN (BUILT_IN_ACOSH):
3197 CASE_FLT_FN (BUILT_IN_ASIN):
3198 CASE_FLT_FN (BUILT_IN_ASINH):
3199 CASE_FLT_FN (BUILT_IN_ATAN):
3200 CASE_FLT_FN (BUILT_IN_ATANH):
3201 CASE_FLT_FN (BUILT_IN_ATAN2):
3202 CASE_FLT_FN (BUILT_IN_CBRT):
3203 CASE_FLT_FN (BUILT_IN_CEIL):
3204 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3205 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3206 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3207 CASE_FLT_FN (BUILT_IN_COS):
3208 CASE_FLT_FN (BUILT_IN_COSH):
3209 CASE_FLT_FN (BUILT_IN_ERF):
3210 CASE_FLT_FN (BUILT_IN_ERFC):
3211 CASE_FLT_FN (BUILT_IN_EXP):
3212 CASE_FLT_FN (BUILT_IN_EXP2):
3213 CASE_FLT_FN (BUILT_IN_EXPM1):
3214 CASE_FLT_FN (BUILT_IN_FABS):
3215 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3216 CASE_FLT_FN (BUILT_IN_FDIM):
3217 CASE_FLT_FN (BUILT_IN_FLOOR):
3218 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3219 CASE_FLT_FN (BUILT_IN_FMA):
3220 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3221 CASE_FLT_FN (BUILT_IN_FMAX):
3222 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3223 CASE_FLT_FN (BUILT_IN_FMIN):
3224 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3225 CASE_FLT_FN (BUILT_IN_FMOD):
3226 CASE_FLT_FN (BUILT_IN_FREXP):
3227 CASE_FLT_FN (BUILT_IN_HYPOT):
3228 CASE_FLT_FN (BUILT_IN_ILOGB):
3229 CASE_FLT_FN (BUILT_IN_LDEXP):
3230 CASE_FLT_FN (BUILT_IN_LGAMMA):
3231 CASE_FLT_FN (BUILT_IN_LLRINT):
3232 CASE_FLT_FN (BUILT_IN_LLROUND):
3233 CASE_FLT_FN (BUILT_IN_LOG):
3234 CASE_FLT_FN (BUILT_IN_LOG10):
3235 CASE_FLT_FN (BUILT_IN_LOG1P):
3236 CASE_FLT_FN (BUILT_IN_LOG2):
3237 CASE_FLT_FN (BUILT_IN_LOGB):
3238 CASE_FLT_FN (BUILT_IN_LRINT):
3239 CASE_FLT_FN (BUILT_IN_LROUND):
3240 CASE_FLT_FN (BUILT_IN_MODF):
3241 CASE_FLT_FN (BUILT_IN_NAN):
3242 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3243 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3244 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3245 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3246 CASE_FLT_FN (BUILT_IN_POW):
3247 CASE_FLT_FN (BUILT_IN_REMAINDER):
3248 CASE_FLT_FN (BUILT_IN_REMQUO):
3249 CASE_FLT_FN (BUILT_IN_RINT):
3250 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3251 CASE_FLT_FN (BUILT_IN_ROUND):
3252 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3253 CASE_FLT_FN (BUILT_IN_SCALBLN):
3254 CASE_FLT_FN (BUILT_IN_SCALBN):
3255 CASE_FLT_FN (BUILT_IN_SIN):
3256 CASE_FLT_FN (BUILT_IN_SINH):
3257 CASE_FLT_FN (BUILT_IN_SINCOS):
3258 CASE_FLT_FN (BUILT_IN_SQRT):
3259 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3260 CASE_FLT_FN (BUILT_IN_TAN):
3261 CASE_FLT_FN (BUILT_IN_TANH):
3262 CASE_FLT_FN (BUILT_IN_TGAMMA):
3263 CASE_FLT_FN (BUILT_IN_TRUNC):
3264 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3265 case BUILT_IN_ISINF:
3266 case BUILT_IN_ISNAN:
3267 return "<math.h>";
3268 CASE_FLT_FN (BUILT_IN_CABS):
3269 CASE_FLT_FN (BUILT_IN_CACOS):
3270 CASE_FLT_FN (BUILT_IN_CACOSH):
3271 CASE_FLT_FN (BUILT_IN_CARG):
3272 CASE_FLT_FN (BUILT_IN_CASIN):
3273 CASE_FLT_FN (BUILT_IN_CASINH):
3274 CASE_FLT_FN (BUILT_IN_CATAN):
3275 CASE_FLT_FN (BUILT_IN_CATANH):
3276 CASE_FLT_FN (BUILT_IN_CCOS):
3277 CASE_FLT_FN (BUILT_IN_CCOSH):
3278 CASE_FLT_FN (BUILT_IN_CEXP):
3279 CASE_FLT_FN (BUILT_IN_CIMAG):
3280 CASE_FLT_FN (BUILT_IN_CLOG):
3281 CASE_FLT_FN (BUILT_IN_CONJ):
3282 CASE_FLT_FN (BUILT_IN_CPOW):
3283 CASE_FLT_FN (BUILT_IN_CPROJ):
3284 CASE_FLT_FN (BUILT_IN_CREAL):
3285 CASE_FLT_FN (BUILT_IN_CSIN):
3286 CASE_FLT_FN (BUILT_IN_CSINH):
3287 CASE_FLT_FN (BUILT_IN_CSQRT):
3288 CASE_FLT_FN (BUILT_IN_CTAN):
3289 CASE_FLT_FN (BUILT_IN_CTANH):
3290 return "<complex.h>";
3291 case BUILT_IN_MEMCHR:
3292 case BUILT_IN_MEMCMP:
3293 case BUILT_IN_MEMCPY:
3294 case BUILT_IN_MEMMOVE:
3295 case BUILT_IN_MEMSET:
3296 case BUILT_IN_STRCAT:
3297 case BUILT_IN_STRCHR:
3298 case BUILT_IN_STRCMP:
3299 case BUILT_IN_STRCPY:
3300 case BUILT_IN_STRCSPN:
3301 case BUILT_IN_STRLEN:
3302 case BUILT_IN_STRNCAT:
3303 case BUILT_IN_STRNCMP:
3304 case BUILT_IN_STRNCPY:
3305 case BUILT_IN_STRPBRK:
3306 case BUILT_IN_STRRCHR:
3307 case BUILT_IN_STRSPN:
3308 case BUILT_IN_STRSTR:
3309 return "<string.h>";
3310 case BUILT_IN_FPRINTF:
3311 case BUILT_IN_PUTC:
3312 case BUILT_IN_FPUTC:
3313 case BUILT_IN_FPUTS:
3314 case BUILT_IN_FSCANF:
3315 case BUILT_IN_FWRITE:
3316 case BUILT_IN_PRINTF:
3317 case BUILT_IN_PUTCHAR:
3318 case BUILT_IN_PUTS:
3319 case BUILT_IN_SCANF:
3320 case BUILT_IN_SNPRINTF:
3321 case BUILT_IN_SPRINTF:
3322 case BUILT_IN_SSCANF:
3323 case BUILT_IN_VFPRINTF:
3324 case BUILT_IN_VFSCANF:
3325 case BUILT_IN_VPRINTF:
3326 case BUILT_IN_VSCANF:
3327 case BUILT_IN_VSNPRINTF:
3328 case BUILT_IN_VSPRINTF:
3329 case BUILT_IN_VSSCANF:
3330 return "<stdio.h>";
3331 case BUILT_IN_ISALNUM:
3332 case BUILT_IN_ISALPHA:
3333 case BUILT_IN_ISBLANK:
3334 case BUILT_IN_ISCNTRL:
3335 case BUILT_IN_ISDIGIT:
3336 case BUILT_IN_ISGRAPH:
3337 case BUILT_IN_ISLOWER:
3338 case BUILT_IN_ISPRINT:
3339 case BUILT_IN_ISPUNCT:
3340 case BUILT_IN_ISSPACE:
3341 case BUILT_IN_ISUPPER:
3342 case BUILT_IN_ISXDIGIT:
3343 case BUILT_IN_TOLOWER:
3344 case BUILT_IN_TOUPPER:
3345 return "<ctype.h>";
3346 case BUILT_IN_ISWALNUM:
3347 case BUILT_IN_ISWALPHA:
3348 case BUILT_IN_ISWBLANK:
3349 case BUILT_IN_ISWCNTRL:
3350 case BUILT_IN_ISWDIGIT:
3351 case BUILT_IN_ISWGRAPH:
3352 case BUILT_IN_ISWLOWER:
3353 case BUILT_IN_ISWPRINT:
3354 case BUILT_IN_ISWPUNCT:
3355 case BUILT_IN_ISWSPACE:
3356 case BUILT_IN_ISWUPPER:
3357 case BUILT_IN_ISWXDIGIT:
3358 case BUILT_IN_TOWLOWER:
3359 case BUILT_IN_TOWUPPER:
3360 return "<wctype.h>";
3361 case BUILT_IN_ABORT:
3362 case BUILT_IN_ABS:
3363 case BUILT_IN_CALLOC:
3364 case BUILT_IN_EXIT:
3365 case BUILT_IN_FREE:
3366 case BUILT_IN_LABS:
3367 case BUILT_IN_LLABS:
3368 case BUILT_IN_MALLOC:
3369 case BUILT_IN_REALLOC:
3370 case BUILT_IN__EXIT2:
3371 case BUILT_IN_ALIGNED_ALLOC:
3372 return "<stdlib.h>";
3373 case BUILT_IN_IMAXABS:
3374 return "<inttypes.h>";
3375 case BUILT_IN_STRFTIME:
3376 return "<time.h>";
3377 default:
3378 return NULL;
3382 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3383 function of type int (). */
3385 tree
3386 implicitly_declare (location_t loc, tree functionid)
3388 struct c_binding *b;
3389 tree decl = NULL_TREE;
3390 tree asmspec_tree;
3392 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3394 if (B_IN_SCOPE (b, external_scope))
3396 decl = b->decl;
3397 break;
3401 if (decl)
3403 if (TREE_CODE (decl) != FUNCTION_DECL)
3404 return decl;
3406 /* FIXME: Objective-C has weird not-really-builtin functions
3407 which are supposed to be visible automatically. They wind up
3408 in the external scope because they're pushed before the file
3409 scope gets created. Catch this here and rebind them into the
3410 file scope. */
3411 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3413 bind (functionid, decl, file_scope,
3414 /*invisible=*/false, /*nested=*/true,
3415 DECL_SOURCE_LOCATION (decl));
3416 return decl;
3418 else
3420 tree newtype = default_function_type;
3421 if (b->u.type)
3422 TREE_TYPE (decl) = b->u.type;
3423 /* Implicit declaration of a function already declared
3424 (somehow) in a different scope, or as a built-in.
3425 If this is the first time this has happened, warn;
3426 then recycle the old declaration but with the new type. */
3427 if (!C_DECL_IMPLICIT (decl))
3429 implicit_decl_warning (loc, functionid, decl);
3430 C_DECL_IMPLICIT (decl) = 1;
3432 if (DECL_BUILT_IN (decl))
3434 newtype = build_type_attribute_variant (newtype,
3435 TYPE_ATTRIBUTES
3436 (TREE_TYPE (decl)));
3437 if (!comptypes (newtype, TREE_TYPE (decl)))
3439 bool warned = warning_at (loc, 0, "incompatible implicit "
3440 "declaration of built-in "
3441 "function %qD", decl);
3442 /* See if we can hint which header to include. */
3443 const char *header
3444 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3445 if (header != NULL && warned)
3447 rich_location richloc (line_table, loc);
3448 maybe_add_include_fixit (&richloc, header);
3449 inform (&richloc,
3450 "include %qs or provide a declaration of %qD",
3451 header, decl);
3453 newtype = TREE_TYPE (decl);
3456 else
3458 if (!comptypes (newtype, TREE_TYPE (decl)))
3460 auto_diagnostic_group d;
3461 error_at (loc, "incompatible implicit declaration of "
3462 "function %qD", decl);
3463 locate_old_decl (decl);
3466 b->u.type = TREE_TYPE (decl);
3467 TREE_TYPE (decl) = newtype;
3468 bind (functionid, decl, current_scope,
3469 /*invisible=*/false, /*nested=*/true,
3470 DECL_SOURCE_LOCATION (decl));
3471 return decl;
3475 /* Not seen before. */
3476 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3477 DECL_EXTERNAL (decl) = 1;
3478 TREE_PUBLIC (decl) = 1;
3479 C_DECL_IMPLICIT (decl) = 1;
3480 implicit_decl_warning (loc, functionid, 0);
3481 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3482 if (asmspec_tree)
3483 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3485 /* C89 says implicit declarations are in the innermost block.
3486 So we record the decl in the standard fashion. */
3487 decl = pushdecl (decl);
3489 /* No need to call objc_check_decl here - it's a function type. */
3490 rest_of_decl_compilation (decl, 0, 0);
3492 /* Write a record describing this implicit function declaration
3493 to the prototypes file (if requested). */
3494 gen_aux_info_record (decl, 0, 1, 0);
3496 /* Possibly apply some default attributes to this implicit declaration. */
3497 decl_attributes (&decl, NULL_TREE, 0);
3499 return decl;
3502 /* Issue an error message for a reference to an undeclared variable
3503 ID, including a reference to a builtin outside of function-call
3504 context. Establish a binding of the identifier to error_mark_node
3505 in an appropriate scope, which will suppress further errors for the
3506 same identifier. The error message should be given location LOC. */
3507 void
3508 undeclared_variable (location_t loc, tree id)
3510 static bool already = false;
3511 struct c_scope *scope;
3513 auto_diagnostic_group d;
3514 if (current_function_decl == NULL_TREE)
3516 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3517 if (guessed_id)
3519 gcc_rich_location richloc (loc);
3520 richloc.add_fixit_replace (guessed_id.suggestion ());
3521 error_at (&richloc,
3522 "%qE undeclared here (not in a function);"
3523 " did you mean %qs?",
3524 id, guessed_id.suggestion ());
3526 else
3527 error_at (loc, "%qE undeclared here (not in a function)", id);
3528 scope = current_scope;
3530 else
3532 if (!objc_diagnose_private_ivar (id))
3534 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3535 if (guessed_id)
3537 gcc_rich_location richloc (loc);
3538 richloc.add_fixit_replace (guessed_id.suggestion ());
3539 error_at (&richloc,
3540 "%qE undeclared (first use in this function);"
3541 " did you mean %qs?",
3542 id, guessed_id.suggestion ());
3544 else
3545 error_at (loc, "%qE undeclared (first use in this function)", id);
3547 if (!already)
3549 inform (loc, "each undeclared identifier is reported only"
3550 " once for each function it appears in");
3551 already = true;
3554 /* If we are parsing old-style parameter decls, current_function_decl
3555 will be nonnull but current_function_scope will be null. */
3556 scope = current_function_scope ? current_function_scope : current_scope;
3558 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3559 UNKNOWN_LOCATION);
3562 /* Subroutine of lookup_label, declare_label, define_label: construct a
3563 LABEL_DECL with all the proper frills. Also create a struct
3564 c_label_vars initialized for the current scope. */
3566 static tree
3567 make_label (location_t location, tree name, bool defining,
3568 struct c_label_vars **p_label_vars)
3570 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3571 DECL_CONTEXT (label) = current_function_decl;
3572 SET_DECL_MODE (label, VOIDmode);
3574 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3575 label_vars->shadowed = NULL;
3576 set_spot_bindings (&label_vars->label_bindings, defining);
3577 label_vars->decls_in_scope = make_tree_vector ();
3578 label_vars->gotos = NULL;
3579 *p_label_vars = label_vars;
3581 return label;
3584 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3585 Create one if none exists so far for the current function.
3586 This is called when a label is used in a goto expression or
3587 has its address taken. */
3589 tree
3590 lookup_label (tree name)
3592 tree label;
3593 struct c_label_vars *label_vars;
3595 if (current_function_scope == 0)
3597 error ("label %qE referenced outside of any function", name);
3598 return NULL_TREE;
3601 /* Use a label already defined or ref'd with this name, but not if
3602 it is inherited from a containing function and wasn't declared
3603 using __label__. */
3604 label = I_LABEL_DECL (name);
3605 if (label && (DECL_CONTEXT (label) == current_function_decl
3606 || C_DECLARED_LABEL_FLAG (label)))
3608 /* If the label has only been declared, update its apparent
3609 location to point here, for better diagnostics if it
3610 turns out not to have been defined. */
3611 if (DECL_INITIAL (label) == NULL_TREE)
3612 DECL_SOURCE_LOCATION (label) = input_location;
3613 return label;
3616 /* No label binding for that identifier; make one. */
3617 label = make_label (input_location, name, false, &label_vars);
3619 /* Ordinary labels go in the current function scope. */
3620 bind_label (name, label, current_function_scope, label_vars);
3622 return label;
3625 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3626 to LABEL. */
3628 static void
3629 warn_about_goto (location_t goto_loc, tree label, tree decl)
3631 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3632 error_at (goto_loc,
3633 "jump into scope of identifier with variably modified type");
3634 else
3635 warning_at (goto_loc, OPT_Wjump_misses_init,
3636 "jump skips variable initialization");
3637 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3638 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3641 /* Look up a label because of a goto statement. This is like
3642 lookup_label, but also issues any appropriate warnings. */
3644 tree
3645 lookup_label_for_goto (location_t loc, tree name)
3647 tree label;
3648 struct c_label_vars *label_vars;
3649 unsigned int ix;
3650 tree decl;
3652 label = lookup_label (name);
3653 if (label == NULL_TREE)
3654 return NULL_TREE;
3656 /* If we are jumping to a different function, we can't issue any
3657 useful warnings. */
3658 if (DECL_CONTEXT (label) != current_function_decl)
3660 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3661 return label;
3664 label_vars = I_LABEL_BINDING (name)->u.label;
3666 /* If the label has not yet been defined, then push this goto on a
3667 list for possible later warnings. */
3668 if (label_vars->label_bindings.scope == NULL)
3670 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3672 g->loc = loc;
3673 set_spot_bindings (&g->goto_bindings, true);
3674 vec_safe_push (label_vars->gotos, g);
3675 return label;
3678 /* If there are any decls in label_vars->decls_in_scope, then this
3679 goto has missed the declaration of the decl. This happens for a
3680 case like
3681 int i = 1;
3682 lab:
3684 goto lab;
3685 Issue a warning or error. */
3686 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3687 warn_about_goto (loc, label, decl);
3689 if (label_vars->label_bindings.left_stmt_expr)
3691 error_at (loc, "jump into statement expression");
3692 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3695 return label;
3698 /* Make a label named NAME in the current function, shadowing silently
3699 any that may be inherited from containing functions or containing
3700 scopes. This is called for __label__ declarations. */
3702 tree
3703 declare_label (tree name)
3705 struct c_binding *b = I_LABEL_BINDING (name);
3706 tree label;
3707 struct c_label_vars *label_vars;
3709 /* Check to make sure that the label hasn't already been declared
3710 at this scope */
3711 if (b && B_IN_CURRENT_SCOPE (b))
3713 auto_diagnostic_group d;
3714 error ("duplicate label declaration %qE", name);
3715 locate_old_decl (b->decl);
3717 /* Just use the previous declaration. */
3718 return b->decl;
3721 label = make_label (input_location, name, false, &label_vars);
3722 C_DECLARED_LABEL_FLAG (label) = 1;
3724 /* Declared labels go in the current scope. */
3725 bind_label (name, label, current_scope, label_vars);
3727 return label;
3730 /* When we define a label, issue any appropriate warnings if there are
3731 any gotos earlier in the function which jump to this label. */
3733 static void
3734 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3736 unsigned int ix;
3737 struct c_goto_bindings *g;
3739 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3741 struct c_binding *b;
3742 struct c_scope *scope;
3744 /* We have a goto to this label. The goto is going forward. In
3745 g->scope, the goto is going to skip any binding which was
3746 defined after g->bindings_in_scope. */
3747 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3749 for (b = g->goto_bindings.scope->bindings;
3750 b != g->goto_bindings.bindings_in_scope;
3751 b = b->prev)
3753 if (decl_jump_unsafe (b->decl))
3754 warn_about_goto (g->loc, label, b->decl);
3758 /* We also need to warn about decls defined in any scopes
3759 between the scope of the label and the scope of the goto. */
3760 for (scope = label_vars->label_bindings.scope;
3761 scope != g->goto_bindings.scope;
3762 scope = scope->outer)
3764 gcc_assert (scope != NULL);
3765 if (scope->has_jump_unsafe_decl)
3767 if (scope == label_vars->label_bindings.scope)
3768 b = label_vars->label_bindings.bindings_in_scope;
3769 else
3770 b = scope->bindings;
3771 for (; b != NULL; b = b->prev)
3773 if (decl_jump_unsafe (b->decl))
3774 warn_about_goto (g->loc, label, b->decl);
3779 if (g->goto_bindings.stmt_exprs > 0)
3781 error_at (g->loc, "jump into statement expression");
3782 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3783 label);
3787 /* Now that the label is defined, we will issue warnings about
3788 subsequent gotos to this label when we see them. */
3789 vec_safe_truncate (label_vars->gotos, 0);
3790 label_vars->gotos = NULL;
3793 /* Define a label, specifying the location in the source file.
3794 Return the LABEL_DECL node for the label, if the definition is valid.
3795 Otherwise return NULL_TREE. */
3797 tree
3798 define_label (location_t location, tree name)
3800 /* Find any preexisting label with this name. It is an error
3801 if that label has already been defined in this function, or
3802 if there is a containing function with a declared label with
3803 the same name. */
3804 tree label = I_LABEL_DECL (name);
3806 if (label
3807 && ((DECL_CONTEXT (label) == current_function_decl
3808 && DECL_INITIAL (label) != NULL_TREE)
3809 || (DECL_CONTEXT (label) != current_function_decl
3810 && C_DECLARED_LABEL_FLAG (label))))
3812 auto_diagnostic_group d;
3813 error_at (location, "duplicate label %qD", label);
3814 locate_old_decl (label);
3815 return NULL_TREE;
3817 else if (label && DECL_CONTEXT (label) == current_function_decl)
3819 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3821 /* The label has been used or declared already in this function,
3822 but not defined. Update its location to point to this
3823 definition. */
3824 DECL_SOURCE_LOCATION (label) = location;
3825 set_spot_bindings (&label_vars->label_bindings, true);
3827 /* Issue warnings as required about any goto statements from
3828 earlier in the function. */
3829 check_earlier_gotos (label, label_vars);
3831 else
3833 struct c_label_vars *label_vars;
3835 /* No label binding for that identifier; make one. */
3836 label = make_label (location, name, true, &label_vars);
3838 /* Ordinary labels go in the current function scope. */
3839 bind_label (name, label, current_function_scope, label_vars);
3842 if (!in_system_header_at (input_location) && lookup_name (name))
3843 warning_at (location, OPT_Wtraditional,
3844 "traditional C lacks a separate namespace "
3845 "for labels, identifier %qE conflicts", name);
3847 /* Mark label as having been defined. */
3848 DECL_INITIAL (label) = error_mark_node;
3849 return label;
3852 /* Get the bindings for a new switch statement. This is used to issue
3853 warnings as appropriate for jumps from the switch to case or
3854 default labels. */
3856 struct c_spot_bindings *
3857 c_get_switch_bindings (void)
3859 struct c_spot_bindings *switch_bindings;
3861 switch_bindings = XNEW (struct c_spot_bindings);
3862 set_spot_bindings (switch_bindings, true);
3863 return switch_bindings;
3866 void
3867 c_release_switch_bindings (struct c_spot_bindings *bindings)
3869 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3870 XDELETE (bindings);
3873 /* This is called at the point of a case or default label to issue
3874 warnings about decls as needed. It returns true if it found an
3875 error, not just a warning. */
3877 bool
3878 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3879 location_t switch_loc, location_t case_loc)
3881 bool saw_error;
3882 struct c_scope *scope;
3884 saw_error = false;
3885 for (scope = current_scope;
3886 scope != switch_bindings->scope;
3887 scope = scope->outer)
3889 struct c_binding *b;
3891 gcc_assert (scope != NULL);
3893 if (!scope->has_jump_unsafe_decl)
3894 continue;
3896 for (b = scope->bindings; b != NULL; b = b->prev)
3898 if (decl_jump_unsafe (b->decl))
3900 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3902 saw_error = true;
3903 error_at (case_loc,
3904 ("switch jumps into scope of identifier with "
3905 "variably modified type"));
3907 else
3908 warning_at (case_loc, OPT_Wjump_misses_init,
3909 "switch jumps over variable initialization");
3910 inform (switch_loc, "switch starts here");
3911 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3912 b->decl);
3917 if (switch_bindings->stmt_exprs > 0)
3919 saw_error = true;
3920 error_at (case_loc, "switch jumps into statement expression");
3921 inform (switch_loc, "switch starts here");
3924 return saw_error;
3927 /* Given NAME, an IDENTIFIER_NODE,
3928 return the structure (or union or enum) definition for that name.
3929 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3930 CODE says which kind of type the caller wants;
3931 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3932 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3933 location where the tag was defined.
3934 If the wrong kind of type is found, an error is reported. */
3936 static tree
3937 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3938 location_t *ploc)
3940 struct c_binding *b = I_TAG_BINDING (name);
3941 bool thislevel = false;
3943 if (!b || !b->decl)
3944 return NULL_TREE;
3946 /* We only care about whether it's in this level if
3947 thislevel_only was set or it might be a type clash. */
3948 if (thislevel_only || TREE_CODE (b->decl) != code)
3950 /* For our purposes, a tag in the external scope is the same as
3951 a tag in the file scope. (Primarily relevant to Objective-C
3952 and its builtin structure tags, which get pushed before the
3953 file scope is created.) */
3954 if (B_IN_CURRENT_SCOPE (b)
3955 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3956 thislevel = true;
3959 if (thislevel_only && !thislevel)
3960 return NULL_TREE;
3962 if (TREE_CODE (b->decl) != code)
3964 /* Definition isn't the kind we were looking for. */
3965 pending_invalid_xref = name;
3966 pending_invalid_xref_location = input_location;
3968 /* If in the same binding level as a declaration as a tag
3969 of a different type, this must not be allowed to
3970 shadow that tag, so give the error immediately.
3971 (For example, "struct foo; union foo;" is invalid.) */
3972 if (thislevel)
3973 pending_xref_error ();
3976 if (ploc != NULL)
3977 *ploc = b->locus;
3979 return b->decl;
3982 /* Return true if a definition exists for NAME with code CODE. */
3984 bool
3985 tag_exists_p (enum tree_code code, tree name)
3987 struct c_binding *b = I_TAG_BINDING (name);
3989 if (b == NULL || b->decl == NULL_TREE)
3990 return false;
3991 return TREE_CODE (b->decl) == code;
3994 /* Print an error message now
3995 for a recent invalid struct, union or enum cross reference.
3996 We don't print them immediately because they are not invalid
3997 when used in the `struct foo;' construct for shadowing. */
3999 void
4000 pending_xref_error (void)
4002 if (pending_invalid_xref != NULL_TREE)
4003 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
4004 pending_invalid_xref);
4005 pending_invalid_xref = NULL_TREE;
4009 /* Look up NAME in the current scope and its superiors
4010 in the namespace of variables, functions and typedefs.
4011 Return a ..._DECL node of some kind representing its definition,
4012 or return NULL_TREE if it is undefined. */
4014 tree
4015 lookup_name (tree name)
4017 struct c_binding *b = I_SYMBOL_BINDING (name);
4018 if (b && !b->invisible)
4020 maybe_record_typedef_use (b->decl);
4021 return b->decl;
4023 return NULL_TREE;
4026 /* Similar to `lookup_name' but look only at the indicated scope. */
4028 static tree
4029 lookup_name_in_scope (tree name, struct c_scope *scope)
4031 struct c_binding *b;
4033 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
4034 if (B_IN_SCOPE (b, scope))
4035 return b->decl;
4036 return NULL_TREE;
4039 /* Look for the closest match for NAME within the currently valid
4040 scopes.
4042 This finds the identifier with the lowest Levenshtein distance to
4043 NAME. If there are multiple candidates with equal minimal distance,
4044 the first one found is returned. Scopes are searched from innermost
4045 outwards, and within a scope in reverse order of declaration, thus
4046 benefiting candidates "near" to the current scope.
4048 The function also looks for similar macro names to NAME, since a
4049 misspelled macro name will not be expanded, and hence looks like an
4050 identifier to the C frontend.
4052 It also looks for start_typename keywords, to detect "singed" vs "signed"
4053 typos.
4055 Use LOC for any deferred diagnostics. */
4057 name_hint
4058 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4060 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4062 /* First, try some well-known names in the C standard library, in case
4063 the user forgot a #include. */
4064 const char *header_hint
4065 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4067 if (header_hint)
4068 return name_hint (NULL,
4069 new suggest_missing_header (loc,
4070 IDENTIFIER_POINTER (name),
4071 header_hint));
4073 /* Only suggest names reserved for the implementation if NAME begins
4074 with an underscore. */
4075 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4077 best_match<tree, tree> bm (name);
4079 /* Look within currently valid scopes. */
4080 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4081 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4083 if (!binding->id || binding->invisible)
4084 continue;
4085 if (binding->decl == error_mark_node)
4086 continue;
4087 /* Don't use bindings from implicitly declared functions,
4088 as they were likely misspellings themselves. */
4089 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4090 if (C_DECL_IMPLICIT (binding->decl))
4091 continue;
4092 /* Don't suggest names that are reserved for use by the
4093 implementation, unless NAME began with an underscore. */
4094 if (!consider_implementation_names)
4096 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4097 if (name_reserved_for_implementation_p (suggestion_str))
4098 continue;
4100 switch (kind)
4102 case FUZZY_LOOKUP_TYPENAME:
4103 if (TREE_CODE (binding->decl) != TYPE_DECL)
4104 continue;
4105 break;
4107 case FUZZY_LOOKUP_FUNCTION_NAME:
4108 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4110 /* Allow function pointers. */
4111 if ((VAR_P (binding->decl)
4112 || TREE_CODE (binding->decl) == PARM_DECL)
4113 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4114 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4115 == FUNCTION_TYPE))
4116 break;
4117 continue;
4119 break;
4121 default:
4122 break;
4124 bm.consider (binding->id);
4127 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4129 x = SOME_OTHER_MACRO (y);
4130 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4131 as a misspelled identifier.
4133 Use the best distance so far so that a candidate is only set if
4134 a macro is better than anything so far. This allows early rejection
4135 (without calculating the edit distance) of macro names that must have
4136 distance >= bm.get_best_distance (), and means that we only get a
4137 non-NULL result for best_macro_match if it's better than any of
4138 the identifiers already checked, which avoids needless creation
4139 of identifiers for macro hashnodes. */
4140 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4141 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4142 /* If a macro is the closest so far to NAME, use it, creating an
4143 identifier tree node for it. */
4144 if (best_macro)
4146 const char *id = (const char *)best_macro->ident.str;
4147 tree macro_as_identifier
4148 = get_identifier_with_length (id, best_macro->ident.len);
4149 bm.set_best_so_far (macro_as_identifier,
4150 bmm.get_best_distance (),
4151 bmm.get_best_candidate_length ());
4154 /* Try the "start_typename" keywords to detect
4155 "singed" vs "signed" typos. */
4156 if (kind == FUZZY_LOOKUP_TYPENAME)
4158 for (unsigned i = 0; i < num_c_common_reswords; i++)
4160 const c_common_resword *resword = &c_common_reswords[i];
4161 if (!c_keyword_starts_typename (resword->rid))
4162 continue;
4163 tree resword_identifier = ridpointers [resword->rid];
4164 if (!resword_identifier)
4165 continue;
4166 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4167 bm.consider (resword_identifier);
4171 tree best = bm.get_best_meaningful_candidate ();
4172 if (best)
4173 return name_hint (IDENTIFIER_POINTER (best), NULL);
4174 else
4175 return name_hint (NULL, NULL);
4179 /* Create the predefined scalar types of C,
4180 and some nodes representing standard constants (0, 1, (void *) 0).
4181 Initialize the global scope.
4182 Make definitions for built-in primitive functions. */
4184 void
4185 c_init_decl_processing (void)
4187 location_t save_loc = input_location;
4189 /* Initialize reserved words for parser. */
4190 c_parse_init ();
4192 current_function_decl = NULL_TREE;
4194 gcc_obstack_init (&parser_obstack);
4196 /* Make the externals scope. */
4197 push_scope ();
4198 external_scope = current_scope;
4200 /* Declarations from c_common_nodes_and_builtins must not be associated
4201 with this input file, lest we get differences between using and not
4202 using preprocessed headers. */
4203 input_location = BUILTINS_LOCATION;
4205 c_common_nodes_and_builtins ();
4207 /* In C, comparisons and TRUTH_* expressions have type int. */
4208 truthvalue_type_node = integer_type_node;
4209 truthvalue_true_node = integer_one_node;
4210 truthvalue_false_node = integer_zero_node;
4212 /* Even in C99, which has a real boolean type. */
4213 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4214 boolean_type_node));
4216 input_location = save_loc;
4218 make_fname_decl = c_make_fname_decl;
4219 start_fname_decls ();
4222 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4223 give the decl, NAME is the initialization string and TYPE_DEP
4224 indicates whether NAME depended on the type of the function. As we
4225 don't yet implement delayed emission of static data, we mark the
4226 decl as emitted so it is not placed in the output. Anything using
4227 it must therefore pull out the STRING_CST initializer directly.
4228 FIXME. */
4230 static tree
4231 c_make_fname_decl (location_t loc, tree id, int type_dep)
4233 const char *name = fname_as_string (type_dep);
4234 tree decl, type, init;
4235 size_t length = strlen (name);
4237 type = build_array_type (char_type_node,
4238 build_index_type (size_int (length)));
4239 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4241 decl = build_decl (loc, VAR_DECL, id, type);
4243 TREE_STATIC (decl) = 1;
4244 TREE_READONLY (decl) = 1;
4245 DECL_ARTIFICIAL (decl) = 1;
4247 init = build_string (length + 1, name);
4248 free (CONST_CAST (char *, name));
4249 TREE_TYPE (init) = type;
4250 DECL_INITIAL (decl) = init;
4252 TREE_USED (decl) = 1;
4254 if (current_function_decl
4255 /* For invalid programs like this:
4257 void foo()
4258 const char* p = __FUNCTION__;
4260 the __FUNCTION__ is believed to appear in K&R style function
4261 parameter declarator. In that case we still don't have
4262 function_scope. */
4263 && current_function_scope)
4265 DECL_CONTEXT (decl) = current_function_decl;
4266 bind (id, decl, current_function_scope,
4267 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4270 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4272 return decl;
4275 tree
4276 c_builtin_function (tree decl)
4278 tree type = TREE_TYPE (decl);
4279 tree id = DECL_NAME (decl);
4281 const char *name = IDENTIFIER_POINTER (id);
4282 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4284 /* Should never be called on a symbol with a preexisting meaning. */
4285 gcc_assert (!I_SYMBOL_BINDING (id));
4287 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4288 UNKNOWN_LOCATION);
4290 /* Builtins in the implementation namespace are made visible without
4291 needing to be explicitly declared. See push_file_scope. */
4292 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4294 DECL_CHAIN (decl) = visible_builtins;
4295 visible_builtins = decl;
4298 return decl;
4301 tree
4302 c_builtin_function_ext_scope (tree decl)
4304 tree type = TREE_TYPE (decl);
4305 tree id = DECL_NAME (decl);
4307 const char *name = IDENTIFIER_POINTER (id);
4308 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4310 if (external_scope)
4311 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4312 UNKNOWN_LOCATION);
4314 /* Builtins in the implementation namespace are made visible without
4315 needing to be explicitly declared. See push_file_scope. */
4316 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4318 DECL_CHAIN (decl) = visible_builtins;
4319 visible_builtins = decl;
4322 return decl;
4325 /* Called when a declaration is seen that contains no names to declare.
4326 If its type is a reference to a structure, union or enum inherited
4327 from a containing scope, shadow that tag name for the current scope
4328 with a forward reference.
4329 If its type defines a new named structure or union
4330 or defines an enum, it is valid but we need not do anything here.
4331 Otherwise, it is an error. */
4333 void
4334 shadow_tag (const struct c_declspecs *declspecs)
4336 shadow_tag_warned (declspecs, 0);
4339 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4340 but no pedwarn. */
4341 void
4342 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4344 bool found_tag = false;
4346 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4348 tree value = declspecs->type;
4349 enum tree_code code = TREE_CODE (value);
4351 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4352 /* Used to test also that TYPE_SIZE (value) != 0.
4353 That caused warning for `struct foo;' at top level in the file. */
4355 tree name = TYPE_NAME (value);
4356 tree t;
4358 found_tag = true;
4360 if (declspecs->restrict_p)
4362 error ("invalid use of %<restrict%>");
4363 warned = 1;
4366 if (name == NULL_TREE)
4368 if (warned != 1 && code != ENUMERAL_TYPE)
4369 /* Empty unnamed enum OK */
4371 pedwarn (input_location, 0,
4372 "unnamed struct/union that defines no instances");
4373 warned = 1;
4376 else if (declspecs->typespec_kind != ctsk_tagdef
4377 && declspecs->typespec_kind != ctsk_tagfirstref
4378 && declspecs->storage_class != csc_none)
4380 if (warned != 1)
4381 pedwarn (input_location, 0,
4382 "empty declaration with storage class specifier "
4383 "does not redeclare tag");
4384 warned = 1;
4385 pending_xref_error ();
4387 else if (declspecs->typespec_kind != ctsk_tagdef
4388 && declspecs->typespec_kind != ctsk_tagfirstref
4389 && (declspecs->const_p
4390 || declspecs->volatile_p
4391 || declspecs->atomic_p
4392 || declspecs->restrict_p
4393 || declspecs->address_space))
4395 if (warned != 1)
4396 pedwarn (input_location, 0,
4397 "empty declaration with type qualifier "
4398 "does not redeclare tag");
4399 warned = 1;
4400 pending_xref_error ();
4402 else if (declspecs->typespec_kind != ctsk_tagdef
4403 && declspecs->typespec_kind != ctsk_tagfirstref
4404 && declspecs->alignas_p)
4406 if (warned != 1)
4407 pedwarn (input_location, 0,
4408 "empty declaration with %<_Alignas%> "
4409 "does not redeclare tag");
4410 warned = 1;
4411 pending_xref_error ();
4413 else
4415 pending_invalid_xref = NULL_TREE;
4416 t = lookup_tag (code, name, true, NULL);
4418 if (t == NULL_TREE)
4420 t = make_node (code);
4421 pushtag (input_location, name, t);
4425 else
4427 if (warned != 1 && !in_system_header_at (input_location))
4429 pedwarn (input_location, 0,
4430 "useless type name in empty declaration");
4431 warned = 1;
4435 else if (warned != 1 && !in_system_header_at (input_location)
4436 && declspecs->typedef_p)
4438 pedwarn (input_location, 0, "useless type name in empty declaration");
4439 warned = 1;
4442 pending_invalid_xref = NULL_TREE;
4444 if (declspecs->inline_p)
4446 error ("%<inline%> in empty declaration");
4447 warned = 1;
4450 if (declspecs->noreturn_p)
4452 error ("%<_Noreturn%> in empty declaration");
4453 warned = 1;
4456 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4458 error ("%<auto%> in file-scope empty declaration");
4459 warned = 1;
4462 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4464 error ("%<register%> in file-scope empty declaration");
4465 warned = 1;
4468 if (!warned && !in_system_header_at (input_location)
4469 && declspecs->storage_class != csc_none)
4471 warning (0, "useless storage class specifier in empty declaration");
4472 warned = 2;
4475 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4477 warning (0, "useless %qs in empty declaration",
4478 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4479 warned = 2;
4482 if (!warned
4483 && !in_system_header_at (input_location)
4484 && (declspecs->const_p
4485 || declspecs->volatile_p
4486 || declspecs->atomic_p
4487 || declspecs->restrict_p
4488 || declspecs->address_space))
4490 warning (0, "useless type qualifier in empty declaration");
4491 warned = 2;
4494 if (!warned && !in_system_header_at (input_location)
4495 && declspecs->alignas_p)
4497 warning (0, "useless %<_Alignas%> in empty declaration");
4498 warned = 2;
4501 if (warned != 1)
4503 if (!found_tag)
4504 pedwarn (input_location, 0, "empty declaration");
4509 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4510 bits. SPECS represents declaration specifiers that the grammar
4511 only permits to contain type qualifiers and attributes. */
4514 quals_from_declspecs (const struct c_declspecs *specs)
4516 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4517 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4518 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4519 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4520 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4521 gcc_assert (!specs->type
4522 && !specs->decl_attr
4523 && specs->typespec_word == cts_none
4524 && specs->storage_class == csc_none
4525 && !specs->typedef_p
4526 && !specs->explicit_signed_p
4527 && !specs->deprecated_p
4528 && !specs->long_p
4529 && !specs->long_long_p
4530 && !specs->short_p
4531 && !specs->signed_p
4532 && !specs->unsigned_p
4533 && !specs->complex_p
4534 && !specs->inline_p
4535 && !specs->noreturn_p
4536 && !specs->thread_p);
4537 return quals;
4540 /* Construct an array declarator. LOC is the location of the
4541 beginning of the array (usually the opening brace). EXPR is the
4542 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4543 inside the [] (to be applied to the pointer to which a parameter
4544 array is converted). STATIC_P is true if "static" is inside the
4545 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4546 VLA of unspecified length which is nevertheless a complete type,
4547 false otherwise. The field for the contained declarator is left to
4548 be filled in by set_array_declarator_inner. */
4550 struct c_declarator *
4551 build_array_declarator (location_t loc,
4552 tree expr, struct c_declspecs *quals, bool static_p,
4553 bool vla_unspec_p)
4555 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4556 struct c_declarator);
4557 declarator->id_loc = loc;
4558 declarator->kind = cdk_array;
4559 declarator->declarator = 0;
4560 declarator->u.array.dimen = expr;
4561 if (quals)
4563 declarator->u.array.attrs = quals->attrs;
4564 declarator->u.array.quals = quals_from_declspecs (quals);
4566 else
4568 declarator->u.array.attrs = NULL_TREE;
4569 declarator->u.array.quals = 0;
4571 declarator->u.array.static_p = static_p;
4572 declarator->u.array.vla_unspec_p = vla_unspec_p;
4573 if (static_p || quals != NULL)
4574 pedwarn_c90 (loc, OPT_Wpedantic,
4575 "ISO C90 does not support %<static%> or type "
4576 "qualifiers in parameter array declarators");
4577 if (vla_unspec_p)
4578 pedwarn_c90 (loc, OPT_Wpedantic,
4579 "ISO C90 does not support %<[*]%> array declarators");
4580 if (vla_unspec_p)
4582 if (!current_scope->parm_flag)
4584 /* C99 6.7.5.2p4 */
4585 error_at (loc, "%<[*]%> not allowed in other than "
4586 "function prototype scope");
4587 declarator->u.array.vla_unspec_p = false;
4588 return NULL;
4590 current_scope->had_vla_unspec = true;
4592 return declarator;
4595 /* Set the contained declarator of an array declarator. DECL is the
4596 declarator, as constructed by build_array_declarator; INNER is what
4597 appears on the left of the []. */
4599 struct c_declarator *
4600 set_array_declarator_inner (struct c_declarator *decl,
4601 struct c_declarator *inner)
4603 decl->declarator = inner;
4604 return decl;
4607 /* INIT is a constructor that forms DECL's initializer. If the final
4608 element initializes a flexible array field, add the size of that
4609 initializer to DECL's size. */
4611 static void
4612 add_flexible_array_elts_to_size (tree decl, tree init)
4614 tree elt, type;
4616 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4617 return;
4619 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4620 type = TREE_TYPE (elt);
4621 if (TREE_CODE (type) == ARRAY_TYPE
4622 && TYPE_SIZE (type) == NULL_TREE
4623 && TYPE_DOMAIN (type) != NULL_TREE
4624 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4626 complete_array_type (&type, elt, false);
4627 DECL_SIZE (decl)
4628 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4629 DECL_SIZE_UNIT (decl)
4630 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4634 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4635 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4636 before the type name, and set *EXPR_CONST_OPERANDS, if
4637 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4638 appear in a constant expression. */
4640 tree
4641 groktypename (struct c_type_name *type_name, tree *expr,
4642 bool *expr_const_operands)
4644 tree type;
4645 tree attrs = type_name->specs->attrs;
4647 type_name->specs->attrs = NULL_TREE;
4649 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4650 false, NULL, &attrs, expr, expr_const_operands,
4651 DEPRECATED_NORMAL);
4653 /* Apply attributes. */
4654 decl_attributes (&type, attrs, 0);
4656 return type;
4659 /* Wrapper for decl_attributes that adds some implicit attributes
4660 to VAR_DECLs or FUNCTION_DECLs. */
4662 static tree
4663 c_decl_attributes (tree *node, tree attributes, int flags)
4665 /* Add implicit "omp declare target" attribute if requested. */
4666 if (current_omp_declare_target_attribute
4667 && ((VAR_P (*node) && is_global_var (*node))
4668 || TREE_CODE (*node) == FUNCTION_DECL))
4670 if (VAR_P (*node)
4671 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4672 attributes = tree_cons (get_identifier ("omp declare target implicit"),
4673 NULL_TREE, attributes);
4674 else
4675 attributes = tree_cons (get_identifier ("omp declare target"),
4676 NULL_TREE, attributes);
4679 /* Look up the current declaration with all the attributes merged
4680 so far so that attributes on the current declaration that's
4681 about to be pushed that conflict with the former can be detected,
4682 diagnosed, and rejected as appropriate. */
4683 tree last_decl = lookup_name (DECL_NAME (*node));
4684 if (!last_decl)
4685 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4687 return decl_attributes (node, attributes, flags, last_decl);
4691 /* Decode a declarator in an ordinary declaration or data definition.
4692 This is called as soon as the type information and variable name
4693 have been parsed, before parsing the initializer if any.
4694 Here we create the ..._DECL node, fill in its type,
4695 and put it on the list of decls for the current context.
4696 The ..._DECL node is returned as the value.
4698 Exception: for arrays where the length is not specified,
4699 the type is left null, to be filled in by `finish_decl'.
4701 Function definitions do not come here; they go to start_function
4702 instead. However, external and forward declarations of functions
4703 do go through here. Structure field declarations are done by
4704 grokfield and not through here. */
4706 tree
4707 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4708 bool initialized, tree attributes)
4710 tree decl;
4711 tree tem;
4712 tree expr = NULL_TREE;
4713 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4715 /* An object declared as __attribute__((deprecated)) suppresses
4716 warnings of uses of other deprecated items. */
4717 if (lookup_attribute ("deprecated", attributes))
4718 deprecated_state = DEPRECATED_SUPPRESS;
4720 decl = grokdeclarator (declarator, declspecs,
4721 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4722 deprecated_state);
4723 if (!decl || decl == error_mark_node)
4724 return NULL_TREE;
4726 if (expr)
4727 add_stmt (fold_convert (void_type_node, expr));
4729 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
4730 && TREE_PUBLIC (decl))
4731 warning (OPT_Wmain, "%q+D is usually a function", decl);
4733 if (initialized)
4734 /* Is it valid for this decl to have an initializer at all?
4735 If not, set INITIALIZED to zero, which will indirectly
4736 tell 'finish_decl' to ignore the initializer once it is parsed. */
4737 switch (TREE_CODE (decl))
4739 case TYPE_DECL:
4740 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4741 initialized = false;
4742 break;
4744 case FUNCTION_DECL:
4745 error ("function %qD is initialized like a variable", decl);
4746 initialized = false;
4747 break;
4749 case PARM_DECL:
4750 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4751 error ("parameter %qD is initialized", decl);
4752 initialized = false;
4753 break;
4755 default:
4756 /* Don't allow initializations for incomplete types except for
4757 arrays which might be completed by the initialization. */
4759 /* This can happen if the array size is an undefined macro.
4760 We already gave a warning, so we don't need another one. */
4761 if (TREE_TYPE (decl) == error_mark_node)
4762 initialized = false;
4763 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4765 /* A complete type is ok if size is fixed. */
4767 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4768 || C_DECL_VARIABLE_SIZE (decl))
4770 error ("variable-sized object may not be initialized");
4771 initialized = false;
4774 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4776 error ("variable %qD has initializer but incomplete type", decl);
4777 initialized = false;
4779 else if (C_DECL_VARIABLE_SIZE (decl))
4781 /* Although C99 is unclear about whether incomplete arrays
4782 of VLAs themselves count as VLAs, it does not make
4783 sense to permit them to be initialized given that
4784 ordinary VLAs may not be initialized. */
4785 error ("variable-sized object may not be initialized");
4786 initialized = false;
4790 if (initialized)
4792 if (current_scope == file_scope)
4793 TREE_STATIC (decl) = 1;
4795 /* Tell 'pushdecl' this is an initialized decl
4796 even though we don't yet have the initializer expression.
4797 Also tell 'finish_decl' it may store the real initializer. */
4798 DECL_INITIAL (decl) = error_mark_node;
4801 /* If this is a function declaration, write a record describing it to the
4802 prototypes file (if requested). */
4804 if (TREE_CODE (decl) == FUNCTION_DECL)
4805 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4807 /* ANSI specifies that a tentative definition which is not merged with
4808 a non-tentative definition behaves exactly like a definition with an
4809 initializer equal to zero. (Section 3.7.2)
4811 -fno-common gives strict ANSI behavior, though this tends to break
4812 a large body of code that grew up without this rule.
4814 Thread-local variables are never common, since there's no entrenched
4815 body of code to break, and it allows more efficient variable references
4816 in the presence of dynamic linking. */
4818 if (VAR_P (decl)
4819 && !initialized
4820 && TREE_PUBLIC (decl)
4821 && !DECL_THREAD_LOCAL_P (decl)
4822 && !flag_no_common)
4823 DECL_COMMON (decl) = 1;
4825 /* Set attributes here so if duplicate decl, will have proper attributes. */
4826 c_decl_attributes (&decl, attributes, 0);
4828 /* Handle gnu_inline attribute. */
4829 if (declspecs->inline_p
4830 && !flag_gnu89_inline
4831 && TREE_CODE (decl) == FUNCTION_DECL
4832 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4833 || current_function_decl))
4835 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4837 else if (declspecs->storage_class != csc_static)
4838 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4841 if (TREE_CODE (decl) == FUNCTION_DECL
4842 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4844 struct c_declarator *ce = declarator;
4846 if (ce->kind == cdk_pointer)
4847 ce = declarator->declarator;
4848 if (ce->kind == cdk_function)
4850 tree args = ce->u.arg_info->parms;
4851 for (; args; args = DECL_CHAIN (args))
4853 tree type = TREE_TYPE (args);
4854 if (type && INTEGRAL_TYPE_P (type)
4855 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4856 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4861 if (TREE_CODE (decl) == FUNCTION_DECL
4862 && DECL_DECLARED_INLINE_P (decl)
4863 && DECL_UNINLINABLE (decl)
4864 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4865 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4866 decl);
4868 /* C99 6.7.4p3: An inline definition of a function with external
4869 linkage shall not contain a definition of a modifiable object
4870 with static storage duration... */
4871 if (VAR_P (decl)
4872 && current_scope != file_scope
4873 && TREE_STATIC (decl)
4874 && !TREE_READONLY (decl)
4875 && DECL_DECLARED_INLINE_P (current_function_decl)
4876 && DECL_EXTERNAL (current_function_decl))
4877 record_inline_static (input_location, current_function_decl,
4878 decl, csi_modifiable);
4880 if (c_dialect_objc ()
4881 && VAR_OR_FUNCTION_DECL_P (decl))
4882 objc_check_global_decl (decl);
4884 /* Add this decl to the current scope.
4885 TEM may equal DECL or it may be a previous decl of the same name. */
4886 tem = pushdecl (decl);
4888 if (initialized && DECL_EXTERNAL (tem))
4890 DECL_EXTERNAL (tem) = 0;
4891 TREE_STATIC (tem) = 1;
4894 return tem;
4897 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4898 DECL or the non-array element type if DECL is an uninitialized array.
4899 If that type has a const member, diagnose this. */
4901 static void
4902 diagnose_uninitialized_cst_member (tree decl, tree type)
4904 tree field;
4905 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4907 tree field_type;
4908 if (TREE_CODE (field) != FIELD_DECL)
4909 continue;
4910 field_type = strip_array_types (TREE_TYPE (field));
4912 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4914 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4915 "uninitialized const member in %qT is invalid in C++",
4916 strip_array_types (TREE_TYPE (decl)));
4917 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4920 if (RECORD_OR_UNION_TYPE_P (field_type))
4921 diagnose_uninitialized_cst_member (decl, field_type);
4925 /* Finish processing of a declaration;
4926 install its initial value.
4927 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4928 If the length of an array type is not known before,
4929 it must be determined now, from the initial value, or it is an error.
4931 INIT_LOC is the location of the initial value. */
4933 void
4934 finish_decl (tree decl, location_t init_loc, tree init,
4935 tree origtype, tree asmspec_tree)
4937 tree type;
4938 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4939 const char *asmspec = 0;
4941 /* If a name was specified, get the string. */
4942 if (VAR_OR_FUNCTION_DECL_P (decl)
4943 && DECL_FILE_SCOPE_P (decl))
4944 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4945 if (asmspec_tree)
4946 asmspec = TREE_STRING_POINTER (asmspec_tree);
4948 if (VAR_P (decl)
4949 && TREE_STATIC (decl)
4950 && global_bindings_p ())
4951 /* So decl is a global variable. Record the types it uses
4952 so that we can decide later to emit debug info for them. */
4953 record_types_used_by_current_var_decl (decl);
4955 /* If `start_decl' didn't like having an initialization, ignore it now. */
4956 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4957 init = NULL_TREE;
4959 /* Don't crash if parm is initialized. */
4960 if (TREE_CODE (decl) == PARM_DECL)
4961 init = NULL_TREE;
4963 if (init)
4964 store_init_value (init_loc, decl, init, origtype);
4966 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4967 || TREE_CODE (decl) == FIELD_DECL))
4968 objc_check_decl (decl);
4970 type = TREE_TYPE (decl);
4972 /* Deduce size of array from initialization, if not already known. */
4973 if (TREE_CODE (type) == ARRAY_TYPE
4974 && TYPE_DOMAIN (type) == NULL_TREE
4975 && TREE_CODE (decl) != TYPE_DECL)
4977 bool do_default
4978 = (TREE_STATIC (decl)
4979 /* Even if pedantic, an external linkage array
4980 may have incomplete type at first. */
4981 ? pedantic && !TREE_PUBLIC (decl)
4982 : !DECL_EXTERNAL (decl));
4983 int failure
4984 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4985 do_default);
4987 /* Get the completed type made by complete_array_type. */
4988 type = TREE_TYPE (decl);
4990 switch (failure)
4992 case 1:
4993 error ("initializer fails to determine size of %q+D", decl);
4994 break;
4996 case 2:
4997 if (do_default)
4998 error ("array size missing in %q+D", decl);
4999 break;
5001 case 3:
5002 error ("zero or negative size array %q+D", decl);
5003 break;
5005 case 0:
5006 /* For global variables, update the copy of the type that
5007 exists in the binding. */
5008 if (TREE_PUBLIC (decl))
5010 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
5011 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
5012 b_ext = b_ext->shadowed;
5013 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
5015 if (b_ext->u.type && comptypes (b_ext->u.type, type))
5016 b_ext->u.type = composite_type (b_ext->u.type, type);
5017 else
5018 b_ext->u.type = type;
5021 break;
5023 default:
5024 gcc_unreachable ();
5027 if (DECL_INITIAL (decl))
5028 TREE_TYPE (DECL_INITIAL (decl)) = type;
5030 relayout_decl (decl);
5033 if (VAR_P (decl))
5035 if (init && TREE_CODE (init) == CONSTRUCTOR)
5036 add_flexible_array_elts_to_size (decl, init);
5038 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5039 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5040 layout_decl (decl, 0);
5042 if (DECL_SIZE (decl) == NULL_TREE
5043 /* Don't give an error if we already gave one earlier. */
5044 && TREE_TYPE (decl) != error_mark_node
5045 && (TREE_STATIC (decl)
5046 /* A static variable with an incomplete type
5047 is an error if it is initialized.
5048 Also if it is not file scope.
5049 Otherwise, let it through, but if it is not `extern'
5050 then it may cause an error message later. */
5051 ? (DECL_INITIAL (decl) != NULL_TREE
5052 || !DECL_FILE_SCOPE_P (decl))
5053 /* An automatic variable with an incomplete type
5054 is an error. */
5055 : !DECL_EXTERNAL (decl)))
5057 error ("storage size of %q+D isn%'t known", decl);
5058 TREE_TYPE (decl) = error_mark_node;
5061 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5062 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5063 && DECL_SIZE (decl) == NULL_TREE
5064 && TREE_STATIC (decl))
5065 incomplete_record_decls.safe_push (decl);
5067 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5069 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5070 constant_expression_warning (DECL_SIZE (decl));
5071 else
5073 error ("storage size of %q+D isn%'t constant", decl);
5074 TREE_TYPE (decl) = error_mark_node;
5078 if (TREE_USED (type))
5080 TREE_USED (decl) = 1;
5081 DECL_READ_P (decl) = 1;
5085 /* If this is a function and an assembler name is specified, reset DECL_RTL
5086 so we can give it its new name. Also, update builtin_decl if it
5087 was a normal built-in. */
5088 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5090 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5091 set_builtin_user_assembler_name (decl, asmspec);
5092 set_user_assembler_name (decl, asmspec);
5095 /* If #pragma weak was used, mark the decl weak now. */
5096 maybe_apply_pragma_weak (decl);
5098 /* Output the assembler code and/or RTL code for variables and functions,
5099 unless the type is an undefined structure or union.
5100 If not, it will get done when the type is completed. */
5102 if (VAR_OR_FUNCTION_DECL_P (decl))
5104 /* Determine the ELF visibility. */
5105 if (TREE_PUBLIC (decl))
5106 c_determine_visibility (decl);
5108 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5109 if (c_dialect_objc ())
5110 objc_check_decl (decl);
5112 if (asmspec)
5114 /* If this is not a static variable, issue a warning.
5115 It doesn't make any sense to give an ASMSPEC for an
5116 ordinary, non-register local variable. Historically,
5117 GCC has accepted -- but ignored -- the ASMSPEC in
5118 this case. */
5119 if (!DECL_FILE_SCOPE_P (decl)
5120 && VAR_P (decl)
5121 && !C_DECL_REGISTER (decl)
5122 && !TREE_STATIC (decl))
5123 warning (0, "ignoring asm-specifier for non-static local "
5124 "variable %q+D", decl);
5125 else
5126 set_user_assembler_name (decl, asmspec);
5129 if (DECL_FILE_SCOPE_P (decl))
5131 if (DECL_INITIAL (decl) == NULL_TREE
5132 || DECL_INITIAL (decl) == error_mark_node)
5133 /* Don't output anything
5134 when a tentative file-scope definition is seen.
5135 But at end of compilation, do output code for them. */
5136 DECL_DEFER_OUTPUT (decl) = 1;
5137 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5138 DECL_HARD_REGISTER (decl) = 1;
5139 rest_of_decl_compilation (decl, true, 0);
5141 else
5143 /* In conjunction with an ASMSPEC, the `register'
5144 keyword indicates that we should place the variable
5145 in a particular register. */
5146 if (asmspec && C_DECL_REGISTER (decl))
5148 DECL_HARD_REGISTER (decl) = 1;
5149 /* This cannot be done for a structure with volatile
5150 fields, on which DECL_REGISTER will have been
5151 reset. */
5152 if (!DECL_REGISTER (decl))
5153 error ("cannot put object with volatile field into register");
5156 if (TREE_CODE (decl) != FUNCTION_DECL)
5158 /* If we're building a variable sized type, and we might be
5159 reachable other than via the top of the current binding
5160 level, then create a new BIND_EXPR so that we deallocate
5161 the object at the right time. */
5162 /* Note that DECL_SIZE can be null due to errors. */
5163 if (DECL_SIZE (decl)
5164 && !TREE_CONSTANT (DECL_SIZE (decl))
5165 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5167 tree bind;
5168 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5169 TREE_SIDE_EFFECTS (bind) = 1;
5170 add_stmt (bind);
5171 BIND_EXPR_BODY (bind) = push_stmt_list ();
5173 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5174 DECL_EXPR, decl));
5179 if (!DECL_FILE_SCOPE_P (decl))
5181 /* Recompute the RTL of a local array now
5182 if it used to be an incomplete type. */
5183 if (was_incomplete && !is_global_var (decl))
5185 /* If we used it already as memory, it must stay in memory. */
5186 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5187 /* If it's still incomplete now, no init will save it. */
5188 if (DECL_SIZE (decl) == NULL_TREE)
5189 DECL_INITIAL (decl) = NULL_TREE;
5194 if (TREE_CODE (decl) == TYPE_DECL)
5196 if (!DECL_FILE_SCOPE_P (decl)
5197 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5198 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5200 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5203 /* Install a cleanup (aka destructor) if one was given. */
5204 if (VAR_P (decl) && !TREE_STATIC (decl))
5206 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5207 if (attr)
5209 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5210 tree cleanup_decl = lookup_name (cleanup_id);
5211 tree cleanup;
5212 vec<tree, va_gc> *v;
5214 /* Build "cleanup(&decl)" for the destructor. */
5215 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5216 vec_alloc (v, 1);
5217 v->quick_push (cleanup);
5218 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5219 vNULL, cleanup_decl, v, NULL);
5220 vec_free (v);
5222 /* Don't warn about decl unused; the cleanup uses it. */
5223 TREE_USED (decl) = 1;
5224 TREE_USED (cleanup_decl) = 1;
5225 DECL_READ_P (decl) = 1;
5227 push_cleanup (decl, cleanup, false);
5231 if (warn_cxx_compat
5232 && VAR_P (decl)
5233 && !DECL_EXTERNAL (decl)
5234 && DECL_INITIAL (decl) == NULL_TREE)
5236 type = strip_array_types (type);
5237 if (TREE_READONLY (decl))
5238 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5239 "uninitialized const %qD is invalid in C++", decl);
5240 else if (RECORD_OR_UNION_TYPE_P (type)
5241 && C_TYPE_FIELDS_READONLY (type))
5242 diagnose_uninitialized_cst_member (decl, type);
5245 if (flag_openmp
5246 && VAR_P (decl)
5247 && lookup_attribute ("omp declare target implicit",
5248 DECL_ATTRIBUTES (decl)))
5250 DECL_ATTRIBUTES (decl)
5251 = remove_attribute ("omp declare target implicit",
5252 DECL_ATTRIBUTES (decl));
5253 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (decl)))
5254 error ("%q+D in declare target directive does not have mappable type",
5255 decl);
5256 else if (!lookup_attribute ("omp declare target",
5257 DECL_ATTRIBUTES (decl))
5258 && !lookup_attribute ("omp declare target link",
5259 DECL_ATTRIBUTES (decl)))
5260 DECL_ATTRIBUTES (decl)
5261 = tree_cons (get_identifier ("omp declare target"),
5262 NULL_TREE, DECL_ATTRIBUTES (decl));
5265 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5268 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5269 EXPR is NULL or a pointer to an expression that needs to be
5270 evaluated for the side effects of array size expressions in the
5271 parameters. */
5273 tree
5274 grokparm (const struct c_parm *parm, tree *expr)
5276 tree attrs = parm->attrs;
5277 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5278 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5280 decl_attributes (&decl, attrs, 0);
5282 return decl;
5285 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5286 and push that on the current scope. EXPR is a pointer to an
5287 expression that needs to be evaluated for the side effects of array
5288 size expressions in the parameters. */
5290 void
5291 push_parm_decl (const struct c_parm *parm, tree *expr)
5293 tree attrs = parm->attrs;
5294 tree decl;
5296 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5297 &attrs, expr, NULL, DEPRECATED_NORMAL);
5298 if (decl && DECL_P (decl))
5299 DECL_SOURCE_LOCATION (decl) = parm->loc;
5300 decl_attributes (&decl, attrs, 0);
5302 decl = pushdecl (decl);
5304 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5307 /* Mark all the parameter declarations to date as forward decls.
5308 Also diagnose use of this extension. */
5310 void
5311 mark_forward_parm_decls (void)
5313 struct c_binding *b;
5315 if (pedantic && !current_scope->warned_forward_parm_decls)
5317 pedwarn (input_location, OPT_Wpedantic,
5318 "ISO C forbids forward parameter declarations");
5319 current_scope->warned_forward_parm_decls = true;
5322 for (b = current_scope->bindings; b; b = b->prev)
5323 if (TREE_CODE (b->decl) == PARM_DECL)
5324 TREE_ASM_WRITTEN (b->decl) = 1;
5327 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5328 literal, which may be an incomplete array type completed by the
5329 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5330 literal. NON_CONST is true if the initializers contain something
5331 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5332 it is the (valid) alignment for this compound literal, as specified
5333 with _Alignas. */
5335 tree
5336 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5337 unsigned int alignas_align)
5339 /* We do not use start_decl here because we have a type, not a declarator;
5340 and do not use finish_decl because the decl should be stored inside
5341 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5342 tree decl;
5343 tree complit;
5344 tree stmt;
5346 if (type == error_mark_node
5347 || init == error_mark_node)
5348 return error_mark_node;
5350 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5351 DECL_EXTERNAL (decl) = 0;
5352 TREE_PUBLIC (decl) = 0;
5353 TREE_STATIC (decl) = (current_scope == file_scope);
5354 DECL_CONTEXT (decl) = current_function_decl;
5355 TREE_USED (decl) = 1;
5356 DECL_READ_P (decl) = 1;
5357 DECL_ARTIFICIAL (decl) = 1;
5358 DECL_IGNORED_P (decl) = 1;
5359 TREE_TYPE (decl) = type;
5360 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5361 if (alignas_align)
5363 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5364 DECL_USER_ALIGN (decl) = 1;
5366 store_init_value (loc, decl, init, NULL_TREE);
5368 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5370 int failure = complete_array_type (&TREE_TYPE (decl),
5371 DECL_INITIAL (decl), true);
5372 /* If complete_array_type returns 3, it means that the
5373 initial value of the compound literal is empty. Allow it. */
5374 gcc_assert (failure == 0 || failure == 3);
5376 type = TREE_TYPE (decl);
5377 TREE_TYPE (DECL_INITIAL (decl)) = type;
5380 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5382 c_incomplete_type_error (loc, NULL_TREE, type);
5383 return error_mark_node;
5386 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5387 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5388 TREE_SIDE_EFFECTS (complit) = 1;
5390 layout_decl (decl, 0);
5392 if (TREE_STATIC (decl))
5394 /* This decl needs a name for the assembler output. */
5395 set_compound_literal_name (decl);
5396 DECL_DEFER_OUTPUT (decl) = 1;
5397 DECL_COMDAT (decl) = 1;
5398 pushdecl (decl);
5399 rest_of_decl_compilation (decl, 1, 0);
5401 else
5402 pushdecl (decl);
5404 if (non_const)
5406 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5407 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5410 return complit;
5413 /* Check the type of a compound literal. Here we just check that it
5414 is valid for C++. */
5416 void
5417 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5419 if (warn_cxx_compat
5420 && (type_name->specs->typespec_kind == ctsk_tagdef
5421 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5422 warning_at (loc, OPT_Wc___compat,
5423 "defining a type in a compound literal is invalid in C++");
5426 /* Determine whether TYPE is a structure with a flexible array member,
5427 or a union containing such a structure (possibly recursively). */
5429 static bool
5430 flexible_array_type_p (tree type)
5432 tree x;
5433 switch (TREE_CODE (type))
5435 case RECORD_TYPE:
5436 x = TYPE_FIELDS (type);
5437 if (x == NULL_TREE)
5438 return false;
5439 while (DECL_CHAIN (x) != NULL_TREE)
5440 x = DECL_CHAIN (x);
5441 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5442 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5443 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5444 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5445 return true;
5446 return false;
5447 case UNION_TYPE:
5448 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5450 if (flexible_array_type_p (TREE_TYPE (x)))
5451 return true;
5453 return false;
5454 default:
5455 return false;
5459 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5460 replacing with appropriate values if they are invalid. */
5462 static void
5463 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5464 tree orig_name)
5466 tree type_mv;
5467 unsigned int max_width;
5468 unsigned HOST_WIDE_INT w;
5469 const char *name = (orig_name
5470 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5471 : _("<anonymous>"));
5473 /* Detect and ignore out of range field width and process valid
5474 field widths. */
5475 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5477 error_at (loc, "bit-field %qs width not an integer constant", name);
5478 *width = integer_one_node;
5480 else
5482 if (TREE_CODE (*width) != INTEGER_CST)
5484 *width = c_fully_fold (*width, false, NULL);
5485 if (TREE_CODE (*width) == INTEGER_CST)
5486 pedwarn (loc, OPT_Wpedantic,
5487 "bit-field %qs width not an integer constant expression",
5488 name);
5490 if (TREE_CODE (*width) != INTEGER_CST)
5492 error_at (loc, "bit-field %qs width not an integer constant", name);
5493 *width = integer_one_node;
5495 constant_expression_warning (*width);
5496 if (tree_int_cst_sgn (*width) < 0)
5498 error_at (loc, "negative width in bit-field %qs", name);
5499 *width = integer_one_node;
5501 else if (integer_zerop (*width) && orig_name)
5503 error_at (loc, "zero width for bit-field %qs", name);
5504 *width = integer_one_node;
5508 /* Detect invalid bit-field type. */
5509 if (TREE_CODE (*type) != INTEGER_TYPE
5510 && TREE_CODE (*type) != BOOLEAN_TYPE
5511 && TREE_CODE (*type) != ENUMERAL_TYPE)
5513 error_at (loc, "bit-field %qs has invalid type", name);
5514 *type = unsigned_type_node;
5517 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5519 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5520 name);
5521 *type = unsigned_type_node;
5524 type_mv = TYPE_MAIN_VARIANT (*type);
5525 if (!in_system_header_at (input_location)
5526 && type_mv != integer_type_node
5527 && type_mv != unsigned_type_node
5528 && type_mv != boolean_type_node)
5529 pedwarn_c90 (loc, OPT_Wpedantic,
5530 "type of bit-field %qs is a GCC extension", name);
5532 max_width = TYPE_PRECISION (*type);
5534 if (compare_tree_int (*width, max_width) > 0)
5536 error_at (loc, "width of %qs exceeds its type", name);
5537 w = max_width;
5538 *width = build_int_cst (integer_type_node, w);
5540 else
5541 w = tree_to_uhwi (*width);
5543 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5545 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5546 if (!lt
5547 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5548 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5549 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5555 /* Print warning about variable length array if necessary. */
5557 static void
5558 warn_variable_length_array (tree name, tree size)
5560 if (TREE_CONSTANT (size))
5562 if (name)
5563 pedwarn_c90 (input_location, OPT_Wvla,
5564 "ISO C90 forbids array %qE whose size "
5565 "can%'t be evaluated", name);
5566 else
5567 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5568 "whose size can%'t be evaluated");
5570 else
5572 if (name)
5573 pedwarn_c90 (input_location, OPT_Wvla,
5574 "ISO C90 forbids variable length array %qE", name);
5575 else
5576 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5577 "length array");
5581 /* Print warning about defaulting to int if necessary. */
5583 static void
5584 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5586 diagnostic_info diagnostic;
5587 va_list ap;
5588 rich_location richloc (line_table, location);
5590 va_start (ap, gmsgid);
5591 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5592 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5593 diagnostic.option_index = opt;
5594 diagnostic_report_diagnostic (global_dc, &diagnostic);
5595 va_end (ap);
5598 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5599 considering only those c_declspec_words found in LIST, which
5600 must be terminated by cdw_number_of_elements. */
5602 static location_t
5603 smallest_type_quals_location (const location_t *locations,
5604 const c_declspec_word *list)
5606 location_t loc = UNKNOWN_LOCATION;
5607 while (*list != cdw_number_of_elements)
5609 location_t newloc = locations[*list];
5610 if (loc == UNKNOWN_LOCATION
5611 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5612 loc = newloc;
5613 list++;
5616 return loc;
5619 /* Given declspecs and a declarator,
5620 determine the name and type of the object declared
5621 and construct a ..._DECL node for it.
5622 (In one case we can return a ..._TYPE node instead.
5623 For invalid input we sometimes return NULL_TREE.)
5625 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5627 DECL_CONTEXT says which syntactic context this declaration is in:
5628 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5629 FUNCDEF for a function definition. Like NORMAL but a few different
5630 error messages in each case. Return value may be zero meaning
5631 this definition is too screwy to try to parse.
5632 PARM for a parameter declaration (either within a function prototype
5633 or before a function body). Make a PARM_DECL, or return void_type_node.
5634 TYPENAME if for a typename (in a cast or sizeof).
5635 Don't make a DECL node; just return the ..._TYPE node.
5636 FIELD for a struct or union field; make a FIELD_DECL.
5637 INITIALIZED is true if the decl has an initializer.
5638 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5639 representing the width of the bit-field.
5640 DECL_ATTRS points to the list of attributes that should be added to this
5641 decl. Any nested attributes that belong on the decl itself will be
5642 added to this list.
5643 If EXPR is not NULL, any expressions that need to be evaluated as
5644 part of evaluating variably modified types will be stored in *EXPR.
5645 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5646 set to indicate whether operands in *EXPR can be used in constant
5647 expressions.
5648 DEPRECATED_STATE is a deprecated_states value indicating whether
5649 deprecation warnings should be suppressed.
5651 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5652 It may also be so in the PARM case, for a prototype where the
5653 argument type is specified but not the name.
5655 This function is where the complicated C meanings of `static'
5656 and `extern' are interpreted. */
5658 static tree
5659 grokdeclarator (const struct c_declarator *declarator,
5660 struct c_declspecs *declspecs,
5661 enum decl_context decl_context, bool initialized, tree *width,
5662 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5663 enum deprecated_states deprecated_state)
5665 tree type = declspecs->type;
5666 bool threadp = declspecs->thread_p;
5667 enum c_storage_class storage_class = declspecs->storage_class;
5668 int constp;
5669 int restrictp;
5670 int volatilep;
5671 int atomicp;
5672 int type_quals = TYPE_UNQUALIFIED;
5673 tree name = NULL_TREE;
5674 bool funcdef_flag = false;
5675 bool funcdef_syntax = false;
5676 bool size_varies = false;
5677 tree decl_attr = declspecs->decl_attr;
5678 int array_ptr_quals = TYPE_UNQUALIFIED;
5679 tree array_ptr_attrs = NULL_TREE;
5680 bool array_parm_static = false;
5681 bool array_parm_vla_unspec_p = false;
5682 tree returned_attrs = NULL_TREE;
5683 bool bitfield = width != NULL;
5684 tree element_type;
5685 tree orig_qual_type = NULL;
5686 size_t orig_qual_indirect = 0;
5687 struct c_arg_info *arg_info = 0;
5688 addr_space_t as1, as2, address_space;
5689 location_t loc = UNKNOWN_LOCATION;
5690 tree expr_dummy;
5691 bool expr_const_operands_dummy;
5692 enum c_declarator_kind first_non_attr_kind;
5693 unsigned int alignas_align = 0;
5695 if (TREE_CODE (type) == ERROR_MARK)
5696 return error_mark_node;
5697 if (expr == NULL)
5699 expr = &expr_dummy;
5700 expr_dummy = NULL_TREE;
5702 if (expr_const_operands == NULL)
5703 expr_const_operands = &expr_const_operands_dummy;
5705 if (declspecs->expr)
5707 if (*expr)
5708 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5709 declspecs->expr);
5710 else
5711 *expr = declspecs->expr;
5713 *expr_const_operands = declspecs->expr_const_operands;
5715 if (decl_context == FUNCDEF)
5716 funcdef_flag = true, decl_context = NORMAL;
5718 /* Look inside a declarator for the name being declared
5719 and get it as an IDENTIFIER_NODE, for an error message. */
5721 const struct c_declarator *decl = declarator;
5723 first_non_attr_kind = cdk_attrs;
5724 while (decl)
5725 switch (decl->kind)
5727 case cdk_array:
5728 loc = decl->id_loc;
5729 /* FALL THRU. */
5731 case cdk_function:
5732 case cdk_pointer:
5733 funcdef_syntax = (decl->kind == cdk_function);
5734 if (first_non_attr_kind == cdk_attrs)
5735 first_non_attr_kind = decl->kind;
5736 decl = decl->declarator;
5737 break;
5739 case cdk_attrs:
5740 decl = decl->declarator;
5741 break;
5743 case cdk_id:
5744 loc = decl->id_loc;
5745 if (decl->u.id)
5746 name = decl->u.id;
5747 if (first_non_attr_kind == cdk_attrs)
5748 first_non_attr_kind = decl->kind;
5749 decl = 0;
5750 break;
5752 default:
5753 gcc_unreachable ();
5755 if (name == NULL_TREE)
5757 gcc_assert (decl_context == PARM
5758 || decl_context == TYPENAME
5759 || (decl_context == FIELD
5760 && declarator->kind == cdk_id));
5761 gcc_assert (!initialized);
5765 /* A function definition's declarator must have the form of
5766 a function declarator. */
5768 if (funcdef_flag && !funcdef_syntax)
5769 return NULL_TREE;
5771 /* If this looks like a function definition, make it one,
5772 even if it occurs where parms are expected.
5773 Then store_parm_decls will reject it and not use it as a parm. */
5774 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5775 decl_context = PARM;
5777 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5778 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5780 if ((decl_context == NORMAL || decl_context == FIELD)
5781 && current_scope == file_scope
5782 && variably_modified_type_p (type, NULL_TREE))
5784 if (name)
5785 error_at (loc, "variably modified %qE at file scope", name);
5786 else
5787 error_at (loc, "variably modified field at file scope");
5788 type = integer_type_node;
5791 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5793 /* Diagnose defaulting to "int". */
5795 if (declspecs->default_int_p && !in_system_header_at (input_location))
5797 /* Issue a warning if this is an ISO C 99 program or if
5798 -Wreturn-type and this is a function, or if -Wimplicit;
5799 prefer the former warning since it is more explicit. */
5800 if ((warn_implicit_int || warn_return_type > 0 || flag_isoc99)
5801 && funcdef_flag)
5802 warn_about_return_type = 1;
5803 else
5805 if (name)
5806 warn_defaults_to (loc, OPT_Wimplicit_int,
5807 "type defaults to %<int%> in declaration "
5808 "of %qE", name);
5809 else
5810 warn_defaults_to (loc, OPT_Wimplicit_int,
5811 "type defaults to %<int%> in type name");
5815 /* Adjust the type if a bit-field is being declared,
5816 -funsigned-bitfields applied and the type is not explicitly
5817 "signed". */
5818 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5819 && TREE_CODE (type) == INTEGER_TYPE)
5820 type = unsigned_type_for (type);
5822 /* Figure out the type qualifiers for the declaration. There are
5823 two ways a declaration can become qualified. One is something
5824 like `const int i' where the `const' is explicit. Another is
5825 something like `typedef const int CI; CI i' where the type of the
5826 declaration contains the `const'. A third possibility is that
5827 there is a type qualifier on the element type of a typedefed
5828 array type, in which case we should extract that qualifier so
5829 that c_apply_type_quals_to_decl receives the full list of
5830 qualifiers to work with (C90 is not entirely clear about whether
5831 duplicate qualifiers should be diagnosed in this case, but it
5832 seems most appropriate to do so). */
5833 element_type = strip_array_types (type);
5834 constp = declspecs->const_p + TYPE_READONLY (element_type);
5835 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5836 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5837 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5838 as1 = declspecs->address_space;
5839 as2 = TYPE_ADDR_SPACE (element_type);
5840 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5842 if (constp > 1)
5843 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5844 if (restrictp > 1)
5845 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5846 if (volatilep > 1)
5847 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5848 if (atomicp > 1)
5849 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5851 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5852 error_at (loc, "conflicting named address spaces (%s vs %s)",
5853 c_addr_space_name (as1), c_addr_space_name (as2));
5855 if ((TREE_CODE (type) == ARRAY_TYPE
5856 || first_non_attr_kind == cdk_array)
5857 && TYPE_QUALS (element_type))
5859 orig_qual_type = type;
5860 type = TYPE_MAIN_VARIANT (type);
5862 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5863 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5864 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5865 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5866 | ENCODE_QUAL_ADDR_SPACE (address_space));
5867 if (type_quals != TYPE_QUALS (element_type))
5868 orig_qual_type = NULL_TREE;
5870 /* Applying the _Atomic qualifier to an array type (through the use
5871 of typedefs or typeof) must be detected here. If the qualifier
5872 is introduced later, any appearance of applying it to an array is
5873 actually applying it to an element of that array. */
5874 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5875 error_at (loc, "%<_Atomic%>-qualified array type");
5877 /* Warn about storage classes that are invalid for certain
5878 kinds of declarations (parameters, typenames, etc.). */
5880 if (funcdef_flag
5881 && (threadp
5882 || storage_class == csc_auto
5883 || storage_class == csc_register
5884 || storage_class == csc_typedef))
5886 if (storage_class == csc_auto)
5887 pedwarn (loc,
5888 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5889 "function definition declared %<auto%>");
5890 if (storage_class == csc_register)
5891 error_at (loc, "function definition declared %<register%>");
5892 if (storage_class == csc_typedef)
5893 error_at (loc, "function definition declared %<typedef%>");
5894 if (threadp)
5895 error_at (loc, "function definition declared %qs",
5896 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5897 threadp = false;
5898 if (storage_class == csc_auto
5899 || storage_class == csc_register
5900 || storage_class == csc_typedef)
5901 storage_class = csc_none;
5903 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5905 if (decl_context == PARM && storage_class == csc_register)
5907 else
5909 switch (decl_context)
5911 case FIELD:
5912 if (name)
5913 error_at (loc, "storage class specified for structure "
5914 "field %qE", name);
5915 else
5916 error_at (loc, "storage class specified for structure field");
5917 break;
5918 case PARM:
5919 if (name)
5920 error_at (loc, "storage class specified for parameter %qE",
5921 name);
5922 else
5923 error_at (loc, "storage class specified for unnamed parameter");
5924 break;
5925 default:
5926 error_at (loc, "storage class specified for typename");
5927 break;
5929 storage_class = csc_none;
5930 threadp = false;
5933 else if (storage_class == csc_extern
5934 && initialized
5935 && !funcdef_flag)
5937 /* 'extern' with initialization is invalid if not at file scope. */
5938 if (current_scope == file_scope)
5940 /* It is fine to have 'extern const' when compiling at C
5941 and C++ intersection. */
5942 if (!(warn_cxx_compat && constp))
5943 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5944 name);
5946 else
5947 error_at (loc, "%qE has both %<extern%> and initializer", name);
5949 else if (current_scope == file_scope)
5951 if (storage_class == csc_auto)
5952 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5953 name);
5954 if (pedantic && storage_class == csc_register)
5955 pedwarn (input_location, OPT_Wpedantic,
5956 "file-scope declaration of %qE specifies %<register%>", name);
5958 else
5960 if (storage_class == csc_extern && funcdef_flag)
5961 error_at (loc, "nested function %qE declared %<extern%>", name);
5962 else if (threadp && storage_class == csc_none)
5964 error_at (loc, "function-scope %qE implicitly auto and declared "
5965 "%qs", name,
5966 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5967 threadp = false;
5971 /* Now figure out the structure of the declarator proper.
5972 Descend through it, creating more complex types, until we reach
5973 the declared identifier (or NULL_TREE, in an absolute declarator).
5974 At each stage we maintain an unqualified version of the type
5975 together with any qualifiers that should be applied to it with
5976 c_build_qualified_type; this way, array types including
5977 multidimensional array types are first built up in unqualified
5978 form and then the qualified form is created with
5979 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5981 while (declarator && declarator->kind != cdk_id)
5983 if (type == error_mark_node)
5985 declarator = declarator->declarator;
5986 continue;
5989 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5990 a cdk_pointer (for *...),
5991 a cdk_function (for ...(...)),
5992 a cdk_attrs (for nested attributes),
5993 or a cdk_id (for the name being declared
5994 or the place in an absolute declarator
5995 where the name was omitted).
5996 For the last case, we have just exited the loop.
5998 At this point, TYPE is the type of elements of an array,
5999 or for a function to return, or for a pointer to point to.
6000 After this sequence of ifs, TYPE is the type of the
6001 array or function or pointer, and DECLARATOR has had its
6002 outermost layer removed. */
6004 if (array_ptr_quals != TYPE_UNQUALIFIED
6005 || array_ptr_attrs != NULL_TREE
6006 || array_parm_static)
6008 /* Only the innermost declarator (making a parameter be of
6009 array type which is converted to pointer type)
6010 may have static or type qualifiers. */
6011 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6012 array_ptr_quals = TYPE_UNQUALIFIED;
6013 array_ptr_attrs = NULL_TREE;
6014 array_parm_static = false;
6017 switch (declarator->kind)
6019 case cdk_attrs:
6021 /* A declarator with embedded attributes. */
6022 tree attrs = declarator->u.attrs;
6023 const struct c_declarator *inner_decl;
6024 int attr_flags = 0;
6025 declarator = declarator->declarator;
6026 inner_decl = declarator;
6027 while (inner_decl->kind == cdk_attrs)
6028 inner_decl = inner_decl->declarator;
6029 if (inner_decl->kind == cdk_id)
6030 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
6031 else if (inner_decl->kind == cdk_function)
6032 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
6033 else if (inner_decl->kind == cdk_array)
6034 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
6035 returned_attrs = decl_attributes (&type,
6036 chainon (returned_attrs, attrs),
6037 attr_flags);
6038 break;
6040 case cdk_array:
6042 tree itype = NULL_TREE;
6043 tree size = declarator->u.array.dimen;
6044 /* The index is a signed object `sizetype' bits wide. */
6045 tree index_type = c_common_signed_type (sizetype);
6047 array_ptr_quals = declarator->u.array.quals;
6048 array_ptr_attrs = declarator->u.array.attrs;
6049 array_parm_static = declarator->u.array.static_p;
6050 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6052 declarator = declarator->declarator;
6054 /* Check for some types that there cannot be arrays of. */
6056 if (VOID_TYPE_P (type))
6058 if (name)
6059 error_at (loc, "declaration of %qE as array of voids", name);
6060 else
6061 error_at (loc, "declaration of type name as array of voids");
6062 type = error_mark_node;
6065 if (TREE_CODE (type) == FUNCTION_TYPE)
6067 if (name)
6068 error_at (loc, "declaration of %qE as array of functions",
6069 name);
6070 else
6071 error_at (loc, "declaration of type name as array of "
6072 "functions");
6073 type = error_mark_node;
6076 if (pedantic && !in_system_header_at (input_location)
6077 && flexible_array_type_p (type))
6078 pedwarn (loc, OPT_Wpedantic,
6079 "invalid use of structure with flexible array member");
6081 if (size == error_mark_node)
6082 type = error_mark_node;
6084 if (type == error_mark_node)
6085 continue;
6087 /* If size was specified, set ITYPE to a range-type for
6088 that size. Otherwise, ITYPE remains null. finish_decl
6089 may figure it out from an initial value. */
6091 if (size)
6093 bool size_maybe_const = true;
6094 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6095 && !TREE_OVERFLOW (size));
6096 bool this_size_varies = false;
6098 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6099 lvalue. */
6100 STRIP_TYPE_NOPS (size);
6102 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6104 if (name)
6105 error_at (loc, "size of array %qE has non-integer type",
6106 name);
6107 else
6108 error_at (loc,
6109 "size of unnamed array has non-integer type");
6110 size = integer_one_node;
6112 /* This can happen with enum forward declaration. */
6113 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6115 if (name)
6116 error_at (loc, "size of array %qE has incomplete type",
6117 name);
6118 else
6119 error_at (loc, "size of unnamed array has incomplete "
6120 "type");
6121 size = integer_one_node;
6124 size = c_fully_fold (size, false, &size_maybe_const);
6126 if (pedantic && size_maybe_const && integer_zerop (size))
6128 if (name)
6129 pedwarn (loc, OPT_Wpedantic,
6130 "ISO C forbids zero-size array %qE", name);
6131 else
6132 pedwarn (loc, OPT_Wpedantic,
6133 "ISO C forbids zero-size array");
6136 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6138 constant_expression_warning (size);
6139 if (tree_int_cst_sgn (size) < 0)
6141 if (name)
6142 error_at (loc, "size of array %qE is negative", name);
6143 else
6144 error_at (loc, "size of unnamed array is negative");
6145 size = integer_one_node;
6147 /* Handle a size folded to an integer constant but
6148 not an integer constant expression. */
6149 if (!size_int_const)
6151 /* If this is a file scope declaration of an
6152 ordinary identifier, this is invalid code;
6153 diagnosing it here and not subsequently
6154 treating the type as variable-length avoids
6155 more confusing diagnostics later. */
6156 if ((decl_context == NORMAL || decl_context == FIELD)
6157 && current_scope == file_scope)
6158 pedwarn (input_location, 0,
6159 "variably modified %qE at file scope",
6160 name);
6161 else
6162 this_size_varies = size_varies = true;
6163 warn_variable_length_array (name, size);
6166 else if ((decl_context == NORMAL || decl_context == FIELD)
6167 && current_scope == file_scope)
6169 error_at (loc, "variably modified %qE at file scope", name);
6170 size = integer_one_node;
6172 else
6174 /* Make sure the array size remains visibly
6175 nonconstant even if it is (eg) a const variable
6176 with known value. */
6177 this_size_varies = size_varies = true;
6178 warn_variable_length_array (name, size);
6179 if (sanitize_flags_p (SANITIZE_VLA)
6180 && current_function_decl != NULL_TREE
6181 && decl_context == NORMAL)
6183 /* Evaluate the array size only once. */
6184 size = save_expr (size);
6185 size = c_fully_fold (size, false, NULL);
6186 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6187 ubsan_instrument_vla (loc, size),
6188 size);
6192 if (integer_zerop (size) && !this_size_varies)
6194 /* A zero-length array cannot be represented with
6195 an unsigned index type, which is what we'll
6196 get with build_index_type. Create an
6197 open-ended range instead. */
6198 itype = build_range_type (sizetype, size, NULL_TREE);
6200 else
6202 /* Arrange for the SAVE_EXPR on the inside of the
6203 MINUS_EXPR, which allows the -1 to get folded
6204 with the +1 that happens when building TYPE_SIZE. */
6205 if (size_varies)
6206 size = save_expr (size);
6207 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6208 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6209 integer_zero_node, size);
6211 /* Compute the maximum valid index, that is, size
6212 - 1. Do the calculation in index_type, so that
6213 if it is a variable the computations will be
6214 done in the proper mode. */
6215 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6216 convert (index_type, size),
6217 convert (index_type,
6218 size_one_node));
6220 /* The above overflows when size does not fit
6221 in index_type.
6222 ??? While a size of INT_MAX+1 technically shouldn't
6223 cause an overflow (because we subtract 1), handling
6224 this case seems like an unnecessary complication. */
6225 if (TREE_CODE (size) == INTEGER_CST
6226 && !int_fits_type_p (size, index_type))
6228 if (name)
6229 error_at (loc, "size of array %qE is too large",
6230 name);
6231 else
6232 error_at (loc, "size of unnamed array is too large");
6233 type = error_mark_node;
6234 continue;
6237 itype = build_index_type (itype);
6239 if (this_size_varies)
6241 if (*expr)
6242 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6243 *expr, size);
6244 else
6245 *expr = size;
6246 *expr_const_operands &= size_maybe_const;
6249 else if (decl_context == FIELD)
6251 bool flexible_array_member = false;
6252 if (array_parm_vla_unspec_p)
6253 /* Field names can in fact have function prototype
6254 scope so [*] is disallowed here through making
6255 the field variably modified, not through being
6256 something other than a declaration with function
6257 prototype scope. */
6258 size_varies = true;
6259 else
6261 const struct c_declarator *t = declarator;
6262 while (t->kind == cdk_attrs)
6263 t = t->declarator;
6264 flexible_array_member = (t->kind == cdk_id);
6266 if (flexible_array_member
6267 && !in_system_header_at (input_location))
6268 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6269 "support flexible array members");
6271 /* ISO C99 Flexible array members are effectively
6272 identical to GCC's zero-length array extension. */
6273 if (flexible_array_member || array_parm_vla_unspec_p)
6274 itype = build_range_type (sizetype, size_zero_node,
6275 NULL_TREE);
6277 else if (decl_context == PARM)
6279 if (array_parm_vla_unspec_p)
6281 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6282 size_varies = true;
6285 else if (decl_context == TYPENAME)
6287 if (array_parm_vla_unspec_p)
6289 /* C99 6.7.5.2p4 */
6290 warning (0, "%<[*]%> not in a declaration");
6291 /* We use this to avoid messing up with incomplete
6292 array types of the same type, that would
6293 otherwise be modified below. */
6294 itype = build_range_type (sizetype, size_zero_node,
6295 NULL_TREE);
6296 size_varies = true;
6300 /* Complain about arrays of incomplete types. */
6301 if (!COMPLETE_TYPE_P (type))
6303 error_at (loc, "array type has incomplete element type %qT",
6304 type);
6305 /* See if we can be more helpful. */
6306 if (TREE_CODE (type) == ARRAY_TYPE)
6308 if (name)
6309 inform (loc, "declaration of %qE as multidimensional "
6310 "array must have bounds for all dimensions "
6311 "except the first", name);
6312 else
6313 inform (loc, "declaration of multidimensional array "
6314 "must have bounds for all dimensions except "
6315 "the first");
6317 type = error_mark_node;
6319 else
6320 /* When itype is NULL, a shared incomplete array type is
6321 returned for all array of a given type. Elsewhere we
6322 make sure we don't complete that type before copying
6323 it, but here we want to make sure we don't ever
6324 modify the shared type, so we gcc_assert (itype)
6325 below. */
6327 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6328 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6329 type = build_qualified_type (type,
6330 ENCODE_QUAL_ADDR_SPACE (as));
6332 type = build_array_type (type, itype);
6335 if (type != error_mark_node)
6337 if (size_varies)
6339 /* It is ok to modify type here even if itype is
6340 NULL: if size_varies, we're in a
6341 multi-dimensional array and the inner type has
6342 variable size, so the enclosing shared array type
6343 must too. */
6344 if (size && TREE_CODE (size) == INTEGER_CST)
6345 type
6346 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6347 C_TYPE_VARIABLE_SIZE (type) = 1;
6350 /* The GCC extension for zero-length arrays differs from
6351 ISO flexible array members in that sizeof yields
6352 zero. */
6353 if (size && integer_zerop (size))
6355 gcc_assert (itype);
6356 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6357 TYPE_SIZE (type) = bitsize_zero_node;
6358 TYPE_SIZE_UNIT (type) = size_zero_node;
6359 SET_TYPE_STRUCTURAL_EQUALITY (type);
6361 if (array_parm_vla_unspec_p)
6363 gcc_assert (itype);
6364 /* The type is complete. C99 6.7.5.2p4 */
6365 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6366 TYPE_SIZE (type) = bitsize_zero_node;
6367 TYPE_SIZE_UNIT (type) = size_zero_node;
6368 SET_TYPE_STRUCTURAL_EQUALITY (type);
6371 if (!valid_array_size_p (loc, type, name))
6372 type = error_mark_node;
6375 if (decl_context != PARM
6376 && (array_ptr_quals != TYPE_UNQUALIFIED
6377 || array_ptr_attrs != NULL_TREE
6378 || array_parm_static))
6380 error_at (loc, "static or type qualifiers in non-parameter "
6381 "array declarator");
6382 array_ptr_quals = TYPE_UNQUALIFIED;
6383 array_ptr_attrs = NULL_TREE;
6384 array_parm_static = false;
6386 orig_qual_indirect++;
6387 break;
6389 case cdk_function:
6391 /* Say it's a definition only for the declarator closest
6392 to the identifier, apart possibly from some
6393 attributes. */
6394 bool really_funcdef = false;
6395 tree arg_types;
6396 orig_qual_type = NULL_TREE;
6397 if (funcdef_flag)
6399 const struct c_declarator *t = declarator->declarator;
6400 while (t->kind == cdk_attrs)
6401 t = t->declarator;
6402 really_funcdef = (t->kind == cdk_id);
6405 /* Declaring a function type. Make sure we have a valid
6406 type for the function to return. */
6407 if (type == error_mark_node)
6408 continue;
6410 size_varies = false;
6412 /* Warn about some types functions can't return. */
6413 if (TREE_CODE (type) == FUNCTION_TYPE)
6415 if (name)
6416 error_at (loc, "%qE declared as function returning a "
6417 "function", name);
6418 else
6419 error_at (loc, "type name declared as function "
6420 "returning a function");
6421 type = integer_type_node;
6423 if (TREE_CODE (type) == ARRAY_TYPE)
6425 if (name)
6426 error_at (loc, "%qE declared as function returning an array",
6427 name);
6428 else
6429 error_at (loc, "type name declared as function returning "
6430 "an array");
6431 type = integer_type_node;
6434 /* Construct the function type and go to the next
6435 inner layer of declarator. */
6436 arg_info = declarator->u.arg_info;
6437 arg_types = grokparms (arg_info, really_funcdef);
6439 /* Type qualifiers before the return type of the function
6440 qualify the return type, not the function type. */
6441 if (type_quals)
6443 const enum c_declspec_word ignored_quals_list[] =
6445 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6446 cdw_atomic, cdw_number_of_elements
6448 location_t specs_loc
6449 = smallest_type_quals_location (declspecs->locations,
6450 ignored_quals_list);
6451 if (specs_loc == UNKNOWN_LOCATION)
6452 specs_loc = declspecs->locations[cdw_typedef];
6453 if (specs_loc == UNKNOWN_LOCATION)
6454 specs_loc = loc;
6456 /* Type qualifiers on a function return type are
6457 normally permitted by the standard but have no
6458 effect, so give a warning at -Wreturn-type.
6459 Qualifiers on a void return type are banned on
6460 function definitions in ISO C; GCC used to used
6461 them for noreturn functions. The resolution of C11
6462 DR#423 means qualifiers (other than _Atomic) are
6463 actually removed from the return type when
6464 determining the function type. */
6465 int quals_used = type_quals;
6466 if (flag_isoc11)
6467 quals_used &= TYPE_QUAL_ATOMIC;
6468 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6469 pedwarn (specs_loc, 0,
6470 "function definition has qualified void return type");
6471 else
6472 warning_at (specs_loc, OPT_Wignored_qualifiers,
6473 "type qualifiers ignored on function return type");
6475 /* Ensure an error for restrict on invalid types; the
6476 DR#423 resolution is not entirely clear about
6477 this. */
6478 if (flag_isoc11
6479 && (type_quals & TYPE_QUAL_RESTRICT)
6480 && (!POINTER_TYPE_P (type)
6481 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6482 error_at (loc, "invalid use of %<restrict%>");
6483 if (quals_used)
6484 type = c_build_qualified_type (type, quals_used);
6486 type_quals = TYPE_UNQUALIFIED;
6488 type = build_function_type (type, arg_types);
6489 declarator = declarator->declarator;
6491 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6492 the formal parameter list of this FUNCTION_TYPE to point to
6493 the FUNCTION_TYPE node itself. */
6495 c_arg_tag *tag;
6496 unsigned ix;
6498 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6499 TYPE_CONTEXT (tag->type) = type;
6501 break;
6503 case cdk_pointer:
6505 /* Merge any constancy or volatility into the target type
6506 for the pointer. */
6507 if ((type_quals & TYPE_QUAL_ATOMIC)
6508 && TREE_CODE (type) == FUNCTION_TYPE)
6510 error_at (loc,
6511 "%<_Atomic%>-qualified function type");
6512 type_quals &= ~TYPE_QUAL_ATOMIC;
6514 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6515 && type_quals)
6516 pedwarn (loc, OPT_Wpedantic,
6517 "ISO C forbids qualified function types");
6518 if (type_quals)
6519 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6520 orig_qual_indirect);
6521 orig_qual_type = NULL_TREE;
6522 size_varies = false;
6524 /* When the pointed-to type involves components of variable size,
6525 care must be taken to ensure that the size evaluation code is
6526 emitted early enough to dominate all the possible later uses
6527 and late enough for the variables on which it depends to have
6528 been assigned.
6530 This is expected to happen automatically when the pointed-to
6531 type has a name/declaration of it's own, but special attention
6532 is required if the type is anonymous.
6534 We attach an artificial TYPE_DECL to such pointed-to type
6535 and arrange for it to be included in a DECL_EXPR. This
6536 forces the sizes evaluation at a safe point and ensures it
6537 is not deferred until e.g. within a deeper conditional context.
6539 PARM contexts have no enclosing statement list that
6540 can hold the DECL_EXPR, so we need to use a BIND_EXPR
6541 instead, and add it to the list of expressions that
6542 need to be evaluated.
6544 TYPENAME contexts do have an enclosing statement list,
6545 but it would be incorrect to use it, as the size should
6546 only be evaluated if the containing expression is
6547 evaluated. We might also be in the middle of an
6548 expression with side effects on the pointed-to type size
6549 "arguments" prior to the pointer declaration point and
6550 the fake TYPE_DECL in the enclosing context would force
6551 the size evaluation prior to the side effects. We therefore
6552 use BIND_EXPRs in TYPENAME contexts too. */
6553 if (!TYPE_NAME (type)
6554 && variably_modified_type_p (type, NULL_TREE))
6556 tree bind = NULL_TREE;
6557 if (decl_context == TYPENAME || decl_context == PARM)
6559 bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
6560 NULL_TREE, NULL_TREE);
6561 TREE_SIDE_EFFECTS (bind) = 1;
6562 BIND_EXPR_BODY (bind) = push_stmt_list ();
6563 push_scope ();
6565 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6566 DECL_ARTIFICIAL (decl) = 1;
6567 pushdecl (decl);
6568 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6569 TYPE_NAME (type) = decl;
6570 if (bind)
6572 pop_scope ();
6573 BIND_EXPR_BODY (bind)
6574 = pop_stmt_list (BIND_EXPR_BODY (bind));
6575 if (*expr)
6576 *expr = build2 (COMPOUND_EXPR, void_type_node, *expr,
6577 bind);
6578 else
6579 *expr = bind;
6583 type = c_build_pointer_type (type);
6585 /* Process type qualifiers (such as const or volatile)
6586 that were given inside the `*'. */
6587 type_quals = declarator->u.pointer_quals;
6589 declarator = declarator->declarator;
6590 break;
6592 default:
6593 gcc_unreachable ();
6596 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6598 /* Now TYPE has the actual type, apart from any qualifiers in
6599 TYPE_QUALS. */
6601 /* Warn about address space used for things other than static memory or
6602 pointers. */
6603 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6604 if (!ADDR_SPACE_GENERIC_P (address_space))
6606 if (decl_context == NORMAL)
6608 switch (storage_class)
6610 case csc_auto:
6611 error ("%qs combined with %<auto%> qualifier for %qE",
6612 c_addr_space_name (address_space), name);
6613 break;
6614 case csc_register:
6615 error ("%qs combined with %<register%> qualifier for %qE",
6616 c_addr_space_name (address_space), name);
6617 break;
6618 case csc_none:
6619 if (current_function_scope)
6621 error ("%qs specified for auto variable %qE",
6622 c_addr_space_name (address_space), name);
6623 break;
6625 break;
6626 case csc_static:
6627 case csc_extern:
6628 case csc_typedef:
6629 break;
6630 default:
6631 gcc_unreachable ();
6634 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6636 if (name)
6637 error ("%qs specified for parameter %qE",
6638 c_addr_space_name (address_space), name);
6639 else
6640 error ("%qs specified for unnamed parameter",
6641 c_addr_space_name (address_space));
6643 else if (decl_context == FIELD)
6645 if (name)
6646 error ("%qs specified for structure field %qE",
6647 c_addr_space_name (address_space), name);
6648 else
6649 error ("%qs specified for structure field",
6650 c_addr_space_name (address_space));
6654 /* Check the type and width of a bit-field. */
6655 if (bitfield)
6657 check_bitfield_type_and_width (loc, &type, width, name);
6658 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6659 atomic types are permitted for bit-fields; we have no code to
6660 make bit-field accesses atomic, so disallow them. */
6661 if (type_quals & TYPE_QUAL_ATOMIC)
6663 if (name)
6664 error_at (loc, "bit-field %qE has atomic type", name);
6665 else
6666 error_at (loc, "bit-field has atomic type");
6667 type_quals &= ~TYPE_QUAL_ATOMIC;
6671 /* Reject invalid uses of _Alignas. */
6672 if (declspecs->alignas_p)
6674 if (storage_class == csc_typedef)
6675 error_at (loc, "alignment specified for typedef %qE", name);
6676 else if (storage_class == csc_register)
6677 error_at (loc, "alignment specified for %<register%> object %qE",
6678 name);
6679 else if (decl_context == PARM)
6681 if (name)
6682 error_at (loc, "alignment specified for parameter %qE", name);
6683 else
6684 error_at (loc, "alignment specified for unnamed parameter");
6686 else if (bitfield)
6688 if (name)
6689 error_at (loc, "alignment specified for bit-field %qE", name);
6690 else
6691 error_at (loc, "alignment specified for unnamed bit-field");
6693 else if (TREE_CODE (type) == FUNCTION_TYPE)
6694 error_at (loc, "alignment specified for function %qE", name);
6695 else if (declspecs->align_log != -1 && TYPE_P (type))
6697 alignas_align = 1U << declspecs->align_log;
6698 if (alignas_align < min_align_of_type (type))
6700 if (name)
6701 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6702 "alignment of %qE", name);
6703 else
6704 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6705 "alignment of unnamed field");
6706 alignas_align = 0;
6711 /* If this is declaring a typedef name, return a TYPE_DECL. */
6713 if (storage_class == csc_typedef)
6715 tree decl;
6716 if ((type_quals & TYPE_QUAL_ATOMIC)
6717 && TREE_CODE (type) == FUNCTION_TYPE)
6719 error_at (loc,
6720 "%<_Atomic%>-qualified function type");
6721 type_quals &= ~TYPE_QUAL_ATOMIC;
6723 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6724 && type_quals)
6725 pedwarn (loc, OPT_Wpedantic,
6726 "ISO C forbids qualified function types");
6727 if (type_quals)
6728 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6729 orig_qual_indirect);
6730 decl = build_decl (declarator->id_loc,
6731 TYPE_DECL, declarator->u.id, type);
6732 if (declspecs->explicit_signed_p)
6733 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6734 if (declspecs->inline_p)
6735 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6736 if (declspecs->noreturn_p)
6737 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6739 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6741 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6743 if (b != NULL
6744 && b->decl != NULL_TREE
6745 && (B_IN_CURRENT_SCOPE (b)
6746 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6747 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6749 auto_diagnostic_group d;
6750 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6751 ("using %qD as both a typedef and a tag is "
6752 "invalid in C++"), decl)
6753 && b->locus != UNKNOWN_LOCATION)
6754 inform (b->locus, "originally defined here");
6758 return decl;
6761 /* If this is a type name (such as, in a cast or sizeof),
6762 compute the type and return it now. */
6764 if (decl_context == TYPENAME)
6766 /* Note that the grammar rejects storage classes in typenames
6767 and fields. */
6768 gcc_assert (storage_class == csc_none && !threadp
6769 && !declspecs->inline_p && !declspecs->noreturn_p);
6770 if ((type_quals & TYPE_QUAL_ATOMIC)
6771 && TREE_CODE (type) == FUNCTION_TYPE)
6773 error_at (loc,
6774 "%<_Atomic%>-qualified function type");
6775 type_quals &= ~TYPE_QUAL_ATOMIC;
6777 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6778 && type_quals)
6779 pedwarn (loc, OPT_Wpedantic,
6780 "ISO C forbids const or volatile function types");
6781 if (type_quals)
6782 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6783 orig_qual_indirect);
6784 return type;
6787 if (pedantic && decl_context == FIELD
6788 && variably_modified_type_p (type, NULL_TREE))
6790 /* C99 6.7.2.1p8 */
6791 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6792 "have a variably modified type");
6795 /* Aside from typedefs and type names (handle above),
6796 `void' at top level (not within pointer)
6797 is allowed only in public variables.
6798 We don't complain about parms either, but that is because
6799 a better error message can be made later. */
6801 if (VOID_TYPE_P (type) && decl_context != PARM
6802 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6803 && (storage_class == csc_extern
6804 || (current_scope == file_scope
6805 && !(storage_class == csc_static
6806 || storage_class == csc_register)))))
6808 error_at (loc, "variable or field %qE declared void", name);
6809 type = integer_type_node;
6812 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6813 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6816 tree decl;
6818 if (decl_context == PARM)
6820 tree promoted_type;
6821 bool array_parameter_p = false;
6823 /* A parameter declared as an array of T is really a pointer to T.
6824 One declared as a function is really a pointer to a function. */
6826 if (TREE_CODE (type) == ARRAY_TYPE)
6828 /* Transfer const-ness of array into that of type pointed to. */
6829 type = TREE_TYPE (type);
6830 if (orig_qual_type != NULL_TREE)
6832 if (orig_qual_indirect == 0)
6833 orig_qual_type = TREE_TYPE (orig_qual_type);
6834 else
6835 orig_qual_indirect--;
6837 if (type_quals)
6838 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6839 orig_qual_indirect);
6840 type = c_build_pointer_type (type);
6841 type_quals = array_ptr_quals;
6842 if (type_quals)
6843 type = c_build_qualified_type (type, type_quals);
6845 /* We don't yet implement attributes in this context. */
6846 if (array_ptr_attrs != NULL_TREE)
6847 warning_at (loc, OPT_Wattributes,
6848 "attributes in parameter array declarator ignored");
6850 size_varies = false;
6851 array_parameter_p = true;
6853 else if (TREE_CODE (type) == FUNCTION_TYPE)
6855 if (type_quals & TYPE_QUAL_ATOMIC)
6857 error_at (loc,
6858 "%<_Atomic%>-qualified function type");
6859 type_quals &= ~TYPE_QUAL_ATOMIC;
6861 else if (type_quals)
6862 pedwarn (loc, OPT_Wpedantic,
6863 "ISO C forbids qualified function types");
6864 if (type_quals)
6865 type = c_build_qualified_type (type, type_quals);
6866 type = c_build_pointer_type (type);
6867 type_quals = TYPE_UNQUALIFIED;
6869 else if (type_quals)
6870 type = c_build_qualified_type (type, type_quals);
6872 decl = build_decl (declarator->id_loc,
6873 PARM_DECL, declarator->u.id, type);
6874 if (size_varies)
6875 C_DECL_VARIABLE_SIZE (decl) = 1;
6876 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6878 /* Compute the type actually passed in the parmlist,
6879 for the case where there is no prototype.
6880 (For example, shorts and chars are passed as ints.)
6881 When there is a prototype, this is overridden later. */
6883 if (type == error_mark_node)
6884 promoted_type = type;
6885 else
6886 promoted_type = c_type_promotes_to (type);
6888 DECL_ARG_TYPE (decl) = promoted_type;
6889 if (declspecs->inline_p)
6890 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6891 if (declspecs->noreturn_p)
6892 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6894 else if (decl_context == FIELD)
6896 /* Note that the grammar rejects storage classes in typenames
6897 and fields. */
6898 gcc_assert (storage_class == csc_none && !threadp
6899 && !declspecs->inline_p && !declspecs->noreturn_p);
6901 /* Structure field. It may not be a function. */
6903 if (TREE_CODE (type) == FUNCTION_TYPE)
6905 error_at (loc, "field %qE declared as a function", name);
6906 type = build_pointer_type (type);
6908 else if (TREE_CODE (type) != ERROR_MARK
6909 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6911 if (name)
6912 error_at (loc, "field %qE has incomplete type", name);
6913 else
6914 error_at (loc, "unnamed field has incomplete type");
6915 type = error_mark_node;
6917 else if (TREE_CODE (type) == ARRAY_TYPE
6918 && TYPE_DOMAIN (type) == NULL_TREE)
6920 /* We have a flexible array member through a typedef.
6921 Set suitable range. Whether this is a correct position
6922 for a flexible array member will be determined elsewhere. */
6923 if (!in_system_header_at (input_location))
6924 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6925 "support flexible array members");
6926 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6927 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6928 NULL_TREE);
6929 if (orig_qual_indirect == 0)
6930 orig_qual_type = NULL_TREE;
6932 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6933 orig_qual_indirect);
6934 decl = build_decl (declarator->id_loc,
6935 FIELD_DECL, declarator->u.id, type);
6936 DECL_NONADDRESSABLE_P (decl) = bitfield;
6937 if (bitfield && !declarator->u.id)
6939 TREE_NO_WARNING (decl) = 1;
6940 DECL_PADDING_P (decl) = 1;
6943 if (size_varies)
6944 C_DECL_VARIABLE_SIZE (decl) = 1;
6946 else if (TREE_CODE (type) == FUNCTION_TYPE)
6948 if (storage_class == csc_register || threadp)
6950 error_at (loc, "invalid storage class for function %qE", name);
6952 else if (current_scope != file_scope)
6954 /* Function declaration not at file scope. Storage
6955 classes other than `extern' are not allowed, C99
6956 6.7.1p5, and `extern' makes no difference. However,
6957 GCC allows 'auto', perhaps with 'inline', to support
6958 nested functions. */
6959 if (storage_class == csc_auto)
6960 pedwarn (loc, OPT_Wpedantic,
6961 "invalid storage class for function %qE", name);
6962 else if (storage_class == csc_static)
6964 error_at (loc, "invalid storage class for function %qE", name);
6965 if (funcdef_flag)
6966 storage_class = declspecs->storage_class = csc_none;
6967 else
6968 return NULL_TREE;
6972 decl = build_decl (declarator->id_loc,
6973 FUNCTION_DECL, declarator->u.id, type);
6974 decl = build_decl_attribute_variant (decl, decl_attr);
6976 if (type_quals & TYPE_QUAL_ATOMIC)
6978 error_at (loc,
6979 "%<_Atomic%>-qualified function type");
6980 type_quals &= ~TYPE_QUAL_ATOMIC;
6982 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6983 pedwarn (loc, OPT_Wpedantic,
6984 "ISO C forbids qualified function types");
6986 /* Every function declaration is an external reference
6987 (DECL_EXTERNAL) except for those which are not at file
6988 scope and are explicitly declared "auto". This is
6989 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6990 GCC to signify a forward declaration of a nested function. */
6991 if (storage_class == csc_auto && current_scope != file_scope)
6992 DECL_EXTERNAL (decl) = 0;
6993 /* In C99, a function which is declared 'inline' with 'extern'
6994 is not an external reference (which is confusing). It
6995 means that the later definition of the function must be output
6996 in this file, C99 6.7.4p6. In GNU C89, a function declared
6997 'extern inline' is an external reference. */
6998 else if (declspecs->inline_p && storage_class != csc_static)
6999 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
7000 == flag_gnu89_inline);
7001 else
7002 DECL_EXTERNAL (decl) = !initialized;
7004 /* Record absence of global scope for `static' or `auto'. */
7005 TREE_PUBLIC (decl)
7006 = !(storage_class == csc_static || storage_class == csc_auto);
7008 /* For a function definition, record the argument information
7009 block where store_parm_decls will look for it. */
7010 if (funcdef_flag)
7011 current_function_arg_info = arg_info;
7013 if (declspecs->default_int_p)
7014 C_FUNCTION_IMPLICIT_INT (decl) = 1;
7016 /* Record presence of `inline' and `_Noreturn', if it is
7017 reasonable. */
7018 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
7020 if (declspecs->inline_p)
7021 pedwarn (loc, 0, "cannot inline function %<main%>");
7022 if (declspecs->noreturn_p)
7023 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
7025 else
7027 if (declspecs->inline_p)
7028 /* Record that the function is declared `inline'. */
7029 DECL_DECLARED_INLINE_P (decl) = 1;
7030 if (declspecs->noreturn_p)
7032 if (flag_isoc99)
7033 pedwarn_c99 (loc, OPT_Wpedantic,
7034 "ISO C99 does not support %<_Noreturn%>");
7035 else
7036 pedwarn_c99 (loc, OPT_Wpedantic,
7037 "ISO C90 does not support %<_Noreturn%>");
7038 TREE_THIS_VOLATILE (decl) = 1;
7042 else
7044 /* It's a variable. */
7045 /* An uninitialized decl with `extern' is a reference. */
7046 int extern_ref = !initialized && storage_class == csc_extern;
7048 type = c_build_qualified_type (type, type_quals, orig_qual_type,
7049 orig_qual_indirect);
7051 /* C99 6.2.2p7: It is invalid (compile-time undefined
7052 behavior) to create an 'extern' declaration for a
7053 variable if there is a global declaration that is
7054 'static' and the global declaration is not visible.
7055 (If the static declaration _is_ currently visible,
7056 the 'extern' declaration is taken to refer to that decl.) */
7057 if (extern_ref && current_scope != file_scope)
7059 tree global_decl = identifier_global_value (declarator->u.id);
7060 tree visible_decl = lookup_name (declarator->u.id);
7062 if (global_decl
7063 && global_decl != visible_decl
7064 && VAR_P (global_decl)
7065 && !TREE_PUBLIC (global_decl))
7066 error_at (loc, "variable previously declared %<static%> "
7067 "redeclared %<extern%>");
7070 decl = build_decl (declarator->id_loc,
7071 VAR_DECL, declarator->u.id, type);
7072 if (size_varies)
7073 C_DECL_VARIABLE_SIZE (decl) = 1;
7075 if (declspecs->inline_p)
7076 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
7077 if (declspecs->noreturn_p)
7078 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7080 /* At file scope, an initialized extern declaration may follow
7081 a static declaration. In that case, DECL_EXTERNAL will be
7082 reset later in start_decl. */
7083 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7085 /* At file scope, the presence of a `static' or `register' storage
7086 class specifier, or the absence of all storage class specifiers
7087 makes this declaration a definition (perhaps tentative). Also,
7088 the absence of `static' makes it public. */
7089 if (current_scope == file_scope)
7091 TREE_PUBLIC (decl) = storage_class != csc_static;
7092 TREE_STATIC (decl) = !extern_ref;
7094 /* Not at file scope, only `static' makes a static definition. */
7095 else
7097 TREE_STATIC (decl) = (storage_class == csc_static);
7098 TREE_PUBLIC (decl) = extern_ref;
7101 if (threadp)
7102 set_decl_tls_model (decl, decl_default_tls_model (decl));
7105 if ((storage_class == csc_extern
7106 || (storage_class == csc_none
7107 && TREE_CODE (type) == FUNCTION_TYPE
7108 && !funcdef_flag))
7109 && variably_modified_type_p (type, NULL_TREE))
7111 /* C99 6.7.5.2p2 */
7112 if (TREE_CODE (type) == FUNCTION_TYPE)
7113 error_at (loc, "non-nested function with variably modified type");
7114 else
7115 error_at (loc, "object with variably modified type must have "
7116 "no linkage");
7119 /* Record `register' declaration for warnings on &
7120 and in case doing stupid register allocation. */
7122 if (storage_class == csc_register)
7124 C_DECL_REGISTER (decl) = 1;
7125 DECL_REGISTER (decl) = 1;
7128 /* Record constancy and volatility. */
7129 c_apply_type_quals_to_decl (type_quals, decl);
7131 /* Apply _Alignas specifiers. */
7132 if (alignas_align)
7134 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7135 DECL_USER_ALIGN (decl) = 1;
7138 /* If a type has volatile components, it should be stored in memory.
7139 Otherwise, the fact that those components are volatile
7140 will be ignored, and would even crash the compiler.
7141 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7142 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7143 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7144 || TREE_CODE (decl) == RESULT_DECL))
7146 /* It is not an error for a structure with volatile fields to
7147 be declared register, but reset DECL_REGISTER since it
7148 cannot actually go in a register. */
7149 int was_reg = C_DECL_REGISTER (decl);
7150 C_DECL_REGISTER (decl) = 0;
7151 DECL_REGISTER (decl) = 0;
7152 c_mark_addressable (decl);
7153 C_DECL_REGISTER (decl) = was_reg;
7156 /* This is the earliest point at which we might know the assembler
7157 name of a variable. Thus, if it's known before this, die horribly. */
7158 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7159 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7161 if (warn_cxx_compat
7162 && VAR_P (decl)
7163 && TREE_PUBLIC (decl)
7164 && TREE_STATIC (decl)
7165 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7166 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7167 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7168 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7169 ("non-local variable %qD with anonymous type is "
7170 "questionable in C++"),
7171 decl);
7173 return decl;
7177 /* Decode the parameter-list info for a function type or function definition.
7178 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7179 if there is an identifier list instead of a parameter decl list).
7180 These two functions are separate because when a function returns
7181 or receives functions then each is called multiple times but the order
7182 of calls is different. The last call to `grokparms' is always the one
7183 that contains the formal parameter names of a function definition.
7185 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7187 FUNCDEF_FLAG is true for a function definition, false for
7188 a mere declaration. A nonempty identifier-list gets an error message
7189 when FUNCDEF_FLAG is false. */
7191 static tree
7192 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7194 tree arg_types = arg_info->types;
7196 if (funcdef_flag && arg_info->had_vla_unspec)
7198 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7199 /* C99 6.7.5.2p4 */
7200 error ("%<[*]%> not allowed in other than function prototype scope");
7203 if (arg_types == NULL_TREE && !funcdef_flag
7204 && !in_system_header_at (input_location))
7205 warning (OPT_Wstrict_prototypes,
7206 "function declaration isn%'t a prototype");
7208 if (arg_types == error_mark_node)
7209 /* Don't set TYPE_ARG_TYPES in this case. */
7210 return NULL_TREE;
7212 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7214 if (!funcdef_flag)
7216 pedwarn (input_location, 0, "parameter names (without types) in "
7217 "function declaration");
7218 arg_info->parms = NULL_TREE;
7220 else
7221 arg_info->parms = arg_info->types;
7223 arg_info->types = NULL_TREE;
7224 return NULL_TREE;
7226 else
7228 tree parm, type, typelt;
7229 unsigned int parmno;
7231 /* If there is a parameter of incomplete type in a definition,
7232 this is an error. In a declaration this is valid, and a
7233 struct or union type may be completed later, before any calls
7234 or definition of the function. In the case where the tag was
7235 first declared within the parameter list, a warning has
7236 already been given. If a parameter has void type, then
7237 however the function cannot be defined or called, so
7238 warn. */
7240 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7241 parm;
7242 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7244 type = TREE_VALUE (typelt);
7245 if (type == error_mark_node)
7246 continue;
7248 if (!COMPLETE_TYPE_P (type))
7250 if (funcdef_flag)
7252 if (DECL_NAME (parm))
7253 error_at (input_location,
7254 "parameter %u (%q+D) has incomplete type",
7255 parmno, parm);
7256 else
7257 error_at (DECL_SOURCE_LOCATION (parm),
7258 "parameter %u has incomplete type",
7259 parmno);
7261 TREE_VALUE (typelt) = error_mark_node;
7262 TREE_TYPE (parm) = error_mark_node;
7263 arg_types = NULL_TREE;
7265 else if (VOID_TYPE_P (type))
7267 if (DECL_NAME (parm))
7268 warning_at (input_location, 0,
7269 "parameter %u (%q+D) has void type",
7270 parmno, parm);
7271 else
7272 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7273 "parameter %u has void type",
7274 parmno);
7278 if (DECL_NAME (parm) && TREE_USED (parm))
7279 warn_if_shadowing (parm);
7281 return arg_types;
7285 /* Allocate and initialize a c_arg_info structure from the parser's
7286 obstack. */
7288 struct c_arg_info *
7289 build_arg_info (void)
7291 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7292 ret->parms = NULL_TREE;
7293 ret->tags = NULL;
7294 ret->types = NULL_TREE;
7295 ret->others = NULL_TREE;
7296 ret->pending_sizes = NULL;
7297 ret->had_vla_unspec = 0;
7298 return ret;
7301 /* Take apart the current scope and return a c_arg_info structure with
7302 info on a parameter list just parsed.
7304 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7306 ELLIPSIS being true means the argument list ended in '...' so don't
7307 append a sentinel (void_list_node) to the end of the type-list.
7309 EXPR is NULL or an expression that needs to be evaluated for the
7310 side effects of array size expressions in the parameters. */
7312 struct c_arg_info *
7313 get_parm_info (bool ellipsis, tree expr)
7315 struct c_binding *b = current_scope->bindings;
7316 struct c_arg_info *arg_info = build_arg_info ();
7318 tree parms = NULL_TREE;
7319 vec<c_arg_tag, va_gc> *tags = NULL;
7320 tree types = NULL_TREE;
7321 tree others = NULL_TREE;
7323 bool gave_void_only_once_err = false;
7325 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7327 /* The bindings in this scope must not get put into a block.
7328 We will take care of deleting the binding nodes. */
7329 current_scope->bindings = 0;
7331 /* This function is only called if there was *something* on the
7332 parameter list. */
7333 gcc_assert (b);
7335 /* A parameter list consisting solely of 'void' indicates that the
7336 function takes no arguments. But if the 'void' is qualified
7337 (by 'const' or 'volatile'), or has a storage class specifier
7338 ('register'), then the behavior is undefined; issue an error.
7339 Typedefs for 'void' are OK (see DR#157). */
7340 if (b->prev == 0 /* one binding */
7341 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7342 && !DECL_NAME (b->decl) /* anonymous */
7343 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7345 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7346 || C_DECL_REGISTER (b->decl))
7347 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7349 /* There cannot be an ellipsis. */
7350 if (ellipsis)
7351 error_at (b->locus, "%<void%> must be the only parameter");
7353 arg_info->types = void_list_node;
7354 return arg_info;
7357 if (!ellipsis)
7358 types = void_list_node;
7360 /* Break up the bindings list into parms, tags, types, and others;
7361 apply sanity checks; purge the name-to-decl bindings. */
7362 while (b)
7364 tree decl = b->decl;
7365 tree type = TREE_TYPE (decl);
7366 c_arg_tag tag;
7367 const char *keyword;
7369 switch (TREE_CODE (decl))
7371 case PARM_DECL:
7372 if (b->id)
7374 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7375 I_SYMBOL_BINDING (b->id) = b->shadowed;
7378 /* Check for forward decls that never got their actual decl. */
7379 if (TREE_ASM_WRITTEN (decl))
7380 error_at (b->locus,
7381 "parameter %q+D has just a forward declaration", decl);
7382 /* Check for (..., void, ...) and issue an error. */
7383 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7385 if (!gave_void_only_once_err)
7387 error_at (b->locus, "%<void%> must be the only parameter");
7388 gave_void_only_once_err = true;
7391 else
7393 /* Valid parameter, add it to the list. */
7394 DECL_CHAIN (decl) = parms;
7395 parms = decl;
7397 /* Since there is a prototype, args are passed in their
7398 declared types. The back end may override this later. */
7399 DECL_ARG_TYPE (decl) = type;
7400 types = tree_cons (0, type, types);
7402 break;
7404 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7405 case UNION_TYPE: keyword = "union"; goto tag;
7406 case RECORD_TYPE: keyword = "struct"; goto tag;
7407 tag:
7408 /* Types may not have tag-names, in which case the type
7409 appears in the bindings list with b->id NULL. */
7410 if (b->id)
7412 gcc_assert (I_TAG_BINDING (b->id) == b);
7413 I_TAG_BINDING (b->id) = b->shadowed;
7416 /* Warn about any struct, union or enum tags defined in a
7417 parameter list. The scope of such types is limited to
7418 the parameter list, which is rarely if ever desirable
7419 (it's impossible to call such a function with type-
7420 correct arguments). An anonymous union parm type is
7421 meaningful as a GNU extension, so don't warn for that. */
7422 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7424 if (b->id)
7425 /* The %s will be one of 'struct', 'union', or 'enum'. */
7426 warning_at (b->locus, 0,
7427 "%<%s %E%> declared inside parameter list"
7428 " will not be visible outside of this definition or"
7429 " declaration", keyword, b->id);
7430 else
7431 /* The %s will be one of 'struct', 'union', or 'enum'. */
7432 warning_at (b->locus, 0,
7433 "anonymous %s declared inside parameter list"
7434 " will not be visible outside of this definition or"
7435 " declaration", keyword);
7438 tag.id = b->id;
7439 tag.type = decl;
7440 vec_safe_push (tags, tag);
7441 break;
7443 case FUNCTION_DECL:
7444 /* FUNCTION_DECLs appear when there is an implicit function
7445 declaration in the parameter list. */
7446 gcc_assert (b->nested || seen_error ());
7447 goto set_shadowed;
7449 case CONST_DECL:
7450 case TYPE_DECL:
7451 /* CONST_DECLs appear here when we have an embedded enum,
7452 and TYPE_DECLs appear here when we have an embedded struct
7453 or union. No warnings for this - we already warned about the
7454 type itself. */
7456 /* When we reinsert this decl in the function body, we need
7457 to reconstruct whether it was marked as nested. */
7458 gcc_assert (!b->nested);
7459 DECL_CHAIN (decl) = others;
7460 others = decl;
7461 /* fall through */
7463 case ERROR_MARK:
7464 set_shadowed:
7465 /* error_mark_node appears here when we have an undeclared
7466 variable. Just throw it away. */
7467 if (b->id)
7469 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7470 I_SYMBOL_BINDING (b->id) = b->shadowed;
7472 break;
7474 /* Other things that might be encountered. */
7475 case LABEL_DECL:
7476 case VAR_DECL:
7477 default:
7478 gcc_unreachable ();
7481 b = free_binding_and_advance (b);
7484 arg_info->parms = parms;
7485 arg_info->tags = tags;
7486 arg_info->types = types;
7487 arg_info->others = others;
7488 arg_info->pending_sizes = expr;
7489 return arg_info;
7492 /* Get the struct, enum or union (CODE says which) with tag NAME.
7493 Define the tag as a forward-reference with location LOC if it is
7494 not defined. Return a c_typespec structure for the type
7495 specifier. */
7497 struct c_typespec
7498 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7500 struct c_typespec ret;
7501 tree ref;
7502 location_t refloc;
7504 ret.expr = NULL_TREE;
7505 ret.expr_const_operands = true;
7507 /* If a cross reference is requested, look up the type
7508 already defined for this tag and return it. */
7510 ref = lookup_tag (code, name, false, &refloc);
7511 /* If this is the right type of tag, return what we found.
7512 (This reference will be shadowed by shadow_tag later if appropriate.)
7513 If this is the wrong type of tag, do not return it. If it was the
7514 wrong type in the same scope, we will have had an error
7515 message already; if in a different scope and declaring
7516 a name, pending_xref_error will give an error message; but if in a
7517 different scope and not declaring a name, this tag should
7518 shadow the previous declaration of a different type of tag, and
7519 this would not work properly if we return the reference found.
7520 (For example, with "struct foo" in an outer scope, "union foo;"
7521 must shadow that tag with a new one of union type.) */
7522 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7523 if (ref && TREE_CODE (ref) == code)
7525 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7526 && loc != UNKNOWN_LOCATION
7527 && warn_cxx_compat)
7529 switch (code)
7531 case ENUMERAL_TYPE:
7532 warning_at (loc, OPT_Wc___compat,
7533 ("enum type defined in struct or union "
7534 "is not visible in C++"));
7535 inform (refloc, "enum type defined here");
7536 break;
7537 case RECORD_TYPE:
7538 warning_at (loc, OPT_Wc___compat,
7539 ("struct defined in struct or union "
7540 "is not visible in C++"));
7541 inform (refloc, "struct defined here");
7542 break;
7543 case UNION_TYPE:
7544 warning_at (loc, OPT_Wc___compat,
7545 ("union defined in struct or union "
7546 "is not visible in C++"));
7547 inform (refloc, "union defined here");
7548 break;
7549 default:
7550 gcc_unreachable();
7554 ret.spec = ref;
7555 return ret;
7558 /* If no such tag is yet defined, create a forward-reference node
7559 and record it as the "definition".
7560 When a real declaration of this type is found,
7561 the forward-reference will be altered into a real type. */
7563 ref = make_node (code);
7564 if (code == ENUMERAL_TYPE)
7566 /* Give the type a default layout like unsigned int
7567 to avoid crashing if it does not get defined. */
7568 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7569 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7570 TYPE_USER_ALIGN (ref) = 0;
7571 TYPE_UNSIGNED (ref) = 1;
7572 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7573 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7574 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7577 pushtag (loc, name, ref);
7579 ret.spec = ref;
7580 return ret;
7583 /* Get the struct, enum or union (CODE says which) with tag NAME.
7584 Define the tag as a forward-reference if it is not defined.
7585 Return a tree for the type. */
7587 tree
7588 xref_tag (enum tree_code code, tree name)
7590 return parser_xref_tag (input_location, code, name).spec;
7593 /* Make sure that the tag NAME is defined *in the current scope*
7594 at least as a forward reference.
7595 LOC is the location of the struct's definition.
7596 CODE says which kind of tag NAME ought to be.
7598 This stores the current value of the file static STRUCT_PARSE_INFO
7599 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7600 new c_struct_parse_info structure. The old value of
7601 STRUCT_PARSE_INFO is restored in finish_struct. */
7603 tree
7604 start_struct (location_t loc, enum tree_code code, tree name,
7605 struct c_struct_parse_info **enclosing_struct_parse_info)
7607 /* If there is already a tag defined at this scope
7608 (as a forward reference), just return it. */
7610 tree ref = NULL_TREE;
7611 location_t refloc = UNKNOWN_LOCATION;
7613 if (name != NULL_TREE)
7614 ref = lookup_tag (code, name, true, &refloc);
7615 if (ref && TREE_CODE (ref) == code)
7617 if (TYPE_STUB_DECL (ref))
7618 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7620 if (TYPE_SIZE (ref))
7622 if (code == UNION_TYPE)
7623 error_at (loc, "redefinition of %<union %E%>", name);
7624 else
7625 error_at (loc, "redefinition of %<struct %E%>", name);
7626 if (refloc != UNKNOWN_LOCATION)
7627 inform (refloc, "originally defined here");
7628 /* Don't create structures using a name already in use. */
7629 ref = NULL_TREE;
7631 else if (C_TYPE_BEING_DEFINED (ref))
7633 if (code == UNION_TYPE)
7634 error_at (loc, "nested redefinition of %<union %E%>", name);
7635 else
7636 error_at (loc, "nested redefinition of %<struct %E%>", name);
7637 /* Don't bother to report "originally defined here" for a
7638 nested redefinition; the original definition should be
7639 obvious. */
7640 /* Don't create structures that contain themselves. */
7641 ref = NULL_TREE;
7645 /* Otherwise create a forward-reference just so the tag is in scope. */
7647 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7649 ref = make_node (code);
7650 pushtag (loc, name, ref);
7653 C_TYPE_BEING_DEFINED (ref) = 1;
7654 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7655 TYPE_PACKED (v) = flag_pack_struct;
7657 *enclosing_struct_parse_info = struct_parse_info;
7658 struct_parse_info = new c_struct_parse_info ();
7660 /* FIXME: This will issue a warning for a use of a type defined
7661 within a statement expr used within sizeof, et. al. This is not
7662 terribly serious as C++ doesn't permit statement exprs within
7663 sizeof anyhow. */
7664 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7665 warning_at (loc, OPT_Wc___compat,
7666 "defining type in %qs expression is invalid in C++",
7667 (in_sizeof
7668 ? "sizeof"
7669 : (in_typeof ? "typeof" : "alignof")));
7671 return ref;
7674 /* Process the specs, declarator and width (NULL if omitted)
7675 of a structure component, returning a FIELD_DECL node.
7676 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7677 DECL_ATTRS is as for grokdeclarator.
7679 LOC is the location of the structure component.
7681 This is done during the parsing of the struct declaration.
7682 The FIELD_DECL nodes are chained together and the lot of them
7683 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7685 tree
7686 grokfield (location_t loc,
7687 struct c_declarator *declarator, struct c_declspecs *declspecs,
7688 tree width, tree *decl_attrs)
7690 tree value;
7692 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7693 && width == NULL_TREE)
7695 /* This is an unnamed decl.
7697 If we have something of the form "union { list } ;" then this
7698 is the anonymous union extension. Similarly for struct.
7700 If this is something of the form "struct foo;", then
7701 If MS or Plan 9 extensions are enabled, this is handled as
7702 an anonymous struct.
7703 Otherwise this is a forward declaration of a structure tag.
7705 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7706 If foo names a structure or union without a tag, then this
7707 is an anonymous struct (this is permitted by C11).
7708 If MS or Plan 9 extensions are enabled and foo names a
7709 structure, then again this is an anonymous struct.
7710 Otherwise this is an error.
7712 Oh what a horrid tangled web we weave. I wonder if MS consciously
7713 took this from Plan 9 or if it was an accident of implementation
7714 that took root before someone noticed the bug... */
7716 tree type = declspecs->type;
7717 bool ok = false;
7719 if (RECORD_OR_UNION_TYPE_P (type)
7720 && (flag_ms_extensions
7721 || flag_plan9_extensions
7722 || !declspecs->typedef_p))
7724 if (flag_ms_extensions || flag_plan9_extensions)
7725 ok = true;
7726 else if (TYPE_NAME (type) == NULL)
7727 ok = true;
7728 else
7729 ok = false;
7731 if (!ok)
7733 pedwarn (loc, 0, "declaration does not declare anything");
7734 return NULL_TREE;
7736 if (flag_isoc99)
7737 pedwarn_c99 (loc, OPT_Wpedantic,
7738 "ISO C99 doesn%'t support unnamed structs/unions");
7739 else
7740 pedwarn_c99 (loc, OPT_Wpedantic,
7741 "ISO C90 doesn%'t support unnamed structs/unions");
7744 value = grokdeclarator (declarator, declspecs, FIELD, false,
7745 width ? &width : NULL, decl_attrs, NULL, NULL,
7746 DEPRECATED_NORMAL);
7748 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7749 DECL_INITIAL (value) = width;
7750 if (width)
7751 SET_DECL_C_BIT_FIELD (value);
7753 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7755 /* If we currently have a binding for this field, set the
7756 in_struct field in the binding, so that we warn about lookups
7757 which find it. */
7758 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7759 if (b != NULL)
7761 /* If the in_struct field is not yet set, push it on a list
7762 to be cleared when this struct is finished. */
7763 if (!b->in_struct)
7765 struct_parse_info->fields.safe_push (b);
7766 b->in_struct = 1;
7771 return value;
7774 /* Subroutine of detect_field_duplicates: return whether X and Y,
7775 which are both fields in the same struct, have duplicate field
7776 names. */
7778 static bool
7779 is_duplicate_field (tree x, tree y)
7781 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7782 return true;
7784 /* When using -fplan9-extensions, an anonymous field whose name is a
7785 typedef can duplicate a field name. */
7786 if (flag_plan9_extensions
7787 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7789 tree xt, xn, yt, yn;
7791 xt = TREE_TYPE (x);
7792 if (DECL_NAME (x) != NULL_TREE)
7793 xn = DECL_NAME (x);
7794 else if (RECORD_OR_UNION_TYPE_P (xt)
7795 && TYPE_NAME (xt) != NULL_TREE
7796 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7797 xn = DECL_NAME (TYPE_NAME (xt));
7798 else
7799 xn = NULL_TREE;
7801 yt = TREE_TYPE (y);
7802 if (DECL_NAME (y) != NULL_TREE)
7803 yn = DECL_NAME (y);
7804 else if (RECORD_OR_UNION_TYPE_P (yt)
7805 && TYPE_NAME (yt) != NULL_TREE
7806 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7807 yn = DECL_NAME (TYPE_NAME (yt));
7808 else
7809 yn = NULL_TREE;
7811 if (xn != NULL_TREE && xn == yn)
7812 return true;
7815 return false;
7818 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7819 to HTAB, giving errors for any duplicates. */
7821 static void
7822 detect_field_duplicates_hash (tree fieldlist,
7823 hash_table<nofree_ptr_hash <tree_node> > *htab)
7825 tree x, y;
7826 tree_node **slot;
7828 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7829 if ((y = DECL_NAME (x)) != NULL_TREE)
7831 slot = htab->find_slot (y, INSERT);
7832 if (*slot)
7834 error ("duplicate member %q+D", x);
7835 DECL_NAME (x) = NULL_TREE;
7837 *slot = y;
7839 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7841 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7843 /* When using -fplan9-extensions, an anonymous field whose
7844 name is a typedef can duplicate a field name. */
7845 if (flag_plan9_extensions
7846 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7847 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7849 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7850 slot = htab->find_slot (xn, INSERT);
7851 if (*slot)
7852 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7853 *slot = xn;
7858 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7859 the list such that this does not present a problem later. */
7861 static void
7862 detect_field_duplicates (tree fieldlist)
7864 tree x, y;
7865 int timeout = 10;
7867 /* If the struct is the list of instance variables of an Objective-C
7868 class, then we need to check all the instance variables of
7869 superclasses when checking for duplicates (since you can't have
7870 an instance variable in a subclass with the same name as an
7871 instance variable in a superclass). We pass on this job to the
7872 Objective-C compiler. objc_detect_field_duplicates() will return
7873 false if we are not checking the list of instance variables and
7874 the C frontend should proceed with the standard field duplicate
7875 checks. If we are checking the list of instance variables, the
7876 ObjC frontend will do the check, emit the errors if needed, and
7877 then return true. */
7878 if (c_dialect_objc ())
7879 if (objc_detect_field_duplicates (false))
7880 return;
7882 /* First, see if there are more than "a few" fields.
7883 This is trivially true if there are zero or one fields. */
7884 if (!fieldlist || !DECL_CHAIN (fieldlist))
7885 return;
7886 x = fieldlist;
7887 do {
7888 timeout--;
7889 if (DECL_NAME (x) == NULL_TREE
7890 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7891 timeout = 0;
7892 x = DECL_CHAIN (x);
7893 } while (timeout > 0 && x);
7895 /* If there were "few" fields and no anonymous structures or unions,
7896 avoid the overhead of allocating a hash table. Instead just do
7897 the nested traversal thing. */
7898 if (timeout > 0)
7900 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7901 /* When using -fplan9-extensions, we can have duplicates
7902 between typedef names and fields. */
7903 if (DECL_NAME (x)
7904 || (flag_plan9_extensions
7905 && DECL_NAME (x) == NULL_TREE
7906 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7907 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7908 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7910 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7911 if (is_duplicate_field (y, x))
7913 error ("duplicate member %q+D", x);
7914 DECL_NAME (x) = NULL_TREE;
7918 else
7920 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7921 detect_field_duplicates_hash (fieldlist, &htab);
7925 /* Finish up struct info used by -Wc++-compat. */
7927 static void
7928 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7929 location_t record_loc)
7931 unsigned int ix;
7932 tree x;
7933 struct c_binding *b;
7935 if (fieldlist == NULL_TREE)
7937 if (code == RECORD_TYPE)
7938 warning_at (record_loc, OPT_Wc___compat,
7939 "empty struct has size 0 in C, size 1 in C++");
7940 else
7941 warning_at (record_loc, OPT_Wc___compat,
7942 "empty union has size 0 in C, size 1 in C++");
7945 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7946 the current struct. We do this now at the end of the struct
7947 because the flag is used to issue visibility warnings, and we
7948 only want to issue those warnings if the type is referenced
7949 outside of the struct declaration. */
7950 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7951 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7953 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7954 typedefs used when declaring fields in this struct. If the name
7955 of any of the fields is also a typedef name then the struct would
7956 not parse in C++, because the C++ lookup rules say that the
7957 typedef name would be looked up in the context of the struct, and
7958 would thus be the field rather than the typedef. */
7959 if (!struct_parse_info->typedefs_seen.is_empty ()
7960 && fieldlist != NULL_TREE)
7962 /* Use a hash_set<tree> using the name of the typedef. We can use
7963 a hash_set<tree> because identifiers are interned. */
7964 hash_set<tree> tset;
7966 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7967 tset.add (DECL_NAME (x));
7969 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7971 if (DECL_NAME (x) != NULL_TREE
7972 && tset.contains (DECL_NAME (x)))
7974 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7975 ("using %qD as both field and typedef name is "
7976 "invalid in C++"),
7978 /* FIXME: It would be nice to report the location where
7979 the typedef name is used. */
7984 /* For each field which has a binding and which was not defined in
7985 an enclosing struct, clear the in_struct field. */
7986 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7987 b->in_struct = 0;
7990 /* Function to help qsort sort FIELD_DECLs by name order. */
7992 static int
7993 field_decl_cmp (const void *x_p, const void *y_p)
7995 const tree *const x = (const tree *) x_p;
7996 const tree *const y = (const tree *) y_p;
7998 if (DECL_NAME (*x) == DECL_NAME (*y))
7999 /* A nontype is "greater" than a type. */
8000 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8001 if (DECL_NAME (*x) == NULL_TREE)
8002 return -1;
8003 if (DECL_NAME (*y) == NULL_TREE)
8004 return 1;
8005 if (DECL_NAME (*x) < DECL_NAME (*y))
8006 return -1;
8007 return 1;
8010 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
8011 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
8012 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
8013 ATTRIBUTES are attributes to be applied to the structure.
8015 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
8016 the struct was started. */
8018 tree
8019 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
8020 struct c_struct_parse_info *enclosing_struct_parse_info)
8022 tree x;
8023 bool toplevel = file_scope == current_scope;
8025 /* If this type was previously laid out as a forward reference,
8026 make sure we lay it out again. */
8028 TYPE_SIZE (t) = NULL_TREE;
8030 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8032 if (pedantic)
8034 for (x = fieldlist; x; x = DECL_CHAIN (x))
8036 if (DECL_NAME (x) != NULL_TREE)
8037 break;
8038 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8039 break;
8042 if (x == NULL_TREE)
8044 if (TREE_CODE (t) == UNION_TYPE)
8046 if (fieldlist)
8047 pedwarn (loc, OPT_Wpedantic, "union has no named members");
8048 else
8049 pedwarn (loc, OPT_Wpedantic, "union has no members");
8051 else
8053 if (fieldlist)
8054 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
8055 else
8056 pedwarn (loc, OPT_Wpedantic, "struct has no members");
8061 /* Install struct as DECL_CONTEXT of each field decl.
8062 Also process specified field sizes, found in the DECL_INITIAL,
8063 storing 0 there after the type has been changed to precision equal
8064 to its width, rather than the precision of the specified standard
8065 type. (Correct layout requires the original type to have been preserved
8066 until now.) */
8068 bool saw_named_field = false;
8069 for (x = fieldlist; x; x = DECL_CHAIN (x))
8071 if (TREE_TYPE (x) == error_mark_node)
8072 continue;
8074 DECL_CONTEXT (x) = t;
8076 /* If any field is const, the structure type is pseudo-const. */
8077 if (TREE_READONLY (x))
8078 C_TYPE_FIELDS_READONLY (t) = 1;
8079 else
8081 /* A field that is pseudo-const makes the structure likewise. */
8082 tree t1 = strip_array_types (TREE_TYPE (x));
8083 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8084 C_TYPE_FIELDS_READONLY (t) = 1;
8087 /* Any field that is volatile means variables of this type must be
8088 treated in some ways as volatile. */
8089 if (TREE_THIS_VOLATILE (x))
8090 C_TYPE_FIELDS_VOLATILE (t) = 1;
8092 /* Any field of nominal variable size implies structure is too. */
8093 if (C_DECL_VARIABLE_SIZE (x))
8094 C_TYPE_VARIABLE_SIZE (t) = 1;
8096 if (DECL_C_BIT_FIELD (x))
8098 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8099 DECL_SIZE (x) = bitsize_int (width);
8100 DECL_BIT_FIELD (x) = 1;
8103 if (TYPE_PACKED (t)
8104 && (DECL_BIT_FIELD (x)
8105 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8106 DECL_PACKED (x) = 1;
8108 /* Detect flexible array member in an invalid context. */
8109 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8110 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8111 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8112 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8114 if (TREE_CODE (t) == UNION_TYPE)
8116 error_at (DECL_SOURCE_LOCATION (x),
8117 "flexible array member in union");
8118 TREE_TYPE (x) = error_mark_node;
8120 else if (DECL_CHAIN (x) != NULL_TREE)
8122 error_at (DECL_SOURCE_LOCATION (x),
8123 "flexible array member not at end of struct");
8124 TREE_TYPE (x) = error_mark_node;
8126 else if (!saw_named_field)
8128 error_at (DECL_SOURCE_LOCATION (x),
8129 "flexible array member in a struct with no named "
8130 "members");
8131 TREE_TYPE (x) = error_mark_node;
8135 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8136 && flexible_array_type_p (TREE_TYPE (x)))
8137 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8138 "invalid use of structure with flexible array member");
8140 if (DECL_NAME (x)
8141 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8142 saw_named_field = true;
8145 detect_field_duplicates (fieldlist);
8147 /* Now we have the nearly final fieldlist. Record it,
8148 then lay out the structure or union (including the fields). */
8150 TYPE_FIELDS (t) = fieldlist;
8152 maybe_apply_pragma_scalar_storage_order (t);
8154 layout_type (t);
8156 if (TYPE_SIZE_UNIT (t)
8157 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8158 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8159 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8160 error ("type %qT is too large", t);
8162 /* Give bit-fields their proper types and rewrite the type of array fields
8163 with scalar component if the enclosing type has reverse storage order. */
8164 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8166 if (TREE_CODE (field) == FIELD_DECL
8167 && DECL_INITIAL (field)
8168 && TREE_TYPE (field) != error_mark_node)
8170 unsigned HOST_WIDE_INT width
8171 = tree_to_uhwi (DECL_INITIAL (field));
8172 tree type = TREE_TYPE (field);
8173 if (width != TYPE_PRECISION (type))
8175 TREE_TYPE (field)
8176 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8177 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8179 DECL_INITIAL (field) = NULL_TREE;
8181 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8182 && TREE_CODE (field) == FIELD_DECL
8183 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8185 tree ftype = TREE_TYPE (field);
8186 tree ctype = strip_array_types (ftype);
8187 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8189 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8190 tree *typep = &fmain_type;
8191 do {
8192 *typep = build_distinct_type_copy (*typep);
8193 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8194 typep = &TREE_TYPE (*typep);
8195 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8196 TREE_TYPE (field)
8197 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8202 /* Now we have the truly final field list.
8203 Store it in this type and in the variants. */
8205 TYPE_FIELDS (t) = fieldlist;
8207 /* If there are lots of fields, sort so we can look through them fast.
8208 We arbitrarily consider 16 or more elts to be "a lot". */
8211 int len = 0;
8213 for (x = fieldlist; x; x = DECL_CHAIN (x))
8215 if (len > 15 || DECL_NAME (x) == NULL)
8216 break;
8217 len += 1;
8220 if (len > 15)
8222 tree *field_array;
8223 struct lang_type *space;
8224 struct sorted_fields_type *space2;
8226 len += list_length (x);
8228 /* Use the same allocation policy here that make_node uses, to
8229 ensure that this lives as long as the rest of the struct decl.
8230 All decls in an inline function need to be saved. */
8232 space = ggc_cleared_alloc<struct lang_type> ();
8233 space2 = (sorted_fields_type *) ggc_internal_alloc
8234 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8236 len = 0;
8237 space->s = space2;
8238 field_array = &space2->elts[0];
8239 for (x = fieldlist; x; x = DECL_CHAIN (x))
8241 field_array[len++] = x;
8243 /* If there is anonymous struct or union, break out of the loop. */
8244 if (DECL_NAME (x) == NULL)
8245 break;
8247 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8248 if (x == NULL)
8250 TYPE_LANG_SPECIFIC (t) = space;
8251 TYPE_LANG_SPECIFIC (t)->s->len = len;
8252 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8253 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8258 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8259 in dwarf2out via rest_of_decl_compilation below and means
8260 something totally different. Since we will be clearing
8261 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8262 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8263 C_TYPE_INCOMPLETE_VARS should use some language specific
8264 node. */
8265 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8266 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8268 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8269 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8270 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8271 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8272 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8273 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8276 /* If this was supposed to be a transparent union, but we can't
8277 make it one, warn and turn off the flag. */
8278 if (TREE_CODE (t) == UNION_TYPE
8279 && TYPE_TRANSPARENT_AGGR (t)
8280 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8282 TYPE_TRANSPARENT_AGGR (t) = 0;
8283 warning_at (loc, 0, "union cannot be made transparent");
8286 /* Update type location to the one of the definition, instead of e.g.
8287 a forward declaration. */
8288 if (TYPE_STUB_DECL (t))
8289 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8291 /* Finish debugging output for this type. */
8292 rest_of_type_compilation (t, toplevel);
8294 /* If this structure or union completes the type of any previous
8295 variable declaration, lay it out and output its rtl. */
8296 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8298 tree decl = TREE_VALUE (x);
8299 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8300 layout_array_type (TREE_TYPE (decl));
8301 if (TREE_CODE (decl) != TYPE_DECL)
8303 layout_decl (decl, 0);
8304 if (c_dialect_objc ())
8305 objc_check_decl (decl);
8306 rest_of_decl_compilation (decl, toplevel, 0);
8310 /* If we're inside a function proper, i.e. not file-scope and not still
8311 parsing parameters, then arrange for the size of a variable sized type
8312 to be bound now. */
8313 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8314 add_stmt (build_stmt (loc,
8315 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8317 if (warn_cxx_compat)
8318 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8320 delete struct_parse_info;
8322 struct_parse_info = enclosing_struct_parse_info;
8324 /* If this struct is defined inside a struct, add it to
8325 struct_types. */
8326 if (warn_cxx_compat
8327 && struct_parse_info != NULL
8328 && !in_sizeof && !in_typeof && !in_alignof)
8329 struct_parse_info->struct_types.safe_push (t);
8331 return t;
8334 static struct {
8335 gt_pointer_operator new_value;
8336 void *cookie;
8337 } resort_data;
8339 /* This routine compares two fields like field_decl_cmp but using the
8340 pointer operator in resort_data. */
8342 static int
8343 resort_field_decl_cmp (const void *x_p, const void *y_p)
8345 const tree *const x = (const tree *) x_p;
8346 const tree *const y = (const tree *) y_p;
8348 if (DECL_NAME (*x) == DECL_NAME (*y))
8349 /* A nontype is "greater" than a type. */
8350 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8351 if (DECL_NAME (*x) == NULL_TREE)
8352 return -1;
8353 if (DECL_NAME (*y) == NULL_TREE)
8354 return 1;
8356 tree d1 = DECL_NAME (*x);
8357 tree d2 = DECL_NAME (*y);
8358 resort_data.new_value (&d1, resort_data.cookie);
8359 resort_data.new_value (&d2, resort_data.cookie);
8360 if (d1 < d2)
8361 return -1;
8363 return 1;
8366 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8368 void
8369 resort_sorted_fields (void *obj,
8370 void * ARG_UNUSED (orig_obj),
8371 gt_pointer_operator new_value,
8372 void *cookie)
8374 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8375 resort_data.new_value = new_value;
8376 resort_data.cookie = cookie;
8377 qsort (&sf->elts[0], sf->len, sizeof (tree),
8378 resort_field_decl_cmp);
8381 /* Lay out the type T, and its element type, and so on. */
8383 static void
8384 layout_array_type (tree t)
8386 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8387 layout_array_type (TREE_TYPE (t));
8388 layout_type (t);
8391 /* Begin compiling the definition of an enumeration type.
8392 NAME is its name (or null if anonymous).
8393 LOC is the enum's location.
8394 Returns the type object, as yet incomplete.
8395 Also records info about it so that build_enumerator
8396 may be used to declare the individual values as they are read. */
8398 tree
8399 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8401 tree enumtype = NULL_TREE;
8402 location_t enumloc = UNKNOWN_LOCATION;
8404 /* If this is the real definition for a previous forward reference,
8405 fill in the contents in the same object that used to be the
8406 forward reference. */
8408 if (name != NULL_TREE)
8409 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8411 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8413 enumtype = make_node (ENUMERAL_TYPE);
8414 pushtag (loc, name, enumtype);
8416 /* Update type location to the one of the definition, instead of e.g.
8417 a forward declaration. */
8418 else if (TYPE_STUB_DECL (enumtype))
8420 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8421 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8424 if (C_TYPE_BEING_DEFINED (enumtype))
8425 error_at (loc, "nested redefinition of %<enum %E%>", name);
8427 C_TYPE_BEING_DEFINED (enumtype) = 1;
8429 if (TYPE_VALUES (enumtype) != NULL_TREE)
8431 /* This enum is a named one that has been declared already. */
8432 error_at (loc, "redeclaration of %<enum %E%>", name);
8433 if (enumloc != UNKNOWN_LOCATION)
8434 inform (enumloc, "originally defined here");
8436 /* Completely replace its old definition.
8437 The old enumerators remain defined, however. */
8438 TYPE_VALUES (enumtype) = NULL_TREE;
8441 the_enum->enum_next_value = integer_zero_node;
8442 the_enum->enum_overflow = 0;
8444 if (flag_short_enums)
8445 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8446 TYPE_PACKED (v) = 1;
8448 /* FIXME: This will issue a warning for a use of a type defined
8449 within sizeof in a statement expr. This is not terribly serious
8450 as C++ doesn't permit statement exprs within sizeof anyhow. */
8451 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8452 warning_at (loc, OPT_Wc___compat,
8453 "defining type in %qs expression is invalid in C++",
8454 (in_sizeof
8455 ? "sizeof"
8456 : (in_typeof ? "typeof" : "alignof")));
8458 return enumtype;
8461 /* After processing and defining all the values of an enumeration type,
8462 install their decls in the enumeration type and finish it off.
8463 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8464 and ATTRIBUTES are the specified attributes.
8465 Returns ENUMTYPE. */
8467 tree
8468 finish_enum (tree enumtype, tree values, tree attributes)
8470 tree pair, tem;
8471 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8472 int precision;
8473 signop sign;
8474 bool toplevel = (file_scope == current_scope);
8475 struct lang_type *lt;
8477 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8479 /* Calculate the maximum value of any enumerator in this type. */
8481 if (values == error_mark_node)
8482 minnode = maxnode = integer_zero_node;
8483 else
8485 minnode = maxnode = TREE_VALUE (values);
8486 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8488 tree value = TREE_VALUE (pair);
8489 if (tree_int_cst_lt (maxnode, value))
8490 maxnode = value;
8491 if (tree_int_cst_lt (value, minnode))
8492 minnode = value;
8496 /* Construct the final type of this enumeration. It is the same
8497 as one of the integral types - the narrowest one that fits, except
8498 that normally we only go as narrow as int - and signed iff any of
8499 the values are negative. */
8500 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8501 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8502 tree_int_cst_min_precision (maxnode, sign));
8504 /* If the precision of the type was specified with an attribute and it
8505 was too small, give an error. Otherwise, use it. */
8506 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8508 if (precision > TYPE_PRECISION (enumtype))
8510 TYPE_PRECISION (enumtype) = 0;
8511 error ("specified mode too small for enumeral values");
8513 else
8514 precision = TYPE_PRECISION (enumtype);
8516 else
8517 TYPE_PRECISION (enumtype) = 0;
8519 if (TYPE_PACKED (enumtype)
8520 || precision > TYPE_PRECISION (integer_type_node)
8521 || TYPE_PRECISION (enumtype))
8523 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8524 if (tem == NULL)
8526 warning (0, "enumeration values exceed range of largest integer");
8527 tem = long_long_integer_type_node;
8530 else
8531 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8533 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8534 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8535 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8536 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8537 TYPE_SIZE (enumtype) = NULL_TREE;
8538 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8540 layout_type (enumtype);
8542 if (values != error_mark_node)
8544 /* Change the type of the enumerators to be the enum type. We
8545 need to do this irrespective of the size of the enum, for
8546 proper type checking. Replace the DECL_INITIALs of the
8547 enumerators, and the value slots of the list, with copies
8548 that have the enum type; they cannot be modified in place
8549 because they may be shared (e.g. integer_zero_node) Finally,
8550 change the purpose slots to point to the names of the decls. */
8551 for (pair = values; pair; pair = TREE_CHAIN (pair))
8553 tree enu = TREE_PURPOSE (pair);
8554 tree ini = DECL_INITIAL (enu);
8556 TREE_TYPE (enu) = enumtype;
8558 /* The ISO C Standard mandates enumerators to have type int,
8559 even though the underlying type of an enum type is
8560 unspecified. However, GCC allows enumerators of any
8561 integer type as an extensions. build_enumerator()
8562 converts any enumerators that fit in an int to type int,
8563 to avoid promotions to unsigned types when comparing
8564 integers with enumerators that fit in the int range.
8565 When -pedantic is given, build_enumerator() would have
8566 already warned about those that don't fit. Here we
8567 convert the rest to the enumerator type. */
8568 if (TREE_TYPE (ini) != integer_type_node)
8569 ini = convert (enumtype, ini);
8571 DECL_INITIAL (enu) = ini;
8572 TREE_PURPOSE (pair) = DECL_NAME (enu);
8573 TREE_VALUE (pair) = ini;
8576 TYPE_VALUES (enumtype) = values;
8579 /* Record the min/max values so that we can warn about bit-field
8580 enumerations that are too small for the values. */
8581 lt = ggc_cleared_alloc<struct lang_type> ();
8582 lt->enum_min = minnode;
8583 lt->enum_max = maxnode;
8584 TYPE_LANG_SPECIFIC (enumtype) = lt;
8586 /* Fix up all variant types of this enum type. */
8587 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8589 if (tem == enumtype)
8590 continue;
8591 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8592 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8593 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8594 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8595 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8596 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8597 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8598 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8599 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8600 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8601 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8604 /* Finish debugging output for this type. */
8605 rest_of_type_compilation (enumtype, toplevel);
8607 /* If this enum is defined inside a struct, add it to
8608 struct_types. */
8609 if (warn_cxx_compat
8610 && struct_parse_info != NULL
8611 && !in_sizeof && !in_typeof && !in_alignof)
8612 struct_parse_info->struct_types.safe_push (enumtype);
8614 return enumtype;
8617 /* Build and install a CONST_DECL for one value of the
8618 current enumeration type (one that was begun with start_enum).
8619 DECL_LOC is the location of the enumerator.
8620 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8621 Return a tree-list containing the CONST_DECL and its value.
8622 Assignment of sequential values by default is handled here. */
8624 tree
8625 build_enumerator (location_t decl_loc, location_t loc,
8626 struct c_enum_contents *the_enum, tree name, tree value)
8628 tree decl, type;
8630 /* Validate and default VALUE. */
8632 if (value != NULL_TREE)
8634 /* Don't issue more errors for error_mark_node (i.e. an
8635 undeclared identifier) - just ignore the value expression. */
8636 if (value == error_mark_node)
8637 value = NULL_TREE;
8638 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8640 error_at (loc, "enumerator value for %qE is not an integer constant",
8641 name);
8642 value = NULL_TREE;
8644 else
8646 if (TREE_CODE (value) != INTEGER_CST)
8648 value = c_fully_fold (value, false, NULL);
8649 if (TREE_CODE (value) == INTEGER_CST)
8650 pedwarn (loc, OPT_Wpedantic,
8651 "enumerator value for %qE is not an integer "
8652 "constant expression", name);
8654 if (TREE_CODE (value) != INTEGER_CST)
8656 error ("enumerator value for %qE is not an integer constant",
8657 name);
8658 value = NULL_TREE;
8660 else
8662 value = default_conversion (value);
8663 constant_expression_warning (value);
8668 /* Default based on previous value. */
8669 /* It should no longer be possible to have NON_LVALUE_EXPR
8670 in the default. */
8671 if (value == NULL_TREE)
8673 value = the_enum->enum_next_value;
8674 if (the_enum->enum_overflow)
8675 error_at (loc, "overflow in enumeration values");
8677 /* Even though the underlying type of an enum is unspecified, the
8678 type of enumeration constants is explicitly defined as int
8679 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8680 an extension. */
8681 else if (!int_fits_type_p (value, integer_type_node))
8682 pedwarn (loc, OPT_Wpedantic,
8683 "ISO C restricts enumerator values to range of %<int%>");
8685 /* The ISO C Standard mandates enumerators to have type int, even
8686 though the underlying type of an enum type is unspecified.
8687 However, GCC allows enumerators of any integer type as an
8688 extensions. Here we convert any enumerators that fit in an int
8689 to type int, to avoid promotions to unsigned types when comparing
8690 integers with enumerators that fit in the int range. When
8691 -pedantic is given, we would have already warned about those that
8692 don't fit. We have to do this here rather than in finish_enum
8693 because this value may be used to define more enumerators. */
8694 if (int_fits_type_p (value, integer_type_node))
8695 value = convert (integer_type_node, value);
8697 /* Set basis for default for next value. */
8698 the_enum->enum_next_value
8699 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8700 PLUS_EXPR, value, integer_one_node, false);
8701 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8703 /* Now create a declaration for the enum value name. */
8705 type = TREE_TYPE (value);
8706 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8707 TYPE_PRECISION (integer_type_node)),
8708 (TYPE_PRECISION (type)
8709 >= TYPE_PRECISION (integer_type_node)
8710 && TYPE_UNSIGNED (type)));
8712 decl = build_decl (decl_loc, CONST_DECL, name, type);
8713 DECL_INITIAL (decl) = convert (type, value);
8714 pushdecl (decl);
8716 return tree_cons (decl, value, NULL_TREE);
8720 /* Create the FUNCTION_DECL for a function definition.
8721 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8722 the declaration; they describe the function's name and the type it returns,
8723 but twisted together in a fashion that parallels the syntax of C.
8725 This function creates a binding context for the function body
8726 as well as setting up the FUNCTION_DECL in current_function_decl.
8728 Returns true on success. If the DECLARATOR is not suitable for a function
8729 (it defines a datum instead), we return false to report a parse error. */
8731 bool
8732 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8733 tree attributes)
8735 tree decl1, old_decl;
8736 tree restype, resdecl;
8737 location_t loc;
8739 current_function_returns_value = 0; /* Assume, until we see it does. */
8740 current_function_returns_null = 0;
8741 current_function_returns_abnormally = 0;
8742 warn_about_return_type = 0;
8743 c_switch_stack = NULL;
8745 /* Indicate no valid break/continue context by setting these variables
8746 to some non-null, non-label value. We'll notice and emit the proper
8747 error message in c_finish_bc_stmt. */
8748 c_break_label = c_cont_label = size_zero_node;
8750 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8751 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8752 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8754 /* If the declarator is not suitable for a function definition,
8755 cause a syntax error. */
8756 if (decl1 == NULL_TREE
8757 || TREE_CODE (decl1) != FUNCTION_DECL)
8758 return false;
8760 loc = DECL_SOURCE_LOCATION (decl1);
8762 c_decl_attributes (&decl1, attributes, 0);
8764 if (DECL_DECLARED_INLINE_P (decl1)
8765 && DECL_UNINLINABLE (decl1)
8766 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8767 warning_at (loc, OPT_Wattributes,
8768 "inline function %qD given attribute noinline",
8769 decl1);
8771 /* Handle gnu_inline attribute. */
8772 if (declspecs->inline_p
8773 && !flag_gnu89_inline
8774 && TREE_CODE (decl1) == FUNCTION_DECL
8775 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8776 || current_function_decl))
8778 if (declspecs->storage_class != csc_static)
8779 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8782 announce_function (decl1);
8784 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8786 error_at (loc, "return type is an incomplete type");
8787 /* Make it return void instead. */
8788 TREE_TYPE (decl1)
8789 = build_function_type (void_type_node,
8790 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8793 if (warn_about_return_type)
8794 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8795 : (warn_return_type > 0 ? OPT_Wreturn_type
8796 : OPT_Wimplicit_int),
8797 "return type defaults to %<int%>");
8799 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8800 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8801 DECL_INITIAL (decl1) = error_mark_node;
8803 /* A nested function is not global. */
8804 if (current_function_decl != NULL_TREE)
8805 TREE_PUBLIC (decl1) = 0;
8807 /* If this definition isn't a prototype and we had a prototype declaration
8808 before, copy the arg type info from that prototype. */
8809 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8810 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8811 old_decl = NULL_TREE;
8812 current_function_prototype_locus = UNKNOWN_LOCATION;
8813 current_function_prototype_built_in = false;
8814 current_function_prototype_arg_types = NULL_TREE;
8815 if (!prototype_p (TREE_TYPE (decl1)))
8817 if (old_decl != NULL_TREE
8818 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8819 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8820 TREE_TYPE (TREE_TYPE (old_decl))))
8822 if (stdarg_p (TREE_TYPE (old_decl)))
8824 auto_diagnostic_group d;
8825 warning_at (loc, 0, "%q+D defined as variadic function "
8826 "without prototype", decl1);
8827 locate_old_decl (old_decl);
8829 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8830 TREE_TYPE (decl1));
8831 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8832 current_function_prototype_built_in
8833 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8834 current_function_prototype_arg_types
8835 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8837 if (TREE_PUBLIC (decl1))
8839 /* If there is an external prototype declaration of this
8840 function, record its location but do not copy information
8841 to this decl. This may be an invisible declaration
8842 (built-in or in a scope which has finished) or simply
8843 have more refined argument types than any declaration
8844 found above. */
8845 struct c_binding *b;
8846 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8847 if (B_IN_SCOPE (b, external_scope))
8848 break;
8849 if (b)
8851 tree ext_decl, ext_type;
8852 ext_decl = b->decl;
8853 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8854 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8855 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8856 TREE_TYPE (ext_type)))
8858 current_function_prototype_locus
8859 = DECL_SOURCE_LOCATION (ext_decl);
8860 current_function_prototype_built_in
8861 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8862 current_function_prototype_arg_types
8863 = TYPE_ARG_TYPES (ext_type);
8869 /* Optionally warn of old-fashioned def with no previous prototype. */
8870 if (warn_strict_prototypes
8871 && old_decl != error_mark_node
8872 && !prototype_p (TREE_TYPE (decl1))
8873 && C_DECL_ISNT_PROTOTYPE (old_decl))
8874 warning_at (loc, OPT_Wstrict_prototypes,
8875 "function declaration isn%'t a prototype");
8876 /* Optionally warn of any global def with no previous prototype. */
8877 else if (warn_missing_prototypes
8878 && old_decl != error_mark_node
8879 && TREE_PUBLIC (decl1)
8880 && !MAIN_NAME_P (DECL_NAME (decl1))
8881 && C_DECL_ISNT_PROTOTYPE (old_decl)
8882 && !DECL_DECLARED_INLINE_P (decl1))
8883 warning_at (loc, OPT_Wmissing_prototypes,
8884 "no previous prototype for %qD", decl1);
8885 /* Optionally warn of any def with no previous prototype
8886 if the function has already been used. */
8887 else if (warn_missing_prototypes
8888 && old_decl != NULL_TREE
8889 && old_decl != error_mark_node
8890 && TREE_USED (old_decl)
8891 && !prototype_p (TREE_TYPE (old_decl)))
8892 warning_at (loc, OPT_Wmissing_prototypes,
8893 "%qD was used with no prototype before its definition", decl1);
8894 /* Optionally warn of any global def with no previous declaration. */
8895 else if (warn_missing_declarations
8896 && TREE_PUBLIC (decl1)
8897 && old_decl == NULL_TREE
8898 && !MAIN_NAME_P (DECL_NAME (decl1))
8899 && !DECL_DECLARED_INLINE_P (decl1))
8900 warning_at (loc, OPT_Wmissing_declarations,
8901 "no previous declaration for %qD",
8902 decl1);
8903 /* Optionally warn of any def with no previous declaration
8904 if the function has already been used. */
8905 else if (warn_missing_declarations
8906 && old_decl != NULL_TREE
8907 && old_decl != error_mark_node
8908 && TREE_USED (old_decl)
8909 && C_DECL_IMPLICIT (old_decl))
8910 warning_at (loc, OPT_Wmissing_declarations,
8911 "%qD was used with no declaration before its definition", decl1);
8913 /* This function exists in static storage.
8914 (This does not mean `static' in the C sense!) */
8915 TREE_STATIC (decl1) = 1;
8917 /* This is the earliest point at which we might know the assembler
8918 name of the function. Thus, if it's set before this, die horribly. */
8919 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8921 /* If #pragma weak was used, mark the decl weak now. */
8922 if (current_scope == file_scope)
8923 maybe_apply_pragma_weak (decl1);
8925 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8926 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8928 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8929 != integer_type_node)
8930 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8931 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8932 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8933 decl1);
8935 check_main_parameter_types (decl1);
8937 if (!TREE_PUBLIC (decl1))
8938 pedwarn (loc, OPT_Wmain,
8939 "%qD is normally a non-static function", decl1);
8942 /* Record the decl so that the function name is defined.
8943 If we already have a decl for this name, and it is a FUNCTION_DECL,
8944 use the old decl. */
8946 current_function_decl = pushdecl (decl1);
8948 push_scope ();
8949 declare_parm_level ();
8951 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8952 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8953 DECL_ARTIFICIAL (resdecl) = 1;
8954 DECL_IGNORED_P (resdecl) = 1;
8955 DECL_RESULT (current_function_decl) = resdecl;
8957 start_fname_decls ();
8959 return true;
8962 /* Subroutine of store_parm_decls which handles new-style function
8963 definitions (prototype format). The parms already have decls, so we
8964 need only record them as in effect and complain if any redundant
8965 old-style parm decls were written. */
8966 static void
8967 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8969 tree decl;
8970 c_arg_tag *tag;
8971 unsigned ix;
8973 if (current_scope->bindings)
8975 error_at (DECL_SOURCE_LOCATION (fndecl),
8976 "old-style parameter declarations in prototyped "
8977 "function definition");
8979 /* Get rid of the old-style declarations. */
8980 pop_scope ();
8981 push_scope ();
8983 /* Don't issue this warning for nested functions, and don't issue this
8984 warning if we got here because ARG_INFO_TYPES was error_mark_node
8985 (this happens when a function definition has just an ellipsis in
8986 its parameter list). */
8987 else if (!in_system_header_at (input_location)
8988 && !current_function_scope
8989 && arg_info->types != error_mark_node)
8990 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8991 "traditional C rejects ISO C style function definitions");
8993 /* Now make all the parameter declarations visible in the function body.
8994 We can bypass most of the grunt work of pushdecl. */
8995 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8997 DECL_CONTEXT (decl) = current_function_decl;
8998 if (DECL_NAME (decl))
9000 bind (DECL_NAME (decl), decl, current_scope,
9001 /*invisible=*/false, /*nested=*/false,
9002 UNKNOWN_LOCATION);
9003 if (!TREE_USED (decl))
9004 warn_if_shadowing (decl);
9006 else
9007 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
9010 /* Record the parameter list in the function declaration. */
9011 DECL_ARGUMENTS (fndecl) = arg_info->parms;
9013 /* Now make all the ancillary declarations visible, likewise. */
9014 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
9016 DECL_CONTEXT (decl) = current_function_decl;
9017 if (DECL_NAME (decl))
9018 bind (DECL_NAME (decl), decl, current_scope,
9019 /*invisible=*/false,
9020 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
9021 UNKNOWN_LOCATION);
9024 /* And all the tag declarations. */
9025 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
9026 if (tag->id)
9027 bind (tag->id, tag->type, current_scope,
9028 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
9031 /* Subroutine of store_parm_decls which handles old-style function
9032 definitions (separate parameter list and declarations). */
9034 static void
9035 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
9037 struct c_binding *b;
9038 tree parm, decl, last;
9039 tree parmids = arg_info->parms;
9040 hash_set<tree> seen_args;
9042 if (!in_system_header_at (input_location))
9043 warning_at (DECL_SOURCE_LOCATION (fndecl),
9044 OPT_Wold_style_definition, "old-style function definition");
9046 /* Match each formal parameter name with its declaration. Save each
9047 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
9048 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9050 if (TREE_VALUE (parm) == NULL_TREE)
9052 error_at (DECL_SOURCE_LOCATION (fndecl),
9053 "parameter name missing from parameter list");
9054 TREE_PURPOSE (parm) = NULL_TREE;
9055 continue;
9058 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
9059 if (b && B_IN_CURRENT_SCOPE (b))
9061 decl = b->decl;
9062 /* Skip erroneous parameters. */
9063 if (decl == error_mark_node)
9064 continue;
9065 /* If we got something other than a PARM_DECL it is an error. */
9066 if (TREE_CODE (decl) != PARM_DECL)
9068 error_at (DECL_SOURCE_LOCATION (decl),
9069 "%qD declared as a non-parameter", decl);
9070 continue;
9072 /* If the declaration is already marked, we have a duplicate
9073 name. Complain and ignore the duplicate. */
9074 else if (seen_args.contains (decl))
9076 error_at (DECL_SOURCE_LOCATION (decl),
9077 "multiple parameters named %qD", decl);
9078 TREE_PURPOSE (parm) = NULL_TREE;
9079 continue;
9081 /* If the declaration says "void", complain and turn it into
9082 an int. */
9083 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9085 error_at (DECL_SOURCE_LOCATION (decl),
9086 "parameter %qD declared with void type", decl);
9087 TREE_TYPE (decl) = integer_type_node;
9088 DECL_ARG_TYPE (decl) = integer_type_node;
9089 layout_decl (decl, 0);
9091 warn_if_shadowing (decl);
9093 /* If no declaration found, default to int. */
9094 else
9096 /* FIXME diagnostics: This should be the location of the argument,
9097 not the FNDECL. E.g., for an old-style declaration
9099 int f10(v) { blah; }
9101 We should use the location of the V, not the F10.
9102 Unfortunately, the V is an IDENTIFIER_NODE which has no
9103 location. In the future we need locations for c_arg_info
9104 entries.
9106 See gcc.dg/Wshadow-3.c for an example of this problem. */
9107 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9108 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9109 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9110 pushdecl (decl);
9111 warn_if_shadowing (decl);
9113 if (flag_isoc99)
9114 pedwarn (DECL_SOURCE_LOCATION (decl),
9115 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9116 decl);
9117 else
9118 warning_at (DECL_SOURCE_LOCATION (decl),
9119 OPT_Wmissing_parameter_type,
9120 "type of %qD defaults to %<int%>", decl);
9123 TREE_PURPOSE (parm) = decl;
9124 seen_args.add (decl);
9127 /* Now examine the parms chain for incomplete declarations
9128 and declarations with no corresponding names. */
9130 for (b = current_scope->bindings; b; b = b->prev)
9132 parm = b->decl;
9133 if (TREE_CODE (parm) != PARM_DECL)
9134 continue;
9136 if (TREE_TYPE (parm) != error_mark_node
9137 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9139 error_at (DECL_SOURCE_LOCATION (parm),
9140 "parameter %qD has incomplete type", parm);
9141 TREE_TYPE (parm) = error_mark_node;
9144 if (!seen_args.contains (parm))
9146 error_at (DECL_SOURCE_LOCATION (parm),
9147 "declaration for parameter %qD but no such parameter",
9148 parm);
9150 /* Pretend the parameter was not missing.
9151 This gets us to a standard state and minimizes
9152 further error messages. */
9153 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9157 /* Chain the declarations together in the order of the list of
9158 names. Store that chain in the function decl, replacing the
9159 list of names. Update the current scope to match. */
9160 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9162 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9163 if (TREE_PURPOSE (parm))
9164 break;
9165 if (parm && TREE_PURPOSE (parm))
9167 last = TREE_PURPOSE (parm);
9168 DECL_ARGUMENTS (fndecl) = last;
9170 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9171 if (TREE_PURPOSE (parm))
9173 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9174 last = TREE_PURPOSE (parm);
9176 DECL_CHAIN (last) = NULL_TREE;
9179 /* If there was a previous prototype,
9180 set the DECL_ARG_TYPE of each argument according to
9181 the type previously specified, and report any mismatches. */
9183 if (current_function_prototype_arg_types)
9185 tree type;
9186 for (parm = DECL_ARGUMENTS (fndecl),
9187 type = current_function_prototype_arg_types;
9188 parm || (type != NULL_TREE
9189 && TREE_VALUE (type) != error_mark_node
9190 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9191 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9193 if (parm == NULL_TREE
9194 || type == NULL_TREE
9195 || (TREE_VALUE (type) != error_mark_node
9196 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9198 if (current_function_prototype_built_in)
9199 warning_at (DECL_SOURCE_LOCATION (fndecl),
9200 0, "number of arguments doesn%'t match "
9201 "built-in prototype");
9202 else
9204 /* FIXME diagnostics: This should be the location of
9205 FNDECL, but there is bug when a prototype is
9206 declared inside function context, but defined
9207 outside of it (e.g., gcc.dg/pr15698-2.c). In
9208 which case FNDECL gets the location of the
9209 prototype, not the definition. */
9210 error_at (input_location,
9211 "number of arguments doesn%'t match prototype");
9213 error_at (current_function_prototype_locus,
9214 "prototype declaration");
9216 break;
9218 /* Type for passing arg must be consistent with that
9219 declared for the arg. ISO C says we take the unqualified
9220 type for parameters declared with qualified type. */
9221 if (TREE_TYPE (parm) != error_mark_node
9222 && TREE_VALUE (type) != error_mark_node
9223 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9224 != TYPE_ATOMIC (TREE_VALUE (type)))
9225 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9226 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9228 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9229 == TYPE_ATOMIC (TREE_VALUE (type)))
9230 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9231 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9233 /* Adjust argument to match prototype. E.g. a previous
9234 `int foo(float);' prototype causes
9235 `int foo(x) float x; {...}' to be treated like
9236 `int foo(float x) {...}'. This is particularly
9237 useful for argument types like uid_t. */
9238 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9240 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9241 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9242 && (TYPE_PRECISION (TREE_TYPE (parm))
9243 < TYPE_PRECISION (integer_type_node)))
9244 DECL_ARG_TYPE (parm)
9245 = c_type_promotes_to (TREE_TYPE (parm));
9247 /* ??? Is it possible to get here with a
9248 built-in prototype or will it always have
9249 been diagnosed as conflicting with an
9250 old-style definition and discarded? */
9251 if (current_function_prototype_built_in)
9252 warning_at (DECL_SOURCE_LOCATION (parm),
9253 OPT_Wpedantic, "promoted argument %qD "
9254 "doesn%'t match built-in prototype", parm);
9255 else
9257 pedwarn (DECL_SOURCE_LOCATION (parm),
9258 OPT_Wpedantic, "promoted argument %qD "
9259 "doesn%'t match prototype", parm);
9260 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9261 "prototype declaration");
9264 else
9266 if (current_function_prototype_built_in)
9267 warning_at (DECL_SOURCE_LOCATION (parm),
9268 0, "argument %qD doesn%'t match "
9269 "built-in prototype", parm);
9270 else
9272 error_at (DECL_SOURCE_LOCATION (parm),
9273 "argument %qD doesn%'t match prototype", parm);
9274 error_at (current_function_prototype_locus,
9275 "prototype declaration");
9280 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9283 /* Otherwise, create a prototype that would match. */
9285 else
9287 tree actual = NULL_TREE, last = NULL_TREE, type;
9289 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9291 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9292 if (last)
9293 TREE_CHAIN (last) = type;
9294 else
9295 actual = type;
9296 last = type;
9298 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9299 if (last)
9300 TREE_CHAIN (last) = type;
9301 else
9302 actual = type;
9304 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9305 of the type of this function, but we need to avoid having this
9306 affect the types of other similarly-typed functions, so we must
9307 first force the generation of an identical (but separate) type
9308 node for the relevant function type. The new node we create
9309 will be a variant of the main variant of the original function
9310 type. */
9312 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9314 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9318 /* Store parameter declarations passed in ARG_INFO into the current
9319 function declaration. */
9321 void
9322 store_parm_decls_from (struct c_arg_info *arg_info)
9324 current_function_arg_info = arg_info;
9325 store_parm_decls ();
9328 /* Called by walk_tree to look for and update context-less labels. */
9330 static tree
9331 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9333 if (TREE_CODE (*tp) == LABEL_EXPR
9334 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9336 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9337 *walk_subtrees = 0;
9340 return NULL_TREE;
9343 /* Store the parameter declarations into the current function declaration.
9344 This is called after parsing the parameter declarations, before
9345 digesting the body of the function.
9347 For an old-style definition, construct a prototype out of the old-style
9348 parameter declarations and inject it into the function's type. */
9350 void
9351 store_parm_decls (void)
9353 tree fndecl = current_function_decl;
9354 bool proto;
9356 /* The argument information block for FNDECL. */
9357 struct c_arg_info *arg_info = current_function_arg_info;
9358 current_function_arg_info = 0;
9360 /* True if this definition is written with a prototype. Note:
9361 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9362 list in a function definition as equivalent to (void) -- an
9363 empty argument list specifies the function has no parameters,
9364 but only (void) sets up a prototype for future calls. */
9365 proto = arg_info->types != 0;
9367 if (proto)
9368 store_parm_decls_newstyle (fndecl, arg_info);
9369 else
9370 store_parm_decls_oldstyle (fndecl, arg_info);
9372 /* The next call to push_scope will be a function body. */
9374 next_is_function_body = true;
9376 /* Write a record describing this function definition to the prototypes
9377 file (if requested). */
9379 gen_aux_info_record (fndecl, 1, 0, proto);
9381 /* Initialize the RTL code for the function. */
9382 allocate_struct_function (fndecl, false);
9384 if (warn_unused_local_typedefs)
9385 cfun->language = ggc_cleared_alloc<language_function> ();
9387 /* Begin the statement tree for this function. */
9388 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9390 /* ??? Insert the contents of the pending sizes list into the function
9391 to be evaluated. The only reason left to have this is
9392 void foo(int n, int array[n++])
9393 because we throw away the array type in favor of a pointer type, and
9394 thus won't naturally see the SAVE_EXPR containing the increment. All
9395 other pending sizes would be handled by gimplify_parameters. */
9396 if (arg_info->pending_sizes)
9398 /* In very special circumstances, e.g. for code like
9399 _Atomic int i = 5;
9400 void f (int a[i += 2]) {}
9401 we need to execute the atomic assignment on function entry.
9402 But in this case, it is not just a straight store, it has the
9403 op= form, which means that build_atomic_assign has generated
9404 gotos, labels, etc. Because at that time the function decl
9405 for F has not been created yet, those labels do not have any
9406 function context. But we have the fndecl now, so update the
9407 labels accordingly. gimplify_expr would crash otherwise. */
9408 walk_tree_without_duplicates (&arg_info->pending_sizes,
9409 set_labels_context_r, fndecl);
9410 add_stmt (arg_info->pending_sizes);
9414 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9415 c_finish_omp_declare_simd for function prototypes. No diagnostics
9416 should be done. */
9418 void
9419 temp_store_parm_decls (tree fndecl, tree parms)
9421 push_scope ();
9422 for (tree p = parms; p; p = DECL_CHAIN (p))
9424 DECL_CONTEXT (p) = fndecl;
9425 if (DECL_NAME (p))
9426 bind (DECL_NAME (p), p, current_scope,
9427 /*invisible=*/false, /*nested=*/false,
9428 UNKNOWN_LOCATION);
9432 /* Undo what temp_store_parm_decls did. */
9434 void
9435 temp_pop_parm_decls (void)
9437 /* Clear all bindings in this temporary scope, so that
9438 pop_scope doesn't create a BLOCK. */
9439 struct c_binding *b = current_scope->bindings;
9440 current_scope->bindings = NULL;
9441 for (; b; b = free_binding_and_advance (b))
9443 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9444 || b->decl == error_mark_node);
9445 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9446 I_SYMBOL_BINDING (b->id) = b->shadowed;
9447 if (b->shadowed && b->shadowed->u.type)
9448 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9450 pop_scope ();
9454 /* Finish up a function declaration and compile that function
9455 all the way to assembler language output. Then free the storage
9456 for the function definition.
9458 This is called after parsing the body of the function definition. */
9460 void
9461 finish_function (void)
9463 tree fndecl = current_function_decl;
9465 if (c_dialect_objc ())
9466 objc_finish_function ();
9468 if (TREE_CODE (fndecl) == FUNCTION_DECL
9469 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9471 tree args = DECL_ARGUMENTS (fndecl);
9472 for (; args; args = DECL_CHAIN (args))
9474 tree type = TREE_TYPE (args);
9475 if (INTEGRAL_TYPE_P (type)
9476 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9477 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9481 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9482 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9484 /* Must mark the RESULT_DECL as being in this function. */
9486 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9487 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9489 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9490 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9491 == integer_type_node && flag_isoc99)
9493 /* Hack. We don't want the middle-end to warn that this return
9494 is unreachable, so we mark its location as special. Using
9495 UNKNOWN_LOCATION has the problem that it gets clobbered in
9496 annotate_one_with_locus. A cleaner solution might be to
9497 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9499 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9502 /* Tie off the statement tree for this function. */
9503 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9505 finish_fname_decls ();
9507 /* Complain if there's no return statement only if option specified on
9508 command line. */
9509 if (warn_return_type > 0
9510 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9511 && !current_function_returns_value && !current_function_returns_null
9512 /* Don't complain if we are no-return. */
9513 && !current_function_returns_abnormally
9514 /* Don't complain if we are declared noreturn. */
9515 && !TREE_THIS_VOLATILE (fndecl)
9516 /* Don't warn for main(). */
9517 && !MAIN_NAME_P (DECL_NAME (fndecl))
9518 /* Or if they didn't actually specify a return type. */
9519 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9520 /* Normally, with -Wreturn-type, flow will complain, but we might
9521 optimize out static functions. */
9522 && !TREE_PUBLIC (fndecl))
9524 warning (OPT_Wreturn_type,
9525 "no return statement in function returning non-void");
9526 TREE_NO_WARNING (fndecl) = 1;
9529 /* Complain about parameters that are only set, but never otherwise used. */
9530 if (warn_unused_but_set_parameter)
9532 tree decl;
9534 for (decl = DECL_ARGUMENTS (fndecl);
9535 decl;
9536 decl = DECL_CHAIN (decl))
9537 if (TREE_USED (decl)
9538 && TREE_CODE (decl) == PARM_DECL
9539 && !DECL_READ_P (decl)
9540 && DECL_NAME (decl)
9541 && !DECL_ARTIFICIAL (decl)
9542 && !TREE_NO_WARNING (decl))
9543 warning_at (DECL_SOURCE_LOCATION (decl),
9544 OPT_Wunused_but_set_parameter,
9545 "parameter %qD set but not used", decl);
9548 /* Complain about locally defined typedefs that are not used in this
9549 function. */
9550 maybe_warn_unused_local_typedefs ();
9552 /* Possibly warn about unused parameters. */
9553 if (warn_unused_parameter)
9554 do_warn_unused_parameter (fndecl);
9556 /* Store the end of the function, so that we get good line number
9557 info for the epilogue. */
9558 cfun->function_end_locus = input_location;
9560 /* Finalize the ELF visibility for the function. */
9561 c_determine_visibility (fndecl);
9563 /* For GNU C extern inline functions disregard inline limits. */
9564 if (DECL_EXTERNAL (fndecl)
9565 && DECL_DECLARED_INLINE_P (fndecl)
9566 && (flag_gnu89_inline
9567 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9568 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9570 /* Genericize before inlining. Delay genericizing nested functions
9571 until their parent function is genericized. Since finalizing
9572 requires GENERIC, delay that as well. */
9574 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9575 && !undef_nested_function)
9577 if (!decl_function_context (fndecl))
9579 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9580 c_genericize (fndecl);
9582 /* ??? Objc emits functions after finalizing the compilation unit.
9583 This should be cleaned up later and this conditional removed. */
9584 if (symtab->global_info_ready)
9586 cgraph_node::add_new_function (fndecl, false);
9587 return;
9589 cgraph_node::finalize_function (fndecl, false);
9591 else
9593 /* Register this function with cgraph just far enough to get it
9594 added to our parent's nested function list. Handy, since the
9595 C front end doesn't have such a list. */
9596 (void) cgraph_node::get_create (fndecl);
9600 if (!decl_function_context (fndecl))
9601 undef_nested_function = false;
9603 if (cfun->language != NULL)
9605 ggc_free (cfun->language);
9606 cfun->language = NULL;
9609 /* We're leaving the context of this function, so zap cfun.
9610 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9611 tree_rest_of_compilation. */
9612 set_cfun (NULL);
9613 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9614 current_function_decl = NULL;
9617 /* Check the declarations given in a for-loop for satisfying the C99
9618 constraints. If exactly one such decl is found, return it. LOC is
9619 the location of the opening parenthesis of the for loop. The last
9620 parameter allows you to control the "for loop initial declarations
9621 are only allowed in C99 mode". Normally, you should pass
9622 flag_isoc99 as that parameter. But in some cases (Objective-C
9623 foreach loop, for example) we want to run the checks in this
9624 function even if not in C99 mode, so we allow the caller to turn
9625 off the error about not being in C99 mode.
9628 tree
9629 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9631 struct c_binding *b;
9632 tree one_decl = NULL_TREE;
9633 int n_decls = 0;
9635 if (!turn_off_iso_c99_error)
9637 static bool hint = true;
9638 /* If we get here, declarations have been used in a for loop without
9639 the C99 for loop scope. This doesn't make much sense, so don't
9640 allow it. */
9641 error_at (loc, "%<for%> loop initial declarations "
9642 "are only allowed in C99 or C11 mode");
9643 if (hint)
9645 inform (loc,
9646 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9647 "to compile your code");
9648 hint = false;
9650 return NULL_TREE;
9652 else
9653 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not support %<for%> loop "
9654 "initial declarations");
9656 /* C99 subclause 6.8.5 paragraph 3:
9658 [#3] The declaration part of a for statement shall only
9659 declare identifiers for objects having storage class auto or
9660 register.
9662 It isn't clear whether, in this sentence, "identifiers" binds to
9663 "shall only declare" or to "objects" - that is, whether all identifiers
9664 declared must be identifiers for objects, or whether the restriction
9665 only applies to those that are. (A question on this in comp.std.c
9666 in November 2000 received no answer.) We implement the strictest
9667 interpretation, to avoid creating an extension which later causes
9668 problems. */
9670 for (b = current_scope->bindings; b; b = b->prev)
9672 tree id = b->id;
9673 tree decl = b->decl;
9675 if (!id)
9676 continue;
9678 switch (TREE_CODE (decl))
9680 case VAR_DECL:
9682 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9683 if (TREE_STATIC (decl))
9684 error_at (decl_loc,
9685 "declaration of static variable %qD in %<for%> loop "
9686 "initial declaration", decl);
9687 else if (DECL_EXTERNAL (decl))
9688 error_at (decl_loc,
9689 "declaration of %<extern%> variable %qD in %<for%> loop "
9690 "initial declaration", decl);
9692 break;
9694 case RECORD_TYPE:
9695 error_at (loc,
9696 "%<struct %E%> declared in %<for%> loop initial "
9697 "declaration", id);
9698 break;
9699 case UNION_TYPE:
9700 error_at (loc,
9701 "%<union %E%> declared in %<for%> loop initial declaration",
9702 id);
9703 break;
9704 case ENUMERAL_TYPE:
9705 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9706 "initial declaration", id);
9707 break;
9708 default:
9709 error_at (loc, "declaration of non-variable "
9710 "%qD in %<for%> loop initial declaration", decl);
9713 n_decls++;
9714 one_decl = decl;
9717 return n_decls == 1 ? one_decl : NULL_TREE;
9720 /* Save and reinitialize the variables
9721 used during compilation of a C function. */
9723 void
9724 c_push_function_context (void)
9726 struct language_function *p = cfun->language;
9727 /* cfun->language might have been already allocated by the use of
9728 -Wunused-local-typedefs. In that case, just re-use it. */
9729 if (p == NULL)
9730 cfun->language = p = ggc_cleared_alloc<language_function> ();
9732 p->base.x_stmt_tree = c_stmt_tree;
9733 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9734 p->x_break_label = c_break_label;
9735 p->x_cont_label = c_cont_label;
9736 p->x_switch_stack = c_switch_stack;
9737 p->arg_info = current_function_arg_info;
9738 p->returns_value = current_function_returns_value;
9739 p->returns_null = current_function_returns_null;
9740 p->returns_abnormally = current_function_returns_abnormally;
9741 p->warn_about_return_type = warn_about_return_type;
9743 push_function_context ();
9746 /* Restore the variables used during compilation of a C function. */
9748 void
9749 c_pop_function_context (void)
9751 struct language_function *p;
9753 pop_function_context ();
9754 p = cfun->language;
9756 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9757 used to store data throughout the life time of the current cfun,
9758 So don't deallocate it. */
9759 if (!warn_unused_local_typedefs)
9760 cfun->language = NULL;
9762 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9763 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9765 /* Stop pointing to the local nodes about to be freed. */
9766 /* But DECL_INITIAL must remain nonzero so we know this
9767 was an actual function definition. */
9768 DECL_INITIAL (current_function_decl) = error_mark_node;
9769 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9772 c_stmt_tree = p->base.x_stmt_tree;
9773 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9774 c_break_label = p->x_break_label;
9775 c_cont_label = p->x_cont_label;
9776 c_switch_stack = p->x_switch_stack;
9777 current_function_arg_info = p->arg_info;
9778 current_function_returns_value = p->returns_value;
9779 current_function_returns_null = p->returns_null;
9780 current_function_returns_abnormally = p->returns_abnormally;
9781 warn_about_return_type = p->warn_about_return_type;
9784 /* The functions below are required for functionality of doing
9785 function at once processing in the C front end. Currently these
9786 functions are not called from anywhere in the C front end, but as
9787 these changes continue, that will change. */
9789 /* Returns the stmt_tree (if any) to which statements are currently
9790 being added. If there is no active statement-tree, NULL is
9791 returned. */
9793 stmt_tree
9794 current_stmt_tree (void)
9796 return &c_stmt_tree;
9799 /* Return the global value of T as a symbol. */
9801 tree
9802 identifier_global_value (tree t)
9804 struct c_binding *b;
9806 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9807 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9808 return b->decl;
9810 return NULL_TREE;
9813 /* In C, the only C-linkage public declaration is at file scope. */
9815 tree
9816 c_linkage_bindings (tree name)
9818 return identifier_global_value (name);
9821 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9822 otherwise the name is found in ridpointers from RID_INDEX. */
9824 void
9825 record_builtin_type (enum rid rid_index, const char *name, tree type)
9827 tree id, decl;
9828 if (name == 0)
9829 id = ridpointers[(int) rid_index];
9830 else
9831 id = get_identifier (name);
9832 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9833 pushdecl (decl);
9834 if (debug_hooks->type_decl)
9835 debug_hooks->type_decl (decl, false);
9838 /* Build the void_list_node (void_type_node having been created). */
9839 tree
9840 build_void_list_node (void)
9842 tree t = build_tree_list (NULL_TREE, void_type_node);
9843 return t;
9846 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9848 struct c_parm *
9849 build_c_parm (struct c_declspecs *specs, tree attrs,
9850 struct c_declarator *declarator,
9851 location_t loc)
9853 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9854 ret->specs = specs;
9855 ret->attrs = attrs;
9856 ret->declarator = declarator;
9857 ret->loc = loc;
9858 return ret;
9861 /* Return a declarator with nested attributes. TARGET is the inner
9862 declarator to which these attributes apply. ATTRS are the
9863 attributes. */
9865 struct c_declarator *
9866 build_attrs_declarator (tree attrs, struct c_declarator *target)
9868 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9869 ret->kind = cdk_attrs;
9870 ret->declarator = target;
9871 ret->u.attrs = attrs;
9872 return ret;
9875 /* Return a declarator for a function with arguments specified by ARGS
9876 and return type specified by TARGET. */
9878 struct c_declarator *
9879 build_function_declarator (struct c_arg_info *args,
9880 struct c_declarator *target)
9882 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9883 ret->kind = cdk_function;
9884 ret->declarator = target;
9885 ret->u.arg_info = args;
9886 return ret;
9889 /* Return a declarator for the identifier IDENT (which may be
9890 NULL_TREE for an abstract declarator). */
9892 struct c_declarator *
9893 build_id_declarator (tree ident)
9895 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9896 ret->kind = cdk_id;
9897 ret->declarator = 0;
9898 ret->u.id = ident;
9899 /* Default value - may get reset to a more precise location. */
9900 ret->id_loc = input_location;
9901 return ret;
9904 /* Return something to represent absolute declarators containing a *.
9905 TARGET is the absolute declarator that the * contains.
9906 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9907 to apply to the pointer type. */
9909 struct c_declarator *
9910 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9911 struct c_declarator *target)
9913 tree attrs;
9914 int quals = 0;
9915 struct c_declarator *itarget = target;
9916 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9917 if (type_quals_attrs)
9919 attrs = type_quals_attrs->attrs;
9920 quals = quals_from_declspecs (type_quals_attrs);
9921 if (attrs != NULL_TREE)
9922 itarget = build_attrs_declarator (attrs, target);
9924 ret->kind = cdk_pointer;
9925 ret->declarator = itarget;
9926 ret->u.pointer_quals = quals;
9927 return ret;
9930 /* Return a pointer to a structure for an empty list of declaration
9931 specifiers. */
9933 struct c_declspecs *
9934 build_null_declspecs (void)
9936 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9937 memset (ret, 0, sizeof *ret);
9938 ret->align_log = -1;
9939 ret->typespec_word = cts_none;
9940 ret->storage_class = csc_none;
9941 ret->expr_const_operands = true;
9942 ret->typespec_kind = ctsk_none;
9943 ret->address_space = ADDR_SPACE_GENERIC;
9944 return ret;
9947 /* Add the address space ADDRSPACE to the declaration specifiers
9948 SPECS, returning SPECS. */
9950 struct c_declspecs *
9951 declspecs_add_addrspace (source_location location,
9952 struct c_declspecs *specs, addr_space_t as)
9954 specs->non_sc_seen_p = true;
9955 specs->declspecs_seen_p = true;
9957 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9958 && specs->address_space != as)
9959 error ("incompatible address space qualifiers %qs and %qs",
9960 c_addr_space_name (as),
9961 c_addr_space_name (specs->address_space));
9962 else
9964 specs->address_space = as;
9965 specs->locations[cdw_address_space] = location;
9967 return specs;
9970 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9971 returning SPECS. */
9973 struct c_declspecs *
9974 declspecs_add_qual (source_location loc,
9975 struct c_declspecs *specs, tree qual)
9977 enum rid i;
9978 bool dupe = false;
9979 specs->non_sc_seen_p = true;
9980 specs->declspecs_seen_p = true;
9981 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9982 && C_IS_RESERVED_WORD (qual));
9983 i = C_RID_CODE (qual);
9984 location_t prev_loc = UNKNOWN_LOCATION;
9985 switch (i)
9987 case RID_CONST:
9988 dupe = specs->const_p;
9989 specs->const_p = true;
9990 prev_loc = specs->locations[cdw_const];
9991 specs->locations[cdw_const] = loc;
9992 break;
9993 case RID_VOLATILE:
9994 dupe = specs->volatile_p;
9995 specs->volatile_p = true;
9996 prev_loc = specs->locations[cdw_volatile];
9997 specs->locations[cdw_volatile] = loc;
9998 break;
9999 case RID_RESTRICT:
10000 dupe = specs->restrict_p;
10001 specs->restrict_p = true;
10002 prev_loc = specs->locations[cdw_restrict];
10003 specs->locations[cdw_restrict] = loc;
10004 break;
10005 case RID_ATOMIC:
10006 dupe = specs->atomic_p;
10007 specs->atomic_p = true;
10008 prev_loc = specs->locations[cdw_atomic];
10009 specs->locations[cdw_atomic] = loc;
10010 break;
10011 default:
10012 gcc_unreachable ();
10014 if (dupe)
10016 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
10017 "duplicate %qE declaration specifier", qual);
10018 if (!warned
10019 && warn_duplicate_decl_specifier
10020 && prev_loc >= RESERVED_LOCATION_COUNT
10021 && !from_macro_expansion_at (prev_loc)
10022 && !from_macro_expansion_at (loc))
10023 warning_at (loc, OPT_Wduplicate_decl_specifier,
10024 "duplicate %qE declaration specifier", qual);
10026 return specs;
10029 /* Add the type specifier TYPE to the declaration specifiers SPECS,
10030 returning SPECS. */
10032 struct c_declspecs *
10033 declspecs_add_type (location_t loc, struct c_declspecs *specs,
10034 struct c_typespec spec)
10036 tree type = spec.spec;
10037 specs->non_sc_seen_p = true;
10038 specs->declspecs_seen_p = true;
10039 specs->typespec_kind = spec.kind;
10040 if (TREE_DEPRECATED (type))
10041 specs->deprecated_p = true;
10043 /* Handle type specifier keywords. */
10044 if (TREE_CODE (type) == IDENTIFIER_NODE
10045 && C_IS_RESERVED_WORD (type)
10046 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
10048 enum rid i = C_RID_CODE (type);
10049 if (specs->type)
10051 error_at (loc, "two or more data types in declaration specifiers");
10052 return specs;
10054 if ((int) i <= (int) RID_LAST_MODIFIER)
10056 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
10057 bool dupe = false;
10058 switch (i)
10060 case RID_LONG:
10061 if (specs->long_long_p)
10063 error_at (loc, "%<long long long%> is too long for GCC");
10064 break;
10066 if (specs->long_p)
10068 if (specs->typespec_word == cts_double)
10070 error_at (loc,
10071 ("both %<long long%> and %<double%> in "
10072 "declaration specifiers"));
10073 break;
10075 pedwarn_c90 (loc, OPT_Wlong_long,
10076 "ISO C90 does not support %<long long%>");
10077 specs->long_long_p = 1;
10078 specs->locations[cdw_long_long] = loc;
10079 break;
10081 if (specs->short_p)
10082 error_at (loc,
10083 ("both %<long%> and %<short%> in "
10084 "declaration specifiers"));
10085 else if (specs->typespec_word == cts_auto_type)
10086 error_at (loc,
10087 ("both %<long%> and %<__auto_type%> in "
10088 "declaration specifiers"));
10089 else if (specs->typespec_word == cts_void)
10090 error_at (loc,
10091 ("both %<long%> and %<void%> in "
10092 "declaration specifiers"));
10093 else if (specs->typespec_word == cts_int_n)
10094 error_at (loc,
10095 ("both %<long%> and %<__int%d%> in "
10096 "declaration specifiers"),
10097 int_n_data[specs->int_n_idx].bitsize);
10098 else if (specs->typespec_word == cts_bool)
10099 error_at (loc,
10100 ("both %<long%> and %<_Bool%> in "
10101 "declaration specifiers"));
10102 else if (specs->typespec_word == cts_char)
10103 error_at (loc,
10104 ("both %<long%> and %<char%> in "
10105 "declaration specifiers"));
10106 else if (specs->typespec_word == cts_float)
10107 error_at (loc,
10108 ("both %<long%> and %<float%> in "
10109 "declaration specifiers"));
10110 else if (specs->typespec_word == cts_floatn_nx)
10111 error_at (loc,
10112 ("both %<long%> and %<_Float%d%s%> in "
10113 "declaration specifiers"),
10114 floatn_nx_types[specs->floatn_nx_idx].n,
10115 (floatn_nx_types[specs->floatn_nx_idx].extended
10116 ? "x"
10117 : ""));
10118 else if (specs->typespec_word == cts_dfloat32)
10119 error_at (loc,
10120 ("both %<long%> and %<_Decimal32%> in "
10121 "declaration specifiers"));
10122 else if (specs->typespec_word == cts_dfloat64)
10123 error_at (loc,
10124 ("both %<long%> and %<_Decimal64%> in "
10125 "declaration specifiers"));
10126 else if (specs->typespec_word == cts_dfloat128)
10127 error_at (loc,
10128 ("both %<long%> and %<_Decimal128%> in "
10129 "declaration specifiers"));
10130 else
10132 specs->long_p = true;
10133 specs->locations[cdw_long] = loc;
10135 break;
10136 case RID_SHORT:
10137 dupe = specs->short_p;
10138 if (specs->long_p)
10139 error_at (loc,
10140 ("both %<long%> and %<short%> in "
10141 "declaration specifiers"));
10142 else if (specs->typespec_word == cts_auto_type)
10143 error_at (loc,
10144 ("both %<short%> and %<__auto_type%> in "
10145 "declaration specifiers"));
10146 else if (specs->typespec_word == cts_void)
10147 error_at (loc,
10148 ("both %<short%> and %<void%> in "
10149 "declaration specifiers"));
10150 else if (specs->typespec_word == cts_int_n)
10151 error_at (loc,
10152 ("both %<short%> and %<__int%d%> in "
10153 "declaration specifiers"),
10154 int_n_data[specs->int_n_idx].bitsize);
10155 else if (specs->typespec_word == cts_bool)
10156 error_at (loc,
10157 ("both %<short%> and %<_Bool%> in "
10158 "declaration specifiers"));
10159 else if (specs->typespec_word == cts_char)
10160 error_at (loc,
10161 ("both %<short%> and %<char%> in "
10162 "declaration specifiers"));
10163 else if (specs->typespec_word == cts_float)
10164 error_at (loc,
10165 ("both %<short%> and %<float%> in "
10166 "declaration specifiers"));
10167 else if (specs->typespec_word == cts_double)
10168 error_at (loc,
10169 ("both %<short%> and %<double%> in "
10170 "declaration specifiers"));
10171 else if (specs->typespec_word == cts_floatn_nx)
10172 error_at (loc,
10173 ("both %<short%> and %<_Float%d%s%> in "
10174 "declaration specifiers"),
10175 floatn_nx_types[specs->floatn_nx_idx].n,
10176 (floatn_nx_types[specs->floatn_nx_idx].extended
10177 ? "x"
10178 : ""));
10179 else if (specs->typespec_word == cts_dfloat32)
10180 error_at (loc,
10181 ("both %<short%> and %<_Decimal32%> in "
10182 "declaration specifiers"));
10183 else if (specs->typespec_word == cts_dfloat64)
10184 error_at (loc,
10185 ("both %<short%> and %<_Decimal64%> in "
10186 "declaration specifiers"));
10187 else if (specs->typespec_word == cts_dfloat128)
10188 error_at (loc,
10189 ("both %<short%> and %<_Decimal128%> in "
10190 "declaration specifiers"));
10191 else
10193 specs->short_p = true;
10194 specs->locations[cdw_short] = loc;
10196 break;
10197 case RID_SIGNED:
10198 dupe = specs->signed_p;
10199 if (specs->unsigned_p)
10200 error_at (loc,
10201 ("both %<signed%> and %<unsigned%> in "
10202 "declaration specifiers"));
10203 else if (specs->typespec_word == cts_auto_type)
10204 error_at (loc,
10205 ("both %<signed%> and %<__auto_type%> in "
10206 "declaration specifiers"));
10207 else if (specs->typespec_word == cts_void)
10208 error_at (loc,
10209 ("both %<signed%> and %<void%> in "
10210 "declaration specifiers"));
10211 else if (specs->typespec_word == cts_bool)
10212 error_at (loc,
10213 ("both %<signed%> and %<_Bool%> in "
10214 "declaration specifiers"));
10215 else if (specs->typespec_word == cts_float)
10216 error_at (loc,
10217 ("both %<signed%> and %<float%> in "
10218 "declaration specifiers"));
10219 else if (specs->typespec_word == cts_double)
10220 error_at (loc,
10221 ("both %<signed%> and %<double%> in "
10222 "declaration specifiers"));
10223 else if (specs->typespec_word == cts_floatn_nx)
10224 error_at (loc,
10225 ("both %<signed%> and %<_Float%d%s%> in "
10226 "declaration specifiers"),
10227 floatn_nx_types[specs->floatn_nx_idx].n,
10228 (floatn_nx_types[specs->floatn_nx_idx].extended
10229 ? "x"
10230 : ""));
10231 else if (specs->typespec_word == cts_dfloat32)
10232 error_at (loc,
10233 ("both %<signed%> and %<_Decimal32%> in "
10234 "declaration specifiers"));
10235 else if (specs->typespec_word == cts_dfloat64)
10236 error_at (loc,
10237 ("both %<signed%> and %<_Decimal64%> in "
10238 "declaration specifiers"));
10239 else if (specs->typespec_word == cts_dfloat128)
10240 error_at (loc,
10241 ("both %<signed%> and %<_Decimal128%> in "
10242 "declaration specifiers"));
10243 else
10245 specs->signed_p = true;
10246 specs->locations[cdw_signed] = loc;
10248 break;
10249 case RID_UNSIGNED:
10250 dupe = specs->unsigned_p;
10251 if (specs->signed_p)
10252 error_at (loc,
10253 ("both %<signed%> and %<unsigned%> in "
10254 "declaration specifiers"));
10255 else if (specs->typespec_word == cts_auto_type)
10256 error_at (loc,
10257 ("both %<unsigned%> and %<__auto_type%> in "
10258 "declaration specifiers"));
10259 else if (specs->typespec_word == cts_void)
10260 error_at (loc,
10261 ("both %<unsigned%> and %<void%> in "
10262 "declaration specifiers"));
10263 else if (specs->typespec_word == cts_bool)
10264 error_at (loc,
10265 ("both %<unsigned%> and %<_Bool%> in "
10266 "declaration specifiers"));
10267 else if (specs->typespec_word == cts_float)
10268 error_at (loc,
10269 ("both %<unsigned%> and %<float%> in "
10270 "declaration specifiers"));
10271 else if (specs->typespec_word == cts_double)
10272 error_at (loc,
10273 ("both %<unsigned%> and %<double%> in "
10274 "declaration specifiers"));
10275 else if (specs->typespec_word == cts_floatn_nx)
10276 error_at (loc,
10277 ("both %<unsigned%> and %<_Float%d%s%> in "
10278 "declaration specifiers"),
10279 floatn_nx_types[specs->floatn_nx_idx].n,
10280 (floatn_nx_types[specs->floatn_nx_idx].extended
10281 ? "x"
10282 : ""));
10283 else if (specs->typespec_word == cts_dfloat32)
10284 error_at (loc,
10285 ("both %<unsigned%> and %<_Decimal32%> in "
10286 "declaration specifiers"));
10287 else if (specs->typespec_word == cts_dfloat64)
10288 error_at (loc,
10289 ("both %<unsigned%> and %<_Decimal64%> in "
10290 "declaration specifiers"));
10291 else if (specs->typespec_word == cts_dfloat128)
10292 error_at (loc,
10293 ("both %<unsigned%> and %<_Decimal128%> in "
10294 "declaration specifiers"));
10295 else
10297 specs->unsigned_p = true;
10298 specs->locations[cdw_unsigned] = loc;
10300 break;
10301 case RID_COMPLEX:
10302 dupe = specs->complex_p;
10303 if (!in_system_header_at (loc))
10304 pedwarn_c90 (loc, OPT_Wpedantic,
10305 "ISO C90 does not support complex types");
10306 if (specs->typespec_word == cts_auto_type)
10307 error_at (loc,
10308 ("both %<complex%> and %<__auto_type%> in "
10309 "declaration specifiers"));
10310 else if (specs->typespec_word == cts_void)
10311 error_at (loc,
10312 ("both %<complex%> and %<void%> in "
10313 "declaration specifiers"));
10314 else if (specs->typespec_word == cts_bool)
10315 error_at (loc,
10316 ("both %<complex%> and %<_Bool%> in "
10317 "declaration specifiers"));
10318 else if (specs->typespec_word == cts_dfloat32)
10319 error_at (loc,
10320 ("both %<complex%> and %<_Decimal32%> in "
10321 "declaration specifiers"));
10322 else if (specs->typespec_word == cts_dfloat64)
10323 error_at (loc,
10324 ("both %<complex%> and %<_Decimal64%> in "
10325 "declaration specifiers"));
10326 else if (specs->typespec_word == cts_dfloat128)
10327 error_at (loc,
10328 ("both %<complex%> and %<_Decimal128%> in "
10329 "declaration specifiers"));
10330 else if (specs->typespec_word == cts_fract)
10331 error_at (loc,
10332 ("both %<complex%> and %<_Fract%> in "
10333 "declaration specifiers"));
10334 else if (specs->typespec_word == cts_accum)
10335 error_at (loc,
10336 ("both %<complex%> and %<_Accum%> in "
10337 "declaration specifiers"));
10338 else if (specs->saturating_p)
10339 error_at (loc,
10340 ("both %<complex%> and %<_Sat%> in "
10341 "declaration specifiers"));
10342 else
10344 specs->complex_p = true;
10345 specs->locations[cdw_complex] = loc;
10347 break;
10348 case RID_SAT:
10349 dupe = specs->saturating_p;
10350 pedwarn (loc, OPT_Wpedantic,
10351 "ISO C does not support saturating types");
10352 if (specs->typespec_word == cts_int_n)
10354 error_at (loc,
10355 ("both %<_Sat%> and %<__int%d%> in "
10356 "declaration specifiers"),
10357 int_n_data[specs->int_n_idx].bitsize);
10359 else if (specs->typespec_word == cts_auto_type)
10360 error_at (loc,
10361 ("both %<_Sat%> and %<__auto_type%> in "
10362 "declaration specifiers"));
10363 else if (specs->typespec_word == cts_void)
10364 error_at (loc,
10365 ("both %<_Sat%> and %<void%> in "
10366 "declaration specifiers"));
10367 else if (specs->typespec_word == cts_bool)
10368 error_at (loc,
10369 ("both %<_Sat%> and %<_Bool%> in "
10370 "declaration specifiers"));
10371 else if (specs->typespec_word == cts_char)
10372 error_at (loc,
10373 ("both %<_Sat%> and %<char%> in "
10374 "declaration specifiers"));
10375 else if (specs->typespec_word == cts_int)
10376 error_at (loc,
10377 ("both %<_Sat%> and %<int%> in "
10378 "declaration specifiers"));
10379 else if (specs->typespec_word == cts_float)
10380 error_at (loc,
10381 ("both %<_Sat%> and %<float%> in "
10382 "declaration specifiers"));
10383 else if (specs->typespec_word == cts_double)
10384 error_at (loc,
10385 ("both %<_Sat%> and %<double%> in "
10386 "declaration specifiers"));
10387 else if (specs->typespec_word == cts_floatn_nx)
10388 error_at (loc,
10389 ("both %<_Sat%> and %<_Float%d%s%> in "
10390 "declaration specifiers"),
10391 floatn_nx_types[specs->floatn_nx_idx].n,
10392 (floatn_nx_types[specs->floatn_nx_idx].extended
10393 ? "x"
10394 : ""));
10395 else if (specs->typespec_word == cts_dfloat32)
10396 error_at (loc,
10397 ("both %<_Sat%> and %<_Decimal32%> in "
10398 "declaration specifiers"));
10399 else if (specs->typespec_word == cts_dfloat64)
10400 error_at (loc,
10401 ("both %<_Sat%> and %<_Decimal64%> in "
10402 "declaration specifiers"));
10403 else if (specs->typespec_word == cts_dfloat128)
10404 error_at (loc,
10405 ("both %<_Sat%> and %<_Decimal128%> in "
10406 "declaration specifiers"));
10407 else if (specs->complex_p)
10408 error_at (loc,
10409 ("both %<_Sat%> and %<complex%> in "
10410 "declaration specifiers"));
10411 else
10413 specs->saturating_p = true;
10414 specs->locations[cdw_saturating] = loc;
10416 break;
10417 default:
10418 gcc_unreachable ();
10421 if (dupe)
10422 error_at (loc, "duplicate %qE", type);
10424 return specs;
10426 else
10428 /* "void", "_Bool", "char", "int", "float", "double",
10429 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10430 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10431 "__auto_type". */
10432 if (specs->typespec_word != cts_none)
10434 error_at (loc,
10435 "two or more data types in declaration specifiers");
10436 return specs;
10438 switch (i)
10440 case RID_AUTO_TYPE:
10441 if (specs->long_p)
10442 error_at (loc,
10443 ("both %<long%> and %<__auto_type%> in "
10444 "declaration specifiers"));
10445 else if (specs->short_p)
10446 error_at (loc,
10447 ("both %<short%> and %<__auto_type%> in "
10448 "declaration specifiers"));
10449 else if (specs->signed_p)
10450 error_at (loc,
10451 ("both %<signed%> and %<__auto_type%> in "
10452 "declaration specifiers"));
10453 else if (specs->unsigned_p)
10454 error_at (loc,
10455 ("both %<unsigned%> and %<__auto_type%> in "
10456 "declaration specifiers"));
10457 else if (specs->complex_p)
10458 error_at (loc,
10459 ("both %<complex%> and %<__auto_type%> in "
10460 "declaration specifiers"));
10461 else if (specs->saturating_p)
10462 error_at (loc,
10463 ("both %<_Sat%> and %<__auto_type%> in "
10464 "declaration specifiers"));
10465 else
10467 specs->typespec_word = cts_auto_type;
10468 specs->locations[cdw_typespec] = loc;
10470 return specs;
10471 case RID_INT_N_0:
10472 case RID_INT_N_1:
10473 case RID_INT_N_2:
10474 case RID_INT_N_3:
10475 specs->int_n_idx = i - RID_INT_N_0;
10476 if (!in_system_header_at (input_location))
10477 pedwarn (loc, OPT_Wpedantic,
10478 "ISO C does not support %<__int%d%> types",
10479 int_n_data[specs->int_n_idx].bitsize);
10481 if (specs->long_p)
10482 error_at (loc,
10483 ("both %<__int%d%> and %<long%> in "
10484 "declaration specifiers"),
10485 int_n_data[specs->int_n_idx].bitsize);
10486 else if (specs->saturating_p)
10487 error_at (loc,
10488 ("both %<_Sat%> and %<__int%d%> in "
10489 "declaration specifiers"),
10490 int_n_data[specs->int_n_idx].bitsize);
10491 else if (specs->short_p)
10492 error_at (loc,
10493 ("both %<__int%d%> and %<short%> in "
10494 "declaration specifiers"),
10495 int_n_data[specs->int_n_idx].bitsize);
10496 else if (! int_n_enabled_p[specs->int_n_idx])
10498 specs->typespec_word = cts_int_n;
10499 error_at (loc,
10500 "%<__int%d%> is not supported on this target",
10501 int_n_data[specs->int_n_idx].bitsize);
10503 else
10505 specs->typespec_word = cts_int_n;
10506 specs->locations[cdw_typespec] = loc;
10508 return specs;
10509 case RID_VOID:
10510 if (specs->long_p)
10511 error_at (loc,
10512 ("both %<long%> and %<void%> in "
10513 "declaration specifiers"));
10514 else if (specs->short_p)
10515 error_at (loc,
10516 ("both %<short%> and %<void%> in "
10517 "declaration specifiers"));
10518 else if (specs->signed_p)
10519 error_at (loc,
10520 ("both %<signed%> and %<void%> in "
10521 "declaration specifiers"));
10522 else if (specs->unsigned_p)
10523 error_at (loc,
10524 ("both %<unsigned%> and %<void%> in "
10525 "declaration specifiers"));
10526 else if (specs->complex_p)
10527 error_at (loc,
10528 ("both %<complex%> and %<void%> in "
10529 "declaration specifiers"));
10530 else if (specs->saturating_p)
10531 error_at (loc,
10532 ("both %<_Sat%> and %<void%> in "
10533 "declaration specifiers"));
10534 else
10536 specs->typespec_word = cts_void;
10537 specs->locations[cdw_typespec] = loc;
10539 return specs;
10540 case RID_BOOL:
10541 if (!in_system_header_at (loc))
10542 pedwarn_c90 (loc, OPT_Wpedantic,
10543 "ISO C90 does not support boolean types");
10544 if (specs->long_p)
10545 error_at (loc,
10546 ("both %<long%> and %<_Bool%> in "
10547 "declaration specifiers"));
10548 else if (specs->short_p)
10549 error_at (loc,
10550 ("both %<short%> and %<_Bool%> in "
10551 "declaration specifiers"));
10552 else if (specs->signed_p)
10553 error_at (loc,
10554 ("both %<signed%> and %<_Bool%> in "
10555 "declaration specifiers"));
10556 else if (specs->unsigned_p)
10557 error_at (loc,
10558 ("both %<unsigned%> and %<_Bool%> in "
10559 "declaration specifiers"));
10560 else if (specs->complex_p)
10561 error_at (loc,
10562 ("both %<complex%> and %<_Bool%> in "
10563 "declaration specifiers"));
10564 else if (specs->saturating_p)
10565 error_at (loc,
10566 ("both %<_Sat%> and %<_Bool%> in "
10567 "declaration specifiers"));
10568 else
10570 specs->typespec_word = cts_bool;
10571 specs->locations[cdw_typespec] = loc;
10573 return specs;
10574 case RID_CHAR:
10575 if (specs->long_p)
10576 error_at (loc,
10577 ("both %<long%> and %<char%> in "
10578 "declaration specifiers"));
10579 else if (specs->short_p)
10580 error_at (loc,
10581 ("both %<short%> and %<char%> in "
10582 "declaration specifiers"));
10583 else if (specs->saturating_p)
10584 error_at (loc,
10585 ("both %<_Sat%> and %<char%> in "
10586 "declaration specifiers"));
10587 else
10589 specs->typespec_word = cts_char;
10590 specs->locations[cdw_typespec] = loc;
10592 return specs;
10593 case RID_INT:
10594 if (specs->saturating_p)
10595 error_at (loc,
10596 ("both %<_Sat%> and %<int%> in "
10597 "declaration specifiers"));
10598 else
10600 specs->typespec_word = cts_int;
10601 specs->locations[cdw_typespec] = loc;
10603 return specs;
10604 case RID_FLOAT:
10605 if (specs->long_p)
10606 error_at (loc,
10607 ("both %<long%> and %<float%> in "
10608 "declaration specifiers"));
10609 else if (specs->short_p)
10610 error_at (loc,
10611 ("both %<short%> and %<float%> in "
10612 "declaration specifiers"));
10613 else if (specs->signed_p)
10614 error_at (loc,
10615 ("both %<signed%> and %<float%> in "
10616 "declaration specifiers"));
10617 else if (specs->unsigned_p)
10618 error_at (loc,
10619 ("both %<unsigned%> and %<float%> in "
10620 "declaration specifiers"));
10621 else if (specs->saturating_p)
10622 error_at (loc,
10623 ("both %<_Sat%> and %<float%> in "
10624 "declaration specifiers"));
10625 else
10627 specs->typespec_word = cts_float;
10628 specs->locations[cdw_typespec] = loc;
10630 return specs;
10631 case RID_DOUBLE:
10632 if (specs->long_long_p)
10633 error_at (loc,
10634 ("both %<long long%> and %<double%> in "
10635 "declaration specifiers"));
10636 else if (specs->short_p)
10637 error_at (loc,
10638 ("both %<short%> and %<double%> in "
10639 "declaration specifiers"));
10640 else if (specs->signed_p)
10641 error_at (loc,
10642 ("both %<signed%> and %<double%> in "
10643 "declaration specifiers"));
10644 else if (specs->unsigned_p)
10645 error_at (loc,
10646 ("both %<unsigned%> and %<double%> in "
10647 "declaration specifiers"));
10648 else if (specs->saturating_p)
10649 error_at (loc,
10650 ("both %<_Sat%> and %<double%> in "
10651 "declaration specifiers"));
10652 else
10654 specs->typespec_word = cts_double;
10655 specs->locations[cdw_typespec] = loc;
10657 return specs;
10658 CASE_RID_FLOATN_NX:
10659 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10660 if (!in_system_header_at (input_location))
10661 pedwarn (loc, OPT_Wpedantic,
10662 "ISO C does not support the %<_Float%d%s%> type",
10663 floatn_nx_types[specs->floatn_nx_idx].n,
10664 (floatn_nx_types[specs->floatn_nx_idx].extended
10665 ? "x"
10666 : ""));
10668 if (specs->long_p)
10669 error_at (loc,
10670 ("both %<long%> and %<_Float%d%s%> in "
10671 "declaration specifiers"),
10672 floatn_nx_types[specs->floatn_nx_idx].n,
10673 (floatn_nx_types[specs->floatn_nx_idx].extended
10674 ? "x"
10675 : ""));
10676 else if (specs->short_p)
10677 error_at (loc,
10678 ("both %<short%> and %<_Float%d%s%> in "
10679 "declaration specifiers"),
10680 floatn_nx_types[specs->floatn_nx_idx].n,
10681 (floatn_nx_types[specs->floatn_nx_idx].extended
10682 ? "x"
10683 : ""));
10684 else if (specs->signed_p)
10685 error_at (loc,
10686 ("both %<signed%> and %<_Float%d%s%> in "
10687 "declaration specifiers"),
10688 floatn_nx_types[specs->floatn_nx_idx].n,
10689 (floatn_nx_types[specs->floatn_nx_idx].extended
10690 ? "x"
10691 : ""));
10692 else if (specs->unsigned_p)
10693 error_at (loc,
10694 ("both %<unsigned%> and %<_Float%d%s%> in "
10695 "declaration specifiers"),
10696 floatn_nx_types[specs->floatn_nx_idx].n,
10697 (floatn_nx_types[specs->floatn_nx_idx].extended
10698 ? "x"
10699 : ""));
10700 else if (specs->saturating_p)
10701 error_at (loc,
10702 ("both %<_Sat%> and %<_Float%d%s%> in "
10703 "declaration specifiers"),
10704 floatn_nx_types[specs->floatn_nx_idx].n,
10705 (floatn_nx_types[specs->floatn_nx_idx].extended
10706 ? "x"
10707 : ""));
10708 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10710 specs->typespec_word = cts_floatn_nx;
10711 error_at (loc,
10712 "%<_Float%d%s%> is not supported on this target",
10713 floatn_nx_types[specs->floatn_nx_idx].n,
10714 (floatn_nx_types[specs->floatn_nx_idx].extended
10715 ? "x"
10716 : ""));
10718 else
10720 specs->typespec_word = cts_floatn_nx;
10721 specs->locations[cdw_typespec] = loc;
10723 return specs;
10724 case RID_DFLOAT32:
10725 case RID_DFLOAT64:
10726 case RID_DFLOAT128:
10728 const char *str;
10729 if (i == RID_DFLOAT32)
10730 str = "_Decimal32";
10731 else if (i == RID_DFLOAT64)
10732 str = "_Decimal64";
10733 else
10734 str = "_Decimal128";
10735 if (specs->long_long_p)
10736 error_at (loc,
10737 ("both %<long long%> and %qs in "
10738 "declaration specifiers"),
10739 str);
10740 if (specs->long_p)
10741 error_at (loc,
10742 ("both %<long%> and %qs in "
10743 "declaration specifiers"),
10744 str);
10745 else if (specs->short_p)
10746 error_at (loc,
10747 ("both %<short%> and %qs in "
10748 "declaration specifiers"),
10749 str);
10750 else if (specs->signed_p)
10751 error_at (loc,
10752 ("both %<signed%> and %qs in "
10753 "declaration specifiers"),
10754 str);
10755 else if (specs->unsigned_p)
10756 error_at (loc,
10757 ("both %<unsigned%> and %qs in "
10758 "declaration specifiers"),
10759 str);
10760 else if (specs->complex_p)
10761 error_at (loc,
10762 ("both %<complex%> and %qs in "
10763 "declaration specifiers"),
10764 str);
10765 else if (specs->saturating_p)
10766 error_at (loc,
10767 ("both %<_Sat%> and %qs in "
10768 "declaration specifiers"),
10769 str);
10770 else if (i == RID_DFLOAT32)
10771 specs->typespec_word = cts_dfloat32;
10772 else if (i == RID_DFLOAT64)
10773 specs->typespec_word = cts_dfloat64;
10774 else
10775 specs->typespec_word = cts_dfloat128;
10776 specs->locations[cdw_typespec] = loc;
10778 if (!targetm.decimal_float_supported_p ())
10779 error_at (loc,
10780 ("decimal floating point not supported "
10781 "for this target"));
10782 pedwarn (loc, OPT_Wpedantic,
10783 "ISO C does not support decimal floating point");
10784 return specs;
10785 case RID_FRACT:
10786 case RID_ACCUM:
10788 const char *str;
10789 if (i == RID_FRACT)
10790 str = "_Fract";
10791 else
10792 str = "_Accum";
10793 if (specs->complex_p)
10794 error_at (loc,
10795 ("both %<complex%> and %qs in "
10796 "declaration specifiers"),
10797 str);
10798 else if (i == RID_FRACT)
10799 specs->typespec_word = cts_fract;
10800 else
10801 specs->typespec_word = cts_accum;
10802 specs->locations[cdw_typespec] = loc;
10804 if (!targetm.fixed_point_supported_p ())
10805 error_at (loc,
10806 "fixed-point types not supported for this target");
10807 pedwarn (loc, OPT_Wpedantic,
10808 "ISO C does not support fixed-point types");
10809 return specs;
10810 default:
10811 /* ObjC reserved word "id", handled below. */
10812 break;
10817 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10818 form of ObjC type, cases such as "int" and "long" being handled
10819 above), a TYPE (struct, union, enum and typeof specifiers) or an
10820 ERROR_MARK. In none of these cases may there have previously
10821 been any type specifiers. */
10822 if (specs->type || specs->typespec_word != cts_none
10823 || specs->long_p || specs->short_p || specs->signed_p
10824 || specs->unsigned_p || specs->complex_p)
10825 error_at (loc, "two or more data types in declaration specifiers");
10826 else if (TREE_CODE (type) == TYPE_DECL)
10828 if (TREE_TYPE (type) == error_mark_node)
10829 ; /* Allow the type to default to int to avoid cascading errors. */
10830 else
10832 specs->type = TREE_TYPE (type);
10833 specs->decl_attr = DECL_ATTRIBUTES (type);
10834 specs->typedef_p = true;
10835 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10836 specs->locations[cdw_typedef] = loc;
10838 /* If this typedef name is defined in a struct, then a C++
10839 lookup would return a different value. */
10840 if (warn_cxx_compat
10841 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10842 warning_at (loc, OPT_Wc___compat,
10843 "C++ lookup of %qD would return a field, not a type",
10844 type);
10846 /* If we are parsing a struct, record that a struct field
10847 used a typedef. */
10848 if (warn_cxx_compat && struct_parse_info != NULL)
10849 struct_parse_info->typedefs_seen.safe_push (type);
10852 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10854 tree t = lookup_name (type);
10855 if (!t || TREE_CODE (t) != TYPE_DECL)
10856 error_at (loc, "%qE fails to be a typedef or built in type", type);
10857 else if (TREE_TYPE (t) == error_mark_node)
10859 else
10861 specs->type = TREE_TYPE (t);
10862 specs->locations[cdw_typespec] = loc;
10865 else
10867 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10869 specs->typedef_p = true;
10870 specs->locations[cdw_typedef] = loc;
10871 if (spec.expr)
10873 if (specs->expr)
10874 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10875 specs->expr, spec.expr);
10876 else
10877 specs->expr = spec.expr;
10878 specs->expr_const_operands &= spec.expr_const_operands;
10881 specs->type = type;
10884 return specs;
10887 /* Add the storage class specifier or function specifier SCSPEC to the
10888 declaration specifiers SPECS, returning SPECS. */
10890 struct c_declspecs *
10891 declspecs_add_scspec (source_location loc,
10892 struct c_declspecs *specs,
10893 tree scspec)
10895 enum rid i;
10896 enum c_storage_class n = csc_none;
10897 bool dupe = false;
10898 specs->declspecs_seen_p = true;
10899 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10900 && C_IS_RESERVED_WORD (scspec));
10901 i = C_RID_CODE (scspec);
10902 if (specs->non_sc_seen_p)
10903 warning (OPT_Wold_style_declaration,
10904 "%qE is not at beginning of declaration", scspec);
10905 switch (i)
10907 case RID_INLINE:
10908 /* C99 permits duplicate inline. Although of doubtful utility,
10909 it seems simplest to permit it in gnu89 mode as well, as
10910 there is also little utility in maintaining this as a
10911 difference between gnu89 and C99 inline. */
10912 dupe = false;
10913 specs->inline_p = true;
10914 specs->locations[cdw_inline] = loc;
10915 break;
10916 case RID_NORETURN:
10917 /* Duplicate _Noreturn is permitted. */
10918 dupe = false;
10919 specs->noreturn_p = true;
10920 specs->locations[cdw_noreturn] = loc;
10921 break;
10922 case RID_THREAD:
10923 dupe = specs->thread_p;
10924 if (specs->storage_class == csc_auto)
10925 error ("%qE used with %<auto%>", scspec);
10926 else if (specs->storage_class == csc_register)
10927 error ("%qE used with %<register%>", scspec);
10928 else if (specs->storage_class == csc_typedef)
10929 error ("%qE used with %<typedef%>", scspec);
10930 else
10932 specs->thread_p = true;
10933 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10934 "__thread") == 0);
10935 /* A diagnostic is not required for the use of this
10936 identifier in the implementation namespace; only diagnose
10937 it for the C11 spelling because of existing code using
10938 the other spelling. */
10939 if (!specs->thread_gnu_p)
10941 if (flag_isoc99)
10942 pedwarn_c99 (loc, OPT_Wpedantic,
10943 "ISO C99 does not support %qE", scspec);
10944 else
10945 pedwarn_c99 (loc, OPT_Wpedantic,
10946 "ISO C90 does not support %qE", scspec);
10948 specs->locations[cdw_thread] = loc;
10950 break;
10951 case RID_AUTO:
10952 n = csc_auto;
10953 break;
10954 case RID_EXTERN:
10955 n = csc_extern;
10956 /* Diagnose "__thread extern". */
10957 if (specs->thread_p && specs->thread_gnu_p)
10958 error ("%<__thread%> before %<extern%>");
10959 break;
10960 case RID_REGISTER:
10961 n = csc_register;
10962 break;
10963 case RID_STATIC:
10964 n = csc_static;
10965 /* Diagnose "__thread static". */
10966 if (specs->thread_p && specs->thread_gnu_p)
10967 error ("%<__thread%> before %<static%>");
10968 break;
10969 case RID_TYPEDEF:
10970 n = csc_typedef;
10971 break;
10972 default:
10973 gcc_unreachable ();
10975 if (n != csc_none && n == specs->storage_class)
10976 dupe = true;
10977 if (dupe)
10979 if (i == RID_THREAD)
10980 error ("duplicate %<_Thread_local%> or %<__thread%>");
10981 else
10982 error ("duplicate %qE", scspec);
10984 if (n != csc_none)
10986 if (specs->storage_class != csc_none && n != specs->storage_class)
10988 error ("multiple storage classes in declaration specifiers");
10990 else
10992 specs->storage_class = n;
10993 specs->locations[cdw_storage_class] = loc;
10994 if (n != csc_extern && n != csc_static && specs->thread_p)
10996 error ("%qs used with %qE",
10997 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10998 scspec);
10999 specs->thread_p = false;
11003 return specs;
11006 /* Add the attributes ATTRS to the declaration specifiers SPECS,
11007 returning SPECS. */
11009 struct c_declspecs *
11010 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
11012 specs->attrs = chainon (attrs, specs->attrs);
11013 specs->locations[cdw_attributes] = loc;
11014 specs->declspecs_seen_p = true;
11015 return specs;
11018 /* Add an _Alignas specifier (expression ALIGN, or type whose
11019 alignment is ALIGN) to the declaration specifiers SPECS, returning
11020 SPECS. */
11021 struct c_declspecs *
11022 declspecs_add_alignas (source_location loc,
11023 struct c_declspecs *specs, tree align)
11025 int align_log;
11026 specs->alignas_p = true;
11027 specs->locations[cdw_alignas] = loc;
11028 if (align == error_mark_node)
11029 return specs;
11030 align_log = check_user_alignment (align, true);
11031 if (align_log > specs->align_log)
11032 specs->align_log = align_log;
11033 return specs;
11036 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
11037 specifiers with any other type specifier to determine the resulting
11038 type. This is where ISO C checks on complex types are made, since
11039 "_Complex long" is a prefix of the valid ISO C type "_Complex long
11040 double". */
11042 struct c_declspecs *
11043 finish_declspecs (struct c_declspecs *specs)
11045 /* If a type was specified as a whole, we have no modifiers and are
11046 done. */
11047 if (specs->type != NULL_TREE)
11049 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11050 && !specs->signed_p && !specs->unsigned_p
11051 && !specs->complex_p);
11053 /* Set a dummy type. */
11054 if (TREE_CODE (specs->type) == ERROR_MARK)
11055 specs->type = integer_type_node;
11056 return specs;
11059 /* If none of "void", "_Bool", "char", "int", "float" or "double"
11060 has been specified, treat it as "int" unless "_Complex" is
11061 present and there are no other specifiers. If we just have
11062 "_Complex", it is equivalent to "_Complex double", but e.g.
11063 "_Complex short" is equivalent to "_Complex short int". */
11064 if (specs->typespec_word == cts_none)
11066 if (specs->saturating_p)
11068 error_at (specs->locations[cdw_saturating],
11069 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
11070 if (!targetm.fixed_point_supported_p ())
11071 error_at (specs->locations[cdw_saturating],
11072 "fixed-point types not supported for this target");
11073 specs->typespec_word = cts_fract;
11075 else if (specs->long_p || specs->short_p
11076 || specs->signed_p || specs->unsigned_p)
11078 specs->typespec_word = cts_int;
11080 else if (specs->complex_p)
11082 specs->typespec_word = cts_double;
11083 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11084 "ISO C does not support plain %<complex%> meaning "
11085 "%<double complex%>");
11087 else
11089 specs->typespec_word = cts_int;
11090 specs->default_int_p = true;
11091 /* We don't diagnose this here because grokdeclarator will
11092 give more specific diagnostics according to whether it is
11093 a function definition. */
11097 /* If "signed" was specified, record this to distinguish "int" and
11098 "signed int" in the case of a bit-field with
11099 -funsigned-bitfields. */
11100 specs->explicit_signed_p = specs->signed_p;
11102 /* Now compute the actual type. */
11103 switch (specs->typespec_word)
11105 case cts_auto_type:
11106 gcc_assert (!specs->long_p && !specs->short_p
11107 && !specs->signed_p && !specs->unsigned_p
11108 && !specs->complex_p);
11109 /* Type to be filled in later. */
11110 break;
11111 case cts_void:
11112 gcc_assert (!specs->long_p && !specs->short_p
11113 && !specs->signed_p && !specs->unsigned_p
11114 && !specs->complex_p);
11115 specs->type = void_type_node;
11116 break;
11117 case cts_bool:
11118 gcc_assert (!specs->long_p && !specs->short_p
11119 && !specs->signed_p && !specs->unsigned_p
11120 && !specs->complex_p);
11121 specs->type = boolean_type_node;
11122 break;
11123 case cts_char:
11124 gcc_assert (!specs->long_p && !specs->short_p);
11125 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11126 if (specs->signed_p)
11127 specs->type = signed_char_type_node;
11128 else if (specs->unsigned_p)
11129 specs->type = unsigned_char_type_node;
11130 else
11131 specs->type = char_type_node;
11132 if (specs->complex_p)
11134 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11135 "ISO C does not support complex integer types");
11136 specs->type = build_complex_type (specs->type);
11138 break;
11139 case cts_int_n:
11140 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11141 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11142 if (! int_n_enabled_p[specs->int_n_idx])
11143 specs->type = integer_type_node;
11144 else
11145 specs->type = (specs->unsigned_p
11146 ? int_n_trees[specs->int_n_idx].unsigned_type
11147 : int_n_trees[specs->int_n_idx].signed_type);
11148 if (specs->complex_p)
11150 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11151 "ISO C does not support complex integer types");
11152 specs->type = build_complex_type (specs->type);
11154 break;
11155 case cts_int:
11156 gcc_assert (!(specs->long_p && specs->short_p));
11157 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11158 if (specs->long_long_p)
11159 specs->type = (specs->unsigned_p
11160 ? long_long_unsigned_type_node
11161 : long_long_integer_type_node);
11162 else if (specs->long_p)
11163 specs->type = (specs->unsigned_p
11164 ? long_unsigned_type_node
11165 : long_integer_type_node);
11166 else if (specs->short_p)
11167 specs->type = (specs->unsigned_p
11168 ? short_unsigned_type_node
11169 : short_integer_type_node);
11170 else
11171 specs->type = (specs->unsigned_p
11172 ? unsigned_type_node
11173 : integer_type_node);
11174 if (specs->complex_p)
11176 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11177 "ISO C does not support complex integer types");
11178 specs->type = build_complex_type (specs->type);
11180 break;
11181 case cts_float:
11182 gcc_assert (!specs->long_p && !specs->short_p
11183 && !specs->signed_p && !specs->unsigned_p);
11184 specs->type = (specs->complex_p
11185 ? complex_float_type_node
11186 : float_type_node);
11187 break;
11188 case cts_double:
11189 gcc_assert (!specs->long_long_p && !specs->short_p
11190 && !specs->signed_p && !specs->unsigned_p);
11191 if (specs->long_p)
11193 specs->type = (specs->complex_p
11194 ? complex_long_double_type_node
11195 : long_double_type_node);
11197 else
11199 specs->type = (specs->complex_p
11200 ? complex_double_type_node
11201 : double_type_node);
11203 break;
11204 case cts_floatn_nx:
11205 gcc_assert (!specs->long_p && !specs->short_p
11206 && !specs->signed_p && !specs->unsigned_p);
11207 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11208 specs->type = integer_type_node;
11209 else if (specs->complex_p)
11210 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11211 else
11212 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11213 break;
11214 case cts_dfloat32:
11215 case cts_dfloat64:
11216 case cts_dfloat128:
11217 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11218 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11219 if (specs->typespec_word == cts_dfloat32)
11220 specs->type = dfloat32_type_node;
11221 else if (specs->typespec_word == cts_dfloat64)
11222 specs->type = dfloat64_type_node;
11223 else
11224 specs->type = dfloat128_type_node;
11225 break;
11226 case cts_fract:
11227 gcc_assert (!specs->complex_p);
11228 if (!targetm.fixed_point_supported_p ())
11229 specs->type = integer_type_node;
11230 else if (specs->saturating_p)
11232 if (specs->long_long_p)
11233 specs->type = specs->unsigned_p
11234 ? sat_unsigned_long_long_fract_type_node
11235 : sat_long_long_fract_type_node;
11236 else if (specs->long_p)
11237 specs->type = specs->unsigned_p
11238 ? sat_unsigned_long_fract_type_node
11239 : sat_long_fract_type_node;
11240 else if (specs->short_p)
11241 specs->type = specs->unsigned_p
11242 ? sat_unsigned_short_fract_type_node
11243 : sat_short_fract_type_node;
11244 else
11245 specs->type = specs->unsigned_p
11246 ? sat_unsigned_fract_type_node
11247 : sat_fract_type_node;
11249 else
11251 if (specs->long_long_p)
11252 specs->type = specs->unsigned_p
11253 ? unsigned_long_long_fract_type_node
11254 : long_long_fract_type_node;
11255 else if (specs->long_p)
11256 specs->type = specs->unsigned_p
11257 ? unsigned_long_fract_type_node
11258 : long_fract_type_node;
11259 else if (specs->short_p)
11260 specs->type = specs->unsigned_p
11261 ? unsigned_short_fract_type_node
11262 : short_fract_type_node;
11263 else
11264 specs->type = specs->unsigned_p
11265 ? unsigned_fract_type_node
11266 : fract_type_node;
11268 break;
11269 case cts_accum:
11270 gcc_assert (!specs->complex_p);
11271 if (!targetm.fixed_point_supported_p ())
11272 specs->type = integer_type_node;
11273 else if (specs->saturating_p)
11275 if (specs->long_long_p)
11276 specs->type = specs->unsigned_p
11277 ? sat_unsigned_long_long_accum_type_node
11278 : sat_long_long_accum_type_node;
11279 else if (specs->long_p)
11280 specs->type = specs->unsigned_p
11281 ? sat_unsigned_long_accum_type_node
11282 : sat_long_accum_type_node;
11283 else if (specs->short_p)
11284 specs->type = specs->unsigned_p
11285 ? sat_unsigned_short_accum_type_node
11286 : sat_short_accum_type_node;
11287 else
11288 specs->type = specs->unsigned_p
11289 ? sat_unsigned_accum_type_node
11290 : sat_accum_type_node;
11292 else
11294 if (specs->long_long_p)
11295 specs->type = specs->unsigned_p
11296 ? unsigned_long_long_accum_type_node
11297 : long_long_accum_type_node;
11298 else if (specs->long_p)
11299 specs->type = specs->unsigned_p
11300 ? unsigned_long_accum_type_node
11301 : long_accum_type_node;
11302 else if (specs->short_p)
11303 specs->type = specs->unsigned_p
11304 ? unsigned_short_accum_type_node
11305 : short_accum_type_node;
11306 else
11307 specs->type = specs->unsigned_p
11308 ? unsigned_accum_type_node
11309 : accum_type_node;
11311 break;
11312 default:
11313 gcc_unreachable ();
11316 return specs;
11319 /* Perform final processing on one file scope's declarations (or the
11320 external scope's declarations), GLOBALS. */
11322 static void
11323 c_write_global_declarations_1 (tree globals)
11325 tree decl;
11326 bool reconsider;
11328 /* Process the decls in the order they were written. */
11329 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11331 /* Check for used but undefined static functions using the C
11332 standard's definition of "used", and set TREE_NO_WARNING so
11333 that check_global_declaration doesn't repeat the check. */
11334 if (TREE_CODE (decl) == FUNCTION_DECL
11335 && DECL_INITIAL (decl) == NULL_TREE
11336 && DECL_EXTERNAL (decl)
11337 && !TREE_PUBLIC (decl))
11339 if (C_DECL_USED (decl))
11341 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11342 TREE_NO_WARNING (decl) = 1;
11344 /* For -Wunused-function warn about unused static prototypes. */
11345 else if (warn_unused_function
11346 && ! DECL_ARTIFICIAL (decl)
11347 && ! TREE_NO_WARNING (decl))
11349 warning (OPT_Wunused_function,
11350 "%q+F declared %<static%> but never defined", decl);
11351 TREE_NO_WARNING (decl) = 1;
11355 wrapup_global_declaration_1 (decl);
11360 reconsider = false;
11361 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11362 reconsider |= wrapup_global_declaration_2 (decl);
11364 while (reconsider);
11367 /* Callback to collect a source_ref from a DECL. */
11369 static void
11370 collect_source_ref_cb (tree decl)
11372 if (!DECL_IS_BUILTIN (decl))
11373 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11376 /* Preserve the external declarations scope across a garbage collect. */
11377 static GTY(()) tree ext_block;
11379 /* Collect all references relevant to SOURCE_FILE. */
11381 static void
11382 collect_all_refs (const char *source_file)
11384 tree t;
11385 unsigned i;
11387 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11388 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11390 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11393 /* Iterate over all global declarations and call CALLBACK. */
11395 static void
11396 for_each_global_decl (void (*callback) (tree decl))
11398 tree t;
11399 tree decls;
11400 tree decl;
11401 unsigned i;
11403 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11405 decls = DECL_INITIAL (t);
11406 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11407 callback (decl);
11410 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11411 callback (decl);
11414 /* Perform any final parser cleanups and generate initial debugging
11415 information. */
11417 void
11418 c_parse_final_cleanups (void)
11420 tree t;
11421 unsigned i;
11423 /* We don't want to do this if generating a PCH. */
11424 if (pch_file)
11425 return;
11427 timevar_stop (TV_PHASE_PARSING);
11428 timevar_start (TV_PHASE_DEFERRED);
11430 /* Do the Objective-C stuff. This is where all the Objective-C
11431 module stuff gets generated (symtab, class/protocol/selector
11432 lists etc). */
11433 if (c_dialect_objc ())
11434 objc_write_global_declarations ();
11436 /* Close the external scope. */
11437 ext_block = pop_scope ();
11438 external_scope = 0;
11439 gcc_assert (!current_scope);
11441 /* Handle -fdump-ada-spec[-slim]. */
11442 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11444 /* Build a table of files to generate specs for */
11445 if (flag_dump_ada_spec_slim)
11446 collect_source_ref (main_input_filename);
11447 else
11448 for_each_global_decl (collect_source_ref_cb);
11450 dump_ada_specs (collect_all_refs, NULL);
11453 /* Process all file scopes in this compilation, and the external_scope,
11454 through wrapup_global_declarations. */
11455 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11456 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11457 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11459 timevar_stop (TV_PHASE_DEFERRED);
11460 timevar_start (TV_PHASE_PARSING);
11462 ext_block = NULL;
11465 /* Register reserved keyword WORD as qualifier for address space AS. */
11467 void
11468 c_register_addr_space (const char *word, addr_space_t as)
11470 int rid = RID_FIRST_ADDR_SPACE + as;
11471 tree id;
11473 /* Address space qualifiers are only supported
11474 in C with GNU extensions enabled. */
11475 if (c_dialect_objc () || flag_no_asm)
11476 return;
11478 id = get_identifier (word);
11479 C_SET_RID_CODE (id, rid);
11480 C_IS_RESERVED_WORD (id) = 1;
11481 ridpointers [rid] = id;
11484 /* Return identifier to look up for omp declare reduction. */
11486 tree
11487 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11489 const char *p = NULL;
11490 switch (reduction_code)
11492 case PLUS_EXPR: p = "+"; break;
11493 case MULT_EXPR: p = "*"; break;
11494 case MINUS_EXPR: p = "-"; break;
11495 case BIT_AND_EXPR: p = "&"; break;
11496 case BIT_XOR_EXPR: p = "^"; break;
11497 case BIT_IOR_EXPR: p = "|"; break;
11498 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11499 case TRUTH_ORIF_EXPR: p = "||"; break;
11500 case MIN_EXPR: p = "min"; break;
11501 case MAX_EXPR: p = "max"; break;
11502 default:
11503 break;
11506 if (p == NULL)
11508 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11509 return error_mark_node;
11510 p = IDENTIFIER_POINTER (reduction_id);
11513 const char prefix[] = "omp declare reduction ";
11514 size_t lenp = sizeof (prefix);
11515 size_t len = strlen (p);
11516 char *name = XALLOCAVEC (char, lenp + len);
11517 memcpy (name, prefix, lenp - 1);
11518 memcpy (name + lenp - 1, p, len + 1);
11519 return get_identifier (name);
11522 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11523 VAR_DECL, bind it into the current scope and return it. */
11525 tree
11526 c_omp_reduction_decl (tree reduction_id)
11528 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11529 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11530 return b->decl;
11532 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11533 reduction_id, integer_type_node);
11534 DECL_ARTIFICIAL (decl) = 1;
11535 DECL_EXTERNAL (decl) = 1;
11536 TREE_STATIC (decl) = 1;
11537 TREE_PUBLIC (decl) = 0;
11538 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11539 return decl;
11542 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11544 tree
11545 c_omp_reduction_lookup (tree reduction_id, tree type)
11547 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11548 while (b)
11550 tree t;
11551 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11552 if (comptypes (TREE_PURPOSE (t), type))
11553 return TREE_VALUE (t);
11554 b = b->shadowed;
11556 return error_mark_node;
11559 /* Helper function called via walk_tree, to diagnose invalid
11560 #pragma omp declare reduction combiners or initializers. */
11562 tree
11563 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11565 tree *vars = (tree *) data;
11566 if (SSA_VAR_P (*tp)
11567 && !DECL_ARTIFICIAL (*tp)
11568 && *tp != vars[0]
11569 && *tp != vars[1])
11571 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11572 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11573 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11574 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11575 *tp);
11576 else
11577 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11578 "to variable %qD which is not %<omp_priv%> nor "
11579 "%<omp_orig%>",
11580 *tp);
11581 return *tp;
11583 return NULL_TREE;
11586 #include "gt-c-c-decl.h"