cp/
[official-gcc.git] / gcc / c / c-decl.c
bloba2edefc5f340422a8d187cdb5495c64cbe251649
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 "toplev.h"
58 #include "tm_p.h"
59 #include "cpplib.h"
60 #include "target.h"
61 #include "debug.h"
62 #include "opts.h"
63 #include "timevar.h"
64 #include "c-family/c-common.h"
65 #include "c-family/c-objc.h"
66 #include "c-family/c-pragma.h"
67 #include "c-family/c-ubsan.h"
68 #include "c-lang.h"
69 #include "langhooks.h"
70 #include "tree-iterator.h"
71 #include "diagnostic-core.h"
72 #include "dumpfile.h"
73 #include "hash-map.h"
74 #include "is-a.h"
75 #include "plugin-api.h"
76 #include "ipa-ref.h"
77 #include "cgraph.h"
78 #include "hash-table.h"
79 #include "langhooks-def.h"
80 #include "plugin.h"
81 #include "c-family/c-ada-spec.h"
82 #include "cilk.h"
83 #include "builtins.h"
85 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
86 enum decl_context
87 { NORMAL, /* Ordinary declaration */
88 FUNCDEF, /* Function definition */
89 PARM, /* Declaration of parm before function body */
90 FIELD, /* Declaration inside struct or union */
91 TYPENAME}; /* Typename (inside cast or sizeof) */
93 /* States indicating how grokdeclarator() should handle declspecs marked
94 with __attribute__((deprecated)). An object declared as
95 __attribute__((deprecated)) suppresses warnings of uses of other
96 deprecated items. */
98 enum deprecated_states {
99 DEPRECATED_NORMAL,
100 DEPRECATED_SUPPRESS
104 /* Nonzero if we have seen an invalid cross reference
105 to a struct, union, or enum, but not yet printed the message. */
106 tree pending_invalid_xref;
108 /* File and line to appear in the eventual error message. */
109 location_t pending_invalid_xref_location;
111 /* The file and line that the prototype came from if this is an
112 old-style definition; used for diagnostics in
113 store_parm_decls_oldstyle. */
115 static location_t current_function_prototype_locus;
117 /* Whether this prototype was built-in. */
119 static bool current_function_prototype_built_in;
121 /* The argument type information of this prototype. */
123 static tree current_function_prototype_arg_types;
125 /* The argument information structure for the function currently being
126 defined. */
128 static struct c_arg_info *current_function_arg_info;
130 /* The obstack on which parser and related data structures, which are
131 not live beyond their top-level declaration or definition, are
132 allocated. */
133 struct obstack parser_obstack;
135 /* The current statement tree. */
137 static GTY(()) struct stmt_tree_s c_stmt_tree;
139 /* State saving variables. */
140 tree c_break_label;
141 tree c_cont_label;
143 /* A list of decls to be made automatically visible in each file scope. */
144 static GTY(()) tree visible_builtins;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a return statement that specifies a return value is seen. */
149 int current_function_returns_value;
151 /* Set to 0 at beginning of a function definition, set to 1 if
152 a return statement with no argument is seen. */
154 int current_function_returns_null;
156 /* Set to 0 at beginning of a function definition, set to 1 if
157 a call to a noreturn function is seen. */
159 int current_function_returns_abnormally;
161 /* Set to nonzero by `grokdeclarator' for a function
162 whose return type is defaulted, if warnings for this are desired. */
164 static int warn_about_return_type;
166 /* Nonzero when the current toplevel function contains a declaration
167 of a nested function which is never defined. */
169 static bool undef_nested_function;
171 /* If non-zero, implicit "omp declare target" attribute is added into the
172 attribute lists. */
173 int current_omp_declare_target_attribute;
175 /* Each c_binding structure describes one binding of an identifier to
176 a decl. All the decls in a scope - irrespective of namespace - are
177 chained together by the ->prev field, which (as the name implies)
178 runs in reverse order. All the decls in a given namespace bound to
179 a given identifier are chained by the ->shadowed field, which runs
180 from inner to outer scopes.
182 The ->decl field usually points to a DECL node, but there are two
183 exceptions. In the namespace of type tags, the bound entity is a
184 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
185 identifier is encountered, it is bound to error_mark_node to
186 suppress further errors about that identifier in the current
187 function.
189 The ->u.type field stores the type of the declaration in this scope;
190 if NULL, the type is the type of the ->decl field. This is only of
191 relevance for objects with external or internal linkage which may
192 be redeclared in inner scopes, forming composite types that only
193 persist for the duration of those scopes. In the external scope,
194 this stores the composite of all the types declared for this
195 object, visible or not. The ->inner_comp field (used only at file
196 scope) stores whether an incomplete array type at file scope was
197 completed at an inner scope to an array size other than 1.
199 The ->u.label field is used for labels. It points to a structure
200 which stores additional information used for warnings.
202 The depth field is copied from the scope structure that holds this
203 decl. It is used to preserve the proper ordering of the ->shadowed
204 field (see bind()) and also for a handful of special-case checks.
205 Finally, the invisible bit is true for a decl which should be
206 ignored for purposes of normal name lookup, and the nested bit is
207 true for a decl that's been bound a second time in an inner scope;
208 in all such cases, the binding in the outer scope will have its
209 invisible bit true. */
211 struct GTY((chain_next ("%h.prev"))) c_binding {
212 union GTY(()) { /* first so GTY desc can use decl */
213 tree GTY((tag ("0"))) type; /* the type in this scope */
214 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
215 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
216 tree decl; /* the decl bound */
217 tree id; /* the identifier it's bound to */
218 struct c_binding *prev; /* the previous decl in this scope */
219 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
220 unsigned int depth : 28; /* depth of this scope */
221 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
222 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
223 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
224 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
225 location_t locus; /* location for nested bindings */
227 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
228 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
229 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
230 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
232 /* Each C symbol points to three linked lists of c_binding structures.
233 These describe the values of the identifier in the three different
234 namespaces defined by the language. */
236 struct GTY(()) lang_identifier {
237 struct c_common_identifier common_id;
238 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
239 struct c_binding *tag_binding; /* struct/union/enum tags */
240 struct c_binding *label_binding; /* labels */
243 /* Validate c-lang.c's assumptions. */
244 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
245 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
247 /* The binding oracle; see c-tree.h. */
248 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
250 /* This flag is set on an identifier if we have previously asked the
251 binding oracle for this identifier's symbol binding. */
252 #define I_SYMBOL_CHECKED(node) \
253 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
255 static inline struct c_binding* *
256 i_symbol_binding (tree node)
258 struct lang_identifier *lid
259 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
261 if (lid->symbol_binding == NULL
262 && c_binding_oracle != NULL
263 && !I_SYMBOL_CHECKED (node))
265 /* Set the "checked" flag first, to avoid infinite recursion
266 when the binding oracle calls back into gcc. */
267 I_SYMBOL_CHECKED (node) = 1;
268 c_binding_oracle (C_ORACLE_SYMBOL, node);
271 return &lid->symbol_binding;
274 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
276 #define I_SYMBOL_DECL(node) \
277 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
279 /* This flag is set on an identifier if we have previously asked the
280 binding oracle for this identifier's tag binding. */
281 #define I_TAG_CHECKED(node) \
282 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
284 static inline struct c_binding **
285 i_tag_binding (tree node)
287 struct lang_identifier *lid
288 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
290 if (lid->tag_binding == NULL
291 && c_binding_oracle != NULL
292 && !I_TAG_CHECKED (node))
294 /* Set the "checked" flag first, to avoid infinite recursion
295 when the binding oracle calls back into gcc. */
296 I_TAG_CHECKED (node) = 1;
297 c_binding_oracle (C_ORACLE_TAG, node);
300 return &lid->tag_binding;
303 #define I_TAG_BINDING(node) (*i_tag_binding (node))
305 #define I_TAG_DECL(node) \
306 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
308 /* This flag is set on an identifier if we have previously asked the
309 binding oracle for this identifier's label binding. */
310 #define I_LABEL_CHECKED(node) \
311 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
313 static inline struct c_binding **
314 i_label_binding (tree node)
316 struct lang_identifier *lid
317 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
319 if (lid->label_binding == NULL
320 && c_binding_oracle != NULL
321 && !I_LABEL_CHECKED (node))
323 /* Set the "checked" flag first, to avoid infinite recursion
324 when the binding oracle calls back into gcc. */
325 I_LABEL_CHECKED (node) = 1;
326 c_binding_oracle (C_ORACLE_LABEL, node);
329 return &lid->label_binding;
332 #define I_LABEL_BINDING(node) (*i_label_binding (node))
334 #define I_LABEL_DECL(node) \
335 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
337 /* The resulting tree type. */
339 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
340 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
342 union tree_node GTY ((tag ("0"),
343 desc ("tree_node_structure (&%h)")))
344 generic;
345 struct lang_identifier GTY ((tag ("1"))) identifier;
348 /* Track bindings and other things that matter for goto warnings. For
349 efficiency, we do not gather all the decls at the point of
350 definition. Instead, we point into the bindings structure. As
351 scopes are popped, we update these structures and gather the decls
352 that matter at that time. */
354 struct GTY(()) c_spot_bindings {
355 /* The currently open scope which holds bindings defined when the
356 label was defined or the goto statement was found. */
357 struct c_scope *scope;
358 /* The bindings in the scope field which were defined at the point
359 of the label or goto. This lets us look at older or newer
360 bindings in the scope, as appropriate. */
361 struct c_binding *bindings_in_scope;
362 /* The number of statement expressions that have started since this
363 label or goto statement was defined. This is zero if we are at
364 the same statement expression level. It is positive if we are in
365 a statement expression started since this spot. It is negative
366 if this spot was in a statement expression and we have left
367 it. */
368 int stmt_exprs;
369 /* Whether we started in a statement expression but are no longer in
370 it. This is set to true if stmt_exprs ever goes negative. */
371 bool left_stmt_expr;
374 /* This structure is used to keep track of bindings seen when a goto
375 statement is defined. This is only used if we see the goto
376 statement before we see the label. */
378 struct GTY(()) c_goto_bindings {
379 /* The location of the goto statement. */
380 location_t loc;
381 /* The bindings of the goto statement. */
382 struct c_spot_bindings goto_bindings;
385 typedef struct c_goto_bindings *c_goto_bindings_p;
387 /* The additional information we keep track of for a label binding.
388 These fields are updated as scopes are popped. */
390 struct GTY(()) c_label_vars {
391 /* The shadowed c_label_vars, when one label shadows another (which
392 can only happen using a __label__ declaration). */
393 struct c_label_vars *shadowed;
394 /* The bindings when the label was defined. */
395 struct c_spot_bindings label_bindings;
396 /* A list of decls that we care about: decls about which we should
397 warn if a goto branches to this label from later in the function.
398 Decls are added to this list as scopes are popped. We only add
399 the decls that matter. */
400 vec<tree, va_gc> *decls_in_scope;
401 /* A list of goto statements to this label. This is only used for
402 goto statements seen before the label was defined, so that we can
403 issue appropriate warnings for them. */
404 vec<c_goto_bindings_p, va_gc> *gotos;
407 /* Each c_scope structure describes the complete contents of one
408 scope. Four scopes are distinguished specially: the innermost or
409 current scope, the innermost function scope, the file scope (always
410 the second to outermost) and the outermost or external scope.
412 Most declarations are recorded in the current scope.
414 All normal label declarations are recorded in the innermost
415 function scope, as are bindings of undeclared identifiers to
416 error_mark_node. (GCC permits nested functions as an extension,
417 hence the 'innermost' qualifier.) Explicitly declared labels
418 (using the __label__ extension) appear in the current scope.
420 Being in the file scope (current_scope == file_scope) causes
421 special behavior in several places below. Also, under some
422 conditions the Objective-C front end records declarations in the
423 file scope even though that isn't the current scope.
425 All declarations with external linkage are recorded in the external
426 scope, even if they aren't visible there; this models the fact that
427 such declarations are visible to the entire program, and (with a
428 bit of cleverness, see pushdecl) allows diagnosis of some violations
429 of C99 6.2.2p7 and 6.2.7p2:
431 If, within the same translation unit, the same identifier appears
432 with both internal and external linkage, the behavior is
433 undefined.
435 All declarations that refer to the same object or function shall
436 have compatible type; otherwise, the behavior is undefined.
438 Initially only the built-in declarations, which describe compiler
439 intrinsic functions plus a subset of the standard library, are in
440 this scope.
442 The order of the blocks list matters, and it is frequently appended
443 to. To avoid having to walk all the way to the end of the list on
444 each insertion, or reverse the list later, we maintain a pointer to
445 the last list entry. (FIXME: It should be feasible to use a reversed
446 list here.)
448 The bindings list is strictly in reverse order of declarations;
449 pop_scope relies on this. */
452 struct GTY((chain_next ("%h.outer"))) c_scope {
453 /* The scope containing this one. */
454 struct c_scope *outer;
456 /* The next outermost function scope. */
457 struct c_scope *outer_function;
459 /* All bindings in this scope. */
460 struct c_binding *bindings;
462 /* For each scope (except the global one), a chain of BLOCK nodes
463 for all the scopes that were entered and exited one level down. */
464 tree blocks;
465 tree blocks_last;
467 /* The depth of this scope. Used to keep the ->shadowed chain of
468 bindings sorted innermost to outermost. */
469 unsigned int depth : 28;
471 /* True if we are currently filling this scope with parameter
472 declarations. */
473 BOOL_BITFIELD parm_flag : 1;
475 /* True if we saw [*] in this scope. Used to give an error messages
476 if these appears in a function definition. */
477 BOOL_BITFIELD had_vla_unspec : 1;
479 /* True if we already complained about forward parameter decls
480 in this scope. This prevents double warnings on
481 foo (int a; int b; ...) */
482 BOOL_BITFIELD warned_forward_parm_decls : 1;
484 /* True if this is the outermost block scope of a function body.
485 This scope contains the parameters, the local variables declared
486 in the outermost block, and all the labels (except those in
487 nested functions, or declared at block scope with __label__). */
488 BOOL_BITFIELD function_body : 1;
490 /* True means make a BLOCK for this scope no matter what. */
491 BOOL_BITFIELD keep : 1;
493 /* True means that an unsuffixed float constant is _Decimal64. */
494 BOOL_BITFIELD float_const_decimal64 : 1;
496 /* True if this scope has any label bindings. This is used to speed
497 up searching for labels when popping scopes, particularly since
498 labels are normally only found at function scope. */
499 BOOL_BITFIELD has_label_bindings : 1;
501 /* True if we should issue a warning if a goto statement crosses any
502 of the bindings. We still need to check the list of bindings to
503 find the specific ones we need to warn about. This is true if
504 decl_jump_unsafe would return true for any of the bindings. This
505 is used to avoid looping over all the bindings unnecessarily. */
506 BOOL_BITFIELD has_jump_unsafe_decl : 1;
509 /* The scope currently in effect. */
511 static GTY(()) struct c_scope *current_scope;
513 /* The innermost function scope. Ordinary (not explicitly declared)
514 labels, bindings to error_mark_node, and the lazily-created
515 bindings of __func__ and its friends get this scope. */
517 static GTY(()) struct c_scope *current_function_scope;
519 /* The C file scope. This is reset for each input translation unit. */
521 static GTY(()) struct c_scope *file_scope;
523 /* The outermost scope. This is used for all declarations with
524 external linkage, and only these, hence the name. */
526 static GTY(()) struct c_scope *external_scope;
528 /* A chain of c_scope structures awaiting reuse. */
530 static GTY((deletable)) struct c_scope *scope_freelist;
532 /* A chain of c_binding structures awaiting reuse. */
534 static GTY((deletable)) struct c_binding *binding_freelist;
536 /* Append VAR to LIST in scope SCOPE. */
537 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
538 struct c_scope *s_ = (scope); \
539 tree d_ = (decl); \
540 if (s_->list##_last) \
541 BLOCK_CHAIN (s_->list##_last) = d_; \
542 else \
543 s_->list = d_; \
544 s_->list##_last = d_; \
545 } while (0)
547 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
548 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
549 struct c_scope *t_ = (tscope); \
550 struct c_scope *f_ = (fscope); \
551 if (t_->to##_last) \
552 BLOCK_CHAIN (t_->to##_last) = f_->from; \
553 else \
554 t_->to = f_->from; \
555 t_->to##_last = f_->from##_last; \
556 } while (0)
558 /* A c_inline_static structure stores details of a static identifier
559 referenced in a definition of a function that may be an inline
560 definition if no subsequent declaration of that function uses
561 "extern" or does not use "inline". */
563 struct GTY((chain_next ("%h.next"))) c_inline_static {
564 /* The location for a diagnostic. */
565 location_t location;
567 /* The function that may be an inline definition. */
568 tree function;
570 /* The object or function referenced. */
571 tree static_decl;
573 /* What sort of reference this is. */
574 enum c_inline_static_type type;
576 /* The next such structure or NULL. */
577 struct c_inline_static *next;
580 /* List of static identifiers used or referenced in functions that may
581 be inline definitions. */
582 static GTY(()) struct c_inline_static *c_inline_statics;
584 /* True means unconditionally make a BLOCK for the next scope pushed. */
586 static bool keep_next_level_flag;
588 /* True means the next call to push_scope will be the outermost scope
589 of a function body, so do not push a new scope, merely cease
590 expecting parameter decls. */
592 static bool next_is_function_body;
594 /* A vector of pointers to c_binding structures. */
596 typedef struct c_binding *c_binding_ptr;
598 /* Information that we keep for a struct or union while it is being
599 parsed. */
601 struct c_struct_parse_info
603 /* If warn_cxx_compat, a list of types defined within this
604 struct. */
605 vec<tree> struct_types;
606 /* If warn_cxx_compat, a list of field names which have bindings,
607 and which are defined in this struct, but which are not defined
608 in any enclosing struct. This is used to clear the in_struct
609 field of the c_bindings structure. */
610 vec<c_binding_ptr> fields;
611 /* If warn_cxx_compat, a list of typedef names used when defining
612 fields in this struct. */
613 vec<tree> typedefs_seen;
616 /* Information for the struct or union currently being parsed, or
617 NULL if not parsing a struct or union. */
618 static struct c_struct_parse_info *struct_parse_info;
620 /* Forward declarations. */
621 static tree lookup_name_in_scope (tree, struct c_scope *);
622 static tree c_make_fname_decl (location_t, tree, int);
623 static tree grokdeclarator (const struct c_declarator *,
624 struct c_declspecs *,
625 enum decl_context, bool, tree *, tree *, tree *,
626 bool *, enum deprecated_states);
627 static tree grokparms (struct c_arg_info *, bool);
628 static void layout_array_type (tree);
629 static void warn_defaults_to (location_t, int, const char *, ...)
630 ATTRIBUTE_GCC_DIAG(3,4);
632 /* T is a statement. Add it to the statement-tree. This is the
633 C/ObjC version--C++ has a slightly different version of this
634 function. */
636 tree
637 add_stmt (tree t)
639 enum tree_code code = TREE_CODE (t);
641 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
643 if (!EXPR_HAS_LOCATION (t))
644 SET_EXPR_LOCATION (t, input_location);
647 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
648 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
650 /* Add T to the statement-tree. Non-side-effect statements need to be
651 recorded during statement expressions. */
652 if (!building_stmt_list_p ())
653 push_stmt_list ();
654 append_to_statement_list_force (t, &cur_stmt_list);
656 return t;
659 /* Build a pointer type using the default pointer mode. */
661 static tree
662 c_build_pointer_type (tree to_type)
664 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
665 : TYPE_ADDR_SPACE (to_type);
666 machine_mode pointer_mode;
668 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
669 pointer_mode = targetm.addr_space.pointer_mode (as);
670 else
671 pointer_mode = c_default_pointer_mode;
672 return build_pointer_type_for_mode (to_type, pointer_mode, false);
676 /* Return true if we will want to say something if a goto statement
677 crosses DECL. */
679 static bool
680 decl_jump_unsafe (tree decl)
682 if (error_operand_p (decl))
683 return false;
685 /* Always warn about crossing variably modified types. */
686 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
687 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
688 return true;
690 /* Otherwise, only warn if -Wgoto-misses-init and this is an
691 initialized automatic decl. */
692 if (warn_jump_misses_init
693 && TREE_CODE (decl) == VAR_DECL
694 && !TREE_STATIC (decl)
695 && DECL_INITIAL (decl) != NULL_TREE)
696 return true;
698 return false;
702 void
703 c_print_identifier (FILE *file, tree node, int indent)
705 void (*save) (enum c_oracle_request, tree identifier);
707 /* Temporarily hide any binding oracle. Without this, calls to
708 debug_tree from the debugger will end up calling into the oracle,
709 making for a confusing debug session. As the oracle isn't needed
710 here for normal operation, it's simplest to suppress it. */
711 save = c_binding_oracle;
712 c_binding_oracle = NULL;
714 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
715 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
716 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
717 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
719 tree rid = ridpointers[C_RID_CODE (node)];
720 indent_to (file, indent + 4);
721 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
722 (void *) rid, IDENTIFIER_POINTER (rid));
725 c_binding_oracle = save;
728 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
729 which may be any of several kinds of DECL or TYPE or error_mark_node,
730 in the scope SCOPE. */
731 static void
732 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
733 bool nested, location_t locus)
735 struct c_binding *b, **here;
737 if (binding_freelist)
739 b = binding_freelist;
740 binding_freelist = b->prev;
742 else
743 b = ggc_alloc<c_binding> ();
745 b->shadowed = 0;
746 b->decl = decl;
747 b->id = name;
748 b->depth = scope->depth;
749 b->invisible = invisible;
750 b->nested = nested;
751 b->inner_comp = 0;
752 b->in_struct = 0;
753 b->locus = locus;
755 b->u.type = NULL;
757 b->prev = scope->bindings;
758 scope->bindings = b;
760 if (decl_jump_unsafe (decl))
761 scope->has_jump_unsafe_decl = 1;
763 if (!name)
764 return;
766 switch (TREE_CODE (decl))
768 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
769 case ENUMERAL_TYPE:
770 case UNION_TYPE:
771 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
772 case VAR_DECL:
773 case FUNCTION_DECL:
774 case TYPE_DECL:
775 case CONST_DECL:
776 case PARM_DECL:
777 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
779 default:
780 gcc_unreachable ();
783 /* Locate the appropriate place in the chain of shadowed decls
784 to insert this binding. Normally, scope == current_scope and
785 this does nothing. */
786 while (*here && (*here)->depth > scope->depth)
787 here = &(*here)->shadowed;
789 b->shadowed = *here;
790 *here = b;
793 /* Clear the binding structure B, stick it on the binding_freelist,
794 and return the former value of b->prev. This is used by pop_scope
795 and get_parm_info to iterate destructively over all the bindings
796 from a given scope. */
797 static struct c_binding *
798 free_binding_and_advance (struct c_binding *b)
800 struct c_binding *prev = b->prev;
802 memset (b, 0, sizeof (struct c_binding));
803 b->prev = binding_freelist;
804 binding_freelist = b;
806 return prev;
809 /* Bind a label. Like bind, but skip fields which aren't used for
810 labels, and add the LABEL_VARS value. */
811 static void
812 bind_label (tree name, tree label, struct c_scope *scope,
813 struct c_label_vars *label_vars)
815 struct c_binding *b;
817 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
818 UNKNOWN_LOCATION);
820 scope->has_label_bindings = true;
822 b = scope->bindings;
823 gcc_assert (b->decl == label);
824 label_vars->shadowed = b->u.label;
825 b->u.label = label_vars;
828 /* Hook called at end of compilation to assume 1 elt
829 for a file-scope tentative array defn that wasn't complete before. */
831 void
832 c_finish_incomplete_decl (tree decl)
834 if (TREE_CODE (decl) == VAR_DECL)
836 tree type = TREE_TYPE (decl);
837 if (type != error_mark_node
838 && TREE_CODE (type) == ARRAY_TYPE
839 && !DECL_EXTERNAL (decl)
840 && TYPE_DOMAIN (type) == 0)
842 warning_at (DECL_SOURCE_LOCATION (decl),
843 0, "array %q+D assumed to have one element", decl);
845 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
847 relayout_decl (decl);
852 /* Record that inline function FUNC contains a reference (location
853 LOC) to static DECL (file-scope or function-local according to
854 TYPE). */
856 void
857 record_inline_static (location_t loc, tree func, tree decl,
858 enum c_inline_static_type type)
860 c_inline_static *csi = ggc_alloc<c_inline_static> ();
861 csi->location = loc;
862 csi->function = func;
863 csi->static_decl = decl;
864 csi->type = type;
865 csi->next = c_inline_statics;
866 c_inline_statics = csi;
869 /* Check for references to static declarations in inline functions at
870 the end of the translation unit and diagnose them if the functions
871 are still inline definitions. */
873 static void
874 check_inline_statics (void)
876 struct c_inline_static *csi;
877 for (csi = c_inline_statics; csi; csi = csi->next)
879 if (DECL_EXTERNAL (csi->function))
880 switch (csi->type)
882 case csi_internal:
883 pedwarn (csi->location, 0,
884 "%qD is static but used in inline function %qD "
885 "which is not static", csi->static_decl, csi->function);
886 break;
887 case csi_modifiable:
888 pedwarn (csi->location, 0,
889 "%q+D is static but declared in inline function %qD "
890 "which is not static", csi->static_decl, csi->function);
891 break;
892 default:
893 gcc_unreachable ();
896 c_inline_statics = NULL;
899 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
900 for the current state, otherwise set it to uninitialized. */
902 static void
903 set_spot_bindings (struct c_spot_bindings *p, bool defining)
905 if (defining)
907 p->scope = current_scope;
908 p->bindings_in_scope = current_scope->bindings;
910 else
912 p->scope = NULL;
913 p->bindings_in_scope = NULL;
915 p->stmt_exprs = 0;
916 p->left_stmt_expr = false;
919 /* Update spot bindings P as we pop out of SCOPE. Return true if we
920 should push decls for a label. */
922 static bool
923 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
925 if (p->scope != scope)
927 /* This label or goto is defined in some other scope, or it is a
928 label which is not yet defined. There is nothing to
929 update. */
930 return false;
933 /* Adjust the spot bindings to refer to the bindings already defined
934 in the enclosing scope. */
935 p->scope = scope->outer;
936 p->bindings_in_scope = p->scope->bindings;
938 return true;
941 /* The Objective-C front-end often needs to determine the current scope. */
943 void *
944 objc_get_current_scope (void)
946 return current_scope;
949 /* The following function is used only by Objective-C. It needs to live here
950 because it accesses the innards of c_scope. */
952 void
953 objc_mark_locals_volatile (void *enclosing_blk)
955 struct c_scope *scope;
956 struct c_binding *b;
958 for (scope = current_scope;
959 scope && scope != enclosing_blk;
960 scope = scope->outer)
962 for (b = scope->bindings; b; b = b->prev)
963 objc_volatilize_decl (b->decl);
965 /* Do not climb up past the current function. */
966 if (scope->function_body)
967 break;
971 /* Return true if we are in the global binding level. */
973 bool
974 global_bindings_p (void)
976 return current_scope == file_scope;
979 void
980 keep_next_level (void)
982 keep_next_level_flag = true;
985 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
987 void
988 set_float_const_decimal64 (void)
990 current_scope->float_const_decimal64 = true;
993 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
995 void
996 clear_float_const_decimal64 (void)
998 current_scope->float_const_decimal64 = false;
1001 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
1003 bool
1004 float_const_decimal64_p (void)
1006 return current_scope->float_const_decimal64;
1009 /* Identify this scope as currently being filled with parameters. */
1011 void
1012 declare_parm_level (void)
1014 current_scope->parm_flag = true;
1017 void
1018 push_scope (void)
1020 if (next_is_function_body)
1022 /* This is the transition from the parameters to the top level
1023 of the function body. These are the same scope
1024 (C99 6.2.1p4,6) so we do not push another scope structure.
1025 next_is_function_body is set only by store_parm_decls, which
1026 in turn is called when and only when we are about to
1027 encounter the opening curly brace for the function body.
1029 The outermost block of a function always gets a BLOCK node,
1030 because the debugging output routines expect that each
1031 function has at least one BLOCK. */
1032 current_scope->parm_flag = false;
1033 current_scope->function_body = true;
1034 current_scope->keep = true;
1035 current_scope->outer_function = current_function_scope;
1036 current_function_scope = current_scope;
1038 keep_next_level_flag = false;
1039 next_is_function_body = false;
1041 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1042 if (current_scope->outer)
1043 current_scope->float_const_decimal64
1044 = current_scope->outer->float_const_decimal64;
1045 else
1046 current_scope->float_const_decimal64 = false;
1048 else
1050 struct c_scope *scope;
1051 if (scope_freelist)
1053 scope = scope_freelist;
1054 scope_freelist = scope->outer;
1056 else
1057 scope = ggc_cleared_alloc<c_scope> ();
1059 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1060 if (current_scope)
1061 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1062 else
1063 scope->float_const_decimal64 = false;
1065 scope->keep = keep_next_level_flag;
1066 scope->outer = current_scope;
1067 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1069 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1070 possible. */
1071 if (current_scope && scope->depth == 0)
1073 scope->depth--;
1074 sorry ("GCC supports only %u nested scopes", scope->depth);
1077 current_scope = scope;
1078 keep_next_level_flag = false;
1082 /* This is called when we are leaving SCOPE. For each label defined
1083 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1084 These are the decls whose initialization will be skipped by a goto
1085 later in the function. */
1087 static void
1088 update_label_decls (struct c_scope *scope)
1090 struct c_scope *s;
1092 s = scope;
1093 while (s != NULL)
1095 if (s->has_label_bindings)
1097 struct c_binding *b;
1099 for (b = s->bindings; b != NULL; b = b->prev)
1101 struct c_label_vars *label_vars;
1102 struct c_binding *b1;
1103 bool hjud;
1104 unsigned int ix;
1105 struct c_goto_bindings *g;
1107 if (TREE_CODE (b->decl) != LABEL_DECL)
1108 continue;
1109 label_vars = b->u.label;
1111 b1 = label_vars->label_bindings.bindings_in_scope;
1112 if (label_vars->label_bindings.scope == NULL)
1113 hjud = false;
1114 else
1115 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1116 if (update_spot_bindings (scope, &label_vars->label_bindings))
1118 /* This label is defined in this scope. */
1119 if (hjud)
1121 for (; b1 != NULL; b1 = b1->prev)
1123 /* A goto from later in the function to this
1124 label will never see the initialization
1125 of B1, if any. Save it to issue a
1126 warning if needed. */
1127 if (decl_jump_unsafe (b1->decl))
1128 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1133 /* Update the bindings of any goto statements associated
1134 with this label. */
1135 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1136 update_spot_bindings (scope, &g->goto_bindings);
1140 /* Don't search beyond the current function. */
1141 if (s == current_function_scope)
1142 break;
1144 s = s->outer;
1148 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1150 static void
1151 set_type_context (tree type, tree context)
1153 for (type = TYPE_MAIN_VARIANT (type); type;
1154 type = TYPE_NEXT_VARIANT (type))
1155 TYPE_CONTEXT (type) = context;
1158 /* Exit a scope. Restore the state of the identifier-decl mappings
1159 that were in effect when this scope was entered. Return a BLOCK
1160 node containing all the DECLs in this scope that are of interest
1161 to debug info generation. */
1163 tree
1164 pop_scope (void)
1166 struct c_scope *scope = current_scope;
1167 tree block, context, p;
1168 struct c_binding *b;
1170 bool functionbody = scope->function_body;
1171 bool keep = functionbody || scope->keep || scope->bindings;
1173 update_label_decls (scope);
1175 /* If appropriate, create a BLOCK to record the decls for the life
1176 of this function. */
1177 block = 0;
1178 if (keep)
1180 block = make_node (BLOCK);
1181 BLOCK_SUBBLOCKS (block) = scope->blocks;
1182 TREE_USED (block) = 1;
1184 /* In each subblock, record that this is its superior. */
1185 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1186 BLOCK_SUPERCONTEXT (p) = block;
1188 BLOCK_VARS (block) = 0;
1191 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1192 scope must be set so that they point to the appropriate
1193 construct, i.e. either to the current FUNCTION_DECL node, or
1194 else to the BLOCK node we just constructed.
1196 Note that for tagged types whose scope is just the formal
1197 parameter list for some function type specification, we can't
1198 properly set their TYPE_CONTEXTs here, because we don't have a
1199 pointer to the appropriate FUNCTION_TYPE node readily available
1200 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1201 type nodes get set in `grokdeclarator' as soon as we have created
1202 the FUNCTION_TYPE node which will represent the "scope" for these
1203 "parameter list local" tagged types. */
1204 if (scope->function_body)
1205 context = current_function_decl;
1206 else if (scope == file_scope)
1208 tree file_decl = build_translation_unit_decl (NULL_TREE);
1209 context = file_decl;
1211 else
1212 context = block;
1214 /* Clear all bindings in this scope. */
1215 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1217 p = b->decl;
1218 switch (TREE_CODE (p))
1220 case LABEL_DECL:
1221 /* Warnings for unused labels, errors for undefined labels. */
1222 if (TREE_USED (p) && !DECL_INITIAL (p))
1224 error ("label %q+D used but not defined", p);
1225 DECL_INITIAL (p) = error_mark_node;
1227 else
1228 warn_for_unused_label (p);
1230 /* Labels go in BLOCK_VARS. */
1231 DECL_CHAIN (p) = BLOCK_VARS (block);
1232 BLOCK_VARS (block) = p;
1233 gcc_assert (I_LABEL_BINDING (b->id) == b);
1234 I_LABEL_BINDING (b->id) = b->shadowed;
1236 /* Also pop back to the shadowed label_vars. */
1237 release_tree_vector (b->u.label->decls_in_scope);
1238 b->u.label = b->u.label->shadowed;
1239 break;
1241 case ENUMERAL_TYPE:
1242 case UNION_TYPE:
1243 case RECORD_TYPE:
1244 set_type_context (p, context);
1246 /* Types may not have tag-names, in which case the type
1247 appears in the bindings list with b->id NULL. */
1248 if (b->id)
1250 gcc_assert (I_TAG_BINDING (b->id) == b);
1251 I_TAG_BINDING (b->id) = b->shadowed;
1253 break;
1255 case FUNCTION_DECL:
1256 /* Propagate TREE_ADDRESSABLE from nested functions to their
1257 containing functions. */
1258 if (!TREE_ASM_WRITTEN (p)
1259 && DECL_INITIAL (p) != 0
1260 && TREE_ADDRESSABLE (p)
1261 && DECL_ABSTRACT_ORIGIN (p) != 0
1262 && DECL_ABSTRACT_ORIGIN (p) != p)
1263 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1264 if (!DECL_EXTERNAL (p)
1265 && !DECL_INITIAL (p)
1266 && scope != file_scope
1267 && scope != external_scope)
1269 error ("nested function %q+D declared but never defined", p);
1270 undef_nested_function = true;
1272 else if (DECL_DECLARED_INLINE_P (p)
1273 && TREE_PUBLIC (p)
1274 && !DECL_INITIAL (p))
1276 /* C99 6.7.4p6: "a function with external linkage... declared
1277 with an inline function specifier ... shall also be defined
1278 in the same translation unit." */
1279 if (!flag_gnu89_inline
1280 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1281 && scope != external_scope)
1282 pedwarn (input_location, 0,
1283 "inline function %q+D declared but never defined", p);
1284 DECL_EXTERNAL (p) = 1;
1287 goto common_symbol;
1289 case VAR_DECL:
1290 /* Warnings for unused variables. */
1291 if ((!TREE_USED (p) || !DECL_READ_P (p))
1292 && !TREE_NO_WARNING (p)
1293 && !DECL_IN_SYSTEM_HEADER (p)
1294 && DECL_NAME (p)
1295 && !DECL_ARTIFICIAL (p)
1296 && scope != file_scope
1297 && scope != external_scope)
1299 if (!TREE_USED (p))
1300 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1301 else if (DECL_CONTEXT (p) == current_function_decl)
1302 warning_at (DECL_SOURCE_LOCATION (p),
1303 OPT_Wunused_but_set_variable,
1304 "variable %qD set but not used", p);
1307 if (b->inner_comp)
1309 error ("type of array %q+D completed incompatibly with"
1310 " implicit initialization", p);
1313 /* Fall through. */
1314 case TYPE_DECL:
1315 case CONST_DECL:
1316 common_symbol:
1317 /* All of these go in BLOCK_VARS, but only if this is the
1318 binding in the home scope. */
1319 if (!b->nested)
1321 DECL_CHAIN (p) = BLOCK_VARS (block);
1322 BLOCK_VARS (block) = p;
1324 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1326 /* For block local externs add a special
1327 DECL_EXTERNAL decl for debug info generation. */
1328 tree extp = copy_node (p);
1330 DECL_EXTERNAL (extp) = 1;
1331 TREE_STATIC (extp) = 0;
1332 TREE_PUBLIC (extp) = 1;
1333 DECL_INITIAL (extp) = NULL_TREE;
1334 DECL_LANG_SPECIFIC (extp) = NULL;
1335 DECL_CONTEXT (extp) = current_function_decl;
1336 if (TREE_CODE (p) == FUNCTION_DECL)
1338 DECL_RESULT (extp) = NULL_TREE;
1339 DECL_SAVED_TREE (extp) = NULL_TREE;
1340 DECL_STRUCT_FUNCTION (extp) = NULL;
1342 if (b->locus != UNKNOWN_LOCATION)
1343 DECL_SOURCE_LOCATION (extp) = b->locus;
1344 DECL_CHAIN (extp) = BLOCK_VARS (block);
1345 BLOCK_VARS (block) = extp;
1347 /* If this is the file scope set DECL_CONTEXT of each decl to
1348 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1349 work. */
1350 if (scope == file_scope)
1352 DECL_CONTEXT (p) = context;
1353 if (TREE_CODE (p) == TYPE_DECL
1354 && TREE_TYPE (p) != error_mark_node)
1355 set_type_context (TREE_TYPE (p), context);
1358 /* Fall through. */
1359 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1360 already been put there by store_parm_decls. Unused-
1361 parameter warnings are handled by function.c.
1362 error_mark_node obviously does not go in BLOCK_VARS and
1363 does not get unused-variable warnings. */
1364 case PARM_DECL:
1365 case ERROR_MARK:
1366 /* It is possible for a decl not to have a name. We get
1367 here with b->id NULL in this case. */
1368 if (b->id)
1370 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1371 I_SYMBOL_BINDING (b->id) = b->shadowed;
1372 if (b->shadowed && b->shadowed->u.type)
1373 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1375 break;
1377 default:
1378 gcc_unreachable ();
1383 /* Dispose of the block that we just made inside some higher level. */
1384 if ((scope->function_body || scope == file_scope) && context)
1386 DECL_INITIAL (context) = block;
1387 BLOCK_SUPERCONTEXT (block) = context;
1389 else if (scope->outer)
1391 if (block)
1392 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1393 /* If we did not make a block for the scope just exited, any
1394 blocks made for inner scopes must be carried forward so they
1395 will later become subblocks of something else. */
1396 else if (scope->blocks)
1397 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1400 /* Pop the current scope, and free the structure for reuse. */
1401 current_scope = scope->outer;
1402 if (scope->function_body)
1403 current_function_scope = scope->outer_function;
1405 memset (scope, 0, sizeof (struct c_scope));
1406 scope->outer = scope_freelist;
1407 scope_freelist = scope;
1409 return block;
1412 void
1413 push_file_scope (void)
1415 tree decl;
1417 if (file_scope)
1418 return;
1420 push_scope ();
1421 file_scope = current_scope;
1423 start_fname_decls ();
1425 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1426 bind (DECL_NAME (decl), decl, file_scope,
1427 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1430 void
1431 pop_file_scope (void)
1433 /* In case there were missing closebraces, get us back to the global
1434 binding level. */
1435 while (current_scope != file_scope)
1436 pop_scope ();
1438 /* __FUNCTION__ is defined at file scope (""). This
1439 call may not be necessary as my tests indicate it
1440 still works without it. */
1441 finish_fname_decls ();
1443 check_inline_statics ();
1445 /* This is the point to write out a PCH if we're doing that.
1446 In that case we do not want to do anything else. */
1447 if (pch_file)
1449 c_common_write_pch ();
1450 return;
1453 /* Pop off the file scope and close this translation unit. */
1454 pop_scope ();
1455 file_scope = 0;
1457 maybe_apply_pending_pragma_weaks ();
1460 /* Adjust the bindings for the start of a statement expression. */
1462 void
1463 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1465 struct c_scope *scope;
1467 for (scope = current_scope; scope != NULL; scope = scope->outer)
1469 struct c_binding *b;
1471 if (!scope->has_label_bindings)
1472 continue;
1474 for (b = scope->bindings; b != NULL; b = b->prev)
1476 struct c_label_vars *label_vars;
1477 unsigned int ix;
1478 struct c_goto_bindings *g;
1480 if (TREE_CODE (b->decl) != LABEL_DECL)
1481 continue;
1482 label_vars = b->u.label;
1483 ++label_vars->label_bindings.stmt_exprs;
1484 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1485 ++g->goto_bindings.stmt_exprs;
1489 if (switch_bindings != NULL)
1490 ++switch_bindings->stmt_exprs;
1493 /* Adjust the bindings for the end of a statement expression. */
1495 void
1496 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1498 struct c_scope *scope;
1500 for (scope = current_scope; scope != NULL; scope = scope->outer)
1502 struct c_binding *b;
1504 if (!scope->has_label_bindings)
1505 continue;
1507 for (b = scope->bindings; b != NULL; b = b->prev)
1509 struct c_label_vars *label_vars;
1510 unsigned int ix;
1511 struct c_goto_bindings *g;
1513 if (TREE_CODE (b->decl) != LABEL_DECL)
1514 continue;
1515 label_vars = b->u.label;
1516 --label_vars->label_bindings.stmt_exprs;
1517 if (label_vars->label_bindings.stmt_exprs < 0)
1519 label_vars->label_bindings.left_stmt_expr = true;
1520 label_vars->label_bindings.stmt_exprs = 0;
1522 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1524 --g->goto_bindings.stmt_exprs;
1525 if (g->goto_bindings.stmt_exprs < 0)
1527 g->goto_bindings.left_stmt_expr = true;
1528 g->goto_bindings.stmt_exprs = 0;
1534 if (switch_bindings != NULL)
1536 --switch_bindings->stmt_exprs;
1537 gcc_assert (switch_bindings->stmt_exprs >= 0);
1541 /* Push a definition or a declaration of struct, union or enum tag "name".
1542 "type" should be the type node.
1543 We assume that the tag "name" is not already defined, and has a location
1544 of LOC.
1546 Note that the definition may really be just a forward reference.
1547 In that case, the TYPE_SIZE will be zero. */
1549 static void
1550 pushtag (location_t loc, tree name, tree type)
1552 /* Record the identifier as the type's name if it has none. */
1553 if (name && !TYPE_NAME (type))
1554 TYPE_NAME (type) = name;
1555 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1557 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1558 tagged type we just added to the current scope. This fake
1559 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1560 to output a representation of a tagged type, and it also gives
1561 us a convenient place to record the "scope start" address for the
1562 tagged type. */
1564 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1565 TYPE_DECL, NULL_TREE, type));
1567 /* An approximation for now, so we can tell this is a function-scope tag.
1568 This will be updated in pop_scope. */
1569 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1571 if (warn_cxx_compat && name != NULL_TREE)
1573 struct c_binding *b = I_SYMBOL_BINDING (name);
1575 if (b != NULL
1576 && b->decl != NULL_TREE
1577 && TREE_CODE (b->decl) == TYPE_DECL
1578 && (B_IN_CURRENT_SCOPE (b)
1579 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1580 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1581 != TYPE_MAIN_VARIANT (type)))
1583 warning_at (loc, OPT_Wc___compat,
1584 ("using %qD as both a typedef and a tag is "
1585 "invalid in C++"),
1586 b->decl);
1587 if (b->locus != UNKNOWN_LOCATION)
1588 inform (b->locus, "originally defined here");
1593 /* An exported interface to pushtag. This is used by the gdb plugin's
1594 binding oracle to introduce a new tag binding. */
1596 void
1597 c_pushtag (location_t loc, tree name, tree type)
1599 pushtag (loc, name, type);
1602 /* An exported interface to bind a declaration. LOC is the location
1603 to use. DECL is the declaration to bind. The decl's name is used
1604 to determine how it is bound. If DECL is a VAR_DECL, then
1605 IS_GLOBAL determines whether the decl is put into the global (file
1606 and external) scope or the current function's scope; if DECL is not
1607 a VAR_DECL then it is always put into the file scope. */
1609 void
1610 c_bind (location_t loc, tree decl, bool is_global)
1612 struct c_scope *scope;
1613 bool nested = false;
1615 if (TREE_CODE (decl) != VAR_DECL || current_function_scope == NULL)
1617 /* Types and functions are always considered to be global. */
1618 scope = file_scope;
1619 DECL_EXTERNAL (decl) = 1;
1620 TREE_PUBLIC (decl) = 1;
1622 else if (is_global)
1624 /* Also bind it into the external scope. */
1625 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1626 nested = true;
1627 scope = file_scope;
1628 DECL_EXTERNAL (decl) = 1;
1629 TREE_PUBLIC (decl) = 1;
1631 else
1633 DECL_CONTEXT (decl) = current_function_decl;
1634 TREE_PUBLIC (decl) = 0;
1635 scope = current_function_scope;
1638 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1641 /* Subroutine of compare_decls. Allow harmless mismatches in return
1642 and argument types provided that the type modes match. This function
1643 return a unified type given a suitable match, and 0 otherwise. */
1645 static tree
1646 match_builtin_function_types (tree newtype, tree oldtype)
1648 tree newrettype, oldrettype;
1649 tree newargs, oldargs;
1650 tree trytype, tryargs;
1652 /* Accept the return type of the new declaration if same modes. */
1653 oldrettype = TREE_TYPE (oldtype);
1654 newrettype = TREE_TYPE (newtype);
1656 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1657 return 0;
1659 oldargs = TYPE_ARG_TYPES (oldtype);
1660 newargs = TYPE_ARG_TYPES (newtype);
1661 tryargs = newargs;
1663 while (oldargs || newargs)
1665 if (!oldargs
1666 || !newargs
1667 || !TREE_VALUE (oldargs)
1668 || !TREE_VALUE (newargs)
1669 || TYPE_MODE (TREE_VALUE (oldargs))
1670 != TYPE_MODE (TREE_VALUE (newargs)))
1671 return 0;
1673 oldargs = TREE_CHAIN (oldargs);
1674 newargs = TREE_CHAIN (newargs);
1677 trytype = build_function_type (newrettype, tryargs);
1678 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1681 /* Subroutine of diagnose_mismatched_decls. Check for function type
1682 mismatch involving an empty arglist vs a nonempty one and give clearer
1683 diagnostics. */
1684 static void
1685 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1686 tree newtype, tree oldtype)
1688 tree t;
1690 if (TREE_CODE (olddecl) != FUNCTION_DECL
1691 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1692 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1693 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1694 return;
1696 t = TYPE_ARG_TYPES (oldtype);
1697 if (t == 0)
1698 t = TYPE_ARG_TYPES (newtype);
1699 for (; t; t = TREE_CHAIN (t))
1701 tree type = TREE_VALUE (t);
1703 if (TREE_CHAIN (t) == 0
1704 && TYPE_MAIN_VARIANT (type) != void_type_node)
1706 inform (input_location, "a parameter list with an ellipsis can%'t match "
1707 "an empty parameter name list declaration");
1708 break;
1711 if (c_type_promotes_to (type) != type)
1713 inform (input_location, "an argument type that has a default promotion can%'t match "
1714 "an empty parameter name list declaration");
1715 break;
1720 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1721 old-style function definition, NEWDECL is a prototype declaration.
1722 Diagnose inconsistencies in the argument list. Returns TRUE if
1723 the prototype is compatible, FALSE if not. */
1724 static bool
1725 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1727 tree newargs, oldargs;
1728 int i;
1730 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1732 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1733 newargs = TYPE_ARG_TYPES (newtype);
1734 i = 1;
1736 for (;;)
1738 tree oldargtype = TREE_VALUE (oldargs);
1739 tree newargtype = TREE_VALUE (newargs);
1741 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1742 return false;
1744 oldargtype = (TYPE_ATOMIC (oldargtype)
1745 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1746 TYPE_QUAL_ATOMIC)
1747 : TYPE_MAIN_VARIANT (oldargtype));
1748 newargtype = (TYPE_ATOMIC (newargtype)
1749 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1750 TYPE_QUAL_ATOMIC)
1751 : TYPE_MAIN_VARIANT (newargtype));
1753 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1754 break;
1756 /* Reaching the end of just one list means the two decls don't
1757 agree on the number of arguments. */
1758 if (END_OF_ARGLIST (oldargtype))
1760 error ("prototype for %q+D declares more arguments "
1761 "than previous old-style definition", newdecl);
1762 return false;
1764 else if (END_OF_ARGLIST (newargtype))
1766 error ("prototype for %q+D declares fewer arguments "
1767 "than previous old-style definition", newdecl);
1768 return false;
1771 /* Type for passing arg must be consistent with that declared
1772 for the arg. */
1773 else if (!comptypes (oldargtype, newargtype))
1775 error ("prototype for %q+D declares argument %d"
1776 " with incompatible type",
1777 newdecl, i);
1778 return false;
1781 oldargs = TREE_CHAIN (oldargs);
1782 newargs = TREE_CHAIN (newargs);
1783 i++;
1786 /* If we get here, no errors were found, but do issue a warning
1787 for this poor-style construct. */
1788 warning (0, "prototype for %q+D follows non-prototype definition",
1789 newdecl);
1790 return true;
1791 #undef END_OF_ARGLIST
1794 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1795 first in a pair of mismatched declarations, using the diagnostic
1796 function DIAG. */
1797 static void
1798 locate_old_decl (tree decl)
1800 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1801 && !C_DECL_DECLARED_BUILTIN (decl))
1803 else if (DECL_INITIAL (decl))
1804 inform (input_location, "previous definition of %q+D was here", decl);
1805 else if (C_DECL_IMPLICIT (decl))
1806 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1807 else
1808 inform (input_location, "previous declaration of %q+D was here", decl);
1811 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1812 Returns true if the caller should proceed to merge the two, false
1813 if OLDDECL should simply be discarded. As a side effect, issues
1814 all necessary diagnostics for invalid or poor-style combinations.
1815 If it returns true, writes the types of NEWDECL and OLDDECL to
1816 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1817 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1819 static bool
1820 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1821 tree *newtypep, tree *oldtypep)
1823 tree newtype, oldtype;
1824 bool pedwarned = false;
1825 bool warned = false;
1826 bool retval = true;
1828 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1829 && DECL_EXTERNAL (DECL))
1831 /* If we have error_mark_node for either decl or type, just discard
1832 the previous decl - we're in an error cascade already. */
1833 if (olddecl == error_mark_node || newdecl == error_mark_node)
1834 return false;
1835 *oldtypep = oldtype = TREE_TYPE (olddecl);
1836 *newtypep = newtype = TREE_TYPE (newdecl);
1837 if (oldtype == error_mark_node || newtype == error_mark_node)
1838 return false;
1840 /* Two different categories of symbol altogether. This is an error
1841 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1842 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1844 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1845 && DECL_BUILT_IN (olddecl)
1846 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1848 error ("%q+D redeclared as different kind of symbol", newdecl);
1849 locate_old_decl (olddecl);
1851 else if (TREE_PUBLIC (newdecl))
1852 warning (0, "built-in function %q+D declared as non-function",
1853 newdecl);
1854 else
1855 warning (OPT_Wshadow, "declaration of %q+D shadows "
1856 "a built-in function", newdecl);
1857 return false;
1860 /* Enumerators have no linkage, so may only be declared once in a
1861 given scope. */
1862 if (TREE_CODE (olddecl) == CONST_DECL)
1864 error ("redeclaration of enumerator %q+D", newdecl);
1865 locate_old_decl (olddecl);
1866 return false;
1869 if (!comptypes (oldtype, newtype))
1871 if (TREE_CODE (olddecl) == FUNCTION_DECL
1872 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1874 /* Accept harmless mismatch in function types.
1875 This is for the ffs and fprintf builtins. */
1876 tree trytype = match_builtin_function_types (newtype, oldtype);
1878 if (trytype && comptypes (newtype, trytype))
1879 *oldtypep = oldtype = trytype;
1880 else
1882 /* If types don't match for a built-in, throw away the
1883 built-in. No point in calling locate_old_decl here, it
1884 won't print anything. */
1885 warning (0, "conflicting types for built-in function %q+D",
1886 newdecl);
1887 return false;
1890 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1891 && DECL_IS_BUILTIN (olddecl))
1893 /* A conflicting function declaration for a predeclared
1894 function that isn't actually built in. Objective C uses
1895 these. The new declaration silently overrides everything
1896 but the volatility (i.e. noreturn) indication. See also
1897 below. FIXME: Make Objective C use normal builtins. */
1898 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1899 return false;
1901 /* Permit void foo (...) to match int foo (...) if the latter is
1902 the definition and implicit int was used. See
1903 c-torture/compile/920625-2.c. */
1904 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1905 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1906 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1907 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1909 pedwarned = pedwarn (input_location, 0,
1910 "conflicting types for %q+D", newdecl);
1911 /* Make sure we keep void as the return type. */
1912 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1913 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1915 /* Permit void foo (...) to match an earlier call to foo (...) with
1916 no declared type (thus, implicitly int). */
1917 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1918 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1919 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1920 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1922 pedwarned = pedwarn (input_location, 0,
1923 "conflicting types for %q+D", newdecl);
1924 /* Make sure we keep void as the return type. */
1925 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1927 else
1929 int new_quals = TYPE_QUALS (newtype);
1930 int old_quals = TYPE_QUALS (oldtype);
1932 if (new_quals != old_quals)
1934 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1935 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1936 if (new_addr != old_addr)
1938 if (ADDR_SPACE_GENERIC_P (new_addr))
1939 error ("conflicting named address spaces (generic vs %s) "
1940 "for %q+D",
1941 c_addr_space_name (old_addr), newdecl);
1942 else if (ADDR_SPACE_GENERIC_P (old_addr))
1943 error ("conflicting named address spaces (%s vs generic) "
1944 "for %q+D",
1945 c_addr_space_name (new_addr), newdecl);
1946 else
1947 error ("conflicting named address spaces (%s vs %s) "
1948 "for %q+D",
1949 c_addr_space_name (new_addr),
1950 c_addr_space_name (old_addr),
1951 newdecl);
1954 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1955 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1956 error ("conflicting type qualifiers for %q+D", newdecl);
1958 else
1959 error ("conflicting types for %q+D", newdecl);
1960 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1961 locate_old_decl (olddecl);
1962 return false;
1966 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1967 but silently ignore the redeclaration if either is in a system
1968 header. (Conflicting redeclarations were handled above.) This
1969 is allowed for C11 if the types are the same, not just
1970 compatible. */
1971 if (TREE_CODE (newdecl) == TYPE_DECL)
1973 bool types_different = false;
1974 int comptypes_result;
1976 comptypes_result
1977 = comptypes_check_different_types (oldtype, newtype, &types_different);
1979 if (comptypes_result != 1 || types_different)
1981 error ("redefinition of typedef %q+D with different type", newdecl);
1982 locate_old_decl (olddecl);
1983 return false;
1986 if (DECL_IN_SYSTEM_HEADER (newdecl)
1987 || DECL_IN_SYSTEM_HEADER (olddecl)
1988 || TREE_NO_WARNING (newdecl)
1989 || TREE_NO_WARNING (olddecl))
1990 return true; /* Allow OLDDECL to continue in use. */
1992 if (variably_modified_type_p (newtype, NULL))
1994 error ("redefinition of typedef %q+D with variably modified type",
1995 newdecl);
1996 locate_old_decl (olddecl);
1998 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1999 "redefinition of typedef %q+D", newdecl))
2000 locate_old_decl (olddecl);
2002 return true;
2005 /* Function declarations can either be 'static' or 'extern' (no
2006 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2007 can never conflict with each other on account of linkage
2008 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2009 gnu89 mode permits two definitions if one is 'extern inline' and
2010 one is not. The non- extern-inline definition supersedes the
2011 extern-inline definition. */
2013 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2015 /* If you declare a built-in function name as static, or
2016 define the built-in with an old-style definition (so we
2017 can't validate the argument list) the built-in definition is
2018 overridden, but optionally warn this was a bad choice of name. */
2019 if (DECL_BUILT_IN (olddecl)
2020 && !C_DECL_DECLARED_BUILTIN (olddecl)
2021 && (!TREE_PUBLIC (newdecl)
2022 || (DECL_INITIAL (newdecl)
2023 && !prototype_p (TREE_TYPE (newdecl)))))
2025 warning (OPT_Wshadow, "declaration of %q+D shadows "
2026 "a built-in function", newdecl);
2027 /* Discard the old built-in function. */
2028 return false;
2031 if (DECL_INITIAL (newdecl))
2033 if (DECL_INITIAL (olddecl))
2035 /* If both decls are in the same TU and the new declaration
2036 isn't overriding an extern inline reject the new decl.
2037 In c99, no overriding is allowed in the same translation
2038 unit. */
2039 if ((!DECL_EXTERN_INLINE (olddecl)
2040 || DECL_EXTERN_INLINE (newdecl)
2041 || (!flag_gnu89_inline
2042 && (!DECL_DECLARED_INLINE_P (olddecl)
2043 || !lookup_attribute ("gnu_inline",
2044 DECL_ATTRIBUTES (olddecl)))
2045 && (!DECL_DECLARED_INLINE_P (newdecl)
2046 || !lookup_attribute ("gnu_inline",
2047 DECL_ATTRIBUTES (newdecl))))
2049 && same_translation_unit_p (newdecl, olddecl))
2051 error ("redefinition of %q+D", newdecl);
2052 locate_old_decl (olddecl);
2053 return false;
2057 /* If we have a prototype after an old-style function definition,
2058 the argument types must be checked specially. */
2059 else if (DECL_INITIAL (olddecl)
2060 && !prototype_p (oldtype) && prototype_p (newtype)
2061 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2062 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2064 locate_old_decl (olddecl);
2065 return false;
2067 /* A non-static declaration (even an "extern") followed by a
2068 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2069 The same is true for a static forward declaration at block
2070 scope followed by a non-static declaration/definition at file
2071 scope. Static followed by non-static at the same scope is
2072 not undefined behavior, and is the most convenient way to get
2073 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2074 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2075 we do diagnose it if -Wtraditional. */
2076 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2078 /* Two exceptions to the rule. If olddecl is an extern
2079 inline, or a predeclared function that isn't actually
2080 built in, newdecl silently overrides olddecl. The latter
2081 occur only in Objective C; see also above. (FIXME: Make
2082 Objective C use normal builtins.) */
2083 if (!DECL_IS_BUILTIN (olddecl)
2084 && !DECL_EXTERN_INLINE (olddecl))
2086 error ("static declaration of %q+D follows "
2087 "non-static declaration", newdecl);
2088 locate_old_decl (olddecl);
2090 return false;
2092 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2094 if (DECL_CONTEXT (olddecl))
2096 error ("non-static declaration of %q+D follows "
2097 "static declaration", newdecl);
2098 locate_old_decl (olddecl);
2099 return false;
2101 else if (warn_traditional)
2103 warned |= warning (OPT_Wtraditional,
2104 "non-static declaration of %q+D "
2105 "follows static declaration", newdecl);
2109 /* Make sure gnu_inline attribute is either not present, or
2110 present on all inline decls. */
2111 if (DECL_DECLARED_INLINE_P (olddecl)
2112 && DECL_DECLARED_INLINE_P (newdecl))
2114 bool newa = lookup_attribute ("gnu_inline",
2115 DECL_ATTRIBUTES (newdecl)) != NULL;
2116 bool olda = lookup_attribute ("gnu_inline",
2117 DECL_ATTRIBUTES (olddecl)) != NULL;
2118 if (newa != olda)
2120 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2121 newa ? newdecl : olddecl);
2122 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2123 "but not here");
2127 else if (TREE_CODE (newdecl) == VAR_DECL)
2129 /* Only variables can be thread-local, and all declarations must
2130 agree on this property. */
2131 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2133 /* Nothing to check. Since OLDDECL is marked threadprivate
2134 and NEWDECL does not have a thread-local attribute, we
2135 will merge the threadprivate attribute into NEWDECL. */
2138 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2140 if (DECL_THREAD_LOCAL_P (newdecl))
2141 error ("thread-local declaration of %q+D follows "
2142 "non-thread-local declaration", newdecl);
2143 else
2144 error ("non-thread-local declaration of %q+D follows "
2145 "thread-local declaration", newdecl);
2147 locate_old_decl (olddecl);
2148 return false;
2151 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2152 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2154 error ("redefinition of %q+D", newdecl);
2155 locate_old_decl (olddecl);
2156 return false;
2159 /* Objects declared at file scope: if the first declaration had
2160 external linkage (even if it was an external reference) the
2161 second must have external linkage as well, or the behavior is
2162 undefined. If the first declaration had internal linkage, then
2163 the second must too, or else be an external reference (in which
2164 case the composite declaration still has internal linkage).
2165 As for function declarations, we warn about the static-then-
2166 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2167 if (DECL_FILE_SCOPE_P (newdecl)
2168 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2170 if (DECL_EXTERNAL (newdecl))
2172 if (!DECL_FILE_SCOPE_P (olddecl))
2174 error ("extern declaration of %q+D follows "
2175 "declaration with no linkage", newdecl);
2176 locate_old_decl (olddecl);
2177 return false;
2179 else if (warn_traditional)
2181 warned |= warning (OPT_Wtraditional,
2182 "non-static declaration of %q+D "
2183 "follows static declaration", newdecl);
2186 else
2188 if (TREE_PUBLIC (newdecl))
2189 error ("non-static declaration of %q+D follows "
2190 "static declaration", newdecl);
2191 else
2192 error ("static declaration of %q+D follows "
2193 "non-static declaration", newdecl);
2195 locate_old_decl (olddecl);
2196 return false;
2199 /* Two objects with the same name declared at the same block
2200 scope must both be external references (6.7p3). */
2201 else if (!DECL_FILE_SCOPE_P (newdecl))
2203 if (DECL_EXTERNAL (newdecl))
2205 /* Extern with initializer at block scope, which will
2206 already have received an error. */
2208 else if (DECL_EXTERNAL (olddecl))
2210 error ("declaration of %q+D with no linkage follows "
2211 "extern declaration", newdecl);
2212 locate_old_decl (olddecl);
2214 else
2216 error ("redeclaration of %q+D with no linkage", newdecl);
2217 locate_old_decl (olddecl);
2220 return false;
2223 /* C++ does not permit a decl to appear multiple times at file
2224 scope. */
2225 if (warn_cxx_compat
2226 && DECL_FILE_SCOPE_P (newdecl)
2227 && !DECL_EXTERNAL (newdecl)
2228 && !DECL_EXTERNAL (olddecl))
2229 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2230 OPT_Wc___compat,
2231 ("duplicate declaration of %qD is "
2232 "invalid in C++"),
2233 newdecl);
2236 /* warnings */
2237 /* All decls must agree on a visibility. */
2238 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2239 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2240 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2242 warned |= warning (0, "redeclaration of %q+D with different visibility "
2243 "(old visibility preserved)", newdecl);
2246 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2248 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2249 if (DECL_DECLARED_INLINE_P (newdecl)
2250 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2251 warned |= warning (OPT_Wattributes,
2252 "inline declaration of %qD follows "
2253 "declaration with attribute noinline", newdecl);
2254 else if (DECL_DECLARED_INLINE_P (olddecl)
2255 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2256 warned |= warning (OPT_Wattributes,
2257 "declaration of %q+D with attribute "
2258 "noinline follows inline declaration ", newdecl);
2259 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2260 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2261 warned |= warning (OPT_Wattributes,
2262 "declaration of %q+D with attribute "
2263 "%qs follows declaration with attribute %qs",
2264 newdecl, "noinline", "always_inline");
2265 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2266 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2267 warned |= warning (OPT_Wattributes,
2268 "declaration of %q+D with attribute "
2269 "%qs follows declaration with attribute %qs",
2270 newdecl, "always_inline", "noinline");
2271 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2272 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2273 warned |= warning (OPT_Wattributes,
2274 "declaration of %q+D with attribute %qs follows "
2275 "declaration with attribute %qs", newdecl, "cold",
2276 "hot");
2277 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2278 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2279 warned |= warning (OPT_Wattributes,
2280 "declaration of %q+D with attribute %qs follows "
2281 "declaration with attribute %qs", newdecl, "hot",
2282 "cold");
2284 else /* PARM_DECL, VAR_DECL */
2286 /* Redeclaration of a parameter is a constraint violation (this is
2287 not explicitly stated, but follows from C99 6.7p3 [no more than
2288 one declaration of the same identifier with no linkage in the
2289 same scope, except type tags] and 6.2.2p6 [parameters have no
2290 linkage]). We must check for a forward parameter declaration,
2291 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2292 an extension, the mandatory diagnostic for which is handled by
2293 mark_forward_parm_decls. */
2295 if (TREE_CODE (newdecl) == PARM_DECL
2296 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2298 error ("redefinition of parameter %q+D", newdecl);
2299 locate_old_decl (olddecl);
2300 return false;
2304 /* Optional warning for completely redundant decls. */
2305 if (!warned && !pedwarned
2306 && warn_redundant_decls
2307 /* Don't warn about a function declaration followed by a
2308 definition. */
2309 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2310 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2311 /* Don't warn about redundant redeclarations of builtins. */
2312 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2313 && !DECL_BUILT_IN (newdecl)
2314 && DECL_BUILT_IN (olddecl)
2315 && !C_DECL_DECLARED_BUILTIN (olddecl))
2316 /* Don't warn about an extern followed by a definition. */
2317 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2318 /* Don't warn about forward parameter decls. */
2319 && !(TREE_CODE (newdecl) == PARM_DECL
2320 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2321 /* Don't warn about a variable definition following a declaration. */
2322 && !(TREE_CODE (newdecl) == VAR_DECL
2323 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2325 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2326 newdecl);
2329 /* Report location of previous decl/defn. */
2330 if (warned || pedwarned)
2331 locate_old_decl (olddecl);
2333 #undef DECL_EXTERN_INLINE
2335 return retval;
2338 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2339 consistent with OLDDECL, but carries new information. Merge the
2340 new information into OLDDECL. This function issues no
2341 diagnostics. */
2343 static void
2344 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2346 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2347 && DECL_INITIAL (newdecl) != 0);
2348 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2349 && prototype_p (TREE_TYPE (newdecl)));
2350 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2351 && prototype_p (TREE_TYPE (olddecl)));
2353 /* For real parm decl following a forward decl, rechain the old decl
2354 in its new location and clear TREE_ASM_WRITTEN (it's not a
2355 forward decl anymore). */
2356 if (TREE_CODE (newdecl) == PARM_DECL
2357 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2359 struct c_binding *b, **here;
2361 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2362 if ((*here)->decl == olddecl)
2363 goto found;
2364 gcc_unreachable ();
2366 found:
2367 b = *here;
2368 *here = b->prev;
2369 b->prev = current_scope->bindings;
2370 current_scope->bindings = b;
2372 TREE_ASM_WRITTEN (olddecl) = 0;
2375 DECL_ATTRIBUTES (newdecl)
2376 = targetm.merge_decl_attributes (olddecl, newdecl);
2378 /* Merge the data types specified in the two decls. */
2379 TREE_TYPE (newdecl)
2380 = TREE_TYPE (olddecl)
2381 = composite_type (newtype, oldtype);
2383 /* Lay the type out, unless already done. */
2384 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2386 if (TREE_TYPE (newdecl) != error_mark_node)
2387 layout_type (TREE_TYPE (newdecl));
2388 if (TREE_CODE (newdecl) != FUNCTION_DECL
2389 && TREE_CODE (newdecl) != TYPE_DECL
2390 && TREE_CODE (newdecl) != CONST_DECL)
2391 layout_decl (newdecl, 0);
2393 else
2395 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2396 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2397 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2398 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2399 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2401 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2402 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2406 /* Keep the old rtl since we can safely use it. */
2407 if (HAS_RTL_P (olddecl))
2408 COPY_DECL_RTL (olddecl, newdecl);
2410 /* Merge the type qualifiers. */
2411 if (TREE_READONLY (newdecl))
2412 TREE_READONLY (olddecl) = 1;
2414 if (TREE_THIS_VOLATILE (newdecl))
2415 TREE_THIS_VOLATILE (olddecl) = 1;
2417 /* Merge deprecatedness. */
2418 if (TREE_DEPRECATED (newdecl))
2419 TREE_DEPRECATED (olddecl) = 1;
2421 /* If a decl is in a system header and the other isn't, keep the one on the
2422 system header. Otherwise, keep source location of definition rather than
2423 declaration and of prototype rather than non-prototype unless that
2424 prototype is built-in. */
2425 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2426 && DECL_IN_SYSTEM_HEADER (olddecl)
2427 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2428 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2429 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2430 && DECL_IN_SYSTEM_HEADER (newdecl)
2431 && !DECL_IN_SYSTEM_HEADER (olddecl))
2432 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2433 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2434 || (old_is_prototype && !new_is_prototype
2435 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2436 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2438 /* Merge the initialization information. */
2439 if (DECL_INITIAL (newdecl) == 0)
2440 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2442 /* Merge the threadprivate attribute. */
2443 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2444 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2446 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2448 /* Copy the assembler name.
2449 Currently, it can only be defined in the prototype. */
2450 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2452 /* Use visibility of whichever declaration had it specified */
2453 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2455 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2456 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2459 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2461 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2462 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2463 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2464 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2465 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2466 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2467 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2468 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2469 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2470 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2471 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2474 /* Merge the storage class information. */
2475 merge_weak (newdecl, olddecl);
2477 /* For functions, static overrides non-static. */
2478 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2480 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2481 /* This is since we don't automatically
2482 copy the attributes of NEWDECL into OLDDECL. */
2483 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2484 /* If this clears `static', clear it in the identifier too. */
2485 if (!TREE_PUBLIC (olddecl))
2486 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2490 /* In c99, 'extern' declaration before (or after) 'inline' means this
2491 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2492 is present. */
2493 if (TREE_CODE (newdecl) == FUNCTION_DECL
2494 && !flag_gnu89_inline
2495 && (DECL_DECLARED_INLINE_P (newdecl)
2496 || DECL_DECLARED_INLINE_P (olddecl))
2497 && (!DECL_DECLARED_INLINE_P (newdecl)
2498 || !DECL_DECLARED_INLINE_P (olddecl)
2499 || !DECL_EXTERNAL (olddecl))
2500 && DECL_EXTERNAL (newdecl)
2501 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2502 && !current_function_decl)
2503 DECL_EXTERNAL (newdecl) = 0;
2505 /* An inline definition following a static declaration is not
2506 DECL_EXTERNAL. */
2507 if (new_is_definition
2508 && (DECL_DECLARED_INLINE_P (newdecl)
2509 || DECL_DECLARED_INLINE_P (olddecl))
2510 && !TREE_PUBLIC (olddecl))
2511 DECL_EXTERNAL (newdecl) = 0;
2513 if (DECL_EXTERNAL (newdecl))
2515 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2516 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2518 /* An extern decl does not override previous storage class. */
2519 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2520 if (!DECL_EXTERNAL (newdecl))
2522 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2523 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2526 else
2528 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2529 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2532 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2534 /* If we're redefining a function previously defined as extern
2535 inline, make sure we emit debug info for the inline before we
2536 throw it away, in case it was inlined into a function that
2537 hasn't been written out yet. */
2538 if (new_is_definition && DECL_INITIAL (olddecl))
2539 /* The new defn must not be inline. */
2540 DECL_UNINLINABLE (newdecl) = 1;
2541 else
2543 /* If either decl says `inline', this fn is inline, unless
2544 its definition was passed already. */
2545 if (DECL_DECLARED_INLINE_P (newdecl)
2546 || DECL_DECLARED_INLINE_P (olddecl))
2547 DECL_DECLARED_INLINE_P (newdecl) = 1;
2549 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2550 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2552 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2553 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2554 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2555 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2558 if (DECL_BUILT_IN (olddecl))
2560 /* If redeclaring a builtin function, it stays built in.
2561 But it gets tagged as having been declared. */
2562 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2563 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2564 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2565 if (new_is_prototype)
2567 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2568 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2570 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2571 switch (fncode)
2573 /* If a compatible prototype of these builtin functions
2574 is seen, assume the runtime implements it with the
2575 expected semantics. */
2576 case BUILT_IN_STPCPY:
2577 if (builtin_decl_explicit_p (fncode))
2578 set_builtin_decl_implicit_p (fncode, true);
2579 break;
2580 default:
2581 if (builtin_decl_explicit_p (fncode))
2582 set_builtin_decl_declared_p (fncode, true);
2583 break;
2587 else
2588 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2589 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2592 /* Preserve function specific target and optimization options */
2593 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2594 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2595 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2596 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2598 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2599 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2600 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2601 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2603 /* Also preserve various other info from the definition. */
2604 if (!new_is_definition)
2606 tree t;
2607 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2608 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2609 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2610 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2611 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2612 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2613 DECL_CONTEXT (t) = newdecl;
2615 /* See if we've got a function to instantiate from. */
2616 if (DECL_SAVED_TREE (olddecl))
2617 DECL_ABSTRACT_ORIGIN (newdecl)
2618 = DECL_ABSTRACT_ORIGIN (olddecl);
2622 /* Merge the USED information. */
2623 if (TREE_USED (olddecl))
2624 TREE_USED (newdecl) = 1;
2625 else if (TREE_USED (newdecl))
2626 TREE_USED (olddecl) = 1;
2627 if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2628 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2629 if (DECL_PRESERVE_P (olddecl))
2630 DECL_PRESERVE_P (newdecl) = 1;
2631 else if (DECL_PRESERVE_P (newdecl))
2632 DECL_PRESERVE_P (olddecl) = 1;
2634 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2635 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2636 DECL_ARGUMENTS (if appropriate). */
2638 unsigned olddecl_uid = DECL_UID (olddecl);
2639 tree olddecl_context = DECL_CONTEXT (olddecl);
2640 tree olddecl_arguments = NULL;
2641 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2642 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2644 memcpy ((char *) olddecl + sizeof (struct tree_common),
2645 (char *) newdecl + sizeof (struct tree_common),
2646 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2647 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2648 switch (TREE_CODE (olddecl))
2650 case FUNCTION_DECL:
2651 case VAR_DECL:
2653 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2655 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2656 (char *) newdecl + sizeof (struct tree_decl_common),
2657 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2658 olddecl->decl_with_vis.symtab_node = snode;
2660 if ((DECL_EXTERNAL (olddecl)
2661 || TREE_PUBLIC (olddecl)
2662 || TREE_STATIC (olddecl))
2663 && DECL_SECTION_NAME (newdecl) != NULL)
2664 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2666 /* This isn't quite correct for something like
2667 int __thread x attribute ((tls_model ("local-exec")));
2668 extern int __thread x;
2669 as we'll lose the "local-exec" model. */
2670 if (TREE_CODE (olddecl) == VAR_DECL
2671 && DECL_THREAD_LOCAL_P (newdecl))
2672 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2673 break;
2676 case FIELD_DECL:
2677 case PARM_DECL:
2678 case LABEL_DECL:
2679 case RESULT_DECL:
2680 case CONST_DECL:
2681 case TYPE_DECL:
2682 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2683 (char *) newdecl + sizeof (struct tree_decl_common),
2684 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2685 break;
2687 default:
2689 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2690 (char *) newdecl + sizeof (struct tree_decl_common),
2691 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2693 DECL_UID (olddecl) = olddecl_uid;
2694 DECL_CONTEXT (olddecl) = olddecl_context;
2695 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2696 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2699 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2700 so that encode_section_info has a chance to look at the new decl
2701 flags and attributes. */
2702 if (DECL_RTL_SET_P (olddecl)
2703 && (TREE_CODE (olddecl) == FUNCTION_DECL
2704 || (TREE_CODE (olddecl) == VAR_DECL
2705 && TREE_STATIC (olddecl))))
2706 make_decl_rtl (olddecl);
2709 /* Handle when a new declaration NEWDECL has the same name as an old
2710 one OLDDECL in the same binding contour. Prints an error message
2711 if appropriate.
2713 If safely possible, alter OLDDECL to look like NEWDECL, and return
2714 true. Otherwise, return false. */
2716 static bool
2717 duplicate_decls (tree newdecl, tree olddecl)
2719 tree newtype = NULL, oldtype = NULL;
2721 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2723 /* Avoid `unused variable' and other warnings for OLDDECL. */
2724 TREE_NO_WARNING (olddecl) = 1;
2725 return false;
2728 merge_decls (newdecl, olddecl, newtype, oldtype);
2730 /* The NEWDECL will no longer be needed.
2732 Before releasing the node, be sure to remove function from symbol
2733 table that might have been inserted there to record comdat group.
2734 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2735 structure is shared in between NEWDECL and OLDECL. */
2736 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2737 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2738 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2740 struct symtab_node *snode = symtab_node::get (newdecl);
2741 if (snode)
2742 snode->remove ();
2744 ggc_free (newdecl);
2745 return true;
2749 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2750 static void
2751 warn_if_shadowing (tree new_decl)
2753 struct c_binding *b;
2755 /* Shadow warnings wanted? */
2756 if (!warn_shadow
2757 /* No shadow warnings for internally generated vars. */
2758 || DECL_IS_BUILTIN (new_decl)
2759 /* No shadow warnings for vars made for inlining. */
2760 || DECL_FROM_INLINE (new_decl))
2761 return;
2763 /* Is anything being shadowed? Invisible decls do not count. */
2764 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2765 if (b->decl && b->decl != new_decl && !b->invisible
2766 && (b->decl == error_mark_node
2767 || diagnostic_report_warnings_p (global_dc,
2768 DECL_SOURCE_LOCATION (b->decl))))
2770 tree old_decl = b->decl;
2771 bool warned = false;
2773 if (old_decl == error_mark_node)
2775 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2776 "non-variable", new_decl);
2777 break;
2779 else if (TREE_CODE (old_decl) == PARM_DECL)
2780 warned = warning (OPT_Wshadow,
2781 "declaration of %q+D shadows a parameter",
2782 new_decl);
2783 else if (DECL_FILE_SCOPE_P (old_decl))
2785 /* Do not warn if a variable shadows a function, unless
2786 the variable is a function or a pointer-to-function. */
2787 if (TREE_CODE (old_decl) == FUNCTION_DECL
2788 && TREE_CODE (new_decl) != FUNCTION_DECL
2789 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2790 continue;
2792 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2793 "declaration of %qD shadows a global "
2794 "declaration",
2795 new_decl);
2797 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2798 && DECL_BUILT_IN (old_decl))
2800 warning (OPT_Wshadow, "declaration of %q+D shadows "
2801 "a built-in function", new_decl);
2802 break;
2804 else
2805 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2806 "previous local", new_decl);
2808 if (warned)
2809 inform (DECL_SOURCE_LOCATION (old_decl),
2810 "shadowed declaration is here");
2812 break;
2816 /* Record a decl-node X as belonging to the current lexical scope.
2817 Check for errors (such as an incompatible declaration for the same
2818 name already seen in the same scope).
2820 Returns either X or an old decl for the same name.
2821 If an old decl is returned, it may have been smashed
2822 to agree with what X says. */
2824 tree
2825 pushdecl (tree x)
2827 tree name = DECL_NAME (x);
2828 struct c_scope *scope = current_scope;
2829 struct c_binding *b;
2830 bool nested = false;
2831 location_t locus = DECL_SOURCE_LOCATION (x);
2833 /* Must set DECL_CONTEXT for everything not at file scope or
2834 DECL_FILE_SCOPE_P won't work. Local externs don't count
2835 unless they have initializers (which generate code). */
2836 if (current_function_decl
2837 && (!VAR_OR_FUNCTION_DECL_P (x)
2838 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2839 DECL_CONTEXT (x) = current_function_decl;
2841 /* Anonymous decls are just inserted in the scope. */
2842 if (!name)
2844 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2845 locus);
2846 return x;
2849 /* First, see if there is another declaration with the same name in
2850 the current scope. If there is, duplicate_decls may do all the
2851 work for us. If duplicate_decls returns false, that indicates
2852 two incompatible decls in the same scope; we are to silently
2853 replace the old one (duplicate_decls has issued all appropriate
2854 diagnostics). In particular, we should not consider possible
2855 duplicates in the external scope, or shadowing. */
2856 b = I_SYMBOL_BINDING (name);
2857 if (b && B_IN_SCOPE (b, scope))
2859 struct c_binding *b_ext, *b_use;
2860 tree type = TREE_TYPE (x);
2861 tree visdecl = b->decl;
2862 tree vistype = TREE_TYPE (visdecl);
2863 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2864 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2865 b->inner_comp = false;
2866 b_use = b;
2867 b_ext = b;
2868 /* If this is an external linkage declaration, we should check
2869 for compatibility with the type in the external scope before
2870 setting the type at this scope based on the visible
2871 information only. */
2872 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2874 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2875 b_ext = b_ext->shadowed;
2876 if (b_ext)
2878 b_use = b_ext;
2879 if (b_use->u.type)
2880 TREE_TYPE (b_use->decl) = b_use->u.type;
2883 if (duplicate_decls (x, b_use->decl))
2885 if (b_use != b)
2887 /* Save the updated type in the external scope and
2888 restore the proper type for this scope. */
2889 tree thistype;
2890 if (comptypes (vistype, type))
2891 thistype = composite_type (vistype, type);
2892 else
2893 thistype = TREE_TYPE (b_use->decl);
2894 b_use->u.type = TREE_TYPE (b_use->decl);
2895 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2896 && DECL_BUILT_IN (b_use->decl))
2897 thistype
2898 = build_type_attribute_variant (thistype,
2899 TYPE_ATTRIBUTES
2900 (b_use->u.type));
2901 TREE_TYPE (b_use->decl) = thistype;
2903 return b_use->decl;
2905 else
2906 goto skip_external_and_shadow_checks;
2909 /* All declarations with external linkage, and all external
2910 references, go in the external scope, no matter what scope is
2911 current. However, the binding in that scope is ignored for
2912 purposes of normal name lookup. A separate binding structure is
2913 created in the requested scope; this governs the normal
2914 visibility of the symbol.
2916 The binding in the externals scope is used exclusively for
2917 detecting duplicate declarations of the same object, no matter
2918 what scope they are in; this is what we do here. (C99 6.2.7p2:
2919 All declarations that refer to the same object or function shall
2920 have compatible type; otherwise, the behavior is undefined.) */
2921 if (DECL_EXTERNAL (x) || scope == file_scope)
2923 tree type = TREE_TYPE (x);
2924 tree vistype = 0;
2925 tree visdecl = 0;
2926 bool type_saved = false;
2927 if (b && !B_IN_EXTERNAL_SCOPE (b)
2928 && VAR_OR_FUNCTION_DECL_P (b->decl)
2929 && DECL_FILE_SCOPE_P (b->decl))
2931 visdecl = b->decl;
2932 vistype = TREE_TYPE (visdecl);
2934 if (scope != file_scope
2935 && !DECL_IN_SYSTEM_HEADER (x))
2936 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2938 while (b && !B_IN_EXTERNAL_SCOPE (b))
2940 /* If this decl might be modified, save its type. This is
2941 done here rather than when the decl is first bound
2942 because the type may change after first binding, through
2943 being completed or through attributes being added. If we
2944 encounter multiple such decls, only the first should have
2945 its type saved; the others will already have had their
2946 proper types saved and the types will not have changed as
2947 their scopes will not have been re-entered. */
2948 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2950 b->u.type = TREE_TYPE (b->decl);
2951 type_saved = true;
2953 if (B_IN_FILE_SCOPE (b)
2954 && TREE_CODE (b->decl) == VAR_DECL
2955 && TREE_STATIC (b->decl)
2956 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2957 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2958 && TREE_CODE (type) == ARRAY_TYPE
2959 && TYPE_DOMAIN (type)
2960 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2961 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2963 /* Array type completed in inner scope, which should be
2964 diagnosed if the completion does not have size 1 and
2965 it does not get completed in the file scope. */
2966 b->inner_comp = true;
2968 b = b->shadowed;
2971 /* If a matching external declaration has been found, set its
2972 type to the composite of all the types of that declaration.
2973 After the consistency checks, it will be reset to the
2974 composite of the visible types only. */
2975 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2976 && b->u.type)
2977 TREE_TYPE (b->decl) = b->u.type;
2979 /* The point of the same_translation_unit_p check here is,
2980 we want to detect a duplicate decl for a construct like
2981 foo() { extern bar(); } ... static bar(); but not if
2982 they are in different translation units. In any case,
2983 the static does not go in the externals scope. */
2984 if (b
2985 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2986 && duplicate_decls (x, b->decl))
2988 tree thistype;
2989 if (vistype)
2991 if (comptypes (vistype, type))
2992 thistype = composite_type (vistype, type);
2993 else
2994 thistype = TREE_TYPE (b->decl);
2996 else
2997 thistype = type;
2998 b->u.type = TREE_TYPE (b->decl);
2999 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3000 thistype
3001 = build_type_attribute_variant (thistype,
3002 TYPE_ATTRIBUTES (b->u.type));
3003 TREE_TYPE (b->decl) = thistype;
3004 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3005 locus);
3006 return b->decl;
3008 else if (TREE_PUBLIC (x))
3010 if (visdecl && !b && duplicate_decls (x, visdecl))
3012 /* An external declaration at block scope referring to a
3013 visible entity with internal linkage. The composite
3014 type will already be correct for this scope, so we
3015 just need to fall through to make the declaration in
3016 this scope. */
3017 nested = true;
3018 x = visdecl;
3020 else
3022 bind (name, x, external_scope, /*invisible=*/true,
3023 /*nested=*/false, locus);
3024 nested = true;
3029 if (TREE_CODE (x) != PARM_DECL)
3030 warn_if_shadowing (x);
3032 skip_external_and_shadow_checks:
3033 if (TREE_CODE (x) == TYPE_DECL)
3035 /* So this is a typedef, set its underlying type. */
3036 set_underlying_type (x);
3038 /* If X is a typedef defined in the current function, record it
3039 for the purpose of implementing the -Wunused-local-typedefs
3040 warning. */
3041 record_locally_defined_typedef (x);
3044 bind (name, x, scope, /*invisible=*/false, nested, locus);
3046 /* If x's type is incomplete because it's based on a
3047 structure or union which has not yet been fully declared,
3048 attach it to that structure or union type, so we can go
3049 back and complete the variable declaration later, if the
3050 structure or union gets fully declared.
3052 If the input is erroneous, we can have error_mark in the type
3053 slot (e.g. "f(void a, ...)") - that doesn't count as an
3054 incomplete type. */
3055 if (TREE_TYPE (x) != error_mark_node
3056 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3058 tree element = TREE_TYPE (x);
3060 while (TREE_CODE (element) == ARRAY_TYPE)
3061 element = TREE_TYPE (element);
3062 element = TYPE_MAIN_VARIANT (element);
3064 if ((TREE_CODE (element) == RECORD_TYPE
3065 || TREE_CODE (element) == UNION_TYPE)
3066 && (TREE_CODE (x) != TYPE_DECL
3067 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3068 && !COMPLETE_TYPE_P (element))
3069 C_TYPE_INCOMPLETE_VARS (element)
3070 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3072 return x;
3075 /* Record X as belonging to file scope.
3076 This is used only internally by the Objective-C front end,
3077 and is limited to its needs. duplicate_decls is not called;
3078 if there is any preexisting decl for this identifier, it is an ICE. */
3080 tree
3081 pushdecl_top_level (tree x)
3083 tree name;
3084 bool nested = false;
3085 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
3087 name = DECL_NAME (x);
3089 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3091 if (TREE_PUBLIC (x))
3093 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3094 UNKNOWN_LOCATION);
3095 nested = true;
3097 if (file_scope)
3098 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3100 return x;
3103 static void
3104 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3106 if (warn_implicit_function_declaration)
3108 bool warned;
3110 if (flag_isoc99)
3111 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3112 "implicit declaration of function %qE", id);
3113 else
3114 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3115 G_("implicit declaration of function %qE"), id);
3116 if (olddecl && warned)
3117 locate_old_decl (olddecl);
3121 /* This function represents mapping of a function code FCODE
3122 to its respective header. */
3124 static const char *
3125 header_for_builtin_fn (enum built_in_function fcode)
3127 switch (fcode)
3129 CASE_FLT_FN (BUILT_IN_ACOS):
3130 CASE_FLT_FN (BUILT_IN_ACOSH):
3131 CASE_FLT_FN (BUILT_IN_ASIN):
3132 CASE_FLT_FN (BUILT_IN_ASINH):
3133 CASE_FLT_FN (BUILT_IN_ATAN):
3134 CASE_FLT_FN (BUILT_IN_ATANH):
3135 CASE_FLT_FN (BUILT_IN_ATAN2):
3136 CASE_FLT_FN (BUILT_IN_CBRT):
3137 CASE_FLT_FN (BUILT_IN_CEIL):
3138 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3139 CASE_FLT_FN (BUILT_IN_COS):
3140 CASE_FLT_FN (BUILT_IN_COSH):
3141 CASE_FLT_FN (BUILT_IN_ERF):
3142 CASE_FLT_FN (BUILT_IN_ERFC):
3143 CASE_FLT_FN (BUILT_IN_EXP):
3144 CASE_FLT_FN (BUILT_IN_EXP2):
3145 CASE_FLT_FN (BUILT_IN_EXPM1):
3146 CASE_FLT_FN (BUILT_IN_FABS):
3147 CASE_FLT_FN (BUILT_IN_FDIM):
3148 CASE_FLT_FN (BUILT_IN_FLOOR):
3149 CASE_FLT_FN (BUILT_IN_FMA):
3150 CASE_FLT_FN (BUILT_IN_FMAX):
3151 CASE_FLT_FN (BUILT_IN_FMIN):
3152 CASE_FLT_FN (BUILT_IN_FMOD):
3153 CASE_FLT_FN (BUILT_IN_FREXP):
3154 CASE_FLT_FN (BUILT_IN_HYPOT):
3155 CASE_FLT_FN (BUILT_IN_ILOGB):
3156 CASE_FLT_FN (BUILT_IN_LDEXP):
3157 CASE_FLT_FN (BUILT_IN_LGAMMA):
3158 CASE_FLT_FN (BUILT_IN_LLRINT):
3159 CASE_FLT_FN (BUILT_IN_LLROUND):
3160 CASE_FLT_FN (BUILT_IN_LOG):
3161 CASE_FLT_FN (BUILT_IN_LOG10):
3162 CASE_FLT_FN (BUILT_IN_LOG1P):
3163 CASE_FLT_FN (BUILT_IN_LOG2):
3164 CASE_FLT_FN (BUILT_IN_LOGB):
3165 CASE_FLT_FN (BUILT_IN_LRINT):
3166 CASE_FLT_FN (BUILT_IN_LROUND):
3167 CASE_FLT_FN (BUILT_IN_MODF):
3168 CASE_FLT_FN (BUILT_IN_NAN):
3169 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3170 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3171 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3172 CASE_FLT_FN (BUILT_IN_POW):
3173 CASE_FLT_FN (BUILT_IN_REMAINDER):
3174 CASE_FLT_FN (BUILT_IN_REMQUO):
3175 CASE_FLT_FN (BUILT_IN_RINT):
3176 CASE_FLT_FN (BUILT_IN_ROUND):
3177 CASE_FLT_FN (BUILT_IN_SCALBLN):
3178 CASE_FLT_FN (BUILT_IN_SCALBN):
3179 CASE_FLT_FN (BUILT_IN_SIN):
3180 CASE_FLT_FN (BUILT_IN_SINH):
3181 CASE_FLT_FN (BUILT_IN_SINCOS):
3182 CASE_FLT_FN (BUILT_IN_SQRT):
3183 CASE_FLT_FN (BUILT_IN_TAN):
3184 CASE_FLT_FN (BUILT_IN_TANH):
3185 CASE_FLT_FN (BUILT_IN_TGAMMA):
3186 CASE_FLT_FN (BUILT_IN_TRUNC):
3187 case BUILT_IN_ISINF:
3188 case BUILT_IN_ISNAN:
3189 return "<math.h>";
3190 CASE_FLT_FN (BUILT_IN_CABS):
3191 CASE_FLT_FN (BUILT_IN_CACOS):
3192 CASE_FLT_FN (BUILT_IN_CACOSH):
3193 CASE_FLT_FN (BUILT_IN_CARG):
3194 CASE_FLT_FN (BUILT_IN_CASIN):
3195 CASE_FLT_FN (BUILT_IN_CASINH):
3196 CASE_FLT_FN (BUILT_IN_CATAN):
3197 CASE_FLT_FN (BUILT_IN_CATANH):
3198 CASE_FLT_FN (BUILT_IN_CCOS):
3199 CASE_FLT_FN (BUILT_IN_CCOSH):
3200 CASE_FLT_FN (BUILT_IN_CEXP):
3201 CASE_FLT_FN (BUILT_IN_CIMAG):
3202 CASE_FLT_FN (BUILT_IN_CLOG):
3203 CASE_FLT_FN (BUILT_IN_CONJ):
3204 CASE_FLT_FN (BUILT_IN_CPOW):
3205 CASE_FLT_FN (BUILT_IN_CPROJ):
3206 CASE_FLT_FN (BUILT_IN_CREAL):
3207 CASE_FLT_FN (BUILT_IN_CSIN):
3208 CASE_FLT_FN (BUILT_IN_CSINH):
3209 CASE_FLT_FN (BUILT_IN_CSQRT):
3210 CASE_FLT_FN (BUILT_IN_CTAN):
3211 CASE_FLT_FN (BUILT_IN_CTANH):
3212 return "<complex.h>";
3213 case BUILT_IN_MEMCHR:
3214 case BUILT_IN_MEMCMP:
3215 case BUILT_IN_MEMCPY:
3216 case BUILT_IN_MEMMOVE:
3217 case BUILT_IN_MEMSET:
3218 case BUILT_IN_STRCAT:
3219 case BUILT_IN_STRCHR:
3220 case BUILT_IN_STRCMP:
3221 case BUILT_IN_STRCPY:
3222 case BUILT_IN_STRCSPN:
3223 case BUILT_IN_STRLEN:
3224 case BUILT_IN_STRNCAT:
3225 case BUILT_IN_STRNCMP:
3226 case BUILT_IN_STRNCPY:
3227 case BUILT_IN_STRPBRK:
3228 case BUILT_IN_STRRCHR:
3229 case BUILT_IN_STRSPN:
3230 case BUILT_IN_STRSTR:
3231 return "<string.h>";
3232 case BUILT_IN_FPRINTF:
3233 case BUILT_IN_PUTC:
3234 case BUILT_IN_FPUTC:
3235 case BUILT_IN_FPUTS:
3236 case BUILT_IN_FSCANF:
3237 case BUILT_IN_FWRITE:
3238 case BUILT_IN_PRINTF:
3239 case BUILT_IN_PUTCHAR:
3240 case BUILT_IN_PUTS:
3241 case BUILT_IN_SCANF:
3242 case BUILT_IN_SNPRINTF:
3243 case BUILT_IN_SPRINTF:
3244 case BUILT_IN_SSCANF:
3245 case BUILT_IN_VFPRINTF:
3246 case BUILT_IN_VFSCANF:
3247 case BUILT_IN_VPRINTF:
3248 case BUILT_IN_VSCANF:
3249 case BUILT_IN_VSNPRINTF:
3250 case BUILT_IN_VSPRINTF:
3251 case BUILT_IN_VSSCANF:
3252 return "<stdio.h>";
3253 case BUILT_IN_ISALNUM:
3254 case BUILT_IN_ISALPHA:
3255 case BUILT_IN_ISBLANK:
3256 case BUILT_IN_ISCNTRL:
3257 case BUILT_IN_ISDIGIT:
3258 case BUILT_IN_ISGRAPH:
3259 case BUILT_IN_ISLOWER:
3260 case BUILT_IN_ISPRINT:
3261 case BUILT_IN_ISPUNCT:
3262 case BUILT_IN_ISSPACE:
3263 case BUILT_IN_ISUPPER:
3264 case BUILT_IN_ISXDIGIT:
3265 case BUILT_IN_TOLOWER:
3266 case BUILT_IN_TOUPPER:
3267 return "<ctype.h>";
3268 case BUILT_IN_ISWALNUM:
3269 case BUILT_IN_ISWALPHA:
3270 case BUILT_IN_ISWBLANK:
3271 case BUILT_IN_ISWCNTRL:
3272 case BUILT_IN_ISWDIGIT:
3273 case BUILT_IN_ISWGRAPH:
3274 case BUILT_IN_ISWLOWER:
3275 case BUILT_IN_ISWPRINT:
3276 case BUILT_IN_ISWPUNCT:
3277 case BUILT_IN_ISWSPACE:
3278 case BUILT_IN_ISWUPPER:
3279 case BUILT_IN_ISWXDIGIT:
3280 case BUILT_IN_TOWLOWER:
3281 case BUILT_IN_TOWUPPER:
3282 return "<wctype.h>";
3283 case BUILT_IN_ABORT:
3284 case BUILT_IN_ABS:
3285 case BUILT_IN_CALLOC:
3286 case BUILT_IN_EXIT:
3287 case BUILT_IN_FREE:
3288 case BUILT_IN_LABS:
3289 case BUILT_IN_LLABS:
3290 case BUILT_IN_MALLOC:
3291 case BUILT_IN_REALLOC:
3292 case BUILT_IN__EXIT2:
3293 case BUILT_IN_ALIGNED_ALLOC:
3294 return "<stdlib.h>";
3295 case BUILT_IN_IMAXABS:
3296 return "<inttypes.h>";
3297 case BUILT_IN_STRFTIME:
3298 return "<time.h>";
3299 default:
3300 return NULL;
3304 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3305 function of type int (). */
3307 tree
3308 implicitly_declare (location_t loc, tree functionid)
3310 struct c_binding *b;
3311 tree decl = 0;
3312 tree asmspec_tree;
3314 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3316 if (B_IN_SCOPE (b, external_scope))
3318 decl = b->decl;
3319 break;
3323 if (decl)
3325 if (decl == error_mark_node)
3326 return decl;
3328 /* FIXME: Objective-C has weird not-really-builtin functions
3329 which are supposed to be visible automatically. They wind up
3330 in the external scope because they're pushed before the file
3331 scope gets created. Catch this here and rebind them into the
3332 file scope. */
3333 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3335 bind (functionid, decl, file_scope,
3336 /*invisible=*/false, /*nested=*/true,
3337 DECL_SOURCE_LOCATION (decl));
3338 return decl;
3340 else
3342 tree newtype = default_function_type;
3343 if (b->u.type)
3344 TREE_TYPE (decl) = b->u.type;
3345 /* Implicit declaration of a function already declared
3346 (somehow) in a different scope, or as a built-in.
3347 If this is the first time this has happened, warn;
3348 then recycle the old declaration but with the new type. */
3349 if (!C_DECL_IMPLICIT (decl))
3351 implicit_decl_warning (loc, functionid, decl);
3352 C_DECL_IMPLICIT (decl) = 1;
3354 if (DECL_BUILT_IN (decl))
3356 newtype = build_type_attribute_variant (newtype,
3357 TYPE_ATTRIBUTES
3358 (TREE_TYPE (decl)));
3359 if (!comptypes (newtype, TREE_TYPE (decl)))
3361 bool warned = warning_at (loc, 0, "incompatible implicit "
3362 "declaration of built-in "
3363 "function %qD", decl);
3364 /* See if we can hint which header to include. */
3365 const char *header
3366 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3367 if (header != NULL && warned)
3368 inform (loc, "include %qs or provide a declaration of %qD",
3369 header, decl);
3370 newtype = TREE_TYPE (decl);
3373 else
3375 if (!comptypes (newtype, TREE_TYPE (decl)))
3377 error_at (loc, "incompatible implicit declaration of "
3378 "function %qD", decl);
3379 locate_old_decl (decl);
3382 b->u.type = TREE_TYPE (decl);
3383 TREE_TYPE (decl) = newtype;
3384 bind (functionid, decl, current_scope,
3385 /*invisible=*/false, /*nested=*/true,
3386 DECL_SOURCE_LOCATION (decl));
3387 return decl;
3391 /* Not seen before. */
3392 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3393 DECL_EXTERNAL (decl) = 1;
3394 TREE_PUBLIC (decl) = 1;
3395 C_DECL_IMPLICIT (decl) = 1;
3396 implicit_decl_warning (loc, functionid, 0);
3397 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3398 if (asmspec_tree)
3399 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3401 /* C89 says implicit declarations are in the innermost block.
3402 So we record the decl in the standard fashion. */
3403 decl = pushdecl (decl);
3405 /* No need to call objc_check_decl here - it's a function type. */
3406 rest_of_decl_compilation (decl, 0, 0);
3408 /* Write a record describing this implicit function declaration
3409 to the prototypes file (if requested). */
3410 gen_aux_info_record (decl, 0, 1, 0);
3412 /* Possibly apply some default attributes to this implicit declaration. */
3413 decl_attributes (&decl, NULL_TREE, 0);
3415 return decl;
3418 /* Issue an error message for a reference to an undeclared variable
3419 ID, including a reference to a builtin outside of function-call
3420 context. Establish a binding of the identifier to error_mark_node
3421 in an appropriate scope, which will suppress further errors for the
3422 same identifier. The error message should be given location LOC. */
3423 void
3424 undeclared_variable (location_t loc, tree id)
3426 static bool already = false;
3427 struct c_scope *scope;
3429 if (current_function_decl == 0)
3431 error_at (loc, "%qE undeclared here (not in a function)", id);
3432 scope = current_scope;
3434 else
3436 if (!objc_diagnose_private_ivar (id))
3437 error_at (loc, "%qE undeclared (first use in this function)", id);
3438 if (!already)
3440 inform (loc, "each undeclared identifier is reported only"
3441 " once for each function it appears in");
3442 already = true;
3445 /* If we are parsing old-style parameter decls, current_function_decl
3446 will be nonnull but current_function_scope will be null. */
3447 scope = current_function_scope ? current_function_scope : current_scope;
3449 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3450 UNKNOWN_LOCATION);
3453 /* Subroutine of lookup_label, declare_label, define_label: construct a
3454 LABEL_DECL with all the proper frills. Also create a struct
3455 c_label_vars initialized for the current scope. */
3457 static tree
3458 make_label (location_t location, tree name, bool defining,
3459 struct c_label_vars **p_label_vars)
3461 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3462 DECL_CONTEXT (label) = current_function_decl;
3463 DECL_MODE (label) = VOIDmode;
3465 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3466 label_vars->shadowed = NULL;
3467 set_spot_bindings (&label_vars->label_bindings, defining);
3468 label_vars->decls_in_scope = make_tree_vector ();
3469 label_vars->gotos = NULL;
3470 *p_label_vars = label_vars;
3472 return label;
3475 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3476 Create one if none exists so far for the current function.
3477 This is called when a label is used in a goto expression or
3478 has its address taken. */
3480 tree
3481 lookup_label (tree name)
3483 tree label;
3484 struct c_label_vars *label_vars;
3486 if (current_function_scope == 0)
3488 error ("label %qE referenced outside of any function", name);
3489 return 0;
3492 /* Use a label already defined or ref'd with this name, but not if
3493 it is inherited from a containing function and wasn't declared
3494 using __label__. */
3495 label = I_LABEL_DECL (name);
3496 if (label && (DECL_CONTEXT (label) == current_function_decl
3497 || C_DECLARED_LABEL_FLAG (label)))
3499 /* If the label has only been declared, update its apparent
3500 location to point here, for better diagnostics if it
3501 turns out not to have been defined. */
3502 if (DECL_INITIAL (label) == NULL_TREE)
3503 DECL_SOURCE_LOCATION (label) = input_location;
3504 return label;
3507 /* No label binding for that identifier; make one. */
3508 label = make_label (input_location, name, false, &label_vars);
3510 /* Ordinary labels go in the current function scope. */
3511 bind_label (name, label, current_function_scope, label_vars);
3513 return label;
3516 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3517 to LABEL. */
3519 static void
3520 warn_about_goto (location_t goto_loc, tree label, tree decl)
3522 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3523 error_at (goto_loc,
3524 "jump into scope of identifier with variably modified type");
3525 else
3526 warning_at (goto_loc, OPT_Wjump_misses_init,
3527 "jump skips variable initialization");
3528 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3529 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3532 /* Look up a label because of a goto statement. This is like
3533 lookup_label, but also issues any appropriate warnings. */
3535 tree
3536 lookup_label_for_goto (location_t loc, tree name)
3538 tree label;
3539 struct c_label_vars *label_vars;
3540 unsigned int ix;
3541 tree decl;
3543 label = lookup_label (name);
3544 if (label == NULL_TREE)
3545 return NULL_TREE;
3547 /* If we are jumping to a different function, we can't issue any
3548 useful warnings. */
3549 if (DECL_CONTEXT (label) != current_function_decl)
3551 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3552 return label;
3555 label_vars = I_LABEL_BINDING (name)->u.label;
3557 /* If the label has not yet been defined, then push this goto on a
3558 list for possible later warnings. */
3559 if (label_vars->label_bindings.scope == NULL)
3561 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3563 g->loc = loc;
3564 set_spot_bindings (&g->goto_bindings, true);
3565 vec_safe_push (label_vars->gotos, g);
3566 return label;
3569 /* If there are any decls in label_vars->decls_in_scope, then this
3570 goto has missed the declaration of the decl. This happens for a
3571 case like
3572 int i = 1;
3573 lab:
3575 goto lab;
3576 Issue a warning or error. */
3577 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3578 warn_about_goto (loc, label, decl);
3580 if (label_vars->label_bindings.left_stmt_expr)
3582 error_at (loc, "jump into statement expression");
3583 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3586 return label;
3589 /* Make a label named NAME in the current function, shadowing silently
3590 any that may be inherited from containing functions or containing
3591 scopes. This is called for __label__ declarations. */
3593 tree
3594 declare_label (tree name)
3596 struct c_binding *b = I_LABEL_BINDING (name);
3597 tree label;
3598 struct c_label_vars *label_vars;
3600 /* Check to make sure that the label hasn't already been declared
3601 at this scope */
3602 if (b && B_IN_CURRENT_SCOPE (b))
3604 error ("duplicate label declaration %qE", name);
3605 locate_old_decl (b->decl);
3607 /* Just use the previous declaration. */
3608 return b->decl;
3611 label = make_label (input_location, name, false, &label_vars);
3612 C_DECLARED_LABEL_FLAG (label) = 1;
3614 /* Declared labels go in the current scope. */
3615 bind_label (name, label, current_scope, label_vars);
3617 return label;
3620 /* When we define a label, issue any appropriate warnings if there are
3621 any gotos earlier in the function which jump to this label. */
3623 static void
3624 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3626 unsigned int ix;
3627 struct c_goto_bindings *g;
3629 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3631 struct c_binding *b;
3632 struct c_scope *scope;
3634 /* We have a goto to this label. The goto is going forward. In
3635 g->scope, the goto is going to skip any binding which was
3636 defined after g->bindings_in_scope. */
3637 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3639 for (b = g->goto_bindings.scope->bindings;
3640 b != g->goto_bindings.bindings_in_scope;
3641 b = b->prev)
3643 if (decl_jump_unsafe (b->decl))
3644 warn_about_goto (g->loc, label, b->decl);
3648 /* We also need to warn about decls defined in any scopes
3649 between the scope of the label and the scope of the goto. */
3650 for (scope = label_vars->label_bindings.scope;
3651 scope != g->goto_bindings.scope;
3652 scope = scope->outer)
3654 gcc_assert (scope != NULL);
3655 if (scope->has_jump_unsafe_decl)
3657 if (scope == label_vars->label_bindings.scope)
3658 b = label_vars->label_bindings.bindings_in_scope;
3659 else
3660 b = scope->bindings;
3661 for (; b != NULL; b = b->prev)
3663 if (decl_jump_unsafe (b->decl))
3664 warn_about_goto (g->loc, label, b->decl);
3669 if (g->goto_bindings.stmt_exprs > 0)
3671 error_at (g->loc, "jump into statement expression");
3672 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3673 label);
3677 /* Now that the label is defined, we will issue warnings about
3678 subsequent gotos to this label when we see them. */
3679 vec_safe_truncate (label_vars->gotos, 0);
3680 label_vars->gotos = NULL;
3683 /* Define a label, specifying the location in the source file.
3684 Return the LABEL_DECL node for the label, if the definition is valid.
3685 Otherwise return 0. */
3687 tree
3688 define_label (location_t location, tree name)
3690 /* Find any preexisting label with this name. It is an error
3691 if that label has already been defined in this function, or
3692 if there is a containing function with a declared label with
3693 the same name. */
3694 tree label = I_LABEL_DECL (name);
3696 if (label
3697 && ((DECL_CONTEXT (label) == current_function_decl
3698 && DECL_INITIAL (label) != 0)
3699 || (DECL_CONTEXT (label) != current_function_decl
3700 && C_DECLARED_LABEL_FLAG (label))))
3702 error_at (location, "duplicate label %qD", label);
3703 locate_old_decl (label);
3704 return 0;
3706 else if (label && DECL_CONTEXT (label) == current_function_decl)
3708 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3710 /* The label has been used or declared already in this function,
3711 but not defined. Update its location to point to this
3712 definition. */
3713 DECL_SOURCE_LOCATION (label) = location;
3714 set_spot_bindings (&label_vars->label_bindings, true);
3716 /* Issue warnings as required about any goto statements from
3717 earlier in the function. */
3718 check_earlier_gotos (label, label_vars);
3720 else
3722 struct c_label_vars *label_vars;
3724 /* No label binding for that identifier; make one. */
3725 label = make_label (location, name, true, &label_vars);
3727 /* Ordinary labels go in the current function scope. */
3728 bind_label (name, label, current_function_scope, label_vars);
3731 if (!in_system_header_at (input_location) && lookup_name (name))
3732 warning_at (location, OPT_Wtraditional,
3733 "traditional C lacks a separate namespace "
3734 "for labels, identifier %qE conflicts", name);
3736 /* Mark label as having been defined. */
3737 DECL_INITIAL (label) = error_mark_node;
3738 return label;
3741 /* Get the bindings for a new switch statement. This is used to issue
3742 warnings as appropriate for jumps from the switch to case or
3743 default labels. */
3745 struct c_spot_bindings *
3746 c_get_switch_bindings (void)
3748 struct c_spot_bindings *switch_bindings;
3750 switch_bindings = XNEW (struct c_spot_bindings);
3751 set_spot_bindings (switch_bindings, true);
3752 return switch_bindings;
3755 void
3756 c_release_switch_bindings (struct c_spot_bindings *bindings)
3758 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3759 XDELETE (bindings);
3762 /* This is called at the point of a case or default label to issue
3763 warnings about decls as needed. It returns true if it found an
3764 error, not just a warning. */
3766 bool
3767 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3768 location_t switch_loc, location_t case_loc)
3770 bool saw_error;
3771 struct c_scope *scope;
3773 saw_error = false;
3774 for (scope = current_scope;
3775 scope != switch_bindings->scope;
3776 scope = scope->outer)
3778 struct c_binding *b;
3780 gcc_assert (scope != NULL);
3782 if (!scope->has_jump_unsafe_decl)
3783 continue;
3785 for (b = scope->bindings; b != NULL; b = b->prev)
3787 if (decl_jump_unsafe (b->decl))
3789 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3791 saw_error = true;
3792 error_at (case_loc,
3793 ("switch jumps into scope of identifier with "
3794 "variably modified type"));
3796 else
3797 warning_at (case_loc, OPT_Wjump_misses_init,
3798 "switch jumps over variable initialization");
3799 inform (switch_loc, "switch starts here");
3800 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3801 b->decl);
3806 if (switch_bindings->stmt_exprs > 0)
3808 saw_error = true;
3809 error_at (case_loc, "switch jumps into statement expression");
3810 inform (switch_loc, "switch starts here");
3813 return saw_error;
3816 /* Given NAME, an IDENTIFIER_NODE,
3817 return the structure (or union or enum) definition for that name.
3818 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3819 CODE says which kind of type the caller wants;
3820 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3821 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3822 location where the tag was defined.
3823 If the wrong kind of type is found, an error is reported. */
3825 static tree
3826 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3827 location_t *ploc)
3829 struct c_binding *b = I_TAG_BINDING (name);
3830 int thislevel = 0;
3832 if (!b || !b->decl)
3833 return 0;
3835 /* We only care about whether it's in this level if
3836 thislevel_only was set or it might be a type clash. */
3837 if (thislevel_only || TREE_CODE (b->decl) != code)
3839 /* For our purposes, a tag in the external scope is the same as
3840 a tag in the file scope. (Primarily relevant to Objective-C
3841 and its builtin structure tags, which get pushed before the
3842 file scope is created.) */
3843 if (B_IN_CURRENT_SCOPE (b)
3844 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3845 thislevel = 1;
3848 if (thislevel_only && !thislevel)
3849 return 0;
3851 if (TREE_CODE (b->decl) != code)
3853 /* Definition isn't the kind we were looking for. */
3854 pending_invalid_xref = name;
3855 pending_invalid_xref_location = input_location;
3857 /* If in the same binding level as a declaration as a tag
3858 of a different type, this must not be allowed to
3859 shadow that tag, so give the error immediately.
3860 (For example, "struct foo; union foo;" is invalid.) */
3861 if (thislevel)
3862 pending_xref_error ();
3865 if (ploc != NULL)
3866 *ploc = b->locus;
3868 return b->decl;
3871 /* Print an error message now
3872 for a recent invalid struct, union or enum cross reference.
3873 We don't print them immediately because they are not invalid
3874 when used in the `struct foo;' construct for shadowing. */
3876 void
3877 pending_xref_error (void)
3879 if (pending_invalid_xref != 0)
3880 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3881 pending_invalid_xref);
3882 pending_invalid_xref = 0;
3886 /* Look up NAME in the current scope and its superiors
3887 in the namespace of variables, functions and typedefs.
3888 Return a ..._DECL node of some kind representing its definition,
3889 or return 0 if it is undefined. */
3891 tree
3892 lookup_name (tree name)
3894 struct c_binding *b = I_SYMBOL_BINDING (name);
3895 if (b && !b->invisible)
3897 maybe_record_typedef_use (b->decl);
3898 return b->decl;
3900 return 0;
3903 /* Similar to `lookup_name' but look only at the indicated scope. */
3905 static tree
3906 lookup_name_in_scope (tree name, struct c_scope *scope)
3908 struct c_binding *b;
3910 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3911 if (B_IN_SCOPE (b, scope))
3912 return b->decl;
3913 return 0;
3916 /* Create the predefined scalar types of C,
3917 and some nodes representing standard constants (0, 1, (void *) 0).
3918 Initialize the global scope.
3919 Make definitions for built-in primitive functions. */
3921 void
3922 c_init_decl_processing (void)
3924 location_t save_loc = input_location;
3926 /* Initialize reserved words for parser. */
3927 c_parse_init ();
3929 current_function_decl = 0;
3931 gcc_obstack_init (&parser_obstack);
3933 /* Make the externals scope. */
3934 push_scope ();
3935 external_scope = current_scope;
3937 /* Declarations from c_common_nodes_and_builtins must not be associated
3938 with this input file, lest we get differences between using and not
3939 using preprocessed headers. */
3940 input_location = BUILTINS_LOCATION;
3942 c_common_nodes_and_builtins ();
3944 /* In C, comparisons and TRUTH_* expressions have type int. */
3945 truthvalue_type_node = integer_type_node;
3946 truthvalue_true_node = integer_one_node;
3947 truthvalue_false_node = integer_zero_node;
3949 /* Even in C99, which has a real boolean type. */
3950 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3951 boolean_type_node));
3953 input_location = save_loc;
3955 make_fname_decl = c_make_fname_decl;
3956 start_fname_decls ();
3959 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3960 give the decl, NAME is the initialization string and TYPE_DEP
3961 indicates whether NAME depended on the type of the function. As we
3962 don't yet implement delayed emission of static data, we mark the
3963 decl as emitted so it is not placed in the output. Anything using
3964 it must therefore pull out the STRING_CST initializer directly.
3965 FIXME. */
3967 static tree
3968 c_make_fname_decl (location_t loc, tree id, int type_dep)
3970 const char *name = fname_as_string (type_dep);
3971 tree decl, type, init;
3972 size_t length = strlen (name);
3974 type = build_array_type (char_type_node,
3975 build_index_type (size_int (length)));
3976 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3978 decl = build_decl (loc, VAR_DECL, id, type);
3980 TREE_STATIC (decl) = 1;
3981 TREE_READONLY (decl) = 1;
3982 DECL_ARTIFICIAL (decl) = 1;
3984 init = build_string (length + 1, name);
3985 free (CONST_CAST (char *, name));
3986 TREE_TYPE (init) = type;
3987 DECL_INITIAL (decl) = init;
3989 TREE_USED (decl) = 1;
3991 if (current_function_decl
3992 /* For invalid programs like this:
3994 void foo()
3995 const char* p = __FUNCTION__;
3997 the __FUNCTION__ is believed to appear in K&R style function
3998 parameter declarator. In that case we still don't have
3999 function_scope. */
4000 && (!seen_error () || current_function_scope))
4002 DECL_CONTEXT (decl) = current_function_decl;
4003 bind (id, decl, current_function_scope,
4004 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4007 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4009 return decl;
4012 tree
4013 c_builtin_function (tree decl)
4015 tree type = TREE_TYPE (decl);
4016 tree id = DECL_NAME (decl);
4018 const char *name = IDENTIFIER_POINTER (id);
4019 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4021 /* Should never be called on a symbol with a preexisting meaning. */
4022 gcc_assert (!I_SYMBOL_BINDING (id));
4024 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4025 UNKNOWN_LOCATION);
4027 /* Builtins in the implementation namespace are made visible without
4028 needing to be explicitly declared. See push_file_scope. */
4029 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4031 DECL_CHAIN (decl) = visible_builtins;
4032 visible_builtins = decl;
4035 return decl;
4038 tree
4039 c_builtin_function_ext_scope (tree decl)
4041 tree type = TREE_TYPE (decl);
4042 tree id = DECL_NAME (decl);
4044 const char *name = IDENTIFIER_POINTER (id);
4045 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4047 if (external_scope)
4048 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4049 UNKNOWN_LOCATION);
4051 /* Builtins in the implementation namespace are made visible without
4052 needing to be explicitly declared. See push_file_scope. */
4053 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4055 DECL_CHAIN (decl) = visible_builtins;
4056 visible_builtins = decl;
4059 return decl;
4062 /* Called when a declaration is seen that contains no names to declare.
4063 If its type is a reference to a structure, union or enum inherited
4064 from a containing scope, shadow that tag name for the current scope
4065 with a forward reference.
4066 If its type defines a new named structure or union
4067 or defines an enum, it is valid but we need not do anything here.
4068 Otherwise, it is an error. */
4070 void
4071 shadow_tag (const struct c_declspecs *declspecs)
4073 shadow_tag_warned (declspecs, 0);
4076 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4077 but no pedwarn. */
4078 void
4079 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4081 bool found_tag = false;
4083 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4085 tree value = declspecs->type;
4086 enum tree_code code = TREE_CODE (value);
4088 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4089 /* Used to test also that TYPE_SIZE (value) != 0.
4090 That caused warning for `struct foo;' at top level in the file. */
4092 tree name = TYPE_NAME (value);
4093 tree t;
4095 found_tag = true;
4097 if (declspecs->restrict_p)
4099 error ("invalid use of %<restrict%>");
4100 warned = 1;
4103 if (name == 0)
4105 if (warned != 1 && code != ENUMERAL_TYPE)
4106 /* Empty unnamed enum OK */
4108 pedwarn (input_location, 0,
4109 "unnamed struct/union that defines no instances");
4110 warned = 1;
4113 else if (declspecs->typespec_kind != ctsk_tagdef
4114 && declspecs->typespec_kind != ctsk_tagfirstref
4115 && declspecs->storage_class != csc_none)
4117 if (warned != 1)
4118 pedwarn (input_location, 0,
4119 "empty declaration with storage class specifier "
4120 "does not redeclare tag");
4121 warned = 1;
4122 pending_xref_error ();
4124 else if (declspecs->typespec_kind != ctsk_tagdef
4125 && declspecs->typespec_kind != ctsk_tagfirstref
4126 && (declspecs->const_p
4127 || declspecs->volatile_p
4128 || declspecs->atomic_p
4129 || declspecs->restrict_p
4130 || declspecs->address_space))
4132 if (warned != 1)
4133 pedwarn (input_location, 0,
4134 "empty declaration with type qualifier "
4135 "does not redeclare tag");
4136 warned = 1;
4137 pending_xref_error ();
4139 else if (declspecs->typespec_kind != ctsk_tagdef
4140 && declspecs->typespec_kind != ctsk_tagfirstref
4141 && declspecs->alignas_p)
4143 if (warned != 1)
4144 pedwarn (input_location, 0,
4145 "empty declaration with %<_Alignas%> "
4146 "does not redeclare tag");
4147 warned = 1;
4148 pending_xref_error ();
4150 else
4152 pending_invalid_xref = 0;
4153 t = lookup_tag (code, name, 1, NULL);
4155 if (t == 0)
4157 t = make_node (code);
4158 pushtag (input_location, name, t);
4162 else
4164 if (warned != 1 && !in_system_header_at (input_location))
4166 pedwarn (input_location, 0,
4167 "useless type name in empty declaration");
4168 warned = 1;
4172 else if (warned != 1 && !in_system_header_at (input_location)
4173 && declspecs->typedef_p)
4175 pedwarn (input_location, 0, "useless type name in empty declaration");
4176 warned = 1;
4179 pending_invalid_xref = 0;
4181 if (declspecs->inline_p)
4183 error ("%<inline%> in empty declaration");
4184 warned = 1;
4187 if (declspecs->noreturn_p)
4189 error ("%<_Noreturn%> in empty declaration");
4190 warned = 1;
4193 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4195 error ("%<auto%> in file-scope empty declaration");
4196 warned = 1;
4199 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4201 error ("%<register%> in file-scope empty declaration");
4202 warned = 1;
4205 if (!warned && !in_system_header_at (input_location)
4206 && declspecs->storage_class != csc_none)
4208 warning (0, "useless storage class specifier in empty declaration");
4209 warned = 2;
4212 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4214 warning (0, "useless %qs in empty declaration",
4215 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4216 warned = 2;
4219 if (!warned
4220 && !in_system_header_at (input_location)
4221 && (declspecs->const_p
4222 || declspecs->volatile_p
4223 || declspecs->atomic_p
4224 || declspecs->restrict_p
4225 || declspecs->address_space))
4227 warning (0, "useless type qualifier in empty declaration");
4228 warned = 2;
4231 if (!warned && !in_system_header_at (input_location)
4232 && declspecs->alignas_p)
4234 warning (0, "useless %<_Alignas%> in empty declaration");
4235 warned = 2;
4238 if (warned != 1)
4240 if (!found_tag)
4241 pedwarn (input_location, 0, "empty declaration");
4246 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4247 bits. SPECS represents declaration specifiers that the grammar
4248 only permits to contain type qualifiers and attributes. */
4251 quals_from_declspecs (const struct c_declspecs *specs)
4253 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4254 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4255 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4256 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4257 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4258 gcc_assert (!specs->type
4259 && !specs->decl_attr
4260 && specs->typespec_word == cts_none
4261 && specs->storage_class == csc_none
4262 && !specs->typedef_p
4263 && !specs->explicit_signed_p
4264 && !specs->deprecated_p
4265 && !specs->long_p
4266 && !specs->long_long_p
4267 && !specs->short_p
4268 && !specs->signed_p
4269 && !specs->unsigned_p
4270 && !specs->complex_p
4271 && !specs->inline_p
4272 && !specs->noreturn_p
4273 && !specs->thread_p);
4274 return quals;
4277 /* Construct an array declarator. LOC is the location of the
4278 beginning of the array (usually the opening brace). EXPR is the
4279 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4280 inside the [] (to be applied to the pointer to which a parameter
4281 array is converted). STATIC_P is true if "static" is inside the
4282 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4283 VLA of unspecified length which is nevertheless a complete type,
4284 false otherwise. The field for the contained declarator is left to
4285 be filled in by set_array_declarator_inner. */
4287 struct c_declarator *
4288 build_array_declarator (location_t loc,
4289 tree expr, struct c_declspecs *quals, bool static_p,
4290 bool vla_unspec_p)
4292 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4293 struct c_declarator);
4294 declarator->id_loc = loc;
4295 declarator->kind = cdk_array;
4296 declarator->declarator = 0;
4297 declarator->u.array.dimen = expr;
4298 if (quals)
4300 declarator->u.array.attrs = quals->attrs;
4301 declarator->u.array.quals = quals_from_declspecs (quals);
4303 else
4305 declarator->u.array.attrs = NULL_TREE;
4306 declarator->u.array.quals = 0;
4308 declarator->u.array.static_p = static_p;
4309 declarator->u.array.vla_unspec_p = vla_unspec_p;
4310 if (static_p || quals != NULL)
4311 pedwarn_c90 (loc, OPT_Wpedantic,
4312 "ISO C90 does not support %<static%> or type "
4313 "qualifiers in parameter array declarators");
4314 if (vla_unspec_p)
4315 pedwarn_c90 (loc, OPT_Wpedantic,
4316 "ISO C90 does not support %<[*]%> array declarators");
4317 if (vla_unspec_p)
4319 if (!current_scope->parm_flag)
4321 /* C99 6.7.5.2p4 */
4322 error_at (loc, "%<[*]%> not allowed in other than "
4323 "function prototype scope");
4324 declarator->u.array.vla_unspec_p = false;
4325 return NULL;
4327 current_scope->had_vla_unspec = true;
4329 return declarator;
4332 /* Set the contained declarator of an array declarator. DECL is the
4333 declarator, as constructed by build_array_declarator; INNER is what
4334 appears on the left of the []. */
4336 struct c_declarator *
4337 set_array_declarator_inner (struct c_declarator *decl,
4338 struct c_declarator *inner)
4340 decl->declarator = inner;
4341 return decl;
4344 /* INIT is a constructor that forms DECL's initializer. If the final
4345 element initializes a flexible array field, add the size of that
4346 initializer to DECL's size. */
4348 static void
4349 add_flexible_array_elts_to_size (tree decl, tree init)
4351 tree elt, type;
4353 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4354 return;
4356 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4357 type = TREE_TYPE (elt);
4358 if (TREE_CODE (type) == ARRAY_TYPE
4359 && TYPE_SIZE (type) == NULL_TREE
4360 && TYPE_DOMAIN (type) != NULL_TREE
4361 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4363 complete_array_type (&type, elt, false);
4364 DECL_SIZE (decl)
4365 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4366 DECL_SIZE_UNIT (decl)
4367 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4371 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4372 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4373 before the type name, and set *EXPR_CONST_OPERANDS, if
4374 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4375 appear in a constant expression. */
4377 tree
4378 groktypename (struct c_type_name *type_name, tree *expr,
4379 bool *expr_const_operands)
4381 tree type;
4382 tree attrs = type_name->specs->attrs;
4384 type_name->specs->attrs = NULL_TREE;
4386 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4387 false, NULL, &attrs, expr, expr_const_operands,
4388 DEPRECATED_NORMAL);
4390 /* Apply attributes. */
4391 decl_attributes (&type, attrs, 0);
4393 return type;
4396 /* Wrapper for decl_attributes that adds some implicit attributes
4397 to VAR_DECLs or FUNCTION_DECLs. */
4399 static tree
4400 c_decl_attributes (tree *node, tree attributes, int flags)
4402 /* Add implicit "omp declare target" attribute if requested. */
4403 if (current_omp_declare_target_attribute
4404 && ((TREE_CODE (*node) == VAR_DECL
4405 && (TREE_STATIC (*node) || DECL_EXTERNAL (*node)))
4406 || TREE_CODE (*node) == FUNCTION_DECL))
4408 if (TREE_CODE (*node) == VAR_DECL
4409 && ((DECL_CONTEXT (*node)
4410 && TREE_CODE (DECL_CONTEXT (*node)) == FUNCTION_DECL)
4411 || (current_function_decl && !DECL_EXTERNAL (*node))))
4412 error ("%q+D in block scope inside of declare target directive",
4413 *node);
4414 else if (TREE_CODE (*node) == VAR_DECL
4415 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4416 error ("%q+D in declare target directive does not have mappable type",
4417 *node);
4418 else
4419 attributes = tree_cons (get_identifier ("omp declare target"),
4420 NULL_TREE, attributes);
4422 return decl_attributes (node, attributes, flags);
4426 /* Decode a declarator in an ordinary declaration or data definition.
4427 This is called as soon as the type information and variable name
4428 have been parsed, before parsing the initializer if any.
4429 Here we create the ..._DECL node, fill in its type,
4430 and put it on the list of decls for the current context.
4431 The ..._DECL node is returned as the value.
4433 Exception: for arrays where the length is not specified,
4434 the type is left null, to be filled in by `finish_decl'.
4436 Function definitions do not come here; they go to start_function
4437 instead. However, external and forward declarations of functions
4438 do go through here. Structure field declarations are done by
4439 grokfield and not through here. */
4441 tree
4442 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4443 bool initialized, tree attributes)
4445 tree decl;
4446 tree tem;
4447 tree expr = NULL_TREE;
4448 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4450 /* An object declared as __attribute__((deprecated)) suppresses
4451 warnings of uses of other deprecated items. */
4452 if (lookup_attribute ("deprecated", attributes))
4453 deprecated_state = DEPRECATED_SUPPRESS;
4455 decl = grokdeclarator (declarator, declspecs,
4456 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4457 deprecated_state);
4458 if (!decl || decl == error_mark_node)
4459 return NULL_TREE;
4461 if (expr)
4462 add_stmt (fold_convert (void_type_node, expr));
4464 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4465 warning (OPT_Wmain, "%q+D is usually a function", decl);
4467 if (initialized)
4468 /* Is it valid for this decl to have an initializer at all?
4469 If not, set INITIALIZED to zero, which will indirectly
4470 tell 'finish_decl' to ignore the initializer once it is parsed. */
4471 switch (TREE_CODE (decl))
4473 case TYPE_DECL:
4474 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4475 initialized = 0;
4476 break;
4478 case FUNCTION_DECL:
4479 error ("function %qD is initialized like a variable", decl);
4480 initialized = 0;
4481 break;
4483 case PARM_DECL:
4484 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4485 error ("parameter %qD is initialized", decl);
4486 initialized = 0;
4487 break;
4489 default:
4490 /* Don't allow initializations for incomplete types except for
4491 arrays which might be completed by the initialization. */
4493 /* This can happen if the array size is an undefined macro.
4494 We already gave a warning, so we don't need another one. */
4495 if (TREE_TYPE (decl) == error_mark_node)
4496 initialized = 0;
4497 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4499 /* A complete type is ok if size is fixed. */
4501 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4502 || C_DECL_VARIABLE_SIZE (decl))
4504 error ("variable-sized object may not be initialized");
4505 initialized = 0;
4508 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4510 error ("variable %qD has initializer but incomplete type", decl);
4511 initialized = 0;
4513 else if (C_DECL_VARIABLE_SIZE (decl))
4515 /* Although C99 is unclear about whether incomplete arrays
4516 of VLAs themselves count as VLAs, it does not make
4517 sense to permit them to be initialized given that
4518 ordinary VLAs may not be initialized. */
4519 error ("variable-sized object may not be initialized");
4520 initialized = 0;
4524 if (initialized)
4526 if (current_scope == file_scope)
4527 TREE_STATIC (decl) = 1;
4529 /* Tell 'pushdecl' this is an initialized decl
4530 even though we don't yet have the initializer expression.
4531 Also tell 'finish_decl' it may store the real initializer. */
4532 DECL_INITIAL (decl) = error_mark_node;
4535 /* If this is a function declaration, write a record describing it to the
4536 prototypes file (if requested). */
4538 if (TREE_CODE (decl) == FUNCTION_DECL)
4539 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4541 /* ANSI specifies that a tentative definition which is not merged with
4542 a non-tentative definition behaves exactly like a definition with an
4543 initializer equal to zero. (Section 3.7.2)
4545 -fno-common gives strict ANSI behavior, though this tends to break
4546 a large body of code that grew up without this rule.
4548 Thread-local variables are never common, since there's no entrenched
4549 body of code to break, and it allows more efficient variable references
4550 in the presence of dynamic linking. */
4552 if (TREE_CODE (decl) == VAR_DECL
4553 && !initialized
4554 && TREE_PUBLIC (decl)
4555 && !DECL_THREAD_LOCAL_P (decl)
4556 && !flag_no_common)
4557 DECL_COMMON (decl) = 1;
4559 /* Set attributes here so if duplicate decl, will have proper attributes. */
4560 c_decl_attributes (&decl, attributes, 0);
4562 /* Handle gnu_inline attribute. */
4563 if (declspecs->inline_p
4564 && !flag_gnu89_inline
4565 && TREE_CODE (decl) == FUNCTION_DECL
4566 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4567 || current_function_decl))
4569 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4571 else if (declspecs->storage_class != csc_static)
4572 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4575 if (TREE_CODE (decl) == FUNCTION_DECL
4576 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4578 struct c_declarator *ce = declarator;
4580 if (ce->kind == cdk_pointer)
4581 ce = declarator->declarator;
4582 if (ce->kind == cdk_function)
4584 tree args = ce->u.arg_info->parms;
4585 for (; args; args = DECL_CHAIN (args))
4587 tree type = TREE_TYPE (args);
4588 if (type && INTEGRAL_TYPE_P (type)
4589 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4590 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4595 if (TREE_CODE (decl) == FUNCTION_DECL
4596 && DECL_DECLARED_INLINE_P (decl)
4597 && DECL_UNINLINABLE (decl)
4598 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4599 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4600 decl);
4602 /* C99 6.7.4p3: An inline definition of a function with external
4603 linkage shall not contain a definition of a modifiable object
4604 with static storage duration... */
4605 if (TREE_CODE (decl) == VAR_DECL
4606 && current_scope != file_scope
4607 && TREE_STATIC (decl)
4608 && !TREE_READONLY (decl)
4609 && DECL_DECLARED_INLINE_P (current_function_decl)
4610 && DECL_EXTERNAL (current_function_decl))
4611 record_inline_static (input_location, current_function_decl,
4612 decl, csi_modifiable);
4614 if (c_dialect_objc ()
4615 && VAR_OR_FUNCTION_DECL_P (decl))
4616 objc_check_global_decl (decl);
4618 /* Add this decl to the current scope.
4619 TEM may equal DECL or it may be a previous decl of the same name. */
4620 tem = pushdecl (decl);
4622 if (initialized && DECL_EXTERNAL (tem))
4624 DECL_EXTERNAL (tem) = 0;
4625 TREE_STATIC (tem) = 1;
4628 return tem;
4631 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4632 DECL or the non-array element type if DECL is an uninitialized array.
4633 If that type has a const member, diagnose this. */
4635 static void
4636 diagnose_uninitialized_cst_member (tree decl, tree type)
4638 tree field;
4639 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4641 tree field_type;
4642 if (TREE_CODE (field) != FIELD_DECL)
4643 continue;
4644 field_type = strip_array_types (TREE_TYPE (field));
4646 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4648 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4649 "uninitialized const member in %qT is invalid in C++",
4650 strip_array_types (TREE_TYPE (decl)));
4651 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4654 if (TREE_CODE (field_type) == RECORD_TYPE
4655 || TREE_CODE (field_type) == UNION_TYPE)
4656 diagnose_uninitialized_cst_member (decl, field_type);
4660 /* Finish processing of a declaration;
4661 install its initial value.
4662 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4663 If the length of an array type is not known before,
4664 it must be determined now, from the initial value, or it is an error.
4666 INIT_LOC is the location of the initial value. */
4668 void
4669 finish_decl (tree decl, location_t init_loc, tree init,
4670 tree origtype, tree asmspec_tree)
4672 tree type;
4673 bool was_incomplete = (DECL_SIZE (decl) == 0);
4674 const char *asmspec = 0;
4676 /* If a name was specified, get the string. */
4677 if (VAR_OR_FUNCTION_DECL_P (decl)
4678 && DECL_FILE_SCOPE_P (decl))
4679 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4680 if (asmspec_tree)
4681 asmspec = TREE_STRING_POINTER (asmspec_tree);
4683 if (TREE_CODE (decl) == VAR_DECL
4684 && TREE_STATIC (decl)
4685 && global_bindings_p ())
4686 /* So decl is a global variable. Record the types it uses
4687 so that we can decide later to emit debug info for them. */
4688 record_types_used_by_current_var_decl (decl);
4690 /* If `start_decl' didn't like having an initialization, ignore it now. */
4691 if (init != 0 && DECL_INITIAL (decl) == 0)
4692 init = 0;
4694 /* Don't crash if parm is initialized. */
4695 if (TREE_CODE (decl) == PARM_DECL)
4696 init = 0;
4698 if (init)
4699 store_init_value (init_loc, decl, init, origtype);
4701 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4702 || TREE_CODE (decl) == FIELD_DECL))
4703 objc_check_decl (decl);
4705 type = TREE_TYPE (decl);
4707 /* Deduce size of array from initialization, if not already known. */
4708 if (TREE_CODE (type) == ARRAY_TYPE
4709 && TYPE_DOMAIN (type) == 0
4710 && TREE_CODE (decl) != TYPE_DECL)
4712 bool do_default
4713 = (TREE_STATIC (decl)
4714 /* Even if pedantic, an external linkage array
4715 may have incomplete type at first. */
4716 ? pedantic && !TREE_PUBLIC (decl)
4717 : !DECL_EXTERNAL (decl));
4718 int failure
4719 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4720 do_default);
4722 /* Get the completed type made by complete_array_type. */
4723 type = TREE_TYPE (decl);
4725 switch (failure)
4727 case 1:
4728 error ("initializer fails to determine size of %q+D", decl);
4729 break;
4731 case 2:
4732 if (do_default)
4733 error ("array size missing in %q+D", decl);
4734 /* If a `static' var's size isn't known,
4735 make it extern as well as static, so it does not get
4736 allocated.
4737 If it is not `static', then do not mark extern;
4738 finish_incomplete_decl will give it a default size
4739 and it will get allocated. */
4740 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4741 DECL_EXTERNAL (decl) = 1;
4742 break;
4744 case 3:
4745 error ("zero or negative size array %q+D", decl);
4746 break;
4748 case 0:
4749 /* For global variables, update the copy of the type that
4750 exists in the binding. */
4751 if (TREE_PUBLIC (decl))
4753 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4754 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4755 b_ext = b_ext->shadowed;
4756 if (b_ext)
4758 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4759 b_ext->u.type = composite_type (b_ext->u.type, type);
4760 else
4761 b_ext->u.type = type;
4764 break;
4766 default:
4767 gcc_unreachable ();
4770 if (DECL_INITIAL (decl))
4771 TREE_TYPE (DECL_INITIAL (decl)) = type;
4773 relayout_decl (decl);
4776 if (TREE_CODE (decl) == VAR_DECL)
4778 if (init && TREE_CODE (init) == CONSTRUCTOR)
4779 add_flexible_array_elts_to_size (decl, init);
4781 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4782 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4783 layout_decl (decl, 0);
4785 if (DECL_SIZE (decl) == 0
4786 /* Don't give an error if we already gave one earlier. */
4787 && TREE_TYPE (decl) != error_mark_node
4788 && (TREE_STATIC (decl)
4789 /* A static variable with an incomplete type
4790 is an error if it is initialized.
4791 Also if it is not file scope.
4792 Otherwise, let it through, but if it is not `extern'
4793 then it may cause an error message later. */
4794 ? (DECL_INITIAL (decl) != 0
4795 || !DECL_FILE_SCOPE_P (decl))
4796 /* An automatic variable with an incomplete type
4797 is an error. */
4798 : !DECL_EXTERNAL (decl)))
4800 error ("storage size of %q+D isn%'t known", decl);
4801 TREE_TYPE (decl) = error_mark_node;
4804 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4805 && DECL_SIZE (decl) != 0)
4807 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4808 constant_expression_warning (DECL_SIZE (decl));
4809 else
4811 error ("storage size of %q+D isn%'t constant", decl);
4812 TREE_TYPE (decl) = error_mark_node;
4816 if (TREE_USED (type))
4818 TREE_USED (decl) = 1;
4819 DECL_READ_P (decl) = 1;
4823 /* If this is a function and an assembler name is specified, reset DECL_RTL
4824 so we can give it its new name. Also, update builtin_decl if it
4825 was a normal built-in. */
4826 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4828 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4829 set_builtin_user_assembler_name (decl, asmspec);
4830 set_user_assembler_name (decl, asmspec);
4833 /* If #pragma weak was used, mark the decl weak now. */
4834 maybe_apply_pragma_weak (decl);
4836 /* Output the assembler code and/or RTL code for variables and functions,
4837 unless the type is an undefined structure or union.
4838 If not, it will get done when the type is completed. */
4840 if (VAR_OR_FUNCTION_DECL_P (decl))
4842 /* Determine the ELF visibility. */
4843 if (TREE_PUBLIC (decl))
4844 c_determine_visibility (decl);
4846 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4847 if (c_dialect_objc ())
4848 objc_check_decl (decl);
4850 if (asmspec)
4852 /* If this is not a static variable, issue a warning.
4853 It doesn't make any sense to give an ASMSPEC for an
4854 ordinary, non-register local variable. Historically,
4855 GCC has accepted -- but ignored -- the ASMSPEC in
4856 this case. */
4857 if (!DECL_FILE_SCOPE_P (decl)
4858 && TREE_CODE (decl) == VAR_DECL
4859 && !C_DECL_REGISTER (decl)
4860 && !TREE_STATIC (decl))
4861 warning (0, "ignoring asm-specifier for non-static local "
4862 "variable %q+D", decl);
4863 else
4864 set_user_assembler_name (decl, asmspec);
4867 if (DECL_FILE_SCOPE_P (decl))
4869 if (DECL_INITIAL (decl) == NULL_TREE
4870 || DECL_INITIAL (decl) == error_mark_node)
4871 /* Don't output anything
4872 when a tentative file-scope definition is seen.
4873 But at end of compilation, do output code for them. */
4874 DECL_DEFER_OUTPUT (decl) = 1;
4875 if (asmspec && C_DECL_REGISTER (decl))
4876 DECL_HARD_REGISTER (decl) = 1;
4877 rest_of_decl_compilation (decl, true, 0);
4879 else
4881 /* In conjunction with an ASMSPEC, the `register'
4882 keyword indicates that we should place the variable
4883 in a particular register. */
4884 if (asmspec && C_DECL_REGISTER (decl))
4886 DECL_HARD_REGISTER (decl) = 1;
4887 /* This cannot be done for a structure with volatile
4888 fields, on which DECL_REGISTER will have been
4889 reset. */
4890 if (!DECL_REGISTER (decl))
4891 error ("cannot put object with volatile field into register");
4894 if (TREE_CODE (decl) != FUNCTION_DECL)
4896 /* If we're building a variable sized type, and we might be
4897 reachable other than via the top of the current binding
4898 level, then create a new BIND_EXPR so that we deallocate
4899 the object at the right time. */
4900 /* Note that DECL_SIZE can be null due to errors. */
4901 if (DECL_SIZE (decl)
4902 && !TREE_CONSTANT (DECL_SIZE (decl))
4903 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4905 tree bind;
4906 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4907 TREE_SIDE_EFFECTS (bind) = 1;
4908 add_stmt (bind);
4909 BIND_EXPR_BODY (bind) = push_stmt_list ();
4911 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4912 DECL_EXPR, decl));
4917 if (!DECL_FILE_SCOPE_P (decl))
4919 /* Recompute the RTL of a local array now
4920 if it used to be an incomplete type. */
4921 if (was_incomplete
4922 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4924 /* If we used it already as memory, it must stay in memory. */
4925 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4926 /* If it's still incomplete now, no init will save it. */
4927 if (DECL_SIZE (decl) == 0)
4928 DECL_INITIAL (decl) = 0;
4933 if (TREE_CODE (decl) == TYPE_DECL)
4935 if (!DECL_FILE_SCOPE_P (decl)
4936 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4937 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4939 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4942 /* Install a cleanup (aka destructor) if one was given. */
4943 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4945 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4946 if (attr)
4948 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4949 tree cleanup_decl = lookup_name (cleanup_id);
4950 tree cleanup;
4951 vec<tree, va_gc> *v;
4953 /* Build "cleanup(&decl)" for the destructor. */
4954 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4955 vec_alloc (v, 1);
4956 v->quick_push (cleanup);
4957 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4958 vNULL, cleanup_decl, v, NULL);
4959 vec_free (v);
4961 /* Don't warn about decl unused; the cleanup uses it. */
4962 TREE_USED (decl) = 1;
4963 TREE_USED (cleanup_decl) = 1;
4964 DECL_READ_P (decl) = 1;
4966 push_cleanup (decl, cleanup, false);
4970 if (warn_cxx_compat
4971 && TREE_CODE (decl) == VAR_DECL
4972 && !DECL_EXTERNAL (decl)
4973 && DECL_INITIAL (decl) == NULL_TREE)
4975 type = strip_array_types (type);
4976 if (TREE_READONLY (decl))
4977 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4978 "uninitialized const %qD is invalid in C++", decl);
4979 else if ((TREE_CODE (type) == RECORD_TYPE
4980 || TREE_CODE (type) == UNION_TYPE)
4981 && C_TYPE_FIELDS_READONLY (type))
4982 diagnose_uninitialized_cst_member (decl, type);
4985 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4988 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4989 EXPR is NULL or a pointer to an expression that needs to be
4990 evaluated for the side effects of array size expressions in the
4991 parameters. */
4993 tree
4994 grokparm (const struct c_parm *parm, tree *expr)
4996 tree attrs = parm->attrs;
4997 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4998 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5000 decl_attributes (&decl, attrs, 0);
5002 return decl;
5005 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5006 and push that on the current scope. EXPR is a pointer to an
5007 expression that needs to be evaluated for the side effects of array
5008 size expressions in the parameters. */
5010 void
5011 push_parm_decl (const struct c_parm *parm, tree *expr)
5013 tree attrs = parm->attrs;
5014 tree decl;
5016 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5017 &attrs, expr, NULL, DEPRECATED_NORMAL);
5018 decl_attributes (&decl, attrs, 0);
5020 decl = pushdecl (decl);
5022 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5025 /* Mark all the parameter declarations to date as forward decls.
5026 Also diagnose use of this extension. */
5028 void
5029 mark_forward_parm_decls (void)
5031 struct c_binding *b;
5033 if (pedantic && !current_scope->warned_forward_parm_decls)
5035 pedwarn (input_location, OPT_Wpedantic,
5036 "ISO C forbids forward parameter declarations");
5037 current_scope->warned_forward_parm_decls = true;
5040 for (b = current_scope->bindings; b; b = b->prev)
5041 if (TREE_CODE (b->decl) == PARM_DECL)
5042 TREE_ASM_WRITTEN (b->decl) = 1;
5045 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5046 literal, which may be an incomplete array type completed by the
5047 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5048 literal. NON_CONST is true if the initializers contain something
5049 that cannot occur in a constant expression. */
5051 tree
5052 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5054 /* We do not use start_decl here because we have a type, not a declarator;
5055 and do not use finish_decl because the decl should be stored inside
5056 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5057 tree decl;
5058 tree complit;
5059 tree stmt;
5061 if (type == error_mark_node
5062 || init == error_mark_node)
5063 return error_mark_node;
5065 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5066 DECL_EXTERNAL (decl) = 0;
5067 TREE_PUBLIC (decl) = 0;
5068 TREE_STATIC (decl) = (current_scope == file_scope);
5069 DECL_CONTEXT (decl) = current_function_decl;
5070 TREE_USED (decl) = 1;
5071 DECL_READ_P (decl) = 1;
5072 TREE_TYPE (decl) = type;
5073 TREE_READONLY (decl) = (TYPE_READONLY (type)
5074 || (TREE_CODE (type) == ARRAY_TYPE
5075 && TYPE_READONLY (TREE_TYPE (type))));
5076 store_init_value (loc, decl, init, NULL_TREE);
5078 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5080 int failure = complete_array_type (&TREE_TYPE (decl),
5081 DECL_INITIAL (decl), true);
5082 /* If complete_array_type returns 3, it means that the
5083 initial value of the compound literal is empty. Allow it. */
5084 gcc_assert (failure == 0 || failure == 3);
5086 type = TREE_TYPE (decl);
5087 TREE_TYPE (DECL_INITIAL (decl)) = type;
5090 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5092 c_incomplete_type_error (NULL_TREE, type);
5093 return error_mark_node;
5096 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5097 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5098 TREE_SIDE_EFFECTS (complit) = 1;
5100 layout_decl (decl, 0);
5102 if (TREE_STATIC (decl))
5104 /* This decl needs a name for the assembler output. */
5105 set_compound_literal_name (decl);
5106 DECL_DEFER_OUTPUT (decl) = 1;
5107 DECL_COMDAT (decl) = 1;
5108 DECL_ARTIFICIAL (decl) = 1;
5109 DECL_IGNORED_P (decl) = 1;
5110 pushdecl (decl);
5111 rest_of_decl_compilation (decl, 1, 0);
5114 if (non_const)
5116 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5117 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5120 return complit;
5123 /* Check the type of a compound literal. Here we just check that it
5124 is valid for C++. */
5126 void
5127 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5129 if (warn_cxx_compat
5130 && (type_name->specs->typespec_kind == ctsk_tagdef
5131 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5132 warning_at (loc, OPT_Wc___compat,
5133 "defining a type in a compound literal is invalid in C++");
5136 /* Determine whether TYPE is a structure with a flexible array member,
5137 or a union containing such a structure (possibly recursively). */
5139 static bool
5140 flexible_array_type_p (tree type)
5142 tree x;
5143 switch (TREE_CODE (type))
5145 case RECORD_TYPE:
5146 x = TYPE_FIELDS (type);
5147 if (x == NULL_TREE)
5148 return false;
5149 while (DECL_CHAIN (x) != NULL_TREE)
5150 x = DECL_CHAIN (x);
5151 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5152 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5153 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5154 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5155 return true;
5156 return false;
5157 case UNION_TYPE:
5158 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5160 if (flexible_array_type_p (TREE_TYPE (x)))
5161 return true;
5163 return false;
5164 default:
5165 return false;
5169 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5170 replacing with appropriate values if they are invalid. */
5171 static void
5172 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
5174 tree type_mv;
5175 unsigned int max_width;
5176 unsigned HOST_WIDE_INT w;
5177 const char *name = (orig_name
5178 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5179 : _("<anonymous>"));
5181 /* Detect and ignore out of range field width and process valid
5182 field widths. */
5183 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5185 error ("bit-field %qs width not an integer constant", name);
5186 *width = integer_one_node;
5188 else
5190 if (TREE_CODE (*width) != INTEGER_CST)
5192 *width = c_fully_fold (*width, false, NULL);
5193 if (TREE_CODE (*width) == INTEGER_CST)
5194 pedwarn (input_location, OPT_Wpedantic,
5195 "bit-field %qs width not an integer constant expression",
5196 name);
5198 if (TREE_CODE (*width) != INTEGER_CST)
5200 error ("bit-field %qs width not an integer constant", name);
5201 *width = integer_one_node;
5203 constant_expression_warning (*width);
5204 if (tree_int_cst_sgn (*width) < 0)
5206 error ("negative width in bit-field %qs", name);
5207 *width = integer_one_node;
5209 else if (integer_zerop (*width) && orig_name)
5211 error ("zero width for bit-field %qs", name);
5212 *width = integer_one_node;
5216 /* Detect invalid bit-field type. */
5217 if (TREE_CODE (*type) != INTEGER_TYPE
5218 && TREE_CODE (*type) != BOOLEAN_TYPE
5219 && TREE_CODE (*type) != ENUMERAL_TYPE)
5221 error ("bit-field %qs has invalid type", name);
5222 *type = unsigned_type_node;
5225 type_mv = TYPE_MAIN_VARIANT (*type);
5226 if (!in_system_header_at (input_location)
5227 && type_mv != integer_type_node
5228 && type_mv != unsigned_type_node
5229 && type_mv != boolean_type_node)
5230 pedwarn_c90 (input_location, OPT_Wpedantic,
5231 "type of bit-field %qs is a GCC extension", name);
5233 max_width = TYPE_PRECISION (*type);
5235 if (0 < compare_tree_int (*width, max_width))
5237 error ("width of %qs exceeds its type", name);
5238 w = max_width;
5239 *width = build_int_cst (integer_type_node, w);
5241 else
5242 w = tree_to_uhwi (*width);
5244 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5246 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5247 if (!lt
5248 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5249 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5250 warning (0, "%qs is narrower than values of its type", name);
5256 /* Print warning about variable length array if necessary. */
5258 static void
5259 warn_variable_length_array (tree name, tree size)
5261 if (TREE_CONSTANT (size))
5263 if (name)
5264 pedwarn_c90 (input_location, OPT_Wvla,
5265 "ISO C90 forbids array %qE whose size "
5266 "can%'t be evaluated", name);
5267 else
5268 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5269 "whose size can%'t be evaluated");
5271 else
5273 if (name)
5274 pedwarn_c90 (input_location, OPT_Wvla,
5275 "ISO C90 forbids variable length array %qE", name);
5276 else
5277 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5278 "length array");
5282 /* Print warning about defaulting to int if necessary. */
5284 static void
5285 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5287 diagnostic_info diagnostic;
5288 va_list ap;
5290 va_start (ap, gmsgid);
5291 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
5292 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5293 diagnostic.option_index = opt;
5294 report_diagnostic (&diagnostic);
5295 va_end (ap);
5298 /* Given declspecs and a declarator,
5299 determine the name and type of the object declared
5300 and construct a ..._DECL node for it.
5301 (In one case we can return a ..._TYPE node instead.
5302 For invalid input we sometimes return 0.)
5304 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5306 DECL_CONTEXT says which syntactic context this declaration is in:
5307 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5308 FUNCDEF for a function definition. Like NORMAL but a few different
5309 error messages in each case. Return value may be zero meaning
5310 this definition is too screwy to try to parse.
5311 PARM for a parameter declaration (either within a function prototype
5312 or before a function body). Make a PARM_DECL, or return void_type_node.
5313 TYPENAME if for a typename (in a cast or sizeof).
5314 Don't make a DECL node; just return the ..._TYPE node.
5315 FIELD for a struct or union field; make a FIELD_DECL.
5316 INITIALIZED is true if the decl has an initializer.
5317 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5318 representing the width of the bit-field.
5319 DECL_ATTRS points to the list of attributes that should be added to this
5320 decl. Any nested attributes that belong on the decl itself will be
5321 added to this list.
5322 If EXPR is not NULL, any expressions that need to be evaluated as
5323 part of evaluating variably modified types will be stored in *EXPR.
5324 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5325 set to indicate whether operands in *EXPR can be used in constant
5326 expressions.
5327 DEPRECATED_STATE is a deprecated_states value indicating whether
5328 deprecation warnings should be suppressed.
5330 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5331 It may also be so in the PARM case, for a prototype where the
5332 argument type is specified but not the name.
5334 This function is where the complicated C meanings of `static'
5335 and `extern' are interpreted. */
5337 static tree
5338 grokdeclarator (const struct c_declarator *declarator,
5339 struct c_declspecs *declspecs,
5340 enum decl_context decl_context, bool initialized, tree *width,
5341 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5342 enum deprecated_states deprecated_state)
5344 tree type = declspecs->type;
5345 bool threadp = declspecs->thread_p;
5346 enum c_storage_class storage_class = declspecs->storage_class;
5347 int constp;
5348 int restrictp;
5349 int volatilep;
5350 int atomicp;
5351 int type_quals = TYPE_UNQUALIFIED;
5352 tree name = NULL_TREE;
5353 bool funcdef_flag = false;
5354 bool funcdef_syntax = false;
5355 bool size_varies = false;
5356 tree decl_attr = declspecs->decl_attr;
5357 int array_ptr_quals = TYPE_UNQUALIFIED;
5358 tree array_ptr_attrs = NULL_TREE;
5359 int array_parm_static = 0;
5360 bool array_parm_vla_unspec_p = false;
5361 tree returned_attrs = NULL_TREE;
5362 bool bitfield = width != NULL;
5363 tree element_type;
5364 struct c_arg_info *arg_info = 0;
5365 addr_space_t as1, as2, address_space;
5366 location_t loc = UNKNOWN_LOCATION;
5367 const char *errmsg;
5368 tree expr_dummy;
5369 bool expr_const_operands_dummy;
5370 enum c_declarator_kind first_non_attr_kind;
5371 unsigned int alignas_align = 0;
5373 if (TREE_CODE (type) == ERROR_MARK)
5374 return error_mark_node;
5375 if (expr == NULL)
5376 expr = &expr_dummy;
5377 if (expr_const_operands == NULL)
5378 expr_const_operands = &expr_const_operands_dummy;
5380 *expr = declspecs->expr;
5381 *expr_const_operands = declspecs->expr_const_operands;
5383 if (decl_context == FUNCDEF)
5384 funcdef_flag = true, decl_context = NORMAL;
5386 /* Look inside a declarator for the name being declared
5387 and get it as an IDENTIFIER_NODE, for an error message. */
5389 const struct c_declarator *decl = declarator;
5391 first_non_attr_kind = cdk_attrs;
5392 while (decl)
5393 switch (decl->kind)
5395 case cdk_array:
5396 loc = decl->id_loc;
5397 /* FALL THRU. */
5399 case cdk_function:
5400 case cdk_pointer:
5401 funcdef_syntax = (decl->kind == cdk_function);
5402 decl = decl->declarator;
5403 if (first_non_attr_kind == cdk_attrs)
5404 first_non_attr_kind = decl->kind;
5405 break;
5407 case cdk_attrs:
5408 decl = decl->declarator;
5409 break;
5411 case cdk_id:
5412 loc = decl->id_loc;
5413 if (decl->u.id)
5414 name = decl->u.id;
5415 if (first_non_attr_kind == cdk_attrs)
5416 first_non_attr_kind = decl->kind;
5417 decl = 0;
5418 break;
5420 default:
5421 gcc_unreachable ();
5423 if (name == 0)
5425 gcc_assert (decl_context == PARM
5426 || decl_context == TYPENAME
5427 || (decl_context == FIELD
5428 && declarator->kind == cdk_id));
5429 gcc_assert (!initialized);
5433 /* A function definition's declarator must have the form of
5434 a function declarator. */
5436 if (funcdef_flag && !funcdef_syntax)
5437 return 0;
5439 /* If this looks like a function definition, make it one,
5440 even if it occurs where parms are expected.
5441 Then store_parm_decls will reject it and not use it as a parm. */
5442 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5443 decl_context = PARM;
5445 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5446 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5448 if ((decl_context == NORMAL || decl_context == FIELD)
5449 && current_scope == file_scope
5450 && variably_modified_type_p (type, NULL_TREE))
5452 if (name)
5453 error_at (loc, "variably modified %qE at file scope", name);
5454 else
5455 error_at (loc, "variably modified field at file scope");
5456 type = integer_type_node;
5459 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5461 /* Diagnose defaulting to "int". */
5463 if (declspecs->default_int_p && !in_system_header_at (input_location))
5465 /* Issue a warning if this is an ISO C 99 program or if
5466 -Wreturn-type and this is a function, or if -Wimplicit;
5467 prefer the former warning since it is more explicit. */
5468 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5469 && funcdef_flag)
5470 warn_about_return_type = 1;
5471 else
5473 if (name)
5474 warn_defaults_to (loc, OPT_Wimplicit_int,
5475 "type defaults to %<int%> in declaration "
5476 "of %qE", name);
5477 else
5478 warn_defaults_to (loc, OPT_Wimplicit_int,
5479 "type defaults to %<int%> in type name");
5483 /* Adjust the type if a bit-field is being declared,
5484 -funsigned-bitfields applied and the type is not explicitly
5485 "signed". */
5486 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5487 && TREE_CODE (type) == INTEGER_TYPE)
5488 type = unsigned_type_for (type);
5490 /* Figure out the type qualifiers for the declaration. There are
5491 two ways a declaration can become qualified. One is something
5492 like `const int i' where the `const' is explicit. Another is
5493 something like `typedef const int CI; CI i' where the type of the
5494 declaration contains the `const'. A third possibility is that
5495 there is a type qualifier on the element type of a typedefed
5496 array type, in which case we should extract that qualifier so
5497 that c_apply_type_quals_to_decl receives the full list of
5498 qualifiers to work with (C90 is not entirely clear about whether
5499 duplicate qualifiers should be diagnosed in this case, but it
5500 seems most appropriate to do so). */
5501 element_type = strip_array_types (type);
5502 constp = declspecs->const_p + TYPE_READONLY (element_type);
5503 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5504 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5505 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5506 as1 = declspecs->address_space;
5507 as2 = TYPE_ADDR_SPACE (element_type);
5508 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5510 if (constp > 1)
5511 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5512 if (restrictp > 1)
5513 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5514 if (volatilep > 1)
5515 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5516 if (atomicp > 1)
5517 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5519 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5520 error_at (loc, "conflicting named address spaces (%s vs %s)",
5521 c_addr_space_name (as1), c_addr_space_name (as2));
5523 if ((TREE_CODE (type) == ARRAY_TYPE
5524 || first_non_attr_kind == cdk_array)
5525 && TYPE_QUALS (element_type))
5526 type = TYPE_MAIN_VARIANT (type);
5527 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5528 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5529 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5530 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5531 | ENCODE_QUAL_ADDR_SPACE (address_space));
5533 /* Applying the _Atomic qualifier to an array type (through the use
5534 of typedefs or typeof) must be detected here. If the qualifier
5535 is introduced later, any appearance of applying it to an array is
5536 actually applying it to an element of that array. */
5537 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5538 error_at (loc, "%<_Atomic%>-qualified array type");
5540 /* Warn about storage classes that are invalid for certain
5541 kinds of declarations (parameters, typenames, etc.). */
5543 if (funcdef_flag
5544 && (threadp
5545 || storage_class == csc_auto
5546 || storage_class == csc_register
5547 || storage_class == csc_typedef))
5549 if (storage_class == csc_auto)
5550 pedwarn (loc,
5551 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5552 "function definition declared %<auto%>");
5553 if (storage_class == csc_register)
5554 error_at (loc, "function definition declared %<register%>");
5555 if (storage_class == csc_typedef)
5556 error_at (loc, "function definition declared %<typedef%>");
5557 if (threadp)
5558 error_at (loc, "function definition declared %qs",
5559 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5560 threadp = false;
5561 if (storage_class == csc_auto
5562 || storage_class == csc_register
5563 || storage_class == csc_typedef)
5564 storage_class = csc_none;
5566 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5568 if (decl_context == PARM && storage_class == csc_register)
5570 else
5572 switch (decl_context)
5574 case FIELD:
5575 if (name)
5576 error_at (loc, "storage class specified for structure "
5577 "field %qE", name);
5578 else
5579 error_at (loc, "storage class specified for structure field");
5580 break;
5581 case PARM:
5582 if (name)
5583 error_at (loc, "storage class specified for parameter %qE",
5584 name);
5585 else
5586 error_at (loc, "storage class specified for unnamed parameter");
5587 break;
5588 default:
5589 error_at (loc, "storage class specified for typename");
5590 break;
5592 storage_class = csc_none;
5593 threadp = false;
5596 else if (storage_class == csc_extern
5597 && initialized
5598 && !funcdef_flag)
5600 /* 'extern' with initialization is invalid if not at file scope. */
5601 if (current_scope == file_scope)
5603 /* It is fine to have 'extern const' when compiling at C
5604 and C++ intersection. */
5605 if (!(warn_cxx_compat && constp))
5606 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5607 name);
5609 else
5610 error_at (loc, "%qE has both %<extern%> and initializer", name);
5612 else if (current_scope == file_scope)
5614 if (storage_class == csc_auto)
5615 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5616 name);
5617 if (pedantic && storage_class == csc_register)
5618 pedwarn (input_location, OPT_Wpedantic,
5619 "file-scope declaration of %qE specifies %<register%>", name);
5621 else
5623 if (storage_class == csc_extern && funcdef_flag)
5624 error_at (loc, "nested function %qE declared %<extern%>", name);
5625 else if (threadp && storage_class == csc_none)
5627 error_at (loc, "function-scope %qE implicitly auto and declared "
5628 "%qs", name,
5629 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5630 threadp = false;
5634 /* Now figure out the structure of the declarator proper.
5635 Descend through it, creating more complex types, until we reach
5636 the declared identifier (or NULL_TREE, in an absolute declarator).
5637 At each stage we maintain an unqualified version of the type
5638 together with any qualifiers that should be applied to it with
5639 c_build_qualified_type; this way, array types including
5640 multidimensional array types are first built up in unqualified
5641 form and then the qualified form is created with
5642 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5644 while (declarator && declarator->kind != cdk_id)
5646 if (type == error_mark_node)
5648 declarator = declarator->declarator;
5649 continue;
5652 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5653 a cdk_pointer (for *...),
5654 a cdk_function (for ...(...)),
5655 a cdk_attrs (for nested attributes),
5656 or a cdk_id (for the name being declared
5657 or the place in an absolute declarator
5658 where the name was omitted).
5659 For the last case, we have just exited the loop.
5661 At this point, TYPE is the type of elements of an array,
5662 or for a function to return, or for a pointer to point to.
5663 After this sequence of ifs, TYPE is the type of the
5664 array or function or pointer, and DECLARATOR has had its
5665 outermost layer removed. */
5667 if (array_ptr_quals != TYPE_UNQUALIFIED
5668 || array_ptr_attrs != NULL_TREE
5669 || array_parm_static)
5671 /* Only the innermost declarator (making a parameter be of
5672 array type which is converted to pointer type)
5673 may have static or type qualifiers. */
5674 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5675 array_ptr_quals = TYPE_UNQUALIFIED;
5676 array_ptr_attrs = NULL_TREE;
5677 array_parm_static = 0;
5680 switch (declarator->kind)
5682 case cdk_attrs:
5684 /* A declarator with embedded attributes. */
5685 tree attrs = declarator->u.attrs;
5686 const struct c_declarator *inner_decl;
5687 int attr_flags = 0;
5688 declarator = declarator->declarator;
5689 inner_decl = declarator;
5690 while (inner_decl->kind == cdk_attrs)
5691 inner_decl = inner_decl->declarator;
5692 if (inner_decl->kind == cdk_id)
5693 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5694 else if (inner_decl->kind == cdk_function)
5695 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5696 else if (inner_decl->kind == cdk_array)
5697 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5698 returned_attrs = decl_attributes (&type,
5699 chainon (returned_attrs, attrs),
5700 attr_flags);
5701 break;
5703 case cdk_array:
5705 tree itype = NULL_TREE;
5706 tree size = declarator->u.array.dimen;
5707 /* The index is a signed object `sizetype' bits wide. */
5708 tree index_type = c_common_signed_type (sizetype);
5710 array_ptr_quals = declarator->u.array.quals;
5711 array_ptr_attrs = declarator->u.array.attrs;
5712 array_parm_static = declarator->u.array.static_p;
5713 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5715 declarator = declarator->declarator;
5717 /* Check for some types that there cannot be arrays of. */
5719 if (VOID_TYPE_P (type))
5721 if (name)
5722 error_at (loc, "declaration of %qE as array of voids", name);
5723 else
5724 error_at (loc, "declaration of type name as array of voids");
5725 type = error_mark_node;
5728 if (TREE_CODE (type) == FUNCTION_TYPE)
5730 if (name)
5731 error_at (loc, "declaration of %qE as array of functions",
5732 name);
5733 else
5734 error_at (loc, "declaration of type name as array of "
5735 "functions");
5736 type = error_mark_node;
5739 if (pedantic && !in_system_header_at (input_location)
5740 && flexible_array_type_p (type))
5741 pedwarn (loc, OPT_Wpedantic,
5742 "invalid use of structure with flexible array member");
5744 if (size == error_mark_node)
5745 type = error_mark_node;
5747 if (type == error_mark_node)
5748 continue;
5750 /* If size was specified, set ITYPE to a range-type for
5751 that size. Otherwise, ITYPE remains null. finish_decl
5752 may figure it out from an initial value. */
5754 if (size)
5756 bool size_maybe_const = true;
5757 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5758 && !TREE_OVERFLOW (size));
5759 bool this_size_varies = false;
5761 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5762 lvalue. */
5763 STRIP_TYPE_NOPS (size);
5765 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5767 if (name)
5768 error_at (loc, "size of array %qE has non-integer type",
5769 name);
5770 else
5771 error_at (loc,
5772 "size of unnamed array has non-integer type");
5773 size = integer_one_node;
5776 size = c_fully_fold (size, false, &size_maybe_const);
5778 if (pedantic && size_maybe_const && integer_zerop (size))
5780 if (name)
5781 pedwarn (loc, OPT_Wpedantic,
5782 "ISO C forbids zero-size array %qE", name);
5783 else
5784 pedwarn (loc, OPT_Wpedantic,
5785 "ISO C forbids zero-size array");
5788 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5790 constant_expression_warning (size);
5791 if (tree_int_cst_sgn (size) < 0)
5793 if (name)
5794 error_at (loc, "size of array %qE is negative", name);
5795 else
5796 error_at (loc, "size of unnamed array is negative");
5797 size = integer_one_node;
5799 /* Handle a size folded to an integer constant but
5800 not an integer constant expression. */
5801 if (!size_int_const)
5803 /* If this is a file scope declaration of an
5804 ordinary identifier, this is invalid code;
5805 diagnosing it here and not subsequently
5806 treating the type as variable-length avoids
5807 more confusing diagnostics later. */
5808 if ((decl_context == NORMAL || decl_context == FIELD)
5809 && current_scope == file_scope)
5810 pedwarn (input_location, 0,
5811 "variably modified %qE at file scope",
5812 name);
5813 else
5814 this_size_varies = size_varies = true;
5815 warn_variable_length_array (name, size);
5818 else if ((decl_context == NORMAL || decl_context == FIELD)
5819 && current_scope == file_scope)
5821 error_at (loc, "variably modified %qE at file scope", name);
5822 size = integer_one_node;
5824 else
5826 /* Make sure the array size remains visibly
5827 nonconstant even if it is (eg) a const variable
5828 with known value. */
5829 this_size_varies = size_varies = true;
5830 warn_variable_length_array (name, size);
5831 if (flag_sanitize & SANITIZE_VLA
5832 && decl_context == NORMAL
5833 && do_ubsan_in_current_function ())
5835 /* Evaluate the array size only once. */
5836 size = c_save_expr (size);
5837 size = c_fully_fold (size, false, NULL);
5838 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5839 ubsan_instrument_vla (loc, size),
5840 size);
5844 if (integer_zerop (size) && !this_size_varies)
5846 /* A zero-length array cannot be represented with
5847 an unsigned index type, which is what we'll
5848 get with build_index_type. Create an
5849 open-ended range instead. */
5850 itype = build_range_type (sizetype, size, NULL_TREE);
5852 else
5854 /* Arrange for the SAVE_EXPR on the inside of the
5855 MINUS_EXPR, which allows the -1 to get folded
5856 with the +1 that happens when building TYPE_SIZE. */
5857 if (size_varies)
5858 size = save_expr (size);
5859 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5860 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5861 integer_zero_node, size);
5863 /* Compute the maximum valid index, that is, size
5864 - 1. Do the calculation in index_type, so that
5865 if it is a variable the computations will be
5866 done in the proper mode. */
5867 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5868 convert (index_type, size),
5869 convert (index_type,
5870 size_one_node));
5872 /* The above overflows when size does not fit
5873 in index_type.
5874 ??? While a size of INT_MAX+1 technically shouldn't
5875 cause an overflow (because we subtract 1), handling
5876 this case seems like an unnecessary complication. */
5877 if (TREE_CODE (size) == INTEGER_CST
5878 && !int_fits_type_p (size, index_type))
5880 if (name)
5881 error_at (loc, "size of array %qE is too large",
5882 name);
5883 else
5884 error_at (loc, "size of unnamed array is too large");
5885 type = error_mark_node;
5886 continue;
5889 itype = build_index_type (itype);
5891 if (this_size_varies)
5893 if (*expr)
5894 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5895 *expr, size);
5896 else
5897 *expr = size;
5898 *expr_const_operands &= size_maybe_const;
5901 else if (decl_context == FIELD)
5903 bool flexible_array_member = false;
5904 if (array_parm_vla_unspec_p)
5905 /* Field names can in fact have function prototype
5906 scope so [*] is disallowed here through making
5907 the field variably modified, not through being
5908 something other than a declaration with function
5909 prototype scope. */
5910 size_varies = true;
5911 else
5913 const struct c_declarator *t = declarator;
5914 while (t->kind == cdk_attrs)
5915 t = t->declarator;
5916 flexible_array_member = (t->kind == cdk_id);
5918 if (flexible_array_member
5919 && !in_system_header_at (input_location))
5920 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
5921 "support flexible array members");
5923 /* ISO C99 Flexible array members are effectively
5924 identical to GCC's zero-length array extension. */
5925 if (flexible_array_member || array_parm_vla_unspec_p)
5926 itype = build_range_type (sizetype, size_zero_node,
5927 NULL_TREE);
5929 else if (decl_context == PARM)
5931 if (array_parm_vla_unspec_p)
5933 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5934 size_varies = true;
5937 else if (decl_context == TYPENAME)
5939 if (array_parm_vla_unspec_p)
5941 /* C99 6.7.5.2p4 */
5942 warning (0, "%<[*]%> not in a declaration");
5943 /* We use this to avoid messing up with incomplete
5944 array types of the same type, that would
5945 otherwise be modified below. */
5946 itype = build_range_type (sizetype, size_zero_node,
5947 NULL_TREE);
5948 size_varies = true;
5952 /* Complain about arrays of incomplete types. */
5953 if (!COMPLETE_TYPE_P (type))
5955 error_at (loc, "array type has incomplete element type %qT",
5956 type);
5957 type = error_mark_node;
5959 else
5960 /* When itype is NULL, a shared incomplete array type is
5961 returned for all array of a given type. Elsewhere we
5962 make sure we don't complete that type before copying
5963 it, but here we want to make sure we don't ever
5964 modify the shared type, so we gcc_assert (itype)
5965 below. */
5967 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5968 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5969 type = build_qualified_type (type,
5970 ENCODE_QUAL_ADDR_SPACE (as));
5972 type = build_array_type (type, itype);
5975 if (type != error_mark_node)
5977 if (size_varies)
5979 /* It is ok to modify type here even if itype is
5980 NULL: if size_varies, we're in a
5981 multi-dimensional array and the inner type has
5982 variable size, so the enclosing shared array type
5983 must too. */
5984 if (size && TREE_CODE (size) == INTEGER_CST)
5985 type
5986 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5987 C_TYPE_VARIABLE_SIZE (type) = 1;
5990 /* The GCC extension for zero-length arrays differs from
5991 ISO flexible array members in that sizeof yields
5992 zero. */
5993 if (size && integer_zerop (size))
5995 gcc_assert (itype);
5996 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5997 TYPE_SIZE (type) = bitsize_zero_node;
5998 TYPE_SIZE_UNIT (type) = size_zero_node;
5999 SET_TYPE_STRUCTURAL_EQUALITY (type);
6001 if (array_parm_vla_unspec_p)
6003 gcc_assert (itype);
6004 /* The type is complete. C99 6.7.5.2p4 */
6005 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6006 TYPE_SIZE (type) = bitsize_zero_node;
6007 TYPE_SIZE_UNIT (type) = size_zero_node;
6008 SET_TYPE_STRUCTURAL_EQUALITY (type);
6012 if (decl_context != PARM
6013 && (array_ptr_quals != TYPE_UNQUALIFIED
6014 || array_ptr_attrs != NULL_TREE
6015 || array_parm_static))
6017 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6018 array_ptr_quals = TYPE_UNQUALIFIED;
6019 array_ptr_attrs = NULL_TREE;
6020 array_parm_static = 0;
6022 break;
6024 case cdk_function:
6026 /* Say it's a definition only for the declarator closest
6027 to the identifier, apart possibly from some
6028 attributes. */
6029 bool really_funcdef = false;
6030 tree arg_types;
6031 if (funcdef_flag)
6033 const struct c_declarator *t = declarator->declarator;
6034 while (t->kind == cdk_attrs)
6035 t = t->declarator;
6036 really_funcdef = (t->kind == cdk_id);
6039 /* Declaring a function type. Make sure we have a valid
6040 type for the function to return. */
6041 if (type == error_mark_node)
6042 continue;
6044 size_varies = false;
6046 /* Warn about some types functions can't return. */
6047 if (TREE_CODE (type) == FUNCTION_TYPE)
6049 if (name)
6050 error_at (loc, "%qE declared as function returning a "
6051 "function", name);
6052 else
6053 error_at (loc, "type name declared as function "
6054 "returning a function");
6055 type = integer_type_node;
6057 if (TREE_CODE (type) == ARRAY_TYPE)
6059 if (name)
6060 error_at (loc, "%qE declared as function returning an array",
6061 name);
6062 else
6063 error_at (loc, "type name declared as function returning "
6064 "an array");
6065 type = integer_type_node;
6067 errmsg = targetm.invalid_return_type (type);
6068 if (errmsg)
6070 error (errmsg);
6071 type = integer_type_node;
6074 /* Construct the function type and go to the next
6075 inner layer of declarator. */
6076 arg_info = declarator->u.arg_info;
6077 arg_types = grokparms (arg_info, really_funcdef);
6079 /* Type qualifiers before the return type of the function
6080 qualify the return type, not the function type. */
6081 if (type_quals)
6083 /* Type qualifiers on a function return type are
6084 normally permitted by the standard but have no
6085 effect, so give a warning at -Wreturn-type.
6086 Qualifiers on a void return type are banned on
6087 function definitions in ISO C; GCC used to used
6088 them for noreturn functions. */
6089 if (VOID_TYPE_P (type) && really_funcdef)
6090 pedwarn (loc, 0,
6091 "function definition has qualified void return type");
6092 else
6093 warning_at (loc, OPT_Wignored_qualifiers,
6094 "type qualifiers ignored on function return type");
6096 type = c_build_qualified_type (type, type_quals);
6098 type_quals = TYPE_UNQUALIFIED;
6100 type = build_function_type (type, arg_types);
6101 declarator = declarator->declarator;
6103 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6104 the formal parameter list of this FUNCTION_TYPE to point to
6105 the FUNCTION_TYPE node itself. */
6107 c_arg_tag *tag;
6108 unsigned ix;
6110 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6111 TYPE_CONTEXT (tag->type) = type;
6113 break;
6115 case cdk_pointer:
6117 /* Merge any constancy or volatility into the target type
6118 for the pointer. */
6119 if ((type_quals & TYPE_QUAL_ATOMIC)
6120 && TREE_CODE (type) == FUNCTION_TYPE)
6122 error_at (loc,
6123 "%<_Atomic%>-qualified function type");
6124 type_quals &= ~TYPE_QUAL_ATOMIC;
6126 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6127 && type_quals)
6128 pedwarn (loc, OPT_Wpedantic,
6129 "ISO C forbids qualified function types");
6130 if (type_quals)
6131 type = c_build_qualified_type (type, type_quals);
6132 size_varies = false;
6134 /* When the pointed-to type involves components of variable size,
6135 care must be taken to ensure that the size evaluation code is
6136 emitted early enough to dominate all the possible later uses
6137 and late enough for the variables on which it depends to have
6138 been assigned.
6140 This is expected to happen automatically when the pointed-to
6141 type has a name/declaration of it's own, but special attention
6142 is required if the type is anonymous.
6144 We handle the NORMAL and FIELD contexts here by attaching an
6145 artificial TYPE_DECL to such pointed-to type. This forces the
6146 sizes evaluation at a safe point and ensures it is not deferred
6147 until e.g. within a deeper conditional context.
6149 We expect nothing to be needed here for PARM or TYPENAME.
6150 Pushing a TYPE_DECL at this point for TYPENAME would actually
6151 be incorrect, as we might be in the middle of an expression
6152 with side effects on the pointed-to type size "arguments" prior
6153 to the pointer declaration point and the fake TYPE_DECL in the
6154 enclosing context would force the size evaluation prior to the
6155 side effects. */
6157 if (!TYPE_NAME (type)
6158 && (decl_context == NORMAL || decl_context == FIELD)
6159 && variably_modified_type_p (type, NULL_TREE))
6161 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6162 DECL_ARTIFICIAL (decl) = 1;
6163 pushdecl (decl);
6164 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6165 TYPE_NAME (type) = decl;
6168 type = c_build_pointer_type (type);
6170 /* Process type qualifiers (such as const or volatile)
6171 that were given inside the `*'. */
6172 type_quals = declarator->u.pointer_quals;
6174 declarator = declarator->declarator;
6175 break;
6177 default:
6178 gcc_unreachable ();
6181 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6183 /* Now TYPE has the actual type, apart from any qualifiers in
6184 TYPE_QUALS. */
6186 /* Warn about address space used for things other than static memory or
6187 pointers. */
6188 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6189 if (!ADDR_SPACE_GENERIC_P (address_space))
6191 if (decl_context == NORMAL)
6193 switch (storage_class)
6195 case csc_auto:
6196 error ("%qs combined with %<auto%> qualifier for %qE",
6197 c_addr_space_name (address_space), name);
6198 break;
6199 case csc_register:
6200 error ("%qs combined with %<register%> qualifier for %qE",
6201 c_addr_space_name (address_space), name);
6202 break;
6203 case csc_none:
6204 if (current_function_scope)
6206 error ("%qs specified for auto variable %qE",
6207 c_addr_space_name (address_space), name);
6208 break;
6210 break;
6211 case csc_static:
6212 case csc_extern:
6213 case csc_typedef:
6214 break;
6215 default:
6216 gcc_unreachable ();
6219 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6221 if (name)
6222 error ("%qs specified for parameter %qE",
6223 c_addr_space_name (address_space), name);
6224 else
6225 error ("%qs specified for unnamed parameter",
6226 c_addr_space_name (address_space));
6228 else if (decl_context == FIELD)
6230 if (name)
6231 error ("%qs specified for structure field %qE",
6232 c_addr_space_name (address_space), name);
6233 else
6234 error ("%qs specified for structure field",
6235 c_addr_space_name (address_space));
6239 /* Check the type and width of a bit-field. */
6240 if (bitfield)
6242 check_bitfield_type_and_width (&type, width, name);
6243 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6244 atomic types are permitted for bit-fields; we have no code to
6245 make bit-field accesses atomic, so disallow them. */
6246 if (type_quals & TYPE_QUAL_ATOMIC)
6248 if (name)
6249 error ("bit-field %qE has atomic type", name);
6250 else
6251 error ("bit-field has atomic type");
6252 type_quals &= ~TYPE_QUAL_ATOMIC;
6256 /* Reject invalid uses of _Alignas. */
6257 if (declspecs->alignas_p)
6259 if (storage_class == csc_typedef)
6260 error_at (loc, "alignment specified for typedef %qE", name);
6261 else if (storage_class == csc_register)
6262 error_at (loc, "alignment specified for %<register%> object %qE",
6263 name);
6264 else if (decl_context == PARM)
6266 if (name)
6267 error_at (loc, "alignment specified for parameter %qE", name);
6268 else
6269 error_at (loc, "alignment specified for unnamed parameter");
6271 else if (bitfield)
6273 if (name)
6274 error_at (loc, "alignment specified for bit-field %qE", name);
6275 else
6276 error_at (loc, "alignment specified for unnamed bit-field");
6278 else if (TREE_CODE (type) == FUNCTION_TYPE)
6279 error_at (loc, "alignment specified for function %qE", name);
6280 else if (declspecs->align_log != -1)
6282 alignas_align = 1U << declspecs->align_log;
6283 if (alignas_align < min_align_of_type (type))
6285 if (name)
6286 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6287 "alignment of %qE", name);
6288 else
6289 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6290 "alignment of unnamed field");
6291 alignas_align = 0;
6296 /* Did array size calculations overflow or does the array cover more
6297 than half of the address-space? */
6298 if (TREE_CODE (type) == ARRAY_TYPE
6299 && COMPLETE_TYPE_P (type)
6300 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6301 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
6303 if (name)
6304 error_at (loc, "size of array %qE is too large", name);
6305 else
6306 error_at (loc, "size of unnamed array is too large");
6307 /* If we proceed with the array type as it is, we'll eventually
6308 crash in tree_to_[su]hwi(). */
6309 type = error_mark_node;
6312 /* If this is declaring a typedef name, return a TYPE_DECL. */
6314 if (storage_class == csc_typedef)
6316 tree decl;
6317 if ((type_quals & TYPE_QUAL_ATOMIC)
6318 && TREE_CODE (type) == FUNCTION_TYPE)
6320 error_at (loc,
6321 "%<_Atomic%>-qualified function type");
6322 type_quals &= ~TYPE_QUAL_ATOMIC;
6324 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6325 && type_quals)
6326 pedwarn (loc, OPT_Wpedantic,
6327 "ISO C forbids qualified function types");
6328 if (type_quals)
6329 type = c_build_qualified_type (type, type_quals);
6330 decl = build_decl (declarator->id_loc,
6331 TYPE_DECL, declarator->u.id, type);
6332 if (declspecs->explicit_signed_p)
6333 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6334 if (declspecs->inline_p)
6335 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6336 if (declspecs->noreturn_p)
6337 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6339 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6341 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6343 if (b != NULL
6344 && b->decl != NULL_TREE
6345 && (B_IN_CURRENT_SCOPE (b)
6346 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6347 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6349 warning_at (declarator->id_loc, OPT_Wc___compat,
6350 ("using %qD as both a typedef and a tag is "
6351 "invalid in C++"),
6352 decl);
6353 if (b->locus != UNKNOWN_LOCATION)
6354 inform (b->locus, "originally defined here");
6358 return decl;
6361 /* If this is a type name (such as, in a cast or sizeof),
6362 compute the type and return it now. */
6364 if (decl_context == TYPENAME)
6366 /* Note that the grammar rejects storage classes in typenames
6367 and fields. */
6368 gcc_assert (storage_class == csc_none && !threadp
6369 && !declspecs->inline_p && !declspecs->noreturn_p);
6370 if ((type_quals & TYPE_QUAL_ATOMIC)
6371 && TREE_CODE (type) == FUNCTION_TYPE)
6373 error_at (loc,
6374 "%<_Atomic%>-qualified function type");
6375 type_quals &= ~TYPE_QUAL_ATOMIC;
6377 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6378 && type_quals)
6379 pedwarn (loc, OPT_Wpedantic,
6380 "ISO C forbids const or volatile function types");
6381 if (type_quals)
6382 type = c_build_qualified_type (type, type_quals);
6383 return type;
6386 if (pedantic && decl_context == FIELD
6387 && variably_modified_type_p (type, NULL_TREE))
6389 /* C99 6.7.2.1p8 */
6390 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6391 "have a variably modified type");
6394 /* Aside from typedefs and type names (handle above),
6395 `void' at top level (not within pointer)
6396 is allowed only in public variables.
6397 We don't complain about parms either, but that is because
6398 a better error message can be made later. */
6400 if (VOID_TYPE_P (type) && decl_context != PARM
6401 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6402 && (storage_class == csc_extern
6403 || (current_scope == file_scope
6404 && !(storage_class == csc_static
6405 || storage_class == csc_register)))))
6407 error_at (loc, "variable or field %qE declared void", name);
6408 type = integer_type_node;
6411 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6412 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6415 tree decl;
6417 if (decl_context == PARM)
6419 tree promoted_type;
6420 bool array_parameter_p = false;
6422 /* A parameter declared as an array of T is really a pointer to T.
6423 One declared as a function is really a pointer to a function. */
6425 if (TREE_CODE (type) == ARRAY_TYPE)
6427 /* Transfer const-ness of array into that of type pointed to. */
6428 type = TREE_TYPE (type);
6429 if (type_quals)
6430 type = c_build_qualified_type (type, type_quals);
6431 type = c_build_pointer_type (type);
6432 type_quals = array_ptr_quals;
6433 if (type_quals)
6434 type = c_build_qualified_type (type, type_quals);
6436 /* We don't yet implement attributes in this context. */
6437 if (array_ptr_attrs != NULL_TREE)
6438 warning_at (loc, OPT_Wattributes,
6439 "attributes in parameter array declarator ignored");
6441 size_varies = false;
6442 array_parameter_p = true;
6444 else if (TREE_CODE (type) == FUNCTION_TYPE)
6446 if (type_quals & TYPE_QUAL_ATOMIC)
6448 error_at (loc,
6449 "%<_Atomic%>-qualified function type");
6450 type_quals &= ~TYPE_QUAL_ATOMIC;
6452 else if (type_quals)
6453 pedwarn (loc, OPT_Wpedantic,
6454 "ISO C forbids qualified function types");
6455 if (type_quals)
6456 type = c_build_qualified_type (type, type_quals);
6457 type = c_build_pointer_type (type);
6458 type_quals = TYPE_UNQUALIFIED;
6460 else if (type_quals)
6461 type = c_build_qualified_type (type, type_quals);
6463 decl = build_decl (declarator->id_loc,
6464 PARM_DECL, declarator->u.id, type);
6465 if (size_varies)
6466 C_DECL_VARIABLE_SIZE (decl) = 1;
6467 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6469 /* Compute the type actually passed in the parmlist,
6470 for the case where there is no prototype.
6471 (For example, shorts and chars are passed as ints.)
6472 When there is a prototype, this is overridden later. */
6474 if (type == error_mark_node)
6475 promoted_type = type;
6476 else
6477 promoted_type = c_type_promotes_to (type);
6479 DECL_ARG_TYPE (decl) = promoted_type;
6480 if (declspecs->inline_p)
6481 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6482 if (declspecs->noreturn_p)
6483 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6485 else if (decl_context == FIELD)
6487 /* Note that the grammar rejects storage classes in typenames
6488 and fields. */
6489 gcc_assert (storage_class == csc_none && !threadp
6490 && !declspecs->inline_p && !declspecs->noreturn_p);
6492 /* Structure field. It may not be a function. */
6494 if (TREE_CODE (type) == FUNCTION_TYPE)
6496 error_at (loc, "field %qE declared as a function", name);
6497 type = build_pointer_type (type);
6499 else if (TREE_CODE (type) != ERROR_MARK
6500 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6502 if (name)
6503 error_at (loc, "field %qE has incomplete type", name);
6504 else
6505 error_at (loc, "unnamed field has incomplete type");
6506 type = error_mark_node;
6508 else if (TREE_CODE (type) == ARRAY_TYPE
6509 && TYPE_DOMAIN (type) == NULL_TREE)
6511 /* We have a flexible array member through a typedef.
6512 Set suitable range. Whether this is a correct position
6513 for a flexible array member will be determined elsewhere. */
6514 if (!in_system_header_at (input_location))
6515 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6516 "support flexible array members");
6517 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6518 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6519 NULL_TREE);
6521 type = c_build_qualified_type (type, type_quals);
6522 decl = build_decl (declarator->id_loc,
6523 FIELD_DECL, declarator->u.id, type);
6524 DECL_NONADDRESSABLE_P (decl) = bitfield;
6525 if (bitfield && !declarator->u.id)
6526 TREE_NO_WARNING (decl) = 1;
6528 if (size_varies)
6529 C_DECL_VARIABLE_SIZE (decl) = 1;
6531 else if (TREE_CODE (type) == FUNCTION_TYPE)
6533 if (storage_class == csc_register || threadp)
6535 error_at (loc, "invalid storage class for function %qE", name);
6537 else if (current_scope != file_scope)
6539 /* Function declaration not at file scope. Storage
6540 classes other than `extern' are not allowed, C99
6541 6.7.1p5, and `extern' makes no difference. However,
6542 GCC allows 'auto', perhaps with 'inline', to support
6543 nested functions. */
6544 if (storage_class == csc_auto)
6545 pedwarn (loc, OPT_Wpedantic,
6546 "invalid storage class for function %qE", name);
6547 else if (storage_class == csc_static)
6549 error_at (loc, "invalid storage class for function %qE", name);
6550 if (funcdef_flag)
6551 storage_class = declspecs->storage_class = csc_none;
6552 else
6553 return 0;
6557 decl = build_decl (declarator->id_loc,
6558 FUNCTION_DECL, declarator->u.id, type);
6559 decl = build_decl_attribute_variant (decl, decl_attr);
6561 if (type_quals & TYPE_QUAL_ATOMIC)
6563 error_at (loc,
6564 "%<_Atomic%>-qualified function type");
6565 type_quals &= ~TYPE_QUAL_ATOMIC;
6567 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6568 pedwarn (loc, OPT_Wpedantic,
6569 "ISO C forbids qualified function types");
6571 /* Every function declaration is an external reference
6572 (DECL_EXTERNAL) except for those which are not at file
6573 scope and are explicitly declared "auto". This is
6574 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6575 GCC to signify a forward declaration of a nested function. */
6576 if (storage_class == csc_auto && current_scope != file_scope)
6577 DECL_EXTERNAL (decl) = 0;
6578 /* In C99, a function which is declared 'inline' with 'extern'
6579 is not an external reference (which is confusing). It
6580 means that the later definition of the function must be output
6581 in this file, C99 6.7.4p6. In GNU C89, a function declared
6582 'extern inline' is an external reference. */
6583 else if (declspecs->inline_p && storage_class != csc_static)
6584 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6585 == flag_gnu89_inline);
6586 else
6587 DECL_EXTERNAL (decl) = !initialized;
6589 /* Record absence of global scope for `static' or `auto'. */
6590 TREE_PUBLIC (decl)
6591 = !(storage_class == csc_static || storage_class == csc_auto);
6593 /* For a function definition, record the argument information
6594 block where store_parm_decls will look for it. */
6595 if (funcdef_flag)
6596 current_function_arg_info = arg_info;
6598 if (declspecs->default_int_p)
6599 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6601 /* Record presence of `inline' and `_Noreturn', if it is
6602 reasonable. */
6603 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6605 if (declspecs->inline_p)
6606 pedwarn (loc, 0, "cannot inline function %<main%>");
6607 if (declspecs->noreturn_p)
6608 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6610 else
6612 if (declspecs->inline_p)
6613 /* Record that the function is declared `inline'. */
6614 DECL_DECLARED_INLINE_P (decl) = 1;
6615 if (declspecs->noreturn_p)
6617 if (flag_isoc99)
6618 pedwarn_c99 (loc, OPT_Wpedantic,
6619 "ISO C99 does not support %<_Noreturn%>");
6620 else
6621 pedwarn_c99 (loc, OPT_Wpedantic,
6622 "ISO C90 does not support %<_Noreturn%>");
6623 TREE_THIS_VOLATILE (decl) = 1;
6627 else
6629 /* It's a variable. */
6630 /* An uninitialized decl with `extern' is a reference. */
6631 int extern_ref = !initialized && storage_class == csc_extern;
6633 type = c_build_qualified_type (type, type_quals);
6635 /* C99 6.2.2p7: It is invalid (compile-time undefined
6636 behavior) to create an 'extern' declaration for a
6637 variable if there is a global declaration that is
6638 'static' and the global declaration is not visible.
6639 (If the static declaration _is_ currently visible,
6640 the 'extern' declaration is taken to refer to that decl.) */
6641 if (extern_ref && current_scope != file_scope)
6643 tree global_decl = identifier_global_value (declarator->u.id);
6644 tree visible_decl = lookup_name (declarator->u.id);
6646 if (global_decl
6647 && global_decl != visible_decl
6648 && TREE_CODE (global_decl) == VAR_DECL
6649 && !TREE_PUBLIC (global_decl))
6650 error_at (loc, "variable previously declared %<static%> "
6651 "redeclared %<extern%>");
6654 decl = build_decl (declarator->id_loc,
6655 VAR_DECL, declarator->u.id, type);
6656 if (size_varies)
6657 C_DECL_VARIABLE_SIZE (decl) = 1;
6659 if (declspecs->inline_p)
6660 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6661 if (declspecs->noreturn_p)
6662 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6664 /* At file scope, an initialized extern declaration may follow
6665 a static declaration. In that case, DECL_EXTERNAL will be
6666 reset later in start_decl. */
6667 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6669 /* At file scope, the presence of a `static' or `register' storage
6670 class specifier, or the absence of all storage class specifiers
6671 makes this declaration a definition (perhaps tentative). Also,
6672 the absence of `static' makes it public. */
6673 if (current_scope == file_scope)
6675 TREE_PUBLIC (decl) = storage_class != csc_static;
6676 TREE_STATIC (decl) = !extern_ref;
6678 /* Not at file scope, only `static' makes a static definition. */
6679 else
6681 TREE_STATIC (decl) = (storage_class == csc_static);
6682 TREE_PUBLIC (decl) = extern_ref;
6685 if (threadp)
6686 set_decl_tls_model (decl, decl_default_tls_model (decl));
6689 if ((storage_class == csc_extern
6690 || (storage_class == csc_none
6691 && TREE_CODE (type) == FUNCTION_TYPE
6692 && !funcdef_flag))
6693 && variably_modified_type_p (type, NULL_TREE))
6695 /* C99 6.7.5.2p2 */
6696 if (TREE_CODE (type) == FUNCTION_TYPE)
6697 error_at (loc, "non-nested function with variably modified type");
6698 else
6699 error_at (loc, "object with variably modified type must have "
6700 "no linkage");
6703 /* Record `register' declaration for warnings on &
6704 and in case doing stupid register allocation. */
6706 if (storage_class == csc_register)
6708 C_DECL_REGISTER (decl) = 1;
6709 DECL_REGISTER (decl) = 1;
6712 /* Record constancy and volatility. */
6713 c_apply_type_quals_to_decl (type_quals, decl);
6715 /* Apply _Alignas specifiers. */
6716 if (alignas_align)
6718 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6719 DECL_USER_ALIGN (decl) = 1;
6722 /* If a type has volatile components, it should be stored in memory.
6723 Otherwise, the fact that those components are volatile
6724 will be ignored, and would even crash the compiler.
6725 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6726 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6727 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6728 || TREE_CODE (decl) == RESULT_DECL))
6730 /* It is not an error for a structure with volatile fields to
6731 be declared register, but reset DECL_REGISTER since it
6732 cannot actually go in a register. */
6733 int was_reg = C_DECL_REGISTER (decl);
6734 C_DECL_REGISTER (decl) = 0;
6735 DECL_REGISTER (decl) = 0;
6736 c_mark_addressable (decl);
6737 C_DECL_REGISTER (decl) = was_reg;
6740 /* This is the earliest point at which we might know the assembler
6741 name of a variable. Thus, if it's known before this, die horribly. */
6742 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6744 if (warn_cxx_compat
6745 && TREE_CODE (decl) == VAR_DECL
6746 && TREE_PUBLIC (decl)
6747 && TREE_STATIC (decl)
6748 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6749 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6750 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6751 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6752 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6753 ("non-local variable %qD with anonymous type is "
6754 "questionable in C++"),
6755 decl);
6757 return decl;
6761 /* Decode the parameter-list info for a function type or function definition.
6762 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6763 if there is an identifier list instead of a parameter decl list).
6764 These two functions are separate because when a function returns
6765 or receives functions then each is called multiple times but the order
6766 of calls is different. The last call to `grokparms' is always the one
6767 that contains the formal parameter names of a function definition.
6769 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6771 FUNCDEF_FLAG is true for a function definition, false for
6772 a mere declaration. A nonempty identifier-list gets an error message
6773 when FUNCDEF_FLAG is false. */
6775 static tree
6776 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6778 tree arg_types = arg_info->types;
6780 if (funcdef_flag && arg_info->had_vla_unspec)
6782 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6783 /* C99 6.7.5.2p4 */
6784 error ("%<[*]%> not allowed in other than function prototype scope");
6787 if (arg_types == 0 && !funcdef_flag
6788 && !in_system_header_at (input_location))
6789 warning (OPT_Wstrict_prototypes,
6790 "function declaration isn%'t a prototype");
6792 if (arg_types == error_mark_node)
6793 return 0; /* don't set TYPE_ARG_TYPES in this case */
6795 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6797 if (!funcdef_flag)
6799 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6800 arg_info->parms = NULL_TREE;
6802 else
6803 arg_info->parms = arg_info->types;
6805 arg_info->types = 0;
6806 return 0;
6808 else
6810 tree parm, type, typelt;
6811 unsigned int parmno;
6812 const char *errmsg;
6814 /* If there is a parameter of incomplete type in a definition,
6815 this is an error. In a declaration this is valid, and a
6816 struct or union type may be completed later, before any calls
6817 or definition of the function. In the case where the tag was
6818 first declared within the parameter list, a warning has
6819 already been given. If a parameter has void type, then
6820 however the function cannot be defined or called, so
6821 warn. */
6823 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6824 parm;
6825 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6827 type = TREE_VALUE (typelt);
6828 if (type == error_mark_node)
6829 continue;
6831 if (!COMPLETE_TYPE_P (type))
6833 if (funcdef_flag)
6835 if (DECL_NAME (parm))
6836 error_at (input_location,
6837 "parameter %u (%q+D) has incomplete type",
6838 parmno, parm);
6839 else
6840 error_at (DECL_SOURCE_LOCATION (parm),
6841 "parameter %u has incomplete type",
6842 parmno);
6844 TREE_VALUE (typelt) = error_mark_node;
6845 TREE_TYPE (parm) = error_mark_node;
6846 arg_types = NULL_TREE;
6848 else if (VOID_TYPE_P (type))
6850 if (DECL_NAME (parm))
6851 warning_at (input_location, 0,
6852 "parameter %u (%q+D) has void type",
6853 parmno, parm);
6854 else
6855 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6856 "parameter %u has void type",
6857 parmno);
6861 errmsg = targetm.invalid_parameter_type (type);
6862 if (errmsg)
6864 error (errmsg);
6865 TREE_VALUE (typelt) = error_mark_node;
6866 TREE_TYPE (parm) = error_mark_node;
6867 arg_types = NULL_TREE;
6870 if (DECL_NAME (parm) && TREE_USED (parm))
6871 warn_if_shadowing (parm);
6873 return arg_types;
6877 /* Allocate and initialize a c_arg_info structure from the parser's
6878 obstack. */
6880 struct c_arg_info *
6881 build_arg_info (void)
6883 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6884 ret->parms = NULL_TREE;
6885 ret->tags = NULL;
6886 ret->types = NULL_TREE;
6887 ret->others = NULL_TREE;
6888 ret->pending_sizes = NULL;
6889 ret->had_vla_unspec = 0;
6890 return ret;
6893 /* Take apart the current scope and return a c_arg_info structure with
6894 info on a parameter list just parsed.
6896 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6898 ELLIPSIS being true means the argument list ended in '...' so don't
6899 append a sentinel (void_list_node) to the end of the type-list.
6901 EXPR is NULL or an expression that needs to be evaluated for the
6902 side effects of array size expressions in the parameters. */
6904 struct c_arg_info *
6905 get_parm_info (bool ellipsis, tree expr)
6907 struct c_binding *b = current_scope->bindings;
6908 struct c_arg_info *arg_info = build_arg_info ();
6910 tree parms = 0;
6911 vec<c_arg_tag, va_gc> *tags = NULL;
6912 tree types = 0;
6913 tree others = 0;
6915 static bool explained_incomplete_types = false;
6916 bool gave_void_only_once_err = false;
6918 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6920 /* The bindings in this scope must not get put into a block.
6921 We will take care of deleting the binding nodes. */
6922 current_scope->bindings = 0;
6924 /* This function is only called if there was *something* on the
6925 parameter list. */
6926 gcc_assert (b);
6928 /* A parameter list consisting solely of 'void' indicates that the
6929 function takes no arguments. But if the 'void' is qualified
6930 (by 'const' or 'volatile'), or has a storage class specifier
6931 ('register'), then the behavior is undefined; issue an error.
6932 Typedefs for 'void' are OK (see DR#157). */
6933 if (b->prev == 0 /* one binding */
6934 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6935 && !DECL_NAME (b->decl) /* anonymous */
6936 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6938 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
6939 || C_DECL_REGISTER (b->decl))
6940 error ("%<void%> as only parameter may not be qualified");
6942 /* There cannot be an ellipsis. */
6943 if (ellipsis)
6944 error ("%<void%> must be the only parameter");
6946 arg_info->types = void_list_node;
6947 return arg_info;
6950 if (!ellipsis)
6951 types = void_list_node;
6953 /* Break up the bindings list into parms, tags, types, and others;
6954 apply sanity checks; purge the name-to-decl bindings. */
6955 while (b)
6957 tree decl = b->decl;
6958 tree type = TREE_TYPE (decl);
6959 c_arg_tag tag;
6960 const char *keyword;
6962 switch (TREE_CODE (decl))
6964 case PARM_DECL:
6965 if (b->id)
6967 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6968 I_SYMBOL_BINDING (b->id) = b->shadowed;
6971 /* Check for forward decls that never got their actual decl. */
6972 if (TREE_ASM_WRITTEN (decl))
6973 error ("parameter %q+D has just a forward declaration", decl);
6974 /* Check for (..., void, ...) and issue an error. */
6975 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6977 if (!gave_void_only_once_err)
6979 error ("%<void%> must be the only parameter");
6980 gave_void_only_once_err = true;
6983 else
6985 /* Valid parameter, add it to the list. */
6986 DECL_CHAIN (decl) = parms;
6987 parms = decl;
6989 /* Since there is a prototype, args are passed in their
6990 declared types. The back end may override this later. */
6991 DECL_ARG_TYPE (decl) = type;
6992 types = tree_cons (0, type, types);
6994 break;
6996 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6997 case UNION_TYPE: keyword = "union"; goto tag;
6998 case RECORD_TYPE: keyword = "struct"; goto tag;
6999 tag:
7000 /* Types may not have tag-names, in which case the type
7001 appears in the bindings list with b->id NULL. */
7002 if (b->id)
7004 gcc_assert (I_TAG_BINDING (b->id) == b);
7005 I_TAG_BINDING (b->id) = b->shadowed;
7008 /* Warn about any struct, union or enum tags defined in a
7009 parameter list. The scope of such types is limited to
7010 the parameter list, which is rarely if ever desirable
7011 (it's impossible to call such a function with type-
7012 correct arguments). An anonymous union parm type is
7013 meaningful as a GNU extension, so don't warn for that. */
7014 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7016 if (b->id)
7017 /* The %s will be one of 'struct', 'union', or 'enum'. */
7018 warning (0, "%<%s %E%> declared inside parameter list",
7019 keyword, b->id);
7020 else
7021 /* The %s will be one of 'struct', 'union', or 'enum'. */
7022 warning (0, "anonymous %s declared inside parameter list",
7023 keyword);
7025 if (!explained_incomplete_types)
7027 warning (0, "its scope is only this definition or declaration,"
7028 " which is probably not what you want");
7029 explained_incomplete_types = true;
7033 tag.id = b->id;
7034 tag.type = decl;
7035 vec_safe_push (tags, tag);
7036 break;
7038 case CONST_DECL:
7039 case TYPE_DECL:
7040 case FUNCTION_DECL:
7041 /* CONST_DECLs appear here when we have an embedded enum,
7042 and TYPE_DECLs appear here when we have an embedded struct
7043 or union. No warnings for this - we already warned about the
7044 type itself. FUNCTION_DECLs appear when there is an implicit
7045 function declaration in the parameter list. */
7047 /* When we reinsert this decl in the function body, we need
7048 to reconstruct whether it was marked as nested. */
7049 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7050 ? b->nested
7051 : !b->nested);
7052 DECL_CHAIN (decl) = others;
7053 others = decl;
7054 /* fall through */
7056 case ERROR_MARK:
7057 /* error_mark_node appears here when we have an undeclared
7058 variable. Just throw it away. */
7059 if (b->id)
7061 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7062 I_SYMBOL_BINDING (b->id) = b->shadowed;
7064 break;
7066 /* Other things that might be encountered. */
7067 case LABEL_DECL:
7068 case VAR_DECL:
7069 default:
7070 gcc_unreachable ();
7073 b = free_binding_and_advance (b);
7076 arg_info->parms = parms;
7077 arg_info->tags = tags;
7078 arg_info->types = types;
7079 arg_info->others = others;
7080 arg_info->pending_sizes = expr;
7081 return arg_info;
7084 /* Get the struct, enum or union (CODE says which) with tag NAME.
7085 Define the tag as a forward-reference with location LOC if it is
7086 not defined. Return a c_typespec structure for the type
7087 specifier. */
7089 struct c_typespec
7090 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7092 struct c_typespec ret;
7093 tree ref;
7094 location_t refloc;
7096 ret.expr = NULL_TREE;
7097 ret.expr_const_operands = true;
7099 /* If a cross reference is requested, look up the type
7100 already defined for this tag and return it. */
7102 ref = lookup_tag (code, name, 0, &refloc);
7103 /* If this is the right type of tag, return what we found.
7104 (This reference will be shadowed by shadow_tag later if appropriate.)
7105 If this is the wrong type of tag, do not return it. If it was the
7106 wrong type in the same scope, we will have had an error
7107 message already; if in a different scope and declaring
7108 a name, pending_xref_error will give an error message; but if in a
7109 different scope and not declaring a name, this tag should
7110 shadow the previous declaration of a different type of tag, and
7111 this would not work properly if we return the reference found.
7112 (For example, with "struct foo" in an outer scope, "union foo;"
7113 must shadow that tag with a new one of union type.) */
7114 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7115 if (ref && TREE_CODE (ref) == code)
7117 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7118 && loc != UNKNOWN_LOCATION
7119 && warn_cxx_compat)
7121 switch (code)
7123 case ENUMERAL_TYPE:
7124 warning_at (loc, OPT_Wc___compat,
7125 ("enum type defined in struct or union "
7126 "is not visible in C++"));
7127 inform (refloc, "enum type defined here");
7128 break;
7129 case RECORD_TYPE:
7130 warning_at (loc, OPT_Wc___compat,
7131 ("struct defined in struct or union "
7132 "is not visible in C++"));
7133 inform (refloc, "struct defined here");
7134 break;
7135 case UNION_TYPE:
7136 warning_at (loc, OPT_Wc___compat,
7137 ("union defined in struct or union "
7138 "is not visible in C++"));
7139 inform (refloc, "union defined here");
7140 break;
7141 default:
7142 gcc_unreachable();
7146 ret.spec = ref;
7147 return ret;
7150 /* If no such tag is yet defined, create a forward-reference node
7151 and record it as the "definition".
7152 When a real declaration of this type is found,
7153 the forward-reference will be altered into a real type. */
7155 ref = make_node (code);
7156 if (code == ENUMERAL_TYPE)
7158 /* Give the type a default layout like unsigned int
7159 to avoid crashing if it does not get defined. */
7160 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7161 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
7162 TYPE_USER_ALIGN (ref) = 0;
7163 TYPE_UNSIGNED (ref) = 1;
7164 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7165 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7166 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7169 pushtag (loc, name, ref);
7171 ret.spec = ref;
7172 return ret;
7175 /* Get the struct, enum or union (CODE says which) with tag NAME.
7176 Define the tag as a forward-reference if it is not defined.
7177 Return a tree for the type. */
7179 tree
7180 xref_tag (enum tree_code code, tree name)
7182 return parser_xref_tag (input_location, code, name).spec;
7185 /* Make sure that the tag NAME is defined *in the current scope*
7186 at least as a forward reference.
7187 LOC is the location of the struct's definition.
7188 CODE says which kind of tag NAME ought to be.
7190 This stores the current value of the file static STRUCT_PARSE_INFO
7191 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7192 new c_struct_parse_info structure. The old value of
7193 STRUCT_PARSE_INFO is restored in finish_struct. */
7195 tree
7196 start_struct (location_t loc, enum tree_code code, tree name,
7197 struct c_struct_parse_info **enclosing_struct_parse_info)
7199 /* If there is already a tag defined at this scope
7200 (as a forward reference), just return it. */
7202 tree ref = NULL_TREE;
7203 location_t refloc = UNKNOWN_LOCATION;
7205 if (name != NULL_TREE)
7206 ref = lookup_tag (code, name, 1, &refloc);
7207 if (ref && TREE_CODE (ref) == code)
7209 if (TYPE_SIZE (ref))
7211 if (code == UNION_TYPE)
7212 error_at (loc, "redefinition of %<union %E%>", name);
7213 else
7214 error_at (loc, "redefinition of %<struct %E%>", name);
7215 if (refloc != UNKNOWN_LOCATION)
7216 inform (refloc, "originally defined here");
7217 /* Don't create structures using a name already in use. */
7218 ref = NULL_TREE;
7220 else if (C_TYPE_BEING_DEFINED (ref))
7222 if (code == UNION_TYPE)
7223 error_at (loc, "nested redefinition of %<union %E%>", name);
7224 else
7225 error_at (loc, "nested redefinition of %<struct %E%>", name);
7226 /* Don't bother to report "originally defined here" for a
7227 nested redefinition; the original definition should be
7228 obvious. */
7229 /* Don't create structures that contain themselves. */
7230 ref = NULL_TREE;
7234 /* Otherwise create a forward-reference just so the tag is in scope. */
7236 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7238 ref = make_node (code);
7239 pushtag (loc, name, ref);
7242 C_TYPE_BEING_DEFINED (ref) = 1;
7243 TYPE_PACKED (ref) = flag_pack_struct;
7245 *enclosing_struct_parse_info = struct_parse_info;
7246 struct_parse_info = XNEW (struct c_struct_parse_info);
7247 struct_parse_info->struct_types.create (0);
7248 struct_parse_info->fields.create (0);
7249 struct_parse_info->typedefs_seen.create (0);
7251 /* FIXME: This will issue a warning for a use of a type defined
7252 within a statement expr used within sizeof, et. al. This is not
7253 terribly serious as C++ doesn't permit statement exprs within
7254 sizeof anyhow. */
7255 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7256 warning_at (loc, OPT_Wc___compat,
7257 "defining type in %qs expression is invalid in C++",
7258 (in_sizeof
7259 ? "sizeof"
7260 : (in_typeof ? "typeof" : "alignof")));
7262 return ref;
7265 /* Process the specs, declarator and width (NULL if omitted)
7266 of a structure component, returning a FIELD_DECL node.
7267 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7268 DECL_ATTRS is as for grokdeclarator.
7270 LOC is the location of the structure component.
7272 This is done during the parsing of the struct declaration.
7273 The FIELD_DECL nodes are chained together and the lot of them
7274 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7276 tree
7277 grokfield (location_t loc,
7278 struct c_declarator *declarator, struct c_declspecs *declspecs,
7279 tree width, tree *decl_attrs)
7281 tree value;
7283 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7284 && width == NULL_TREE)
7286 /* This is an unnamed decl.
7288 If we have something of the form "union { list } ;" then this
7289 is the anonymous union extension. Similarly for struct.
7291 If this is something of the form "struct foo;", then
7292 If MS or Plan 9 extensions are enabled, this is handled as
7293 an anonymous struct.
7294 Otherwise this is a forward declaration of a structure tag.
7296 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7297 If foo names a structure or union without a tag, then this
7298 is an anonymous struct (this is permitted by C11).
7299 If MS or Plan 9 extensions are enabled and foo names a
7300 structure, then again this is an anonymous struct.
7301 Otherwise this is an error.
7303 Oh what a horrid tangled web we weave. I wonder if MS consciously
7304 took this from Plan 9 or if it was an accident of implementation
7305 that took root before someone noticed the bug... */
7307 tree type = declspecs->type;
7308 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
7309 || TREE_CODE (type) == UNION_TYPE);
7310 bool ok = false;
7312 if (type_ok
7313 && (flag_ms_extensions
7314 || flag_plan9_extensions
7315 || !declspecs->typedef_p))
7317 if (flag_ms_extensions || flag_plan9_extensions)
7318 ok = true;
7319 else if (TYPE_NAME (type) == NULL)
7320 ok = true;
7321 else
7322 ok = false;
7324 if (!ok)
7326 pedwarn (loc, 0, "declaration does not declare anything");
7327 return NULL_TREE;
7329 if (flag_isoc99)
7330 pedwarn_c99 (loc, OPT_Wpedantic,
7331 "ISO C99 doesn%'t support unnamed structs/unions");
7332 else
7333 pedwarn_c99 (loc, OPT_Wpedantic,
7334 "ISO C90 doesn%'t support unnamed structs/unions");
7337 value = grokdeclarator (declarator, declspecs, FIELD, false,
7338 width ? &width : NULL, decl_attrs, NULL, NULL,
7339 DEPRECATED_NORMAL);
7341 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7342 DECL_INITIAL (value) = width;
7344 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7346 /* If we currently have a binding for this field, set the
7347 in_struct field in the binding, so that we warn about lookups
7348 which find it. */
7349 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7350 if (b != NULL)
7352 /* If the in_struct field is not yet set, push it on a list
7353 to be cleared when this struct is finished. */
7354 if (!b->in_struct)
7356 struct_parse_info->fields.safe_push (b);
7357 b->in_struct = 1;
7362 return value;
7365 /* Subroutine of detect_field_duplicates: return whether X and Y,
7366 which are both fields in the same struct, have duplicate field
7367 names. */
7369 static bool
7370 is_duplicate_field (tree x, tree y)
7372 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7373 return true;
7375 /* When using -fplan9-extensions, an anonymous field whose name is a
7376 typedef can duplicate a field name. */
7377 if (flag_plan9_extensions
7378 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7380 tree xt, xn, yt, yn;
7382 xt = TREE_TYPE (x);
7383 if (DECL_NAME (x) != NULL_TREE)
7384 xn = DECL_NAME (x);
7385 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
7386 && TYPE_NAME (xt) != NULL_TREE
7387 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7388 xn = DECL_NAME (TYPE_NAME (xt));
7389 else
7390 xn = NULL_TREE;
7392 yt = TREE_TYPE (y);
7393 if (DECL_NAME (y) != NULL_TREE)
7394 yn = DECL_NAME (y);
7395 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
7396 && TYPE_NAME (yt) != NULL_TREE
7397 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7398 yn = DECL_NAME (TYPE_NAME (yt));
7399 else
7400 yn = NULL_TREE;
7402 if (xn != NULL_TREE && xn == yn)
7403 return true;
7406 return false;
7409 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7410 to HTAB, giving errors for any duplicates. */
7412 static void
7413 detect_field_duplicates_hash (tree fieldlist,
7414 hash_table<pointer_hash <tree_node> > *htab)
7416 tree x, y;
7417 tree_node **slot;
7419 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7420 if ((y = DECL_NAME (x)) != 0)
7422 slot = htab->find_slot (y, INSERT);
7423 if (*slot)
7425 error ("duplicate member %q+D", x);
7426 DECL_NAME (x) = NULL_TREE;
7428 *slot = y;
7430 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7431 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7433 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7435 /* When using -fplan9-extensions, an anonymous field whose
7436 name is a typedef can duplicate a field name. */
7437 if (flag_plan9_extensions
7438 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7439 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7441 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7442 slot = htab->find_slot (xn, INSERT);
7443 if (*slot)
7444 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7445 *slot = xn;
7450 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7451 the list such that this does not present a problem later. */
7453 static void
7454 detect_field_duplicates (tree fieldlist)
7456 tree x, y;
7457 int timeout = 10;
7459 /* If the struct is the list of instance variables of an Objective-C
7460 class, then we need to check all the instance variables of
7461 superclasses when checking for duplicates (since you can't have
7462 an instance variable in a subclass with the same name as an
7463 instance variable in a superclass). We pass on this job to the
7464 Objective-C compiler. objc_detect_field_duplicates() will return
7465 false if we are not checking the list of instance variables and
7466 the C frontend should proceed with the standard field duplicate
7467 checks. If we are checking the list of instance variables, the
7468 ObjC frontend will do the check, emit the errors if needed, and
7469 then return true. */
7470 if (c_dialect_objc ())
7471 if (objc_detect_field_duplicates (false))
7472 return;
7474 /* First, see if there are more than "a few" fields.
7475 This is trivially true if there are zero or one fields. */
7476 if (!fieldlist || !DECL_CHAIN (fieldlist))
7477 return;
7478 x = fieldlist;
7479 do {
7480 timeout--;
7481 if (DECL_NAME (x) == NULL_TREE
7482 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7483 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7484 timeout = 0;
7485 x = DECL_CHAIN (x);
7486 } while (timeout > 0 && x);
7488 /* If there were "few" fields and no anonymous structures or unions,
7489 avoid the overhead of allocating a hash table. Instead just do
7490 the nested traversal thing. */
7491 if (timeout > 0)
7493 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7494 /* When using -fplan9-extensions, we can have duplicates
7495 between typedef names and fields. */
7496 if (DECL_NAME (x)
7497 || (flag_plan9_extensions
7498 && DECL_NAME (x) == NULL_TREE
7499 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7500 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7501 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7502 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7504 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7505 if (is_duplicate_field (y, x))
7507 error ("duplicate member %q+D", x);
7508 DECL_NAME (x) = NULL_TREE;
7512 else
7514 hash_table<pointer_hash <tree_node> > htab (37);
7515 detect_field_duplicates_hash (fieldlist, &htab);
7519 /* Finish up struct info used by -Wc++-compat. */
7521 static void
7522 warn_cxx_compat_finish_struct (tree fieldlist)
7524 unsigned int ix;
7525 tree x;
7526 struct c_binding *b;
7528 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7529 the current struct. We do this now at the end of the struct
7530 because the flag is used to issue visibility warnings, and we
7531 only want to issue those warnings if the type is referenced
7532 outside of the struct declaration. */
7533 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7534 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7536 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7537 typedefs used when declaring fields in this struct. If the name
7538 of any of the fields is also a typedef name then the struct would
7539 not parse in C++, because the C++ lookup rules say that the
7540 typedef name would be looked up in the context of the struct, and
7541 would thus be the field rather than the typedef. */
7542 if (!struct_parse_info->typedefs_seen.is_empty ()
7543 && fieldlist != NULL_TREE)
7545 /* Use a hash_set<tree> using the name of the typedef. We can use
7546 a hash_set<tree> because identifiers are interned. */
7547 hash_set<tree> tset;
7549 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7550 tset.add (DECL_NAME (x));
7552 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7554 if (DECL_NAME (x) != NULL_TREE
7555 && tset.contains (DECL_NAME (x)))
7557 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7558 ("using %qD as both field and typedef name is "
7559 "invalid in C++"),
7561 /* FIXME: It would be nice to report the location where
7562 the typedef name is used. */
7567 /* For each field which has a binding and which was not defined in
7568 an enclosing struct, clear the in_struct field. */
7569 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7570 b->in_struct = 0;
7573 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7574 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7575 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7576 ATTRIBUTES are attributes to be applied to the structure.
7578 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7579 the struct was started. */
7581 tree
7582 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7583 struct c_struct_parse_info *enclosing_struct_parse_info)
7585 tree x;
7586 bool toplevel = file_scope == current_scope;
7587 int saw_named_field;
7589 /* If this type was previously laid out as a forward reference,
7590 make sure we lay it out again. */
7592 TYPE_SIZE (t) = 0;
7594 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7596 if (pedantic)
7598 for (x = fieldlist; x; x = DECL_CHAIN (x))
7600 if (DECL_NAME (x) != 0)
7601 break;
7602 if (flag_isoc11
7603 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7604 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7605 break;
7608 if (x == 0)
7610 if (TREE_CODE (t) == UNION_TYPE)
7612 if (fieldlist)
7613 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7614 else
7615 pedwarn (loc, OPT_Wpedantic, "union has no members");
7617 else
7619 if (fieldlist)
7620 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7621 else
7622 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7627 /* Install struct as DECL_CONTEXT of each field decl.
7628 Also process specified field sizes, found in the DECL_INITIAL,
7629 storing 0 there after the type has been changed to precision equal
7630 to its width, rather than the precision of the specified standard
7631 type. (Correct layout requires the original type to have been preserved
7632 until now.) */
7634 saw_named_field = 0;
7635 for (x = fieldlist; x; x = DECL_CHAIN (x))
7637 if (TREE_TYPE (x) == error_mark_node)
7638 continue;
7640 DECL_CONTEXT (x) = t;
7642 /* If any field is const, the structure type is pseudo-const. */
7643 if (TREE_READONLY (x))
7644 C_TYPE_FIELDS_READONLY (t) = 1;
7645 else
7647 /* A field that is pseudo-const makes the structure likewise. */
7648 tree t1 = strip_array_types (TREE_TYPE (x));
7649 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7650 && C_TYPE_FIELDS_READONLY (t1))
7651 C_TYPE_FIELDS_READONLY (t) = 1;
7654 /* Any field that is volatile means variables of this type must be
7655 treated in some ways as volatile. */
7656 if (TREE_THIS_VOLATILE (x))
7657 C_TYPE_FIELDS_VOLATILE (t) = 1;
7659 /* Any field of nominal variable size implies structure is too. */
7660 if (C_DECL_VARIABLE_SIZE (x))
7661 C_TYPE_VARIABLE_SIZE (t) = 1;
7663 if (DECL_INITIAL (x))
7665 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7666 DECL_SIZE (x) = bitsize_int (width);
7667 DECL_BIT_FIELD (x) = 1;
7668 SET_DECL_C_BIT_FIELD (x);
7671 if (TYPE_PACKED (t)
7672 && (DECL_BIT_FIELD (x)
7673 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7674 DECL_PACKED (x) = 1;
7676 /* Detect flexible array member in an invalid context. */
7677 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7678 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7679 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7680 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7682 if (TREE_CODE (t) == UNION_TYPE)
7684 error_at (DECL_SOURCE_LOCATION (x),
7685 "flexible array member in union");
7686 TREE_TYPE (x) = error_mark_node;
7688 else if (DECL_CHAIN (x) != NULL_TREE)
7690 error_at (DECL_SOURCE_LOCATION (x),
7691 "flexible array member not at end of struct");
7692 TREE_TYPE (x) = error_mark_node;
7694 else if (!saw_named_field)
7696 error_at (DECL_SOURCE_LOCATION (x),
7697 "flexible array member in otherwise empty struct");
7698 TREE_TYPE (x) = error_mark_node;
7702 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7703 && flexible_array_type_p (TREE_TYPE (x)))
7704 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7705 "invalid use of structure with flexible array member");
7707 if (DECL_NAME (x)
7708 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7709 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7710 saw_named_field = 1;
7713 detect_field_duplicates (fieldlist);
7715 /* Now we have the nearly final fieldlist. Record it,
7716 then lay out the structure or union (including the fields). */
7718 TYPE_FIELDS (t) = fieldlist;
7720 layout_type (t);
7722 if (TYPE_SIZE_UNIT (t)
7723 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7724 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7725 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7726 error ("type %qT is too large", t);
7728 /* Give bit-fields their proper types. */
7730 tree *fieldlistp = &fieldlist;
7731 while (*fieldlistp)
7732 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7733 && TREE_TYPE (*fieldlistp) != error_mark_node)
7735 unsigned HOST_WIDE_INT width
7736 = tree_to_uhwi (DECL_INITIAL (*fieldlistp));
7737 tree type = TREE_TYPE (*fieldlistp);
7738 if (width != TYPE_PRECISION (type))
7740 TREE_TYPE (*fieldlistp)
7741 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7742 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7744 DECL_INITIAL (*fieldlistp) = 0;
7746 else
7747 fieldlistp = &DECL_CHAIN (*fieldlistp);
7750 /* Now we have the truly final field list.
7751 Store it in this type and in the variants. */
7753 TYPE_FIELDS (t) = fieldlist;
7755 /* If there are lots of fields, sort so we can look through them fast.
7756 We arbitrarily consider 16 or more elts to be "a lot". */
7759 int len = 0;
7761 for (x = fieldlist; x; x = DECL_CHAIN (x))
7763 if (len > 15 || DECL_NAME (x) == NULL)
7764 break;
7765 len += 1;
7768 if (len > 15)
7770 tree *field_array;
7771 struct lang_type *space;
7772 struct sorted_fields_type *space2;
7774 len += list_length (x);
7776 /* Use the same allocation policy here that make_node uses, to
7777 ensure that this lives as long as the rest of the struct decl.
7778 All decls in an inline function need to be saved. */
7780 space = ggc_cleared_alloc<struct lang_type> ();
7781 space2 = (sorted_fields_type *) ggc_internal_alloc
7782 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7784 len = 0;
7785 space->s = space2;
7786 field_array = &space2->elts[0];
7787 for (x = fieldlist; x; x = DECL_CHAIN (x))
7789 field_array[len++] = x;
7791 /* If there is anonymous struct or union, break out of the loop. */
7792 if (DECL_NAME (x) == NULL)
7793 break;
7795 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7796 if (x == NULL)
7798 TYPE_LANG_SPECIFIC (t) = space;
7799 TYPE_LANG_SPECIFIC (t)->s->len = len;
7800 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7801 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7806 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7808 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7809 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7810 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7811 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7812 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7815 /* If this was supposed to be a transparent union, but we can't
7816 make it one, warn and turn off the flag. */
7817 if (TREE_CODE (t) == UNION_TYPE
7818 && TYPE_TRANSPARENT_AGGR (t)
7819 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7821 TYPE_TRANSPARENT_AGGR (t) = 0;
7822 warning_at (loc, 0, "union cannot be made transparent");
7825 /* If this structure or union completes the type of any previous
7826 variable declaration, lay it out and output its rtl. */
7827 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7829 x = TREE_CHAIN (x))
7831 tree decl = TREE_VALUE (x);
7832 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7833 layout_array_type (TREE_TYPE (decl));
7834 if (TREE_CODE (decl) != TYPE_DECL)
7836 layout_decl (decl, 0);
7837 if (c_dialect_objc ())
7838 objc_check_decl (decl);
7839 rest_of_decl_compilation (decl, toplevel, 0);
7842 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7844 /* Update type location to the one of the definition, instead of e.g.
7845 a forward declaration. */
7846 if (TYPE_STUB_DECL (t))
7847 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7849 /* Finish debugging output for this type. */
7850 rest_of_type_compilation (t, toplevel);
7852 /* If we're inside a function proper, i.e. not file-scope and not still
7853 parsing parameters, then arrange for the size of a variable sized type
7854 to be bound now. */
7855 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7856 add_stmt (build_stmt (loc,
7857 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7859 if (warn_cxx_compat)
7860 warn_cxx_compat_finish_struct (fieldlist);
7862 struct_parse_info->struct_types.release ();
7863 struct_parse_info->fields.release ();
7864 struct_parse_info->typedefs_seen.release ();
7865 XDELETE (struct_parse_info);
7867 struct_parse_info = enclosing_struct_parse_info;
7869 /* If this struct is defined inside a struct, add it to
7870 struct_types. */
7871 if (warn_cxx_compat
7872 && struct_parse_info != NULL
7873 && !in_sizeof && !in_typeof && !in_alignof)
7874 struct_parse_info->struct_types.safe_push (t);
7876 return t;
7879 /* Lay out the type T, and its element type, and so on. */
7881 static void
7882 layout_array_type (tree t)
7884 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7885 layout_array_type (TREE_TYPE (t));
7886 layout_type (t);
7889 /* Begin compiling the definition of an enumeration type.
7890 NAME is its name (or null if anonymous).
7891 LOC is the enum's location.
7892 Returns the type object, as yet incomplete.
7893 Also records info about it so that build_enumerator
7894 may be used to declare the individual values as they are read. */
7896 tree
7897 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7899 tree enumtype = NULL_TREE;
7900 location_t enumloc = UNKNOWN_LOCATION;
7902 /* If this is the real definition for a previous forward reference,
7903 fill in the contents in the same object that used to be the
7904 forward reference. */
7906 if (name != NULL_TREE)
7907 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7909 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7911 enumtype = make_node (ENUMERAL_TYPE);
7912 pushtag (loc, name, enumtype);
7915 if (C_TYPE_BEING_DEFINED (enumtype))
7916 error_at (loc, "nested redefinition of %<enum %E%>", name);
7918 C_TYPE_BEING_DEFINED (enumtype) = 1;
7920 if (TYPE_VALUES (enumtype) != 0)
7922 /* This enum is a named one that has been declared already. */
7923 error_at (loc, "redeclaration of %<enum %E%>", name);
7924 if (enumloc != UNKNOWN_LOCATION)
7925 inform (enumloc, "originally defined here");
7927 /* Completely replace its old definition.
7928 The old enumerators remain defined, however. */
7929 TYPE_VALUES (enumtype) = 0;
7932 the_enum->enum_next_value = integer_zero_node;
7933 the_enum->enum_overflow = 0;
7935 if (flag_short_enums)
7936 TYPE_PACKED (enumtype) = 1;
7938 /* FIXME: This will issue a warning for a use of a type defined
7939 within sizeof in a statement expr. This is not terribly serious
7940 as C++ doesn't permit statement exprs within sizeof anyhow. */
7941 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7942 warning_at (loc, OPT_Wc___compat,
7943 "defining type in %qs expression is invalid in C++",
7944 (in_sizeof
7945 ? "sizeof"
7946 : (in_typeof ? "typeof" : "alignof")));
7948 return enumtype;
7951 /* After processing and defining all the values of an enumeration type,
7952 install their decls in the enumeration type and finish it off.
7953 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7954 and ATTRIBUTES are the specified attributes.
7955 Returns ENUMTYPE. */
7957 tree
7958 finish_enum (tree enumtype, tree values, tree attributes)
7960 tree pair, tem;
7961 tree minnode = 0, maxnode = 0;
7962 int precision;
7963 signop sign;
7964 bool toplevel = (file_scope == current_scope);
7965 struct lang_type *lt;
7967 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7969 /* Calculate the maximum value of any enumerator in this type. */
7971 if (values == error_mark_node)
7972 minnode = maxnode = integer_zero_node;
7973 else
7975 minnode = maxnode = TREE_VALUE (values);
7976 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7978 tree value = TREE_VALUE (pair);
7979 if (tree_int_cst_lt (maxnode, value))
7980 maxnode = value;
7981 if (tree_int_cst_lt (value, minnode))
7982 minnode = value;
7986 /* Construct the final type of this enumeration. It is the same
7987 as one of the integral types - the narrowest one that fits, except
7988 that normally we only go as narrow as int - and signed iff any of
7989 the values are negative. */
7990 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
7991 precision = MAX (tree_int_cst_min_precision (minnode, sign),
7992 tree_int_cst_min_precision (maxnode, sign));
7994 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7996 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
7997 if (tem == NULL)
7999 warning (0, "enumeration values exceed range of largest integer");
8000 tem = long_long_integer_type_node;
8003 else
8004 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8006 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8007 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8008 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8009 TYPE_ALIGN (enumtype) = TYPE_ALIGN (tem);
8010 TYPE_SIZE (enumtype) = 0;
8012 /* If the precision of the type was specified with an attribute and it
8013 was too small, give an error. Otherwise, use it. */
8014 if (TYPE_PRECISION (enumtype)
8015 && lookup_attribute ("mode", attributes))
8017 if (precision > TYPE_PRECISION (enumtype))
8018 error ("specified mode too small for enumeral values");
8020 else
8021 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8023 layout_type (enumtype);
8025 if (values != error_mark_node)
8027 /* Change the type of the enumerators to be the enum type. We
8028 need to do this irrespective of the size of the enum, for
8029 proper type checking. Replace the DECL_INITIALs of the
8030 enumerators, and the value slots of the list, with copies
8031 that have the enum type; they cannot be modified in place
8032 because they may be shared (e.g. integer_zero_node) Finally,
8033 change the purpose slots to point to the names of the decls. */
8034 for (pair = values; pair; pair = TREE_CHAIN (pair))
8036 tree enu = TREE_PURPOSE (pair);
8037 tree ini = DECL_INITIAL (enu);
8039 TREE_TYPE (enu) = enumtype;
8041 /* The ISO C Standard mandates enumerators to have type int,
8042 even though the underlying type of an enum type is
8043 unspecified. However, GCC allows enumerators of any
8044 integer type as an extensions. build_enumerator()
8045 converts any enumerators that fit in an int to type int,
8046 to avoid promotions to unsigned types when comparing
8047 integers with enumerators that fit in the int range.
8048 When -pedantic is given, build_enumerator() would have
8049 already warned about those that don't fit. Here we
8050 convert the rest to the enumerator type. */
8051 if (TREE_TYPE (ini) != integer_type_node)
8052 ini = convert (enumtype, ini);
8054 DECL_INITIAL (enu) = ini;
8055 TREE_PURPOSE (pair) = DECL_NAME (enu);
8056 TREE_VALUE (pair) = ini;
8059 TYPE_VALUES (enumtype) = values;
8062 /* Record the min/max values so that we can warn about bit-field
8063 enumerations that are too small for the values. */
8064 lt = ggc_cleared_alloc<struct lang_type> ();
8065 lt->enum_min = minnode;
8066 lt->enum_max = maxnode;
8067 TYPE_LANG_SPECIFIC (enumtype) = lt;
8069 /* Fix up all variant types of this enum type. */
8070 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8072 if (tem == enumtype)
8073 continue;
8074 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8075 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8076 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8077 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8078 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8079 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8080 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8081 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
8082 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8083 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8084 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8087 /* Finish debugging output for this type. */
8088 rest_of_type_compilation (enumtype, toplevel);
8090 /* If this enum is defined inside a struct, add it to
8091 struct_types. */
8092 if (warn_cxx_compat
8093 && struct_parse_info != NULL
8094 && !in_sizeof && !in_typeof && !in_alignof)
8095 struct_parse_info->struct_types.safe_push (enumtype);
8097 return enumtype;
8100 /* Build and install a CONST_DECL for one value of the
8101 current enumeration type (one that was begun with start_enum).
8102 DECL_LOC is the location of the enumerator.
8103 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8104 Return a tree-list containing the CONST_DECL and its value.
8105 Assignment of sequential values by default is handled here. */
8107 tree
8108 build_enumerator (location_t decl_loc, location_t loc,
8109 struct c_enum_contents *the_enum, tree name, tree value)
8111 tree decl, type;
8113 /* Validate and default VALUE. */
8115 if (value != 0)
8117 /* Don't issue more errors for error_mark_node (i.e. an
8118 undeclared identifier) - just ignore the value expression. */
8119 if (value == error_mark_node)
8120 value = 0;
8121 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8123 error_at (loc, "enumerator value for %qE is not an integer constant",
8124 name);
8125 value = 0;
8127 else
8129 if (TREE_CODE (value) != INTEGER_CST)
8131 value = c_fully_fold (value, false, NULL);
8132 if (TREE_CODE (value) == INTEGER_CST)
8133 pedwarn (loc, OPT_Wpedantic,
8134 "enumerator value for %qE is not an integer "
8135 "constant expression", name);
8137 if (TREE_CODE (value) != INTEGER_CST)
8139 error ("enumerator value for %qE is not an integer constant",
8140 name);
8141 value = 0;
8143 else
8145 value = default_conversion (value);
8146 constant_expression_warning (value);
8151 /* Default based on previous value. */
8152 /* It should no longer be possible to have NON_LVALUE_EXPR
8153 in the default. */
8154 if (value == 0)
8156 value = the_enum->enum_next_value;
8157 if (the_enum->enum_overflow)
8158 error_at (loc, "overflow in enumeration values");
8160 /* Even though the underlying type of an enum is unspecified, the
8161 type of enumeration constants is explicitly defined as int
8162 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8163 an extension. */
8164 else if (!int_fits_type_p (value, integer_type_node))
8165 pedwarn (loc, OPT_Wpedantic,
8166 "ISO C restricts enumerator values to range of %<int%>");
8168 /* The ISO C Standard mandates enumerators to have type int, even
8169 though the underlying type of an enum type is unspecified.
8170 However, GCC allows enumerators of any integer type as an
8171 extensions. Here we convert any enumerators that fit in an int
8172 to type int, to avoid promotions to unsigned types when comparing
8173 integers with enumerators that fit in the int range. When
8174 -pedantic is given, we would have already warned about those that
8175 don't fit. We have to do this here rather than in finish_enum
8176 because this value may be used to define more enumerators. */
8177 if (int_fits_type_p (value, integer_type_node))
8178 value = convert (integer_type_node, value);
8180 /* Set basis for default for next value. */
8181 the_enum->enum_next_value
8182 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8183 PLUS_EXPR, value, integer_one_node, 0);
8184 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8186 /* Now create a declaration for the enum value name. */
8188 type = TREE_TYPE (value);
8189 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8190 TYPE_PRECISION (integer_type_node)),
8191 (TYPE_PRECISION (type)
8192 >= TYPE_PRECISION (integer_type_node)
8193 && TYPE_UNSIGNED (type)));
8195 decl = build_decl (decl_loc, CONST_DECL, name, type);
8196 DECL_INITIAL (decl) = convert (type, value);
8197 pushdecl (decl);
8199 return tree_cons (decl, value, NULL_TREE);
8203 /* Create the FUNCTION_DECL for a function definition.
8204 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8205 the declaration; they describe the function's name and the type it returns,
8206 but twisted together in a fashion that parallels the syntax of C.
8208 This function creates a binding context for the function body
8209 as well as setting up the FUNCTION_DECL in current_function_decl.
8211 Returns 1 on success. If the DECLARATOR is not suitable for a function
8212 (it defines a datum instead), we return 0, which tells
8213 yyparse to report a parse error. */
8216 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8217 tree attributes)
8219 tree decl1, old_decl;
8220 tree restype, resdecl;
8221 location_t loc;
8223 current_function_returns_value = 0; /* Assume, until we see it does. */
8224 current_function_returns_null = 0;
8225 current_function_returns_abnormally = 0;
8226 warn_about_return_type = 0;
8227 c_switch_stack = NULL;
8229 /* Indicate no valid break/continue context by setting these variables
8230 to some non-null, non-label value. We'll notice and emit the proper
8231 error message in c_finish_bc_stmt. */
8232 c_break_label = c_cont_label = size_zero_node;
8234 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8235 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8237 /* If the declarator is not suitable for a function definition,
8238 cause a syntax error. */
8239 if (decl1 == 0
8240 || TREE_CODE (decl1) != FUNCTION_DECL)
8241 return 0;
8243 loc = DECL_SOURCE_LOCATION (decl1);
8245 c_decl_attributes (&decl1, attributes, 0);
8247 if (DECL_DECLARED_INLINE_P (decl1)
8248 && DECL_UNINLINABLE (decl1)
8249 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8250 warning_at (loc, OPT_Wattributes,
8251 "inline function %qD given attribute noinline",
8252 decl1);
8254 /* Handle gnu_inline attribute. */
8255 if (declspecs->inline_p
8256 && !flag_gnu89_inline
8257 && TREE_CODE (decl1) == FUNCTION_DECL
8258 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8259 || current_function_decl))
8261 if (declspecs->storage_class != csc_static)
8262 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8265 announce_function (decl1);
8267 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8269 error_at (loc, "return type is an incomplete type");
8270 /* Make it return void instead. */
8271 TREE_TYPE (decl1)
8272 = build_function_type (void_type_node,
8273 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8276 if (warn_about_return_type)
8277 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8278 : (warn_return_type ? OPT_Wreturn_type
8279 : OPT_Wimplicit_int),
8280 "return type defaults to %<int%>");
8282 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8283 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8284 DECL_INITIAL (decl1) = error_mark_node;
8286 /* A nested function is not global. */
8287 if (current_function_decl != 0)
8288 TREE_PUBLIC (decl1) = 0;
8290 /* If this definition isn't a prototype and we had a prototype declaration
8291 before, copy the arg type info from that prototype. */
8292 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8293 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8294 old_decl = 0;
8295 current_function_prototype_locus = UNKNOWN_LOCATION;
8296 current_function_prototype_built_in = false;
8297 current_function_prototype_arg_types = NULL_TREE;
8298 if (!prototype_p (TREE_TYPE (decl1)))
8300 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8301 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8302 TREE_TYPE (TREE_TYPE (old_decl))))
8304 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8305 TREE_TYPE (decl1));
8306 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8307 current_function_prototype_built_in
8308 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8309 current_function_prototype_arg_types
8310 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8312 if (TREE_PUBLIC (decl1))
8314 /* If there is an external prototype declaration of this
8315 function, record its location but do not copy information
8316 to this decl. This may be an invisible declaration
8317 (built-in or in a scope which has finished) or simply
8318 have more refined argument types than any declaration
8319 found above. */
8320 struct c_binding *b;
8321 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8322 if (B_IN_SCOPE (b, external_scope))
8323 break;
8324 if (b)
8326 tree ext_decl, ext_type;
8327 ext_decl = b->decl;
8328 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8329 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8330 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8331 TREE_TYPE (ext_type)))
8333 current_function_prototype_locus
8334 = DECL_SOURCE_LOCATION (ext_decl);
8335 current_function_prototype_built_in
8336 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8337 current_function_prototype_arg_types
8338 = TYPE_ARG_TYPES (ext_type);
8344 /* Optionally warn of old-fashioned def with no previous prototype. */
8345 if (warn_strict_prototypes
8346 && old_decl != error_mark_node
8347 && !prototype_p (TREE_TYPE (decl1))
8348 && C_DECL_ISNT_PROTOTYPE (old_decl))
8349 warning_at (loc, OPT_Wstrict_prototypes,
8350 "function declaration isn%'t a prototype");
8351 /* Optionally warn of any global def with no previous prototype. */
8352 else if (warn_missing_prototypes
8353 && old_decl != error_mark_node
8354 && TREE_PUBLIC (decl1)
8355 && !MAIN_NAME_P (DECL_NAME (decl1))
8356 && C_DECL_ISNT_PROTOTYPE (old_decl)
8357 && !DECL_DECLARED_INLINE_P (decl1))
8358 warning_at (loc, OPT_Wmissing_prototypes,
8359 "no previous prototype for %qD", decl1);
8360 /* Optionally warn of any def with no previous prototype
8361 if the function has already been used. */
8362 else if (warn_missing_prototypes
8363 && old_decl != 0
8364 && old_decl != error_mark_node
8365 && TREE_USED (old_decl)
8366 && !prototype_p (TREE_TYPE (old_decl)))
8367 warning_at (loc, OPT_Wmissing_prototypes,
8368 "%qD was used with no prototype before its definition", decl1);
8369 /* Optionally warn of any global def with no previous declaration. */
8370 else if (warn_missing_declarations
8371 && TREE_PUBLIC (decl1)
8372 && old_decl == 0
8373 && !MAIN_NAME_P (DECL_NAME (decl1))
8374 && !DECL_DECLARED_INLINE_P (decl1))
8375 warning_at (loc, OPT_Wmissing_declarations,
8376 "no previous declaration for %qD",
8377 decl1);
8378 /* Optionally warn of any def with no previous declaration
8379 if the function has already been used. */
8380 else if (warn_missing_declarations
8381 && old_decl != 0
8382 && old_decl != error_mark_node
8383 && TREE_USED (old_decl)
8384 && C_DECL_IMPLICIT (old_decl))
8385 warning_at (loc, OPT_Wmissing_declarations,
8386 "%qD was used with no declaration before its definition", decl1);
8388 /* This function exists in static storage.
8389 (This does not mean `static' in the C sense!) */
8390 TREE_STATIC (decl1) = 1;
8392 /* This is the earliest point at which we might know the assembler
8393 name of the function. Thus, if it's set before this, die horribly. */
8394 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8396 /* If #pragma weak was used, mark the decl weak now. */
8397 if (current_scope == file_scope)
8398 maybe_apply_pragma_weak (decl1);
8400 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8401 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8403 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8404 != integer_type_node)
8405 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8406 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8407 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8408 decl1);
8410 check_main_parameter_types (decl1);
8412 if (!TREE_PUBLIC (decl1))
8413 pedwarn (loc, OPT_Wmain,
8414 "%qD is normally a non-static function", decl1);
8417 /* Record the decl so that the function name is defined.
8418 If we already have a decl for this name, and it is a FUNCTION_DECL,
8419 use the old decl. */
8421 current_function_decl = pushdecl (decl1);
8423 push_scope ();
8424 declare_parm_level ();
8426 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8427 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8428 DECL_ARTIFICIAL (resdecl) = 1;
8429 DECL_IGNORED_P (resdecl) = 1;
8430 DECL_RESULT (current_function_decl) = resdecl;
8432 start_fname_decls ();
8434 return 1;
8437 /* Subroutine of store_parm_decls which handles new-style function
8438 definitions (prototype format). The parms already have decls, so we
8439 need only record them as in effect and complain if any redundant
8440 old-style parm decls were written. */
8441 static void
8442 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8444 tree decl;
8445 c_arg_tag *tag;
8446 unsigned ix;
8448 if (current_scope->bindings)
8450 error_at (DECL_SOURCE_LOCATION (fndecl),
8451 "old-style parameter declarations in prototyped "
8452 "function definition");
8454 /* Get rid of the old-style declarations. */
8455 pop_scope ();
8456 push_scope ();
8458 /* Don't issue this warning for nested functions, and don't issue this
8459 warning if we got here because ARG_INFO_TYPES was error_mark_node
8460 (this happens when a function definition has just an ellipsis in
8461 its parameter list). */
8462 else if (!in_system_header_at (input_location)
8463 && !current_function_scope
8464 && arg_info->types != error_mark_node)
8465 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8466 "traditional C rejects ISO C style function definitions");
8468 /* Now make all the parameter declarations visible in the function body.
8469 We can bypass most of the grunt work of pushdecl. */
8470 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8472 DECL_CONTEXT (decl) = current_function_decl;
8473 if (DECL_NAME (decl))
8475 bind (DECL_NAME (decl), decl, current_scope,
8476 /*invisible=*/false, /*nested=*/false,
8477 UNKNOWN_LOCATION);
8478 if (!TREE_USED (decl))
8479 warn_if_shadowing (decl);
8481 else
8482 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8485 /* Record the parameter list in the function declaration. */
8486 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8488 /* Now make all the ancillary declarations visible, likewise. */
8489 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8491 DECL_CONTEXT (decl) = current_function_decl;
8492 if (DECL_NAME (decl))
8493 bind (DECL_NAME (decl), decl, current_scope,
8494 /*invisible=*/false,
8495 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8496 UNKNOWN_LOCATION);
8499 /* And all the tag declarations. */
8500 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8501 if (tag->id)
8502 bind (tag->id, tag->type, current_scope,
8503 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8506 /* Subroutine of store_parm_decls which handles old-style function
8507 definitions (separate parameter list and declarations). */
8509 static void
8510 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8512 struct c_binding *b;
8513 tree parm, decl, last;
8514 tree parmids = arg_info->parms;
8515 hash_set<tree> seen_args;
8517 if (!in_system_header_at (input_location))
8518 warning_at (DECL_SOURCE_LOCATION (fndecl),
8519 OPT_Wold_style_definition, "old-style function definition");
8521 /* Match each formal parameter name with its declaration. Save each
8522 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8523 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8525 if (TREE_VALUE (parm) == 0)
8527 error_at (DECL_SOURCE_LOCATION (fndecl),
8528 "parameter name missing from parameter list");
8529 TREE_PURPOSE (parm) = 0;
8530 continue;
8533 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8534 if (b && B_IN_CURRENT_SCOPE (b))
8536 decl = b->decl;
8537 /* Skip erroneous parameters. */
8538 if (decl == error_mark_node)
8539 continue;
8540 /* If we got something other than a PARM_DECL it is an error. */
8541 if (TREE_CODE (decl) != PARM_DECL)
8542 error_at (DECL_SOURCE_LOCATION (decl),
8543 "%qD declared as a non-parameter", decl);
8544 /* If the declaration is already marked, we have a duplicate
8545 name. Complain and ignore the duplicate. */
8546 else if (seen_args.contains (decl))
8548 error_at (DECL_SOURCE_LOCATION (decl),
8549 "multiple parameters named %qD", decl);
8550 TREE_PURPOSE (parm) = 0;
8551 continue;
8553 /* If the declaration says "void", complain and turn it into
8554 an int. */
8555 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8557 error_at (DECL_SOURCE_LOCATION (decl),
8558 "parameter %qD declared with void type", decl);
8559 TREE_TYPE (decl) = integer_type_node;
8560 DECL_ARG_TYPE (decl) = integer_type_node;
8561 layout_decl (decl, 0);
8563 warn_if_shadowing (decl);
8565 /* If no declaration found, default to int. */
8566 else
8568 /* FIXME diagnostics: This should be the location of the argument,
8569 not the FNDECL. E.g., for an old-style declaration
8571 int f10(v) { blah; }
8573 We should use the location of the V, not the F10.
8574 Unfortunately, the V is an IDENTIFIER_NODE which has no
8575 location. In the future we need locations for c_arg_info
8576 entries.
8578 See gcc.dg/Wshadow-3.c for an example of this problem. */
8579 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8580 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8581 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8582 pushdecl (decl);
8583 warn_if_shadowing (decl);
8585 if (flag_isoc99)
8586 pedwarn (DECL_SOURCE_LOCATION (decl),
8587 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8588 decl);
8589 else
8590 warning_at (DECL_SOURCE_LOCATION (decl),
8591 OPT_Wmissing_parameter_type,
8592 "type of %qD defaults to %<int%>", decl);
8595 TREE_PURPOSE (parm) = decl;
8596 seen_args.add (decl);
8599 /* Now examine the parms chain for incomplete declarations
8600 and declarations with no corresponding names. */
8602 for (b = current_scope->bindings; b; b = b->prev)
8604 parm = b->decl;
8605 if (TREE_CODE (parm) != PARM_DECL)
8606 continue;
8608 if (TREE_TYPE (parm) != error_mark_node
8609 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8611 error_at (DECL_SOURCE_LOCATION (parm),
8612 "parameter %qD has incomplete type", parm);
8613 TREE_TYPE (parm) = error_mark_node;
8616 if (!seen_args.contains (parm))
8618 error_at (DECL_SOURCE_LOCATION (parm),
8619 "declaration for parameter %qD but no such parameter",
8620 parm);
8622 /* Pretend the parameter was not missing.
8623 This gets us to a standard state and minimizes
8624 further error messages. */
8625 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8629 /* Chain the declarations together in the order of the list of
8630 names. Store that chain in the function decl, replacing the
8631 list of names. Update the current scope to match. */
8632 DECL_ARGUMENTS (fndecl) = 0;
8634 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8635 if (TREE_PURPOSE (parm))
8636 break;
8637 if (parm && TREE_PURPOSE (parm))
8639 last = TREE_PURPOSE (parm);
8640 DECL_ARGUMENTS (fndecl) = last;
8642 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8643 if (TREE_PURPOSE (parm))
8645 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8646 last = TREE_PURPOSE (parm);
8648 DECL_CHAIN (last) = 0;
8651 /* If there was a previous prototype,
8652 set the DECL_ARG_TYPE of each argument according to
8653 the type previously specified, and report any mismatches. */
8655 if (current_function_prototype_arg_types)
8657 tree type;
8658 for (parm = DECL_ARGUMENTS (fndecl),
8659 type = current_function_prototype_arg_types;
8660 parm || (type && TREE_VALUE (type) != error_mark_node
8661 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8662 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8664 if (parm == 0 || type == 0
8665 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8667 if (current_function_prototype_built_in)
8668 warning_at (DECL_SOURCE_LOCATION (fndecl),
8669 0, "number of arguments doesn%'t match "
8670 "built-in prototype");
8671 else
8673 /* FIXME diagnostics: This should be the location of
8674 FNDECL, but there is bug when a prototype is
8675 declared inside function context, but defined
8676 outside of it (e.g., gcc.dg/pr15698-2.c). In
8677 which case FNDECL gets the location of the
8678 prototype, not the definition. */
8679 error_at (input_location,
8680 "number of arguments doesn%'t match prototype");
8682 error_at (current_function_prototype_locus,
8683 "prototype declaration");
8685 break;
8687 /* Type for passing arg must be consistent with that
8688 declared for the arg. ISO C says we take the unqualified
8689 type for parameters declared with qualified type. */
8690 if (TREE_TYPE (parm) != error_mark_node
8691 && TREE_TYPE (type) != error_mark_node
8692 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8693 != TYPE_ATOMIC (TREE_VALUE (type)))
8694 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8695 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8697 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8698 == TYPE_ATOMIC (TREE_VALUE (type)))
8699 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8700 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8702 /* Adjust argument to match prototype. E.g. a previous
8703 `int foo(float);' prototype causes
8704 `int foo(x) float x; {...}' to be treated like
8705 `int foo(float x) {...}'. This is particularly
8706 useful for argument types like uid_t. */
8707 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8709 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8710 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8711 && TYPE_PRECISION (TREE_TYPE (parm))
8712 < TYPE_PRECISION (integer_type_node))
8713 DECL_ARG_TYPE (parm)
8714 = c_type_promotes_to (TREE_TYPE (parm));
8716 /* ??? Is it possible to get here with a
8717 built-in prototype or will it always have
8718 been diagnosed as conflicting with an
8719 old-style definition and discarded? */
8720 if (current_function_prototype_built_in)
8721 warning_at (DECL_SOURCE_LOCATION (parm),
8722 OPT_Wpedantic, "promoted argument %qD "
8723 "doesn%'t match built-in prototype", parm);
8724 else
8726 pedwarn (DECL_SOURCE_LOCATION (parm),
8727 OPT_Wpedantic, "promoted argument %qD "
8728 "doesn%'t match prototype", parm);
8729 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8730 "prototype declaration");
8733 else
8735 if (current_function_prototype_built_in)
8736 warning_at (DECL_SOURCE_LOCATION (parm),
8737 0, "argument %qD doesn%'t match "
8738 "built-in prototype", parm);
8739 else
8741 error_at (DECL_SOURCE_LOCATION (parm),
8742 "argument %qD doesn%'t match prototype", parm);
8743 error_at (current_function_prototype_locus,
8744 "prototype declaration");
8749 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8752 /* Otherwise, create a prototype that would match. */
8754 else
8756 tree actual = 0, last = 0, type;
8758 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8760 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8761 if (last)
8762 TREE_CHAIN (last) = type;
8763 else
8764 actual = type;
8765 last = type;
8767 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8768 if (last)
8769 TREE_CHAIN (last) = type;
8770 else
8771 actual = type;
8773 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8774 of the type of this function, but we need to avoid having this
8775 affect the types of other similarly-typed functions, so we must
8776 first force the generation of an identical (but separate) type
8777 node for the relevant function type. The new node we create
8778 will be a variant of the main variant of the original function
8779 type. */
8781 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8783 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8787 /* Store parameter declarations passed in ARG_INFO into the current
8788 function declaration. */
8790 void
8791 store_parm_decls_from (struct c_arg_info *arg_info)
8793 current_function_arg_info = arg_info;
8794 store_parm_decls ();
8797 /* Called by walk_tree to look for and update context-less labels. */
8799 static tree
8800 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
8802 if (TREE_CODE (*tp) == LABEL_EXPR
8803 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
8805 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
8806 *walk_subtrees = 0;
8809 return NULL_TREE;
8812 /* Store the parameter declarations into the current function declaration.
8813 This is called after parsing the parameter declarations, before
8814 digesting the body of the function.
8816 For an old-style definition, construct a prototype out of the old-style
8817 parameter declarations and inject it into the function's type. */
8819 void
8820 store_parm_decls (void)
8822 tree fndecl = current_function_decl;
8823 bool proto;
8825 /* The argument information block for FNDECL. */
8826 struct c_arg_info *arg_info = current_function_arg_info;
8827 current_function_arg_info = 0;
8829 /* True if this definition is written with a prototype. Note:
8830 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8831 list in a function definition as equivalent to (void) -- an
8832 empty argument list specifies the function has no parameters,
8833 but only (void) sets up a prototype for future calls. */
8834 proto = arg_info->types != 0;
8836 if (proto)
8837 store_parm_decls_newstyle (fndecl, arg_info);
8838 else
8839 store_parm_decls_oldstyle (fndecl, arg_info);
8841 /* The next call to push_scope will be a function body. */
8843 next_is_function_body = true;
8845 /* Write a record describing this function definition to the prototypes
8846 file (if requested). */
8848 gen_aux_info_record (fndecl, 1, 0, proto);
8850 /* Initialize the RTL code for the function. */
8851 allocate_struct_function (fndecl, false);
8853 if (warn_unused_local_typedefs)
8854 cfun->language = ggc_cleared_alloc<language_function> ();
8856 /* Begin the statement tree for this function. */
8857 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8859 /* ??? Insert the contents of the pending sizes list into the function
8860 to be evaluated. The only reason left to have this is
8861 void foo(int n, int array[n++])
8862 because we throw away the array type in favor of a pointer type, and
8863 thus won't naturally see the SAVE_EXPR containing the increment. All
8864 other pending sizes would be handled by gimplify_parameters. */
8865 if (arg_info->pending_sizes)
8867 /* In very special circumstances, e.g. for code like
8868 _Atomic int i = 5;
8869 void f (int a[i += 2]) {}
8870 we need to execute the atomic assignment on function entry.
8871 But in this case, it is not just a straight store, it has the
8872 op= form, which means that build_atomic_assign has generated
8873 gotos, labels, etc. Because at that time the function decl
8874 for F has not been created yet, those labels do not have any
8875 function context. But we have the fndecl now, so update the
8876 labels accordingly. gimplify_expr would crash otherwise. */
8877 walk_tree_without_duplicates (&arg_info->pending_sizes,
8878 set_labels_context_r, fndecl);
8879 add_stmt (arg_info->pending_sizes);
8883 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8884 c_finish_omp_declare_simd for function prototypes. No diagnostics
8885 should be done. */
8887 void
8888 temp_store_parm_decls (tree fndecl, tree parms)
8890 push_scope ();
8891 for (tree p = parms; p; p = DECL_CHAIN (p))
8893 DECL_CONTEXT (p) = fndecl;
8894 if (DECL_NAME (p))
8895 bind (DECL_NAME (p), p, current_scope,
8896 /*invisible=*/false, /*nested=*/false,
8897 UNKNOWN_LOCATION);
8901 /* Undo what temp_store_parm_decls did. */
8903 void
8904 temp_pop_parm_decls (void)
8906 /* Clear all bindings in this temporary scope, so that
8907 pop_scope doesn't create a BLOCK. */
8908 struct c_binding *b = current_scope->bindings;
8909 current_scope->bindings = NULL;
8910 for (; b; b = free_binding_and_advance (b))
8912 gcc_assert (TREE_CODE (b->decl) == PARM_DECL);
8913 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8914 I_SYMBOL_BINDING (b->id) = b->shadowed;
8915 if (b->shadowed && b->shadowed->u.type)
8916 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
8918 pop_scope ();
8922 /* Finish up a function declaration and compile that function
8923 all the way to assembler language output. Then free the storage
8924 for the function definition.
8926 This is called after parsing the body of the function definition. */
8928 void
8929 finish_function (void)
8931 tree fndecl = current_function_decl;
8933 if (c_dialect_objc ())
8934 objc_finish_function ();
8936 if (TREE_CODE (fndecl) == FUNCTION_DECL
8937 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8939 tree args = DECL_ARGUMENTS (fndecl);
8940 for (; args; args = DECL_CHAIN (args))
8942 tree type = TREE_TYPE (args);
8943 if (INTEGRAL_TYPE_P (type)
8944 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8945 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
8949 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8950 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8952 /* Must mark the RESULT_DECL as being in this function. */
8954 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8955 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8957 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8958 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8959 == integer_type_node && flag_isoc99)
8961 /* Hack. We don't want the middle-end to warn that this return
8962 is unreachable, so we mark its location as special. Using
8963 UNKNOWN_LOCATION has the problem that it gets clobbered in
8964 annotate_one_with_locus. A cleaner solution might be to
8965 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8967 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8970 /* Tie off the statement tree for this function. */
8971 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8973 /* If the function has _Cilk_spawn in front of a function call inside it
8974 i.e. it is a spawning function, then add the appropriate Cilk plus
8975 functions inside. */
8976 if (fn_contains_cilk_spawn_p (cfun))
8977 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
8979 finish_fname_decls ();
8981 /* Complain if there's just no return statement. */
8982 if (warn_return_type
8983 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8984 && !current_function_returns_value && !current_function_returns_null
8985 /* Don't complain if we are no-return. */
8986 && !current_function_returns_abnormally
8987 /* Don't complain if we are declared noreturn. */
8988 && !TREE_THIS_VOLATILE (fndecl)
8989 /* Don't warn for main(). */
8990 && !MAIN_NAME_P (DECL_NAME (fndecl))
8991 /* Or if they didn't actually specify a return type. */
8992 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8993 /* Normally, with -Wreturn-type, flow will complain, but we might
8994 optimize out static functions. */
8995 && !TREE_PUBLIC (fndecl))
8997 warning (OPT_Wreturn_type,
8998 "no return statement in function returning non-void");
8999 TREE_NO_WARNING (fndecl) = 1;
9002 /* Complain about parameters that are only set, but never otherwise used. */
9003 if (warn_unused_but_set_parameter)
9005 tree decl;
9007 for (decl = DECL_ARGUMENTS (fndecl);
9008 decl;
9009 decl = DECL_CHAIN (decl))
9010 if (TREE_USED (decl)
9011 && TREE_CODE (decl) == PARM_DECL
9012 && !DECL_READ_P (decl)
9013 && DECL_NAME (decl)
9014 && !DECL_ARTIFICIAL (decl)
9015 && !TREE_NO_WARNING (decl))
9016 warning_at (DECL_SOURCE_LOCATION (decl),
9017 OPT_Wunused_but_set_parameter,
9018 "parameter %qD set but not used", decl);
9021 /* Complain about locally defined typedefs that are not used in this
9022 function. */
9023 maybe_warn_unused_local_typedefs ();
9025 /* Store the end of the function, so that we get good line number
9026 info for the epilogue. */
9027 cfun->function_end_locus = input_location;
9029 /* Finalize the ELF visibility for the function. */
9030 c_determine_visibility (fndecl);
9032 /* For GNU C extern inline functions disregard inline limits. */
9033 if (DECL_EXTERNAL (fndecl)
9034 && DECL_DECLARED_INLINE_P (fndecl))
9035 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9037 /* Genericize before inlining. Delay genericizing nested functions
9038 until their parent function is genericized. Since finalizing
9039 requires GENERIC, delay that as well. */
9041 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9042 && !undef_nested_function)
9044 if (!decl_function_context (fndecl))
9046 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9047 c_genericize (fndecl);
9049 /* ??? Objc emits functions after finalizing the compilation unit.
9050 This should be cleaned up later and this conditional removed. */
9051 if (symtab->global_info_ready)
9053 cgraph_node::add_new_function (fndecl, false);
9054 return;
9056 cgraph_node::finalize_function (fndecl, false);
9058 else
9060 /* Register this function with cgraph just far enough to get it
9061 added to our parent's nested function list. Handy, since the
9062 C front end doesn't have such a list. */
9063 (void) cgraph_node::get_create (fndecl);
9067 if (!decl_function_context (fndecl))
9068 undef_nested_function = false;
9070 if (cfun->language != NULL)
9072 ggc_free (cfun->language);
9073 cfun->language = NULL;
9076 /* We're leaving the context of this function, so zap cfun.
9077 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9078 tree_rest_of_compilation. */
9079 set_cfun (NULL);
9080 current_function_decl = NULL;
9083 /* Check the declarations given in a for-loop for satisfying the C99
9084 constraints. If exactly one such decl is found, return it. LOC is
9085 the location of the opening parenthesis of the for loop. The last
9086 parameter allows you to control the "for loop initial declarations
9087 are only allowed in C99 mode". Normally, you should pass
9088 flag_isoc99 as that parameter. But in some cases (Objective-C
9089 foreach loop, for example) we want to run the checks in this
9090 function even if not in C99 mode, so we allow the caller to turn
9091 off the error about not being in C99 mode.
9094 tree
9095 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9097 struct c_binding *b;
9098 tree one_decl = NULL_TREE;
9099 int n_decls = 0;
9101 if (!turn_off_iso_c99_error)
9103 static bool hint = true;
9104 /* If we get here, declarations have been used in a for loop without
9105 the C99 for loop scope. This doesn't make much sense, so don't
9106 allow it. */
9107 error_at (loc, "%<for%> loop initial declarations "
9108 "are only allowed in C99 or C11 mode");
9109 if (hint)
9111 inform (loc,
9112 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9113 "to compile your code");
9114 hint = false;
9116 return NULL_TREE;
9118 /* C99 subclause 6.8.5 paragraph 3:
9120 [#3] The declaration part of a for statement shall only
9121 declare identifiers for objects having storage class auto or
9122 register.
9124 It isn't clear whether, in this sentence, "identifiers" binds to
9125 "shall only declare" or to "objects" - that is, whether all identifiers
9126 declared must be identifiers for objects, or whether the restriction
9127 only applies to those that are. (A question on this in comp.std.c
9128 in November 2000 received no answer.) We implement the strictest
9129 interpretation, to avoid creating an extension which later causes
9130 problems. */
9132 for (b = current_scope->bindings; b; b = b->prev)
9134 tree id = b->id;
9135 tree decl = b->decl;
9137 if (!id)
9138 continue;
9140 switch (TREE_CODE (decl))
9142 case VAR_DECL:
9144 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9145 if (TREE_STATIC (decl))
9146 error_at (decl_loc,
9147 "declaration of static variable %qD in %<for%> loop "
9148 "initial declaration", decl);
9149 else if (DECL_EXTERNAL (decl))
9150 error_at (decl_loc,
9151 "declaration of %<extern%> variable %qD in %<for%> loop "
9152 "initial declaration", decl);
9154 break;
9156 case RECORD_TYPE:
9157 error_at (loc,
9158 "%<struct %E%> declared in %<for%> loop initial "
9159 "declaration", id);
9160 break;
9161 case UNION_TYPE:
9162 error_at (loc,
9163 "%<union %E%> declared in %<for%> loop initial declaration",
9164 id);
9165 break;
9166 case ENUMERAL_TYPE:
9167 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9168 "initial declaration", id);
9169 break;
9170 default:
9171 error_at (loc, "declaration of non-variable "
9172 "%qD in %<for%> loop initial declaration", decl);
9175 n_decls++;
9176 one_decl = decl;
9179 return n_decls == 1 ? one_decl : NULL_TREE;
9182 /* Save and reinitialize the variables
9183 used during compilation of a C function. */
9185 void
9186 c_push_function_context (void)
9188 struct language_function *p = cfun->language;
9189 /* cfun->language might have been already allocated by the use of
9190 -Wunused-local-typedefs. In that case, just re-use it. */
9191 if (p == NULL)
9192 cfun->language = p = ggc_cleared_alloc<language_function> ();
9194 p->base.x_stmt_tree = c_stmt_tree;
9195 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9196 p->x_break_label = c_break_label;
9197 p->x_cont_label = c_cont_label;
9198 p->x_switch_stack = c_switch_stack;
9199 p->arg_info = current_function_arg_info;
9200 p->returns_value = current_function_returns_value;
9201 p->returns_null = current_function_returns_null;
9202 p->returns_abnormally = current_function_returns_abnormally;
9203 p->warn_about_return_type = warn_about_return_type;
9205 push_function_context ();
9208 /* Restore the variables used during compilation of a C function. */
9210 void
9211 c_pop_function_context (void)
9213 struct language_function *p;
9215 pop_function_context ();
9216 p = cfun->language;
9218 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9219 used to store data throughout the life time of the current cfun,
9220 So don't deallocate it. */
9221 if (!warn_unused_local_typedefs)
9222 cfun->language = NULL;
9224 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9225 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9227 /* Stop pointing to the local nodes about to be freed. */
9228 /* But DECL_INITIAL must remain nonzero so we know this
9229 was an actual function definition. */
9230 DECL_INITIAL (current_function_decl) = error_mark_node;
9231 DECL_ARGUMENTS (current_function_decl) = 0;
9234 c_stmt_tree = p->base.x_stmt_tree;
9235 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9236 c_break_label = p->x_break_label;
9237 c_cont_label = p->x_cont_label;
9238 c_switch_stack = p->x_switch_stack;
9239 current_function_arg_info = p->arg_info;
9240 current_function_returns_value = p->returns_value;
9241 current_function_returns_null = p->returns_null;
9242 current_function_returns_abnormally = p->returns_abnormally;
9243 warn_about_return_type = p->warn_about_return_type;
9246 /* The functions below are required for functionality of doing
9247 function at once processing in the C front end. Currently these
9248 functions are not called from anywhere in the C front end, but as
9249 these changes continue, that will change. */
9251 /* Returns the stmt_tree (if any) to which statements are currently
9252 being added. If there is no active statement-tree, NULL is
9253 returned. */
9255 stmt_tree
9256 current_stmt_tree (void)
9258 return &c_stmt_tree;
9261 /* Return the global value of T as a symbol. */
9263 tree
9264 identifier_global_value (tree t)
9266 struct c_binding *b;
9268 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9269 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9270 return b->decl;
9272 return 0;
9275 /* In C, the only C-linkage public declaration is at file scope. */
9277 tree
9278 c_linkage_bindings (tree name)
9280 return identifier_global_value (name);
9283 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9284 otherwise the name is found in ridpointers from RID_INDEX. */
9286 void
9287 record_builtin_type (enum rid rid_index, const char *name, tree type)
9289 tree id, decl;
9290 if (name == 0)
9291 id = ridpointers[(int) rid_index];
9292 else
9293 id = get_identifier (name);
9294 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9295 pushdecl (decl);
9296 if (debug_hooks->type_decl)
9297 debug_hooks->type_decl (decl, false);
9300 /* Build the void_list_node (void_type_node having been created). */
9301 tree
9302 build_void_list_node (void)
9304 tree t = build_tree_list (NULL_TREE, void_type_node);
9305 return t;
9308 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9310 struct c_parm *
9311 build_c_parm (struct c_declspecs *specs, tree attrs,
9312 struct c_declarator *declarator)
9314 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9315 ret->specs = specs;
9316 ret->attrs = attrs;
9317 ret->declarator = declarator;
9318 return ret;
9321 /* Return a declarator with nested attributes. TARGET is the inner
9322 declarator to which these attributes apply. ATTRS are the
9323 attributes. */
9325 struct c_declarator *
9326 build_attrs_declarator (tree attrs, struct c_declarator *target)
9328 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9329 ret->kind = cdk_attrs;
9330 ret->declarator = target;
9331 ret->u.attrs = attrs;
9332 return ret;
9335 /* Return a declarator for a function with arguments specified by ARGS
9336 and return type specified by TARGET. */
9338 struct c_declarator *
9339 build_function_declarator (struct c_arg_info *args,
9340 struct c_declarator *target)
9342 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9343 ret->kind = cdk_function;
9344 ret->declarator = target;
9345 ret->u.arg_info = args;
9346 return ret;
9349 /* Return a declarator for the identifier IDENT (which may be
9350 NULL_TREE for an abstract declarator). */
9352 struct c_declarator *
9353 build_id_declarator (tree ident)
9355 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9356 ret->kind = cdk_id;
9357 ret->declarator = 0;
9358 ret->u.id = ident;
9359 /* Default value - may get reset to a more precise location. */
9360 ret->id_loc = input_location;
9361 return ret;
9364 /* Return something to represent absolute declarators containing a *.
9365 TARGET is the absolute declarator that the * contains.
9366 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9367 to apply to the pointer type. */
9369 struct c_declarator *
9370 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9371 struct c_declarator *target)
9373 tree attrs;
9374 int quals = 0;
9375 struct c_declarator *itarget = target;
9376 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9377 if (type_quals_attrs)
9379 attrs = type_quals_attrs->attrs;
9380 quals = quals_from_declspecs (type_quals_attrs);
9381 if (attrs != NULL_TREE)
9382 itarget = build_attrs_declarator (attrs, target);
9384 ret->kind = cdk_pointer;
9385 ret->declarator = itarget;
9386 ret->u.pointer_quals = quals;
9387 return ret;
9390 /* Return a pointer to a structure for an empty list of declaration
9391 specifiers. */
9393 struct c_declspecs *
9394 build_null_declspecs (void)
9396 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9397 memset (&ret->locations, 0, cdw_number_of_elements);
9398 ret->type = 0;
9399 ret->expr = 0;
9400 ret->decl_attr = 0;
9401 ret->attrs = 0;
9402 ret->align_log = -1;
9403 ret->typespec_word = cts_none;
9404 ret->storage_class = csc_none;
9405 ret->expr_const_operands = true;
9406 ret->declspecs_seen_p = false;
9407 ret->typespec_kind = ctsk_none;
9408 ret->non_sc_seen_p = false;
9409 ret->typedef_p = false;
9410 ret->explicit_signed_p = false;
9411 ret->deprecated_p = false;
9412 ret->default_int_p = false;
9413 ret->long_p = false;
9414 ret->long_long_p = false;
9415 ret->short_p = false;
9416 ret->signed_p = false;
9417 ret->unsigned_p = false;
9418 ret->complex_p = false;
9419 ret->inline_p = false;
9420 ret->noreturn_p = false;
9421 ret->thread_p = false;
9422 ret->thread_gnu_p = false;
9423 ret->const_p = false;
9424 ret->volatile_p = false;
9425 ret->atomic_p = false;
9426 ret->restrict_p = false;
9427 ret->saturating_p = false;
9428 ret->alignas_p = false;
9429 ret->address_space = ADDR_SPACE_GENERIC;
9430 return ret;
9433 /* Add the address space ADDRSPACE to the declaration specifiers
9434 SPECS, returning SPECS. */
9436 struct c_declspecs *
9437 declspecs_add_addrspace (source_location location,
9438 struct c_declspecs *specs, addr_space_t as)
9440 specs->non_sc_seen_p = true;
9441 specs->declspecs_seen_p = true;
9443 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9444 && specs->address_space != as)
9445 error ("incompatible address space qualifiers %qs and %qs",
9446 c_addr_space_name (as),
9447 c_addr_space_name (specs->address_space));
9448 else
9450 specs->address_space = as;
9451 specs->locations[cdw_address_space] = location;
9453 return specs;
9456 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9457 returning SPECS. */
9459 struct c_declspecs *
9460 declspecs_add_qual (source_location loc,
9461 struct c_declspecs *specs, tree qual)
9463 enum rid i;
9464 bool dupe = false;
9465 specs->non_sc_seen_p = true;
9466 specs->declspecs_seen_p = true;
9467 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9468 && C_IS_RESERVED_WORD (qual));
9469 i = C_RID_CODE (qual);
9470 switch (i)
9472 case RID_CONST:
9473 dupe = specs->const_p;
9474 specs->const_p = true;
9475 specs->locations[cdw_const] = loc;
9476 break;
9477 case RID_VOLATILE:
9478 dupe = specs->volatile_p;
9479 specs->volatile_p = true;
9480 specs->locations[cdw_volatile] = loc;
9481 break;
9482 case RID_RESTRICT:
9483 dupe = specs->restrict_p;
9484 specs->restrict_p = true;
9485 specs->locations[cdw_restrict] = loc;
9486 break;
9487 case RID_ATOMIC:
9488 dupe = specs->atomic_p;
9489 specs->atomic_p = true;
9490 break;
9491 default:
9492 gcc_unreachable ();
9494 if (dupe)
9495 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9496 return specs;
9499 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9500 returning SPECS. */
9502 struct c_declspecs *
9503 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9504 struct c_typespec spec)
9506 tree type = spec.spec;
9507 specs->non_sc_seen_p = true;
9508 specs->declspecs_seen_p = true;
9509 specs->typespec_kind = spec.kind;
9510 if (TREE_DEPRECATED (type))
9511 specs->deprecated_p = true;
9513 /* Handle type specifier keywords. */
9514 if (TREE_CODE (type) == IDENTIFIER_NODE
9515 && C_IS_RESERVED_WORD (type)
9516 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9518 enum rid i = C_RID_CODE (type);
9519 if (specs->type)
9521 error_at (loc, "two or more data types in declaration specifiers");
9522 return specs;
9524 if ((int) i <= (int) RID_LAST_MODIFIER)
9526 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9527 bool dupe = false;
9528 switch (i)
9530 case RID_LONG:
9531 if (specs->long_long_p)
9533 error_at (loc, "%<long long long%> is too long for GCC");
9534 break;
9536 if (specs->long_p)
9538 if (specs->typespec_word == cts_double)
9540 error_at (loc,
9541 ("both %<long long%> and %<double%> in "
9542 "declaration specifiers"));
9543 break;
9545 pedwarn_c90 (loc, OPT_Wlong_long,
9546 "ISO C90 does not support %<long long%>");
9547 specs->long_long_p = 1;
9548 specs->locations[cdw_long_long] = loc;
9549 break;
9551 if (specs->short_p)
9552 error_at (loc,
9553 ("both %<long%> and %<short%> in "
9554 "declaration specifiers"));
9555 else if (specs->typespec_word == cts_auto_type)
9556 error_at (loc,
9557 ("both %<long%> and %<__auto_type%> in "
9558 "declaration specifiers"));
9559 else if (specs->typespec_word == cts_void)
9560 error_at (loc,
9561 ("both %<long%> and %<void%> in "
9562 "declaration specifiers"));
9563 else if (specs->typespec_word == cts_int_n)
9564 error_at (loc,
9565 ("both %<long%> and %<__int%d%> in "
9566 "declaration specifiers"),
9567 int_n_data[specs->int_n_idx].bitsize);
9568 else if (specs->typespec_word == cts_bool)
9569 error_at (loc,
9570 ("both %<long%> and %<_Bool%> in "
9571 "declaration specifiers"));
9572 else if (specs->typespec_word == cts_char)
9573 error_at (loc,
9574 ("both %<long%> and %<char%> in "
9575 "declaration specifiers"));
9576 else if (specs->typespec_word == cts_float)
9577 error_at (loc,
9578 ("both %<long%> and %<float%> in "
9579 "declaration specifiers"));
9580 else if (specs->typespec_word == cts_dfloat32)
9581 error_at (loc,
9582 ("both %<long%> and %<_Decimal32%> in "
9583 "declaration specifiers"));
9584 else if (specs->typespec_word == cts_dfloat64)
9585 error_at (loc,
9586 ("both %<long%> and %<_Decimal64%> in "
9587 "declaration specifiers"));
9588 else if (specs->typespec_word == cts_dfloat128)
9589 error_at (loc,
9590 ("both %<long%> and %<_Decimal128%> in "
9591 "declaration specifiers"));
9592 else
9594 specs->long_p = true;
9595 specs->locations[cdw_long] = loc;
9597 break;
9598 case RID_SHORT:
9599 dupe = specs->short_p;
9600 if (specs->long_p)
9601 error_at (loc,
9602 ("both %<long%> and %<short%> in "
9603 "declaration specifiers"));
9604 else if (specs->typespec_word == cts_auto_type)
9605 error_at (loc,
9606 ("both %<short%> and %<__auto_type%> in "
9607 "declaration specifiers"));
9608 else if (specs->typespec_word == cts_void)
9609 error_at (loc,
9610 ("both %<short%> and %<void%> in "
9611 "declaration specifiers"));
9612 else if (specs->typespec_word == cts_int_n)
9613 error_at (loc,
9614 ("both %<short%> and %<__int%d%> in "
9615 "declaration specifiers"),
9616 int_n_data[specs->int_n_idx].bitsize);
9617 else if (specs->typespec_word == cts_bool)
9618 error_at (loc,
9619 ("both %<short%> and %<_Bool%> in "
9620 "declaration specifiers"));
9621 else if (specs->typespec_word == cts_char)
9622 error_at (loc,
9623 ("both %<short%> and %<char%> in "
9624 "declaration specifiers"));
9625 else if (specs->typespec_word == cts_float)
9626 error_at (loc,
9627 ("both %<short%> and %<float%> in "
9628 "declaration specifiers"));
9629 else if (specs->typespec_word == cts_double)
9630 error_at (loc,
9631 ("both %<short%> and %<double%> in "
9632 "declaration specifiers"));
9633 else if (specs->typespec_word == cts_dfloat32)
9634 error_at (loc,
9635 ("both %<short%> and %<_Decimal32%> in "
9636 "declaration specifiers"));
9637 else if (specs->typespec_word == cts_dfloat64)
9638 error_at (loc,
9639 ("both %<short%> and %<_Decimal64%> in "
9640 "declaration specifiers"));
9641 else if (specs->typespec_word == cts_dfloat128)
9642 error_at (loc,
9643 ("both %<short%> and %<_Decimal128%> in "
9644 "declaration specifiers"));
9645 else
9647 specs->short_p = true;
9648 specs->locations[cdw_short] = loc;
9650 break;
9651 case RID_SIGNED:
9652 dupe = specs->signed_p;
9653 if (specs->unsigned_p)
9654 error_at (loc,
9655 ("both %<signed%> and %<unsigned%> in "
9656 "declaration specifiers"));
9657 else if (specs->typespec_word == cts_auto_type)
9658 error_at (loc,
9659 ("both %<signed%> and %<__auto_type%> in "
9660 "declaration specifiers"));
9661 else if (specs->typespec_word == cts_void)
9662 error_at (loc,
9663 ("both %<signed%> and %<void%> in "
9664 "declaration specifiers"));
9665 else if (specs->typespec_word == cts_bool)
9666 error_at (loc,
9667 ("both %<signed%> and %<_Bool%> in "
9668 "declaration specifiers"));
9669 else if (specs->typespec_word == cts_float)
9670 error_at (loc,
9671 ("both %<signed%> and %<float%> in "
9672 "declaration specifiers"));
9673 else if (specs->typespec_word == cts_double)
9674 error_at (loc,
9675 ("both %<signed%> and %<double%> in "
9676 "declaration specifiers"));
9677 else if (specs->typespec_word == cts_dfloat32)
9678 error_at (loc,
9679 ("both %<signed%> and %<_Decimal32%> in "
9680 "declaration specifiers"));
9681 else if (specs->typespec_word == cts_dfloat64)
9682 error_at (loc,
9683 ("both %<signed%> and %<_Decimal64%> in "
9684 "declaration specifiers"));
9685 else if (specs->typespec_word == cts_dfloat128)
9686 error_at (loc,
9687 ("both %<signed%> and %<_Decimal128%> in "
9688 "declaration specifiers"));
9689 else
9691 specs->signed_p = true;
9692 specs->locations[cdw_signed] = loc;
9694 break;
9695 case RID_UNSIGNED:
9696 dupe = specs->unsigned_p;
9697 if (specs->signed_p)
9698 error_at (loc,
9699 ("both %<signed%> and %<unsigned%> in "
9700 "declaration specifiers"));
9701 else if (specs->typespec_word == cts_auto_type)
9702 error_at (loc,
9703 ("both %<unsigned%> and %<__auto_type%> in "
9704 "declaration specifiers"));
9705 else if (specs->typespec_word == cts_void)
9706 error_at (loc,
9707 ("both %<unsigned%> and %<void%> in "
9708 "declaration specifiers"));
9709 else if (specs->typespec_word == cts_bool)
9710 error_at (loc,
9711 ("both %<unsigned%> and %<_Bool%> in "
9712 "declaration specifiers"));
9713 else if (specs->typespec_word == cts_float)
9714 error_at (loc,
9715 ("both %<unsigned%> and %<float%> in "
9716 "declaration specifiers"));
9717 else if (specs->typespec_word == cts_double)
9718 error_at (loc,
9719 ("both %<unsigned%> and %<double%> in "
9720 "declaration specifiers"));
9721 else if (specs->typespec_word == cts_dfloat32)
9722 error_at (loc,
9723 ("both %<unsigned%> and %<_Decimal32%> in "
9724 "declaration specifiers"));
9725 else if (specs->typespec_word == cts_dfloat64)
9726 error_at (loc,
9727 ("both %<unsigned%> and %<_Decimal64%> in "
9728 "declaration specifiers"));
9729 else if (specs->typespec_word == cts_dfloat128)
9730 error_at (loc,
9731 ("both %<unsigned%> and %<_Decimal128%> in "
9732 "declaration specifiers"));
9733 else
9735 specs->unsigned_p = true;
9736 specs->locations[cdw_unsigned] = loc;
9738 break;
9739 case RID_COMPLEX:
9740 dupe = specs->complex_p;
9741 if (!in_system_header_at (loc))
9742 pedwarn_c90 (loc, OPT_Wpedantic,
9743 "ISO C90 does not support complex types");
9744 if (specs->typespec_word == cts_auto_type)
9745 error_at (loc,
9746 ("both %<complex%> and %<__auto_type%> in "
9747 "declaration specifiers"));
9748 else if (specs->typespec_word == cts_void)
9749 error_at (loc,
9750 ("both %<complex%> and %<void%> in "
9751 "declaration specifiers"));
9752 else if (specs->typespec_word == cts_bool)
9753 error_at (loc,
9754 ("both %<complex%> and %<_Bool%> in "
9755 "declaration specifiers"));
9756 else if (specs->typespec_word == cts_dfloat32)
9757 error_at (loc,
9758 ("both %<complex%> and %<_Decimal32%> in "
9759 "declaration specifiers"));
9760 else if (specs->typespec_word == cts_dfloat64)
9761 error_at (loc,
9762 ("both %<complex%> and %<_Decimal64%> in "
9763 "declaration specifiers"));
9764 else if (specs->typespec_word == cts_dfloat128)
9765 error_at (loc,
9766 ("both %<complex%> and %<_Decimal128%> in "
9767 "declaration specifiers"));
9768 else if (specs->typespec_word == cts_fract)
9769 error_at (loc,
9770 ("both %<complex%> and %<_Fract%> in "
9771 "declaration specifiers"));
9772 else if (specs->typespec_word == cts_accum)
9773 error_at (loc,
9774 ("both %<complex%> and %<_Accum%> in "
9775 "declaration specifiers"));
9776 else if (specs->saturating_p)
9777 error_at (loc,
9778 ("both %<complex%> and %<_Sat%> in "
9779 "declaration specifiers"));
9780 else
9782 specs->complex_p = true;
9783 specs->locations[cdw_complex] = loc;
9785 break;
9786 case RID_SAT:
9787 dupe = specs->saturating_p;
9788 pedwarn (loc, OPT_Wpedantic,
9789 "ISO C does not support saturating types");
9790 if (specs->typespec_word == cts_int_n)
9792 error_at (loc,
9793 ("both %<_Sat%> and %<__int%d%> in "
9794 "declaration specifiers"),
9795 int_n_data[specs->int_n_idx].bitsize);
9797 else if (specs->typespec_word == cts_auto_type)
9798 error_at (loc,
9799 ("both %<_Sat%> and %<__auto_type%> in "
9800 "declaration specifiers"));
9801 else if (specs->typespec_word == cts_void)
9802 error_at (loc,
9803 ("both %<_Sat%> and %<void%> in "
9804 "declaration specifiers"));
9805 else if (specs->typespec_word == cts_bool)
9806 error_at (loc,
9807 ("both %<_Sat%> and %<_Bool%> in "
9808 "declaration specifiers"));
9809 else if (specs->typespec_word == cts_char)
9810 error_at (loc,
9811 ("both %<_Sat%> and %<char%> in "
9812 "declaration specifiers"));
9813 else if (specs->typespec_word == cts_int)
9814 error_at (loc,
9815 ("both %<_Sat%> and %<int%> in "
9816 "declaration specifiers"));
9817 else if (specs->typespec_word == cts_float)
9818 error_at (loc,
9819 ("both %<_Sat%> and %<float%> in "
9820 "declaration specifiers"));
9821 else if (specs->typespec_word == cts_double)
9822 error_at (loc,
9823 ("both %<_Sat%> and %<double%> in "
9824 "declaration specifiers"));
9825 else if (specs->typespec_word == cts_dfloat32)
9826 error_at (loc,
9827 ("both %<_Sat%> and %<_Decimal32%> in "
9828 "declaration specifiers"));
9829 else if (specs->typespec_word == cts_dfloat64)
9830 error_at (loc,
9831 ("both %<_Sat%> and %<_Decimal64%> in "
9832 "declaration specifiers"));
9833 else if (specs->typespec_word == cts_dfloat128)
9834 error_at (loc,
9835 ("both %<_Sat%> and %<_Decimal128%> in "
9836 "declaration specifiers"));
9837 else if (specs->complex_p)
9838 error_at (loc,
9839 ("both %<_Sat%> and %<complex%> in "
9840 "declaration specifiers"));
9841 else
9843 specs->saturating_p = true;
9844 specs->locations[cdw_saturating] = loc;
9846 break;
9847 default:
9848 gcc_unreachable ();
9851 if (dupe)
9852 error_at (loc, "duplicate %qE", type);
9854 return specs;
9856 else
9858 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9859 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
9860 "__auto_type". */
9861 if (specs->typespec_word != cts_none)
9863 error_at (loc,
9864 "two or more data types in declaration specifiers");
9865 return specs;
9867 switch (i)
9869 case RID_AUTO_TYPE:
9870 if (specs->long_p)
9871 error_at (loc,
9872 ("both %<long%> and %<__auto_type%> in "
9873 "declaration specifiers"));
9874 else if (specs->short_p)
9875 error_at (loc,
9876 ("both %<short%> and %<__auto_type%> in "
9877 "declaration specifiers"));
9878 else if (specs->signed_p)
9879 error_at (loc,
9880 ("both %<signed%> and %<__auto_type%> in "
9881 "declaration specifiers"));
9882 else if (specs->unsigned_p)
9883 error_at (loc,
9884 ("both %<unsigned%> and %<__auto_type%> in "
9885 "declaration specifiers"));
9886 else if (specs->complex_p)
9887 error_at (loc,
9888 ("both %<complex%> and %<__auto_type%> in "
9889 "declaration specifiers"));
9890 else if (specs->saturating_p)
9891 error_at (loc,
9892 ("both %<_Sat%> and %<__auto_type%> in "
9893 "declaration specifiers"));
9894 else
9896 specs->typespec_word = cts_auto_type;
9897 specs->locations[cdw_typespec] = loc;
9899 return specs;
9900 case RID_INT_N_0:
9901 case RID_INT_N_1:
9902 case RID_INT_N_2:
9903 case RID_INT_N_3:
9904 specs->int_n_idx = i - RID_INT_N_0;
9905 if (!in_system_header_at (input_location))
9906 pedwarn (loc, OPT_Wpedantic,
9907 "ISO C does not support %<__int%d%> types",
9908 int_n_data[specs->int_n_idx].bitsize);
9910 if (specs->long_p)
9911 error_at (loc,
9912 ("both %<__int%d%> and %<long%> in "
9913 "declaration specifiers"),
9914 int_n_data[specs->int_n_idx].bitsize);
9915 else if (specs->saturating_p)
9916 error_at (loc,
9917 ("both %<_Sat%> and %<__int%d%> in "
9918 "declaration specifiers"),
9919 int_n_data[specs->int_n_idx].bitsize);
9920 else if (specs->short_p)
9921 error_at (loc,
9922 ("both %<__int%d%> and %<short%> in "
9923 "declaration specifiers"),
9924 int_n_data[specs->int_n_idx].bitsize);
9925 else if (! int_n_enabled_p [specs->int_n_idx])
9926 error_at (loc,
9927 "%<__int%d%> is not supported on this target",
9928 int_n_data[specs->int_n_idx].bitsize);
9929 else
9931 specs->typespec_word = cts_int_n;
9932 specs->locations[cdw_typespec] = loc;
9934 return specs;
9935 case RID_VOID:
9936 if (specs->long_p)
9937 error_at (loc,
9938 ("both %<long%> and %<void%> in "
9939 "declaration specifiers"));
9940 else if (specs->short_p)
9941 error_at (loc,
9942 ("both %<short%> and %<void%> in "
9943 "declaration specifiers"));
9944 else if (specs->signed_p)
9945 error_at (loc,
9946 ("both %<signed%> and %<void%> in "
9947 "declaration specifiers"));
9948 else if (specs->unsigned_p)
9949 error_at (loc,
9950 ("both %<unsigned%> and %<void%> in "
9951 "declaration specifiers"));
9952 else if (specs->complex_p)
9953 error_at (loc,
9954 ("both %<complex%> and %<void%> in "
9955 "declaration specifiers"));
9956 else if (specs->saturating_p)
9957 error_at (loc,
9958 ("both %<_Sat%> and %<void%> in "
9959 "declaration specifiers"));
9960 else
9962 specs->typespec_word = cts_void;
9963 specs->locations[cdw_typespec] = loc;
9965 return specs;
9966 case RID_BOOL:
9967 if (!in_system_header_at (loc))
9968 pedwarn_c90 (loc, OPT_Wpedantic,
9969 "ISO C90 does not support boolean types");
9970 if (specs->long_p)
9971 error_at (loc,
9972 ("both %<long%> and %<_Bool%> in "
9973 "declaration specifiers"));
9974 else if (specs->short_p)
9975 error_at (loc,
9976 ("both %<short%> and %<_Bool%> in "
9977 "declaration specifiers"));
9978 else if (specs->signed_p)
9979 error_at (loc,
9980 ("both %<signed%> and %<_Bool%> in "
9981 "declaration specifiers"));
9982 else if (specs->unsigned_p)
9983 error_at (loc,
9984 ("both %<unsigned%> and %<_Bool%> in "
9985 "declaration specifiers"));
9986 else if (specs->complex_p)
9987 error_at (loc,
9988 ("both %<complex%> and %<_Bool%> in "
9989 "declaration specifiers"));
9990 else if (specs->saturating_p)
9991 error_at (loc,
9992 ("both %<_Sat%> and %<_Bool%> in "
9993 "declaration specifiers"));
9994 else
9996 specs->typespec_word = cts_bool;
9997 specs->locations[cdw_typespec] = loc;
9999 return specs;
10000 case RID_CHAR:
10001 if (specs->long_p)
10002 error_at (loc,
10003 ("both %<long%> and %<char%> in "
10004 "declaration specifiers"));
10005 else if (specs->short_p)
10006 error_at (loc,
10007 ("both %<short%> and %<char%> in "
10008 "declaration specifiers"));
10009 else if (specs->saturating_p)
10010 error_at (loc,
10011 ("both %<_Sat%> and %<char%> in "
10012 "declaration specifiers"));
10013 else
10015 specs->typespec_word = cts_char;
10016 specs->locations[cdw_typespec] = loc;
10018 return specs;
10019 case RID_INT:
10020 if (specs->saturating_p)
10021 error_at (loc,
10022 ("both %<_Sat%> and %<int%> in "
10023 "declaration specifiers"));
10024 else
10026 specs->typespec_word = cts_int;
10027 specs->locations[cdw_typespec] = loc;
10029 return specs;
10030 case RID_FLOAT:
10031 if (specs->long_p)
10032 error_at (loc,
10033 ("both %<long%> and %<float%> in "
10034 "declaration specifiers"));
10035 else if (specs->short_p)
10036 error_at (loc,
10037 ("both %<short%> and %<float%> in "
10038 "declaration specifiers"));
10039 else if (specs->signed_p)
10040 error_at (loc,
10041 ("both %<signed%> and %<float%> in "
10042 "declaration specifiers"));
10043 else if (specs->unsigned_p)
10044 error_at (loc,
10045 ("both %<unsigned%> and %<float%> in "
10046 "declaration specifiers"));
10047 else if (specs->saturating_p)
10048 error_at (loc,
10049 ("both %<_Sat%> and %<float%> in "
10050 "declaration specifiers"));
10051 else
10053 specs->typespec_word = cts_float;
10054 specs->locations[cdw_typespec] = loc;
10056 return specs;
10057 case RID_DOUBLE:
10058 if (specs->long_long_p)
10059 error_at (loc,
10060 ("both %<long long%> and %<double%> in "
10061 "declaration specifiers"));
10062 else if (specs->short_p)
10063 error_at (loc,
10064 ("both %<short%> and %<double%> in "
10065 "declaration specifiers"));
10066 else if (specs->signed_p)
10067 error_at (loc,
10068 ("both %<signed%> and %<double%> in "
10069 "declaration specifiers"));
10070 else if (specs->unsigned_p)
10071 error_at (loc,
10072 ("both %<unsigned%> and %<double%> in "
10073 "declaration specifiers"));
10074 else if (specs->saturating_p)
10075 error_at (loc,
10076 ("both %<_Sat%> and %<double%> in "
10077 "declaration specifiers"));
10078 else
10080 specs->typespec_word = cts_double;
10081 specs->locations[cdw_typespec] = loc;
10083 return specs;
10084 case RID_DFLOAT32:
10085 case RID_DFLOAT64:
10086 case RID_DFLOAT128:
10088 const char *str;
10089 if (i == RID_DFLOAT32)
10090 str = "_Decimal32";
10091 else if (i == RID_DFLOAT64)
10092 str = "_Decimal64";
10093 else
10094 str = "_Decimal128";
10095 if (specs->long_long_p)
10096 error_at (loc,
10097 ("both %<long long%> and %<%s%> in "
10098 "declaration specifiers"),
10099 str);
10100 if (specs->long_p)
10101 error_at (loc,
10102 ("both %<long%> and %<%s%> in "
10103 "declaration specifiers"),
10104 str);
10105 else if (specs->short_p)
10106 error_at (loc,
10107 ("both %<short%> and %<%s%> in "
10108 "declaration specifiers"),
10109 str);
10110 else if (specs->signed_p)
10111 error_at (loc,
10112 ("both %<signed%> and %<%s%> in "
10113 "declaration specifiers"),
10114 str);
10115 else if (specs->unsigned_p)
10116 error_at (loc,
10117 ("both %<unsigned%> and %<%s%> in "
10118 "declaration specifiers"),
10119 str);
10120 else if (specs->complex_p)
10121 error_at (loc,
10122 ("both %<complex%> and %<%s%> in "
10123 "declaration specifiers"),
10124 str);
10125 else if (specs->saturating_p)
10126 error_at (loc,
10127 ("both %<_Sat%> and %<%s%> in "
10128 "declaration specifiers"),
10129 str);
10130 else if (i == RID_DFLOAT32)
10131 specs->typespec_word = cts_dfloat32;
10132 else if (i == RID_DFLOAT64)
10133 specs->typespec_word = cts_dfloat64;
10134 else
10135 specs->typespec_word = cts_dfloat128;
10136 specs->locations[cdw_typespec] = loc;
10138 if (!targetm.decimal_float_supported_p ())
10139 error_at (loc,
10140 ("decimal floating point not supported "
10141 "for this target"));
10142 pedwarn (loc, OPT_Wpedantic,
10143 "ISO C does not support decimal floating point");
10144 return specs;
10145 case RID_FRACT:
10146 case RID_ACCUM:
10148 const char *str;
10149 if (i == RID_FRACT)
10150 str = "_Fract";
10151 else
10152 str = "_Accum";
10153 if (specs->complex_p)
10154 error_at (loc,
10155 ("both %<complex%> and %<%s%> in "
10156 "declaration specifiers"),
10157 str);
10158 else if (i == RID_FRACT)
10159 specs->typespec_word = cts_fract;
10160 else
10161 specs->typespec_word = cts_accum;
10162 specs->locations[cdw_typespec] = loc;
10164 if (!targetm.fixed_point_supported_p ())
10165 error_at (loc,
10166 "fixed-point types not supported for this target");
10167 pedwarn (loc, OPT_Wpedantic,
10168 "ISO C does not support fixed-point types");
10169 return specs;
10170 default:
10171 /* ObjC reserved word "id", handled below. */
10172 break;
10177 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10178 form of ObjC type, cases such as "int" and "long" being handled
10179 above), a TYPE (struct, union, enum and typeof specifiers) or an
10180 ERROR_MARK. In none of these cases may there have previously
10181 been any type specifiers. */
10182 if (specs->type || specs->typespec_word != cts_none
10183 || specs->long_p || specs->short_p || specs->signed_p
10184 || specs->unsigned_p || specs->complex_p)
10185 error_at (loc, "two or more data types in declaration specifiers");
10186 else if (TREE_CODE (type) == TYPE_DECL)
10188 if (TREE_TYPE (type) == error_mark_node)
10189 ; /* Allow the type to default to int to avoid cascading errors. */
10190 else
10192 specs->type = TREE_TYPE (type);
10193 specs->decl_attr = DECL_ATTRIBUTES (type);
10194 specs->typedef_p = true;
10195 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10196 specs->locations[cdw_typedef] = loc;
10198 /* If this typedef name is defined in a struct, then a C++
10199 lookup would return a different value. */
10200 if (warn_cxx_compat
10201 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10202 warning_at (loc, OPT_Wc___compat,
10203 "C++ lookup of %qD would return a field, not a type",
10204 type);
10206 /* If we are parsing a struct, record that a struct field
10207 used a typedef. */
10208 if (warn_cxx_compat && struct_parse_info != NULL)
10209 struct_parse_info->typedefs_seen.safe_push (type);
10212 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10214 tree t = lookup_name (type);
10215 if (!t || TREE_CODE (t) != TYPE_DECL)
10216 error_at (loc, "%qE fails to be a typedef or built in type", type);
10217 else if (TREE_TYPE (t) == error_mark_node)
10219 else
10221 specs->type = TREE_TYPE (t);
10222 specs->locations[cdw_typespec] = loc;
10225 else
10227 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10229 specs->typedef_p = true;
10230 specs->locations[cdw_typedef] = loc;
10231 if (spec.expr)
10233 if (specs->expr)
10234 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10235 specs->expr, spec.expr);
10236 else
10237 specs->expr = spec.expr;
10238 specs->expr_const_operands &= spec.expr_const_operands;
10241 specs->type = type;
10244 return specs;
10247 /* Add the storage class specifier or function specifier SCSPEC to the
10248 declaration specifiers SPECS, returning SPECS. */
10250 struct c_declspecs *
10251 declspecs_add_scspec (source_location loc,
10252 struct c_declspecs *specs,
10253 tree scspec)
10255 enum rid i;
10256 enum c_storage_class n = csc_none;
10257 bool dupe = false;
10258 specs->declspecs_seen_p = true;
10259 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10260 && C_IS_RESERVED_WORD (scspec));
10261 i = C_RID_CODE (scspec);
10262 if (specs->non_sc_seen_p)
10263 warning (OPT_Wold_style_declaration,
10264 "%qE is not at beginning of declaration", scspec);
10265 switch (i)
10267 case RID_INLINE:
10268 /* C99 permits duplicate inline. Although of doubtful utility,
10269 it seems simplest to permit it in gnu89 mode as well, as
10270 there is also little utility in maintaining this as a
10271 difference between gnu89 and C99 inline. */
10272 dupe = false;
10273 specs->inline_p = true;
10274 specs->locations[cdw_inline] = loc;
10275 break;
10276 case RID_NORETURN:
10277 /* Duplicate _Noreturn is permitted. */
10278 dupe = false;
10279 specs->noreturn_p = true;
10280 specs->locations[cdw_noreturn] = loc;
10281 break;
10282 case RID_THREAD:
10283 dupe = specs->thread_p;
10284 if (specs->storage_class == csc_auto)
10285 error ("%qE used with %<auto%>", scspec);
10286 else if (specs->storage_class == csc_register)
10287 error ("%qE used with %<register%>", scspec);
10288 else if (specs->storage_class == csc_typedef)
10289 error ("%qE used with %<typedef%>", scspec);
10290 else
10292 specs->thread_p = true;
10293 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10294 "__thread") == 0);
10295 /* A diagnostic is not required for the use of this
10296 identifier in the implementation namespace; only diagnose
10297 it for the C11 spelling because of existing code using
10298 the other spelling. */
10299 if (!specs->thread_gnu_p)
10301 if (flag_isoc99)
10302 pedwarn_c99 (loc, OPT_Wpedantic,
10303 "ISO C99 does not support %qE", scspec);
10304 else
10305 pedwarn_c99 (loc, OPT_Wpedantic,
10306 "ISO C90 does not support %qE", scspec);
10308 specs->locations[cdw_thread] = loc;
10310 break;
10311 case RID_AUTO:
10312 n = csc_auto;
10313 break;
10314 case RID_EXTERN:
10315 n = csc_extern;
10316 /* Diagnose "__thread extern". */
10317 if (specs->thread_p && specs->thread_gnu_p)
10318 error ("%<__thread%> before %<extern%>");
10319 break;
10320 case RID_REGISTER:
10321 n = csc_register;
10322 break;
10323 case RID_STATIC:
10324 n = csc_static;
10325 /* Diagnose "__thread static". */
10326 if (specs->thread_p && specs->thread_gnu_p)
10327 error ("%<__thread%> before %<static%>");
10328 break;
10329 case RID_TYPEDEF:
10330 n = csc_typedef;
10331 break;
10332 default:
10333 gcc_unreachable ();
10335 if (n != csc_none && n == specs->storage_class)
10336 dupe = true;
10337 if (dupe)
10339 if (i == RID_THREAD)
10340 error ("duplicate %<_Thread_local%> or %<__thread%>");
10341 else
10342 error ("duplicate %qE", scspec);
10344 if (n != csc_none)
10346 if (specs->storage_class != csc_none && n != specs->storage_class)
10348 error ("multiple storage classes in declaration specifiers");
10350 else
10352 specs->storage_class = n;
10353 specs->locations[cdw_storage_class] = loc;
10354 if (n != csc_extern && n != csc_static && specs->thread_p)
10356 error ("%qs used with %qE",
10357 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10358 scspec);
10359 specs->thread_p = false;
10363 return specs;
10366 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10367 returning SPECS. */
10369 struct c_declspecs *
10370 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10372 specs->attrs = chainon (attrs, specs->attrs);
10373 specs->locations[cdw_attributes] = loc;
10374 specs->declspecs_seen_p = true;
10375 return specs;
10378 /* Add an _Alignas specifier (expression ALIGN, or type whose
10379 alignment is ALIGN) to the declaration specifiers SPECS, returning
10380 SPECS. */
10381 struct c_declspecs *
10382 declspecs_add_alignas (source_location loc,
10383 struct c_declspecs *specs, tree align)
10385 int align_log;
10386 specs->alignas_p = true;
10387 specs->locations[cdw_alignas] = loc;
10388 if (align == error_mark_node)
10389 return specs;
10390 align_log = check_user_alignment (align, true);
10391 if (align_log > specs->align_log)
10392 specs->align_log = align_log;
10393 return specs;
10396 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10397 specifiers with any other type specifier to determine the resulting
10398 type. This is where ISO C checks on complex types are made, since
10399 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10400 double". */
10402 struct c_declspecs *
10403 finish_declspecs (struct c_declspecs *specs)
10405 /* If a type was specified as a whole, we have no modifiers and are
10406 done. */
10407 if (specs->type != NULL_TREE)
10409 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10410 && !specs->signed_p && !specs->unsigned_p
10411 && !specs->complex_p);
10413 /* Set a dummy type. */
10414 if (TREE_CODE (specs->type) == ERROR_MARK)
10415 specs->type = integer_type_node;
10416 return specs;
10419 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10420 has been specified, treat it as "int" unless "_Complex" is
10421 present and there are no other specifiers. If we just have
10422 "_Complex", it is equivalent to "_Complex double", but e.g.
10423 "_Complex short" is equivalent to "_Complex short int". */
10424 if (specs->typespec_word == cts_none)
10426 if (specs->saturating_p)
10428 error_at (specs->locations[cdw_saturating],
10429 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10430 if (!targetm.fixed_point_supported_p ())
10431 error_at (specs->locations[cdw_saturating],
10432 "fixed-point types not supported for this target");
10433 specs->typespec_word = cts_fract;
10435 else if (specs->long_p || specs->short_p
10436 || specs->signed_p || specs->unsigned_p)
10438 specs->typespec_word = cts_int;
10440 else if (specs->complex_p)
10442 specs->typespec_word = cts_double;
10443 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10444 "ISO C does not support plain %<complex%> meaning "
10445 "%<double complex%>");
10447 else
10449 specs->typespec_word = cts_int;
10450 specs->default_int_p = true;
10451 /* We don't diagnose this here because grokdeclarator will
10452 give more specific diagnostics according to whether it is
10453 a function definition. */
10457 /* If "signed" was specified, record this to distinguish "int" and
10458 "signed int" in the case of a bit-field with
10459 -funsigned-bitfields. */
10460 specs->explicit_signed_p = specs->signed_p;
10462 /* Now compute the actual type. */
10463 switch (specs->typespec_word)
10465 case cts_auto_type:
10466 gcc_assert (!specs->long_p && !specs->short_p
10467 && !specs->signed_p && !specs->unsigned_p
10468 && !specs->complex_p);
10469 /* Type to be filled in later. */
10470 break;
10471 case cts_void:
10472 gcc_assert (!specs->long_p && !specs->short_p
10473 && !specs->signed_p && !specs->unsigned_p
10474 && !specs->complex_p);
10475 specs->type = void_type_node;
10476 break;
10477 case cts_bool:
10478 gcc_assert (!specs->long_p && !specs->short_p
10479 && !specs->signed_p && !specs->unsigned_p
10480 && !specs->complex_p);
10481 specs->type = boolean_type_node;
10482 break;
10483 case cts_char:
10484 gcc_assert (!specs->long_p && !specs->short_p);
10485 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10486 if (specs->signed_p)
10487 specs->type = signed_char_type_node;
10488 else if (specs->unsigned_p)
10489 specs->type = unsigned_char_type_node;
10490 else
10491 specs->type = char_type_node;
10492 if (specs->complex_p)
10494 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10495 "ISO C does not support complex integer types");
10496 specs->type = build_complex_type (specs->type);
10498 break;
10499 case cts_int_n:
10500 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10501 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10502 specs->type = (specs->unsigned_p
10503 ? int_n_trees[specs->int_n_idx].unsigned_type
10504 : int_n_trees[specs->int_n_idx].signed_type);
10505 if (specs->complex_p)
10507 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10508 "ISO C does not support complex integer types");
10509 specs->type = build_complex_type (specs->type);
10511 break;
10512 case cts_int:
10513 gcc_assert (!(specs->long_p && specs->short_p));
10514 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10515 if (specs->long_long_p)
10516 specs->type = (specs->unsigned_p
10517 ? long_long_unsigned_type_node
10518 : long_long_integer_type_node);
10519 else if (specs->long_p)
10520 specs->type = (specs->unsigned_p
10521 ? long_unsigned_type_node
10522 : long_integer_type_node);
10523 else if (specs->short_p)
10524 specs->type = (specs->unsigned_p
10525 ? short_unsigned_type_node
10526 : short_integer_type_node);
10527 else
10528 specs->type = (specs->unsigned_p
10529 ? unsigned_type_node
10530 : integer_type_node);
10531 if (specs->complex_p)
10533 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10534 "ISO C does not support complex integer types");
10535 specs->type = build_complex_type (specs->type);
10537 break;
10538 case cts_float:
10539 gcc_assert (!specs->long_p && !specs->short_p
10540 && !specs->signed_p && !specs->unsigned_p);
10541 specs->type = (specs->complex_p
10542 ? complex_float_type_node
10543 : float_type_node);
10544 break;
10545 case cts_double:
10546 gcc_assert (!specs->long_long_p && !specs->short_p
10547 && !specs->signed_p && !specs->unsigned_p);
10548 if (specs->long_p)
10550 specs->type = (specs->complex_p
10551 ? complex_long_double_type_node
10552 : long_double_type_node);
10554 else
10556 specs->type = (specs->complex_p
10557 ? complex_double_type_node
10558 : double_type_node);
10560 break;
10561 case cts_dfloat32:
10562 case cts_dfloat64:
10563 case cts_dfloat128:
10564 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10565 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10566 if (specs->typespec_word == cts_dfloat32)
10567 specs->type = dfloat32_type_node;
10568 else if (specs->typespec_word == cts_dfloat64)
10569 specs->type = dfloat64_type_node;
10570 else
10571 specs->type = dfloat128_type_node;
10572 break;
10573 case cts_fract:
10574 gcc_assert (!specs->complex_p);
10575 if (!targetm.fixed_point_supported_p ())
10576 specs->type = integer_type_node;
10577 else if (specs->saturating_p)
10579 if (specs->long_long_p)
10580 specs->type = specs->unsigned_p
10581 ? sat_unsigned_long_long_fract_type_node
10582 : sat_long_long_fract_type_node;
10583 else if (specs->long_p)
10584 specs->type = specs->unsigned_p
10585 ? sat_unsigned_long_fract_type_node
10586 : sat_long_fract_type_node;
10587 else if (specs->short_p)
10588 specs->type = specs->unsigned_p
10589 ? sat_unsigned_short_fract_type_node
10590 : sat_short_fract_type_node;
10591 else
10592 specs->type = specs->unsigned_p
10593 ? sat_unsigned_fract_type_node
10594 : sat_fract_type_node;
10596 else
10598 if (specs->long_long_p)
10599 specs->type = specs->unsigned_p
10600 ? unsigned_long_long_fract_type_node
10601 : long_long_fract_type_node;
10602 else if (specs->long_p)
10603 specs->type = specs->unsigned_p
10604 ? unsigned_long_fract_type_node
10605 : long_fract_type_node;
10606 else if (specs->short_p)
10607 specs->type = specs->unsigned_p
10608 ? unsigned_short_fract_type_node
10609 : short_fract_type_node;
10610 else
10611 specs->type = specs->unsigned_p
10612 ? unsigned_fract_type_node
10613 : fract_type_node;
10615 break;
10616 case cts_accum:
10617 gcc_assert (!specs->complex_p);
10618 if (!targetm.fixed_point_supported_p ())
10619 specs->type = integer_type_node;
10620 else if (specs->saturating_p)
10622 if (specs->long_long_p)
10623 specs->type = specs->unsigned_p
10624 ? sat_unsigned_long_long_accum_type_node
10625 : sat_long_long_accum_type_node;
10626 else if (specs->long_p)
10627 specs->type = specs->unsigned_p
10628 ? sat_unsigned_long_accum_type_node
10629 : sat_long_accum_type_node;
10630 else if (specs->short_p)
10631 specs->type = specs->unsigned_p
10632 ? sat_unsigned_short_accum_type_node
10633 : sat_short_accum_type_node;
10634 else
10635 specs->type = specs->unsigned_p
10636 ? sat_unsigned_accum_type_node
10637 : sat_accum_type_node;
10639 else
10641 if (specs->long_long_p)
10642 specs->type = specs->unsigned_p
10643 ? unsigned_long_long_accum_type_node
10644 : long_long_accum_type_node;
10645 else if (specs->long_p)
10646 specs->type = specs->unsigned_p
10647 ? unsigned_long_accum_type_node
10648 : long_accum_type_node;
10649 else if (specs->short_p)
10650 specs->type = specs->unsigned_p
10651 ? unsigned_short_accum_type_node
10652 : short_accum_type_node;
10653 else
10654 specs->type = specs->unsigned_p
10655 ? unsigned_accum_type_node
10656 : accum_type_node;
10658 break;
10659 default:
10660 gcc_unreachable ();
10663 return specs;
10666 /* A subroutine of c_write_global_declarations. Perform final processing
10667 on one file scope's declarations (or the external scope's declarations),
10668 GLOBALS. */
10670 static void
10671 c_write_global_declarations_1 (tree globals)
10673 tree decl;
10674 bool reconsider;
10676 /* Process the decls in the order they were written. */
10677 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10679 /* Check for used but undefined static functions using the C
10680 standard's definition of "used", and set TREE_NO_WARNING so
10681 that check_global_declarations doesn't repeat the check. */
10682 if (TREE_CODE (decl) == FUNCTION_DECL
10683 && DECL_INITIAL (decl) == 0
10684 && DECL_EXTERNAL (decl)
10685 && !TREE_PUBLIC (decl)
10686 && C_DECL_USED (decl))
10688 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10689 TREE_NO_WARNING (decl) = 1;
10692 wrapup_global_declaration_1 (decl);
10697 reconsider = false;
10698 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10699 reconsider |= wrapup_global_declaration_2 (decl);
10701 while (reconsider);
10703 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10704 check_global_declaration_1 (decl);
10707 /* A subroutine of c_write_global_declarations Emit debug information for each
10708 of the declarations in GLOBALS. */
10710 static void
10711 c_write_global_declarations_2 (tree globals)
10713 tree decl;
10715 for (decl = globals; decl ; decl = DECL_CHAIN (decl))
10716 debug_hooks->global_decl (decl);
10719 /* Callback to collect a source_ref from a DECL. */
10721 static void
10722 collect_source_ref_cb (tree decl)
10724 if (!DECL_IS_BUILTIN (decl))
10725 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10728 /* Preserve the external declarations scope across a garbage collect. */
10729 static GTY(()) tree ext_block;
10731 /* Collect all references relevant to SOURCE_FILE. */
10733 static void
10734 collect_all_refs (const char *source_file)
10736 tree t;
10737 unsigned i;
10739 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10740 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10742 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10745 /* Iterate over all global declarations and call CALLBACK. */
10747 static void
10748 for_each_global_decl (void (*callback) (tree decl))
10750 tree t;
10751 tree decls;
10752 tree decl;
10753 unsigned i;
10755 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10757 decls = DECL_INITIAL (t);
10758 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10759 callback (decl);
10762 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10763 callback (decl);
10766 void
10767 c_write_global_declarations (void)
10769 tree t;
10770 unsigned i;
10772 /* We don't want to do this if generating a PCH. */
10773 if (pch_file)
10774 return;
10776 timevar_start (TV_PHASE_DEFERRED);
10778 /* Do the Objective-C stuff. This is where all the Objective-C
10779 module stuff gets generated (symtab, class/protocol/selector
10780 lists etc). */
10781 if (c_dialect_objc ())
10782 objc_write_global_declarations ();
10784 /* Close the external scope. */
10785 ext_block = pop_scope ();
10786 external_scope = 0;
10787 gcc_assert (!current_scope);
10789 /* Handle -fdump-ada-spec[-slim]. */
10790 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10792 /* Build a table of files to generate specs for */
10793 if (flag_dump_ada_spec_slim)
10794 collect_source_ref (main_input_filename);
10795 else
10796 for_each_global_decl (collect_source_ref_cb);
10798 dump_ada_specs (collect_all_refs, NULL);
10801 if (ext_block)
10803 tree tmp = BLOCK_VARS (ext_block);
10804 int flags;
10805 FILE * stream = dump_begin (TDI_tu, &flags);
10806 if (stream && tmp)
10808 dump_node (tmp, flags & ~TDF_SLIM, stream);
10809 dump_end (TDI_tu, stream);
10813 /* Process all file scopes in this compilation, and the external_scope,
10814 through wrapup_global_declarations and check_global_declarations. */
10815 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10816 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10817 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10819 timevar_stop (TV_PHASE_DEFERRED);
10820 timevar_start (TV_PHASE_OPT_GEN);
10822 /* We're done parsing; proceed to optimize and emit assembly.
10823 FIXME: shouldn't be the front end's responsibility to call this. */
10824 symtab->finalize_compilation_unit ();
10826 timevar_stop (TV_PHASE_OPT_GEN);
10827 timevar_start (TV_PHASE_DBGINFO);
10829 /* After cgraph has had a chance to emit everything that's going to
10830 be emitted, output debug information for globals. */
10831 if (!seen_error ())
10833 timevar_push (TV_SYMOUT);
10834 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10835 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10836 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10837 timevar_pop (TV_SYMOUT);
10840 ext_block = NULL;
10841 timevar_stop (TV_PHASE_DBGINFO);
10844 /* Register reserved keyword WORD as qualifier for address space AS. */
10846 void
10847 c_register_addr_space (const char *word, addr_space_t as)
10849 int rid = RID_FIRST_ADDR_SPACE + as;
10850 tree id;
10852 /* Address space qualifiers are only supported
10853 in C with GNU extensions enabled. */
10854 if (c_dialect_objc () || flag_no_asm)
10855 return;
10857 id = get_identifier (word);
10858 C_SET_RID_CODE (id, rid);
10859 C_IS_RESERVED_WORD (id) = 1;
10860 ridpointers [rid] = id;
10863 /* Return identifier to look up for omp declare reduction. */
10865 tree
10866 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
10868 const char *p = NULL;
10869 switch (reduction_code)
10871 case PLUS_EXPR: p = "+"; break;
10872 case MULT_EXPR: p = "*"; break;
10873 case MINUS_EXPR: p = "-"; break;
10874 case BIT_AND_EXPR: p = "&"; break;
10875 case BIT_XOR_EXPR: p = "^"; break;
10876 case BIT_IOR_EXPR: p = "|"; break;
10877 case TRUTH_ANDIF_EXPR: p = "&&"; break;
10878 case TRUTH_ORIF_EXPR: p = "||"; break;
10879 case MIN_EXPR: p = "min"; break;
10880 case MAX_EXPR: p = "max"; break;
10881 default:
10882 break;
10885 if (p == NULL)
10887 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
10888 return error_mark_node;
10889 p = IDENTIFIER_POINTER (reduction_id);
10892 const char prefix[] = "omp declare reduction ";
10893 size_t lenp = sizeof (prefix);
10894 size_t len = strlen (p);
10895 char *name = XALLOCAVEC (char, lenp + len);
10896 memcpy (name, prefix, lenp - 1);
10897 memcpy (name + lenp - 1, p, len + 1);
10898 return get_identifier (name);
10901 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10902 VAR_DECL, bind it into the current scope and return it. */
10904 tree
10905 c_omp_reduction_decl (tree reduction_id)
10907 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10908 if (b != NULL && B_IN_CURRENT_SCOPE (b))
10909 return b->decl;
10911 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
10912 reduction_id, integer_type_node);
10913 DECL_ARTIFICIAL (decl) = 1;
10914 DECL_EXTERNAL (decl) = 1;
10915 TREE_STATIC (decl) = 1;
10916 TREE_PUBLIC (decl) = 0;
10917 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
10918 return decl;
10921 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10923 tree
10924 c_omp_reduction_lookup (tree reduction_id, tree type)
10926 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10927 while (b)
10929 tree t;
10930 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
10931 if (comptypes (TREE_PURPOSE (t), type))
10932 return TREE_VALUE (t);
10933 b = b->shadowed;
10935 return error_mark_node;
10938 /* Helper function called via walk_tree, to diagnose invalid
10939 #pragma omp declare reduction combiners or initializers. */
10941 tree
10942 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
10944 tree *vars = (tree *) data;
10945 if (SSA_VAR_P (*tp)
10946 && !DECL_ARTIFICIAL (*tp)
10947 && *tp != vars[0]
10948 && *tp != vars[1])
10950 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
10951 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
10952 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
10953 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10954 *tp);
10955 else
10956 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
10957 "to variable %qD which is not %<omp_priv%> nor "
10958 "%<omp_orig%>",
10959 *tp);
10960 return *tp;
10962 return NULL_TREE;
10965 #include "gt-c-c-decl.h"