PR c/77323
[official-gcc.git] / gcc / c / c-decl.c
blob0c52a64a77eaf593fc347d7dfdfb6005ba8acb6e
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2016 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 "target.h"
31 #include "function.h"
32 #include "c-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "cgraph.h"
36 #include "intl.h"
37 #include "print-tree.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "toplev.h"
42 #include "debug.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-pragma.h"
45 #include "c-family/c-ubsan.h"
46 #include "c-lang.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
49 #include "dumpfile.h"
50 #include "plugin.h"
51 #include "c-family/c-ada-spec.h"
52 #include "cilk.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
57 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
58 enum decl_context
59 { NORMAL, /* Ordinary declaration */
60 FUNCDEF, /* Function definition */
61 PARM, /* Declaration of parm before function body */
62 FIELD, /* Declaration inside struct or union */
63 TYPENAME}; /* Typename (inside cast or sizeof) */
65 /* States indicating how grokdeclarator() should handle declspecs marked
66 with __attribute__((deprecated)). An object declared as
67 __attribute__((deprecated)) suppresses warnings of uses of other
68 deprecated items. */
70 enum deprecated_states {
71 DEPRECATED_NORMAL,
72 DEPRECATED_SUPPRESS
76 /* Nonzero if we have seen an invalid cross reference
77 to a struct, union, or enum, but not yet printed the message. */
78 tree pending_invalid_xref;
80 /* File and line to appear in the eventual error message. */
81 location_t pending_invalid_xref_location;
83 /* The file and line that the prototype came from if this is an
84 old-style definition; used for diagnostics in
85 store_parm_decls_oldstyle. */
87 static location_t current_function_prototype_locus;
89 /* Whether this prototype was built-in. */
91 static bool current_function_prototype_built_in;
93 /* The argument type information of this prototype. */
95 static tree current_function_prototype_arg_types;
97 /* The argument information structure for the function currently being
98 defined. */
100 static struct c_arg_info *current_function_arg_info;
102 /* The obstack on which parser and related data structures, which are
103 not live beyond their top-level declaration or definition, are
104 allocated. */
105 struct obstack parser_obstack;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree;
111 /* State saving variables. */
112 tree c_break_label;
113 tree c_cont_label;
115 /* A list of decls to be made automatically visible in each file scope. */
116 static GTY(()) tree visible_builtins;
118 /* Set to 0 at beginning of a function definition, set to 1 if
119 a return statement that specifies a return value is seen. */
121 int current_function_returns_value;
123 /* Set to 0 at beginning of a function definition, set to 1 if
124 a return statement with no argument is seen. */
126 int current_function_returns_null;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a call to a noreturn function is seen. */
131 int current_function_returns_abnormally;
133 /* Set to nonzero by `grokdeclarator' for a function
134 whose return type is defaulted, if warnings for this are desired. */
136 static int warn_about_return_type;
138 /* Nonzero when the current toplevel function contains a declaration
139 of a nested function which is never defined. */
141 static bool undef_nested_function;
143 /* If non-zero, implicit "omp declare target" attribute is added into the
144 attribute lists. */
145 int current_omp_declare_target_attribute;
147 /* Each c_binding structure describes one binding of an identifier to
148 a decl. All the decls in a scope - irrespective of namespace - are
149 chained together by the ->prev field, which (as the name implies)
150 runs in reverse order. All the decls in a given namespace bound to
151 a given identifier are chained by the ->shadowed field, which runs
152 from inner to outer scopes.
154 The ->decl field usually points to a DECL node, but there are two
155 exceptions. In the namespace of type tags, the bound entity is a
156 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
157 identifier is encountered, it is bound to error_mark_node to
158 suppress further errors about that identifier in the current
159 function.
161 The ->u.type field stores the type of the declaration in this scope;
162 if NULL, the type is the type of the ->decl field. This is only of
163 relevance for objects with external or internal linkage which may
164 be redeclared in inner scopes, forming composite types that only
165 persist for the duration of those scopes. In the external scope,
166 this stores the composite of all the types declared for this
167 object, visible or not. The ->inner_comp field (used only at file
168 scope) stores whether an incomplete array type at file scope was
169 completed at an inner scope to an array size other than 1.
171 The ->u.label field is used for labels. It points to a structure
172 which stores additional information used for warnings.
174 The depth field is copied from the scope structure that holds this
175 decl. It is used to preserve the proper ordering of the ->shadowed
176 field (see bind()) and also for a handful of special-case checks.
177 Finally, the invisible bit is true for a decl which should be
178 ignored for purposes of normal name lookup, and the nested bit is
179 true for a decl that's been bound a second time in an inner scope;
180 in all such cases, the binding in the outer scope will have its
181 invisible bit true. */
183 struct GTY((chain_next ("%h.prev"))) c_binding {
184 union GTY(()) { /* first so GTY desc can use decl */
185 tree GTY((tag ("0"))) type; /* the type in this scope */
186 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
187 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
188 tree decl; /* the decl bound */
189 tree id; /* the identifier it's bound to */
190 struct c_binding *prev; /* the previous decl in this scope */
191 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
192 unsigned int depth : 28; /* depth of this scope */
193 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
194 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
195 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
196 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
197 location_t locus; /* location for nested bindings */
199 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
200 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
201 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
202 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
204 /* Each C symbol points to three linked lists of c_binding structures.
205 These describe the values of the identifier in the three different
206 namespaces defined by the language. */
208 struct GTY(()) lang_identifier {
209 struct c_common_identifier common_id;
210 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
211 struct c_binding *tag_binding; /* struct/union/enum tags */
212 struct c_binding *label_binding; /* labels */
215 /* Validate c-lang.c's assumptions. */
216 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
217 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
219 /* The binding oracle; see c-tree.h. */
220 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
222 /* This flag is set on an identifier if we have previously asked the
223 binding oracle for this identifier's symbol binding. */
224 #define I_SYMBOL_CHECKED(node) \
225 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
227 static inline struct c_binding* *
228 i_symbol_binding (tree node)
230 struct lang_identifier *lid
231 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
233 if (lid->symbol_binding == NULL
234 && c_binding_oracle != NULL
235 && !I_SYMBOL_CHECKED (node))
237 /* Set the "checked" flag first, to avoid infinite recursion
238 when the binding oracle calls back into gcc. */
239 I_SYMBOL_CHECKED (node) = 1;
240 c_binding_oracle (C_ORACLE_SYMBOL, node);
243 return &lid->symbol_binding;
246 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
248 #define I_SYMBOL_DECL(node) \
249 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
251 /* This flag is set on an identifier if we have previously asked the
252 binding oracle for this identifier's tag binding. */
253 #define I_TAG_CHECKED(node) \
254 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
256 static inline struct c_binding **
257 i_tag_binding (tree node)
259 struct lang_identifier *lid
260 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
262 if (lid->tag_binding == NULL
263 && c_binding_oracle != NULL
264 && !I_TAG_CHECKED (node))
266 /* Set the "checked" flag first, to avoid infinite recursion
267 when the binding oracle calls back into gcc. */
268 I_TAG_CHECKED (node) = 1;
269 c_binding_oracle (C_ORACLE_TAG, node);
272 return &lid->tag_binding;
275 #define I_TAG_BINDING(node) (*i_tag_binding (node))
277 #define I_TAG_DECL(node) \
278 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
280 /* This flag is set on an identifier if we have previously asked the
281 binding oracle for this identifier's label binding. */
282 #define I_LABEL_CHECKED(node) \
283 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
285 static inline struct c_binding **
286 i_label_binding (tree node)
288 struct lang_identifier *lid
289 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
291 if (lid->label_binding == NULL
292 && c_binding_oracle != NULL
293 && !I_LABEL_CHECKED (node))
295 /* Set the "checked" flag first, to avoid infinite recursion
296 when the binding oracle calls back into gcc. */
297 I_LABEL_CHECKED (node) = 1;
298 c_binding_oracle (C_ORACLE_LABEL, node);
301 return &lid->label_binding;
304 #define I_LABEL_BINDING(node) (*i_label_binding (node))
306 #define I_LABEL_DECL(node) \
307 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
309 /* The resulting tree type. */
311 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
312 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
314 union tree_node GTY ((tag ("0"),
315 desc ("tree_node_structure (&%h)")))
316 generic;
317 struct lang_identifier GTY ((tag ("1"))) identifier;
320 /* Track bindings and other things that matter for goto warnings. For
321 efficiency, we do not gather all the decls at the point of
322 definition. Instead, we point into the bindings structure. As
323 scopes are popped, we update these structures and gather the decls
324 that matter at that time. */
326 struct GTY(()) c_spot_bindings {
327 /* The currently open scope which holds bindings defined when the
328 label was defined or the goto statement was found. */
329 struct c_scope *scope;
330 /* The bindings in the scope field which were defined at the point
331 of the label or goto. This lets us look at older or newer
332 bindings in the scope, as appropriate. */
333 struct c_binding *bindings_in_scope;
334 /* The number of statement expressions that have started since this
335 label or goto statement was defined. This is zero if we are at
336 the same statement expression level. It is positive if we are in
337 a statement expression started since this spot. It is negative
338 if this spot was in a statement expression and we have left
339 it. */
340 int stmt_exprs;
341 /* Whether we started in a statement expression but are no longer in
342 it. This is set to true if stmt_exprs ever goes negative. */
343 bool left_stmt_expr;
346 /* This structure is used to keep track of bindings seen when a goto
347 statement is defined. This is only used if we see the goto
348 statement before we see the label. */
350 struct GTY(()) c_goto_bindings {
351 /* The location of the goto statement. */
352 location_t loc;
353 /* The bindings of the goto statement. */
354 struct c_spot_bindings goto_bindings;
357 typedef struct c_goto_bindings *c_goto_bindings_p;
359 /* The additional information we keep track of for a label binding.
360 These fields are updated as scopes are popped. */
362 struct GTY(()) c_label_vars {
363 /* The shadowed c_label_vars, when one label shadows another (which
364 can only happen using a __label__ declaration). */
365 struct c_label_vars *shadowed;
366 /* The bindings when the label was defined. */
367 struct c_spot_bindings label_bindings;
368 /* A list of decls that we care about: decls about which we should
369 warn if a goto branches to this label from later in the function.
370 Decls are added to this list as scopes are popped. We only add
371 the decls that matter. */
372 vec<tree, va_gc> *decls_in_scope;
373 /* A list of goto statements to this label. This is only used for
374 goto statements seen before the label was defined, so that we can
375 issue appropriate warnings for them. */
376 vec<c_goto_bindings_p, va_gc> *gotos;
379 /* Each c_scope structure describes the complete contents of one
380 scope. Four scopes are distinguished specially: the innermost or
381 current scope, the innermost function scope, the file scope (always
382 the second to outermost) and the outermost or external scope.
384 Most declarations are recorded in the current scope.
386 All normal label declarations are recorded in the innermost
387 function scope, as are bindings of undeclared identifiers to
388 error_mark_node. (GCC permits nested functions as an extension,
389 hence the 'innermost' qualifier.) Explicitly declared labels
390 (using the __label__ extension) appear in the current scope.
392 Being in the file scope (current_scope == file_scope) causes
393 special behavior in several places below. Also, under some
394 conditions the Objective-C front end records declarations in the
395 file scope even though that isn't the current scope.
397 All declarations with external linkage are recorded in the external
398 scope, even if they aren't visible there; this models the fact that
399 such declarations are visible to the entire program, and (with a
400 bit of cleverness, see pushdecl) allows diagnosis of some violations
401 of C99 6.2.2p7 and 6.2.7p2:
403 If, within the same translation unit, the same identifier appears
404 with both internal and external linkage, the behavior is
405 undefined.
407 All declarations that refer to the same object or function shall
408 have compatible type; otherwise, the behavior is undefined.
410 Initially only the built-in declarations, which describe compiler
411 intrinsic functions plus a subset of the standard library, are in
412 this scope.
414 The order of the blocks list matters, and it is frequently appended
415 to. To avoid having to walk all the way to the end of the list on
416 each insertion, or reverse the list later, we maintain a pointer to
417 the last list entry. (FIXME: It should be feasible to use a reversed
418 list here.)
420 The bindings list is strictly in reverse order of declarations;
421 pop_scope relies on this. */
424 struct GTY((chain_next ("%h.outer"))) c_scope {
425 /* The scope containing this one. */
426 struct c_scope *outer;
428 /* The next outermost function scope. */
429 struct c_scope *outer_function;
431 /* All bindings in this scope. */
432 struct c_binding *bindings;
434 /* For each scope (except the global one), a chain of BLOCK nodes
435 for all the scopes that were entered and exited one level down. */
436 tree blocks;
437 tree blocks_last;
439 /* The depth of this scope. Used to keep the ->shadowed chain of
440 bindings sorted innermost to outermost. */
441 unsigned int depth : 28;
443 /* True if we are currently filling this scope with parameter
444 declarations. */
445 BOOL_BITFIELD parm_flag : 1;
447 /* True if we saw [*] in this scope. Used to give an error messages
448 if these appears in a function definition. */
449 BOOL_BITFIELD had_vla_unspec : 1;
451 /* True if we already complained about forward parameter decls
452 in this scope. This prevents double warnings on
453 foo (int a; int b; ...) */
454 BOOL_BITFIELD warned_forward_parm_decls : 1;
456 /* True if this is the outermost block scope of a function body.
457 This scope contains the parameters, the local variables declared
458 in the outermost block, and all the labels (except those in
459 nested functions, or declared at block scope with __label__). */
460 BOOL_BITFIELD function_body : 1;
462 /* True means make a BLOCK for this scope no matter what. */
463 BOOL_BITFIELD keep : 1;
465 /* True means that an unsuffixed float constant is _Decimal64. */
466 BOOL_BITFIELD float_const_decimal64 : 1;
468 /* True if this scope has any label bindings. This is used to speed
469 up searching for labels when popping scopes, particularly since
470 labels are normally only found at function scope. */
471 BOOL_BITFIELD has_label_bindings : 1;
473 /* True if we should issue a warning if a goto statement crosses any
474 of the bindings. We still need to check the list of bindings to
475 find the specific ones we need to warn about. This is true if
476 decl_jump_unsafe would return true for any of the bindings. This
477 is used to avoid looping over all the bindings unnecessarily. */
478 BOOL_BITFIELD has_jump_unsafe_decl : 1;
481 /* The scope currently in effect. */
483 static GTY(()) struct c_scope *current_scope;
485 /* The innermost function scope. Ordinary (not explicitly declared)
486 labels, bindings to error_mark_node, and the lazily-created
487 bindings of __func__ and its friends get this scope. */
489 static GTY(()) struct c_scope *current_function_scope;
491 /* The C file scope. This is reset for each input translation unit. */
493 static GTY(()) struct c_scope *file_scope;
495 /* The outermost scope. This is used for all declarations with
496 external linkage, and only these, hence the name. */
498 static GTY(()) struct c_scope *external_scope;
500 /* A chain of c_scope structures awaiting reuse. */
502 static GTY((deletable)) struct c_scope *scope_freelist;
504 /* A chain of c_binding structures awaiting reuse. */
506 static GTY((deletable)) struct c_binding *binding_freelist;
508 /* Append VAR to LIST in scope SCOPE. */
509 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
510 struct c_scope *s_ = (scope); \
511 tree d_ = (decl); \
512 if (s_->list##_last) \
513 BLOCK_CHAIN (s_->list##_last) = d_; \
514 else \
515 s_->list = d_; \
516 s_->list##_last = d_; \
517 } while (0)
519 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
520 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
521 struct c_scope *t_ = (tscope); \
522 struct c_scope *f_ = (fscope); \
523 if (t_->to##_last) \
524 BLOCK_CHAIN (t_->to##_last) = f_->from; \
525 else \
526 t_->to = f_->from; \
527 t_->to##_last = f_->from##_last; \
528 } while (0)
530 /* A c_inline_static structure stores details of a static identifier
531 referenced in a definition of a function that may be an inline
532 definition if no subsequent declaration of that function uses
533 "extern" or does not use "inline". */
535 struct GTY((chain_next ("%h.next"))) c_inline_static {
536 /* The location for a diagnostic. */
537 location_t location;
539 /* The function that may be an inline definition. */
540 tree function;
542 /* The object or function referenced. */
543 tree static_decl;
545 /* What sort of reference this is. */
546 enum c_inline_static_type type;
548 /* The next such structure or NULL. */
549 struct c_inline_static *next;
552 /* List of static identifiers used or referenced in functions that may
553 be inline definitions. */
554 static GTY(()) struct c_inline_static *c_inline_statics;
556 /* True means unconditionally make a BLOCK for the next scope pushed. */
558 static bool keep_next_level_flag;
560 /* True means the next call to push_scope will be the outermost scope
561 of a function body, so do not push a new scope, merely cease
562 expecting parameter decls. */
564 static bool next_is_function_body;
566 /* A vector of pointers to c_binding structures. */
568 typedef struct c_binding *c_binding_ptr;
570 /* Information that we keep for a struct or union while it is being
571 parsed. */
573 struct c_struct_parse_info
575 /* If warn_cxx_compat, a list of types defined within this
576 struct. */
577 auto_vec<tree> struct_types;
578 /* If warn_cxx_compat, a list of field names which have bindings,
579 and which are defined in this struct, but which are not defined
580 in any enclosing struct. This is used to clear the in_struct
581 field of the c_bindings structure. */
582 auto_vec<c_binding_ptr> fields;
583 /* If warn_cxx_compat, a list of typedef names used when defining
584 fields in this struct. */
585 auto_vec<tree> typedefs_seen;
588 /* Information for the struct or union currently being parsed, or
589 NULL if not parsing a struct or union. */
590 static struct c_struct_parse_info *struct_parse_info;
592 /* Forward declarations. */
593 static tree lookup_name_in_scope (tree, struct c_scope *);
594 static tree c_make_fname_decl (location_t, tree, int);
595 static tree grokdeclarator (const struct c_declarator *,
596 struct c_declspecs *,
597 enum decl_context, bool, tree *, tree *, tree *,
598 bool *, enum deprecated_states);
599 static tree grokparms (struct c_arg_info *, bool);
600 static void layout_array_type (tree);
601 static void warn_defaults_to (location_t, int, const char *, ...)
602 ATTRIBUTE_GCC_DIAG(3,4);
604 /* T is a statement. Add it to the statement-tree. This is the
605 C/ObjC version--C++ has a slightly different version of this
606 function. */
608 tree
609 add_stmt (tree t)
611 enum tree_code code = TREE_CODE (t);
613 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
615 if (!EXPR_HAS_LOCATION (t))
616 SET_EXPR_LOCATION (t, input_location);
619 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
620 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
622 /* Add T to the statement-tree. Non-side-effect statements need to be
623 recorded during statement expressions. */
624 if (!building_stmt_list_p ())
625 push_stmt_list ();
626 append_to_statement_list_force (t, &cur_stmt_list);
628 return t;
631 /* Build a pointer type using the default pointer mode. */
633 static tree
634 c_build_pointer_type (tree to_type)
636 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
637 : TYPE_ADDR_SPACE (to_type);
638 machine_mode pointer_mode;
640 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
641 pointer_mode = targetm.addr_space.pointer_mode (as);
642 else
643 pointer_mode = c_default_pointer_mode;
644 return build_pointer_type_for_mode (to_type, pointer_mode, false);
648 /* Return true if we will want to say something if a goto statement
649 crosses DECL. */
651 static bool
652 decl_jump_unsafe (tree decl)
654 if (error_operand_p (decl))
655 return false;
657 /* Always warn about crossing variably modified types. */
658 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
659 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
660 return true;
662 /* Otherwise, only warn if -Wgoto-misses-init and this is an
663 initialized automatic decl. */
664 if (warn_jump_misses_init
665 && VAR_P (decl)
666 && !TREE_STATIC (decl)
667 && DECL_INITIAL (decl) != NULL_TREE)
668 return true;
670 return false;
674 void
675 c_print_identifier (FILE *file, tree node, int indent)
677 void (*save) (enum c_oracle_request, tree identifier);
679 /* Temporarily hide any binding oracle. Without this, calls to
680 debug_tree from the debugger will end up calling into the oracle,
681 making for a confusing debug session. As the oracle isn't needed
682 here for normal operation, it's simplest to suppress it. */
683 save = c_binding_oracle;
684 c_binding_oracle = NULL;
686 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
687 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
688 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
689 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
691 tree rid = ridpointers[C_RID_CODE (node)];
692 indent_to (file, indent + 4);
693 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
694 (void *) rid, IDENTIFIER_POINTER (rid));
697 c_binding_oracle = save;
700 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
701 which may be any of several kinds of DECL or TYPE or error_mark_node,
702 in the scope SCOPE. */
703 static void
704 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
705 bool nested, location_t locus)
707 struct c_binding *b, **here;
709 if (binding_freelist)
711 b = binding_freelist;
712 binding_freelist = b->prev;
714 else
715 b = ggc_alloc<c_binding> ();
717 b->shadowed = 0;
718 b->decl = decl;
719 b->id = name;
720 b->depth = scope->depth;
721 b->invisible = invisible;
722 b->nested = nested;
723 b->inner_comp = 0;
724 b->in_struct = 0;
725 b->locus = locus;
727 b->u.type = NULL;
729 b->prev = scope->bindings;
730 scope->bindings = b;
732 if (decl_jump_unsafe (decl))
733 scope->has_jump_unsafe_decl = 1;
735 if (!name)
736 return;
738 switch (TREE_CODE (decl))
740 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
741 case ENUMERAL_TYPE:
742 case UNION_TYPE:
743 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
744 case VAR_DECL:
745 case FUNCTION_DECL:
746 case TYPE_DECL:
747 case CONST_DECL:
748 case PARM_DECL:
749 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
751 default:
752 gcc_unreachable ();
755 /* Locate the appropriate place in the chain of shadowed decls
756 to insert this binding. Normally, scope == current_scope and
757 this does nothing. */
758 while (*here && (*here)->depth > scope->depth)
759 here = &(*here)->shadowed;
761 b->shadowed = *here;
762 *here = b;
765 /* Clear the binding structure B, stick it on the binding_freelist,
766 and return the former value of b->prev. This is used by pop_scope
767 and get_parm_info to iterate destructively over all the bindings
768 from a given scope. */
769 static struct c_binding *
770 free_binding_and_advance (struct c_binding *b)
772 struct c_binding *prev = b->prev;
774 memset (b, 0, sizeof (struct c_binding));
775 b->prev = binding_freelist;
776 binding_freelist = b;
778 return prev;
781 /* Bind a label. Like bind, but skip fields which aren't used for
782 labels, and add the LABEL_VARS value. */
783 static void
784 bind_label (tree name, tree label, struct c_scope *scope,
785 struct c_label_vars *label_vars)
787 struct c_binding *b;
789 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
790 UNKNOWN_LOCATION);
792 scope->has_label_bindings = true;
794 b = scope->bindings;
795 gcc_assert (b->decl == label);
796 label_vars->shadowed = b->u.label;
797 b->u.label = label_vars;
800 /* Hook called at end of compilation to assume 1 elt
801 for a file-scope tentative array defn that wasn't complete before. */
803 void
804 c_finish_incomplete_decl (tree decl)
806 if (VAR_P (decl))
808 tree type = TREE_TYPE (decl);
809 if (type != error_mark_node
810 && TREE_CODE (type) == ARRAY_TYPE
811 && !DECL_EXTERNAL (decl)
812 && TYPE_DOMAIN (type) == 0)
814 warning_at (DECL_SOURCE_LOCATION (decl),
815 0, "array %q+D assumed to have one element", decl);
817 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
819 relayout_decl (decl);
824 /* Record that inline function FUNC contains a reference (location
825 LOC) to static DECL (file-scope or function-local according to
826 TYPE). */
828 void
829 record_inline_static (location_t loc, tree func, tree decl,
830 enum c_inline_static_type type)
832 c_inline_static *csi = ggc_alloc<c_inline_static> ();
833 csi->location = loc;
834 csi->function = func;
835 csi->static_decl = decl;
836 csi->type = type;
837 csi->next = c_inline_statics;
838 c_inline_statics = csi;
841 /* Check for references to static declarations in inline functions at
842 the end of the translation unit and diagnose them if the functions
843 are still inline definitions. */
845 static void
846 check_inline_statics (void)
848 struct c_inline_static *csi;
849 for (csi = c_inline_statics; csi; csi = csi->next)
851 if (DECL_EXTERNAL (csi->function))
852 switch (csi->type)
854 case csi_internal:
855 pedwarn (csi->location, 0,
856 "%qD is static but used in inline function %qD "
857 "which is not static", csi->static_decl, csi->function);
858 break;
859 case csi_modifiable:
860 pedwarn (csi->location, 0,
861 "%q+D is static but declared in inline function %qD "
862 "which is not static", csi->static_decl, csi->function);
863 break;
864 default:
865 gcc_unreachable ();
868 c_inline_statics = NULL;
871 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
872 for the current state, otherwise set it to uninitialized. */
874 static void
875 set_spot_bindings (struct c_spot_bindings *p, bool defining)
877 if (defining)
879 p->scope = current_scope;
880 p->bindings_in_scope = current_scope->bindings;
882 else
884 p->scope = NULL;
885 p->bindings_in_scope = NULL;
887 p->stmt_exprs = 0;
888 p->left_stmt_expr = false;
891 /* Update spot bindings P as we pop out of SCOPE. Return true if we
892 should push decls for a label. */
894 static bool
895 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
897 if (p->scope != scope)
899 /* This label or goto is defined in some other scope, or it is a
900 label which is not yet defined. There is nothing to
901 update. */
902 return false;
905 /* Adjust the spot bindings to refer to the bindings already defined
906 in the enclosing scope. */
907 p->scope = scope->outer;
908 p->bindings_in_scope = p->scope->bindings;
910 return true;
913 /* The Objective-C front-end often needs to determine the current scope. */
915 void *
916 objc_get_current_scope (void)
918 return current_scope;
921 /* The following function is used only by Objective-C. It needs to live here
922 because it accesses the innards of c_scope. */
924 void
925 objc_mark_locals_volatile (void *enclosing_blk)
927 struct c_scope *scope;
928 struct c_binding *b;
930 for (scope = current_scope;
931 scope && scope != enclosing_blk;
932 scope = scope->outer)
934 for (b = scope->bindings; b; b = b->prev)
935 objc_volatilize_decl (b->decl);
937 /* Do not climb up past the current function. */
938 if (scope->function_body)
939 break;
943 /* Return true if we are in the global binding level. */
945 bool
946 global_bindings_p (void)
948 return current_scope == file_scope;
951 void
952 keep_next_level (void)
954 keep_next_level_flag = true;
957 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
959 void
960 set_float_const_decimal64 (void)
962 current_scope->float_const_decimal64 = true;
965 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
967 void
968 clear_float_const_decimal64 (void)
970 current_scope->float_const_decimal64 = false;
973 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
975 bool
976 float_const_decimal64_p (void)
978 return current_scope->float_const_decimal64;
981 /* Identify this scope as currently being filled with parameters. */
983 void
984 declare_parm_level (void)
986 current_scope->parm_flag = true;
989 void
990 push_scope (void)
992 if (next_is_function_body)
994 /* This is the transition from the parameters to the top level
995 of the function body. These are the same scope
996 (C99 6.2.1p4,6) so we do not push another scope structure.
997 next_is_function_body is set only by store_parm_decls, which
998 in turn is called when and only when we are about to
999 encounter the opening curly brace for the function body.
1001 The outermost block of a function always gets a BLOCK node,
1002 because the debugging output routines expect that each
1003 function has at least one BLOCK. */
1004 current_scope->parm_flag = false;
1005 current_scope->function_body = true;
1006 current_scope->keep = true;
1007 current_scope->outer_function = current_function_scope;
1008 current_function_scope = current_scope;
1010 keep_next_level_flag = false;
1011 next_is_function_body = false;
1013 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1014 if (current_scope->outer)
1015 current_scope->float_const_decimal64
1016 = current_scope->outer->float_const_decimal64;
1017 else
1018 current_scope->float_const_decimal64 = false;
1020 else
1022 struct c_scope *scope;
1023 if (scope_freelist)
1025 scope = scope_freelist;
1026 scope_freelist = scope->outer;
1028 else
1029 scope = ggc_cleared_alloc<c_scope> ();
1031 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1032 if (current_scope)
1033 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1034 else
1035 scope->float_const_decimal64 = false;
1037 scope->keep = keep_next_level_flag;
1038 scope->outer = current_scope;
1039 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1041 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1042 possible. */
1043 if (current_scope && scope->depth == 0)
1045 scope->depth--;
1046 sorry ("GCC supports only %u nested scopes", scope->depth);
1049 current_scope = scope;
1050 keep_next_level_flag = false;
1054 /* This is called when we are leaving SCOPE. For each label defined
1055 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1056 These are the decls whose initialization will be skipped by a goto
1057 later in the function. */
1059 static void
1060 update_label_decls (struct c_scope *scope)
1062 struct c_scope *s;
1064 s = scope;
1065 while (s != NULL)
1067 if (s->has_label_bindings)
1069 struct c_binding *b;
1071 for (b = s->bindings; b != NULL; b = b->prev)
1073 struct c_label_vars *label_vars;
1074 struct c_binding *b1;
1075 bool hjud;
1076 unsigned int ix;
1077 struct c_goto_bindings *g;
1079 if (TREE_CODE (b->decl) != LABEL_DECL)
1080 continue;
1081 label_vars = b->u.label;
1083 b1 = label_vars->label_bindings.bindings_in_scope;
1084 if (label_vars->label_bindings.scope == NULL)
1085 hjud = false;
1086 else
1087 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1088 if (update_spot_bindings (scope, &label_vars->label_bindings))
1090 /* This label is defined in this scope. */
1091 if (hjud)
1093 for (; b1 != NULL; b1 = b1->prev)
1095 /* A goto from later in the function to this
1096 label will never see the initialization
1097 of B1, if any. Save it to issue a
1098 warning if needed. */
1099 if (decl_jump_unsafe (b1->decl))
1100 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1105 /* Update the bindings of any goto statements associated
1106 with this label. */
1107 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1108 update_spot_bindings (scope, &g->goto_bindings);
1112 /* Don't search beyond the current function. */
1113 if (s == current_function_scope)
1114 break;
1116 s = s->outer;
1120 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1122 static void
1123 set_type_context (tree type, tree context)
1125 for (type = TYPE_MAIN_VARIANT (type); type;
1126 type = TYPE_NEXT_VARIANT (type))
1127 TYPE_CONTEXT (type) = context;
1130 /* Exit a scope. Restore the state of the identifier-decl mappings
1131 that were in effect when this scope was entered. Return a BLOCK
1132 node containing all the DECLs in this scope that are of interest
1133 to debug info generation. */
1135 tree
1136 pop_scope (void)
1138 struct c_scope *scope = current_scope;
1139 tree block, context, p;
1140 struct c_binding *b;
1142 bool functionbody = scope->function_body;
1143 bool keep = functionbody || scope->keep || scope->bindings;
1145 update_label_decls (scope);
1147 /* If appropriate, create a BLOCK to record the decls for the life
1148 of this function. */
1149 block = 0;
1150 if (keep)
1152 block = make_node (BLOCK);
1153 BLOCK_SUBBLOCKS (block) = scope->blocks;
1154 TREE_USED (block) = 1;
1156 /* In each subblock, record that this is its superior. */
1157 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1158 BLOCK_SUPERCONTEXT (p) = block;
1160 BLOCK_VARS (block) = 0;
1163 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1164 scope must be set so that they point to the appropriate
1165 construct, i.e. either to the current FUNCTION_DECL node, or
1166 else to the BLOCK node we just constructed.
1168 Note that for tagged types whose scope is just the formal
1169 parameter list for some function type specification, we can't
1170 properly set their TYPE_CONTEXTs here, because we don't have a
1171 pointer to the appropriate FUNCTION_TYPE node readily available
1172 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1173 type nodes get set in `grokdeclarator' as soon as we have created
1174 the FUNCTION_TYPE node which will represent the "scope" for these
1175 "parameter list local" tagged types. */
1176 if (scope->function_body)
1177 context = current_function_decl;
1178 else if (scope == file_scope)
1180 tree file_decl = build_translation_unit_decl (NULL_TREE);
1181 context = file_decl;
1182 debug_hooks->register_main_translation_unit (file_decl);
1184 else
1185 context = block;
1187 /* Clear all bindings in this scope. */
1188 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1190 p = b->decl;
1191 switch (TREE_CODE (p))
1193 case LABEL_DECL:
1194 /* Warnings for unused labels, errors for undefined labels. */
1195 if (TREE_USED (p) && !DECL_INITIAL (p))
1197 error ("label %q+D used but not defined", p);
1198 DECL_INITIAL (p) = error_mark_node;
1200 else
1201 warn_for_unused_label (p);
1203 /* Labels go in BLOCK_VARS. */
1204 DECL_CHAIN (p) = BLOCK_VARS (block);
1205 BLOCK_VARS (block) = p;
1206 gcc_assert (I_LABEL_BINDING (b->id) == b);
1207 I_LABEL_BINDING (b->id) = b->shadowed;
1209 /* Also pop back to the shadowed label_vars. */
1210 release_tree_vector (b->u.label->decls_in_scope);
1211 b->u.label = b->u.label->shadowed;
1212 break;
1214 case ENUMERAL_TYPE:
1215 case UNION_TYPE:
1216 case RECORD_TYPE:
1217 set_type_context (p, context);
1219 /* Types may not have tag-names, in which case the type
1220 appears in the bindings list with b->id NULL. */
1221 if (b->id)
1223 gcc_assert (I_TAG_BINDING (b->id) == b);
1224 I_TAG_BINDING (b->id) = b->shadowed;
1226 break;
1228 case FUNCTION_DECL:
1229 /* Propagate TREE_ADDRESSABLE from nested functions to their
1230 containing functions. */
1231 if (!TREE_ASM_WRITTEN (p)
1232 && DECL_INITIAL (p) != 0
1233 && TREE_ADDRESSABLE (p)
1234 && DECL_ABSTRACT_ORIGIN (p) != 0
1235 && DECL_ABSTRACT_ORIGIN (p) != p)
1236 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1237 if (!DECL_EXTERNAL (p)
1238 && !DECL_INITIAL (p)
1239 && scope != file_scope
1240 && scope != external_scope)
1242 error ("nested function %q+D declared but never defined", p);
1243 undef_nested_function = true;
1245 else if (DECL_DECLARED_INLINE_P (p)
1246 && TREE_PUBLIC (p)
1247 && !DECL_INITIAL (p))
1249 /* C99 6.7.4p6: "a function with external linkage... declared
1250 with an inline function specifier ... shall also be defined
1251 in the same translation unit." */
1252 if (!flag_gnu89_inline
1253 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1254 && scope != external_scope)
1255 pedwarn (input_location, 0,
1256 "inline function %q+D declared but never defined", p);
1257 DECL_EXTERNAL (p) = 1;
1260 goto common_symbol;
1262 case VAR_DECL:
1263 /* Warnings for unused variables. */
1264 if ((!TREE_USED (p) || !DECL_READ_P (p))
1265 && !TREE_NO_WARNING (p)
1266 && !DECL_IN_SYSTEM_HEADER (p)
1267 && DECL_NAME (p)
1268 && !DECL_ARTIFICIAL (p)
1269 && scope != file_scope
1270 && scope != external_scope)
1272 if (!TREE_USED (p))
1273 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1274 else if (DECL_CONTEXT (p) == current_function_decl)
1275 warning_at (DECL_SOURCE_LOCATION (p),
1276 OPT_Wunused_but_set_variable,
1277 "variable %qD set but not used", p);
1280 if (b->inner_comp)
1282 error ("type of array %q+D completed incompatibly with"
1283 " implicit initialization", p);
1286 /* Fall through. */
1287 case TYPE_DECL:
1288 case CONST_DECL:
1289 common_symbol:
1290 /* All of these go in BLOCK_VARS, but only if this is the
1291 binding in the home scope. */
1292 if (!b->nested)
1294 DECL_CHAIN (p) = BLOCK_VARS (block);
1295 BLOCK_VARS (block) = p;
1297 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1299 /* For block local externs add a special
1300 DECL_EXTERNAL decl for debug info generation. */
1301 tree extp = copy_node (p);
1303 DECL_EXTERNAL (extp) = 1;
1304 TREE_STATIC (extp) = 0;
1305 TREE_PUBLIC (extp) = 1;
1306 DECL_INITIAL (extp) = NULL_TREE;
1307 DECL_LANG_SPECIFIC (extp) = NULL;
1308 DECL_CONTEXT (extp) = current_function_decl;
1309 if (TREE_CODE (p) == FUNCTION_DECL)
1311 DECL_RESULT (extp) = NULL_TREE;
1312 DECL_SAVED_TREE (extp) = NULL_TREE;
1313 DECL_STRUCT_FUNCTION (extp) = NULL;
1315 if (b->locus != UNKNOWN_LOCATION)
1316 DECL_SOURCE_LOCATION (extp) = b->locus;
1317 DECL_CHAIN (extp) = BLOCK_VARS (block);
1318 BLOCK_VARS (block) = extp;
1320 /* If this is the file scope set DECL_CONTEXT of each decl to
1321 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1322 work. */
1323 if (scope == file_scope)
1325 DECL_CONTEXT (p) = context;
1326 if (TREE_CODE (p) == TYPE_DECL
1327 && TREE_TYPE (p) != error_mark_node)
1328 set_type_context (TREE_TYPE (p), context);
1331 /* Fall through. */
1332 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1333 already been put there by store_parm_decls. Unused-
1334 parameter warnings are handled by function.c.
1335 error_mark_node obviously does not go in BLOCK_VARS and
1336 does not get unused-variable warnings. */
1337 case PARM_DECL:
1338 case ERROR_MARK:
1339 /* It is possible for a decl not to have a name. We get
1340 here with b->id NULL in this case. */
1341 if (b->id)
1343 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1344 I_SYMBOL_BINDING (b->id) = b->shadowed;
1345 if (b->shadowed && b->shadowed->u.type)
1346 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1348 break;
1350 default:
1351 gcc_unreachable ();
1356 /* Dispose of the block that we just made inside some higher level. */
1357 if ((scope->function_body || scope == file_scope) && context)
1359 DECL_INITIAL (context) = block;
1360 BLOCK_SUPERCONTEXT (block) = context;
1362 else if (scope->outer)
1364 if (block)
1365 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1366 /* If we did not make a block for the scope just exited, any
1367 blocks made for inner scopes must be carried forward so they
1368 will later become subblocks of something else. */
1369 else if (scope->blocks)
1370 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1373 /* Pop the current scope, and free the structure for reuse. */
1374 current_scope = scope->outer;
1375 if (scope->function_body)
1376 current_function_scope = scope->outer_function;
1378 memset (scope, 0, sizeof (struct c_scope));
1379 scope->outer = scope_freelist;
1380 scope_freelist = scope;
1382 return block;
1385 void
1386 push_file_scope (void)
1388 tree decl;
1390 if (file_scope)
1391 return;
1393 push_scope ();
1394 file_scope = current_scope;
1396 start_fname_decls ();
1398 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1399 bind (DECL_NAME (decl), decl, file_scope,
1400 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1403 void
1404 pop_file_scope (void)
1406 /* In case there were missing closebraces, get us back to the global
1407 binding level. */
1408 while (current_scope != file_scope)
1409 pop_scope ();
1411 /* __FUNCTION__ is defined at file scope (""). This
1412 call may not be necessary as my tests indicate it
1413 still works without it. */
1414 finish_fname_decls ();
1416 check_inline_statics ();
1418 /* This is the point to write out a PCH if we're doing that.
1419 In that case we do not want to do anything else. */
1420 if (pch_file)
1422 c_common_write_pch ();
1423 return;
1426 /* Pop off the file scope and close this translation unit. */
1427 pop_scope ();
1428 file_scope = 0;
1430 maybe_apply_pending_pragma_weaks ();
1433 /* Adjust the bindings for the start of a statement expression. */
1435 void
1436 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1438 struct c_scope *scope;
1440 for (scope = current_scope; scope != NULL; scope = scope->outer)
1442 struct c_binding *b;
1444 if (!scope->has_label_bindings)
1445 continue;
1447 for (b = scope->bindings; b != NULL; b = b->prev)
1449 struct c_label_vars *label_vars;
1450 unsigned int ix;
1451 struct c_goto_bindings *g;
1453 if (TREE_CODE (b->decl) != LABEL_DECL)
1454 continue;
1455 label_vars = b->u.label;
1456 ++label_vars->label_bindings.stmt_exprs;
1457 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1458 ++g->goto_bindings.stmt_exprs;
1462 if (switch_bindings != NULL)
1463 ++switch_bindings->stmt_exprs;
1466 /* Adjust the bindings for the end of a statement expression. */
1468 void
1469 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1471 struct c_scope *scope;
1473 for (scope = current_scope; scope != NULL; scope = scope->outer)
1475 struct c_binding *b;
1477 if (!scope->has_label_bindings)
1478 continue;
1480 for (b = scope->bindings; b != NULL; b = b->prev)
1482 struct c_label_vars *label_vars;
1483 unsigned int ix;
1484 struct c_goto_bindings *g;
1486 if (TREE_CODE (b->decl) != LABEL_DECL)
1487 continue;
1488 label_vars = b->u.label;
1489 --label_vars->label_bindings.stmt_exprs;
1490 if (label_vars->label_bindings.stmt_exprs < 0)
1492 label_vars->label_bindings.left_stmt_expr = true;
1493 label_vars->label_bindings.stmt_exprs = 0;
1495 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1497 --g->goto_bindings.stmt_exprs;
1498 if (g->goto_bindings.stmt_exprs < 0)
1500 g->goto_bindings.left_stmt_expr = true;
1501 g->goto_bindings.stmt_exprs = 0;
1507 if (switch_bindings != NULL)
1509 --switch_bindings->stmt_exprs;
1510 gcc_assert (switch_bindings->stmt_exprs >= 0);
1514 /* Push a definition or a declaration of struct, union or enum tag "name".
1515 "type" should be the type node.
1516 We assume that the tag "name" is not already defined, and has a location
1517 of LOC.
1519 Note that the definition may really be just a forward reference.
1520 In that case, the TYPE_SIZE will be zero. */
1522 static void
1523 pushtag (location_t loc, tree name, tree type)
1525 /* Record the identifier as the type's name if it has none. */
1526 if (name && !TYPE_NAME (type))
1527 TYPE_NAME (type) = name;
1528 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1530 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1531 tagged type we just added to the current scope. This fake
1532 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1533 to output a representation of a tagged type, and it also gives
1534 us a convenient place to record the "scope start" address for the
1535 tagged type. */
1537 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1538 TYPE_DECL, NULL_TREE, type));
1540 /* An approximation for now, so we can tell this is a function-scope tag.
1541 This will be updated in pop_scope. */
1542 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1544 if (warn_cxx_compat && name != NULL_TREE)
1546 struct c_binding *b = I_SYMBOL_BINDING (name);
1548 if (b != NULL
1549 && b->decl != NULL_TREE
1550 && TREE_CODE (b->decl) == TYPE_DECL
1551 && (B_IN_CURRENT_SCOPE (b)
1552 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1553 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1554 != TYPE_MAIN_VARIANT (type)))
1556 warning_at (loc, OPT_Wc___compat,
1557 ("using %qD as both a typedef and a tag is "
1558 "invalid in C++"),
1559 b->decl);
1560 if (b->locus != UNKNOWN_LOCATION)
1561 inform (b->locus, "originally defined here");
1566 /* An exported interface to pushtag. This is used by the gdb plugin's
1567 binding oracle to introduce a new tag binding. */
1569 void
1570 c_pushtag (location_t loc, tree name, tree type)
1572 pushtag (loc, name, type);
1575 /* An exported interface to bind a declaration. LOC is the location
1576 to use. DECL is the declaration to bind. The decl's name is used
1577 to determine how it is bound. If DECL is a VAR_DECL, then
1578 IS_GLOBAL determines whether the decl is put into the global (file
1579 and external) scope or the current function's scope; if DECL is not
1580 a VAR_DECL then it is always put into the file scope. */
1582 void
1583 c_bind (location_t loc, tree decl, bool is_global)
1585 struct c_scope *scope;
1586 bool nested = false;
1588 if (!VAR_P (decl) || current_function_scope == NULL)
1590 /* Types and functions are always considered to be global. */
1591 scope = file_scope;
1592 DECL_EXTERNAL (decl) = 1;
1593 TREE_PUBLIC (decl) = 1;
1595 else if (is_global)
1597 /* Also bind it into the external scope. */
1598 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1599 nested = true;
1600 scope = file_scope;
1601 DECL_EXTERNAL (decl) = 1;
1602 TREE_PUBLIC (decl) = 1;
1604 else
1606 DECL_CONTEXT (decl) = current_function_decl;
1607 TREE_PUBLIC (decl) = 0;
1608 scope = current_function_scope;
1611 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1614 /* Subroutine of compare_decls. Allow harmless mismatches in return
1615 and argument types provided that the type modes match. This function
1616 return a unified type given a suitable match, and 0 otherwise. */
1618 static tree
1619 match_builtin_function_types (tree newtype, tree oldtype)
1621 tree newrettype, oldrettype;
1622 tree newargs, oldargs;
1623 tree trytype, tryargs;
1625 /* Accept the return type of the new declaration if same modes. */
1626 oldrettype = TREE_TYPE (oldtype);
1627 newrettype = TREE_TYPE (newtype);
1629 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1630 return 0;
1632 oldargs = TYPE_ARG_TYPES (oldtype);
1633 newargs = TYPE_ARG_TYPES (newtype);
1634 tryargs = newargs;
1636 while (oldargs || newargs)
1638 if (!oldargs
1639 || !newargs
1640 || !TREE_VALUE (oldargs)
1641 || !TREE_VALUE (newargs)
1642 || TYPE_MODE (TREE_VALUE (oldargs))
1643 != TYPE_MODE (TREE_VALUE (newargs)))
1644 return 0;
1646 oldargs = TREE_CHAIN (oldargs);
1647 newargs = TREE_CHAIN (newargs);
1650 trytype = build_function_type (newrettype, tryargs);
1652 /* Allow declaration to change transaction_safe attribute. */
1653 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1654 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1655 tree newattrs = TYPE_ATTRIBUTES (newtype);
1656 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1657 if (oldtsafe && !newtsafe)
1658 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1659 else if (newtsafe && !oldtsafe)
1660 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1661 NULL_TREE, oldattrs);
1663 return build_type_attribute_variant (trytype, oldattrs);
1666 /* Subroutine of diagnose_mismatched_decls. Check for function type
1667 mismatch involving an empty arglist vs a nonempty one and give clearer
1668 diagnostics. */
1669 static void
1670 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1671 tree newtype, tree oldtype)
1673 tree t;
1675 if (TREE_CODE (olddecl) != FUNCTION_DECL
1676 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1677 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1678 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1679 return;
1681 t = TYPE_ARG_TYPES (oldtype);
1682 if (t == 0)
1683 t = TYPE_ARG_TYPES (newtype);
1684 for (; t; t = TREE_CHAIN (t))
1686 tree type = TREE_VALUE (t);
1688 if (TREE_CHAIN (t) == 0
1689 && TYPE_MAIN_VARIANT (type) != void_type_node)
1691 inform (input_location, "a parameter list with an ellipsis can%'t match "
1692 "an empty parameter name list declaration");
1693 break;
1696 if (c_type_promotes_to (type) != type)
1698 inform (input_location, "an argument type that has a default promotion can%'t match "
1699 "an empty parameter name list declaration");
1700 break;
1705 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1706 old-style function definition, NEWDECL is a prototype declaration.
1707 Diagnose inconsistencies in the argument list. Returns TRUE if
1708 the prototype is compatible, FALSE if not. */
1709 static bool
1710 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1712 tree newargs, oldargs;
1713 int i;
1715 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1717 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1718 newargs = TYPE_ARG_TYPES (newtype);
1719 i = 1;
1721 for (;;)
1723 tree oldargtype = TREE_VALUE (oldargs);
1724 tree newargtype = TREE_VALUE (newargs);
1726 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1727 return false;
1729 oldargtype = (TYPE_ATOMIC (oldargtype)
1730 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1731 TYPE_QUAL_ATOMIC)
1732 : TYPE_MAIN_VARIANT (oldargtype));
1733 newargtype = (TYPE_ATOMIC (newargtype)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1735 TYPE_QUAL_ATOMIC)
1736 : TYPE_MAIN_VARIANT (newargtype));
1738 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1739 break;
1741 /* Reaching the end of just one list means the two decls don't
1742 agree on the number of arguments. */
1743 if (END_OF_ARGLIST (oldargtype))
1745 error ("prototype for %q+D declares more arguments "
1746 "than previous old-style definition", newdecl);
1747 return false;
1749 else if (END_OF_ARGLIST (newargtype))
1751 error ("prototype for %q+D declares fewer arguments "
1752 "than previous old-style definition", newdecl);
1753 return false;
1756 /* Type for passing arg must be consistent with that declared
1757 for the arg. */
1758 else if (!comptypes (oldargtype, newargtype))
1760 error ("prototype for %q+D declares argument %d"
1761 " with incompatible type",
1762 newdecl, i);
1763 return false;
1766 oldargs = TREE_CHAIN (oldargs);
1767 newargs = TREE_CHAIN (newargs);
1768 i++;
1771 /* If we get here, no errors were found, but do issue a warning
1772 for this poor-style construct. */
1773 warning (0, "prototype for %q+D follows non-prototype definition",
1774 newdecl);
1775 return true;
1776 #undef END_OF_ARGLIST
1779 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1780 first in a pair of mismatched declarations, using the diagnostic
1781 function DIAG. */
1782 static void
1783 locate_old_decl (tree decl)
1785 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1786 && !C_DECL_DECLARED_BUILTIN (decl))
1788 else if (DECL_INITIAL (decl))
1789 inform (input_location, "previous definition of %q+D was here", decl);
1790 else if (C_DECL_IMPLICIT (decl))
1791 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1792 else
1793 inform (input_location, "previous declaration of %q+D was here", decl);
1796 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1797 Returns true if the caller should proceed to merge the two, false
1798 if OLDDECL should simply be discarded. As a side effect, issues
1799 all necessary diagnostics for invalid or poor-style combinations.
1800 If it returns true, writes the types of NEWDECL and OLDDECL to
1801 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1802 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1804 static bool
1805 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1806 tree *newtypep, tree *oldtypep)
1808 tree newtype, oldtype;
1809 bool pedwarned = false;
1810 bool warned = false;
1811 bool retval = true;
1813 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1814 && DECL_EXTERNAL (DECL))
1816 /* If we have error_mark_node for either decl or type, just discard
1817 the previous decl - we're in an error cascade already. */
1818 if (olddecl == error_mark_node || newdecl == error_mark_node)
1819 return false;
1820 *oldtypep = oldtype = TREE_TYPE (olddecl);
1821 *newtypep = newtype = TREE_TYPE (newdecl);
1822 if (oldtype == error_mark_node || newtype == error_mark_node)
1823 return false;
1825 /* Two different categories of symbol altogether. This is an error
1826 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1827 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1829 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1830 && DECL_BUILT_IN (olddecl)
1831 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1833 error ("%q+D redeclared as different kind of symbol", newdecl);
1834 locate_old_decl (olddecl);
1836 else if (TREE_PUBLIC (newdecl))
1837 warning (0, "built-in function %q+D declared as non-function",
1838 newdecl);
1839 else
1840 warning (OPT_Wshadow, "declaration of %q+D shadows "
1841 "a built-in function", newdecl);
1842 return false;
1845 /* Enumerators have no linkage, so may only be declared once in a
1846 given scope. */
1847 if (TREE_CODE (olddecl) == CONST_DECL)
1849 error ("redeclaration of enumerator %q+D", newdecl);
1850 locate_old_decl (olddecl);
1851 return false;
1854 if (!comptypes (oldtype, newtype))
1856 if (TREE_CODE (olddecl) == FUNCTION_DECL
1857 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1859 /* Accept harmless mismatch in function types.
1860 This is for the ffs and fprintf builtins. */
1861 tree trytype = match_builtin_function_types (newtype, oldtype);
1863 if (trytype && comptypes (newtype, trytype))
1864 *oldtypep = oldtype = trytype;
1865 else
1867 /* If types don't match for a built-in, throw away the
1868 built-in. No point in calling locate_old_decl here, it
1869 won't print anything. */
1870 warning (0, "conflicting types for built-in function %q+D",
1871 newdecl);
1872 return false;
1875 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1876 && DECL_IS_BUILTIN (olddecl))
1878 /* A conflicting function declaration for a predeclared
1879 function that isn't actually built in. Objective C uses
1880 these. The new declaration silently overrides everything
1881 but the volatility (i.e. noreturn) indication. See also
1882 below. FIXME: Make Objective C use normal builtins. */
1883 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1884 return false;
1886 /* Permit void foo (...) to match int foo (...) if the latter is
1887 the definition and implicit int was used. See
1888 c-torture/compile/920625-2.c. */
1889 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1890 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1891 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1892 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1894 pedwarned = pedwarn (input_location, 0,
1895 "conflicting types for %q+D", newdecl);
1896 /* Make sure we keep void as the return type. */
1897 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1898 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1900 /* Permit void foo (...) to match an earlier call to foo (...) with
1901 no declared type (thus, implicitly int). */
1902 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1903 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1904 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1905 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1907 pedwarned = pedwarn (input_location, 0,
1908 "conflicting types for %q+D", newdecl);
1909 /* Make sure we keep void as the return type. */
1910 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1912 else
1914 int new_quals = TYPE_QUALS (newtype);
1915 int old_quals = TYPE_QUALS (oldtype);
1917 if (new_quals != old_quals)
1919 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1920 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1921 if (new_addr != old_addr)
1923 if (ADDR_SPACE_GENERIC_P (new_addr))
1924 error ("conflicting named address spaces (generic vs %s) "
1925 "for %q+D",
1926 c_addr_space_name (old_addr), newdecl);
1927 else if (ADDR_SPACE_GENERIC_P (old_addr))
1928 error ("conflicting named address spaces (%s vs generic) "
1929 "for %q+D",
1930 c_addr_space_name (new_addr), newdecl);
1931 else
1932 error ("conflicting named address spaces (%s vs %s) "
1933 "for %q+D",
1934 c_addr_space_name (new_addr),
1935 c_addr_space_name (old_addr),
1936 newdecl);
1939 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1940 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1941 error ("conflicting type qualifiers for %q+D", newdecl);
1943 else
1944 error ("conflicting types for %q+D", newdecl);
1945 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1946 locate_old_decl (olddecl);
1947 return false;
1951 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1952 but silently ignore the redeclaration if either is in a system
1953 header. (Conflicting redeclarations were handled above.) This
1954 is allowed for C11 if the types are the same, not just
1955 compatible. */
1956 if (TREE_CODE (newdecl) == TYPE_DECL)
1958 bool types_different = false;
1959 int comptypes_result;
1961 comptypes_result
1962 = comptypes_check_different_types (oldtype, newtype, &types_different);
1964 if (comptypes_result != 1 || types_different)
1966 error ("redefinition of typedef %q+D with different type", newdecl);
1967 locate_old_decl (olddecl);
1968 return false;
1971 if (DECL_IN_SYSTEM_HEADER (newdecl)
1972 || DECL_IN_SYSTEM_HEADER (olddecl)
1973 || TREE_NO_WARNING (newdecl)
1974 || TREE_NO_WARNING (olddecl))
1975 return true; /* Allow OLDDECL to continue in use. */
1977 if (variably_modified_type_p (newtype, NULL))
1979 error ("redefinition of typedef %q+D with variably modified type",
1980 newdecl);
1981 locate_old_decl (olddecl);
1983 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1984 "redefinition of typedef %q+D", newdecl))
1985 locate_old_decl (olddecl);
1987 return true;
1990 /* Function declarations can either be 'static' or 'extern' (no
1991 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1992 can never conflict with each other on account of linkage
1993 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1994 gnu89 mode permits two definitions if one is 'extern inline' and
1995 one is not. The non- extern-inline definition supersedes the
1996 extern-inline definition. */
1998 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2000 /* If you declare a built-in function name as static, or
2001 define the built-in with an old-style definition (so we
2002 can't validate the argument list) the built-in definition is
2003 overridden, but optionally warn this was a bad choice of name. */
2004 if (DECL_BUILT_IN (olddecl)
2005 && !C_DECL_DECLARED_BUILTIN (olddecl)
2006 && (!TREE_PUBLIC (newdecl)
2007 || (DECL_INITIAL (newdecl)
2008 && !prototype_p (TREE_TYPE (newdecl)))))
2010 warning (OPT_Wshadow, "declaration of %q+D shadows "
2011 "a built-in function", newdecl);
2012 /* Discard the old built-in function. */
2013 return false;
2016 if (DECL_INITIAL (newdecl))
2018 if (DECL_INITIAL (olddecl))
2020 /* If both decls are in the same TU and the new declaration
2021 isn't overriding an extern inline reject the new decl.
2022 In c99, no overriding is allowed in the same translation
2023 unit. */
2024 if ((!DECL_EXTERN_INLINE (olddecl)
2025 || DECL_EXTERN_INLINE (newdecl)
2026 || (!flag_gnu89_inline
2027 && (!DECL_DECLARED_INLINE_P (olddecl)
2028 || !lookup_attribute ("gnu_inline",
2029 DECL_ATTRIBUTES (olddecl)))
2030 && (!DECL_DECLARED_INLINE_P (newdecl)
2031 || !lookup_attribute ("gnu_inline",
2032 DECL_ATTRIBUTES (newdecl))))
2034 && same_translation_unit_p (newdecl, olddecl))
2036 error ("redefinition of %q+D", newdecl);
2037 locate_old_decl (olddecl);
2038 return false;
2042 /* If we have a prototype after an old-style function definition,
2043 the argument types must be checked specially. */
2044 else if (DECL_INITIAL (olddecl)
2045 && !prototype_p (oldtype) && prototype_p (newtype)
2046 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2047 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2049 locate_old_decl (olddecl);
2050 return false;
2052 /* A non-static declaration (even an "extern") followed by a
2053 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2054 The same is true for a static forward declaration at block
2055 scope followed by a non-static declaration/definition at file
2056 scope. Static followed by non-static at the same scope is
2057 not undefined behavior, and is the most convenient way to get
2058 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2059 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2060 we do diagnose it if -Wtraditional. */
2061 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2063 /* Two exceptions to the rule. If olddecl is an extern
2064 inline, or a predeclared function that isn't actually
2065 built in, newdecl silently overrides olddecl. The latter
2066 occur only in Objective C; see also above. (FIXME: Make
2067 Objective C use normal builtins.) */
2068 if (!DECL_IS_BUILTIN (olddecl)
2069 && !DECL_EXTERN_INLINE (olddecl))
2071 error ("static declaration of %q+D follows "
2072 "non-static declaration", newdecl);
2073 locate_old_decl (olddecl);
2075 return false;
2077 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2079 if (DECL_CONTEXT (olddecl))
2081 error ("non-static declaration of %q+D follows "
2082 "static declaration", newdecl);
2083 locate_old_decl (olddecl);
2084 return false;
2086 else if (warn_traditional)
2088 warned |= warning (OPT_Wtraditional,
2089 "non-static declaration of %q+D "
2090 "follows static declaration", newdecl);
2094 /* Make sure gnu_inline attribute is either not present, or
2095 present on all inline decls. */
2096 if (DECL_DECLARED_INLINE_P (olddecl)
2097 && DECL_DECLARED_INLINE_P (newdecl))
2099 bool newa = lookup_attribute ("gnu_inline",
2100 DECL_ATTRIBUTES (newdecl)) != NULL;
2101 bool olda = lookup_attribute ("gnu_inline",
2102 DECL_ATTRIBUTES (olddecl)) != NULL;
2103 if (newa != olda)
2105 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2106 newa ? newdecl : olddecl);
2107 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2108 "but not here");
2112 else if (VAR_P (newdecl))
2114 /* Only variables can be thread-local, and all declarations must
2115 agree on this property. */
2116 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2118 /* Nothing to check. Since OLDDECL is marked threadprivate
2119 and NEWDECL does not have a thread-local attribute, we
2120 will merge the threadprivate attribute into NEWDECL. */
2123 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2125 if (DECL_THREAD_LOCAL_P (newdecl))
2126 error ("thread-local declaration of %q+D follows "
2127 "non-thread-local declaration", newdecl);
2128 else
2129 error ("non-thread-local declaration of %q+D follows "
2130 "thread-local declaration", newdecl);
2132 locate_old_decl (olddecl);
2133 return false;
2136 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2137 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2139 error ("redefinition of %q+D", newdecl);
2140 locate_old_decl (olddecl);
2141 return false;
2144 /* Objects declared at file scope: if the first declaration had
2145 external linkage (even if it was an external reference) the
2146 second must have external linkage as well, or the behavior is
2147 undefined. If the first declaration had internal linkage, then
2148 the second must too, or else be an external reference (in which
2149 case the composite declaration still has internal linkage).
2150 As for function declarations, we warn about the static-then-
2151 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2152 if (DECL_FILE_SCOPE_P (newdecl)
2153 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2155 if (DECL_EXTERNAL (newdecl))
2157 if (!DECL_FILE_SCOPE_P (olddecl))
2159 error ("extern declaration of %q+D follows "
2160 "declaration with no linkage", newdecl);
2161 locate_old_decl (olddecl);
2162 return false;
2164 else if (warn_traditional)
2166 warned |= warning (OPT_Wtraditional,
2167 "non-static declaration of %q+D "
2168 "follows static declaration", newdecl);
2171 else
2173 if (TREE_PUBLIC (newdecl))
2174 error ("non-static declaration of %q+D follows "
2175 "static declaration", newdecl);
2176 else
2177 error ("static declaration of %q+D follows "
2178 "non-static declaration", newdecl);
2180 locate_old_decl (olddecl);
2181 return false;
2184 /* Two objects with the same name declared at the same block
2185 scope must both be external references (6.7p3). */
2186 else if (!DECL_FILE_SCOPE_P (newdecl))
2188 if (DECL_EXTERNAL (newdecl))
2190 /* Extern with initializer at block scope, which will
2191 already have received an error. */
2193 else if (DECL_EXTERNAL (olddecl))
2195 error ("declaration of %q+D with no linkage follows "
2196 "extern declaration", newdecl);
2197 locate_old_decl (olddecl);
2199 else
2201 error ("redeclaration of %q+D with no linkage", newdecl);
2202 locate_old_decl (olddecl);
2205 return false;
2208 /* C++ does not permit a decl to appear multiple times at file
2209 scope. */
2210 if (warn_cxx_compat
2211 && DECL_FILE_SCOPE_P (newdecl)
2212 && !DECL_EXTERNAL (newdecl)
2213 && !DECL_EXTERNAL (olddecl))
2214 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2215 OPT_Wc___compat,
2216 ("duplicate declaration of %qD is "
2217 "invalid in C++"),
2218 newdecl);
2221 /* warnings */
2222 /* All decls must agree on a visibility. */
2223 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2224 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2225 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2227 warned |= warning (0, "redeclaration of %q+D with different visibility "
2228 "(old visibility preserved)", newdecl);
2231 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2232 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2233 else /* PARM_DECL, VAR_DECL */
2235 /* Redeclaration of a parameter is a constraint violation (this is
2236 not explicitly stated, but follows from C99 6.7p3 [no more than
2237 one declaration of the same identifier with no linkage in the
2238 same scope, except type tags] and 6.2.2p6 [parameters have no
2239 linkage]). We must check for a forward parameter declaration,
2240 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2241 an extension, the mandatory diagnostic for which is handled by
2242 mark_forward_parm_decls. */
2244 if (TREE_CODE (newdecl) == PARM_DECL
2245 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2247 error ("redefinition of parameter %q+D", newdecl);
2248 locate_old_decl (olddecl);
2249 return false;
2253 /* Optional warning for completely redundant decls. */
2254 if (!warned && !pedwarned
2255 && warn_redundant_decls
2256 /* Don't warn about a function declaration followed by a
2257 definition. */
2258 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2259 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2260 /* Don't warn about redundant redeclarations of builtins. */
2261 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2262 && !DECL_BUILT_IN (newdecl)
2263 && DECL_BUILT_IN (olddecl)
2264 && !C_DECL_DECLARED_BUILTIN (olddecl))
2265 /* Don't warn about an extern followed by a definition. */
2266 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2267 /* Don't warn about forward parameter decls. */
2268 && !(TREE_CODE (newdecl) == PARM_DECL
2269 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2270 /* Don't warn about a variable definition following a declaration. */
2271 && !(VAR_P (newdecl)
2272 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2274 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2275 newdecl);
2278 /* Report location of previous decl/defn. */
2279 if (warned || pedwarned)
2280 locate_old_decl (olddecl);
2282 #undef DECL_EXTERN_INLINE
2284 return retval;
2287 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2288 consistent with OLDDECL, but carries new information. Merge the
2289 new information into OLDDECL. This function issues no
2290 diagnostics. */
2292 static void
2293 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2295 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2296 && DECL_INITIAL (newdecl) != 0);
2297 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2298 && prototype_p (TREE_TYPE (newdecl)));
2299 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2300 && prototype_p (TREE_TYPE (olddecl)));
2302 /* For real parm decl following a forward decl, rechain the old decl
2303 in its new location and clear TREE_ASM_WRITTEN (it's not a
2304 forward decl anymore). */
2305 if (TREE_CODE (newdecl) == PARM_DECL
2306 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2308 struct c_binding *b, **here;
2310 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2311 if ((*here)->decl == olddecl)
2312 goto found;
2313 gcc_unreachable ();
2315 found:
2316 b = *here;
2317 *here = b->prev;
2318 b->prev = current_scope->bindings;
2319 current_scope->bindings = b;
2321 TREE_ASM_WRITTEN (olddecl) = 0;
2324 DECL_ATTRIBUTES (newdecl)
2325 = targetm.merge_decl_attributes (olddecl, newdecl);
2327 /* For typedefs use the old type, as the new type's DECL_NAME points
2328 at newdecl, which will be ggc_freed. */
2329 if (TREE_CODE (newdecl) == TYPE_DECL)
2331 /* But NEWTYPE might have an attribute, honor that. */
2332 tree tem = newtype;
2333 newtype = oldtype;
2335 if (TYPE_USER_ALIGN (tem))
2337 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2338 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2339 TYPE_USER_ALIGN (newtype) = true;
2342 /* And remove the new type from the variants list. */
2343 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2345 tree remove = TREE_TYPE (newdecl);
2346 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2347 t = TYPE_NEXT_VARIANT (t))
2348 if (TYPE_NEXT_VARIANT (t) == remove)
2350 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2351 break;
2356 /* Merge the data types specified in the two decls. */
2357 TREE_TYPE (newdecl)
2358 = TREE_TYPE (olddecl)
2359 = composite_type (newtype, oldtype);
2361 /* Lay the type out, unless already done. */
2362 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2364 if (TREE_TYPE (newdecl) != error_mark_node)
2365 layout_type (TREE_TYPE (newdecl));
2366 if (TREE_CODE (newdecl) != FUNCTION_DECL
2367 && TREE_CODE (newdecl) != TYPE_DECL
2368 && TREE_CODE (newdecl) != CONST_DECL)
2369 layout_decl (newdecl, 0);
2371 else
2373 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2374 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2375 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2376 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2377 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2379 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2380 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2384 /* Keep the old rtl since we can safely use it. */
2385 if (HAS_RTL_P (olddecl))
2386 COPY_DECL_RTL (olddecl, newdecl);
2388 /* Merge the type qualifiers. */
2389 if (TREE_READONLY (newdecl))
2390 TREE_READONLY (olddecl) = 1;
2392 if (TREE_THIS_VOLATILE (newdecl))
2393 TREE_THIS_VOLATILE (olddecl) = 1;
2395 /* Merge deprecatedness. */
2396 if (TREE_DEPRECATED (newdecl))
2397 TREE_DEPRECATED (olddecl) = 1;
2399 /* If a decl is in a system header and the other isn't, keep the one on the
2400 system header. Otherwise, keep source location of definition rather than
2401 declaration and of prototype rather than non-prototype unless that
2402 prototype is built-in. */
2403 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2404 && DECL_IN_SYSTEM_HEADER (olddecl)
2405 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2406 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2407 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2408 && DECL_IN_SYSTEM_HEADER (newdecl)
2409 && !DECL_IN_SYSTEM_HEADER (olddecl))
2410 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2411 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2412 || (old_is_prototype && !new_is_prototype
2413 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2414 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2416 /* Merge the initialization information. */
2417 if (DECL_INITIAL (newdecl) == 0)
2418 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2420 /* Merge the threadprivate attribute. */
2421 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2422 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2424 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2426 /* Copy the assembler name.
2427 Currently, it can only be defined in the prototype. */
2428 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2430 /* Use visibility of whichever declaration had it specified */
2431 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2433 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2434 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2437 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2439 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2440 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2441 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2442 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2443 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2444 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2445 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2446 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2447 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2448 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2449 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2452 /* Merge the storage class information. */
2453 merge_weak (newdecl, olddecl);
2455 /* For functions, static overrides non-static. */
2456 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2458 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2459 /* This is since we don't automatically
2460 copy the attributes of NEWDECL into OLDDECL. */
2461 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2462 /* If this clears `static', clear it in the identifier too. */
2463 if (!TREE_PUBLIC (olddecl))
2464 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2468 /* In c99, 'extern' declaration before (or after) 'inline' means this
2469 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2470 is present. */
2471 if (TREE_CODE (newdecl) == FUNCTION_DECL
2472 && !flag_gnu89_inline
2473 && (DECL_DECLARED_INLINE_P (newdecl)
2474 || DECL_DECLARED_INLINE_P (olddecl))
2475 && (!DECL_DECLARED_INLINE_P (newdecl)
2476 || !DECL_DECLARED_INLINE_P (olddecl)
2477 || !DECL_EXTERNAL (olddecl))
2478 && DECL_EXTERNAL (newdecl)
2479 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2480 && !current_function_decl)
2481 DECL_EXTERNAL (newdecl) = 0;
2483 /* An inline definition following a static declaration is not
2484 DECL_EXTERNAL. */
2485 if (new_is_definition
2486 && (DECL_DECLARED_INLINE_P (newdecl)
2487 || DECL_DECLARED_INLINE_P (olddecl))
2488 && !TREE_PUBLIC (olddecl))
2489 DECL_EXTERNAL (newdecl) = 0;
2491 if (DECL_EXTERNAL (newdecl))
2493 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2494 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2496 /* An extern decl does not override previous storage class. */
2497 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2498 if (!DECL_EXTERNAL (newdecl))
2500 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2501 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2504 else
2506 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2507 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2510 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2512 /* If we're redefining a function previously defined as extern
2513 inline, make sure we emit debug info for the inline before we
2514 throw it away, in case it was inlined into a function that
2515 hasn't been written out yet. */
2516 if (new_is_definition && DECL_INITIAL (olddecl))
2517 /* The new defn must not be inline. */
2518 DECL_UNINLINABLE (newdecl) = 1;
2519 else
2521 /* If either decl says `inline', this fn is inline, unless
2522 its definition was passed already. */
2523 if (DECL_DECLARED_INLINE_P (newdecl)
2524 || DECL_DECLARED_INLINE_P (olddecl))
2525 DECL_DECLARED_INLINE_P (newdecl) = 1;
2527 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2528 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2530 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2531 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2532 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2533 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2536 if (DECL_BUILT_IN (olddecl))
2538 /* If redeclaring a builtin function, it stays built in.
2539 But it gets tagged as having been declared. */
2540 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2541 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2542 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2543 if (new_is_prototype)
2545 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2546 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2548 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2549 switch (fncode)
2551 /* If a compatible prototype of these builtin functions
2552 is seen, assume the runtime implements it with the
2553 expected semantics. */
2554 case BUILT_IN_STPCPY:
2555 if (builtin_decl_explicit_p (fncode))
2556 set_builtin_decl_implicit_p (fncode, true);
2557 break;
2558 default:
2559 if (builtin_decl_explicit_p (fncode))
2560 set_builtin_decl_declared_p (fncode, true);
2561 break;
2565 else
2566 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2567 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2570 /* Preserve function specific target and optimization options */
2571 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2572 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2573 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2574 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2576 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2577 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2578 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2579 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2581 /* Also preserve various other info from the definition. */
2582 if (!new_is_definition)
2584 tree t;
2585 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2586 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2587 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2588 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2589 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2590 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2591 DECL_CONTEXT (t) = newdecl;
2593 /* See if we've got a function to instantiate from. */
2594 if (DECL_SAVED_TREE (olddecl))
2595 DECL_ABSTRACT_ORIGIN (newdecl)
2596 = DECL_ABSTRACT_ORIGIN (olddecl);
2600 /* Merge the USED information. */
2601 if (TREE_USED (olddecl))
2602 TREE_USED (newdecl) = 1;
2603 else if (TREE_USED (newdecl))
2604 TREE_USED (olddecl) = 1;
2605 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2606 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2607 if (DECL_PRESERVE_P (olddecl))
2608 DECL_PRESERVE_P (newdecl) = 1;
2609 else if (DECL_PRESERVE_P (newdecl))
2610 DECL_PRESERVE_P (olddecl) = 1;
2612 /* Merge DECL_COMMON */
2613 if (VAR_P (olddecl) && VAR_P (newdecl)
2614 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2615 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2616 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2618 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2619 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2620 DECL_ARGUMENTS (if appropriate). */
2622 unsigned olddecl_uid = DECL_UID (olddecl);
2623 tree olddecl_context = DECL_CONTEXT (olddecl);
2624 tree olddecl_arguments = NULL;
2625 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2626 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2628 memcpy ((char *) olddecl + sizeof (struct tree_common),
2629 (char *) newdecl + sizeof (struct tree_common),
2630 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2631 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2632 switch (TREE_CODE (olddecl))
2634 case FUNCTION_DECL:
2635 case VAR_DECL:
2637 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2639 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2640 (char *) newdecl + sizeof (struct tree_decl_common),
2641 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2642 olddecl->decl_with_vis.symtab_node = snode;
2644 if ((DECL_EXTERNAL (olddecl)
2645 || TREE_PUBLIC (olddecl)
2646 || TREE_STATIC (olddecl))
2647 && DECL_SECTION_NAME (newdecl) != NULL)
2648 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2650 /* This isn't quite correct for something like
2651 int __thread x attribute ((tls_model ("local-exec")));
2652 extern int __thread x;
2653 as we'll lose the "local-exec" model. */
2654 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2655 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2656 break;
2659 case FIELD_DECL:
2660 case PARM_DECL:
2661 case LABEL_DECL:
2662 case RESULT_DECL:
2663 case CONST_DECL:
2664 case TYPE_DECL:
2665 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2666 (char *) newdecl + sizeof (struct tree_decl_common),
2667 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2668 break;
2670 default:
2672 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2673 (char *) newdecl + sizeof (struct tree_decl_common),
2674 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2676 DECL_UID (olddecl) = olddecl_uid;
2677 DECL_CONTEXT (olddecl) = olddecl_context;
2678 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2679 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2682 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2683 so that encode_section_info has a chance to look at the new decl
2684 flags and attributes. */
2685 if (DECL_RTL_SET_P (olddecl)
2686 && (TREE_CODE (olddecl) == FUNCTION_DECL
2687 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2688 make_decl_rtl (olddecl);
2691 /* Handle when a new declaration NEWDECL has the same name as an old
2692 one OLDDECL in the same binding contour. Prints an error message
2693 if appropriate.
2695 If safely possible, alter OLDDECL to look like NEWDECL, and return
2696 true. Otherwise, return false. */
2698 static bool
2699 duplicate_decls (tree newdecl, tree olddecl)
2701 tree newtype = NULL, oldtype = NULL;
2703 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2705 /* Avoid `unused variable' and other warnings for OLDDECL. */
2706 TREE_NO_WARNING (olddecl) = 1;
2707 return false;
2710 merge_decls (newdecl, olddecl, newtype, oldtype);
2712 /* The NEWDECL will no longer be needed.
2714 Before releasing the node, be sure to remove function from symbol
2715 table that might have been inserted there to record comdat group.
2716 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2717 structure is shared in between NEWDECL and OLDECL. */
2718 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2719 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2720 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2722 struct symtab_node *snode = symtab_node::get (newdecl);
2723 if (snode)
2724 snode->remove ();
2726 ggc_free (newdecl);
2727 return true;
2731 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2732 static void
2733 warn_if_shadowing (tree new_decl)
2735 struct c_binding *b;
2737 /* Shadow warnings wanted? */
2738 if (!warn_shadow
2739 /* No shadow warnings for internally generated vars. */
2740 || DECL_IS_BUILTIN (new_decl)
2741 /* No shadow warnings for vars made for inlining. */
2742 || DECL_FROM_INLINE (new_decl))
2743 return;
2745 /* Is anything being shadowed? Invisible decls do not count. */
2746 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2747 if (b->decl && b->decl != new_decl && !b->invisible
2748 && (b->decl == error_mark_node
2749 || diagnostic_report_warnings_p (global_dc,
2750 DECL_SOURCE_LOCATION (b->decl))))
2752 tree old_decl = b->decl;
2753 bool warned = false;
2755 if (old_decl == error_mark_node)
2757 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2758 "non-variable", new_decl);
2759 break;
2761 else if (TREE_CODE (old_decl) == PARM_DECL)
2762 warned = warning (OPT_Wshadow,
2763 "declaration of %q+D shadows a parameter",
2764 new_decl);
2765 else if (DECL_FILE_SCOPE_P (old_decl))
2767 /* Do not warn if a variable shadows a function, unless
2768 the variable is a function or a pointer-to-function. */
2769 if (TREE_CODE (old_decl) == FUNCTION_DECL
2770 && TREE_CODE (new_decl) != FUNCTION_DECL
2771 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2772 continue;
2774 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2775 "declaration of %qD shadows a global "
2776 "declaration",
2777 new_decl);
2779 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2780 && DECL_BUILT_IN (old_decl))
2782 warning (OPT_Wshadow, "declaration of %q+D shadows "
2783 "a built-in function", new_decl);
2784 break;
2786 else
2787 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2788 "previous local", new_decl);
2790 if (warned)
2791 inform (DECL_SOURCE_LOCATION (old_decl),
2792 "shadowed declaration is here");
2794 break;
2798 /* Record a decl-node X as belonging to the current lexical scope.
2799 Check for errors (such as an incompatible declaration for the same
2800 name already seen in the same scope).
2802 Returns either X or an old decl for the same name.
2803 If an old decl is returned, it may have been smashed
2804 to agree with what X says. */
2806 tree
2807 pushdecl (tree x)
2809 tree name = DECL_NAME (x);
2810 struct c_scope *scope = current_scope;
2811 struct c_binding *b;
2812 bool nested = false;
2813 location_t locus = DECL_SOURCE_LOCATION (x);
2815 /* Must set DECL_CONTEXT for everything not at file scope or
2816 DECL_FILE_SCOPE_P won't work. Local externs don't count
2817 unless they have initializers (which generate code). */
2818 if (current_function_decl
2819 && (!VAR_OR_FUNCTION_DECL_P (x)
2820 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2821 DECL_CONTEXT (x) = current_function_decl;
2823 /* Anonymous decls are just inserted in the scope. */
2824 if (!name)
2826 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2827 locus);
2828 return x;
2831 /* First, see if there is another declaration with the same name in
2832 the current scope. If there is, duplicate_decls may do all the
2833 work for us. If duplicate_decls returns false, that indicates
2834 two incompatible decls in the same scope; we are to silently
2835 replace the old one (duplicate_decls has issued all appropriate
2836 diagnostics). In particular, we should not consider possible
2837 duplicates in the external scope, or shadowing. */
2838 b = I_SYMBOL_BINDING (name);
2839 if (b && B_IN_SCOPE (b, scope))
2841 struct c_binding *b_ext, *b_use;
2842 tree type = TREE_TYPE (x);
2843 tree visdecl = b->decl;
2844 tree vistype = TREE_TYPE (visdecl);
2845 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2846 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2847 b->inner_comp = false;
2848 b_use = b;
2849 b_ext = b;
2850 /* If this is an external linkage declaration, we should check
2851 for compatibility with the type in the external scope before
2852 setting the type at this scope based on the visible
2853 information only. */
2854 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2856 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2857 b_ext = b_ext->shadowed;
2858 if (b_ext)
2860 b_use = b_ext;
2861 if (b_use->u.type)
2862 TREE_TYPE (b_use->decl) = b_use->u.type;
2865 if (duplicate_decls (x, b_use->decl))
2867 if (b_use != b)
2869 /* Save the updated type in the external scope and
2870 restore the proper type for this scope. */
2871 tree thistype;
2872 if (comptypes (vistype, type))
2873 thistype = composite_type (vistype, type);
2874 else
2875 thistype = TREE_TYPE (b_use->decl);
2876 b_use->u.type = TREE_TYPE (b_use->decl);
2877 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2878 && DECL_BUILT_IN (b_use->decl))
2879 thistype
2880 = build_type_attribute_variant (thistype,
2881 TYPE_ATTRIBUTES
2882 (b_use->u.type));
2883 TREE_TYPE (b_use->decl) = thistype;
2885 return b_use->decl;
2887 else
2888 goto skip_external_and_shadow_checks;
2891 /* All declarations with external linkage, and all external
2892 references, go in the external scope, no matter what scope is
2893 current. However, the binding in that scope is ignored for
2894 purposes of normal name lookup. A separate binding structure is
2895 created in the requested scope; this governs the normal
2896 visibility of the symbol.
2898 The binding in the externals scope is used exclusively for
2899 detecting duplicate declarations of the same object, no matter
2900 what scope they are in; this is what we do here. (C99 6.2.7p2:
2901 All declarations that refer to the same object or function shall
2902 have compatible type; otherwise, the behavior is undefined.) */
2903 if (DECL_EXTERNAL (x) || scope == file_scope)
2905 tree type = TREE_TYPE (x);
2906 tree vistype = 0;
2907 tree visdecl = 0;
2908 bool type_saved = false;
2909 if (b && !B_IN_EXTERNAL_SCOPE (b)
2910 && VAR_OR_FUNCTION_DECL_P (b->decl)
2911 && DECL_FILE_SCOPE_P (b->decl))
2913 visdecl = b->decl;
2914 vistype = TREE_TYPE (visdecl);
2916 if (scope != file_scope
2917 && !DECL_IN_SYSTEM_HEADER (x))
2918 warning_at (locus, OPT_Wnested_externs,
2919 "nested extern declaration of %qD", x);
2921 while (b && !B_IN_EXTERNAL_SCOPE (b))
2923 /* If this decl might be modified, save its type. This is
2924 done here rather than when the decl is first bound
2925 because the type may change after first binding, through
2926 being completed or through attributes being added. If we
2927 encounter multiple such decls, only the first should have
2928 its type saved; the others will already have had their
2929 proper types saved and the types will not have changed as
2930 their scopes will not have been re-entered. */
2931 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2933 b->u.type = TREE_TYPE (b->decl);
2934 type_saved = true;
2936 if (B_IN_FILE_SCOPE (b)
2937 && VAR_P (b->decl)
2938 && TREE_STATIC (b->decl)
2939 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2940 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2941 && TREE_CODE (type) == ARRAY_TYPE
2942 && TYPE_DOMAIN (type)
2943 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2944 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2946 /* Array type completed in inner scope, which should be
2947 diagnosed if the completion does not have size 1 and
2948 it does not get completed in the file scope. */
2949 b->inner_comp = true;
2951 b = b->shadowed;
2954 /* If a matching external declaration has been found, set its
2955 type to the composite of all the types of that declaration.
2956 After the consistency checks, it will be reset to the
2957 composite of the visible types only. */
2958 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2959 && b->u.type)
2960 TREE_TYPE (b->decl) = b->u.type;
2962 /* The point of the same_translation_unit_p check here is,
2963 we want to detect a duplicate decl for a construct like
2964 foo() { extern bar(); } ... static bar(); but not if
2965 they are in different translation units. In any case,
2966 the static does not go in the externals scope. */
2967 if (b
2968 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2969 && duplicate_decls (x, b->decl))
2971 tree thistype;
2972 if (vistype)
2974 if (comptypes (vistype, type))
2975 thistype = composite_type (vistype, type);
2976 else
2977 thistype = TREE_TYPE (b->decl);
2979 else
2980 thistype = type;
2981 b->u.type = TREE_TYPE (b->decl);
2982 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2983 thistype
2984 = build_type_attribute_variant (thistype,
2985 TYPE_ATTRIBUTES (b->u.type));
2986 TREE_TYPE (b->decl) = thistype;
2987 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2988 locus);
2989 return b->decl;
2991 else if (TREE_PUBLIC (x))
2993 if (visdecl && !b && duplicate_decls (x, visdecl))
2995 /* An external declaration at block scope referring to a
2996 visible entity with internal linkage. The composite
2997 type will already be correct for this scope, so we
2998 just need to fall through to make the declaration in
2999 this scope. */
3000 nested = true;
3001 x = visdecl;
3003 else
3005 bind (name, x, external_scope, /*invisible=*/true,
3006 /*nested=*/false, locus);
3007 nested = true;
3012 if (TREE_CODE (x) != PARM_DECL)
3013 warn_if_shadowing (x);
3015 skip_external_and_shadow_checks:
3016 if (TREE_CODE (x) == TYPE_DECL)
3018 /* So this is a typedef, set its underlying type. */
3019 set_underlying_type (x);
3021 /* If X is a typedef defined in the current function, record it
3022 for the purpose of implementing the -Wunused-local-typedefs
3023 warning. */
3024 record_locally_defined_typedef (x);
3027 bind (name, x, scope, /*invisible=*/false, nested, locus);
3029 /* If x's type is incomplete because it's based on a
3030 structure or union which has not yet been fully declared,
3031 attach it to that structure or union type, so we can go
3032 back and complete the variable declaration later, if the
3033 structure or union gets fully declared.
3035 If the input is erroneous, we can have error_mark in the type
3036 slot (e.g. "f(void a, ...)") - that doesn't count as an
3037 incomplete type. */
3038 if (TREE_TYPE (x) != error_mark_node
3039 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3041 tree element = TREE_TYPE (x);
3043 while (TREE_CODE (element) == ARRAY_TYPE)
3044 element = TREE_TYPE (element);
3045 element = TYPE_MAIN_VARIANT (element);
3047 if (RECORD_OR_UNION_TYPE_P (element)
3048 && (TREE_CODE (x) != TYPE_DECL
3049 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3050 && !COMPLETE_TYPE_P (element))
3051 C_TYPE_INCOMPLETE_VARS (element)
3052 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3054 return x;
3057 /* Record X as belonging to file scope.
3058 This is used only internally by the Objective-C front end,
3059 and is limited to its needs. duplicate_decls is not called;
3060 if there is any preexisting decl for this identifier, it is an ICE. */
3062 tree
3063 pushdecl_top_level (tree x)
3065 tree name;
3066 bool nested = false;
3067 gcc_assert (VAR_P (x) || TREE_CODE (x) == CONST_DECL);
3069 name = DECL_NAME (x);
3071 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3073 if (TREE_PUBLIC (x))
3075 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3076 UNKNOWN_LOCATION);
3077 nested = true;
3079 if (file_scope)
3080 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3082 return x;
3085 static void
3086 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3088 if (warn_implicit_function_declaration)
3090 bool warned;
3091 const char *hint = NULL;
3092 if (!olddecl)
3093 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME);
3095 if (flag_isoc99)
3096 if (hint)
3098 gcc_rich_location richloc (loc);
3099 richloc.add_fixit_misspelled_id (loc, hint);
3100 warned = pedwarn_at_rich_loc
3101 (&richloc, OPT_Wimplicit_function_declaration,
3102 "implicit declaration of function %qE; did you mean %qs?",
3103 id, hint);
3105 else
3106 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3107 "implicit declaration of function %qE", id);
3108 else
3109 if (hint)
3111 gcc_rich_location richloc (loc);
3112 richloc.add_fixit_misspelled_id (loc, hint);
3113 warned = warning_at_rich_loc
3114 (&richloc, OPT_Wimplicit_function_declaration,
3115 G_("implicit declaration of function %qE;did you mean %qs?"),
3116 id, hint);
3118 else
3119 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3120 G_("implicit declaration of function %qE"), id);
3121 if (olddecl && warned)
3122 locate_old_decl (olddecl);
3126 /* This function represents mapping of a function code FCODE
3127 to its respective header. */
3129 static const char *
3130 header_for_builtin_fn (enum built_in_function fcode)
3132 switch (fcode)
3134 CASE_FLT_FN (BUILT_IN_ACOS):
3135 CASE_FLT_FN (BUILT_IN_ACOSH):
3136 CASE_FLT_FN (BUILT_IN_ASIN):
3137 CASE_FLT_FN (BUILT_IN_ASINH):
3138 CASE_FLT_FN (BUILT_IN_ATAN):
3139 CASE_FLT_FN (BUILT_IN_ATANH):
3140 CASE_FLT_FN (BUILT_IN_ATAN2):
3141 CASE_FLT_FN (BUILT_IN_CBRT):
3142 CASE_FLT_FN (BUILT_IN_CEIL):
3143 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3144 CASE_FLT_FN (BUILT_IN_COS):
3145 CASE_FLT_FN (BUILT_IN_COSH):
3146 CASE_FLT_FN (BUILT_IN_ERF):
3147 CASE_FLT_FN (BUILT_IN_ERFC):
3148 CASE_FLT_FN (BUILT_IN_EXP):
3149 CASE_FLT_FN (BUILT_IN_EXP2):
3150 CASE_FLT_FN (BUILT_IN_EXPM1):
3151 CASE_FLT_FN (BUILT_IN_FABS):
3152 CASE_FLT_FN (BUILT_IN_FDIM):
3153 CASE_FLT_FN (BUILT_IN_FLOOR):
3154 CASE_FLT_FN (BUILT_IN_FMA):
3155 CASE_FLT_FN (BUILT_IN_FMAX):
3156 CASE_FLT_FN (BUILT_IN_FMIN):
3157 CASE_FLT_FN (BUILT_IN_FMOD):
3158 CASE_FLT_FN (BUILT_IN_FREXP):
3159 CASE_FLT_FN (BUILT_IN_HYPOT):
3160 CASE_FLT_FN (BUILT_IN_ILOGB):
3161 CASE_FLT_FN (BUILT_IN_LDEXP):
3162 CASE_FLT_FN (BUILT_IN_LGAMMA):
3163 CASE_FLT_FN (BUILT_IN_LLRINT):
3164 CASE_FLT_FN (BUILT_IN_LLROUND):
3165 CASE_FLT_FN (BUILT_IN_LOG):
3166 CASE_FLT_FN (BUILT_IN_LOG10):
3167 CASE_FLT_FN (BUILT_IN_LOG1P):
3168 CASE_FLT_FN (BUILT_IN_LOG2):
3169 CASE_FLT_FN (BUILT_IN_LOGB):
3170 CASE_FLT_FN (BUILT_IN_LRINT):
3171 CASE_FLT_FN (BUILT_IN_LROUND):
3172 CASE_FLT_FN (BUILT_IN_MODF):
3173 CASE_FLT_FN (BUILT_IN_NAN):
3174 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3175 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3176 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3177 CASE_FLT_FN (BUILT_IN_POW):
3178 CASE_FLT_FN (BUILT_IN_REMAINDER):
3179 CASE_FLT_FN (BUILT_IN_REMQUO):
3180 CASE_FLT_FN (BUILT_IN_RINT):
3181 CASE_FLT_FN (BUILT_IN_ROUND):
3182 CASE_FLT_FN (BUILT_IN_SCALBLN):
3183 CASE_FLT_FN (BUILT_IN_SCALBN):
3184 CASE_FLT_FN (BUILT_IN_SIN):
3185 CASE_FLT_FN (BUILT_IN_SINH):
3186 CASE_FLT_FN (BUILT_IN_SINCOS):
3187 CASE_FLT_FN (BUILT_IN_SQRT):
3188 CASE_FLT_FN (BUILT_IN_TAN):
3189 CASE_FLT_FN (BUILT_IN_TANH):
3190 CASE_FLT_FN (BUILT_IN_TGAMMA):
3191 CASE_FLT_FN (BUILT_IN_TRUNC):
3192 case BUILT_IN_ISINF:
3193 case BUILT_IN_ISNAN:
3194 return "<math.h>";
3195 CASE_FLT_FN (BUILT_IN_CABS):
3196 CASE_FLT_FN (BUILT_IN_CACOS):
3197 CASE_FLT_FN (BUILT_IN_CACOSH):
3198 CASE_FLT_FN (BUILT_IN_CARG):
3199 CASE_FLT_FN (BUILT_IN_CASIN):
3200 CASE_FLT_FN (BUILT_IN_CASINH):
3201 CASE_FLT_FN (BUILT_IN_CATAN):
3202 CASE_FLT_FN (BUILT_IN_CATANH):
3203 CASE_FLT_FN (BUILT_IN_CCOS):
3204 CASE_FLT_FN (BUILT_IN_CCOSH):
3205 CASE_FLT_FN (BUILT_IN_CEXP):
3206 CASE_FLT_FN (BUILT_IN_CIMAG):
3207 CASE_FLT_FN (BUILT_IN_CLOG):
3208 CASE_FLT_FN (BUILT_IN_CONJ):
3209 CASE_FLT_FN (BUILT_IN_CPOW):
3210 CASE_FLT_FN (BUILT_IN_CPROJ):
3211 CASE_FLT_FN (BUILT_IN_CREAL):
3212 CASE_FLT_FN (BUILT_IN_CSIN):
3213 CASE_FLT_FN (BUILT_IN_CSINH):
3214 CASE_FLT_FN (BUILT_IN_CSQRT):
3215 CASE_FLT_FN (BUILT_IN_CTAN):
3216 CASE_FLT_FN (BUILT_IN_CTANH):
3217 return "<complex.h>";
3218 case BUILT_IN_MEMCHR:
3219 case BUILT_IN_MEMCMP:
3220 case BUILT_IN_MEMCPY:
3221 case BUILT_IN_MEMMOVE:
3222 case BUILT_IN_MEMSET:
3223 case BUILT_IN_STRCAT:
3224 case BUILT_IN_STRCHR:
3225 case BUILT_IN_STRCMP:
3226 case BUILT_IN_STRCPY:
3227 case BUILT_IN_STRCSPN:
3228 case BUILT_IN_STRLEN:
3229 case BUILT_IN_STRNCAT:
3230 case BUILT_IN_STRNCMP:
3231 case BUILT_IN_STRNCPY:
3232 case BUILT_IN_STRPBRK:
3233 case BUILT_IN_STRRCHR:
3234 case BUILT_IN_STRSPN:
3235 case BUILT_IN_STRSTR:
3236 return "<string.h>";
3237 case BUILT_IN_FPRINTF:
3238 case BUILT_IN_PUTC:
3239 case BUILT_IN_FPUTC:
3240 case BUILT_IN_FPUTS:
3241 case BUILT_IN_FSCANF:
3242 case BUILT_IN_FWRITE:
3243 case BUILT_IN_PRINTF:
3244 case BUILT_IN_PUTCHAR:
3245 case BUILT_IN_PUTS:
3246 case BUILT_IN_SCANF:
3247 case BUILT_IN_SNPRINTF:
3248 case BUILT_IN_SPRINTF:
3249 case BUILT_IN_SSCANF:
3250 case BUILT_IN_VFPRINTF:
3251 case BUILT_IN_VFSCANF:
3252 case BUILT_IN_VPRINTF:
3253 case BUILT_IN_VSCANF:
3254 case BUILT_IN_VSNPRINTF:
3255 case BUILT_IN_VSPRINTF:
3256 case BUILT_IN_VSSCANF:
3257 return "<stdio.h>";
3258 case BUILT_IN_ISALNUM:
3259 case BUILT_IN_ISALPHA:
3260 case BUILT_IN_ISBLANK:
3261 case BUILT_IN_ISCNTRL:
3262 case BUILT_IN_ISDIGIT:
3263 case BUILT_IN_ISGRAPH:
3264 case BUILT_IN_ISLOWER:
3265 case BUILT_IN_ISPRINT:
3266 case BUILT_IN_ISPUNCT:
3267 case BUILT_IN_ISSPACE:
3268 case BUILT_IN_ISUPPER:
3269 case BUILT_IN_ISXDIGIT:
3270 case BUILT_IN_TOLOWER:
3271 case BUILT_IN_TOUPPER:
3272 return "<ctype.h>";
3273 case BUILT_IN_ISWALNUM:
3274 case BUILT_IN_ISWALPHA:
3275 case BUILT_IN_ISWBLANK:
3276 case BUILT_IN_ISWCNTRL:
3277 case BUILT_IN_ISWDIGIT:
3278 case BUILT_IN_ISWGRAPH:
3279 case BUILT_IN_ISWLOWER:
3280 case BUILT_IN_ISWPRINT:
3281 case BUILT_IN_ISWPUNCT:
3282 case BUILT_IN_ISWSPACE:
3283 case BUILT_IN_ISWUPPER:
3284 case BUILT_IN_ISWXDIGIT:
3285 case BUILT_IN_TOWLOWER:
3286 case BUILT_IN_TOWUPPER:
3287 return "<wctype.h>";
3288 case BUILT_IN_ABORT:
3289 case BUILT_IN_ABS:
3290 case BUILT_IN_CALLOC:
3291 case BUILT_IN_EXIT:
3292 case BUILT_IN_FREE:
3293 case BUILT_IN_LABS:
3294 case BUILT_IN_LLABS:
3295 case BUILT_IN_MALLOC:
3296 case BUILT_IN_REALLOC:
3297 case BUILT_IN__EXIT2:
3298 case BUILT_IN_ALIGNED_ALLOC:
3299 return "<stdlib.h>";
3300 case BUILT_IN_IMAXABS:
3301 return "<inttypes.h>";
3302 case BUILT_IN_STRFTIME:
3303 return "<time.h>";
3304 default:
3305 return NULL;
3309 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3310 function of type int (). */
3312 tree
3313 implicitly_declare (location_t loc, tree functionid)
3315 struct c_binding *b;
3316 tree decl = 0;
3317 tree asmspec_tree;
3319 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3321 if (B_IN_SCOPE (b, external_scope))
3323 decl = b->decl;
3324 break;
3328 if (decl)
3330 if (TREE_CODE (decl) != FUNCTION_DECL)
3331 return decl;
3333 /* FIXME: Objective-C has weird not-really-builtin functions
3334 which are supposed to be visible automatically. They wind up
3335 in the external scope because they're pushed before the file
3336 scope gets created. Catch this here and rebind them into the
3337 file scope. */
3338 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3340 bind (functionid, decl, file_scope,
3341 /*invisible=*/false, /*nested=*/true,
3342 DECL_SOURCE_LOCATION (decl));
3343 return decl;
3345 else
3347 tree newtype = default_function_type;
3348 if (b->u.type)
3349 TREE_TYPE (decl) = b->u.type;
3350 /* Implicit declaration of a function already declared
3351 (somehow) in a different scope, or as a built-in.
3352 If this is the first time this has happened, warn;
3353 then recycle the old declaration but with the new type. */
3354 if (!C_DECL_IMPLICIT (decl))
3356 implicit_decl_warning (loc, functionid, decl);
3357 C_DECL_IMPLICIT (decl) = 1;
3359 if (DECL_BUILT_IN (decl))
3361 newtype = build_type_attribute_variant (newtype,
3362 TYPE_ATTRIBUTES
3363 (TREE_TYPE (decl)));
3364 if (!comptypes (newtype, TREE_TYPE (decl)))
3366 bool warned = warning_at (loc, 0, "incompatible implicit "
3367 "declaration of built-in "
3368 "function %qD", decl);
3369 /* See if we can hint which header to include. */
3370 const char *header
3371 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3372 if (header != NULL && warned)
3373 inform (loc, "include %qs or provide a declaration of %qD",
3374 header, decl);
3375 newtype = TREE_TYPE (decl);
3378 else
3380 if (!comptypes (newtype, TREE_TYPE (decl)))
3382 error_at (loc, "incompatible implicit declaration of "
3383 "function %qD", decl);
3384 locate_old_decl (decl);
3387 b->u.type = TREE_TYPE (decl);
3388 TREE_TYPE (decl) = newtype;
3389 bind (functionid, decl, current_scope,
3390 /*invisible=*/false, /*nested=*/true,
3391 DECL_SOURCE_LOCATION (decl));
3392 return decl;
3396 /* Not seen before. */
3397 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3398 DECL_EXTERNAL (decl) = 1;
3399 TREE_PUBLIC (decl) = 1;
3400 C_DECL_IMPLICIT (decl) = 1;
3401 implicit_decl_warning (loc, functionid, 0);
3402 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3403 if (asmspec_tree)
3404 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3406 /* C89 says implicit declarations are in the innermost block.
3407 So we record the decl in the standard fashion. */
3408 decl = pushdecl (decl);
3410 /* No need to call objc_check_decl here - it's a function type. */
3411 rest_of_decl_compilation (decl, 0, 0);
3413 /* Write a record describing this implicit function declaration
3414 to the prototypes file (if requested). */
3415 gen_aux_info_record (decl, 0, 1, 0);
3417 /* Possibly apply some default attributes to this implicit declaration. */
3418 decl_attributes (&decl, NULL_TREE, 0);
3420 return decl;
3423 /* Issue an error message for a reference to an undeclared variable
3424 ID, including a reference to a builtin outside of function-call
3425 context. Establish a binding of the identifier to error_mark_node
3426 in an appropriate scope, which will suppress further errors for the
3427 same identifier. The error message should be given location LOC. */
3428 void
3429 undeclared_variable (location_t loc, tree id)
3431 static bool already = false;
3432 struct c_scope *scope;
3434 if (current_function_decl == 0)
3436 const char *guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME);
3437 if (guessed_id)
3439 gcc_rich_location richloc (loc);
3440 richloc.add_fixit_misspelled_id (loc, guessed_id);
3441 error_at_rich_loc (&richloc,
3442 "%qE undeclared here (not in a function);"
3443 " did you mean %qs?",
3444 id, guessed_id);
3446 else
3447 error_at (loc, "%qE undeclared here (not in a function)", id);
3448 scope = current_scope;
3450 else
3452 if (!objc_diagnose_private_ivar (id))
3454 const char *guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME);
3455 if (guessed_id)
3457 gcc_rich_location richloc (loc);
3458 richloc.add_fixit_misspelled_id (loc, guessed_id);
3459 error_at_rich_loc
3460 (&richloc,
3461 "%qE undeclared (first use in this function);"
3462 " did you mean %qs?",
3463 id, guessed_id);
3465 else
3466 error_at (loc, "%qE undeclared (first use in this function)", id);
3468 if (!already)
3470 inform (loc, "each undeclared identifier is reported only"
3471 " once for each function it appears in");
3472 already = true;
3475 /* If we are parsing old-style parameter decls, current_function_decl
3476 will be nonnull but current_function_scope will be null. */
3477 scope = current_function_scope ? current_function_scope : current_scope;
3479 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3480 UNKNOWN_LOCATION);
3483 /* Subroutine of lookup_label, declare_label, define_label: construct a
3484 LABEL_DECL with all the proper frills. Also create a struct
3485 c_label_vars initialized for the current scope. */
3487 static tree
3488 make_label (location_t location, tree name, bool defining,
3489 struct c_label_vars **p_label_vars)
3491 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3492 DECL_CONTEXT (label) = current_function_decl;
3493 DECL_MODE (label) = VOIDmode;
3495 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3496 label_vars->shadowed = NULL;
3497 set_spot_bindings (&label_vars->label_bindings, defining);
3498 label_vars->decls_in_scope = make_tree_vector ();
3499 label_vars->gotos = NULL;
3500 *p_label_vars = label_vars;
3502 return label;
3505 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3506 Create one if none exists so far for the current function.
3507 This is called when a label is used in a goto expression or
3508 has its address taken. */
3510 tree
3511 lookup_label (tree name)
3513 tree label;
3514 struct c_label_vars *label_vars;
3516 if (current_function_scope == 0)
3518 error ("label %qE referenced outside of any function", name);
3519 return NULL_TREE;
3522 /* Use a label already defined or ref'd with this name, but not if
3523 it is inherited from a containing function and wasn't declared
3524 using __label__. */
3525 label = I_LABEL_DECL (name);
3526 if (label && (DECL_CONTEXT (label) == current_function_decl
3527 || C_DECLARED_LABEL_FLAG (label)))
3529 /* If the label has only been declared, update its apparent
3530 location to point here, for better diagnostics if it
3531 turns out not to have been defined. */
3532 if (DECL_INITIAL (label) == NULL_TREE)
3533 DECL_SOURCE_LOCATION (label) = input_location;
3534 return label;
3537 /* No label binding for that identifier; make one. */
3538 label = make_label (input_location, name, false, &label_vars);
3540 /* Ordinary labels go in the current function scope. */
3541 bind_label (name, label, current_function_scope, label_vars);
3543 return label;
3546 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3547 to LABEL. */
3549 static void
3550 warn_about_goto (location_t goto_loc, tree label, tree decl)
3552 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3553 error_at (goto_loc,
3554 "jump into scope of identifier with variably modified type");
3555 else
3556 warning_at (goto_loc, OPT_Wjump_misses_init,
3557 "jump skips variable initialization");
3558 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3559 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3562 /* Look up a label because of a goto statement. This is like
3563 lookup_label, but also issues any appropriate warnings. */
3565 tree
3566 lookup_label_for_goto (location_t loc, tree name)
3568 tree label;
3569 struct c_label_vars *label_vars;
3570 unsigned int ix;
3571 tree decl;
3573 label = lookup_label (name);
3574 if (label == NULL_TREE)
3575 return NULL_TREE;
3577 /* If we are jumping to a different function, we can't issue any
3578 useful warnings. */
3579 if (DECL_CONTEXT (label) != current_function_decl)
3581 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3582 return label;
3585 label_vars = I_LABEL_BINDING (name)->u.label;
3587 /* If the label has not yet been defined, then push this goto on a
3588 list for possible later warnings. */
3589 if (label_vars->label_bindings.scope == NULL)
3591 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3593 g->loc = loc;
3594 set_spot_bindings (&g->goto_bindings, true);
3595 vec_safe_push (label_vars->gotos, g);
3596 return label;
3599 /* If there are any decls in label_vars->decls_in_scope, then this
3600 goto has missed the declaration of the decl. This happens for a
3601 case like
3602 int i = 1;
3603 lab:
3605 goto lab;
3606 Issue a warning or error. */
3607 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3608 warn_about_goto (loc, label, decl);
3610 if (label_vars->label_bindings.left_stmt_expr)
3612 error_at (loc, "jump into statement expression");
3613 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3616 return label;
3619 /* Make a label named NAME in the current function, shadowing silently
3620 any that may be inherited from containing functions or containing
3621 scopes. This is called for __label__ declarations. */
3623 tree
3624 declare_label (tree name)
3626 struct c_binding *b = I_LABEL_BINDING (name);
3627 tree label;
3628 struct c_label_vars *label_vars;
3630 /* Check to make sure that the label hasn't already been declared
3631 at this scope */
3632 if (b && B_IN_CURRENT_SCOPE (b))
3634 error ("duplicate label declaration %qE", name);
3635 locate_old_decl (b->decl);
3637 /* Just use the previous declaration. */
3638 return b->decl;
3641 label = make_label (input_location, name, false, &label_vars);
3642 C_DECLARED_LABEL_FLAG (label) = 1;
3644 /* Declared labels go in the current scope. */
3645 bind_label (name, label, current_scope, label_vars);
3647 return label;
3650 /* When we define a label, issue any appropriate warnings if there are
3651 any gotos earlier in the function which jump to this label. */
3653 static void
3654 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3656 unsigned int ix;
3657 struct c_goto_bindings *g;
3659 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3661 struct c_binding *b;
3662 struct c_scope *scope;
3664 /* We have a goto to this label. The goto is going forward. In
3665 g->scope, the goto is going to skip any binding which was
3666 defined after g->bindings_in_scope. */
3667 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3669 for (b = g->goto_bindings.scope->bindings;
3670 b != g->goto_bindings.bindings_in_scope;
3671 b = b->prev)
3673 if (decl_jump_unsafe (b->decl))
3674 warn_about_goto (g->loc, label, b->decl);
3678 /* We also need to warn about decls defined in any scopes
3679 between the scope of the label and the scope of the goto. */
3680 for (scope = label_vars->label_bindings.scope;
3681 scope != g->goto_bindings.scope;
3682 scope = scope->outer)
3684 gcc_assert (scope != NULL);
3685 if (scope->has_jump_unsafe_decl)
3687 if (scope == label_vars->label_bindings.scope)
3688 b = label_vars->label_bindings.bindings_in_scope;
3689 else
3690 b = scope->bindings;
3691 for (; b != NULL; b = b->prev)
3693 if (decl_jump_unsafe (b->decl))
3694 warn_about_goto (g->loc, label, b->decl);
3699 if (g->goto_bindings.stmt_exprs > 0)
3701 error_at (g->loc, "jump into statement expression");
3702 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3703 label);
3707 /* Now that the label is defined, we will issue warnings about
3708 subsequent gotos to this label when we see them. */
3709 vec_safe_truncate (label_vars->gotos, 0);
3710 label_vars->gotos = NULL;
3713 /* Define a label, specifying the location in the source file.
3714 Return the LABEL_DECL node for the label, if the definition is valid.
3715 Otherwise return 0. */
3717 tree
3718 define_label (location_t location, tree name)
3720 /* Find any preexisting label with this name. It is an error
3721 if that label has already been defined in this function, or
3722 if there is a containing function with a declared label with
3723 the same name. */
3724 tree label = I_LABEL_DECL (name);
3726 if (label
3727 && ((DECL_CONTEXT (label) == current_function_decl
3728 && DECL_INITIAL (label) != 0)
3729 || (DECL_CONTEXT (label) != current_function_decl
3730 && C_DECLARED_LABEL_FLAG (label))))
3732 error_at (location, "duplicate label %qD", label);
3733 locate_old_decl (label);
3734 return 0;
3736 else if (label && DECL_CONTEXT (label) == current_function_decl)
3738 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3740 /* The label has been used or declared already in this function,
3741 but not defined. Update its location to point to this
3742 definition. */
3743 DECL_SOURCE_LOCATION (label) = location;
3744 set_spot_bindings (&label_vars->label_bindings, true);
3746 /* Issue warnings as required about any goto statements from
3747 earlier in the function. */
3748 check_earlier_gotos (label, label_vars);
3750 else
3752 struct c_label_vars *label_vars;
3754 /* No label binding for that identifier; make one. */
3755 label = make_label (location, name, true, &label_vars);
3757 /* Ordinary labels go in the current function scope. */
3758 bind_label (name, label, current_function_scope, label_vars);
3761 if (!in_system_header_at (input_location) && lookup_name (name))
3762 warning_at (location, OPT_Wtraditional,
3763 "traditional C lacks a separate namespace "
3764 "for labels, identifier %qE conflicts", name);
3766 /* Mark label as having been defined. */
3767 DECL_INITIAL (label) = error_mark_node;
3768 return label;
3771 /* Get the bindings for a new switch statement. This is used to issue
3772 warnings as appropriate for jumps from the switch to case or
3773 default labels. */
3775 struct c_spot_bindings *
3776 c_get_switch_bindings (void)
3778 struct c_spot_bindings *switch_bindings;
3780 switch_bindings = XNEW (struct c_spot_bindings);
3781 set_spot_bindings (switch_bindings, true);
3782 return switch_bindings;
3785 void
3786 c_release_switch_bindings (struct c_spot_bindings *bindings)
3788 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3789 XDELETE (bindings);
3792 /* This is called at the point of a case or default label to issue
3793 warnings about decls as needed. It returns true if it found an
3794 error, not just a warning. */
3796 bool
3797 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3798 location_t switch_loc, location_t case_loc)
3800 bool saw_error;
3801 struct c_scope *scope;
3803 saw_error = false;
3804 for (scope = current_scope;
3805 scope != switch_bindings->scope;
3806 scope = scope->outer)
3808 struct c_binding *b;
3810 gcc_assert (scope != NULL);
3812 if (!scope->has_jump_unsafe_decl)
3813 continue;
3815 for (b = scope->bindings; b != NULL; b = b->prev)
3817 if (decl_jump_unsafe (b->decl))
3819 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3821 saw_error = true;
3822 error_at (case_loc,
3823 ("switch jumps into scope of identifier with "
3824 "variably modified type"));
3826 else
3827 warning_at (case_loc, OPT_Wjump_misses_init,
3828 "switch jumps over variable initialization");
3829 inform (switch_loc, "switch starts here");
3830 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3831 b->decl);
3836 if (switch_bindings->stmt_exprs > 0)
3838 saw_error = true;
3839 error_at (case_loc, "switch jumps into statement expression");
3840 inform (switch_loc, "switch starts here");
3843 return saw_error;
3846 /* Given NAME, an IDENTIFIER_NODE,
3847 return the structure (or union or enum) definition for that name.
3848 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3849 CODE says which kind of type the caller wants;
3850 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3851 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3852 location where the tag was defined.
3853 If the wrong kind of type is found, an error is reported. */
3855 static tree
3856 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3857 location_t *ploc)
3859 struct c_binding *b = I_TAG_BINDING (name);
3860 bool thislevel = false;
3862 if (!b || !b->decl)
3863 return NULL_TREE;
3865 /* We only care about whether it's in this level if
3866 thislevel_only was set or it might be a type clash. */
3867 if (thislevel_only || TREE_CODE (b->decl) != code)
3869 /* For our purposes, a tag in the external scope is the same as
3870 a tag in the file scope. (Primarily relevant to Objective-C
3871 and its builtin structure tags, which get pushed before the
3872 file scope is created.) */
3873 if (B_IN_CURRENT_SCOPE (b)
3874 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3875 thislevel = true;
3878 if (thislevel_only && !thislevel)
3879 return NULL_TREE;
3881 if (TREE_CODE (b->decl) != code)
3883 /* Definition isn't the kind we were looking for. */
3884 pending_invalid_xref = name;
3885 pending_invalid_xref_location = input_location;
3887 /* If in the same binding level as a declaration as a tag
3888 of a different type, this must not be allowed to
3889 shadow that tag, so give the error immediately.
3890 (For example, "struct foo; union foo;" is invalid.) */
3891 if (thislevel)
3892 pending_xref_error ();
3895 if (ploc != NULL)
3896 *ploc = b->locus;
3898 return b->decl;
3901 /* Return true if a definition exists for NAME with code CODE. */
3903 bool
3904 tag_exists_p (enum tree_code code, tree name)
3906 struct c_binding *b = I_TAG_BINDING (name);
3908 if (b == NULL || b->decl == NULL_TREE)
3909 return false;
3910 return TREE_CODE (b->decl) == code;
3913 /* Print an error message now
3914 for a recent invalid struct, union or enum cross reference.
3915 We don't print them immediately because they are not invalid
3916 when used in the `struct foo;' construct for shadowing. */
3918 void
3919 pending_xref_error (void)
3921 if (pending_invalid_xref != 0)
3922 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3923 pending_invalid_xref);
3924 pending_invalid_xref = 0;
3928 /* Look up NAME in the current scope and its superiors
3929 in the namespace of variables, functions and typedefs.
3930 Return a ..._DECL node of some kind representing its definition,
3931 or return 0 if it is undefined. */
3933 tree
3934 lookup_name (tree name)
3936 struct c_binding *b = I_SYMBOL_BINDING (name);
3937 if (b && !b->invisible)
3939 maybe_record_typedef_use (b->decl);
3940 return b->decl;
3942 return NULL_TREE;
3945 /* Similar to `lookup_name' but look only at the indicated scope. */
3947 static tree
3948 lookup_name_in_scope (tree name, struct c_scope *scope)
3950 struct c_binding *b;
3952 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3953 if (B_IN_SCOPE (b, scope))
3954 return b->decl;
3955 return NULL_TREE;
3958 /* Look for the closest match for NAME within the currently valid
3959 scopes.
3961 This finds the identifier with the lowest Levenshtein distance to
3962 NAME. If there are multiple candidates with equal minimal distance,
3963 the first one found is returned. Scopes are searched from innermost
3964 outwards, and within a scope in reverse order of declaration, thus
3965 benefiting candidates "near" to the current scope.
3967 The function also looks for similar macro names to NAME, since a
3968 misspelled macro name will not be expanded, and hence looks like an
3969 identifier to the C frontend.
3971 It also looks for start_typename keywords, to detect "singed" vs "signed"
3972 typos. */
3974 const char *
3975 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
3977 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3979 best_match<tree, tree> bm (name);
3981 /* Look within currently valid scopes. */
3982 for (c_scope *scope = current_scope; scope; scope = scope->outer)
3983 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
3985 if (!binding->id || binding->invisible)
3986 continue;
3987 /* Don't use bindings from implicitly declared functions,
3988 as they were likely misspellings themselves. */
3989 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
3990 if (C_DECL_IMPLICIT (binding->decl))
3991 continue;
3992 switch (kind)
3994 case FUZZY_LOOKUP_TYPENAME:
3995 if (TREE_CODE (binding->decl) != TYPE_DECL)
3996 continue;
3997 break;
3999 case FUZZY_LOOKUP_FUNCTION_NAME:
4000 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4002 /* Allow function pointers. */
4003 if ((VAR_P (binding->decl)
4004 || TREE_CODE (binding->decl) == PARM_DECL)
4005 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4006 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4007 == FUNCTION_TYPE))
4008 break;
4009 continue;
4011 break;
4013 default:
4014 break;
4016 bm.consider (binding->id);
4019 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4021 x = SOME_OTHER_MACRO (y);
4022 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4023 as a misspelled identifier.
4025 Use the best distance so far so that a candidate is only set if
4026 a macro is better than anything so far. This allows early rejection
4027 (without calculating the edit distance) of macro names that must have
4028 distance >= bm.get_best_distance (), and means that we only get a
4029 non-NULL result for best_macro_match if it's better than any of
4030 the identifiers already checked, which avoids needless creation
4031 of identifiers for macro hashnodes. */
4032 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4033 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4034 /* If a macro is the closest so far to NAME, use it, creating an
4035 identifier tree node for it. */
4036 if (best_macro)
4038 const char *id = (const char *)best_macro->ident.str;
4039 tree macro_as_identifier
4040 = get_identifier_with_length (id, best_macro->ident.len);
4041 bm.set_best_so_far (macro_as_identifier,
4042 bmm.get_best_distance (),
4043 bmm.get_best_candidate_length ());
4046 /* Try the "start_typename" keywords to detect
4047 "singed" vs "signed" typos. */
4048 if (kind == FUZZY_LOOKUP_TYPENAME)
4050 for (unsigned i = 0; i < num_c_common_reswords; i++)
4052 const c_common_resword *resword = &c_common_reswords[i];
4053 if (!c_keyword_starts_typename (resword->rid))
4054 continue;
4055 tree resword_identifier = ridpointers [resword->rid];
4056 if (!resword_identifier)
4057 continue;
4058 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4059 bm.consider (resword_identifier);
4063 tree best = bm.get_best_meaningful_candidate ();
4064 if (best)
4065 return IDENTIFIER_POINTER (best);
4066 else
4067 return NULL;
4071 /* Create the predefined scalar types of C,
4072 and some nodes representing standard constants (0, 1, (void *) 0).
4073 Initialize the global scope.
4074 Make definitions for built-in primitive functions. */
4076 void
4077 c_init_decl_processing (void)
4079 location_t save_loc = input_location;
4081 /* Initialize reserved words for parser. */
4082 c_parse_init ();
4084 current_function_decl = 0;
4086 gcc_obstack_init (&parser_obstack);
4088 /* Make the externals scope. */
4089 push_scope ();
4090 external_scope = current_scope;
4092 /* Declarations from c_common_nodes_and_builtins must not be associated
4093 with this input file, lest we get differences between using and not
4094 using preprocessed headers. */
4095 input_location = BUILTINS_LOCATION;
4097 c_common_nodes_and_builtins ();
4099 /* In C, comparisons and TRUTH_* expressions have type int. */
4100 truthvalue_type_node = integer_type_node;
4101 truthvalue_true_node = integer_one_node;
4102 truthvalue_false_node = integer_zero_node;
4104 /* Even in C99, which has a real boolean type. */
4105 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4106 boolean_type_node));
4108 input_location = save_loc;
4110 make_fname_decl = c_make_fname_decl;
4111 start_fname_decls ();
4114 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4115 give the decl, NAME is the initialization string and TYPE_DEP
4116 indicates whether NAME depended on the type of the function. As we
4117 don't yet implement delayed emission of static data, we mark the
4118 decl as emitted so it is not placed in the output. Anything using
4119 it must therefore pull out the STRING_CST initializer directly.
4120 FIXME. */
4122 static tree
4123 c_make_fname_decl (location_t loc, tree id, int type_dep)
4125 const char *name = fname_as_string (type_dep);
4126 tree decl, type, init;
4127 size_t length = strlen (name);
4129 type = build_array_type (char_type_node,
4130 build_index_type (size_int (length)));
4131 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4133 decl = build_decl (loc, VAR_DECL, id, type);
4135 TREE_STATIC (decl) = 1;
4136 TREE_READONLY (decl) = 1;
4137 DECL_ARTIFICIAL (decl) = 1;
4139 init = build_string (length + 1, name);
4140 free (CONST_CAST (char *, name));
4141 TREE_TYPE (init) = type;
4142 DECL_INITIAL (decl) = init;
4144 TREE_USED (decl) = 1;
4146 if (current_function_decl
4147 /* For invalid programs like this:
4149 void foo()
4150 const char* p = __FUNCTION__;
4152 the __FUNCTION__ is believed to appear in K&R style function
4153 parameter declarator. In that case we still don't have
4154 function_scope. */
4155 && current_function_scope)
4157 DECL_CONTEXT (decl) = current_function_decl;
4158 bind (id, decl, current_function_scope,
4159 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4162 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4164 return decl;
4167 tree
4168 c_builtin_function (tree decl)
4170 tree type = TREE_TYPE (decl);
4171 tree id = DECL_NAME (decl);
4173 const char *name = IDENTIFIER_POINTER (id);
4174 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4176 /* Should never be called on a symbol with a preexisting meaning. */
4177 gcc_assert (!I_SYMBOL_BINDING (id));
4179 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4180 UNKNOWN_LOCATION);
4182 /* Builtins in the implementation namespace are made visible without
4183 needing to be explicitly declared. See push_file_scope. */
4184 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4186 DECL_CHAIN (decl) = visible_builtins;
4187 visible_builtins = decl;
4190 return decl;
4193 tree
4194 c_builtin_function_ext_scope (tree decl)
4196 tree type = TREE_TYPE (decl);
4197 tree id = DECL_NAME (decl);
4199 const char *name = IDENTIFIER_POINTER (id);
4200 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4202 if (external_scope)
4203 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4204 UNKNOWN_LOCATION);
4206 /* Builtins in the implementation namespace are made visible without
4207 needing to be explicitly declared. See push_file_scope. */
4208 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4210 DECL_CHAIN (decl) = visible_builtins;
4211 visible_builtins = decl;
4214 return decl;
4217 /* Called when a declaration is seen that contains no names to declare.
4218 If its type is a reference to a structure, union or enum inherited
4219 from a containing scope, shadow that tag name for the current scope
4220 with a forward reference.
4221 If its type defines a new named structure or union
4222 or defines an enum, it is valid but we need not do anything here.
4223 Otherwise, it is an error. */
4225 void
4226 shadow_tag (const struct c_declspecs *declspecs)
4228 shadow_tag_warned (declspecs, 0);
4231 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4232 but no pedwarn. */
4233 void
4234 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4236 bool found_tag = false;
4238 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4240 tree value = declspecs->type;
4241 enum tree_code code = TREE_CODE (value);
4243 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4244 /* Used to test also that TYPE_SIZE (value) != 0.
4245 That caused warning for `struct foo;' at top level in the file. */
4247 tree name = TYPE_NAME (value);
4248 tree t;
4250 found_tag = true;
4252 if (declspecs->restrict_p)
4254 error ("invalid use of %<restrict%>");
4255 warned = 1;
4258 if (name == 0)
4260 if (warned != 1 && code != ENUMERAL_TYPE)
4261 /* Empty unnamed enum OK */
4263 pedwarn (input_location, 0,
4264 "unnamed struct/union that defines no instances");
4265 warned = 1;
4268 else if (declspecs->typespec_kind != ctsk_tagdef
4269 && declspecs->typespec_kind != ctsk_tagfirstref
4270 && declspecs->storage_class != csc_none)
4272 if (warned != 1)
4273 pedwarn (input_location, 0,
4274 "empty declaration with storage class specifier "
4275 "does not redeclare tag");
4276 warned = 1;
4277 pending_xref_error ();
4279 else if (declspecs->typespec_kind != ctsk_tagdef
4280 && declspecs->typespec_kind != ctsk_tagfirstref
4281 && (declspecs->const_p
4282 || declspecs->volatile_p
4283 || declspecs->atomic_p
4284 || declspecs->restrict_p
4285 || declspecs->address_space))
4287 if (warned != 1)
4288 pedwarn (input_location, 0,
4289 "empty declaration with type qualifier "
4290 "does not redeclare tag");
4291 warned = 1;
4292 pending_xref_error ();
4294 else if (declspecs->typespec_kind != ctsk_tagdef
4295 && declspecs->typespec_kind != ctsk_tagfirstref
4296 && declspecs->alignas_p)
4298 if (warned != 1)
4299 pedwarn (input_location, 0,
4300 "empty declaration with %<_Alignas%> "
4301 "does not redeclare tag");
4302 warned = 1;
4303 pending_xref_error ();
4305 else
4307 pending_invalid_xref = 0;
4308 t = lookup_tag (code, name, true, NULL);
4310 if (t == NULL_TREE)
4312 t = make_node (code);
4313 pushtag (input_location, name, t);
4317 else
4319 if (warned != 1 && !in_system_header_at (input_location))
4321 pedwarn (input_location, 0,
4322 "useless type name in empty declaration");
4323 warned = 1;
4327 else if (warned != 1 && !in_system_header_at (input_location)
4328 && declspecs->typedef_p)
4330 pedwarn (input_location, 0, "useless type name in empty declaration");
4331 warned = 1;
4334 pending_invalid_xref = 0;
4336 if (declspecs->inline_p)
4338 error ("%<inline%> in empty declaration");
4339 warned = 1;
4342 if (declspecs->noreturn_p)
4344 error ("%<_Noreturn%> in empty declaration");
4345 warned = 1;
4348 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4350 error ("%<auto%> in file-scope empty declaration");
4351 warned = 1;
4354 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4356 error ("%<register%> in file-scope empty declaration");
4357 warned = 1;
4360 if (!warned && !in_system_header_at (input_location)
4361 && declspecs->storage_class != csc_none)
4363 warning (0, "useless storage class specifier in empty declaration");
4364 warned = 2;
4367 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4369 warning (0, "useless %qs in empty declaration",
4370 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4371 warned = 2;
4374 if (!warned
4375 && !in_system_header_at (input_location)
4376 && (declspecs->const_p
4377 || declspecs->volatile_p
4378 || declspecs->atomic_p
4379 || declspecs->restrict_p
4380 || declspecs->address_space))
4382 warning (0, "useless type qualifier in empty declaration");
4383 warned = 2;
4386 if (!warned && !in_system_header_at (input_location)
4387 && declspecs->alignas_p)
4389 warning (0, "useless %<_Alignas%> in empty declaration");
4390 warned = 2;
4393 if (warned != 1)
4395 if (!found_tag)
4396 pedwarn (input_location, 0, "empty declaration");
4401 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4402 bits. SPECS represents declaration specifiers that the grammar
4403 only permits to contain type qualifiers and attributes. */
4406 quals_from_declspecs (const struct c_declspecs *specs)
4408 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4409 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4410 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4411 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4412 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4413 gcc_assert (!specs->type
4414 && !specs->decl_attr
4415 && specs->typespec_word == cts_none
4416 && specs->storage_class == csc_none
4417 && !specs->typedef_p
4418 && !specs->explicit_signed_p
4419 && !specs->deprecated_p
4420 && !specs->long_p
4421 && !specs->long_long_p
4422 && !specs->short_p
4423 && !specs->signed_p
4424 && !specs->unsigned_p
4425 && !specs->complex_p
4426 && !specs->inline_p
4427 && !specs->noreturn_p
4428 && !specs->thread_p);
4429 return quals;
4432 /* Construct an array declarator. LOC is the location of the
4433 beginning of the array (usually the opening brace). EXPR is the
4434 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4435 inside the [] (to be applied to the pointer to which a parameter
4436 array is converted). STATIC_P is true if "static" is inside the
4437 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4438 VLA of unspecified length which is nevertheless a complete type,
4439 false otherwise. The field for the contained declarator is left to
4440 be filled in by set_array_declarator_inner. */
4442 struct c_declarator *
4443 build_array_declarator (location_t loc,
4444 tree expr, struct c_declspecs *quals, bool static_p,
4445 bool vla_unspec_p)
4447 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4448 struct c_declarator);
4449 declarator->id_loc = loc;
4450 declarator->kind = cdk_array;
4451 declarator->declarator = 0;
4452 declarator->u.array.dimen = expr;
4453 if (quals)
4455 declarator->u.array.attrs = quals->attrs;
4456 declarator->u.array.quals = quals_from_declspecs (quals);
4458 else
4460 declarator->u.array.attrs = NULL_TREE;
4461 declarator->u.array.quals = 0;
4463 declarator->u.array.static_p = static_p;
4464 declarator->u.array.vla_unspec_p = vla_unspec_p;
4465 if (static_p || quals != NULL)
4466 pedwarn_c90 (loc, OPT_Wpedantic,
4467 "ISO C90 does not support %<static%> or type "
4468 "qualifiers in parameter array declarators");
4469 if (vla_unspec_p)
4470 pedwarn_c90 (loc, OPT_Wpedantic,
4471 "ISO C90 does not support %<[*]%> array declarators");
4472 if (vla_unspec_p)
4474 if (!current_scope->parm_flag)
4476 /* C99 6.7.5.2p4 */
4477 error_at (loc, "%<[*]%> not allowed in other than "
4478 "function prototype scope");
4479 declarator->u.array.vla_unspec_p = false;
4480 return NULL;
4482 current_scope->had_vla_unspec = true;
4484 return declarator;
4487 /* Set the contained declarator of an array declarator. DECL is the
4488 declarator, as constructed by build_array_declarator; INNER is what
4489 appears on the left of the []. */
4491 struct c_declarator *
4492 set_array_declarator_inner (struct c_declarator *decl,
4493 struct c_declarator *inner)
4495 decl->declarator = inner;
4496 return decl;
4499 /* INIT is a constructor that forms DECL's initializer. If the final
4500 element initializes a flexible array field, add the size of that
4501 initializer to DECL's size. */
4503 static void
4504 add_flexible_array_elts_to_size (tree decl, tree init)
4506 tree elt, type;
4508 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4509 return;
4511 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4512 type = TREE_TYPE (elt);
4513 if (TREE_CODE (type) == ARRAY_TYPE
4514 && TYPE_SIZE (type) == NULL_TREE
4515 && TYPE_DOMAIN (type) != NULL_TREE
4516 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4518 complete_array_type (&type, elt, false);
4519 DECL_SIZE (decl)
4520 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4521 DECL_SIZE_UNIT (decl)
4522 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4526 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4527 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4528 before the type name, and set *EXPR_CONST_OPERANDS, if
4529 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4530 appear in a constant expression. */
4532 tree
4533 groktypename (struct c_type_name *type_name, tree *expr,
4534 bool *expr_const_operands)
4536 tree type;
4537 tree attrs = type_name->specs->attrs;
4539 type_name->specs->attrs = NULL_TREE;
4541 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4542 false, NULL, &attrs, expr, expr_const_operands,
4543 DEPRECATED_NORMAL);
4545 /* Apply attributes. */
4546 decl_attributes (&type, attrs, 0);
4548 return type;
4551 /* Wrapper for decl_attributes that adds some implicit attributes
4552 to VAR_DECLs or FUNCTION_DECLs. */
4554 static tree
4555 c_decl_attributes (tree *node, tree attributes, int flags)
4557 /* Add implicit "omp declare target" attribute if requested. */
4558 if (current_omp_declare_target_attribute
4559 && ((VAR_P (*node) && is_global_var (*node))
4560 || TREE_CODE (*node) == FUNCTION_DECL))
4562 if (VAR_P (*node)
4563 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4564 error ("%q+D in declare target directive does not have mappable type",
4565 *node);
4566 else
4567 attributes = tree_cons (get_identifier ("omp declare target"),
4568 NULL_TREE, attributes);
4570 return decl_attributes (node, attributes, flags);
4574 /* Decode a declarator in an ordinary declaration or data definition.
4575 This is called as soon as the type information and variable name
4576 have been parsed, before parsing the initializer if any.
4577 Here we create the ..._DECL node, fill in its type,
4578 and put it on the list of decls for the current context.
4579 The ..._DECL node is returned as the value.
4581 Exception: for arrays where the length is not specified,
4582 the type is left null, to be filled in by `finish_decl'.
4584 Function definitions do not come here; they go to start_function
4585 instead. However, external and forward declarations of functions
4586 do go through here. Structure field declarations are done by
4587 grokfield and not through here. */
4589 tree
4590 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4591 bool initialized, tree attributes)
4593 tree decl;
4594 tree tem;
4595 tree expr = NULL_TREE;
4596 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4598 /* An object declared as __attribute__((deprecated)) suppresses
4599 warnings of uses of other deprecated items. */
4600 if (lookup_attribute ("deprecated", attributes))
4601 deprecated_state = DEPRECATED_SUPPRESS;
4603 decl = grokdeclarator (declarator, declspecs,
4604 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4605 deprecated_state);
4606 if (!decl || decl == error_mark_node)
4607 return NULL_TREE;
4609 if (expr)
4610 add_stmt (fold_convert (void_type_node, expr));
4612 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4613 warning (OPT_Wmain, "%q+D is usually a function", decl);
4615 if (initialized)
4616 /* Is it valid for this decl to have an initializer at all?
4617 If not, set INITIALIZED to zero, which will indirectly
4618 tell 'finish_decl' to ignore the initializer once it is parsed. */
4619 switch (TREE_CODE (decl))
4621 case TYPE_DECL:
4622 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4623 initialized = 0;
4624 break;
4626 case FUNCTION_DECL:
4627 error ("function %qD is initialized like a variable", decl);
4628 initialized = 0;
4629 break;
4631 case PARM_DECL:
4632 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4633 error ("parameter %qD is initialized", decl);
4634 initialized = 0;
4635 break;
4637 default:
4638 /* Don't allow initializations for incomplete types except for
4639 arrays which might be completed by the initialization. */
4641 /* This can happen if the array size is an undefined macro.
4642 We already gave a warning, so we don't need another one. */
4643 if (TREE_TYPE (decl) == error_mark_node)
4644 initialized = 0;
4645 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4647 /* A complete type is ok if size is fixed. */
4649 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4650 || C_DECL_VARIABLE_SIZE (decl))
4652 error ("variable-sized object may not be initialized");
4653 initialized = 0;
4656 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4658 error ("variable %qD has initializer but incomplete type", decl);
4659 initialized = 0;
4661 else if (C_DECL_VARIABLE_SIZE (decl))
4663 /* Although C99 is unclear about whether incomplete arrays
4664 of VLAs themselves count as VLAs, it does not make
4665 sense to permit them to be initialized given that
4666 ordinary VLAs may not be initialized. */
4667 error ("variable-sized object may not be initialized");
4668 initialized = 0;
4672 if (initialized)
4674 if (current_scope == file_scope)
4675 TREE_STATIC (decl) = 1;
4677 /* Tell 'pushdecl' this is an initialized decl
4678 even though we don't yet have the initializer expression.
4679 Also tell 'finish_decl' it may store the real initializer. */
4680 DECL_INITIAL (decl) = error_mark_node;
4683 /* If this is a function declaration, write a record describing it to the
4684 prototypes file (if requested). */
4686 if (TREE_CODE (decl) == FUNCTION_DECL)
4687 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4689 /* ANSI specifies that a tentative definition which is not merged with
4690 a non-tentative definition behaves exactly like a definition with an
4691 initializer equal to zero. (Section 3.7.2)
4693 -fno-common gives strict ANSI behavior, though this tends to break
4694 a large body of code that grew up without this rule.
4696 Thread-local variables are never common, since there's no entrenched
4697 body of code to break, and it allows more efficient variable references
4698 in the presence of dynamic linking. */
4700 if (VAR_P (decl)
4701 && !initialized
4702 && TREE_PUBLIC (decl)
4703 && !DECL_THREAD_LOCAL_P (decl)
4704 && !flag_no_common)
4705 DECL_COMMON (decl) = 1;
4707 /* Set attributes here so if duplicate decl, will have proper attributes. */
4708 c_decl_attributes (&decl, attributes, 0);
4710 /* Handle gnu_inline attribute. */
4711 if (declspecs->inline_p
4712 && !flag_gnu89_inline
4713 && TREE_CODE (decl) == FUNCTION_DECL
4714 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4715 || current_function_decl))
4717 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4719 else if (declspecs->storage_class != csc_static)
4720 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4723 if (TREE_CODE (decl) == FUNCTION_DECL
4724 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4726 struct c_declarator *ce = declarator;
4728 if (ce->kind == cdk_pointer)
4729 ce = declarator->declarator;
4730 if (ce->kind == cdk_function)
4732 tree args = ce->u.arg_info->parms;
4733 for (; args; args = DECL_CHAIN (args))
4735 tree type = TREE_TYPE (args);
4736 if (type && INTEGRAL_TYPE_P (type)
4737 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4738 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4743 if (TREE_CODE (decl) == FUNCTION_DECL
4744 && DECL_DECLARED_INLINE_P (decl)
4745 && DECL_UNINLINABLE (decl)
4746 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4747 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4748 decl);
4750 /* C99 6.7.4p3: An inline definition of a function with external
4751 linkage shall not contain a definition of a modifiable object
4752 with static storage duration... */
4753 if (VAR_P (decl)
4754 && current_scope != file_scope
4755 && TREE_STATIC (decl)
4756 && !TREE_READONLY (decl)
4757 && DECL_DECLARED_INLINE_P (current_function_decl)
4758 && DECL_EXTERNAL (current_function_decl))
4759 record_inline_static (input_location, current_function_decl,
4760 decl, csi_modifiable);
4762 if (c_dialect_objc ()
4763 && VAR_OR_FUNCTION_DECL_P (decl))
4764 objc_check_global_decl (decl);
4766 /* Add this decl to the current scope.
4767 TEM may equal DECL or it may be a previous decl of the same name. */
4768 tem = pushdecl (decl);
4770 if (initialized && DECL_EXTERNAL (tem))
4772 DECL_EXTERNAL (tem) = 0;
4773 TREE_STATIC (tem) = 1;
4776 return tem;
4779 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4780 DECL or the non-array element type if DECL is an uninitialized array.
4781 If that type has a const member, diagnose this. */
4783 static void
4784 diagnose_uninitialized_cst_member (tree decl, tree type)
4786 tree field;
4787 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4789 tree field_type;
4790 if (TREE_CODE (field) != FIELD_DECL)
4791 continue;
4792 field_type = strip_array_types (TREE_TYPE (field));
4794 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4796 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4797 "uninitialized const member in %qT is invalid in C++",
4798 strip_array_types (TREE_TYPE (decl)));
4799 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4802 if (RECORD_OR_UNION_TYPE_P (field_type))
4803 diagnose_uninitialized_cst_member (decl, field_type);
4807 /* Finish processing of a declaration;
4808 install its initial value.
4809 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4810 If the length of an array type is not known before,
4811 it must be determined now, from the initial value, or it is an error.
4813 INIT_LOC is the location of the initial value. */
4815 void
4816 finish_decl (tree decl, location_t init_loc, tree init,
4817 tree origtype, tree asmspec_tree)
4819 tree type;
4820 bool was_incomplete = (DECL_SIZE (decl) == 0);
4821 const char *asmspec = 0;
4823 /* If a name was specified, get the string. */
4824 if (VAR_OR_FUNCTION_DECL_P (decl)
4825 && DECL_FILE_SCOPE_P (decl))
4826 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4827 if (asmspec_tree)
4828 asmspec = TREE_STRING_POINTER (asmspec_tree);
4830 if (VAR_P (decl)
4831 && TREE_STATIC (decl)
4832 && global_bindings_p ())
4833 /* So decl is a global variable. Record the types it uses
4834 so that we can decide later to emit debug info for them. */
4835 record_types_used_by_current_var_decl (decl);
4837 /* If `start_decl' didn't like having an initialization, ignore it now. */
4838 if (init != 0 && DECL_INITIAL (decl) == 0)
4839 init = 0;
4841 /* Don't crash if parm is initialized. */
4842 if (TREE_CODE (decl) == PARM_DECL)
4843 init = 0;
4845 if (init)
4846 store_init_value (init_loc, decl, init, origtype);
4848 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4849 || TREE_CODE (decl) == FIELD_DECL))
4850 objc_check_decl (decl);
4852 type = TREE_TYPE (decl);
4854 /* Deduce size of array from initialization, if not already known. */
4855 if (TREE_CODE (type) == ARRAY_TYPE
4856 && TYPE_DOMAIN (type) == 0
4857 && TREE_CODE (decl) != TYPE_DECL)
4859 bool do_default
4860 = (TREE_STATIC (decl)
4861 /* Even if pedantic, an external linkage array
4862 may have incomplete type at first. */
4863 ? pedantic && !TREE_PUBLIC (decl)
4864 : !DECL_EXTERNAL (decl));
4865 int failure
4866 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4867 do_default);
4869 /* Get the completed type made by complete_array_type. */
4870 type = TREE_TYPE (decl);
4872 switch (failure)
4874 case 1:
4875 error ("initializer fails to determine size of %q+D", decl);
4876 break;
4878 case 2:
4879 if (do_default)
4880 error ("array size missing in %q+D", decl);
4881 /* If a `static' var's size isn't known,
4882 make it extern as well as static, so it does not get
4883 allocated.
4884 If it is not `static', then do not mark extern;
4885 finish_incomplete_decl will give it a default size
4886 and it will get allocated. */
4887 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4888 DECL_EXTERNAL (decl) = 1;
4889 break;
4891 case 3:
4892 error ("zero or negative size array %q+D", decl);
4893 break;
4895 case 0:
4896 /* For global variables, update the copy of the type that
4897 exists in the binding. */
4898 if (TREE_PUBLIC (decl))
4900 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4901 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4902 b_ext = b_ext->shadowed;
4903 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4905 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4906 b_ext->u.type = composite_type (b_ext->u.type, type);
4907 else
4908 b_ext->u.type = type;
4911 break;
4913 default:
4914 gcc_unreachable ();
4917 if (DECL_INITIAL (decl))
4918 TREE_TYPE (DECL_INITIAL (decl)) = type;
4920 relayout_decl (decl);
4923 if (VAR_P (decl))
4925 if (init && TREE_CODE (init) == CONSTRUCTOR)
4926 add_flexible_array_elts_to_size (decl, init);
4928 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4929 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4930 layout_decl (decl, 0);
4932 if (DECL_SIZE (decl) == 0
4933 /* Don't give an error if we already gave one earlier. */
4934 && TREE_TYPE (decl) != error_mark_node
4935 && (TREE_STATIC (decl)
4936 /* A static variable with an incomplete type
4937 is an error if it is initialized.
4938 Also if it is not file scope.
4939 Otherwise, let it through, but if it is not `extern'
4940 then it may cause an error message later. */
4941 ? (DECL_INITIAL (decl) != 0
4942 || !DECL_FILE_SCOPE_P (decl))
4943 /* An automatic variable with an incomplete type
4944 is an error. */
4945 : !DECL_EXTERNAL (decl)))
4947 error ("storage size of %q+D isn%'t known", decl);
4948 TREE_TYPE (decl) = error_mark_node;
4951 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
4952 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
4953 && DECL_SIZE (decl) == NULL_TREE
4954 && TREE_STATIC (decl))
4955 incomplete_record_decls.safe_push (decl);
4957 if (is_global_var (decl) && DECL_SIZE (decl) != 0)
4959 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4960 constant_expression_warning (DECL_SIZE (decl));
4961 else
4963 error ("storage size of %q+D isn%'t constant", decl);
4964 TREE_TYPE (decl) = error_mark_node;
4968 if (TREE_USED (type))
4970 TREE_USED (decl) = 1;
4971 DECL_READ_P (decl) = 1;
4975 /* If this is a function and an assembler name is specified, reset DECL_RTL
4976 so we can give it its new name. Also, update builtin_decl if it
4977 was a normal built-in. */
4978 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4980 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4981 set_builtin_user_assembler_name (decl, asmspec);
4982 set_user_assembler_name (decl, asmspec);
4985 /* If #pragma weak was used, mark the decl weak now. */
4986 maybe_apply_pragma_weak (decl);
4988 /* Output the assembler code and/or RTL code for variables and functions,
4989 unless the type is an undefined structure or union.
4990 If not, it will get done when the type is completed. */
4992 if (VAR_OR_FUNCTION_DECL_P (decl))
4994 /* Determine the ELF visibility. */
4995 if (TREE_PUBLIC (decl))
4996 c_determine_visibility (decl);
4998 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4999 if (c_dialect_objc ())
5000 objc_check_decl (decl);
5002 if (asmspec)
5004 /* If this is not a static variable, issue a warning.
5005 It doesn't make any sense to give an ASMSPEC for an
5006 ordinary, non-register local variable. Historically,
5007 GCC has accepted -- but ignored -- the ASMSPEC in
5008 this case. */
5009 if (!DECL_FILE_SCOPE_P (decl)
5010 && VAR_P (decl)
5011 && !C_DECL_REGISTER (decl)
5012 && !TREE_STATIC (decl))
5013 warning (0, "ignoring asm-specifier for non-static local "
5014 "variable %q+D", decl);
5015 else
5016 set_user_assembler_name (decl, asmspec);
5019 if (DECL_FILE_SCOPE_P (decl))
5021 if (DECL_INITIAL (decl) == NULL_TREE
5022 || DECL_INITIAL (decl) == error_mark_node)
5023 /* Don't output anything
5024 when a tentative file-scope definition is seen.
5025 But at end of compilation, do output code for them. */
5026 DECL_DEFER_OUTPUT (decl) = 1;
5027 if (asmspec && C_DECL_REGISTER (decl))
5028 DECL_HARD_REGISTER (decl) = 1;
5029 rest_of_decl_compilation (decl, true, 0);
5031 else
5033 /* In conjunction with an ASMSPEC, the `register'
5034 keyword indicates that we should place the variable
5035 in a particular register. */
5036 if (asmspec && C_DECL_REGISTER (decl))
5038 DECL_HARD_REGISTER (decl) = 1;
5039 /* This cannot be done for a structure with volatile
5040 fields, on which DECL_REGISTER will have been
5041 reset. */
5042 if (!DECL_REGISTER (decl))
5043 error ("cannot put object with volatile field into register");
5046 if (TREE_CODE (decl) != FUNCTION_DECL)
5048 /* If we're building a variable sized type, and we might be
5049 reachable other than via the top of the current binding
5050 level, then create a new BIND_EXPR so that we deallocate
5051 the object at the right time. */
5052 /* Note that DECL_SIZE can be null due to errors. */
5053 if (DECL_SIZE (decl)
5054 && !TREE_CONSTANT (DECL_SIZE (decl))
5055 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5057 tree bind;
5058 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5059 TREE_SIDE_EFFECTS (bind) = 1;
5060 add_stmt (bind);
5061 BIND_EXPR_BODY (bind) = push_stmt_list ();
5063 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5064 DECL_EXPR, decl));
5069 if (!DECL_FILE_SCOPE_P (decl))
5071 /* Recompute the RTL of a local array now
5072 if it used to be an incomplete type. */
5073 if (was_incomplete && !is_global_var (decl))
5075 /* If we used it already as memory, it must stay in memory. */
5076 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5077 /* If it's still incomplete now, no init will save it. */
5078 if (DECL_SIZE (decl) == 0)
5079 DECL_INITIAL (decl) = 0;
5084 if (TREE_CODE (decl) == TYPE_DECL)
5086 if (!DECL_FILE_SCOPE_P (decl)
5087 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5088 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5090 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5093 /* Install a cleanup (aka destructor) if one was given. */
5094 if (VAR_P (decl) && !TREE_STATIC (decl))
5096 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5097 if (attr)
5099 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5100 tree cleanup_decl = lookup_name (cleanup_id);
5101 tree cleanup;
5102 vec<tree, va_gc> *v;
5104 /* Build "cleanup(&decl)" for the destructor. */
5105 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
5106 vec_alloc (v, 1);
5107 v->quick_push (cleanup);
5108 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5109 vNULL, cleanup_decl, v, NULL);
5110 vec_free (v);
5112 /* Don't warn about decl unused; the cleanup uses it. */
5113 TREE_USED (decl) = 1;
5114 TREE_USED (cleanup_decl) = 1;
5115 DECL_READ_P (decl) = 1;
5117 push_cleanup (decl, cleanup, false);
5121 if (warn_cxx_compat
5122 && VAR_P (decl)
5123 && !DECL_EXTERNAL (decl)
5124 && DECL_INITIAL (decl) == NULL_TREE)
5126 type = strip_array_types (type);
5127 if (TREE_READONLY (decl))
5128 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5129 "uninitialized const %qD is invalid in C++", decl);
5130 else if (RECORD_OR_UNION_TYPE_P (type)
5131 && C_TYPE_FIELDS_READONLY (type))
5132 diagnose_uninitialized_cst_member (decl, type);
5135 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5138 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5139 EXPR is NULL or a pointer to an expression that needs to be
5140 evaluated for the side effects of array size expressions in the
5141 parameters. */
5143 tree
5144 grokparm (const struct c_parm *parm, tree *expr)
5146 tree attrs = parm->attrs;
5147 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5148 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5150 decl_attributes (&decl, attrs, 0);
5152 return decl;
5155 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5156 and push that on the current scope. EXPR is a pointer to an
5157 expression that needs to be evaluated for the side effects of array
5158 size expressions in the parameters. */
5160 void
5161 push_parm_decl (const struct c_parm *parm, tree *expr)
5163 tree attrs = parm->attrs;
5164 tree decl;
5166 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5167 &attrs, expr, NULL, DEPRECATED_NORMAL);
5168 decl_attributes (&decl, attrs, 0);
5170 decl = pushdecl (decl);
5172 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5175 /* Mark all the parameter declarations to date as forward decls.
5176 Also diagnose use of this extension. */
5178 void
5179 mark_forward_parm_decls (void)
5181 struct c_binding *b;
5183 if (pedantic && !current_scope->warned_forward_parm_decls)
5185 pedwarn (input_location, OPT_Wpedantic,
5186 "ISO C forbids forward parameter declarations");
5187 current_scope->warned_forward_parm_decls = true;
5190 for (b = current_scope->bindings; b; b = b->prev)
5191 if (TREE_CODE (b->decl) == PARM_DECL)
5192 TREE_ASM_WRITTEN (b->decl) = 1;
5195 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5196 literal, which may be an incomplete array type completed by the
5197 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5198 literal. NON_CONST is true if the initializers contain something
5199 that cannot occur in a constant expression. */
5201 tree
5202 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5204 /* We do not use start_decl here because we have a type, not a declarator;
5205 and do not use finish_decl because the decl should be stored inside
5206 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5207 tree decl;
5208 tree complit;
5209 tree stmt;
5211 if (type == error_mark_node
5212 || init == error_mark_node)
5213 return error_mark_node;
5215 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5216 DECL_EXTERNAL (decl) = 0;
5217 TREE_PUBLIC (decl) = 0;
5218 TREE_STATIC (decl) = (current_scope == file_scope);
5219 DECL_CONTEXT (decl) = current_function_decl;
5220 TREE_USED (decl) = 1;
5221 DECL_READ_P (decl) = 1;
5222 TREE_TYPE (decl) = type;
5223 TREE_READONLY (decl) = (TYPE_READONLY (type)
5224 || (TREE_CODE (type) == ARRAY_TYPE
5225 && TYPE_READONLY (TREE_TYPE (type))));
5226 store_init_value (loc, decl, init, NULL_TREE);
5228 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5230 int failure = complete_array_type (&TREE_TYPE (decl),
5231 DECL_INITIAL (decl), true);
5232 /* If complete_array_type returns 3, it means that the
5233 initial value of the compound literal is empty. Allow it. */
5234 gcc_assert (failure == 0 || failure == 3);
5236 type = TREE_TYPE (decl);
5237 TREE_TYPE (DECL_INITIAL (decl)) = type;
5240 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5242 c_incomplete_type_error (loc, NULL_TREE, type);
5243 return error_mark_node;
5246 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5247 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5248 TREE_SIDE_EFFECTS (complit) = 1;
5250 layout_decl (decl, 0);
5252 if (TREE_STATIC (decl))
5254 /* This decl needs a name for the assembler output. */
5255 set_compound_literal_name (decl);
5256 DECL_DEFER_OUTPUT (decl) = 1;
5257 DECL_COMDAT (decl) = 1;
5258 DECL_ARTIFICIAL (decl) = 1;
5259 DECL_IGNORED_P (decl) = 1;
5260 pushdecl (decl);
5261 rest_of_decl_compilation (decl, 1, 0);
5264 if (non_const)
5266 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5267 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5270 return complit;
5273 /* Check the type of a compound literal. Here we just check that it
5274 is valid for C++. */
5276 void
5277 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5279 if (warn_cxx_compat
5280 && (type_name->specs->typespec_kind == ctsk_tagdef
5281 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5282 warning_at (loc, OPT_Wc___compat,
5283 "defining a type in a compound literal is invalid in C++");
5286 /* Determine whether TYPE is a structure with a flexible array member,
5287 or a union containing such a structure (possibly recursively). */
5289 static bool
5290 flexible_array_type_p (tree type)
5292 tree x;
5293 switch (TREE_CODE (type))
5295 case RECORD_TYPE:
5296 x = TYPE_FIELDS (type);
5297 if (x == NULL_TREE)
5298 return false;
5299 while (DECL_CHAIN (x) != NULL_TREE)
5300 x = DECL_CHAIN (x);
5301 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5302 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5303 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5304 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5305 return true;
5306 return false;
5307 case UNION_TYPE:
5308 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5310 if (flexible_array_type_p (TREE_TYPE (x)))
5311 return true;
5313 return false;
5314 default:
5315 return false;
5319 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5320 replacing with appropriate values if they are invalid. */
5322 static void
5323 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5324 tree orig_name)
5326 tree type_mv;
5327 unsigned int max_width;
5328 unsigned HOST_WIDE_INT w;
5329 const char *name = (orig_name
5330 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5331 : _("<anonymous>"));
5333 /* Detect and ignore out of range field width and process valid
5334 field widths. */
5335 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5337 error_at (loc, "bit-field %qs width not an integer constant", name);
5338 *width = integer_one_node;
5340 else
5342 if (TREE_CODE (*width) != INTEGER_CST)
5344 *width = c_fully_fold (*width, false, NULL);
5345 if (TREE_CODE (*width) == INTEGER_CST)
5346 pedwarn (loc, OPT_Wpedantic,
5347 "bit-field %qs width not an integer constant expression",
5348 name);
5350 if (TREE_CODE (*width) != INTEGER_CST)
5352 error_at (loc, "bit-field %qs width not an integer constant", name);
5353 *width = integer_one_node;
5355 constant_expression_warning (*width);
5356 if (tree_int_cst_sgn (*width) < 0)
5358 error_at (loc, "negative width in bit-field %qs", name);
5359 *width = integer_one_node;
5361 else if (integer_zerop (*width) && orig_name)
5363 error_at (loc, "zero width for bit-field %qs", name);
5364 *width = integer_one_node;
5368 /* Detect invalid bit-field type. */
5369 if (TREE_CODE (*type) != INTEGER_TYPE
5370 && TREE_CODE (*type) != BOOLEAN_TYPE
5371 && TREE_CODE (*type) != ENUMERAL_TYPE)
5373 error_at (loc, "bit-field %qs has invalid type", name);
5374 *type = unsigned_type_node;
5377 type_mv = TYPE_MAIN_VARIANT (*type);
5378 if (!in_system_header_at (input_location)
5379 && type_mv != integer_type_node
5380 && type_mv != unsigned_type_node
5381 && type_mv != boolean_type_node)
5382 pedwarn_c90 (loc, OPT_Wpedantic,
5383 "type of bit-field %qs is a GCC extension", name);
5385 max_width = TYPE_PRECISION (*type);
5387 if (0 < compare_tree_int (*width, max_width))
5389 error_at (loc, "width of %qs exceeds its type", name);
5390 w = max_width;
5391 *width = build_int_cst (integer_type_node, w);
5393 else
5394 w = tree_to_uhwi (*width);
5396 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5398 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5399 if (!lt
5400 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5401 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5402 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5408 /* Print warning about variable length array if necessary. */
5410 static void
5411 warn_variable_length_array (tree name, tree size)
5413 if (TREE_CONSTANT (size))
5415 if (name)
5416 pedwarn_c90 (input_location, OPT_Wvla,
5417 "ISO C90 forbids array %qE whose size "
5418 "can%'t be evaluated", name);
5419 else
5420 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5421 "whose size can%'t be evaluated");
5423 else
5425 if (name)
5426 pedwarn_c90 (input_location, OPT_Wvla,
5427 "ISO C90 forbids variable length array %qE", name);
5428 else
5429 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5430 "length array");
5434 /* Print warning about defaulting to int if necessary. */
5436 static void
5437 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5439 diagnostic_info diagnostic;
5440 va_list ap;
5441 rich_location richloc (line_table, location);
5443 va_start (ap, gmsgid);
5444 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5445 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5446 diagnostic.option_index = opt;
5447 report_diagnostic (&diagnostic);
5448 va_end (ap);
5451 /* Given declspecs and a declarator,
5452 determine the name and type of the object declared
5453 and construct a ..._DECL node for it.
5454 (In one case we can return a ..._TYPE node instead.
5455 For invalid input we sometimes return 0.)
5457 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5459 DECL_CONTEXT says which syntactic context this declaration is in:
5460 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5461 FUNCDEF for a function definition. Like NORMAL but a few different
5462 error messages in each case. Return value may be zero meaning
5463 this definition is too screwy to try to parse.
5464 PARM for a parameter declaration (either within a function prototype
5465 or before a function body). Make a PARM_DECL, or return void_type_node.
5466 TYPENAME if for a typename (in a cast or sizeof).
5467 Don't make a DECL node; just return the ..._TYPE node.
5468 FIELD for a struct or union field; make a FIELD_DECL.
5469 INITIALIZED is true if the decl has an initializer.
5470 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5471 representing the width of the bit-field.
5472 DECL_ATTRS points to the list of attributes that should be added to this
5473 decl. Any nested attributes that belong on the decl itself will be
5474 added to this list.
5475 If EXPR is not NULL, any expressions that need to be evaluated as
5476 part of evaluating variably modified types will be stored in *EXPR.
5477 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5478 set to indicate whether operands in *EXPR can be used in constant
5479 expressions.
5480 DEPRECATED_STATE is a deprecated_states value indicating whether
5481 deprecation warnings should be suppressed.
5483 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5484 It may also be so in the PARM case, for a prototype where the
5485 argument type is specified but not the name.
5487 This function is where the complicated C meanings of `static'
5488 and `extern' are interpreted. */
5490 static tree
5491 grokdeclarator (const struct c_declarator *declarator,
5492 struct c_declspecs *declspecs,
5493 enum decl_context decl_context, bool initialized, tree *width,
5494 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5495 enum deprecated_states deprecated_state)
5497 tree type = declspecs->type;
5498 bool threadp = declspecs->thread_p;
5499 enum c_storage_class storage_class = declspecs->storage_class;
5500 int constp;
5501 int restrictp;
5502 int volatilep;
5503 int atomicp;
5504 int type_quals = TYPE_UNQUALIFIED;
5505 tree name = NULL_TREE;
5506 bool funcdef_flag = false;
5507 bool funcdef_syntax = false;
5508 bool size_varies = false;
5509 tree decl_attr = declspecs->decl_attr;
5510 int array_ptr_quals = TYPE_UNQUALIFIED;
5511 tree array_ptr_attrs = NULL_TREE;
5512 int array_parm_static = 0;
5513 bool array_parm_vla_unspec_p = false;
5514 tree returned_attrs = NULL_TREE;
5515 bool bitfield = width != NULL;
5516 tree element_type;
5517 tree orig_qual_type = NULL;
5518 size_t orig_qual_indirect = 0;
5519 struct c_arg_info *arg_info = 0;
5520 addr_space_t as1, as2, address_space;
5521 location_t loc = UNKNOWN_LOCATION;
5522 tree expr_dummy;
5523 bool expr_const_operands_dummy;
5524 enum c_declarator_kind first_non_attr_kind;
5525 unsigned int alignas_align = 0;
5527 if (TREE_CODE (type) == ERROR_MARK)
5528 return error_mark_node;
5529 if (expr == NULL)
5530 expr = &expr_dummy;
5531 if (expr_const_operands == NULL)
5532 expr_const_operands = &expr_const_operands_dummy;
5534 *expr = declspecs->expr;
5535 *expr_const_operands = declspecs->expr_const_operands;
5537 if (decl_context == FUNCDEF)
5538 funcdef_flag = true, decl_context = NORMAL;
5540 /* Look inside a declarator for the name being declared
5541 and get it as an IDENTIFIER_NODE, for an error message. */
5543 const struct c_declarator *decl = declarator;
5545 first_non_attr_kind = cdk_attrs;
5546 while (decl)
5547 switch (decl->kind)
5549 case cdk_array:
5550 loc = decl->id_loc;
5551 /* FALL THRU. */
5553 case cdk_function:
5554 case cdk_pointer:
5555 funcdef_syntax = (decl->kind == cdk_function);
5556 if (first_non_attr_kind == cdk_attrs)
5557 first_non_attr_kind = decl->kind;
5558 decl = decl->declarator;
5559 break;
5561 case cdk_attrs:
5562 decl = decl->declarator;
5563 break;
5565 case cdk_id:
5566 loc = decl->id_loc;
5567 if (decl->u.id)
5568 name = decl->u.id;
5569 if (first_non_attr_kind == cdk_attrs)
5570 first_non_attr_kind = decl->kind;
5571 decl = 0;
5572 break;
5574 default:
5575 gcc_unreachable ();
5577 if (name == 0)
5579 gcc_assert (decl_context == PARM
5580 || decl_context == TYPENAME
5581 || (decl_context == FIELD
5582 && declarator->kind == cdk_id));
5583 gcc_assert (!initialized);
5587 /* A function definition's declarator must have the form of
5588 a function declarator. */
5590 if (funcdef_flag && !funcdef_syntax)
5591 return 0;
5593 /* If this looks like a function definition, make it one,
5594 even if it occurs where parms are expected.
5595 Then store_parm_decls will reject it and not use it as a parm. */
5596 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5597 decl_context = PARM;
5599 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5600 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5602 if ((decl_context == NORMAL || decl_context == FIELD)
5603 && current_scope == file_scope
5604 && variably_modified_type_p (type, NULL_TREE))
5606 if (name)
5607 error_at (loc, "variably modified %qE at file scope", name);
5608 else
5609 error_at (loc, "variably modified field at file scope");
5610 type = integer_type_node;
5613 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5615 /* Diagnose defaulting to "int". */
5617 if (declspecs->default_int_p && !in_system_header_at (input_location))
5619 /* Issue a warning if this is an ISO C 99 program or if
5620 -Wreturn-type and this is a function, or if -Wimplicit;
5621 prefer the former warning since it is more explicit. */
5622 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5623 && funcdef_flag)
5624 warn_about_return_type = 1;
5625 else
5627 if (name)
5628 warn_defaults_to (loc, OPT_Wimplicit_int,
5629 "type defaults to %<int%> in declaration "
5630 "of %qE", name);
5631 else
5632 warn_defaults_to (loc, OPT_Wimplicit_int,
5633 "type defaults to %<int%> in type name");
5637 /* Adjust the type if a bit-field is being declared,
5638 -funsigned-bitfields applied and the type is not explicitly
5639 "signed". */
5640 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5641 && TREE_CODE (type) == INTEGER_TYPE)
5642 type = unsigned_type_for (type);
5644 /* Figure out the type qualifiers for the declaration. There are
5645 two ways a declaration can become qualified. One is something
5646 like `const int i' where the `const' is explicit. Another is
5647 something like `typedef const int CI; CI i' where the type of the
5648 declaration contains the `const'. A third possibility is that
5649 there is a type qualifier on the element type of a typedefed
5650 array type, in which case we should extract that qualifier so
5651 that c_apply_type_quals_to_decl receives the full list of
5652 qualifiers to work with (C90 is not entirely clear about whether
5653 duplicate qualifiers should be diagnosed in this case, but it
5654 seems most appropriate to do so). */
5655 element_type = strip_array_types (type);
5656 constp = declspecs->const_p + TYPE_READONLY (element_type);
5657 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5658 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5659 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5660 as1 = declspecs->address_space;
5661 as2 = TYPE_ADDR_SPACE (element_type);
5662 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5664 if (constp > 1)
5665 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5666 if (restrictp > 1)
5667 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5668 if (volatilep > 1)
5669 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5670 if (atomicp > 1)
5671 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5673 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5674 error_at (loc, "conflicting named address spaces (%s vs %s)",
5675 c_addr_space_name (as1), c_addr_space_name (as2));
5677 if ((TREE_CODE (type) == ARRAY_TYPE
5678 || first_non_attr_kind == cdk_array)
5679 && TYPE_QUALS (element_type))
5681 orig_qual_type = type;
5682 type = TYPE_MAIN_VARIANT (type);
5684 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5685 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5686 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5687 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5688 | ENCODE_QUAL_ADDR_SPACE (address_space));
5689 if (type_quals != TYPE_QUALS (element_type))
5690 orig_qual_type = NULL_TREE;
5692 /* Applying the _Atomic qualifier to an array type (through the use
5693 of typedefs or typeof) must be detected here. If the qualifier
5694 is introduced later, any appearance of applying it to an array is
5695 actually applying it to an element of that array. */
5696 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5697 error_at (loc, "%<_Atomic%>-qualified array type");
5699 /* Warn about storage classes that are invalid for certain
5700 kinds of declarations (parameters, typenames, etc.). */
5702 if (funcdef_flag
5703 && (threadp
5704 || storage_class == csc_auto
5705 || storage_class == csc_register
5706 || storage_class == csc_typedef))
5708 if (storage_class == csc_auto)
5709 pedwarn (loc,
5710 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5711 "function definition declared %<auto%>");
5712 if (storage_class == csc_register)
5713 error_at (loc, "function definition declared %<register%>");
5714 if (storage_class == csc_typedef)
5715 error_at (loc, "function definition declared %<typedef%>");
5716 if (threadp)
5717 error_at (loc, "function definition declared %qs",
5718 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5719 threadp = false;
5720 if (storage_class == csc_auto
5721 || storage_class == csc_register
5722 || storage_class == csc_typedef)
5723 storage_class = csc_none;
5725 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5727 if (decl_context == PARM && storage_class == csc_register)
5729 else
5731 switch (decl_context)
5733 case FIELD:
5734 if (name)
5735 error_at (loc, "storage class specified for structure "
5736 "field %qE", name);
5737 else
5738 error_at (loc, "storage class specified for structure field");
5739 break;
5740 case PARM:
5741 if (name)
5742 error_at (loc, "storage class specified for parameter %qE",
5743 name);
5744 else
5745 error_at (loc, "storage class specified for unnamed parameter");
5746 break;
5747 default:
5748 error_at (loc, "storage class specified for typename");
5749 break;
5751 storage_class = csc_none;
5752 threadp = false;
5755 else if (storage_class == csc_extern
5756 && initialized
5757 && !funcdef_flag)
5759 /* 'extern' with initialization is invalid if not at file scope. */
5760 if (current_scope == file_scope)
5762 /* It is fine to have 'extern const' when compiling at C
5763 and C++ intersection. */
5764 if (!(warn_cxx_compat && constp))
5765 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5766 name);
5768 else
5769 error_at (loc, "%qE has both %<extern%> and initializer", name);
5771 else if (current_scope == file_scope)
5773 if (storage_class == csc_auto)
5774 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5775 name);
5776 if (pedantic && storage_class == csc_register)
5777 pedwarn (input_location, OPT_Wpedantic,
5778 "file-scope declaration of %qE specifies %<register%>", name);
5780 else
5782 if (storage_class == csc_extern && funcdef_flag)
5783 error_at (loc, "nested function %qE declared %<extern%>", name);
5784 else if (threadp && storage_class == csc_none)
5786 error_at (loc, "function-scope %qE implicitly auto and declared "
5787 "%qs", name,
5788 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5789 threadp = false;
5793 /* Now figure out the structure of the declarator proper.
5794 Descend through it, creating more complex types, until we reach
5795 the declared identifier (or NULL_TREE, in an absolute declarator).
5796 At each stage we maintain an unqualified version of the type
5797 together with any qualifiers that should be applied to it with
5798 c_build_qualified_type; this way, array types including
5799 multidimensional array types are first built up in unqualified
5800 form and then the qualified form is created with
5801 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5803 while (declarator && declarator->kind != cdk_id)
5805 if (type == error_mark_node)
5807 declarator = declarator->declarator;
5808 continue;
5811 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5812 a cdk_pointer (for *...),
5813 a cdk_function (for ...(...)),
5814 a cdk_attrs (for nested attributes),
5815 or a cdk_id (for the name being declared
5816 or the place in an absolute declarator
5817 where the name was omitted).
5818 For the last case, we have just exited the loop.
5820 At this point, TYPE is the type of elements of an array,
5821 or for a function to return, or for a pointer to point to.
5822 After this sequence of ifs, TYPE is the type of the
5823 array or function or pointer, and DECLARATOR has had its
5824 outermost layer removed. */
5826 if (array_ptr_quals != TYPE_UNQUALIFIED
5827 || array_ptr_attrs != NULL_TREE
5828 || array_parm_static)
5830 /* Only the innermost declarator (making a parameter be of
5831 array type which is converted to pointer type)
5832 may have static or type qualifiers. */
5833 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5834 array_ptr_quals = TYPE_UNQUALIFIED;
5835 array_ptr_attrs = NULL_TREE;
5836 array_parm_static = 0;
5839 switch (declarator->kind)
5841 case cdk_attrs:
5843 /* A declarator with embedded attributes. */
5844 tree attrs = declarator->u.attrs;
5845 const struct c_declarator *inner_decl;
5846 int attr_flags = 0;
5847 declarator = declarator->declarator;
5848 inner_decl = declarator;
5849 while (inner_decl->kind == cdk_attrs)
5850 inner_decl = inner_decl->declarator;
5851 if (inner_decl->kind == cdk_id)
5852 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5853 else if (inner_decl->kind == cdk_function)
5854 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5855 else if (inner_decl->kind == cdk_array)
5856 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5857 returned_attrs = decl_attributes (&type,
5858 chainon (returned_attrs, attrs),
5859 attr_flags);
5860 break;
5862 case cdk_array:
5864 tree itype = NULL_TREE;
5865 tree size = declarator->u.array.dimen;
5866 /* The index is a signed object `sizetype' bits wide. */
5867 tree index_type = c_common_signed_type (sizetype);
5869 array_ptr_quals = declarator->u.array.quals;
5870 array_ptr_attrs = declarator->u.array.attrs;
5871 array_parm_static = declarator->u.array.static_p;
5872 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5874 declarator = declarator->declarator;
5876 /* Check for some types that there cannot be arrays of. */
5878 if (VOID_TYPE_P (type))
5880 if (name)
5881 error_at (loc, "declaration of %qE as array of voids", name);
5882 else
5883 error_at (loc, "declaration of type name as array of voids");
5884 type = error_mark_node;
5887 if (TREE_CODE (type) == FUNCTION_TYPE)
5889 if (name)
5890 error_at (loc, "declaration of %qE as array of functions",
5891 name);
5892 else
5893 error_at (loc, "declaration of type name as array of "
5894 "functions");
5895 type = error_mark_node;
5898 if (pedantic && !in_system_header_at (input_location)
5899 && flexible_array_type_p (type))
5900 pedwarn (loc, OPT_Wpedantic,
5901 "invalid use of structure with flexible array member");
5903 if (size == error_mark_node)
5904 type = error_mark_node;
5906 if (type == error_mark_node)
5907 continue;
5909 /* If size was specified, set ITYPE to a range-type for
5910 that size. Otherwise, ITYPE remains null. finish_decl
5911 may figure it out from an initial value. */
5913 if (size)
5915 bool size_maybe_const = true;
5916 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5917 && !TREE_OVERFLOW (size));
5918 bool this_size_varies = false;
5920 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5921 lvalue. */
5922 STRIP_TYPE_NOPS (size);
5924 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5926 if (name)
5927 error_at (loc, "size of array %qE has non-integer type",
5928 name);
5929 else
5930 error_at (loc,
5931 "size of unnamed array has non-integer type");
5932 size = integer_one_node;
5934 /* This can happen with enum forward declaration. */
5935 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
5937 if (name)
5938 error_at (loc, "size of array %qE has incomplete type",
5939 name);
5940 else
5941 error_at (loc, "size of unnamed array has incomplete "
5942 "type");
5943 size = integer_one_node;
5946 size = c_fully_fold (size, false, &size_maybe_const);
5948 if (pedantic && size_maybe_const && integer_zerop (size))
5950 if (name)
5951 pedwarn (loc, OPT_Wpedantic,
5952 "ISO C forbids zero-size array %qE", name);
5953 else
5954 pedwarn (loc, OPT_Wpedantic,
5955 "ISO C forbids zero-size array");
5958 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5960 constant_expression_warning (size);
5961 if (tree_int_cst_sgn (size) < 0)
5963 if (name)
5964 error_at (loc, "size of array %qE is negative", name);
5965 else
5966 error_at (loc, "size of unnamed array is negative");
5967 size = integer_one_node;
5969 /* Handle a size folded to an integer constant but
5970 not an integer constant expression. */
5971 if (!size_int_const)
5973 /* If this is a file scope declaration of an
5974 ordinary identifier, this is invalid code;
5975 diagnosing it here and not subsequently
5976 treating the type as variable-length avoids
5977 more confusing diagnostics later. */
5978 if ((decl_context == NORMAL || decl_context == FIELD)
5979 && current_scope == file_scope)
5980 pedwarn (input_location, 0,
5981 "variably modified %qE at file scope",
5982 name);
5983 else
5984 this_size_varies = size_varies = true;
5985 warn_variable_length_array (name, size);
5988 else if ((decl_context == NORMAL || decl_context == FIELD)
5989 && current_scope == file_scope)
5991 error_at (loc, "variably modified %qE at file scope", name);
5992 size = integer_one_node;
5994 else
5996 /* Make sure the array size remains visibly
5997 nonconstant even if it is (eg) a const variable
5998 with known value. */
5999 this_size_varies = size_varies = true;
6000 warn_variable_length_array (name, size);
6001 if (flag_sanitize & SANITIZE_VLA
6002 && decl_context == NORMAL
6003 && do_ubsan_in_current_function ())
6005 /* Evaluate the array size only once. */
6006 size = c_save_expr (size);
6007 size = c_fully_fold (size, false, NULL);
6008 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6009 ubsan_instrument_vla (loc, size),
6010 size);
6014 if (integer_zerop (size) && !this_size_varies)
6016 /* A zero-length array cannot be represented with
6017 an unsigned index type, which is what we'll
6018 get with build_index_type. Create an
6019 open-ended range instead. */
6020 itype = build_range_type (sizetype, size, NULL_TREE);
6022 else
6024 /* Arrange for the SAVE_EXPR on the inside of the
6025 MINUS_EXPR, which allows the -1 to get folded
6026 with the +1 that happens when building TYPE_SIZE. */
6027 if (size_varies)
6028 size = save_expr (size);
6029 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6030 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6031 integer_zero_node, size);
6033 /* Compute the maximum valid index, that is, size
6034 - 1. Do the calculation in index_type, so that
6035 if it is a variable the computations will be
6036 done in the proper mode. */
6037 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6038 convert (index_type, size),
6039 convert (index_type,
6040 size_one_node));
6042 /* The above overflows when size does not fit
6043 in index_type.
6044 ??? While a size of INT_MAX+1 technically shouldn't
6045 cause an overflow (because we subtract 1), handling
6046 this case seems like an unnecessary complication. */
6047 if (TREE_CODE (size) == INTEGER_CST
6048 && !int_fits_type_p (size, index_type))
6050 if (name)
6051 error_at (loc, "size of array %qE is too large",
6052 name);
6053 else
6054 error_at (loc, "size of unnamed array is too large");
6055 type = error_mark_node;
6056 continue;
6059 itype = build_index_type (itype);
6061 if (this_size_varies)
6063 if (*expr)
6064 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6065 *expr, size);
6066 else
6067 *expr = size;
6068 *expr_const_operands &= size_maybe_const;
6071 else if (decl_context == FIELD)
6073 bool flexible_array_member = false;
6074 if (array_parm_vla_unspec_p)
6075 /* Field names can in fact have function prototype
6076 scope so [*] is disallowed here through making
6077 the field variably modified, not through being
6078 something other than a declaration with function
6079 prototype scope. */
6080 size_varies = true;
6081 else
6083 const struct c_declarator *t = declarator;
6084 while (t->kind == cdk_attrs)
6085 t = t->declarator;
6086 flexible_array_member = (t->kind == cdk_id);
6088 if (flexible_array_member
6089 && !in_system_header_at (input_location))
6090 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6091 "support flexible array members");
6093 /* ISO C99 Flexible array members are effectively
6094 identical to GCC's zero-length array extension. */
6095 if (flexible_array_member || array_parm_vla_unspec_p)
6096 itype = build_range_type (sizetype, size_zero_node,
6097 NULL_TREE);
6099 else if (decl_context == PARM)
6101 if (array_parm_vla_unspec_p)
6103 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6104 size_varies = true;
6107 else if (decl_context == TYPENAME)
6109 if (array_parm_vla_unspec_p)
6111 /* C99 6.7.5.2p4 */
6112 warning (0, "%<[*]%> not in a declaration");
6113 /* We use this to avoid messing up with incomplete
6114 array types of the same type, that would
6115 otherwise be modified below. */
6116 itype = build_range_type (sizetype, size_zero_node,
6117 NULL_TREE);
6118 size_varies = true;
6122 /* Complain about arrays of incomplete types. */
6123 if (!COMPLETE_TYPE_P (type))
6125 error_at (loc, "array type has incomplete element type %qT",
6126 type);
6127 /* See if we can be more helpful. */
6128 if (TREE_CODE (type) == ARRAY_TYPE)
6130 if (name)
6131 inform (loc, "declaration of %qE as multidimensional "
6132 "array must have bounds for all dimensions "
6133 "except the first", name);
6134 else
6135 inform (loc, "declaration of multidimensional array "
6136 "must have bounds for all dimensions except "
6137 "the first");
6139 type = error_mark_node;
6141 else
6142 /* When itype is NULL, a shared incomplete array type is
6143 returned for all array of a given type. Elsewhere we
6144 make sure we don't complete that type before copying
6145 it, but here we want to make sure we don't ever
6146 modify the shared type, so we gcc_assert (itype)
6147 below. */
6149 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6150 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6151 type = build_qualified_type (type,
6152 ENCODE_QUAL_ADDR_SPACE (as));
6154 type = build_array_type (type, itype);
6157 if (type != error_mark_node)
6159 if (size_varies)
6161 /* It is ok to modify type here even if itype is
6162 NULL: if size_varies, we're in a
6163 multi-dimensional array and the inner type has
6164 variable size, so the enclosing shared array type
6165 must too. */
6166 if (size && TREE_CODE (size) == INTEGER_CST)
6167 type
6168 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6169 C_TYPE_VARIABLE_SIZE (type) = 1;
6172 /* The GCC extension for zero-length arrays differs from
6173 ISO flexible array members in that sizeof yields
6174 zero. */
6175 if (size && integer_zerop (size))
6177 gcc_assert (itype);
6178 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6179 TYPE_SIZE (type) = bitsize_zero_node;
6180 TYPE_SIZE_UNIT (type) = size_zero_node;
6181 SET_TYPE_STRUCTURAL_EQUALITY (type);
6183 if (array_parm_vla_unspec_p)
6185 gcc_assert (itype);
6186 /* The type is complete. C99 6.7.5.2p4 */
6187 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6188 TYPE_SIZE (type) = bitsize_zero_node;
6189 TYPE_SIZE_UNIT (type) = size_zero_node;
6190 SET_TYPE_STRUCTURAL_EQUALITY (type);
6193 if (!valid_array_size_p (loc, type, name))
6194 type = error_mark_node;
6197 if (decl_context != PARM
6198 && (array_ptr_quals != TYPE_UNQUALIFIED
6199 || array_ptr_attrs != NULL_TREE
6200 || array_parm_static))
6202 error_at (loc, "static or type qualifiers in non-parameter "
6203 "array declarator");
6204 array_ptr_quals = TYPE_UNQUALIFIED;
6205 array_ptr_attrs = NULL_TREE;
6206 array_parm_static = 0;
6208 orig_qual_indirect++;
6209 break;
6211 case cdk_function:
6213 /* Say it's a definition only for the declarator closest
6214 to the identifier, apart possibly from some
6215 attributes. */
6216 bool really_funcdef = false;
6217 tree arg_types;
6218 orig_qual_type = NULL_TREE;
6219 if (funcdef_flag)
6221 const struct c_declarator *t = declarator->declarator;
6222 while (t->kind == cdk_attrs)
6223 t = t->declarator;
6224 really_funcdef = (t->kind == cdk_id);
6227 /* Declaring a function type. Make sure we have a valid
6228 type for the function to return. */
6229 if (type == error_mark_node)
6230 continue;
6232 size_varies = false;
6234 /* Warn about some types functions can't return. */
6235 if (TREE_CODE (type) == FUNCTION_TYPE)
6237 if (name)
6238 error_at (loc, "%qE declared as function returning a "
6239 "function", name);
6240 else
6241 error_at (loc, "type name declared as function "
6242 "returning a function");
6243 type = integer_type_node;
6245 if (TREE_CODE (type) == ARRAY_TYPE)
6247 if (name)
6248 error_at (loc, "%qE declared as function returning an array",
6249 name);
6250 else
6251 error_at (loc, "type name declared as function returning "
6252 "an array");
6253 type = integer_type_node;
6256 /* Construct the function type and go to the next
6257 inner layer of declarator. */
6258 arg_info = declarator->u.arg_info;
6259 arg_types = grokparms (arg_info, really_funcdef);
6261 /* Type qualifiers before the return type of the function
6262 qualify the return type, not the function type. */
6263 if (type_quals)
6265 int quals_used = type_quals;
6266 /* Type qualifiers on a function return type are
6267 normally permitted by the standard but have no
6268 effect, so give a warning at -Wreturn-type.
6269 Qualifiers on a void return type are banned on
6270 function definitions in ISO C; GCC used to used
6271 them for noreturn functions. The resolution of C11
6272 DR#423 means qualifiers (other than _Atomic) are
6273 actually removed from the return type when
6274 determining the function type. */
6275 if (flag_isoc11)
6276 quals_used &= TYPE_QUAL_ATOMIC;
6277 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6278 pedwarn (loc, 0,
6279 "function definition has qualified void return type");
6280 else
6281 warning_at (loc, OPT_Wignored_qualifiers,
6282 "type qualifiers ignored on function return type");
6284 /* Ensure an error for restrict on invalid types; the
6285 DR#423 resolution is not entirely clear about
6286 this. */
6287 if (flag_isoc11
6288 && (type_quals & TYPE_QUAL_RESTRICT)
6289 && (!POINTER_TYPE_P (type)
6290 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6291 error_at (loc, "invalid use of %<restrict%>");
6292 if (quals_used)
6293 type = c_build_qualified_type (type, quals_used);
6295 type_quals = TYPE_UNQUALIFIED;
6297 type = build_function_type (type, arg_types);
6298 declarator = declarator->declarator;
6300 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6301 the formal parameter list of this FUNCTION_TYPE to point to
6302 the FUNCTION_TYPE node itself. */
6304 c_arg_tag *tag;
6305 unsigned ix;
6307 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6308 TYPE_CONTEXT (tag->type) = type;
6310 break;
6312 case cdk_pointer:
6314 /* Merge any constancy or volatility into the target type
6315 for the pointer. */
6316 if ((type_quals & TYPE_QUAL_ATOMIC)
6317 && TREE_CODE (type) == FUNCTION_TYPE)
6319 error_at (loc,
6320 "%<_Atomic%>-qualified function type");
6321 type_quals &= ~TYPE_QUAL_ATOMIC;
6323 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6324 && type_quals)
6325 pedwarn (loc, OPT_Wpedantic,
6326 "ISO C forbids qualified function types");
6327 if (type_quals)
6328 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6329 orig_qual_indirect);
6330 orig_qual_type = NULL_TREE;
6331 size_varies = false;
6333 /* When the pointed-to type involves components of variable size,
6334 care must be taken to ensure that the size evaluation code is
6335 emitted early enough to dominate all the possible later uses
6336 and late enough for the variables on which it depends to have
6337 been assigned.
6339 This is expected to happen automatically when the pointed-to
6340 type has a name/declaration of it's own, but special attention
6341 is required if the type is anonymous.
6343 We handle the NORMAL and FIELD contexts here by attaching an
6344 artificial TYPE_DECL to such pointed-to type. This forces the
6345 sizes evaluation at a safe point and ensures it is not deferred
6346 until e.g. within a deeper conditional context.
6348 We expect nothing to be needed here for PARM or TYPENAME.
6349 Pushing a TYPE_DECL at this point for TYPENAME would actually
6350 be incorrect, as we might be in the middle of an expression
6351 with side effects on the pointed-to type size "arguments" prior
6352 to the pointer declaration point and the fake TYPE_DECL in the
6353 enclosing context would force the size evaluation prior to the
6354 side effects. */
6356 if (!TYPE_NAME (type)
6357 && (decl_context == NORMAL || decl_context == FIELD)
6358 && variably_modified_type_p (type, NULL_TREE))
6360 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6361 DECL_ARTIFICIAL (decl) = 1;
6362 pushdecl (decl);
6363 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6364 TYPE_NAME (type) = decl;
6367 type = c_build_pointer_type (type);
6369 /* Process type qualifiers (such as const or volatile)
6370 that were given inside the `*'. */
6371 type_quals = declarator->u.pointer_quals;
6373 declarator = declarator->declarator;
6374 break;
6376 default:
6377 gcc_unreachable ();
6380 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6382 /* Now TYPE has the actual type, apart from any qualifiers in
6383 TYPE_QUALS. */
6385 /* Warn about address space used for things other than static memory or
6386 pointers. */
6387 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6388 if (!ADDR_SPACE_GENERIC_P (address_space))
6390 if (decl_context == NORMAL)
6392 switch (storage_class)
6394 case csc_auto:
6395 error ("%qs combined with %<auto%> qualifier for %qE",
6396 c_addr_space_name (address_space), name);
6397 break;
6398 case csc_register:
6399 error ("%qs combined with %<register%> qualifier for %qE",
6400 c_addr_space_name (address_space), name);
6401 break;
6402 case csc_none:
6403 if (current_function_scope)
6405 error ("%qs specified for auto variable %qE",
6406 c_addr_space_name (address_space), name);
6407 break;
6409 break;
6410 case csc_static:
6411 case csc_extern:
6412 case csc_typedef:
6413 break;
6414 default:
6415 gcc_unreachable ();
6418 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6420 if (name)
6421 error ("%qs specified for parameter %qE",
6422 c_addr_space_name (address_space), name);
6423 else
6424 error ("%qs specified for unnamed parameter",
6425 c_addr_space_name (address_space));
6427 else if (decl_context == FIELD)
6429 if (name)
6430 error ("%qs specified for structure field %qE",
6431 c_addr_space_name (address_space), name);
6432 else
6433 error ("%qs specified for structure field",
6434 c_addr_space_name (address_space));
6438 /* Check the type and width of a bit-field. */
6439 if (bitfield)
6441 check_bitfield_type_and_width (loc, &type, width, name);
6442 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6443 atomic types are permitted for bit-fields; we have no code to
6444 make bit-field accesses atomic, so disallow them. */
6445 if (type_quals & TYPE_QUAL_ATOMIC)
6447 if (name)
6448 error_at (loc, "bit-field %qE has atomic type", name);
6449 else
6450 error_at (loc, "bit-field has atomic type");
6451 type_quals &= ~TYPE_QUAL_ATOMIC;
6455 /* Reject invalid uses of _Alignas. */
6456 if (declspecs->alignas_p)
6458 if (storage_class == csc_typedef)
6459 error_at (loc, "alignment specified for typedef %qE", name);
6460 else if (storage_class == csc_register)
6461 error_at (loc, "alignment specified for %<register%> object %qE",
6462 name);
6463 else if (decl_context == PARM)
6465 if (name)
6466 error_at (loc, "alignment specified for parameter %qE", name);
6467 else
6468 error_at (loc, "alignment specified for unnamed parameter");
6470 else if (bitfield)
6472 if (name)
6473 error_at (loc, "alignment specified for bit-field %qE", name);
6474 else
6475 error_at (loc, "alignment specified for unnamed bit-field");
6477 else if (TREE_CODE (type) == FUNCTION_TYPE)
6478 error_at (loc, "alignment specified for function %qE", name);
6479 else if (declspecs->align_log != -1 && TYPE_P (type))
6481 alignas_align = 1U << declspecs->align_log;
6482 if (alignas_align < min_align_of_type (type))
6484 if (name)
6485 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6486 "alignment of %qE", name);
6487 else
6488 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6489 "alignment of unnamed field");
6490 alignas_align = 0;
6495 /* If this is declaring a typedef name, return a TYPE_DECL. */
6497 if (storage_class == csc_typedef)
6499 tree decl;
6500 if ((type_quals & TYPE_QUAL_ATOMIC)
6501 && TREE_CODE (type) == FUNCTION_TYPE)
6503 error_at (loc,
6504 "%<_Atomic%>-qualified function type");
6505 type_quals &= ~TYPE_QUAL_ATOMIC;
6507 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6508 && type_quals)
6509 pedwarn (loc, OPT_Wpedantic,
6510 "ISO C forbids qualified function types");
6511 if (type_quals)
6512 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6513 orig_qual_indirect);
6514 decl = build_decl (declarator->id_loc,
6515 TYPE_DECL, declarator->u.id, type);
6516 if (declspecs->explicit_signed_p)
6517 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6518 if (declspecs->inline_p)
6519 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6520 if (declspecs->noreturn_p)
6521 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6523 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6525 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6527 if (b != NULL
6528 && b->decl != NULL_TREE
6529 && (B_IN_CURRENT_SCOPE (b)
6530 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6531 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6533 warning_at (declarator->id_loc, OPT_Wc___compat,
6534 ("using %qD as both a typedef and a tag is "
6535 "invalid in C++"),
6536 decl);
6537 if (b->locus != UNKNOWN_LOCATION)
6538 inform (b->locus, "originally defined here");
6542 return decl;
6545 /* If this is a type name (such as, in a cast or sizeof),
6546 compute the type and return it now. */
6548 if (decl_context == TYPENAME)
6550 /* Note that the grammar rejects storage classes in typenames
6551 and fields. */
6552 gcc_assert (storage_class == csc_none && !threadp
6553 && !declspecs->inline_p && !declspecs->noreturn_p);
6554 if ((type_quals & TYPE_QUAL_ATOMIC)
6555 && TREE_CODE (type) == FUNCTION_TYPE)
6557 error_at (loc,
6558 "%<_Atomic%>-qualified function type");
6559 type_quals &= ~TYPE_QUAL_ATOMIC;
6561 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6562 && type_quals)
6563 pedwarn (loc, OPT_Wpedantic,
6564 "ISO C forbids const or volatile function types");
6565 if (type_quals)
6566 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6567 orig_qual_indirect);
6568 return type;
6571 if (pedantic && decl_context == FIELD
6572 && variably_modified_type_p (type, NULL_TREE))
6574 /* C99 6.7.2.1p8 */
6575 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6576 "have a variably modified type");
6579 /* Aside from typedefs and type names (handle above),
6580 `void' at top level (not within pointer)
6581 is allowed only in public variables.
6582 We don't complain about parms either, but that is because
6583 a better error message can be made later. */
6585 if (VOID_TYPE_P (type) && decl_context != PARM
6586 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6587 && (storage_class == csc_extern
6588 || (current_scope == file_scope
6589 && !(storage_class == csc_static
6590 || storage_class == csc_register)))))
6592 error_at (loc, "variable or field %qE declared void", name);
6593 type = integer_type_node;
6596 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6597 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6600 tree decl;
6602 if (decl_context == PARM)
6604 tree promoted_type;
6605 bool array_parameter_p = false;
6607 /* A parameter declared as an array of T is really a pointer to T.
6608 One declared as a function is really a pointer to a function. */
6610 if (TREE_CODE (type) == ARRAY_TYPE)
6612 /* Transfer const-ness of array into that of type pointed to. */
6613 type = TREE_TYPE (type);
6614 if (orig_qual_type != NULL_TREE)
6616 if (orig_qual_indirect == 0)
6617 orig_qual_type = TREE_TYPE (orig_qual_type);
6618 else
6619 orig_qual_indirect--;
6621 if (type_quals)
6622 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6623 orig_qual_indirect);
6624 type = c_build_pointer_type (type);
6625 type_quals = array_ptr_quals;
6626 if (type_quals)
6627 type = c_build_qualified_type (type, type_quals);
6629 /* We don't yet implement attributes in this context. */
6630 if (array_ptr_attrs != NULL_TREE)
6631 warning_at (loc, OPT_Wattributes,
6632 "attributes in parameter array declarator ignored");
6634 size_varies = false;
6635 array_parameter_p = true;
6637 else if (TREE_CODE (type) == FUNCTION_TYPE)
6639 if (type_quals & TYPE_QUAL_ATOMIC)
6641 error_at (loc,
6642 "%<_Atomic%>-qualified function type");
6643 type_quals &= ~TYPE_QUAL_ATOMIC;
6645 else if (type_quals)
6646 pedwarn (loc, OPT_Wpedantic,
6647 "ISO C forbids qualified function types");
6648 if (type_quals)
6649 type = c_build_qualified_type (type, type_quals);
6650 type = c_build_pointer_type (type);
6651 type_quals = TYPE_UNQUALIFIED;
6653 else if (type_quals)
6654 type = c_build_qualified_type (type, type_quals);
6656 decl = build_decl (declarator->id_loc,
6657 PARM_DECL, declarator->u.id, type);
6658 if (size_varies)
6659 C_DECL_VARIABLE_SIZE (decl) = 1;
6660 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6662 /* Compute the type actually passed in the parmlist,
6663 for the case where there is no prototype.
6664 (For example, shorts and chars are passed as ints.)
6665 When there is a prototype, this is overridden later. */
6667 if (type == error_mark_node)
6668 promoted_type = type;
6669 else
6670 promoted_type = c_type_promotes_to (type);
6672 DECL_ARG_TYPE (decl) = promoted_type;
6673 if (declspecs->inline_p)
6674 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6675 if (declspecs->noreturn_p)
6676 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6678 else if (decl_context == FIELD)
6680 /* Note that the grammar rejects storage classes in typenames
6681 and fields. */
6682 gcc_assert (storage_class == csc_none && !threadp
6683 && !declspecs->inline_p && !declspecs->noreturn_p);
6685 /* Structure field. It may not be a function. */
6687 if (TREE_CODE (type) == FUNCTION_TYPE)
6689 error_at (loc, "field %qE declared as a function", name);
6690 type = build_pointer_type (type);
6692 else if (TREE_CODE (type) != ERROR_MARK
6693 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6695 if (name)
6696 error_at (loc, "field %qE has incomplete type", name);
6697 else
6698 error_at (loc, "unnamed field has incomplete type");
6699 type = error_mark_node;
6701 else if (TREE_CODE (type) == ARRAY_TYPE
6702 && TYPE_DOMAIN (type) == NULL_TREE)
6704 /* We have a flexible array member through a typedef.
6705 Set suitable range. Whether this is a correct position
6706 for a flexible array member will be determined elsewhere. */
6707 if (!in_system_header_at (input_location))
6708 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6709 "support flexible array members");
6710 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6711 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6712 NULL_TREE);
6713 if (orig_qual_indirect == 0)
6714 orig_qual_type = NULL_TREE;
6716 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6717 orig_qual_indirect);
6718 decl = build_decl (declarator->id_loc,
6719 FIELD_DECL, declarator->u.id, type);
6720 DECL_NONADDRESSABLE_P (decl) = bitfield;
6721 if (bitfield && !declarator->u.id)
6722 TREE_NO_WARNING (decl) = 1;
6724 if (size_varies)
6725 C_DECL_VARIABLE_SIZE (decl) = 1;
6727 else if (TREE_CODE (type) == FUNCTION_TYPE)
6729 if (storage_class == csc_register || threadp)
6731 error_at (loc, "invalid storage class for function %qE", name);
6733 else if (current_scope != file_scope)
6735 /* Function declaration not at file scope. Storage
6736 classes other than `extern' are not allowed, C99
6737 6.7.1p5, and `extern' makes no difference. However,
6738 GCC allows 'auto', perhaps with 'inline', to support
6739 nested functions. */
6740 if (storage_class == csc_auto)
6741 pedwarn (loc, OPT_Wpedantic,
6742 "invalid storage class for function %qE", name);
6743 else if (storage_class == csc_static)
6745 error_at (loc, "invalid storage class for function %qE", name);
6746 if (funcdef_flag)
6747 storage_class = declspecs->storage_class = csc_none;
6748 else
6749 return 0;
6753 decl = build_decl (declarator->id_loc,
6754 FUNCTION_DECL, declarator->u.id, type);
6755 decl = build_decl_attribute_variant (decl, decl_attr);
6757 if (type_quals & TYPE_QUAL_ATOMIC)
6759 error_at (loc,
6760 "%<_Atomic%>-qualified function type");
6761 type_quals &= ~TYPE_QUAL_ATOMIC;
6763 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6764 pedwarn (loc, OPT_Wpedantic,
6765 "ISO C forbids qualified function types");
6767 /* Every function declaration is an external reference
6768 (DECL_EXTERNAL) except for those which are not at file
6769 scope and are explicitly declared "auto". This is
6770 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6771 GCC to signify a forward declaration of a nested function. */
6772 if (storage_class == csc_auto && current_scope != file_scope)
6773 DECL_EXTERNAL (decl) = 0;
6774 /* In C99, a function which is declared 'inline' with 'extern'
6775 is not an external reference (which is confusing). It
6776 means that the later definition of the function must be output
6777 in this file, C99 6.7.4p6. In GNU C89, a function declared
6778 'extern inline' is an external reference. */
6779 else if (declspecs->inline_p && storage_class != csc_static)
6780 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6781 == flag_gnu89_inline);
6782 else
6783 DECL_EXTERNAL (decl) = !initialized;
6785 /* Record absence of global scope for `static' or `auto'. */
6786 TREE_PUBLIC (decl)
6787 = !(storage_class == csc_static || storage_class == csc_auto);
6789 /* For a function definition, record the argument information
6790 block where store_parm_decls will look for it. */
6791 if (funcdef_flag)
6792 current_function_arg_info = arg_info;
6794 if (declspecs->default_int_p)
6795 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6797 /* Record presence of `inline' and `_Noreturn', if it is
6798 reasonable. */
6799 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6801 if (declspecs->inline_p)
6802 pedwarn (loc, 0, "cannot inline function %<main%>");
6803 if (declspecs->noreturn_p)
6804 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6806 else
6808 if (declspecs->inline_p)
6809 /* Record that the function is declared `inline'. */
6810 DECL_DECLARED_INLINE_P (decl) = 1;
6811 if (declspecs->noreturn_p)
6813 if (flag_isoc99)
6814 pedwarn_c99 (loc, OPT_Wpedantic,
6815 "ISO C99 does not support %<_Noreturn%>");
6816 else
6817 pedwarn_c99 (loc, OPT_Wpedantic,
6818 "ISO C90 does not support %<_Noreturn%>");
6819 TREE_THIS_VOLATILE (decl) = 1;
6823 else
6825 /* It's a variable. */
6826 /* An uninitialized decl with `extern' is a reference. */
6827 int extern_ref = !initialized && storage_class == csc_extern;
6829 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6830 orig_qual_indirect);
6832 /* C99 6.2.2p7: It is invalid (compile-time undefined
6833 behavior) to create an 'extern' declaration for a
6834 variable if there is a global declaration that is
6835 'static' and the global declaration is not visible.
6836 (If the static declaration _is_ currently visible,
6837 the 'extern' declaration is taken to refer to that decl.) */
6838 if (extern_ref && current_scope != file_scope)
6840 tree global_decl = identifier_global_value (declarator->u.id);
6841 tree visible_decl = lookup_name (declarator->u.id);
6843 if (global_decl
6844 && global_decl != visible_decl
6845 && VAR_P (global_decl)
6846 && !TREE_PUBLIC (global_decl))
6847 error_at (loc, "variable previously declared %<static%> "
6848 "redeclared %<extern%>");
6851 decl = build_decl (declarator->id_loc,
6852 VAR_DECL, declarator->u.id, type);
6853 if (size_varies)
6854 C_DECL_VARIABLE_SIZE (decl) = 1;
6856 if (declspecs->inline_p)
6857 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6858 if (declspecs->noreturn_p)
6859 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6861 /* At file scope, an initialized extern declaration may follow
6862 a static declaration. In that case, DECL_EXTERNAL will be
6863 reset later in start_decl. */
6864 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6866 /* At file scope, the presence of a `static' or `register' storage
6867 class specifier, or the absence of all storage class specifiers
6868 makes this declaration a definition (perhaps tentative). Also,
6869 the absence of `static' makes it public. */
6870 if (current_scope == file_scope)
6872 TREE_PUBLIC (decl) = storage_class != csc_static;
6873 TREE_STATIC (decl) = !extern_ref;
6875 /* Not at file scope, only `static' makes a static definition. */
6876 else
6878 TREE_STATIC (decl) = (storage_class == csc_static);
6879 TREE_PUBLIC (decl) = extern_ref;
6882 if (threadp)
6883 set_decl_tls_model (decl, decl_default_tls_model (decl));
6886 if ((storage_class == csc_extern
6887 || (storage_class == csc_none
6888 && TREE_CODE (type) == FUNCTION_TYPE
6889 && !funcdef_flag))
6890 && variably_modified_type_p (type, NULL_TREE))
6892 /* C99 6.7.5.2p2 */
6893 if (TREE_CODE (type) == FUNCTION_TYPE)
6894 error_at (loc, "non-nested function with variably modified type");
6895 else
6896 error_at (loc, "object with variably modified type must have "
6897 "no linkage");
6900 /* Record `register' declaration for warnings on &
6901 and in case doing stupid register allocation. */
6903 if (storage_class == csc_register)
6905 C_DECL_REGISTER (decl) = 1;
6906 DECL_REGISTER (decl) = 1;
6909 /* Record constancy and volatility. */
6910 c_apply_type_quals_to_decl (type_quals, decl);
6912 /* Apply _Alignas specifiers. */
6913 if (alignas_align)
6915 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6916 DECL_USER_ALIGN (decl) = 1;
6919 /* If a type has volatile components, it should be stored in memory.
6920 Otherwise, the fact that those components are volatile
6921 will be ignored, and would even crash the compiler.
6922 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6923 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6924 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
6925 || TREE_CODE (decl) == RESULT_DECL))
6927 /* It is not an error for a structure with volatile fields to
6928 be declared register, but reset DECL_REGISTER since it
6929 cannot actually go in a register. */
6930 int was_reg = C_DECL_REGISTER (decl);
6931 C_DECL_REGISTER (decl) = 0;
6932 DECL_REGISTER (decl) = 0;
6933 c_mark_addressable (decl);
6934 C_DECL_REGISTER (decl) = was_reg;
6937 /* This is the earliest point at which we might know the assembler
6938 name of a variable. Thus, if it's known before this, die horribly. */
6939 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6941 if (warn_cxx_compat
6942 && VAR_P (decl)
6943 && TREE_PUBLIC (decl)
6944 && TREE_STATIC (decl)
6945 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6946 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6947 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6948 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6949 ("non-local variable %qD with anonymous type is "
6950 "questionable in C++"),
6951 decl);
6953 return decl;
6957 /* Decode the parameter-list info for a function type or function definition.
6958 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6959 if there is an identifier list instead of a parameter decl list).
6960 These two functions are separate because when a function returns
6961 or receives functions then each is called multiple times but the order
6962 of calls is different. The last call to `grokparms' is always the one
6963 that contains the formal parameter names of a function definition.
6965 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6967 FUNCDEF_FLAG is true for a function definition, false for
6968 a mere declaration. A nonempty identifier-list gets an error message
6969 when FUNCDEF_FLAG is false. */
6971 static tree
6972 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6974 tree arg_types = arg_info->types;
6976 if (funcdef_flag && arg_info->had_vla_unspec)
6978 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6979 /* C99 6.7.5.2p4 */
6980 error ("%<[*]%> not allowed in other than function prototype scope");
6983 if (arg_types == 0 && !funcdef_flag
6984 && !in_system_header_at (input_location))
6985 warning (OPT_Wstrict_prototypes,
6986 "function declaration isn%'t a prototype");
6988 if (arg_types == error_mark_node)
6989 return 0; /* don't set TYPE_ARG_TYPES in this case */
6991 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6993 if (!funcdef_flag)
6995 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6996 arg_info->parms = NULL_TREE;
6998 else
6999 arg_info->parms = arg_info->types;
7001 arg_info->types = 0;
7002 return 0;
7004 else
7006 tree parm, type, typelt;
7007 unsigned int parmno;
7009 /* If there is a parameter of incomplete type in a definition,
7010 this is an error. In a declaration this is valid, and a
7011 struct or union type may be completed later, before any calls
7012 or definition of the function. In the case where the tag was
7013 first declared within the parameter list, a warning has
7014 already been given. If a parameter has void type, then
7015 however the function cannot be defined or called, so
7016 warn. */
7018 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7019 parm;
7020 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7022 type = TREE_VALUE (typelt);
7023 if (type == error_mark_node)
7024 continue;
7026 if (!COMPLETE_TYPE_P (type))
7028 if (funcdef_flag)
7030 if (DECL_NAME (parm))
7031 error_at (input_location,
7032 "parameter %u (%q+D) has incomplete type",
7033 parmno, parm);
7034 else
7035 error_at (DECL_SOURCE_LOCATION (parm),
7036 "parameter %u has incomplete type",
7037 parmno);
7039 TREE_VALUE (typelt) = error_mark_node;
7040 TREE_TYPE (parm) = error_mark_node;
7041 arg_types = NULL_TREE;
7043 else if (VOID_TYPE_P (type))
7045 if (DECL_NAME (parm))
7046 warning_at (input_location, 0,
7047 "parameter %u (%q+D) has void type",
7048 parmno, parm);
7049 else
7050 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7051 "parameter %u has void type",
7052 parmno);
7056 if (DECL_NAME (parm) && TREE_USED (parm))
7057 warn_if_shadowing (parm);
7059 return arg_types;
7063 /* Allocate and initialize a c_arg_info structure from the parser's
7064 obstack. */
7066 struct c_arg_info *
7067 build_arg_info (void)
7069 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7070 ret->parms = NULL_TREE;
7071 ret->tags = NULL;
7072 ret->types = NULL_TREE;
7073 ret->others = NULL_TREE;
7074 ret->pending_sizes = NULL;
7075 ret->had_vla_unspec = 0;
7076 return ret;
7079 /* Take apart the current scope and return a c_arg_info structure with
7080 info on a parameter list just parsed.
7082 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7084 ELLIPSIS being true means the argument list ended in '...' so don't
7085 append a sentinel (void_list_node) to the end of the type-list.
7087 EXPR is NULL or an expression that needs to be evaluated for the
7088 side effects of array size expressions in the parameters. */
7090 struct c_arg_info *
7091 get_parm_info (bool ellipsis, tree expr)
7093 struct c_binding *b = current_scope->bindings;
7094 struct c_arg_info *arg_info = build_arg_info ();
7096 tree parms = 0;
7097 vec<c_arg_tag, va_gc> *tags = NULL;
7098 tree types = 0;
7099 tree others = 0;
7101 bool gave_void_only_once_err = false;
7103 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7105 /* The bindings in this scope must not get put into a block.
7106 We will take care of deleting the binding nodes. */
7107 current_scope->bindings = 0;
7109 /* This function is only called if there was *something* on the
7110 parameter list. */
7111 gcc_assert (b);
7113 /* A parameter list consisting solely of 'void' indicates that the
7114 function takes no arguments. But if the 'void' is qualified
7115 (by 'const' or 'volatile'), or has a storage class specifier
7116 ('register'), then the behavior is undefined; issue an error.
7117 Typedefs for 'void' are OK (see DR#157). */
7118 if (b->prev == 0 /* one binding */
7119 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7120 && !DECL_NAME (b->decl) /* anonymous */
7121 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7123 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7124 || C_DECL_REGISTER (b->decl))
7125 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7127 /* There cannot be an ellipsis. */
7128 if (ellipsis)
7129 error_at (b->locus, "%<void%> must be the only parameter");
7131 arg_info->types = void_list_node;
7132 return arg_info;
7135 if (!ellipsis)
7136 types = void_list_node;
7138 /* Break up the bindings list into parms, tags, types, and others;
7139 apply sanity checks; purge the name-to-decl bindings. */
7140 while (b)
7142 tree decl = b->decl;
7143 tree type = TREE_TYPE (decl);
7144 c_arg_tag tag;
7145 const char *keyword;
7147 switch (TREE_CODE (decl))
7149 case PARM_DECL:
7150 if (b->id)
7152 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7153 I_SYMBOL_BINDING (b->id) = b->shadowed;
7156 /* Check for forward decls that never got their actual decl. */
7157 if (TREE_ASM_WRITTEN (decl))
7158 error_at (b->locus,
7159 "parameter %q+D has just a forward declaration", decl);
7160 /* Check for (..., void, ...) and issue an error. */
7161 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7163 if (!gave_void_only_once_err)
7165 error_at (b->locus, "%<void%> must be the only parameter");
7166 gave_void_only_once_err = true;
7169 else
7171 /* Valid parameter, add it to the list. */
7172 DECL_CHAIN (decl) = parms;
7173 parms = decl;
7175 /* Since there is a prototype, args are passed in their
7176 declared types. The back end may override this later. */
7177 DECL_ARG_TYPE (decl) = type;
7178 types = tree_cons (0, type, types);
7180 break;
7182 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7183 case UNION_TYPE: keyword = "union"; goto tag;
7184 case RECORD_TYPE: keyword = "struct"; goto tag;
7185 tag:
7186 /* Types may not have tag-names, in which case the type
7187 appears in the bindings list with b->id NULL. */
7188 if (b->id)
7190 gcc_assert (I_TAG_BINDING (b->id) == b);
7191 I_TAG_BINDING (b->id) = b->shadowed;
7194 /* Warn about any struct, union or enum tags defined in a
7195 parameter list. The scope of such types is limited to
7196 the parameter list, which is rarely if ever desirable
7197 (it's impossible to call such a function with type-
7198 correct arguments). An anonymous union parm type is
7199 meaningful as a GNU extension, so don't warn for that. */
7200 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7202 if (b->id)
7203 /* The %s will be one of 'struct', 'union', or 'enum'. */
7204 warning_at (b->locus, 0,
7205 "%<%s %E%> declared inside parameter list"
7206 " will not be visible outside of this definition or"
7207 " declaration", keyword, b->id);
7208 else
7209 /* The %s will be one of 'struct', 'union', or 'enum'. */
7210 warning_at (b->locus, 0,
7211 "anonymous %s declared inside parameter list"
7212 " will not be visible outside of this definition or"
7213 " declaration", keyword);
7216 tag.id = b->id;
7217 tag.type = decl;
7218 vec_safe_push (tags, tag);
7219 break;
7221 case FUNCTION_DECL:
7222 /* FUNCTION_DECLs appear when there is an implicit function
7223 declaration in the parameter list. */
7224 gcc_assert (b->nested || seen_error ());
7225 goto set_shadowed;
7227 case CONST_DECL:
7228 case TYPE_DECL:
7229 /* CONST_DECLs appear here when we have an embedded enum,
7230 and TYPE_DECLs appear here when we have an embedded struct
7231 or union. No warnings for this - we already warned about the
7232 type itself. */
7234 /* When we reinsert this decl in the function body, we need
7235 to reconstruct whether it was marked as nested. */
7236 gcc_assert (!b->nested);
7237 DECL_CHAIN (decl) = others;
7238 others = decl;
7239 /* fall through */
7241 case ERROR_MARK:
7242 set_shadowed:
7243 /* error_mark_node appears here when we have an undeclared
7244 variable. Just throw it away. */
7245 if (b->id)
7247 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7248 I_SYMBOL_BINDING (b->id) = b->shadowed;
7250 break;
7252 /* Other things that might be encountered. */
7253 case LABEL_DECL:
7254 case VAR_DECL:
7255 default:
7256 gcc_unreachable ();
7259 b = free_binding_and_advance (b);
7262 arg_info->parms = parms;
7263 arg_info->tags = tags;
7264 arg_info->types = types;
7265 arg_info->others = others;
7266 arg_info->pending_sizes = expr;
7267 return arg_info;
7270 /* Get the struct, enum or union (CODE says which) with tag NAME.
7271 Define the tag as a forward-reference with location LOC if it is
7272 not defined. Return a c_typespec structure for the type
7273 specifier. */
7275 struct c_typespec
7276 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7278 struct c_typespec ret;
7279 tree ref;
7280 location_t refloc;
7282 ret.expr = NULL_TREE;
7283 ret.expr_const_operands = true;
7285 /* If a cross reference is requested, look up the type
7286 already defined for this tag and return it. */
7288 ref = lookup_tag (code, name, false, &refloc);
7289 /* If this is the right type of tag, return what we found.
7290 (This reference will be shadowed by shadow_tag later if appropriate.)
7291 If this is the wrong type of tag, do not return it. If it was the
7292 wrong type in the same scope, we will have had an error
7293 message already; if in a different scope and declaring
7294 a name, pending_xref_error will give an error message; but if in a
7295 different scope and not declaring a name, this tag should
7296 shadow the previous declaration of a different type of tag, and
7297 this would not work properly if we return the reference found.
7298 (For example, with "struct foo" in an outer scope, "union foo;"
7299 must shadow that tag with a new one of union type.) */
7300 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7301 if (ref && TREE_CODE (ref) == code)
7303 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7304 && loc != UNKNOWN_LOCATION
7305 && warn_cxx_compat)
7307 switch (code)
7309 case ENUMERAL_TYPE:
7310 warning_at (loc, OPT_Wc___compat,
7311 ("enum type defined in struct or union "
7312 "is not visible in C++"));
7313 inform (refloc, "enum type defined here");
7314 break;
7315 case RECORD_TYPE:
7316 warning_at (loc, OPT_Wc___compat,
7317 ("struct defined in struct or union "
7318 "is not visible in C++"));
7319 inform (refloc, "struct defined here");
7320 break;
7321 case UNION_TYPE:
7322 warning_at (loc, OPT_Wc___compat,
7323 ("union defined in struct or union "
7324 "is not visible in C++"));
7325 inform (refloc, "union defined here");
7326 break;
7327 default:
7328 gcc_unreachable();
7332 ret.spec = ref;
7333 return ret;
7336 /* If no such tag is yet defined, create a forward-reference node
7337 and record it as the "definition".
7338 When a real declaration of this type is found,
7339 the forward-reference will be altered into a real type. */
7341 ref = make_node (code);
7342 if (code == ENUMERAL_TYPE)
7344 /* Give the type a default layout like unsigned int
7345 to avoid crashing if it does not get defined. */
7346 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7347 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7348 TYPE_USER_ALIGN (ref) = 0;
7349 TYPE_UNSIGNED (ref) = 1;
7350 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7351 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7352 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7355 pushtag (loc, name, ref);
7357 ret.spec = ref;
7358 return ret;
7361 /* Get the struct, enum or union (CODE says which) with tag NAME.
7362 Define the tag as a forward-reference if it is not defined.
7363 Return a tree for the type. */
7365 tree
7366 xref_tag (enum tree_code code, tree name)
7368 return parser_xref_tag (input_location, code, name).spec;
7371 /* Make sure that the tag NAME is defined *in the current scope*
7372 at least as a forward reference.
7373 LOC is the location of the struct's definition.
7374 CODE says which kind of tag NAME ought to be.
7376 This stores the current value of the file static STRUCT_PARSE_INFO
7377 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7378 new c_struct_parse_info structure. The old value of
7379 STRUCT_PARSE_INFO is restored in finish_struct. */
7381 tree
7382 start_struct (location_t loc, enum tree_code code, tree name,
7383 struct c_struct_parse_info **enclosing_struct_parse_info)
7385 /* If there is already a tag defined at this scope
7386 (as a forward reference), just return it. */
7388 tree ref = NULL_TREE;
7389 location_t refloc = UNKNOWN_LOCATION;
7391 if (name != NULL_TREE)
7392 ref = lookup_tag (code, name, true, &refloc);
7393 if (ref && TREE_CODE (ref) == code)
7395 if (TYPE_SIZE (ref))
7397 if (code == UNION_TYPE)
7398 error_at (loc, "redefinition of %<union %E%>", name);
7399 else
7400 error_at (loc, "redefinition of %<struct %E%>", name);
7401 if (refloc != UNKNOWN_LOCATION)
7402 inform (refloc, "originally defined here");
7403 /* Don't create structures using a name already in use. */
7404 ref = NULL_TREE;
7406 else if (C_TYPE_BEING_DEFINED (ref))
7408 if (code == UNION_TYPE)
7409 error_at (loc, "nested redefinition of %<union %E%>", name);
7410 else
7411 error_at (loc, "nested redefinition of %<struct %E%>", name);
7412 /* Don't bother to report "originally defined here" for a
7413 nested redefinition; the original definition should be
7414 obvious. */
7415 /* Don't create structures that contain themselves. */
7416 ref = NULL_TREE;
7420 /* Otherwise create a forward-reference just so the tag is in scope. */
7422 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7424 ref = make_node (code);
7425 pushtag (loc, name, ref);
7428 C_TYPE_BEING_DEFINED (ref) = 1;
7429 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7430 TYPE_PACKED (v) = flag_pack_struct;
7432 *enclosing_struct_parse_info = struct_parse_info;
7433 struct_parse_info = new c_struct_parse_info ();
7435 /* FIXME: This will issue a warning for a use of a type defined
7436 within a statement expr used within sizeof, et. al. This is not
7437 terribly serious as C++ doesn't permit statement exprs within
7438 sizeof anyhow. */
7439 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7440 warning_at (loc, OPT_Wc___compat,
7441 "defining type in %qs expression is invalid in C++",
7442 (in_sizeof
7443 ? "sizeof"
7444 : (in_typeof ? "typeof" : "alignof")));
7446 return ref;
7449 /* Process the specs, declarator and width (NULL if omitted)
7450 of a structure component, returning a FIELD_DECL node.
7451 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7452 DECL_ATTRS is as for grokdeclarator.
7454 LOC is the location of the structure component.
7456 This is done during the parsing of the struct declaration.
7457 The FIELD_DECL nodes are chained together and the lot of them
7458 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7460 tree
7461 grokfield (location_t loc,
7462 struct c_declarator *declarator, struct c_declspecs *declspecs,
7463 tree width, tree *decl_attrs)
7465 tree value;
7467 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7468 && width == NULL_TREE)
7470 /* This is an unnamed decl.
7472 If we have something of the form "union { list } ;" then this
7473 is the anonymous union extension. Similarly for struct.
7475 If this is something of the form "struct foo;", then
7476 If MS or Plan 9 extensions are enabled, this is handled as
7477 an anonymous struct.
7478 Otherwise this is a forward declaration of a structure tag.
7480 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7481 If foo names a structure or union without a tag, then this
7482 is an anonymous struct (this is permitted by C11).
7483 If MS or Plan 9 extensions are enabled and foo names a
7484 structure, then again this is an anonymous struct.
7485 Otherwise this is an error.
7487 Oh what a horrid tangled web we weave. I wonder if MS consciously
7488 took this from Plan 9 or if it was an accident of implementation
7489 that took root before someone noticed the bug... */
7491 tree type = declspecs->type;
7492 bool type_ok = RECORD_OR_UNION_TYPE_P (type);
7493 bool ok = false;
7495 if (type_ok
7496 && (flag_ms_extensions
7497 || flag_plan9_extensions
7498 || !declspecs->typedef_p))
7500 if (flag_ms_extensions || flag_plan9_extensions)
7501 ok = true;
7502 else if (TYPE_NAME (type) == NULL)
7503 ok = true;
7504 else
7505 ok = false;
7507 if (!ok)
7509 pedwarn (loc, 0, "declaration does not declare anything");
7510 return NULL_TREE;
7512 if (flag_isoc99)
7513 pedwarn_c99 (loc, OPT_Wpedantic,
7514 "ISO C99 doesn%'t support unnamed structs/unions");
7515 else
7516 pedwarn_c99 (loc, OPT_Wpedantic,
7517 "ISO C90 doesn%'t support unnamed structs/unions");
7520 value = grokdeclarator (declarator, declspecs, FIELD, false,
7521 width ? &width : NULL, decl_attrs, NULL, NULL,
7522 DEPRECATED_NORMAL);
7524 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7525 DECL_INITIAL (value) = width;
7527 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7529 /* If we currently have a binding for this field, set the
7530 in_struct field in the binding, so that we warn about lookups
7531 which find it. */
7532 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7533 if (b != NULL)
7535 /* If the in_struct field is not yet set, push it on a list
7536 to be cleared when this struct is finished. */
7537 if (!b->in_struct)
7539 struct_parse_info->fields.safe_push (b);
7540 b->in_struct = 1;
7545 return value;
7548 /* Subroutine of detect_field_duplicates: return whether X and Y,
7549 which are both fields in the same struct, have duplicate field
7550 names. */
7552 static bool
7553 is_duplicate_field (tree x, tree y)
7555 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7556 return true;
7558 /* When using -fplan9-extensions, an anonymous field whose name is a
7559 typedef can duplicate a field name. */
7560 if (flag_plan9_extensions
7561 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7563 tree xt, xn, yt, yn;
7565 xt = TREE_TYPE (x);
7566 if (DECL_NAME (x) != NULL_TREE)
7567 xn = DECL_NAME (x);
7568 else if (RECORD_OR_UNION_TYPE_P (xt)
7569 && TYPE_NAME (xt) != NULL_TREE
7570 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7571 xn = DECL_NAME (TYPE_NAME (xt));
7572 else
7573 xn = NULL_TREE;
7575 yt = TREE_TYPE (y);
7576 if (DECL_NAME (y) != NULL_TREE)
7577 yn = DECL_NAME (y);
7578 else if (RECORD_OR_UNION_TYPE_P (yt)
7579 && TYPE_NAME (yt) != NULL_TREE
7580 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7581 yn = DECL_NAME (TYPE_NAME (yt));
7582 else
7583 yn = NULL_TREE;
7585 if (xn != NULL_TREE && xn == yn)
7586 return true;
7589 return false;
7592 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7593 to HTAB, giving errors for any duplicates. */
7595 static void
7596 detect_field_duplicates_hash (tree fieldlist,
7597 hash_table<nofree_ptr_hash <tree_node> > *htab)
7599 tree x, y;
7600 tree_node **slot;
7602 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7603 if ((y = DECL_NAME (x)) != 0)
7605 slot = htab->find_slot (y, INSERT);
7606 if (*slot)
7608 error ("duplicate member %q+D", x);
7609 DECL_NAME (x) = NULL_TREE;
7611 *slot = y;
7613 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7615 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7617 /* When using -fplan9-extensions, an anonymous field whose
7618 name is a typedef can duplicate a field name. */
7619 if (flag_plan9_extensions
7620 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7621 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7623 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7624 slot = htab->find_slot (xn, INSERT);
7625 if (*slot)
7626 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7627 *slot = xn;
7632 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7633 the list such that this does not present a problem later. */
7635 static void
7636 detect_field_duplicates (tree fieldlist)
7638 tree x, y;
7639 int timeout = 10;
7641 /* If the struct is the list of instance variables of an Objective-C
7642 class, then we need to check all the instance variables of
7643 superclasses when checking for duplicates (since you can't have
7644 an instance variable in a subclass with the same name as an
7645 instance variable in a superclass). We pass on this job to the
7646 Objective-C compiler. objc_detect_field_duplicates() will return
7647 false if we are not checking the list of instance variables and
7648 the C frontend should proceed with the standard field duplicate
7649 checks. If we are checking the list of instance variables, the
7650 ObjC frontend will do the check, emit the errors if needed, and
7651 then return true. */
7652 if (c_dialect_objc ())
7653 if (objc_detect_field_duplicates (false))
7654 return;
7656 /* First, see if there are more than "a few" fields.
7657 This is trivially true if there are zero or one fields. */
7658 if (!fieldlist || !DECL_CHAIN (fieldlist))
7659 return;
7660 x = fieldlist;
7661 do {
7662 timeout--;
7663 if (DECL_NAME (x) == NULL_TREE
7664 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7665 timeout = 0;
7666 x = DECL_CHAIN (x);
7667 } while (timeout > 0 && x);
7669 /* If there were "few" fields and no anonymous structures or unions,
7670 avoid the overhead of allocating a hash table. Instead just do
7671 the nested traversal thing. */
7672 if (timeout > 0)
7674 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7675 /* When using -fplan9-extensions, we can have duplicates
7676 between typedef names and fields. */
7677 if (DECL_NAME (x)
7678 || (flag_plan9_extensions
7679 && DECL_NAME (x) == NULL_TREE
7680 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7681 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7682 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7684 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7685 if (is_duplicate_field (y, x))
7687 error ("duplicate member %q+D", x);
7688 DECL_NAME (x) = NULL_TREE;
7692 else
7694 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7695 detect_field_duplicates_hash (fieldlist, &htab);
7699 /* Finish up struct info used by -Wc++-compat. */
7701 static void
7702 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7703 location_t record_loc)
7705 unsigned int ix;
7706 tree x;
7707 struct c_binding *b;
7709 if (fieldlist == NULL_TREE)
7711 if (code == RECORD_TYPE)
7712 warning_at (record_loc, OPT_Wc___compat,
7713 "empty struct has size 0 in C, size 1 in C++");
7714 else
7715 warning_at (record_loc, OPT_Wc___compat,
7716 "empty union has size 0 in C, size 1 in C++");
7719 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7720 the current struct. We do this now at the end of the struct
7721 because the flag is used to issue visibility warnings, and we
7722 only want to issue those warnings if the type is referenced
7723 outside of the struct declaration. */
7724 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7725 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7727 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7728 typedefs used when declaring fields in this struct. If the name
7729 of any of the fields is also a typedef name then the struct would
7730 not parse in C++, because the C++ lookup rules say that the
7731 typedef name would be looked up in the context of the struct, and
7732 would thus be the field rather than the typedef. */
7733 if (!struct_parse_info->typedefs_seen.is_empty ()
7734 && fieldlist != NULL_TREE)
7736 /* Use a hash_set<tree> using the name of the typedef. We can use
7737 a hash_set<tree> because identifiers are interned. */
7738 hash_set<tree> tset;
7740 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7741 tset.add (DECL_NAME (x));
7743 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7745 if (DECL_NAME (x) != NULL_TREE
7746 && tset.contains (DECL_NAME (x)))
7748 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7749 ("using %qD as both field and typedef name is "
7750 "invalid in C++"),
7752 /* FIXME: It would be nice to report the location where
7753 the typedef name is used. */
7758 /* For each field which has a binding and which was not defined in
7759 an enclosing struct, clear the in_struct field. */
7760 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7761 b->in_struct = 0;
7764 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7765 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7766 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7767 ATTRIBUTES are attributes to be applied to the structure.
7769 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7770 the struct was started. */
7772 tree
7773 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7774 struct c_struct_parse_info *enclosing_struct_parse_info)
7776 tree x;
7777 bool toplevel = file_scope == current_scope;
7778 int saw_named_field;
7780 /* If this type was previously laid out as a forward reference,
7781 make sure we lay it out again. */
7783 TYPE_SIZE (t) = 0;
7785 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7787 if (pedantic)
7789 for (x = fieldlist; x; x = DECL_CHAIN (x))
7791 if (DECL_NAME (x) != 0)
7792 break;
7793 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7794 break;
7797 if (x == 0)
7799 if (TREE_CODE (t) == UNION_TYPE)
7801 if (fieldlist)
7802 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7803 else
7804 pedwarn (loc, OPT_Wpedantic, "union has no members");
7806 else
7808 if (fieldlist)
7809 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7810 else
7811 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7816 /* Install struct as DECL_CONTEXT of each field decl.
7817 Also process specified field sizes, found in the DECL_INITIAL,
7818 storing 0 there after the type has been changed to precision equal
7819 to its width, rather than the precision of the specified standard
7820 type. (Correct layout requires the original type to have been preserved
7821 until now.) */
7823 saw_named_field = 0;
7824 for (x = fieldlist; x; x = DECL_CHAIN (x))
7826 if (TREE_TYPE (x) == error_mark_node)
7827 continue;
7829 DECL_CONTEXT (x) = t;
7831 /* If any field is const, the structure type is pseudo-const. */
7832 if (TREE_READONLY (x))
7833 C_TYPE_FIELDS_READONLY (t) = 1;
7834 else
7836 /* A field that is pseudo-const makes the structure likewise. */
7837 tree t1 = strip_array_types (TREE_TYPE (x));
7838 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7839 C_TYPE_FIELDS_READONLY (t) = 1;
7842 /* Any field that is volatile means variables of this type must be
7843 treated in some ways as volatile. */
7844 if (TREE_THIS_VOLATILE (x))
7845 C_TYPE_FIELDS_VOLATILE (t) = 1;
7847 /* Any field of nominal variable size implies structure is too. */
7848 if (C_DECL_VARIABLE_SIZE (x))
7849 C_TYPE_VARIABLE_SIZE (t) = 1;
7851 if (DECL_INITIAL (x))
7853 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7854 DECL_SIZE (x) = bitsize_int (width);
7855 DECL_BIT_FIELD (x) = 1;
7856 SET_DECL_C_BIT_FIELD (x);
7859 if (TYPE_PACKED (t)
7860 && (DECL_BIT_FIELD (x)
7861 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7862 DECL_PACKED (x) = 1;
7864 /* Detect flexible array member in an invalid context. */
7865 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7866 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7867 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7868 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7870 if (TREE_CODE (t) == UNION_TYPE)
7872 error_at (DECL_SOURCE_LOCATION (x),
7873 "flexible array member in union");
7874 TREE_TYPE (x) = error_mark_node;
7876 else if (DECL_CHAIN (x) != NULL_TREE)
7878 error_at (DECL_SOURCE_LOCATION (x),
7879 "flexible array member not at end of struct");
7880 TREE_TYPE (x) = error_mark_node;
7882 else if (!saw_named_field)
7884 error_at (DECL_SOURCE_LOCATION (x),
7885 "flexible array member in a struct with no named "
7886 "members");
7887 TREE_TYPE (x) = error_mark_node;
7891 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7892 && flexible_array_type_p (TREE_TYPE (x)))
7893 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7894 "invalid use of structure with flexible array member");
7896 if (DECL_NAME (x)
7897 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7898 saw_named_field = 1;
7901 detect_field_duplicates (fieldlist);
7903 /* Now we have the nearly final fieldlist. Record it,
7904 then lay out the structure or union (including the fields). */
7906 TYPE_FIELDS (t) = fieldlist;
7908 maybe_apply_pragma_scalar_storage_order (t);
7910 layout_type (t);
7912 if (TYPE_SIZE_UNIT (t)
7913 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7914 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7915 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7916 error ("type %qT is too large", t);
7918 /* Give bit-fields their proper types and rewrite the type of array fields
7919 with scalar component if the enclosing type has reverse storage order. */
7920 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
7922 if (TREE_CODE (field) == FIELD_DECL
7923 && DECL_INITIAL (field)
7924 && TREE_TYPE (field) != error_mark_node)
7926 unsigned HOST_WIDE_INT width
7927 = tree_to_uhwi (DECL_INITIAL (field));
7928 tree type = TREE_TYPE (field);
7929 if (width != TYPE_PRECISION (type))
7931 TREE_TYPE (field)
7932 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7933 DECL_MODE (field) = TYPE_MODE (TREE_TYPE (field));
7935 DECL_INITIAL (field) = 0;
7937 else if (TYPE_REVERSE_STORAGE_ORDER (t)
7938 && TREE_CODE (field) == FIELD_DECL
7939 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
7941 tree ftype = TREE_TYPE (field);
7942 tree ctype = strip_array_types (ftype);
7943 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
7945 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
7946 tree *typep = &fmain_type;
7947 do {
7948 *typep = build_distinct_type_copy (*typep);
7949 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
7950 typep = &TREE_TYPE (*typep);
7951 } while (TREE_CODE (*typep) == ARRAY_TYPE);
7952 TREE_TYPE (field)
7953 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
7958 /* Now we have the truly final field list.
7959 Store it in this type and in the variants. */
7961 TYPE_FIELDS (t) = fieldlist;
7963 /* If there are lots of fields, sort so we can look through them fast.
7964 We arbitrarily consider 16 or more elts to be "a lot". */
7967 int len = 0;
7969 for (x = fieldlist; x; x = DECL_CHAIN (x))
7971 if (len > 15 || DECL_NAME (x) == NULL)
7972 break;
7973 len += 1;
7976 if (len > 15)
7978 tree *field_array;
7979 struct lang_type *space;
7980 struct sorted_fields_type *space2;
7982 len += list_length (x);
7984 /* Use the same allocation policy here that make_node uses, to
7985 ensure that this lives as long as the rest of the struct decl.
7986 All decls in an inline function need to be saved. */
7988 space = ggc_cleared_alloc<struct lang_type> ();
7989 space2 = (sorted_fields_type *) ggc_internal_alloc
7990 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7992 len = 0;
7993 space->s = space2;
7994 field_array = &space2->elts[0];
7995 for (x = fieldlist; x; x = DECL_CHAIN (x))
7997 field_array[len++] = x;
7999 /* If there is anonymous struct or union, break out of the loop. */
8000 if (DECL_NAME (x) == NULL)
8001 break;
8003 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8004 if (x == NULL)
8006 TYPE_LANG_SPECIFIC (t) = space;
8007 TYPE_LANG_SPECIFIC (t)->s->len = len;
8008 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8009 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8014 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8015 in dwarf2out via rest_of_decl_compilation below and means
8016 something totally different. Since we will be clearing
8017 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8018 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8019 C_TYPE_INCOMPLETE_VARS should use some language specific
8020 node. */
8021 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8022 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8024 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8025 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8026 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8027 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8028 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8029 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8032 /* If this was supposed to be a transparent union, but we can't
8033 make it one, warn and turn off the flag. */
8034 if (TREE_CODE (t) == UNION_TYPE
8035 && TYPE_TRANSPARENT_AGGR (t)
8036 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8038 TYPE_TRANSPARENT_AGGR (t) = 0;
8039 warning_at (loc, 0, "union cannot be made transparent");
8042 /* If this structure or union completes the type of any previous
8043 variable declaration, lay it out and output its rtl. */
8044 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8046 tree decl = TREE_VALUE (x);
8047 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8048 layout_array_type (TREE_TYPE (decl));
8049 if (TREE_CODE (decl) != TYPE_DECL)
8051 layout_decl (decl, 0);
8052 if (c_dialect_objc ())
8053 objc_check_decl (decl);
8054 rest_of_decl_compilation (decl, toplevel, 0);
8058 /* Update type location to the one of the definition, instead of e.g.
8059 a forward declaration. */
8060 if (TYPE_STUB_DECL (t))
8061 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8063 /* Finish debugging output for this type. */
8064 rest_of_type_compilation (t, toplevel);
8066 /* If we're inside a function proper, i.e. not file-scope and not still
8067 parsing parameters, then arrange for the size of a variable sized type
8068 to be bound now. */
8069 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8070 add_stmt (build_stmt (loc,
8071 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8073 if (warn_cxx_compat)
8074 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8076 delete struct_parse_info;
8078 struct_parse_info = enclosing_struct_parse_info;
8080 /* If this struct is defined inside a struct, add it to
8081 struct_types. */
8082 if (warn_cxx_compat
8083 && struct_parse_info != NULL
8084 && !in_sizeof && !in_typeof && !in_alignof)
8085 struct_parse_info->struct_types.safe_push (t);
8087 return t;
8090 /* Lay out the type T, and its element type, and so on. */
8092 static void
8093 layout_array_type (tree t)
8095 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8096 layout_array_type (TREE_TYPE (t));
8097 layout_type (t);
8100 /* Begin compiling the definition of an enumeration type.
8101 NAME is its name (or null if anonymous).
8102 LOC is the enum's location.
8103 Returns the type object, as yet incomplete.
8104 Also records info about it so that build_enumerator
8105 may be used to declare the individual values as they are read. */
8107 tree
8108 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8110 tree enumtype = NULL_TREE;
8111 location_t enumloc = UNKNOWN_LOCATION;
8113 /* If this is the real definition for a previous forward reference,
8114 fill in the contents in the same object that used to be the
8115 forward reference. */
8117 if (name != NULL_TREE)
8118 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8120 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8122 enumtype = make_node (ENUMERAL_TYPE);
8123 pushtag (loc, name, enumtype);
8126 if (C_TYPE_BEING_DEFINED (enumtype))
8127 error_at (loc, "nested redefinition of %<enum %E%>", name);
8129 C_TYPE_BEING_DEFINED (enumtype) = 1;
8131 if (TYPE_VALUES (enumtype) != 0)
8133 /* This enum is a named one that has been declared already. */
8134 error_at (loc, "redeclaration of %<enum %E%>", name);
8135 if (enumloc != UNKNOWN_LOCATION)
8136 inform (enumloc, "originally defined here");
8138 /* Completely replace its old definition.
8139 The old enumerators remain defined, however. */
8140 TYPE_VALUES (enumtype) = 0;
8143 the_enum->enum_next_value = integer_zero_node;
8144 the_enum->enum_overflow = 0;
8146 if (flag_short_enums)
8147 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8148 TYPE_PACKED (v) = 1;
8150 /* FIXME: This will issue a warning for a use of a type defined
8151 within sizeof in a statement expr. This is not terribly serious
8152 as C++ doesn't permit statement exprs within sizeof anyhow. */
8153 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8154 warning_at (loc, OPT_Wc___compat,
8155 "defining type in %qs expression is invalid in C++",
8156 (in_sizeof
8157 ? "sizeof"
8158 : (in_typeof ? "typeof" : "alignof")));
8160 return enumtype;
8163 /* After processing and defining all the values of an enumeration type,
8164 install their decls in the enumeration type and finish it off.
8165 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8166 and ATTRIBUTES are the specified attributes.
8167 Returns ENUMTYPE. */
8169 tree
8170 finish_enum (tree enumtype, tree values, tree attributes)
8172 tree pair, tem;
8173 tree minnode = 0, maxnode = 0;
8174 int precision;
8175 signop sign;
8176 bool toplevel = (file_scope == current_scope);
8177 struct lang_type *lt;
8179 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8181 /* Calculate the maximum value of any enumerator in this type. */
8183 if (values == error_mark_node)
8184 minnode = maxnode = integer_zero_node;
8185 else
8187 minnode = maxnode = TREE_VALUE (values);
8188 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8190 tree value = TREE_VALUE (pair);
8191 if (tree_int_cst_lt (maxnode, value))
8192 maxnode = value;
8193 if (tree_int_cst_lt (value, minnode))
8194 minnode = value;
8198 /* Construct the final type of this enumeration. It is the same
8199 as one of the integral types - the narrowest one that fits, except
8200 that normally we only go as narrow as int - and signed iff any of
8201 the values are negative. */
8202 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8203 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8204 tree_int_cst_min_precision (maxnode, sign));
8206 /* If the precision of the type was specified with an attribute and it
8207 was too small, give an error. Otherwise, use it. */
8208 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8210 if (precision > TYPE_PRECISION (enumtype))
8212 TYPE_PRECISION (enumtype) = 0;
8213 error ("specified mode too small for enumeral values");
8215 else
8216 precision = TYPE_PRECISION (enumtype);
8218 else
8219 TYPE_PRECISION (enumtype) = 0;
8221 if (TYPE_PACKED (enumtype)
8222 || precision > TYPE_PRECISION (integer_type_node)
8223 || TYPE_PRECISION (enumtype))
8225 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8226 if (tem == NULL)
8228 warning (0, "enumeration values exceed range of largest integer");
8229 tem = long_long_integer_type_node;
8232 else
8233 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8235 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8236 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8237 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8238 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8239 TYPE_SIZE (enumtype) = 0;
8240 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8242 layout_type (enumtype);
8244 if (values != error_mark_node)
8246 /* Change the type of the enumerators to be the enum type. We
8247 need to do this irrespective of the size of the enum, for
8248 proper type checking. Replace the DECL_INITIALs of the
8249 enumerators, and the value slots of the list, with copies
8250 that have the enum type; they cannot be modified in place
8251 because they may be shared (e.g. integer_zero_node) Finally,
8252 change the purpose slots to point to the names of the decls. */
8253 for (pair = values; pair; pair = TREE_CHAIN (pair))
8255 tree enu = TREE_PURPOSE (pair);
8256 tree ini = DECL_INITIAL (enu);
8258 TREE_TYPE (enu) = enumtype;
8260 /* The ISO C Standard mandates enumerators to have type int,
8261 even though the underlying type of an enum type is
8262 unspecified. However, GCC allows enumerators of any
8263 integer type as an extensions. build_enumerator()
8264 converts any enumerators that fit in an int to type int,
8265 to avoid promotions to unsigned types when comparing
8266 integers with enumerators that fit in the int range.
8267 When -pedantic is given, build_enumerator() would have
8268 already warned about those that don't fit. Here we
8269 convert the rest to the enumerator type. */
8270 if (TREE_TYPE (ini) != integer_type_node)
8271 ini = convert (enumtype, ini);
8273 DECL_INITIAL (enu) = ini;
8274 TREE_PURPOSE (pair) = DECL_NAME (enu);
8275 TREE_VALUE (pair) = ini;
8278 TYPE_VALUES (enumtype) = values;
8281 /* Record the min/max values so that we can warn about bit-field
8282 enumerations that are too small for the values. */
8283 lt = ggc_cleared_alloc<struct lang_type> ();
8284 lt->enum_min = minnode;
8285 lt->enum_max = maxnode;
8286 TYPE_LANG_SPECIFIC (enumtype) = lt;
8288 /* Fix up all variant types of this enum type. */
8289 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8291 if (tem == enumtype)
8292 continue;
8293 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8294 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8295 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8296 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8297 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8298 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8299 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8300 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8301 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8302 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8303 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8306 /* Finish debugging output for this type. */
8307 rest_of_type_compilation (enumtype, toplevel);
8309 /* If this enum is defined inside a struct, add it to
8310 struct_types. */
8311 if (warn_cxx_compat
8312 && struct_parse_info != NULL
8313 && !in_sizeof && !in_typeof && !in_alignof)
8314 struct_parse_info->struct_types.safe_push (enumtype);
8316 return enumtype;
8319 /* Build and install a CONST_DECL for one value of the
8320 current enumeration type (one that was begun with start_enum).
8321 DECL_LOC is the location of the enumerator.
8322 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8323 Return a tree-list containing the CONST_DECL and its value.
8324 Assignment of sequential values by default is handled here. */
8326 tree
8327 build_enumerator (location_t decl_loc, location_t loc,
8328 struct c_enum_contents *the_enum, tree name, tree value)
8330 tree decl, type;
8332 /* Validate and default VALUE. */
8334 if (value != 0)
8336 /* Don't issue more errors for error_mark_node (i.e. an
8337 undeclared identifier) - just ignore the value expression. */
8338 if (value == error_mark_node)
8339 value = 0;
8340 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8342 error_at (loc, "enumerator value for %qE is not an integer constant",
8343 name);
8344 value = 0;
8346 else
8348 if (TREE_CODE (value) != INTEGER_CST)
8350 value = c_fully_fold (value, false, NULL);
8351 if (TREE_CODE (value) == INTEGER_CST)
8352 pedwarn (loc, OPT_Wpedantic,
8353 "enumerator value for %qE is not an integer "
8354 "constant expression", name);
8356 if (TREE_CODE (value) != INTEGER_CST)
8358 error ("enumerator value for %qE is not an integer constant",
8359 name);
8360 value = 0;
8362 else
8364 value = default_conversion (value);
8365 constant_expression_warning (value);
8370 /* Default based on previous value. */
8371 /* It should no longer be possible to have NON_LVALUE_EXPR
8372 in the default. */
8373 if (value == 0)
8375 value = the_enum->enum_next_value;
8376 if (the_enum->enum_overflow)
8377 error_at (loc, "overflow in enumeration values");
8379 /* Even though the underlying type of an enum is unspecified, the
8380 type of enumeration constants is explicitly defined as int
8381 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8382 an extension. */
8383 else if (!int_fits_type_p (value, integer_type_node))
8384 pedwarn (loc, OPT_Wpedantic,
8385 "ISO C restricts enumerator values to range of %<int%>");
8387 /* The ISO C Standard mandates enumerators to have type int, even
8388 though the underlying type of an enum type is unspecified.
8389 However, GCC allows enumerators of any integer type as an
8390 extensions. Here we convert any enumerators that fit in an int
8391 to type int, to avoid promotions to unsigned types when comparing
8392 integers with enumerators that fit in the int range. When
8393 -pedantic is given, we would have already warned about those that
8394 don't fit. We have to do this here rather than in finish_enum
8395 because this value may be used to define more enumerators. */
8396 if (int_fits_type_p (value, integer_type_node))
8397 value = convert (integer_type_node, value);
8399 /* Set basis for default for next value. */
8400 the_enum->enum_next_value
8401 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8402 PLUS_EXPR, value, integer_one_node, 0);
8403 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8405 /* Now create a declaration for the enum value name. */
8407 type = TREE_TYPE (value);
8408 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8409 TYPE_PRECISION (integer_type_node)),
8410 (TYPE_PRECISION (type)
8411 >= TYPE_PRECISION (integer_type_node)
8412 && TYPE_UNSIGNED (type)));
8414 decl = build_decl (decl_loc, CONST_DECL, name, type);
8415 DECL_INITIAL (decl) = convert (type, value);
8416 pushdecl (decl);
8418 return tree_cons (decl, value, NULL_TREE);
8422 /* Create the FUNCTION_DECL for a function definition.
8423 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8424 the declaration; they describe the function's name and the type it returns,
8425 but twisted together in a fashion that parallels the syntax of C.
8427 This function creates a binding context for the function body
8428 as well as setting up the FUNCTION_DECL in current_function_decl.
8430 Returns 1 on success. If the DECLARATOR is not suitable for a function
8431 (it defines a datum instead), we return 0, which tells
8432 yyparse to report a parse error. */
8435 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8436 tree attributes)
8438 tree decl1, old_decl;
8439 tree restype, resdecl;
8440 location_t loc;
8442 current_function_returns_value = 0; /* Assume, until we see it does. */
8443 current_function_returns_null = 0;
8444 current_function_returns_abnormally = 0;
8445 warn_about_return_type = 0;
8446 c_switch_stack = NULL;
8448 /* Indicate no valid break/continue context by setting these variables
8449 to some non-null, non-label value. We'll notice and emit the proper
8450 error message in c_finish_bc_stmt. */
8451 c_break_label = c_cont_label = size_zero_node;
8453 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8454 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8455 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8457 /* If the declarator is not suitable for a function definition,
8458 cause a syntax error. */
8459 if (decl1 == 0
8460 || TREE_CODE (decl1) != FUNCTION_DECL)
8461 return 0;
8463 loc = DECL_SOURCE_LOCATION (decl1);
8465 c_decl_attributes (&decl1, attributes, 0);
8467 if (DECL_DECLARED_INLINE_P (decl1)
8468 && DECL_UNINLINABLE (decl1)
8469 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8470 warning_at (loc, OPT_Wattributes,
8471 "inline function %qD given attribute noinline",
8472 decl1);
8474 /* Handle gnu_inline attribute. */
8475 if (declspecs->inline_p
8476 && !flag_gnu89_inline
8477 && TREE_CODE (decl1) == FUNCTION_DECL
8478 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8479 || current_function_decl))
8481 if (declspecs->storage_class != csc_static)
8482 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8485 announce_function (decl1);
8487 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8489 error_at (loc, "return type is an incomplete type");
8490 /* Make it return void instead. */
8491 TREE_TYPE (decl1)
8492 = build_function_type (void_type_node,
8493 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8496 if (warn_about_return_type)
8497 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8498 : (warn_return_type ? OPT_Wreturn_type
8499 : OPT_Wimplicit_int),
8500 "return type defaults to %<int%>");
8502 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8503 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8504 DECL_INITIAL (decl1) = error_mark_node;
8506 /* A nested function is not global. */
8507 if (current_function_decl != 0)
8508 TREE_PUBLIC (decl1) = 0;
8510 /* If this definition isn't a prototype and we had a prototype declaration
8511 before, copy the arg type info from that prototype. */
8512 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8513 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8514 old_decl = 0;
8515 current_function_prototype_locus = UNKNOWN_LOCATION;
8516 current_function_prototype_built_in = false;
8517 current_function_prototype_arg_types = NULL_TREE;
8518 if (!prototype_p (TREE_TYPE (decl1)))
8520 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8521 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8522 TREE_TYPE (TREE_TYPE (old_decl))))
8524 if (stdarg_p (TREE_TYPE (old_decl)))
8526 warning_at (loc, 0, "%q+D defined as variadic function "
8527 "without prototype", decl1);
8528 locate_old_decl (old_decl);
8530 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8531 TREE_TYPE (decl1));
8532 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8533 current_function_prototype_built_in
8534 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8535 current_function_prototype_arg_types
8536 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8538 if (TREE_PUBLIC (decl1))
8540 /* If there is an external prototype declaration of this
8541 function, record its location but do not copy information
8542 to this decl. This may be an invisible declaration
8543 (built-in or in a scope which has finished) or simply
8544 have more refined argument types than any declaration
8545 found above. */
8546 struct c_binding *b;
8547 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8548 if (B_IN_SCOPE (b, external_scope))
8549 break;
8550 if (b)
8552 tree ext_decl, ext_type;
8553 ext_decl = b->decl;
8554 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8555 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8556 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8557 TREE_TYPE (ext_type)))
8559 current_function_prototype_locus
8560 = DECL_SOURCE_LOCATION (ext_decl);
8561 current_function_prototype_built_in
8562 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8563 current_function_prototype_arg_types
8564 = TYPE_ARG_TYPES (ext_type);
8570 /* Optionally warn of old-fashioned def with no previous prototype. */
8571 if (warn_strict_prototypes
8572 && old_decl != error_mark_node
8573 && !prototype_p (TREE_TYPE (decl1))
8574 && C_DECL_ISNT_PROTOTYPE (old_decl))
8575 warning_at (loc, OPT_Wstrict_prototypes,
8576 "function declaration isn%'t a prototype");
8577 /* Optionally warn of any global def with no previous prototype. */
8578 else if (warn_missing_prototypes
8579 && old_decl != error_mark_node
8580 && TREE_PUBLIC (decl1)
8581 && !MAIN_NAME_P (DECL_NAME (decl1))
8582 && C_DECL_ISNT_PROTOTYPE (old_decl)
8583 && !DECL_DECLARED_INLINE_P (decl1))
8584 warning_at (loc, OPT_Wmissing_prototypes,
8585 "no previous prototype for %qD", decl1);
8586 /* Optionally warn of any def with no previous prototype
8587 if the function has already been used. */
8588 else if (warn_missing_prototypes
8589 && old_decl != 0
8590 && old_decl != error_mark_node
8591 && TREE_USED (old_decl)
8592 && !prototype_p (TREE_TYPE (old_decl)))
8593 warning_at (loc, OPT_Wmissing_prototypes,
8594 "%qD was used with no prototype before its definition", decl1);
8595 /* Optionally warn of any global def with no previous declaration. */
8596 else if (warn_missing_declarations
8597 && TREE_PUBLIC (decl1)
8598 && old_decl == 0
8599 && !MAIN_NAME_P (DECL_NAME (decl1))
8600 && !DECL_DECLARED_INLINE_P (decl1))
8601 warning_at (loc, OPT_Wmissing_declarations,
8602 "no previous declaration for %qD",
8603 decl1);
8604 /* Optionally warn of any def with no previous declaration
8605 if the function has already been used. */
8606 else if (warn_missing_declarations
8607 && old_decl != 0
8608 && old_decl != error_mark_node
8609 && TREE_USED (old_decl)
8610 && C_DECL_IMPLICIT (old_decl))
8611 warning_at (loc, OPT_Wmissing_declarations,
8612 "%qD was used with no declaration before its definition", decl1);
8614 /* This function exists in static storage.
8615 (This does not mean `static' in the C sense!) */
8616 TREE_STATIC (decl1) = 1;
8618 /* This is the earliest point at which we might know the assembler
8619 name of the function. Thus, if it's set before this, die horribly. */
8620 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8622 /* If #pragma weak was used, mark the decl weak now. */
8623 if (current_scope == file_scope)
8624 maybe_apply_pragma_weak (decl1);
8626 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8627 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8629 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8630 != integer_type_node)
8631 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8632 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8633 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8634 decl1);
8636 check_main_parameter_types (decl1);
8638 if (!TREE_PUBLIC (decl1))
8639 pedwarn (loc, OPT_Wmain,
8640 "%qD is normally a non-static function", decl1);
8643 /* Record the decl so that the function name is defined.
8644 If we already have a decl for this name, and it is a FUNCTION_DECL,
8645 use the old decl. */
8647 current_function_decl = pushdecl (decl1);
8649 push_scope ();
8650 declare_parm_level ();
8652 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8653 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8654 DECL_ARTIFICIAL (resdecl) = 1;
8655 DECL_IGNORED_P (resdecl) = 1;
8656 DECL_RESULT (current_function_decl) = resdecl;
8658 start_fname_decls ();
8660 return 1;
8663 /* Subroutine of store_parm_decls which handles new-style function
8664 definitions (prototype format). The parms already have decls, so we
8665 need only record them as in effect and complain if any redundant
8666 old-style parm decls were written. */
8667 static void
8668 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8670 tree decl;
8671 c_arg_tag *tag;
8672 unsigned ix;
8674 if (current_scope->bindings)
8676 error_at (DECL_SOURCE_LOCATION (fndecl),
8677 "old-style parameter declarations in prototyped "
8678 "function definition");
8680 /* Get rid of the old-style declarations. */
8681 pop_scope ();
8682 push_scope ();
8684 /* Don't issue this warning for nested functions, and don't issue this
8685 warning if we got here because ARG_INFO_TYPES was error_mark_node
8686 (this happens when a function definition has just an ellipsis in
8687 its parameter list). */
8688 else if (!in_system_header_at (input_location)
8689 && !current_function_scope
8690 && arg_info->types != error_mark_node)
8691 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8692 "traditional C rejects ISO C style function definitions");
8694 /* Now make all the parameter declarations visible in the function body.
8695 We can bypass most of the grunt work of pushdecl. */
8696 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8698 DECL_CONTEXT (decl) = current_function_decl;
8699 if (DECL_NAME (decl))
8701 bind (DECL_NAME (decl), decl, current_scope,
8702 /*invisible=*/false, /*nested=*/false,
8703 UNKNOWN_LOCATION);
8704 if (!TREE_USED (decl))
8705 warn_if_shadowing (decl);
8707 else
8708 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8711 /* Record the parameter list in the function declaration. */
8712 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8714 /* Now make all the ancillary declarations visible, likewise. */
8715 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8717 DECL_CONTEXT (decl) = current_function_decl;
8718 if (DECL_NAME (decl))
8719 bind (DECL_NAME (decl), decl, current_scope,
8720 /*invisible=*/false,
8721 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8722 UNKNOWN_LOCATION);
8725 /* And all the tag declarations. */
8726 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8727 if (tag->id)
8728 bind (tag->id, tag->type, current_scope,
8729 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8732 /* Subroutine of store_parm_decls which handles old-style function
8733 definitions (separate parameter list and declarations). */
8735 static void
8736 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8738 struct c_binding *b;
8739 tree parm, decl, last;
8740 tree parmids = arg_info->parms;
8741 hash_set<tree> seen_args;
8743 if (!in_system_header_at (input_location))
8744 warning_at (DECL_SOURCE_LOCATION (fndecl),
8745 OPT_Wold_style_definition, "old-style function definition");
8747 /* Match each formal parameter name with its declaration. Save each
8748 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8749 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8751 if (TREE_VALUE (parm) == 0)
8753 error_at (DECL_SOURCE_LOCATION (fndecl),
8754 "parameter name missing from parameter list");
8755 TREE_PURPOSE (parm) = 0;
8756 continue;
8759 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8760 if (b && B_IN_CURRENT_SCOPE (b))
8762 decl = b->decl;
8763 /* Skip erroneous parameters. */
8764 if (decl == error_mark_node)
8765 continue;
8766 /* If we got something other than a PARM_DECL it is an error. */
8767 if (TREE_CODE (decl) != PARM_DECL)
8769 error_at (DECL_SOURCE_LOCATION (decl),
8770 "%qD declared as a non-parameter", decl);
8771 continue;
8773 /* If the declaration is already marked, we have a duplicate
8774 name. Complain and ignore the duplicate. */
8775 else if (seen_args.contains (decl))
8777 error_at (DECL_SOURCE_LOCATION (decl),
8778 "multiple parameters named %qD", decl);
8779 TREE_PURPOSE (parm) = 0;
8780 continue;
8782 /* If the declaration says "void", complain and turn it into
8783 an int. */
8784 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8786 error_at (DECL_SOURCE_LOCATION (decl),
8787 "parameter %qD declared with void type", decl);
8788 TREE_TYPE (decl) = integer_type_node;
8789 DECL_ARG_TYPE (decl) = integer_type_node;
8790 layout_decl (decl, 0);
8792 warn_if_shadowing (decl);
8794 /* If no declaration found, default to int. */
8795 else
8797 /* FIXME diagnostics: This should be the location of the argument,
8798 not the FNDECL. E.g., for an old-style declaration
8800 int f10(v) { blah; }
8802 We should use the location of the V, not the F10.
8803 Unfortunately, the V is an IDENTIFIER_NODE which has no
8804 location. In the future we need locations for c_arg_info
8805 entries.
8807 See gcc.dg/Wshadow-3.c for an example of this problem. */
8808 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8809 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8810 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8811 pushdecl (decl);
8812 warn_if_shadowing (decl);
8814 if (flag_isoc99)
8815 pedwarn (DECL_SOURCE_LOCATION (decl),
8816 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8817 decl);
8818 else
8819 warning_at (DECL_SOURCE_LOCATION (decl),
8820 OPT_Wmissing_parameter_type,
8821 "type of %qD defaults to %<int%>", decl);
8824 TREE_PURPOSE (parm) = decl;
8825 seen_args.add (decl);
8828 /* Now examine the parms chain for incomplete declarations
8829 and declarations with no corresponding names. */
8831 for (b = current_scope->bindings; b; b = b->prev)
8833 parm = b->decl;
8834 if (TREE_CODE (parm) != PARM_DECL)
8835 continue;
8837 if (TREE_TYPE (parm) != error_mark_node
8838 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8840 error_at (DECL_SOURCE_LOCATION (parm),
8841 "parameter %qD has incomplete type", parm);
8842 TREE_TYPE (parm) = error_mark_node;
8845 if (!seen_args.contains (parm))
8847 error_at (DECL_SOURCE_LOCATION (parm),
8848 "declaration for parameter %qD but no such parameter",
8849 parm);
8851 /* Pretend the parameter was not missing.
8852 This gets us to a standard state and minimizes
8853 further error messages. */
8854 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8858 /* Chain the declarations together in the order of the list of
8859 names. Store that chain in the function decl, replacing the
8860 list of names. Update the current scope to match. */
8861 DECL_ARGUMENTS (fndecl) = 0;
8863 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8864 if (TREE_PURPOSE (parm))
8865 break;
8866 if (parm && TREE_PURPOSE (parm))
8868 last = TREE_PURPOSE (parm);
8869 DECL_ARGUMENTS (fndecl) = last;
8871 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8872 if (TREE_PURPOSE (parm))
8874 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8875 last = TREE_PURPOSE (parm);
8877 DECL_CHAIN (last) = 0;
8880 /* If there was a previous prototype,
8881 set the DECL_ARG_TYPE of each argument according to
8882 the type previously specified, and report any mismatches. */
8884 if (current_function_prototype_arg_types)
8886 tree type;
8887 for (parm = DECL_ARGUMENTS (fndecl),
8888 type = current_function_prototype_arg_types;
8889 parm || (type && TREE_VALUE (type) != error_mark_node
8890 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8891 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8893 if (parm == 0 || type == 0
8894 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8896 if (current_function_prototype_built_in)
8897 warning_at (DECL_SOURCE_LOCATION (fndecl),
8898 0, "number of arguments doesn%'t match "
8899 "built-in prototype");
8900 else
8902 /* FIXME diagnostics: This should be the location of
8903 FNDECL, but there is bug when a prototype is
8904 declared inside function context, but defined
8905 outside of it (e.g., gcc.dg/pr15698-2.c). In
8906 which case FNDECL gets the location of the
8907 prototype, not the definition. */
8908 error_at (input_location,
8909 "number of arguments doesn%'t match prototype");
8911 error_at (current_function_prototype_locus,
8912 "prototype declaration");
8914 break;
8916 /* Type for passing arg must be consistent with that
8917 declared for the arg. ISO C says we take the unqualified
8918 type for parameters declared with qualified type. */
8919 if (TREE_TYPE (parm) != error_mark_node
8920 && TREE_TYPE (type) != error_mark_node
8921 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8922 != TYPE_ATOMIC (TREE_VALUE (type)))
8923 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8924 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8926 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8927 == TYPE_ATOMIC (TREE_VALUE (type)))
8928 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8929 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8931 /* Adjust argument to match prototype. E.g. a previous
8932 `int foo(float);' prototype causes
8933 `int foo(x) float x; {...}' to be treated like
8934 `int foo(float x) {...}'. This is particularly
8935 useful for argument types like uid_t. */
8936 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8938 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8939 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8940 && TYPE_PRECISION (TREE_TYPE (parm))
8941 < TYPE_PRECISION (integer_type_node))
8942 DECL_ARG_TYPE (parm)
8943 = c_type_promotes_to (TREE_TYPE (parm));
8945 /* ??? Is it possible to get here with a
8946 built-in prototype or will it always have
8947 been diagnosed as conflicting with an
8948 old-style definition and discarded? */
8949 if (current_function_prototype_built_in)
8950 warning_at (DECL_SOURCE_LOCATION (parm),
8951 OPT_Wpedantic, "promoted argument %qD "
8952 "doesn%'t match built-in prototype", parm);
8953 else
8955 pedwarn (DECL_SOURCE_LOCATION (parm),
8956 OPT_Wpedantic, "promoted argument %qD "
8957 "doesn%'t match prototype", parm);
8958 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8959 "prototype declaration");
8962 else
8964 if (current_function_prototype_built_in)
8965 warning_at (DECL_SOURCE_LOCATION (parm),
8966 0, "argument %qD doesn%'t match "
8967 "built-in prototype", parm);
8968 else
8970 error_at (DECL_SOURCE_LOCATION (parm),
8971 "argument %qD doesn%'t match prototype", parm);
8972 error_at (current_function_prototype_locus,
8973 "prototype declaration");
8978 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8981 /* Otherwise, create a prototype that would match. */
8983 else
8985 tree actual = 0, last = 0, type;
8987 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8989 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8990 if (last)
8991 TREE_CHAIN (last) = type;
8992 else
8993 actual = type;
8994 last = type;
8996 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8997 if (last)
8998 TREE_CHAIN (last) = type;
8999 else
9000 actual = type;
9002 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9003 of the type of this function, but we need to avoid having this
9004 affect the types of other similarly-typed functions, so we must
9005 first force the generation of an identical (but separate) type
9006 node for the relevant function type. The new node we create
9007 will be a variant of the main variant of the original function
9008 type. */
9010 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9012 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9016 /* Store parameter declarations passed in ARG_INFO into the current
9017 function declaration. */
9019 void
9020 store_parm_decls_from (struct c_arg_info *arg_info)
9022 current_function_arg_info = arg_info;
9023 store_parm_decls ();
9026 /* Called by walk_tree to look for and update context-less labels. */
9028 static tree
9029 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9031 if (TREE_CODE (*tp) == LABEL_EXPR
9032 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9034 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9035 *walk_subtrees = 0;
9038 return NULL_TREE;
9041 /* Store the parameter declarations into the current function declaration.
9042 This is called after parsing the parameter declarations, before
9043 digesting the body of the function.
9045 For an old-style definition, construct a prototype out of the old-style
9046 parameter declarations and inject it into the function's type. */
9048 void
9049 store_parm_decls (void)
9051 tree fndecl = current_function_decl;
9052 bool proto;
9054 /* The argument information block for FNDECL. */
9055 struct c_arg_info *arg_info = current_function_arg_info;
9056 current_function_arg_info = 0;
9058 /* True if this definition is written with a prototype. Note:
9059 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9060 list in a function definition as equivalent to (void) -- an
9061 empty argument list specifies the function has no parameters,
9062 but only (void) sets up a prototype for future calls. */
9063 proto = arg_info->types != 0;
9065 if (proto)
9066 store_parm_decls_newstyle (fndecl, arg_info);
9067 else
9068 store_parm_decls_oldstyle (fndecl, arg_info);
9070 /* The next call to push_scope will be a function body. */
9072 next_is_function_body = true;
9074 /* Write a record describing this function definition to the prototypes
9075 file (if requested). */
9077 gen_aux_info_record (fndecl, 1, 0, proto);
9079 /* Initialize the RTL code for the function. */
9080 allocate_struct_function (fndecl, false);
9082 if (warn_unused_local_typedefs)
9083 cfun->language = ggc_cleared_alloc<language_function> ();
9085 /* Begin the statement tree for this function. */
9086 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9088 /* ??? Insert the contents of the pending sizes list into the function
9089 to be evaluated. The only reason left to have this is
9090 void foo(int n, int array[n++])
9091 because we throw away the array type in favor of a pointer type, and
9092 thus won't naturally see the SAVE_EXPR containing the increment. All
9093 other pending sizes would be handled by gimplify_parameters. */
9094 if (arg_info->pending_sizes)
9096 /* In very special circumstances, e.g. for code like
9097 _Atomic int i = 5;
9098 void f (int a[i += 2]) {}
9099 we need to execute the atomic assignment on function entry.
9100 But in this case, it is not just a straight store, it has the
9101 op= form, which means that build_atomic_assign has generated
9102 gotos, labels, etc. Because at that time the function decl
9103 for F has not been created yet, those labels do not have any
9104 function context. But we have the fndecl now, so update the
9105 labels accordingly. gimplify_expr would crash otherwise. */
9106 walk_tree_without_duplicates (&arg_info->pending_sizes,
9107 set_labels_context_r, fndecl);
9108 add_stmt (arg_info->pending_sizes);
9112 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9113 c_finish_omp_declare_simd for function prototypes. No diagnostics
9114 should be done. */
9116 void
9117 temp_store_parm_decls (tree fndecl, tree parms)
9119 push_scope ();
9120 for (tree p = parms; p; p = DECL_CHAIN (p))
9122 DECL_CONTEXT (p) = fndecl;
9123 if (DECL_NAME (p))
9124 bind (DECL_NAME (p), p, current_scope,
9125 /*invisible=*/false, /*nested=*/false,
9126 UNKNOWN_LOCATION);
9130 /* Undo what temp_store_parm_decls did. */
9132 void
9133 temp_pop_parm_decls (void)
9135 /* Clear all bindings in this temporary scope, so that
9136 pop_scope doesn't create a BLOCK. */
9137 struct c_binding *b = current_scope->bindings;
9138 current_scope->bindings = NULL;
9139 for (; b; b = free_binding_and_advance (b))
9141 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9142 || b->decl == error_mark_node);
9143 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9144 I_SYMBOL_BINDING (b->id) = b->shadowed;
9145 if (b->shadowed && b->shadowed->u.type)
9146 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9148 pop_scope ();
9152 /* Finish up a function declaration and compile that function
9153 all the way to assembler language output. Then free the storage
9154 for the function definition.
9156 This is called after parsing the body of the function definition. */
9158 void
9159 finish_function (void)
9161 tree fndecl = current_function_decl;
9163 if (c_dialect_objc ())
9164 objc_finish_function ();
9166 if (TREE_CODE (fndecl) == FUNCTION_DECL
9167 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9169 tree args = DECL_ARGUMENTS (fndecl);
9170 for (; args; args = DECL_CHAIN (args))
9172 tree type = TREE_TYPE (args);
9173 if (INTEGRAL_TYPE_P (type)
9174 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9175 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9179 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9180 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9182 /* Must mark the RESULT_DECL as being in this function. */
9184 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9185 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9187 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9188 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9189 == integer_type_node && flag_isoc99)
9191 /* Hack. We don't want the middle-end to warn that this return
9192 is unreachable, so we mark its location as special. Using
9193 UNKNOWN_LOCATION has the problem that it gets clobbered in
9194 annotate_one_with_locus. A cleaner solution might be to
9195 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9197 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9200 /* Tie off the statement tree for this function. */
9201 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9203 /* If the function has _Cilk_spawn in front of a function call inside it
9204 i.e. it is a spawning function, then add the appropriate Cilk plus
9205 functions inside. */
9206 if (fn_contains_cilk_spawn_p (cfun))
9207 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9209 finish_fname_decls ();
9211 /* Complain if there's just no return statement. */
9212 if (warn_return_type
9213 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9214 && !current_function_returns_value && !current_function_returns_null
9215 /* Don't complain if we are no-return. */
9216 && !current_function_returns_abnormally
9217 /* Don't complain if we are declared noreturn. */
9218 && !TREE_THIS_VOLATILE (fndecl)
9219 /* Don't warn for main(). */
9220 && !MAIN_NAME_P (DECL_NAME (fndecl))
9221 /* Or if they didn't actually specify a return type. */
9222 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9223 /* Normally, with -Wreturn-type, flow will complain, but we might
9224 optimize out static functions. */
9225 && !TREE_PUBLIC (fndecl))
9227 warning (OPT_Wreturn_type,
9228 "no return statement in function returning non-void");
9229 TREE_NO_WARNING (fndecl) = 1;
9232 /* Complain about parameters that are only set, but never otherwise used. */
9233 if (warn_unused_but_set_parameter)
9235 tree decl;
9237 for (decl = DECL_ARGUMENTS (fndecl);
9238 decl;
9239 decl = DECL_CHAIN (decl))
9240 if (TREE_USED (decl)
9241 && TREE_CODE (decl) == PARM_DECL
9242 && !DECL_READ_P (decl)
9243 && DECL_NAME (decl)
9244 && !DECL_ARTIFICIAL (decl)
9245 && !TREE_NO_WARNING (decl))
9246 warning_at (DECL_SOURCE_LOCATION (decl),
9247 OPT_Wunused_but_set_parameter,
9248 "parameter %qD set but not used", decl);
9251 /* Complain about locally defined typedefs that are not used in this
9252 function. */
9253 maybe_warn_unused_local_typedefs ();
9255 /* Possibly warn about unused parameters. */
9256 if (warn_unused_parameter)
9257 do_warn_unused_parameter (fndecl);
9259 /* Store the end of the function, so that we get good line number
9260 info for the epilogue. */
9261 cfun->function_end_locus = input_location;
9263 /* Finalize the ELF visibility for the function. */
9264 c_determine_visibility (fndecl);
9266 /* For GNU C extern inline functions disregard inline limits. */
9267 if (DECL_EXTERNAL (fndecl)
9268 && DECL_DECLARED_INLINE_P (fndecl)
9269 && (flag_gnu89_inline
9270 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9271 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9273 /* Genericize before inlining. Delay genericizing nested functions
9274 until their parent function is genericized. Since finalizing
9275 requires GENERIC, delay that as well. */
9277 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9278 && !undef_nested_function)
9280 if (!decl_function_context (fndecl))
9282 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9283 c_genericize (fndecl);
9285 /* ??? Objc emits functions after finalizing the compilation unit.
9286 This should be cleaned up later and this conditional removed. */
9287 if (symtab->global_info_ready)
9289 cgraph_node::add_new_function (fndecl, false);
9290 return;
9292 cgraph_node::finalize_function (fndecl, false);
9294 else
9296 /* Register this function with cgraph just far enough to get it
9297 added to our parent's nested function list. Handy, since the
9298 C front end doesn't have such a list. */
9299 (void) cgraph_node::get_create (fndecl);
9303 if (!decl_function_context (fndecl))
9304 undef_nested_function = false;
9306 if (cfun->language != NULL)
9308 ggc_free (cfun->language);
9309 cfun->language = NULL;
9312 /* We're leaving the context of this function, so zap cfun.
9313 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9314 tree_rest_of_compilation. */
9315 set_cfun (NULL);
9316 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9317 current_function_decl = NULL;
9320 /* Check the declarations given in a for-loop for satisfying the C99
9321 constraints. If exactly one such decl is found, return it. LOC is
9322 the location of the opening parenthesis of the for loop. The last
9323 parameter allows you to control the "for loop initial declarations
9324 are only allowed in C99 mode". Normally, you should pass
9325 flag_isoc99 as that parameter. But in some cases (Objective-C
9326 foreach loop, for example) we want to run the checks in this
9327 function even if not in C99 mode, so we allow the caller to turn
9328 off the error about not being in C99 mode.
9331 tree
9332 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9334 struct c_binding *b;
9335 tree one_decl = NULL_TREE;
9336 int n_decls = 0;
9338 if (!turn_off_iso_c99_error)
9340 static bool hint = true;
9341 /* If we get here, declarations have been used in a for loop without
9342 the C99 for loop scope. This doesn't make much sense, so don't
9343 allow it. */
9344 error_at (loc, "%<for%> loop initial declarations "
9345 "are only allowed in C99 or C11 mode");
9346 if (hint)
9348 inform (loc,
9349 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9350 "to compile your code");
9351 hint = false;
9353 return NULL_TREE;
9355 /* C99 subclause 6.8.5 paragraph 3:
9357 [#3] The declaration part of a for statement shall only
9358 declare identifiers for objects having storage class auto or
9359 register.
9361 It isn't clear whether, in this sentence, "identifiers" binds to
9362 "shall only declare" or to "objects" - that is, whether all identifiers
9363 declared must be identifiers for objects, or whether the restriction
9364 only applies to those that are. (A question on this in comp.std.c
9365 in November 2000 received no answer.) We implement the strictest
9366 interpretation, to avoid creating an extension which later causes
9367 problems. */
9369 for (b = current_scope->bindings; b; b = b->prev)
9371 tree id = b->id;
9372 tree decl = b->decl;
9374 if (!id)
9375 continue;
9377 switch (TREE_CODE (decl))
9379 case VAR_DECL:
9381 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9382 if (TREE_STATIC (decl))
9383 error_at (decl_loc,
9384 "declaration of static variable %qD in %<for%> loop "
9385 "initial declaration", decl);
9386 else if (DECL_EXTERNAL (decl))
9387 error_at (decl_loc,
9388 "declaration of %<extern%> variable %qD in %<for%> loop "
9389 "initial declaration", decl);
9391 break;
9393 case RECORD_TYPE:
9394 error_at (loc,
9395 "%<struct %E%> declared in %<for%> loop initial "
9396 "declaration", id);
9397 break;
9398 case UNION_TYPE:
9399 error_at (loc,
9400 "%<union %E%> declared in %<for%> loop initial declaration",
9401 id);
9402 break;
9403 case ENUMERAL_TYPE:
9404 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9405 "initial declaration", id);
9406 break;
9407 default:
9408 error_at (loc, "declaration of non-variable "
9409 "%qD in %<for%> loop initial declaration", decl);
9412 n_decls++;
9413 one_decl = decl;
9416 return n_decls == 1 ? one_decl : NULL_TREE;
9419 /* Save and reinitialize the variables
9420 used during compilation of a C function. */
9422 void
9423 c_push_function_context (void)
9425 struct language_function *p = cfun->language;
9426 /* cfun->language might have been already allocated by the use of
9427 -Wunused-local-typedefs. In that case, just re-use it. */
9428 if (p == NULL)
9429 cfun->language = p = ggc_cleared_alloc<language_function> ();
9431 p->base.x_stmt_tree = c_stmt_tree;
9432 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9433 p->x_break_label = c_break_label;
9434 p->x_cont_label = c_cont_label;
9435 p->x_switch_stack = c_switch_stack;
9436 p->arg_info = current_function_arg_info;
9437 p->returns_value = current_function_returns_value;
9438 p->returns_null = current_function_returns_null;
9439 p->returns_abnormally = current_function_returns_abnormally;
9440 p->warn_about_return_type = warn_about_return_type;
9442 push_function_context ();
9445 /* Restore the variables used during compilation of a C function. */
9447 void
9448 c_pop_function_context (void)
9450 struct language_function *p;
9452 pop_function_context ();
9453 p = cfun->language;
9455 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9456 used to store data throughout the life time of the current cfun,
9457 So don't deallocate it. */
9458 if (!warn_unused_local_typedefs)
9459 cfun->language = NULL;
9461 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9462 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9464 /* Stop pointing to the local nodes about to be freed. */
9465 /* But DECL_INITIAL must remain nonzero so we know this
9466 was an actual function definition. */
9467 DECL_INITIAL (current_function_decl) = error_mark_node;
9468 DECL_ARGUMENTS (current_function_decl) = 0;
9471 c_stmt_tree = p->base.x_stmt_tree;
9472 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9473 c_break_label = p->x_break_label;
9474 c_cont_label = p->x_cont_label;
9475 c_switch_stack = p->x_switch_stack;
9476 current_function_arg_info = p->arg_info;
9477 current_function_returns_value = p->returns_value;
9478 current_function_returns_null = p->returns_null;
9479 current_function_returns_abnormally = p->returns_abnormally;
9480 warn_about_return_type = p->warn_about_return_type;
9483 /* The functions below are required for functionality of doing
9484 function at once processing in the C front end. Currently these
9485 functions are not called from anywhere in the C front end, but as
9486 these changes continue, that will change. */
9488 /* Returns the stmt_tree (if any) to which statements are currently
9489 being added. If there is no active statement-tree, NULL is
9490 returned. */
9492 stmt_tree
9493 current_stmt_tree (void)
9495 return &c_stmt_tree;
9498 /* Return the global value of T as a symbol. */
9500 tree
9501 identifier_global_value (tree t)
9503 struct c_binding *b;
9505 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9506 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9507 return b->decl;
9509 return 0;
9512 /* In C, the only C-linkage public declaration is at file scope. */
9514 tree
9515 c_linkage_bindings (tree name)
9517 return identifier_global_value (name);
9520 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9521 otherwise the name is found in ridpointers from RID_INDEX. */
9523 void
9524 record_builtin_type (enum rid rid_index, const char *name, tree type)
9526 tree id, decl;
9527 if (name == 0)
9528 id = ridpointers[(int) rid_index];
9529 else
9530 id = get_identifier (name);
9531 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9532 pushdecl (decl);
9533 if (debug_hooks->type_decl)
9534 debug_hooks->type_decl (decl, false);
9537 /* Build the void_list_node (void_type_node having been created). */
9538 tree
9539 build_void_list_node (void)
9541 tree t = build_tree_list (NULL_TREE, void_type_node);
9542 return t;
9545 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9547 struct c_parm *
9548 build_c_parm (struct c_declspecs *specs, tree attrs,
9549 struct c_declarator *declarator)
9551 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9552 ret->specs = specs;
9553 ret->attrs = attrs;
9554 ret->declarator = declarator;
9555 return ret;
9558 /* Return a declarator with nested attributes. TARGET is the inner
9559 declarator to which these attributes apply. ATTRS are the
9560 attributes. */
9562 struct c_declarator *
9563 build_attrs_declarator (tree attrs, struct c_declarator *target)
9565 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9566 ret->kind = cdk_attrs;
9567 ret->declarator = target;
9568 ret->u.attrs = attrs;
9569 return ret;
9572 /* Return a declarator for a function with arguments specified by ARGS
9573 and return type specified by TARGET. */
9575 struct c_declarator *
9576 build_function_declarator (struct c_arg_info *args,
9577 struct c_declarator *target)
9579 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9580 ret->kind = cdk_function;
9581 ret->declarator = target;
9582 ret->u.arg_info = args;
9583 return ret;
9586 /* Return a declarator for the identifier IDENT (which may be
9587 NULL_TREE for an abstract declarator). */
9589 struct c_declarator *
9590 build_id_declarator (tree ident)
9592 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9593 ret->kind = cdk_id;
9594 ret->declarator = 0;
9595 ret->u.id = ident;
9596 /* Default value - may get reset to a more precise location. */
9597 ret->id_loc = input_location;
9598 return ret;
9601 /* Return something to represent absolute declarators containing a *.
9602 TARGET is the absolute declarator that the * contains.
9603 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9604 to apply to the pointer type. */
9606 struct c_declarator *
9607 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9608 struct c_declarator *target)
9610 tree attrs;
9611 int quals = 0;
9612 struct c_declarator *itarget = target;
9613 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9614 if (type_quals_attrs)
9616 attrs = type_quals_attrs->attrs;
9617 quals = quals_from_declspecs (type_quals_attrs);
9618 if (attrs != NULL_TREE)
9619 itarget = build_attrs_declarator (attrs, target);
9621 ret->kind = cdk_pointer;
9622 ret->declarator = itarget;
9623 ret->u.pointer_quals = quals;
9624 return ret;
9627 /* Return a pointer to a structure for an empty list of declaration
9628 specifiers. */
9630 struct c_declspecs *
9631 build_null_declspecs (void)
9633 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9634 memset (ret, 0, sizeof *ret);
9635 ret->align_log = -1;
9636 ret->typespec_word = cts_none;
9637 ret->storage_class = csc_none;
9638 ret->expr_const_operands = true;
9639 ret->typespec_kind = ctsk_none;
9640 ret->address_space = ADDR_SPACE_GENERIC;
9641 return ret;
9644 /* Add the address space ADDRSPACE to the declaration specifiers
9645 SPECS, returning SPECS. */
9647 struct c_declspecs *
9648 declspecs_add_addrspace (source_location location,
9649 struct c_declspecs *specs, addr_space_t as)
9651 specs->non_sc_seen_p = true;
9652 specs->declspecs_seen_p = true;
9654 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9655 && specs->address_space != as)
9656 error ("incompatible address space qualifiers %qs and %qs",
9657 c_addr_space_name (as),
9658 c_addr_space_name (specs->address_space));
9659 else
9661 specs->address_space = as;
9662 specs->locations[cdw_address_space] = location;
9664 return specs;
9667 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9668 returning SPECS. */
9670 struct c_declspecs *
9671 declspecs_add_qual (source_location loc,
9672 struct c_declspecs *specs, tree qual)
9674 enum rid i;
9675 bool dupe = false;
9676 specs->non_sc_seen_p = true;
9677 specs->declspecs_seen_p = true;
9678 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9679 && C_IS_RESERVED_WORD (qual));
9680 i = C_RID_CODE (qual);
9681 location_t prev_loc = 0;
9682 switch (i)
9684 case RID_CONST:
9685 dupe = specs->const_p;
9686 specs->const_p = true;
9687 prev_loc = specs->locations[cdw_const];
9688 specs->locations[cdw_const] = loc;
9689 break;
9690 case RID_VOLATILE:
9691 dupe = specs->volatile_p;
9692 specs->volatile_p = true;
9693 prev_loc = specs->locations[cdw_volatile];
9694 specs->locations[cdw_volatile] = loc;
9695 break;
9696 case RID_RESTRICT:
9697 dupe = specs->restrict_p;
9698 specs->restrict_p = true;
9699 prev_loc = specs->locations[cdw_restrict];
9700 specs->locations[cdw_restrict] = loc;
9701 break;
9702 case RID_ATOMIC:
9703 dupe = specs->atomic_p;
9704 specs->atomic_p = true;
9705 prev_loc = specs->locations[cdw_atomic];
9706 specs->locations[cdw_atomic] = loc;
9707 break;
9708 default:
9709 gcc_unreachable ();
9711 if (dupe)
9713 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9714 "duplicate %qE declaration specifier", qual);
9715 if (!warned
9716 && warn_duplicate_decl_specifier
9717 && prev_loc >= RESERVED_LOCATION_COUNT
9718 && !from_macro_expansion_at (prev_loc)
9719 && !from_macro_expansion_at (loc))
9720 warning_at (loc, OPT_Wduplicate_decl_specifier,
9721 "duplicate %qE declaration specifier", qual);
9723 return specs;
9726 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9727 returning SPECS. */
9729 struct c_declspecs *
9730 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9731 struct c_typespec spec)
9733 tree type = spec.spec;
9734 specs->non_sc_seen_p = true;
9735 specs->declspecs_seen_p = true;
9736 specs->typespec_kind = spec.kind;
9737 if (TREE_DEPRECATED (type))
9738 specs->deprecated_p = true;
9740 /* Handle type specifier keywords. */
9741 if (TREE_CODE (type) == IDENTIFIER_NODE
9742 && C_IS_RESERVED_WORD (type)
9743 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9745 enum rid i = C_RID_CODE (type);
9746 if (specs->type)
9748 error_at (loc, "two or more data types in declaration specifiers");
9749 return specs;
9751 if ((int) i <= (int) RID_LAST_MODIFIER)
9753 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9754 bool dupe = false;
9755 switch (i)
9757 case RID_LONG:
9758 if (specs->long_long_p)
9760 error_at (loc, "%<long long long%> is too long for GCC");
9761 break;
9763 if (specs->long_p)
9765 if (specs->typespec_word == cts_double)
9767 error_at (loc,
9768 ("both %<long long%> and %<double%> in "
9769 "declaration specifiers"));
9770 break;
9772 pedwarn_c90 (loc, OPT_Wlong_long,
9773 "ISO C90 does not support %<long long%>");
9774 specs->long_long_p = 1;
9775 specs->locations[cdw_long_long] = loc;
9776 break;
9778 if (specs->short_p)
9779 error_at (loc,
9780 ("both %<long%> and %<short%> in "
9781 "declaration specifiers"));
9782 else if (specs->typespec_word == cts_auto_type)
9783 error_at (loc,
9784 ("both %<long%> and %<__auto_type%> in "
9785 "declaration specifiers"));
9786 else if (specs->typespec_word == cts_void)
9787 error_at (loc,
9788 ("both %<long%> and %<void%> in "
9789 "declaration specifiers"));
9790 else if (specs->typespec_word == cts_int_n)
9791 error_at (loc,
9792 ("both %<long%> and %<__int%d%> in "
9793 "declaration specifiers"),
9794 int_n_data[specs->int_n_idx].bitsize);
9795 else if (specs->typespec_word == cts_bool)
9796 error_at (loc,
9797 ("both %<long%> and %<_Bool%> in "
9798 "declaration specifiers"));
9799 else if (specs->typespec_word == cts_char)
9800 error_at (loc,
9801 ("both %<long%> and %<char%> in "
9802 "declaration specifiers"));
9803 else if (specs->typespec_word == cts_float)
9804 error_at (loc,
9805 ("both %<long%> and %<float%> in "
9806 "declaration specifiers"));
9807 else if (specs->typespec_word == cts_floatn_nx)
9808 error_at (loc,
9809 ("both %<long%> and %<_Float%d%s%> in "
9810 "declaration specifiers"),
9811 floatn_nx_types[specs->floatn_nx_idx].n,
9812 (floatn_nx_types[specs->floatn_nx_idx].extended
9813 ? "x"
9814 : ""));
9815 else if (specs->typespec_word == cts_dfloat32)
9816 error_at (loc,
9817 ("both %<long%> and %<_Decimal32%> in "
9818 "declaration specifiers"));
9819 else if (specs->typespec_word == cts_dfloat64)
9820 error_at (loc,
9821 ("both %<long%> and %<_Decimal64%> in "
9822 "declaration specifiers"));
9823 else if (specs->typespec_word == cts_dfloat128)
9824 error_at (loc,
9825 ("both %<long%> and %<_Decimal128%> in "
9826 "declaration specifiers"));
9827 else
9829 specs->long_p = true;
9830 specs->locations[cdw_long] = loc;
9832 break;
9833 case RID_SHORT:
9834 dupe = specs->short_p;
9835 if (specs->long_p)
9836 error_at (loc,
9837 ("both %<long%> and %<short%> in "
9838 "declaration specifiers"));
9839 else if (specs->typespec_word == cts_auto_type)
9840 error_at (loc,
9841 ("both %<short%> and %<__auto_type%> in "
9842 "declaration specifiers"));
9843 else if (specs->typespec_word == cts_void)
9844 error_at (loc,
9845 ("both %<short%> and %<void%> in "
9846 "declaration specifiers"));
9847 else if (specs->typespec_word == cts_int_n)
9848 error_at (loc,
9849 ("both %<short%> and %<__int%d%> in "
9850 "declaration specifiers"),
9851 int_n_data[specs->int_n_idx].bitsize);
9852 else if (specs->typespec_word == cts_bool)
9853 error_at (loc,
9854 ("both %<short%> and %<_Bool%> in "
9855 "declaration specifiers"));
9856 else if (specs->typespec_word == cts_char)
9857 error_at (loc,
9858 ("both %<short%> and %<char%> in "
9859 "declaration specifiers"));
9860 else if (specs->typespec_word == cts_float)
9861 error_at (loc,
9862 ("both %<short%> and %<float%> in "
9863 "declaration specifiers"));
9864 else if (specs->typespec_word == cts_double)
9865 error_at (loc,
9866 ("both %<short%> and %<double%> in "
9867 "declaration specifiers"));
9868 else if (specs->typespec_word == cts_floatn_nx)
9869 error_at (loc,
9870 ("both %<short%> and %<_Float%d%s%> in "
9871 "declaration specifiers"),
9872 floatn_nx_types[specs->floatn_nx_idx].n,
9873 (floatn_nx_types[specs->floatn_nx_idx].extended
9874 ? "x"
9875 : ""));
9876 else if (specs->typespec_word == cts_dfloat32)
9877 error_at (loc,
9878 ("both %<short%> and %<_Decimal32%> in "
9879 "declaration specifiers"));
9880 else if (specs->typespec_word == cts_dfloat64)
9881 error_at (loc,
9882 ("both %<short%> and %<_Decimal64%> in "
9883 "declaration specifiers"));
9884 else if (specs->typespec_word == cts_dfloat128)
9885 error_at (loc,
9886 ("both %<short%> and %<_Decimal128%> in "
9887 "declaration specifiers"));
9888 else
9890 specs->short_p = true;
9891 specs->locations[cdw_short] = loc;
9893 break;
9894 case RID_SIGNED:
9895 dupe = specs->signed_p;
9896 if (specs->unsigned_p)
9897 error_at (loc,
9898 ("both %<signed%> and %<unsigned%> in "
9899 "declaration specifiers"));
9900 else if (specs->typespec_word == cts_auto_type)
9901 error_at (loc,
9902 ("both %<signed%> and %<__auto_type%> in "
9903 "declaration specifiers"));
9904 else if (specs->typespec_word == cts_void)
9905 error_at (loc,
9906 ("both %<signed%> and %<void%> in "
9907 "declaration specifiers"));
9908 else if (specs->typespec_word == cts_bool)
9909 error_at (loc,
9910 ("both %<signed%> and %<_Bool%> in "
9911 "declaration specifiers"));
9912 else if (specs->typespec_word == cts_float)
9913 error_at (loc,
9914 ("both %<signed%> and %<float%> in "
9915 "declaration specifiers"));
9916 else if (specs->typespec_word == cts_double)
9917 error_at (loc,
9918 ("both %<signed%> and %<double%> in "
9919 "declaration specifiers"));
9920 else if (specs->typespec_word == cts_floatn_nx)
9921 error_at (loc,
9922 ("both %<signed%> and %<_Float%d%s%> in "
9923 "declaration specifiers"),
9924 floatn_nx_types[specs->floatn_nx_idx].n,
9925 (floatn_nx_types[specs->floatn_nx_idx].extended
9926 ? "x"
9927 : ""));
9928 else if (specs->typespec_word == cts_dfloat32)
9929 error_at (loc,
9930 ("both %<signed%> and %<_Decimal32%> in "
9931 "declaration specifiers"));
9932 else if (specs->typespec_word == cts_dfloat64)
9933 error_at (loc,
9934 ("both %<signed%> and %<_Decimal64%> in "
9935 "declaration specifiers"));
9936 else if (specs->typespec_word == cts_dfloat128)
9937 error_at (loc,
9938 ("both %<signed%> and %<_Decimal128%> in "
9939 "declaration specifiers"));
9940 else
9942 specs->signed_p = true;
9943 specs->locations[cdw_signed] = loc;
9945 break;
9946 case RID_UNSIGNED:
9947 dupe = specs->unsigned_p;
9948 if (specs->signed_p)
9949 error_at (loc,
9950 ("both %<signed%> and %<unsigned%> in "
9951 "declaration specifiers"));
9952 else if (specs->typespec_word == cts_auto_type)
9953 error_at (loc,
9954 ("both %<unsigned%> and %<__auto_type%> in "
9955 "declaration specifiers"));
9956 else if (specs->typespec_word == cts_void)
9957 error_at (loc,
9958 ("both %<unsigned%> and %<void%> in "
9959 "declaration specifiers"));
9960 else if (specs->typespec_word == cts_bool)
9961 error_at (loc,
9962 ("both %<unsigned%> and %<_Bool%> in "
9963 "declaration specifiers"));
9964 else if (specs->typespec_word == cts_float)
9965 error_at (loc,
9966 ("both %<unsigned%> and %<float%> in "
9967 "declaration specifiers"));
9968 else if (specs->typespec_word == cts_double)
9969 error_at (loc,
9970 ("both %<unsigned%> and %<double%> in "
9971 "declaration specifiers"));
9972 else if (specs->typespec_word == cts_floatn_nx)
9973 error_at (loc,
9974 ("both %<unsigned%> and %<_Float%d%s%> in "
9975 "declaration specifiers"),
9976 floatn_nx_types[specs->floatn_nx_idx].n,
9977 (floatn_nx_types[specs->floatn_nx_idx].extended
9978 ? "x"
9979 : ""));
9980 else if (specs->typespec_word == cts_dfloat32)
9981 error_at (loc,
9982 ("both %<unsigned%> and %<_Decimal32%> in "
9983 "declaration specifiers"));
9984 else if (specs->typespec_word == cts_dfloat64)
9985 error_at (loc,
9986 ("both %<unsigned%> and %<_Decimal64%> in "
9987 "declaration specifiers"));
9988 else if (specs->typespec_word == cts_dfloat128)
9989 error_at (loc,
9990 ("both %<unsigned%> and %<_Decimal128%> in "
9991 "declaration specifiers"));
9992 else
9994 specs->unsigned_p = true;
9995 specs->locations[cdw_unsigned] = loc;
9997 break;
9998 case RID_COMPLEX:
9999 dupe = specs->complex_p;
10000 if (!in_system_header_at (loc))
10001 pedwarn_c90 (loc, OPT_Wpedantic,
10002 "ISO C90 does not support complex types");
10003 if (specs->typespec_word == cts_auto_type)
10004 error_at (loc,
10005 ("both %<complex%> and %<__auto_type%> in "
10006 "declaration specifiers"));
10007 else if (specs->typespec_word == cts_void)
10008 error_at (loc,
10009 ("both %<complex%> and %<void%> in "
10010 "declaration specifiers"));
10011 else if (specs->typespec_word == cts_bool)
10012 error_at (loc,
10013 ("both %<complex%> and %<_Bool%> in "
10014 "declaration specifiers"));
10015 else if (specs->typespec_word == cts_dfloat32)
10016 error_at (loc,
10017 ("both %<complex%> and %<_Decimal32%> in "
10018 "declaration specifiers"));
10019 else if (specs->typespec_word == cts_dfloat64)
10020 error_at (loc,
10021 ("both %<complex%> and %<_Decimal64%> in "
10022 "declaration specifiers"));
10023 else if (specs->typespec_word == cts_dfloat128)
10024 error_at (loc,
10025 ("both %<complex%> and %<_Decimal128%> in "
10026 "declaration specifiers"));
10027 else if (specs->typespec_word == cts_fract)
10028 error_at (loc,
10029 ("both %<complex%> and %<_Fract%> in "
10030 "declaration specifiers"));
10031 else if (specs->typespec_word == cts_accum)
10032 error_at (loc,
10033 ("both %<complex%> and %<_Accum%> in "
10034 "declaration specifiers"));
10035 else if (specs->saturating_p)
10036 error_at (loc,
10037 ("both %<complex%> and %<_Sat%> in "
10038 "declaration specifiers"));
10039 else
10041 specs->complex_p = true;
10042 specs->locations[cdw_complex] = loc;
10044 break;
10045 case RID_SAT:
10046 dupe = specs->saturating_p;
10047 pedwarn (loc, OPT_Wpedantic,
10048 "ISO C does not support saturating types");
10049 if (specs->typespec_word == cts_int_n)
10051 error_at (loc,
10052 ("both %<_Sat%> and %<__int%d%> in "
10053 "declaration specifiers"),
10054 int_n_data[specs->int_n_idx].bitsize);
10056 else if (specs->typespec_word == cts_auto_type)
10057 error_at (loc,
10058 ("both %<_Sat%> and %<__auto_type%> in "
10059 "declaration specifiers"));
10060 else if (specs->typespec_word == cts_void)
10061 error_at (loc,
10062 ("both %<_Sat%> and %<void%> in "
10063 "declaration specifiers"));
10064 else if (specs->typespec_word == cts_bool)
10065 error_at (loc,
10066 ("both %<_Sat%> and %<_Bool%> in "
10067 "declaration specifiers"));
10068 else if (specs->typespec_word == cts_char)
10069 error_at (loc,
10070 ("both %<_Sat%> and %<char%> in "
10071 "declaration specifiers"));
10072 else if (specs->typespec_word == cts_int)
10073 error_at (loc,
10074 ("both %<_Sat%> and %<int%> in "
10075 "declaration specifiers"));
10076 else if (specs->typespec_word == cts_float)
10077 error_at (loc,
10078 ("both %<_Sat%> and %<float%> in "
10079 "declaration specifiers"));
10080 else if (specs->typespec_word == cts_double)
10081 error_at (loc,
10082 ("both %<_Sat%> and %<double%> in "
10083 "declaration specifiers"));
10084 else if (specs->typespec_word == cts_floatn_nx)
10085 error_at (loc,
10086 ("both %<_Sat%> and %<_Float%d%s%> in "
10087 "declaration specifiers"),
10088 floatn_nx_types[specs->floatn_nx_idx].n,
10089 (floatn_nx_types[specs->floatn_nx_idx].extended
10090 ? "x"
10091 : ""));
10092 else if (specs->typespec_word == cts_dfloat32)
10093 error_at (loc,
10094 ("both %<_Sat%> and %<_Decimal32%> in "
10095 "declaration specifiers"));
10096 else if (specs->typespec_word == cts_dfloat64)
10097 error_at (loc,
10098 ("both %<_Sat%> and %<_Decimal64%> in "
10099 "declaration specifiers"));
10100 else if (specs->typespec_word == cts_dfloat128)
10101 error_at (loc,
10102 ("both %<_Sat%> and %<_Decimal128%> in "
10103 "declaration specifiers"));
10104 else if (specs->complex_p)
10105 error_at (loc,
10106 ("both %<_Sat%> and %<complex%> in "
10107 "declaration specifiers"));
10108 else
10110 specs->saturating_p = true;
10111 specs->locations[cdw_saturating] = loc;
10113 break;
10114 default:
10115 gcc_unreachable ();
10118 if (dupe)
10119 error_at (loc, "duplicate %qE", type);
10121 return specs;
10123 else
10125 /* "void", "_Bool", "char", "int", "float", "double",
10126 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10127 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10128 "__auto_type". */
10129 if (specs->typespec_word != cts_none)
10131 error_at (loc,
10132 "two or more data types in declaration specifiers");
10133 return specs;
10135 switch (i)
10137 case RID_AUTO_TYPE:
10138 if (specs->long_p)
10139 error_at (loc,
10140 ("both %<long%> and %<__auto_type%> in "
10141 "declaration specifiers"));
10142 else if (specs->short_p)
10143 error_at (loc,
10144 ("both %<short%> and %<__auto_type%> in "
10145 "declaration specifiers"));
10146 else if (specs->signed_p)
10147 error_at (loc,
10148 ("both %<signed%> and %<__auto_type%> in "
10149 "declaration specifiers"));
10150 else if (specs->unsigned_p)
10151 error_at (loc,
10152 ("both %<unsigned%> and %<__auto_type%> in "
10153 "declaration specifiers"));
10154 else if (specs->complex_p)
10155 error_at (loc,
10156 ("both %<complex%> and %<__auto_type%> in "
10157 "declaration specifiers"));
10158 else if (specs->saturating_p)
10159 error_at (loc,
10160 ("both %<_Sat%> and %<__auto_type%> in "
10161 "declaration specifiers"));
10162 else
10164 specs->typespec_word = cts_auto_type;
10165 specs->locations[cdw_typespec] = loc;
10167 return specs;
10168 case RID_INT_N_0:
10169 case RID_INT_N_1:
10170 case RID_INT_N_2:
10171 case RID_INT_N_3:
10172 specs->int_n_idx = i - RID_INT_N_0;
10173 if (!in_system_header_at (input_location))
10174 pedwarn (loc, OPT_Wpedantic,
10175 "ISO C does not support %<__int%d%> types",
10176 int_n_data[specs->int_n_idx].bitsize);
10178 if (specs->long_p)
10179 error_at (loc,
10180 ("both %<__int%d%> and %<long%> in "
10181 "declaration specifiers"),
10182 int_n_data[specs->int_n_idx].bitsize);
10183 else if (specs->saturating_p)
10184 error_at (loc,
10185 ("both %<_Sat%> and %<__int%d%> in "
10186 "declaration specifiers"),
10187 int_n_data[specs->int_n_idx].bitsize);
10188 else if (specs->short_p)
10189 error_at (loc,
10190 ("both %<__int%d%> and %<short%> in "
10191 "declaration specifiers"),
10192 int_n_data[specs->int_n_idx].bitsize);
10193 else if (! int_n_enabled_p[specs->int_n_idx])
10195 specs->typespec_word = cts_int_n;
10196 error_at (loc,
10197 "%<__int%d%> is not supported on this target",
10198 int_n_data[specs->int_n_idx].bitsize);
10200 else
10202 specs->typespec_word = cts_int_n;
10203 specs->locations[cdw_typespec] = loc;
10205 return specs;
10206 case RID_VOID:
10207 if (specs->long_p)
10208 error_at (loc,
10209 ("both %<long%> and %<void%> in "
10210 "declaration specifiers"));
10211 else if (specs->short_p)
10212 error_at (loc,
10213 ("both %<short%> and %<void%> in "
10214 "declaration specifiers"));
10215 else if (specs->signed_p)
10216 error_at (loc,
10217 ("both %<signed%> and %<void%> in "
10218 "declaration specifiers"));
10219 else if (specs->unsigned_p)
10220 error_at (loc,
10221 ("both %<unsigned%> and %<void%> in "
10222 "declaration specifiers"));
10223 else if (specs->complex_p)
10224 error_at (loc,
10225 ("both %<complex%> and %<void%> in "
10226 "declaration specifiers"));
10227 else if (specs->saturating_p)
10228 error_at (loc,
10229 ("both %<_Sat%> and %<void%> in "
10230 "declaration specifiers"));
10231 else
10233 specs->typespec_word = cts_void;
10234 specs->locations[cdw_typespec] = loc;
10236 return specs;
10237 case RID_BOOL:
10238 if (!in_system_header_at (loc))
10239 pedwarn_c90 (loc, OPT_Wpedantic,
10240 "ISO C90 does not support boolean types");
10241 if (specs->long_p)
10242 error_at (loc,
10243 ("both %<long%> and %<_Bool%> in "
10244 "declaration specifiers"));
10245 else if (specs->short_p)
10246 error_at (loc,
10247 ("both %<short%> and %<_Bool%> in "
10248 "declaration specifiers"));
10249 else if (specs->signed_p)
10250 error_at (loc,
10251 ("both %<signed%> and %<_Bool%> in "
10252 "declaration specifiers"));
10253 else if (specs->unsigned_p)
10254 error_at (loc,
10255 ("both %<unsigned%> and %<_Bool%> in "
10256 "declaration specifiers"));
10257 else if (specs->complex_p)
10258 error_at (loc,
10259 ("both %<complex%> and %<_Bool%> in "
10260 "declaration specifiers"));
10261 else if (specs->saturating_p)
10262 error_at (loc,
10263 ("both %<_Sat%> and %<_Bool%> in "
10264 "declaration specifiers"));
10265 else
10267 specs->typespec_word = cts_bool;
10268 specs->locations[cdw_typespec] = loc;
10270 return specs;
10271 case RID_CHAR:
10272 if (specs->long_p)
10273 error_at (loc,
10274 ("both %<long%> and %<char%> in "
10275 "declaration specifiers"));
10276 else if (specs->short_p)
10277 error_at (loc,
10278 ("both %<short%> and %<char%> in "
10279 "declaration specifiers"));
10280 else if (specs->saturating_p)
10281 error_at (loc,
10282 ("both %<_Sat%> and %<char%> in "
10283 "declaration specifiers"));
10284 else
10286 specs->typespec_word = cts_char;
10287 specs->locations[cdw_typespec] = loc;
10289 return specs;
10290 case RID_INT:
10291 if (specs->saturating_p)
10292 error_at (loc,
10293 ("both %<_Sat%> and %<int%> in "
10294 "declaration specifiers"));
10295 else
10297 specs->typespec_word = cts_int;
10298 specs->locations[cdw_typespec] = loc;
10300 return specs;
10301 case RID_FLOAT:
10302 if (specs->long_p)
10303 error_at (loc,
10304 ("both %<long%> and %<float%> in "
10305 "declaration specifiers"));
10306 else if (specs->short_p)
10307 error_at (loc,
10308 ("both %<short%> and %<float%> in "
10309 "declaration specifiers"));
10310 else if (specs->signed_p)
10311 error_at (loc,
10312 ("both %<signed%> and %<float%> in "
10313 "declaration specifiers"));
10314 else if (specs->unsigned_p)
10315 error_at (loc,
10316 ("both %<unsigned%> and %<float%> in "
10317 "declaration specifiers"));
10318 else if (specs->saturating_p)
10319 error_at (loc,
10320 ("both %<_Sat%> and %<float%> in "
10321 "declaration specifiers"));
10322 else
10324 specs->typespec_word = cts_float;
10325 specs->locations[cdw_typespec] = loc;
10327 return specs;
10328 case RID_DOUBLE:
10329 if (specs->long_long_p)
10330 error_at (loc,
10331 ("both %<long long%> and %<double%> in "
10332 "declaration specifiers"));
10333 else if (specs->short_p)
10334 error_at (loc,
10335 ("both %<short%> and %<double%> in "
10336 "declaration specifiers"));
10337 else if (specs->signed_p)
10338 error_at (loc,
10339 ("both %<signed%> and %<double%> in "
10340 "declaration specifiers"));
10341 else if (specs->unsigned_p)
10342 error_at (loc,
10343 ("both %<unsigned%> and %<double%> in "
10344 "declaration specifiers"));
10345 else if (specs->saturating_p)
10346 error_at (loc,
10347 ("both %<_Sat%> and %<double%> in "
10348 "declaration specifiers"));
10349 else
10351 specs->typespec_word = cts_double;
10352 specs->locations[cdw_typespec] = loc;
10354 return specs;
10355 CASE_RID_FLOATN_NX:
10356 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10357 if (!in_system_header_at (input_location))
10358 pedwarn (loc, OPT_Wpedantic,
10359 "ISO C does not support the %<_Float%d%s%> type",
10360 floatn_nx_types[specs->floatn_nx_idx].n,
10361 (floatn_nx_types[specs->floatn_nx_idx].extended
10362 ? "x"
10363 : ""));
10365 if (specs->long_p)
10366 error_at (loc,
10367 ("both %<long%> and %<_Float%d%s%> in "
10368 "declaration specifiers"),
10369 floatn_nx_types[specs->floatn_nx_idx].n,
10370 (floatn_nx_types[specs->floatn_nx_idx].extended
10371 ? "x"
10372 : ""));
10373 else if (specs->short_p)
10374 error_at (loc,
10375 ("both %<short%> and %<_Float%d%s%> in "
10376 "declaration specifiers"),
10377 floatn_nx_types[specs->floatn_nx_idx].n,
10378 (floatn_nx_types[specs->floatn_nx_idx].extended
10379 ? "x"
10380 : ""));
10381 else if (specs->signed_p)
10382 error_at (loc,
10383 ("both %<signed%> and %<_Float%d%s%> in "
10384 "declaration specifiers"),
10385 floatn_nx_types[specs->floatn_nx_idx].n,
10386 (floatn_nx_types[specs->floatn_nx_idx].extended
10387 ? "x"
10388 : ""));
10389 else if (specs->unsigned_p)
10390 error_at (loc,
10391 ("both %<unsigned%> and %<_Float%d%s%> in "
10392 "declaration specifiers"),
10393 floatn_nx_types[specs->floatn_nx_idx].n,
10394 (floatn_nx_types[specs->floatn_nx_idx].extended
10395 ? "x"
10396 : ""));
10397 else if (specs->saturating_p)
10398 error_at (loc,
10399 ("both %<_Sat%> and %<_Float%d%s%> in "
10400 "declaration specifiers"),
10401 floatn_nx_types[specs->floatn_nx_idx].n,
10402 (floatn_nx_types[specs->floatn_nx_idx].extended
10403 ? "x"
10404 : ""));
10405 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10407 specs->typespec_word = cts_floatn_nx;
10408 error_at (loc,
10409 "%<_Float%d%s%> is not supported on this target",
10410 floatn_nx_types[specs->floatn_nx_idx].n,
10411 (floatn_nx_types[specs->floatn_nx_idx].extended
10412 ? "x"
10413 : ""));
10415 else
10417 specs->typespec_word = cts_floatn_nx;
10418 specs->locations[cdw_typespec] = loc;
10420 return specs;
10421 case RID_DFLOAT32:
10422 case RID_DFLOAT64:
10423 case RID_DFLOAT128:
10425 const char *str;
10426 if (i == RID_DFLOAT32)
10427 str = "_Decimal32";
10428 else if (i == RID_DFLOAT64)
10429 str = "_Decimal64";
10430 else
10431 str = "_Decimal128";
10432 if (specs->long_long_p)
10433 error_at (loc,
10434 ("both %<long long%> and %<%s%> in "
10435 "declaration specifiers"),
10436 str);
10437 if (specs->long_p)
10438 error_at (loc,
10439 ("both %<long%> and %<%s%> in "
10440 "declaration specifiers"),
10441 str);
10442 else if (specs->short_p)
10443 error_at (loc,
10444 ("both %<short%> and %<%s%> in "
10445 "declaration specifiers"),
10446 str);
10447 else if (specs->signed_p)
10448 error_at (loc,
10449 ("both %<signed%> and %<%s%> in "
10450 "declaration specifiers"),
10451 str);
10452 else if (specs->unsigned_p)
10453 error_at (loc,
10454 ("both %<unsigned%> and %<%s%> in "
10455 "declaration specifiers"),
10456 str);
10457 else if (specs->complex_p)
10458 error_at (loc,
10459 ("both %<complex%> and %<%s%> in "
10460 "declaration specifiers"),
10461 str);
10462 else if (specs->saturating_p)
10463 error_at (loc,
10464 ("both %<_Sat%> and %<%s%> in "
10465 "declaration specifiers"),
10466 str);
10467 else if (i == RID_DFLOAT32)
10468 specs->typespec_word = cts_dfloat32;
10469 else if (i == RID_DFLOAT64)
10470 specs->typespec_word = cts_dfloat64;
10471 else
10472 specs->typespec_word = cts_dfloat128;
10473 specs->locations[cdw_typespec] = loc;
10475 if (!targetm.decimal_float_supported_p ())
10476 error_at (loc,
10477 ("decimal floating point not supported "
10478 "for this target"));
10479 pedwarn (loc, OPT_Wpedantic,
10480 "ISO C does not support decimal floating point");
10481 return specs;
10482 case RID_FRACT:
10483 case RID_ACCUM:
10485 const char *str;
10486 if (i == RID_FRACT)
10487 str = "_Fract";
10488 else
10489 str = "_Accum";
10490 if (specs->complex_p)
10491 error_at (loc,
10492 ("both %<complex%> and %<%s%> in "
10493 "declaration specifiers"),
10494 str);
10495 else if (i == RID_FRACT)
10496 specs->typespec_word = cts_fract;
10497 else
10498 specs->typespec_word = cts_accum;
10499 specs->locations[cdw_typespec] = loc;
10501 if (!targetm.fixed_point_supported_p ())
10502 error_at (loc,
10503 "fixed-point types not supported for this target");
10504 pedwarn (loc, OPT_Wpedantic,
10505 "ISO C does not support fixed-point types");
10506 return specs;
10507 default:
10508 /* ObjC reserved word "id", handled below. */
10509 break;
10514 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10515 form of ObjC type, cases such as "int" and "long" being handled
10516 above), a TYPE (struct, union, enum and typeof specifiers) or an
10517 ERROR_MARK. In none of these cases may there have previously
10518 been any type specifiers. */
10519 if (specs->type || specs->typespec_word != cts_none
10520 || specs->long_p || specs->short_p || specs->signed_p
10521 || specs->unsigned_p || specs->complex_p)
10522 error_at (loc, "two or more data types in declaration specifiers");
10523 else if (TREE_CODE (type) == TYPE_DECL)
10525 if (TREE_TYPE (type) == error_mark_node)
10526 ; /* Allow the type to default to int to avoid cascading errors. */
10527 else
10529 specs->type = TREE_TYPE (type);
10530 specs->decl_attr = DECL_ATTRIBUTES (type);
10531 specs->typedef_p = true;
10532 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10533 specs->locations[cdw_typedef] = loc;
10535 /* If this typedef name is defined in a struct, then a C++
10536 lookup would return a different value. */
10537 if (warn_cxx_compat
10538 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10539 warning_at (loc, OPT_Wc___compat,
10540 "C++ lookup of %qD would return a field, not a type",
10541 type);
10543 /* If we are parsing a struct, record that a struct field
10544 used a typedef. */
10545 if (warn_cxx_compat && struct_parse_info != NULL)
10546 struct_parse_info->typedefs_seen.safe_push (type);
10549 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10551 tree t = lookup_name (type);
10552 if (!t || TREE_CODE (t) != TYPE_DECL)
10553 error_at (loc, "%qE fails to be a typedef or built in type", type);
10554 else if (TREE_TYPE (t) == error_mark_node)
10556 else
10558 specs->type = TREE_TYPE (t);
10559 specs->locations[cdw_typespec] = loc;
10562 else
10564 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10566 specs->typedef_p = true;
10567 specs->locations[cdw_typedef] = loc;
10568 if (spec.expr)
10570 if (specs->expr)
10571 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10572 specs->expr, spec.expr);
10573 else
10574 specs->expr = spec.expr;
10575 specs->expr_const_operands &= spec.expr_const_operands;
10578 specs->type = type;
10581 return specs;
10584 /* Add the storage class specifier or function specifier SCSPEC to the
10585 declaration specifiers SPECS, returning SPECS. */
10587 struct c_declspecs *
10588 declspecs_add_scspec (source_location loc,
10589 struct c_declspecs *specs,
10590 tree scspec)
10592 enum rid i;
10593 enum c_storage_class n = csc_none;
10594 bool dupe = false;
10595 specs->declspecs_seen_p = true;
10596 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10597 && C_IS_RESERVED_WORD (scspec));
10598 i = C_RID_CODE (scspec);
10599 if (specs->non_sc_seen_p)
10600 warning (OPT_Wold_style_declaration,
10601 "%qE is not at beginning of declaration", scspec);
10602 switch (i)
10604 case RID_INLINE:
10605 /* C99 permits duplicate inline. Although of doubtful utility,
10606 it seems simplest to permit it in gnu89 mode as well, as
10607 there is also little utility in maintaining this as a
10608 difference between gnu89 and C99 inline. */
10609 dupe = false;
10610 specs->inline_p = true;
10611 specs->locations[cdw_inline] = loc;
10612 break;
10613 case RID_NORETURN:
10614 /* Duplicate _Noreturn is permitted. */
10615 dupe = false;
10616 specs->noreturn_p = true;
10617 specs->locations[cdw_noreturn] = loc;
10618 break;
10619 case RID_THREAD:
10620 dupe = specs->thread_p;
10621 if (specs->storage_class == csc_auto)
10622 error ("%qE used with %<auto%>", scspec);
10623 else if (specs->storage_class == csc_register)
10624 error ("%qE used with %<register%>", scspec);
10625 else if (specs->storage_class == csc_typedef)
10626 error ("%qE used with %<typedef%>", scspec);
10627 else
10629 specs->thread_p = true;
10630 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10631 "__thread") == 0);
10632 /* A diagnostic is not required for the use of this
10633 identifier in the implementation namespace; only diagnose
10634 it for the C11 spelling because of existing code using
10635 the other spelling. */
10636 if (!specs->thread_gnu_p)
10638 if (flag_isoc99)
10639 pedwarn_c99 (loc, OPT_Wpedantic,
10640 "ISO C99 does not support %qE", scspec);
10641 else
10642 pedwarn_c99 (loc, OPT_Wpedantic,
10643 "ISO C90 does not support %qE", scspec);
10645 specs->locations[cdw_thread] = loc;
10647 break;
10648 case RID_AUTO:
10649 n = csc_auto;
10650 break;
10651 case RID_EXTERN:
10652 n = csc_extern;
10653 /* Diagnose "__thread extern". */
10654 if (specs->thread_p && specs->thread_gnu_p)
10655 error ("%<__thread%> before %<extern%>");
10656 break;
10657 case RID_REGISTER:
10658 n = csc_register;
10659 break;
10660 case RID_STATIC:
10661 n = csc_static;
10662 /* Diagnose "__thread static". */
10663 if (specs->thread_p && specs->thread_gnu_p)
10664 error ("%<__thread%> before %<static%>");
10665 break;
10666 case RID_TYPEDEF:
10667 n = csc_typedef;
10668 break;
10669 default:
10670 gcc_unreachable ();
10672 if (n != csc_none && n == specs->storage_class)
10673 dupe = true;
10674 if (dupe)
10676 if (i == RID_THREAD)
10677 error ("duplicate %<_Thread_local%> or %<__thread%>");
10678 else
10679 error ("duplicate %qE", scspec);
10681 if (n != csc_none)
10683 if (specs->storage_class != csc_none && n != specs->storage_class)
10685 error ("multiple storage classes in declaration specifiers");
10687 else
10689 specs->storage_class = n;
10690 specs->locations[cdw_storage_class] = loc;
10691 if (n != csc_extern && n != csc_static && specs->thread_p)
10693 error ("%qs used with %qE",
10694 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10695 scspec);
10696 specs->thread_p = false;
10700 return specs;
10703 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10704 returning SPECS. */
10706 struct c_declspecs *
10707 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10709 specs->attrs = chainon (attrs, specs->attrs);
10710 specs->locations[cdw_attributes] = loc;
10711 specs->declspecs_seen_p = true;
10712 return specs;
10715 /* Add an _Alignas specifier (expression ALIGN, or type whose
10716 alignment is ALIGN) to the declaration specifiers SPECS, returning
10717 SPECS. */
10718 struct c_declspecs *
10719 declspecs_add_alignas (source_location loc,
10720 struct c_declspecs *specs, tree align)
10722 int align_log;
10723 specs->alignas_p = true;
10724 specs->locations[cdw_alignas] = loc;
10725 if (align == error_mark_node)
10726 return specs;
10727 align_log = check_user_alignment (align, true);
10728 if (align_log > specs->align_log)
10729 specs->align_log = align_log;
10730 return specs;
10733 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10734 specifiers with any other type specifier to determine the resulting
10735 type. This is where ISO C checks on complex types are made, since
10736 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10737 double". */
10739 struct c_declspecs *
10740 finish_declspecs (struct c_declspecs *specs)
10742 /* If a type was specified as a whole, we have no modifiers and are
10743 done. */
10744 if (specs->type != NULL_TREE)
10746 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10747 && !specs->signed_p && !specs->unsigned_p
10748 && !specs->complex_p);
10750 /* Set a dummy type. */
10751 if (TREE_CODE (specs->type) == ERROR_MARK)
10752 specs->type = integer_type_node;
10753 return specs;
10756 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10757 has been specified, treat it as "int" unless "_Complex" is
10758 present and there are no other specifiers. If we just have
10759 "_Complex", it is equivalent to "_Complex double", but e.g.
10760 "_Complex short" is equivalent to "_Complex short int". */
10761 if (specs->typespec_word == cts_none)
10763 if (specs->saturating_p)
10765 error_at (specs->locations[cdw_saturating],
10766 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10767 if (!targetm.fixed_point_supported_p ())
10768 error_at (specs->locations[cdw_saturating],
10769 "fixed-point types not supported for this target");
10770 specs->typespec_word = cts_fract;
10772 else if (specs->long_p || specs->short_p
10773 || specs->signed_p || specs->unsigned_p)
10775 specs->typespec_word = cts_int;
10777 else if (specs->complex_p)
10779 specs->typespec_word = cts_double;
10780 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10781 "ISO C does not support plain %<complex%> meaning "
10782 "%<double complex%>");
10784 else
10786 specs->typespec_word = cts_int;
10787 specs->default_int_p = true;
10788 /* We don't diagnose this here because grokdeclarator will
10789 give more specific diagnostics according to whether it is
10790 a function definition. */
10794 /* If "signed" was specified, record this to distinguish "int" and
10795 "signed int" in the case of a bit-field with
10796 -funsigned-bitfields. */
10797 specs->explicit_signed_p = specs->signed_p;
10799 /* Now compute the actual type. */
10800 switch (specs->typespec_word)
10802 case cts_auto_type:
10803 gcc_assert (!specs->long_p && !specs->short_p
10804 && !specs->signed_p && !specs->unsigned_p
10805 && !specs->complex_p);
10806 /* Type to be filled in later. */
10807 break;
10808 case cts_void:
10809 gcc_assert (!specs->long_p && !specs->short_p
10810 && !specs->signed_p && !specs->unsigned_p
10811 && !specs->complex_p);
10812 specs->type = void_type_node;
10813 break;
10814 case cts_bool:
10815 gcc_assert (!specs->long_p && !specs->short_p
10816 && !specs->signed_p && !specs->unsigned_p
10817 && !specs->complex_p);
10818 specs->type = boolean_type_node;
10819 break;
10820 case cts_char:
10821 gcc_assert (!specs->long_p && !specs->short_p);
10822 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10823 if (specs->signed_p)
10824 specs->type = signed_char_type_node;
10825 else if (specs->unsigned_p)
10826 specs->type = unsigned_char_type_node;
10827 else
10828 specs->type = char_type_node;
10829 if (specs->complex_p)
10831 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10832 "ISO C does not support complex integer types");
10833 specs->type = build_complex_type (specs->type);
10835 break;
10836 case cts_int_n:
10837 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10838 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10839 specs->type = (specs->unsigned_p
10840 ? int_n_trees[specs->int_n_idx].unsigned_type
10841 : int_n_trees[specs->int_n_idx].signed_type);
10842 if (specs->complex_p)
10844 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10845 "ISO C does not support complex integer types");
10846 specs->type = build_complex_type (specs->type);
10848 break;
10849 case cts_int:
10850 gcc_assert (!(specs->long_p && specs->short_p));
10851 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10852 if (specs->long_long_p)
10853 specs->type = (specs->unsigned_p
10854 ? long_long_unsigned_type_node
10855 : long_long_integer_type_node);
10856 else if (specs->long_p)
10857 specs->type = (specs->unsigned_p
10858 ? long_unsigned_type_node
10859 : long_integer_type_node);
10860 else if (specs->short_p)
10861 specs->type = (specs->unsigned_p
10862 ? short_unsigned_type_node
10863 : short_integer_type_node);
10864 else
10865 specs->type = (specs->unsigned_p
10866 ? unsigned_type_node
10867 : integer_type_node);
10868 if (specs->complex_p)
10870 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10871 "ISO C does not support complex integer types");
10872 specs->type = build_complex_type (specs->type);
10874 break;
10875 case cts_float:
10876 gcc_assert (!specs->long_p && !specs->short_p
10877 && !specs->signed_p && !specs->unsigned_p);
10878 specs->type = (specs->complex_p
10879 ? complex_float_type_node
10880 : float_type_node);
10881 break;
10882 case cts_double:
10883 gcc_assert (!specs->long_long_p && !specs->short_p
10884 && !specs->signed_p && !specs->unsigned_p);
10885 if (specs->long_p)
10887 specs->type = (specs->complex_p
10888 ? complex_long_double_type_node
10889 : long_double_type_node);
10891 else
10893 specs->type = (specs->complex_p
10894 ? complex_double_type_node
10895 : double_type_node);
10897 break;
10898 case cts_floatn_nx:
10899 gcc_assert (!specs->long_p && !specs->short_p
10900 && !specs->signed_p && !specs->unsigned_p);
10901 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10902 specs->type = integer_type_node;
10903 else if (specs->complex_p)
10904 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
10905 else
10906 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
10907 break;
10908 case cts_dfloat32:
10909 case cts_dfloat64:
10910 case cts_dfloat128:
10911 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10912 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10913 if (specs->typespec_word == cts_dfloat32)
10914 specs->type = dfloat32_type_node;
10915 else if (specs->typespec_word == cts_dfloat64)
10916 specs->type = dfloat64_type_node;
10917 else
10918 specs->type = dfloat128_type_node;
10919 break;
10920 case cts_fract:
10921 gcc_assert (!specs->complex_p);
10922 if (!targetm.fixed_point_supported_p ())
10923 specs->type = integer_type_node;
10924 else if (specs->saturating_p)
10926 if (specs->long_long_p)
10927 specs->type = specs->unsigned_p
10928 ? sat_unsigned_long_long_fract_type_node
10929 : sat_long_long_fract_type_node;
10930 else if (specs->long_p)
10931 specs->type = specs->unsigned_p
10932 ? sat_unsigned_long_fract_type_node
10933 : sat_long_fract_type_node;
10934 else if (specs->short_p)
10935 specs->type = specs->unsigned_p
10936 ? sat_unsigned_short_fract_type_node
10937 : sat_short_fract_type_node;
10938 else
10939 specs->type = specs->unsigned_p
10940 ? sat_unsigned_fract_type_node
10941 : sat_fract_type_node;
10943 else
10945 if (specs->long_long_p)
10946 specs->type = specs->unsigned_p
10947 ? unsigned_long_long_fract_type_node
10948 : long_long_fract_type_node;
10949 else if (specs->long_p)
10950 specs->type = specs->unsigned_p
10951 ? unsigned_long_fract_type_node
10952 : long_fract_type_node;
10953 else if (specs->short_p)
10954 specs->type = specs->unsigned_p
10955 ? unsigned_short_fract_type_node
10956 : short_fract_type_node;
10957 else
10958 specs->type = specs->unsigned_p
10959 ? unsigned_fract_type_node
10960 : fract_type_node;
10962 break;
10963 case cts_accum:
10964 gcc_assert (!specs->complex_p);
10965 if (!targetm.fixed_point_supported_p ())
10966 specs->type = integer_type_node;
10967 else if (specs->saturating_p)
10969 if (specs->long_long_p)
10970 specs->type = specs->unsigned_p
10971 ? sat_unsigned_long_long_accum_type_node
10972 : sat_long_long_accum_type_node;
10973 else if (specs->long_p)
10974 specs->type = specs->unsigned_p
10975 ? sat_unsigned_long_accum_type_node
10976 : sat_long_accum_type_node;
10977 else if (specs->short_p)
10978 specs->type = specs->unsigned_p
10979 ? sat_unsigned_short_accum_type_node
10980 : sat_short_accum_type_node;
10981 else
10982 specs->type = specs->unsigned_p
10983 ? sat_unsigned_accum_type_node
10984 : sat_accum_type_node;
10986 else
10988 if (specs->long_long_p)
10989 specs->type = specs->unsigned_p
10990 ? unsigned_long_long_accum_type_node
10991 : long_long_accum_type_node;
10992 else if (specs->long_p)
10993 specs->type = specs->unsigned_p
10994 ? unsigned_long_accum_type_node
10995 : long_accum_type_node;
10996 else if (specs->short_p)
10997 specs->type = specs->unsigned_p
10998 ? unsigned_short_accum_type_node
10999 : short_accum_type_node;
11000 else
11001 specs->type = specs->unsigned_p
11002 ? unsigned_accum_type_node
11003 : accum_type_node;
11005 break;
11006 default:
11007 gcc_unreachable ();
11010 return specs;
11013 /* Perform final processing on one file scope's declarations (or the
11014 external scope's declarations), GLOBALS. */
11016 static void
11017 c_write_global_declarations_1 (tree globals)
11019 tree decl;
11020 bool reconsider;
11022 /* Process the decls in the order they were written. */
11023 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11025 /* Check for used but undefined static functions using the C
11026 standard's definition of "used", and set TREE_NO_WARNING so
11027 that check_global_declaration doesn't repeat the check. */
11028 if (TREE_CODE (decl) == FUNCTION_DECL
11029 && DECL_INITIAL (decl) == 0
11030 && DECL_EXTERNAL (decl)
11031 && !TREE_PUBLIC (decl))
11033 if (C_DECL_USED (decl))
11035 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11036 TREE_NO_WARNING (decl) = 1;
11038 /* For -Wunused-function warn about unused static prototypes. */
11039 else if (warn_unused_function
11040 && ! DECL_ARTIFICIAL (decl)
11041 && ! TREE_NO_WARNING (decl))
11043 warning (OPT_Wunused_function,
11044 "%q+F declared %<static%> but never defined", decl);
11045 TREE_NO_WARNING (decl) = 1;
11049 wrapup_global_declaration_1 (decl);
11054 reconsider = false;
11055 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11056 reconsider |= wrapup_global_declaration_2 (decl);
11058 while (reconsider);
11061 /* Callback to collect a source_ref from a DECL. */
11063 static void
11064 collect_source_ref_cb (tree decl)
11066 if (!DECL_IS_BUILTIN (decl))
11067 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11070 /* Preserve the external declarations scope across a garbage collect. */
11071 static GTY(()) tree ext_block;
11073 /* Collect all references relevant to SOURCE_FILE. */
11075 static void
11076 collect_all_refs (const char *source_file)
11078 tree t;
11079 unsigned i;
11081 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11082 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11084 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11087 /* Iterate over all global declarations and call CALLBACK. */
11089 static void
11090 for_each_global_decl (void (*callback) (tree decl))
11092 tree t;
11093 tree decls;
11094 tree decl;
11095 unsigned i;
11097 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11099 decls = DECL_INITIAL (t);
11100 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11101 callback (decl);
11104 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11105 callback (decl);
11108 /* Perform any final parser cleanups and generate initial debugging
11109 information. */
11111 void
11112 c_parse_final_cleanups (void)
11114 tree t;
11115 unsigned i;
11117 /* We don't want to do this if generating a PCH. */
11118 if (pch_file)
11119 return;
11121 timevar_stop (TV_PHASE_PARSING);
11122 timevar_start (TV_PHASE_DEFERRED);
11124 /* Do the Objective-C stuff. This is where all the Objective-C
11125 module stuff gets generated (symtab, class/protocol/selector
11126 lists etc). */
11127 if (c_dialect_objc ())
11128 objc_write_global_declarations ();
11130 /* Close the external scope. */
11131 ext_block = pop_scope ();
11132 external_scope = 0;
11133 gcc_assert (!current_scope);
11135 /* Handle -fdump-ada-spec[-slim]. */
11136 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11138 /* Build a table of files to generate specs for */
11139 if (flag_dump_ada_spec_slim)
11140 collect_source_ref (main_input_filename);
11141 else
11142 for_each_global_decl (collect_source_ref_cb);
11144 dump_ada_specs (collect_all_refs, NULL);
11147 if (ext_block)
11149 tree tmp = BLOCK_VARS (ext_block);
11150 int flags;
11151 FILE * stream = dump_begin (TDI_tu, &flags);
11152 if (stream && tmp)
11154 dump_node (tmp, flags & ~TDF_SLIM, stream);
11155 dump_end (TDI_tu, stream);
11159 /* Process all file scopes in this compilation, and the external_scope,
11160 through wrapup_global_declarations. */
11161 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11162 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11163 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11165 timevar_stop (TV_PHASE_DEFERRED);
11166 timevar_start (TV_PHASE_PARSING);
11168 ext_block = NULL;
11171 /* Register reserved keyword WORD as qualifier for address space AS. */
11173 void
11174 c_register_addr_space (const char *word, addr_space_t as)
11176 int rid = RID_FIRST_ADDR_SPACE + as;
11177 tree id;
11179 /* Address space qualifiers are only supported
11180 in C with GNU extensions enabled. */
11181 if (c_dialect_objc () || flag_no_asm)
11182 return;
11184 id = get_identifier (word);
11185 C_SET_RID_CODE (id, rid);
11186 C_IS_RESERVED_WORD (id) = 1;
11187 ridpointers [rid] = id;
11190 /* Return identifier to look up for omp declare reduction. */
11192 tree
11193 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11195 const char *p = NULL;
11196 switch (reduction_code)
11198 case PLUS_EXPR: p = "+"; break;
11199 case MULT_EXPR: p = "*"; break;
11200 case MINUS_EXPR: p = "-"; break;
11201 case BIT_AND_EXPR: p = "&"; break;
11202 case BIT_XOR_EXPR: p = "^"; break;
11203 case BIT_IOR_EXPR: p = "|"; break;
11204 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11205 case TRUTH_ORIF_EXPR: p = "||"; break;
11206 case MIN_EXPR: p = "min"; break;
11207 case MAX_EXPR: p = "max"; break;
11208 default:
11209 break;
11212 if (p == NULL)
11214 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11215 return error_mark_node;
11216 p = IDENTIFIER_POINTER (reduction_id);
11219 const char prefix[] = "omp declare reduction ";
11220 size_t lenp = sizeof (prefix);
11221 size_t len = strlen (p);
11222 char *name = XALLOCAVEC (char, lenp + len);
11223 memcpy (name, prefix, lenp - 1);
11224 memcpy (name + lenp - 1, p, len + 1);
11225 return get_identifier (name);
11228 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11229 VAR_DECL, bind it into the current scope and return it. */
11231 tree
11232 c_omp_reduction_decl (tree reduction_id)
11234 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11235 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11236 return b->decl;
11238 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11239 reduction_id, integer_type_node);
11240 DECL_ARTIFICIAL (decl) = 1;
11241 DECL_EXTERNAL (decl) = 1;
11242 TREE_STATIC (decl) = 1;
11243 TREE_PUBLIC (decl) = 0;
11244 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11245 return decl;
11248 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11250 tree
11251 c_omp_reduction_lookup (tree reduction_id, tree type)
11253 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11254 while (b)
11256 tree t;
11257 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11258 if (comptypes (TREE_PURPOSE (t), type))
11259 return TREE_VALUE (t);
11260 b = b->shadowed;
11262 return error_mark_node;
11265 /* Helper function called via walk_tree, to diagnose invalid
11266 #pragma omp declare reduction combiners or initializers. */
11268 tree
11269 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11271 tree *vars = (tree *) data;
11272 if (SSA_VAR_P (*tp)
11273 && !DECL_ARTIFICIAL (*tp)
11274 && *tp != vars[0]
11275 && *tp != vars[1])
11277 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11278 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11279 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11280 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11281 *tp);
11282 else
11283 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11284 "to variable %qD which is not %<omp_priv%> nor "
11285 "%<omp_orig%>",
11286 *tp);
11287 return *tp;
11289 return NULL_TREE;
11292 #include "gt-c-c-decl.h"