2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / c-decl.c
blob72fad86f9d12238f8f533002393eea1910572214
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_Wpedantic,
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)));
2176 /* For real parm decl following a forward decl, rechain the old decl
2177 in its new location and clear TREE_ASM_WRITTEN (it's not a
2178 forward decl anymore). */
2179 if (TREE_CODE (newdecl) == PARM_DECL
2180 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2182 struct c_binding *b, **here;
2184 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2185 if ((*here)->decl == olddecl)
2186 goto found;
2187 gcc_unreachable ();
2189 found:
2190 b = *here;
2191 *here = b->prev;
2192 b->prev = current_scope->bindings;
2193 current_scope->bindings = b;
2195 TREE_ASM_WRITTEN (olddecl) = 0;
2198 DECL_ATTRIBUTES (newdecl)
2199 = targetm.merge_decl_attributes (olddecl, newdecl);
2201 /* Merge the data types specified in the two decls. */
2202 TREE_TYPE (newdecl)
2203 = TREE_TYPE (olddecl)
2204 = composite_type (newtype, oldtype);
2206 /* Lay the type out, unless already done. */
2207 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2209 if (TREE_TYPE (newdecl) != error_mark_node)
2210 layout_type (TREE_TYPE (newdecl));
2211 if (TREE_CODE (newdecl) != FUNCTION_DECL
2212 && TREE_CODE (newdecl) != TYPE_DECL
2213 && TREE_CODE (newdecl) != CONST_DECL)
2214 layout_decl (newdecl, 0);
2216 else
2218 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2219 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2220 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2221 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2222 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2224 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2225 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2229 /* Keep the old rtl since we can safely use it. */
2230 if (HAS_RTL_P (olddecl))
2231 COPY_DECL_RTL (olddecl, newdecl);
2233 /* Merge the type qualifiers. */
2234 if (TREE_READONLY (newdecl))
2235 TREE_READONLY (olddecl) = 1;
2237 if (TREE_THIS_VOLATILE (newdecl))
2238 TREE_THIS_VOLATILE (olddecl) = 1;
2240 /* Merge deprecatedness. */
2241 if (TREE_DEPRECATED (newdecl))
2242 TREE_DEPRECATED (olddecl) = 1;
2244 /* If a decl is in a system header and the other isn't, keep the one on the
2245 system header. Otherwise, keep source location of definition rather than
2246 declaration and of prototype rather than non-prototype unless that
2247 prototype is built-in. */
2248 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2249 && DECL_IN_SYSTEM_HEADER (olddecl)
2250 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2251 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2252 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2253 && DECL_IN_SYSTEM_HEADER (newdecl)
2254 && !DECL_IN_SYSTEM_HEADER (olddecl))
2255 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2256 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2257 || (old_is_prototype && !new_is_prototype
2258 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2259 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2261 /* Merge the initialization information. */
2262 if (DECL_INITIAL (newdecl) == 0)
2263 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2265 /* Merge the threadprivate attribute. */
2266 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2268 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2269 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2272 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2274 /* Merge the section attribute.
2275 We want to issue an error if the sections conflict but that
2276 must be done later in decl_attributes since we are called
2277 before attributes are assigned. */
2278 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2279 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2281 /* Copy the assembler name.
2282 Currently, it can only be defined in the prototype. */
2283 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2285 /* Use visibility of whichever declaration had it specified */
2286 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2288 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2289 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2292 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2294 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2295 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2296 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2297 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2298 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2299 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2300 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2301 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2302 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2303 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2304 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2307 /* Merge the storage class information. */
2308 merge_weak (newdecl, olddecl);
2310 /* For functions, static overrides non-static. */
2311 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2313 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2314 /* This is since we don't automatically
2315 copy the attributes of NEWDECL into OLDDECL. */
2316 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2317 /* If this clears `static', clear it in the identifier too. */
2318 if (!TREE_PUBLIC (olddecl))
2319 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2323 /* In c99, 'extern' declaration before (or after) 'inline' means this
2324 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2325 is present. */
2326 if (TREE_CODE (newdecl) == FUNCTION_DECL
2327 && !flag_gnu89_inline
2328 && (DECL_DECLARED_INLINE_P (newdecl)
2329 || DECL_DECLARED_INLINE_P (olddecl))
2330 && (!DECL_DECLARED_INLINE_P (newdecl)
2331 || !DECL_DECLARED_INLINE_P (olddecl)
2332 || !DECL_EXTERNAL (olddecl))
2333 && DECL_EXTERNAL (newdecl)
2334 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2335 && !current_function_decl)
2336 DECL_EXTERNAL (newdecl) = 0;
2338 if (DECL_EXTERNAL (newdecl))
2340 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2341 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2343 /* An extern decl does not override previous storage class. */
2344 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2345 if (!DECL_EXTERNAL (newdecl))
2347 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2348 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2351 else
2353 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2354 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2357 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2359 /* If we're redefining a function previously defined as extern
2360 inline, make sure we emit debug info for the inline before we
2361 throw it away, in case it was inlined into a function that
2362 hasn't been written out yet. */
2363 if (new_is_definition && DECL_INITIAL (olddecl))
2364 /* The new defn must not be inline. */
2365 DECL_UNINLINABLE (newdecl) = 1;
2366 else
2368 /* If either decl says `inline', this fn is inline, unless
2369 its definition was passed already. */
2370 if (DECL_DECLARED_INLINE_P (newdecl)
2371 || DECL_DECLARED_INLINE_P (olddecl))
2372 DECL_DECLARED_INLINE_P (newdecl) = 1;
2374 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2375 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2377 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2378 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2379 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2380 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2383 if (DECL_BUILT_IN (olddecl))
2385 /* If redeclaring a builtin function, it stays built in.
2386 But it gets tagged as having been declared. */
2387 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2388 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2389 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2390 if (new_is_prototype)
2392 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2393 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2395 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2396 switch (fncode)
2398 /* If a compatible prototype of these builtin functions
2399 is seen, assume the runtime implements it with the
2400 expected semantics. */
2401 case BUILT_IN_STPCPY:
2402 if (builtin_decl_explicit_p (fncode))
2403 set_builtin_decl_implicit_p (fncode, true);
2404 break;
2405 default:
2406 break;
2410 else
2411 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2412 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2415 /* Preserve function specific target and optimization options */
2416 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2417 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2418 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2419 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2421 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2422 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2423 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2424 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2426 /* Also preserve various other info from the definition. */
2427 if (!new_is_definition)
2429 tree t;
2430 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2431 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2432 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2433 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2434 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2435 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2436 DECL_CONTEXT (t) = newdecl;
2438 /* See if we've got a function to instantiate from. */
2439 if (DECL_SAVED_TREE (olddecl))
2440 DECL_ABSTRACT_ORIGIN (newdecl)
2441 = DECL_ABSTRACT_ORIGIN (olddecl);
2445 /* Merge the USED information. */
2446 if (TREE_USED (olddecl))
2447 TREE_USED (newdecl) = 1;
2448 else if (TREE_USED (newdecl))
2449 TREE_USED (olddecl) = 1;
2450 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2451 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2452 if (DECL_PRESERVE_P (olddecl))
2453 DECL_PRESERVE_P (newdecl) = 1;
2454 else if (DECL_PRESERVE_P (newdecl))
2455 DECL_PRESERVE_P (olddecl) = 1;
2457 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2458 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2459 DECL_ARGUMENTS (if appropriate). */
2461 unsigned olddecl_uid = DECL_UID (olddecl);
2462 tree olddecl_context = DECL_CONTEXT (olddecl);
2463 tree olddecl_arguments = NULL;
2464 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2465 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2467 memcpy ((char *) olddecl + sizeof (struct tree_common),
2468 (char *) newdecl + sizeof (struct tree_common),
2469 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2470 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2471 switch (TREE_CODE (olddecl))
2473 case FUNCTION_DECL:
2474 case FIELD_DECL:
2475 case VAR_DECL:
2476 case PARM_DECL:
2477 case LABEL_DECL:
2478 case RESULT_DECL:
2479 case CONST_DECL:
2480 case TYPE_DECL:
2481 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2482 (char *) newdecl + sizeof (struct tree_decl_common),
2483 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2484 break;
2486 default:
2488 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2489 (char *) newdecl + sizeof (struct tree_decl_common),
2490 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2492 DECL_UID (olddecl) = olddecl_uid;
2493 DECL_CONTEXT (olddecl) = olddecl_context;
2494 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2495 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2498 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2499 so that encode_section_info has a chance to look at the new decl
2500 flags and attributes. */
2501 if (DECL_RTL_SET_P (olddecl)
2502 && (TREE_CODE (olddecl) == FUNCTION_DECL
2503 || (TREE_CODE (olddecl) == VAR_DECL
2504 && TREE_STATIC (olddecl))))
2505 make_decl_rtl (olddecl);
2508 /* Handle when a new declaration NEWDECL has the same name as an old
2509 one OLDDECL in the same binding contour. Prints an error message
2510 if appropriate.
2512 If safely possible, alter OLDDECL to look like NEWDECL, and return
2513 true. Otherwise, return false. */
2515 static bool
2516 duplicate_decls (tree newdecl, tree olddecl)
2518 tree newtype = NULL, oldtype = NULL;
2520 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2522 /* Avoid `unused variable' and other warnings for OLDDECL. */
2523 TREE_NO_WARNING (olddecl) = 1;
2524 return false;
2527 merge_decls (newdecl, olddecl, newtype, oldtype);
2528 return true;
2532 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2533 static void
2534 warn_if_shadowing (tree new_decl)
2536 struct c_binding *b;
2538 /* Shadow warnings wanted? */
2539 if (!warn_shadow
2540 /* No shadow warnings for internally generated vars. */
2541 || DECL_IS_BUILTIN (new_decl)
2542 /* No shadow warnings for vars made for inlining. */
2543 || DECL_FROM_INLINE (new_decl))
2544 return;
2546 /* Is anything being shadowed? Invisible decls do not count. */
2547 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2548 if (b->decl && b->decl != new_decl && !b->invisible
2549 && (b->decl == error_mark_node
2550 || diagnostic_report_warnings_p (global_dc,
2551 DECL_SOURCE_LOCATION (b->decl))))
2553 tree old_decl = b->decl;
2555 if (old_decl == error_mark_node)
2557 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2558 "non-variable", new_decl);
2559 break;
2561 else if (TREE_CODE (old_decl) == PARM_DECL)
2562 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2563 new_decl);
2564 else if (DECL_FILE_SCOPE_P (old_decl))
2565 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2566 "declaration", new_decl);
2567 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2568 && DECL_BUILT_IN (old_decl))
2570 warning (OPT_Wshadow, "declaration of %q+D shadows "
2571 "a built-in function", new_decl);
2572 break;
2574 else
2575 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2576 new_decl);
2578 warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2579 "shadowed declaration is here");
2581 break;
2585 /* Record a decl-node X as belonging to the current lexical scope.
2586 Check for errors (such as an incompatible declaration for the same
2587 name already seen in the same scope).
2589 Returns either X or an old decl for the same name.
2590 If an old decl is returned, it may have been smashed
2591 to agree with what X says. */
2593 tree
2594 pushdecl (tree x)
2596 tree name = DECL_NAME (x);
2597 struct c_scope *scope = current_scope;
2598 struct c_binding *b;
2599 bool nested = false;
2600 location_t locus = DECL_SOURCE_LOCATION (x);
2602 /* Must set DECL_CONTEXT for everything not at file scope or
2603 DECL_FILE_SCOPE_P won't work. Local externs don't count
2604 unless they have initializers (which generate code). */
2605 if (current_function_decl
2606 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2607 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2608 DECL_CONTEXT (x) = current_function_decl;
2610 /* Anonymous decls are just inserted in the scope. */
2611 if (!name)
2613 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2614 locus);
2615 return x;
2618 /* First, see if there is another declaration with the same name in
2619 the current scope. If there is, duplicate_decls may do all the
2620 work for us. If duplicate_decls returns false, that indicates
2621 two incompatible decls in the same scope; we are to silently
2622 replace the old one (duplicate_decls has issued all appropriate
2623 diagnostics). In particular, we should not consider possible
2624 duplicates in the external scope, or shadowing. */
2625 b = I_SYMBOL_BINDING (name);
2626 if (b && B_IN_SCOPE (b, scope))
2628 struct c_binding *b_ext, *b_use;
2629 tree type = TREE_TYPE (x);
2630 tree visdecl = b->decl;
2631 tree vistype = TREE_TYPE (visdecl);
2632 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2633 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2634 b->inner_comp = false;
2635 b_use = b;
2636 b_ext = b;
2637 /* If this is an external linkage declaration, we should check
2638 for compatibility with the type in the external scope before
2639 setting the type at this scope based on the visible
2640 information only. */
2641 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2643 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2644 b_ext = b_ext->shadowed;
2645 if (b_ext)
2647 b_use = b_ext;
2648 if (b_use->u.type)
2649 TREE_TYPE (b_use->decl) = b_use->u.type;
2652 if (duplicate_decls (x, b_use->decl))
2654 if (b_use != b)
2656 /* Save the updated type in the external scope and
2657 restore the proper type for this scope. */
2658 tree thistype;
2659 if (comptypes (vistype, type))
2660 thistype = composite_type (vistype, type);
2661 else
2662 thistype = TREE_TYPE (b_use->decl);
2663 b_use->u.type = TREE_TYPE (b_use->decl);
2664 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2665 && DECL_BUILT_IN (b_use->decl))
2666 thistype
2667 = build_type_attribute_variant (thistype,
2668 TYPE_ATTRIBUTES
2669 (b_use->u.type));
2670 TREE_TYPE (b_use->decl) = thistype;
2672 return b_use->decl;
2674 else
2675 goto skip_external_and_shadow_checks;
2678 /* All declarations with external linkage, and all external
2679 references, go in the external scope, no matter what scope is
2680 current. However, the binding in that scope is ignored for
2681 purposes of normal name lookup. A separate binding structure is
2682 created in the requested scope; this governs the normal
2683 visibility of the symbol.
2685 The binding in the externals scope is used exclusively for
2686 detecting duplicate declarations of the same object, no matter
2687 what scope they are in; this is what we do here. (C99 6.2.7p2:
2688 All declarations that refer to the same object or function shall
2689 have compatible type; otherwise, the behavior is undefined.) */
2690 if (DECL_EXTERNAL (x) || scope == file_scope)
2692 tree type = TREE_TYPE (x);
2693 tree vistype = 0;
2694 tree visdecl = 0;
2695 bool type_saved = false;
2696 if (b && !B_IN_EXTERNAL_SCOPE (b)
2697 && (TREE_CODE (b->decl) == FUNCTION_DECL
2698 || TREE_CODE (b->decl) == VAR_DECL)
2699 && DECL_FILE_SCOPE_P (b->decl))
2701 visdecl = b->decl;
2702 vistype = TREE_TYPE (visdecl);
2704 if (scope != file_scope
2705 && !DECL_IN_SYSTEM_HEADER (x))
2706 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2708 while (b && !B_IN_EXTERNAL_SCOPE (b))
2710 /* If this decl might be modified, save its type. This is
2711 done here rather than when the decl is first bound
2712 because the type may change after first binding, through
2713 being completed or through attributes being added. If we
2714 encounter multiple such decls, only the first should have
2715 its type saved; the others will already have had their
2716 proper types saved and the types will not have changed as
2717 their scopes will not have been re-entered. */
2718 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2720 b->u.type = TREE_TYPE (b->decl);
2721 type_saved = true;
2723 if (B_IN_FILE_SCOPE (b)
2724 && TREE_CODE (b->decl) == VAR_DECL
2725 && TREE_STATIC (b->decl)
2726 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2727 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2728 && TREE_CODE (type) == ARRAY_TYPE
2729 && TYPE_DOMAIN (type)
2730 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2731 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2733 /* Array type completed in inner scope, which should be
2734 diagnosed if the completion does not have size 1 and
2735 it does not get completed in the file scope. */
2736 b->inner_comp = true;
2738 b = b->shadowed;
2741 /* If a matching external declaration has been found, set its
2742 type to the composite of all the types of that declaration.
2743 After the consistency checks, it will be reset to the
2744 composite of the visible types only. */
2745 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2746 && b->u.type)
2747 TREE_TYPE (b->decl) = b->u.type;
2749 /* The point of the same_translation_unit_p check here is,
2750 we want to detect a duplicate decl for a construct like
2751 foo() { extern bar(); } ... static bar(); but not if
2752 they are in different translation units. In any case,
2753 the static does not go in the externals scope. */
2754 if (b
2755 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2756 && duplicate_decls (x, b->decl))
2758 tree thistype;
2759 if (vistype)
2761 if (comptypes (vistype, type))
2762 thistype = composite_type (vistype, type);
2763 else
2764 thistype = TREE_TYPE (b->decl);
2766 else
2767 thistype = type;
2768 b->u.type = TREE_TYPE (b->decl);
2769 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2770 thistype
2771 = build_type_attribute_variant (thistype,
2772 TYPE_ATTRIBUTES (b->u.type));
2773 TREE_TYPE (b->decl) = thistype;
2774 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2775 locus);
2776 return b->decl;
2778 else if (TREE_PUBLIC (x))
2780 if (visdecl && !b && duplicate_decls (x, visdecl))
2782 /* An external declaration at block scope referring to a
2783 visible entity with internal linkage. The composite
2784 type will already be correct for this scope, so we
2785 just need to fall through to make the declaration in
2786 this scope. */
2787 nested = true;
2788 x = visdecl;
2790 else
2792 bind (name, x, external_scope, /*invisible=*/true,
2793 /*nested=*/false, locus);
2794 nested = true;
2799 if (TREE_CODE (x) != PARM_DECL)
2800 warn_if_shadowing (x);
2802 skip_external_and_shadow_checks:
2803 if (TREE_CODE (x) == TYPE_DECL)
2805 /* So this is a typedef, set its underlying type. */
2806 set_underlying_type (x);
2808 /* If X is a typedef defined in the current function, record it
2809 for the purpose of implementing the -Wunused-local-typedefs
2810 warning. */
2811 record_locally_defined_typedef (x);
2814 bind (name, x, scope, /*invisible=*/false, nested, locus);
2816 /* If x's type is incomplete because it's based on a
2817 structure or union which has not yet been fully declared,
2818 attach it to that structure or union type, so we can go
2819 back and complete the variable declaration later, if the
2820 structure or union gets fully declared.
2822 If the input is erroneous, we can have error_mark in the type
2823 slot (e.g. "f(void a, ...)") - that doesn't count as an
2824 incomplete type. */
2825 if (TREE_TYPE (x) != error_mark_node
2826 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2828 tree element = TREE_TYPE (x);
2830 while (TREE_CODE (element) == ARRAY_TYPE)
2831 element = TREE_TYPE (element);
2832 element = TYPE_MAIN_VARIANT (element);
2834 if ((TREE_CODE (element) == RECORD_TYPE
2835 || TREE_CODE (element) == UNION_TYPE)
2836 && (TREE_CODE (x) != TYPE_DECL
2837 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2838 && !COMPLETE_TYPE_P (element))
2839 C_TYPE_INCOMPLETE_VARS (element)
2840 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2842 return x;
2845 /* Record X as belonging to file scope.
2846 This is used only internally by the Objective-C front end,
2847 and is limited to its needs. duplicate_decls is not called;
2848 if there is any preexisting decl for this identifier, it is an ICE. */
2850 tree
2851 pushdecl_top_level (tree x)
2853 tree name;
2854 bool nested = false;
2855 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2857 name = DECL_NAME (x);
2859 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2861 if (TREE_PUBLIC (x))
2863 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2864 UNKNOWN_LOCATION);
2865 nested = true;
2867 if (file_scope)
2868 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2870 return x;
2873 static void
2874 implicit_decl_warning (tree id, tree olddecl)
2876 if (warn_implicit_function_declaration)
2878 bool warned;
2880 if (flag_isoc99)
2881 warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2882 "implicit declaration of function %qE", id);
2883 else
2884 warned = warning (OPT_Wimplicit_function_declaration,
2885 G_("implicit declaration of function %qE"), id);
2886 if (olddecl && warned)
2887 locate_old_decl (olddecl);
2891 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2892 function of type int (). */
2894 tree
2895 implicitly_declare (location_t loc, tree functionid)
2897 struct c_binding *b;
2898 tree decl = 0;
2899 tree asmspec_tree;
2901 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2903 if (B_IN_SCOPE (b, external_scope))
2905 decl = b->decl;
2906 break;
2910 if (decl)
2912 if (decl == error_mark_node)
2913 return decl;
2915 /* FIXME: Objective-C has weird not-really-builtin functions
2916 which are supposed to be visible automatically. They wind up
2917 in the external scope because they're pushed before the file
2918 scope gets created. Catch this here and rebind them into the
2919 file scope. */
2920 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2922 bind (functionid, decl, file_scope,
2923 /*invisible=*/false, /*nested=*/true,
2924 DECL_SOURCE_LOCATION (decl));
2925 return decl;
2927 else
2929 tree newtype = default_function_type;
2930 if (b->u.type)
2931 TREE_TYPE (decl) = b->u.type;
2932 /* Implicit declaration of a function already declared
2933 (somehow) in a different scope, or as a built-in.
2934 If this is the first time this has happened, warn;
2935 then recycle the old declaration but with the new type. */
2936 if (!C_DECL_IMPLICIT (decl))
2938 implicit_decl_warning (functionid, decl);
2939 C_DECL_IMPLICIT (decl) = 1;
2941 if (DECL_BUILT_IN (decl))
2943 newtype = build_type_attribute_variant (newtype,
2944 TYPE_ATTRIBUTES
2945 (TREE_TYPE (decl)));
2946 if (!comptypes (newtype, TREE_TYPE (decl)))
2948 warning_at (loc, 0, "incompatible implicit declaration of "
2949 "built-in function %qD", decl);
2950 newtype = TREE_TYPE (decl);
2953 else
2955 if (!comptypes (newtype, TREE_TYPE (decl)))
2957 error_at (loc, "incompatible implicit declaration of function %qD", decl);
2958 locate_old_decl (decl);
2961 b->u.type = TREE_TYPE (decl);
2962 TREE_TYPE (decl) = newtype;
2963 bind (functionid, decl, current_scope,
2964 /*invisible=*/false, /*nested=*/true,
2965 DECL_SOURCE_LOCATION (decl));
2966 return decl;
2970 /* Not seen before. */
2971 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2972 DECL_EXTERNAL (decl) = 1;
2973 TREE_PUBLIC (decl) = 1;
2974 C_DECL_IMPLICIT (decl) = 1;
2975 implicit_decl_warning (functionid, 0);
2976 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2977 if (asmspec_tree)
2978 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2980 /* C89 says implicit declarations are in the innermost block.
2981 So we record the decl in the standard fashion. */
2982 decl = pushdecl (decl);
2984 /* No need to call objc_check_decl here - it's a function type. */
2985 rest_of_decl_compilation (decl, 0, 0);
2987 /* Write a record describing this implicit function declaration
2988 to the prototypes file (if requested). */
2989 gen_aux_info_record (decl, 0, 1, 0);
2991 /* Possibly apply some default attributes to this implicit declaration. */
2992 decl_attributes (&decl, NULL_TREE, 0);
2994 return decl;
2997 /* Issue an error message for a reference to an undeclared variable
2998 ID, including a reference to a builtin outside of function-call
2999 context. Establish a binding of the identifier to error_mark_node
3000 in an appropriate scope, which will suppress further errors for the
3001 same identifier. The error message should be given location LOC. */
3002 void
3003 undeclared_variable (location_t loc, tree id)
3005 static bool already = false;
3006 struct c_scope *scope;
3008 if (current_function_decl == 0)
3010 error_at (loc, "%qE undeclared here (not in a function)", id);
3011 scope = current_scope;
3013 else
3015 if (!objc_diagnose_private_ivar (id))
3016 error_at (loc, "%qE undeclared (first use in this function)", id);
3017 if (!already)
3019 inform (loc, "each undeclared identifier is reported only"
3020 " once for each function it appears in");
3021 already = true;
3024 /* If we are parsing old-style parameter decls, current_function_decl
3025 will be nonnull but current_function_scope will be null. */
3026 scope = current_function_scope ? current_function_scope : current_scope;
3028 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3029 UNKNOWN_LOCATION);
3032 /* Subroutine of lookup_label, declare_label, define_label: construct a
3033 LABEL_DECL with all the proper frills. Also create a struct
3034 c_label_vars initialized for the current scope. */
3036 static tree
3037 make_label (location_t location, tree name, bool defining,
3038 struct c_label_vars **p_label_vars)
3040 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3041 struct c_label_vars *label_vars;
3043 DECL_CONTEXT (label) = current_function_decl;
3044 DECL_MODE (label) = VOIDmode;
3046 label_vars = ggc_alloc_c_label_vars ();
3047 label_vars->shadowed = NULL;
3048 set_spot_bindings (&label_vars->label_bindings, defining);
3049 label_vars->decls_in_scope = make_tree_vector ();
3050 label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3051 *p_label_vars = label_vars;
3053 return label;
3056 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3057 Create one if none exists so far for the current function.
3058 This is called when a label is used in a goto expression or
3059 has its address taken. */
3061 tree
3062 lookup_label (tree name)
3064 tree label;
3065 struct c_label_vars *label_vars;
3067 if (current_function_scope == 0)
3069 error ("label %qE referenced outside of any function", name);
3070 return 0;
3073 /* Use a label already defined or ref'd with this name, but not if
3074 it is inherited from a containing function and wasn't declared
3075 using __label__. */
3076 label = I_LABEL_DECL (name);
3077 if (label && (DECL_CONTEXT (label) == current_function_decl
3078 || C_DECLARED_LABEL_FLAG (label)))
3080 /* If the label has only been declared, update its apparent
3081 location to point here, for better diagnostics if it
3082 turns out not to have been defined. */
3083 if (DECL_INITIAL (label) == NULL_TREE)
3084 DECL_SOURCE_LOCATION (label) = input_location;
3085 return label;
3088 /* No label binding for that identifier; make one. */
3089 label = make_label (input_location, name, false, &label_vars);
3091 /* Ordinary labels go in the current function scope. */
3092 bind_label (name, label, current_function_scope, label_vars);
3094 return label;
3097 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3098 to LABEL. */
3100 static void
3101 warn_about_goto (location_t goto_loc, tree label, tree decl)
3103 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3104 error_at (goto_loc,
3105 "jump into scope of identifier with variably modified type");
3106 else
3107 warning_at (goto_loc, OPT_Wjump_misses_init,
3108 "jump skips variable initialization");
3109 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3110 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3113 /* Look up a label because of a goto statement. This is like
3114 lookup_label, but also issues any appropriate warnings. */
3116 tree
3117 lookup_label_for_goto (location_t loc, tree name)
3119 tree label;
3120 struct c_label_vars *label_vars;
3121 unsigned int ix;
3122 tree decl;
3124 label = lookup_label (name);
3125 if (label == NULL_TREE)
3126 return NULL_TREE;
3128 /* If we are jumping to a different function, we can't issue any
3129 useful warnings. */
3130 if (DECL_CONTEXT (label) != current_function_decl)
3132 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3133 return label;
3136 label_vars = I_LABEL_BINDING (name)->u.label;
3138 /* If the label has not yet been defined, then push this goto on a
3139 list for possible later warnings. */
3140 if (label_vars->label_bindings.scope == NULL)
3142 struct c_goto_bindings *g;
3144 g = ggc_alloc_c_goto_bindings ();
3145 g->loc = loc;
3146 set_spot_bindings (&g->goto_bindings, true);
3147 VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3148 return label;
3151 /* If there are any decls in label_vars->decls_in_scope, then this
3152 goto has missed the declaration of the decl. This happens for a
3153 case like
3154 int i = 1;
3155 lab:
3157 goto lab;
3158 Issue a warning or error. */
3159 FOR_EACH_VEC_ELT (tree, label_vars->decls_in_scope, ix, decl)
3160 warn_about_goto (loc, label, decl);
3162 if (label_vars->label_bindings.left_stmt_expr)
3164 error_at (loc, "jump into statement expression");
3165 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3168 return label;
3171 /* Make a label named NAME in the current function, shadowing silently
3172 any that may be inherited from containing functions or containing
3173 scopes. This is called for __label__ declarations. */
3175 tree
3176 declare_label (tree name)
3178 struct c_binding *b = I_LABEL_BINDING (name);
3179 tree label;
3180 struct c_label_vars *label_vars;
3182 /* Check to make sure that the label hasn't already been declared
3183 at this scope */
3184 if (b && B_IN_CURRENT_SCOPE (b))
3186 error ("duplicate label declaration %qE", name);
3187 locate_old_decl (b->decl);
3189 /* Just use the previous declaration. */
3190 return b->decl;
3193 label = make_label (input_location, name, false, &label_vars);
3194 C_DECLARED_LABEL_FLAG (label) = 1;
3196 /* Declared labels go in the current scope. */
3197 bind_label (name, label, current_scope, label_vars);
3199 return label;
3202 /* When we define a label, issue any appropriate warnings if there are
3203 any gotos earlier in the function which jump to this label. */
3205 static void
3206 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3208 unsigned int ix;
3209 struct c_goto_bindings *g;
3211 FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
3213 struct c_binding *b;
3214 struct c_scope *scope;
3216 /* We have a goto to this label. The goto is going forward. In
3217 g->scope, the goto is going to skip any binding which was
3218 defined after g->bindings_in_scope. */
3219 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3221 for (b = g->goto_bindings.scope->bindings;
3222 b != g->goto_bindings.bindings_in_scope;
3223 b = b->prev)
3225 if (decl_jump_unsafe (b->decl))
3226 warn_about_goto (g->loc, label, b->decl);
3230 /* We also need to warn about decls defined in any scopes
3231 between the scope of the label and the scope of the goto. */
3232 for (scope = label_vars->label_bindings.scope;
3233 scope != g->goto_bindings.scope;
3234 scope = scope->outer)
3236 gcc_assert (scope != NULL);
3237 if (scope->has_jump_unsafe_decl)
3239 if (scope == label_vars->label_bindings.scope)
3240 b = label_vars->label_bindings.bindings_in_scope;
3241 else
3242 b = scope->bindings;
3243 for (; b != NULL; b = b->prev)
3245 if (decl_jump_unsafe (b->decl))
3246 warn_about_goto (g->loc, label, b->decl);
3251 if (g->goto_bindings.stmt_exprs > 0)
3253 error_at (g->loc, "jump into statement expression");
3254 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3255 label);
3259 /* Now that the label is defined, we will issue warnings about
3260 subsequent gotos to this label when we see them. */
3261 VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3262 label_vars->gotos = NULL;
3265 /* Define a label, specifying the location in the source file.
3266 Return the LABEL_DECL node for the label, if the definition is valid.
3267 Otherwise return 0. */
3269 tree
3270 define_label (location_t location, tree name)
3272 /* Find any preexisting label with this name. It is an error
3273 if that label has already been defined in this function, or
3274 if there is a containing function with a declared label with
3275 the same name. */
3276 tree label = I_LABEL_DECL (name);
3278 if (label
3279 && ((DECL_CONTEXT (label) == current_function_decl
3280 && DECL_INITIAL (label) != 0)
3281 || (DECL_CONTEXT (label) != current_function_decl
3282 && C_DECLARED_LABEL_FLAG (label))))
3284 error_at (location, "duplicate label %qD", label);
3285 locate_old_decl (label);
3286 return 0;
3288 else if (label && DECL_CONTEXT (label) == current_function_decl)
3290 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3292 /* The label has been used or declared already in this function,
3293 but not defined. Update its location to point to this
3294 definition. */
3295 DECL_SOURCE_LOCATION (label) = location;
3296 set_spot_bindings (&label_vars->label_bindings, true);
3298 /* Issue warnings as required about any goto statements from
3299 earlier in the function. */
3300 check_earlier_gotos (label, label_vars);
3302 else
3304 struct c_label_vars *label_vars;
3306 /* No label binding for that identifier; make one. */
3307 label = make_label (location, name, true, &label_vars);
3309 /* Ordinary labels go in the current function scope. */
3310 bind_label (name, label, current_function_scope, label_vars);
3313 if (!in_system_header && lookup_name (name))
3314 warning_at (location, OPT_Wtraditional,
3315 "traditional C lacks a separate namespace "
3316 "for labels, identifier %qE conflicts", name);
3318 /* Mark label as having been defined. */
3319 DECL_INITIAL (label) = error_mark_node;
3320 return label;
3323 /* Get the bindings for a new switch statement. This is used to issue
3324 warnings as appropriate for jumps from the switch to case or
3325 default labels. */
3327 struct c_spot_bindings *
3328 c_get_switch_bindings (void)
3330 struct c_spot_bindings *switch_bindings;
3332 switch_bindings = XNEW (struct c_spot_bindings);
3333 set_spot_bindings (switch_bindings, true);
3334 return switch_bindings;
3337 void
3338 c_release_switch_bindings (struct c_spot_bindings *bindings)
3340 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3341 XDELETE (bindings);
3344 /* This is called at the point of a case or default label to issue
3345 warnings about decls as needed. It returns true if it found an
3346 error, not just a warning. */
3348 bool
3349 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3350 location_t switch_loc, location_t case_loc)
3352 bool saw_error;
3353 struct c_scope *scope;
3355 saw_error = false;
3356 for (scope = current_scope;
3357 scope != switch_bindings->scope;
3358 scope = scope->outer)
3360 struct c_binding *b;
3362 gcc_assert (scope != NULL);
3364 if (!scope->has_jump_unsafe_decl)
3365 continue;
3367 for (b = scope->bindings; b != NULL; b = b->prev)
3369 if (decl_jump_unsafe (b->decl))
3371 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3373 saw_error = true;
3374 error_at (case_loc,
3375 ("switch jumps into scope of identifier with "
3376 "variably modified type"));
3378 else
3379 warning_at (case_loc, OPT_Wjump_misses_init,
3380 "switch jumps over variable initialization");
3381 inform (switch_loc, "switch starts here");
3382 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3383 b->decl);
3388 if (switch_bindings->stmt_exprs > 0)
3390 saw_error = true;
3391 error_at (case_loc, "switch jumps into statement expression");
3392 inform (switch_loc, "switch starts here");
3395 return saw_error;
3398 /* Given NAME, an IDENTIFIER_NODE,
3399 return the structure (or union or enum) definition for that name.
3400 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3401 CODE says which kind of type the caller wants;
3402 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3403 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3404 location where the tag was defined.
3405 If the wrong kind of type is found, an error is reported. */
3407 static tree
3408 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3409 location_t *ploc)
3411 struct c_binding *b = I_TAG_BINDING (name);
3412 int thislevel = 0;
3414 if (!b || !b->decl)
3415 return 0;
3417 /* We only care about whether it's in this level if
3418 thislevel_only was set or it might be a type clash. */
3419 if (thislevel_only || TREE_CODE (b->decl) != code)
3421 /* For our purposes, a tag in the external scope is the same as
3422 a tag in the file scope. (Primarily relevant to Objective-C
3423 and its builtin structure tags, which get pushed before the
3424 file scope is created.) */
3425 if (B_IN_CURRENT_SCOPE (b)
3426 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3427 thislevel = 1;
3430 if (thislevel_only && !thislevel)
3431 return 0;
3433 if (TREE_CODE (b->decl) != code)
3435 /* Definition isn't the kind we were looking for. */
3436 pending_invalid_xref = name;
3437 pending_invalid_xref_location = input_location;
3439 /* If in the same binding level as a declaration as a tag
3440 of a different type, this must not be allowed to
3441 shadow that tag, so give the error immediately.
3442 (For example, "struct foo; union foo;" is invalid.) */
3443 if (thislevel)
3444 pending_xref_error ();
3447 if (ploc != NULL)
3448 *ploc = b->locus;
3450 return b->decl;
3453 /* Print an error message now
3454 for a recent invalid struct, union or enum cross reference.
3455 We don't print them immediately because they are not invalid
3456 when used in the `struct foo;' construct for shadowing. */
3458 void
3459 pending_xref_error (void)
3461 if (pending_invalid_xref != 0)
3462 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3463 pending_invalid_xref);
3464 pending_invalid_xref = 0;
3468 /* Look up NAME in the current scope and its superiors
3469 in the namespace of variables, functions and typedefs.
3470 Return a ..._DECL node of some kind representing its definition,
3471 or return 0 if it is undefined. */
3473 tree
3474 lookup_name (tree name)
3476 struct c_binding *b = I_SYMBOL_BINDING (name);
3477 if (b && !b->invisible)
3479 maybe_record_typedef_use (b->decl);
3480 return b->decl;
3482 return 0;
3485 /* Similar to `lookup_name' but look only at the indicated scope. */
3487 static tree
3488 lookup_name_in_scope (tree name, struct c_scope *scope)
3490 struct c_binding *b;
3492 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3493 if (B_IN_SCOPE (b, scope))
3494 return b->decl;
3495 return 0;
3498 /* Create the predefined scalar types of C,
3499 and some nodes representing standard constants (0, 1, (void *) 0).
3500 Initialize the global scope.
3501 Make definitions for built-in primitive functions. */
3503 void
3504 c_init_decl_processing (void)
3506 location_t save_loc = input_location;
3508 /* Initialize reserved words for parser. */
3509 c_parse_init ();
3511 current_function_decl = 0;
3513 gcc_obstack_init (&parser_obstack);
3515 /* Make the externals scope. */
3516 push_scope ();
3517 external_scope = current_scope;
3519 /* Declarations from c_common_nodes_and_builtins must not be associated
3520 with this input file, lest we get differences between using and not
3521 using preprocessed headers. */
3522 input_location = BUILTINS_LOCATION;
3524 c_common_nodes_and_builtins ();
3526 /* In C, comparisons and TRUTH_* expressions have type int. */
3527 truthvalue_type_node = integer_type_node;
3528 truthvalue_true_node = integer_one_node;
3529 truthvalue_false_node = integer_zero_node;
3531 /* Even in C99, which has a real boolean type. */
3532 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3533 boolean_type_node));
3535 input_location = save_loc;
3537 pedantic_lvalues = true;
3539 make_fname_decl = c_make_fname_decl;
3540 start_fname_decls ();
3543 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3544 give the decl, NAME is the initialization string and TYPE_DEP
3545 indicates whether NAME depended on the type of the function. As we
3546 don't yet implement delayed emission of static data, we mark the
3547 decl as emitted so it is not placed in the output. Anything using
3548 it must therefore pull out the STRING_CST initializer directly.
3549 FIXME. */
3551 static tree
3552 c_make_fname_decl (location_t loc, tree id, int type_dep)
3554 const char *name = fname_as_string (type_dep);
3555 tree decl, type, init;
3556 size_t length = strlen (name);
3558 type = build_array_type (char_type_node,
3559 build_index_type (size_int (length)));
3560 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3562 decl = build_decl (loc, VAR_DECL, id, type);
3564 TREE_STATIC (decl) = 1;
3565 TREE_READONLY (decl) = 1;
3566 DECL_ARTIFICIAL (decl) = 1;
3568 init = build_string (length + 1, name);
3569 free (CONST_CAST (char *, name));
3570 TREE_TYPE (init) = type;
3571 DECL_INITIAL (decl) = init;
3573 TREE_USED (decl) = 1;
3575 if (current_function_decl
3576 /* For invalid programs like this:
3578 void foo()
3579 const char* p = __FUNCTION__;
3581 the __FUNCTION__ is believed to appear in K&R style function
3582 parameter declarator. In that case we still don't have
3583 function_scope. */
3584 && (!seen_error () || current_function_scope))
3586 DECL_CONTEXT (decl) = current_function_decl;
3587 bind (id, decl, current_function_scope,
3588 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3591 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3593 return decl;
3596 tree
3597 c_builtin_function (tree decl)
3599 tree type = TREE_TYPE (decl);
3600 tree id = DECL_NAME (decl);
3602 const char *name = IDENTIFIER_POINTER (id);
3603 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3605 /* Should never be called on a symbol with a preexisting meaning. */
3606 gcc_assert (!I_SYMBOL_BINDING (id));
3608 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3609 UNKNOWN_LOCATION);
3611 /* Builtins in the implementation namespace are made visible without
3612 needing to be explicitly declared. See push_file_scope. */
3613 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3615 DECL_CHAIN (decl) = visible_builtins;
3616 visible_builtins = decl;
3619 return decl;
3622 tree
3623 c_builtin_function_ext_scope (tree decl)
3625 tree type = TREE_TYPE (decl);
3626 tree id = DECL_NAME (decl);
3628 const char *name = IDENTIFIER_POINTER (id);
3629 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3631 /* Should never be called on a symbol with a preexisting meaning. */
3632 gcc_assert (!I_SYMBOL_BINDING (id));
3634 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3635 UNKNOWN_LOCATION);
3637 /* Builtins in the implementation namespace are made visible without
3638 needing to be explicitly declared. See push_file_scope. */
3639 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3641 DECL_CHAIN (decl) = visible_builtins;
3642 visible_builtins = decl;
3645 return decl;
3648 /* Called when a declaration is seen that contains no names to declare.
3649 If its type is a reference to a structure, union or enum inherited
3650 from a containing scope, shadow that tag name for the current scope
3651 with a forward reference.
3652 If its type defines a new named structure or union
3653 or defines an enum, it is valid but we need not do anything here.
3654 Otherwise, it is an error. */
3656 void
3657 shadow_tag (const struct c_declspecs *declspecs)
3659 shadow_tag_warned (declspecs, 0);
3662 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3663 but no pedwarn. */
3664 void
3665 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3667 bool found_tag = false;
3669 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3671 tree value = declspecs->type;
3672 enum tree_code code = TREE_CODE (value);
3674 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3675 /* Used to test also that TYPE_SIZE (value) != 0.
3676 That caused warning for `struct foo;' at top level in the file. */
3678 tree name = TYPE_NAME (value);
3679 tree t;
3681 found_tag = true;
3683 if (declspecs->restrict_p)
3685 error ("invalid use of %<restrict%>");
3686 warned = 1;
3689 if (name == 0)
3691 if (warned != 1 && code != ENUMERAL_TYPE)
3692 /* Empty unnamed enum OK */
3694 pedwarn (input_location, 0,
3695 "unnamed struct/union that defines no instances");
3696 warned = 1;
3699 else if (declspecs->typespec_kind != ctsk_tagdef
3700 && declspecs->typespec_kind != ctsk_tagfirstref
3701 && declspecs->storage_class != csc_none)
3703 if (warned != 1)
3704 pedwarn (input_location, 0,
3705 "empty declaration with storage class specifier "
3706 "does not redeclare tag");
3707 warned = 1;
3708 pending_xref_error ();
3710 else if (declspecs->typespec_kind != ctsk_tagdef
3711 && declspecs->typespec_kind != ctsk_tagfirstref
3712 && (declspecs->const_p
3713 || declspecs->volatile_p
3714 || declspecs->restrict_p
3715 || declspecs->address_space))
3717 if (warned != 1)
3718 pedwarn (input_location, 0,
3719 "empty declaration with type qualifier "
3720 "does not redeclare tag");
3721 warned = 1;
3722 pending_xref_error ();
3724 else if (declspecs->typespec_kind != ctsk_tagdef
3725 && declspecs->typespec_kind != ctsk_tagfirstref
3726 && declspecs->alignas_p)
3728 if (warned != 1)
3729 pedwarn (input_location, 0,
3730 "empty declaration with %<_Alignas%> "
3731 "does not redeclare tag");
3732 warned = 1;
3733 pending_xref_error ();
3735 else
3737 pending_invalid_xref = 0;
3738 t = lookup_tag (code, name, 1, NULL);
3740 if (t == 0)
3742 t = make_node (code);
3743 pushtag (input_location, name, t);
3747 else
3749 if (warned != 1 && !in_system_header)
3751 pedwarn (input_location, 0,
3752 "useless type name in empty declaration");
3753 warned = 1;
3757 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3759 pedwarn (input_location, 0, "useless type name in empty declaration");
3760 warned = 1;
3763 pending_invalid_xref = 0;
3765 if (declspecs->inline_p)
3767 error ("%<inline%> in empty declaration");
3768 warned = 1;
3771 if (declspecs->noreturn_p)
3773 error ("%<_Noreturn%> in empty declaration");
3774 warned = 1;
3777 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3779 error ("%<auto%> in file-scope empty declaration");
3780 warned = 1;
3783 if (current_scope == file_scope && declspecs->storage_class == csc_register)
3785 error ("%<register%> in file-scope empty declaration");
3786 warned = 1;
3789 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3791 warning (0, "useless storage class specifier in empty declaration");
3792 warned = 2;
3795 if (!warned && !in_system_header && declspecs->thread_p)
3797 warning (0, "useless %<__thread%> in empty declaration");
3798 warned = 2;
3801 if (!warned && !in_system_header && (declspecs->const_p
3802 || declspecs->volatile_p
3803 || declspecs->restrict_p
3804 || declspecs->address_space))
3806 warning (0, "useless type qualifier in empty declaration");
3807 warned = 2;
3810 if (!warned && !in_system_header && declspecs->alignas_p)
3812 warning (0, "useless %<_Alignas%> in empty declaration");
3813 warned = 2;
3816 if (warned != 1)
3818 if (!found_tag)
3819 pedwarn (input_location, 0, "empty declaration");
3824 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3825 bits. SPECS represents declaration specifiers that the grammar
3826 only permits to contain type qualifiers and attributes. */
3829 quals_from_declspecs (const struct c_declspecs *specs)
3831 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3832 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3833 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3834 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3835 gcc_assert (!specs->type
3836 && !specs->decl_attr
3837 && specs->typespec_word == cts_none
3838 && specs->storage_class == csc_none
3839 && !specs->typedef_p
3840 && !specs->explicit_signed_p
3841 && !specs->deprecated_p
3842 && !specs->long_p
3843 && !specs->long_long_p
3844 && !specs->short_p
3845 && !specs->signed_p
3846 && !specs->unsigned_p
3847 && !specs->complex_p
3848 && !specs->inline_p
3849 && !specs->noreturn_p
3850 && !specs->thread_p);
3851 return quals;
3854 /* Construct an array declarator. LOC is the location of the
3855 beginning of the array (usually the opening brace). EXPR is the
3856 expression inside [], or NULL_TREE. QUALS are the type qualifiers
3857 inside the [] (to be applied to the pointer to which a parameter
3858 array is converted). STATIC_P is true if "static" is inside the
3859 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3860 VLA of unspecified length which is nevertheless a complete type,
3861 false otherwise. The field for the contained declarator is left to
3862 be filled in by set_array_declarator_inner. */
3864 struct c_declarator *
3865 build_array_declarator (location_t loc,
3866 tree expr, struct c_declspecs *quals, bool static_p,
3867 bool vla_unspec_p)
3869 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3870 struct c_declarator);
3871 declarator->id_loc = loc;
3872 declarator->kind = cdk_array;
3873 declarator->declarator = 0;
3874 declarator->u.array.dimen = expr;
3875 if (quals)
3877 declarator->u.array.attrs = quals->attrs;
3878 declarator->u.array.quals = quals_from_declspecs (quals);
3880 else
3882 declarator->u.array.attrs = NULL_TREE;
3883 declarator->u.array.quals = 0;
3885 declarator->u.array.static_p = static_p;
3886 declarator->u.array.vla_unspec_p = vla_unspec_p;
3887 if (!flag_isoc99)
3889 if (static_p || quals != NULL)
3890 pedwarn (loc, OPT_Wpedantic,
3891 "ISO C90 does not support %<static%> or type "
3892 "qualifiers in parameter array declarators");
3893 if (vla_unspec_p)
3894 pedwarn (loc, OPT_Wpedantic,
3895 "ISO C90 does not support %<[*]%> array declarators");
3897 if (vla_unspec_p)
3899 if (!current_scope->parm_flag)
3901 /* C99 6.7.5.2p4 */
3902 error_at (loc, "%<[*]%> not allowed in other than "
3903 "function prototype scope");
3904 declarator->u.array.vla_unspec_p = false;
3905 return NULL;
3907 current_scope->had_vla_unspec = true;
3909 return declarator;
3912 /* Set the contained declarator of an array declarator. DECL is the
3913 declarator, as constructed by build_array_declarator; INNER is what
3914 appears on the left of the []. */
3916 struct c_declarator *
3917 set_array_declarator_inner (struct c_declarator *decl,
3918 struct c_declarator *inner)
3920 decl->declarator = inner;
3921 return decl;
3924 /* INIT is a constructor that forms DECL's initializer. If the final
3925 element initializes a flexible array field, add the size of that
3926 initializer to DECL's size. */
3928 static void
3929 add_flexible_array_elts_to_size (tree decl, tree init)
3931 tree elt, type;
3933 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3934 return;
3936 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3937 type = TREE_TYPE (elt);
3938 if (TREE_CODE (type) == ARRAY_TYPE
3939 && TYPE_SIZE (type) == NULL_TREE
3940 && TYPE_DOMAIN (type) != NULL_TREE
3941 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3943 complete_array_type (&type, elt, false);
3944 DECL_SIZE (decl)
3945 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3946 DECL_SIZE_UNIT (decl)
3947 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3951 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3952 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3953 before the type name, and set *EXPR_CONST_OPERANDS, if
3954 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3955 appear in a constant expression. */
3957 tree
3958 groktypename (struct c_type_name *type_name, tree *expr,
3959 bool *expr_const_operands)
3961 tree type;
3962 tree attrs = type_name->specs->attrs;
3964 type_name->specs->attrs = NULL_TREE;
3966 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3967 false, NULL, &attrs, expr, expr_const_operands,
3968 DEPRECATED_NORMAL);
3970 /* Apply attributes. */
3971 decl_attributes (&type, attrs, 0);
3973 return type;
3976 /* Decode a declarator in an ordinary declaration or data definition.
3977 This is called as soon as the type information and variable name
3978 have been parsed, before parsing the initializer if any.
3979 Here we create the ..._DECL node, fill in its type,
3980 and put it on the list of decls for the current context.
3981 The ..._DECL node is returned as the value.
3983 Exception: for arrays where the length is not specified,
3984 the type is left null, to be filled in by `finish_decl'.
3986 Function definitions do not come here; they go to start_function
3987 instead. However, external and forward declarations of functions
3988 do go through here. Structure field declarations are done by
3989 grokfield and not through here. */
3991 tree
3992 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3993 bool initialized, tree attributes)
3995 tree decl;
3996 tree tem;
3997 tree expr = NULL_TREE;
3998 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4000 /* An object declared as __attribute__((deprecated)) suppresses
4001 warnings of uses of other deprecated items. */
4002 if (lookup_attribute ("deprecated", attributes))
4003 deprecated_state = DEPRECATED_SUPPRESS;
4005 decl = grokdeclarator (declarator, declspecs,
4006 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4007 deprecated_state);
4008 if (!decl)
4009 return 0;
4011 if (expr)
4012 add_stmt (fold_convert (void_type_node, expr));
4014 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4015 warning (OPT_Wmain, "%q+D is usually a function", decl);
4017 if (initialized)
4018 /* Is it valid for this decl to have an initializer at all?
4019 If not, set INITIALIZED to zero, which will indirectly
4020 tell 'finish_decl' to ignore the initializer once it is parsed. */
4021 switch (TREE_CODE (decl))
4023 case TYPE_DECL:
4024 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4025 initialized = 0;
4026 break;
4028 case FUNCTION_DECL:
4029 error ("function %qD is initialized like a variable", decl);
4030 initialized = 0;
4031 break;
4033 case PARM_DECL:
4034 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4035 error ("parameter %qD is initialized", decl);
4036 initialized = 0;
4037 break;
4039 default:
4040 /* Don't allow initializations for incomplete types except for
4041 arrays which might be completed by the initialization. */
4043 /* This can happen if the array size is an undefined macro.
4044 We already gave a warning, so we don't need another one. */
4045 if (TREE_TYPE (decl) == error_mark_node)
4046 initialized = 0;
4047 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4049 /* A complete type is ok if size is fixed. */
4051 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4052 || C_DECL_VARIABLE_SIZE (decl))
4054 error ("variable-sized object may not be initialized");
4055 initialized = 0;
4058 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4060 error ("variable %qD has initializer but incomplete type", decl);
4061 initialized = 0;
4063 else if (C_DECL_VARIABLE_SIZE (decl))
4065 /* Although C99 is unclear about whether incomplete arrays
4066 of VLAs themselves count as VLAs, it does not make
4067 sense to permit them to be initialized given that
4068 ordinary VLAs may not be initialized. */
4069 error ("variable-sized object may not be initialized");
4070 initialized = 0;
4074 if (initialized)
4076 if (current_scope == file_scope)
4077 TREE_STATIC (decl) = 1;
4079 /* Tell 'pushdecl' this is an initialized decl
4080 even though we don't yet have the initializer expression.
4081 Also tell 'finish_decl' it may store the real initializer. */
4082 DECL_INITIAL (decl) = error_mark_node;
4085 /* If this is a function declaration, write a record describing it to the
4086 prototypes file (if requested). */
4088 if (TREE_CODE (decl) == FUNCTION_DECL)
4089 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4091 /* ANSI specifies that a tentative definition which is not merged with
4092 a non-tentative definition behaves exactly like a definition with an
4093 initializer equal to zero. (Section 3.7.2)
4095 -fno-common gives strict ANSI behavior, though this tends to break
4096 a large body of code that grew up without this rule.
4098 Thread-local variables are never common, since there's no entrenched
4099 body of code to break, and it allows more efficient variable references
4100 in the presence of dynamic linking. */
4102 if (TREE_CODE (decl) == VAR_DECL
4103 && !initialized
4104 && TREE_PUBLIC (decl)
4105 && !DECL_THREAD_LOCAL_P (decl)
4106 && !flag_no_common)
4107 DECL_COMMON (decl) = 1;
4109 /* Set attributes here so if duplicate decl, will have proper attributes. */
4110 decl_attributes (&decl, attributes, 0);
4112 /* Handle gnu_inline attribute. */
4113 if (declspecs->inline_p
4114 && !flag_gnu89_inline
4115 && TREE_CODE (decl) == FUNCTION_DECL
4116 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4117 || current_function_decl))
4119 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4121 else if (declspecs->storage_class != csc_static)
4122 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4125 if (TREE_CODE (decl) == FUNCTION_DECL
4126 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4128 struct c_declarator *ce = declarator;
4130 if (ce->kind == cdk_pointer)
4131 ce = declarator->declarator;
4132 if (ce->kind == cdk_function)
4134 tree args = ce->u.arg_info->parms;
4135 for (; args; args = DECL_CHAIN (args))
4137 tree type = TREE_TYPE (args);
4138 if (type && INTEGRAL_TYPE_P (type)
4139 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4140 DECL_ARG_TYPE (args) = integer_type_node;
4145 if (TREE_CODE (decl) == FUNCTION_DECL
4146 && DECL_DECLARED_INLINE_P (decl)
4147 && DECL_UNINLINABLE (decl)
4148 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4149 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4150 decl);
4152 /* C99 6.7.4p3: An inline definition of a function with external
4153 linkage shall not contain a definition of a modifiable object
4154 with static storage duration... */
4155 if (TREE_CODE (decl) == VAR_DECL
4156 && current_scope != file_scope
4157 && TREE_STATIC (decl)
4158 && !TREE_READONLY (decl)
4159 && DECL_DECLARED_INLINE_P (current_function_decl)
4160 && DECL_EXTERNAL (current_function_decl))
4161 record_inline_static (input_location, current_function_decl,
4162 decl, csi_modifiable);
4164 if (c_dialect_objc ()
4165 && (TREE_CODE (decl) == VAR_DECL
4166 || TREE_CODE (decl) == FUNCTION_DECL))
4167 objc_check_global_decl (decl);
4169 /* Add this decl to the current scope.
4170 TEM may equal DECL or it may be a previous decl of the same name. */
4171 tem = pushdecl (decl);
4173 if (initialized && DECL_EXTERNAL (tem))
4175 DECL_EXTERNAL (tem) = 0;
4176 TREE_STATIC (tem) = 1;
4179 return tem;
4182 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4183 DECL or the non-array element type if DECL is an uninitialized array.
4184 If that type has a const member, diagnose this. */
4186 static void
4187 diagnose_uninitialized_cst_member (tree decl, tree type)
4189 tree field;
4190 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4192 tree field_type;
4193 if (TREE_CODE (field) != FIELD_DECL)
4194 continue;
4195 field_type = strip_array_types (TREE_TYPE (field));
4197 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4199 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4200 "uninitialized const member in %qT is invalid in C++",
4201 strip_array_types (TREE_TYPE (decl)));
4202 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4205 if (TREE_CODE (field_type) == RECORD_TYPE
4206 || TREE_CODE (field_type) == UNION_TYPE)
4207 diagnose_uninitialized_cst_member (decl, field_type);
4211 /* Finish processing of a declaration;
4212 install its initial value.
4213 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4214 If the length of an array type is not known before,
4215 it must be determined now, from the initial value, or it is an error.
4217 INIT_LOC is the location of the initial value. */
4219 void
4220 finish_decl (tree decl, location_t init_loc, tree init,
4221 tree origtype, tree asmspec_tree)
4223 tree type;
4224 bool was_incomplete = (DECL_SIZE (decl) == 0);
4225 const char *asmspec = 0;
4227 /* If a name was specified, get the string. */
4228 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4229 && DECL_FILE_SCOPE_P (decl))
4230 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4231 if (asmspec_tree)
4232 asmspec = TREE_STRING_POINTER (asmspec_tree);
4234 if (TREE_CODE (decl) == VAR_DECL
4235 && TREE_STATIC (decl)
4236 && global_bindings_p ())
4237 /* So decl is a global variable. Record the types it uses
4238 so that we can decide later to emit debug info for them. */
4239 record_types_used_by_current_var_decl (decl);
4241 /* If `start_decl' didn't like having an initialization, ignore it now. */
4242 if (init != 0 && DECL_INITIAL (decl) == 0)
4243 init = 0;
4245 /* Don't crash if parm is initialized. */
4246 if (TREE_CODE (decl) == PARM_DECL)
4247 init = 0;
4249 if (init)
4250 store_init_value (init_loc, decl, init, origtype);
4252 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4253 || TREE_CODE (decl) == FUNCTION_DECL
4254 || TREE_CODE (decl) == FIELD_DECL))
4255 objc_check_decl (decl);
4257 type = TREE_TYPE (decl);
4259 /* Deduce size of array from initialization, if not already known. */
4260 if (TREE_CODE (type) == ARRAY_TYPE
4261 && TYPE_DOMAIN (type) == 0
4262 && TREE_CODE (decl) != TYPE_DECL)
4264 bool do_default
4265 = (TREE_STATIC (decl)
4266 /* Even if pedantic, an external linkage array
4267 may have incomplete type at first. */
4268 ? pedantic && !TREE_PUBLIC (decl)
4269 : !DECL_EXTERNAL (decl));
4270 int failure
4271 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4272 do_default);
4274 /* Get the completed type made by complete_array_type. */
4275 type = TREE_TYPE (decl);
4277 switch (failure)
4279 case 1:
4280 error ("initializer fails to determine size of %q+D", decl);
4281 break;
4283 case 2:
4284 if (do_default)
4285 error ("array size missing in %q+D", decl);
4286 /* If a `static' var's size isn't known,
4287 make it extern as well as static, so it does not get
4288 allocated.
4289 If it is not `static', then do not mark extern;
4290 finish_incomplete_decl will give it a default size
4291 and it will get allocated. */
4292 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4293 DECL_EXTERNAL (decl) = 1;
4294 break;
4296 case 3:
4297 error ("zero or negative size array %q+D", decl);
4298 break;
4300 case 0:
4301 /* For global variables, update the copy of the type that
4302 exists in the binding. */
4303 if (TREE_PUBLIC (decl))
4305 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4306 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4307 b_ext = b_ext->shadowed;
4308 if (b_ext)
4310 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4311 b_ext->u.type = composite_type (b_ext->u.type, type);
4312 else
4313 b_ext->u.type = type;
4316 break;
4318 default:
4319 gcc_unreachable ();
4322 if (DECL_INITIAL (decl))
4323 TREE_TYPE (DECL_INITIAL (decl)) = type;
4325 relayout_decl (decl);
4328 if (TREE_CODE (decl) == VAR_DECL)
4330 if (init && TREE_CODE (init) == CONSTRUCTOR)
4331 add_flexible_array_elts_to_size (decl, init);
4333 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4334 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4335 layout_decl (decl, 0);
4337 if (DECL_SIZE (decl) == 0
4338 /* Don't give an error if we already gave one earlier. */
4339 && TREE_TYPE (decl) != error_mark_node
4340 && (TREE_STATIC (decl)
4341 /* A static variable with an incomplete type
4342 is an error if it is initialized.
4343 Also if it is not file scope.
4344 Otherwise, let it through, but if it is not `extern'
4345 then it may cause an error message later. */
4346 ? (DECL_INITIAL (decl) != 0
4347 || !DECL_FILE_SCOPE_P (decl))
4348 /* An automatic variable with an incomplete type
4349 is an error. */
4350 : !DECL_EXTERNAL (decl)))
4352 error ("storage size of %q+D isn%'t known", decl);
4353 TREE_TYPE (decl) = error_mark_node;
4356 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4357 && DECL_SIZE (decl) != 0)
4359 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4360 constant_expression_warning (DECL_SIZE (decl));
4361 else
4363 error ("storage size of %q+D isn%'t constant", decl);
4364 TREE_TYPE (decl) = error_mark_node;
4368 if (TREE_USED (type))
4370 TREE_USED (decl) = 1;
4371 DECL_READ_P (decl) = 1;
4375 /* If this is a function and an assembler name is specified, reset DECL_RTL
4376 so we can give it its new name. Also, update builtin_decl if it
4377 was a normal built-in. */
4378 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4380 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4381 set_builtin_user_assembler_name (decl, asmspec);
4382 set_user_assembler_name (decl, asmspec);
4385 /* If #pragma weak was used, mark the decl weak now. */
4386 maybe_apply_pragma_weak (decl);
4388 /* Output the assembler code and/or RTL code for variables and functions,
4389 unless the type is an undefined structure or union.
4390 If not, it will get done when the type is completed. */
4392 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4394 /* Determine the ELF visibility. */
4395 if (TREE_PUBLIC (decl))
4396 c_determine_visibility (decl);
4398 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4399 if (c_dialect_objc ())
4400 objc_check_decl (decl);
4402 if (asmspec)
4404 /* If this is not a static variable, issue a warning.
4405 It doesn't make any sense to give an ASMSPEC for an
4406 ordinary, non-register local variable. Historically,
4407 GCC has accepted -- but ignored -- the ASMSPEC in
4408 this case. */
4409 if (!DECL_FILE_SCOPE_P (decl)
4410 && TREE_CODE (decl) == VAR_DECL
4411 && !C_DECL_REGISTER (decl)
4412 && !TREE_STATIC (decl))
4413 warning (0, "ignoring asm-specifier for non-static local "
4414 "variable %q+D", decl);
4415 else
4416 set_user_assembler_name (decl, asmspec);
4419 if (DECL_FILE_SCOPE_P (decl))
4421 if (DECL_INITIAL (decl) == NULL_TREE
4422 || DECL_INITIAL (decl) == error_mark_node)
4423 /* Don't output anything
4424 when a tentative file-scope definition is seen.
4425 But at end of compilation, do output code for them. */
4426 DECL_DEFER_OUTPUT (decl) = 1;
4427 if (asmspec && C_DECL_REGISTER (decl))
4428 DECL_HARD_REGISTER (decl) = 1;
4429 rest_of_decl_compilation (decl, true, 0);
4431 else
4433 /* In conjunction with an ASMSPEC, the `register'
4434 keyword indicates that we should place the variable
4435 in a particular register. */
4436 if (asmspec && C_DECL_REGISTER (decl))
4438 DECL_HARD_REGISTER (decl) = 1;
4439 /* This cannot be done for a structure with volatile
4440 fields, on which DECL_REGISTER will have been
4441 reset. */
4442 if (!DECL_REGISTER (decl))
4443 error ("cannot put object with volatile field into register");
4446 if (TREE_CODE (decl) != FUNCTION_DECL)
4448 /* If we're building a variable sized type, and we might be
4449 reachable other than via the top of the current binding
4450 level, then create a new BIND_EXPR so that we deallocate
4451 the object at the right time. */
4452 /* Note that DECL_SIZE can be null due to errors. */
4453 if (DECL_SIZE (decl)
4454 && !TREE_CONSTANT (DECL_SIZE (decl))
4455 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4457 tree bind;
4458 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4459 TREE_SIDE_EFFECTS (bind) = 1;
4460 add_stmt (bind);
4461 BIND_EXPR_BODY (bind) = push_stmt_list ();
4463 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4464 DECL_EXPR, decl));
4469 if (!DECL_FILE_SCOPE_P (decl))
4471 /* Recompute the RTL of a local array now
4472 if it used to be an incomplete type. */
4473 if (was_incomplete
4474 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4476 /* If we used it already as memory, it must stay in memory. */
4477 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4478 /* If it's still incomplete now, no init will save it. */
4479 if (DECL_SIZE (decl) == 0)
4480 DECL_INITIAL (decl) = 0;
4485 if (TREE_CODE (decl) == TYPE_DECL)
4487 if (!DECL_FILE_SCOPE_P (decl)
4488 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4489 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4491 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4494 /* Install a cleanup (aka destructor) if one was given. */
4495 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4497 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4498 if (attr)
4500 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4501 tree cleanup_decl = lookup_name (cleanup_id);
4502 tree cleanup;
4503 VEC(tree,gc) *vec;
4505 /* Build "cleanup(&decl)" for the destructor. */
4506 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4507 vec = VEC_alloc (tree, gc, 1);
4508 VEC_quick_push (tree, vec, cleanup);
4509 cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4510 cleanup_decl, vec, NULL);
4511 VEC_free (tree, gc, vec);
4513 /* Don't warn about decl unused; the cleanup uses it. */
4514 TREE_USED (decl) = 1;
4515 TREE_USED (cleanup_decl) = 1;
4516 DECL_READ_P (decl) = 1;
4518 push_cleanup (decl, cleanup, false);
4522 if (warn_cxx_compat
4523 && TREE_CODE (decl) == VAR_DECL
4524 && !DECL_EXTERNAL (decl)
4525 && DECL_INITIAL (decl) == NULL_TREE)
4527 type = strip_array_types (type);
4528 if (TREE_READONLY (decl))
4529 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4530 "uninitialized const %qD is invalid in C++", decl);
4531 else if ((TREE_CODE (type) == RECORD_TYPE
4532 || TREE_CODE (type) == UNION_TYPE)
4533 && C_TYPE_FIELDS_READONLY (type))
4534 diagnose_uninitialized_cst_member (decl, type);
4537 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4540 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4541 EXPR is NULL or a pointer to an expression that needs to be
4542 evaluated for the side effects of array size expressions in the
4543 parameters. */
4545 tree
4546 grokparm (const struct c_parm *parm, tree *expr)
4548 tree attrs = parm->attrs;
4549 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4550 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4552 decl_attributes (&decl, attrs, 0);
4554 return decl;
4557 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4558 and push that on the current scope. EXPR is a pointer to an
4559 expression that needs to be evaluated for the side effects of array
4560 size expressions in the parameters. */
4562 void
4563 push_parm_decl (const struct c_parm *parm, tree *expr)
4565 tree attrs = parm->attrs;
4566 tree decl;
4568 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4569 &attrs, expr, NULL, DEPRECATED_NORMAL);
4570 decl_attributes (&decl, attrs, 0);
4572 decl = pushdecl (decl);
4574 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4577 /* Mark all the parameter declarations to date as forward decls.
4578 Also diagnose use of this extension. */
4580 void
4581 mark_forward_parm_decls (void)
4583 struct c_binding *b;
4585 if (pedantic && !current_scope->warned_forward_parm_decls)
4587 pedwarn (input_location, OPT_Wpedantic,
4588 "ISO C forbids forward parameter declarations");
4589 current_scope->warned_forward_parm_decls = true;
4592 for (b = current_scope->bindings; b; b = b->prev)
4593 if (TREE_CODE (b->decl) == PARM_DECL)
4594 TREE_ASM_WRITTEN (b->decl) = 1;
4597 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4598 literal, which may be an incomplete array type completed by the
4599 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4600 literal. NON_CONST is true if the initializers contain something
4601 that cannot occur in a constant expression. */
4603 tree
4604 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4606 /* We do not use start_decl here because we have a type, not a declarator;
4607 and do not use finish_decl because the decl should be stored inside
4608 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4609 tree decl;
4610 tree complit;
4611 tree stmt;
4613 if (type == error_mark_node
4614 || init == error_mark_node)
4615 return error_mark_node;
4617 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4618 DECL_EXTERNAL (decl) = 0;
4619 TREE_PUBLIC (decl) = 0;
4620 TREE_STATIC (decl) = (current_scope == file_scope);
4621 DECL_CONTEXT (decl) = current_function_decl;
4622 TREE_USED (decl) = 1;
4623 DECL_READ_P (decl) = 1;
4624 TREE_TYPE (decl) = type;
4625 TREE_READONLY (decl) = TYPE_READONLY (type);
4626 store_init_value (loc, decl, init, NULL_TREE);
4628 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4630 int failure = complete_array_type (&TREE_TYPE (decl),
4631 DECL_INITIAL (decl), true);
4632 gcc_assert (!failure);
4634 type = TREE_TYPE (decl);
4635 TREE_TYPE (DECL_INITIAL (decl)) = type;
4638 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4639 return error_mark_node;
4641 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4642 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4643 TREE_SIDE_EFFECTS (complit) = 1;
4645 layout_decl (decl, 0);
4647 if (TREE_STATIC (decl))
4649 /* This decl needs a name for the assembler output. */
4650 set_compound_literal_name (decl);
4651 DECL_DEFER_OUTPUT (decl) = 1;
4652 DECL_COMDAT (decl) = 1;
4653 DECL_ARTIFICIAL (decl) = 1;
4654 DECL_IGNORED_P (decl) = 1;
4655 pushdecl (decl);
4656 rest_of_decl_compilation (decl, 1, 0);
4659 if (non_const)
4661 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4662 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4665 return complit;
4668 /* Check the type of a compound literal. Here we just check that it
4669 is valid for C++. */
4671 void
4672 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4674 if (warn_cxx_compat
4675 && (type_name->specs->typespec_kind == ctsk_tagdef
4676 || type_name->specs->typespec_kind == ctsk_tagfirstref))
4677 warning_at (loc, OPT_Wc___compat,
4678 "defining a type in a compound literal is invalid in C++");
4681 /* Determine whether TYPE is a structure with a flexible array member,
4682 or a union containing such a structure (possibly recursively). */
4684 static bool
4685 flexible_array_type_p (tree type)
4687 tree x;
4688 switch (TREE_CODE (type))
4690 case RECORD_TYPE:
4691 x = TYPE_FIELDS (type);
4692 if (x == NULL_TREE)
4693 return false;
4694 while (DECL_CHAIN (x) != NULL_TREE)
4695 x = DECL_CHAIN (x);
4696 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4697 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4698 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4699 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4700 return true;
4701 return false;
4702 case UNION_TYPE:
4703 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4705 if (flexible_array_type_p (TREE_TYPE (x)))
4706 return true;
4708 return false;
4709 default:
4710 return false;
4714 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4715 replacing with appropriate values if they are invalid. */
4716 static void
4717 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4719 tree type_mv;
4720 unsigned int max_width;
4721 unsigned HOST_WIDE_INT w;
4722 const char *name = (orig_name
4723 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4724 : _("<anonymous>"));
4726 /* Detect and ignore out of range field width and process valid
4727 field widths. */
4728 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4730 error ("bit-field %qs width not an integer constant", name);
4731 *width = integer_one_node;
4733 else
4735 if (TREE_CODE (*width) != INTEGER_CST)
4737 *width = c_fully_fold (*width, false, NULL);
4738 if (TREE_CODE (*width) == INTEGER_CST)
4739 pedwarn (input_location, OPT_Wpedantic,
4740 "bit-field %qs width not an integer constant expression",
4741 name);
4743 if (TREE_CODE (*width) != INTEGER_CST)
4745 error ("bit-field %qs width not an integer constant", name);
4746 *width = integer_one_node;
4748 constant_expression_warning (*width);
4749 if (tree_int_cst_sgn (*width) < 0)
4751 error ("negative width in bit-field %qs", name);
4752 *width = integer_one_node;
4754 else if (integer_zerop (*width) && orig_name)
4756 error ("zero width for bit-field %qs", name);
4757 *width = integer_one_node;
4761 /* Detect invalid bit-field type. */
4762 if (TREE_CODE (*type) != INTEGER_TYPE
4763 && TREE_CODE (*type) != BOOLEAN_TYPE
4764 && TREE_CODE (*type) != ENUMERAL_TYPE)
4766 error ("bit-field %qs has invalid type", name);
4767 *type = unsigned_type_node;
4770 type_mv = TYPE_MAIN_VARIANT (*type);
4771 if (!in_system_header
4772 && type_mv != integer_type_node
4773 && type_mv != unsigned_type_node
4774 && type_mv != boolean_type_node)
4775 pedwarn (input_location, OPT_Wpedantic,
4776 "type of bit-field %qs is a GCC extension", name);
4778 max_width = TYPE_PRECISION (*type);
4780 if (0 < compare_tree_int (*width, max_width))
4782 error ("width of %qs exceeds its type", name);
4783 w = max_width;
4784 *width = build_int_cst (integer_type_node, w);
4786 else
4787 w = tree_low_cst (*width, 1);
4789 if (TREE_CODE (*type) == ENUMERAL_TYPE)
4791 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4792 if (!lt
4793 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4794 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4795 warning (0, "%qs is narrower than values of its type", name);
4801 /* Print warning about variable length array if necessary. */
4803 static void
4804 warn_variable_length_array (tree name, tree size)
4806 int const_size = TREE_CONSTANT (size);
4808 if (!flag_isoc99 && pedantic && warn_vla != 0)
4810 if (const_size)
4812 if (name)
4813 pedwarn (input_location, OPT_Wvla,
4814 "ISO C90 forbids array %qE whose size "
4815 "can%'t be evaluated",
4816 name);
4817 else
4818 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4819 "can%'t be evaluated");
4821 else
4823 if (name)
4824 pedwarn (input_location, OPT_Wvla,
4825 "ISO C90 forbids variable length array %qE",
4826 name);
4827 else
4828 pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4831 else if (warn_vla > 0)
4833 if (const_size)
4835 if (name)
4836 warning (OPT_Wvla,
4837 "the size of array %qE can"
4838 "%'t be evaluated", name);
4839 else
4840 warning (OPT_Wvla,
4841 "the size of array can %'t be evaluated");
4843 else
4845 if (name)
4846 warning (OPT_Wvla,
4847 "variable length array %qE is used",
4848 name);
4849 else
4850 warning (OPT_Wvla,
4851 "variable length array is used");
4856 /* Given declspecs and a declarator,
4857 determine the name and type of the object declared
4858 and construct a ..._DECL node for it.
4859 (In one case we can return a ..._TYPE node instead.
4860 For invalid input we sometimes return 0.)
4862 DECLSPECS is a c_declspecs structure for the declaration specifiers.
4864 DECL_CONTEXT says which syntactic context this declaration is in:
4865 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4866 FUNCDEF for a function definition. Like NORMAL but a few different
4867 error messages in each case. Return value may be zero meaning
4868 this definition is too screwy to try to parse.
4869 PARM for a parameter declaration (either within a function prototype
4870 or before a function body). Make a PARM_DECL, or return void_type_node.
4871 TYPENAME if for a typename (in a cast or sizeof).
4872 Don't make a DECL node; just return the ..._TYPE node.
4873 FIELD for a struct or union field; make a FIELD_DECL.
4874 INITIALIZED is true if the decl has an initializer.
4875 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4876 representing the width of the bit-field.
4877 DECL_ATTRS points to the list of attributes that should be added to this
4878 decl. Any nested attributes that belong on the decl itself will be
4879 added to this list.
4880 If EXPR is not NULL, any expressions that need to be evaluated as
4881 part of evaluating variably modified types will be stored in *EXPR.
4882 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4883 set to indicate whether operands in *EXPR can be used in constant
4884 expressions.
4885 DEPRECATED_STATE is a deprecated_states value indicating whether
4886 deprecation warnings should be suppressed.
4888 In the TYPENAME case, DECLARATOR is really an absolute declarator.
4889 It may also be so in the PARM case, for a prototype where the
4890 argument type is specified but not the name.
4892 This function is where the complicated C meanings of `static'
4893 and `extern' are interpreted. */
4895 static tree
4896 grokdeclarator (const struct c_declarator *declarator,
4897 struct c_declspecs *declspecs,
4898 enum decl_context decl_context, bool initialized, tree *width,
4899 tree *decl_attrs, tree *expr, bool *expr_const_operands,
4900 enum deprecated_states deprecated_state)
4902 tree type = declspecs->type;
4903 bool threadp = declspecs->thread_p;
4904 enum c_storage_class storage_class = declspecs->storage_class;
4905 int constp;
4906 int restrictp;
4907 int volatilep;
4908 int type_quals = TYPE_UNQUALIFIED;
4909 tree name = NULL_TREE;
4910 bool funcdef_flag = false;
4911 bool funcdef_syntax = false;
4912 bool size_varies = false;
4913 tree decl_attr = declspecs->decl_attr;
4914 int array_ptr_quals = TYPE_UNQUALIFIED;
4915 tree array_ptr_attrs = NULL_TREE;
4916 int array_parm_static = 0;
4917 bool array_parm_vla_unspec_p = false;
4918 tree returned_attrs = NULL_TREE;
4919 bool bitfield = width != NULL;
4920 tree element_type;
4921 struct c_arg_info *arg_info = 0;
4922 addr_space_t as1, as2, address_space;
4923 location_t loc = UNKNOWN_LOCATION;
4924 const char *errmsg;
4925 tree expr_dummy;
4926 bool expr_const_operands_dummy;
4927 enum c_declarator_kind first_non_attr_kind;
4928 unsigned int alignas_align = 0;
4930 if (TREE_CODE (type) == ERROR_MARK)
4931 return error_mark_node;
4932 if (expr == NULL)
4933 expr = &expr_dummy;
4934 if (expr_const_operands == NULL)
4935 expr_const_operands = &expr_const_operands_dummy;
4937 *expr = declspecs->expr;
4938 *expr_const_operands = declspecs->expr_const_operands;
4940 if (decl_context == FUNCDEF)
4941 funcdef_flag = true, decl_context = NORMAL;
4943 /* Look inside a declarator for the name being declared
4944 and get it as an IDENTIFIER_NODE, for an error message. */
4946 const struct c_declarator *decl = declarator;
4948 first_non_attr_kind = cdk_attrs;
4949 while (decl)
4950 switch (decl->kind)
4952 case cdk_array:
4953 loc = decl->id_loc;
4954 /* FALL THRU. */
4956 case cdk_function:
4957 case cdk_pointer:
4958 funcdef_syntax = (decl->kind == cdk_function);
4959 decl = decl->declarator;
4960 if (first_non_attr_kind == cdk_attrs)
4961 first_non_attr_kind = decl->kind;
4962 break;
4964 case cdk_attrs:
4965 decl = decl->declarator;
4966 break;
4968 case cdk_id:
4969 loc = decl->id_loc;
4970 if (decl->u.id)
4971 name = decl->u.id;
4972 if (first_non_attr_kind == cdk_attrs)
4973 first_non_attr_kind = decl->kind;
4974 decl = 0;
4975 break;
4977 default:
4978 gcc_unreachable ();
4980 if (name == 0)
4982 gcc_assert (decl_context == PARM
4983 || decl_context == TYPENAME
4984 || (decl_context == FIELD
4985 && declarator->kind == cdk_id));
4986 gcc_assert (!initialized);
4990 /* A function definition's declarator must have the form of
4991 a function declarator. */
4993 if (funcdef_flag && !funcdef_syntax)
4994 return 0;
4996 /* If this looks like a function definition, make it one,
4997 even if it occurs where parms are expected.
4998 Then store_parm_decls will reject it and not use it as a parm. */
4999 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5000 decl_context = PARM;
5002 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5003 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5005 if ((decl_context == NORMAL || decl_context == FIELD)
5006 && current_scope == file_scope
5007 && variably_modified_type_p (type, NULL_TREE))
5009 if (name)
5010 error_at (loc, "variably modified %qE at file scope", name);
5011 else
5012 error_at (loc, "variably modified field at file scope");
5013 type = integer_type_node;
5016 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5018 /* Diagnose defaulting to "int". */
5020 if (declspecs->default_int_p && !in_system_header)
5022 /* Issue a warning if this is an ISO C 99 program or if
5023 -Wreturn-type and this is a function, or if -Wimplicit;
5024 prefer the former warning since it is more explicit. */
5025 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5026 && funcdef_flag)
5027 warn_about_return_type = 1;
5028 else
5030 if (name)
5031 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5032 "type defaults to %<int%> in declaration of %qE",
5033 name);
5034 else
5035 pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5036 "type defaults to %<int%> in type name");
5040 /* Adjust the type if a bit-field is being declared,
5041 -funsigned-bitfields applied and the type is not explicitly
5042 "signed". */
5043 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5044 && TREE_CODE (type) == INTEGER_TYPE)
5045 type = unsigned_type_for (type);
5047 /* Figure out the type qualifiers for the declaration. There are
5048 two ways a declaration can become qualified. One is something
5049 like `const int i' where the `const' is explicit. Another is
5050 something like `typedef const int CI; CI i' where the type of the
5051 declaration contains the `const'. A third possibility is that
5052 there is a type qualifier on the element type of a typedefed
5053 array type, in which case we should extract that qualifier so
5054 that c_apply_type_quals_to_decl receives the full list of
5055 qualifiers to work with (C90 is not entirely clear about whether
5056 duplicate qualifiers should be diagnosed in this case, but it
5057 seems most appropriate to do so). */
5058 element_type = strip_array_types (type);
5059 constp = declspecs->const_p + TYPE_READONLY (element_type);
5060 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5061 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5062 as1 = declspecs->address_space;
5063 as2 = TYPE_ADDR_SPACE (element_type);
5064 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5066 if (pedantic && !flag_isoc99)
5068 if (constp > 1)
5069 pedwarn (loc, OPT_Wpedantic, "duplicate %<const%>");
5070 if (restrictp > 1)
5071 pedwarn (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5072 if (volatilep > 1)
5073 pedwarn (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5076 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5077 error_at (loc, "conflicting named address spaces (%s vs %s)",
5078 c_addr_space_name (as1), c_addr_space_name (as2));
5080 if ((TREE_CODE (type) == ARRAY_TYPE
5081 || first_non_attr_kind == cdk_array)
5082 && TYPE_QUALS (element_type))
5083 type = TYPE_MAIN_VARIANT (type);
5084 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5085 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5086 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5087 | ENCODE_QUAL_ADDR_SPACE (address_space));
5089 /* Warn about storage classes that are invalid for certain
5090 kinds of declarations (parameters, typenames, etc.). */
5092 if (funcdef_flag
5093 && (threadp
5094 || storage_class == csc_auto
5095 || storage_class == csc_register
5096 || storage_class == csc_typedef))
5098 if (storage_class == csc_auto)
5099 pedwarn (loc,
5100 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5101 "function definition declared %<auto%>");
5102 if (storage_class == csc_register)
5103 error_at (loc, "function definition declared %<register%>");
5104 if (storage_class == csc_typedef)
5105 error_at (loc, "function definition declared %<typedef%>");
5106 if (threadp)
5107 error_at (loc, "function definition declared %<__thread%>");
5108 threadp = false;
5109 if (storage_class == csc_auto
5110 || storage_class == csc_register
5111 || storage_class == csc_typedef)
5112 storage_class = csc_none;
5114 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5116 if (decl_context == PARM && storage_class == csc_register)
5118 else
5120 switch (decl_context)
5122 case FIELD:
5123 if (name)
5124 error_at (loc, "storage class specified for structure "
5125 "field %qE", name);
5126 else
5127 error_at (loc, "storage class specified for structure field");
5128 break;
5129 case PARM:
5130 if (name)
5131 error_at (loc, "storage class specified for parameter %qE",
5132 name);
5133 else
5134 error_at (loc, "storage class specified for unnamed parameter");
5135 break;
5136 default:
5137 error_at (loc, "storage class specified for typename");
5138 break;
5140 storage_class = csc_none;
5141 threadp = false;
5144 else if (storage_class == csc_extern
5145 && initialized
5146 && !funcdef_flag)
5148 /* 'extern' with initialization is invalid if not at file scope. */
5149 if (current_scope == file_scope)
5151 /* It is fine to have 'extern const' when compiling at C
5152 and C++ intersection. */
5153 if (!(warn_cxx_compat && constp))
5154 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5155 name);
5157 else
5158 error_at (loc, "%qE has both %<extern%> and initializer", name);
5160 else if (current_scope == file_scope)
5162 if (storage_class == csc_auto)
5163 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5164 name);
5165 if (pedantic && storage_class == csc_register)
5166 pedwarn (input_location, OPT_Wpedantic,
5167 "file-scope declaration of %qE specifies %<register%>", name);
5169 else
5171 if (storage_class == csc_extern && funcdef_flag)
5172 error_at (loc, "nested function %qE declared %<extern%>", name);
5173 else if (threadp && storage_class == csc_none)
5175 error_at (loc, "function-scope %qE implicitly auto and declared "
5176 "%<__thread%>",
5177 name);
5178 threadp = false;
5182 /* Now figure out the structure of the declarator proper.
5183 Descend through it, creating more complex types, until we reach
5184 the declared identifier (or NULL_TREE, in an absolute declarator).
5185 At each stage we maintain an unqualified version of the type
5186 together with any qualifiers that should be applied to it with
5187 c_build_qualified_type; this way, array types including
5188 multidimensional array types are first built up in unqualified
5189 form and then the qualified form is created with
5190 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5192 while (declarator && declarator->kind != cdk_id)
5194 if (type == error_mark_node)
5196 declarator = declarator->declarator;
5197 continue;
5200 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5201 a cdk_pointer (for *...),
5202 a cdk_function (for ...(...)),
5203 a cdk_attrs (for nested attributes),
5204 or a cdk_id (for the name being declared
5205 or the place in an absolute declarator
5206 where the name was omitted).
5207 For the last case, we have just exited the loop.
5209 At this point, TYPE is the type of elements of an array,
5210 or for a function to return, or for a pointer to point to.
5211 After this sequence of ifs, TYPE is the type of the
5212 array or function or pointer, and DECLARATOR has had its
5213 outermost layer removed. */
5215 if (array_ptr_quals != TYPE_UNQUALIFIED
5216 || array_ptr_attrs != NULL_TREE
5217 || array_parm_static)
5219 /* Only the innermost declarator (making a parameter be of
5220 array type which is converted to pointer type)
5221 may have static or type qualifiers. */
5222 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5223 array_ptr_quals = TYPE_UNQUALIFIED;
5224 array_ptr_attrs = NULL_TREE;
5225 array_parm_static = 0;
5228 switch (declarator->kind)
5230 case cdk_attrs:
5232 /* A declarator with embedded attributes. */
5233 tree attrs = declarator->u.attrs;
5234 const struct c_declarator *inner_decl;
5235 int attr_flags = 0;
5236 declarator = declarator->declarator;
5237 inner_decl = declarator;
5238 while (inner_decl->kind == cdk_attrs)
5239 inner_decl = inner_decl->declarator;
5240 if (inner_decl->kind == cdk_id)
5241 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5242 else if (inner_decl->kind == cdk_function)
5243 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5244 else if (inner_decl->kind == cdk_array)
5245 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5246 returned_attrs = decl_attributes (&type,
5247 chainon (returned_attrs, attrs),
5248 attr_flags);
5249 break;
5251 case cdk_array:
5253 tree itype = NULL_TREE;
5254 tree size = declarator->u.array.dimen;
5255 /* The index is a signed object `sizetype' bits wide. */
5256 tree index_type = c_common_signed_type (sizetype);
5258 array_ptr_quals = declarator->u.array.quals;
5259 array_ptr_attrs = declarator->u.array.attrs;
5260 array_parm_static = declarator->u.array.static_p;
5261 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5263 declarator = declarator->declarator;
5265 /* Check for some types that there cannot be arrays of. */
5267 if (VOID_TYPE_P (type))
5269 if (name)
5270 error_at (loc, "declaration of %qE as array of voids", name);
5271 else
5272 error_at (loc, "declaration of type name as array of voids");
5273 type = error_mark_node;
5276 if (TREE_CODE (type) == FUNCTION_TYPE)
5278 if (name)
5279 error_at (loc, "declaration of %qE as array of functions",
5280 name);
5281 else
5282 error_at (loc, "declaration of type name as array of "
5283 "functions");
5284 type = error_mark_node;
5287 if (pedantic && !in_system_header && flexible_array_type_p (type))
5288 pedwarn (loc, OPT_Wpedantic,
5289 "invalid use of structure with flexible array member");
5291 if (size == error_mark_node)
5292 type = error_mark_node;
5294 if (type == error_mark_node)
5295 continue;
5297 /* If size was specified, set ITYPE to a range-type for
5298 that size. Otherwise, ITYPE remains null. finish_decl
5299 may figure it out from an initial value. */
5301 if (size)
5303 bool size_maybe_const = true;
5304 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5305 && !TREE_OVERFLOW (size));
5306 bool this_size_varies = false;
5308 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5309 lvalue. */
5310 STRIP_TYPE_NOPS (size);
5312 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5314 if (name)
5315 error_at (loc, "size of array %qE has non-integer type",
5316 name);
5317 else
5318 error_at (loc,
5319 "size of unnamed array has non-integer type");
5320 size = integer_one_node;
5323 size = c_fully_fold (size, false, &size_maybe_const);
5325 if (pedantic && size_maybe_const && integer_zerop (size))
5327 if (name)
5328 pedwarn (loc, OPT_Wpedantic,
5329 "ISO C forbids zero-size array %qE", name);
5330 else
5331 pedwarn (loc, OPT_Wpedantic,
5332 "ISO C forbids zero-size array");
5335 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5337 constant_expression_warning (size);
5338 if (tree_int_cst_sgn (size) < 0)
5340 if (name)
5341 error_at (loc, "size of array %qE is negative", name);
5342 else
5343 error_at (loc, "size of unnamed array is negative");
5344 size = integer_one_node;
5346 /* Handle a size folded to an integer constant but
5347 not an integer constant expression. */
5348 if (!size_int_const)
5350 /* If this is a file scope declaration of an
5351 ordinary identifier, this is invalid code;
5352 diagnosing it here and not subsequently
5353 treating the type as variable-length avoids
5354 more confusing diagnostics later. */
5355 if ((decl_context == NORMAL || decl_context == FIELD)
5356 && current_scope == file_scope)
5357 pedwarn (input_location, 0,
5358 "variably modified %qE at file scope",
5359 name);
5360 else
5361 this_size_varies = size_varies = true;
5362 warn_variable_length_array (name, size);
5365 else if ((decl_context == NORMAL || decl_context == FIELD)
5366 && current_scope == file_scope)
5368 error_at (loc, "variably modified %qE at file scope", name);
5369 size = integer_one_node;
5371 else
5373 /* Make sure the array size remains visibly
5374 nonconstant even if it is (eg) a const variable
5375 with known value. */
5376 this_size_varies = size_varies = true;
5377 warn_variable_length_array (name, size);
5380 if (integer_zerop (size) && !this_size_varies)
5382 /* A zero-length array cannot be represented with
5383 an unsigned index type, which is what we'll
5384 get with build_index_type. Create an
5385 open-ended range instead. */
5386 itype = build_range_type (sizetype, size, NULL_TREE);
5388 else
5390 /* Arrange for the SAVE_EXPR on the inside of the
5391 MINUS_EXPR, which allows the -1 to get folded
5392 with the +1 that happens when building TYPE_SIZE. */
5393 if (size_varies)
5394 size = save_expr (size);
5395 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5396 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5397 integer_zero_node, size);
5399 /* Compute the maximum valid index, that is, size
5400 - 1. Do the calculation in index_type, so that
5401 if it is a variable the computations will be
5402 done in the proper mode. */
5403 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5404 convert (index_type, size),
5405 convert (index_type,
5406 size_one_node));
5408 /* The above overflows when size does not fit
5409 in index_type.
5410 ??? While a size of INT_MAX+1 technically shouldn't
5411 cause an overflow (because we subtract 1), handling
5412 this case seems like an unnecessary complication. */
5413 if (TREE_CODE (size) == INTEGER_CST
5414 && !int_fits_type_p (size, index_type))
5416 if (name)
5417 error_at (loc, "size of array %qE is too large",
5418 name);
5419 else
5420 error_at (loc, "size of unnamed array is too large");
5421 type = error_mark_node;
5422 continue;
5425 itype = build_index_type (itype);
5427 if (this_size_varies)
5429 if (*expr)
5430 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5431 *expr, size);
5432 else
5433 *expr = size;
5434 *expr_const_operands &= size_maybe_const;
5437 else if (decl_context == FIELD)
5439 bool flexible_array_member = false;
5440 if (array_parm_vla_unspec_p)
5441 /* Field names can in fact have function prototype
5442 scope so [*] is disallowed here through making
5443 the field variably modified, not through being
5444 something other than a declaration with function
5445 prototype scope. */
5446 size_varies = true;
5447 else
5449 const struct c_declarator *t = declarator;
5450 while (t->kind == cdk_attrs)
5451 t = t->declarator;
5452 flexible_array_member = (t->kind == cdk_id);
5454 if (flexible_array_member
5455 && pedantic && !flag_isoc99 && !in_system_header)
5456 pedwarn (loc, OPT_Wpedantic,
5457 "ISO C90 does not support flexible array members");
5459 /* ISO C99 Flexible array members are effectively
5460 identical to GCC's zero-length array extension. */
5461 if (flexible_array_member || array_parm_vla_unspec_p)
5462 itype = build_range_type (sizetype, size_zero_node,
5463 NULL_TREE);
5465 else if (decl_context == PARM)
5467 if (array_parm_vla_unspec_p)
5469 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5470 size_varies = true;
5473 else if (decl_context == TYPENAME)
5475 if (array_parm_vla_unspec_p)
5477 /* C99 6.7.5.2p4 */
5478 warning (0, "%<[*]%> not in a declaration");
5479 /* We use this to avoid messing up with incomplete
5480 array types of the same type, that would
5481 otherwise be modified below. */
5482 itype = build_range_type (sizetype, size_zero_node,
5483 NULL_TREE);
5484 size_varies = true;
5488 /* Complain about arrays of incomplete types. */
5489 if (!COMPLETE_TYPE_P (type))
5491 error_at (loc, "array type has incomplete element type");
5492 type = error_mark_node;
5494 else
5495 /* When itype is NULL, a shared incomplete array type is
5496 returned for all array of a given type. Elsewhere we
5497 make sure we don't complete that type before copying
5498 it, but here we want to make sure we don't ever
5499 modify the shared type, so we gcc_assert (itype)
5500 below. */
5502 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5503 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5504 type = build_qualified_type (type,
5505 ENCODE_QUAL_ADDR_SPACE (as));
5507 type = build_array_type (type, itype);
5510 if (type != error_mark_node)
5512 if (size_varies)
5514 /* It is ok to modify type here even if itype is
5515 NULL: if size_varies, we're in a
5516 multi-dimensional array and the inner type has
5517 variable size, so the enclosing shared array type
5518 must too. */
5519 if (size && TREE_CODE (size) == INTEGER_CST)
5520 type
5521 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5522 C_TYPE_VARIABLE_SIZE (type) = 1;
5525 /* The GCC extension for zero-length arrays differs from
5526 ISO flexible array members in that sizeof yields
5527 zero. */
5528 if (size && integer_zerop (size))
5530 gcc_assert (itype);
5531 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5532 TYPE_SIZE (type) = bitsize_zero_node;
5533 TYPE_SIZE_UNIT (type) = size_zero_node;
5534 SET_TYPE_STRUCTURAL_EQUALITY (type);
5536 if (array_parm_vla_unspec_p)
5538 gcc_assert (itype);
5539 /* The type is complete. C99 6.7.5.2p4 */
5540 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5541 TYPE_SIZE (type) = bitsize_zero_node;
5542 TYPE_SIZE_UNIT (type) = size_zero_node;
5543 SET_TYPE_STRUCTURAL_EQUALITY (type);
5547 if (decl_context != PARM
5548 && (array_ptr_quals != TYPE_UNQUALIFIED
5549 || array_ptr_attrs != NULL_TREE
5550 || array_parm_static))
5552 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5553 array_ptr_quals = TYPE_UNQUALIFIED;
5554 array_ptr_attrs = NULL_TREE;
5555 array_parm_static = 0;
5557 break;
5559 case cdk_function:
5561 /* Say it's a definition only for the declarator closest
5562 to the identifier, apart possibly from some
5563 attributes. */
5564 bool really_funcdef = false;
5565 tree arg_types;
5566 if (funcdef_flag)
5568 const struct c_declarator *t = declarator->declarator;
5569 while (t->kind == cdk_attrs)
5570 t = t->declarator;
5571 really_funcdef = (t->kind == cdk_id);
5574 /* Declaring a function type. Make sure we have a valid
5575 type for the function to return. */
5576 if (type == error_mark_node)
5577 continue;
5579 size_varies = false;
5581 /* Warn about some types functions can't return. */
5582 if (TREE_CODE (type) == FUNCTION_TYPE)
5584 if (name)
5585 error_at (loc, "%qE declared as function returning a "
5586 "function", name);
5587 else
5588 error_at (loc, "type name declared as function "
5589 "returning a function");
5590 type = integer_type_node;
5592 if (TREE_CODE (type) == ARRAY_TYPE)
5594 if (name)
5595 error_at (loc, "%qE declared as function returning an array",
5596 name);
5597 else
5598 error_at (loc, "type name declared as function returning "
5599 "an array");
5600 type = integer_type_node;
5602 errmsg = targetm.invalid_return_type (type);
5603 if (errmsg)
5605 error (errmsg);
5606 type = integer_type_node;
5609 /* Construct the function type and go to the next
5610 inner layer of declarator. */
5611 arg_info = declarator->u.arg_info;
5612 arg_types = grokparms (arg_info, really_funcdef);
5614 /* Type qualifiers before the return type of the function
5615 qualify the return type, not the function type. */
5616 if (type_quals)
5618 /* Type qualifiers on a function return type are
5619 normally permitted by the standard but have no
5620 effect, so give a warning at -Wreturn-type.
5621 Qualifiers on a void return type are banned on
5622 function definitions in ISO C; GCC used to used
5623 them for noreturn functions. */
5624 if (VOID_TYPE_P (type) && really_funcdef)
5625 pedwarn (loc, 0,
5626 "function definition has qualified void return type");
5627 else
5628 warning_at (loc, OPT_Wignored_qualifiers,
5629 "type qualifiers ignored on function return type");
5631 type = c_build_qualified_type (type, type_quals);
5633 type_quals = TYPE_UNQUALIFIED;
5635 type = build_function_type (type, arg_types);
5636 declarator = declarator->declarator;
5638 /* Set the TYPE_CONTEXTs for each tagged type which is local to
5639 the formal parameter list of this FUNCTION_TYPE to point to
5640 the FUNCTION_TYPE node itself. */
5642 c_arg_tag *tag;
5643 unsigned ix;
5645 FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
5646 TYPE_CONTEXT (tag->type) = type;
5648 break;
5650 case cdk_pointer:
5652 /* Merge any constancy or volatility into the target type
5653 for the pointer. */
5655 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5656 && type_quals)
5657 pedwarn (loc, OPT_Wpedantic,
5658 "ISO C forbids qualified function types");
5659 if (type_quals)
5660 type = c_build_qualified_type (type, type_quals);
5661 size_varies = false;
5663 /* When the pointed-to type involves components of variable size,
5664 care must be taken to ensure that the size evaluation code is
5665 emitted early enough to dominate all the possible later uses
5666 and late enough for the variables on which it depends to have
5667 been assigned.
5669 This is expected to happen automatically when the pointed-to
5670 type has a name/declaration of it's own, but special attention
5671 is required if the type is anonymous.
5673 We handle the NORMAL and FIELD contexts here by attaching an
5674 artificial TYPE_DECL to such pointed-to type. This forces the
5675 sizes evaluation at a safe point and ensures it is not deferred
5676 until e.g. within a deeper conditional context.
5678 We expect nothing to be needed here for PARM or TYPENAME.
5679 Pushing a TYPE_DECL at this point for TYPENAME would actually
5680 be incorrect, as we might be in the middle of an expression
5681 with side effects on the pointed-to type size "arguments" prior
5682 to the pointer declaration point and the fake TYPE_DECL in the
5683 enclosing context would force the size evaluation prior to the
5684 side effects. */
5686 if (!TYPE_NAME (type)
5687 && (decl_context == NORMAL || decl_context == FIELD)
5688 && variably_modified_type_p (type, NULL_TREE))
5690 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5691 DECL_ARTIFICIAL (decl) = 1;
5692 pushdecl (decl);
5693 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5694 TYPE_NAME (type) = decl;
5697 type = c_build_pointer_type (type);
5699 /* Process type qualifiers (such as const or volatile)
5700 that were given inside the `*'. */
5701 type_quals = declarator->u.pointer_quals;
5703 declarator = declarator->declarator;
5704 break;
5706 default:
5707 gcc_unreachable ();
5710 *decl_attrs = chainon (returned_attrs, *decl_attrs);
5712 /* Now TYPE has the actual type, apart from any qualifiers in
5713 TYPE_QUALS. */
5715 /* Warn about address space used for things other than static memory or
5716 pointers. */
5717 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5718 if (!ADDR_SPACE_GENERIC_P (address_space))
5720 if (decl_context == NORMAL)
5722 switch (storage_class)
5724 case csc_auto:
5725 error ("%qs combined with %<auto%> qualifier for %qE",
5726 c_addr_space_name (address_space), name);
5727 break;
5728 case csc_register:
5729 error ("%qs combined with %<register%> qualifier for %qE",
5730 c_addr_space_name (address_space), name);
5731 break;
5732 case csc_none:
5733 if (current_function_scope)
5735 error ("%qs specified for auto variable %qE",
5736 c_addr_space_name (address_space), name);
5737 break;
5739 break;
5740 case csc_static:
5741 case csc_extern:
5742 case csc_typedef:
5743 break;
5744 default:
5745 gcc_unreachable ();
5748 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5750 if (name)
5751 error ("%qs specified for parameter %qE",
5752 c_addr_space_name (address_space), name);
5753 else
5754 error ("%qs specified for unnamed parameter",
5755 c_addr_space_name (address_space));
5757 else if (decl_context == FIELD)
5759 if (name)
5760 error ("%qs specified for structure field %qE",
5761 c_addr_space_name (address_space), name);
5762 else
5763 error ("%qs specified for structure field",
5764 c_addr_space_name (address_space));
5768 /* Check the type and width of a bit-field. */
5769 if (bitfield)
5770 check_bitfield_type_and_width (&type, width, name);
5772 /* Reject invalid uses of _Alignas. */
5773 if (declspecs->alignas_p)
5775 if (storage_class == csc_typedef)
5776 error_at (loc, "alignment specified for typedef %qE", name);
5777 else if (storage_class == csc_register)
5778 error_at (loc, "alignment specified for %<register%> object %qE",
5779 name);
5780 else if (decl_context == PARM)
5782 if (name)
5783 error_at (loc, "alignment specified for parameter %qE", name);
5784 else
5785 error_at (loc, "alignment specified for unnamed parameter");
5787 else if (bitfield)
5789 if (name)
5790 error_at (loc, "alignment specified for bit-field %qE", name);
5791 else
5792 error_at (loc, "alignment specified for unnamed bit-field");
5794 else if (TREE_CODE (type) == FUNCTION_TYPE)
5795 error_at (loc, "alignment specified for function %qE", name);
5796 else if (declspecs->align_log != -1)
5798 alignas_align = 1U << declspecs->align_log;
5799 if (alignas_align < TYPE_ALIGN_UNIT (type))
5801 if (name)
5802 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5803 "alignment of %qE", name);
5804 else
5805 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5806 "alignment of unnamed field");
5807 alignas_align = 0;
5812 /* Did array size calculations overflow? */
5814 if (TREE_CODE (type) == ARRAY_TYPE
5815 && COMPLETE_TYPE_P (type)
5816 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5817 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5819 if (name)
5820 error_at (loc, "size of array %qE is too large", name);
5821 else
5822 error_at (loc, "size of unnamed array is too large");
5823 /* If we proceed with the array type as it is, we'll eventually
5824 crash in tree_low_cst(). */
5825 type = error_mark_node;
5828 /* If this is declaring a typedef name, return a TYPE_DECL. */
5830 if (storage_class == csc_typedef)
5832 tree decl;
5833 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5834 && type_quals)
5835 pedwarn (loc, OPT_Wpedantic,
5836 "ISO C forbids qualified function types");
5837 if (type_quals)
5838 type = c_build_qualified_type (type, type_quals);
5839 decl = build_decl (declarator->id_loc,
5840 TYPE_DECL, declarator->u.id, type);
5841 if (declspecs->explicit_signed_p)
5842 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5843 if (declspecs->inline_p)
5844 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5845 if (declspecs->noreturn_p)
5846 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
5848 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5850 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5852 if (b != NULL
5853 && b->decl != NULL_TREE
5854 && (B_IN_CURRENT_SCOPE (b)
5855 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5856 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5858 warning_at (declarator->id_loc, OPT_Wc___compat,
5859 ("using %qD as both a typedef and a tag is "
5860 "invalid in C++"),
5861 decl);
5862 if (b->locus != UNKNOWN_LOCATION)
5863 inform (b->locus, "originally defined here");
5867 return decl;
5870 /* If this is a type name (such as, in a cast or sizeof),
5871 compute the type and return it now. */
5873 if (decl_context == TYPENAME)
5875 /* Note that the grammar rejects storage classes in typenames
5876 and fields. */
5877 gcc_assert (storage_class == csc_none && !threadp
5878 && !declspecs->inline_p && !declspecs->noreturn_p);
5879 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5880 && type_quals)
5881 pedwarn (loc, OPT_Wpedantic,
5882 "ISO C forbids const or volatile function types");
5883 if (type_quals)
5884 type = c_build_qualified_type (type, type_quals);
5885 return type;
5888 if (pedantic && decl_context == FIELD
5889 && variably_modified_type_p (type, NULL_TREE))
5891 /* C99 6.7.2.1p8 */
5892 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
5893 "have a variably modified type");
5896 /* Aside from typedefs and type names (handle above),
5897 `void' at top level (not within pointer)
5898 is allowed only in public variables.
5899 We don't complain about parms either, but that is because
5900 a better error message can be made later. */
5902 if (VOID_TYPE_P (type) && decl_context != PARM
5903 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5904 && (storage_class == csc_extern
5905 || (current_scope == file_scope
5906 && !(storage_class == csc_static
5907 || storage_class == csc_register)))))
5909 error_at (loc, "variable or field %qE declared void", name);
5910 type = integer_type_node;
5913 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5914 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5917 tree decl;
5919 if (decl_context == PARM)
5921 tree promoted_type;
5923 /* A parameter declared as an array of T is really a pointer to T.
5924 One declared as a function is really a pointer to a function. */
5926 if (TREE_CODE (type) == ARRAY_TYPE)
5928 /* Transfer const-ness of array into that of type pointed to. */
5929 type = TREE_TYPE (type);
5930 if (type_quals)
5931 type = c_build_qualified_type (type, type_quals);
5932 type = c_build_pointer_type (type);
5933 type_quals = array_ptr_quals;
5934 if (type_quals)
5935 type = c_build_qualified_type (type, type_quals);
5937 /* We don't yet implement attributes in this context. */
5938 if (array_ptr_attrs != NULL_TREE)
5939 warning_at (loc, OPT_Wattributes,
5940 "attributes in parameter array declarator ignored");
5942 size_varies = false;
5944 else if (TREE_CODE (type) == FUNCTION_TYPE)
5946 if (type_quals)
5947 pedwarn (loc, OPT_Wpedantic,
5948 "ISO C forbids qualified function types");
5949 if (type_quals)
5950 type = c_build_qualified_type (type, type_quals);
5951 type = c_build_pointer_type (type);
5952 type_quals = TYPE_UNQUALIFIED;
5954 else if (type_quals)
5955 type = c_build_qualified_type (type, type_quals);
5957 decl = build_decl (declarator->id_loc,
5958 PARM_DECL, declarator->u.id, type);
5959 if (size_varies)
5960 C_DECL_VARIABLE_SIZE (decl) = 1;
5962 /* Compute the type actually passed in the parmlist,
5963 for the case where there is no prototype.
5964 (For example, shorts and chars are passed as ints.)
5965 When there is a prototype, this is overridden later. */
5967 if (type == error_mark_node)
5968 promoted_type = type;
5969 else
5970 promoted_type = c_type_promotes_to (type);
5972 DECL_ARG_TYPE (decl) = promoted_type;
5973 if (declspecs->inline_p)
5974 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5975 if (declspecs->noreturn_p)
5976 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
5978 else if (decl_context == FIELD)
5980 /* Note that the grammar rejects storage classes in typenames
5981 and fields. */
5982 gcc_assert (storage_class == csc_none && !threadp
5983 && !declspecs->inline_p && !declspecs->noreturn_p);
5985 /* Structure field. It may not be a function. */
5987 if (TREE_CODE (type) == FUNCTION_TYPE)
5989 error_at (loc, "field %qE declared as a function", name);
5990 type = build_pointer_type (type);
5992 else if (TREE_CODE (type) != ERROR_MARK
5993 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5995 if (name)
5996 error_at (loc, "field %qE has incomplete type", name);
5997 else
5998 error_at (loc, "unnamed field has incomplete type");
5999 type = error_mark_node;
6001 type = c_build_qualified_type (type, type_quals);
6002 decl = build_decl (declarator->id_loc,
6003 FIELD_DECL, declarator->u.id, type);
6004 DECL_NONADDRESSABLE_P (decl) = bitfield;
6005 if (bitfield && !declarator->u.id)
6006 TREE_NO_WARNING (decl) = 1;
6008 if (size_varies)
6009 C_DECL_VARIABLE_SIZE (decl) = 1;
6011 else if (TREE_CODE (type) == FUNCTION_TYPE)
6013 if (storage_class == csc_register || threadp)
6015 error_at (loc, "invalid storage class for function %qE", name);
6017 else if (current_scope != file_scope)
6019 /* Function declaration not at file scope. Storage
6020 classes other than `extern' are not allowed, C99
6021 6.7.1p5, and `extern' makes no difference. However,
6022 GCC allows 'auto', perhaps with 'inline', to support
6023 nested functions. */
6024 if (storage_class == csc_auto)
6025 pedwarn (loc, OPT_Wpedantic,
6026 "invalid storage class for function %qE", name);
6027 else if (storage_class == csc_static)
6029 error_at (loc, "invalid storage class for function %qE", name);
6030 if (funcdef_flag)
6031 storage_class = declspecs->storage_class = csc_none;
6032 else
6033 return 0;
6037 decl = build_decl (declarator->id_loc,
6038 FUNCTION_DECL, declarator->u.id, type);
6039 decl = build_decl_attribute_variant (decl, decl_attr);
6041 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6042 pedwarn (loc, OPT_Wpedantic,
6043 "ISO C forbids qualified function types");
6045 /* Every function declaration is an external reference
6046 (DECL_EXTERNAL) except for those which are not at file
6047 scope and are explicitly declared "auto". This is
6048 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6049 GCC to signify a forward declaration of a nested function. */
6050 if (storage_class == csc_auto && current_scope != file_scope)
6051 DECL_EXTERNAL (decl) = 0;
6052 /* In C99, a function which is declared 'inline' with 'extern'
6053 is not an external reference (which is confusing). It
6054 means that the later definition of the function must be output
6055 in this file, C99 6.7.4p6. In GNU C89, a function declared
6056 'extern inline' is an external reference. */
6057 else if (declspecs->inline_p && storage_class != csc_static)
6058 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6059 == flag_gnu89_inline);
6060 else
6061 DECL_EXTERNAL (decl) = !initialized;
6063 /* Record absence of global scope for `static' or `auto'. */
6064 TREE_PUBLIC (decl)
6065 = !(storage_class == csc_static || storage_class == csc_auto);
6067 /* For a function definition, record the argument information
6068 block where store_parm_decls will look for it. */
6069 if (funcdef_flag)
6070 current_function_arg_info = arg_info;
6072 if (declspecs->default_int_p)
6073 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6075 /* Record presence of `inline' and `_Noreturn', if it is
6076 reasonable. */
6077 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6079 if (declspecs->inline_p)
6080 pedwarn (loc, 0, "cannot inline function %<main%>");
6081 if (declspecs->noreturn_p)
6082 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6084 else
6086 if (declspecs->inline_p)
6087 /* Record that the function is declared `inline'. */
6088 DECL_DECLARED_INLINE_P (decl) = 1;
6089 if (declspecs->noreturn_p)
6091 if (!flag_isoc11)
6093 if (flag_isoc99)
6094 pedwarn (loc, OPT_Wpedantic,
6095 "ISO C99 does not support %<_Noreturn%>");
6096 else
6097 pedwarn (loc, OPT_Wpedantic,
6098 "ISO C90 does not support %<_Noreturn%>");
6100 TREE_THIS_VOLATILE (decl) = 1;
6104 else
6106 /* It's a variable. */
6107 /* An uninitialized decl with `extern' is a reference. */
6108 int extern_ref = !initialized && storage_class == csc_extern;
6110 type = c_build_qualified_type (type, type_quals);
6112 /* C99 6.2.2p7: It is invalid (compile-time undefined
6113 behavior) to create an 'extern' declaration for a
6114 variable if there is a global declaration that is
6115 'static' and the global declaration is not visible.
6116 (If the static declaration _is_ currently visible,
6117 the 'extern' declaration is taken to refer to that decl.) */
6118 if (extern_ref && current_scope != file_scope)
6120 tree global_decl = identifier_global_value (declarator->u.id);
6121 tree visible_decl = lookup_name (declarator->u.id);
6123 if (global_decl
6124 && global_decl != visible_decl
6125 && TREE_CODE (global_decl) == VAR_DECL
6126 && !TREE_PUBLIC (global_decl))
6127 error_at (loc, "variable previously declared %<static%> "
6128 "redeclared %<extern%>");
6131 decl = build_decl (declarator->id_loc,
6132 VAR_DECL, declarator->u.id, type);
6133 if (size_varies)
6134 C_DECL_VARIABLE_SIZE (decl) = 1;
6136 if (declspecs->inline_p)
6137 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6138 if (declspecs->noreturn_p)
6139 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6141 /* At file scope, an initialized extern declaration may follow
6142 a static declaration. In that case, DECL_EXTERNAL will be
6143 reset later in start_decl. */
6144 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6146 /* At file scope, the presence of a `static' or `register' storage
6147 class specifier, or the absence of all storage class specifiers
6148 makes this declaration a definition (perhaps tentative). Also,
6149 the absence of `static' makes it public. */
6150 if (current_scope == file_scope)
6152 TREE_PUBLIC (decl) = storage_class != csc_static;
6153 TREE_STATIC (decl) = !extern_ref;
6155 /* Not at file scope, only `static' makes a static definition. */
6156 else
6158 TREE_STATIC (decl) = (storage_class == csc_static);
6159 TREE_PUBLIC (decl) = extern_ref;
6162 if (threadp)
6163 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6166 if ((storage_class == csc_extern
6167 || (storage_class == csc_none
6168 && TREE_CODE (type) == FUNCTION_TYPE
6169 && !funcdef_flag))
6170 && variably_modified_type_p (type, NULL_TREE))
6172 /* C99 6.7.5.2p2 */
6173 if (TREE_CODE (type) == FUNCTION_TYPE)
6174 error_at (loc, "non-nested function with variably modified type");
6175 else
6176 error_at (loc, "object with variably modified type must have "
6177 "no linkage");
6180 /* Record `register' declaration for warnings on &
6181 and in case doing stupid register allocation. */
6183 if (storage_class == csc_register)
6185 C_DECL_REGISTER (decl) = 1;
6186 DECL_REGISTER (decl) = 1;
6189 /* Record constancy and volatility. */
6190 c_apply_type_quals_to_decl (type_quals, decl);
6192 /* Apply _Alignas specifiers. */
6193 if (alignas_align)
6195 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6196 DECL_USER_ALIGN (decl) = 1;
6199 /* If a type has volatile components, it should be stored in memory.
6200 Otherwise, the fact that those components are volatile
6201 will be ignored, and would even crash the compiler.
6202 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6203 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6204 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6205 || TREE_CODE (decl) == RESULT_DECL))
6207 /* It is not an error for a structure with volatile fields to
6208 be declared register, but reset DECL_REGISTER since it
6209 cannot actually go in a register. */
6210 int was_reg = C_DECL_REGISTER (decl);
6211 C_DECL_REGISTER (decl) = 0;
6212 DECL_REGISTER (decl) = 0;
6213 c_mark_addressable (decl);
6214 C_DECL_REGISTER (decl) = was_reg;
6217 /* This is the earliest point at which we might know the assembler
6218 name of a variable. Thus, if it's known before this, die horribly. */
6219 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6221 if (warn_cxx_compat
6222 && TREE_CODE (decl) == VAR_DECL
6223 && TREE_PUBLIC (decl)
6224 && TREE_STATIC (decl)
6225 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6226 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6227 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6228 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6229 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6230 ("non-local variable %qD with anonymous type is "
6231 "questionable in C++"),
6232 decl);
6234 return decl;
6238 /* Decode the parameter-list info for a function type or function definition.
6239 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6240 if there is an identifier list instead of a parameter decl list).
6241 These two functions are separate because when a function returns
6242 or receives functions then each is called multiple times but the order
6243 of calls is different. The last call to `grokparms' is always the one
6244 that contains the formal parameter names of a function definition.
6246 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6248 FUNCDEF_FLAG is true for a function definition, false for
6249 a mere declaration. A nonempty identifier-list gets an error message
6250 when FUNCDEF_FLAG is false. */
6252 static tree
6253 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6255 tree arg_types = arg_info->types;
6257 if (funcdef_flag && arg_info->had_vla_unspec)
6259 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6260 /* C99 6.7.5.2p4 */
6261 error ("%<[*]%> not allowed in other than function prototype scope");
6264 if (arg_types == 0 && !funcdef_flag && !in_system_header)
6265 warning (OPT_Wstrict_prototypes,
6266 "function declaration isn%'t a prototype");
6268 if (arg_types == error_mark_node)
6269 return 0; /* don't set TYPE_ARG_TYPES in this case */
6271 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6273 if (!funcdef_flag)
6275 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6276 arg_info->parms = NULL_TREE;
6278 else
6279 arg_info->parms = arg_info->types;
6281 arg_info->types = 0;
6282 return 0;
6284 else
6286 tree parm, type, typelt;
6287 unsigned int parmno;
6288 const char *errmsg;
6290 /* If there is a parameter of incomplete type in a definition,
6291 this is an error. In a declaration this is valid, and a
6292 struct or union type may be completed later, before any calls
6293 or definition of the function. In the case where the tag was
6294 first declared within the parameter list, a warning has
6295 already been given. If a parameter has void type, then
6296 however the function cannot be defined or called, so
6297 warn. */
6299 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6300 parm;
6301 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6303 type = TREE_VALUE (typelt);
6304 if (type == error_mark_node)
6305 continue;
6307 if (!COMPLETE_TYPE_P (type))
6309 if (funcdef_flag)
6311 if (DECL_NAME (parm))
6312 error_at (input_location,
6313 "parameter %u (%q+D) has incomplete type",
6314 parmno, parm);
6315 else
6316 error_at (DECL_SOURCE_LOCATION (parm),
6317 "parameter %u has incomplete type",
6318 parmno);
6320 TREE_VALUE (typelt) = error_mark_node;
6321 TREE_TYPE (parm) = error_mark_node;
6322 arg_types = NULL_TREE;
6324 else if (VOID_TYPE_P (type))
6326 if (DECL_NAME (parm))
6327 warning_at (input_location, 0,
6328 "parameter %u (%q+D) has void type",
6329 parmno, parm);
6330 else
6331 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6332 "parameter %u has void type",
6333 parmno);
6337 errmsg = targetm.invalid_parameter_type (type);
6338 if (errmsg)
6340 error (errmsg);
6341 TREE_VALUE (typelt) = error_mark_node;
6342 TREE_TYPE (parm) = error_mark_node;
6343 arg_types = NULL_TREE;
6346 if (DECL_NAME (parm) && TREE_USED (parm))
6347 warn_if_shadowing (parm);
6349 return arg_types;
6353 /* Allocate and initialize a c_arg_info structure from the parser's
6354 obstack. */
6356 struct c_arg_info *
6357 build_arg_info (void)
6359 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6360 ret->parms = NULL_TREE;
6361 ret->tags = NULL;
6362 ret->types = NULL_TREE;
6363 ret->others = NULL_TREE;
6364 ret->pending_sizes = NULL;
6365 ret->had_vla_unspec = 0;
6366 return ret;
6369 /* Take apart the current scope and return a c_arg_info structure with
6370 info on a parameter list just parsed.
6372 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6374 ELLIPSIS being true means the argument list ended in '...' so don't
6375 append a sentinel (void_list_node) to the end of the type-list.
6377 EXPR is NULL or an expression that needs to be evaluated for the
6378 side effects of array size expressions in the parameters. */
6380 struct c_arg_info *
6381 get_parm_info (bool ellipsis, tree expr)
6383 struct c_binding *b = current_scope->bindings;
6384 struct c_arg_info *arg_info = build_arg_info ();
6386 tree parms = 0;
6387 VEC(c_arg_tag,gc) *tags = NULL;
6388 tree types = 0;
6389 tree others = 0;
6391 static bool explained_incomplete_types = false;
6392 bool gave_void_only_once_err = false;
6394 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6396 /* The bindings in this scope must not get put into a block.
6397 We will take care of deleting the binding nodes. */
6398 current_scope->bindings = 0;
6400 /* This function is only called if there was *something* on the
6401 parameter list. */
6402 gcc_assert (b);
6404 /* A parameter list consisting solely of 'void' indicates that the
6405 function takes no arguments. But if the 'void' is qualified
6406 (by 'const' or 'volatile'), or has a storage class specifier
6407 ('register'), then the behavior is undefined; issue an error.
6408 Typedefs for 'void' are OK (see DR#157). */
6409 if (b->prev == 0 /* one binding */
6410 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6411 && !DECL_NAME (b->decl) /* anonymous */
6412 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6414 if (TREE_THIS_VOLATILE (b->decl)
6415 || TREE_READONLY (b->decl)
6416 || C_DECL_REGISTER (b->decl))
6417 error ("%<void%> as only parameter may not be qualified");
6419 /* There cannot be an ellipsis. */
6420 if (ellipsis)
6421 error ("%<void%> must be the only parameter");
6423 arg_info->types = void_list_node;
6424 return arg_info;
6427 if (!ellipsis)
6428 types = void_list_node;
6430 /* Break up the bindings list into parms, tags, types, and others;
6431 apply sanity checks; purge the name-to-decl bindings. */
6432 while (b)
6434 tree decl = b->decl;
6435 tree type = TREE_TYPE (decl);
6436 c_arg_tag *tag;
6437 const char *keyword;
6439 switch (TREE_CODE (decl))
6441 case PARM_DECL:
6442 if (b->id)
6444 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6445 I_SYMBOL_BINDING (b->id) = b->shadowed;
6448 /* Check for forward decls that never got their actual decl. */
6449 if (TREE_ASM_WRITTEN (decl))
6450 error ("parameter %q+D has just a forward declaration", decl);
6451 /* Check for (..., void, ...) and issue an error. */
6452 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6454 if (!gave_void_only_once_err)
6456 error ("%<void%> must be the only parameter");
6457 gave_void_only_once_err = true;
6460 else
6462 /* Valid parameter, add it to the list. */
6463 DECL_CHAIN (decl) = parms;
6464 parms = decl;
6466 /* Since there is a prototype, args are passed in their
6467 declared types. The back end may override this later. */
6468 DECL_ARG_TYPE (decl) = type;
6469 types = tree_cons (0, type, types);
6471 break;
6473 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6474 case UNION_TYPE: keyword = "union"; goto tag;
6475 case RECORD_TYPE: keyword = "struct"; goto tag;
6476 tag:
6477 /* Types may not have tag-names, in which case the type
6478 appears in the bindings list with b->id NULL. */
6479 if (b->id)
6481 gcc_assert (I_TAG_BINDING (b->id) == b);
6482 I_TAG_BINDING (b->id) = b->shadowed;
6485 /* Warn about any struct, union or enum tags defined in a
6486 parameter list. The scope of such types is limited to
6487 the parameter list, which is rarely if ever desirable
6488 (it's impossible to call such a function with type-
6489 correct arguments). An anonymous union parm type is
6490 meaningful as a GNU extension, so don't warn for that. */
6491 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6493 if (b->id)
6494 /* The %s will be one of 'struct', 'union', or 'enum'. */
6495 warning (0, "%<%s %E%> declared inside parameter list",
6496 keyword, b->id);
6497 else
6498 /* The %s will be one of 'struct', 'union', or 'enum'. */
6499 warning (0, "anonymous %s declared inside parameter list",
6500 keyword);
6502 if (!explained_incomplete_types)
6504 warning (0, "its scope is only this definition or declaration,"
6505 " which is probably not what you want");
6506 explained_incomplete_types = true;
6510 tag = VEC_safe_push (c_arg_tag, gc, tags, NULL);
6511 tag->id = b->id;
6512 tag->type = decl;
6513 break;
6515 case CONST_DECL:
6516 case TYPE_DECL:
6517 case FUNCTION_DECL:
6518 /* CONST_DECLs appear here when we have an embedded enum,
6519 and TYPE_DECLs appear here when we have an embedded struct
6520 or union. No warnings for this - we already warned about the
6521 type itself. FUNCTION_DECLs appear when there is an implicit
6522 function declaration in the parameter list. */
6524 /* When we reinsert this decl in the function body, we need
6525 to reconstruct whether it was marked as nested. */
6526 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6527 ? b->nested
6528 : !b->nested);
6529 DECL_CHAIN (decl) = others;
6530 others = decl;
6531 /* fall through */
6533 case ERROR_MARK:
6534 /* error_mark_node appears here when we have an undeclared
6535 variable. Just throw it away. */
6536 if (b->id)
6538 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6539 I_SYMBOL_BINDING (b->id) = b->shadowed;
6541 break;
6543 /* Other things that might be encountered. */
6544 case LABEL_DECL:
6545 case VAR_DECL:
6546 default:
6547 gcc_unreachable ();
6550 b = free_binding_and_advance (b);
6553 arg_info->parms = parms;
6554 arg_info->tags = tags;
6555 arg_info->types = types;
6556 arg_info->others = others;
6557 arg_info->pending_sizes = expr;
6558 return arg_info;
6561 /* Get the struct, enum or union (CODE says which) with tag NAME.
6562 Define the tag as a forward-reference with location LOC if it is
6563 not defined. Return a c_typespec structure for the type
6564 specifier. */
6566 struct c_typespec
6567 parser_xref_tag (location_t loc, enum tree_code code, tree name)
6569 struct c_typespec ret;
6570 tree ref;
6571 location_t refloc;
6573 ret.expr = NULL_TREE;
6574 ret.expr_const_operands = true;
6576 /* If a cross reference is requested, look up the type
6577 already defined for this tag and return it. */
6579 ref = lookup_tag (code, name, 0, &refloc);
6580 /* If this is the right type of tag, return what we found.
6581 (This reference will be shadowed by shadow_tag later if appropriate.)
6582 If this is the wrong type of tag, do not return it. If it was the
6583 wrong type in the same scope, we will have had an error
6584 message already; if in a different scope and declaring
6585 a name, pending_xref_error will give an error message; but if in a
6586 different scope and not declaring a name, this tag should
6587 shadow the previous declaration of a different type of tag, and
6588 this would not work properly if we return the reference found.
6589 (For example, with "struct foo" in an outer scope, "union foo;"
6590 must shadow that tag with a new one of union type.) */
6591 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6592 if (ref && TREE_CODE (ref) == code)
6594 if (C_TYPE_DEFINED_IN_STRUCT (ref)
6595 && loc != UNKNOWN_LOCATION
6596 && warn_cxx_compat)
6598 switch (code)
6600 case ENUMERAL_TYPE:
6601 warning_at (loc, OPT_Wc___compat,
6602 ("enum type defined in struct or union "
6603 "is not visible in C++"));
6604 inform (refloc, "enum type defined here");
6605 break;
6606 case RECORD_TYPE:
6607 warning_at (loc, OPT_Wc___compat,
6608 ("struct defined in struct or union "
6609 "is not visible in C++"));
6610 inform (refloc, "struct defined here");
6611 break;
6612 case UNION_TYPE:
6613 warning_at (loc, OPT_Wc___compat,
6614 ("union defined in struct or union "
6615 "is not visible in C++"));
6616 inform (refloc, "union defined here");
6617 break;
6618 default:
6619 gcc_unreachable();
6623 ret.spec = ref;
6624 return ret;
6627 /* If no such tag is yet defined, create a forward-reference node
6628 and record it as the "definition".
6629 When a real declaration of this type is found,
6630 the forward-reference will be altered into a real type. */
6632 ref = make_node (code);
6633 if (code == ENUMERAL_TYPE)
6635 /* Give the type a default layout like unsigned int
6636 to avoid crashing if it does not get defined. */
6637 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6638 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6639 TYPE_USER_ALIGN (ref) = 0;
6640 TYPE_UNSIGNED (ref) = 1;
6641 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6642 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6643 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6646 pushtag (loc, name, ref);
6648 ret.spec = ref;
6649 return ret;
6652 /* Get the struct, enum or union (CODE says which) with tag NAME.
6653 Define the tag as a forward-reference if it is not defined.
6654 Return a tree for the type. */
6656 tree
6657 xref_tag (enum tree_code code, tree name)
6659 return parser_xref_tag (input_location, code, name).spec;
6662 /* Make sure that the tag NAME is defined *in the current scope*
6663 at least as a forward reference.
6664 LOC is the location of the struct's definition.
6665 CODE says which kind of tag NAME ought to be.
6667 This stores the current value of the file static STRUCT_PARSE_INFO
6668 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6669 new c_struct_parse_info structure. The old value of
6670 STRUCT_PARSE_INFO is restored in finish_struct. */
6672 tree
6673 start_struct (location_t loc, enum tree_code code, tree name,
6674 struct c_struct_parse_info **enclosing_struct_parse_info)
6676 /* If there is already a tag defined at this scope
6677 (as a forward reference), just return it. */
6679 tree ref = NULL_TREE;
6680 location_t refloc = UNKNOWN_LOCATION;
6682 if (name != NULL_TREE)
6683 ref = lookup_tag (code, name, 1, &refloc);
6684 if (ref && TREE_CODE (ref) == code)
6686 if (TYPE_SIZE (ref))
6688 if (code == UNION_TYPE)
6689 error_at (loc, "redefinition of %<union %E%>", name);
6690 else
6691 error_at (loc, "redefinition of %<struct %E%>", name);
6692 if (refloc != UNKNOWN_LOCATION)
6693 inform (refloc, "originally defined here");
6694 /* Don't create structures using a name already in use. */
6695 ref = NULL_TREE;
6697 else if (C_TYPE_BEING_DEFINED (ref))
6699 if (code == UNION_TYPE)
6700 error_at (loc, "nested redefinition of %<union %E%>", name);
6701 else
6702 error_at (loc, "nested redefinition of %<struct %E%>", name);
6703 /* Don't bother to report "originally defined here" for a
6704 nested redefinition; the original definition should be
6705 obvious. */
6706 /* Don't create structures that contain themselves. */
6707 ref = NULL_TREE;
6711 /* Otherwise create a forward-reference just so the tag is in scope. */
6713 if (ref == NULL_TREE || TREE_CODE (ref) != code)
6715 ref = make_node (code);
6716 pushtag (loc, name, ref);
6719 C_TYPE_BEING_DEFINED (ref) = 1;
6720 TYPE_PACKED (ref) = flag_pack_struct;
6722 *enclosing_struct_parse_info = struct_parse_info;
6723 struct_parse_info = XNEW (struct c_struct_parse_info);
6724 struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6725 struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6726 struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6728 /* FIXME: This will issue a warning for a use of a type defined
6729 within a statement expr used within sizeof, et. al. This is not
6730 terribly serious as C++ doesn't permit statement exprs within
6731 sizeof anyhow. */
6732 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6733 warning_at (loc, OPT_Wc___compat,
6734 "defining type in %qs expression is invalid in C++",
6735 (in_sizeof
6736 ? "sizeof"
6737 : (in_typeof ? "typeof" : "alignof")));
6739 return ref;
6742 /* Process the specs, declarator and width (NULL if omitted)
6743 of a structure component, returning a FIELD_DECL node.
6744 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6745 DECL_ATTRS is as for grokdeclarator.
6747 LOC is the location of the structure component.
6749 This is done during the parsing of the struct declaration.
6750 The FIELD_DECL nodes are chained together and the lot of them
6751 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6753 tree
6754 grokfield (location_t loc,
6755 struct c_declarator *declarator, struct c_declspecs *declspecs,
6756 tree width, tree *decl_attrs)
6758 tree value;
6760 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6761 && width == NULL_TREE)
6763 /* This is an unnamed decl.
6765 If we have something of the form "union { list } ;" then this
6766 is the anonymous union extension. Similarly for struct.
6768 If this is something of the form "struct foo;", then
6769 If MS or Plan 9 extensions are enabled, this is handled as
6770 an anonymous struct.
6771 Otherwise this is a forward declaration of a structure tag.
6773 If this is something of the form "foo;" and foo is a TYPE_DECL, then
6774 If foo names a structure or union without a tag, then this
6775 is an anonymous struct (this is permitted by C11).
6776 If MS or Plan 9 extensions are enabled and foo names a
6777 structure, then again this is an anonymous struct.
6778 Otherwise this is an error.
6780 Oh what a horrid tangled web we weave. I wonder if MS consciously
6781 took this from Plan 9 or if it was an accident of implementation
6782 that took root before someone noticed the bug... */
6784 tree type = declspecs->type;
6785 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6786 || TREE_CODE (type) == UNION_TYPE);
6787 bool ok = false;
6789 if (type_ok
6790 && (flag_ms_extensions
6791 || flag_plan9_extensions
6792 || !declspecs->typedef_p))
6794 if (flag_ms_extensions || flag_plan9_extensions)
6795 ok = true;
6796 else if (TYPE_NAME (type) == NULL)
6797 ok = true;
6798 else
6799 ok = false;
6801 if (!ok)
6803 pedwarn (loc, 0, "declaration does not declare anything");
6804 return NULL_TREE;
6806 if (!flag_isoc11)
6808 if (flag_isoc99)
6809 pedwarn (loc, OPT_Wpedantic,
6810 "ISO C99 doesn%'t support unnamed structs/unions");
6811 else
6812 pedwarn (loc, OPT_Wpedantic,
6813 "ISO C90 doesn%'t support unnamed structs/unions");
6817 value = grokdeclarator (declarator, declspecs, FIELD, false,
6818 width ? &width : NULL, decl_attrs, NULL, NULL,
6819 DEPRECATED_NORMAL);
6821 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6822 DECL_INITIAL (value) = width;
6824 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6826 /* If we currently have a binding for this field, set the
6827 in_struct field in the binding, so that we warn about lookups
6828 which find it. */
6829 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6830 if (b != NULL)
6832 /* If the in_struct field is not yet set, push it on a list
6833 to be cleared when this struct is finished. */
6834 if (!b->in_struct)
6836 VEC_safe_push (c_binding_ptr, heap,
6837 struct_parse_info->fields, b);
6838 b->in_struct = 1;
6843 return value;
6846 /* Subroutine of detect_field_duplicates: return whether X and Y,
6847 which are both fields in the same struct, have duplicate field
6848 names. */
6850 static bool
6851 is_duplicate_field (tree x, tree y)
6853 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
6854 return true;
6856 /* When using -fplan9-extensions, an anonymous field whose name is a
6857 typedef can duplicate a field name. */
6858 if (flag_plan9_extensions
6859 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
6861 tree xt, xn, yt, yn;
6863 xt = TREE_TYPE (x);
6864 if (DECL_NAME (x) != NULL_TREE)
6865 xn = DECL_NAME (x);
6866 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
6867 && TYPE_NAME (xt) != NULL_TREE
6868 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
6869 xn = DECL_NAME (TYPE_NAME (xt));
6870 else
6871 xn = NULL_TREE;
6873 yt = TREE_TYPE (y);
6874 if (DECL_NAME (y) != NULL_TREE)
6875 yn = DECL_NAME (y);
6876 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
6877 && TYPE_NAME (yt) != NULL_TREE
6878 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
6879 yn = DECL_NAME (TYPE_NAME (yt));
6880 else
6881 yn = NULL_TREE;
6883 if (xn != NULL_TREE && xn == yn)
6884 return true;
6887 return false;
6890 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6891 to HTAB, giving errors for any duplicates. */
6893 static void
6894 detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6896 tree x, y;
6897 void **slot;
6899 for (x = fieldlist; x ; x = DECL_CHAIN (x))
6900 if ((y = DECL_NAME (x)) != 0)
6902 slot = htab_find_slot (htab, y, INSERT);
6903 if (*slot)
6905 error ("duplicate member %q+D", x);
6906 DECL_NAME (x) = NULL_TREE;
6908 *slot = y;
6910 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6911 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6913 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6915 /* When using -fplan9-extensions, an anonymous field whose
6916 name is a typedef can duplicate a field name. */
6917 if (flag_plan9_extensions
6918 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6919 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
6921 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
6922 slot = htab_find_slot (htab, xn, INSERT);
6923 if (*slot)
6924 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
6925 *slot = xn;
6930 /* Generate an error for any duplicate field names in FIELDLIST. Munge
6931 the list such that this does not present a problem later. */
6933 static void
6934 detect_field_duplicates (tree fieldlist)
6936 tree x, y;
6937 int timeout = 10;
6939 /* If the struct is the list of instance variables of an Objective-C
6940 class, then we need to check all the instance variables of
6941 superclasses when checking for duplicates (since you can't have
6942 an instance variable in a subclass with the same name as an
6943 instance variable in a superclass). We pass on this job to the
6944 Objective-C compiler. objc_detect_field_duplicates() will return
6945 false if we are not checking the list of instance variables and
6946 the C frontend should proceed with the standard field duplicate
6947 checks. If we are checking the list of instance variables, the
6948 ObjC frontend will do the check, emit the errors if needed, and
6949 then return true. */
6950 if (c_dialect_objc ())
6951 if (objc_detect_field_duplicates (false))
6952 return;
6954 /* First, see if there are more than "a few" fields.
6955 This is trivially true if there are zero or one fields. */
6956 if (!fieldlist || !DECL_CHAIN (fieldlist))
6957 return;
6958 x = fieldlist;
6959 do {
6960 timeout--;
6961 if (DECL_NAME (x) == NULL_TREE
6962 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6963 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6964 timeout = 0;
6965 x = DECL_CHAIN (x);
6966 } while (timeout > 0 && x);
6968 /* If there were "few" fields and no anonymous structures or unions,
6969 avoid the overhead of allocating a hash table. Instead just do
6970 the nested traversal thing. */
6971 if (timeout > 0)
6973 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
6974 /* When using -fplan9-extensions, we can have duplicates
6975 between typedef names and fields. */
6976 if (DECL_NAME (x)
6977 || (flag_plan9_extensions
6978 && DECL_NAME (x) == NULL_TREE
6979 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6980 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6981 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6982 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
6984 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6985 if (is_duplicate_field (y, x))
6987 error ("duplicate member %q+D", x);
6988 DECL_NAME (x) = NULL_TREE;
6992 else
6994 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6996 detect_field_duplicates_hash (fieldlist, htab);
6997 htab_delete (htab);
7001 /* Finish up struct info used by -Wc++-compat. */
7003 static void
7004 warn_cxx_compat_finish_struct (tree fieldlist)
7006 unsigned int ix;
7007 tree x;
7008 struct c_binding *b;
7010 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7011 the current struct. We do this now at the end of the struct
7012 because the flag is used to issue visibility warnings, and we
7013 only want to issue those warnings if the type is referenced
7014 outside of the struct declaration. */
7015 FOR_EACH_VEC_ELT (tree, struct_parse_info->struct_types, ix, x)
7016 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7018 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7019 typedefs used when declaring fields in this struct. If the name
7020 of any of the fields is also a typedef name then the struct would
7021 not parse in C++, because the C++ lookup rules say that the
7022 typedef name would be looked up in the context of the struct, and
7023 would thus be the field rather than the typedef. */
7024 if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
7025 && fieldlist != NULL_TREE)
7027 /* Use a pointer_set using the name of the typedef. We can use
7028 a pointer_set because identifiers are interned. */
7029 struct pointer_set_t *tset = pointer_set_create ();
7031 FOR_EACH_VEC_ELT (tree, struct_parse_info->typedefs_seen, ix, x)
7032 pointer_set_insert (tset, DECL_NAME (x));
7034 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7036 if (DECL_NAME (x) != NULL_TREE
7037 && pointer_set_contains (tset, DECL_NAME (x)))
7039 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7040 ("using %qD as both field and typedef name is "
7041 "invalid in C++"),
7043 /* FIXME: It would be nice to report the location where
7044 the typedef name is used. */
7048 pointer_set_destroy (tset);
7051 /* For each field which has a binding and which was not defined in
7052 an enclosing struct, clear the in_struct field. */
7053 FOR_EACH_VEC_ELT (c_binding_ptr, struct_parse_info->fields, ix, b)
7054 b->in_struct = 0;
7057 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7058 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7059 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7060 ATTRIBUTES are attributes to be applied to the structure.
7062 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7063 the struct was started. */
7065 tree
7066 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7067 struct c_struct_parse_info *enclosing_struct_parse_info)
7069 tree x;
7070 bool toplevel = file_scope == current_scope;
7071 int saw_named_field;
7073 /* If this type was previously laid out as a forward reference,
7074 make sure we lay it out again. */
7076 TYPE_SIZE (t) = 0;
7078 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7080 if (pedantic)
7082 for (x = fieldlist; x; x = DECL_CHAIN (x))
7084 if (DECL_NAME (x) != 0)
7085 break;
7086 if (flag_isoc11
7087 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7088 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7089 break;
7092 if (x == 0)
7094 if (TREE_CODE (t) == UNION_TYPE)
7096 if (fieldlist)
7097 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7098 else
7099 pedwarn (loc, OPT_Wpedantic, "union has no members");
7101 else
7103 if (fieldlist)
7104 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7105 else
7106 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7111 /* Install struct as DECL_CONTEXT of each field decl.
7112 Also process specified field sizes, found in the DECL_INITIAL,
7113 storing 0 there after the type has been changed to precision equal
7114 to its width, rather than the precision of the specified standard
7115 type. (Correct layout requires the original type to have been preserved
7116 until now.) */
7118 saw_named_field = 0;
7119 for (x = fieldlist; x; x = DECL_CHAIN (x))
7121 if (TREE_TYPE (x) == error_mark_node)
7122 continue;
7124 DECL_CONTEXT (x) = t;
7126 /* If any field is const, the structure type is pseudo-const. */
7127 if (TREE_READONLY (x))
7128 C_TYPE_FIELDS_READONLY (t) = 1;
7129 else
7131 /* A field that is pseudo-const makes the structure likewise. */
7132 tree t1 = strip_array_types (TREE_TYPE (x));
7133 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7134 && C_TYPE_FIELDS_READONLY (t1))
7135 C_TYPE_FIELDS_READONLY (t) = 1;
7138 /* Any field that is volatile means variables of this type must be
7139 treated in some ways as volatile. */
7140 if (TREE_THIS_VOLATILE (x))
7141 C_TYPE_FIELDS_VOLATILE (t) = 1;
7143 /* Any field of nominal variable size implies structure is too. */
7144 if (C_DECL_VARIABLE_SIZE (x))
7145 C_TYPE_VARIABLE_SIZE (t) = 1;
7147 if (DECL_INITIAL (x))
7149 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
7150 DECL_SIZE (x) = bitsize_int (width);
7151 DECL_BIT_FIELD (x) = 1;
7152 SET_DECL_C_BIT_FIELD (x);
7155 if (TYPE_PACKED (t)
7156 && (DECL_BIT_FIELD (x)
7157 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7158 DECL_PACKED (x) = 1;
7160 /* Detect flexible array member in an invalid context. */
7161 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7162 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7163 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7164 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7166 if (TREE_CODE (t) == UNION_TYPE)
7168 error_at (DECL_SOURCE_LOCATION (x),
7169 "flexible array member in union");
7170 TREE_TYPE (x) = error_mark_node;
7172 else if (DECL_CHAIN (x) != NULL_TREE)
7174 error_at (DECL_SOURCE_LOCATION (x),
7175 "flexible array member not at end of struct");
7176 TREE_TYPE (x) = error_mark_node;
7178 else if (!saw_named_field)
7180 error_at (DECL_SOURCE_LOCATION (x),
7181 "flexible array member in otherwise empty struct");
7182 TREE_TYPE (x) = error_mark_node;
7186 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7187 && flexible_array_type_p (TREE_TYPE (x)))
7188 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7189 "invalid use of structure with flexible array member");
7191 if (DECL_NAME (x)
7192 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7193 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7194 saw_named_field = 1;
7197 detect_field_duplicates (fieldlist);
7199 /* Now we have the nearly final fieldlist. Record it,
7200 then lay out the structure or union (including the fields). */
7202 TYPE_FIELDS (t) = fieldlist;
7204 layout_type (t);
7206 /* Give bit-fields their proper types. */
7208 tree *fieldlistp = &fieldlist;
7209 while (*fieldlistp)
7210 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7211 && TREE_TYPE (*fieldlistp) != error_mark_node)
7213 unsigned HOST_WIDE_INT width
7214 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
7215 tree type = TREE_TYPE (*fieldlistp);
7216 if (width != TYPE_PRECISION (type))
7218 TREE_TYPE (*fieldlistp)
7219 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7220 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7222 DECL_INITIAL (*fieldlistp) = 0;
7224 else
7225 fieldlistp = &DECL_CHAIN (*fieldlistp);
7228 /* Now we have the truly final field list.
7229 Store it in this type and in the variants. */
7231 TYPE_FIELDS (t) = fieldlist;
7233 /* If there are lots of fields, sort so we can look through them fast.
7234 We arbitrarily consider 16 or more elts to be "a lot". */
7237 int len = 0;
7239 for (x = fieldlist; x; x = DECL_CHAIN (x))
7241 if (len > 15 || DECL_NAME (x) == NULL)
7242 break;
7243 len += 1;
7246 if (len > 15)
7248 tree *field_array;
7249 struct lang_type *space;
7250 struct sorted_fields_type *space2;
7252 len += list_length (x);
7254 /* Use the same allocation policy here that make_node uses, to
7255 ensure that this lives as long as the rest of the struct decl.
7256 All decls in an inline function need to be saved. */
7258 space = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7259 space2 = ggc_alloc_sorted_fields_type
7260 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7262 len = 0;
7263 space->s = space2;
7264 field_array = &space2->elts[0];
7265 for (x = fieldlist; x; x = DECL_CHAIN (x))
7267 field_array[len++] = x;
7269 /* If there is anonymous struct or union, break out of the loop. */
7270 if (DECL_NAME (x) == NULL)
7271 break;
7273 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7274 if (x == NULL)
7276 TYPE_LANG_SPECIFIC (t) = space;
7277 TYPE_LANG_SPECIFIC (t)->s->len = len;
7278 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7279 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7284 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7286 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7287 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7288 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7289 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7290 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7293 /* If this was supposed to be a transparent union, but we can't
7294 make it one, warn and turn off the flag. */
7295 if (TREE_CODE (t) == UNION_TYPE
7296 && TYPE_TRANSPARENT_AGGR (t)
7297 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7299 TYPE_TRANSPARENT_AGGR (t) = 0;
7300 warning_at (loc, 0, "union cannot be made transparent");
7303 /* If this structure or union completes the type of any previous
7304 variable declaration, lay it out and output its rtl. */
7305 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7307 x = TREE_CHAIN (x))
7309 tree decl = TREE_VALUE (x);
7310 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7311 layout_array_type (TREE_TYPE (decl));
7312 if (TREE_CODE (decl) != TYPE_DECL)
7314 layout_decl (decl, 0);
7315 if (c_dialect_objc ())
7316 objc_check_decl (decl);
7317 rest_of_decl_compilation (decl, toplevel, 0);
7318 if (!toplevel)
7319 expand_decl (decl);
7322 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7324 /* Update type location to the one of the definition, instead of e.g.
7325 a forward declaration. */
7326 if (TYPE_STUB_DECL (t))
7327 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7329 /* Finish debugging output for this type. */
7330 rest_of_type_compilation (t, toplevel);
7332 /* If we're inside a function proper, i.e. not file-scope and not still
7333 parsing parameters, then arrange for the size of a variable sized type
7334 to be bound now. */
7335 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7336 add_stmt (build_stmt (loc,
7337 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7339 if (warn_cxx_compat)
7340 warn_cxx_compat_finish_struct (fieldlist);
7342 VEC_free (tree, heap, struct_parse_info->struct_types);
7343 VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7344 VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7345 XDELETE (struct_parse_info);
7347 struct_parse_info = enclosing_struct_parse_info;
7349 /* If this struct is defined inside a struct, add it to
7350 struct_types. */
7351 if (warn_cxx_compat
7352 && struct_parse_info != NULL
7353 && !in_sizeof && !in_typeof && !in_alignof)
7354 VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7356 return t;
7359 /* Lay out the type T, and its element type, and so on. */
7361 static void
7362 layout_array_type (tree t)
7364 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7365 layout_array_type (TREE_TYPE (t));
7366 layout_type (t);
7369 /* Begin compiling the definition of an enumeration type.
7370 NAME is its name (or null if anonymous).
7371 LOC is the enum's location.
7372 Returns the type object, as yet incomplete.
7373 Also records info about it so that build_enumerator
7374 may be used to declare the individual values as they are read. */
7376 tree
7377 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7379 tree enumtype = NULL_TREE;
7380 location_t enumloc = UNKNOWN_LOCATION;
7382 /* If this is the real definition for a previous forward reference,
7383 fill in the contents in the same object that used to be the
7384 forward reference. */
7386 if (name != NULL_TREE)
7387 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7389 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7391 enumtype = make_node (ENUMERAL_TYPE);
7392 pushtag (loc, name, enumtype);
7395 if (C_TYPE_BEING_DEFINED (enumtype))
7396 error_at (loc, "nested redefinition of %<enum %E%>", name);
7398 C_TYPE_BEING_DEFINED (enumtype) = 1;
7400 if (TYPE_VALUES (enumtype) != 0)
7402 /* This enum is a named one that has been declared already. */
7403 error_at (loc, "redeclaration of %<enum %E%>", name);
7404 if (enumloc != UNKNOWN_LOCATION)
7405 inform (enumloc, "originally defined here");
7407 /* Completely replace its old definition.
7408 The old enumerators remain defined, however. */
7409 TYPE_VALUES (enumtype) = 0;
7412 the_enum->enum_next_value = integer_zero_node;
7413 the_enum->enum_overflow = 0;
7415 if (flag_short_enums)
7416 TYPE_PACKED (enumtype) = 1;
7418 /* FIXME: This will issue a warning for a use of a type defined
7419 within sizeof in a statement expr. This is not terribly serious
7420 as C++ doesn't permit statement exprs within sizeof anyhow. */
7421 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7422 warning_at (loc, OPT_Wc___compat,
7423 "defining type in %qs expression is invalid in C++",
7424 (in_sizeof
7425 ? "sizeof"
7426 : (in_typeof ? "typeof" : "alignof")));
7428 return enumtype;
7431 /* After processing and defining all the values of an enumeration type,
7432 install their decls in the enumeration type and finish it off.
7433 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7434 and ATTRIBUTES are the specified attributes.
7435 Returns ENUMTYPE. */
7437 tree
7438 finish_enum (tree enumtype, tree values, tree attributes)
7440 tree pair, tem;
7441 tree minnode = 0, maxnode = 0;
7442 int precision, unsign;
7443 bool toplevel = (file_scope == current_scope);
7444 struct lang_type *lt;
7446 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7448 /* Calculate the maximum value of any enumerator in this type. */
7450 if (values == error_mark_node)
7451 minnode = maxnode = integer_zero_node;
7452 else
7454 minnode = maxnode = TREE_VALUE (values);
7455 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7457 tree value = TREE_VALUE (pair);
7458 if (tree_int_cst_lt (maxnode, value))
7459 maxnode = value;
7460 if (tree_int_cst_lt (value, minnode))
7461 minnode = value;
7465 /* Construct the final type of this enumeration. It is the same
7466 as one of the integral types - the narrowest one that fits, except
7467 that normally we only go as narrow as int - and signed iff any of
7468 the values are negative. */
7469 unsign = (tree_int_cst_sgn (minnode) >= 0);
7470 precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7471 tree_int_cst_min_precision (maxnode, unsign));
7473 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7475 tem = c_common_type_for_size (precision, unsign);
7476 if (tem == NULL)
7478 warning (0, "enumeration values exceed range of largest integer");
7479 tem = long_long_integer_type_node;
7482 else
7483 tem = unsign ? unsigned_type_node : integer_type_node;
7485 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7486 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7487 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7488 TYPE_SIZE (enumtype) = 0;
7490 /* If the precision of the type was specific with an attribute and it
7491 was too small, give an error. Otherwise, use it. */
7492 if (TYPE_PRECISION (enumtype))
7494 if (precision > TYPE_PRECISION (enumtype))
7495 error ("specified mode too small for enumeral values");
7497 else
7498 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7500 layout_type (enumtype);
7502 if (values != error_mark_node)
7504 /* Change the type of the enumerators to be the enum type. We
7505 need to do this irrespective of the size of the enum, for
7506 proper type checking. Replace the DECL_INITIALs of the
7507 enumerators, and the value slots of the list, with copies
7508 that have the enum type; they cannot be modified in place
7509 because they may be shared (e.g. integer_zero_node) Finally,
7510 change the purpose slots to point to the names of the decls. */
7511 for (pair = values; pair; pair = TREE_CHAIN (pair))
7513 tree enu = TREE_PURPOSE (pair);
7514 tree ini = DECL_INITIAL (enu);
7516 TREE_TYPE (enu) = enumtype;
7518 /* The ISO C Standard mandates enumerators to have type int,
7519 even though the underlying type of an enum type is
7520 unspecified. However, GCC allows enumerators of any
7521 integer type as an extensions. build_enumerator()
7522 converts any enumerators that fit in an int to type int,
7523 to avoid promotions to unsigned types when comparing
7524 integers with enumerators that fit in the int range.
7525 When -pedantic is given, build_enumerator() would have
7526 already warned about those that don't fit. Here we
7527 convert the rest to the enumerator type. */
7528 if (TREE_TYPE (ini) != integer_type_node)
7529 ini = convert (enumtype, ini);
7531 DECL_INITIAL (enu) = ini;
7532 TREE_PURPOSE (pair) = DECL_NAME (enu);
7533 TREE_VALUE (pair) = ini;
7536 TYPE_VALUES (enumtype) = values;
7539 /* Record the min/max values so that we can warn about bit-field
7540 enumerations that are too small for the values. */
7541 lt = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7542 lt->enum_min = minnode;
7543 lt->enum_max = maxnode;
7544 TYPE_LANG_SPECIFIC (enumtype) = lt;
7546 /* Fix up all variant types of this enum type. */
7547 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7549 if (tem == enumtype)
7550 continue;
7551 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7552 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7553 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7554 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7555 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7556 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7557 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7558 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7559 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7560 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7561 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7564 /* Finish debugging output for this type. */
7565 rest_of_type_compilation (enumtype, toplevel);
7567 /* If this enum is defined inside a struct, add it to
7568 struct_types. */
7569 if (warn_cxx_compat
7570 && struct_parse_info != NULL
7571 && !in_sizeof && !in_typeof && !in_alignof)
7572 VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7574 return enumtype;
7577 /* Build and install a CONST_DECL for one value of the
7578 current enumeration type (one that was begun with start_enum).
7579 DECL_LOC is the location of the enumerator.
7580 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7581 Return a tree-list containing the CONST_DECL and its value.
7582 Assignment of sequential values by default is handled here. */
7584 tree
7585 build_enumerator (location_t decl_loc, location_t loc,
7586 struct c_enum_contents *the_enum, tree name, tree value)
7588 tree decl, type;
7590 /* Validate and default VALUE. */
7592 if (value != 0)
7594 /* Don't issue more errors for error_mark_node (i.e. an
7595 undeclared identifier) - just ignore the value expression. */
7596 if (value == error_mark_node)
7597 value = 0;
7598 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7600 error_at (loc, "enumerator value for %qE is not an integer constant",
7601 name);
7602 value = 0;
7604 else
7606 if (TREE_CODE (value) != INTEGER_CST)
7608 value = c_fully_fold (value, false, NULL);
7609 if (TREE_CODE (value) == INTEGER_CST)
7610 pedwarn (loc, OPT_Wpedantic,
7611 "enumerator value for %qE is not an integer "
7612 "constant expression", name);
7614 if (TREE_CODE (value) != INTEGER_CST)
7616 error ("enumerator value for %qE is not an integer constant",
7617 name);
7618 value = 0;
7620 else
7622 value = default_conversion (value);
7623 constant_expression_warning (value);
7628 /* Default based on previous value. */
7629 /* It should no longer be possible to have NON_LVALUE_EXPR
7630 in the default. */
7631 if (value == 0)
7633 value = the_enum->enum_next_value;
7634 if (the_enum->enum_overflow)
7635 error_at (loc, "overflow in enumeration values");
7637 /* Even though the underlying type of an enum is unspecified, the
7638 type of enumeration constants is explicitly defined as int
7639 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7640 an extension. */
7641 else if (!int_fits_type_p (value, integer_type_node))
7642 pedwarn (loc, OPT_Wpedantic,
7643 "ISO C restricts enumerator values to range of %<int%>");
7645 /* The ISO C Standard mandates enumerators to have type int, even
7646 though the underlying type of an enum type is unspecified.
7647 However, GCC allows enumerators of any integer type as an
7648 extensions. Here we convert any enumerators that fit in an int
7649 to type int, to avoid promotions to unsigned types when comparing
7650 integers with enumerators that fit in the int range. When
7651 -pedantic is given, we would have already warned about those that
7652 don't fit. We have to do this here rather than in finish_enum
7653 because this value may be used to define more enumerators. */
7654 if (int_fits_type_p (value, integer_type_node))
7655 value = convert (integer_type_node, value);
7657 /* Set basis for default for next value. */
7658 the_enum->enum_next_value
7659 = build_binary_op (EXPR_LOC_OR_HERE (value),
7660 PLUS_EXPR, value, integer_one_node, 0);
7661 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7663 /* Now create a declaration for the enum value name. */
7665 type = TREE_TYPE (value);
7666 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7667 TYPE_PRECISION (integer_type_node)),
7668 (TYPE_PRECISION (type)
7669 >= TYPE_PRECISION (integer_type_node)
7670 && TYPE_UNSIGNED (type)));
7672 decl = build_decl (decl_loc, CONST_DECL, name, type);
7673 DECL_INITIAL (decl) = convert (type, value);
7674 pushdecl (decl);
7676 return tree_cons (decl, value, NULL_TREE);
7680 /* Create the FUNCTION_DECL for a function definition.
7681 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7682 the declaration; they describe the function's name and the type it returns,
7683 but twisted together in a fashion that parallels the syntax of C.
7685 This function creates a binding context for the function body
7686 as well as setting up the FUNCTION_DECL in current_function_decl.
7688 Returns 1 on success. If the DECLARATOR is not suitable for a function
7689 (it defines a datum instead), we return 0, which tells
7690 yyparse to report a parse error. */
7693 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7694 tree attributes)
7696 tree decl1, old_decl;
7697 tree restype, resdecl;
7698 location_t loc;
7700 current_function_returns_value = 0; /* Assume, until we see it does. */
7701 current_function_returns_null = 0;
7702 current_function_returns_abnormally = 0;
7703 warn_about_return_type = 0;
7704 c_switch_stack = NULL;
7706 /* Indicate no valid break/continue context by setting these variables
7707 to some non-null, non-label value. We'll notice and emit the proper
7708 error message in c_finish_bc_stmt. */
7709 c_break_label = c_cont_label = size_zero_node;
7711 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7712 &attributes, NULL, NULL, DEPRECATED_NORMAL);
7714 /* If the declarator is not suitable for a function definition,
7715 cause a syntax error. */
7716 if (decl1 == 0
7717 || TREE_CODE (decl1) != FUNCTION_DECL)
7718 return 0;
7720 loc = DECL_SOURCE_LOCATION (decl1);
7722 decl_attributes (&decl1, attributes, 0);
7724 if (DECL_DECLARED_INLINE_P (decl1)
7725 && DECL_UNINLINABLE (decl1)
7726 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7727 warning_at (loc, OPT_Wattributes,
7728 "inline function %qD given attribute noinline",
7729 decl1);
7731 /* Handle gnu_inline attribute. */
7732 if (declspecs->inline_p
7733 && !flag_gnu89_inline
7734 && TREE_CODE (decl1) == FUNCTION_DECL
7735 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7736 || current_function_decl))
7738 if (declspecs->storage_class != csc_static)
7739 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7742 announce_function (decl1);
7744 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7746 error_at (loc, "return type is an incomplete type");
7747 /* Make it return void instead. */
7748 TREE_TYPE (decl1)
7749 = build_function_type (void_type_node,
7750 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7753 if (warn_about_return_type)
7754 pedwarn_c99 (loc, flag_isoc99 ? 0
7755 : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7756 "return type defaults to %<int%>");
7758 /* Make the init_value nonzero so pushdecl knows this is not tentative.
7759 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7760 DECL_INITIAL (decl1) = error_mark_node;
7762 /* A nested function is not global. */
7763 if (current_function_decl != 0)
7764 TREE_PUBLIC (decl1) = 0;
7766 /* If this definition isn't a prototype and we had a prototype declaration
7767 before, copy the arg type info from that prototype. */
7768 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7769 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7770 old_decl = 0;
7771 current_function_prototype_locus = UNKNOWN_LOCATION;
7772 current_function_prototype_built_in = false;
7773 current_function_prototype_arg_types = NULL_TREE;
7774 if (!prototype_p (TREE_TYPE (decl1)))
7776 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7777 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7778 TREE_TYPE (TREE_TYPE (old_decl))))
7780 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7781 TREE_TYPE (decl1));
7782 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7783 current_function_prototype_built_in
7784 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7785 current_function_prototype_arg_types
7786 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7788 if (TREE_PUBLIC (decl1))
7790 /* If there is an external prototype declaration of this
7791 function, record its location but do not copy information
7792 to this decl. This may be an invisible declaration
7793 (built-in or in a scope which has finished) or simply
7794 have more refined argument types than any declaration
7795 found above. */
7796 struct c_binding *b;
7797 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7798 if (B_IN_SCOPE (b, external_scope))
7799 break;
7800 if (b)
7802 tree ext_decl, ext_type;
7803 ext_decl = b->decl;
7804 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7805 if (TREE_CODE (ext_type) == FUNCTION_TYPE
7806 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7807 TREE_TYPE (ext_type)))
7809 current_function_prototype_locus
7810 = DECL_SOURCE_LOCATION (ext_decl);
7811 current_function_prototype_built_in
7812 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7813 current_function_prototype_arg_types
7814 = TYPE_ARG_TYPES (ext_type);
7820 /* Optionally warn of old-fashioned def with no previous prototype. */
7821 if (warn_strict_prototypes
7822 && old_decl != error_mark_node
7823 && !prototype_p (TREE_TYPE (decl1))
7824 && C_DECL_ISNT_PROTOTYPE (old_decl))
7825 warning_at (loc, OPT_Wstrict_prototypes,
7826 "function declaration isn%'t a prototype");
7827 /* Optionally warn of any global def with no previous prototype. */
7828 else if (warn_missing_prototypes
7829 && old_decl != error_mark_node
7830 && TREE_PUBLIC (decl1)
7831 && !MAIN_NAME_P (DECL_NAME (decl1))
7832 && C_DECL_ISNT_PROTOTYPE (old_decl))
7833 warning_at (loc, OPT_Wmissing_prototypes,
7834 "no previous prototype for %qD", decl1);
7835 /* Optionally warn of any def with no previous prototype
7836 if the function has already been used. */
7837 else if (warn_missing_prototypes
7838 && old_decl != 0
7839 && old_decl != error_mark_node
7840 && TREE_USED (old_decl)
7841 && !prototype_p (TREE_TYPE (old_decl)))
7842 warning_at (loc, OPT_Wmissing_prototypes,
7843 "%qD was used with no prototype before its definition", decl1);
7844 /* Optionally warn of any global def with no previous declaration. */
7845 else if (warn_missing_declarations
7846 && TREE_PUBLIC (decl1)
7847 && old_decl == 0
7848 && !MAIN_NAME_P (DECL_NAME (decl1)))
7849 warning_at (loc, OPT_Wmissing_declarations,
7850 "no previous declaration for %qD",
7851 decl1);
7852 /* Optionally warn of any def with no previous declaration
7853 if the function has already been used. */
7854 else if (warn_missing_declarations
7855 && old_decl != 0
7856 && old_decl != error_mark_node
7857 && TREE_USED (old_decl)
7858 && C_DECL_IMPLICIT (old_decl))
7859 warning_at (loc, OPT_Wmissing_declarations,
7860 "%qD was used with no declaration before its definition", decl1);
7862 /* This function exists in static storage.
7863 (This does not mean `static' in the C sense!) */
7864 TREE_STATIC (decl1) = 1;
7866 /* This is the earliest point at which we might know the assembler
7867 name of the function. Thus, if it's set before this, die horribly. */
7868 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7870 /* If #pragma weak was used, mark the decl weak now. */
7871 if (current_scope == file_scope)
7872 maybe_apply_pragma_weak (decl1);
7874 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7875 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7877 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7878 != integer_type_node)
7879 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7881 check_main_parameter_types (decl1);
7883 if (!TREE_PUBLIC (decl1))
7884 pedwarn (loc, OPT_Wmain,
7885 "%qD is normally a non-static function", decl1);
7888 /* Record the decl so that the function name is defined.
7889 If we already have a decl for this name, and it is a FUNCTION_DECL,
7890 use the old decl. */
7892 current_function_decl = pushdecl (decl1);
7894 push_scope ();
7895 declare_parm_level ();
7897 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7898 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7899 DECL_ARTIFICIAL (resdecl) = 1;
7900 DECL_IGNORED_P (resdecl) = 1;
7901 DECL_RESULT (current_function_decl) = resdecl;
7903 start_fname_decls ();
7905 return 1;
7908 /* Subroutine of store_parm_decls which handles new-style function
7909 definitions (prototype format). The parms already have decls, so we
7910 need only record them as in effect and complain if any redundant
7911 old-style parm decls were written. */
7912 static void
7913 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7915 tree decl;
7916 c_arg_tag *tag;
7917 unsigned ix;
7919 if (current_scope->bindings)
7921 error_at (DECL_SOURCE_LOCATION (fndecl),
7922 "old-style parameter declarations in prototyped "
7923 "function definition");
7925 /* Get rid of the old-style declarations. */
7926 pop_scope ();
7927 push_scope ();
7929 /* Don't issue this warning for nested functions, and don't issue this
7930 warning if we got here because ARG_INFO_TYPES was error_mark_node
7931 (this happens when a function definition has just an ellipsis in
7932 its parameter list). */
7933 else if (!in_system_header && !current_function_scope
7934 && arg_info->types != error_mark_node)
7935 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7936 "traditional C rejects ISO C style function definitions");
7938 /* Now make all the parameter declarations visible in the function body.
7939 We can bypass most of the grunt work of pushdecl. */
7940 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
7942 DECL_CONTEXT (decl) = current_function_decl;
7943 if (DECL_NAME (decl))
7945 bind (DECL_NAME (decl), decl, current_scope,
7946 /*invisible=*/false, /*nested=*/false,
7947 UNKNOWN_LOCATION);
7948 if (!TREE_USED (decl))
7949 warn_if_shadowing (decl);
7951 else
7952 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7955 /* Record the parameter list in the function declaration. */
7956 DECL_ARGUMENTS (fndecl) = arg_info->parms;
7958 /* Now make all the ancillary declarations visible, likewise. */
7959 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
7961 DECL_CONTEXT (decl) = current_function_decl;
7962 if (DECL_NAME (decl))
7963 bind (DECL_NAME (decl), decl, current_scope,
7964 /*invisible=*/false,
7965 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7966 UNKNOWN_LOCATION);
7969 /* And all the tag declarations. */
7970 FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
7971 if (tag->id)
7972 bind (tag->id, tag->type, current_scope,
7973 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7976 /* Subroutine of store_parm_decls which handles old-style function
7977 definitions (separate parameter list and declarations). */
7979 static void
7980 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7982 struct c_binding *b;
7983 tree parm, decl, last;
7984 tree parmids = arg_info->parms;
7985 struct pointer_set_t *seen_args = pointer_set_create ();
7987 if (!in_system_header)
7988 warning_at (DECL_SOURCE_LOCATION (fndecl),
7989 OPT_Wold_style_definition, "old-style function definition");
7991 /* Match each formal parameter name with its declaration. Save each
7992 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7993 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7995 if (TREE_VALUE (parm) == 0)
7997 error_at (DECL_SOURCE_LOCATION (fndecl),
7998 "parameter name missing from parameter list");
7999 TREE_PURPOSE (parm) = 0;
8000 continue;
8003 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8004 if (b && B_IN_CURRENT_SCOPE (b))
8006 decl = b->decl;
8007 /* Skip erroneous parameters. */
8008 if (decl == error_mark_node)
8009 continue;
8010 /* If we got something other than a PARM_DECL it is an error. */
8011 if (TREE_CODE (decl) != PARM_DECL)
8012 error_at (DECL_SOURCE_LOCATION (decl),
8013 "%qD declared as a non-parameter", decl);
8014 /* If the declaration is already marked, we have a duplicate
8015 name. Complain and ignore the duplicate. */
8016 else if (pointer_set_contains (seen_args, decl))
8018 error_at (DECL_SOURCE_LOCATION (decl),
8019 "multiple parameters named %qD", decl);
8020 TREE_PURPOSE (parm) = 0;
8021 continue;
8023 /* If the declaration says "void", complain and turn it into
8024 an int. */
8025 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8027 error_at (DECL_SOURCE_LOCATION (decl),
8028 "parameter %qD declared with void type", decl);
8029 TREE_TYPE (decl) = integer_type_node;
8030 DECL_ARG_TYPE (decl) = integer_type_node;
8031 layout_decl (decl, 0);
8033 warn_if_shadowing (decl);
8035 /* If no declaration found, default to int. */
8036 else
8038 /* FIXME diagnostics: This should be the location of the argument,
8039 not the FNDECL. E.g., for an old-style declaration
8041 int f10(v) { blah; }
8043 We should use the location of the V, not the F10.
8044 Unfortunately, the V is an IDENTIFIER_NODE which has no
8045 location. In the future we need locations for c_arg_info
8046 entries.
8048 See gcc.dg/Wshadow-3.c for an example of this problem. */
8049 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8050 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8051 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8052 pushdecl (decl);
8053 warn_if_shadowing (decl);
8055 if (flag_isoc99)
8056 pedwarn (DECL_SOURCE_LOCATION (decl),
8057 0, "type of %qD defaults to %<int%>", decl);
8058 else
8059 warning_at (DECL_SOURCE_LOCATION (decl),
8060 OPT_Wmissing_parameter_type,
8061 "type of %qD defaults to %<int%>", decl);
8064 TREE_PURPOSE (parm) = decl;
8065 pointer_set_insert (seen_args, decl);
8068 /* Now examine the parms chain for incomplete declarations
8069 and declarations with no corresponding names. */
8071 for (b = current_scope->bindings; b; b = b->prev)
8073 parm = b->decl;
8074 if (TREE_CODE (parm) != PARM_DECL)
8075 continue;
8077 if (TREE_TYPE (parm) != error_mark_node
8078 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8080 error_at (DECL_SOURCE_LOCATION (parm),
8081 "parameter %qD has incomplete type", parm);
8082 TREE_TYPE (parm) = error_mark_node;
8085 if (!pointer_set_contains (seen_args, parm))
8087 error_at (DECL_SOURCE_LOCATION (parm),
8088 "declaration for parameter %qD but no such parameter",
8089 parm);
8091 /* Pretend the parameter was not missing.
8092 This gets us to a standard state and minimizes
8093 further error messages. */
8094 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8098 /* Chain the declarations together in the order of the list of
8099 names. Store that chain in the function decl, replacing the
8100 list of names. Update the current scope to match. */
8101 DECL_ARGUMENTS (fndecl) = 0;
8103 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8104 if (TREE_PURPOSE (parm))
8105 break;
8106 if (parm && TREE_PURPOSE (parm))
8108 last = TREE_PURPOSE (parm);
8109 DECL_ARGUMENTS (fndecl) = last;
8111 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8112 if (TREE_PURPOSE (parm))
8114 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8115 last = TREE_PURPOSE (parm);
8117 DECL_CHAIN (last) = 0;
8120 pointer_set_destroy (seen_args);
8122 /* If there was a previous prototype,
8123 set the DECL_ARG_TYPE of each argument according to
8124 the type previously specified, and report any mismatches. */
8126 if (current_function_prototype_arg_types)
8128 tree type;
8129 for (parm = DECL_ARGUMENTS (fndecl),
8130 type = current_function_prototype_arg_types;
8131 parm || (type && TREE_VALUE (type) != error_mark_node
8132 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8133 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8135 if (parm == 0 || type == 0
8136 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8138 if (current_function_prototype_built_in)
8139 warning_at (DECL_SOURCE_LOCATION (fndecl),
8140 0, "number of arguments doesn%'t match "
8141 "built-in prototype");
8142 else
8144 /* FIXME diagnostics: This should be the location of
8145 FNDECL, but there is bug when a prototype is
8146 declared inside function context, but defined
8147 outside of it (e.g., gcc.dg/pr15698-2.c). In
8148 which case FNDECL gets the location of the
8149 prototype, not the definition. */
8150 error_at (input_location,
8151 "number of arguments doesn%'t match prototype");
8153 error_at (current_function_prototype_locus,
8154 "prototype declaration");
8156 break;
8158 /* Type for passing arg must be consistent with that
8159 declared for the arg. ISO C says we take the unqualified
8160 type for parameters declared with qualified type. */
8161 if (TREE_TYPE (parm) != error_mark_node
8162 && TREE_TYPE (type) != error_mark_node
8163 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8164 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8166 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8167 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
8169 /* Adjust argument to match prototype. E.g. a previous
8170 `int foo(float);' prototype causes
8171 `int foo(x) float x; {...}' to be treated like
8172 `int foo(float x) {...}'. This is particularly
8173 useful for argument types like uid_t. */
8174 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8176 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8177 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8178 && TYPE_PRECISION (TREE_TYPE (parm))
8179 < TYPE_PRECISION (integer_type_node))
8180 DECL_ARG_TYPE (parm) = integer_type_node;
8182 /* ??? Is it possible to get here with a
8183 built-in prototype or will it always have
8184 been diagnosed as conflicting with an
8185 old-style definition and discarded? */
8186 if (current_function_prototype_built_in)
8187 warning_at (DECL_SOURCE_LOCATION (parm),
8188 OPT_Wpedantic, "promoted argument %qD "
8189 "doesn%'t match built-in prototype", parm);
8190 else
8192 pedwarn (DECL_SOURCE_LOCATION (parm),
8193 OPT_Wpedantic, "promoted argument %qD "
8194 "doesn%'t match prototype", parm);
8195 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8196 "prototype declaration");
8199 else
8201 if (current_function_prototype_built_in)
8202 warning_at (DECL_SOURCE_LOCATION (parm),
8203 0, "argument %qD doesn%'t match "
8204 "built-in prototype", parm);
8205 else
8207 error_at (DECL_SOURCE_LOCATION (parm),
8208 "argument %qD doesn%'t match prototype", parm);
8209 error_at (current_function_prototype_locus,
8210 "prototype declaration");
8215 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8218 /* Otherwise, create a prototype that would match. */
8220 else
8222 tree actual = 0, last = 0, type;
8224 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8226 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8227 if (last)
8228 TREE_CHAIN (last) = type;
8229 else
8230 actual = type;
8231 last = type;
8233 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8234 if (last)
8235 TREE_CHAIN (last) = type;
8236 else
8237 actual = type;
8239 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8240 of the type of this function, but we need to avoid having this
8241 affect the types of other similarly-typed functions, so we must
8242 first force the generation of an identical (but separate) type
8243 node for the relevant function type. The new node we create
8244 will be a variant of the main variant of the original function
8245 type. */
8247 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8249 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8253 /* Store parameter declarations passed in ARG_INFO into the current
8254 function declaration. */
8256 void
8257 store_parm_decls_from (struct c_arg_info *arg_info)
8259 current_function_arg_info = arg_info;
8260 store_parm_decls ();
8263 /* Store the parameter declarations into the current function declaration.
8264 This is called after parsing the parameter declarations, before
8265 digesting the body of the function.
8267 For an old-style definition, construct a prototype out of the old-style
8268 parameter declarations and inject it into the function's type. */
8270 void
8271 store_parm_decls (void)
8273 tree fndecl = current_function_decl;
8274 bool proto;
8276 /* The argument information block for FNDECL. */
8277 struct c_arg_info *arg_info = current_function_arg_info;
8278 current_function_arg_info = 0;
8280 /* True if this definition is written with a prototype. Note:
8281 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8282 list in a function definition as equivalent to (void) -- an
8283 empty argument list specifies the function has no parameters,
8284 but only (void) sets up a prototype for future calls. */
8285 proto = arg_info->types != 0;
8287 if (proto)
8288 store_parm_decls_newstyle (fndecl, arg_info);
8289 else
8290 store_parm_decls_oldstyle (fndecl, arg_info);
8292 /* The next call to push_scope will be a function body. */
8294 next_is_function_body = true;
8296 /* Write a record describing this function definition to the prototypes
8297 file (if requested). */
8299 gen_aux_info_record (fndecl, 1, 0, proto);
8301 /* Initialize the RTL code for the function. */
8302 allocate_struct_function (fndecl, false);
8304 if (warn_unused_local_typedefs)
8305 cfun->language = ggc_alloc_cleared_language_function ();
8307 /* Begin the statement tree for this function. */
8308 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8310 /* ??? Insert the contents of the pending sizes list into the function
8311 to be evaluated. The only reason left to have this is
8312 void foo(int n, int array[n++])
8313 because we throw away the array type in favor of a pointer type, and
8314 thus won't naturally see the SAVE_EXPR containing the increment. All
8315 other pending sizes would be handled by gimplify_parameters. */
8316 if (arg_info->pending_sizes)
8317 add_stmt (arg_info->pending_sizes);
8321 /* Finish up a function declaration and compile that function
8322 all the way to assembler language output. Then free the storage
8323 for the function definition.
8325 This is called after parsing the body of the function definition. */
8327 void
8328 finish_function (void)
8330 tree fndecl = current_function_decl;
8332 if (c_dialect_objc ())
8333 objc_finish_function ();
8335 if (TREE_CODE (fndecl) == FUNCTION_DECL
8336 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8338 tree args = DECL_ARGUMENTS (fndecl);
8339 for (; args; args = DECL_CHAIN (args))
8341 tree type = TREE_TYPE (args);
8342 if (INTEGRAL_TYPE_P (type)
8343 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8344 DECL_ARG_TYPE (args) = integer_type_node;
8348 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8349 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8351 /* Must mark the RESULT_DECL as being in this function. */
8353 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8354 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8356 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8357 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8358 == integer_type_node && flag_isoc99)
8360 /* Hack. We don't want the middle-end to warn that this return
8361 is unreachable, so we mark its location as special. Using
8362 UNKNOWN_LOCATION has the problem that it gets clobbered in
8363 annotate_one_with_locus. A cleaner solution might be to
8364 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8366 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8369 /* Tie off the statement tree for this function. */
8370 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8372 finish_fname_decls ();
8374 /* Complain if there's just no return statement. */
8375 if (warn_return_type
8376 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8377 && !current_function_returns_value && !current_function_returns_null
8378 /* Don't complain if we are no-return. */
8379 && !current_function_returns_abnormally
8380 /* Don't complain if we are declared noreturn. */
8381 && !TREE_THIS_VOLATILE (fndecl)
8382 /* Don't warn for main(). */
8383 && !MAIN_NAME_P (DECL_NAME (fndecl))
8384 /* Or if they didn't actually specify a return type. */
8385 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8386 /* Normally, with -Wreturn-type, flow will complain, but we might
8387 optimize out static functions. */
8388 && !TREE_PUBLIC (fndecl))
8390 warning (OPT_Wreturn_type,
8391 "no return statement in function returning non-void");
8392 TREE_NO_WARNING (fndecl) = 1;
8395 /* Complain about parameters that are only set, but never otherwise used. */
8396 if (warn_unused_but_set_parameter)
8398 tree decl;
8400 for (decl = DECL_ARGUMENTS (fndecl);
8401 decl;
8402 decl = DECL_CHAIN (decl))
8403 if (TREE_USED (decl)
8404 && TREE_CODE (decl) == PARM_DECL
8405 && !DECL_READ_P (decl)
8406 && DECL_NAME (decl)
8407 && !DECL_ARTIFICIAL (decl)
8408 && !TREE_NO_WARNING (decl))
8409 warning_at (DECL_SOURCE_LOCATION (decl),
8410 OPT_Wunused_but_set_parameter,
8411 "parameter %qD set but not used", decl);
8414 /* Complain about locally defined typedefs that are not used in this
8415 function. */
8416 maybe_warn_unused_local_typedefs ();
8418 /* Store the end of the function, so that we get good line number
8419 info for the epilogue. */
8420 cfun->function_end_locus = input_location;
8422 /* Finalize the ELF visibility for the function. */
8423 c_determine_visibility (fndecl);
8425 /* For GNU C extern inline functions disregard inline limits. */
8426 if (DECL_EXTERNAL (fndecl)
8427 && DECL_DECLARED_INLINE_P (fndecl))
8428 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8430 /* Genericize before inlining. Delay genericizing nested functions
8431 until their parent function is genericized. Since finalizing
8432 requires GENERIC, delay that as well. */
8434 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8435 && !undef_nested_function)
8437 if (!decl_function_context (fndecl))
8439 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8440 c_genericize (fndecl);
8442 /* ??? Objc emits functions after finalizing the compilation unit.
8443 This should be cleaned up later and this conditional removed. */
8444 if (cgraph_global_info_ready)
8446 cgraph_add_new_function (fndecl, false);
8447 return;
8449 cgraph_finalize_function (fndecl, false);
8451 else
8453 /* Register this function with cgraph just far enough to get it
8454 added to our parent's nested function list. Handy, since the
8455 C front end doesn't have such a list. */
8456 (void) cgraph_get_create_node (fndecl);
8460 if (!decl_function_context (fndecl))
8461 undef_nested_function = false;
8463 if (cfun->language != NULL)
8465 ggc_free (cfun->language);
8466 cfun->language = NULL;
8469 /* We're leaving the context of this function, so zap cfun.
8470 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8471 tree_rest_of_compilation. */
8472 set_cfun (NULL);
8473 current_function_decl = NULL;
8476 /* Check the declarations given in a for-loop for satisfying the C99
8477 constraints. If exactly one such decl is found, return it. LOC is
8478 the location of the opening parenthesis of the for loop. The last
8479 parameter allows you to control the "for loop initial declarations
8480 are only allowed in C99 mode". Normally, you should pass
8481 flag_isoc99 as that parameter. But in some cases (Objective-C
8482 foreach loop, for example) we want to run the checks in this
8483 function even if not in C99 mode, so we allow the caller to turn
8484 off the error about not being in C99 mode.
8487 tree
8488 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8490 struct c_binding *b;
8491 tree one_decl = NULL_TREE;
8492 int n_decls = 0;
8494 if (!turn_off_iso_c99_error)
8496 static bool hint = true;
8497 /* If we get here, declarations have been used in a for loop without
8498 the C99 for loop scope. This doesn't make much sense, so don't
8499 allow it. */
8500 error_at (loc, "%<for%> loop initial declarations "
8501 "are only allowed in C99 mode");
8502 if (hint)
8504 inform (loc,
8505 "use option -std=c99 or -std=gnu99 to compile your code");
8506 hint = false;
8508 return NULL_TREE;
8510 /* C99 subclause 6.8.5 paragraph 3:
8512 [#3] The declaration part of a for statement shall only
8513 declare identifiers for objects having storage class auto or
8514 register.
8516 It isn't clear whether, in this sentence, "identifiers" binds to
8517 "shall only declare" or to "objects" - that is, whether all identifiers
8518 declared must be identifiers for objects, or whether the restriction
8519 only applies to those that are. (A question on this in comp.std.c
8520 in November 2000 received no answer.) We implement the strictest
8521 interpretation, to avoid creating an extension which later causes
8522 problems. */
8524 for (b = current_scope->bindings; b; b = b->prev)
8526 tree id = b->id;
8527 tree decl = b->decl;
8529 if (!id)
8530 continue;
8532 switch (TREE_CODE (decl))
8534 case VAR_DECL:
8536 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8537 if (TREE_STATIC (decl))
8538 error_at (decl_loc,
8539 "declaration of static variable %qD in %<for%> loop "
8540 "initial declaration", decl);
8541 else if (DECL_EXTERNAL (decl))
8542 error_at (decl_loc,
8543 "declaration of %<extern%> variable %qD in %<for%> loop "
8544 "initial declaration", decl);
8546 break;
8548 case RECORD_TYPE:
8549 error_at (loc,
8550 "%<struct %E%> declared in %<for%> loop initial "
8551 "declaration", id);
8552 break;
8553 case UNION_TYPE:
8554 error_at (loc,
8555 "%<union %E%> declared in %<for%> loop initial declaration",
8556 id);
8557 break;
8558 case ENUMERAL_TYPE:
8559 error_at (loc, "%<enum %E%> declared in %<for%> loop "
8560 "initial declaration", id);
8561 break;
8562 default:
8563 error_at (loc, "declaration of non-variable "
8564 "%qD in %<for%> loop initial declaration", decl);
8567 n_decls++;
8568 one_decl = decl;
8571 return n_decls == 1 ? one_decl : NULL_TREE;
8574 /* Save and reinitialize the variables
8575 used during compilation of a C function. */
8577 void
8578 c_push_function_context (void)
8580 struct language_function *p = cfun->language;
8581 /* cfun->language might have been already allocated by the use of
8582 -Wunused-local-typedefs. In that case, just re-use it. */
8583 if (p == NULL)
8584 cfun->language = p = ggc_alloc_cleared_language_function ();
8586 p->base.x_stmt_tree = c_stmt_tree;
8587 c_stmt_tree.x_cur_stmt_list
8588 = VEC_copy (tree, gc, c_stmt_tree.x_cur_stmt_list);
8589 p->x_break_label = c_break_label;
8590 p->x_cont_label = c_cont_label;
8591 p->x_switch_stack = c_switch_stack;
8592 p->arg_info = current_function_arg_info;
8593 p->returns_value = current_function_returns_value;
8594 p->returns_null = current_function_returns_null;
8595 p->returns_abnormally = current_function_returns_abnormally;
8596 p->warn_about_return_type = warn_about_return_type;
8598 push_function_context ();
8601 /* Restore the variables used during compilation of a C function. */
8603 void
8604 c_pop_function_context (void)
8606 struct language_function *p;
8608 pop_function_context ();
8609 p = cfun->language;
8610 /* When -Wunused-local-typedefs is in effect, cfun->languages is
8611 used to store data throughout the life time of the current cfun,
8612 So don't deallocate it. */
8613 if (!warn_unused_local_typedefs)
8614 cfun->language = NULL;
8616 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8617 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8619 /* Stop pointing to the local nodes about to be freed. */
8620 /* But DECL_INITIAL must remain nonzero so we know this
8621 was an actual function definition. */
8622 DECL_INITIAL (current_function_decl) = error_mark_node;
8623 DECL_ARGUMENTS (current_function_decl) = 0;
8626 c_stmt_tree = p->base.x_stmt_tree;
8627 c_break_label = p->x_break_label;
8628 c_cont_label = p->x_cont_label;
8629 c_switch_stack = p->x_switch_stack;
8630 current_function_arg_info = p->arg_info;
8631 current_function_returns_value = p->returns_value;
8632 current_function_returns_null = p->returns_null;
8633 current_function_returns_abnormally = p->returns_abnormally;
8634 warn_about_return_type = p->warn_about_return_type;
8637 /* The functions below are required for functionality of doing
8638 function at once processing in the C front end. Currently these
8639 functions are not called from anywhere in the C front end, but as
8640 these changes continue, that will change. */
8642 /* Returns the stmt_tree (if any) to which statements are currently
8643 being added. If there is no active statement-tree, NULL is
8644 returned. */
8646 stmt_tree
8647 current_stmt_tree (void)
8649 return &c_stmt_tree;
8652 /* Return the global value of T as a symbol. */
8654 tree
8655 identifier_global_value (tree t)
8657 struct c_binding *b;
8659 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8660 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8661 return b->decl;
8663 return 0;
8666 /* In C, the only C-linkage public declaration is at file scope. */
8668 tree
8669 c_linkage_bindings (tree name)
8671 return identifier_global_value (name);
8674 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8675 otherwise the name is found in ridpointers from RID_INDEX. */
8677 void
8678 record_builtin_type (enum rid rid_index, const char *name, tree type)
8680 tree id, decl;
8681 if (name == 0)
8682 id = ridpointers[(int) rid_index];
8683 else
8684 id = get_identifier (name);
8685 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8686 pushdecl (decl);
8687 if (debug_hooks->type_decl)
8688 debug_hooks->type_decl (decl, false);
8691 /* Build the void_list_node (void_type_node having been created). */
8692 tree
8693 build_void_list_node (void)
8695 tree t = build_tree_list (NULL_TREE, void_type_node);
8696 return t;
8699 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8701 struct c_parm *
8702 build_c_parm (struct c_declspecs *specs, tree attrs,
8703 struct c_declarator *declarator)
8705 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8706 ret->specs = specs;
8707 ret->attrs = attrs;
8708 ret->declarator = declarator;
8709 return ret;
8712 /* Return a declarator with nested attributes. TARGET is the inner
8713 declarator to which these attributes apply. ATTRS are the
8714 attributes. */
8716 struct c_declarator *
8717 build_attrs_declarator (tree attrs, struct c_declarator *target)
8719 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8720 ret->kind = cdk_attrs;
8721 ret->declarator = target;
8722 ret->u.attrs = attrs;
8723 return ret;
8726 /* Return a declarator for a function with arguments specified by ARGS
8727 and return type specified by TARGET. */
8729 struct c_declarator *
8730 build_function_declarator (struct c_arg_info *args,
8731 struct c_declarator *target)
8733 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8734 ret->kind = cdk_function;
8735 ret->declarator = target;
8736 ret->u.arg_info = args;
8737 return ret;
8740 /* Return a declarator for the identifier IDENT (which may be
8741 NULL_TREE for an abstract declarator). */
8743 struct c_declarator *
8744 build_id_declarator (tree ident)
8746 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8747 ret->kind = cdk_id;
8748 ret->declarator = 0;
8749 ret->u.id = ident;
8750 /* Default value - may get reset to a more precise location. */
8751 ret->id_loc = input_location;
8752 return ret;
8755 /* Return something to represent absolute declarators containing a *.
8756 TARGET is the absolute declarator that the * contains.
8757 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8758 to apply to the pointer type. */
8760 struct c_declarator *
8761 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8762 struct c_declarator *target)
8764 tree attrs;
8765 int quals = 0;
8766 struct c_declarator *itarget = target;
8767 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8768 if (type_quals_attrs)
8770 attrs = type_quals_attrs->attrs;
8771 quals = quals_from_declspecs (type_quals_attrs);
8772 if (attrs != NULL_TREE)
8773 itarget = build_attrs_declarator (attrs, target);
8775 ret->kind = cdk_pointer;
8776 ret->declarator = itarget;
8777 ret->u.pointer_quals = quals;
8778 return ret;
8781 /* Return a pointer to a structure for an empty list of declaration
8782 specifiers. */
8784 struct c_declspecs *
8785 build_null_declspecs (void)
8787 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8788 ret->type = 0;
8789 ret->expr = 0;
8790 ret->decl_attr = 0;
8791 ret->attrs = 0;
8792 ret->align_log = -1;
8793 ret->typespec_word = cts_none;
8794 ret->storage_class = csc_none;
8795 ret->expr_const_operands = true;
8796 ret->declspecs_seen_p = false;
8797 ret->typespec_kind = ctsk_none;
8798 ret->non_sc_seen_p = false;
8799 ret->typedef_p = false;
8800 ret->explicit_signed_p = false;
8801 ret->deprecated_p = false;
8802 ret->default_int_p = false;
8803 ret->long_p = false;
8804 ret->long_long_p = false;
8805 ret->short_p = false;
8806 ret->signed_p = false;
8807 ret->unsigned_p = false;
8808 ret->complex_p = false;
8809 ret->inline_p = false;
8810 ret->noreturn_p = false;
8811 ret->thread_p = false;
8812 ret->const_p = false;
8813 ret->volatile_p = false;
8814 ret->restrict_p = false;
8815 ret->saturating_p = false;
8816 ret->alignas_p = false;
8817 ret->address_space = ADDR_SPACE_GENERIC;
8818 return ret;
8821 /* Add the address space ADDRSPACE to the declaration specifiers
8822 SPECS, returning SPECS. */
8824 struct c_declspecs *
8825 declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8827 specs->non_sc_seen_p = true;
8828 specs->declspecs_seen_p = true;
8830 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8831 && specs->address_space != as)
8832 error ("incompatible address space qualifiers %qs and %qs",
8833 c_addr_space_name (as),
8834 c_addr_space_name (specs->address_space));
8835 else
8836 specs->address_space = as;
8837 return specs;
8840 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8841 returning SPECS. */
8843 struct c_declspecs *
8844 declspecs_add_qual (struct c_declspecs *specs, tree qual)
8846 enum rid i;
8847 bool dupe = false;
8848 specs->non_sc_seen_p = true;
8849 specs->declspecs_seen_p = true;
8850 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8851 && C_IS_RESERVED_WORD (qual));
8852 i = C_RID_CODE (qual);
8853 switch (i)
8855 case RID_CONST:
8856 dupe = specs->const_p;
8857 specs->const_p = true;
8858 break;
8859 case RID_VOLATILE:
8860 dupe = specs->volatile_p;
8861 specs->volatile_p = true;
8862 break;
8863 case RID_RESTRICT:
8864 dupe = specs->restrict_p;
8865 specs->restrict_p = true;
8866 break;
8867 default:
8868 gcc_unreachable ();
8870 if (dupe && !flag_isoc99)
8871 pedwarn (input_location, OPT_Wpedantic, "duplicate %qE", qual);
8872 return specs;
8875 /* Add the type specifier TYPE to the declaration specifiers SPECS,
8876 returning SPECS. */
8878 struct c_declspecs *
8879 declspecs_add_type (location_t loc, struct c_declspecs *specs,
8880 struct c_typespec spec)
8882 tree type = spec.spec;
8883 specs->non_sc_seen_p = true;
8884 specs->declspecs_seen_p = true;
8885 specs->typespec_kind = spec.kind;
8886 if (TREE_DEPRECATED (type))
8887 specs->deprecated_p = true;
8889 /* Handle type specifier keywords. */
8890 if (TREE_CODE (type) == IDENTIFIER_NODE
8891 && C_IS_RESERVED_WORD (type)
8892 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8894 enum rid i = C_RID_CODE (type);
8895 if (specs->type)
8897 error_at (loc, "two or more data types in declaration specifiers");
8898 return specs;
8900 if ((int) i <= (int) RID_LAST_MODIFIER)
8902 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8903 bool dupe = false;
8904 switch (i)
8906 case RID_LONG:
8907 if (specs->long_long_p)
8909 error_at (loc, "%<long long long%> is too long for GCC");
8910 break;
8912 if (specs->long_p)
8914 if (specs->typespec_word == cts_double)
8916 error_at (loc,
8917 ("both %<long long%> and %<double%> in "
8918 "declaration specifiers"));
8919 break;
8921 pedwarn_c90 (loc, OPT_Wlong_long,
8922 "ISO C90 does not support %<long long%>");
8923 specs->long_long_p = 1;
8924 break;
8926 if (specs->short_p)
8927 error_at (loc,
8928 ("both %<long%> and %<short%> in "
8929 "declaration specifiers"));
8930 else if (specs->typespec_word == cts_void)
8931 error_at (loc,
8932 ("both %<long%> and %<void%> in "
8933 "declaration specifiers"));
8934 else if (specs->typespec_word == cts_int128)
8935 error_at (loc,
8936 ("both %<long%> and %<__int128%> in "
8937 "declaration specifiers"));
8938 else if (specs->typespec_word == cts_bool)
8939 error_at (loc,
8940 ("both %<long%> and %<_Bool%> in "
8941 "declaration specifiers"));
8942 else if (specs->typespec_word == cts_char)
8943 error_at (loc,
8944 ("both %<long%> and %<char%> in "
8945 "declaration specifiers"));
8946 else if (specs->typespec_word == cts_float)
8947 error_at (loc,
8948 ("both %<long%> and %<float%> in "
8949 "declaration specifiers"));
8950 else if (specs->typespec_word == cts_dfloat32)
8951 error_at (loc,
8952 ("both %<long%> and %<_Decimal32%> in "
8953 "declaration specifiers"));
8954 else if (specs->typespec_word == cts_dfloat64)
8955 error_at (loc,
8956 ("both %<long%> and %<_Decimal64%> in "
8957 "declaration specifiers"));
8958 else if (specs->typespec_word == cts_dfloat128)
8959 error_at (loc,
8960 ("both %<long%> and %<_Decimal128%> in "
8961 "declaration specifiers"));
8962 else
8963 specs->long_p = true;
8964 break;
8965 case RID_SHORT:
8966 dupe = specs->short_p;
8967 if (specs->long_p)
8968 error_at (loc,
8969 ("both %<long%> and %<short%> in "
8970 "declaration specifiers"));
8971 else if (specs->typespec_word == cts_void)
8972 error_at (loc,
8973 ("both %<short%> and %<void%> in "
8974 "declaration specifiers"));
8975 else if (specs->typespec_word == cts_int128)
8976 error_at (loc,
8977 ("both %<short%> and %<__int128%> in "
8978 "declaration specifiers"));
8979 else if (specs->typespec_word == cts_bool)
8980 error_at (loc,
8981 ("both %<short%> and %<_Bool%> in "
8982 "declaration specifiers"));
8983 else if (specs->typespec_word == cts_char)
8984 error_at (loc,
8985 ("both %<short%> and %<char%> in "
8986 "declaration specifiers"));
8987 else if (specs->typespec_word == cts_float)
8988 error_at (loc,
8989 ("both %<short%> and %<float%> in "
8990 "declaration specifiers"));
8991 else if (specs->typespec_word == cts_double)
8992 error_at (loc,
8993 ("both %<short%> and %<double%> in "
8994 "declaration specifiers"));
8995 else if (specs->typespec_word == cts_dfloat32)
8996 error_at (loc,
8997 ("both %<short%> and %<_Decimal32%> in "
8998 "declaration specifiers"));
8999 else if (specs->typespec_word == cts_dfloat64)
9000 error_at (loc,
9001 ("both %<short%> and %<_Decimal64%> in "
9002 "declaration specifiers"));
9003 else if (specs->typespec_word == cts_dfloat128)
9004 error_at (loc,
9005 ("both %<short%> and %<_Decimal128%> in "
9006 "declaration specifiers"));
9007 else
9008 specs->short_p = true;
9009 break;
9010 case RID_SIGNED:
9011 dupe = specs->signed_p;
9012 if (specs->unsigned_p)
9013 error_at (loc,
9014 ("both %<signed%> and %<unsigned%> in "
9015 "declaration specifiers"));
9016 else if (specs->typespec_word == cts_void)
9017 error_at (loc,
9018 ("both %<signed%> and %<void%> in "
9019 "declaration specifiers"));
9020 else if (specs->typespec_word == cts_bool)
9021 error_at (loc,
9022 ("both %<signed%> and %<_Bool%> in "
9023 "declaration specifiers"));
9024 else if (specs->typespec_word == cts_float)
9025 error_at (loc,
9026 ("both %<signed%> and %<float%> in "
9027 "declaration specifiers"));
9028 else if (specs->typespec_word == cts_double)
9029 error_at (loc,
9030 ("both %<signed%> and %<double%> in "
9031 "declaration specifiers"));
9032 else if (specs->typespec_word == cts_dfloat32)
9033 error_at (loc,
9034 ("both %<signed%> and %<_Decimal32%> in "
9035 "declaration specifiers"));
9036 else if (specs->typespec_word == cts_dfloat64)
9037 error_at (loc,
9038 ("both %<signed%> and %<_Decimal64%> in "
9039 "declaration specifiers"));
9040 else if (specs->typespec_word == cts_dfloat128)
9041 error_at (loc,
9042 ("both %<signed%> and %<_Decimal128%> in "
9043 "declaration specifiers"));
9044 else
9045 specs->signed_p = true;
9046 break;
9047 case RID_UNSIGNED:
9048 dupe = specs->unsigned_p;
9049 if (specs->signed_p)
9050 error_at (loc,
9051 ("both %<signed%> and %<unsigned%> in "
9052 "declaration specifiers"));
9053 else if (specs->typespec_word == cts_void)
9054 error_at (loc,
9055 ("both %<unsigned%> and %<void%> in "
9056 "declaration specifiers"));
9057 else if (specs->typespec_word == cts_bool)
9058 error_at (loc,
9059 ("both %<unsigned%> and %<_Bool%> in "
9060 "declaration specifiers"));
9061 else if (specs->typespec_word == cts_float)
9062 error_at (loc,
9063 ("both %<unsigned%> and %<float%> in "
9064 "declaration specifiers"));
9065 else if (specs->typespec_word == cts_double)
9066 error_at (loc,
9067 ("both %<unsigned%> and %<double%> in "
9068 "declaration specifiers"));
9069 else if (specs->typespec_word == cts_dfloat32)
9070 error_at (loc,
9071 ("both %<unsigned%> and %<_Decimal32%> in "
9072 "declaration specifiers"));
9073 else if (specs->typespec_word == cts_dfloat64)
9074 error_at (loc,
9075 ("both %<unsigned%> and %<_Decimal64%> in "
9076 "declaration specifiers"));
9077 else if (specs->typespec_word == cts_dfloat128)
9078 error_at (loc,
9079 ("both %<unsigned%> and %<_Decimal128%> in "
9080 "declaration specifiers"));
9081 else
9082 specs->unsigned_p = true;
9083 break;
9084 case RID_COMPLEX:
9085 dupe = specs->complex_p;
9086 if (!flag_isoc99 && !in_system_header)
9087 pedwarn (loc, OPT_Wpedantic,
9088 "ISO C90 does not support complex types");
9089 if (specs->typespec_word == cts_void)
9090 error_at (loc,
9091 ("both %<complex%> and %<void%> in "
9092 "declaration specifiers"));
9093 else if (specs->typespec_word == cts_bool)
9094 error_at (loc,
9095 ("both %<complex%> and %<_Bool%> in "
9096 "declaration specifiers"));
9097 else if (specs->typespec_word == cts_dfloat32)
9098 error_at (loc,
9099 ("both %<complex%> and %<_Decimal32%> in "
9100 "declaration specifiers"));
9101 else if (specs->typespec_word == cts_dfloat64)
9102 error_at (loc,
9103 ("both %<complex%> and %<_Decimal64%> in "
9104 "declaration specifiers"));
9105 else if (specs->typespec_word == cts_dfloat128)
9106 error_at (loc,
9107 ("both %<complex%> and %<_Decimal128%> in "
9108 "declaration specifiers"));
9109 else if (specs->typespec_word == cts_fract)
9110 error_at (loc,
9111 ("both %<complex%> and %<_Fract%> in "
9112 "declaration specifiers"));
9113 else if (specs->typespec_word == cts_accum)
9114 error_at (loc,
9115 ("both %<complex%> and %<_Accum%> in "
9116 "declaration specifiers"));
9117 else if (specs->saturating_p)
9118 error_at (loc,
9119 ("both %<complex%> and %<_Sat%> in "
9120 "declaration specifiers"));
9121 else
9122 specs->complex_p = true;
9123 break;
9124 case RID_SAT:
9125 dupe = specs->saturating_p;
9126 pedwarn (loc, OPT_Wpedantic,
9127 "ISO C does not support saturating types");
9128 if (specs->typespec_word == cts_int128)
9130 error_at (loc,
9131 ("both %<_Sat%> and %<__int128%> in "
9132 "declaration specifiers"));
9134 else if (specs->typespec_word == cts_void)
9135 error_at (loc,
9136 ("both %<_Sat%> and %<void%> in "
9137 "declaration specifiers"));
9138 else if (specs->typespec_word == cts_bool)
9139 error_at (loc,
9140 ("both %<_Sat%> and %<_Bool%> in "
9141 "declaration specifiers"));
9142 else if (specs->typespec_word == cts_char)
9143 error_at (loc,
9144 ("both %<_Sat%> and %<char%> in "
9145 "declaration specifiers"));
9146 else if (specs->typespec_word == cts_int)
9147 error_at (loc,
9148 ("both %<_Sat%> and %<int%> in "
9149 "declaration specifiers"));
9150 else if (specs->typespec_word == cts_float)
9151 error_at (loc,
9152 ("both %<_Sat%> and %<float%> in "
9153 "declaration specifiers"));
9154 else if (specs->typespec_word == cts_double)
9155 error_at (loc,
9156 ("both %<_Sat%> and %<double%> in "
9157 "declaration specifiers"));
9158 else if (specs->typespec_word == cts_dfloat32)
9159 error_at (loc,
9160 ("both %<_Sat%> and %<_Decimal32%> in "
9161 "declaration specifiers"));
9162 else if (specs->typespec_word == cts_dfloat64)
9163 error_at (loc,
9164 ("both %<_Sat%> and %<_Decimal64%> in "
9165 "declaration specifiers"));
9166 else if (specs->typespec_word == cts_dfloat128)
9167 error_at (loc,
9168 ("both %<_Sat%> and %<_Decimal128%> in "
9169 "declaration specifiers"));
9170 else if (specs->complex_p)
9171 error_at (loc,
9172 ("both %<_Sat%> and %<complex%> in "
9173 "declaration specifiers"));
9174 else
9175 specs->saturating_p = true;
9176 break;
9177 default:
9178 gcc_unreachable ();
9181 if (dupe)
9182 error_at (loc, "duplicate %qE", type);
9184 return specs;
9186 else
9188 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9189 "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9190 if (specs->typespec_word != cts_none)
9192 error_at (loc,
9193 "two or more data types in declaration specifiers");
9194 return specs;
9196 switch (i)
9198 case RID_INT128:
9199 if (int128_integer_type_node == NULL_TREE)
9201 error_at (loc, "%<__int128%> is not supported for this target");
9202 return specs;
9204 if (!in_system_header)
9205 pedwarn (loc, OPT_Wpedantic,
9206 "ISO C does not support %<__int128%> type");
9208 if (specs->long_p)
9209 error_at (loc,
9210 ("both %<__int128%> and %<long%> in "
9211 "declaration specifiers"));
9212 else if (specs->saturating_p)
9213 error_at (loc,
9214 ("both %<_Sat%> and %<__int128%> in "
9215 "declaration specifiers"));
9216 else if (specs->short_p)
9217 error_at (loc,
9218 ("both %<__int128%> and %<short%> in "
9219 "declaration specifiers"));
9220 else
9221 specs->typespec_word = cts_int128;
9222 return specs;
9223 case RID_VOID:
9224 if (specs->long_p)
9225 error_at (loc,
9226 ("both %<long%> and %<void%> in "
9227 "declaration specifiers"));
9228 else if (specs->short_p)
9229 error_at (loc,
9230 ("both %<short%> and %<void%> in "
9231 "declaration specifiers"));
9232 else if (specs->signed_p)
9233 error_at (loc,
9234 ("both %<signed%> and %<void%> in "
9235 "declaration specifiers"));
9236 else if (specs->unsigned_p)
9237 error_at (loc,
9238 ("both %<unsigned%> and %<void%> in "
9239 "declaration specifiers"));
9240 else if (specs->complex_p)
9241 error_at (loc,
9242 ("both %<complex%> and %<void%> in "
9243 "declaration specifiers"));
9244 else if (specs->saturating_p)
9245 error_at (loc,
9246 ("both %<_Sat%> and %<void%> in "
9247 "declaration specifiers"));
9248 else
9249 specs->typespec_word = cts_void;
9250 return specs;
9251 case RID_BOOL:
9252 if (specs->long_p)
9253 error_at (loc,
9254 ("both %<long%> and %<_Bool%> in "
9255 "declaration specifiers"));
9256 else if (specs->short_p)
9257 error_at (loc,
9258 ("both %<short%> and %<_Bool%> in "
9259 "declaration specifiers"));
9260 else if (specs->signed_p)
9261 error_at (loc,
9262 ("both %<signed%> and %<_Bool%> in "
9263 "declaration specifiers"));
9264 else if (specs->unsigned_p)
9265 error_at (loc,
9266 ("both %<unsigned%> and %<_Bool%> in "
9267 "declaration specifiers"));
9268 else if (specs->complex_p)
9269 error_at (loc,
9270 ("both %<complex%> and %<_Bool%> in "
9271 "declaration specifiers"));
9272 else if (specs->saturating_p)
9273 error_at (loc,
9274 ("both %<_Sat%> and %<_Bool%> in "
9275 "declaration specifiers"));
9276 else
9277 specs->typespec_word = cts_bool;
9278 return specs;
9279 case RID_CHAR:
9280 if (specs->long_p)
9281 error_at (loc,
9282 ("both %<long%> and %<char%> in "
9283 "declaration specifiers"));
9284 else if (specs->short_p)
9285 error_at (loc,
9286 ("both %<short%> and %<char%> in "
9287 "declaration specifiers"));
9288 else if (specs->saturating_p)
9289 error_at (loc,
9290 ("both %<_Sat%> and %<char%> in "
9291 "declaration specifiers"));
9292 else
9293 specs->typespec_word = cts_char;
9294 return specs;
9295 case RID_INT:
9296 if (specs->saturating_p)
9297 error_at (loc,
9298 ("both %<_Sat%> and %<int%> in "
9299 "declaration specifiers"));
9300 else
9301 specs->typespec_word = cts_int;
9302 return specs;
9303 case RID_FLOAT:
9304 if (specs->long_p)
9305 error_at (loc,
9306 ("both %<long%> and %<float%> in "
9307 "declaration specifiers"));
9308 else if (specs->short_p)
9309 error_at (loc,
9310 ("both %<short%> and %<float%> in "
9311 "declaration specifiers"));
9312 else if (specs->signed_p)
9313 error_at (loc,
9314 ("both %<signed%> and %<float%> in "
9315 "declaration specifiers"));
9316 else if (specs->unsigned_p)
9317 error_at (loc,
9318 ("both %<unsigned%> and %<float%> in "
9319 "declaration specifiers"));
9320 else if (specs->saturating_p)
9321 error_at (loc,
9322 ("both %<_Sat%> and %<float%> in "
9323 "declaration specifiers"));
9324 else
9325 specs->typespec_word = cts_float;
9326 return specs;
9327 case RID_DOUBLE:
9328 if (specs->long_long_p)
9329 error_at (loc,
9330 ("both %<long long%> and %<double%> in "
9331 "declaration specifiers"));
9332 else if (specs->short_p)
9333 error_at (loc,
9334 ("both %<short%> and %<double%> in "
9335 "declaration specifiers"));
9336 else if (specs->signed_p)
9337 error_at (loc,
9338 ("both %<signed%> and %<double%> in "
9339 "declaration specifiers"));
9340 else if (specs->unsigned_p)
9341 error_at (loc,
9342 ("both %<unsigned%> and %<double%> in "
9343 "declaration specifiers"));
9344 else if (specs->saturating_p)
9345 error_at (loc,
9346 ("both %<_Sat%> and %<double%> in "
9347 "declaration specifiers"));
9348 else
9349 specs->typespec_word = cts_double;
9350 return specs;
9351 case RID_DFLOAT32:
9352 case RID_DFLOAT64:
9353 case RID_DFLOAT128:
9355 const char *str;
9356 if (i == RID_DFLOAT32)
9357 str = "_Decimal32";
9358 else if (i == RID_DFLOAT64)
9359 str = "_Decimal64";
9360 else
9361 str = "_Decimal128";
9362 if (specs->long_long_p)
9363 error_at (loc,
9364 ("both %<long long%> and %<%s%> in "
9365 "declaration specifiers"),
9366 str);
9367 if (specs->long_p)
9368 error_at (loc,
9369 ("both %<long%> and %<%s%> in "
9370 "declaration specifiers"),
9371 str);
9372 else if (specs->short_p)
9373 error_at (loc,
9374 ("both %<short%> and %<%s%> in "
9375 "declaration specifiers"),
9376 str);
9377 else if (specs->signed_p)
9378 error_at (loc,
9379 ("both %<signed%> and %<%s%> in "
9380 "declaration specifiers"),
9381 str);
9382 else if (specs->unsigned_p)
9383 error_at (loc,
9384 ("both %<unsigned%> and %<%s%> in "
9385 "declaration specifiers"),
9386 str);
9387 else if (specs->complex_p)
9388 error_at (loc,
9389 ("both %<complex%> and %<%s%> in "
9390 "declaration specifiers"),
9391 str);
9392 else if (specs->saturating_p)
9393 error_at (loc,
9394 ("both %<_Sat%> and %<%s%> in "
9395 "declaration specifiers"),
9396 str);
9397 else if (i == RID_DFLOAT32)
9398 specs->typespec_word = cts_dfloat32;
9399 else if (i == RID_DFLOAT64)
9400 specs->typespec_word = cts_dfloat64;
9401 else
9402 specs->typespec_word = cts_dfloat128;
9404 if (!targetm.decimal_float_supported_p ())
9405 error_at (loc,
9406 ("decimal floating point not supported "
9407 "for this target"));
9408 pedwarn (loc, OPT_Wpedantic,
9409 "ISO C does not support decimal floating point");
9410 return specs;
9411 case RID_FRACT:
9412 case RID_ACCUM:
9414 const char *str;
9415 if (i == RID_FRACT)
9416 str = "_Fract";
9417 else
9418 str = "_Accum";
9419 if (specs->complex_p)
9420 error_at (loc,
9421 ("both %<complex%> and %<%s%> in "
9422 "declaration specifiers"),
9423 str);
9424 else if (i == RID_FRACT)
9425 specs->typespec_word = cts_fract;
9426 else
9427 specs->typespec_word = cts_accum;
9429 if (!targetm.fixed_point_supported_p ())
9430 error_at (loc,
9431 "fixed-point types not supported for this target");
9432 pedwarn (loc, OPT_Wpedantic,
9433 "ISO C does not support fixed-point types");
9434 return specs;
9435 default:
9436 /* ObjC reserved word "id", handled below. */
9437 break;
9442 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9443 form of ObjC type, cases such as "int" and "long" being handled
9444 above), a TYPE (struct, union, enum and typeof specifiers) or an
9445 ERROR_MARK. In none of these cases may there have previously
9446 been any type specifiers. */
9447 if (specs->type || specs->typespec_word != cts_none
9448 || specs->long_p || specs->short_p || specs->signed_p
9449 || specs->unsigned_p || specs->complex_p)
9450 error_at (loc, "two or more data types in declaration specifiers");
9451 else if (TREE_CODE (type) == TYPE_DECL)
9453 if (TREE_TYPE (type) == error_mark_node)
9454 ; /* Allow the type to default to int to avoid cascading errors. */
9455 else
9457 specs->type = TREE_TYPE (type);
9458 specs->decl_attr = DECL_ATTRIBUTES (type);
9459 specs->typedef_p = true;
9460 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9462 /* If this typedef name is defined in a struct, then a C++
9463 lookup would return a different value. */
9464 if (warn_cxx_compat
9465 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9466 warning_at (loc, OPT_Wc___compat,
9467 "C++ lookup of %qD would return a field, not a type",
9468 type);
9470 /* If we are parsing a struct, record that a struct field
9471 used a typedef. */
9472 if (warn_cxx_compat && struct_parse_info != NULL)
9473 VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9476 else if (TREE_CODE (type) == IDENTIFIER_NODE)
9478 tree t = lookup_name (type);
9479 if (!t || TREE_CODE (t) != TYPE_DECL)
9480 error_at (loc, "%qE fails to be a typedef or built in type", type);
9481 else if (TREE_TYPE (t) == error_mark_node)
9483 else
9484 specs->type = TREE_TYPE (t);
9486 else
9488 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9490 specs->typedef_p = true;
9491 if (spec.expr)
9493 if (specs->expr)
9494 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9495 specs->expr, spec.expr);
9496 else
9497 specs->expr = spec.expr;
9498 specs->expr_const_operands &= spec.expr_const_operands;
9501 specs->type = type;
9504 return specs;
9507 /* Add the storage class specifier or function specifier SCSPEC to the
9508 declaration specifiers SPECS, returning SPECS. */
9510 struct c_declspecs *
9511 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9513 enum rid i;
9514 enum c_storage_class n = csc_none;
9515 bool dupe = false;
9516 specs->declspecs_seen_p = true;
9517 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9518 && C_IS_RESERVED_WORD (scspec));
9519 i = C_RID_CODE (scspec);
9520 if (specs->non_sc_seen_p)
9521 warning (OPT_Wold_style_declaration,
9522 "%qE is not at beginning of declaration", scspec);
9523 switch (i)
9525 case RID_INLINE:
9526 /* C99 permits duplicate inline. Although of doubtful utility,
9527 it seems simplest to permit it in gnu89 mode as well, as
9528 there is also little utility in maintaining this as a
9529 difference between gnu89 and C99 inline. */
9530 dupe = false;
9531 specs->inline_p = true;
9532 break;
9533 case RID_NORETURN:
9534 /* Duplicate _Noreturn is permitted. */
9535 dupe = false;
9536 specs->noreturn_p = true;
9537 break;
9538 case RID_THREAD:
9539 dupe = specs->thread_p;
9540 if (specs->storage_class == csc_auto)
9541 error ("%<__thread%> used with %<auto%>");
9542 else if (specs->storage_class == csc_register)
9543 error ("%<__thread%> used with %<register%>");
9544 else if (specs->storage_class == csc_typedef)
9545 error ("%<__thread%> used with %<typedef%>");
9546 else
9547 specs->thread_p = true;
9548 break;
9549 case RID_AUTO:
9550 n = csc_auto;
9551 break;
9552 case RID_EXTERN:
9553 n = csc_extern;
9554 /* Diagnose "__thread extern". */
9555 if (specs->thread_p)
9556 error ("%<__thread%> before %<extern%>");
9557 break;
9558 case RID_REGISTER:
9559 n = csc_register;
9560 break;
9561 case RID_STATIC:
9562 n = csc_static;
9563 /* Diagnose "__thread static". */
9564 if (specs->thread_p)
9565 error ("%<__thread%> before %<static%>");
9566 break;
9567 case RID_TYPEDEF:
9568 n = csc_typedef;
9569 break;
9570 default:
9571 gcc_unreachable ();
9573 if (n != csc_none && n == specs->storage_class)
9574 dupe = true;
9575 if (dupe)
9576 error ("duplicate %qE", scspec);
9577 if (n != csc_none)
9579 if (specs->storage_class != csc_none && n != specs->storage_class)
9581 error ("multiple storage classes in declaration specifiers");
9583 else
9585 specs->storage_class = n;
9586 if (n != csc_extern && n != csc_static && specs->thread_p)
9588 error ("%<__thread%> used with %qE", scspec);
9589 specs->thread_p = false;
9593 return specs;
9596 /* Add the attributes ATTRS to the declaration specifiers SPECS,
9597 returning SPECS. */
9599 struct c_declspecs *
9600 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9602 specs->attrs = chainon (attrs, specs->attrs);
9603 specs->declspecs_seen_p = true;
9604 return specs;
9607 /* Add an _Alignas specifier (expression ALIGN, or type whose
9608 alignment is ALIGN) to the declaration specifiers SPECS, returning
9609 SPECS. */
9610 struct c_declspecs *
9611 declspecs_add_alignas (struct c_declspecs *specs, tree align)
9613 int align_log;
9614 specs->alignas_p = true;
9615 if (align == error_mark_node)
9616 return specs;
9617 align_log = check_user_alignment (align, true);
9618 if (align_log > specs->align_log)
9619 specs->align_log = align_log;
9620 return specs;
9623 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9624 specifiers with any other type specifier to determine the resulting
9625 type. This is where ISO C checks on complex types are made, since
9626 "_Complex long" is a prefix of the valid ISO C type "_Complex long
9627 double". */
9629 struct c_declspecs *
9630 finish_declspecs (struct c_declspecs *specs)
9632 /* If a type was specified as a whole, we have no modifiers and are
9633 done. */
9634 if (specs->type != NULL_TREE)
9636 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9637 && !specs->signed_p && !specs->unsigned_p
9638 && !specs->complex_p);
9640 /* Set a dummy type. */
9641 if (TREE_CODE (specs->type) == ERROR_MARK)
9642 specs->type = integer_type_node;
9643 return specs;
9646 /* If none of "void", "_Bool", "char", "int", "float" or "double"
9647 has been specified, treat it as "int" unless "_Complex" is
9648 present and there are no other specifiers. If we just have
9649 "_Complex", it is equivalent to "_Complex double", but e.g.
9650 "_Complex short" is equivalent to "_Complex short int". */
9651 if (specs->typespec_word == cts_none)
9653 if (specs->saturating_p)
9655 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9656 if (!targetm.fixed_point_supported_p ())
9657 error ("fixed-point types not supported for this target");
9658 specs->typespec_word = cts_fract;
9660 else if (specs->long_p || specs->short_p
9661 || specs->signed_p || specs->unsigned_p)
9663 specs->typespec_word = cts_int;
9665 else if (specs->complex_p)
9667 specs->typespec_word = cts_double;
9668 pedwarn (input_location, OPT_Wpedantic,
9669 "ISO C does not support plain %<complex%> meaning "
9670 "%<double complex%>");
9672 else
9674 specs->typespec_word = cts_int;
9675 specs->default_int_p = true;
9676 /* We don't diagnose this here because grokdeclarator will
9677 give more specific diagnostics according to whether it is
9678 a function definition. */
9682 /* If "signed" was specified, record this to distinguish "int" and
9683 "signed int" in the case of a bit-field with
9684 -funsigned-bitfields. */
9685 specs->explicit_signed_p = specs->signed_p;
9687 /* Now compute the actual type. */
9688 switch (specs->typespec_word)
9690 case cts_void:
9691 gcc_assert (!specs->long_p && !specs->short_p
9692 && !specs->signed_p && !specs->unsigned_p
9693 && !specs->complex_p);
9694 specs->type = void_type_node;
9695 break;
9696 case cts_bool:
9697 gcc_assert (!specs->long_p && !specs->short_p
9698 && !specs->signed_p && !specs->unsigned_p
9699 && !specs->complex_p);
9700 specs->type = boolean_type_node;
9701 break;
9702 case cts_char:
9703 gcc_assert (!specs->long_p && !specs->short_p);
9704 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9705 if (specs->signed_p)
9706 specs->type = signed_char_type_node;
9707 else if (specs->unsigned_p)
9708 specs->type = unsigned_char_type_node;
9709 else
9710 specs->type = char_type_node;
9711 if (specs->complex_p)
9713 pedwarn (input_location, OPT_Wpedantic,
9714 "ISO C does not support complex integer types");
9715 specs->type = build_complex_type (specs->type);
9717 break;
9718 case cts_int128:
9719 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
9720 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9721 specs->type = (specs->unsigned_p
9722 ? int128_unsigned_type_node
9723 : int128_integer_type_node);
9724 if (specs->complex_p)
9726 pedwarn (input_location, OPT_Wpedantic,
9727 "ISO C does not support complex integer types");
9728 specs->type = build_complex_type (specs->type);
9730 break;
9731 case cts_int:
9732 gcc_assert (!(specs->long_p && specs->short_p));
9733 gcc_assert (!(specs->signed_p && specs->unsigned_p));
9734 if (specs->long_long_p)
9735 specs->type = (specs->unsigned_p
9736 ? long_long_unsigned_type_node
9737 : long_long_integer_type_node);
9738 else if (specs->long_p)
9739 specs->type = (specs->unsigned_p
9740 ? long_unsigned_type_node
9741 : long_integer_type_node);
9742 else if (specs->short_p)
9743 specs->type = (specs->unsigned_p
9744 ? short_unsigned_type_node
9745 : short_integer_type_node);
9746 else
9747 specs->type = (specs->unsigned_p
9748 ? unsigned_type_node
9749 : integer_type_node);
9750 if (specs->complex_p)
9752 pedwarn (input_location, OPT_Wpedantic,
9753 "ISO C does not support complex integer types");
9754 specs->type = build_complex_type (specs->type);
9756 break;
9757 case cts_float:
9758 gcc_assert (!specs->long_p && !specs->short_p
9759 && !specs->signed_p && !specs->unsigned_p);
9760 specs->type = (specs->complex_p
9761 ? complex_float_type_node
9762 : float_type_node);
9763 break;
9764 case cts_double:
9765 gcc_assert (!specs->long_long_p && !specs->short_p
9766 && !specs->signed_p && !specs->unsigned_p);
9767 if (specs->long_p)
9769 specs->type = (specs->complex_p
9770 ? complex_long_double_type_node
9771 : long_double_type_node);
9773 else
9775 specs->type = (specs->complex_p
9776 ? complex_double_type_node
9777 : double_type_node);
9779 break;
9780 case cts_dfloat32:
9781 case cts_dfloat64:
9782 case cts_dfloat128:
9783 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9784 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9785 if (specs->typespec_word == cts_dfloat32)
9786 specs->type = dfloat32_type_node;
9787 else if (specs->typespec_word == cts_dfloat64)
9788 specs->type = dfloat64_type_node;
9789 else
9790 specs->type = dfloat128_type_node;
9791 break;
9792 case cts_fract:
9793 gcc_assert (!specs->complex_p);
9794 if (!targetm.fixed_point_supported_p ())
9795 specs->type = integer_type_node;
9796 else if (specs->saturating_p)
9798 if (specs->long_long_p)
9799 specs->type = specs->unsigned_p
9800 ? sat_unsigned_long_long_fract_type_node
9801 : sat_long_long_fract_type_node;
9802 else if (specs->long_p)
9803 specs->type = specs->unsigned_p
9804 ? sat_unsigned_long_fract_type_node
9805 : sat_long_fract_type_node;
9806 else if (specs->short_p)
9807 specs->type = specs->unsigned_p
9808 ? sat_unsigned_short_fract_type_node
9809 : sat_short_fract_type_node;
9810 else
9811 specs->type = specs->unsigned_p
9812 ? sat_unsigned_fract_type_node
9813 : sat_fract_type_node;
9815 else
9817 if (specs->long_long_p)
9818 specs->type = specs->unsigned_p
9819 ? unsigned_long_long_fract_type_node
9820 : long_long_fract_type_node;
9821 else if (specs->long_p)
9822 specs->type = specs->unsigned_p
9823 ? unsigned_long_fract_type_node
9824 : long_fract_type_node;
9825 else if (specs->short_p)
9826 specs->type = specs->unsigned_p
9827 ? unsigned_short_fract_type_node
9828 : short_fract_type_node;
9829 else
9830 specs->type = specs->unsigned_p
9831 ? unsigned_fract_type_node
9832 : fract_type_node;
9834 break;
9835 case cts_accum:
9836 gcc_assert (!specs->complex_p);
9837 if (!targetm.fixed_point_supported_p ())
9838 specs->type = integer_type_node;
9839 else if (specs->saturating_p)
9841 if (specs->long_long_p)
9842 specs->type = specs->unsigned_p
9843 ? sat_unsigned_long_long_accum_type_node
9844 : sat_long_long_accum_type_node;
9845 else if (specs->long_p)
9846 specs->type = specs->unsigned_p
9847 ? sat_unsigned_long_accum_type_node
9848 : sat_long_accum_type_node;
9849 else if (specs->short_p)
9850 specs->type = specs->unsigned_p
9851 ? sat_unsigned_short_accum_type_node
9852 : sat_short_accum_type_node;
9853 else
9854 specs->type = specs->unsigned_p
9855 ? sat_unsigned_accum_type_node
9856 : sat_accum_type_node;
9858 else
9860 if (specs->long_long_p)
9861 specs->type = specs->unsigned_p
9862 ? unsigned_long_long_accum_type_node
9863 : long_long_accum_type_node;
9864 else if (specs->long_p)
9865 specs->type = specs->unsigned_p
9866 ? unsigned_long_accum_type_node
9867 : long_accum_type_node;
9868 else if (specs->short_p)
9869 specs->type = specs->unsigned_p
9870 ? unsigned_short_accum_type_node
9871 : short_accum_type_node;
9872 else
9873 specs->type = specs->unsigned_p
9874 ? unsigned_accum_type_node
9875 : accum_type_node;
9877 break;
9878 default:
9879 gcc_unreachable ();
9882 return specs;
9885 /* A subroutine of c_write_global_declarations. Perform final processing
9886 on one file scope's declarations (or the external scope's declarations),
9887 GLOBALS. */
9889 static void
9890 c_write_global_declarations_1 (tree globals)
9892 tree decl;
9893 bool reconsider;
9895 /* Process the decls in the order they were written. */
9896 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9898 /* Check for used but undefined static functions using the C
9899 standard's definition of "used", and set TREE_NO_WARNING so
9900 that check_global_declarations doesn't repeat the check. */
9901 if (TREE_CODE (decl) == FUNCTION_DECL
9902 && DECL_INITIAL (decl) == 0
9903 && DECL_EXTERNAL (decl)
9904 && !TREE_PUBLIC (decl)
9905 && C_DECL_USED (decl))
9907 pedwarn (input_location, 0, "%q+F used but never defined", decl);
9908 TREE_NO_WARNING (decl) = 1;
9911 wrapup_global_declaration_1 (decl);
9916 reconsider = false;
9917 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9918 reconsider |= wrapup_global_declaration_2 (decl);
9920 while (reconsider);
9922 for (decl = globals; decl; decl = DECL_CHAIN (decl))
9923 check_global_declaration_1 (decl);
9926 /* A subroutine of c_write_global_declarations Emit debug information for each
9927 of the declarations in GLOBALS. */
9929 static void
9930 c_write_global_declarations_2 (tree globals)
9932 tree decl;
9934 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
9935 debug_hooks->global_decl (decl);
9938 /* Callback to collect a source_ref from a DECL. */
9940 static void
9941 collect_source_ref_cb (tree decl)
9943 if (!DECL_IS_BUILTIN (decl))
9944 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
9947 /* Preserve the external declarations scope across a garbage collect. */
9948 static GTY(()) tree ext_block;
9950 /* Collect all references relevant to SOURCE_FILE. */
9952 static void
9953 collect_all_refs (const char *source_file)
9955 tree t;
9956 unsigned i;
9958 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9959 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
9961 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
9964 /* Iterate over all global declarations and call CALLBACK. */
9966 static void
9967 for_each_global_decl (void (*callback) (tree decl))
9969 tree t;
9970 tree decls;
9971 tree decl;
9972 unsigned i;
9974 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9976 decls = DECL_INITIAL (t);
9977 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
9978 callback (decl);
9981 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
9982 callback (decl);
9985 void
9986 c_write_global_declarations (void)
9988 tree t;
9989 unsigned i;
9991 /* We don't want to do this if generating a PCH. */
9992 if (pch_file)
9993 return;
9995 timevar_start (TV_PHASE_DEFERRED);
9997 /* Do the Objective-C stuff. This is where all the Objective-C
9998 module stuff gets generated (symtab, class/protocol/selector
9999 lists etc). */
10000 if (c_dialect_objc ())
10001 objc_write_global_declarations ();
10003 /* Close the external scope. */
10004 ext_block = pop_scope ();
10005 external_scope = 0;
10006 gcc_assert (!current_scope);
10008 /* Handle -fdump-ada-spec[-slim]. */
10009 if (dump_enabled_p (TDI_ada))
10011 /* Build a table of files to generate specs for */
10012 if (get_dump_file_info (TDI_ada)->flags & TDF_SLIM)
10013 collect_source_ref (main_input_filename);
10014 else
10015 for_each_global_decl (collect_source_ref_cb);
10017 dump_ada_specs (collect_all_refs, NULL);
10020 if (ext_block)
10022 tree tmp = BLOCK_VARS (ext_block);
10023 int flags;
10024 FILE * stream = dump_begin (TDI_tu, &flags);
10025 if (stream && tmp)
10027 dump_node (tmp, flags & ~TDF_SLIM, stream);
10028 dump_end (TDI_tu, stream);
10032 /* Process all file scopes in this compilation, and the external_scope,
10033 through wrapup_global_declarations and check_global_declarations. */
10034 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
10035 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10036 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10038 timevar_stop (TV_PHASE_DEFERRED);
10039 timevar_start (TV_PHASE_CGRAPH);
10041 /* We're done parsing; proceed to optimize and emit assembly.
10042 FIXME: shouldn't be the front end's responsibility to call this. */
10043 finalize_compilation_unit ();
10045 timevar_stop (TV_PHASE_CGRAPH);
10046 timevar_start (TV_PHASE_DBGINFO);
10048 /* After cgraph has had a chance to emit everything that's going to
10049 be emitted, output debug information for globals. */
10050 if (!seen_error ())
10052 timevar_push (TV_SYMOUT);
10053 FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
10054 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10055 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10056 timevar_pop (TV_SYMOUT);
10059 ext_block = NULL;
10060 timevar_stop (TV_PHASE_DBGINFO);
10063 /* Register reserved keyword WORD as qualifier for address space AS. */
10065 void
10066 c_register_addr_space (const char *word, addr_space_t as)
10068 int rid = RID_FIRST_ADDR_SPACE + as;
10069 tree id;
10071 /* Address space qualifiers are only supported
10072 in C with GNU extensions enabled. */
10073 if (c_dialect_objc () || flag_no_asm)
10074 return;
10076 id = get_identifier (word);
10077 C_SET_RID_CODE (id, rid);
10078 C_IS_RESERVED_WORD (id) = 1;
10079 ridpointers [rid] = id;
10082 #include "gt-c-decl.h"