gcc/ada/
[official-gcc.git] / gcc / c / c-decl.c
blob23cad017dad777765bfdf901c84db48108f4f7f6
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2014 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 #include "system.h"
29 #include "coretypes.h"
30 #include "input.h"
31 #include "tm.h"
32 #include "intl.h"
33 #include "tree.h"
34 #include "print-tree.h"
35 #include "stor-layout.h"
36 #include "varasm.h"
37 #include "attribs.h"
38 #include "stringpool.h"
39 #include "tree-inline.h"
40 #include "flags.h"
41 #include "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "hard-reg-set.h"
46 #include "function.h"
47 #include "c-tree.h"
48 #include "toplev.h"
49 #include "tm_p.h"
50 #include "cpplib.h"
51 #include "target.h"
52 #include "debug.h"
53 #include "opts.h"
54 #include "timevar.h"
55 #include "c-family/c-common.h"
56 #include "c-family/c-objc.h"
57 #include "c-family/c-pragma.h"
58 #include "c-family/c-ubsan.h"
59 #include "c-lang.h"
60 #include "langhooks.h"
61 #include "tree-iterator.h"
62 #include "diagnostic-core.h"
63 #include "dumpfile.h"
64 #include "hash-map.h"
65 #include "is-a.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "hash-table.h"
70 #include "langhooks-def.h"
71 #include "plugin.h"
72 #include "c-family/c-ada-spec.h"
73 #include "cilk.h"
74 #include "builtins.h"
76 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
77 enum decl_context
78 { NORMAL, /* Ordinary declaration */
79 FUNCDEF, /* Function definition */
80 PARM, /* Declaration of parm before function body */
81 FIELD, /* Declaration inside struct or union */
82 TYPENAME}; /* Typename (inside cast or sizeof) */
84 /* States indicating how grokdeclarator() should handle declspecs marked
85 with __attribute__((deprecated)). An object declared as
86 __attribute__((deprecated)) suppresses warnings of uses of other
87 deprecated items. */
89 enum deprecated_states {
90 DEPRECATED_NORMAL,
91 DEPRECATED_SUPPRESS
95 /* Nonzero if we have seen an invalid cross reference
96 to a struct, union, or enum, but not yet printed the message. */
97 tree pending_invalid_xref;
99 /* File and line to appear in the eventual error message. */
100 location_t pending_invalid_xref_location;
102 /* The file and line that the prototype came from if this is an
103 old-style definition; used for diagnostics in
104 store_parm_decls_oldstyle. */
106 static location_t current_function_prototype_locus;
108 /* Whether this prototype was built-in. */
110 static bool current_function_prototype_built_in;
112 /* The argument type information of this prototype. */
114 static tree current_function_prototype_arg_types;
116 /* The argument information structure for the function currently being
117 defined. */
119 static struct c_arg_info *current_function_arg_info;
121 /* The obstack on which parser and related data structures, which are
122 not live beyond their top-level declaration or definition, are
123 allocated. */
124 struct obstack parser_obstack;
126 /* The current statement tree. */
128 static GTY(()) struct stmt_tree_s c_stmt_tree;
130 /* State saving variables. */
131 tree c_break_label;
132 tree c_cont_label;
134 /* A list of decls to be made automatically visible in each file scope. */
135 static GTY(()) tree visible_builtins;
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a return statement that specifies a return value is seen. */
140 int current_function_returns_value;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement with no argument is seen. */
145 int current_function_returns_null;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a call to a noreturn function is seen. */
150 int current_function_returns_abnormally;
152 /* Set to nonzero by `grokdeclarator' for a function
153 whose return type is defaulted, if warnings for this are desired. */
155 static int warn_about_return_type;
157 /* Nonzero when the current toplevel function contains a declaration
158 of a nested function which is never defined. */
160 static bool undef_nested_function;
162 /* Mode used to build pointers (VOIDmode means ptr_mode). */
164 machine_mode c_default_pointer_mode = VOIDmode;
166 /* If non-zero, implicit "omp declare target" attribute is added into the
167 attribute lists. */
168 int current_omp_declare_target_attribute;
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
182 function.
184 The ->u.type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
194 The ->u.label field is used for labels. It points to a structure
195 which stores additional information used for warnings.
197 The depth field is copied from the scope structure that holds this
198 decl. It is used to preserve the proper ordering of the ->shadowed
199 field (see bind()) and also for a handful of special-case checks.
200 Finally, the invisible bit is true for a decl which should be
201 ignored for purposes of normal name lookup, and the nested bit is
202 true for a decl that's been bound a second time in an inner scope;
203 in all such cases, the binding in the outer scope will have its
204 invisible bit true. */
206 struct GTY((chain_next ("%h.prev"))) c_binding {
207 union GTY(()) { /* first so GTY desc can use decl */
208 tree GTY((tag ("0"))) type; /* the type in this scope */
209 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
210 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
211 tree decl; /* the decl bound */
212 tree id; /* the identifier it's bound to */
213 struct c_binding *prev; /* the previous decl in this scope */
214 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
215 unsigned int depth : 28; /* depth of this scope */
216 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
217 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
218 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
219 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
220 location_t locus; /* location for nested bindings */
222 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
223 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
224 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
225 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
227 /* Each C symbol points to three linked lists of c_binding structures.
228 These describe the values of the identifier in the three different
229 namespaces defined by the language. */
231 struct GTY(()) lang_identifier {
232 struct c_common_identifier common_id;
233 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
234 struct c_binding *tag_binding; /* struct/union/enum tags */
235 struct c_binding *label_binding; /* labels */
238 /* Validate c-lang.c's assumptions. */
239 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
240 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
242 /* The binding oracle; see c-tree.h. */
243 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
245 /* This flag is set on an identifier if we have previously asked the
246 binding oracle for this identifier's symbol binding. */
247 #define I_SYMBOL_CHECKED(node) \
248 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
250 static inline struct c_binding* *
251 i_symbol_binding (tree node)
253 struct lang_identifier *lid
254 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
256 if (lid->symbol_binding == NULL
257 && c_binding_oracle != NULL
258 && !I_SYMBOL_CHECKED (node))
260 /* Set the "checked" flag first, to avoid infinite recursion
261 when the binding oracle calls back into gcc. */
262 I_SYMBOL_CHECKED (node) = 1;
263 c_binding_oracle (C_ORACLE_SYMBOL, node);
266 return &lid->symbol_binding;
269 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
271 #define I_SYMBOL_DECL(node) \
272 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
274 /* This flag is set on an identifier if we have previously asked the
275 binding oracle for this identifier's tag binding. */
276 #define I_TAG_CHECKED(node) \
277 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
279 static inline struct c_binding **
280 i_tag_binding (tree node)
282 struct lang_identifier *lid
283 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
285 if (lid->tag_binding == NULL
286 && c_binding_oracle != NULL
287 && !I_TAG_CHECKED (node))
289 /* Set the "checked" flag first, to avoid infinite recursion
290 when the binding oracle calls back into gcc. */
291 I_TAG_CHECKED (node) = 1;
292 c_binding_oracle (C_ORACLE_TAG, node);
295 return &lid->tag_binding;
298 #define I_TAG_BINDING(node) (*i_tag_binding (node))
300 #define I_TAG_DECL(node) \
301 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
303 /* This flag is set on an identifier if we have previously asked the
304 binding oracle for this identifier's label binding. */
305 #define I_LABEL_CHECKED(node) \
306 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
308 static inline struct c_binding **
309 i_label_binding (tree node)
311 struct lang_identifier *lid
312 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
314 if (lid->label_binding == NULL
315 && c_binding_oracle != NULL
316 && !I_LABEL_CHECKED (node))
318 /* Set the "checked" flag first, to avoid infinite recursion
319 when the binding oracle calls back into gcc. */
320 I_LABEL_CHECKED (node) = 1;
321 c_binding_oracle (C_ORACLE_LABEL, node);
324 return &lid->label_binding;
327 #define I_LABEL_BINDING(node) (*i_label_binding (node))
329 #define I_LABEL_DECL(node) \
330 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
332 /* The resulting tree type. */
334 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
335 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
337 union tree_node GTY ((tag ("0"),
338 desc ("tree_node_structure (&%h)")))
339 generic;
340 struct lang_identifier GTY ((tag ("1"))) identifier;
343 /* Track bindings and other things that matter for goto warnings. For
344 efficiency, we do not gather all the decls at the point of
345 definition. Instead, we point into the bindings structure. As
346 scopes are popped, we update these structures and gather the decls
347 that matter at that time. */
349 struct GTY(()) c_spot_bindings {
350 /* The currently open scope which holds bindings defined when the
351 label was defined or the goto statement was found. */
352 struct c_scope *scope;
353 /* The bindings in the scope field which were defined at the point
354 of the label or goto. This lets us look at older or newer
355 bindings in the scope, as appropriate. */
356 struct c_binding *bindings_in_scope;
357 /* The number of statement expressions that have started since this
358 label or goto statement was defined. This is zero if we are at
359 the same statement expression level. It is positive if we are in
360 a statement expression started since this spot. It is negative
361 if this spot was in a statement expression and we have left
362 it. */
363 int stmt_exprs;
364 /* Whether we started in a statement expression but are no longer in
365 it. This is set to true if stmt_exprs ever goes negative. */
366 bool left_stmt_expr;
369 /* This structure is used to keep track of bindings seen when a goto
370 statement is defined. This is only used if we see the goto
371 statement before we see the label. */
373 struct GTY(()) c_goto_bindings {
374 /* The location of the goto statement. */
375 location_t loc;
376 /* The bindings of the goto statement. */
377 struct c_spot_bindings goto_bindings;
380 typedef struct c_goto_bindings *c_goto_bindings_p;
382 /* The additional information we keep track of for a label binding.
383 These fields are updated as scopes are popped. */
385 struct GTY(()) c_label_vars {
386 /* The shadowed c_label_vars, when one label shadows another (which
387 can only happen using a __label__ declaration). */
388 struct c_label_vars *shadowed;
389 /* The bindings when the label was defined. */
390 struct c_spot_bindings label_bindings;
391 /* A list of decls that we care about: decls about which we should
392 warn if a goto branches to this label from later in the function.
393 Decls are added to this list as scopes are popped. We only add
394 the decls that matter. */
395 vec<tree, va_gc> *decls_in_scope;
396 /* A list of goto statements to this label. This is only used for
397 goto statements seen before the label was defined, so that we can
398 issue appropriate warnings for them. */
399 vec<c_goto_bindings_p, va_gc> *gotos;
402 /* Each c_scope structure describes the complete contents of one
403 scope. Four scopes are distinguished specially: the innermost or
404 current scope, the innermost function scope, the file scope (always
405 the second to outermost) and the outermost or external scope.
407 Most declarations are recorded in the current scope.
409 All normal label declarations are recorded in the innermost
410 function scope, as are bindings of undeclared identifiers to
411 error_mark_node. (GCC permits nested functions as an extension,
412 hence the 'innermost' qualifier.) Explicitly declared labels
413 (using the __label__ extension) appear in the current scope.
415 Being in the file scope (current_scope == file_scope) causes
416 special behavior in several places below. Also, under some
417 conditions the Objective-C front end records declarations in the
418 file scope even though that isn't the current scope.
420 All declarations with external linkage are recorded in the external
421 scope, even if they aren't visible there; this models the fact that
422 such declarations are visible to the entire program, and (with a
423 bit of cleverness, see pushdecl) allows diagnosis of some violations
424 of C99 6.2.2p7 and 6.2.7p2:
426 If, within the same translation unit, the same identifier appears
427 with both internal and external linkage, the behavior is
428 undefined.
430 All declarations that refer to the same object or function shall
431 have compatible type; otherwise, the behavior is undefined.
433 Initially only the built-in declarations, which describe compiler
434 intrinsic functions plus a subset of the standard library, are in
435 this scope.
437 The order of the blocks list matters, and it is frequently appended
438 to. To avoid having to walk all the way to the end of the list on
439 each insertion, or reverse the list later, we maintain a pointer to
440 the last list entry. (FIXME: It should be feasible to use a reversed
441 list here.)
443 The bindings list is strictly in reverse order of declarations;
444 pop_scope relies on this. */
447 struct GTY((chain_next ("%h.outer"))) c_scope {
448 /* The scope containing this one. */
449 struct c_scope *outer;
451 /* The next outermost function scope. */
452 struct c_scope *outer_function;
454 /* All bindings in this scope. */
455 struct c_binding *bindings;
457 /* For each scope (except the global one), a chain of BLOCK nodes
458 for all the scopes that were entered and exited one level down. */
459 tree blocks;
460 tree blocks_last;
462 /* The depth of this scope. Used to keep the ->shadowed chain of
463 bindings sorted innermost to outermost. */
464 unsigned int depth : 28;
466 /* True if we are currently filling this scope with parameter
467 declarations. */
468 BOOL_BITFIELD parm_flag : 1;
470 /* True if we saw [*] in this scope. Used to give an error messages
471 if these appears in a function definition. */
472 BOOL_BITFIELD had_vla_unspec : 1;
474 /* True if we already complained about forward parameter decls
475 in this scope. This prevents double warnings on
476 foo (int a; int b; ...) */
477 BOOL_BITFIELD warned_forward_parm_decls : 1;
479 /* True if this is the outermost block scope of a function body.
480 This scope contains the parameters, the local variables declared
481 in the outermost block, and all the labels (except those in
482 nested functions, or declared at block scope with __label__). */
483 BOOL_BITFIELD function_body : 1;
485 /* True means make a BLOCK for this scope no matter what. */
486 BOOL_BITFIELD keep : 1;
488 /* True means that an unsuffixed float constant is _Decimal64. */
489 BOOL_BITFIELD float_const_decimal64 : 1;
491 /* True if this scope has any label bindings. This is used to speed
492 up searching for labels when popping scopes, particularly since
493 labels are normally only found at function scope. */
494 BOOL_BITFIELD has_label_bindings : 1;
496 /* True if we should issue a warning if a goto statement crosses any
497 of the bindings. We still need to check the list of bindings to
498 find the specific ones we need to warn about. This is true if
499 decl_jump_unsafe would return true for any of the bindings. This
500 is used to avoid looping over all the bindings unnecessarily. */
501 BOOL_BITFIELD has_jump_unsafe_decl : 1;
504 /* The scope currently in effect. */
506 static GTY(()) struct c_scope *current_scope;
508 /* The innermost function scope. Ordinary (not explicitly declared)
509 labels, bindings to error_mark_node, and the lazily-created
510 bindings of __func__ and its friends get this scope. */
512 static GTY(()) struct c_scope *current_function_scope;
514 /* The C file scope. This is reset for each input translation unit. */
516 static GTY(()) struct c_scope *file_scope;
518 /* The outermost scope. This is used for all declarations with
519 external linkage, and only these, hence the name. */
521 static GTY(()) struct c_scope *external_scope;
523 /* A chain of c_scope structures awaiting reuse. */
525 static GTY((deletable)) struct c_scope *scope_freelist;
527 /* A chain of c_binding structures awaiting reuse. */
529 static GTY((deletable)) struct c_binding *binding_freelist;
531 /* Append VAR to LIST in scope SCOPE. */
532 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
533 struct c_scope *s_ = (scope); \
534 tree d_ = (decl); \
535 if (s_->list##_last) \
536 BLOCK_CHAIN (s_->list##_last) = d_; \
537 else \
538 s_->list = d_; \
539 s_->list##_last = d_; \
540 } while (0)
542 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
543 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
544 struct c_scope *t_ = (tscope); \
545 struct c_scope *f_ = (fscope); \
546 if (t_->to##_last) \
547 BLOCK_CHAIN (t_->to##_last) = f_->from; \
548 else \
549 t_->to = f_->from; \
550 t_->to##_last = f_->from##_last; \
551 } while (0)
553 /* A c_inline_static structure stores details of a static identifier
554 referenced in a definition of a function that may be an inline
555 definition if no subsequent declaration of that function uses
556 "extern" or does not use "inline". */
558 struct GTY((chain_next ("%h.next"))) c_inline_static {
559 /* The location for a diagnostic. */
560 location_t location;
562 /* The function that may be an inline definition. */
563 tree function;
565 /* The object or function referenced. */
566 tree static_decl;
568 /* What sort of reference this is. */
569 enum c_inline_static_type type;
571 /* The next such structure or NULL. */
572 struct c_inline_static *next;
575 /* List of static identifiers used or referenced in functions that may
576 be inline definitions. */
577 static GTY(()) struct c_inline_static *c_inline_statics;
579 /* True means unconditionally make a BLOCK for the next scope pushed. */
581 static bool keep_next_level_flag;
583 /* True means the next call to push_scope will be the outermost scope
584 of a function body, so do not push a new scope, merely cease
585 expecting parameter decls. */
587 static bool next_is_function_body;
589 /* A vector of pointers to c_binding structures. */
591 typedef struct c_binding *c_binding_ptr;
593 /* Information that we keep for a struct or union while it is being
594 parsed. */
596 struct c_struct_parse_info
598 /* If warn_cxx_compat, a list of types defined within this
599 struct. */
600 vec<tree> struct_types;
601 /* If warn_cxx_compat, a list of field names which have bindings,
602 and which are defined in this struct, but which are not defined
603 in any enclosing struct. This is used to clear the in_struct
604 field of the c_bindings structure. */
605 vec<c_binding_ptr> fields;
606 /* If warn_cxx_compat, a list of typedef names used when defining
607 fields in this struct. */
608 vec<tree> typedefs_seen;
611 /* Information for the struct or union currently being parsed, or
612 NULL if not parsing a struct or union. */
613 static struct c_struct_parse_info *struct_parse_info;
615 /* Forward declarations. */
616 static tree lookup_name_in_scope (tree, struct c_scope *);
617 static tree c_make_fname_decl (location_t, tree, int);
618 static tree grokdeclarator (const struct c_declarator *,
619 struct c_declspecs *,
620 enum decl_context, bool, tree *, tree *, tree *,
621 bool *, enum deprecated_states);
622 static tree grokparms (struct c_arg_info *, bool);
623 static void layout_array_type (tree);
624 static void warn_defaults_to (location_t, int, const char *, ...)
625 ATTRIBUTE_GCC_DIAG(3,4);
627 /* T is a statement. Add it to the statement-tree. This is the
628 C/ObjC version--C++ has a slightly different version of this
629 function. */
631 tree
632 add_stmt (tree t)
634 enum tree_code code = TREE_CODE (t);
636 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
638 if (!EXPR_HAS_LOCATION (t))
639 SET_EXPR_LOCATION (t, input_location);
642 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
643 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
645 /* Add T to the statement-tree. Non-side-effect statements need to be
646 recorded during statement expressions. */
647 if (!building_stmt_list_p ())
648 push_stmt_list ();
649 append_to_statement_list_force (t, &cur_stmt_list);
651 return t;
654 /* Build a pointer type using the default pointer mode. */
656 static tree
657 c_build_pointer_type (tree to_type)
659 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
660 : TYPE_ADDR_SPACE (to_type);
661 machine_mode pointer_mode;
663 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
664 pointer_mode = targetm.addr_space.pointer_mode (as);
665 else
666 pointer_mode = c_default_pointer_mode;
667 return build_pointer_type_for_mode (to_type, pointer_mode, false);
671 /* Return true if we will want to say something if a goto statement
672 crosses DECL. */
674 static bool
675 decl_jump_unsafe (tree decl)
677 if (error_operand_p (decl))
678 return false;
680 /* Always warn about crossing variably modified types. */
681 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
682 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
683 return true;
685 /* Otherwise, only warn if -Wgoto-misses-init and this is an
686 initialized automatic decl. */
687 if (warn_jump_misses_init
688 && TREE_CODE (decl) == VAR_DECL
689 && !TREE_STATIC (decl)
690 && DECL_INITIAL (decl) != NULL_TREE)
691 return true;
693 return false;
697 void
698 c_print_identifier (FILE *file, tree node, int indent)
700 void (*save) (enum c_oracle_request, tree identifier);
702 /* Temporarily hide any binding oracle. Without this, calls to
703 debug_tree from the debugger will end up calling into the oracle,
704 making for a confusing debug session. As the oracle isn't needed
705 here for normal operation, it's simplest to suppress it. */
706 save = c_binding_oracle;
707 c_binding_oracle = NULL;
709 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
710 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
711 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
712 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
714 tree rid = ridpointers[C_RID_CODE (node)];
715 indent_to (file, indent + 4);
716 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
717 (void *) rid, IDENTIFIER_POINTER (rid));
720 c_binding_oracle = save;
723 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
724 which may be any of several kinds of DECL or TYPE or error_mark_node,
725 in the scope SCOPE. */
726 static void
727 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
728 bool nested, location_t locus)
730 struct c_binding *b, **here;
732 if (binding_freelist)
734 b = binding_freelist;
735 binding_freelist = b->prev;
737 else
738 b = ggc_alloc<c_binding> ();
740 b->shadowed = 0;
741 b->decl = decl;
742 b->id = name;
743 b->depth = scope->depth;
744 b->invisible = invisible;
745 b->nested = nested;
746 b->inner_comp = 0;
747 b->in_struct = 0;
748 b->locus = locus;
750 b->u.type = NULL;
752 b->prev = scope->bindings;
753 scope->bindings = b;
755 if (decl_jump_unsafe (decl))
756 scope->has_jump_unsafe_decl = 1;
758 if (!name)
759 return;
761 switch (TREE_CODE (decl))
763 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
764 case ENUMERAL_TYPE:
765 case UNION_TYPE:
766 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
767 case VAR_DECL:
768 case FUNCTION_DECL:
769 case TYPE_DECL:
770 case CONST_DECL:
771 case PARM_DECL:
772 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
774 default:
775 gcc_unreachable ();
778 /* Locate the appropriate place in the chain of shadowed decls
779 to insert this binding. Normally, scope == current_scope and
780 this does nothing. */
781 while (*here && (*here)->depth > scope->depth)
782 here = &(*here)->shadowed;
784 b->shadowed = *here;
785 *here = b;
788 /* Clear the binding structure B, stick it on the binding_freelist,
789 and return the former value of b->prev. This is used by pop_scope
790 and get_parm_info to iterate destructively over all the bindings
791 from a given scope. */
792 static struct c_binding *
793 free_binding_and_advance (struct c_binding *b)
795 struct c_binding *prev = b->prev;
797 memset (b, 0, sizeof (struct c_binding));
798 b->prev = binding_freelist;
799 binding_freelist = b;
801 return prev;
804 /* Bind a label. Like bind, but skip fields which aren't used for
805 labels, and add the LABEL_VARS value. */
806 static void
807 bind_label (tree name, tree label, struct c_scope *scope,
808 struct c_label_vars *label_vars)
810 struct c_binding *b;
812 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
813 UNKNOWN_LOCATION);
815 scope->has_label_bindings = true;
817 b = scope->bindings;
818 gcc_assert (b->decl == label);
819 label_vars->shadowed = b->u.label;
820 b->u.label = label_vars;
823 /* Hook called at end of compilation to assume 1 elt
824 for a file-scope tentative array defn that wasn't complete before. */
826 void
827 c_finish_incomplete_decl (tree decl)
829 if (TREE_CODE (decl) == VAR_DECL)
831 tree type = TREE_TYPE (decl);
832 if (type != error_mark_node
833 && TREE_CODE (type) == ARRAY_TYPE
834 && !DECL_EXTERNAL (decl)
835 && TYPE_DOMAIN (type) == 0)
837 warning_at (DECL_SOURCE_LOCATION (decl),
838 0, "array %q+D assumed to have one element", decl);
840 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
842 relayout_decl (decl);
847 /* Record that inline function FUNC contains a reference (location
848 LOC) to static DECL (file-scope or function-local according to
849 TYPE). */
851 void
852 record_inline_static (location_t loc, tree func, tree decl,
853 enum c_inline_static_type type)
855 c_inline_static *csi = ggc_alloc<c_inline_static> ();
856 csi->location = loc;
857 csi->function = func;
858 csi->static_decl = decl;
859 csi->type = type;
860 csi->next = c_inline_statics;
861 c_inline_statics = csi;
864 /* Check for references to static declarations in inline functions at
865 the end of the translation unit and diagnose them if the functions
866 are still inline definitions. */
868 static void
869 check_inline_statics (void)
871 struct c_inline_static *csi;
872 for (csi = c_inline_statics; csi; csi = csi->next)
874 if (DECL_EXTERNAL (csi->function))
875 switch (csi->type)
877 case csi_internal:
878 pedwarn (csi->location, 0,
879 "%qD is static but used in inline function %qD "
880 "which is not static", csi->static_decl, csi->function);
881 break;
882 case csi_modifiable:
883 pedwarn (csi->location, 0,
884 "%q+D is static but declared in inline function %qD "
885 "which is not static", csi->static_decl, csi->function);
886 break;
887 default:
888 gcc_unreachable ();
891 c_inline_statics = NULL;
894 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
895 for the current state, otherwise set it to uninitialized. */
897 static void
898 set_spot_bindings (struct c_spot_bindings *p, bool defining)
900 if (defining)
902 p->scope = current_scope;
903 p->bindings_in_scope = current_scope->bindings;
905 else
907 p->scope = NULL;
908 p->bindings_in_scope = NULL;
910 p->stmt_exprs = 0;
911 p->left_stmt_expr = false;
914 /* Update spot bindings P as we pop out of SCOPE. Return true if we
915 should push decls for a label. */
917 static bool
918 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
920 if (p->scope != scope)
922 /* This label or goto is defined in some other scope, or it is a
923 label which is not yet defined. There is nothing to
924 update. */
925 return false;
928 /* Adjust the spot bindings to refer to the bindings already defined
929 in the enclosing scope. */
930 p->scope = scope->outer;
931 p->bindings_in_scope = p->scope->bindings;
933 return true;
936 /* The Objective-C front-end often needs to determine the current scope. */
938 void *
939 objc_get_current_scope (void)
941 return current_scope;
944 /* The following function is used only by Objective-C. It needs to live here
945 because it accesses the innards of c_scope. */
947 void
948 objc_mark_locals_volatile (void *enclosing_blk)
950 struct c_scope *scope;
951 struct c_binding *b;
953 for (scope = current_scope;
954 scope && scope != enclosing_blk;
955 scope = scope->outer)
957 for (b = scope->bindings; b; b = b->prev)
958 objc_volatilize_decl (b->decl);
960 /* Do not climb up past the current function. */
961 if (scope->function_body)
962 break;
966 /* Return true if we are in the global binding level. */
968 bool
969 global_bindings_p (void)
971 return current_scope == file_scope;
974 void
975 keep_next_level (void)
977 keep_next_level_flag = true;
980 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
982 void
983 set_float_const_decimal64 (void)
985 current_scope->float_const_decimal64 = true;
988 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
990 void
991 clear_float_const_decimal64 (void)
993 current_scope->float_const_decimal64 = false;
996 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
998 bool
999 float_const_decimal64_p (void)
1001 return current_scope->float_const_decimal64;
1004 /* Identify this scope as currently being filled with parameters. */
1006 void
1007 declare_parm_level (void)
1009 current_scope->parm_flag = true;
1012 void
1013 push_scope (void)
1015 if (next_is_function_body)
1017 /* This is the transition from the parameters to the top level
1018 of the function body. These are the same scope
1019 (C99 6.2.1p4,6) so we do not push another scope structure.
1020 next_is_function_body is set only by store_parm_decls, which
1021 in turn is called when and only when we are about to
1022 encounter the opening curly brace for the function body.
1024 The outermost block of a function always gets a BLOCK node,
1025 because the debugging output routines expect that each
1026 function has at least one BLOCK. */
1027 current_scope->parm_flag = false;
1028 current_scope->function_body = true;
1029 current_scope->keep = true;
1030 current_scope->outer_function = current_function_scope;
1031 current_function_scope = current_scope;
1033 keep_next_level_flag = false;
1034 next_is_function_body = false;
1036 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1037 if (current_scope->outer)
1038 current_scope->float_const_decimal64
1039 = current_scope->outer->float_const_decimal64;
1040 else
1041 current_scope->float_const_decimal64 = false;
1043 else
1045 struct c_scope *scope;
1046 if (scope_freelist)
1048 scope = scope_freelist;
1049 scope_freelist = scope->outer;
1051 else
1052 scope = ggc_cleared_alloc<c_scope> ();
1054 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1055 if (current_scope)
1056 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1057 else
1058 scope->float_const_decimal64 = false;
1060 scope->keep = keep_next_level_flag;
1061 scope->outer = current_scope;
1062 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1064 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1065 possible. */
1066 if (current_scope && scope->depth == 0)
1068 scope->depth--;
1069 sorry ("GCC supports only %u nested scopes", scope->depth);
1072 current_scope = scope;
1073 keep_next_level_flag = false;
1077 /* This is called when we are leaving SCOPE. For each label defined
1078 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1079 These are the decls whose initialization will be skipped by a goto
1080 later in the function. */
1082 static void
1083 update_label_decls (struct c_scope *scope)
1085 struct c_scope *s;
1087 s = scope;
1088 while (s != NULL)
1090 if (s->has_label_bindings)
1092 struct c_binding *b;
1094 for (b = s->bindings; b != NULL; b = b->prev)
1096 struct c_label_vars *label_vars;
1097 struct c_binding *b1;
1098 bool hjud;
1099 unsigned int ix;
1100 struct c_goto_bindings *g;
1102 if (TREE_CODE (b->decl) != LABEL_DECL)
1103 continue;
1104 label_vars = b->u.label;
1106 b1 = label_vars->label_bindings.bindings_in_scope;
1107 if (label_vars->label_bindings.scope == NULL)
1108 hjud = false;
1109 else
1110 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1111 if (update_spot_bindings (scope, &label_vars->label_bindings))
1113 /* This label is defined in this scope. */
1114 if (hjud)
1116 for (; b1 != NULL; b1 = b1->prev)
1118 /* A goto from later in the function to this
1119 label will never see the initialization
1120 of B1, if any. Save it to issue a
1121 warning if needed. */
1122 if (decl_jump_unsafe (b1->decl))
1123 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1128 /* Update the bindings of any goto statements associated
1129 with this label. */
1130 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1131 update_spot_bindings (scope, &g->goto_bindings);
1135 /* Don't search beyond the current function. */
1136 if (s == current_function_scope)
1137 break;
1139 s = s->outer;
1143 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1145 static void
1146 set_type_context (tree type, tree context)
1148 for (type = TYPE_MAIN_VARIANT (type); type;
1149 type = TYPE_NEXT_VARIANT (type))
1150 TYPE_CONTEXT (type) = context;
1153 /* Exit a scope. Restore the state of the identifier-decl mappings
1154 that were in effect when this scope was entered. Return a BLOCK
1155 node containing all the DECLs in this scope that are of interest
1156 to debug info generation. */
1158 tree
1159 pop_scope (void)
1161 struct c_scope *scope = current_scope;
1162 tree block, context, p;
1163 struct c_binding *b;
1165 bool functionbody = scope->function_body;
1166 bool keep = functionbody || scope->keep || scope->bindings;
1168 update_label_decls (scope);
1170 /* If appropriate, create a BLOCK to record the decls for the life
1171 of this function. */
1172 block = 0;
1173 if (keep)
1175 block = make_node (BLOCK);
1176 BLOCK_SUBBLOCKS (block) = scope->blocks;
1177 TREE_USED (block) = 1;
1179 /* In each subblock, record that this is its superior. */
1180 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1181 BLOCK_SUPERCONTEXT (p) = block;
1183 BLOCK_VARS (block) = 0;
1186 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1187 scope must be set so that they point to the appropriate
1188 construct, i.e. either to the current FUNCTION_DECL node, or
1189 else to the BLOCK node we just constructed.
1191 Note that for tagged types whose scope is just the formal
1192 parameter list for some function type specification, we can't
1193 properly set their TYPE_CONTEXTs here, because we don't have a
1194 pointer to the appropriate FUNCTION_TYPE node readily available
1195 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1196 type nodes get set in `grokdeclarator' as soon as we have created
1197 the FUNCTION_TYPE node which will represent the "scope" for these
1198 "parameter list local" tagged types. */
1199 if (scope->function_body)
1200 context = current_function_decl;
1201 else if (scope == file_scope)
1203 tree file_decl = build_translation_unit_decl (NULL_TREE);
1204 context = file_decl;
1206 else
1207 context = block;
1209 /* Clear all bindings in this scope. */
1210 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1212 p = b->decl;
1213 switch (TREE_CODE (p))
1215 case LABEL_DECL:
1216 /* Warnings for unused labels, errors for undefined labels. */
1217 if (TREE_USED (p) && !DECL_INITIAL (p))
1219 error ("label %q+D used but not defined", p);
1220 DECL_INITIAL (p) = error_mark_node;
1222 else
1223 warn_for_unused_label (p);
1225 /* Labels go in BLOCK_VARS. */
1226 DECL_CHAIN (p) = BLOCK_VARS (block);
1227 BLOCK_VARS (block) = p;
1228 gcc_assert (I_LABEL_BINDING (b->id) == b);
1229 I_LABEL_BINDING (b->id) = b->shadowed;
1231 /* Also pop back to the shadowed label_vars. */
1232 release_tree_vector (b->u.label->decls_in_scope);
1233 b->u.label = b->u.label->shadowed;
1234 break;
1236 case ENUMERAL_TYPE:
1237 case UNION_TYPE:
1238 case RECORD_TYPE:
1239 set_type_context (p, context);
1241 /* Types may not have tag-names, in which case the type
1242 appears in the bindings list with b->id NULL. */
1243 if (b->id)
1245 gcc_assert (I_TAG_BINDING (b->id) == b);
1246 I_TAG_BINDING (b->id) = b->shadowed;
1248 break;
1250 case FUNCTION_DECL:
1251 /* Propagate TREE_ADDRESSABLE from nested functions to their
1252 containing functions. */
1253 if (!TREE_ASM_WRITTEN (p)
1254 && DECL_INITIAL (p) != 0
1255 && TREE_ADDRESSABLE (p)
1256 && DECL_ABSTRACT_ORIGIN (p) != 0
1257 && DECL_ABSTRACT_ORIGIN (p) != p)
1258 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1259 if (!DECL_EXTERNAL (p)
1260 && !DECL_INITIAL (p)
1261 && scope != file_scope
1262 && scope != external_scope)
1264 error ("nested function %q+D declared but never defined", p);
1265 undef_nested_function = true;
1267 else if (DECL_DECLARED_INLINE_P (p)
1268 && TREE_PUBLIC (p)
1269 && !DECL_INITIAL (p))
1271 /* C99 6.7.4p6: "a function with external linkage... declared
1272 with an inline function specifier ... shall also be defined
1273 in the same translation unit." */
1274 if (!flag_gnu89_inline
1275 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1276 && scope != external_scope)
1277 pedwarn (input_location, 0,
1278 "inline function %q+D declared but never defined", p);
1279 DECL_EXTERNAL (p) = 1;
1282 goto common_symbol;
1284 case VAR_DECL:
1285 /* Warnings for unused variables. */
1286 if ((!TREE_USED (p) || !DECL_READ_P (p))
1287 && !TREE_NO_WARNING (p)
1288 && !DECL_IN_SYSTEM_HEADER (p)
1289 && DECL_NAME (p)
1290 && !DECL_ARTIFICIAL (p)
1291 && scope != file_scope
1292 && scope != external_scope)
1294 if (!TREE_USED (p))
1295 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1296 else if (DECL_CONTEXT (p) == current_function_decl)
1297 warning_at (DECL_SOURCE_LOCATION (p),
1298 OPT_Wunused_but_set_variable,
1299 "variable %qD set but not used", p);
1302 if (b->inner_comp)
1304 error ("type of array %q+D completed incompatibly with"
1305 " implicit initialization", p);
1308 /* Fall through. */
1309 case TYPE_DECL:
1310 case CONST_DECL:
1311 common_symbol:
1312 /* All of these go in BLOCK_VARS, but only if this is the
1313 binding in the home scope. */
1314 if (!b->nested)
1316 DECL_CHAIN (p) = BLOCK_VARS (block);
1317 BLOCK_VARS (block) = p;
1319 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1321 /* For block local externs add a special
1322 DECL_EXTERNAL decl for debug info generation. */
1323 tree extp = copy_node (p);
1325 DECL_EXTERNAL (extp) = 1;
1326 TREE_STATIC (extp) = 0;
1327 TREE_PUBLIC (extp) = 1;
1328 DECL_INITIAL (extp) = NULL_TREE;
1329 DECL_LANG_SPECIFIC (extp) = NULL;
1330 DECL_CONTEXT (extp) = current_function_decl;
1331 if (TREE_CODE (p) == FUNCTION_DECL)
1333 DECL_RESULT (extp) = NULL_TREE;
1334 DECL_SAVED_TREE (extp) = NULL_TREE;
1335 DECL_STRUCT_FUNCTION (extp) = NULL;
1337 if (b->locus != UNKNOWN_LOCATION)
1338 DECL_SOURCE_LOCATION (extp) = b->locus;
1339 DECL_CHAIN (extp) = BLOCK_VARS (block);
1340 BLOCK_VARS (block) = extp;
1342 /* If this is the file scope set DECL_CONTEXT of each decl to
1343 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1344 work. */
1345 if (scope == file_scope)
1347 DECL_CONTEXT (p) = context;
1348 if (TREE_CODE (p) == TYPE_DECL
1349 && TREE_TYPE (p) != error_mark_node)
1350 set_type_context (TREE_TYPE (p), context);
1353 /* Fall through. */
1354 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1355 already been put there by store_parm_decls. Unused-
1356 parameter warnings are handled by function.c.
1357 error_mark_node obviously does not go in BLOCK_VARS and
1358 does not get unused-variable warnings. */
1359 case PARM_DECL:
1360 case ERROR_MARK:
1361 /* It is possible for a decl not to have a name. We get
1362 here with b->id NULL in this case. */
1363 if (b->id)
1365 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1366 I_SYMBOL_BINDING (b->id) = b->shadowed;
1367 if (b->shadowed && b->shadowed->u.type)
1368 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1370 break;
1372 default:
1373 gcc_unreachable ();
1378 /* Dispose of the block that we just made inside some higher level. */
1379 if ((scope->function_body || scope == file_scope) && context)
1381 DECL_INITIAL (context) = block;
1382 BLOCK_SUPERCONTEXT (block) = context;
1384 else if (scope->outer)
1386 if (block)
1387 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1388 /* If we did not make a block for the scope just exited, any
1389 blocks made for inner scopes must be carried forward so they
1390 will later become subblocks of something else. */
1391 else if (scope->blocks)
1392 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1395 /* Pop the current scope, and free the structure for reuse. */
1396 current_scope = scope->outer;
1397 if (scope->function_body)
1398 current_function_scope = scope->outer_function;
1400 memset (scope, 0, sizeof (struct c_scope));
1401 scope->outer = scope_freelist;
1402 scope_freelist = scope;
1404 return block;
1407 void
1408 push_file_scope (void)
1410 tree decl;
1412 if (file_scope)
1413 return;
1415 push_scope ();
1416 file_scope = current_scope;
1418 start_fname_decls ();
1420 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1421 bind (DECL_NAME (decl), decl, file_scope,
1422 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1425 void
1426 pop_file_scope (void)
1428 /* In case there were missing closebraces, get us back to the global
1429 binding level. */
1430 while (current_scope != file_scope)
1431 pop_scope ();
1433 /* __FUNCTION__ is defined at file scope (""). This
1434 call may not be necessary as my tests indicate it
1435 still works without it. */
1436 finish_fname_decls ();
1438 check_inline_statics ();
1440 /* This is the point to write out a PCH if we're doing that.
1441 In that case we do not want to do anything else. */
1442 if (pch_file)
1444 c_common_write_pch ();
1445 return;
1448 /* Pop off the file scope and close this translation unit. */
1449 pop_scope ();
1450 file_scope = 0;
1452 maybe_apply_pending_pragma_weaks ();
1455 /* Adjust the bindings for the start of a statement expression. */
1457 void
1458 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1460 struct c_scope *scope;
1462 for (scope = current_scope; scope != NULL; scope = scope->outer)
1464 struct c_binding *b;
1466 if (!scope->has_label_bindings)
1467 continue;
1469 for (b = scope->bindings; b != NULL; b = b->prev)
1471 struct c_label_vars *label_vars;
1472 unsigned int ix;
1473 struct c_goto_bindings *g;
1475 if (TREE_CODE (b->decl) != LABEL_DECL)
1476 continue;
1477 label_vars = b->u.label;
1478 ++label_vars->label_bindings.stmt_exprs;
1479 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1480 ++g->goto_bindings.stmt_exprs;
1484 if (switch_bindings != NULL)
1485 ++switch_bindings->stmt_exprs;
1488 /* Adjust the bindings for the end of a statement expression. */
1490 void
1491 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1493 struct c_scope *scope;
1495 for (scope = current_scope; scope != NULL; scope = scope->outer)
1497 struct c_binding *b;
1499 if (!scope->has_label_bindings)
1500 continue;
1502 for (b = scope->bindings; b != NULL; b = b->prev)
1504 struct c_label_vars *label_vars;
1505 unsigned int ix;
1506 struct c_goto_bindings *g;
1508 if (TREE_CODE (b->decl) != LABEL_DECL)
1509 continue;
1510 label_vars = b->u.label;
1511 --label_vars->label_bindings.stmt_exprs;
1512 if (label_vars->label_bindings.stmt_exprs < 0)
1514 label_vars->label_bindings.left_stmt_expr = true;
1515 label_vars->label_bindings.stmt_exprs = 0;
1517 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1519 --g->goto_bindings.stmt_exprs;
1520 if (g->goto_bindings.stmt_exprs < 0)
1522 g->goto_bindings.left_stmt_expr = true;
1523 g->goto_bindings.stmt_exprs = 0;
1529 if (switch_bindings != NULL)
1531 --switch_bindings->stmt_exprs;
1532 gcc_assert (switch_bindings->stmt_exprs >= 0);
1536 /* Push a definition or a declaration of struct, union or enum tag "name".
1537 "type" should be the type node.
1538 We assume that the tag "name" is not already defined, and has a location
1539 of LOC.
1541 Note that the definition may really be just a forward reference.
1542 In that case, the TYPE_SIZE will be zero. */
1544 static void
1545 pushtag (location_t loc, tree name, tree type)
1547 /* Record the identifier as the type's name if it has none. */
1548 if (name && !TYPE_NAME (type))
1549 TYPE_NAME (type) = name;
1550 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1552 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1553 tagged type we just added to the current scope. This fake
1554 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1555 to output a representation of a tagged type, and it also gives
1556 us a convenient place to record the "scope start" address for the
1557 tagged type. */
1559 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1560 TYPE_DECL, NULL_TREE, type));
1562 /* An approximation for now, so we can tell this is a function-scope tag.
1563 This will be updated in pop_scope. */
1564 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1566 if (warn_cxx_compat && name != NULL_TREE)
1568 struct c_binding *b = I_SYMBOL_BINDING (name);
1570 if (b != NULL
1571 && b->decl != NULL_TREE
1572 && TREE_CODE (b->decl) == TYPE_DECL
1573 && (B_IN_CURRENT_SCOPE (b)
1574 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1575 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1576 != TYPE_MAIN_VARIANT (type)))
1578 warning_at (loc, OPT_Wc___compat,
1579 ("using %qD as both a typedef and a tag is "
1580 "invalid in C++"),
1581 b->decl);
1582 if (b->locus != UNKNOWN_LOCATION)
1583 inform (b->locus, "originally defined here");
1588 /* An exported interface to pushtag. This is used by the gdb plugin's
1589 binding oracle to introduce a new tag binding. */
1591 void
1592 c_pushtag (location_t loc, tree name, tree type)
1594 pushtag (loc, name, type);
1597 /* An exported interface to bind a declaration. LOC is the location
1598 to use. DECL is the declaration to bind. The decl's name is used
1599 to determine how it is bound. If DECL is a VAR_DECL, then
1600 IS_GLOBAL determines whether the decl is put into the global (file
1601 and external) scope or the current function's scope; if DECL is not
1602 a VAR_DECL then it is always put into the file scope. */
1604 void
1605 c_bind (location_t loc, tree decl, bool is_global)
1607 struct c_scope *scope;
1608 bool nested = false;
1610 if (TREE_CODE (decl) != VAR_DECL || current_function_scope == NULL)
1612 /* Types and functions are always considered to be global. */
1613 scope = file_scope;
1614 DECL_EXTERNAL (decl) = 1;
1615 TREE_PUBLIC (decl) = 1;
1617 else if (is_global)
1619 /* Also bind it into the external scope. */
1620 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1621 nested = true;
1622 scope = file_scope;
1623 DECL_EXTERNAL (decl) = 1;
1624 TREE_PUBLIC (decl) = 1;
1626 else
1628 DECL_CONTEXT (decl) = current_function_decl;
1629 TREE_PUBLIC (decl) = 0;
1630 scope = current_function_scope;
1633 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1636 /* Subroutine of compare_decls. Allow harmless mismatches in return
1637 and argument types provided that the type modes match. This function
1638 return a unified type given a suitable match, and 0 otherwise. */
1640 static tree
1641 match_builtin_function_types (tree newtype, tree oldtype)
1643 tree newrettype, oldrettype;
1644 tree newargs, oldargs;
1645 tree trytype, tryargs;
1647 /* Accept the return type of the new declaration if same modes. */
1648 oldrettype = TREE_TYPE (oldtype);
1649 newrettype = TREE_TYPE (newtype);
1651 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1652 return 0;
1654 oldargs = TYPE_ARG_TYPES (oldtype);
1655 newargs = TYPE_ARG_TYPES (newtype);
1656 tryargs = newargs;
1658 while (oldargs || newargs)
1660 if (!oldargs
1661 || !newargs
1662 || !TREE_VALUE (oldargs)
1663 || !TREE_VALUE (newargs)
1664 || TYPE_MODE (TREE_VALUE (oldargs))
1665 != TYPE_MODE (TREE_VALUE (newargs)))
1666 return 0;
1668 oldargs = TREE_CHAIN (oldargs);
1669 newargs = TREE_CHAIN (newargs);
1672 trytype = build_function_type (newrettype, tryargs);
1673 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1676 /* Subroutine of diagnose_mismatched_decls. Check for function type
1677 mismatch involving an empty arglist vs a nonempty one and give clearer
1678 diagnostics. */
1679 static void
1680 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1681 tree newtype, tree oldtype)
1683 tree t;
1685 if (TREE_CODE (olddecl) != FUNCTION_DECL
1686 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1687 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1688 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1689 return;
1691 t = TYPE_ARG_TYPES (oldtype);
1692 if (t == 0)
1693 t = TYPE_ARG_TYPES (newtype);
1694 for (; t; t = TREE_CHAIN (t))
1696 tree type = TREE_VALUE (t);
1698 if (TREE_CHAIN (t) == 0
1699 && TYPE_MAIN_VARIANT (type) != void_type_node)
1701 inform (input_location, "a parameter list with an ellipsis can%'t match "
1702 "an empty parameter name list declaration");
1703 break;
1706 if (c_type_promotes_to (type) != type)
1708 inform (input_location, "an argument type that has a default promotion can%'t match "
1709 "an empty parameter name list declaration");
1710 break;
1715 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1716 old-style function definition, NEWDECL is a prototype declaration.
1717 Diagnose inconsistencies in the argument list. Returns TRUE if
1718 the prototype is compatible, FALSE if not. */
1719 static bool
1720 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1722 tree newargs, oldargs;
1723 int i;
1725 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1727 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1728 newargs = TYPE_ARG_TYPES (newtype);
1729 i = 1;
1731 for (;;)
1733 tree oldargtype = TREE_VALUE (oldargs);
1734 tree newargtype = TREE_VALUE (newargs);
1736 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1737 return false;
1739 oldargtype = (TYPE_ATOMIC (oldargtype)
1740 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1741 TYPE_QUAL_ATOMIC)
1742 : TYPE_MAIN_VARIANT (oldargtype));
1743 newargtype = (TYPE_ATOMIC (newargtype)
1744 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1745 TYPE_QUAL_ATOMIC)
1746 : TYPE_MAIN_VARIANT (newargtype));
1748 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1749 break;
1751 /* Reaching the end of just one list means the two decls don't
1752 agree on the number of arguments. */
1753 if (END_OF_ARGLIST (oldargtype))
1755 error ("prototype for %q+D declares more arguments "
1756 "than previous old-style definition", newdecl);
1757 return false;
1759 else if (END_OF_ARGLIST (newargtype))
1761 error ("prototype for %q+D declares fewer arguments "
1762 "than previous old-style definition", newdecl);
1763 return false;
1766 /* Type for passing arg must be consistent with that declared
1767 for the arg. */
1768 else if (!comptypes (oldargtype, newargtype))
1770 error ("prototype for %q+D declares argument %d"
1771 " with incompatible type",
1772 newdecl, i);
1773 return false;
1776 oldargs = TREE_CHAIN (oldargs);
1777 newargs = TREE_CHAIN (newargs);
1778 i++;
1781 /* If we get here, no errors were found, but do issue a warning
1782 for this poor-style construct. */
1783 warning (0, "prototype for %q+D follows non-prototype definition",
1784 newdecl);
1785 return true;
1786 #undef END_OF_ARGLIST
1789 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1790 first in a pair of mismatched declarations, using the diagnostic
1791 function DIAG. */
1792 static void
1793 locate_old_decl (tree decl)
1795 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1796 && !C_DECL_DECLARED_BUILTIN (decl))
1798 else if (DECL_INITIAL (decl))
1799 inform (input_location, "previous definition of %q+D was here", decl);
1800 else if (C_DECL_IMPLICIT (decl))
1801 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1802 else
1803 inform (input_location, "previous declaration of %q+D was here", decl);
1806 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1807 Returns true if the caller should proceed to merge the two, false
1808 if OLDDECL should simply be discarded. As a side effect, issues
1809 all necessary diagnostics for invalid or poor-style combinations.
1810 If it returns true, writes the types of NEWDECL and OLDDECL to
1811 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1812 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1814 static bool
1815 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1816 tree *newtypep, tree *oldtypep)
1818 tree newtype, oldtype;
1819 bool pedwarned = false;
1820 bool warned = false;
1821 bool retval = true;
1823 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1824 && DECL_EXTERNAL (DECL))
1826 /* If we have error_mark_node for either decl or type, just discard
1827 the previous decl - we're in an error cascade already. */
1828 if (olddecl == error_mark_node || newdecl == error_mark_node)
1829 return false;
1830 *oldtypep = oldtype = TREE_TYPE (olddecl);
1831 *newtypep = newtype = TREE_TYPE (newdecl);
1832 if (oldtype == error_mark_node || newtype == error_mark_node)
1833 return false;
1835 /* Two different categories of symbol altogether. This is an error
1836 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1837 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1839 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1840 && DECL_BUILT_IN (olddecl)
1841 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1843 error ("%q+D redeclared as different kind of symbol", newdecl);
1844 locate_old_decl (olddecl);
1846 else if (TREE_PUBLIC (newdecl))
1847 warning (0, "built-in function %q+D declared as non-function",
1848 newdecl);
1849 else
1850 warning (OPT_Wshadow, "declaration of %q+D shadows "
1851 "a built-in function", newdecl);
1852 return false;
1855 /* Enumerators have no linkage, so may only be declared once in a
1856 given scope. */
1857 if (TREE_CODE (olddecl) == CONST_DECL)
1859 error ("redeclaration of enumerator %q+D", newdecl);
1860 locate_old_decl (olddecl);
1861 return false;
1864 if (!comptypes (oldtype, newtype))
1866 if (TREE_CODE (olddecl) == FUNCTION_DECL
1867 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1869 /* Accept harmless mismatch in function types.
1870 This is for the ffs and fprintf builtins. */
1871 tree trytype = match_builtin_function_types (newtype, oldtype);
1873 if (trytype && comptypes (newtype, trytype))
1874 *oldtypep = oldtype = trytype;
1875 else
1877 /* If types don't match for a built-in, throw away the
1878 built-in. No point in calling locate_old_decl here, it
1879 won't print anything. */
1880 warning (0, "conflicting types for built-in function %q+D",
1881 newdecl);
1882 return false;
1885 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1886 && DECL_IS_BUILTIN (olddecl))
1888 /* A conflicting function declaration for a predeclared
1889 function that isn't actually built in. Objective C uses
1890 these. The new declaration silently overrides everything
1891 but the volatility (i.e. noreturn) indication. See also
1892 below. FIXME: Make Objective C use normal builtins. */
1893 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1894 return false;
1896 /* Permit void foo (...) to match int foo (...) if the latter is
1897 the definition and implicit int was used. See
1898 c-torture/compile/920625-2.c. */
1899 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1900 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1901 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1902 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1904 pedwarned = pedwarn (input_location, 0,
1905 "conflicting types for %q+D", newdecl);
1906 /* Make sure we keep void as the return type. */
1907 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1908 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1910 /* Permit void foo (...) to match an earlier call to foo (...) with
1911 no declared type (thus, implicitly int). */
1912 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1913 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1914 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1915 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1917 pedwarned = pedwarn (input_location, 0,
1918 "conflicting types for %q+D", newdecl);
1919 /* Make sure we keep void as the return type. */
1920 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1922 else
1924 int new_quals = TYPE_QUALS (newtype);
1925 int old_quals = TYPE_QUALS (oldtype);
1927 if (new_quals != old_quals)
1929 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1930 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1931 if (new_addr != old_addr)
1933 if (ADDR_SPACE_GENERIC_P (new_addr))
1934 error ("conflicting named address spaces (generic vs %s) "
1935 "for %q+D",
1936 c_addr_space_name (old_addr), newdecl);
1937 else if (ADDR_SPACE_GENERIC_P (old_addr))
1938 error ("conflicting named address spaces (%s vs generic) "
1939 "for %q+D",
1940 c_addr_space_name (new_addr), newdecl);
1941 else
1942 error ("conflicting named address spaces (%s vs %s) "
1943 "for %q+D",
1944 c_addr_space_name (new_addr),
1945 c_addr_space_name (old_addr),
1946 newdecl);
1949 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1950 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1951 error ("conflicting type qualifiers for %q+D", newdecl);
1953 else
1954 error ("conflicting types for %q+D", newdecl);
1955 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1956 locate_old_decl (olddecl);
1957 return false;
1961 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1962 but silently ignore the redeclaration if either is in a system
1963 header. (Conflicting redeclarations were handled above.) This
1964 is allowed for C11 if the types are the same, not just
1965 compatible. */
1966 if (TREE_CODE (newdecl) == TYPE_DECL)
1968 bool types_different = false;
1969 int comptypes_result;
1971 comptypes_result
1972 = comptypes_check_different_types (oldtype, newtype, &types_different);
1974 if (comptypes_result != 1 || types_different)
1976 error ("redefinition of typedef %q+D with different type", newdecl);
1977 locate_old_decl (olddecl);
1978 return false;
1981 if (DECL_IN_SYSTEM_HEADER (newdecl)
1982 || DECL_IN_SYSTEM_HEADER (olddecl)
1983 || TREE_NO_WARNING (newdecl)
1984 || TREE_NO_WARNING (olddecl))
1985 return true; /* Allow OLDDECL to continue in use. */
1987 if (variably_modified_type_p (newtype, NULL))
1989 error ("redefinition of typedef %q+D with variably modified type",
1990 newdecl);
1991 locate_old_decl (olddecl);
1993 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1994 "redefinition of typedef %q+D", newdecl))
1995 locate_old_decl (olddecl);
1997 return true;
2000 /* Function declarations can either be 'static' or 'extern' (no
2001 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2002 can never conflict with each other on account of linkage
2003 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2004 gnu89 mode permits two definitions if one is 'extern inline' and
2005 one is not. The non- extern-inline definition supersedes the
2006 extern-inline definition. */
2008 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2010 /* If you declare a built-in function name as static, or
2011 define the built-in with an old-style definition (so we
2012 can't validate the argument list) the built-in definition is
2013 overridden, but optionally warn this was a bad choice of name. */
2014 if (DECL_BUILT_IN (olddecl)
2015 && !C_DECL_DECLARED_BUILTIN (olddecl)
2016 && (!TREE_PUBLIC (newdecl)
2017 || (DECL_INITIAL (newdecl)
2018 && !prototype_p (TREE_TYPE (newdecl)))))
2020 warning (OPT_Wshadow, "declaration of %q+D shadows "
2021 "a built-in function", newdecl);
2022 /* Discard the old built-in function. */
2023 return false;
2026 if (DECL_INITIAL (newdecl))
2028 if (DECL_INITIAL (olddecl))
2030 /* If both decls are in the same TU and the new declaration
2031 isn't overriding an extern inline reject the new decl.
2032 In c99, no overriding is allowed in the same translation
2033 unit. */
2034 if ((!DECL_EXTERN_INLINE (olddecl)
2035 || DECL_EXTERN_INLINE (newdecl)
2036 || (!flag_gnu89_inline
2037 && (!DECL_DECLARED_INLINE_P (olddecl)
2038 || !lookup_attribute ("gnu_inline",
2039 DECL_ATTRIBUTES (olddecl)))
2040 && (!DECL_DECLARED_INLINE_P (newdecl)
2041 || !lookup_attribute ("gnu_inline",
2042 DECL_ATTRIBUTES (newdecl))))
2044 && same_translation_unit_p (newdecl, olddecl))
2046 error ("redefinition of %q+D", newdecl);
2047 locate_old_decl (olddecl);
2048 return false;
2052 /* If we have a prototype after an old-style function definition,
2053 the argument types must be checked specially. */
2054 else if (DECL_INITIAL (olddecl)
2055 && !prototype_p (oldtype) && prototype_p (newtype)
2056 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2057 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2059 locate_old_decl (olddecl);
2060 return false;
2062 /* A non-static declaration (even an "extern") followed by a
2063 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2064 The same is true for a static forward declaration at block
2065 scope followed by a non-static declaration/definition at file
2066 scope. Static followed by non-static at the same scope is
2067 not undefined behavior, and is the most convenient way to get
2068 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2069 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2070 we do diagnose it if -Wtraditional. */
2071 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2073 /* Two exceptions to the rule. If olddecl is an extern
2074 inline, or a predeclared function that isn't actually
2075 built in, newdecl silently overrides olddecl. The latter
2076 occur only in Objective C; see also above. (FIXME: Make
2077 Objective C use normal builtins.) */
2078 if (!DECL_IS_BUILTIN (olddecl)
2079 && !DECL_EXTERN_INLINE (olddecl))
2081 error ("static declaration of %q+D follows "
2082 "non-static declaration", newdecl);
2083 locate_old_decl (olddecl);
2085 return false;
2087 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2089 if (DECL_CONTEXT (olddecl))
2091 error ("non-static declaration of %q+D follows "
2092 "static declaration", newdecl);
2093 locate_old_decl (olddecl);
2094 return false;
2096 else if (warn_traditional)
2098 warned |= warning (OPT_Wtraditional,
2099 "non-static declaration of %q+D "
2100 "follows static declaration", newdecl);
2104 /* Make sure gnu_inline attribute is either not present, or
2105 present on all inline decls. */
2106 if (DECL_DECLARED_INLINE_P (olddecl)
2107 && DECL_DECLARED_INLINE_P (newdecl))
2109 bool newa = lookup_attribute ("gnu_inline",
2110 DECL_ATTRIBUTES (newdecl)) != NULL;
2111 bool olda = lookup_attribute ("gnu_inline",
2112 DECL_ATTRIBUTES (olddecl)) != NULL;
2113 if (newa != olda)
2115 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2116 newa ? newdecl : olddecl);
2117 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2118 "but not here");
2122 else if (TREE_CODE (newdecl) == VAR_DECL)
2124 /* Only variables can be thread-local, and all declarations must
2125 agree on this property. */
2126 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2128 /* Nothing to check. Since OLDDECL is marked threadprivate
2129 and NEWDECL does not have a thread-local attribute, we
2130 will merge the threadprivate attribute into NEWDECL. */
2133 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2135 if (DECL_THREAD_LOCAL_P (newdecl))
2136 error ("thread-local declaration of %q+D follows "
2137 "non-thread-local declaration", newdecl);
2138 else
2139 error ("non-thread-local declaration of %q+D follows "
2140 "thread-local declaration", newdecl);
2142 locate_old_decl (olddecl);
2143 return false;
2146 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2147 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2149 error ("redefinition of %q+D", newdecl);
2150 locate_old_decl (olddecl);
2151 return false;
2154 /* Objects declared at file scope: if the first declaration had
2155 external linkage (even if it was an external reference) the
2156 second must have external linkage as well, or the behavior is
2157 undefined. If the first declaration had internal linkage, then
2158 the second must too, or else be an external reference (in which
2159 case the composite declaration still has internal linkage).
2160 As for function declarations, we warn about the static-then-
2161 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2162 if (DECL_FILE_SCOPE_P (newdecl)
2163 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2165 if (DECL_EXTERNAL (newdecl))
2167 if (!DECL_FILE_SCOPE_P (olddecl))
2169 error ("extern declaration of %q+D follows "
2170 "declaration with no linkage", newdecl);
2171 locate_old_decl (olddecl);
2172 return false;
2174 else if (warn_traditional)
2176 warned |= warning (OPT_Wtraditional,
2177 "non-static declaration of %q+D "
2178 "follows static declaration", newdecl);
2181 else
2183 if (TREE_PUBLIC (newdecl))
2184 error ("non-static declaration of %q+D follows "
2185 "static declaration", newdecl);
2186 else
2187 error ("static declaration of %q+D follows "
2188 "non-static declaration", newdecl);
2190 locate_old_decl (olddecl);
2191 return false;
2194 /* Two objects with the same name declared at the same block
2195 scope must both be external references (6.7p3). */
2196 else if (!DECL_FILE_SCOPE_P (newdecl))
2198 if (DECL_EXTERNAL (newdecl))
2200 /* Extern with initializer at block scope, which will
2201 already have received an error. */
2203 else if (DECL_EXTERNAL (olddecl))
2205 error ("declaration of %q+D with no linkage follows "
2206 "extern declaration", newdecl);
2207 locate_old_decl (olddecl);
2209 else
2211 error ("redeclaration of %q+D with no linkage", newdecl);
2212 locate_old_decl (olddecl);
2215 return false;
2218 /* C++ does not permit a decl to appear multiple times at file
2219 scope. */
2220 if (warn_cxx_compat
2221 && DECL_FILE_SCOPE_P (newdecl)
2222 && !DECL_EXTERNAL (newdecl)
2223 && !DECL_EXTERNAL (olddecl))
2224 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2225 OPT_Wc___compat,
2226 ("duplicate declaration of %qD is "
2227 "invalid in C++"),
2228 newdecl);
2231 /* warnings */
2232 /* All decls must agree on a visibility. */
2233 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2234 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2235 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2237 warned |= warning (0, "redeclaration of %q+D with different visibility "
2238 "(old visibility preserved)", newdecl);
2241 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2243 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2244 if (DECL_DECLARED_INLINE_P (newdecl)
2245 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2246 warned |= warning (OPT_Wattributes,
2247 "inline declaration of %qD follows "
2248 "declaration with attribute noinline", newdecl);
2249 else if (DECL_DECLARED_INLINE_P (olddecl)
2250 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2251 warned |= warning (OPT_Wattributes,
2252 "declaration of %q+D with attribute "
2253 "noinline follows inline declaration ", newdecl);
2254 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2255 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2256 warned |= warning (OPT_Wattributes,
2257 "declaration of %q+D with attribute "
2258 "%qs follows declaration with attribute %qs",
2259 newdecl, "noinline", "always_inline");
2260 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2261 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2262 warned |= warning (OPT_Wattributes,
2263 "declaration of %q+D with attribute "
2264 "%qs follows declaration with attribute %qs",
2265 newdecl, "always_inline", "noinline");
2266 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2267 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2268 warned |= warning (OPT_Wattributes,
2269 "declaration of %q+D with attribute %qs follows "
2270 "declaration with attribute %qs", newdecl, "cold",
2271 "hot");
2272 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2273 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2274 warned |= warning (OPT_Wattributes,
2275 "declaration of %q+D with attribute %qs follows "
2276 "declaration with attribute %qs", newdecl, "hot",
2277 "cold");
2279 else /* PARM_DECL, VAR_DECL */
2281 /* Redeclaration of a parameter is a constraint violation (this is
2282 not explicitly stated, but follows from C99 6.7p3 [no more than
2283 one declaration of the same identifier with no linkage in the
2284 same scope, except type tags] and 6.2.2p6 [parameters have no
2285 linkage]). We must check for a forward parameter declaration,
2286 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2287 an extension, the mandatory diagnostic for which is handled by
2288 mark_forward_parm_decls. */
2290 if (TREE_CODE (newdecl) == PARM_DECL
2291 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2293 error ("redefinition of parameter %q+D", newdecl);
2294 locate_old_decl (olddecl);
2295 return false;
2299 /* Optional warning for completely redundant decls. */
2300 if (!warned && !pedwarned
2301 && warn_redundant_decls
2302 /* Don't warn about a function declaration followed by a
2303 definition. */
2304 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2305 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2306 /* Don't warn about redundant redeclarations of builtins. */
2307 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2308 && !DECL_BUILT_IN (newdecl)
2309 && DECL_BUILT_IN (olddecl)
2310 && !C_DECL_DECLARED_BUILTIN (olddecl))
2311 /* Don't warn about an extern followed by a definition. */
2312 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2313 /* Don't warn about forward parameter decls. */
2314 && !(TREE_CODE (newdecl) == PARM_DECL
2315 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2316 /* Don't warn about a variable definition following a declaration. */
2317 && !(TREE_CODE (newdecl) == VAR_DECL
2318 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2320 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2321 newdecl);
2324 /* Report location of previous decl/defn. */
2325 if (warned || pedwarned)
2326 locate_old_decl (olddecl);
2328 #undef DECL_EXTERN_INLINE
2330 return retval;
2333 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2334 consistent with OLDDECL, but carries new information. Merge the
2335 new information into OLDDECL. This function issues no
2336 diagnostics. */
2338 static void
2339 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2341 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2342 && DECL_INITIAL (newdecl) != 0);
2343 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2344 && prototype_p (TREE_TYPE (newdecl)));
2345 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2346 && prototype_p (TREE_TYPE (olddecl)));
2348 /* For real parm decl following a forward decl, rechain the old decl
2349 in its new location and clear TREE_ASM_WRITTEN (it's not a
2350 forward decl anymore). */
2351 if (TREE_CODE (newdecl) == PARM_DECL
2352 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2354 struct c_binding *b, **here;
2356 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2357 if ((*here)->decl == olddecl)
2358 goto found;
2359 gcc_unreachable ();
2361 found:
2362 b = *here;
2363 *here = b->prev;
2364 b->prev = current_scope->bindings;
2365 current_scope->bindings = b;
2367 TREE_ASM_WRITTEN (olddecl) = 0;
2370 DECL_ATTRIBUTES (newdecl)
2371 = targetm.merge_decl_attributes (olddecl, newdecl);
2373 /* Merge the data types specified in the two decls. */
2374 TREE_TYPE (newdecl)
2375 = TREE_TYPE (olddecl)
2376 = composite_type (newtype, oldtype);
2378 /* Lay the type out, unless already done. */
2379 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2381 if (TREE_TYPE (newdecl) != error_mark_node)
2382 layout_type (TREE_TYPE (newdecl));
2383 if (TREE_CODE (newdecl) != FUNCTION_DECL
2384 && TREE_CODE (newdecl) != TYPE_DECL
2385 && TREE_CODE (newdecl) != CONST_DECL)
2386 layout_decl (newdecl, 0);
2388 else
2390 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2391 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2392 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2393 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2394 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2396 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2397 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2401 /* Keep the old rtl since we can safely use it. */
2402 if (HAS_RTL_P (olddecl))
2403 COPY_DECL_RTL (olddecl, newdecl);
2405 /* Merge the type qualifiers. */
2406 if (TREE_READONLY (newdecl))
2407 TREE_READONLY (olddecl) = 1;
2409 if (TREE_THIS_VOLATILE (newdecl))
2410 TREE_THIS_VOLATILE (olddecl) = 1;
2412 /* Merge deprecatedness. */
2413 if (TREE_DEPRECATED (newdecl))
2414 TREE_DEPRECATED (olddecl) = 1;
2416 /* If a decl is in a system header and the other isn't, keep the one on the
2417 system header. Otherwise, keep source location of definition rather than
2418 declaration and of prototype rather than non-prototype unless that
2419 prototype is built-in. */
2420 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2421 && DECL_IN_SYSTEM_HEADER (olddecl)
2422 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2423 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2424 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2425 && DECL_IN_SYSTEM_HEADER (newdecl)
2426 && !DECL_IN_SYSTEM_HEADER (olddecl))
2427 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2428 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2429 || (old_is_prototype && !new_is_prototype
2430 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2431 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2433 /* Merge the initialization information. */
2434 if (DECL_INITIAL (newdecl) == 0)
2435 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2437 /* Merge the threadprivate attribute. */
2438 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2439 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2441 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2443 /* Copy the assembler name.
2444 Currently, it can only be defined in the prototype. */
2445 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2447 /* Use visibility of whichever declaration had it specified */
2448 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2450 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2451 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2454 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2456 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2457 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2458 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2459 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2460 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2461 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2462 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2463 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2464 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2465 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2466 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2469 /* Merge the storage class information. */
2470 merge_weak (newdecl, olddecl);
2472 /* For functions, static overrides non-static. */
2473 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2475 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2476 /* This is since we don't automatically
2477 copy the attributes of NEWDECL into OLDDECL. */
2478 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2479 /* If this clears `static', clear it in the identifier too. */
2480 if (!TREE_PUBLIC (olddecl))
2481 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2485 /* In c99, 'extern' declaration before (or after) 'inline' means this
2486 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2487 is present. */
2488 if (TREE_CODE (newdecl) == FUNCTION_DECL
2489 && !flag_gnu89_inline
2490 && (DECL_DECLARED_INLINE_P (newdecl)
2491 || DECL_DECLARED_INLINE_P (olddecl))
2492 && (!DECL_DECLARED_INLINE_P (newdecl)
2493 || !DECL_DECLARED_INLINE_P (olddecl)
2494 || !DECL_EXTERNAL (olddecl))
2495 && DECL_EXTERNAL (newdecl)
2496 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2497 && !current_function_decl)
2498 DECL_EXTERNAL (newdecl) = 0;
2500 /* An inline definition following a static declaration is not
2501 DECL_EXTERNAL. */
2502 if (new_is_definition
2503 && (DECL_DECLARED_INLINE_P (newdecl)
2504 || DECL_DECLARED_INLINE_P (olddecl))
2505 && !TREE_PUBLIC (olddecl))
2506 DECL_EXTERNAL (newdecl) = 0;
2508 if (DECL_EXTERNAL (newdecl))
2510 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2511 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2513 /* An extern decl does not override previous storage class. */
2514 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2515 if (!DECL_EXTERNAL (newdecl))
2517 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2518 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2521 else
2523 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2524 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2527 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2529 /* If we're redefining a function previously defined as extern
2530 inline, make sure we emit debug info for the inline before we
2531 throw it away, in case it was inlined into a function that
2532 hasn't been written out yet. */
2533 if (new_is_definition && DECL_INITIAL (olddecl))
2534 /* The new defn must not be inline. */
2535 DECL_UNINLINABLE (newdecl) = 1;
2536 else
2538 /* If either decl says `inline', this fn is inline, unless
2539 its definition was passed already. */
2540 if (DECL_DECLARED_INLINE_P (newdecl)
2541 || DECL_DECLARED_INLINE_P (olddecl))
2542 DECL_DECLARED_INLINE_P (newdecl) = 1;
2544 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2545 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2547 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2548 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2549 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2550 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2553 if (DECL_BUILT_IN (olddecl))
2555 /* If redeclaring a builtin function, it stays built in.
2556 But it gets tagged as having been declared. */
2557 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2558 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2559 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2560 if (new_is_prototype)
2562 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2563 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2565 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2566 switch (fncode)
2568 /* If a compatible prototype of these builtin functions
2569 is seen, assume the runtime implements it with the
2570 expected semantics. */
2571 case BUILT_IN_STPCPY:
2572 if (builtin_decl_explicit_p (fncode))
2573 set_builtin_decl_implicit_p (fncode, true);
2574 break;
2575 default:
2576 break;
2580 else
2581 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2582 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2585 /* Preserve function specific target and optimization options */
2586 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2587 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2588 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2589 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2591 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2592 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2593 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2594 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2596 /* Also preserve various other info from the definition. */
2597 if (!new_is_definition)
2599 tree t;
2600 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2601 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2602 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2603 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2604 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2605 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2606 DECL_CONTEXT (t) = newdecl;
2608 /* See if we've got a function to instantiate from. */
2609 if (DECL_SAVED_TREE (olddecl))
2610 DECL_ABSTRACT_ORIGIN (newdecl)
2611 = DECL_ABSTRACT_ORIGIN (olddecl);
2615 /* Merge the USED information. */
2616 if (TREE_USED (olddecl))
2617 TREE_USED (newdecl) = 1;
2618 else if (TREE_USED (newdecl))
2619 TREE_USED (olddecl) = 1;
2620 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2621 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2622 if (DECL_PRESERVE_P (olddecl))
2623 DECL_PRESERVE_P (newdecl) = 1;
2624 else if (DECL_PRESERVE_P (newdecl))
2625 DECL_PRESERVE_P (olddecl) = 1;
2627 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2628 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2629 DECL_ARGUMENTS (if appropriate). */
2631 unsigned olddecl_uid = DECL_UID (olddecl);
2632 tree olddecl_context = DECL_CONTEXT (olddecl);
2633 tree olddecl_arguments = NULL;
2634 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2635 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2637 memcpy ((char *) olddecl + sizeof (struct tree_common),
2638 (char *) newdecl + sizeof (struct tree_common),
2639 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2640 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2641 switch (TREE_CODE (olddecl))
2643 case FUNCTION_DECL:
2644 case VAR_DECL:
2646 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2648 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2649 (char *) newdecl + sizeof (struct tree_decl_common),
2650 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2651 olddecl->decl_with_vis.symtab_node = snode;
2653 if ((DECL_EXTERNAL (olddecl)
2654 || TREE_PUBLIC (olddecl)
2655 || TREE_STATIC (olddecl))
2656 && DECL_SECTION_NAME (newdecl) != NULL)
2657 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2659 /* This isn't quite correct for something like
2660 int __thread x attribute ((tls_model ("local-exec")));
2661 extern int __thread x;
2662 as we'll lose the "local-exec" model. */
2663 if (TREE_CODE (olddecl) == VAR_DECL
2664 && DECL_THREAD_LOCAL_P (newdecl))
2665 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2666 break;
2669 case FIELD_DECL:
2670 case PARM_DECL:
2671 case LABEL_DECL:
2672 case RESULT_DECL:
2673 case CONST_DECL:
2674 case TYPE_DECL:
2675 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2676 (char *) newdecl + sizeof (struct tree_decl_common),
2677 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2678 break;
2680 default:
2682 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2683 (char *) newdecl + sizeof (struct tree_decl_common),
2684 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2686 DECL_UID (olddecl) = olddecl_uid;
2687 DECL_CONTEXT (olddecl) = olddecl_context;
2688 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2689 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2692 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2693 so that encode_section_info has a chance to look at the new decl
2694 flags and attributes. */
2695 if (DECL_RTL_SET_P (olddecl)
2696 && (TREE_CODE (olddecl) == FUNCTION_DECL
2697 || (TREE_CODE (olddecl) == VAR_DECL
2698 && TREE_STATIC (olddecl))))
2699 make_decl_rtl (olddecl);
2702 /* Handle when a new declaration NEWDECL has the same name as an old
2703 one OLDDECL in the same binding contour. Prints an error message
2704 if appropriate.
2706 If safely possible, alter OLDDECL to look like NEWDECL, and return
2707 true. Otherwise, return false. */
2709 static bool
2710 duplicate_decls (tree newdecl, tree olddecl)
2712 tree newtype = NULL, oldtype = NULL;
2714 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2716 /* Avoid `unused variable' and other warnings for OLDDECL. */
2717 TREE_NO_WARNING (olddecl) = 1;
2718 return false;
2721 merge_decls (newdecl, olddecl, newtype, oldtype);
2723 /* The NEWDECL will no longer be needed.
2725 Before releasing the node, be sure to remove function from symbol
2726 table that might have been inserted there to record comdat group.
2727 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2728 structure is shared in between NEWDECL and OLDECL. */
2729 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2730 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2731 if (TREE_CODE (newdecl) == FUNCTION_DECL
2732 || TREE_CODE (newdecl) == VAR_DECL)
2734 struct symtab_node *snode = symtab_node::get (newdecl);
2735 if (snode)
2736 snode->remove ();
2738 ggc_free (newdecl);
2739 return true;
2743 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2744 static void
2745 warn_if_shadowing (tree new_decl)
2747 struct c_binding *b;
2749 /* Shadow warnings wanted? */
2750 if (!warn_shadow
2751 /* No shadow warnings for internally generated vars. */
2752 || DECL_IS_BUILTIN (new_decl)
2753 /* No shadow warnings for vars made for inlining. */
2754 || DECL_FROM_INLINE (new_decl))
2755 return;
2757 /* Is anything being shadowed? Invisible decls do not count. */
2758 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2759 if (b->decl && b->decl != new_decl && !b->invisible
2760 && (b->decl == error_mark_node
2761 || diagnostic_report_warnings_p (global_dc,
2762 DECL_SOURCE_LOCATION (b->decl))))
2764 tree old_decl = b->decl;
2765 bool warned = false;
2767 if (old_decl == error_mark_node)
2769 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2770 "non-variable", new_decl);
2771 break;
2773 else if (TREE_CODE (old_decl) == PARM_DECL)
2774 warned = warning (OPT_Wshadow,
2775 "declaration of %q+D shadows a parameter",
2776 new_decl);
2777 else if (DECL_FILE_SCOPE_P (old_decl))
2779 /* Do not warn if a variable shadows a function, unless
2780 the variable is a function or a pointer-to-function. */
2781 if (TREE_CODE (old_decl) == FUNCTION_DECL
2782 && TREE_CODE (new_decl) != FUNCTION_DECL
2783 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2784 continue;
2786 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2787 "declaration of %qD shadows a global "
2788 "declaration",
2789 new_decl);
2791 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2792 && DECL_BUILT_IN (old_decl))
2794 warning (OPT_Wshadow, "declaration of %q+D shadows "
2795 "a built-in function", new_decl);
2796 break;
2798 else
2799 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2800 "previous local", new_decl);
2802 if (warned)
2803 inform (DECL_SOURCE_LOCATION (old_decl),
2804 "shadowed declaration is here");
2806 break;
2810 /* Record a decl-node X as belonging to the current lexical scope.
2811 Check for errors (such as an incompatible declaration for the same
2812 name already seen in the same scope).
2814 Returns either X or an old decl for the same name.
2815 If an old decl is returned, it may have been smashed
2816 to agree with what X says. */
2818 tree
2819 pushdecl (tree x)
2821 tree name = DECL_NAME (x);
2822 struct c_scope *scope = current_scope;
2823 struct c_binding *b;
2824 bool nested = false;
2825 location_t locus = DECL_SOURCE_LOCATION (x);
2827 /* Must set DECL_CONTEXT for everything not at file scope or
2828 DECL_FILE_SCOPE_P won't work. Local externs don't count
2829 unless they have initializers (which generate code). */
2830 if (current_function_decl
2831 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2832 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2833 DECL_CONTEXT (x) = current_function_decl;
2835 /* Anonymous decls are just inserted in the scope. */
2836 if (!name)
2838 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2839 locus);
2840 return x;
2843 /* First, see if there is another declaration with the same name in
2844 the current scope. If there is, duplicate_decls may do all the
2845 work for us. If duplicate_decls returns false, that indicates
2846 two incompatible decls in the same scope; we are to silently
2847 replace the old one (duplicate_decls has issued all appropriate
2848 diagnostics). In particular, we should not consider possible
2849 duplicates in the external scope, or shadowing. */
2850 b = I_SYMBOL_BINDING (name);
2851 if (b && B_IN_SCOPE (b, scope))
2853 struct c_binding *b_ext, *b_use;
2854 tree type = TREE_TYPE (x);
2855 tree visdecl = b->decl;
2856 tree vistype = TREE_TYPE (visdecl);
2857 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2858 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2859 b->inner_comp = false;
2860 b_use = b;
2861 b_ext = b;
2862 /* If this is an external linkage declaration, we should check
2863 for compatibility with the type in the external scope before
2864 setting the type at this scope based on the visible
2865 information only. */
2866 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2868 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2869 b_ext = b_ext->shadowed;
2870 if (b_ext)
2872 b_use = b_ext;
2873 if (b_use->u.type)
2874 TREE_TYPE (b_use->decl) = b_use->u.type;
2877 if (duplicate_decls (x, b_use->decl))
2879 if (b_use != b)
2881 /* Save the updated type in the external scope and
2882 restore the proper type for this scope. */
2883 tree thistype;
2884 if (comptypes (vistype, type))
2885 thistype = composite_type (vistype, type);
2886 else
2887 thistype = TREE_TYPE (b_use->decl);
2888 b_use->u.type = TREE_TYPE (b_use->decl);
2889 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2890 && DECL_BUILT_IN (b_use->decl))
2891 thistype
2892 = build_type_attribute_variant (thistype,
2893 TYPE_ATTRIBUTES
2894 (b_use->u.type));
2895 TREE_TYPE (b_use->decl) = thistype;
2897 return b_use->decl;
2899 else
2900 goto skip_external_and_shadow_checks;
2903 /* All declarations with external linkage, and all external
2904 references, go in the external scope, no matter what scope is
2905 current. However, the binding in that scope is ignored for
2906 purposes of normal name lookup. A separate binding structure is
2907 created in the requested scope; this governs the normal
2908 visibility of the symbol.
2910 The binding in the externals scope is used exclusively for
2911 detecting duplicate declarations of the same object, no matter
2912 what scope they are in; this is what we do here. (C99 6.2.7p2:
2913 All declarations that refer to the same object or function shall
2914 have compatible type; otherwise, the behavior is undefined.) */
2915 if (DECL_EXTERNAL (x) || scope == file_scope)
2917 tree type = TREE_TYPE (x);
2918 tree vistype = 0;
2919 tree visdecl = 0;
2920 bool type_saved = false;
2921 if (b && !B_IN_EXTERNAL_SCOPE (b)
2922 && (TREE_CODE (b->decl) == FUNCTION_DECL
2923 || TREE_CODE (b->decl) == VAR_DECL)
2924 && DECL_FILE_SCOPE_P (b->decl))
2926 visdecl = b->decl;
2927 vistype = TREE_TYPE (visdecl);
2929 if (scope != file_scope
2930 && !DECL_IN_SYSTEM_HEADER (x))
2931 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2933 while (b && !B_IN_EXTERNAL_SCOPE (b))
2935 /* If this decl might be modified, save its type. This is
2936 done here rather than when the decl is first bound
2937 because the type may change after first binding, through
2938 being completed or through attributes being added. If we
2939 encounter multiple such decls, only the first should have
2940 its type saved; the others will already have had their
2941 proper types saved and the types will not have changed as
2942 their scopes will not have been re-entered. */
2943 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2945 b->u.type = TREE_TYPE (b->decl);
2946 type_saved = true;
2948 if (B_IN_FILE_SCOPE (b)
2949 && TREE_CODE (b->decl) == VAR_DECL
2950 && TREE_STATIC (b->decl)
2951 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2952 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2953 && TREE_CODE (type) == ARRAY_TYPE
2954 && TYPE_DOMAIN (type)
2955 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2956 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2958 /* Array type completed in inner scope, which should be
2959 diagnosed if the completion does not have size 1 and
2960 it does not get completed in the file scope. */
2961 b->inner_comp = true;
2963 b = b->shadowed;
2966 /* If a matching external declaration has been found, set its
2967 type to the composite of all the types of that declaration.
2968 After the consistency checks, it will be reset to the
2969 composite of the visible types only. */
2970 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2971 && b->u.type)
2972 TREE_TYPE (b->decl) = b->u.type;
2974 /* The point of the same_translation_unit_p check here is,
2975 we want to detect a duplicate decl for a construct like
2976 foo() { extern bar(); } ... static bar(); but not if
2977 they are in different translation units. In any case,
2978 the static does not go in the externals scope. */
2979 if (b
2980 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2981 && duplicate_decls (x, b->decl))
2983 tree thistype;
2984 if (vistype)
2986 if (comptypes (vistype, type))
2987 thistype = composite_type (vistype, type);
2988 else
2989 thistype = TREE_TYPE (b->decl);
2991 else
2992 thistype = type;
2993 b->u.type = TREE_TYPE (b->decl);
2994 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2995 thistype
2996 = build_type_attribute_variant (thistype,
2997 TYPE_ATTRIBUTES (b->u.type));
2998 TREE_TYPE (b->decl) = thistype;
2999 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3000 locus);
3001 return b->decl;
3003 else if (TREE_PUBLIC (x))
3005 if (visdecl && !b && duplicate_decls (x, visdecl))
3007 /* An external declaration at block scope referring to a
3008 visible entity with internal linkage. The composite
3009 type will already be correct for this scope, so we
3010 just need to fall through to make the declaration in
3011 this scope. */
3012 nested = true;
3013 x = visdecl;
3015 else
3017 bind (name, x, external_scope, /*invisible=*/true,
3018 /*nested=*/false, locus);
3019 nested = true;
3024 if (TREE_CODE (x) != PARM_DECL)
3025 warn_if_shadowing (x);
3027 skip_external_and_shadow_checks:
3028 if (TREE_CODE (x) == TYPE_DECL)
3030 /* So this is a typedef, set its underlying type. */
3031 set_underlying_type (x);
3033 /* If X is a typedef defined in the current function, record it
3034 for the purpose of implementing the -Wunused-local-typedefs
3035 warning. */
3036 record_locally_defined_typedef (x);
3039 bind (name, x, scope, /*invisible=*/false, nested, locus);
3041 /* If x's type is incomplete because it's based on a
3042 structure or union which has not yet been fully declared,
3043 attach it to that structure or union type, so we can go
3044 back and complete the variable declaration later, if the
3045 structure or union gets fully declared.
3047 If the input is erroneous, we can have error_mark in the type
3048 slot (e.g. "f(void a, ...)") - that doesn't count as an
3049 incomplete type. */
3050 if (TREE_TYPE (x) != error_mark_node
3051 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3053 tree element = TREE_TYPE (x);
3055 while (TREE_CODE (element) == ARRAY_TYPE)
3056 element = TREE_TYPE (element);
3057 element = TYPE_MAIN_VARIANT (element);
3059 if ((TREE_CODE (element) == RECORD_TYPE
3060 || TREE_CODE (element) == UNION_TYPE)
3061 && (TREE_CODE (x) != TYPE_DECL
3062 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3063 && !COMPLETE_TYPE_P (element))
3064 C_TYPE_INCOMPLETE_VARS (element)
3065 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3067 return x;
3070 /* Record X as belonging to file scope.
3071 This is used only internally by the Objective-C front end,
3072 and is limited to its needs. duplicate_decls is not called;
3073 if there is any preexisting decl for this identifier, it is an ICE. */
3075 tree
3076 pushdecl_top_level (tree x)
3078 tree name;
3079 bool nested = false;
3080 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
3082 name = DECL_NAME (x);
3084 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3086 if (TREE_PUBLIC (x))
3088 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3089 UNKNOWN_LOCATION);
3090 nested = true;
3092 if (file_scope)
3093 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3095 return x;
3098 static void
3099 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3101 if (warn_implicit_function_declaration)
3103 bool warned;
3105 if (flag_isoc99)
3106 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3107 "implicit declaration of function %qE", id);
3108 else
3109 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3110 G_("implicit declaration of function %qE"), id);
3111 if (olddecl && warned)
3112 locate_old_decl (olddecl);
3116 /* This function represents mapping of a function code FCODE
3117 to its respective header. */
3119 static const char *
3120 header_for_builtin_fn (enum built_in_function fcode)
3122 switch (fcode)
3124 CASE_FLT_FN (BUILT_IN_ACOS):
3125 CASE_FLT_FN (BUILT_IN_ACOSH):
3126 CASE_FLT_FN (BUILT_IN_ASIN):
3127 CASE_FLT_FN (BUILT_IN_ASINH):
3128 CASE_FLT_FN (BUILT_IN_ATAN):
3129 CASE_FLT_FN (BUILT_IN_ATANH):
3130 CASE_FLT_FN (BUILT_IN_ATAN2):
3131 CASE_FLT_FN (BUILT_IN_CBRT):
3132 CASE_FLT_FN (BUILT_IN_CEIL):
3133 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3134 CASE_FLT_FN (BUILT_IN_COS):
3135 CASE_FLT_FN (BUILT_IN_COSH):
3136 CASE_FLT_FN (BUILT_IN_ERF):
3137 CASE_FLT_FN (BUILT_IN_ERFC):
3138 CASE_FLT_FN (BUILT_IN_EXP):
3139 CASE_FLT_FN (BUILT_IN_EXP2):
3140 CASE_FLT_FN (BUILT_IN_EXPM1):
3141 CASE_FLT_FN (BUILT_IN_FABS):
3142 CASE_FLT_FN (BUILT_IN_FDIM):
3143 CASE_FLT_FN (BUILT_IN_FLOOR):
3144 CASE_FLT_FN (BUILT_IN_FMA):
3145 CASE_FLT_FN (BUILT_IN_FMAX):
3146 CASE_FLT_FN (BUILT_IN_FMIN):
3147 CASE_FLT_FN (BUILT_IN_FMOD):
3148 CASE_FLT_FN (BUILT_IN_FREXP):
3149 CASE_FLT_FN (BUILT_IN_HYPOT):
3150 CASE_FLT_FN (BUILT_IN_ILOGB):
3151 CASE_FLT_FN (BUILT_IN_LDEXP):
3152 CASE_FLT_FN (BUILT_IN_LGAMMA):
3153 CASE_FLT_FN (BUILT_IN_LLRINT):
3154 CASE_FLT_FN (BUILT_IN_LLROUND):
3155 CASE_FLT_FN (BUILT_IN_LOG):
3156 CASE_FLT_FN (BUILT_IN_LOG10):
3157 CASE_FLT_FN (BUILT_IN_LOG1P):
3158 CASE_FLT_FN (BUILT_IN_LOG2):
3159 CASE_FLT_FN (BUILT_IN_LOGB):
3160 CASE_FLT_FN (BUILT_IN_LRINT):
3161 CASE_FLT_FN (BUILT_IN_LROUND):
3162 CASE_FLT_FN (BUILT_IN_MODF):
3163 CASE_FLT_FN (BUILT_IN_NAN):
3164 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3165 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3166 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3167 CASE_FLT_FN (BUILT_IN_POW):
3168 CASE_FLT_FN (BUILT_IN_REMAINDER):
3169 CASE_FLT_FN (BUILT_IN_REMQUO):
3170 CASE_FLT_FN (BUILT_IN_RINT):
3171 CASE_FLT_FN (BUILT_IN_ROUND):
3172 CASE_FLT_FN (BUILT_IN_SCALBLN):
3173 CASE_FLT_FN (BUILT_IN_SCALBN):
3174 CASE_FLT_FN (BUILT_IN_SIN):
3175 CASE_FLT_FN (BUILT_IN_SINH):
3176 CASE_FLT_FN (BUILT_IN_SINCOS):
3177 CASE_FLT_FN (BUILT_IN_SQRT):
3178 CASE_FLT_FN (BUILT_IN_TAN):
3179 CASE_FLT_FN (BUILT_IN_TANH):
3180 CASE_FLT_FN (BUILT_IN_TGAMMA):
3181 CASE_FLT_FN (BUILT_IN_TRUNC):
3182 case BUILT_IN_ISINF:
3183 case BUILT_IN_ISNAN:
3184 return "<math.h>";
3185 CASE_FLT_FN (BUILT_IN_CABS):
3186 CASE_FLT_FN (BUILT_IN_CACOS):
3187 CASE_FLT_FN (BUILT_IN_CACOSH):
3188 CASE_FLT_FN (BUILT_IN_CARG):
3189 CASE_FLT_FN (BUILT_IN_CASIN):
3190 CASE_FLT_FN (BUILT_IN_CASINH):
3191 CASE_FLT_FN (BUILT_IN_CATAN):
3192 CASE_FLT_FN (BUILT_IN_CATANH):
3193 CASE_FLT_FN (BUILT_IN_CCOS):
3194 CASE_FLT_FN (BUILT_IN_CCOSH):
3195 CASE_FLT_FN (BUILT_IN_CEXP):
3196 CASE_FLT_FN (BUILT_IN_CIMAG):
3197 CASE_FLT_FN (BUILT_IN_CLOG):
3198 CASE_FLT_FN (BUILT_IN_CONJ):
3199 CASE_FLT_FN (BUILT_IN_CPOW):
3200 CASE_FLT_FN (BUILT_IN_CPROJ):
3201 CASE_FLT_FN (BUILT_IN_CREAL):
3202 CASE_FLT_FN (BUILT_IN_CSIN):
3203 CASE_FLT_FN (BUILT_IN_CSINH):
3204 CASE_FLT_FN (BUILT_IN_CSQRT):
3205 CASE_FLT_FN (BUILT_IN_CTAN):
3206 CASE_FLT_FN (BUILT_IN_CTANH):
3207 return "<complex.h>";
3208 case BUILT_IN_MEMCHR:
3209 case BUILT_IN_MEMCMP:
3210 case BUILT_IN_MEMCPY:
3211 case BUILT_IN_MEMMOVE:
3212 case BUILT_IN_MEMSET:
3213 case BUILT_IN_STRCAT:
3214 case BUILT_IN_STRCHR:
3215 case BUILT_IN_STRCMP:
3216 case BUILT_IN_STRCPY:
3217 case BUILT_IN_STRCSPN:
3218 case BUILT_IN_STRLEN:
3219 case BUILT_IN_STRNCAT:
3220 case BUILT_IN_STRNCMP:
3221 case BUILT_IN_STRNCPY:
3222 case BUILT_IN_STRPBRK:
3223 case BUILT_IN_STRRCHR:
3224 case BUILT_IN_STRSPN:
3225 case BUILT_IN_STRSTR:
3226 return "<string.h>";
3227 case BUILT_IN_FPRINTF:
3228 case BUILT_IN_PUTC:
3229 case BUILT_IN_FPUTC:
3230 case BUILT_IN_FPUTS:
3231 case BUILT_IN_FSCANF:
3232 case BUILT_IN_FWRITE:
3233 case BUILT_IN_PRINTF:
3234 case BUILT_IN_PUTCHAR:
3235 case BUILT_IN_PUTS:
3236 case BUILT_IN_SCANF:
3237 case BUILT_IN_SNPRINTF:
3238 case BUILT_IN_SPRINTF:
3239 case BUILT_IN_SSCANF:
3240 case BUILT_IN_VFPRINTF:
3241 case BUILT_IN_VFSCANF:
3242 case BUILT_IN_VPRINTF:
3243 case BUILT_IN_VSCANF:
3244 case BUILT_IN_VSNPRINTF:
3245 case BUILT_IN_VSPRINTF:
3246 case BUILT_IN_VSSCANF:
3247 return "<stdio.h>";
3248 case BUILT_IN_ISALNUM:
3249 case BUILT_IN_ISALPHA:
3250 case BUILT_IN_ISBLANK:
3251 case BUILT_IN_ISCNTRL:
3252 case BUILT_IN_ISDIGIT:
3253 case BUILT_IN_ISGRAPH:
3254 case BUILT_IN_ISLOWER:
3255 case BUILT_IN_ISPRINT:
3256 case BUILT_IN_ISPUNCT:
3257 case BUILT_IN_ISSPACE:
3258 case BUILT_IN_ISUPPER:
3259 case BUILT_IN_ISXDIGIT:
3260 case BUILT_IN_TOLOWER:
3261 case BUILT_IN_TOUPPER:
3262 return "<ctype.h>";
3263 case BUILT_IN_ISWALNUM:
3264 case BUILT_IN_ISWALPHA:
3265 case BUILT_IN_ISWBLANK:
3266 case BUILT_IN_ISWCNTRL:
3267 case BUILT_IN_ISWDIGIT:
3268 case BUILT_IN_ISWGRAPH:
3269 case BUILT_IN_ISWLOWER:
3270 case BUILT_IN_ISWPRINT:
3271 case BUILT_IN_ISWPUNCT:
3272 case BUILT_IN_ISWSPACE:
3273 case BUILT_IN_ISWUPPER:
3274 case BUILT_IN_ISWXDIGIT:
3275 case BUILT_IN_TOWLOWER:
3276 case BUILT_IN_TOWUPPER:
3277 return "<wctype.h>";
3278 case BUILT_IN_ABORT:
3279 case BUILT_IN_ABS:
3280 case BUILT_IN_CALLOC:
3281 case BUILT_IN_EXIT:
3282 case BUILT_IN_FREE:
3283 case BUILT_IN_LABS:
3284 case BUILT_IN_LLABS:
3285 case BUILT_IN_MALLOC:
3286 case BUILT_IN_REALLOC:
3287 case BUILT_IN__EXIT2:
3288 case BUILT_IN_ALIGNED_ALLOC:
3289 return "<stdlib.h>";
3290 case BUILT_IN_IMAXABS:
3291 return "<inttypes.h>";
3292 case BUILT_IN_STRFTIME:
3293 return "<time.h>";
3294 default:
3295 return NULL;
3299 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3300 function of type int (). */
3302 tree
3303 implicitly_declare (location_t loc, tree functionid)
3305 struct c_binding *b;
3306 tree decl = 0;
3307 tree asmspec_tree;
3309 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3311 if (B_IN_SCOPE (b, external_scope))
3313 decl = b->decl;
3314 break;
3318 if (decl)
3320 if (decl == error_mark_node)
3321 return decl;
3323 /* FIXME: Objective-C has weird not-really-builtin functions
3324 which are supposed to be visible automatically. They wind up
3325 in the external scope because they're pushed before the file
3326 scope gets created. Catch this here and rebind them into the
3327 file scope. */
3328 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3330 bind (functionid, decl, file_scope,
3331 /*invisible=*/false, /*nested=*/true,
3332 DECL_SOURCE_LOCATION (decl));
3333 return decl;
3335 else
3337 tree newtype = default_function_type;
3338 if (b->u.type)
3339 TREE_TYPE (decl) = b->u.type;
3340 /* Implicit declaration of a function already declared
3341 (somehow) in a different scope, or as a built-in.
3342 If this is the first time this has happened, warn;
3343 then recycle the old declaration but with the new type. */
3344 if (!C_DECL_IMPLICIT (decl))
3346 implicit_decl_warning (loc, functionid, decl);
3347 C_DECL_IMPLICIT (decl) = 1;
3349 if (DECL_BUILT_IN (decl))
3351 newtype = build_type_attribute_variant (newtype,
3352 TYPE_ATTRIBUTES
3353 (TREE_TYPE (decl)));
3354 if (!comptypes (newtype, TREE_TYPE (decl)))
3356 bool warned = warning_at (loc, 0, "incompatible implicit "
3357 "declaration of built-in "
3358 "function %qD", decl);
3359 /* See if we can hint which header to include. */
3360 const char *header
3361 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3362 if (header != NULL && warned)
3363 inform (loc, "include %qs or provide a declaration of %qD",
3364 header, decl);
3365 newtype = TREE_TYPE (decl);
3368 else
3370 if (!comptypes (newtype, TREE_TYPE (decl)))
3372 error_at (loc, "incompatible implicit declaration of "
3373 "function %qD", decl);
3374 locate_old_decl (decl);
3377 b->u.type = TREE_TYPE (decl);
3378 TREE_TYPE (decl) = newtype;
3379 bind (functionid, decl, current_scope,
3380 /*invisible=*/false, /*nested=*/true,
3381 DECL_SOURCE_LOCATION (decl));
3382 return decl;
3386 /* Not seen before. */
3387 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3388 DECL_EXTERNAL (decl) = 1;
3389 TREE_PUBLIC (decl) = 1;
3390 C_DECL_IMPLICIT (decl) = 1;
3391 implicit_decl_warning (loc, functionid, 0);
3392 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3393 if (asmspec_tree)
3394 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3396 /* C89 says implicit declarations are in the innermost block.
3397 So we record the decl in the standard fashion. */
3398 decl = pushdecl (decl);
3400 /* No need to call objc_check_decl here - it's a function type. */
3401 rest_of_decl_compilation (decl, 0, 0);
3403 /* Write a record describing this implicit function declaration
3404 to the prototypes file (if requested). */
3405 gen_aux_info_record (decl, 0, 1, 0);
3407 /* Possibly apply some default attributes to this implicit declaration. */
3408 decl_attributes (&decl, NULL_TREE, 0);
3410 return decl;
3413 /* Issue an error message for a reference to an undeclared variable
3414 ID, including a reference to a builtin outside of function-call
3415 context. Establish a binding of the identifier to error_mark_node
3416 in an appropriate scope, which will suppress further errors for the
3417 same identifier. The error message should be given location LOC. */
3418 void
3419 undeclared_variable (location_t loc, tree id)
3421 static bool already = false;
3422 struct c_scope *scope;
3424 if (current_function_decl == 0)
3426 error_at (loc, "%qE undeclared here (not in a function)", id);
3427 scope = current_scope;
3429 else
3431 if (!objc_diagnose_private_ivar (id))
3432 error_at (loc, "%qE undeclared (first use in this function)", id);
3433 if (!already)
3435 inform (loc, "each undeclared identifier is reported only"
3436 " once for each function it appears in");
3437 already = true;
3440 /* If we are parsing old-style parameter decls, current_function_decl
3441 will be nonnull but current_function_scope will be null. */
3442 scope = current_function_scope ? current_function_scope : current_scope;
3444 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3445 UNKNOWN_LOCATION);
3448 /* Subroutine of lookup_label, declare_label, define_label: construct a
3449 LABEL_DECL with all the proper frills. Also create a struct
3450 c_label_vars initialized for the current scope. */
3452 static tree
3453 make_label (location_t location, tree name, bool defining,
3454 struct c_label_vars **p_label_vars)
3456 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3457 DECL_CONTEXT (label) = current_function_decl;
3458 DECL_MODE (label) = VOIDmode;
3460 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3461 label_vars->shadowed = NULL;
3462 set_spot_bindings (&label_vars->label_bindings, defining);
3463 label_vars->decls_in_scope = make_tree_vector ();
3464 label_vars->gotos = NULL;
3465 *p_label_vars = label_vars;
3467 return label;
3470 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3471 Create one if none exists so far for the current function.
3472 This is called when a label is used in a goto expression or
3473 has its address taken. */
3475 tree
3476 lookup_label (tree name)
3478 tree label;
3479 struct c_label_vars *label_vars;
3481 if (current_function_scope == 0)
3483 error ("label %qE referenced outside of any function", name);
3484 return 0;
3487 /* Use a label already defined or ref'd with this name, but not if
3488 it is inherited from a containing function and wasn't declared
3489 using __label__. */
3490 label = I_LABEL_DECL (name);
3491 if (label && (DECL_CONTEXT (label) == current_function_decl
3492 || C_DECLARED_LABEL_FLAG (label)))
3494 /* If the label has only been declared, update its apparent
3495 location to point here, for better diagnostics if it
3496 turns out not to have been defined. */
3497 if (DECL_INITIAL (label) == NULL_TREE)
3498 DECL_SOURCE_LOCATION (label) = input_location;
3499 return label;
3502 /* No label binding for that identifier; make one. */
3503 label = make_label (input_location, name, false, &label_vars);
3505 /* Ordinary labels go in the current function scope. */
3506 bind_label (name, label, current_function_scope, label_vars);
3508 return label;
3511 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3512 to LABEL. */
3514 static void
3515 warn_about_goto (location_t goto_loc, tree label, tree decl)
3517 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3518 error_at (goto_loc,
3519 "jump into scope of identifier with variably modified type");
3520 else
3521 warning_at (goto_loc, OPT_Wjump_misses_init,
3522 "jump skips variable initialization");
3523 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3524 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3527 /* Look up a label because of a goto statement. This is like
3528 lookup_label, but also issues any appropriate warnings. */
3530 tree
3531 lookup_label_for_goto (location_t loc, tree name)
3533 tree label;
3534 struct c_label_vars *label_vars;
3535 unsigned int ix;
3536 tree decl;
3538 label = lookup_label (name);
3539 if (label == NULL_TREE)
3540 return NULL_TREE;
3542 /* If we are jumping to a different function, we can't issue any
3543 useful warnings. */
3544 if (DECL_CONTEXT (label) != current_function_decl)
3546 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3547 return label;
3550 label_vars = I_LABEL_BINDING (name)->u.label;
3552 /* If the label has not yet been defined, then push this goto on a
3553 list for possible later warnings. */
3554 if (label_vars->label_bindings.scope == NULL)
3556 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3558 g->loc = loc;
3559 set_spot_bindings (&g->goto_bindings, true);
3560 vec_safe_push (label_vars->gotos, g);
3561 return label;
3564 /* If there are any decls in label_vars->decls_in_scope, then this
3565 goto has missed the declaration of the decl. This happens for a
3566 case like
3567 int i = 1;
3568 lab:
3570 goto lab;
3571 Issue a warning or error. */
3572 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3573 warn_about_goto (loc, label, decl);
3575 if (label_vars->label_bindings.left_stmt_expr)
3577 error_at (loc, "jump into statement expression");
3578 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3581 return label;
3584 /* Make a label named NAME in the current function, shadowing silently
3585 any that may be inherited from containing functions or containing
3586 scopes. This is called for __label__ declarations. */
3588 tree
3589 declare_label (tree name)
3591 struct c_binding *b = I_LABEL_BINDING (name);
3592 tree label;
3593 struct c_label_vars *label_vars;
3595 /* Check to make sure that the label hasn't already been declared
3596 at this scope */
3597 if (b && B_IN_CURRENT_SCOPE (b))
3599 error ("duplicate label declaration %qE", name);
3600 locate_old_decl (b->decl);
3602 /* Just use the previous declaration. */
3603 return b->decl;
3606 label = make_label (input_location, name, false, &label_vars);
3607 C_DECLARED_LABEL_FLAG (label) = 1;
3609 /* Declared labels go in the current scope. */
3610 bind_label (name, label, current_scope, label_vars);
3612 return label;
3615 /* When we define a label, issue any appropriate warnings if there are
3616 any gotos earlier in the function which jump to this label. */
3618 static void
3619 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3621 unsigned int ix;
3622 struct c_goto_bindings *g;
3624 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3626 struct c_binding *b;
3627 struct c_scope *scope;
3629 /* We have a goto to this label. The goto is going forward. In
3630 g->scope, the goto is going to skip any binding which was
3631 defined after g->bindings_in_scope. */
3632 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3634 for (b = g->goto_bindings.scope->bindings;
3635 b != g->goto_bindings.bindings_in_scope;
3636 b = b->prev)
3638 if (decl_jump_unsafe (b->decl))
3639 warn_about_goto (g->loc, label, b->decl);
3643 /* We also need to warn about decls defined in any scopes
3644 between the scope of the label and the scope of the goto. */
3645 for (scope = label_vars->label_bindings.scope;
3646 scope != g->goto_bindings.scope;
3647 scope = scope->outer)
3649 gcc_assert (scope != NULL);
3650 if (scope->has_jump_unsafe_decl)
3652 if (scope == label_vars->label_bindings.scope)
3653 b = label_vars->label_bindings.bindings_in_scope;
3654 else
3655 b = scope->bindings;
3656 for (; b != NULL; b = b->prev)
3658 if (decl_jump_unsafe (b->decl))
3659 warn_about_goto (g->loc, label, b->decl);
3664 if (g->goto_bindings.stmt_exprs > 0)
3666 error_at (g->loc, "jump into statement expression");
3667 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3668 label);
3672 /* Now that the label is defined, we will issue warnings about
3673 subsequent gotos to this label when we see them. */
3674 vec_safe_truncate (label_vars->gotos, 0);
3675 label_vars->gotos = NULL;
3678 /* Define a label, specifying the location in the source file.
3679 Return the LABEL_DECL node for the label, if the definition is valid.
3680 Otherwise return 0. */
3682 tree
3683 define_label (location_t location, tree name)
3685 /* Find any preexisting label with this name. It is an error
3686 if that label has already been defined in this function, or
3687 if there is a containing function with a declared label with
3688 the same name. */
3689 tree label = I_LABEL_DECL (name);
3691 if (label
3692 && ((DECL_CONTEXT (label) == current_function_decl
3693 && DECL_INITIAL (label) != 0)
3694 || (DECL_CONTEXT (label) != current_function_decl
3695 && C_DECLARED_LABEL_FLAG (label))))
3697 error_at (location, "duplicate label %qD", label);
3698 locate_old_decl (label);
3699 return 0;
3701 else if (label && DECL_CONTEXT (label) == current_function_decl)
3703 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3705 /* The label has been used or declared already in this function,
3706 but not defined. Update its location to point to this
3707 definition. */
3708 DECL_SOURCE_LOCATION (label) = location;
3709 set_spot_bindings (&label_vars->label_bindings, true);
3711 /* Issue warnings as required about any goto statements from
3712 earlier in the function. */
3713 check_earlier_gotos (label, label_vars);
3715 else
3717 struct c_label_vars *label_vars;
3719 /* No label binding for that identifier; make one. */
3720 label = make_label (location, name, true, &label_vars);
3722 /* Ordinary labels go in the current function scope. */
3723 bind_label (name, label, current_function_scope, label_vars);
3726 if (!in_system_header_at (input_location) && lookup_name (name))
3727 warning_at (location, OPT_Wtraditional,
3728 "traditional C lacks a separate namespace "
3729 "for labels, identifier %qE conflicts", name);
3731 /* Mark label as having been defined. */
3732 DECL_INITIAL (label) = error_mark_node;
3733 return label;
3736 /* Get the bindings for a new switch statement. This is used to issue
3737 warnings as appropriate for jumps from the switch to case or
3738 default labels. */
3740 struct c_spot_bindings *
3741 c_get_switch_bindings (void)
3743 struct c_spot_bindings *switch_bindings;
3745 switch_bindings = XNEW (struct c_spot_bindings);
3746 set_spot_bindings (switch_bindings, true);
3747 return switch_bindings;
3750 void
3751 c_release_switch_bindings (struct c_spot_bindings *bindings)
3753 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3754 XDELETE (bindings);
3757 /* This is called at the point of a case or default label to issue
3758 warnings about decls as needed. It returns true if it found an
3759 error, not just a warning. */
3761 bool
3762 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3763 location_t switch_loc, location_t case_loc)
3765 bool saw_error;
3766 struct c_scope *scope;
3768 saw_error = false;
3769 for (scope = current_scope;
3770 scope != switch_bindings->scope;
3771 scope = scope->outer)
3773 struct c_binding *b;
3775 gcc_assert (scope != NULL);
3777 if (!scope->has_jump_unsafe_decl)
3778 continue;
3780 for (b = scope->bindings; b != NULL; b = b->prev)
3782 if (decl_jump_unsafe (b->decl))
3784 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3786 saw_error = true;
3787 error_at (case_loc,
3788 ("switch jumps into scope of identifier with "
3789 "variably modified type"));
3791 else
3792 warning_at (case_loc, OPT_Wjump_misses_init,
3793 "switch jumps over variable initialization");
3794 inform (switch_loc, "switch starts here");
3795 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3796 b->decl);
3801 if (switch_bindings->stmt_exprs > 0)
3803 saw_error = true;
3804 error_at (case_loc, "switch jumps into statement expression");
3805 inform (switch_loc, "switch starts here");
3808 return saw_error;
3811 /* Given NAME, an IDENTIFIER_NODE,
3812 return the structure (or union or enum) definition for that name.
3813 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3814 CODE says which kind of type the caller wants;
3815 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3816 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3817 location where the tag was defined.
3818 If the wrong kind of type is found, an error is reported. */
3820 static tree
3821 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3822 location_t *ploc)
3824 struct c_binding *b = I_TAG_BINDING (name);
3825 int thislevel = 0;
3827 if (!b || !b->decl)
3828 return 0;
3830 /* We only care about whether it's in this level if
3831 thislevel_only was set or it might be a type clash. */
3832 if (thislevel_only || TREE_CODE (b->decl) != code)
3834 /* For our purposes, a tag in the external scope is the same as
3835 a tag in the file scope. (Primarily relevant to Objective-C
3836 and its builtin structure tags, which get pushed before the
3837 file scope is created.) */
3838 if (B_IN_CURRENT_SCOPE (b)
3839 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3840 thislevel = 1;
3843 if (thislevel_only && !thislevel)
3844 return 0;
3846 if (TREE_CODE (b->decl) != code)
3848 /* Definition isn't the kind we were looking for. */
3849 pending_invalid_xref = name;
3850 pending_invalid_xref_location = input_location;
3852 /* If in the same binding level as a declaration as a tag
3853 of a different type, this must not be allowed to
3854 shadow that tag, so give the error immediately.
3855 (For example, "struct foo; union foo;" is invalid.) */
3856 if (thislevel)
3857 pending_xref_error ();
3860 if (ploc != NULL)
3861 *ploc = b->locus;
3863 return b->decl;
3866 /* Print an error message now
3867 for a recent invalid struct, union or enum cross reference.
3868 We don't print them immediately because they are not invalid
3869 when used in the `struct foo;' construct for shadowing. */
3871 void
3872 pending_xref_error (void)
3874 if (pending_invalid_xref != 0)
3875 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3876 pending_invalid_xref);
3877 pending_invalid_xref = 0;
3881 /* Look up NAME in the current scope and its superiors
3882 in the namespace of variables, functions and typedefs.
3883 Return a ..._DECL node of some kind representing its definition,
3884 or return 0 if it is undefined. */
3886 tree
3887 lookup_name (tree name)
3889 struct c_binding *b = I_SYMBOL_BINDING (name);
3890 if (b && !b->invisible)
3892 maybe_record_typedef_use (b->decl);
3893 return b->decl;
3895 return 0;
3898 /* Similar to `lookup_name' but look only at the indicated scope. */
3900 static tree
3901 lookup_name_in_scope (tree name, struct c_scope *scope)
3903 struct c_binding *b;
3905 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3906 if (B_IN_SCOPE (b, scope))
3907 return b->decl;
3908 return 0;
3911 /* Create the predefined scalar types of C,
3912 and some nodes representing standard constants (0, 1, (void *) 0).
3913 Initialize the global scope.
3914 Make definitions for built-in primitive functions. */
3916 void
3917 c_init_decl_processing (void)
3919 location_t save_loc = input_location;
3921 /* Initialize reserved words for parser. */
3922 c_parse_init ();
3924 current_function_decl = 0;
3926 gcc_obstack_init (&parser_obstack);
3928 /* Make the externals scope. */
3929 push_scope ();
3930 external_scope = current_scope;
3932 /* Declarations from c_common_nodes_and_builtins must not be associated
3933 with this input file, lest we get differences between using and not
3934 using preprocessed headers. */
3935 input_location = BUILTINS_LOCATION;
3937 c_common_nodes_and_builtins ();
3939 /* In C, comparisons and TRUTH_* expressions have type int. */
3940 truthvalue_type_node = integer_type_node;
3941 truthvalue_true_node = integer_one_node;
3942 truthvalue_false_node = integer_zero_node;
3944 /* Even in C99, which has a real boolean type. */
3945 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3946 boolean_type_node));
3948 input_location = save_loc;
3950 pedantic_lvalues = true;
3952 make_fname_decl = c_make_fname_decl;
3953 start_fname_decls ();
3956 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3957 give the decl, NAME is the initialization string and TYPE_DEP
3958 indicates whether NAME depended on the type of the function. As we
3959 don't yet implement delayed emission of static data, we mark the
3960 decl as emitted so it is not placed in the output. Anything using
3961 it must therefore pull out the STRING_CST initializer directly.
3962 FIXME. */
3964 static tree
3965 c_make_fname_decl (location_t loc, tree id, int type_dep)
3967 const char *name = fname_as_string (type_dep);
3968 tree decl, type, init;
3969 size_t length = strlen (name);
3971 type = build_array_type (char_type_node,
3972 build_index_type (size_int (length)));
3973 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3975 decl = build_decl (loc, VAR_DECL, id, type);
3977 TREE_STATIC (decl) = 1;
3978 TREE_READONLY (decl) = 1;
3979 DECL_ARTIFICIAL (decl) = 1;
3981 init = build_string (length + 1, name);
3982 free (CONST_CAST (char *, name));
3983 TREE_TYPE (init) = type;
3984 DECL_INITIAL (decl) = init;
3986 TREE_USED (decl) = 1;
3988 if (current_function_decl
3989 /* For invalid programs like this:
3991 void foo()
3992 const char* p = __FUNCTION__;
3994 the __FUNCTION__ is believed to appear in K&R style function
3995 parameter declarator. In that case we still don't have
3996 function_scope. */
3997 && (!seen_error () || current_function_scope))
3999 DECL_CONTEXT (decl) = current_function_decl;
4000 bind (id, decl, current_function_scope,
4001 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4004 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4006 return decl;
4009 tree
4010 c_builtin_function (tree decl)
4012 tree type = TREE_TYPE (decl);
4013 tree id = DECL_NAME (decl);
4015 const char *name = IDENTIFIER_POINTER (id);
4016 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4018 /* Should never be called on a symbol with a preexisting meaning. */
4019 gcc_assert (!I_SYMBOL_BINDING (id));
4021 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4022 UNKNOWN_LOCATION);
4024 /* Builtins in the implementation namespace are made visible without
4025 needing to be explicitly declared. See push_file_scope. */
4026 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4028 DECL_CHAIN (decl) = visible_builtins;
4029 visible_builtins = decl;
4032 return decl;
4035 tree
4036 c_builtin_function_ext_scope (tree decl)
4038 tree type = TREE_TYPE (decl);
4039 tree id = DECL_NAME (decl);
4041 const char *name = IDENTIFIER_POINTER (id);
4042 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4044 if (external_scope)
4045 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4046 UNKNOWN_LOCATION);
4048 /* Builtins in the implementation namespace are made visible without
4049 needing to be explicitly declared. See push_file_scope. */
4050 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4052 DECL_CHAIN (decl) = visible_builtins;
4053 visible_builtins = decl;
4056 return decl;
4059 /* Called when a declaration is seen that contains no names to declare.
4060 If its type is a reference to a structure, union or enum inherited
4061 from a containing scope, shadow that tag name for the current scope
4062 with a forward reference.
4063 If its type defines a new named structure or union
4064 or defines an enum, it is valid but we need not do anything here.
4065 Otherwise, it is an error. */
4067 void
4068 shadow_tag (const struct c_declspecs *declspecs)
4070 shadow_tag_warned (declspecs, 0);
4073 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4074 but no pedwarn. */
4075 void
4076 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4078 bool found_tag = false;
4080 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4082 tree value = declspecs->type;
4083 enum tree_code code = TREE_CODE (value);
4085 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4086 /* Used to test also that TYPE_SIZE (value) != 0.
4087 That caused warning for `struct foo;' at top level in the file. */
4089 tree name = TYPE_NAME (value);
4090 tree t;
4092 found_tag = true;
4094 if (declspecs->restrict_p)
4096 error ("invalid use of %<restrict%>");
4097 warned = 1;
4100 if (name == 0)
4102 if (warned != 1 && code != ENUMERAL_TYPE)
4103 /* Empty unnamed enum OK */
4105 pedwarn (input_location, 0,
4106 "unnamed struct/union that defines no instances");
4107 warned = 1;
4110 else if (declspecs->typespec_kind != ctsk_tagdef
4111 && declspecs->typespec_kind != ctsk_tagfirstref
4112 && declspecs->storage_class != csc_none)
4114 if (warned != 1)
4115 pedwarn (input_location, 0,
4116 "empty declaration with storage class specifier "
4117 "does not redeclare tag");
4118 warned = 1;
4119 pending_xref_error ();
4121 else if (declspecs->typespec_kind != ctsk_tagdef
4122 && declspecs->typespec_kind != ctsk_tagfirstref
4123 && (declspecs->const_p
4124 || declspecs->volatile_p
4125 || declspecs->atomic_p
4126 || declspecs->restrict_p
4127 || declspecs->address_space))
4129 if (warned != 1)
4130 pedwarn (input_location, 0,
4131 "empty declaration with type qualifier "
4132 "does not redeclare tag");
4133 warned = 1;
4134 pending_xref_error ();
4136 else if (declspecs->typespec_kind != ctsk_tagdef
4137 && declspecs->typespec_kind != ctsk_tagfirstref
4138 && declspecs->alignas_p)
4140 if (warned != 1)
4141 pedwarn (input_location, 0,
4142 "empty declaration with %<_Alignas%> "
4143 "does not redeclare tag");
4144 warned = 1;
4145 pending_xref_error ();
4147 else
4149 pending_invalid_xref = 0;
4150 t = lookup_tag (code, name, 1, NULL);
4152 if (t == 0)
4154 t = make_node (code);
4155 pushtag (input_location, name, t);
4159 else
4161 if (warned != 1 && !in_system_header_at (input_location))
4163 pedwarn (input_location, 0,
4164 "useless type name in empty declaration");
4165 warned = 1;
4169 else if (warned != 1 && !in_system_header_at (input_location)
4170 && declspecs->typedef_p)
4172 pedwarn (input_location, 0, "useless type name in empty declaration");
4173 warned = 1;
4176 pending_invalid_xref = 0;
4178 if (declspecs->inline_p)
4180 error ("%<inline%> in empty declaration");
4181 warned = 1;
4184 if (declspecs->noreturn_p)
4186 error ("%<_Noreturn%> in empty declaration");
4187 warned = 1;
4190 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4192 error ("%<auto%> in file-scope empty declaration");
4193 warned = 1;
4196 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4198 error ("%<register%> in file-scope empty declaration");
4199 warned = 1;
4202 if (!warned && !in_system_header_at (input_location)
4203 && declspecs->storage_class != csc_none)
4205 warning (0, "useless storage class specifier in empty declaration");
4206 warned = 2;
4209 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4211 warning (0, "useless %qs in empty declaration",
4212 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4213 warned = 2;
4216 if (!warned
4217 && !in_system_header_at (input_location)
4218 && (declspecs->const_p
4219 || declspecs->volatile_p
4220 || declspecs->atomic_p
4221 || declspecs->restrict_p
4222 || declspecs->address_space))
4224 warning (0, "useless type qualifier in empty declaration");
4225 warned = 2;
4228 if (!warned && !in_system_header_at (input_location)
4229 && declspecs->alignas_p)
4231 warning (0, "useless %<_Alignas%> in empty declaration");
4232 warned = 2;
4235 if (warned != 1)
4237 if (!found_tag)
4238 pedwarn (input_location, 0, "empty declaration");
4243 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4244 bits. SPECS represents declaration specifiers that the grammar
4245 only permits to contain type qualifiers and attributes. */
4248 quals_from_declspecs (const struct c_declspecs *specs)
4250 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4251 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4252 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4253 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4254 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4255 gcc_assert (!specs->type
4256 && !specs->decl_attr
4257 && specs->typespec_word == cts_none
4258 && specs->storage_class == csc_none
4259 && !specs->typedef_p
4260 && !specs->explicit_signed_p
4261 && !specs->deprecated_p
4262 && !specs->long_p
4263 && !specs->long_long_p
4264 && !specs->short_p
4265 && !specs->signed_p
4266 && !specs->unsigned_p
4267 && !specs->complex_p
4268 && !specs->inline_p
4269 && !specs->noreturn_p
4270 && !specs->thread_p);
4271 return quals;
4274 /* Construct an array declarator. LOC is the location of the
4275 beginning of the array (usually the opening brace). EXPR is the
4276 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4277 inside the [] (to be applied to the pointer to which a parameter
4278 array is converted). STATIC_P is true if "static" is inside the
4279 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4280 VLA of unspecified length which is nevertheless a complete type,
4281 false otherwise. The field for the contained declarator is left to
4282 be filled in by set_array_declarator_inner. */
4284 struct c_declarator *
4285 build_array_declarator (location_t loc,
4286 tree expr, struct c_declspecs *quals, bool static_p,
4287 bool vla_unspec_p)
4289 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4290 struct c_declarator);
4291 declarator->id_loc = loc;
4292 declarator->kind = cdk_array;
4293 declarator->declarator = 0;
4294 declarator->u.array.dimen = expr;
4295 if (quals)
4297 declarator->u.array.attrs = quals->attrs;
4298 declarator->u.array.quals = quals_from_declspecs (quals);
4300 else
4302 declarator->u.array.attrs = NULL_TREE;
4303 declarator->u.array.quals = 0;
4305 declarator->u.array.static_p = static_p;
4306 declarator->u.array.vla_unspec_p = vla_unspec_p;
4307 if (static_p || quals != NULL)
4308 pedwarn_c90 (loc, OPT_Wpedantic,
4309 "ISO C90 does not support %<static%> or type "
4310 "qualifiers in parameter array declarators");
4311 if (vla_unspec_p)
4312 pedwarn_c90 (loc, OPT_Wpedantic,
4313 "ISO C90 does not support %<[*]%> array declarators");
4314 if (vla_unspec_p)
4316 if (!current_scope->parm_flag)
4318 /* C99 6.7.5.2p4 */
4319 error_at (loc, "%<[*]%> not allowed in other than "
4320 "function prototype scope");
4321 declarator->u.array.vla_unspec_p = false;
4322 return NULL;
4324 current_scope->had_vla_unspec = true;
4326 return declarator;
4329 /* Set the contained declarator of an array declarator. DECL is the
4330 declarator, as constructed by build_array_declarator; INNER is what
4331 appears on the left of the []. */
4333 struct c_declarator *
4334 set_array_declarator_inner (struct c_declarator *decl,
4335 struct c_declarator *inner)
4337 decl->declarator = inner;
4338 return decl;
4341 /* INIT is a constructor that forms DECL's initializer. If the final
4342 element initializes a flexible array field, add the size of that
4343 initializer to DECL's size. */
4345 static void
4346 add_flexible_array_elts_to_size (tree decl, tree init)
4348 tree elt, type;
4350 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4351 return;
4353 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4354 type = TREE_TYPE (elt);
4355 if (TREE_CODE (type) == ARRAY_TYPE
4356 && TYPE_SIZE (type) == NULL_TREE
4357 && TYPE_DOMAIN (type) != NULL_TREE
4358 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4360 complete_array_type (&type, elt, false);
4361 DECL_SIZE (decl)
4362 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4363 DECL_SIZE_UNIT (decl)
4364 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4368 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4369 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4370 before the type name, and set *EXPR_CONST_OPERANDS, if
4371 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4372 appear in a constant expression. */
4374 tree
4375 groktypename (struct c_type_name *type_name, tree *expr,
4376 bool *expr_const_operands)
4378 tree type;
4379 tree attrs = type_name->specs->attrs;
4381 type_name->specs->attrs = NULL_TREE;
4383 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4384 false, NULL, &attrs, expr, expr_const_operands,
4385 DEPRECATED_NORMAL);
4387 /* Apply attributes. */
4388 decl_attributes (&type, attrs, 0);
4390 return type;
4393 /* Wrapper for decl_attributes that adds some implicit attributes
4394 to VAR_DECLs or FUNCTION_DECLs. */
4396 static tree
4397 c_decl_attributes (tree *node, tree attributes, int flags)
4399 /* Add implicit "omp declare target" attribute if requested. */
4400 if (current_omp_declare_target_attribute
4401 && ((TREE_CODE (*node) == VAR_DECL && TREE_STATIC (*node))
4402 || TREE_CODE (*node) == FUNCTION_DECL))
4404 if (TREE_CODE (*node) == VAR_DECL
4405 && ((DECL_CONTEXT (*node)
4406 && TREE_CODE (DECL_CONTEXT (*node)) == FUNCTION_DECL)
4407 || (current_function_decl && !DECL_EXTERNAL (*node))))
4408 error ("%q+D in block scope inside of declare target directive",
4409 *node);
4410 else if (TREE_CODE (*node) == VAR_DECL
4411 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4412 error ("%q+D in declare target directive does not have mappable type",
4413 *node);
4414 else
4415 attributes = tree_cons (get_identifier ("omp declare target"),
4416 NULL_TREE, attributes);
4418 return decl_attributes (node, attributes, flags);
4422 /* Decode a declarator in an ordinary declaration or data definition.
4423 This is called as soon as the type information and variable name
4424 have been parsed, before parsing the initializer if any.
4425 Here we create the ..._DECL node, fill in its type,
4426 and put it on the list of decls for the current context.
4427 The ..._DECL node is returned as the value.
4429 Exception: for arrays where the length is not specified,
4430 the type is left null, to be filled in by `finish_decl'.
4432 Function definitions do not come here; they go to start_function
4433 instead. However, external and forward declarations of functions
4434 do go through here. Structure field declarations are done by
4435 grokfield and not through here. */
4437 tree
4438 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4439 bool initialized, tree attributes)
4441 tree decl;
4442 tree tem;
4443 tree expr = NULL_TREE;
4444 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4446 /* An object declared as __attribute__((deprecated)) suppresses
4447 warnings of uses of other deprecated items. */
4448 if (lookup_attribute ("deprecated", attributes))
4449 deprecated_state = DEPRECATED_SUPPRESS;
4451 decl = grokdeclarator (declarator, declspecs,
4452 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4453 deprecated_state);
4454 if (!decl)
4455 return 0;
4457 if (expr)
4458 add_stmt (fold_convert (void_type_node, expr));
4460 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4461 warning (OPT_Wmain, "%q+D is usually a function", decl);
4463 if (initialized)
4464 /* Is it valid for this decl to have an initializer at all?
4465 If not, set INITIALIZED to zero, which will indirectly
4466 tell 'finish_decl' to ignore the initializer once it is parsed. */
4467 switch (TREE_CODE (decl))
4469 case TYPE_DECL:
4470 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4471 initialized = 0;
4472 break;
4474 case FUNCTION_DECL:
4475 error ("function %qD is initialized like a variable", decl);
4476 initialized = 0;
4477 break;
4479 case PARM_DECL:
4480 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4481 error ("parameter %qD is initialized", decl);
4482 initialized = 0;
4483 break;
4485 default:
4486 /* Don't allow initializations for incomplete types except for
4487 arrays which might be completed by the initialization. */
4489 /* This can happen if the array size is an undefined macro.
4490 We already gave a warning, so we don't need another one. */
4491 if (TREE_TYPE (decl) == error_mark_node)
4492 initialized = 0;
4493 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4495 /* A complete type is ok if size is fixed. */
4497 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4498 || C_DECL_VARIABLE_SIZE (decl))
4500 error ("variable-sized object may not be initialized");
4501 initialized = 0;
4504 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4506 error ("variable %qD has initializer but incomplete type", decl);
4507 initialized = 0;
4509 else if (C_DECL_VARIABLE_SIZE (decl))
4511 /* Although C99 is unclear about whether incomplete arrays
4512 of VLAs themselves count as VLAs, it does not make
4513 sense to permit them to be initialized given that
4514 ordinary VLAs may not be initialized. */
4515 error ("variable-sized object may not be initialized");
4516 initialized = 0;
4520 if (initialized)
4522 if (current_scope == file_scope)
4523 TREE_STATIC (decl) = 1;
4525 /* Tell 'pushdecl' this is an initialized decl
4526 even though we don't yet have the initializer expression.
4527 Also tell 'finish_decl' it may store the real initializer. */
4528 DECL_INITIAL (decl) = error_mark_node;
4531 /* If this is a function declaration, write a record describing it to the
4532 prototypes file (if requested). */
4534 if (TREE_CODE (decl) == FUNCTION_DECL)
4535 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4537 /* ANSI specifies that a tentative definition which is not merged with
4538 a non-tentative definition behaves exactly like a definition with an
4539 initializer equal to zero. (Section 3.7.2)
4541 -fno-common gives strict ANSI behavior, though this tends to break
4542 a large body of code that grew up without this rule.
4544 Thread-local variables are never common, since there's no entrenched
4545 body of code to break, and it allows more efficient variable references
4546 in the presence of dynamic linking. */
4548 if (TREE_CODE (decl) == VAR_DECL
4549 && !initialized
4550 && TREE_PUBLIC (decl)
4551 && !DECL_THREAD_LOCAL_P (decl)
4552 && !flag_no_common)
4553 DECL_COMMON (decl) = 1;
4555 /* Set attributes here so if duplicate decl, will have proper attributes. */
4556 c_decl_attributes (&decl, attributes, 0);
4558 /* Handle gnu_inline attribute. */
4559 if (declspecs->inline_p
4560 && !flag_gnu89_inline
4561 && TREE_CODE (decl) == FUNCTION_DECL
4562 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4563 || current_function_decl))
4565 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4567 else if (declspecs->storage_class != csc_static)
4568 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4571 if (TREE_CODE (decl) == FUNCTION_DECL
4572 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4574 struct c_declarator *ce = declarator;
4576 if (ce->kind == cdk_pointer)
4577 ce = declarator->declarator;
4578 if (ce->kind == cdk_function)
4580 tree args = ce->u.arg_info->parms;
4581 for (; args; args = DECL_CHAIN (args))
4583 tree type = TREE_TYPE (args);
4584 if (type && INTEGRAL_TYPE_P (type)
4585 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4586 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4591 if (TREE_CODE (decl) == FUNCTION_DECL
4592 && DECL_DECLARED_INLINE_P (decl)
4593 && DECL_UNINLINABLE (decl)
4594 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4595 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4596 decl);
4598 /* C99 6.7.4p3: An inline definition of a function with external
4599 linkage shall not contain a definition of a modifiable object
4600 with static storage duration... */
4601 if (TREE_CODE (decl) == VAR_DECL
4602 && current_scope != file_scope
4603 && TREE_STATIC (decl)
4604 && !TREE_READONLY (decl)
4605 && DECL_DECLARED_INLINE_P (current_function_decl)
4606 && DECL_EXTERNAL (current_function_decl))
4607 record_inline_static (input_location, current_function_decl,
4608 decl, csi_modifiable);
4610 if (c_dialect_objc ()
4611 && (TREE_CODE (decl) == VAR_DECL
4612 || TREE_CODE (decl) == FUNCTION_DECL))
4613 objc_check_global_decl (decl);
4615 /* Add this decl to the current scope.
4616 TEM may equal DECL or it may be a previous decl of the same name. */
4617 tem = pushdecl (decl);
4619 if (initialized && DECL_EXTERNAL (tem))
4621 DECL_EXTERNAL (tem) = 0;
4622 TREE_STATIC (tem) = 1;
4625 return tem;
4628 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4629 DECL or the non-array element type if DECL is an uninitialized array.
4630 If that type has a const member, diagnose this. */
4632 static void
4633 diagnose_uninitialized_cst_member (tree decl, tree type)
4635 tree field;
4636 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4638 tree field_type;
4639 if (TREE_CODE (field) != FIELD_DECL)
4640 continue;
4641 field_type = strip_array_types (TREE_TYPE (field));
4643 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4645 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4646 "uninitialized const member in %qT is invalid in C++",
4647 strip_array_types (TREE_TYPE (decl)));
4648 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4651 if (TREE_CODE (field_type) == RECORD_TYPE
4652 || TREE_CODE (field_type) == UNION_TYPE)
4653 diagnose_uninitialized_cst_member (decl, field_type);
4657 /* Finish processing of a declaration;
4658 install its initial value.
4659 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4660 If the length of an array type is not known before,
4661 it must be determined now, from the initial value, or it is an error.
4663 INIT_LOC is the location of the initial value. */
4665 void
4666 finish_decl (tree decl, location_t init_loc, tree init,
4667 tree origtype, tree asmspec_tree)
4669 tree type;
4670 bool was_incomplete = (DECL_SIZE (decl) == 0);
4671 const char *asmspec = 0;
4673 /* If a name was specified, get the string. */
4674 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4675 && DECL_FILE_SCOPE_P (decl))
4676 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4677 if (asmspec_tree)
4678 asmspec = TREE_STRING_POINTER (asmspec_tree);
4680 if (TREE_CODE (decl) == VAR_DECL
4681 && TREE_STATIC (decl)
4682 && global_bindings_p ())
4683 /* So decl is a global variable. Record the types it uses
4684 so that we can decide later to emit debug info for them. */
4685 record_types_used_by_current_var_decl (decl);
4687 /* If `start_decl' didn't like having an initialization, ignore it now. */
4688 if (init != 0 && DECL_INITIAL (decl) == 0)
4689 init = 0;
4691 /* Don't crash if parm is initialized. */
4692 if (TREE_CODE (decl) == PARM_DECL)
4693 init = 0;
4695 if (init)
4696 store_init_value (init_loc, decl, init, origtype);
4698 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4699 || TREE_CODE (decl) == FUNCTION_DECL
4700 || TREE_CODE (decl) == FIELD_DECL))
4701 objc_check_decl (decl);
4703 type = TREE_TYPE (decl);
4705 /* Deduce size of array from initialization, if not already known. */
4706 if (TREE_CODE (type) == ARRAY_TYPE
4707 && TYPE_DOMAIN (type) == 0
4708 && TREE_CODE (decl) != TYPE_DECL)
4710 bool do_default
4711 = (TREE_STATIC (decl)
4712 /* Even if pedantic, an external linkage array
4713 may have incomplete type at first. */
4714 ? pedantic && !TREE_PUBLIC (decl)
4715 : !DECL_EXTERNAL (decl));
4716 int failure
4717 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4718 do_default);
4720 /* Get the completed type made by complete_array_type. */
4721 type = TREE_TYPE (decl);
4723 switch (failure)
4725 case 1:
4726 error ("initializer fails to determine size of %q+D", decl);
4727 break;
4729 case 2:
4730 if (do_default)
4731 error ("array size missing in %q+D", decl);
4732 /* If a `static' var's size isn't known,
4733 make it extern as well as static, so it does not get
4734 allocated.
4735 If it is not `static', then do not mark extern;
4736 finish_incomplete_decl will give it a default size
4737 and it will get allocated. */
4738 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4739 DECL_EXTERNAL (decl) = 1;
4740 break;
4742 case 3:
4743 error ("zero or negative size array %q+D", decl);
4744 break;
4746 case 0:
4747 /* For global variables, update the copy of the type that
4748 exists in the binding. */
4749 if (TREE_PUBLIC (decl))
4751 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4752 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4753 b_ext = b_ext->shadowed;
4754 if (b_ext)
4756 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4757 b_ext->u.type = composite_type (b_ext->u.type, type);
4758 else
4759 b_ext->u.type = type;
4762 break;
4764 default:
4765 gcc_unreachable ();
4768 if (DECL_INITIAL (decl))
4769 TREE_TYPE (DECL_INITIAL (decl)) = type;
4771 relayout_decl (decl);
4774 if (TREE_CODE (decl) == VAR_DECL)
4776 if (init && TREE_CODE (init) == CONSTRUCTOR)
4777 add_flexible_array_elts_to_size (decl, init);
4779 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4780 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4781 layout_decl (decl, 0);
4783 if (DECL_SIZE (decl) == 0
4784 /* Don't give an error if we already gave one earlier. */
4785 && TREE_TYPE (decl) != error_mark_node
4786 && (TREE_STATIC (decl)
4787 /* A static variable with an incomplete type
4788 is an error if it is initialized.
4789 Also if it is not file scope.
4790 Otherwise, let it through, but if it is not `extern'
4791 then it may cause an error message later. */
4792 ? (DECL_INITIAL (decl) != 0
4793 || !DECL_FILE_SCOPE_P (decl))
4794 /* An automatic variable with an incomplete type
4795 is an error. */
4796 : !DECL_EXTERNAL (decl)))
4798 error ("storage size of %q+D isn%'t known", decl);
4799 TREE_TYPE (decl) = error_mark_node;
4802 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4803 && DECL_SIZE (decl) != 0)
4805 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4806 constant_expression_warning (DECL_SIZE (decl));
4807 else
4809 error ("storage size of %q+D isn%'t constant", decl);
4810 TREE_TYPE (decl) = error_mark_node;
4814 if (TREE_USED (type))
4816 TREE_USED (decl) = 1;
4817 DECL_READ_P (decl) = 1;
4821 /* If this is a function and an assembler name is specified, reset DECL_RTL
4822 so we can give it its new name. Also, update builtin_decl if it
4823 was a normal built-in. */
4824 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4826 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4827 set_builtin_user_assembler_name (decl, asmspec);
4828 set_user_assembler_name (decl, asmspec);
4831 /* If #pragma weak was used, mark the decl weak now. */
4832 maybe_apply_pragma_weak (decl);
4834 /* Output the assembler code and/or RTL code for variables and functions,
4835 unless the type is an undefined structure or union.
4836 If not, it will get done when the type is completed. */
4838 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4840 /* Determine the ELF visibility. */
4841 if (TREE_PUBLIC (decl))
4842 c_determine_visibility (decl);
4844 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4845 if (c_dialect_objc ())
4846 objc_check_decl (decl);
4848 if (asmspec)
4850 /* If this is not a static variable, issue a warning.
4851 It doesn't make any sense to give an ASMSPEC for an
4852 ordinary, non-register local variable. Historically,
4853 GCC has accepted -- but ignored -- the ASMSPEC in
4854 this case. */
4855 if (!DECL_FILE_SCOPE_P (decl)
4856 && TREE_CODE (decl) == VAR_DECL
4857 && !C_DECL_REGISTER (decl)
4858 && !TREE_STATIC (decl))
4859 warning (0, "ignoring asm-specifier for non-static local "
4860 "variable %q+D", decl);
4861 else
4862 set_user_assembler_name (decl, asmspec);
4865 if (DECL_FILE_SCOPE_P (decl))
4867 if (DECL_INITIAL (decl) == NULL_TREE
4868 || DECL_INITIAL (decl) == error_mark_node)
4869 /* Don't output anything
4870 when a tentative file-scope definition is seen.
4871 But at end of compilation, do output code for them. */
4872 DECL_DEFER_OUTPUT (decl) = 1;
4873 if (asmspec && C_DECL_REGISTER (decl))
4874 DECL_HARD_REGISTER (decl) = 1;
4875 rest_of_decl_compilation (decl, true, 0);
4877 else
4879 /* In conjunction with an ASMSPEC, the `register'
4880 keyword indicates that we should place the variable
4881 in a particular register. */
4882 if (asmspec && C_DECL_REGISTER (decl))
4884 DECL_HARD_REGISTER (decl) = 1;
4885 /* This cannot be done for a structure with volatile
4886 fields, on which DECL_REGISTER will have been
4887 reset. */
4888 if (!DECL_REGISTER (decl))
4889 error ("cannot put object with volatile field into register");
4892 if (TREE_CODE (decl) != FUNCTION_DECL)
4894 /* If we're building a variable sized type, and we might be
4895 reachable other than via the top of the current binding
4896 level, then create a new BIND_EXPR so that we deallocate
4897 the object at the right time. */
4898 /* Note that DECL_SIZE can be null due to errors. */
4899 if (DECL_SIZE (decl)
4900 && !TREE_CONSTANT (DECL_SIZE (decl))
4901 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4903 tree bind;
4904 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4905 TREE_SIDE_EFFECTS (bind) = 1;
4906 add_stmt (bind);
4907 BIND_EXPR_BODY (bind) = push_stmt_list ();
4909 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4910 DECL_EXPR, decl));
4915 if (!DECL_FILE_SCOPE_P (decl))
4917 /* Recompute the RTL of a local array now
4918 if it used to be an incomplete type. */
4919 if (was_incomplete
4920 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4922 /* If we used it already as memory, it must stay in memory. */
4923 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4924 /* If it's still incomplete now, no init will save it. */
4925 if (DECL_SIZE (decl) == 0)
4926 DECL_INITIAL (decl) = 0;
4931 if (TREE_CODE (decl) == TYPE_DECL)
4933 if (!DECL_FILE_SCOPE_P (decl)
4934 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4935 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4937 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4940 /* Install a cleanup (aka destructor) if one was given. */
4941 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4943 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4944 if (attr)
4946 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4947 tree cleanup_decl = lookup_name (cleanup_id);
4948 tree cleanup;
4949 vec<tree, va_gc> *v;
4951 /* Build "cleanup(&decl)" for the destructor. */
4952 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4953 vec_alloc (v, 1);
4954 v->quick_push (cleanup);
4955 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4956 vNULL, cleanup_decl, v, NULL);
4957 vec_free (v);
4959 /* Don't warn about decl unused; the cleanup uses it. */
4960 TREE_USED (decl) = 1;
4961 TREE_USED (cleanup_decl) = 1;
4962 DECL_READ_P (decl) = 1;
4964 push_cleanup (decl, cleanup, false);
4968 if (warn_cxx_compat
4969 && TREE_CODE (decl) == VAR_DECL
4970 && !DECL_EXTERNAL (decl)
4971 && DECL_INITIAL (decl) == NULL_TREE)
4973 type = strip_array_types (type);
4974 if (TREE_READONLY (decl))
4975 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4976 "uninitialized const %qD is invalid in C++", decl);
4977 else if ((TREE_CODE (type) == RECORD_TYPE
4978 || TREE_CODE (type) == UNION_TYPE)
4979 && C_TYPE_FIELDS_READONLY (type))
4980 diagnose_uninitialized_cst_member (decl, type);
4983 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4986 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4987 EXPR is NULL or a pointer to an expression that needs to be
4988 evaluated for the side effects of array size expressions in the
4989 parameters. */
4991 tree
4992 grokparm (const struct c_parm *parm, tree *expr)
4994 tree attrs = parm->attrs;
4995 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4996 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4998 decl_attributes (&decl, attrs, 0);
5000 return decl;
5003 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5004 and push that on the current scope. EXPR is a pointer to an
5005 expression that needs to be evaluated for the side effects of array
5006 size expressions in the parameters. */
5008 void
5009 push_parm_decl (const struct c_parm *parm, tree *expr)
5011 tree attrs = parm->attrs;
5012 tree decl;
5014 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5015 &attrs, expr, NULL, DEPRECATED_NORMAL);
5016 decl_attributes (&decl, attrs, 0);
5018 decl = pushdecl (decl);
5020 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5023 /* Mark all the parameter declarations to date as forward decls.
5024 Also diagnose use of this extension. */
5026 void
5027 mark_forward_parm_decls (void)
5029 struct c_binding *b;
5031 if (pedantic && !current_scope->warned_forward_parm_decls)
5033 pedwarn (input_location, OPT_Wpedantic,
5034 "ISO C forbids forward parameter declarations");
5035 current_scope->warned_forward_parm_decls = true;
5038 for (b = current_scope->bindings; b; b = b->prev)
5039 if (TREE_CODE (b->decl) == PARM_DECL)
5040 TREE_ASM_WRITTEN (b->decl) = 1;
5043 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5044 literal, which may be an incomplete array type completed by the
5045 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5046 literal. NON_CONST is true if the initializers contain something
5047 that cannot occur in a constant expression. */
5049 tree
5050 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5052 /* We do not use start_decl here because we have a type, not a declarator;
5053 and do not use finish_decl because the decl should be stored inside
5054 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5055 tree decl;
5056 tree complit;
5057 tree stmt;
5059 if (type == error_mark_node
5060 || init == error_mark_node)
5061 return error_mark_node;
5063 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5064 DECL_EXTERNAL (decl) = 0;
5065 TREE_PUBLIC (decl) = 0;
5066 TREE_STATIC (decl) = (current_scope == file_scope);
5067 DECL_CONTEXT (decl) = current_function_decl;
5068 TREE_USED (decl) = 1;
5069 DECL_READ_P (decl) = 1;
5070 TREE_TYPE (decl) = type;
5071 TREE_READONLY (decl) = (TYPE_READONLY (type)
5072 || (TREE_CODE (type) == ARRAY_TYPE
5073 && TYPE_READONLY (TREE_TYPE (type))));
5074 store_init_value (loc, decl, init, NULL_TREE);
5076 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5078 int failure = complete_array_type (&TREE_TYPE (decl),
5079 DECL_INITIAL (decl), true);
5080 /* If complete_array_type returns 3, it means that the
5081 initial value of the compound literal is empty. Allow it. */
5082 gcc_assert (failure == 0 || failure == 3);
5084 type = TREE_TYPE (decl);
5085 TREE_TYPE (DECL_INITIAL (decl)) = type;
5088 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5090 c_incomplete_type_error (NULL_TREE, type);
5091 return error_mark_node;
5094 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5095 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5096 TREE_SIDE_EFFECTS (complit) = 1;
5098 layout_decl (decl, 0);
5100 if (TREE_STATIC (decl))
5102 /* This decl needs a name for the assembler output. */
5103 set_compound_literal_name (decl);
5104 DECL_DEFER_OUTPUT (decl) = 1;
5105 DECL_COMDAT (decl) = 1;
5106 DECL_ARTIFICIAL (decl) = 1;
5107 DECL_IGNORED_P (decl) = 1;
5108 pushdecl (decl);
5109 rest_of_decl_compilation (decl, 1, 0);
5112 if (non_const)
5114 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5115 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5118 return complit;
5121 /* Check the type of a compound literal. Here we just check that it
5122 is valid for C++. */
5124 void
5125 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5127 if (warn_cxx_compat
5128 && (type_name->specs->typespec_kind == ctsk_tagdef
5129 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5130 warning_at (loc, OPT_Wc___compat,
5131 "defining a type in a compound literal is invalid in C++");
5134 /* Determine whether TYPE is a structure with a flexible array member,
5135 or a union containing such a structure (possibly recursively). */
5137 static bool
5138 flexible_array_type_p (tree type)
5140 tree x;
5141 switch (TREE_CODE (type))
5143 case RECORD_TYPE:
5144 x = TYPE_FIELDS (type);
5145 if (x == NULL_TREE)
5146 return false;
5147 while (DECL_CHAIN (x) != NULL_TREE)
5148 x = DECL_CHAIN (x);
5149 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5150 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5151 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5152 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5153 return true;
5154 return false;
5155 case UNION_TYPE:
5156 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5158 if (flexible_array_type_p (TREE_TYPE (x)))
5159 return true;
5161 return false;
5162 default:
5163 return false;
5167 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5168 replacing with appropriate values if they are invalid. */
5169 static void
5170 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
5172 tree type_mv;
5173 unsigned int max_width;
5174 unsigned HOST_WIDE_INT w;
5175 const char *name = (orig_name
5176 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5177 : _("<anonymous>"));
5179 /* Detect and ignore out of range field width and process valid
5180 field widths. */
5181 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5183 error ("bit-field %qs width not an integer constant", name);
5184 *width = integer_one_node;
5186 else
5188 if (TREE_CODE (*width) != INTEGER_CST)
5190 *width = c_fully_fold (*width, false, NULL);
5191 if (TREE_CODE (*width) == INTEGER_CST)
5192 pedwarn (input_location, OPT_Wpedantic,
5193 "bit-field %qs width not an integer constant expression",
5194 name);
5196 if (TREE_CODE (*width) != INTEGER_CST)
5198 error ("bit-field %qs width not an integer constant", name);
5199 *width = integer_one_node;
5201 constant_expression_warning (*width);
5202 if (tree_int_cst_sgn (*width) < 0)
5204 error ("negative width in bit-field %qs", name);
5205 *width = integer_one_node;
5207 else if (integer_zerop (*width) && orig_name)
5209 error ("zero width for bit-field %qs", name);
5210 *width = integer_one_node;
5214 /* Detect invalid bit-field type. */
5215 if (TREE_CODE (*type) != INTEGER_TYPE
5216 && TREE_CODE (*type) != BOOLEAN_TYPE
5217 && TREE_CODE (*type) != ENUMERAL_TYPE)
5219 error ("bit-field %qs has invalid type", name);
5220 *type = unsigned_type_node;
5223 type_mv = TYPE_MAIN_VARIANT (*type);
5224 if (!in_system_header_at (input_location)
5225 && type_mv != integer_type_node
5226 && type_mv != unsigned_type_node
5227 && type_mv != boolean_type_node)
5228 pedwarn_c90 (input_location, OPT_Wpedantic,
5229 "type of bit-field %qs is a GCC extension", name);
5231 max_width = TYPE_PRECISION (*type);
5233 if (0 < compare_tree_int (*width, max_width))
5235 error ("width of %qs exceeds its type", name);
5236 w = max_width;
5237 *width = build_int_cst (integer_type_node, w);
5239 else
5240 w = tree_to_uhwi (*width);
5242 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5244 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5245 if (!lt
5246 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5247 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5248 warning (0, "%qs is narrower than values of its type", name);
5254 /* Print warning about variable length array if necessary. */
5256 static void
5257 warn_variable_length_array (tree name, tree size)
5259 if (TREE_CONSTANT (size))
5261 if (name)
5262 pedwarn_c90 (input_location, OPT_Wvla,
5263 "ISO C90 forbids array %qE whose size "
5264 "can%'t be evaluated", name);
5265 else
5266 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5267 "whose size can%'t be evaluated");
5269 else
5271 if (name)
5272 pedwarn_c90 (input_location, OPT_Wvla,
5273 "ISO C90 forbids variable length array %qE", name);
5274 else
5275 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5276 "length array");
5280 /* Print warning about defaulting to int if necessary. */
5282 static void
5283 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5285 diagnostic_info diagnostic;
5286 va_list ap;
5288 va_start (ap, gmsgid);
5289 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
5290 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5291 diagnostic.option_index = opt;
5292 report_diagnostic (&diagnostic);
5293 va_end (ap);
5296 /* Given declspecs and a declarator,
5297 determine the name and type of the object declared
5298 and construct a ..._DECL node for it.
5299 (In one case we can return a ..._TYPE node instead.
5300 For invalid input we sometimes return 0.)
5302 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5304 DECL_CONTEXT says which syntactic context this declaration is in:
5305 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5306 FUNCDEF for a function definition. Like NORMAL but a few different
5307 error messages in each case. Return value may be zero meaning
5308 this definition is too screwy to try to parse.
5309 PARM for a parameter declaration (either within a function prototype
5310 or before a function body). Make a PARM_DECL, or return void_type_node.
5311 TYPENAME if for a typename (in a cast or sizeof).
5312 Don't make a DECL node; just return the ..._TYPE node.
5313 FIELD for a struct or union field; make a FIELD_DECL.
5314 INITIALIZED is true if the decl has an initializer.
5315 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5316 representing the width of the bit-field.
5317 DECL_ATTRS points to the list of attributes that should be added to this
5318 decl. Any nested attributes that belong on the decl itself will be
5319 added to this list.
5320 If EXPR is not NULL, any expressions that need to be evaluated as
5321 part of evaluating variably modified types will be stored in *EXPR.
5322 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5323 set to indicate whether operands in *EXPR can be used in constant
5324 expressions.
5325 DEPRECATED_STATE is a deprecated_states value indicating whether
5326 deprecation warnings should be suppressed.
5328 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5329 It may also be so in the PARM case, for a prototype where the
5330 argument type is specified but not the name.
5332 This function is where the complicated C meanings of `static'
5333 and `extern' are interpreted. */
5335 static tree
5336 grokdeclarator (const struct c_declarator *declarator,
5337 struct c_declspecs *declspecs,
5338 enum decl_context decl_context, bool initialized, tree *width,
5339 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5340 enum deprecated_states deprecated_state)
5342 tree type = declspecs->type;
5343 bool threadp = declspecs->thread_p;
5344 enum c_storage_class storage_class = declspecs->storage_class;
5345 int constp;
5346 int restrictp;
5347 int volatilep;
5348 int atomicp;
5349 int type_quals = TYPE_UNQUALIFIED;
5350 tree name = NULL_TREE;
5351 bool funcdef_flag = false;
5352 bool funcdef_syntax = false;
5353 bool size_varies = false;
5354 tree decl_attr = declspecs->decl_attr;
5355 int array_ptr_quals = TYPE_UNQUALIFIED;
5356 tree array_ptr_attrs = NULL_TREE;
5357 int array_parm_static = 0;
5358 bool array_parm_vla_unspec_p = false;
5359 tree returned_attrs = NULL_TREE;
5360 bool bitfield = width != NULL;
5361 tree element_type;
5362 struct c_arg_info *arg_info = 0;
5363 addr_space_t as1, as2, address_space;
5364 location_t loc = UNKNOWN_LOCATION;
5365 const char *errmsg;
5366 tree expr_dummy;
5367 bool expr_const_operands_dummy;
5368 enum c_declarator_kind first_non_attr_kind;
5369 unsigned int alignas_align = 0;
5371 if (TREE_CODE (type) == ERROR_MARK)
5372 return error_mark_node;
5373 if (expr == NULL)
5374 expr = &expr_dummy;
5375 if (expr_const_operands == NULL)
5376 expr_const_operands = &expr_const_operands_dummy;
5378 *expr = declspecs->expr;
5379 *expr_const_operands = declspecs->expr_const_operands;
5381 if (decl_context == FUNCDEF)
5382 funcdef_flag = true, decl_context = NORMAL;
5384 /* Look inside a declarator for the name being declared
5385 and get it as an IDENTIFIER_NODE, for an error message. */
5387 const struct c_declarator *decl = declarator;
5389 first_non_attr_kind = cdk_attrs;
5390 while (decl)
5391 switch (decl->kind)
5393 case cdk_array:
5394 loc = decl->id_loc;
5395 /* FALL THRU. */
5397 case cdk_function:
5398 case cdk_pointer:
5399 funcdef_syntax = (decl->kind == cdk_function);
5400 decl = decl->declarator;
5401 if (first_non_attr_kind == cdk_attrs)
5402 first_non_attr_kind = decl->kind;
5403 break;
5405 case cdk_attrs:
5406 decl = decl->declarator;
5407 break;
5409 case cdk_id:
5410 loc = decl->id_loc;
5411 if (decl->u.id)
5412 name = decl->u.id;
5413 if (first_non_attr_kind == cdk_attrs)
5414 first_non_attr_kind = decl->kind;
5415 decl = 0;
5416 break;
5418 default:
5419 gcc_unreachable ();
5421 if (name == 0)
5423 gcc_assert (decl_context == PARM
5424 || decl_context == TYPENAME
5425 || (decl_context == FIELD
5426 && declarator->kind == cdk_id));
5427 gcc_assert (!initialized);
5431 /* A function definition's declarator must have the form of
5432 a function declarator. */
5434 if (funcdef_flag && !funcdef_syntax)
5435 return 0;
5437 /* If this looks like a function definition, make it one,
5438 even if it occurs where parms are expected.
5439 Then store_parm_decls will reject it and not use it as a parm. */
5440 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5441 decl_context = PARM;
5443 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5444 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5446 if ((decl_context == NORMAL || decl_context == FIELD)
5447 && current_scope == file_scope
5448 && variably_modified_type_p (type, NULL_TREE))
5450 if (name)
5451 error_at (loc, "variably modified %qE at file scope", name);
5452 else
5453 error_at (loc, "variably modified field at file scope");
5454 type = integer_type_node;
5457 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5459 /* Diagnose defaulting to "int". */
5461 if (declspecs->default_int_p && !in_system_header_at (input_location))
5463 /* Issue a warning if this is an ISO C 99 program or if
5464 -Wreturn-type and this is a function, or if -Wimplicit;
5465 prefer the former warning since it is more explicit. */
5466 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5467 && funcdef_flag)
5468 warn_about_return_type = 1;
5469 else
5471 if (name)
5472 warn_defaults_to (loc, OPT_Wimplicit_int,
5473 "type defaults to %<int%> in declaration "
5474 "of %qE", name);
5475 else
5476 warn_defaults_to (loc, OPT_Wimplicit_int,
5477 "type defaults to %<int%> in type name");
5481 /* Adjust the type if a bit-field is being declared,
5482 -funsigned-bitfields applied and the type is not explicitly
5483 "signed". */
5484 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5485 && TREE_CODE (type) == INTEGER_TYPE)
5486 type = unsigned_type_for (type);
5488 /* Figure out the type qualifiers for the declaration. There are
5489 two ways a declaration can become qualified. One is something
5490 like `const int i' where the `const' is explicit. Another is
5491 something like `typedef const int CI; CI i' where the type of the
5492 declaration contains the `const'. A third possibility is that
5493 there is a type qualifier on the element type of a typedefed
5494 array type, in which case we should extract that qualifier so
5495 that c_apply_type_quals_to_decl receives the full list of
5496 qualifiers to work with (C90 is not entirely clear about whether
5497 duplicate qualifiers should be diagnosed in this case, but it
5498 seems most appropriate to do so). */
5499 element_type = strip_array_types (type);
5500 constp = declspecs->const_p + TYPE_READONLY (element_type);
5501 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5502 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5503 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5504 as1 = declspecs->address_space;
5505 as2 = TYPE_ADDR_SPACE (element_type);
5506 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5508 if (constp > 1)
5509 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5510 if (restrictp > 1)
5511 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5512 if (volatilep > 1)
5513 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5514 if (atomicp > 1)
5515 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5517 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5518 error_at (loc, "conflicting named address spaces (%s vs %s)",
5519 c_addr_space_name (as1), c_addr_space_name (as2));
5521 if ((TREE_CODE (type) == ARRAY_TYPE
5522 || first_non_attr_kind == cdk_array)
5523 && TYPE_QUALS (element_type))
5524 type = TYPE_MAIN_VARIANT (type);
5525 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5526 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5527 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5528 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5529 | ENCODE_QUAL_ADDR_SPACE (address_space));
5531 /* Applying the _Atomic qualifier to an array type (through the use
5532 of typedefs or typeof) must be detected here. If the qualifier
5533 is introduced later, any appearance of applying it to an array is
5534 actually applying it to an element of that array. */
5535 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5536 error_at (loc, "%<_Atomic%>-qualified array type");
5538 /* Warn about storage classes that are invalid for certain
5539 kinds of declarations (parameters, typenames, etc.). */
5541 if (funcdef_flag
5542 && (threadp
5543 || storage_class == csc_auto
5544 || storage_class == csc_register
5545 || storage_class == csc_typedef))
5547 if (storage_class == csc_auto)
5548 pedwarn (loc,
5549 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5550 "function definition declared %<auto%>");
5551 if (storage_class == csc_register)
5552 error_at (loc, "function definition declared %<register%>");
5553 if (storage_class == csc_typedef)
5554 error_at (loc, "function definition declared %<typedef%>");
5555 if (threadp)
5556 error_at (loc, "function definition declared %qs",
5557 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5558 threadp = false;
5559 if (storage_class == csc_auto
5560 || storage_class == csc_register
5561 || storage_class == csc_typedef)
5562 storage_class = csc_none;
5564 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5566 if (decl_context == PARM && storage_class == csc_register)
5568 else
5570 switch (decl_context)
5572 case FIELD:
5573 if (name)
5574 error_at (loc, "storage class specified for structure "
5575 "field %qE", name);
5576 else
5577 error_at (loc, "storage class specified for structure field");
5578 break;
5579 case PARM:
5580 if (name)
5581 error_at (loc, "storage class specified for parameter %qE",
5582 name);
5583 else
5584 error_at (loc, "storage class specified for unnamed parameter");
5585 break;
5586 default:
5587 error_at (loc, "storage class specified for typename");
5588 break;
5590 storage_class = csc_none;
5591 threadp = false;
5594 else if (storage_class == csc_extern
5595 && initialized
5596 && !funcdef_flag)
5598 /* 'extern' with initialization is invalid if not at file scope. */
5599 if (current_scope == file_scope)
5601 /* It is fine to have 'extern const' when compiling at C
5602 and C++ intersection. */
5603 if (!(warn_cxx_compat && constp))
5604 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5605 name);
5607 else
5608 error_at (loc, "%qE has both %<extern%> and initializer", name);
5610 else if (current_scope == file_scope)
5612 if (storage_class == csc_auto)
5613 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5614 name);
5615 if (pedantic && storage_class == csc_register)
5616 pedwarn (input_location, OPT_Wpedantic,
5617 "file-scope declaration of %qE specifies %<register%>", name);
5619 else
5621 if (storage_class == csc_extern && funcdef_flag)
5622 error_at (loc, "nested function %qE declared %<extern%>", name);
5623 else if (threadp && storage_class == csc_none)
5625 error_at (loc, "function-scope %qE implicitly auto and declared "
5626 "%qs", name,
5627 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5628 threadp = false;
5632 /* Now figure out the structure of the declarator proper.
5633 Descend through it, creating more complex types, until we reach
5634 the declared identifier (or NULL_TREE, in an absolute declarator).
5635 At each stage we maintain an unqualified version of the type
5636 together with any qualifiers that should be applied to it with
5637 c_build_qualified_type; this way, array types including
5638 multidimensional array types are first built up in unqualified
5639 form and then the qualified form is created with
5640 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5642 while (declarator && declarator->kind != cdk_id)
5644 if (type == error_mark_node)
5646 declarator = declarator->declarator;
5647 continue;
5650 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5651 a cdk_pointer (for *...),
5652 a cdk_function (for ...(...)),
5653 a cdk_attrs (for nested attributes),
5654 or a cdk_id (for the name being declared
5655 or the place in an absolute declarator
5656 where the name was omitted).
5657 For the last case, we have just exited the loop.
5659 At this point, TYPE is the type of elements of an array,
5660 or for a function to return, or for a pointer to point to.
5661 After this sequence of ifs, TYPE is the type of the
5662 array or function or pointer, and DECLARATOR has had its
5663 outermost layer removed. */
5665 if (array_ptr_quals != TYPE_UNQUALIFIED
5666 || array_ptr_attrs != NULL_TREE
5667 || array_parm_static)
5669 /* Only the innermost declarator (making a parameter be of
5670 array type which is converted to pointer type)
5671 may have static or type qualifiers. */
5672 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5673 array_ptr_quals = TYPE_UNQUALIFIED;
5674 array_ptr_attrs = NULL_TREE;
5675 array_parm_static = 0;
5678 switch (declarator->kind)
5680 case cdk_attrs:
5682 /* A declarator with embedded attributes. */
5683 tree attrs = declarator->u.attrs;
5684 const struct c_declarator *inner_decl;
5685 int attr_flags = 0;
5686 declarator = declarator->declarator;
5687 inner_decl = declarator;
5688 while (inner_decl->kind == cdk_attrs)
5689 inner_decl = inner_decl->declarator;
5690 if (inner_decl->kind == cdk_id)
5691 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5692 else if (inner_decl->kind == cdk_function)
5693 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5694 else if (inner_decl->kind == cdk_array)
5695 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5696 returned_attrs = decl_attributes (&type,
5697 chainon (returned_attrs, attrs),
5698 attr_flags);
5699 break;
5701 case cdk_array:
5703 tree itype = NULL_TREE;
5704 tree size = declarator->u.array.dimen;
5705 /* The index is a signed object `sizetype' bits wide. */
5706 tree index_type = c_common_signed_type (sizetype);
5708 array_ptr_quals = declarator->u.array.quals;
5709 array_ptr_attrs = declarator->u.array.attrs;
5710 array_parm_static = declarator->u.array.static_p;
5711 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5713 declarator = declarator->declarator;
5715 /* Check for some types that there cannot be arrays of. */
5717 if (VOID_TYPE_P (type))
5719 if (name)
5720 error_at (loc, "declaration of %qE as array of voids", name);
5721 else
5722 error_at (loc, "declaration of type name as array of voids");
5723 type = error_mark_node;
5726 if (TREE_CODE (type) == FUNCTION_TYPE)
5728 if (name)
5729 error_at (loc, "declaration of %qE as array of functions",
5730 name);
5731 else
5732 error_at (loc, "declaration of type name as array of "
5733 "functions");
5734 type = error_mark_node;
5737 if (pedantic && !in_system_header_at (input_location)
5738 && flexible_array_type_p (type))
5739 pedwarn (loc, OPT_Wpedantic,
5740 "invalid use of structure with flexible array member");
5742 if (size == error_mark_node)
5743 type = error_mark_node;
5745 if (type == error_mark_node)
5746 continue;
5748 /* If size was specified, set ITYPE to a range-type for
5749 that size. Otherwise, ITYPE remains null. finish_decl
5750 may figure it out from an initial value. */
5752 if (size)
5754 bool size_maybe_const = true;
5755 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5756 && !TREE_OVERFLOW (size));
5757 bool this_size_varies = false;
5759 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5760 lvalue. */
5761 STRIP_TYPE_NOPS (size);
5763 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5765 if (name)
5766 error_at (loc, "size of array %qE has non-integer type",
5767 name);
5768 else
5769 error_at (loc,
5770 "size of unnamed array has non-integer type");
5771 size = integer_one_node;
5774 size = c_fully_fold (size, false, &size_maybe_const);
5776 if (pedantic && size_maybe_const && integer_zerop (size))
5778 if (name)
5779 pedwarn (loc, OPT_Wpedantic,
5780 "ISO C forbids zero-size array %qE", name);
5781 else
5782 pedwarn (loc, OPT_Wpedantic,
5783 "ISO C forbids zero-size array");
5786 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5788 constant_expression_warning (size);
5789 if (tree_int_cst_sgn (size) < 0)
5791 if (name)
5792 error_at (loc, "size of array %qE is negative", name);
5793 else
5794 error_at (loc, "size of unnamed array is negative");
5795 size = integer_one_node;
5797 /* Handle a size folded to an integer constant but
5798 not an integer constant expression. */
5799 if (!size_int_const)
5801 /* If this is a file scope declaration of an
5802 ordinary identifier, this is invalid code;
5803 diagnosing it here and not subsequently
5804 treating the type as variable-length avoids
5805 more confusing diagnostics later. */
5806 if ((decl_context == NORMAL || decl_context == FIELD)
5807 && current_scope == file_scope)
5808 pedwarn (input_location, 0,
5809 "variably modified %qE at file scope",
5810 name);
5811 else
5812 this_size_varies = size_varies = true;
5813 warn_variable_length_array (name, size);
5816 else if ((decl_context == NORMAL || decl_context == FIELD)
5817 && current_scope == file_scope)
5819 error_at (loc, "variably modified %qE at file scope", name);
5820 size = integer_one_node;
5822 else
5824 /* Make sure the array size remains visibly
5825 nonconstant even if it is (eg) a const variable
5826 with known value. */
5827 this_size_varies = size_varies = true;
5828 warn_variable_length_array (name, size);
5829 if (flag_sanitize & SANITIZE_VLA
5830 && decl_context == NORMAL
5831 && current_function_decl != NULL_TREE
5832 && !lookup_attribute ("no_sanitize_undefined",
5833 DECL_ATTRIBUTES
5834 (current_function_decl)))
5836 /* Evaluate the array size only once. */
5837 size = c_save_expr (size);
5838 size = c_fully_fold (size, false, NULL);
5839 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5840 ubsan_instrument_vla (loc, size),
5841 size);
5845 if (integer_zerop (size) && !this_size_varies)
5847 /* A zero-length array cannot be represented with
5848 an unsigned index type, which is what we'll
5849 get with build_index_type. Create an
5850 open-ended range instead. */
5851 itype = build_range_type (sizetype, size, NULL_TREE);
5853 else
5855 /* Arrange for the SAVE_EXPR on the inside of the
5856 MINUS_EXPR, which allows the -1 to get folded
5857 with the +1 that happens when building TYPE_SIZE. */
5858 if (size_varies)
5859 size = save_expr (size);
5860 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5861 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5862 integer_zero_node, size);
5864 /* Compute the maximum valid index, that is, size
5865 - 1. Do the calculation in index_type, so that
5866 if it is a variable the computations will be
5867 done in the proper mode. */
5868 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5869 convert (index_type, size),
5870 convert (index_type,
5871 size_one_node));
5873 /* The above overflows when size does not fit
5874 in index_type.
5875 ??? While a size of INT_MAX+1 technically shouldn't
5876 cause an overflow (because we subtract 1), handling
5877 this case seems like an unnecessary complication. */
5878 if (TREE_CODE (size) == INTEGER_CST
5879 && !int_fits_type_p (size, index_type))
5881 if (name)
5882 error_at (loc, "size of array %qE is too large",
5883 name);
5884 else
5885 error_at (loc, "size of unnamed array is too large");
5886 type = error_mark_node;
5887 continue;
5890 itype = build_index_type (itype);
5892 if (this_size_varies)
5894 if (*expr)
5895 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5896 *expr, size);
5897 else
5898 *expr = size;
5899 *expr_const_operands &= size_maybe_const;
5902 else if (decl_context == FIELD)
5904 bool flexible_array_member = false;
5905 if (array_parm_vla_unspec_p)
5906 /* Field names can in fact have function prototype
5907 scope so [*] is disallowed here through making
5908 the field variably modified, not through being
5909 something other than a declaration with function
5910 prototype scope. */
5911 size_varies = true;
5912 else
5914 const struct c_declarator *t = declarator;
5915 while (t->kind == cdk_attrs)
5916 t = t->declarator;
5917 flexible_array_member = (t->kind == cdk_id);
5919 if (flexible_array_member
5920 && !in_system_header_at (input_location))
5921 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
5922 "support flexible array members");
5924 /* ISO C99 Flexible array members are effectively
5925 identical to GCC's zero-length array extension. */
5926 if (flexible_array_member || array_parm_vla_unspec_p)
5927 itype = build_range_type (sizetype, size_zero_node,
5928 NULL_TREE);
5930 else if (decl_context == PARM)
5932 if (array_parm_vla_unspec_p)
5934 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5935 size_varies = true;
5938 else if (decl_context == TYPENAME)
5940 if (array_parm_vla_unspec_p)
5942 /* C99 6.7.5.2p4 */
5943 warning (0, "%<[*]%> not in a declaration");
5944 /* We use this to avoid messing up with incomplete
5945 array types of the same type, that would
5946 otherwise be modified below. */
5947 itype = build_range_type (sizetype, size_zero_node,
5948 NULL_TREE);
5949 size_varies = true;
5953 /* Complain about arrays of incomplete types. */
5954 if (!COMPLETE_TYPE_P (type))
5956 error_at (loc, "array type has incomplete element type");
5957 type = error_mark_node;
5959 else
5960 /* When itype is NULL, a shared incomplete array type is
5961 returned for all array of a given type. Elsewhere we
5962 make sure we don't complete that type before copying
5963 it, but here we want to make sure we don't ever
5964 modify the shared type, so we gcc_assert (itype)
5965 below. */
5967 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5968 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5969 type = build_qualified_type (type,
5970 ENCODE_QUAL_ADDR_SPACE (as));
5972 type = build_array_type (type, itype);
5975 if (type != error_mark_node)
5977 if (size_varies)
5979 /* It is ok to modify type here even if itype is
5980 NULL: if size_varies, we're in a
5981 multi-dimensional array and the inner type has
5982 variable size, so the enclosing shared array type
5983 must too. */
5984 if (size && TREE_CODE (size) == INTEGER_CST)
5985 type
5986 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5987 C_TYPE_VARIABLE_SIZE (type) = 1;
5990 /* The GCC extension for zero-length arrays differs from
5991 ISO flexible array members in that sizeof yields
5992 zero. */
5993 if (size && integer_zerop (size))
5995 gcc_assert (itype);
5996 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5997 TYPE_SIZE (type) = bitsize_zero_node;
5998 TYPE_SIZE_UNIT (type) = size_zero_node;
5999 SET_TYPE_STRUCTURAL_EQUALITY (type);
6001 if (array_parm_vla_unspec_p)
6003 gcc_assert (itype);
6004 /* The type is complete. C99 6.7.5.2p4 */
6005 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6006 TYPE_SIZE (type) = bitsize_zero_node;
6007 TYPE_SIZE_UNIT (type) = size_zero_node;
6008 SET_TYPE_STRUCTURAL_EQUALITY (type);
6012 if (decl_context != PARM
6013 && (array_ptr_quals != TYPE_UNQUALIFIED
6014 || array_ptr_attrs != NULL_TREE
6015 || array_parm_static))
6017 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6018 array_ptr_quals = TYPE_UNQUALIFIED;
6019 array_ptr_attrs = NULL_TREE;
6020 array_parm_static = 0;
6022 break;
6024 case cdk_function:
6026 /* Say it's a definition only for the declarator closest
6027 to the identifier, apart possibly from some
6028 attributes. */
6029 bool really_funcdef = false;
6030 tree arg_types;
6031 if (funcdef_flag)
6033 const struct c_declarator *t = declarator->declarator;
6034 while (t->kind == cdk_attrs)
6035 t = t->declarator;
6036 really_funcdef = (t->kind == cdk_id);
6039 /* Declaring a function type. Make sure we have a valid
6040 type for the function to return. */
6041 if (type == error_mark_node)
6042 continue;
6044 size_varies = false;
6046 /* Warn about some types functions can't return. */
6047 if (TREE_CODE (type) == FUNCTION_TYPE)
6049 if (name)
6050 error_at (loc, "%qE declared as function returning a "
6051 "function", name);
6052 else
6053 error_at (loc, "type name declared as function "
6054 "returning a function");
6055 type = integer_type_node;
6057 if (TREE_CODE (type) == ARRAY_TYPE)
6059 if (name)
6060 error_at (loc, "%qE declared as function returning an array",
6061 name);
6062 else
6063 error_at (loc, "type name declared as function returning "
6064 "an array");
6065 type = integer_type_node;
6067 errmsg = targetm.invalid_return_type (type);
6068 if (errmsg)
6070 error (errmsg);
6071 type = integer_type_node;
6074 /* Construct the function type and go to the next
6075 inner layer of declarator. */
6076 arg_info = declarator->u.arg_info;
6077 arg_types = grokparms (arg_info, really_funcdef);
6079 /* Type qualifiers before the return type of the function
6080 qualify the return type, not the function type. */
6081 if (type_quals)
6083 /* Type qualifiers on a function return type are
6084 normally permitted by the standard but have no
6085 effect, so give a warning at -Wreturn-type.
6086 Qualifiers on a void return type are banned on
6087 function definitions in ISO C; GCC used to used
6088 them for noreturn functions. */
6089 if (VOID_TYPE_P (type) && really_funcdef)
6090 pedwarn (loc, 0,
6091 "function definition has qualified void return type");
6092 else
6093 warning_at (loc, OPT_Wignored_qualifiers,
6094 "type qualifiers ignored on function return type");
6096 type = c_build_qualified_type (type, type_quals);
6098 type_quals = TYPE_UNQUALIFIED;
6100 type = build_function_type (type, arg_types);
6101 declarator = declarator->declarator;
6103 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6104 the formal parameter list of this FUNCTION_TYPE to point to
6105 the FUNCTION_TYPE node itself. */
6107 c_arg_tag *tag;
6108 unsigned ix;
6110 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6111 TYPE_CONTEXT (tag->type) = type;
6113 break;
6115 case cdk_pointer:
6117 /* Merge any constancy or volatility into the target type
6118 for the pointer. */
6119 if ((type_quals & TYPE_QUAL_ATOMIC)
6120 && TREE_CODE (type) == FUNCTION_TYPE)
6122 error_at (loc,
6123 "%<_Atomic%>-qualified function type");
6124 type_quals &= ~TYPE_QUAL_ATOMIC;
6126 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6127 && type_quals)
6128 pedwarn (loc, OPT_Wpedantic,
6129 "ISO C forbids qualified function types");
6130 if (type_quals)
6131 type = c_build_qualified_type (type, type_quals);
6132 size_varies = false;
6134 /* When the pointed-to type involves components of variable size,
6135 care must be taken to ensure that the size evaluation code is
6136 emitted early enough to dominate all the possible later uses
6137 and late enough for the variables on which it depends to have
6138 been assigned.
6140 This is expected to happen automatically when the pointed-to
6141 type has a name/declaration of it's own, but special attention
6142 is required if the type is anonymous.
6144 We handle the NORMAL and FIELD contexts here by attaching an
6145 artificial TYPE_DECL to such pointed-to type. This forces the
6146 sizes evaluation at a safe point and ensures it is not deferred
6147 until e.g. within a deeper conditional context.
6149 We expect nothing to be needed here for PARM or TYPENAME.
6150 Pushing a TYPE_DECL at this point for TYPENAME would actually
6151 be incorrect, as we might be in the middle of an expression
6152 with side effects on the pointed-to type size "arguments" prior
6153 to the pointer declaration point and the fake TYPE_DECL in the
6154 enclosing context would force the size evaluation prior to the
6155 side effects. */
6157 if (!TYPE_NAME (type)
6158 && (decl_context == NORMAL || decl_context == FIELD)
6159 && variably_modified_type_p (type, NULL_TREE))
6161 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6162 DECL_ARTIFICIAL (decl) = 1;
6163 pushdecl (decl);
6164 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6165 TYPE_NAME (type) = decl;
6168 type = c_build_pointer_type (type);
6170 /* Process type qualifiers (such as const or volatile)
6171 that were given inside the `*'. */
6172 type_quals = declarator->u.pointer_quals;
6174 declarator = declarator->declarator;
6175 break;
6177 default:
6178 gcc_unreachable ();
6181 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6183 /* Now TYPE has the actual type, apart from any qualifiers in
6184 TYPE_QUALS. */
6186 /* Warn about address space used for things other than static memory or
6187 pointers. */
6188 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6189 if (!ADDR_SPACE_GENERIC_P (address_space))
6191 if (decl_context == NORMAL)
6193 switch (storage_class)
6195 case csc_auto:
6196 error ("%qs combined with %<auto%> qualifier for %qE",
6197 c_addr_space_name (address_space), name);
6198 break;
6199 case csc_register:
6200 error ("%qs combined with %<register%> qualifier for %qE",
6201 c_addr_space_name (address_space), name);
6202 break;
6203 case csc_none:
6204 if (current_function_scope)
6206 error ("%qs specified for auto variable %qE",
6207 c_addr_space_name (address_space), name);
6208 break;
6210 break;
6211 case csc_static:
6212 case csc_extern:
6213 case csc_typedef:
6214 break;
6215 default:
6216 gcc_unreachable ();
6219 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6221 if (name)
6222 error ("%qs specified for parameter %qE",
6223 c_addr_space_name (address_space), name);
6224 else
6225 error ("%qs specified for unnamed parameter",
6226 c_addr_space_name (address_space));
6228 else if (decl_context == FIELD)
6230 if (name)
6231 error ("%qs specified for structure field %qE",
6232 c_addr_space_name (address_space), name);
6233 else
6234 error ("%qs specified for structure field",
6235 c_addr_space_name (address_space));
6239 /* Check the type and width of a bit-field. */
6240 if (bitfield)
6242 check_bitfield_type_and_width (&type, width, name);
6243 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6244 atomic types are permitted for bit-fields; we have no code to
6245 make bit-field accesses atomic, so disallow them. */
6246 if (type_quals & TYPE_QUAL_ATOMIC)
6248 if (name)
6249 error ("bit-field %qE has atomic type", name);
6250 else
6251 error ("bit-field has atomic type");
6252 type_quals &= ~TYPE_QUAL_ATOMIC;
6256 /* Reject invalid uses of _Alignas. */
6257 if (declspecs->alignas_p)
6259 if (storage_class == csc_typedef)
6260 error_at (loc, "alignment specified for typedef %qE", name);
6261 else if (storage_class == csc_register)
6262 error_at (loc, "alignment specified for %<register%> object %qE",
6263 name);
6264 else if (decl_context == PARM)
6266 if (name)
6267 error_at (loc, "alignment specified for parameter %qE", name);
6268 else
6269 error_at (loc, "alignment specified for unnamed parameter");
6271 else if (bitfield)
6273 if (name)
6274 error_at (loc, "alignment specified for bit-field %qE", name);
6275 else
6276 error_at (loc, "alignment specified for unnamed bit-field");
6278 else if (TREE_CODE (type) == FUNCTION_TYPE)
6279 error_at (loc, "alignment specified for function %qE", name);
6280 else if (declspecs->align_log != -1)
6282 alignas_align = 1U << declspecs->align_log;
6283 if (alignas_align < min_align_of_type (type))
6285 if (name)
6286 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6287 "alignment of %qE", name);
6288 else
6289 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6290 "alignment of unnamed field");
6291 alignas_align = 0;
6296 /* Did array size calculations overflow or does the array cover more
6297 than half of the address-space? */
6298 if (TREE_CODE (type) == ARRAY_TYPE
6299 && COMPLETE_TYPE_P (type)
6300 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6301 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
6303 if (name)
6304 error_at (loc, "size of array %qE is too large", name);
6305 else
6306 error_at (loc, "size of unnamed array is too large");
6307 /* If we proceed with the array type as it is, we'll eventually
6308 crash in tree_to_[su]hwi(). */
6309 type = error_mark_node;
6312 /* If this is declaring a typedef name, return a TYPE_DECL. */
6314 if (storage_class == csc_typedef)
6316 tree decl;
6317 if ((type_quals & TYPE_QUAL_ATOMIC)
6318 && TREE_CODE (type) == FUNCTION_TYPE)
6320 error_at (loc,
6321 "%<_Atomic%>-qualified function type");
6322 type_quals &= ~TYPE_QUAL_ATOMIC;
6324 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6325 && type_quals)
6326 pedwarn (loc, OPT_Wpedantic,
6327 "ISO C forbids qualified function types");
6328 if (type_quals)
6329 type = c_build_qualified_type (type, type_quals);
6330 decl = build_decl (declarator->id_loc,
6331 TYPE_DECL, declarator->u.id, type);
6332 if (declspecs->explicit_signed_p)
6333 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6334 if (declspecs->inline_p)
6335 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6336 if (declspecs->noreturn_p)
6337 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6339 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6341 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6343 if (b != NULL
6344 && b->decl != NULL_TREE
6345 && (B_IN_CURRENT_SCOPE (b)
6346 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6347 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6349 warning_at (declarator->id_loc, OPT_Wc___compat,
6350 ("using %qD as both a typedef and a tag is "
6351 "invalid in C++"),
6352 decl);
6353 if (b->locus != UNKNOWN_LOCATION)
6354 inform (b->locus, "originally defined here");
6358 return decl;
6361 /* If this is a type name (such as, in a cast or sizeof),
6362 compute the type and return it now. */
6364 if (decl_context == TYPENAME)
6366 /* Note that the grammar rejects storage classes in typenames
6367 and fields. */
6368 gcc_assert (storage_class == csc_none && !threadp
6369 && !declspecs->inline_p && !declspecs->noreturn_p);
6370 if ((type_quals & TYPE_QUAL_ATOMIC)
6371 && TREE_CODE (type) == FUNCTION_TYPE)
6373 error_at (loc,
6374 "%<_Atomic%>-qualified function type");
6375 type_quals &= ~TYPE_QUAL_ATOMIC;
6377 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6378 && type_quals)
6379 pedwarn (loc, OPT_Wpedantic,
6380 "ISO C forbids const or volatile function types");
6381 if (type_quals)
6382 type = c_build_qualified_type (type, type_quals);
6383 return type;
6386 if (pedantic && decl_context == FIELD
6387 && variably_modified_type_p (type, NULL_TREE))
6389 /* C99 6.7.2.1p8 */
6390 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6391 "have a variably modified type");
6394 /* Aside from typedefs and type names (handle above),
6395 `void' at top level (not within pointer)
6396 is allowed only in public variables.
6397 We don't complain about parms either, but that is because
6398 a better error message can be made later. */
6400 if (VOID_TYPE_P (type) && decl_context != PARM
6401 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6402 && (storage_class == csc_extern
6403 || (current_scope == file_scope
6404 && !(storage_class == csc_static
6405 || storage_class == csc_register)))))
6407 error_at (loc, "variable or field %qE declared void", name);
6408 type = integer_type_node;
6411 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6412 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6415 tree decl;
6417 if (decl_context == PARM)
6419 tree promoted_type;
6420 bool array_parameter_p = false;
6422 /* A parameter declared as an array of T is really a pointer to T.
6423 One declared as a function is really a pointer to a function. */
6425 if (TREE_CODE (type) == ARRAY_TYPE)
6427 /* Transfer const-ness of array into that of type pointed to. */
6428 type = TREE_TYPE (type);
6429 if (type_quals)
6430 type = c_build_qualified_type (type, type_quals);
6431 type = c_build_pointer_type (type);
6432 type_quals = array_ptr_quals;
6433 if (type_quals)
6434 type = c_build_qualified_type (type, type_quals);
6436 /* We don't yet implement attributes in this context. */
6437 if (array_ptr_attrs != NULL_TREE)
6438 warning_at (loc, OPT_Wattributes,
6439 "attributes in parameter array declarator ignored");
6441 size_varies = false;
6442 array_parameter_p = true;
6444 else if (TREE_CODE (type) == FUNCTION_TYPE)
6446 if (type_quals & TYPE_QUAL_ATOMIC)
6448 error_at (loc,
6449 "%<_Atomic%>-qualified function type");
6450 type_quals &= ~TYPE_QUAL_ATOMIC;
6452 else if (type_quals)
6453 pedwarn (loc, OPT_Wpedantic,
6454 "ISO C forbids qualified function types");
6455 if (type_quals)
6456 type = c_build_qualified_type (type, type_quals);
6457 type = c_build_pointer_type (type);
6458 type_quals = TYPE_UNQUALIFIED;
6460 else if (type_quals)
6461 type = c_build_qualified_type (type, type_quals);
6463 decl = build_decl (declarator->id_loc,
6464 PARM_DECL, declarator->u.id, type);
6465 if (size_varies)
6466 C_DECL_VARIABLE_SIZE (decl) = 1;
6467 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6469 /* Compute the type actually passed in the parmlist,
6470 for the case where there is no prototype.
6471 (For example, shorts and chars are passed as ints.)
6472 When there is a prototype, this is overridden later. */
6474 if (type == error_mark_node)
6475 promoted_type = type;
6476 else
6477 promoted_type = c_type_promotes_to (type);
6479 DECL_ARG_TYPE (decl) = promoted_type;
6480 if (declspecs->inline_p)
6481 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6482 if (declspecs->noreturn_p)
6483 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6485 else if (decl_context == FIELD)
6487 /* Note that the grammar rejects storage classes in typenames
6488 and fields. */
6489 gcc_assert (storage_class == csc_none && !threadp
6490 && !declspecs->inline_p && !declspecs->noreturn_p);
6492 /* Structure field. It may not be a function. */
6494 if (TREE_CODE (type) == FUNCTION_TYPE)
6496 error_at (loc, "field %qE declared as a function", name);
6497 type = build_pointer_type (type);
6499 else if (TREE_CODE (type) != ERROR_MARK
6500 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6502 if (name)
6503 error_at (loc, "field %qE has incomplete type", name);
6504 else
6505 error_at (loc, "unnamed field has incomplete type");
6506 type = error_mark_node;
6508 type = c_build_qualified_type (type, type_quals);
6509 decl = build_decl (declarator->id_loc,
6510 FIELD_DECL, declarator->u.id, type);
6511 DECL_NONADDRESSABLE_P (decl) = bitfield;
6512 if (bitfield && !declarator->u.id)
6513 TREE_NO_WARNING (decl) = 1;
6515 if (size_varies)
6516 C_DECL_VARIABLE_SIZE (decl) = 1;
6518 else if (TREE_CODE (type) == FUNCTION_TYPE)
6520 if (storage_class == csc_register || threadp)
6522 error_at (loc, "invalid storage class for function %qE", name);
6524 else if (current_scope != file_scope)
6526 /* Function declaration not at file scope. Storage
6527 classes other than `extern' are not allowed, C99
6528 6.7.1p5, and `extern' makes no difference. However,
6529 GCC allows 'auto', perhaps with 'inline', to support
6530 nested functions. */
6531 if (storage_class == csc_auto)
6532 pedwarn (loc, OPT_Wpedantic,
6533 "invalid storage class for function %qE", name);
6534 else if (storage_class == csc_static)
6536 error_at (loc, "invalid storage class for function %qE", name);
6537 if (funcdef_flag)
6538 storage_class = declspecs->storage_class = csc_none;
6539 else
6540 return 0;
6544 decl = build_decl (declarator->id_loc,
6545 FUNCTION_DECL, declarator->u.id, type);
6546 decl = build_decl_attribute_variant (decl, decl_attr);
6548 if (type_quals & TYPE_QUAL_ATOMIC)
6550 error_at (loc,
6551 "%<_Atomic%>-qualified function type");
6552 type_quals &= ~TYPE_QUAL_ATOMIC;
6554 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6555 pedwarn (loc, OPT_Wpedantic,
6556 "ISO C forbids qualified function types");
6558 /* Every function declaration is an external reference
6559 (DECL_EXTERNAL) except for those which are not at file
6560 scope and are explicitly declared "auto". This is
6561 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6562 GCC to signify a forward declaration of a nested function. */
6563 if (storage_class == csc_auto && current_scope != file_scope)
6564 DECL_EXTERNAL (decl) = 0;
6565 /* In C99, a function which is declared 'inline' with 'extern'
6566 is not an external reference (which is confusing). It
6567 means that the later definition of the function must be output
6568 in this file, C99 6.7.4p6. In GNU C89, a function declared
6569 'extern inline' is an external reference. */
6570 else if (declspecs->inline_p && storage_class != csc_static)
6571 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6572 == flag_gnu89_inline);
6573 else
6574 DECL_EXTERNAL (decl) = !initialized;
6576 /* Record absence of global scope for `static' or `auto'. */
6577 TREE_PUBLIC (decl)
6578 = !(storage_class == csc_static || storage_class == csc_auto);
6580 /* For a function definition, record the argument information
6581 block where store_parm_decls will look for it. */
6582 if (funcdef_flag)
6583 current_function_arg_info = arg_info;
6585 if (declspecs->default_int_p)
6586 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6588 /* Record presence of `inline' and `_Noreturn', if it is
6589 reasonable. */
6590 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6592 if (declspecs->inline_p)
6593 pedwarn (loc, 0, "cannot inline function %<main%>");
6594 if (declspecs->noreturn_p)
6595 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6597 else
6599 if (declspecs->inline_p)
6600 /* Record that the function is declared `inline'. */
6601 DECL_DECLARED_INLINE_P (decl) = 1;
6602 if (declspecs->noreturn_p)
6604 if (flag_isoc99)
6605 pedwarn_c99 (loc, OPT_Wpedantic,
6606 "ISO C99 does not support %<_Noreturn%>");
6607 else
6608 pedwarn_c99 (loc, OPT_Wpedantic,
6609 "ISO C90 does not support %<_Noreturn%>");
6610 TREE_THIS_VOLATILE (decl) = 1;
6614 else
6616 /* It's a variable. */
6617 /* An uninitialized decl with `extern' is a reference. */
6618 int extern_ref = !initialized && storage_class == csc_extern;
6620 type = c_build_qualified_type (type, type_quals);
6622 /* C99 6.2.2p7: It is invalid (compile-time undefined
6623 behavior) to create an 'extern' declaration for a
6624 variable if there is a global declaration that is
6625 'static' and the global declaration is not visible.
6626 (If the static declaration _is_ currently visible,
6627 the 'extern' declaration is taken to refer to that decl.) */
6628 if (extern_ref && current_scope != file_scope)
6630 tree global_decl = identifier_global_value (declarator->u.id);
6631 tree visible_decl = lookup_name (declarator->u.id);
6633 if (global_decl
6634 && global_decl != visible_decl
6635 && TREE_CODE (global_decl) == VAR_DECL
6636 && !TREE_PUBLIC (global_decl))
6637 error_at (loc, "variable previously declared %<static%> "
6638 "redeclared %<extern%>");
6641 decl = build_decl (declarator->id_loc,
6642 VAR_DECL, declarator->u.id, type);
6643 if (size_varies)
6644 C_DECL_VARIABLE_SIZE (decl) = 1;
6646 if (declspecs->inline_p)
6647 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6648 if (declspecs->noreturn_p)
6649 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6651 /* At file scope, an initialized extern declaration may follow
6652 a static declaration. In that case, DECL_EXTERNAL will be
6653 reset later in start_decl. */
6654 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6656 /* At file scope, the presence of a `static' or `register' storage
6657 class specifier, or the absence of all storage class specifiers
6658 makes this declaration a definition (perhaps tentative). Also,
6659 the absence of `static' makes it public. */
6660 if (current_scope == file_scope)
6662 TREE_PUBLIC (decl) = storage_class != csc_static;
6663 TREE_STATIC (decl) = !extern_ref;
6665 /* Not at file scope, only `static' makes a static definition. */
6666 else
6668 TREE_STATIC (decl) = (storage_class == csc_static);
6669 TREE_PUBLIC (decl) = extern_ref;
6672 if (threadp)
6673 set_decl_tls_model (decl, decl_default_tls_model (decl));
6676 if ((storage_class == csc_extern
6677 || (storage_class == csc_none
6678 && TREE_CODE (type) == FUNCTION_TYPE
6679 && !funcdef_flag))
6680 && variably_modified_type_p (type, NULL_TREE))
6682 /* C99 6.7.5.2p2 */
6683 if (TREE_CODE (type) == FUNCTION_TYPE)
6684 error_at (loc, "non-nested function with variably modified type");
6685 else
6686 error_at (loc, "object with variably modified type must have "
6687 "no linkage");
6690 /* Record `register' declaration for warnings on &
6691 and in case doing stupid register allocation. */
6693 if (storage_class == csc_register)
6695 C_DECL_REGISTER (decl) = 1;
6696 DECL_REGISTER (decl) = 1;
6699 /* Record constancy and volatility. */
6700 c_apply_type_quals_to_decl (type_quals, decl);
6702 /* Apply _Alignas specifiers. */
6703 if (alignas_align)
6705 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6706 DECL_USER_ALIGN (decl) = 1;
6709 /* If a type has volatile components, it should be stored in memory.
6710 Otherwise, the fact that those components are volatile
6711 will be ignored, and would even crash the compiler.
6712 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6713 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6714 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6715 || TREE_CODE (decl) == RESULT_DECL))
6717 /* It is not an error for a structure with volatile fields to
6718 be declared register, but reset DECL_REGISTER since it
6719 cannot actually go in a register. */
6720 int was_reg = C_DECL_REGISTER (decl);
6721 C_DECL_REGISTER (decl) = 0;
6722 DECL_REGISTER (decl) = 0;
6723 c_mark_addressable (decl);
6724 C_DECL_REGISTER (decl) = was_reg;
6727 /* This is the earliest point at which we might know the assembler
6728 name of a variable. Thus, if it's known before this, die horribly. */
6729 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6731 if (warn_cxx_compat
6732 && TREE_CODE (decl) == VAR_DECL
6733 && TREE_PUBLIC (decl)
6734 && TREE_STATIC (decl)
6735 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6736 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6737 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6738 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6739 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6740 ("non-local variable %qD with anonymous type is "
6741 "questionable in C++"),
6742 decl);
6744 return decl;
6748 /* Decode the parameter-list info for a function type or function definition.
6749 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6750 if there is an identifier list instead of a parameter decl list).
6751 These two functions are separate because when a function returns
6752 or receives functions then each is called multiple times but the order
6753 of calls is different. The last call to `grokparms' is always the one
6754 that contains the formal parameter names of a function definition.
6756 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6758 FUNCDEF_FLAG is true for a function definition, false for
6759 a mere declaration. A nonempty identifier-list gets an error message
6760 when FUNCDEF_FLAG is false. */
6762 static tree
6763 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6765 tree arg_types = arg_info->types;
6767 if (funcdef_flag && arg_info->had_vla_unspec)
6769 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6770 /* C99 6.7.5.2p4 */
6771 error ("%<[*]%> not allowed in other than function prototype scope");
6774 if (arg_types == 0 && !funcdef_flag
6775 && !in_system_header_at (input_location))
6776 warning (OPT_Wstrict_prototypes,
6777 "function declaration isn%'t a prototype");
6779 if (arg_types == error_mark_node)
6780 return 0; /* don't set TYPE_ARG_TYPES in this case */
6782 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6784 if (!funcdef_flag)
6786 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6787 arg_info->parms = NULL_TREE;
6789 else
6790 arg_info->parms = arg_info->types;
6792 arg_info->types = 0;
6793 return 0;
6795 else
6797 tree parm, type, typelt;
6798 unsigned int parmno;
6799 const char *errmsg;
6801 /* If there is a parameter of incomplete type in a definition,
6802 this is an error. In a declaration this is valid, and a
6803 struct or union type may be completed later, before any calls
6804 or definition of the function. In the case where the tag was
6805 first declared within the parameter list, a warning has
6806 already been given. If a parameter has void type, then
6807 however the function cannot be defined or called, so
6808 warn. */
6810 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6811 parm;
6812 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6814 type = TREE_VALUE (typelt);
6815 if (type == error_mark_node)
6816 continue;
6818 if (!COMPLETE_TYPE_P (type))
6820 if (funcdef_flag)
6822 if (DECL_NAME (parm))
6823 error_at (input_location,
6824 "parameter %u (%q+D) has incomplete type",
6825 parmno, parm);
6826 else
6827 error_at (DECL_SOURCE_LOCATION (parm),
6828 "parameter %u has incomplete type",
6829 parmno);
6831 TREE_VALUE (typelt) = error_mark_node;
6832 TREE_TYPE (parm) = error_mark_node;
6833 arg_types = NULL_TREE;
6835 else if (VOID_TYPE_P (type))
6837 if (DECL_NAME (parm))
6838 warning_at (input_location, 0,
6839 "parameter %u (%q+D) has void type",
6840 parmno, parm);
6841 else
6842 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6843 "parameter %u has void type",
6844 parmno);
6848 errmsg = targetm.invalid_parameter_type (type);
6849 if (errmsg)
6851 error (errmsg);
6852 TREE_VALUE (typelt) = error_mark_node;
6853 TREE_TYPE (parm) = error_mark_node;
6854 arg_types = NULL_TREE;
6857 if (DECL_NAME (parm) && TREE_USED (parm))
6858 warn_if_shadowing (parm);
6860 return arg_types;
6864 /* Allocate and initialize a c_arg_info structure from the parser's
6865 obstack. */
6867 struct c_arg_info *
6868 build_arg_info (void)
6870 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6871 ret->parms = NULL_TREE;
6872 ret->tags = NULL;
6873 ret->types = NULL_TREE;
6874 ret->others = NULL_TREE;
6875 ret->pending_sizes = NULL;
6876 ret->had_vla_unspec = 0;
6877 return ret;
6880 /* Take apart the current scope and return a c_arg_info structure with
6881 info on a parameter list just parsed.
6883 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6885 ELLIPSIS being true means the argument list ended in '...' so don't
6886 append a sentinel (void_list_node) to the end of the type-list.
6888 EXPR is NULL or an expression that needs to be evaluated for the
6889 side effects of array size expressions in the parameters. */
6891 struct c_arg_info *
6892 get_parm_info (bool ellipsis, tree expr)
6894 struct c_binding *b = current_scope->bindings;
6895 struct c_arg_info *arg_info = build_arg_info ();
6897 tree parms = 0;
6898 vec<c_arg_tag, va_gc> *tags = NULL;
6899 tree types = 0;
6900 tree others = 0;
6902 static bool explained_incomplete_types = false;
6903 bool gave_void_only_once_err = false;
6905 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6907 /* The bindings in this scope must not get put into a block.
6908 We will take care of deleting the binding nodes. */
6909 current_scope->bindings = 0;
6911 /* This function is only called if there was *something* on the
6912 parameter list. */
6913 gcc_assert (b);
6915 /* A parameter list consisting solely of 'void' indicates that the
6916 function takes no arguments. But if the 'void' is qualified
6917 (by 'const' or 'volatile'), or has a storage class specifier
6918 ('register'), then the behavior is undefined; issue an error.
6919 Typedefs for 'void' are OK (see DR#157). */
6920 if (b->prev == 0 /* one binding */
6921 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6922 && !DECL_NAME (b->decl) /* anonymous */
6923 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6925 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
6926 || C_DECL_REGISTER (b->decl))
6927 error ("%<void%> as only parameter may not be qualified");
6929 /* There cannot be an ellipsis. */
6930 if (ellipsis)
6931 error ("%<void%> must be the only parameter");
6933 arg_info->types = void_list_node;
6934 return arg_info;
6937 if (!ellipsis)
6938 types = void_list_node;
6940 /* Break up the bindings list into parms, tags, types, and others;
6941 apply sanity checks; purge the name-to-decl bindings. */
6942 while (b)
6944 tree decl = b->decl;
6945 tree type = TREE_TYPE (decl);
6946 c_arg_tag tag;
6947 const char *keyword;
6949 switch (TREE_CODE (decl))
6951 case PARM_DECL:
6952 if (b->id)
6954 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6955 I_SYMBOL_BINDING (b->id) = b->shadowed;
6958 /* Check for forward decls that never got their actual decl. */
6959 if (TREE_ASM_WRITTEN (decl))
6960 error ("parameter %q+D has just a forward declaration", decl);
6961 /* Check for (..., void, ...) and issue an error. */
6962 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6964 if (!gave_void_only_once_err)
6966 error ("%<void%> must be the only parameter");
6967 gave_void_only_once_err = true;
6970 else
6972 /* Valid parameter, add it to the list. */
6973 DECL_CHAIN (decl) = parms;
6974 parms = decl;
6976 /* Since there is a prototype, args are passed in their
6977 declared types. The back end may override this later. */
6978 DECL_ARG_TYPE (decl) = type;
6979 types = tree_cons (0, type, types);
6981 break;
6983 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6984 case UNION_TYPE: keyword = "union"; goto tag;
6985 case RECORD_TYPE: keyword = "struct"; goto tag;
6986 tag:
6987 /* Types may not have tag-names, in which case the type
6988 appears in the bindings list with b->id NULL. */
6989 if (b->id)
6991 gcc_assert (I_TAG_BINDING (b->id) == b);
6992 I_TAG_BINDING (b->id) = b->shadowed;
6995 /* Warn about any struct, union or enum tags defined in a
6996 parameter list. The scope of such types is limited to
6997 the parameter list, which is rarely if ever desirable
6998 (it's impossible to call such a function with type-
6999 correct arguments). An anonymous union parm type is
7000 meaningful as a GNU extension, so don't warn for that. */
7001 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7003 if (b->id)
7004 /* The %s will be one of 'struct', 'union', or 'enum'. */
7005 warning (0, "%<%s %E%> declared inside parameter list",
7006 keyword, b->id);
7007 else
7008 /* The %s will be one of 'struct', 'union', or 'enum'. */
7009 warning (0, "anonymous %s declared inside parameter list",
7010 keyword);
7012 if (!explained_incomplete_types)
7014 warning (0, "its scope is only this definition or declaration,"
7015 " which is probably not what you want");
7016 explained_incomplete_types = true;
7020 tag.id = b->id;
7021 tag.type = decl;
7022 vec_safe_push (tags, tag);
7023 break;
7025 case CONST_DECL:
7026 case TYPE_DECL:
7027 case FUNCTION_DECL:
7028 /* CONST_DECLs appear here when we have an embedded enum,
7029 and TYPE_DECLs appear here when we have an embedded struct
7030 or union. No warnings for this - we already warned about the
7031 type itself. FUNCTION_DECLs appear when there is an implicit
7032 function declaration in the parameter list. */
7034 /* When we reinsert this decl in the function body, we need
7035 to reconstruct whether it was marked as nested. */
7036 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7037 ? b->nested
7038 : !b->nested);
7039 DECL_CHAIN (decl) = others;
7040 others = decl;
7041 /* fall through */
7043 case ERROR_MARK:
7044 /* error_mark_node appears here when we have an undeclared
7045 variable. Just throw it away. */
7046 if (b->id)
7048 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7049 I_SYMBOL_BINDING (b->id) = b->shadowed;
7051 break;
7053 /* Other things that might be encountered. */
7054 case LABEL_DECL:
7055 case VAR_DECL:
7056 default:
7057 gcc_unreachable ();
7060 b = free_binding_and_advance (b);
7063 arg_info->parms = parms;
7064 arg_info->tags = tags;
7065 arg_info->types = types;
7066 arg_info->others = others;
7067 arg_info->pending_sizes = expr;
7068 return arg_info;
7071 /* Get the struct, enum or union (CODE says which) with tag NAME.
7072 Define the tag as a forward-reference with location LOC if it is
7073 not defined. Return a c_typespec structure for the type
7074 specifier. */
7076 struct c_typespec
7077 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7079 struct c_typespec ret;
7080 tree ref;
7081 location_t refloc;
7083 ret.expr = NULL_TREE;
7084 ret.expr_const_operands = true;
7086 /* If a cross reference is requested, look up the type
7087 already defined for this tag and return it. */
7089 ref = lookup_tag (code, name, 0, &refloc);
7090 /* If this is the right type of tag, return what we found.
7091 (This reference will be shadowed by shadow_tag later if appropriate.)
7092 If this is the wrong type of tag, do not return it. If it was the
7093 wrong type in the same scope, we will have had an error
7094 message already; if in a different scope and declaring
7095 a name, pending_xref_error will give an error message; but if in a
7096 different scope and not declaring a name, this tag should
7097 shadow the previous declaration of a different type of tag, and
7098 this would not work properly if we return the reference found.
7099 (For example, with "struct foo" in an outer scope, "union foo;"
7100 must shadow that tag with a new one of union type.) */
7101 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7102 if (ref && TREE_CODE (ref) == code)
7104 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7105 && loc != UNKNOWN_LOCATION
7106 && warn_cxx_compat)
7108 switch (code)
7110 case ENUMERAL_TYPE:
7111 warning_at (loc, OPT_Wc___compat,
7112 ("enum type defined in struct or union "
7113 "is not visible in C++"));
7114 inform (refloc, "enum type defined here");
7115 break;
7116 case RECORD_TYPE:
7117 warning_at (loc, OPT_Wc___compat,
7118 ("struct defined in struct or union "
7119 "is not visible in C++"));
7120 inform (refloc, "struct defined here");
7121 break;
7122 case UNION_TYPE:
7123 warning_at (loc, OPT_Wc___compat,
7124 ("union defined in struct or union "
7125 "is not visible in C++"));
7126 inform (refloc, "union defined here");
7127 break;
7128 default:
7129 gcc_unreachable();
7133 ret.spec = ref;
7134 return ret;
7137 /* If no such tag is yet defined, create a forward-reference node
7138 and record it as the "definition".
7139 When a real declaration of this type is found,
7140 the forward-reference will be altered into a real type. */
7142 ref = make_node (code);
7143 if (code == ENUMERAL_TYPE)
7145 /* Give the type a default layout like unsigned int
7146 to avoid crashing if it does not get defined. */
7147 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7148 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
7149 TYPE_USER_ALIGN (ref) = 0;
7150 TYPE_UNSIGNED (ref) = 1;
7151 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7152 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7153 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7156 pushtag (loc, name, ref);
7158 ret.spec = ref;
7159 return ret;
7162 /* Get the struct, enum or union (CODE says which) with tag NAME.
7163 Define the tag as a forward-reference if it is not defined.
7164 Return a tree for the type. */
7166 tree
7167 xref_tag (enum tree_code code, tree name)
7169 return parser_xref_tag (input_location, code, name).spec;
7172 /* Make sure that the tag NAME is defined *in the current scope*
7173 at least as a forward reference.
7174 LOC is the location of the struct's definition.
7175 CODE says which kind of tag NAME ought to be.
7177 This stores the current value of the file static STRUCT_PARSE_INFO
7178 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7179 new c_struct_parse_info structure. The old value of
7180 STRUCT_PARSE_INFO is restored in finish_struct. */
7182 tree
7183 start_struct (location_t loc, enum tree_code code, tree name,
7184 struct c_struct_parse_info **enclosing_struct_parse_info)
7186 /* If there is already a tag defined at this scope
7187 (as a forward reference), just return it. */
7189 tree ref = NULL_TREE;
7190 location_t refloc = UNKNOWN_LOCATION;
7192 if (name != NULL_TREE)
7193 ref = lookup_tag (code, name, 1, &refloc);
7194 if (ref && TREE_CODE (ref) == code)
7196 if (TYPE_SIZE (ref))
7198 if (code == UNION_TYPE)
7199 error_at (loc, "redefinition of %<union %E%>", name);
7200 else
7201 error_at (loc, "redefinition of %<struct %E%>", name);
7202 if (refloc != UNKNOWN_LOCATION)
7203 inform (refloc, "originally defined here");
7204 /* Don't create structures using a name already in use. */
7205 ref = NULL_TREE;
7207 else if (C_TYPE_BEING_DEFINED (ref))
7209 if (code == UNION_TYPE)
7210 error_at (loc, "nested redefinition of %<union %E%>", name);
7211 else
7212 error_at (loc, "nested redefinition of %<struct %E%>", name);
7213 /* Don't bother to report "originally defined here" for a
7214 nested redefinition; the original definition should be
7215 obvious. */
7216 /* Don't create structures that contain themselves. */
7217 ref = NULL_TREE;
7221 /* Otherwise create a forward-reference just so the tag is in scope. */
7223 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7225 ref = make_node (code);
7226 pushtag (loc, name, ref);
7229 C_TYPE_BEING_DEFINED (ref) = 1;
7230 TYPE_PACKED (ref) = flag_pack_struct;
7232 *enclosing_struct_parse_info = struct_parse_info;
7233 struct_parse_info = XNEW (struct c_struct_parse_info);
7234 struct_parse_info->struct_types.create (0);
7235 struct_parse_info->fields.create (0);
7236 struct_parse_info->typedefs_seen.create (0);
7238 /* FIXME: This will issue a warning for a use of a type defined
7239 within a statement expr used within sizeof, et. al. This is not
7240 terribly serious as C++ doesn't permit statement exprs within
7241 sizeof anyhow. */
7242 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7243 warning_at (loc, OPT_Wc___compat,
7244 "defining type in %qs expression is invalid in C++",
7245 (in_sizeof
7246 ? "sizeof"
7247 : (in_typeof ? "typeof" : "alignof")));
7249 return ref;
7252 /* Process the specs, declarator and width (NULL if omitted)
7253 of a structure component, returning a FIELD_DECL node.
7254 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7255 DECL_ATTRS is as for grokdeclarator.
7257 LOC is the location of the structure component.
7259 This is done during the parsing of the struct declaration.
7260 The FIELD_DECL nodes are chained together and the lot of them
7261 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7263 tree
7264 grokfield (location_t loc,
7265 struct c_declarator *declarator, struct c_declspecs *declspecs,
7266 tree width, tree *decl_attrs)
7268 tree value;
7270 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7271 && width == NULL_TREE)
7273 /* This is an unnamed decl.
7275 If we have something of the form "union { list } ;" then this
7276 is the anonymous union extension. Similarly for struct.
7278 If this is something of the form "struct foo;", then
7279 If MS or Plan 9 extensions are enabled, this is handled as
7280 an anonymous struct.
7281 Otherwise this is a forward declaration of a structure tag.
7283 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7284 If foo names a structure or union without a tag, then this
7285 is an anonymous struct (this is permitted by C11).
7286 If MS or Plan 9 extensions are enabled and foo names a
7287 structure, then again this is an anonymous struct.
7288 Otherwise this is an error.
7290 Oh what a horrid tangled web we weave. I wonder if MS consciously
7291 took this from Plan 9 or if it was an accident of implementation
7292 that took root before someone noticed the bug... */
7294 tree type = declspecs->type;
7295 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
7296 || TREE_CODE (type) == UNION_TYPE);
7297 bool ok = false;
7299 if (type_ok
7300 && (flag_ms_extensions
7301 || flag_plan9_extensions
7302 || !declspecs->typedef_p))
7304 if (flag_ms_extensions || flag_plan9_extensions)
7305 ok = true;
7306 else if (TYPE_NAME (type) == NULL)
7307 ok = true;
7308 else
7309 ok = false;
7311 if (!ok)
7313 pedwarn (loc, 0, "declaration does not declare anything");
7314 return NULL_TREE;
7316 if (flag_isoc99)
7317 pedwarn_c99 (loc, OPT_Wpedantic,
7318 "ISO C99 doesn%'t support unnamed structs/unions");
7319 else
7320 pedwarn_c99 (loc, OPT_Wpedantic,
7321 "ISO C90 doesn%'t support unnamed structs/unions");
7324 value = grokdeclarator (declarator, declspecs, FIELD, false,
7325 width ? &width : NULL, decl_attrs, NULL, NULL,
7326 DEPRECATED_NORMAL);
7328 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7329 DECL_INITIAL (value) = width;
7331 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7333 /* If we currently have a binding for this field, set the
7334 in_struct field in the binding, so that we warn about lookups
7335 which find it. */
7336 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7337 if (b != NULL)
7339 /* If the in_struct field is not yet set, push it on a list
7340 to be cleared when this struct is finished. */
7341 if (!b->in_struct)
7343 struct_parse_info->fields.safe_push (b);
7344 b->in_struct = 1;
7349 return value;
7352 /* Subroutine of detect_field_duplicates: return whether X and Y,
7353 which are both fields in the same struct, have duplicate field
7354 names. */
7356 static bool
7357 is_duplicate_field (tree x, tree y)
7359 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7360 return true;
7362 /* When using -fplan9-extensions, an anonymous field whose name is a
7363 typedef can duplicate a field name. */
7364 if (flag_plan9_extensions
7365 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7367 tree xt, xn, yt, yn;
7369 xt = TREE_TYPE (x);
7370 if (DECL_NAME (x) != NULL_TREE)
7371 xn = DECL_NAME (x);
7372 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
7373 && TYPE_NAME (xt) != NULL_TREE
7374 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7375 xn = DECL_NAME (TYPE_NAME (xt));
7376 else
7377 xn = NULL_TREE;
7379 yt = TREE_TYPE (y);
7380 if (DECL_NAME (y) != NULL_TREE)
7381 yn = DECL_NAME (y);
7382 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
7383 && TYPE_NAME (yt) != NULL_TREE
7384 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7385 yn = DECL_NAME (TYPE_NAME (yt));
7386 else
7387 yn = NULL_TREE;
7389 if (xn != NULL_TREE && xn == yn)
7390 return true;
7393 return false;
7396 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7397 to HTAB, giving errors for any duplicates. */
7399 static void
7400 detect_field_duplicates_hash (tree fieldlist,
7401 hash_table<pointer_hash <tree_node> > *htab)
7403 tree x, y;
7404 tree_node **slot;
7406 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7407 if ((y = DECL_NAME (x)) != 0)
7409 slot = htab->find_slot (y, INSERT);
7410 if (*slot)
7412 error ("duplicate member %q+D", x);
7413 DECL_NAME (x) = NULL_TREE;
7415 *slot = y;
7417 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7418 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7420 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7422 /* When using -fplan9-extensions, an anonymous field whose
7423 name is a typedef can duplicate a field name. */
7424 if (flag_plan9_extensions
7425 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7426 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7428 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7429 slot = htab->find_slot (xn, INSERT);
7430 if (*slot)
7431 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7432 *slot = xn;
7437 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7438 the list such that this does not present a problem later. */
7440 static void
7441 detect_field_duplicates (tree fieldlist)
7443 tree x, y;
7444 int timeout = 10;
7446 /* If the struct is the list of instance variables of an Objective-C
7447 class, then we need to check all the instance variables of
7448 superclasses when checking for duplicates (since you can't have
7449 an instance variable in a subclass with the same name as an
7450 instance variable in a superclass). We pass on this job to the
7451 Objective-C compiler. objc_detect_field_duplicates() will return
7452 false if we are not checking the list of instance variables and
7453 the C frontend should proceed with the standard field duplicate
7454 checks. If we are checking the list of instance variables, the
7455 ObjC frontend will do the check, emit the errors if needed, and
7456 then return true. */
7457 if (c_dialect_objc ())
7458 if (objc_detect_field_duplicates (false))
7459 return;
7461 /* First, see if there are more than "a few" fields.
7462 This is trivially true if there are zero or one fields. */
7463 if (!fieldlist || !DECL_CHAIN (fieldlist))
7464 return;
7465 x = fieldlist;
7466 do {
7467 timeout--;
7468 if (DECL_NAME (x) == NULL_TREE
7469 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7470 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7471 timeout = 0;
7472 x = DECL_CHAIN (x);
7473 } while (timeout > 0 && x);
7475 /* If there were "few" fields and no anonymous structures or unions,
7476 avoid the overhead of allocating a hash table. Instead just do
7477 the nested traversal thing. */
7478 if (timeout > 0)
7480 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7481 /* When using -fplan9-extensions, we can have duplicates
7482 between typedef names and fields. */
7483 if (DECL_NAME (x)
7484 || (flag_plan9_extensions
7485 && DECL_NAME (x) == NULL_TREE
7486 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7487 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7488 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7489 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7491 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7492 if (is_duplicate_field (y, x))
7494 error ("duplicate member %q+D", x);
7495 DECL_NAME (x) = NULL_TREE;
7499 else
7501 hash_table<pointer_hash <tree_node> > htab (37);
7502 detect_field_duplicates_hash (fieldlist, &htab);
7506 /* Finish up struct info used by -Wc++-compat. */
7508 static void
7509 warn_cxx_compat_finish_struct (tree fieldlist)
7511 unsigned int ix;
7512 tree x;
7513 struct c_binding *b;
7515 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7516 the current struct. We do this now at the end of the struct
7517 because the flag is used to issue visibility warnings, and we
7518 only want to issue those warnings if the type is referenced
7519 outside of the struct declaration. */
7520 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7521 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7523 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7524 typedefs used when declaring fields in this struct. If the name
7525 of any of the fields is also a typedef name then the struct would
7526 not parse in C++, because the C++ lookup rules say that the
7527 typedef name would be looked up in the context of the struct, and
7528 would thus be the field rather than the typedef. */
7529 if (!struct_parse_info->typedefs_seen.is_empty ()
7530 && fieldlist != NULL_TREE)
7532 /* Use a hash_set<tree> using the name of the typedef. We can use
7533 a hash_set<tree> because identifiers are interned. */
7534 hash_set<tree> tset;
7536 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7537 tset.add (DECL_NAME (x));
7539 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7541 if (DECL_NAME (x) != NULL_TREE
7542 && tset.contains (DECL_NAME (x)))
7544 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7545 ("using %qD as both field and typedef name is "
7546 "invalid in C++"),
7548 /* FIXME: It would be nice to report the location where
7549 the typedef name is used. */
7554 /* For each field which has a binding and which was not defined in
7555 an enclosing struct, clear the in_struct field. */
7556 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7557 b->in_struct = 0;
7560 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7561 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7562 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7563 ATTRIBUTES are attributes to be applied to the structure.
7565 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7566 the struct was started. */
7568 tree
7569 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7570 struct c_struct_parse_info *enclosing_struct_parse_info)
7572 tree x;
7573 bool toplevel = file_scope == current_scope;
7574 int saw_named_field;
7576 /* If this type was previously laid out as a forward reference,
7577 make sure we lay it out again. */
7579 TYPE_SIZE (t) = 0;
7581 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7583 if (pedantic)
7585 for (x = fieldlist; x; x = DECL_CHAIN (x))
7587 if (DECL_NAME (x) != 0)
7588 break;
7589 if (flag_isoc11
7590 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7591 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7592 break;
7595 if (x == 0)
7597 if (TREE_CODE (t) == UNION_TYPE)
7599 if (fieldlist)
7600 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7601 else
7602 pedwarn (loc, OPT_Wpedantic, "union has no members");
7604 else
7606 if (fieldlist)
7607 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7608 else
7609 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7614 /* Install struct as DECL_CONTEXT of each field decl.
7615 Also process specified field sizes, found in the DECL_INITIAL,
7616 storing 0 there after the type has been changed to precision equal
7617 to its width, rather than the precision of the specified standard
7618 type. (Correct layout requires the original type to have been preserved
7619 until now.) */
7621 saw_named_field = 0;
7622 for (x = fieldlist; x; x = DECL_CHAIN (x))
7624 if (TREE_TYPE (x) == error_mark_node)
7625 continue;
7627 DECL_CONTEXT (x) = t;
7629 /* If any field is const, the structure type is pseudo-const. */
7630 if (TREE_READONLY (x))
7631 C_TYPE_FIELDS_READONLY (t) = 1;
7632 else
7634 /* A field that is pseudo-const makes the structure likewise. */
7635 tree t1 = strip_array_types (TREE_TYPE (x));
7636 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7637 && C_TYPE_FIELDS_READONLY (t1))
7638 C_TYPE_FIELDS_READONLY (t) = 1;
7641 /* Any field that is volatile means variables of this type must be
7642 treated in some ways as volatile. */
7643 if (TREE_THIS_VOLATILE (x))
7644 C_TYPE_FIELDS_VOLATILE (t) = 1;
7646 /* Any field of nominal variable size implies structure is too. */
7647 if (C_DECL_VARIABLE_SIZE (x))
7648 C_TYPE_VARIABLE_SIZE (t) = 1;
7650 if (DECL_INITIAL (x))
7652 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7653 DECL_SIZE (x) = bitsize_int (width);
7654 DECL_BIT_FIELD (x) = 1;
7655 SET_DECL_C_BIT_FIELD (x);
7658 if (TYPE_PACKED (t)
7659 && (DECL_BIT_FIELD (x)
7660 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7661 DECL_PACKED (x) = 1;
7663 /* Detect flexible array member in an invalid context. */
7664 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7665 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7666 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7667 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7669 if (TREE_CODE (t) == UNION_TYPE)
7671 error_at (DECL_SOURCE_LOCATION (x),
7672 "flexible array member in union");
7673 TREE_TYPE (x) = error_mark_node;
7675 else if (DECL_CHAIN (x) != NULL_TREE)
7677 error_at (DECL_SOURCE_LOCATION (x),
7678 "flexible array member not at end of struct");
7679 TREE_TYPE (x) = error_mark_node;
7681 else if (!saw_named_field)
7683 error_at (DECL_SOURCE_LOCATION (x),
7684 "flexible array member in otherwise empty struct");
7685 TREE_TYPE (x) = error_mark_node;
7689 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7690 && flexible_array_type_p (TREE_TYPE (x)))
7691 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7692 "invalid use of structure with flexible array member");
7694 if (DECL_NAME (x)
7695 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7696 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7697 saw_named_field = 1;
7700 detect_field_duplicates (fieldlist);
7702 /* Now we have the nearly final fieldlist. Record it,
7703 then lay out the structure or union (including the fields). */
7705 TYPE_FIELDS (t) = fieldlist;
7707 layout_type (t);
7709 if (TYPE_SIZE_UNIT (t)
7710 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7711 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7712 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7713 error ("type %qT is too large", t);
7715 /* Give bit-fields their proper types. */
7717 tree *fieldlistp = &fieldlist;
7718 while (*fieldlistp)
7719 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7720 && TREE_TYPE (*fieldlistp) != error_mark_node)
7722 unsigned HOST_WIDE_INT width
7723 = tree_to_uhwi (DECL_INITIAL (*fieldlistp));
7724 tree type = TREE_TYPE (*fieldlistp);
7725 if (width != TYPE_PRECISION (type))
7727 TREE_TYPE (*fieldlistp)
7728 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7729 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7731 DECL_INITIAL (*fieldlistp) = 0;
7733 else
7734 fieldlistp = &DECL_CHAIN (*fieldlistp);
7737 /* Now we have the truly final field list.
7738 Store it in this type and in the variants. */
7740 TYPE_FIELDS (t) = fieldlist;
7742 /* If there are lots of fields, sort so we can look through them fast.
7743 We arbitrarily consider 16 or more elts to be "a lot". */
7746 int len = 0;
7748 for (x = fieldlist; x; x = DECL_CHAIN (x))
7750 if (len > 15 || DECL_NAME (x) == NULL)
7751 break;
7752 len += 1;
7755 if (len > 15)
7757 tree *field_array;
7758 struct lang_type *space;
7759 struct sorted_fields_type *space2;
7761 len += list_length (x);
7763 /* Use the same allocation policy here that make_node uses, to
7764 ensure that this lives as long as the rest of the struct decl.
7765 All decls in an inline function need to be saved. */
7767 space = ggc_cleared_alloc<struct lang_type> ();
7768 space2 = (sorted_fields_type *) ggc_internal_alloc
7769 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7771 len = 0;
7772 space->s = space2;
7773 field_array = &space2->elts[0];
7774 for (x = fieldlist; x; x = DECL_CHAIN (x))
7776 field_array[len++] = x;
7778 /* If there is anonymous struct or union, break out of the loop. */
7779 if (DECL_NAME (x) == NULL)
7780 break;
7782 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7783 if (x == NULL)
7785 TYPE_LANG_SPECIFIC (t) = space;
7786 TYPE_LANG_SPECIFIC (t)->s->len = len;
7787 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7788 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7793 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7795 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7796 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7797 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7798 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7799 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7802 /* If this was supposed to be a transparent union, but we can't
7803 make it one, warn and turn off the flag. */
7804 if (TREE_CODE (t) == UNION_TYPE
7805 && TYPE_TRANSPARENT_AGGR (t)
7806 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7808 TYPE_TRANSPARENT_AGGR (t) = 0;
7809 warning_at (loc, 0, "union cannot be made transparent");
7812 /* If this structure or union completes the type of any previous
7813 variable declaration, lay it out and output its rtl. */
7814 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7816 x = TREE_CHAIN (x))
7818 tree decl = TREE_VALUE (x);
7819 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7820 layout_array_type (TREE_TYPE (decl));
7821 if (TREE_CODE (decl) != TYPE_DECL)
7823 layout_decl (decl, 0);
7824 if (c_dialect_objc ())
7825 objc_check_decl (decl);
7826 rest_of_decl_compilation (decl, toplevel, 0);
7829 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7831 /* Update type location to the one of the definition, instead of e.g.
7832 a forward declaration. */
7833 if (TYPE_STUB_DECL (t))
7834 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7836 /* Finish debugging output for this type. */
7837 rest_of_type_compilation (t, toplevel);
7839 /* If we're inside a function proper, i.e. not file-scope and not still
7840 parsing parameters, then arrange for the size of a variable sized type
7841 to be bound now. */
7842 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7843 add_stmt (build_stmt (loc,
7844 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7846 if (warn_cxx_compat)
7847 warn_cxx_compat_finish_struct (fieldlist);
7849 struct_parse_info->struct_types.release ();
7850 struct_parse_info->fields.release ();
7851 struct_parse_info->typedefs_seen.release ();
7852 XDELETE (struct_parse_info);
7854 struct_parse_info = enclosing_struct_parse_info;
7856 /* If this struct is defined inside a struct, add it to
7857 struct_types. */
7858 if (warn_cxx_compat
7859 && struct_parse_info != NULL
7860 && !in_sizeof && !in_typeof && !in_alignof)
7861 struct_parse_info->struct_types.safe_push (t);
7863 return t;
7866 /* Lay out the type T, and its element type, and so on. */
7868 static void
7869 layout_array_type (tree t)
7871 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7872 layout_array_type (TREE_TYPE (t));
7873 layout_type (t);
7876 /* Begin compiling the definition of an enumeration type.
7877 NAME is its name (or null if anonymous).
7878 LOC is the enum's location.
7879 Returns the type object, as yet incomplete.
7880 Also records info about it so that build_enumerator
7881 may be used to declare the individual values as they are read. */
7883 tree
7884 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7886 tree enumtype = NULL_TREE;
7887 location_t enumloc = UNKNOWN_LOCATION;
7889 /* If this is the real definition for a previous forward reference,
7890 fill in the contents in the same object that used to be the
7891 forward reference. */
7893 if (name != NULL_TREE)
7894 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7896 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7898 enumtype = make_node (ENUMERAL_TYPE);
7899 pushtag (loc, name, enumtype);
7902 if (C_TYPE_BEING_DEFINED (enumtype))
7903 error_at (loc, "nested redefinition of %<enum %E%>", name);
7905 C_TYPE_BEING_DEFINED (enumtype) = 1;
7907 if (TYPE_VALUES (enumtype) != 0)
7909 /* This enum is a named one that has been declared already. */
7910 error_at (loc, "redeclaration of %<enum %E%>", name);
7911 if (enumloc != UNKNOWN_LOCATION)
7912 inform (enumloc, "originally defined here");
7914 /* Completely replace its old definition.
7915 The old enumerators remain defined, however. */
7916 TYPE_VALUES (enumtype) = 0;
7919 the_enum->enum_next_value = integer_zero_node;
7920 the_enum->enum_overflow = 0;
7922 if (flag_short_enums)
7923 TYPE_PACKED (enumtype) = 1;
7925 /* FIXME: This will issue a warning for a use of a type defined
7926 within sizeof in a statement expr. This is not terribly serious
7927 as C++ doesn't permit statement exprs within sizeof anyhow. */
7928 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7929 warning_at (loc, OPT_Wc___compat,
7930 "defining type in %qs expression is invalid in C++",
7931 (in_sizeof
7932 ? "sizeof"
7933 : (in_typeof ? "typeof" : "alignof")));
7935 return enumtype;
7938 /* After processing and defining all the values of an enumeration type,
7939 install their decls in the enumeration type and finish it off.
7940 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7941 and ATTRIBUTES are the specified attributes.
7942 Returns ENUMTYPE. */
7944 tree
7945 finish_enum (tree enumtype, tree values, tree attributes)
7947 tree pair, tem;
7948 tree minnode = 0, maxnode = 0;
7949 int precision;
7950 signop sign;
7951 bool toplevel = (file_scope == current_scope);
7952 struct lang_type *lt;
7954 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7956 /* Calculate the maximum value of any enumerator in this type. */
7958 if (values == error_mark_node)
7959 minnode = maxnode = integer_zero_node;
7960 else
7962 minnode = maxnode = TREE_VALUE (values);
7963 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7965 tree value = TREE_VALUE (pair);
7966 if (tree_int_cst_lt (maxnode, value))
7967 maxnode = value;
7968 if (tree_int_cst_lt (value, minnode))
7969 minnode = value;
7973 /* Construct the final type of this enumeration. It is the same
7974 as one of the integral types - the narrowest one that fits, except
7975 that normally we only go as narrow as int - and signed iff any of
7976 the values are negative. */
7977 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
7978 precision = MAX (tree_int_cst_min_precision (minnode, sign),
7979 tree_int_cst_min_precision (maxnode, sign));
7981 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7983 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
7984 if (tem == NULL)
7986 warning (0, "enumeration values exceed range of largest integer");
7987 tem = long_long_integer_type_node;
7990 else
7991 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
7993 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7994 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7995 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7996 TYPE_SIZE (enumtype) = 0;
7998 /* If the precision of the type was specific with an attribute and it
7999 was too small, give an error. Otherwise, use it. */
8000 if (TYPE_PRECISION (enumtype))
8002 if (precision > TYPE_PRECISION (enumtype))
8003 error ("specified mode too small for enumeral values");
8005 else
8006 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8008 layout_type (enumtype);
8010 if (values != error_mark_node)
8012 /* Change the type of the enumerators to be the enum type. We
8013 need to do this irrespective of the size of the enum, for
8014 proper type checking. Replace the DECL_INITIALs of the
8015 enumerators, and the value slots of the list, with copies
8016 that have the enum type; they cannot be modified in place
8017 because they may be shared (e.g. integer_zero_node) Finally,
8018 change the purpose slots to point to the names of the decls. */
8019 for (pair = values; pair; pair = TREE_CHAIN (pair))
8021 tree enu = TREE_PURPOSE (pair);
8022 tree ini = DECL_INITIAL (enu);
8024 TREE_TYPE (enu) = enumtype;
8026 /* The ISO C Standard mandates enumerators to have type int,
8027 even though the underlying type of an enum type is
8028 unspecified. However, GCC allows enumerators of any
8029 integer type as an extensions. build_enumerator()
8030 converts any enumerators that fit in an int to type int,
8031 to avoid promotions to unsigned types when comparing
8032 integers with enumerators that fit in the int range.
8033 When -pedantic is given, build_enumerator() would have
8034 already warned about those that don't fit. Here we
8035 convert the rest to the enumerator type. */
8036 if (TREE_TYPE (ini) != integer_type_node)
8037 ini = convert (enumtype, ini);
8039 DECL_INITIAL (enu) = ini;
8040 TREE_PURPOSE (pair) = DECL_NAME (enu);
8041 TREE_VALUE (pair) = ini;
8044 TYPE_VALUES (enumtype) = values;
8047 /* Record the min/max values so that we can warn about bit-field
8048 enumerations that are too small for the values. */
8049 lt = ggc_cleared_alloc<struct lang_type> ();
8050 lt->enum_min = minnode;
8051 lt->enum_max = maxnode;
8052 TYPE_LANG_SPECIFIC (enumtype) = lt;
8054 /* Fix up all variant types of this enum type. */
8055 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8057 if (tem == enumtype)
8058 continue;
8059 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8060 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8061 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8062 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8063 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8064 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8065 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8066 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
8067 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8068 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8069 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8072 /* Finish debugging output for this type. */
8073 rest_of_type_compilation (enumtype, toplevel);
8075 /* If this enum is defined inside a struct, add it to
8076 struct_types. */
8077 if (warn_cxx_compat
8078 && struct_parse_info != NULL
8079 && !in_sizeof && !in_typeof && !in_alignof)
8080 struct_parse_info->struct_types.safe_push (enumtype);
8082 return enumtype;
8085 /* Build and install a CONST_DECL for one value of the
8086 current enumeration type (one that was begun with start_enum).
8087 DECL_LOC is the location of the enumerator.
8088 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8089 Return a tree-list containing the CONST_DECL and its value.
8090 Assignment of sequential values by default is handled here. */
8092 tree
8093 build_enumerator (location_t decl_loc, location_t loc,
8094 struct c_enum_contents *the_enum, tree name, tree value)
8096 tree decl, type;
8098 /* Validate and default VALUE. */
8100 if (value != 0)
8102 /* Don't issue more errors for error_mark_node (i.e. an
8103 undeclared identifier) - just ignore the value expression. */
8104 if (value == error_mark_node)
8105 value = 0;
8106 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8108 error_at (loc, "enumerator value for %qE is not an integer constant",
8109 name);
8110 value = 0;
8112 else
8114 if (TREE_CODE (value) != INTEGER_CST)
8116 value = c_fully_fold (value, false, NULL);
8117 if (TREE_CODE (value) == INTEGER_CST)
8118 pedwarn (loc, OPT_Wpedantic,
8119 "enumerator value for %qE is not an integer "
8120 "constant expression", name);
8122 if (TREE_CODE (value) != INTEGER_CST)
8124 error ("enumerator value for %qE is not an integer constant",
8125 name);
8126 value = 0;
8128 else
8130 value = default_conversion (value);
8131 constant_expression_warning (value);
8136 /* Default based on previous value. */
8137 /* It should no longer be possible to have NON_LVALUE_EXPR
8138 in the default. */
8139 if (value == 0)
8141 value = the_enum->enum_next_value;
8142 if (the_enum->enum_overflow)
8143 error_at (loc, "overflow in enumeration values");
8145 /* Even though the underlying type of an enum is unspecified, the
8146 type of enumeration constants is explicitly defined as int
8147 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8148 an extension. */
8149 else if (!int_fits_type_p (value, integer_type_node))
8150 pedwarn (loc, OPT_Wpedantic,
8151 "ISO C restricts enumerator values to range of %<int%>");
8153 /* The ISO C Standard mandates enumerators to have type int, even
8154 though the underlying type of an enum type is unspecified.
8155 However, GCC allows enumerators of any integer type as an
8156 extensions. Here we convert any enumerators that fit in an int
8157 to type int, to avoid promotions to unsigned types when comparing
8158 integers with enumerators that fit in the int range. When
8159 -pedantic is given, we would have already warned about those that
8160 don't fit. We have to do this here rather than in finish_enum
8161 because this value may be used to define more enumerators. */
8162 if (int_fits_type_p (value, integer_type_node))
8163 value = convert (integer_type_node, value);
8165 /* Set basis for default for next value. */
8166 the_enum->enum_next_value
8167 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8168 PLUS_EXPR, value, integer_one_node, 0);
8169 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8171 /* Now create a declaration for the enum value name. */
8173 type = TREE_TYPE (value);
8174 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8175 TYPE_PRECISION (integer_type_node)),
8176 (TYPE_PRECISION (type)
8177 >= TYPE_PRECISION (integer_type_node)
8178 && TYPE_UNSIGNED (type)));
8180 decl = build_decl (decl_loc, CONST_DECL, name, type);
8181 DECL_INITIAL (decl) = convert (type, value);
8182 pushdecl (decl);
8184 return tree_cons (decl, value, NULL_TREE);
8188 /* Create the FUNCTION_DECL for a function definition.
8189 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8190 the declaration; they describe the function's name and the type it returns,
8191 but twisted together in a fashion that parallels the syntax of C.
8193 This function creates a binding context for the function body
8194 as well as setting up the FUNCTION_DECL in current_function_decl.
8196 Returns 1 on success. If the DECLARATOR is not suitable for a function
8197 (it defines a datum instead), we return 0, which tells
8198 yyparse to report a parse error. */
8201 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8202 tree attributes)
8204 tree decl1, old_decl;
8205 tree restype, resdecl;
8206 location_t loc;
8208 current_function_returns_value = 0; /* Assume, until we see it does. */
8209 current_function_returns_null = 0;
8210 current_function_returns_abnormally = 0;
8211 warn_about_return_type = 0;
8212 c_switch_stack = NULL;
8214 /* Indicate no valid break/continue context by setting these variables
8215 to some non-null, non-label value. We'll notice and emit the proper
8216 error message in c_finish_bc_stmt. */
8217 c_break_label = c_cont_label = size_zero_node;
8219 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8220 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8222 /* If the declarator is not suitable for a function definition,
8223 cause a syntax error. */
8224 if (decl1 == 0
8225 || TREE_CODE (decl1) != FUNCTION_DECL)
8226 return 0;
8228 loc = DECL_SOURCE_LOCATION (decl1);
8230 c_decl_attributes (&decl1, attributes, 0);
8232 if (DECL_DECLARED_INLINE_P (decl1)
8233 && DECL_UNINLINABLE (decl1)
8234 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8235 warning_at (loc, OPT_Wattributes,
8236 "inline function %qD given attribute noinline",
8237 decl1);
8239 /* Handle gnu_inline attribute. */
8240 if (declspecs->inline_p
8241 && !flag_gnu89_inline
8242 && TREE_CODE (decl1) == FUNCTION_DECL
8243 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8244 || current_function_decl))
8246 if (declspecs->storage_class != csc_static)
8247 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8250 announce_function (decl1);
8252 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8254 error_at (loc, "return type is an incomplete type");
8255 /* Make it return void instead. */
8256 TREE_TYPE (decl1)
8257 = build_function_type (void_type_node,
8258 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8261 if (warn_about_return_type)
8262 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8263 : (warn_return_type ? OPT_Wreturn_type
8264 : OPT_Wimplicit_int),
8265 "return type defaults to %<int%>");
8267 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8268 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8269 DECL_INITIAL (decl1) = error_mark_node;
8271 /* A nested function is not global. */
8272 if (current_function_decl != 0)
8273 TREE_PUBLIC (decl1) = 0;
8275 /* If this definition isn't a prototype and we had a prototype declaration
8276 before, copy the arg type info from that prototype. */
8277 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8278 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8279 old_decl = 0;
8280 current_function_prototype_locus = UNKNOWN_LOCATION;
8281 current_function_prototype_built_in = false;
8282 current_function_prototype_arg_types = NULL_TREE;
8283 if (!prototype_p (TREE_TYPE (decl1)))
8285 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8286 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8287 TREE_TYPE (TREE_TYPE (old_decl))))
8289 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8290 TREE_TYPE (decl1));
8291 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8292 current_function_prototype_built_in
8293 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8294 current_function_prototype_arg_types
8295 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8297 if (TREE_PUBLIC (decl1))
8299 /* If there is an external prototype declaration of this
8300 function, record its location but do not copy information
8301 to this decl. This may be an invisible declaration
8302 (built-in or in a scope which has finished) or simply
8303 have more refined argument types than any declaration
8304 found above. */
8305 struct c_binding *b;
8306 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8307 if (B_IN_SCOPE (b, external_scope))
8308 break;
8309 if (b)
8311 tree ext_decl, ext_type;
8312 ext_decl = b->decl;
8313 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8314 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8315 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8316 TREE_TYPE (ext_type)))
8318 current_function_prototype_locus
8319 = DECL_SOURCE_LOCATION (ext_decl);
8320 current_function_prototype_built_in
8321 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8322 current_function_prototype_arg_types
8323 = TYPE_ARG_TYPES (ext_type);
8329 /* Optionally warn of old-fashioned def with no previous prototype. */
8330 if (warn_strict_prototypes
8331 && old_decl != error_mark_node
8332 && !prototype_p (TREE_TYPE (decl1))
8333 && C_DECL_ISNT_PROTOTYPE (old_decl))
8334 warning_at (loc, OPT_Wstrict_prototypes,
8335 "function declaration isn%'t a prototype");
8336 /* Optionally warn of any global def with no previous prototype. */
8337 else if (warn_missing_prototypes
8338 && old_decl != error_mark_node
8339 && TREE_PUBLIC (decl1)
8340 && !MAIN_NAME_P (DECL_NAME (decl1))
8341 && C_DECL_ISNT_PROTOTYPE (old_decl)
8342 && !DECL_DECLARED_INLINE_P (decl1))
8343 warning_at (loc, OPT_Wmissing_prototypes,
8344 "no previous prototype for %qD", decl1);
8345 /* Optionally warn of any def with no previous prototype
8346 if the function has already been used. */
8347 else if (warn_missing_prototypes
8348 && old_decl != 0
8349 && old_decl != error_mark_node
8350 && TREE_USED (old_decl)
8351 && !prototype_p (TREE_TYPE (old_decl)))
8352 warning_at (loc, OPT_Wmissing_prototypes,
8353 "%qD was used with no prototype before its definition", decl1);
8354 /* Optionally warn of any global def with no previous declaration. */
8355 else if (warn_missing_declarations
8356 && TREE_PUBLIC (decl1)
8357 && old_decl == 0
8358 && !MAIN_NAME_P (DECL_NAME (decl1)))
8359 warning_at (loc, OPT_Wmissing_declarations,
8360 "no previous declaration for %qD",
8361 decl1);
8362 /* Optionally warn of any def with no previous declaration
8363 if the function has already been used. */
8364 else if (warn_missing_declarations
8365 && old_decl != 0
8366 && old_decl != error_mark_node
8367 && TREE_USED (old_decl)
8368 && C_DECL_IMPLICIT (old_decl))
8369 warning_at (loc, OPT_Wmissing_declarations,
8370 "%qD was used with no declaration before its definition", decl1);
8372 /* This function exists in static storage.
8373 (This does not mean `static' in the C sense!) */
8374 TREE_STATIC (decl1) = 1;
8376 /* This is the earliest point at which we might know the assembler
8377 name of the function. Thus, if it's set before this, die horribly. */
8378 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8380 /* If #pragma weak was used, mark the decl weak now. */
8381 if (current_scope == file_scope)
8382 maybe_apply_pragma_weak (decl1);
8384 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8385 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8387 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8388 != integer_type_node)
8389 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8390 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8391 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8392 decl1);
8394 check_main_parameter_types (decl1);
8396 if (!TREE_PUBLIC (decl1))
8397 pedwarn (loc, OPT_Wmain,
8398 "%qD is normally a non-static function", decl1);
8401 /* Record the decl so that the function name is defined.
8402 If we already have a decl for this name, and it is a FUNCTION_DECL,
8403 use the old decl. */
8405 current_function_decl = pushdecl (decl1);
8407 push_scope ();
8408 declare_parm_level ();
8410 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8411 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8412 DECL_ARTIFICIAL (resdecl) = 1;
8413 DECL_IGNORED_P (resdecl) = 1;
8414 DECL_RESULT (current_function_decl) = resdecl;
8416 start_fname_decls ();
8418 return 1;
8421 /* Subroutine of store_parm_decls which handles new-style function
8422 definitions (prototype format). The parms already have decls, so we
8423 need only record them as in effect and complain if any redundant
8424 old-style parm decls were written. */
8425 static void
8426 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8428 tree decl;
8429 c_arg_tag *tag;
8430 unsigned ix;
8432 if (current_scope->bindings)
8434 error_at (DECL_SOURCE_LOCATION (fndecl),
8435 "old-style parameter declarations in prototyped "
8436 "function definition");
8438 /* Get rid of the old-style declarations. */
8439 pop_scope ();
8440 push_scope ();
8442 /* Don't issue this warning for nested functions, and don't issue this
8443 warning if we got here because ARG_INFO_TYPES was error_mark_node
8444 (this happens when a function definition has just an ellipsis in
8445 its parameter list). */
8446 else if (!in_system_header_at (input_location)
8447 && !current_function_scope
8448 && arg_info->types != error_mark_node)
8449 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8450 "traditional C rejects ISO C style function definitions");
8452 /* Now make all the parameter declarations visible in the function body.
8453 We can bypass most of the grunt work of pushdecl. */
8454 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8456 DECL_CONTEXT (decl) = current_function_decl;
8457 if (DECL_NAME (decl))
8459 bind (DECL_NAME (decl), decl, current_scope,
8460 /*invisible=*/false, /*nested=*/false,
8461 UNKNOWN_LOCATION);
8462 if (!TREE_USED (decl))
8463 warn_if_shadowing (decl);
8465 else
8466 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8469 /* Record the parameter list in the function declaration. */
8470 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8472 /* Now make all the ancillary declarations visible, likewise. */
8473 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8475 DECL_CONTEXT (decl) = current_function_decl;
8476 if (DECL_NAME (decl))
8477 bind (DECL_NAME (decl), decl, current_scope,
8478 /*invisible=*/false,
8479 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8480 UNKNOWN_LOCATION);
8483 /* And all the tag declarations. */
8484 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8485 if (tag->id)
8486 bind (tag->id, tag->type, current_scope,
8487 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8490 /* Subroutine of store_parm_decls which handles old-style function
8491 definitions (separate parameter list and declarations). */
8493 static void
8494 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8496 struct c_binding *b;
8497 tree parm, decl, last;
8498 tree parmids = arg_info->parms;
8499 hash_set<tree> seen_args;
8501 if (!in_system_header_at (input_location))
8502 warning_at (DECL_SOURCE_LOCATION (fndecl),
8503 OPT_Wold_style_definition, "old-style function definition");
8505 /* Match each formal parameter name with its declaration. Save each
8506 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8507 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8509 if (TREE_VALUE (parm) == 0)
8511 error_at (DECL_SOURCE_LOCATION (fndecl),
8512 "parameter name missing from parameter list");
8513 TREE_PURPOSE (parm) = 0;
8514 continue;
8517 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8518 if (b && B_IN_CURRENT_SCOPE (b))
8520 decl = b->decl;
8521 /* Skip erroneous parameters. */
8522 if (decl == error_mark_node)
8523 continue;
8524 /* If we got something other than a PARM_DECL it is an error. */
8525 if (TREE_CODE (decl) != PARM_DECL)
8526 error_at (DECL_SOURCE_LOCATION (decl),
8527 "%qD declared as a non-parameter", decl);
8528 /* If the declaration is already marked, we have a duplicate
8529 name. Complain and ignore the duplicate. */
8530 else if (seen_args.contains (decl))
8532 error_at (DECL_SOURCE_LOCATION (decl),
8533 "multiple parameters named %qD", decl);
8534 TREE_PURPOSE (parm) = 0;
8535 continue;
8537 /* If the declaration says "void", complain and turn it into
8538 an int. */
8539 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8541 error_at (DECL_SOURCE_LOCATION (decl),
8542 "parameter %qD declared with void type", decl);
8543 TREE_TYPE (decl) = integer_type_node;
8544 DECL_ARG_TYPE (decl) = integer_type_node;
8545 layout_decl (decl, 0);
8547 warn_if_shadowing (decl);
8549 /* If no declaration found, default to int. */
8550 else
8552 /* FIXME diagnostics: This should be the location of the argument,
8553 not the FNDECL. E.g., for an old-style declaration
8555 int f10(v) { blah; }
8557 We should use the location of the V, not the F10.
8558 Unfortunately, the V is an IDENTIFIER_NODE which has no
8559 location. In the future we need locations for c_arg_info
8560 entries.
8562 See gcc.dg/Wshadow-3.c for an example of this problem. */
8563 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8564 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8565 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8566 pushdecl (decl);
8567 warn_if_shadowing (decl);
8569 if (flag_isoc99)
8570 pedwarn (DECL_SOURCE_LOCATION (decl),
8571 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8572 decl);
8573 else
8574 warning_at (DECL_SOURCE_LOCATION (decl),
8575 OPT_Wmissing_parameter_type,
8576 "type of %qD defaults to %<int%>", decl);
8579 TREE_PURPOSE (parm) = decl;
8580 seen_args.add (decl);
8583 /* Now examine the parms chain for incomplete declarations
8584 and declarations with no corresponding names. */
8586 for (b = current_scope->bindings; b; b = b->prev)
8588 parm = b->decl;
8589 if (TREE_CODE (parm) != PARM_DECL)
8590 continue;
8592 if (TREE_TYPE (parm) != error_mark_node
8593 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8595 error_at (DECL_SOURCE_LOCATION (parm),
8596 "parameter %qD has incomplete type", parm);
8597 TREE_TYPE (parm) = error_mark_node;
8600 if (!seen_args.contains (parm))
8602 error_at (DECL_SOURCE_LOCATION (parm),
8603 "declaration for parameter %qD but no such parameter",
8604 parm);
8606 /* Pretend the parameter was not missing.
8607 This gets us to a standard state and minimizes
8608 further error messages. */
8609 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8613 /* Chain the declarations together in the order of the list of
8614 names. Store that chain in the function decl, replacing the
8615 list of names. Update the current scope to match. */
8616 DECL_ARGUMENTS (fndecl) = 0;
8618 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8619 if (TREE_PURPOSE (parm))
8620 break;
8621 if (parm && TREE_PURPOSE (parm))
8623 last = TREE_PURPOSE (parm);
8624 DECL_ARGUMENTS (fndecl) = last;
8626 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8627 if (TREE_PURPOSE (parm))
8629 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8630 last = TREE_PURPOSE (parm);
8632 DECL_CHAIN (last) = 0;
8635 /* If there was a previous prototype,
8636 set the DECL_ARG_TYPE of each argument according to
8637 the type previously specified, and report any mismatches. */
8639 if (current_function_prototype_arg_types)
8641 tree type;
8642 for (parm = DECL_ARGUMENTS (fndecl),
8643 type = current_function_prototype_arg_types;
8644 parm || (type && TREE_VALUE (type) != error_mark_node
8645 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8646 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8648 if (parm == 0 || type == 0
8649 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8651 if (current_function_prototype_built_in)
8652 warning_at (DECL_SOURCE_LOCATION (fndecl),
8653 0, "number of arguments doesn%'t match "
8654 "built-in prototype");
8655 else
8657 /* FIXME diagnostics: This should be the location of
8658 FNDECL, but there is bug when a prototype is
8659 declared inside function context, but defined
8660 outside of it (e.g., gcc.dg/pr15698-2.c). In
8661 which case FNDECL gets the location of the
8662 prototype, not the definition. */
8663 error_at (input_location,
8664 "number of arguments doesn%'t match prototype");
8666 error_at (current_function_prototype_locus,
8667 "prototype declaration");
8669 break;
8671 /* Type for passing arg must be consistent with that
8672 declared for the arg. ISO C says we take the unqualified
8673 type for parameters declared with qualified type. */
8674 if (TREE_TYPE (parm) != error_mark_node
8675 && TREE_TYPE (type) != error_mark_node
8676 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8677 != TYPE_ATOMIC (TREE_VALUE (type)))
8678 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8679 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8681 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8682 == TYPE_ATOMIC (TREE_VALUE (type)))
8683 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8684 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8686 /* Adjust argument to match prototype. E.g. a previous
8687 `int foo(float);' prototype causes
8688 `int foo(x) float x; {...}' to be treated like
8689 `int foo(float x) {...}'. This is particularly
8690 useful for argument types like uid_t. */
8691 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8693 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8694 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8695 && TYPE_PRECISION (TREE_TYPE (parm))
8696 < TYPE_PRECISION (integer_type_node))
8697 DECL_ARG_TYPE (parm)
8698 = c_type_promotes_to (TREE_TYPE (parm));
8700 /* ??? Is it possible to get here with a
8701 built-in prototype or will it always have
8702 been diagnosed as conflicting with an
8703 old-style definition and discarded? */
8704 if (current_function_prototype_built_in)
8705 warning_at (DECL_SOURCE_LOCATION (parm),
8706 OPT_Wpedantic, "promoted argument %qD "
8707 "doesn%'t match built-in prototype", parm);
8708 else
8710 pedwarn (DECL_SOURCE_LOCATION (parm),
8711 OPT_Wpedantic, "promoted argument %qD "
8712 "doesn%'t match prototype", parm);
8713 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8714 "prototype declaration");
8717 else
8719 if (current_function_prototype_built_in)
8720 warning_at (DECL_SOURCE_LOCATION (parm),
8721 0, "argument %qD doesn%'t match "
8722 "built-in prototype", parm);
8723 else
8725 error_at (DECL_SOURCE_LOCATION (parm),
8726 "argument %qD doesn%'t match prototype", parm);
8727 error_at (current_function_prototype_locus,
8728 "prototype declaration");
8733 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8736 /* Otherwise, create a prototype that would match. */
8738 else
8740 tree actual = 0, last = 0, type;
8742 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8744 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8745 if (last)
8746 TREE_CHAIN (last) = type;
8747 else
8748 actual = type;
8749 last = type;
8751 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8752 if (last)
8753 TREE_CHAIN (last) = type;
8754 else
8755 actual = type;
8757 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8758 of the type of this function, but we need to avoid having this
8759 affect the types of other similarly-typed functions, so we must
8760 first force the generation of an identical (but separate) type
8761 node for the relevant function type. The new node we create
8762 will be a variant of the main variant of the original function
8763 type. */
8765 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8767 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8771 /* Store parameter declarations passed in ARG_INFO into the current
8772 function declaration. */
8774 void
8775 store_parm_decls_from (struct c_arg_info *arg_info)
8777 current_function_arg_info = arg_info;
8778 store_parm_decls ();
8781 /* Store the parameter declarations into the current function declaration.
8782 This is called after parsing the parameter declarations, before
8783 digesting the body of the function.
8785 For an old-style definition, construct a prototype out of the old-style
8786 parameter declarations and inject it into the function's type. */
8788 void
8789 store_parm_decls (void)
8791 tree fndecl = current_function_decl;
8792 bool proto;
8794 /* The argument information block for FNDECL. */
8795 struct c_arg_info *arg_info = current_function_arg_info;
8796 current_function_arg_info = 0;
8798 /* True if this definition is written with a prototype. Note:
8799 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8800 list in a function definition as equivalent to (void) -- an
8801 empty argument list specifies the function has no parameters,
8802 but only (void) sets up a prototype for future calls. */
8803 proto = arg_info->types != 0;
8805 if (proto)
8806 store_parm_decls_newstyle (fndecl, arg_info);
8807 else
8808 store_parm_decls_oldstyle (fndecl, arg_info);
8810 /* The next call to push_scope will be a function body. */
8812 next_is_function_body = true;
8814 /* Write a record describing this function definition to the prototypes
8815 file (if requested). */
8817 gen_aux_info_record (fndecl, 1, 0, proto);
8819 /* Initialize the RTL code for the function. */
8820 allocate_struct_function (fndecl, false);
8822 if (warn_unused_local_typedefs)
8823 cfun->language = ggc_cleared_alloc<language_function> ();
8825 /* Begin the statement tree for this function. */
8826 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8828 /* ??? Insert the contents of the pending sizes list into the function
8829 to be evaluated. The only reason left to have this is
8830 void foo(int n, int array[n++])
8831 because we throw away the array type in favor of a pointer type, and
8832 thus won't naturally see the SAVE_EXPR containing the increment. All
8833 other pending sizes would be handled by gimplify_parameters. */
8834 if (arg_info->pending_sizes)
8835 add_stmt (arg_info->pending_sizes);
8838 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8839 c_finish_omp_declare_simd for function prototypes. No diagnostics
8840 should be done. */
8842 void
8843 temp_store_parm_decls (tree fndecl, tree parms)
8845 push_scope ();
8846 for (tree p = parms; p; p = DECL_CHAIN (p))
8848 DECL_CONTEXT (p) = fndecl;
8849 if (DECL_NAME (p))
8850 bind (DECL_NAME (p), p, current_scope,
8851 /*invisible=*/false, /*nested=*/false,
8852 UNKNOWN_LOCATION);
8856 /* Undo what temp_store_parm_decls did. */
8858 void
8859 temp_pop_parm_decls (void)
8861 /* Clear all bindings in this temporary scope, so that
8862 pop_scope doesn't create a BLOCK. */
8863 struct c_binding *b = current_scope->bindings;
8864 current_scope->bindings = NULL;
8865 for (; b; b = free_binding_and_advance (b))
8867 gcc_assert (TREE_CODE (b->decl) == PARM_DECL);
8868 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8869 I_SYMBOL_BINDING (b->id) = b->shadowed;
8870 if (b->shadowed && b->shadowed->u.type)
8871 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
8873 pop_scope ();
8877 /* Finish up a function declaration and compile that function
8878 all the way to assembler language output. Then free the storage
8879 for the function definition.
8881 This is called after parsing the body of the function definition. */
8883 void
8884 finish_function (void)
8886 tree fndecl = current_function_decl;
8888 if (c_dialect_objc ())
8889 objc_finish_function ();
8891 if (TREE_CODE (fndecl) == FUNCTION_DECL
8892 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8894 tree args = DECL_ARGUMENTS (fndecl);
8895 for (; args; args = DECL_CHAIN (args))
8897 tree type = TREE_TYPE (args);
8898 if (INTEGRAL_TYPE_P (type)
8899 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8900 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
8904 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8905 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8907 /* Must mark the RESULT_DECL as being in this function. */
8909 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8910 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8912 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8913 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8914 == integer_type_node && flag_isoc99)
8916 /* Hack. We don't want the middle-end to warn that this return
8917 is unreachable, so we mark its location as special. Using
8918 UNKNOWN_LOCATION has the problem that it gets clobbered in
8919 annotate_one_with_locus. A cleaner solution might be to
8920 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8922 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8925 /* Tie off the statement tree for this function. */
8926 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8928 /* If the function has _Cilk_spawn in front of a function call inside it
8929 i.e. it is a spawning function, then add the appropriate Cilk plus
8930 functions inside. */
8931 if (fn_contains_cilk_spawn_p (cfun))
8932 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
8934 finish_fname_decls ();
8936 /* Complain if there's just no return statement. */
8937 if (warn_return_type
8938 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8939 && !current_function_returns_value && !current_function_returns_null
8940 /* Don't complain if we are no-return. */
8941 && !current_function_returns_abnormally
8942 /* Don't complain if we are declared noreturn. */
8943 && !TREE_THIS_VOLATILE (fndecl)
8944 /* Don't warn for main(). */
8945 && !MAIN_NAME_P (DECL_NAME (fndecl))
8946 /* Or if they didn't actually specify a return type. */
8947 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8948 /* Normally, with -Wreturn-type, flow will complain, but we might
8949 optimize out static functions. */
8950 && !TREE_PUBLIC (fndecl))
8952 warning (OPT_Wreturn_type,
8953 "no return statement in function returning non-void");
8954 TREE_NO_WARNING (fndecl) = 1;
8957 /* Complain about parameters that are only set, but never otherwise used. */
8958 if (warn_unused_but_set_parameter)
8960 tree decl;
8962 for (decl = DECL_ARGUMENTS (fndecl);
8963 decl;
8964 decl = DECL_CHAIN (decl))
8965 if (TREE_USED (decl)
8966 && TREE_CODE (decl) == PARM_DECL
8967 && !DECL_READ_P (decl)
8968 && DECL_NAME (decl)
8969 && !DECL_ARTIFICIAL (decl)
8970 && !TREE_NO_WARNING (decl))
8971 warning_at (DECL_SOURCE_LOCATION (decl),
8972 OPT_Wunused_but_set_parameter,
8973 "parameter %qD set but not used", decl);
8976 /* Complain about locally defined typedefs that are not used in this
8977 function. */
8978 maybe_warn_unused_local_typedefs ();
8980 /* Store the end of the function, so that we get good line number
8981 info for the epilogue. */
8982 cfun->function_end_locus = input_location;
8984 /* Finalize the ELF visibility for the function. */
8985 c_determine_visibility (fndecl);
8987 /* For GNU C extern inline functions disregard inline limits. */
8988 if (DECL_EXTERNAL (fndecl)
8989 && DECL_DECLARED_INLINE_P (fndecl))
8990 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8992 /* Genericize before inlining. Delay genericizing nested functions
8993 until their parent function is genericized. Since finalizing
8994 requires GENERIC, delay that as well. */
8996 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8997 && !undef_nested_function)
8999 if (!decl_function_context (fndecl))
9001 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9002 c_genericize (fndecl);
9004 /* ??? Objc emits functions after finalizing the compilation unit.
9005 This should be cleaned up later and this conditional removed. */
9006 if (symtab->global_info_ready)
9008 cgraph_node::add_new_function (fndecl, false);
9009 return;
9011 cgraph_node::finalize_function (fndecl, false);
9013 else
9015 /* Register this function with cgraph just far enough to get it
9016 added to our parent's nested function list. Handy, since the
9017 C front end doesn't have such a list. */
9018 (void) cgraph_node::get_create (fndecl);
9022 if (!decl_function_context (fndecl))
9023 undef_nested_function = false;
9025 if (cfun->language != NULL)
9027 ggc_free (cfun->language);
9028 cfun->language = NULL;
9031 /* We're leaving the context of this function, so zap cfun.
9032 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9033 tree_rest_of_compilation. */
9034 set_cfun (NULL);
9035 current_function_decl = NULL;
9038 /* Check the declarations given in a for-loop for satisfying the C99
9039 constraints. If exactly one such decl is found, return it. LOC is
9040 the location of the opening parenthesis of the for loop. The last
9041 parameter allows you to control the "for loop initial declarations
9042 are only allowed in C99 mode". Normally, you should pass
9043 flag_isoc99 as that parameter. But in some cases (Objective-C
9044 foreach loop, for example) we want to run the checks in this
9045 function even if not in C99 mode, so we allow the caller to turn
9046 off the error about not being in C99 mode.
9049 tree
9050 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9052 struct c_binding *b;
9053 tree one_decl = NULL_TREE;
9054 int n_decls = 0;
9056 if (!turn_off_iso_c99_error)
9058 static bool hint = true;
9059 /* If we get here, declarations have been used in a for loop without
9060 the C99 for loop scope. This doesn't make much sense, so don't
9061 allow it. */
9062 error_at (loc, "%<for%> loop initial declarations "
9063 "are only allowed in C99 or C11 mode");
9064 if (hint)
9066 inform (loc,
9067 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9068 "to compile your code");
9069 hint = false;
9071 return NULL_TREE;
9073 /* C99 subclause 6.8.5 paragraph 3:
9075 [#3] The declaration part of a for statement shall only
9076 declare identifiers for objects having storage class auto or
9077 register.
9079 It isn't clear whether, in this sentence, "identifiers" binds to
9080 "shall only declare" or to "objects" - that is, whether all identifiers
9081 declared must be identifiers for objects, or whether the restriction
9082 only applies to those that are. (A question on this in comp.std.c
9083 in November 2000 received no answer.) We implement the strictest
9084 interpretation, to avoid creating an extension which later causes
9085 problems. */
9087 for (b = current_scope->bindings; b; b = b->prev)
9089 tree id = b->id;
9090 tree decl = b->decl;
9092 if (!id)
9093 continue;
9095 switch (TREE_CODE (decl))
9097 case VAR_DECL:
9099 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9100 if (TREE_STATIC (decl))
9101 error_at (decl_loc,
9102 "declaration of static variable %qD in %<for%> loop "
9103 "initial declaration", decl);
9104 else if (DECL_EXTERNAL (decl))
9105 error_at (decl_loc,
9106 "declaration of %<extern%> variable %qD in %<for%> loop "
9107 "initial declaration", decl);
9109 break;
9111 case RECORD_TYPE:
9112 error_at (loc,
9113 "%<struct %E%> declared in %<for%> loop initial "
9114 "declaration", id);
9115 break;
9116 case UNION_TYPE:
9117 error_at (loc,
9118 "%<union %E%> declared in %<for%> loop initial declaration",
9119 id);
9120 break;
9121 case ENUMERAL_TYPE:
9122 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9123 "initial declaration", id);
9124 break;
9125 default:
9126 error_at (loc, "declaration of non-variable "
9127 "%qD in %<for%> loop initial declaration", decl);
9130 n_decls++;
9131 one_decl = decl;
9134 return n_decls == 1 ? one_decl : NULL_TREE;
9137 /* Save and reinitialize the variables
9138 used during compilation of a C function. */
9140 void
9141 c_push_function_context (void)
9143 struct language_function *p = cfun->language;
9144 /* cfun->language might have been already allocated by the use of
9145 -Wunused-local-typedefs. In that case, just re-use it. */
9146 if (p == NULL)
9147 cfun->language = p = ggc_cleared_alloc<language_function> ();
9149 p->base.x_stmt_tree = c_stmt_tree;
9150 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9151 p->x_break_label = c_break_label;
9152 p->x_cont_label = c_cont_label;
9153 p->x_switch_stack = c_switch_stack;
9154 p->arg_info = current_function_arg_info;
9155 p->returns_value = current_function_returns_value;
9156 p->returns_null = current_function_returns_null;
9157 p->returns_abnormally = current_function_returns_abnormally;
9158 p->warn_about_return_type = warn_about_return_type;
9160 push_function_context ();
9163 /* Restore the variables used during compilation of a C function. */
9165 void
9166 c_pop_function_context (void)
9168 struct language_function *p;
9170 pop_function_context ();
9171 p = cfun->language;
9173 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9174 used to store data throughout the life time of the current cfun,
9175 So don't deallocate it. */
9176 if (!warn_unused_local_typedefs)
9177 cfun->language = NULL;
9179 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9180 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9182 /* Stop pointing to the local nodes about to be freed. */
9183 /* But DECL_INITIAL must remain nonzero so we know this
9184 was an actual function definition. */
9185 DECL_INITIAL (current_function_decl) = error_mark_node;
9186 DECL_ARGUMENTS (current_function_decl) = 0;
9189 c_stmt_tree = p->base.x_stmt_tree;
9190 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9191 c_break_label = p->x_break_label;
9192 c_cont_label = p->x_cont_label;
9193 c_switch_stack = p->x_switch_stack;
9194 current_function_arg_info = p->arg_info;
9195 current_function_returns_value = p->returns_value;
9196 current_function_returns_null = p->returns_null;
9197 current_function_returns_abnormally = p->returns_abnormally;
9198 warn_about_return_type = p->warn_about_return_type;
9201 /* The functions below are required for functionality of doing
9202 function at once processing in the C front end. Currently these
9203 functions are not called from anywhere in the C front end, but as
9204 these changes continue, that will change. */
9206 /* Returns the stmt_tree (if any) to which statements are currently
9207 being added. If there is no active statement-tree, NULL is
9208 returned. */
9210 stmt_tree
9211 current_stmt_tree (void)
9213 return &c_stmt_tree;
9216 /* Return the global value of T as a symbol. */
9218 tree
9219 identifier_global_value (tree t)
9221 struct c_binding *b;
9223 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9224 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9225 return b->decl;
9227 return 0;
9230 /* In C, the only C-linkage public declaration is at file scope. */
9232 tree
9233 c_linkage_bindings (tree name)
9235 return identifier_global_value (name);
9238 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9239 otherwise the name is found in ridpointers from RID_INDEX. */
9241 void
9242 record_builtin_type (enum rid rid_index, const char *name, tree type)
9244 tree id, decl;
9245 if (name == 0)
9246 id = ridpointers[(int) rid_index];
9247 else
9248 id = get_identifier (name);
9249 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9250 pushdecl (decl);
9251 if (debug_hooks->type_decl)
9252 debug_hooks->type_decl (decl, false);
9255 /* Build the void_list_node (void_type_node having been created). */
9256 tree
9257 build_void_list_node (void)
9259 tree t = build_tree_list (NULL_TREE, void_type_node);
9260 return t;
9263 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9265 struct c_parm *
9266 build_c_parm (struct c_declspecs *specs, tree attrs,
9267 struct c_declarator *declarator)
9269 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9270 ret->specs = specs;
9271 ret->attrs = attrs;
9272 ret->declarator = declarator;
9273 return ret;
9276 /* Return a declarator with nested attributes. TARGET is the inner
9277 declarator to which these attributes apply. ATTRS are the
9278 attributes. */
9280 struct c_declarator *
9281 build_attrs_declarator (tree attrs, struct c_declarator *target)
9283 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9284 ret->kind = cdk_attrs;
9285 ret->declarator = target;
9286 ret->u.attrs = attrs;
9287 return ret;
9290 /* Return a declarator for a function with arguments specified by ARGS
9291 and return type specified by TARGET. */
9293 struct c_declarator *
9294 build_function_declarator (struct c_arg_info *args,
9295 struct c_declarator *target)
9297 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9298 ret->kind = cdk_function;
9299 ret->declarator = target;
9300 ret->u.arg_info = args;
9301 return ret;
9304 /* Return a declarator for the identifier IDENT (which may be
9305 NULL_TREE for an abstract declarator). */
9307 struct c_declarator *
9308 build_id_declarator (tree ident)
9310 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9311 ret->kind = cdk_id;
9312 ret->declarator = 0;
9313 ret->u.id = ident;
9314 /* Default value - may get reset to a more precise location. */
9315 ret->id_loc = input_location;
9316 return ret;
9319 /* Return something to represent absolute declarators containing a *.
9320 TARGET is the absolute declarator that the * contains.
9321 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9322 to apply to the pointer type. */
9324 struct c_declarator *
9325 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9326 struct c_declarator *target)
9328 tree attrs;
9329 int quals = 0;
9330 struct c_declarator *itarget = target;
9331 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9332 if (type_quals_attrs)
9334 attrs = type_quals_attrs->attrs;
9335 quals = quals_from_declspecs (type_quals_attrs);
9336 if (attrs != NULL_TREE)
9337 itarget = build_attrs_declarator (attrs, target);
9339 ret->kind = cdk_pointer;
9340 ret->declarator = itarget;
9341 ret->u.pointer_quals = quals;
9342 return ret;
9345 /* Return a pointer to a structure for an empty list of declaration
9346 specifiers. */
9348 struct c_declspecs *
9349 build_null_declspecs (void)
9351 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9352 memset (&ret->locations, 0, cdw_number_of_elements);
9353 ret->type = 0;
9354 ret->expr = 0;
9355 ret->decl_attr = 0;
9356 ret->attrs = 0;
9357 ret->align_log = -1;
9358 ret->typespec_word = cts_none;
9359 ret->storage_class = csc_none;
9360 ret->expr_const_operands = true;
9361 ret->declspecs_seen_p = false;
9362 ret->typespec_kind = ctsk_none;
9363 ret->non_sc_seen_p = false;
9364 ret->typedef_p = false;
9365 ret->explicit_signed_p = false;
9366 ret->deprecated_p = false;
9367 ret->default_int_p = false;
9368 ret->long_p = false;
9369 ret->long_long_p = false;
9370 ret->short_p = false;
9371 ret->signed_p = false;
9372 ret->unsigned_p = false;
9373 ret->complex_p = false;
9374 ret->inline_p = false;
9375 ret->noreturn_p = false;
9376 ret->thread_p = false;
9377 ret->thread_gnu_p = false;
9378 ret->const_p = false;
9379 ret->volatile_p = false;
9380 ret->atomic_p = false;
9381 ret->restrict_p = false;
9382 ret->saturating_p = false;
9383 ret->alignas_p = false;
9384 ret->address_space = ADDR_SPACE_GENERIC;
9385 return ret;
9388 /* Add the address space ADDRSPACE to the declaration specifiers
9389 SPECS, returning SPECS. */
9391 struct c_declspecs *
9392 declspecs_add_addrspace (source_location location,
9393 struct c_declspecs *specs, addr_space_t as)
9395 specs->non_sc_seen_p = true;
9396 specs->declspecs_seen_p = true;
9398 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9399 && specs->address_space != as)
9400 error ("incompatible address space qualifiers %qs and %qs",
9401 c_addr_space_name (as),
9402 c_addr_space_name (specs->address_space));
9403 else
9405 specs->address_space = as;
9406 specs->locations[cdw_address_space] = location;
9408 return specs;
9411 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9412 returning SPECS. */
9414 struct c_declspecs *
9415 declspecs_add_qual (source_location loc,
9416 struct c_declspecs *specs, tree qual)
9418 enum rid i;
9419 bool dupe = false;
9420 specs->non_sc_seen_p = true;
9421 specs->declspecs_seen_p = true;
9422 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9423 && C_IS_RESERVED_WORD (qual));
9424 i = C_RID_CODE (qual);
9425 switch (i)
9427 case RID_CONST:
9428 dupe = specs->const_p;
9429 specs->const_p = true;
9430 specs->locations[cdw_const] = loc;
9431 break;
9432 case RID_VOLATILE:
9433 dupe = specs->volatile_p;
9434 specs->volatile_p = true;
9435 specs->locations[cdw_volatile] = loc;
9436 break;
9437 case RID_RESTRICT:
9438 dupe = specs->restrict_p;
9439 specs->restrict_p = true;
9440 specs->locations[cdw_restrict] = loc;
9441 break;
9442 case RID_ATOMIC:
9443 dupe = specs->atomic_p;
9444 specs->atomic_p = true;
9445 break;
9446 default:
9447 gcc_unreachable ();
9449 if (dupe)
9450 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9451 return specs;
9454 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9455 returning SPECS. */
9457 struct c_declspecs *
9458 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9459 struct c_typespec spec)
9461 tree type = spec.spec;
9462 specs->non_sc_seen_p = true;
9463 specs->declspecs_seen_p = true;
9464 specs->typespec_kind = spec.kind;
9465 if (TREE_DEPRECATED (type))
9466 specs->deprecated_p = true;
9468 /* Handle type specifier keywords. */
9469 if (TREE_CODE (type) == IDENTIFIER_NODE
9470 && C_IS_RESERVED_WORD (type)
9471 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9473 enum rid i = C_RID_CODE (type);
9474 if (specs->type)
9476 error_at (loc, "two or more data types in declaration specifiers");
9477 return specs;
9479 if ((int) i <= (int) RID_LAST_MODIFIER)
9481 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9482 bool dupe = false;
9483 switch (i)
9485 case RID_LONG:
9486 if (specs->long_long_p)
9488 error_at (loc, "%<long long long%> is too long for GCC");
9489 break;
9491 if (specs->long_p)
9493 if (specs->typespec_word == cts_double)
9495 error_at (loc,
9496 ("both %<long long%> and %<double%> in "
9497 "declaration specifiers"));
9498 break;
9500 pedwarn_c90 (loc, OPT_Wlong_long,
9501 "ISO C90 does not support %<long long%>");
9502 specs->long_long_p = 1;
9503 specs->locations[cdw_long_long] = loc;
9504 break;
9506 if (specs->short_p)
9507 error_at (loc,
9508 ("both %<long%> and %<short%> in "
9509 "declaration specifiers"));
9510 else if (specs->typespec_word == cts_auto_type)
9511 error_at (loc,
9512 ("both %<long%> and %<__auto_type%> in "
9513 "declaration specifiers"));
9514 else if (specs->typespec_word == cts_void)
9515 error_at (loc,
9516 ("both %<long%> and %<void%> in "
9517 "declaration specifiers"));
9518 else if (specs->typespec_word == cts_int_n)
9519 error_at (loc,
9520 ("both %<long%> and %<__int%d%> in "
9521 "declaration specifiers"),
9522 int_n_data[specs->int_n_idx].bitsize);
9523 else if (specs->typespec_word == cts_bool)
9524 error_at (loc,
9525 ("both %<long%> and %<_Bool%> in "
9526 "declaration specifiers"));
9527 else if (specs->typespec_word == cts_char)
9528 error_at (loc,
9529 ("both %<long%> and %<char%> in "
9530 "declaration specifiers"));
9531 else if (specs->typespec_word == cts_float)
9532 error_at (loc,
9533 ("both %<long%> and %<float%> in "
9534 "declaration specifiers"));
9535 else if (specs->typespec_word == cts_dfloat32)
9536 error_at (loc,
9537 ("both %<long%> and %<_Decimal32%> in "
9538 "declaration specifiers"));
9539 else if (specs->typespec_word == cts_dfloat64)
9540 error_at (loc,
9541 ("both %<long%> and %<_Decimal64%> in "
9542 "declaration specifiers"));
9543 else if (specs->typespec_word == cts_dfloat128)
9544 error_at (loc,
9545 ("both %<long%> and %<_Decimal128%> in "
9546 "declaration specifiers"));
9547 else
9549 specs->long_p = true;
9550 specs->locations[cdw_long] = loc;
9552 break;
9553 case RID_SHORT:
9554 dupe = specs->short_p;
9555 if (specs->long_p)
9556 error_at (loc,
9557 ("both %<long%> and %<short%> in "
9558 "declaration specifiers"));
9559 else if (specs->typespec_word == cts_auto_type)
9560 error_at (loc,
9561 ("both %<short%> and %<__auto_type%> in "
9562 "declaration specifiers"));
9563 else if (specs->typespec_word == cts_void)
9564 error_at (loc,
9565 ("both %<short%> and %<void%> in "
9566 "declaration specifiers"));
9567 else if (specs->typespec_word == cts_int_n)
9568 error_at (loc,
9569 ("both %<short%> and %<__int%d%> in "
9570 "declaration specifiers"),
9571 int_n_data[specs->int_n_idx].bitsize);
9572 else if (specs->typespec_word == cts_bool)
9573 error_at (loc,
9574 ("both %<short%> and %<_Bool%> in "
9575 "declaration specifiers"));
9576 else if (specs->typespec_word == cts_char)
9577 error_at (loc,
9578 ("both %<short%> and %<char%> in "
9579 "declaration specifiers"));
9580 else if (specs->typespec_word == cts_float)
9581 error_at (loc,
9582 ("both %<short%> and %<float%> in "
9583 "declaration specifiers"));
9584 else if (specs->typespec_word == cts_double)
9585 error_at (loc,
9586 ("both %<short%> and %<double%> in "
9587 "declaration specifiers"));
9588 else if (specs->typespec_word == cts_dfloat32)
9589 error_at (loc,
9590 ("both %<short%> and %<_Decimal32%> in "
9591 "declaration specifiers"));
9592 else if (specs->typespec_word == cts_dfloat64)
9593 error_at (loc,
9594 ("both %<short%> and %<_Decimal64%> in "
9595 "declaration specifiers"));
9596 else if (specs->typespec_word == cts_dfloat128)
9597 error_at (loc,
9598 ("both %<short%> and %<_Decimal128%> in "
9599 "declaration specifiers"));
9600 else
9602 specs->short_p = true;
9603 specs->locations[cdw_short] = loc;
9605 break;
9606 case RID_SIGNED:
9607 dupe = specs->signed_p;
9608 if (specs->unsigned_p)
9609 error_at (loc,
9610 ("both %<signed%> and %<unsigned%> in "
9611 "declaration specifiers"));
9612 else if (specs->typespec_word == cts_auto_type)
9613 error_at (loc,
9614 ("both %<signed%> and %<__auto_type%> in "
9615 "declaration specifiers"));
9616 else if (specs->typespec_word == cts_void)
9617 error_at (loc,
9618 ("both %<signed%> and %<void%> in "
9619 "declaration specifiers"));
9620 else if (specs->typespec_word == cts_bool)
9621 error_at (loc,
9622 ("both %<signed%> and %<_Bool%> in "
9623 "declaration specifiers"));
9624 else if (specs->typespec_word == cts_float)
9625 error_at (loc,
9626 ("both %<signed%> and %<float%> in "
9627 "declaration specifiers"));
9628 else if (specs->typespec_word == cts_double)
9629 error_at (loc,
9630 ("both %<signed%> and %<double%> in "
9631 "declaration specifiers"));
9632 else if (specs->typespec_word == cts_dfloat32)
9633 error_at (loc,
9634 ("both %<signed%> and %<_Decimal32%> in "
9635 "declaration specifiers"));
9636 else if (specs->typespec_word == cts_dfloat64)
9637 error_at (loc,
9638 ("both %<signed%> and %<_Decimal64%> in "
9639 "declaration specifiers"));
9640 else if (specs->typespec_word == cts_dfloat128)
9641 error_at (loc,
9642 ("both %<signed%> and %<_Decimal128%> in "
9643 "declaration specifiers"));
9644 else
9646 specs->signed_p = true;
9647 specs->locations[cdw_signed] = loc;
9649 break;
9650 case RID_UNSIGNED:
9651 dupe = specs->unsigned_p;
9652 if (specs->signed_p)
9653 error_at (loc,
9654 ("both %<signed%> and %<unsigned%> in "
9655 "declaration specifiers"));
9656 else if (specs->typespec_word == cts_auto_type)
9657 error_at (loc,
9658 ("both %<unsigned%> and %<__auto_type%> in "
9659 "declaration specifiers"));
9660 else if (specs->typespec_word == cts_void)
9661 error_at (loc,
9662 ("both %<unsigned%> and %<void%> in "
9663 "declaration specifiers"));
9664 else if (specs->typespec_word == cts_bool)
9665 error_at (loc,
9666 ("both %<unsigned%> and %<_Bool%> in "
9667 "declaration specifiers"));
9668 else if (specs->typespec_word == cts_float)
9669 error_at (loc,
9670 ("both %<unsigned%> and %<float%> in "
9671 "declaration specifiers"));
9672 else if (specs->typespec_word == cts_double)
9673 error_at (loc,
9674 ("both %<unsigned%> and %<double%> in "
9675 "declaration specifiers"));
9676 else if (specs->typespec_word == cts_dfloat32)
9677 error_at (loc,
9678 ("both %<unsigned%> and %<_Decimal32%> in "
9679 "declaration specifiers"));
9680 else if (specs->typespec_word == cts_dfloat64)
9681 error_at (loc,
9682 ("both %<unsigned%> and %<_Decimal64%> in "
9683 "declaration specifiers"));
9684 else if (specs->typespec_word == cts_dfloat128)
9685 error_at (loc,
9686 ("both %<unsigned%> and %<_Decimal128%> in "
9687 "declaration specifiers"));
9688 else
9690 specs->unsigned_p = true;
9691 specs->locations[cdw_unsigned] = loc;
9693 break;
9694 case RID_COMPLEX:
9695 dupe = specs->complex_p;
9696 if (!in_system_header_at (loc))
9697 pedwarn_c90 (loc, OPT_Wpedantic,
9698 "ISO C90 does not support complex types");
9699 if (specs->typespec_word == cts_auto_type)
9700 error_at (loc,
9701 ("both %<complex%> and %<__auto_type%> in "
9702 "declaration specifiers"));
9703 else if (specs->typespec_word == cts_void)
9704 error_at (loc,
9705 ("both %<complex%> and %<void%> in "
9706 "declaration specifiers"));
9707 else if (specs->typespec_word == cts_bool)
9708 error_at (loc,
9709 ("both %<complex%> and %<_Bool%> in "
9710 "declaration specifiers"));
9711 else if (specs->typespec_word == cts_dfloat32)
9712 error_at (loc,
9713 ("both %<complex%> and %<_Decimal32%> in "
9714 "declaration specifiers"));
9715 else if (specs->typespec_word == cts_dfloat64)
9716 error_at (loc,
9717 ("both %<complex%> and %<_Decimal64%> in "
9718 "declaration specifiers"));
9719 else if (specs->typespec_word == cts_dfloat128)
9720 error_at (loc,
9721 ("both %<complex%> and %<_Decimal128%> in "
9722 "declaration specifiers"));
9723 else if (specs->typespec_word == cts_fract)
9724 error_at (loc,
9725 ("both %<complex%> and %<_Fract%> in "
9726 "declaration specifiers"));
9727 else if (specs->typespec_word == cts_accum)
9728 error_at (loc,
9729 ("both %<complex%> and %<_Accum%> in "
9730 "declaration specifiers"));
9731 else if (specs->saturating_p)
9732 error_at (loc,
9733 ("both %<complex%> and %<_Sat%> in "
9734 "declaration specifiers"));
9735 else
9737 specs->complex_p = true;
9738 specs->locations[cdw_complex] = loc;
9740 break;
9741 case RID_SAT:
9742 dupe = specs->saturating_p;
9743 pedwarn (loc, OPT_Wpedantic,
9744 "ISO C does not support saturating types");
9745 if (specs->typespec_word == cts_int_n)
9747 error_at (loc,
9748 ("both %<_Sat%> and %<__int%d%> in "
9749 "declaration specifiers"),
9750 int_n_data[specs->int_n_idx].bitsize);
9752 else if (specs->typespec_word == cts_auto_type)
9753 error_at (loc,
9754 ("both %<_Sat%> and %<__auto_type%> in "
9755 "declaration specifiers"));
9756 else if (specs->typespec_word == cts_void)
9757 error_at (loc,
9758 ("both %<_Sat%> and %<void%> in "
9759 "declaration specifiers"));
9760 else if (specs->typespec_word == cts_bool)
9761 error_at (loc,
9762 ("both %<_Sat%> and %<_Bool%> in "
9763 "declaration specifiers"));
9764 else if (specs->typespec_word == cts_char)
9765 error_at (loc,
9766 ("both %<_Sat%> and %<char%> in "
9767 "declaration specifiers"));
9768 else if (specs->typespec_word == cts_int)
9769 error_at (loc,
9770 ("both %<_Sat%> and %<int%> in "
9771 "declaration specifiers"));
9772 else if (specs->typespec_word == cts_float)
9773 error_at (loc,
9774 ("both %<_Sat%> and %<float%> in "
9775 "declaration specifiers"));
9776 else if (specs->typespec_word == cts_double)
9777 error_at (loc,
9778 ("both %<_Sat%> and %<double%> in "
9779 "declaration specifiers"));
9780 else if (specs->typespec_word == cts_dfloat32)
9781 error_at (loc,
9782 ("both %<_Sat%> and %<_Decimal32%> in "
9783 "declaration specifiers"));
9784 else if (specs->typespec_word == cts_dfloat64)
9785 error_at (loc,
9786 ("both %<_Sat%> and %<_Decimal64%> in "
9787 "declaration specifiers"));
9788 else if (specs->typespec_word == cts_dfloat128)
9789 error_at (loc,
9790 ("both %<_Sat%> and %<_Decimal128%> in "
9791 "declaration specifiers"));
9792 else if (specs->complex_p)
9793 error_at (loc,
9794 ("both %<_Sat%> and %<complex%> in "
9795 "declaration specifiers"));
9796 else
9798 specs->saturating_p = true;
9799 specs->locations[cdw_saturating] = loc;
9801 break;
9802 default:
9803 gcc_unreachable ();
9806 if (dupe)
9807 error_at (loc, "duplicate %qE", type);
9809 return specs;
9811 else
9813 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9814 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
9815 "__auto_type". */
9816 if (specs->typespec_word != cts_none)
9818 error_at (loc,
9819 "two or more data types in declaration specifiers");
9820 return specs;
9822 switch (i)
9824 case RID_AUTO_TYPE:
9825 if (specs->long_p)
9826 error_at (loc,
9827 ("both %<long%> and %<__auto_type%> in "
9828 "declaration specifiers"));
9829 else if (specs->short_p)
9830 error_at (loc,
9831 ("both %<short%> and %<__auto_type%> in "
9832 "declaration specifiers"));
9833 else if (specs->signed_p)
9834 error_at (loc,
9835 ("both %<signed%> and %<__auto_type%> in "
9836 "declaration specifiers"));
9837 else if (specs->unsigned_p)
9838 error_at (loc,
9839 ("both %<unsigned%> and %<__auto_type%> in "
9840 "declaration specifiers"));
9841 else if (specs->complex_p)
9842 error_at (loc,
9843 ("both %<complex%> and %<__auto_type%> in "
9844 "declaration specifiers"));
9845 else if (specs->saturating_p)
9846 error_at (loc,
9847 ("both %<_Sat%> and %<__auto_type%> in "
9848 "declaration specifiers"));
9849 else
9851 specs->typespec_word = cts_auto_type;
9852 specs->locations[cdw_typespec] = loc;
9854 return specs;
9855 case RID_INT_N_0:
9856 case RID_INT_N_1:
9857 case RID_INT_N_2:
9858 case RID_INT_N_3:
9859 specs->int_n_idx = i - RID_INT_N_0;
9860 if (!in_system_header_at (input_location))
9861 pedwarn (loc, OPT_Wpedantic,
9862 "ISO C does not support %<__int%d%> types",
9863 int_n_data[specs->int_n_idx].bitsize);
9865 if (specs->long_p)
9866 error_at (loc,
9867 ("both %<__int%d%> and %<long%> in "
9868 "declaration specifiers"),
9869 int_n_data[specs->int_n_idx].bitsize);
9870 else if (specs->saturating_p)
9871 error_at (loc,
9872 ("both %<_Sat%> and %<__int%d%> in "
9873 "declaration specifiers"),
9874 int_n_data[specs->int_n_idx].bitsize);
9875 else if (specs->short_p)
9876 error_at (loc,
9877 ("both %<__int%d%> and %<short%> in "
9878 "declaration specifiers"),
9879 int_n_data[specs->int_n_idx].bitsize);
9880 else if (! int_n_enabled_p [specs->int_n_idx])
9881 error_at (loc,
9882 "%<__int%d%> is not supported on this target",
9883 int_n_data[specs->int_n_idx].bitsize);
9884 else
9886 specs->typespec_word = cts_int_n;
9887 specs->locations[cdw_typespec] = loc;
9889 return specs;
9890 case RID_VOID:
9891 if (specs->long_p)
9892 error_at (loc,
9893 ("both %<long%> and %<void%> in "
9894 "declaration specifiers"));
9895 else if (specs->short_p)
9896 error_at (loc,
9897 ("both %<short%> and %<void%> in "
9898 "declaration specifiers"));
9899 else if (specs->signed_p)
9900 error_at (loc,
9901 ("both %<signed%> and %<void%> in "
9902 "declaration specifiers"));
9903 else if (specs->unsigned_p)
9904 error_at (loc,
9905 ("both %<unsigned%> and %<void%> in "
9906 "declaration specifiers"));
9907 else if (specs->complex_p)
9908 error_at (loc,
9909 ("both %<complex%> and %<void%> in "
9910 "declaration specifiers"));
9911 else if (specs->saturating_p)
9912 error_at (loc,
9913 ("both %<_Sat%> and %<void%> in "
9914 "declaration specifiers"));
9915 else
9917 specs->typespec_word = cts_void;
9918 specs->locations[cdw_typespec] = loc;
9920 return specs;
9921 case RID_BOOL:
9922 if (!in_system_header_at (loc))
9923 pedwarn_c90 (loc, OPT_Wpedantic,
9924 "ISO C90 does not support boolean types");
9925 if (specs->long_p)
9926 error_at (loc,
9927 ("both %<long%> and %<_Bool%> in "
9928 "declaration specifiers"));
9929 else if (specs->short_p)
9930 error_at (loc,
9931 ("both %<short%> and %<_Bool%> in "
9932 "declaration specifiers"));
9933 else if (specs->signed_p)
9934 error_at (loc,
9935 ("both %<signed%> and %<_Bool%> in "
9936 "declaration specifiers"));
9937 else if (specs->unsigned_p)
9938 error_at (loc,
9939 ("both %<unsigned%> and %<_Bool%> in "
9940 "declaration specifiers"));
9941 else if (specs->complex_p)
9942 error_at (loc,
9943 ("both %<complex%> and %<_Bool%> in "
9944 "declaration specifiers"));
9945 else if (specs->saturating_p)
9946 error_at (loc,
9947 ("both %<_Sat%> and %<_Bool%> in "
9948 "declaration specifiers"));
9949 else
9951 specs->typespec_word = cts_bool;
9952 specs->locations[cdw_typespec] = loc;
9954 return specs;
9955 case RID_CHAR:
9956 if (specs->long_p)
9957 error_at (loc,
9958 ("both %<long%> and %<char%> in "
9959 "declaration specifiers"));
9960 else if (specs->short_p)
9961 error_at (loc,
9962 ("both %<short%> and %<char%> in "
9963 "declaration specifiers"));
9964 else if (specs->saturating_p)
9965 error_at (loc,
9966 ("both %<_Sat%> and %<char%> in "
9967 "declaration specifiers"));
9968 else
9970 specs->typespec_word = cts_char;
9971 specs->locations[cdw_typespec] = loc;
9973 return specs;
9974 case RID_INT:
9975 if (specs->saturating_p)
9976 error_at (loc,
9977 ("both %<_Sat%> and %<int%> in "
9978 "declaration specifiers"));
9979 else
9981 specs->typespec_word = cts_int;
9982 specs->locations[cdw_typespec] = loc;
9984 return specs;
9985 case RID_FLOAT:
9986 if (specs->long_p)
9987 error_at (loc,
9988 ("both %<long%> and %<float%> in "
9989 "declaration specifiers"));
9990 else if (specs->short_p)
9991 error_at (loc,
9992 ("both %<short%> and %<float%> in "
9993 "declaration specifiers"));
9994 else if (specs->signed_p)
9995 error_at (loc,
9996 ("both %<signed%> and %<float%> in "
9997 "declaration specifiers"));
9998 else if (specs->unsigned_p)
9999 error_at (loc,
10000 ("both %<unsigned%> and %<float%> in "
10001 "declaration specifiers"));
10002 else if (specs->saturating_p)
10003 error_at (loc,
10004 ("both %<_Sat%> and %<float%> in "
10005 "declaration specifiers"));
10006 else
10008 specs->typespec_word = cts_float;
10009 specs->locations[cdw_typespec] = loc;
10011 return specs;
10012 case RID_DOUBLE:
10013 if (specs->long_long_p)
10014 error_at (loc,
10015 ("both %<long long%> and %<double%> in "
10016 "declaration specifiers"));
10017 else if (specs->short_p)
10018 error_at (loc,
10019 ("both %<short%> and %<double%> in "
10020 "declaration specifiers"));
10021 else if (specs->signed_p)
10022 error_at (loc,
10023 ("both %<signed%> and %<double%> in "
10024 "declaration specifiers"));
10025 else if (specs->unsigned_p)
10026 error_at (loc,
10027 ("both %<unsigned%> and %<double%> in "
10028 "declaration specifiers"));
10029 else if (specs->saturating_p)
10030 error_at (loc,
10031 ("both %<_Sat%> and %<double%> in "
10032 "declaration specifiers"));
10033 else
10035 specs->typespec_word = cts_double;
10036 specs->locations[cdw_typespec] = loc;
10038 return specs;
10039 case RID_DFLOAT32:
10040 case RID_DFLOAT64:
10041 case RID_DFLOAT128:
10043 const char *str;
10044 if (i == RID_DFLOAT32)
10045 str = "_Decimal32";
10046 else if (i == RID_DFLOAT64)
10047 str = "_Decimal64";
10048 else
10049 str = "_Decimal128";
10050 if (specs->long_long_p)
10051 error_at (loc,
10052 ("both %<long long%> and %<%s%> in "
10053 "declaration specifiers"),
10054 str);
10055 if (specs->long_p)
10056 error_at (loc,
10057 ("both %<long%> and %<%s%> in "
10058 "declaration specifiers"),
10059 str);
10060 else if (specs->short_p)
10061 error_at (loc,
10062 ("both %<short%> and %<%s%> in "
10063 "declaration specifiers"),
10064 str);
10065 else if (specs->signed_p)
10066 error_at (loc,
10067 ("both %<signed%> and %<%s%> in "
10068 "declaration specifiers"),
10069 str);
10070 else if (specs->unsigned_p)
10071 error_at (loc,
10072 ("both %<unsigned%> and %<%s%> in "
10073 "declaration specifiers"),
10074 str);
10075 else if (specs->complex_p)
10076 error_at (loc,
10077 ("both %<complex%> and %<%s%> in "
10078 "declaration specifiers"),
10079 str);
10080 else if (specs->saturating_p)
10081 error_at (loc,
10082 ("both %<_Sat%> and %<%s%> in "
10083 "declaration specifiers"),
10084 str);
10085 else if (i == RID_DFLOAT32)
10086 specs->typespec_word = cts_dfloat32;
10087 else if (i == RID_DFLOAT64)
10088 specs->typespec_word = cts_dfloat64;
10089 else
10090 specs->typespec_word = cts_dfloat128;
10091 specs->locations[cdw_typespec] = loc;
10093 if (!targetm.decimal_float_supported_p ())
10094 error_at (loc,
10095 ("decimal floating point not supported "
10096 "for this target"));
10097 pedwarn (loc, OPT_Wpedantic,
10098 "ISO C does not support decimal floating point");
10099 return specs;
10100 case RID_FRACT:
10101 case RID_ACCUM:
10103 const char *str;
10104 if (i == RID_FRACT)
10105 str = "_Fract";
10106 else
10107 str = "_Accum";
10108 if (specs->complex_p)
10109 error_at (loc,
10110 ("both %<complex%> and %<%s%> in "
10111 "declaration specifiers"),
10112 str);
10113 else if (i == RID_FRACT)
10114 specs->typespec_word = cts_fract;
10115 else
10116 specs->typespec_word = cts_accum;
10117 specs->locations[cdw_typespec] = loc;
10119 if (!targetm.fixed_point_supported_p ())
10120 error_at (loc,
10121 "fixed-point types not supported for this target");
10122 pedwarn (loc, OPT_Wpedantic,
10123 "ISO C does not support fixed-point types");
10124 return specs;
10125 default:
10126 /* ObjC reserved word "id", handled below. */
10127 break;
10132 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10133 form of ObjC type, cases such as "int" and "long" being handled
10134 above), a TYPE (struct, union, enum and typeof specifiers) or an
10135 ERROR_MARK. In none of these cases may there have previously
10136 been any type specifiers. */
10137 if (specs->type || specs->typespec_word != cts_none
10138 || specs->long_p || specs->short_p || specs->signed_p
10139 || specs->unsigned_p || specs->complex_p)
10140 error_at (loc, "two or more data types in declaration specifiers");
10141 else if (TREE_CODE (type) == TYPE_DECL)
10143 if (TREE_TYPE (type) == error_mark_node)
10144 ; /* Allow the type to default to int to avoid cascading errors. */
10145 else
10147 specs->type = TREE_TYPE (type);
10148 specs->decl_attr = DECL_ATTRIBUTES (type);
10149 specs->typedef_p = true;
10150 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10151 specs->locations[cdw_typedef] = loc;
10153 /* If this typedef name is defined in a struct, then a C++
10154 lookup would return a different value. */
10155 if (warn_cxx_compat
10156 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10157 warning_at (loc, OPT_Wc___compat,
10158 "C++ lookup of %qD would return a field, not a type",
10159 type);
10161 /* If we are parsing a struct, record that a struct field
10162 used a typedef. */
10163 if (warn_cxx_compat && struct_parse_info != NULL)
10164 struct_parse_info->typedefs_seen.safe_push (type);
10167 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10169 tree t = lookup_name (type);
10170 if (!t || TREE_CODE (t) != TYPE_DECL)
10171 error_at (loc, "%qE fails to be a typedef or built in type", type);
10172 else if (TREE_TYPE (t) == error_mark_node)
10174 else
10176 specs->type = TREE_TYPE (t);
10177 specs->locations[cdw_typespec] = loc;
10180 else
10182 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10184 specs->typedef_p = true;
10185 specs->locations[cdw_typedef] = loc;
10186 if (spec.expr)
10188 if (specs->expr)
10189 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10190 specs->expr, spec.expr);
10191 else
10192 specs->expr = spec.expr;
10193 specs->expr_const_operands &= spec.expr_const_operands;
10196 specs->type = type;
10199 return specs;
10202 /* Add the storage class specifier or function specifier SCSPEC to the
10203 declaration specifiers SPECS, returning SPECS. */
10205 struct c_declspecs *
10206 declspecs_add_scspec (source_location loc,
10207 struct c_declspecs *specs,
10208 tree scspec)
10210 enum rid i;
10211 enum c_storage_class n = csc_none;
10212 bool dupe = false;
10213 specs->declspecs_seen_p = true;
10214 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10215 && C_IS_RESERVED_WORD (scspec));
10216 i = C_RID_CODE (scspec);
10217 if (specs->non_sc_seen_p)
10218 warning (OPT_Wold_style_declaration,
10219 "%qE is not at beginning of declaration", scspec);
10220 switch (i)
10222 case RID_INLINE:
10223 /* C99 permits duplicate inline. Although of doubtful utility,
10224 it seems simplest to permit it in gnu89 mode as well, as
10225 there is also little utility in maintaining this as a
10226 difference between gnu89 and C99 inline. */
10227 dupe = false;
10228 specs->inline_p = true;
10229 specs->locations[cdw_inline] = loc;
10230 break;
10231 case RID_NORETURN:
10232 /* Duplicate _Noreturn is permitted. */
10233 dupe = false;
10234 specs->noreturn_p = true;
10235 specs->locations[cdw_noreturn] = loc;
10236 break;
10237 case RID_THREAD:
10238 dupe = specs->thread_p;
10239 if (specs->storage_class == csc_auto)
10240 error ("%qE used with %<auto%>", scspec);
10241 else if (specs->storage_class == csc_register)
10242 error ("%qE used with %<register%>", scspec);
10243 else if (specs->storage_class == csc_typedef)
10244 error ("%qE used with %<typedef%>", scspec);
10245 else
10247 specs->thread_p = true;
10248 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10249 "__thread") == 0);
10250 /* A diagnostic is not required for the use of this
10251 identifier in the implementation namespace; only diagnose
10252 it for the C11 spelling because of existing code using
10253 the other spelling. */
10254 if (!specs->thread_gnu_p)
10256 if (flag_isoc99)
10257 pedwarn_c99 (loc, OPT_Wpedantic,
10258 "ISO C99 does not support %qE", scspec);
10259 else
10260 pedwarn_c99 (loc, OPT_Wpedantic,
10261 "ISO C90 does not support %qE", scspec);
10263 specs->locations[cdw_thread] = loc;
10265 break;
10266 case RID_AUTO:
10267 n = csc_auto;
10268 break;
10269 case RID_EXTERN:
10270 n = csc_extern;
10271 /* Diagnose "__thread extern". */
10272 if (specs->thread_p && specs->thread_gnu_p)
10273 error ("%<__thread%> before %<extern%>");
10274 break;
10275 case RID_REGISTER:
10276 n = csc_register;
10277 break;
10278 case RID_STATIC:
10279 n = csc_static;
10280 /* Diagnose "__thread static". */
10281 if (specs->thread_p && specs->thread_gnu_p)
10282 error ("%<__thread%> before %<static%>");
10283 break;
10284 case RID_TYPEDEF:
10285 n = csc_typedef;
10286 break;
10287 default:
10288 gcc_unreachable ();
10290 if (n != csc_none && n == specs->storage_class)
10291 dupe = true;
10292 if (dupe)
10294 if (i == RID_THREAD)
10295 error ("duplicate %<_Thread_local%> or %<__thread%>");
10296 else
10297 error ("duplicate %qE", scspec);
10299 if (n != csc_none)
10301 if (specs->storage_class != csc_none && n != specs->storage_class)
10303 error ("multiple storage classes in declaration specifiers");
10305 else
10307 specs->storage_class = n;
10308 specs->locations[cdw_storage_class] = loc;
10309 if (n != csc_extern && n != csc_static && specs->thread_p)
10311 error ("%qs used with %qE",
10312 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10313 scspec);
10314 specs->thread_p = false;
10318 return specs;
10321 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10322 returning SPECS. */
10324 struct c_declspecs *
10325 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10327 specs->attrs = chainon (attrs, specs->attrs);
10328 specs->locations[cdw_attributes] = loc;
10329 specs->declspecs_seen_p = true;
10330 return specs;
10333 /* Add an _Alignas specifier (expression ALIGN, or type whose
10334 alignment is ALIGN) to the declaration specifiers SPECS, returning
10335 SPECS. */
10336 struct c_declspecs *
10337 declspecs_add_alignas (source_location loc,
10338 struct c_declspecs *specs, tree align)
10340 int align_log;
10341 specs->alignas_p = true;
10342 specs->locations[cdw_alignas] = loc;
10343 if (align == error_mark_node)
10344 return specs;
10345 align_log = check_user_alignment (align, true);
10346 if (align_log > specs->align_log)
10347 specs->align_log = align_log;
10348 return specs;
10351 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10352 specifiers with any other type specifier to determine the resulting
10353 type. This is where ISO C checks on complex types are made, since
10354 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10355 double". */
10357 struct c_declspecs *
10358 finish_declspecs (struct c_declspecs *specs)
10360 /* If a type was specified as a whole, we have no modifiers and are
10361 done. */
10362 if (specs->type != NULL_TREE)
10364 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10365 && !specs->signed_p && !specs->unsigned_p
10366 && !specs->complex_p);
10368 /* Set a dummy type. */
10369 if (TREE_CODE (specs->type) == ERROR_MARK)
10370 specs->type = integer_type_node;
10371 return specs;
10374 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10375 has been specified, treat it as "int" unless "_Complex" is
10376 present and there are no other specifiers. If we just have
10377 "_Complex", it is equivalent to "_Complex double", but e.g.
10378 "_Complex short" is equivalent to "_Complex short int". */
10379 if (specs->typespec_word == cts_none)
10381 if (specs->saturating_p)
10383 error_at (specs->locations[cdw_saturating],
10384 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10385 if (!targetm.fixed_point_supported_p ())
10386 error_at (specs->locations[cdw_saturating],
10387 "fixed-point types not supported for this target");
10388 specs->typespec_word = cts_fract;
10390 else if (specs->long_p || specs->short_p
10391 || specs->signed_p || specs->unsigned_p)
10393 specs->typespec_word = cts_int;
10395 else if (specs->complex_p)
10397 specs->typespec_word = cts_double;
10398 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10399 "ISO C does not support plain %<complex%> meaning "
10400 "%<double complex%>");
10402 else
10404 specs->typespec_word = cts_int;
10405 specs->default_int_p = true;
10406 /* We don't diagnose this here because grokdeclarator will
10407 give more specific diagnostics according to whether it is
10408 a function definition. */
10412 /* If "signed" was specified, record this to distinguish "int" and
10413 "signed int" in the case of a bit-field with
10414 -funsigned-bitfields. */
10415 specs->explicit_signed_p = specs->signed_p;
10417 /* Now compute the actual type. */
10418 switch (specs->typespec_word)
10420 case cts_auto_type:
10421 gcc_assert (!specs->long_p && !specs->short_p
10422 && !specs->signed_p && !specs->unsigned_p
10423 && !specs->complex_p);
10424 /* Type to be filled in later. */
10425 break;
10426 case cts_void:
10427 gcc_assert (!specs->long_p && !specs->short_p
10428 && !specs->signed_p && !specs->unsigned_p
10429 && !specs->complex_p);
10430 specs->type = void_type_node;
10431 break;
10432 case cts_bool:
10433 gcc_assert (!specs->long_p && !specs->short_p
10434 && !specs->signed_p && !specs->unsigned_p
10435 && !specs->complex_p);
10436 specs->type = boolean_type_node;
10437 break;
10438 case cts_char:
10439 gcc_assert (!specs->long_p && !specs->short_p);
10440 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10441 if (specs->signed_p)
10442 specs->type = signed_char_type_node;
10443 else if (specs->unsigned_p)
10444 specs->type = unsigned_char_type_node;
10445 else
10446 specs->type = char_type_node;
10447 if (specs->complex_p)
10449 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10450 "ISO C does not support complex integer types");
10451 specs->type = build_complex_type (specs->type);
10453 break;
10454 case cts_int_n:
10455 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10456 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10457 specs->type = (specs->unsigned_p
10458 ? int_n_trees[specs->int_n_idx].unsigned_type
10459 : int_n_trees[specs->int_n_idx].signed_type);
10460 if (specs->complex_p)
10462 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10463 "ISO C does not support complex integer types");
10464 specs->type = build_complex_type (specs->type);
10466 break;
10467 case cts_int:
10468 gcc_assert (!(specs->long_p && specs->short_p));
10469 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10470 if (specs->long_long_p)
10471 specs->type = (specs->unsigned_p
10472 ? long_long_unsigned_type_node
10473 : long_long_integer_type_node);
10474 else if (specs->long_p)
10475 specs->type = (specs->unsigned_p
10476 ? long_unsigned_type_node
10477 : long_integer_type_node);
10478 else if (specs->short_p)
10479 specs->type = (specs->unsigned_p
10480 ? short_unsigned_type_node
10481 : short_integer_type_node);
10482 else
10483 specs->type = (specs->unsigned_p
10484 ? unsigned_type_node
10485 : integer_type_node);
10486 if (specs->complex_p)
10488 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10489 "ISO C does not support complex integer types");
10490 specs->type = build_complex_type (specs->type);
10492 break;
10493 case cts_float:
10494 gcc_assert (!specs->long_p && !specs->short_p
10495 && !specs->signed_p && !specs->unsigned_p);
10496 specs->type = (specs->complex_p
10497 ? complex_float_type_node
10498 : float_type_node);
10499 break;
10500 case cts_double:
10501 gcc_assert (!specs->long_long_p && !specs->short_p
10502 && !specs->signed_p && !specs->unsigned_p);
10503 if (specs->long_p)
10505 specs->type = (specs->complex_p
10506 ? complex_long_double_type_node
10507 : long_double_type_node);
10509 else
10511 specs->type = (specs->complex_p
10512 ? complex_double_type_node
10513 : double_type_node);
10515 break;
10516 case cts_dfloat32:
10517 case cts_dfloat64:
10518 case cts_dfloat128:
10519 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10520 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10521 if (specs->typespec_word == cts_dfloat32)
10522 specs->type = dfloat32_type_node;
10523 else if (specs->typespec_word == cts_dfloat64)
10524 specs->type = dfloat64_type_node;
10525 else
10526 specs->type = dfloat128_type_node;
10527 break;
10528 case cts_fract:
10529 gcc_assert (!specs->complex_p);
10530 if (!targetm.fixed_point_supported_p ())
10531 specs->type = integer_type_node;
10532 else if (specs->saturating_p)
10534 if (specs->long_long_p)
10535 specs->type = specs->unsigned_p
10536 ? sat_unsigned_long_long_fract_type_node
10537 : sat_long_long_fract_type_node;
10538 else if (specs->long_p)
10539 specs->type = specs->unsigned_p
10540 ? sat_unsigned_long_fract_type_node
10541 : sat_long_fract_type_node;
10542 else if (specs->short_p)
10543 specs->type = specs->unsigned_p
10544 ? sat_unsigned_short_fract_type_node
10545 : sat_short_fract_type_node;
10546 else
10547 specs->type = specs->unsigned_p
10548 ? sat_unsigned_fract_type_node
10549 : sat_fract_type_node;
10551 else
10553 if (specs->long_long_p)
10554 specs->type = specs->unsigned_p
10555 ? unsigned_long_long_fract_type_node
10556 : long_long_fract_type_node;
10557 else if (specs->long_p)
10558 specs->type = specs->unsigned_p
10559 ? unsigned_long_fract_type_node
10560 : long_fract_type_node;
10561 else if (specs->short_p)
10562 specs->type = specs->unsigned_p
10563 ? unsigned_short_fract_type_node
10564 : short_fract_type_node;
10565 else
10566 specs->type = specs->unsigned_p
10567 ? unsigned_fract_type_node
10568 : fract_type_node;
10570 break;
10571 case cts_accum:
10572 gcc_assert (!specs->complex_p);
10573 if (!targetm.fixed_point_supported_p ())
10574 specs->type = integer_type_node;
10575 else if (specs->saturating_p)
10577 if (specs->long_long_p)
10578 specs->type = specs->unsigned_p
10579 ? sat_unsigned_long_long_accum_type_node
10580 : sat_long_long_accum_type_node;
10581 else if (specs->long_p)
10582 specs->type = specs->unsigned_p
10583 ? sat_unsigned_long_accum_type_node
10584 : sat_long_accum_type_node;
10585 else if (specs->short_p)
10586 specs->type = specs->unsigned_p
10587 ? sat_unsigned_short_accum_type_node
10588 : sat_short_accum_type_node;
10589 else
10590 specs->type = specs->unsigned_p
10591 ? sat_unsigned_accum_type_node
10592 : sat_accum_type_node;
10594 else
10596 if (specs->long_long_p)
10597 specs->type = specs->unsigned_p
10598 ? unsigned_long_long_accum_type_node
10599 : long_long_accum_type_node;
10600 else if (specs->long_p)
10601 specs->type = specs->unsigned_p
10602 ? unsigned_long_accum_type_node
10603 : long_accum_type_node;
10604 else if (specs->short_p)
10605 specs->type = specs->unsigned_p
10606 ? unsigned_short_accum_type_node
10607 : short_accum_type_node;
10608 else
10609 specs->type = specs->unsigned_p
10610 ? unsigned_accum_type_node
10611 : accum_type_node;
10613 break;
10614 default:
10615 gcc_unreachable ();
10618 return specs;
10621 /* A subroutine of c_write_global_declarations. Perform final processing
10622 on one file scope's declarations (or the external scope's declarations),
10623 GLOBALS. */
10625 static void
10626 c_write_global_declarations_1 (tree globals)
10628 tree decl;
10629 bool reconsider;
10631 /* Process the decls in the order they were written. */
10632 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10634 /* Check for used but undefined static functions using the C
10635 standard's definition of "used", and set TREE_NO_WARNING so
10636 that check_global_declarations doesn't repeat the check. */
10637 if (TREE_CODE (decl) == FUNCTION_DECL
10638 && DECL_INITIAL (decl) == 0
10639 && DECL_EXTERNAL (decl)
10640 && !TREE_PUBLIC (decl)
10641 && C_DECL_USED (decl))
10643 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10644 TREE_NO_WARNING (decl) = 1;
10647 wrapup_global_declaration_1 (decl);
10652 reconsider = false;
10653 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10654 reconsider |= wrapup_global_declaration_2 (decl);
10656 while (reconsider);
10658 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10659 check_global_declaration_1 (decl);
10662 /* A subroutine of c_write_global_declarations Emit debug information for each
10663 of the declarations in GLOBALS. */
10665 static void
10666 c_write_global_declarations_2 (tree globals)
10668 tree decl;
10670 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
10671 debug_hooks->global_decl (decl);
10674 /* Callback to collect a source_ref from a DECL. */
10676 static void
10677 collect_source_ref_cb (tree decl)
10679 if (!DECL_IS_BUILTIN (decl))
10680 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10683 /* Preserve the external declarations scope across a garbage collect. */
10684 static GTY(()) tree ext_block;
10686 /* Collect all references relevant to SOURCE_FILE. */
10688 static void
10689 collect_all_refs (const char *source_file)
10691 tree t;
10692 unsigned i;
10694 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10695 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10697 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10700 /* Iterate over all global declarations and call CALLBACK. */
10702 static void
10703 for_each_global_decl (void (*callback) (tree decl))
10705 tree t;
10706 tree decls;
10707 tree decl;
10708 unsigned i;
10710 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10712 decls = DECL_INITIAL (t);
10713 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10714 callback (decl);
10717 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10718 callback (decl);
10721 void
10722 c_write_global_declarations (void)
10724 tree t;
10725 unsigned i;
10727 /* We don't want to do this if generating a PCH. */
10728 if (pch_file)
10729 return;
10731 timevar_start (TV_PHASE_DEFERRED);
10733 /* Do the Objective-C stuff. This is where all the Objective-C
10734 module stuff gets generated (symtab, class/protocol/selector
10735 lists etc). */
10736 if (c_dialect_objc ())
10737 objc_write_global_declarations ();
10739 /* Close the external scope. */
10740 ext_block = pop_scope ();
10741 external_scope = 0;
10742 gcc_assert (!current_scope);
10744 /* Handle -fdump-ada-spec[-slim]. */
10745 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10747 /* Build a table of files to generate specs for */
10748 if (flag_dump_ada_spec_slim)
10749 collect_source_ref (main_input_filename);
10750 else
10751 for_each_global_decl (collect_source_ref_cb);
10753 dump_ada_specs (collect_all_refs, NULL);
10756 if (ext_block)
10758 tree tmp = BLOCK_VARS (ext_block);
10759 int flags;
10760 FILE * stream = dump_begin (TDI_tu, &flags);
10761 if (stream && tmp)
10763 dump_node (tmp, flags & ~TDF_SLIM, stream);
10764 dump_end (TDI_tu, stream);
10768 /* Process all file scopes in this compilation, and the external_scope,
10769 through wrapup_global_declarations and check_global_declarations. */
10770 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10771 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10772 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10774 timevar_stop (TV_PHASE_DEFERRED);
10775 timevar_start (TV_PHASE_OPT_GEN);
10777 /* We're done parsing; proceed to optimize and emit assembly.
10778 FIXME: shouldn't be the front end's responsibility to call this. */
10779 symtab->finalize_compilation_unit ();
10781 timevar_stop (TV_PHASE_OPT_GEN);
10782 timevar_start (TV_PHASE_DBGINFO);
10784 /* After cgraph has had a chance to emit everything that's going to
10785 be emitted, output debug information for globals. */
10786 if (!seen_error ())
10788 timevar_push (TV_SYMOUT);
10789 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10790 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10791 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10792 timevar_pop (TV_SYMOUT);
10795 ext_block = NULL;
10796 timevar_stop (TV_PHASE_DBGINFO);
10799 /* Register reserved keyword WORD as qualifier for address space AS. */
10801 void
10802 c_register_addr_space (const char *word, addr_space_t as)
10804 int rid = RID_FIRST_ADDR_SPACE + as;
10805 tree id;
10807 /* Address space qualifiers are only supported
10808 in C with GNU extensions enabled. */
10809 if (c_dialect_objc () || flag_no_asm)
10810 return;
10812 id = get_identifier (word);
10813 C_SET_RID_CODE (id, rid);
10814 C_IS_RESERVED_WORD (id) = 1;
10815 ridpointers [rid] = id;
10818 /* Return identifier to look up for omp declare reduction. */
10820 tree
10821 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
10823 const char *p = NULL;
10824 switch (reduction_code)
10826 case PLUS_EXPR: p = "+"; break;
10827 case MULT_EXPR: p = "*"; break;
10828 case MINUS_EXPR: p = "-"; break;
10829 case BIT_AND_EXPR: p = "&"; break;
10830 case BIT_XOR_EXPR: p = "^"; break;
10831 case BIT_IOR_EXPR: p = "|"; break;
10832 case TRUTH_ANDIF_EXPR: p = "&&"; break;
10833 case TRUTH_ORIF_EXPR: p = "||"; break;
10834 case MIN_EXPR: p = "min"; break;
10835 case MAX_EXPR: p = "max"; break;
10836 default:
10837 break;
10840 if (p == NULL)
10842 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
10843 return error_mark_node;
10844 p = IDENTIFIER_POINTER (reduction_id);
10847 const char prefix[] = "omp declare reduction ";
10848 size_t lenp = sizeof (prefix);
10849 size_t len = strlen (p);
10850 char *name = XALLOCAVEC (char, lenp + len);
10851 memcpy (name, prefix, lenp - 1);
10852 memcpy (name + lenp - 1, p, len + 1);
10853 return get_identifier (name);
10856 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10857 VAR_DECL, bind it into the current scope and return it. */
10859 tree
10860 c_omp_reduction_decl (tree reduction_id)
10862 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10863 if (b != NULL && B_IN_CURRENT_SCOPE (b))
10864 return b->decl;
10866 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
10867 reduction_id, integer_type_node);
10868 DECL_ARTIFICIAL (decl) = 1;
10869 DECL_EXTERNAL (decl) = 1;
10870 TREE_STATIC (decl) = 1;
10871 TREE_PUBLIC (decl) = 0;
10872 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
10873 return decl;
10876 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10878 tree
10879 c_omp_reduction_lookup (tree reduction_id, tree type)
10881 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10882 while (b)
10884 tree t;
10885 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
10886 if (comptypes (TREE_PURPOSE (t), type))
10887 return TREE_VALUE (t);
10888 b = b->shadowed;
10890 return error_mark_node;
10893 /* Helper function called via walk_tree, to diagnose invalid
10894 #pragma omp declare reduction combiners or initializers. */
10896 tree
10897 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
10899 tree *vars = (tree *) data;
10900 if (SSA_VAR_P (*tp)
10901 && !DECL_ARTIFICIAL (*tp)
10902 && *tp != vars[0]
10903 && *tp != vars[1])
10905 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
10906 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
10907 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
10908 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10909 *tp);
10910 else
10911 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
10912 "to variable %qD which is not %<omp_priv%> nor "
10913 "%<omp_orig%>",
10914 *tp);
10915 return *tp;
10917 return NULL_TREE;
10920 #include "gt-c-c-decl.h"