[48/77] Make subroutines of num_sign_bit_copies operate on scalar_int_mode
[official-gcc.git] / gcc / c / c-decl.c
blobd526f0e88e4e20a6000f93944e8a9e10732d99d5
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "function.h"
32 #include "c-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "cgraph.h"
36 #include "intl.h"
37 #include "print-tree.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "toplev.h"
42 #include "debug.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-pragma.h"
45 #include "c-family/c-ubsan.h"
46 #include "c-lang.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
49 #include "dumpfile.h"
50 #include "plugin.h"
51 #include "c-family/c-ada-spec.h"
52 #include "cilk.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
58 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
59 enum decl_context
60 { NORMAL, /* Ordinary declaration */
61 FUNCDEF, /* Function definition */
62 PARM, /* Declaration of parm before function body */
63 FIELD, /* Declaration inside struct or union */
64 TYPENAME}; /* Typename (inside cast or sizeof) */
66 /* States indicating how grokdeclarator() should handle declspecs marked
67 with __attribute__((deprecated)). An object declared as
68 __attribute__((deprecated)) suppresses warnings of uses of other
69 deprecated items. */
71 enum deprecated_states {
72 DEPRECATED_NORMAL,
73 DEPRECATED_SUPPRESS
77 /* Nonzero if we have seen an invalid cross reference
78 to a struct, union, or enum, but not yet printed the message. */
79 tree pending_invalid_xref;
81 /* File and line to appear in the eventual error message. */
82 location_t pending_invalid_xref_location;
84 /* The file and line that the prototype came from if this is an
85 old-style definition; used for diagnostics in
86 store_parm_decls_oldstyle. */
88 static location_t current_function_prototype_locus;
90 /* Whether this prototype was built-in. */
92 static bool current_function_prototype_built_in;
94 /* The argument type information of this prototype. */
96 static tree current_function_prototype_arg_types;
98 /* The argument information structure for the function currently being
99 defined. */
101 static struct c_arg_info *current_function_arg_info;
103 /* The obstack on which parser and related data structures, which are
104 not live beyond their top-level declaration or definition, are
105 allocated. */
106 struct obstack parser_obstack;
108 /* The current statement tree. */
110 static GTY(()) struct stmt_tree_s c_stmt_tree;
112 /* State saving variables. */
113 tree c_break_label;
114 tree c_cont_label;
116 /* A list of decls to be made automatically visible in each file scope. */
117 static GTY(()) tree visible_builtins;
119 /* Set to 0 at beginning of a function definition, set to 1 if
120 a return statement that specifies a return value is seen. */
122 int current_function_returns_value;
124 /* Set to 0 at beginning of a function definition, set to 1 if
125 a return statement with no argument is seen. */
127 int current_function_returns_null;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a call to a noreturn function is seen. */
132 int current_function_returns_abnormally;
134 /* Set to nonzero by `grokdeclarator' for a function
135 whose return type is defaulted, if warnings for this are desired. */
137 static int warn_about_return_type;
139 /* Nonzero when the current toplevel function contains a declaration
140 of a nested function which is never defined. */
142 static bool undef_nested_function;
144 /* If non-zero, implicit "omp declare target" attribute is added into the
145 attribute lists. */
146 int current_omp_declare_target_attribute;
148 /* Each c_binding structure describes one binding of an identifier to
149 a decl. All the decls in a scope - irrespective of namespace - are
150 chained together by the ->prev field, which (as the name implies)
151 runs in reverse order. All the decls in a given namespace bound to
152 a given identifier are chained by the ->shadowed field, which runs
153 from inner to outer scopes.
155 The ->decl field usually points to a DECL node, but there are two
156 exceptions. In the namespace of type tags, the bound entity is a
157 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
158 identifier is encountered, it is bound to error_mark_node to
159 suppress further errors about that identifier in the current
160 function.
162 The ->u.type field stores the type of the declaration in this scope;
163 if NULL, the type is the type of the ->decl field. This is only of
164 relevance for objects with external or internal linkage which may
165 be redeclared in inner scopes, forming composite types that only
166 persist for the duration of those scopes. In the external scope,
167 this stores the composite of all the types declared for this
168 object, visible or not. The ->inner_comp field (used only at file
169 scope) stores whether an incomplete array type at file scope was
170 completed at an inner scope to an array size other than 1.
172 The ->u.label field is used for labels. It points to a structure
173 which stores additional information used for warnings.
175 The depth field is copied from the scope structure that holds this
176 decl. It is used to preserve the proper ordering of the ->shadowed
177 field (see bind()) and also for a handful of special-case checks.
178 Finally, the invisible bit is true for a decl which should be
179 ignored for purposes of normal name lookup, and the nested bit is
180 true for a decl that's been bound a second time in an inner scope;
181 in all such cases, the binding in the outer scope will have its
182 invisible bit true. */
184 struct GTY((chain_next ("%h.prev"))) c_binding {
185 union GTY(()) { /* first so GTY desc can use decl */
186 tree GTY((tag ("0"))) type; /* the type in this scope */
187 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
188 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
189 tree decl; /* the decl bound */
190 tree id; /* the identifier it's bound to */
191 struct c_binding *prev; /* the previous decl in this scope */
192 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
193 unsigned int depth : 28; /* depth of this scope */
194 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
195 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
196 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
197 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
198 location_t locus; /* location for nested bindings */
200 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
201 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
202 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
203 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
205 /* Each C symbol points to three linked lists of c_binding structures.
206 These describe the values of the identifier in the three different
207 namespaces defined by the language. */
209 struct GTY(()) lang_identifier {
210 struct c_common_identifier common_id;
211 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
212 struct c_binding *tag_binding; /* struct/union/enum tags */
213 struct c_binding *label_binding; /* labels */
216 /* Validate c-lang.c's assumptions. */
217 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
218 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
220 /* The binding oracle; see c-tree.h. */
221 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
223 /* This flag is set on an identifier if we have previously asked the
224 binding oracle for this identifier's symbol binding. */
225 #define I_SYMBOL_CHECKED(node) \
226 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
228 static inline struct c_binding* *
229 i_symbol_binding (tree node)
231 struct lang_identifier *lid
232 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
234 if (lid->symbol_binding == NULL
235 && c_binding_oracle != NULL
236 && !I_SYMBOL_CHECKED (node))
238 /* Set the "checked" flag first, to avoid infinite recursion
239 when the binding oracle calls back into gcc. */
240 I_SYMBOL_CHECKED (node) = 1;
241 c_binding_oracle (C_ORACLE_SYMBOL, node);
244 return &lid->symbol_binding;
247 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
249 #define I_SYMBOL_DECL(node) \
250 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
252 /* This flag is set on an identifier if we have previously asked the
253 binding oracle for this identifier's tag binding. */
254 #define I_TAG_CHECKED(node) \
255 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
257 static inline struct c_binding **
258 i_tag_binding (tree node)
260 struct lang_identifier *lid
261 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
263 if (lid->tag_binding == NULL
264 && c_binding_oracle != NULL
265 && !I_TAG_CHECKED (node))
267 /* Set the "checked" flag first, to avoid infinite recursion
268 when the binding oracle calls back into gcc. */
269 I_TAG_CHECKED (node) = 1;
270 c_binding_oracle (C_ORACLE_TAG, node);
273 return &lid->tag_binding;
276 #define I_TAG_BINDING(node) (*i_tag_binding (node))
278 #define I_TAG_DECL(node) \
279 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
281 /* This flag is set on an identifier if we have previously asked the
282 binding oracle for this identifier's label binding. */
283 #define I_LABEL_CHECKED(node) \
284 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
286 static inline struct c_binding **
287 i_label_binding (tree node)
289 struct lang_identifier *lid
290 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
292 if (lid->label_binding == NULL
293 && c_binding_oracle != NULL
294 && !I_LABEL_CHECKED (node))
296 /* Set the "checked" flag first, to avoid infinite recursion
297 when the binding oracle calls back into gcc. */
298 I_LABEL_CHECKED (node) = 1;
299 c_binding_oracle (C_ORACLE_LABEL, node);
302 return &lid->label_binding;
305 #define I_LABEL_BINDING(node) (*i_label_binding (node))
307 #define I_LABEL_DECL(node) \
308 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
310 /* The resulting tree type. */
312 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
313 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
315 union tree_node GTY ((tag ("0"),
316 desc ("tree_node_structure (&%h)")))
317 generic;
318 struct lang_identifier GTY ((tag ("1"))) identifier;
321 /* Track bindings and other things that matter for goto warnings. For
322 efficiency, we do not gather all the decls at the point of
323 definition. Instead, we point into the bindings structure. As
324 scopes are popped, we update these structures and gather the decls
325 that matter at that time. */
327 struct GTY(()) c_spot_bindings {
328 /* The currently open scope which holds bindings defined when the
329 label was defined or the goto statement was found. */
330 struct c_scope *scope;
331 /* The bindings in the scope field which were defined at the point
332 of the label or goto. This lets us look at older or newer
333 bindings in the scope, as appropriate. */
334 struct c_binding *bindings_in_scope;
335 /* The number of statement expressions that have started since this
336 label or goto statement was defined. This is zero if we are at
337 the same statement expression level. It is positive if we are in
338 a statement expression started since this spot. It is negative
339 if this spot was in a statement expression and we have left
340 it. */
341 int stmt_exprs;
342 /* Whether we started in a statement expression but are no longer in
343 it. This is set to true if stmt_exprs ever goes negative. */
344 bool left_stmt_expr;
347 /* This structure is used to keep track of bindings seen when a goto
348 statement is defined. This is only used if we see the goto
349 statement before we see the label. */
351 struct GTY(()) c_goto_bindings {
352 /* The location of the goto statement. */
353 location_t loc;
354 /* The bindings of the goto statement. */
355 struct c_spot_bindings goto_bindings;
358 typedef struct c_goto_bindings *c_goto_bindings_p;
360 /* The additional information we keep track of for a label binding.
361 These fields are updated as scopes are popped. */
363 struct GTY(()) c_label_vars {
364 /* The shadowed c_label_vars, when one label shadows another (which
365 can only happen using a __label__ declaration). */
366 struct c_label_vars *shadowed;
367 /* The bindings when the label was defined. */
368 struct c_spot_bindings label_bindings;
369 /* A list of decls that we care about: decls about which we should
370 warn if a goto branches to this label from later in the function.
371 Decls are added to this list as scopes are popped. We only add
372 the decls that matter. */
373 vec<tree, va_gc> *decls_in_scope;
374 /* A list of goto statements to this label. This is only used for
375 goto statements seen before the label was defined, so that we can
376 issue appropriate warnings for them. */
377 vec<c_goto_bindings_p, va_gc> *gotos;
380 /* Each c_scope structure describes the complete contents of one
381 scope. Four scopes are distinguished specially: the innermost or
382 current scope, the innermost function scope, the file scope (always
383 the second to outermost) and the outermost or external scope.
385 Most declarations are recorded in the current scope.
387 All normal label declarations are recorded in the innermost
388 function scope, as are bindings of undeclared identifiers to
389 error_mark_node. (GCC permits nested functions as an extension,
390 hence the 'innermost' qualifier.) Explicitly declared labels
391 (using the __label__ extension) appear in the current scope.
393 Being in the file scope (current_scope == file_scope) causes
394 special behavior in several places below. Also, under some
395 conditions the Objective-C front end records declarations in the
396 file scope even though that isn't the current scope.
398 All declarations with external linkage are recorded in the external
399 scope, even if they aren't visible there; this models the fact that
400 such declarations are visible to the entire program, and (with a
401 bit of cleverness, see pushdecl) allows diagnosis of some violations
402 of C99 6.2.2p7 and 6.2.7p2:
404 If, within the same translation unit, the same identifier appears
405 with both internal and external linkage, the behavior is
406 undefined.
408 All declarations that refer to the same object or function shall
409 have compatible type; otherwise, the behavior is undefined.
411 Initially only the built-in declarations, which describe compiler
412 intrinsic functions plus a subset of the standard library, are in
413 this scope.
415 The order of the blocks list matters, and it is frequently appended
416 to. To avoid having to walk all the way to the end of the list on
417 each insertion, or reverse the list later, we maintain a pointer to
418 the last list entry. (FIXME: It should be feasible to use a reversed
419 list here.)
421 The bindings list is strictly in reverse order of declarations;
422 pop_scope relies on this. */
425 struct GTY((chain_next ("%h.outer"))) c_scope {
426 /* The scope containing this one. */
427 struct c_scope *outer;
429 /* The next outermost function scope. */
430 struct c_scope *outer_function;
432 /* All bindings in this scope. */
433 struct c_binding *bindings;
435 /* For each scope (except the global one), a chain of BLOCK nodes
436 for all the scopes that were entered and exited one level down. */
437 tree blocks;
438 tree blocks_last;
440 /* The depth of this scope. Used to keep the ->shadowed chain of
441 bindings sorted innermost to outermost. */
442 unsigned int depth : 28;
444 /* True if we are currently filling this scope with parameter
445 declarations. */
446 BOOL_BITFIELD parm_flag : 1;
448 /* True if we saw [*] in this scope. Used to give an error messages
449 if these appears in a function definition. */
450 BOOL_BITFIELD had_vla_unspec : 1;
452 /* True if we already complained about forward parameter decls
453 in this scope. This prevents double warnings on
454 foo (int a; int b; ...) */
455 BOOL_BITFIELD warned_forward_parm_decls : 1;
457 /* True if this is the outermost block scope of a function body.
458 This scope contains the parameters, the local variables declared
459 in the outermost block, and all the labels (except those in
460 nested functions, or declared at block scope with __label__). */
461 BOOL_BITFIELD function_body : 1;
463 /* True means make a BLOCK for this scope no matter what. */
464 BOOL_BITFIELD keep : 1;
466 /* True means that an unsuffixed float constant is _Decimal64. */
467 BOOL_BITFIELD float_const_decimal64 : 1;
469 /* True if this scope has any label bindings. This is used to speed
470 up searching for labels when popping scopes, particularly since
471 labels are normally only found at function scope. */
472 BOOL_BITFIELD has_label_bindings : 1;
474 /* True if we should issue a warning if a goto statement crosses any
475 of the bindings. We still need to check the list of bindings to
476 find the specific ones we need to warn about. This is true if
477 decl_jump_unsafe would return true for any of the bindings. This
478 is used to avoid looping over all the bindings unnecessarily. */
479 BOOL_BITFIELD has_jump_unsafe_decl : 1;
482 /* The scope currently in effect. */
484 static GTY(()) struct c_scope *current_scope;
486 /* The innermost function scope. Ordinary (not explicitly declared)
487 labels, bindings to error_mark_node, and the lazily-created
488 bindings of __func__ and its friends get this scope. */
490 static GTY(()) struct c_scope *current_function_scope;
492 /* The C file scope. This is reset for each input translation unit. */
494 static GTY(()) struct c_scope *file_scope;
496 /* The outermost scope. This is used for all declarations with
497 external linkage, and only these, hence the name. */
499 static GTY(()) struct c_scope *external_scope;
501 /* A chain of c_scope structures awaiting reuse. */
503 static GTY((deletable)) struct c_scope *scope_freelist;
505 /* A chain of c_binding structures awaiting reuse. */
507 static GTY((deletable)) struct c_binding *binding_freelist;
509 /* Append VAR to LIST in scope SCOPE. */
510 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
511 struct c_scope *s_ = (scope); \
512 tree d_ = (decl); \
513 if (s_->list##_last) \
514 BLOCK_CHAIN (s_->list##_last) = d_; \
515 else \
516 s_->list = d_; \
517 s_->list##_last = d_; \
518 } while (0)
520 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
521 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
522 struct c_scope *t_ = (tscope); \
523 struct c_scope *f_ = (fscope); \
524 if (t_->to##_last) \
525 BLOCK_CHAIN (t_->to##_last) = f_->from; \
526 else \
527 t_->to = f_->from; \
528 t_->to##_last = f_->from##_last; \
529 } while (0)
531 /* A c_inline_static structure stores details of a static identifier
532 referenced in a definition of a function that may be an inline
533 definition if no subsequent declaration of that function uses
534 "extern" or does not use "inline". */
536 struct GTY((chain_next ("%h.next"))) c_inline_static {
537 /* The location for a diagnostic. */
538 location_t location;
540 /* The function that may be an inline definition. */
541 tree function;
543 /* The object or function referenced. */
544 tree static_decl;
546 /* What sort of reference this is. */
547 enum c_inline_static_type type;
549 /* The next such structure or NULL. */
550 struct c_inline_static *next;
553 /* List of static identifiers used or referenced in functions that may
554 be inline definitions. */
555 static GTY(()) struct c_inline_static *c_inline_statics;
557 /* True means unconditionally make a BLOCK for the next scope pushed. */
559 static bool keep_next_level_flag;
561 /* True means the next call to push_scope will be the outermost scope
562 of a function body, so do not push a new scope, merely cease
563 expecting parameter decls. */
565 static bool next_is_function_body;
567 /* A vector of pointers to c_binding structures. */
569 typedef struct c_binding *c_binding_ptr;
571 /* Information that we keep for a struct or union while it is being
572 parsed. */
574 struct c_struct_parse_info
576 /* If warn_cxx_compat, a list of types defined within this
577 struct. */
578 auto_vec<tree> struct_types;
579 /* If warn_cxx_compat, a list of field names which have bindings,
580 and which are defined in this struct, but which are not defined
581 in any enclosing struct. This is used to clear the in_struct
582 field of the c_bindings structure. */
583 auto_vec<c_binding_ptr> fields;
584 /* If warn_cxx_compat, a list of typedef names used when defining
585 fields in this struct. */
586 auto_vec<tree> typedefs_seen;
589 /* Information for the struct or union currently being parsed, or
590 NULL if not parsing a struct or union. */
591 static struct c_struct_parse_info *struct_parse_info;
593 /* Forward declarations. */
594 static tree lookup_name_in_scope (tree, struct c_scope *);
595 static tree c_make_fname_decl (location_t, tree, int);
596 static tree grokdeclarator (const struct c_declarator *,
597 struct c_declspecs *,
598 enum decl_context, bool, tree *, tree *, tree *,
599 bool *, enum deprecated_states);
600 static tree grokparms (struct c_arg_info *, bool);
601 static void layout_array_type (tree);
602 static void warn_defaults_to (location_t, int, const char *, ...)
603 ATTRIBUTE_GCC_DIAG(3,4);
605 /* T is a statement. Add it to the statement-tree. This is the
606 C/ObjC version--C++ has a slightly different version of this
607 function. */
609 tree
610 add_stmt (tree t)
612 enum tree_code code = TREE_CODE (t);
614 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
616 if (!EXPR_HAS_LOCATION (t))
617 SET_EXPR_LOCATION (t, input_location);
620 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
621 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
623 /* Add T to the statement-tree. Non-side-effect statements need to be
624 recorded during statement expressions. */
625 if (!building_stmt_list_p ())
626 push_stmt_list ();
627 append_to_statement_list_force (t, &cur_stmt_list);
629 return t;
632 /* Build a pointer type using the default pointer mode. */
634 static tree
635 c_build_pointer_type (tree to_type)
637 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
638 : TYPE_ADDR_SPACE (to_type);
639 machine_mode pointer_mode;
641 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
642 pointer_mode = targetm.addr_space.pointer_mode (as);
643 else
644 pointer_mode = c_default_pointer_mode;
645 return build_pointer_type_for_mode (to_type, pointer_mode, false);
649 /* Return true if we will want to say something if a goto statement
650 crosses DECL. */
652 static bool
653 decl_jump_unsafe (tree decl)
655 if (error_operand_p (decl))
656 return false;
658 /* Always warn about crossing variably modified types. */
659 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
660 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
661 return true;
663 /* Otherwise, only warn if -Wgoto-misses-init and this is an
664 initialized automatic decl. */
665 if (warn_jump_misses_init
666 && VAR_P (decl)
667 && !TREE_STATIC (decl)
668 && DECL_INITIAL (decl) != NULL_TREE)
669 return true;
671 return false;
675 void
676 c_print_identifier (FILE *file, tree node, int indent)
678 void (*save) (enum c_oracle_request, tree identifier);
680 /* Temporarily hide any binding oracle. Without this, calls to
681 debug_tree from the debugger will end up calling into the oracle,
682 making for a confusing debug session. As the oracle isn't needed
683 here for normal operation, it's simplest to suppress it. */
684 save = c_binding_oracle;
685 c_binding_oracle = NULL;
687 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
688 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
689 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
690 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
692 tree rid = ridpointers[C_RID_CODE (node)];
693 indent_to (file, indent + 4);
694 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
695 (void *) rid, IDENTIFIER_POINTER (rid));
698 c_binding_oracle = save;
701 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
702 which may be any of several kinds of DECL or TYPE or error_mark_node,
703 in the scope SCOPE. */
704 static void
705 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
706 bool nested, location_t locus)
708 struct c_binding *b, **here;
710 if (binding_freelist)
712 b = binding_freelist;
713 binding_freelist = b->prev;
715 else
716 b = ggc_alloc<c_binding> ();
718 b->shadowed = 0;
719 b->decl = decl;
720 b->id = name;
721 b->depth = scope->depth;
722 b->invisible = invisible;
723 b->nested = nested;
724 b->inner_comp = 0;
725 b->in_struct = 0;
726 b->locus = locus;
728 b->u.type = NULL;
730 b->prev = scope->bindings;
731 scope->bindings = b;
733 if (decl_jump_unsafe (decl))
734 scope->has_jump_unsafe_decl = 1;
736 if (!name)
737 return;
739 switch (TREE_CODE (decl))
741 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
742 case ENUMERAL_TYPE:
743 case UNION_TYPE:
744 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
745 case VAR_DECL:
746 case FUNCTION_DECL:
747 case TYPE_DECL:
748 case CONST_DECL:
749 case PARM_DECL:
750 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
752 default:
753 gcc_unreachable ();
756 /* Locate the appropriate place in the chain of shadowed decls
757 to insert this binding. Normally, scope == current_scope and
758 this does nothing. */
759 while (*here && (*here)->depth > scope->depth)
760 here = &(*here)->shadowed;
762 b->shadowed = *here;
763 *here = b;
766 /* Clear the binding structure B, stick it on the binding_freelist,
767 and return the former value of b->prev. This is used by pop_scope
768 and get_parm_info to iterate destructively over all the bindings
769 from a given scope. */
770 static struct c_binding *
771 free_binding_and_advance (struct c_binding *b)
773 struct c_binding *prev = b->prev;
775 memset (b, 0, sizeof (struct c_binding));
776 b->prev = binding_freelist;
777 binding_freelist = b;
779 return prev;
782 /* Bind a label. Like bind, but skip fields which aren't used for
783 labels, and add the LABEL_VARS value. */
784 static void
785 bind_label (tree name, tree label, struct c_scope *scope,
786 struct c_label_vars *label_vars)
788 struct c_binding *b;
790 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
791 UNKNOWN_LOCATION);
793 scope->has_label_bindings = true;
795 b = scope->bindings;
796 gcc_assert (b->decl == label);
797 label_vars->shadowed = b->u.label;
798 b->u.label = label_vars;
801 /* Hook called at end of compilation to assume 1 elt
802 for a file-scope tentative array defn that wasn't complete before. */
804 void
805 c_finish_incomplete_decl (tree decl)
807 if (VAR_P (decl))
809 tree type = TREE_TYPE (decl);
810 if (type != error_mark_node
811 && TREE_CODE (type) == ARRAY_TYPE
812 && !DECL_EXTERNAL (decl)
813 && TYPE_DOMAIN (type) == NULL_TREE)
815 warning_at (DECL_SOURCE_LOCATION (decl),
816 0, "array %q+D assumed to have one element", decl);
818 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
820 relayout_decl (decl);
825 /* Record that inline function FUNC contains a reference (location
826 LOC) to static DECL (file-scope or function-local according to
827 TYPE). */
829 void
830 record_inline_static (location_t loc, tree func, tree decl,
831 enum c_inline_static_type type)
833 c_inline_static *csi = ggc_alloc<c_inline_static> ();
834 csi->location = loc;
835 csi->function = func;
836 csi->static_decl = decl;
837 csi->type = type;
838 csi->next = c_inline_statics;
839 c_inline_statics = csi;
842 /* Check for references to static declarations in inline functions at
843 the end of the translation unit and diagnose them if the functions
844 are still inline definitions. */
846 static void
847 check_inline_statics (void)
849 struct c_inline_static *csi;
850 for (csi = c_inline_statics; csi; csi = csi->next)
852 if (DECL_EXTERNAL (csi->function))
853 switch (csi->type)
855 case csi_internal:
856 pedwarn (csi->location, 0,
857 "%qD is static but used in inline function %qD "
858 "which is not static", csi->static_decl, csi->function);
859 break;
860 case csi_modifiable:
861 pedwarn (csi->location, 0,
862 "%q+D is static but declared in inline function %qD "
863 "which is not static", csi->static_decl, csi->function);
864 break;
865 default:
866 gcc_unreachable ();
869 c_inline_statics = NULL;
872 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
873 for the current state, otherwise set it to uninitialized. */
875 static void
876 set_spot_bindings (struct c_spot_bindings *p, bool defining)
878 if (defining)
880 p->scope = current_scope;
881 p->bindings_in_scope = current_scope->bindings;
883 else
885 p->scope = NULL;
886 p->bindings_in_scope = NULL;
888 p->stmt_exprs = 0;
889 p->left_stmt_expr = false;
892 /* Update spot bindings P as we pop out of SCOPE. Return true if we
893 should push decls for a label. */
895 static bool
896 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
898 if (p->scope != scope)
900 /* This label or goto is defined in some other scope, or it is a
901 label which is not yet defined. There is nothing to
902 update. */
903 return false;
906 /* Adjust the spot bindings to refer to the bindings already defined
907 in the enclosing scope. */
908 p->scope = scope->outer;
909 p->bindings_in_scope = p->scope->bindings;
911 return true;
914 /* The Objective-C front-end often needs to determine the current scope. */
916 void *
917 objc_get_current_scope (void)
919 return current_scope;
922 /* The following function is used only by Objective-C. It needs to live here
923 because it accesses the innards of c_scope. */
925 void
926 objc_mark_locals_volatile (void *enclosing_blk)
928 struct c_scope *scope;
929 struct c_binding *b;
931 for (scope = current_scope;
932 scope && scope != enclosing_blk;
933 scope = scope->outer)
935 for (b = scope->bindings; b; b = b->prev)
936 objc_volatilize_decl (b->decl);
938 /* Do not climb up past the current function. */
939 if (scope->function_body)
940 break;
944 /* Return true if we are in the global binding level. */
946 bool
947 global_bindings_p (void)
949 return current_scope == file_scope;
952 void
953 keep_next_level (void)
955 keep_next_level_flag = true;
958 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
960 void
961 set_float_const_decimal64 (void)
963 current_scope->float_const_decimal64 = true;
966 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
968 void
969 clear_float_const_decimal64 (void)
971 current_scope->float_const_decimal64 = false;
974 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
976 bool
977 float_const_decimal64_p (void)
979 return current_scope->float_const_decimal64;
982 /* Identify this scope as currently being filled with parameters. */
984 void
985 declare_parm_level (void)
987 current_scope->parm_flag = true;
990 void
991 push_scope (void)
993 if (next_is_function_body)
995 /* This is the transition from the parameters to the top level
996 of the function body. These are the same scope
997 (C99 6.2.1p4,6) so we do not push another scope structure.
998 next_is_function_body is set only by store_parm_decls, which
999 in turn is called when and only when we are about to
1000 encounter the opening curly brace for the function body.
1002 The outermost block of a function always gets a BLOCK node,
1003 because the debugging output routines expect that each
1004 function has at least one BLOCK. */
1005 current_scope->parm_flag = false;
1006 current_scope->function_body = true;
1007 current_scope->keep = true;
1008 current_scope->outer_function = current_function_scope;
1009 current_function_scope = current_scope;
1011 keep_next_level_flag = false;
1012 next_is_function_body = false;
1014 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1015 if (current_scope->outer)
1016 current_scope->float_const_decimal64
1017 = current_scope->outer->float_const_decimal64;
1018 else
1019 current_scope->float_const_decimal64 = false;
1021 else
1023 struct c_scope *scope;
1024 if (scope_freelist)
1026 scope = scope_freelist;
1027 scope_freelist = scope->outer;
1029 else
1030 scope = ggc_cleared_alloc<c_scope> ();
1032 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1033 if (current_scope)
1034 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1035 else
1036 scope->float_const_decimal64 = false;
1038 scope->keep = keep_next_level_flag;
1039 scope->outer = current_scope;
1040 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1042 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1043 possible. */
1044 if (current_scope && scope->depth == 0)
1046 scope->depth--;
1047 sorry ("GCC supports only %u nested scopes", scope->depth);
1050 current_scope = scope;
1051 keep_next_level_flag = false;
1055 /* This is called when we are leaving SCOPE. For each label defined
1056 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1057 These are the decls whose initialization will be skipped by a goto
1058 later in the function. */
1060 static void
1061 update_label_decls (struct c_scope *scope)
1063 struct c_scope *s;
1065 s = scope;
1066 while (s != NULL)
1068 if (s->has_label_bindings)
1070 struct c_binding *b;
1072 for (b = s->bindings; b != NULL; b = b->prev)
1074 struct c_label_vars *label_vars;
1075 struct c_binding *b1;
1076 bool hjud;
1077 unsigned int ix;
1078 struct c_goto_bindings *g;
1080 if (TREE_CODE (b->decl) != LABEL_DECL)
1081 continue;
1082 label_vars = b->u.label;
1084 b1 = label_vars->label_bindings.bindings_in_scope;
1085 if (label_vars->label_bindings.scope == NULL)
1086 hjud = false;
1087 else
1088 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1089 if (update_spot_bindings (scope, &label_vars->label_bindings))
1091 /* This label is defined in this scope. */
1092 if (hjud)
1094 for (; b1 != NULL; b1 = b1->prev)
1096 /* A goto from later in the function to this
1097 label will never see the initialization
1098 of B1, if any. Save it to issue a
1099 warning if needed. */
1100 if (decl_jump_unsafe (b1->decl))
1101 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1106 /* Update the bindings of any goto statements associated
1107 with this label. */
1108 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1109 update_spot_bindings (scope, &g->goto_bindings);
1113 /* Don't search beyond the current function. */
1114 if (s == current_function_scope)
1115 break;
1117 s = s->outer;
1121 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1123 static void
1124 set_type_context (tree type, tree context)
1126 for (type = TYPE_MAIN_VARIANT (type); type;
1127 type = TYPE_NEXT_VARIANT (type))
1128 TYPE_CONTEXT (type) = context;
1131 /* Exit a scope. Restore the state of the identifier-decl mappings
1132 that were in effect when this scope was entered. Return a BLOCK
1133 node containing all the DECLs in this scope that are of interest
1134 to debug info generation. */
1136 tree
1137 pop_scope (void)
1139 struct c_scope *scope = current_scope;
1140 tree block, context, p;
1141 struct c_binding *b;
1143 bool functionbody = scope->function_body;
1144 bool keep = functionbody || scope->keep || scope->bindings;
1146 update_label_decls (scope);
1148 /* If appropriate, create a BLOCK to record the decls for the life
1149 of this function. */
1150 block = NULL_TREE;
1151 if (keep)
1153 block = make_node (BLOCK);
1154 BLOCK_SUBBLOCKS (block) = scope->blocks;
1155 TREE_USED (block) = 1;
1157 /* In each subblock, record that this is its superior. */
1158 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1159 BLOCK_SUPERCONTEXT (p) = block;
1161 BLOCK_VARS (block) = NULL_TREE;
1164 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1165 scope must be set so that they point to the appropriate
1166 construct, i.e. either to the current FUNCTION_DECL node, or
1167 else to the BLOCK node we just constructed.
1169 Note that for tagged types whose scope is just the formal
1170 parameter list for some function type specification, we can't
1171 properly set their TYPE_CONTEXTs here, because we don't have a
1172 pointer to the appropriate FUNCTION_TYPE node readily available
1173 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1174 type nodes get set in `grokdeclarator' as soon as we have created
1175 the FUNCTION_TYPE node which will represent the "scope" for these
1176 "parameter list local" tagged types. */
1177 if (scope->function_body)
1178 context = current_function_decl;
1179 else if (scope == file_scope)
1181 tree file_decl
1182 = build_translation_unit_decl (get_identifier (main_input_filename));
1183 context = file_decl;
1184 debug_hooks->register_main_translation_unit (file_decl);
1186 else
1187 context = block;
1189 /* Clear all bindings in this scope. */
1190 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1192 p = b->decl;
1193 switch (TREE_CODE (p))
1195 case LABEL_DECL:
1196 /* Warnings for unused labels, errors for undefined labels. */
1197 if (TREE_USED (p) && !DECL_INITIAL (p))
1199 error ("label %q+D used but not defined", p);
1200 DECL_INITIAL (p) = error_mark_node;
1202 else
1203 warn_for_unused_label (p);
1205 /* Labels go in BLOCK_VARS. */
1206 DECL_CHAIN (p) = BLOCK_VARS (block);
1207 BLOCK_VARS (block) = p;
1208 gcc_assert (I_LABEL_BINDING (b->id) == b);
1209 I_LABEL_BINDING (b->id) = b->shadowed;
1211 /* Also pop back to the shadowed label_vars. */
1212 release_tree_vector (b->u.label->decls_in_scope);
1213 b->u.label = b->u.label->shadowed;
1214 break;
1216 case ENUMERAL_TYPE:
1217 case UNION_TYPE:
1218 case RECORD_TYPE:
1219 set_type_context (p, context);
1221 /* Types may not have tag-names, in which case the type
1222 appears in the bindings list with b->id NULL. */
1223 if (b->id)
1225 gcc_assert (I_TAG_BINDING (b->id) == b);
1226 I_TAG_BINDING (b->id) = b->shadowed;
1228 break;
1230 case FUNCTION_DECL:
1231 /* Propagate TREE_ADDRESSABLE from nested functions to their
1232 containing functions. */
1233 if (!TREE_ASM_WRITTEN (p)
1234 && DECL_INITIAL (p) != NULL_TREE
1235 && TREE_ADDRESSABLE (p)
1236 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1237 && DECL_ABSTRACT_ORIGIN (p) != p)
1238 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1239 if (!DECL_EXTERNAL (p)
1240 && !DECL_INITIAL (p)
1241 && scope != file_scope
1242 && scope != external_scope)
1244 error ("nested function %q+D declared but never defined", p);
1245 undef_nested_function = true;
1247 else if (DECL_DECLARED_INLINE_P (p)
1248 && TREE_PUBLIC (p)
1249 && !DECL_INITIAL (p))
1251 /* C99 6.7.4p6: "a function with external linkage... declared
1252 with an inline function specifier ... shall also be defined
1253 in the same translation unit." */
1254 if (!flag_gnu89_inline
1255 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1256 && scope != external_scope)
1257 pedwarn (input_location, 0,
1258 "inline function %q+D declared but never defined", p);
1259 DECL_EXTERNAL (p) = 1;
1262 goto common_symbol;
1264 case VAR_DECL:
1265 /* Warnings for unused variables. */
1266 if ((!TREE_USED (p) || !DECL_READ_P (p))
1267 && !TREE_NO_WARNING (p)
1268 && !DECL_IN_SYSTEM_HEADER (p)
1269 && DECL_NAME (p)
1270 && !DECL_ARTIFICIAL (p)
1271 && scope != file_scope
1272 && scope != external_scope)
1274 if (!TREE_USED (p))
1275 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1276 else if (DECL_CONTEXT (p) == current_function_decl)
1277 warning_at (DECL_SOURCE_LOCATION (p),
1278 OPT_Wunused_but_set_variable,
1279 "variable %qD set but not used", p);
1282 if (b->inner_comp)
1284 error ("type of array %q+D completed incompatibly with"
1285 " implicit initialization", p);
1288 /* Fall through. */
1289 case TYPE_DECL:
1290 case CONST_DECL:
1291 common_symbol:
1292 /* All of these go in BLOCK_VARS, but only if this is the
1293 binding in the home scope. */
1294 if (!b->nested)
1296 DECL_CHAIN (p) = BLOCK_VARS (block);
1297 BLOCK_VARS (block) = p;
1299 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1301 /* For block local externs add a special
1302 DECL_EXTERNAL decl for debug info generation. */
1303 tree extp = copy_node (p);
1305 DECL_EXTERNAL (extp) = 1;
1306 TREE_STATIC (extp) = 0;
1307 TREE_PUBLIC (extp) = 1;
1308 DECL_INITIAL (extp) = NULL_TREE;
1309 DECL_LANG_SPECIFIC (extp) = NULL;
1310 DECL_CONTEXT (extp) = current_function_decl;
1311 if (TREE_CODE (p) == FUNCTION_DECL)
1313 DECL_RESULT (extp) = NULL_TREE;
1314 DECL_SAVED_TREE (extp) = NULL_TREE;
1315 DECL_STRUCT_FUNCTION (extp) = NULL;
1317 if (b->locus != UNKNOWN_LOCATION)
1318 DECL_SOURCE_LOCATION (extp) = b->locus;
1319 DECL_CHAIN (extp) = BLOCK_VARS (block);
1320 BLOCK_VARS (block) = extp;
1322 /* If this is the file scope set DECL_CONTEXT of each decl to
1323 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1324 work. */
1325 if (scope == file_scope)
1327 DECL_CONTEXT (p) = context;
1328 if (TREE_CODE (p) == TYPE_DECL
1329 && TREE_TYPE (p) != error_mark_node)
1330 set_type_context (TREE_TYPE (p), context);
1333 gcc_fallthrough ();
1334 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1335 already been put there by store_parm_decls. Unused-
1336 parameter warnings are handled by function.c.
1337 error_mark_node obviously does not go in BLOCK_VARS and
1338 does not get unused-variable warnings. */
1339 case PARM_DECL:
1340 case ERROR_MARK:
1341 /* It is possible for a decl not to have a name. We get
1342 here with b->id NULL in this case. */
1343 if (b->id)
1345 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1346 I_SYMBOL_BINDING (b->id) = b->shadowed;
1347 if (b->shadowed && b->shadowed->u.type)
1348 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1350 break;
1352 default:
1353 gcc_unreachable ();
1358 /* Dispose of the block that we just made inside some higher level. */
1359 if ((scope->function_body || scope == file_scope) && context)
1361 DECL_INITIAL (context) = block;
1362 BLOCK_SUPERCONTEXT (block) = context;
1364 else if (scope->outer)
1366 if (block)
1367 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1368 /* If we did not make a block for the scope just exited, any
1369 blocks made for inner scopes must be carried forward so they
1370 will later become subblocks of something else. */
1371 else if (scope->blocks)
1372 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1375 /* Pop the current scope, and free the structure for reuse. */
1376 current_scope = scope->outer;
1377 if (scope->function_body)
1378 current_function_scope = scope->outer_function;
1380 memset (scope, 0, sizeof (struct c_scope));
1381 scope->outer = scope_freelist;
1382 scope_freelist = scope;
1384 return block;
1387 void
1388 push_file_scope (void)
1390 tree decl;
1392 if (file_scope)
1393 return;
1395 push_scope ();
1396 file_scope = current_scope;
1398 start_fname_decls ();
1400 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1401 bind (DECL_NAME (decl), decl, file_scope,
1402 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1405 void
1406 pop_file_scope (void)
1408 /* In case there were missing closebraces, get us back to the global
1409 binding level. */
1410 while (current_scope != file_scope)
1411 pop_scope ();
1413 /* __FUNCTION__ is defined at file scope (""). This
1414 call may not be necessary as my tests indicate it
1415 still works without it. */
1416 finish_fname_decls ();
1418 check_inline_statics ();
1420 /* This is the point to write out a PCH if we're doing that.
1421 In that case we do not want to do anything else. */
1422 if (pch_file)
1424 c_common_write_pch ();
1425 /* Ensure even the callers don't try to finalize the CU. */
1426 flag_syntax_only = 1;
1427 return;
1430 /* Pop off the file scope and close this translation unit. */
1431 pop_scope ();
1432 file_scope = 0;
1434 maybe_apply_pending_pragma_weaks ();
1437 /* Adjust the bindings for the start of a statement expression. */
1439 void
1440 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1442 struct c_scope *scope;
1444 for (scope = current_scope; scope != NULL; scope = scope->outer)
1446 struct c_binding *b;
1448 if (!scope->has_label_bindings)
1449 continue;
1451 for (b = scope->bindings; b != NULL; b = b->prev)
1453 struct c_label_vars *label_vars;
1454 unsigned int ix;
1455 struct c_goto_bindings *g;
1457 if (TREE_CODE (b->decl) != LABEL_DECL)
1458 continue;
1459 label_vars = b->u.label;
1460 ++label_vars->label_bindings.stmt_exprs;
1461 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1462 ++g->goto_bindings.stmt_exprs;
1466 if (switch_bindings != NULL)
1467 ++switch_bindings->stmt_exprs;
1470 /* Adjust the bindings for the end of a statement expression. */
1472 void
1473 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1475 struct c_scope *scope;
1477 for (scope = current_scope; scope != NULL; scope = scope->outer)
1479 struct c_binding *b;
1481 if (!scope->has_label_bindings)
1482 continue;
1484 for (b = scope->bindings; b != NULL; b = b->prev)
1486 struct c_label_vars *label_vars;
1487 unsigned int ix;
1488 struct c_goto_bindings *g;
1490 if (TREE_CODE (b->decl) != LABEL_DECL)
1491 continue;
1492 label_vars = b->u.label;
1493 --label_vars->label_bindings.stmt_exprs;
1494 if (label_vars->label_bindings.stmt_exprs < 0)
1496 label_vars->label_bindings.left_stmt_expr = true;
1497 label_vars->label_bindings.stmt_exprs = 0;
1499 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1501 --g->goto_bindings.stmt_exprs;
1502 if (g->goto_bindings.stmt_exprs < 0)
1504 g->goto_bindings.left_stmt_expr = true;
1505 g->goto_bindings.stmt_exprs = 0;
1511 if (switch_bindings != NULL)
1513 --switch_bindings->stmt_exprs;
1514 gcc_assert (switch_bindings->stmt_exprs >= 0);
1518 /* Push a definition or a declaration of struct, union or enum tag "name".
1519 "type" should be the type node.
1520 We assume that the tag "name" is not already defined, and has a location
1521 of LOC.
1523 Note that the definition may really be just a forward reference.
1524 In that case, the TYPE_SIZE will be zero. */
1526 static void
1527 pushtag (location_t loc, tree name, tree type)
1529 /* Record the identifier as the type's name if it has none. */
1530 if (name && !TYPE_NAME (type))
1531 TYPE_NAME (type) = name;
1532 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1534 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1535 tagged type we just added to the current scope. This fake
1536 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1537 to output a representation of a tagged type, and it also gives
1538 us a convenient place to record the "scope start" address for the
1539 tagged type. */
1541 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1542 TYPE_DECL, NULL_TREE, type));
1544 /* An approximation for now, so we can tell this is a function-scope tag.
1545 This will be updated in pop_scope. */
1546 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1548 if (warn_cxx_compat && name != NULL_TREE)
1550 struct c_binding *b = I_SYMBOL_BINDING (name);
1552 if (b != NULL
1553 && b->decl != NULL_TREE
1554 && TREE_CODE (b->decl) == TYPE_DECL
1555 && (B_IN_CURRENT_SCOPE (b)
1556 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1557 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1558 != TYPE_MAIN_VARIANT (type)))
1560 if (warning_at (loc, OPT_Wc___compat,
1561 ("using %qD as both a typedef and a tag is "
1562 "invalid in C++"), b->decl)
1563 && b->locus != UNKNOWN_LOCATION)
1564 inform (b->locus, "originally defined here");
1569 /* An exported interface to pushtag. This is used by the gdb plugin's
1570 binding oracle to introduce a new tag binding. */
1572 void
1573 c_pushtag (location_t loc, tree name, tree type)
1575 pushtag (loc, name, type);
1578 /* An exported interface to bind a declaration. LOC is the location
1579 to use. DECL is the declaration to bind. The decl's name is used
1580 to determine how it is bound. If DECL is a VAR_DECL, then
1581 IS_GLOBAL determines whether the decl is put into the global (file
1582 and external) scope or the current function's scope; if DECL is not
1583 a VAR_DECL then it is always put into the file scope. */
1585 void
1586 c_bind (location_t loc, tree decl, bool is_global)
1588 struct c_scope *scope;
1589 bool nested = false;
1591 if (!VAR_P (decl) || current_function_scope == NULL)
1593 /* Types and functions are always considered to be global. */
1594 scope = file_scope;
1595 DECL_EXTERNAL (decl) = 1;
1596 TREE_PUBLIC (decl) = 1;
1598 else if (is_global)
1600 /* Also bind it into the external scope. */
1601 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1602 nested = true;
1603 scope = file_scope;
1604 DECL_EXTERNAL (decl) = 1;
1605 TREE_PUBLIC (decl) = 1;
1607 else
1609 DECL_CONTEXT (decl) = current_function_decl;
1610 TREE_PUBLIC (decl) = 0;
1611 scope = current_function_scope;
1614 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1617 /* Subroutine of compare_decls. Allow harmless mismatches in return
1618 and argument types provided that the type modes match. This function
1619 return a unified type given a suitable match, and 0 otherwise. */
1621 static tree
1622 match_builtin_function_types (tree newtype, tree oldtype)
1624 tree newrettype, oldrettype;
1625 tree newargs, oldargs;
1626 tree trytype, tryargs;
1628 /* Accept the return type of the new declaration if same modes. */
1629 oldrettype = TREE_TYPE (oldtype);
1630 newrettype = TREE_TYPE (newtype);
1632 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1633 return NULL_TREE;
1635 oldargs = TYPE_ARG_TYPES (oldtype);
1636 newargs = TYPE_ARG_TYPES (newtype);
1637 tryargs = newargs;
1639 while (oldargs || newargs)
1641 if (!oldargs
1642 || !newargs
1643 || !TREE_VALUE (oldargs)
1644 || !TREE_VALUE (newargs)
1645 || TYPE_MODE (TREE_VALUE (oldargs))
1646 != TYPE_MODE (TREE_VALUE (newargs)))
1647 return NULL_TREE;
1649 oldargs = TREE_CHAIN (oldargs);
1650 newargs = TREE_CHAIN (newargs);
1653 trytype = build_function_type (newrettype, tryargs);
1655 /* Allow declaration to change transaction_safe attribute. */
1656 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1657 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1658 tree newattrs = TYPE_ATTRIBUTES (newtype);
1659 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1660 if (oldtsafe && !newtsafe)
1661 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1662 else if (newtsafe && !oldtsafe)
1663 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1664 NULL_TREE, oldattrs);
1666 return build_type_attribute_variant (trytype, oldattrs);
1669 /* Subroutine of diagnose_mismatched_decls. Check for function type
1670 mismatch involving an empty arglist vs a nonempty one and give clearer
1671 diagnostics. */
1672 static void
1673 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1674 tree newtype, tree oldtype)
1676 tree t;
1678 if (TREE_CODE (olddecl) != FUNCTION_DECL
1679 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1680 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1681 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1682 return;
1684 t = TYPE_ARG_TYPES (oldtype);
1685 if (t == NULL_TREE)
1686 t = TYPE_ARG_TYPES (newtype);
1687 for (; t; t = TREE_CHAIN (t))
1689 tree type = TREE_VALUE (t);
1691 if (TREE_CHAIN (t) == NULL_TREE
1692 && TYPE_MAIN_VARIANT (type) != void_type_node)
1694 inform (input_location, "a parameter list with an ellipsis can%'t match "
1695 "an empty parameter name list declaration");
1696 break;
1699 if (c_type_promotes_to (type) != type)
1701 inform (input_location, "an argument type that has a default promotion can%'t match "
1702 "an empty parameter name list declaration");
1703 break;
1708 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1709 old-style function definition, NEWDECL is a prototype declaration.
1710 Diagnose inconsistencies in the argument list. Returns TRUE if
1711 the prototype is compatible, FALSE if not. */
1712 static bool
1713 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1715 tree newargs, oldargs;
1716 int i;
1718 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1720 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1721 newargs = TYPE_ARG_TYPES (newtype);
1722 i = 1;
1724 for (;;)
1726 tree oldargtype = TREE_VALUE (oldargs);
1727 tree newargtype = TREE_VALUE (newargs);
1729 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1730 return false;
1732 oldargtype = (TYPE_ATOMIC (oldargtype)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (oldargtype));
1736 newargtype = (TYPE_ATOMIC (newargtype)
1737 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1738 TYPE_QUAL_ATOMIC)
1739 : TYPE_MAIN_VARIANT (newargtype));
1741 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1742 break;
1744 /* Reaching the end of just one list means the two decls don't
1745 agree on the number of arguments. */
1746 if (END_OF_ARGLIST (oldargtype))
1748 error ("prototype for %q+D declares more arguments "
1749 "than previous old-style definition", newdecl);
1750 return false;
1752 else if (END_OF_ARGLIST (newargtype))
1754 error ("prototype for %q+D declares fewer arguments "
1755 "than previous old-style definition", newdecl);
1756 return false;
1759 /* Type for passing arg must be consistent with that declared
1760 for the arg. */
1761 else if (!comptypes (oldargtype, newargtype))
1763 error ("prototype for %q+D declares argument %d"
1764 " with incompatible type",
1765 newdecl, i);
1766 return false;
1769 oldargs = TREE_CHAIN (oldargs);
1770 newargs = TREE_CHAIN (newargs);
1771 i++;
1774 /* If we get here, no errors were found, but do issue a warning
1775 for this poor-style construct. */
1776 warning (0, "prototype for %q+D follows non-prototype definition",
1777 newdecl);
1778 return true;
1779 #undef END_OF_ARGLIST
1782 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1783 first in a pair of mismatched declarations, using the diagnostic
1784 function DIAG. */
1785 static void
1786 locate_old_decl (tree decl)
1788 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1789 && !C_DECL_DECLARED_BUILTIN (decl))
1791 else if (DECL_INITIAL (decl))
1792 inform (input_location, "previous definition of %q+D was here", decl);
1793 else if (C_DECL_IMPLICIT (decl))
1794 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1795 else
1796 inform (input_location, "previous declaration of %q+D was here", decl);
1799 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1800 Returns true if the caller should proceed to merge the two, false
1801 if OLDDECL should simply be discarded. As a side effect, issues
1802 all necessary diagnostics for invalid or poor-style combinations.
1803 If it returns true, writes the types of NEWDECL and OLDDECL to
1804 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1805 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1807 static bool
1808 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1809 tree *newtypep, tree *oldtypep)
1811 tree newtype, oldtype;
1812 bool pedwarned = false;
1813 bool warned = false;
1814 bool retval = true;
1816 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1817 && DECL_EXTERNAL (DECL))
1819 /* If we have error_mark_node for either decl or type, just discard
1820 the previous decl - we're in an error cascade already. */
1821 if (olddecl == error_mark_node || newdecl == error_mark_node)
1822 return false;
1823 *oldtypep = oldtype = TREE_TYPE (olddecl);
1824 *newtypep = newtype = TREE_TYPE (newdecl);
1825 if (oldtype == error_mark_node || newtype == error_mark_node)
1826 return false;
1828 /* Two different categories of symbol altogether. This is an error
1829 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1830 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1832 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1833 && DECL_BUILT_IN (olddecl)
1834 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1836 error ("%q+D redeclared as different kind of symbol", newdecl);
1837 locate_old_decl (olddecl);
1839 else if (TREE_PUBLIC (newdecl))
1840 warning (0, "built-in function %q+D declared as non-function",
1841 newdecl);
1842 else
1843 warning (OPT_Wshadow, "declaration of %q+D shadows "
1844 "a built-in function", newdecl);
1845 return false;
1848 /* Enumerators have no linkage, so may only be declared once in a
1849 given scope. */
1850 if (TREE_CODE (olddecl) == CONST_DECL)
1852 error ("redeclaration of enumerator %q+D", newdecl);
1853 locate_old_decl (olddecl);
1854 return false;
1857 if (!comptypes (oldtype, newtype))
1859 if (TREE_CODE (olddecl) == FUNCTION_DECL
1860 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1862 /* Accept harmless mismatch in function types.
1863 This is for the ffs and fprintf builtins. */
1864 tree trytype = match_builtin_function_types (newtype, oldtype);
1866 if (trytype && comptypes (newtype, trytype))
1867 *oldtypep = oldtype = trytype;
1868 else
1870 /* If types don't match for a built-in, throw away the
1871 built-in. No point in calling locate_old_decl here, it
1872 won't print anything. */
1873 warning (OPT_Wbuiltin_declaration_mismatch,
1874 "conflicting types for built-in function %q+D",
1875 newdecl);
1876 return false;
1879 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1880 && DECL_IS_BUILTIN (olddecl))
1882 /* A conflicting function declaration for a predeclared
1883 function that isn't actually built in. Objective C uses
1884 these. The new declaration silently overrides everything
1885 but the volatility (i.e. noreturn) indication. See also
1886 below. FIXME: Make Objective C use normal builtins. */
1887 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1888 return false;
1890 /* Permit void foo (...) to match int foo (...) if the latter is
1891 the definition and implicit int was used. See
1892 c-torture/compile/920625-2.c. */
1893 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1894 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1895 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1896 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1898 pedwarned = pedwarn (input_location, 0,
1899 "conflicting types for %q+D", newdecl);
1900 /* Make sure we keep void as the return type. */
1901 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1902 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1904 /* Permit void foo (...) to match an earlier call to foo (...) with
1905 no declared type (thus, implicitly int). */
1906 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1907 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1908 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1909 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1911 pedwarned = pedwarn (input_location, 0,
1912 "conflicting types for %q+D", newdecl);
1913 /* Make sure we keep void as the return type. */
1914 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1916 else
1918 int new_quals = TYPE_QUALS (newtype);
1919 int old_quals = TYPE_QUALS (oldtype);
1921 if (new_quals != old_quals)
1923 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1924 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1925 if (new_addr != old_addr)
1927 if (ADDR_SPACE_GENERIC_P (new_addr))
1928 error ("conflicting named address spaces (generic vs %s) "
1929 "for %q+D",
1930 c_addr_space_name (old_addr), newdecl);
1931 else if (ADDR_SPACE_GENERIC_P (old_addr))
1932 error ("conflicting named address spaces (%s vs generic) "
1933 "for %q+D",
1934 c_addr_space_name (new_addr), newdecl);
1935 else
1936 error ("conflicting named address spaces (%s vs %s) "
1937 "for %q+D",
1938 c_addr_space_name (new_addr),
1939 c_addr_space_name (old_addr),
1940 newdecl);
1943 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1944 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1945 error ("conflicting type qualifiers for %q+D", newdecl);
1947 else
1948 error ("conflicting types for %q+D", newdecl);
1949 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1950 locate_old_decl (olddecl);
1951 return false;
1955 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1956 but silently ignore the redeclaration if either is in a system
1957 header. (Conflicting redeclarations were handled above.) This
1958 is allowed for C11 if the types are the same, not just
1959 compatible. */
1960 if (TREE_CODE (newdecl) == TYPE_DECL)
1962 bool types_different = false;
1963 int comptypes_result;
1965 comptypes_result
1966 = comptypes_check_different_types (oldtype, newtype, &types_different);
1968 if (comptypes_result != 1 || types_different)
1970 error ("redefinition of typedef %q+D with different type", newdecl);
1971 locate_old_decl (olddecl);
1972 return false;
1975 if (DECL_IN_SYSTEM_HEADER (newdecl)
1976 || DECL_IN_SYSTEM_HEADER (olddecl)
1977 || TREE_NO_WARNING (newdecl)
1978 || TREE_NO_WARNING (olddecl))
1979 return true; /* Allow OLDDECL to continue in use. */
1981 if (variably_modified_type_p (newtype, NULL))
1983 error ("redefinition of typedef %q+D with variably modified type",
1984 newdecl);
1985 locate_old_decl (olddecl);
1987 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1988 "redefinition of typedef %q+D", newdecl))
1989 locate_old_decl (olddecl);
1991 return true;
1994 /* Function declarations can either be 'static' or 'extern' (no
1995 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1996 can never conflict with each other on account of linkage
1997 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1998 gnu89 mode permits two definitions if one is 'extern inline' and
1999 one is not. The non- extern-inline definition supersedes the
2000 extern-inline definition. */
2002 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2004 /* If you declare a built-in function name as static, or
2005 define the built-in with an old-style definition (so we
2006 can't validate the argument list) the built-in definition is
2007 overridden, but optionally warn this was a bad choice of name. */
2008 if (DECL_BUILT_IN (olddecl)
2009 && !C_DECL_DECLARED_BUILTIN (olddecl)
2010 && (!TREE_PUBLIC (newdecl)
2011 || (DECL_INITIAL (newdecl)
2012 && !prototype_p (TREE_TYPE (newdecl)))))
2014 warning (OPT_Wshadow, "declaration of %q+D shadows "
2015 "a built-in function", newdecl);
2016 /* Discard the old built-in function. */
2017 return false;
2020 if (DECL_INITIAL (newdecl))
2022 if (DECL_INITIAL (olddecl))
2024 /* If both decls are in the same TU and the new declaration
2025 isn't overriding an extern inline reject the new decl.
2026 In c99, no overriding is allowed in the same translation
2027 unit. */
2028 if ((!DECL_EXTERN_INLINE (olddecl)
2029 || DECL_EXTERN_INLINE (newdecl)
2030 || (!flag_gnu89_inline
2031 && (!DECL_DECLARED_INLINE_P (olddecl)
2032 || !lookup_attribute ("gnu_inline",
2033 DECL_ATTRIBUTES (olddecl)))
2034 && (!DECL_DECLARED_INLINE_P (newdecl)
2035 || !lookup_attribute ("gnu_inline",
2036 DECL_ATTRIBUTES (newdecl))))
2038 && same_translation_unit_p (newdecl, olddecl))
2040 error ("redefinition of %q+D", newdecl);
2041 locate_old_decl (olddecl);
2042 return false;
2046 /* If we have a prototype after an old-style function definition,
2047 the argument types must be checked specially. */
2048 else if (DECL_INITIAL (olddecl)
2049 && !prototype_p (oldtype) && prototype_p (newtype)
2050 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2051 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2053 locate_old_decl (olddecl);
2054 return false;
2056 /* A non-static declaration (even an "extern") followed by a
2057 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2058 The same is true for a static forward declaration at block
2059 scope followed by a non-static declaration/definition at file
2060 scope. Static followed by non-static at the same scope is
2061 not undefined behavior, and is the most convenient way to get
2062 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2063 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2064 we do diagnose it if -Wtraditional. */
2065 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2067 /* Two exceptions to the rule. If olddecl is an extern
2068 inline, or a predeclared function that isn't actually
2069 built in, newdecl silently overrides olddecl. The latter
2070 occur only in Objective C; see also above. (FIXME: Make
2071 Objective C use normal builtins.) */
2072 if (!DECL_IS_BUILTIN (olddecl)
2073 && !DECL_EXTERN_INLINE (olddecl))
2075 error ("static declaration of %q+D follows "
2076 "non-static declaration", newdecl);
2077 locate_old_decl (olddecl);
2079 return false;
2081 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2083 if (DECL_CONTEXT (olddecl))
2085 error ("non-static declaration of %q+D follows "
2086 "static declaration", newdecl);
2087 locate_old_decl (olddecl);
2088 return false;
2090 else if (warn_traditional)
2092 warned |= warning (OPT_Wtraditional,
2093 "non-static declaration of %q+D "
2094 "follows static declaration", newdecl);
2098 /* Make sure gnu_inline attribute is either not present, or
2099 present on all inline decls. */
2100 if (DECL_DECLARED_INLINE_P (olddecl)
2101 && DECL_DECLARED_INLINE_P (newdecl))
2103 bool newa = lookup_attribute ("gnu_inline",
2104 DECL_ATTRIBUTES (newdecl)) != NULL;
2105 bool olda = lookup_attribute ("gnu_inline",
2106 DECL_ATTRIBUTES (olddecl)) != NULL;
2107 if (newa != olda)
2109 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2110 newa ? newdecl : olddecl);
2111 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2112 "but not here");
2116 else if (VAR_P (newdecl))
2118 /* Only variables can be thread-local, and all declarations must
2119 agree on this property. */
2120 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2122 /* Nothing to check. Since OLDDECL is marked threadprivate
2123 and NEWDECL does not have a thread-local attribute, we
2124 will merge the threadprivate attribute into NEWDECL. */
2127 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2129 if (DECL_THREAD_LOCAL_P (newdecl))
2130 error ("thread-local declaration of %q+D follows "
2131 "non-thread-local declaration", newdecl);
2132 else
2133 error ("non-thread-local declaration of %q+D follows "
2134 "thread-local declaration", newdecl);
2136 locate_old_decl (olddecl);
2137 return false;
2140 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2141 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2143 error ("redefinition of %q+D", newdecl);
2144 locate_old_decl (olddecl);
2145 return false;
2148 /* Objects declared at file scope: if the first declaration had
2149 external linkage (even if it was an external reference) the
2150 second must have external linkage as well, or the behavior is
2151 undefined. If the first declaration had internal linkage, then
2152 the second must too, or else be an external reference (in which
2153 case the composite declaration still has internal linkage).
2154 As for function declarations, we warn about the static-then-
2155 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2156 if (DECL_FILE_SCOPE_P (newdecl)
2157 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2159 if (DECL_EXTERNAL (newdecl))
2161 if (!DECL_FILE_SCOPE_P (olddecl))
2163 error ("extern declaration of %q+D follows "
2164 "declaration with no linkage", newdecl);
2165 locate_old_decl (olddecl);
2166 return false;
2168 else if (warn_traditional)
2170 warned |= warning (OPT_Wtraditional,
2171 "non-static declaration of %q+D "
2172 "follows static declaration", newdecl);
2175 else
2177 if (TREE_PUBLIC (newdecl))
2178 error ("non-static declaration of %q+D follows "
2179 "static declaration", newdecl);
2180 else
2181 error ("static declaration of %q+D follows "
2182 "non-static declaration", newdecl);
2184 locate_old_decl (olddecl);
2185 return false;
2188 /* Two objects with the same name declared at the same block
2189 scope must both be external references (6.7p3). */
2190 else if (!DECL_FILE_SCOPE_P (newdecl))
2192 if (DECL_EXTERNAL (newdecl))
2194 /* Extern with initializer at block scope, which will
2195 already have received an error. */
2197 else if (DECL_EXTERNAL (olddecl))
2199 error ("declaration of %q+D with no linkage follows "
2200 "extern declaration", newdecl);
2201 locate_old_decl (olddecl);
2203 else
2205 error ("redeclaration of %q+D with no linkage", newdecl);
2206 locate_old_decl (olddecl);
2209 return false;
2212 /* C++ does not permit a decl to appear multiple times at file
2213 scope. */
2214 if (warn_cxx_compat
2215 && DECL_FILE_SCOPE_P (newdecl)
2216 && !DECL_EXTERNAL (newdecl)
2217 && !DECL_EXTERNAL (olddecl))
2218 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2219 OPT_Wc___compat,
2220 ("duplicate declaration of %qD is "
2221 "invalid in C++"),
2222 newdecl);
2225 /* warnings */
2226 /* All decls must agree on a visibility. */
2227 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2228 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2229 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2231 warned |= warning (0, "redeclaration of %q+D with different visibility "
2232 "(old visibility preserved)", newdecl);
2235 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2236 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2237 else /* PARM_DECL, VAR_DECL */
2239 /* Redeclaration of a parameter is a constraint violation (this is
2240 not explicitly stated, but follows from C99 6.7p3 [no more than
2241 one declaration of the same identifier with no linkage in the
2242 same scope, except type tags] and 6.2.2p6 [parameters have no
2243 linkage]). We must check for a forward parameter declaration,
2244 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2245 an extension, the mandatory diagnostic for which is handled by
2246 mark_forward_parm_decls. */
2248 if (TREE_CODE (newdecl) == PARM_DECL
2249 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2251 error ("redefinition of parameter %q+D", newdecl);
2252 locate_old_decl (olddecl);
2253 return false;
2257 /* Optional warning for completely redundant decls. */
2258 if (!warned && !pedwarned
2259 && warn_redundant_decls
2260 /* Don't warn about a function declaration followed by a
2261 definition. */
2262 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2263 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2264 /* Don't warn about redundant redeclarations of builtins. */
2265 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2266 && !DECL_BUILT_IN (newdecl)
2267 && DECL_BUILT_IN (olddecl)
2268 && !C_DECL_DECLARED_BUILTIN (olddecl))
2269 /* Don't warn about an extern followed by a definition. */
2270 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2271 /* Don't warn about forward parameter decls. */
2272 && !(TREE_CODE (newdecl) == PARM_DECL
2273 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2274 /* Don't warn about a variable definition following a declaration. */
2275 && !(VAR_P (newdecl)
2276 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2278 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2279 newdecl);
2282 /* Report location of previous decl/defn. */
2283 if (warned || pedwarned)
2284 locate_old_decl (olddecl);
2286 #undef DECL_EXTERN_INLINE
2288 return retval;
2291 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2292 consistent with OLDDECL, but carries new information. Merge the
2293 new information into OLDDECL. This function issues no
2294 diagnostics. */
2296 static void
2297 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2299 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2300 && DECL_INITIAL (newdecl) != NULL_TREE);
2301 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2302 && prototype_p (TREE_TYPE (newdecl)));
2303 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2304 && prototype_p (TREE_TYPE (olddecl)));
2306 /* For real parm decl following a forward decl, rechain the old decl
2307 in its new location and clear TREE_ASM_WRITTEN (it's not a
2308 forward decl anymore). */
2309 if (TREE_CODE (newdecl) == PARM_DECL
2310 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2312 struct c_binding *b, **here;
2314 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2315 if ((*here)->decl == olddecl)
2316 goto found;
2317 gcc_unreachable ();
2319 found:
2320 b = *here;
2321 *here = b->prev;
2322 b->prev = current_scope->bindings;
2323 current_scope->bindings = b;
2325 TREE_ASM_WRITTEN (olddecl) = 0;
2328 DECL_ATTRIBUTES (newdecl)
2329 = targetm.merge_decl_attributes (olddecl, newdecl);
2331 /* For typedefs use the old type, as the new type's DECL_NAME points
2332 at newdecl, which will be ggc_freed. */
2333 if (TREE_CODE (newdecl) == TYPE_DECL)
2335 /* But NEWTYPE might have an attribute, honor that. */
2336 tree tem = newtype;
2337 newtype = oldtype;
2339 if (TYPE_USER_ALIGN (tem))
2341 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2342 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2343 TYPE_USER_ALIGN (newtype) = true;
2346 /* And remove the new type from the variants list. */
2347 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2349 tree remove = TREE_TYPE (newdecl);
2350 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2351 t = TYPE_NEXT_VARIANT (t))
2352 if (TYPE_NEXT_VARIANT (t) == remove)
2354 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2355 break;
2360 /* Merge the data types specified in the two decls. */
2361 TREE_TYPE (newdecl)
2362 = TREE_TYPE (olddecl)
2363 = composite_type (newtype, oldtype);
2365 /* Lay the type out, unless already done. */
2366 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2368 if (TREE_TYPE (newdecl) != error_mark_node)
2369 layout_type (TREE_TYPE (newdecl));
2370 if (TREE_CODE (newdecl) != FUNCTION_DECL
2371 && TREE_CODE (newdecl) != TYPE_DECL
2372 && TREE_CODE (newdecl) != CONST_DECL)
2373 layout_decl (newdecl, 0);
2375 else
2377 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2378 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2379 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2380 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2381 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2383 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2384 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2386 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2387 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2388 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2389 DECL_WARN_IF_NOT_ALIGN (olddecl));
2392 /* Keep the old rtl since we can safely use it. */
2393 if (HAS_RTL_P (olddecl))
2394 COPY_DECL_RTL (olddecl, newdecl);
2396 /* Merge the type qualifiers. */
2397 if (TREE_READONLY (newdecl))
2398 TREE_READONLY (olddecl) = 1;
2400 if (TREE_THIS_VOLATILE (newdecl))
2401 TREE_THIS_VOLATILE (olddecl) = 1;
2403 /* Merge deprecatedness. */
2404 if (TREE_DEPRECATED (newdecl))
2405 TREE_DEPRECATED (olddecl) = 1;
2407 /* If a decl is in a system header and the other isn't, keep the one on the
2408 system header. Otherwise, keep source location of definition rather than
2409 declaration and of prototype rather than non-prototype unless that
2410 prototype is built-in. */
2411 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2412 && DECL_IN_SYSTEM_HEADER (olddecl)
2413 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2414 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2415 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2416 && DECL_IN_SYSTEM_HEADER (newdecl)
2417 && !DECL_IN_SYSTEM_HEADER (olddecl))
2418 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2419 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2420 && DECL_INITIAL (olddecl) != NULL_TREE)
2421 || (old_is_prototype && !new_is_prototype
2422 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2423 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2425 /* Merge the initialization information. */
2426 if (DECL_INITIAL (newdecl) == NULL_TREE)
2427 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2429 /* Merge the threadprivate attribute. */
2430 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2431 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2433 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2435 /* Copy the assembler name.
2436 Currently, it can only be defined in the prototype. */
2437 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2439 /* Use visibility of whichever declaration had it specified */
2440 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2442 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2443 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2446 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2448 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2449 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2450 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2451 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2452 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2453 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2454 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2455 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2456 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2457 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2458 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2461 /* Merge the storage class information. */
2462 merge_weak (newdecl, olddecl);
2464 /* For functions, static overrides non-static. */
2465 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2467 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2468 /* This is since we don't automatically
2469 copy the attributes of NEWDECL into OLDDECL. */
2470 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2471 /* If this clears `static', clear it in the identifier too. */
2472 if (!TREE_PUBLIC (olddecl))
2473 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2477 /* In c99, 'extern' declaration before (or after) 'inline' means this
2478 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2479 is present. */
2480 if (TREE_CODE (newdecl) == FUNCTION_DECL
2481 && !flag_gnu89_inline
2482 && (DECL_DECLARED_INLINE_P (newdecl)
2483 || DECL_DECLARED_INLINE_P (olddecl))
2484 && (!DECL_DECLARED_INLINE_P (newdecl)
2485 || !DECL_DECLARED_INLINE_P (olddecl)
2486 || !DECL_EXTERNAL (olddecl))
2487 && DECL_EXTERNAL (newdecl)
2488 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2489 && !current_function_decl)
2490 DECL_EXTERNAL (newdecl) = 0;
2492 /* An inline definition following a static declaration is not
2493 DECL_EXTERNAL. */
2494 if (new_is_definition
2495 && (DECL_DECLARED_INLINE_P (newdecl)
2496 || DECL_DECLARED_INLINE_P (olddecl))
2497 && !TREE_PUBLIC (olddecl))
2498 DECL_EXTERNAL (newdecl) = 0;
2500 if (DECL_EXTERNAL (newdecl))
2502 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2503 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2505 /* An extern decl does not override previous storage class. */
2506 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2507 if (!DECL_EXTERNAL (newdecl))
2509 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2510 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2513 else
2515 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2516 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2519 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2521 /* If we're redefining a function previously defined as extern
2522 inline, make sure we emit debug info for the inline before we
2523 throw it away, in case it was inlined into a function that
2524 hasn't been written out yet. */
2525 if (new_is_definition && DECL_INITIAL (olddecl))
2526 /* The new defn must not be inline. */
2527 DECL_UNINLINABLE (newdecl) = 1;
2528 else
2530 /* If either decl says `inline', this fn is inline, unless
2531 its definition was passed already. */
2532 if (DECL_DECLARED_INLINE_P (newdecl)
2533 || DECL_DECLARED_INLINE_P (olddecl))
2534 DECL_DECLARED_INLINE_P (newdecl) = 1;
2536 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2537 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2539 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2540 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2541 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2542 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2545 if (DECL_BUILT_IN (olddecl))
2547 /* If redeclaring a builtin function, it stays built in.
2548 But it gets tagged as having been declared. */
2549 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2550 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2551 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2552 if (new_is_prototype)
2554 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2555 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2557 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2558 switch (fncode)
2560 /* If a compatible prototype of these builtin functions
2561 is seen, assume the runtime implements it with the
2562 expected semantics. */
2563 case BUILT_IN_STPCPY:
2564 if (builtin_decl_explicit_p (fncode))
2565 set_builtin_decl_implicit_p (fncode, true);
2566 break;
2567 default:
2568 if (builtin_decl_explicit_p (fncode))
2569 set_builtin_decl_declared_p (fncode, true);
2570 break;
2574 else
2575 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2576 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2579 /* Preserve function specific target and optimization options */
2580 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2581 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2582 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2583 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2585 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2586 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2587 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2588 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2590 /* Also preserve various other info from the definition. */
2591 if (!new_is_definition)
2593 tree t;
2594 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2595 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2596 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2597 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2598 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2599 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2600 DECL_CONTEXT (t) = newdecl;
2602 /* See if we've got a function to instantiate from. */
2603 if (DECL_SAVED_TREE (olddecl))
2604 DECL_ABSTRACT_ORIGIN (newdecl)
2605 = DECL_ABSTRACT_ORIGIN (olddecl);
2609 /* Merge the USED information. */
2610 if (TREE_USED (olddecl))
2611 TREE_USED (newdecl) = 1;
2612 else if (TREE_USED (newdecl))
2613 TREE_USED (olddecl) = 1;
2614 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2615 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2616 if (DECL_PRESERVE_P (olddecl))
2617 DECL_PRESERVE_P (newdecl) = 1;
2618 else if (DECL_PRESERVE_P (newdecl))
2619 DECL_PRESERVE_P (olddecl) = 1;
2621 /* Merge DECL_COMMON */
2622 if (VAR_P (olddecl) && VAR_P (newdecl)
2623 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2624 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2625 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2627 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2628 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2629 DECL_ARGUMENTS (if appropriate). */
2631 unsigned olddecl_uid = DECL_UID (olddecl);
2632 tree olddecl_context = DECL_CONTEXT (olddecl);
2633 tree olddecl_arguments = NULL;
2634 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2635 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2637 memcpy ((char *) olddecl + sizeof (struct tree_common),
2638 (char *) newdecl + sizeof (struct tree_common),
2639 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2640 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2641 switch (TREE_CODE (olddecl))
2643 case FUNCTION_DECL:
2644 case VAR_DECL:
2646 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2648 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2649 (char *) newdecl + sizeof (struct tree_decl_common),
2650 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2651 olddecl->decl_with_vis.symtab_node = snode;
2653 if ((DECL_EXTERNAL (olddecl)
2654 || TREE_PUBLIC (olddecl)
2655 || TREE_STATIC (olddecl))
2656 && DECL_SECTION_NAME (newdecl) != NULL)
2657 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2659 /* This isn't quite correct for something like
2660 int __thread x attribute ((tls_model ("local-exec")));
2661 extern int __thread x;
2662 as we'll lose the "local-exec" model. */
2663 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2664 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2665 break;
2668 case FIELD_DECL:
2669 case PARM_DECL:
2670 case LABEL_DECL:
2671 case RESULT_DECL:
2672 case CONST_DECL:
2673 case TYPE_DECL:
2674 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2675 (char *) newdecl + sizeof (struct tree_decl_common),
2676 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2677 break;
2679 default:
2681 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2682 (char *) newdecl + sizeof (struct tree_decl_common),
2683 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2685 DECL_UID (olddecl) = olddecl_uid;
2686 DECL_CONTEXT (olddecl) = olddecl_context;
2687 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2688 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2691 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2692 so that encode_section_info has a chance to look at the new decl
2693 flags and attributes. */
2694 if (DECL_RTL_SET_P (olddecl)
2695 && (TREE_CODE (olddecl) == FUNCTION_DECL
2696 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2697 make_decl_rtl (olddecl);
2700 /* Handle when a new declaration NEWDECL has the same name as an old
2701 one OLDDECL in the same binding contour. Prints an error message
2702 if appropriate.
2704 If safely possible, alter OLDDECL to look like NEWDECL, and return
2705 true. Otherwise, return false. */
2707 static bool
2708 duplicate_decls (tree newdecl, tree olddecl)
2710 tree newtype = NULL, oldtype = NULL;
2712 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2714 /* Avoid `unused variable' and other warnings for OLDDECL. */
2715 TREE_NO_WARNING (olddecl) = 1;
2716 return false;
2719 merge_decls (newdecl, olddecl, newtype, oldtype);
2721 /* The NEWDECL will no longer be needed.
2723 Before releasing the node, be sure to remove function from symbol
2724 table that might have been inserted there to record comdat group.
2725 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2726 structure is shared in between NEWDECL and OLDECL. */
2727 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2728 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2729 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2731 struct symtab_node *snode = symtab_node::get (newdecl);
2732 if (snode)
2733 snode->remove ();
2735 ggc_free (newdecl);
2736 return true;
2740 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2741 static void
2742 warn_if_shadowing (tree new_decl)
2744 struct c_binding *b;
2746 /* Shadow warnings wanted? */
2747 if (!(warn_shadow
2748 || warn_shadow_local
2749 || warn_shadow_compatible_local)
2750 /* No shadow warnings for internally generated vars. */
2751 || DECL_IS_BUILTIN (new_decl)
2752 /* No shadow warnings for vars made for inlining. */
2753 || DECL_FROM_INLINE (new_decl))
2754 return;
2756 /* Is anything being shadowed? Invisible decls do not count. */
2757 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2758 if (b->decl && b->decl != new_decl && !b->invisible
2759 && (b->decl == error_mark_node
2760 || diagnostic_report_warnings_p (global_dc,
2761 DECL_SOURCE_LOCATION (b->decl))))
2763 tree old_decl = b->decl;
2764 bool warned = false;
2766 if (old_decl == error_mark_node)
2768 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2769 "non-variable", new_decl);
2770 break;
2772 else if (TREE_CODE (old_decl) == PARM_DECL)
2774 enum opt_code warning_code;
2776 /* If '-Wshadow=compatible-local' is specified without other
2777 -Wshadow= flags, we will warn only when the types of the
2778 shadowing variable (i.e. new_decl) and the shadowed variable
2779 (old_decl) are compatible. */
2780 if (warn_shadow)
2781 warning_code = OPT_Wshadow;
2782 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2783 warning_code = OPT_Wshadow_compatible_local;
2784 else
2785 warning_code = OPT_Wshadow_local;
2786 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2787 "declaration of %qD shadows a parameter",
2788 new_decl);
2790 else if (DECL_FILE_SCOPE_P (old_decl))
2792 /* Do not warn if a variable shadows a function, unless
2793 the variable is a function or a pointer-to-function. */
2794 if (TREE_CODE (old_decl) == FUNCTION_DECL
2795 && TREE_CODE (new_decl) != FUNCTION_DECL
2796 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2797 continue;
2799 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2800 "declaration of %qD shadows a global "
2801 "declaration",
2802 new_decl);
2804 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2805 && DECL_BUILT_IN (old_decl))
2807 warning (OPT_Wshadow, "declaration of %q+D shadows "
2808 "a built-in function", new_decl);
2809 break;
2811 else
2813 enum opt_code warning_code;
2815 /* If '-Wshadow=compatible-local' is specified without other
2816 -Wshadow= flags, we will warn only when the types of the
2817 shadowing variable (i.e. new_decl) and the shadowed variable
2818 (old_decl) are compatible. */
2819 if (warn_shadow)
2820 warning_code = OPT_Wshadow;
2821 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2822 warning_code = OPT_Wshadow_compatible_local;
2823 else
2824 warning_code = OPT_Wshadow_local;
2825 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2826 "declaration of %qD shadows a previous local",
2827 new_decl);
2830 if (warned)
2831 inform (DECL_SOURCE_LOCATION (old_decl),
2832 "shadowed declaration is here");
2834 break;
2838 /* Record a decl-node X as belonging to the current lexical scope.
2839 Check for errors (such as an incompatible declaration for the same
2840 name already seen in the same scope).
2842 Returns either X or an old decl for the same name.
2843 If an old decl is returned, it may have been smashed
2844 to agree with what X says. */
2846 tree
2847 pushdecl (tree x)
2849 tree name = DECL_NAME (x);
2850 struct c_scope *scope = current_scope;
2851 struct c_binding *b;
2852 bool nested = false;
2853 location_t locus = DECL_SOURCE_LOCATION (x);
2855 /* Must set DECL_CONTEXT for everything not at file scope or
2856 DECL_FILE_SCOPE_P won't work. Local externs don't count
2857 unless they have initializers (which generate code). */
2858 if (current_function_decl
2859 && (!VAR_OR_FUNCTION_DECL_P (x)
2860 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2861 DECL_CONTEXT (x) = current_function_decl;
2863 /* Anonymous decls are just inserted in the scope. */
2864 if (!name)
2866 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2867 locus);
2868 return x;
2871 /* First, see if there is another declaration with the same name in
2872 the current scope. If there is, duplicate_decls may do all the
2873 work for us. If duplicate_decls returns false, that indicates
2874 two incompatible decls in the same scope; we are to silently
2875 replace the old one (duplicate_decls has issued all appropriate
2876 diagnostics). In particular, we should not consider possible
2877 duplicates in the external scope, or shadowing. */
2878 b = I_SYMBOL_BINDING (name);
2879 if (b && B_IN_SCOPE (b, scope))
2881 struct c_binding *b_ext, *b_use;
2882 tree type = TREE_TYPE (x);
2883 tree visdecl = b->decl;
2884 tree vistype = TREE_TYPE (visdecl);
2885 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2886 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2887 b->inner_comp = false;
2888 b_use = b;
2889 b_ext = b;
2890 /* If this is an external linkage declaration, we should check
2891 for compatibility with the type in the external scope before
2892 setting the type at this scope based on the visible
2893 information only. */
2894 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2896 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2897 b_ext = b_ext->shadowed;
2898 if (b_ext)
2900 b_use = b_ext;
2901 if (b_use->u.type)
2902 TREE_TYPE (b_use->decl) = b_use->u.type;
2905 if (duplicate_decls (x, b_use->decl))
2907 if (b_use != b)
2909 /* Save the updated type in the external scope and
2910 restore the proper type for this scope. */
2911 tree thistype;
2912 if (comptypes (vistype, type))
2913 thistype = composite_type (vistype, type);
2914 else
2915 thistype = TREE_TYPE (b_use->decl);
2916 b_use->u.type = TREE_TYPE (b_use->decl);
2917 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2918 && DECL_BUILT_IN (b_use->decl))
2919 thistype
2920 = build_type_attribute_variant (thistype,
2921 TYPE_ATTRIBUTES
2922 (b_use->u.type));
2923 TREE_TYPE (b_use->decl) = thistype;
2925 return b_use->decl;
2927 else
2928 goto skip_external_and_shadow_checks;
2931 /* All declarations with external linkage, and all external
2932 references, go in the external scope, no matter what scope is
2933 current. However, the binding in that scope is ignored for
2934 purposes of normal name lookup. A separate binding structure is
2935 created in the requested scope; this governs the normal
2936 visibility of the symbol.
2938 The binding in the externals scope is used exclusively for
2939 detecting duplicate declarations of the same object, no matter
2940 what scope they are in; this is what we do here. (C99 6.2.7p2:
2941 All declarations that refer to the same object or function shall
2942 have compatible type; otherwise, the behavior is undefined.) */
2943 if (DECL_EXTERNAL (x) || scope == file_scope)
2945 tree type = TREE_TYPE (x);
2946 tree vistype = NULL_TREE;
2947 tree visdecl = NULL_TREE;
2948 bool type_saved = false;
2949 if (b && !B_IN_EXTERNAL_SCOPE (b)
2950 && VAR_OR_FUNCTION_DECL_P (b->decl)
2951 && DECL_FILE_SCOPE_P (b->decl))
2953 visdecl = b->decl;
2954 vistype = TREE_TYPE (visdecl);
2956 if (scope != file_scope
2957 && !DECL_IN_SYSTEM_HEADER (x))
2958 warning_at (locus, OPT_Wnested_externs,
2959 "nested extern declaration of %qD", x);
2961 while (b && !B_IN_EXTERNAL_SCOPE (b))
2963 /* If this decl might be modified, save its type. This is
2964 done here rather than when the decl is first bound
2965 because the type may change after first binding, through
2966 being completed or through attributes being added. If we
2967 encounter multiple such decls, only the first should have
2968 its type saved; the others will already have had their
2969 proper types saved and the types will not have changed as
2970 their scopes will not have been re-entered. */
2971 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2973 b->u.type = TREE_TYPE (b->decl);
2974 type_saved = true;
2976 if (B_IN_FILE_SCOPE (b)
2977 && VAR_P (b->decl)
2978 && TREE_STATIC (b->decl)
2979 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2980 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2981 && TREE_CODE (type) == ARRAY_TYPE
2982 && TYPE_DOMAIN (type)
2983 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2984 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2986 /* Array type completed in inner scope, which should be
2987 diagnosed if the completion does not have size 1 and
2988 it does not get completed in the file scope. */
2989 b->inner_comp = true;
2991 b = b->shadowed;
2994 /* If a matching external declaration has been found, set its
2995 type to the composite of all the types of that declaration.
2996 After the consistency checks, it will be reset to the
2997 composite of the visible types only. */
2998 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2999 && b->u.type)
3000 TREE_TYPE (b->decl) = b->u.type;
3002 /* The point of the same_translation_unit_p check here is,
3003 we want to detect a duplicate decl for a construct like
3004 foo() { extern bar(); } ... static bar(); but not if
3005 they are in different translation units. In any case,
3006 the static does not go in the externals scope. */
3007 if (b
3008 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3009 && duplicate_decls (x, b->decl))
3011 tree thistype;
3012 if (vistype)
3014 if (comptypes (vistype, type))
3015 thistype = composite_type (vistype, type);
3016 else
3017 thistype = TREE_TYPE (b->decl);
3019 else
3020 thistype = type;
3021 b->u.type = TREE_TYPE (b->decl);
3022 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3023 thistype
3024 = build_type_attribute_variant (thistype,
3025 TYPE_ATTRIBUTES (b->u.type));
3026 TREE_TYPE (b->decl) = thistype;
3027 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3028 locus);
3029 return b->decl;
3031 else if (TREE_PUBLIC (x))
3033 if (visdecl && !b && duplicate_decls (x, visdecl))
3035 /* An external declaration at block scope referring to a
3036 visible entity with internal linkage. The composite
3037 type will already be correct for this scope, so we
3038 just need to fall through to make the declaration in
3039 this scope. */
3040 nested = true;
3041 x = visdecl;
3043 else
3045 bind (name, x, external_scope, /*invisible=*/true,
3046 /*nested=*/false, locus);
3047 nested = true;
3052 if (TREE_CODE (x) != PARM_DECL)
3053 warn_if_shadowing (x);
3055 skip_external_and_shadow_checks:
3056 if (TREE_CODE (x) == TYPE_DECL)
3058 /* So this is a typedef, set its underlying type. */
3059 set_underlying_type (x);
3061 /* If X is a typedef defined in the current function, record it
3062 for the purpose of implementing the -Wunused-local-typedefs
3063 warning. */
3064 record_locally_defined_typedef (x);
3067 bind (name, x, scope, /*invisible=*/false, nested, locus);
3069 /* If x's type is incomplete because it's based on a
3070 structure or union which has not yet been fully declared,
3071 attach it to that structure or union type, so we can go
3072 back and complete the variable declaration later, if the
3073 structure or union gets fully declared.
3075 If the input is erroneous, we can have error_mark in the type
3076 slot (e.g. "f(void a, ...)") - that doesn't count as an
3077 incomplete type. */
3078 if (TREE_TYPE (x) != error_mark_node
3079 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3081 tree element = TREE_TYPE (x);
3083 while (TREE_CODE (element) == ARRAY_TYPE)
3084 element = TREE_TYPE (element);
3085 element = TYPE_MAIN_VARIANT (element);
3087 if (RECORD_OR_UNION_TYPE_P (element)
3088 && (TREE_CODE (x) != TYPE_DECL
3089 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3090 && !COMPLETE_TYPE_P (element))
3091 C_TYPE_INCOMPLETE_VARS (element)
3092 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3094 return x;
3098 /* Issue a warning about implicit function declaration. ID is the function
3099 identifier, OLDDECL is a declaration of the function in a different scope,
3100 or NULL_TREE. */
3102 static void
3103 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3105 if (!warn_implicit_function_declaration)
3106 return;
3108 bool warned;
3109 const char *hint = NULL;
3110 if (!olddecl)
3111 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME);
3113 if (flag_isoc99)
3115 if (hint)
3117 gcc_rich_location richloc (loc);
3118 richloc.add_fixit_replace (hint);
3119 warned = pedwarn_at_rich_loc
3120 (&richloc, OPT_Wimplicit_function_declaration,
3121 "implicit declaration of function %qE; did you mean %qs?",
3122 id, hint);
3124 else
3125 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3126 "implicit declaration of function %qE", id);
3128 else if (hint)
3130 gcc_rich_location richloc (loc);
3131 richloc.add_fixit_replace (hint);
3132 warned = warning_at_rich_loc
3133 (&richloc, OPT_Wimplicit_function_declaration,
3134 G_("implicit declaration of function %qE; did you mean %qs?"),
3135 id, hint);
3137 else
3138 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3139 G_("implicit declaration of function %qE"), id);
3141 if (olddecl && warned)
3142 locate_old_decl (olddecl);
3145 /* This function represents mapping of a function code FCODE
3146 to its respective header. */
3148 static const char *
3149 header_for_builtin_fn (enum built_in_function fcode)
3151 switch (fcode)
3153 CASE_FLT_FN (BUILT_IN_ACOS):
3154 CASE_FLT_FN (BUILT_IN_ACOSH):
3155 CASE_FLT_FN (BUILT_IN_ASIN):
3156 CASE_FLT_FN (BUILT_IN_ASINH):
3157 CASE_FLT_FN (BUILT_IN_ATAN):
3158 CASE_FLT_FN (BUILT_IN_ATANH):
3159 CASE_FLT_FN (BUILT_IN_ATAN2):
3160 CASE_FLT_FN (BUILT_IN_CBRT):
3161 CASE_FLT_FN (BUILT_IN_CEIL):
3162 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3163 CASE_FLT_FN (BUILT_IN_COS):
3164 CASE_FLT_FN (BUILT_IN_COSH):
3165 CASE_FLT_FN (BUILT_IN_ERF):
3166 CASE_FLT_FN (BUILT_IN_ERFC):
3167 CASE_FLT_FN (BUILT_IN_EXP):
3168 CASE_FLT_FN (BUILT_IN_EXP2):
3169 CASE_FLT_FN (BUILT_IN_EXPM1):
3170 CASE_FLT_FN (BUILT_IN_FABS):
3171 CASE_FLT_FN (BUILT_IN_FDIM):
3172 CASE_FLT_FN (BUILT_IN_FLOOR):
3173 CASE_FLT_FN (BUILT_IN_FMA):
3174 CASE_FLT_FN (BUILT_IN_FMAX):
3175 CASE_FLT_FN (BUILT_IN_FMIN):
3176 CASE_FLT_FN (BUILT_IN_FMOD):
3177 CASE_FLT_FN (BUILT_IN_FREXP):
3178 CASE_FLT_FN (BUILT_IN_HYPOT):
3179 CASE_FLT_FN (BUILT_IN_ILOGB):
3180 CASE_FLT_FN (BUILT_IN_LDEXP):
3181 CASE_FLT_FN (BUILT_IN_LGAMMA):
3182 CASE_FLT_FN (BUILT_IN_LLRINT):
3183 CASE_FLT_FN (BUILT_IN_LLROUND):
3184 CASE_FLT_FN (BUILT_IN_LOG):
3185 CASE_FLT_FN (BUILT_IN_LOG10):
3186 CASE_FLT_FN (BUILT_IN_LOG1P):
3187 CASE_FLT_FN (BUILT_IN_LOG2):
3188 CASE_FLT_FN (BUILT_IN_LOGB):
3189 CASE_FLT_FN (BUILT_IN_LRINT):
3190 CASE_FLT_FN (BUILT_IN_LROUND):
3191 CASE_FLT_FN (BUILT_IN_MODF):
3192 CASE_FLT_FN (BUILT_IN_NAN):
3193 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3194 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3195 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3196 CASE_FLT_FN (BUILT_IN_POW):
3197 CASE_FLT_FN (BUILT_IN_REMAINDER):
3198 CASE_FLT_FN (BUILT_IN_REMQUO):
3199 CASE_FLT_FN (BUILT_IN_RINT):
3200 CASE_FLT_FN (BUILT_IN_ROUND):
3201 CASE_FLT_FN (BUILT_IN_SCALBLN):
3202 CASE_FLT_FN (BUILT_IN_SCALBN):
3203 CASE_FLT_FN (BUILT_IN_SIN):
3204 CASE_FLT_FN (BUILT_IN_SINH):
3205 CASE_FLT_FN (BUILT_IN_SINCOS):
3206 CASE_FLT_FN (BUILT_IN_SQRT):
3207 CASE_FLT_FN (BUILT_IN_TAN):
3208 CASE_FLT_FN (BUILT_IN_TANH):
3209 CASE_FLT_FN (BUILT_IN_TGAMMA):
3210 CASE_FLT_FN (BUILT_IN_TRUNC):
3211 case BUILT_IN_ISINF:
3212 case BUILT_IN_ISNAN:
3213 return "<math.h>";
3214 CASE_FLT_FN (BUILT_IN_CABS):
3215 CASE_FLT_FN (BUILT_IN_CACOS):
3216 CASE_FLT_FN (BUILT_IN_CACOSH):
3217 CASE_FLT_FN (BUILT_IN_CARG):
3218 CASE_FLT_FN (BUILT_IN_CASIN):
3219 CASE_FLT_FN (BUILT_IN_CASINH):
3220 CASE_FLT_FN (BUILT_IN_CATAN):
3221 CASE_FLT_FN (BUILT_IN_CATANH):
3222 CASE_FLT_FN (BUILT_IN_CCOS):
3223 CASE_FLT_FN (BUILT_IN_CCOSH):
3224 CASE_FLT_FN (BUILT_IN_CEXP):
3225 CASE_FLT_FN (BUILT_IN_CIMAG):
3226 CASE_FLT_FN (BUILT_IN_CLOG):
3227 CASE_FLT_FN (BUILT_IN_CONJ):
3228 CASE_FLT_FN (BUILT_IN_CPOW):
3229 CASE_FLT_FN (BUILT_IN_CPROJ):
3230 CASE_FLT_FN (BUILT_IN_CREAL):
3231 CASE_FLT_FN (BUILT_IN_CSIN):
3232 CASE_FLT_FN (BUILT_IN_CSINH):
3233 CASE_FLT_FN (BUILT_IN_CSQRT):
3234 CASE_FLT_FN (BUILT_IN_CTAN):
3235 CASE_FLT_FN (BUILT_IN_CTANH):
3236 return "<complex.h>";
3237 case BUILT_IN_MEMCHR:
3238 case BUILT_IN_MEMCMP:
3239 case BUILT_IN_MEMCPY:
3240 case BUILT_IN_MEMMOVE:
3241 case BUILT_IN_MEMSET:
3242 case BUILT_IN_STRCAT:
3243 case BUILT_IN_STRCHR:
3244 case BUILT_IN_STRCMP:
3245 case BUILT_IN_STRCPY:
3246 case BUILT_IN_STRCSPN:
3247 case BUILT_IN_STRLEN:
3248 case BUILT_IN_STRNCAT:
3249 case BUILT_IN_STRNCMP:
3250 case BUILT_IN_STRNCPY:
3251 case BUILT_IN_STRPBRK:
3252 case BUILT_IN_STRRCHR:
3253 case BUILT_IN_STRSPN:
3254 case BUILT_IN_STRSTR:
3255 return "<string.h>";
3256 case BUILT_IN_FPRINTF:
3257 case BUILT_IN_PUTC:
3258 case BUILT_IN_FPUTC:
3259 case BUILT_IN_FPUTS:
3260 case BUILT_IN_FSCANF:
3261 case BUILT_IN_FWRITE:
3262 case BUILT_IN_PRINTF:
3263 case BUILT_IN_PUTCHAR:
3264 case BUILT_IN_PUTS:
3265 case BUILT_IN_SCANF:
3266 case BUILT_IN_SNPRINTF:
3267 case BUILT_IN_SPRINTF:
3268 case BUILT_IN_SSCANF:
3269 case BUILT_IN_VFPRINTF:
3270 case BUILT_IN_VFSCANF:
3271 case BUILT_IN_VPRINTF:
3272 case BUILT_IN_VSCANF:
3273 case BUILT_IN_VSNPRINTF:
3274 case BUILT_IN_VSPRINTF:
3275 case BUILT_IN_VSSCANF:
3276 return "<stdio.h>";
3277 case BUILT_IN_ISALNUM:
3278 case BUILT_IN_ISALPHA:
3279 case BUILT_IN_ISBLANK:
3280 case BUILT_IN_ISCNTRL:
3281 case BUILT_IN_ISDIGIT:
3282 case BUILT_IN_ISGRAPH:
3283 case BUILT_IN_ISLOWER:
3284 case BUILT_IN_ISPRINT:
3285 case BUILT_IN_ISPUNCT:
3286 case BUILT_IN_ISSPACE:
3287 case BUILT_IN_ISUPPER:
3288 case BUILT_IN_ISXDIGIT:
3289 case BUILT_IN_TOLOWER:
3290 case BUILT_IN_TOUPPER:
3291 return "<ctype.h>";
3292 case BUILT_IN_ISWALNUM:
3293 case BUILT_IN_ISWALPHA:
3294 case BUILT_IN_ISWBLANK:
3295 case BUILT_IN_ISWCNTRL:
3296 case BUILT_IN_ISWDIGIT:
3297 case BUILT_IN_ISWGRAPH:
3298 case BUILT_IN_ISWLOWER:
3299 case BUILT_IN_ISWPRINT:
3300 case BUILT_IN_ISWPUNCT:
3301 case BUILT_IN_ISWSPACE:
3302 case BUILT_IN_ISWUPPER:
3303 case BUILT_IN_ISWXDIGIT:
3304 case BUILT_IN_TOWLOWER:
3305 case BUILT_IN_TOWUPPER:
3306 return "<wctype.h>";
3307 case BUILT_IN_ABORT:
3308 case BUILT_IN_ABS:
3309 case BUILT_IN_CALLOC:
3310 case BUILT_IN_EXIT:
3311 case BUILT_IN_FREE:
3312 case BUILT_IN_LABS:
3313 case BUILT_IN_LLABS:
3314 case BUILT_IN_MALLOC:
3315 case BUILT_IN_REALLOC:
3316 case BUILT_IN__EXIT2:
3317 case BUILT_IN_ALIGNED_ALLOC:
3318 return "<stdlib.h>";
3319 case BUILT_IN_IMAXABS:
3320 return "<inttypes.h>";
3321 case BUILT_IN_STRFTIME:
3322 return "<time.h>";
3323 default:
3324 return NULL;
3328 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3329 function of type int (). */
3331 tree
3332 implicitly_declare (location_t loc, tree functionid)
3334 struct c_binding *b;
3335 tree decl = NULL_TREE;
3336 tree asmspec_tree;
3338 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3340 if (B_IN_SCOPE (b, external_scope))
3342 decl = b->decl;
3343 break;
3347 if (decl)
3349 if (TREE_CODE (decl) != FUNCTION_DECL)
3350 return decl;
3352 /* FIXME: Objective-C has weird not-really-builtin functions
3353 which are supposed to be visible automatically. They wind up
3354 in the external scope because they're pushed before the file
3355 scope gets created. Catch this here and rebind them into the
3356 file scope. */
3357 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3359 bind (functionid, decl, file_scope,
3360 /*invisible=*/false, /*nested=*/true,
3361 DECL_SOURCE_LOCATION (decl));
3362 return decl;
3364 else
3366 tree newtype = default_function_type;
3367 if (b->u.type)
3368 TREE_TYPE (decl) = b->u.type;
3369 /* Implicit declaration of a function already declared
3370 (somehow) in a different scope, or as a built-in.
3371 If this is the first time this has happened, warn;
3372 then recycle the old declaration but with the new type. */
3373 if (!C_DECL_IMPLICIT (decl))
3375 implicit_decl_warning (loc, functionid, decl);
3376 C_DECL_IMPLICIT (decl) = 1;
3378 if (DECL_BUILT_IN (decl))
3380 newtype = build_type_attribute_variant (newtype,
3381 TYPE_ATTRIBUTES
3382 (TREE_TYPE (decl)));
3383 if (!comptypes (newtype, TREE_TYPE (decl)))
3385 bool warned = warning_at (loc, 0, "incompatible implicit "
3386 "declaration of built-in "
3387 "function %qD", decl);
3388 /* See if we can hint which header to include. */
3389 const char *header
3390 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3391 if (header != NULL && warned)
3393 rich_location richloc (line_table, loc);
3394 maybe_add_include_fixit (&richloc, header);
3395 inform_at_rich_loc
3396 (&richloc,
3397 "include %qs or provide a declaration of %qD",
3398 header, decl);
3400 newtype = TREE_TYPE (decl);
3403 else
3405 if (!comptypes (newtype, TREE_TYPE (decl)))
3407 error_at (loc, "incompatible implicit declaration of "
3408 "function %qD", decl);
3409 locate_old_decl (decl);
3412 b->u.type = TREE_TYPE (decl);
3413 TREE_TYPE (decl) = newtype;
3414 bind (functionid, decl, current_scope,
3415 /*invisible=*/false, /*nested=*/true,
3416 DECL_SOURCE_LOCATION (decl));
3417 return decl;
3421 /* Not seen before. */
3422 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3423 DECL_EXTERNAL (decl) = 1;
3424 TREE_PUBLIC (decl) = 1;
3425 C_DECL_IMPLICIT (decl) = 1;
3426 implicit_decl_warning (loc, functionid, 0);
3427 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3428 if (asmspec_tree)
3429 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3431 /* C89 says implicit declarations are in the innermost block.
3432 So we record the decl in the standard fashion. */
3433 decl = pushdecl (decl);
3435 /* No need to call objc_check_decl here - it's a function type. */
3436 rest_of_decl_compilation (decl, 0, 0);
3438 /* Write a record describing this implicit function declaration
3439 to the prototypes file (if requested). */
3440 gen_aux_info_record (decl, 0, 1, 0);
3442 /* Possibly apply some default attributes to this implicit declaration. */
3443 decl_attributes (&decl, NULL_TREE, 0);
3445 return decl;
3448 /* Issue an error message for a reference to an undeclared variable
3449 ID, including a reference to a builtin outside of function-call
3450 context. Establish a binding of the identifier to error_mark_node
3451 in an appropriate scope, which will suppress further errors for the
3452 same identifier. The error message should be given location LOC. */
3453 void
3454 undeclared_variable (location_t loc, tree id)
3456 static bool already = false;
3457 struct c_scope *scope;
3459 if (current_function_decl == NULL_TREE)
3461 const char *guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME);
3462 if (guessed_id)
3464 gcc_rich_location richloc (loc);
3465 richloc.add_fixit_replace (guessed_id);
3466 error_at_rich_loc (&richloc,
3467 "%qE undeclared here (not in a function);"
3468 " did you mean %qs?",
3469 id, guessed_id);
3471 else
3472 error_at (loc, "%qE undeclared here (not in a function)", id);
3473 scope = current_scope;
3475 else
3477 if (!objc_diagnose_private_ivar (id))
3479 const char *guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME);
3480 if (guessed_id)
3482 gcc_rich_location richloc (loc);
3483 richloc.add_fixit_replace (guessed_id);
3484 error_at_rich_loc
3485 (&richloc,
3486 "%qE undeclared (first use in this function);"
3487 " did you mean %qs?",
3488 id, guessed_id);
3490 else
3491 error_at (loc, "%qE undeclared (first use in this function)", id);
3493 if (!already)
3495 inform (loc, "each undeclared identifier is reported only"
3496 " once for each function it appears in");
3497 already = true;
3500 /* If we are parsing old-style parameter decls, current_function_decl
3501 will be nonnull but current_function_scope will be null. */
3502 scope = current_function_scope ? current_function_scope : current_scope;
3504 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3505 UNKNOWN_LOCATION);
3508 /* Subroutine of lookup_label, declare_label, define_label: construct a
3509 LABEL_DECL with all the proper frills. Also create a struct
3510 c_label_vars initialized for the current scope. */
3512 static tree
3513 make_label (location_t location, tree name, bool defining,
3514 struct c_label_vars **p_label_vars)
3516 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3517 DECL_CONTEXT (label) = current_function_decl;
3518 SET_DECL_MODE (label, VOIDmode);
3520 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3521 label_vars->shadowed = NULL;
3522 set_spot_bindings (&label_vars->label_bindings, defining);
3523 label_vars->decls_in_scope = make_tree_vector ();
3524 label_vars->gotos = NULL;
3525 *p_label_vars = label_vars;
3527 return label;
3530 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3531 Create one if none exists so far for the current function.
3532 This is called when a label is used in a goto expression or
3533 has its address taken. */
3535 tree
3536 lookup_label (tree name)
3538 tree label;
3539 struct c_label_vars *label_vars;
3541 if (current_function_scope == 0)
3543 error ("label %qE referenced outside of any function", name);
3544 return NULL_TREE;
3547 /* Use a label already defined or ref'd with this name, but not if
3548 it is inherited from a containing function and wasn't declared
3549 using __label__. */
3550 label = I_LABEL_DECL (name);
3551 if (label && (DECL_CONTEXT (label) == current_function_decl
3552 || C_DECLARED_LABEL_FLAG (label)))
3554 /* If the label has only been declared, update its apparent
3555 location to point here, for better diagnostics if it
3556 turns out not to have been defined. */
3557 if (DECL_INITIAL (label) == NULL_TREE)
3558 DECL_SOURCE_LOCATION (label) = input_location;
3559 return label;
3562 /* No label binding for that identifier; make one. */
3563 label = make_label (input_location, name, false, &label_vars);
3565 /* Ordinary labels go in the current function scope. */
3566 bind_label (name, label, current_function_scope, label_vars);
3568 return label;
3571 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3572 to LABEL. */
3574 static void
3575 warn_about_goto (location_t goto_loc, tree label, tree decl)
3577 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3578 error_at (goto_loc,
3579 "jump into scope of identifier with variably modified type");
3580 else
3581 warning_at (goto_loc, OPT_Wjump_misses_init,
3582 "jump skips variable initialization");
3583 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3584 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3587 /* Look up a label because of a goto statement. This is like
3588 lookup_label, but also issues any appropriate warnings. */
3590 tree
3591 lookup_label_for_goto (location_t loc, tree name)
3593 tree label;
3594 struct c_label_vars *label_vars;
3595 unsigned int ix;
3596 tree decl;
3598 label = lookup_label (name);
3599 if (label == NULL_TREE)
3600 return NULL_TREE;
3602 /* If we are jumping to a different function, we can't issue any
3603 useful warnings. */
3604 if (DECL_CONTEXT (label) != current_function_decl)
3606 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3607 return label;
3610 label_vars = I_LABEL_BINDING (name)->u.label;
3612 /* If the label has not yet been defined, then push this goto on a
3613 list for possible later warnings. */
3614 if (label_vars->label_bindings.scope == NULL)
3616 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3618 g->loc = loc;
3619 set_spot_bindings (&g->goto_bindings, true);
3620 vec_safe_push (label_vars->gotos, g);
3621 return label;
3624 /* If there are any decls in label_vars->decls_in_scope, then this
3625 goto has missed the declaration of the decl. This happens for a
3626 case like
3627 int i = 1;
3628 lab:
3630 goto lab;
3631 Issue a warning or error. */
3632 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3633 warn_about_goto (loc, label, decl);
3635 if (label_vars->label_bindings.left_stmt_expr)
3637 error_at (loc, "jump into statement expression");
3638 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3641 return label;
3644 /* Make a label named NAME in the current function, shadowing silently
3645 any that may be inherited from containing functions or containing
3646 scopes. This is called for __label__ declarations. */
3648 tree
3649 declare_label (tree name)
3651 struct c_binding *b = I_LABEL_BINDING (name);
3652 tree label;
3653 struct c_label_vars *label_vars;
3655 /* Check to make sure that the label hasn't already been declared
3656 at this scope */
3657 if (b && B_IN_CURRENT_SCOPE (b))
3659 error ("duplicate label declaration %qE", name);
3660 locate_old_decl (b->decl);
3662 /* Just use the previous declaration. */
3663 return b->decl;
3666 label = make_label (input_location, name, false, &label_vars);
3667 C_DECLARED_LABEL_FLAG (label) = 1;
3669 /* Declared labels go in the current scope. */
3670 bind_label (name, label, current_scope, label_vars);
3672 return label;
3675 /* When we define a label, issue any appropriate warnings if there are
3676 any gotos earlier in the function which jump to this label. */
3678 static void
3679 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3681 unsigned int ix;
3682 struct c_goto_bindings *g;
3684 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3686 struct c_binding *b;
3687 struct c_scope *scope;
3689 /* We have a goto to this label. The goto is going forward. In
3690 g->scope, the goto is going to skip any binding which was
3691 defined after g->bindings_in_scope. */
3692 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3694 for (b = g->goto_bindings.scope->bindings;
3695 b != g->goto_bindings.bindings_in_scope;
3696 b = b->prev)
3698 if (decl_jump_unsafe (b->decl))
3699 warn_about_goto (g->loc, label, b->decl);
3703 /* We also need to warn about decls defined in any scopes
3704 between the scope of the label and the scope of the goto. */
3705 for (scope = label_vars->label_bindings.scope;
3706 scope != g->goto_bindings.scope;
3707 scope = scope->outer)
3709 gcc_assert (scope != NULL);
3710 if (scope->has_jump_unsafe_decl)
3712 if (scope == label_vars->label_bindings.scope)
3713 b = label_vars->label_bindings.bindings_in_scope;
3714 else
3715 b = scope->bindings;
3716 for (; b != NULL; b = b->prev)
3718 if (decl_jump_unsafe (b->decl))
3719 warn_about_goto (g->loc, label, b->decl);
3724 if (g->goto_bindings.stmt_exprs > 0)
3726 error_at (g->loc, "jump into statement expression");
3727 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3728 label);
3732 /* Now that the label is defined, we will issue warnings about
3733 subsequent gotos to this label when we see them. */
3734 vec_safe_truncate (label_vars->gotos, 0);
3735 label_vars->gotos = NULL;
3738 /* Define a label, specifying the location in the source file.
3739 Return the LABEL_DECL node for the label, if the definition is valid.
3740 Otherwise return NULL_TREE. */
3742 tree
3743 define_label (location_t location, tree name)
3745 /* Find any preexisting label with this name. It is an error
3746 if that label has already been defined in this function, or
3747 if there is a containing function with a declared label with
3748 the same name. */
3749 tree label = I_LABEL_DECL (name);
3751 if (label
3752 && ((DECL_CONTEXT (label) == current_function_decl
3753 && DECL_INITIAL (label) != NULL_TREE)
3754 || (DECL_CONTEXT (label) != current_function_decl
3755 && C_DECLARED_LABEL_FLAG (label))))
3757 error_at (location, "duplicate label %qD", label);
3758 locate_old_decl (label);
3759 return NULL_TREE;
3761 else if (label && DECL_CONTEXT (label) == current_function_decl)
3763 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3765 /* The label has been used or declared already in this function,
3766 but not defined. Update its location to point to this
3767 definition. */
3768 DECL_SOURCE_LOCATION (label) = location;
3769 set_spot_bindings (&label_vars->label_bindings, true);
3771 /* Issue warnings as required about any goto statements from
3772 earlier in the function. */
3773 check_earlier_gotos (label, label_vars);
3775 else
3777 struct c_label_vars *label_vars;
3779 /* No label binding for that identifier; make one. */
3780 label = make_label (location, name, true, &label_vars);
3782 /* Ordinary labels go in the current function scope. */
3783 bind_label (name, label, current_function_scope, label_vars);
3786 if (!in_system_header_at (input_location) && lookup_name (name))
3787 warning_at (location, OPT_Wtraditional,
3788 "traditional C lacks a separate namespace "
3789 "for labels, identifier %qE conflicts", name);
3791 /* Mark label as having been defined. */
3792 DECL_INITIAL (label) = error_mark_node;
3793 return label;
3796 /* Get the bindings for a new switch statement. This is used to issue
3797 warnings as appropriate for jumps from the switch to case or
3798 default labels. */
3800 struct c_spot_bindings *
3801 c_get_switch_bindings (void)
3803 struct c_spot_bindings *switch_bindings;
3805 switch_bindings = XNEW (struct c_spot_bindings);
3806 set_spot_bindings (switch_bindings, true);
3807 return switch_bindings;
3810 void
3811 c_release_switch_bindings (struct c_spot_bindings *bindings)
3813 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3814 XDELETE (bindings);
3817 /* This is called at the point of a case or default label to issue
3818 warnings about decls as needed. It returns true if it found an
3819 error, not just a warning. */
3821 bool
3822 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3823 location_t switch_loc, location_t case_loc)
3825 bool saw_error;
3826 struct c_scope *scope;
3828 saw_error = false;
3829 for (scope = current_scope;
3830 scope != switch_bindings->scope;
3831 scope = scope->outer)
3833 struct c_binding *b;
3835 gcc_assert (scope != NULL);
3837 if (!scope->has_jump_unsafe_decl)
3838 continue;
3840 for (b = scope->bindings; b != NULL; b = b->prev)
3842 if (decl_jump_unsafe (b->decl))
3844 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3846 saw_error = true;
3847 error_at (case_loc,
3848 ("switch jumps into scope of identifier with "
3849 "variably modified type"));
3851 else
3852 warning_at (case_loc, OPT_Wjump_misses_init,
3853 "switch jumps over variable initialization");
3854 inform (switch_loc, "switch starts here");
3855 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3856 b->decl);
3861 if (switch_bindings->stmt_exprs > 0)
3863 saw_error = true;
3864 error_at (case_loc, "switch jumps into statement expression");
3865 inform (switch_loc, "switch starts here");
3868 return saw_error;
3871 /* Given NAME, an IDENTIFIER_NODE,
3872 return the structure (or union or enum) definition for that name.
3873 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3874 CODE says which kind of type the caller wants;
3875 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3876 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3877 location where the tag was defined.
3878 If the wrong kind of type is found, an error is reported. */
3880 static tree
3881 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3882 location_t *ploc)
3884 struct c_binding *b = I_TAG_BINDING (name);
3885 bool thislevel = false;
3887 if (!b || !b->decl)
3888 return NULL_TREE;
3890 /* We only care about whether it's in this level if
3891 thislevel_only was set or it might be a type clash. */
3892 if (thislevel_only || TREE_CODE (b->decl) != code)
3894 /* For our purposes, a tag in the external scope is the same as
3895 a tag in the file scope. (Primarily relevant to Objective-C
3896 and its builtin structure tags, which get pushed before the
3897 file scope is created.) */
3898 if (B_IN_CURRENT_SCOPE (b)
3899 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3900 thislevel = true;
3903 if (thislevel_only && !thislevel)
3904 return NULL_TREE;
3906 if (TREE_CODE (b->decl) != code)
3908 /* Definition isn't the kind we were looking for. */
3909 pending_invalid_xref = name;
3910 pending_invalid_xref_location = input_location;
3912 /* If in the same binding level as a declaration as a tag
3913 of a different type, this must not be allowed to
3914 shadow that tag, so give the error immediately.
3915 (For example, "struct foo; union foo;" is invalid.) */
3916 if (thislevel)
3917 pending_xref_error ();
3920 if (ploc != NULL)
3921 *ploc = b->locus;
3923 return b->decl;
3926 /* Return true if a definition exists for NAME with code CODE. */
3928 bool
3929 tag_exists_p (enum tree_code code, tree name)
3931 struct c_binding *b = I_TAG_BINDING (name);
3933 if (b == NULL || b->decl == NULL_TREE)
3934 return false;
3935 return TREE_CODE (b->decl) == code;
3938 /* Print an error message now
3939 for a recent invalid struct, union or enum cross reference.
3940 We don't print them immediately because they are not invalid
3941 when used in the `struct foo;' construct for shadowing. */
3943 void
3944 pending_xref_error (void)
3946 if (pending_invalid_xref != NULL_TREE)
3947 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3948 pending_invalid_xref);
3949 pending_invalid_xref = NULL_TREE;
3953 /* Look up NAME in the current scope and its superiors
3954 in the namespace of variables, functions and typedefs.
3955 Return a ..._DECL node of some kind representing its definition,
3956 or return NULL_TREE if it is undefined. */
3958 tree
3959 lookup_name (tree name)
3961 struct c_binding *b = I_SYMBOL_BINDING (name);
3962 if (b && !b->invisible)
3964 maybe_record_typedef_use (b->decl);
3965 return b->decl;
3967 return NULL_TREE;
3970 /* Similar to `lookup_name' but look only at the indicated scope. */
3972 static tree
3973 lookup_name_in_scope (tree name, struct c_scope *scope)
3975 struct c_binding *b;
3977 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3978 if (B_IN_SCOPE (b, scope))
3979 return b->decl;
3980 return NULL_TREE;
3983 /* Look for the closest match for NAME within the currently valid
3984 scopes.
3986 This finds the identifier with the lowest Levenshtein distance to
3987 NAME. If there are multiple candidates with equal minimal distance,
3988 the first one found is returned. Scopes are searched from innermost
3989 outwards, and within a scope in reverse order of declaration, thus
3990 benefiting candidates "near" to the current scope.
3992 The function also looks for similar macro names to NAME, since a
3993 misspelled macro name will not be expanded, and hence looks like an
3994 identifier to the C frontend.
3996 It also looks for start_typename keywords, to detect "singed" vs "signed"
3997 typos. */
3999 const char *
4000 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind)
4002 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4004 best_match<tree, tree> bm (name);
4006 /* Look within currently valid scopes. */
4007 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4008 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4010 if (!binding->id || binding->invisible)
4011 continue;
4012 /* Don't use bindings from implicitly declared functions,
4013 as they were likely misspellings themselves. */
4014 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4015 if (C_DECL_IMPLICIT (binding->decl))
4016 continue;
4017 switch (kind)
4019 case FUZZY_LOOKUP_TYPENAME:
4020 if (TREE_CODE (binding->decl) != TYPE_DECL)
4021 continue;
4022 break;
4024 case FUZZY_LOOKUP_FUNCTION_NAME:
4025 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4027 /* Allow function pointers. */
4028 if ((VAR_P (binding->decl)
4029 || TREE_CODE (binding->decl) == PARM_DECL)
4030 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4031 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4032 == FUNCTION_TYPE))
4033 break;
4034 continue;
4036 break;
4038 default:
4039 break;
4041 bm.consider (binding->id);
4044 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4046 x = SOME_OTHER_MACRO (y);
4047 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4048 as a misspelled identifier.
4050 Use the best distance so far so that a candidate is only set if
4051 a macro is better than anything so far. This allows early rejection
4052 (without calculating the edit distance) of macro names that must have
4053 distance >= bm.get_best_distance (), and means that we only get a
4054 non-NULL result for best_macro_match if it's better than any of
4055 the identifiers already checked, which avoids needless creation
4056 of identifiers for macro hashnodes. */
4057 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4058 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4059 /* If a macro is the closest so far to NAME, use it, creating an
4060 identifier tree node for it. */
4061 if (best_macro)
4063 const char *id = (const char *)best_macro->ident.str;
4064 tree macro_as_identifier
4065 = get_identifier_with_length (id, best_macro->ident.len);
4066 bm.set_best_so_far (macro_as_identifier,
4067 bmm.get_best_distance (),
4068 bmm.get_best_candidate_length ());
4071 /* Try the "start_typename" keywords to detect
4072 "singed" vs "signed" typos. */
4073 if (kind == FUZZY_LOOKUP_TYPENAME)
4075 for (unsigned i = 0; i < num_c_common_reswords; i++)
4077 const c_common_resword *resword = &c_common_reswords[i];
4078 if (!c_keyword_starts_typename (resword->rid))
4079 continue;
4080 tree resword_identifier = ridpointers [resword->rid];
4081 if (!resword_identifier)
4082 continue;
4083 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4084 bm.consider (resword_identifier);
4088 tree best = bm.get_best_meaningful_candidate ();
4089 if (best)
4090 return IDENTIFIER_POINTER (best);
4091 else
4092 return NULL;
4096 /* Create the predefined scalar types of C,
4097 and some nodes representing standard constants (0, 1, (void *) 0).
4098 Initialize the global scope.
4099 Make definitions for built-in primitive functions. */
4101 void
4102 c_init_decl_processing (void)
4104 location_t save_loc = input_location;
4106 /* Initialize reserved words for parser. */
4107 c_parse_init ();
4109 current_function_decl = NULL_TREE;
4111 gcc_obstack_init (&parser_obstack);
4113 /* Make the externals scope. */
4114 push_scope ();
4115 external_scope = current_scope;
4117 /* Declarations from c_common_nodes_and_builtins must not be associated
4118 with this input file, lest we get differences between using and not
4119 using preprocessed headers. */
4120 input_location = BUILTINS_LOCATION;
4122 c_common_nodes_and_builtins ();
4124 /* In C, comparisons and TRUTH_* expressions have type int. */
4125 truthvalue_type_node = integer_type_node;
4126 truthvalue_true_node = integer_one_node;
4127 truthvalue_false_node = integer_zero_node;
4129 /* Even in C99, which has a real boolean type. */
4130 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4131 boolean_type_node));
4133 input_location = save_loc;
4135 make_fname_decl = c_make_fname_decl;
4136 start_fname_decls ();
4139 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4140 give the decl, NAME is the initialization string and TYPE_DEP
4141 indicates whether NAME depended on the type of the function. As we
4142 don't yet implement delayed emission of static data, we mark the
4143 decl as emitted so it is not placed in the output. Anything using
4144 it must therefore pull out the STRING_CST initializer directly.
4145 FIXME. */
4147 static tree
4148 c_make_fname_decl (location_t loc, tree id, int type_dep)
4150 const char *name = fname_as_string (type_dep);
4151 tree decl, type, init;
4152 size_t length = strlen (name);
4154 type = build_array_type (char_type_node,
4155 build_index_type (size_int (length)));
4156 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4158 decl = build_decl (loc, VAR_DECL, id, type);
4160 TREE_STATIC (decl) = 1;
4161 TREE_READONLY (decl) = 1;
4162 DECL_ARTIFICIAL (decl) = 1;
4164 init = build_string (length + 1, name);
4165 free (CONST_CAST (char *, name));
4166 TREE_TYPE (init) = type;
4167 DECL_INITIAL (decl) = init;
4169 TREE_USED (decl) = 1;
4171 if (current_function_decl
4172 /* For invalid programs like this:
4174 void foo()
4175 const char* p = __FUNCTION__;
4177 the __FUNCTION__ is believed to appear in K&R style function
4178 parameter declarator. In that case we still don't have
4179 function_scope. */
4180 && current_function_scope)
4182 DECL_CONTEXT (decl) = current_function_decl;
4183 bind (id, decl, current_function_scope,
4184 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4187 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4189 return decl;
4192 tree
4193 c_builtin_function (tree decl)
4195 tree type = TREE_TYPE (decl);
4196 tree id = DECL_NAME (decl);
4198 const char *name = IDENTIFIER_POINTER (id);
4199 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4201 /* Should never be called on a symbol with a preexisting meaning. */
4202 gcc_assert (!I_SYMBOL_BINDING (id));
4204 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4205 UNKNOWN_LOCATION);
4207 /* Builtins in the implementation namespace are made visible without
4208 needing to be explicitly declared. See push_file_scope. */
4209 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4211 DECL_CHAIN (decl) = visible_builtins;
4212 visible_builtins = decl;
4215 return decl;
4218 tree
4219 c_builtin_function_ext_scope (tree decl)
4221 tree type = TREE_TYPE (decl);
4222 tree id = DECL_NAME (decl);
4224 const char *name = IDENTIFIER_POINTER (id);
4225 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4227 if (external_scope)
4228 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4229 UNKNOWN_LOCATION);
4231 /* Builtins in the implementation namespace are made visible without
4232 needing to be explicitly declared. See push_file_scope. */
4233 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4235 DECL_CHAIN (decl) = visible_builtins;
4236 visible_builtins = decl;
4239 return decl;
4242 /* Called when a declaration is seen that contains no names to declare.
4243 If its type is a reference to a structure, union or enum inherited
4244 from a containing scope, shadow that tag name for the current scope
4245 with a forward reference.
4246 If its type defines a new named structure or union
4247 or defines an enum, it is valid but we need not do anything here.
4248 Otherwise, it is an error. */
4250 void
4251 shadow_tag (const struct c_declspecs *declspecs)
4253 shadow_tag_warned (declspecs, 0);
4256 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4257 but no pedwarn. */
4258 void
4259 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4261 bool found_tag = false;
4263 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4265 tree value = declspecs->type;
4266 enum tree_code code = TREE_CODE (value);
4268 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4269 /* Used to test also that TYPE_SIZE (value) != 0.
4270 That caused warning for `struct foo;' at top level in the file. */
4272 tree name = TYPE_NAME (value);
4273 tree t;
4275 found_tag = true;
4277 if (declspecs->restrict_p)
4279 error ("invalid use of %<restrict%>");
4280 warned = 1;
4283 if (name == NULL_TREE)
4285 if (warned != 1 && code != ENUMERAL_TYPE)
4286 /* Empty unnamed enum OK */
4288 pedwarn (input_location, 0,
4289 "unnamed struct/union that defines no instances");
4290 warned = 1;
4293 else if (declspecs->typespec_kind != ctsk_tagdef
4294 && declspecs->typespec_kind != ctsk_tagfirstref
4295 && declspecs->storage_class != csc_none)
4297 if (warned != 1)
4298 pedwarn (input_location, 0,
4299 "empty declaration with storage class specifier "
4300 "does not redeclare tag");
4301 warned = 1;
4302 pending_xref_error ();
4304 else if (declspecs->typespec_kind != ctsk_tagdef
4305 && declspecs->typespec_kind != ctsk_tagfirstref
4306 && (declspecs->const_p
4307 || declspecs->volatile_p
4308 || declspecs->atomic_p
4309 || declspecs->restrict_p
4310 || declspecs->address_space))
4312 if (warned != 1)
4313 pedwarn (input_location, 0,
4314 "empty declaration with type qualifier "
4315 "does not redeclare tag");
4316 warned = 1;
4317 pending_xref_error ();
4319 else if (declspecs->typespec_kind != ctsk_tagdef
4320 && declspecs->typespec_kind != ctsk_tagfirstref
4321 && declspecs->alignas_p)
4323 if (warned != 1)
4324 pedwarn (input_location, 0,
4325 "empty declaration with %<_Alignas%> "
4326 "does not redeclare tag");
4327 warned = 1;
4328 pending_xref_error ();
4330 else
4332 pending_invalid_xref = NULL_TREE;
4333 t = lookup_tag (code, name, true, NULL);
4335 if (t == NULL_TREE)
4337 t = make_node (code);
4338 pushtag (input_location, name, t);
4342 else
4344 if (warned != 1 && !in_system_header_at (input_location))
4346 pedwarn (input_location, 0,
4347 "useless type name in empty declaration");
4348 warned = 1;
4352 else if (warned != 1 && !in_system_header_at (input_location)
4353 && declspecs->typedef_p)
4355 pedwarn (input_location, 0, "useless type name in empty declaration");
4356 warned = 1;
4359 pending_invalid_xref = NULL_TREE;
4361 if (declspecs->inline_p)
4363 error ("%<inline%> in empty declaration");
4364 warned = 1;
4367 if (declspecs->noreturn_p)
4369 error ("%<_Noreturn%> in empty declaration");
4370 warned = 1;
4373 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4375 error ("%<auto%> in file-scope empty declaration");
4376 warned = 1;
4379 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4381 error ("%<register%> in file-scope empty declaration");
4382 warned = 1;
4385 if (!warned && !in_system_header_at (input_location)
4386 && declspecs->storage_class != csc_none)
4388 warning (0, "useless storage class specifier in empty declaration");
4389 warned = 2;
4392 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4394 warning (0, "useless %qs in empty declaration",
4395 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4396 warned = 2;
4399 if (!warned
4400 && !in_system_header_at (input_location)
4401 && (declspecs->const_p
4402 || declspecs->volatile_p
4403 || declspecs->atomic_p
4404 || declspecs->restrict_p
4405 || declspecs->address_space))
4407 warning (0, "useless type qualifier in empty declaration");
4408 warned = 2;
4411 if (!warned && !in_system_header_at (input_location)
4412 && declspecs->alignas_p)
4414 warning (0, "useless %<_Alignas%> in empty declaration");
4415 warned = 2;
4418 if (warned != 1)
4420 if (!found_tag)
4421 pedwarn (input_location, 0, "empty declaration");
4426 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4427 bits. SPECS represents declaration specifiers that the grammar
4428 only permits to contain type qualifiers and attributes. */
4431 quals_from_declspecs (const struct c_declspecs *specs)
4433 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4434 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4435 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4436 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4437 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4438 gcc_assert (!specs->type
4439 && !specs->decl_attr
4440 && specs->typespec_word == cts_none
4441 && specs->storage_class == csc_none
4442 && !specs->typedef_p
4443 && !specs->explicit_signed_p
4444 && !specs->deprecated_p
4445 && !specs->long_p
4446 && !specs->long_long_p
4447 && !specs->short_p
4448 && !specs->signed_p
4449 && !specs->unsigned_p
4450 && !specs->complex_p
4451 && !specs->inline_p
4452 && !specs->noreturn_p
4453 && !specs->thread_p);
4454 return quals;
4457 /* Construct an array declarator. LOC is the location of the
4458 beginning of the array (usually the opening brace). EXPR is the
4459 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4460 inside the [] (to be applied to the pointer to which a parameter
4461 array is converted). STATIC_P is true if "static" is inside the
4462 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4463 VLA of unspecified length which is nevertheless a complete type,
4464 false otherwise. The field for the contained declarator is left to
4465 be filled in by set_array_declarator_inner. */
4467 struct c_declarator *
4468 build_array_declarator (location_t loc,
4469 tree expr, struct c_declspecs *quals, bool static_p,
4470 bool vla_unspec_p)
4472 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4473 struct c_declarator);
4474 declarator->id_loc = loc;
4475 declarator->kind = cdk_array;
4476 declarator->declarator = 0;
4477 declarator->u.array.dimen = expr;
4478 if (quals)
4480 declarator->u.array.attrs = quals->attrs;
4481 declarator->u.array.quals = quals_from_declspecs (quals);
4483 else
4485 declarator->u.array.attrs = NULL_TREE;
4486 declarator->u.array.quals = 0;
4488 declarator->u.array.static_p = static_p;
4489 declarator->u.array.vla_unspec_p = vla_unspec_p;
4490 if (static_p || quals != NULL)
4491 pedwarn_c90 (loc, OPT_Wpedantic,
4492 "ISO C90 does not support %<static%> or type "
4493 "qualifiers in parameter array declarators");
4494 if (vla_unspec_p)
4495 pedwarn_c90 (loc, OPT_Wpedantic,
4496 "ISO C90 does not support %<[*]%> array declarators");
4497 if (vla_unspec_p)
4499 if (!current_scope->parm_flag)
4501 /* C99 6.7.5.2p4 */
4502 error_at (loc, "%<[*]%> not allowed in other than "
4503 "function prototype scope");
4504 declarator->u.array.vla_unspec_p = false;
4505 return NULL;
4507 current_scope->had_vla_unspec = true;
4509 return declarator;
4512 /* Set the contained declarator of an array declarator. DECL is the
4513 declarator, as constructed by build_array_declarator; INNER is what
4514 appears on the left of the []. */
4516 struct c_declarator *
4517 set_array_declarator_inner (struct c_declarator *decl,
4518 struct c_declarator *inner)
4520 decl->declarator = inner;
4521 return decl;
4524 /* INIT is a constructor that forms DECL's initializer. If the final
4525 element initializes a flexible array field, add the size of that
4526 initializer to DECL's size. */
4528 static void
4529 add_flexible_array_elts_to_size (tree decl, tree init)
4531 tree elt, type;
4533 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4534 return;
4536 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4537 type = TREE_TYPE (elt);
4538 if (TREE_CODE (type) == ARRAY_TYPE
4539 && TYPE_SIZE (type) == NULL_TREE
4540 && TYPE_DOMAIN (type) != NULL_TREE
4541 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4543 complete_array_type (&type, elt, false);
4544 DECL_SIZE (decl)
4545 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4546 DECL_SIZE_UNIT (decl)
4547 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4551 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4552 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4553 before the type name, and set *EXPR_CONST_OPERANDS, if
4554 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4555 appear in a constant expression. */
4557 tree
4558 groktypename (struct c_type_name *type_name, tree *expr,
4559 bool *expr_const_operands)
4561 tree type;
4562 tree attrs = type_name->specs->attrs;
4564 type_name->specs->attrs = NULL_TREE;
4566 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4567 false, NULL, &attrs, expr, expr_const_operands,
4568 DEPRECATED_NORMAL);
4570 /* Apply attributes. */
4571 decl_attributes (&type, attrs, 0);
4573 return type;
4576 /* Wrapper for decl_attributes that adds some implicit attributes
4577 to VAR_DECLs or FUNCTION_DECLs. */
4579 static tree
4580 c_decl_attributes (tree *node, tree attributes, int flags)
4582 /* Add implicit "omp declare target" attribute if requested. */
4583 if (current_omp_declare_target_attribute
4584 && ((VAR_P (*node) && is_global_var (*node))
4585 || TREE_CODE (*node) == FUNCTION_DECL))
4587 if (VAR_P (*node)
4588 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4589 error ("%q+D in declare target directive does not have mappable type",
4590 *node);
4591 else
4592 attributes = tree_cons (get_identifier ("omp declare target"),
4593 NULL_TREE, attributes);
4595 return decl_attributes (node, attributes, flags);
4599 /* Decode a declarator in an ordinary declaration or data definition.
4600 This is called as soon as the type information and variable name
4601 have been parsed, before parsing the initializer if any.
4602 Here we create the ..._DECL node, fill in its type,
4603 and put it on the list of decls for the current context.
4604 The ..._DECL node is returned as the value.
4606 Exception: for arrays where the length is not specified,
4607 the type is left null, to be filled in by `finish_decl'.
4609 Function definitions do not come here; they go to start_function
4610 instead. However, external and forward declarations of functions
4611 do go through here. Structure field declarations are done by
4612 grokfield and not through here. */
4614 tree
4615 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4616 bool initialized, tree attributes)
4618 tree decl;
4619 tree tem;
4620 tree expr = NULL_TREE;
4621 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4623 /* An object declared as __attribute__((deprecated)) suppresses
4624 warnings of uses of other deprecated items. */
4625 if (lookup_attribute ("deprecated", attributes))
4626 deprecated_state = DEPRECATED_SUPPRESS;
4628 decl = grokdeclarator (declarator, declspecs,
4629 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4630 deprecated_state);
4631 if (!decl || decl == error_mark_node)
4632 return NULL_TREE;
4634 if (expr)
4635 add_stmt (fold_convert (void_type_node, expr));
4637 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4638 warning (OPT_Wmain, "%q+D is usually a function", decl);
4640 if (initialized)
4641 /* Is it valid for this decl to have an initializer at all?
4642 If not, set INITIALIZED to zero, which will indirectly
4643 tell 'finish_decl' to ignore the initializer once it is parsed. */
4644 switch (TREE_CODE (decl))
4646 case TYPE_DECL:
4647 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4648 initialized = false;
4649 break;
4651 case FUNCTION_DECL:
4652 error ("function %qD is initialized like a variable", decl);
4653 initialized = false;
4654 break;
4656 case PARM_DECL:
4657 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4658 error ("parameter %qD is initialized", decl);
4659 initialized = false;
4660 break;
4662 default:
4663 /* Don't allow initializations for incomplete types except for
4664 arrays which might be completed by the initialization. */
4666 /* This can happen if the array size is an undefined macro.
4667 We already gave a warning, so we don't need another one. */
4668 if (TREE_TYPE (decl) == error_mark_node)
4669 initialized = false;
4670 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4672 /* A complete type is ok if size is fixed. */
4674 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4675 || C_DECL_VARIABLE_SIZE (decl))
4677 error ("variable-sized object may not be initialized");
4678 initialized = false;
4681 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4683 error ("variable %qD has initializer but incomplete type", decl);
4684 initialized = false;
4686 else if (C_DECL_VARIABLE_SIZE (decl))
4688 /* Although C99 is unclear about whether incomplete arrays
4689 of VLAs themselves count as VLAs, it does not make
4690 sense to permit them to be initialized given that
4691 ordinary VLAs may not be initialized. */
4692 error ("variable-sized object may not be initialized");
4693 initialized = false;
4697 if (initialized)
4699 if (current_scope == file_scope)
4700 TREE_STATIC (decl) = 1;
4702 /* Tell 'pushdecl' this is an initialized decl
4703 even though we don't yet have the initializer expression.
4704 Also tell 'finish_decl' it may store the real initializer. */
4705 DECL_INITIAL (decl) = error_mark_node;
4708 /* If this is a function declaration, write a record describing it to the
4709 prototypes file (if requested). */
4711 if (TREE_CODE (decl) == FUNCTION_DECL)
4712 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4714 /* ANSI specifies that a tentative definition which is not merged with
4715 a non-tentative definition behaves exactly like a definition with an
4716 initializer equal to zero. (Section 3.7.2)
4718 -fno-common gives strict ANSI behavior, though this tends to break
4719 a large body of code that grew up without this rule.
4721 Thread-local variables are never common, since there's no entrenched
4722 body of code to break, and it allows more efficient variable references
4723 in the presence of dynamic linking. */
4725 if (VAR_P (decl)
4726 && !initialized
4727 && TREE_PUBLIC (decl)
4728 && !DECL_THREAD_LOCAL_P (decl)
4729 && !flag_no_common)
4730 DECL_COMMON (decl) = 1;
4732 /* Set attributes here so if duplicate decl, will have proper attributes. */
4733 c_decl_attributes (&decl, attributes, 0);
4735 /* Handle gnu_inline attribute. */
4736 if (declspecs->inline_p
4737 && !flag_gnu89_inline
4738 && TREE_CODE (decl) == FUNCTION_DECL
4739 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4740 || current_function_decl))
4742 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4744 else if (declspecs->storage_class != csc_static)
4745 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4748 if (TREE_CODE (decl) == FUNCTION_DECL
4749 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4751 struct c_declarator *ce = declarator;
4753 if (ce->kind == cdk_pointer)
4754 ce = declarator->declarator;
4755 if (ce->kind == cdk_function)
4757 tree args = ce->u.arg_info->parms;
4758 for (; args; args = DECL_CHAIN (args))
4760 tree type = TREE_TYPE (args);
4761 if (type && INTEGRAL_TYPE_P (type)
4762 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4763 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4768 if (TREE_CODE (decl) == FUNCTION_DECL
4769 && DECL_DECLARED_INLINE_P (decl)
4770 && DECL_UNINLINABLE (decl)
4771 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4772 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4773 decl);
4775 /* C99 6.7.4p3: An inline definition of a function with external
4776 linkage shall not contain a definition of a modifiable object
4777 with static storage duration... */
4778 if (VAR_P (decl)
4779 && current_scope != file_scope
4780 && TREE_STATIC (decl)
4781 && !TREE_READONLY (decl)
4782 && DECL_DECLARED_INLINE_P (current_function_decl)
4783 && DECL_EXTERNAL (current_function_decl))
4784 record_inline_static (input_location, current_function_decl,
4785 decl, csi_modifiable);
4787 if (c_dialect_objc ()
4788 && VAR_OR_FUNCTION_DECL_P (decl))
4789 objc_check_global_decl (decl);
4791 /* Add this decl to the current scope.
4792 TEM may equal DECL or it may be a previous decl of the same name. */
4793 tem = pushdecl (decl);
4795 if (initialized && DECL_EXTERNAL (tem))
4797 DECL_EXTERNAL (tem) = 0;
4798 TREE_STATIC (tem) = 1;
4801 return tem;
4804 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4805 DECL or the non-array element type if DECL is an uninitialized array.
4806 If that type has a const member, diagnose this. */
4808 static void
4809 diagnose_uninitialized_cst_member (tree decl, tree type)
4811 tree field;
4812 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4814 tree field_type;
4815 if (TREE_CODE (field) != FIELD_DECL)
4816 continue;
4817 field_type = strip_array_types (TREE_TYPE (field));
4819 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4821 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4822 "uninitialized const member in %qT is invalid in C++",
4823 strip_array_types (TREE_TYPE (decl)));
4824 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4827 if (RECORD_OR_UNION_TYPE_P (field_type))
4828 diagnose_uninitialized_cst_member (decl, field_type);
4832 /* Finish processing of a declaration;
4833 install its initial value.
4834 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4835 If the length of an array type is not known before,
4836 it must be determined now, from the initial value, or it is an error.
4838 INIT_LOC is the location of the initial value. */
4840 void
4841 finish_decl (tree decl, location_t init_loc, tree init,
4842 tree origtype, tree asmspec_tree)
4844 tree type;
4845 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4846 const char *asmspec = 0;
4848 /* If a name was specified, get the string. */
4849 if (VAR_OR_FUNCTION_DECL_P (decl)
4850 && DECL_FILE_SCOPE_P (decl))
4851 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4852 if (asmspec_tree)
4853 asmspec = TREE_STRING_POINTER (asmspec_tree);
4855 if (VAR_P (decl)
4856 && TREE_STATIC (decl)
4857 && global_bindings_p ())
4858 /* So decl is a global variable. Record the types it uses
4859 so that we can decide later to emit debug info for them. */
4860 record_types_used_by_current_var_decl (decl);
4862 /* If `start_decl' didn't like having an initialization, ignore it now. */
4863 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4864 init = NULL_TREE;
4866 /* Don't crash if parm is initialized. */
4867 if (TREE_CODE (decl) == PARM_DECL)
4868 init = NULL_TREE;
4870 if (init)
4871 store_init_value (init_loc, decl, init, origtype);
4873 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4874 || TREE_CODE (decl) == FIELD_DECL))
4875 objc_check_decl (decl);
4877 type = TREE_TYPE (decl);
4879 /* Deduce size of array from initialization, if not already known. */
4880 if (TREE_CODE (type) == ARRAY_TYPE
4881 && TYPE_DOMAIN (type) == NULL_TREE
4882 && TREE_CODE (decl) != TYPE_DECL)
4884 bool do_default
4885 = (TREE_STATIC (decl)
4886 /* Even if pedantic, an external linkage array
4887 may have incomplete type at first. */
4888 ? pedantic && !TREE_PUBLIC (decl)
4889 : !DECL_EXTERNAL (decl));
4890 int failure
4891 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4892 do_default);
4894 /* Get the completed type made by complete_array_type. */
4895 type = TREE_TYPE (decl);
4897 switch (failure)
4899 case 1:
4900 error ("initializer fails to determine size of %q+D", decl);
4901 break;
4903 case 2:
4904 if (do_default)
4905 error ("array size missing in %q+D", decl);
4906 /* If a `static' var's size isn't known,
4907 make it extern as well as static, so it does not get
4908 allocated.
4909 If it is not `static', then do not mark extern;
4910 finish_incomplete_decl will give it a default size
4911 and it will get allocated. */
4912 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4913 DECL_EXTERNAL (decl) = 1;
4914 break;
4916 case 3:
4917 error ("zero or negative size array %q+D", decl);
4918 break;
4920 case 0:
4921 /* For global variables, update the copy of the type that
4922 exists in the binding. */
4923 if (TREE_PUBLIC (decl))
4925 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4926 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4927 b_ext = b_ext->shadowed;
4928 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4930 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4931 b_ext->u.type = composite_type (b_ext->u.type, type);
4932 else
4933 b_ext->u.type = type;
4936 break;
4938 default:
4939 gcc_unreachable ();
4942 if (DECL_INITIAL (decl))
4943 TREE_TYPE (DECL_INITIAL (decl)) = type;
4945 relayout_decl (decl);
4948 if (VAR_P (decl))
4950 if (init && TREE_CODE (init) == CONSTRUCTOR)
4951 add_flexible_array_elts_to_size (decl, init);
4953 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
4954 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4955 layout_decl (decl, 0);
4957 if (DECL_SIZE (decl) == NULL_TREE
4958 /* Don't give an error if we already gave one earlier. */
4959 && TREE_TYPE (decl) != error_mark_node
4960 && (TREE_STATIC (decl)
4961 /* A static variable with an incomplete type
4962 is an error if it is initialized.
4963 Also if it is not file scope.
4964 Otherwise, let it through, but if it is not `extern'
4965 then it may cause an error message later. */
4966 ? (DECL_INITIAL (decl) != NULL_TREE
4967 || !DECL_FILE_SCOPE_P (decl))
4968 /* An automatic variable with an incomplete type
4969 is an error. */
4970 : !DECL_EXTERNAL (decl)))
4972 error ("storage size of %q+D isn%'t known", decl);
4973 TREE_TYPE (decl) = error_mark_node;
4976 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
4977 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
4978 && DECL_SIZE (decl) == NULL_TREE
4979 && TREE_STATIC (decl))
4980 incomplete_record_decls.safe_push (decl);
4982 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
4984 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4985 constant_expression_warning (DECL_SIZE (decl));
4986 else
4988 error ("storage size of %q+D isn%'t constant", decl);
4989 TREE_TYPE (decl) = error_mark_node;
4993 if (TREE_USED (type))
4995 TREE_USED (decl) = 1;
4996 DECL_READ_P (decl) = 1;
5000 /* If this is a function and an assembler name is specified, reset DECL_RTL
5001 so we can give it its new name. Also, update builtin_decl if it
5002 was a normal built-in. */
5003 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5005 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5006 set_builtin_user_assembler_name (decl, asmspec);
5007 set_user_assembler_name (decl, asmspec);
5010 /* If #pragma weak was used, mark the decl weak now. */
5011 maybe_apply_pragma_weak (decl);
5013 /* Output the assembler code and/or RTL code for variables and functions,
5014 unless the type is an undefined structure or union.
5015 If not, it will get done when the type is completed. */
5017 if (VAR_OR_FUNCTION_DECL_P (decl))
5019 /* Determine the ELF visibility. */
5020 if (TREE_PUBLIC (decl))
5021 c_determine_visibility (decl);
5023 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5024 if (c_dialect_objc ())
5025 objc_check_decl (decl);
5027 if (asmspec)
5029 /* If this is not a static variable, issue a warning.
5030 It doesn't make any sense to give an ASMSPEC for an
5031 ordinary, non-register local variable. Historically,
5032 GCC has accepted -- but ignored -- the ASMSPEC in
5033 this case. */
5034 if (!DECL_FILE_SCOPE_P (decl)
5035 && VAR_P (decl)
5036 && !C_DECL_REGISTER (decl)
5037 && !TREE_STATIC (decl))
5038 warning (0, "ignoring asm-specifier for non-static local "
5039 "variable %q+D", decl);
5040 else
5041 set_user_assembler_name (decl, asmspec);
5044 if (DECL_FILE_SCOPE_P (decl))
5046 if (DECL_INITIAL (decl) == NULL_TREE
5047 || DECL_INITIAL (decl) == error_mark_node)
5048 /* Don't output anything
5049 when a tentative file-scope definition is seen.
5050 But at end of compilation, do output code for them. */
5051 DECL_DEFER_OUTPUT (decl) = 1;
5052 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5053 DECL_HARD_REGISTER (decl) = 1;
5054 rest_of_decl_compilation (decl, true, 0);
5056 else
5058 /* In conjunction with an ASMSPEC, the `register'
5059 keyword indicates that we should place the variable
5060 in a particular register. */
5061 if (asmspec && C_DECL_REGISTER (decl))
5063 DECL_HARD_REGISTER (decl) = 1;
5064 /* This cannot be done for a structure with volatile
5065 fields, on which DECL_REGISTER will have been
5066 reset. */
5067 if (!DECL_REGISTER (decl))
5068 error ("cannot put object with volatile field into register");
5071 if (TREE_CODE (decl) != FUNCTION_DECL)
5073 /* If we're building a variable sized type, and we might be
5074 reachable other than via the top of the current binding
5075 level, then create a new BIND_EXPR so that we deallocate
5076 the object at the right time. */
5077 /* Note that DECL_SIZE can be null due to errors. */
5078 if (DECL_SIZE (decl)
5079 && !TREE_CONSTANT (DECL_SIZE (decl))
5080 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5082 tree bind;
5083 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5084 TREE_SIDE_EFFECTS (bind) = 1;
5085 add_stmt (bind);
5086 BIND_EXPR_BODY (bind) = push_stmt_list ();
5088 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5089 DECL_EXPR, decl));
5094 if (!DECL_FILE_SCOPE_P (decl))
5096 /* Recompute the RTL of a local array now
5097 if it used to be an incomplete type. */
5098 if (was_incomplete && !is_global_var (decl))
5100 /* If we used it already as memory, it must stay in memory. */
5101 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5102 /* If it's still incomplete now, no init will save it. */
5103 if (DECL_SIZE (decl) == NULL_TREE)
5104 DECL_INITIAL (decl) = NULL_TREE;
5109 if (TREE_CODE (decl) == TYPE_DECL)
5111 if (!DECL_FILE_SCOPE_P (decl)
5112 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5113 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5115 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5118 /* Install a cleanup (aka destructor) if one was given. */
5119 if (VAR_P (decl) && !TREE_STATIC (decl))
5121 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5122 if (attr)
5124 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5125 tree cleanup_decl = lookup_name (cleanup_id);
5126 tree cleanup;
5127 vec<tree, va_gc> *v;
5129 /* Build "cleanup(&decl)" for the destructor. */
5130 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5131 vec_alloc (v, 1);
5132 v->quick_push (cleanup);
5133 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5134 vNULL, cleanup_decl, v, NULL);
5135 vec_free (v);
5137 /* Don't warn about decl unused; the cleanup uses it. */
5138 TREE_USED (decl) = 1;
5139 TREE_USED (cleanup_decl) = 1;
5140 DECL_READ_P (decl) = 1;
5142 push_cleanup (decl, cleanup, false);
5146 if (warn_cxx_compat
5147 && VAR_P (decl)
5148 && !DECL_EXTERNAL (decl)
5149 && DECL_INITIAL (decl) == NULL_TREE)
5151 type = strip_array_types (type);
5152 if (TREE_READONLY (decl))
5153 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5154 "uninitialized const %qD is invalid in C++", decl);
5155 else if (RECORD_OR_UNION_TYPE_P (type)
5156 && C_TYPE_FIELDS_READONLY (type))
5157 diagnose_uninitialized_cst_member (decl, type);
5160 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5163 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5164 EXPR is NULL or a pointer to an expression that needs to be
5165 evaluated for the side effects of array size expressions in the
5166 parameters. */
5168 tree
5169 grokparm (const struct c_parm *parm, tree *expr)
5171 tree attrs = parm->attrs;
5172 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5173 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5175 decl_attributes (&decl, attrs, 0);
5177 return decl;
5180 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5181 and push that on the current scope. EXPR is a pointer to an
5182 expression that needs to be evaluated for the side effects of array
5183 size expressions in the parameters. */
5185 void
5186 push_parm_decl (const struct c_parm *parm, tree *expr)
5188 tree attrs = parm->attrs;
5189 tree decl;
5191 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5192 &attrs, expr, NULL, DEPRECATED_NORMAL);
5193 decl_attributes (&decl, attrs, 0);
5195 decl = pushdecl (decl);
5197 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5200 /* Mark all the parameter declarations to date as forward decls.
5201 Also diagnose use of this extension. */
5203 void
5204 mark_forward_parm_decls (void)
5206 struct c_binding *b;
5208 if (pedantic && !current_scope->warned_forward_parm_decls)
5210 pedwarn (input_location, OPT_Wpedantic,
5211 "ISO C forbids forward parameter declarations");
5212 current_scope->warned_forward_parm_decls = true;
5215 for (b = current_scope->bindings; b; b = b->prev)
5216 if (TREE_CODE (b->decl) == PARM_DECL)
5217 TREE_ASM_WRITTEN (b->decl) = 1;
5220 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5221 literal, which may be an incomplete array type completed by the
5222 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5223 literal. NON_CONST is true if the initializers contain something
5224 that cannot occur in a constant expression. */
5226 tree
5227 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5229 /* We do not use start_decl here because we have a type, not a declarator;
5230 and do not use finish_decl because the decl should be stored inside
5231 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5232 tree decl;
5233 tree complit;
5234 tree stmt;
5236 if (type == error_mark_node
5237 || init == error_mark_node)
5238 return error_mark_node;
5240 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5241 DECL_EXTERNAL (decl) = 0;
5242 TREE_PUBLIC (decl) = 0;
5243 TREE_STATIC (decl) = (current_scope == file_scope);
5244 DECL_CONTEXT (decl) = current_function_decl;
5245 TREE_USED (decl) = 1;
5246 DECL_READ_P (decl) = 1;
5247 DECL_ARTIFICIAL (decl) = 1;
5248 DECL_IGNORED_P (decl) = 1;
5249 TREE_TYPE (decl) = type;
5250 TREE_READONLY (decl) = (TYPE_READONLY (type)
5251 || (TREE_CODE (type) == ARRAY_TYPE
5252 && TYPE_READONLY (TREE_TYPE (type))));
5253 store_init_value (loc, decl, init, NULL_TREE);
5255 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5257 int failure = complete_array_type (&TREE_TYPE (decl),
5258 DECL_INITIAL (decl), true);
5259 /* If complete_array_type returns 3, it means that the
5260 initial value of the compound literal is empty. Allow it. */
5261 gcc_assert (failure == 0 || failure == 3);
5263 type = TREE_TYPE (decl);
5264 TREE_TYPE (DECL_INITIAL (decl)) = type;
5267 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5269 c_incomplete_type_error (loc, NULL_TREE, type);
5270 return error_mark_node;
5273 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5274 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5275 TREE_SIDE_EFFECTS (complit) = 1;
5277 layout_decl (decl, 0);
5279 if (TREE_STATIC (decl))
5281 /* This decl needs a name for the assembler output. */
5282 set_compound_literal_name (decl);
5283 DECL_DEFER_OUTPUT (decl) = 1;
5284 DECL_COMDAT (decl) = 1;
5285 pushdecl (decl);
5286 rest_of_decl_compilation (decl, 1, 0);
5289 if (non_const)
5291 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5292 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5295 return complit;
5298 /* Check the type of a compound literal. Here we just check that it
5299 is valid for C++. */
5301 void
5302 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5304 if (warn_cxx_compat
5305 && (type_name->specs->typespec_kind == ctsk_tagdef
5306 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5307 warning_at (loc, OPT_Wc___compat,
5308 "defining a type in a compound literal is invalid in C++");
5311 /* Determine whether TYPE is a structure with a flexible array member,
5312 or a union containing such a structure (possibly recursively). */
5314 static bool
5315 flexible_array_type_p (tree type)
5317 tree x;
5318 switch (TREE_CODE (type))
5320 case RECORD_TYPE:
5321 x = TYPE_FIELDS (type);
5322 if (x == NULL_TREE)
5323 return false;
5324 while (DECL_CHAIN (x) != NULL_TREE)
5325 x = DECL_CHAIN (x);
5326 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5327 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5328 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5329 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5330 return true;
5331 return false;
5332 case UNION_TYPE:
5333 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5335 if (flexible_array_type_p (TREE_TYPE (x)))
5336 return true;
5338 return false;
5339 default:
5340 return false;
5344 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5345 replacing with appropriate values if they are invalid. */
5347 static void
5348 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5349 tree orig_name)
5351 tree type_mv;
5352 unsigned int max_width;
5353 unsigned HOST_WIDE_INT w;
5354 const char *name = (orig_name
5355 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5356 : _("<anonymous>"));
5358 /* Detect and ignore out of range field width and process valid
5359 field widths. */
5360 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5362 error_at (loc, "bit-field %qs width not an integer constant", name);
5363 *width = integer_one_node;
5365 else
5367 if (TREE_CODE (*width) != INTEGER_CST)
5369 *width = c_fully_fold (*width, false, NULL);
5370 if (TREE_CODE (*width) == INTEGER_CST)
5371 pedwarn (loc, OPT_Wpedantic,
5372 "bit-field %qs width not an integer constant expression",
5373 name);
5375 if (TREE_CODE (*width) != INTEGER_CST)
5377 error_at (loc, "bit-field %qs width not an integer constant", name);
5378 *width = integer_one_node;
5380 constant_expression_warning (*width);
5381 if (tree_int_cst_sgn (*width) < 0)
5383 error_at (loc, "negative width in bit-field %qs", name);
5384 *width = integer_one_node;
5386 else if (integer_zerop (*width) && orig_name)
5388 error_at (loc, "zero width for bit-field %qs", name);
5389 *width = integer_one_node;
5393 /* Detect invalid bit-field type. */
5394 if (TREE_CODE (*type) != INTEGER_TYPE
5395 && TREE_CODE (*type) != BOOLEAN_TYPE
5396 && TREE_CODE (*type) != ENUMERAL_TYPE)
5398 error_at (loc, "bit-field %qs has invalid type", name);
5399 *type = unsigned_type_node;
5402 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5404 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5405 name);
5406 *type = unsigned_type_node;
5409 type_mv = TYPE_MAIN_VARIANT (*type);
5410 if (!in_system_header_at (input_location)
5411 && type_mv != integer_type_node
5412 && type_mv != unsigned_type_node
5413 && type_mv != boolean_type_node)
5414 pedwarn_c90 (loc, OPT_Wpedantic,
5415 "type of bit-field %qs is a GCC extension", name);
5417 max_width = TYPE_PRECISION (*type);
5419 if (0 < compare_tree_int (*width, max_width))
5421 error_at (loc, "width of %qs exceeds its type", name);
5422 w = max_width;
5423 *width = build_int_cst (integer_type_node, w);
5425 else
5426 w = tree_to_uhwi (*width);
5428 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5430 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5431 if (!lt
5432 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5433 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5434 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5440 /* Print warning about variable length array if necessary. */
5442 static void
5443 warn_variable_length_array (tree name, tree size)
5445 if (TREE_CONSTANT (size))
5447 if (name)
5448 pedwarn_c90 (input_location, OPT_Wvla,
5449 "ISO C90 forbids array %qE whose size "
5450 "can%'t be evaluated", name);
5451 else
5452 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5453 "whose size can%'t be evaluated");
5455 else
5457 if (name)
5458 pedwarn_c90 (input_location, OPT_Wvla,
5459 "ISO C90 forbids variable length array %qE", name);
5460 else
5461 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5462 "length array");
5466 /* Print warning about defaulting to int if necessary. */
5468 static void
5469 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5471 diagnostic_info diagnostic;
5472 va_list ap;
5473 rich_location richloc (line_table, location);
5475 va_start (ap, gmsgid);
5476 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5477 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5478 diagnostic.option_index = opt;
5479 diagnostic_report_diagnostic (global_dc, &diagnostic);
5480 va_end (ap);
5483 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5484 considering only those c_declspec_words found in LIST, which
5485 must be terminated by cdw_number_of_elements. */
5487 static location_t
5488 smallest_type_quals_location (const location_t *locations,
5489 const c_declspec_word *list)
5491 location_t loc = UNKNOWN_LOCATION;
5492 while (*list != cdw_number_of_elements)
5494 location_t newloc = locations[*list];
5495 if (loc == UNKNOWN_LOCATION
5496 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5497 loc = newloc;
5498 list++;
5501 return loc;
5504 /* Given declspecs and a declarator,
5505 determine the name and type of the object declared
5506 and construct a ..._DECL node for it.
5507 (In one case we can return a ..._TYPE node instead.
5508 For invalid input we sometimes return NULL_TREE.)
5510 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5512 DECL_CONTEXT says which syntactic context this declaration is in:
5513 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5514 FUNCDEF for a function definition. Like NORMAL but a few different
5515 error messages in each case. Return value may be zero meaning
5516 this definition is too screwy to try to parse.
5517 PARM for a parameter declaration (either within a function prototype
5518 or before a function body). Make a PARM_DECL, or return void_type_node.
5519 TYPENAME if for a typename (in a cast or sizeof).
5520 Don't make a DECL node; just return the ..._TYPE node.
5521 FIELD for a struct or union field; make a FIELD_DECL.
5522 INITIALIZED is true if the decl has an initializer.
5523 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5524 representing the width of the bit-field.
5525 DECL_ATTRS points to the list of attributes that should be added to this
5526 decl. Any nested attributes that belong on the decl itself will be
5527 added to this list.
5528 If EXPR is not NULL, any expressions that need to be evaluated as
5529 part of evaluating variably modified types will be stored in *EXPR.
5530 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5531 set to indicate whether operands in *EXPR can be used in constant
5532 expressions.
5533 DEPRECATED_STATE is a deprecated_states value indicating whether
5534 deprecation warnings should be suppressed.
5536 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5537 It may also be so in the PARM case, for a prototype where the
5538 argument type is specified but not the name.
5540 This function is where the complicated C meanings of `static'
5541 and `extern' are interpreted. */
5543 static tree
5544 grokdeclarator (const struct c_declarator *declarator,
5545 struct c_declspecs *declspecs,
5546 enum decl_context decl_context, bool initialized, tree *width,
5547 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5548 enum deprecated_states deprecated_state)
5550 tree type = declspecs->type;
5551 bool threadp = declspecs->thread_p;
5552 enum c_storage_class storage_class = declspecs->storage_class;
5553 int constp;
5554 int restrictp;
5555 int volatilep;
5556 int atomicp;
5557 int type_quals = TYPE_UNQUALIFIED;
5558 tree name = NULL_TREE;
5559 bool funcdef_flag = false;
5560 bool funcdef_syntax = false;
5561 bool size_varies = false;
5562 tree decl_attr = declspecs->decl_attr;
5563 int array_ptr_quals = TYPE_UNQUALIFIED;
5564 tree array_ptr_attrs = NULL_TREE;
5565 bool array_parm_static = false;
5566 bool array_parm_vla_unspec_p = false;
5567 tree returned_attrs = NULL_TREE;
5568 bool bitfield = width != NULL;
5569 tree element_type;
5570 tree orig_qual_type = NULL;
5571 size_t orig_qual_indirect = 0;
5572 struct c_arg_info *arg_info = 0;
5573 addr_space_t as1, as2, address_space;
5574 location_t loc = UNKNOWN_LOCATION;
5575 tree expr_dummy;
5576 bool expr_const_operands_dummy;
5577 enum c_declarator_kind first_non_attr_kind;
5578 unsigned int alignas_align = 0;
5580 if (TREE_CODE (type) == ERROR_MARK)
5581 return error_mark_node;
5582 if (expr == NULL)
5584 expr = &expr_dummy;
5585 expr_dummy = NULL_TREE;
5587 if (expr_const_operands == NULL)
5588 expr_const_operands = &expr_const_operands_dummy;
5590 if (declspecs->expr)
5592 if (*expr)
5593 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5594 declspecs->expr);
5595 else
5596 *expr = declspecs->expr;
5598 *expr_const_operands = declspecs->expr_const_operands;
5600 if (decl_context == FUNCDEF)
5601 funcdef_flag = true, decl_context = NORMAL;
5603 /* Look inside a declarator for the name being declared
5604 and get it as an IDENTIFIER_NODE, for an error message. */
5606 const struct c_declarator *decl = declarator;
5608 first_non_attr_kind = cdk_attrs;
5609 while (decl)
5610 switch (decl->kind)
5612 case cdk_array:
5613 loc = decl->id_loc;
5614 /* FALL THRU. */
5616 case cdk_function:
5617 case cdk_pointer:
5618 funcdef_syntax = (decl->kind == cdk_function);
5619 if (first_non_attr_kind == cdk_attrs)
5620 first_non_attr_kind = decl->kind;
5621 decl = decl->declarator;
5622 break;
5624 case cdk_attrs:
5625 decl = decl->declarator;
5626 break;
5628 case cdk_id:
5629 loc = decl->id_loc;
5630 if (decl->u.id)
5631 name = decl->u.id;
5632 if (first_non_attr_kind == cdk_attrs)
5633 first_non_attr_kind = decl->kind;
5634 decl = 0;
5635 break;
5637 default:
5638 gcc_unreachable ();
5640 if (name == NULL_TREE)
5642 gcc_assert (decl_context == PARM
5643 || decl_context == TYPENAME
5644 || (decl_context == FIELD
5645 && declarator->kind == cdk_id));
5646 gcc_assert (!initialized);
5650 /* A function definition's declarator must have the form of
5651 a function declarator. */
5653 if (funcdef_flag && !funcdef_syntax)
5654 return NULL_TREE;
5656 /* If this looks like a function definition, make it one,
5657 even if it occurs where parms are expected.
5658 Then store_parm_decls will reject it and not use it as a parm. */
5659 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5660 decl_context = PARM;
5662 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5663 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5665 if ((decl_context == NORMAL || decl_context == FIELD)
5666 && current_scope == file_scope
5667 && variably_modified_type_p (type, NULL_TREE))
5669 if (name)
5670 error_at (loc, "variably modified %qE at file scope", name);
5671 else
5672 error_at (loc, "variably modified field at file scope");
5673 type = integer_type_node;
5676 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5678 /* Diagnose defaulting to "int". */
5680 if (declspecs->default_int_p && !in_system_header_at (input_location))
5682 /* Issue a warning if this is an ISO C 99 program or if
5683 -Wreturn-type and this is a function, or if -Wimplicit;
5684 prefer the former warning since it is more explicit. */
5685 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5686 && funcdef_flag)
5687 warn_about_return_type = 1;
5688 else
5690 if (name)
5691 warn_defaults_to (loc, OPT_Wimplicit_int,
5692 "type defaults to %<int%> in declaration "
5693 "of %qE", name);
5694 else
5695 warn_defaults_to (loc, OPT_Wimplicit_int,
5696 "type defaults to %<int%> in type name");
5700 /* Adjust the type if a bit-field is being declared,
5701 -funsigned-bitfields applied and the type is not explicitly
5702 "signed". */
5703 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5704 && TREE_CODE (type) == INTEGER_TYPE)
5705 type = unsigned_type_for (type);
5707 /* Figure out the type qualifiers for the declaration. There are
5708 two ways a declaration can become qualified. One is something
5709 like `const int i' where the `const' is explicit. Another is
5710 something like `typedef const int CI; CI i' where the type of the
5711 declaration contains the `const'. A third possibility is that
5712 there is a type qualifier on the element type of a typedefed
5713 array type, in which case we should extract that qualifier so
5714 that c_apply_type_quals_to_decl receives the full list of
5715 qualifiers to work with (C90 is not entirely clear about whether
5716 duplicate qualifiers should be diagnosed in this case, but it
5717 seems most appropriate to do so). */
5718 element_type = strip_array_types (type);
5719 constp = declspecs->const_p + TYPE_READONLY (element_type);
5720 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5721 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5722 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5723 as1 = declspecs->address_space;
5724 as2 = TYPE_ADDR_SPACE (element_type);
5725 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5727 if (constp > 1)
5728 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5729 if (restrictp > 1)
5730 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5731 if (volatilep > 1)
5732 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5733 if (atomicp > 1)
5734 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5736 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5737 error_at (loc, "conflicting named address spaces (%s vs %s)",
5738 c_addr_space_name (as1), c_addr_space_name (as2));
5740 if ((TREE_CODE (type) == ARRAY_TYPE
5741 || first_non_attr_kind == cdk_array)
5742 && TYPE_QUALS (element_type))
5744 orig_qual_type = type;
5745 type = TYPE_MAIN_VARIANT (type);
5747 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5748 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5749 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5750 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5751 | ENCODE_QUAL_ADDR_SPACE (address_space));
5752 if (type_quals != TYPE_QUALS (element_type))
5753 orig_qual_type = NULL_TREE;
5755 /* Applying the _Atomic qualifier to an array type (through the use
5756 of typedefs or typeof) must be detected here. If the qualifier
5757 is introduced later, any appearance of applying it to an array is
5758 actually applying it to an element of that array. */
5759 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5760 error_at (loc, "%<_Atomic%>-qualified array type");
5762 /* Warn about storage classes that are invalid for certain
5763 kinds of declarations (parameters, typenames, etc.). */
5765 if (funcdef_flag
5766 && (threadp
5767 || storage_class == csc_auto
5768 || storage_class == csc_register
5769 || storage_class == csc_typedef))
5771 if (storage_class == csc_auto)
5772 pedwarn (loc,
5773 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5774 "function definition declared %<auto%>");
5775 if (storage_class == csc_register)
5776 error_at (loc, "function definition declared %<register%>");
5777 if (storage_class == csc_typedef)
5778 error_at (loc, "function definition declared %<typedef%>");
5779 if (threadp)
5780 error_at (loc, "function definition declared %qs",
5781 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5782 threadp = false;
5783 if (storage_class == csc_auto
5784 || storage_class == csc_register
5785 || storage_class == csc_typedef)
5786 storage_class = csc_none;
5788 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5790 if (decl_context == PARM && storage_class == csc_register)
5792 else
5794 switch (decl_context)
5796 case FIELD:
5797 if (name)
5798 error_at (loc, "storage class specified for structure "
5799 "field %qE", name);
5800 else
5801 error_at (loc, "storage class specified for structure field");
5802 break;
5803 case PARM:
5804 if (name)
5805 error_at (loc, "storage class specified for parameter %qE",
5806 name);
5807 else
5808 error_at (loc, "storage class specified for unnamed parameter");
5809 break;
5810 default:
5811 error_at (loc, "storage class specified for typename");
5812 break;
5814 storage_class = csc_none;
5815 threadp = false;
5818 else if (storage_class == csc_extern
5819 && initialized
5820 && !funcdef_flag)
5822 /* 'extern' with initialization is invalid if not at file scope. */
5823 if (current_scope == file_scope)
5825 /* It is fine to have 'extern const' when compiling at C
5826 and C++ intersection. */
5827 if (!(warn_cxx_compat && constp))
5828 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5829 name);
5831 else
5832 error_at (loc, "%qE has both %<extern%> and initializer", name);
5834 else if (current_scope == file_scope)
5836 if (storage_class == csc_auto)
5837 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5838 name);
5839 if (pedantic && storage_class == csc_register)
5840 pedwarn (input_location, OPT_Wpedantic,
5841 "file-scope declaration of %qE specifies %<register%>", name);
5843 else
5845 if (storage_class == csc_extern && funcdef_flag)
5846 error_at (loc, "nested function %qE declared %<extern%>", name);
5847 else if (threadp && storage_class == csc_none)
5849 error_at (loc, "function-scope %qE implicitly auto and declared "
5850 "%qs", name,
5851 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5852 threadp = false;
5856 /* Now figure out the structure of the declarator proper.
5857 Descend through it, creating more complex types, until we reach
5858 the declared identifier (or NULL_TREE, in an absolute declarator).
5859 At each stage we maintain an unqualified version of the type
5860 together with any qualifiers that should be applied to it with
5861 c_build_qualified_type; this way, array types including
5862 multidimensional array types are first built up in unqualified
5863 form and then the qualified form is created with
5864 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5866 while (declarator && declarator->kind != cdk_id)
5868 if (type == error_mark_node)
5870 declarator = declarator->declarator;
5871 continue;
5874 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5875 a cdk_pointer (for *...),
5876 a cdk_function (for ...(...)),
5877 a cdk_attrs (for nested attributes),
5878 or a cdk_id (for the name being declared
5879 or the place in an absolute declarator
5880 where the name was omitted).
5881 For the last case, we have just exited the loop.
5883 At this point, TYPE is the type of elements of an array,
5884 or for a function to return, or for a pointer to point to.
5885 After this sequence of ifs, TYPE is the type of the
5886 array or function or pointer, and DECLARATOR has had its
5887 outermost layer removed. */
5889 if (array_ptr_quals != TYPE_UNQUALIFIED
5890 || array_ptr_attrs != NULL_TREE
5891 || array_parm_static)
5893 /* Only the innermost declarator (making a parameter be of
5894 array type which is converted to pointer type)
5895 may have static or type qualifiers. */
5896 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5897 array_ptr_quals = TYPE_UNQUALIFIED;
5898 array_ptr_attrs = NULL_TREE;
5899 array_parm_static = false;
5902 switch (declarator->kind)
5904 case cdk_attrs:
5906 /* A declarator with embedded attributes. */
5907 tree attrs = declarator->u.attrs;
5908 const struct c_declarator *inner_decl;
5909 int attr_flags = 0;
5910 declarator = declarator->declarator;
5911 inner_decl = declarator;
5912 while (inner_decl->kind == cdk_attrs)
5913 inner_decl = inner_decl->declarator;
5914 if (inner_decl->kind == cdk_id)
5915 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5916 else if (inner_decl->kind == cdk_function)
5917 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5918 else if (inner_decl->kind == cdk_array)
5919 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5920 returned_attrs = decl_attributes (&type,
5921 chainon (returned_attrs, attrs),
5922 attr_flags);
5923 break;
5925 case cdk_array:
5927 tree itype = NULL_TREE;
5928 tree size = declarator->u.array.dimen;
5929 /* The index is a signed object `sizetype' bits wide. */
5930 tree index_type = c_common_signed_type (sizetype);
5932 array_ptr_quals = declarator->u.array.quals;
5933 array_ptr_attrs = declarator->u.array.attrs;
5934 array_parm_static = declarator->u.array.static_p;
5935 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5937 declarator = declarator->declarator;
5939 /* Check for some types that there cannot be arrays of. */
5941 if (VOID_TYPE_P (type))
5943 if (name)
5944 error_at (loc, "declaration of %qE as array of voids", name);
5945 else
5946 error_at (loc, "declaration of type name as array of voids");
5947 type = error_mark_node;
5950 if (TREE_CODE (type) == FUNCTION_TYPE)
5952 if (name)
5953 error_at (loc, "declaration of %qE as array of functions",
5954 name);
5955 else
5956 error_at (loc, "declaration of type name as array of "
5957 "functions");
5958 type = error_mark_node;
5961 if (pedantic && !in_system_header_at (input_location)
5962 && flexible_array_type_p (type))
5963 pedwarn (loc, OPT_Wpedantic,
5964 "invalid use of structure with flexible array member");
5966 if (size == error_mark_node)
5967 type = error_mark_node;
5969 if (type == error_mark_node)
5970 continue;
5972 /* If size was specified, set ITYPE to a range-type for
5973 that size. Otherwise, ITYPE remains null. finish_decl
5974 may figure it out from an initial value. */
5976 if (size)
5978 bool size_maybe_const = true;
5979 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5980 && !TREE_OVERFLOW (size));
5981 bool this_size_varies = false;
5983 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5984 lvalue. */
5985 STRIP_TYPE_NOPS (size);
5987 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5989 if (name)
5990 error_at (loc, "size of array %qE has non-integer type",
5991 name);
5992 else
5993 error_at (loc,
5994 "size of unnamed array has non-integer type");
5995 size = integer_one_node;
5997 /* This can happen with enum forward declaration. */
5998 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6000 if (name)
6001 error_at (loc, "size of array %qE has incomplete type",
6002 name);
6003 else
6004 error_at (loc, "size of unnamed array has incomplete "
6005 "type");
6006 size = integer_one_node;
6009 size = c_fully_fold (size, false, &size_maybe_const);
6011 if (pedantic && size_maybe_const && integer_zerop (size))
6013 if (name)
6014 pedwarn (loc, OPT_Wpedantic,
6015 "ISO C forbids zero-size array %qE", name);
6016 else
6017 pedwarn (loc, OPT_Wpedantic,
6018 "ISO C forbids zero-size array");
6021 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6023 constant_expression_warning (size);
6024 if (tree_int_cst_sgn (size) < 0)
6026 if (name)
6027 error_at (loc, "size of array %qE is negative", name);
6028 else
6029 error_at (loc, "size of unnamed array is negative");
6030 size = integer_one_node;
6032 /* Handle a size folded to an integer constant but
6033 not an integer constant expression. */
6034 if (!size_int_const)
6036 /* If this is a file scope declaration of an
6037 ordinary identifier, this is invalid code;
6038 diagnosing it here and not subsequently
6039 treating the type as variable-length avoids
6040 more confusing diagnostics later. */
6041 if ((decl_context == NORMAL || decl_context == FIELD)
6042 && current_scope == file_scope)
6043 pedwarn (input_location, 0,
6044 "variably modified %qE at file scope",
6045 name);
6046 else
6047 this_size_varies = size_varies = true;
6048 warn_variable_length_array (name, size);
6051 else if ((decl_context == NORMAL || decl_context == FIELD)
6052 && current_scope == file_scope)
6054 error_at (loc, "variably modified %qE at file scope", name);
6055 size = integer_one_node;
6057 else
6059 /* Make sure the array size remains visibly
6060 nonconstant even if it is (eg) a const variable
6061 with known value. */
6062 this_size_varies = size_varies = true;
6063 warn_variable_length_array (name, size);
6064 if (sanitize_flags_p (SANITIZE_VLA)
6065 && current_function_decl != NULL_TREE
6066 && decl_context == NORMAL)
6068 /* Evaluate the array size only once. */
6069 size = save_expr (size);
6070 size = c_fully_fold (size, false, NULL);
6071 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6072 ubsan_instrument_vla (loc, size),
6073 size);
6077 if (integer_zerop (size) && !this_size_varies)
6079 /* A zero-length array cannot be represented with
6080 an unsigned index type, which is what we'll
6081 get with build_index_type. Create an
6082 open-ended range instead. */
6083 itype = build_range_type (sizetype, size, NULL_TREE);
6085 else
6087 /* Arrange for the SAVE_EXPR on the inside of the
6088 MINUS_EXPR, which allows the -1 to get folded
6089 with the +1 that happens when building TYPE_SIZE. */
6090 if (size_varies)
6091 size = save_expr (size);
6092 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6093 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6094 integer_zero_node, size);
6096 /* Compute the maximum valid index, that is, size
6097 - 1. Do the calculation in index_type, so that
6098 if it is a variable the computations will be
6099 done in the proper mode. */
6100 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6101 convert (index_type, size),
6102 convert (index_type,
6103 size_one_node));
6105 /* The above overflows when size does not fit
6106 in index_type.
6107 ??? While a size of INT_MAX+1 technically shouldn't
6108 cause an overflow (because we subtract 1), handling
6109 this case seems like an unnecessary complication. */
6110 if (TREE_CODE (size) == INTEGER_CST
6111 && !int_fits_type_p (size, index_type))
6113 if (name)
6114 error_at (loc, "size of array %qE is too large",
6115 name);
6116 else
6117 error_at (loc, "size of unnamed array is too large");
6118 type = error_mark_node;
6119 continue;
6122 itype = build_index_type (itype);
6124 if (this_size_varies)
6126 if (*expr)
6127 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6128 *expr, size);
6129 else
6130 *expr = size;
6131 *expr_const_operands &= size_maybe_const;
6134 else if (decl_context == FIELD)
6136 bool flexible_array_member = false;
6137 if (array_parm_vla_unspec_p)
6138 /* Field names can in fact have function prototype
6139 scope so [*] is disallowed here through making
6140 the field variably modified, not through being
6141 something other than a declaration with function
6142 prototype scope. */
6143 size_varies = true;
6144 else
6146 const struct c_declarator *t = declarator;
6147 while (t->kind == cdk_attrs)
6148 t = t->declarator;
6149 flexible_array_member = (t->kind == cdk_id);
6151 if (flexible_array_member
6152 && !in_system_header_at (input_location))
6153 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6154 "support flexible array members");
6156 /* ISO C99 Flexible array members are effectively
6157 identical to GCC's zero-length array extension. */
6158 if (flexible_array_member || array_parm_vla_unspec_p)
6159 itype = build_range_type (sizetype, size_zero_node,
6160 NULL_TREE);
6162 else if (decl_context == PARM)
6164 if (array_parm_vla_unspec_p)
6166 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6167 size_varies = true;
6170 else if (decl_context == TYPENAME)
6172 if (array_parm_vla_unspec_p)
6174 /* C99 6.7.5.2p4 */
6175 warning (0, "%<[*]%> not in a declaration");
6176 /* We use this to avoid messing up with incomplete
6177 array types of the same type, that would
6178 otherwise be modified below. */
6179 itype = build_range_type (sizetype, size_zero_node,
6180 NULL_TREE);
6181 size_varies = true;
6185 /* Complain about arrays of incomplete types. */
6186 if (!COMPLETE_TYPE_P (type))
6188 error_at (loc, "array type has incomplete element type %qT",
6189 type);
6190 /* See if we can be more helpful. */
6191 if (TREE_CODE (type) == ARRAY_TYPE)
6193 if (name)
6194 inform (loc, "declaration of %qE as multidimensional "
6195 "array must have bounds for all dimensions "
6196 "except the first", name);
6197 else
6198 inform (loc, "declaration of multidimensional array "
6199 "must have bounds for all dimensions except "
6200 "the first");
6202 type = error_mark_node;
6204 else
6205 /* When itype is NULL, a shared incomplete array type is
6206 returned for all array of a given type. Elsewhere we
6207 make sure we don't complete that type before copying
6208 it, but here we want to make sure we don't ever
6209 modify the shared type, so we gcc_assert (itype)
6210 below. */
6212 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6213 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6214 type = build_qualified_type (type,
6215 ENCODE_QUAL_ADDR_SPACE (as));
6217 type = build_array_type (type, itype);
6220 if (type != error_mark_node)
6222 if (size_varies)
6224 /* It is ok to modify type here even if itype is
6225 NULL: if size_varies, we're in a
6226 multi-dimensional array and the inner type has
6227 variable size, so the enclosing shared array type
6228 must too. */
6229 if (size && TREE_CODE (size) == INTEGER_CST)
6230 type
6231 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6232 C_TYPE_VARIABLE_SIZE (type) = 1;
6235 /* The GCC extension for zero-length arrays differs from
6236 ISO flexible array members in that sizeof yields
6237 zero. */
6238 if (size && integer_zerop (size))
6240 gcc_assert (itype);
6241 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6242 TYPE_SIZE (type) = bitsize_zero_node;
6243 TYPE_SIZE_UNIT (type) = size_zero_node;
6244 SET_TYPE_STRUCTURAL_EQUALITY (type);
6246 if (array_parm_vla_unspec_p)
6248 gcc_assert (itype);
6249 /* The type is complete. C99 6.7.5.2p4 */
6250 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6251 TYPE_SIZE (type) = bitsize_zero_node;
6252 TYPE_SIZE_UNIT (type) = size_zero_node;
6253 SET_TYPE_STRUCTURAL_EQUALITY (type);
6256 if (!valid_array_size_p (loc, type, name))
6257 type = error_mark_node;
6260 if (decl_context != PARM
6261 && (array_ptr_quals != TYPE_UNQUALIFIED
6262 || array_ptr_attrs != NULL_TREE
6263 || array_parm_static))
6265 error_at (loc, "static or type qualifiers in non-parameter "
6266 "array declarator");
6267 array_ptr_quals = TYPE_UNQUALIFIED;
6268 array_ptr_attrs = NULL_TREE;
6269 array_parm_static = false;
6271 orig_qual_indirect++;
6272 break;
6274 case cdk_function:
6276 /* Say it's a definition only for the declarator closest
6277 to the identifier, apart possibly from some
6278 attributes. */
6279 bool really_funcdef = false;
6280 tree arg_types;
6281 orig_qual_type = NULL_TREE;
6282 if (funcdef_flag)
6284 const struct c_declarator *t = declarator->declarator;
6285 while (t->kind == cdk_attrs)
6286 t = t->declarator;
6287 really_funcdef = (t->kind == cdk_id);
6290 /* Declaring a function type. Make sure we have a valid
6291 type for the function to return. */
6292 if (type == error_mark_node)
6293 continue;
6295 size_varies = false;
6297 /* Warn about some types functions can't return. */
6298 if (TREE_CODE (type) == FUNCTION_TYPE)
6300 if (name)
6301 error_at (loc, "%qE declared as function returning a "
6302 "function", name);
6303 else
6304 error_at (loc, "type name declared as function "
6305 "returning a function");
6306 type = integer_type_node;
6308 if (TREE_CODE (type) == ARRAY_TYPE)
6310 if (name)
6311 error_at (loc, "%qE declared as function returning an array",
6312 name);
6313 else
6314 error_at (loc, "type name declared as function returning "
6315 "an array");
6316 type = integer_type_node;
6319 /* Construct the function type and go to the next
6320 inner layer of declarator. */
6321 arg_info = declarator->u.arg_info;
6322 arg_types = grokparms (arg_info, really_funcdef);
6324 /* Type qualifiers before the return type of the function
6325 qualify the return type, not the function type. */
6326 if (type_quals)
6328 const enum c_declspec_word ignored_quals_list[] =
6330 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6331 cdw_atomic, cdw_number_of_elements
6333 location_t specs_loc
6334 = smallest_type_quals_location (declspecs->locations,
6335 ignored_quals_list);
6336 if (specs_loc == UNKNOWN_LOCATION)
6337 specs_loc = declspecs->locations[cdw_typedef];
6338 if (specs_loc == UNKNOWN_LOCATION)
6339 specs_loc = loc;
6341 /* Type qualifiers on a function return type are
6342 normally permitted by the standard but have no
6343 effect, so give a warning at -Wreturn-type.
6344 Qualifiers on a void return type are banned on
6345 function definitions in ISO C; GCC used to used
6346 them for noreturn functions. The resolution of C11
6347 DR#423 means qualifiers (other than _Atomic) are
6348 actually removed from the return type when
6349 determining the function type. */
6350 int quals_used = type_quals;
6351 if (flag_isoc11)
6352 quals_used &= TYPE_QUAL_ATOMIC;
6353 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6354 pedwarn (specs_loc, 0,
6355 "function definition has qualified void return type");
6356 else
6357 warning_at (specs_loc, OPT_Wignored_qualifiers,
6358 "type qualifiers ignored on function return type");
6360 /* Ensure an error for restrict on invalid types; the
6361 DR#423 resolution is not entirely clear about
6362 this. */
6363 if (flag_isoc11
6364 && (type_quals & TYPE_QUAL_RESTRICT)
6365 && (!POINTER_TYPE_P (type)
6366 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6367 error_at (loc, "invalid use of %<restrict%>");
6368 if (quals_used)
6369 type = c_build_qualified_type (type, quals_used);
6371 type_quals = TYPE_UNQUALIFIED;
6373 type = build_function_type (type, arg_types);
6374 declarator = declarator->declarator;
6376 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6377 the formal parameter list of this FUNCTION_TYPE to point to
6378 the FUNCTION_TYPE node itself. */
6380 c_arg_tag *tag;
6381 unsigned ix;
6383 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6384 TYPE_CONTEXT (tag->type) = type;
6386 break;
6388 case cdk_pointer:
6390 /* Merge any constancy or volatility into the target type
6391 for the pointer. */
6392 if ((type_quals & TYPE_QUAL_ATOMIC)
6393 && TREE_CODE (type) == FUNCTION_TYPE)
6395 error_at (loc,
6396 "%<_Atomic%>-qualified function type");
6397 type_quals &= ~TYPE_QUAL_ATOMIC;
6399 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6400 && type_quals)
6401 pedwarn (loc, OPT_Wpedantic,
6402 "ISO C forbids qualified function types");
6403 if (type_quals)
6404 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6405 orig_qual_indirect);
6406 orig_qual_type = NULL_TREE;
6407 size_varies = false;
6409 /* When the pointed-to type involves components of variable size,
6410 care must be taken to ensure that the size evaluation code is
6411 emitted early enough to dominate all the possible later uses
6412 and late enough for the variables on which it depends to have
6413 been assigned.
6415 This is expected to happen automatically when the pointed-to
6416 type has a name/declaration of it's own, but special attention
6417 is required if the type is anonymous.
6419 We handle the NORMAL and FIELD contexts here by attaching an
6420 artificial TYPE_DECL to such pointed-to type. This forces the
6421 sizes evaluation at a safe point and ensures it is not deferred
6422 until e.g. within a deeper conditional context.
6424 We expect nothing to be needed here for PARM or TYPENAME.
6425 Pushing a TYPE_DECL at this point for TYPENAME would actually
6426 be incorrect, as we might be in the middle of an expression
6427 with side effects on the pointed-to type size "arguments" prior
6428 to the pointer declaration point and the fake TYPE_DECL in the
6429 enclosing context would force the size evaluation prior to the
6430 side effects. */
6432 if (!TYPE_NAME (type)
6433 && (decl_context == NORMAL || decl_context == FIELD)
6434 && variably_modified_type_p (type, NULL_TREE))
6436 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6437 DECL_ARTIFICIAL (decl) = 1;
6438 pushdecl (decl);
6439 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6440 TYPE_NAME (type) = decl;
6443 type = c_build_pointer_type (type);
6445 /* Process type qualifiers (such as const or volatile)
6446 that were given inside the `*'. */
6447 type_quals = declarator->u.pointer_quals;
6449 declarator = declarator->declarator;
6450 break;
6452 default:
6453 gcc_unreachable ();
6456 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6458 /* Now TYPE has the actual type, apart from any qualifiers in
6459 TYPE_QUALS. */
6461 /* Warn about address space used for things other than static memory or
6462 pointers. */
6463 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6464 if (!ADDR_SPACE_GENERIC_P (address_space))
6466 if (decl_context == NORMAL)
6468 switch (storage_class)
6470 case csc_auto:
6471 error ("%qs combined with %<auto%> qualifier for %qE",
6472 c_addr_space_name (address_space), name);
6473 break;
6474 case csc_register:
6475 error ("%qs combined with %<register%> qualifier for %qE",
6476 c_addr_space_name (address_space), name);
6477 break;
6478 case csc_none:
6479 if (current_function_scope)
6481 error ("%qs specified for auto variable %qE",
6482 c_addr_space_name (address_space), name);
6483 break;
6485 break;
6486 case csc_static:
6487 case csc_extern:
6488 case csc_typedef:
6489 break;
6490 default:
6491 gcc_unreachable ();
6494 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6496 if (name)
6497 error ("%qs specified for parameter %qE",
6498 c_addr_space_name (address_space), name);
6499 else
6500 error ("%qs specified for unnamed parameter",
6501 c_addr_space_name (address_space));
6503 else if (decl_context == FIELD)
6505 if (name)
6506 error ("%qs specified for structure field %qE",
6507 c_addr_space_name (address_space), name);
6508 else
6509 error ("%qs specified for structure field",
6510 c_addr_space_name (address_space));
6514 /* Check the type and width of a bit-field. */
6515 if (bitfield)
6517 check_bitfield_type_and_width (loc, &type, width, name);
6518 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6519 atomic types are permitted for bit-fields; we have no code to
6520 make bit-field accesses atomic, so disallow them. */
6521 if (type_quals & TYPE_QUAL_ATOMIC)
6523 if (name)
6524 error_at (loc, "bit-field %qE has atomic type", name);
6525 else
6526 error_at (loc, "bit-field has atomic type");
6527 type_quals &= ~TYPE_QUAL_ATOMIC;
6531 /* Reject invalid uses of _Alignas. */
6532 if (declspecs->alignas_p)
6534 if (storage_class == csc_typedef)
6535 error_at (loc, "alignment specified for typedef %qE", name);
6536 else if (storage_class == csc_register)
6537 error_at (loc, "alignment specified for %<register%> object %qE",
6538 name);
6539 else if (decl_context == PARM)
6541 if (name)
6542 error_at (loc, "alignment specified for parameter %qE", name);
6543 else
6544 error_at (loc, "alignment specified for unnamed parameter");
6546 else if (bitfield)
6548 if (name)
6549 error_at (loc, "alignment specified for bit-field %qE", name);
6550 else
6551 error_at (loc, "alignment specified for unnamed bit-field");
6553 else if (TREE_CODE (type) == FUNCTION_TYPE)
6554 error_at (loc, "alignment specified for function %qE", name);
6555 else if (declspecs->align_log != -1 && TYPE_P (type))
6557 alignas_align = 1U << declspecs->align_log;
6558 if (alignas_align < min_align_of_type (type))
6560 if (name)
6561 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6562 "alignment of %qE", name);
6563 else
6564 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6565 "alignment of unnamed field");
6566 alignas_align = 0;
6571 /* If this is declaring a typedef name, return a TYPE_DECL. */
6573 if (storage_class == csc_typedef)
6575 tree decl;
6576 if ((type_quals & TYPE_QUAL_ATOMIC)
6577 && TREE_CODE (type) == FUNCTION_TYPE)
6579 error_at (loc,
6580 "%<_Atomic%>-qualified function type");
6581 type_quals &= ~TYPE_QUAL_ATOMIC;
6583 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6584 && type_quals)
6585 pedwarn (loc, OPT_Wpedantic,
6586 "ISO C forbids qualified function types");
6587 if (type_quals)
6588 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6589 orig_qual_indirect);
6590 decl = build_decl (declarator->id_loc,
6591 TYPE_DECL, declarator->u.id, type);
6592 if (declspecs->explicit_signed_p)
6593 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6594 if (declspecs->inline_p)
6595 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6596 if (declspecs->noreturn_p)
6597 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6599 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6601 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6603 if (b != NULL
6604 && b->decl != NULL_TREE
6605 && (B_IN_CURRENT_SCOPE (b)
6606 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6607 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6609 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6610 ("using %qD as both a typedef and a tag is "
6611 "invalid in C++"), decl)
6612 && b->locus != UNKNOWN_LOCATION)
6613 inform (b->locus, "originally defined here");
6617 return decl;
6620 /* If this is a type name (such as, in a cast or sizeof),
6621 compute the type and return it now. */
6623 if (decl_context == TYPENAME)
6625 /* Note that the grammar rejects storage classes in typenames
6626 and fields. */
6627 gcc_assert (storage_class == csc_none && !threadp
6628 && !declspecs->inline_p && !declspecs->noreturn_p);
6629 if ((type_quals & TYPE_QUAL_ATOMIC)
6630 && TREE_CODE (type) == FUNCTION_TYPE)
6632 error_at (loc,
6633 "%<_Atomic%>-qualified function type");
6634 type_quals &= ~TYPE_QUAL_ATOMIC;
6636 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6637 && type_quals)
6638 pedwarn (loc, OPT_Wpedantic,
6639 "ISO C forbids const or volatile function types");
6640 if (type_quals)
6641 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6642 orig_qual_indirect);
6643 return type;
6646 if (pedantic && decl_context == FIELD
6647 && variably_modified_type_p (type, NULL_TREE))
6649 /* C99 6.7.2.1p8 */
6650 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6651 "have a variably modified type");
6654 /* Aside from typedefs and type names (handle above),
6655 `void' at top level (not within pointer)
6656 is allowed only in public variables.
6657 We don't complain about parms either, but that is because
6658 a better error message can be made later. */
6660 if (VOID_TYPE_P (type) && decl_context != PARM
6661 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6662 && (storage_class == csc_extern
6663 || (current_scope == file_scope
6664 && !(storage_class == csc_static
6665 || storage_class == csc_register)))))
6667 error_at (loc, "variable or field %qE declared void", name);
6668 type = integer_type_node;
6671 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6672 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6675 tree decl;
6677 if (decl_context == PARM)
6679 tree promoted_type;
6680 bool array_parameter_p = false;
6682 /* A parameter declared as an array of T is really a pointer to T.
6683 One declared as a function is really a pointer to a function. */
6685 if (TREE_CODE (type) == ARRAY_TYPE)
6687 /* Transfer const-ness of array into that of type pointed to. */
6688 type = TREE_TYPE (type);
6689 if (orig_qual_type != NULL_TREE)
6691 if (orig_qual_indirect == 0)
6692 orig_qual_type = TREE_TYPE (orig_qual_type);
6693 else
6694 orig_qual_indirect--;
6696 if (type_quals)
6697 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6698 orig_qual_indirect);
6699 type = c_build_pointer_type (type);
6700 type_quals = array_ptr_quals;
6701 if (type_quals)
6702 type = c_build_qualified_type (type, type_quals);
6704 /* We don't yet implement attributes in this context. */
6705 if (array_ptr_attrs != NULL_TREE)
6706 warning_at (loc, OPT_Wattributes,
6707 "attributes in parameter array declarator ignored");
6709 size_varies = false;
6710 array_parameter_p = true;
6712 else if (TREE_CODE (type) == FUNCTION_TYPE)
6714 if (type_quals & TYPE_QUAL_ATOMIC)
6716 error_at (loc,
6717 "%<_Atomic%>-qualified function type");
6718 type_quals &= ~TYPE_QUAL_ATOMIC;
6720 else if (type_quals)
6721 pedwarn (loc, OPT_Wpedantic,
6722 "ISO C forbids qualified function types");
6723 if (type_quals)
6724 type = c_build_qualified_type (type, type_quals);
6725 type = c_build_pointer_type (type);
6726 type_quals = TYPE_UNQUALIFIED;
6728 else if (type_quals)
6729 type = c_build_qualified_type (type, type_quals);
6731 decl = build_decl (declarator->id_loc,
6732 PARM_DECL, declarator->u.id, type);
6733 if (size_varies)
6734 C_DECL_VARIABLE_SIZE (decl) = 1;
6735 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6737 /* Compute the type actually passed in the parmlist,
6738 for the case where there is no prototype.
6739 (For example, shorts and chars are passed as ints.)
6740 When there is a prototype, this is overridden later. */
6742 if (type == error_mark_node)
6743 promoted_type = type;
6744 else
6745 promoted_type = c_type_promotes_to (type);
6747 DECL_ARG_TYPE (decl) = promoted_type;
6748 if (declspecs->inline_p)
6749 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6750 if (declspecs->noreturn_p)
6751 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6753 else if (decl_context == FIELD)
6755 /* Note that the grammar rejects storage classes in typenames
6756 and fields. */
6757 gcc_assert (storage_class == csc_none && !threadp
6758 && !declspecs->inline_p && !declspecs->noreturn_p);
6760 /* Structure field. It may not be a function. */
6762 if (TREE_CODE (type) == FUNCTION_TYPE)
6764 error_at (loc, "field %qE declared as a function", name);
6765 type = build_pointer_type (type);
6767 else if (TREE_CODE (type) != ERROR_MARK
6768 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6770 if (name)
6771 error_at (loc, "field %qE has incomplete type", name);
6772 else
6773 error_at (loc, "unnamed field has incomplete type");
6774 type = error_mark_node;
6776 else if (TREE_CODE (type) == ARRAY_TYPE
6777 && TYPE_DOMAIN (type) == NULL_TREE)
6779 /* We have a flexible array member through a typedef.
6780 Set suitable range. Whether this is a correct position
6781 for a flexible array member will be determined elsewhere. */
6782 if (!in_system_header_at (input_location))
6783 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6784 "support flexible array members");
6785 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6786 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6787 NULL_TREE);
6788 if (orig_qual_indirect == 0)
6789 orig_qual_type = NULL_TREE;
6791 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6792 orig_qual_indirect);
6793 decl = build_decl (declarator->id_loc,
6794 FIELD_DECL, declarator->u.id, type);
6795 DECL_NONADDRESSABLE_P (decl) = bitfield;
6796 if (bitfield && !declarator->u.id)
6797 TREE_NO_WARNING (decl) = 1;
6799 if (size_varies)
6800 C_DECL_VARIABLE_SIZE (decl) = 1;
6802 else if (TREE_CODE (type) == FUNCTION_TYPE)
6804 if (storage_class == csc_register || threadp)
6806 error_at (loc, "invalid storage class for function %qE", name);
6808 else if (current_scope != file_scope)
6810 /* Function declaration not at file scope. Storage
6811 classes other than `extern' are not allowed, C99
6812 6.7.1p5, and `extern' makes no difference. However,
6813 GCC allows 'auto', perhaps with 'inline', to support
6814 nested functions. */
6815 if (storage_class == csc_auto)
6816 pedwarn (loc, OPT_Wpedantic,
6817 "invalid storage class for function %qE", name);
6818 else if (storage_class == csc_static)
6820 error_at (loc, "invalid storage class for function %qE", name);
6821 if (funcdef_flag)
6822 storage_class = declspecs->storage_class = csc_none;
6823 else
6824 return NULL_TREE;
6828 decl = build_decl (declarator->id_loc,
6829 FUNCTION_DECL, declarator->u.id, type);
6830 decl = build_decl_attribute_variant (decl, decl_attr);
6832 if (type_quals & TYPE_QUAL_ATOMIC)
6834 error_at (loc,
6835 "%<_Atomic%>-qualified function type");
6836 type_quals &= ~TYPE_QUAL_ATOMIC;
6838 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6839 pedwarn (loc, OPT_Wpedantic,
6840 "ISO C forbids qualified function types");
6842 /* Every function declaration is an external reference
6843 (DECL_EXTERNAL) except for those which are not at file
6844 scope and are explicitly declared "auto". This is
6845 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6846 GCC to signify a forward declaration of a nested function. */
6847 if (storage_class == csc_auto && current_scope != file_scope)
6848 DECL_EXTERNAL (decl) = 0;
6849 /* In C99, a function which is declared 'inline' with 'extern'
6850 is not an external reference (which is confusing). It
6851 means that the later definition of the function must be output
6852 in this file, C99 6.7.4p6. In GNU C89, a function declared
6853 'extern inline' is an external reference. */
6854 else if (declspecs->inline_p && storage_class != csc_static)
6855 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6856 == flag_gnu89_inline);
6857 else
6858 DECL_EXTERNAL (decl) = !initialized;
6860 /* Record absence of global scope for `static' or `auto'. */
6861 TREE_PUBLIC (decl)
6862 = !(storage_class == csc_static || storage_class == csc_auto);
6864 /* For a function definition, record the argument information
6865 block where store_parm_decls will look for it. */
6866 if (funcdef_flag)
6867 current_function_arg_info = arg_info;
6869 if (declspecs->default_int_p)
6870 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6872 /* Record presence of `inline' and `_Noreturn', if it is
6873 reasonable. */
6874 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6876 if (declspecs->inline_p)
6877 pedwarn (loc, 0, "cannot inline function %<main%>");
6878 if (declspecs->noreturn_p)
6879 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6881 else
6883 if (declspecs->inline_p)
6884 /* Record that the function is declared `inline'. */
6885 DECL_DECLARED_INLINE_P (decl) = 1;
6886 if (declspecs->noreturn_p)
6888 if (flag_isoc99)
6889 pedwarn_c99 (loc, OPT_Wpedantic,
6890 "ISO C99 does not support %<_Noreturn%>");
6891 else
6892 pedwarn_c99 (loc, OPT_Wpedantic,
6893 "ISO C90 does not support %<_Noreturn%>");
6894 TREE_THIS_VOLATILE (decl) = 1;
6898 else
6900 /* It's a variable. */
6901 /* An uninitialized decl with `extern' is a reference. */
6902 int extern_ref = !initialized && storage_class == csc_extern;
6904 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6905 orig_qual_indirect);
6907 /* C99 6.2.2p7: It is invalid (compile-time undefined
6908 behavior) to create an 'extern' declaration for a
6909 variable if there is a global declaration that is
6910 'static' and the global declaration is not visible.
6911 (If the static declaration _is_ currently visible,
6912 the 'extern' declaration is taken to refer to that decl.) */
6913 if (extern_ref && current_scope != file_scope)
6915 tree global_decl = identifier_global_value (declarator->u.id);
6916 tree visible_decl = lookup_name (declarator->u.id);
6918 if (global_decl
6919 && global_decl != visible_decl
6920 && VAR_P (global_decl)
6921 && !TREE_PUBLIC (global_decl))
6922 error_at (loc, "variable previously declared %<static%> "
6923 "redeclared %<extern%>");
6926 decl = build_decl (declarator->id_loc,
6927 VAR_DECL, declarator->u.id, type);
6928 if (size_varies)
6929 C_DECL_VARIABLE_SIZE (decl) = 1;
6931 if (declspecs->inline_p)
6932 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6933 if (declspecs->noreturn_p)
6934 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6936 /* At file scope, an initialized extern declaration may follow
6937 a static declaration. In that case, DECL_EXTERNAL will be
6938 reset later in start_decl. */
6939 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6941 /* At file scope, the presence of a `static' or `register' storage
6942 class specifier, or the absence of all storage class specifiers
6943 makes this declaration a definition (perhaps tentative). Also,
6944 the absence of `static' makes it public. */
6945 if (current_scope == file_scope)
6947 TREE_PUBLIC (decl) = storage_class != csc_static;
6948 TREE_STATIC (decl) = !extern_ref;
6950 /* Not at file scope, only `static' makes a static definition. */
6951 else
6953 TREE_STATIC (decl) = (storage_class == csc_static);
6954 TREE_PUBLIC (decl) = extern_ref;
6957 if (threadp)
6958 set_decl_tls_model (decl, decl_default_tls_model (decl));
6961 if ((storage_class == csc_extern
6962 || (storage_class == csc_none
6963 && TREE_CODE (type) == FUNCTION_TYPE
6964 && !funcdef_flag))
6965 && variably_modified_type_p (type, NULL_TREE))
6967 /* C99 6.7.5.2p2 */
6968 if (TREE_CODE (type) == FUNCTION_TYPE)
6969 error_at (loc, "non-nested function with variably modified type");
6970 else
6971 error_at (loc, "object with variably modified type must have "
6972 "no linkage");
6975 /* Record `register' declaration for warnings on &
6976 and in case doing stupid register allocation. */
6978 if (storage_class == csc_register)
6980 C_DECL_REGISTER (decl) = 1;
6981 DECL_REGISTER (decl) = 1;
6984 /* Record constancy and volatility. */
6985 c_apply_type_quals_to_decl (type_quals, decl);
6987 /* Apply _Alignas specifiers. */
6988 if (alignas_align)
6990 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
6991 DECL_USER_ALIGN (decl) = 1;
6994 /* If a type has volatile components, it should be stored in memory.
6995 Otherwise, the fact that those components are volatile
6996 will be ignored, and would even crash the compiler.
6997 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6998 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6999 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7000 || TREE_CODE (decl) == RESULT_DECL))
7002 /* It is not an error for a structure with volatile fields to
7003 be declared register, but reset DECL_REGISTER since it
7004 cannot actually go in a register. */
7005 int was_reg = C_DECL_REGISTER (decl);
7006 C_DECL_REGISTER (decl) = 0;
7007 DECL_REGISTER (decl) = 0;
7008 c_mark_addressable (decl);
7009 C_DECL_REGISTER (decl) = was_reg;
7012 /* This is the earliest point at which we might know the assembler
7013 name of a variable. Thus, if it's known before this, die horribly. */
7014 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
7016 if (warn_cxx_compat
7017 && VAR_P (decl)
7018 && TREE_PUBLIC (decl)
7019 && TREE_STATIC (decl)
7020 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7021 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7022 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7023 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7024 ("non-local variable %qD with anonymous type is "
7025 "questionable in C++"),
7026 decl);
7028 return decl;
7032 /* Decode the parameter-list info for a function type or function definition.
7033 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7034 if there is an identifier list instead of a parameter decl list).
7035 These two functions are separate because when a function returns
7036 or receives functions then each is called multiple times but the order
7037 of calls is different. The last call to `grokparms' is always the one
7038 that contains the formal parameter names of a function definition.
7040 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7042 FUNCDEF_FLAG is true for a function definition, false for
7043 a mere declaration. A nonempty identifier-list gets an error message
7044 when FUNCDEF_FLAG is false. */
7046 static tree
7047 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7049 tree arg_types = arg_info->types;
7051 if (funcdef_flag && arg_info->had_vla_unspec)
7053 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7054 /* C99 6.7.5.2p4 */
7055 error ("%<[*]%> not allowed in other than function prototype scope");
7058 if (arg_types == NULL_TREE && !funcdef_flag
7059 && !in_system_header_at (input_location))
7060 warning (OPT_Wstrict_prototypes,
7061 "function declaration isn%'t a prototype");
7063 if (arg_types == error_mark_node)
7064 /* Don't set TYPE_ARG_TYPES in this case. */
7065 return NULL_TREE;
7067 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7069 if (!funcdef_flag)
7071 pedwarn (input_location, 0, "parameter names (without types) in "
7072 "function declaration");
7073 arg_info->parms = NULL_TREE;
7075 else
7076 arg_info->parms = arg_info->types;
7078 arg_info->types = NULL_TREE;
7079 return NULL_TREE;
7081 else
7083 tree parm, type, typelt;
7084 unsigned int parmno;
7086 /* If there is a parameter of incomplete type in a definition,
7087 this is an error. In a declaration this is valid, and a
7088 struct or union type may be completed later, before any calls
7089 or definition of the function. In the case where the tag was
7090 first declared within the parameter list, a warning has
7091 already been given. If a parameter has void type, then
7092 however the function cannot be defined or called, so
7093 warn. */
7095 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7096 parm;
7097 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7099 type = TREE_VALUE (typelt);
7100 if (type == error_mark_node)
7101 continue;
7103 if (!COMPLETE_TYPE_P (type))
7105 if (funcdef_flag)
7107 if (DECL_NAME (parm))
7108 error_at (input_location,
7109 "parameter %u (%q+D) has incomplete type",
7110 parmno, parm);
7111 else
7112 error_at (DECL_SOURCE_LOCATION (parm),
7113 "parameter %u has incomplete type",
7114 parmno);
7116 TREE_VALUE (typelt) = error_mark_node;
7117 TREE_TYPE (parm) = error_mark_node;
7118 arg_types = NULL_TREE;
7120 else if (VOID_TYPE_P (type))
7122 if (DECL_NAME (parm))
7123 warning_at (input_location, 0,
7124 "parameter %u (%q+D) has void type",
7125 parmno, parm);
7126 else
7127 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7128 "parameter %u has void type",
7129 parmno);
7133 if (DECL_NAME (parm) && TREE_USED (parm))
7134 warn_if_shadowing (parm);
7136 return arg_types;
7140 /* Allocate and initialize a c_arg_info structure from the parser's
7141 obstack. */
7143 struct c_arg_info *
7144 build_arg_info (void)
7146 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7147 ret->parms = NULL_TREE;
7148 ret->tags = NULL;
7149 ret->types = NULL_TREE;
7150 ret->others = NULL_TREE;
7151 ret->pending_sizes = NULL;
7152 ret->had_vla_unspec = 0;
7153 return ret;
7156 /* Take apart the current scope and return a c_arg_info structure with
7157 info on a parameter list just parsed.
7159 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7161 ELLIPSIS being true means the argument list ended in '...' so don't
7162 append a sentinel (void_list_node) to the end of the type-list.
7164 EXPR is NULL or an expression that needs to be evaluated for the
7165 side effects of array size expressions in the parameters. */
7167 struct c_arg_info *
7168 get_parm_info (bool ellipsis, tree expr)
7170 struct c_binding *b = current_scope->bindings;
7171 struct c_arg_info *arg_info = build_arg_info ();
7173 tree parms = NULL_TREE;
7174 vec<c_arg_tag, va_gc> *tags = NULL;
7175 tree types = NULL_TREE;
7176 tree others = NULL_TREE;
7178 bool gave_void_only_once_err = false;
7180 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7182 /* The bindings in this scope must not get put into a block.
7183 We will take care of deleting the binding nodes. */
7184 current_scope->bindings = 0;
7186 /* This function is only called if there was *something* on the
7187 parameter list. */
7188 gcc_assert (b);
7190 /* A parameter list consisting solely of 'void' indicates that the
7191 function takes no arguments. But if the 'void' is qualified
7192 (by 'const' or 'volatile'), or has a storage class specifier
7193 ('register'), then the behavior is undefined; issue an error.
7194 Typedefs for 'void' are OK (see DR#157). */
7195 if (b->prev == 0 /* one binding */
7196 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7197 && !DECL_NAME (b->decl) /* anonymous */
7198 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7200 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7201 || C_DECL_REGISTER (b->decl))
7202 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7204 /* There cannot be an ellipsis. */
7205 if (ellipsis)
7206 error_at (b->locus, "%<void%> must be the only parameter");
7208 arg_info->types = void_list_node;
7209 return arg_info;
7212 if (!ellipsis)
7213 types = void_list_node;
7215 /* Break up the bindings list into parms, tags, types, and others;
7216 apply sanity checks; purge the name-to-decl bindings. */
7217 while (b)
7219 tree decl = b->decl;
7220 tree type = TREE_TYPE (decl);
7221 c_arg_tag tag;
7222 const char *keyword;
7224 switch (TREE_CODE (decl))
7226 case PARM_DECL:
7227 if (b->id)
7229 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7230 I_SYMBOL_BINDING (b->id) = b->shadowed;
7233 /* Check for forward decls that never got their actual decl. */
7234 if (TREE_ASM_WRITTEN (decl))
7235 error_at (b->locus,
7236 "parameter %q+D has just a forward declaration", decl);
7237 /* Check for (..., void, ...) and issue an error. */
7238 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7240 if (!gave_void_only_once_err)
7242 error_at (b->locus, "%<void%> must be the only parameter");
7243 gave_void_only_once_err = true;
7246 else
7248 /* Valid parameter, add it to the list. */
7249 DECL_CHAIN (decl) = parms;
7250 parms = decl;
7252 /* Since there is a prototype, args are passed in their
7253 declared types. The back end may override this later. */
7254 DECL_ARG_TYPE (decl) = type;
7255 types = tree_cons (0, type, types);
7257 break;
7259 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7260 case UNION_TYPE: keyword = "union"; goto tag;
7261 case RECORD_TYPE: keyword = "struct"; goto tag;
7262 tag:
7263 /* Types may not have tag-names, in which case the type
7264 appears in the bindings list with b->id NULL. */
7265 if (b->id)
7267 gcc_assert (I_TAG_BINDING (b->id) == b);
7268 I_TAG_BINDING (b->id) = b->shadowed;
7271 /* Warn about any struct, union or enum tags defined in a
7272 parameter list. The scope of such types is limited to
7273 the parameter list, which is rarely if ever desirable
7274 (it's impossible to call such a function with type-
7275 correct arguments). An anonymous union parm type is
7276 meaningful as a GNU extension, so don't warn for that. */
7277 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7279 if (b->id)
7280 /* The %s will be one of 'struct', 'union', or 'enum'. */
7281 warning_at (b->locus, 0,
7282 "%<%s %E%> declared inside parameter list"
7283 " will not be visible outside of this definition or"
7284 " declaration", keyword, b->id);
7285 else
7286 /* The %s will be one of 'struct', 'union', or 'enum'. */
7287 warning_at (b->locus, 0,
7288 "anonymous %s declared inside parameter list"
7289 " will not be visible outside of this definition or"
7290 " declaration", keyword);
7293 tag.id = b->id;
7294 tag.type = decl;
7295 vec_safe_push (tags, tag);
7296 break;
7298 case FUNCTION_DECL:
7299 /* FUNCTION_DECLs appear when there is an implicit function
7300 declaration in the parameter list. */
7301 gcc_assert (b->nested || seen_error ());
7302 goto set_shadowed;
7304 case CONST_DECL:
7305 case TYPE_DECL:
7306 /* CONST_DECLs appear here when we have an embedded enum,
7307 and TYPE_DECLs appear here when we have an embedded struct
7308 or union. No warnings for this - we already warned about the
7309 type itself. */
7311 /* When we reinsert this decl in the function body, we need
7312 to reconstruct whether it was marked as nested. */
7313 gcc_assert (!b->nested);
7314 DECL_CHAIN (decl) = others;
7315 others = decl;
7316 /* fall through */
7318 case ERROR_MARK:
7319 set_shadowed:
7320 /* error_mark_node appears here when we have an undeclared
7321 variable. Just throw it away. */
7322 if (b->id)
7324 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7325 I_SYMBOL_BINDING (b->id) = b->shadowed;
7327 break;
7329 /* Other things that might be encountered. */
7330 case LABEL_DECL:
7331 case VAR_DECL:
7332 default:
7333 gcc_unreachable ();
7336 b = free_binding_and_advance (b);
7339 arg_info->parms = parms;
7340 arg_info->tags = tags;
7341 arg_info->types = types;
7342 arg_info->others = others;
7343 arg_info->pending_sizes = expr;
7344 return arg_info;
7347 /* Get the struct, enum or union (CODE says which) with tag NAME.
7348 Define the tag as a forward-reference with location LOC if it is
7349 not defined. Return a c_typespec structure for the type
7350 specifier. */
7352 struct c_typespec
7353 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7355 struct c_typespec ret;
7356 tree ref;
7357 location_t refloc;
7359 ret.expr = NULL_TREE;
7360 ret.expr_const_operands = true;
7362 /* If a cross reference is requested, look up the type
7363 already defined for this tag and return it. */
7365 ref = lookup_tag (code, name, false, &refloc);
7366 /* If this is the right type of tag, return what we found.
7367 (This reference will be shadowed by shadow_tag later if appropriate.)
7368 If this is the wrong type of tag, do not return it. If it was the
7369 wrong type in the same scope, we will have had an error
7370 message already; if in a different scope and declaring
7371 a name, pending_xref_error will give an error message; but if in a
7372 different scope and not declaring a name, this tag should
7373 shadow the previous declaration of a different type of tag, and
7374 this would not work properly if we return the reference found.
7375 (For example, with "struct foo" in an outer scope, "union foo;"
7376 must shadow that tag with a new one of union type.) */
7377 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7378 if (ref && TREE_CODE (ref) == code)
7380 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7381 && loc != UNKNOWN_LOCATION
7382 && warn_cxx_compat)
7384 switch (code)
7386 case ENUMERAL_TYPE:
7387 warning_at (loc, OPT_Wc___compat,
7388 ("enum type defined in struct or union "
7389 "is not visible in C++"));
7390 inform (refloc, "enum type defined here");
7391 break;
7392 case RECORD_TYPE:
7393 warning_at (loc, OPT_Wc___compat,
7394 ("struct defined in struct or union "
7395 "is not visible in C++"));
7396 inform (refloc, "struct defined here");
7397 break;
7398 case UNION_TYPE:
7399 warning_at (loc, OPT_Wc___compat,
7400 ("union defined in struct or union "
7401 "is not visible in C++"));
7402 inform (refloc, "union defined here");
7403 break;
7404 default:
7405 gcc_unreachable();
7409 ret.spec = ref;
7410 return ret;
7413 /* If no such tag is yet defined, create a forward-reference node
7414 and record it as the "definition".
7415 When a real declaration of this type is found,
7416 the forward-reference will be altered into a real type. */
7418 ref = make_node (code);
7419 if (code == ENUMERAL_TYPE)
7421 /* Give the type a default layout like unsigned int
7422 to avoid crashing if it does not get defined. */
7423 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7424 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7425 TYPE_USER_ALIGN (ref) = 0;
7426 TYPE_UNSIGNED (ref) = 1;
7427 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7428 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7429 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7432 pushtag (loc, name, ref);
7434 ret.spec = ref;
7435 return ret;
7438 /* Get the struct, enum or union (CODE says which) with tag NAME.
7439 Define the tag as a forward-reference if it is not defined.
7440 Return a tree for the type. */
7442 tree
7443 xref_tag (enum tree_code code, tree name)
7445 return parser_xref_tag (input_location, code, name).spec;
7448 /* Make sure that the tag NAME is defined *in the current scope*
7449 at least as a forward reference.
7450 LOC is the location of the struct's definition.
7451 CODE says which kind of tag NAME ought to be.
7453 This stores the current value of the file static STRUCT_PARSE_INFO
7454 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7455 new c_struct_parse_info structure. The old value of
7456 STRUCT_PARSE_INFO is restored in finish_struct. */
7458 tree
7459 start_struct (location_t loc, enum tree_code code, tree name,
7460 struct c_struct_parse_info **enclosing_struct_parse_info)
7462 /* If there is already a tag defined at this scope
7463 (as a forward reference), just return it. */
7465 tree ref = NULL_TREE;
7466 location_t refloc = UNKNOWN_LOCATION;
7468 if (name != NULL_TREE)
7469 ref = lookup_tag (code, name, true, &refloc);
7470 if (ref && TREE_CODE (ref) == code)
7472 if (TYPE_STUB_DECL (ref))
7473 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7475 if (TYPE_SIZE (ref))
7477 if (code == UNION_TYPE)
7478 error_at (loc, "redefinition of %<union %E%>", name);
7479 else
7480 error_at (loc, "redefinition of %<struct %E%>", name);
7481 if (refloc != UNKNOWN_LOCATION)
7482 inform (refloc, "originally defined here");
7483 /* Don't create structures using a name already in use. */
7484 ref = NULL_TREE;
7486 else if (C_TYPE_BEING_DEFINED (ref))
7488 if (code == UNION_TYPE)
7489 error_at (loc, "nested redefinition of %<union %E%>", name);
7490 else
7491 error_at (loc, "nested redefinition of %<struct %E%>", name);
7492 /* Don't bother to report "originally defined here" for a
7493 nested redefinition; the original definition should be
7494 obvious. */
7495 /* Don't create structures that contain themselves. */
7496 ref = NULL_TREE;
7500 /* Otherwise create a forward-reference just so the tag is in scope. */
7502 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7504 ref = make_node (code);
7505 pushtag (loc, name, ref);
7508 C_TYPE_BEING_DEFINED (ref) = 1;
7509 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7510 TYPE_PACKED (v) = flag_pack_struct;
7512 *enclosing_struct_parse_info = struct_parse_info;
7513 struct_parse_info = new c_struct_parse_info ();
7515 /* FIXME: This will issue a warning for a use of a type defined
7516 within a statement expr used within sizeof, et. al. This is not
7517 terribly serious as C++ doesn't permit statement exprs within
7518 sizeof anyhow. */
7519 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7520 warning_at (loc, OPT_Wc___compat,
7521 "defining type in %qs expression is invalid in C++",
7522 (in_sizeof
7523 ? "sizeof"
7524 : (in_typeof ? "typeof" : "alignof")));
7526 return ref;
7529 /* Process the specs, declarator and width (NULL if omitted)
7530 of a structure component, returning a FIELD_DECL node.
7531 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7532 DECL_ATTRS is as for grokdeclarator.
7534 LOC is the location of the structure component.
7536 This is done during the parsing of the struct declaration.
7537 The FIELD_DECL nodes are chained together and the lot of them
7538 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7540 tree
7541 grokfield (location_t loc,
7542 struct c_declarator *declarator, struct c_declspecs *declspecs,
7543 tree width, tree *decl_attrs)
7545 tree value;
7547 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7548 && width == NULL_TREE)
7550 /* This is an unnamed decl.
7552 If we have something of the form "union { list } ;" then this
7553 is the anonymous union extension. Similarly for struct.
7555 If this is something of the form "struct foo;", then
7556 If MS or Plan 9 extensions are enabled, this is handled as
7557 an anonymous struct.
7558 Otherwise this is a forward declaration of a structure tag.
7560 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7561 If foo names a structure or union without a tag, then this
7562 is an anonymous struct (this is permitted by C11).
7563 If MS or Plan 9 extensions are enabled and foo names a
7564 structure, then again this is an anonymous struct.
7565 Otherwise this is an error.
7567 Oh what a horrid tangled web we weave. I wonder if MS consciously
7568 took this from Plan 9 or if it was an accident of implementation
7569 that took root before someone noticed the bug... */
7571 tree type = declspecs->type;
7572 bool ok = false;
7574 if (RECORD_OR_UNION_TYPE_P (type)
7575 && (flag_ms_extensions
7576 || flag_plan9_extensions
7577 || !declspecs->typedef_p))
7579 if (flag_ms_extensions || flag_plan9_extensions)
7580 ok = true;
7581 else if (TYPE_NAME (type) == NULL)
7582 ok = true;
7583 else
7584 ok = false;
7586 if (!ok)
7588 pedwarn (loc, 0, "declaration does not declare anything");
7589 return NULL_TREE;
7591 if (flag_isoc99)
7592 pedwarn_c99 (loc, OPT_Wpedantic,
7593 "ISO C99 doesn%'t support unnamed structs/unions");
7594 else
7595 pedwarn_c99 (loc, OPT_Wpedantic,
7596 "ISO C90 doesn%'t support unnamed structs/unions");
7599 value = grokdeclarator (declarator, declspecs, FIELD, false,
7600 width ? &width : NULL, decl_attrs, NULL, NULL,
7601 DEPRECATED_NORMAL);
7603 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7604 DECL_INITIAL (value) = width;
7606 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7608 /* If we currently have a binding for this field, set the
7609 in_struct field in the binding, so that we warn about lookups
7610 which find it. */
7611 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7612 if (b != NULL)
7614 /* If the in_struct field is not yet set, push it on a list
7615 to be cleared when this struct is finished. */
7616 if (!b->in_struct)
7618 struct_parse_info->fields.safe_push (b);
7619 b->in_struct = 1;
7624 return value;
7627 /* Subroutine of detect_field_duplicates: return whether X and Y,
7628 which are both fields in the same struct, have duplicate field
7629 names. */
7631 static bool
7632 is_duplicate_field (tree x, tree y)
7634 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7635 return true;
7637 /* When using -fplan9-extensions, an anonymous field whose name is a
7638 typedef can duplicate a field name. */
7639 if (flag_plan9_extensions
7640 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7642 tree xt, xn, yt, yn;
7644 xt = TREE_TYPE (x);
7645 if (DECL_NAME (x) != NULL_TREE)
7646 xn = DECL_NAME (x);
7647 else if (RECORD_OR_UNION_TYPE_P (xt)
7648 && TYPE_NAME (xt) != NULL_TREE
7649 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7650 xn = DECL_NAME (TYPE_NAME (xt));
7651 else
7652 xn = NULL_TREE;
7654 yt = TREE_TYPE (y);
7655 if (DECL_NAME (y) != NULL_TREE)
7656 yn = DECL_NAME (y);
7657 else if (RECORD_OR_UNION_TYPE_P (yt)
7658 && TYPE_NAME (yt) != NULL_TREE
7659 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7660 yn = DECL_NAME (TYPE_NAME (yt));
7661 else
7662 yn = NULL_TREE;
7664 if (xn != NULL_TREE && xn == yn)
7665 return true;
7668 return false;
7671 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7672 to HTAB, giving errors for any duplicates. */
7674 static void
7675 detect_field_duplicates_hash (tree fieldlist,
7676 hash_table<nofree_ptr_hash <tree_node> > *htab)
7678 tree x, y;
7679 tree_node **slot;
7681 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7682 if ((y = DECL_NAME (x)) != NULL_TREE)
7684 slot = htab->find_slot (y, INSERT);
7685 if (*slot)
7687 error ("duplicate member %q+D", x);
7688 DECL_NAME (x) = NULL_TREE;
7690 *slot = y;
7692 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7694 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7696 /* When using -fplan9-extensions, an anonymous field whose
7697 name is a typedef can duplicate a field name. */
7698 if (flag_plan9_extensions
7699 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7700 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7702 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7703 slot = htab->find_slot (xn, INSERT);
7704 if (*slot)
7705 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7706 *slot = xn;
7711 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7712 the list such that this does not present a problem later. */
7714 static void
7715 detect_field_duplicates (tree fieldlist)
7717 tree x, y;
7718 int timeout = 10;
7720 /* If the struct is the list of instance variables of an Objective-C
7721 class, then we need to check all the instance variables of
7722 superclasses when checking for duplicates (since you can't have
7723 an instance variable in a subclass with the same name as an
7724 instance variable in a superclass). We pass on this job to the
7725 Objective-C compiler. objc_detect_field_duplicates() will return
7726 false if we are not checking the list of instance variables and
7727 the C frontend should proceed with the standard field duplicate
7728 checks. If we are checking the list of instance variables, the
7729 ObjC frontend will do the check, emit the errors if needed, and
7730 then return true. */
7731 if (c_dialect_objc ())
7732 if (objc_detect_field_duplicates (false))
7733 return;
7735 /* First, see if there are more than "a few" fields.
7736 This is trivially true if there are zero or one fields. */
7737 if (!fieldlist || !DECL_CHAIN (fieldlist))
7738 return;
7739 x = fieldlist;
7740 do {
7741 timeout--;
7742 if (DECL_NAME (x) == NULL_TREE
7743 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7744 timeout = 0;
7745 x = DECL_CHAIN (x);
7746 } while (timeout > 0 && x);
7748 /* If there were "few" fields and no anonymous structures or unions,
7749 avoid the overhead of allocating a hash table. Instead just do
7750 the nested traversal thing. */
7751 if (timeout > 0)
7753 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7754 /* When using -fplan9-extensions, we can have duplicates
7755 between typedef names and fields. */
7756 if (DECL_NAME (x)
7757 || (flag_plan9_extensions
7758 && DECL_NAME (x) == NULL_TREE
7759 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7760 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7761 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7763 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7764 if (is_duplicate_field (y, x))
7766 error ("duplicate member %q+D", x);
7767 DECL_NAME (x) = NULL_TREE;
7771 else
7773 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7774 detect_field_duplicates_hash (fieldlist, &htab);
7778 /* Finish up struct info used by -Wc++-compat. */
7780 static void
7781 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7782 location_t record_loc)
7784 unsigned int ix;
7785 tree x;
7786 struct c_binding *b;
7788 if (fieldlist == NULL_TREE)
7790 if (code == RECORD_TYPE)
7791 warning_at (record_loc, OPT_Wc___compat,
7792 "empty struct has size 0 in C, size 1 in C++");
7793 else
7794 warning_at (record_loc, OPT_Wc___compat,
7795 "empty union has size 0 in C, size 1 in C++");
7798 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7799 the current struct. We do this now at the end of the struct
7800 because the flag is used to issue visibility warnings, and we
7801 only want to issue those warnings if the type is referenced
7802 outside of the struct declaration. */
7803 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7804 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7806 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7807 typedefs used when declaring fields in this struct. If the name
7808 of any of the fields is also a typedef name then the struct would
7809 not parse in C++, because the C++ lookup rules say that the
7810 typedef name would be looked up in the context of the struct, and
7811 would thus be the field rather than the typedef. */
7812 if (!struct_parse_info->typedefs_seen.is_empty ()
7813 && fieldlist != NULL_TREE)
7815 /* Use a hash_set<tree> using the name of the typedef. We can use
7816 a hash_set<tree> because identifiers are interned. */
7817 hash_set<tree> tset;
7819 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7820 tset.add (DECL_NAME (x));
7822 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7824 if (DECL_NAME (x) != NULL_TREE
7825 && tset.contains (DECL_NAME (x)))
7827 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7828 ("using %qD as both field and typedef name is "
7829 "invalid in C++"),
7831 /* FIXME: It would be nice to report the location where
7832 the typedef name is used. */
7837 /* For each field which has a binding and which was not defined in
7838 an enclosing struct, clear the in_struct field. */
7839 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7840 b->in_struct = 0;
7843 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7844 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7845 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7846 ATTRIBUTES are attributes to be applied to the structure.
7848 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7849 the struct was started. */
7851 tree
7852 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7853 struct c_struct_parse_info *enclosing_struct_parse_info)
7855 tree x;
7856 bool toplevel = file_scope == current_scope;
7858 /* If this type was previously laid out as a forward reference,
7859 make sure we lay it out again. */
7861 TYPE_SIZE (t) = NULL_TREE;
7863 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7865 if (pedantic)
7867 for (x = fieldlist; x; x = DECL_CHAIN (x))
7869 if (DECL_NAME (x) != NULL_TREE)
7870 break;
7871 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7872 break;
7875 if (x == NULL_TREE)
7877 if (TREE_CODE (t) == UNION_TYPE)
7879 if (fieldlist)
7880 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7881 else
7882 pedwarn (loc, OPT_Wpedantic, "union has no members");
7884 else
7886 if (fieldlist)
7887 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7888 else
7889 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7894 /* Install struct as DECL_CONTEXT of each field decl.
7895 Also process specified field sizes, found in the DECL_INITIAL,
7896 storing 0 there after the type has been changed to precision equal
7897 to its width, rather than the precision of the specified standard
7898 type. (Correct layout requires the original type to have been preserved
7899 until now.) */
7901 bool saw_named_field = false;
7902 for (x = fieldlist; x; x = DECL_CHAIN (x))
7904 if (TREE_TYPE (x) == error_mark_node)
7905 continue;
7907 DECL_CONTEXT (x) = t;
7909 /* If any field is const, the structure type is pseudo-const. */
7910 if (TREE_READONLY (x))
7911 C_TYPE_FIELDS_READONLY (t) = 1;
7912 else
7914 /* A field that is pseudo-const makes the structure likewise. */
7915 tree t1 = strip_array_types (TREE_TYPE (x));
7916 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7917 C_TYPE_FIELDS_READONLY (t) = 1;
7920 /* Any field that is volatile means variables of this type must be
7921 treated in some ways as volatile. */
7922 if (TREE_THIS_VOLATILE (x))
7923 C_TYPE_FIELDS_VOLATILE (t) = 1;
7925 /* Any field of nominal variable size implies structure is too. */
7926 if (C_DECL_VARIABLE_SIZE (x))
7927 C_TYPE_VARIABLE_SIZE (t) = 1;
7929 if (DECL_INITIAL (x))
7931 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7932 DECL_SIZE (x) = bitsize_int (width);
7933 DECL_BIT_FIELD (x) = 1;
7934 SET_DECL_C_BIT_FIELD (x);
7937 if (TYPE_PACKED (t)
7938 && (DECL_BIT_FIELD (x)
7939 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7940 DECL_PACKED (x) = 1;
7942 /* Detect flexible array member in an invalid context. */
7943 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7944 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7945 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7946 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7948 if (TREE_CODE (t) == UNION_TYPE)
7950 error_at (DECL_SOURCE_LOCATION (x),
7951 "flexible array member in union");
7952 TREE_TYPE (x) = error_mark_node;
7954 else if (DECL_CHAIN (x) != NULL_TREE)
7956 error_at (DECL_SOURCE_LOCATION (x),
7957 "flexible array member not at end of struct");
7958 TREE_TYPE (x) = error_mark_node;
7960 else if (!saw_named_field)
7962 error_at (DECL_SOURCE_LOCATION (x),
7963 "flexible array member in a struct with no named "
7964 "members");
7965 TREE_TYPE (x) = error_mark_node;
7969 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7970 && flexible_array_type_p (TREE_TYPE (x)))
7971 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7972 "invalid use of structure with flexible array member");
7974 if (DECL_NAME (x)
7975 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7976 saw_named_field = true;
7979 detect_field_duplicates (fieldlist);
7981 /* Now we have the nearly final fieldlist. Record it,
7982 then lay out the structure or union (including the fields). */
7984 TYPE_FIELDS (t) = fieldlist;
7986 maybe_apply_pragma_scalar_storage_order (t);
7988 layout_type (t);
7990 if (TYPE_SIZE_UNIT (t)
7991 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7992 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7993 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7994 error ("type %qT is too large", t);
7996 /* Give bit-fields their proper types and rewrite the type of array fields
7997 with scalar component if the enclosing type has reverse storage order. */
7998 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8000 if (TREE_CODE (field) == FIELD_DECL
8001 && DECL_INITIAL (field)
8002 && TREE_TYPE (field) != error_mark_node)
8004 unsigned HOST_WIDE_INT width
8005 = tree_to_uhwi (DECL_INITIAL (field));
8006 tree type = TREE_TYPE (field);
8007 if (width != TYPE_PRECISION (type))
8009 TREE_TYPE (field)
8010 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8011 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8013 DECL_INITIAL (field) = NULL_TREE;
8015 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8016 && TREE_CODE (field) == FIELD_DECL
8017 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8019 tree ftype = TREE_TYPE (field);
8020 tree ctype = strip_array_types (ftype);
8021 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8023 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8024 tree *typep = &fmain_type;
8025 do {
8026 *typep = build_distinct_type_copy (*typep);
8027 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8028 typep = &TREE_TYPE (*typep);
8029 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8030 TREE_TYPE (field)
8031 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8036 /* Now we have the truly final field list.
8037 Store it in this type and in the variants. */
8039 TYPE_FIELDS (t) = fieldlist;
8041 /* If there are lots of fields, sort so we can look through them fast.
8042 We arbitrarily consider 16 or more elts to be "a lot". */
8045 int len = 0;
8047 for (x = fieldlist; x; x = DECL_CHAIN (x))
8049 if (len > 15 || DECL_NAME (x) == NULL)
8050 break;
8051 len += 1;
8054 if (len > 15)
8056 tree *field_array;
8057 struct lang_type *space;
8058 struct sorted_fields_type *space2;
8060 len += list_length (x);
8062 /* Use the same allocation policy here that make_node uses, to
8063 ensure that this lives as long as the rest of the struct decl.
8064 All decls in an inline function need to be saved. */
8066 space = ggc_cleared_alloc<struct lang_type> ();
8067 space2 = (sorted_fields_type *) ggc_internal_alloc
8068 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8070 len = 0;
8071 space->s = space2;
8072 field_array = &space2->elts[0];
8073 for (x = fieldlist; x; x = DECL_CHAIN (x))
8075 field_array[len++] = x;
8077 /* If there is anonymous struct or union, break out of the loop. */
8078 if (DECL_NAME (x) == NULL)
8079 break;
8081 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8082 if (x == NULL)
8084 TYPE_LANG_SPECIFIC (t) = space;
8085 TYPE_LANG_SPECIFIC (t)->s->len = len;
8086 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8087 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8092 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8093 in dwarf2out via rest_of_decl_compilation below and means
8094 something totally different. Since we will be clearing
8095 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8096 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8097 C_TYPE_INCOMPLETE_VARS should use some language specific
8098 node. */
8099 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8100 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8102 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8103 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8104 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8105 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8106 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8107 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8110 /* If this was supposed to be a transparent union, but we can't
8111 make it one, warn and turn off the flag. */
8112 if (TREE_CODE (t) == UNION_TYPE
8113 && TYPE_TRANSPARENT_AGGR (t)
8114 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8116 TYPE_TRANSPARENT_AGGR (t) = 0;
8117 warning_at (loc, 0, "union cannot be made transparent");
8120 /* If this structure or union completes the type of any previous
8121 variable declaration, lay it out and output its rtl. */
8122 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8124 tree decl = TREE_VALUE (x);
8125 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8126 layout_array_type (TREE_TYPE (decl));
8127 if (TREE_CODE (decl) != TYPE_DECL)
8129 layout_decl (decl, 0);
8130 if (c_dialect_objc ())
8131 objc_check_decl (decl);
8132 rest_of_decl_compilation (decl, toplevel, 0);
8136 /* Update type location to the one of the definition, instead of e.g.
8137 a forward declaration. */
8138 if (TYPE_STUB_DECL (t))
8139 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8141 /* Finish debugging output for this type. */
8142 rest_of_type_compilation (t, toplevel);
8144 /* If we're inside a function proper, i.e. not file-scope and not still
8145 parsing parameters, then arrange for the size of a variable sized type
8146 to be bound now. */
8147 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8148 add_stmt (build_stmt (loc,
8149 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8151 if (warn_cxx_compat)
8152 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8154 delete struct_parse_info;
8156 struct_parse_info = enclosing_struct_parse_info;
8158 /* If this struct is defined inside a struct, add it to
8159 struct_types. */
8160 if (warn_cxx_compat
8161 && struct_parse_info != NULL
8162 && !in_sizeof && !in_typeof && !in_alignof)
8163 struct_parse_info->struct_types.safe_push (t);
8165 return t;
8168 /* Lay out the type T, and its element type, and so on. */
8170 static void
8171 layout_array_type (tree t)
8173 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8174 layout_array_type (TREE_TYPE (t));
8175 layout_type (t);
8178 /* Begin compiling the definition of an enumeration type.
8179 NAME is its name (or null if anonymous).
8180 LOC is the enum's location.
8181 Returns the type object, as yet incomplete.
8182 Also records info about it so that build_enumerator
8183 may be used to declare the individual values as they are read. */
8185 tree
8186 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8188 tree enumtype = NULL_TREE;
8189 location_t enumloc = UNKNOWN_LOCATION;
8191 /* If this is the real definition for a previous forward reference,
8192 fill in the contents in the same object that used to be the
8193 forward reference. */
8195 if (name != NULL_TREE)
8196 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8198 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8200 enumtype = make_node (ENUMERAL_TYPE);
8201 pushtag (loc, name, enumtype);
8203 /* Update type location to the one of the definition, instead of e.g.
8204 a forward declaration. */
8205 else if (TYPE_STUB_DECL (enumtype))
8207 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8208 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8211 if (C_TYPE_BEING_DEFINED (enumtype))
8212 error_at (loc, "nested redefinition of %<enum %E%>", name);
8214 C_TYPE_BEING_DEFINED (enumtype) = 1;
8216 if (TYPE_VALUES (enumtype) != NULL_TREE)
8218 /* This enum is a named one that has been declared already. */
8219 error_at (loc, "redeclaration of %<enum %E%>", name);
8220 if (enumloc != UNKNOWN_LOCATION)
8221 inform (enumloc, "originally defined here");
8223 /* Completely replace its old definition.
8224 The old enumerators remain defined, however. */
8225 TYPE_VALUES (enumtype) = NULL_TREE;
8228 the_enum->enum_next_value = integer_zero_node;
8229 the_enum->enum_overflow = 0;
8231 if (flag_short_enums)
8232 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8233 TYPE_PACKED (v) = 1;
8235 /* FIXME: This will issue a warning for a use of a type defined
8236 within sizeof in a statement expr. This is not terribly serious
8237 as C++ doesn't permit statement exprs within sizeof anyhow. */
8238 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8239 warning_at (loc, OPT_Wc___compat,
8240 "defining type in %qs expression is invalid in C++",
8241 (in_sizeof
8242 ? "sizeof"
8243 : (in_typeof ? "typeof" : "alignof")));
8245 return enumtype;
8248 /* After processing and defining all the values of an enumeration type,
8249 install their decls in the enumeration type and finish it off.
8250 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8251 and ATTRIBUTES are the specified attributes.
8252 Returns ENUMTYPE. */
8254 tree
8255 finish_enum (tree enumtype, tree values, tree attributes)
8257 tree pair, tem;
8258 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8259 int precision;
8260 signop sign;
8261 bool toplevel = (file_scope == current_scope);
8262 struct lang_type *lt;
8264 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8266 /* Calculate the maximum value of any enumerator in this type. */
8268 if (values == error_mark_node)
8269 minnode = maxnode = integer_zero_node;
8270 else
8272 minnode = maxnode = TREE_VALUE (values);
8273 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8275 tree value = TREE_VALUE (pair);
8276 if (tree_int_cst_lt (maxnode, value))
8277 maxnode = value;
8278 if (tree_int_cst_lt (value, minnode))
8279 minnode = value;
8283 /* Construct the final type of this enumeration. It is the same
8284 as one of the integral types - the narrowest one that fits, except
8285 that normally we only go as narrow as int - and signed iff any of
8286 the values are negative. */
8287 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8288 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8289 tree_int_cst_min_precision (maxnode, sign));
8291 /* If the precision of the type was specified with an attribute and it
8292 was too small, give an error. Otherwise, use it. */
8293 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8295 if (precision > TYPE_PRECISION (enumtype))
8297 TYPE_PRECISION (enumtype) = 0;
8298 error ("specified mode too small for enumeral values");
8300 else
8301 precision = TYPE_PRECISION (enumtype);
8303 else
8304 TYPE_PRECISION (enumtype) = 0;
8306 if (TYPE_PACKED (enumtype)
8307 || precision > TYPE_PRECISION (integer_type_node)
8308 || TYPE_PRECISION (enumtype))
8310 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8311 if (tem == NULL)
8313 warning (0, "enumeration values exceed range of largest integer");
8314 tem = long_long_integer_type_node;
8317 else
8318 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8320 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8321 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8322 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8323 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8324 TYPE_SIZE (enumtype) = NULL_TREE;
8325 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8327 layout_type (enumtype);
8329 if (values != error_mark_node)
8331 /* Change the type of the enumerators to be the enum type. We
8332 need to do this irrespective of the size of the enum, for
8333 proper type checking. Replace the DECL_INITIALs of the
8334 enumerators, and the value slots of the list, with copies
8335 that have the enum type; they cannot be modified in place
8336 because they may be shared (e.g. integer_zero_node) Finally,
8337 change the purpose slots to point to the names of the decls. */
8338 for (pair = values; pair; pair = TREE_CHAIN (pair))
8340 tree enu = TREE_PURPOSE (pair);
8341 tree ini = DECL_INITIAL (enu);
8343 TREE_TYPE (enu) = enumtype;
8345 /* The ISO C Standard mandates enumerators to have type int,
8346 even though the underlying type of an enum type is
8347 unspecified. However, GCC allows enumerators of any
8348 integer type as an extensions. build_enumerator()
8349 converts any enumerators that fit in an int to type int,
8350 to avoid promotions to unsigned types when comparing
8351 integers with enumerators that fit in the int range.
8352 When -pedantic is given, build_enumerator() would have
8353 already warned about those that don't fit. Here we
8354 convert the rest to the enumerator type. */
8355 if (TREE_TYPE (ini) != integer_type_node)
8356 ini = convert (enumtype, ini);
8358 DECL_INITIAL (enu) = ini;
8359 TREE_PURPOSE (pair) = DECL_NAME (enu);
8360 TREE_VALUE (pair) = ini;
8363 TYPE_VALUES (enumtype) = values;
8366 /* Record the min/max values so that we can warn about bit-field
8367 enumerations that are too small for the values. */
8368 lt = ggc_cleared_alloc<struct lang_type> ();
8369 lt->enum_min = minnode;
8370 lt->enum_max = maxnode;
8371 TYPE_LANG_SPECIFIC (enumtype) = lt;
8373 /* Fix up all variant types of this enum type. */
8374 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8376 if (tem == enumtype)
8377 continue;
8378 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8379 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8380 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8381 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8382 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8383 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8384 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8385 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8386 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8387 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8388 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8391 /* Finish debugging output for this type. */
8392 rest_of_type_compilation (enumtype, toplevel);
8394 /* If this enum is defined inside a struct, add it to
8395 struct_types. */
8396 if (warn_cxx_compat
8397 && struct_parse_info != NULL
8398 && !in_sizeof && !in_typeof && !in_alignof)
8399 struct_parse_info->struct_types.safe_push (enumtype);
8401 return enumtype;
8404 /* Build and install a CONST_DECL for one value of the
8405 current enumeration type (one that was begun with start_enum).
8406 DECL_LOC is the location of the enumerator.
8407 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8408 Return a tree-list containing the CONST_DECL and its value.
8409 Assignment of sequential values by default is handled here. */
8411 tree
8412 build_enumerator (location_t decl_loc, location_t loc,
8413 struct c_enum_contents *the_enum, tree name, tree value)
8415 tree decl, type;
8417 /* Validate and default VALUE. */
8419 if (value != NULL_TREE)
8421 /* Don't issue more errors for error_mark_node (i.e. an
8422 undeclared identifier) - just ignore the value expression. */
8423 if (value == error_mark_node)
8424 value = NULL_TREE;
8425 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8427 error_at (loc, "enumerator value for %qE is not an integer constant",
8428 name);
8429 value = NULL_TREE;
8431 else
8433 if (TREE_CODE (value) != INTEGER_CST)
8435 value = c_fully_fold (value, false, NULL);
8436 if (TREE_CODE (value) == INTEGER_CST)
8437 pedwarn (loc, OPT_Wpedantic,
8438 "enumerator value for %qE is not an integer "
8439 "constant expression", name);
8441 if (TREE_CODE (value) != INTEGER_CST)
8443 error ("enumerator value for %qE is not an integer constant",
8444 name);
8445 value = NULL_TREE;
8447 else
8449 value = default_conversion (value);
8450 constant_expression_warning (value);
8455 /* Default based on previous value. */
8456 /* It should no longer be possible to have NON_LVALUE_EXPR
8457 in the default. */
8458 if (value == NULL_TREE)
8460 value = the_enum->enum_next_value;
8461 if (the_enum->enum_overflow)
8462 error_at (loc, "overflow in enumeration values");
8464 /* Even though the underlying type of an enum is unspecified, the
8465 type of enumeration constants is explicitly defined as int
8466 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8467 an extension. */
8468 else if (!int_fits_type_p (value, integer_type_node))
8469 pedwarn (loc, OPT_Wpedantic,
8470 "ISO C restricts enumerator values to range of %<int%>");
8472 /* The ISO C Standard mandates enumerators to have type int, even
8473 though the underlying type of an enum type is unspecified.
8474 However, GCC allows enumerators of any integer type as an
8475 extensions. Here we convert any enumerators that fit in an int
8476 to type int, to avoid promotions to unsigned types when comparing
8477 integers with enumerators that fit in the int range. When
8478 -pedantic is given, we would have already warned about those that
8479 don't fit. We have to do this here rather than in finish_enum
8480 because this value may be used to define more enumerators. */
8481 if (int_fits_type_p (value, integer_type_node))
8482 value = convert (integer_type_node, value);
8484 /* Set basis for default for next value. */
8485 the_enum->enum_next_value
8486 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8487 PLUS_EXPR, value, integer_one_node, false);
8488 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8490 /* Now create a declaration for the enum value name. */
8492 type = TREE_TYPE (value);
8493 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8494 TYPE_PRECISION (integer_type_node)),
8495 (TYPE_PRECISION (type)
8496 >= TYPE_PRECISION (integer_type_node)
8497 && TYPE_UNSIGNED (type)));
8499 decl = build_decl (decl_loc, CONST_DECL, name, type);
8500 DECL_INITIAL (decl) = convert (type, value);
8501 pushdecl (decl);
8503 return tree_cons (decl, value, NULL_TREE);
8507 /* Create the FUNCTION_DECL for a function definition.
8508 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8509 the declaration; they describe the function's name and the type it returns,
8510 but twisted together in a fashion that parallels the syntax of C.
8512 This function creates a binding context for the function body
8513 as well as setting up the FUNCTION_DECL in current_function_decl.
8515 Returns true on success. If the DECLARATOR is not suitable for a function
8516 (it defines a datum instead), we return false to report a parse error. */
8518 bool
8519 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8520 tree attributes)
8522 tree decl1, old_decl;
8523 tree restype, resdecl;
8524 location_t loc;
8526 current_function_returns_value = 0; /* Assume, until we see it does. */
8527 current_function_returns_null = 0;
8528 current_function_returns_abnormally = 0;
8529 warn_about_return_type = 0;
8530 c_switch_stack = NULL;
8532 /* Indicate no valid break/continue context by setting these variables
8533 to some non-null, non-label value. We'll notice and emit the proper
8534 error message in c_finish_bc_stmt. */
8535 c_break_label = c_cont_label = size_zero_node;
8537 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8538 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8539 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8541 /* If the declarator is not suitable for a function definition,
8542 cause a syntax error. */
8543 if (decl1 == NULL_TREE
8544 || TREE_CODE (decl1) != FUNCTION_DECL)
8545 return false;
8547 loc = DECL_SOURCE_LOCATION (decl1);
8549 c_decl_attributes (&decl1, attributes, 0);
8551 if (DECL_DECLARED_INLINE_P (decl1)
8552 && DECL_UNINLINABLE (decl1)
8553 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8554 warning_at (loc, OPT_Wattributes,
8555 "inline function %qD given attribute noinline",
8556 decl1);
8558 /* Handle gnu_inline attribute. */
8559 if (declspecs->inline_p
8560 && !flag_gnu89_inline
8561 && TREE_CODE (decl1) == FUNCTION_DECL
8562 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8563 || current_function_decl))
8565 if (declspecs->storage_class != csc_static)
8566 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8569 announce_function (decl1);
8571 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8573 error_at (loc, "return type is an incomplete type");
8574 /* Make it return void instead. */
8575 TREE_TYPE (decl1)
8576 = build_function_type (void_type_node,
8577 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8580 if (warn_about_return_type)
8581 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8582 : (warn_return_type ? OPT_Wreturn_type
8583 : OPT_Wimplicit_int),
8584 "return type defaults to %<int%>");
8586 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8587 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8588 DECL_INITIAL (decl1) = error_mark_node;
8590 /* A nested function is not global. */
8591 if (current_function_decl != NULL_TREE)
8592 TREE_PUBLIC (decl1) = 0;
8594 /* If this definition isn't a prototype and we had a prototype declaration
8595 before, copy the arg type info from that prototype. */
8596 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8597 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8598 old_decl = NULL_TREE;
8599 current_function_prototype_locus = UNKNOWN_LOCATION;
8600 current_function_prototype_built_in = false;
8601 current_function_prototype_arg_types = NULL_TREE;
8602 if (!prototype_p (TREE_TYPE (decl1)))
8604 if (old_decl != NULL_TREE
8605 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8606 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8607 TREE_TYPE (TREE_TYPE (old_decl))))
8609 if (stdarg_p (TREE_TYPE (old_decl)))
8611 warning_at (loc, 0, "%q+D defined as variadic function "
8612 "without prototype", decl1);
8613 locate_old_decl (old_decl);
8615 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8616 TREE_TYPE (decl1));
8617 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8618 current_function_prototype_built_in
8619 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8620 current_function_prototype_arg_types
8621 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8623 if (TREE_PUBLIC (decl1))
8625 /* If there is an external prototype declaration of this
8626 function, record its location but do not copy information
8627 to this decl. This may be an invisible declaration
8628 (built-in or in a scope which has finished) or simply
8629 have more refined argument types than any declaration
8630 found above. */
8631 struct c_binding *b;
8632 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8633 if (B_IN_SCOPE (b, external_scope))
8634 break;
8635 if (b)
8637 tree ext_decl, ext_type;
8638 ext_decl = b->decl;
8639 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8640 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8641 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8642 TREE_TYPE (ext_type)))
8644 current_function_prototype_locus
8645 = DECL_SOURCE_LOCATION (ext_decl);
8646 current_function_prototype_built_in
8647 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8648 current_function_prototype_arg_types
8649 = TYPE_ARG_TYPES (ext_type);
8655 /* Optionally warn of old-fashioned def with no previous prototype. */
8656 if (warn_strict_prototypes
8657 && old_decl != error_mark_node
8658 && !prototype_p (TREE_TYPE (decl1))
8659 && C_DECL_ISNT_PROTOTYPE (old_decl))
8660 warning_at (loc, OPT_Wstrict_prototypes,
8661 "function declaration isn%'t a prototype");
8662 /* Optionally warn of any global def with no previous prototype. */
8663 else if (warn_missing_prototypes
8664 && old_decl != error_mark_node
8665 && TREE_PUBLIC (decl1)
8666 && !MAIN_NAME_P (DECL_NAME (decl1))
8667 && C_DECL_ISNT_PROTOTYPE (old_decl)
8668 && !DECL_DECLARED_INLINE_P (decl1))
8669 warning_at (loc, OPT_Wmissing_prototypes,
8670 "no previous prototype for %qD", decl1);
8671 /* Optionally warn of any def with no previous prototype
8672 if the function has already been used. */
8673 else if (warn_missing_prototypes
8674 && old_decl != NULL_TREE
8675 && old_decl != error_mark_node
8676 && TREE_USED (old_decl)
8677 && !prototype_p (TREE_TYPE (old_decl)))
8678 warning_at (loc, OPT_Wmissing_prototypes,
8679 "%qD was used with no prototype before its definition", decl1);
8680 /* Optionally warn of any global def with no previous declaration. */
8681 else if (warn_missing_declarations
8682 && TREE_PUBLIC (decl1)
8683 && old_decl == NULL_TREE
8684 && !MAIN_NAME_P (DECL_NAME (decl1))
8685 && !DECL_DECLARED_INLINE_P (decl1))
8686 warning_at (loc, OPT_Wmissing_declarations,
8687 "no previous declaration for %qD",
8688 decl1);
8689 /* Optionally warn of any def with no previous declaration
8690 if the function has already been used. */
8691 else if (warn_missing_declarations
8692 && old_decl != NULL_TREE
8693 && old_decl != error_mark_node
8694 && TREE_USED (old_decl)
8695 && C_DECL_IMPLICIT (old_decl))
8696 warning_at (loc, OPT_Wmissing_declarations,
8697 "%qD was used with no declaration before its definition", decl1);
8699 /* This function exists in static storage.
8700 (This does not mean `static' in the C sense!) */
8701 TREE_STATIC (decl1) = 1;
8703 /* This is the earliest point at which we might know the assembler
8704 name of the function. Thus, if it's set before this, die horribly. */
8705 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8707 /* If #pragma weak was used, mark the decl weak now. */
8708 if (current_scope == file_scope)
8709 maybe_apply_pragma_weak (decl1);
8711 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8712 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8714 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8715 != integer_type_node)
8716 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8717 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8718 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8719 decl1);
8721 check_main_parameter_types (decl1);
8723 if (!TREE_PUBLIC (decl1))
8724 pedwarn (loc, OPT_Wmain,
8725 "%qD is normally a non-static function", decl1);
8728 /* Record the decl so that the function name is defined.
8729 If we already have a decl for this name, and it is a FUNCTION_DECL,
8730 use the old decl. */
8732 current_function_decl = pushdecl (decl1);
8734 push_scope ();
8735 declare_parm_level ();
8737 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8738 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8739 DECL_ARTIFICIAL (resdecl) = 1;
8740 DECL_IGNORED_P (resdecl) = 1;
8741 DECL_RESULT (current_function_decl) = resdecl;
8743 start_fname_decls ();
8745 return true;
8748 /* Subroutine of store_parm_decls which handles new-style function
8749 definitions (prototype format). The parms already have decls, so we
8750 need only record them as in effect and complain if any redundant
8751 old-style parm decls were written. */
8752 static void
8753 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8755 tree decl;
8756 c_arg_tag *tag;
8757 unsigned ix;
8759 if (current_scope->bindings)
8761 error_at (DECL_SOURCE_LOCATION (fndecl),
8762 "old-style parameter declarations in prototyped "
8763 "function definition");
8765 /* Get rid of the old-style declarations. */
8766 pop_scope ();
8767 push_scope ();
8769 /* Don't issue this warning for nested functions, and don't issue this
8770 warning if we got here because ARG_INFO_TYPES was error_mark_node
8771 (this happens when a function definition has just an ellipsis in
8772 its parameter list). */
8773 else if (!in_system_header_at (input_location)
8774 && !current_function_scope
8775 && arg_info->types != error_mark_node)
8776 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8777 "traditional C rejects ISO C style function definitions");
8779 /* Now make all the parameter declarations visible in the function body.
8780 We can bypass most of the grunt work of pushdecl. */
8781 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8783 DECL_CONTEXT (decl) = current_function_decl;
8784 if (DECL_NAME (decl))
8786 bind (DECL_NAME (decl), decl, current_scope,
8787 /*invisible=*/false, /*nested=*/false,
8788 UNKNOWN_LOCATION);
8789 if (!TREE_USED (decl))
8790 warn_if_shadowing (decl);
8792 else
8793 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8796 /* Record the parameter list in the function declaration. */
8797 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8799 /* Now make all the ancillary declarations visible, likewise. */
8800 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8802 DECL_CONTEXT (decl) = current_function_decl;
8803 if (DECL_NAME (decl))
8804 bind (DECL_NAME (decl), decl, current_scope,
8805 /*invisible=*/false,
8806 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8807 UNKNOWN_LOCATION);
8810 /* And all the tag declarations. */
8811 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8812 if (tag->id)
8813 bind (tag->id, tag->type, current_scope,
8814 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8817 /* Subroutine of store_parm_decls which handles old-style function
8818 definitions (separate parameter list and declarations). */
8820 static void
8821 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8823 struct c_binding *b;
8824 tree parm, decl, last;
8825 tree parmids = arg_info->parms;
8826 hash_set<tree> seen_args;
8828 if (!in_system_header_at (input_location))
8829 warning_at (DECL_SOURCE_LOCATION (fndecl),
8830 OPT_Wold_style_definition, "old-style function definition");
8832 /* Match each formal parameter name with its declaration. Save each
8833 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8834 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8836 if (TREE_VALUE (parm) == NULL_TREE)
8838 error_at (DECL_SOURCE_LOCATION (fndecl),
8839 "parameter name missing from parameter list");
8840 TREE_PURPOSE (parm) = NULL_TREE;
8841 continue;
8844 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8845 if (b && B_IN_CURRENT_SCOPE (b))
8847 decl = b->decl;
8848 /* Skip erroneous parameters. */
8849 if (decl == error_mark_node)
8850 continue;
8851 /* If we got something other than a PARM_DECL it is an error. */
8852 if (TREE_CODE (decl) != PARM_DECL)
8854 error_at (DECL_SOURCE_LOCATION (decl),
8855 "%qD declared as a non-parameter", decl);
8856 continue;
8858 /* If the declaration is already marked, we have a duplicate
8859 name. Complain and ignore the duplicate. */
8860 else if (seen_args.contains (decl))
8862 error_at (DECL_SOURCE_LOCATION (decl),
8863 "multiple parameters named %qD", decl);
8864 TREE_PURPOSE (parm) = NULL_TREE;
8865 continue;
8867 /* If the declaration says "void", complain and turn it into
8868 an int. */
8869 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8871 error_at (DECL_SOURCE_LOCATION (decl),
8872 "parameter %qD declared with void type", decl);
8873 TREE_TYPE (decl) = integer_type_node;
8874 DECL_ARG_TYPE (decl) = integer_type_node;
8875 layout_decl (decl, 0);
8877 warn_if_shadowing (decl);
8879 /* If no declaration found, default to int. */
8880 else
8882 /* FIXME diagnostics: This should be the location of the argument,
8883 not the FNDECL. E.g., for an old-style declaration
8885 int f10(v) { blah; }
8887 We should use the location of the V, not the F10.
8888 Unfortunately, the V is an IDENTIFIER_NODE which has no
8889 location. In the future we need locations for c_arg_info
8890 entries.
8892 See gcc.dg/Wshadow-3.c for an example of this problem. */
8893 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8894 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8895 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8896 pushdecl (decl);
8897 warn_if_shadowing (decl);
8899 if (flag_isoc99)
8900 pedwarn (DECL_SOURCE_LOCATION (decl),
8901 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8902 decl);
8903 else
8904 warning_at (DECL_SOURCE_LOCATION (decl),
8905 OPT_Wmissing_parameter_type,
8906 "type of %qD defaults to %<int%>", decl);
8909 TREE_PURPOSE (parm) = decl;
8910 seen_args.add (decl);
8913 /* Now examine the parms chain for incomplete declarations
8914 and declarations with no corresponding names. */
8916 for (b = current_scope->bindings; b; b = b->prev)
8918 parm = b->decl;
8919 if (TREE_CODE (parm) != PARM_DECL)
8920 continue;
8922 if (TREE_TYPE (parm) != error_mark_node
8923 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8925 error_at (DECL_SOURCE_LOCATION (parm),
8926 "parameter %qD has incomplete type", parm);
8927 TREE_TYPE (parm) = error_mark_node;
8930 if (!seen_args.contains (parm))
8932 error_at (DECL_SOURCE_LOCATION (parm),
8933 "declaration for parameter %qD but no such parameter",
8934 parm);
8936 /* Pretend the parameter was not missing.
8937 This gets us to a standard state and minimizes
8938 further error messages. */
8939 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8943 /* Chain the declarations together in the order of the list of
8944 names. Store that chain in the function decl, replacing the
8945 list of names. Update the current scope to match. */
8946 DECL_ARGUMENTS (fndecl) = NULL_TREE;
8948 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8949 if (TREE_PURPOSE (parm))
8950 break;
8951 if (parm && TREE_PURPOSE (parm))
8953 last = TREE_PURPOSE (parm);
8954 DECL_ARGUMENTS (fndecl) = last;
8956 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8957 if (TREE_PURPOSE (parm))
8959 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8960 last = TREE_PURPOSE (parm);
8962 DECL_CHAIN (last) = NULL_TREE;
8965 /* If there was a previous prototype,
8966 set the DECL_ARG_TYPE of each argument according to
8967 the type previously specified, and report any mismatches. */
8969 if (current_function_prototype_arg_types)
8971 tree type;
8972 for (parm = DECL_ARGUMENTS (fndecl),
8973 type = current_function_prototype_arg_types;
8974 parm || (type != NULL_TREE
8975 && TREE_VALUE (type) != error_mark_node
8976 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
8977 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8979 if (parm == NULL_TREE
8980 || type == NULL_TREE
8981 || (TREE_VALUE (type) != error_mark_node
8982 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
8984 if (current_function_prototype_built_in)
8985 warning_at (DECL_SOURCE_LOCATION (fndecl),
8986 0, "number of arguments doesn%'t match "
8987 "built-in prototype");
8988 else
8990 /* FIXME diagnostics: This should be the location of
8991 FNDECL, but there is bug when a prototype is
8992 declared inside function context, but defined
8993 outside of it (e.g., gcc.dg/pr15698-2.c). In
8994 which case FNDECL gets the location of the
8995 prototype, not the definition. */
8996 error_at (input_location,
8997 "number of arguments doesn%'t match prototype");
8999 error_at (current_function_prototype_locus,
9000 "prototype declaration");
9002 break;
9004 /* Type for passing arg must be consistent with that
9005 declared for the arg. ISO C says we take the unqualified
9006 type for parameters declared with qualified type. */
9007 if (TREE_TYPE (parm) != error_mark_node
9008 && TREE_VALUE (type) != error_mark_node
9009 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9010 != TYPE_ATOMIC (TREE_VALUE (type)))
9011 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9012 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9014 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9015 == TYPE_ATOMIC (TREE_VALUE (type)))
9016 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9017 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9019 /* Adjust argument to match prototype. E.g. a previous
9020 `int foo(float);' prototype causes
9021 `int foo(x) float x; {...}' to be treated like
9022 `int foo(float x) {...}'. This is particularly
9023 useful for argument types like uid_t. */
9024 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9026 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9027 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9028 && (TYPE_PRECISION (TREE_TYPE (parm))
9029 < TYPE_PRECISION (integer_type_node)))
9030 DECL_ARG_TYPE (parm)
9031 = c_type_promotes_to (TREE_TYPE (parm));
9033 /* ??? Is it possible to get here with a
9034 built-in prototype or will it always have
9035 been diagnosed as conflicting with an
9036 old-style definition and discarded? */
9037 if (current_function_prototype_built_in)
9038 warning_at (DECL_SOURCE_LOCATION (parm),
9039 OPT_Wpedantic, "promoted argument %qD "
9040 "doesn%'t match built-in prototype", parm);
9041 else
9043 pedwarn (DECL_SOURCE_LOCATION (parm),
9044 OPT_Wpedantic, "promoted argument %qD "
9045 "doesn%'t match prototype", parm);
9046 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9047 "prototype declaration");
9050 else
9052 if (current_function_prototype_built_in)
9053 warning_at (DECL_SOURCE_LOCATION (parm),
9054 0, "argument %qD doesn%'t match "
9055 "built-in prototype", parm);
9056 else
9058 error_at (DECL_SOURCE_LOCATION (parm),
9059 "argument %qD doesn%'t match prototype", parm);
9060 error_at (current_function_prototype_locus,
9061 "prototype declaration");
9066 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9069 /* Otherwise, create a prototype that would match. */
9071 else
9073 tree actual = NULL_TREE, last = NULL_TREE, type;
9075 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9077 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9078 if (last)
9079 TREE_CHAIN (last) = type;
9080 else
9081 actual = type;
9082 last = type;
9084 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9085 if (last)
9086 TREE_CHAIN (last) = type;
9087 else
9088 actual = type;
9090 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9091 of the type of this function, but we need to avoid having this
9092 affect the types of other similarly-typed functions, so we must
9093 first force the generation of an identical (but separate) type
9094 node for the relevant function type. The new node we create
9095 will be a variant of the main variant of the original function
9096 type. */
9098 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9100 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9104 /* Store parameter declarations passed in ARG_INFO into the current
9105 function declaration. */
9107 void
9108 store_parm_decls_from (struct c_arg_info *arg_info)
9110 current_function_arg_info = arg_info;
9111 store_parm_decls ();
9114 /* Called by walk_tree to look for and update context-less labels. */
9116 static tree
9117 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9119 if (TREE_CODE (*tp) == LABEL_EXPR
9120 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9122 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9123 *walk_subtrees = 0;
9126 return NULL_TREE;
9129 /* Store the parameter declarations into the current function declaration.
9130 This is called after parsing the parameter declarations, before
9131 digesting the body of the function.
9133 For an old-style definition, construct a prototype out of the old-style
9134 parameter declarations and inject it into the function's type. */
9136 void
9137 store_parm_decls (void)
9139 tree fndecl = current_function_decl;
9140 bool proto;
9142 /* The argument information block for FNDECL. */
9143 struct c_arg_info *arg_info = current_function_arg_info;
9144 current_function_arg_info = 0;
9146 /* True if this definition is written with a prototype. Note:
9147 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9148 list in a function definition as equivalent to (void) -- an
9149 empty argument list specifies the function has no parameters,
9150 but only (void) sets up a prototype for future calls. */
9151 proto = arg_info->types != 0;
9153 if (proto)
9154 store_parm_decls_newstyle (fndecl, arg_info);
9155 else
9156 store_parm_decls_oldstyle (fndecl, arg_info);
9158 /* The next call to push_scope will be a function body. */
9160 next_is_function_body = true;
9162 /* Write a record describing this function definition to the prototypes
9163 file (if requested). */
9165 gen_aux_info_record (fndecl, 1, 0, proto);
9167 /* Initialize the RTL code for the function. */
9168 allocate_struct_function (fndecl, false);
9170 if (warn_unused_local_typedefs)
9171 cfun->language = ggc_cleared_alloc<language_function> ();
9173 /* Begin the statement tree for this function. */
9174 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9176 /* ??? Insert the contents of the pending sizes list into the function
9177 to be evaluated. The only reason left to have this is
9178 void foo(int n, int array[n++])
9179 because we throw away the array type in favor of a pointer type, and
9180 thus won't naturally see the SAVE_EXPR containing the increment. All
9181 other pending sizes would be handled by gimplify_parameters. */
9182 if (arg_info->pending_sizes)
9184 /* In very special circumstances, e.g. for code like
9185 _Atomic int i = 5;
9186 void f (int a[i += 2]) {}
9187 we need to execute the atomic assignment on function entry.
9188 But in this case, it is not just a straight store, it has the
9189 op= form, which means that build_atomic_assign has generated
9190 gotos, labels, etc. Because at that time the function decl
9191 for F has not been created yet, those labels do not have any
9192 function context. But we have the fndecl now, so update the
9193 labels accordingly. gimplify_expr would crash otherwise. */
9194 walk_tree_without_duplicates (&arg_info->pending_sizes,
9195 set_labels_context_r, fndecl);
9196 add_stmt (arg_info->pending_sizes);
9200 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9201 c_finish_omp_declare_simd for function prototypes. No diagnostics
9202 should be done. */
9204 void
9205 temp_store_parm_decls (tree fndecl, tree parms)
9207 push_scope ();
9208 for (tree p = parms; p; p = DECL_CHAIN (p))
9210 DECL_CONTEXT (p) = fndecl;
9211 if (DECL_NAME (p))
9212 bind (DECL_NAME (p), p, current_scope,
9213 /*invisible=*/false, /*nested=*/false,
9214 UNKNOWN_LOCATION);
9218 /* Undo what temp_store_parm_decls did. */
9220 void
9221 temp_pop_parm_decls (void)
9223 /* Clear all bindings in this temporary scope, so that
9224 pop_scope doesn't create a BLOCK. */
9225 struct c_binding *b = current_scope->bindings;
9226 current_scope->bindings = NULL;
9227 for (; b; b = free_binding_and_advance (b))
9229 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9230 || b->decl == error_mark_node);
9231 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9232 I_SYMBOL_BINDING (b->id) = b->shadowed;
9233 if (b->shadowed && b->shadowed->u.type)
9234 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9236 pop_scope ();
9240 /* Finish up a function declaration and compile that function
9241 all the way to assembler language output. Then free the storage
9242 for the function definition.
9244 This is called after parsing the body of the function definition. */
9246 void
9247 finish_function (void)
9249 tree fndecl = current_function_decl;
9251 if (c_dialect_objc ())
9252 objc_finish_function ();
9254 if (TREE_CODE (fndecl) == FUNCTION_DECL
9255 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9257 tree args = DECL_ARGUMENTS (fndecl);
9258 for (; args; args = DECL_CHAIN (args))
9260 tree type = TREE_TYPE (args);
9261 if (INTEGRAL_TYPE_P (type)
9262 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9263 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9267 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9268 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9270 /* Must mark the RESULT_DECL as being in this function. */
9272 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9273 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9275 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9276 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9277 == integer_type_node && flag_isoc99)
9279 /* Hack. We don't want the middle-end to warn that this return
9280 is unreachable, so we mark its location as special. Using
9281 UNKNOWN_LOCATION has the problem that it gets clobbered in
9282 annotate_one_with_locus. A cleaner solution might be to
9283 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9285 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9288 /* Tie off the statement tree for this function. */
9289 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9291 /* If the function has _Cilk_spawn in front of a function call inside it
9292 i.e. it is a spawning function, then add the appropriate Cilk plus
9293 functions inside. */
9294 if (fn_contains_cilk_spawn_p (cfun))
9295 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9297 finish_fname_decls ();
9299 /* Complain if there's just no return statement. */
9300 if (warn_return_type
9301 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9302 && !current_function_returns_value && !current_function_returns_null
9303 /* Don't complain if we are no-return. */
9304 && !current_function_returns_abnormally
9305 /* Don't complain if we are declared noreturn. */
9306 && !TREE_THIS_VOLATILE (fndecl)
9307 /* Don't warn for main(). */
9308 && !MAIN_NAME_P (DECL_NAME (fndecl))
9309 /* Or if they didn't actually specify a return type. */
9310 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9311 /* Normally, with -Wreturn-type, flow will complain, but we might
9312 optimize out static functions. */
9313 && !TREE_PUBLIC (fndecl))
9315 warning (OPT_Wreturn_type,
9316 "no return statement in function returning non-void");
9317 TREE_NO_WARNING (fndecl) = 1;
9320 /* Complain about parameters that are only set, but never otherwise used. */
9321 if (warn_unused_but_set_parameter)
9323 tree decl;
9325 for (decl = DECL_ARGUMENTS (fndecl);
9326 decl;
9327 decl = DECL_CHAIN (decl))
9328 if (TREE_USED (decl)
9329 && TREE_CODE (decl) == PARM_DECL
9330 && !DECL_READ_P (decl)
9331 && DECL_NAME (decl)
9332 && !DECL_ARTIFICIAL (decl)
9333 && !TREE_NO_WARNING (decl))
9334 warning_at (DECL_SOURCE_LOCATION (decl),
9335 OPT_Wunused_but_set_parameter,
9336 "parameter %qD set but not used", decl);
9339 /* Complain about locally defined typedefs that are not used in this
9340 function. */
9341 maybe_warn_unused_local_typedefs ();
9343 /* Possibly warn about unused parameters. */
9344 if (warn_unused_parameter)
9345 do_warn_unused_parameter (fndecl);
9347 /* Store the end of the function, so that we get good line number
9348 info for the epilogue. */
9349 cfun->function_end_locus = input_location;
9351 /* Finalize the ELF visibility for the function. */
9352 c_determine_visibility (fndecl);
9354 /* For GNU C extern inline functions disregard inline limits. */
9355 if (DECL_EXTERNAL (fndecl)
9356 && DECL_DECLARED_INLINE_P (fndecl)
9357 && (flag_gnu89_inline
9358 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9359 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9361 /* Genericize before inlining. Delay genericizing nested functions
9362 until their parent function is genericized. Since finalizing
9363 requires GENERIC, delay that as well. */
9365 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9366 && !undef_nested_function)
9368 if (!decl_function_context (fndecl))
9370 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9371 c_genericize (fndecl);
9373 /* ??? Objc emits functions after finalizing the compilation unit.
9374 This should be cleaned up later and this conditional removed. */
9375 if (symtab->global_info_ready)
9377 cgraph_node::add_new_function (fndecl, false);
9378 return;
9380 cgraph_node::finalize_function (fndecl, false);
9382 else
9384 /* Register this function with cgraph just far enough to get it
9385 added to our parent's nested function list. Handy, since the
9386 C front end doesn't have such a list. */
9387 (void) cgraph_node::get_create (fndecl);
9391 if (!decl_function_context (fndecl))
9392 undef_nested_function = false;
9394 if (cfun->language != NULL)
9396 ggc_free (cfun->language);
9397 cfun->language = NULL;
9400 /* We're leaving the context of this function, so zap cfun.
9401 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9402 tree_rest_of_compilation. */
9403 set_cfun (NULL);
9404 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9405 current_function_decl = NULL;
9408 /* Check the declarations given in a for-loop for satisfying the C99
9409 constraints. If exactly one such decl is found, return it. LOC is
9410 the location of the opening parenthesis of the for loop. The last
9411 parameter allows you to control the "for loop initial declarations
9412 are only allowed in C99 mode". Normally, you should pass
9413 flag_isoc99 as that parameter. But in some cases (Objective-C
9414 foreach loop, for example) we want to run the checks in this
9415 function even if not in C99 mode, so we allow the caller to turn
9416 off the error about not being in C99 mode.
9419 tree
9420 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9422 struct c_binding *b;
9423 tree one_decl = NULL_TREE;
9424 int n_decls = 0;
9426 if (!turn_off_iso_c99_error)
9428 static bool hint = true;
9429 /* If we get here, declarations have been used in a for loop without
9430 the C99 for loop scope. This doesn't make much sense, so don't
9431 allow it. */
9432 error_at (loc, "%<for%> loop initial declarations "
9433 "are only allowed in C99 or C11 mode");
9434 if (hint)
9436 inform (loc,
9437 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9438 "to compile your code");
9439 hint = false;
9441 return NULL_TREE;
9443 /* C99 subclause 6.8.5 paragraph 3:
9445 [#3] The declaration part of a for statement shall only
9446 declare identifiers for objects having storage class auto or
9447 register.
9449 It isn't clear whether, in this sentence, "identifiers" binds to
9450 "shall only declare" or to "objects" - that is, whether all identifiers
9451 declared must be identifiers for objects, or whether the restriction
9452 only applies to those that are. (A question on this in comp.std.c
9453 in November 2000 received no answer.) We implement the strictest
9454 interpretation, to avoid creating an extension which later causes
9455 problems. */
9457 for (b = current_scope->bindings; b; b = b->prev)
9459 tree id = b->id;
9460 tree decl = b->decl;
9462 if (!id)
9463 continue;
9465 switch (TREE_CODE (decl))
9467 case VAR_DECL:
9469 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9470 if (TREE_STATIC (decl))
9471 error_at (decl_loc,
9472 "declaration of static variable %qD in %<for%> loop "
9473 "initial declaration", decl);
9474 else if (DECL_EXTERNAL (decl))
9475 error_at (decl_loc,
9476 "declaration of %<extern%> variable %qD in %<for%> loop "
9477 "initial declaration", decl);
9479 break;
9481 case RECORD_TYPE:
9482 error_at (loc,
9483 "%<struct %E%> declared in %<for%> loop initial "
9484 "declaration", id);
9485 break;
9486 case UNION_TYPE:
9487 error_at (loc,
9488 "%<union %E%> declared in %<for%> loop initial declaration",
9489 id);
9490 break;
9491 case ENUMERAL_TYPE:
9492 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9493 "initial declaration", id);
9494 break;
9495 default:
9496 error_at (loc, "declaration of non-variable "
9497 "%qD in %<for%> loop initial declaration", decl);
9500 n_decls++;
9501 one_decl = decl;
9504 return n_decls == 1 ? one_decl : NULL_TREE;
9507 /* Save and reinitialize the variables
9508 used during compilation of a C function. */
9510 void
9511 c_push_function_context (void)
9513 struct language_function *p = cfun->language;
9514 /* cfun->language might have been already allocated by the use of
9515 -Wunused-local-typedefs. In that case, just re-use it. */
9516 if (p == NULL)
9517 cfun->language = p = ggc_cleared_alloc<language_function> ();
9519 p->base.x_stmt_tree = c_stmt_tree;
9520 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9521 p->x_break_label = c_break_label;
9522 p->x_cont_label = c_cont_label;
9523 p->x_switch_stack = c_switch_stack;
9524 p->arg_info = current_function_arg_info;
9525 p->returns_value = current_function_returns_value;
9526 p->returns_null = current_function_returns_null;
9527 p->returns_abnormally = current_function_returns_abnormally;
9528 p->warn_about_return_type = warn_about_return_type;
9530 push_function_context ();
9533 /* Restore the variables used during compilation of a C function. */
9535 void
9536 c_pop_function_context (void)
9538 struct language_function *p;
9540 pop_function_context ();
9541 p = cfun->language;
9543 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9544 used to store data throughout the life time of the current cfun,
9545 So don't deallocate it. */
9546 if (!warn_unused_local_typedefs)
9547 cfun->language = NULL;
9549 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9550 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9552 /* Stop pointing to the local nodes about to be freed. */
9553 /* But DECL_INITIAL must remain nonzero so we know this
9554 was an actual function definition. */
9555 DECL_INITIAL (current_function_decl) = error_mark_node;
9556 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9559 c_stmt_tree = p->base.x_stmt_tree;
9560 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9561 c_break_label = p->x_break_label;
9562 c_cont_label = p->x_cont_label;
9563 c_switch_stack = p->x_switch_stack;
9564 current_function_arg_info = p->arg_info;
9565 current_function_returns_value = p->returns_value;
9566 current_function_returns_null = p->returns_null;
9567 current_function_returns_abnormally = p->returns_abnormally;
9568 warn_about_return_type = p->warn_about_return_type;
9571 /* The functions below are required for functionality of doing
9572 function at once processing in the C front end. Currently these
9573 functions are not called from anywhere in the C front end, but as
9574 these changes continue, that will change. */
9576 /* Returns the stmt_tree (if any) to which statements are currently
9577 being added. If there is no active statement-tree, NULL is
9578 returned. */
9580 stmt_tree
9581 current_stmt_tree (void)
9583 return &c_stmt_tree;
9586 /* Return the global value of T as a symbol. */
9588 tree
9589 identifier_global_value (tree t)
9591 struct c_binding *b;
9593 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9594 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9595 return b->decl;
9597 return NULL_TREE;
9600 /* In C, the only C-linkage public declaration is at file scope. */
9602 tree
9603 c_linkage_bindings (tree name)
9605 return identifier_global_value (name);
9608 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9609 otherwise the name is found in ridpointers from RID_INDEX. */
9611 void
9612 record_builtin_type (enum rid rid_index, const char *name, tree type)
9614 tree id, decl;
9615 if (name == 0)
9616 id = ridpointers[(int) rid_index];
9617 else
9618 id = get_identifier (name);
9619 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9620 pushdecl (decl);
9621 if (debug_hooks->type_decl)
9622 debug_hooks->type_decl (decl, false);
9625 /* Build the void_list_node (void_type_node having been created). */
9626 tree
9627 build_void_list_node (void)
9629 tree t = build_tree_list (NULL_TREE, void_type_node);
9630 return t;
9633 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9635 struct c_parm *
9636 build_c_parm (struct c_declspecs *specs, tree attrs,
9637 struct c_declarator *declarator)
9639 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9640 ret->specs = specs;
9641 ret->attrs = attrs;
9642 ret->declarator = declarator;
9643 return ret;
9646 /* Return a declarator with nested attributes. TARGET is the inner
9647 declarator to which these attributes apply. ATTRS are the
9648 attributes. */
9650 struct c_declarator *
9651 build_attrs_declarator (tree attrs, struct c_declarator *target)
9653 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9654 ret->kind = cdk_attrs;
9655 ret->declarator = target;
9656 ret->u.attrs = attrs;
9657 return ret;
9660 /* Return a declarator for a function with arguments specified by ARGS
9661 and return type specified by TARGET. */
9663 struct c_declarator *
9664 build_function_declarator (struct c_arg_info *args,
9665 struct c_declarator *target)
9667 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9668 ret->kind = cdk_function;
9669 ret->declarator = target;
9670 ret->u.arg_info = args;
9671 return ret;
9674 /* Return a declarator for the identifier IDENT (which may be
9675 NULL_TREE for an abstract declarator). */
9677 struct c_declarator *
9678 build_id_declarator (tree ident)
9680 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9681 ret->kind = cdk_id;
9682 ret->declarator = 0;
9683 ret->u.id = ident;
9684 /* Default value - may get reset to a more precise location. */
9685 ret->id_loc = input_location;
9686 return ret;
9689 /* Return something to represent absolute declarators containing a *.
9690 TARGET is the absolute declarator that the * contains.
9691 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9692 to apply to the pointer type. */
9694 struct c_declarator *
9695 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9696 struct c_declarator *target)
9698 tree attrs;
9699 int quals = 0;
9700 struct c_declarator *itarget = target;
9701 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9702 if (type_quals_attrs)
9704 attrs = type_quals_attrs->attrs;
9705 quals = quals_from_declspecs (type_quals_attrs);
9706 if (attrs != NULL_TREE)
9707 itarget = build_attrs_declarator (attrs, target);
9709 ret->kind = cdk_pointer;
9710 ret->declarator = itarget;
9711 ret->u.pointer_quals = quals;
9712 return ret;
9715 /* Return a pointer to a structure for an empty list of declaration
9716 specifiers. */
9718 struct c_declspecs *
9719 build_null_declspecs (void)
9721 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9722 memset (ret, 0, sizeof *ret);
9723 ret->align_log = -1;
9724 ret->typespec_word = cts_none;
9725 ret->storage_class = csc_none;
9726 ret->expr_const_operands = true;
9727 ret->typespec_kind = ctsk_none;
9728 ret->address_space = ADDR_SPACE_GENERIC;
9729 return ret;
9732 /* Add the address space ADDRSPACE to the declaration specifiers
9733 SPECS, returning SPECS. */
9735 struct c_declspecs *
9736 declspecs_add_addrspace (source_location location,
9737 struct c_declspecs *specs, addr_space_t as)
9739 specs->non_sc_seen_p = true;
9740 specs->declspecs_seen_p = true;
9742 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9743 && specs->address_space != as)
9744 error ("incompatible address space qualifiers %qs and %qs",
9745 c_addr_space_name (as),
9746 c_addr_space_name (specs->address_space));
9747 else
9749 specs->address_space = as;
9750 specs->locations[cdw_address_space] = location;
9752 return specs;
9755 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9756 returning SPECS. */
9758 struct c_declspecs *
9759 declspecs_add_qual (source_location loc,
9760 struct c_declspecs *specs, tree qual)
9762 enum rid i;
9763 bool dupe = false;
9764 specs->non_sc_seen_p = true;
9765 specs->declspecs_seen_p = true;
9766 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9767 && C_IS_RESERVED_WORD (qual));
9768 i = C_RID_CODE (qual);
9769 location_t prev_loc = UNKNOWN_LOCATION;
9770 switch (i)
9772 case RID_CONST:
9773 dupe = specs->const_p;
9774 specs->const_p = true;
9775 prev_loc = specs->locations[cdw_const];
9776 specs->locations[cdw_const] = loc;
9777 break;
9778 case RID_VOLATILE:
9779 dupe = specs->volatile_p;
9780 specs->volatile_p = true;
9781 prev_loc = specs->locations[cdw_volatile];
9782 specs->locations[cdw_volatile] = loc;
9783 break;
9784 case RID_RESTRICT:
9785 dupe = specs->restrict_p;
9786 specs->restrict_p = true;
9787 prev_loc = specs->locations[cdw_restrict];
9788 specs->locations[cdw_restrict] = loc;
9789 break;
9790 case RID_ATOMIC:
9791 dupe = specs->atomic_p;
9792 specs->atomic_p = true;
9793 prev_loc = specs->locations[cdw_atomic];
9794 specs->locations[cdw_atomic] = loc;
9795 break;
9796 default:
9797 gcc_unreachable ();
9799 if (dupe)
9801 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9802 "duplicate %qE declaration specifier", qual);
9803 if (!warned
9804 && warn_duplicate_decl_specifier
9805 && prev_loc >= RESERVED_LOCATION_COUNT
9806 && !from_macro_expansion_at (prev_loc)
9807 && !from_macro_expansion_at (loc))
9808 warning_at (loc, OPT_Wduplicate_decl_specifier,
9809 "duplicate %qE declaration specifier", qual);
9811 return specs;
9814 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9815 returning SPECS. */
9817 struct c_declspecs *
9818 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9819 struct c_typespec spec)
9821 tree type = spec.spec;
9822 specs->non_sc_seen_p = true;
9823 specs->declspecs_seen_p = true;
9824 specs->typespec_kind = spec.kind;
9825 if (TREE_DEPRECATED (type))
9826 specs->deprecated_p = true;
9828 /* Handle type specifier keywords. */
9829 if (TREE_CODE (type) == IDENTIFIER_NODE
9830 && C_IS_RESERVED_WORD (type)
9831 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9833 enum rid i = C_RID_CODE (type);
9834 if (specs->type)
9836 error_at (loc, "two or more data types in declaration specifiers");
9837 return specs;
9839 if ((int) i <= (int) RID_LAST_MODIFIER)
9841 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9842 bool dupe = false;
9843 switch (i)
9845 case RID_LONG:
9846 if (specs->long_long_p)
9848 error_at (loc, "%<long long long%> is too long for GCC");
9849 break;
9851 if (specs->long_p)
9853 if (specs->typespec_word == cts_double)
9855 error_at (loc,
9856 ("both %<long long%> and %<double%> in "
9857 "declaration specifiers"));
9858 break;
9860 pedwarn_c90 (loc, OPT_Wlong_long,
9861 "ISO C90 does not support %<long long%>");
9862 specs->long_long_p = 1;
9863 specs->locations[cdw_long_long] = loc;
9864 break;
9866 if (specs->short_p)
9867 error_at (loc,
9868 ("both %<long%> and %<short%> in "
9869 "declaration specifiers"));
9870 else if (specs->typespec_word == cts_auto_type)
9871 error_at (loc,
9872 ("both %<long%> and %<__auto_type%> in "
9873 "declaration specifiers"));
9874 else if (specs->typespec_word == cts_void)
9875 error_at (loc,
9876 ("both %<long%> and %<void%> in "
9877 "declaration specifiers"));
9878 else if (specs->typespec_word == cts_int_n)
9879 error_at (loc,
9880 ("both %<long%> and %<__int%d%> in "
9881 "declaration specifiers"),
9882 int_n_data[specs->int_n_idx].bitsize);
9883 else if (specs->typespec_word == cts_bool)
9884 error_at (loc,
9885 ("both %<long%> and %<_Bool%> in "
9886 "declaration specifiers"));
9887 else if (specs->typespec_word == cts_char)
9888 error_at (loc,
9889 ("both %<long%> and %<char%> in "
9890 "declaration specifiers"));
9891 else if (specs->typespec_word == cts_float)
9892 error_at (loc,
9893 ("both %<long%> and %<float%> in "
9894 "declaration specifiers"));
9895 else if (specs->typespec_word == cts_floatn_nx)
9896 error_at (loc,
9897 ("both %<long%> and %<_Float%d%s%> in "
9898 "declaration specifiers"),
9899 floatn_nx_types[specs->floatn_nx_idx].n,
9900 (floatn_nx_types[specs->floatn_nx_idx].extended
9901 ? "x"
9902 : ""));
9903 else if (specs->typespec_word == cts_dfloat32)
9904 error_at (loc,
9905 ("both %<long%> and %<_Decimal32%> in "
9906 "declaration specifiers"));
9907 else if (specs->typespec_word == cts_dfloat64)
9908 error_at (loc,
9909 ("both %<long%> and %<_Decimal64%> in "
9910 "declaration specifiers"));
9911 else if (specs->typespec_word == cts_dfloat128)
9912 error_at (loc,
9913 ("both %<long%> and %<_Decimal128%> in "
9914 "declaration specifiers"));
9915 else
9917 specs->long_p = true;
9918 specs->locations[cdw_long] = loc;
9920 break;
9921 case RID_SHORT:
9922 dupe = specs->short_p;
9923 if (specs->long_p)
9924 error_at (loc,
9925 ("both %<long%> and %<short%> in "
9926 "declaration specifiers"));
9927 else if (specs->typespec_word == cts_auto_type)
9928 error_at (loc,
9929 ("both %<short%> and %<__auto_type%> in "
9930 "declaration specifiers"));
9931 else if (specs->typespec_word == cts_void)
9932 error_at (loc,
9933 ("both %<short%> and %<void%> in "
9934 "declaration specifiers"));
9935 else if (specs->typespec_word == cts_int_n)
9936 error_at (loc,
9937 ("both %<short%> and %<__int%d%> in "
9938 "declaration specifiers"),
9939 int_n_data[specs->int_n_idx].bitsize);
9940 else if (specs->typespec_word == cts_bool)
9941 error_at (loc,
9942 ("both %<short%> and %<_Bool%> in "
9943 "declaration specifiers"));
9944 else if (specs->typespec_word == cts_char)
9945 error_at (loc,
9946 ("both %<short%> and %<char%> in "
9947 "declaration specifiers"));
9948 else if (specs->typespec_word == cts_float)
9949 error_at (loc,
9950 ("both %<short%> and %<float%> in "
9951 "declaration specifiers"));
9952 else if (specs->typespec_word == cts_double)
9953 error_at (loc,
9954 ("both %<short%> and %<double%> in "
9955 "declaration specifiers"));
9956 else if (specs->typespec_word == cts_floatn_nx)
9957 error_at (loc,
9958 ("both %<short%> and %<_Float%d%s%> in "
9959 "declaration specifiers"),
9960 floatn_nx_types[specs->floatn_nx_idx].n,
9961 (floatn_nx_types[specs->floatn_nx_idx].extended
9962 ? "x"
9963 : ""));
9964 else if (specs->typespec_word == cts_dfloat32)
9965 error_at (loc,
9966 ("both %<short%> and %<_Decimal32%> in "
9967 "declaration specifiers"));
9968 else if (specs->typespec_word == cts_dfloat64)
9969 error_at (loc,
9970 ("both %<short%> and %<_Decimal64%> in "
9971 "declaration specifiers"));
9972 else if (specs->typespec_word == cts_dfloat128)
9973 error_at (loc,
9974 ("both %<short%> and %<_Decimal128%> in "
9975 "declaration specifiers"));
9976 else
9978 specs->short_p = true;
9979 specs->locations[cdw_short] = loc;
9981 break;
9982 case RID_SIGNED:
9983 dupe = specs->signed_p;
9984 if (specs->unsigned_p)
9985 error_at (loc,
9986 ("both %<signed%> and %<unsigned%> in "
9987 "declaration specifiers"));
9988 else if (specs->typespec_word == cts_auto_type)
9989 error_at (loc,
9990 ("both %<signed%> and %<__auto_type%> in "
9991 "declaration specifiers"));
9992 else if (specs->typespec_word == cts_void)
9993 error_at (loc,
9994 ("both %<signed%> and %<void%> in "
9995 "declaration specifiers"));
9996 else if (specs->typespec_word == cts_bool)
9997 error_at (loc,
9998 ("both %<signed%> and %<_Bool%> in "
9999 "declaration specifiers"));
10000 else if (specs->typespec_word == cts_float)
10001 error_at (loc,
10002 ("both %<signed%> and %<float%> in "
10003 "declaration specifiers"));
10004 else if (specs->typespec_word == cts_double)
10005 error_at (loc,
10006 ("both %<signed%> and %<double%> in "
10007 "declaration specifiers"));
10008 else if (specs->typespec_word == cts_floatn_nx)
10009 error_at (loc,
10010 ("both %<signed%> and %<_Float%d%s%> in "
10011 "declaration specifiers"),
10012 floatn_nx_types[specs->floatn_nx_idx].n,
10013 (floatn_nx_types[specs->floatn_nx_idx].extended
10014 ? "x"
10015 : ""));
10016 else if (specs->typespec_word == cts_dfloat32)
10017 error_at (loc,
10018 ("both %<signed%> and %<_Decimal32%> in "
10019 "declaration specifiers"));
10020 else if (specs->typespec_word == cts_dfloat64)
10021 error_at (loc,
10022 ("both %<signed%> and %<_Decimal64%> in "
10023 "declaration specifiers"));
10024 else if (specs->typespec_word == cts_dfloat128)
10025 error_at (loc,
10026 ("both %<signed%> and %<_Decimal128%> in "
10027 "declaration specifiers"));
10028 else
10030 specs->signed_p = true;
10031 specs->locations[cdw_signed] = loc;
10033 break;
10034 case RID_UNSIGNED:
10035 dupe = specs->unsigned_p;
10036 if (specs->signed_p)
10037 error_at (loc,
10038 ("both %<signed%> and %<unsigned%> in "
10039 "declaration specifiers"));
10040 else if (specs->typespec_word == cts_auto_type)
10041 error_at (loc,
10042 ("both %<unsigned%> and %<__auto_type%> in "
10043 "declaration specifiers"));
10044 else if (specs->typespec_word == cts_void)
10045 error_at (loc,
10046 ("both %<unsigned%> and %<void%> in "
10047 "declaration specifiers"));
10048 else if (specs->typespec_word == cts_bool)
10049 error_at (loc,
10050 ("both %<unsigned%> and %<_Bool%> in "
10051 "declaration specifiers"));
10052 else if (specs->typespec_word == cts_float)
10053 error_at (loc,
10054 ("both %<unsigned%> and %<float%> in "
10055 "declaration specifiers"));
10056 else if (specs->typespec_word == cts_double)
10057 error_at (loc,
10058 ("both %<unsigned%> and %<double%> in "
10059 "declaration specifiers"));
10060 else if (specs->typespec_word == cts_floatn_nx)
10061 error_at (loc,
10062 ("both %<unsigned%> and %<_Float%d%s%> in "
10063 "declaration specifiers"),
10064 floatn_nx_types[specs->floatn_nx_idx].n,
10065 (floatn_nx_types[specs->floatn_nx_idx].extended
10066 ? "x"
10067 : ""));
10068 else if (specs->typespec_word == cts_dfloat32)
10069 error_at (loc,
10070 ("both %<unsigned%> and %<_Decimal32%> in "
10071 "declaration specifiers"));
10072 else if (specs->typespec_word == cts_dfloat64)
10073 error_at (loc,
10074 ("both %<unsigned%> and %<_Decimal64%> in "
10075 "declaration specifiers"));
10076 else if (specs->typespec_word == cts_dfloat128)
10077 error_at (loc,
10078 ("both %<unsigned%> and %<_Decimal128%> in "
10079 "declaration specifiers"));
10080 else
10082 specs->unsigned_p = true;
10083 specs->locations[cdw_unsigned] = loc;
10085 break;
10086 case RID_COMPLEX:
10087 dupe = specs->complex_p;
10088 if (!in_system_header_at (loc))
10089 pedwarn_c90 (loc, OPT_Wpedantic,
10090 "ISO C90 does not support complex types");
10091 if (specs->typespec_word == cts_auto_type)
10092 error_at (loc,
10093 ("both %<complex%> and %<__auto_type%> in "
10094 "declaration specifiers"));
10095 else if (specs->typespec_word == cts_void)
10096 error_at (loc,
10097 ("both %<complex%> and %<void%> in "
10098 "declaration specifiers"));
10099 else if (specs->typespec_word == cts_bool)
10100 error_at (loc,
10101 ("both %<complex%> and %<_Bool%> in "
10102 "declaration specifiers"));
10103 else if (specs->typespec_word == cts_dfloat32)
10104 error_at (loc,
10105 ("both %<complex%> and %<_Decimal32%> in "
10106 "declaration specifiers"));
10107 else if (specs->typespec_word == cts_dfloat64)
10108 error_at (loc,
10109 ("both %<complex%> and %<_Decimal64%> in "
10110 "declaration specifiers"));
10111 else if (specs->typespec_word == cts_dfloat128)
10112 error_at (loc,
10113 ("both %<complex%> and %<_Decimal128%> in "
10114 "declaration specifiers"));
10115 else if (specs->typespec_word == cts_fract)
10116 error_at (loc,
10117 ("both %<complex%> and %<_Fract%> in "
10118 "declaration specifiers"));
10119 else if (specs->typespec_word == cts_accum)
10120 error_at (loc,
10121 ("both %<complex%> and %<_Accum%> in "
10122 "declaration specifiers"));
10123 else if (specs->saturating_p)
10124 error_at (loc,
10125 ("both %<complex%> and %<_Sat%> in "
10126 "declaration specifiers"));
10127 else
10129 specs->complex_p = true;
10130 specs->locations[cdw_complex] = loc;
10132 break;
10133 case RID_SAT:
10134 dupe = specs->saturating_p;
10135 pedwarn (loc, OPT_Wpedantic,
10136 "ISO C does not support saturating types");
10137 if (specs->typespec_word == cts_int_n)
10139 error_at (loc,
10140 ("both %<_Sat%> and %<__int%d%> in "
10141 "declaration specifiers"),
10142 int_n_data[specs->int_n_idx].bitsize);
10144 else if (specs->typespec_word == cts_auto_type)
10145 error_at (loc,
10146 ("both %<_Sat%> and %<__auto_type%> in "
10147 "declaration specifiers"));
10148 else if (specs->typespec_word == cts_void)
10149 error_at (loc,
10150 ("both %<_Sat%> and %<void%> in "
10151 "declaration specifiers"));
10152 else if (specs->typespec_word == cts_bool)
10153 error_at (loc,
10154 ("both %<_Sat%> and %<_Bool%> in "
10155 "declaration specifiers"));
10156 else if (specs->typespec_word == cts_char)
10157 error_at (loc,
10158 ("both %<_Sat%> and %<char%> in "
10159 "declaration specifiers"));
10160 else if (specs->typespec_word == cts_int)
10161 error_at (loc,
10162 ("both %<_Sat%> and %<int%> in "
10163 "declaration specifiers"));
10164 else if (specs->typespec_word == cts_float)
10165 error_at (loc,
10166 ("both %<_Sat%> and %<float%> in "
10167 "declaration specifiers"));
10168 else if (specs->typespec_word == cts_double)
10169 error_at (loc,
10170 ("both %<_Sat%> and %<double%> in "
10171 "declaration specifiers"));
10172 else if (specs->typespec_word == cts_floatn_nx)
10173 error_at (loc,
10174 ("both %<_Sat%> and %<_Float%d%s%> in "
10175 "declaration specifiers"),
10176 floatn_nx_types[specs->floatn_nx_idx].n,
10177 (floatn_nx_types[specs->floatn_nx_idx].extended
10178 ? "x"
10179 : ""));
10180 else if (specs->typespec_word == cts_dfloat32)
10181 error_at (loc,
10182 ("both %<_Sat%> and %<_Decimal32%> in "
10183 "declaration specifiers"));
10184 else if (specs->typespec_word == cts_dfloat64)
10185 error_at (loc,
10186 ("both %<_Sat%> and %<_Decimal64%> in "
10187 "declaration specifiers"));
10188 else if (specs->typespec_word == cts_dfloat128)
10189 error_at (loc,
10190 ("both %<_Sat%> and %<_Decimal128%> in "
10191 "declaration specifiers"));
10192 else if (specs->complex_p)
10193 error_at (loc,
10194 ("both %<_Sat%> and %<complex%> in "
10195 "declaration specifiers"));
10196 else
10198 specs->saturating_p = true;
10199 specs->locations[cdw_saturating] = loc;
10201 break;
10202 default:
10203 gcc_unreachable ();
10206 if (dupe)
10207 error_at (loc, "duplicate %qE", type);
10209 return specs;
10211 else
10213 /* "void", "_Bool", "char", "int", "float", "double",
10214 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10215 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10216 "__auto_type". */
10217 if (specs->typespec_word != cts_none)
10219 error_at (loc,
10220 "two or more data types in declaration specifiers");
10221 return specs;
10223 switch (i)
10225 case RID_AUTO_TYPE:
10226 if (specs->long_p)
10227 error_at (loc,
10228 ("both %<long%> and %<__auto_type%> in "
10229 "declaration specifiers"));
10230 else if (specs->short_p)
10231 error_at (loc,
10232 ("both %<short%> and %<__auto_type%> in "
10233 "declaration specifiers"));
10234 else if (specs->signed_p)
10235 error_at (loc,
10236 ("both %<signed%> and %<__auto_type%> in "
10237 "declaration specifiers"));
10238 else if (specs->unsigned_p)
10239 error_at (loc,
10240 ("both %<unsigned%> and %<__auto_type%> in "
10241 "declaration specifiers"));
10242 else if (specs->complex_p)
10243 error_at (loc,
10244 ("both %<complex%> and %<__auto_type%> in "
10245 "declaration specifiers"));
10246 else if (specs->saturating_p)
10247 error_at (loc,
10248 ("both %<_Sat%> and %<__auto_type%> in "
10249 "declaration specifiers"));
10250 else
10252 specs->typespec_word = cts_auto_type;
10253 specs->locations[cdw_typespec] = loc;
10255 return specs;
10256 case RID_INT_N_0:
10257 case RID_INT_N_1:
10258 case RID_INT_N_2:
10259 case RID_INT_N_3:
10260 specs->int_n_idx = i - RID_INT_N_0;
10261 if (!in_system_header_at (input_location))
10262 pedwarn (loc, OPT_Wpedantic,
10263 "ISO C does not support %<__int%d%> types",
10264 int_n_data[specs->int_n_idx].bitsize);
10266 if (specs->long_p)
10267 error_at (loc,
10268 ("both %<__int%d%> and %<long%> in "
10269 "declaration specifiers"),
10270 int_n_data[specs->int_n_idx].bitsize);
10271 else if (specs->saturating_p)
10272 error_at (loc,
10273 ("both %<_Sat%> and %<__int%d%> in "
10274 "declaration specifiers"),
10275 int_n_data[specs->int_n_idx].bitsize);
10276 else if (specs->short_p)
10277 error_at (loc,
10278 ("both %<__int%d%> and %<short%> in "
10279 "declaration specifiers"),
10280 int_n_data[specs->int_n_idx].bitsize);
10281 else if (! int_n_enabled_p[specs->int_n_idx])
10283 specs->typespec_word = cts_int_n;
10284 error_at (loc,
10285 "%<__int%d%> is not supported on this target",
10286 int_n_data[specs->int_n_idx].bitsize);
10288 else
10290 specs->typespec_word = cts_int_n;
10291 specs->locations[cdw_typespec] = loc;
10293 return specs;
10294 case RID_VOID:
10295 if (specs->long_p)
10296 error_at (loc,
10297 ("both %<long%> and %<void%> in "
10298 "declaration specifiers"));
10299 else if (specs->short_p)
10300 error_at (loc,
10301 ("both %<short%> and %<void%> in "
10302 "declaration specifiers"));
10303 else if (specs->signed_p)
10304 error_at (loc,
10305 ("both %<signed%> and %<void%> in "
10306 "declaration specifiers"));
10307 else if (specs->unsigned_p)
10308 error_at (loc,
10309 ("both %<unsigned%> and %<void%> in "
10310 "declaration specifiers"));
10311 else if (specs->complex_p)
10312 error_at (loc,
10313 ("both %<complex%> and %<void%> in "
10314 "declaration specifiers"));
10315 else if (specs->saturating_p)
10316 error_at (loc,
10317 ("both %<_Sat%> and %<void%> in "
10318 "declaration specifiers"));
10319 else
10321 specs->typespec_word = cts_void;
10322 specs->locations[cdw_typespec] = loc;
10324 return specs;
10325 case RID_BOOL:
10326 if (!in_system_header_at (loc))
10327 pedwarn_c90 (loc, OPT_Wpedantic,
10328 "ISO C90 does not support boolean types");
10329 if (specs->long_p)
10330 error_at (loc,
10331 ("both %<long%> and %<_Bool%> in "
10332 "declaration specifiers"));
10333 else if (specs->short_p)
10334 error_at (loc,
10335 ("both %<short%> and %<_Bool%> in "
10336 "declaration specifiers"));
10337 else if (specs->signed_p)
10338 error_at (loc,
10339 ("both %<signed%> and %<_Bool%> in "
10340 "declaration specifiers"));
10341 else if (specs->unsigned_p)
10342 error_at (loc,
10343 ("both %<unsigned%> and %<_Bool%> in "
10344 "declaration specifiers"));
10345 else if (specs->complex_p)
10346 error_at (loc,
10347 ("both %<complex%> and %<_Bool%> in "
10348 "declaration specifiers"));
10349 else if (specs->saturating_p)
10350 error_at (loc,
10351 ("both %<_Sat%> and %<_Bool%> in "
10352 "declaration specifiers"));
10353 else
10355 specs->typespec_word = cts_bool;
10356 specs->locations[cdw_typespec] = loc;
10358 return specs;
10359 case RID_CHAR:
10360 if (specs->long_p)
10361 error_at (loc,
10362 ("both %<long%> and %<char%> in "
10363 "declaration specifiers"));
10364 else if (specs->short_p)
10365 error_at (loc,
10366 ("both %<short%> and %<char%> in "
10367 "declaration specifiers"));
10368 else if (specs->saturating_p)
10369 error_at (loc,
10370 ("both %<_Sat%> and %<char%> in "
10371 "declaration specifiers"));
10372 else
10374 specs->typespec_word = cts_char;
10375 specs->locations[cdw_typespec] = loc;
10377 return specs;
10378 case RID_INT:
10379 if (specs->saturating_p)
10380 error_at (loc,
10381 ("both %<_Sat%> and %<int%> in "
10382 "declaration specifiers"));
10383 else
10385 specs->typespec_word = cts_int;
10386 specs->locations[cdw_typespec] = loc;
10388 return specs;
10389 case RID_FLOAT:
10390 if (specs->long_p)
10391 error_at (loc,
10392 ("both %<long%> and %<float%> in "
10393 "declaration specifiers"));
10394 else if (specs->short_p)
10395 error_at (loc,
10396 ("both %<short%> and %<float%> in "
10397 "declaration specifiers"));
10398 else if (specs->signed_p)
10399 error_at (loc,
10400 ("both %<signed%> and %<float%> in "
10401 "declaration specifiers"));
10402 else if (specs->unsigned_p)
10403 error_at (loc,
10404 ("both %<unsigned%> and %<float%> in "
10405 "declaration specifiers"));
10406 else if (specs->saturating_p)
10407 error_at (loc,
10408 ("both %<_Sat%> and %<float%> in "
10409 "declaration specifiers"));
10410 else
10412 specs->typespec_word = cts_float;
10413 specs->locations[cdw_typespec] = loc;
10415 return specs;
10416 case RID_DOUBLE:
10417 if (specs->long_long_p)
10418 error_at (loc,
10419 ("both %<long long%> and %<double%> in "
10420 "declaration specifiers"));
10421 else if (specs->short_p)
10422 error_at (loc,
10423 ("both %<short%> and %<double%> in "
10424 "declaration specifiers"));
10425 else if (specs->signed_p)
10426 error_at (loc,
10427 ("both %<signed%> and %<double%> in "
10428 "declaration specifiers"));
10429 else if (specs->unsigned_p)
10430 error_at (loc,
10431 ("both %<unsigned%> and %<double%> in "
10432 "declaration specifiers"));
10433 else if (specs->saturating_p)
10434 error_at (loc,
10435 ("both %<_Sat%> and %<double%> in "
10436 "declaration specifiers"));
10437 else
10439 specs->typespec_word = cts_double;
10440 specs->locations[cdw_typespec] = loc;
10442 return specs;
10443 CASE_RID_FLOATN_NX:
10444 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10445 if (!in_system_header_at (input_location))
10446 pedwarn (loc, OPT_Wpedantic,
10447 "ISO C does not support the %<_Float%d%s%> type",
10448 floatn_nx_types[specs->floatn_nx_idx].n,
10449 (floatn_nx_types[specs->floatn_nx_idx].extended
10450 ? "x"
10451 : ""));
10453 if (specs->long_p)
10454 error_at (loc,
10455 ("both %<long%> and %<_Float%d%s%> in "
10456 "declaration specifiers"),
10457 floatn_nx_types[specs->floatn_nx_idx].n,
10458 (floatn_nx_types[specs->floatn_nx_idx].extended
10459 ? "x"
10460 : ""));
10461 else if (specs->short_p)
10462 error_at (loc,
10463 ("both %<short%> and %<_Float%d%s%> in "
10464 "declaration specifiers"),
10465 floatn_nx_types[specs->floatn_nx_idx].n,
10466 (floatn_nx_types[specs->floatn_nx_idx].extended
10467 ? "x"
10468 : ""));
10469 else if (specs->signed_p)
10470 error_at (loc,
10471 ("both %<signed%> and %<_Float%d%s%> in "
10472 "declaration specifiers"),
10473 floatn_nx_types[specs->floatn_nx_idx].n,
10474 (floatn_nx_types[specs->floatn_nx_idx].extended
10475 ? "x"
10476 : ""));
10477 else if (specs->unsigned_p)
10478 error_at (loc,
10479 ("both %<unsigned%> and %<_Float%d%s%> in "
10480 "declaration specifiers"),
10481 floatn_nx_types[specs->floatn_nx_idx].n,
10482 (floatn_nx_types[specs->floatn_nx_idx].extended
10483 ? "x"
10484 : ""));
10485 else if (specs->saturating_p)
10486 error_at (loc,
10487 ("both %<_Sat%> and %<_Float%d%s%> in "
10488 "declaration specifiers"),
10489 floatn_nx_types[specs->floatn_nx_idx].n,
10490 (floatn_nx_types[specs->floatn_nx_idx].extended
10491 ? "x"
10492 : ""));
10493 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10495 specs->typespec_word = cts_floatn_nx;
10496 error_at (loc,
10497 "%<_Float%d%s%> is not supported on this target",
10498 floatn_nx_types[specs->floatn_nx_idx].n,
10499 (floatn_nx_types[specs->floatn_nx_idx].extended
10500 ? "x"
10501 : ""));
10503 else
10505 specs->typespec_word = cts_floatn_nx;
10506 specs->locations[cdw_typespec] = loc;
10508 return specs;
10509 case RID_DFLOAT32:
10510 case RID_DFLOAT64:
10511 case RID_DFLOAT128:
10513 const char *str;
10514 if (i == RID_DFLOAT32)
10515 str = "_Decimal32";
10516 else if (i == RID_DFLOAT64)
10517 str = "_Decimal64";
10518 else
10519 str = "_Decimal128";
10520 if (specs->long_long_p)
10521 error_at (loc,
10522 ("both %<long long%> and %qs in "
10523 "declaration specifiers"),
10524 str);
10525 if (specs->long_p)
10526 error_at (loc,
10527 ("both %<long%> and %qs in "
10528 "declaration specifiers"),
10529 str);
10530 else if (specs->short_p)
10531 error_at (loc,
10532 ("both %<short%> and %qs in "
10533 "declaration specifiers"),
10534 str);
10535 else if (specs->signed_p)
10536 error_at (loc,
10537 ("both %<signed%> and %qs in "
10538 "declaration specifiers"),
10539 str);
10540 else if (specs->unsigned_p)
10541 error_at (loc,
10542 ("both %<unsigned%> and %qs in "
10543 "declaration specifiers"),
10544 str);
10545 else if (specs->complex_p)
10546 error_at (loc,
10547 ("both %<complex%> and %qs in "
10548 "declaration specifiers"),
10549 str);
10550 else if (specs->saturating_p)
10551 error_at (loc,
10552 ("both %<_Sat%> and %qs in "
10553 "declaration specifiers"),
10554 str);
10555 else if (i == RID_DFLOAT32)
10556 specs->typespec_word = cts_dfloat32;
10557 else if (i == RID_DFLOAT64)
10558 specs->typespec_word = cts_dfloat64;
10559 else
10560 specs->typespec_word = cts_dfloat128;
10561 specs->locations[cdw_typespec] = loc;
10563 if (!targetm.decimal_float_supported_p ())
10564 error_at (loc,
10565 ("decimal floating point not supported "
10566 "for this target"));
10567 pedwarn (loc, OPT_Wpedantic,
10568 "ISO C does not support decimal floating point");
10569 return specs;
10570 case RID_FRACT:
10571 case RID_ACCUM:
10573 const char *str;
10574 if (i == RID_FRACT)
10575 str = "_Fract";
10576 else
10577 str = "_Accum";
10578 if (specs->complex_p)
10579 error_at (loc,
10580 ("both %<complex%> and %qs in "
10581 "declaration specifiers"),
10582 str);
10583 else if (i == RID_FRACT)
10584 specs->typespec_word = cts_fract;
10585 else
10586 specs->typespec_word = cts_accum;
10587 specs->locations[cdw_typespec] = loc;
10589 if (!targetm.fixed_point_supported_p ())
10590 error_at (loc,
10591 "fixed-point types not supported for this target");
10592 pedwarn (loc, OPT_Wpedantic,
10593 "ISO C does not support fixed-point types");
10594 return specs;
10595 default:
10596 /* ObjC reserved word "id", handled below. */
10597 break;
10602 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10603 form of ObjC type, cases such as "int" and "long" being handled
10604 above), a TYPE (struct, union, enum and typeof specifiers) or an
10605 ERROR_MARK. In none of these cases may there have previously
10606 been any type specifiers. */
10607 if (specs->type || specs->typespec_word != cts_none
10608 || specs->long_p || specs->short_p || specs->signed_p
10609 || specs->unsigned_p || specs->complex_p)
10610 error_at (loc, "two or more data types in declaration specifiers");
10611 else if (TREE_CODE (type) == TYPE_DECL)
10613 if (TREE_TYPE (type) == error_mark_node)
10614 ; /* Allow the type to default to int to avoid cascading errors. */
10615 else
10617 specs->type = TREE_TYPE (type);
10618 specs->decl_attr = DECL_ATTRIBUTES (type);
10619 specs->typedef_p = true;
10620 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10621 specs->locations[cdw_typedef] = loc;
10623 /* If this typedef name is defined in a struct, then a C++
10624 lookup would return a different value. */
10625 if (warn_cxx_compat
10626 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10627 warning_at (loc, OPT_Wc___compat,
10628 "C++ lookup of %qD would return a field, not a type",
10629 type);
10631 /* If we are parsing a struct, record that a struct field
10632 used a typedef. */
10633 if (warn_cxx_compat && struct_parse_info != NULL)
10634 struct_parse_info->typedefs_seen.safe_push (type);
10637 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10639 tree t = lookup_name (type);
10640 if (!t || TREE_CODE (t) != TYPE_DECL)
10641 error_at (loc, "%qE fails to be a typedef or built in type", type);
10642 else if (TREE_TYPE (t) == error_mark_node)
10644 else
10646 specs->type = TREE_TYPE (t);
10647 specs->locations[cdw_typespec] = loc;
10650 else
10652 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10654 specs->typedef_p = true;
10655 specs->locations[cdw_typedef] = loc;
10656 if (spec.expr)
10658 if (specs->expr)
10659 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10660 specs->expr, spec.expr);
10661 else
10662 specs->expr = spec.expr;
10663 specs->expr_const_operands &= spec.expr_const_operands;
10666 specs->type = type;
10669 return specs;
10672 /* Add the storage class specifier or function specifier SCSPEC to the
10673 declaration specifiers SPECS, returning SPECS. */
10675 struct c_declspecs *
10676 declspecs_add_scspec (source_location loc,
10677 struct c_declspecs *specs,
10678 tree scspec)
10680 enum rid i;
10681 enum c_storage_class n = csc_none;
10682 bool dupe = false;
10683 specs->declspecs_seen_p = true;
10684 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10685 && C_IS_RESERVED_WORD (scspec));
10686 i = C_RID_CODE (scspec);
10687 if (specs->non_sc_seen_p)
10688 warning (OPT_Wold_style_declaration,
10689 "%qE is not at beginning of declaration", scspec);
10690 switch (i)
10692 case RID_INLINE:
10693 /* C99 permits duplicate inline. Although of doubtful utility,
10694 it seems simplest to permit it in gnu89 mode as well, as
10695 there is also little utility in maintaining this as a
10696 difference between gnu89 and C99 inline. */
10697 dupe = false;
10698 specs->inline_p = true;
10699 specs->locations[cdw_inline] = loc;
10700 break;
10701 case RID_NORETURN:
10702 /* Duplicate _Noreturn is permitted. */
10703 dupe = false;
10704 specs->noreturn_p = true;
10705 specs->locations[cdw_noreturn] = loc;
10706 break;
10707 case RID_THREAD:
10708 dupe = specs->thread_p;
10709 if (specs->storage_class == csc_auto)
10710 error ("%qE used with %<auto%>", scspec);
10711 else if (specs->storage_class == csc_register)
10712 error ("%qE used with %<register%>", scspec);
10713 else if (specs->storage_class == csc_typedef)
10714 error ("%qE used with %<typedef%>", scspec);
10715 else
10717 specs->thread_p = true;
10718 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10719 "__thread") == 0);
10720 /* A diagnostic is not required for the use of this
10721 identifier in the implementation namespace; only diagnose
10722 it for the C11 spelling because of existing code using
10723 the other spelling. */
10724 if (!specs->thread_gnu_p)
10726 if (flag_isoc99)
10727 pedwarn_c99 (loc, OPT_Wpedantic,
10728 "ISO C99 does not support %qE", scspec);
10729 else
10730 pedwarn_c99 (loc, OPT_Wpedantic,
10731 "ISO C90 does not support %qE", scspec);
10733 specs->locations[cdw_thread] = loc;
10735 break;
10736 case RID_AUTO:
10737 n = csc_auto;
10738 break;
10739 case RID_EXTERN:
10740 n = csc_extern;
10741 /* Diagnose "__thread extern". */
10742 if (specs->thread_p && specs->thread_gnu_p)
10743 error ("%<__thread%> before %<extern%>");
10744 break;
10745 case RID_REGISTER:
10746 n = csc_register;
10747 break;
10748 case RID_STATIC:
10749 n = csc_static;
10750 /* Diagnose "__thread static". */
10751 if (specs->thread_p && specs->thread_gnu_p)
10752 error ("%<__thread%> before %<static%>");
10753 break;
10754 case RID_TYPEDEF:
10755 n = csc_typedef;
10756 break;
10757 default:
10758 gcc_unreachable ();
10760 if (n != csc_none && n == specs->storage_class)
10761 dupe = true;
10762 if (dupe)
10764 if (i == RID_THREAD)
10765 error ("duplicate %<_Thread_local%> or %<__thread%>");
10766 else
10767 error ("duplicate %qE", scspec);
10769 if (n != csc_none)
10771 if (specs->storage_class != csc_none && n != specs->storage_class)
10773 error ("multiple storage classes in declaration specifiers");
10775 else
10777 specs->storage_class = n;
10778 specs->locations[cdw_storage_class] = loc;
10779 if (n != csc_extern && n != csc_static && specs->thread_p)
10781 error ("%qs used with %qE",
10782 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10783 scspec);
10784 specs->thread_p = false;
10788 return specs;
10791 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10792 returning SPECS. */
10794 struct c_declspecs *
10795 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10797 specs->attrs = chainon (attrs, specs->attrs);
10798 specs->locations[cdw_attributes] = loc;
10799 specs->declspecs_seen_p = true;
10800 return specs;
10803 /* Add an _Alignas specifier (expression ALIGN, or type whose
10804 alignment is ALIGN) to the declaration specifiers SPECS, returning
10805 SPECS. */
10806 struct c_declspecs *
10807 declspecs_add_alignas (source_location loc,
10808 struct c_declspecs *specs, tree align)
10810 int align_log;
10811 specs->alignas_p = true;
10812 specs->locations[cdw_alignas] = loc;
10813 if (align == error_mark_node)
10814 return specs;
10815 align_log = check_user_alignment (align, true);
10816 if (align_log > specs->align_log)
10817 specs->align_log = align_log;
10818 return specs;
10821 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10822 specifiers with any other type specifier to determine the resulting
10823 type. This is where ISO C checks on complex types are made, since
10824 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10825 double". */
10827 struct c_declspecs *
10828 finish_declspecs (struct c_declspecs *specs)
10830 /* If a type was specified as a whole, we have no modifiers and are
10831 done. */
10832 if (specs->type != NULL_TREE)
10834 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10835 && !specs->signed_p && !specs->unsigned_p
10836 && !specs->complex_p);
10838 /* Set a dummy type. */
10839 if (TREE_CODE (specs->type) == ERROR_MARK)
10840 specs->type = integer_type_node;
10841 return specs;
10844 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10845 has been specified, treat it as "int" unless "_Complex" is
10846 present and there are no other specifiers. If we just have
10847 "_Complex", it is equivalent to "_Complex double", but e.g.
10848 "_Complex short" is equivalent to "_Complex short int". */
10849 if (specs->typespec_word == cts_none)
10851 if (specs->saturating_p)
10853 error_at (specs->locations[cdw_saturating],
10854 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10855 if (!targetm.fixed_point_supported_p ())
10856 error_at (specs->locations[cdw_saturating],
10857 "fixed-point types not supported for this target");
10858 specs->typespec_word = cts_fract;
10860 else if (specs->long_p || specs->short_p
10861 || specs->signed_p || specs->unsigned_p)
10863 specs->typespec_word = cts_int;
10865 else if (specs->complex_p)
10867 specs->typespec_word = cts_double;
10868 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10869 "ISO C does not support plain %<complex%> meaning "
10870 "%<double complex%>");
10872 else
10874 specs->typespec_word = cts_int;
10875 specs->default_int_p = true;
10876 /* We don't diagnose this here because grokdeclarator will
10877 give more specific diagnostics according to whether it is
10878 a function definition. */
10882 /* If "signed" was specified, record this to distinguish "int" and
10883 "signed int" in the case of a bit-field with
10884 -funsigned-bitfields. */
10885 specs->explicit_signed_p = specs->signed_p;
10887 /* Now compute the actual type. */
10888 switch (specs->typespec_word)
10890 case cts_auto_type:
10891 gcc_assert (!specs->long_p && !specs->short_p
10892 && !specs->signed_p && !specs->unsigned_p
10893 && !specs->complex_p);
10894 /* Type to be filled in later. */
10895 break;
10896 case cts_void:
10897 gcc_assert (!specs->long_p && !specs->short_p
10898 && !specs->signed_p && !specs->unsigned_p
10899 && !specs->complex_p);
10900 specs->type = void_type_node;
10901 break;
10902 case cts_bool:
10903 gcc_assert (!specs->long_p && !specs->short_p
10904 && !specs->signed_p && !specs->unsigned_p
10905 && !specs->complex_p);
10906 specs->type = boolean_type_node;
10907 break;
10908 case cts_char:
10909 gcc_assert (!specs->long_p && !specs->short_p);
10910 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10911 if (specs->signed_p)
10912 specs->type = signed_char_type_node;
10913 else if (specs->unsigned_p)
10914 specs->type = unsigned_char_type_node;
10915 else
10916 specs->type = char_type_node;
10917 if (specs->complex_p)
10919 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10920 "ISO C does not support complex integer types");
10921 specs->type = build_complex_type (specs->type);
10923 break;
10924 case cts_int_n:
10925 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10926 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10927 if (! int_n_enabled_p[specs->int_n_idx])
10928 specs->type = integer_type_node;
10929 else
10930 specs->type = (specs->unsigned_p
10931 ? int_n_trees[specs->int_n_idx].unsigned_type
10932 : int_n_trees[specs->int_n_idx].signed_type);
10933 if (specs->complex_p)
10935 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10936 "ISO C does not support complex integer types");
10937 specs->type = build_complex_type (specs->type);
10939 break;
10940 case cts_int:
10941 gcc_assert (!(specs->long_p && specs->short_p));
10942 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10943 if (specs->long_long_p)
10944 specs->type = (specs->unsigned_p
10945 ? long_long_unsigned_type_node
10946 : long_long_integer_type_node);
10947 else if (specs->long_p)
10948 specs->type = (specs->unsigned_p
10949 ? long_unsigned_type_node
10950 : long_integer_type_node);
10951 else if (specs->short_p)
10952 specs->type = (specs->unsigned_p
10953 ? short_unsigned_type_node
10954 : short_integer_type_node);
10955 else
10956 specs->type = (specs->unsigned_p
10957 ? unsigned_type_node
10958 : integer_type_node);
10959 if (specs->complex_p)
10961 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10962 "ISO C does not support complex integer types");
10963 specs->type = build_complex_type (specs->type);
10965 break;
10966 case cts_float:
10967 gcc_assert (!specs->long_p && !specs->short_p
10968 && !specs->signed_p && !specs->unsigned_p);
10969 specs->type = (specs->complex_p
10970 ? complex_float_type_node
10971 : float_type_node);
10972 break;
10973 case cts_double:
10974 gcc_assert (!specs->long_long_p && !specs->short_p
10975 && !specs->signed_p && !specs->unsigned_p);
10976 if (specs->long_p)
10978 specs->type = (specs->complex_p
10979 ? complex_long_double_type_node
10980 : long_double_type_node);
10982 else
10984 specs->type = (specs->complex_p
10985 ? complex_double_type_node
10986 : double_type_node);
10988 break;
10989 case cts_floatn_nx:
10990 gcc_assert (!specs->long_p && !specs->short_p
10991 && !specs->signed_p && !specs->unsigned_p);
10992 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10993 specs->type = integer_type_node;
10994 else if (specs->complex_p)
10995 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
10996 else
10997 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
10998 break;
10999 case cts_dfloat32:
11000 case cts_dfloat64:
11001 case cts_dfloat128:
11002 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11003 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11004 if (specs->typespec_word == cts_dfloat32)
11005 specs->type = dfloat32_type_node;
11006 else if (specs->typespec_word == cts_dfloat64)
11007 specs->type = dfloat64_type_node;
11008 else
11009 specs->type = dfloat128_type_node;
11010 break;
11011 case cts_fract:
11012 gcc_assert (!specs->complex_p);
11013 if (!targetm.fixed_point_supported_p ())
11014 specs->type = integer_type_node;
11015 else if (specs->saturating_p)
11017 if (specs->long_long_p)
11018 specs->type = specs->unsigned_p
11019 ? sat_unsigned_long_long_fract_type_node
11020 : sat_long_long_fract_type_node;
11021 else if (specs->long_p)
11022 specs->type = specs->unsigned_p
11023 ? sat_unsigned_long_fract_type_node
11024 : sat_long_fract_type_node;
11025 else if (specs->short_p)
11026 specs->type = specs->unsigned_p
11027 ? sat_unsigned_short_fract_type_node
11028 : sat_short_fract_type_node;
11029 else
11030 specs->type = specs->unsigned_p
11031 ? sat_unsigned_fract_type_node
11032 : sat_fract_type_node;
11034 else
11036 if (specs->long_long_p)
11037 specs->type = specs->unsigned_p
11038 ? unsigned_long_long_fract_type_node
11039 : long_long_fract_type_node;
11040 else if (specs->long_p)
11041 specs->type = specs->unsigned_p
11042 ? unsigned_long_fract_type_node
11043 : long_fract_type_node;
11044 else if (specs->short_p)
11045 specs->type = specs->unsigned_p
11046 ? unsigned_short_fract_type_node
11047 : short_fract_type_node;
11048 else
11049 specs->type = specs->unsigned_p
11050 ? unsigned_fract_type_node
11051 : fract_type_node;
11053 break;
11054 case cts_accum:
11055 gcc_assert (!specs->complex_p);
11056 if (!targetm.fixed_point_supported_p ())
11057 specs->type = integer_type_node;
11058 else if (specs->saturating_p)
11060 if (specs->long_long_p)
11061 specs->type = specs->unsigned_p
11062 ? sat_unsigned_long_long_accum_type_node
11063 : sat_long_long_accum_type_node;
11064 else if (specs->long_p)
11065 specs->type = specs->unsigned_p
11066 ? sat_unsigned_long_accum_type_node
11067 : sat_long_accum_type_node;
11068 else if (specs->short_p)
11069 specs->type = specs->unsigned_p
11070 ? sat_unsigned_short_accum_type_node
11071 : sat_short_accum_type_node;
11072 else
11073 specs->type = specs->unsigned_p
11074 ? sat_unsigned_accum_type_node
11075 : sat_accum_type_node;
11077 else
11079 if (specs->long_long_p)
11080 specs->type = specs->unsigned_p
11081 ? unsigned_long_long_accum_type_node
11082 : long_long_accum_type_node;
11083 else if (specs->long_p)
11084 specs->type = specs->unsigned_p
11085 ? unsigned_long_accum_type_node
11086 : long_accum_type_node;
11087 else if (specs->short_p)
11088 specs->type = specs->unsigned_p
11089 ? unsigned_short_accum_type_node
11090 : short_accum_type_node;
11091 else
11092 specs->type = specs->unsigned_p
11093 ? unsigned_accum_type_node
11094 : accum_type_node;
11096 break;
11097 default:
11098 gcc_unreachable ();
11101 return specs;
11104 /* Perform final processing on one file scope's declarations (or the
11105 external scope's declarations), GLOBALS. */
11107 static void
11108 c_write_global_declarations_1 (tree globals)
11110 tree decl;
11111 bool reconsider;
11113 /* Process the decls in the order they were written. */
11114 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11116 /* Check for used but undefined static functions using the C
11117 standard's definition of "used", and set TREE_NO_WARNING so
11118 that check_global_declaration doesn't repeat the check. */
11119 if (TREE_CODE (decl) == FUNCTION_DECL
11120 && DECL_INITIAL (decl) == NULL_TREE
11121 && DECL_EXTERNAL (decl)
11122 && !TREE_PUBLIC (decl))
11124 if (C_DECL_USED (decl))
11126 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11127 TREE_NO_WARNING (decl) = 1;
11129 /* For -Wunused-function warn about unused static prototypes. */
11130 else if (warn_unused_function
11131 && ! DECL_ARTIFICIAL (decl)
11132 && ! TREE_NO_WARNING (decl))
11134 warning (OPT_Wunused_function,
11135 "%q+F declared %<static%> but never defined", decl);
11136 TREE_NO_WARNING (decl) = 1;
11140 wrapup_global_declaration_1 (decl);
11145 reconsider = false;
11146 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11147 reconsider |= wrapup_global_declaration_2 (decl);
11149 while (reconsider);
11152 /* Callback to collect a source_ref from a DECL. */
11154 static void
11155 collect_source_ref_cb (tree decl)
11157 if (!DECL_IS_BUILTIN (decl))
11158 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11161 /* Preserve the external declarations scope across a garbage collect. */
11162 static GTY(()) tree ext_block;
11164 /* Collect all references relevant to SOURCE_FILE. */
11166 static void
11167 collect_all_refs (const char *source_file)
11169 tree t;
11170 unsigned i;
11172 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11173 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11175 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11178 /* Iterate over all global declarations and call CALLBACK. */
11180 static void
11181 for_each_global_decl (void (*callback) (tree decl))
11183 tree t;
11184 tree decls;
11185 tree decl;
11186 unsigned i;
11188 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11190 decls = DECL_INITIAL (t);
11191 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11192 callback (decl);
11195 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11196 callback (decl);
11199 /* Perform any final parser cleanups and generate initial debugging
11200 information. */
11202 void
11203 c_parse_final_cleanups (void)
11205 tree t;
11206 unsigned i;
11208 /* We don't want to do this if generating a PCH. */
11209 if (pch_file)
11210 return;
11212 timevar_stop (TV_PHASE_PARSING);
11213 timevar_start (TV_PHASE_DEFERRED);
11215 /* Do the Objective-C stuff. This is where all the Objective-C
11216 module stuff gets generated (symtab, class/protocol/selector
11217 lists etc). */
11218 if (c_dialect_objc ())
11219 objc_write_global_declarations ();
11221 /* Close the external scope. */
11222 ext_block = pop_scope ();
11223 external_scope = 0;
11224 gcc_assert (!current_scope);
11226 /* Handle -fdump-ada-spec[-slim]. */
11227 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11229 /* Build a table of files to generate specs for */
11230 if (flag_dump_ada_spec_slim)
11231 collect_source_ref (main_input_filename);
11232 else
11233 for_each_global_decl (collect_source_ref_cb);
11235 dump_ada_specs (collect_all_refs, NULL);
11238 /* Process all file scopes in this compilation, and the external_scope,
11239 through wrapup_global_declarations. */
11240 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11241 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11242 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11244 timevar_stop (TV_PHASE_DEFERRED);
11245 timevar_start (TV_PHASE_PARSING);
11247 ext_block = NULL;
11250 /* Register reserved keyword WORD as qualifier for address space AS. */
11252 void
11253 c_register_addr_space (const char *word, addr_space_t as)
11255 int rid = RID_FIRST_ADDR_SPACE + as;
11256 tree id;
11258 /* Address space qualifiers are only supported
11259 in C with GNU extensions enabled. */
11260 if (c_dialect_objc () || flag_no_asm)
11261 return;
11263 id = get_identifier (word);
11264 C_SET_RID_CODE (id, rid);
11265 C_IS_RESERVED_WORD (id) = 1;
11266 ridpointers [rid] = id;
11269 /* Return identifier to look up for omp declare reduction. */
11271 tree
11272 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11274 const char *p = NULL;
11275 switch (reduction_code)
11277 case PLUS_EXPR: p = "+"; break;
11278 case MULT_EXPR: p = "*"; break;
11279 case MINUS_EXPR: p = "-"; break;
11280 case BIT_AND_EXPR: p = "&"; break;
11281 case BIT_XOR_EXPR: p = "^"; break;
11282 case BIT_IOR_EXPR: p = "|"; break;
11283 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11284 case TRUTH_ORIF_EXPR: p = "||"; break;
11285 case MIN_EXPR: p = "min"; break;
11286 case MAX_EXPR: p = "max"; break;
11287 default:
11288 break;
11291 if (p == NULL)
11293 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11294 return error_mark_node;
11295 p = IDENTIFIER_POINTER (reduction_id);
11298 const char prefix[] = "omp declare reduction ";
11299 size_t lenp = sizeof (prefix);
11300 size_t len = strlen (p);
11301 char *name = XALLOCAVEC (char, lenp + len);
11302 memcpy (name, prefix, lenp - 1);
11303 memcpy (name + lenp - 1, p, len + 1);
11304 return get_identifier (name);
11307 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11308 VAR_DECL, bind it into the current scope and return it. */
11310 tree
11311 c_omp_reduction_decl (tree reduction_id)
11313 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11314 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11315 return b->decl;
11317 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11318 reduction_id, integer_type_node);
11319 DECL_ARTIFICIAL (decl) = 1;
11320 DECL_EXTERNAL (decl) = 1;
11321 TREE_STATIC (decl) = 1;
11322 TREE_PUBLIC (decl) = 0;
11323 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11324 return decl;
11327 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11329 tree
11330 c_omp_reduction_lookup (tree reduction_id, tree type)
11332 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11333 while (b)
11335 tree t;
11336 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11337 if (comptypes (TREE_PURPOSE (t), type))
11338 return TREE_VALUE (t);
11339 b = b->shadowed;
11341 return error_mark_node;
11344 /* Helper function called via walk_tree, to diagnose invalid
11345 #pragma omp declare reduction combiners or initializers. */
11347 tree
11348 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11350 tree *vars = (tree *) data;
11351 if (SSA_VAR_P (*tp)
11352 && !DECL_ARTIFICIAL (*tp)
11353 && *tp != vars[0]
11354 && *tp != vars[1])
11356 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11357 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11358 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11359 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11360 *tp);
11361 else
11362 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11363 "to variable %qD which is not %<omp_priv%> nor "
11364 "%<omp_orig%>",
11365 *tp);
11366 return *tp;
11368 return NULL_TREE;
11371 #include "gt-c-c-decl.h"