Merge trunk version 221103 into gupc branch.
[official-gcc.git] / gcc / c / c-decl.c
blob9df71bc4d94c77f76da0f7b740599ec355c36cc3
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "input.h"
31 #include "tm.h"
32 #include "intl.h"
33 #include "hash-set.h"
34 #include "vec.h"
35 #include "symtab.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "double-int.h"
39 #include "machmode.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "fold-const.h"
43 #include "print-tree.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "attribs.h"
47 #include "stringpool.h"
48 #include "tree-inline.h"
49 #include "flags.h"
50 #include "hashtab.h"
51 #include "hash-set.h"
52 #include "vec.h"
53 #include "machmode.h"
54 #include "hard-reg-set.h"
55 #include "function.h"
56 #include "c-tree.h"
57 #include "tree-upc.h"
58 #include "toplev.h"
59 #include "tm_p.h"
60 #include "cpplib.h"
61 #include "target.h"
62 #include "debug.h"
63 #include "opts.h"
64 #include "timevar.h"
65 #include "c-family/c-common.h"
66 #include "c-family/c-objc.h"
67 #include "c-family/c-upc.h"
68 #include "c-family/c-pragma.h"
69 #include "c-family/c-ubsan.h"
70 #include "c-lang.h"
71 #include "langhooks.h"
72 #include "tree-iterator.h"
73 #include "diagnostic-core.h"
74 #include "dumpfile.h"
75 #include "hash-map.h"
76 #include "is-a.h"
77 #include "plugin-api.h"
78 #include "ipa-ref.h"
79 #include "cgraph.h"
80 #include "hash-table.h"
81 #include "langhooks-def.h"
82 #include "plugin.h"
83 #include "c-family/c-ada-spec.h"
84 #include "cilk.h"
85 #include "builtins.h"
87 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
88 enum decl_context
89 { NORMAL, /* Ordinary declaration */
90 FUNCDEF, /* Function definition */
91 PARM, /* Declaration of parm before function body */
92 FIELD, /* Declaration inside struct or union */
93 TYPENAME}; /* Typename (inside cast or sizeof) */
95 /* States indicating how grokdeclarator() should handle declspecs marked
96 with __attribute__((deprecated)). An object declared as
97 __attribute__((deprecated)) suppresses warnings of uses of other
98 deprecated items. */
100 enum deprecated_states {
101 DEPRECATED_NORMAL,
102 DEPRECATED_SUPPRESS
106 /* Nonzero if we have seen an invalid cross reference
107 to a struct, union, or enum, but not yet printed the message. */
108 tree pending_invalid_xref;
110 /* File and line to appear in the eventual error message. */
111 location_t pending_invalid_xref_location;
113 /* The file and line that the prototype came from if this is an
114 old-style definition; used for diagnostics in
115 store_parm_decls_oldstyle. */
117 static location_t current_function_prototype_locus;
119 /* Whether this prototype was built-in. */
121 static bool current_function_prototype_built_in;
123 /* The argument type information of this prototype. */
125 static tree current_function_prototype_arg_types;
127 /* The argument information structure for the function currently being
128 defined. */
130 static struct c_arg_info *current_function_arg_info;
132 /* The obstack on which parser and related data structures, which are
133 not live beyond their top-level declaration or definition, are
134 allocated. */
135 struct obstack parser_obstack;
137 /* The current statement tree. */
139 static GTY(()) struct stmt_tree_s c_stmt_tree;
141 /* State saving variables. */
142 tree c_break_label;
143 tree c_cont_label;
145 /* A list of decls to be made automatically visible in each file scope. */
146 static GTY(()) tree visible_builtins;
148 /* Set to 0 at beginning of a function definition, set to 1 if
149 a return statement that specifies a return value is seen. */
151 int current_function_returns_value;
153 /* Set to 0 at beginning of a function definition, set to 1 if
154 a return statement with no argument is seen. */
156 int current_function_returns_null;
158 /* Set to 0 at beginning of a function definition, set to 1 if
159 a call to a noreturn function is seen. */
161 int current_function_returns_abnormally;
163 /* Set to nonzero by `grokdeclarator' for a function
164 whose return type is defaulted, if warnings for this are desired. */
166 static int warn_about_return_type;
168 /* Nonzero when the current toplevel function contains a declaration
169 of a nested function which is never defined. */
171 static bool undef_nested_function;
173 /* Mode used to build pointers (VOIDmode means ptr_mode). */
175 machine_mode c_default_pointer_mode = VOIDmode;
177 /* If non-zero, implicit "omp declare target" attribute is added into the
178 attribute lists. */
179 int current_omp_declare_target_attribute;
181 /* Each c_binding structure describes one binding of an identifier to
182 a decl. All the decls in a scope - irrespective of namespace - are
183 chained together by the ->prev field, which (as the name implies)
184 runs in reverse order. All the decls in a given namespace bound to
185 a given identifier are chained by the ->shadowed field, which runs
186 from inner to outer scopes.
188 The ->decl field usually points to a DECL node, but there are two
189 exceptions. In the namespace of type tags, the bound entity is a
190 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
191 identifier is encountered, it is bound to error_mark_node to
192 suppress further errors about that identifier in the current
193 function.
195 The ->u.type field stores the type of the declaration in this scope;
196 if NULL, the type is the type of the ->decl field. This is only of
197 relevance for objects with external or internal linkage which may
198 be redeclared in inner scopes, forming composite types that only
199 persist for the duration of those scopes. In the external scope,
200 this stores the composite of all the types declared for this
201 object, visible or not. The ->inner_comp field (used only at file
202 scope) stores whether an incomplete array type at file scope was
203 completed at an inner scope to an array size other than 1.
205 The ->u.label field is used for labels. It points to a structure
206 which stores additional information used for warnings.
208 The depth field is copied from the scope structure that holds this
209 decl. It is used to preserve the proper ordering of the ->shadowed
210 field (see bind()) and also for a handful of special-case checks.
211 Finally, the invisible bit is true for a decl which should be
212 ignored for purposes of normal name lookup, and the nested bit is
213 true for a decl that's been bound a second time in an inner scope;
214 in all such cases, the binding in the outer scope will have its
215 invisible bit true. */
217 struct GTY((chain_next ("%h.prev"))) c_binding {
218 union GTY(()) { /* first so GTY desc can use decl */
219 tree GTY((tag ("0"))) type; /* the type in this scope */
220 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
221 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
222 tree decl; /* the decl bound */
223 tree id; /* the identifier it's bound to */
224 struct c_binding *prev; /* the previous decl in this scope */
225 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
226 unsigned int depth : 28; /* depth of this scope */
227 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
228 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
229 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
230 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
231 location_t locus; /* location for nested bindings */
233 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
234 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
235 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
236 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
238 /* Each C symbol points to three linked lists of c_binding structures.
239 These describe the values of the identifier in the three different
240 namespaces defined by the language. */
242 struct GTY(()) lang_identifier {
243 struct c_common_identifier common_id;
244 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
245 struct c_binding *tag_binding; /* struct/union/enum tags */
246 struct c_binding *label_binding; /* labels */
249 /* Validate c-lang.c's assumptions. */
250 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
251 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
253 /* The binding oracle; see c-tree.h. */
254 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
256 /* This flag is set on an identifier if we have previously asked the
257 binding oracle for this identifier's symbol binding. */
258 #define I_SYMBOL_CHECKED(node) \
259 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
261 static inline struct c_binding* *
262 i_symbol_binding (tree node)
264 struct lang_identifier *lid
265 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
267 if (lid->symbol_binding == NULL
268 && c_binding_oracle != NULL
269 && !I_SYMBOL_CHECKED (node))
271 /* Set the "checked" flag first, to avoid infinite recursion
272 when the binding oracle calls back into gcc. */
273 I_SYMBOL_CHECKED (node) = 1;
274 c_binding_oracle (C_ORACLE_SYMBOL, node);
277 return &lid->symbol_binding;
280 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
282 #define I_SYMBOL_DECL(node) \
283 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
285 /* This flag is set on an identifier if we have previously asked the
286 binding oracle for this identifier's tag binding. */
287 #define I_TAG_CHECKED(node) \
288 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
290 static inline struct c_binding **
291 i_tag_binding (tree node)
293 struct lang_identifier *lid
294 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
296 if (lid->tag_binding == NULL
297 && c_binding_oracle != NULL
298 && !I_TAG_CHECKED (node))
300 /* Set the "checked" flag first, to avoid infinite recursion
301 when the binding oracle calls back into gcc. */
302 I_TAG_CHECKED (node) = 1;
303 c_binding_oracle (C_ORACLE_TAG, node);
306 return &lid->tag_binding;
309 #define I_TAG_BINDING(node) (*i_tag_binding (node))
311 #define I_TAG_DECL(node) \
312 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
314 /* This flag is set on an identifier if we have previously asked the
315 binding oracle for this identifier's label binding. */
316 #define I_LABEL_CHECKED(node) \
317 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
319 static inline struct c_binding **
320 i_label_binding (tree node)
322 struct lang_identifier *lid
323 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
325 if (lid->label_binding == NULL
326 && c_binding_oracle != NULL
327 && !I_LABEL_CHECKED (node))
329 /* Set the "checked" flag first, to avoid infinite recursion
330 when the binding oracle calls back into gcc. */
331 I_LABEL_CHECKED (node) = 1;
332 c_binding_oracle (C_ORACLE_LABEL, node);
335 return &lid->label_binding;
338 #define I_LABEL_BINDING(node) (*i_label_binding (node))
340 #define I_LABEL_DECL(node) \
341 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
343 /* The resulting tree type. */
345 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
346 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
348 union tree_node GTY ((tag ("0"),
349 desc ("tree_node_structure (&%h)")))
350 generic;
351 struct lang_identifier GTY ((tag ("1"))) identifier;
354 /* Track bindings and other things that matter for goto warnings. For
355 efficiency, we do not gather all the decls at the point of
356 definition. Instead, we point into the bindings structure. As
357 scopes are popped, we update these structures and gather the decls
358 that matter at that time. */
360 struct GTY(()) c_spot_bindings {
361 /* The currently open scope which holds bindings defined when the
362 label was defined or the goto statement was found. */
363 struct c_scope *scope;
364 /* The bindings in the scope field which were defined at the point
365 of the label or goto. This lets us look at older or newer
366 bindings in the scope, as appropriate. */
367 struct c_binding *bindings_in_scope;
368 /* The number of statement expressions that have started since this
369 label or goto statement was defined. This is zero if we are at
370 the same statement expression level. It is positive if we are in
371 a statement expression started since this spot. It is negative
372 if this spot was in a statement expression and we have left
373 it. */
374 int stmt_exprs;
375 /* Whether we started in a statement expression but are no longer in
376 it. This is set to true if stmt_exprs ever goes negative. */
377 bool left_stmt_expr;
380 /* This structure is used to keep track of bindings seen when a goto
381 statement is defined. This is only used if we see the goto
382 statement before we see the label. */
384 struct GTY(()) c_goto_bindings {
385 /* The location of the goto statement. */
386 location_t loc;
387 /* The bindings of the goto statement. */
388 struct c_spot_bindings goto_bindings;
391 typedef struct c_goto_bindings *c_goto_bindings_p;
393 /* The additional information we keep track of for a label binding.
394 These fields are updated as scopes are popped. */
396 struct GTY(()) c_label_vars {
397 /* The shadowed c_label_vars, when one label shadows another (which
398 can only happen using a __label__ declaration). */
399 struct c_label_vars *shadowed;
400 /* The bindings when the label was defined. */
401 struct c_spot_bindings label_bindings;
402 /* A list of decls that we care about: decls about which we should
403 warn if a goto branches to this label from later in the function.
404 Decls are added to this list as scopes are popped. We only add
405 the decls that matter. */
406 vec<tree, va_gc> *decls_in_scope;
407 /* A list of goto statements to this label. This is only used for
408 goto statements seen before the label was defined, so that we can
409 issue appropriate warnings for them. */
410 vec<c_goto_bindings_p, va_gc> *gotos;
413 /* Each c_scope structure describes the complete contents of one
414 scope. Four scopes are distinguished specially: the innermost or
415 current scope, the innermost function scope, the file scope (always
416 the second to outermost) and the outermost or external scope.
418 Most declarations are recorded in the current scope.
420 All normal label declarations are recorded in the innermost
421 function scope, as are bindings of undeclared identifiers to
422 error_mark_node. (GCC permits nested functions as an extension,
423 hence the 'innermost' qualifier.) Explicitly declared labels
424 (using the __label__ extension) appear in the current scope.
426 Being in the file scope (current_scope == file_scope) causes
427 special behavior in several places below. Also, under some
428 conditions the Objective-C front end records declarations in the
429 file scope even though that isn't the current scope.
431 All declarations with external linkage are recorded in the external
432 scope, even if they aren't visible there; this models the fact that
433 such declarations are visible to the entire program, and (with a
434 bit of cleverness, see pushdecl) allows diagnosis of some violations
435 of C99 6.2.2p7 and 6.2.7p2:
437 If, within the same translation unit, the same identifier appears
438 with both internal and external linkage, the behavior is
439 undefined.
441 All declarations that refer to the same object or function shall
442 have compatible type; otherwise, the behavior is undefined.
444 Initially only the built-in declarations, which describe compiler
445 intrinsic functions plus a subset of the standard library, are in
446 this scope.
448 The order of the blocks list matters, and it is frequently appended
449 to. To avoid having to walk all the way to the end of the list on
450 each insertion, or reverse the list later, we maintain a pointer to
451 the last list entry. (FIXME: It should be feasible to use a reversed
452 list here.)
454 The bindings list is strictly in reverse order of declarations;
455 pop_scope relies on this. */
458 struct GTY((chain_next ("%h.outer"))) c_scope {
459 /* The scope containing this one. */
460 struct c_scope *outer;
462 /* The next outermost function scope. */
463 struct c_scope *outer_function;
465 /* All bindings in this scope. */
466 struct c_binding *bindings;
468 /* For each scope (except the global one), a chain of BLOCK nodes
469 for all the scopes that were entered and exited one level down. */
470 tree blocks;
471 tree blocks_last;
473 /* The depth of this scope. Used to keep the ->shadowed chain of
474 bindings sorted innermost to outermost. */
475 unsigned int depth : 28;
477 /* True if we are currently filling this scope with parameter
478 declarations. */
479 BOOL_BITFIELD parm_flag : 1;
481 /* True if we saw [*] in this scope. Used to give an error messages
482 if these appears in a function definition. */
483 BOOL_BITFIELD had_vla_unspec : 1;
485 /* True if we already complained about forward parameter decls
486 in this scope. This prevents double warnings on
487 foo (int a; int b; ...) */
488 BOOL_BITFIELD warned_forward_parm_decls : 1;
490 /* True if this is the outermost block scope of a function body.
491 This scope contains the parameters, the local variables declared
492 in the outermost block, and all the labels (except those in
493 nested functions, or declared at block scope with __label__). */
494 BOOL_BITFIELD function_body : 1;
496 /* True means make a BLOCK for this scope no matter what. */
497 BOOL_BITFIELD keep : 1;
499 /* True means that an unsuffixed float constant is _Decimal64. */
500 BOOL_BITFIELD float_const_decimal64 : 1;
502 /* True if this scope has any label bindings. This is used to speed
503 up searching for labels when popping scopes, particularly since
504 labels are normally only found at function scope. */
505 BOOL_BITFIELD has_label_bindings : 1;
507 /* True if we should issue a warning if a goto statement crosses any
508 of the bindings. We still need to check the list of bindings to
509 find the specific ones we need to warn about. This is true if
510 decl_jump_unsafe would return true for any of the bindings. This
511 is used to avoid looping over all the bindings unnecessarily. */
512 BOOL_BITFIELD has_jump_unsafe_decl : 1;
515 /* The scope currently in effect. */
517 static GTY(()) struct c_scope *current_scope;
519 /* The innermost function scope. Ordinary (not explicitly declared)
520 labels, bindings to error_mark_node, and the lazily-created
521 bindings of __func__ and its friends get this scope. */
523 static GTY(()) struct c_scope *current_function_scope;
525 /* The C file scope. This is reset for each input translation unit. */
527 static GTY(()) struct c_scope *file_scope;
529 /* The outermost scope. This is used for all declarations with
530 external linkage, and only these, hence the name. */
532 static GTY(()) struct c_scope *external_scope;
534 /* A chain of c_scope structures awaiting reuse. */
536 static GTY((deletable)) struct c_scope *scope_freelist;
538 /* A chain of c_binding structures awaiting reuse. */
540 static GTY((deletable)) struct c_binding *binding_freelist;
542 /* Append VAR to LIST in scope SCOPE. */
543 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
544 struct c_scope *s_ = (scope); \
545 tree d_ = (decl); \
546 if (s_->list##_last) \
547 BLOCK_CHAIN (s_->list##_last) = d_; \
548 else \
549 s_->list = d_; \
550 s_->list##_last = d_; \
551 } while (0)
553 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
554 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
555 struct c_scope *t_ = (tscope); \
556 struct c_scope *f_ = (fscope); \
557 if (t_->to##_last) \
558 BLOCK_CHAIN (t_->to##_last) = f_->from; \
559 else \
560 t_->to = f_->from; \
561 t_->to##_last = f_->from##_last; \
562 } while (0)
564 /* A c_inline_static structure stores details of a static identifier
565 referenced in a definition of a function that may be an inline
566 definition if no subsequent declaration of that function uses
567 "extern" or does not use "inline". */
569 struct GTY((chain_next ("%h.next"))) c_inline_static {
570 /* The location for a diagnostic. */
571 location_t location;
573 /* The function that may be an inline definition. */
574 tree function;
576 /* The object or function referenced. */
577 tree static_decl;
579 /* What sort of reference this is. */
580 enum c_inline_static_type type;
582 /* The next such structure or NULL. */
583 struct c_inline_static *next;
586 /* List of static identifiers used or referenced in functions that may
587 be inline definitions. */
588 static GTY(()) struct c_inline_static *c_inline_statics;
590 /* True means unconditionally make a BLOCK for the next scope pushed. */
592 static bool keep_next_level_flag;
594 /* True means the next call to push_scope will be the outermost scope
595 of a function body, so do not push a new scope, merely cease
596 expecting parameter decls. */
598 static bool next_is_function_body;
600 /* A vector of pointers to c_binding structures. */
602 typedef struct c_binding *c_binding_ptr;
604 /* Information that we keep for a struct or union while it is being
605 parsed. */
607 struct c_struct_parse_info
609 /* If warn_cxx_compat, a list of types defined within this
610 struct. */
611 vec<tree> struct_types;
612 /* If warn_cxx_compat, a list of field names which have bindings,
613 and which are defined in this struct, but which are not defined
614 in any enclosing struct. This is used to clear the in_struct
615 field of the c_bindings structure. */
616 vec<c_binding_ptr> fields;
617 /* If warn_cxx_compat, a list of typedef names used when defining
618 fields in this struct. */
619 vec<tree> typedefs_seen;
622 /* Information for the struct or union currently being parsed, or
623 NULL if not parsing a struct or union. */
624 static struct c_struct_parse_info *struct_parse_info;
626 /* Forward declarations. */
627 static tree lookup_name_in_scope (tree, struct c_scope *);
628 static tree c_make_fname_decl (location_t, tree, int);
629 static tree grokdeclarator (const struct c_declarator *,
630 struct c_declspecs *,
631 enum decl_context, bool, tree *, tree *, tree *,
632 bool *, enum deprecated_states);
633 static tree grokparms (struct c_arg_info *, bool);
634 static void layout_array_type (tree);
635 static void warn_defaults_to (location_t, int, const char *, ...)
636 ATTRIBUTE_GCC_DIAG(3,4);
638 /* T is a statement. Add it to the statement-tree. This is the
639 C/ObjC version--C++ has a slightly different version of this
640 function. */
642 tree
643 add_stmt (tree t)
645 enum tree_code code = TREE_CODE (t);
647 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
649 if (!EXPR_HAS_LOCATION (t))
650 SET_EXPR_LOCATION (t, input_location);
653 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
654 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
656 /* Add T to the statement-tree. Non-side-effect statements need to be
657 recorded during statement expressions. */
658 if (!building_stmt_list_p ())
659 push_stmt_list ();
660 append_to_statement_list_force (t, &cur_stmt_list);
662 return t;
665 /* Build a pointer type using the default pointer mode. */
667 static tree
668 c_build_pointer_type (tree to_type)
670 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
671 : TYPE_ADDR_SPACE (to_type);
672 machine_mode pointer_mode;
674 if (upc_shared_type_p (to_type))
675 return build_pointer_type (to_type);
676 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
677 pointer_mode = targetm.addr_space.pointer_mode (as);
678 else
679 pointer_mode = c_default_pointer_mode;
680 return build_pointer_type_for_mode (to_type, pointer_mode, false);
684 /* Return true if we will want to say something if a goto statement
685 crosses DECL. */
687 static bool
688 decl_jump_unsafe (tree decl)
690 if (error_operand_p (decl))
691 return false;
693 /* Always warn about crossing variably modified types. */
694 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
695 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
696 return true;
698 /* Otherwise, only warn if -Wgoto-misses-init and this is an
699 initialized automatic decl. */
700 if (warn_jump_misses_init
701 && TREE_CODE (decl) == VAR_DECL
702 && !TREE_STATIC (decl)
703 && DECL_INITIAL (decl) != NULL_TREE)
704 return true;
706 return false;
710 void
711 c_print_identifier (FILE *file, tree node, int indent)
713 void (*save) (enum c_oracle_request, tree identifier);
715 /* Temporarily hide any binding oracle. Without this, calls to
716 debug_tree from the debugger will end up calling into the oracle,
717 making for a confusing debug session. As the oracle isn't needed
718 here for normal operation, it's simplest to suppress it. */
719 save = c_binding_oracle;
720 c_binding_oracle = NULL;
722 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
723 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
724 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
725 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
727 tree rid = ridpointers[C_RID_CODE (node)];
728 indent_to (file, indent + 4);
729 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
730 (void *) rid, IDENTIFIER_POINTER (rid));
733 c_binding_oracle = save;
736 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
737 which may be any of several kinds of DECL or TYPE or error_mark_node,
738 in the scope SCOPE. */
739 static void
740 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
741 bool nested, location_t locus)
743 struct c_binding *b, **here;
745 if (binding_freelist)
747 b = binding_freelist;
748 binding_freelist = b->prev;
750 else
751 b = ggc_alloc<c_binding> ();
753 b->shadowed = 0;
754 b->decl = decl;
755 b->id = name;
756 b->depth = scope->depth;
757 b->invisible = invisible;
758 b->nested = nested;
759 b->inner_comp = 0;
760 b->in_struct = 0;
761 b->locus = locus;
763 b->u.type = NULL;
765 b->prev = scope->bindings;
766 scope->bindings = b;
768 if (decl_jump_unsafe (decl))
769 scope->has_jump_unsafe_decl = 1;
771 if (!name)
772 return;
774 switch (TREE_CODE (decl))
776 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
777 case ENUMERAL_TYPE:
778 case UNION_TYPE:
779 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
780 case VAR_DECL:
781 case FUNCTION_DECL:
782 case TYPE_DECL:
783 case CONST_DECL:
784 case PARM_DECL:
785 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
787 default:
788 gcc_unreachable ();
791 /* Locate the appropriate place in the chain of shadowed decls
792 to insert this binding. Normally, scope == current_scope and
793 this does nothing. */
794 while (*here && (*here)->depth > scope->depth)
795 here = &(*here)->shadowed;
797 b->shadowed = *here;
798 *here = b;
801 /* Clear the binding structure B, stick it on the binding_freelist,
802 and return the former value of b->prev. This is used by pop_scope
803 and get_parm_info to iterate destructively over all the bindings
804 from a given scope. */
805 static struct c_binding *
806 free_binding_and_advance (struct c_binding *b)
808 struct c_binding *prev = b->prev;
810 memset (b, 0, sizeof (struct c_binding));
811 b->prev = binding_freelist;
812 binding_freelist = b;
814 return prev;
817 /* Bind a label. Like bind, but skip fields which aren't used for
818 labels, and add the LABEL_VARS value. */
819 static void
820 bind_label (tree name, tree label, struct c_scope *scope,
821 struct c_label_vars *label_vars)
823 struct c_binding *b;
825 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
826 UNKNOWN_LOCATION);
828 scope->has_label_bindings = true;
830 b = scope->bindings;
831 gcc_assert (b->decl == label);
832 label_vars->shadowed = b->u.label;
833 b->u.label = label_vars;
836 /* Hook called at end of compilation to assume 1 elt
837 for a file-scope tentative array defn that wasn't complete before. */
839 void
840 c_finish_incomplete_decl (tree decl)
842 if (TREE_CODE (decl) == VAR_DECL)
844 tree type = TREE_TYPE (decl);
845 if (type != error_mark_node
846 && TREE_CODE (type) == ARRAY_TYPE
847 && !DECL_EXTERNAL (decl)
848 && TYPE_DOMAIN (type) == 0)
850 warning_at (DECL_SOURCE_LOCATION (decl),
851 0, "array %q+D assumed to have one element", decl);
853 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
855 relayout_decl (decl);
860 /* Record that inline function FUNC contains a reference (location
861 LOC) to static DECL (file-scope or function-local according to
862 TYPE). */
864 void
865 record_inline_static (location_t loc, tree func, tree decl,
866 enum c_inline_static_type type)
868 c_inline_static *csi = ggc_alloc<c_inline_static> ();
869 csi->location = loc;
870 csi->function = func;
871 csi->static_decl = decl;
872 csi->type = type;
873 csi->next = c_inline_statics;
874 c_inline_statics = csi;
877 /* Check for references to static declarations in inline functions at
878 the end of the translation unit and diagnose them if the functions
879 are still inline definitions. */
881 static void
882 check_inline_statics (void)
884 struct c_inline_static *csi;
885 for (csi = c_inline_statics; csi; csi = csi->next)
887 if (DECL_EXTERNAL (csi->function))
888 switch (csi->type)
890 case csi_internal:
891 pedwarn (csi->location, 0,
892 "%qD is static but used in inline function %qD "
893 "which is not static", csi->static_decl, csi->function);
894 break;
895 case csi_modifiable:
896 pedwarn (csi->location, 0,
897 "%q+D is static but declared in inline function %qD "
898 "which is not static", csi->static_decl, csi->function);
899 break;
900 default:
901 gcc_unreachable ();
904 c_inline_statics = NULL;
907 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
908 for the current state, otherwise set it to uninitialized. */
910 static void
911 set_spot_bindings (struct c_spot_bindings *p, bool defining)
913 if (defining)
915 p->scope = current_scope;
916 p->bindings_in_scope = current_scope->bindings;
918 else
920 p->scope = NULL;
921 p->bindings_in_scope = NULL;
923 p->stmt_exprs = 0;
924 p->left_stmt_expr = false;
927 /* Update spot bindings P as we pop out of SCOPE. Return true if we
928 should push decls for a label. */
930 static bool
931 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
933 if (p->scope != scope)
935 /* This label or goto is defined in some other scope, or it is a
936 label which is not yet defined. There is nothing to
937 update. */
938 return false;
941 /* Adjust the spot bindings to refer to the bindings already defined
942 in the enclosing scope. */
943 p->scope = scope->outer;
944 p->bindings_in_scope = p->scope->bindings;
946 return true;
949 /* The Objective-C front-end often needs to determine the current scope. */
951 void *
952 objc_get_current_scope (void)
954 return current_scope;
957 /* The following function is used only by Objective-C. It needs to live here
958 because it accesses the innards of c_scope. */
960 void
961 objc_mark_locals_volatile (void *enclosing_blk)
963 struct c_scope *scope;
964 struct c_binding *b;
966 for (scope = current_scope;
967 scope && scope != enclosing_blk;
968 scope = scope->outer)
970 for (b = scope->bindings; b; b = b->prev)
971 objc_volatilize_decl (b->decl);
973 /* Do not climb up past the current function. */
974 if (scope->function_body)
975 break;
979 /* Return true if we are in the global binding level. */
981 bool
982 global_bindings_p (void)
984 return current_scope == file_scope;
987 void
988 keep_next_level (void)
990 keep_next_level_flag = true;
993 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
995 void
996 set_float_const_decimal64 (void)
998 current_scope->float_const_decimal64 = true;
1001 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
1003 void
1004 clear_float_const_decimal64 (void)
1006 current_scope->float_const_decimal64 = false;
1009 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1011 bool
1012 float_const_decimal64_p (void)
1014 return current_scope->float_const_decimal64;
1017 /* Identify this scope as currently being filled with parameters. */
1019 void
1020 declare_parm_level (void)
1022 current_scope->parm_flag = true;
1025 void
1026 push_scope (void)
1028 if (next_is_function_body)
1030 /* This is the transition from the parameters to the top level
1031 of the function body. These are the same scope
1032 (C99 6.2.1p4,6) so we do not push another scope structure.
1033 next_is_function_body is set only by store_parm_decls, which
1034 in turn is called when and only when we are about to
1035 encounter the opening curly brace for the function body.
1037 The outermost block of a function always gets a BLOCK node,
1038 because the debugging output routines expect that each
1039 function has at least one BLOCK. */
1040 current_scope->parm_flag = false;
1041 current_scope->function_body = true;
1042 current_scope->keep = true;
1043 current_scope->outer_function = current_function_scope;
1044 current_function_scope = current_scope;
1046 keep_next_level_flag = false;
1047 next_is_function_body = false;
1049 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1050 if (current_scope->outer)
1051 current_scope->float_const_decimal64
1052 = current_scope->outer->float_const_decimal64;
1053 else
1054 current_scope->float_const_decimal64 = false;
1056 else
1058 struct c_scope *scope;
1059 if (scope_freelist)
1061 scope = scope_freelist;
1062 scope_freelist = scope->outer;
1064 else
1065 scope = ggc_cleared_alloc<c_scope> ();
1067 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1068 if (current_scope)
1069 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1070 else
1071 scope->float_const_decimal64 = false;
1073 scope->keep = keep_next_level_flag;
1074 scope->outer = current_scope;
1075 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1077 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1078 possible. */
1079 if (current_scope && scope->depth == 0)
1081 scope->depth--;
1082 sorry ("GCC supports only %u nested scopes", scope->depth);
1085 current_scope = scope;
1086 keep_next_level_flag = false;
1090 /* This is called when we are leaving SCOPE. For each label defined
1091 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1092 These are the decls whose initialization will be skipped by a goto
1093 later in the function. */
1095 static void
1096 update_label_decls (struct c_scope *scope)
1098 struct c_scope *s;
1100 s = scope;
1101 while (s != NULL)
1103 if (s->has_label_bindings)
1105 struct c_binding *b;
1107 for (b = s->bindings; b != NULL; b = b->prev)
1109 struct c_label_vars *label_vars;
1110 struct c_binding *b1;
1111 bool hjud;
1112 unsigned int ix;
1113 struct c_goto_bindings *g;
1115 if (TREE_CODE (b->decl) != LABEL_DECL)
1116 continue;
1117 label_vars = b->u.label;
1119 b1 = label_vars->label_bindings.bindings_in_scope;
1120 if (label_vars->label_bindings.scope == NULL)
1121 hjud = false;
1122 else
1123 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1124 if (update_spot_bindings (scope, &label_vars->label_bindings))
1126 /* This label is defined in this scope. */
1127 if (hjud)
1129 for (; b1 != NULL; b1 = b1->prev)
1131 /* A goto from later in the function to this
1132 label will never see the initialization
1133 of B1, if any. Save it to issue a
1134 warning if needed. */
1135 if (decl_jump_unsafe (b1->decl))
1136 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1141 /* Update the bindings of any goto statements associated
1142 with this label. */
1143 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1144 update_spot_bindings (scope, &g->goto_bindings);
1148 /* Don't search beyond the current function. */
1149 if (s == current_function_scope)
1150 break;
1152 s = s->outer;
1156 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1158 static void
1159 set_type_context (tree type, tree context)
1161 for (type = TYPE_MAIN_VARIANT (type); type;
1162 type = TYPE_NEXT_VARIANT (type))
1163 TYPE_CONTEXT (type) = context;
1166 /* Exit a scope. Restore the state of the identifier-decl mappings
1167 that were in effect when this scope was entered. Return a BLOCK
1168 node containing all the DECLs in this scope that are of interest
1169 to debug info generation. */
1171 tree
1172 pop_scope (void)
1174 struct c_scope *scope = current_scope;
1175 tree block, context, p;
1176 struct c_binding *b;
1178 bool functionbody = scope->function_body;
1179 bool keep = functionbody || scope->keep || scope->bindings;
1181 update_label_decls (scope);
1183 /* If appropriate, create a BLOCK to record the decls for the life
1184 of this function. */
1185 block = 0;
1186 if (keep)
1188 block = make_node (BLOCK);
1189 BLOCK_SUBBLOCKS (block) = scope->blocks;
1190 TREE_USED (block) = 1;
1192 /* In each subblock, record that this is its superior. */
1193 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1194 BLOCK_SUPERCONTEXT (p) = block;
1196 BLOCK_VARS (block) = 0;
1199 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1200 scope must be set so that they point to the appropriate
1201 construct, i.e. either to the current FUNCTION_DECL node, or
1202 else to the BLOCK node we just constructed.
1204 Note that for tagged types whose scope is just the formal
1205 parameter list for some function type specification, we can't
1206 properly set their TYPE_CONTEXTs here, because we don't have a
1207 pointer to the appropriate FUNCTION_TYPE node readily available
1208 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1209 type nodes get set in `grokdeclarator' as soon as we have created
1210 the FUNCTION_TYPE node which will represent the "scope" for these
1211 "parameter list local" tagged types. */
1212 if (scope->function_body)
1213 context = current_function_decl;
1214 else if (scope == file_scope)
1216 tree file_decl = build_translation_unit_decl (NULL_TREE);
1217 context = file_decl;
1219 else
1220 context = block;
1222 /* Clear all bindings in this scope. */
1223 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1225 p = b->decl;
1226 switch (TREE_CODE (p))
1228 case LABEL_DECL:
1229 /* Warnings for unused labels, errors for undefined labels. */
1230 if (TREE_USED (p) && !DECL_INITIAL (p))
1232 error ("label %q+D used but not defined", p);
1233 DECL_INITIAL (p) = error_mark_node;
1235 else
1236 warn_for_unused_label (p);
1238 /* Labels go in BLOCK_VARS. */
1239 DECL_CHAIN (p) = BLOCK_VARS (block);
1240 BLOCK_VARS (block) = p;
1241 gcc_assert (I_LABEL_BINDING (b->id) == b);
1242 I_LABEL_BINDING (b->id) = b->shadowed;
1244 /* Also pop back to the shadowed label_vars. */
1245 release_tree_vector (b->u.label->decls_in_scope);
1246 b->u.label = b->u.label->shadowed;
1247 break;
1249 case ENUMERAL_TYPE:
1250 case UNION_TYPE:
1251 case RECORD_TYPE:
1252 set_type_context (p, context);
1254 /* Types may not have tag-names, in which case the type
1255 appears in the bindings list with b->id NULL. */
1256 if (b->id)
1258 gcc_assert (I_TAG_BINDING (b->id) == b);
1259 I_TAG_BINDING (b->id) = b->shadowed;
1261 break;
1263 case FUNCTION_DECL:
1264 /* Propagate TREE_ADDRESSABLE from nested functions to their
1265 containing functions. */
1266 if (!TREE_ASM_WRITTEN (p)
1267 && DECL_INITIAL (p) != 0
1268 && TREE_ADDRESSABLE (p)
1269 && DECL_ABSTRACT_ORIGIN (p) != 0
1270 && DECL_ABSTRACT_ORIGIN (p) != p)
1271 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1272 if (!DECL_EXTERNAL (p)
1273 && !DECL_INITIAL (p)
1274 && scope != file_scope
1275 && scope != external_scope)
1277 error ("nested function %q+D declared but never defined", p);
1278 undef_nested_function = true;
1280 else if (DECL_DECLARED_INLINE_P (p)
1281 && TREE_PUBLIC (p)
1282 && !DECL_INITIAL (p))
1284 /* C99 6.7.4p6: "a function with external linkage... declared
1285 with an inline function specifier ... shall also be defined
1286 in the same translation unit." */
1287 if (!flag_gnu89_inline
1288 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1289 && scope != external_scope)
1290 pedwarn (input_location, 0,
1291 "inline function %q+D declared but never defined", p);
1292 DECL_EXTERNAL (p) = 1;
1295 goto common_symbol;
1297 case VAR_DECL:
1298 /* Warnings for unused variables. */
1299 if ((!TREE_USED (p) || !DECL_READ_P (p))
1300 && !TREE_NO_WARNING (p)
1301 && !DECL_IN_SYSTEM_HEADER (p)
1302 && DECL_NAME (p)
1303 && !DECL_ARTIFICIAL (p)
1304 && scope != file_scope
1305 && scope != external_scope)
1307 if (!TREE_USED (p))
1308 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1309 else if (DECL_CONTEXT (p) == current_function_decl)
1310 warning_at (DECL_SOURCE_LOCATION (p),
1311 OPT_Wunused_but_set_variable,
1312 "variable %qD set but not used", p);
1315 if (b->inner_comp)
1317 error ("type of array %q+D completed incompatibly with"
1318 " implicit initialization", p);
1321 /* Fall through. */
1322 case TYPE_DECL:
1323 case CONST_DECL:
1324 common_symbol:
1325 /* All of these go in BLOCK_VARS, but only if this is the
1326 binding in the home scope. */
1327 if (!b->nested)
1329 DECL_CHAIN (p) = BLOCK_VARS (block);
1330 BLOCK_VARS (block) = p;
1332 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1334 /* For block local externs add a special
1335 DECL_EXTERNAL decl for debug info generation. */
1336 tree extp = copy_node (p);
1338 DECL_EXTERNAL (extp) = 1;
1339 TREE_STATIC (extp) = 0;
1340 TREE_PUBLIC (extp) = 1;
1341 DECL_INITIAL (extp) = NULL_TREE;
1342 DECL_LANG_SPECIFIC (extp) = NULL;
1343 DECL_CONTEXT (extp) = current_function_decl;
1344 if (TREE_CODE (p) == FUNCTION_DECL)
1346 DECL_RESULT (extp) = NULL_TREE;
1347 DECL_SAVED_TREE (extp) = NULL_TREE;
1348 DECL_STRUCT_FUNCTION (extp) = NULL;
1350 if (b->locus != UNKNOWN_LOCATION)
1351 DECL_SOURCE_LOCATION (extp) = b->locus;
1352 DECL_CHAIN (extp) = BLOCK_VARS (block);
1353 BLOCK_VARS (block) = extp;
1355 /* If this is the file scope set DECL_CONTEXT of each decl to
1356 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1357 work. */
1358 if (scope == file_scope)
1360 DECL_CONTEXT (p) = context;
1361 if (TREE_CODE (p) == TYPE_DECL
1362 && TREE_TYPE (p) != error_mark_node)
1363 set_type_context (TREE_TYPE (p), context);
1366 /* Fall through. */
1367 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1368 already been put there by store_parm_decls. Unused-
1369 parameter warnings are handled by function.c.
1370 error_mark_node obviously does not go in BLOCK_VARS and
1371 does not get unused-variable warnings. */
1372 case PARM_DECL:
1373 case ERROR_MARK:
1374 /* It is possible for a decl not to have a name. We get
1375 here with b->id NULL in this case. */
1376 if (b->id)
1378 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1379 I_SYMBOL_BINDING (b->id) = b->shadowed;
1380 if (b->shadowed && b->shadowed->u.type)
1381 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1383 break;
1385 default:
1386 gcc_unreachable ();
1391 /* Dispose of the block that we just made inside some higher level. */
1392 if ((scope->function_body || scope == file_scope) && context)
1394 DECL_INITIAL (context) = block;
1395 BLOCK_SUPERCONTEXT (block) = context;
1397 else if (scope->outer)
1399 if (block)
1400 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1401 /* If we did not make a block for the scope just exited, any
1402 blocks made for inner scopes must be carried forward so they
1403 will later become subblocks of something else. */
1404 else if (scope->blocks)
1405 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1408 /* Pop the current scope, and free the structure for reuse. */
1409 current_scope = scope->outer;
1410 if (scope->function_body)
1411 current_function_scope = scope->outer_function;
1413 memset (scope, 0, sizeof (struct c_scope));
1414 scope->outer = scope_freelist;
1415 scope_freelist = scope;
1417 return block;
1420 void
1421 push_file_scope (void)
1423 tree decl;
1425 if (file_scope)
1426 return;
1428 push_scope ();
1429 file_scope = current_scope;
1431 start_fname_decls ();
1433 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1434 bind (DECL_NAME (decl), decl, file_scope,
1435 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1438 void
1439 pop_file_scope (void)
1441 /* In case there were missing closebraces, get us back to the global
1442 binding level. */
1443 while (current_scope != file_scope)
1444 pop_scope ();
1446 /* __FUNCTION__ is defined at file scope (""). This
1447 call may not be necessary as my tests indicate it
1448 still works without it. */
1449 finish_fname_decls ();
1451 check_inline_statics ();
1453 /* This is the point to write out a PCH if we're doing that.
1454 In that case we do not want to do anything else. */
1455 if (pch_file)
1457 c_common_write_pch ();
1458 return;
1461 /* Pop off the file scope and close this translation unit. */
1462 pop_scope ();
1463 file_scope = 0;
1465 maybe_apply_pending_pragma_weaks ();
1468 /* Adjust the bindings for the start of a statement expression. */
1470 void
1471 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1473 struct c_scope *scope;
1475 for (scope = current_scope; scope != NULL; scope = scope->outer)
1477 struct c_binding *b;
1479 if (!scope->has_label_bindings)
1480 continue;
1482 for (b = scope->bindings; b != NULL; b = b->prev)
1484 struct c_label_vars *label_vars;
1485 unsigned int ix;
1486 struct c_goto_bindings *g;
1488 if (TREE_CODE (b->decl) != LABEL_DECL)
1489 continue;
1490 label_vars = b->u.label;
1491 ++label_vars->label_bindings.stmt_exprs;
1492 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1493 ++g->goto_bindings.stmt_exprs;
1497 if (switch_bindings != NULL)
1498 ++switch_bindings->stmt_exprs;
1501 /* Adjust the bindings for the end of a statement expression. */
1503 void
1504 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1506 struct c_scope *scope;
1508 for (scope = current_scope; scope != NULL; scope = scope->outer)
1510 struct c_binding *b;
1512 if (!scope->has_label_bindings)
1513 continue;
1515 for (b = scope->bindings; b != NULL; b = b->prev)
1517 struct c_label_vars *label_vars;
1518 unsigned int ix;
1519 struct c_goto_bindings *g;
1521 if (TREE_CODE (b->decl) != LABEL_DECL)
1522 continue;
1523 label_vars = b->u.label;
1524 --label_vars->label_bindings.stmt_exprs;
1525 if (label_vars->label_bindings.stmt_exprs < 0)
1527 label_vars->label_bindings.left_stmt_expr = true;
1528 label_vars->label_bindings.stmt_exprs = 0;
1530 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1532 --g->goto_bindings.stmt_exprs;
1533 if (g->goto_bindings.stmt_exprs < 0)
1535 g->goto_bindings.left_stmt_expr = true;
1536 g->goto_bindings.stmt_exprs = 0;
1542 if (switch_bindings != NULL)
1544 --switch_bindings->stmt_exprs;
1545 gcc_assert (switch_bindings->stmt_exprs >= 0);
1549 /* Push a definition or a declaration of struct, union or enum tag "name".
1550 "type" should be the type node.
1551 We assume that the tag "name" is not already defined, and has a location
1552 of LOC.
1554 Note that the definition may really be just a forward reference.
1555 In that case, the TYPE_SIZE will be zero. */
1557 static void
1558 pushtag (location_t loc, tree name, tree type)
1560 /* Record the identifier as the type's name if it has none. */
1561 if (name && !TYPE_NAME (type))
1562 TYPE_NAME (type) = name;
1563 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1565 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1566 tagged type we just added to the current scope. This fake
1567 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1568 to output a representation of a tagged type, and it also gives
1569 us a convenient place to record the "scope start" address for the
1570 tagged type. */
1572 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1573 TYPE_DECL, NULL_TREE, type));
1575 /* An approximation for now, so we can tell this is a function-scope tag.
1576 This will be updated in pop_scope. */
1577 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1579 if (warn_cxx_compat && name != NULL_TREE)
1581 struct c_binding *b = I_SYMBOL_BINDING (name);
1583 if (b != NULL
1584 && b->decl != NULL_TREE
1585 && TREE_CODE (b->decl) == TYPE_DECL
1586 && (B_IN_CURRENT_SCOPE (b)
1587 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1588 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1589 != TYPE_MAIN_VARIANT (type)))
1591 warning_at (loc, OPT_Wc___compat,
1592 ("using %qD as both a typedef and a tag is "
1593 "invalid in C++"),
1594 b->decl);
1595 if (b->locus != UNKNOWN_LOCATION)
1596 inform (b->locus, "originally defined here");
1601 /* An exported interface to pushtag. This is used by the gdb plugin's
1602 binding oracle to introduce a new tag binding. */
1604 void
1605 c_pushtag (location_t loc, tree name, tree type)
1607 pushtag (loc, name, type);
1610 /* An exported interface to bind a declaration. LOC is the location
1611 to use. DECL is the declaration to bind. The decl's name is used
1612 to determine how it is bound. If DECL is a VAR_DECL, then
1613 IS_GLOBAL determines whether the decl is put into the global (file
1614 and external) scope or the current function's scope; if DECL is not
1615 a VAR_DECL then it is always put into the file scope. */
1617 void
1618 c_bind (location_t loc, tree decl, bool is_global)
1620 struct c_scope *scope;
1621 bool nested = false;
1623 if (TREE_CODE (decl) != VAR_DECL || current_function_scope == NULL)
1625 /* Types and functions are always considered to be global. */
1626 scope = file_scope;
1627 DECL_EXTERNAL (decl) = 1;
1628 TREE_PUBLIC (decl) = 1;
1630 else if (is_global)
1632 /* Also bind it into the external scope. */
1633 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1634 nested = true;
1635 scope = file_scope;
1636 DECL_EXTERNAL (decl) = 1;
1637 TREE_PUBLIC (decl) = 1;
1639 else
1641 DECL_CONTEXT (decl) = current_function_decl;
1642 TREE_PUBLIC (decl) = 0;
1643 scope = current_function_scope;
1646 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1649 /* Subroutine of compare_decls. Allow harmless mismatches in return
1650 and argument types provided that the type modes match. This function
1651 return a unified type given a suitable match, and 0 otherwise. */
1653 static tree
1654 match_builtin_function_types (tree newtype, tree oldtype)
1656 tree newrettype, oldrettype;
1657 tree newargs, oldargs;
1658 tree trytype, tryargs;
1660 /* Accept the return type of the new declaration if same modes. */
1661 oldrettype = TREE_TYPE (oldtype);
1662 newrettype = TREE_TYPE (newtype);
1664 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1665 return 0;
1667 oldargs = TYPE_ARG_TYPES (oldtype);
1668 newargs = TYPE_ARG_TYPES (newtype);
1669 tryargs = newargs;
1671 while (oldargs || newargs)
1673 if (!oldargs
1674 || !newargs
1675 || !TREE_VALUE (oldargs)
1676 || !TREE_VALUE (newargs)
1677 || TYPE_MODE (TREE_VALUE (oldargs))
1678 != TYPE_MODE (TREE_VALUE (newargs)))
1679 return 0;
1681 oldargs = TREE_CHAIN (oldargs);
1682 newargs = TREE_CHAIN (newargs);
1685 trytype = build_function_type (newrettype, tryargs);
1686 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1689 /* Subroutine of diagnose_mismatched_decls. Check for function type
1690 mismatch involving an empty arglist vs a nonempty one and give clearer
1691 diagnostics. */
1692 static void
1693 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1694 tree newtype, tree oldtype)
1696 tree t;
1698 if (TREE_CODE (olddecl) != FUNCTION_DECL
1699 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1700 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1701 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1702 return;
1704 t = TYPE_ARG_TYPES (oldtype);
1705 if (t == 0)
1706 t = TYPE_ARG_TYPES (newtype);
1707 for (; t; t = TREE_CHAIN (t))
1709 tree type = TREE_VALUE (t);
1711 if (TREE_CHAIN (t) == 0
1712 && TYPE_MAIN_VARIANT (type) != void_type_node)
1714 inform (input_location, "a parameter list with an ellipsis can%'t match "
1715 "an empty parameter name list declaration");
1716 break;
1719 if (c_type_promotes_to (type) != type)
1721 inform (input_location, "an argument type that has a default promotion can%'t match "
1722 "an empty parameter name list declaration");
1723 break;
1728 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1729 old-style function definition, NEWDECL is a prototype declaration.
1730 Diagnose inconsistencies in the argument list. Returns TRUE if
1731 the prototype is compatible, FALSE if not. */
1732 static bool
1733 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1735 tree newargs, oldargs;
1736 int i;
1738 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1740 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1741 newargs = TYPE_ARG_TYPES (newtype);
1742 i = 1;
1744 for (;;)
1746 tree oldargtype = TREE_VALUE (oldargs);
1747 tree newargtype = TREE_VALUE (newargs);
1749 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1750 return false;
1752 oldargtype = (TYPE_ATOMIC (oldargtype)
1753 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1754 TYPE_QUAL_ATOMIC)
1755 : TYPE_MAIN_VARIANT (oldargtype));
1756 newargtype = (TYPE_ATOMIC (newargtype)
1757 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1758 TYPE_QUAL_ATOMIC)
1759 : TYPE_MAIN_VARIANT (newargtype));
1761 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1762 break;
1764 /* Reaching the end of just one list means the two decls don't
1765 agree on the number of arguments. */
1766 if (END_OF_ARGLIST (oldargtype))
1768 error ("prototype for %q+D declares more arguments "
1769 "than previous old-style definition", newdecl);
1770 return false;
1772 else if (END_OF_ARGLIST (newargtype))
1774 error ("prototype for %q+D declares fewer arguments "
1775 "than previous old-style definition", newdecl);
1776 return false;
1779 /* Type for passing arg must be consistent with that declared
1780 for the arg. */
1781 else if (!comptypes (oldargtype, newargtype))
1783 error ("prototype for %q+D declares argument %d"
1784 " with incompatible type",
1785 newdecl, i);
1786 return false;
1789 oldargs = TREE_CHAIN (oldargs);
1790 newargs = TREE_CHAIN (newargs);
1791 i++;
1794 /* If we get here, no errors were found, but do issue a warning
1795 for this poor-style construct. */
1796 warning (0, "prototype for %q+D follows non-prototype definition",
1797 newdecl);
1798 return true;
1799 #undef END_OF_ARGLIST
1802 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1803 first in a pair of mismatched declarations, using the diagnostic
1804 function DIAG. */
1805 static void
1806 locate_old_decl (tree decl)
1808 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1809 && !C_DECL_DECLARED_BUILTIN (decl))
1811 else if (DECL_INITIAL (decl))
1812 inform (input_location, "previous definition of %q+D was here", decl);
1813 else if (C_DECL_IMPLICIT (decl))
1814 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1815 else
1816 inform (input_location, "previous declaration of %q+D was here", decl);
1819 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1820 Returns true if the caller should proceed to merge the two, false
1821 if OLDDECL should simply be discarded. As a side effect, issues
1822 all necessary diagnostics for invalid or poor-style combinations.
1823 If it returns true, writes the types of NEWDECL and OLDDECL to
1824 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1825 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1827 static bool
1828 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1829 tree *newtypep, tree *oldtypep)
1831 tree newtype, oldtype;
1832 bool pedwarned = false;
1833 bool warned = false;
1834 bool retval = true;
1836 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1837 && DECL_EXTERNAL (DECL))
1839 /* If we have error_mark_node for either decl or type, just discard
1840 the previous decl - we're in an error cascade already. */
1841 if (olddecl == error_mark_node || newdecl == error_mark_node)
1842 return false;
1843 *oldtypep = oldtype = TREE_TYPE (olddecl);
1844 *newtypep = newtype = TREE_TYPE (newdecl);
1845 if (oldtype == error_mark_node || newtype == error_mark_node)
1846 return false;
1848 /* Two different categories of symbol altogether. This is an error
1849 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1850 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1852 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1853 && DECL_BUILT_IN (olddecl)
1854 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1856 error ("%q+D redeclared as different kind of symbol", newdecl);
1857 locate_old_decl (olddecl);
1859 else if (TREE_PUBLIC (newdecl))
1860 warning (0, "built-in function %q+D declared as non-function",
1861 newdecl);
1862 else
1863 warning (OPT_Wshadow, "declaration of %q+D shadows "
1864 "a built-in function", newdecl);
1865 return false;
1868 /* Enumerators have no linkage, so may only be declared once in a
1869 given scope. */
1870 if (TREE_CODE (olddecl) == CONST_DECL)
1872 error ("redeclaration of enumerator %q+D", newdecl);
1873 locate_old_decl (olddecl);
1874 return false;
1877 if (!comptypes (oldtype, newtype))
1879 if (TREE_CODE (olddecl) == FUNCTION_DECL
1880 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1882 /* Accept harmless mismatch in function types.
1883 This is for the ffs and fprintf builtins. */
1884 tree trytype = match_builtin_function_types (newtype, oldtype);
1886 if (trytype && comptypes (newtype, trytype))
1887 *oldtypep = oldtype = trytype;
1888 else
1890 /* If types don't match for a built-in, throw away the
1891 built-in. No point in calling locate_old_decl here, it
1892 won't print anything. */
1893 warning (0, "conflicting types for built-in function %q+D",
1894 newdecl);
1895 return false;
1898 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1899 && DECL_IS_BUILTIN (olddecl))
1901 /* A conflicting function declaration for a predeclared
1902 function that isn't actually built in. Objective C uses
1903 these. The new declaration silently overrides everything
1904 but the volatility (i.e. noreturn) indication. See also
1905 below. FIXME: Make Objective C use normal builtins. */
1906 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1907 return false;
1909 /* Permit void foo (...) to match int foo (...) if the latter is
1910 the definition and implicit int was used. See
1911 c-torture/compile/920625-2.c. */
1912 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1913 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1914 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1915 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1917 pedwarned = pedwarn (input_location, 0,
1918 "conflicting types for %q+D", newdecl);
1919 /* Make sure we keep void as the return type. */
1920 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1921 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1923 /* Permit void foo (...) to match an earlier call to foo (...) with
1924 no declared type (thus, implicitly int). */
1925 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1926 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1927 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1928 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1930 pedwarned = pedwarn (input_location, 0,
1931 "conflicting types for %q+D", newdecl);
1932 /* Make sure we keep void as the return type. */
1933 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1935 else
1937 int new_quals = TYPE_QUALS (newtype);
1938 int old_quals = TYPE_QUALS (oldtype);
1940 if (new_quals != old_quals)
1942 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1943 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1944 if (new_addr != old_addr)
1946 if (ADDR_SPACE_GENERIC_P (new_addr))
1947 error ("conflicting named address spaces (generic vs %s) "
1948 "for %q+D",
1949 c_addr_space_name (old_addr), newdecl);
1950 else if (ADDR_SPACE_GENERIC_P (old_addr))
1951 error ("conflicting named address spaces (%s vs generic) "
1952 "for %q+D",
1953 c_addr_space_name (new_addr), newdecl);
1954 else
1955 error ("conflicting named address spaces (%s vs %s) "
1956 "for %q+D",
1957 c_addr_space_name (new_addr),
1958 c_addr_space_name (old_addr),
1959 newdecl);
1962 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1963 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1964 error ("conflicting type qualifiers for %q+D", newdecl);
1966 else
1967 error ("conflicting types for %q+D", newdecl);
1968 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1969 locate_old_decl (olddecl);
1970 return false;
1974 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1975 but silently ignore the redeclaration if either is in a system
1976 header. (Conflicting redeclarations were handled above.) This
1977 is allowed for C11 if the types are the same, not just
1978 compatible. */
1979 if (TREE_CODE (newdecl) == TYPE_DECL)
1981 bool types_different = false;
1982 int comptypes_result;
1984 comptypes_result
1985 = comptypes_check_different_types (oldtype, newtype, &types_different);
1987 if (comptypes_result != 1 || types_different)
1989 error ("redefinition of typedef %q+D with different type", newdecl);
1990 locate_old_decl (olddecl);
1991 return false;
1994 if (DECL_IN_SYSTEM_HEADER (newdecl)
1995 || DECL_IN_SYSTEM_HEADER (olddecl)
1996 || TREE_NO_WARNING (newdecl)
1997 || TREE_NO_WARNING (olddecl))
1998 return true; /* Allow OLDDECL to continue in use. */
2000 if (variably_modified_type_p (newtype, NULL))
2002 error ("redefinition of typedef %q+D with variably modified type",
2003 newdecl);
2004 locate_old_decl (olddecl);
2006 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
2007 "redefinition of typedef %q+D", newdecl))
2008 locate_old_decl (olddecl);
2010 return true;
2013 /* Function declarations can either be 'static' or 'extern' (no
2014 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2015 can never conflict with each other on account of linkage
2016 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2017 gnu89 mode permits two definitions if one is 'extern inline' and
2018 one is not. The non- extern-inline definition supersedes the
2019 extern-inline definition. */
2021 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2023 /* If you declare a built-in function name as static, or
2024 define the built-in with an old-style definition (so we
2025 can't validate the argument list) the built-in definition is
2026 overridden, but optionally warn this was a bad choice of name. */
2027 if (DECL_BUILT_IN (olddecl)
2028 && !C_DECL_DECLARED_BUILTIN (olddecl)
2029 && (!TREE_PUBLIC (newdecl)
2030 || (DECL_INITIAL (newdecl)
2031 && !prototype_p (TREE_TYPE (newdecl)))))
2033 warning (OPT_Wshadow, "declaration of %q+D shadows "
2034 "a built-in function", newdecl);
2035 /* Discard the old built-in function. */
2036 return false;
2039 if (DECL_INITIAL (newdecl))
2041 if (DECL_INITIAL (olddecl))
2043 /* If both decls are in the same TU and the new declaration
2044 isn't overriding an extern inline reject the new decl.
2045 In c99, no overriding is allowed in the same translation
2046 unit. */
2047 if ((!DECL_EXTERN_INLINE (olddecl)
2048 || DECL_EXTERN_INLINE (newdecl)
2049 || (!flag_gnu89_inline
2050 && (!DECL_DECLARED_INLINE_P (olddecl)
2051 || !lookup_attribute ("gnu_inline",
2052 DECL_ATTRIBUTES (olddecl)))
2053 && (!DECL_DECLARED_INLINE_P (newdecl)
2054 || !lookup_attribute ("gnu_inline",
2055 DECL_ATTRIBUTES (newdecl))))
2057 && same_translation_unit_p (newdecl, olddecl))
2059 error ("redefinition of %q+D", newdecl);
2060 locate_old_decl (olddecl);
2061 return false;
2065 /* If we have a prototype after an old-style function definition,
2066 the argument types must be checked specially. */
2067 else if (DECL_INITIAL (olddecl)
2068 && !prototype_p (oldtype) && prototype_p (newtype)
2069 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2070 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2072 locate_old_decl (olddecl);
2073 return false;
2075 /* A non-static declaration (even an "extern") followed by a
2076 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2077 The same is true for a static forward declaration at block
2078 scope followed by a non-static declaration/definition at file
2079 scope. Static followed by non-static at the same scope is
2080 not undefined behavior, and is the most convenient way to get
2081 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2082 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2083 we do diagnose it if -Wtraditional. */
2084 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2086 /* Two exceptions to the rule. If olddecl is an extern
2087 inline, or a predeclared function that isn't actually
2088 built in, newdecl silently overrides olddecl. The latter
2089 occur only in Objective C; see also above. (FIXME: Make
2090 Objective C use normal builtins.) */
2091 if (!DECL_IS_BUILTIN (olddecl)
2092 && !DECL_EXTERN_INLINE (olddecl))
2094 error ("static declaration of %q+D follows "
2095 "non-static declaration", newdecl);
2096 locate_old_decl (olddecl);
2098 return false;
2100 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2102 if (DECL_CONTEXT (olddecl))
2104 error ("non-static declaration of %q+D follows "
2105 "static declaration", newdecl);
2106 locate_old_decl (olddecl);
2107 return false;
2109 else if (warn_traditional)
2111 warned |= warning (OPT_Wtraditional,
2112 "non-static declaration of %q+D "
2113 "follows static declaration", newdecl);
2117 /* Make sure gnu_inline attribute is either not present, or
2118 present on all inline decls. */
2119 if (DECL_DECLARED_INLINE_P (olddecl)
2120 && DECL_DECLARED_INLINE_P (newdecl))
2122 bool newa = lookup_attribute ("gnu_inline",
2123 DECL_ATTRIBUTES (newdecl)) != NULL;
2124 bool olda = lookup_attribute ("gnu_inline",
2125 DECL_ATTRIBUTES (olddecl)) != NULL;
2126 if (newa != olda)
2128 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2129 newa ? newdecl : olddecl);
2130 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2131 "but not here");
2135 else if (TREE_CODE (newdecl) == VAR_DECL)
2137 /* Only variables can be thread-local, and all declarations must
2138 agree on this property. */
2139 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2141 /* Nothing to check. Since OLDDECL is marked threadprivate
2142 and NEWDECL does not have a thread-local attribute, we
2143 will merge the threadprivate attribute into NEWDECL. */
2146 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2148 if (DECL_THREAD_LOCAL_P (newdecl))
2149 error ("thread-local declaration of %q+D follows "
2150 "non-thread-local declaration", newdecl);
2151 else
2152 error ("non-thread-local declaration of %q+D follows "
2153 "thread-local declaration", newdecl);
2155 locate_old_decl (olddecl);
2156 return false;
2159 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2160 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2162 error ("redefinition of %q+D", newdecl);
2163 locate_old_decl (olddecl);
2164 return false;
2167 /* Objects declared at file scope: if the first declaration had
2168 external linkage (even if it was an external reference) the
2169 second must have external linkage as well, or the behavior is
2170 undefined. If the first declaration had internal linkage, then
2171 the second must too, or else be an external reference (in which
2172 case the composite declaration still has internal linkage).
2173 As for function declarations, we warn about the static-then-
2174 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2175 if (DECL_FILE_SCOPE_P (newdecl)
2176 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2178 if (DECL_EXTERNAL (newdecl))
2180 if (!DECL_FILE_SCOPE_P (olddecl))
2182 error ("extern declaration of %q+D follows "
2183 "declaration with no linkage", newdecl);
2184 locate_old_decl (olddecl);
2185 return false;
2187 else if (warn_traditional)
2189 warned |= warning (OPT_Wtraditional,
2190 "non-static declaration of %q+D "
2191 "follows static declaration", newdecl);
2194 else
2196 if (TREE_PUBLIC (newdecl))
2197 error ("non-static declaration of %q+D follows "
2198 "static declaration", newdecl);
2199 else
2200 error ("static declaration of %q+D follows "
2201 "non-static declaration", newdecl);
2203 locate_old_decl (olddecl);
2204 return false;
2207 /* Two objects with the same name declared at the same block
2208 scope must both be external references (6.7p3). */
2209 else if (!DECL_FILE_SCOPE_P (newdecl))
2211 if (DECL_EXTERNAL (newdecl))
2213 /* Extern with initializer at block scope, which will
2214 already have received an error. */
2216 else if (DECL_EXTERNAL (olddecl))
2218 error ("declaration of %q+D with no linkage follows "
2219 "extern declaration", newdecl);
2220 locate_old_decl (olddecl);
2222 else
2224 error ("redeclaration of %q+D with no linkage", newdecl);
2225 locate_old_decl (olddecl);
2228 return false;
2231 /* C++ does not permit a decl to appear multiple times at file
2232 scope. */
2233 if (warn_cxx_compat
2234 && DECL_FILE_SCOPE_P (newdecl)
2235 && !DECL_EXTERNAL (newdecl)
2236 && !DECL_EXTERNAL (olddecl))
2237 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2238 OPT_Wc___compat,
2239 ("duplicate declaration of %qD is "
2240 "invalid in C++"),
2241 newdecl);
2244 /* warnings */
2245 /* All decls must agree on a visibility. */
2246 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2247 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2248 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2250 warned |= warning (0, "redeclaration of %q+D with different visibility "
2251 "(old visibility preserved)", newdecl);
2254 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2256 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2257 if (DECL_DECLARED_INLINE_P (newdecl)
2258 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2259 warned |= warning (OPT_Wattributes,
2260 "inline declaration of %qD follows "
2261 "declaration with attribute noinline", newdecl);
2262 else if (DECL_DECLARED_INLINE_P (olddecl)
2263 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2264 warned |= warning (OPT_Wattributes,
2265 "declaration of %q+D with attribute "
2266 "noinline follows inline declaration ", newdecl);
2267 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2268 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2269 warned |= warning (OPT_Wattributes,
2270 "declaration of %q+D with attribute "
2271 "%qs follows declaration with attribute %qs",
2272 newdecl, "noinline", "always_inline");
2273 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2274 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2275 warned |= warning (OPT_Wattributes,
2276 "declaration of %q+D with attribute "
2277 "%qs follows declaration with attribute %qs",
2278 newdecl, "always_inline", "noinline");
2279 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2280 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2281 warned |= warning (OPT_Wattributes,
2282 "declaration of %q+D with attribute %qs follows "
2283 "declaration with attribute %qs", newdecl, "cold",
2284 "hot");
2285 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2286 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2287 warned |= warning (OPT_Wattributes,
2288 "declaration of %q+D with attribute %qs follows "
2289 "declaration with attribute %qs", newdecl, "hot",
2290 "cold");
2292 else /* PARM_DECL, VAR_DECL */
2294 /* Redeclaration of a parameter is a constraint violation (this is
2295 not explicitly stated, but follows from C99 6.7p3 [no more than
2296 one declaration of the same identifier with no linkage in the
2297 same scope, except type tags] and 6.2.2p6 [parameters have no
2298 linkage]). We must check for a forward parameter declaration,
2299 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2300 an extension, the mandatory diagnostic for which is handled by
2301 mark_forward_parm_decls. */
2303 if (TREE_CODE (newdecl) == PARM_DECL
2304 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2306 error ("redefinition of parameter %q+D", newdecl);
2307 locate_old_decl (olddecl);
2308 return false;
2312 /* Optional warning for completely redundant decls. */
2313 if (!warned && !pedwarned
2314 && warn_redundant_decls
2315 /* Don't warn about a function declaration followed by a
2316 definition. */
2317 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2318 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2319 /* Don't warn about redundant redeclarations of builtins. */
2320 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2321 && !DECL_BUILT_IN (newdecl)
2322 && DECL_BUILT_IN (olddecl)
2323 && !C_DECL_DECLARED_BUILTIN (olddecl))
2324 /* Don't warn about an extern followed by a definition. */
2325 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2326 /* Don't warn about forward parameter decls. */
2327 && !(TREE_CODE (newdecl) == PARM_DECL
2328 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2329 /* Don't warn about a variable definition following a declaration. */
2330 && !(TREE_CODE (newdecl) == VAR_DECL
2331 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2333 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2334 newdecl);
2337 /* Report location of previous decl/defn. */
2338 if (warned || pedwarned)
2339 locate_old_decl (olddecl);
2341 #undef DECL_EXTERN_INLINE
2343 return retval;
2346 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2347 consistent with OLDDECL, but carries new information. Merge the
2348 new information into OLDDECL. This function issues no
2349 diagnostics. */
2351 static void
2352 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2354 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2355 && DECL_INITIAL (newdecl) != 0);
2356 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2357 && prototype_p (TREE_TYPE (newdecl)));
2358 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2359 && prototype_p (TREE_TYPE (olddecl)));
2361 /* For real parm decl following a forward decl, rechain the old decl
2362 in its new location and clear TREE_ASM_WRITTEN (it's not a
2363 forward decl anymore). */
2364 if (TREE_CODE (newdecl) == PARM_DECL
2365 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2367 struct c_binding *b, **here;
2369 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2370 if ((*here)->decl == olddecl)
2371 goto found;
2372 gcc_unreachable ();
2374 found:
2375 b = *here;
2376 *here = b->prev;
2377 b->prev = current_scope->bindings;
2378 current_scope->bindings = b;
2380 TREE_ASM_WRITTEN (olddecl) = 0;
2383 DECL_ATTRIBUTES (newdecl)
2384 = targetm.merge_decl_attributes (olddecl, newdecl);
2386 /* Merge the data types specified in the two decls. */
2387 TREE_TYPE (newdecl)
2388 = TREE_TYPE (olddecl)
2389 = composite_type (newtype, oldtype);
2391 /* Lay the type out, unless already done. */
2392 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2394 if (TREE_TYPE (newdecl) != error_mark_node)
2395 layout_type (TREE_TYPE (newdecl));
2396 if (TREE_CODE (newdecl) != FUNCTION_DECL
2397 && TREE_CODE (newdecl) != TYPE_DECL
2398 && TREE_CODE (newdecl) != CONST_DECL)
2399 layout_decl (newdecl, 0);
2401 else
2403 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2404 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2405 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2406 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2407 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2409 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2410 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2414 /* Keep the old rtl since we can safely use it. */
2415 if (HAS_RTL_P (olddecl))
2416 COPY_DECL_RTL (olddecl, newdecl);
2418 /* Merge the type qualifiers. */
2419 if (TREE_READONLY (newdecl))
2420 TREE_READONLY (olddecl) = 1;
2422 if (TREE_THIS_VOLATILE (newdecl))
2423 TREE_THIS_VOLATILE (olddecl) = 1;
2425 if (TREE_SHARED (newdecl))
2427 TREE_SHARED (olddecl) = 1;
2428 if (TREE_CODE (newdecl) == VAR_DECL)
2429 TREE_THIS_VOLATILE (newdecl) = 1;
2432 /* Merge deprecatedness. */
2433 if (TREE_DEPRECATED (newdecl))
2434 TREE_DEPRECATED (olddecl) = 1;
2436 /* If a decl is in a system header and the other isn't, keep the one on the
2437 system header. Otherwise, keep source location of definition rather than
2438 declaration and of prototype rather than non-prototype unless that
2439 prototype is built-in. */
2440 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2441 && DECL_IN_SYSTEM_HEADER (olddecl)
2442 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2443 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2444 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2445 && DECL_IN_SYSTEM_HEADER (newdecl)
2446 && !DECL_IN_SYSTEM_HEADER (olddecl))
2447 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2448 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2449 || (old_is_prototype && !new_is_prototype
2450 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2451 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2453 /* Merge the initialization information. */
2454 if (DECL_INITIAL (newdecl) == 0)
2455 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2457 /* Merge the threadprivate attribute. */
2458 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2459 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2461 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2463 /* Copy the assembler name.
2464 Currently, it can only be defined in the prototype. */
2465 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2467 /* Use visibility of whichever declaration had it specified */
2468 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2470 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2471 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2474 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2476 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2477 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2478 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2479 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2480 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2481 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2482 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2483 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2484 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2485 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2486 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2489 /* Merge the storage class information. */
2490 merge_weak (newdecl, olddecl);
2492 /* For functions, static overrides non-static. */
2493 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2495 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2496 /* This is since we don't automatically
2497 copy the attributes of NEWDECL into OLDDECL. */
2498 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2499 /* If this clears `static', clear it in the identifier too. */
2500 if (!TREE_PUBLIC (olddecl))
2501 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2505 /* In c99, 'extern' declaration before (or after) 'inline' means this
2506 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2507 is present. */
2508 if (TREE_CODE (newdecl) == FUNCTION_DECL
2509 && !flag_gnu89_inline
2510 && (DECL_DECLARED_INLINE_P (newdecl)
2511 || DECL_DECLARED_INLINE_P (olddecl))
2512 && (!DECL_DECLARED_INLINE_P (newdecl)
2513 || !DECL_DECLARED_INLINE_P (olddecl)
2514 || !DECL_EXTERNAL (olddecl))
2515 && DECL_EXTERNAL (newdecl)
2516 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2517 && !current_function_decl)
2518 DECL_EXTERNAL (newdecl) = 0;
2520 /* An inline definition following a static declaration is not
2521 DECL_EXTERNAL. */
2522 if (new_is_definition
2523 && (DECL_DECLARED_INLINE_P (newdecl)
2524 || DECL_DECLARED_INLINE_P (olddecl))
2525 && !TREE_PUBLIC (olddecl))
2526 DECL_EXTERNAL (newdecl) = 0;
2528 if (DECL_EXTERNAL (newdecl))
2530 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2531 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2533 /* An extern decl does not override previous storage class. */
2534 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2535 if (!DECL_EXTERNAL (newdecl))
2537 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2538 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2541 else
2543 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2544 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2547 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2549 /* If we're redefining a function previously defined as extern
2550 inline, make sure we emit debug info for the inline before we
2551 throw it away, in case it was inlined into a function that
2552 hasn't been written out yet. */
2553 if (new_is_definition && DECL_INITIAL (olddecl))
2554 /* The new defn must not be inline. */
2555 DECL_UNINLINABLE (newdecl) = 1;
2556 else
2558 /* If either decl says `inline', this fn is inline, unless
2559 its definition was passed already. */
2560 if (DECL_DECLARED_INLINE_P (newdecl)
2561 || DECL_DECLARED_INLINE_P (olddecl))
2562 DECL_DECLARED_INLINE_P (newdecl) = 1;
2564 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2565 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2567 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2568 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2569 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2570 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2573 if (DECL_BUILT_IN (olddecl))
2575 /* If redeclaring a builtin function, it stays built in.
2576 But it gets tagged as having been declared. */
2577 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2578 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2579 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2580 if (new_is_prototype)
2582 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2583 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2585 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2586 switch (fncode)
2588 /* If a compatible prototype of these builtin functions
2589 is seen, assume the runtime implements it with the
2590 expected semantics. */
2591 case BUILT_IN_STPCPY:
2592 if (builtin_decl_explicit_p (fncode))
2593 set_builtin_decl_implicit_p (fncode, true);
2594 break;
2595 default:
2596 if (builtin_decl_explicit_p (fncode))
2597 set_builtin_decl_declared_p (fncode, true);
2598 break;
2602 else
2603 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2604 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2607 /* Preserve function specific target and optimization options */
2608 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2609 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2610 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2611 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2613 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2614 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2615 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2616 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2618 /* Also preserve various other info from the definition. */
2619 if (!new_is_definition)
2621 tree t;
2622 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2623 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2624 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2625 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2626 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2627 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2628 DECL_CONTEXT (t) = newdecl;
2630 /* See if we've got a function to instantiate from. */
2631 if (DECL_SAVED_TREE (olddecl))
2632 DECL_ABSTRACT_ORIGIN (newdecl)
2633 = DECL_ABSTRACT_ORIGIN (olddecl);
2637 /* Merge the USED information. */
2638 if (TREE_USED (olddecl))
2639 TREE_USED (newdecl) = 1;
2640 else if (TREE_USED (newdecl))
2641 TREE_USED (olddecl) = 1;
2642 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2643 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2644 if (DECL_PRESERVE_P (olddecl))
2645 DECL_PRESERVE_P (newdecl) = 1;
2646 else if (DECL_PRESERVE_P (newdecl))
2647 DECL_PRESERVE_P (olddecl) = 1;
2649 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2650 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2651 DECL_ARGUMENTS (if appropriate). */
2653 unsigned olddecl_uid = DECL_UID (olddecl);
2654 tree olddecl_context = DECL_CONTEXT (olddecl);
2655 tree olddecl_arguments = NULL;
2656 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2657 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2659 memcpy ((char *) olddecl + sizeof (struct tree_common),
2660 (char *) newdecl + sizeof (struct tree_common),
2661 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2662 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2663 switch (TREE_CODE (olddecl))
2665 case FUNCTION_DECL:
2666 case VAR_DECL:
2668 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2670 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2671 (char *) newdecl + sizeof (struct tree_decl_common),
2672 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2673 olddecl->decl_with_vis.symtab_node = snode;
2675 if ((DECL_EXTERNAL (olddecl)
2676 || TREE_PUBLIC (olddecl)
2677 || TREE_STATIC (olddecl))
2678 && DECL_SECTION_NAME (newdecl) != NULL)
2679 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2681 /* This isn't quite correct for something like
2682 int __thread x attribute ((tls_model ("local-exec")));
2683 extern int __thread x;
2684 as we'll lose the "local-exec" model. */
2685 if (TREE_CODE (olddecl) == VAR_DECL
2686 && DECL_THREAD_LOCAL_P (newdecl))
2687 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2688 break;
2691 case FIELD_DECL:
2692 case PARM_DECL:
2693 case LABEL_DECL:
2694 case RESULT_DECL:
2695 case CONST_DECL:
2696 case TYPE_DECL:
2697 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2698 (char *) newdecl + sizeof (struct tree_decl_common),
2699 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2700 break;
2702 default:
2704 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2705 (char *) newdecl + sizeof (struct tree_decl_common),
2706 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2708 DECL_UID (olddecl) = olddecl_uid;
2709 DECL_CONTEXT (olddecl) = olddecl_context;
2710 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2711 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2714 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2715 so that encode_section_info has a chance to look at the new decl
2716 flags and attributes. */
2717 if (DECL_RTL_SET_P (olddecl)
2718 && (TREE_CODE (olddecl) == FUNCTION_DECL
2719 || (TREE_CODE (olddecl) == VAR_DECL
2720 && TREE_STATIC (olddecl))))
2721 make_decl_rtl (olddecl);
2724 /* Handle when a new declaration NEWDECL has the same name as an old
2725 one OLDDECL in the same binding contour. Prints an error message
2726 if appropriate.
2728 If safely possible, alter OLDDECL to look like NEWDECL, and return
2729 true. Otherwise, return false. */
2731 static bool
2732 duplicate_decls (tree newdecl, tree olddecl)
2734 tree newtype = NULL, oldtype = NULL;
2736 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2738 /* Avoid `unused variable' and other warnings for OLDDECL. */
2739 TREE_NO_WARNING (olddecl) = 1;
2740 return false;
2743 merge_decls (newdecl, olddecl, newtype, oldtype);
2745 /* The NEWDECL will no longer be needed.
2747 Before releasing the node, be sure to remove function from symbol
2748 table that might have been inserted there to record comdat group.
2749 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2750 structure is shared in between NEWDECL and OLDECL. */
2751 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2752 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2753 if (TREE_CODE (newdecl) == FUNCTION_DECL
2754 || TREE_CODE (newdecl) == VAR_DECL)
2756 struct symtab_node *snode = symtab_node::get (newdecl);
2757 if (snode)
2758 snode->remove ();
2760 ggc_free (newdecl);
2761 return true;
2765 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2766 static void
2767 warn_if_shadowing (tree new_decl)
2769 struct c_binding *b;
2771 /* Shadow warnings wanted? */
2772 if (!warn_shadow
2773 /* No shadow warnings for internally generated vars. */
2774 || DECL_IS_BUILTIN (new_decl)
2775 /* No shadow warnings for vars made for inlining. */
2776 || DECL_FROM_INLINE (new_decl))
2777 return;
2779 /* Is anything being shadowed? Invisible decls do not count. */
2780 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2781 if (b->decl && b->decl != new_decl && !b->invisible
2782 && (b->decl == error_mark_node
2783 || diagnostic_report_warnings_p (global_dc,
2784 DECL_SOURCE_LOCATION (b->decl))))
2786 tree old_decl = b->decl;
2787 bool warned = false;
2789 if (old_decl == error_mark_node)
2791 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2792 "non-variable", new_decl);
2793 break;
2795 else if (TREE_CODE (old_decl) == PARM_DECL)
2796 warned = warning (OPT_Wshadow,
2797 "declaration of %q+D shadows a parameter",
2798 new_decl);
2799 else if (DECL_FILE_SCOPE_P (old_decl))
2801 /* Do not warn if a variable shadows a function, unless
2802 the variable is a function or a pointer-to-function. */
2803 if (TREE_CODE (old_decl) == FUNCTION_DECL
2804 && TREE_CODE (new_decl) != FUNCTION_DECL
2805 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2806 continue;
2808 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2809 "declaration of %qD shadows a global "
2810 "declaration",
2811 new_decl);
2813 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2814 && DECL_BUILT_IN (old_decl))
2816 warning (OPT_Wshadow, "declaration of %q+D shadows "
2817 "a built-in function", new_decl);
2818 break;
2820 else
2821 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2822 "previous local", new_decl);
2824 if (warned)
2825 inform (DECL_SOURCE_LOCATION (old_decl),
2826 "shadowed declaration is here");
2828 break;
2832 /* Record a decl-node X as belonging to the current lexical scope.
2833 Check for errors (such as an incompatible declaration for the same
2834 name already seen in the same scope).
2836 Returns either X or an old decl for the same name.
2837 If an old decl is returned, it may have been smashed
2838 to agree with what X says. */
2840 tree
2841 pushdecl (tree x)
2843 tree name = DECL_NAME (x);
2844 struct c_scope *scope = current_scope;
2845 struct c_binding *b;
2846 bool nested = false;
2847 location_t locus = DECL_SOURCE_LOCATION (x);
2849 /* Must set DECL_CONTEXT for everything not at file scope or
2850 DECL_FILE_SCOPE_P won't work. Local externs don't count
2851 unless they have initializers (which generate code). */
2852 if (current_function_decl
2853 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2854 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2855 DECL_CONTEXT (x) = current_function_decl;
2857 /* Anonymous decls are just inserted in the scope. */
2858 if (!name)
2860 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2861 locus);
2862 return x;
2865 /* First, see if there is another declaration with the same name in
2866 the current scope. If there is, duplicate_decls may do all the
2867 work for us. If duplicate_decls returns false, that indicates
2868 two incompatible decls in the same scope; we are to silently
2869 replace the old one (duplicate_decls has issued all appropriate
2870 diagnostics). In particular, we should not consider possible
2871 duplicates in the external scope, or shadowing. */
2872 b = I_SYMBOL_BINDING (name);
2873 if (b && B_IN_SCOPE (b, scope))
2875 struct c_binding *b_ext, *b_use;
2876 tree type = TREE_TYPE (x);
2877 tree visdecl = b->decl;
2878 tree vistype = TREE_TYPE (visdecl);
2879 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2880 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2881 b->inner_comp = false;
2882 b_use = b;
2883 b_ext = b;
2884 /* If this is an external linkage declaration, we should check
2885 for compatibility with the type in the external scope before
2886 setting the type at this scope based on the visible
2887 information only. */
2888 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2890 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2891 b_ext = b_ext->shadowed;
2892 if (b_ext)
2894 b_use = b_ext;
2895 if (b_use->u.type)
2896 TREE_TYPE (b_use->decl) = b_use->u.type;
2899 if (duplicate_decls (x, b_use->decl))
2901 if (b_use != b)
2903 /* Save the updated type in the external scope and
2904 restore the proper type for this scope. */
2905 tree thistype;
2906 if (comptypes (vistype, type))
2907 thistype = composite_type (vistype, type);
2908 else
2909 thistype = TREE_TYPE (b_use->decl);
2910 b_use->u.type = TREE_TYPE (b_use->decl);
2911 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2912 && DECL_BUILT_IN (b_use->decl))
2913 thistype
2914 = build_type_attribute_variant (thistype,
2915 TYPE_ATTRIBUTES
2916 (b_use->u.type));
2917 TREE_TYPE (b_use->decl) = thistype;
2919 return b_use->decl;
2921 else
2922 goto skip_external_and_shadow_checks;
2925 /* All declarations with external linkage, and all external
2926 references, go in the external scope, no matter what scope is
2927 current. However, the binding in that scope is ignored for
2928 purposes of normal name lookup. A separate binding structure is
2929 created in the requested scope; this governs the normal
2930 visibility of the symbol.
2932 The binding in the externals scope is used exclusively for
2933 detecting duplicate declarations of the same object, no matter
2934 what scope they are in; this is what we do here. (C99 6.2.7p2:
2935 All declarations that refer to the same object or function shall
2936 have compatible type; otherwise, the behavior is undefined.) */
2937 if (DECL_EXTERNAL (x) || scope == file_scope)
2939 tree type = TREE_TYPE (x);
2940 tree vistype = 0;
2941 tree visdecl = 0;
2942 bool type_saved = false;
2943 if (b && !B_IN_EXTERNAL_SCOPE (b)
2944 && (TREE_CODE (b->decl) == FUNCTION_DECL
2945 || TREE_CODE (b->decl) == VAR_DECL)
2946 && DECL_FILE_SCOPE_P (b->decl))
2948 visdecl = b->decl;
2949 vistype = TREE_TYPE (visdecl);
2951 if (scope != file_scope
2952 && !DECL_IN_SYSTEM_HEADER (x))
2953 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2955 while (b && !B_IN_EXTERNAL_SCOPE (b))
2957 /* If this decl might be modified, save its type. This is
2958 done here rather than when the decl is first bound
2959 because the type may change after first binding, through
2960 being completed or through attributes being added. If we
2961 encounter multiple such decls, only the first should have
2962 its type saved; the others will already have had their
2963 proper types saved and the types will not have changed as
2964 their scopes will not have been re-entered. */
2965 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2967 b->u.type = TREE_TYPE (b->decl);
2968 type_saved = true;
2970 if (B_IN_FILE_SCOPE (b)
2971 && TREE_CODE (b->decl) == VAR_DECL
2972 && TREE_STATIC (b->decl)
2973 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2974 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2975 && TREE_CODE (type) == ARRAY_TYPE
2976 && TYPE_DOMAIN (type)
2977 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2978 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2980 /* Array type completed in inner scope, which should be
2981 diagnosed if the completion does not have size 1 and
2982 it does not get completed in the file scope. */
2983 b->inner_comp = true;
2985 b = b->shadowed;
2988 /* If a matching external declaration has been found, set its
2989 type to the composite of all the types of that declaration.
2990 After the consistency checks, it will be reset to the
2991 composite of the visible types only. */
2992 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2993 && b->u.type)
2994 TREE_TYPE (b->decl) = b->u.type;
2996 /* The point of the same_translation_unit_p check here is,
2997 we want to detect a duplicate decl for a construct like
2998 foo() { extern bar(); } ... static bar(); but not if
2999 they are in different translation units. In any case,
3000 the static does not go in the externals scope. */
3001 if (b
3002 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3003 && duplicate_decls (x, b->decl))
3005 tree thistype;
3006 if (vistype)
3008 if (comptypes (vistype, type))
3009 thistype = composite_type (vistype, type);
3010 else
3011 thistype = TREE_TYPE (b->decl);
3013 else
3014 thistype = type;
3015 b->u.type = TREE_TYPE (b->decl);
3016 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3017 thistype
3018 = build_type_attribute_variant (thistype,
3019 TYPE_ATTRIBUTES (b->u.type));
3020 TREE_TYPE (b->decl) = thistype;
3021 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3022 locus);
3023 return b->decl;
3025 else if (TREE_PUBLIC (x))
3027 if (visdecl && !b && duplicate_decls (x, visdecl))
3029 /* An external declaration at block scope referring to a
3030 visible entity with internal linkage. The composite
3031 type will already be correct for this scope, so we
3032 just need to fall through to make the declaration in
3033 this scope. */
3034 nested = true;
3035 x = visdecl;
3037 else
3039 bind (name, x, external_scope, /*invisible=*/true,
3040 /*nested=*/false, locus);
3041 nested = true;
3046 if (TREE_CODE (x) != PARM_DECL)
3047 warn_if_shadowing (x);
3049 skip_external_and_shadow_checks:
3050 if (TREE_CODE (x) == TYPE_DECL)
3052 /* So this is a typedef, set its underlying type. */
3053 set_underlying_type (x);
3055 /* If X is a typedef defined in the current function, record it
3056 for the purpose of implementing the -Wunused-local-typedefs
3057 warning. */
3058 record_locally_defined_typedef (x);
3061 bind (name, x, scope, /*invisible=*/false, nested, locus);
3063 /* If x's type is incomplete because it's based on a
3064 structure or union which has not yet been fully declared,
3065 attach it to that structure or union type, so we can go
3066 back and complete the variable declaration later, if the
3067 structure or union gets fully declared.
3069 If the input is erroneous, we can have error_mark in the type
3070 slot (e.g. "f(void a, ...)") - that doesn't count as an
3071 incomplete type. */
3072 if (TREE_TYPE (x) != error_mark_node
3073 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3075 tree element = TREE_TYPE (x);
3077 while (TREE_CODE (element) == ARRAY_TYPE)
3078 element = TREE_TYPE (element);
3079 element = TYPE_MAIN_VARIANT (element);
3081 if ((TREE_CODE (element) == RECORD_TYPE
3082 || TREE_CODE (element) == UNION_TYPE)
3083 && (TREE_CODE (x) != TYPE_DECL
3084 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3085 && !COMPLETE_TYPE_P (element))
3086 C_TYPE_INCOMPLETE_VARS (element)
3087 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3089 return x;
3092 /* Record X as belonging to file scope.
3093 This is used only internally by the Objective-C front end,
3094 and is limited to its needs. duplicate_decls is not called;
3095 if there is any preexisting decl for this identifier, it is an ICE. */
3097 tree
3098 pushdecl_top_level (tree x)
3100 tree name;
3101 bool nested = false;
3102 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
3104 name = DECL_NAME (x);
3106 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3108 if (TREE_PUBLIC (x))
3110 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3111 UNKNOWN_LOCATION);
3112 nested = true;
3114 if (file_scope)
3115 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3117 return x;
3120 static void
3121 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3123 if (upc_diagnose_deprecated_stmt (input_location, id))
3124 return;
3125 if (warn_implicit_function_declaration)
3127 bool warned;
3129 if (flag_isoc99)
3130 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3131 "implicit declaration of function %qE", id);
3132 else
3133 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3134 G_("implicit declaration of function %qE"), id);
3135 if (olddecl && warned)
3136 locate_old_decl (olddecl);
3140 /* This function represents mapping of a function code FCODE
3141 to its respective header. */
3143 static const char *
3144 header_for_builtin_fn (enum built_in_function fcode)
3146 switch (fcode)
3148 CASE_FLT_FN (BUILT_IN_ACOS):
3149 CASE_FLT_FN (BUILT_IN_ACOSH):
3150 CASE_FLT_FN (BUILT_IN_ASIN):
3151 CASE_FLT_FN (BUILT_IN_ASINH):
3152 CASE_FLT_FN (BUILT_IN_ATAN):
3153 CASE_FLT_FN (BUILT_IN_ATANH):
3154 CASE_FLT_FN (BUILT_IN_ATAN2):
3155 CASE_FLT_FN (BUILT_IN_CBRT):
3156 CASE_FLT_FN (BUILT_IN_CEIL):
3157 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3158 CASE_FLT_FN (BUILT_IN_COS):
3159 CASE_FLT_FN (BUILT_IN_COSH):
3160 CASE_FLT_FN (BUILT_IN_ERF):
3161 CASE_FLT_FN (BUILT_IN_ERFC):
3162 CASE_FLT_FN (BUILT_IN_EXP):
3163 CASE_FLT_FN (BUILT_IN_EXP2):
3164 CASE_FLT_FN (BUILT_IN_EXPM1):
3165 CASE_FLT_FN (BUILT_IN_FABS):
3166 CASE_FLT_FN (BUILT_IN_FDIM):
3167 CASE_FLT_FN (BUILT_IN_FLOOR):
3168 CASE_FLT_FN (BUILT_IN_FMA):
3169 CASE_FLT_FN (BUILT_IN_FMAX):
3170 CASE_FLT_FN (BUILT_IN_FMIN):
3171 CASE_FLT_FN (BUILT_IN_FMOD):
3172 CASE_FLT_FN (BUILT_IN_FREXP):
3173 CASE_FLT_FN (BUILT_IN_HYPOT):
3174 CASE_FLT_FN (BUILT_IN_ILOGB):
3175 CASE_FLT_FN (BUILT_IN_LDEXP):
3176 CASE_FLT_FN (BUILT_IN_LGAMMA):
3177 CASE_FLT_FN (BUILT_IN_LLRINT):
3178 CASE_FLT_FN (BUILT_IN_LLROUND):
3179 CASE_FLT_FN (BUILT_IN_LOG):
3180 CASE_FLT_FN (BUILT_IN_LOG10):
3181 CASE_FLT_FN (BUILT_IN_LOG1P):
3182 CASE_FLT_FN (BUILT_IN_LOG2):
3183 CASE_FLT_FN (BUILT_IN_LOGB):
3184 CASE_FLT_FN (BUILT_IN_LRINT):
3185 CASE_FLT_FN (BUILT_IN_LROUND):
3186 CASE_FLT_FN (BUILT_IN_MODF):
3187 CASE_FLT_FN (BUILT_IN_NAN):
3188 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3189 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3190 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3191 CASE_FLT_FN (BUILT_IN_POW):
3192 CASE_FLT_FN (BUILT_IN_REMAINDER):
3193 CASE_FLT_FN (BUILT_IN_REMQUO):
3194 CASE_FLT_FN (BUILT_IN_RINT):
3195 CASE_FLT_FN (BUILT_IN_ROUND):
3196 CASE_FLT_FN (BUILT_IN_SCALBLN):
3197 CASE_FLT_FN (BUILT_IN_SCALBN):
3198 CASE_FLT_FN (BUILT_IN_SIN):
3199 CASE_FLT_FN (BUILT_IN_SINH):
3200 CASE_FLT_FN (BUILT_IN_SINCOS):
3201 CASE_FLT_FN (BUILT_IN_SQRT):
3202 CASE_FLT_FN (BUILT_IN_TAN):
3203 CASE_FLT_FN (BUILT_IN_TANH):
3204 CASE_FLT_FN (BUILT_IN_TGAMMA):
3205 CASE_FLT_FN (BUILT_IN_TRUNC):
3206 case BUILT_IN_ISINF:
3207 case BUILT_IN_ISNAN:
3208 return "<math.h>";
3209 CASE_FLT_FN (BUILT_IN_CABS):
3210 CASE_FLT_FN (BUILT_IN_CACOS):
3211 CASE_FLT_FN (BUILT_IN_CACOSH):
3212 CASE_FLT_FN (BUILT_IN_CARG):
3213 CASE_FLT_FN (BUILT_IN_CASIN):
3214 CASE_FLT_FN (BUILT_IN_CASINH):
3215 CASE_FLT_FN (BUILT_IN_CATAN):
3216 CASE_FLT_FN (BUILT_IN_CATANH):
3217 CASE_FLT_FN (BUILT_IN_CCOS):
3218 CASE_FLT_FN (BUILT_IN_CCOSH):
3219 CASE_FLT_FN (BUILT_IN_CEXP):
3220 CASE_FLT_FN (BUILT_IN_CIMAG):
3221 CASE_FLT_FN (BUILT_IN_CLOG):
3222 CASE_FLT_FN (BUILT_IN_CONJ):
3223 CASE_FLT_FN (BUILT_IN_CPOW):
3224 CASE_FLT_FN (BUILT_IN_CPROJ):
3225 CASE_FLT_FN (BUILT_IN_CREAL):
3226 CASE_FLT_FN (BUILT_IN_CSIN):
3227 CASE_FLT_FN (BUILT_IN_CSINH):
3228 CASE_FLT_FN (BUILT_IN_CSQRT):
3229 CASE_FLT_FN (BUILT_IN_CTAN):
3230 CASE_FLT_FN (BUILT_IN_CTANH):
3231 return "<complex.h>";
3232 case BUILT_IN_MEMCHR:
3233 case BUILT_IN_MEMCMP:
3234 case BUILT_IN_MEMCPY:
3235 case BUILT_IN_MEMMOVE:
3236 case BUILT_IN_MEMSET:
3237 case BUILT_IN_STRCAT:
3238 case BUILT_IN_STRCHR:
3239 case BUILT_IN_STRCMP:
3240 case BUILT_IN_STRCPY:
3241 case BUILT_IN_STRCSPN:
3242 case BUILT_IN_STRLEN:
3243 case BUILT_IN_STRNCAT:
3244 case BUILT_IN_STRNCMP:
3245 case BUILT_IN_STRNCPY:
3246 case BUILT_IN_STRPBRK:
3247 case BUILT_IN_STRRCHR:
3248 case BUILT_IN_STRSPN:
3249 case BUILT_IN_STRSTR:
3250 return "<string.h>";
3251 case BUILT_IN_FPRINTF:
3252 case BUILT_IN_PUTC:
3253 case BUILT_IN_FPUTC:
3254 case BUILT_IN_FPUTS:
3255 case BUILT_IN_FSCANF:
3256 case BUILT_IN_FWRITE:
3257 case BUILT_IN_PRINTF:
3258 case BUILT_IN_PUTCHAR:
3259 case BUILT_IN_PUTS:
3260 case BUILT_IN_SCANF:
3261 case BUILT_IN_SNPRINTF:
3262 case BUILT_IN_SPRINTF:
3263 case BUILT_IN_SSCANF:
3264 case BUILT_IN_VFPRINTF:
3265 case BUILT_IN_VFSCANF:
3266 case BUILT_IN_VPRINTF:
3267 case BUILT_IN_VSCANF:
3268 case BUILT_IN_VSNPRINTF:
3269 case BUILT_IN_VSPRINTF:
3270 case BUILT_IN_VSSCANF:
3271 return "<stdio.h>";
3272 case BUILT_IN_ISALNUM:
3273 case BUILT_IN_ISALPHA:
3274 case BUILT_IN_ISBLANK:
3275 case BUILT_IN_ISCNTRL:
3276 case BUILT_IN_ISDIGIT:
3277 case BUILT_IN_ISGRAPH:
3278 case BUILT_IN_ISLOWER:
3279 case BUILT_IN_ISPRINT:
3280 case BUILT_IN_ISPUNCT:
3281 case BUILT_IN_ISSPACE:
3282 case BUILT_IN_ISUPPER:
3283 case BUILT_IN_ISXDIGIT:
3284 case BUILT_IN_TOLOWER:
3285 case BUILT_IN_TOUPPER:
3286 return "<ctype.h>";
3287 case BUILT_IN_ISWALNUM:
3288 case BUILT_IN_ISWALPHA:
3289 case BUILT_IN_ISWBLANK:
3290 case BUILT_IN_ISWCNTRL:
3291 case BUILT_IN_ISWDIGIT:
3292 case BUILT_IN_ISWGRAPH:
3293 case BUILT_IN_ISWLOWER:
3294 case BUILT_IN_ISWPRINT:
3295 case BUILT_IN_ISWPUNCT:
3296 case BUILT_IN_ISWSPACE:
3297 case BUILT_IN_ISWUPPER:
3298 case BUILT_IN_ISWXDIGIT:
3299 case BUILT_IN_TOWLOWER:
3300 case BUILT_IN_TOWUPPER:
3301 return "<wctype.h>";
3302 case BUILT_IN_ABORT:
3303 case BUILT_IN_ABS:
3304 case BUILT_IN_CALLOC:
3305 case BUILT_IN_EXIT:
3306 case BUILT_IN_FREE:
3307 case BUILT_IN_LABS:
3308 case BUILT_IN_LLABS:
3309 case BUILT_IN_MALLOC:
3310 case BUILT_IN_REALLOC:
3311 case BUILT_IN__EXIT2:
3312 case BUILT_IN_ALIGNED_ALLOC:
3313 return "<stdlib.h>";
3314 case BUILT_IN_IMAXABS:
3315 return "<inttypes.h>";
3316 case BUILT_IN_STRFTIME:
3317 return "<time.h>";
3318 default:
3319 return NULL;
3323 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3324 function of type int (). */
3326 tree
3327 implicitly_declare (location_t loc, tree functionid)
3329 struct c_binding *b;
3330 tree decl = 0;
3331 tree asmspec_tree;
3333 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3335 if (B_IN_SCOPE (b, external_scope))
3337 decl = b->decl;
3338 break;
3342 if (decl)
3344 if (decl == error_mark_node)
3345 return decl;
3347 /* FIXME: Objective-C has weird not-really-builtin functions
3348 which are supposed to be visible automatically. They wind up
3349 in the external scope because they're pushed before the file
3350 scope gets created. Catch this here and rebind them into the
3351 file scope. */
3352 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3354 bind (functionid, decl, file_scope,
3355 /*invisible=*/false, /*nested=*/true,
3356 DECL_SOURCE_LOCATION (decl));
3357 return decl;
3359 else
3361 tree newtype = default_function_type;
3362 if (b->u.type)
3363 TREE_TYPE (decl) = b->u.type;
3364 /* Implicit declaration of a function already declared
3365 (somehow) in a different scope, or as a built-in.
3366 If this is the first time this has happened, warn;
3367 then recycle the old declaration but with the new type. */
3368 if (!C_DECL_IMPLICIT (decl))
3370 implicit_decl_warning (loc, functionid, decl);
3371 C_DECL_IMPLICIT (decl) = 1;
3373 if (DECL_BUILT_IN (decl))
3375 newtype = build_type_attribute_variant (newtype,
3376 TYPE_ATTRIBUTES
3377 (TREE_TYPE (decl)));
3378 if (!comptypes (newtype, TREE_TYPE (decl)))
3380 bool warned = warning_at (loc, 0, "incompatible implicit "
3381 "declaration of built-in "
3382 "function %qD", decl);
3383 /* See if we can hint which header to include. */
3384 const char *header
3385 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3386 if (header != NULL && warned)
3387 inform (loc, "include %qs or provide a declaration of %qD",
3388 header, decl);
3389 newtype = TREE_TYPE (decl);
3392 else
3394 if (!comptypes (newtype, TREE_TYPE (decl)))
3396 error_at (loc, "incompatible implicit declaration of "
3397 "function %qD", decl);
3398 locate_old_decl (decl);
3401 b->u.type = TREE_TYPE (decl);
3402 TREE_TYPE (decl) = newtype;
3403 bind (functionid, decl, current_scope,
3404 /*invisible=*/false, /*nested=*/true,
3405 DECL_SOURCE_LOCATION (decl));
3406 return decl;
3410 /* Not seen before. */
3411 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3412 DECL_EXTERNAL (decl) = 1;
3413 TREE_PUBLIC (decl) = 1;
3414 C_DECL_IMPLICIT (decl) = 1;
3415 implicit_decl_warning (loc, functionid, 0);
3416 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3417 if (asmspec_tree)
3418 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3420 /* C89 says implicit declarations are in the innermost block.
3421 So we record the decl in the standard fashion. */
3422 decl = pushdecl (decl);
3424 /* No need to call objc_check_decl here - it's a function type. */
3425 rest_of_decl_compilation (decl, 0, 0);
3427 /* Write a record describing this implicit function declaration
3428 to the prototypes file (if requested). */
3429 gen_aux_info_record (decl, 0, 1, 0);
3431 /* Possibly apply some default attributes to this implicit declaration. */
3432 decl_attributes (&decl, NULL_TREE, 0);
3434 return decl;
3437 /* Issue an error message for a reference to an undeclared variable
3438 ID, including a reference to a builtin outside of function-call
3439 context. Establish a binding of the identifier to error_mark_node
3440 in an appropriate scope, which will suppress further errors for the
3441 same identifier. The error message should be given location LOC. */
3442 void
3443 undeclared_variable (location_t loc, tree id)
3445 static bool already = false;
3446 struct c_scope *scope;
3448 if (current_function_decl == 0)
3450 error_at (loc, "%qE undeclared here (not in a function)", id);
3451 scope = current_scope;
3453 else
3455 if (upc_diagnose_deprecated_stmt (loc, id))
3456 return;
3457 if (!objc_diagnose_private_ivar (id))
3458 error_at (loc, "%qE undeclared (first use in this function)", id);
3459 if (!already)
3461 inform (loc, "each undeclared identifier is reported only"
3462 " once for each function it appears in");
3463 already = true;
3466 /* If we are parsing old-style parameter decls, current_function_decl
3467 will be nonnull but current_function_scope will be null. */
3468 scope = current_function_scope ? current_function_scope : current_scope;
3470 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3471 UNKNOWN_LOCATION);
3474 /* Subroutine of lookup_label, declare_label, define_label: construct a
3475 LABEL_DECL with all the proper frills. Also create a struct
3476 c_label_vars initialized for the current scope. */
3478 static tree
3479 make_label (location_t location, tree name, bool defining,
3480 struct c_label_vars **p_label_vars)
3482 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3483 DECL_CONTEXT (label) = current_function_decl;
3484 DECL_MODE (label) = VOIDmode;
3486 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3487 label_vars->shadowed = NULL;
3488 set_spot_bindings (&label_vars->label_bindings, defining);
3489 label_vars->decls_in_scope = make_tree_vector ();
3490 label_vars->gotos = NULL;
3491 *p_label_vars = label_vars;
3493 return label;
3496 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3497 Create one if none exists so far for the current function.
3498 This is called when a label is used in a goto expression or
3499 has its address taken. */
3501 tree
3502 lookup_label (tree name)
3504 tree label;
3505 struct c_label_vars *label_vars;
3507 if (current_function_scope == 0)
3509 error ("label %qE referenced outside of any function", name);
3510 return 0;
3513 /* Use a label already defined or ref'd with this name, but not if
3514 it is inherited from a containing function and wasn't declared
3515 using __label__. */
3516 label = I_LABEL_DECL (name);
3517 if (label && (DECL_CONTEXT (label) == current_function_decl
3518 || C_DECLARED_LABEL_FLAG (label)))
3520 /* If the label has only been declared, update its apparent
3521 location to point here, for better diagnostics if it
3522 turns out not to have been defined. */
3523 if (DECL_INITIAL (label) == NULL_TREE)
3524 DECL_SOURCE_LOCATION (label) = input_location;
3525 return label;
3528 /* No label binding for that identifier; make one. */
3529 label = make_label (input_location, name, false, &label_vars);
3531 /* Ordinary labels go in the current function scope. */
3532 bind_label (name, label, current_function_scope, label_vars);
3534 return label;
3537 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3538 to LABEL. */
3540 static void
3541 warn_about_goto (location_t goto_loc, tree label, tree decl)
3543 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3544 error_at (goto_loc,
3545 "jump into scope of identifier with variably modified type");
3546 else
3547 warning_at (goto_loc, OPT_Wjump_misses_init,
3548 "jump skips variable initialization");
3549 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3550 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3553 /* Look up a label because of a goto statement. This is like
3554 lookup_label, but also issues any appropriate warnings. */
3556 tree
3557 lookup_label_for_goto (location_t loc, tree name)
3559 tree label;
3560 struct c_label_vars *label_vars;
3561 unsigned int ix;
3562 tree decl;
3564 label = lookup_label (name);
3565 if (label == NULL_TREE)
3566 return NULL_TREE;
3568 /* If we are jumping to a different function, we can't issue any
3569 useful warnings. */
3570 if (DECL_CONTEXT (label) != current_function_decl)
3572 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3573 return label;
3576 label_vars = I_LABEL_BINDING (name)->u.label;
3578 /* If the label has not yet been defined, then push this goto on a
3579 list for possible later warnings. */
3580 if (label_vars->label_bindings.scope == NULL)
3582 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3584 g->loc = loc;
3585 set_spot_bindings (&g->goto_bindings, true);
3586 vec_safe_push (label_vars->gotos, g);
3587 return label;
3590 /* If there are any decls in label_vars->decls_in_scope, then this
3591 goto has missed the declaration of the decl. This happens for a
3592 case like
3593 int i = 1;
3594 lab:
3596 goto lab;
3597 Issue a warning or error. */
3598 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3599 warn_about_goto (loc, label, decl);
3601 if (label_vars->label_bindings.left_stmt_expr)
3603 error_at (loc, "jump into statement expression");
3604 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3607 return label;
3610 /* Make a label named NAME in the current function, shadowing silently
3611 any that may be inherited from containing functions or containing
3612 scopes. This is called for __label__ declarations. */
3614 tree
3615 declare_label (tree name)
3617 struct c_binding *b = I_LABEL_BINDING (name);
3618 tree label;
3619 struct c_label_vars *label_vars;
3621 /* Check to make sure that the label hasn't already been declared
3622 at this scope */
3623 if (b && B_IN_CURRENT_SCOPE (b))
3625 error ("duplicate label declaration %qE", name);
3626 locate_old_decl (b->decl);
3628 /* Just use the previous declaration. */
3629 return b->decl;
3632 label = make_label (input_location, name, false, &label_vars);
3633 C_DECLARED_LABEL_FLAG (label) = 1;
3635 /* Declared labels go in the current scope. */
3636 bind_label (name, label, current_scope, label_vars);
3638 return label;
3641 /* When we define a label, issue any appropriate warnings if there are
3642 any gotos earlier in the function which jump to this label. */
3644 static void
3645 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3647 unsigned int ix;
3648 struct c_goto_bindings *g;
3650 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3652 struct c_binding *b;
3653 struct c_scope *scope;
3655 /* We have a goto to this label. The goto is going forward. In
3656 g->scope, the goto is going to skip any binding which was
3657 defined after g->bindings_in_scope. */
3658 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3660 for (b = g->goto_bindings.scope->bindings;
3661 b != g->goto_bindings.bindings_in_scope;
3662 b = b->prev)
3664 if (decl_jump_unsafe (b->decl))
3665 warn_about_goto (g->loc, label, b->decl);
3669 /* We also need to warn about decls defined in any scopes
3670 between the scope of the label and the scope of the goto. */
3671 for (scope = label_vars->label_bindings.scope;
3672 scope != g->goto_bindings.scope;
3673 scope = scope->outer)
3675 gcc_assert (scope != NULL);
3676 if (scope->has_jump_unsafe_decl)
3678 if (scope == label_vars->label_bindings.scope)
3679 b = label_vars->label_bindings.bindings_in_scope;
3680 else
3681 b = scope->bindings;
3682 for (; b != NULL; b = b->prev)
3684 if (decl_jump_unsafe (b->decl))
3685 warn_about_goto (g->loc, label, b->decl);
3690 if (g->goto_bindings.stmt_exprs > 0)
3692 error_at (g->loc, "jump into statement expression");
3693 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3694 label);
3698 /* Now that the label is defined, we will issue warnings about
3699 subsequent gotos to this label when we see them. */
3700 vec_safe_truncate (label_vars->gotos, 0);
3701 label_vars->gotos = NULL;
3704 /* Define a label, specifying the location in the source file.
3705 Return the LABEL_DECL node for the label, if the definition is valid.
3706 Otherwise return 0. */
3708 tree
3709 define_label (location_t location, tree name)
3711 /* Find any preexisting label with this name. It is an error
3712 if that label has already been defined in this function, or
3713 if there is a containing function with a declared label with
3714 the same name. */
3715 tree label = I_LABEL_DECL (name);
3717 if (label
3718 && ((DECL_CONTEXT (label) == current_function_decl
3719 && DECL_INITIAL (label) != 0)
3720 || (DECL_CONTEXT (label) != current_function_decl
3721 && C_DECLARED_LABEL_FLAG (label))))
3723 error_at (location, "duplicate label %qD", label);
3724 locate_old_decl (label);
3725 return 0;
3727 else if (label && DECL_CONTEXT (label) == current_function_decl)
3729 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3731 /* The label has been used or declared already in this function,
3732 but not defined. Update its location to point to this
3733 definition. */
3734 DECL_SOURCE_LOCATION (label) = location;
3735 set_spot_bindings (&label_vars->label_bindings, true);
3737 /* Issue warnings as required about any goto statements from
3738 earlier in the function. */
3739 check_earlier_gotos (label, label_vars);
3741 else
3743 struct c_label_vars *label_vars;
3745 /* No label binding for that identifier; make one. */
3746 label = make_label (location, name, true, &label_vars);
3748 /* Ordinary labels go in the current function scope. */
3749 bind_label (name, label, current_function_scope, label_vars);
3752 if (!in_system_header_at (input_location) && lookup_name (name))
3753 warning_at (location, OPT_Wtraditional,
3754 "traditional C lacks a separate namespace "
3755 "for labels, identifier %qE conflicts", name);
3757 /* Mark label as having been defined. */
3758 DECL_INITIAL (label) = error_mark_node;
3759 return label;
3762 /* Get the bindings for a new switch statement. This is used to issue
3763 warnings as appropriate for jumps from the switch to case or
3764 default labels. */
3766 struct c_spot_bindings *
3767 c_get_switch_bindings (void)
3769 struct c_spot_bindings *switch_bindings;
3771 switch_bindings = XNEW (struct c_spot_bindings);
3772 set_spot_bindings (switch_bindings, true);
3773 return switch_bindings;
3776 void
3777 c_release_switch_bindings (struct c_spot_bindings *bindings)
3779 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3780 XDELETE (bindings);
3783 /* This is called at the point of a case or default label to issue
3784 warnings about decls as needed. It returns true if it found an
3785 error, not just a warning. */
3787 bool
3788 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3789 location_t switch_loc, location_t case_loc)
3791 bool saw_error;
3792 struct c_scope *scope;
3794 saw_error = false;
3795 for (scope = current_scope;
3796 scope != switch_bindings->scope;
3797 scope = scope->outer)
3799 struct c_binding *b;
3801 gcc_assert (scope != NULL);
3803 if (!scope->has_jump_unsafe_decl)
3804 continue;
3806 for (b = scope->bindings; b != NULL; b = b->prev)
3808 if (decl_jump_unsafe (b->decl))
3810 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3812 saw_error = true;
3813 error_at (case_loc,
3814 ("switch jumps into scope of identifier with "
3815 "variably modified type"));
3817 else
3818 warning_at (case_loc, OPT_Wjump_misses_init,
3819 "switch jumps over variable initialization");
3820 inform (switch_loc, "switch starts here");
3821 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3822 b->decl);
3827 if (switch_bindings->stmt_exprs > 0)
3829 saw_error = true;
3830 error_at (case_loc, "switch jumps into statement expression");
3831 inform (switch_loc, "switch starts here");
3834 return saw_error;
3837 /* Given NAME, an IDENTIFIER_NODE,
3838 return the structure (or union or enum) definition for that name.
3839 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3840 CODE says which kind of type the caller wants;
3841 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3842 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3843 location where the tag was defined.
3844 If the wrong kind of type is found, an error is reported. */
3846 static tree
3847 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3848 location_t *ploc)
3850 struct c_binding *b = I_TAG_BINDING (name);
3851 int thislevel = 0;
3853 if (!b || !b->decl)
3854 return 0;
3856 /* We only care about whether it's in this level if
3857 thislevel_only was set or it might be a type clash. */
3858 if (thislevel_only || TREE_CODE (b->decl) != code)
3860 /* For our purposes, a tag in the external scope is the same as
3861 a tag in the file scope. (Primarily relevant to Objective-C
3862 and its builtin structure tags, which get pushed before the
3863 file scope is created.) */
3864 if (B_IN_CURRENT_SCOPE (b)
3865 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3866 thislevel = 1;
3869 if (thislevel_only && !thislevel)
3870 return 0;
3872 if (TREE_CODE (b->decl) != code)
3874 /* Definition isn't the kind we were looking for. */
3875 pending_invalid_xref = name;
3876 pending_invalid_xref_location = input_location;
3878 /* If in the same binding level as a declaration as a tag
3879 of a different type, this must not be allowed to
3880 shadow that tag, so give the error immediately.
3881 (For example, "struct foo; union foo;" is invalid.) */
3882 if (thislevel)
3883 pending_xref_error ();
3886 if (ploc != NULL)
3887 *ploc = b->locus;
3889 return b->decl;
3892 /* Print an error message now
3893 for a recent invalid struct, union or enum cross reference.
3894 We don't print them immediately because they are not invalid
3895 when used in the `struct foo;' construct for shadowing. */
3897 void
3898 pending_xref_error (void)
3900 if (pending_invalid_xref != 0)
3901 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3902 pending_invalid_xref);
3903 pending_invalid_xref = 0;
3907 /* Look up NAME in the current scope and its superiors
3908 in the namespace of variables, functions and typedefs.
3909 Return a ..._DECL node of some kind representing its definition,
3910 or return 0 if it is undefined. */
3912 tree
3913 lookup_name (tree name)
3915 struct c_binding *b = I_SYMBOL_BINDING (name);
3916 if (b && !b->invisible)
3918 maybe_record_typedef_use (b->decl);
3919 return b->decl;
3921 return 0;
3924 /* Similar to `lookup_name' but look only at the indicated scope. */
3926 static tree
3927 lookup_name_in_scope (tree name, struct c_scope *scope)
3929 struct c_binding *b;
3931 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3932 if (B_IN_SCOPE (b, scope))
3933 return b->decl;
3934 return 0;
3937 /* Create the predefined scalar types of C,
3938 and some nodes representing standard constants (0, 1, (void *) 0).
3939 Initialize the global scope.
3940 Make definitions for built-in primitive functions. */
3942 void
3943 c_init_decl_processing (void)
3945 location_t save_loc = input_location;
3947 /* Initialize reserved words for parser. */
3948 c_parse_init ();
3950 current_function_decl = 0;
3952 gcc_obstack_init (&parser_obstack);
3954 /* Make the externals scope. */
3955 push_scope ();
3956 external_scope = current_scope;
3958 /* Declarations from c_common_nodes_and_builtins must not be associated
3959 with this input file, lest we get differences between using and not
3960 using preprocessed headers. */
3961 input_location = BUILTINS_LOCATION;
3963 c_common_nodes_and_builtins ();
3965 /* In C, comparisons and TRUTH_* expressions have type int. */
3966 truthvalue_type_node = integer_type_node;
3967 truthvalue_true_node = integer_one_node;
3968 truthvalue_false_node = integer_zero_node;
3970 /* Even in C99, which has a real boolean type. */
3971 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3972 boolean_type_node));
3974 input_location = save_loc;
3976 make_fname_decl = c_make_fname_decl;
3977 start_fname_decls ();
3980 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3981 give the decl, NAME is the initialization string and TYPE_DEP
3982 indicates whether NAME depended on the type of the function. As we
3983 don't yet implement delayed emission of static data, we mark the
3984 decl as emitted so it is not placed in the output. Anything using
3985 it must therefore pull out the STRING_CST initializer directly.
3986 FIXME. */
3988 static tree
3989 c_make_fname_decl (location_t loc, tree id, int type_dep)
3991 const char *name = fname_as_string (type_dep);
3992 tree decl, type, init;
3993 size_t length = strlen (name);
3995 type = build_array_type (char_type_node,
3996 build_index_type (size_int (length)));
3997 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3999 decl = build_decl (loc, VAR_DECL, id, type);
4001 TREE_STATIC (decl) = 1;
4002 TREE_READONLY (decl) = 1;
4003 DECL_ARTIFICIAL (decl) = 1;
4005 init = build_string (length + 1, name);
4006 free (CONST_CAST (char *, name));
4007 TREE_TYPE (init) = type;
4008 DECL_INITIAL (decl) = init;
4010 TREE_USED (decl) = 1;
4012 if (current_function_decl
4013 /* For invalid programs like this:
4015 void foo()
4016 const char* p = __FUNCTION__;
4018 the __FUNCTION__ is believed to appear in K&R style function
4019 parameter declarator. In that case we still don't have
4020 function_scope. */
4021 && (!seen_error () || current_function_scope))
4023 DECL_CONTEXT (decl) = current_function_decl;
4024 bind (id, decl, current_function_scope,
4025 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4028 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4030 return decl;
4033 tree
4034 c_builtin_function (tree decl)
4036 tree type = TREE_TYPE (decl);
4037 tree id = DECL_NAME (decl);
4039 const char *name = IDENTIFIER_POINTER (id);
4040 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4042 /* Should never be called on a symbol with a preexisting meaning. */
4043 gcc_assert (!I_SYMBOL_BINDING (id));
4045 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4046 UNKNOWN_LOCATION);
4048 /* Builtins in the implementation namespace are made visible without
4049 needing to be explicitly declared. See push_file_scope. */
4050 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4052 DECL_CHAIN (decl) = visible_builtins;
4053 visible_builtins = decl;
4056 return decl;
4059 tree
4060 c_builtin_function_ext_scope (tree decl)
4062 tree type = TREE_TYPE (decl);
4063 tree id = DECL_NAME (decl);
4065 const char *name = IDENTIFIER_POINTER (id);
4066 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4068 if (external_scope)
4069 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4070 UNKNOWN_LOCATION);
4072 /* Builtins in the implementation namespace are made visible without
4073 needing to be explicitly declared. See push_file_scope. */
4074 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4076 DECL_CHAIN (decl) = visible_builtins;
4077 visible_builtins = decl;
4080 return decl;
4083 /* Called when a declaration is seen that contains no names to declare.
4084 If its type is a reference to a structure, union or enum inherited
4085 from a containing scope, shadow that tag name for the current scope
4086 with a forward reference.
4087 If its type defines a new named structure or union
4088 or defines an enum, it is valid but we need not do anything here.
4089 Otherwise, it is an error. */
4091 void
4092 shadow_tag (const struct c_declspecs *declspecs)
4094 shadow_tag_warned (declspecs, 0);
4097 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4098 but no pedwarn. */
4099 void
4100 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4102 bool found_tag = false;
4104 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4106 tree value = declspecs->type;
4107 enum tree_code code = TREE_CODE (value);
4109 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4110 /* Used to test also that TYPE_SIZE (value) != 0.
4111 That caused warning for `struct foo;' at top level in the file. */
4113 tree name = TYPE_NAME (value);
4114 tree t;
4116 found_tag = true;
4118 if (declspecs->restrict_p)
4120 error ("invalid use of %<restrict%>");
4121 warned = 1;
4124 if (name == 0)
4126 if (warned != 1 && code != ENUMERAL_TYPE)
4127 /* Empty unnamed enum OK */
4129 pedwarn (input_location, 0,
4130 "unnamed struct/union that defines no instances");
4131 warned = 1;
4134 else if (declspecs->typespec_kind != ctsk_tagdef
4135 && declspecs->typespec_kind != ctsk_tagfirstref
4136 && declspecs->storage_class != csc_none)
4138 if (warned != 1)
4139 pedwarn (input_location, 0,
4140 "empty declaration with storage class specifier "
4141 "does not redeclare tag");
4142 warned = 1;
4143 pending_xref_error ();
4145 else if (declspecs->typespec_kind != ctsk_tagdef
4146 && declspecs->typespec_kind != ctsk_tagfirstref
4147 && (declspecs->const_p
4148 || declspecs->volatile_p
4149 || declspecs->atomic_p
4150 || declspecs->restrict_p
4151 || declspecs->address_space))
4153 if (warned != 1)
4154 pedwarn (input_location, 0,
4155 "empty declaration with type qualifier "
4156 "does not redeclare tag");
4157 warned = 1;
4158 pending_xref_error ();
4160 else if (declspecs->typespec_kind != ctsk_tagdef
4161 && declspecs->typespec_kind != ctsk_tagfirstref
4162 && declspecs->alignas_p)
4164 if (warned != 1)
4165 pedwarn (input_location, 0,
4166 "empty declaration with %<_Alignas%> "
4167 "does not redeclare tag");
4168 warned = 1;
4169 pending_xref_error ();
4171 else
4173 pending_invalid_xref = 0;
4174 t = lookup_tag (code, name, 1, NULL);
4176 if (t == 0)
4178 t = make_node (code);
4179 pushtag (input_location, name, t);
4183 else
4185 if (warned != 1 && !in_system_header_at (input_location))
4187 pedwarn (input_location, 0,
4188 "useless type name in empty declaration");
4189 warned = 1;
4193 else if (warned != 1 && !in_system_header_at (input_location)
4194 && declspecs->typedef_p)
4196 pedwarn (input_location, 0, "useless type name in empty declaration");
4197 warned = 1;
4200 pending_invalid_xref = 0;
4202 if (declspecs->inline_p)
4204 error ("%<inline%> in empty declaration");
4205 warned = 1;
4208 if (declspecs->noreturn_p)
4210 error ("%<_Noreturn%> in empty declaration");
4211 warned = 1;
4214 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4216 error ("%<auto%> in file-scope empty declaration");
4217 warned = 1;
4220 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4222 error ("%<register%> in file-scope empty declaration");
4223 warned = 1;
4226 if (!warned && !in_system_header_at (input_location)
4227 && declspecs->storage_class != csc_none)
4229 warning (0, "useless storage class specifier in empty declaration");
4230 warned = 2;
4233 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4235 warning (0, "useless %qs in empty declaration",
4236 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4237 warned = 2;
4240 if (!warned
4241 && !in_system_header_at (input_location)
4242 && (declspecs->const_p
4243 || declspecs->volatile_p
4244 || declspecs->atomic_p
4245 || declspecs->restrict_p
4246 || declspecs->address_space))
4248 warning (0, "useless type qualifier in empty declaration");
4249 warned = 2;
4252 if (!warned && !in_system_header_at (input_location)
4253 && declspecs->alignas_p)
4255 warning (0, "useless %<_Alignas%> in empty declaration");
4256 warned = 2;
4259 if (warned != 1)
4261 if (!found_tag)
4262 pedwarn (input_location, 0, "empty declaration");
4267 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4268 bits. SPECS represents declaration specifiers that the grammar
4269 only permits to contain type qualifiers and attributes. */
4272 quals_from_declspecs (const struct c_declspecs *specs)
4274 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4275 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4276 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4277 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4278 | (specs->shared_p ? TYPE_QUAL_UPC_SHARED : 0)
4279 | (specs->strict_p ? TYPE_QUAL_UPC_STRICT : 0)
4280 | (specs->relaxed_p ? TYPE_QUAL_UPC_RELAXED : 0)
4281 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4282 gcc_assert (!specs->type
4283 && !specs->decl_attr
4284 && specs->typespec_word == cts_none
4285 && specs->storage_class == csc_none
4286 && !specs->typedef_p
4287 && !specs->explicit_signed_p
4288 && !specs->deprecated_p
4289 && !specs->long_p
4290 && !specs->long_long_p
4291 && !specs->short_p
4292 && !specs->signed_p
4293 && !specs->unsigned_p
4294 && !specs->complex_p
4295 && !specs->inline_p
4296 && !specs->noreturn_p
4297 && !specs->thread_p);
4298 return quals;
4301 /* Construct an array declarator. LOC is the location of the
4302 beginning of the array (usually the opening brace). EXPR is the
4303 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4304 inside the [] (to be applied to the pointer to which a parameter
4305 array is converted). STATIC_P is true if "static" is inside the
4306 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4307 VLA of unspecified length which is nevertheless a complete type,
4308 false otherwise. The field for the contained declarator is left to
4309 be filled in by set_array_declarator_inner. */
4311 struct c_declarator *
4312 build_array_declarator (location_t loc,
4313 tree expr, struct c_declspecs *quals, bool static_p,
4314 bool vla_unspec_p)
4316 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4317 struct c_declarator);
4318 declarator->id_loc = loc;
4319 declarator->kind = cdk_array;
4320 declarator->declarator = 0;
4321 declarator->u.array.dimen = expr;
4322 if (quals)
4324 declarator->u.array.attrs = quals->attrs;
4325 declarator->u.array.quals = quals_from_declspecs (quals);
4327 else
4329 declarator->u.array.attrs = NULL_TREE;
4330 declarator->u.array.quals = 0;
4332 declarator->u.array.static_p = static_p;
4333 declarator->u.array.vla_unspec_p = vla_unspec_p;
4334 if (static_p || quals != NULL)
4335 pedwarn_c90 (loc, OPT_Wpedantic,
4336 "ISO C90 does not support %<static%> or type "
4337 "qualifiers in parameter array declarators");
4338 if (vla_unspec_p)
4339 pedwarn_c90 (loc, OPT_Wpedantic,
4340 "ISO C90 does not support %<[*]%> array declarators");
4341 if (vla_unspec_p)
4343 if (!current_scope->parm_flag)
4345 /* C99 6.7.5.2p4 */
4346 error_at (loc, "%<[*]%> not allowed in other than "
4347 "function prototype scope");
4348 declarator->u.array.vla_unspec_p = false;
4349 return NULL;
4351 current_scope->had_vla_unspec = true;
4353 return declarator;
4356 /* Set the contained declarator of an array declarator. DECL is the
4357 declarator, as constructed by build_array_declarator; INNER is what
4358 appears on the left of the []. */
4360 struct c_declarator *
4361 set_array_declarator_inner (struct c_declarator *decl,
4362 struct c_declarator *inner)
4364 decl->declarator = inner;
4365 return decl;
4368 /* INIT is a constructor that forms DECL's initializer. If the final
4369 element initializes a flexible array field, add the size of that
4370 initializer to DECL's size. */
4372 static void
4373 add_flexible_array_elts_to_size (tree decl, tree init)
4375 tree elt, type;
4377 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4378 return;
4380 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4381 type = TREE_TYPE (elt);
4382 if (TREE_CODE (type) == ARRAY_TYPE
4383 && TYPE_SIZE (type) == NULL_TREE
4384 && TYPE_DOMAIN (type) != NULL_TREE
4385 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4387 complete_array_type (&type, elt, false);
4388 DECL_SIZE (decl)
4389 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4390 DECL_SIZE_UNIT (decl)
4391 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4395 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4396 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4397 before the type name, and set *EXPR_CONST_OPERANDS, if
4398 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4399 appear in a constant expression. */
4401 tree
4402 groktypename (struct c_type_name *type_name, tree *expr,
4403 bool *expr_const_operands)
4405 tree type;
4406 tree attrs = type_name->specs->attrs;
4408 type_name->specs->attrs = NULL_TREE;
4410 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4411 false, NULL, &attrs, expr, expr_const_operands,
4412 DEPRECATED_NORMAL);
4414 /* Apply attributes. */
4415 decl_attributes (&type, attrs, 0);
4417 return type;
4420 /* Wrapper for decl_attributes that adds some implicit attributes
4421 to VAR_DECLs or FUNCTION_DECLs. */
4423 static tree
4424 c_decl_attributes (tree *node, tree attributes, int flags)
4426 /* Add implicit "omp declare target" attribute if requested. */
4427 if (current_omp_declare_target_attribute
4428 && ((TREE_CODE (*node) == VAR_DECL && TREE_STATIC (*node))
4429 || TREE_CODE (*node) == FUNCTION_DECL))
4431 if (TREE_CODE (*node) == VAR_DECL
4432 && ((DECL_CONTEXT (*node)
4433 && TREE_CODE (DECL_CONTEXT (*node)) == FUNCTION_DECL)
4434 || (current_function_decl && !DECL_EXTERNAL (*node))))
4435 error ("%q+D in block scope inside of declare target directive",
4436 *node);
4437 else if (TREE_CODE (*node) == VAR_DECL
4438 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4439 error ("%q+D in declare target directive does not have mappable type",
4440 *node);
4441 else
4442 attributes = tree_cons (get_identifier ("omp declare target"),
4443 NULL_TREE, attributes);
4445 return decl_attributes (node, attributes, flags);
4449 /* Decode a declarator in an ordinary declaration or data definition.
4450 This is called as soon as the type information and variable name
4451 have been parsed, before parsing the initializer if any.
4452 Here we create the ..._DECL node, fill in its type,
4453 and put it on the list of decls for the current context.
4454 The ..._DECL node is returned as the value.
4456 Exception: for arrays where the length is not specified,
4457 the type is left null, to be filled in by `finish_decl'.
4459 Function definitions do not come here; they go to start_function
4460 instead. However, external and forward declarations of functions
4461 do go through here. Structure field declarations are done by
4462 grokfield and not through here. */
4464 tree
4465 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4466 bool initialized, tree attributes)
4468 tree decl;
4469 tree tem;
4470 tree expr = NULL_TREE;
4471 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4473 /* An object declared as __attribute__((deprecated)) suppresses
4474 warnings of uses of other deprecated items. */
4475 if (lookup_attribute ("deprecated", attributes))
4476 deprecated_state = DEPRECATED_SUPPRESS;
4478 decl = grokdeclarator (declarator, declspecs,
4479 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4480 deprecated_state);
4481 if (!decl || decl == error_mark_node)
4482 return NULL_TREE;
4484 if (expr)
4485 add_stmt (fold_convert (void_type_node, expr));
4487 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4488 warning (OPT_Wmain, "%q+D is usually a function", decl);
4490 if (initialized)
4491 /* Is it valid for this decl to have an initializer at all?
4492 If not, set INITIALIZED to zero, which will indirectly
4493 tell 'finish_decl' to ignore the initializer once it is parsed. */
4494 switch (TREE_CODE (decl))
4496 case TYPE_DECL:
4497 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4498 initialized = 0;
4499 break;
4501 case FUNCTION_DECL:
4502 error ("function %qD is initialized like a variable", decl);
4503 initialized = 0;
4504 break;
4506 case PARM_DECL:
4507 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4508 error ("parameter %qD is initialized", decl);
4509 initialized = 0;
4510 break;
4512 default:
4513 /* Don't allow initializations for incomplete types except for
4514 arrays which might be completed by the initialization. */
4516 /* This can happen if the array size is an undefined macro.
4517 We already gave a warning, so we don't need another one. */
4518 if (TREE_TYPE (decl) == error_mark_node)
4519 initialized = 0;
4520 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4522 /* A complete type is ok if size is fixed. */
4524 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4525 || C_DECL_VARIABLE_SIZE (decl))
4527 error ("variable-sized object may not be initialized");
4528 initialized = 0;
4531 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4533 error ("variable %qD has initializer but incomplete type", decl);
4534 initialized = 0;
4536 else if (C_DECL_VARIABLE_SIZE (decl))
4538 /* Although C99 is unclear about whether incomplete arrays
4539 of VLAs themselves count as VLAs, it does not make
4540 sense to permit them to be initialized given that
4541 ordinary VLAs may not be initialized. */
4542 error ("variable-sized object may not be initialized");
4543 initialized = 0;
4547 if (initialized)
4549 if (current_scope == file_scope)
4550 TREE_STATIC (decl) = 1;
4552 /* Tell 'pushdecl' this is an initialized decl
4553 even though we don't yet have the initializer expression.
4554 Also tell 'finish_decl' it may store the real initializer. */
4555 DECL_INITIAL (decl) = error_mark_node;
4558 /* If this is a function declaration, write a record describing it to the
4559 prototypes file (if requested). */
4561 if (TREE_CODE (decl) == FUNCTION_DECL)
4562 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4564 /* ANSI specifies that a tentative definition which is not merged with
4565 a non-tentative definition behaves exactly like a definition with an
4566 initializer equal to zero. (Section 3.7.2)
4568 -fno-common gives strict ANSI behavior, though this tends to break
4569 a large body of code that grew up without this rule.
4571 Thread-local variables are never common, since there's no entrenched
4572 body of code to break, and it allows more efficient variable references
4573 in the presence of dynamic linking. */
4575 if (TREE_CODE (decl) == VAR_DECL
4576 && !initialized
4577 && TREE_PUBLIC (decl)
4578 && !DECL_THREAD_LOCAL_P (decl)
4579 && !flag_no_common)
4580 DECL_COMMON (decl) = 1;
4582 /* Set attributes here so if duplicate decl, will have proper attributes. */
4583 c_decl_attributes (&decl, attributes, 0);
4585 /* Handle gnu_inline attribute. */
4586 if (declspecs->inline_p
4587 && !flag_gnu89_inline
4588 && TREE_CODE (decl) == FUNCTION_DECL
4589 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4590 || current_function_decl))
4592 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4594 else if (declspecs->storage_class != csc_static)
4595 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4598 if (TREE_CODE (decl) == FUNCTION_DECL
4599 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4601 struct c_declarator *ce = declarator;
4603 if (ce->kind == cdk_pointer)
4604 ce = declarator->declarator;
4605 if (ce->kind == cdk_function)
4607 tree args = ce->u.arg_info->parms;
4608 for (; args; args = DECL_CHAIN (args))
4610 tree type = TREE_TYPE (args);
4611 if (type && INTEGRAL_TYPE_P (type)
4612 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4613 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4618 if (TREE_CODE (decl) == FUNCTION_DECL
4619 && DECL_DECLARED_INLINE_P (decl)
4620 && DECL_UNINLINABLE (decl)
4621 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4622 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4623 decl);
4625 /* C99 6.7.4p3: An inline definition of a function with external
4626 linkage shall not contain a definition of a modifiable object
4627 with static storage duration... */
4628 if (TREE_CODE (decl) == VAR_DECL
4629 && current_scope != file_scope
4630 && TREE_STATIC (decl)
4631 && !TREE_READONLY (decl)
4632 && DECL_DECLARED_INLINE_P (current_function_decl)
4633 && DECL_EXTERNAL (current_function_decl))
4634 record_inline_static (input_location, current_function_decl,
4635 decl, csi_modifiable);
4637 if (c_dialect_objc ()
4638 && (TREE_CODE (decl) == VAR_DECL
4639 || TREE_CODE (decl) == FUNCTION_DECL))
4640 objc_check_global_decl (decl);
4642 /* Add this decl to the current scope.
4643 TEM may equal DECL or it may be a previous decl of the same name. */
4644 tem = pushdecl (decl);
4646 if (initialized && DECL_EXTERNAL (tem))
4648 DECL_EXTERNAL (tem) = 0;
4649 TREE_STATIC (tem) = 1;
4652 return tem;
4655 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4656 DECL or the non-array element type if DECL is an uninitialized array.
4657 If that type has a const member, diagnose this. */
4659 static void
4660 diagnose_uninitialized_cst_member (tree decl, tree type)
4662 tree field;
4663 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4665 tree field_type;
4666 if (TREE_CODE (field) != FIELD_DECL)
4667 continue;
4668 field_type = strip_array_types (TREE_TYPE (field));
4670 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4672 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4673 "uninitialized const member in %qT is invalid in C++",
4674 strip_array_types (TREE_TYPE (decl)));
4675 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4678 if (TREE_CODE (field_type) == RECORD_TYPE
4679 || TREE_CODE (field_type) == UNION_TYPE)
4680 diagnose_uninitialized_cst_member (decl, field_type);
4684 /* Finish processing of a declaration;
4685 install its initial value.
4686 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4687 If the length of an array type is not known before,
4688 it must be determined now, from the initial value, or it is an error.
4690 INIT_LOC is the location of the initial value. */
4692 void
4693 finish_decl (tree decl, location_t init_loc, tree init,
4694 tree origtype, tree asmspec_tree)
4696 tree type;
4697 bool was_incomplete = (DECL_SIZE (decl) == 0);
4698 const char *asmspec = 0;
4700 /* If a name was specified, get the string. */
4701 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4702 && DECL_FILE_SCOPE_P (decl))
4703 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4704 if (asmspec_tree)
4705 asmspec = TREE_STRING_POINTER (asmspec_tree);
4707 if (TREE_CODE (decl) == VAR_DECL
4708 && TREE_STATIC (decl)
4709 && global_bindings_p ())
4710 /* So decl is a global variable. Record the types it uses
4711 so that we can decide later to emit debug info for them. */
4712 record_types_used_by_current_var_decl (decl);
4714 /* If `start_decl' didn't like having an initialization, ignore it now. */
4715 if (init != 0 && DECL_INITIAL (decl) == 0)
4716 init = 0;
4718 /* Don't crash if parm is initialized. */
4719 if (TREE_CODE (decl) == PARM_DECL)
4720 init = 0;
4722 if (init)
4723 store_init_value (init_loc, decl, init, origtype);
4725 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4726 || TREE_CODE (decl) == FUNCTION_DECL
4727 || TREE_CODE (decl) == FIELD_DECL))
4728 objc_check_decl (decl);
4730 type = TREE_TYPE (decl);
4732 /* Deduce size of array from initialization, if not already known. */
4733 if (TREE_CODE (type) == ARRAY_TYPE
4734 && TYPE_DOMAIN (type) == 0
4735 && TREE_CODE (decl) != TYPE_DECL)
4737 bool do_default
4738 = (TREE_STATIC (decl)
4739 /* Even if pedantic, an external linkage array
4740 may have incomplete type at first. */
4741 ? pedantic && !TREE_PUBLIC (decl)
4742 : !DECL_EXTERNAL (decl));
4743 int failure
4744 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4745 do_default);
4747 /* Get the completed type made by complete_array_type. */
4748 type = TREE_TYPE (decl);
4750 switch (failure)
4752 case 1:
4753 error ("initializer fails to determine size of %q+D", decl);
4754 break;
4756 case 2:
4757 if (do_default)
4758 error ("array size missing in %q+D", decl);
4759 /* If a `static' var's size isn't known,
4760 make it extern as well as static, so it does not get
4761 allocated.
4762 If it is not `static', then do not mark extern;
4763 finish_incomplete_decl will give it a default size
4764 and it will get allocated. */
4765 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4766 DECL_EXTERNAL (decl) = 1;
4767 break;
4769 case 3:
4770 error ("zero or negative size array %q+D", decl);
4771 break;
4773 case 0:
4774 /* For global variables, update the copy of the type that
4775 exists in the binding. */
4776 if (TREE_PUBLIC (decl))
4778 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4779 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4780 b_ext = b_ext->shadowed;
4781 if (b_ext)
4783 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4784 b_ext->u.type = composite_type (b_ext->u.type, type);
4785 else
4786 b_ext->u.type = type;
4789 break;
4791 default:
4792 gcc_unreachable ();
4795 if (DECL_INITIAL (decl))
4796 TREE_TYPE (DECL_INITIAL (decl)) = type;
4798 relayout_decl (decl);
4801 if (TREE_CODE (decl) == VAR_DECL)
4803 if (init && TREE_CODE (init) == CONSTRUCTOR)
4804 add_flexible_array_elts_to_size (decl, init);
4806 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4807 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4808 layout_decl (decl, 0);
4810 if (DECL_SIZE (decl) == 0
4811 /* Don't give an error if we already gave one earlier. */
4812 && TREE_TYPE (decl) != error_mark_node
4813 && (TREE_STATIC (decl)
4814 /* A static variable with an incomplete type
4815 is an error if it is initialized.
4816 Also if it is not file scope.
4817 Otherwise, let it through, but if it is not `extern'
4818 then it may cause an error message later. */
4819 ? (DECL_INITIAL (decl) != 0
4820 || !DECL_FILE_SCOPE_P (decl))
4821 /* An automatic variable with an incomplete type
4822 is an error. */
4823 : !DECL_EXTERNAL (decl)))
4825 error ("storage size of %q+D isn%'t known", decl);
4826 TREE_TYPE (decl) = error_mark_node;
4829 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4830 && DECL_SIZE (decl) != 0)
4832 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4833 constant_expression_warning (DECL_SIZE (decl));
4834 else
4836 if (upc_shared_type_p (TREE_TYPE (decl)))
4838 gcc_assert (!flag_upc_threads);
4839 if (TYPE_HAS_THREADS_FACTOR (TREE_TYPE (decl)))
4840 error ("in the UPC dynamic translation environment, " "THREADS may not appear in declarations "
4841 "of shared arrays with indefinite block size; "
4842 "the storage size of %q+D cannot be calculated",
4843 decl);
4844 else
4845 error ("in the UPC dynamic translation environment, "
4846 "THREADS must appear exactly once in "
4847 "declarations of shared arrays; "
4848 "the storage size of %q+D cannot be calculated",
4849 decl);
4851 else
4852 error ("storage size of %q+D isn%'t constant", decl);
4853 TREE_TYPE (decl) = error_mark_node;
4857 if (TREE_USED (type))
4859 TREE_USED (decl) = 1;
4860 DECL_READ_P (decl) = 1;
4864 /* If this is a function and an assembler name is specified, reset DECL_RTL
4865 so we can give it its new name. Also, update builtin_decl if it
4866 was a normal built-in. */
4867 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4869 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4870 set_builtin_user_assembler_name (decl, asmspec);
4871 set_user_assembler_name (decl, asmspec);
4874 /* If #pragma weak was used, mark the decl weak now. */
4875 maybe_apply_pragma_weak (decl);
4877 /* Output the assembler code and/or RTL code for variables and functions,
4878 unless the type is an undefined structure or union.
4879 If not, it will get done when the type is completed. */
4881 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4883 /* Determine the ELF visibility. */
4884 if (TREE_PUBLIC (decl))
4885 c_determine_visibility (decl);
4887 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4888 if (c_dialect_objc ())
4889 objc_check_decl (decl);
4891 /* Give UPC a chance to check the declaration. */
4892 if (flag_upc)
4893 upc_check_decl (decl);
4895 if (asmspec)
4897 /* If this is not a static variable, issue a warning.
4898 It doesn't make any sense to give an ASMSPEC for an
4899 ordinary, non-register local variable. Historically,
4900 GCC has accepted -- but ignored -- the ASMSPEC in
4901 this case. */
4902 if (!DECL_FILE_SCOPE_P (decl)
4903 && TREE_CODE (decl) == VAR_DECL
4904 && !C_DECL_REGISTER (decl)
4905 && !TREE_STATIC (decl))
4906 warning (0, "ignoring asm-specifier for non-static local "
4907 "variable %q+D", decl);
4908 else
4909 set_user_assembler_name (decl, asmspec);
4912 if (DECL_FILE_SCOPE_P (decl))
4914 if (DECL_INITIAL (decl) == NULL_TREE
4915 || DECL_INITIAL (decl) == error_mark_node)
4916 /* Don't output anything
4917 when a tentative file-scope definition is seen.
4918 But at end of compilation, do output code for them. */
4919 DECL_DEFER_OUTPUT (decl) = 1;
4920 if (asmspec && C_DECL_REGISTER (decl))
4921 DECL_HARD_REGISTER (decl) = 1;
4922 rest_of_decl_compilation (decl, true, 0);
4924 else
4926 /* In conjunction with an ASMSPEC, the `register'
4927 keyword indicates that we should place the variable
4928 in a particular register. */
4929 if (asmspec && C_DECL_REGISTER (decl))
4931 DECL_HARD_REGISTER (decl) = 1;
4932 /* This cannot be done for a structure with volatile
4933 fields, on which DECL_REGISTER will have been
4934 reset. */
4935 if (!DECL_REGISTER (decl))
4936 error ("cannot put object with volatile field into register");
4939 if (TREE_CODE (decl) != FUNCTION_DECL)
4941 /* If we're building a variable sized type, and we might be
4942 reachable other than via the top of the current binding
4943 level, then create a new BIND_EXPR so that we deallocate
4944 the object at the right time. */
4945 /* Note that DECL_SIZE can be null due to errors. */
4946 if (DECL_SIZE (decl)
4947 && !TREE_CONSTANT (DECL_SIZE (decl))
4948 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4950 tree bind;
4951 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4952 TREE_SIDE_EFFECTS (bind) = 1;
4953 add_stmt (bind);
4954 BIND_EXPR_BODY (bind) = push_stmt_list ();
4956 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4957 DECL_EXPR, decl));
4962 if (!DECL_FILE_SCOPE_P (decl))
4964 /* Recompute the RTL of a local array now
4965 if it used to be an incomplete type. */
4966 if (was_incomplete
4967 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4969 /* If we used it already as memory, it must stay in memory. */
4970 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4971 /* If it's still incomplete now, no init will save it. */
4972 if (DECL_SIZE (decl) == 0)
4973 DECL_INITIAL (decl) = 0;
4978 if (TREE_CODE (decl) == TYPE_DECL)
4980 if (!DECL_FILE_SCOPE_P (decl)
4981 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4982 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4984 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4987 /* Install a cleanup (aka destructor) if one was given. */
4988 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4990 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4991 if (attr)
4993 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4994 tree cleanup_decl = lookup_name (cleanup_id);
4995 tree cleanup;
4996 vec<tree, va_gc> *v;
4998 /* Build "cleanup(&decl)" for the destructor. */
4999 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
5000 vec_alloc (v, 1);
5001 v->quick_push (cleanup);
5002 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5003 vNULL, cleanup_decl, v, NULL);
5004 vec_free (v);
5006 /* Don't warn about decl unused; the cleanup uses it. */
5007 TREE_USED (decl) = 1;
5008 TREE_USED (cleanup_decl) = 1;
5009 DECL_READ_P (decl) = 1;
5011 push_cleanup (decl, cleanup, false);
5015 if (warn_cxx_compat
5016 && TREE_CODE (decl) == VAR_DECL
5017 && !DECL_EXTERNAL (decl)
5018 && DECL_INITIAL (decl) == NULL_TREE)
5020 type = strip_array_types (type);
5021 if (TREE_READONLY (decl))
5022 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5023 "uninitialized const %qD is invalid in C++", decl);
5024 else if ((TREE_CODE (type) == RECORD_TYPE
5025 || TREE_CODE (type) == UNION_TYPE)
5026 && C_TYPE_FIELDS_READONLY (type))
5027 diagnose_uninitialized_cst_member (decl, type);
5030 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5033 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5034 EXPR is NULL or a pointer to an expression that needs to be
5035 evaluated for the side effects of array size expressions in the
5036 parameters. */
5038 tree
5039 grokparm (const struct c_parm *parm, tree *expr)
5041 tree attrs = parm->attrs;
5042 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5043 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5045 decl_attributes (&decl, attrs, 0);
5047 return decl;
5050 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5051 and push that on the current scope. EXPR is a pointer to an
5052 expression that needs to be evaluated for the side effects of array
5053 size expressions in the parameters. */
5055 void
5056 push_parm_decl (const struct c_parm *parm, tree *expr)
5058 tree attrs = parm->attrs;
5059 tree decl;
5061 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5062 &attrs, expr, NULL, DEPRECATED_NORMAL);
5063 decl_attributes (&decl, attrs, 0);
5065 decl = pushdecl (decl);
5067 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5070 /* Mark all the parameter declarations to date as forward decls.
5071 Also diagnose use of this extension. */
5073 void
5074 mark_forward_parm_decls (void)
5076 struct c_binding *b;
5078 if (pedantic && !current_scope->warned_forward_parm_decls)
5080 pedwarn (input_location, OPT_Wpedantic,
5081 "ISO C forbids forward parameter declarations");
5082 current_scope->warned_forward_parm_decls = true;
5085 for (b = current_scope->bindings; b; b = b->prev)
5086 if (TREE_CODE (b->decl) == PARM_DECL)
5087 TREE_ASM_WRITTEN (b->decl) = 1;
5090 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5091 literal, which may be an incomplete array type completed by the
5092 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5093 literal. NON_CONST is true if the initializers contain something
5094 that cannot occur in a constant expression. */
5096 tree
5097 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5099 /* We do not use start_decl here because we have a type, not a declarator;
5100 and do not use finish_decl because the decl should be stored inside
5101 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5102 tree decl;
5103 tree complit;
5104 tree stmt;
5106 if (type == error_mark_node
5107 || init == error_mark_node)
5108 return error_mark_node;
5110 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5111 DECL_EXTERNAL (decl) = 0;
5112 TREE_PUBLIC (decl) = 0;
5113 TREE_STATIC (decl) = (current_scope == file_scope);
5114 DECL_CONTEXT (decl) = current_function_decl;
5115 TREE_USED (decl) = 1;
5116 DECL_READ_P (decl) = 1;
5117 TREE_TYPE (decl) = type;
5118 TREE_READONLY (decl) = (TYPE_READONLY (type)
5119 || (TREE_CODE (type) == ARRAY_TYPE
5120 && TYPE_READONLY (TREE_TYPE (type))));
5121 store_init_value (loc, decl, init, NULL_TREE);
5123 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5125 int failure = complete_array_type (&TREE_TYPE (decl),
5126 DECL_INITIAL (decl), true);
5127 /* If complete_array_type returns 3, it means that the
5128 initial value of the compound literal is empty. Allow it. */
5129 gcc_assert (failure == 0 || failure == 3);
5131 type = TREE_TYPE (decl);
5132 TREE_TYPE (DECL_INITIAL (decl)) = type;
5135 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5137 c_incomplete_type_error (NULL_TREE, type);
5138 return error_mark_node;
5141 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5142 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5143 TREE_SIDE_EFFECTS (complit) = 1;
5145 layout_decl (decl, 0);
5147 if (TREE_STATIC (decl))
5149 /* This decl needs a name for the assembler output. */
5150 set_compound_literal_name (decl);
5151 DECL_DEFER_OUTPUT (decl) = 1;
5152 DECL_COMDAT (decl) = 1;
5153 DECL_ARTIFICIAL (decl) = 1;
5154 DECL_IGNORED_P (decl) = 1;
5155 pushdecl (decl);
5156 rest_of_decl_compilation (decl, 1, 0);
5159 if (non_const)
5161 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5162 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5165 return complit;
5168 /* Check the type of a compound literal. Here we just check that it
5169 is valid for C++. */
5171 void
5172 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5174 if (warn_cxx_compat
5175 && (type_name->specs->typespec_kind == ctsk_tagdef
5176 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5177 warning_at (loc, OPT_Wc___compat,
5178 "defining a type in a compound literal is invalid in C++");
5181 /* Determine whether TYPE is a structure with a flexible array member,
5182 or a union containing such a structure (possibly recursively). */
5184 static bool
5185 flexible_array_type_p (tree type)
5187 tree x;
5188 switch (TREE_CODE (type))
5190 case RECORD_TYPE:
5191 x = TYPE_FIELDS (type);
5192 if (x == NULL_TREE)
5193 return false;
5194 while (DECL_CHAIN (x) != NULL_TREE)
5195 x = DECL_CHAIN (x);
5196 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5197 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5198 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5199 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5200 return true;
5201 return false;
5202 case UNION_TYPE:
5203 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5205 if (flexible_array_type_p (TREE_TYPE (x)))
5206 return true;
5208 return false;
5209 default:
5210 return false;
5214 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5215 replacing with appropriate values if they are invalid. */
5216 static void
5217 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
5219 tree type_mv;
5220 unsigned int max_width;
5221 unsigned HOST_WIDE_INT w;
5222 const char *name = (orig_name
5223 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5224 : _("<anonymous>"));
5226 /* Detect and ignore out of range field width and process valid
5227 field widths. */
5228 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5230 error ("bit-field %qs width not an integer constant", name);
5231 *width = integer_one_node;
5233 else
5235 if (TREE_CODE (*width) != INTEGER_CST)
5237 *width = c_fully_fold (*width, false, NULL);
5238 if (TREE_CODE (*width) == INTEGER_CST)
5239 pedwarn (input_location, OPT_Wpedantic,
5240 "bit-field %qs width not an integer constant expression",
5241 name);
5243 if (TREE_CODE (*width) != INTEGER_CST)
5245 error ("bit-field %qs width not an integer constant", name);
5246 *width = integer_one_node;
5248 constant_expression_warning (*width);
5249 if (tree_int_cst_sgn (*width) < 0)
5251 error ("negative width in bit-field %qs", name);
5252 *width = integer_one_node;
5254 else if (integer_zerop (*width) && orig_name)
5256 error ("zero width for bit-field %qs", name);
5257 *width = integer_one_node;
5261 /* Detect invalid bit-field type. */
5262 if (TREE_CODE (*type) != INTEGER_TYPE
5263 && TREE_CODE (*type) != BOOLEAN_TYPE
5264 && TREE_CODE (*type) != ENUMERAL_TYPE)
5266 error ("bit-field %qs has invalid type", name);
5267 *type = unsigned_type_node;
5270 type_mv = TYPE_MAIN_VARIANT (*type);
5271 if (!in_system_header_at (input_location)
5272 && type_mv != integer_type_node
5273 && type_mv != unsigned_type_node
5274 && type_mv != boolean_type_node)
5275 pedwarn_c90 (input_location, OPT_Wpedantic,
5276 "type of bit-field %qs is a GCC extension", name);
5278 max_width = TYPE_PRECISION (*type);
5280 if (0 < compare_tree_int (*width, max_width))
5282 error ("width of %qs exceeds its type", name);
5283 w = max_width;
5284 *width = build_int_cst (integer_type_node, w);
5286 else
5287 w = tree_to_uhwi (*width);
5289 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5291 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5292 if (!lt
5293 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5294 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5295 warning (0, "%qs is narrower than values of its type", name);
5301 /* Print warning about variable length array if necessary. */
5303 static void
5304 warn_variable_length_array (tree name, tree size)
5306 if (TREE_CONSTANT (size))
5308 if (name)
5309 pedwarn_c90 (input_location, OPT_Wvla,
5310 "ISO C90 forbids array %qE whose size "
5311 "can%'t be evaluated", name);
5312 else
5313 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5314 "whose size can%'t be evaluated");
5316 else
5318 if (name)
5319 pedwarn_c90 (input_location, OPT_Wvla,
5320 "ISO C90 forbids variable length array %qE", name);
5321 else
5322 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5323 "length array");
5327 /* Print warning about defaulting to int if necessary. */
5329 static void
5330 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5332 diagnostic_info diagnostic;
5333 va_list ap;
5335 va_start (ap, gmsgid);
5336 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
5337 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5338 diagnostic.option_index = opt;
5339 report_diagnostic (&diagnostic);
5340 va_end (ap);
5343 /* Given declspecs and a declarator,
5344 determine the name and type of the object declared
5345 and construct a ..._DECL node for it.
5346 (In one case we can return a ..._TYPE node instead.
5347 For invalid input we sometimes return 0.)
5349 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5351 DECL_CONTEXT says which syntactic context this declaration is in:
5352 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5353 FUNCDEF for a function definition. Like NORMAL but a few different
5354 error messages in each case. Return value may be zero meaning
5355 this definition is too screwy to try to parse.
5356 PARM for a parameter declaration (either within a function prototype
5357 or before a function body). Make a PARM_DECL, or return void_type_node.
5358 TYPENAME if for a typename (in a cast or sizeof).
5359 Don't make a DECL node; just return the ..._TYPE node.
5360 FIELD for a struct or union field; make a FIELD_DECL.
5361 INITIALIZED is true if the decl has an initializer.
5362 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5363 representing the width of the bit-field.
5364 DECL_ATTRS points to the list of attributes that should be added to this
5365 decl. Any nested attributes that belong on the decl itself will be
5366 added to this list.
5367 If EXPR is not NULL, any expressions that need to be evaluated as
5368 part of evaluating variably modified types will be stored in *EXPR.
5369 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5370 set to indicate whether operands in *EXPR can be used in constant
5371 expressions.
5372 DEPRECATED_STATE is a deprecated_states value indicating whether
5373 deprecation warnings should be suppressed.
5375 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5376 It may also be so in the PARM case, for a prototype where the
5377 argument type is specified but not the name.
5379 This function is where the complicated C meanings of `static'
5380 and `extern' are interpreted. */
5382 static tree
5383 grokdeclarator (const struct c_declarator *declarator,
5384 struct c_declspecs *declspecs,
5385 enum decl_context decl_context, bool initialized, tree *width,
5386 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5387 enum deprecated_states deprecated_state)
5389 tree type = declspecs->type;
5390 bool threadp = declspecs->thread_p;
5391 enum c_storage_class storage_class = declspecs->storage_class;
5392 int constp;
5393 int restrictp;
5394 int volatilep;
5395 int atomicp;
5396 int sharedp;
5397 int strictp;
5398 int relaxedp;
5399 int type_quals = TYPE_UNQUALIFIED;
5400 tree name = NULL_TREE;
5401 bool funcdef_flag = false;
5402 bool funcdef_syntax = false;
5403 bool size_varies = false;
5404 tree decl_attr = declspecs->decl_attr;
5405 int array_ptr_quals = TYPE_UNQUALIFIED;
5406 tree array_ptr_attrs = NULL_TREE;
5407 int array_parm_static = 0;
5408 bool array_parm_vla_unspec_p = false;
5409 tree returned_attrs = NULL_TREE;
5410 int upc_threads_ref = 0; /* for static declarations of shared arrays */
5411 tree upc_layout_qualifier;
5412 tree upc_elem_block_factor;
5413 tree upc_block_factor = NULL;
5414 bool bitfield = width != NULL;
5415 tree element_type;
5416 struct c_arg_info *arg_info = 0;
5417 addr_space_t as1, as2, address_space;
5418 location_t loc = UNKNOWN_LOCATION;
5419 const char *errmsg;
5420 tree expr_dummy;
5421 bool expr_const_operands_dummy;
5422 enum c_declarator_kind first_non_attr_kind;
5423 unsigned int alignas_align = 0;
5425 if (TREE_CODE (type) == ERROR_MARK)
5426 return error_mark_node;
5427 if (expr == NULL)
5428 expr = &expr_dummy;
5429 if (expr_const_operands == NULL)
5430 expr_const_operands = &expr_const_operands_dummy;
5432 *expr = declspecs->expr;
5433 *expr_const_operands = declspecs->expr_const_operands;
5435 if (decl_context == FUNCDEF)
5436 funcdef_flag = true, decl_context = NORMAL;
5438 /* Look inside a declarator for the name being declared
5439 and get it as an IDENTIFIER_NODE, for an error message. */
5441 const struct c_declarator *decl = declarator;
5443 first_non_attr_kind = cdk_attrs;
5444 while (decl)
5445 switch (decl->kind)
5447 case cdk_array:
5448 loc = decl->id_loc;
5449 /* FALL THRU. */
5451 case cdk_function:
5452 case cdk_pointer:
5453 funcdef_syntax = (decl->kind == cdk_function);
5454 decl = decl->declarator;
5455 if (first_non_attr_kind == cdk_attrs)
5456 first_non_attr_kind = decl->kind;
5457 break;
5459 case cdk_attrs:
5460 decl = decl->declarator;
5461 break;
5463 case cdk_id:
5464 loc = decl->id_loc;
5465 if (decl->u.id)
5466 name = decl->u.id;
5467 if (first_non_attr_kind == cdk_attrs)
5468 first_non_attr_kind = decl->kind;
5469 decl = 0;
5470 break;
5472 default:
5473 gcc_unreachable ();
5475 if (name == 0)
5477 gcc_assert (decl_context == PARM
5478 || decl_context == TYPENAME
5479 || (decl_context == FIELD
5480 && declarator->kind == cdk_id));
5481 gcc_assert (!initialized);
5485 /* A function definition's declarator must have the form of
5486 a function declarator. */
5488 if (funcdef_flag && !funcdef_syntax)
5489 return 0;
5491 /* If this looks like a function definition, make it one,
5492 even if it occurs where parms are expected.
5493 Then store_parm_decls will reject it and not use it as a parm. */
5494 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5495 decl_context = PARM;
5497 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5498 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5500 if ((decl_context == NORMAL || decl_context == FIELD)
5501 && current_scope == file_scope
5502 && variably_modified_type_p (type, NULL_TREE))
5504 if (name)
5505 error_at (loc, "variably modified %qE at file scope", name);
5506 else
5507 error_at (loc, "variably modified field at file scope");
5508 type = integer_type_node;
5511 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5513 upc_threads_ref = TYPE_HAS_THREADS_FACTOR (type);
5515 /* Diagnose defaulting to "int". */
5517 if (declspecs->default_int_p && !in_system_header_at (input_location))
5519 /* Issue a warning if this is an ISO C 99 program or if
5520 -Wreturn-type and this is a function, or if -Wimplicit;
5521 prefer the former warning since it is more explicit. */
5522 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5523 && funcdef_flag)
5524 warn_about_return_type = 1;
5525 else
5527 if (name)
5528 warn_defaults_to (loc, OPT_Wimplicit_int,
5529 "type defaults to %<int%> in declaration "
5530 "of %qE", name);
5531 else
5532 warn_defaults_to (loc, OPT_Wimplicit_int,
5533 "type defaults to %<int%> in type name");
5537 /* Adjust the type if a bit-field is being declared,
5538 -funsigned-bitfields applied and the type is not explicitly
5539 "signed". */
5540 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5541 && TREE_CODE (type) == INTEGER_TYPE)
5542 type = unsigned_type_for (type);
5544 /* Figure out the type qualifiers for the declaration. There are
5545 two ways a declaration can become qualified. One is something
5546 like `const int i' where the `const' is explicit. Another is
5547 something like `typedef const int CI; CI i' where the type of the
5548 declaration contains the `const'. A third possibility is that
5549 there is a type qualifier on the element type of a typedefed
5550 array type, in which case we should extract that qualifier so
5551 that c_apply_type_quals_to_decl receives the full list of
5552 qualifiers to work with (C90 is not entirely clear about whether
5553 duplicate qualifiers should be diagnosed in this case, but it
5554 seems most appropriate to do so). */
5555 element_type = strip_array_types (type);
5556 constp = declspecs->const_p + TYPE_READONLY (element_type);
5557 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5558 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5559 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5560 sharedp = declspecs->shared_p + upc_shared_type_p (element_type);
5561 strictp = declspecs->strict_p + TYPE_UPC_STRICT (element_type);
5562 relaxedp = declspecs->relaxed_p + TYPE_UPC_RELAXED (element_type);
5563 upc_elem_block_factor = TYPE_UPC_BLOCK_FACTOR (element_type);
5564 upc_layout_qualifier = declspecs->upc_layout_qualifier;
5565 as1 = declspecs->address_space;
5566 as2 = TYPE_ADDR_SPACE (element_type);
5567 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5569 if (constp > 1)
5570 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5571 if (restrictp > 1)
5572 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5573 if (volatilep > 1)
5574 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5575 if (atomicp > 1)
5576 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5577 if (sharedp > 1)
5578 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<shared%>");
5579 if (strictp > 1)
5580 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<strict%>");
5581 if (relaxedp > 1)
5582 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<relaxed%>");
5583 if (strictp && relaxedp)
5584 error_at (loc, "UPC shared variable %qE is declared "
5585 "both strict and relaxed", name);
5586 if (strictp && !sharedp)
5587 error_at (loc, "%qE is declared with UPC strict qualifier "
5588 "but not shared", name);
5589 if (relaxedp && !sharedp)
5590 error_at (loc, "%qE is declared with UPC relaxed qualifier "
5591 "but not shared", name);
5593 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5594 error_at (loc, "conflicting named address spaces (%s vs %s)",
5595 c_addr_space_name (as1), c_addr_space_name (as2));
5597 if ((TREE_CODE (type) == ARRAY_TYPE
5598 || first_non_attr_kind == cdk_array)
5599 && TYPE_QUALS (element_type))
5600 type = TYPE_MAIN_VARIANT (type);
5601 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5602 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5603 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5604 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5605 | (sharedp ? TYPE_QUAL_UPC_SHARED : 0)
5606 | (strictp ? TYPE_QUAL_UPC_STRICT : 0)
5607 | (relaxedp ? TYPE_QUAL_UPC_RELAXED : 0)
5608 | ENCODE_QUAL_ADDR_SPACE (address_space));
5610 /* Applying the _Atomic qualifier to an array type (through the use
5611 of typedefs or typeof) must be detected here. If the qualifier
5612 is introduced later, any appearance of applying it to an array is
5613 actually applying it to an element of that array. */
5614 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5615 error_at (loc, "%<_Atomic%>-qualified array type");
5617 /* Warn about storage classes that are invalid for certain
5618 kinds of declarations (parameters, typenames, etc.). */
5620 if (funcdef_flag
5621 && (threadp
5622 || storage_class == csc_auto
5623 || storage_class == csc_register
5624 || storage_class == csc_typedef))
5626 if (storage_class == csc_auto)
5627 pedwarn (loc,
5628 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5629 "function definition declared %<auto%>");
5630 if (storage_class == csc_register)
5631 error_at (loc, "function definition declared %<register%>");
5632 if (storage_class == csc_typedef)
5633 error_at (loc, "function definition declared %<typedef%>");
5634 if (threadp)
5635 error_at (loc, "function definition declared %qs",
5636 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5637 threadp = false;
5638 if (storage_class == csc_auto
5639 || storage_class == csc_register
5640 || storage_class == csc_typedef)
5641 storage_class = csc_none;
5643 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5645 if (decl_context == PARM && storage_class == csc_register)
5647 else
5649 switch (decl_context)
5651 case FIELD:
5652 if (name)
5653 error_at (loc, "storage class specified for structure "
5654 "field %qE", name);
5655 else
5656 error_at (loc, "storage class specified for structure field");
5657 break;
5658 case PARM:
5659 if (name)
5660 error_at (loc, "storage class specified for parameter %qE",
5661 name);
5662 else
5663 error_at (loc, "storage class specified for unnamed parameter");
5664 break;
5665 default:
5666 error_at (loc, "storage class specified for typename");
5667 break;
5669 storage_class = csc_none;
5670 threadp = false;
5673 else if (storage_class == csc_extern
5674 && initialized
5675 && !funcdef_flag)
5677 /* 'extern' with initialization is invalid if not at file scope. */
5678 if (current_scope == file_scope)
5680 /* It is fine to have 'extern const' when compiling at C
5681 and C++ intersection. */
5682 if (!(warn_cxx_compat && constp))
5683 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5684 name);
5686 else
5687 error_at (loc, "%qE has both %<extern%> and initializer", name);
5689 else if (current_scope == file_scope)
5691 if (storage_class == csc_auto)
5692 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5693 name);
5694 if (pedantic && storage_class == csc_register)
5695 pedwarn (input_location, OPT_Wpedantic,
5696 "file-scope declaration of %qE specifies %<register%>", name);
5698 else
5700 if (storage_class == csc_extern && funcdef_flag)
5701 error_at (loc, "nested function %qE declared %<extern%>", name);
5702 else if (threadp && storage_class == csc_none)
5704 error_at (loc, "function-scope %qE implicitly auto and declared "
5705 "%qs", name,
5706 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5707 threadp = false;
5711 /* Now figure out the structure of the declarator proper.
5712 Descend through it, creating more complex types, until we reach
5713 the declared identifier (or NULL_TREE, in an absolute declarator).
5714 At each stage we maintain an unqualified version of the type
5715 together with any qualifiers that should be applied to it with
5716 c_build_qualified_type; this way, array types including
5717 multidimensional array types are first built up in unqualified
5718 form and then the qualified form is created with
5719 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5721 while (declarator && declarator->kind != cdk_id)
5723 if (type == error_mark_node)
5725 declarator = declarator->declarator;
5726 continue;
5729 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5730 a cdk_pointer (for *...),
5731 a cdk_function (for ...(...)),
5732 a cdk_attrs (for nested attributes),
5733 or a cdk_id (for the name being declared
5734 or the place in an absolute declarator
5735 where the name was omitted).
5736 For the last case, we have just exited the loop.
5738 At this point, TYPE is the type of elements of an array,
5739 or for a function to return, or for a pointer to point to.
5740 After this sequence of ifs, TYPE is the type of the
5741 array or function or pointer, and DECLARATOR has had its
5742 outermost layer removed. */
5744 if (array_ptr_quals != TYPE_UNQUALIFIED
5745 || array_ptr_attrs != NULL_TREE
5746 || array_parm_static)
5748 /* Only the innermost declarator (making a parameter be of
5749 array type which is converted to pointer type)
5750 may have static or type qualifiers. */
5751 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5752 array_ptr_quals = TYPE_UNQUALIFIED;
5753 array_ptr_attrs = NULL_TREE;
5754 array_parm_static = 0;
5757 switch (declarator->kind)
5759 case cdk_attrs:
5761 /* A declarator with embedded attributes. */
5762 tree attrs = declarator->u.attrs;
5763 const struct c_declarator *inner_decl;
5764 int attr_flags = 0;
5765 declarator = declarator->declarator;
5766 inner_decl = declarator;
5767 while (inner_decl->kind == cdk_attrs)
5768 inner_decl = inner_decl->declarator;
5769 if (inner_decl->kind == cdk_id)
5770 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5771 else if (inner_decl->kind == cdk_function)
5772 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5773 else if (inner_decl->kind == cdk_array)
5774 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5775 returned_attrs = decl_attributes (&type,
5776 chainon (returned_attrs, attrs),
5777 attr_flags);
5778 break;
5780 case cdk_array:
5782 tree itype = NULL_TREE;
5783 tree size = declarator->u.array.dimen;
5784 /* The index is a signed object `sizetype' bits wide. */
5785 tree index_type = c_common_signed_type (sizetype);
5787 array_ptr_quals = declarator->u.array.quals;
5788 array_ptr_attrs = declarator->u.array.attrs;
5789 array_parm_static = declarator->u.array.static_p;
5790 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5792 declarator = declarator->declarator;
5794 /* Check for some types that there cannot be arrays of. */
5796 if (VOID_TYPE_P (type))
5798 if (name)
5799 error_at (loc, "declaration of %qE as array of voids", name);
5800 else
5801 error_at (loc, "declaration of type name as array of voids");
5802 type = error_mark_node;
5805 if (TREE_CODE (type) == FUNCTION_TYPE)
5807 if (name)
5808 error_at (loc, "declaration of %qE as array of functions",
5809 name);
5810 else
5811 error_at (loc, "declaration of type name as array of "
5812 "functions");
5813 type = error_mark_node;
5816 if (pedantic && !in_system_header_at (input_location)
5817 && flexible_array_type_p (type))
5818 pedwarn (loc, OPT_Wpedantic,
5819 "invalid use of structure with flexible array member");
5821 if (size == error_mark_node)
5822 type = error_mark_node;
5824 if (type == error_mark_node)
5825 continue;
5827 /* If size was specified, set ITYPE to a range-type for
5828 that size. Otherwise, ITYPE remains null. finish_decl
5829 may figure it out from an initial value. */
5831 if (size)
5833 bool size_maybe_const = true;
5834 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5835 && !TREE_OVERFLOW (size));
5836 bool this_size_varies = false;
5838 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5839 lvalue. */
5840 STRIP_TYPE_NOPS (size);
5842 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5844 if (name)
5845 error_at (loc, "size of array %qE has non-integer type",
5846 name);
5847 else
5848 error_at (loc,
5849 "size of unnamed array has non-integer type");
5850 size = integer_one_node;
5853 size = c_fully_fold (size, false, &size_maybe_const);
5855 if (pedantic && size_maybe_const && integer_zerop (size))
5857 if (name)
5858 pedwarn (loc, OPT_Wpedantic,
5859 "ISO C forbids zero-size array %qE", name);
5860 else
5861 pedwarn (loc, OPT_Wpedantic,
5862 "ISO C forbids zero-size array");
5865 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5867 constant_expression_warning (size);
5868 if (tree_int_cst_sgn (size) < 0)
5870 if (name)
5871 error_at (loc, "size of array %qE is negative", name);
5872 else
5873 error_at (loc, "size of unnamed array is negative");
5874 size = integer_one_node;
5876 /* Handle a size folded to an integer constant but
5877 not an integer constant expression. */
5878 if (!size_int_const)
5880 /* If this is a file scope declaration of an
5881 ordinary identifier, this is invalid code;
5882 diagnosing it here and not subsequently
5883 treating the type as variable-length avoids
5884 more confusing diagnostics later. */
5885 if ((decl_context == NORMAL || decl_context == FIELD)
5886 && current_scope == file_scope)
5887 pedwarn (input_location, 0,
5888 "variably modified %qE at file scope",
5889 name);
5890 else
5891 this_size_varies = size_varies = true;
5892 warn_variable_length_array (name, size);
5895 else if (sharedp && count_upc_threads_refs (size))
5897 /* We have a shared array with a non-constant
5898 dimension. If the expression is a factor of
5899 THREADS, then we'll need to set the flag
5900 in the result type. Otherwise, it is an error. */
5901 int n_thread_refs = count_upc_threads_refs (size);
5902 if (upc_threads_ref || n_thread_refs > 1)
5904 error_at (loc, "UPC shared array declaration references THREADS "
5905 "more than once; the size of %qE "
5906 "cannot be calculated", name);
5907 size = integer_one_node;
5909 else if (!is_multiple_of_upc_threads (size))
5911 error_at (loc, "UPC shared array dimension is not a simple multiple "
5912 "of THREADS; the size of %qE "
5913 "cannot be calculated.", name);
5914 size = integer_one_node;
5916 else
5918 upc_threads_ref = 1;
5919 set_upc_threads_refs_to_one (&size);
5920 size = fold (size);
5921 if (TREE_CODE (size) != INTEGER_CST)
5923 error_at (loc, "UPC forbids variable-size shared array %qE",
5924 name);
5925 size = integer_one_node;
5929 else if ((decl_context == NORMAL || decl_context == FIELD)
5930 && current_scope == file_scope)
5932 error_at (loc, "variably modified %qE at file scope", name);
5933 size = integer_one_node;
5935 else
5937 /* Make sure the array size remains visibly
5938 nonconstant even if it is (eg) a const variable
5939 with known value. */
5940 this_size_varies = size_varies = true;
5941 warn_variable_length_array (name, size);
5942 if (flag_sanitize & SANITIZE_VLA
5943 && decl_context == NORMAL
5944 && current_function_decl != NULL_TREE
5945 && !lookup_attribute ("no_sanitize_undefined",
5946 DECL_ATTRIBUTES
5947 (current_function_decl)))
5949 /* Evaluate the array size only once. */
5950 size = c_save_expr (size);
5951 size = c_fully_fold (size, false, NULL);
5952 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5953 ubsan_instrument_vla (loc, size),
5954 size);
5958 if (integer_zerop (size) && !this_size_varies)
5960 /* A zero-length array cannot be represented with
5961 an unsigned index type, which is what we'll
5962 get with build_index_type. Create an
5963 open-ended range instead. */
5964 itype = build_range_type (sizetype, size, NULL_TREE);
5966 else
5968 /* Arrange for the SAVE_EXPR on the inside of the
5969 MINUS_EXPR, which allows the -1 to get folded
5970 with the +1 that happens when building TYPE_SIZE. */
5971 if (size_varies)
5972 size = save_expr (size);
5973 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5974 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5975 integer_zero_node, size);
5977 /* Compute the maximum valid index, that is, size
5978 - 1. Do the calculation in index_type, so that
5979 if it is a variable the computations will be
5980 done in the proper mode. */
5981 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5982 convert (index_type, size),
5983 convert (index_type,
5984 size_one_node));
5986 /* The above overflows when size does not fit
5987 in index_type.
5988 ??? While a size of INT_MAX+1 technically shouldn't
5989 cause an overflow (because we subtract 1), handling
5990 this case seems like an unnecessary complication. */
5991 if (TREE_CODE (size) == INTEGER_CST
5992 && !int_fits_type_p (size, index_type))
5994 if (name)
5995 error_at (loc, "size of array %qE is too large",
5996 name);
5997 else
5998 error_at (loc, "size of unnamed array is too large");
5999 type = error_mark_node;
6000 continue;
6003 itype = build_index_type (itype);
6005 if (this_size_varies)
6007 if (*expr)
6008 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6009 *expr, size);
6010 else
6011 *expr = size;
6012 *expr_const_operands &= size_maybe_const;
6015 else if (decl_context == FIELD)
6017 bool flexible_array_member = false;
6018 if (array_parm_vla_unspec_p)
6019 /* Field names can in fact have function prototype
6020 scope so [*] is disallowed here through making
6021 the field variably modified, not through being
6022 something other than a declaration with function
6023 prototype scope. */
6024 size_varies = true;
6025 else
6027 const struct c_declarator *t = declarator;
6028 while (t->kind == cdk_attrs)
6029 t = t->declarator;
6030 flexible_array_member = (t->kind == cdk_id);
6032 if (flexible_array_member
6033 && !in_system_header_at (input_location))
6034 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6035 "support flexible array members");
6037 /* ISO C99 Flexible array members are effectively
6038 identical to GCC's zero-length array extension. */
6039 if (flexible_array_member || array_parm_vla_unspec_p)
6040 itype = build_range_type (sizetype, size_zero_node,
6041 NULL_TREE);
6043 else if (decl_context == PARM)
6045 if (array_parm_vla_unspec_p)
6047 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6048 size_varies = true;
6051 else if (decl_context == TYPENAME)
6053 if (array_parm_vla_unspec_p)
6055 /* C99 6.7.5.2p4 */
6056 warning (0, "%<[*]%> not in a declaration");
6057 /* We use this to avoid messing up with incomplete
6058 array types of the same type, that would
6059 otherwise be modified below. */
6060 itype = build_range_type (sizetype, size_zero_node,
6061 NULL_TREE);
6062 size_varies = true;
6066 /* Complain about arrays of incomplete types. */
6067 if (!COMPLETE_TYPE_P (type))
6069 error_at (loc, "array type has incomplete element type %qT",
6070 type);
6071 type = error_mark_node;
6073 else
6074 /* When itype is NULL, a shared incomplete array type is
6075 returned for all array of a given type. Elsewhere we
6076 make sure we don't complete that type before copying
6077 it, but here we want to make sure we don't ever
6078 modify the shared type, so we gcc_assert (itype)
6079 below. */
6081 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6082 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6083 type = build_qualified_type (type,
6084 ENCODE_QUAL_ADDR_SPACE (as));
6086 type = build_array_type (type, itype);
6089 if (type != error_mark_node)
6091 if (size_varies)
6093 /* It is ok to modify type here even if itype is
6094 NULL: if size_varies, we're in a
6095 multi-dimensional array and the inner type has
6096 variable size, so the enclosing shared array type
6097 must too. */
6098 if (size && TREE_CODE (size) == INTEGER_CST)
6099 type
6100 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6101 C_TYPE_VARIABLE_SIZE (type) = 1;
6104 if (upc_threads_ref)
6106 /* We need a unique type copy here for UPC shared
6107 array types compiled in a dynamic threads enviroment
6108 that reference THREADS as a multiplier; to avoid
6109 setting the "has threads factor" bit in
6110 a re-used non- UPC shared array type node. */
6111 if (size && TREE_CODE (size) == INTEGER_CST)
6112 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6113 TYPE_HAS_THREADS_FACTOR (type) = 1;
6116 /* The GCC extension for zero-length arrays differs from
6117 ISO flexible array members in that sizeof yields
6118 zero. */
6119 if (size && integer_zerop (size))
6121 gcc_assert (itype);
6122 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6123 TYPE_SIZE (type) = bitsize_zero_node;
6124 TYPE_SIZE_UNIT (type) = size_zero_node;
6125 SET_TYPE_STRUCTURAL_EQUALITY (type);
6127 if (array_parm_vla_unspec_p)
6129 gcc_assert (itype);
6130 /* The type is complete. C99 6.7.5.2p4 */
6131 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6132 TYPE_SIZE (type) = bitsize_zero_node;
6133 TYPE_SIZE_UNIT (type) = size_zero_node;
6134 SET_TYPE_STRUCTURAL_EQUALITY (type);
6138 if (decl_context != PARM
6139 && (array_ptr_quals != TYPE_UNQUALIFIED
6140 || array_ptr_attrs != NULL_TREE
6141 || array_parm_static))
6143 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6144 array_ptr_quals = TYPE_UNQUALIFIED;
6145 array_ptr_attrs = NULL_TREE;
6146 array_parm_static = 0;
6148 break;
6150 case cdk_function:
6152 /* Say it's a definition only for the declarator closest
6153 to the identifier, apart possibly from some
6154 attributes. */
6155 bool really_funcdef = false;
6156 tree arg_types;
6157 if (funcdef_flag)
6159 const struct c_declarator *t = declarator->declarator;
6160 while (t->kind == cdk_attrs)
6161 t = t->declarator;
6162 really_funcdef = (t->kind == cdk_id);
6165 /* Declaring a function type. Make sure we have a valid
6166 type for the function to return. */
6167 if (type == error_mark_node)
6168 continue;
6170 size_varies = false;
6171 upc_threads_ref = 0;
6172 upc_layout_qualifier = 0;
6174 /* Warn about some types functions can't return. */
6175 if (TREE_CODE (type) == FUNCTION_TYPE)
6177 if (name)
6178 error_at (loc, "%qE declared as function returning a "
6179 "function", name);
6180 else
6181 error_at (loc, "type name declared as function "
6182 "returning a function");
6183 type = integer_type_node;
6185 if (TREE_CODE (type) == ARRAY_TYPE)
6187 if (name)
6188 error_at (loc, "%qE declared as function returning an array",
6189 name);
6190 else
6191 error_at (loc, "type name declared as function returning "
6192 "an array");
6193 type = integer_type_node;
6195 errmsg = targetm.invalid_return_type (type);
6196 if (errmsg)
6198 error (errmsg);
6199 type = integer_type_node;
6202 /* Construct the function type and go to the next
6203 inner layer of declarator. */
6204 arg_info = declarator->u.arg_info;
6205 arg_types = grokparms (arg_info, really_funcdef);
6207 /* Type qualifiers before the return type of the function
6208 qualify the return type, not the function type. */
6209 if (type_quals)
6211 /* Type qualifiers on a function return type are
6212 normally permitted by the standard but have no
6213 effect, so give a warning at -Wreturn-type.
6214 Qualifiers on a void return type are banned on
6215 function definitions in ISO C; GCC used to used
6216 them for noreturn functions. */
6217 if (VOID_TYPE_P (type) && really_funcdef)
6218 pedwarn (loc, 0,
6219 "function definition has qualified void return type");
6220 else if (type_quals & TYPE_QUAL_UPC_SHARED)
6222 error_at (loc, "function definition has UPC shared qualified return type");
6223 type_quals &= ~(TYPE_QUAL_UPC_SHARED | TYPE_QUAL_UPC_STRICT
6224 | TYPE_QUAL_UPC_RELAXED);
6226 else
6227 warning_at (loc, OPT_Wignored_qualifiers,
6228 "type qualifiers ignored on function return type");
6230 type = c_build_qualified_type (type, type_quals);
6232 type_quals = TYPE_UNQUALIFIED;
6234 type = build_function_type (type, arg_types);
6235 declarator = declarator->declarator;
6237 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6238 the formal parameter list of this FUNCTION_TYPE to point to
6239 the FUNCTION_TYPE node itself. */
6241 c_arg_tag *tag;
6242 unsigned ix;
6244 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6245 TYPE_CONTEXT (tag->type) = type;
6247 break;
6249 case cdk_pointer:
6251 /* Merge any constancy or volatility into the target type
6252 for the pointer. */
6253 if ((type_quals & TYPE_QUAL_ATOMIC)
6254 && TREE_CODE (type) == FUNCTION_TYPE)
6256 error_at (loc,
6257 "%<_Atomic%>-qualified function type");
6258 type_quals &= ~TYPE_QUAL_ATOMIC;
6260 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6261 && type_quals)
6262 pedwarn (loc, OPT_Wpedantic,
6263 "ISO C forbids qualified function types");
6265 if (upc_layout_qualifier)
6266 upc_block_factor = upc_grok_layout_qualifier (
6267 loc, POINTER_TYPE, type,
6268 NULL_TREE, upc_layout_qualifier);
6270 if (type_quals)
6271 type = c_build_qualified_type_1 (type, type_quals,
6272 upc_block_factor);
6274 size_varies = false;
6275 upc_threads_ref = 0;
6276 upc_block_factor = 0;
6278 /* When the pointed-to type involves components of variable size,
6279 care must be taken to ensure that the size evaluation code is
6280 emitted early enough to dominate all the possible later uses
6281 and late enough for the variables on which it depends to have
6282 been assigned.
6284 This is expected to happen automatically when the pointed-to
6285 type has a name/declaration of it's own, but special attention
6286 is required if the type is anonymous.
6288 We handle the NORMAL and FIELD contexts here by attaching an
6289 artificial TYPE_DECL to such pointed-to type. This forces the
6290 sizes evaluation at a safe point and ensures it is not deferred
6291 until e.g. within a deeper conditional context.
6293 We expect nothing to be needed here for PARM or TYPENAME.
6294 Pushing a TYPE_DECL at this point for TYPENAME would actually
6295 be incorrect, as we might be in the middle of an expression
6296 with side effects on the pointed-to type size "arguments" prior
6297 to the pointer declaration point and the fake TYPE_DECL in the
6298 enclosing context would force the size evaluation prior to the
6299 side effects. */
6301 if (!TYPE_NAME (type)
6302 && (decl_context == NORMAL || decl_context == FIELD)
6303 && variably_modified_type_p (type, NULL_TREE))
6305 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6306 DECL_ARTIFICIAL (decl) = 1;
6307 pushdecl (decl);
6308 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6309 TYPE_NAME (type) = decl;
6312 type = c_build_pointer_type (type);
6314 /* Process type qualifiers (such as const or volatile)
6315 that were given inside the `*'. */
6316 type_quals = declarator->u.pointer.quals;
6317 upc_layout_qualifier = declarator->u.pointer.upc_layout_qual;
6318 sharedp = ((type_quals & TYPE_QUAL_UPC_SHARED) != 0);
6320 declarator = declarator->declarator;
6321 break;
6323 default:
6324 gcc_unreachable ();
6327 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6329 /* Now TYPE has the actual type, apart from any qualifiers in
6330 TYPE_QUALS. */
6332 /* Warn about address space used for things other than static memory or
6333 pointers. */
6334 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6335 if (!ADDR_SPACE_GENERIC_P (address_space))
6337 if (decl_context == NORMAL)
6339 switch (storage_class)
6341 case csc_auto:
6342 error ("%qs combined with %<auto%> qualifier for %qE",
6343 c_addr_space_name (address_space), name);
6344 break;
6345 case csc_register:
6346 error ("%qs combined with %<register%> qualifier for %qE",
6347 c_addr_space_name (address_space), name);
6348 break;
6349 case csc_none:
6350 if (current_function_scope)
6352 error ("%qs specified for auto variable %qE",
6353 c_addr_space_name (address_space), name);
6354 break;
6356 break;
6357 case csc_static:
6358 case csc_extern:
6359 case csc_typedef:
6360 break;
6361 default:
6362 gcc_unreachable ();
6365 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6367 if (name)
6368 error ("%qs specified for parameter %qE",
6369 c_addr_space_name (address_space), name);
6370 else
6371 error ("%qs specified for unnamed parameter",
6372 c_addr_space_name (address_space));
6374 else if (decl_context == FIELD)
6376 if (name)
6377 error ("%qs specified for structure field %qE",
6378 c_addr_space_name (address_space), name);
6379 else
6380 error ("%qs specified for structure field",
6381 c_addr_space_name (address_space));
6385 /* Check the type and width of a bit-field. */
6386 if (bitfield)
6388 check_bitfield_type_and_width (&type, width, name);
6389 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6390 atomic types are permitted for bit-fields; we have no code to
6391 make bit-field accesses atomic, so disallow them. */
6392 if (type_quals & TYPE_QUAL_ATOMIC)
6394 if (name)
6395 error ("bit-field %qE has atomic type", name);
6396 else
6397 error ("bit-field has atomic type");
6398 type_quals &= ~TYPE_QUAL_ATOMIC;
6402 /* Check for UPC's layout qualifier. */
6403 if (upc_layout_qualifier || upc_elem_block_factor)
6405 upc_block_factor = upc_grok_layout_qualifier (loc, TREE_CODE (type), type,
6406 upc_elem_block_factor, upc_layout_qualifier);
6407 upc_layout_qualifier = 0;
6410 /* Reject invalid uses of _Alignas. */
6411 if (declspecs->alignas_p)
6413 if (storage_class == csc_typedef)
6414 error_at (loc, "alignment specified for typedef %qE", name);
6415 else if (storage_class == csc_register)
6416 error_at (loc, "alignment specified for %<register%> object %qE",
6417 name);
6418 else if (decl_context == PARM)
6420 if (name)
6421 error_at (loc, "alignment specified for parameter %qE", name);
6422 else
6423 error_at (loc, "alignment specified for unnamed parameter");
6425 else if (bitfield)
6427 if (name)
6428 error_at (loc, "alignment specified for bit-field %qE", name);
6429 else
6430 error_at (loc, "alignment specified for unnamed bit-field");
6432 else if (TREE_CODE (type) == FUNCTION_TYPE)
6433 error_at (loc, "alignment specified for function %qE", name);
6434 else if (declspecs->align_log != -1)
6436 alignas_align = 1U << declspecs->align_log;
6437 if (alignas_align < min_align_of_type (type))
6439 if (name)
6440 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6441 "alignment of %qE", name);
6442 else
6443 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6444 "alignment of unnamed field");
6445 alignas_align = 0;
6450 /* Did array size calculations overflow or does the array cover more
6451 than half of the address-space? */
6452 if (TREE_CODE (type) == ARRAY_TYPE
6453 && COMPLETE_TYPE_P (type)
6454 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6455 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
6457 if (name)
6458 error_at (loc, "size of array %qE is too large", name);
6459 else
6460 error_at (loc, "size of unnamed array is too large");
6461 /* If we proceed with the array type as it is, we'll eventually
6462 crash in tree_to_[su]hwi(). */
6463 type = error_mark_node;
6466 /* If this is declaring a typedef name, return a TYPE_DECL. */
6468 if (storage_class == csc_typedef)
6470 tree decl;
6471 if ((type_quals & TYPE_QUAL_ATOMIC)
6472 && TREE_CODE (type) == FUNCTION_TYPE)
6474 error_at (loc,
6475 "%<_Atomic%>-qualified function type");
6476 type_quals &= ~TYPE_QUAL_ATOMIC;
6478 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6479 && type_quals)
6480 pedwarn (loc, OPT_Wpedantic,
6481 "ISO C forbids qualified function types");
6482 if (type_quals)
6483 type = c_build_qualified_type_1 (type, type_quals, upc_block_factor);
6485 decl = build_decl (declarator->id_loc,
6486 TYPE_DECL, declarator->u.id, type);
6487 if (declspecs->explicit_signed_p)
6488 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6489 if (declspecs->inline_p)
6490 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6491 if (declspecs->noreturn_p)
6492 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6494 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6496 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6498 if (b != NULL
6499 && b->decl != NULL_TREE
6500 && (B_IN_CURRENT_SCOPE (b)
6501 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6502 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6504 warning_at (declarator->id_loc, OPT_Wc___compat,
6505 ("using %qD as both a typedef and a tag is "
6506 "invalid in C++"),
6507 decl);
6508 if (b->locus != UNKNOWN_LOCATION)
6509 inform (b->locus, "originally defined here");
6513 return decl;
6516 /* If this is a type name (such as, in a cast or sizeof),
6517 compute the type and return it now. */
6519 if (decl_context == TYPENAME)
6521 /* Note that the grammar rejects storage classes in typenames
6522 and fields. */
6523 gcc_assert (storage_class == csc_none && !threadp
6524 && !declspecs->inline_p && !declspecs->noreturn_p);
6525 if ((type_quals & TYPE_QUAL_ATOMIC)
6526 && TREE_CODE (type) == FUNCTION_TYPE)
6528 error_at (loc,
6529 "%<_Atomic%>-qualified function type");
6530 type_quals &= ~TYPE_QUAL_ATOMIC;
6532 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6533 && type_quals)
6534 pedwarn (loc, OPT_Wpedantic,
6535 "ISO C forbids const or volatile function types");
6536 if (type_quals)
6537 type = c_build_qualified_type_1 (type, type_quals, upc_block_factor);
6538 return type;
6541 if (pedantic && decl_context == FIELD
6542 && variably_modified_type_p (type, NULL_TREE))
6544 /* C99 6.7.2.1p8 */
6545 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6546 "have a variably modified type");
6549 /* Aside from typedefs and type names (handle above),
6550 `void' at top level (not within pointer)
6551 is allowed only in public variables.
6552 We don't complain about parms either, but that is because
6553 a better error message can be made later. */
6555 if (VOID_TYPE_P (type) && decl_context != PARM
6556 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6557 && (storage_class == csc_extern
6558 || (current_scope == file_scope
6559 && !(storage_class == csc_static
6560 || storage_class == csc_register)))))
6562 error_at (loc, "variable or field %qE declared void", name);
6563 type = integer_type_node;
6566 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6567 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6570 tree decl;
6572 if (decl_context == PARM)
6574 tree promoted_type;
6575 bool array_parameter_p = false;
6577 /* A parameter declared as an array of T is really a pointer to T.
6578 One declared as a function is really a pointer to a function. */
6580 if (TREE_CODE (type) == ARRAY_TYPE)
6582 /* Transfer const-ness of array into that of type pointed to. */
6583 type = TREE_TYPE (type);
6584 if (type_quals)
6585 type = c_build_qualified_type_1 (type, type_quals,
6586 upc_block_factor);
6587 type = c_build_pointer_type (type);
6588 type_quals = array_ptr_quals;
6589 if (type_quals)
6590 type = c_build_qualified_type (type, type_quals);
6592 /* We don't yet implement attributes in this context. */
6593 if (array_ptr_attrs != NULL_TREE)
6594 warning_at (loc, OPT_Wattributes,
6595 "attributes in parameter array declarator ignored");
6597 size_varies = false;
6598 array_parameter_p = true;
6600 else if (TREE_CODE (type) == FUNCTION_TYPE)
6602 if (type_quals & TYPE_QUAL_ATOMIC)
6604 error_at (loc,
6605 "%<_Atomic%>-qualified function type");
6606 type_quals &= ~TYPE_QUAL_ATOMIC;
6608 else if (type_quals)
6609 pedwarn (loc, OPT_Wpedantic,
6610 "ISO C forbids qualified function types");
6611 if (type_quals)
6612 type = c_build_qualified_type (type, type_quals);
6613 type = c_build_pointer_type (type);
6614 type_quals = TYPE_UNQUALIFIED;
6616 else if (type_quals & TYPE_QUAL_UPC_SHARED)
6618 error ("parameter declared with UPC shared qualifier");
6619 type_quals &= ~(TYPE_QUAL_UPC_SHARED | TYPE_QUAL_UPC_STRICT
6620 | TYPE_QUAL_UPC_RELAXED);
6622 else if (type_quals)
6623 type = c_build_qualified_type (type, type_quals);
6625 decl = build_decl (declarator->id_loc,
6626 PARM_DECL, declarator->u.id, type);
6627 if (size_varies)
6628 C_DECL_VARIABLE_SIZE (decl) = 1;
6629 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6631 /* Compute the type actually passed in the parmlist,
6632 for the case where there is no prototype.
6633 (For example, shorts and chars are passed as ints.)
6634 When there is a prototype, this is overridden later. */
6636 if (type == error_mark_node)
6637 promoted_type = type;
6638 else
6639 promoted_type = c_type_promotes_to (type);
6641 DECL_ARG_TYPE (decl) = promoted_type;
6642 if (declspecs->inline_p)
6643 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6644 if (declspecs->noreturn_p)
6645 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6647 else if (decl_context == FIELD)
6649 /* Note that the grammar rejects storage classes in typenames
6650 and fields. */
6651 gcc_assert (storage_class == csc_none && !threadp
6652 && !declspecs->inline_p && !declspecs->noreturn_p);
6654 /* Structure field. It may not be a function. */
6656 if (TREE_CODE (type) == FUNCTION_TYPE)
6658 error_at (loc, "field %qE declared as a function", name);
6659 type = build_pointer_type (type);
6661 else if (TREE_CODE (type) != ERROR_MARK
6662 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6664 if (name)
6665 error_at (loc, "field %qE has incomplete type", name);
6666 else
6667 error_at (loc, "unnamed field has incomplete type");
6668 type = error_mark_node;
6670 else if (TREE_CODE (type) == ARRAY_TYPE
6671 && TYPE_DOMAIN (type) == NULL_TREE)
6673 /* We have a flexible array member through a typedef.
6674 Set suitable range. Whether this is a correct position
6675 for a flexible array member will be determined elsewhere. */
6676 if (!in_system_header_at (input_location))
6677 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6678 "support flexible array members");
6679 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6680 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6681 NULL_TREE);
6683 else if (type_quals & TYPE_QUAL_UPC_SHARED)
6685 error_at (loc, "field %qE declared with UPC shared qualifier",
6686 name);
6687 type_quals &= ~(TYPE_QUAL_UPC_SHARED | TYPE_QUAL_UPC_STRICT
6688 | TYPE_QUAL_UPC_RELAXED);
6690 type = c_build_qualified_type (type, type_quals);
6691 decl = build_decl (declarator->id_loc,
6692 FIELD_DECL, declarator->u.id, type);
6693 DECL_NONADDRESSABLE_P (decl) = bitfield;
6694 if (bitfield && !declarator->u.id)
6695 TREE_NO_WARNING (decl) = 1;
6697 if (size_varies)
6698 C_DECL_VARIABLE_SIZE (decl) = 1;
6700 else if (TREE_CODE (type) == FUNCTION_TYPE)
6702 if (storage_class == csc_register || threadp)
6704 error_at (loc, "invalid storage class for function %qE", name);
6706 else if (current_scope != file_scope)
6708 /* Function declaration not at file scope. Storage
6709 classes other than `extern' are not allowed, C99
6710 6.7.1p5, and `extern' makes no difference. However,
6711 GCC allows 'auto', perhaps with 'inline', to support
6712 nested functions. */
6713 if (storage_class == csc_auto)
6714 pedwarn (loc, OPT_Wpedantic,
6715 "invalid storage class for function %qE", name);
6716 else if (storage_class == csc_static)
6718 error_at (loc, "invalid storage class for function %qE", name);
6719 if (funcdef_flag)
6720 storage_class = declspecs->storage_class = csc_none;
6721 else
6722 return 0;
6726 decl = build_decl (declarator->id_loc,
6727 FUNCTION_DECL, declarator->u.id, type);
6728 decl = build_decl_attribute_variant (decl, decl_attr);
6730 if (type_quals & TYPE_QUAL_ATOMIC)
6732 error_at (loc,
6733 "%<_Atomic%>-qualified function type");
6734 type_quals &= ~TYPE_QUAL_ATOMIC;
6736 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6737 pedwarn (loc, OPT_Wpedantic,
6738 "ISO C forbids qualified function types");
6740 /* Every function declaration is an external reference
6741 (DECL_EXTERNAL) except for those which are not at file
6742 scope and are explicitly declared "auto". This is
6743 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6744 GCC to signify a forward declaration of a nested function. */
6745 if (storage_class == csc_auto && current_scope != file_scope)
6746 DECL_EXTERNAL (decl) = 0;
6747 /* In C99, a function which is declared 'inline' with 'extern'
6748 is not an external reference (which is confusing). It
6749 means that the later definition of the function must be output
6750 in this file, C99 6.7.4p6. In GNU C89, a function declared
6751 'extern inline' is an external reference. */
6752 else if (declspecs->inline_p && storage_class != csc_static)
6753 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6754 == flag_gnu89_inline);
6755 else
6756 DECL_EXTERNAL (decl) = !initialized;
6758 /* Record absence of global scope for `static' or `auto'. */
6759 TREE_PUBLIC (decl)
6760 = !(storage_class == csc_static || storage_class == csc_auto);
6762 /* For a function definition, record the argument information
6763 block where store_parm_decls will look for it. */
6764 if (funcdef_flag)
6765 current_function_arg_info = arg_info;
6767 if (declspecs->default_int_p)
6768 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6770 /* Record presence of `inline' and `_Noreturn', if it is
6771 reasonable. */
6772 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6774 if (declspecs->inline_p)
6775 pedwarn (loc, 0, "cannot inline function %<main%>");
6776 if (declspecs->noreturn_p)
6777 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6779 else
6781 if (declspecs->inline_p)
6782 /* Record that the function is declared `inline'. */
6783 DECL_DECLARED_INLINE_P (decl) = 1;
6784 if (declspecs->noreturn_p)
6786 if (flag_isoc99)
6787 pedwarn_c99 (loc, OPT_Wpedantic,
6788 "ISO C99 does not support %<_Noreturn%>");
6789 else
6790 pedwarn_c99 (loc, OPT_Wpedantic,
6791 "ISO C90 does not support %<_Noreturn%>");
6792 TREE_THIS_VOLATILE (decl) = 1;
6796 else
6798 /* It's a variable. */
6799 /* An uninitialized decl with `extern' is a reference. */
6800 int extern_ref = !initialized && storage_class == csc_extern;
6802 if ((type_quals & TYPE_QUAL_UPC_SHARED)
6803 && !extern_ref
6804 && !((current_scope == file_scope)
6805 || (storage_class == csc_static)))
6807 error_at (loc, "UPC does not support shared auto variables");
6808 type = error_mark_node;
6811 type = c_build_qualified_type_1 (type, type_quals, upc_block_factor);
6813 /* C99 6.2.2p7: It is invalid (compile-time undefined
6814 behavior) to create an 'extern' declaration for a
6815 variable if there is a global declaration that is
6816 'static' and the global declaration is not visible.
6817 (If the static declaration _is_ currently visible,
6818 the 'extern' declaration is taken to refer to that decl.) */
6819 if (extern_ref && current_scope != file_scope)
6821 tree global_decl = identifier_global_value (declarator->u.id);
6822 tree visible_decl = lookup_name (declarator->u.id);
6824 if (global_decl
6825 && global_decl != visible_decl
6826 && TREE_CODE (global_decl) == VAR_DECL
6827 && !TREE_PUBLIC (global_decl))
6828 error_at (loc, "variable previously declared %<static%> "
6829 "redeclared %<extern%>");
6832 decl = build_decl (declarator->id_loc,
6833 VAR_DECL, declarator->u.id, type);
6834 if (size_varies)
6835 C_DECL_VARIABLE_SIZE (decl) = 1;
6837 if (declspecs->inline_p)
6838 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6839 if (declspecs->noreturn_p)
6840 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6842 /* At file scope, an initialized extern declaration may follow
6843 a static declaration. In that case, DECL_EXTERNAL will be
6844 reset later in start_decl. */
6845 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6847 /* At file scope, the presence of a `static' or `register' storage
6848 class specifier, or the absence of all storage class specifiers
6849 makes this declaration a definition (perhaps tentative). Also,
6850 the absence of `static' makes it public. */
6851 if (current_scope == file_scope)
6853 TREE_PUBLIC (decl) = storage_class != csc_static;
6854 TREE_STATIC (decl) = !extern_ref;
6856 /* Not at file scope, only `static' makes a static definition. */
6857 else
6859 TREE_STATIC (decl) = (storage_class == csc_static);
6860 /* UPC's 'shared' attribute implies that the storage
6861 is 'static' to the extent it is stored in
6862 memory. */
6863 if (type_quals & TYPE_QUAL_UPC_SHARED)
6864 TREE_STATIC (decl) = 1;
6865 TREE_PUBLIC (decl) = extern_ref;
6868 if (threadp)
6869 set_decl_tls_model (decl, decl_default_tls_model (decl));
6872 if ((storage_class == csc_extern
6873 || (storage_class == csc_none
6874 && TREE_CODE (type) == FUNCTION_TYPE
6875 && !funcdef_flag))
6876 && variably_modified_type_p (type, NULL_TREE))
6878 /* C99 6.7.5.2p2 */
6879 if (TREE_CODE (type) == FUNCTION_TYPE)
6880 error_at (loc, "non-nested function with variably modified type");
6881 else
6882 error_at (loc, "object with variably modified type must have "
6883 "no linkage");
6886 /* Record `register' declaration for warnings on &
6887 and in case doing stupid register allocation. */
6889 if (storage_class == csc_register)
6891 C_DECL_REGISTER (decl) = 1;
6892 DECL_REGISTER (decl) = 1;
6895 /* Record constancy and volatility. */
6896 c_apply_type_quals_to_decl (type_quals, decl);
6898 /* Apply _Alignas specifiers. */
6899 if (alignas_align)
6901 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6902 DECL_USER_ALIGN (decl) = 1;
6905 /* If a type has volatile components, it should be stored in memory.
6906 Otherwise, the fact that those components are volatile
6907 will be ignored, and would even crash the compiler.
6908 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6909 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6910 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6911 || TREE_CODE (decl) == RESULT_DECL))
6913 /* It is not an error for a structure with volatile fields to
6914 be declared register, but reset DECL_REGISTER since it
6915 cannot actually go in a register. */
6916 int was_reg = C_DECL_REGISTER (decl);
6917 C_DECL_REGISTER (decl) = 0;
6918 DECL_REGISTER (decl) = 0;
6919 c_mark_addressable (decl);
6920 C_DECL_REGISTER (decl) = was_reg;
6923 /* This is the earliest point at which we might know the assembler
6924 name of a variable. Thus, if it's known before this, die horribly. */
6925 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6927 if (warn_cxx_compat
6928 && TREE_CODE (decl) == VAR_DECL
6929 && TREE_PUBLIC (decl)
6930 && TREE_STATIC (decl)
6931 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6932 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6933 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6934 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6935 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6936 ("non-local variable %qD with anonymous type is "
6937 "questionable in C++"),
6938 decl);
6940 /* Shared variables are given their own link section on
6941 most target platforms, and if compiling in pthreads mode
6942 regular local file scope variables are made thread local. */
6943 if ((TREE_CODE(decl) == VAR_DECL)
6944 && !threadp && (TREE_SHARED (decl) || flag_upc_pthreads))
6945 upc_set_decl_section (decl);
6947 return decl;
6951 /* Decode the parameter-list info for a function type or function definition.
6952 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6953 if there is an identifier list instead of a parameter decl list).
6954 These two functions are separate because when a function returns
6955 or receives functions then each is called multiple times but the order
6956 of calls is different. The last call to `grokparms' is always the one
6957 that contains the formal parameter names of a function definition.
6959 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6961 FUNCDEF_FLAG is true for a function definition, false for
6962 a mere declaration. A nonempty identifier-list gets an error message
6963 when FUNCDEF_FLAG is false. */
6965 static tree
6966 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6968 tree arg_types = arg_info->types;
6970 if (funcdef_flag && arg_info->had_vla_unspec)
6972 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6973 /* C99 6.7.5.2p4 */
6974 error ("%<[*]%> not allowed in other than function prototype scope");
6977 if (arg_types == 0 && !funcdef_flag
6978 && !in_system_header_at (input_location))
6979 warning (OPT_Wstrict_prototypes,
6980 "function declaration isn%'t a prototype");
6982 if (arg_types == error_mark_node)
6983 return 0; /* don't set TYPE_ARG_TYPES in this case */
6985 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6987 if (!funcdef_flag)
6989 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6990 arg_info->parms = NULL_TREE;
6992 else
6993 arg_info->parms = arg_info->types;
6995 arg_info->types = 0;
6996 return 0;
6998 else
7000 tree parm, type, typelt;
7001 unsigned int parmno;
7002 const char *errmsg;
7004 /* If there is a parameter of incomplete type in a definition,
7005 this is an error. In a declaration this is valid, and a
7006 struct or union type may be completed later, before any calls
7007 or definition of the function. In the case where the tag was
7008 first declared within the parameter list, a warning has
7009 already been given. If a parameter has void type, then
7010 however the function cannot be defined or called, so
7011 warn. */
7013 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7014 parm;
7015 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7017 type = TREE_VALUE (typelt);
7018 if (type == error_mark_node)
7019 continue;
7021 if (!COMPLETE_TYPE_P (type))
7023 if (funcdef_flag)
7025 if (DECL_NAME (parm))
7026 error_at (input_location,
7027 "parameter %u (%q+D) has incomplete type",
7028 parmno, parm);
7029 else
7030 error_at (DECL_SOURCE_LOCATION (parm),
7031 "parameter %u has incomplete type",
7032 parmno);
7034 TREE_VALUE (typelt) = error_mark_node;
7035 TREE_TYPE (parm) = error_mark_node;
7036 arg_types = NULL_TREE;
7038 else if (VOID_TYPE_P (type))
7040 if (DECL_NAME (parm))
7041 warning_at (input_location, 0,
7042 "parameter %u (%q+D) has void type",
7043 parmno, parm);
7044 else
7045 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7046 "parameter %u has void type",
7047 parmno);
7051 errmsg = targetm.invalid_parameter_type (type);
7052 if (errmsg)
7054 error (errmsg);
7055 TREE_VALUE (typelt) = error_mark_node;
7056 TREE_TYPE (parm) = error_mark_node;
7057 arg_types = NULL_TREE;
7060 if (DECL_NAME (parm) && TREE_USED (parm))
7061 warn_if_shadowing (parm);
7063 return arg_types;
7067 /* Allocate and initialize a c_arg_info structure from the parser's
7068 obstack. */
7070 struct c_arg_info *
7071 build_arg_info (void)
7073 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7074 ret->parms = NULL_TREE;
7075 ret->tags = NULL;
7076 ret->types = NULL_TREE;
7077 ret->others = NULL_TREE;
7078 ret->pending_sizes = NULL;
7079 ret->had_vla_unspec = 0;
7080 return ret;
7083 /* Take apart the current scope and return a c_arg_info structure with
7084 info on a parameter list just parsed.
7086 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7088 ELLIPSIS being true means the argument list ended in '...' so don't
7089 append a sentinel (void_list_node) to the end of the type-list.
7091 EXPR is NULL or an expression that needs to be evaluated for the
7092 side effects of array size expressions in the parameters. */
7094 struct c_arg_info *
7095 get_parm_info (bool ellipsis, tree expr)
7097 struct c_binding *b = current_scope->bindings;
7098 struct c_arg_info *arg_info = build_arg_info ();
7100 tree parms = 0;
7101 vec<c_arg_tag, va_gc> *tags = NULL;
7102 tree types = 0;
7103 tree others = 0;
7105 static bool explained_incomplete_types = false;
7106 bool gave_void_only_once_err = false;
7108 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7110 /* The bindings in this scope must not get put into a block.
7111 We will take care of deleting the binding nodes. */
7112 current_scope->bindings = 0;
7114 /* This function is only called if there was *something* on the
7115 parameter list. */
7116 gcc_assert (b);
7118 /* A parameter list consisting solely of 'void' indicates that the
7119 function takes no arguments. But if the 'void' is qualified
7120 (by 'const' or 'volatile'), or has a storage class specifier
7121 ('register'), then the behavior is undefined; issue an error.
7122 Typedefs for 'void' are OK (see DR#157). */
7123 if (b->prev == 0 /* one binding */
7124 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7125 && !DECL_NAME (b->decl) /* anonymous */
7126 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7128 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7129 || C_DECL_REGISTER (b->decl))
7130 error ("%<void%> as only parameter may not be qualified");
7132 /* There cannot be an ellipsis. */
7133 if (ellipsis)
7134 error ("%<void%> must be the only parameter");
7136 arg_info->types = void_list_node;
7137 return arg_info;
7140 if (!ellipsis)
7141 types = void_list_node;
7143 /* Break up the bindings list into parms, tags, types, and others;
7144 apply sanity checks; purge the name-to-decl bindings. */
7145 while (b)
7147 tree decl = b->decl;
7148 tree type = TREE_TYPE (decl);
7149 c_arg_tag tag;
7150 const char *keyword;
7152 switch (TREE_CODE (decl))
7154 case PARM_DECL:
7155 if (b->id)
7157 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7158 I_SYMBOL_BINDING (b->id) = b->shadowed;
7161 /* Check for forward decls that never got their actual decl. */
7162 if (TREE_ASM_WRITTEN (decl))
7163 error ("parameter %q+D has just a forward declaration", decl);
7164 /* Check for (..., void, ...) and issue an error. */
7165 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7167 if (!gave_void_only_once_err)
7169 error ("%<void%> must be the only parameter");
7170 gave_void_only_once_err = true;
7173 else
7175 /* Valid parameter, add it to the list. */
7176 DECL_CHAIN (decl) = parms;
7177 parms = decl;
7179 /* Since there is a prototype, args are passed in their
7180 declared types. The back end may override this later. */
7181 DECL_ARG_TYPE (decl) = type;
7182 types = tree_cons (0, type, types);
7184 break;
7186 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7187 case UNION_TYPE: keyword = "union"; goto tag;
7188 case RECORD_TYPE: keyword = "struct"; goto tag;
7189 tag:
7190 /* Types may not have tag-names, in which case the type
7191 appears in the bindings list with b->id NULL. */
7192 if (b->id)
7194 gcc_assert (I_TAG_BINDING (b->id) == b);
7195 I_TAG_BINDING (b->id) = b->shadowed;
7198 /* Warn about any struct, union or enum tags defined in a
7199 parameter list. The scope of such types is limited to
7200 the parameter list, which is rarely if ever desirable
7201 (it's impossible to call such a function with type-
7202 correct arguments). An anonymous union parm type is
7203 meaningful as a GNU extension, so don't warn for that. */
7204 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7206 if (b->id)
7207 /* The %s will be one of 'struct', 'union', or 'enum'. */
7208 warning (0, "%<%s %E%> declared inside parameter list",
7209 keyword, b->id);
7210 else
7211 /* The %s will be one of 'struct', 'union', or 'enum'. */
7212 warning (0, "anonymous %s declared inside parameter list",
7213 keyword);
7215 if (!explained_incomplete_types)
7217 warning (0, "its scope is only this definition or declaration,"
7218 " which is probably not what you want");
7219 explained_incomplete_types = true;
7223 tag.id = b->id;
7224 tag.type = decl;
7225 vec_safe_push (tags, tag);
7226 break;
7228 case CONST_DECL:
7229 case TYPE_DECL:
7230 case FUNCTION_DECL:
7231 /* CONST_DECLs appear here when we have an embedded enum,
7232 and TYPE_DECLs appear here when we have an embedded struct
7233 or union. No warnings for this - we already warned about the
7234 type itself. FUNCTION_DECLs appear when there is an implicit
7235 function declaration in the parameter list. */
7237 /* When we reinsert this decl in the function body, we need
7238 to reconstruct whether it was marked as nested. */
7239 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7240 ? b->nested
7241 : !b->nested);
7242 DECL_CHAIN (decl) = others;
7243 others = decl;
7244 /* fall through */
7246 case ERROR_MARK:
7247 /* error_mark_node appears here when we have an undeclared
7248 variable. Just throw it away. */
7249 if (b->id)
7251 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7252 I_SYMBOL_BINDING (b->id) = b->shadowed;
7254 break;
7256 /* Other things that might be encountered. */
7257 case LABEL_DECL:
7258 case VAR_DECL:
7259 default:
7260 gcc_unreachable ();
7263 b = free_binding_and_advance (b);
7266 arg_info->parms = parms;
7267 arg_info->tags = tags;
7268 arg_info->types = types;
7269 arg_info->others = others;
7270 arg_info->pending_sizes = expr;
7271 return arg_info;
7274 /* Get the struct, enum or union (CODE says which) with tag NAME.
7275 Define the tag as a forward-reference with location LOC if it is
7276 not defined. Return a c_typespec structure for the type
7277 specifier. */
7279 struct c_typespec
7280 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7282 struct c_typespec ret;
7283 tree ref;
7284 location_t refloc;
7286 ret.expr = NULL_TREE;
7287 ret.expr_const_operands = true;
7289 /* If a cross reference is requested, look up the type
7290 already defined for this tag and return it. */
7292 ref = lookup_tag (code, name, 0, &refloc);
7293 /* If this is the right type of tag, return what we found.
7294 (This reference will be shadowed by shadow_tag later if appropriate.)
7295 If this is the wrong type of tag, do not return it. If it was the
7296 wrong type in the same scope, we will have had an error
7297 message already; if in a different scope and declaring
7298 a name, pending_xref_error will give an error message; but if in a
7299 different scope and not declaring a name, this tag should
7300 shadow the previous declaration of a different type of tag, and
7301 this would not work properly if we return the reference found.
7302 (For example, with "struct foo" in an outer scope, "union foo;"
7303 must shadow that tag with a new one of union type.) */
7304 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7305 if (ref && TREE_CODE (ref) == code)
7307 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7308 && loc != UNKNOWN_LOCATION
7309 && warn_cxx_compat)
7311 switch (code)
7313 case ENUMERAL_TYPE:
7314 warning_at (loc, OPT_Wc___compat,
7315 ("enum type defined in struct or union "
7316 "is not visible in C++"));
7317 inform (refloc, "enum type defined here");
7318 break;
7319 case RECORD_TYPE:
7320 warning_at (loc, OPT_Wc___compat,
7321 ("struct defined in struct or union "
7322 "is not visible in C++"));
7323 inform (refloc, "struct defined here");
7324 break;
7325 case UNION_TYPE:
7326 warning_at (loc, OPT_Wc___compat,
7327 ("union defined in struct or union "
7328 "is not visible in C++"));
7329 inform (refloc, "union defined here");
7330 break;
7331 default:
7332 gcc_unreachable();
7336 ret.spec = ref;
7337 return ret;
7340 /* If no such tag is yet defined, create a forward-reference node
7341 and record it as the "definition".
7342 When a real declaration of this type is found,
7343 the forward-reference will be altered into a real type. */
7345 ref = make_node (code);
7346 if (code == ENUMERAL_TYPE)
7348 /* Give the type a default layout like unsigned int
7349 to avoid crashing if it does not get defined. */
7350 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7351 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
7352 TYPE_USER_ALIGN (ref) = 0;
7353 TYPE_UNSIGNED (ref) = 1;
7354 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7355 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7356 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7359 pushtag (loc, name, ref);
7361 ret.spec = ref;
7362 return ret;
7365 /* Get the struct, enum or union (CODE says which) with tag NAME.
7366 Define the tag as a forward-reference if it is not defined.
7367 Return a tree for the type. */
7369 tree
7370 xref_tag (enum tree_code code, tree name)
7372 return parser_xref_tag (input_location, code, name).spec;
7375 /* Make sure that the tag NAME is defined *in the current scope*
7376 at least as a forward reference.
7377 LOC is the location of the struct's definition.
7378 CODE says which kind of tag NAME ought to be.
7380 This stores the current value of the file static STRUCT_PARSE_INFO
7381 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7382 new c_struct_parse_info structure. The old value of
7383 STRUCT_PARSE_INFO is restored in finish_struct. */
7385 tree
7386 start_struct (location_t loc, enum tree_code code, tree name,
7387 struct c_struct_parse_info **enclosing_struct_parse_info)
7389 /* If there is already a tag defined at this scope
7390 (as a forward reference), just return it. */
7392 tree ref = NULL_TREE;
7393 location_t refloc = UNKNOWN_LOCATION;
7395 if (name != NULL_TREE)
7396 ref = lookup_tag (code, name, 1, &refloc);
7397 if (ref && TREE_CODE (ref) == code)
7399 if (TYPE_SIZE (ref))
7401 if (code == UNION_TYPE)
7402 error_at (loc, "redefinition of %<union %E%>", name);
7403 else
7404 error_at (loc, "redefinition of %<struct %E%>", name);
7405 if (refloc != UNKNOWN_LOCATION)
7406 inform (refloc, "originally defined here");
7407 /* Don't create structures using a name already in use. */
7408 ref = NULL_TREE;
7410 else if (C_TYPE_BEING_DEFINED (ref))
7412 if (code == UNION_TYPE)
7413 error_at (loc, "nested redefinition of %<union %E%>", name);
7414 else
7415 error_at (loc, "nested redefinition of %<struct %E%>", name);
7416 /* Don't bother to report "originally defined here" for a
7417 nested redefinition; the original definition should be
7418 obvious. */
7419 /* Don't create structures that contain themselves. */
7420 ref = NULL_TREE;
7424 /* Otherwise create a forward-reference just so the tag is in scope. */
7426 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7428 ref = make_node (code);
7429 pushtag (loc, name, ref);
7432 C_TYPE_BEING_DEFINED (ref) = 1;
7433 TYPE_PACKED (ref) = flag_pack_struct;
7435 *enclosing_struct_parse_info = struct_parse_info;
7436 struct_parse_info = XNEW (struct c_struct_parse_info);
7437 struct_parse_info->struct_types.create (0);
7438 struct_parse_info->fields.create (0);
7439 struct_parse_info->typedefs_seen.create (0);
7441 /* FIXME: This will issue a warning for a use of a type defined
7442 within a statement expr used within sizeof, et. al. This is not
7443 terribly serious as C++ doesn't permit statement exprs within
7444 sizeof anyhow. */
7445 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7446 warning_at (loc, OPT_Wc___compat,
7447 "defining type in %qs expression is invalid in C++",
7448 (in_sizeof
7449 ? "sizeof"
7450 : (in_typeof ? "typeof" : "alignof")));
7452 return ref;
7455 /* Process the specs, declarator and width (NULL if omitted)
7456 of a structure component, returning a FIELD_DECL node.
7457 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7458 DECL_ATTRS is as for grokdeclarator.
7460 LOC is the location of the structure component.
7462 This is done during the parsing of the struct declaration.
7463 The FIELD_DECL nodes are chained together and the lot of them
7464 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7466 tree
7467 grokfield (location_t loc,
7468 struct c_declarator *declarator, struct c_declspecs *declspecs,
7469 tree width, tree *decl_attrs)
7471 tree value;
7473 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7474 && width == NULL_TREE)
7476 /* This is an unnamed decl.
7478 If we have something of the form "union { list } ;" then this
7479 is the anonymous union extension. Similarly for struct.
7481 If this is something of the form "struct foo;", then
7482 If MS or Plan 9 extensions are enabled, this is handled as
7483 an anonymous struct.
7484 Otherwise this is a forward declaration of a structure tag.
7486 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7487 If foo names a structure or union without a tag, then this
7488 is an anonymous struct (this is permitted by C11).
7489 If MS or Plan 9 extensions are enabled and foo names a
7490 structure, then again this is an anonymous struct.
7491 Otherwise this is an error.
7493 Oh what a horrid tangled web we weave. I wonder if MS consciously
7494 took this from Plan 9 or if it was an accident of implementation
7495 that took root before someone noticed the bug... */
7497 tree type = declspecs->type;
7498 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
7499 || TREE_CODE (type) == UNION_TYPE);
7500 bool ok = false;
7502 if (type_ok
7503 && (flag_ms_extensions
7504 || flag_plan9_extensions
7505 || !declspecs->typedef_p))
7507 if (flag_ms_extensions || flag_plan9_extensions)
7508 ok = true;
7509 else if (TYPE_NAME (type) == NULL)
7510 ok = true;
7511 else
7512 ok = false;
7514 if (!ok)
7516 pedwarn (loc, 0, "declaration does not declare anything");
7517 return NULL_TREE;
7519 if (flag_isoc99)
7520 pedwarn_c99 (loc, OPT_Wpedantic,
7521 "ISO C99 doesn%'t support unnamed structs/unions");
7522 else
7523 pedwarn_c99 (loc, OPT_Wpedantic,
7524 "ISO C90 doesn%'t support unnamed structs/unions");
7527 value = grokdeclarator (declarator, declspecs, FIELD, false,
7528 width ? &width : NULL, decl_attrs, NULL, NULL,
7529 DEPRECATED_NORMAL);
7531 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7532 DECL_INITIAL (value) = width;
7534 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7536 /* If we currently have a binding for this field, set the
7537 in_struct field in the binding, so that we warn about lookups
7538 which find it. */
7539 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7540 if (b != NULL)
7542 /* If the in_struct field is not yet set, push it on a list
7543 to be cleared when this struct is finished. */
7544 if (!b->in_struct)
7546 struct_parse_info->fields.safe_push (b);
7547 b->in_struct = 1;
7552 return value;
7555 /* Subroutine of detect_field_duplicates: return whether X and Y,
7556 which are both fields in the same struct, have duplicate field
7557 names. */
7559 static bool
7560 is_duplicate_field (tree x, tree y)
7562 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7563 return true;
7565 /* When using -fplan9-extensions, an anonymous field whose name is a
7566 typedef can duplicate a field name. */
7567 if (flag_plan9_extensions
7568 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7570 tree xt, xn, yt, yn;
7572 xt = TREE_TYPE (x);
7573 if (DECL_NAME (x) != NULL_TREE)
7574 xn = DECL_NAME (x);
7575 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
7576 && TYPE_NAME (xt) != NULL_TREE
7577 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7578 xn = DECL_NAME (TYPE_NAME (xt));
7579 else
7580 xn = NULL_TREE;
7582 yt = TREE_TYPE (y);
7583 if (DECL_NAME (y) != NULL_TREE)
7584 yn = DECL_NAME (y);
7585 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
7586 && TYPE_NAME (yt) != NULL_TREE
7587 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7588 yn = DECL_NAME (TYPE_NAME (yt));
7589 else
7590 yn = NULL_TREE;
7592 if (xn != NULL_TREE && xn == yn)
7593 return true;
7596 return false;
7599 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7600 to HTAB, giving errors for any duplicates. */
7602 static void
7603 detect_field_duplicates_hash (tree fieldlist,
7604 hash_table<pointer_hash <tree_node> > *htab)
7606 tree x, y;
7607 tree_node **slot;
7609 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7610 if ((y = DECL_NAME (x)) != 0)
7612 slot = htab->find_slot (y, INSERT);
7613 if (*slot)
7615 error ("duplicate member %q+D", x);
7616 DECL_NAME (x) = NULL_TREE;
7618 *slot = y;
7620 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7621 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7623 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7625 /* When using -fplan9-extensions, an anonymous field whose
7626 name is a typedef can duplicate a field name. */
7627 if (flag_plan9_extensions
7628 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7629 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7631 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7632 slot = htab->find_slot (xn, INSERT);
7633 if (*slot)
7634 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7635 *slot = xn;
7640 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7641 the list such that this does not present a problem later. */
7643 static void
7644 detect_field_duplicates (tree fieldlist)
7646 tree x, y;
7647 int timeout = 10;
7649 /* If the struct is the list of instance variables of an Objective-C
7650 class, then we need to check all the instance variables of
7651 superclasses when checking for duplicates (since you can't have
7652 an instance variable in a subclass with the same name as an
7653 instance variable in a superclass). We pass on this job to the
7654 Objective-C compiler. objc_detect_field_duplicates() will return
7655 false if we are not checking the list of instance variables and
7656 the C frontend should proceed with the standard field duplicate
7657 checks. If we are checking the list of instance variables, the
7658 ObjC frontend will do the check, emit the errors if needed, and
7659 then return true. */
7660 if (c_dialect_objc ())
7661 if (objc_detect_field_duplicates (false))
7662 return;
7664 /* First, see if there are more than "a few" fields.
7665 This is trivially true if there are zero or one fields. */
7666 if (!fieldlist || !DECL_CHAIN (fieldlist))
7667 return;
7668 x = fieldlist;
7669 do {
7670 timeout--;
7671 if (DECL_NAME (x) == NULL_TREE
7672 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7673 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7674 timeout = 0;
7675 x = DECL_CHAIN (x);
7676 } while (timeout > 0 && x);
7678 /* If there were "few" fields and no anonymous structures or unions,
7679 avoid the overhead of allocating a hash table. Instead just do
7680 the nested traversal thing. */
7681 if (timeout > 0)
7683 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7684 /* When using -fplan9-extensions, we can have duplicates
7685 between typedef names and fields. */
7686 if (DECL_NAME (x)
7687 || (flag_plan9_extensions
7688 && DECL_NAME (x) == NULL_TREE
7689 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7690 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7691 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7692 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7694 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7695 if (is_duplicate_field (y, x))
7697 error ("duplicate member %q+D", x);
7698 DECL_NAME (x) = NULL_TREE;
7702 else
7704 hash_table<pointer_hash <tree_node> > htab (37);
7705 detect_field_duplicates_hash (fieldlist, &htab);
7709 /* Finish up struct info used by -Wc++-compat. */
7711 static void
7712 warn_cxx_compat_finish_struct (tree fieldlist)
7714 unsigned int ix;
7715 tree x;
7716 struct c_binding *b;
7718 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7719 the current struct. We do this now at the end of the struct
7720 because the flag is used to issue visibility warnings, and we
7721 only want to issue those warnings if the type is referenced
7722 outside of the struct declaration. */
7723 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7724 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7726 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7727 typedefs used when declaring fields in this struct. If the name
7728 of any of the fields is also a typedef name then the struct would
7729 not parse in C++, because the C++ lookup rules say that the
7730 typedef name would be looked up in the context of the struct, and
7731 would thus be the field rather than the typedef. */
7732 if (!struct_parse_info->typedefs_seen.is_empty ()
7733 && fieldlist != NULL_TREE)
7735 /* Use a hash_set<tree> using the name of the typedef. We can use
7736 a hash_set<tree> because identifiers are interned. */
7737 hash_set<tree> tset;
7739 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7740 tset.add (DECL_NAME (x));
7742 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7744 if (DECL_NAME (x) != NULL_TREE
7745 && tset.contains (DECL_NAME (x)))
7747 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7748 ("using %qD as both field and typedef name is "
7749 "invalid in C++"),
7751 /* FIXME: It would be nice to report the location where
7752 the typedef name is used. */
7757 /* For each field which has a binding and which was not defined in
7758 an enclosing struct, clear the in_struct field. */
7759 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7760 b->in_struct = 0;
7763 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7764 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7765 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7766 ATTRIBUTES are attributes to be applied to the structure.
7768 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7769 the struct was started. */
7771 tree
7772 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7773 struct c_struct_parse_info *enclosing_struct_parse_info)
7775 tree x;
7776 bool toplevel = file_scope == current_scope;
7777 int saw_named_field;
7779 /* If this type was previously laid out as a forward reference,
7780 make sure we lay it out again. */
7782 TYPE_SIZE (t) = 0;
7784 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7786 if (pedantic)
7788 for (x = fieldlist; x; x = DECL_CHAIN (x))
7790 if (DECL_NAME (x) != 0)
7791 break;
7792 if (flag_isoc11
7793 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7794 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7795 break;
7798 if (x == 0)
7800 if (TREE_CODE (t) == UNION_TYPE)
7802 if (fieldlist)
7803 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7804 else
7805 pedwarn (loc, OPT_Wpedantic, "union has no members");
7807 else
7809 if (fieldlist)
7810 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7811 else
7812 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7817 /* Install struct as DECL_CONTEXT of each field decl.
7818 Also process specified field sizes, found in the DECL_INITIAL,
7819 storing 0 there after the type has been changed to precision equal
7820 to its width, rather than the precision of the specified standard
7821 type. (Correct layout requires the original type to have been preserved
7822 until now.) */
7824 saw_named_field = 0;
7825 for (x = fieldlist; x; x = DECL_CHAIN (x))
7827 if (TREE_TYPE (x) == error_mark_node)
7828 continue;
7830 DECL_CONTEXT (x) = t;
7832 /* If any field is const, the structure type is pseudo-const. */
7833 if (TREE_READONLY (x))
7834 C_TYPE_FIELDS_READONLY (t) = 1;
7835 else
7837 /* A field that is pseudo-const makes the structure likewise. */
7838 tree t1 = strip_array_types (TREE_TYPE (x));
7839 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7840 && C_TYPE_FIELDS_READONLY (t1))
7841 C_TYPE_FIELDS_READONLY (t) = 1;
7844 /* Any field that is volatile means variables of this type must be
7845 treated in some ways as volatile. */
7846 if (TREE_THIS_VOLATILE (x))
7847 C_TYPE_FIELDS_VOLATILE (t) = 1;
7849 /* Any field of nominal variable size implies structure is too. */
7850 if (C_DECL_VARIABLE_SIZE (x))
7851 C_TYPE_VARIABLE_SIZE (t) = 1;
7853 if (DECL_INITIAL (x))
7855 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7856 DECL_SIZE (x) = bitsize_int (width);
7857 DECL_BIT_FIELD (x) = 1;
7858 SET_DECL_C_BIT_FIELD (x);
7861 if (TYPE_PACKED (t)
7862 && (DECL_BIT_FIELD (x)
7863 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7864 DECL_PACKED (x) = 1;
7866 /* Detect flexible array member in an invalid context. */
7867 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7868 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7869 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7870 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7872 if (TREE_CODE (t) == UNION_TYPE)
7874 error_at (DECL_SOURCE_LOCATION (x),
7875 "flexible array member in union");
7876 TREE_TYPE (x) = error_mark_node;
7878 else if (DECL_CHAIN (x) != NULL_TREE)
7880 error_at (DECL_SOURCE_LOCATION (x),
7881 "flexible array member not at end of struct");
7882 TREE_TYPE (x) = error_mark_node;
7884 else if (!saw_named_field)
7886 error_at (DECL_SOURCE_LOCATION (x),
7887 "flexible array member in otherwise empty struct");
7888 TREE_TYPE (x) = error_mark_node;
7892 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7893 && flexible_array_type_p (TREE_TYPE (x)))
7894 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7895 "invalid use of structure with flexible array member");
7897 if (DECL_NAME (x)
7898 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7899 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7900 saw_named_field = 1;
7903 detect_field_duplicates (fieldlist);
7905 /* Now we have the nearly final fieldlist. Record it,
7906 then lay out the structure or union (including the fields). */
7908 TYPE_FIELDS (t) = fieldlist;
7910 layout_type (t);
7912 if (TYPE_SIZE_UNIT (t)
7913 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7914 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7915 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7916 error ("type %qT is too large", t);
7918 /* Give bit-fields their proper types. */
7920 tree *fieldlistp = &fieldlist;
7921 while (*fieldlistp)
7922 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7923 && TREE_TYPE (*fieldlistp) != error_mark_node)
7925 unsigned HOST_WIDE_INT width
7926 = tree_to_uhwi (DECL_INITIAL (*fieldlistp));
7927 tree type = TREE_TYPE (*fieldlistp);
7928 if (width != TYPE_PRECISION (type))
7930 TREE_TYPE (*fieldlistp)
7931 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7932 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7934 DECL_INITIAL (*fieldlistp) = 0;
7936 else
7937 fieldlistp = &DECL_CHAIN (*fieldlistp);
7940 /* Now we have the truly final field list.
7941 Store it in this type and in the variants. */
7943 TYPE_FIELDS (t) = fieldlist;
7945 /* If there are lots of fields, sort so we can look through them fast.
7946 We arbitrarily consider 16 or more elts to be "a lot". */
7949 int len = 0;
7951 for (x = fieldlist; x; x = DECL_CHAIN (x))
7953 if (len > 15 || DECL_NAME (x) == NULL)
7954 break;
7955 len += 1;
7958 if (len > 15)
7960 tree *field_array;
7961 struct lang_type *space;
7962 struct sorted_fields_type *space2;
7964 len += list_length (x);
7966 /* Use the same allocation policy here that make_node uses, to
7967 ensure that this lives as long as the rest of the struct decl.
7968 All decls in an inline function need to be saved. */
7970 space = ggc_cleared_alloc<struct lang_type> ();
7971 space2 = (sorted_fields_type *) ggc_internal_alloc
7972 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7974 len = 0;
7975 space->s = space2;
7976 field_array = &space2->elts[0];
7977 for (x = fieldlist; x; x = DECL_CHAIN (x))
7979 field_array[len++] = x;
7981 /* If there is anonymous struct or union, break out of the loop. */
7982 if (DECL_NAME (x) == NULL)
7983 break;
7985 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7986 if (x == NULL)
7988 TYPE_LANG_SPECIFIC (t) = space;
7989 TYPE_LANG_SPECIFIC (t)->s->len = len;
7990 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7991 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7996 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7998 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7999 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8000 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8001 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8002 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8005 /* If this was supposed to be a transparent union, but we can't
8006 make it one, warn and turn off the flag. */
8007 if (TREE_CODE (t) == UNION_TYPE
8008 && TYPE_TRANSPARENT_AGGR (t)
8009 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8011 TYPE_TRANSPARENT_AGGR (t) = 0;
8012 warning_at (loc, 0, "union cannot be made transparent");
8015 /* If this structure or union completes the type of any previous
8016 variable declaration, lay it out and output its rtl. */
8017 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8019 x = TREE_CHAIN (x))
8021 tree decl = TREE_VALUE (x);
8022 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8023 layout_array_type (TREE_TYPE (decl));
8024 if (TREE_CODE (decl) != TYPE_DECL)
8026 layout_decl (decl, 0);
8027 if (c_dialect_objc ())
8028 objc_check_decl (decl);
8029 rest_of_decl_compilation (decl, toplevel, 0);
8032 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
8034 /* Update type location to the one of the definition, instead of e.g.
8035 a forward declaration. */
8036 if (TYPE_STUB_DECL (t))
8037 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8039 /* Finish debugging output for this type. */
8040 rest_of_type_compilation (t, toplevel);
8042 /* If we're inside a function proper, i.e. not file-scope and not still
8043 parsing parameters, then arrange for the size of a variable sized type
8044 to be bound now. */
8045 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8046 add_stmt (build_stmt (loc,
8047 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8049 if (warn_cxx_compat)
8050 warn_cxx_compat_finish_struct (fieldlist);
8052 struct_parse_info->struct_types.release ();
8053 struct_parse_info->fields.release ();
8054 struct_parse_info->typedefs_seen.release ();
8055 XDELETE (struct_parse_info);
8057 struct_parse_info = enclosing_struct_parse_info;
8059 /* If this struct is defined inside a struct, add it to
8060 struct_types. */
8061 if (warn_cxx_compat
8062 && struct_parse_info != NULL
8063 && !in_sizeof && !in_typeof && !in_alignof)
8064 struct_parse_info->struct_types.safe_push (t);
8066 return t;
8069 /* Lay out the type T, and its element type, and so on. */
8071 static void
8072 layout_array_type (tree t)
8074 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8075 layout_array_type (TREE_TYPE (t));
8076 layout_type (t);
8079 /* Begin compiling the definition of an enumeration type.
8080 NAME is its name (or null if anonymous).
8081 LOC is the enum's location.
8082 Returns the type object, as yet incomplete.
8083 Also records info about it so that build_enumerator
8084 may be used to declare the individual values as they are read. */
8086 tree
8087 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8089 tree enumtype = NULL_TREE;
8090 location_t enumloc = UNKNOWN_LOCATION;
8092 /* If this is the real definition for a previous forward reference,
8093 fill in the contents in the same object that used to be the
8094 forward reference. */
8096 if (name != NULL_TREE)
8097 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
8099 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8101 enumtype = make_node (ENUMERAL_TYPE);
8102 pushtag (loc, name, enumtype);
8105 if (C_TYPE_BEING_DEFINED (enumtype))
8106 error_at (loc, "nested redefinition of %<enum %E%>", name);
8108 C_TYPE_BEING_DEFINED (enumtype) = 1;
8110 if (TYPE_VALUES (enumtype) != 0)
8112 /* This enum is a named one that has been declared already. */
8113 error_at (loc, "redeclaration of %<enum %E%>", name);
8114 if (enumloc != UNKNOWN_LOCATION)
8115 inform (enumloc, "originally defined here");
8117 /* Completely replace its old definition.
8118 The old enumerators remain defined, however. */
8119 TYPE_VALUES (enumtype) = 0;
8122 the_enum->enum_next_value = integer_zero_node;
8123 the_enum->enum_overflow = 0;
8125 if (flag_short_enums)
8126 TYPE_PACKED (enumtype) = 1;
8128 /* FIXME: This will issue a warning for a use of a type defined
8129 within sizeof in a statement expr. This is not terribly serious
8130 as C++ doesn't permit statement exprs within sizeof anyhow. */
8131 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8132 warning_at (loc, OPT_Wc___compat,
8133 "defining type in %qs expression is invalid in C++",
8134 (in_sizeof
8135 ? "sizeof"
8136 : (in_typeof ? "typeof" : "alignof")));
8138 return enumtype;
8141 /* After processing and defining all the values of an enumeration type,
8142 install their decls in the enumeration type and finish it off.
8143 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8144 and ATTRIBUTES are the specified attributes.
8145 Returns ENUMTYPE. */
8147 tree
8148 finish_enum (tree enumtype, tree values, tree attributes)
8150 tree pair, tem;
8151 tree minnode = 0, maxnode = 0;
8152 int precision;
8153 signop sign;
8154 bool toplevel = (file_scope == current_scope);
8155 struct lang_type *lt;
8157 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8159 /* Calculate the maximum value of any enumerator in this type. */
8161 if (values == error_mark_node)
8162 minnode = maxnode = integer_zero_node;
8163 else
8165 minnode = maxnode = TREE_VALUE (values);
8166 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8168 tree value = TREE_VALUE (pair);
8169 if (tree_int_cst_lt (maxnode, value))
8170 maxnode = value;
8171 if (tree_int_cst_lt (value, minnode))
8172 minnode = value;
8176 /* Construct the final type of this enumeration. It is the same
8177 as one of the integral types - the narrowest one that fits, except
8178 that normally we only go as narrow as int - and signed iff any of
8179 the values are negative. */
8180 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8181 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8182 tree_int_cst_min_precision (maxnode, sign));
8184 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
8186 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8187 if (tem == NULL)
8189 warning (0, "enumeration values exceed range of largest integer");
8190 tem = long_long_integer_type_node;
8193 else
8194 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8196 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8197 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8198 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8199 TYPE_SIZE (enumtype) = 0;
8201 /* If the precision of the type was specific with an attribute and it
8202 was too small, give an error. Otherwise, use it. */
8203 if (TYPE_PRECISION (enumtype))
8205 if (precision > TYPE_PRECISION (enumtype))
8206 error ("specified mode too small for enumeral values");
8208 else
8209 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8211 layout_type (enumtype);
8213 if (values != error_mark_node)
8215 /* Change the type of the enumerators to be the enum type. We
8216 need to do this irrespective of the size of the enum, for
8217 proper type checking. Replace the DECL_INITIALs of the
8218 enumerators, and the value slots of the list, with copies
8219 that have the enum type; they cannot be modified in place
8220 because they may be shared (e.g. integer_zero_node) Finally,
8221 change the purpose slots to point to the names of the decls. */
8222 for (pair = values; pair; pair = TREE_CHAIN (pair))
8224 tree enu = TREE_PURPOSE (pair);
8225 tree ini = DECL_INITIAL (enu);
8227 TREE_TYPE (enu) = enumtype;
8229 /* The ISO C Standard mandates enumerators to have type int,
8230 even though the underlying type of an enum type is
8231 unspecified. However, GCC allows enumerators of any
8232 integer type as an extensions. build_enumerator()
8233 converts any enumerators that fit in an int to type int,
8234 to avoid promotions to unsigned types when comparing
8235 integers with enumerators that fit in the int range.
8236 When -pedantic is given, build_enumerator() would have
8237 already warned about those that don't fit. Here we
8238 convert the rest to the enumerator type. */
8239 if (TREE_TYPE (ini) != integer_type_node)
8240 ini = convert (enumtype, ini);
8242 DECL_INITIAL (enu) = ini;
8243 TREE_PURPOSE (pair) = DECL_NAME (enu);
8244 TREE_VALUE (pair) = ini;
8247 TYPE_VALUES (enumtype) = values;
8250 /* Record the min/max values so that we can warn about bit-field
8251 enumerations that are too small for the values. */
8252 lt = ggc_cleared_alloc<struct lang_type> ();
8253 lt->enum_min = minnode;
8254 lt->enum_max = maxnode;
8255 TYPE_LANG_SPECIFIC (enumtype) = lt;
8257 /* Fix up all variant types of this enum type. */
8258 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8260 if (tem == enumtype)
8261 continue;
8262 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8263 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8264 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8265 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8266 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8267 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8268 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8269 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
8270 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8271 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8272 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8275 /* Finish debugging output for this type. */
8276 rest_of_type_compilation (enumtype, toplevel);
8278 /* If this enum is defined inside a struct, add it to
8279 struct_types. */
8280 if (warn_cxx_compat
8281 && struct_parse_info != NULL
8282 && !in_sizeof && !in_typeof && !in_alignof)
8283 struct_parse_info->struct_types.safe_push (enumtype);
8285 return enumtype;
8288 /* Build and install a CONST_DECL for one value of the
8289 current enumeration type (one that was begun with start_enum).
8290 DECL_LOC is the location of the enumerator.
8291 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8292 Return a tree-list containing the CONST_DECL and its value.
8293 Assignment of sequential values by default is handled here. */
8295 tree
8296 build_enumerator (location_t decl_loc, location_t loc,
8297 struct c_enum_contents *the_enum, tree name, tree value)
8299 tree decl, type;
8301 /* Validate and default VALUE. */
8303 if (value != 0)
8305 /* Don't issue more errors for error_mark_node (i.e. an
8306 undeclared identifier) - just ignore the value expression. */
8307 if (value == error_mark_node)
8308 value = 0;
8309 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8311 error_at (loc, "enumerator value for %qE is not an integer constant",
8312 name);
8313 value = 0;
8315 else
8317 if (TREE_CODE (value) != INTEGER_CST)
8319 value = c_fully_fold (value, false, NULL);
8320 if (TREE_CODE (value) == INTEGER_CST)
8321 pedwarn (loc, OPT_Wpedantic,
8322 "enumerator value for %qE is not an integer "
8323 "constant expression", name);
8325 if (TREE_CODE (value) != INTEGER_CST)
8327 error ("enumerator value for %qE is not an integer constant",
8328 name);
8329 value = 0;
8331 else
8333 value = default_conversion (value);
8334 constant_expression_warning (value);
8339 /* Default based on previous value. */
8340 /* It should no longer be possible to have NON_LVALUE_EXPR
8341 in the default. */
8342 if (value == 0)
8344 value = the_enum->enum_next_value;
8345 if (the_enum->enum_overflow)
8346 error_at (loc, "overflow in enumeration values");
8348 /* Even though the underlying type of an enum is unspecified, the
8349 type of enumeration constants is explicitly defined as int
8350 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8351 an extension. */
8352 else if (!int_fits_type_p (value, integer_type_node))
8353 pedwarn (loc, OPT_Wpedantic,
8354 "ISO C restricts enumerator values to range of %<int%>");
8356 /* The ISO C Standard mandates enumerators to have type int, even
8357 though the underlying type of an enum type is unspecified.
8358 However, GCC allows enumerators of any integer type as an
8359 extensions. Here we convert any enumerators that fit in an int
8360 to type int, to avoid promotions to unsigned types when comparing
8361 integers with enumerators that fit in the int range. When
8362 -pedantic is given, we would have already warned about those that
8363 don't fit. We have to do this here rather than in finish_enum
8364 because this value may be used to define more enumerators. */
8365 if (int_fits_type_p (value, integer_type_node))
8366 value = convert (integer_type_node, value);
8368 /* Set basis for default for next value. */
8369 the_enum->enum_next_value
8370 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8371 PLUS_EXPR, value, integer_one_node, 0);
8372 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8374 /* Now create a declaration for the enum value name. */
8376 type = TREE_TYPE (value);
8377 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8378 TYPE_PRECISION (integer_type_node)),
8379 (TYPE_PRECISION (type)
8380 >= TYPE_PRECISION (integer_type_node)
8381 && TYPE_UNSIGNED (type)));
8383 decl = build_decl (decl_loc, CONST_DECL, name, type);
8384 DECL_INITIAL (decl) = convert (type, value);
8385 pushdecl (decl);
8387 return tree_cons (decl, value, NULL_TREE);
8391 /* Create the FUNCTION_DECL for a function definition.
8392 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8393 the declaration; they describe the function's name and the type it returns,
8394 but twisted together in a fashion that parallels the syntax of C.
8396 This function creates a binding context for the function body
8397 as well as setting up the FUNCTION_DECL in current_function_decl.
8399 Returns 1 on success. If the DECLARATOR is not suitable for a function
8400 (it defines a datum instead), we return 0, which tells
8401 yyparse to report a parse error. */
8404 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8405 tree attributes)
8407 tree decl1, old_decl;
8408 tree restype, resdecl;
8409 location_t loc;
8411 current_function_returns_value = 0; /* Assume, until we see it does. */
8412 current_function_returns_null = 0;
8413 current_function_returns_abnormally = 0;
8414 warn_about_return_type = 0;
8415 c_switch_stack = NULL;
8417 /* Indicate no valid break/continue context by setting these variables
8418 to some non-null, non-label value. We'll notice and emit the proper
8419 error message in c_finish_bc_stmt. */
8420 c_break_label = c_cont_label = size_zero_node;
8422 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8423 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8425 /* If the declarator is not suitable for a function definition,
8426 cause a syntax error. */
8427 if (decl1 == 0
8428 || TREE_CODE (decl1) != FUNCTION_DECL)
8429 return 0;
8431 loc = DECL_SOURCE_LOCATION (decl1);
8433 c_decl_attributes (&decl1, attributes, 0);
8435 if (DECL_DECLARED_INLINE_P (decl1)
8436 && DECL_UNINLINABLE (decl1)
8437 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8438 warning_at (loc, OPT_Wattributes,
8439 "inline function %qD given attribute noinline",
8440 decl1);
8442 /* Handle gnu_inline attribute. */
8443 if (declspecs->inline_p
8444 && !flag_gnu89_inline
8445 && TREE_CODE (decl1) == FUNCTION_DECL
8446 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8447 || current_function_decl))
8449 if (declspecs->storage_class != csc_static)
8450 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8453 announce_function (decl1);
8455 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8457 error_at (loc, "return type is an incomplete type");
8458 /* Make it return void instead. */
8459 TREE_TYPE (decl1)
8460 = build_function_type (void_type_node,
8461 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8464 if (warn_about_return_type)
8465 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8466 : (warn_return_type ? OPT_Wreturn_type
8467 : OPT_Wimplicit_int),
8468 "return type defaults to %<int%>");
8470 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8471 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8472 DECL_INITIAL (decl1) = error_mark_node;
8474 /* A nested function is not global. */
8475 if (current_function_decl != 0)
8476 TREE_PUBLIC (decl1) = 0;
8478 /* If this definition isn't a prototype and we had a prototype declaration
8479 before, copy the arg type info from that prototype. */
8480 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8481 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8482 old_decl = 0;
8483 current_function_prototype_locus = UNKNOWN_LOCATION;
8484 current_function_prototype_built_in = false;
8485 current_function_prototype_arg_types = NULL_TREE;
8486 if (!prototype_p (TREE_TYPE (decl1)))
8488 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8489 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8490 TREE_TYPE (TREE_TYPE (old_decl))))
8492 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8493 TREE_TYPE (decl1));
8494 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8495 current_function_prototype_built_in
8496 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8497 current_function_prototype_arg_types
8498 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8500 if (TREE_PUBLIC (decl1))
8502 /* If there is an external prototype declaration of this
8503 function, record its location but do not copy information
8504 to this decl. This may be an invisible declaration
8505 (built-in or in a scope which has finished) or simply
8506 have more refined argument types than any declaration
8507 found above. */
8508 struct c_binding *b;
8509 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8510 if (B_IN_SCOPE (b, external_scope))
8511 break;
8512 if (b)
8514 tree ext_decl, ext_type;
8515 ext_decl = b->decl;
8516 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8517 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8518 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8519 TREE_TYPE (ext_type)))
8521 current_function_prototype_locus
8522 = DECL_SOURCE_LOCATION (ext_decl);
8523 current_function_prototype_built_in
8524 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8525 current_function_prototype_arg_types
8526 = TYPE_ARG_TYPES (ext_type);
8532 /* Optionally warn of old-fashioned def with no previous prototype. */
8533 if (warn_strict_prototypes
8534 && old_decl != error_mark_node
8535 && !prototype_p (TREE_TYPE (decl1))
8536 && C_DECL_ISNT_PROTOTYPE (old_decl))
8537 warning_at (loc, OPT_Wstrict_prototypes,
8538 "function declaration isn%'t a prototype");
8539 /* Optionally warn of any global def with no previous prototype. */
8540 else if (warn_missing_prototypes
8541 && old_decl != error_mark_node
8542 && TREE_PUBLIC (decl1)
8543 && !MAIN_NAME_P (DECL_NAME (decl1))
8544 && C_DECL_ISNT_PROTOTYPE (old_decl)
8545 && !DECL_DECLARED_INLINE_P (decl1))
8546 warning_at (loc, OPT_Wmissing_prototypes,
8547 "no previous prototype for %qD", decl1);
8548 /* Optionally warn of any def with no previous prototype
8549 if the function has already been used. */
8550 else if (warn_missing_prototypes
8551 && old_decl != 0
8552 && old_decl != error_mark_node
8553 && TREE_USED (old_decl)
8554 && !prototype_p (TREE_TYPE (old_decl)))
8555 warning_at (loc, OPT_Wmissing_prototypes,
8556 "%qD was used with no prototype before its definition", decl1);
8557 /* Optionally warn of any global def with no previous declaration. */
8558 else if (warn_missing_declarations
8559 && TREE_PUBLIC (decl1)
8560 && old_decl == 0
8561 && !MAIN_NAME_P (DECL_NAME (decl1))
8562 && !DECL_DECLARED_INLINE_P (decl1))
8563 warning_at (loc, OPT_Wmissing_declarations,
8564 "no previous declaration for %qD",
8565 decl1);
8566 /* Optionally warn of any def with no previous declaration
8567 if the function has already been used. */
8568 else if (warn_missing_declarations
8569 && old_decl != 0
8570 && old_decl != error_mark_node
8571 && TREE_USED (old_decl)
8572 && C_DECL_IMPLICIT (old_decl))
8573 warning_at (loc, OPT_Wmissing_declarations,
8574 "%qD was used with no declaration before its definition", decl1);
8576 /* This function exists in static storage.
8577 (This does not mean `static' in the C sense!) */
8578 TREE_STATIC (decl1) = 1;
8580 /* This is the earliest point at which we might know the assembler
8581 name of the function. Thus, if it's set before this, die horribly. */
8582 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8584 /* If #pragma weak was used, mark the decl weak now. */
8585 if (current_scope == file_scope)
8586 maybe_apply_pragma_weak (decl1);
8588 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8589 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8591 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8592 != integer_type_node)
8593 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8594 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8595 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8596 decl1);
8598 check_main_parameter_types (decl1);
8600 if (!TREE_PUBLIC (decl1))
8601 pedwarn (loc, OPT_Wmain,
8602 "%qD is normally a non-static function", decl1);
8605 /* Record the decl so that the function name is defined.
8606 If we already have a decl for this name, and it is a FUNCTION_DECL,
8607 use the old decl. */
8609 current_function_decl = pushdecl (decl1);
8611 push_scope ();
8612 declare_parm_level ();
8614 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8615 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8616 DECL_ARTIFICIAL (resdecl) = 1;
8617 DECL_IGNORED_P (resdecl) = 1;
8618 DECL_RESULT (current_function_decl) = resdecl;
8620 start_fname_decls ();
8622 return 1;
8625 /* Subroutine of store_parm_decls which handles new-style function
8626 definitions (prototype format). The parms already have decls, so we
8627 need only record them as in effect and complain if any redundant
8628 old-style parm decls were written. */
8629 static void
8630 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8632 tree decl;
8633 c_arg_tag *tag;
8634 unsigned ix;
8636 if (current_scope->bindings)
8638 error_at (DECL_SOURCE_LOCATION (fndecl),
8639 "old-style parameter declarations in prototyped "
8640 "function definition");
8642 /* Get rid of the old-style declarations. */
8643 pop_scope ();
8644 push_scope ();
8646 /* Don't issue this warning for nested functions, and don't issue this
8647 warning if we got here because ARG_INFO_TYPES was error_mark_node
8648 (this happens when a function definition has just an ellipsis in
8649 its parameter list). */
8650 else if (!in_system_header_at (input_location)
8651 && !current_function_scope
8652 && arg_info->types != error_mark_node)
8653 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8654 "traditional C rejects ISO C style function definitions");
8656 /* Now make all the parameter declarations visible in the function body.
8657 We can bypass most of the grunt work of pushdecl. */
8658 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8660 DECL_CONTEXT (decl) = current_function_decl;
8661 if (DECL_NAME (decl))
8663 bind (DECL_NAME (decl), decl, current_scope,
8664 /*invisible=*/false, /*nested=*/false,
8665 UNKNOWN_LOCATION);
8666 if (!TREE_USED (decl))
8667 warn_if_shadowing (decl);
8669 else
8670 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8673 /* Record the parameter list in the function declaration. */
8674 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8676 /* Now make all the ancillary declarations visible, likewise. */
8677 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8679 DECL_CONTEXT (decl) = current_function_decl;
8680 if (DECL_NAME (decl))
8681 bind (DECL_NAME (decl), decl, current_scope,
8682 /*invisible=*/false,
8683 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8684 UNKNOWN_LOCATION);
8687 /* And all the tag declarations. */
8688 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8689 if (tag->id)
8690 bind (tag->id, tag->type, current_scope,
8691 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8694 /* Subroutine of store_parm_decls which handles old-style function
8695 definitions (separate parameter list and declarations). */
8697 static void
8698 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8700 struct c_binding *b;
8701 tree parm, decl, last;
8702 tree parmids = arg_info->parms;
8703 hash_set<tree> seen_args;
8705 if (!in_system_header_at (input_location))
8706 warning_at (DECL_SOURCE_LOCATION (fndecl),
8707 OPT_Wold_style_definition, "old-style function definition");
8709 /* Match each formal parameter name with its declaration. Save each
8710 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8711 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8713 if (TREE_VALUE (parm) == 0)
8715 error_at (DECL_SOURCE_LOCATION (fndecl),
8716 "parameter name missing from parameter list");
8717 TREE_PURPOSE (parm) = 0;
8718 continue;
8721 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8722 if (b && B_IN_CURRENT_SCOPE (b))
8724 decl = b->decl;
8725 /* Skip erroneous parameters. */
8726 if (decl == error_mark_node)
8727 continue;
8728 /* If we got something other than a PARM_DECL it is an error. */
8729 if (TREE_CODE (decl) != PARM_DECL)
8730 error_at (DECL_SOURCE_LOCATION (decl),
8731 "%qD declared as a non-parameter", decl);
8732 /* If the declaration is already marked, we have a duplicate
8733 name. Complain and ignore the duplicate. */
8734 else if (seen_args.contains (decl))
8736 error_at (DECL_SOURCE_LOCATION (decl),
8737 "multiple parameters named %qD", decl);
8738 TREE_PURPOSE (parm) = 0;
8739 continue;
8741 /* If the declaration says "void", complain and turn it into
8742 an int. */
8743 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8745 error_at (DECL_SOURCE_LOCATION (decl),
8746 "parameter %qD declared with void type", decl);
8747 TREE_TYPE (decl) = integer_type_node;
8748 DECL_ARG_TYPE (decl) = integer_type_node;
8749 layout_decl (decl, 0);
8751 warn_if_shadowing (decl);
8753 /* If no declaration found, default to int. */
8754 else
8756 /* FIXME diagnostics: This should be the location of the argument,
8757 not the FNDECL. E.g., for an old-style declaration
8759 int f10(v) { blah; }
8761 We should use the location of the V, not the F10.
8762 Unfortunately, the V is an IDENTIFIER_NODE which has no
8763 location. In the future we need locations for c_arg_info
8764 entries.
8766 See gcc.dg/Wshadow-3.c for an example of this problem. */
8767 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8768 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8769 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8770 pushdecl (decl);
8771 warn_if_shadowing (decl);
8773 if (flag_isoc99)
8774 pedwarn (DECL_SOURCE_LOCATION (decl),
8775 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8776 decl);
8777 else
8778 warning_at (DECL_SOURCE_LOCATION (decl),
8779 OPT_Wmissing_parameter_type,
8780 "type of %qD defaults to %<int%>", decl);
8783 TREE_PURPOSE (parm) = decl;
8784 seen_args.add (decl);
8787 /* Now examine the parms chain for incomplete declarations
8788 and declarations with no corresponding names. */
8790 for (b = current_scope->bindings; b; b = b->prev)
8792 parm = b->decl;
8793 if (TREE_CODE (parm) != PARM_DECL)
8794 continue;
8796 if (TREE_TYPE (parm) != error_mark_node
8797 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8799 error_at (DECL_SOURCE_LOCATION (parm),
8800 "parameter %qD has incomplete type", parm);
8801 TREE_TYPE (parm) = error_mark_node;
8804 if (!seen_args.contains (parm))
8806 error_at (DECL_SOURCE_LOCATION (parm),
8807 "declaration for parameter %qD but no such parameter",
8808 parm);
8810 /* Pretend the parameter was not missing.
8811 This gets us to a standard state and minimizes
8812 further error messages. */
8813 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8817 /* Chain the declarations together in the order of the list of
8818 names. Store that chain in the function decl, replacing the
8819 list of names. Update the current scope to match. */
8820 DECL_ARGUMENTS (fndecl) = 0;
8822 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8823 if (TREE_PURPOSE (parm))
8824 break;
8825 if (parm && TREE_PURPOSE (parm))
8827 last = TREE_PURPOSE (parm);
8828 DECL_ARGUMENTS (fndecl) = last;
8830 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8831 if (TREE_PURPOSE (parm))
8833 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8834 last = TREE_PURPOSE (parm);
8836 DECL_CHAIN (last) = 0;
8839 /* If there was a previous prototype,
8840 set the DECL_ARG_TYPE of each argument according to
8841 the type previously specified, and report any mismatches. */
8843 if (current_function_prototype_arg_types)
8845 tree type;
8846 for (parm = DECL_ARGUMENTS (fndecl),
8847 type = current_function_prototype_arg_types;
8848 parm || (type && TREE_VALUE (type) != error_mark_node
8849 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8850 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8852 if (parm == 0 || type == 0
8853 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8855 if (current_function_prototype_built_in)
8856 warning_at (DECL_SOURCE_LOCATION (fndecl),
8857 0, "number of arguments doesn%'t match "
8858 "built-in prototype");
8859 else
8861 /* FIXME diagnostics: This should be the location of
8862 FNDECL, but there is bug when a prototype is
8863 declared inside function context, but defined
8864 outside of it (e.g., gcc.dg/pr15698-2.c). In
8865 which case FNDECL gets the location of the
8866 prototype, not the definition. */
8867 error_at (input_location,
8868 "number of arguments doesn%'t match prototype");
8870 error_at (current_function_prototype_locus,
8871 "prototype declaration");
8873 break;
8875 /* Type for passing arg must be consistent with that
8876 declared for the arg. ISO C says we take the unqualified
8877 type for parameters declared with qualified type. */
8878 if (TREE_TYPE (parm) != error_mark_node
8879 && TREE_TYPE (type) != error_mark_node
8880 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8881 != TYPE_ATOMIC (TREE_VALUE (type)))
8882 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8883 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8885 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8886 == TYPE_ATOMIC (TREE_VALUE (type)))
8887 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8888 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8890 /* Adjust argument to match prototype. E.g. a previous
8891 `int foo(float);' prototype causes
8892 `int foo(x) float x; {...}' to be treated like
8893 `int foo(float x) {...}'. This is particularly
8894 useful for argument types like uid_t. */
8895 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8897 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8898 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8899 && TYPE_PRECISION (TREE_TYPE (parm))
8900 < TYPE_PRECISION (integer_type_node))
8901 DECL_ARG_TYPE (parm)
8902 = c_type_promotes_to (TREE_TYPE (parm));
8904 /* ??? Is it possible to get here with a
8905 built-in prototype or will it always have
8906 been diagnosed as conflicting with an
8907 old-style definition and discarded? */
8908 if (current_function_prototype_built_in)
8909 warning_at (DECL_SOURCE_LOCATION (parm),
8910 OPT_Wpedantic, "promoted argument %qD "
8911 "doesn%'t match built-in prototype", parm);
8912 else
8914 pedwarn (DECL_SOURCE_LOCATION (parm),
8915 OPT_Wpedantic, "promoted argument %qD "
8916 "doesn%'t match prototype", parm);
8917 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8918 "prototype declaration");
8921 else
8923 if (current_function_prototype_built_in)
8924 warning_at (DECL_SOURCE_LOCATION (parm),
8925 0, "argument %qD doesn%'t match "
8926 "built-in prototype", parm);
8927 else
8929 error_at (DECL_SOURCE_LOCATION (parm),
8930 "argument %qD doesn%'t match prototype", parm);
8931 error_at (current_function_prototype_locus,
8932 "prototype declaration");
8937 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8940 /* Otherwise, create a prototype that would match. */
8942 else
8944 tree actual = 0, last = 0, type;
8946 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8948 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8949 if (last)
8950 TREE_CHAIN (last) = type;
8951 else
8952 actual = type;
8953 last = type;
8955 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8956 if (last)
8957 TREE_CHAIN (last) = type;
8958 else
8959 actual = type;
8961 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8962 of the type of this function, but we need to avoid having this
8963 affect the types of other similarly-typed functions, so we must
8964 first force the generation of an identical (but separate) type
8965 node for the relevant function type. The new node we create
8966 will be a variant of the main variant of the original function
8967 type. */
8969 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8971 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8975 /* Store parameter declarations passed in ARG_INFO into the current
8976 function declaration. */
8978 void
8979 store_parm_decls_from (struct c_arg_info *arg_info)
8981 current_function_arg_info = arg_info;
8982 store_parm_decls ();
8985 /* Store the parameter declarations into the current function declaration.
8986 This is called after parsing the parameter declarations, before
8987 digesting the body of the function.
8989 For an old-style definition, construct a prototype out of the old-style
8990 parameter declarations and inject it into the function's type. */
8992 void
8993 store_parm_decls (void)
8995 tree fndecl = current_function_decl;
8996 bool proto;
8998 /* The argument information block for FNDECL. */
8999 struct c_arg_info *arg_info = current_function_arg_info;
9000 current_function_arg_info = 0;
9002 /* True if this definition is written with a prototype. Note:
9003 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9004 list in a function definition as equivalent to (void) -- an
9005 empty argument list specifies the function has no parameters,
9006 but only (void) sets up a prototype for future calls. */
9007 proto = arg_info->types != 0;
9009 if (proto)
9010 store_parm_decls_newstyle (fndecl, arg_info);
9011 else
9012 store_parm_decls_oldstyle (fndecl, arg_info);
9014 /* The next call to push_scope will be a function body. */
9016 next_is_function_body = true;
9018 /* Write a record describing this function definition to the prototypes
9019 file (if requested). */
9021 gen_aux_info_record (fndecl, 1, 0, proto);
9023 /* Initialize the RTL code for the function. */
9024 allocate_struct_function (fndecl, false);
9026 if (warn_unused_local_typedefs)
9027 cfun->language = ggc_cleared_alloc<language_function> ();
9029 /* Begin the statement tree for this function. */
9030 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9032 /* ??? Insert the contents of the pending sizes list into the function
9033 to be evaluated. The only reason left to have this is
9034 void foo(int n, int array[n++])
9035 because we throw away the array type in favor of a pointer type, and
9036 thus won't naturally see the SAVE_EXPR containing the increment. All
9037 other pending sizes would be handled by gimplify_parameters. */
9038 if (arg_info->pending_sizes)
9039 add_stmt (arg_info->pending_sizes);
9042 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9043 c_finish_omp_declare_simd for function prototypes. No diagnostics
9044 should be done. */
9046 void
9047 temp_store_parm_decls (tree fndecl, tree parms)
9049 push_scope ();
9050 for (tree p = parms; p; p = DECL_CHAIN (p))
9052 DECL_CONTEXT (p) = fndecl;
9053 if (DECL_NAME (p))
9054 bind (DECL_NAME (p), p, current_scope,
9055 /*invisible=*/false, /*nested=*/false,
9056 UNKNOWN_LOCATION);
9060 /* Undo what temp_store_parm_decls did. */
9062 void
9063 temp_pop_parm_decls (void)
9065 /* Clear all bindings in this temporary scope, so that
9066 pop_scope doesn't create a BLOCK. */
9067 struct c_binding *b = current_scope->bindings;
9068 current_scope->bindings = NULL;
9069 for (; b; b = free_binding_and_advance (b))
9071 gcc_assert (TREE_CODE (b->decl) == PARM_DECL);
9072 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9073 I_SYMBOL_BINDING (b->id) = b->shadowed;
9074 if (b->shadowed && b->shadowed->u.type)
9075 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9077 pop_scope ();
9081 /* Finish up a function declaration and compile that function
9082 all the way to assembler language output. Then free the storage
9083 for the function definition.
9085 This is called after parsing the body of the function definition. */
9087 void
9088 finish_function (void)
9090 tree fndecl = current_function_decl;
9092 if (c_dialect_objc ())
9093 objc_finish_function ();
9095 if (TREE_CODE (fndecl) == FUNCTION_DECL
9096 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9098 tree args = DECL_ARGUMENTS (fndecl);
9099 for (; args; args = DECL_CHAIN (args))
9101 tree type = TREE_TYPE (args);
9102 if (INTEGRAL_TYPE_P (type)
9103 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9104 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9108 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9109 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9111 /* Must mark the RESULT_DECL as being in this function. */
9113 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9114 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9116 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9117 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9118 == integer_type_node && flag_isoc99)
9120 /* Hack. We don't want the middle-end to warn that this return
9121 is unreachable, so we mark its location as special. Using
9122 UNKNOWN_LOCATION has the problem that it gets clobbered in
9123 annotate_one_with_locus. A cleaner solution might be to
9124 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9126 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9129 /* Tie off the statement tree for this function. */
9130 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9132 /* If the function has _Cilk_spawn in front of a function call inside it
9133 i.e. it is a spawning function, then add the appropriate Cilk plus
9134 functions inside. */
9135 if (fn_contains_cilk_spawn_p (cfun))
9136 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9138 finish_fname_decls ();
9140 /* Complain if there's just no return statement. */
9141 if (warn_return_type
9142 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9143 && !current_function_returns_value && !current_function_returns_null
9144 /* Don't complain if we are no-return. */
9145 && !current_function_returns_abnormally
9146 /* Don't complain if we are declared noreturn. */
9147 && !TREE_THIS_VOLATILE (fndecl)
9148 /* Don't warn for main(). */
9149 && !MAIN_NAME_P (DECL_NAME (fndecl))
9150 /* Or if they didn't actually specify a return type. */
9151 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9152 /* Normally, with -Wreturn-type, flow will complain, but we might
9153 optimize out static functions. */
9154 && !TREE_PUBLIC (fndecl))
9156 warning (OPT_Wreturn_type,
9157 "no return statement in function returning non-void");
9158 TREE_NO_WARNING (fndecl) = 1;
9161 /* Complain about parameters that are only set, but never otherwise used. */
9162 if (warn_unused_but_set_parameter)
9164 tree decl;
9166 for (decl = DECL_ARGUMENTS (fndecl);
9167 decl;
9168 decl = DECL_CHAIN (decl))
9169 if (TREE_USED (decl)
9170 && TREE_CODE (decl) == PARM_DECL
9171 && !DECL_READ_P (decl)
9172 && DECL_NAME (decl)
9173 && !DECL_ARTIFICIAL (decl)
9174 && !TREE_NO_WARNING (decl))
9175 warning_at (DECL_SOURCE_LOCATION (decl),
9176 OPT_Wunused_but_set_parameter,
9177 "parameter %qD set but not used", decl);
9180 /* Complain about locally defined typedefs that are not used in this
9181 function. */
9182 maybe_warn_unused_local_typedefs ();
9184 /* Store the end of the function, so that we get good line number
9185 info for the epilogue. */
9186 cfun->function_end_locus = input_location;
9188 /* Finalize the ELF visibility for the function. */
9189 c_determine_visibility (fndecl);
9191 /* For GNU C extern inline functions disregard inline limits. */
9192 if (DECL_EXTERNAL (fndecl)
9193 && DECL_DECLARED_INLINE_P (fndecl))
9194 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9196 /* Genericize before inlining. Delay genericizing nested functions
9197 until their parent function is genericized. Since finalizing
9198 requires GENERIC, delay that as well. */
9200 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9201 && !undef_nested_function)
9203 if (!decl_function_context (fndecl))
9205 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9206 c_genericize (fndecl);
9208 /* ??? Objc emits functions after finalizing the compilation unit.
9209 This should be cleaned up later and this conditional removed. */
9210 if (symtab->global_info_ready)
9212 cgraph_node::add_new_function (fndecl, false);
9213 return;
9215 cgraph_node::finalize_function (fndecl, false);
9217 else
9219 /* Register this function with cgraph just far enough to get it
9220 added to our parent's nested function list. Handy, since the
9221 C front end doesn't have such a list. */
9222 (void) cgraph_node::get_create (fndecl);
9226 if (!decl_function_context (fndecl))
9227 undef_nested_function = false;
9229 if (cfun->language != NULL)
9231 ggc_free (cfun->language);
9232 cfun->language = NULL;
9235 /* We're leaving the context of this function, so zap cfun.
9236 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9237 tree_rest_of_compilation. */
9238 set_cfun (NULL);
9239 current_function_decl = NULL;
9242 /* Check the declarations given in a for-loop for satisfying the C99
9243 constraints. If exactly one such decl is found, return it. LOC is
9244 the location of the opening parenthesis of the for loop. The last
9245 parameter allows you to control the "for loop initial declarations
9246 are only allowed in C99 mode". Normally, you should pass
9247 flag_isoc99 as that parameter. But in some cases (Objective-C
9248 foreach loop, for example) we want to run the checks in this
9249 function even if not in C99 mode, so we allow the caller to turn
9250 off the error about not being in C99 mode.
9253 tree
9254 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9256 struct c_binding *b;
9257 tree one_decl = NULL_TREE;
9258 int n_decls = 0;
9260 if (!turn_off_iso_c99_error)
9262 static bool hint = true;
9263 /* If we get here, declarations have been used in a for loop without
9264 the C99 for loop scope. This doesn't make much sense, so don't
9265 allow it. */
9266 error_at (loc, "%<for%> loop initial declarations "
9267 "are only allowed in C99 or C11 mode");
9268 if (hint)
9270 inform (loc,
9271 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9272 "to compile your code");
9273 hint = false;
9275 return NULL_TREE;
9277 /* C99 subclause 6.8.5 paragraph 3:
9279 [#3] The declaration part of a for statement shall only
9280 declare identifiers for objects having storage class auto or
9281 register.
9283 It isn't clear whether, in this sentence, "identifiers" binds to
9284 "shall only declare" or to "objects" - that is, whether all identifiers
9285 declared must be identifiers for objects, or whether the restriction
9286 only applies to those that are. (A question on this in comp.std.c
9287 in November 2000 received no answer.) We implement the strictest
9288 interpretation, to avoid creating an extension which later causes
9289 problems. */
9291 for (b = current_scope->bindings; b; b = b->prev)
9293 tree id = b->id;
9294 tree decl = b->decl;
9296 if (!id)
9297 continue;
9299 switch (TREE_CODE (decl))
9301 case VAR_DECL:
9303 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9304 if (TREE_STATIC (decl))
9305 error_at (decl_loc,
9306 "declaration of static variable %qD in %<for%> loop "
9307 "initial declaration", decl);
9308 else if (DECL_EXTERNAL (decl))
9309 error_at (decl_loc,
9310 "declaration of %<extern%> variable %qD in %<for%> loop "
9311 "initial declaration", decl);
9313 break;
9315 case RECORD_TYPE:
9316 error_at (loc,
9317 "%<struct %E%> declared in %<for%> loop initial "
9318 "declaration", id);
9319 break;
9320 case UNION_TYPE:
9321 error_at (loc,
9322 "%<union %E%> declared in %<for%> loop initial declaration",
9323 id);
9324 break;
9325 case ENUMERAL_TYPE:
9326 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9327 "initial declaration", id);
9328 break;
9329 default:
9330 error_at (loc, "declaration of non-variable "
9331 "%qD in %<for%> loop initial declaration", decl);
9334 n_decls++;
9335 one_decl = decl;
9338 return n_decls == 1 ? one_decl : NULL_TREE;
9341 /* Save and reinitialize the variables
9342 used during compilation of a C function. */
9344 void
9345 c_push_function_context (void)
9347 struct language_function *p = cfun->language;
9348 /* cfun->language might have been already allocated by the use of
9349 -Wunused-local-typedefs. In that case, just re-use it. */
9350 if (p == NULL)
9351 cfun->language = p = ggc_cleared_alloc<language_function> ();
9353 p->base.x_stmt_tree = c_stmt_tree;
9354 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9355 p->x_break_label = c_break_label;
9356 p->x_cont_label = c_cont_label;
9357 p->x_switch_stack = c_switch_stack;
9358 p->arg_info = current_function_arg_info;
9359 p->returns_value = current_function_returns_value;
9360 p->returns_null = current_function_returns_null;
9361 p->returns_abnormally = current_function_returns_abnormally;
9362 p->warn_about_return_type = warn_about_return_type;
9364 push_function_context ();
9367 /* Restore the variables used during compilation of a C function. */
9369 void
9370 c_pop_function_context (void)
9372 struct language_function *p;
9374 pop_function_context ();
9375 p = cfun->language;
9377 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9378 used to store data throughout the life time of the current cfun,
9379 So don't deallocate it. */
9380 if (!warn_unused_local_typedefs)
9381 cfun->language = NULL;
9383 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9384 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9386 /* Stop pointing to the local nodes about to be freed. */
9387 /* But DECL_INITIAL must remain nonzero so we know this
9388 was an actual function definition. */
9389 DECL_INITIAL (current_function_decl) = error_mark_node;
9390 DECL_ARGUMENTS (current_function_decl) = 0;
9393 c_stmt_tree = p->base.x_stmt_tree;
9394 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9395 c_break_label = p->x_break_label;
9396 c_cont_label = p->x_cont_label;
9397 c_switch_stack = p->x_switch_stack;
9398 current_function_arg_info = p->arg_info;
9399 current_function_returns_value = p->returns_value;
9400 current_function_returns_null = p->returns_null;
9401 current_function_returns_abnormally = p->returns_abnormally;
9402 warn_about_return_type = p->warn_about_return_type;
9405 /* The functions below are required for functionality of doing
9406 function at once processing in the C front end. Currently these
9407 functions are not called from anywhere in the C front end, but as
9408 these changes continue, that will change. */
9410 /* Returns the stmt_tree (if any) to which statements are currently
9411 being added. If there is no active statement-tree, NULL is
9412 returned. */
9414 stmt_tree
9415 current_stmt_tree (void)
9417 return &c_stmt_tree;
9420 /* Return the global value of T as a symbol. */
9422 tree
9423 identifier_global_value (tree t)
9425 struct c_binding *b;
9427 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9428 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9429 return b->decl;
9431 return 0;
9434 /* In C, the only C-linkage public declaration is at file scope. */
9436 tree
9437 c_linkage_bindings (tree name)
9439 return identifier_global_value (name);
9442 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9443 otherwise the name is found in ridpointers from RID_INDEX. */
9445 void
9446 record_builtin_type (enum rid rid_index, const char *name, tree type)
9448 tree id, decl;
9449 if (name == 0)
9450 id = ridpointers[(int) rid_index];
9451 else
9452 id = get_identifier (name);
9453 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9454 pushdecl (decl);
9455 if (debug_hooks->type_decl)
9456 debug_hooks->type_decl (decl, false);
9459 /* Build the void_list_node (void_type_node having been created). */
9460 tree
9461 build_void_list_node (void)
9463 tree t = build_tree_list (NULL_TREE, void_type_node);
9464 return t;
9467 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9469 struct c_parm *
9470 build_c_parm (struct c_declspecs *specs, tree attrs,
9471 struct c_declarator *declarator)
9473 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9474 ret->specs = specs;
9475 ret->attrs = attrs;
9476 ret->declarator = declarator;
9477 return ret;
9480 /* Return a declarator with nested attributes. TARGET is the inner
9481 declarator to which these attributes apply. ATTRS are the
9482 attributes. */
9484 struct c_declarator *
9485 build_attrs_declarator (tree attrs, struct c_declarator *target)
9487 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9488 ret->kind = cdk_attrs;
9489 ret->declarator = target;
9490 ret->u.attrs = attrs;
9491 return ret;
9494 /* Return a declarator for a function with arguments specified by ARGS
9495 and return type specified by TARGET. */
9497 struct c_declarator *
9498 build_function_declarator (struct c_arg_info *args,
9499 struct c_declarator *target)
9501 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9502 ret->kind = cdk_function;
9503 ret->declarator = target;
9504 ret->u.arg_info = args;
9505 return ret;
9508 /* Return a declarator for the identifier IDENT (which may be
9509 NULL_TREE for an abstract declarator). */
9511 struct c_declarator *
9512 build_id_declarator (tree ident)
9514 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9515 ret->kind = cdk_id;
9516 ret->declarator = 0;
9517 ret->u.id = ident;
9518 /* Default value - may get reset to a more precise location. */
9519 ret->id_loc = input_location;
9520 return ret;
9523 /* Return something to represent absolute declarators containing a *.
9524 TARGET is the absolute declarator that the * contains.
9525 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9526 to apply to the pointer type. */
9528 struct c_declarator *
9529 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9530 struct c_declarator *target)
9532 tree attrs;
9533 int quals = 0;
9534 tree upc_layout_qual = 0;
9535 struct c_declarator *itarget = target;
9536 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9537 if (type_quals_attrs)
9539 attrs = type_quals_attrs->attrs;
9540 quals = quals_from_declspecs (type_quals_attrs);
9541 upc_layout_qual = type_quals_attrs->upc_layout_qualifier;
9542 if (attrs != NULL_TREE)
9543 itarget = build_attrs_declarator (attrs, target);
9545 ret->kind = cdk_pointer;
9546 ret->declarator = itarget;
9547 ret->u.pointer.quals = quals;
9548 ret->u.pointer.upc_layout_qual = upc_layout_qual;
9549 return ret;
9552 /* Return a pointer to a structure for an empty list of declaration
9553 specifiers. */
9555 struct c_declspecs *
9556 build_null_declspecs (void)
9558 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9559 memset (&ret->locations, 0, cdw_number_of_elements);
9560 ret->type = 0;
9561 ret->expr = 0;
9562 ret->decl_attr = 0;
9563 ret->attrs = 0;
9564 ret->upc_layout_qualifier = 0;
9565 ret->align_log = -1;
9566 ret->typespec_word = cts_none;
9567 ret->storage_class = csc_none;
9568 ret->expr_const_operands = true;
9569 ret->declspecs_seen_p = false;
9570 ret->typespec_kind = ctsk_none;
9571 ret->non_sc_seen_p = false;
9572 ret->typedef_p = false;
9573 ret->explicit_signed_p = false;
9574 ret->deprecated_p = false;
9575 ret->default_int_p = false;
9576 ret->long_p = false;
9577 ret->long_long_p = false;
9578 ret->short_p = false;
9579 ret->signed_p = false;
9580 ret->unsigned_p = false;
9581 ret->complex_p = false;
9582 ret->inline_p = false;
9583 ret->noreturn_p = false;
9584 ret->thread_p = false;
9585 ret->thread_gnu_p = false;
9586 ret->const_p = false;
9587 ret->volatile_p = false;
9588 ret->atomic_p = false;
9589 ret->restrict_p = false;
9590 ret->shared_p = false;
9591 ret->strict_p = false;
9592 ret->relaxed_p = false;
9593 ret->saturating_p = false;
9594 ret->alignas_p = false;
9595 ret->address_space = ADDR_SPACE_GENERIC;
9596 return ret;
9599 /* Add the address space ADDRSPACE to the declaration specifiers
9600 SPECS, returning SPECS. */
9602 struct c_declspecs *
9603 declspecs_add_addrspace (source_location location,
9604 struct c_declspecs *specs, addr_space_t as)
9606 specs->non_sc_seen_p = true;
9607 specs->declspecs_seen_p = true;
9609 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9610 && specs->address_space != as)
9611 error ("incompatible address space qualifiers %qs and %qs",
9612 c_addr_space_name (as),
9613 c_addr_space_name (specs->address_space));
9614 else
9616 specs->address_space = as;
9617 specs->locations[cdw_address_space] = location;
9619 return specs;
9622 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9623 returning SPECS. */
9625 struct c_declspecs *
9626 declspecs_add_qual (source_location loc,
9627 struct c_declspecs *specs, tree qual)
9629 enum rid i;
9630 bool dupe = false;
9631 specs->non_sc_seen_p = true;
9632 specs->declspecs_seen_p = true;
9634 /* A UPC layout qualifier is encoded as an ARRAY_REF,
9635 further, it implies the presence of the 'shared' keyword. */
9636 if (TREE_CODE (qual) == ARRAY_REF)
9638 if (specs->upc_layout_qualifier)
9640 error ("two or more layout qualifiers specified");
9641 return specs;
9643 else
9645 specs->upc_layout_qualifier = qual;
9646 qual = ridpointers[RID_SHARED];
9650 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9651 && C_IS_RESERVED_WORD (qual));
9652 i = C_RID_CODE (qual);
9653 switch (i)
9655 case RID_CONST:
9656 dupe = specs->const_p;
9657 specs->const_p = true;
9658 specs->locations[cdw_const] = loc;
9659 break;
9660 case RID_VOLATILE:
9661 dupe = specs->volatile_p;
9662 specs->volatile_p = true;
9663 specs->locations[cdw_volatile] = loc;
9664 break;
9665 case RID_RESTRICT:
9666 dupe = specs->restrict_p;
9667 specs->restrict_p = true;
9668 specs->locations[cdw_restrict] = loc;
9669 break;
9670 case RID_ATOMIC:
9671 dupe = specs->atomic_p;
9672 specs->atomic_p = true;
9673 break;
9674 case RID_SHARED:
9675 dupe = specs->shared_p;
9676 specs->shared_p = true;
9677 break;
9678 case RID_STRICT:
9679 dupe = specs->strict_p;
9680 specs->strict_p = true;
9681 break;
9682 case RID_RELAXED:
9683 dupe = specs->relaxed_p;
9684 specs->relaxed_p = true;
9685 break;
9686 default:
9687 gcc_unreachable ();
9689 if (dupe)
9690 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9691 return specs;
9694 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9695 returning SPECS. */
9697 struct c_declspecs *
9698 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9699 struct c_typespec spec)
9701 tree type = spec.spec;
9702 specs->non_sc_seen_p = true;
9703 specs->declspecs_seen_p = true;
9704 specs->typespec_kind = spec.kind;
9705 if (TREE_DEPRECATED (type))
9706 specs->deprecated_p = true;
9708 /* Handle type specifier keywords. */
9709 if (TREE_CODE (type) == IDENTIFIER_NODE
9710 && C_IS_RESERVED_WORD (type)
9711 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9713 enum rid i = C_RID_CODE (type);
9714 if (specs->type)
9716 error_at (loc, "two or more data types in declaration specifiers");
9717 return specs;
9719 if ((int) i <= (int) RID_LAST_MODIFIER)
9721 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9722 bool dupe = false;
9723 switch (i)
9725 case RID_LONG:
9726 if (specs->long_long_p)
9728 error_at (loc, "%<long long long%> is too long for GCC");
9729 break;
9731 if (specs->long_p)
9733 if (specs->typespec_word == cts_double)
9735 error_at (loc,
9736 ("both %<long long%> and %<double%> in "
9737 "declaration specifiers"));
9738 break;
9740 pedwarn_c90 (loc, OPT_Wlong_long,
9741 "ISO C90 does not support %<long long%>");
9742 specs->long_long_p = 1;
9743 specs->locations[cdw_long_long] = loc;
9744 break;
9746 if (specs->short_p)
9747 error_at (loc,
9748 ("both %<long%> and %<short%> in "
9749 "declaration specifiers"));
9750 else if (specs->typespec_word == cts_auto_type)
9751 error_at (loc,
9752 ("both %<long%> and %<__auto_type%> in "
9753 "declaration specifiers"));
9754 else if (specs->typespec_word == cts_void)
9755 error_at (loc,
9756 ("both %<long%> and %<void%> in "
9757 "declaration specifiers"));
9758 else if (specs->typespec_word == cts_int_n)
9759 error_at (loc,
9760 ("both %<long%> and %<__int%d%> in "
9761 "declaration specifiers"),
9762 int_n_data[specs->int_n_idx].bitsize);
9763 else if (specs->typespec_word == cts_bool)
9764 error_at (loc,
9765 ("both %<long%> and %<_Bool%> in "
9766 "declaration specifiers"));
9767 else if (specs->typespec_word == cts_char)
9768 error_at (loc,
9769 ("both %<long%> and %<char%> in "
9770 "declaration specifiers"));
9771 else if (specs->typespec_word == cts_float)
9772 error_at (loc,
9773 ("both %<long%> and %<float%> in "
9774 "declaration specifiers"));
9775 else if (specs->typespec_word == cts_dfloat32)
9776 error_at (loc,
9777 ("both %<long%> and %<_Decimal32%> in "
9778 "declaration specifiers"));
9779 else if (specs->typespec_word == cts_dfloat64)
9780 error_at (loc,
9781 ("both %<long%> and %<_Decimal64%> in "
9782 "declaration specifiers"));
9783 else if (specs->typespec_word == cts_dfloat128)
9784 error_at (loc,
9785 ("both %<long%> and %<_Decimal128%> in "
9786 "declaration specifiers"));
9787 else
9789 specs->long_p = true;
9790 specs->locations[cdw_long] = loc;
9792 break;
9793 case RID_SHORT:
9794 dupe = specs->short_p;
9795 if (specs->long_p)
9796 error_at (loc,
9797 ("both %<long%> and %<short%> in "
9798 "declaration specifiers"));
9799 else if (specs->typespec_word == cts_auto_type)
9800 error_at (loc,
9801 ("both %<short%> and %<__auto_type%> in "
9802 "declaration specifiers"));
9803 else if (specs->typespec_word == cts_void)
9804 error_at (loc,
9805 ("both %<short%> and %<void%> in "
9806 "declaration specifiers"));
9807 else if (specs->typespec_word == cts_int_n)
9808 error_at (loc,
9809 ("both %<short%> and %<__int%d%> in "
9810 "declaration specifiers"),
9811 int_n_data[specs->int_n_idx].bitsize);
9812 else if (specs->typespec_word == cts_bool)
9813 error_at (loc,
9814 ("both %<short%> and %<_Bool%> in "
9815 "declaration specifiers"));
9816 else if (specs->typespec_word == cts_char)
9817 error_at (loc,
9818 ("both %<short%> and %<char%> in "
9819 "declaration specifiers"));
9820 else if (specs->typespec_word == cts_float)
9821 error_at (loc,
9822 ("both %<short%> and %<float%> in "
9823 "declaration specifiers"));
9824 else if (specs->typespec_word == cts_double)
9825 error_at (loc,
9826 ("both %<short%> and %<double%> in "
9827 "declaration specifiers"));
9828 else if (specs->typespec_word == cts_dfloat32)
9829 error_at (loc,
9830 ("both %<short%> and %<_Decimal32%> in "
9831 "declaration specifiers"));
9832 else if (specs->typespec_word == cts_dfloat64)
9833 error_at (loc,
9834 ("both %<short%> and %<_Decimal64%> in "
9835 "declaration specifiers"));
9836 else if (specs->typespec_word == cts_dfloat128)
9837 error_at (loc,
9838 ("both %<short%> and %<_Decimal128%> in "
9839 "declaration specifiers"));
9840 else
9842 specs->short_p = true;
9843 specs->locations[cdw_short] = loc;
9845 break;
9846 case RID_SIGNED:
9847 dupe = specs->signed_p;
9848 if (specs->unsigned_p)
9849 error_at (loc,
9850 ("both %<signed%> and %<unsigned%> in "
9851 "declaration specifiers"));
9852 else if (specs->typespec_word == cts_auto_type)
9853 error_at (loc,
9854 ("both %<signed%> and %<__auto_type%> in "
9855 "declaration specifiers"));
9856 else if (specs->typespec_word == cts_void)
9857 error_at (loc,
9858 ("both %<signed%> and %<void%> in "
9859 "declaration specifiers"));
9860 else if (specs->typespec_word == cts_bool)
9861 error_at (loc,
9862 ("both %<signed%> and %<_Bool%> in "
9863 "declaration specifiers"));
9864 else if (specs->typespec_word == cts_float)
9865 error_at (loc,
9866 ("both %<signed%> and %<float%> in "
9867 "declaration specifiers"));
9868 else if (specs->typespec_word == cts_double)
9869 error_at (loc,
9870 ("both %<signed%> and %<double%> in "
9871 "declaration specifiers"));
9872 else if (specs->typespec_word == cts_dfloat32)
9873 error_at (loc,
9874 ("both %<signed%> and %<_Decimal32%> in "
9875 "declaration specifiers"));
9876 else if (specs->typespec_word == cts_dfloat64)
9877 error_at (loc,
9878 ("both %<signed%> and %<_Decimal64%> in "
9879 "declaration specifiers"));
9880 else if (specs->typespec_word == cts_dfloat128)
9881 error_at (loc,
9882 ("both %<signed%> and %<_Decimal128%> in "
9883 "declaration specifiers"));
9884 else
9886 specs->signed_p = true;
9887 specs->locations[cdw_signed] = loc;
9889 break;
9890 case RID_UNSIGNED:
9891 dupe = specs->unsigned_p;
9892 if (specs->signed_p)
9893 error_at (loc,
9894 ("both %<signed%> and %<unsigned%> in "
9895 "declaration specifiers"));
9896 else if (specs->typespec_word == cts_auto_type)
9897 error_at (loc,
9898 ("both %<unsigned%> and %<__auto_type%> in "
9899 "declaration specifiers"));
9900 else if (specs->typespec_word == cts_void)
9901 error_at (loc,
9902 ("both %<unsigned%> and %<void%> in "
9903 "declaration specifiers"));
9904 else if (specs->typespec_word == cts_bool)
9905 error_at (loc,
9906 ("both %<unsigned%> and %<_Bool%> in "
9907 "declaration specifiers"));
9908 else if (specs->typespec_word == cts_float)
9909 error_at (loc,
9910 ("both %<unsigned%> and %<float%> in "
9911 "declaration specifiers"));
9912 else if (specs->typespec_word == cts_double)
9913 error_at (loc,
9914 ("both %<unsigned%> and %<double%> in "
9915 "declaration specifiers"));
9916 else if (specs->typespec_word == cts_dfloat32)
9917 error_at (loc,
9918 ("both %<unsigned%> and %<_Decimal32%> in "
9919 "declaration specifiers"));
9920 else if (specs->typespec_word == cts_dfloat64)
9921 error_at (loc,
9922 ("both %<unsigned%> and %<_Decimal64%> in "
9923 "declaration specifiers"));
9924 else if (specs->typespec_word == cts_dfloat128)
9925 error_at (loc,
9926 ("both %<unsigned%> and %<_Decimal128%> in "
9927 "declaration specifiers"));
9928 else
9930 specs->unsigned_p = true;
9931 specs->locations[cdw_unsigned] = loc;
9933 break;
9934 case RID_COMPLEX:
9935 dupe = specs->complex_p;
9936 if (!in_system_header_at (loc))
9937 pedwarn_c90 (loc, OPT_Wpedantic,
9938 "ISO C90 does not support complex types");
9939 if (specs->typespec_word == cts_auto_type)
9940 error_at (loc,
9941 ("both %<complex%> and %<__auto_type%> in "
9942 "declaration specifiers"));
9943 else if (specs->typespec_word == cts_void)
9944 error_at (loc,
9945 ("both %<complex%> and %<void%> in "
9946 "declaration specifiers"));
9947 else if (specs->typespec_word == cts_bool)
9948 error_at (loc,
9949 ("both %<complex%> and %<_Bool%> in "
9950 "declaration specifiers"));
9951 else if (specs->typespec_word == cts_dfloat32)
9952 error_at (loc,
9953 ("both %<complex%> and %<_Decimal32%> in "
9954 "declaration specifiers"));
9955 else if (specs->typespec_word == cts_dfloat64)
9956 error_at (loc,
9957 ("both %<complex%> and %<_Decimal64%> in "
9958 "declaration specifiers"));
9959 else if (specs->typespec_word == cts_dfloat128)
9960 error_at (loc,
9961 ("both %<complex%> and %<_Decimal128%> in "
9962 "declaration specifiers"));
9963 else if (specs->typespec_word == cts_fract)
9964 error_at (loc,
9965 ("both %<complex%> and %<_Fract%> in "
9966 "declaration specifiers"));
9967 else if (specs->typespec_word == cts_accum)
9968 error_at (loc,
9969 ("both %<complex%> and %<_Accum%> in "
9970 "declaration specifiers"));
9971 else if (specs->saturating_p)
9972 error_at (loc,
9973 ("both %<complex%> and %<_Sat%> in "
9974 "declaration specifiers"));
9975 else
9977 specs->complex_p = true;
9978 specs->locations[cdw_complex] = loc;
9980 break;
9981 case RID_SAT:
9982 dupe = specs->saturating_p;
9983 pedwarn (loc, OPT_Wpedantic,
9984 "ISO C does not support saturating types");
9985 if (specs->typespec_word == cts_int_n)
9987 error_at (loc,
9988 ("both %<_Sat%> and %<__int%d%> in "
9989 "declaration specifiers"),
9990 int_n_data[specs->int_n_idx].bitsize);
9992 else if (specs->typespec_word == cts_auto_type)
9993 error_at (loc,
9994 ("both %<_Sat%> and %<__auto_type%> in "
9995 "declaration specifiers"));
9996 else if (specs->typespec_word == cts_void)
9997 error_at (loc,
9998 ("both %<_Sat%> and %<void%> in "
9999 "declaration specifiers"));
10000 else if (specs->typespec_word == cts_bool)
10001 error_at (loc,
10002 ("both %<_Sat%> and %<_Bool%> in "
10003 "declaration specifiers"));
10004 else if (specs->typespec_word == cts_char)
10005 error_at (loc,
10006 ("both %<_Sat%> and %<char%> in "
10007 "declaration specifiers"));
10008 else if (specs->typespec_word == cts_int)
10009 error_at (loc,
10010 ("both %<_Sat%> and %<int%> in "
10011 "declaration specifiers"));
10012 else if (specs->typespec_word == cts_float)
10013 error_at (loc,
10014 ("both %<_Sat%> and %<float%> in "
10015 "declaration specifiers"));
10016 else if (specs->typespec_word == cts_double)
10017 error_at (loc,
10018 ("both %<_Sat%> and %<double%> in "
10019 "declaration specifiers"));
10020 else if (specs->typespec_word == cts_dfloat32)
10021 error_at (loc,
10022 ("both %<_Sat%> and %<_Decimal32%> in "
10023 "declaration specifiers"));
10024 else if (specs->typespec_word == cts_dfloat64)
10025 error_at (loc,
10026 ("both %<_Sat%> and %<_Decimal64%> in "
10027 "declaration specifiers"));
10028 else if (specs->typespec_word == cts_dfloat128)
10029 error_at (loc,
10030 ("both %<_Sat%> and %<_Decimal128%> in "
10031 "declaration specifiers"));
10032 else if (specs->complex_p)
10033 error_at (loc,
10034 ("both %<_Sat%> and %<complex%> in "
10035 "declaration specifiers"));
10036 else
10038 specs->saturating_p = true;
10039 specs->locations[cdw_saturating] = loc;
10041 break;
10042 default:
10043 gcc_unreachable ();
10046 if (dupe)
10047 error_at (loc, "duplicate %qE", type);
10049 return specs;
10051 else
10053 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
10054 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10055 "__auto_type". */
10056 if (specs->typespec_word != cts_none)
10058 error_at (loc,
10059 "two or more data types in declaration specifiers");
10060 return specs;
10062 switch (i)
10064 case RID_AUTO_TYPE:
10065 if (specs->long_p)
10066 error_at (loc,
10067 ("both %<long%> and %<__auto_type%> in "
10068 "declaration specifiers"));
10069 else if (specs->short_p)
10070 error_at (loc,
10071 ("both %<short%> and %<__auto_type%> in "
10072 "declaration specifiers"));
10073 else if (specs->signed_p)
10074 error_at (loc,
10075 ("both %<signed%> and %<__auto_type%> in "
10076 "declaration specifiers"));
10077 else if (specs->unsigned_p)
10078 error_at (loc,
10079 ("both %<unsigned%> and %<__auto_type%> in "
10080 "declaration specifiers"));
10081 else if (specs->complex_p)
10082 error_at (loc,
10083 ("both %<complex%> and %<__auto_type%> in "
10084 "declaration specifiers"));
10085 else if (specs->saturating_p)
10086 error_at (loc,
10087 ("both %<_Sat%> and %<__auto_type%> in "
10088 "declaration specifiers"));
10089 else
10091 specs->typespec_word = cts_auto_type;
10092 specs->locations[cdw_typespec] = loc;
10094 return specs;
10095 case RID_INT_N_0:
10096 case RID_INT_N_1:
10097 case RID_INT_N_2:
10098 case RID_INT_N_3:
10099 specs->int_n_idx = i - RID_INT_N_0;
10100 if (!in_system_header_at (input_location))
10101 pedwarn (loc, OPT_Wpedantic,
10102 "ISO C does not support %<__int%d%> types",
10103 int_n_data[specs->int_n_idx].bitsize);
10105 if (specs->long_p)
10106 error_at (loc,
10107 ("both %<__int%d%> and %<long%> in "
10108 "declaration specifiers"),
10109 int_n_data[specs->int_n_idx].bitsize);
10110 else if (specs->saturating_p)
10111 error_at (loc,
10112 ("both %<_Sat%> and %<__int%d%> in "
10113 "declaration specifiers"),
10114 int_n_data[specs->int_n_idx].bitsize);
10115 else if (specs->short_p)
10116 error_at (loc,
10117 ("both %<__int%d%> and %<short%> in "
10118 "declaration specifiers"),
10119 int_n_data[specs->int_n_idx].bitsize);
10120 else if (! int_n_enabled_p [specs->int_n_idx])
10121 error_at (loc,
10122 "%<__int%d%> is not supported on this target",
10123 int_n_data[specs->int_n_idx].bitsize);
10124 else
10126 specs->typespec_word = cts_int_n;
10127 specs->locations[cdw_typespec] = loc;
10129 return specs;
10130 case RID_VOID:
10131 if (specs->long_p)
10132 error_at (loc,
10133 ("both %<long%> and %<void%> in "
10134 "declaration specifiers"));
10135 else if (specs->short_p)
10136 error_at (loc,
10137 ("both %<short%> and %<void%> in "
10138 "declaration specifiers"));
10139 else if (specs->signed_p)
10140 error_at (loc,
10141 ("both %<signed%> and %<void%> in "
10142 "declaration specifiers"));
10143 else if (specs->unsigned_p)
10144 error_at (loc,
10145 ("both %<unsigned%> and %<void%> in "
10146 "declaration specifiers"));
10147 else if (specs->complex_p)
10148 error_at (loc,
10149 ("both %<complex%> and %<void%> in "
10150 "declaration specifiers"));
10151 else if (specs->saturating_p)
10152 error_at (loc,
10153 ("both %<_Sat%> and %<void%> in "
10154 "declaration specifiers"));
10155 else
10157 specs->typespec_word = cts_void;
10158 specs->locations[cdw_typespec] = loc;
10160 return specs;
10161 case RID_BOOL:
10162 if (!in_system_header_at (loc))
10163 pedwarn_c90 (loc, OPT_Wpedantic,
10164 "ISO C90 does not support boolean types");
10165 if (specs->long_p)
10166 error_at (loc,
10167 ("both %<long%> and %<_Bool%> in "
10168 "declaration specifiers"));
10169 else if (specs->short_p)
10170 error_at (loc,
10171 ("both %<short%> and %<_Bool%> in "
10172 "declaration specifiers"));
10173 else if (specs->signed_p)
10174 error_at (loc,
10175 ("both %<signed%> and %<_Bool%> in "
10176 "declaration specifiers"));
10177 else if (specs->unsigned_p)
10178 error_at (loc,
10179 ("both %<unsigned%> and %<_Bool%> in "
10180 "declaration specifiers"));
10181 else if (specs->complex_p)
10182 error_at (loc,
10183 ("both %<complex%> and %<_Bool%> in "
10184 "declaration specifiers"));
10185 else if (specs->saturating_p)
10186 error_at (loc,
10187 ("both %<_Sat%> and %<_Bool%> in "
10188 "declaration specifiers"));
10189 else
10191 specs->typespec_word = cts_bool;
10192 specs->locations[cdw_typespec] = loc;
10194 return specs;
10195 case RID_CHAR:
10196 if (specs->long_p)
10197 error_at (loc,
10198 ("both %<long%> and %<char%> in "
10199 "declaration specifiers"));
10200 else if (specs->short_p)
10201 error_at (loc,
10202 ("both %<short%> and %<char%> in "
10203 "declaration specifiers"));
10204 else if (specs->saturating_p)
10205 error_at (loc,
10206 ("both %<_Sat%> and %<char%> in "
10207 "declaration specifiers"));
10208 else
10210 specs->typespec_word = cts_char;
10211 specs->locations[cdw_typespec] = loc;
10213 return specs;
10214 case RID_INT:
10215 if (specs->saturating_p)
10216 error_at (loc,
10217 ("both %<_Sat%> and %<int%> in "
10218 "declaration specifiers"));
10219 else
10221 specs->typespec_word = cts_int;
10222 specs->locations[cdw_typespec] = loc;
10224 return specs;
10225 case RID_FLOAT:
10226 if (specs->long_p)
10227 error_at (loc,
10228 ("both %<long%> and %<float%> in "
10229 "declaration specifiers"));
10230 else if (specs->short_p)
10231 error_at (loc,
10232 ("both %<short%> and %<float%> in "
10233 "declaration specifiers"));
10234 else if (specs->signed_p)
10235 error_at (loc,
10236 ("both %<signed%> and %<float%> in "
10237 "declaration specifiers"));
10238 else if (specs->unsigned_p)
10239 error_at (loc,
10240 ("both %<unsigned%> and %<float%> in "
10241 "declaration specifiers"));
10242 else if (specs->saturating_p)
10243 error_at (loc,
10244 ("both %<_Sat%> and %<float%> in "
10245 "declaration specifiers"));
10246 else
10248 specs->typespec_word = cts_float;
10249 specs->locations[cdw_typespec] = loc;
10251 return specs;
10252 case RID_DOUBLE:
10253 if (specs->long_long_p)
10254 error_at (loc,
10255 ("both %<long long%> and %<double%> in "
10256 "declaration specifiers"));
10257 else if (specs->short_p)
10258 error_at (loc,
10259 ("both %<short%> and %<double%> in "
10260 "declaration specifiers"));
10261 else if (specs->signed_p)
10262 error_at (loc,
10263 ("both %<signed%> and %<double%> in "
10264 "declaration specifiers"));
10265 else if (specs->unsigned_p)
10266 error_at (loc,
10267 ("both %<unsigned%> and %<double%> in "
10268 "declaration specifiers"));
10269 else if (specs->saturating_p)
10270 error_at (loc,
10271 ("both %<_Sat%> and %<double%> in "
10272 "declaration specifiers"));
10273 else
10275 specs->typespec_word = cts_double;
10276 specs->locations[cdw_typespec] = loc;
10278 return specs;
10279 case RID_DFLOAT32:
10280 case RID_DFLOAT64:
10281 case RID_DFLOAT128:
10283 const char *str;
10284 if (i == RID_DFLOAT32)
10285 str = "_Decimal32";
10286 else if (i == RID_DFLOAT64)
10287 str = "_Decimal64";
10288 else
10289 str = "_Decimal128";
10290 if (specs->long_long_p)
10291 error_at (loc,
10292 ("both %<long long%> and %<%s%> in "
10293 "declaration specifiers"),
10294 str);
10295 if (specs->long_p)
10296 error_at (loc,
10297 ("both %<long%> and %<%s%> in "
10298 "declaration specifiers"),
10299 str);
10300 else if (specs->short_p)
10301 error_at (loc,
10302 ("both %<short%> and %<%s%> in "
10303 "declaration specifiers"),
10304 str);
10305 else if (specs->signed_p)
10306 error_at (loc,
10307 ("both %<signed%> and %<%s%> in "
10308 "declaration specifiers"),
10309 str);
10310 else if (specs->unsigned_p)
10311 error_at (loc,
10312 ("both %<unsigned%> and %<%s%> in "
10313 "declaration specifiers"),
10314 str);
10315 else if (specs->complex_p)
10316 error_at (loc,
10317 ("both %<complex%> and %<%s%> in "
10318 "declaration specifiers"),
10319 str);
10320 else if (specs->saturating_p)
10321 error_at (loc,
10322 ("both %<_Sat%> and %<%s%> in "
10323 "declaration specifiers"),
10324 str);
10325 else if (i == RID_DFLOAT32)
10326 specs->typespec_word = cts_dfloat32;
10327 else if (i == RID_DFLOAT64)
10328 specs->typespec_word = cts_dfloat64;
10329 else
10330 specs->typespec_word = cts_dfloat128;
10331 specs->locations[cdw_typespec] = loc;
10333 if (!targetm.decimal_float_supported_p ())
10334 error_at (loc,
10335 ("decimal floating point not supported "
10336 "for this target"));
10337 pedwarn (loc, OPT_Wpedantic,
10338 "ISO C does not support decimal floating point");
10339 return specs;
10340 case RID_FRACT:
10341 case RID_ACCUM:
10343 const char *str;
10344 if (i == RID_FRACT)
10345 str = "_Fract";
10346 else
10347 str = "_Accum";
10348 if (specs->complex_p)
10349 error_at (loc,
10350 ("both %<complex%> and %<%s%> in "
10351 "declaration specifiers"),
10352 str);
10353 else if (i == RID_FRACT)
10354 specs->typespec_word = cts_fract;
10355 else
10356 specs->typespec_word = cts_accum;
10357 specs->locations[cdw_typespec] = loc;
10359 if (!targetm.fixed_point_supported_p ())
10360 error_at (loc,
10361 "fixed-point types not supported for this target");
10362 pedwarn (loc, OPT_Wpedantic,
10363 "ISO C does not support fixed-point types");
10364 return specs;
10365 default:
10366 /* ObjC reserved word "id", handled below. */
10367 break;
10372 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10373 form of ObjC type, cases such as "int" and "long" being handled
10374 above), a TYPE (struct, union, enum and typeof specifiers) or an
10375 ERROR_MARK. In none of these cases may there have previously
10376 been any type specifiers. */
10377 if (specs->type || specs->typespec_word != cts_none
10378 || specs->long_p || specs->short_p || specs->signed_p
10379 || specs->unsigned_p || specs->complex_p)
10380 error_at (loc, "two or more data types in declaration specifiers");
10381 else if (TREE_CODE (type) == TYPE_DECL)
10383 if (TREE_TYPE (type) == error_mark_node)
10384 ; /* Allow the type to default to int to avoid cascading errors. */
10385 else
10387 specs->type = TREE_TYPE (type);
10388 specs->decl_attr = DECL_ATTRIBUTES (type);
10389 specs->typedef_p = true;
10390 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10391 specs->locations[cdw_typedef] = loc;
10393 /* If this typedef name is defined in a struct, then a C++
10394 lookup would return a different value. */
10395 if (warn_cxx_compat
10396 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10397 warning_at (loc, OPT_Wc___compat,
10398 "C++ lookup of %qD would return a field, not a type",
10399 type);
10401 /* If we are parsing a struct, record that a struct field
10402 used a typedef. */
10403 if (warn_cxx_compat && struct_parse_info != NULL)
10404 struct_parse_info->typedefs_seen.safe_push (type);
10407 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10409 tree t = lookup_name (type);
10410 if (!t || TREE_CODE (t) != TYPE_DECL)
10411 error_at (loc, "%qE fails to be a typedef or built in type", type);
10412 else if (TREE_TYPE (t) == error_mark_node)
10414 else
10416 specs->type = TREE_TYPE (t);
10417 specs->locations[cdw_typespec] = loc;
10420 else
10422 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10424 specs->typedef_p = true;
10425 specs->locations[cdw_typedef] = loc;
10426 if (spec.expr)
10428 if (specs->expr)
10429 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10430 specs->expr, spec.expr);
10431 else
10432 specs->expr = spec.expr;
10433 specs->expr_const_operands &= spec.expr_const_operands;
10436 specs->type = type;
10439 return specs;
10442 /* Add the storage class specifier or function specifier SCSPEC to the
10443 declaration specifiers SPECS, returning SPECS. */
10445 struct c_declspecs *
10446 declspecs_add_scspec (source_location loc,
10447 struct c_declspecs *specs,
10448 tree scspec)
10450 enum rid i;
10451 enum c_storage_class n = csc_none;
10452 bool dupe = false;
10453 specs->declspecs_seen_p = true;
10454 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10455 && C_IS_RESERVED_WORD (scspec));
10456 i = C_RID_CODE (scspec);
10457 if (specs->non_sc_seen_p)
10458 warning (OPT_Wold_style_declaration,
10459 "%qE is not at beginning of declaration", scspec);
10460 switch (i)
10462 case RID_INLINE:
10463 /* C99 permits duplicate inline. Although of doubtful utility,
10464 it seems simplest to permit it in gnu89 mode as well, as
10465 there is also little utility in maintaining this as a
10466 difference between gnu89 and C99 inline. */
10467 dupe = false;
10468 specs->inline_p = true;
10469 specs->locations[cdw_inline] = loc;
10470 break;
10471 case RID_NORETURN:
10472 /* Duplicate _Noreturn is permitted. */
10473 dupe = false;
10474 specs->noreturn_p = true;
10475 specs->locations[cdw_noreturn] = loc;
10476 break;
10477 case RID_THREAD:
10478 dupe = specs->thread_p;
10479 if (specs->storage_class == csc_auto)
10480 error ("%qE used with %<auto%>", scspec);
10481 else if (specs->storage_class == csc_register)
10482 error ("%qE used with %<register%>", scspec);
10483 else if (specs->storage_class == csc_typedef)
10484 error ("%qE used with %<typedef%>", scspec);
10485 else
10487 specs->thread_p = true;
10488 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10489 "__thread") == 0);
10490 /* A diagnostic is not required for the use of this
10491 identifier in the implementation namespace; only diagnose
10492 it for the C11 spelling because of existing code using
10493 the other spelling. */
10494 if (!specs->thread_gnu_p)
10496 if (flag_isoc99)
10497 pedwarn_c99 (loc, OPT_Wpedantic,
10498 "ISO C99 does not support %qE", scspec);
10499 else
10500 pedwarn_c99 (loc, OPT_Wpedantic,
10501 "ISO C90 does not support %qE", scspec);
10503 specs->locations[cdw_thread] = loc;
10505 break;
10506 case RID_AUTO:
10507 n = csc_auto;
10508 break;
10509 case RID_EXTERN:
10510 n = csc_extern;
10511 /* Diagnose "__thread extern". */
10512 if (specs->thread_p && specs->thread_gnu_p)
10513 error ("%<__thread%> before %<extern%>");
10514 break;
10515 case RID_REGISTER:
10516 n = csc_register;
10517 break;
10518 case RID_STATIC:
10519 n = csc_static;
10520 /* Diagnose "__thread static". */
10521 if (specs->thread_p && specs->thread_gnu_p)
10522 error ("%<__thread%> before %<static%>");
10523 break;
10524 case RID_TYPEDEF:
10525 n = csc_typedef;
10526 break;
10527 default:
10528 gcc_unreachable ();
10530 if (n != csc_none && n == specs->storage_class)
10531 dupe = true;
10532 if (dupe)
10534 if (i == RID_THREAD)
10535 error ("duplicate %<_Thread_local%> or %<__thread%>");
10536 else
10537 error ("duplicate %qE", scspec);
10539 if (n != csc_none)
10541 if (specs->storage_class != csc_none && n != specs->storage_class)
10543 error ("multiple storage classes in declaration specifiers");
10545 else
10547 specs->storage_class = n;
10548 specs->locations[cdw_storage_class] = loc;
10549 if (n != csc_extern && n != csc_static && specs->thread_p)
10551 error ("%qs used with %qE",
10552 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10553 scspec);
10554 specs->thread_p = false;
10558 return specs;
10561 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10562 returning SPECS. */
10564 struct c_declspecs *
10565 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10567 specs->attrs = chainon (attrs, specs->attrs);
10568 specs->locations[cdw_attributes] = loc;
10569 specs->declspecs_seen_p = true;
10570 return specs;
10573 /* Add an _Alignas specifier (expression ALIGN, or type whose
10574 alignment is ALIGN) to the declaration specifiers SPECS, returning
10575 SPECS. */
10576 struct c_declspecs *
10577 declspecs_add_alignas (source_location loc,
10578 struct c_declspecs *specs, tree align)
10580 int align_log;
10581 specs->alignas_p = true;
10582 specs->locations[cdw_alignas] = loc;
10583 if (align == error_mark_node)
10584 return specs;
10585 align_log = check_user_alignment (align, true);
10586 if (align_log > specs->align_log)
10587 specs->align_log = align_log;
10588 return specs;
10591 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10592 specifiers with any other type specifier to determine the resulting
10593 type. This is where ISO C checks on complex types are made, since
10594 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10595 double". */
10597 struct c_declspecs *
10598 finish_declspecs (struct c_declspecs *specs)
10600 /* If a type was specified as a whole, we have no modifiers and are
10601 done. */
10602 if (specs->type != NULL_TREE)
10604 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10605 && !specs->signed_p && !specs->unsigned_p
10606 && !specs->complex_p);
10608 /* Set a dummy type. */
10609 if (TREE_CODE (specs->type) == ERROR_MARK)
10610 specs->type = integer_type_node;
10611 return specs;
10614 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10615 has been specified, treat it as "int" unless "_Complex" is
10616 present and there are no other specifiers. If we just have
10617 "_Complex", it is equivalent to "_Complex double", but e.g.
10618 "_Complex short" is equivalent to "_Complex short int". */
10619 if (specs->typespec_word == cts_none)
10621 if (specs->saturating_p)
10623 error_at (specs->locations[cdw_saturating],
10624 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10625 if (!targetm.fixed_point_supported_p ())
10626 error_at (specs->locations[cdw_saturating],
10627 "fixed-point types not supported for this target");
10628 specs->typespec_word = cts_fract;
10630 else if (specs->long_p || specs->short_p
10631 || specs->signed_p || specs->unsigned_p)
10633 specs->typespec_word = cts_int;
10635 else if (specs->complex_p)
10637 specs->typespec_word = cts_double;
10638 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10639 "ISO C does not support plain %<complex%> meaning "
10640 "%<double complex%>");
10642 else
10644 specs->typespec_word = cts_int;
10645 specs->default_int_p = true;
10646 /* We don't diagnose this here because grokdeclarator will
10647 give more specific diagnostics according to whether it is
10648 a function definition. */
10652 /* If "signed" was specified, record this to distinguish "int" and
10653 "signed int" in the case of a bit-field with
10654 -funsigned-bitfields. */
10655 specs->explicit_signed_p = specs->signed_p;
10657 /* Now compute the actual type. */
10658 switch (specs->typespec_word)
10660 case cts_auto_type:
10661 gcc_assert (!specs->long_p && !specs->short_p
10662 && !specs->signed_p && !specs->unsigned_p
10663 && !specs->complex_p);
10664 /* Type to be filled in later. */
10665 break;
10666 case cts_void:
10667 gcc_assert (!specs->long_p && !specs->short_p
10668 && !specs->signed_p && !specs->unsigned_p
10669 && !specs->complex_p);
10670 specs->type = void_type_node;
10671 break;
10672 case cts_bool:
10673 gcc_assert (!specs->long_p && !specs->short_p
10674 && !specs->signed_p && !specs->unsigned_p
10675 && !specs->complex_p);
10676 specs->type = boolean_type_node;
10677 break;
10678 case cts_char:
10679 gcc_assert (!specs->long_p && !specs->short_p);
10680 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10681 if (specs->signed_p)
10682 specs->type = signed_char_type_node;
10683 else if (specs->unsigned_p)
10684 specs->type = unsigned_char_type_node;
10685 else
10686 specs->type = char_type_node;
10687 if (specs->complex_p)
10689 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10690 "ISO C does not support complex integer types");
10691 specs->type = build_complex_type (specs->type);
10693 break;
10694 case cts_int_n:
10695 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10696 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10697 specs->type = (specs->unsigned_p
10698 ? int_n_trees[specs->int_n_idx].unsigned_type
10699 : int_n_trees[specs->int_n_idx].signed_type);
10700 if (specs->complex_p)
10702 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10703 "ISO C does not support complex integer types");
10704 specs->type = build_complex_type (specs->type);
10706 break;
10707 case cts_int:
10708 gcc_assert (!(specs->long_p && specs->short_p));
10709 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10710 if (specs->long_long_p)
10711 specs->type = (specs->unsigned_p
10712 ? long_long_unsigned_type_node
10713 : long_long_integer_type_node);
10714 else if (specs->long_p)
10715 specs->type = (specs->unsigned_p
10716 ? long_unsigned_type_node
10717 : long_integer_type_node);
10718 else if (specs->short_p)
10719 specs->type = (specs->unsigned_p
10720 ? short_unsigned_type_node
10721 : short_integer_type_node);
10722 else
10723 specs->type = (specs->unsigned_p
10724 ? unsigned_type_node
10725 : integer_type_node);
10726 if (specs->complex_p)
10728 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10729 "ISO C does not support complex integer types");
10730 specs->type = build_complex_type (specs->type);
10732 break;
10733 case cts_float:
10734 gcc_assert (!specs->long_p && !specs->short_p
10735 && !specs->signed_p && !specs->unsigned_p);
10736 specs->type = (specs->complex_p
10737 ? complex_float_type_node
10738 : float_type_node);
10739 break;
10740 case cts_double:
10741 gcc_assert (!specs->long_long_p && !specs->short_p
10742 && !specs->signed_p && !specs->unsigned_p);
10743 if (specs->long_p)
10745 specs->type = (specs->complex_p
10746 ? complex_long_double_type_node
10747 : long_double_type_node);
10749 else
10751 specs->type = (specs->complex_p
10752 ? complex_double_type_node
10753 : double_type_node);
10755 break;
10756 case cts_dfloat32:
10757 case cts_dfloat64:
10758 case cts_dfloat128:
10759 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10760 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10761 if (specs->typespec_word == cts_dfloat32)
10762 specs->type = dfloat32_type_node;
10763 else if (specs->typespec_word == cts_dfloat64)
10764 specs->type = dfloat64_type_node;
10765 else
10766 specs->type = dfloat128_type_node;
10767 break;
10768 case cts_fract:
10769 gcc_assert (!specs->complex_p);
10770 if (!targetm.fixed_point_supported_p ())
10771 specs->type = integer_type_node;
10772 else if (specs->saturating_p)
10774 if (specs->long_long_p)
10775 specs->type = specs->unsigned_p
10776 ? sat_unsigned_long_long_fract_type_node
10777 : sat_long_long_fract_type_node;
10778 else if (specs->long_p)
10779 specs->type = specs->unsigned_p
10780 ? sat_unsigned_long_fract_type_node
10781 : sat_long_fract_type_node;
10782 else if (specs->short_p)
10783 specs->type = specs->unsigned_p
10784 ? sat_unsigned_short_fract_type_node
10785 : sat_short_fract_type_node;
10786 else
10787 specs->type = specs->unsigned_p
10788 ? sat_unsigned_fract_type_node
10789 : sat_fract_type_node;
10791 else
10793 if (specs->long_long_p)
10794 specs->type = specs->unsigned_p
10795 ? unsigned_long_long_fract_type_node
10796 : long_long_fract_type_node;
10797 else if (specs->long_p)
10798 specs->type = specs->unsigned_p
10799 ? unsigned_long_fract_type_node
10800 : long_fract_type_node;
10801 else if (specs->short_p)
10802 specs->type = specs->unsigned_p
10803 ? unsigned_short_fract_type_node
10804 : short_fract_type_node;
10805 else
10806 specs->type = specs->unsigned_p
10807 ? unsigned_fract_type_node
10808 : fract_type_node;
10810 break;
10811 case cts_accum:
10812 gcc_assert (!specs->complex_p);
10813 if (!targetm.fixed_point_supported_p ())
10814 specs->type = integer_type_node;
10815 else if (specs->saturating_p)
10817 if (specs->long_long_p)
10818 specs->type = specs->unsigned_p
10819 ? sat_unsigned_long_long_accum_type_node
10820 : sat_long_long_accum_type_node;
10821 else if (specs->long_p)
10822 specs->type = specs->unsigned_p
10823 ? sat_unsigned_long_accum_type_node
10824 : sat_long_accum_type_node;
10825 else if (specs->short_p)
10826 specs->type = specs->unsigned_p
10827 ? sat_unsigned_short_accum_type_node
10828 : sat_short_accum_type_node;
10829 else
10830 specs->type = specs->unsigned_p
10831 ? sat_unsigned_accum_type_node
10832 : sat_accum_type_node;
10834 else
10836 if (specs->long_long_p)
10837 specs->type = specs->unsigned_p
10838 ? unsigned_long_long_accum_type_node
10839 : long_long_accum_type_node;
10840 else if (specs->long_p)
10841 specs->type = specs->unsigned_p
10842 ? unsigned_long_accum_type_node
10843 : long_accum_type_node;
10844 else if (specs->short_p)
10845 specs->type = specs->unsigned_p
10846 ? unsigned_short_accum_type_node
10847 : short_accum_type_node;
10848 else
10849 specs->type = specs->unsigned_p
10850 ? unsigned_accum_type_node
10851 : accum_type_node;
10853 break;
10854 default:
10855 gcc_unreachable ();
10858 return specs;
10861 /* A subroutine of c_write_global_declarations. Perform final processing
10862 on one file scope's declarations (or the external scope's declarations),
10863 GLOBALS. */
10865 static void
10866 c_write_global_declarations_1 (tree globals)
10868 tree decl;
10869 bool reconsider;
10871 /* Process the decls in the order they were written. */
10872 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10874 /* Check for used but undefined static functions using the C
10875 standard's definition of "used", and set TREE_NO_WARNING so
10876 that check_global_declarations doesn't repeat the check. */
10877 if (TREE_CODE (decl) == FUNCTION_DECL
10878 && DECL_INITIAL (decl) == 0
10879 && DECL_EXTERNAL (decl)
10880 && !TREE_PUBLIC (decl)
10881 && C_DECL_USED (decl))
10883 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10884 TREE_NO_WARNING (decl) = 1;
10887 wrapup_global_declaration_1 (decl);
10892 reconsider = false;
10893 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10894 reconsider |= wrapup_global_declaration_2 (decl);
10896 while (reconsider);
10898 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10899 check_global_declaration_1 (decl);
10902 /* A subroutine of c_write_global_declarations Emit debug information for each
10903 of the declarations in GLOBALS. */
10905 static void
10906 c_write_global_declarations_2 (tree globals)
10908 tree decl;
10910 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
10911 debug_hooks->global_decl (decl);
10914 /* Callback to collect a source_ref from a DECL. */
10916 static void
10917 collect_source_ref_cb (tree decl)
10919 if (!DECL_IS_BUILTIN (decl))
10920 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10923 /* Preserve the external declarations scope across a garbage collect. */
10924 static GTY(()) tree ext_block;
10926 /* Collect all references relevant to SOURCE_FILE. */
10928 static void
10929 collect_all_refs (const char *source_file)
10931 tree t;
10932 unsigned i;
10934 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10935 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10937 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10940 /* Iterate over all global declarations and call CALLBACK. */
10942 static void
10943 for_each_global_decl (void (*callback) (tree decl))
10945 tree t;
10946 tree decls;
10947 tree decl;
10948 unsigned i;
10950 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10952 decls = DECL_INITIAL (t);
10953 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10954 callback (decl);
10957 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10958 callback (decl);
10961 void
10962 c_write_global_declarations (void)
10964 tree t;
10965 unsigned i;
10967 /* We don't want to do this if generating a PCH. */
10968 if (pch_file)
10969 return;
10971 timevar_start (TV_PHASE_DEFERRED);
10973 /* Do the Objective-C stuff. This is where all the Objective-C
10974 module stuff gets generated (symtab, class/protocol/selector
10975 lists etc). */
10976 if (c_dialect_objc ())
10977 objc_write_global_declarations ();
10979 /* Close the external scope. */
10980 ext_block = pop_scope ();
10981 external_scope = 0;
10982 gcc_assert (!current_scope);
10984 /* Handle -fdump-ada-spec[-slim]. */
10985 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10987 /* Build a table of files to generate specs for */
10988 if (flag_dump_ada_spec_slim)
10989 collect_source_ref (main_input_filename);
10990 else
10991 for_each_global_decl (collect_source_ref_cb);
10993 dump_ada_specs (collect_all_refs, NULL);
10996 if (ext_block)
10998 tree tmp = BLOCK_VARS (ext_block);
10999 int flags;
11000 FILE * stream = dump_begin (TDI_tu, &flags);
11001 if (stream && tmp)
11003 dump_node (tmp, flags & ~TDF_SLIM, stream);
11004 dump_end (TDI_tu, stream);
11008 /* Process all file scopes in this compilation, and the external_scope,
11009 through wrapup_global_declarations and check_global_declarations. */
11010 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11011 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11012 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11014 timevar_stop (TV_PHASE_DEFERRED);
11015 timevar_start (TV_PHASE_OPT_GEN);
11017 /* We're done parsing; proceed to optimize and emit assembly.
11018 FIXME: shouldn't be the front end's responsibility to call this. */
11019 symtab->finalize_compilation_unit ();
11021 timevar_stop (TV_PHASE_OPT_GEN);
11022 timevar_start (TV_PHASE_DBGINFO);
11024 /* After cgraph has had a chance to emit everything that's going to
11025 be emitted, output debug information for globals. */
11026 if (!seen_error ())
11028 timevar_push (TV_SYMOUT);
11029 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11030 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
11031 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
11032 timevar_pop (TV_SYMOUT);
11035 ext_block = NULL;
11036 timevar_stop (TV_PHASE_DBGINFO);
11039 /* Register reserved keyword WORD as qualifier for address space AS. */
11041 void
11042 c_register_addr_space (const char *word, addr_space_t as)
11044 int rid = RID_FIRST_ADDR_SPACE + as;
11045 tree id;
11047 /* Address space qualifiers are only supported
11048 in C with GNU extensions enabled. */
11049 if (c_dialect_objc () || flag_no_asm)
11050 return;
11052 id = get_identifier (word);
11053 C_SET_RID_CODE (id, rid);
11054 C_IS_RESERVED_WORD (id) = 1;
11055 ridpointers [rid] = id;
11058 /* Return identifier to look up for omp declare reduction. */
11060 tree
11061 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11063 const char *p = NULL;
11064 switch (reduction_code)
11066 case PLUS_EXPR: p = "+"; break;
11067 case MULT_EXPR: p = "*"; break;
11068 case MINUS_EXPR: p = "-"; break;
11069 case BIT_AND_EXPR: p = "&"; break;
11070 case BIT_XOR_EXPR: p = "^"; break;
11071 case BIT_IOR_EXPR: p = "|"; break;
11072 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11073 case TRUTH_ORIF_EXPR: p = "||"; break;
11074 case MIN_EXPR: p = "min"; break;
11075 case MAX_EXPR: p = "max"; break;
11076 default:
11077 break;
11080 if (p == NULL)
11082 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11083 return error_mark_node;
11084 p = IDENTIFIER_POINTER (reduction_id);
11087 const char prefix[] = "omp declare reduction ";
11088 size_t lenp = sizeof (prefix);
11089 size_t len = strlen (p);
11090 char *name = XALLOCAVEC (char, lenp + len);
11091 memcpy (name, prefix, lenp - 1);
11092 memcpy (name + lenp - 1, p, len + 1);
11093 return get_identifier (name);
11096 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11097 VAR_DECL, bind it into the current scope and return it. */
11099 tree
11100 c_omp_reduction_decl (tree reduction_id)
11102 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11103 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11104 return b->decl;
11106 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11107 reduction_id, integer_type_node);
11108 DECL_ARTIFICIAL (decl) = 1;
11109 DECL_EXTERNAL (decl) = 1;
11110 TREE_STATIC (decl) = 1;
11111 TREE_PUBLIC (decl) = 0;
11112 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11113 return decl;
11116 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11118 tree
11119 c_omp_reduction_lookup (tree reduction_id, tree type)
11121 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11122 while (b)
11124 tree t;
11125 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11126 if (comptypes (TREE_PURPOSE (t), type))
11127 return TREE_VALUE (t);
11128 b = b->shadowed;
11130 return error_mark_node;
11133 /* Helper function called via walk_tree, to diagnose invalid
11134 #pragma omp declare reduction combiners or initializers. */
11136 tree
11137 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11139 tree *vars = (tree *) data;
11140 if (SSA_VAR_P (*tp)
11141 && !DECL_ARTIFICIAL (*tp)
11142 && *tp != vars[0]
11143 && *tp != vars[1])
11145 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11146 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11147 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11148 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11149 *tp);
11150 else
11151 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11152 "to variable %qD which is not %<omp_priv%> nor "
11153 "%<omp_orig%>",
11154 *tp);
11155 return *tp;
11157 return NULL_TREE;
11160 #include "gt-c-c-decl.h"