* src/powerpc/aix_closure.S (ffi_closure_ASM): Adjust for Darwin64
[official-gcc.git] / gcc / c-decl.c
blob160d393e7badf8783e2fd1b2b62926155da971b0
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "c-tree.h"
41 #include "toplev.h"
42 #include "tm_p.h"
43 #include "cpplib.h"
44 #include "target.h"
45 #include "debug.h"
46 #include "opts.h"
47 #include "timevar.h"
48 #include "c-family/c-common.h"
49 #include "c-family/c-objc.h"
50 #include "c-family/c-pragma.h"
51 #include "c-lang.h"
52 #include "langhooks.h"
53 #include "tree-iterator.h"
54 #include "diagnostic-core.h"
55 #include "tree-dump.h"
56 #include "cgraph.h"
57 #include "hashtab.h"
58 #include "langhooks-def.h"
59 #include "pointer-set.h"
60 #include "plugin.h"
61 #include "c-family/c-ada-spec.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64 enum decl_context
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
71 /* States indicating how grokdeclarator() should handle declspecs marked
72 with __attribute__((deprecated)). An object declared as
73 __attribute__((deprecated)) suppresses warnings of uses of other
74 deprecated items. */
76 enum deprecated_states {
77 DEPRECATED_NORMAL,
78 DEPRECATED_SUPPRESS
82 /* Nonzero if we have seen an invalid cross reference
83 to a struct, union, or enum, but not yet printed the message. */
84 tree pending_invalid_xref;
86 /* File and line to appear in the eventual error message. */
87 location_t pending_invalid_xref_location;
89 /* The file and line that the prototype came from if this is an
90 old-style definition; used for diagnostics in
91 store_parm_decls_oldstyle. */
93 static location_t current_function_prototype_locus;
95 /* Whether this prototype was built-in. */
97 static bool current_function_prototype_built_in;
99 /* The argument type information of this prototype. */
101 static tree current_function_prototype_arg_types;
103 /* The argument information structure for the function currently being
104 defined. */
106 static struct c_arg_info *current_function_arg_info;
108 /* The obstack on which parser and related data structures, which are
109 not live beyond their top-level declaration or definition, are
110 allocated. */
111 struct obstack parser_obstack;
113 /* The current statement tree. */
115 static GTY(()) struct stmt_tree_s c_stmt_tree;
117 /* State saving variables. */
118 tree c_break_label;
119 tree c_cont_label;
121 /* A list of decls to be made automatically visible in each file scope. */
122 static GTY(()) tree visible_builtins;
124 /* Set to 0 at beginning of a function definition, set to 1 if
125 a return statement that specifies a return value is seen. */
127 int current_function_returns_value;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement with no argument is seen. */
132 int current_function_returns_null;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a call to a noreturn function is seen. */
137 int current_function_returns_abnormally;
139 /* Set to nonzero by `grokdeclarator' for a function
140 whose return type is defaulted, if warnings for this are desired. */
142 static int warn_about_return_type;
144 /* Nonzero when the current toplevel function contains a declaration
145 of a nested function which is never defined. */
147 static bool undef_nested_function;
149 /* Mode used to build pointers (VOIDmode means ptr_mode). */
151 enum machine_mode c_default_pointer_mode = VOIDmode;
154 /* Each c_binding structure describes one binding of an identifier to
155 a decl. All the decls in a scope - irrespective of namespace - are
156 chained together by the ->prev field, which (as the name implies)
157 runs in reverse order. All the decls in a given namespace bound to
158 a given identifier are chained by the ->shadowed field, which runs
159 from inner to outer scopes.
161 The ->decl field usually points to a DECL node, but there are two
162 exceptions. In the namespace of type tags, the bound entity is a
163 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
164 identifier is encountered, it is bound to error_mark_node to
165 suppress further errors about that identifier in the current
166 function.
168 The ->u.type field stores the type of the declaration in this scope;
169 if NULL, the type is the type of the ->decl field. This is only of
170 relevance for objects with external or internal linkage which may
171 be redeclared in inner scopes, forming composite types that only
172 persist for the duration of those scopes. In the external scope,
173 this stores the composite of all the types declared for this
174 object, visible or not. The ->inner_comp field (used only at file
175 scope) stores whether an incomplete array type at file scope was
176 completed at an inner scope to an array size other than 1.
178 The ->u.label field is used for labels. It points to a structure
179 which stores additional information used for warnings.
181 The depth field is copied from the scope structure that holds this
182 decl. It is used to preserve the proper ordering of the ->shadowed
183 field (see bind()) and also for a handful of special-case checks.
184 Finally, the invisible bit is true for a decl which should be
185 ignored for purposes of normal name lookup, and the nested bit is
186 true for a decl that's been bound a second time in an inner scope;
187 in all such cases, the binding in the outer scope will have its
188 invisible bit true. */
190 struct GTY((chain_next ("%h.prev"))) c_binding {
191 union GTY(()) { /* first so GTY desc can use decl */
192 tree GTY((tag ("0"))) type; /* the type in this scope */
193 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
194 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
195 tree decl; /* the decl bound */
196 tree id; /* the identifier it's bound to */
197 struct c_binding *prev; /* the previous decl in this scope */
198 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
199 unsigned int depth : 28; /* depth of this scope */
200 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
201 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
202 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
203 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
204 location_t locus; /* location for nested bindings */
206 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
207 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
208 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
209 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
211 #define I_SYMBOL_BINDING(node) \
212 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
213 #define I_SYMBOL_DECL(node) \
214 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
216 #define I_TAG_BINDING(node) \
217 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
218 #define I_TAG_DECL(node) \
219 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
221 #define I_LABEL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
223 #define I_LABEL_DECL(node) \
224 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
226 /* Each C symbol points to three linked lists of c_binding structures.
227 These describe the values of the identifier in the three different
228 namespaces defined by the language. */
230 struct GTY(()) lang_identifier {
231 struct c_common_identifier common_id;
232 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
233 struct c_binding *tag_binding; /* struct/union/enum tags */
234 struct c_binding *label_binding; /* labels */
237 /* Validate c-lang.c's assumptions. */
238 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
239 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
241 /* The resulting tree type. */
243 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
244 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
246 union tree_node GTY ((tag ("0"),
247 desc ("tree_node_structure (&%h)")))
248 generic;
249 struct lang_identifier GTY ((tag ("1"))) identifier;
252 /* Track bindings and other things that matter for goto warnings. For
253 efficiency, we do not gather all the decls at the point of
254 definition. Instead, we point into the bindings structure. As
255 scopes are popped, we update these structures and gather the decls
256 that matter at that time. */
258 struct GTY(()) c_spot_bindings {
259 /* The currently open scope which holds bindings defined when the
260 label was defined or the goto statement was found. */
261 struct c_scope *scope;
262 /* The bindings in the scope field which were defined at the point
263 of the label or goto. This lets us look at older or newer
264 bindings in the scope, as appropriate. */
265 struct c_binding *bindings_in_scope;
266 /* The number of statement expressions that have started since this
267 label or goto statement was defined. This is zero if we are at
268 the same statement expression level. It is positive if we are in
269 a statement expression started since this spot. It is negative
270 if this spot was in a statement expression and we have left
271 it. */
272 int stmt_exprs;
273 /* Whether we started in a statement expression but are no longer in
274 it. This is set to true if stmt_exprs ever goes negative. */
275 bool left_stmt_expr;
278 /* This structure is used to keep track of bindings seen when a goto
279 statement is defined. This is only used if we see the goto
280 statement before we see the label. */
282 struct GTY(()) c_goto_bindings {
283 /* The location of the goto statement. */
284 location_t loc;
285 /* The bindings of the goto statement. */
286 struct c_spot_bindings goto_bindings;
289 typedef struct c_goto_bindings *c_goto_bindings_p;
290 DEF_VEC_P(c_goto_bindings_p);
291 DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
293 /* The additional information we keep track of for a label binding.
294 These fields are updated as scopes are popped. */
296 struct GTY(()) c_label_vars {
297 /* The shadowed c_label_vars, when one label shadows another (which
298 can only happen using a __label__ declaration). */
299 struct c_label_vars *shadowed;
300 /* The bindings when the label was defined. */
301 struct c_spot_bindings label_bindings;
302 /* A list of decls that we care about: decls about which we should
303 warn if a goto branches to this label from later in the function.
304 Decls are added to this list as scopes are popped. We only add
305 the decls that matter. */
306 VEC(tree,gc) *decls_in_scope;
307 /* A list of goto statements to this label. This is only used for
308 goto statements seen before the label was defined, so that we can
309 issue appropriate warnings for them. */
310 VEC(c_goto_bindings_p,gc) *gotos;
313 /* Each c_scope structure describes the complete contents of one
314 scope. Four scopes are distinguished specially: the innermost or
315 current scope, the innermost function scope, the file scope (always
316 the second to outermost) and the outermost or external scope.
318 Most declarations are recorded in the current scope.
320 All normal label declarations are recorded in the innermost
321 function scope, as are bindings of undeclared identifiers to
322 error_mark_node. (GCC permits nested functions as an extension,
323 hence the 'innermost' qualifier.) Explicitly declared labels
324 (using the __label__ extension) appear in the current scope.
326 Being in the file scope (current_scope == file_scope) causes
327 special behavior in several places below. Also, under some
328 conditions the Objective-C front end records declarations in the
329 file scope even though that isn't the current scope.
331 All declarations with external linkage are recorded in the external
332 scope, even if they aren't visible there; this models the fact that
333 such declarations are visible to the entire program, and (with a
334 bit of cleverness, see pushdecl) allows diagnosis of some violations
335 of C99 6.2.2p7 and 6.2.7p2:
337 If, within the same translation unit, the same identifier appears
338 with both internal and external linkage, the behavior is
339 undefined.
341 All declarations that refer to the same object or function shall
342 have compatible type; otherwise, the behavior is undefined.
344 Initially only the built-in declarations, which describe compiler
345 intrinsic functions plus a subset of the standard library, are in
346 this scope.
348 The order of the blocks list matters, and it is frequently appended
349 to. To avoid having to walk all the way to the end of the list on
350 each insertion, or reverse the list later, we maintain a pointer to
351 the last list entry. (FIXME: It should be feasible to use a reversed
352 list here.)
354 The bindings list is strictly in reverse order of declarations;
355 pop_scope relies on this. */
358 struct GTY((chain_next ("%h.outer"))) c_scope {
359 /* The scope containing this one. */
360 struct c_scope *outer;
362 /* The next outermost function scope. */
363 struct c_scope *outer_function;
365 /* All bindings in this scope. */
366 struct c_binding *bindings;
368 /* For each scope (except the global one), a chain of BLOCK nodes
369 for all the scopes that were entered and exited one level down. */
370 tree blocks;
371 tree blocks_last;
373 /* The depth of this scope. Used to keep the ->shadowed chain of
374 bindings sorted innermost to outermost. */
375 unsigned int depth : 28;
377 /* True if we are currently filling this scope with parameter
378 declarations. */
379 BOOL_BITFIELD parm_flag : 1;
381 /* True if we saw [*] in this scope. Used to give an error messages
382 if these appears in a function definition. */
383 BOOL_BITFIELD had_vla_unspec : 1;
385 /* True if we already complained about forward parameter decls
386 in this scope. This prevents double warnings on
387 foo (int a; int b; ...) */
388 BOOL_BITFIELD warned_forward_parm_decls : 1;
390 /* True if this is the outermost block scope of a function body.
391 This scope contains the parameters, the local variables declared
392 in the outermost block, and all the labels (except those in
393 nested functions, or declared at block scope with __label__). */
394 BOOL_BITFIELD function_body : 1;
396 /* True means make a BLOCK for this scope no matter what. */
397 BOOL_BITFIELD keep : 1;
399 /* True means that an unsuffixed float constant is _Decimal64. */
400 BOOL_BITFIELD float_const_decimal64 : 1;
402 /* True if this scope has any label bindings. This is used to speed
403 up searching for labels when popping scopes, particularly since
404 labels are normally only found at function scope. */
405 BOOL_BITFIELD has_label_bindings : 1;
407 /* True if we should issue a warning if a goto statement crosses any
408 of the bindings. We still need to check the list of bindings to
409 find the specific ones we need to warn about. This is true if
410 decl_jump_unsafe would return true for any of the bindings. This
411 is used to avoid looping over all the bindings unnecessarily. */
412 BOOL_BITFIELD has_jump_unsafe_decl : 1;
415 /* The scope currently in effect. */
417 static GTY(()) struct c_scope *current_scope;
419 /* The innermost function scope. Ordinary (not explicitly declared)
420 labels, bindings to error_mark_node, and the lazily-created
421 bindings of __func__ and its friends get this scope. */
423 static GTY(()) struct c_scope *current_function_scope;
425 /* The C file scope. This is reset for each input translation unit. */
427 static GTY(()) struct c_scope *file_scope;
429 /* The outermost scope. This is used for all declarations with
430 external linkage, and only these, hence the name. */
432 static GTY(()) struct c_scope *external_scope;
434 /* A chain of c_scope structures awaiting reuse. */
436 static GTY((deletable)) struct c_scope *scope_freelist;
438 /* A chain of c_binding structures awaiting reuse. */
440 static GTY((deletable)) struct c_binding *binding_freelist;
442 /* Append VAR to LIST in scope SCOPE. */
443 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
444 struct c_scope *s_ = (scope); \
445 tree d_ = (decl); \
446 if (s_->list##_last) \
447 BLOCK_CHAIN (s_->list##_last) = d_; \
448 else \
449 s_->list = d_; \
450 s_->list##_last = d_; \
451 } while (0)
453 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
454 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
455 struct c_scope *t_ = (tscope); \
456 struct c_scope *f_ = (fscope); \
457 if (t_->to##_last) \
458 BLOCK_CHAIN (t_->to##_last) = f_->from; \
459 else \
460 t_->to = f_->from; \
461 t_->to##_last = f_->from##_last; \
462 } while (0)
464 /* A c_inline_static structure stores details of a static identifier
465 referenced in a definition of a function that may be an inline
466 definition if no subsequent declaration of that function uses
467 "extern" or does not use "inline". */
469 struct GTY((chain_next ("%h.next"))) c_inline_static {
470 /* The location for a diagnostic. */
471 location_t location;
473 /* The function that may be an inline definition. */
474 tree function;
476 /* The object or function referenced. */
477 tree static_decl;
479 /* What sort of reference this is. */
480 enum c_inline_static_type type;
482 /* The next such structure or NULL. */
483 struct c_inline_static *next;
486 /* List of static identifiers used or referenced in functions that may
487 be inline definitions. */
488 static GTY(()) struct c_inline_static *c_inline_statics;
490 /* True means unconditionally make a BLOCK for the next scope pushed. */
492 static bool keep_next_level_flag;
494 /* True means the next call to push_scope will be the outermost scope
495 of a function body, so do not push a new scope, merely cease
496 expecting parameter decls. */
498 static bool next_is_function_body;
500 /* A VEC of pointers to c_binding structures. */
502 typedef struct c_binding *c_binding_ptr;
503 DEF_VEC_P(c_binding_ptr);
504 DEF_VEC_ALLOC_P(c_binding_ptr,heap);
506 /* Information that we keep for a struct or union while it is being
507 parsed. */
509 struct c_struct_parse_info
511 /* If warn_cxx_compat, a list of types defined within this
512 struct. */
513 VEC(tree,heap) *struct_types;
514 /* If warn_cxx_compat, a list of field names which have bindings,
515 and which are defined in this struct, but which are not defined
516 in any enclosing struct. This is used to clear the in_struct
517 field of the c_bindings structure. */
518 VEC(c_binding_ptr,heap) *fields;
519 /* If warn_cxx_compat, a list of typedef names used when defining
520 fields in this struct. */
521 VEC(tree,heap) *typedefs_seen;
524 /* Information for the struct or union currently being parsed, or
525 NULL if not parsing a struct or union. */
526 static struct c_struct_parse_info *struct_parse_info;
528 /* Forward declarations. */
529 static tree lookup_name_in_scope (tree, struct c_scope *);
530 static tree c_make_fname_decl (location_t, tree, int);
531 static tree grokdeclarator (const struct c_declarator *,
532 struct c_declspecs *,
533 enum decl_context, bool, tree *, tree *, tree *,
534 bool *, enum deprecated_states);
535 static tree grokparms (struct c_arg_info *, bool);
536 static void layout_array_type (tree);
538 /* T is a statement. Add it to the statement-tree. This is the
539 C/ObjC version--C++ has a slightly different version of this
540 function. */
542 tree
543 add_stmt (tree t)
545 enum tree_code code = TREE_CODE (t);
547 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
549 if (!EXPR_HAS_LOCATION (t))
550 SET_EXPR_LOCATION (t, input_location);
553 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
554 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
556 /* Add T to the statement-tree. Non-side-effect statements need to be
557 recorded during statement expressions. */
558 if (!building_stmt_list_p ())
559 push_stmt_list ();
560 append_to_statement_list_force (t, &cur_stmt_list);
562 return t;
565 /* Build a pointer type using the default pointer mode. */
567 static tree
568 c_build_pointer_type (tree to_type)
570 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
571 : TYPE_ADDR_SPACE (to_type);
572 enum machine_mode pointer_mode;
574 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
575 pointer_mode = targetm.addr_space.pointer_mode (as);
576 else
577 pointer_mode = c_default_pointer_mode;
578 return build_pointer_type_for_mode (to_type, pointer_mode, false);
582 /* Return true if we will want to say something if a goto statement
583 crosses DECL. */
585 static bool
586 decl_jump_unsafe (tree decl)
588 if (error_operand_p (decl))
589 return false;
591 /* Always warn about crossing variably modified types. */
592 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
593 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
594 return true;
596 /* Otherwise, only warn if -Wgoto-misses-init and this is an
597 initialized automatic decl. */
598 if (warn_jump_misses_init
599 && TREE_CODE (decl) == VAR_DECL
600 && !TREE_STATIC (decl)
601 && DECL_INITIAL (decl) != NULL_TREE)
602 return true;
604 return false;
608 void
609 c_print_identifier (FILE *file, tree node, int indent)
611 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
612 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
613 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
614 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
616 tree rid = ridpointers[C_RID_CODE (node)];
617 indent_to (file, indent + 4);
618 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
619 (void *) rid, IDENTIFIER_POINTER (rid));
623 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
624 which may be any of several kinds of DECL or TYPE or error_mark_node,
625 in the scope SCOPE. */
626 static void
627 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
628 bool nested, location_t locus)
630 struct c_binding *b, **here;
632 if (binding_freelist)
634 b = binding_freelist;
635 binding_freelist = b->prev;
637 else
638 b = ggc_alloc_c_binding ();
640 b->shadowed = 0;
641 b->decl = decl;
642 b->id = name;
643 b->depth = scope->depth;
644 b->invisible = invisible;
645 b->nested = nested;
646 b->inner_comp = 0;
647 b->in_struct = 0;
648 b->locus = locus;
650 b->u.type = NULL;
652 b->prev = scope->bindings;
653 scope->bindings = b;
655 if (decl_jump_unsafe (decl))
656 scope->has_jump_unsafe_decl = 1;
658 if (!name)
659 return;
661 switch (TREE_CODE (decl))
663 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
664 case ENUMERAL_TYPE:
665 case UNION_TYPE:
666 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
667 case VAR_DECL:
668 case FUNCTION_DECL:
669 case TYPE_DECL:
670 case CONST_DECL:
671 case PARM_DECL:
672 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
674 default:
675 gcc_unreachable ();
678 /* Locate the appropriate place in the chain of shadowed decls
679 to insert this binding. Normally, scope == current_scope and
680 this does nothing. */
681 while (*here && (*here)->depth > scope->depth)
682 here = &(*here)->shadowed;
684 b->shadowed = *here;
685 *here = b;
688 /* Clear the binding structure B, stick it on the binding_freelist,
689 and return the former value of b->prev. This is used by pop_scope
690 and get_parm_info to iterate destructively over all the bindings
691 from a given scope. */
692 static struct c_binding *
693 free_binding_and_advance (struct c_binding *b)
695 struct c_binding *prev = b->prev;
697 memset (b, 0, sizeof (struct c_binding));
698 b->prev = binding_freelist;
699 binding_freelist = b;
701 return prev;
704 /* Bind a label. Like bind, but skip fields which aren't used for
705 labels, and add the LABEL_VARS value. */
706 static void
707 bind_label (tree name, tree label, struct c_scope *scope,
708 struct c_label_vars *label_vars)
710 struct c_binding *b;
712 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
713 UNKNOWN_LOCATION);
715 scope->has_label_bindings = true;
717 b = scope->bindings;
718 gcc_assert (b->decl == label);
719 label_vars->shadowed = b->u.label;
720 b->u.label = label_vars;
723 /* Hook called at end of compilation to assume 1 elt
724 for a file-scope tentative array defn that wasn't complete before. */
726 void
727 c_finish_incomplete_decl (tree decl)
729 if (TREE_CODE (decl) == VAR_DECL)
731 tree type = TREE_TYPE (decl);
732 if (type != error_mark_node
733 && TREE_CODE (type) == ARRAY_TYPE
734 && !DECL_EXTERNAL (decl)
735 && TYPE_DOMAIN (type) == 0)
737 warning_at (DECL_SOURCE_LOCATION (decl),
738 0, "array %q+D assumed to have one element", decl);
740 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
742 relayout_decl (decl);
747 /* Record that inline function FUNC contains a reference (location
748 LOC) to static DECL (file-scope or function-local according to
749 TYPE). */
751 void
752 record_inline_static (location_t loc, tree func, tree decl,
753 enum c_inline_static_type type)
755 struct c_inline_static *csi = ggc_alloc_c_inline_static ();
756 csi->location = loc;
757 csi->function = func;
758 csi->static_decl = decl;
759 csi->type = type;
760 csi->next = c_inline_statics;
761 c_inline_statics = csi;
764 /* Check for references to static declarations in inline functions at
765 the end of the translation unit and diagnose them if the functions
766 are still inline definitions. */
768 static void
769 check_inline_statics (void)
771 struct c_inline_static *csi;
772 for (csi = c_inline_statics; csi; csi = csi->next)
774 if (DECL_EXTERNAL (csi->function))
775 switch (csi->type)
777 case csi_internal:
778 pedwarn (csi->location, 0,
779 "%qD is static but used in inline function %qD "
780 "which is not static", csi->static_decl, csi->function);
781 break;
782 case csi_modifiable:
783 pedwarn (csi->location, 0,
784 "%q+D is static but declared in inline function %qD "
785 "which is not static", csi->static_decl, csi->function);
786 break;
787 default:
788 gcc_unreachable ();
791 c_inline_statics = NULL;
794 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
795 for the current state, otherwise set it to uninitialized. */
797 static void
798 set_spot_bindings (struct c_spot_bindings *p, bool defining)
800 if (defining)
802 p->scope = current_scope;
803 p->bindings_in_scope = current_scope->bindings;
805 else
807 p->scope = NULL;
808 p->bindings_in_scope = NULL;
810 p->stmt_exprs = 0;
811 p->left_stmt_expr = false;
814 /* Update spot bindings P as we pop out of SCOPE. Return true if we
815 should push decls for a label. */
817 static bool
818 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
820 if (p->scope != scope)
822 /* This label or goto is defined in some other scope, or it is a
823 label which is not yet defined. There is nothing to
824 update. */
825 return false;
828 /* Adjust the spot bindings to refer to the bindings already defined
829 in the enclosing scope. */
830 p->scope = scope->outer;
831 p->bindings_in_scope = p->scope->bindings;
833 return true;
836 /* The Objective-C front-end often needs to determine the current scope. */
838 void *
839 objc_get_current_scope (void)
841 return current_scope;
844 /* The following function is used only by Objective-C. It needs to live here
845 because it accesses the innards of c_scope. */
847 void
848 objc_mark_locals_volatile (void *enclosing_blk)
850 struct c_scope *scope;
851 struct c_binding *b;
853 for (scope = current_scope;
854 scope && scope != enclosing_blk;
855 scope = scope->outer)
857 for (b = scope->bindings; b; b = b->prev)
858 objc_volatilize_decl (b->decl);
860 /* Do not climb up past the current function. */
861 if (scope->function_body)
862 break;
866 /* Return true if we are in the global binding level. */
868 bool
869 global_bindings_p (void)
871 return current_scope == file_scope;
874 void
875 keep_next_level (void)
877 keep_next_level_flag = true;
880 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
882 void
883 set_float_const_decimal64 (void)
885 current_scope->float_const_decimal64 = true;
888 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
890 void
891 clear_float_const_decimal64 (void)
893 current_scope->float_const_decimal64 = false;
896 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
898 bool
899 float_const_decimal64_p (void)
901 return current_scope->float_const_decimal64;
904 /* Identify this scope as currently being filled with parameters. */
906 void
907 declare_parm_level (void)
909 current_scope->parm_flag = true;
912 void
913 push_scope (void)
915 if (next_is_function_body)
917 /* This is the transition from the parameters to the top level
918 of the function body. These are the same scope
919 (C99 6.2.1p4,6) so we do not push another scope structure.
920 next_is_function_body is set only by store_parm_decls, which
921 in turn is called when and only when we are about to
922 encounter the opening curly brace for the function body.
924 The outermost block of a function always gets a BLOCK node,
925 because the debugging output routines expect that each
926 function has at least one BLOCK. */
927 current_scope->parm_flag = false;
928 current_scope->function_body = true;
929 current_scope->keep = true;
930 current_scope->outer_function = current_function_scope;
931 current_function_scope = current_scope;
933 keep_next_level_flag = false;
934 next_is_function_body = false;
936 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
937 if (current_scope->outer)
938 current_scope->float_const_decimal64
939 = current_scope->outer->float_const_decimal64;
940 else
941 current_scope->float_const_decimal64 = false;
943 else
945 struct c_scope *scope;
946 if (scope_freelist)
948 scope = scope_freelist;
949 scope_freelist = scope->outer;
951 else
952 scope = ggc_alloc_cleared_c_scope ();
954 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
955 if (current_scope)
956 scope->float_const_decimal64 = current_scope->float_const_decimal64;
957 else
958 scope->float_const_decimal64 = false;
960 scope->keep = keep_next_level_flag;
961 scope->outer = current_scope;
962 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
964 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
965 possible. */
966 if (current_scope && scope->depth == 0)
968 scope->depth--;
969 sorry ("GCC supports only %u nested scopes", scope->depth);
972 current_scope = scope;
973 keep_next_level_flag = false;
977 /* This is called when we are leaving SCOPE. For each label defined
978 in SCOPE, add any appropriate decls to its decls_in_scope fields.
979 These are the decls whose initialization will be skipped by a goto
980 later in the function. */
982 static void
983 update_label_decls (struct c_scope *scope)
985 struct c_scope *s;
987 s = scope;
988 while (s != NULL)
990 if (s->has_label_bindings)
992 struct c_binding *b;
994 for (b = s->bindings; b != NULL; b = b->prev)
996 struct c_label_vars *label_vars;
997 struct c_binding *b1;
998 bool hjud;
999 unsigned int ix;
1000 struct c_goto_bindings *g;
1002 if (TREE_CODE (b->decl) != LABEL_DECL)
1003 continue;
1004 label_vars = b->u.label;
1006 b1 = label_vars->label_bindings.bindings_in_scope;
1007 if (label_vars->label_bindings.scope == NULL)
1008 hjud = false;
1009 else
1010 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1011 if (update_spot_bindings (scope, &label_vars->label_bindings))
1013 /* This label is defined in this scope. */
1014 if (hjud)
1016 for (; b1 != NULL; b1 = b1->prev)
1018 /* A goto from later in the function to this
1019 label will never see the initialization
1020 of B1, if any. Save it to issue a
1021 warning if needed. */
1022 if (decl_jump_unsafe (b1->decl))
1023 VEC_safe_push (tree, gc,
1024 label_vars->decls_in_scope,
1025 b1->decl);
1030 /* Update the bindings of any goto statements associated
1031 with this label. */
1032 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1033 update_spot_bindings (scope, &g->goto_bindings);
1037 /* Don't search beyond the current function. */
1038 if (s == current_function_scope)
1039 break;
1041 s = s->outer;
1045 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1047 static void
1048 set_type_context (tree type, tree context)
1050 for (type = TYPE_MAIN_VARIANT (type); type;
1051 type = TYPE_NEXT_VARIANT (type))
1052 TYPE_CONTEXT (type) = context;
1055 /* Exit a scope. Restore the state of the identifier-decl mappings
1056 that were in effect when this scope was entered. Return a BLOCK
1057 node containing all the DECLs in this scope that are of interest
1058 to debug info generation. */
1060 tree
1061 pop_scope (void)
1063 struct c_scope *scope = current_scope;
1064 tree block, context, p;
1065 struct c_binding *b;
1067 bool functionbody = scope->function_body;
1068 bool keep = functionbody || scope->keep || scope->bindings;
1070 update_label_decls (scope);
1072 /* If appropriate, create a BLOCK to record the decls for the life
1073 of this function. */
1074 block = 0;
1075 if (keep)
1077 block = make_node (BLOCK);
1078 BLOCK_SUBBLOCKS (block) = scope->blocks;
1079 TREE_USED (block) = 1;
1081 /* In each subblock, record that this is its superior. */
1082 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1083 BLOCK_SUPERCONTEXT (p) = block;
1085 BLOCK_VARS (block) = 0;
1088 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1089 scope must be set so that they point to the appropriate
1090 construct, i.e. either to the current FUNCTION_DECL node, or
1091 else to the BLOCK node we just constructed.
1093 Note that for tagged types whose scope is just the formal
1094 parameter list for some function type specification, we can't
1095 properly set their TYPE_CONTEXTs here, because we don't have a
1096 pointer to the appropriate FUNCTION_TYPE node readily available
1097 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1098 type nodes get set in `grokdeclarator' as soon as we have created
1099 the FUNCTION_TYPE node which will represent the "scope" for these
1100 "parameter list local" tagged types. */
1101 if (scope->function_body)
1102 context = current_function_decl;
1103 else if (scope == file_scope)
1105 tree file_decl = build_translation_unit_decl (NULL_TREE);
1106 context = file_decl;
1108 else
1109 context = block;
1111 /* Clear all bindings in this scope. */
1112 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1114 p = b->decl;
1115 switch (TREE_CODE (p))
1117 case LABEL_DECL:
1118 /* Warnings for unused labels, errors for undefined labels. */
1119 if (TREE_USED (p) && !DECL_INITIAL (p))
1121 error ("label %q+D used but not defined", p);
1122 DECL_INITIAL (p) = error_mark_node;
1124 else
1125 warn_for_unused_label (p);
1127 /* Labels go in BLOCK_VARS. */
1128 DECL_CHAIN (p) = BLOCK_VARS (block);
1129 BLOCK_VARS (block) = p;
1130 gcc_assert (I_LABEL_BINDING (b->id) == b);
1131 I_LABEL_BINDING (b->id) = b->shadowed;
1133 /* Also pop back to the shadowed label_vars. */
1134 release_tree_vector (b->u.label->decls_in_scope);
1135 b->u.label = b->u.label->shadowed;
1136 break;
1138 case ENUMERAL_TYPE:
1139 case UNION_TYPE:
1140 case RECORD_TYPE:
1141 set_type_context (p, context);
1143 /* Types may not have tag-names, in which case the type
1144 appears in the bindings list with b->id NULL. */
1145 if (b->id)
1147 gcc_assert (I_TAG_BINDING (b->id) == b);
1148 I_TAG_BINDING (b->id) = b->shadowed;
1150 break;
1152 case FUNCTION_DECL:
1153 /* Propagate TREE_ADDRESSABLE from nested functions to their
1154 containing functions. */
1155 if (!TREE_ASM_WRITTEN (p)
1156 && DECL_INITIAL (p) != 0
1157 && TREE_ADDRESSABLE (p)
1158 && DECL_ABSTRACT_ORIGIN (p) != 0
1159 && DECL_ABSTRACT_ORIGIN (p) != p)
1160 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1161 if (!DECL_EXTERNAL (p)
1162 && !DECL_INITIAL (p)
1163 && scope != file_scope
1164 && scope != external_scope)
1166 error ("nested function %q+D declared but never defined", p);
1167 undef_nested_function = true;
1169 else if (DECL_DECLARED_INLINE_P (p)
1170 && TREE_PUBLIC (p)
1171 && !DECL_INITIAL (p))
1173 /* C99 6.7.4p6: "a function with external linkage... declared
1174 with an inline function specifier ... shall also be defined
1175 in the same translation unit." */
1176 if (!flag_gnu89_inline)
1177 pedwarn (input_location, 0,
1178 "inline function %q+D declared but never defined", p);
1179 DECL_EXTERNAL (p) = 1;
1182 goto common_symbol;
1184 case VAR_DECL:
1185 /* Warnings for unused variables. */
1186 if ((!TREE_USED (p) || !DECL_READ_P (p))
1187 && !TREE_NO_WARNING (p)
1188 && !DECL_IN_SYSTEM_HEADER (p)
1189 && DECL_NAME (p)
1190 && !DECL_ARTIFICIAL (p)
1191 && scope != file_scope
1192 && scope != external_scope)
1194 if (!TREE_USED (p))
1195 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1196 else if (DECL_CONTEXT (p) == current_function_decl)
1197 warning_at (DECL_SOURCE_LOCATION (p),
1198 OPT_Wunused_but_set_variable,
1199 "variable %qD set but not used", p);
1202 if (b->inner_comp)
1204 error ("type of array %q+D completed incompatibly with"
1205 " implicit initialization", p);
1208 /* Fall through. */
1209 case TYPE_DECL:
1210 case CONST_DECL:
1211 common_symbol:
1212 /* All of these go in BLOCK_VARS, but only if this is the
1213 binding in the home scope. */
1214 if (!b->nested)
1216 DECL_CHAIN (p) = BLOCK_VARS (block);
1217 BLOCK_VARS (block) = p;
1219 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1221 /* For block local externs add a special
1222 DECL_EXTERNAL decl for debug info generation. */
1223 tree extp = copy_node (p);
1225 DECL_EXTERNAL (extp) = 1;
1226 TREE_STATIC (extp) = 0;
1227 TREE_PUBLIC (extp) = 1;
1228 DECL_INITIAL (extp) = NULL_TREE;
1229 DECL_LANG_SPECIFIC (extp) = NULL;
1230 DECL_CONTEXT (extp) = current_function_decl;
1231 if (TREE_CODE (p) == FUNCTION_DECL)
1233 DECL_RESULT (extp) = NULL_TREE;
1234 DECL_SAVED_TREE (extp) = NULL_TREE;
1235 DECL_STRUCT_FUNCTION (extp) = NULL;
1237 if (b->locus != UNKNOWN_LOCATION)
1238 DECL_SOURCE_LOCATION (extp) = b->locus;
1239 DECL_CHAIN (extp) = BLOCK_VARS (block);
1240 BLOCK_VARS (block) = extp;
1242 /* If this is the file scope set DECL_CONTEXT of each decl to
1243 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1244 work. */
1245 if (scope == file_scope)
1247 DECL_CONTEXT (p) = context;
1248 if (TREE_CODE (p) == TYPE_DECL
1249 && TREE_TYPE (p) != error_mark_node)
1250 set_type_context (TREE_TYPE (p), context);
1253 /* Fall through. */
1254 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1255 already been put there by store_parm_decls. Unused-
1256 parameter warnings are handled by function.c.
1257 error_mark_node obviously does not go in BLOCK_VARS and
1258 does not get unused-variable warnings. */
1259 case PARM_DECL:
1260 case ERROR_MARK:
1261 /* It is possible for a decl not to have a name. We get
1262 here with b->id NULL in this case. */
1263 if (b->id)
1265 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1266 I_SYMBOL_BINDING (b->id) = b->shadowed;
1267 if (b->shadowed && b->shadowed->u.type)
1268 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1270 break;
1272 default:
1273 gcc_unreachable ();
1278 /* Dispose of the block that we just made inside some higher level. */
1279 if ((scope->function_body || scope == file_scope) && context)
1281 DECL_INITIAL (context) = block;
1282 BLOCK_SUPERCONTEXT (block) = context;
1284 else if (scope->outer)
1286 if (block)
1287 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1288 /* If we did not make a block for the scope just exited, any
1289 blocks made for inner scopes must be carried forward so they
1290 will later become subblocks of something else. */
1291 else if (scope->blocks)
1292 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1295 /* Pop the current scope, and free the structure for reuse. */
1296 current_scope = scope->outer;
1297 if (scope->function_body)
1298 current_function_scope = scope->outer_function;
1300 memset (scope, 0, sizeof (struct c_scope));
1301 scope->outer = scope_freelist;
1302 scope_freelist = scope;
1304 return block;
1307 void
1308 push_file_scope (void)
1310 tree decl;
1312 if (file_scope)
1313 return;
1315 push_scope ();
1316 file_scope = current_scope;
1318 start_fname_decls ();
1320 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1321 bind (DECL_NAME (decl), decl, file_scope,
1322 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1325 void
1326 pop_file_scope (void)
1328 /* In case there were missing closebraces, get us back to the global
1329 binding level. */
1330 while (current_scope != file_scope)
1331 pop_scope ();
1333 /* __FUNCTION__ is defined at file scope (""). This
1334 call may not be necessary as my tests indicate it
1335 still works without it. */
1336 finish_fname_decls ();
1338 check_inline_statics ();
1340 /* This is the point to write out a PCH if we're doing that.
1341 In that case we do not want to do anything else. */
1342 if (pch_file)
1344 c_common_write_pch ();
1345 return;
1348 /* Pop off the file scope and close this translation unit. */
1349 pop_scope ();
1350 file_scope = 0;
1352 maybe_apply_pending_pragma_weaks ();
1355 /* Adjust the bindings for the start of a statement expression. */
1357 void
1358 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1360 struct c_scope *scope;
1362 for (scope = current_scope; scope != NULL; scope = scope->outer)
1364 struct c_binding *b;
1366 if (!scope->has_label_bindings)
1367 continue;
1369 for (b = scope->bindings; b != NULL; b = b->prev)
1371 struct c_label_vars *label_vars;
1372 unsigned int ix;
1373 struct c_goto_bindings *g;
1375 if (TREE_CODE (b->decl) != LABEL_DECL)
1376 continue;
1377 label_vars = b->u.label;
1378 ++label_vars->label_bindings.stmt_exprs;
1379 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1380 ++g->goto_bindings.stmt_exprs;
1384 if (switch_bindings != NULL)
1385 ++switch_bindings->stmt_exprs;
1388 /* Adjust the bindings for the end of a statement expression. */
1390 void
1391 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1393 struct c_scope *scope;
1395 for (scope = current_scope; scope != NULL; scope = scope->outer)
1397 struct c_binding *b;
1399 if (!scope->has_label_bindings)
1400 continue;
1402 for (b = scope->bindings; b != NULL; b = b->prev)
1404 struct c_label_vars *label_vars;
1405 unsigned int ix;
1406 struct c_goto_bindings *g;
1408 if (TREE_CODE (b->decl) != LABEL_DECL)
1409 continue;
1410 label_vars = b->u.label;
1411 --label_vars->label_bindings.stmt_exprs;
1412 if (label_vars->label_bindings.stmt_exprs < 0)
1414 label_vars->label_bindings.left_stmt_expr = true;
1415 label_vars->label_bindings.stmt_exprs = 0;
1417 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1419 --g->goto_bindings.stmt_exprs;
1420 if (g->goto_bindings.stmt_exprs < 0)
1422 g->goto_bindings.left_stmt_expr = true;
1423 g->goto_bindings.stmt_exprs = 0;
1429 if (switch_bindings != NULL)
1431 --switch_bindings->stmt_exprs;
1432 gcc_assert (switch_bindings->stmt_exprs >= 0);
1436 /* Push a definition or a declaration of struct, union or enum tag "name".
1437 "type" should be the type node.
1438 We assume that the tag "name" is not already defined, and has a location
1439 of LOC.
1441 Note that the definition may really be just a forward reference.
1442 In that case, the TYPE_SIZE will be zero. */
1444 static void
1445 pushtag (location_t loc, tree name, tree type)
1447 /* Record the identifier as the type's name if it has none. */
1448 if (name && !TYPE_NAME (type))
1449 TYPE_NAME (type) = name;
1450 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1452 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1453 tagged type we just added to the current scope. This fake
1454 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1455 to output a representation of a tagged type, and it also gives
1456 us a convenient place to record the "scope start" address for the
1457 tagged type. */
1459 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1460 TYPE_DECL, NULL_TREE, type));
1462 /* An approximation for now, so we can tell this is a function-scope tag.
1463 This will be updated in pop_scope. */
1464 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1466 if (warn_cxx_compat && name != NULL_TREE)
1468 struct c_binding *b = I_SYMBOL_BINDING (name);
1470 if (b != NULL
1471 && b->decl != NULL_TREE
1472 && TREE_CODE (b->decl) == TYPE_DECL
1473 && (B_IN_CURRENT_SCOPE (b)
1474 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1475 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1476 != TYPE_MAIN_VARIANT (type)))
1478 warning_at (loc, OPT_Wc___compat,
1479 ("using %qD as both a typedef and a tag is "
1480 "invalid in C++"),
1481 b->decl);
1482 if (b->locus != UNKNOWN_LOCATION)
1483 inform (b->locus, "originally defined here");
1488 /* Subroutine of compare_decls. Allow harmless mismatches in return
1489 and argument types provided that the type modes match. This function
1490 return a unified type given a suitable match, and 0 otherwise. */
1492 static tree
1493 match_builtin_function_types (tree newtype, tree oldtype)
1495 tree newrettype, oldrettype;
1496 tree newargs, oldargs;
1497 tree trytype, tryargs;
1499 /* Accept the return type of the new declaration if same modes. */
1500 oldrettype = TREE_TYPE (oldtype);
1501 newrettype = TREE_TYPE (newtype);
1503 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1504 return 0;
1506 oldargs = TYPE_ARG_TYPES (oldtype);
1507 newargs = TYPE_ARG_TYPES (newtype);
1508 tryargs = newargs;
1510 while (oldargs || newargs)
1512 if (!oldargs
1513 || !newargs
1514 || !TREE_VALUE (oldargs)
1515 || !TREE_VALUE (newargs)
1516 || TYPE_MODE (TREE_VALUE (oldargs))
1517 != TYPE_MODE (TREE_VALUE (newargs)))
1518 return 0;
1520 oldargs = TREE_CHAIN (oldargs);
1521 newargs = TREE_CHAIN (newargs);
1524 trytype = build_function_type (newrettype, tryargs);
1525 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1528 /* Subroutine of diagnose_mismatched_decls. Check for function type
1529 mismatch involving an empty arglist vs a nonempty one and give clearer
1530 diagnostics. */
1531 static void
1532 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1533 tree newtype, tree oldtype)
1535 tree t;
1537 if (TREE_CODE (olddecl) != FUNCTION_DECL
1538 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1539 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1540 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1541 return;
1543 t = TYPE_ARG_TYPES (oldtype);
1544 if (t == 0)
1545 t = TYPE_ARG_TYPES (newtype);
1546 for (; t; t = TREE_CHAIN (t))
1548 tree type = TREE_VALUE (t);
1550 if (TREE_CHAIN (t) == 0
1551 && TYPE_MAIN_VARIANT (type) != void_type_node)
1553 inform (input_location, "a parameter list with an ellipsis can%'t match "
1554 "an empty parameter name list declaration");
1555 break;
1558 if (c_type_promotes_to (type) != type)
1560 inform (input_location, "an argument type that has a default promotion can%'t match "
1561 "an empty parameter name list declaration");
1562 break;
1567 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1568 old-style function definition, NEWDECL is a prototype declaration.
1569 Diagnose inconsistencies in the argument list. Returns TRUE if
1570 the prototype is compatible, FALSE if not. */
1571 static bool
1572 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1574 tree newargs, oldargs;
1575 int i;
1577 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1579 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1580 newargs = TYPE_ARG_TYPES (newtype);
1581 i = 1;
1583 for (;;)
1585 tree oldargtype = TREE_VALUE (oldargs);
1586 tree newargtype = TREE_VALUE (newargs);
1588 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1589 return false;
1591 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1592 newargtype = TYPE_MAIN_VARIANT (newargtype);
1594 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1595 break;
1597 /* Reaching the end of just one list means the two decls don't
1598 agree on the number of arguments. */
1599 if (END_OF_ARGLIST (oldargtype))
1601 error ("prototype for %q+D declares more arguments "
1602 "than previous old-style definition", newdecl);
1603 return false;
1605 else if (END_OF_ARGLIST (newargtype))
1607 error ("prototype for %q+D declares fewer arguments "
1608 "than previous old-style definition", newdecl);
1609 return false;
1612 /* Type for passing arg must be consistent with that declared
1613 for the arg. */
1614 else if (!comptypes (oldargtype, newargtype))
1616 error ("prototype for %q+D declares argument %d"
1617 " with incompatible type",
1618 newdecl, i);
1619 return false;
1622 oldargs = TREE_CHAIN (oldargs);
1623 newargs = TREE_CHAIN (newargs);
1624 i++;
1627 /* If we get here, no errors were found, but do issue a warning
1628 for this poor-style construct. */
1629 warning (0, "prototype for %q+D follows non-prototype definition",
1630 newdecl);
1631 return true;
1632 #undef END_OF_ARGLIST
1635 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1636 first in a pair of mismatched declarations, using the diagnostic
1637 function DIAG. */
1638 static void
1639 locate_old_decl (tree decl)
1641 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1643 else if (DECL_INITIAL (decl))
1644 inform (input_location, "previous definition of %q+D was here", decl);
1645 else if (C_DECL_IMPLICIT (decl))
1646 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1647 else
1648 inform (input_location, "previous declaration of %q+D was here", decl);
1651 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1652 Returns true if the caller should proceed to merge the two, false
1653 if OLDDECL should simply be discarded. As a side effect, issues
1654 all necessary diagnostics for invalid or poor-style combinations.
1655 If it returns true, writes the types of NEWDECL and OLDDECL to
1656 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1657 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1659 static bool
1660 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1661 tree *newtypep, tree *oldtypep)
1663 tree newtype, oldtype;
1664 bool pedwarned = false;
1665 bool warned = false;
1666 bool retval = true;
1668 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1669 && DECL_EXTERNAL (DECL))
1671 /* If we have error_mark_node for either decl or type, just discard
1672 the previous decl - we're in an error cascade already. */
1673 if (olddecl == error_mark_node || newdecl == error_mark_node)
1674 return false;
1675 *oldtypep = oldtype = TREE_TYPE (olddecl);
1676 *newtypep = newtype = TREE_TYPE (newdecl);
1677 if (oldtype == error_mark_node || newtype == error_mark_node)
1678 return false;
1680 /* Two different categories of symbol altogether. This is an error
1681 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1682 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1684 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1685 && DECL_BUILT_IN (olddecl)
1686 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1688 error ("%q+D redeclared as different kind of symbol", newdecl);
1689 locate_old_decl (olddecl);
1691 else if (TREE_PUBLIC (newdecl))
1692 warning (0, "built-in function %q+D declared as non-function",
1693 newdecl);
1694 else
1695 warning (OPT_Wshadow, "declaration of %q+D shadows "
1696 "a built-in function", newdecl);
1697 return false;
1700 /* Enumerators have no linkage, so may only be declared once in a
1701 given scope. */
1702 if (TREE_CODE (olddecl) == CONST_DECL)
1704 error ("redeclaration of enumerator %q+D", newdecl);
1705 locate_old_decl (olddecl);
1706 return false;
1709 if (!comptypes (oldtype, newtype))
1711 if (TREE_CODE (olddecl) == FUNCTION_DECL
1712 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1714 /* Accept harmless mismatch in function types.
1715 This is for the ffs and fprintf builtins. */
1716 tree trytype = match_builtin_function_types (newtype, oldtype);
1718 if (trytype && comptypes (newtype, trytype))
1719 *oldtypep = oldtype = trytype;
1720 else
1722 /* If types don't match for a built-in, throw away the
1723 built-in. No point in calling locate_old_decl here, it
1724 won't print anything. */
1725 warning (0, "conflicting types for built-in function %q+D",
1726 newdecl);
1727 return false;
1730 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1731 && DECL_IS_BUILTIN (olddecl))
1733 /* A conflicting function declaration for a predeclared
1734 function that isn't actually built in. Objective C uses
1735 these. The new declaration silently overrides everything
1736 but the volatility (i.e. noreturn) indication. See also
1737 below. FIXME: Make Objective C use normal builtins. */
1738 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1739 return false;
1741 /* Permit void foo (...) to match int foo (...) if the latter is
1742 the definition and implicit int was used. See
1743 c-torture/compile/920625-2.c. */
1744 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1745 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1746 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1747 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1749 pedwarned = pedwarn (input_location, 0,
1750 "conflicting types for %q+D", newdecl);
1751 /* Make sure we keep void as the return type. */
1752 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1753 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1755 /* Permit void foo (...) to match an earlier call to foo (...) with
1756 no declared type (thus, implicitly int). */
1757 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1758 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1759 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1760 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1762 pedwarned = pedwarn (input_location, 0,
1763 "conflicting types for %q+D", newdecl);
1764 /* Make sure we keep void as the return type. */
1765 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1767 else
1769 int new_quals = TYPE_QUALS (newtype);
1770 int old_quals = TYPE_QUALS (oldtype);
1772 if (new_quals != old_quals)
1774 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1775 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1776 if (new_addr != old_addr)
1778 if (ADDR_SPACE_GENERIC_P (new_addr))
1779 error ("conflicting named address spaces (generic vs %s) "
1780 "for %q+D",
1781 c_addr_space_name (old_addr), newdecl);
1782 else if (ADDR_SPACE_GENERIC_P (old_addr))
1783 error ("conflicting named address spaces (%s vs generic) "
1784 "for %q+D",
1785 c_addr_space_name (new_addr), newdecl);
1786 else
1787 error ("conflicting named address spaces (%s vs %s) "
1788 "for %q+D",
1789 c_addr_space_name (new_addr),
1790 c_addr_space_name (old_addr),
1791 newdecl);
1794 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1795 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1796 error ("conflicting type qualifiers for %q+D", newdecl);
1798 else
1799 error ("conflicting types for %q+D", newdecl);
1800 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1801 locate_old_decl (olddecl);
1802 return false;
1806 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1807 but silently ignore the redeclaration if either is in a system
1808 header. (Conflicting redeclarations were handled above.) This
1809 is allowed for C11 if the types are the same, not just
1810 compatible. */
1811 if (TREE_CODE (newdecl) == TYPE_DECL)
1813 bool types_different = false;
1814 int comptypes_result;
1816 comptypes_result
1817 = comptypes_check_different_types (oldtype, newtype, &types_different);
1819 if (comptypes_result != 1 || types_different)
1821 error ("redefinition of typedef %q+D with different type", newdecl);
1822 locate_old_decl (olddecl);
1823 return false;
1826 if (DECL_IN_SYSTEM_HEADER (newdecl)
1827 || DECL_IN_SYSTEM_HEADER (olddecl)
1828 || TREE_NO_WARNING (newdecl)
1829 || TREE_NO_WARNING (olddecl))
1830 return true; /* Allow OLDDECL to continue in use. */
1832 if (variably_modified_type_p (newtype, NULL))
1834 error ("redefinition of typedef %q+D with variably modified type",
1835 newdecl);
1836 locate_old_decl (olddecl);
1838 else if (pedantic && !flag_isoc11)
1840 pedwarn (input_location, OPT_pedantic,
1841 "redefinition of typedef %q+D", newdecl);
1842 locate_old_decl (olddecl);
1845 return true;
1848 /* Function declarations can either be 'static' or 'extern' (no
1849 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1850 can never conflict with each other on account of linkage
1851 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1852 gnu89 mode permits two definitions if one is 'extern inline' and
1853 one is not. The non- extern-inline definition supersedes the
1854 extern-inline definition. */
1856 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1858 /* If you declare a built-in function name as static, or
1859 define the built-in with an old-style definition (so we
1860 can't validate the argument list) the built-in definition is
1861 overridden, but optionally warn this was a bad choice of name. */
1862 if (DECL_BUILT_IN (olddecl)
1863 && !C_DECL_DECLARED_BUILTIN (olddecl)
1864 && (!TREE_PUBLIC (newdecl)
1865 || (DECL_INITIAL (newdecl)
1866 && !prototype_p (TREE_TYPE (newdecl)))))
1868 warning (OPT_Wshadow, "declaration of %q+D shadows "
1869 "a built-in function", newdecl);
1870 /* Discard the old built-in function. */
1871 return false;
1874 if (DECL_INITIAL (newdecl))
1876 if (DECL_INITIAL (olddecl))
1878 /* If both decls are in the same TU and the new declaration
1879 isn't overriding an extern inline reject the new decl.
1880 In c99, no overriding is allowed in the same translation
1881 unit. */
1882 if ((!DECL_EXTERN_INLINE (olddecl)
1883 || DECL_EXTERN_INLINE (newdecl)
1884 || (!flag_gnu89_inline
1885 && (!DECL_DECLARED_INLINE_P (olddecl)
1886 || !lookup_attribute ("gnu_inline",
1887 DECL_ATTRIBUTES (olddecl)))
1888 && (!DECL_DECLARED_INLINE_P (newdecl)
1889 || !lookup_attribute ("gnu_inline",
1890 DECL_ATTRIBUTES (newdecl))))
1892 && same_translation_unit_p (newdecl, olddecl))
1894 error ("redefinition of %q+D", newdecl);
1895 locate_old_decl (olddecl);
1896 return false;
1900 /* If we have a prototype after an old-style function definition,
1901 the argument types must be checked specially. */
1902 else if (DECL_INITIAL (olddecl)
1903 && !prototype_p (oldtype) && prototype_p (newtype)
1904 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1905 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1907 locate_old_decl (olddecl);
1908 return false;
1910 /* A non-static declaration (even an "extern") followed by a
1911 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1912 The same is true for a static forward declaration at block
1913 scope followed by a non-static declaration/definition at file
1914 scope. Static followed by non-static at the same scope is
1915 not undefined behavior, and is the most convenient way to get
1916 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1917 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1918 we do diagnose it if -Wtraditional. */
1919 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1921 /* Two exceptions to the rule. If olddecl is an extern
1922 inline, or a predeclared function that isn't actually
1923 built in, newdecl silently overrides olddecl. The latter
1924 occur only in Objective C; see also above. (FIXME: Make
1925 Objective C use normal builtins.) */
1926 if (!DECL_IS_BUILTIN (olddecl)
1927 && !DECL_EXTERN_INLINE (olddecl))
1929 error ("static declaration of %q+D follows "
1930 "non-static declaration", newdecl);
1931 locate_old_decl (olddecl);
1933 return false;
1935 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1937 if (DECL_CONTEXT (olddecl))
1939 error ("non-static declaration of %q+D follows "
1940 "static declaration", newdecl);
1941 locate_old_decl (olddecl);
1942 return false;
1944 else if (warn_traditional)
1946 warned |= warning (OPT_Wtraditional,
1947 "non-static declaration of %q+D "
1948 "follows static declaration", newdecl);
1952 /* Make sure gnu_inline attribute is either not present, or
1953 present on all inline decls. */
1954 if (DECL_DECLARED_INLINE_P (olddecl)
1955 && DECL_DECLARED_INLINE_P (newdecl))
1957 bool newa = lookup_attribute ("gnu_inline",
1958 DECL_ATTRIBUTES (newdecl)) != NULL;
1959 bool olda = lookup_attribute ("gnu_inline",
1960 DECL_ATTRIBUTES (olddecl)) != NULL;
1961 if (newa != olda)
1963 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1964 newa ? newdecl : olddecl);
1965 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1966 "but not here");
1970 else if (TREE_CODE (newdecl) == VAR_DECL)
1972 /* Only variables can be thread-local, and all declarations must
1973 agree on this property. */
1974 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1976 /* Nothing to check. Since OLDDECL is marked threadprivate
1977 and NEWDECL does not have a thread-local attribute, we
1978 will merge the threadprivate attribute into NEWDECL. */
1981 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1983 if (DECL_THREAD_LOCAL_P (newdecl))
1984 error ("thread-local declaration of %q+D follows "
1985 "non-thread-local declaration", newdecl);
1986 else
1987 error ("non-thread-local declaration of %q+D follows "
1988 "thread-local declaration", newdecl);
1990 locate_old_decl (olddecl);
1991 return false;
1994 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1995 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1997 error ("redefinition of %q+D", newdecl);
1998 locate_old_decl (olddecl);
1999 return false;
2002 /* Objects declared at file scope: if the first declaration had
2003 external linkage (even if it was an external reference) the
2004 second must have external linkage as well, or the behavior is
2005 undefined. If the first declaration had internal linkage, then
2006 the second must too, or else be an external reference (in which
2007 case the composite declaration still has internal linkage).
2008 As for function declarations, we warn about the static-then-
2009 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2010 if (DECL_FILE_SCOPE_P (newdecl)
2011 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2013 if (DECL_EXTERNAL (newdecl))
2015 if (!DECL_FILE_SCOPE_P (olddecl))
2017 error ("extern declaration of %q+D follows "
2018 "declaration with no linkage", newdecl);
2019 locate_old_decl (olddecl);
2020 return false;
2022 else if (warn_traditional)
2024 warned |= warning (OPT_Wtraditional,
2025 "non-static declaration of %q+D "
2026 "follows static declaration", newdecl);
2029 else
2031 if (TREE_PUBLIC (newdecl))
2032 error ("non-static declaration of %q+D follows "
2033 "static declaration", newdecl);
2034 else
2035 error ("static declaration of %q+D follows "
2036 "non-static declaration", newdecl);
2038 locate_old_decl (olddecl);
2039 return false;
2042 /* Two objects with the same name declared at the same block
2043 scope must both be external references (6.7p3). */
2044 else if (!DECL_FILE_SCOPE_P (newdecl))
2046 if (DECL_EXTERNAL (newdecl))
2048 /* Extern with initializer at block scope, which will
2049 already have received an error. */
2051 else if (DECL_EXTERNAL (olddecl))
2053 error ("declaration of %q+D with no linkage follows "
2054 "extern declaration", newdecl);
2055 locate_old_decl (olddecl);
2057 else
2059 error ("redeclaration of %q+D with no linkage", newdecl);
2060 locate_old_decl (olddecl);
2063 return false;
2066 /* C++ does not permit a decl to appear multiple times at file
2067 scope. */
2068 if (warn_cxx_compat
2069 && DECL_FILE_SCOPE_P (newdecl)
2070 && !DECL_EXTERNAL (newdecl)
2071 && !DECL_EXTERNAL (olddecl))
2072 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2073 OPT_Wc___compat,
2074 ("duplicate declaration of %qD is "
2075 "invalid in C++"),
2076 newdecl);
2079 /* warnings */
2080 /* All decls must agree on a visibility. */
2081 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2082 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2083 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2085 warned |= warning (0, "redeclaration of %q+D with different visibility "
2086 "(old visibility preserved)", newdecl);
2089 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2091 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2092 if (DECL_DECLARED_INLINE_P (newdecl)
2093 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2095 warned |= warning (OPT_Wattributes,
2096 "inline declaration of %qD follows "
2097 "declaration with attribute noinline", newdecl);
2099 else if (DECL_DECLARED_INLINE_P (olddecl)
2100 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2102 warned |= warning (OPT_Wattributes,
2103 "declaration of %q+D with attribute "
2104 "noinline follows inline declaration ", newdecl);
2107 else /* PARM_DECL, VAR_DECL */
2109 /* Redeclaration of a parameter is a constraint violation (this is
2110 not explicitly stated, but follows from C99 6.7p3 [no more than
2111 one declaration of the same identifier with no linkage in the
2112 same scope, except type tags] and 6.2.2p6 [parameters have no
2113 linkage]). We must check for a forward parameter declaration,
2114 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2115 an extension, the mandatory diagnostic for which is handled by
2116 mark_forward_parm_decls. */
2118 if (TREE_CODE (newdecl) == PARM_DECL
2119 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2121 error ("redefinition of parameter %q+D", newdecl);
2122 locate_old_decl (olddecl);
2123 return false;
2127 /* Optional warning for completely redundant decls. */
2128 if (!warned && !pedwarned
2129 && warn_redundant_decls
2130 /* Don't warn about a function declaration followed by a
2131 definition. */
2132 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2133 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2134 /* Don't warn about redundant redeclarations of builtins. */
2135 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2136 && !DECL_BUILT_IN (newdecl)
2137 && DECL_BUILT_IN (olddecl)
2138 && !C_DECL_DECLARED_BUILTIN (olddecl))
2139 /* Don't warn about an extern followed by a definition. */
2140 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2141 /* Don't warn about forward parameter decls. */
2142 && !(TREE_CODE (newdecl) == PARM_DECL
2143 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2144 /* Don't warn about a variable definition following a declaration. */
2145 && !(TREE_CODE (newdecl) == VAR_DECL
2146 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2148 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2149 newdecl);
2152 /* Report location of previous decl/defn. */
2153 if (warned || pedwarned)
2154 locate_old_decl (olddecl);
2156 #undef DECL_EXTERN_INLINE
2158 return retval;
2161 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2162 consistent with OLDDECL, but carries new information. Merge the
2163 new information into OLDDECL. This function issues no
2164 diagnostics. */
2166 static void
2167 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2169 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2170 && DECL_INITIAL (newdecl) != 0);
2171 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2172 && prototype_p (TREE_TYPE (newdecl)));
2173 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2174 && prototype_p (TREE_TYPE (olddecl)));
2175 bool extern_changed = false;
2177 /* For real parm decl following a forward decl, rechain the old decl
2178 in its new location and clear TREE_ASM_WRITTEN (it's not a
2179 forward decl anymore). */
2180 if (TREE_CODE (newdecl) == PARM_DECL
2181 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2183 struct c_binding *b, **here;
2185 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2186 if ((*here)->decl == olddecl)
2187 goto found;
2188 gcc_unreachable ();
2190 found:
2191 b = *here;
2192 *here = b->prev;
2193 b->prev = current_scope->bindings;
2194 current_scope->bindings = b;
2196 TREE_ASM_WRITTEN (olddecl) = 0;
2199 DECL_ATTRIBUTES (newdecl)
2200 = targetm.merge_decl_attributes (olddecl, newdecl);
2202 /* Merge the data types specified in the two decls. */
2203 TREE_TYPE (newdecl)
2204 = TREE_TYPE (olddecl)
2205 = composite_type (newtype, oldtype);
2207 /* Lay the type out, unless already done. */
2208 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2210 if (TREE_TYPE (newdecl) != error_mark_node)
2211 layout_type (TREE_TYPE (newdecl));
2212 if (TREE_CODE (newdecl) != FUNCTION_DECL
2213 && TREE_CODE (newdecl) != TYPE_DECL
2214 && TREE_CODE (newdecl) != CONST_DECL)
2215 layout_decl (newdecl, 0);
2217 else
2219 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2220 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2221 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2222 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2223 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2225 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2226 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2230 /* Keep the old rtl since we can safely use it. */
2231 if (HAS_RTL_P (olddecl))
2232 COPY_DECL_RTL (olddecl, newdecl);
2234 /* Merge the type qualifiers. */
2235 if (TREE_READONLY (newdecl))
2236 TREE_READONLY (olddecl) = 1;
2238 if (TREE_THIS_VOLATILE (newdecl))
2239 TREE_THIS_VOLATILE (olddecl) = 1;
2241 /* Merge deprecatedness. */
2242 if (TREE_DEPRECATED (newdecl))
2243 TREE_DEPRECATED (olddecl) = 1;
2245 /* If a decl is in a system header and the other isn't, keep the one on the
2246 system header. Otherwise, keep source location of definition rather than
2247 declaration and of prototype rather than non-prototype unless that
2248 prototype is built-in. */
2249 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2250 && DECL_IN_SYSTEM_HEADER (olddecl)
2251 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2252 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2253 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2254 && DECL_IN_SYSTEM_HEADER (newdecl)
2255 && !DECL_IN_SYSTEM_HEADER (olddecl))
2256 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2257 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2258 || (old_is_prototype && !new_is_prototype
2259 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2260 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2262 /* Merge the initialization information. */
2263 if (DECL_INITIAL (newdecl) == 0)
2264 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2266 /* Merge the threadprivate attribute. */
2267 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2269 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2270 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2273 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2275 /* Merge the section attribute.
2276 We want to issue an error if the sections conflict but that
2277 must be done later in decl_attributes since we are called
2278 before attributes are assigned. */
2279 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2280 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2282 /* Copy the assembler name.
2283 Currently, it can only be defined in the prototype. */
2284 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2286 /* Use visibility of whichever declaration had it specified */
2287 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2289 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2290 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2293 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2295 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2296 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2297 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2298 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2299 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2300 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2301 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2302 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2303 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2304 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2305 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2308 /* Merge the storage class information. */
2309 merge_weak (newdecl, olddecl);
2311 /* For functions, static overrides non-static. */
2312 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2314 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2315 /* This is since we don't automatically
2316 copy the attributes of NEWDECL into OLDDECL. */
2317 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2318 /* If this clears `static', clear it in the identifier too. */
2319 if (!TREE_PUBLIC (olddecl))
2320 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2324 /* In c99, 'extern' declaration before (or after) 'inline' means this
2325 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2326 is present. */
2327 if (TREE_CODE (newdecl) == FUNCTION_DECL
2328 && !flag_gnu89_inline
2329 && (DECL_DECLARED_INLINE_P (newdecl)
2330 || DECL_DECLARED_INLINE_P (olddecl))
2331 && (!DECL_DECLARED_INLINE_P (newdecl)
2332 || !DECL_DECLARED_INLINE_P (olddecl)
2333 || !DECL_EXTERNAL (olddecl))
2334 && DECL_EXTERNAL (newdecl)
2335 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2336 && !current_function_decl)
2337 DECL_EXTERNAL (newdecl) = 0;
2339 if (DECL_EXTERNAL (newdecl))
2341 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2342 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2344 /* An extern decl does not override previous storage class. */
2345 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2346 if (!DECL_EXTERNAL (newdecl))
2348 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2349 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2352 else
2354 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2355 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2358 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2360 /* If we're redefining a function previously defined as extern
2361 inline, make sure we emit debug info for the inline before we
2362 throw it away, in case it was inlined into a function that
2363 hasn't been written out yet. */
2364 if (new_is_definition && DECL_INITIAL (olddecl))
2365 /* The new defn must not be inline. */
2366 DECL_UNINLINABLE (newdecl) = 1;
2367 else
2369 /* If either decl says `inline', this fn is inline, unless
2370 its definition was passed already. */
2371 if (DECL_DECLARED_INLINE_P (newdecl)
2372 || DECL_DECLARED_INLINE_P (olddecl))
2373 DECL_DECLARED_INLINE_P (newdecl) = 1;
2375 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2376 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2378 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2379 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2380 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2381 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2384 if (DECL_BUILT_IN (olddecl))
2386 /* If redeclaring a builtin function, it stays built in.
2387 But it gets tagged as having been declared. */
2388 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2389 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2390 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2391 if (new_is_prototype)
2393 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2394 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2396 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2397 switch (fncode)
2399 /* If a compatible prototype of these builtin functions
2400 is seen, assume the runtime implements it with the
2401 expected semantics. */
2402 case BUILT_IN_STPCPY:
2403 if (builtin_decl_explicit_p (fncode))
2404 set_builtin_decl_implicit_p (fncode, true);
2405 break;
2406 default:
2407 break;
2411 else
2412 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2413 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2416 /* Preserve function specific target and optimization options */
2417 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2418 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2419 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2420 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2422 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2423 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2424 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2425 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2427 /* Also preserve various other info from the definition. */
2428 if (!new_is_definition)
2430 tree t;
2431 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2432 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2433 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2434 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2435 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2436 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2437 DECL_CONTEXT (t) = newdecl;
2439 /* See if we've got a function to instantiate from. */
2440 if (DECL_SAVED_TREE (olddecl))
2441 DECL_ABSTRACT_ORIGIN (newdecl)
2442 = DECL_ABSTRACT_ORIGIN (olddecl);
2446 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2448 /* Merge the USED information. */
2449 if (TREE_USED (olddecl))
2450 TREE_USED (newdecl) = 1;
2451 else if (TREE_USED (newdecl))
2452 TREE_USED (olddecl) = 1;
2453 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2454 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2455 if (DECL_PRESERVE_P (olddecl))
2456 DECL_PRESERVE_P (newdecl) = 1;
2457 else if (DECL_PRESERVE_P (newdecl))
2458 DECL_PRESERVE_P (olddecl) = 1;
2460 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2461 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2462 DECL_ARGUMENTS (if appropriate). */
2464 unsigned olddecl_uid = DECL_UID (olddecl);
2465 tree olddecl_context = DECL_CONTEXT (olddecl);
2466 tree olddecl_arguments = NULL;
2467 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2468 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2470 memcpy ((char *) olddecl + sizeof (struct tree_common),
2471 (char *) newdecl + sizeof (struct tree_common),
2472 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2473 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2474 switch (TREE_CODE (olddecl))
2476 case FUNCTION_DECL:
2477 case FIELD_DECL:
2478 case VAR_DECL:
2479 case PARM_DECL:
2480 case LABEL_DECL:
2481 case RESULT_DECL:
2482 case CONST_DECL:
2483 case TYPE_DECL:
2484 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2485 (char *) newdecl + sizeof (struct tree_decl_common),
2486 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2487 break;
2489 default:
2491 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2492 (char *) newdecl + sizeof (struct tree_decl_common),
2493 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2495 DECL_UID (olddecl) = olddecl_uid;
2496 DECL_CONTEXT (olddecl) = olddecl_context;
2497 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2498 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2501 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2502 so that encode_section_info has a chance to look at the new decl
2503 flags and attributes. */
2504 if (DECL_RTL_SET_P (olddecl)
2505 && (TREE_CODE (olddecl) == FUNCTION_DECL
2506 || (TREE_CODE (olddecl) == VAR_DECL
2507 && TREE_STATIC (olddecl))))
2508 make_decl_rtl (olddecl);
2510 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2511 and the definition is coming from the old version, cgraph needs
2512 to be called again. */
2513 if (extern_changed && !new_is_definition
2514 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2515 cgraph_mark_if_needed (olddecl);
2518 /* Handle when a new declaration NEWDECL has the same name as an old
2519 one OLDDECL in the same binding contour. Prints an error message
2520 if appropriate.
2522 If safely possible, alter OLDDECL to look like NEWDECL, and return
2523 true. Otherwise, return false. */
2525 static bool
2526 duplicate_decls (tree newdecl, tree olddecl)
2528 tree newtype = NULL, oldtype = NULL;
2530 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2532 /* Avoid `unused variable' and other warnings for OLDDECL. */
2533 TREE_NO_WARNING (olddecl) = 1;
2534 return false;
2537 merge_decls (newdecl, olddecl, newtype, oldtype);
2538 return true;
2542 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2543 static void
2544 warn_if_shadowing (tree new_decl)
2546 struct c_binding *b;
2548 /* Shadow warnings wanted? */
2549 if (!warn_shadow
2550 /* No shadow warnings for internally generated vars. */
2551 || DECL_IS_BUILTIN (new_decl)
2552 /* No shadow warnings for vars made for inlining. */
2553 || DECL_FROM_INLINE (new_decl))
2554 return;
2556 /* Is anything being shadowed? Invisible decls do not count. */
2557 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2558 if (b->decl && b->decl != new_decl && !b->invisible
2559 && (b->decl == error_mark_node
2560 || diagnostic_report_warnings_p (global_dc,
2561 DECL_SOURCE_LOCATION (b->decl))))
2563 tree old_decl = b->decl;
2565 if (old_decl == error_mark_node)
2567 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2568 "non-variable", new_decl);
2569 break;
2571 else if (TREE_CODE (old_decl) == PARM_DECL)
2572 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2573 new_decl);
2574 else if (DECL_FILE_SCOPE_P (old_decl))
2575 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2576 "declaration", new_decl);
2577 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2578 && DECL_BUILT_IN (old_decl))
2580 warning (OPT_Wshadow, "declaration of %q+D shadows "
2581 "a built-in function", new_decl);
2582 break;
2584 else
2585 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2586 new_decl);
2588 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2589 "shadowed declaration is here");
2591 break;
2595 /* Record a decl-node X as belonging to the current lexical scope.
2596 Check for errors (such as an incompatible declaration for the same
2597 name already seen in the same scope).
2599 Returns either X or an old decl for the same name.
2600 If an old decl is returned, it may have been smashed
2601 to agree with what X says. */
2603 tree
2604 pushdecl (tree x)
2606 tree name = DECL_NAME (x);
2607 struct c_scope *scope = current_scope;
2608 struct c_binding *b;
2609 bool nested = false;
2610 location_t locus = DECL_SOURCE_LOCATION (x);
2612 /* Must set DECL_CONTEXT for everything not at file scope or
2613 DECL_FILE_SCOPE_P won't work. Local externs don't count
2614 unless they have initializers (which generate code). */
2615 if (current_function_decl
2616 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2617 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2618 DECL_CONTEXT (x) = current_function_decl;
2620 /* Anonymous decls are just inserted in the scope. */
2621 if (!name)
2623 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2624 locus);
2625 return x;
2628 /* First, see if there is another declaration with the same name in
2629 the current scope. If there is, duplicate_decls may do all the
2630 work for us. If duplicate_decls returns false, that indicates
2631 two incompatible decls in the same scope; we are to silently
2632 replace the old one (duplicate_decls has issued all appropriate
2633 diagnostics). In particular, we should not consider possible
2634 duplicates in the external scope, or shadowing. */
2635 b = I_SYMBOL_BINDING (name);
2636 if (b && B_IN_SCOPE (b, scope))
2638 struct c_binding *b_ext, *b_use;
2639 tree type = TREE_TYPE (x);
2640 tree visdecl = b->decl;
2641 tree vistype = TREE_TYPE (visdecl);
2642 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2643 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2644 b->inner_comp = false;
2645 b_use = b;
2646 b_ext = b;
2647 /* If this is an external linkage declaration, we should check
2648 for compatibility with the type in the external scope before
2649 setting the type at this scope based on the visible
2650 information only. */
2651 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2653 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2654 b_ext = b_ext->shadowed;
2655 if (b_ext)
2657 b_use = b_ext;
2658 if (b_use->u.type)
2659 TREE_TYPE (b_use->decl) = b_use->u.type;
2662 if (duplicate_decls (x, b_use->decl))
2664 if (b_use != b)
2666 /* Save the updated type in the external scope and
2667 restore the proper type for this scope. */
2668 tree thistype;
2669 if (comptypes (vistype, type))
2670 thistype = composite_type (vistype, type);
2671 else
2672 thistype = TREE_TYPE (b_use->decl);
2673 b_use->u.type = TREE_TYPE (b_use->decl);
2674 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2675 && DECL_BUILT_IN (b_use->decl))
2676 thistype
2677 = build_type_attribute_variant (thistype,
2678 TYPE_ATTRIBUTES
2679 (b_use->u.type));
2680 TREE_TYPE (b_use->decl) = thistype;
2682 return b_use->decl;
2684 else
2685 goto skip_external_and_shadow_checks;
2688 /* All declarations with external linkage, and all external
2689 references, go in the external scope, no matter what scope is
2690 current. However, the binding in that scope is ignored for
2691 purposes of normal name lookup. A separate binding structure is
2692 created in the requested scope; this governs the normal
2693 visibility of the symbol.
2695 The binding in the externals scope is used exclusively for
2696 detecting duplicate declarations of the same object, no matter
2697 what scope they are in; this is what we do here. (C99 6.2.7p2:
2698 All declarations that refer to the same object or function shall
2699 have compatible type; otherwise, the behavior is undefined.) */
2700 if (DECL_EXTERNAL (x) || scope == file_scope)
2702 tree type = TREE_TYPE (x);
2703 tree vistype = 0;
2704 tree visdecl = 0;
2705 bool type_saved = false;
2706 if (b && !B_IN_EXTERNAL_SCOPE (b)
2707 && (TREE_CODE (b->decl) == FUNCTION_DECL
2708 || TREE_CODE (b->decl) == VAR_DECL)
2709 && DECL_FILE_SCOPE_P (b->decl))
2711 visdecl = b->decl;
2712 vistype = TREE_TYPE (visdecl);
2714 if (scope != file_scope
2715 && !DECL_IN_SYSTEM_HEADER (x))
2716 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2718 while (b && !B_IN_EXTERNAL_SCOPE (b))
2720 /* If this decl might be modified, save its type. This is
2721 done here rather than when the decl is first bound
2722 because the type may change after first binding, through
2723 being completed or through attributes being added. If we
2724 encounter multiple such decls, only the first should have
2725 its type saved; the others will already have had their
2726 proper types saved and the types will not have changed as
2727 their scopes will not have been re-entered. */
2728 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2730 b->u.type = TREE_TYPE (b->decl);
2731 type_saved = true;
2733 if (B_IN_FILE_SCOPE (b)
2734 && TREE_CODE (b->decl) == VAR_DECL
2735 && TREE_STATIC (b->decl)
2736 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2737 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2738 && TREE_CODE (type) == ARRAY_TYPE
2739 && TYPE_DOMAIN (type)
2740 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2741 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2743 /* Array type completed in inner scope, which should be
2744 diagnosed if the completion does not have size 1 and
2745 it does not get completed in the file scope. */
2746 b->inner_comp = true;
2748 b = b->shadowed;
2751 /* If a matching external declaration has been found, set its
2752 type to the composite of all the types of that declaration.
2753 After the consistency checks, it will be reset to the
2754 composite of the visible types only. */
2755 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2756 && b->u.type)
2757 TREE_TYPE (b->decl) = b->u.type;
2759 /* The point of the same_translation_unit_p check here is,
2760 we want to detect a duplicate decl for a construct like
2761 foo() { extern bar(); } ... static bar(); but not if
2762 they are in different translation units. In any case,
2763 the static does not go in the externals scope. */
2764 if (b
2765 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2766 && duplicate_decls (x, b->decl))
2768 tree thistype;
2769 if (vistype)
2771 if (comptypes (vistype, type))
2772 thistype = composite_type (vistype, type);
2773 else
2774 thistype = TREE_TYPE (b->decl);
2776 else
2777 thistype = type;
2778 b->u.type = TREE_TYPE (b->decl);
2779 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2780 thistype
2781 = build_type_attribute_variant (thistype,
2782 TYPE_ATTRIBUTES (b->u.type));
2783 TREE_TYPE (b->decl) = thistype;
2784 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2785 locus);
2786 return b->decl;
2788 else if (TREE_PUBLIC (x))
2790 if (visdecl && !b && duplicate_decls (x, visdecl))
2792 /* An external declaration at block scope referring to a
2793 visible entity with internal linkage. The composite
2794 type will already be correct for this scope, so we
2795 just need to fall through to make the declaration in
2796 this scope. */
2797 nested = true;
2798 x = visdecl;
2800 else
2802 bind (name, x, external_scope, /*invisible=*/true,
2803 /*nested=*/false, locus);
2804 nested = true;
2809 if (TREE_CODE (x) != PARM_DECL)
2810 warn_if_shadowing (x);
2812 skip_external_and_shadow_checks:
2813 if (TREE_CODE (x) == TYPE_DECL)
2815 /* So this is a typedef, set its underlying type. */
2816 set_underlying_type (x);
2818 /* If X is a typedef defined in the current function, record it
2819 for the purpose of implementing the -Wunused-local-typedefs
2820 warning. */
2821 record_locally_defined_typedef (x);
2824 bind (name, x, scope, /*invisible=*/false, nested, locus);
2826 /* If x's type is incomplete because it's based on a
2827 structure or union which has not yet been fully declared,
2828 attach it to that structure or union type, so we can go
2829 back and complete the variable declaration later, if the
2830 structure or union gets fully declared.
2832 If the input is erroneous, we can have error_mark in the type
2833 slot (e.g. "f(void a, ...)") - that doesn't count as an
2834 incomplete type. */
2835 if (TREE_TYPE (x) != error_mark_node
2836 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2838 tree element = TREE_TYPE (x);
2840 while (TREE_CODE (element) == ARRAY_TYPE)
2841 element = TREE_TYPE (element);
2842 element = TYPE_MAIN_VARIANT (element);
2844 if ((TREE_CODE (element) == RECORD_TYPE
2845 || TREE_CODE (element) == UNION_TYPE)
2846 && (TREE_CODE (x) != TYPE_DECL
2847 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2848 && !COMPLETE_TYPE_P (element))
2849 C_TYPE_INCOMPLETE_VARS (element)
2850 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2852 return x;
2855 /* Record X as belonging to file scope.
2856 This is used only internally by the Objective-C front end,
2857 and is limited to its needs. duplicate_decls is not called;
2858 if there is any preexisting decl for this identifier, it is an ICE. */
2860 tree
2861 pushdecl_top_level (tree x)
2863 tree name;
2864 bool nested = false;
2865 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2867 name = DECL_NAME (x);
2869 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2871 if (TREE_PUBLIC (x))
2873 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2874 UNKNOWN_LOCATION);
2875 nested = true;
2877 if (file_scope)
2878 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2880 return x;
2883 static void
2884 implicit_decl_warning (tree id, tree olddecl)
2886 if (warn_implicit_function_declaration)
2888 bool warned;
2890 if (flag_isoc99)
2891 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2892 "implicit declaration of function %qE", id);
2893 else
2894 warned = warning (OPT_Wimplicit_function_declaration,
2895 G_("implicit declaration of function %qE"), id);
2896 if (olddecl && warned)
2897 locate_old_decl (olddecl);
2901 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2902 function of type int (). */
2904 tree
2905 implicitly_declare (location_t loc, tree functionid)
2907 struct c_binding *b;
2908 tree decl = 0;
2909 tree asmspec_tree;
2911 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2913 if (B_IN_SCOPE (b, external_scope))
2915 decl = b->decl;
2916 break;
2920 if (decl)
2922 if (decl == error_mark_node)
2923 return decl;
2925 /* FIXME: Objective-C has weird not-really-builtin functions
2926 which are supposed to be visible automatically. They wind up
2927 in the external scope because they're pushed before the file
2928 scope gets created. Catch this here and rebind them into the
2929 file scope. */
2930 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2932 bind (functionid, decl, file_scope,
2933 /*invisible=*/false, /*nested=*/true,
2934 DECL_SOURCE_LOCATION (decl));
2935 return decl;
2937 else
2939 tree newtype = default_function_type;
2940 if (b->u.type)
2941 TREE_TYPE (decl) = b->u.type;
2942 /* Implicit declaration of a function already declared
2943 (somehow) in a different scope, or as a built-in.
2944 If this is the first time this has happened, warn;
2945 then recycle the old declaration but with the new type. */
2946 if (!C_DECL_IMPLICIT (decl))
2948 implicit_decl_warning (functionid, decl);
2949 C_DECL_IMPLICIT (decl) = 1;
2951 if (DECL_BUILT_IN (decl))
2953 newtype = build_type_attribute_variant (newtype,
2954 TYPE_ATTRIBUTES
2955 (TREE_TYPE (decl)));
2956 if (!comptypes (newtype, TREE_TYPE (decl)))
2958 warning_at (loc, 0, "incompatible implicit declaration of "
2959 "built-in function %qD", decl);
2960 newtype = TREE_TYPE (decl);
2963 else
2965 if (!comptypes (newtype, TREE_TYPE (decl)))
2967 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2968 locate_old_decl (decl);
2971 b->u.type = TREE_TYPE (decl);
2972 TREE_TYPE (decl) = newtype;
2973 bind (functionid, decl, current_scope,
2974 /*invisible=*/false, /*nested=*/true,
2975 DECL_SOURCE_LOCATION (decl));
2976 return decl;
2980 /* Not seen before. */
2981 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2982 DECL_EXTERNAL (decl) = 1;
2983 TREE_PUBLIC (decl) = 1;
2984 C_DECL_IMPLICIT (decl) = 1;
2985 implicit_decl_warning (functionid, 0);
2986 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2987 if (asmspec_tree)
2988 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2990 /* C89 says implicit declarations are in the innermost block.
2991 So we record the decl in the standard fashion. */
2992 decl = pushdecl (decl);
2994 /* No need to call objc_check_decl here - it's a function type. */
2995 rest_of_decl_compilation (decl, 0, 0);
2997 /* Write a record describing this implicit function declaration
2998 to the prototypes file (if requested). */
2999 gen_aux_info_record (decl, 0, 1, 0);
3001 /* Possibly apply some default attributes to this implicit declaration. */
3002 decl_attributes (&decl, NULL_TREE, 0);
3004 return decl;
3007 /* Issue an error message for a reference to an undeclared variable
3008 ID, including a reference to a builtin outside of function-call
3009 context. Establish a binding of the identifier to error_mark_node
3010 in an appropriate scope, which will suppress further errors for the
3011 same identifier. The error message should be given location LOC. */
3012 void
3013 undeclared_variable (location_t loc, tree id)
3015 static bool already = false;
3016 struct c_scope *scope;
3018 if (current_function_decl == 0)
3020 error_at (loc, "%qE undeclared here (not in a function)", id);
3021 scope = current_scope;
3023 else
3025 if (!objc_diagnose_private_ivar (id))
3026 error_at (loc, "%qE undeclared (first use in this function)", id);
3027 if (!already)
3029 inform (loc, "each undeclared identifier is reported only"
3030 " once for each function it appears in");
3031 already = true;
3034 /* If we are parsing old-style parameter decls, current_function_decl
3035 will be nonnull but current_function_scope will be null. */
3036 scope = current_function_scope ? current_function_scope : current_scope;
3038 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3039 UNKNOWN_LOCATION);
3042 /* Subroutine of lookup_label, declare_label, define_label: construct a
3043 LABEL_DECL with all the proper frills. Also create a struct
3044 c_label_vars initialized for the current scope. */
3046 static tree
3047 make_label (location_t location, tree name, bool defining,
3048 struct c_label_vars **p_label_vars)
3050 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3051 struct c_label_vars *label_vars;
3053 DECL_CONTEXT (label) = current_function_decl;
3054 DECL_MODE (label) = VOIDmode;
3056 label_vars = ggc_alloc_c_label_vars ();
3057 label_vars->shadowed = NULL;
3058 set_spot_bindings (&label_vars->label_bindings, defining);
3059 label_vars->decls_in_scope = make_tree_vector ();
3060 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3061 *p_label_vars = label_vars;
3063 return label;
3066 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3067 Create one if none exists so far for the current function.
3068 This is called when a label is used in a goto expression or
3069 has its address taken. */
3071 tree
3072 lookup_label (tree name)
3074 tree label;
3075 struct c_label_vars *label_vars;
3077 if (current_function_scope == 0)
3079 error ("label %qE referenced outside of any function", name);
3080 return 0;
3083 /* Use a label already defined or ref'd with this name, but not if
3084 it is inherited from a containing function and wasn't declared
3085 using __label__. */
3086 label = I_LABEL_DECL (name);
3087 if (label && (DECL_CONTEXT (label) == current_function_decl
3088 || C_DECLARED_LABEL_FLAG (label)))
3090 /* If the label has only been declared, update its apparent
3091 location to point here, for better diagnostics if it
3092 turns out not to have been defined. */
3093 if (DECL_INITIAL (label) == NULL_TREE)
3094 DECL_SOURCE_LOCATION (label) = input_location;
3095 return label;
3098 /* No label binding for that identifier; make one. */
3099 label = make_label (input_location, name, false, &label_vars);
3101 /* Ordinary labels go in the current function scope. */
3102 bind_label (name, label, current_function_scope, label_vars);
3104 return label;
3107 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3108 to LABEL. */
3110 static void
3111 warn_about_goto (location_t goto_loc, tree label, tree decl)
3113 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3114 error_at (goto_loc,
3115 "jump into scope of identifier with variably modified type");
3116 else
3117 warning_at (goto_loc, OPT_Wjump_misses_init,
3118 "jump skips variable initialization");
3119 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3120 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3123 /* Look up a label because of a goto statement. This is like
3124 lookup_label, but also issues any appropriate warnings. */
3126 tree
3127 lookup_label_for_goto (location_t loc, tree name)
3129 tree label;
3130 struct c_label_vars *label_vars;
3131 unsigned int ix;
3132 tree decl;
3134 label = lookup_label (name);
3135 if (label == NULL_TREE)
3136 return NULL_TREE;
3138 /* If we are jumping to a different function, we can't issue any
3139 useful warnings. */
3140 if (DECL_CONTEXT (label) != current_function_decl)
3142 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3143 return label;
3146 label_vars = I_LABEL_BINDING (name)->u.label;
3148 /* If the label has not yet been defined, then push this goto on a
3149 list for possible later warnings. */
3150 if (label_vars->label_bindings.scope == NULL)
3152 struct c_goto_bindings *g;
3154 g = ggc_alloc_c_goto_bindings ();
3155 g->loc = loc;
3156 set_spot_bindings (&g->goto_bindings, true);
3157 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3158 return label;
3161 /* If there are any decls in label_vars->decls_in_scope, then this
3162 goto has missed the declaration of the decl. This happens for a
3163 case like
3164 int i = 1;
3165 lab:
3167 goto lab;
3168 Issue a warning or error. */
3169 FOR_EACH_VEC_ELT (tree, label_vars->decls_in_scope, ix, decl)
3170 warn_about_goto (loc, label, decl);
3172 if (label_vars->label_bindings.left_stmt_expr)
3174 error_at (loc, "jump into statement expression");
3175 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3178 return label;
3181 /* Make a label named NAME in the current function, shadowing silently
3182 any that may be inherited from containing functions or containing
3183 scopes. This is called for __label__ declarations. */
3185 tree
3186 declare_label (tree name)
3188 struct c_binding *b = I_LABEL_BINDING (name);
3189 tree label;
3190 struct c_label_vars *label_vars;
3192 /* Check to make sure that the label hasn't already been declared
3193 at this scope */
3194 if (b && B_IN_CURRENT_SCOPE (b))
3196 error ("duplicate label declaration %qE", name);
3197 locate_old_decl (b->decl);
3199 /* Just use the previous declaration. */
3200 return b->decl;
3203 label = make_label (input_location, name, false, &label_vars);
3204 C_DECLARED_LABEL_FLAG (label) = 1;
3206 /* Declared labels go in the current scope. */
3207 bind_label (name, label, current_scope, label_vars);
3209 return label;
3212 /* When we define a label, issue any appropriate warnings if there are
3213 any gotos earlier in the function which jump to this label. */
3215 static void
3216 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3218 unsigned int ix;
3219 struct c_goto_bindings *g;
3221 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
3223 struct c_binding *b;
3224 struct c_scope *scope;
3226 /* We have a goto to this label. The goto is going forward. In
3227 g->scope, the goto is going to skip any binding which was
3228 defined after g->bindings_in_scope. */
3229 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3231 for (b = g->goto_bindings.scope->bindings;
3232 b != g->goto_bindings.bindings_in_scope;
3233 b = b->prev)
3235 if (decl_jump_unsafe (b->decl))
3236 warn_about_goto (g->loc, label, b->decl);
3240 /* We also need to warn about decls defined in any scopes
3241 between the scope of the label and the scope of the goto. */
3242 for (scope = label_vars->label_bindings.scope;
3243 scope != g->goto_bindings.scope;
3244 scope = scope->outer)
3246 gcc_assert (scope != NULL);
3247 if (scope->has_jump_unsafe_decl)
3249 if (scope == label_vars->label_bindings.scope)
3250 b = label_vars->label_bindings.bindings_in_scope;
3251 else
3252 b = scope->bindings;
3253 for (; b != NULL; b = b->prev)
3255 if (decl_jump_unsafe (b->decl))
3256 warn_about_goto (g->loc, label, b->decl);
3261 if (g->goto_bindings.stmt_exprs > 0)
3263 error_at (g->loc, "jump into statement expression");
3264 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3265 label);
3269 /* Now that the label is defined, we will issue warnings about
3270 subsequent gotos to this label when we see them. */
3271 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3272 label_vars->gotos = NULL;
3275 /* Define a label, specifying the location in the source file.
3276 Return the LABEL_DECL node for the label, if the definition is valid.
3277 Otherwise return 0. */
3279 tree
3280 define_label (location_t location, tree name)
3282 /* Find any preexisting label with this name. It is an error
3283 if that label has already been defined in this function, or
3284 if there is a containing function with a declared label with
3285 the same name. */
3286 tree label = I_LABEL_DECL (name);
3288 if (label
3289 && ((DECL_CONTEXT (label) == current_function_decl
3290 && DECL_INITIAL (label) != 0)
3291 || (DECL_CONTEXT (label) != current_function_decl
3292 && C_DECLARED_LABEL_FLAG (label))))
3294 error_at (location, "duplicate label %qD", label);
3295 locate_old_decl (label);
3296 return 0;
3298 else if (label && DECL_CONTEXT (label) == current_function_decl)
3300 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3302 /* The label has been used or declared already in this function,
3303 but not defined. Update its location to point to this
3304 definition. */
3305 DECL_SOURCE_LOCATION (label) = location;
3306 set_spot_bindings (&label_vars->label_bindings, true);
3308 /* Issue warnings as required about any goto statements from
3309 earlier in the function. */
3310 check_earlier_gotos (label, label_vars);
3312 else
3314 struct c_label_vars *label_vars;
3316 /* No label binding for that identifier; make one. */
3317 label = make_label (location, name, true, &label_vars);
3319 /* Ordinary labels go in the current function scope. */
3320 bind_label (name, label, current_function_scope, label_vars);
3323 if (!in_system_header && lookup_name (name))
3324 warning_at (location, OPT_Wtraditional,
3325 "traditional C lacks a separate namespace "
3326 "for labels, identifier %qE conflicts", name);
3328 /* Mark label as having been defined. */
3329 DECL_INITIAL (label) = error_mark_node;
3330 return label;
3333 /* Get the bindings for a new switch statement. This is used to issue
3334 warnings as appropriate for jumps from the switch to case or
3335 default labels. */
3337 struct c_spot_bindings *
3338 c_get_switch_bindings (void)
3340 struct c_spot_bindings *switch_bindings;
3342 switch_bindings = XNEW (struct c_spot_bindings);
3343 set_spot_bindings (switch_bindings, true);
3344 return switch_bindings;
3347 void
3348 c_release_switch_bindings (struct c_spot_bindings *bindings)
3350 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3351 XDELETE (bindings);
3354 /* This is called at the point of a case or default label to issue
3355 warnings about decls as needed. It returns true if it found an
3356 error, not just a warning. */
3358 bool
3359 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3360 location_t switch_loc, location_t case_loc)
3362 bool saw_error;
3363 struct c_scope *scope;
3365 saw_error = false;
3366 for (scope = current_scope;
3367 scope != switch_bindings->scope;
3368 scope = scope->outer)
3370 struct c_binding *b;
3372 gcc_assert (scope != NULL);
3374 if (!scope->has_jump_unsafe_decl)
3375 continue;
3377 for (b = scope->bindings; b != NULL; b = b->prev)
3379 if (decl_jump_unsafe (b->decl))
3381 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3383 saw_error = true;
3384 error_at (case_loc,
3385 ("switch jumps into scope of identifier with "
3386 "variably modified type"));
3388 else
3389 warning_at (case_loc, OPT_Wjump_misses_init,
3390 "switch jumps over variable initialization");
3391 inform (switch_loc, "switch starts here");
3392 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3393 b->decl);
3398 if (switch_bindings->stmt_exprs > 0)
3400 saw_error = true;
3401 error_at (case_loc, "switch jumps into statement expression");
3402 inform (switch_loc, "switch starts here");
3405 return saw_error;
3408 /* Given NAME, an IDENTIFIER_NODE,
3409 return the structure (or union or enum) definition for that name.
3410 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3411 CODE says which kind of type the caller wants;
3412 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3413 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3414 location where the tag was defined.
3415 If the wrong kind of type is found, an error is reported. */
3417 static tree
3418 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3419 location_t *ploc)
3421 struct c_binding *b = I_TAG_BINDING (name);
3422 int thislevel = 0;
3424 if (!b || !b->decl)
3425 return 0;
3427 /* We only care about whether it's in this level if
3428 thislevel_only was set or it might be a type clash. */
3429 if (thislevel_only || TREE_CODE (b->decl) != code)
3431 /* For our purposes, a tag in the external scope is the same as
3432 a tag in the file scope. (Primarily relevant to Objective-C
3433 and its builtin structure tags, which get pushed before the
3434 file scope is created.) */
3435 if (B_IN_CURRENT_SCOPE (b)
3436 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3437 thislevel = 1;
3440 if (thislevel_only && !thislevel)
3441 return 0;
3443 if (TREE_CODE (b->decl) != code)
3445 /* Definition isn't the kind we were looking for. */
3446 pending_invalid_xref = name;
3447 pending_invalid_xref_location = input_location;
3449 /* If in the same binding level as a declaration as a tag
3450 of a different type, this must not be allowed to
3451 shadow that tag, so give the error immediately.
3452 (For example, "struct foo; union foo;" is invalid.) */
3453 if (thislevel)
3454 pending_xref_error ();
3457 if (ploc != NULL)
3458 *ploc = b->locus;
3460 return b->decl;
3463 /* Print an error message now
3464 for a recent invalid struct, union or enum cross reference.
3465 We don't print them immediately because they are not invalid
3466 when used in the `struct foo;' construct for shadowing. */
3468 void
3469 pending_xref_error (void)
3471 if (pending_invalid_xref != 0)
3472 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3473 pending_invalid_xref);
3474 pending_invalid_xref = 0;
3478 /* Look up NAME in the current scope and its superiors
3479 in the namespace of variables, functions and typedefs.
3480 Return a ..._DECL node of some kind representing its definition,
3481 or return 0 if it is undefined. */
3483 tree
3484 lookup_name (tree name)
3486 struct c_binding *b = I_SYMBOL_BINDING (name);
3487 if (b && !b->invisible)
3489 maybe_record_typedef_use (b->decl);
3490 return b->decl;
3492 return 0;
3495 /* Similar to `lookup_name' but look only at the indicated scope. */
3497 static tree
3498 lookup_name_in_scope (tree name, struct c_scope *scope)
3500 struct c_binding *b;
3502 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3503 if (B_IN_SCOPE (b, scope))
3504 return b->decl;
3505 return 0;
3508 /* Create the predefined scalar types of C,
3509 and some nodes representing standard constants (0, 1, (void *) 0).
3510 Initialize the global scope.
3511 Make definitions for built-in primitive functions. */
3513 void
3514 c_init_decl_processing (void)
3516 location_t save_loc = input_location;
3518 /* Initialize reserved words for parser. */
3519 c_parse_init ();
3521 current_function_decl = 0;
3523 gcc_obstack_init (&parser_obstack);
3525 /* Make the externals scope. */
3526 push_scope ();
3527 external_scope = current_scope;
3529 /* Declarations from c_common_nodes_and_builtins must not be associated
3530 with this input file, lest we get differences between using and not
3531 using preprocessed headers. */
3532 input_location = BUILTINS_LOCATION;
3534 c_common_nodes_and_builtins ();
3536 /* In C, comparisons and TRUTH_* expressions have type int. */
3537 truthvalue_type_node = integer_type_node;
3538 truthvalue_true_node = integer_one_node;
3539 truthvalue_false_node = integer_zero_node;
3541 /* Even in C99, which has a real boolean type. */
3542 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3543 boolean_type_node));
3545 input_location = save_loc;
3547 pedantic_lvalues = true;
3549 make_fname_decl = c_make_fname_decl;
3550 start_fname_decls ();
3553 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3554 give the decl, NAME is the initialization string and TYPE_DEP
3555 indicates whether NAME depended on the type of the function. As we
3556 don't yet implement delayed emission of static data, we mark the
3557 decl as emitted so it is not placed in the output. Anything using
3558 it must therefore pull out the STRING_CST initializer directly.
3559 FIXME. */
3561 static tree
3562 c_make_fname_decl (location_t loc, tree id, int type_dep)
3564 const char *name = fname_as_string (type_dep);
3565 tree decl, type, init;
3566 size_t length = strlen (name);
3568 type = build_array_type (char_type_node,
3569 build_index_type (size_int (length)));
3570 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3572 decl = build_decl (loc, VAR_DECL, id, type);
3574 TREE_STATIC (decl) = 1;
3575 TREE_READONLY (decl) = 1;
3576 DECL_ARTIFICIAL (decl) = 1;
3578 init = build_string (length + 1, name);
3579 free (CONST_CAST (char *, name));
3580 TREE_TYPE (init) = type;
3581 DECL_INITIAL (decl) = init;
3583 TREE_USED (decl) = 1;
3585 if (current_function_decl
3586 /* For invalid programs like this:
3588 void foo()
3589 const char* p = __FUNCTION__;
3591 the __FUNCTION__ is believed to appear in K&R style function
3592 parameter declarator. In that case we still don't have
3593 function_scope. */
3594 && (!seen_error () || current_function_scope))
3596 DECL_CONTEXT (decl) = current_function_decl;
3597 bind (id, decl, current_function_scope,
3598 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3601 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3603 return decl;
3606 tree
3607 c_builtin_function (tree decl)
3609 tree type = TREE_TYPE (decl);
3610 tree id = DECL_NAME (decl);
3612 const char *name = IDENTIFIER_POINTER (id);
3613 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3615 /* Should never be called on a symbol with a preexisting meaning. */
3616 gcc_assert (!I_SYMBOL_BINDING (id));
3618 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3619 UNKNOWN_LOCATION);
3621 /* Builtins in the implementation namespace are made visible without
3622 needing to be explicitly declared. See push_file_scope. */
3623 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3625 DECL_CHAIN (decl) = visible_builtins;
3626 visible_builtins = decl;
3629 return decl;
3632 tree
3633 c_builtin_function_ext_scope (tree decl)
3635 tree type = TREE_TYPE (decl);
3636 tree id = DECL_NAME (decl);
3638 const char *name = IDENTIFIER_POINTER (id);
3639 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3641 /* Should never be called on a symbol with a preexisting meaning. */
3642 gcc_assert (!I_SYMBOL_BINDING (id));
3644 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3645 UNKNOWN_LOCATION);
3647 /* Builtins in the implementation namespace are made visible without
3648 needing to be explicitly declared. See push_file_scope. */
3649 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3651 DECL_CHAIN (decl) = visible_builtins;
3652 visible_builtins = decl;
3655 return decl;
3658 /* Called when a declaration is seen that contains no names to declare.
3659 If its type is a reference to a structure, union or enum inherited
3660 from a containing scope, shadow that tag name for the current scope
3661 with a forward reference.
3662 If its type defines a new named structure or union
3663 or defines an enum, it is valid but we need not do anything here.
3664 Otherwise, it is an error. */
3666 void
3667 shadow_tag (const struct c_declspecs *declspecs)
3669 shadow_tag_warned (declspecs, 0);
3672 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3673 but no pedwarn. */
3674 void
3675 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3677 bool found_tag = false;
3679 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3681 tree value = declspecs->type;
3682 enum tree_code code = TREE_CODE (value);
3684 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3685 /* Used to test also that TYPE_SIZE (value) != 0.
3686 That caused warning for `struct foo;' at top level in the file. */
3688 tree name = TYPE_NAME (value);
3689 tree t;
3691 found_tag = true;
3693 if (declspecs->restrict_p)
3695 error ("invalid use of %<restrict%>");
3696 warned = 1;
3699 if (name == 0)
3701 if (warned != 1 && code != ENUMERAL_TYPE)
3702 /* Empty unnamed enum OK */
3704 pedwarn (input_location, 0,
3705 "unnamed struct/union that defines no instances");
3706 warned = 1;
3709 else if (declspecs->typespec_kind != ctsk_tagdef
3710 && declspecs->typespec_kind != ctsk_tagfirstref
3711 && declspecs->storage_class != csc_none)
3713 if (warned != 1)
3714 pedwarn (input_location, 0,
3715 "empty declaration with storage class specifier "
3716 "does not redeclare tag");
3717 warned = 1;
3718 pending_xref_error ();
3720 else if (declspecs->typespec_kind != ctsk_tagdef
3721 && declspecs->typespec_kind != ctsk_tagfirstref
3722 && (declspecs->const_p
3723 || declspecs->volatile_p
3724 || declspecs->restrict_p
3725 || declspecs->address_space))
3727 if (warned != 1)
3728 pedwarn (input_location, 0,
3729 "empty declaration with type qualifier "
3730 "does not redeclare tag");
3731 warned = 1;
3732 pending_xref_error ();
3734 else if (declspecs->typespec_kind != ctsk_tagdef
3735 && declspecs->typespec_kind != ctsk_tagfirstref
3736 && declspecs->alignas_p)
3738 if (warned != 1)
3739 pedwarn (input_location, 0,
3740 "empty declaration with %<_Alignas%> "
3741 "does not redeclare tag");
3742 warned = 1;
3743 pending_xref_error ();
3745 else
3747 pending_invalid_xref = 0;
3748 t = lookup_tag (code, name, 1, NULL);
3750 if (t == 0)
3752 t = make_node (code);
3753 pushtag (input_location, name, t);
3757 else
3759 if (warned != 1 && !in_system_header)
3761 pedwarn (input_location, 0,
3762 "useless type name in empty declaration");
3763 warned = 1;
3767 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3769 pedwarn (input_location, 0, "useless type name in empty declaration");
3770 warned = 1;
3773 pending_invalid_xref = 0;
3775 if (declspecs->inline_p)
3777 error ("%<inline%> in empty declaration");
3778 warned = 1;
3781 if (declspecs->noreturn_p)
3783 error ("%<_Noreturn%> in empty declaration");
3784 warned = 1;
3787 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3789 error ("%<auto%> in file-scope empty declaration");
3790 warned = 1;
3793 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3795 error ("%<register%> in file-scope empty declaration");
3796 warned = 1;
3799 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3801 warning (0, "useless storage class specifier in empty declaration");
3802 warned = 2;
3805 if (!warned && !in_system_header && declspecs->thread_p)
3807 warning (0, "useless %<__thread%> in empty declaration");
3808 warned = 2;
3811 if (!warned && !in_system_header && (declspecs->const_p
3812 || declspecs->volatile_p
3813 || declspecs->restrict_p
3814 || declspecs->address_space))
3816 warning (0, "useless type qualifier in empty declaration");
3817 warned = 2;
3820 if (!warned && !in_system_header && declspecs->alignas_p)
3822 warning (0, "useless %<_Alignas%> in empty declaration");
3823 warned = 2;
3826 if (warned != 1)
3828 if (!found_tag)
3829 pedwarn (input_location, 0, "empty declaration");
3834 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3835 bits. SPECS represents declaration specifiers that the grammar
3836 only permits to contain type qualifiers and attributes. */
3839 quals_from_declspecs (const struct c_declspecs *specs)
3841 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3842 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3843 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3844 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3845 gcc_assert (!specs->type
3846 && !specs->decl_attr
3847 && specs->typespec_word == cts_none
3848 && specs->storage_class == csc_none
3849 && !specs->typedef_p
3850 && !specs->explicit_signed_p
3851 && !specs->deprecated_p
3852 && !specs->long_p
3853 && !specs->long_long_p
3854 && !specs->short_p
3855 && !specs->signed_p
3856 && !specs->unsigned_p
3857 && !specs->complex_p
3858 && !specs->inline_p
3859 && !specs->noreturn_p
3860 && !specs->thread_p);
3861 return quals;
3864 /* Construct an array declarator. LOC is the location of the
3865 beginning of the array (usually the opening brace). EXPR is the
3866 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3867 inside the [] (to be applied to the pointer to which a parameter
3868 array is converted). STATIC_P is true if "static" is inside the
3869 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3870 VLA of unspecified length which is nevertheless a complete type,
3871 false otherwise. The field for the contained declarator is left to
3872 be filled in by set_array_declarator_inner. */
3874 struct c_declarator *
3875 build_array_declarator (location_t loc,
3876 tree expr, struct c_declspecs *quals, bool static_p,
3877 bool vla_unspec_p)
3879 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3880 struct c_declarator);
3881 declarator->id_loc = loc;
3882 declarator->kind = cdk_array;
3883 declarator->declarator = 0;
3884 declarator->u.array.dimen = expr;
3885 if (quals)
3887 declarator->u.array.attrs = quals->attrs;
3888 declarator->u.array.quals = quals_from_declspecs (quals);
3890 else
3892 declarator->u.array.attrs = NULL_TREE;
3893 declarator->u.array.quals = 0;
3895 declarator->u.array.static_p = static_p;
3896 declarator->u.array.vla_unspec_p = vla_unspec_p;
3897 if (!flag_isoc99)
3899 if (static_p || quals != NULL)
3900 pedwarn (loc, OPT_pedantic,
3901 "ISO C90 does not support %<static%> or type "
3902 "qualifiers in parameter array declarators");
3903 if (vla_unspec_p)
3904 pedwarn (loc, OPT_pedantic,
3905 "ISO C90 does not support %<[*]%> array declarators");
3907 if (vla_unspec_p)
3909 if (!current_scope->parm_flag)
3911 /* C99 6.7.5.2p4 */
3912 error_at (loc, "%<[*]%> not allowed in other than "
3913 "function prototype scope");
3914 declarator->u.array.vla_unspec_p = false;
3915 return NULL;
3917 current_scope->had_vla_unspec = true;
3919 return declarator;
3922 /* Set the contained declarator of an array declarator. DECL is the
3923 declarator, as constructed by build_array_declarator; INNER is what
3924 appears on the left of the []. */
3926 struct c_declarator *
3927 set_array_declarator_inner (struct c_declarator *decl,
3928 struct c_declarator *inner)
3930 decl->declarator = inner;
3931 return decl;
3934 /* INIT is a constructor that forms DECL's initializer. If the final
3935 element initializes a flexible array field, add the size of that
3936 initializer to DECL's size. */
3938 static void
3939 add_flexible_array_elts_to_size (tree decl, tree init)
3941 tree elt, type;
3943 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3944 return;
3946 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3947 type = TREE_TYPE (elt);
3948 if (TREE_CODE (type) == ARRAY_TYPE
3949 && TYPE_SIZE (type) == NULL_TREE
3950 && TYPE_DOMAIN (type) != NULL_TREE
3951 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3953 complete_array_type (&type, elt, false);
3954 DECL_SIZE (decl)
3955 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3956 DECL_SIZE_UNIT (decl)
3957 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3961 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3962 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3963 before the type name, and set *EXPR_CONST_OPERANDS, if
3964 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3965 appear in a constant expression. */
3967 tree
3968 groktypename (struct c_type_name *type_name, tree *expr,
3969 bool *expr_const_operands)
3971 tree type;
3972 tree attrs = type_name->specs->attrs;
3974 type_name->specs->attrs = NULL_TREE;
3976 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3977 false, NULL, &attrs, expr, expr_const_operands,
3978 DEPRECATED_NORMAL);
3980 /* Apply attributes. */
3981 decl_attributes (&type, attrs, 0);
3983 return type;
3986 /* Decode a declarator in an ordinary declaration or data definition.
3987 This is called as soon as the type information and variable name
3988 have been parsed, before parsing the initializer if any.
3989 Here we create the ..._DECL node, fill in its type,
3990 and put it on the list of decls for the current context.
3991 The ..._DECL node is returned as the value.
3993 Exception: for arrays where the length is not specified,
3994 the type is left null, to be filled in by `finish_decl'.
3996 Function definitions do not come here; they go to start_function
3997 instead. However, external and forward declarations of functions
3998 do go through here. Structure field declarations are done by
3999 grokfield and not through here. */
4001 tree
4002 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4003 bool initialized, tree attributes)
4005 tree decl;
4006 tree tem;
4007 tree expr = NULL_TREE;
4008 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4010 /* An object declared as __attribute__((deprecated)) suppresses
4011 warnings of uses of other deprecated items. */
4012 if (lookup_attribute ("deprecated", attributes))
4013 deprecated_state = DEPRECATED_SUPPRESS;
4015 decl = grokdeclarator (declarator, declspecs,
4016 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4017 deprecated_state);
4018 if (!decl)
4019 return 0;
4021 if (expr)
4022 add_stmt (fold_convert (void_type_node, expr));
4024 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4025 warning (OPT_Wmain, "%q+D is usually a function", decl);
4027 if (initialized)
4028 /* Is it valid for this decl to have an initializer at all?
4029 If not, set INITIALIZED to zero, which will indirectly
4030 tell 'finish_decl' to ignore the initializer once it is parsed. */
4031 switch (TREE_CODE (decl))
4033 case TYPE_DECL:
4034 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4035 initialized = 0;
4036 break;
4038 case FUNCTION_DECL:
4039 error ("function %qD is initialized like a variable", decl);
4040 initialized = 0;
4041 break;
4043 case PARM_DECL:
4044 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4045 error ("parameter %qD is initialized", decl);
4046 initialized = 0;
4047 break;
4049 default:
4050 /* Don't allow initializations for incomplete types except for
4051 arrays which might be completed by the initialization. */
4053 /* This can happen if the array size is an undefined macro.
4054 We already gave a warning, so we don't need another one. */
4055 if (TREE_TYPE (decl) == error_mark_node)
4056 initialized = 0;
4057 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4059 /* A complete type is ok if size is fixed. */
4061 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4062 || C_DECL_VARIABLE_SIZE (decl))
4064 error ("variable-sized object may not be initialized");
4065 initialized = 0;
4068 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4070 error ("variable %qD has initializer but incomplete type", decl);
4071 initialized = 0;
4073 else if (C_DECL_VARIABLE_SIZE (decl))
4075 /* Although C99 is unclear about whether incomplete arrays
4076 of VLAs themselves count as VLAs, it does not make
4077 sense to permit them to be initialized given that
4078 ordinary VLAs may not be initialized. */
4079 error ("variable-sized object may not be initialized");
4080 initialized = 0;
4084 if (initialized)
4086 if (current_scope == file_scope)
4087 TREE_STATIC (decl) = 1;
4089 /* Tell 'pushdecl' this is an initialized decl
4090 even though we don't yet have the initializer expression.
4091 Also tell 'finish_decl' it may store the real initializer. */
4092 DECL_INITIAL (decl) = error_mark_node;
4095 /* If this is a function declaration, write a record describing it to the
4096 prototypes file (if requested). */
4098 if (TREE_CODE (decl) == FUNCTION_DECL)
4099 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4101 /* ANSI specifies that a tentative definition which is not merged with
4102 a non-tentative definition behaves exactly like a definition with an
4103 initializer equal to zero. (Section 3.7.2)
4105 -fno-common gives strict ANSI behavior, though this tends to break
4106 a large body of code that grew up without this rule.
4108 Thread-local variables are never common, since there's no entrenched
4109 body of code to break, and it allows more efficient variable references
4110 in the presence of dynamic linking. */
4112 if (TREE_CODE (decl) == VAR_DECL
4113 && !initialized
4114 && TREE_PUBLIC (decl)
4115 && !DECL_THREAD_LOCAL_P (decl)
4116 && !flag_no_common)
4117 DECL_COMMON (decl) = 1;
4119 /* Set attributes here so if duplicate decl, will have proper attributes. */
4120 decl_attributes (&decl, attributes, 0);
4122 /* Handle gnu_inline attribute. */
4123 if (declspecs->inline_p
4124 && !flag_gnu89_inline
4125 && TREE_CODE (decl) == FUNCTION_DECL
4126 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4127 || current_function_decl))
4129 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4131 else if (declspecs->storage_class != csc_static)
4132 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4135 if (TREE_CODE (decl) == FUNCTION_DECL
4136 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4138 struct c_declarator *ce = declarator;
4140 if (ce->kind == cdk_pointer)
4141 ce = declarator->declarator;
4142 if (ce->kind == cdk_function)
4144 tree args = ce->u.arg_info->parms;
4145 for (; args; args = DECL_CHAIN (args))
4147 tree type = TREE_TYPE (args);
4148 if (type && INTEGRAL_TYPE_P (type)
4149 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4150 DECL_ARG_TYPE (args) = integer_type_node;
4155 if (TREE_CODE (decl) == FUNCTION_DECL
4156 && DECL_DECLARED_INLINE_P (decl)
4157 && DECL_UNINLINABLE (decl)
4158 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4159 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4160 decl);
4162 /* C99 6.7.4p3: An inline definition of a function with external
4163 linkage shall not contain a definition of a modifiable object
4164 with static storage duration... */
4165 if (TREE_CODE (decl) == VAR_DECL
4166 && current_scope != file_scope
4167 && TREE_STATIC (decl)
4168 && !TREE_READONLY (decl)
4169 && DECL_DECLARED_INLINE_P (current_function_decl)
4170 && DECL_EXTERNAL (current_function_decl))
4171 record_inline_static (input_location, current_function_decl,
4172 decl, csi_modifiable);
4174 if (c_dialect_objc ()
4175 && (TREE_CODE (decl) == VAR_DECL
4176 || TREE_CODE (decl) == FUNCTION_DECL))
4177 objc_check_global_decl (decl);
4179 /* Add this decl to the current scope.
4180 TEM may equal DECL or it may be a previous decl of the same name. */
4181 tem = pushdecl (decl);
4183 if (initialized && DECL_EXTERNAL (tem))
4185 DECL_EXTERNAL (tem) = 0;
4186 TREE_STATIC (tem) = 1;
4189 return tem;
4192 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4193 DECL or the non-array element type if DECL is an uninitialized array.
4194 If that type has a const member, diagnose this. */
4196 static void
4197 diagnose_uninitialized_cst_member (tree decl, tree type)
4199 tree field;
4200 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4202 tree field_type;
4203 if (TREE_CODE (field) != FIELD_DECL)
4204 continue;
4205 field_type = strip_array_types (TREE_TYPE (field));
4207 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4209 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4210 "uninitialized const member in %qT is invalid in C++",
4211 strip_array_types (TREE_TYPE (decl)));
4212 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4215 if (TREE_CODE (field_type) == RECORD_TYPE
4216 || TREE_CODE (field_type) == UNION_TYPE)
4217 diagnose_uninitialized_cst_member (decl, field_type);
4221 /* Finish processing of a declaration;
4222 install its initial value.
4223 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4224 If the length of an array type is not known before,
4225 it must be determined now, from the initial value, or it is an error.
4227 INIT_LOC is the location of the initial value. */
4229 void
4230 finish_decl (tree decl, location_t init_loc, tree init,
4231 tree origtype, tree asmspec_tree)
4233 tree type;
4234 bool was_incomplete = (DECL_SIZE (decl) == 0);
4235 const char *asmspec = 0;
4237 /* If a name was specified, get the string. */
4238 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4239 && DECL_FILE_SCOPE_P (decl))
4240 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4241 if (asmspec_tree)
4242 asmspec = TREE_STRING_POINTER (asmspec_tree);
4244 if (TREE_CODE (decl) == VAR_DECL
4245 && TREE_STATIC (decl)
4246 && global_bindings_p ())
4247 /* So decl is a global variable. Record the types it uses
4248 so that we can decide later to emit debug info for them. */
4249 record_types_used_by_current_var_decl (decl);
4251 /* If `start_decl' didn't like having an initialization, ignore it now. */
4252 if (init != 0 && DECL_INITIAL (decl) == 0)
4253 init = 0;
4255 /* Don't crash if parm is initialized. */
4256 if (TREE_CODE (decl) == PARM_DECL)
4257 init = 0;
4259 if (init)
4260 store_init_value (init_loc, decl, init, origtype);
4262 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4263 || TREE_CODE (decl) == FUNCTION_DECL
4264 || TREE_CODE (decl) == FIELD_DECL))
4265 objc_check_decl (decl);
4267 type = TREE_TYPE (decl);
4269 /* Deduce size of array from initialization, if not already known. */
4270 if (TREE_CODE (type) == ARRAY_TYPE
4271 && TYPE_DOMAIN (type) == 0
4272 && TREE_CODE (decl) != TYPE_DECL)
4274 bool do_default
4275 = (TREE_STATIC (decl)
4276 /* Even if pedantic, an external linkage array
4277 may have incomplete type at first. */
4278 ? pedantic && !TREE_PUBLIC (decl)
4279 : !DECL_EXTERNAL (decl));
4280 int failure
4281 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4282 do_default);
4284 /* Get the completed type made by complete_array_type. */
4285 type = TREE_TYPE (decl);
4287 switch (failure)
4289 case 1:
4290 error ("initializer fails to determine size of %q+D", decl);
4291 break;
4293 case 2:
4294 if (do_default)
4295 error ("array size missing in %q+D", decl);
4296 /* If a `static' var's size isn't known,
4297 make it extern as well as static, so it does not get
4298 allocated.
4299 If it is not `static', then do not mark extern;
4300 finish_incomplete_decl will give it a default size
4301 and it will get allocated. */
4302 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4303 DECL_EXTERNAL (decl) = 1;
4304 break;
4306 case 3:
4307 error ("zero or negative size array %q+D", decl);
4308 break;
4310 case 0:
4311 /* For global variables, update the copy of the type that
4312 exists in the binding. */
4313 if (TREE_PUBLIC (decl))
4315 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4316 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4317 b_ext = b_ext->shadowed;
4318 if (b_ext)
4320 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4321 b_ext->u.type = composite_type (b_ext->u.type, type);
4322 else
4323 b_ext->u.type = type;
4326 break;
4328 default:
4329 gcc_unreachable ();
4332 if (DECL_INITIAL (decl))
4333 TREE_TYPE (DECL_INITIAL (decl)) = type;
4335 relayout_decl (decl);
4338 if (TREE_CODE (decl) == VAR_DECL)
4340 if (init && TREE_CODE (init) == CONSTRUCTOR)
4341 add_flexible_array_elts_to_size (decl, init);
4343 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4344 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4345 layout_decl (decl, 0);
4347 if (DECL_SIZE (decl) == 0
4348 /* Don't give an error if we already gave one earlier. */
4349 && TREE_TYPE (decl) != error_mark_node
4350 && (TREE_STATIC (decl)
4351 /* A static variable with an incomplete type
4352 is an error if it is initialized.
4353 Also if it is not file scope.
4354 Otherwise, let it through, but if it is not `extern'
4355 then it may cause an error message later. */
4356 ? (DECL_INITIAL (decl) != 0
4357 || !DECL_FILE_SCOPE_P (decl))
4358 /* An automatic variable with an incomplete type
4359 is an error. */
4360 : !DECL_EXTERNAL (decl)))
4362 error ("storage size of %q+D isn%'t known", decl);
4363 TREE_TYPE (decl) = error_mark_node;
4366 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4367 && DECL_SIZE (decl) != 0)
4369 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4370 constant_expression_warning (DECL_SIZE (decl));
4371 else
4373 error ("storage size of %q+D isn%'t constant", decl);
4374 TREE_TYPE (decl) = error_mark_node;
4378 if (TREE_USED (type))
4380 TREE_USED (decl) = 1;
4381 DECL_READ_P (decl) = 1;
4385 /* If this is a function and an assembler name is specified, reset DECL_RTL
4386 so we can give it its new name. Also, update builtin_decl if it
4387 was a normal built-in. */
4388 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4390 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4391 set_builtin_user_assembler_name (decl, asmspec);
4392 set_user_assembler_name (decl, asmspec);
4395 /* If #pragma weak was used, mark the decl weak now. */
4396 maybe_apply_pragma_weak (decl);
4398 /* Output the assembler code and/or RTL code for variables and functions,
4399 unless the type is an undefined structure or union.
4400 If not, it will get done when the type is completed. */
4402 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4404 /* Determine the ELF visibility. */
4405 if (TREE_PUBLIC (decl))
4406 c_determine_visibility (decl);
4408 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4409 if (c_dialect_objc ())
4410 objc_check_decl (decl);
4412 if (asmspec)
4414 /* If this is not a static variable, issue a warning.
4415 It doesn't make any sense to give an ASMSPEC for an
4416 ordinary, non-register local variable. Historically,
4417 GCC has accepted -- but ignored -- the ASMSPEC in
4418 this case. */
4419 if (!DECL_FILE_SCOPE_P (decl)
4420 && TREE_CODE (decl) == VAR_DECL
4421 && !C_DECL_REGISTER (decl)
4422 && !TREE_STATIC (decl))
4423 warning (0, "ignoring asm-specifier for non-static local "
4424 "variable %q+D", decl);
4425 else
4426 set_user_assembler_name (decl, asmspec);
4429 if (DECL_FILE_SCOPE_P (decl))
4431 if (DECL_INITIAL (decl) == NULL_TREE
4432 || DECL_INITIAL (decl) == error_mark_node)
4433 /* Don't output anything
4434 when a tentative file-scope definition is seen.
4435 But at end of compilation, do output code for them. */
4436 DECL_DEFER_OUTPUT (decl) = 1;
4437 if (asmspec && C_DECL_REGISTER (decl))
4438 DECL_HARD_REGISTER (decl) = 1;
4439 rest_of_decl_compilation (decl, true, 0);
4441 else
4443 /* In conjunction with an ASMSPEC, the `register'
4444 keyword indicates that we should place the variable
4445 in a particular register. */
4446 if (asmspec && C_DECL_REGISTER (decl))
4448 DECL_HARD_REGISTER (decl) = 1;
4449 /* This cannot be done for a structure with volatile
4450 fields, on which DECL_REGISTER will have been
4451 reset. */
4452 if (!DECL_REGISTER (decl))
4453 error ("cannot put object with volatile field into register");
4456 if (TREE_CODE (decl) != FUNCTION_DECL)
4458 /* If we're building a variable sized type, and we might be
4459 reachable other than via the top of the current binding
4460 level, then create a new BIND_EXPR so that we deallocate
4461 the object at the right time. */
4462 /* Note that DECL_SIZE can be null due to errors. */
4463 if (DECL_SIZE (decl)
4464 && !TREE_CONSTANT (DECL_SIZE (decl))
4465 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4467 tree bind;
4468 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4469 TREE_SIDE_EFFECTS (bind) = 1;
4470 add_stmt (bind);
4471 BIND_EXPR_BODY (bind) = push_stmt_list ();
4473 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4474 DECL_EXPR, decl));
4479 if (!DECL_FILE_SCOPE_P (decl))
4481 /* Recompute the RTL of a local array now
4482 if it used to be an incomplete type. */
4483 if (was_incomplete
4484 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4486 /* If we used it already as memory, it must stay in memory. */
4487 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4488 /* If it's still incomplete now, no init will save it. */
4489 if (DECL_SIZE (decl) == 0)
4490 DECL_INITIAL (decl) = 0;
4495 if (TREE_CODE (decl) == TYPE_DECL)
4497 if (!DECL_FILE_SCOPE_P (decl)
4498 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4499 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4501 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4504 /* Install a cleanup (aka destructor) if one was given. */
4505 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4507 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4508 if (attr)
4510 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4511 tree cleanup_decl = lookup_name (cleanup_id);
4512 tree cleanup;
4513 VEC(tree,gc) *vec;
4515 /* Build "cleanup(&decl)" for the destructor. */
4516 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4517 vec = VEC_alloc (tree, gc, 1);
4518 VEC_quick_push (tree, vec, cleanup);
4519 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4520 cleanup_decl, vec, NULL);
4521 VEC_free (tree, gc, vec);
4523 /* Don't warn about decl unused; the cleanup uses it. */
4524 TREE_USED (decl) = 1;
4525 TREE_USED (cleanup_decl) = 1;
4526 DECL_READ_P (decl) = 1;
4528 push_cleanup (decl, cleanup, false);
4532 if (warn_cxx_compat
4533 && TREE_CODE (decl) == VAR_DECL
4534 && !DECL_EXTERNAL (decl)
4535 && DECL_INITIAL (decl) == NULL_TREE)
4537 type = strip_array_types (type);
4538 if (TREE_READONLY (decl))
4539 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4540 "uninitialized const %qD is invalid in C++", decl);
4541 else if ((TREE_CODE (type) == RECORD_TYPE
4542 || TREE_CODE (type) == UNION_TYPE)
4543 && C_TYPE_FIELDS_READONLY (type))
4544 diagnose_uninitialized_cst_member (decl, type);
4547 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4550 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4551 EXPR is NULL or a pointer to an expression that needs to be
4552 evaluated for the side effects of array size expressions in the
4553 parameters. */
4555 tree
4556 grokparm (const struct c_parm *parm, tree *expr)
4558 tree attrs = parm->attrs;
4559 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4560 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4562 decl_attributes (&decl, attrs, 0);
4564 return decl;
4567 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4568 and push that on the current scope. EXPR is a pointer to an
4569 expression that needs to be evaluated for the side effects of array
4570 size expressions in the parameters. */
4572 void
4573 push_parm_decl (const struct c_parm *parm, tree *expr)
4575 tree attrs = parm->attrs;
4576 tree decl;
4578 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4579 &attrs, expr, NULL, DEPRECATED_NORMAL);
4580 decl_attributes (&decl, attrs, 0);
4582 decl = pushdecl (decl);
4584 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4587 /* Mark all the parameter declarations to date as forward decls.
4588 Also diagnose use of this extension. */
4590 void
4591 mark_forward_parm_decls (void)
4593 struct c_binding *b;
4595 if (pedantic && !current_scope->warned_forward_parm_decls)
4597 pedwarn (input_location, OPT_pedantic,
4598 "ISO C forbids forward parameter declarations");
4599 current_scope->warned_forward_parm_decls = true;
4602 for (b = current_scope->bindings; b; b = b->prev)
4603 if (TREE_CODE (b->decl) == PARM_DECL)
4604 TREE_ASM_WRITTEN (b->decl) = 1;
4607 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4608 literal, which may be an incomplete array type completed by the
4609 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4610 literal. NON_CONST is true if the initializers contain something
4611 that cannot occur in a constant expression. */
4613 tree
4614 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4616 /* We do not use start_decl here because we have a type, not a declarator;
4617 and do not use finish_decl because the decl should be stored inside
4618 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4619 tree decl;
4620 tree complit;
4621 tree stmt;
4623 if (type == error_mark_node
4624 || init == error_mark_node)
4625 return error_mark_node;
4627 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4628 DECL_EXTERNAL (decl) = 0;
4629 TREE_PUBLIC (decl) = 0;
4630 TREE_STATIC (decl) = (current_scope == file_scope);
4631 DECL_CONTEXT (decl) = current_function_decl;
4632 TREE_USED (decl) = 1;
4633 DECL_READ_P (decl) = 1;
4634 TREE_TYPE (decl) = type;
4635 TREE_READONLY (decl) = TYPE_READONLY (type);
4636 store_init_value (loc, decl, init, NULL_TREE);
4638 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4640 int failure = complete_array_type (&TREE_TYPE (decl),
4641 DECL_INITIAL (decl), true);
4642 gcc_assert (!failure);
4644 type = TREE_TYPE (decl);
4645 TREE_TYPE (DECL_INITIAL (decl)) = type;
4648 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4649 return error_mark_node;
4651 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4652 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4653 TREE_SIDE_EFFECTS (complit) = 1;
4655 layout_decl (decl, 0);
4657 if (TREE_STATIC (decl))
4659 /* This decl needs a name for the assembler output. */
4660 set_compound_literal_name (decl);
4661 DECL_DEFER_OUTPUT (decl) = 1;
4662 DECL_COMDAT (decl) = 1;
4663 DECL_ARTIFICIAL (decl) = 1;
4664 DECL_IGNORED_P (decl) = 1;
4665 pushdecl (decl);
4666 rest_of_decl_compilation (decl, 1, 0);
4669 if (non_const)
4671 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4672 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4675 return complit;
4678 /* Check the type of a compound literal. Here we just check that it
4679 is valid for C++. */
4681 void
4682 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4684 if (warn_cxx_compat
4685 && (type_name->specs->typespec_kind == ctsk_tagdef
4686 || type_name->specs->typespec_kind == ctsk_tagfirstref))
4687 warning_at (loc, OPT_Wc___compat,
4688 "defining a type in a compound literal is invalid in C++");
4691 /* Determine whether TYPE is a structure with a flexible array member,
4692 or a union containing such a structure (possibly recursively). */
4694 static bool
4695 flexible_array_type_p (tree type)
4697 tree x;
4698 switch (TREE_CODE (type))
4700 case RECORD_TYPE:
4701 x = TYPE_FIELDS (type);
4702 if (x == NULL_TREE)
4703 return false;
4704 while (DECL_CHAIN (x) != NULL_TREE)
4705 x = DECL_CHAIN (x);
4706 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4707 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4708 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4709 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4710 return true;
4711 return false;
4712 case UNION_TYPE:
4713 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4715 if (flexible_array_type_p (TREE_TYPE (x)))
4716 return true;
4718 return false;
4719 default:
4720 return false;
4724 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4725 replacing with appropriate values if they are invalid. */
4726 static void
4727 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4729 tree type_mv;
4730 unsigned int max_width;
4731 unsigned HOST_WIDE_INT w;
4732 const char *name = (orig_name
4733 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4734 : _("<anonymous>"));
4736 /* Detect and ignore out of range field width and process valid
4737 field widths. */
4738 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4740 error ("bit-field %qs width not an integer constant", name);
4741 *width = integer_one_node;
4743 else
4745 if (TREE_CODE (*width) != INTEGER_CST)
4747 *width = c_fully_fold (*width, false, NULL);
4748 if (TREE_CODE (*width) == INTEGER_CST)
4749 pedwarn (input_location, OPT_pedantic,
4750 "bit-field %qs width not an integer constant expression",
4751 name);
4753 if (TREE_CODE (*width) != INTEGER_CST)
4755 error ("bit-field %qs width not an integer constant", name);
4756 *width = integer_one_node;
4758 constant_expression_warning (*width);
4759 if (tree_int_cst_sgn (*width) < 0)
4761 error ("negative width in bit-field %qs", name);
4762 *width = integer_one_node;
4764 else if (integer_zerop (*width) && orig_name)
4766 error ("zero width for bit-field %qs", name);
4767 *width = integer_one_node;
4771 /* Detect invalid bit-field type. */
4772 if (TREE_CODE (*type) != INTEGER_TYPE
4773 && TREE_CODE (*type) != BOOLEAN_TYPE
4774 && TREE_CODE (*type) != ENUMERAL_TYPE)
4776 error ("bit-field %qs has invalid type", name);
4777 *type = unsigned_type_node;
4780 type_mv = TYPE_MAIN_VARIANT (*type);
4781 if (!in_system_header
4782 && type_mv != integer_type_node
4783 && type_mv != unsigned_type_node
4784 && type_mv != boolean_type_node)
4785 pedwarn (input_location, OPT_pedantic,
4786 "type of bit-field %qs is a GCC extension", name);
4788 max_width = TYPE_PRECISION (*type);
4790 if (0 < compare_tree_int (*width, max_width))
4792 error ("width of %qs exceeds its type", name);
4793 w = max_width;
4794 *width = build_int_cst (integer_type_node, w);
4796 else
4797 w = tree_low_cst (*width, 1);
4799 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4801 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4802 if (!lt
4803 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4804 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4805 warning (0, "%qs is narrower than values of its type", name);
4811 /* Print warning about variable length array if necessary. */
4813 static void
4814 warn_variable_length_array (tree name, tree size)
4816 int const_size = TREE_CONSTANT (size);
4818 if (!flag_isoc99 && pedantic && warn_vla != 0)
4820 if (const_size)
4822 if (name)
4823 pedwarn (input_location, OPT_Wvla,
4824 "ISO C90 forbids array %qE whose size "
4825 "can%'t be evaluated",
4826 name);
4827 else
4828 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4829 "can%'t be evaluated");
4831 else
4833 if (name)
4834 pedwarn (input_location, OPT_Wvla,
4835 "ISO C90 forbids variable length array %qE",
4836 name);
4837 else
4838 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4841 else if (warn_vla > 0)
4843 if (const_size)
4845 if (name)
4846 warning (OPT_Wvla,
4847 "the size of array %qE can"
4848 "%'t be evaluated", name);
4849 else
4850 warning (OPT_Wvla,
4851 "the size of array can %'t be evaluated");
4853 else
4855 if (name)
4856 warning (OPT_Wvla,
4857 "variable length array %qE is used",
4858 name);
4859 else
4860 warning (OPT_Wvla,
4861 "variable length array is used");
4866 /* Given declspecs and a declarator,
4867 determine the name and type of the object declared
4868 and construct a ..._DECL node for it.
4869 (In one case we can return a ..._TYPE node instead.
4870 For invalid input we sometimes return 0.)
4872 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4874 DECL_CONTEXT says which syntactic context this declaration is in:
4875 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4876 FUNCDEF for a function definition. Like NORMAL but a few different
4877 error messages in each case. Return value may be zero meaning
4878 this definition is too screwy to try to parse.
4879 PARM for a parameter declaration (either within a function prototype
4880 or before a function body). Make a PARM_DECL, or return void_type_node.
4881 TYPENAME if for a typename (in a cast or sizeof).
4882 Don't make a DECL node; just return the ..._TYPE node.
4883 FIELD for a struct or union field; make a FIELD_DECL.
4884 INITIALIZED is true if the decl has an initializer.
4885 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4886 representing the width of the bit-field.
4887 DECL_ATTRS points to the list of attributes that should be added to this
4888 decl. Any nested attributes that belong on the decl itself will be
4889 added to this list.
4890 If EXPR is not NULL, any expressions that need to be evaluated as
4891 part of evaluating variably modified types will be stored in *EXPR.
4892 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4893 set to indicate whether operands in *EXPR can be used in constant
4894 expressions.
4895 DEPRECATED_STATE is a deprecated_states value indicating whether
4896 deprecation warnings should be suppressed.
4898 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4899 It may also be so in the PARM case, for a prototype where the
4900 argument type is specified but not the name.
4902 This function is where the complicated C meanings of `static'
4903 and `extern' are interpreted. */
4905 static tree
4906 grokdeclarator (const struct c_declarator *declarator,
4907 struct c_declspecs *declspecs,
4908 enum decl_context decl_context, bool initialized, tree *width,
4909 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4910 enum deprecated_states deprecated_state)
4912 tree type = declspecs->type;
4913 bool threadp = declspecs->thread_p;
4914 enum c_storage_class storage_class = declspecs->storage_class;
4915 int constp;
4916 int restrictp;
4917 int volatilep;
4918 int type_quals = TYPE_UNQUALIFIED;
4919 tree name = NULL_TREE;
4920 bool funcdef_flag = false;
4921 bool funcdef_syntax = false;
4922 bool size_varies = false;
4923 tree decl_attr = declspecs->decl_attr;
4924 int array_ptr_quals = TYPE_UNQUALIFIED;
4925 tree array_ptr_attrs = NULL_TREE;
4926 int array_parm_static = 0;
4927 bool array_parm_vla_unspec_p = false;
4928 tree returned_attrs = NULL_TREE;
4929 bool bitfield = width != NULL;
4930 tree element_type;
4931 struct c_arg_info *arg_info = 0;
4932 addr_space_t as1, as2, address_space;
4933 location_t loc = UNKNOWN_LOCATION;
4934 const char *errmsg;
4935 tree expr_dummy;
4936 bool expr_const_operands_dummy;
4937 enum c_declarator_kind first_non_attr_kind;
4938 unsigned int alignas_align = 0;
4940 if (TREE_CODE (type) == ERROR_MARK)
4941 return error_mark_node;
4942 if (expr == NULL)
4943 expr = &expr_dummy;
4944 if (expr_const_operands == NULL)
4945 expr_const_operands = &expr_const_operands_dummy;
4947 *expr = declspecs->expr;
4948 *expr_const_operands = declspecs->expr_const_operands;
4950 if (decl_context == FUNCDEF)
4951 funcdef_flag = true, decl_context = NORMAL;
4953 /* Look inside a declarator for the name being declared
4954 and get it as an IDENTIFIER_NODE, for an error message. */
4956 const struct c_declarator *decl = declarator;
4958 first_non_attr_kind = cdk_attrs;
4959 while (decl)
4960 switch (decl->kind)
4962 case cdk_array:
4963 loc = decl->id_loc;
4964 /* FALL THRU. */
4966 case cdk_function:
4967 case cdk_pointer:
4968 funcdef_syntax = (decl->kind == cdk_function);
4969 decl = decl->declarator;
4970 if (first_non_attr_kind == cdk_attrs)
4971 first_non_attr_kind = decl->kind;
4972 break;
4974 case cdk_attrs:
4975 decl = decl->declarator;
4976 break;
4978 case cdk_id:
4979 loc = decl->id_loc;
4980 if (decl->u.id)
4981 name = decl->u.id;
4982 if (first_non_attr_kind == cdk_attrs)
4983 first_non_attr_kind = decl->kind;
4984 decl = 0;
4985 break;
4987 default:
4988 gcc_unreachable ();
4990 if (name == 0)
4992 gcc_assert (decl_context == PARM
4993 || decl_context == TYPENAME
4994 || (decl_context == FIELD
4995 && declarator->kind == cdk_id));
4996 gcc_assert (!initialized);
5000 /* A function definition's declarator must have the form of
5001 a function declarator. */
5003 if (funcdef_flag && !funcdef_syntax)
5004 return 0;
5006 /* If this looks like a function definition, make it one,
5007 even if it occurs where parms are expected.
5008 Then store_parm_decls will reject it and not use it as a parm. */
5009 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5010 decl_context = PARM;
5012 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5013 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5015 if ((decl_context == NORMAL || decl_context == FIELD)
5016 && current_scope == file_scope
5017 && variably_modified_type_p (type, NULL_TREE))
5019 if (name)
5020 error_at (loc, "variably modified %qE at file scope", name);
5021 else
5022 error_at (loc, "variably modified field at file scope");
5023 type = integer_type_node;
5026 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5028 /* Diagnose defaulting to "int". */
5030 if (declspecs->default_int_p && !in_system_header)
5032 /* Issue a warning if this is an ISO C 99 program or if
5033 -Wreturn-type and this is a function, or if -Wimplicit;
5034 prefer the former warning since it is more explicit. */
5035 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5036 && funcdef_flag)
5037 warn_about_return_type = 1;
5038 else
5040 if (name)
5041 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5042 "type defaults to %<int%> in declaration of %qE",
5043 name);
5044 else
5045 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5046 "type defaults to %<int%> in type name");
5050 /* Adjust the type if a bit-field is being declared,
5051 -funsigned-bitfields applied and the type is not explicitly
5052 "signed". */
5053 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5054 && TREE_CODE (type) == INTEGER_TYPE)
5055 type = unsigned_type_for (type);
5057 /* Figure out the type qualifiers for the declaration. There are
5058 two ways a declaration can become qualified. One is something
5059 like `const int i' where the `const' is explicit. Another is
5060 something like `typedef const int CI; CI i' where the type of the
5061 declaration contains the `const'. A third possibility is that
5062 there is a type qualifier on the element type of a typedefed
5063 array type, in which case we should extract that qualifier so
5064 that c_apply_type_quals_to_decl receives the full list of
5065 qualifiers to work with (C90 is not entirely clear about whether
5066 duplicate qualifiers should be diagnosed in this case, but it
5067 seems most appropriate to do so). */
5068 element_type = strip_array_types (type);
5069 constp = declspecs->const_p + TYPE_READONLY (element_type);
5070 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5071 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5072 as1 = declspecs->address_space;
5073 as2 = TYPE_ADDR_SPACE (element_type);
5074 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5076 if (pedantic && !flag_isoc99)
5078 if (constp > 1)
5079 pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
5080 if (restrictp > 1)
5081 pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
5082 if (volatilep > 1)
5083 pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
5086 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5087 error_at (loc, "conflicting named address spaces (%s vs %s)",
5088 c_addr_space_name (as1), c_addr_space_name (as2));
5090 if ((TREE_CODE (type) == ARRAY_TYPE
5091 || first_non_attr_kind == cdk_array)
5092 && TYPE_QUALS (element_type))
5093 type = TYPE_MAIN_VARIANT (type);
5094 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5095 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5096 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5097 | ENCODE_QUAL_ADDR_SPACE (address_space));
5099 /* Warn about storage classes that are invalid for certain
5100 kinds of declarations (parameters, typenames, etc.). */
5102 if (funcdef_flag
5103 && (threadp
5104 || storage_class == csc_auto
5105 || storage_class == csc_register
5106 || storage_class == csc_typedef))
5108 if (storage_class == csc_auto)
5109 pedwarn (loc,
5110 (current_scope == file_scope) ? 0 : OPT_pedantic,
5111 "function definition declared %<auto%>");
5112 if (storage_class == csc_register)
5113 error_at (loc, "function definition declared %<register%>");
5114 if (storage_class == csc_typedef)
5115 error_at (loc, "function definition declared %<typedef%>");
5116 if (threadp)
5117 error_at (loc, "function definition declared %<__thread%>");
5118 threadp = false;
5119 if (storage_class == csc_auto
5120 || storage_class == csc_register
5121 || storage_class == csc_typedef)
5122 storage_class = csc_none;
5124 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5126 if (decl_context == PARM && storage_class == csc_register)
5128 else
5130 switch (decl_context)
5132 case FIELD:
5133 if (name)
5134 error_at (loc, "storage class specified for structure "
5135 "field %qE", name);
5136 else
5137 error_at (loc, "storage class specified for structure field");
5138 break;
5139 case PARM:
5140 if (name)
5141 error_at (loc, "storage class specified for parameter %qE",
5142 name);
5143 else
5144 error_at (loc, "storage class specified for unnamed parameter");
5145 break;
5146 default:
5147 error_at (loc, "storage class specified for typename");
5148 break;
5150 storage_class = csc_none;
5151 threadp = false;
5154 else if (storage_class == csc_extern
5155 && initialized
5156 && !funcdef_flag)
5158 /* 'extern' with initialization is invalid if not at file scope. */
5159 if (current_scope == file_scope)
5161 /* It is fine to have 'extern const' when compiling at C
5162 and C++ intersection. */
5163 if (!(warn_cxx_compat && constp))
5164 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5165 name);
5167 else
5168 error_at (loc, "%qE has both %<extern%> and initializer", name);
5170 else if (current_scope == file_scope)
5172 if (storage_class == csc_auto)
5173 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5174 name);
5175 if (pedantic && storage_class == csc_register)
5176 pedwarn (input_location, OPT_pedantic,
5177 "file-scope declaration of %qE specifies %<register%>", name);
5179 else
5181 if (storage_class == csc_extern && funcdef_flag)
5182 error_at (loc, "nested function %qE declared %<extern%>", name);
5183 else if (threadp && storage_class == csc_none)
5185 error_at (loc, "function-scope %qE implicitly auto and declared "
5186 "%<__thread%>",
5187 name);
5188 threadp = false;
5192 /* Now figure out the structure of the declarator proper.
5193 Descend through it, creating more complex types, until we reach
5194 the declared identifier (or NULL_TREE, in an absolute declarator).
5195 At each stage we maintain an unqualified version of the type
5196 together with any qualifiers that should be applied to it with
5197 c_build_qualified_type; this way, array types including
5198 multidimensional array types are first built up in unqualified
5199 form and then the qualified form is created with
5200 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5202 while (declarator && declarator->kind != cdk_id)
5204 if (type == error_mark_node)
5206 declarator = declarator->declarator;
5207 continue;
5210 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5211 a cdk_pointer (for *...),
5212 a cdk_function (for ...(...)),
5213 a cdk_attrs (for nested attributes),
5214 or a cdk_id (for the name being declared
5215 or the place in an absolute declarator
5216 where the name was omitted).
5217 For the last case, we have just exited the loop.
5219 At this point, TYPE is the type of elements of an array,
5220 or for a function to return, or for a pointer to point to.
5221 After this sequence of ifs, TYPE is the type of the
5222 array or function or pointer, and DECLARATOR has had its
5223 outermost layer removed. */
5225 if (array_ptr_quals != TYPE_UNQUALIFIED
5226 || array_ptr_attrs != NULL_TREE
5227 || array_parm_static)
5229 /* Only the innermost declarator (making a parameter be of
5230 array type which is converted to pointer type)
5231 may have static or type qualifiers. */
5232 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5233 array_ptr_quals = TYPE_UNQUALIFIED;
5234 array_ptr_attrs = NULL_TREE;
5235 array_parm_static = 0;
5238 switch (declarator->kind)
5240 case cdk_attrs:
5242 /* A declarator with embedded attributes. */
5243 tree attrs = declarator->u.attrs;
5244 const struct c_declarator *inner_decl;
5245 int attr_flags = 0;
5246 declarator = declarator->declarator;
5247 inner_decl = declarator;
5248 while (inner_decl->kind == cdk_attrs)
5249 inner_decl = inner_decl->declarator;
5250 if (inner_decl->kind == cdk_id)
5251 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5252 else if (inner_decl->kind == cdk_function)
5253 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5254 else if (inner_decl->kind == cdk_array)
5255 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5256 returned_attrs = decl_attributes (&type,
5257 chainon (returned_attrs, attrs),
5258 attr_flags);
5259 break;
5261 case cdk_array:
5263 tree itype = NULL_TREE;
5264 tree size = declarator->u.array.dimen;
5265 /* The index is a signed object `sizetype' bits wide. */
5266 tree index_type = c_common_signed_type (sizetype);
5268 array_ptr_quals = declarator->u.array.quals;
5269 array_ptr_attrs = declarator->u.array.attrs;
5270 array_parm_static = declarator->u.array.static_p;
5271 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5273 declarator = declarator->declarator;
5275 /* Check for some types that there cannot be arrays of. */
5277 if (VOID_TYPE_P (type))
5279 if (name)
5280 error_at (loc, "declaration of %qE as array of voids", name);
5281 else
5282 error_at (loc, "declaration of type name as array of voids");
5283 type = error_mark_node;
5286 if (TREE_CODE (type) == FUNCTION_TYPE)
5288 if (name)
5289 error_at (loc, "declaration of %qE as array of functions",
5290 name);
5291 else
5292 error_at (loc, "declaration of type name as array of "
5293 "functions");
5294 type = error_mark_node;
5297 if (pedantic && !in_system_header && flexible_array_type_p (type))
5298 pedwarn (loc, OPT_pedantic,
5299 "invalid use of structure with flexible array member");
5301 if (size == error_mark_node)
5302 type = error_mark_node;
5304 if (type == error_mark_node)
5305 continue;
5307 /* If size was specified, set ITYPE to a range-type for
5308 that size. Otherwise, ITYPE remains null. finish_decl
5309 may figure it out from an initial value. */
5311 if (size)
5313 bool size_maybe_const = true;
5314 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5315 && !TREE_OVERFLOW (size));
5316 bool this_size_varies = false;
5318 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5319 lvalue. */
5320 STRIP_TYPE_NOPS (size);
5322 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5324 if (name)
5325 error_at (loc, "size of array %qE has non-integer type",
5326 name);
5327 else
5328 error_at (loc,
5329 "size of unnamed array has non-integer type");
5330 size = integer_one_node;
5333 size = c_fully_fold (size, false, &size_maybe_const);
5335 if (pedantic && size_maybe_const && integer_zerop (size))
5337 if (name)
5338 pedwarn (loc, OPT_pedantic,
5339 "ISO C forbids zero-size array %qE", name);
5340 else
5341 pedwarn (loc, OPT_pedantic,
5342 "ISO C forbids zero-size array");
5345 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5347 constant_expression_warning (size);
5348 if (tree_int_cst_sgn (size) < 0)
5350 if (name)
5351 error_at (loc, "size of array %qE is negative", name);
5352 else
5353 error_at (loc, "size of unnamed array is negative");
5354 size = integer_one_node;
5356 /* Handle a size folded to an integer constant but
5357 not an integer constant expression. */
5358 if (!size_int_const)
5360 /* If this is a file scope declaration of an
5361 ordinary identifier, this is invalid code;
5362 diagnosing it here and not subsequently
5363 treating the type as variable-length avoids
5364 more confusing diagnostics later. */
5365 if ((decl_context == NORMAL || decl_context == FIELD)
5366 && current_scope == file_scope)
5367 pedwarn (input_location, 0,
5368 "variably modified %qE at file scope",
5369 name);
5370 else
5371 this_size_varies = size_varies = true;
5372 warn_variable_length_array (name, size);
5375 else if ((decl_context == NORMAL || decl_context == FIELD)
5376 && current_scope == file_scope)
5378 error_at (loc, "variably modified %qE at file scope", name);
5379 size = integer_one_node;
5381 else
5383 /* Make sure the array size remains visibly
5384 nonconstant even if it is (eg) a const variable
5385 with known value. */
5386 this_size_varies = size_varies = true;
5387 warn_variable_length_array (name, size);
5390 if (integer_zerop (size) && !this_size_varies)
5392 /* A zero-length array cannot be represented with
5393 an unsigned index type, which is what we'll
5394 get with build_index_type. Create an
5395 open-ended range instead. */
5396 itype = build_range_type (sizetype, size, NULL_TREE);
5398 else
5400 /* Arrange for the SAVE_EXPR on the inside of the
5401 MINUS_EXPR, which allows the -1 to get folded
5402 with the +1 that happens when building TYPE_SIZE. */
5403 if (size_varies)
5404 size = save_expr (size);
5405 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5406 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5407 integer_zero_node, size);
5409 /* Compute the maximum valid index, that is, size
5410 - 1. Do the calculation in index_type, so that
5411 if it is a variable the computations will be
5412 done in the proper mode. */
5413 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5414 convert (index_type, size),
5415 convert (index_type,
5416 size_one_node));
5418 /* The above overflows when size does not fit
5419 in index_type.
5420 ??? While a size of INT_MAX+1 technically shouldn't
5421 cause an overflow (because we subtract 1), handling
5422 this case seems like an unnecessary complication. */
5423 if (TREE_CODE (size) == INTEGER_CST
5424 && !int_fits_type_p (size, index_type))
5426 if (name)
5427 error_at (loc, "size of array %qE is too large",
5428 name);
5429 else
5430 error_at (loc, "size of unnamed array is too large");
5431 type = error_mark_node;
5432 continue;
5435 itype = build_index_type (itype);
5437 if (this_size_varies)
5439 if (*expr)
5440 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5441 *expr, size);
5442 else
5443 *expr = size;
5444 *expr_const_operands &= size_maybe_const;
5447 else if (decl_context == FIELD)
5449 bool flexible_array_member = false;
5450 if (array_parm_vla_unspec_p)
5451 /* Field names can in fact have function prototype
5452 scope so [*] is disallowed here through making
5453 the field variably modified, not through being
5454 something other than a declaration with function
5455 prototype scope. */
5456 size_varies = true;
5457 else
5459 const struct c_declarator *t = declarator;
5460 while (t->kind == cdk_attrs)
5461 t = t->declarator;
5462 flexible_array_member = (t->kind == cdk_id);
5464 if (flexible_array_member
5465 && pedantic && !flag_isoc99 && !in_system_header)
5466 pedwarn (loc, OPT_pedantic,
5467 "ISO C90 does not support flexible array members");
5469 /* ISO C99 Flexible array members are effectively
5470 identical to GCC's zero-length array extension. */
5471 if (flexible_array_member || array_parm_vla_unspec_p)
5472 itype = build_range_type (sizetype, size_zero_node,
5473 NULL_TREE);
5475 else if (decl_context == PARM)
5477 if (array_parm_vla_unspec_p)
5479 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5480 size_varies = true;
5483 else if (decl_context == TYPENAME)
5485 if (array_parm_vla_unspec_p)
5487 /* C99 6.7.5.2p4 */
5488 warning (0, "%<[*]%> not in a declaration");
5489 /* We use this to avoid messing up with incomplete
5490 array types of the same type, that would
5491 otherwise be modified below. */
5492 itype = build_range_type (sizetype, size_zero_node,
5493 NULL_TREE);
5494 size_varies = true;
5498 /* Complain about arrays of incomplete types. */
5499 if (!COMPLETE_TYPE_P (type))
5501 error_at (loc, "array type has incomplete element type");
5502 type = error_mark_node;
5504 else
5505 /* When itype is NULL, a shared incomplete array type is
5506 returned for all array of a given type. Elsewhere we
5507 make sure we don't complete that type before copying
5508 it, but here we want to make sure we don't ever
5509 modify the shared type, so we gcc_assert (itype)
5510 below. */
5512 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5513 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5514 type = build_qualified_type (type,
5515 ENCODE_QUAL_ADDR_SPACE (as));
5517 type = build_array_type (type, itype);
5520 if (type != error_mark_node)
5522 if (size_varies)
5524 /* It is ok to modify type here even if itype is
5525 NULL: if size_varies, we're in a
5526 multi-dimensional array and the inner type has
5527 variable size, so the enclosing shared array type
5528 must too. */
5529 if (size && TREE_CODE (size) == INTEGER_CST)
5530 type
5531 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5532 C_TYPE_VARIABLE_SIZE (type) = 1;
5535 /* The GCC extension for zero-length arrays differs from
5536 ISO flexible array members in that sizeof yields
5537 zero. */
5538 if (size && integer_zerop (size))
5540 gcc_assert (itype);
5541 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5542 TYPE_SIZE (type) = bitsize_zero_node;
5543 TYPE_SIZE_UNIT (type) = size_zero_node;
5544 SET_TYPE_STRUCTURAL_EQUALITY (type);
5546 if (array_parm_vla_unspec_p)
5548 gcc_assert (itype);
5549 /* The type is complete. C99 6.7.5.2p4 */
5550 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5551 TYPE_SIZE (type) = bitsize_zero_node;
5552 TYPE_SIZE_UNIT (type) = size_zero_node;
5553 SET_TYPE_STRUCTURAL_EQUALITY (type);
5557 if (decl_context != PARM
5558 && (array_ptr_quals != TYPE_UNQUALIFIED
5559 || array_ptr_attrs != NULL_TREE
5560 || array_parm_static))
5562 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5563 array_ptr_quals = TYPE_UNQUALIFIED;
5564 array_ptr_attrs = NULL_TREE;
5565 array_parm_static = 0;
5567 break;
5569 case cdk_function:
5571 /* Say it's a definition only for the declarator closest
5572 to the identifier, apart possibly from some
5573 attributes. */
5574 bool really_funcdef = false;
5575 tree arg_types;
5576 if (funcdef_flag)
5578 const struct c_declarator *t = declarator->declarator;
5579 while (t->kind == cdk_attrs)
5580 t = t->declarator;
5581 really_funcdef = (t->kind == cdk_id);
5584 /* Declaring a function type. Make sure we have a valid
5585 type for the function to return. */
5586 if (type == error_mark_node)
5587 continue;
5589 size_varies = false;
5591 /* Warn about some types functions can't return. */
5592 if (TREE_CODE (type) == FUNCTION_TYPE)
5594 if (name)
5595 error_at (loc, "%qE declared as function returning a "
5596 "function", name);
5597 else
5598 error_at (loc, "type name declared as function "
5599 "returning a function");
5600 type = integer_type_node;
5602 if (TREE_CODE (type) == ARRAY_TYPE)
5604 if (name)
5605 error_at (loc, "%qE declared as function returning an array",
5606 name);
5607 else
5608 error_at (loc, "type name declared as function returning "
5609 "an array");
5610 type = integer_type_node;
5612 errmsg = targetm.invalid_return_type (type);
5613 if (errmsg)
5615 error (errmsg);
5616 type = integer_type_node;
5619 /* Construct the function type and go to the next
5620 inner layer of declarator. */
5621 arg_info = declarator->u.arg_info;
5622 arg_types = grokparms (arg_info, really_funcdef);
5624 /* Type qualifiers before the return type of the function
5625 qualify the return type, not the function type. */
5626 if (type_quals)
5628 /* Type qualifiers on a function return type are
5629 normally permitted by the standard but have no
5630 effect, so give a warning at -Wreturn-type.
5631 Qualifiers on a void return type are banned on
5632 function definitions in ISO C; GCC used to used
5633 them for noreturn functions. */
5634 if (VOID_TYPE_P (type) && really_funcdef)
5635 pedwarn (loc, 0,
5636 "function definition has qualified void return type");
5637 else
5638 warning_at (loc, OPT_Wignored_qualifiers,
5639 "type qualifiers ignored on function return type");
5641 type = c_build_qualified_type (type, type_quals);
5643 type_quals = TYPE_UNQUALIFIED;
5645 type = build_function_type (type, arg_types);
5646 declarator = declarator->declarator;
5648 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5649 the formal parameter list of this FUNCTION_TYPE to point to
5650 the FUNCTION_TYPE node itself. */
5652 c_arg_tag *tag;
5653 unsigned ix;
5655 FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
5656 TYPE_CONTEXT (tag->type) = type;
5658 break;
5660 case cdk_pointer:
5662 /* Merge any constancy or volatility into the target type
5663 for the pointer. */
5665 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5666 && type_quals)
5667 pedwarn (loc, OPT_pedantic,
5668 "ISO C forbids qualified function types");
5669 if (type_quals)
5670 type = c_build_qualified_type (type, type_quals);
5671 size_varies = false;
5673 /* When the pointed-to type involves components of variable size,
5674 care must be taken to ensure that the size evaluation code is
5675 emitted early enough to dominate all the possible later uses
5676 and late enough for the variables on which it depends to have
5677 been assigned.
5679 This is expected to happen automatically when the pointed-to
5680 type has a name/declaration of it's own, but special attention
5681 is required if the type is anonymous.
5683 We handle the NORMAL and FIELD contexts here by attaching an
5684 artificial TYPE_DECL to such pointed-to type. This forces the
5685 sizes evaluation at a safe point and ensures it is not deferred
5686 until e.g. within a deeper conditional context.
5688 We expect nothing to be needed here for PARM or TYPENAME.
5689 Pushing a TYPE_DECL at this point for TYPENAME would actually
5690 be incorrect, as we might be in the middle of an expression
5691 with side effects on the pointed-to type size "arguments" prior
5692 to the pointer declaration point and the fake TYPE_DECL in the
5693 enclosing context would force the size evaluation prior to the
5694 side effects. */
5696 if (!TYPE_NAME (type)
5697 && (decl_context == NORMAL || decl_context == FIELD)
5698 && variably_modified_type_p (type, NULL_TREE))
5700 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5701 DECL_ARTIFICIAL (decl) = 1;
5702 pushdecl (decl);
5703 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5704 TYPE_NAME (type) = decl;
5707 type = c_build_pointer_type (type);
5709 /* Process type qualifiers (such as const or volatile)
5710 that were given inside the `*'. */
5711 type_quals = declarator->u.pointer_quals;
5713 declarator = declarator->declarator;
5714 break;
5716 default:
5717 gcc_unreachable ();
5720 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5722 /* Now TYPE has the actual type, apart from any qualifiers in
5723 TYPE_QUALS. */
5725 /* Warn about address space used for things other than static memory or
5726 pointers. */
5727 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5728 if (!ADDR_SPACE_GENERIC_P (address_space))
5730 if (decl_context == NORMAL)
5732 switch (storage_class)
5734 case csc_auto:
5735 error ("%qs combined with %<auto%> qualifier for %qE",
5736 c_addr_space_name (address_space), name);
5737 break;
5738 case csc_register:
5739 error ("%qs combined with %<register%> qualifier for %qE",
5740 c_addr_space_name (address_space), name);
5741 break;
5742 case csc_none:
5743 if (current_function_scope)
5745 error ("%qs specified for auto variable %qE",
5746 c_addr_space_name (address_space), name);
5747 break;
5749 break;
5750 case csc_static:
5751 case csc_extern:
5752 case csc_typedef:
5753 break;
5754 default:
5755 gcc_unreachable ();
5758 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5760 if (name)
5761 error ("%qs specified for parameter %qE",
5762 c_addr_space_name (address_space), name);
5763 else
5764 error ("%qs specified for unnamed parameter",
5765 c_addr_space_name (address_space));
5767 else if (decl_context == FIELD)
5769 if (name)
5770 error ("%qs specified for structure field %qE",
5771 c_addr_space_name (address_space), name);
5772 else
5773 error ("%qs specified for structure field",
5774 c_addr_space_name (address_space));
5778 /* Check the type and width of a bit-field. */
5779 if (bitfield)
5780 check_bitfield_type_and_width (&type, width, name);
5782 /* Reject invalid uses of _Alignas. */
5783 if (declspecs->alignas_p)
5785 if (storage_class == csc_typedef)
5786 error_at (loc, "alignment specified for typedef %qE", name);
5787 else if (storage_class == csc_register)
5788 error_at (loc, "alignment specified for %<register%> object %qE",
5789 name);
5790 else if (decl_context == PARM)
5792 if (name)
5793 error_at (loc, "alignment specified for parameter %qE", name);
5794 else
5795 error_at (loc, "alignment specified for unnamed parameter");
5797 else if (bitfield)
5799 if (name)
5800 error_at (loc, "alignment specified for bit-field %qE", name);
5801 else
5802 error_at (loc, "alignment specified for unnamed bit-field");
5804 else if (TREE_CODE (type) == FUNCTION_TYPE)
5805 error_at (loc, "alignment specified for function %qE", name);
5806 else if (declspecs->align_log != -1)
5808 alignas_align = 1U << declspecs->align_log;
5809 if (alignas_align < TYPE_ALIGN_UNIT (type))
5811 if (name)
5812 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5813 "alignment of %qE", name);
5814 else
5815 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5816 "alignment of unnamed field");
5817 alignas_align = 0;
5822 /* Did array size calculations overflow? */
5824 if (TREE_CODE (type) == ARRAY_TYPE
5825 && COMPLETE_TYPE_P (type)
5826 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5827 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5829 if (name)
5830 error_at (loc, "size of array %qE is too large", name);
5831 else
5832 error_at (loc, "size of unnamed array is too large");
5833 /* If we proceed with the array type as it is, we'll eventually
5834 crash in tree_low_cst(). */
5835 type = error_mark_node;
5838 /* If this is declaring a typedef name, return a TYPE_DECL. */
5840 if (storage_class == csc_typedef)
5842 tree decl;
5843 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5844 && type_quals)
5845 pedwarn (loc, OPT_pedantic,
5846 "ISO C forbids qualified function types");
5847 if (type_quals)
5848 type = c_build_qualified_type (type, type_quals);
5849 decl = build_decl (declarator->id_loc,
5850 TYPE_DECL, declarator->u.id, type);
5851 if (declspecs->explicit_signed_p)
5852 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5853 if (declspecs->inline_p)
5854 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5855 if (declspecs->noreturn_p)
5856 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
5858 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5860 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5862 if (b != NULL
5863 && b->decl != NULL_TREE
5864 && (B_IN_CURRENT_SCOPE (b)
5865 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5866 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5868 warning_at (declarator->id_loc, OPT_Wc___compat,
5869 ("using %qD as both a typedef and a tag is "
5870 "invalid in C++"),
5871 decl);
5872 if (b->locus != UNKNOWN_LOCATION)
5873 inform (b->locus, "originally defined here");
5877 return decl;
5880 /* If this is a type name (such as, in a cast or sizeof),
5881 compute the type and return it now. */
5883 if (decl_context == TYPENAME)
5885 /* Note that the grammar rejects storage classes in typenames
5886 and fields. */
5887 gcc_assert (storage_class == csc_none && !threadp
5888 && !declspecs->inline_p && !declspecs->noreturn_p);
5889 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5890 && type_quals)
5891 pedwarn (loc, OPT_pedantic,
5892 "ISO C forbids const or volatile function types");
5893 if (type_quals)
5894 type = c_build_qualified_type (type, type_quals);
5895 return type;
5898 if (pedantic && decl_context == FIELD
5899 && variably_modified_type_p (type, NULL_TREE))
5901 /* C99 6.7.2.1p8 */
5902 pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5903 "have a variably modified type");
5906 /* Aside from typedefs and type names (handle above),
5907 `void' at top level (not within pointer)
5908 is allowed only in public variables.
5909 We don't complain about parms either, but that is because
5910 a better error message can be made later. */
5912 if (VOID_TYPE_P (type) && decl_context != PARM
5913 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5914 && (storage_class == csc_extern
5915 || (current_scope == file_scope
5916 && !(storage_class == csc_static
5917 || storage_class == csc_register)))))
5919 error_at (loc, "variable or field %qE declared void", name);
5920 type = integer_type_node;
5923 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5924 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5927 tree decl;
5929 if (decl_context == PARM)
5931 tree promoted_type;
5933 /* A parameter declared as an array of T is really a pointer to T.
5934 One declared as a function is really a pointer to a function. */
5936 if (TREE_CODE (type) == ARRAY_TYPE)
5938 /* Transfer const-ness of array into that of type pointed to. */
5939 type = TREE_TYPE (type);
5940 if (type_quals)
5941 type = c_build_qualified_type (type, type_quals);
5942 type = c_build_pointer_type (type);
5943 type_quals = array_ptr_quals;
5944 if (type_quals)
5945 type = c_build_qualified_type (type, type_quals);
5947 /* We don't yet implement attributes in this context. */
5948 if (array_ptr_attrs != NULL_TREE)
5949 warning_at (loc, OPT_Wattributes,
5950 "attributes in parameter array declarator ignored");
5952 size_varies = false;
5954 else if (TREE_CODE (type) == FUNCTION_TYPE)
5956 if (type_quals)
5957 pedwarn (loc, OPT_pedantic,
5958 "ISO C forbids qualified function types");
5959 if (type_quals)
5960 type = c_build_qualified_type (type, type_quals);
5961 type = c_build_pointer_type (type);
5962 type_quals = TYPE_UNQUALIFIED;
5964 else if (type_quals)
5965 type = c_build_qualified_type (type, type_quals);
5967 decl = build_decl (declarator->id_loc,
5968 PARM_DECL, declarator->u.id, type);
5969 if (size_varies)
5970 C_DECL_VARIABLE_SIZE (decl) = 1;
5972 /* Compute the type actually passed in the parmlist,
5973 for the case where there is no prototype.
5974 (For example, shorts and chars are passed as ints.)
5975 When there is a prototype, this is overridden later. */
5977 if (type == error_mark_node)
5978 promoted_type = type;
5979 else
5980 promoted_type = c_type_promotes_to (type);
5982 DECL_ARG_TYPE (decl) = promoted_type;
5983 if (declspecs->inline_p)
5984 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5985 if (declspecs->noreturn_p)
5986 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
5988 else if (decl_context == FIELD)
5990 /* Note that the grammar rejects storage classes in typenames
5991 and fields. */
5992 gcc_assert (storage_class == csc_none && !threadp
5993 && !declspecs->inline_p && !declspecs->noreturn_p);
5995 /* Structure field. It may not be a function. */
5997 if (TREE_CODE (type) == FUNCTION_TYPE)
5999 error_at (loc, "field %qE declared as a function", name);
6000 type = build_pointer_type (type);
6002 else if (TREE_CODE (type) != ERROR_MARK
6003 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6005 if (name)
6006 error_at (loc, "field %qE has incomplete type", name);
6007 else
6008 error_at (loc, "unnamed field has incomplete type");
6009 type = error_mark_node;
6011 type = c_build_qualified_type (type, type_quals);
6012 decl = build_decl (declarator->id_loc,
6013 FIELD_DECL, declarator->u.id, type);
6014 DECL_NONADDRESSABLE_P (decl) = bitfield;
6015 if (bitfield && !declarator->u.id)
6016 TREE_NO_WARNING (decl) = 1;
6018 if (size_varies)
6019 C_DECL_VARIABLE_SIZE (decl) = 1;
6021 else if (TREE_CODE (type) == FUNCTION_TYPE)
6023 if (storage_class == csc_register || threadp)
6025 error_at (loc, "invalid storage class for function %qE", name);
6027 else if (current_scope != file_scope)
6029 /* Function declaration not at file scope. Storage
6030 classes other than `extern' are not allowed, C99
6031 6.7.1p5, and `extern' makes no difference. However,
6032 GCC allows 'auto', perhaps with 'inline', to support
6033 nested functions. */
6034 if (storage_class == csc_auto)
6035 pedwarn (loc, OPT_pedantic,
6036 "invalid storage class for function %qE", name);
6037 else if (storage_class == csc_static)
6039 error_at (loc, "invalid storage class for function %qE", name);
6040 if (funcdef_flag)
6041 storage_class = declspecs->storage_class = csc_none;
6042 else
6043 return 0;
6047 decl = build_decl (declarator->id_loc,
6048 FUNCTION_DECL, declarator->u.id, type);
6049 decl = build_decl_attribute_variant (decl, decl_attr);
6051 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6052 pedwarn (loc, OPT_pedantic,
6053 "ISO C forbids qualified function types");
6055 /* Every function declaration is an external reference
6056 (DECL_EXTERNAL) except for those which are not at file
6057 scope and are explicitly declared "auto". This is
6058 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6059 GCC to signify a forward declaration of a nested function. */
6060 if (storage_class == csc_auto && current_scope != file_scope)
6061 DECL_EXTERNAL (decl) = 0;
6062 /* In C99, a function which is declared 'inline' with 'extern'
6063 is not an external reference (which is confusing). It
6064 means that the later definition of the function must be output
6065 in this file, C99 6.7.4p6. In GNU C89, a function declared
6066 'extern inline' is an external reference. */
6067 else if (declspecs->inline_p && storage_class != csc_static)
6068 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6069 == flag_gnu89_inline);
6070 else
6071 DECL_EXTERNAL (decl) = !initialized;
6073 /* Record absence of global scope for `static' or `auto'. */
6074 TREE_PUBLIC (decl)
6075 = !(storage_class == csc_static || storage_class == csc_auto);
6077 /* For a function definition, record the argument information
6078 block where store_parm_decls will look for it. */
6079 if (funcdef_flag)
6080 current_function_arg_info = arg_info;
6082 if (declspecs->default_int_p)
6083 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6085 /* Record presence of `inline' and `_Noreturn', if it is
6086 reasonable. */
6087 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6089 if (declspecs->inline_p)
6090 pedwarn (loc, 0, "cannot inline function %<main%>");
6091 if (declspecs->noreturn_p)
6092 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6094 else
6096 if (declspecs->inline_p)
6097 /* Record that the function is declared `inline'. */
6098 DECL_DECLARED_INLINE_P (decl) = 1;
6099 if (declspecs->noreturn_p)
6101 if (!flag_isoc11)
6103 if (flag_isoc99)
6104 pedwarn (loc, OPT_pedantic,
6105 "ISO C99 does not support %<_Noreturn%>");
6106 else
6107 pedwarn (loc, OPT_pedantic,
6108 "ISO C90 does not support %<_Noreturn%>");
6110 TREE_THIS_VOLATILE (decl) = 1;
6114 else
6116 /* It's a variable. */
6117 /* An uninitialized decl with `extern' is a reference. */
6118 int extern_ref = !initialized && storage_class == csc_extern;
6120 type = c_build_qualified_type (type, type_quals);
6122 /* C99 6.2.2p7: It is invalid (compile-time undefined
6123 behavior) to create an 'extern' declaration for a
6124 variable if there is a global declaration that is
6125 'static' and the global declaration is not visible.
6126 (If the static declaration _is_ currently visible,
6127 the 'extern' declaration is taken to refer to that decl.) */
6128 if (extern_ref && current_scope != file_scope)
6130 tree global_decl = identifier_global_value (declarator->u.id);
6131 tree visible_decl = lookup_name (declarator->u.id);
6133 if (global_decl
6134 && global_decl != visible_decl
6135 && TREE_CODE (global_decl) == VAR_DECL
6136 && !TREE_PUBLIC (global_decl))
6137 error_at (loc, "variable previously declared %<static%> "
6138 "redeclared %<extern%>");
6141 decl = build_decl (declarator->id_loc,
6142 VAR_DECL, declarator->u.id, type);
6143 if (size_varies)
6144 C_DECL_VARIABLE_SIZE (decl) = 1;
6146 if (declspecs->inline_p)
6147 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6148 if (declspecs->noreturn_p)
6149 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6151 /* At file scope, an initialized extern declaration may follow
6152 a static declaration. In that case, DECL_EXTERNAL will be
6153 reset later in start_decl. */
6154 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6156 /* At file scope, the presence of a `static' or `register' storage
6157 class specifier, or the absence of all storage class specifiers
6158 makes this declaration a definition (perhaps tentative). Also,
6159 the absence of `static' makes it public. */
6160 if (current_scope == file_scope)
6162 TREE_PUBLIC (decl) = storage_class != csc_static;
6163 TREE_STATIC (decl) = !extern_ref;
6165 /* Not at file scope, only `static' makes a static definition. */
6166 else
6168 TREE_STATIC (decl) = (storage_class == csc_static);
6169 TREE_PUBLIC (decl) = extern_ref;
6172 if (threadp)
6173 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6176 if ((storage_class == csc_extern
6177 || (storage_class == csc_none
6178 && TREE_CODE (type) == FUNCTION_TYPE
6179 && !funcdef_flag))
6180 && variably_modified_type_p (type, NULL_TREE))
6182 /* C99 6.7.5.2p2 */
6183 if (TREE_CODE (type) == FUNCTION_TYPE)
6184 error_at (loc, "non-nested function with variably modified type");
6185 else
6186 error_at (loc, "object with variably modified type must have "
6187 "no linkage");
6190 /* Record `register' declaration for warnings on &
6191 and in case doing stupid register allocation. */
6193 if (storage_class == csc_register)
6195 C_DECL_REGISTER (decl) = 1;
6196 DECL_REGISTER (decl) = 1;
6199 /* Record constancy and volatility. */
6200 c_apply_type_quals_to_decl (type_quals, decl);
6202 /* Apply _Alignas specifiers. */
6203 if (alignas_align)
6205 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6206 DECL_USER_ALIGN (decl) = 1;
6209 /* If a type has volatile components, it should be stored in memory.
6210 Otherwise, the fact that those components are volatile
6211 will be ignored, and would even crash the compiler.
6212 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6213 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6214 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6215 || TREE_CODE (decl) == RESULT_DECL))
6217 /* It is not an error for a structure with volatile fields to
6218 be declared register, but reset DECL_REGISTER since it
6219 cannot actually go in a register. */
6220 int was_reg = C_DECL_REGISTER (decl);
6221 C_DECL_REGISTER (decl) = 0;
6222 DECL_REGISTER (decl) = 0;
6223 c_mark_addressable (decl);
6224 C_DECL_REGISTER (decl) = was_reg;
6227 /* This is the earliest point at which we might know the assembler
6228 name of a variable. Thus, if it's known before this, die horribly. */
6229 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6231 if (warn_cxx_compat
6232 && TREE_CODE (decl) == VAR_DECL
6233 && TREE_PUBLIC (decl)
6234 && TREE_STATIC (decl)
6235 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6236 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6237 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6238 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6239 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6240 ("non-local variable %qD with anonymous type is "
6241 "questionable in C++"),
6242 decl);
6244 return decl;
6248 /* Decode the parameter-list info for a function type or function definition.
6249 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6250 if there is an identifier list instead of a parameter decl list).
6251 These two functions are separate because when a function returns
6252 or receives functions then each is called multiple times but the order
6253 of calls is different. The last call to `grokparms' is always the one
6254 that contains the formal parameter names of a function definition.
6256 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6258 FUNCDEF_FLAG is true for a function definition, false for
6259 a mere declaration. A nonempty identifier-list gets an error message
6260 when FUNCDEF_FLAG is false. */
6262 static tree
6263 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6265 tree arg_types = arg_info->types;
6267 if (funcdef_flag && arg_info->had_vla_unspec)
6269 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6270 /* C99 6.7.5.2p4 */
6271 error ("%<[*]%> not allowed in other than function prototype scope");
6274 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6275 warning (OPT_Wstrict_prototypes,
6276 "function declaration isn%'t a prototype");
6278 if (arg_types == error_mark_node)
6279 return 0; /* don't set TYPE_ARG_TYPES in this case */
6281 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6283 if (!funcdef_flag)
6285 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6286 arg_info->parms = NULL_TREE;
6288 else
6289 arg_info->parms = arg_info->types;
6291 arg_info->types = 0;
6292 return 0;
6294 else
6296 tree parm, type, typelt;
6297 unsigned int parmno;
6298 const char *errmsg;
6300 /* If there is a parameter of incomplete type in a definition,
6301 this is an error. In a declaration this is valid, and a
6302 struct or union type may be completed later, before any calls
6303 or definition of the function. In the case where the tag was
6304 first declared within the parameter list, a warning has
6305 already been given. If a parameter has void type, then
6306 however the function cannot be defined or called, so
6307 warn. */
6309 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6310 parm;
6311 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6313 type = TREE_VALUE (typelt);
6314 if (type == error_mark_node)
6315 continue;
6317 if (!COMPLETE_TYPE_P (type))
6319 if (funcdef_flag)
6321 if (DECL_NAME (parm))
6322 error_at (input_location,
6323 "parameter %u (%q+D) has incomplete type",
6324 parmno, parm);
6325 else
6326 error_at (DECL_SOURCE_LOCATION (parm),
6327 "parameter %u has incomplete type",
6328 parmno);
6330 TREE_VALUE (typelt) = error_mark_node;
6331 TREE_TYPE (parm) = error_mark_node;
6332 arg_types = NULL_TREE;
6334 else if (VOID_TYPE_P (type))
6336 if (DECL_NAME (parm))
6337 warning_at (input_location, 0,
6338 "parameter %u (%q+D) has void type",
6339 parmno, parm);
6340 else
6341 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6342 "parameter %u has void type",
6343 parmno);
6347 errmsg = targetm.invalid_parameter_type (type);
6348 if (errmsg)
6350 error (errmsg);
6351 TREE_VALUE (typelt) = error_mark_node;
6352 TREE_TYPE (parm) = error_mark_node;
6353 arg_types = NULL_TREE;
6356 if (DECL_NAME (parm) && TREE_USED (parm))
6357 warn_if_shadowing (parm);
6359 return arg_types;
6363 /* Allocate and initialize a c_arg_info structure from the parser's
6364 obstack. */
6366 struct c_arg_info *
6367 build_arg_info (void)
6369 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6370 ret->parms = NULL_TREE;
6371 ret->tags = NULL;
6372 ret->types = NULL_TREE;
6373 ret->others = NULL_TREE;
6374 ret->pending_sizes = NULL;
6375 ret->had_vla_unspec = 0;
6376 return ret;
6379 /* Take apart the current scope and return a c_arg_info structure with
6380 info on a parameter list just parsed.
6382 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6384 ELLIPSIS being true means the argument list ended in '...' so don't
6385 append a sentinel (void_list_node) to the end of the type-list.
6387 EXPR is NULL or an expression that needs to be evaluated for the
6388 side effects of array size expressions in the parameters. */
6390 struct c_arg_info *
6391 get_parm_info (bool ellipsis, tree expr)
6393 struct c_binding *b = current_scope->bindings;
6394 struct c_arg_info *arg_info = build_arg_info ();
6396 tree parms = 0;
6397 VEC(c_arg_tag,gc) *tags = NULL;
6398 tree types = 0;
6399 tree others = 0;
6401 static bool explained_incomplete_types = false;
6402 bool gave_void_only_once_err = false;
6404 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6406 /* The bindings in this scope must not get put into a block.
6407 We will take care of deleting the binding nodes. */
6408 current_scope->bindings = 0;
6410 /* This function is only called if there was *something* on the
6411 parameter list. */
6412 gcc_assert (b);
6414 /* A parameter list consisting solely of 'void' indicates that the
6415 function takes no arguments. But if the 'void' is qualified
6416 (by 'const' or 'volatile'), or has a storage class specifier
6417 ('register'), then the behavior is undefined; issue an error.
6418 Typedefs for 'void' are OK (see DR#157). */
6419 if (b->prev == 0 /* one binding */
6420 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6421 && !DECL_NAME (b->decl) /* anonymous */
6422 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6424 if (TREE_THIS_VOLATILE (b->decl)
6425 || TREE_READONLY (b->decl)
6426 || C_DECL_REGISTER (b->decl))
6427 error ("%<void%> as only parameter may not be qualified");
6429 /* There cannot be an ellipsis. */
6430 if (ellipsis)
6431 error ("%<void%> must be the only parameter");
6433 arg_info->types = void_list_node;
6434 return arg_info;
6437 if (!ellipsis)
6438 types = void_list_node;
6440 /* Break up the bindings list into parms, tags, types, and others;
6441 apply sanity checks; purge the name-to-decl bindings. */
6442 while (b)
6444 tree decl = b->decl;
6445 tree type = TREE_TYPE (decl);
6446 c_arg_tag *tag;
6447 const char *keyword;
6449 switch (TREE_CODE (decl))
6451 case PARM_DECL:
6452 if (b->id)
6454 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6455 I_SYMBOL_BINDING (b->id) = b->shadowed;
6458 /* Check for forward decls that never got their actual decl. */
6459 if (TREE_ASM_WRITTEN (decl))
6460 error ("parameter %q+D has just a forward declaration", decl);
6461 /* Check for (..., void, ...) and issue an error. */
6462 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6464 if (!gave_void_only_once_err)
6466 error ("%<void%> must be the only parameter");
6467 gave_void_only_once_err = true;
6470 else
6472 /* Valid parameter, add it to the list. */
6473 DECL_CHAIN (decl) = parms;
6474 parms = decl;
6476 /* Since there is a prototype, args are passed in their
6477 declared types. The back end may override this later. */
6478 DECL_ARG_TYPE (decl) = type;
6479 types = tree_cons (0, type, types);
6481 break;
6483 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6484 case UNION_TYPE: keyword = "union"; goto tag;
6485 case RECORD_TYPE: keyword = "struct"; goto tag;
6486 tag:
6487 /* Types may not have tag-names, in which case the type
6488 appears in the bindings list with b->id NULL. */
6489 if (b->id)
6491 gcc_assert (I_TAG_BINDING (b->id) == b);
6492 I_TAG_BINDING (b->id) = b->shadowed;
6495 /* Warn about any struct, union or enum tags defined in a
6496 parameter list. The scope of such types is limited to
6497 the parameter list, which is rarely if ever desirable
6498 (it's impossible to call such a function with type-
6499 correct arguments). An anonymous union parm type is
6500 meaningful as a GNU extension, so don't warn for that. */
6501 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6503 if (b->id)
6504 /* The %s will be one of 'struct', 'union', or 'enum'. */
6505 warning (0, "%<%s %E%> declared inside parameter list",
6506 keyword, b->id);
6507 else
6508 /* The %s will be one of 'struct', 'union', or 'enum'. */
6509 warning (0, "anonymous %s declared inside parameter list",
6510 keyword);
6512 if (!explained_incomplete_types)
6514 warning (0, "its scope is only this definition or declaration,"
6515 " which is probably not what you want");
6516 explained_incomplete_types = true;
6520 tag = VEC_safe_push (c_arg_tag, gc, tags, NULL);
6521 tag->id = b->id;
6522 tag->type = decl;
6523 break;
6525 case CONST_DECL:
6526 case TYPE_DECL:
6527 case FUNCTION_DECL:
6528 /* CONST_DECLs appear here when we have an embedded enum,
6529 and TYPE_DECLs appear here when we have an embedded struct
6530 or union. No warnings for this - we already warned about the
6531 type itself. FUNCTION_DECLs appear when there is an implicit
6532 function declaration in the parameter list. */
6534 /* When we reinsert this decl in the function body, we need
6535 to reconstruct whether it was marked as nested. */
6536 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6537 ? b->nested
6538 : !b->nested);
6539 DECL_CHAIN (decl) = others;
6540 others = decl;
6541 /* fall through */
6543 case ERROR_MARK:
6544 /* error_mark_node appears here when we have an undeclared
6545 variable. Just throw it away. */
6546 if (b->id)
6548 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6549 I_SYMBOL_BINDING (b->id) = b->shadowed;
6551 break;
6553 /* Other things that might be encountered. */
6554 case LABEL_DECL:
6555 case VAR_DECL:
6556 default:
6557 gcc_unreachable ();
6560 b = free_binding_and_advance (b);
6563 arg_info->parms = parms;
6564 arg_info->tags = tags;
6565 arg_info->types = types;
6566 arg_info->others = others;
6567 arg_info->pending_sizes = expr;
6568 return arg_info;
6571 /* Get the struct, enum or union (CODE says which) with tag NAME.
6572 Define the tag as a forward-reference with location LOC if it is
6573 not defined. Return a c_typespec structure for the type
6574 specifier. */
6576 struct c_typespec
6577 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6579 struct c_typespec ret;
6580 tree ref;
6581 location_t refloc;
6583 ret.expr = NULL_TREE;
6584 ret.expr_const_operands = true;
6586 /* If a cross reference is requested, look up the type
6587 already defined for this tag and return it. */
6589 ref = lookup_tag (code, name, 0, &refloc);
6590 /* If this is the right type of tag, return what we found.
6591 (This reference will be shadowed by shadow_tag later if appropriate.)
6592 If this is the wrong type of tag, do not return it. If it was the
6593 wrong type in the same scope, we will have had an error
6594 message already; if in a different scope and declaring
6595 a name, pending_xref_error will give an error message; but if in a
6596 different scope and not declaring a name, this tag should
6597 shadow the previous declaration of a different type of tag, and
6598 this would not work properly if we return the reference found.
6599 (For example, with "struct foo" in an outer scope, "union foo;"
6600 must shadow that tag with a new one of union type.) */
6601 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6602 if (ref && TREE_CODE (ref) == code)
6604 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6605 && loc != UNKNOWN_LOCATION
6606 && warn_cxx_compat)
6608 switch (code)
6610 case ENUMERAL_TYPE:
6611 warning_at (loc, OPT_Wc___compat,
6612 ("enum type defined in struct or union "
6613 "is not visible in C++"));
6614 inform (refloc, "enum type defined here");
6615 break;
6616 case RECORD_TYPE:
6617 warning_at (loc, OPT_Wc___compat,
6618 ("struct defined in struct or union "
6619 "is not visible in C++"));
6620 inform (refloc, "struct defined here");
6621 break;
6622 case UNION_TYPE:
6623 warning_at (loc, OPT_Wc___compat,
6624 ("union defined in struct or union "
6625 "is not visible in C++"));
6626 inform (refloc, "union defined here");
6627 break;
6628 default:
6629 gcc_unreachable();
6633 ret.spec = ref;
6634 return ret;
6637 /* If no such tag is yet defined, create a forward-reference node
6638 and record it as the "definition".
6639 When a real declaration of this type is found,
6640 the forward-reference will be altered into a real type. */
6642 ref = make_node (code);
6643 if (code == ENUMERAL_TYPE)
6645 /* Give the type a default layout like unsigned int
6646 to avoid crashing if it does not get defined. */
6647 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6648 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6649 TYPE_USER_ALIGN (ref) = 0;
6650 TYPE_UNSIGNED (ref) = 1;
6651 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6652 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6653 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6656 pushtag (loc, name, ref);
6658 ret.spec = ref;
6659 return ret;
6662 /* Get the struct, enum or union (CODE says which) with tag NAME.
6663 Define the tag as a forward-reference if it is not defined.
6664 Return a tree for the type. */
6666 tree
6667 xref_tag (enum tree_code code, tree name)
6669 return parser_xref_tag (input_location, code, name).spec;
6672 /* Make sure that the tag NAME is defined *in the current scope*
6673 at least as a forward reference.
6674 LOC is the location of the struct's definition.
6675 CODE says which kind of tag NAME ought to be.
6677 This stores the current value of the file static STRUCT_PARSE_INFO
6678 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6679 new c_struct_parse_info structure. The old value of
6680 STRUCT_PARSE_INFO is restored in finish_struct. */
6682 tree
6683 start_struct (location_t loc, enum tree_code code, tree name,
6684 struct c_struct_parse_info **enclosing_struct_parse_info)
6686 /* If there is already a tag defined at this scope
6687 (as a forward reference), just return it. */
6689 tree ref = NULL_TREE;
6690 location_t refloc = UNKNOWN_LOCATION;
6692 if (name != NULL_TREE)
6693 ref = lookup_tag (code, name, 1, &refloc);
6694 if (ref && TREE_CODE (ref) == code)
6696 if (TYPE_SIZE (ref))
6698 if (code == UNION_TYPE)
6699 error_at (loc, "redefinition of %<union %E%>", name);
6700 else
6701 error_at (loc, "redefinition of %<struct %E%>", name);
6702 if (refloc != UNKNOWN_LOCATION)
6703 inform (refloc, "originally defined here");
6704 /* Don't create structures using a name already in use. */
6705 ref = NULL_TREE;
6707 else if (C_TYPE_BEING_DEFINED (ref))
6709 if (code == UNION_TYPE)
6710 error_at (loc, "nested redefinition of %<union %E%>", name);
6711 else
6712 error_at (loc, "nested redefinition of %<struct %E%>", name);
6713 /* Don't bother to report "originally defined here" for a
6714 nested redefinition; the original definition should be
6715 obvious. */
6716 /* Don't create structures that contain themselves. */
6717 ref = NULL_TREE;
6721 /* Otherwise create a forward-reference just so the tag is in scope. */
6723 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6725 ref = make_node (code);
6726 pushtag (loc, name, ref);
6729 C_TYPE_BEING_DEFINED (ref) = 1;
6730 TYPE_PACKED (ref) = flag_pack_struct;
6732 *enclosing_struct_parse_info = struct_parse_info;
6733 struct_parse_info = XNEW (struct c_struct_parse_info);
6734 struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6735 struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6736 struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6738 /* FIXME: This will issue a warning for a use of a type defined
6739 within a statement expr used within sizeof, et. al. This is not
6740 terribly serious as C++ doesn't permit statement exprs within
6741 sizeof anyhow. */
6742 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6743 warning_at (loc, OPT_Wc___compat,
6744 "defining type in %qs expression is invalid in C++",
6745 (in_sizeof
6746 ? "sizeof"
6747 : (in_typeof ? "typeof" : "alignof")));
6749 return ref;
6752 /* Process the specs, declarator and width (NULL if omitted)
6753 of a structure component, returning a FIELD_DECL node.
6754 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6755 DECL_ATTRS is as for grokdeclarator.
6757 LOC is the location of the structure component.
6759 This is done during the parsing of the struct declaration.
6760 The FIELD_DECL nodes are chained together and the lot of them
6761 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6763 tree
6764 grokfield (location_t loc,
6765 struct c_declarator *declarator, struct c_declspecs *declspecs,
6766 tree width, tree *decl_attrs)
6768 tree value;
6770 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6771 && width == NULL_TREE)
6773 /* This is an unnamed decl.
6775 If we have something of the form "union { list } ;" then this
6776 is the anonymous union extension. Similarly for struct.
6778 If this is something of the form "struct foo;", then
6779 If MS or Plan 9 extensions are enabled, this is handled as
6780 an anonymous struct.
6781 Otherwise this is a forward declaration of a structure tag.
6783 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6784 If foo names a structure or union without a tag, then this
6785 is an anonymous struct (this is permitted by C11).
6786 If MS or Plan 9 extensions are enabled and foo names a
6787 structure, then again this is an anonymous struct.
6788 Otherwise this is an error.
6790 Oh what a horrid tangled web we weave. I wonder if MS consciously
6791 took this from Plan 9 or if it was an accident of implementation
6792 that took root before someone noticed the bug... */
6794 tree type = declspecs->type;
6795 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6796 || TREE_CODE (type) == UNION_TYPE);
6797 bool ok = false;
6799 if (type_ok
6800 && (flag_ms_extensions
6801 || flag_plan9_extensions
6802 || !declspecs->typedef_p))
6804 if (flag_ms_extensions || flag_plan9_extensions)
6805 ok = true;
6806 else if (TYPE_NAME (type) == NULL)
6807 ok = true;
6808 else
6809 ok = false;
6811 if (!ok)
6813 pedwarn (loc, 0, "declaration does not declare anything");
6814 return NULL_TREE;
6816 if (!flag_isoc11)
6818 if (flag_isoc99)
6819 pedwarn (loc, OPT_pedantic,
6820 "ISO C99 doesn%'t support unnamed structs/unions");
6821 else
6822 pedwarn (loc, OPT_pedantic,
6823 "ISO C90 doesn%'t support unnamed structs/unions");
6827 value = grokdeclarator (declarator, declspecs, FIELD, false,
6828 width ? &width : NULL, decl_attrs, NULL, NULL,
6829 DEPRECATED_NORMAL);
6831 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6832 DECL_INITIAL (value) = width;
6834 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6836 /* If we currently have a binding for this field, set the
6837 in_struct field in the binding, so that we warn about lookups
6838 which find it. */
6839 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6840 if (b != NULL)
6842 /* If the in_struct field is not yet set, push it on a list
6843 to be cleared when this struct is finished. */
6844 if (!b->in_struct)
6846 VEC_safe_push (c_binding_ptr, heap,
6847 struct_parse_info->fields, b);
6848 b->in_struct = 1;
6853 return value;
6856 /* Subroutine of detect_field_duplicates: return whether X and Y,
6857 which are both fields in the same struct, have duplicate field
6858 names. */
6860 static bool
6861 is_duplicate_field (tree x, tree y)
6863 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
6864 return true;
6866 /* When using -fplan9-extensions, an anonymous field whose name is a
6867 typedef can duplicate a field name. */
6868 if (flag_plan9_extensions
6869 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
6871 tree xt, xn, yt, yn;
6873 xt = TREE_TYPE (x);
6874 if (DECL_NAME (x) != NULL_TREE)
6875 xn = DECL_NAME (x);
6876 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
6877 && TYPE_NAME (xt) != NULL_TREE
6878 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
6879 xn = DECL_NAME (TYPE_NAME (xt));
6880 else
6881 xn = NULL_TREE;
6883 yt = TREE_TYPE (y);
6884 if (DECL_NAME (y) != NULL_TREE)
6885 yn = DECL_NAME (y);
6886 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
6887 && TYPE_NAME (yt) != NULL_TREE
6888 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
6889 yn = DECL_NAME (TYPE_NAME (yt));
6890 else
6891 yn = NULL_TREE;
6893 if (xn != NULL_TREE && xn == yn)
6894 return true;
6897 return false;
6900 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6901 to HTAB, giving errors for any duplicates. */
6903 static void
6904 detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6906 tree x, y;
6907 void **slot;
6909 for (x = fieldlist; x ; x = DECL_CHAIN (x))
6910 if ((y = DECL_NAME (x)) != 0)
6912 slot = htab_find_slot (htab, y, INSERT);
6913 if (*slot)
6915 error ("duplicate member %q+D", x);
6916 DECL_NAME (x) = NULL_TREE;
6918 *slot = y;
6920 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6921 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6923 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6925 /* When using -fplan9-extensions, an anonymous field whose
6926 name is a typedef can duplicate a field name. */
6927 if (flag_plan9_extensions
6928 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6929 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
6931 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
6932 slot = htab_find_slot (htab, xn, INSERT);
6933 if (*slot)
6934 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
6935 *slot = xn;
6940 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6941 the list such that this does not present a problem later. */
6943 static void
6944 detect_field_duplicates (tree fieldlist)
6946 tree x, y;
6947 int timeout = 10;
6949 /* If the struct is the list of instance variables of an Objective-C
6950 class, then we need to check all the instance variables of
6951 superclasses when checking for duplicates (since you can't have
6952 an instance variable in a subclass with the same name as an
6953 instance variable in a superclass). We pass on this job to the
6954 Objective-C compiler. objc_detect_field_duplicates() will return
6955 false if we are not checking the list of instance variables and
6956 the C frontend should proceed with the standard field duplicate
6957 checks. If we are checking the list of instance variables, the
6958 ObjC frontend will do the check, emit the errors if needed, and
6959 then return true. */
6960 if (c_dialect_objc ())
6961 if (objc_detect_field_duplicates (false))
6962 return;
6964 /* First, see if there are more than "a few" fields.
6965 This is trivially true if there are zero or one fields. */
6966 if (!fieldlist || !DECL_CHAIN (fieldlist))
6967 return;
6968 x = fieldlist;
6969 do {
6970 timeout--;
6971 if (DECL_NAME (x) == NULL_TREE
6972 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6973 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6974 timeout = 0;
6975 x = DECL_CHAIN (x);
6976 } while (timeout > 0 && x);
6978 /* If there were "few" fields and no anonymous structures or unions,
6979 avoid the overhead of allocating a hash table. Instead just do
6980 the nested traversal thing. */
6981 if (timeout > 0)
6983 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
6984 /* When using -fplan9-extensions, we can have duplicates
6985 between typedef names and fields. */
6986 if (DECL_NAME (x)
6987 || (flag_plan9_extensions
6988 && DECL_NAME (x) == NULL_TREE
6989 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6990 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6991 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6992 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
6994 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6995 if (is_duplicate_field (y, x))
6997 error ("duplicate member %q+D", x);
6998 DECL_NAME (x) = NULL_TREE;
7002 else
7004 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
7006 detect_field_duplicates_hash (fieldlist, htab);
7007 htab_delete (htab);
7011 /* Finish up struct info used by -Wc++-compat. */
7013 static void
7014 warn_cxx_compat_finish_struct (tree fieldlist)
7016 unsigned int ix;
7017 tree x;
7018 struct c_binding *b;
7020 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7021 the current struct. We do this now at the end of the struct
7022 because the flag is used to issue visibility warnings, and we
7023 only want to issue those warnings if the type is referenced
7024 outside of the struct declaration. */
7025 FOR_EACH_VEC_ELT (tree, struct_parse_info->struct_types, ix, x)
7026 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7028 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7029 typedefs used when declaring fields in this struct. If the name
7030 of any of the fields is also a typedef name then the struct would
7031 not parse in C++, because the C++ lookup rules say that the
7032 typedef name would be looked up in the context of the struct, and
7033 would thus be the field rather than the typedef. */
7034 if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
7035 && fieldlist != NULL_TREE)
7037 /* Use a pointer_set using the name of the typedef. We can use
7038 a pointer_set because identifiers are interned. */
7039 struct pointer_set_t *tset = pointer_set_create ();
7041 FOR_EACH_VEC_ELT (tree, struct_parse_info->typedefs_seen, ix, x)
7042 pointer_set_insert (tset, DECL_NAME (x));
7044 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7046 if (DECL_NAME (x) != NULL_TREE
7047 && pointer_set_contains (tset, DECL_NAME (x)))
7049 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7050 ("using %qD as both field and typedef name is "
7051 "invalid in C++"),
7053 /* FIXME: It would be nice to report the location where
7054 the typedef name is used. */
7058 pointer_set_destroy (tset);
7061 /* For each field which has a binding and which was not defined in
7062 an enclosing struct, clear the in_struct field. */
7063 FOR_EACH_VEC_ELT (c_binding_ptr, struct_parse_info->fields, ix, b)
7064 b->in_struct = 0;
7067 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7068 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7069 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7070 ATTRIBUTES are attributes to be applied to the structure.
7072 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7073 the struct was started. */
7075 tree
7076 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7077 struct c_struct_parse_info *enclosing_struct_parse_info)
7079 tree x;
7080 bool toplevel = file_scope == current_scope;
7081 int saw_named_field;
7083 /* If this type was previously laid out as a forward reference,
7084 make sure we lay it out again. */
7086 TYPE_SIZE (t) = 0;
7088 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7090 if (pedantic)
7092 for (x = fieldlist; x; x = DECL_CHAIN (x))
7094 if (DECL_NAME (x) != 0)
7095 break;
7096 if (flag_isoc11
7097 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7098 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7099 break;
7102 if (x == 0)
7104 if (TREE_CODE (t) == UNION_TYPE)
7106 if (fieldlist)
7107 pedwarn (loc, OPT_pedantic, "union has no named members");
7108 else
7109 pedwarn (loc, OPT_pedantic, "union has no members");
7111 else
7113 if (fieldlist)
7114 pedwarn (loc, OPT_pedantic, "struct has no named members");
7115 else
7116 pedwarn (loc, OPT_pedantic, "struct has no members");
7121 /* Install struct as DECL_CONTEXT of each field decl.
7122 Also process specified field sizes, found in the DECL_INITIAL,
7123 storing 0 there after the type has been changed to precision equal
7124 to its width, rather than the precision of the specified standard
7125 type. (Correct layout requires the original type to have been preserved
7126 until now.) */
7128 saw_named_field = 0;
7129 for (x = fieldlist; x; x = DECL_CHAIN (x))
7131 if (TREE_TYPE (x) == error_mark_node)
7132 continue;
7134 DECL_CONTEXT (x) = t;
7136 /* If any field is const, the structure type is pseudo-const. */
7137 if (TREE_READONLY (x))
7138 C_TYPE_FIELDS_READONLY (t) = 1;
7139 else
7141 /* A field that is pseudo-const makes the structure likewise. */
7142 tree t1 = strip_array_types (TREE_TYPE (x));
7143 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7144 && C_TYPE_FIELDS_READONLY (t1))
7145 C_TYPE_FIELDS_READONLY (t) = 1;
7148 /* Any field that is volatile means variables of this type must be
7149 treated in some ways as volatile. */
7150 if (TREE_THIS_VOLATILE (x))
7151 C_TYPE_FIELDS_VOLATILE (t) = 1;
7153 /* Any field of nominal variable size implies structure is too. */
7154 if (C_DECL_VARIABLE_SIZE (x))
7155 C_TYPE_VARIABLE_SIZE (t) = 1;
7157 if (DECL_INITIAL (x))
7159 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
7160 DECL_SIZE (x) = bitsize_int (width);
7161 DECL_BIT_FIELD (x) = 1;
7162 SET_DECL_C_BIT_FIELD (x);
7165 if (TYPE_PACKED (t)
7166 && (DECL_BIT_FIELD (x)
7167 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7168 DECL_PACKED (x) = 1;
7170 /* Detect flexible array member in an invalid context. */
7171 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7172 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7173 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7174 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7176 if (TREE_CODE (t) == UNION_TYPE)
7178 error_at (DECL_SOURCE_LOCATION (x),
7179 "flexible array member in union");
7180 TREE_TYPE (x) = error_mark_node;
7182 else if (DECL_CHAIN (x) != NULL_TREE)
7184 error_at (DECL_SOURCE_LOCATION (x),
7185 "flexible array member not at end of struct");
7186 TREE_TYPE (x) = error_mark_node;
7188 else if (!saw_named_field)
7190 error_at (DECL_SOURCE_LOCATION (x),
7191 "flexible array member in otherwise empty struct");
7192 TREE_TYPE (x) = error_mark_node;
7196 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7197 && flexible_array_type_p (TREE_TYPE (x)))
7198 pedwarn (DECL_SOURCE_LOCATION (x), OPT_pedantic,
7199 "invalid use of structure with flexible array member");
7201 if (DECL_NAME (x)
7202 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7203 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7204 saw_named_field = 1;
7207 detect_field_duplicates (fieldlist);
7209 /* Now we have the nearly final fieldlist. Record it,
7210 then lay out the structure or union (including the fields). */
7212 TYPE_FIELDS (t) = fieldlist;
7214 layout_type (t);
7216 /* Give bit-fields their proper types. */
7218 tree *fieldlistp = &fieldlist;
7219 while (*fieldlistp)
7220 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7221 && TREE_TYPE (*fieldlistp) != error_mark_node)
7223 unsigned HOST_WIDE_INT width
7224 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
7225 tree type = TREE_TYPE (*fieldlistp);
7226 if (width != TYPE_PRECISION (type))
7228 TREE_TYPE (*fieldlistp)
7229 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7230 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7232 DECL_INITIAL (*fieldlistp) = 0;
7234 else
7235 fieldlistp = &DECL_CHAIN (*fieldlistp);
7238 /* Now we have the truly final field list.
7239 Store it in this type and in the variants. */
7241 TYPE_FIELDS (t) = fieldlist;
7243 /* If there are lots of fields, sort so we can look through them fast.
7244 We arbitrarily consider 16 or more elts to be "a lot". */
7247 int len = 0;
7249 for (x = fieldlist; x; x = DECL_CHAIN (x))
7251 if (len > 15 || DECL_NAME (x) == NULL)
7252 break;
7253 len += 1;
7256 if (len > 15)
7258 tree *field_array;
7259 struct lang_type *space;
7260 struct sorted_fields_type *space2;
7262 len += list_length (x);
7264 /* Use the same allocation policy here that make_node uses, to
7265 ensure that this lives as long as the rest of the struct decl.
7266 All decls in an inline function need to be saved. */
7268 space = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7269 space2 = ggc_alloc_sorted_fields_type
7270 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7272 len = 0;
7273 space->s = space2;
7274 field_array = &space2->elts[0];
7275 for (x = fieldlist; x; x = DECL_CHAIN (x))
7277 field_array[len++] = x;
7279 /* If there is anonymous struct or union, break out of the loop. */
7280 if (DECL_NAME (x) == NULL)
7281 break;
7283 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7284 if (x == NULL)
7286 TYPE_LANG_SPECIFIC (t) = space;
7287 TYPE_LANG_SPECIFIC (t)->s->len = len;
7288 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7289 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7294 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7296 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7297 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7298 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7299 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7300 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7303 /* If this was supposed to be a transparent union, but we can't
7304 make it one, warn and turn off the flag. */
7305 if (TREE_CODE (t) == UNION_TYPE
7306 && TYPE_TRANSPARENT_AGGR (t)
7307 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7309 TYPE_TRANSPARENT_AGGR (t) = 0;
7310 warning_at (loc, 0, "union cannot be made transparent");
7313 /* If this structure or union completes the type of any previous
7314 variable declaration, lay it out and output its rtl. */
7315 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7317 x = TREE_CHAIN (x))
7319 tree decl = TREE_VALUE (x);
7320 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7321 layout_array_type (TREE_TYPE (decl));
7322 if (TREE_CODE (decl) != TYPE_DECL)
7324 layout_decl (decl, 0);
7325 if (c_dialect_objc ())
7326 objc_check_decl (decl);
7327 rest_of_decl_compilation (decl, toplevel, 0);
7328 if (!toplevel)
7329 expand_decl (decl);
7332 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7334 /* Update type location to the one of the definition, instead of e.g.
7335 a forward declaration. */
7336 if (TYPE_STUB_DECL (t))
7337 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7339 /* Finish debugging output for this type. */
7340 rest_of_type_compilation (t, toplevel);
7342 /* If we're inside a function proper, i.e. not file-scope and not still
7343 parsing parameters, then arrange for the size of a variable sized type
7344 to be bound now. */
7345 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7346 add_stmt (build_stmt (loc,
7347 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7349 if (warn_cxx_compat)
7350 warn_cxx_compat_finish_struct (fieldlist);
7352 VEC_free (tree, heap, struct_parse_info->struct_types);
7353 VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7354 VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7355 XDELETE (struct_parse_info);
7357 struct_parse_info = enclosing_struct_parse_info;
7359 /* If this struct is defined inside a struct, add it to
7360 struct_types. */
7361 if (warn_cxx_compat
7362 && struct_parse_info != NULL
7363 && !in_sizeof && !in_typeof && !in_alignof)
7364 VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7366 return t;
7369 /* Lay out the type T, and its element type, and so on. */
7371 static void
7372 layout_array_type (tree t)
7374 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7375 layout_array_type (TREE_TYPE (t));
7376 layout_type (t);
7379 /* Begin compiling the definition of an enumeration type.
7380 NAME is its name (or null if anonymous).
7381 LOC is the enum's location.
7382 Returns the type object, as yet incomplete.
7383 Also records info about it so that build_enumerator
7384 may be used to declare the individual values as they are read. */
7386 tree
7387 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7389 tree enumtype = NULL_TREE;
7390 location_t enumloc = UNKNOWN_LOCATION;
7392 /* If this is the real definition for a previous forward reference,
7393 fill in the contents in the same object that used to be the
7394 forward reference. */
7396 if (name != NULL_TREE)
7397 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7399 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7401 enumtype = make_node (ENUMERAL_TYPE);
7402 pushtag (loc, name, enumtype);
7405 if (C_TYPE_BEING_DEFINED (enumtype))
7406 error_at (loc, "nested redefinition of %<enum %E%>", name);
7408 C_TYPE_BEING_DEFINED (enumtype) = 1;
7410 if (TYPE_VALUES (enumtype) != 0)
7412 /* This enum is a named one that has been declared already. */
7413 error_at (loc, "redeclaration of %<enum %E%>", name);
7414 if (enumloc != UNKNOWN_LOCATION)
7415 inform (enumloc, "originally defined here");
7417 /* Completely replace its old definition.
7418 The old enumerators remain defined, however. */
7419 TYPE_VALUES (enumtype) = 0;
7422 the_enum->enum_next_value = integer_zero_node;
7423 the_enum->enum_overflow = 0;
7425 if (flag_short_enums)
7426 TYPE_PACKED (enumtype) = 1;
7428 /* FIXME: This will issue a warning for a use of a type defined
7429 within sizeof in a statement expr. This is not terribly serious
7430 as C++ doesn't permit statement exprs within sizeof anyhow. */
7431 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7432 warning_at (loc, OPT_Wc___compat,
7433 "defining type in %qs expression is invalid in C++",
7434 (in_sizeof
7435 ? "sizeof"
7436 : (in_typeof ? "typeof" : "alignof")));
7438 return enumtype;
7441 /* After processing and defining all the values of an enumeration type,
7442 install their decls in the enumeration type and finish it off.
7443 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7444 and ATTRIBUTES are the specified attributes.
7445 Returns ENUMTYPE. */
7447 tree
7448 finish_enum (tree enumtype, tree values, tree attributes)
7450 tree pair, tem;
7451 tree minnode = 0, maxnode = 0;
7452 int precision, unsign;
7453 bool toplevel = (file_scope == current_scope);
7454 struct lang_type *lt;
7456 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7458 /* Calculate the maximum value of any enumerator in this type. */
7460 if (values == error_mark_node)
7461 minnode = maxnode = integer_zero_node;
7462 else
7464 minnode = maxnode = TREE_VALUE (values);
7465 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7467 tree value = TREE_VALUE (pair);
7468 if (tree_int_cst_lt (maxnode, value))
7469 maxnode = value;
7470 if (tree_int_cst_lt (value, minnode))
7471 minnode = value;
7475 /* Construct the final type of this enumeration. It is the same
7476 as one of the integral types - the narrowest one that fits, except
7477 that normally we only go as narrow as int - and signed iff any of
7478 the values are negative. */
7479 unsign = (tree_int_cst_sgn (minnode) >= 0);
7480 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7481 tree_int_cst_min_precision (maxnode, unsign));
7483 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7485 tem = c_common_type_for_size (precision, unsign);
7486 if (tem == NULL)
7488 warning (0, "enumeration values exceed range of largest integer");
7489 tem = long_long_integer_type_node;
7492 else
7493 tem = unsign ? unsigned_type_node : integer_type_node;
7495 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7496 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7497 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7498 TYPE_SIZE (enumtype) = 0;
7500 /* If the precision of the type was specific with an attribute and it
7501 was too small, give an error. Otherwise, use it. */
7502 if (TYPE_PRECISION (enumtype))
7504 if (precision > TYPE_PRECISION (enumtype))
7505 error ("specified mode too small for enumeral values");
7507 else
7508 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7510 layout_type (enumtype);
7512 if (values != error_mark_node)
7514 /* Change the type of the enumerators to be the enum type. We
7515 need to do this irrespective of the size of the enum, for
7516 proper type checking. Replace the DECL_INITIALs of the
7517 enumerators, and the value slots of the list, with copies
7518 that have the enum type; they cannot be modified in place
7519 because they may be shared (e.g. integer_zero_node) Finally,
7520 change the purpose slots to point to the names of the decls. */
7521 for (pair = values; pair; pair = TREE_CHAIN (pair))
7523 tree enu = TREE_PURPOSE (pair);
7524 tree ini = DECL_INITIAL (enu);
7526 TREE_TYPE (enu) = enumtype;
7528 /* The ISO C Standard mandates enumerators to have type int,
7529 even though the underlying type of an enum type is
7530 unspecified. However, GCC allows enumerators of any
7531 integer type as an extensions. build_enumerator()
7532 converts any enumerators that fit in an int to type int,
7533 to avoid promotions to unsigned types when comparing
7534 integers with enumerators that fit in the int range.
7535 When -pedantic is given, build_enumerator() would have
7536 already warned about those that don't fit. Here we
7537 convert the rest to the enumerator type. */
7538 if (TREE_TYPE (ini) != integer_type_node)
7539 ini = convert (enumtype, ini);
7541 DECL_INITIAL (enu) = ini;
7542 TREE_PURPOSE (pair) = DECL_NAME (enu);
7543 TREE_VALUE (pair) = ini;
7546 TYPE_VALUES (enumtype) = values;
7549 /* Record the min/max values so that we can warn about bit-field
7550 enumerations that are too small for the values. */
7551 lt = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7552 lt->enum_min = minnode;
7553 lt->enum_max = maxnode;
7554 TYPE_LANG_SPECIFIC (enumtype) = lt;
7556 /* Fix up all variant types of this enum type. */
7557 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7559 if (tem == enumtype)
7560 continue;
7561 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7562 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7563 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7564 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7565 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7566 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7567 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7568 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7569 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7570 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7571 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7574 /* Finish debugging output for this type. */
7575 rest_of_type_compilation (enumtype, toplevel);
7577 /* If this enum is defined inside a struct, add it to
7578 struct_types. */
7579 if (warn_cxx_compat
7580 && struct_parse_info != NULL
7581 && !in_sizeof && !in_typeof && !in_alignof)
7582 VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7584 return enumtype;
7587 /* Build and install a CONST_DECL for one value of the
7588 current enumeration type (one that was begun with start_enum).
7589 DECL_LOC is the location of the enumerator.
7590 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7591 Return a tree-list containing the CONST_DECL and its value.
7592 Assignment of sequential values by default is handled here. */
7594 tree
7595 build_enumerator (location_t decl_loc, location_t loc,
7596 struct c_enum_contents *the_enum, tree name, tree value)
7598 tree decl, type;
7600 /* Validate and default VALUE. */
7602 if (value != 0)
7604 /* Don't issue more errors for error_mark_node (i.e. an
7605 undeclared identifier) - just ignore the value expression. */
7606 if (value == error_mark_node)
7607 value = 0;
7608 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7610 error_at (loc, "enumerator value for %qE is not an integer constant",
7611 name);
7612 value = 0;
7614 else
7616 if (TREE_CODE (value) != INTEGER_CST)
7618 value = c_fully_fold (value, false, NULL);
7619 if (TREE_CODE (value) == INTEGER_CST)
7620 pedwarn (loc, OPT_pedantic,
7621 "enumerator value for %qE is not an integer "
7622 "constant expression", name);
7624 if (TREE_CODE (value) != INTEGER_CST)
7626 error ("enumerator value for %qE is not an integer constant",
7627 name);
7628 value = 0;
7630 else
7632 value = default_conversion (value);
7633 constant_expression_warning (value);
7638 /* Default based on previous value. */
7639 /* It should no longer be possible to have NON_LVALUE_EXPR
7640 in the default. */
7641 if (value == 0)
7643 value = the_enum->enum_next_value;
7644 if (the_enum->enum_overflow)
7645 error_at (loc, "overflow in enumeration values");
7647 /* Even though the underlying type of an enum is unspecified, the
7648 type of enumeration constants is explicitly defined as int
7649 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7650 an extension. */
7651 else if (!int_fits_type_p (value, integer_type_node))
7652 pedwarn (loc, OPT_pedantic,
7653 "ISO C restricts enumerator values to range of %<int%>");
7655 /* The ISO C Standard mandates enumerators to have type int, even
7656 though the underlying type of an enum type is unspecified.
7657 However, GCC allows enumerators of any integer type as an
7658 extensions. Here we convert any enumerators that fit in an int
7659 to type int, to avoid promotions to unsigned types when comparing
7660 integers with enumerators that fit in the int range. When
7661 -pedantic is given, we would have already warned about those that
7662 don't fit. We have to do this here rather than in finish_enum
7663 because this value may be used to define more enumerators. */
7664 if (int_fits_type_p (value, integer_type_node))
7665 value = convert (integer_type_node, value);
7667 /* Set basis for default for next value. */
7668 the_enum->enum_next_value
7669 = build_binary_op (EXPR_LOC_OR_HERE (value),
7670 PLUS_EXPR, value, integer_one_node, 0);
7671 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7673 /* Now create a declaration for the enum value name. */
7675 type = TREE_TYPE (value);
7676 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7677 TYPE_PRECISION (integer_type_node)),
7678 (TYPE_PRECISION (type)
7679 >= TYPE_PRECISION (integer_type_node)
7680 && TYPE_UNSIGNED (type)));
7682 decl = build_decl (decl_loc, CONST_DECL, name, type);
7683 DECL_INITIAL (decl) = convert (type, value);
7684 pushdecl (decl);
7686 return tree_cons (decl, value, NULL_TREE);
7690 /* Create the FUNCTION_DECL for a function definition.
7691 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7692 the declaration; they describe the function's name and the type it returns,
7693 but twisted together in a fashion that parallels the syntax of C.
7695 This function creates a binding context for the function body
7696 as well as setting up the FUNCTION_DECL in current_function_decl.
7698 Returns 1 on success. If the DECLARATOR is not suitable for a function
7699 (it defines a datum instead), we return 0, which tells
7700 yyparse to report a parse error. */
7703 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7704 tree attributes)
7706 tree decl1, old_decl;
7707 tree restype, resdecl;
7708 location_t loc;
7710 current_function_returns_value = 0; /* Assume, until we see it does. */
7711 current_function_returns_null = 0;
7712 current_function_returns_abnormally = 0;
7713 warn_about_return_type = 0;
7714 c_switch_stack = NULL;
7716 /* Indicate no valid break/continue context by setting these variables
7717 to some non-null, non-label value. We'll notice and emit the proper
7718 error message in c_finish_bc_stmt. */
7719 c_break_label = c_cont_label = size_zero_node;
7721 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7722 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7724 /* If the declarator is not suitable for a function definition,
7725 cause a syntax error. */
7726 if (decl1 == 0
7727 || TREE_CODE (decl1) != FUNCTION_DECL)
7728 return 0;
7730 loc = DECL_SOURCE_LOCATION (decl1);
7732 decl_attributes (&decl1, attributes, 0);
7734 if (DECL_DECLARED_INLINE_P (decl1)
7735 && DECL_UNINLINABLE (decl1)
7736 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7737 warning_at (loc, OPT_Wattributes,
7738 "inline function %qD given attribute noinline",
7739 decl1);
7741 /* Handle gnu_inline attribute. */
7742 if (declspecs->inline_p
7743 && !flag_gnu89_inline
7744 && TREE_CODE (decl1) == FUNCTION_DECL
7745 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7746 || current_function_decl))
7748 if (declspecs->storage_class != csc_static)
7749 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7752 announce_function (decl1);
7754 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7756 error_at (loc, "return type is an incomplete type");
7757 /* Make it return void instead. */
7758 TREE_TYPE (decl1)
7759 = build_function_type (void_type_node,
7760 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7763 if (warn_about_return_type)
7764 pedwarn_c99 (loc, flag_isoc99 ? 0
7765 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7766 "return type defaults to %<int%>");
7768 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7769 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7770 DECL_INITIAL (decl1) = error_mark_node;
7772 /* A nested function is not global. */
7773 if (current_function_decl != 0)
7774 TREE_PUBLIC (decl1) = 0;
7776 /* If this definition isn't a prototype and we had a prototype declaration
7777 before, copy the arg type info from that prototype. */
7778 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7779 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7780 old_decl = 0;
7781 current_function_prototype_locus = UNKNOWN_LOCATION;
7782 current_function_prototype_built_in = false;
7783 current_function_prototype_arg_types = NULL_TREE;
7784 if (!prototype_p (TREE_TYPE (decl1)))
7786 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7787 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7788 TREE_TYPE (TREE_TYPE (old_decl))))
7790 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7791 TREE_TYPE (decl1));
7792 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7793 current_function_prototype_built_in
7794 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7795 current_function_prototype_arg_types
7796 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7798 if (TREE_PUBLIC (decl1))
7800 /* If there is an external prototype declaration of this
7801 function, record its location but do not copy information
7802 to this decl. This may be an invisible declaration
7803 (built-in or in a scope which has finished) or simply
7804 have more refined argument types than any declaration
7805 found above. */
7806 struct c_binding *b;
7807 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7808 if (B_IN_SCOPE (b, external_scope))
7809 break;
7810 if (b)
7812 tree ext_decl, ext_type;
7813 ext_decl = b->decl;
7814 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7815 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7816 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7817 TREE_TYPE (ext_type)))
7819 current_function_prototype_locus
7820 = DECL_SOURCE_LOCATION (ext_decl);
7821 current_function_prototype_built_in
7822 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7823 current_function_prototype_arg_types
7824 = TYPE_ARG_TYPES (ext_type);
7830 /* Optionally warn of old-fashioned def with no previous prototype. */
7831 if (warn_strict_prototypes
7832 && old_decl != error_mark_node
7833 && !prototype_p (TREE_TYPE (decl1))
7834 && C_DECL_ISNT_PROTOTYPE (old_decl))
7835 warning_at (loc, OPT_Wstrict_prototypes,
7836 "function declaration isn%'t a prototype");
7837 /* Optionally warn of any global def with no previous prototype. */
7838 else if (warn_missing_prototypes
7839 && old_decl != error_mark_node
7840 && TREE_PUBLIC (decl1)
7841 && !MAIN_NAME_P (DECL_NAME (decl1))
7842 && C_DECL_ISNT_PROTOTYPE (old_decl))
7843 warning_at (loc, OPT_Wmissing_prototypes,
7844 "no previous prototype for %qD", decl1);
7845 /* Optionally warn of any def with no previous prototype
7846 if the function has already been used. */
7847 else if (warn_missing_prototypes
7848 && old_decl != 0
7849 && old_decl != error_mark_node
7850 && TREE_USED (old_decl)
7851 && !prototype_p (TREE_TYPE (old_decl)))
7852 warning_at (loc, OPT_Wmissing_prototypes,
7853 "%qD was used with no prototype before its definition", decl1);
7854 /* Optionally warn of any global def with no previous declaration. */
7855 else if (warn_missing_declarations
7856 && TREE_PUBLIC (decl1)
7857 && old_decl == 0
7858 && !MAIN_NAME_P (DECL_NAME (decl1)))
7859 warning_at (loc, OPT_Wmissing_declarations,
7860 "no previous declaration for %qD",
7861 decl1);
7862 /* Optionally warn of any def with no previous declaration
7863 if the function has already been used. */
7864 else if (warn_missing_declarations
7865 && old_decl != 0
7866 && old_decl != error_mark_node
7867 && TREE_USED (old_decl)
7868 && C_DECL_IMPLICIT (old_decl))
7869 warning_at (loc, OPT_Wmissing_declarations,
7870 "%qD was used with no declaration before its definition", decl1);
7872 /* This function exists in static storage.
7873 (This does not mean `static' in the C sense!) */
7874 TREE_STATIC (decl1) = 1;
7876 /* This is the earliest point at which we might know the assembler
7877 name of the function. Thus, if it's set before this, die horribly. */
7878 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7880 /* If #pragma weak was used, mark the decl weak now. */
7881 if (current_scope == file_scope)
7882 maybe_apply_pragma_weak (decl1);
7884 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7885 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7887 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7888 != integer_type_node)
7889 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7891 check_main_parameter_types (decl1);
7893 if (!TREE_PUBLIC (decl1))
7894 pedwarn (loc, OPT_Wmain,
7895 "%qD is normally a non-static function", decl1);
7898 /* Record the decl so that the function name is defined.
7899 If we already have a decl for this name, and it is a FUNCTION_DECL,
7900 use the old decl. */
7902 current_function_decl = pushdecl (decl1);
7904 push_scope ();
7905 declare_parm_level ();
7907 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7908 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7909 DECL_ARTIFICIAL (resdecl) = 1;
7910 DECL_IGNORED_P (resdecl) = 1;
7911 DECL_RESULT (current_function_decl) = resdecl;
7913 start_fname_decls ();
7915 return 1;
7918 /* Subroutine of store_parm_decls which handles new-style function
7919 definitions (prototype format). The parms already have decls, so we
7920 need only record them as in effect and complain if any redundant
7921 old-style parm decls were written. */
7922 static void
7923 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7925 tree decl;
7926 c_arg_tag *tag;
7927 unsigned ix;
7929 if (current_scope->bindings)
7931 error_at (DECL_SOURCE_LOCATION (fndecl),
7932 "old-style parameter declarations in prototyped "
7933 "function definition");
7935 /* Get rid of the old-style declarations. */
7936 pop_scope ();
7937 push_scope ();
7939 /* Don't issue this warning for nested functions, and don't issue this
7940 warning if we got here because ARG_INFO_TYPES was error_mark_node
7941 (this happens when a function definition has just an ellipsis in
7942 its parameter list). */
7943 else if (!in_system_header && !current_function_scope
7944 && arg_info->types != error_mark_node)
7945 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7946 "traditional C rejects ISO C style function definitions");
7948 /* Now make all the parameter declarations visible in the function body.
7949 We can bypass most of the grunt work of pushdecl. */
7950 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
7952 DECL_CONTEXT (decl) = current_function_decl;
7953 if (DECL_NAME (decl))
7955 bind (DECL_NAME (decl), decl, current_scope,
7956 /*invisible=*/false, /*nested=*/false,
7957 UNKNOWN_LOCATION);
7958 if (!TREE_USED (decl))
7959 warn_if_shadowing (decl);
7961 else
7962 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7965 /* Record the parameter list in the function declaration. */
7966 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7968 /* Now make all the ancillary declarations visible, likewise. */
7969 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
7971 DECL_CONTEXT (decl) = current_function_decl;
7972 if (DECL_NAME (decl))
7973 bind (DECL_NAME (decl), decl, current_scope,
7974 /*invisible=*/false,
7975 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7976 UNKNOWN_LOCATION);
7979 /* And all the tag declarations. */
7980 FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
7981 if (tag->id)
7982 bind (tag->id, tag->type, current_scope,
7983 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7986 /* Subroutine of store_parm_decls which handles old-style function
7987 definitions (separate parameter list and declarations). */
7989 static void
7990 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7992 struct c_binding *b;
7993 tree parm, decl, last;
7994 tree parmids = arg_info->parms;
7995 struct pointer_set_t *seen_args = pointer_set_create ();
7997 if (!in_system_header)
7998 warning_at (DECL_SOURCE_LOCATION (fndecl),
7999 OPT_Wold_style_definition, "old-style function definition");
8001 /* Match each formal parameter name with its declaration. Save each
8002 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8003 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8005 if (TREE_VALUE (parm) == 0)
8007 error_at (DECL_SOURCE_LOCATION (fndecl),
8008 "parameter name missing from parameter list");
8009 TREE_PURPOSE (parm) = 0;
8010 continue;
8013 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8014 if (b && B_IN_CURRENT_SCOPE (b))
8016 decl = b->decl;
8017 /* Skip erroneous parameters. */
8018 if (decl == error_mark_node)
8019 continue;
8020 /* If we got something other than a PARM_DECL it is an error. */
8021 if (TREE_CODE (decl) != PARM_DECL)
8022 error_at (DECL_SOURCE_LOCATION (decl),
8023 "%qD declared as a non-parameter", decl);
8024 /* If the declaration is already marked, we have a duplicate
8025 name. Complain and ignore the duplicate. */
8026 else if (pointer_set_contains (seen_args, decl))
8028 error_at (DECL_SOURCE_LOCATION (decl),
8029 "multiple parameters named %qD", decl);
8030 TREE_PURPOSE (parm) = 0;
8031 continue;
8033 /* If the declaration says "void", complain and turn it into
8034 an int. */
8035 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8037 error_at (DECL_SOURCE_LOCATION (decl),
8038 "parameter %qD declared with void type", decl);
8039 TREE_TYPE (decl) = integer_type_node;
8040 DECL_ARG_TYPE (decl) = integer_type_node;
8041 layout_decl (decl, 0);
8043 warn_if_shadowing (decl);
8045 /* If no declaration found, default to int. */
8046 else
8048 /* FIXME diagnostics: This should be the location of the argument,
8049 not the FNDECL. E.g., for an old-style declaration
8051 int f10(v) { blah; }
8053 We should use the location of the V, not the F10.
8054 Unfortunately, the V is an IDENTIFIER_NODE which has no
8055 location. In the future we need locations for c_arg_info
8056 entries.
8058 See gcc.dg/Wshadow-3.c for an example of this problem. */
8059 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8060 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8061 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8062 pushdecl (decl);
8063 warn_if_shadowing (decl);
8065 if (flag_isoc99)
8066 pedwarn (DECL_SOURCE_LOCATION (decl),
8067 0, "type of %qD defaults to %<int%>", decl);
8068 else
8069 warning_at (DECL_SOURCE_LOCATION (decl),
8070 OPT_Wmissing_parameter_type,
8071 "type of %qD defaults to %<int%>", decl);
8074 TREE_PURPOSE (parm) = decl;
8075 pointer_set_insert (seen_args, decl);
8078 /* Now examine the parms chain for incomplete declarations
8079 and declarations with no corresponding names. */
8081 for (b = current_scope->bindings; b; b = b->prev)
8083 parm = b->decl;
8084 if (TREE_CODE (parm) != PARM_DECL)
8085 continue;
8087 if (TREE_TYPE (parm) != error_mark_node
8088 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8090 error_at (DECL_SOURCE_LOCATION (parm),
8091 "parameter %qD has incomplete type", parm);
8092 TREE_TYPE (parm) = error_mark_node;
8095 if (!pointer_set_contains (seen_args, parm))
8097 error_at (DECL_SOURCE_LOCATION (parm),
8098 "declaration for parameter %qD but no such parameter",
8099 parm);
8101 /* Pretend the parameter was not missing.
8102 This gets us to a standard state and minimizes
8103 further error messages. */
8104 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8108 /* Chain the declarations together in the order of the list of
8109 names. Store that chain in the function decl, replacing the
8110 list of names. Update the current scope to match. */
8111 DECL_ARGUMENTS (fndecl) = 0;
8113 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8114 if (TREE_PURPOSE (parm))
8115 break;
8116 if (parm && TREE_PURPOSE (parm))
8118 last = TREE_PURPOSE (parm);
8119 DECL_ARGUMENTS (fndecl) = last;
8121 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8122 if (TREE_PURPOSE (parm))
8124 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8125 last = TREE_PURPOSE (parm);
8127 DECL_CHAIN (last) = 0;
8130 pointer_set_destroy (seen_args);
8132 /* If there was a previous prototype,
8133 set the DECL_ARG_TYPE of each argument according to
8134 the type previously specified, and report any mismatches. */
8136 if (current_function_prototype_arg_types)
8138 tree type;
8139 for (parm = DECL_ARGUMENTS (fndecl),
8140 type = current_function_prototype_arg_types;
8141 parm || (type && TREE_VALUE (type) != error_mark_node
8142 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8143 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8145 if (parm == 0 || type == 0
8146 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8148 if (current_function_prototype_built_in)
8149 warning_at (DECL_SOURCE_LOCATION (fndecl),
8150 0, "number of arguments doesn%'t match "
8151 "built-in prototype");
8152 else
8154 /* FIXME diagnostics: This should be the location of
8155 FNDECL, but there is bug when a prototype is
8156 declared inside function context, but defined
8157 outside of it (e.g., gcc.dg/pr15698-2.c). In
8158 which case FNDECL gets the location of the
8159 prototype, not the definition. */
8160 error_at (input_location,
8161 "number of arguments doesn%'t match prototype");
8163 error_at (current_function_prototype_locus,
8164 "prototype declaration");
8166 break;
8168 /* Type for passing arg must be consistent with that
8169 declared for the arg. ISO C says we take the unqualified
8170 type for parameters declared with qualified type. */
8171 if (TREE_TYPE (parm) != error_mark_node
8172 && TREE_TYPE (type) != error_mark_node
8173 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8174 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8176 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8177 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
8179 /* Adjust argument to match prototype. E.g. a previous
8180 `int foo(float);' prototype causes
8181 `int foo(x) float x; {...}' to be treated like
8182 `int foo(float x) {...}'. This is particularly
8183 useful for argument types like uid_t. */
8184 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8186 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8187 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8188 && TYPE_PRECISION (TREE_TYPE (parm))
8189 < TYPE_PRECISION (integer_type_node))
8190 DECL_ARG_TYPE (parm) = integer_type_node;
8192 /* ??? Is it possible to get here with a
8193 built-in prototype or will it always have
8194 been diagnosed as conflicting with an
8195 old-style definition and discarded? */
8196 if (current_function_prototype_built_in)
8197 warning_at (DECL_SOURCE_LOCATION (parm),
8198 OPT_pedantic, "promoted argument %qD "
8199 "doesn%'t match built-in prototype", parm);
8200 else
8202 pedwarn (DECL_SOURCE_LOCATION (parm),
8203 OPT_pedantic, "promoted argument %qD "
8204 "doesn%'t match prototype", parm);
8205 pedwarn (current_function_prototype_locus, OPT_pedantic,
8206 "prototype declaration");
8209 else
8211 if (current_function_prototype_built_in)
8212 warning_at (DECL_SOURCE_LOCATION (parm),
8213 0, "argument %qD doesn%'t match "
8214 "built-in prototype", parm);
8215 else
8217 error_at (DECL_SOURCE_LOCATION (parm),
8218 "argument %qD doesn%'t match prototype", parm);
8219 error_at (current_function_prototype_locus,
8220 "prototype declaration");
8225 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8228 /* Otherwise, create a prototype that would match. */
8230 else
8232 tree actual = 0, last = 0, type;
8234 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8236 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8237 if (last)
8238 TREE_CHAIN (last) = type;
8239 else
8240 actual = type;
8241 last = type;
8243 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8244 if (last)
8245 TREE_CHAIN (last) = type;
8246 else
8247 actual = type;
8249 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8250 of the type of this function, but we need to avoid having this
8251 affect the types of other similarly-typed functions, so we must
8252 first force the generation of an identical (but separate) type
8253 node for the relevant function type. The new node we create
8254 will be a variant of the main variant of the original function
8255 type. */
8257 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8259 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8263 /* Store parameter declarations passed in ARG_INFO into the current
8264 function declaration. */
8266 void
8267 store_parm_decls_from (struct c_arg_info *arg_info)
8269 current_function_arg_info = arg_info;
8270 store_parm_decls ();
8273 /* Store the parameter declarations into the current function declaration.
8274 This is called after parsing the parameter declarations, before
8275 digesting the body of the function.
8277 For an old-style definition, construct a prototype out of the old-style
8278 parameter declarations and inject it into the function's type. */
8280 void
8281 store_parm_decls (void)
8283 tree fndecl = current_function_decl;
8284 bool proto;
8286 /* The argument information block for FNDECL. */
8287 struct c_arg_info *arg_info = current_function_arg_info;
8288 current_function_arg_info = 0;
8290 /* True if this definition is written with a prototype. Note:
8291 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8292 list in a function definition as equivalent to (void) -- an
8293 empty argument list specifies the function has no parameters,
8294 but only (void) sets up a prototype for future calls. */
8295 proto = arg_info->types != 0;
8297 if (proto)
8298 store_parm_decls_newstyle (fndecl, arg_info);
8299 else
8300 store_parm_decls_oldstyle (fndecl, arg_info);
8302 /* The next call to push_scope will be a function body. */
8304 next_is_function_body = true;
8306 /* Write a record describing this function definition to the prototypes
8307 file (if requested). */
8309 gen_aux_info_record (fndecl, 1, 0, proto);
8311 /* Initialize the RTL code for the function. */
8312 allocate_struct_function (fndecl, false);
8314 if (warn_unused_local_typedefs)
8315 cfun->language = ggc_alloc_cleared_language_function ();
8317 /* Begin the statement tree for this function. */
8318 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8320 /* ??? Insert the contents of the pending sizes list into the function
8321 to be evaluated. The only reason left to have this is
8322 void foo(int n, int array[n++])
8323 because we throw away the array type in favor of a pointer type, and
8324 thus won't naturally see the SAVE_EXPR containing the increment. All
8325 other pending sizes would be handled by gimplify_parameters. */
8326 if (arg_info->pending_sizes)
8327 add_stmt (arg_info->pending_sizes);
8331 /* Finish up a function declaration and compile that function
8332 all the way to assembler language output. Then free the storage
8333 for the function definition.
8335 This is called after parsing the body of the function definition. */
8337 void
8338 finish_function (void)
8340 tree fndecl = current_function_decl;
8342 if (c_dialect_objc ())
8343 objc_finish_function ();
8345 if (TREE_CODE (fndecl) == FUNCTION_DECL
8346 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8348 tree args = DECL_ARGUMENTS (fndecl);
8349 for (; args; args = DECL_CHAIN (args))
8351 tree type = TREE_TYPE (args);
8352 if (INTEGRAL_TYPE_P (type)
8353 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8354 DECL_ARG_TYPE (args) = integer_type_node;
8358 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8359 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8361 /* Must mark the RESULT_DECL as being in this function. */
8363 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8364 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8366 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8367 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8368 == integer_type_node && flag_isoc99)
8370 /* Hack. We don't want the middle-end to warn that this return
8371 is unreachable, so we mark its location as special. Using
8372 UNKNOWN_LOCATION has the problem that it gets clobbered in
8373 annotate_one_with_locus. A cleaner solution might be to
8374 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8376 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8379 /* Tie off the statement tree for this function. */
8380 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8382 finish_fname_decls ();
8384 /* Complain if there's just no return statement. */
8385 if (warn_return_type
8386 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8387 && !current_function_returns_value && !current_function_returns_null
8388 /* Don't complain if we are no-return. */
8389 && !current_function_returns_abnormally
8390 /* Don't complain if we are declared noreturn. */
8391 && !TREE_THIS_VOLATILE (fndecl)
8392 /* Don't warn for main(). */
8393 && !MAIN_NAME_P (DECL_NAME (fndecl))
8394 /* Or if they didn't actually specify a return type. */
8395 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8396 /* Normally, with -Wreturn-type, flow will complain, but we might
8397 optimize out static functions. */
8398 && !TREE_PUBLIC (fndecl))
8400 warning (OPT_Wreturn_type,
8401 "no return statement in function returning non-void");
8402 TREE_NO_WARNING (fndecl) = 1;
8405 /* Complain about parameters that are only set, but never otherwise used. */
8406 if (warn_unused_but_set_parameter)
8408 tree decl;
8410 for (decl = DECL_ARGUMENTS (fndecl);
8411 decl;
8412 decl = DECL_CHAIN (decl))
8413 if (TREE_USED (decl)
8414 && TREE_CODE (decl) == PARM_DECL
8415 && !DECL_READ_P (decl)
8416 && DECL_NAME (decl)
8417 && !DECL_ARTIFICIAL (decl)
8418 && !TREE_NO_WARNING (decl))
8419 warning_at (DECL_SOURCE_LOCATION (decl),
8420 OPT_Wunused_but_set_parameter,
8421 "parameter %qD set but not used", decl);
8424 /* Complain about locally defined typedefs that are not used in this
8425 function. */
8426 maybe_warn_unused_local_typedefs ();
8428 /* Store the end of the function, so that we get good line number
8429 info for the epilogue. */
8430 cfun->function_end_locus = input_location;
8432 /* Finalize the ELF visibility for the function. */
8433 c_determine_visibility (fndecl);
8435 /* For GNU C extern inline functions disregard inline limits. */
8436 if (DECL_EXTERNAL (fndecl)
8437 && DECL_DECLARED_INLINE_P (fndecl))
8438 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8440 /* Genericize before inlining. Delay genericizing nested functions
8441 until their parent function is genericized. Since finalizing
8442 requires GENERIC, delay that as well. */
8444 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8445 && !undef_nested_function)
8447 if (!decl_function_context (fndecl))
8449 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8450 c_genericize (fndecl);
8452 /* ??? Objc emits functions after finalizing the compilation unit.
8453 This should be cleaned up later and this conditional removed. */
8454 if (cgraph_global_info_ready)
8456 cgraph_add_new_function (fndecl, false);
8457 return;
8459 cgraph_finalize_function (fndecl, false);
8461 else
8463 /* Register this function with cgraph just far enough to get it
8464 added to our parent's nested function list. Handy, since the
8465 C front end doesn't have such a list. */
8466 (void) cgraph_get_create_node (fndecl);
8470 if (!decl_function_context (fndecl))
8471 undef_nested_function = false;
8473 if (cfun->language != NULL)
8475 ggc_free (cfun->language);
8476 cfun->language = NULL;
8479 /* We're leaving the context of this function, so zap cfun.
8480 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8481 tree_rest_of_compilation. */
8482 set_cfun (NULL);
8483 current_function_decl = NULL;
8486 /* Check the declarations given in a for-loop for satisfying the C99
8487 constraints. If exactly one such decl is found, return it. LOC is
8488 the location of the opening parenthesis of the for loop. The last
8489 parameter allows you to control the "for loop initial declarations
8490 are only allowed in C99 mode". Normally, you should pass
8491 flag_isoc99 as that parameter. But in some cases (Objective-C
8492 foreach loop, for example) we want to run the checks in this
8493 function even if not in C99 mode, so we allow the caller to turn
8494 off the error about not being in C99 mode.
8497 tree
8498 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8500 struct c_binding *b;
8501 tree one_decl = NULL_TREE;
8502 int n_decls = 0;
8504 if (!turn_off_iso_c99_error)
8506 static bool hint = true;
8507 /* If we get here, declarations have been used in a for loop without
8508 the C99 for loop scope. This doesn't make much sense, so don't
8509 allow it. */
8510 error_at (loc, "%<for%> loop initial declarations "
8511 "are only allowed in C99 mode");
8512 if (hint)
8514 inform (loc,
8515 "use option -std=c99 or -std=gnu99 to compile your code");
8516 hint = false;
8518 return NULL_TREE;
8520 /* C99 subclause 6.8.5 paragraph 3:
8522 [#3] The declaration part of a for statement shall only
8523 declare identifiers for objects having storage class auto or
8524 register.
8526 It isn't clear whether, in this sentence, "identifiers" binds to
8527 "shall only declare" or to "objects" - that is, whether all identifiers
8528 declared must be identifiers for objects, or whether the restriction
8529 only applies to those that are. (A question on this in comp.std.c
8530 in November 2000 received no answer.) We implement the strictest
8531 interpretation, to avoid creating an extension which later causes
8532 problems. */
8534 for (b = current_scope->bindings; b; b = b->prev)
8536 tree id = b->id;
8537 tree decl = b->decl;
8539 if (!id)
8540 continue;
8542 switch (TREE_CODE (decl))
8544 case VAR_DECL:
8546 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8547 if (TREE_STATIC (decl))
8548 error_at (decl_loc,
8549 "declaration of static variable %qD in %<for%> loop "
8550 "initial declaration", decl);
8551 else if (DECL_EXTERNAL (decl))
8552 error_at (decl_loc,
8553 "declaration of %<extern%> variable %qD in %<for%> loop "
8554 "initial declaration", decl);
8556 break;
8558 case RECORD_TYPE:
8559 error_at (loc,
8560 "%<struct %E%> declared in %<for%> loop initial "
8561 "declaration", id);
8562 break;
8563 case UNION_TYPE:
8564 error_at (loc,
8565 "%<union %E%> declared in %<for%> loop initial declaration",
8566 id);
8567 break;
8568 case ENUMERAL_TYPE:
8569 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8570 "initial declaration", id);
8571 break;
8572 default:
8573 error_at (loc, "declaration of non-variable "
8574 "%qD in %<for%> loop initial declaration", decl);
8577 n_decls++;
8578 one_decl = decl;
8581 return n_decls == 1 ? one_decl : NULL_TREE;
8584 /* Save and reinitialize the variables
8585 used during compilation of a C function. */
8587 void
8588 c_push_function_context (void)
8590 struct language_function *p = cfun->language;
8591 /* cfun->language might have been already allocated by the use of
8592 -Wunused-local-typedefs. In that case, just re-use it. */
8593 if (p == NULL)
8594 cfun->language = p = ggc_alloc_cleared_language_function ();
8596 p->base.x_stmt_tree = c_stmt_tree;
8597 c_stmt_tree.x_cur_stmt_list
8598 = VEC_copy (tree, gc, c_stmt_tree.x_cur_stmt_list);
8599 p->x_break_label = c_break_label;
8600 p->x_cont_label = c_cont_label;
8601 p->x_switch_stack = c_switch_stack;
8602 p->arg_info = current_function_arg_info;
8603 p->returns_value = current_function_returns_value;
8604 p->returns_null = current_function_returns_null;
8605 p->returns_abnormally = current_function_returns_abnormally;
8606 p->warn_about_return_type = warn_about_return_type;
8608 push_function_context ();
8611 /* Restore the variables used during compilation of a C function. */
8613 void
8614 c_pop_function_context (void)
8616 struct language_function *p;
8618 pop_function_context ();
8619 p = cfun->language;
8620 /* When -Wunused-local-typedefs is in effect, cfun->languages is
8621 used to store data throughout the life time of the current cfun,
8622 So don't deallocate it. */
8623 if (!warn_unused_local_typedefs)
8624 cfun->language = NULL;
8626 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8627 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8629 /* Stop pointing to the local nodes about to be freed. */
8630 /* But DECL_INITIAL must remain nonzero so we know this
8631 was an actual function definition. */
8632 DECL_INITIAL (current_function_decl) = error_mark_node;
8633 DECL_ARGUMENTS (current_function_decl) = 0;
8636 c_stmt_tree = p->base.x_stmt_tree;
8637 c_break_label = p->x_break_label;
8638 c_cont_label = p->x_cont_label;
8639 c_switch_stack = p->x_switch_stack;
8640 current_function_arg_info = p->arg_info;
8641 current_function_returns_value = p->returns_value;
8642 current_function_returns_null = p->returns_null;
8643 current_function_returns_abnormally = p->returns_abnormally;
8644 warn_about_return_type = p->warn_about_return_type;
8647 /* The functions below are required for functionality of doing
8648 function at once processing in the C front end. Currently these
8649 functions are not called from anywhere in the C front end, but as
8650 these changes continue, that will change. */
8652 /* Returns the stmt_tree (if any) to which statements are currently
8653 being added. If there is no active statement-tree, NULL is
8654 returned. */
8656 stmt_tree
8657 current_stmt_tree (void)
8659 return &c_stmt_tree;
8662 /* Return the global value of T as a symbol. */
8664 tree
8665 identifier_global_value (tree t)
8667 struct c_binding *b;
8669 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8670 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8671 return b->decl;
8673 return 0;
8676 /* In C, the only C-linkage public declaration is at file scope. */
8678 tree
8679 c_linkage_bindings (tree name)
8681 return identifier_global_value (name);
8684 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8685 otherwise the name is found in ridpointers from RID_INDEX. */
8687 void
8688 record_builtin_type (enum rid rid_index, const char *name, tree type)
8690 tree id, decl;
8691 if (name == 0)
8692 id = ridpointers[(int) rid_index];
8693 else
8694 id = get_identifier (name);
8695 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8696 pushdecl (decl);
8697 if (debug_hooks->type_decl)
8698 debug_hooks->type_decl (decl, false);
8701 /* Build the void_list_node (void_type_node having been created). */
8702 tree
8703 build_void_list_node (void)
8705 tree t = build_tree_list (NULL_TREE, void_type_node);
8706 return t;
8709 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8711 struct c_parm *
8712 build_c_parm (struct c_declspecs *specs, tree attrs,
8713 struct c_declarator *declarator)
8715 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8716 ret->specs = specs;
8717 ret->attrs = attrs;
8718 ret->declarator = declarator;
8719 return ret;
8722 /* Return a declarator with nested attributes. TARGET is the inner
8723 declarator to which these attributes apply. ATTRS are the
8724 attributes. */
8726 struct c_declarator *
8727 build_attrs_declarator (tree attrs, struct c_declarator *target)
8729 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8730 ret->kind = cdk_attrs;
8731 ret->declarator = target;
8732 ret->u.attrs = attrs;
8733 return ret;
8736 /* Return a declarator for a function with arguments specified by ARGS
8737 and return type specified by TARGET. */
8739 struct c_declarator *
8740 build_function_declarator (struct c_arg_info *args,
8741 struct c_declarator *target)
8743 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8744 ret->kind = cdk_function;
8745 ret->declarator = target;
8746 ret->u.arg_info = args;
8747 return ret;
8750 /* Return a declarator for the identifier IDENT (which may be
8751 NULL_TREE for an abstract declarator). */
8753 struct c_declarator *
8754 build_id_declarator (tree ident)
8756 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8757 ret->kind = cdk_id;
8758 ret->declarator = 0;
8759 ret->u.id = ident;
8760 /* Default value - may get reset to a more precise location. */
8761 ret->id_loc = input_location;
8762 return ret;
8765 /* Return something to represent absolute declarators containing a *.
8766 TARGET is the absolute declarator that the * contains.
8767 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8768 to apply to the pointer type. */
8770 struct c_declarator *
8771 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8772 struct c_declarator *target)
8774 tree attrs;
8775 int quals = 0;
8776 struct c_declarator *itarget = target;
8777 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8778 if (type_quals_attrs)
8780 attrs = type_quals_attrs->attrs;
8781 quals = quals_from_declspecs (type_quals_attrs);
8782 if (attrs != NULL_TREE)
8783 itarget = build_attrs_declarator (attrs, target);
8785 ret->kind = cdk_pointer;
8786 ret->declarator = itarget;
8787 ret->u.pointer_quals = quals;
8788 return ret;
8791 /* Return a pointer to a structure for an empty list of declaration
8792 specifiers. */
8794 struct c_declspecs *
8795 build_null_declspecs (void)
8797 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8798 ret->type = 0;
8799 ret->expr = 0;
8800 ret->decl_attr = 0;
8801 ret->attrs = 0;
8802 ret->align_log = -1;
8803 ret->typespec_word = cts_none;
8804 ret->storage_class = csc_none;
8805 ret->expr_const_operands = true;
8806 ret->declspecs_seen_p = false;
8807 ret->typespec_kind = ctsk_none;
8808 ret->non_sc_seen_p = false;
8809 ret->typedef_p = false;
8810 ret->explicit_signed_p = false;
8811 ret->deprecated_p = false;
8812 ret->default_int_p = false;
8813 ret->long_p = false;
8814 ret->long_long_p = false;
8815 ret->short_p = false;
8816 ret->signed_p = false;
8817 ret->unsigned_p = false;
8818 ret->complex_p = false;
8819 ret->inline_p = false;
8820 ret->noreturn_p = false;
8821 ret->thread_p = false;
8822 ret->const_p = false;
8823 ret->volatile_p = false;
8824 ret->restrict_p = false;
8825 ret->saturating_p = false;
8826 ret->alignas_p = false;
8827 ret->address_space = ADDR_SPACE_GENERIC;
8828 return ret;
8831 /* Add the address space ADDRSPACE to the declaration specifiers
8832 SPECS, returning SPECS. */
8834 struct c_declspecs *
8835 declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8837 specs->non_sc_seen_p = true;
8838 specs->declspecs_seen_p = true;
8840 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8841 && specs->address_space != as)
8842 error ("incompatible address space qualifiers %qs and %qs",
8843 c_addr_space_name (as),
8844 c_addr_space_name (specs->address_space));
8845 else
8846 specs->address_space = as;
8847 return specs;
8850 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8851 returning SPECS. */
8853 struct c_declspecs *
8854 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8856 enum rid i;
8857 bool dupe = false;
8858 specs->non_sc_seen_p = true;
8859 specs->declspecs_seen_p = true;
8860 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8861 && C_IS_RESERVED_WORD (qual));
8862 i = C_RID_CODE (qual);
8863 switch (i)
8865 case RID_CONST:
8866 dupe = specs->const_p;
8867 specs->const_p = true;
8868 break;
8869 case RID_VOLATILE:
8870 dupe = specs->volatile_p;
8871 specs->volatile_p = true;
8872 break;
8873 case RID_RESTRICT:
8874 dupe = specs->restrict_p;
8875 specs->restrict_p = true;
8876 break;
8877 default:
8878 gcc_unreachable ();
8880 if (dupe && !flag_isoc99)
8881 pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
8882 return specs;
8885 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8886 returning SPECS. */
8888 struct c_declspecs *
8889 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8890 struct c_typespec spec)
8892 tree type = spec.spec;
8893 specs->non_sc_seen_p = true;
8894 specs->declspecs_seen_p = true;
8895 specs->typespec_kind = spec.kind;
8896 if (TREE_DEPRECATED (type))
8897 specs->deprecated_p = true;
8899 /* Handle type specifier keywords. */
8900 if (TREE_CODE (type) == IDENTIFIER_NODE
8901 && C_IS_RESERVED_WORD (type)
8902 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8904 enum rid i = C_RID_CODE (type);
8905 if (specs->type)
8907 error_at (loc, "two or more data types in declaration specifiers");
8908 return specs;
8910 if ((int) i <= (int) RID_LAST_MODIFIER)
8912 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8913 bool dupe = false;
8914 switch (i)
8916 case RID_LONG:
8917 if (specs->long_long_p)
8919 error_at (loc, "%<long long long%> is too long for GCC");
8920 break;
8922 if (specs->long_p)
8924 if (specs->typespec_word == cts_double)
8926 error_at (loc,
8927 ("both %<long long%> and %<double%> in "
8928 "declaration specifiers"));
8929 break;
8931 pedwarn_c90 (loc, OPT_Wlong_long,
8932 "ISO C90 does not support %<long long%>");
8933 specs->long_long_p = 1;
8934 break;
8936 if (specs->short_p)
8937 error_at (loc,
8938 ("both %<long%> and %<short%> in "
8939 "declaration specifiers"));
8940 else if (specs->typespec_word == cts_void)
8941 error_at (loc,
8942 ("both %<long%> and %<void%> in "
8943 "declaration specifiers"));
8944 else if (specs->typespec_word == cts_int128)
8945 error_at (loc,
8946 ("both %<long%> and %<__int128%> in "
8947 "declaration specifiers"));
8948 else if (specs->typespec_word == cts_bool)
8949 error_at (loc,
8950 ("both %<long%> and %<_Bool%> in "
8951 "declaration specifiers"));
8952 else if (specs->typespec_word == cts_char)
8953 error_at (loc,
8954 ("both %<long%> and %<char%> in "
8955 "declaration specifiers"));
8956 else if (specs->typespec_word == cts_float)
8957 error_at (loc,
8958 ("both %<long%> and %<float%> in "
8959 "declaration specifiers"));
8960 else if (specs->typespec_word == cts_dfloat32)
8961 error_at (loc,
8962 ("both %<long%> and %<_Decimal32%> in "
8963 "declaration specifiers"));
8964 else if (specs->typespec_word == cts_dfloat64)
8965 error_at (loc,
8966 ("both %<long%> and %<_Decimal64%> in "
8967 "declaration specifiers"));
8968 else if (specs->typespec_word == cts_dfloat128)
8969 error_at (loc,
8970 ("both %<long%> and %<_Decimal128%> in "
8971 "declaration specifiers"));
8972 else
8973 specs->long_p = true;
8974 break;
8975 case RID_SHORT:
8976 dupe = specs->short_p;
8977 if (specs->long_p)
8978 error_at (loc,
8979 ("both %<long%> and %<short%> in "
8980 "declaration specifiers"));
8981 else if (specs->typespec_word == cts_void)
8982 error_at (loc,
8983 ("both %<short%> and %<void%> in "
8984 "declaration specifiers"));
8985 else if (specs->typespec_word == cts_int128)
8986 error_at (loc,
8987 ("both %<short%> and %<__int128%> in "
8988 "declaration specifiers"));
8989 else if (specs->typespec_word == cts_bool)
8990 error_at (loc,
8991 ("both %<short%> and %<_Bool%> in "
8992 "declaration specifiers"));
8993 else if (specs->typespec_word == cts_char)
8994 error_at (loc,
8995 ("both %<short%> and %<char%> in "
8996 "declaration specifiers"));
8997 else if (specs->typespec_word == cts_float)
8998 error_at (loc,
8999 ("both %<short%> and %<float%> in "
9000 "declaration specifiers"));
9001 else if (specs->typespec_word == cts_double)
9002 error_at (loc,
9003 ("both %<short%> and %<double%> in "
9004 "declaration specifiers"));
9005 else if (specs->typespec_word == cts_dfloat32)
9006 error_at (loc,
9007 ("both %<short%> and %<_Decimal32%> in "
9008 "declaration specifiers"));
9009 else if (specs->typespec_word == cts_dfloat64)
9010 error_at (loc,
9011 ("both %<short%> and %<_Decimal64%> in "
9012 "declaration specifiers"));
9013 else if (specs->typespec_word == cts_dfloat128)
9014 error_at (loc,
9015 ("both %<short%> and %<_Decimal128%> in "
9016 "declaration specifiers"));
9017 else
9018 specs->short_p = true;
9019 break;
9020 case RID_SIGNED:
9021 dupe = specs->signed_p;
9022 if (specs->unsigned_p)
9023 error_at (loc,
9024 ("both %<signed%> and %<unsigned%> in "
9025 "declaration specifiers"));
9026 else if (specs->typespec_word == cts_void)
9027 error_at (loc,
9028 ("both %<signed%> and %<void%> in "
9029 "declaration specifiers"));
9030 else if (specs->typespec_word == cts_bool)
9031 error_at (loc,
9032 ("both %<signed%> and %<_Bool%> in "
9033 "declaration specifiers"));
9034 else if (specs->typespec_word == cts_float)
9035 error_at (loc,
9036 ("both %<signed%> and %<float%> in "
9037 "declaration specifiers"));
9038 else if (specs->typespec_word == cts_double)
9039 error_at (loc,
9040 ("both %<signed%> and %<double%> in "
9041 "declaration specifiers"));
9042 else if (specs->typespec_word == cts_dfloat32)
9043 error_at (loc,
9044 ("both %<signed%> and %<_Decimal32%> in "
9045 "declaration specifiers"));
9046 else if (specs->typespec_word == cts_dfloat64)
9047 error_at (loc,
9048 ("both %<signed%> and %<_Decimal64%> in "
9049 "declaration specifiers"));
9050 else if (specs->typespec_word == cts_dfloat128)
9051 error_at (loc,
9052 ("both %<signed%> and %<_Decimal128%> in "
9053 "declaration specifiers"));
9054 else
9055 specs->signed_p = true;
9056 break;
9057 case RID_UNSIGNED:
9058 dupe = specs->unsigned_p;
9059 if (specs->signed_p)
9060 error_at (loc,
9061 ("both %<signed%> and %<unsigned%> in "
9062 "declaration specifiers"));
9063 else if (specs->typespec_word == cts_void)
9064 error_at (loc,
9065 ("both %<unsigned%> and %<void%> in "
9066 "declaration specifiers"));
9067 else if (specs->typespec_word == cts_bool)
9068 error_at (loc,
9069 ("both %<unsigned%> and %<_Bool%> in "
9070 "declaration specifiers"));
9071 else if (specs->typespec_word == cts_float)
9072 error_at (loc,
9073 ("both %<unsigned%> and %<float%> in "
9074 "declaration specifiers"));
9075 else if (specs->typespec_word == cts_double)
9076 error_at (loc,
9077 ("both %<unsigned%> and %<double%> in "
9078 "declaration specifiers"));
9079 else if (specs->typespec_word == cts_dfloat32)
9080 error_at (loc,
9081 ("both %<unsigned%> and %<_Decimal32%> in "
9082 "declaration specifiers"));
9083 else if (specs->typespec_word == cts_dfloat64)
9084 error_at (loc,
9085 ("both %<unsigned%> and %<_Decimal64%> in "
9086 "declaration specifiers"));
9087 else if (specs->typespec_word == cts_dfloat128)
9088 error_at (loc,
9089 ("both %<unsigned%> and %<_Decimal128%> in "
9090 "declaration specifiers"));
9091 else
9092 specs->unsigned_p = true;
9093 break;
9094 case RID_COMPLEX:
9095 dupe = specs->complex_p;
9096 if (!flag_isoc99 && !in_system_header)
9097 pedwarn (loc, OPT_pedantic,
9098 "ISO C90 does not support complex types");
9099 if (specs->typespec_word == cts_void)
9100 error_at (loc,
9101 ("both %<complex%> and %<void%> in "
9102 "declaration specifiers"));
9103 else if (specs->typespec_word == cts_bool)
9104 error_at (loc,
9105 ("both %<complex%> and %<_Bool%> in "
9106 "declaration specifiers"));
9107 else if (specs->typespec_word == cts_dfloat32)
9108 error_at (loc,
9109 ("both %<complex%> and %<_Decimal32%> in "
9110 "declaration specifiers"));
9111 else if (specs->typespec_word == cts_dfloat64)
9112 error_at (loc,
9113 ("both %<complex%> and %<_Decimal64%> in "
9114 "declaration specifiers"));
9115 else if (specs->typespec_word == cts_dfloat128)
9116 error_at (loc,
9117 ("both %<complex%> and %<_Decimal128%> in "
9118 "declaration specifiers"));
9119 else if (specs->typespec_word == cts_fract)
9120 error_at (loc,
9121 ("both %<complex%> and %<_Fract%> in "
9122 "declaration specifiers"));
9123 else if (specs->typespec_word == cts_accum)
9124 error_at (loc,
9125 ("both %<complex%> and %<_Accum%> in "
9126 "declaration specifiers"));
9127 else if (specs->saturating_p)
9128 error_at (loc,
9129 ("both %<complex%> and %<_Sat%> in "
9130 "declaration specifiers"));
9131 else
9132 specs->complex_p = true;
9133 break;
9134 case RID_SAT:
9135 dupe = specs->saturating_p;
9136 pedwarn (loc, OPT_pedantic,
9137 "ISO C does not support saturating types");
9138 if (specs->typespec_word == cts_int128)
9140 error_at (loc,
9141 ("both %<_Sat%> and %<__int128%> in "
9142 "declaration specifiers"));
9144 else if (specs->typespec_word == cts_void)
9145 error_at (loc,
9146 ("both %<_Sat%> and %<void%> in "
9147 "declaration specifiers"));
9148 else if (specs->typespec_word == cts_bool)
9149 error_at (loc,
9150 ("both %<_Sat%> and %<_Bool%> in "
9151 "declaration specifiers"));
9152 else if (specs->typespec_word == cts_char)
9153 error_at (loc,
9154 ("both %<_Sat%> and %<char%> in "
9155 "declaration specifiers"));
9156 else if (specs->typespec_word == cts_int)
9157 error_at (loc,
9158 ("both %<_Sat%> and %<int%> in "
9159 "declaration specifiers"));
9160 else if (specs->typespec_word == cts_float)
9161 error_at (loc,
9162 ("both %<_Sat%> and %<float%> in "
9163 "declaration specifiers"));
9164 else if (specs->typespec_word == cts_double)
9165 error_at (loc,
9166 ("both %<_Sat%> and %<double%> in "
9167 "declaration specifiers"));
9168 else if (specs->typespec_word == cts_dfloat32)
9169 error_at (loc,
9170 ("both %<_Sat%> and %<_Decimal32%> in "
9171 "declaration specifiers"));
9172 else if (specs->typespec_word == cts_dfloat64)
9173 error_at (loc,
9174 ("both %<_Sat%> and %<_Decimal64%> in "
9175 "declaration specifiers"));
9176 else if (specs->typespec_word == cts_dfloat128)
9177 error_at (loc,
9178 ("both %<_Sat%> and %<_Decimal128%> in "
9179 "declaration specifiers"));
9180 else if (specs->complex_p)
9181 error_at (loc,
9182 ("both %<_Sat%> and %<complex%> in "
9183 "declaration specifiers"));
9184 else
9185 specs->saturating_p = true;
9186 break;
9187 default:
9188 gcc_unreachable ();
9191 if (dupe)
9192 error_at (loc, "duplicate %qE", type);
9194 return specs;
9196 else
9198 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9199 "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9200 if (specs->typespec_word != cts_none)
9202 error_at (loc,
9203 "two or more data types in declaration specifiers");
9204 return specs;
9206 switch (i)
9208 case RID_INT128:
9209 if (int128_integer_type_node == NULL_TREE)
9211 error_at (loc, "%<__int128%> is not supported for this target");
9212 return specs;
9214 if (!in_system_header)
9215 pedwarn (loc, OPT_pedantic,
9216 "ISO C does not support %<__int128%> type");
9218 if (specs->long_p)
9219 error_at (loc,
9220 ("both %<__int128%> and %<long%> in "
9221 "declaration specifiers"));
9222 else if (specs->saturating_p)
9223 error_at (loc,
9224 ("both %<_Sat%> and %<__int128%> in "
9225 "declaration specifiers"));
9226 else if (specs->short_p)
9227 error_at (loc,
9228 ("both %<__int128%> and %<short%> in "
9229 "declaration specifiers"));
9230 else
9231 specs->typespec_word = cts_int128;
9232 return specs;
9233 case RID_VOID:
9234 if (specs->long_p)
9235 error_at (loc,
9236 ("both %<long%> and %<void%> in "
9237 "declaration specifiers"));
9238 else if (specs->short_p)
9239 error_at (loc,
9240 ("both %<short%> and %<void%> in "
9241 "declaration specifiers"));
9242 else if (specs->signed_p)
9243 error_at (loc,
9244 ("both %<signed%> and %<void%> in "
9245 "declaration specifiers"));
9246 else if (specs->unsigned_p)
9247 error_at (loc,
9248 ("both %<unsigned%> and %<void%> in "
9249 "declaration specifiers"));
9250 else if (specs->complex_p)
9251 error_at (loc,
9252 ("both %<complex%> and %<void%> in "
9253 "declaration specifiers"));
9254 else if (specs->saturating_p)
9255 error_at (loc,
9256 ("both %<_Sat%> and %<void%> in "
9257 "declaration specifiers"));
9258 else
9259 specs->typespec_word = cts_void;
9260 return specs;
9261 case RID_BOOL:
9262 if (specs->long_p)
9263 error_at (loc,
9264 ("both %<long%> and %<_Bool%> in "
9265 "declaration specifiers"));
9266 else if (specs->short_p)
9267 error_at (loc,
9268 ("both %<short%> and %<_Bool%> in "
9269 "declaration specifiers"));
9270 else if (specs->signed_p)
9271 error_at (loc,
9272 ("both %<signed%> and %<_Bool%> in "
9273 "declaration specifiers"));
9274 else if (specs->unsigned_p)
9275 error_at (loc,
9276 ("both %<unsigned%> and %<_Bool%> in "
9277 "declaration specifiers"));
9278 else if (specs->complex_p)
9279 error_at (loc,
9280 ("both %<complex%> and %<_Bool%> in "
9281 "declaration specifiers"));
9282 else if (specs->saturating_p)
9283 error_at (loc,
9284 ("both %<_Sat%> and %<_Bool%> in "
9285 "declaration specifiers"));
9286 else
9287 specs->typespec_word = cts_bool;
9288 return specs;
9289 case RID_CHAR:
9290 if (specs->long_p)
9291 error_at (loc,
9292 ("both %<long%> and %<char%> in "
9293 "declaration specifiers"));
9294 else if (specs->short_p)
9295 error_at (loc,
9296 ("both %<short%> and %<char%> in "
9297 "declaration specifiers"));
9298 else if (specs->saturating_p)
9299 error_at (loc,
9300 ("both %<_Sat%> and %<char%> in "
9301 "declaration specifiers"));
9302 else
9303 specs->typespec_word = cts_char;
9304 return specs;
9305 case RID_INT:
9306 if (specs->saturating_p)
9307 error_at (loc,
9308 ("both %<_Sat%> and %<int%> in "
9309 "declaration specifiers"));
9310 else
9311 specs->typespec_word = cts_int;
9312 return specs;
9313 case RID_FLOAT:
9314 if (specs->long_p)
9315 error_at (loc,
9316 ("both %<long%> and %<float%> in "
9317 "declaration specifiers"));
9318 else if (specs->short_p)
9319 error_at (loc,
9320 ("both %<short%> and %<float%> in "
9321 "declaration specifiers"));
9322 else if (specs->signed_p)
9323 error_at (loc,
9324 ("both %<signed%> and %<float%> in "
9325 "declaration specifiers"));
9326 else if (specs->unsigned_p)
9327 error_at (loc,
9328 ("both %<unsigned%> and %<float%> in "
9329 "declaration specifiers"));
9330 else if (specs->saturating_p)
9331 error_at (loc,
9332 ("both %<_Sat%> and %<float%> in "
9333 "declaration specifiers"));
9334 else
9335 specs->typespec_word = cts_float;
9336 return specs;
9337 case RID_DOUBLE:
9338 if (specs->long_long_p)
9339 error_at (loc,
9340 ("both %<long long%> and %<double%> in "
9341 "declaration specifiers"));
9342 else if (specs->short_p)
9343 error_at (loc,
9344 ("both %<short%> and %<double%> in "
9345 "declaration specifiers"));
9346 else if (specs->signed_p)
9347 error_at (loc,
9348 ("both %<signed%> and %<double%> in "
9349 "declaration specifiers"));
9350 else if (specs->unsigned_p)
9351 error_at (loc,
9352 ("both %<unsigned%> and %<double%> in "
9353 "declaration specifiers"));
9354 else if (specs->saturating_p)
9355 error_at (loc,
9356 ("both %<_Sat%> and %<double%> in "
9357 "declaration specifiers"));
9358 else
9359 specs->typespec_word = cts_double;
9360 return specs;
9361 case RID_DFLOAT32:
9362 case RID_DFLOAT64:
9363 case RID_DFLOAT128:
9365 const char *str;
9366 if (i == RID_DFLOAT32)
9367 str = "_Decimal32";
9368 else if (i == RID_DFLOAT64)
9369 str = "_Decimal64";
9370 else
9371 str = "_Decimal128";
9372 if (specs->long_long_p)
9373 error_at (loc,
9374 ("both %<long long%> and %<%s%> in "
9375 "declaration specifiers"),
9376 str);
9377 if (specs->long_p)
9378 error_at (loc,
9379 ("both %<long%> and %<%s%> in "
9380 "declaration specifiers"),
9381 str);
9382 else if (specs->short_p)
9383 error_at (loc,
9384 ("both %<short%> and %<%s%> in "
9385 "declaration specifiers"),
9386 str);
9387 else if (specs->signed_p)
9388 error_at (loc,
9389 ("both %<signed%> and %<%s%> in "
9390 "declaration specifiers"),
9391 str);
9392 else if (specs->unsigned_p)
9393 error_at (loc,
9394 ("both %<unsigned%> and %<%s%> in "
9395 "declaration specifiers"),
9396 str);
9397 else if (specs->complex_p)
9398 error_at (loc,
9399 ("both %<complex%> and %<%s%> in "
9400 "declaration specifiers"),
9401 str);
9402 else if (specs->saturating_p)
9403 error_at (loc,
9404 ("both %<_Sat%> and %<%s%> in "
9405 "declaration specifiers"),
9406 str);
9407 else if (i == RID_DFLOAT32)
9408 specs->typespec_word = cts_dfloat32;
9409 else if (i == RID_DFLOAT64)
9410 specs->typespec_word = cts_dfloat64;
9411 else
9412 specs->typespec_word = cts_dfloat128;
9414 if (!targetm.decimal_float_supported_p ())
9415 error_at (loc,
9416 ("decimal floating point not supported "
9417 "for this target"));
9418 pedwarn (loc, OPT_pedantic,
9419 "ISO C does not support decimal floating point");
9420 return specs;
9421 case RID_FRACT:
9422 case RID_ACCUM:
9424 const char *str;
9425 if (i == RID_FRACT)
9426 str = "_Fract";
9427 else
9428 str = "_Accum";
9429 if (specs->complex_p)
9430 error_at (loc,
9431 ("both %<complex%> and %<%s%> in "
9432 "declaration specifiers"),
9433 str);
9434 else if (i == RID_FRACT)
9435 specs->typespec_word = cts_fract;
9436 else
9437 specs->typespec_word = cts_accum;
9439 if (!targetm.fixed_point_supported_p ())
9440 error_at (loc,
9441 "fixed-point types not supported for this target");
9442 pedwarn (loc, OPT_pedantic,
9443 "ISO C does not support fixed-point types");
9444 return specs;
9445 default:
9446 /* ObjC reserved word "id", handled below. */
9447 break;
9452 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9453 form of ObjC type, cases such as "int" and "long" being handled
9454 above), a TYPE (struct, union, enum and typeof specifiers) or an
9455 ERROR_MARK. In none of these cases may there have previously
9456 been any type specifiers. */
9457 if (specs->type || specs->typespec_word != cts_none
9458 || specs->long_p || specs->short_p || specs->signed_p
9459 || specs->unsigned_p || specs->complex_p)
9460 error_at (loc, "two or more data types in declaration specifiers");
9461 else if (TREE_CODE (type) == TYPE_DECL)
9463 if (TREE_TYPE (type) == error_mark_node)
9464 ; /* Allow the type to default to int to avoid cascading errors. */
9465 else
9467 specs->type = TREE_TYPE (type);
9468 specs->decl_attr = DECL_ATTRIBUTES (type);
9469 specs->typedef_p = true;
9470 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9472 /* If this typedef name is defined in a struct, then a C++
9473 lookup would return a different value. */
9474 if (warn_cxx_compat
9475 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9476 warning_at (loc, OPT_Wc___compat,
9477 "C++ lookup of %qD would return a field, not a type",
9478 type);
9480 /* If we are parsing a struct, record that a struct field
9481 used a typedef. */
9482 if (warn_cxx_compat && struct_parse_info != NULL)
9483 VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9486 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9488 tree t = lookup_name (type);
9489 if (!t || TREE_CODE (t) != TYPE_DECL)
9490 error_at (loc, "%qE fails to be a typedef or built in type", type);
9491 else if (TREE_TYPE (t) == error_mark_node)
9493 else
9494 specs->type = TREE_TYPE (t);
9496 else
9498 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9500 specs->typedef_p = true;
9501 if (spec.expr)
9503 if (specs->expr)
9504 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9505 specs->expr, spec.expr);
9506 else
9507 specs->expr = spec.expr;
9508 specs->expr_const_operands &= spec.expr_const_operands;
9511 specs->type = type;
9514 return specs;
9517 /* Add the storage class specifier or function specifier SCSPEC to the
9518 declaration specifiers SPECS, returning SPECS. */
9520 struct c_declspecs *
9521 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9523 enum rid i;
9524 enum c_storage_class n = csc_none;
9525 bool dupe = false;
9526 specs->declspecs_seen_p = true;
9527 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9528 && C_IS_RESERVED_WORD (scspec));
9529 i = C_RID_CODE (scspec);
9530 if (specs->non_sc_seen_p)
9531 warning (OPT_Wold_style_declaration,
9532 "%qE is not at beginning of declaration", scspec);
9533 switch (i)
9535 case RID_INLINE:
9536 /* C99 permits duplicate inline. Although of doubtful utility,
9537 it seems simplest to permit it in gnu89 mode as well, as
9538 there is also little utility in maintaining this as a
9539 difference between gnu89 and C99 inline. */
9540 dupe = false;
9541 specs->inline_p = true;
9542 break;
9543 case RID_NORETURN:
9544 /* Duplicate _Noreturn is permitted. */
9545 dupe = false;
9546 specs->noreturn_p = true;
9547 break;
9548 case RID_THREAD:
9549 dupe = specs->thread_p;
9550 if (specs->storage_class == csc_auto)
9551 error ("%<__thread%> used with %<auto%>");
9552 else if (specs->storage_class == csc_register)
9553 error ("%<__thread%> used with %<register%>");
9554 else if (specs->storage_class == csc_typedef)
9555 error ("%<__thread%> used with %<typedef%>");
9556 else
9557 specs->thread_p = true;
9558 break;
9559 case RID_AUTO:
9560 n = csc_auto;
9561 break;
9562 case RID_EXTERN:
9563 n = csc_extern;
9564 /* Diagnose "__thread extern". */
9565 if (specs->thread_p)
9566 error ("%<__thread%> before %<extern%>");
9567 break;
9568 case RID_REGISTER:
9569 n = csc_register;
9570 break;
9571 case RID_STATIC:
9572 n = csc_static;
9573 /* Diagnose "__thread static". */
9574 if (specs->thread_p)
9575 error ("%<__thread%> before %<static%>");
9576 break;
9577 case RID_TYPEDEF:
9578 n = csc_typedef;
9579 break;
9580 default:
9581 gcc_unreachable ();
9583 if (n != csc_none && n == specs->storage_class)
9584 dupe = true;
9585 if (dupe)
9586 error ("duplicate %qE", scspec);
9587 if (n != csc_none)
9589 if (specs->storage_class != csc_none && n != specs->storage_class)
9591 error ("multiple storage classes in declaration specifiers");
9593 else
9595 specs->storage_class = n;
9596 if (n != csc_extern && n != csc_static && specs->thread_p)
9598 error ("%<__thread%> used with %qE", scspec);
9599 specs->thread_p = false;
9603 return specs;
9606 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9607 returning SPECS. */
9609 struct c_declspecs *
9610 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9612 specs->attrs = chainon (attrs, specs->attrs);
9613 specs->declspecs_seen_p = true;
9614 return specs;
9617 /* Add an _Alignas specifier (expression ALIGN, or type whose
9618 alignment is ALIGN) to the declaration specifiers SPECS, returning
9619 SPECS. */
9620 struct c_declspecs *
9621 declspecs_add_alignas (struct c_declspecs *specs, tree align)
9623 int align_log;
9624 specs->alignas_p = true;
9625 if (align == error_mark_node)
9626 return specs;
9627 align_log = check_user_alignment (align, true);
9628 if (align_log > specs->align_log)
9629 specs->align_log = align_log;
9630 return specs;
9633 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9634 specifiers with any other type specifier to determine the resulting
9635 type. This is where ISO C checks on complex types are made, since
9636 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9637 double". */
9639 struct c_declspecs *
9640 finish_declspecs (struct c_declspecs *specs)
9642 /* If a type was specified as a whole, we have no modifiers and are
9643 done. */
9644 if (specs->type != NULL_TREE)
9646 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9647 && !specs->signed_p && !specs->unsigned_p
9648 && !specs->complex_p);
9650 /* Set a dummy type. */
9651 if (TREE_CODE (specs->type) == ERROR_MARK)
9652 specs->type = integer_type_node;
9653 return specs;
9656 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9657 has been specified, treat it as "int" unless "_Complex" is
9658 present and there are no other specifiers. If we just have
9659 "_Complex", it is equivalent to "_Complex double", but e.g.
9660 "_Complex short" is equivalent to "_Complex short int". */
9661 if (specs->typespec_word == cts_none)
9663 if (specs->saturating_p)
9665 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9666 if (!targetm.fixed_point_supported_p ())
9667 error ("fixed-point types not supported for this target");
9668 specs->typespec_word = cts_fract;
9670 else if (specs->long_p || specs->short_p
9671 || specs->signed_p || specs->unsigned_p)
9673 specs->typespec_word = cts_int;
9675 else if (specs->complex_p)
9677 specs->typespec_word = cts_double;
9678 pedwarn (input_location, OPT_pedantic,
9679 "ISO C does not support plain %<complex%> meaning "
9680 "%<double complex%>");
9682 else
9684 specs->typespec_word = cts_int;
9685 specs->default_int_p = true;
9686 /* We don't diagnose this here because grokdeclarator will
9687 give more specific diagnostics according to whether it is
9688 a function definition. */
9692 /* If "signed" was specified, record this to distinguish "int" and
9693 "signed int" in the case of a bit-field with
9694 -funsigned-bitfields. */
9695 specs->explicit_signed_p = specs->signed_p;
9697 /* Now compute the actual type. */
9698 switch (specs->typespec_word)
9700 case cts_void:
9701 gcc_assert (!specs->long_p && !specs->short_p
9702 && !specs->signed_p && !specs->unsigned_p
9703 && !specs->complex_p);
9704 specs->type = void_type_node;
9705 break;
9706 case cts_bool:
9707 gcc_assert (!specs->long_p && !specs->short_p
9708 && !specs->signed_p && !specs->unsigned_p
9709 && !specs->complex_p);
9710 specs->type = boolean_type_node;
9711 break;
9712 case cts_char:
9713 gcc_assert (!specs->long_p && !specs->short_p);
9714 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9715 if (specs->signed_p)
9716 specs->type = signed_char_type_node;
9717 else if (specs->unsigned_p)
9718 specs->type = unsigned_char_type_node;
9719 else
9720 specs->type = char_type_node;
9721 if (specs->complex_p)
9723 pedwarn (input_location, OPT_pedantic,
9724 "ISO C does not support complex integer types");
9725 specs->type = build_complex_type (specs->type);
9727 break;
9728 case cts_int128:
9729 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
9730 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9731 specs->type = (specs->unsigned_p
9732 ? int128_unsigned_type_node
9733 : int128_integer_type_node);
9734 if (specs->complex_p)
9736 pedwarn (input_location, OPT_pedantic,
9737 "ISO C does not support complex integer types");
9738 specs->type = build_complex_type (specs->type);
9740 break;
9741 case cts_int:
9742 gcc_assert (!(specs->long_p && specs->short_p));
9743 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9744 if (specs->long_long_p)
9745 specs->type = (specs->unsigned_p
9746 ? long_long_unsigned_type_node
9747 : long_long_integer_type_node);
9748 else if (specs->long_p)
9749 specs->type = (specs->unsigned_p
9750 ? long_unsigned_type_node
9751 : long_integer_type_node);
9752 else if (specs->short_p)
9753 specs->type = (specs->unsigned_p
9754 ? short_unsigned_type_node
9755 : short_integer_type_node);
9756 else
9757 specs->type = (specs->unsigned_p
9758 ? unsigned_type_node
9759 : integer_type_node);
9760 if (specs->complex_p)
9762 pedwarn (input_location, OPT_pedantic,
9763 "ISO C does not support complex integer types");
9764 specs->type = build_complex_type (specs->type);
9766 break;
9767 case cts_float:
9768 gcc_assert (!specs->long_p && !specs->short_p
9769 && !specs->signed_p && !specs->unsigned_p);
9770 specs->type = (specs->complex_p
9771 ? complex_float_type_node
9772 : float_type_node);
9773 break;
9774 case cts_double:
9775 gcc_assert (!specs->long_long_p && !specs->short_p
9776 && !specs->signed_p && !specs->unsigned_p);
9777 if (specs->long_p)
9779 specs->type = (specs->complex_p
9780 ? complex_long_double_type_node
9781 : long_double_type_node);
9783 else
9785 specs->type = (specs->complex_p
9786 ? complex_double_type_node
9787 : double_type_node);
9789 break;
9790 case cts_dfloat32:
9791 case cts_dfloat64:
9792 case cts_dfloat128:
9793 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9794 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9795 if (specs->typespec_word == cts_dfloat32)
9796 specs->type = dfloat32_type_node;
9797 else if (specs->typespec_word == cts_dfloat64)
9798 specs->type = dfloat64_type_node;
9799 else
9800 specs->type = dfloat128_type_node;
9801 break;
9802 case cts_fract:
9803 gcc_assert (!specs->complex_p);
9804 if (!targetm.fixed_point_supported_p ())
9805 specs->type = integer_type_node;
9806 else if (specs->saturating_p)
9808 if (specs->long_long_p)
9809 specs->type = specs->unsigned_p
9810 ? sat_unsigned_long_long_fract_type_node
9811 : sat_long_long_fract_type_node;
9812 else if (specs->long_p)
9813 specs->type = specs->unsigned_p
9814 ? sat_unsigned_long_fract_type_node
9815 : sat_long_fract_type_node;
9816 else if (specs->short_p)
9817 specs->type = specs->unsigned_p
9818 ? sat_unsigned_short_fract_type_node
9819 : sat_short_fract_type_node;
9820 else
9821 specs->type = specs->unsigned_p
9822 ? sat_unsigned_fract_type_node
9823 : sat_fract_type_node;
9825 else
9827 if (specs->long_long_p)
9828 specs->type = specs->unsigned_p
9829 ? unsigned_long_long_fract_type_node
9830 : long_long_fract_type_node;
9831 else if (specs->long_p)
9832 specs->type = specs->unsigned_p
9833 ? unsigned_long_fract_type_node
9834 : long_fract_type_node;
9835 else if (specs->short_p)
9836 specs->type = specs->unsigned_p
9837 ? unsigned_short_fract_type_node
9838 : short_fract_type_node;
9839 else
9840 specs->type = specs->unsigned_p
9841 ? unsigned_fract_type_node
9842 : fract_type_node;
9844 break;
9845 case cts_accum:
9846 gcc_assert (!specs->complex_p);
9847 if (!targetm.fixed_point_supported_p ())
9848 specs->type = integer_type_node;
9849 else if (specs->saturating_p)
9851 if (specs->long_long_p)
9852 specs->type = specs->unsigned_p
9853 ? sat_unsigned_long_long_accum_type_node
9854 : sat_long_long_accum_type_node;
9855 else if (specs->long_p)
9856 specs->type = specs->unsigned_p
9857 ? sat_unsigned_long_accum_type_node
9858 : sat_long_accum_type_node;
9859 else if (specs->short_p)
9860 specs->type = specs->unsigned_p
9861 ? sat_unsigned_short_accum_type_node
9862 : sat_short_accum_type_node;
9863 else
9864 specs->type = specs->unsigned_p
9865 ? sat_unsigned_accum_type_node
9866 : sat_accum_type_node;
9868 else
9870 if (specs->long_long_p)
9871 specs->type = specs->unsigned_p
9872 ? unsigned_long_long_accum_type_node
9873 : long_long_accum_type_node;
9874 else if (specs->long_p)
9875 specs->type = specs->unsigned_p
9876 ? unsigned_long_accum_type_node
9877 : long_accum_type_node;
9878 else if (specs->short_p)
9879 specs->type = specs->unsigned_p
9880 ? unsigned_short_accum_type_node
9881 : short_accum_type_node;
9882 else
9883 specs->type = specs->unsigned_p
9884 ? unsigned_accum_type_node
9885 : accum_type_node;
9887 break;
9888 default:
9889 gcc_unreachable ();
9892 return specs;
9895 /* A subroutine of c_write_global_declarations. Perform final processing
9896 on one file scope's declarations (or the external scope's declarations),
9897 GLOBALS. */
9899 static void
9900 c_write_global_declarations_1 (tree globals)
9902 tree decl;
9903 bool reconsider;
9905 /* Process the decls in the order they were written. */
9906 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9908 /* Check for used but undefined static functions using the C
9909 standard's definition of "used", and set TREE_NO_WARNING so
9910 that check_global_declarations doesn't repeat the check. */
9911 if (TREE_CODE (decl) == FUNCTION_DECL
9912 && DECL_INITIAL (decl) == 0
9913 && DECL_EXTERNAL (decl)
9914 && !TREE_PUBLIC (decl)
9915 && C_DECL_USED (decl))
9917 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9918 TREE_NO_WARNING (decl) = 1;
9921 wrapup_global_declaration_1 (decl);
9926 reconsider = false;
9927 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9928 reconsider |= wrapup_global_declaration_2 (decl);
9930 while (reconsider);
9932 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9933 check_global_declaration_1 (decl);
9936 /* A subroutine of c_write_global_declarations Emit debug information for each
9937 of the declarations in GLOBALS. */
9939 static void
9940 c_write_global_declarations_2 (tree globals)
9942 tree decl;
9944 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
9945 debug_hooks->global_decl (decl);
9948 /* Callback to collect a source_ref from a DECL. */
9950 static void
9951 collect_source_ref_cb (tree decl)
9953 if (!DECL_IS_BUILTIN (decl))
9954 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
9957 /* Preserve the external declarations scope across a garbage collect. */
9958 static GTY(()) tree ext_block;
9960 /* Collect all references relevant to SOURCE_FILE. */
9962 static void
9963 collect_all_refs (const char *source_file)
9965 tree t;
9966 unsigned i;
9968 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9969 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
9971 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
9974 /* Iterate over all global declarations and call CALLBACK. */
9976 static void
9977 for_each_global_decl (void (*callback) (tree decl))
9979 tree t;
9980 tree decls;
9981 tree decl;
9982 unsigned i;
9984 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9986 decls = DECL_INITIAL (t);
9987 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
9988 callback (decl);
9991 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
9992 callback (decl);
9995 void
9996 c_write_global_declarations (void)
9998 tree t;
9999 unsigned i;
10001 /* We don't want to do this if generating a PCH. */
10002 if (pch_file)
10003 return;
10005 timevar_start (TV_PHASE_DEFERRED);
10007 /* Do the Objective-C stuff. This is where all the Objective-C
10008 module stuff gets generated (symtab, class/protocol/selector
10009 lists etc). */
10010 if (c_dialect_objc ())
10011 objc_write_global_declarations ();
10013 /* Close the external scope. */
10014 ext_block = pop_scope ();
10015 external_scope = 0;
10016 gcc_assert (!current_scope);
10018 /* Handle -fdump-ada-spec[-slim]. */
10019 if (dump_enabled_p (TDI_ada))
10021 /* Build a table of files to generate specs for */
10022 if (get_dump_file_info (TDI_ada)->flags & TDF_SLIM)
10023 collect_source_ref (main_input_filename);
10024 else
10025 for_each_global_decl (collect_source_ref_cb);
10027 dump_ada_specs (collect_all_refs, NULL);
10030 if (ext_block)
10032 tree tmp = BLOCK_VARS (ext_block);
10033 int flags;
10034 FILE * stream = dump_begin (TDI_tu, &flags);
10035 if (stream && tmp)
10037 dump_node (tmp, flags & ~TDF_SLIM, stream);
10038 dump_end (TDI_tu, stream);
10042 /* Process all file scopes in this compilation, and the external_scope,
10043 through wrapup_global_declarations and check_global_declarations. */
10044 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
10045 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10046 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10048 timevar_stop (TV_PHASE_DEFERRED);
10049 timevar_start (TV_PHASE_CGRAPH);
10051 /* We're done parsing; proceed to optimize and emit assembly.
10052 FIXME: shouldn't be the front end's responsibility to call this. */
10053 cgraph_finalize_compilation_unit ();
10055 timevar_stop (TV_PHASE_CGRAPH);
10056 timevar_start (TV_PHASE_DBGINFO);
10058 /* After cgraph has had a chance to emit everything that's going to
10059 be emitted, output debug information for globals. */
10060 if (!seen_error ())
10062 timevar_push (TV_SYMOUT);
10063 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
10064 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10065 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10066 timevar_pop (TV_SYMOUT);
10069 ext_block = NULL;
10070 timevar_stop (TV_PHASE_DBGINFO);
10073 /* Register reserved keyword WORD as qualifier for address space AS. */
10075 void
10076 c_register_addr_space (const char *word, addr_space_t as)
10078 int rid = RID_FIRST_ADDR_SPACE + as;
10079 tree id;
10081 /* Address space qualifiers are only supported
10082 in C with GNU extensions enabled. */
10083 if (c_dialect_objc () || flag_no_asm)
10084 return;
10086 id = get_identifier (word);
10087 C_SET_RID_CODE (id, rid);
10088 C_IS_RESERVED_WORD (id) = 1;
10089 ridpointers [rid] = id;
10092 #include "gt-c-decl.h"