PR c/81544 - attribute noreturn and warn_unused_result on the same function accepted
[official-gcc.git] / gcc / c / c-decl.c
blobaaa967874727613e3fc955627ab18829340a53c6
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 #define INCLUDE_UNIQUE_PTR
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
62 enum decl_context
63 { NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
69 /* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
72 deprecated items. */
74 enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
91 static location_t current_function_prototype_locus;
93 /* Whether this prototype was built-in. */
95 static bool current_function_prototype_built_in;
97 /* The argument type information of this prototype. */
99 static tree current_function_prototype_arg_types;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* A list of decls to be made automatically visible in each file scope. */
120 static GTY(()) tree visible_builtins;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
145 static bool undef_nested_function;
147 /* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149 int current_omp_declare_target_attribute;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 /* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
212 struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The binding oracle; see c-tree.h. */
224 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
226 /* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228 #define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
231 static inline struct c_binding* *
232 i_symbol_binding (tree node)
234 struct lang_identifier *lid
235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
237 if (lid->symbol_binding == NULL
238 && c_binding_oracle != NULL
239 && !I_SYMBOL_CHECKED (node))
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL, node);
247 return &lid->symbol_binding;
250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
252 #define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
255 /* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257 #define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
260 static inline struct c_binding **
261 i_tag_binding (tree node)
263 struct lang_identifier *lid
264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
266 if (lid->tag_binding == NULL
267 && c_binding_oracle != NULL
268 && !I_TAG_CHECKED (node))
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node) = 1;
273 c_binding_oracle (C_ORACLE_TAG, node);
276 return &lid->tag_binding;
279 #define I_TAG_BINDING(node) (*i_tag_binding (node))
281 #define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
284 /* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286 #define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
289 static inline struct c_binding **
290 i_label_binding (tree node)
292 struct lang_identifier *lid
293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
295 if (lid->label_binding == NULL
296 && c_binding_oracle != NULL
297 && !I_LABEL_CHECKED (node))
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node) = 1;
302 c_binding_oracle (C_ORACLE_LABEL, node);
305 return &lid->label_binding;
308 #define I_LABEL_BINDING(node) (*i_label_binding (node))
310 #define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
313 /* The resulting tree type. */
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
318 union tree_node GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
320 generic;
321 struct lang_identifier GTY ((tag ("1"))) identifier;
324 /* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
330 struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
350 /* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
354 struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
361 typedef struct c_goto_bindings *c_goto_bindings_p;
363 /* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
366 struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
383 /* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
388 Most declarations are recorded in the current scope.
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
409 undefined.
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
416 this scope.
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
422 list here.)
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
428 struct GTY((chain_next ("%h.outer"))) c_scope {
429 /* The scope containing this one. */
430 struct c_scope *outer;
432 /* The next outermost function scope. */
433 struct c_scope *outer_function;
435 /* All bindings in this scope. */
436 struct c_binding *bindings;
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
440 tree blocks;
441 tree blocks_last;
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth : 28;
447 /* True if we are currently filling this scope with parameter
448 declarations. */
449 BOOL_BITFIELD parm_flag : 1;
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec : 1;
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls : 1;
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body : 1;
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep : 1;
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64 : 1;
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings : 1;
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl : 1;
485 /* The scope currently in effect. */
487 static GTY(()) struct c_scope *current_scope;
489 /* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
493 static GTY(()) struct c_scope *current_function_scope;
495 /* The C file scope. This is reset for each input translation unit. */
497 static GTY(()) struct c_scope *file_scope;
499 /* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
502 static GTY(()) struct c_scope *external_scope;
504 /* A chain of c_scope structures awaiting reuse. */
506 static GTY((deletable)) struct c_scope *scope_freelist;
508 /* A chain of c_binding structures awaiting reuse. */
510 static GTY((deletable)) struct c_binding *binding_freelist;
512 /* Append VAR to LIST in scope SCOPE. */
513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
515 tree d_ = (decl); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
518 else \
519 s_->list = d_; \
520 s_->list##_last = d_; \
521 } while (0)
523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
527 if (t_->to##_last) \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
529 else \
530 t_->to = f_->from; \
531 t_->to##_last = f_->from##_last; \
532 } while (0)
534 /* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
539 struct GTY((chain_next ("%h.next"))) c_inline_static {
540 /* The location for a diagnostic. */
541 location_t location;
543 /* The function that may be an inline definition. */
544 tree function;
546 /* The object or function referenced. */
547 tree static_decl;
549 /* What sort of reference this is. */
550 enum c_inline_static_type type;
552 /* The next such structure or NULL. */
553 struct c_inline_static *next;
556 /* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558 static GTY(()) struct c_inline_static *c_inline_statics;
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
562 static bool keep_next_level_flag;
564 /* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
568 static bool next_is_function_body;
570 /* A vector of pointers to c_binding structures. */
572 typedef struct c_binding *c_binding_ptr;
574 /* Information that we keep for a struct or union while it is being
575 parsed. */
577 struct c_struct_parse_info
579 /* If warn_cxx_compat, a list of types defined within this
580 struct. */
581 auto_vec<tree> struct_types;
582 /* If warn_cxx_compat, a list of field names which have bindings,
583 and which are defined in this struct, but which are not defined
584 in any enclosing struct. This is used to clear the in_struct
585 field of the c_bindings structure. */
586 auto_vec<c_binding_ptr> fields;
587 /* If warn_cxx_compat, a list of typedef names used when defining
588 fields in this struct. */
589 auto_vec<tree> typedefs_seen;
592 /* Information for the struct or union currently being parsed, or
593 NULL if not parsing a struct or union. */
594 static struct c_struct_parse_info *struct_parse_info;
596 /* Forward declarations. */
597 static tree lookup_name_in_scope (tree, struct c_scope *);
598 static tree c_make_fname_decl (location_t, tree, int);
599 static tree grokdeclarator (const struct c_declarator *,
600 struct c_declspecs *,
601 enum decl_context, bool, tree *, tree *, tree *,
602 bool *, enum deprecated_states);
603 static tree grokparms (struct c_arg_info *, bool);
604 static void layout_array_type (tree);
605 static void warn_defaults_to (location_t, int, const char *, ...)
606 ATTRIBUTE_GCC_DIAG(3,4);
608 /* T is a statement. Add it to the statement-tree. This is the
609 C/ObjC version--C++ has a slightly different version of this
610 function. */
612 tree
613 add_stmt (tree t)
615 enum tree_code code = TREE_CODE (t);
617 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
619 if (!EXPR_HAS_LOCATION (t))
620 SET_EXPR_LOCATION (t, input_location);
623 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
624 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
626 /* Add T to the statement-tree. Non-side-effect statements need to be
627 recorded during statement expressions. */
628 if (!building_stmt_list_p ())
629 push_stmt_list ();
630 append_to_statement_list_force (t, &cur_stmt_list);
632 return t;
635 /* Build a pointer type using the default pointer mode. */
637 static tree
638 c_build_pointer_type (tree to_type)
640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
641 : TYPE_ADDR_SPACE (to_type);
642 machine_mode pointer_mode;
644 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
645 pointer_mode = targetm.addr_space.pointer_mode (as);
646 else
647 pointer_mode = c_default_pointer_mode;
648 return build_pointer_type_for_mode (to_type, pointer_mode, false);
652 /* Return true if we will want to say something if a goto statement
653 crosses DECL. */
655 static bool
656 decl_jump_unsafe (tree decl)
658 if (error_operand_p (decl))
659 return false;
661 /* Always warn about crossing variably modified types. */
662 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
663 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
664 return true;
666 /* Otherwise, only warn if -Wgoto-misses-init and this is an
667 initialized automatic decl. */
668 if (warn_jump_misses_init
669 && VAR_P (decl)
670 && !TREE_STATIC (decl)
671 && DECL_INITIAL (decl) != NULL_TREE)
672 return true;
674 return false;
678 void
679 c_print_identifier (FILE *file, tree node, int indent)
681 void (*save) (enum c_oracle_request, tree identifier);
683 /* Temporarily hide any binding oracle. Without this, calls to
684 debug_tree from the debugger will end up calling into the oracle,
685 making for a confusing debug session. As the oracle isn't needed
686 here for normal operation, it's simplest to suppress it. */
687 save = c_binding_oracle;
688 c_binding_oracle = NULL;
690 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
691 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
692 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
693 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
695 tree rid = ridpointers[C_RID_CODE (node)];
696 indent_to (file, indent + 4);
697 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
698 (void *) rid, IDENTIFIER_POINTER (rid));
701 c_binding_oracle = save;
704 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
705 which may be any of several kinds of DECL or TYPE or error_mark_node,
706 in the scope SCOPE. */
707 static void
708 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
709 bool nested, location_t locus)
711 struct c_binding *b, **here;
713 if (binding_freelist)
715 b = binding_freelist;
716 binding_freelist = b->prev;
718 else
719 b = ggc_alloc<c_binding> ();
721 b->shadowed = 0;
722 b->decl = decl;
723 b->id = name;
724 b->depth = scope->depth;
725 b->invisible = invisible;
726 b->nested = nested;
727 b->inner_comp = 0;
728 b->in_struct = 0;
729 b->locus = locus;
731 b->u.type = NULL;
733 b->prev = scope->bindings;
734 scope->bindings = b;
736 if (decl_jump_unsafe (decl))
737 scope->has_jump_unsafe_decl = 1;
739 if (!name)
740 return;
742 switch (TREE_CODE (decl))
744 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
745 case ENUMERAL_TYPE:
746 case UNION_TYPE:
747 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
748 case VAR_DECL:
749 case FUNCTION_DECL:
750 case TYPE_DECL:
751 case CONST_DECL:
752 case PARM_DECL:
753 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
755 default:
756 gcc_unreachable ();
759 /* Locate the appropriate place in the chain of shadowed decls
760 to insert this binding. Normally, scope == current_scope and
761 this does nothing. */
762 while (*here && (*here)->depth > scope->depth)
763 here = &(*here)->shadowed;
765 b->shadowed = *here;
766 *here = b;
769 /* Clear the binding structure B, stick it on the binding_freelist,
770 and return the former value of b->prev. This is used by pop_scope
771 and get_parm_info to iterate destructively over all the bindings
772 from a given scope. */
773 static struct c_binding *
774 free_binding_and_advance (struct c_binding *b)
776 struct c_binding *prev = b->prev;
778 memset (b, 0, sizeof (struct c_binding));
779 b->prev = binding_freelist;
780 binding_freelist = b;
782 return prev;
785 /* Bind a label. Like bind, but skip fields which aren't used for
786 labels, and add the LABEL_VARS value. */
787 static void
788 bind_label (tree name, tree label, struct c_scope *scope,
789 struct c_label_vars *label_vars)
791 struct c_binding *b;
793 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
794 UNKNOWN_LOCATION);
796 scope->has_label_bindings = true;
798 b = scope->bindings;
799 gcc_assert (b->decl == label);
800 label_vars->shadowed = b->u.label;
801 b->u.label = label_vars;
804 /* Hook called at end of compilation to assume 1 elt
805 for a file-scope tentative array defn that wasn't complete before. */
807 void
808 c_finish_incomplete_decl (tree decl)
810 if (VAR_P (decl))
812 tree type = TREE_TYPE (decl);
813 if (type != error_mark_node
814 && TREE_CODE (type) == ARRAY_TYPE
815 && !DECL_EXTERNAL (decl)
816 && TYPE_DOMAIN (type) == NULL_TREE)
818 warning_at (DECL_SOURCE_LOCATION (decl),
819 0, "array %q+D assumed to have one element", decl);
821 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
823 relayout_decl (decl);
828 /* Record that inline function FUNC contains a reference (location
829 LOC) to static DECL (file-scope or function-local according to
830 TYPE). */
832 void
833 record_inline_static (location_t loc, tree func, tree decl,
834 enum c_inline_static_type type)
836 c_inline_static *csi = ggc_alloc<c_inline_static> ();
837 csi->location = loc;
838 csi->function = func;
839 csi->static_decl = decl;
840 csi->type = type;
841 csi->next = c_inline_statics;
842 c_inline_statics = csi;
845 /* Check for references to static declarations in inline functions at
846 the end of the translation unit and diagnose them if the functions
847 are still inline definitions. */
849 static void
850 check_inline_statics (void)
852 struct c_inline_static *csi;
853 for (csi = c_inline_statics; csi; csi = csi->next)
855 if (DECL_EXTERNAL (csi->function))
856 switch (csi->type)
858 case csi_internal:
859 pedwarn (csi->location, 0,
860 "%qD is static but used in inline function %qD "
861 "which is not static", csi->static_decl, csi->function);
862 break;
863 case csi_modifiable:
864 pedwarn (csi->location, 0,
865 "%q+D is static but declared in inline function %qD "
866 "which is not static", csi->static_decl, csi->function);
867 break;
868 default:
869 gcc_unreachable ();
872 c_inline_statics = NULL;
875 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
876 for the current state, otherwise set it to uninitialized. */
878 static void
879 set_spot_bindings (struct c_spot_bindings *p, bool defining)
881 if (defining)
883 p->scope = current_scope;
884 p->bindings_in_scope = current_scope->bindings;
886 else
888 p->scope = NULL;
889 p->bindings_in_scope = NULL;
891 p->stmt_exprs = 0;
892 p->left_stmt_expr = false;
895 /* Update spot bindings P as we pop out of SCOPE. Return true if we
896 should push decls for a label. */
898 static bool
899 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
901 if (p->scope != scope)
903 /* This label or goto is defined in some other scope, or it is a
904 label which is not yet defined. There is nothing to
905 update. */
906 return false;
909 /* Adjust the spot bindings to refer to the bindings already defined
910 in the enclosing scope. */
911 p->scope = scope->outer;
912 p->bindings_in_scope = p->scope->bindings;
914 return true;
917 /* The Objective-C front-end often needs to determine the current scope. */
919 void *
920 objc_get_current_scope (void)
922 return current_scope;
925 /* The following function is used only by Objective-C. It needs to live here
926 because it accesses the innards of c_scope. */
928 void
929 objc_mark_locals_volatile (void *enclosing_blk)
931 struct c_scope *scope;
932 struct c_binding *b;
934 for (scope = current_scope;
935 scope && scope != enclosing_blk;
936 scope = scope->outer)
938 for (b = scope->bindings; b; b = b->prev)
939 objc_volatilize_decl (b->decl);
941 /* Do not climb up past the current function. */
942 if (scope->function_body)
943 break;
947 /* Return true if we are in the global binding level. */
949 bool
950 global_bindings_p (void)
952 return current_scope == file_scope;
955 void
956 keep_next_level (void)
958 keep_next_level_flag = true;
961 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
963 void
964 set_float_const_decimal64 (void)
966 current_scope->float_const_decimal64 = true;
969 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
971 void
972 clear_float_const_decimal64 (void)
974 current_scope->float_const_decimal64 = false;
977 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
979 bool
980 float_const_decimal64_p (void)
982 return current_scope->float_const_decimal64;
985 /* Identify this scope as currently being filled with parameters. */
987 void
988 declare_parm_level (void)
990 current_scope->parm_flag = true;
993 void
994 push_scope (void)
996 if (next_is_function_body)
998 /* This is the transition from the parameters to the top level
999 of the function body. These are the same scope
1000 (C99 6.2.1p4,6) so we do not push another scope structure.
1001 next_is_function_body is set only by store_parm_decls, which
1002 in turn is called when and only when we are about to
1003 encounter the opening curly brace for the function body.
1005 The outermost block of a function always gets a BLOCK node,
1006 because the debugging output routines expect that each
1007 function has at least one BLOCK. */
1008 current_scope->parm_flag = false;
1009 current_scope->function_body = true;
1010 current_scope->keep = true;
1011 current_scope->outer_function = current_function_scope;
1012 current_function_scope = current_scope;
1014 keep_next_level_flag = false;
1015 next_is_function_body = false;
1017 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1018 if (current_scope->outer)
1019 current_scope->float_const_decimal64
1020 = current_scope->outer->float_const_decimal64;
1021 else
1022 current_scope->float_const_decimal64 = false;
1024 else
1026 struct c_scope *scope;
1027 if (scope_freelist)
1029 scope = scope_freelist;
1030 scope_freelist = scope->outer;
1032 else
1033 scope = ggc_cleared_alloc<c_scope> ();
1035 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1036 if (current_scope)
1037 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1038 else
1039 scope->float_const_decimal64 = false;
1041 scope->keep = keep_next_level_flag;
1042 scope->outer = current_scope;
1043 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1045 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1046 possible. */
1047 if (current_scope && scope->depth == 0)
1049 scope->depth--;
1050 sorry ("GCC supports only %u nested scopes", scope->depth);
1053 current_scope = scope;
1054 keep_next_level_flag = false;
1058 /* This is called when we are leaving SCOPE. For each label defined
1059 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1060 These are the decls whose initialization will be skipped by a goto
1061 later in the function. */
1063 static void
1064 update_label_decls (struct c_scope *scope)
1066 struct c_scope *s;
1068 s = scope;
1069 while (s != NULL)
1071 if (s->has_label_bindings)
1073 struct c_binding *b;
1075 for (b = s->bindings; b != NULL; b = b->prev)
1077 struct c_label_vars *label_vars;
1078 struct c_binding *b1;
1079 bool hjud;
1080 unsigned int ix;
1081 struct c_goto_bindings *g;
1083 if (TREE_CODE (b->decl) != LABEL_DECL)
1084 continue;
1085 label_vars = b->u.label;
1087 b1 = label_vars->label_bindings.bindings_in_scope;
1088 if (label_vars->label_bindings.scope == NULL)
1089 hjud = false;
1090 else
1091 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1092 if (update_spot_bindings (scope, &label_vars->label_bindings))
1094 /* This label is defined in this scope. */
1095 if (hjud)
1097 for (; b1 != NULL; b1 = b1->prev)
1099 /* A goto from later in the function to this
1100 label will never see the initialization
1101 of B1, if any. Save it to issue a
1102 warning if needed. */
1103 if (decl_jump_unsafe (b1->decl))
1104 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1109 /* Update the bindings of any goto statements associated
1110 with this label. */
1111 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1112 update_spot_bindings (scope, &g->goto_bindings);
1116 /* Don't search beyond the current function. */
1117 if (s == current_function_scope)
1118 break;
1120 s = s->outer;
1124 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1126 static void
1127 set_type_context (tree type, tree context)
1129 for (type = TYPE_MAIN_VARIANT (type); type;
1130 type = TYPE_NEXT_VARIANT (type))
1131 TYPE_CONTEXT (type) = context;
1134 /* Exit a scope. Restore the state of the identifier-decl mappings
1135 that were in effect when this scope was entered. Return a BLOCK
1136 node containing all the DECLs in this scope that are of interest
1137 to debug info generation. */
1139 tree
1140 pop_scope (void)
1142 struct c_scope *scope = current_scope;
1143 tree block, context, p;
1144 struct c_binding *b;
1146 bool functionbody = scope->function_body;
1147 bool keep = functionbody || scope->keep || scope->bindings;
1149 update_label_decls (scope);
1151 /* If appropriate, create a BLOCK to record the decls for the life
1152 of this function. */
1153 block = NULL_TREE;
1154 if (keep)
1156 block = make_node (BLOCK);
1157 BLOCK_SUBBLOCKS (block) = scope->blocks;
1158 TREE_USED (block) = 1;
1160 /* In each subblock, record that this is its superior. */
1161 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1162 BLOCK_SUPERCONTEXT (p) = block;
1164 BLOCK_VARS (block) = NULL_TREE;
1167 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1168 scope must be set so that they point to the appropriate
1169 construct, i.e. either to the current FUNCTION_DECL node, or
1170 else to the BLOCK node we just constructed.
1172 Note that for tagged types whose scope is just the formal
1173 parameter list for some function type specification, we can't
1174 properly set their TYPE_CONTEXTs here, because we don't have a
1175 pointer to the appropriate FUNCTION_TYPE node readily available
1176 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1177 type nodes get set in `grokdeclarator' as soon as we have created
1178 the FUNCTION_TYPE node which will represent the "scope" for these
1179 "parameter list local" tagged types. */
1180 if (scope->function_body)
1181 context = current_function_decl;
1182 else if (scope == file_scope)
1184 tree file_decl
1185 = build_translation_unit_decl (get_identifier (main_input_filename));
1186 context = file_decl;
1187 debug_hooks->register_main_translation_unit (file_decl);
1189 else
1190 context = block;
1192 /* Clear all bindings in this scope. */
1193 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1195 p = b->decl;
1196 switch (TREE_CODE (p))
1198 case LABEL_DECL:
1199 /* Warnings for unused labels, errors for undefined labels. */
1200 if (TREE_USED (p) && !DECL_INITIAL (p))
1202 error ("label %q+D used but not defined", p);
1203 DECL_INITIAL (p) = error_mark_node;
1205 else
1206 warn_for_unused_label (p);
1208 /* Labels go in BLOCK_VARS. */
1209 DECL_CHAIN (p) = BLOCK_VARS (block);
1210 BLOCK_VARS (block) = p;
1211 gcc_assert (I_LABEL_BINDING (b->id) == b);
1212 I_LABEL_BINDING (b->id) = b->shadowed;
1214 /* Also pop back to the shadowed label_vars. */
1215 release_tree_vector (b->u.label->decls_in_scope);
1216 b->u.label = b->u.label->shadowed;
1217 break;
1219 case ENUMERAL_TYPE:
1220 case UNION_TYPE:
1221 case RECORD_TYPE:
1222 set_type_context (p, context);
1224 /* Types may not have tag-names, in which case the type
1225 appears in the bindings list with b->id NULL. */
1226 if (b->id)
1228 gcc_assert (I_TAG_BINDING (b->id) == b);
1229 I_TAG_BINDING (b->id) = b->shadowed;
1231 break;
1233 case FUNCTION_DECL:
1234 /* Propagate TREE_ADDRESSABLE from nested functions to their
1235 containing functions. */
1236 if (!TREE_ASM_WRITTEN (p)
1237 && DECL_INITIAL (p) != NULL_TREE
1238 && TREE_ADDRESSABLE (p)
1239 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1240 && DECL_ABSTRACT_ORIGIN (p) != p)
1241 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1242 if (!DECL_EXTERNAL (p)
1243 && !DECL_INITIAL (p)
1244 && scope != file_scope
1245 && scope != external_scope)
1247 error ("nested function %q+D declared but never defined", p);
1248 undef_nested_function = true;
1250 else if (DECL_DECLARED_INLINE_P (p)
1251 && TREE_PUBLIC (p)
1252 && !DECL_INITIAL (p))
1254 /* C99 6.7.4p6: "a function with external linkage... declared
1255 with an inline function specifier ... shall also be defined
1256 in the same translation unit." */
1257 if (!flag_gnu89_inline
1258 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1259 && scope != external_scope)
1260 pedwarn (input_location, 0,
1261 "inline function %q+D declared but never defined", p);
1262 DECL_EXTERNAL (p) = 1;
1265 goto common_symbol;
1267 case VAR_DECL:
1268 /* Warnings for unused variables. */
1269 if ((!TREE_USED (p) || !DECL_READ_P (p))
1270 && !TREE_NO_WARNING (p)
1271 && !DECL_IN_SYSTEM_HEADER (p)
1272 && DECL_NAME (p)
1273 && !DECL_ARTIFICIAL (p)
1274 && scope != file_scope
1275 && scope != external_scope)
1277 if (!TREE_USED (p))
1278 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1279 else if (DECL_CONTEXT (p) == current_function_decl)
1280 warning_at (DECL_SOURCE_LOCATION (p),
1281 OPT_Wunused_but_set_variable,
1282 "variable %qD set but not used", p);
1285 if (b->inner_comp)
1287 error ("type of array %q+D completed incompatibly with"
1288 " implicit initialization", p);
1291 /* Fall through. */
1292 case TYPE_DECL:
1293 case CONST_DECL:
1294 common_symbol:
1295 /* All of these go in BLOCK_VARS, but only if this is the
1296 binding in the home scope. */
1297 if (!b->nested)
1299 DECL_CHAIN (p) = BLOCK_VARS (block);
1300 BLOCK_VARS (block) = p;
1302 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1304 /* For block local externs add a special
1305 DECL_EXTERNAL decl for debug info generation. */
1306 tree extp = copy_node (p);
1308 DECL_EXTERNAL (extp) = 1;
1309 TREE_STATIC (extp) = 0;
1310 TREE_PUBLIC (extp) = 1;
1311 DECL_INITIAL (extp) = NULL_TREE;
1312 DECL_LANG_SPECIFIC (extp) = NULL;
1313 DECL_CONTEXT (extp) = current_function_decl;
1314 if (TREE_CODE (p) == FUNCTION_DECL)
1316 DECL_RESULT (extp) = NULL_TREE;
1317 DECL_SAVED_TREE (extp) = NULL_TREE;
1318 DECL_STRUCT_FUNCTION (extp) = NULL;
1320 if (b->locus != UNKNOWN_LOCATION)
1321 DECL_SOURCE_LOCATION (extp) = b->locus;
1322 DECL_CHAIN (extp) = BLOCK_VARS (block);
1323 BLOCK_VARS (block) = extp;
1325 /* If this is the file scope set DECL_CONTEXT of each decl to
1326 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1327 work. */
1328 if (scope == file_scope)
1330 DECL_CONTEXT (p) = context;
1331 if (TREE_CODE (p) == TYPE_DECL
1332 && TREE_TYPE (p) != error_mark_node)
1333 set_type_context (TREE_TYPE (p), context);
1336 gcc_fallthrough ();
1337 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1338 already been put there by store_parm_decls. Unused-
1339 parameter warnings are handled by function.c.
1340 error_mark_node obviously does not go in BLOCK_VARS and
1341 does not get unused-variable warnings. */
1342 case PARM_DECL:
1343 case ERROR_MARK:
1344 /* It is possible for a decl not to have a name. We get
1345 here with b->id NULL in this case. */
1346 if (b->id)
1348 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1349 I_SYMBOL_BINDING (b->id) = b->shadowed;
1350 if (b->shadowed && b->shadowed->u.type)
1351 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1353 break;
1355 default:
1356 gcc_unreachable ();
1361 /* Dispose of the block that we just made inside some higher level. */
1362 if ((scope->function_body || scope == file_scope) && context)
1364 DECL_INITIAL (context) = block;
1365 BLOCK_SUPERCONTEXT (block) = context;
1367 else if (scope->outer)
1369 if (block)
1370 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1371 /* If we did not make a block for the scope just exited, any
1372 blocks made for inner scopes must be carried forward so they
1373 will later become subblocks of something else. */
1374 else if (scope->blocks)
1375 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1378 /* Pop the current scope, and free the structure for reuse. */
1379 current_scope = scope->outer;
1380 if (scope->function_body)
1381 current_function_scope = scope->outer_function;
1383 memset (scope, 0, sizeof (struct c_scope));
1384 scope->outer = scope_freelist;
1385 scope_freelist = scope;
1387 return block;
1390 void
1391 push_file_scope (void)
1393 tree decl;
1395 if (file_scope)
1396 return;
1398 push_scope ();
1399 file_scope = current_scope;
1401 start_fname_decls ();
1403 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1404 bind (DECL_NAME (decl), decl, file_scope,
1405 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1408 void
1409 pop_file_scope (void)
1411 /* In case there were missing closebraces, get us back to the global
1412 binding level. */
1413 while (current_scope != file_scope)
1414 pop_scope ();
1416 /* __FUNCTION__ is defined at file scope (""). This
1417 call may not be necessary as my tests indicate it
1418 still works without it. */
1419 finish_fname_decls ();
1421 check_inline_statics ();
1423 /* This is the point to write out a PCH if we're doing that.
1424 In that case we do not want to do anything else. */
1425 if (pch_file)
1427 c_common_write_pch ();
1428 /* Ensure even the callers don't try to finalize the CU. */
1429 flag_syntax_only = 1;
1430 return;
1433 /* Pop off the file scope and close this translation unit. */
1434 pop_scope ();
1435 file_scope = 0;
1437 maybe_apply_pending_pragma_weaks ();
1440 /* Adjust the bindings for the start of a statement expression. */
1442 void
1443 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1445 struct c_scope *scope;
1447 for (scope = current_scope; scope != NULL; scope = scope->outer)
1449 struct c_binding *b;
1451 if (!scope->has_label_bindings)
1452 continue;
1454 for (b = scope->bindings; b != NULL; b = b->prev)
1456 struct c_label_vars *label_vars;
1457 unsigned int ix;
1458 struct c_goto_bindings *g;
1460 if (TREE_CODE (b->decl) != LABEL_DECL)
1461 continue;
1462 label_vars = b->u.label;
1463 ++label_vars->label_bindings.stmt_exprs;
1464 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1465 ++g->goto_bindings.stmt_exprs;
1469 if (switch_bindings != NULL)
1470 ++switch_bindings->stmt_exprs;
1473 /* Adjust the bindings for the end of a statement expression. */
1475 void
1476 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1478 struct c_scope *scope;
1480 for (scope = current_scope; scope != NULL; scope = scope->outer)
1482 struct c_binding *b;
1484 if (!scope->has_label_bindings)
1485 continue;
1487 for (b = scope->bindings; b != NULL; b = b->prev)
1489 struct c_label_vars *label_vars;
1490 unsigned int ix;
1491 struct c_goto_bindings *g;
1493 if (TREE_CODE (b->decl) != LABEL_DECL)
1494 continue;
1495 label_vars = b->u.label;
1496 --label_vars->label_bindings.stmt_exprs;
1497 if (label_vars->label_bindings.stmt_exprs < 0)
1499 label_vars->label_bindings.left_stmt_expr = true;
1500 label_vars->label_bindings.stmt_exprs = 0;
1502 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1504 --g->goto_bindings.stmt_exprs;
1505 if (g->goto_bindings.stmt_exprs < 0)
1507 g->goto_bindings.left_stmt_expr = true;
1508 g->goto_bindings.stmt_exprs = 0;
1514 if (switch_bindings != NULL)
1516 --switch_bindings->stmt_exprs;
1517 gcc_assert (switch_bindings->stmt_exprs >= 0);
1521 /* Push a definition or a declaration of struct, union or enum tag "name".
1522 "type" should be the type node.
1523 We assume that the tag "name" is not already defined, and has a location
1524 of LOC.
1526 Note that the definition may really be just a forward reference.
1527 In that case, the TYPE_SIZE will be zero. */
1529 static void
1530 pushtag (location_t loc, tree name, tree type)
1532 /* Record the identifier as the type's name if it has none. */
1533 if (name && !TYPE_NAME (type))
1534 TYPE_NAME (type) = name;
1535 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1537 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1538 tagged type we just added to the current scope. This fake
1539 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1540 to output a representation of a tagged type, and it also gives
1541 us a convenient place to record the "scope start" address for the
1542 tagged type. */
1544 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1545 TYPE_DECL, NULL_TREE, type));
1547 /* An approximation for now, so we can tell this is a function-scope tag.
1548 This will be updated in pop_scope. */
1549 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1551 if (warn_cxx_compat && name != NULL_TREE)
1553 struct c_binding *b = I_SYMBOL_BINDING (name);
1555 if (b != NULL
1556 && b->decl != NULL_TREE
1557 && TREE_CODE (b->decl) == TYPE_DECL
1558 && (B_IN_CURRENT_SCOPE (b)
1559 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1560 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1561 != TYPE_MAIN_VARIANT (type)))
1563 if (warning_at (loc, OPT_Wc___compat,
1564 ("using %qD as both a typedef and a tag is "
1565 "invalid in C++"), b->decl)
1566 && b->locus != UNKNOWN_LOCATION)
1567 inform (b->locus, "originally defined here");
1572 /* An exported interface to pushtag. This is used by the gdb plugin's
1573 binding oracle to introduce a new tag binding. */
1575 void
1576 c_pushtag (location_t loc, tree name, tree type)
1578 pushtag (loc, name, type);
1581 /* An exported interface to bind a declaration. LOC is the location
1582 to use. DECL is the declaration to bind. The decl's name is used
1583 to determine how it is bound. If DECL is a VAR_DECL, then
1584 IS_GLOBAL determines whether the decl is put into the global (file
1585 and external) scope or the current function's scope; if DECL is not
1586 a VAR_DECL then it is always put into the file scope. */
1588 void
1589 c_bind (location_t loc, tree decl, bool is_global)
1591 struct c_scope *scope;
1592 bool nested = false;
1594 if (!VAR_P (decl) || current_function_scope == NULL)
1596 /* Types and functions are always considered to be global. */
1597 scope = file_scope;
1598 DECL_EXTERNAL (decl) = 1;
1599 TREE_PUBLIC (decl) = 1;
1601 else if (is_global)
1603 /* Also bind it into the external scope. */
1604 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1605 nested = true;
1606 scope = file_scope;
1607 DECL_EXTERNAL (decl) = 1;
1608 TREE_PUBLIC (decl) = 1;
1610 else
1612 DECL_CONTEXT (decl) = current_function_decl;
1613 TREE_PUBLIC (decl) = 0;
1614 scope = current_function_scope;
1617 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1620 /* Subroutine of compare_decls. Allow harmless mismatches in return
1621 and argument types provided that the type modes match. This function
1622 return a unified type given a suitable match, and 0 otherwise. */
1624 static tree
1625 match_builtin_function_types (tree newtype, tree oldtype)
1627 tree newrettype, oldrettype;
1628 tree newargs, oldargs;
1629 tree trytype, tryargs;
1631 /* Accept the return type of the new declaration if same modes. */
1632 oldrettype = TREE_TYPE (oldtype);
1633 newrettype = TREE_TYPE (newtype);
1635 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1636 return NULL_TREE;
1638 oldargs = TYPE_ARG_TYPES (oldtype);
1639 newargs = TYPE_ARG_TYPES (newtype);
1640 tryargs = newargs;
1642 while (oldargs || newargs)
1644 if (!oldargs
1645 || !newargs
1646 || !TREE_VALUE (oldargs)
1647 || !TREE_VALUE (newargs)
1648 || TYPE_MODE (TREE_VALUE (oldargs))
1649 != TYPE_MODE (TREE_VALUE (newargs)))
1650 return NULL_TREE;
1652 oldargs = TREE_CHAIN (oldargs);
1653 newargs = TREE_CHAIN (newargs);
1656 trytype = build_function_type (newrettype, tryargs);
1658 /* Allow declaration to change transaction_safe attribute. */
1659 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1660 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1661 tree newattrs = TYPE_ATTRIBUTES (newtype);
1662 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1663 if (oldtsafe && !newtsafe)
1664 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1665 else if (newtsafe && !oldtsafe)
1666 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1667 NULL_TREE, oldattrs);
1669 return build_type_attribute_variant (trytype, oldattrs);
1672 /* Subroutine of diagnose_mismatched_decls. Check for function type
1673 mismatch involving an empty arglist vs a nonempty one and give clearer
1674 diagnostics. */
1675 static void
1676 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1677 tree newtype, tree oldtype)
1679 tree t;
1681 if (TREE_CODE (olddecl) != FUNCTION_DECL
1682 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1683 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1684 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1685 return;
1687 t = TYPE_ARG_TYPES (oldtype);
1688 if (t == NULL_TREE)
1689 t = TYPE_ARG_TYPES (newtype);
1690 for (; t; t = TREE_CHAIN (t))
1692 tree type = TREE_VALUE (t);
1694 if (TREE_CHAIN (t) == NULL_TREE
1695 && TYPE_MAIN_VARIANT (type) != void_type_node)
1697 inform (input_location, "a parameter list with an ellipsis can%'t match "
1698 "an empty parameter name list declaration");
1699 break;
1702 if (c_type_promotes_to (type) != type)
1704 inform (input_location, "an argument type that has a default promotion can%'t match "
1705 "an empty parameter name list declaration");
1706 break;
1711 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1712 old-style function definition, NEWDECL is a prototype declaration.
1713 Diagnose inconsistencies in the argument list. Returns TRUE if
1714 the prototype is compatible, FALSE if not. */
1715 static bool
1716 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1718 tree newargs, oldargs;
1719 int i;
1721 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1723 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1724 newargs = TYPE_ARG_TYPES (newtype);
1725 i = 1;
1727 for (;;)
1729 tree oldargtype = TREE_VALUE (oldargs);
1730 tree newargtype = TREE_VALUE (newargs);
1732 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1733 return false;
1735 oldargtype = (TYPE_ATOMIC (oldargtype)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (oldargtype));
1739 newargtype = (TYPE_ATOMIC (newargtype)
1740 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1741 TYPE_QUAL_ATOMIC)
1742 : TYPE_MAIN_VARIANT (newargtype));
1744 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1745 break;
1747 /* Reaching the end of just one list means the two decls don't
1748 agree on the number of arguments. */
1749 if (END_OF_ARGLIST (oldargtype))
1751 error ("prototype for %q+D declares more arguments "
1752 "than previous old-style definition", newdecl);
1753 return false;
1755 else if (END_OF_ARGLIST (newargtype))
1757 error ("prototype for %q+D declares fewer arguments "
1758 "than previous old-style definition", newdecl);
1759 return false;
1762 /* Type for passing arg must be consistent with that declared
1763 for the arg. */
1764 else if (!comptypes (oldargtype, newargtype))
1766 error ("prototype for %q+D declares argument %d"
1767 " with incompatible type",
1768 newdecl, i);
1769 return false;
1772 oldargs = TREE_CHAIN (oldargs);
1773 newargs = TREE_CHAIN (newargs);
1774 i++;
1777 /* If we get here, no errors were found, but do issue a warning
1778 for this poor-style construct. */
1779 warning (0, "prototype for %q+D follows non-prototype definition",
1780 newdecl);
1781 return true;
1782 #undef END_OF_ARGLIST
1785 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1786 first in a pair of mismatched declarations, using the diagnostic
1787 function DIAG. */
1788 static void
1789 locate_old_decl (tree decl)
1791 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1792 && !C_DECL_DECLARED_BUILTIN (decl))
1794 else if (DECL_INITIAL (decl))
1795 inform (input_location, "previous definition of %q+D was here", decl);
1796 else if (C_DECL_IMPLICIT (decl))
1797 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1798 else
1799 inform (input_location, "previous declaration of %q+D was here", decl);
1802 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1803 Returns true if the caller should proceed to merge the two, false
1804 if OLDDECL should simply be discarded. As a side effect, issues
1805 all necessary diagnostics for invalid or poor-style combinations.
1806 If it returns true, writes the types of NEWDECL and OLDDECL to
1807 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1808 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1810 static bool
1811 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1812 tree *newtypep, tree *oldtypep)
1814 tree newtype, oldtype;
1815 bool pedwarned = false;
1816 bool warned = false;
1817 bool retval = true;
1819 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1820 && DECL_EXTERNAL (DECL))
1822 /* If we have error_mark_node for either decl or type, just discard
1823 the previous decl - we're in an error cascade already. */
1824 if (olddecl == error_mark_node || newdecl == error_mark_node)
1825 return false;
1826 *oldtypep = oldtype = TREE_TYPE (olddecl);
1827 *newtypep = newtype = TREE_TYPE (newdecl);
1828 if (oldtype == error_mark_node || newtype == error_mark_node)
1829 return false;
1831 /* Two different categories of symbol altogether. This is an error
1832 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1833 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1835 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1836 && DECL_BUILT_IN (olddecl)
1837 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1839 error ("%q+D redeclared as different kind of symbol", newdecl);
1840 locate_old_decl (olddecl);
1842 else if (TREE_PUBLIC (newdecl))
1843 warning (OPT_Wbuiltin_declaration_mismatch,
1844 "built-in function %q+D declared as non-function",
1845 newdecl);
1846 else
1847 warning (OPT_Wshadow, "declaration of %q+D shadows "
1848 "a built-in function", newdecl);
1849 return false;
1852 /* Enumerators have no linkage, so may only be declared once in a
1853 given scope. */
1854 if (TREE_CODE (olddecl) == CONST_DECL)
1856 error ("redeclaration of enumerator %q+D", newdecl);
1857 locate_old_decl (olddecl);
1858 return false;
1861 if (!comptypes (oldtype, newtype))
1863 if (TREE_CODE (olddecl) == FUNCTION_DECL
1864 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1866 /* Accept harmless mismatch in function types.
1867 This is for the ffs and fprintf builtins. */
1868 tree trytype = match_builtin_function_types (newtype, oldtype);
1870 if (trytype && comptypes (newtype, trytype))
1871 *oldtypep = oldtype = trytype;
1872 else
1874 /* If types don't match for a built-in, throw away the
1875 built-in. No point in calling locate_old_decl here, it
1876 won't print anything. */
1877 warning (OPT_Wbuiltin_declaration_mismatch,
1878 "conflicting types for built-in function %q+D",
1879 newdecl);
1880 return false;
1883 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1884 && DECL_IS_BUILTIN (olddecl))
1886 /* A conflicting function declaration for a predeclared
1887 function that isn't actually built in. Objective C uses
1888 these. The new declaration silently overrides everything
1889 but the volatility (i.e. noreturn) indication. See also
1890 below. FIXME: Make Objective C use normal builtins. */
1891 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1892 return false;
1894 /* Permit void foo (...) to match int foo (...) if the latter is
1895 the definition and implicit int was used. See
1896 c-torture/compile/920625-2.c. */
1897 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1898 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1899 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1900 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1902 pedwarned = pedwarn (input_location, 0,
1903 "conflicting types for %q+D", newdecl);
1904 /* Make sure we keep void as the return type. */
1905 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1906 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1908 /* Permit void foo (...) to match an earlier call to foo (...) with
1909 no declared type (thus, implicitly int). */
1910 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1911 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1912 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1913 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1915 pedwarned = pedwarn (input_location, 0,
1916 "conflicting types for %q+D", newdecl);
1917 /* Make sure we keep void as the return type. */
1918 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1920 else
1922 int new_quals = TYPE_QUALS (newtype);
1923 int old_quals = TYPE_QUALS (oldtype);
1925 if (new_quals != old_quals)
1927 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1928 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1929 if (new_addr != old_addr)
1931 if (ADDR_SPACE_GENERIC_P (new_addr))
1932 error ("conflicting named address spaces (generic vs %s) "
1933 "for %q+D",
1934 c_addr_space_name (old_addr), newdecl);
1935 else if (ADDR_SPACE_GENERIC_P (old_addr))
1936 error ("conflicting named address spaces (%s vs generic) "
1937 "for %q+D",
1938 c_addr_space_name (new_addr), newdecl);
1939 else
1940 error ("conflicting named address spaces (%s vs %s) "
1941 "for %q+D",
1942 c_addr_space_name (new_addr),
1943 c_addr_space_name (old_addr),
1944 newdecl);
1947 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1948 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1949 error ("conflicting type qualifiers for %q+D", newdecl);
1951 else
1952 error ("conflicting types for %q+D", newdecl);
1953 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1954 locate_old_decl (olddecl);
1955 return false;
1959 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1960 but silently ignore the redeclaration if either is in a system
1961 header. (Conflicting redeclarations were handled above.) This
1962 is allowed for C11 if the types are the same, not just
1963 compatible. */
1964 if (TREE_CODE (newdecl) == TYPE_DECL)
1966 bool types_different = false;
1967 int comptypes_result;
1969 comptypes_result
1970 = comptypes_check_different_types (oldtype, newtype, &types_different);
1972 if (comptypes_result != 1 || types_different)
1974 error ("redefinition of typedef %q+D with different type", newdecl);
1975 locate_old_decl (olddecl);
1976 return false;
1979 if (DECL_IN_SYSTEM_HEADER (newdecl)
1980 || DECL_IN_SYSTEM_HEADER (olddecl)
1981 || TREE_NO_WARNING (newdecl)
1982 || TREE_NO_WARNING (olddecl))
1983 return true; /* Allow OLDDECL to continue in use. */
1985 if (variably_modified_type_p (newtype, NULL))
1987 error ("redefinition of typedef %q+D with variably modified type",
1988 newdecl);
1989 locate_old_decl (olddecl);
1991 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1992 "redefinition of typedef %q+D", newdecl))
1993 locate_old_decl (olddecl);
1995 return true;
1998 /* Function declarations can either be 'static' or 'extern' (no
1999 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2000 can never conflict with each other on account of linkage
2001 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2002 gnu89 mode permits two definitions if one is 'extern inline' and
2003 one is not. The non- extern-inline definition supersedes the
2004 extern-inline definition. */
2006 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2008 /* If you declare a built-in function name as static, or
2009 define the built-in with an old-style definition (so we
2010 can't validate the argument list) the built-in definition is
2011 overridden, but optionally warn this was a bad choice of name. */
2012 if (DECL_BUILT_IN (olddecl)
2013 && !C_DECL_DECLARED_BUILTIN (olddecl)
2014 && (!TREE_PUBLIC (newdecl)
2015 || (DECL_INITIAL (newdecl)
2016 && !prototype_p (TREE_TYPE (newdecl)))))
2018 warning (OPT_Wshadow, "declaration of %q+D shadows "
2019 "a built-in function", newdecl);
2020 /* Discard the old built-in function. */
2021 return false;
2024 if (DECL_INITIAL (newdecl))
2026 if (DECL_INITIAL (olddecl))
2028 /* If both decls are in the same TU and the new declaration
2029 isn't overriding an extern inline reject the new decl.
2030 In c99, no overriding is allowed in the same translation
2031 unit. */
2032 if ((!DECL_EXTERN_INLINE (olddecl)
2033 || DECL_EXTERN_INLINE (newdecl)
2034 || (!flag_gnu89_inline
2035 && (!DECL_DECLARED_INLINE_P (olddecl)
2036 || !lookup_attribute ("gnu_inline",
2037 DECL_ATTRIBUTES (olddecl)))
2038 && (!DECL_DECLARED_INLINE_P (newdecl)
2039 || !lookup_attribute ("gnu_inline",
2040 DECL_ATTRIBUTES (newdecl))))
2042 && same_translation_unit_p (newdecl, olddecl))
2044 error ("redefinition of %q+D", newdecl);
2045 locate_old_decl (olddecl);
2046 return false;
2050 /* If we have a prototype after an old-style function definition,
2051 the argument types must be checked specially. */
2052 else if (DECL_INITIAL (olddecl)
2053 && !prototype_p (oldtype) && prototype_p (newtype)
2054 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2055 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2057 locate_old_decl (olddecl);
2058 return false;
2060 /* A non-static declaration (even an "extern") followed by a
2061 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2062 The same is true for a static forward declaration at block
2063 scope followed by a non-static declaration/definition at file
2064 scope. Static followed by non-static at the same scope is
2065 not undefined behavior, and is the most convenient way to get
2066 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2067 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2068 we do diagnose it if -Wtraditional. */
2069 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2071 /* Two exceptions to the rule. If olddecl is an extern
2072 inline, or a predeclared function that isn't actually
2073 built in, newdecl silently overrides olddecl. The latter
2074 occur only in Objective C; see also above. (FIXME: Make
2075 Objective C use normal builtins.) */
2076 if (!DECL_IS_BUILTIN (olddecl)
2077 && !DECL_EXTERN_INLINE (olddecl))
2079 error ("static declaration of %q+D follows "
2080 "non-static declaration", newdecl);
2081 locate_old_decl (olddecl);
2083 return false;
2085 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2087 if (DECL_CONTEXT (olddecl))
2089 error ("non-static declaration of %q+D follows "
2090 "static declaration", newdecl);
2091 locate_old_decl (olddecl);
2092 return false;
2094 else if (warn_traditional)
2096 warned |= warning (OPT_Wtraditional,
2097 "non-static declaration of %q+D "
2098 "follows static declaration", newdecl);
2102 /* Make sure gnu_inline attribute is either not present, or
2103 present on all inline decls. */
2104 if (DECL_DECLARED_INLINE_P (olddecl)
2105 && DECL_DECLARED_INLINE_P (newdecl))
2107 bool newa = lookup_attribute ("gnu_inline",
2108 DECL_ATTRIBUTES (newdecl)) != NULL;
2109 bool olda = lookup_attribute ("gnu_inline",
2110 DECL_ATTRIBUTES (olddecl)) != NULL;
2111 if (newa != olda)
2113 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2114 newa ? newdecl : olddecl);
2115 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2116 "but not here");
2120 else if (VAR_P (newdecl))
2122 /* Only variables can be thread-local, and all declarations must
2123 agree on this property. */
2124 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2126 /* Nothing to check. Since OLDDECL is marked threadprivate
2127 and NEWDECL does not have a thread-local attribute, we
2128 will merge the threadprivate attribute into NEWDECL. */
2131 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2133 if (DECL_THREAD_LOCAL_P (newdecl))
2134 error ("thread-local declaration of %q+D follows "
2135 "non-thread-local declaration", newdecl);
2136 else
2137 error ("non-thread-local declaration of %q+D follows "
2138 "thread-local declaration", newdecl);
2140 locate_old_decl (olddecl);
2141 return false;
2144 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2145 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2147 error ("redefinition of %q+D", newdecl);
2148 locate_old_decl (olddecl);
2149 return false;
2152 /* Objects declared at file scope: if the first declaration had
2153 external linkage (even if it was an external reference) the
2154 second must have external linkage as well, or the behavior is
2155 undefined. If the first declaration had internal linkage, then
2156 the second must too, or else be an external reference (in which
2157 case the composite declaration still has internal linkage).
2158 As for function declarations, we warn about the static-then-
2159 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2160 if (DECL_FILE_SCOPE_P (newdecl)
2161 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2163 if (DECL_EXTERNAL (newdecl))
2165 if (!DECL_FILE_SCOPE_P (olddecl))
2167 error ("extern declaration of %q+D follows "
2168 "declaration with no linkage", newdecl);
2169 locate_old_decl (olddecl);
2170 return false;
2172 else if (warn_traditional)
2174 warned |= warning (OPT_Wtraditional,
2175 "non-static declaration of %q+D "
2176 "follows static declaration", newdecl);
2179 else
2181 if (TREE_PUBLIC (newdecl))
2182 error ("non-static declaration of %q+D follows "
2183 "static declaration", newdecl);
2184 else
2185 error ("static declaration of %q+D follows "
2186 "non-static declaration", newdecl);
2188 locate_old_decl (olddecl);
2189 return false;
2192 /* Two objects with the same name declared at the same block
2193 scope must both be external references (6.7p3). */
2194 else if (!DECL_FILE_SCOPE_P (newdecl))
2196 if (DECL_EXTERNAL (newdecl))
2198 /* Extern with initializer at block scope, which will
2199 already have received an error. */
2201 else if (DECL_EXTERNAL (olddecl))
2203 error ("declaration of %q+D with no linkage follows "
2204 "extern declaration", newdecl);
2205 locate_old_decl (olddecl);
2207 else
2209 error ("redeclaration of %q+D with no linkage", newdecl);
2210 locate_old_decl (olddecl);
2213 return false;
2216 /* C++ does not permit a decl to appear multiple times at file
2217 scope. */
2218 if (warn_cxx_compat
2219 && DECL_FILE_SCOPE_P (newdecl)
2220 && !DECL_EXTERNAL (newdecl)
2221 && !DECL_EXTERNAL (olddecl))
2222 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2223 OPT_Wc___compat,
2224 ("duplicate declaration of %qD is "
2225 "invalid in C++"),
2226 newdecl);
2229 /* warnings */
2230 /* All decls must agree on a visibility. */
2231 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2232 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2233 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2235 warned |= warning (0, "redeclaration of %q+D with different visibility "
2236 "(old visibility preserved)", newdecl);
2239 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2240 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2241 else /* PARM_DECL, VAR_DECL */
2243 /* Redeclaration of a parameter is a constraint violation (this is
2244 not explicitly stated, but follows from C99 6.7p3 [no more than
2245 one declaration of the same identifier with no linkage in the
2246 same scope, except type tags] and 6.2.2p6 [parameters have no
2247 linkage]). We must check for a forward parameter declaration,
2248 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2249 an extension, the mandatory diagnostic for which is handled by
2250 mark_forward_parm_decls. */
2252 if (TREE_CODE (newdecl) == PARM_DECL
2253 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2255 error ("redefinition of parameter %q+D", newdecl);
2256 locate_old_decl (olddecl);
2257 return false;
2261 /* Optional warning for completely redundant decls. */
2262 if (!warned && !pedwarned
2263 && warn_redundant_decls
2264 /* Don't warn about a function declaration followed by a
2265 definition. */
2266 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2267 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2268 /* Don't warn about redundant redeclarations of builtins. */
2269 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2270 && !DECL_BUILT_IN (newdecl)
2271 && DECL_BUILT_IN (olddecl)
2272 && !C_DECL_DECLARED_BUILTIN (olddecl))
2273 /* Don't warn about an extern followed by a definition. */
2274 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2275 /* Don't warn about forward parameter decls. */
2276 && !(TREE_CODE (newdecl) == PARM_DECL
2277 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2278 /* Don't warn about a variable definition following a declaration. */
2279 && !(VAR_P (newdecl)
2280 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2282 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2283 newdecl);
2286 /* Report location of previous decl/defn. */
2287 if (warned || pedwarned)
2288 locate_old_decl (olddecl);
2290 #undef DECL_EXTERN_INLINE
2292 return retval;
2295 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2296 consistent with OLDDECL, but carries new information. Merge the
2297 new information into OLDDECL. This function issues no
2298 diagnostics. */
2300 static void
2301 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2303 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2304 && DECL_INITIAL (newdecl) != NULL_TREE);
2305 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2306 && prototype_p (TREE_TYPE (newdecl)));
2307 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2308 && prototype_p (TREE_TYPE (olddecl)));
2310 /* For real parm decl following a forward decl, rechain the old decl
2311 in its new location and clear TREE_ASM_WRITTEN (it's not a
2312 forward decl anymore). */
2313 if (TREE_CODE (newdecl) == PARM_DECL
2314 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2316 struct c_binding *b, **here;
2318 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2319 if ((*here)->decl == olddecl)
2320 goto found;
2321 gcc_unreachable ();
2323 found:
2324 b = *here;
2325 *here = b->prev;
2326 b->prev = current_scope->bindings;
2327 current_scope->bindings = b;
2329 TREE_ASM_WRITTEN (olddecl) = 0;
2332 DECL_ATTRIBUTES (newdecl)
2333 = targetm.merge_decl_attributes (olddecl, newdecl);
2335 /* For typedefs use the old type, as the new type's DECL_NAME points
2336 at newdecl, which will be ggc_freed. */
2337 if (TREE_CODE (newdecl) == TYPE_DECL)
2339 /* But NEWTYPE might have an attribute, honor that. */
2340 tree tem = newtype;
2341 newtype = oldtype;
2343 if (TYPE_USER_ALIGN (tem))
2345 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2346 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2347 TYPE_USER_ALIGN (newtype) = true;
2350 /* And remove the new type from the variants list. */
2351 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2353 tree remove = TREE_TYPE (newdecl);
2354 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2355 t = TYPE_NEXT_VARIANT (t))
2356 if (TYPE_NEXT_VARIANT (t) == remove)
2358 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2359 break;
2364 /* Merge the data types specified in the two decls. */
2365 TREE_TYPE (newdecl)
2366 = TREE_TYPE (olddecl)
2367 = composite_type (newtype, oldtype);
2369 /* Lay the type out, unless already done. */
2370 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2372 if (TREE_TYPE (newdecl) != error_mark_node)
2373 layout_type (TREE_TYPE (newdecl));
2374 if (TREE_CODE (newdecl) != FUNCTION_DECL
2375 && TREE_CODE (newdecl) != TYPE_DECL
2376 && TREE_CODE (newdecl) != CONST_DECL)
2377 layout_decl (newdecl, 0);
2379 else
2381 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2382 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2383 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2384 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2385 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2387 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2388 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2390 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2391 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2392 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2393 DECL_WARN_IF_NOT_ALIGN (olddecl));
2396 /* Keep the old rtl since we can safely use it. */
2397 if (HAS_RTL_P (olddecl))
2398 COPY_DECL_RTL (olddecl, newdecl);
2400 /* Merge the type qualifiers. */
2401 if (TREE_READONLY (newdecl))
2402 TREE_READONLY (olddecl) = 1;
2404 if (TREE_THIS_VOLATILE (newdecl))
2405 TREE_THIS_VOLATILE (olddecl) = 1;
2407 /* Merge deprecatedness. */
2408 if (TREE_DEPRECATED (newdecl))
2409 TREE_DEPRECATED (olddecl) = 1;
2411 /* If a decl is in a system header and the other isn't, keep the one on the
2412 system header. Otherwise, keep source location of definition rather than
2413 declaration and of prototype rather than non-prototype unless that
2414 prototype is built-in. */
2415 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2416 && DECL_IN_SYSTEM_HEADER (olddecl)
2417 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2418 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2419 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2420 && DECL_IN_SYSTEM_HEADER (newdecl)
2421 && !DECL_IN_SYSTEM_HEADER (olddecl))
2422 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2423 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2424 && DECL_INITIAL (olddecl) != NULL_TREE)
2425 || (old_is_prototype && !new_is_prototype
2426 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2427 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2429 /* Merge the initialization information. */
2430 if (DECL_INITIAL (newdecl) == NULL_TREE)
2431 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2433 /* Merge the threadprivate attribute. */
2434 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2435 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2437 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2439 /* Copy the assembler name.
2440 Currently, it can only be defined in the prototype. */
2441 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2443 /* Use visibility of whichever declaration had it specified */
2444 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2446 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2447 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2450 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2452 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2453 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2454 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2455 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2456 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2457 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2458 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2459 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2460 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2461 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2462 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2465 /* Merge the storage class information. */
2466 merge_weak (newdecl, olddecl);
2468 /* For functions, static overrides non-static. */
2469 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2471 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2472 /* This is since we don't automatically
2473 copy the attributes of NEWDECL into OLDDECL. */
2474 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2475 /* If this clears `static', clear it in the identifier too. */
2476 if (!TREE_PUBLIC (olddecl))
2477 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2481 /* In c99, 'extern' declaration before (or after) 'inline' means this
2482 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2483 is present. */
2484 if (TREE_CODE (newdecl) == FUNCTION_DECL
2485 && !flag_gnu89_inline
2486 && (DECL_DECLARED_INLINE_P (newdecl)
2487 || DECL_DECLARED_INLINE_P (olddecl))
2488 && (!DECL_DECLARED_INLINE_P (newdecl)
2489 || !DECL_DECLARED_INLINE_P (olddecl)
2490 || !DECL_EXTERNAL (olddecl))
2491 && DECL_EXTERNAL (newdecl)
2492 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2493 && !current_function_decl)
2494 DECL_EXTERNAL (newdecl) = 0;
2496 /* An inline definition following a static declaration is not
2497 DECL_EXTERNAL. */
2498 if (new_is_definition
2499 && (DECL_DECLARED_INLINE_P (newdecl)
2500 || DECL_DECLARED_INLINE_P (olddecl))
2501 && !TREE_PUBLIC (olddecl))
2502 DECL_EXTERNAL (newdecl) = 0;
2504 if (DECL_EXTERNAL (newdecl))
2506 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2507 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2509 /* An extern decl does not override previous storage class. */
2510 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2511 if (!DECL_EXTERNAL (newdecl))
2513 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2514 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2517 else
2519 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2520 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2523 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2525 /* If we're redefining a function previously defined as extern
2526 inline, make sure we emit debug info for the inline before we
2527 throw it away, in case it was inlined into a function that
2528 hasn't been written out yet. */
2529 if (new_is_definition && DECL_INITIAL (olddecl))
2530 /* The new defn must not be inline. */
2531 DECL_UNINLINABLE (newdecl) = 1;
2532 else
2534 /* If either decl says `inline', this fn is inline, unless
2535 its definition was passed already. */
2536 if (DECL_DECLARED_INLINE_P (newdecl)
2537 || DECL_DECLARED_INLINE_P (olddecl))
2538 DECL_DECLARED_INLINE_P (newdecl) = 1;
2540 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2541 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2543 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2544 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2545 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2546 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2549 if (DECL_BUILT_IN (olddecl))
2551 /* If redeclaring a builtin function, it stays built in.
2552 But it gets tagged as having been declared. */
2553 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2554 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2555 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2556 if (new_is_prototype)
2558 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2559 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2561 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2562 switch (fncode)
2564 /* If a compatible prototype of these builtin functions
2565 is seen, assume the runtime implements it with the
2566 expected semantics. */
2567 case BUILT_IN_STPCPY:
2568 if (builtin_decl_explicit_p (fncode))
2569 set_builtin_decl_implicit_p (fncode, true);
2570 break;
2571 default:
2572 if (builtin_decl_explicit_p (fncode))
2573 set_builtin_decl_declared_p (fncode, true);
2574 break;
2577 copy_attributes_to_builtin (newdecl);
2580 else
2581 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2582 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2585 /* Preserve function specific target and optimization options */
2586 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2587 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2588 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2589 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2591 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2592 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2593 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2594 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2596 /* Also preserve various other info from the definition. */
2597 if (!new_is_definition)
2599 tree t;
2600 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2601 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2602 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2603 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2604 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2605 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2606 DECL_CONTEXT (t) = newdecl;
2608 /* See if we've got a function to instantiate from. */
2609 if (DECL_SAVED_TREE (olddecl))
2610 DECL_ABSTRACT_ORIGIN (newdecl)
2611 = DECL_ABSTRACT_ORIGIN (olddecl);
2615 /* Merge the USED information. */
2616 if (TREE_USED (olddecl))
2617 TREE_USED (newdecl) = 1;
2618 else if (TREE_USED (newdecl))
2619 TREE_USED (olddecl) = 1;
2620 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2621 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2622 if (DECL_PRESERVE_P (olddecl))
2623 DECL_PRESERVE_P (newdecl) = 1;
2624 else if (DECL_PRESERVE_P (newdecl))
2625 DECL_PRESERVE_P (olddecl) = 1;
2627 /* Merge DECL_COMMON */
2628 if (VAR_P (olddecl) && VAR_P (newdecl)
2629 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2630 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2631 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2633 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2634 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2635 DECL_ARGUMENTS (if appropriate). */
2637 unsigned olddecl_uid = DECL_UID (olddecl);
2638 tree olddecl_context = DECL_CONTEXT (olddecl);
2639 tree olddecl_arguments = NULL;
2640 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2641 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2643 memcpy ((char *) olddecl + sizeof (struct tree_common),
2644 (char *) newdecl + sizeof (struct tree_common),
2645 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2646 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2647 switch (TREE_CODE (olddecl))
2649 case FUNCTION_DECL:
2650 case VAR_DECL:
2652 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2654 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2655 (char *) newdecl + sizeof (struct tree_decl_common),
2656 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2657 olddecl->decl_with_vis.symtab_node = snode;
2659 if ((DECL_EXTERNAL (olddecl)
2660 || TREE_PUBLIC (olddecl)
2661 || TREE_STATIC (olddecl))
2662 && DECL_SECTION_NAME (newdecl) != NULL)
2663 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2665 /* This isn't quite correct for something like
2666 int __thread x attribute ((tls_model ("local-exec")));
2667 extern int __thread x;
2668 as we'll lose the "local-exec" model. */
2669 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2670 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2671 break;
2674 case FIELD_DECL:
2675 case PARM_DECL:
2676 case LABEL_DECL:
2677 case RESULT_DECL:
2678 case CONST_DECL:
2679 case TYPE_DECL:
2680 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2681 (char *) newdecl + sizeof (struct tree_decl_common),
2682 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2683 break;
2685 default:
2687 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2688 (char *) newdecl + sizeof (struct tree_decl_common),
2689 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2691 DECL_UID (olddecl) = olddecl_uid;
2692 DECL_CONTEXT (olddecl) = olddecl_context;
2693 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2694 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2697 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2698 so that encode_section_info has a chance to look at the new decl
2699 flags and attributes. */
2700 if (DECL_RTL_SET_P (olddecl)
2701 && (TREE_CODE (olddecl) == FUNCTION_DECL
2702 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2703 make_decl_rtl (olddecl);
2706 /* Handle when a new declaration NEWDECL has the same name as an old
2707 one OLDDECL in the same binding contour. Prints an error message
2708 if appropriate.
2710 If safely possible, alter OLDDECL to look like NEWDECL, and return
2711 true. Otherwise, return false. */
2713 static bool
2714 duplicate_decls (tree newdecl, tree olddecl)
2716 tree newtype = NULL, oldtype = NULL;
2718 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2720 /* Avoid `unused variable' and other warnings for OLDDECL. */
2721 TREE_NO_WARNING (olddecl) = 1;
2722 return false;
2725 merge_decls (newdecl, olddecl, newtype, oldtype);
2727 /* The NEWDECL will no longer be needed.
2729 Before releasing the node, be sure to remove function from symbol
2730 table that might have been inserted there to record comdat group.
2731 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2732 structure is shared in between NEWDECL and OLDECL. */
2733 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2734 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2735 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2737 struct symtab_node *snode = symtab_node::get (newdecl);
2738 if (snode)
2739 snode->remove ();
2741 ggc_free (newdecl);
2742 return true;
2746 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2747 static void
2748 warn_if_shadowing (tree new_decl)
2750 struct c_binding *b;
2752 /* Shadow warnings wanted? */
2753 if (!(warn_shadow
2754 || warn_shadow_local
2755 || warn_shadow_compatible_local)
2756 /* No shadow warnings for internally generated vars. */
2757 || DECL_IS_BUILTIN (new_decl)
2758 /* No shadow warnings for vars made for inlining. */
2759 || DECL_FROM_INLINE (new_decl))
2760 return;
2762 /* Is anything being shadowed? Invisible decls do not count. */
2763 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2764 if (b->decl && b->decl != new_decl && !b->invisible
2765 && (b->decl == error_mark_node
2766 || diagnostic_report_warnings_p (global_dc,
2767 DECL_SOURCE_LOCATION (b->decl))))
2769 tree old_decl = b->decl;
2770 bool warned = false;
2772 if (old_decl == error_mark_node)
2774 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2775 "non-variable", new_decl);
2776 break;
2778 else if (TREE_CODE (old_decl) == PARM_DECL)
2780 enum opt_code warning_code;
2782 /* If '-Wshadow=compatible-local' is specified without other
2783 -Wshadow= flags, we will warn only when the types of the
2784 shadowing variable (i.e. new_decl) and the shadowed variable
2785 (old_decl) are compatible. */
2786 if (warn_shadow)
2787 warning_code = OPT_Wshadow;
2788 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2789 warning_code = OPT_Wshadow_compatible_local;
2790 else
2791 warning_code = OPT_Wshadow_local;
2792 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2793 "declaration of %qD shadows a parameter",
2794 new_decl);
2796 else if (DECL_FILE_SCOPE_P (old_decl))
2798 /* Do not warn if a variable shadows a function, unless
2799 the variable is a function or a pointer-to-function. */
2800 if (TREE_CODE (old_decl) == FUNCTION_DECL
2801 && TREE_CODE (new_decl) != FUNCTION_DECL
2802 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2803 continue;
2805 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2806 "declaration of %qD shadows a global "
2807 "declaration",
2808 new_decl);
2810 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2811 && DECL_BUILT_IN (old_decl))
2813 warning (OPT_Wshadow, "declaration of %q+D shadows "
2814 "a built-in function", new_decl);
2815 break;
2817 else
2819 enum opt_code warning_code;
2821 /* If '-Wshadow=compatible-local' is specified without other
2822 -Wshadow= flags, we will warn only when the types of the
2823 shadowing variable (i.e. new_decl) and the shadowed variable
2824 (old_decl) are compatible. */
2825 if (warn_shadow)
2826 warning_code = OPT_Wshadow;
2827 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2828 warning_code = OPT_Wshadow_compatible_local;
2829 else
2830 warning_code = OPT_Wshadow_local;
2831 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2832 "declaration of %qD shadows a previous local",
2833 new_decl);
2836 if (warned)
2837 inform (DECL_SOURCE_LOCATION (old_decl),
2838 "shadowed declaration is here");
2840 break;
2844 /* Record a decl-node X as belonging to the current lexical scope.
2845 Check for errors (such as an incompatible declaration for the same
2846 name already seen in the same scope).
2848 Returns either X or an old decl for the same name.
2849 If an old decl is returned, it may have been smashed
2850 to agree with what X says. */
2852 tree
2853 pushdecl (tree x)
2855 tree name = DECL_NAME (x);
2856 struct c_scope *scope = current_scope;
2857 struct c_binding *b;
2858 bool nested = false;
2859 location_t locus = DECL_SOURCE_LOCATION (x);
2861 /* Must set DECL_CONTEXT for everything not at file scope or
2862 DECL_FILE_SCOPE_P won't work. Local externs don't count
2863 unless they have initializers (which generate code). */
2864 if (current_function_decl
2865 && (!VAR_OR_FUNCTION_DECL_P (x)
2866 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2867 DECL_CONTEXT (x) = current_function_decl;
2869 /* Anonymous decls are just inserted in the scope. */
2870 if (!name)
2872 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2873 locus);
2874 return x;
2877 /* First, see if there is another declaration with the same name in
2878 the current scope. If there is, duplicate_decls may do all the
2879 work for us. If duplicate_decls returns false, that indicates
2880 two incompatible decls in the same scope; we are to silently
2881 replace the old one (duplicate_decls has issued all appropriate
2882 diagnostics). In particular, we should not consider possible
2883 duplicates in the external scope, or shadowing. */
2884 b = I_SYMBOL_BINDING (name);
2885 if (b && B_IN_SCOPE (b, scope))
2887 struct c_binding *b_ext, *b_use;
2888 tree type = TREE_TYPE (x);
2889 tree visdecl = b->decl;
2890 tree vistype = TREE_TYPE (visdecl);
2891 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2892 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2893 b->inner_comp = false;
2894 b_use = b;
2895 b_ext = b;
2896 /* If this is an external linkage declaration, we should check
2897 for compatibility with the type in the external scope before
2898 setting the type at this scope based on the visible
2899 information only. */
2900 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2902 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2903 b_ext = b_ext->shadowed;
2904 if (b_ext)
2906 b_use = b_ext;
2907 if (b_use->u.type)
2908 TREE_TYPE (b_use->decl) = b_use->u.type;
2911 if (duplicate_decls (x, b_use->decl))
2913 if (b_use != b)
2915 /* Save the updated type in the external scope and
2916 restore the proper type for this scope. */
2917 tree thistype;
2918 if (comptypes (vistype, type))
2919 thistype = composite_type (vistype, type);
2920 else
2921 thistype = TREE_TYPE (b_use->decl);
2922 b_use->u.type = TREE_TYPE (b_use->decl);
2923 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2924 && DECL_BUILT_IN (b_use->decl))
2925 thistype
2926 = build_type_attribute_variant (thistype,
2927 TYPE_ATTRIBUTES
2928 (b_use->u.type));
2929 TREE_TYPE (b_use->decl) = thistype;
2931 return b_use->decl;
2933 else
2934 goto skip_external_and_shadow_checks;
2937 /* All declarations with external linkage, and all external
2938 references, go in the external scope, no matter what scope is
2939 current. However, the binding in that scope is ignored for
2940 purposes of normal name lookup. A separate binding structure is
2941 created in the requested scope; this governs the normal
2942 visibility of the symbol.
2944 The binding in the externals scope is used exclusively for
2945 detecting duplicate declarations of the same object, no matter
2946 what scope they are in; this is what we do here. (C99 6.2.7p2:
2947 All declarations that refer to the same object or function shall
2948 have compatible type; otherwise, the behavior is undefined.) */
2949 if (DECL_EXTERNAL (x) || scope == file_scope)
2951 tree type = TREE_TYPE (x);
2952 tree vistype = NULL_TREE;
2953 tree visdecl = NULL_TREE;
2954 bool type_saved = false;
2955 if (b && !B_IN_EXTERNAL_SCOPE (b)
2956 && VAR_OR_FUNCTION_DECL_P (b->decl)
2957 && DECL_FILE_SCOPE_P (b->decl))
2959 visdecl = b->decl;
2960 vistype = TREE_TYPE (visdecl);
2962 if (scope != file_scope
2963 && !DECL_IN_SYSTEM_HEADER (x))
2964 warning_at (locus, OPT_Wnested_externs,
2965 "nested extern declaration of %qD", x);
2967 while (b && !B_IN_EXTERNAL_SCOPE (b))
2969 /* If this decl might be modified, save its type. This is
2970 done here rather than when the decl is first bound
2971 because the type may change after first binding, through
2972 being completed or through attributes being added. If we
2973 encounter multiple such decls, only the first should have
2974 its type saved; the others will already have had their
2975 proper types saved and the types will not have changed as
2976 their scopes will not have been re-entered. */
2977 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2979 b->u.type = TREE_TYPE (b->decl);
2980 type_saved = true;
2982 if (B_IN_FILE_SCOPE (b)
2983 && VAR_P (b->decl)
2984 && TREE_STATIC (b->decl)
2985 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2986 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2987 && TREE_CODE (type) == ARRAY_TYPE
2988 && TYPE_DOMAIN (type)
2989 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2990 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2992 /* Array type completed in inner scope, which should be
2993 diagnosed if the completion does not have size 1 and
2994 it does not get completed in the file scope. */
2995 b->inner_comp = true;
2997 b = b->shadowed;
3000 /* If a matching external declaration has been found, set its
3001 type to the composite of all the types of that declaration.
3002 After the consistency checks, it will be reset to the
3003 composite of the visible types only. */
3004 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3005 && b->u.type)
3006 TREE_TYPE (b->decl) = b->u.type;
3008 /* The point of the same_translation_unit_p check here is,
3009 we want to detect a duplicate decl for a construct like
3010 foo() { extern bar(); } ... static bar(); but not if
3011 they are in different translation units. In any case,
3012 the static does not go in the externals scope. */
3013 if (b
3014 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3015 && duplicate_decls (x, b->decl))
3017 tree thistype;
3018 if (vistype)
3020 if (comptypes (vistype, type))
3021 thistype = composite_type (vistype, type);
3022 else
3023 thistype = TREE_TYPE (b->decl);
3025 else
3026 thistype = type;
3027 b->u.type = TREE_TYPE (b->decl);
3028 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3029 thistype
3030 = build_type_attribute_variant (thistype,
3031 TYPE_ATTRIBUTES (b->u.type));
3032 TREE_TYPE (b->decl) = thistype;
3033 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3034 locus);
3035 return b->decl;
3037 else if (TREE_PUBLIC (x))
3039 if (visdecl && !b && duplicate_decls (x, visdecl))
3041 /* An external declaration at block scope referring to a
3042 visible entity with internal linkage. The composite
3043 type will already be correct for this scope, so we
3044 just need to fall through to make the declaration in
3045 this scope. */
3046 nested = true;
3047 x = visdecl;
3049 else
3051 bind (name, x, external_scope, /*invisible=*/true,
3052 /*nested=*/false, locus);
3053 nested = true;
3058 if (TREE_CODE (x) != PARM_DECL)
3059 warn_if_shadowing (x);
3061 skip_external_and_shadow_checks:
3062 if (TREE_CODE (x) == TYPE_DECL)
3064 /* So this is a typedef, set its underlying type. */
3065 set_underlying_type (x);
3067 /* If X is a typedef defined in the current function, record it
3068 for the purpose of implementing the -Wunused-local-typedefs
3069 warning. */
3070 record_locally_defined_typedef (x);
3073 bind (name, x, scope, /*invisible=*/false, nested, locus);
3075 /* If x's type is incomplete because it's based on a
3076 structure or union which has not yet been fully declared,
3077 attach it to that structure or union type, so we can go
3078 back and complete the variable declaration later, if the
3079 structure or union gets fully declared.
3081 If the input is erroneous, we can have error_mark in the type
3082 slot (e.g. "f(void a, ...)") - that doesn't count as an
3083 incomplete type. */
3084 if (TREE_TYPE (x) != error_mark_node
3085 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3087 tree element = TREE_TYPE (x);
3089 while (TREE_CODE (element) == ARRAY_TYPE)
3090 element = TREE_TYPE (element);
3091 element = TYPE_MAIN_VARIANT (element);
3093 if (RECORD_OR_UNION_TYPE_P (element)
3094 && (TREE_CODE (x) != TYPE_DECL
3095 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3096 && !COMPLETE_TYPE_P (element))
3097 C_TYPE_INCOMPLETE_VARS (element)
3098 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3100 return x;
3104 /* Issue a warning about implicit function declaration. ID is the function
3105 identifier, OLDDECL is a declaration of the function in a different scope,
3106 or NULL_TREE. */
3108 static void
3109 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3111 if (!warn_implicit_function_declaration)
3112 return;
3114 bool warned;
3115 name_hint hint;
3116 if (!olddecl)
3117 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3119 if (flag_isoc99)
3121 if (hint)
3123 gcc_rich_location richloc (loc);
3124 richloc.add_fixit_replace (hint.suggestion ());
3125 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3126 "implicit declaration of function %qE;"
3127 " did you mean %qs?",
3128 id, hint.suggestion ());
3130 else
3131 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3132 "implicit declaration of function %qE", id);
3134 else if (hint)
3136 gcc_rich_location richloc (loc);
3137 richloc.add_fixit_replace (hint.suggestion ());
3138 warned = warning_at
3139 (&richloc, OPT_Wimplicit_function_declaration,
3140 G_("implicit declaration of function %qE; did you mean %qs?"),
3141 id, hint.suggestion ());
3143 else
3144 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3145 G_("implicit declaration of function %qE"), id);
3147 if (olddecl && warned)
3148 locate_old_decl (olddecl);
3150 if (!warned)
3151 hint.suppress ();
3154 /* This function represents mapping of a function code FCODE
3155 to its respective header. */
3157 static const char *
3158 header_for_builtin_fn (enum built_in_function fcode)
3160 switch (fcode)
3162 CASE_FLT_FN (BUILT_IN_ACOS):
3163 CASE_FLT_FN (BUILT_IN_ACOSH):
3164 CASE_FLT_FN (BUILT_IN_ASIN):
3165 CASE_FLT_FN (BUILT_IN_ASINH):
3166 CASE_FLT_FN (BUILT_IN_ATAN):
3167 CASE_FLT_FN (BUILT_IN_ATANH):
3168 CASE_FLT_FN (BUILT_IN_ATAN2):
3169 CASE_FLT_FN (BUILT_IN_CBRT):
3170 CASE_FLT_FN (BUILT_IN_CEIL):
3171 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3172 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3173 CASE_FLT_FN (BUILT_IN_COS):
3174 CASE_FLT_FN (BUILT_IN_COSH):
3175 CASE_FLT_FN (BUILT_IN_ERF):
3176 CASE_FLT_FN (BUILT_IN_ERFC):
3177 CASE_FLT_FN (BUILT_IN_EXP):
3178 CASE_FLT_FN (BUILT_IN_EXP2):
3179 CASE_FLT_FN (BUILT_IN_EXPM1):
3180 CASE_FLT_FN (BUILT_IN_FABS):
3181 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3182 CASE_FLT_FN (BUILT_IN_FDIM):
3183 CASE_FLT_FN (BUILT_IN_FLOOR):
3184 CASE_FLT_FN (BUILT_IN_FMA):
3185 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3186 CASE_FLT_FN (BUILT_IN_FMAX):
3187 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3188 CASE_FLT_FN (BUILT_IN_FMIN):
3189 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3190 CASE_FLT_FN (BUILT_IN_FMOD):
3191 CASE_FLT_FN (BUILT_IN_FREXP):
3192 CASE_FLT_FN (BUILT_IN_HYPOT):
3193 CASE_FLT_FN (BUILT_IN_ILOGB):
3194 CASE_FLT_FN (BUILT_IN_LDEXP):
3195 CASE_FLT_FN (BUILT_IN_LGAMMA):
3196 CASE_FLT_FN (BUILT_IN_LLRINT):
3197 CASE_FLT_FN (BUILT_IN_LLROUND):
3198 CASE_FLT_FN (BUILT_IN_LOG):
3199 CASE_FLT_FN (BUILT_IN_LOG10):
3200 CASE_FLT_FN (BUILT_IN_LOG1P):
3201 CASE_FLT_FN (BUILT_IN_LOG2):
3202 CASE_FLT_FN (BUILT_IN_LOGB):
3203 CASE_FLT_FN (BUILT_IN_LRINT):
3204 CASE_FLT_FN (BUILT_IN_LROUND):
3205 CASE_FLT_FN (BUILT_IN_MODF):
3206 CASE_FLT_FN (BUILT_IN_NAN):
3207 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3208 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3209 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3210 CASE_FLT_FN (BUILT_IN_POW):
3211 CASE_FLT_FN (BUILT_IN_REMAINDER):
3212 CASE_FLT_FN (BUILT_IN_REMQUO):
3213 CASE_FLT_FN (BUILT_IN_RINT):
3214 CASE_FLT_FN (BUILT_IN_ROUND):
3215 CASE_FLT_FN (BUILT_IN_SCALBLN):
3216 CASE_FLT_FN (BUILT_IN_SCALBN):
3217 CASE_FLT_FN (BUILT_IN_SIN):
3218 CASE_FLT_FN (BUILT_IN_SINH):
3219 CASE_FLT_FN (BUILT_IN_SINCOS):
3220 CASE_FLT_FN (BUILT_IN_SQRT):
3221 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3222 CASE_FLT_FN (BUILT_IN_TAN):
3223 CASE_FLT_FN (BUILT_IN_TANH):
3224 CASE_FLT_FN (BUILT_IN_TGAMMA):
3225 CASE_FLT_FN (BUILT_IN_TRUNC):
3226 case BUILT_IN_ISINF:
3227 case BUILT_IN_ISNAN:
3228 return "<math.h>";
3229 CASE_FLT_FN (BUILT_IN_CABS):
3230 CASE_FLT_FN (BUILT_IN_CACOS):
3231 CASE_FLT_FN (BUILT_IN_CACOSH):
3232 CASE_FLT_FN (BUILT_IN_CARG):
3233 CASE_FLT_FN (BUILT_IN_CASIN):
3234 CASE_FLT_FN (BUILT_IN_CASINH):
3235 CASE_FLT_FN (BUILT_IN_CATAN):
3236 CASE_FLT_FN (BUILT_IN_CATANH):
3237 CASE_FLT_FN (BUILT_IN_CCOS):
3238 CASE_FLT_FN (BUILT_IN_CCOSH):
3239 CASE_FLT_FN (BUILT_IN_CEXP):
3240 CASE_FLT_FN (BUILT_IN_CIMAG):
3241 CASE_FLT_FN (BUILT_IN_CLOG):
3242 CASE_FLT_FN (BUILT_IN_CONJ):
3243 CASE_FLT_FN (BUILT_IN_CPOW):
3244 CASE_FLT_FN (BUILT_IN_CPROJ):
3245 CASE_FLT_FN (BUILT_IN_CREAL):
3246 CASE_FLT_FN (BUILT_IN_CSIN):
3247 CASE_FLT_FN (BUILT_IN_CSINH):
3248 CASE_FLT_FN (BUILT_IN_CSQRT):
3249 CASE_FLT_FN (BUILT_IN_CTAN):
3250 CASE_FLT_FN (BUILT_IN_CTANH):
3251 return "<complex.h>";
3252 case BUILT_IN_MEMCHR:
3253 case BUILT_IN_MEMCMP:
3254 case BUILT_IN_MEMCPY:
3255 case BUILT_IN_MEMMOVE:
3256 case BUILT_IN_MEMSET:
3257 case BUILT_IN_STRCAT:
3258 case BUILT_IN_STRCHR:
3259 case BUILT_IN_STRCMP:
3260 case BUILT_IN_STRCPY:
3261 case BUILT_IN_STRCSPN:
3262 case BUILT_IN_STRLEN:
3263 case BUILT_IN_STRNCAT:
3264 case BUILT_IN_STRNCMP:
3265 case BUILT_IN_STRNCPY:
3266 case BUILT_IN_STRPBRK:
3267 case BUILT_IN_STRRCHR:
3268 case BUILT_IN_STRSPN:
3269 case BUILT_IN_STRSTR:
3270 return "<string.h>";
3271 case BUILT_IN_FPRINTF:
3272 case BUILT_IN_PUTC:
3273 case BUILT_IN_FPUTC:
3274 case BUILT_IN_FPUTS:
3275 case BUILT_IN_FSCANF:
3276 case BUILT_IN_FWRITE:
3277 case BUILT_IN_PRINTF:
3278 case BUILT_IN_PUTCHAR:
3279 case BUILT_IN_PUTS:
3280 case BUILT_IN_SCANF:
3281 case BUILT_IN_SNPRINTF:
3282 case BUILT_IN_SPRINTF:
3283 case BUILT_IN_SSCANF:
3284 case BUILT_IN_VFPRINTF:
3285 case BUILT_IN_VFSCANF:
3286 case BUILT_IN_VPRINTF:
3287 case BUILT_IN_VSCANF:
3288 case BUILT_IN_VSNPRINTF:
3289 case BUILT_IN_VSPRINTF:
3290 case BUILT_IN_VSSCANF:
3291 return "<stdio.h>";
3292 case BUILT_IN_ISALNUM:
3293 case BUILT_IN_ISALPHA:
3294 case BUILT_IN_ISBLANK:
3295 case BUILT_IN_ISCNTRL:
3296 case BUILT_IN_ISDIGIT:
3297 case BUILT_IN_ISGRAPH:
3298 case BUILT_IN_ISLOWER:
3299 case BUILT_IN_ISPRINT:
3300 case BUILT_IN_ISPUNCT:
3301 case BUILT_IN_ISSPACE:
3302 case BUILT_IN_ISUPPER:
3303 case BUILT_IN_ISXDIGIT:
3304 case BUILT_IN_TOLOWER:
3305 case BUILT_IN_TOUPPER:
3306 return "<ctype.h>";
3307 case BUILT_IN_ISWALNUM:
3308 case BUILT_IN_ISWALPHA:
3309 case BUILT_IN_ISWBLANK:
3310 case BUILT_IN_ISWCNTRL:
3311 case BUILT_IN_ISWDIGIT:
3312 case BUILT_IN_ISWGRAPH:
3313 case BUILT_IN_ISWLOWER:
3314 case BUILT_IN_ISWPRINT:
3315 case BUILT_IN_ISWPUNCT:
3316 case BUILT_IN_ISWSPACE:
3317 case BUILT_IN_ISWUPPER:
3318 case BUILT_IN_ISWXDIGIT:
3319 case BUILT_IN_TOWLOWER:
3320 case BUILT_IN_TOWUPPER:
3321 return "<wctype.h>";
3322 case BUILT_IN_ABORT:
3323 case BUILT_IN_ABS:
3324 case BUILT_IN_CALLOC:
3325 case BUILT_IN_EXIT:
3326 case BUILT_IN_FREE:
3327 case BUILT_IN_LABS:
3328 case BUILT_IN_LLABS:
3329 case BUILT_IN_MALLOC:
3330 case BUILT_IN_REALLOC:
3331 case BUILT_IN__EXIT2:
3332 case BUILT_IN_ALIGNED_ALLOC:
3333 return "<stdlib.h>";
3334 case BUILT_IN_IMAXABS:
3335 return "<inttypes.h>";
3336 case BUILT_IN_STRFTIME:
3337 return "<time.h>";
3338 default:
3339 return NULL;
3343 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3344 function of type int (). */
3346 tree
3347 implicitly_declare (location_t loc, tree functionid)
3349 struct c_binding *b;
3350 tree decl = NULL_TREE;
3351 tree asmspec_tree;
3353 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3355 if (B_IN_SCOPE (b, external_scope))
3357 decl = b->decl;
3358 break;
3362 if (decl)
3364 if (TREE_CODE (decl) != FUNCTION_DECL)
3365 return decl;
3367 /* FIXME: Objective-C has weird not-really-builtin functions
3368 which are supposed to be visible automatically. They wind up
3369 in the external scope because they're pushed before the file
3370 scope gets created. Catch this here and rebind them into the
3371 file scope. */
3372 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3374 bind (functionid, decl, file_scope,
3375 /*invisible=*/false, /*nested=*/true,
3376 DECL_SOURCE_LOCATION (decl));
3377 return decl;
3379 else
3381 tree newtype = default_function_type;
3382 if (b->u.type)
3383 TREE_TYPE (decl) = b->u.type;
3384 /* Implicit declaration of a function already declared
3385 (somehow) in a different scope, or as a built-in.
3386 If this is the first time this has happened, warn;
3387 then recycle the old declaration but with the new type. */
3388 if (!C_DECL_IMPLICIT (decl))
3390 implicit_decl_warning (loc, functionid, decl);
3391 C_DECL_IMPLICIT (decl) = 1;
3393 if (DECL_BUILT_IN (decl))
3395 newtype = build_type_attribute_variant (newtype,
3396 TYPE_ATTRIBUTES
3397 (TREE_TYPE (decl)));
3398 if (!comptypes (newtype, TREE_TYPE (decl)))
3400 bool warned = warning_at (loc, 0, "incompatible implicit "
3401 "declaration of built-in "
3402 "function %qD", decl);
3403 /* See if we can hint which header to include. */
3404 const char *header
3405 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3406 if (header != NULL && warned)
3408 rich_location richloc (line_table, loc);
3409 maybe_add_include_fixit (&richloc, header);
3410 inform (&richloc,
3411 "include %qs or provide a declaration of %qD",
3412 header, decl);
3414 newtype = TREE_TYPE (decl);
3417 else
3419 if (!comptypes (newtype, TREE_TYPE (decl)))
3421 error_at (loc, "incompatible implicit declaration of "
3422 "function %qD", decl);
3423 locate_old_decl (decl);
3426 b->u.type = TREE_TYPE (decl);
3427 TREE_TYPE (decl) = newtype;
3428 bind (functionid, decl, current_scope,
3429 /*invisible=*/false, /*nested=*/true,
3430 DECL_SOURCE_LOCATION (decl));
3431 return decl;
3435 /* Not seen before. */
3436 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3437 DECL_EXTERNAL (decl) = 1;
3438 TREE_PUBLIC (decl) = 1;
3439 C_DECL_IMPLICIT (decl) = 1;
3440 implicit_decl_warning (loc, functionid, 0);
3441 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3442 if (asmspec_tree)
3443 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3445 /* C89 says implicit declarations are in the innermost block.
3446 So we record the decl in the standard fashion. */
3447 decl = pushdecl (decl);
3449 /* No need to call objc_check_decl here - it's a function type. */
3450 rest_of_decl_compilation (decl, 0, 0);
3452 /* Write a record describing this implicit function declaration
3453 to the prototypes file (if requested). */
3454 gen_aux_info_record (decl, 0, 1, 0);
3456 /* Possibly apply some default attributes to this implicit declaration. */
3457 decl_attributes (&decl, NULL_TREE, 0);
3459 return decl;
3462 /* Issue an error message for a reference to an undeclared variable
3463 ID, including a reference to a builtin outside of function-call
3464 context. Establish a binding of the identifier to error_mark_node
3465 in an appropriate scope, which will suppress further errors for the
3466 same identifier. The error message should be given location LOC. */
3467 void
3468 undeclared_variable (location_t loc, tree id)
3470 static bool already = false;
3471 struct c_scope *scope;
3473 if (current_function_decl == NULL_TREE)
3475 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3476 if (guessed_id)
3478 gcc_rich_location richloc (loc);
3479 richloc.add_fixit_replace (guessed_id.suggestion ());
3480 error_at (&richloc,
3481 "%qE undeclared here (not in a function);"
3482 " did you mean %qs?",
3483 id, guessed_id.suggestion ());
3485 else
3486 error_at (loc, "%qE undeclared here (not in a function)", id);
3487 scope = current_scope;
3489 else
3491 if (!objc_diagnose_private_ivar (id))
3493 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3494 if (guessed_id)
3496 gcc_rich_location richloc (loc);
3497 richloc.add_fixit_replace (guessed_id.suggestion ());
3498 error_at (&richloc,
3499 "%qE undeclared (first use in this function);"
3500 " did you mean %qs?",
3501 id, guessed_id.suggestion ());
3503 else
3504 error_at (loc, "%qE undeclared (first use in this function)", id);
3506 if (!already)
3508 inform (loc, "each undeclared identifier is reported only"
3509 " once for each function it appears in");
3510 already = true;
3513 /* If we are parsing old-style parameter decls, current_function_decl
3514 will be nonnull but current_function_scope will be null. */
3515 scope = current_function_scope ? current_function_scope : current_scope;
3517 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3518 UNKNOWN_LOCATION);
3521 /* Subroutine of lookup_label, declare_label, define_label: construct a
3522 LABEL_DECL with all the proper frills. Also create a struct
3523 c_label_vars initialized for the current scope. */
3525 static tree
3526 make_label (location_t location, tree name, bool defining,
3527 struct c_label_vars **p_label_vars)
3529 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3530 DECL_CONTEXT (label) = current_function_decl;
3531 SET_DECL_MODE (label, VOIDmode);
3533 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3534 label_vars->shadowed = NULL;
3535 set_spot_bindings (&label_vars->label_bindings, defining);
3536 label_vars->decls_in_scope = make_tree_vector ();
3537 label_vars->gotos = NULL;
3538 *p_label_vars = label_vars;
3540 return label;
3543 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3544 Create one if none exists so far for the current function.
3545 This is called when a label is used in a goto expression or
3546 has its address taken. */
3548 tree
3549 lookup_label (tree name)
3551 tree label;
3552 struct c_label_vars *label_vars;
3554 if (current_function_scope == 0)
3556 error ("label %qE referenced outside of any function", name);
3557 return NULL_TREE;
3560 /* Use a label already defined or ref'd with this name, but not if
3561 it is inherited from a containing function and wasn't declared
3562 using __label__. */
3563 label = I_LABEL_DECL (name);
3564 if (label && (DECL_CONTEXT (label) == current_function_decl
3565 || C_DECLARED_LABEL_FLAG (label)))
3567 /* If the label has only been declared, update its apparent
3568 location to point here, for better diagnostics if it
3569 turns out not to have been defined. */
3570 if (DECL_INITIAL (label) == NULL_TREE)
3571 DECL_SOURCE_LOCATION (label) = input_location;
3572 return label;
3575 /* No label binding for that identifier; make one. */
3576 label = make_label (input_location, name, false, &label_vars);
3578 /* Ordinary labels go in the current function scope. */
3579 bind_label (name, label, current_function_scope, label_vars);
3581 return label;
3584 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3585 to LABEL. */
3587 static void
3588 warn_about_goto (location_t goto_loc, tree label, tree decl)
3590 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3591 error_at (goto_loc,
3592 "jump into scope of identifier with variably modified type");
3593 else
3594 warning_at (goto_loc, OPT_Wjump_misses_init,
3595 "jump skips variable initialization");
3596 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3597 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3600 /* Look up a label because of a goto statement. This is like
3601 lookup_label, but also issues any appropriate warnings. */
3603 tree
3604 lookup_label_for_goto (location_t loc, tree name)
3606 tree label;
3607 struct c_label_vars *label_vars;
3608 unsigned int ix;
3609 tree decl;
3611 label = lookup_label (name);
3612 if (label == NULL_TREE)
3613 return NULL_TREE;
3615 /* If we are jumping to a different function, we can't issue any
3616 useful warnings. */
3617 if (DECL_CONTEXT (label) != current_function_decl)
3619 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3620 return label;
3623 label_vars = I_LABEL_BINDING (name)->u.label;
3625 /* If the label has not yet been defined, then push this goto on a
3626 list for possible later warnings. */
3627 if (label_vars->label_bindings.scope == NULL)
3629 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3631 g->loc = loc;
3632 set_spot_bindings (&g->goto_bindings, true);
3633 vec_safe_push (label_vars->gotos, g);
3634 return label;
3637 /* If there are any decls in label_vars->decls_in_scope, then this
3638 goto has missed the declaration of the decl. This happens for a
3639 case like
3640 int i = 1;
3641 lab:
3643 goto lab;
3644 Issue a warning or error. */
3645 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3646 warn_about_goto (loc, label, decl);
3648 if (label_vars->label_bindings.left_stmt_expr)
3650 error_at (loc, "jump into statement expression");
3651 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3654 return label;
3657 /* Make a label named NAME in the current function, shadowing silently
3658 any that may be inherited from containing functions or containing
3659 scopes. This is called for __label__ declarations. */
3661 tree
3662 declare_label (tree name)
3664 struct c_binding *b = I_LABEL_BINDING (name);
3665 tree label;
3666 struct c_label_vars *label_vars;
3668 /* Check to make sure that the label hasn't already been declared
3669 at this scope */
3670 if (b && B_IN_CURRENT_SCOPE (b))
3672 error ("duplicate label declaration %qE", name);
3673 locate_old_decl (b->decl);
3675 /* Just use the previous declaration. */
3676 return b->decl;
3679 label = make_label (input_location, name, false, &label_vars);
3680 C_DECLARED_LABEL_FLAG (label) = 1;
3682 /* Declared labels go in the current scope. */
3683 bind_label (name, label, current_scope, label_vars);
3685 return label;
3688 /* When we define a label, issue any appropriate warnings if there are
3689 any gotos earlier in the function which jump to this label. */
3691 static void
3692 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3694 unsigned int ix;
3695 struct c_goto_bindings *g;
3697 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3699 struct c_binding *b;
3700 struct c_scope *scope;
3702 /* We have a goto to this label. The goto is going forward. In
3703 g->scope, the goto is going to skip any binding which was
3704 defined after g->bindings_in_scope. */
3705 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3707 for (b = g->goto_bindings.scope->bindings;
3708 b != g->goto_bindings.bindings_in_scope;
3709 b = b->prev)
3711 if (decl_jump_unsafe (b->decl))
3712 warn_about_goto (g->loc, label, b->decl);
3716 /* We also need to warn about decls defined in any scopes
3717 between the scope of the label and the scope of the goto. */
3718 for (scope = label_vars->label_bindings.scope;
3719 scope != g->goto_bindings.scope;
3720 scope = scope->outer)
3722 gcc_assert (scope != NULL);
3723 if (scope->has_jump_unsafe_decl)
3725 if (scope == label_vars->label_bindings.scope)
3726 b = label_vars->label_bindings.bindings_in_scope;
3727 else
3728 b = scope->bindings;
3729 for (; b != NULL; b = b->prev)
3731 if (decl_jump_unsafe (b->decl))
3732 warn_about_goto (g->loc, label, b->decl);
3737 if (g->goto_bindings.stmt_exprs > 0)
3739 error_at (g->loc, "jump into statement expression");
3740 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3741 label);
3745 /* Now that the label is defined, we will issue warnings about
3746 subsequent gotos to this label when we see them. */
3747 vec_safe_truncate (label_vars->gotos, 0);
3748 label_vars->gotos = NULL;
3751 /* Define a label, specifying the location in the source file.
3752 Return the LABEL_DECL node for the label, if the definition is valid.
3753 Otherwise return NULL_TREE. */
3755 tree
3756 define_label (location_t location, tree name)
3758 /* Find any preexisting label with this name. It is an error
3759 if that label has already been defined in this function, or
3760 if there is a containing function with a declared label with
3761 the same name. */
3762 tree label = I_LABEL_DECL (name);
3764 if (label
3765 && ((DECL_CONTEXT (label) == current_function_decl
3766 && DECL_INITIAL (label) != NULL_TREE)
3767 || (DECL_CONTEXT (label) != current_function_decl
3768 && C_DECLARED_LABEL_FLAG (label))))
3770 error_at (location, "duplicate label %qD", label);
3771 locate_old_decl (label);
3772 return NULL_TREE;
3774 else if (label && DECL_CONTEXT (label) == current_function_decl)
3776 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3778 /* The label has been used or declared already in this function,
3779 but not defined. Update its location to point to this
3780 definition. */
3781 DECL_SOURCE_LOCATION (label) = location;
3782 set_spot_bindings (&label_vars->label_bindings, true);
3784 /* Issue warnings as required about any goto statements from
3785 earlier in the function. */
3786 check_earlier_gotos (label, label_vars);
3788 else
3790 struct c_label_vars *label_vars;
3792 /* No label binding for that identifier; make one. */
3793 label = make_label (location, name, true, &label_vars);
3795 /* Ordinary labels go in the current function scope. */
3796 bind_label (name, label, current_function_scope, label_vars);
3799 if (!in_system_header_at (input_location) && lookup_name (name))
3800 warning_at (location, OPT_Wtraditional,
3801 "traditional C lacks a separate namespace "
3802 "for labels, identifier %qE conflicts", name);
3804 /* Mark label as having been defined. */
3805 DECL_INITIAL (label) = error_mark_node;
3806 return label;
3809 /* Get the bindings for a new switch statement. This is used to issue
3810 warnings as appropriate for jumps from the switch to case or
3811 default labels. */
3813 struct c_spot_bindings *
3814 c_get_switch_bindings (void)
3816 struct c_spot_bindings *switch_bindings;
3818 switch_bindings = XNEW (struct c_spot_bindings);
3819 set_spot_bindings (switch_bindings, true);
3820 return switch_bindings;
3823 void
3824 c_release_switch_bindings (struct c_spot_bindings *bindings)
3826 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3827 XDELETE (bindings);
3830 /* This is called at the point of a case or default label to issue
3831 warnings about decls as needed. It returns true if it found an
3832 error, not just a warning. */
3834 bool
3835 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3836 location_t switch_loc, location_t case_loc)
3838 bool saw_error;
3839 struct c_scope *scope;
3841 saw_error = false;
3842 for (scope = current_scope;
3843 scope != switch_bindings->scope;
3844 scope = scope->outer)
3846 struct c_binding *b;
3848 gcc_assert (scope != NULL);
3850 if (!scope->has_jump_unsafe_decl)
3851 continue;
3853 for (b = scope->bindings; b != NULL; b = b->prev)
3855 if (decl_jump_unsafe (b->decl))
3857 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3859 saw_error = true;
3860 error_at (case_loc,
3861 ("switch jumps into scope of identifier with "
3862 "variably modified type"));
3864 else
3865 warning_at (case_loc, OPT_Wjump_misses_init,
3866 "switch jumps over variable initialization");
3867 inform (switch_loc, "switch starts here");
3868 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3869 b->decl);
3874 if (switch_bindings->stmt_exprs > 0)
3876 saw_error = true;
3877 error_at (case_loc, "switch jumps into statement expression");
3878 inform (switch_loc, "switch starts here");
3881 return saw_error;
3884 /* Given NAME, an IDENTIFIER_NODE,
3885 return the structure (or union or enum) definition for that name.
3886 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3887 CODE says which kind of type the caller wants;
3888 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3889 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3890 location where the tag was defined.
3891 If the wrong kind of type is found, an error is reported. */
3893 static tree
3894 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3895 location_t *ploc)
3897 struct c_binding *b = I_TAG_BINDING (name);
3898 bool thislevel = false;
3900 if (!b || !b->decl)
3901 return NULL_TREE;
3903 /* We only care about whether it's in this level if
3904 thislevel_only was set or it might be a type clash. */
3905 if (thislevel_only || TREE_CODE (b->decl) != code)
3907 /* For our purposes, a tag in the external scope is the same as
3908 a tag in the file scope. (Primarily relevant to Objective-C
3909 and its builtin structure tags, which get pushed before the
3910 file scope is created.) */
3911 if (B_IN_CURRENT_SCOPE (b)
3912 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3913 thislevel = true;
3916 if (thislevel_only && !thislevel)
3917 return NULL_TREE;
3919 if (TREE_CODE (b->decl) != code)
3921 /* Definition isn't the kind we were looking for. */
3922 pending_invalid_xref = name;
3923 pending_invalid_xref_location = input_location;
3925 /* If in the same binding level as a declaration as a tag
3926 of a different type, this must not be allowed to
3927 shadow that tag, so give the error immediately.
3928 (For example, "struct foo; union foo;" is invalid.) */
3929 if (thislevel)
3930 pending_xref_error ();
3933 if (ploc != NULL)
3934 *ploc = b->locus;
3936 return b->decl;
3939 /* Return true if a definition exists for NAME with code CODE. */
3941 bool
3942 tag_exists_p (enum tree_code code, tree name)
3944 struct c_binding *b = I_TAG_BINDING (name);
3946 if (b == NULL || b->decl == NULL_TREE)
3947 return false;
3948 return TREE_CODE (b->decl) == code;
3951 /* Print an error message now
3952 for a recent invalid struct, union or enum cross reference.
3953 We don't print them immediately because they are not invalid
3954 when used in the `struct foo;' construct for shadowing. */
3956 void
3957 pending_xref_error (void)
3959 if (pending_invalid_xref != NULL_TREE)
3960 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3961 pending_invalid_xref);
3962 pending_invalid_xref = NULL_TREE;
3966 /* Look up NAME in the current scope and its superiors
3967 in the namespace of variables, functions and typedefs.
3968 Return a ..._DECL node of some kind representing its definition,
3969 or return NULL_TREE if it is undefined. */
3971 tree
3972 lookup_name (tree name)
3974 struct c_binding *b = I_SYMBOL_BINDING (name);
3975 if (b && !b->invisible)
3977 maybe_record_typedef_use (b->decl);
3978 return b->decl;
3980 return NULL_TREE;
3983 /* Similar to `lookup_name' but look only at the indicated scope. */
3985 static tree
3986 lookup_name_in_scope (tree name, struct c_scope *scope)
3988 struct c_binding *b;
3990 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3991 if (B_IN_SCOPE (b, scope))
3992 return b->decl;
3993 return NULL_TREE;
3996 /* Look for the closest match for NAME within the currently valid
3997 scopes.
3999 This finds the identifier with the lowest Levenshtein distance to
4000 NAME. If there are multiple candidates with equal minimal distance,
4001 the first one found is returned. Scopes are searched from innermost
4002 outwards, and within a scope in reverse order of declaration, thus
4003 benefiting candidates "near" to the current scope.
4005 The function also looks for similar macro names to NAME, since a
4006 misspelled macro name will not be expanded, and hence looks like an
4007 identifier to the C frontend.
4009 It also looks for start_typename keywords, to detect "singed" vs "signed"
4010 typos.
4012 Use LOC for any deferred diagnostics. */
4014 name_hint
4015 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4017 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4019 /* First, try some well-known names in the C standard library, in case
4020 the user forgot a #include. */
4021 const char *header_hint
4022 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4024 if (header_hint)
4025 return name_hint (NULL,
4026 new suggest_missing_header (loc,
4027 IDENTIFIER_POINTER (name),
4028 header_hint));
4030 /* Only suggest names reserved for the implementation if NAME begins
4031 with an underscore. */
4032 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4034 best_match<tree, tree> bm (name);
4036 /* Look within currently valid scopes. */
4037 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4038 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4040 if (!binding->id || binding->invisible)
4041 continue;
4042 if (binding->decl == error_mark_node)
4043 continue;
4044 /* Don't use bindings from implicitly declared functions,
4045 as they were likely misspellings themselves. */
4046 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4047 if (C_DECL_IMPLICIT (binding->decl))
4048 continue;
4049 /* Don't suggest names that are reserved for use by the
4050 implementation, unless NAME began with an underscore. */
4051 if (!consider_implementation_names)
4053 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4054 if (name_reserved_for_implementation_p (suggestion_str))
4055 continue;
4057 switch (kind)
4059 case FUZZY_LOOKUP_TYPENAME:
4060 if (TREE_CODE (binding->decl) != TYPE_DECL)
4061 continue;
4062 break;
4064 case FUZZY_LOOKUP_FUNCTION_NAME:
4065 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4067 /* Allow function pointers. */
4068 if ((VAR_P (binding->decl)
4069 || TREE_CODE (binding->decl) == PARM_DECL)
4070 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4071 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4072 == FUNCTION_TYPE))
4073 break;
4074 continue;
4076 break;
4078 default:
4079 break;
4081 bm.consider (binding->id);
4084 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4086 x = SOME_OTHER_MACRO (y);
4087 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4088 as a misspelled identifier.
4090 Use the best distance so far so that a candidate is only set if
4091 a macro is better than anything so far. This allows early rejection
4092 (without calculating the edit distance) of macro names that must have
4093 distance >= bm.get_best_distance (), and means that we only get a
4094 non-NULL result for best_macro_match if it's better than any of
4095 the identifiers already checked, which avoids needless creation
4096 of identifiers for macro hashnodes. */
4097 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4098 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4099 /* If a macro is the closest so far to NAME, use it, creating an
4100 identifier tree node for it. */
4101 if (best_macro)
4103 const char *id = (const char *)best_macro->ident.str;
4104 tree macro_as_identifier
4105 = get_identifier_with_length (id, best_macro->ident.len);
4106 bm.set_best_so_far (macro_as_identifier,
4107 bmm.get_best_distance (),
4108 bmm.get_best_candidate_length ());
4111 /* Try the "start_typename" keywords to detect
4112 "singed" vs "signed" typos. */
4113 if (kind == FUZZY_LOOKUP_TYPENAME)
4115 for (unsigned i = 0; i < num_c_common_reswords; i++)
4117 const c_common_resword *resword = &c_common_reswords[i];
4118 if (!c_keyword_starts_typename (resword->rid))
4119 continue;
4120 tree resword_identifier = ridpointers [resword->rid];
4121 if (!resword_identifier)
4122 continue;
4123 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4124 bm.consider (resword_identifier);
4128 tree best = bm.get_best_meaningful_candidate ();
4129 if (best)
4130 return name_hint (IDENTIFIER_POINTER (best), NULL);
4131 else
4132 return name_hint (NULL, NULL);
4136 /* Create the predefined scalar types of C,
4137 and some nodes representing standard constants (0, 1, (void *) 0).
4138 Initialize the global scope.
4139 Make definitions for built-in primitive functions. */
4141 void
4142 c_init_decl_processing (void)
4144 location_t save_loc = input_location;
4146 /* Initialize reserved words for parser. */
4147 c_parse_init ();
4149 current_function_decl = NULL_TREE;
4151 gcc_obstack_init (&parser_obstack);
4153 /* Make the externals scope. */
4154 push_scope ();
4155 external_scope = current_scope;
4157 /* Declarations from c_common_nodes_and_builtins must not be associated
4158 with this input file, lest we get differences between using and not
4159 using preprocessed headers. */
4160 input_location = BUILTINS_LOCATION;
4162 c_common_nodes_and_builtins ();
4164 /* In C, comparisons and TRUTH_* expressions have type int. */
4165 truthvalue_type_node = integer_type_node;
4166 truthvalue_true_node = integer_one_node;
4167 truthvalue_false_node = integer_zero_node;
4169 /* Even in C99, which has a real boolean type. */
4170 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4171 boolean_type_node));
4173 input_location = save_loc;
4175 make_fname_decl = c_make_fname_decl;
4176 start_fname_decls ();
4179 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4180 give the decl, NAME is the initialization string and TYPE_DEP
4181 indicates whether NAME depended on the type of the function. As we
4182 don't yet implement delayed emission of static data, we mark the
4183 decl as emitted so it is not placed in the output. Anything using
4184 it must therefore pull out the STRING_CST initializer directly.
4185 FIXME. */
4187 static tree
4188 c_make_fname_decl (location_t loc, tree id, int type_dep)
4190 const char *name = fname_as_string (type_dep);
4191 tree decl, type, init;
4192 size_t length = strlen (name);
4194 type = build_array_type (char_type_node,
4195 build_index_type (size_int (length)));
4196 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4198 decl = build_decl (loc, VAR_DECL, id, type);
4200 TREE_STATIC (decl) = 1;
4201 TREE_READONLY (decl) = 1;
4202 DECL_ARTIFICIAL (decl) = 1;
4204 init = build_string (length + 1, name);
4205 free (CONST_CAST (char *, name));
4206 TREE_TYPE (init) = type;
4207 DECL_INITIAL (decl) = init;
4209 TREE_USED (decl) = 1;
4211 if (current_function_decl
4212 /* For invalid programs like this:
4214 void foo()
4215 const char* p = __FUNCTION__;
4217 the __FUNCTION__ is believed to appear in K&R style function
4218 parameter declarator. In that case we still don't have
4219 function_scope. */
4220 && current_function_scope)
4222 DECL_CONTEXT (decl) = current_function_decl;
4223 bind (id, decl, current_function_scope,
4224 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4227 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4229 return decl;
4232 tree
4233 c_builtin_function (tree decl)
4235 tree type = TREE_TYPE (decl);
4236 tree id = DECL_NAME (decl);
4238 const char *name = IDENTIFIER_POINTER (id);
4239 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4241 /* Should never be called on a symbol with a preexisting meaning. */
4242 gcc_assert (!I_SYMBOL_BINDING (id));
4244 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4245 UNKNOWN_LOCATION);
4247 /* Builtins in the implementation namespace are made visible without
4248 needing to be explicitly declared. See push_file_scope. */
4249 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4251 DECL_CHAIN (decl) = visible_builtins;
4252 visible_builtins = decl;
4255 return decl;
4258 tree
4259 c_builtin_function_ext_scope (tree decl)
4261 tree type = TREE_TYPE (decl);
4262 tree id = DECL_NAME (decl);
4264 const char *name = IDENTIFIER_POINTER (id);
4265 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4267 if (external_scope)
4268 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4269 UNKNOWN_LOCATION);
4271 /* Builtins in the implementation namespace are made visible without
4272 needing to be explicitly declared. See push_file_scope. */
4273 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4275 DECL_CHAIN (decl) = visible_builtins;
4276 visible_builtins = decl;
4279 return decl;
4282 /* Called when a declaration is seen that contains no names to declare.
4283 If its type is a reference to a structure, union or enum inherited
4284 from a containing scope, shadow that tag name for the current scope
4285 with a forward reference.
4286 If its type defines a new named structure or union
4287 or defines an enum, it is valid but we need not do anything here.
4288 Otherwise, it is an error. */
4290 void
4291 shadow_tag (const struct c_declspecs *declspecs)
4293 shadow_tag_warned (declspecs, 0);
4296 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4297 but no pedwarn. */
4298 void
4299 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4301 bool found_tag = false;
4303 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4305 tree value = declspecs->type;
4306 enum tree_code code = TREE_CODE (value);
4308 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4309 /* Used to test also that TYPE_SIZE (value) != 0.
4310 That caused warning for `struct foo;' at top level in the file. */
4312 tree name = TYPE_NAME (value);
4313 tree t;
4315 found_tag = true;
4317 if (declspecs->restrict_p)
4319 error ("invalid use of %<restrict%>");
4320 warned = 1;
4323 if (name == NULL_TREE)
4325 if (warned != 1 && code != ENUMERAL_TYPE)
4326 /* Empty unnamed enum OK */
4328 pedwarn (input_location, 0,
4329 "unnamed struct/union that defines no instances");
4330 warned = 1;
4333 else if (declspecs->typespec_kind != ctsk_tagdef
4334 && declspecs->typespec_kind != ctsk_tagfirstref
4335 && declspecs->storage_class != csc_none)
4337 if (warned != 1)
4338 pedwarn (input_location, 0,
4339 "empty declaration with storage class specifier "
4340 "does not redeclare tag");
4341 warned = 1;
4342 pending_xref_error ();
4344 else if (declspecs->typespec_kind != ctsk_tagdef
4345 && declspecs->typespec_kind != ctsk_tagfirstref
4346 && (declspecs->const_p
4347 || declspecs->volatile_p
4348 || declspecs->atomic_p
4349 || declspecs->restrict_p
4350 || declspecs->address_space))
4352 if (warned != 1)
4353 pedwarn (input_location, 0,
4354 "empty declaration with type qualifier "
4355 "does not redeclare tag");
4356 warned = 1;
4357 pending_xref_error ();
4359 else if (declspecs->typespec_kind != ctsk_tagdef
4360 && declspecs->typespec_kind != ctsk_tagfirstref
4361 && declspecs->alignas_p)
4363 if (warned != 1)
4364 pedwarn (input_location, 0,
4365 "empty declaration with %<_Alignas%> "
4366 "does not redeclare tag");
4367 warned = 1;
4368 pending_xref_error ();
4370 else
4372 pending_invalid_xref = NULL_TREE;
4373 t = lookup_tag (code, name, true, NULL);
4375 if (t == NULL_TREE)
4377 t = make_node (code);
4378 pushtag (input_location, name, t);
4382 else
4384 if (warned != 1 && !in_system_header_at (input_location))
4386 pedwarn (input_location, 0,
4387 "useless type name in empty declaration");
4388 warned = 1;
4392 else if (warned != 1 && !in_system_header_at (input_location)
4393 && declspecs->typedef_p)
4395 pedwarn (input_location, 0, "useless type name in empty declaration");
4396 warned = 1;
4399 pending_invalid_xref = NULL_TREE;
4401 if (declspecs->inline_p)
4403 error ("%<inline%> in empty declaration");
4404 warned = 1;
4407 if (declspecs->noreturn_p)
4409 error ("%<_Noreturn%> in empty declaration");
4410 warned = 1;
4413 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4415 error ("%<auto%> in file-scope empty declaration");
4416 warned = 1;
4419 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4421 error ("%<register%> in file-scope empty declaration");
4422 warned = 1;
4425 if (!warned && !in_system_header_at (input_location)
4426 && declspecs->storage_class != csc_none)
4428 warning (0, "useless storage class specifier in empty declaration");
4429 warned = 2;
4432 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4434 warning (0, "useless %qs in empty declaration",
4435 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4436 warned = 2;
4439 if (!warned
4440 && !in_system_header_at (input_location)
4441 && (declspecs->const_p
4442 || declspecs->volatile_p
4443 || declspecs->atomic_p
4444 || declspecs->restrict_p
4445 || declspecs->address_space))
4447 warning (0, "useless type qualifier in empty declaration");
4448 warned = 2;
4451 if (!warned && !in_system_header_at (input_location)
4452 && declspecs->alignas_p)
4454 warning (0, "useless %<_Alignas%> in empty declaration");
4455 warned = 2;
4458 if (warned != 1)
4460 if (!found_tag)
4461 pedwarn (input_location, 0, "empty declaration");
4466 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4467 bits. SPECS represents declaration specifiers that the grammar
4468 only permits to contain type qualifiers and attributes. */
4471 quals_from_declspecs (const struct c_declspecs *specs)
4473 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4474 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4475 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4476 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4477 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4478 gcc_assert (!specs->type
4479 && !specs->decl_attr
4480 && specs->typespec_word == cts_none
4481 && specs->storage_class == csc_none
4482 && !specs->typedef_p
4483 && !specs->explicit_signed_p
4484 && !specs->deprecated_p
4485 && !specs->long_p
4486 && !specs->long_long_p
4487 && !specs->short_p
4488 && !specs->signed_p
4489 && !specs->unsigned_p
4490 && !specs->complex_p
4491 && !specs->inline_p
4492 && !specs->noreturn_p
4493 && !specs->thread_p);
4494 return quals;
4497 /* Construct an array declarator. LOC is the location of the
4498 beginning of the array (usually the opening brace). EXPR is the
4499 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4500 inside the [] (to be applied to the pointer to which a parameter
4501 array is converted). STATIC_P is true if "static" is inside the
4502 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4503 VLA of unspecified length which is nevertheless a complete type,
4504 false otherwise. The field for the contained declarator is left to
4505 be filled in by set_array_declarator_inner. */
4507 struct c_declarator *
4508 build_array_declarator (location_t loc,
4509 tree expr, struct c_declspecs *quals, bool static_p,
4510 bool vla_unspec_p)
4512 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4513 struct c_declarator);
4514 declarator->id_loc = loc;
4515 declarator->kind = cdk_array;
4516 declarator->declarator = 0;
4517 declarator->u.array.dimen = expr;
4518 if (quals)
4520 declarator->u.array.attrs = quals->attrs;
4521 declarator->u.array.quals = quals_from_declspecs (quals);
4523 else
4525 declarator->u.array.attrs = NULL_TREE;
4526 declarator->u.array.quals = 0;
4528 declarator->u.array.static_p = static_p;
4529 declarator->u.array.vla_unspec_p = vla_unspec_p;
4530 if (static_p || quals != NULL)
4531 pedwarn_c90 (loc, OPT_Wpedantic,
4532 "ISO C90 does not support %<static%> or type "
4533 "qualifiers in parameter array declarators");
4534 if (vla_unspec_p)
4535 pedwarn_c90 (loc, OPT_Wpedantic,
4536 "ISO C90 does not support %<[*]%> array declarators");
4537 if (vla_unspec_p)
4539 if (!current_scope->parm_flag)
4541 /* C99 6.7.5.2p4 */
4542 error_at (loc, "%<[*]%> not allowed in other than "
4543 "function prototype scope");
4544 declarator->u.array.vla_unspec_p = false;
4545 return NULL;
4547 current_scope->had_vla_unspec = true;
4549 return declarator;
4552 /* Set the contained declarator of an array declarator. DECL is the
4553 declarator, as constructed by build_array_declarator; INNER is what
4554 appears on the left of the []. */
4556 struct c_declarator *
4557 set_array_declarator_inner (struct c_declarator *decl,
4558 struct c_declarator *inner)
4560 decl->declarator = inner;
4561 return decl;
4564 /* INIT is a constructor that forms DECL's initializer. If the final
4565 element initializes a flexible array field, add the size of that
4566 initializer to DECL's size. */
4568 static void
4569 add_flexible_array_elts_to_size (tree decl, tree init)
4571 tree elt, type;
4573 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4574 return;
4576 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4577 type = TREE_TYPE (elt);
4578 if (TREE_CODE (type) == ARRAY_TYPE
4579 && TYPE_SIZE (type) == NULL_TREE
4580 && TYPE_DOMAIN (type) != NULL_TREE
4581 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4583 complete_array_type (&type, elt, false);
4584 DECL_SIZE (decl)
4585 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4586 DECL_SIZE_UNIT (decl)
4587 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4591 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4592 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4593 before the type name, and set *EXPR_CONST_OPERANDS, if
4594 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4595 appear in a constant expression. */
4597 tree
4598 groktypename (struct c_type_name *type_name, tree *expr,
4599 bool *expr_const_operands)
4601 tree type;
4602 tree attrs = type_name->specs->attrs;
4604 type_name->specs->attrs = NULL_TREE;
4606 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4607 false, NULL, &attrs, expr, expr_const_operands,
4608 DEPRECATED_NORMAL);
4610 /* Apply attributes. */
4611 decl_attributes (&type, attrs, 0);
4613 return type;
4616 /* Wrapper for decl_attributes that adds some implicit attributes
4617 to VAR_DECLs or FUNCTION_DECLs. */
4619 static tree
4620 c_decl_attributes (tree *node, tree attributes, int flags)
4622 /* Add implicit "omp declare target" attribute if requested. */
4623 if (current_omp_declare_target_attribute
4624 && ((VAR_P (*node) && is_global_var (*node))
4625 || TREE_CODE (*node) == FUNCTION_DECL))
4627 if (VAR_P (*node)
4628 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4629 error ("%q+D in declare target directive does not have mappable type",
4630 *node);
4631 else
4632 attributes = tree_cons (get_identifier ("omp declare target"),
4633 NULL_TREE, attributes);
4636 /* Look up the current declaration with all the attributes merged
4637 so far so that attributes on the current declaration that's
4638 about to be pushed that conflict with the former can be detected,
4639 diagnosed, and rejected as appropriate. */
4640 tree last_decl = lookup_name (DECL_NAME (*node));
4641 if (!last_decl)
4642 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4644 return decl_attributes (node, attributes, flags, last_decl);
4648 /* Decode a declarator in an ordinary declaration or data definition.
4649 This is called as soon as the type information and variable name
4650 have been parsed, before parsing the initializer if any.
4651 Here we create the ..._DECL node, fill in its type,
4652 and put it on the list of decls for the current context.
4653 The ..._DECL node is returned as the value.
4655 Exception: for arrays where the length is not specified,
4656 the type is left null, to be filled in by `finish_decl'.
4658 Function definitions do not come here; they go to start_function
4659 instead. However, external and forward declarations of functions
4660 do go through here. Structure field declarations are done by
4661 grokfield and not through here. */
4663 tree
4664 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4665 bool initialized, tree attributes)
4667 tree decl;
4668 tree tem;
4669 tree expr = NULL_TREE;
4670 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4672 /* An object declared as __attribute__((deprecated)) suppresses
4673 warnings of uses of other deprecated items. */
4674 if (lookup_attribute ("deprecated", attributes))
4675 deprecated_state = DEPRECATED_SUPPRESS;
4677 decl = grokdeclarator (declarator, declspecs,
4678 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4679 deprecated_state);
4680 if (!decl || decl == error_mark_node)
4681 return NULL_TREE;
4683 if (expr)
4684 add_stmt (fold_convert (void_type_node, expr));
4686 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4687 warning (OPT_Wmain, "%q+D is usually a function", decl);
4689 if (initialized)
4690 /* Is it valid for this decl to have an initializer at all?
4691 If not, set INITIALIZED to zero, which will indirectly
4692 tell 'finish_decl' to ignore the initializer once it is parsed. */
4693 switch (TREE_CODE (decl))
4695 case TYPE_DECL:
4696 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4697 initialized = false;
4698 break;
4700 case FUNCTION_DECL:
4701 error ("function %qD is initialized like a variable", decl);
4702 initialized = false;
4703 break;
4705 case PARM_DECL:
4706 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4707 error ("parameter %qD is initialized", decl);
4708 initialized = false;
4709 break;
4711 default:
4712 /* Don't allow initializations for incomplete types except for
4713 arrays which might be completed by the initialization. */
4715 /* This can happen if the array size is an undefined macro.
4716 We already gave a warning, so we don't need another one. */
4717 if (TREE_TYPE (decl) == error_mark_node)
4718 initialized = false;
4719 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4721 /* A complete type is ok if size is fixed. */
4723 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4724 || C_DECL_VARIABLE_SIZE (decl))
4726 error ("variable-sized object may not be initialized");
4727 initialized = false;
4730 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4732 error ("variable %qD has initializer but incomplete type", decl);
4733 initialized = false;
4735 else if (C_DECL_VARIABLE_SIZE (decl))
4737 /* Although C99 is unclear about whether incomplete arrays
4738 of VLAs themselves count as VLAs, it does not make
4739 sense to permit them to be initialized given that
4740 ordinary VLAs may not be initialized. */
4741 error ("variable-sized object may not be initialized");
4742 initialized = false;
4746 if (initialized)
4748 if (current_scope == file_scope)
4749 TREE_STATIC (decl) = 1;
4751 /* Tell 'pushdecl' this is an initialized decl
4752 even though we don't yet have the initializer expression.
4753 Also tell 'finish_decl' it may store the real initializer. */
4754 DECL_INITIAL (decl) = error_mark_node;
4757 /* If this is a function declaration, write a record describing it to the
4758 prototypes file (if requested). */
4760 if (TREE_CODE (decl) == FUNCTION_DECL)
4761 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4763 /* ANSI specifies that a tentative definition which is not merged with
4764 a non-tentative definition behaves exactly like a definition with an
4765 initializer equal to zero. (Section 3.7.2)
4767 -fno-common gives strict ANSI behavior, though this tends to break
4768 a large body of code that grew up without this rule.
4770 Thread-local variables are never common, since there's no entrenched
4771 body of code to break, and it allows more efficient variable references
4772 in the presence of dynamic linking. */
4774 if (VAR_P (decl)
4775 && !initialized
4776 && TREE_PUBLIC (decl)
4777 && !DECL_THREAD_LOCAL_P (decl)
4778 && !flag_no_common)
4779 DECL_COMMON (decl) = 1;
4781 /* Set attributes here so if duplicate decl, will have proper attributes. */
4782 c_decl_attributes (&decl, attributes, 0);
4784 /* Handle gnu_inline attribute. */
4785 if (declspecs->inline_p
4786 && !flag_gnu89_inline
4787 && TREE_CODE (decl) == FUNCTION_DECL
4788 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4789 || current_function_decl))
4791 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4793 else if (declspecs->storage_class != csc_static)
4794 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4797 if (TREE_CODE (decl) == FUNCTION_DECL
4798 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4800 struct c_declarator *ce = declarator;
4802 if (ce->kind == cdk_pointer)
4803 ce = declarator->declarator;
4804 if (ce->kind == cdk_function)
4806 tree args = ce->u.arg_info->parms;
4807 for (; args; args = DECL_CHAIN (args))
4809 tree type = TREE_TYPE (args);
4810 if (type && INTEGRAL_TYPE_P (type)
4811 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4812 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4817 if (TREE_CODE (decl) == FUNCTION_DECL
4818 && DECL_DECLARED_INLINE_P (decl)
4819 && DECL_UNINLINABLE (decl)
4820 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4821 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4822 decl);
4824 /* C99 6.7.4p3: An inline definition of a function with external
4825 linkage shall not contain a definition of a modifiable object
4826 with static storage duration... */
4827 if (VAR_P (decl)
4828 && current_scope != file_scope
4829 && TREE_STATIC (decl)
4830 && !TREE_READONLY (decl)
4831 && DECL_DECLARED_INLINE_P (current_function_decl)
4832 && DECL_EXTERNAL (current_function_decl))
4833 record_inline_static (input_location, current_function_decl,
4834 decl, csi_modifiable);
4836 if (c_dialect_objc ()
4837 && VAR_OR_FUNCTION_DECL_P (decl))
4838 objc_check_global_decl (decl);
4840 /* Add this decl to the current scope.
4841 TEM may equal DECL or it may be a previous decl of the same name. */
4842 tem = pushdecl (decl);
4844 if (initialized && DECL_EXTERNAL (tem))
4846 DECL_EXTERNAL (tem) = 0;
4847 TREE_STATIC (tem) = 1;
4850 return tem;
4853 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4854 DECL or the non-array element type if DECL is an uninitialized array.
4855 If that type has a const member, diagnose this. */
4857 static void
4858 diagnose_uninitialized_cst_member (tree decl, tree type)
4860 tree field;
4861 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4863 tree field_type;
4864 if (TREE_CODE (field) != FIELD_DECL)
4865 continue;
4866 field_type = strip_array_types (TREE_TYPE (field));
4868 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4870 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4871 "uninitialized const member in %qT is invalid in C++",
4872 strip_array_types (TREE_TYPE (decl)));
4873 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4876 if (RECORD_OR_UNION_TYPE_P (field_type))
4877 diagnose_uninitialized_cst_member (decl, field_type);
4881 /* Finish processing of a declaration;
4882 install its initial value.
4883 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4884 If the length of an array type is not known before,
4885 it must be determined now, from the initial value, or it is an error.
4887 INIT_LOC is the location of the initial value. */
4889 void
4890 finish_decl (tree decl, location_t init_loc, tree init,
4891 tree origtype, tree asmspec_tree)
4893 tree type;
4894 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4895 const char *asmspec = 0;
4897 /* If a name was specified, get the string. */
4898 if (VAR_OR_FUNCTION_DECL_P (decl)
4899 && DECL_FILE_SCOPE_P (decl))
4900 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4901 if (asmspec_tree)
4902 asmspec = TREE_STRING_POINTER (asmspec_tree);
4904 if (VAR_P (decl)
4905 && TREE_STATIC (decl)
4906 && global_bindings_p ())
4907 /* So decl is a global variable. Record the types it uses
4908 so that we can decide later to emit debug info for them. */
4909 record_types_used_by_current_var_decl (decl);
4911 /* If `start_decl' didn't like having an initialization, ignore it now. */
4912 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4913 init = NULL_TREE;
4915 /* Don't crash if parm is initialized. */
4916 if (TREE_CODE (decl) == PARM_DECL)
4917 init = NULL_TREE;
4919 if (init)
4920 store_init_value (init_loc, decl, init, origtype);
4922 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4923 || TREE_CODE (decl) == FIELD_DECL))
4924 objc_check_decl (decl);
4926 type = TREE_TYPE (decl);
4928 /* Deduce size of array from initialization, if not already known. */
4929 if (TREE_CODE (type) == ARRAY_TYPE
4930 && TYPE_DOMAIN (type) == NULL_TREE
4931 && TREE_CODE (decl) != TYPE_DECL)
4933 bool do_default
4934 = (TREE_STATIC (decl)
4935 /* Even if pedantic, an external linkage array
4936 may have incomplete type at first. */
4937 ? pedantic && !TREE_PUBLIC (decl)
4938 : !DECL_EXTERNAL (decl));
4939 int failure
4940 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4941 do_default);
4943 /* Get the completed type made by complete_array_type. */
4944 type = TREE_TYPE (decl);
4946 switch (failure)
4948 case 1:
4949 error ("initializer fails to determine size of %q+D", decl);
4950 break;
4952 case 2:
4953 if (do_default)
4954 error ("array size missing in %q+D", decl);
4955 /* If a `static' var's size isn't known,
4956 make it extern as well as static, so it does not get
4957 allocated.
4958 If it is not `static', then do not mark extern;
4959 finish_incomplete_decl will give it a default size
4960 and it will get allocated. */
4961 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4962 DECL_EXTERNAL (decl) = 1;
4963 break;
4965 case 3:
4966 error ("zero or negative size array %q+D", decl);
4967 break;
4969 case 0:
4970 /* For global variables, update the copy of the type that
4971 exists in the binding. */
4972 if (TREE_PUBLIC (decl))
4974 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4975 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4976 b_ext = b_ext->shadowed;
4977 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4979 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4980 b_ext->u.type = composite_type (b_ext->u.type, type);
4981 else
4982 b_ext->u.type = type;
4985 break;
4987 default:
4988 gcc_unreachable ();
4991 if (DECL_INITIAL (decl))
4992 TREE_TYPE (DECL_INITIAL (decl)) = type;
4994 relayout_decl (decl);
4997 if (VAR_P (decl))
4999 if (init && TREE_CODE (init) == CONSTRUCTOR)
5000 add_flexible_array_elts_to_size (decl, init);
5002 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5003 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5004 layout_decl (decl, 0);
5006 if (DECL_SIZE (decl) == NULL_TREE
5007 /* Don't give an error if we already gave one earlier. */
5008 && TREE_TYPE (decl) != error_mark_node
5009 && (TREE_STATIC (decl)
5010 /* A static variable with an incomplete type
5011 is an error if it is initialized.
5012 Also if it is not file scope.
5013 Otherwise, let it through, but if it is not `extern'
5014 then it may cause an error message later. */
5015 ? (DECL_INITIAL (decl) != NULL_TREE
5016 || !DECL_FILE_SCOPE_P (decl))
5017 /* An automatic variable with an incomplete type
5018 is an error. */
5019 : !DECL_EXTERNAL (decl)))
5021 error ("storage size of %q+D isn%'t known", decl);
5022 TREE_TYPE (decl) = error_mark_node;
5025 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5026 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5027 && DECL_SIZE (decl) == NULL_TREE
5028 && TREE_STATIC (decl))
5029 incomplete_record_decls.safe_push (decl);
5031 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5033 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5034 constant_expression_warning (DECL_SIZE (decl));
5035 else
5037 error ("storage size of %q+D isn%'t constant", decl);
5038 TREE_TYPE (decl) = error_mark_node;
5042 if (TREE_USED (type))
5044 TREE_USED (decl) = 1;
5045 DECL_READ_P (decl) = 1;
5049 /* If this is a function and an assembler name is specified, reset DECL_RTL
5050 so we can give it its new name. Also, update builtin_decl if it
5051 was a normal built-in. */
5052 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5054 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5055 set_builtin_user_assembler_name (decl, asmspec);
5056 set_user_assembler_name (decl, asmspec);
5059 /* If #pragma weak was used, mark the decl weak now. */
5060 maybe_apply_pragma_weak (decl);
5062 /* Output the assembler code and/or RTL code for variables and functions,
5063 unless the type is an undefined structure or union.
5064 If not, it will get done when the type is completed. */
5066 if (VAR_OR_FUNCTION_DECL_P (decl))
5068 /* Determine the ELF visibility. */
5069 if (TREE_PUBLIC (decl))
5070 c_determine_visibility (decl);
5072 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5073 if (c_dialect_objc ())
5074 objc_check_decl (decl);
5076 if (asmspec)
5078 /* If this is not a static variable, issue a warning.
5079 It doesn't make any sense to give an ASMSPEC for an
5080 ordinary, non-register local variable. Historically,
5081 GCC has accepted -- but ignored -- the ASMSPEC in
5082 this case. */
5083 if (!DECL_FILE_SCOPE_P (decl)
5084 && VAR_P (decl)
5085 && !C_DECL_REGISTER (decl)
5086 && !TREE_STATIC (decl))
5087 warning (0, "ignoring asm-specifier for non-static local "
5088 "variable %q+D", decl);
5089 else
5090 set_user_assembler_name (decl, asmspec);
5093 if (DECL_FILE_SCOPE_P (decl))
5095 if (DECL_INITIAL (decl) == NULL_TREE
5096 || DECL_INITIAL (decl) == error_mark_node)
5097 /* Don't output anything
5098 when a tentative file-scope definition is seen.
5099 But at end of compilation, do output code for them. */
5100 DECL_DEFER_OUTPUT (decl) = 1;
5101 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5102 DECL_HARD_REGISTER (decl) = 1;
5103 rest_of_decl_compilation (decl, true, 0);
5105 else
5107 /* In conjunction with an ASMSPEC, the `register'
5108 keyword indicates that we should place the variable
5109 in a particular register. */
5110 if (asmspec && C_DECL_REGISTER (decl))
5112 DECL_HARD_REGISTER (decl) = 1;
5113 /* This cannot be done for a structure with volatile
5114 fields, on which DECL_REGISTER will have been
5115 reset. */
5116 if (!DECL_REGISTER (decl))
5117 error ("cannot put object with volatile field into register");
5120 if (TREE_CODE (decl) != FUNCTION_DECL)
5122 /* If we're building a variable sized type, and we might be
5123 reachable other than via the top of the current binding
5124 level, then create a new BIND_EXPR so that we deallocate
5125 the object at the right time. */
5126 /* Note that DECL_SIZE can be null due to errors. */
5127 if (DECL_SIZE (decl)
5128 && !TREE_CONSTANT (DECL_SIZE (decl))
5129 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5131 tree bind;
5132 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5133 TREE_SIDE_EFFECTS (bind) = 1;
5134 add_stmt (bind);
5135 BIND_EXPR_BODY (bind) = push_stmt_list ();
5137 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5138 DECL_EXPR, decl));
5143 if (!DECL_FILE_SCOPE_P (decl))
5145 /* Recompute the RTL of a local array now
5146 if it used to be an incomplete type. */
5147 if (was_incomplete && !is_global_var (decl))
5149 /* If we used it already as memory, it must stay in memory. */
5150 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5151 /* If it's still incomplete now, no init will save it. */
5152 if (DECL_SIZE (decl) == NULL_TREE)
5153 DECL_INITIAL (decl) = NULL_TREE;
5158 if (TREE_CODE (decl) == TYPE_DECL)
5160 if (!DECL_FILE_SCOPE_P (decl)
5161 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5162 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5164 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5167 /* Install a cleanup (aka destructor) if one was given. */
5168 if (VAR_P (decl) && !TREE_STATIC (decl))
5170 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5171 if (attr)
5173 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5174 tree cleanup_decl = lookup_name (cleanup_id);
5175 tree cleanup;
5176 vec<tree, va_gc> *v;
5178 /* Build "cleanup(&decl)" for the destructor. */
5179 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5180 vec_alloc (v, 1);
5181 v->quick_push (cleanup);
5182 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5183 vNULL, cleanup_decl, v, NULL);
5184 vec_free (v);
5186 /* Don't warn about decl unused; the cleanup uses it. */
5187 TREE_USED (decl) = 1;
5188 TREE_USED (cleanup_decl) = 1;
5189 DECL_READ_P (decl) = 1;
5191 push_cleanup (decl, cleanup, false);
5195 if (warn_cxx_compat
5196 && VAR_P (decl)
5197 && !DECL_EXTERNAL (decl)
5198 && DECL_INITIAL (decl) == NULL_TREE)
5200 type = strip_array_types (type);
5201 if (TREE_READONLY (decl))
5202 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5203 "uninitialized const %qD is invalid in C++", decl);
5204 else if (RECORD_OR_UNION_TYPE_P (type)
5205 && C_TYPE_FIELDS_READONLY (type))
5206 diagnose_uninitialized_cst_member (decl, type);
5209 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5212 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5213 EXPR is NULL or a pointer to an expression that needs to be
5214 evaluated for the side effects of array size expressions in the
5215 parameters. */
5217 tree
5218 grokparm (const struct c_parm *parm, tree *expr)
5220 tree attrs = parm->attrs;
5221 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5222 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5224 decl_attributes (&decl, attrs, 0);
5226 return decl;
5229 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5230 and push that on the current scope. EXPR is a pointer to an
5231 expression that needs to be evaluated for the side effects of array
5232 size expressions in the parameters. */
5234 void
5235 push_parm_decl (const struct c_parm *parm, tree *expr)
5237 tree attrs = parm->attrs;
5238 tree decl;
5240 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5241 &attrs, expr, NULL, DEPRECATED_NORMAL);
5242 if (decl && DECL_P (decl))
5243 DECL_SOURCE_LOCATION (decl) = parm->loc;
5244 decl_attributes (&decl, attrs, 0);
5246 decl = pushdecl (decl);
5248 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5251 /* Mark all the parameter declarations to date as forward decls.
5252 Also diagnose use of this extension. */
5254 void
5255 mark_forward_parm_decls (void)
5257 struct c_binding *b;
5259 if (pedantic && !current_scope->warned_forward_parm_decls)
5261 pedwarn (input_location, OPT_Wpedantic,
5262 "ISO C forbids forward parameter declarations");
5263 current_scope->warned_forward_parm_decls = true;
5266 for (b = current_scope->bindings; b; b = b->prev)
5267 if (TREE_CODE (b->decl) == PARM_DECL)
5268 TREE_ASM_WRITTEN (b->decl) = 1;
5271 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5272 literal, which may be an incomplete array type completed by the
5273 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5274 literal. NON_CONST is true if the initializers contain something
5275 that cannot occur in a constant expression. */
5277 tree
5278 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5280 /* We do not use start_decl here because we have a type, not a declarator;
5281 and do not use finish_decl because the decl should be stored inside
5282 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5283 tree decl;
5284 tree complit;
5285 tree stmt;
5287 if (type == error_mark_node
5288 || init == error_mark_node)
5289 return error_mark_node;
5291 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5292 DECL_EXTERNAL (decl) = 0;
5293 TREE_PUBLIC (decl) = 0;
5294 TREE_STATIC (decl) = (current_scope == file_scope);
5295 DECL_CONTEXT (decl) = current_function_decl;
5296 TREE_USED (decl) = 1;
5297 DECL_READ_P (decl) = 1;
5298 DECL_ARTIFICIAL (decl) = 1;
5299 DECL_IGNORED_P (decl) = 1;
5300 TREE_TYPE (decl) = type;
5301 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5302 store_init_value (loc, decl, init, NULL_TREE);
5304 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5306 int failure = complete_array_type (&TREE_TYPE (decl),
5307 DECL_INITIAL (decl), true);
5308 /* If complete_array_type returns 3, it means that the
5309 initial value of the compound literal is empty. Allow it. */
5310 gcc_assert (failure == 0 || failure == 3);
5312 type = TREE_TYPE (decl);
5313 TREE_TYPE (DECL_INITIAL (decl)) = type;
5316 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5318 c_incomplete_type_error (loc, NULL_TREE, type);
5319 return error_mark_node;
5322 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5323 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5324 TREE_SIDE_EFFECTS (complit) = 1;
5326 layout_decl (decl, 0);
5328 if (TREE_STATIC (decl))
5330 /* This decl needs a name for the assembler output. */
5331 set_compound_literal_name (decl);
5332 DECL_DEFER_OUTPUT (decl) = 1;
5333 DECL_COMDAT (decl) = 1;
5334 pushdecl (decl);
5335 rest_of_decl_compilation (decl, 1, 0);
5338 if (non_const)
5340 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5341 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5344 return complit;
5347 /* Check the type of a compound literal. Here we just check that it
5348 is valid for C++. */
5350 void
5351 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5353 if (warn_cxx_compat
5354 && (type_name->specs->typespec_kind == ctsk_tagdef
5355 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5356 warning_at (loc, OPT_Wc___compat,
5357 "defining a type in a compound literal is invalid in C++");
5360 /* Determine whether TYPE is a structure with a flexible array member,
5361 or a union containing such a structure (possibly recursively). */
5363 static bool
5364 flexible_array_type_p (tree type)
5366 tree x;
5367 switch (TREE_CODE (type))
5369 case RECORD_TYPE:
5370 x = TYPE_FIELDS (type);
5371 if (x == NULL_TREE)
5372 return false;
5373 while (DECL_CHAIN (x) != NULL_TREE)
5374 x = DECL_CHAIN (x);
5375 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5376 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5377 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5378 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5379 return true;
5380 return false;
5381 case UNION_TYPE:
5382 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5384 if (flexible_array_type_p (TREE_TYPE (x)))
5385 return true;
5387 return false;
5388 default:
5389 return false;
5393 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5394 replacing with appropriate values if they are invalid. */
5396 static void
5397 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5398 tree orig_name)
5400 tree type_mv;
5401 unsigned int max_width;
5402 unsigned HOST_WIDE_INT w;
5403 const char *name = (orig_name
5404 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5405 : _("<anonymous>"));
5407 /* Detect and ignore out of range field width and process valid
5408 field widths. */
5409 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5411 error_at (loc, "bit-field %qs width not an integer constant", name);
5412 *width = integer_one_node;
5414 else
5416 if (TREE_CODE (*width) != INTEGER_CST)
5418 *width = c_fully_fold (*width, false, NULL);
5419 if (TREE_CODE (*width) == INTEGER_CST)
5420 pedwarn (loc, OPT_Wpedantic,
5421 "bit-field %qs width not an integer constant expression",
5422 name);
5424 if (TREE_CODE (*width) != INTEGER_CST)
5426 error_at (loc, "bit-field %qs width not an integer constant", name);
5427 *width = integer_one_node;
5429 constant_expression_warning (*width);
5430 if (tree_int_cst_sgn (*width) < 0)
5432 error_at (loc, "negative width in bit-field %qs", name);
5433 *width = integer_one_node;
5435 else if (integer_zerop (*width) && orig_name)
5437 error_at (loc, "zero width for bit-field %qs", name);
5438 *width = integer_one_node;
5442 /* Detect invalid bit-field type. */
5443 if (TREE_CODE (*type) != INTEGER_TYPE
5444 && TREE_CODE (*type) != BOOLEAN_TYPE
5445 && TREE_CODE (*type) != ENUMERAL_TYPE)
5447 error_at (loc, "bit-field %qs has invalid type", name);
5448 *type = unsigned_type_node;
5451 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5453 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5454 name);
5455 *type = unsigned_type_node;
5458 type_mv = TYPE_MAIN_VARIANT (*type);
5459 if (!in_system_header_at (input_location)
5460 && type_mv != integer_type_node
5461 && type_mv != unsigned_type_node
5462 && type_mv != boolean_type_node)
5463 pedwarn_c90 (loc, OPT_Wpedantic,
5464 "type of bit-field %qs is a GCC extension", name);
5466 max_width = TYPE_PRECISION (*type);
5468 if (0 < compare_tree_int (*width, max_width))
5470 error_at (loc, "width of %qs exceeds its type", name);
5471 w = max_width;
5472 *width = build_int_cst (integer_type_node, w);
5474 else
5475 w = tree_to_uhwi (*width);
5477 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5479 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5480 if (!lt
5481 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5482 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5483 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5489 /* Print warning about variable length array if necessary. */
5491 static void
5492 warn_variable_length_array (tree name, tree size)
5494 if (TREE_CONSTANT (size))
5496 if (name)
5497 pedwarn_c90 (input_location, OPT_Wvla,
5498 "ISO C90 forbids array %qE whose size "
5499 "can%'t be evaluated", name);
5500 else
5501 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5502 "whose size can%'t be evaluated");
5504 else
5506 if (name)
5507 pedwarn_c90 (input_location, OPT_Wvla,
5508 "ISO C90 forbids variable length array %qE", name);
5509 else
5510 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5511 "length array");
5515 /* Print warning about defaulting to int if necessary. */
5517 static void
5518 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5520 diagnostic_info diagnostic;
5521 va_list ap;
5522 rich_location richloc (line_table, location);
5524 va_start (ap, gmsgid);
5525 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5526 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5527 diagnostic.option_index = opt;
5528 diagnostic_report_diagnostic (global_dc, &diagnostic);
5529 va_end (ap);
5532 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5533 considering only those c_declspec_words found in LIST, which
5534 must be terminated by cdw_number_of_elements. */
5536 static location_t
5537 smallest_type_quals_location (const location_t *locations,
5538 const c_declspec_word *list)
5540 location_t loc = UNKNOWN_LOCATION;
5541 while (*list != cdw_number_of_elements)
5543 location_t newloc = locations[*list];
5544 if (loc == UNKNOWN_LOCATION
5545 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5546 loc = newloc;
5547 list++;
5550 return loc;
5553 /* Given declspecs and a declarator,
5554 determine the name and type of the object declared
5555 and construct a ..._DECL node for it.
5556 (In one case we can return a ..._TYPE node instead.
5557 For invalid input we sometimes return NULL_TREE.)
5559 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5561 DECL_CONTEXT says which syntactic context this declaration is in:
5562 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5563 FUNCDEF for a function definition. Like NORMAL but a few different
5564 error messages in each case. Return value may be zero meaning
5565 this definition is too screwy to try to parse.
5566 PARM for a parameter declaration (either within a function prototype
5567 or before a function body). Make a PARM_DECL, or return void_type_node.
5568 TYPENAME if for a typename (in a cast or sizeof).
5569 Don't make a DECL node; just return the ..._TYPE node.
5570 FIELD for a struct or union field; make a FIELD_DECL.
5571 INITIALIZED is true if the decl has an initializer.
5572 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5573 representing the width of the bit-field.
5574 DECL_ATTRS points to the list of attributes that should be added to this
5575 decl. Any nested attributes that belong on the decl itself will be
5576 added to this list.
5577 If EXPR is not NULL, any expressions that need to be evaluated as
5578 part of evaluating variably modified types will be stored in *EXPR.
5579 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5580 set to indicate whether operands in *EXPR can be used in constant
5581 expressions.
5582 DEPRECATED_STATE is a deprecated_states value indicating whether
5583 deprecation warnings should be suppressed.
5585 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5586 It may also be so in the PARM case, for a prototype where the
5587 argument type is specified but not the name.
5589 This function is where the complicated C meanings of `static'
5590 and `extern' are interpreted. */
5592 static tree
5593 grokdeclarator (const struct c_declarator *declarator,
5594 struct c_declspecs *declspecs,
5595 enum decl_context decl_context, bool initialized, tree *width,
5596 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5597 enum deprecated_states deprecated_state)
5599 tree type = declspecs->type;
5600 bool threadp = declspecs->thread_p;
5601 enum c_storage_class storage_class = declspecs->storage_class;
5602 int constp;
5603 int restrictp;
5604 int volatilep;
5605 int atomicp;
5606 int type_quals = TYPE_UNQUALIFIED;
5607 tree name = NULL_TREE;
5608 bool funcdef_flag = false;
5609 bool funcdef_syntax = false;
5610 bool size_varies = false;
5611 tree decl_attr = declspecs->decl_attr;
5612 int array_ptr_quals = TYPE_UNQUALIFIED;
5613 tree array_ptr_attrs = NULL_TREE;
5614 bool array_parm_static = false;
5615 bool array_parm_vla_unspec_p = false;
5616 tree returned_attrs = NULL_TREE;
5617 bool bitfield = width != NULL;
5618 tree element_type;
5619 tree orig_qual_type = NULL;
5620 size_t orig_qual_indirect = 0;
5621 struct c_arg_info *arg_info = 0;
5622 addr_space_t as1, as2, address_space;
5623 location_t loc = UNKNOWN_LOCATION;
5624 tree expr_dummy;
5625 bool expr_const_operands_dummy;
5626 enum c_declarator_kind first_non_attr_kind;
5627 unsigned int alignas_align = 0;
5629 if (TREE_CODE (type) == ERROR_MARK)
5630 return error_mark_node;
5631 if (expr == NULL)
5633 expr = &expr_dummy;
5634 expr_dummy = NULL_TREE;
5636 if (expr_const_operands == NULL)
5637 expr_const_operands = &expr_const_operands_dummy;
5639 if (declspecs->expr)
5641 if (*expr)
5642 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5643 declspecs->expr);
5644 else
5645 *expr = declspecs->expr;
5647 *expr_const_operands = declspecs->expr_const_operands;
5649 if (decl_context == FUNCDEF)
5650 funcdef_flag = true, decl_context = NORMAL;
5652 /* Look inside a declarator for the name being declared
5653 and get it as an IDENTIFIER_NODE, for an error message. */
5655 const struct c_declarator *decl = declarator;
5657 first_non_attr_kind = cdk_attrs;
5658 while (decl)
5659 switch (decl->kind)
5661 case cdk_array:
5662 loc = decl->id_loc;
5663 /* FALL THRU. */
5665 case cdk_function:
5666 case cdk_pointer:
5667 funcdef_syntax = (decl->kind == cdk_function);
5668 if (first_non_attr_kind == cdk_attrs)
5669 first_non_attr_kind = decl->kind;
5670 decl = decl->declarator;
5671 break;
5673 case cdk_attrs:
5674 decl = decl->declarator;
5675 break;
5677 case cdk_id:
5678 loc = decl->id_loc;
5679 if (decl->u.id)
5680 name = decl->u.id;
5681 if (first_non_attr_kind == cdk_attrs)
5682 first_non_attr_kind = decl->kind;
5683 decl = 0;
5684 break;
5686 default:
5687 gcc_unreachable ();
5689 if (name == NULL_TREE)
5691 gcc_assert (decl_context == PARM
5692 || decl_context == TYPENAME
5693 || (decl_context == FIELD
5694 && declarator->kind == cdk_id));
5695 gcc_assert (!initialized);
5699 /* A function definition's declarator must have the form of
5700 a function declarator. */
5702 if (funcdef_flag && !funcdef_syntax)
5703 return NULL_TREE;
5705 /* If this looks like a function definition, make it one,
5706 even if it occurs where parms are expected.
5707 Then store_parm_decls will reject it and not use it as a parm. */
5708 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5709 decl_context = PARM;
5711 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5712 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5714 if ((decl_context == NORMAL || decl_context == FIELD)
5715 && current_scope == file_scope
5716 && variably_modified_type_p (type, NULL_TREE))
5718 if (name)
5719 error_at (loc, "variably modified %qE at file scope", name);
5720 else
5721 error_at (loc, "variably modified field at file scope");
5722 type = integer_type_node;
5725 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5727 /* Diagnose defaulting to "int". */
5729 if (declspecs->default_int_p && !in_system_header_at (input_location))
5731 /* Issue a warning if this is an ISO C 99 program or if
5732 -Wreturn-type and this is a function, or if -Wimplicit;
5733 prefer the former warning since it is more explicit. */
5734 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5735 && funcdef_flag)
5736 warn_about_return_type = 1;
5737 else
5739 if (name)
5740 warn_defaults_to (loc, OPT_Wimplicit_int,
5741 "type defaults to %<int%> in declaration "
5742 "of %qE", name);
5743 else
5744 warn_defaults_to (loc, OPT_Wimplicit_int,
5745 "type defaults to %<int%> in type name");
5749 /* Adjust the type if a bit-field is being declared,
5750 -funsigned-bitfields applied and the type is not explicitly
5751 "signed". */
5752 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5753 && TREE_CODE (type) == INTEGER_TYPE)
5754 type = unsigned_type_for (type);
5756 /* Figure out the type qualifiers for the declaration. There are
5757 two ways a declaration can become qualified. One is something
5758 like `const int i' where the `const' is explicit. Another is
5759 something like `typedef const int CI; CI i' where the type of the
5760 declaration contains the `const'. A third possibility is that
5761 there is a type qualifier on the element type of a typedefed
5762 array type, in which case we should extract that qualifier so
5763 that c_apply_type_quals_to_decl receives the full list of
5764 qualifiers to work with (C90 is not entirely clear about whether
5765 duplicate qualifiers should be diagnosed in this case, but it
5766 seems most appropriate to do so). */
5767 element_type = strip_array_types (type);
5768 constp = declspecs->const_p + TYPE_READONLY (element_type);
5769 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5770 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5771 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5772 as1 = declspecs->address_space;
5773 as2 = TYPE_ADDR_SPACE (element_type);
5774 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5776 if (constp > 1)
5777 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5778 if (restrictp > 1)
5779 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5780 if (volatilep > 1)
5781 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5782 if (atomicp > 1)
5783 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5785 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5786 error_at (loc, "conflicting named address spaces (%s vs %s)",
5787 c_addr_space_name (as1), c_addr_space_name (as2));
5789 if ((TREE_CODE (type) == ARRAY_TYPE
5790 || first_non_attr_kind == cdk_array)
5791 && TYPE_QUALS (element_type))
5793 orig_qual_type = type;
5794 type = TYPE_MAIN_VARIANT (type);
5796 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5797 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5798 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5799 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5800 | ENCODE_QUAL_ADDR_SPACE (address_space));
5801 if (type_quals != TYPE_QUALS (element_type))
5802 orig_qual_type = NULL_TREE;
5804 /* Applying the _Atomic qualifier to an array type (through the use
5805 of typedefs or typeof) must be detected here. If the qualifier
5806 is introduced later, any appearance of applying it to an array is
5807 actually applying it to an element of that array. */
5808 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5809 error_at (loc, "%<_Atomic%>-qualified array type");
5811 /* Warn about storage classes that are invalid for certain
5812 kinds of declarations (parameters, typenames, etc.). */
5814 if (funcdef_flag
5815 && (threadp
5816 || storage_class == csc_auto
5817 || storage_class == csc_register
5818 || storage_class == csc_typedef))
5820 if (storage_class == csc_auto)
5821 pedwarn (loc,
5822 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5823 "function definition declared %<auto%>");
5824 if (storage_class == csc_register)
5825 error_at (loc, "function definition declared %<register%>");
5826 if (storage_class == csc_typedef)
5827 error_at (loc, "function definition declared %<typedef%>");
5828 if (threadp)
5829 error_at (loc, "function definition declared %qs",
5830 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5831 threadp = false;
5832 if (storage_class == csc_auto
5833 || storage_class == csc_register
5834 || storage_class == csc_typedef)
5835 storage_class = csc_none;
5837 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5839 if (decl_context == PARM && storage_class == csc_register)
5841 else
5843 switch (decl_context)
5845 case FIELD:
5846 if (name)
5847 error_at (loc, "storage class specified for structure "
5848 "field %qE", name);
5849 else
5850 error_at (loc, "storage class specified for structure field");
5851 break;
5852 case PARM:
5853 if (name)
5854 error_at (loc, "storage class specified for parameter %qE",
5855 name);
5856 else
5857 error_at (loc, "storage class specified for unnamed parameter");
5858 break;
5859 default:
5860 error_at (loc, "storage class specified for typename");
5861 break;
5863 storage_class = csc_none;
5864 threadp = false;
5867 else if (storage_class == csc_extern
5868 && initialized
5869 && !funcdef_flag)
5871 /* 'extern' with initialization is invalid if not at file scope. */
5872 if (current_scope == file_scope)
5874 /* It is fine to have 'extern const' when compiling at C
5875 and C++ intersection. */
5876 if (!(warn_cxx_compat && constp))
5877 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5878 name);
5880 else
5881 error_at (loc, "%qE has both %<extern%> and initializer", name);
5883 else if (current_scope == file_scope)
5885 if (storage_class == csc_auto)
5886 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5887 name);
5888 if (pedantic && storage_class == csc_register)
5889 pedwarn (input_location, OPT_Wpedantic,
5890 "file-scope declaration of %qE specifies %<register%>", name);
5892 else
5894 if (storage_class == csc_extern && funcdef_flag)
5895 error_at (loc, "nested function %qE declared %<extern%>", name);
5896 else if (threadp && storage_class == csc_none)
5898 error_at (loc, "function-scope %qE implicitly auto and declared "
5899 "%qs", name,
5900 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5901 threadp = false;
5905 /* Now figure out the structure of the declarator proper.
5906 Descend through it, creating more complex types, until we reach
5907 the declared identifier (or NULL_TREE, in an absolute declarator).
5908 At each stage we maintain an unqualified version of the type
5909 together with any qualifiers that should be applied to it with
5910 c_build_qualified_type; this way, array types including
5911 multidimensional array types are first built up in unqualified
5912 form and then the qualified form is created with
5913 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5915 while (declarator && declarator->kind != cdk_id)
5917 if (type == error_mark_node)
5919 declarator = declarator->declarator;
5920 continue;
5923 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5924 a cdk_pointer (for *...),
5925 a cdk_function (for ...(...)),
5926 a cdk_attrs (for nested attributes),
5927 or a cdk_id (for the name being declared
5928 or the place in an absolute declarator
5929 where the name was omitted).
5930 For the last case, we have just exited the loop.
5932 At this point, TYPE is the type of elements of an array,
5933 or for a function to return, or for a pointer to point to.
5934 After this sequence of ifs, TYPE is the type of the
5935 array or function or pointer, and DECLARATOR has had its
5936 outermost layer removed. */
5938 if (array_ptr_quals != TYPE_UNQUALIFIED
5939 || array_ptr_attrs != NULL_TREE
5940 || array_parm_static)
5942 /* Only the innermost declarator (making a parameter be of
5943 array type which is converted to pointer type)
5944 may have static or type qualifiers. */
5945 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5946 array_ptr_quals = TYPE_UNQUALIFIED;
5947 array_ptr_attrs = NULL_TREE;
5948 array_parm_static = false;
5951 switch (declarator->kind)
5953 case cdk_attrs:
5955 /* A declarator with embedded attributes. */
5956 tree attrs = declarator->u.attrs;
5957 const struct c_declarator *inner_decl;
5958 int attr_flags = 0;
5959 declarator = declarator->declarator;
5960 inner_decl = declarator;
5961 while (inner_decl->kind == cdk_attrs)
5962 inner_decl = inner_decl->declarator;
5963 if (inner_decl->kind == cdk_id)
5964 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5965 else if (inner_decl->kind == cdk_function)
5966 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5967 else if (inner_decl->kind == cdk_array)
5968 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5969 returned_attrs = decl_attributes (&type,
5970 chainon (returned_attrs, attrs),
5971 attr_flags);
5972 break;
5974 case cdk_array:
5976 tree itype = NULL_TREE;
5977 tree size = declarator->u.array.dimen;
5978 /* The index is a signed object `sizetype' bits wide. */
5979 tree index_type = c_common_signed_type (sizetype);
5981 array_ptr_quals = declarator->u.array.quals;
5982 array_ptr_attrs = declarator->u.array.attrs;
5983 array_parm_static = declarator->u.array.static_p;
5984 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5986 declarator = declarator->declarator;
5988 /* Check for some types that there cannot be arrays of. */
5990 if (VOID_TYPE_P (type))
5992 if (name)
5993 error_at (loc, "declaration of %qE as array of voids", name);
5994 else
5995 error_at (loc, "declaration of type name as array of voids");
5996 type = error_mark_node;
5999 if (TREE_CODE (type) == FUNCTION_TYPE)
6001 if (name)
6002 error_at (loc, "declaration of %qE as array of functions",
6003 name);
6004 else
6005 error_at (loc, "declaration of type name as array of "
6006 "functions");
6007 type = error_mark_node;
6010 if (pedantic && !in_system_header_at (input_location)
6011 && flexible_array_type_p (type))
6012 pedwarn (loc, OPT_Wpedantic,
6013 "invalid use of structure with flexible array member");
6015 if (size == error_mark_node)
6016 type = error_mark_node;
6018 if (type == error_mark_node)
6019 continue;
6021 /* If size was specified, set ITYPE to a range-type for
6022 that size. Otherwise, ITYPE remains null. finish_decl
6023 may figure it out from an initial value. */
6025 if (size)
6027 bool size_maybe_const = true;
6028 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6029 && !TREE_OVERFLOW (size));
6030 bool this_size_varies = false;
6032 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6033 lvalue. */
6034 STRIP_TYPE_NOPS (size);
6036 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6038 if (name)
6039 error_at (loc, "size of array %qE has non-integer type",
6040 name);
6041 else
6042 error_at (loc,
6043 "size of unnamed array has non-integer type");
6044 size = integer_one_node;
6046 /* This can happen with enum forward declaration. */
6047 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6049 if (name)
6050 error_at (loc, "size of array %qE has incomplete type",
6051 name);
6052 else
6053 error_at (loc, "size of unnamed array has incomplete "
6054 "type");
6055 size = integer_one_node;
6058 size = c_fully_fold (size, false, &size_maybe_const);
6060 if (pedantic && size_maybe_const && integer_zerop (size))
6062 if (name)
6063 pedwarn (loc, OPT_Wpedantic,
6064 "ISO C forbids zero-size array %qE", name);
6065 else
6066 pedwarn (loc, OPT_Wpedantic,
6067 "ISO C forbids zero-size array");
6070 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6072 constant_expression_warning (size);
6073 if (tree_int_cst_sgn (size) < 0)
6075 if (name)
6076 error_at (loc, "size of array %qE is negative", name);
6077 else
6078 error_at (loc, "size of unnamed array is negative");
6079 size = integer_one_node;
6081 /* Handle a size folded to an integer constant but
6082 not an integer constant expression. */
6083 if (!size_int_const)
6085 /* If this is a file scope declaration of an
6086 ordinary identifier, this is invalid code;
6087 diagnosing it here and not subsequently
6088 treating the type as variable-length avoids
6089 more confusing diagnostics later. */
6090 if ((decl_context == NORMAL || decl_context == FIELD)
6091 && current_scope == file_scope)
6092 pedwarn (input_location, 0,
6093 "variably modified %qE at file scope",
6094 name);
6095 else
6096 this_size_varies = size_varies = true;
6097 warn_variable_length_array (name, size);
6100 else if ((decl_context == NORMAL || decl_context == FIELD)
6101 && current_scope == file_scope)
6103 error_at (loc, "variably modified %qE at file scope", name);
6104 size = integer_one_node;
6106 else
6108 /* Make sure the array size remains visibly
6109 nonconstant even if it is (eg) a const variable
6110 with known value. */
6111 this_size_varies = size_varies = true;
6112 warn_variable_length_array (name, size);
6113 if (sanitize_flags_p (SANITIZE_VLA)
6114 && current_function_decl != NULL_TREE
6115 && decl_context == NORMAL)
6117 /* Evaluate the array size only once. */
6118 size = save_expr (size);
6119 size = c_fully_fold (size, false, NULL);
6120 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6121 ubsan_instrument_vla (loc, size),
6122 size);
6126 if (integer_zerop (size) && !this_size_varies)
6128 /* A zero-length array cannot be represented with
6129 an unsigned index type, which is what we'll
6130 get with build_index_type. Create an
6131 open-ended range instead. */
6132 itype = build_range_type (sizetype, size, NULL_TREE);
6134 else
6136 /* Arrange for the SAVE_EXPR on the inside of the
6137 MINUS_EXPR, which allows the -1 to get folded
6138 with the +1 that happens when building TYPE_SIZE. */
6139 if (size_varies)
6140 size = save_expr (size);
6141 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6142 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6143 integer_zero_node, size);
6145 /* Compute the maximum valid index, that is, size
6146 - 1. Do the calculation in index_type, so that
6147 if it is a variable the computations will be
6148 done in the proper mode. */
6149 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6150 convert (index_type, size),
6151 convert (index_type,
6152 size_one_node));
6154 /* The above overflows when size does not fit
6155 in index_type.
6156 ??? While a size of INT_MAX+1 technically shouldn't
6157 cause an overflow (because we subtract 1), handling
6158 this case seems like an unnecessary complication. */
6159 if (TREE_CODE (size) == INTEGER_CST
6160 && !int_fits_type_p (size, index_type))
6162 if (name)
6163 error_at (loc, "size of array %qE is too large",
6164 name);
6165 else
6166 error_at (loc, "size of unnamed array is too large");
6167 type = error_mark_node;
6168 continue;
6171 itype = build_index_type (itype);
6173 if (this_size_varies)
6175 if (*expr)
6176 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6177 *expr, size);
6178 else
6179 *expr = size;
6180 *expr_const_operands &= size_maybe_const;
6183 else if (decl_context == FIELD)
6185 bool flexible_array_member = false;
6186 if (array_parm_vla_unspec_p)
6187 /* Field names can in fact have function prototype
6188 scope so [*] is disallowed here through making
6189 the field variably modified, not through being
6190 something other than a declaration with function
6191 prototype scope. */
6192 size_varies = true;
6193 else
6195 const struct c_declarator *t = declarator;
6196 while (t->kind == cdk_attrs)
6197 t = t->declarator;
6198 flexible_array_member = (t->kind == cdk_id);
6200 if (flexible_array_member
6201 && !in_system_header_at (input_location))
6202 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6203 "support flexible array members");
6205 /* ISO C99 Flexible array members are effectively
6206 identical to GCC's zero-length array extension. */
6207 if (flexible_array_member || array_parm_vla_unspec_p)
6208 itype = build_range_type (sizetype, size_zero_node,
6209 NULL_TREE);
6211 else if (decl_context == PARM)
6213 if (array_parm_vla_unspec_p)
6215 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6216 size_varies = true;
6219 else if (decl_context == TYPENAME)
6221 if (array_parm_vla_unspec_p)
6223 /* C99 6.7.5.2p4 */
6224 warning (0, "%<[*]%> not in a declaration");
6225 /* We use this to avoid messing up with incomplete
6226 array types of the same type, that would
6227 otherwise be modified below. */
6228 itype = build_range_type (sizetype, size_zero_node,
6229 NULL_TREE);
6230 size_varies = true;
6234 /* Complain about arrays of incomplete types. */
6235 if (!COMPLETE_TYPE_P (type))
6237 error_at (loc, "array type has incomplete element type %qT",
6238 type);
6239 /* See if we can be more helpful. */
6240 if (TREE_CODE (type) == ARRAY_TYPE)
6242 if (name)
6243 inform (loc, "declaration of %qE as multidimensional "
6244 "array must have bounds for all dimensions "
6245 "except the first", name);
6246 else
6247 inform (loc, "declaration of multidimensional array "
6248 "must have bounds for all dimensions except "
6249 "the first");
6251 type = error_mark_node;
6253 else
6254 /* When itype is NULL, a shared incomplete array type is
6255 returned for all array of a given type. Elsewhere we
6256 make sure we don't complete that type before copying
6257 it, but here we want to make sure we don't ever
6258 modify the shared type, so we gcc_assert (itype)
6259 below. */
6261 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6262 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6263 type = build_qualified_type (type,
6264 ENCODE_QUAL_ADDR_SPACE (as));
6266 type = build_array_type (type, itype);
6269 if (type != error_mark_node)
6271 if (size_varies)
6273 /* It is ok to modify type here even if itype is
6274 NULL: if size_varies, we're in a
6275 multi-dimensional array and the inner type has
6276 variable size, so the enclosing shared array type
6277 must too. */
6278 if (size && TREE_CODE (size) == INTEGER_CST)
6279 type
6280 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6281 C_TYPE_VARIABLE_SIZE (type) = 1;
6284 /* The GCC extension for zero-length arrays differs from
6285 ISO flexible array members in that sizeof yields
6286 zero. */
6287 if (size && integer_zerop (size))
6289 gcc_assert (itype);
6290 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6291 TYPE_SIZE (type) = bitsize_zero_node;
6292 TYPE_SIZE_UNIT (type) = size_zero_node;
6293 SET_TYPE_STRUCTURAL_EQUALITY (type);
6295 if (array_parm_vla_unspec_p)
6297 gcc_assert (itype);
6298 /* The type is complete. C99 6.7.5.2p4 */
6299 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6300 TYPE_SIZE (type) = bitsize_zero_node;
6301 TYPE_SIZE_UNIT (type) = size_zero_node;
6302 SET_TYPE_STRUCTURAL_EQUALITY (type);
6305 if (!valid_array_size_p (loc, type, name))
6306 type = error_mark_node;
6309 if (decl_context != PARM
6310 && (array_ptr_quals != TYPE_UNQUALIFIED
6311 || array_ptr_attrs != NULL_TREE
6312 || array_parm_static))
6314 error_at (loc, "static or type qualifiers in non-parameter "
6315 "array declarator");
6316 array_ptr_quals = TYPE_UNQUALIFIED;
6317 array_ptr_attrs = NULL_TREE;
6318 array_parm_static = false;
6320 orig_qual_indirect++;
6321 break;
6323 case cdk_function:
6325 /* Say it's a definition only for the declarator closest
6326 to the identifier, apart possibly from some
6327 attributes. */
6328 bool really_funcdef = false;
6329 tree arg_types;
6330 orig_qual_type = NULL_TREE;
6331 if (funcdef_flag)
6333 const struct c_declarator *t = declarator->declarator;
6334 while (t->kind == cdk_attrs)
6335 t = t->declarator;
6336 really_funcdef = (t->kind == cdk_id);
6339 /* Declaring a function type. Make sure we have a valid
6340 type for the function to return. */
6341 if (type == error_mark_node)
6342 continue;
6344 size_varies = false;
6346 /* Warn about some types functions can't return. */
6347 if (TREE_CODE (type) == FUNCTION_TYPE)
6349 if (name)
6350 error_at (loc, "%qE declared as function returning a "
6351 "function", name);
6352 else
6353 error_at (loc, "type name declared as function "
6354 "returning a function");
6355 type = integer_type_node;
6357 if (TREE_CODE (type) == ARRAY_TYPE)
6359 if (name)
6360 error_at (loc, "%qE declared as function returning an array",
6361 name);
6362 else
6363 error_at (loc, "type name declared as function returning "
6364 "an array");
6365 type = integer_type_node;
6368 /* Construct the function type and go to the next
6369 inner layer of declarator. */
6370 arg_info = declarator->u.arg_info;
6371 arg_types = grokparms (arg_info, really_funcdef);
6373 /* Type qualifiers before the return type of the function
6374 qualify the return type, not the function type. */
6375 if (type_quals)
6377 const enum c_declspec_word ignored_quals_list[] =
6379 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6380 cdw_atomic, cdw_number_of_elements
6382 location_t specs_loc
6383 = smallest_type_quals_location (declspecs->locations,
6384 ignored_quals_list);
6385 if (specs_loc == UNKNOWN_LOCATION)
6386 specs_loc = declspecs->locations[cdw_typedef];
6387 if (specs_loc == UNKNOWN_LOCATION)
6388 specs_loc = loc;
6390 /* Type qualifiers on a function return type are
6391 normally permitted by the standard but have no
6392 effect, so give a warning at -Wreturn-type.
6393 Qualifiers on a void return type are banned on
6394 function definitions in ISO C; GCC used to used
6395 them for noreturn functions. The resolution of C11
6396 DR#423 means qualifiers (other than _Atomic) are
6397 actually removed from the return type when
6398 determining the function type. */
6399 int quals_used = type_quals;
6400 if (flag_isoc11)
6401 quals_used &= TYPE_QUAL_ATOMIC;
6402 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6403 pedwarn (specs_loc, 0,
6404 "function definition has qualified void return type");
6405 else
6406 warning_at (specs_loc, OPT_Wignored_qualifiers,
6407 "type qualifiers ignored on function return type");
6409 /* Ensure an error for restrict on invalid types; the
6410 DR#423 resolution is not entirely clear about
6411 this. */
6412 if (flag_isoc11
6413 && (type_quals & TYPE_QUAL_RESTRICT)
6414 && (!POINTER_TYPE_P (type)
6415 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6416 error_at (loc, "invalid use of %<restrict%>");
6417 if (quals_used)
6418 type = c_build_qualified_type (type, quals_used);
6420 type_quals = TYPE_UNQUALIFIED;
6422 type = build_function_type (type, arg_types);
6423 declarator = declarator->declarator;
6425 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6426 the formal parameter list of this FUNCTION_TYPE to point to
6427 the FUNCTION_TYPE node itself. */
6429 c_arg_tag *tag;
6430 unsigned ix;
6432 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6433 TYPE_CONTEXT (tag->type) = type;
6435 break;
6437 case cdk_pointer:
6439 /* Merge any constancy or volatility into the target type
6440 for the pointer. */
6441 if ((type_quals & TYPE_QUAL_ATOMIC)
6442 && TREE_CODE (type) == FUNCTION_TYPE)
6444 error_at (loc,
6445 "%<_Atomic%>-qualified function type");
6446 type_quals &= ~TYPE_QUAL_ATOMIC;
6448 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6449 && type_quals)
6450 pedwarn (loc, OPT_Wpedantic,
6451 "ISO C forbids qualified function types");
6452 if (type_quals)
6453 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6454 orig_qual_indirect);
6455 orig_qual_type = NULL_TREE;
6456 size_varies = false;
6458 /* When the pointed-to type involves components of variable size,
6459 care must be taken to ensure that the size evaluation code is
6460 emitted early enough to dominate all the possible later uses
6461 and late enough for the variables on which it depends to have
6462 been assigned.
6464 This is expected to happen automatically when the pointed-to
6465 type has a name/declaration of it's own, but special attention
6466 is required if the type is anonymous.
6468 We handle the NORMAL and FIELD contexts here by attaching an
6469 artificial TYPE_DECL to such pointed-to type. This forces the
6470 sizes evaluation at a safe point and ensures it is not deferred
6471 until e.g. within a deeper conditional context.
6473 We expect nothing to be needed here for PARM or TYPENAME.
6474 Pushing a TYPE_DECL at this point for TYPENAME would actually
6475 be incorrect, as we might be in the middle of an expression
6476 with side effects on the pointed-to type size "arguments" prior
6477 to the pointer declaration point and the fake TYPE_DECL in the
6478 enclosing context would force the size evaluation prior to the
6479 side effects. */
6481 if (!TYPE_NAME (type)
6482 && (decl_context == NORMAL || decl_context == FIELD)
6483 && variably_modified_type_p (type, NULL_TREE))
6485 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6486 DECL_ARTIFICIAL (decl) = 1;
6487 pushdecl (decl);
6488 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6489 TYPE_NAME (type) = decl;
6492 type = c_build_pointer_type (type);
6494 /* Process type qualifiers (such as const or volatile)
6495 that were given inside the `*'. */
6496 type_quals = declarator->u.pointer_quals;
6498 declarator = declarator->declarator;
6499 break;
6501 default:
6502 gcc_unreachable ();
6505 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6507 /* Now TYPE has the actual type, apart from any qualifiers in
6508 TYPE_QUALS. */
6510 /* Warn about address space used for things other than static memory or
6511 pointers. */
6512 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6513 if (!ADDR_SPACE_GENERIC_P (address_space))
6515 if (decl_context == NORMAL)
6517 switch (storage_class)
6519 case csc_auto:
6520 error ("%qs combined with %<auto%> qualifier for %qE",
6521 c_addr_space_name (address_space), name);
6522 break;
6523 case csc_register:
6524 error ("%qs combined with %<register%> qualifier for %qE",
6525 c_addr_space_name (address_space), name);
6526 break;
6527 case csc_none:
6528 if (current_function_scope)
6530 error ("%qs specified for auto variable %qE",
6531 c_addr_space_name (address_space), name);
6532 break;
6534 break;
6535 case csc_static:
6536 case csc_extern:
6537 case csc_typedef:
6538 break;
6539 default:
6540 gcc_unreachable ();
6543 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6545 if (name)
6546 error ("%qs specified for parameter %qE",
6547 c_addr_space_name (address_space), name);
6548 else
6549 error ("%qs specified for unnamed parameter",
6550 c_addr_space_name (address_space));
6552 else if (decl_context == FIELD)
6554 if (name)
6555 error ("%qs specified for structure field %qE",
6556 c_addr_space_name (address_space), name);
6557 else
6558 error ("%qs specified for structure field",
6559 c_addr_space_name (address_space));
6563 /* Check the type and width of a bit-field. */
6564 if (bitfield)
6566 check_bitfield_type_and_width (loc, &type, width, name);
6567 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6568 atomic types are permitted for bit-fields; we have no code to
6569 make bit-field accesses atomic, so disallow them. */
6570 if (type_quals & TYPE_QUAL_ATOMIC)
6572 if (name)
6573 error_at (loc, "bit-field %qE has atomic type", name);
6574 else
6575 error_at (loc, "bit-field has atomic type");
6576 type_quals &= ~TYPE_QUAL_ATOMIC;
6580 /* Reject invalid uses of _Alignas. */
6581 if (declspecs->alignas_p)
6583 if (storage_class == csc_typedef)
6584 error_at (loc, "alignment specified for typedef %qE", name);
6585 else if (storage_class == csc_register)
6586 error_at (loc, "alignment specified for %<register%> object %qE",
6587 name);
6588 else if (decl_context == PARM)
6590 if (name)
6591 error_at (loc, "alignment specified for parameter %qE", name);
6592 else
6593 error_at (loc, "alignment specified for unnamed parameter");
6595 else if (bitfield)
6597 if (name)
6598 error_at (loc, "alignment specified for bit-field %qE", name);
6599 else
6600 error_at (loc, "alignment specified for unnamed bit-field");
6602 else if (TREE_CODE (type) == FUNCTION_TYPE)
6603 error_at (loc, "alignment specified for function %qE", name);
6604 else if (declspecs->align_log != -1 && TYPE_P (type))
6606 alignas_align = 1U << declspecs->align_log;
6607 if (alignas_align < min_align_of_type (type))
6609 if (name)
6610 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6611 "alignment of %qE", name);
6612 else
6613 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6614 "alignment of unnamed field");
6615 alignas_align = 0;
6620 /* If this is declaring a typedef name, return a TYPE_DECL. */
6622 if (storage_class == csc_typedef)
6624 tree decl;
6625 if ((type_quals & TYPE_QUAL_ATOMIC)
6626 && TREE_CODE (type) == FUNCTION_TYPE)
6628 error_at (loc,
6629 "%<_Atomic%>-qualified function type");
6630 type_quals &= ~TYPE_QUAL_ATOMIC;
6632 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6633 && type_quals)
6634 pedwarn (loc, OPT_Wpedantic,
6635 "ISO C forbids qualified function types");
6636 if (type_quals)
6637 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6638 orig_qual_indirect);
6639 decl = build_decl (declarator->id_loc,
6640 TYPE_DECL, declarator->u.id, type);
6641 if (declspecs->explicit_signed_p)
6642 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6643 if (declspecs->inline_p)
6644 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6645 if (declspecs->noreturn_p)
6646 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6648 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6650 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6652 if (b != NULL
6653 && b->decl != NULL_TREE
6654 && (B_IN_CURRENT_SCOPE (b)
6655 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6656 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6658 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6659 ("using %qD as both a typedef and a tag is "
6660 "invalid in C++"), decl)
6661 && b->locus != UNKNOWN_LOCATION)
6662 inform (b->locus, "originally defined here");
6666 return decl;
6669 /* If this is a type name (such as, in a cast or sizeof),
6670 compute the type and return it now. */
6672 if (decl_context == TYPENAME)
6674 /* Note that the grammar rejects storage classes in typenames
6675 and fields. */
6676 gcc_assert (storage_class == csc_none && !threadp
6677 && !declspecs->inline_p && !declspecs->noreturn_p);
6678 if ((type_quals & TYPE_QUAL_ATOMIC)
6679 && TREE_CODE (type) == FUNCTION_TYPE)
6681 error_at (loc,
6682 "%<_Atomic%>-qualified function type");
6683 type_quals &= ~TYPE_QUAL_ATOMIC;
6685 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6686 && type_quals)
6687 pedwarn (loc, OPT_Wpedantic,
6688 "ISO C forbids const or volatile function types");
6689 if (type_quals)
6690 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6691 orig_qual_indirect);
6692 return type;
6695 if (pedantic && decl_context == FIELD
6696 && variably_modified_type_p (type, NULL_TREE))
6698 /* C99 6.7.2.1p8 */
6699 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6700 "have a variably modified type");
6703 /* Aside from typedefs and type names (handle above),
6704 `void' at top level (not within pointer)
6705 is allowed only in public variables.
6706 We don't complain about parms either, but that is because
6707 a better error message can be made later. */
6709 if (VOID_TYPE_P (type) && decl_context != PARM
6710 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6711 && (storage_class == csc_extern
6712 || (current_scope == file_scope
6713 && !(storage_class == csc_static
6714 || storage_class == csc_register)))))
6716 error_at (loc, "variable or field %qE declared void", name);
6717 type = integer_type_node;
6720 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6721 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6724 tree decl;
6726 if (decl_context == PARM)
6728 tree promoted_type;
6729 bool array_parameter_p = false;
6731 /* A parameter declared as an array of T is really a pointer to T.
6732 One declared as a function is really a pointer to a function. */
6734 if (TREE_CODE (type) == ARRAY_TYPE)
6736 /* Transfer const-ness of array into that of type pointed to. */
6737 type = TREE_TYPE (type);
6738 if (orig_qual_type != NULL_TREE)
6740 if (orig_qual_indirect == 0)
6741 orig_qual_type = TREE_TYPE (orig_qual_type);
6742 else
6743 orig_qual_indirect--;
6745 if (type_quals)
6746 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6747 orig_qual_indirect);
6748 type = c_build_pointer_type (type);
6749 type_quals = array_ptr_quals;
6750 if (type_quals)
6751 type = c_build_qualified_type (type, type_quals);
6753 /* We don't yet implement attributes in this context. */
6754 if (array_ptr_attrs != NULL_TREE)
6755 warning_at (loc, OPT_Wattributes,
6756 "attributes in parameter array declarator ignored");
6758 size_varies = false;
6759 array_parameter_p = true;
6761 else if (TREE_CODE (type) == FUNCTION_TYPE)
6763 if (type_quals & TYPE_QUAL_ATOMIC)
6765 error_at (loc,
6766 "%<_Atomic%>-qualified function type");
6767 type_quals &= ~TYPE_QUAL_ATOMIC;
6769 else if (type_quals)
6770 pedwarn (loc, OPT_Wpedantic,
6771 "ISO C forbids qualified function types");
6772 if (type_quals)
6773 type = c_build_qualified_type (type, type_quals);
6774 type = c_build_pointer_type (type);
6775 type_quals = TYPE_UNQUALIFIED;
6777 else if (type_quals)
6778 type = c_build_qualified_type (type, type_quals);
6780 decl = build_decl (declarator->id_loc,
6781 PARM_DECL, declarator->u.id, type);
6782 if (size_varies)
6783 C_DECL_VARIABLE_SIZE (decl) = 1;
6784 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6786 /* Compute the type actually passed in the parmlist,
6787 for the case where there is no prototype.
6788 (For example, shorts and chars are passed as ints.)
6789 When there is a prototype, this is overridden later. */
6791 if (type == error_mark_node)
6792 promoted_type = type;
6793 else
6794 promoted_type = c_type_promotes_to (type);
6796 DECL_ARG_TYPE (decl) = promoted_type;
6797 if (declspecs->inline_p)
6798 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6799 if (declspecs->noreturn_p)
6800 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6802 else if (decl_context == FIELD)
6804 /* Note that the grammar rejects storage classes in typenames
6805 and fields. */
6806 gcc_assert (storage_class == csc_none && !threadp
6807 && !declspecs->inline_p && !declspecs->noreturn_p);
6809 /* Structure field. It may not be a function. */
6811 if (TREE_CODE (type) == FUNCTION_TYPE)
6813 error_at (loc, "field %qE declared as a function", name);
6814 type = build_pointer_type (type);
6816 else if (TREE_CODE (type) != ERROR_MARK
6817 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6819 if (name)
6820 error_at (loc, "field %qE has incomplete type", name);
6821 else
6822 error_at (loc, "unnamed field has incomplete type");
6823 type = error_mark_node;
6825 else if (TREE_CODE (type) == ARRAY_TYPE
6826 && TYPE_DOMAIN (type) == NULL_TREE)
6828 /* We have a flexible array member through a typedef.
6829 Set suitable range. Whether this is a correct position
6830 for a flexible array member will be determined elsewhere. */
6831 if (!in_system_header_at (input_location))
6832 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6833 "support flexible array members");
6834 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6835 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6836 NULL_TREE);
6837 if (orig_qual_indirect == 0)
6838 orig_qual_type = NULL_TREE;
6840 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6841 orig_qual_indirect);
6842 decl = build_decl (declarator->id_loc,
6843 FIELD_DECL, declarator->u.id, type);
6844 DECL_NONADDRESSABLE_P (decl) = bitfield;
6845 if (bitfield && !declarator->u.id)
6847 TREE_NO_WARNING (decl) = 1;
6848 DECL_PADDING_P (decl) = 1;
6851 if (size_varies)
6852 C_DECL_VARIABLE_SIZE (decl) = 1;
6854 else if (TREE_CODE (type) == FUNCTION_TYPE)
6856 if (storage_class == csc_register || threadp)
6858 error_at (loc, "invalid storage class for function %qE", name);
6860 else if (current_scope != file_scope)
6862 /* Function declaration not at file scope. Storage
6863 classes other than `extern' are not allowed, C99
6864 6.7.1p5, and `extern' makes no difference. However,
6865 GCC allows 'auto', perhaps with 'inline', to support
6866 nested functions. */
6867 if (storage_class == csc_auto)
6868 pedwarn (loc, OPT_Wpedantic,
6869 "invalid storage class for function %qE", name);
6870 else if (storage_class == csc_static)
6872 error_at (loc, "invalid storage class for function %qE", name);
6873 if (funcdef_flag)
6874 storage_class = declspecs->storage_class = csc_none;
6875 else
6876 return NULL_TREE;
6880 decl = build_decl (declarator->id_loc,
6881 FUNCTION_DECL, declarator->u.id, type);
6882 decl = build_decl_attribute_variant (decl, decl_attr);
6884 if (type_quals & TYPE_QUAL_ATOMIC)
6886 error_at (loc,
6887 "%<_Atomic%>-qualified function type");
6888 type_quals &= ~TYPE_QUAL_ATOMIC;
6890 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6891 pedwarn (loc, OPT_Wpedantic,
6892 "ISO C forbids qualified function types");
6894 /* Every function declaration is an external reference
6895 (DECL_EXTERNAL) except for those which are not at file
6896 scope and are explicitly declared "auto". This is
6897 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6898 GCC to signify a forward declaration of a nested function. */
6899 if (storage_class == csc_auto && current_scope != file_scope)
6900 DECL_EXTERNAL (decl) = 0;
6901 /* In C99, a function which is declared 'inline' with 'extern'
6902 is not an external reference (which is confusing). It
6903 means that the later definition of the function must be output
6904 in this file, C99 6.7.4p6. In GNU C89, a function declared
6905 'extern inline' is an external reference. */
6906 else if (declspecs->inline_p && storage_class != csc_static)
6907 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6908 == flag_gnu89_inline);
6909 else
6910 DECL_EXTERNAL (decl) = !initialized;
6912 /* Record absence of global scope for `static' or `auto'. */
6913 TREE_PUBLIC (decl)
6914 = !(storage_class == csc_static || storage_class == csc_auto);
6916 /* For a function definition, record the argument information
6917 block where store_parm_decls will look for it. */
6918 if (funcdef_flag)
6919 current_function_arg_info = arg_info;
6921 if (declspecs->default_int_p)
6922 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6924 /* Record presence of `inline' and `_Noreturn', if it is
6925 reasonable. */
6926 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6928 if (declspecs->inline_p)
6929 pedwarn (loc, 0, "cannot inline function %<main%>");
6930 if (declspecs->noreturn_p)
6931 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6933 else
6935 if (declspecs->inline_p)
6936 /* Record that the function is declared `inline'. */
6937 DECL_DECLARED_INLINE_P (decl) = 1;
6938 if (declspecs->noreturn_p)
6940 if (flag_isoc99)
6941 pedwarn_c99 (loc, OPT_Wpedantic,
6942 "ISO C99 does not support %<_Noreturn%>");
6943 else
6944 pedwarn_c99 (loc, OPT_Wpedantic,
6945 "ISO C90 does not support %<_Noreturn%>");
6946 TREE_THIS_VOLATILE (decl) = 1;
6950 else
6952 /* It's a variable. */
6953 /* An uninitialized decl with `extern' is a reference. */
6954 int extern_ref = !initialized && storage_class == csc_extern;
6956 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6957 orig_qual_indirect);
6959 /* C99 6.2.2p7: It is invalid (compile-time undefined
6960 behavior) to create an 'extern' declaration for a
6961 variable if there is a global declaration that is
6962 'static' and the global declaration is not visible.
6963 (If the static declaration _is_ currently visible,
6964 the 'extern' declaration is taken to refer to that decl.) */
6965 if (extern_ref && current_scope != file_scope)
6967 tree global_decl = identifier_global_value (declarator->u.id);
6968 tree visible_decl = lookup_name (declarator->u.id);
6970 if (global_decl
6971 && global_decl != visible_decl
6972 && VAR_P (global_decl)
6973 && !TREE_PUBLIC (global_decl))
6974 error_at (loc, "variable previously declared %<static%> "
6975 "redeclared %<extern%>");
6978 decl = build_decl (declarator->id_loc,
6979 VAR_DECL, declarator->u.id, type);
6980 if (size_varies)
6981 C_DECL_VARIABLE_SIZE (decl) = 1;
6983 if (declspecs->inline_p)
6984 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6985 if (declspecs->noreturn_p)
6986 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6988 /* At file scope, an initialized extern declaration may follow
6989 a static declaration. In that case, DECL_EXTERNAL will be
6990 reset later in start_decl. */
6991 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6993 /* At file scope, the presence of a `static' or `register' storage
6994 class specifier, or the absence of all storage class specifiers
6995 makes this declaration a definition (perhaps tentative). Also,
6996 the absence of `static' makes it public. */
6997 if (current_scope == file_scope)
6999 TREE_PUBLIC (decl) = storage_class != csc_static;
7000 TREE_STATIC (decl) = !extern_ref;
7002 /* Not at file scope, only `static' makes a static definition. */
7003 else
7005 TREE_STATIC (decl) = (storage_class == csc_static);
7006 TREE_PUBLIC (decl) = extern_ref;
7009 if (threadp)
7010 set_decl_tls_model (decl, decl_default_tls_model (decl));
7013 if ((storage_class == csc_extern
7014 || (storage_class == csc_none
7015 && TREE_CODE (type) == FUNCTION_TYPE
7016 && !funcdef_flag))
7017 && variably_modified_type_p (type, NULL_TREE))
7019 /* C99 6.7.5.2p2 */
7020 if (TREE_CODE (type) == FUNCTION_TYPE)
7021 error_at (loc, "non-nested function with variably modified type");
7022 else
7023 error_at (loc, "object with variably modified type must have "
7024 "no linkage");
7027 /* Record `register' declaration for warnings on &
7028 and in case doing stupid register allocation. */
7030 if (storage_class == csc_register)
7032 C_DECL_REGISTER (decl) = 1;
7033 DECL_REGISTER (decl) = 1;
7036 /* Record constancy and volatility. */
7037 c_apply_type_quals_to_decl (type_quals, decl);
7039 /* Apply _Alignas specifiers. */
7040 if (alignas_align)
7042 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7043 DECL_USER_ALIGN (decl) = 1;
7046 /* If a type has volatile components, it should be stored in memory.
7047 Otherwise, the fact that those components are volatile
7048 will be ignored, and would even crash the compiler.
7049 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7050 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7051 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7052 || TREE_CODE (decl) == RESULT_DECL))
7054 /* It is not an error for a structure with volatile fields to
7055 be declared register, but reset DECL_REGISTER since it
7056 cannot actually go in a register. */
7057 int was_reg = C_DECL_REGISTER (decl);
7058 C_DECL_REGISTER (decl) = 0;
7059 DECL_REGISTER (decl) = 0;
7060 c_mark_addressable (decl);
7061 C_DECL_REGISTER (decl) = was_reg;
7064 /* This is the earliest point at which we might know the assembler
7065 name of a variable. Thus, if it's known before this, die horribly. */
7066 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7067 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7069 if (warn_cxx_compat
7070 && VAR_P (decl)
7071 && TREE_PUBLIC (decl)
7072 && TREE_STATIC (decl)
7073 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7074 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7075 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7076 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7077 ("non-local variable %qD with anonymous type is "
7078 "questionable in C++"),
7079 decl);
7081 return decl;
7085 /* Decode the parameter-list info for a function type or function definition.
7086 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7087 if there is an identifier list instead of a parameter decl list).
7088 These two functions are separate because when a function returns
7089 or receives functions then each is called multiple times but the order
7090 of calls is different. The last call to `grokparms' is always the one
7091 that contains the formal parameter names of a function definition.
7093 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7095 FUNCDEF_FLAG is true for a function definition, false for
7096 a mere declaration. A nonempty identifier-list gets an error message
7097 when FUNCDEF_FLAG is false. */
7099 static tree
7100 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7102 tree arg_types = arg_info->types;
7104 if (funcdef_flag && arg_info->had_vla_unspec)
7106 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7107 /* C99 6.7.5.2p4 */
7108 error ("%<[*]%> not allowed in other than function prototype scope");
7111 if (arg_types == NULL_TREE && !funcdef_flag
7112 && !in_system_header_at (input_location))
7113 warning (OPT_Wstrict_prototypes,
7114 "function declaration isn%'t a prototype");
7116 if (arg_types == error_mark_node)
7117 /* Don't set TYPE_ARG_TYPES in this case. */
7118 return NULL_TREE;
7120 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7122 if (!funcdef_flag)
7124 pedwarn (input_location, 0, "parameter names (without types) in "
7125 "function declaration");
7126 arg_info->parms = NULL_TREE;
7128 else
7129 arg_info->parms = arg_info->types;
7131 arg_info->types = NULL_TREE;
7132 return NULL_TREE;
7134 else
7136 tree parm, type, typelt;
7137 unsigned int parmno;
7139 /* If there is a parameter of incomplete type in a definition,
7140 this is an error. In a declaration this is valid, and a
7141 struct or union type may be completed later, before any calls
7142 or definition of the function. In the case where the tag was
7143 first declared within the parameter list, a warning has
7144 already been given. If a parameter has void type, then
7145 however the function cannot be defined or called, so
7146 warn. */
7148 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7149 parm;
7150 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7152 type = TREE_VALUE (typelt);
7153 if (type == error_mark_node)
7154 continue;
7156 if (!COMPLETE_TYPE_P (type))
7158 if (funcdef_flag)
7160 if (DECL_NAME (parm))
7161 error_at (input_location,
7162 "parameter %u (%q+D) has incomplete type",
7163 parmno, parm);
7164 else
7165 error_at (DECL_SOURCE_LOCATION (parm),
7166 "parameter %u has incomplete type",
7167 parmno);
7169 TREE_VALUE (typelt) = error_mark_node;
7170 TREE_TYPE (parm) = error_mark_node;
7171 arg_types = NULL_TREE;
7173 else if (VOID_TYPE_P (type))
7175 if (DECL_NAME (parm))
7176 warning_at (input_location, 0,
7177 "parameter %u (%q+D) has void type",
7178 parmno, parm);
7179 else
7180 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7181 "parameter %u has void type",
7182 parmno);
7186 if (DECL_NAME (parm) && TREE_USED (parm))
7187 warn_if_shadowing (parm);
7189 return arg_types;
7193 /* Allocate and initialize a c_arg_info structure from the parser's
7194 obstack. */
7196 struct c_arg_info *
7197 build_arg_info (void)
7199 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7200 ret->parms = NULL_TREE;
7201 ret->tags = NULL;
7202 ret->types = NULL_TREE;
7203 ret->others = NULL_TREE;
7204 ret->pending_sizes = NULL;
7205 ret->had_vla_unspec = 0;
7206 return ret;
7209 /* Take apart the current scope and return a c_arg_info structure with
7210 info on a parameter list just parsed.
7212 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7214 ELLIPSIS being true means the argument list ended in '...' so don't
7215 append a sentinel (void_list_node) to the end of the type-list.
7217 EXPR is NULL or an expression that needs to be evaluated for the
7218 side effects of array size expressions in the parameters. */
7220 struct c_arg_info *
7221 get_parm_info (bool ellipsis, tree expr)
7223 struct c_binding *b = current_scope->bindings;
7224 struct c_arg_info *arg_info = build_arg_info ();
7226 tree parms = NULL_TREE;
7227 vec<c_arg_tag, va_gc> *tags = NULL;
7228 tree types = NULL_TREE;
7229 tree others = NULL_TREE;
7231 bool gave_void_only_once_err = false;
7233 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7235 /* The bindings in this scope must not get put into a block.
7236 We will take care of deleting the binding nodes. */
7237 current_scope->bindings = 0;
7239 /* This function is only called if there was *something* on the
7240 parameter list. */
7241 gcc_assert (b);
7243 /* A parameter list consisting solely of 'void' indicates that the
7244 function takes no arguments. But if the 'void' is qualified
7245 (by 'const' or 'volatile'), or has a storage class specifier
7246 ('register'), then the behavior is undefined; issue an error.
7247 Typedefs for 'void' are OK (see DR#157). */
7248 if (b->prev == 0 /* one binding */
7249 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7250 && !DECL_NAME (b->decl) /* anonymous */
7251 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7253 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7254 || C_DECL_REGISTER (b->decl))
7255 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7257 /* There cannot be an ellipsis. */
7258 if (ellipsis)
7259 error_at (b->locus, "%<void%> must be the only parameter");
7261 arg_info->types = void_list_node;
7262 return arg_info;
7265 if (!ellipsis)
7266 types = void_list_node;
7268 /* Break up the bindings list into parms, tags, types, and others;
7269 apply sanity checks; purge the name-to-decl bindings. */
7270 while (b)
7272 tree decl = b->decl;
7273 tree type = TREE_TYPE (decl);
7274 c_arg_tag tag;
7275 const char *keyword;
7277 switch (TREE_CODE (decl))
7279 case PARM_DECL:
7280 if (b->id)
7282 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7283 I_SYMBOL_BINDING (b->id) = b->shadowed;
7286 /* Check for forward decls that never got their actual decl. */
7287 if (TREE_ASM_WRITTEN (decl))
7288 error_at (b->locus,
7289 "parameter %q+D has just a forward declaration", decl);
7290 /* Check for (..., void, ...) and issue an error. */
7291 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7293 if (!gave_void_only_once_err)
7295 error_at (b->locus, "%<void%> must be the only parameter");
7296 gave_void_only_once_err = true;
7299 else
7301 /* Valid parameter, add it to the list. */
7302 DECL_CHAIN (decl) = parms;
7303 parms = decl;
7305 /* Since there is a prototype, args are passed in their
7306 declared types. The back end may override this later. */
7307 DECL_ARG_TYPE (decl) = type;
7308 types = tree_cons (0, type, types);
7310 break;
7312 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7313 case UNION_TYPE: keyword = "union"; goto tag;
7314 case RECORD_TYPE: keyword = "struct"; goto tag;
7315 tag:
7316 /* Types may not have tag-names, in which case the type
7317 appears in the bindings list with b->id NULL. */
7318 if (b->id)
7320 gcc_assert (I_TAG_BINDING (b->id) == b);
7321 I_TAG_BINDING (b->id) = b->shadowed;
7324 /* Warn about any struct, union or enum tags defined in a
7325 parameter list. The scope of such types is limited to
7326 the parameter list, which is rarely if ever desirable
7327 (it's impossible to call such a function with type-
7328 correct arguments). An anonymous union parm type is
7329 meaningful as a GNU extension, so don't warn for that. */
7330 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7332 if (b->id)
7333 /* The %s will be one of 'struct', 'union', or 'enum'. */
7334 warning_at (b->locus, 0,
7335 "%<%s %E%> declared inside parameter list"
7336 " will not be visible outside of this definition or"
7337 " declaration", keyword, b->id);
7338 else
7339 /* The %s will be one of 'struct', 'union', or 'enum'. */
7340 warning_at (b->locus, 0,
7341 "anonymous %s declared inside parameter list"
7342 " will not be visible outside of this definition or"
7343 " declaration", keyword);
7346 tag.id = b->id;
7347 tag.type = decl;
7348 vec_safe_push (tags, tag);
7349 break;
7351 case FUNCTION_DECL:
7352 /* FUNCTION_DECLs appear when there is an implicit function
7353 declaration in the parameter list. */
7354 gcc_assert (b->nested || seen_error ());
7355 goto set_shadowed;
7357 case CONST_DECL:
7358 case TYPE_DECL:
7359 /* CONST_DECLs appear here when we have an embedded enum,
7360 and TYPE_DECLs appear here when we have an embedded struct
7361 or union. No warnings for this - we already warned about the
7362 type itself. */
7364 /* When we reinsert this decl in the function body, we need
7365 to reconstruct whether it was marked as nested. */
7366 gcc_assert (!b->nested);
7367 DECL_CHAIN (decl) = others;
7368 others = decl;
7369 /* fall through */
7371 case ERROR_MARK:
7372 set_shadowed:
7373 /* error_mark_node appears here when we have an undeclared
7374 variable. Just throw it away. */
7375 if (b->id)
7377 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7378 I_SYMBOL_BINDING (b->id) = b->shadowed;
7380 break;
7382 /* Other things that might be encountered. */
7383 case LABEL_DECL:
7384 case VAR_DECL:
7385 default:
7386 gcc_unreachable ();
7389 b = free_binding_and_advance (b);
7392 arg_info->parms = parms;
7393 arg_info->tags = tags;
7394 arg_info->types = types;
7395 arg_info->others = others;
7396 arg_info->pending_sizes = expr;
7397 return arg_info;
7400 /* Get the struct, enum or union (CODE says which) with tag NAME.
7401 Define the tag as a forward-reference with location LOC if it is
7402 not defined. Return a c_typespec structure for the type
7403 specifier. */
7405 struct c_typespec
7406 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7408 struct c_typespec ret;
7409 tree ref;
7410 location_t refloc;
7412 ret.expr = NULL_TREE;
7413 ret.expr_const_operands = true;
7415 /* If a cross reference is requested, look up the type
7416 already defined for this tag and return it. */
7418 ref = lookup_tag (code, name, false, &refloc);
7419 /* If this is the right type of tag, return what we found.
7420 (This reference will be shadowed by shadow_tag later if appropriate.)
7421 If this is the wrong type of tag, do not return it. If it was the
7422 wrong type in the same scope, we will have had an error
7423 message already; if in a different scope and declaring
7424 a name, pending_xref_error will give an error message; but if in a
7425 different scope and not declaring a name, this tag should
7426 shadow the previous declaration of a different type of tag, and
7427 this would not work properly if we return the reference found.
7428 (For example, with "struct foo" in an outer scope, "union foo;"
7429 must shadow that tag with a new one of union type.) */
7430 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7431 if (ref && TREE_CODE (ref) == code)
7433 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7434 && loc != UNKNOWN_LOCATION
7435 && warn_cxx_compat)
7437 switch (code)
7439 case ENUMERAL_TYPE:
7440 warning_at (loc, OPT_Wc___compat,
7441 ("enum type defined in struct or union "
7442 "is not visible in C++"));
7443 inform (refloc, "enum type defined here");
7444 break;
7445 case RECORD_TYPE:
7446 warning_at (loc, OPT_Wc___compat,
7447 ("struct defined in struct or union "
7448 "is not visible in C++"));
7449 inform (refloc, "struct defined here");
7450 break;
7451 case UNION_TYPE:
7452 warning_at (loc, OPT_Wc___compat,
7453 ("union defined in struct or union "
7454 "is not visible in C++"));
7455 inform (refloc, "union defined here");
7456 break;
7457 default:
7458 gcc_unreachable();
7462 ret.spec = ref;
7463 return ret;
7466 /* If no such tag is yet defined, create a forward-reference node
7467 and record it as the "definition".
7468 When a real declaration of this type is found,
7469 the forward-reference will be altered into a real type. */
7471 ref = make_node (code);
7472 if (code == ENUMERAL_TYPE)
7474 /* Give the type a default layout like unsigned int
7475 to avoid crashing if it does not get defined. */
7476 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7477 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7478 TYPE_USER_ALIGN (ref) = 0;
7479 TYPE_UNSIGNED (ref) = 1;
7480 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7481 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7482 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7485 pushtag (loc, name, ref);
7487 ret.spec = ref;
7488 return ret;
7491 /* Get the struct, enum or union (CODE says which) with tag NAME.
7492 Define the tag as a forward-reference if it is not defined.
7493 Return a tree for the type. */
7495 tree
7496 xref_tag (enum tree_code code, tree name)
7498 return parser_xref_tag (input_location, code, name).spec;
7501 /* Make sure that the tag NAME is defined *in the current scope*
7502 at least as a forward reference.
7503 LOC is the location of the struct's definition.
7504 CODE says which kind of tag NAME ought to be.
7506 This stores the current value of the file static STRUCT_PARSE_INFO
7507 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7508 new c_struct_parse_info structure. The old value of
7509 STRUCT_PARSE_INFO is restored in finish_struct. */
7511 tree
7512 start_struct (location_t loc, enum tree_code code, tree name,
7513 struct c_struct_parse_info **enclosing_struct_parse_info)
7515 /* If there is already a tag defined at this scope
7516 (as a forward reference), just return it. */
7518 tree ref = NULL_TREE;
7519 location_t refloc = UNKNOWN_LOCATION;
7521 if (name != NULL_TREE)
7522 ref = lookup_tag (code, name, true, &refloc);
7523 if (ref && TREE_CODE (ref) == code)
7525 if (TYPE_STUB_DECL (ref))
7526 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7528 if (TYPE_SIZE (ref))
7530 if (code == UNION_TYPE)
7531 error_at (loc, "redefinition of %<union %E%>", name);
7532 else
7533 error_at (loc, "redefinition of %<struct %E%>", name);
7534 if (refloc != UNKNOWN_LOCATION)
7535 inform (refloc, "originally defined here");
7536 /* Don't create structures using a name already in use. */
7537 ref = NULL_TREE;
7539 else if (C_TYPE_BEING_DEFINED (ref))
7541 if (code == UNION_TYPE)
7542 error_at (loc, "nested redefinition of %<union %E%>", name);
7543 else
7544 error_at (loc, "nested redefinition of %<struct %E%>", name);
7545 /* Don't bother to report "originally defined here" for a
7546 nested redefinition; the original definition should be
7547 obvious. */
7548 /* Don't create structures that contain themselves. */
7549 ref = NULL_TREE;
7553 /* Otherwise create a forward-reference just so the tag is in scope. */
7555 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7557 ref = make_node (code);
7558 pushtag (loc, name, ref);
7561 C_TYPE_BEING_DEFINED (ref) = 1;
7562 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7563 TYPE_PACKED (v) = flag_pack_struct;
7565 *enclosing_struct_parse_info = struct_parse_info;
7566 struct_parse_info = new c_struct_parse_info ();
7568 /* FIXME: This will issue a warning for a use of a type defined
7569 within a statement expr used within sizeof, et. al. This is not
7570 terribly serious as C++ doesn't permit statement exprs within
7571 sizeof anyhow. */
7572 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7573 warning_at (loc, OPT_Wc___compat,
7574 "defining type in %qs expression is invalid in C++",
7575 (in_sizeof
7576 ? "sizeof"
7577 : (in_typeof ? "typeof" : "alignof")));
7579 return ref;
7582 /* Process the specs, declarator and width (NULL if omitted)
7583 of a structure component, returning a FIELD_DECL node.
7584 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7585 DECL_ATTRS is as for grokdeclarator.
7587 LOC is the location of the structure component.
7589 This is done during the parsing of the struct declaration.
7590 The FIELD_DECL nodes are chained together and the lot of them
7591 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7593 tree
7594 grokfield (location_t loc,
7595 struct c_declarator *declarator, struct c_declspecs *declspecs,
7596 tree width, tree *decl_attrs)
7598 tree value;
7600 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7601 && width == NULL_TREE)
7603 /* This is an unnamed decl.
7605 If we have something of the form "union { list } ;" then this
7606 is the anonymous union extension. Similarly for struct.
7608 If this is something of the form "struct foo;", then
7609 If MS or Plan 9 extensions are enabled, this is handled as
7610 an anonymous struct.
7611 Otherwise this is a forward declaration of a structure tag.
7613 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7614 If foo names a structure or union without a tag, then this
7615 is an anonymous struct (this is permitted by C11).
7616 If MS or Plan 9 extensions are enabled and foo names a
7617 structure, then again this is an anonymous struct.
7618 Otherwise this is an error.
7620 Oh what a horrid tangled web we weave. I wonder if MS consciously
7621 took this from Plan 9 or if it was an accident of implementation
7622 that took root before someone noticed the bug... */
7624 tree type = declspecs->type;
7625 bool ok = false;
7627 if (RECORD_OR_UNION_TYPE_P (type)
7628 && (flag_ms_extensions
7629 || flag_plan9_extensions
7630 || !declspecs->typedef_p))
7632 if (flag_ms_extensions || flag_plan9_extensions)
7633 ok = true;
7634 else if (TYPE_NAME (type) == NULL)
7635 ok = true;
7636 else
7637 ok = false;
7639 if (!ok)
7641 pedwarn (loc, 0, "declaration does not declare anything");
7642 return NULL_TREE;
7644 if (flag_isoc99)
7645 pedwarn_c99 (loc, OPT_Wpedantic,
7646 "ISO C99 doesn%'t support unnamed structs/unions");
7647 else
7648 pedwarn_c99 (loc, OPT_Wpedantic,
7649 "ISO C90 doesn%'t support unnamed structs/unions");
7652 value = grokdeclarator (declarator, declspecs, FIELD, false,
7653 width ? &width : NULL, decl_attrs, NULL, NULL,
7654 DEPRECATED_NORMAL);
7656 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7657 DECL_INITIAL (value) = width;
7658 if (width)
7659 SET_DECL_C_BIT_FIELD (value);
7661 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7663 /* If we currently have a binding for this field, set the
7664 in_struct field in the binding, so that we warn about lookups
7665 which find it. */
7666 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7667 if (b != NULL)
7669 /* If the in_struct field is not yet set, push it on a list
7670 to be cleared when this struct is finished. */
7671 if (!b->in_struct)
7673 struct_parse_info->fields.safe_push (b);
7674 b->in_struct = 1;
7679 return value;
7682 /* Subroutine of detect_field_duplicates: return whether X and Y,
7683 which are both fields in the same struct, have duplicate field
7684 names. */
7686 static bool
7687 is_duplicate_field (tree x, tree y)
7689 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7690 return true;
7692 /* When using -fplan9-extensions, an anonymous field whose name is a
7693 typedef can duplicate a field name. */
7694 if (flag_plan9_extensions
7695 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7697 tree xt, xn, yt, yn;
7699 xt = TREE_TYPE (x);
7700 if (DECL_NAME (x) != NULL_TREE)
7701 xn = DECL_NAME (x);
7702 else if (RECORD_OR_UNION_TYPE_P (xt)
7703 && TYPE_NAME (xt) != NULL_TREE
7704 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7705 xn = DECL_NAME (TYPE_NAME (xt));
7706 else
7707 xn = NULL_TREE;
7709 yt = TREE_TYPE (y);
7710 if (DECL_NAME (y) != NULL_TREE)
7711 yn = DECL_NAME (y);
7712 else if (RECORD_OR_UNION_TYPE_P (yt)
7713 && TYPE_NAME (yt) != NULL_TREE
7714 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7715 yn = DECL_NAME (TYPE_NAME (yt));
7716 else
7717 yn = NULL_TREE;
7719 if (xn != NULL_TREE && xn == yn)
7720 return true;
7723 return false;
7726 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7727 to HTAB, giving errors for any duplicates. */
7729 static void
7730 detect_field_duplicates_hash (tree fieldlist,
7731 hash_table<nofree_ptr_hash <tree_node> > *htab)
7733 tree x, y;
7734 tree_node **slot;
7736 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7737 if ((y = DECL_NAME (x)) != NULL_TREE)
7739 slot = htab->find_slot (y, INSERT);
7740 if (*slot)
7742 error ("duplicate member %q+D", x);
7743 DECL_NAME (x) = NULL_TREE;
7745 *slot = y;
7747 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7749 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7751 /* When using -fplan9-extensions, an anonymous field whose
7752 name is a typedef can duplicate a field name. */
7753 if (flag_plan9_extensions
7754 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7755 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7757 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7758 slot = htab->find_slot (xn, INSERT);
7759 if (*slot)
7760 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7761 *slot = xn;
7766 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7767 the list such that this does not present a problem later. */
7769 static void
7770 detect_field_duplicates (tree fieldlist)
7772 tree x, y;
7773 int timeout = 10;
7775 /* If the struct is the list of instance variables of an Objective-C
7776 class, then we need to check all the instance variables of
7777 superclasses when checking for duplicates (since you can't have
7778 an instance variable in a subclass with the same name as an
7779 instance variable in a superclass). We pass on this job to the
7780 Objective-C compiler. objc_detect_field_duplicates() will return
7781 false if we are not checking the list of instance variables and
7782 the C frontend should proceed with the standard field duplicate
7783 checks. If we are checking the list of instance variables, the
7784 ObjC frontend will do the check, emit the errors if needed, and
7785 then return true. */
7786 if (c_dialect_objc ())
7787 if (objc_detect_field_duplicates (false))
7788 return;
7790 /* First, see if there are more than "a few" fields.
7791 This is trivially true if there are zero or one fields. */
7792 if (!fieldlist || !DECL_CHAIN (fieldlist))
7793 return;
7794 x = fieldlist;
7795 do {
7796 timeout--;
7797 if (DECL_NAME (x) == NULL_TREE
7798 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7799 timeout = 0;
7800 x = DECL_CHAIN (x);
7801 } while (timeout > 0 && x);
7803 /* If there were "few" fields and no anonymous structures or unions,
7804 avoid the overhead of allocating a hash table. Instead just do
7805 the nested traversal thing. */
7806 if (timeout > 0)
7808 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7809 /* When using -fplan9-extensions, we can have duplicates
7810 between typedef names and fields. */
7811 if (DECL_NAME (x)
7812 || (flag_plan9_extensions
7813 && DECL_NAME (x) == NULL_TREE
7814 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7815 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7816 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7818 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7819 if (is_duplicate_field (y, x))
7821 error ("duplicate member %q+D", x);
7822 DECL_NAME (x) = NULL_TREE;
7826 else
7828 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7829 detect_field_duplicates_hash (fieldlist, &htab);
7833 /* Finish up struct info used by -Wc++-compat. */
7835 static void
7836 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7837 location_t record_loc)
7839 unsigned int ix;
7840 tree x;
7841 struct c_binding *b;
7843 if (fieldlist == NULL_TREE)
7845 if (code == RECORD_TYPE)
7846 warning_at (record_loc, OPT_Wc___compat,
7847 "empty struct has size 0 in C, size 1 in C++");
7848 else
7849 warning_at (record_loc, OPT_Wc___compat,
7850 "empty union has size 0 in C, size 1 in C++");
7853 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7854 the current struct. We do this now at the end of the struct
7855 because the flag is used to issue visibility warnings, and we
7856 only want to issue those warnings if the type is referenced
7857 outside of the struct declaration. */
7858 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7859 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7861 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7862 typedefs used when declaring fields in this struct. If the name
7863 of any of the fields is also a typedef name then the struct would
7864 not parse in C++, because the C++ lookup rules say that the
7865 typedef name would be looked up in the context of the struct, and
7866 would thus be the field rather than the typedef. */
7867 if (!struct_parse_info->typedefs_seen.is_empty ()
7868 && fieldlist != NULL_TREE)
7870 /* Use a hash_set<tree> using the name of the typedef. We can use
7871 a hash_set<tree> because identifiers are interned. */
7872 hash_set<tree> tset;
7874 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7875 tset.add (DECL_NAME (x));
7877 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7879 if (DECL_NAME (x) != NULL_TREE
7880 && tset.contains (DECL_NAME (x)))
7882 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7883 ("using %qD as both field and typedef name is "
7884 "invalid in C++"),
7886 /* FIXME: It would be nice to report the location where
7887 the typedef name is used. */
7892 /* For each field which has a binding and which was not defined in
7893 an enclosing struct, clear the in_struct field. */
7894 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7895 b->in_struct = 0;
7898 /* Function to help qsort sort FIELD_DECLs by name order. */
7900 static int
7901 field_decl_cmp (const void *x_p, const void *y_p)
7903 const tree *const x = (const tree *) x_p;
7904 const tree *const y = (const tree *) y_p;
7906 if (DECL_NAME (*x) == DECL_NAME (*y))
7907 /* A nontype is "greater" than a type. */
7908 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7909 if (DECL_NAME (*x) == NULL_TREE)
7910 return -1;
7911 if (DECL_NAME (*y) == NULL_TREE)
7912 return 1;
7913 if (DECL_NAME (*x) < DECL_NAME (*y))
7914 return -1;
7915 return 1;
7918 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7919 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7920 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7921 ATTRIBUTES are attributes to be applied to the structure.
7923 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7924 the struct was started. */
7926 tree
7927 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7928 struct c_struct_parse_info *enclosing_struct_parse_info)
7930 tree x;
7931 bool toplevel = file_scope == current_scope;
7933 /* If this type was previously laid out as a forward reference,
7934 make sure we lay it out again. */
7936 TYPE_SIZE (t) = NULL_TREE;
7938 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7940 if (pedantic)
7942 for (x = fieldlist; x; x = DECL_CHAIN (x))
7944 if (DECL_NAME (x) != NULL_TREE)
7945 break;
7946 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7947 break;
7950 if (x == NULL_TREE)
7952 if (TREE_CODE (t) == UNION_TYPE)
7954 if (fieldlist)
7955 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7956 else
7957 pedwarn (loc, OPT_Wpedantic, "union has no members");
7959 else
7961 if (fieldlist)
7962 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7963 else
7964 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7969 /* Install struct as DECL_CONTEXT of each field decl.
7970 Also process specified field sizes, found in the DECL_INITIAL,
7971 storing 0 there after the type has been changed to precision equal
7972 to its width, rather than the precision of the specified standard
7973 type. (Correct layout requires the original type to have been preserved
7974 until now.) */
7976 bool saw_named_field = false;
7977 for (x = fieldlist; x; x = DECL_CHAIN (x))
7979 if (TREE_TYPE (x) == error_mark_node)
7980 continue;
7982 DECL_CONTEXT (x) = t;
7984 /* If any field is const, the structure type is pseudo-const. */
7985 if (TREE_READONLY (x))
7986 C_TYPE_FIELDS_READONLY (t) = 1;
7987 else
7989 /* A field that is pseudo-const makes the structure likewise. */
7990 tree t1 = strip_array_types (TREE_TYPE (x));
7991 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7992 C_TYPE_FIELDS_READONLY (t) = 1;
7995 /* Any field that is volatile means variables of this type must be
7996 treated in some ways as volatile. */
7997 if (TREE_THIS_VOLATILE (x))
7998 C_TYPE_FIELDS_VOLATILE (t) = 1;
8000 /* Any field of nominal variable size implies structure is too. */
8001 if (C_DECL_VARIABLE_SIZE (x))
8002 C_TYPE_VARIABLE_SIZE (t) = 1;
8004 if (DECL_C_BIT_FIELD (x))
8006 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8007 DECL_SIZE (x) = bitsize_int (width);
8008 DECL_BIT_FIELD (x) = 1;
8011 if (TYPE_PACKED (t)
8012 && (DECL_BIT_FIELD (x)
8013 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8014 DECL_PACKED (x) = 1;
8016 /* Detect flexible array member in an invalid context. */
8017 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8018 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8019 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8020 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8022 if (TREE_CODE (t) == UNION_TYPE)
8024 error_at (DECL_SOURCE_LOCATION (x),
8025 "flexible array member in union");
8026 TREE_TYPE (x) = error_mark_node;
8028 else if (DECL_CHAIN (x) != NULL_TREE)
8030 error_at (DECL_SOURCE_LOCATION (x),
8031 "flexible array member not at end of struct");
8032 TREE_TYPE (x) = error_mark_node;
8034 else if (!saw_named_field)
8036 error_at (DECL_SOURCE_LOCATION (x),
8037 "flexible array member in a struct with no named "
8038 "members");
8039 TREE_TYPE (x) = error_mark_node;
8043 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8044 && flexible_array_type_p (TREE_TYPE (x)))
8045 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8046 "invalid use of structure with flexible array member");
8048 if (DECL_NAME (x)
8049 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8050 saw_named_field = true;
8053 detect_field_duplicates (fieldlist);
8055 /* Now we have the nearly final fieldlist. Record it,
8056 then lay out the structure or union (including the fields). */
8058 TYPE_FIELDS (t) = fieldlist;
8060 maybe_apply_pragma_scalar_storage_order (t);
8062 layout_type (t);
8064 if (TYPE_SIZE_UNIT (t)
8065 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8066 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8067 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8068 error ("type %qT is too large", t);
8070 /* Give bit-fields their proper types and rewrite the type of array fields
8071 with scalar component if the enclosing type has reverse storage order. */
8072 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8074 if (TREE_CODE (field) == FIELD_DECL
8075 && DECL_INITIAL (field)
8076 && TREE_TYPE (field) != error_mark_node)
8078 unsigned HOST_WIDE_INT width
8079 = tree_to_uhwi (DECL_INITIAL (field));
8080 tree type = TREE_TYPE (field);
8081 if (width != TYPE_PRECISION (type))
8083 TREE_TYPE (field)
8084 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8085 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8087 DECL_INITIAL (field) = NULL_TREE;
8089 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8090 && TREE_CODE (field) == FIELD_DECL
8091 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8093 tree ftype = TREE_TYPE (field);
8094 tree ctype = strip_array_types (ftype);
8095 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8097 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8098 tree *typep = &fmain_type;
8099 do {
8100 *typep = build_distinct_type_copy (*typep);
8101 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8102 typep = &TREE_TYPE (*typep);
8103 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8104 TREE_TYPE (field)
8105 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8110 /* Now we have the truly final field list.
8111 Store it in this type and in the variants. */
8113 TYPE_FIELDS (t) = fieldlist;
8115 /* If there are lots of fields, sort so we can look through them fast.
8116 We arbitrarily consider 16 or more elts to be "a lot". */
8119 int len = 0;
8121 for (x = fieldlist; x; x = DECL_CHAIN (x))
8123 if (len > 15 || DECL_NAME (x) == NULL)
8124 break;
8125 len += 1;
8128 if (len > 15)
8130 tree *field_array;
8131 struct lang_type *space;
8132 struct sorted_fields_type *space2;
8134 len += list_length (x);
8136 /* Use the same allocation policy here that make_node uses, to
8137 ensure that this lives as long as the rest of the struct decl.
8138 All decls in an inline function need to be saved. */
8140 space = ggc_cleared_alloc<struct lang_type> ();
8141 space2 = (sorted_fields_type *) ggc_internal_alloc
8142 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8144 len = 0;
8145 space->s = space2;
8146 field_array = &space2->elts[0];
8147 for (x = fieldlist; x; x = DECL_CHAIN (x))
8149 field_array[len++] = x;
8151 /* If there is anonymous struct or union, break out of the loop. */
8152 if (DECL_NAME (x) == NULL)
8153 break;
8155 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8156 if (x == NULL)
8158 TYPE_LANG_SPECIFIC (t) = space;
8159 TYPE_LANG_SPECIFIC (t)->s->len = len;
8160 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8161 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8166 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8167 in dwarf2out via rest_of_decl_compilation below and means
8168 something totally different. Since we will be clearing
8169 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8170 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8171 C_TYPE_INCOMPLETE_VARS should use some language specific
8172 node. */
8173 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8174 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8176 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8177 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8178 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8179 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8180 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8181 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8184 /* If this was supposed to be a transparent union, but we can't
8185 make it one, warn and turn off the flag. */
8186 if (TREE_CODE (t) == UNION_TYPE
8187 && TYPE_TRANSPARENT_AGGR (t)
8188 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8190 TYPE_TRANSPARENT_AGGR (t) = 0;
8191 warning_at (loc, 0, "union cannot be made transparent");
8194 /* If this structure or union completes the type of any previous
8195 variable declaration, lay it out and output its rtl. */
8196 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8198 tree decl = TREE_VALUE (x);
8199 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8200 layout_array_type (TREE_TYPE (decl));
8201 if (TREE_CODE (decl) != TYPE_DECL)
8203 layout_decl (decl, 0);
8204 if (c_dialect_objc ())
8205 objc_check_decl (decl);
8206 rest_of_decl_compilation (decl, toplevel, 0);
8210 /* Update type location to the one of the definition, instead of e.g.
8211 a forward declaration. */
8212 if (TYPE_STUB_DECL (t))
8213 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8215 /* Finish debugging output for this type. */
8216 rest_of_type_compilation (t, toplevel);
8218 /* If we're inside a function proper, i.e. not file-scope and not still
8219 parsing parameters, then arrange for the size of a variable sized type
8220 to be bound now. */
8221 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8222 add_stmt (build_stmt (loc,
8223 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8225 if (warn_cxx_compat)
8226 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8228 delete struct_parse_info;
8230 struct_parse_info = enclosing_struct_parse_info;
8232 /* If this struct is defined inside a struct, add it to
8233 struct_types. */
8234 if (warn_cxx_compat
8235 && struct_parse_info != NULL
8236 && !in_sizeof && !in_typeof && !in_alignof)
8237 struct_parse_info->struct_types.safe_push (t);
8239 return t;
8242 static struct {
8243 gt_pointer_operator new_value;
8244 void *cookie;
8245 } resort_data;
8247 /* This routine compares two fields like field_decl_cmp but using the
8248 pointer operator in resort_data. */
8250 static int
8251 resort_field_decl_cmp (const void *x_p, const void *y_p)
8253 const tree *const x = (const tree *) x_p;
8254 const tree *const y = (const tree *) y_p;
8256 if (DECL_NAME (*x) == DECL_NAME (*y))
8257 /* A nontype is "greater" than a type. */
8258 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8259 if (DECL_NAME (*x) == NULL_TREE)
8260 return -1;
8261 if (DECL_NAME (*y) == NULL_TREE)
8262 return 1;
8264 tree d1 = DECL_NAME (*x);
8265 tree d2 = DECL_NAME (*y);
8266 resort_data.new_value (&d1, resort_data.cookie);
8267 resort_data.new_value (&d2, resort_data.cookie);
8268 if (d1 < d2)
8269 return -1;
8271 return 1;
8274 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8276 void
8277 resort_sorted_fields (void *obj,
8278 void * ARG_UNUSED (orig_obj),
8279 gt_pointer_operator new_value,
8280 void *cookie)
8282 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8283 resort_data.new_value = new_value;
8284 resort_data.cookie = cookie;
8285 qsort (&sf->elts[0], sf->len, sizeof (tree),
8286 resort_field_decl_cmp);
8289 /* Lay out the type T, and its element type, and so on. */
8291 static void
8292 layout_array_type (tree t)
8294 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8295 layout_array_type (TREE_TYPE (t));
8296 layout_type (t);
8299 /* Begin compiling the definition of an enumeration type.
8300 NAME is its name (or null if anonymous).
8301 LOC is the enum's location.
8302 Returns the type object, as yet incomplete.
8303 Also records info about it so that build_enumerator
8304 may be used to declare the individual values as they are read. */
8306 tree
8307 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8309 tree enumtype = NULL_TREE;
8310 location_t enumloc = UNKNOWN_LOCATION;
8312 /* If this is the real definition for a previous forward reference,
8313 fill in the contents in the same object that used to be the
8314 forward reference. */
8316 if (name != NULL_TREE)
8317 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8319 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8321 enumtype = make_node (ENUMERAL_TYPE);
8322 pushtag (loc, name, enumtype);
8324 /* Update type location to the one of the definition, instead of e.g.
8325 a forward declaration. */
8326 else if (TYPE_STUB_DECL (enumtype))
8328 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8329 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8332 if (C_TYPE_BEING_DEFINED (enumtype))
8333 error_at (loc, "nested redefinition of %<enum %E%>", name);
8335 C_TYPE_BEING_DEFINED (enumtype) = 1;
8337 if (TYPE_VALUES (enumtype) != NULL_TREE)
8339 /* This enum is a named one that has been declared already. */
8340 error_at (loc, "redeclaration of %<enum %E%>", name);
8341 if (enumloc != UNKNOWN_LOCATION)
8342 inform (enumloc, "originally defined here");
8344 /* Completely replace its old definition.
8345 The old enumerators remain defined, however. */
8346 TYPE_VALUES (enumtype) = NULL_TREE;
8349 the_enum->enum_next_value = integer_zero_node;
8350 the_enum->enum_overflow = 0;
8352 if (flag_short_enums)
8353 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8354 TYPE_PACKED (v) = 1;
8356 /* FIXME: This will issue a warning for a use of a type defined
8357 within sizeof in a statement expr. This is not terribly serious
8358 as C++ doesn't permit statement exprs within sizeof anyhow. */
8359 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8360 warning_at (loc, OPT_Wc___compat,
8361 "defining type in %qs expression is invalid in C++",
8362 (in_sizeof
8363 ? "sizeof"
8364 : (in_typeof ? "typeof" : "alignof")));
8366 return enumtype;
8369 /* After processing and defining all the values of an enumeration type,
8370 install their decls in the enumeration type and finish it off.
8371 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8372 and ATTRIBUTES are the specified attributes.
8373 Returns ENUMTYPE. */
8375 tree
8376 finish_enum (tree enumtype, tree values, tree attributes)
8378 tree pair, tem;
8379 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8380 int precision;
8381 signop sign;
8382 bool toplevel = (file_scope == current_scope);
8383 struct lang_type *lt;
8385 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8387 /* Calculate the maximum value of any enumerator in this type. */
8389 if (values == error_mark_node)
8390 minnode = maxnode = integer_zero_node;
8391 else
8393 minnode = maxnode = TREE_VALUE (values);
8394 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8396 tree value = TREE_VALUE (pair);
8397 if (tree_int_cst_lt (maxnode, value))
8398 maxnode = value;
8399 if (tree_int_cst_lt (value, minnode))
8400 minnode = value;
8404 /* Construct the final type of this enumeration. It is the same
8405 as one of the integral types - the narrowest one that fits, except
8406 that normally we only go as narrow as int - and signed iff any of
8407 the values are negative. */
8408 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8409 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8410 tree_int_cst_min_precision (maxnode, sign));
8412 /* If the precision of the type was specified with an attribute and it
8413 was too small, give an error. Otherwise, use it. */
8414 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8416 if (precision > TYPE_PRECISION (enumtype))
8418 TYPE_PRECISION (enumtype) = 0;
8419 error ("specified mode too small for enumeral values");
8421 else
8422 precision = TYPE_PRECISION (enumtype);
8424 else
8425 TYPE_PRECISION (enumtype) = 0;
8427 if (TYPE_PACKED (enumtype)
8428 || precision > TYPE_PRECISION (integer_type_node)
8429 || TYPE_PRECISION (enumtype))
8431 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8432 if (tem == NULL)
8434 warning (0, "enumeration values exceed range of largest integer");
8435 tem = long_long_integer_type_node;
8438 else
8439 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8441 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8442 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8443 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8444 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8445 TYPE_SIZE (enumtype) = NULL_TREE;
8446 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8448 layout_type (enumtype);
8450 if (values != error_mark_node)
8452 /* Change the type of the enumerators to be the enum type. We
8453 need to do this irrespective of the size of the enum, for
8454 proper type checking. Replace the DECL_INITIALs of the
8455 enumerators, and the value slots of the list, with copies
8456 that have the enum type; they cannot be modified in place
8457 because they may be shared (e.g. integer_zero_node) Finally,
8458 change the purpose slots to point to the names of the decls. */
8459 for (pair = values; pair; pair = TREE_CHAIN (pair))
8461 tree enu = TREE_PURPOSE (pair);
8462 tree ini = DECL_INITIAL (enu);
8464 TREE_TYPE (enu) = enumtype;
8466 /* The ISO C Standard mandates enumerators to have type int,
8467 even though the underlying type of an enum type is
8468 unspecified. However, GCC allows enumerators of any
8469 integer type as an extensions. build_enumerator()
8470 converts any enumerators that fit in an int to type int,
8471 to avoid promotions to unsigned types when comparing
8472 integers with enumerators that fit in the int range.
8473 When -pedantic is given, build_enumerator() would have
8474 already warned about those that don't fit. Here we
8475 convert the rest to the enumerator type. */
8476 if (TREE_TYPE (ini) != integer_type_node)
8477 ini = convert (enumtype, ini);
8479 DECL_INITIAL (enu) = ini;
8480 TREE_PURPOSE (pair) = DECL_NAME (enu);
8481 TREE_VALUE (pair) = ini;
8484 TYPE_VALUES (enumtype) = values;
8487 /* Record the min/max values so that we can warn about bit-field
8488 enumerations that are too small for the values. */
8489 lt = ggc_cleared_alloc<struct lang_type> ();
8490 lt->enum_min = minnode;
8491 lt->enum_max = maxnode;
8492 TYPE_LANG_SPECIFIC (enumtype) = lt;
8494 /* Fix up all variant types of this enum type. */
8495 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8497 if (tem == enumtype)
8498 continue;
8499 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8500 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8501 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8502 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8503 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8504 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8505 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8506 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8507 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8508 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8509 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8512 /* Finish debugging output for this type. */
8513 rest_of_type_compilation (enumtype, toplevel);
8515 /* If this enum is defined inside a struct, add it to
8516 struct_types. */
8517 if (warn_cxx_compat
8518 && struct_parse_info != NULL
8519 && !in_sizeof && !in_typeof && !in_alignof)
8520 struct_parse_info->struct_types.safe_push (enumtype);
8522 return enumtype;
8525 /* Build and install a CONST_DECL for one value of the
8526 current enumeration type (one that was begun with start_enum).
8527 DECL_LOC is the location of the enumerator.
8528 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8529 Return a tree-list containing the CONST_DECL and its value.
8530 Assignment of sequential values by default is handled here. */
8532 tree
8533 build_enumerator (location_t decl_loc, location_t loc,
8534 struct c_enum_contents *the_enum, tree name, tree value)
8536 tree decl, type;
8538 /* Validate and default VALUE. */
8540 if (value != NULL_TREE)
8542 /* Don't issue more errors for error_mark_node (i.e. an
8543 undeclared identifier) - just ignore the value expression. */
8544 if (value == error_mark_node)
8545 value = NULL_TREE;
8546 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8548 error_at (loc, "enumerator value for %qE is not an integer constant",
8549 name);
8550 value = NULL_TREE;
8552 else
8554 if (TREE_CODE (value) != INTEGER_CST)
8556 value = c_fully_fold (value, false, NULL);
8557 if (TREE_CODE (value) == INTEGER_CST)
8558 pedwarn (loc, OPT_Wpedantic,
8559 "enumerator value for %qE is not an integer "
8560 "constant expression", name);
8562 if (TREE_CODE (value) != INTEGER_CST)
8564 error ("enumerator value for %qE is not an integer constant",
8565 name);
8566 value = NULL_TREE;
8568 else
8570 value = default_conversion (value);
8571 constant_expression_warning (value);
8576 /* Default based on previous value. */
8577 /* It should no longer be possible to have NON_LVALUE_EXPR
8578 in the default. */
8579 if (value == NULL_TREE)
8581 value = the_enum->enum_next_value;
8582 if (the_enum->enum_overflow)
8583 error_at (loc, "overflow in enumeration values");
8585 /* Even though the underlying type of an enum is unspecified, the
8586 type of enumeration constants is explicitly defined as int
8587 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8588 an extension. */
8589 else if (!int_fits_type_p (value, integer_type_node))
8590 pedwarn (loc, OPT_Wpedantic,
8591 "ISO C restricts enumerator values to range of %<int%>");
8593 /* The ISO C Standard mandates enumerators to have type int, even
8594 though the underlying type of an enum type is unspecified.
8595 However, GCC allows enumerators of any integer type as an
8596 extensions. Here we convert any enumerators that fit in an int
8597 to type int, to avoid promotions to unsigned types when comparing
8598 integers with enumerators that fit in the int range. When
8599 -pedantic is given, we would have already warned about those that
8600 don't fit. We have to do this here rather than in finish_enum
8601 because this value may be used to define more enumerators. */
8602 if (int_fits_type_p (value, integer_type_node))
8603 value = convert (integer_type_node, value);
8605 /* Set basis for default for next value. */
8606 the_enum->enum_next_value
8607 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8608 PLUS_EXPR, value, integer_one_node, false);
8609 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8611 /* Now create a declaration for the enum value name. */
8613 type = TREE_TYPE (value);
8614 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8615 TYPE_PRECISION (integer_type_node)),
8616 (TYPE_PRECISION (type)
8617 >= TYPE_PRECISION (integer_type_node)
8618 && TYPE_UNSIGNED (type)));
8620 decl = build_decl (decl_loc, CONST_DECL, name, type);
8621 DECL_INITIAL (decl) = convert (type, value);
8622 pushdecl (decl);
8624 return tree_cons (decl, value, NULL_TREE);
8628 /* Create the FUNCTION_DECL for a function definition.
8629 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8630 the declaration; they describe the function's name and the type it returns,
8631 but twisted together in a fashion that parallels the syntax of C.
8633 This function creates a binding context for the function body
8634 as well as setting up the FUNCTION_DECL in current_function_decl.
8636 Returns true on success. If the DECLARATOR is not suitable for a function
8637 (it defines a datum instead), we return false to report a parse error. */
8639 bool
8640 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8641 tree attributes)
8643 tree decl1, old_decl;
8644 tree restype, resdecl;
8645 location_t loc;
8647 current_function_returns_value = 0; /* Assume, until we see it does. */
8648 current_function_returns_null = 0;
8649 current_function_returns_abnormally = 0;
8650 warn_about_return_type = 0;
8651 c_switch_stack = NULL;
8653 /* Indicate no valid break/continue context by setting these variables
8654 to some non-null, non-label value. We'll notice and emit the proper
8655 error message in c_finish_bc_stmt. */
8656 c_break_label = c_cont_label = size_zero_node;
8658 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8659 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8660 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8662 /* If the declarator is not suitable for a function definition,
8663 cause a syntax error. */
8664 if (decl1 == NULL_TREE
8665 || TREE_CODE (decl1) != FUNCTION_DECL)
8666 return false;
8668 loc = DECL_SOURCE_LOCATION (decl1);
8670 c_decl_attributes (&decl1, attributes, 0);
8672 if (DECL_DECLARED_INLINE_P (decl1)
8673 && DECL_UNINLINABLE (decl1)
8674 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8675 warning_at (loc, OPT_Wattributes,
8676 "inline function %qD given attribute noinline",
8677 decl1);
8679 /* Handle gnu_inline attribute. */
8680 if (declspecs->inline_p
8681 && !flag_gnu89_inline
8682 && TREE_CODE (decl1) == FUNCTION_DECL
8683 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8684 || current_function_decl))
8686 if (declspecs->storage_class != csc_static)
8687 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8690 announce_function (decl1);
8692 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8694 error_at (loc, "return type is an incomplete type");
8695 /* Make it return void instead. */
8696 TREE_TYPE (decl1)
8697 = build_function_type (void_type_node,
8698 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8701 if (warn_about_return_type)
8702 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8703 : (warn_return_type ? OPT_Wreturn_type
8704 : OPT_Wimplicit_int),
8705 "return type defaults to %<int%>");
8707 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8708 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8709 DECL_INITIAL (decl1) = error_mark_node;
8711 /* A nested function is not global. */
8712 if (current_function_decl != NULL_TREE)
8713 TREE_PUBLIC (decl1) = 0;
8715 /* If this definition isn't a prototype and we had a prototype declaration
8716 before, copy the arg type info from that prototype. */
8717 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8718 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8719 old_decl = NULL_TREE;
8720 current_function_prototype_locus = UNKNOWN_LOCATION;
8721 current_function_prototype_built_in = false;
8722 current_function_prototype_arg_types = NULL_TREE;
8723 if (!prototype_p (TREE_TYPE (decl1)))
8725 if (old_decl != NULL_TREE
8726 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8727 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8728 TREE_TYPE (TREE_TYPE (old_decl))))
8730 if (stdarg_p (TREE_TYPE (old_decl)))
8732 warning_at (loc, 0, "%q+D defined as variadic function "
8733 "without prototype", decl1);
8734 locate_old_decl (old_decl);
8736 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8737 TREE_TYPE (decl1));
8738 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8739 current_function_prototype_built_in
8740 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8741 current_function_prototype_arg_types
8742 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8744 if (TREE_PUBLIC (decl1))
8746 /* If there is an external prototype declaration of this
8747 function, record its location but do not copy information
8748 to this decl. This may be an invisible declaration
8749 (built-in or in a scope which has finished) or simply
8750 have more refined argument types than any declaration
8751 found above. */
8752 struct c_binding *b;
8753 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8754 if (B_IN_SCOPE (b, external_scope))
8755 break;
8756 if (b)
8758 tree ext_decl, ext_type;
8759 ext_decl = b->decl;
8760 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8761 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8762 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8763 TREE_TYPE (ext_type)))
8765 current_function_prototype_locus
8766 = DECL_SOURCE_LOCATION (ext_decl);
8767 current_function_prototype_built_in
8768 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8769 current_function_prototype_arg_types
8770 = TYPE_ARG_TYPES (ext_type);
8776 /* Optionally warn of old-fashioned def with no previous prototype. */
8777 if (warn_strict_prototypes
8778 && old_decl != error_mark_node
8779 && !prototype_p (TREE_TYPE (decl1))
8780 && C_DECL_ISNT_PROTOTYPE (old_decl))
8781 warning_at (loc, OPT_Wstrict_prototypes,
8782 "function declaration isn%'t a prototype");
8783 /* Optionally warn of any global def with no previous prototype. */
8784 else if (warn_missing_prototypes
8785 && old_decl != error_mark_node
8786 && TREE_PUBLIC (decl1)
8787 && !MAIN_NAME_P (DECL_NAME (decl1))
8788 && C_DECL_ISNT_PROTOTYPE (old_decl)
8789 && !DECL_DECLARED_INLINE_P (decl1))
8790 warning_at (loc, OPT_Wmissing_prototypes,
8791 "no previous prototype for %qD", decl1);
8792 /* Optionally warn of any def with no previous prototype
8793 if the function has already been used. */
8794 else if (warn_missing_prototypes
8795 && old_decl != NULL_TREE
8796 && old_decl != error_mark_node
8797 && TREE_USED (old_decl)
8798 && !prototype_p (TREE_TYPE (old_decl)))
8799 warning_at (loc, OPT_Wmissing_prototypes,
8800 "%qD was used with no prototype before its definition", decl1);
8801 /* Optionally warn of any global def with no previous declaration. */
8802 else if (warn_missing_declarations
8803 && TREE_PUBLIC (decl1)
8804 && old_decl == NULL_TREE
8805 && !MAIN_NAME_P (DECL_NAME (decl1))
8806 && !DECL_DECLARED_INLINE_P (decl1))
8807 warning_at (loc, OPT_Wmissing_declarations,
8808 "no previous declaration for %qD",
8809 decl1);
8810 /* Optionally warn of any def with no previous declaration
8811 if the function has already been used. */
8812 else if (warn_missing_declarations
8813 && old_decl != NULL_TREE
8814 && old_decl != error_mark_node
8815 && TREE_USED (old_decl)
8816 && C_DECL_IMPLICIT (old_decl))
8817 warning_at (loc, OPT_Wmissing_declarations,
8818 "%qD was used with no declaration before its definition", decl1);
8820 /* This function exists in static storage.
8821 (This does not mean `static' in the C sense!) */
8822 TREE_STATIC (decl1) = 1;
8824 /* This is the earliest point at which we might know the assembler
8825 name of the function. Thus, if it's set before this, die horribly. */
8826 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8828 /* If #pragma weak was used, mark the decl weak now. */
8829 if (current_scope == file_scope)
8830 maybe_apply_pragma_weak (decl1);
8832 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8833 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8835 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8836 != integer_type_node)
8837 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8838 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8839 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8840 decl1);
8842 check_main_parameter_types (decl1);
8844 if (!TREE_PUBLIC (decl1))
8845 pedwarn (loc, OPT_Wmain,
8846 "%qD is normally a non-static function", decl1);
8849 /* Record the decl so that the function name is defined.
8850 If we already have a decl for this name, and it is a FUNCTION_DECL,
8851 use the old decl. */
8853 current_function_decl = pushdecl (decl1);
8855 push_scope ();
8856 declare_parm_level ();
8858 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8859 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8860 DECL_ARTIFICIAL (resdecl) = 1;
8861 DECL_IGNORED_P (resdecl) = 1;
8862 DECL_RESULT (current_function_decl) = resdecl;
8864 start_fname_decls ();
8866 return true;
8869 /* Subroutine of store_parm_decls which handles new-style function
8870 definitions (prototype format). The parms already have decls, so we
8871 need only record them as in effect and complain if any redundant
8872 old-style parm decls were written. */
8873 static void
8874 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8876 tree decl;
8877 c_arg_tag *tag;
8878 unsigned ix;
8880 if (current_scope->bindings)
8882 error_at (DECL_SOURCE_LOCATION (fndecl),
8883 "old-style parameter declarations in prototyped "
8884 "function definition");
8886 /* Get rid of the old-style declarations. */
8887 pop_scope ();
8888 push_scope ();
8890 /* Don't issue this warning for nested functions, and don't issue this
8891 warning if we got here because ARG_INFO_TYPES was error_mark_node
8892 (this happens when a function definition has just an ellipsis in
8893 its parameter list). */
8894 else if (!in_system_header_at (input_location)
8895 && !current_function_scope
8896 && arg_info->types != error_mark_node)
8897 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8898 "traditional C rejects ISO C style function definitions");
8900 /* Now make all the parameter declarations visible in the function body.
8901 We can bypass most of the grunt work of pushdecl. */
8902 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8904 DECL_CONTEXT (decl) = current_function_decl;
8905 if (DECL_NAME (decl))
8907 bind (DECL_NAME (decl), decl, current_scope,
8908 /*invisible=*/false, /*nested=*/false,
8909 UNKNOWN_LOCATION);
8910 if (!TREE_USED (decl))
8911 warn_if_shadowing (decl);
8913 else
8914 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8917 /* Record the parameter list in the function declaration. */
8918 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8920 /* Now make all the ancillary declarations visible, likewise. */
8921 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8923 DECL_CONTEXT (decl) = current_function_decl;
8924 if (DECL_NAME (decl))
8925 bind (DECL_NAME (decl), decl, current_scope,
8926 /*invisible=*/false,
8927 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8928 UNKNOWN_LOCATION);
8931 /* And all the tag declarations. */
8932 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8933 if (tag->id)
8934 bind (tag->id, tag->type, current_scope,
8935 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8938 /* Subroutine of store_parm_decls which handles old-style function
8939 definitions (separate parameter list and declarations). */
8941 static void
8942 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8944 struct c_binding *b;
8945 tree parm, decl, last;
8946 tree parmids = arg_info->parms;
8947 hash_set<tree> seen_args;
8949 if (!in_system_header_at (input_location))
8950 warning_at (DECL_SOURCE_LOCATION (fndecl),
8951 OPT_Wold_style_definition, "old-style function definition");
8953 /* Match each formal parameter name with its declaration. Save each
8954 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8955 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8957 if (TREE_VALUE (parm) == NULL_TREE)
8959 error_at (DECL_SOURCE_LOCATION (fndecl),
8960 "parameter name missing from parameter list");
8961 TREE_PURPOSE (parm) = NULL_TREE;
8962 continue;
8965 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8966 if (b && B_IN_CURRENT_SCOPE (b))
8968 decl = b->decl;
8969 /* Skip erroneous parameters. */
8970 if (decl == error_mark_node)
8971 continue;
8972 /* If we got something other than a PARM_DECL it is an error. */
8973 if (TREE_CODE (decl) != PARM_DECL)
8975 error_at (DECL_SOURCE_LOCATION (decl),
8976 "%qD declared as a non-parameter", decl);
8977 continue;
8979 /* If the declaration is already marked, we have a duplicate
8980 name. Complain and ignore the duplicate. */
8981 else if (seen_args.contains (decl))
8983 error_at (DECL_SOURCE_LOCATION (decl),
8984 "multiple parameters named %qD", decl);
8985 TREE_PURPOSE (parm) = NULL_TREE;
8986 continue;
8988 /* If the declaration says "void", complain and turn it into
8989 an int. */
8990 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8992 error_at (DECL_SOURCE_LOCATION (decl),
8993 "parameter %qD declared with void type", decl);
8994 TREE_TYPE (decl) = integer_type_node;
8995 DECL_ARG_TYPE (decl) = integer_type_node;
8996 layout_decl (decl, 0);
8998 warn_if_shadowing (decl);
9000 /* If no declaration found, default to int. */
9001 else
9003 /* FIXME diagnostics: This should be the location of the argument,
9004 not the FNDECL. E.g., for an old-style declaration
9006 int f10(v) { blah; }
9008 We should use the location of the V, not the F10.
9009 Unfortunately, the V is an IDENTIFIER_NODE which has no
9010 location. In the future we need locations for c_arg_info
9011 entries.
9013 See gcc.dg/Wshadow-3.c for an example of this problem. */
9014 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9015 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9016 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9017 pushdecl (decl);
9018 warn_if_shadowing (decl);
9020 if (flag_isoc99)
9021 pedwarn (DECL_SOURCE_LOCATION (decl),
9022 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9023 decl);
9024 else
9025 warning_at (DECL_SOURCE_LOCATION (decl),
9026 OPT_Wmissing_parameter_type,
9027 "type of %qD defaults to %<int%>", decl);
9030 TREE_PURPOSE (parm) = decl;
9031 seen_args.add (decl);
9034 /* Now examine the parms chain for incomplete declarations
9035 and declarations with no corresponding names. */
9037 for (b = current_scope->bindings; b; b = b->prev)
9039 parm = b->decl;
9040 if (TREE_CODE (parm) != PARM_DECL)
9041 continue;
9043 if (TREE_TYPE (parm) != error_mark_node
9044 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9046 error_at (DECL_SOURCE_LOCATION (parm),
9047 "parameter %qD has incomplete type", parm);
9048 TREE_TYPE (parm) = error_mark_node;
9051 if (!seen_args.contains (parm))
9053 error_at (DECL_SOURCE_LOCATION (parm),
9054 "declaration for parameter %qD but no such parameter",
9055 parm);
9057 /* Pretend the parameter was not missing.
9058 This gets us to a standard state and minimizes
9059 further error messages. */
9060 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9064 /* Chain the declarations together in the order of the list of
9065 names. Store that chain in the function decl, replacing the
9066 list of names. Update the current scope to match. */
9067 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9069 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9070 if (TREE_PURPOSE (parm))
9071 break;
9072 if (parm && TREE_PURPOSE (parm))
9074 last = TREE_PURPOSE (parm);
9075 DECL_ARGUMENTS (fndecl) = last;
9077 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9078 if (TREE_PURPOSE (parm))
9080 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9081 last = TREE_PURPOSE (parm);
9083 DECL_CHAIN (last) = NULL_TREE;
9086 /* If there was a previous prototype,
9087 set the DECL_ARG_TYPE of each argument according to
9088 the type previously specified, and report any mismatches. */
9090 if (current_function_prototype_arg_types)
9092 tree type;
9093 for (parm = DECL_ARGUMENTS (fndecl),
9094 type = current_function_prototype_arg_types;
9095 parm || (type != NULL_TREE
9096 && TREE_VALUE (type) != error_mark_node
9097 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9098 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9100 if (parm == NULL_TREE
9101 || type == NULL_TREE
9102 || (TREE_VALUE (type) != error_mark_node
9103 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9105 if (current_function_prototype_built_in)
9106 warning_at (DECL_SOURCE_LOCATION (fndecl),
9107 0, "number of arguments doesn%'t match "
9108 "built-in prototype");
9109 else
9111 /* FIXME diagnostics: This should be the location of
9112 FNDECL, but there is bug when a prototype is
9113 declared inside function context, but defined
9114 outside of it (e.g., gcc.dg/pr15698-2.c). In
9115 which case FNDECL gets the location of the
9116 prototype, not the definition. */
9117 error_at (input_location,
9118 "number of arguments doesn%'t match prototype");
9120 error_at (current_function_prototype_locus,
9121 "prototype declaration");
9123 break;
9125 /* Type for passing arg must be consistent with that
9126 declared for the arg. ISO C says we take the unqualified
9127 type for parameters declared with qualified type. */
9128 if (TREE_TYPE (parm) != error_mark_node
9129 && TREE_VALUE (type) != error_mark_node
9130 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9131 != TYPE_ATOMIC (TREE_VALUE (type)))
9132 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9133 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9135 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9136 == TYPE_ATOMIC (TREE_VALUE (type)))
9137 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9138 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9140 /* Adjust argument to match prototype. E.g. a previous
9141 `int foo(float);' prototype causes
9142 `int foo(x) float x; {...}' to be treated like
9143 `int foo(float x) {...}'. This is particularly
9144 useful for argument types like uid_t. */
9145 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9147 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9148 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9149 && (TYPE_PRECISION (TREE_TYPE (parm))
9150 < TYPE_PRECISION (integer_type_node)))
9151 DECL_ARG_TYPE (parm)
9152 = c_type_promotes_to (TREE_TYPE (parm));
9154 /* ??? Is it possible to get here with a
9155 built-in prototype or will it always have
9156 been diagnosed as conflicting with an
9157 old-style definition and discarded? */
9158 if (current_function_prototype_built_in)
9159 warning_at (DECL_SOURCE_LOCATION (parm),
9160 OPT_Wpedantic, "promoted argument %qD "
9161 "doesn%'t match built-in prototype", parm);
9162 else
9164 pedwarn (DECL_SOURCE_LOCATION (parm),
9165 OPT_Wpedantic, "promoted argument %qD "
9166 "doesn%'t match prototype", parm);
9167 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9168 "prototype declaration");
9171 else
9173 if (current_function_prototype_built_in)
9174 warning_at (DECL_SOURCE_LOCATION (parm),
9175 0, "argument %qD doesn%'t match "
9176 "built-in prototype", parm);
9177 else
9179 error_at (DECL_SOURCE_LOCATION (parm),
9180 "argument %qD doesn%'t match prototype", parm);
9181 error_at (current_function_prototype_locus,
9182 "prototype declaration");
9187 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9190 /* Otherwise, create a prototype that would match. */
9192 else
9194 tree actual = NULL_TREE, last = NULL_TREE, type;
9196 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9198 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9199 if (last)
9200 TREE_CHAIN (last) = type;
9201 else
9202 actual = type;
9203 last = type;
9205 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9206 if (last)
9207 TREE_CHAIN (last) = type;
9208 else
9209 actual = type;
9211 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9212 of the type of this function, but we need to avoid having this
9213 affect the types of other similarly-typed functions, so we must
9214 first force the generation of an identical (but separate) type
9215 node for the relevant function type. The new node we create
9216 will be a variant of the main variant of the original function
9217 type. */
9219 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9221 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9225 /* Store parameter declarations passed in ARG_INFO into the current
9226 function declaration. */
9228 void
9229 store_parm_decls_from (struct c_arg_info *arg_info)
9231 current_function_arg_info = arg_info;
9232 store_parm_decls ();
9235 /* Called by walk_tree to look for and update context-less labels. */
9237 static tree
9238 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9240 if (TREE_CODE (*tp) == LABEL_EXPR
9241 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9243 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9244 *walk_subtrees = 0;
9247 return NULL_TREE;
9250 /* Store the parameter declarations into the current function declaration.
9251 This is called after parsing the parameter declarations, before
9252 digesting the body of the function.
9254 For an old-style definition, construct a prototype out of the old-style
9255 parameter declarations and inject it into the function's type. */
9257 void
9258 store_parm_decls (void)
9260 tree fndecl = current_function_decl;
9261 bool proto;
9263 /* The argument information block for FNDECL. */
9264 struct c_arg_info *arg_info = current_function_arg_info;
9265 current_function_arg_info = 0;
9267 /* True if this definition is written with a prototype. Note:
9268 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9269 list in a function definition as equivalent to (void) -- an
9270 empty argument list specifies the function has no parameters,
9271 but only (void) sets up a prototype for future calls. */
9272 proto = arg_info->types != 0;
9274 if (proto)
9275 store_parm_decls_newstyle (fndecl, arg_info);
9276 else
9277 store_parm_decls_oldstyle (fndecl, arg_info);
9279 /* The next call to push_scope will be a function body. */
9281 next_is_function_body = true;
9283 /* Write a record describing this function definition to the prototypes
9284 file (if requested). */
9286 gen_aux_info_record (fndecl, 1, 0, proto);
9288 /* Initialize the RTL code for the function. */
9289 allocate_struct_function (fndecl, false);
9291 if (warn_unused_local_typedefs)
9292 cfun->language = ggc_cleared_alloc<language_function> ();
9294 /* Begin the statement tree for this function. */
9295 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9297 /* ??? Insert the contents of the pending sizes list into the function
9298 to be evaluated. The only reason left to have this is
9299 void foo(int n, int array[n++])
9300 because we throw away the array type in favor of a pointer type, and
9301 thus won't naturally see the SAVE_EXPR containing the increment. All
9302 other pending sizes would be handled by gimplify_parameters. */
9303 if (arg_info->pending_sizes)
9305 /* In very special circumstances, e.g. for code like
9306 _Atomic int i = 5;
9307 void f (int a[i += 2]) {}
9308 we need to execute the atomic assignment on function entry.
9309 But in this case, it is not just a straight store, it has the
9310 op= form, which means that build_atomic_assign has generated
9311 gotos, labels, etc. Because at that time the function decl
9312 for F has not been created yet, those labels do not have any
9313 function context. But we have the fndecl now, so update the
9314 labels accordingly. gimplify_expr would crash otherwise. */
9315 walk_tree_without_duplicates (&arg_info->pending_sizes,
9316 set_labels_context_r, fndecl);
9317 add_stmt (arg_info->pending_sizes);
9321 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9322 c_finish_omp_declare_simd for function prototypes. No diagnostics
9323 should be done. */
9325 void
9326 temp_store_parm_decls (tree fndecl, tree parms)
9328 push_scope ();
9329 for (tree p = parms; p; p = DECL_CHAIN (p))
9331 DECL_CONTEXT (p) = fndecl;
9332 if (DECL_NAME (p))
9333 bind (DECL_NAME (p), p, current_scope,
9334 /*invisible=*/false, /*nested=*/false,
9335 UNKNOWN_LOCATION);
9339 /* Undo what temp_store_parm_decls did. */
9341 void
9342 temp_pop_parm_decls (void)
9344 /* Clear all bindings in this temporary scope, so that
9345 pop_scope doesn't create a BLOCK. */
9346 struct c_binding *b = current_scope->bindings;
9347 current_scope->bindings = NULL;
9348 for (; b; b = free_binding_and_advance (b))
9350 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9351 || b->decl == error_mark_node);
9352 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9353 I_SYMBOL_BINDING (b->id) = b->shadowed;
9354 if (b->shadowed && b->shadowed->u.type)
9355 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9357 pop_scope ();
9361 /* Finish up a function declaration and compile that function
9362 all the way to assembler language output. Then free the storage
9363 for the function definition.
9365 This is called after parsing the body of the function definition. */
9367 void
9368 finish_function (void)
9370 tree fndecl = current_function_decl;
9372 if (c_dialect_objc ())
9373 objc_finish_function ();
9375 if (TREE_CODE (fndecl) == FUNCTION_DECL
9376 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9378 tree args = DECL_ARGUMENTS (fndecl);
9379 for (; args; args = DECL_CHAIN (args))
9381 tree type = TREE_TYPE (args);
9382 if (INTEGRAL_TYPE_P (type)
9383 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9384 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9388 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9389 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9391 /* Must mark the RESULT_DECL as being in this function. */
9393 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9394 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9396 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9397 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9398 == integer_type_node && flag_isoc99)
9400 /* Hack. We don't want the middle-end to warn that this return
9401 is unreachable, so we mark its location as special. Using
9402 UNKNOWN_LOCATION has the problem that it gets clobbered in
9403 annotate_one_with_locus. A cleaner solution might be to
9404 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9406 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9409 /* Tie off the statement tree for this function. */
9410 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9412 finish_fname_decls ();
9414 /* Complain if there's just no return statement. */
9415 if (warn_return_type
9416 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9417 && !current_function_returns_value && !current_function_returns_null
9418 /* Don't complain if we are no-return. */
9419 && !current_function_returns_abnormally
9420 /* Don't complain if we are declared noreturn. */
9421 && !TREE_THIS_VOLATILE (fndecl)
9422 /* Don't warn for main(). */
9423 && !MAIN_NAME_P (DECL_NAME (fndecl))
9424 /* Or if they didn't actually specify a return type. */
9425 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9426 /* Normally, with -Wreturn-type, flow will complain, but we might
9427 optimize out static functions. */
9428 && !TREE_PUBLIC (fndecl))
9430 warning (OPT_Wreturn_type,
9431 "no return statement in function returning non-void");
9432 TREE_NO_WARNING (fndecl) = 1;
9435 /* Complain about parameters that are only set, but never otherwise used. */
9436 if (warn_unused_but_set_parameter)
9438 tree decl;
9440 for (decl = DECL_ARGUMENTS (fndecl);
9441 decl;
9442 decl = DECL_CHAIN (decl))
9443 if (TREE_USED (decl)
9444 && TREE_CODE (decl) == PARM_DECL
9445 && !DECL_READ_P (decl)
9446 && DECL_NAME (decl)
9447 && !DECL_ARTIFICIAL (decl)
9448 && !TREE_NO_WARNING (decl))
9449 warning_at (DECL_SOURCE_LOCATION (decl),
9450 OPT_Wunused_but_set_parameter,
9451 "parameter %qD set but not used", decl);
9454 /* Complain about locally defined typedefs that are not used in this
9455 function. */
9456 maybe_warn_unused_local_typedefs ();
9458 /* Possibly warn about unused parameters. */
9459 if (warn_unused_parameter)
9460 do_warn_unused_parameter (fndecl);
9462 /* Store the end of the function, so that we get good line number
9463 info for the epilogue. */
9464 cfun->function_end_locus = input_location;
9466 /* Finalize the ELF visibility for the function. */
9467 c_determine_visibility (fndecl);
9469 /* For GNU C extern inline functions disregard inline limits. */
9470 if (DECL_EXTERNAL (fndecl)
9471 && DECL_DECLARED_INLINE_P (fndecl)
9472 && (flag_gnu89_inline
9473 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9474 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9476 /* Genericize before inlining. Delay genericizing nested functions
9477 until their parent function is genericized. Since finalizing
9478 requires GENERIC, delay that as well. */
9480 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9481 && !undef_nested_function)
9483 if (!decl_function_context (fndecl))
9485 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9486 c_genericize (fndecl);
9488 /* ??? Objc emits functions after finalizing the compilation unit.
9489 This should be cleaned up later and this conditional removed. */
9490 if (symtab->global_info_ready)
9492 cgraph_node::add_new_function (fndecl, false);
9493 return;
9495 cgraph_node::finalize_function (fndecl, false);
9497 else
9499 /* Register this function with cgraph just far enough to get it
9500 added to our parent's nested function list. Handy, since the
9501 C front end doesn't have such a list. */
9502 (void) cgraph_node::get_create (fndecl);
9506 if (!decl_function_context (fndecl))
9507 undef_nested_function = false;
9509 if (cfun->language != NULL)
9511 ggc_free (cfun->language);
9512 cfun->language = NULL;
9515 /* We're leaving the context of this function, so zap cfun.
9516 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9517 tree_rest_of_compilation. */
9518 set_cfun (NULL);
9519 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9520 current_function_decl = NULL;
9523 /* Check the declarations given in a for-loop for satisfying the C99
9524 constraints. If exactly one such decl is found, return it. LOC is
9525 the location of the opening parenthesis of the for loop. The last
9526 parameter allows you to control the "for loop initial declarations
9527 are only allowed in C99 mode". Normally, you should pass
9528 flag_isoc99 as that parameter. But in some cases (Objective-C
9529 foreach loop, for example) we want to run the checks in this
9530 function even if not in C99 mode, so we allow the caller to turn
9531 off the error about not being in C99 mode.
9534 tree
9535 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9537 struct c_binding *b;
9538 tree one_decl = NULL_TREE;
9539 int n_decls = 0;
9541 if (!turn_off_iso_c99_error)
9543 static bool hint = true;
9544 /* If we get here, declarations have been used in a for loop without
9545 the C99 for loop scope. This doesn't make much sense, so don't
9546 allow it. */
9547 error_at (loc, "%<for%> loop initial declarations "
9548 "are only allowed in C99 or C11 mode");
9549 if (hint)
9551 inform (loc,
9552 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9553 "to compile your code");
9554 hint = false;
9556 return NULL_TREE;
9558 /* C99 subclause 6.8.5 paragraph 3:
9560 [#3] The declaration part of a for statement shall only
9561 declare identifiers for objects having storage class auto or
9562 register.
9564 It isn't clear whether, in this sentence, "identifiers" binds to
9565 "shall only declare" or to "objects" - that is, whether all identifiers
9566 declared must be identifiers for objects, or whether the restriction
9567 only applies to those that are. (A question on this in comp.std.c
9568 in November 2000 received no answer.) We implement the strictest
9569 interpretation, to avoid creating an extension which later causes
9570 problems. */
9572 for (b = current_scope->bindings; b; b = b->prev)
9574 tree id = b->id;
9575 tree decl = b->decl;
9577 if (!id)
9578 continue;
9580 switch (TREE_CODE (decl))
9582 case VAR_DECL:
9584 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9585 if (TREE_STATIC (decl))
9586 error_at (decl_loc,
9587 "declaration of static variable %qD in %<for%> loop "
9588 "initial declaration", decl);
9589 else if (DECL_EXTERNAL (decl))
9590 error_at (decl_loc,
9591 "declaration of %<extern%> variable %qD in %<for%> loop "
9592 "initial declaration", decl);
9594 break;
9596 case RECORD_TYPE:
9597 error_at (loc,
9598 "%<struct %E%> declared in %<for%> loop initial "
9599 "declaration", id);
9600 break;
9601 case UNION_TYPE:
9602 error_at (loc,
9603 "%<union %E%> declared in %<for%> loop initial declaration",
9604 id);
9605 break;
9606 case ENUMERAL_TYPE:
9607 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9608 "initial declaration", id);
9609 break;
9610 default:
9611 error_at (loc, "declaration of non-variable "
9612 "%qD in %<for%> loop initial declaration", decl);
9615 n_decls++;
9616 one_decl = decl;
9619 return n_decls == 1 ? one_decl : NULL_TREE;
9622 /* Save and reinitialize the variables
9623 used during compilation of a C function. */
9625 void
9626 c_push_function_context (void)
9628 struct language_function *p = cfun->language;
9629 /* cfun->language might have been already allocated by the use of
9630 -Wunused-local-typedefs. In that case, just re-use it. */
9631 if (p == NULL)
9632 cfun->language = p = ggc_cleared_alloc<language_function> ();
9634 p->base.x_stmt_tree = c_stmt_tree;
9635 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9636 p->x_break_label = c_break_label;
9637 p->x_cont_label = c_cont_label;
9638 p->x_switch_stack = c_switch_stack;
9639 p->arg_info = current_function_arg_info;
9640 p->returns_value = current_function_returns_value;
9641 p->returns_null = current_function_returns_null;
9642 p->returns_abnormally = current_function_returns_abnormally;
9643 p->warn_about_return_type = warn_about_return_type;
9645 push_function_context ();
9648 /* Restore the variables used during compilation of a C function. */
9650 void
9651 c_pop_function_context (void)
9653 struct language_function *p;
9655 pop_function_context ();
9656 p = cfun->language;
9658 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9659 used to store data throughout the life time of the current cfun,
9660 So don't deallocate it. */
9661 if (!warn_unused_local_typedefs)
9662 cfun->language = NULL;
9664 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9665 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9667 /* Stop pointing to the local nodes about to be freed. */
9668 /* But DECL_INITIAL must remain nonzero so we know this
9669 was an actual function definition. */
9670 DECL_INITIAL (current_function_decl) = error_mark_node;
9671 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9674 c_stmt_tree = p->base.x_stmt_tree;
9675 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9676 c_break_label = p->x_break_label;
9677 c_cont_label = p->x_cont_label;
9678 c_switch_stack = p->x_switch_stack;
9679 current_function_arg_info = p->arg_info;
9680 current_function_returns_value = p->returns_value;
9681 current_function_returns_null = p->returns_null;
9682 current_function_returns_abnormally = p->returns_abnormally;
9683 warn_about_return_type = p->warn_about_return_type;
9686 /* The functions below are required for functionality of doing
9687 function at once processing in the C front end. Currently these
9688 functions are not called from anywhere in the C front end, but as
9689 these changes continue, that will change. */
9691 /* Returns the stmt_tree (if any) to which statements are currently
9692 being added. If there is no active statement-tree, NULL is
9693 returned. */
9695 stmt_tree
9696 current_stmt_tree (void)
9698 return &c_stmt_tree;
9701 /* Return the global value of T as a symbol. */
9703 tree
9704 identifier_global_value (tree t)
9706 struct c_binding *b;
9708 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9709 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9710 return b->decl;
9712 return NULL_TREE;
9715 /* In C, the only C-linkage public declaration is at file scope. */
9717 tree
9718 c_linkage_bindings (tree name)
9720 return identifier_global_value (name);
9723 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9724 otherwise the name is found in ridpointers from RID_INDEX. */
9726 void
9727 record_builtin_type (enum rid rid_index, const char *name, tree type)
9729 tree id, decl;
9730 if (name == 0)
9731 id = ridpointers[(int) rid_index];
9732 else
9733 id = get_identifier (name);
9734 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9735 pushdecl (decl);
9736 if (debug_hooks->type_decl)
9737 debug_hooks->type_decl (decl, false);
9740 /* Build the void_list_node (void_type_node having been created). */
9741 tree
9742 build_void_list_node (void)
9744 tree t = build_tree_list (NULL_TREE, void_type_node);
9745 return t;
9748 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9750 struct c_parm *
9751 build_c_parm (struct c_declspecs *specs, tree attrs,
9752 struct c_declarator *declarator,
9753 location_t loc)
9755 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9756 ret->specs = specs;
9757 ret->attrs = attrs;
9758 ret->declarator = declarator;
9759 ret->loc = loc;
9760 return ret;
9763 /* Return a declarator with nested attributes. TARGET is the inner
9764 declarator to which these attributes apply. ATTRS are the
9765 attributes. */
9767 struct c_declarator *
9768 build_attrs_declarator (tree attrs, struct c_declarator *target)
9770 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9771 ret->kind = cdk_attrs;
9772 ret->declarator = target;
9773 ret->u.attrs = attrs;
9774 return ret;
9777 /* Return a declarator for a function with arguments specified by ARGS
9778 and return type specified by TARGET. */
9780 struct c_declarator *
9781 build_function_declarator (struct c_arg_info *args,
9782 struct c_declarator *target)
9784 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9785 ret->kind = cdk_function;
9786 ret->declarator = target;
9787 ret->u.arg_info = args;
9788 return ret;
9791 /* Return a declarator for the identifier IDENT (which may be
9792 NULL_TREE for an abstract declarator). */
9794 struct c_declarator *
9795 build_id_declarator (tree ident)
9797 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9798 ret->kind = cdk_id;
9799 ret->declarator = 0;
9800 ret->u.id = ident;
9801 /* Default value - may get reset to a more precise location. */
9802 ret->id_loc = input_location;
9803 return ret;
9806 /* Return something to represent absolute declarators containing a *.
9807 TARGET is the absolute declarator that the * contains.
9808 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9809 to apply to the pointer type. */
9811 struct c_declarator *
9812 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9813 struct c_declarator *target)
9815 tree attrs;
9816 int quals = 0;
9817 struct c_declarator *itarget = target;
9818 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9819 if (type_quals_attrs)
9821 attrs = type_quals_attrs->attrs;
9822 quals = quals_from_declspecs (type_quals_attrs);
9823 if (attrs != NULL_TREE)
9824 itarget = build_attrs_declarator (attrs, target);
9826 ret->kind = cdk_pointer;
9827 ret->declarator = itarget;
9828 ret->u.pointer_quals = quals;
9829 return ret;
9832 /* Return a pointer to a structure for an empty list of declaration
9833 specifiers. */
9835 struct c_declspecs *
9836 build_null_declspecs (void)
9838 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9839 memset (ret, 0, sizeof *ret);
9840 ret->align_log = -1;
9841 ret->typespec_word = cts_none;
9842 ret->storage_class = csc_none;
9843 ret->expr_const_operands = true;
9844 ret->typespec_kind = ctsk_none;
9845 ret->address_space = ADDR_SPACE_GENERIC;
9846 return ret;
9849 /* Add the address space ADDRSPACE to the declaration specifiers
9850 SPECS, returning SPECS. */
9852 struct c_declspecs *
9853 declspecs_add_addrspace (source_location location,
9854 struct c_declspecs *specs, addr_space_t as)
9856 specs->non_sc_seen_p = true;
9857 specs->declspecs_seen_p = true;
9859 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9860 && specs->address_space != as)
9861 error ("incompatible address space qualifiers %qs and %qs",
9862 c_addr_space_name (as),
9863 c_addr_space_name (specs->address_space));
9864 else
9866 specs->address_space = as;
9867 specs->locations[cdw_address_space] = location;
9869 return specs;
9872 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9873 returning SPECS. */
9875 struct c_declspecs *
9876 declspecs_add_qual (source_location loc,
9877 struct c_declspecs *specs, tree qual)
9879 enum rid i;
9880 bool dupe = false;
9881 specs->non_sc_seen_p = true;
9882 specs->declspecs_seen_p = true;
9883 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9884 && C_IS_RESERVED_WORD (qual));
9885 i = C_RID_CODE (qual);
9886 location_t prev_loc = UNKNOWN_LOCATION;
9887 switch (i)
9889 case RID_CONST:
9890 dupe = specs->const_p;
9891 specs->const_p = true;
9892 prev_loc = specs->locations[cdw_const];
9893 specs->locations[cdw_const] = loc;
9894 break;
9895 case RID_VOLATILE:
9896 dupe = specs->volatile_p;
9897 specs->volatile_p = true;
9898 prev_loc = specs->locations[cdw_volatile];
9899 specs->locations[cdw_volatile] = loc;
9900 break;
9901 case RID_RESTRICT:
9902 dupe = specs->restrict_p;
9903 specs->restrict_p = true;
9904 prev_loc = specs->locations[cdw_restrict];
9905 specs->locations[cdw_restrict] = loc;
9906 break;
9907 case RID_ATOMIC:
9908 dupe = specs->atomic_p;
9909 specs->atomic_p = true;
9910 prev_loc = specs->locations[cdw_atomic];
9911 specs->locations[cdw_atomic] = loc;
9912 break;
9913 default:
9914 gcc_unreachable ();
9916 if (dupe)
9918 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9919 "duplicate %qE declaration specifier", qual);
9920 if (!warned
9921 && warn_duplicate_decl_specifier
9922 && prev_loc >= RESERVED_LOCATION_COUNT
9923 && !from_macro_expansion_at (prev_loc)
9924 && !from_macro_expansion_at (loc))
9925 warning_at (loc, OPT_Wduplicate_decl_specifier,
9926 "duplicate %qE declaration specifier", qual);
9928 return specs;
9931 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9932 returning SPECS. */
9934 struct c_declspecs *
9935 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9936 struct c_typespec spec)
9938 tree type = spec.spec;
9939 specs->non_sc_seen_p = true;
9940 specs->declspecs_seen_p = true;
9941 specs->typespec_kind = spec.kind;
9942 if (TREE_DEPRECATED (type))
9943 specs->deprecated_p = true;
9945 /* Handle type specifier keywords. */
9946 if (TREE_CODE (type) == IDENTIFIER_NODE
9947 && C_IS_RESERVED_WORD (type)
9948 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9950 enum rid i = C_RID_CODE (type);
9951 if (specs->type)
9953 error_at (loc, "two or more data types in declaration specifiers");
9954 return specs;
9956 if ((int) i <= (int) RID_LAST_MODIFIER)
9958 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9959 bool dupe = false;
9960 switch (i)
9962 case RID_LONG:
9963 if (specs->long_long_p)
9965 error_at (loc, "%<long long long%> is too long for GCC");
9966 break;
9968 if (specs->long_p)
9970 if (specs->typespec_word == cts_double)
9972 error_at (loc,
9973 ("both %<long long%> and %<double%> in "
9974 "declaration specifiers"));
9975 break;
9977 pedwarn_c90 (loc, OPT_Wlong_long,
9978 "ISO C90 does not support %<long long%>");
9979 specs->long_long_p = 1;
9980 specs->locations[cdw_long_long] = loc;
9981 break;
9983 if (specs->short_p)
9984 error_at (loc,
9985 ("both %<long%> and %<short%> in "
9986 "declaration specifiers"));
9987 else if (specs->typespec_word == cts_auto_type)
9988 error_at (loc,
9989 ("both %<long%> and %<__auto_type%> in "
9990 "declaration specifiers"));
9991 else if (specs->typespec_word == cts_void)
9992 error_at (loc,
9993 ("both %<long%> and %<void%> in "
9994 "declaration specifiers"));
9995 else if (specs->typespec_word == cts_int_n)
9996 error_at (loc,
9997 ("both %<long%> and %<__int%d%> in "
9998 "declaration specifiers"),
9999 int_n_data[specs->int_n_idx].bitsize);
10000 else if (specs->typespec_word == cts_bool)
10001 error_at (loc,
10002 ("both %<long%> and %<_Bool%> in "
10003 "declaration specifiers"));
10004 else if (specs->typespec_word == cts_char)
10005 error_at (loc,
10006 ("both %<long%> and %<char%> in "
10007 "declaration specifiers"));
10008 else if (specs->typespec_word == cts_float)
10009 error_at (loc,
10010 ("both %<long%> and %<float%> in "
10011 "declaration specifiers"));
10012 else if (specs->typespec_word == cts_floatn_nx)
10013 error_at (loc,
10014 ("both %<long%> and %<_Float%d%s%> in "
10015 "declaration specifiers"),
10016 floatn_nx_types[specs->floatn_nx_idx].n,
10017 (floatn_nx_types[specs->floatn_nx_idx].extended
10018 ? "x"
10019 : ""));
10020 else if (specs->typespec_word == cts_dfloat32)
10021 error_at (loc,
10022 ("both %<long%> and %<_Decimal32%> in "
10023 "declaration specifiers"));
10024 else if (specs->typespec_word == cts_dfloat64)
10025 error_at (loc,
10026 ("both %<long%> and %<_Decimal64%> in "
10027 "declaration specifiers"));
10028 else if (specs->typespec_word == cts_dfloat128)
10029 error_at (loc,
10030 ("both %<long%> and %<_Decimal128%> in "
10031 "declaration specifiers"));
10032 else
10034 specs->long_p = true;
10035 specs->locations[cdw_long] = loc;
10037 break;
10038 case RID_SHORT:
10039 dupe = specs->short_p;
10040 if (specs->long_p)
10041 error_at (loc,
10042 ("both %<long%> and %<short%> in "
10043 "declaration specifiers"));
10044 else if (specs->typespec_word == cts_auto_type)
10045 error_at (loc,
10046 ("both %<short%> and %<__auto_type%> in "
10047 "declaration specifiers"));
10048 else if (specs->typespec_word == cts_void)
10049 error_at (loc,
10050 ("both %<short%> and %<void%> in "
10051 "declaration specifiers"));
10052 else if (specs->typespec_word == cts_int_n)
10053 error_at (loc,
10054 ("both %<short%> and %<__int%d%> in "
10055 "declaration specifiers"),
10056 int_n_data[specs->int_n_idx].bitsize);
10057 else if (specs->typespec_word == cts_bool)
10058 error_at (loc,
10059 ("both %<short%> and %<_Bool%> in "
10060 "declaration specifiers"));
10061 else if (specs->typespec_word == cts_char)
10062 error_at (loc,
10063 ("both %<short%> and %<char%> in "
10064 "declaration specifiers"));
10065 else if (specs->typespec_word == cts_float)
10066 error_at (loc,
10067 ("both %<short%> and %<float%> in "
10068 "declaration specifiers"));
10069 else if (specs->typespec_word == cts_double)
10070 error_at (loc,
10071 ("both %<short%> and %<double%> in "
10072 "declaration specifiers"));
10073 else if (specs->typespec_word == cts_floatn_nx)
10074 error_at (loc,
10075 ("both %<short%> and %<_Float%d%s%> in "
10076 "declaration specifiers"),
10077 floatn_nx_types[specs->floatn_nx_idx].n,
10078 (floatn_nx_types[specs->floatn_nx_idx].extended
10079 ? "x"
10080 : ""));
10081 else if (specs->typespec_word == cts_dfloat32)
10082 error_at (loc,
10083 ("both %<short%> and %<_Decimal32%> in "
10084 "declaration specifiers"));
10085 else if (specs->typespec_word == cts_dfloat64)
10086 error_at (loc,
10087 ("both %<short%> and %<_Decimal64%> in "
10088 "declaration specifiers"));
10089 else if (specs->typespec_word == cts_dfloat128)
10090 error_at (loc,
10091 ("both %<short%> and %<_Decimal128%> in "
10092 "declaration specifiers"));
10093 else
10095 specs->short_p = true;
10096 specs->locations[cdw_short] = loc;
10098 break;
10099 case RID_SIGNED:
10100 dupe = specs->signed_p;
10101 if (specs->unsigned_p)
10102 error_at (loc,
10103 ("both %<signed%> and %<unsigned%> in "
10104 "declaration specifiers"));
10105 else if (specs->typespec_word == cts_auto_type)
10106 error_at (loc,
10107 ("both %<signed%> and %<__auto_type%> in "
10108 "declaration specifiers"));
10109 else if (specs->typespec_word == cts_void)
10110 error_at (loc,
10111 ("both %<signed%> and %<void%> in "
10112 "declaration specifiers"));
10113 else if (specs->typespec_word == cts_bool)
10114 error_at (loc,
10115 ("both %<signed%> and %<_Bool%> in "
10116 "declaration specifiers"));
10117 else if (specs->typespec_word == cts_float)
10118 error_at (loc,
10119 ("both %<signed%> and %<float%> in "
10120 "declaration specifiers"));
10121 else if (specs->typespec_word == cts_double)
10122 error_at (loc,
10123 ("both %<signed%> and %<double%> in "
10124 "declaration specifiers"));
10125 else if (specs->typespec_word == cts_floatn_nx)
10126 error_at (loc,
10127 ("both %<signed%> and %<_Float%d%s%> in "
10128 "declaration specifiers"),
10129 floatn_nx_types[specs->floatn_nx_idx].n,
10130 (floatn_nx_types[specs->floatn_nx_idx].extended
10131 ? "x"
10132 : ""));
10133 else if (specs->typespec_word == cts_dfloat32)
10134 error_at (loc,
10135 ("both %<signed%> and %<_Decimal32%> in "
10136 "declaration specifiers"));
10137 else if (specs->typespec_word == cts_dfloat64)
10138 error_at (loc,
10139 ("both %<signed%> and %<_Decimal64%> in "
10140 "declaration specifiers"));
10141 else if (specs->typespec_word == cts_dfloat128)
10142 error_at (loc,
10143 ("both %<signed%> and %<_Decimal128%> in "
10144 "declaration specifiers"));
10145 else
10147 specs->signed_p = true;
10148 specs->locations[cdw_signed] = loc;
10150 break;
10151 case RID_UNSIGNED:
10152 dupe = specs->unsigned_p;
10153 if (specs->signed_p)
10154 error_at (loc,
10155 ("both %<signed%> and %<unsigned%> in "
10156 "declaration specifiers"));
10157 else if (specs->typespec_word == cts_auto_type)
10158 error_at (loc,
10159 ("both %<unsigned%> and %<__auto_type%> in "
10160 "declaration specifiers"));
10161 else if (specs->typespec_word == cts_void)
10162 error_at (loc,
10163 ("both %<unsigned%> and %<void%> in "
10164 "declaration specifiers"));
10165 else if (specs->typespec_word == cts_bool)
10166 error_at (loc,
10167 ("both %<unsigned%> and %<_Bool%> in "
10168 "declaration specifiers"));
10169 else if (specs->typespec_word == cts_float)
10170 error_at (loc,
10171 ("both %<unsigned%> and %<float%> in "
10172 "declaration specifiers"));
10173 else if (specs->typespec_word == cts_double)
10174 error_at (loc,
10175 ("both %<unsigned%> and %<double%> in "
10176 "declaration specifiers"));
10177 else if (specs->typespec_word == cts_floatn_nx)
10178 error_at (loc,
10179 ("both %<unsigned%> and %<_Float%d%s%> in "
10180 "declaration specifiers"),
10181 floatn_nx_types[specs->floatn_nx_idx].n,
10182 (floatn_nx_types[specs->floatn_nx_idx].extended
10183 ? "x"
10184 : ""));
10185 else if (specs->typespec_word == cts_dfloat32)
10186 error_at (loc,
10187 ("both %<unsigned%> and %<_Decimal32%> in "
10188 "declaration specifiers"));
10189 else if (specs->typespec_word == cts_dfloat64)
10190 error_at (loc,
10191 ("both %<unsigned%> and %<_Decimal64%> in "
10192 "declaration specifiers"));
10193 else if (specs->typespec_word == cts_dfloat128)
10194 error_at (loc,
10195 ("both %<unsigned%> and %<_Decimal128%> in "
10196 "declaration specifiers"));
10197 else
10199 specs->unsigned_p = true;
10200 specs->locations[cdw_unsigned] = loc;
10202 break;
10203 case RID_COMPLEX:
10204 dupe = specs->complex_p;
10205 if (!in_system_header_at (loc))
10206 pedwarn_c90 (loc, OPT_Wpedantic,
10207 "ISO C90 does not support complex types");
10208 if (specs->typespec_word == cts_auto_type)
10209 error_at (loc,
10210 ("both %<complex%> and %<__auto_type%> in "
10211 "declaration specifiers"));
10212 else if (specs->typespec_word == cts_void)
10213 error_at (loc,
10214 ("both %<complex%> and %<void%> in "
10215 "declaration specifiers"));
10216 else if (specs->typespec_word == cts_bool)
10217 error_at (loc,
10218 ("both %<complex%> and %<_Bool%> in "
10219 "declaration specifiers"));
10220 else if (specs->typespec_word == cts_dfloat32)
10221 error_at (loc,
10222 ("both %<complex%> and %<_Decimal32%> in "
10223 "declaration specifiers"));
10224 else if (specs->typespec_word == cts_dfloat64)
10225 error_at (loc,
10226 ("both %<complex%> and %<_Decimal64%> in "
10227 "declaration specifiers"));
10228 else if (specs->typespec_word == cts_dfloat128)
10229 error_at (loc,
10230 ("both %<complex%> and %<_Decimal128%> in "
10231 "declaration specifiers"));
10232 else if (specs->typespec_word == cts_fract)
10233 error_at (loc,
10234 ("both %<complex%> and %<_Fract%> in "
10235 "declaration specifiers"));
10236 else if (specs->typespec_word == cts_accum)
10237 error_at (loc,
10238 ("both %<complex%> and %<_Accum%> in "
10239 "declaration specifiers"));
10240 else if (specs->saturating_p)
10241 error_at (loc,
10242 ("both %<complex%> and %<_Sat%> in "
10243 "declaration specifiers"));
10244 else
10246 specs->complex_p = true;
10247 specs->locations[cdw_complex] = loc;
10249 break;
10250 case RID_SAT:
10251 dupe = specs->saturating_p;
10252 pedwarn (loc, OPT_Wpedantic,
10253 "ISO C does not support saturating types");
10254 if (specs->typespec_word == cts_int_n)
10256 error_at (loc,
10257 ("both %<_Sat%> and %<__int%d%> in "
10258 "declaration specifiers"),
10259 int_n_data[specs->int_n_idx].bitsize);
10261 else if (specs->typespec_word == cts_auto_type)
10262 error_at (loc,
10263 ("both %<_Sat%> and %<__auto_type%> in "
10264 "declaration specifiers"));
10265 else if (specs->typespec_word == cts_void)
10266 error_at (loc,
10267 ("both %<_Sat%> and %<void%> in "
10268 "declaration specifiers"));
10269 else if (specs->typespec_word == cts_bool)
10270 error_at (loc,
10271 ("both %<_Sat%> and %<_Bool%> in "
10272 "declaration specifiers"));
10273 else if (specs->typespec_word == cts_char)
10274 error_at (loc,
10275 ("both %<_Sat%> and %<char%> in "
10276 "declaration specifiers"));
10277 else if (specs->typespec_word == cts_int)
10278 error_at (loc,
10279 ("both %<_Sat%> and %<int%> in "
10280 "declaration specifiers"));
10281 else if (specs->typespec_word == cts_float)
10282 error_at (loc,
10283 ("both %<_Sat%> and %<float%> in "
10284 "declaration specifiers"));
10285 else if (specs->typespec_word == cts_double)
10286 error_at (loc,
10287 ("both %<_Sat%> and %<double%> in "
10288 "declaration specifiers"));
10289 else if (specs->typespec_word == cts_floatn_nx)
10290 error_at (loc,
10291 ("both %<_Sat%> and %<_Float%d%s%> in "
10292 "declaration specifiers"),
10293 floatn_nx_types[specs->floatn_nx_idx].n,
10294 (floatn_nx_types[specs->floatn_nx_idx].extended
10295 ? "x"
10296 : ""));
10297 else if (specs->typespec_word == cts_dfloat32)
10298 error_at (loc,
10299 ("both %<_Sat%> and %<_Decimal32%> in "
10300 "declaration specifiers"));
10301 else if (specs->typespec_word == cts_dfloat64)
10302 error_at (loc,
10303 ("both %<_Sat%> and %<_Decimal64%> in "
10304 "declaration specifiers"));
10305 else if (specs->typespec_word == cts_dfloat128)
10306 error_at (loc,
10307 ("both %<_Sat%> and %<_Decimal128%> in "
10308 "declaration specifiers"));
10309 else if (specs->complex_p)
10310 error_at (loc,
10311 ("both %<_Sat%> and %<complex%> in "
10312 "declaration specifiers"));
10313 else
10315 specs->saturating_p = true;
10316 specs->locations[cdw_saturating] = loc;
10318 break;
10319 default:
10320 gcc_unreachable ();
10323 if (dupe)
10324 error_at (loc, "duplicate %qE", type);
10326 return specs;
10328 else
10330 /* "void", "_Bool", "char", "int", "float", "double",
10331 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10332 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10333 "__auto_type". */
10334 if (specs->typespec_word != cts_none)
10336 error_at (loc,
10337 "two or more data types in declaration specifiers");
10338 return specs;
10340 switch (i)
10342 case RID_AUTO_TYPE:
10343 if (specs->long_p)
10344 error_at (loc,
10345 ("both %<long%> and %<__auto_type%> in "
10346 "declaration specifiers"));
10347 else if (specs->short_p)
10348 error_at (loc,
10349 ("both %<short%> and %<__auto_type%> in "
10350 "declaration specifiers"));
10351 else if (specs->signed_p)
10352 error_at (loc,
10353 ("both %<signed%> and %<__auto_type%> in "
10354 "declaration specifiers"));
10355 else if (specs->unsigned_p)
10356 error_at (loc,
10357 ("both %<unsigned%> and %<__auto_type%> in "
10358 "declaration specifiers"));
10359 else if (specs->complex_p)
10360 error_at (loc,
10361 ("both %<complex%> and %<__auto_type%> in "
10362 "declaration specifiers"));
10363 else if (specs->saturating_p)
10364 error_at (loc,
10365 ("both %<_Sat%> and %<__auto_type%> in "
10366 "declaration specifiers"));
10367 else
10369 specs->typespec_word = cts_auto_type;
10370 specs->locations[cdw_typespec] = loc;
10372 return specs;
10373 case RID_INT_N_0:
10374 case RID_INT_N_1:
10375 case RID_INT_N_2:
10376 case RID_INT_N_3:
10377 specs->int_n_idx = i - RID_INT_N_0;
10378 if (!in_system_header_at (input_location))
10379 pedwarn (loc, OPT_Wpedantic,
10380 "ISO C does not support %<__int%d%> types",
10381 int_n_data[specs->int_n_idx].bitsize);
10383 if (specs->long_p)
10384 error_at (loc,
10385 ("both %<__int%d%> and %<long%> in "
10386 "declaration specifiers"),
10387 int_n_data[specs->int_n_idx].bitsize);
10388 else if (specs->saturating_p)
10389 error_at (loc,
10390 ("both %<_Sat%> and %<__int%d%> in "
10391 "declaration specifiers"),
10392 int_n_data[specs->int_n_idx].bitsize);
10393 else if (specs->short_p)
10394 error_at (loc,
10395 ("both %<__int%d%> and %<short%> in "
10396 "declaration specifiers"),
10397 int_n_data[specs->int_n_idx].bitsize);
10398 else if (! int_n_enabled_p[specs->int_n_idx])
10400 specs->typespec_word = cts_int_n;
10401 error_at (loc,
10402 "%<__int%d%> is not supported on this target",
10403 int_n_data[specs->int_n_idx].bitsize);
10405 else
10407 specs->typespec_word = cts_int_n;
10408 specs->locations[cdw_typespec] = loc;
10410 return specs;
10411 case RID_VOID:
10412 if (specs->long_p)
10413 error_at (loc,
10414 ("both %<long%> and %<void%> in "
10415 "declaration specifiers"));
10416 else if (specs->short_p)
10417 error_at (loc,
10418 ("both %<short%> and %<void%> in "
10419 "declaration specifiers"));
10420 else if (specs->signed_p)
10421 error_at (loc,
10422 ("both %<signed%> and %<void%> in "
10423 "declaration specifiers"));
10424 else if (specs->unsigned_p)
10425 error_at (loc,
10426 ("both %<unsigned%> and %<void%> in "
10427 "declaration specifiers"));
10428 else if (specs->complex_p)
10429 error_at (loc,
10430 ("both %<complex%> and %<void%> in "
10431 "declaration specifiers"));
10432 else if (specs->saturating_p)
10433 error_at (loc,
10434 ("both %<_Sat%> and %<void%> in "
10435 "declaration specifiers"));
10436 else
10438 specs->typespec_word = cts_void;
10439 specs->locations[cdw_typespec] = loc;
10441 return specs;
10442 case RID_BOOL:
10443 if (!in_system_header_at (loc))
10444 pedwarn_c90 (loc, OPT_Wpedantic,
10445 "ISO C90 does not support boolean types");
10446 if (specs->long_p)
10447 error_at (loc,
10448 ("both %<long%> and %<_Bool%> in "
10449 "declaration specifiers"));
10450 else if (specs->short_p)
10451 error_at (loc,
10452 ("both %<short%> and %<_Bool%> in "
10453 "declaration specifiers"));
10454 else if (specs->signed_p)
10455 error_at (loc,
10456 ("both %<signed%> and %<_Bool%> in "
10457 "declaration specifiers"));
10458 else if (specs->unsigned_p)
10459 error_at (loc,
10460 ("both %<unsigned%> and %<_Bool%> in "
10461 "declaration specifiers"));
10462 else if (specs->complex_p)
10463 error_at (loc,
10464 ("both %<complex%> and %<_Bool%> in "
10465 "declaration specifiers"));
10466 else if (specs->saturating_p)
10467 error_at (loc,
10468 ("both %<_Sat%> and %<_Bool%> in "
10469 "declaration specifiers"));
10470 else
10472 specs->typespec_word = cts_bool;
10473 specs->locations[cdw_typespec] = loc;
10475 return specs;
10476 case RID_CHAR:
10477 if (specs->long_p)
10478 error_at (loc,
10479 ("both %<long%> and %<char%> in "
10480 "declaration specifiers"));
10481 else if (specs->short_p)
10482 error_at (loc,
10483 ("both %<short%> and %<char%> in "
10484 "declaration specifiers"));
10485 else if (specs->saturating_p)
10486 error_at (loc,
10487 ("both %<_Sat%> and %<char%> in "
10488 "declaration specifiers"));
10489 else
10491 specs->typespec_word = cts_char;
10492 specs->locations[cdw_typespec] = loc;
10494 return specs;
10495 case RID_INT:
10496 if (specs->saturating_p)
10497 error_at (loc,
10498 ("both %<_Sat%> and %<int%> in "
10499 "declaration specifiers"));
10500 else
10502 specs->typespec_word = cts_int;
10503 specs->locations[cdw_typespec] = loc;
10505 return specs;
10506 case RID_FLOAT:
10507 if (specs->long_p)
10508 error_at (loc,
10509 ("both %<long%> and %<float%> in "
10510 "declaration specifiers"));
10511 else if (specs->short_p)
10512 error_at (loc,
10513 ("both %<short%> and %<float%> in "
10514 "declaration specifiers"));
10515 else if (specs->signed_p)
10516 error_at (loc,
10517 ("both %<signed%> and %<float%> in "
10518 "declaration specifiers"));
10519 else if (specs->unsigned_p)
10520 error_at (loc,
10521 ("both %<unsigned%> and %<float%> in "
10522 "declaration specifiers"));
10523 else if (specs->saturating_p)
10524 error_at (loc,
10525 ("both %<_Sat%> and %<float%> in "
10526 "declaration specifiers"));
10527 else
10529 specs->typespec_word = cts_float;
10530 specs->locations[cdw_typespec] = loc;
10532 return specs;
10533 case RID_DOUBLE:
10534 if (specs->long_long_p)
10535 error_at (loc,
10536 ("both %<long long%> and %<double%> in "
10537 "declaration specifiers"));
10538 else if (specs->short_p)
10539 error_at (loc,
10540 ("both %<short%> and %<double%> in "
10541 "declaration specifiers"));
10542 else if (specs->signed_p)
10543 error_at (loc,
10544 ("both %<signed%> and %<double%> in "
10545 "declaration specifiers"));
10546 else if (specs->unsigned_p)
10547 error_at (loc,
10548 ("both %<unsigned%> and %<double%> in "
10549 "declaration specifiers"));
10550 else if (specs->saturating_p)
10551 error_at (loc,
10552 ("both %<_Sat%> and %<double%> in "
10553 "declaration specifiers"));
10554 else
10556 specs->typespec_word = cts_double;
10557 specs->locations[cdw_typespec] = loc;
10559 return specs;
10560 CASE_RID_FLOATN_NX:
10561 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10562 if (!in_system_header_at (input_location))
10563 pedwarn (loc, OPT_Wpedantic,
10564 "ISO C does not support the %<_Float%d%s%> type",
10565 floatn_nx_types[specs->floatn_nx_idx].n,
10566 (floatn_nx_types[specs->floatn_nx_idx].extended
10567 ? "x"
10568 : ""));
10570 if (specs->long_p)
10571 error_at (loc,
10572 ("both %<long%> and %<_Float%d%s%> in "
10573 "declaration specifiers"),
10574 floatn_nx_types[specs->floatn_nx_idx].n,
10575 (floatn_nx_types[specs->floatn_nx_idx].extended
10576 ? "x"
10577 : ""));
10578 else if (specs->short_p)
10579 error_at (loc,
10580 ("both %<short%> and %<_Float%d%s%> in "
10581 "declaration specifiers"),
10582 floatn_nx_types[specs->floatn_nx_idx].n,
10583 (floatn_nx_types[specs->floatn_nx_idx].extended
10584 ? "x"
10585 : ""));
10586 else if (specs->signed_p)
10587 error_at (loc,
10588 ("both %<signed%> and %<_Float%d%s%> in "
10589 "declaration specifiers"),
10590 floatn_nx_types[specs->floatn_nx_idx].n,
10591 (floatn_nx_types[specs->floatn_nx_idx].extended
10592 ? "x"
10593 : ""));
10594 else if (specs->unsigned_p)
10595 error_at (loc,
10596 ("both %<unsigned%> and %<_Float%d%s%> in "
10597 "declaration specifiers"),
10598 floatn_nx_types[specs->floatn_nx_idx].n,
10599 (floatn_nx_types[specs->floatn_nx_idx].extended
10600 ? "x"
10601 : ""));
10602 else if (specs->saturating_p)
10603 error_at (loc,
10604 ("both %<_Sat%> and %<_Float%d%s%> in "
10605 "declaration specifiers"),
10606 floatn_nx_types[specs->floatn_nx_idx].n,
10607 (floatn_nx_types[specs->floatn_nx_idx].extended
10608 ? "x"
10609 : ""));
10610 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10612 specs->typespec_word = cts_floatn_nx;
10613 error_at (loc,
10614 "%<_Float%d%s%> is not supported on this target",
10615 floatn_nx_types[specs->floatn_nx_idx].n,
10616 (floatn_nx_types[specs->floatn_nx_idx].extended
10617 ? "x"
10618 : ""));
10620 else
10622 specs->typespec_word = cts_floatn_nx;
10623 specs->locations[cdw_typespec] = loc;
10625 return specs;
10626 case RID_DFLOAT32:
10627 case RID_DFLOAT64:
10628 case RID_DFLOAT128:
10630 const char *str;
10631 if (i == RID_DFLOAT32)
10632 str = "_Decimal32";
10633 else if (i == RID_DFLOAT64)
10634 str = "_Decimal64";
10635 else
10636 str = "_Decimal128";
10637 if (specs->long_long_p)
10638 error_at (loc,
10639 ("both %<long long%> and %qs in "
10640 "declaration specifiers"),
10641 str);
10642 if (specs->long_p)
10643 error_at (loc,
10644 ("both %<long%> and %qs in "
10645 "declaration specifiers"),
10646 str);
10647 else if (specs->short_p)
10648 error_at (loc,
10649 ("both %<short%> and %qs in "
10650 "declaration specifiers"),
10651 str);
10652 else if (specs->signed_p)
10653 error_at (loc,
10654 ("both %<signed%> and %qs in "
10655 "declaration specifiers"),
10656 str);
10657 else if (specs->unsigned_p)
10658 error_at (loc,
10659 ("both %<unsigned%> and %qs in "
10660 "declaration specifiers"),
10661 str);
10662 else if (specs->complex_p)
10663 error_at (loc,
10664 ("both %<complex%> and %qs in "
10665 "declaration specifiers"),
10666 str);
10667 else if (specs->saturating_p)
10668 error_at (loc,
10669 ("both %<_Sat%> and %qs in "
10670 "declaration specifiers"),
10671 str);
10672 else if (i == RID_DFLOAT32)
10673 specs->typespec_word = cts_dfloat32;
10674 else if (i == RID_DFLOAT64)
10675 specs->typespec_word = cts_dfloat64;
10676 else
10677 specs->typespec_word = cts_dfloat128;
10678 specs->locations[cdw_typespec] = loc;
10680 if (!targetm.decimal_float_supported_p ())
10681 error_at (loc,
10682 ("decimal floating point not supported "
10683 "for this target"));
10684 pedwarn (loc, OPT_Wpedantic,
10685 "ISO C does not support decimal floating point");
10686 return specs;
10687 case RID_FRACT:
10688 case RID_ACCUM:
10690 const char *str;
10691 if (i == RID_FRACT)
10692 str = "_Fract";
10693 else
10694 str = "_Accum";
10695 if (specs->complex_p)
10696 error_at (loc,
10697 ("both %<complex%> and %qs in "
10698 "declaration specifiers"),
10699 str);
10700 else if (i == RID_FRACT)
10701 specs->typespec_word = cts_fract;
10702 else
10703 specs->typespec_word = cts_accum;
10704 specs->locations[cdw_typespec] = loc;
10706 if (!targetm.fixed_point_supported_p ())
10707 error_at (loc,
10708 "fixed-point types not supported for this target");
10709 pedwarn (loc, OPT_Wpedantic,
10710 "ISO C does not support fixed-point types");
10711 return specs;
10712 default:
10713 /* ObjC reserved word "id", handled below. */
10714 break;
10719 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10720 form of ObjC type, cases such as "int" and "long" being handled
10721 above), a TYPE (struct, union, enum and typeof specifiers) or an
10722 ERROR_MARK. In none of these cases may there have previously
10723 been any type specifiers. */
10724 if (specs->type || specs->typespec_word != cts_none
10725 || specs->long_p || specs->short_p || specs->signed_p
10726 || specs->unsigned_p || specs->complex_p)
10727 error_at (loc, "two or more data types in declaration specifiers");
10728 else if (TREE_CODE (type) == TYPE_DECL)
10730 if (TREE_TYPE (type) == error_mark_node)
10731 ; /* Allow the type to default to int to avoid cascading errors. */
10732 else
10734 specs->type = TREE_TYPE (type);
10735 specs->decl_attr = DECL_ATTRIBUTES (type);
10736 specs->typedef_p = true;
10737 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10738 specs->locations[cdw_typedef] = loc;
10740 /* If this typedef name is defined in a struct, then a C++
10741 lookup would return a different value. */
10742 if (warn_cxx_compat
10743 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10744 warning_at (loc, OPT_Wc___compat,
10745 "C++ lookup of %qD would return a field, not a type",
10746 type);
10748 /* If we are parsing a struct, record that a struct field
10749 used a typedef. */
10750 if (warn_cxx_compat && struct_parse_info != NULL)
10751 struct_parse_info->typedefs_seen.safe_push (type);
10754 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10756 tree t = lookup_name (type);
10757 if (!t || TREE_CODE (t) != TYPE_DECL)
10758 error_at (loc, "%qE fails to be a typedef or built in type", type);
10759 else if (TREE_TYPE (t) == error_mark_node)
10761 else
10763 specs->type = TREE_TYPE (t);
10764 specs->locations[cdw_typespec] = loc;
10767 else
10769 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10771 specs->typedef_p = true;
10772 specs->locations[cdw_typedef] = loc;
10773 if (spec.expr)
10775 if (specs->expr)
10776 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10777 specs->expr, spec.expr);
10778 else
10779 specs->expr = spec.expr;
10780 specs->expr_const_operands &= spec.expr_const_operands;
10783 specs->type = type;
10786 return specs;
10789 /* Add the storage class specifier or function specifier SCSPEC to the
10790 declaration specifiers SPECS, returning SPECS. */
10792 struct c_declspecs *
10793 declspecs_add_scspec (source_location loc,
10794 struct c_declspecs *specs,
10795 tree scspec)
10797 enum rid i;
10798 enum c_storage_class n = csc_none;
10799 bool dupe = false;
10800 specs->declspecs_seen_p = true;
10801 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10802 && C_IS_RESERVED_WORD (scspec));
10803 i = C_RID_CODE (scspec);
10804 if (specs->non_sc_seen_p)
10805 warning (OPT_Wold_style_declaration,
10806 "%qE is not at beginning of declaration", scspec);
10807 switch (i)
10809 case RID_INLINE:
10810 /* C99 permits duplicate inline. Although of doubtful utility,
10811 it seems simplest to permit it in gnu89 mode as well, as
10812 there is also little utility in maintaining this as a
10813 difference between gnu89 and C99 inline. */
10814 dupe = false;
10815 specs->inline_p = true;
10816 specs->locations[cdw_inline] = loc;
10817 break;
10818 case RID_NORETURN:
10819 /* Duplicate _Noreturn is permitted. */
10820 dupe = false;
10821 specs->noreturn_p = true;
10822 specs->locations[cdw_noreturn] = loc;
10823 break;
10824 case RID_THREAD:
10825 dupe = specs->thread_p;
10826 if (specs->storage_class == csc_auto)
10827 error ("%qE used with %<auto%>", scspec);
10828 else if (specs->storage_class == csc_register)
10829 error ("%qE used with %<register%>", scspec);
10830 else if (specs->storage_class == csc_typedef)
10831 error ("%qE used with %<typedef%>", scspec);
10832 else
10834 specs->thread_p = true;
10835 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10836 "__thread") == 0);
10837 /* A diagnostic is not required for the use of this
10838 identifier in the implementation namespace; only diagnose
10839 it for the C11 spelling because of existing code using
10840 the other spelling. */
10841 if (!specs->thread_gnu_p)
10843 if (flag_isoc99)
10844 pedwarn_c99 (loc, OPT_Wpedantic,
10845 "ISO C99 does not support %qE", scspec);
10846 else
10847 pedwarn_c99 (loc, OPT_Wpedantic,
10848 "ISO C90 does not support %qE", scspec);
10850 specs->locations[cdw_thread] = loc;
10852 break;
10853 case RID_AUTO:
10854 n = csc_auto;
10855 break;
10856 case RID_EXTERN:
10857 n = csc_extern;
10858 /* Diagnose "__thread extern". */
10859 if (specs->thread_p && specs->thread_gnu_p)
10860 error ("%<__thread%> before %<extern%>");
10861 break;
10862 case RID_REGISTER:
10863 n = csc_register;
10864 break;
10865 case RID_STATIC:
10866 n = csc_static;
10867 /* Diagnose "__thread static". */
10868 if (specs->thread_p && specs->thread_gnu_p)
10869 error ("%<__thread%> before %<static%>");
10870 break;
10871 case RID_TYPEDEF:
10872 n = csc_typedef;
10873 break;
10874 default:
10875 gcc_unreachable ();
10877 if (n != csc_none && n == specs->storage_class)
10878 dupe = true;
10879 if (dupe)
10881 if (i == RID_THREAD)
10882 error ("duplicate %<_Thread_local%> or %<__thread%>");
10883 else
10884 error ("duplicate %qE", scspec);
10886 if (n != csc_none)
10888 if (specs->storage_class != csc_none && n != specs->storage_class)
10890 error ("multiple storage classes in declaration specifiers");
10892 else
10894 specs->storage_class = n;
10895 specs->locations[cdw_storage_class] = loc;
10896 if (n != csc_extern && n != csc_static && specs->thread_p)
10898 error ("%qs used with %qE",
10899 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10900 scspec);
10901 specs->thread_p = false;
10905 return specs;
10908 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10909 returning SPECS. */
10911 struct c_declspecs *
10912 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10914 specs->attrs = chainon (attrs, specs->attrs);
10915 specs->locations[cdw_attributes] = loc;
10916 specs->declspecs_seen_p = true;
10917 return specs;
10920 /* Add an _Alignas specifier (expression ALIGN, or type whose
10921 alignment is ALIGN) to the declaration specifiers SPECS, returning
10922 SPECS. */
10923 struct c_declspecs *
10924 declspecs_add_alignas (source_location loc,
10925 struct c_declspecs *specs, tree align)
10927 int align_log;
10928 specs->alignas_p = true;
10929 specs->locations[cdw_alignas] = loc;
10930 if (align == error_mark_node)
10931 return specs;
10932 align_log = check_user_alignment (align, true);
10933 if (align_log > specs->align_log)
10934 specs->align_log = align_log;
10935 return specs;
10938 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10939 specifiers with any other type specifier to determine the resulting
10940 type. This is where ISO C checks on complex types are made, since
10941 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10942 double". */
10944 struct c_declspecs *
10945 finish_declspecs (struct c_declspecs *specs)
10947 /* If a type was specified as a whole, we have no modifiers and are
10948 done. */
10949 if (specs->type != NULL_TREE)
10951 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10952 && !specs->signed_p && !specs->unsigned_p
10953 && !specs->complex_p);
10955 /* Set a dummy type. */
10956 if (TREE_CODE (specs->type) == ERROR_MARK)
10957 specs->type = integer_type_node;
10958 return specs;
10961 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10962 has been specified, treat it as "int" unless "_Complex" is
10963 present and there are no other specifiers. If we just have
10964 "_Complex", it is equivalent to "_Complex double", but e.g.
10965 "_Complex short" is equivalent to "_Complex short int". */
10966 if (specs->typespec_word == cts_none)
10968 if (specs->saturating_p)
10970 error_at (specs->locations[cdw_saturating],
10971 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10972 if (!targetm.fixed_point_supported_p ())
10973 error_at (specs->locations[cdw_saturating],
10974 "fixed-point types not supported for this target");
10975 specs->typespec_word = cts_fract;
10977 else if (specs->long_p || specs->short_p
10978 || specs->signed_p || specs->unsigned_p)
10980 specs->typespec_word = cts_int;
10982 else if (specs->complex_p)
10984 specs->typespec_word = cts_double;
10985 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10986 "ISO C does not support plain %<complex%> meaning "
10987 "%<double complex%>");
10989 else
10991 specs->typespec_word = cts_int;
10992 specs->default_int_p = true;
10993 /* We don't diagnose this here because grokdeclarator will
10994 give more specific diagnostics according to whether it is
10995 a function definition. */
10999 /* If "signed" was specified, record this to distinguish "int" and
11000 "signed int" in the case of a bit-field with
11001 -funsigned-bitfields. */
11002 specs->explicit_signed_p = specs->signed_p;
11004 /* Now compute the actual type. */
11005 switch (specs->typespec_word)
11007 case cts_auto_type:
11008 gcc_assert (!specs->long_p && !specs->short_p
11009 && !specs->signed_p && !specs->unsigned_p
11010 && !specs->complex_p);
11011 /* Type to be filled in later. */
11012 break;
11013 case cts_void:
11014 gcc_assert (!specs->long_p && !specs->short_p
11015 && !specs->signed_p && !specs->unsigned_p
11016 && !specs->complex_p);
11017 specs->type = void_type_node;
11018 break;
11019 case cts_bool:
11020 gcc_assert (!specs->long_p && !specs->short_p
11021 && !specs->signed_p && !specs->unsigned_p
11022 && !specs->complex_p);
11023 specs->type = boolean_type_node;
11024 break;
11025 case cts_char:
11026 gcc_assert (!specs->long_p && !specs->short_p);
11027 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11028 if (specs->signed_p)
11029 specs->type = signed_char_type_node;
11030 else if (specs->unsigned_p)
11031 specs->type = unsigned_char_type_node;
11032 else
11033 specs->type = char_type_node;
11034 if (specs->complex_p)
11036 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11037 "ISO C does not support complex integer types");
11038 specs->type = build_complex_type (specs->type);
11040 break;
11041 case cts_int_n:
11042 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11043 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11044 if (! int_n_enabled_p[specs->int_n_idx])
11045 specs->type = integer_type_node;
11046 else
11047 specs->type = (specs->unsigned_p
11048 ? int_n_trees[specs->int_n_idx].unsigned_type
11049 : int_n_trees[specs->int_n_idx].signed_type);
11050 if (specs->complex_p)
11052 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11053 "ISO C does not support complex integer types");
11054 specs->type = build_complex_type (specs->type);
11056 break;
11057 case cts_int:
11058 gcc_assert (!(specs->long_p && specs->short_p));
11059 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11060 if (specs->long_long_p)
11061 specs->type = (specs->unsigned_p
11062 ? long_long_unsigned_type_node
11063 : long_long_integer_type_node);
11064 else if (specs->long_p)
11065 specs->type = (specs->unsigned_p
11066 ? long_unsigned_type_node
11067 : long_integer_type_node);
11068 else if (specs->short_p)
11069 specs->type = (specs->unsigned_p
11070 ? short_unsigned_type_node
11071 : short_integer_type_node);
11072 else
11073 specs->type = (specs->unsigned_p
11074 ? unsigned_type_node
11075 : integer_type_node);
11076 if (specs->complex_p)
11078 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11079 "ISO C does not support complex integer types");
11080 specs->type = build_complex_type (specs->type);
11082 break;
11083 case cts_float:
11084 gcc_assert (!specs->long_p && !specs->short_p
11085 && !specs->signed_p && !specs->unsigned_p);
11086 specs->type = (specs->complex_p
11087 ? complex_float_type_node
11088 : float_type_node);
11089 break;
11090 case cts_double:
11091 gcc_assert (!specs->long_long_p && !specs->short_p
11092 && !specs->signed_p && !specs->unsigned_p);
11093 if (specs->long_p)
11095 specs->type = (specs->complex_p
11096 ? complex_long_double_type_node
11097 : long_double_type_node);
11099 else
11101 specs->type = (specs->complex_p
11102 ? complex_double_type_node
11103 : double_type_node);
11105 break;
11106 case cts_floatn_nx:
11107 gcc_assert (!specs->long_p && !specs->short_p
11108 && !specs->signed_p && !specs->unsigned_p);
11109 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11110 specs->type = integer_type_node;
11111 else if (specs->complex_p)
11112 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11113 else
11114 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11115 break;
11116 case cts_dfloat32:
11117 case cts_dfloat64:
11118 case cts_dfloat128:
11119 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11120 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11121 if (specs->typespec_word == cts_dfloat32)
11122 specs->type = dfloat32_type_node;
11123 else if (specs->typespec_word == cts_dfloat64)
11124 specs->type = dfloat64_type_node;
11125 else
11126 specs->type = dfloat128_type_node;
11127 break;
11128 case cts_fract:
11129 gcc_assert (!specs->complex_p);
11130 if (!targetm.fixed_point_supported_p ())
11131 specs->type = integer_type_node;
11132 else if (specs->saturating_p)
11134 if (specs->long_long_p)
11135 specs->type = specs->unsigned_p
11136 ? sat_unsigned_long_long_fract_type_node
11137 : sat_long_long_fract_type_node;
11138 else if (specs->long_p)
11139 specs->type = specs->unsigned_p
11140 ? sat_unsigned_long_fract_type_node
11141 : sat_long_fract_type_node;
11142 else if (specs->short_p)
11143 specs->type = specs->unsigned_p
11144 ? sat_unsigned_short_fract_type_node
11145 : sat_short_fract_type_node;
11146 else
11147 specs->type = specs->unsigned_p
11148 ? sat_unsigned_fract_type_node
11149 : sat_fract_type_node;
11151 else
11153 if (specs->long_long_p)
11154 specs->type = specs->unsigned_p
11155 ? unsigned_long_long_fract_type_node
11156 : long_long_fract_type_node;
11157 else if (specs->long_p)
11158 specs->type = specs->unsigned_p
11159 ? unsigned_long_fract_type_node
11160 : long_fract_type_node;
11161 else if (specs->short_p)
11162 specs->type = specs->unsigned_p
11163 ? unsigned_short_fract_type_node
11164 : short_fract_type_node;
11165 else
11166 specs->type = specs->unsigned_p
11167 ? unsigned_fract_type_node
11168 : fract_type_node;
11170 break;
11171 case cts_accum:
11172 gcc_assert (!specs->complex_p);
11173 if (!targetm.fixed_point_supported_p ())
11174 specs->type = integer_type_node;
11175 else if (specs->saturating_p)
11177 if (specs->long_long_p)
11178 specs->type = specs->unsigned_p
11179 ? sat_unsigned_long_long_accum_type_node
11180 : sat_long_long_accum_type_node;
11181 else if (specs->long_p)
11182 specs->type = specs->unsigned_p
11183 ? sat_unsigned_long_accum_type_node
11184 : sat_long_accum_type_node;
11185 else if (specs->short_p)
11186 specs->type = specs->unsigned_p
11187 ? sat_unsigned_short_accum_type_node
11188 : sat_short_accum_type_node;
11189 else
11190 specs->type = specs->unsigned_p
11191 ? sat_unsigned_accum_type_node
11192 : sat_accum_type_node;
11194 else
11196 if (specs->long_long_p)
11197 specs->type = specs->unsigned_p
11198 ? unsigned_long_long_accum_type_node
11199 : long_long_accum_type_node;
11200 else if (specs->long_p)
11201 specs->type = specs->unsigned_p
11202 ? unsigned_long_accum_type_node
11203 : long_accum_type_node;
11204 else if (specs->short_p)
11205 specs->type = specs->unsigned_p
11206 ? unsigned_short_accum_type_node
11207 : short_accum_type_node;
11208 else
11209 specs->type = specs->unsigned_p
11210 ? unsigned_accum_type_node
11211 : accum_type_node;
11213 break;
11214 default:
11215 gcc_unreachable ();
11218 return specs;
11221 /* Perform final processing on one file scope's declarations (or the
11222 external scope's declarations), GLOBALS. */
11224 static void
11225 c_write_global_declarations_1 (tree globals)
11227 tree decl;
11228 bool reconsider;
11230 /* Process the decls in the order they were written. */
11231 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11233 /* Check for used but undefined static functions using the C
11234 standard's definition of "used", and set TREE_NO_WARNING so
11235 that check_global_declaration doesn't repeat the check. */
11236 if (TREE_CODE (decl) == FUNCTION_DECL
11237 && DECL_INITIAL (decl) == NULL_TREE
11238 && DECL_EXTERNAL (decl)
11239 && !TREE_PUBLIC (decl))
11241 if (C_DECL_USED (decl))
11243 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11244 TREE_NO_WARNING (decl) = 1;
11246 /* For -Wunused-function warn about unused static prototypes. */
11247 else if (warn_unused_function
11248 && ! DECL_ARTIFICIAL (decl)
11249 && ! TREE_NO_WARNING (decl))
11251 warning (OPT_Wunused_function,
11252 "%q+F declared %<static%> but never defined", decl);
11253 TREE_NO_WARNING (decl) = 1;
11257 wrapup_global_declaration_1 (decl);
11262 reconsider = false;
11263 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11264 reconsider |= wrapup_global_declaration_2 (decl);
11266 while (reconsider);
11269 /* Callback to collect a source_ref from a DECL. */
11271 static void
11272 collect_source_ref_cb (tree decl)
11274 if (!DECL_IS_BUILTIN (decl))
11275 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11278 /* Preserve the external declarations scope across a garbage collect. */
11279 static GTY(()) tree ext_block;
11281 /* Collect all references relevant to SOURCE_FILE. */
11283 static void
11284 collect_all_refs (const char *source_file)
11286 tree t;
11287 unsigned i;
11289 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11290 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11292 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11295 /* Iterate over all global declarations and call CALLBACK. */
11297 static void
11298 for_each_global_decl (void (*callback) (tree decl))
11300 tree t;
11301 tree decls;
11302 tree decl;
11303 unsigned i;
11305 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11307 decls = DECL_INITIAL (t);
11308 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11309 callback (decl);
11312 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11313 callback (decl);
11316 /* Perform any final parser cleanups and generate initial debugging
11317 information. */
11319 void
11320 c_parse_final_cleanups (void)
11322 tree t;
11323 unsigned i;
11325 /* We don't want to do this if generating a PCH. */
11326 if (pch_file)
11327 return;
11329 timevar_stop (TV_PHASE_PARSING);
11330 timevar_start (TV_PHASE_DEFERRED);
11332 /* Do the Objective-C stuff. This is where all the Objective-C
11333 module stuff gets generated (symtab, class/protocol/selector
11334 lists etc). */
11335 if (c_dialect_objc ())
11336 objc_write_global_declarations ();
11338 /* Close the external scope. */
11339 ext_block = pop_scope ();
11340 external_scope = 0;
11341 gcc_assert (!current_scope);
11343 /* Handle -fdump-ada-spec[-slim]. */
11344 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11346 /* Build a table of files to generate specs for */
11347 if (flag_dump_ada_spec_slim)
11348 collect_source_ref (main_input_filename);
11349 else
11350 for_each_global_decl (collect_source_ref_cb);
11352 dump_ada_specs (collect_all_refs, NULL);
11355 /* Process all file scopes in this compilation, and the external_scope,
11356 through wrapup_global_declarations. */
11357 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11358 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11359 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11361 timevar_stop (TV_PHASE_DEFERRED);
11362 timevar_start (TV_PHASE_PARSING);
11364 ext_block = NULL;
11367 /* Register reserved keyword WORD as qualifier for address space AS. */
11369 void
11370 c_register_addr_space (const char *word, addr_space_t as)
11372 int rid = RID_FIRST_ADDR_SPACE + as;
11373 tree id;
11375 /* Address space qualifiers are only supported
11376 in C with GNU extensions enabled. */
11377 if (c_dialect_objc () || flag_no_asm)
11378 return;
11380 id = get_identifier (word);
11381 C_SET_RID_CODE (id, rid);
11382 C_IS_RESERVED_WORD (id) = 1;
11383 ridpointers [rid] = id;
11386 /* Return identifier to look up for omp declare reduction. */
11388 tree
11389 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11391 const char *p = NULL;
11392 switch (reduction_code)
11394 case PLUS_EXPR: p = "+"; break;
11395 case MULT_EXPR: p = "*"; break;
11396 case MINUS_EXPR: p = "-"; break;
11397 case BIT_AND_EXPR: p = "&"; break;
11398 case BIT_XOR_EXPR: p = "^"; break;
11399 case BIT_IOR_EXPR: p = "|"; break;
11400 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11401 case TRUTH_ORIF_EXPR: p = "||"; break;
11402 case MIN_EXPR: p = "min"; break;
11403 case MAX_EXPR: p = "max"; break;
11404 default:
11405 break;
11408 if (p == NULL)
11410 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11411 return error_mark_node;
11412 p = IDENTIFIER_POINTER (reduction_id);
11415 const char prefix[] = "omp declare reduction ";
11416 size_t lenp = sizeof (prefix);
11417 size_t len = strlen (p);
11418 char *name = XALLOCAVEC (char, lenp + len);
11419 memcpy (name, prefix, lenp - 1);
11420 memcpy (name + lenp - 1, p, len + 1);
11421 return get_identifier (name);
11424 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11425 VAR_DECL, bind it into the current scope and return it. */
11427 tree
11428 c_omp_reduction_decl (tree reduction_id)
11430 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11431 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11432 return b->decl;
11434 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11435 reduction_id, integer_type_node);
11436 DECL_ARTIFICIAL (decl) = 1;
11437 DECL_EXTERNAL (decl) = 1;
11438 TREE_STATIC (decl) = 1;
11439 TREE_PUBLIC (decl) = 0;
11440 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11441 return decl;
11444 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11446 tree
11447 c_omp_reduction_lookup (tree reduction_id, tree type)
11449 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11450 while (b)
11452 tree t;
11453 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11454 if (comptypes (TREE_PURPOSE (t), type))
11455 return TREE_VALUE (t);
11456 b = b->shadowed;
11458 return error_mark_node;
11461 /* Helper function called via walk_tree, to diagnose invalid
11462 #pragma omp declare reduction combiners or initializers. */
11464 tree
11465 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11467 tree *vars = (tree *) data;
11468 if (SSA_VAR_P (*tp)
11469 && !DECL_ARTIFICIAL (*tp)
11470 && *tp != vars[0]
11471 && *tp != vars[1])
11473 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11474 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11475 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11476 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11477 *tp);
11478 else
11479 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11480 "to variable %qD which is not %<omp_priv%> nor "
11481 "%<omp_orig%>",
11482 *tp);
11483 return *tp;
11485 return NULL_TREE;
11488 #include "gt-c-c-decl.h"