* doc/generic.texi (ANNOTATE_EXPR): Document 3rd operand.
[official-gcc.git] / gcc / c / c-decl.c
blobc66bc4043ce9687d256a609acd7b4399dee67b5a
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 "cilk.h"
54 #include "builtins.h"
55 #include "spellcheck-tree.h"
56 #include "gcc-rich-location.h"
57 #include "asan.h"
58 #include "c-family/name-hint.h"
59 #include "c-family/known-headers.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 best_match<tree, tree> bm (name);
4032 /* Look within currently valid scopes. */
4033 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4034 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4036 if (!binding->id || binding->invisible)
4037 continue;
4038 if (binding->decl == error_mark_node)
4039 continue;
4040 /* Don't use bindings from implicitly declared functions,
4041 as they were likely misspellings themselves. */
4042 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4043 if (C_DECL_IMPLICIT (binding->decl))
4044 continue;
4045 switch (kind)
4047 case FUZZY_LOOKUP_TYPENAME:
4048 if (TREE_CODE (binding->decl) != TYPE_DECL)
4049 continue;
4050 break;
4052 case FUZZY_LOOKUP_FUNCTION_NAME:
4053 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4055 /* Allow function pointers. */
4056 if ((VAR_P (binding->decl)
4057 || TREE_CODE (binding->decl) == PARM_DECL)
4058 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4059 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4060 == FUNCTION_TYPE))
4061 break;
4062 continue;
4064 break;
4066 default:
4067 break;
4069 bm.consider (binding->id);
4072 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4074 x = SOME_OTHER_MACRO (y);
4075 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4076 as a misspelled identifier.
4078 Use the best distance so far so that a candidate is only set if
4079 a macro is better than anything so far. This allows early rejection
4080 (without calculating the edit distance) of macro names that must have
4081 distance >= bm.get_best_distance (), and means that we only get a
4082 non-NULL result for best_macro_match if it's better than any of
4083 the identifiers already checked, which avoids needless creation
4084 of identifiers for macro hashnodes. */
4085 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4086 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4087 /* If a macro is the closest so far to NAME, use it, creating an
4088 identifier tree node for it. */
4089 if (best_macro)
4091 const char *id = (const char *)best_macro->ident.str;
4092 tree macro_as_identifier
4093 = get_identifier_with_length (id, best_macro->ident.len);
4094 bm.set_best_so_far (macro_as_identifier,
4095 bmm.get_best_distance (),
4096 bmm.get_best_candidate_length ());
4099 /* Try the "start_typename" keywords to detect
4100 "singed" vs "signed" typos. */
4101 if (kind == FUZZY_LOOKUP_TYPENAME)
4103 for (unsigned i = 0; i < num_c_common_reswords; i++)
4105 const c_common_resword *resword = &c_common_reswords[i];
4106 if (!c_keyword_starts_typename (resword->rid))
4107 continue;
4108 tree resword_identifier = ridpointers [resword->rid];
4109 if (!resword_identifier)
4110 continue;
4111 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4112 bm.consider (resword_identifier);
4116 tree best = bm.get_best_meaningful_candidate ();
4117 if (best)
4118 return name_hint (IDENTIFIER_POINTER (best), NULL);
4119 else
4120 return name_hint (NULL, NULL);
4124 /* Create the predefined scalar types of C,
4125 and some nodes representing standard constants (0, 1, (void *) 0).
4126 Initialize the global scope.
4127 Make definitions for built-in primitive functions. */
4129 void
4130 c_init_decl_processing (void)
4132 location_t save_loc = input_location;
4134 /* Initialize reserved words for parser. */
4135 c_parse_init ();
4137 current_function_decl = NULL_TREE;
4139 gcc_obstack_init (&parser_obstack);
4141 /* Make the externals scope. */
4142 push_scope ();
4143 external_scope = current_scope;
4145 /* Declarations from c_common_nodes_and_builtins must not be associated
4146 with this input file, lest we get differences between using and not
4147 using preprocessed headers. */
4148 input_location = BUILTINS_LOCATION;
4150 c_common_nodes_and_builtins ();
4152 /* In C, comparisons and TRUTH_* expressions have type int. */
4153 truthvalue_type_node = integer_type_node;
4154 truthvalue_true_node = integer_one_node;
4155 truthvalue_false_node = integer_zero_node;
4157 /* Even in C99, which has a real boolean type. */
4158 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4159 boolean_type_node));
4161 input_location = save_loc;
4163 make_fname_decl = c_make_fname_decl;
4164 start_fname_decls ();
4167 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4168 give the decl, NAME is the initialization string and TYPE_DEP
4169 indicates whether NAME depended on the type of the function. As we
4170 don't yet implement delayed emission of static data, we mark the
4171 decl as emitted so it is not placed in the output. Anything using
4172 it must therefore pull out the STRING_CST initializer directly.
4173 FIXME. */
4175 static tree
4176 c_make_fname_decl (location_t loc, tree id, int type_dep)
4178 const char *name = fname_as_string (type_dep);
4179 tree decl, type, init;
4180 size_t length = strlen (name);
4182 type = build_array_type (char_type_node,
4183 build_index_type (size_int (length)));
4184 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4186 decl = build_decl (loc, VAR_DECL, id, type);
4188 TREE_STATIC (decl) = 1;
4189 TREE_READONLY (decl) = 1;
4190 DECL_ARTIFICIAL (decl) = 1;
4192 init = build_string (length + 1, name);
4193 free (CONST_CAST (char *, name));
4194 TREE_TYPE (init) = type;
4195 DECL_INITIAL (decl) = init;
4197 TREE_USED (decl) = 1;
4199 if (current_function_decl
4200 /* For invalid programs like this:
4202 void foo()
4203 const char* p = __FUNCTION__;
4205 the __FUNCTION__ is believed to appear in K&R style function
4206 parameter declarator. In that case we still don't have
4207 function_scope. */
4208 && current_function_scope)
4210 DECL_CONTEXT (decl) = current_function_decl;
4211 bind (id, decl, current_function_scope,
4212 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4215 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4217 return decl;
4220 tree
4221 c_builtin_function (tree decl)
4223 tree type = TREE_TYPE (decl);
4224 tree id = DECL_NAME (decl);
4226 const char *name = IDENTIFIER_POINTER (id);
4227 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4229 /* Should never be called on a symbol with a preexisting meaning. */
4230 gcc_assert (!I_SYMBOL_BINDING (id));
4232 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4233 UNKNOWN_LOCATION);
4235 /* Builtins in the implementation namespace are made visible without
4236 needing to be explicitly declared. See push_file_scope. */
4237 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4239 DECL_CHAIN (decl) = visible_builtins;
4240 visible_builtins = decl;
4243 return decl;
4246 tree
4247 c_builtin_function_ext_scope (tree decl)
4249 tree type = TREE_TYPE (decl);
4250 tree id = DECL_NAME (decl);
4252 const char *name = IDENTIFIER_POINTER (id);
4253 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4255 if (external_scope)
4256 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4257 UNKNOWN_LOCATION);
4259 /* Builtins in the implementation namespace are made visible without
4260 needing to be explicitly declared. See push_file_scope. */
4261 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4263 DECL_CHAIN (decl) = visible_builtins;
4264 visible_builtins = decl;
4267 return decl;
4270 /* Called when a declaration is seen that contains no names to declare.
4271 If its type is a reference to a structure, union or enum inherited
4272 from a containing scope, shadow that tag name for the current scope
4273 with a forward reference.
4274 If its type defines a new named structure or union
4275 or defines an enum, it is valid but we need not do anything here.
4276 Otherwise, it is an error. */
4278 void
4279 shadow_tag (const struct c_declspecs *declspecs)
4281 shadow_tag_warned (declspecs, 0);
4284 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4285 but no pedwarn. */
4286 void
4287 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4289 bool found_tag = false;
4291 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4293 tree value = declspecs->type;
4294 enum tree_code code = TREE_CODE (value);
4296 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4297 /* Used to test also that TYPE_SIZE (value) != 0.
4298 That caused warning for `struct foo;' at top level in the file. */
4300 tree name = TYPE_NAME (value);
4301 tree t;
4303 found_tag = true;
4305 if (declspecs->restrict_p)
4307 error ("invalid use of %<restrict%>");
4308 warned = 1;
4311 if (name == NULL_TREE)
4313 if (warned != 1 && code != ENUMERAL_TYPE)
4314 /* Empty unnamed enum OK */
4316 pedwarn (input_location, 0,
4317 "unnamed struct/union that defines no instances");
4318 warned = 1;
4321 else if (declspecs->typespec_kind != ctsk_tagdef
4322 && declspecs->typespec_kind != ctsk_tagfirstref
4323 && declspecs->storage_class != csc_none)
4325 if (warned != 1)
4326 pedwarn (input_location, 0,
4327 "empty declaration with storage class specifier "
4328 "does not redeclare tag");
4329 warned = 1;
4330 pending_xref_error ();
4332 else if (declspecs->typespec_kind != ctsk_tagdef
4333 && declspecs->typespec_kind != ctsk_tagfirstref
4334 && (declspecs->const_p
4335 || declspecs->volatile_p
4336 || declspecs->atomic_p
4337 || declspecs->restrict_p
4338 || declspecs->address_space))
4340 if (warned != 1)
4341 pedwarn (input_location, 0,
4342 "empty declaration with type qualifier "
4343 "does not redeclare tag");
4344 warned = 1;
4345 pending_xref_error ();
4347 else if (declspecs->typespec_kind != ctsk_tagdef
4348 && declspecs->typespec_kind != ctsk_tagfirstref
4349 && declspecs->alignas_p)
4351 if (warned != 1)
4352 pedwarn (input_location, 0,
4353 "empty declaration with %<_Alignas%> "
4354 "does not redeclare tag");
4355 warned = 1;
4356 pending_xref_error ();
4358 else
4360 pending_invalid_xref = NULL_TREE;
4361 t = lookup_tag (code, name, true, NULL);
4363 if (t == NULL_TREE)
4365 t = make_node (code);
4366 pushtag (input_location, name, t);
4370 else
4372 if (warned != 1 && !in_system_header_at (input_location))
4374 pedwarn (input_location, 0,
4375 "useless type name in empty declaration");
4376 warned = 1;
4380 else if (warned != 1 && !in_system_header_at (input_location)
4381 && declspecs->typedef_p)
4383 pedwarn (input_location, 0, "useless type name in empty declaration");
4384 warned = 1;
4387 pending_invalid_xref = NULL_TREE;
4389 if (declspecs->inline_p)
4391 error ("%<inline%> in empty declaration");
4392 warned = 1;
4395 if (declspecs->noreturn_p)
4397 error ("%<_Noreturn%> in empty declaration");
4398 warned = 1;
4401 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4403 error ("%<auto%> in file-scope empty declaration");
4404 warned = 1;
4407 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4409 error ("%<register%> in file-scope empty declaration");
4410 warned = 1;
4413 if (!warned && !in_system_header_at (input_location)
4414 && declspecs->storage_class != csc_none)
4416 warning (0, "useless storage class specifier in empty declaration");
4417 warned = 2;
4420 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4422 warning (0, "useless %qs in empty declaration",
4423 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4424 warned = 2;
4427 if (!warned
4428 && !in_system_header_at (input_location)
4429 && (declspecs->const_p
4430 || declspecs->volatile_p
4431 || declspecs->atomic_p
4432 || declspecs->restrict_p
4433 || declspecs->address_space))
4435 warning (0, "useless type qualifier in empty declaration");
4436 warned = 2;
4439 if (!warned && !in_system_header_at (input_location)
4440 && declspecs->alignas_p)
4442 warning (0, "useless %<_Alignas%> in empty declaration");
4443 warned = 2;
4446 if (warned != 1)
4448 if (!found_tag)
4449 pedwarn (input_location, 0, "empty declaration");
4454 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4455 bits. SPECS represents declaration specifiers that the grammar
4456 only permits to contain type qualifiers and attributes. */
4459 quals_from_declspecs (const struct c_declspecs *specs)
4461 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4462 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4463 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4464 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4465 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4466 gcc_assert (!specs->type
4467 && !specs->decl_attr
4468 && specs->typespec_word == cts_none
4469 && specs->storage_class == csc_none
4470 && !specs->typedef_p
4471 && !specs->explicit_signed_p
4472 && !specs->deprecated_p
4473 && !specs->long_p
4474 && !specs->long_long_p
4475 && !specs->short_p
4476 && !specs->signed_p
4477 && !specs->unsigned_p
4478 && !specs->complex_p
4479 && !specs->inline_p
4480 && !specs->noreturn_p
4481 && !specs->thread_p);
4482 return quals;
4485 /* Construct an array declarator. LOC is the location of the
4486 beginning of the array (usually the opening brace). EXPR is the
4487 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4488 inside the [] (to be applied to the pointer to which a parameter
4489 array is converted). STATIC_P is true if "static" is inside the
4490 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4491 VLA of unspecified length which is nevertheless a complete type,
4492 false otherwise. The field for the contained declarator is left to
4493 be filled in by set_array_declarator_inner. */
4495 struct c_declarator *
4496 build_array_declarator (location_t loc,
4497 tree expr, struct c_declspecs *quals, bool static_p,
4498 bool vla_unspec_p)
4500 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4501 struct c_declarator);
4502 declarator->id_loc = loc;
4503 declarator->kind = cdk_array;
4504 declarator->declarator = 0;
4505 declarator->u.array.dimen = expr;
4506 if (quals)
4508 declarator->u.array.attrs = quals->attrs;
4509 declarator->u.array.quals = quals_from_declspecs (quals);
4511 else
4513 declarator->u.array.attrs = NULL_TREE;
4514 declarator->u.array.quals = 0;
4516 declarator->u.array.static_p = static_p;
4517 declarator->u.array.vla_unspec_p = vla_unspec_p;
4518 if (static_p || quals != NULL)
4519 pedwarn_c90 (loc, OPT_Wpedantic,
4520 "ISO C90 does not support %<static%> or type "
4521 "qualifiers in parameter array declarators");
4522 if (vla_unspec_p)
4523 pedwarn_c90 (loc, OPT_Wpedantic,
4524 "ISO C90 does not support %<[*]%> array declarators");
4525 if (vla_unspec_p)
4527 if (!current_scope->parm_flag)
4529 /* C99 6.7.5.2p4 */
4530 error_at (loc, "%<[*]%> not allowed in other than "
4531 "function prototype scope");
4532 declarator->u.array.vla_unspec_p = false;
4533 return NULL;
4535 current_scope->had_vla_unspec = true;
4537 return declarator;
4540 /* Set the contained declarator of an array declarator. DECL is the
4541 declarator, as constructed by build_array_declarator; INNER is what
4542 appears on the left of the []. */
4544 struct c_declarator *
4545 set_array_declarator_inner (struct c_declarator *decl,
4546 struct c_declarator *inner)
4548 decl->declarator = inner;
4549 return decl;
4552 /* INIT is a constructor that forms DECL's initializer. If the final
4553 element initializes a flexible array field, add the size of that
4554 initializer to DECL's size. */
4556 static void
4557 add_flexible_array_elts_to_size (tree decl, tree init)
4559 tree elt, type;
4561 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4562 return;
4564 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4565 type = TREE_TYPE (elt);
4566 if (TREE_CODE (type) == ARRAY_TYPE
4567 && TYPE_SIZE (type) == NULL_TREE
4568 && TYPE_DOMAIN (type) != NULL_TREE
4569 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4571 complete_array_type (&type, elt, false);
4572 DECL_SIZE (decl)
4573 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4574 DECL_SIZE_UNIT (decl)
4575 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4579 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4580 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4581 before the type name, and set *EXPR_CONST_OPERANDS, if
4582 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4583 appear in a constant expression. */
4585 tree
4586 groktypename (struct c_type_name *type_name, tree *expr,
4587 bool *expr_const_operands)
4589 tree type;
4590 tree attrs = type_name->specs->attrs;
4592 type_name->specs->attrs = NULL_TREE;
4594 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4595 false, NULL, &attrs, expr, expr_const_operands,
4596 DEPRECATED_NORMAL);
4598 /* Apply attributes. */
4599 decl_attributes (&type, attrs, 0);
4601 return type;
4604 /* Wrapper for decl_attributes that adds some implicit attributes
4605 to VAR_DECLs or FUNCTION_DECLs. */
4607 static tree
4608 c_decl_attributes (tree *node, tree attributes, int flags)
4610 /* Add implicit "omp declare target" attribute if requested. */
4611 if (current_omp_declare_target_attribute
4612 && ((VAR_P (*node) && is_global_var (*node))
4613 || TREE_CODE (*node) == FUNCTION_DECL))
4615 if (VAR_P (*node)
4616 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4617 error ("%q+D in declare target directive does not have mappable type",
4618 *node);
4619 else
4620 attributes = tree_cons (get_identifier ("omp declare target"),
4621 NULL_TREE, attributes);
4623 return decl_attributes (node, attributes, flags);
4627 /* Decode a declarator in an ordinary declaration or data definition.
4628 This is called as soon as the type information and variable name
4629 have been parsed, before parsing the initializer if any.
4630 Here we create the ..._DECL node, fill in its type,
4631 and put it on the list of decls for the current context.
4632 The ..._DECL node is returned as the value.
4634 Exception: for arrays where the length is not specified,
4635 the type is left null, to be filled in by `finish_decl'.
4637 Function definitions do not come here; they go to start_function
4638 instead. However, external and forward declarations of functions
4639 do go through here. Structure field declarations are done by
4640 grokfield and not through here. */
4642 tree
4643 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4644 bool initialized, tree attributes)
4646 tree decl;
4647 tree tem;
4648 tree expr = NULL_TREE;
4649 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4651 /* An object declared as __attribute__((deprecated)) suppresses
4652 warnings of uses of other deprecated items. */
4653 if (lookup_attribute ("deprecated", attributes))
4654 deprecated_state = DEPRECATED_SUPPRESS;
4656 decl = grokdeclarator (declarator, declspecs,
4657 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4658 deprecated_state);
4659 if (!decl || decl == error_mark_node)
4660 return NULL_TREE;
4662 if (expr)
4663 add_stmt (fold_convert (void_type_node, expr));
4665 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4666 warning (OPT_Wmain, "%q+D is usually a function", decl);
4668 if (initialized)
4669 /* Is it valid for this decl to have an initializer at all?
4670 If not, set INITIALIZED to zero, which will indirectly
4671 tell 'finish_decl' to ignore the initializer once it is parsed. */
4672 switch (TREE_CODE (decl))
4674 case TYPE_DECL:
4675 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4676 initialized = false;
4677 break;
4679 case FUNCTION_DECL:
4680 error ("function %qD is initialized like a variable", decl);
4681 initialized = false;
4682 break;
4684 case PARM_DECL:
4685 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4686 error ("parameter %qD is initialized", decl);
4687 initialized = false;
4688 break;
4690 default:
4691 /* Don't allow initializations for incomplete types except for
4692 arrays which might be completed by the initialization. */
4694 /* This can happen if the array size is an undefined macro.
4695 We already gave a warning, so we don't need another one. */
4696 if (TREE_TYPE (decl) == error_mark_node)
4697 initialized = false;
4698 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4700 /* A complete type is ok if size is fixed. */
4702 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4703 || C_DECL_VARIABLE_SIZE (decl))
4705 error ("variable-sized object may not be initialized");
4706 initialized = false;
4709 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4711 error ("variable %qD has initializer but incomplete type", decl);
4712 initialized = false;
4714 else if (C_DECL_VARIABLE_SIZE (decl))
4716 /* Although C99 is unclear about whether incomplete arrays
4717 of VLAs themselves count as VLAs, it does not make
4718 sense to permit them to be initialized given that
4719 ordinary VLAs may not be initialized. */
4720 error ("variable-sized object may not be initialized");
4721 initialized = false;
4725 if (initialized)
4727 if (current_scope == file_scope)
4728 TREE_STATIC (decl) = 1;
4730 /* Tell 'pushdecl' this is an initialized decl
4731 even though we don't yet have the initializer expression.
4732 Also tell 'finish_decl' it may store the real initializer. */
4733 DECL_INITIAL (decl) = error_mark_node;
4736 /* If this is a function declaration, write a record describing it to the
4737 prototypes file (if requested). */
4739 if (TREE_CODE (decl) == FUNCTION_DECL)
4740 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4742 /* ANSI specifies that a tentative definition which is not merged with
4743 a non-tentative definition behaves exactly like a definition with an
4744 initializer equal to zero. (Section 3.7.2)
4746 -fno-common gives strict ANSI behavior, though this tends to break
4747 a large body of code that grew up without this rule.
4749 Thread-local variables are never common, since there's no entrenched
4750 body of code to break, and it allows more efficient variable references
4751 in the presence of dynamic linking. */
4753 if (VAR_P (decl)
4754 && !initialized
4755 && TREE_PUBLIC (decl)
4756 && !DECL_THREAD_LOCAL_P (decl)
4757 && !flag_no_common)
4758 DECL_COMMON (decl) = 1;
4760 /* Set attributes here so if duplicate decl, will have proper attributes. */
4761 c_decl_attributes (&decl, attributes, 0);
4763 /* Handle gnu_inline attribute. */
4764 if (declspecs->inline_p
4765 && !flag_gnu89_inline
4766 && TREE_CODE (decl) == FUNCTION_DECL
4767 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4768 || current_function_decl))
4770 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4772 else if (declspecs->storage_class != csc_static)
4773 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4776 if (TREE_CODE (decl) == FUNCTION_DECL
4777 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4779 struct c_declarator *ce = declarator;
4781 if (ce->kind == cdk_pointer)
4782 ce = declarator->declarator;
4783 if (ce->kind == cdk_function)
4785 tree args = ce->u.arg_info->parms;
4786 for (; args; args = DECL_CHAIN (args))
4788 tree type = TREE_TYPE (args);
4789 if (type && INTEGRAL_TYPE_P (type)
4790 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4791 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4796 if (TREE_CODE (decl) == FUNCTION_DECL
4797 && DECL_DECLARED_INLINE_P (decl)
4798 && DECL_UNINLINABLE (decl)
4799 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4800 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4801 decl);
4803 /* C99 6.7.4p3: An inline definition of a function with external
4804 linkage shall not contain a definition of a modifiable object
4805 with static storage duration... */
4806 if (VAR_P (decl)
4807 && current_scope != file_scope
4808 && TREE_STATIC (decl)
4809 && !TREE_READONLY (decl)
4810 && DECL_DECLARED_INLINE_P (current_function_decl)
4811 && DECL_EXTERNAL (current_function_decl))
4812 record_inline_static (input_location, current_function_decl,
4813 decl, csi_modifiable);
4815 if (c_dialect_objc ()
4816 && VAR_OR_FUNCTION_DECL_P (decl))
4817 objc_check_global_decl (decl);
4819 /* Add this decl to the current scope.
4820 TEM may equal DECL or it may be a previous decl of the same name. */
4821 tem = pushdecl (decl);
4823 if (initialized && DECL_EXTERNAL (tem))
4825 DECL_EXTERNAL (tem) = 0;
4826 TREE_STATIC (tem) = 1;
4829 return tem;
4832 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4833 DECL or the non-array element type if DECL is an uninitialized array.
4834 If that type has a const member, diagnose this. */
4836 static void
4837 diagnose_uninitialized_cst_member (tree decl, tree type)
4839 tree field;
4840 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4842 tree field_type;
4843 if (TREE_CODE (field) != FIELD_DECL)
4844 continue;
4845 field_type = strip_array_types (TREE_TYPE (field));
4847 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4849 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4850 "uninitialized const member in %qT is invalid in C++",
4851 strip_array_types (TREE_TYPE (decl)));
4852 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4855 if (RECORD_OR_UNION_TYPE_P (field_type))
4856 diagnose_uninitialized_cst_member (decl, field_type);
4860 /* Finish processing of a declaration;
4861 install its initial value.
4862 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4863 If the length of an array type is not known before,
4864 it must be determined now, from the initial value, or it is an error.
4866 INIT_LOC is the location of the initial value. */
4868 void
4869 finish_decl (tree decl, location_t init_loc, tree init,
4870 tree origtype, tree asmspec_tree)
4872 tree type;
4873 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4874 const char *asmspec = 0;
4876 /* If a name was specified, get the string. */
4877 if (VAR_OR_FUNCTION_DECL_P (decl)
4878 && DECL_FILE_SCOPE_P (decl))
4879 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4880 if (asmspec_tree)
4881 asmspec = TREE_STRING_POINTER (asmspec_tree);
4883 if (VAR_P (decl)
4884 && TREE_STATIC (decl)
4885 && global_bindings_p ())
4886 /* So decl is a global variable. Record the types it uses
4887 so that we can decide later to emit debug info for them. */
4888 record_types_used_by_current_var_decl (decl);
4890 /* If `start_decl' didn't like having an initialization, ignore it now. */
4891 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4892 init = NULL_TREE;
4894 /* Don't crash if parm is initialized. */
4895 if (TREE_CODE (decl) == PARM_DECL)
4896 init = NULL_TREE;
4898 if (init)
4899 store_init_value (init_loc, decl, init, origtype);
4901 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4902 || TREE_CODE (decl) == FIELD_DECL))
4903 objc_check_decl (decl);
4905 type = TREE_TYPE (decl);
4907 /* Deduce size of array from initialization, if not already known. */
4908 if (TREE_CODE (type) == ARRAY_TYPE
4909 && TYPE_DOMAIN (type) == NULL_TREE
4910 && TREE_CODE (decl) != TYPE_DECL)
4912 bool do_default
4913 = (TREE_STATIC (decl)
4914 /* Even if pedantic, an external linkage array
4915 may have incomplete type at first. */
4916 ? pedantic && !TREE_PUBLIC (decl)
4917 : !DECL_EXTERNAL (decl));
4918 int failure
4919 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4920 do_default);
4922 /* Get the completed type made by complete_array_type. */
4923 type = TREE_TYPE (decl);
4925 switch (failure)
4927 case 1:
4928 error ("initializer fails to determine size of %q+D", decl);
4929 break;
4931 case 2:
4932 if (do_default)
4933 error ("array size missing in %q+D", decl);
4934 /* If a `static' var's size isn't known,
4935 make it extern as well as static, so it does not get
4936 allocated.
4937 If it is not `static', then do not mark extern;
4938 finish_incomplete_decl will give it a default size
4939 and it will get allocated. */
4940 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4941 DECL_EXTERNAL (decl) = 1;
4942 break;
4944 case 3:
4945 error ("zero or negative size array %q+D", decl);
4946 break;
4948 case 0:
4949 /* For global variables, update the copy of the type that
4950 exists in the binding. */
4951 if (TREE_PUBLIC (decl))
4953 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4954 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4955 b_ext = b_ext->shadowed;
4956 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4958 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4959 b_ext->u.type = composite_type (b_ext->u.type, type);
4960 else
4961 b_ext->u.type = type;
4964 break;
4966 default:
4967 gcc_unreachable ();
4970 if (DECL_INITIAL (decl))
4971 TREE_TYPE (DECL_INITIAL (decl)) = type;
4973 relayout_decl (decl);
4976 if (VAR_P (decl))
4978 if (init && TREE_CODE (init) == CONSTRUCTOR)
4979 add_flexible_array_elts_to_size (decl, init);
4981 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
4982 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4983 layout_decl (decl, 0);
4985 if (DECL_SIZE (decl) == NULL_TREE
4986 /* Don't give an error if we already gave one earlier. */
4987 && TREE_TYPE (decl) != error_mark_node
4988 && (TREE_STATIC (decl)
4989 /* A static variable with an incomplete type
4990 is an error if it is initialized.
4991 Also if it is not file scope.
4992 Otherwise, let it through, but if it is not `extern'
4993 then it may cause an error message later. */
4994 ? (DECL_INITIAL (decl) != NULL_TREE
4995 || !DECL_FILE_SCOPE_P (decl))
4996 /* An automatic variable with an incomplete type
4997 is an error. */
4998 : !DECL_EXTERNAL (decl)))
5000 error ("storage size of %q+D isn%'t known", decl);
5001 TREE_TYPE (decl) = error_mark_node;
5004 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5005 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5006 && DECL_SIZE (decl) == NULL_TREE
5007 && TREE_STATIC (decl))
5008 incomplete_record_decls.safe_push (decl);
5010 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5012 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5013 constant_expression_warning (DECL_SIZE (decl));
5014 else
5016 error ("storage size of %q+D isn%'t constant", decl);
5017 TREE_TYPE (decl) = error_mark_node;
5021 if (TREE_USED (type))
5023 TREE_USED (decl) = 1;
5024 DECL_READ_P (decl) = 1;
5028 /* If this is a function and an assembler name is specified, reset DECL_RTL
5029 so we can give it its new name. Also, update builtin_decl if it
5030 was a normal built-in. */
5031 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5033 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5034 set_builtin_user_assembler_name (decl, asmspec);
5035 set_user_assembler_name (decl, asmspec);
5038 /* If #pragma weak was used, mark the decl weak now. */
5039 maybe_apply_pragma_weak (decl);
5041 /* Output the assembler code and/or RTL code for variables and functions,
5042 unless the type is an undefined structure or union.
5043 If not, it will get done when the type is completed. */
5045 if (VAR_OR_FUNCTION_DECL_P (decl))
5047 /* Determine the ELF visibility. */
5048 if (TREE_PUBLIC (decl))
5049 c_determine_visibility (decl);
5051 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5052 if (c_dialect_objc ())
5053 objc_check_decl (decl);
5055 if (asmspec)
5057 /* If this is not a static variable, issue a warning.
5058 It doesn't make any sense to give an ASMSPEC for an
5059 ordinary, non-register local variable. Historically,
5060 GCC has accepted -- but ignored -- the ASMSPEC in
5061 this case. */
5062 if (!DECL_FILE_SCOPE_P (decl)
5063 && VAR_P (decl)
5064 && !C_DECL_REGISTER (decl)
5065 && !TREE_STATIC (decl))
5066 warning (0, "ignoring asm-specifier for non-static local "
5067 "variable %q+D", decl);
5068 else
5069 set_user_assembler_name (decl, asmspec);
5072 if (DECL_FILE_SCOPE_P (decl))
5074 if (DECL_INITIAL (decl) == NULL_TREE
5075 || DECL_INITIAL (decl) == error_mark_node)
5076 /* Don't output anything
5077 when a tentative file-scope definition is seen.
5078 But at end of compilation, do output code for them. */
5079 DECL_DEFER_OUTPUT (decl) = 1;
5080 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5081 DECL_HARD_REGISTER (decl) = 1;
5082 rest_of_decl_compilation (decl, true, 0);
5084 else
5086 /* In conjunction with an ASMSPEC, the `register'
5087 keyword indicates that we should place the variable
5088 in a particular register. */
5089 if (asmspec && C_DECL_REGISTER (decl))
5091 DECL_HARD_REGISTER (decl) = 1;
5092 /* This cannot be done for a structure with volatile
5093 fields, on which DECL_REGISTER will have been
5094 reset. */
5095 if (!DECL_REGISTER (decl))
5096 error ("cannot put object with volatile field into register");
5099 if (TREE_CODE (decl) != FUNCTION_DECL)
5101 /* If we're building a variable sized type, and we might be
5102 reachable other than via the top of the current binding
5103 level, then create a new BIND_EXPR so that we deallocate
5104 the object at the right time. */
5105 /* Note that DECL_SIZE can be null due to errors. */
5106 if (DECL_SIZE (decl)
5107 && !TREE_CONSTANT (DECL_SIZE (decl))
5108 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5110 tree bind;
5111 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5112 TREE_SIDE_EFFECTS (bind) = 1;
5113 add_stmt (bind);
5114 BIND_EXPR_BODY (bind) = push_stmt_list ();
5116 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5117 DECL_EXPR, decl));
5122 if (!DECL_FILE_SCOPE_P (decl))
5124 /* Recompute the RTL of a local array now
5125 if it used to be an incomplete type. */
5126 if (was_incomplete && !is_global_var (decl))
5128 /* If we used it already as memory, it must stay in memory. */
5129 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5130 /* If it's still incomplete now, no init will save it. */
5131 if (DECL_SIZE (decl) == NULL_TREE)
5132 DECL_INITIAL (decl) = NULL_TREE;
5137 if (TREE_CODE (decl) == TYPE_DECL)
5139 if (!DECL_FILE_SCOPE_P (decl)
5140 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5141 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5143 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5146 /* Install a cleanup (aka destructor) if one was given. */
5147 if (VAR_P (decl) && !TREE_STATIC (decl))
5149 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5150 if (attr)
5152 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5153 tree cleanup_decl = lookup_name (cleanup_id);
5154 tree cleanup;
5155 vec<tree, va_gc> *v;
5157 /* Build "cleanup(&decl)" for the destructor. */
5158 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5159 vec_alloc (v, 1);
5160 v->quick_push (cleanup);
5161 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5162 vNULL, cleanup_decl, v, NULL);
5163 vec_free (v);
5165 /* Don't warn about decl unused; the cleanup uses it. */
5166 TREE_USED (decl) = 1;
5167 TREE_USED (cleanup_decl) = 1;
5168 DECL_READ_P (decl) = 1;
5170 push_cleanup (decl, cleanup, false);
5174 if (warn_cxx_compat
5175 && VAR_P (decl)
5176 && !DECL_EXTERNAL (decl)
5177 && DECL_INITIAL (decl) == NULL_TREE)
5179 type = strip_array_types (type);
5180 if (TREE_READONLY (decl))
5181 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5182 "uninitialized const %qD is invalid in C++", decl);
5183 else if (RECORD_OR_UNION_TYPE_P (type)
5184 && C_TYPE_FIELDS_READONLY (type))
5185 diagnose_uninitialized_cst_member (decl, type);
5188 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5191 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5192 EXPR is NULL or a pointer to an expression that needs to be
5193 evaluated for the side effects of array size expressions in the
5194 parameters. */
5196 tree
5197 grokparm (const struct c_parm *parm, tree *expr)
5199 tree attrs = parm->attrs;
5200 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5201 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5203 decl_attributes (&decl, attrs, 0);
5205 return decl;
5208 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5209 and push that on the current scope. EXPR is a pointer to an
5210 expression that needs to be evaluated for the side effects of array
5211 size expressions in the parameters. */
5213 void
5214 push_parm_decl (const struct c_parm *parm, tree *expr)
5216 tree attrs = parm->attrs;
5217 tree decl;
5219 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5220 &attrs, expr, NULL, DEPRECATED_NORMAL);
5221 if (decl && DECL_P (decl))
5222 DECL_SOURCE_LOCATION (decl) = parm->loc;
5223 decl_attributes (&decl, attrs, 0);
5225 decl = pushdecl (decl);
5227 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5230 /* Mark all the parameter declarations to date as forward decls.
5231 Also diagnose use of this extension. */
5233 void
5234 mark_forward_parm_decls (void)
5236 struct c_binding *b;
5238 if (pedantic && !current_scope->warned_forward_parm_decls)
5240 pedwarn (input_location, OPT_Wpedantic,
5241 "ISO C forbids forward parameter declarations");
5242 current_scope->warned_forward_parm_decls = true;
5245 for (b = current_scope->bindings; b; b = b->prev)
5246 if (TREE_CODE (b->decl) == PARM_DECL)
5247 TREE_ASM_WRITTEN (b->decl) = 1;
5250 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5251 literal, which may be an incomplete array type completed by the
5252 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5253 literal. NON_CONST is true if the initializers contain something
5254 that cannot occur in a constant expression. */
5256 tree
5257 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5259 /* We do not use start_decl here because we have a type, not a declarator;
5260 and do not use finish_decl because the decl should be stored inside
5261 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5262 tree decl;
5263 tree complit;
5264 tree stmt;
5266 if (type == error_mark_node
5267 || init == error_mark_node)
5268 return error_mark_node;
5270 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5271 DECL_EXTERNAL (decl) = 0;
5272 TREE_PUBLIC (decl) = 0;
5273 TREE_STATIC (decl) = (current_scope == file_scope);
5274 DECL_CONTEXT (decl) = current_function_decl;
5275 TREE_USED (decl) = 1;
5276 DECL_READ_P (decl) = 1;
5277 DECL_ARTIFICIAL (decl) = 1;
5278 DECL_IGNORED_P (decl) = 1;
5279 TREE_TYPE (decl) = type;
5280 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5281 store_init_value (loc, decl, init, NULL_TREE);
5283 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5285 int failure = complete_array_type (&TREE_TYPE (decl),
5286 DECL_INITIAL (decl), true);
5287 /* If complete_array_type returns 3, it means that the
5288 initial value of the compound literal is empty. Allow it. */
5289 gcc_assert (failure == 0 || failure == 3);
5291 type = TREE_TYPE (decl);
5292 TREE_TYPE (DECL_INITIAL (decl)) = type;
5295 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5297 c_incomplete_type_error (loc, NULL_TREE, type);
5298 return error_mark_node;
5301 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5302 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5303 TREE_SIDE_EFFECTS (complit) = 1;
5305 layout_decl (decl, 0);
5307 if (TREE_STATIC (decl))
5309 /* This decl needs a name for the assembler output. */
5310 set_compound_literal_name (decl);
5311 DECL_DEFER_OUTPUT (decl) = 1;
5312 DECL_COMDAT (decl) = 1;
5313 pushdecl (decl);
5314 rest_of_decl_compilation (decl, 1, 0);
5317 if (non_const)
5319 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5320 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5323 return complit;
5326 /* Check the type of a compound literal. Here we just check that it
5327 is valid for C++. */
5329 void
5330 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5332 if (warn_cxx_compat
5333 && (type_name->specs->typespec_kind == ctsk_tagdef
5334 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5335 warning_at (loc, OPT_Wc___compat,
5336 "defining a type in a compound literal is invalid in C++");
5339 /* Determine whether TYPE is a structure with a flexible array member,
5340 or a union containing such a structure (possibly recursively). */
5342 static bool
5343 flexible_array_type_p (tree type)
5345 tree x;
5346 switch (TREE_CODE (type))
5348 case RECORD_TYPE:
5349 x = TYPE_FIELDS (type);
5350 if (x == NULL_TREE)
5351 return false;
5352 while (DECL_CHAIN (x) != NULL_TREE)
5353 x = DECL_CHAIN (x);
5354 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5355 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5356 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5357 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5358 return true;
5359 return false;
5360 case UNION_TYPE:
5361 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5363 if (flexible_array_type_p (TREE_TYPE (x)))
5364 return true;
5366 return false;
5367 default:
5368 return false;
5372 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5373 replacing with appropriate values if they are invalid. */
5375 static void
5376 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5377 tree orig_name)
5379 tree type_mv;
5380 unsigned int max_width;
5381 unsigned HOST_WIDE_INT w;
5382 const char *name = (orig_name
5383 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5384 : _("<anonymous>"));
5386 /* Detect and ignore out of range field width and process valid
5387 field widths. */
5388 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5390 error_at (loc, "bit-field %qs width not an integer constant", name);
5391 *width = integer_one_node;
5393 else
5395 if (TREE_CODE (*width) != INTEGER_CST)
5397 *width = c_fully_fold (*width, false, NULL);
5398 if (TREE_CODE (*width) == INTEGER_CST)
5399 pedwarn (loc, OPT_Wpedantic,
5400 "bit-field %qs width not an integer constant expression",
5401 name);
5403 if (TREE_CODE (*width) != INTEGER_CST)
5405 error_at (loc, "bit-field %qs width not an integer constant", name);
5406 *width = integer_one_node;
5408 constant_expression_warning (*width);
5409 if (tree_int_cst_sgn (*width) < 0)
5411 error_at (loc, "negative width in bit-field %qs", name);
5412 *width = integer_one_node;
5414 else if (integer_zerop (*width) && orig_name)
5416 error_at (loc, "zero width for bit-field %qs", name);
5417 *width = integer_one_node;
5421 /* Detect invalid bit-field type. */
5422 if (TREE_CODE (*type) != INTEGER_TYPE
5423 && TREE_CODE (*type) != BOOLEAN_TYPE
5424 && TREE_CODE (*type) != ENUMERAL_TYPE)
5426 error_at (loc, "bit-field %qs has invalid type", name);
5427 *type = unsigned_type_node;
5430 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5432 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5433 name);
5434 *type = unsigned_type_node;
5437 type_mv = TYPE_MAIN_VARIANT (*type);
5438 if (!in_system_header_at (input_location)
5439 && type_mv != integer_type_node
5440 && type_mv != unsigned_type_node
5441 && type_mv != boolean_type_node)
5442 pedwarn_c90 (loc, OPT_Wpedantic,
5443 "type of bit-field %qs is a GCC extension", name);
5445 max_width = TYPE_PRECISION (*type);
5447 if (0 < compare_tree_int (*width, max_width))
5449 error_at (loc, "width of %qs exceeds its type", name);
5450 w = max_width;
5451 *width = build_int_cst (integer_type_node, w);
5453 else
5454 w = tree_to_uhwi (*width);
5456 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5458 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5459 if (!lt
5460 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5461 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5462 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5468 /* Print warning about variable length array if necessary. */
5470 static void
5471 warn_variable_length_array (tree name, tree size)
5473 if (TREE_CONSTANT (size))
5475 if (name)
5476 pedwarn_c90 (input_location, OPT_Wvla,
5477 "ISO C90 forbids array %qE whose size "
5478 "can%'t be evaluated", name);
5479 else
5480 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5481 "whose size can%'t be evaluated");
5483 else
5485 if (name)
5486 pedwarn_c90 (input_location, OPT_Wvla,
5487 "ISO C90 forbids variable length array %qE", name);
5488 else
5489 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5490 "length array");
5494 /* Print warning about defaulting to int if necessary. */
5496 static void
5497 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5499 diagnostic_info diagnostic;
5500 va_list ap;
5501 rich_location richloc (line_table, location);
5503 va_start (ap, gmsgid);
5504 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5505 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5506 diagnostic.option_index = opt;
5507 diagnostic_report_diagnostic (global_dc, &diagnostic);
5508 va_end (ap);
5511 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5512 considering only those c_declspec_words found in LIST, which
5513 must be terminated by cdw_number_of_elements. */
5515 static location_t
5516 smallest_type_quals_location (const location_t *locations,
5517 const c_declspec_word *list)
5519 location_t loc = UNKNOWN_LOCATION;
5520 while (*list != cdw_number_of_elements)
5522 location_t newloc = locations[*list];
5523 if (loc == UNKNOWN_LOCATION
5524 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5525 loc = newloc;
5526 list++;
5529 return loc;
5532 /* Given declspecs and a declarator,
5533 determine the name and type of the object declared
5534 and construct a ..._DECL node for it.
5535 (In one case we can return a ..._TYPE node instead.
5536 For invalid input we sometimes return NULL_TREE.)
5538 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5540 DECL_CONTEXT says which syntactic context this declaration is in:
5541 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5542 FUNCDEF for a function definition. Like NORMAL but a few different
5543 error messages in each case. Return value may be zero meaning
5544 this definition is too screwy to try to parse.
5545 PARM for a parameter declaration (either within a function prototype
5546 or before a function body). Make a PARM_DECL, or return void_type_node.
5547 TYPENAME if for a typename (in a cast or sizeof).
5548 Don't make a DECL node; just return the ..._TYPE node.
5549 FIELD for a struct or union field; make a FIELD_DECL.
5550 INITIALIZED is true if the decl has an initializer.
5551 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5552 representing the width of the bit-field.
5553 DECL_ATTRS points to the list of attributes that should be added to this
5554 decl. Any nested attributes that belong on the decl itself will be
5555 added to this list.
5556 If EXPR is not NULL, any expressions that need to be evaluated as
5557 part of evaluating variably modified types will be stored in *EXPR.
5558 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5559 set to indicate whether operands in *EXPR can be used in constant
5560 expressions.
5561 DEPRECATED_STATE is a deprecated_states value indicating whether
5562 deprecation warnings should be suppressed.
5564 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5565 It may also be so in the PARM case, for a prototype where the
5566 argument type is specified but not the name.
5568 This function is where the complicated C meanings of `static'
5569 and `extern' are interpreted. */
5571 static tree
5572 grokdeclarator (const struct c_declarator *declarator,
5573 struct c_declspecs *declspecs,
5574 enum decl_context decl_context, bool initialized, tree *width,
5575 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5576 enum deprecated_states deprecated_state)
5578 tree type = declspecs->type;
5579 bool threadp = declspecs->thread_p;
5580 enum c_storage_class storage_class = declspecs->storage_class;
5581 int constp;
5582 int restrictp;
5583 int volatilep;
5584 int atomicp;
5585 int type_quals = TYPE_UNQUALIFIED;
5586 tree name = NULL_TREE;
5587 bool funcdef_flag = false;
5588 bool funcdef_syntax = false;
5589 bool size_varies = false;
5590 tree decl_attr = declspecs->decl_attr;
5591 int array_ptr_quals = TYPE_UNQUALIFIED;
5592 tree array_ptr_attrs = NULL_TREE;
5593 bool array_parm_static = false;
5594 bool array_parm_vla_unspec_p = false;
5595 tree returned_attrs = NULL_TREE;
5596 bool bitfield = width != NULL;
5597 tree element_type;
5598 tree orig_qual_type = NULL;
5599 size_t orig_qual_indirect = 0;
5600 struct c_arg_info *arg_info = 0;
5601 addr_space_t as1, as2, address_space;
5602 location_t loc = UNKNOWN_LOCATION;
5603 tree expr_dummy;
5604 bool expr_const_operands_dummy;
5605 enum c_declarator_kind first_non_attr_kind;
5606 unsigned int alignas_align = 0;
5608 if (TREE_CODE (type) == ERROR_MARK)
5609 return error_mark_node;
5610 if (expr == NULL)
5612 expr = &expr_dummy;
5613 expr_dummy = NULL_TREE;
5615 if (expr_const_operands == NULL)
5616 expr_const_operands = &expr_const_operands_dummy;
5618 if (declspecs->expr)
5620 if (*expr)
5621 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5622 declspecs->expr);
5623 else
5624 *expr = declspecs->expr;
5626 *expr_const_operands = declspecs->expr_const_operands;
5628 if (decl_context == FUNCDEF)
5629 funcdef_flag = true, decl_context = NORMAL;
5631 /* Look inside a declarator for the name being declared
5632 and get it as an IDENTIFIER_NODE, for an error message. */
5634 const struct c_declarator *decl = declarator;
5636 first_non_attr_kind = cdk_attrs;
5637 while (decl)
5638 switch (decl->kind)
5640 case cdk_array:
5641 loc = decl->id_loc;
5642 /* FALL THRU. */
5644 case cdk_function:
5645 case cdk_pointer:
5646 funcdef_syntax = (decl->kind == cdk_function);
5647 if (first_non_attr_kind == cdk_attrs)
5648 first_non_attr_kind = decl->kind;
5649 decl = decl->declarator;
5650 break;
5652 case cdk_attrs:
5653 decl = decl->declarator;
5654 break;
5656 case cdk_id:
5657 loc = decl->id_loc;
5658 if (decl->u.id)
5659 name = decl->u.id;
5660 if (first_non_attr_kind == cdk_attrs)
5661 first_non_attr_kind = decl->kind;
5662 decl = 0;
5663 break;
5665 default:
5666 gcc_unreachable ();
5668 if (name == NULL_TREE)
5670 gcc_assert (decl_context == PARM
5671 || decl_context == TYPENAME
5672 || (decl_context == FIELD
5673 && declarator->kind == cdk_id));
5674 gcc_assert (!initialized);
5678 /* A function definition's declarator must have the form of
5679 a function declarator. */
5681 if (funcdef_flag && !funcdef_syntax)
5682 return NULL_TREE;
5684 /* If this looks like a function definition, make it one,
5685 even if it occurs where parms are expected.
5686 Then store_parm_decls will reject it and not use it as a parm. */
5687 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5688 decl_context = PARM;
5690 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5691 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5693 if ((decl_context == NORMAL || decl_context == FIELD)
5694 && current_scope == file_scope
5695 && variably_modified_type_p (type, NULL_TREE))
5697 if (name)
5698 error_at (loc, "variably modified %qE at file scope", name);
5699 else
5700 error_at (loc, "variably modified field at file scope");
5701 type = integer_type_node;
5704 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5706 /* Diagnose defaulting to "int". */
5708 if (declspecs->default_int_p && !in_system_header_at (input_location))
5710 /* Issue a warning if this is an ISO C 99 program or if
5711 -Wreturn-type and this is a function, or if -Wimplicit;
5712 prefer the former warning since it is more explicit. */
5713 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5714 && funcdef_flag)
5715 warn_about_return_type = 1;
5716 else
5718 if (name)
5719 warn_defaults_to (loc, OPT_Wimplicit_int,
5720 "type defaults to %<int%> in declaration "
5721 "of %qE", name);
5722 else
5723 warn_defaults_to (loc, OPT_Wimplicit_int,
5724 "type defaults to %<int%> in type name");
5728 /* Adjust the type if a bit-field is being declared,
5729 -funsigned-bitfields applied and the type is not explicitly
5730 "signed". */
5731 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5732 && TREE_CODE (type) == INTEGER_TYPE)
5733 type = unsigned_type_for (type);
5735 /* Figure out the type qualifiers for the declaration. There are
5736 two ways a declaration can become qualified. One is something
5737 like `const int i' where the `const' is explicit. Another is
5738 something like `typedef const int CI; CI i' where the type of the
5739 declaration contains the `const'. A third possibility is that
5740 there is a type qualifier on the element type of a typedefed
5741 array type, in which case we should extract that qualifier so
5742 that c_apply_type_quals_to_decl receives the full list of
5743 qualifiers to work with (C90 is not entirely clear about whether
5744 duplicate qualifiers should be diagnosed in this case, but it
5745 seems most appropriate to do so). */
5746 element_type = strip_array_types (type);
5747 constp = declspecs->const_p + TYPE_READONLY (element_type);
5748 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5749 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5750 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5751 as1 = declspecs->address_space;
5752 as2 = TYPE_ADDR_SPACE (element_type);
5753 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5755 if (constp > 1)
5756 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5757 if (restrictp > 1)
5758 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5759 if (volatilep > 1)
5760 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5761 if (atomicp > 1)
5762 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5764 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5765 error_at (loc, "conflicting named address spaces (%s vs %s)",
5766 c_addr_space_name (as1), c_addr_space_name (as2));
5768 if ((TREE_CODE (type) == ARRAY_TYPE
5769 || first_non_attr_kind == cdk_array)
5770 && TYPE_QUALS (element_type))
5772 orig_qual_type = type;
5773 type = TYPE_MAIN_VARIANT (type);
5775 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5776 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5777 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5778 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5779 | ENCODE_QUAL_ADDR_SPACE (address_space));
5780 if (type_quals != TYPE_QUALS (element_type))
5781 orig_qual_type = NULL_TREE;
5783 /* Applying the _Atomic qualifier to an array type (through the use
5784 of typedefs or typeof) must be detected here. If the qualifier
5785 is introduced later, any appearance of applying it to an array is
5786 actually applying it to an element of that array. */
5787 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5788 error_at (loc, "%<_Atomic%>-qualified array type");
5790 /* Warn about storage classes that are invalid for certain
5791 kinds of declarations (parameters, typenames, etc.). */
5793 if (funcdef_flag
5794 && (threadp
5795 || storage_class == csc_auto
5796 || storage_class == csc_register
5797 || storage_class == csc_typedef))
5799 if (storage_class == csc_auto)
5800 pedwarn (loc,
5801 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5802 "function definition declared %<auto%>");
5803 if (storage_class == csc_register)
5804 error_at (loc, "function definition declared %<register%>");
5805 if (storage_class == csc_typedef)
5806 error_at (loc, "function definition declared %<typedef%>");
5807 if (threadp)
5808 error_at (loc, "function definition declared %qs",
5809 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5810 threadp = false;
5811 if (storage_class == csc_auto
5812 || storage_class == csc_register
5813 || storage_class == csc_typedef)
5814 storage_class = csc_none;
5816 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5818 if (decl_context == PARM && storage_class == csc_register)
5820 else
5822 switch (decl_context)
5824 case FIELD:
5825 if (name)
5826 error_at (loc, "storage class specified for structure "
5827 "field %qE", name);
5828 else
5829 error_at (loc, "storage class specified for structure field");
5830 break;
5831 case PARM:
5832 if (name)
5833 error_at (loc, "storage class specified for parameter %qE",
5834 name);
5835 else
5836 error_at (loc, "storage class specified for unnamed parameter");
5837 break;
5838 default:
5839 error_at (loc, "storage class specified for typename");
5840 break;
5842 storage_class = csc_none;
5843 threadp = false;
5846 else if (storage_class == csc_extern
5847 && initialized
5848 && !funcdef_flag)
5850 /* 'extern' with initialization is invalid if not at file scope. */
5851 if (current_scope == file_scope)
5853 /* It is fine to have 'extern const' when compiling at C
5854 and C++ intersection. */
5855 if (!(warn_cxx_compat && constp))
5856 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5857 name);
5859 else
5860 error_at (loc, "%qE has both %<extern%> and initializer", name);
5862 else if (current_scope == file_scope)
5864 if (storage_class == csc_auto)
5865 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5866 name);
5867 if (pedantic && storage_class == csc_register)
5868 pedwarn (input_location, OPT_Wpedantic,
5869 "file-scope declaration of %qE specifies %<register%>", name);
5871 else
5873 if (storage_class == csc_extern && funcdef_flag)
5874 error_at (loc, "nested function %qE declared %<extern%>", name);
5875 else if (threadp && storage_class == csc_none)
5877 error_at (loc, "function-scope %qE implicitly auto and declared "
5878 "%qs", name,
5879 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5880 threadp = false;
5884 /* Now figure out the structure of the declarator proper.
5885 Descend through it, creating more complex types, until we reach
5886 the declared identifier (or NULL_TREE, in an absolute declarator).
5887 At each stage we maintain an unqualified version of the type
5888 together with any qualifiers that should be applied to it with
5889 c_build_qualified_type; this way, array types including
5890 multidimensional array types are first built up in unqualified
5891 form and then the qualified form is created with
5892 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5894 while (declarator && declarator->kind != cdk_id)
5896 if (type == error_mark_node)
5898 declarator = declarator->declarator;
5899 continue;
5902 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5903 a cdk_pointer (for *...),
5904 a cdk_function (for ...(...)),
5905 a cdk_attrs (for nested attributes),
5906 or a cdk_id (for the name being declared
5907 or the place in an absolute declarator
5908 where the name was omitted).
5909 For the last case, we have just exited the loop.
5911 At this point, TYPE is the type of elements of an array,
5912 or for a function to return, or for a pointer to point to.
5913 After this sequence of ifs, TYPE is the type of the
5914 array or function or pointer, and DECLARATOR has had its
5915 outermost layer removed. */
5917 if (array_ptr_quals != TYPE_UNQUALIFIED
5918 || array_ptr_attrs != NULL_TREE
5919 || array_parm_static)
5921 /* Only the innermost declarator (making a parameter be of
5922 array type which is converted to pointer type)
5923 may have static or type qualifiers. */
5924 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5925 array_ptr_quals = TYPE_UNQUALIFIED;
5926 array_ptr_attrs = NULL_TREE;
5927 array_parm_static = false;
5930 switch (declarator->kind)
5932 case cdk_attrs:
5934 /* A declarator with embedded attributes. */
5935 tree attrs = declarator->u.attrs;
5936 const struct c_declarator *inner_decl;
5937 int attr_flags = 0;
5938 declarator = declarator->declarator;
5939 inner_decl = declarator;
5940 while (inner_decl->kind == cdk_attrs)
5941 inner_decl = inner_decl->declarator;
5942 if (inner_decl->kind == cdk_id)
5943 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5944 else if (inner_decl->kind == cdk_function)
5945 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5946 else if (inner_decl->kind == cdk_array)
5947 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5948 returned_attrs = decl_attributes (&type,
5949 chainon (returned_attrs, attrs),
5950 attr_flags);
5951 break;
5953 case cdk_array:
5955 tree itype = NULL_TREE;
5956 tree size = declarator->u.array.dimen;
5957 /* The index is a signed object `sizetype' bits wide. */
5958 tree index_type = c_common_signed_type (sizetype);
5960 array_ptr_quals = declarator->u.array.quals;
5961 array_ptr_attrs = declarator->u.array.attrs;
5962 array_parm_static = declarator->u.array.static_p;
5963 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5965 declarator = declarator->declarator;
5967 /* Check for some types that there cannot be arrays of. */
5969 if (VOID_TYPE_P (type))
5971 if (name)
5972 error_at (loc, "declaration of %qE as array of voids", name);
5973 else
5974 error_at (loc, "declaration of type name as array of voids");
5975 type = error_mark_node;
5978 if (TREE_CODE (type) == FUNCTION_TYPE)
5980 if (name)
5981 error_at (loc, "declaration of %qE as array of functions",
5982 name);
5983 else
5984 error_at (loc, "declaration of type name as array of "
5985 "functions");
5986 type = error_mark_node;
5989 if (pedantic && !in_system_header_at (input_location)
5990 && flexible_array_type_p (type))
5991 pedwarn (loc, OPT_Wpedantic,
5992 "invalid use of structure with flexible array member");
5994 if (size == error_mark_node)
5995 type = error_mark_node;
5997 if (type == error_mark_node)
5998 continue;
6000 /* If size was specified, set ITYPE to a range-type for
6001 that size. Otherwise, ITYPE remains null. finish_decl
6002 may figure it out from an initial value. */
6004 if (size)
6006 bool size_maybe_const = true;
6007 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6008 && !TREE_OVERFLOW (size));
6009 bool this_size_varies = false;
6011 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6012 lvalue. */
6013 STRIP_TYPE_NOPS (size);
6015 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6017 if (name)
6018 error_at (loc, "size of array %qE has non-integer type",
6019 name);
6020 else
6021 error_at (loc,
6022 "size of unnamed array has non-integer type");
6023 size = integer_one_node;
6025 /* This can happen with enum forward declaration. */
6026 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6028 if (name)
6029 error_at (loc, "size of array %qE has incomplete type",
6030 name);
6031 else
6032 error_at (loc, "size of unnamed array has incomplete "
6033 "type");
6034 size = integer_one_node;
6037 size = c_fully_fold (size, false, &size_maybe_const);
6039 if (pedantic && size_maybe_const && integer_zerop (size))
6041 if (name)
6042 pedwarn (loc, OPT_Wpedantic,
6043 "ISO C forbids zero-size array %qE", name);
6044 else
6045 pedwarn (loc, OPT_Wpedantic,
6046 "ISO C forbids zero-size array");
6049 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6051 constant_expression_warning (size);
6052 if (tree_int_cst_sgn (size) < 0)
6054 if (name)
6055 error_at (loc, "size of array %qE is negative", name);
6056 else
6057 error_at (loc, "size of unnamed array is negative");
6058 size = integer_one_node;
6060 /* Handle a size folded to an integer constant but
6061 not an integer constant expression. */
6062 if (!size_int_const)
6064 /* If this is a file scope declaration of an
6065 ordinary identifier, this is invalid code;
6066 diagnosing it here and not subsequently
6067 treating the type as variable-length avoids
6068 more confusing diagnostics later. */
6069 if ((decl_context == NORMAL || decl_context == FIELD)
6070 && current_scope == file_scope)
6071 pedwarn (input_location, 0,
6072 "variably modified %qE at file scope",
6073 name);
6074 else
6075 this_size_varies = size_varies = true;
6076 warn_variable_length_array (name, size);
6079 else if ((decl_context == NORMAL || decl_context == FIELD)
6080 && current_scope == file_scope)
6082 error_at (loc, "variably modified %qE at file scope", name);
6083 size = integer_one_node;
6085 else
6087 /* Make sure the array size remains visibly
6088 nonconstant even if it is (eg) a const variable
6089 with known value. */
6090 this_size_varies = size_varies = true;
6091 warn_variable_length_array (name, size);
6092 if (sanitize_flags_p (SANITIZE_VLA)
6093 && current_function_decl != NULL_TREE
6094 && decl_context == NORMAL)
6096 /* Evaluate the array size only once. */
6097 size = save_expr (size);
6098 size = c_fully_fold (size, false, NULL);
6099 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6100 ubsan_instrument_vla (loc, size),
6101 size);
6105 if (integer_zerop (size) && !this_size_varies)
6107 /* A zero-length array cannot be represented with
6108 an unsigned index type, which is what we'll
6109 get with build_index_type. Create an
6110 open-ended range instead. */
6111 itype = build_range_type (sizetype, size, NULL_TREE);
6113 else
6115 /* Arrange for the SAVE_EXPR on the inside of the
6116 MINUS_EXPR, which allows the -1 to get folded
6117 with the +1 that happens when building TYPE_SIZE. */
6118 if (size_varies)
6119 size = save_expr (size);
6120 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6121 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6122 integer_zero_node, size);
6124 /* Compute the maximum valid index, that is, size
6125 - 1. Do the calculation in index_type, so that
6126 if it is a variable the computations will be
6127 done in the proper mode. */
6128 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6129 convert (index_type, size),
6130 convert (index_type,
6131 size_one_node));
6133 /* The above overflows when size does not fit
6134 in index_type.
6135 ??? While a size of INT_MAX+1 technically shouldn't
6136 cause an overflow (because we subtract 1), handling
6137 this case seems like an unnecessary complication. */
6138 if (TREE_CODE (size) == INTEGER_CST
6139 && !int_fits_type_p (size, index_type))
6141 if (name)
6142 error_at (loc, "size of array %qE is too large",
6143 name);
6144 else
6145 error_at (loc, "size of unnamed array is too large");
6146 type = error_mark_node;
6147 continue;
6150 itype = build_index_type (itype);
6152 if (this_size_varies)
6154 if (*expr)
6155 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6156 *expr, size);
6157 else
6158 *expr = size;
6159 *expr_const_operands &= size_maybe_const;
6162 else if (decl_context == FIELD)
6164 bool flexible_array_member = false;
6165 if (array_parm_vla_unspec_p)
6166 /* Field names can in fact have function prototype
6167 scope so [*] is disallowed here through making
6168 the field variably modified, not through being
6169 something other than a declaration with function
6170 prototype scope. */
6171 size_varies = true;
6172 else
6174 const struct c_declarator *t = declarator;
6175 while (t->kind == cdk_attrs)
6176 t = t->declarator;
6177 flexible_array_member = (t->kind == cdk_id);
6179 if (flexible_array_member
6180 && !in_system_header_at (input_location))
6181 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6182 "support flexible array members");
6184 /* ISO C99 Flexible array members are effectively
6185 identical to GCC's zero-length array extension. */
6186 if (flexible_array_member || array_parm_vla_unspec_p)
6187 itype = build_range_type (sizetype, size_zero_node,
6188 NULL_TREE);
6190 else if (decl_context == PARM)
6192 if (array_parm_vla_unspec_p)
6194 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6195 size_varies = true;
6198 else if (decl_context == TYPENAME)
6200 if (array_parm_vla_unspec_p)
6202 /* C99 6.7.5.2p4 */
6203 warning (0, "%<[*]%> not in a declaration");
6204 /* We use this to avoid messing up with incomplete
6205 array types of the same type, that would
6206 otherwise be modified below. */
6207 itype = build_range_type (sizetype, size_zero_node,
6208 NULL_TREE);
6209 size_varies = true;
6213 /* Complain about arrays of incomplete types. */
6214 if (!COMPLETE_TYPE_P (type))
6216 error_at (loc, "array type has incomplete element type %qT",
6217 type);
6218 /* See if we can be more helpful. */
6219 if (TREE_CODE (type) == ARRAY_TYPE)
6221 if (name)
6222 inform (loc, "declaration of %qE as multidimensional "
6223 "array must have bounds for all dimensions "
6224 "except the first", name);
6225 else
6226 inform (loc, "declaration of multidimensional array "
6227 "must have bounds for all dimensions except "
6228 "the first");
6230 type = error_mark_node;
6232 else
6233 /* When itype is NULL, a shared incomplete array type is
6234 returned for all array of a given type. Elsewhere we
6235 make sure we don't complete that type before copying
6236 it, but here we want to make sure we don't ever
6237 modify the shared type, so we gcc_assert (itype)
6238 below. */
6240 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6241 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6242 type = build_qualified_type (type,
6243 ENCODE_QUAL_ADDR_SPACE (as));
6245 type = build_array_type (type, itype);
6248 if (type != error_mark_node)
6250 if (size_varies)
6252 /* It is ok to modify type here even if itype is
6253 NULL: if size_varies, we're in a
6254 multi-dimensional array and the inner type has
6255 variable size, so the enclosing shared array type
6256 must too. */
6257 if (size && TREE_CODE (size) == INTEGER_CST)
6258 type
6259 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6260 C_TYPE_VARIABLE_SIZE (type) = 1;
6263 /* The GCC extension for zero-length arrays differs from
6264 ISO flexible array members in that sizeof yields
6265 zero. */
6266 if (size && integer_zerop (size))
6268 gcc_assert (itype);
6269 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6270 TYPE_SIZE (type) = bitsize_zero_node;
6271 TYPE_SIZE_UNIT (type) = size_zero_node;
6272 SET_TYPE_STRUCTURAL_EQUALITY (type);
6274 if (array_parm_vla_unspec_p)
6276 gcc_assert (itype);
6277 /* The type is complete. C99 6.7.5.2p4 */
6278 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6279 TYPE_SIZE (type) = bitsize_zero_node;
6280 TYPE_SIZE_UNIT (type) = size_zero_node;
6281 SET_TYPE_STRUCTURAL_EQUALITY (type);
6284 if (!valid_array_size_p (loc, type, name))
6285 type = error_mark_node;
6288 if (decl_context != PARM
6289 && (array_ptr_quals != TYPE_UNQUALIFIED
6290 || array_ptr_attrs != NULL_TREE
6291 || array_parm_static))
6293 error_at (loc, "static or type qualifiers in non-parameter "
6294 "array declarator");
6295 array_ptr_quals = TYPE_UNQUALIFIED;
6296 array_ptr_attrs = NULL_TREE;
6297 array_parm_static = false;
6299 orig_qual_indirect++;
6300 break;
6302 case cdk_function:
6304 /* Say it's a definition only for the declarator closest
6305 to the identifier, apart possibly from some
6306 attributes. */
6307 bool really_funcdef = false;
6308 tree arg_types;
6309 orig_qual_type = NULL_TREE;
6310 if (funcdef_flag)
6312 const struct c_declarator *t = declarator->declarator;
6313 while (t->kind == cdk_attrs)
6314 t = t->declarator;
6315 really_funcdef = (t->kind == cdk_id);
6318 /* Declaring a function type. Make sure we have a valid
6319 type for the function to return. */
6320 if (type == error_mark_node)
6321 continue;
6323 size_varies = false;
6325 /* Warn about some types functions can't return. */
6326 if (TREE_CODE (type) == FUNCTION_TYPE)
6328 if (name)
6329 error_at (loc, "%qE declared as function returning a "
6330 "function", name);
6331 else
6332 error_at (loc, "type name declared as function "
6333 "returning a function");
6334 type = integer_type_node;
6336 if (TREE_CODE (type) == ARRAY_TYPE)
6338 if (name)
6339 error_at (loc, "%qE declared as function returning an array",
6340 name);
6341 else
6342 error_at (loc, "type name declared as function returning "
6343 "an array");
6344 type = integer_type_node;
6347 /* Construct the function type and go to the next
6348 inner layer of declarator. */
6349 arg_info = declarator->u.arg_info;
6350 arg_types = grokparms (arg_info, really_funcdef);
6352 /* Type qualifiers before the return type of the function
6353 qualify the return type, not the function type. */
6354 if (type_quals)
6356 const enum c_declspec_word ignored_quals_list[] =
6358 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6359 cdw_atomic, cdw_number_of_elements
6361 location_t specs_loc
6362 = smallest_type_quals_location (declspecs->locations,
6363 ignored_quals_list);
6364 if (specs_loc == UNKNOWN_LOCATION)
6365 specs_loc = declspecs->locations[cdw_typedef];
6366 if (specs_loc == UNKNOWN_LOCATION)
6367 specs_loc = loc;
6369 /* Type qualifiers on a function return type are
6370 normally permitted by the standard but have no
6371 effect, so give a warning at -Wreturn-type.
6372 Qualifiers on a void return type are banned on
6373 function definitions in ISO C; GCC used to used
6374 them for noreturn functions. The resolution of C11
6375 DR#423 means qualifiers (other than _Atomic) are
6376 actually removed from the return type when
6377 determining the function type. */
6378 int quals_used = type_quals;
6379 if (flag_isoc11)
6380 quals_used &= TYPE_QUAL_ATOMIC;
6381 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6382 pedwarn (specs_loc, 0,
6383 "function definition has qualified void return type");
6384 else
6385 warning_at (specs_loc, OPT_Wignored_qualifiers,
6386 "type qualifiers ignored on function return type");
6388 /* Ensure an error for restrict on invalid types; the
6389 DR#423 resolution is not entirely clear about
6390 this. */
6391 if (flag_isoc11
6392 && (type_quals & TYPE_QUAL_RESTRICT)
6393 && (!POINTER_TYPE_P (type)
6394 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6395 error_at (loc, "invalid use of %<restrict%>");
6396 if (quals_used)
6397 type = c_build_qualified_type (type, quals_used);
6399 type_quals = TYPE_UNQUALIFIED;
6401 type = build_function_type (type, arg_types);
6402 declarator = declarator->declarator;
6404 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6405 the formal parameter list of this FUNCTION_TYPE to point to
6406 the FUNCTION_TYPE node itself. */
6408 c_arg_tag *tag;
6409 unsigned ix;
6411 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6412 TYPE_CONTEXT (tag->type) = type;
6414 break;
6416 case cdk_pointer:
6418 /* Merge any constancy or volatility into the target type
6419 for the pointer. */
6420 if ((type_quals & TYPE_QUAL_ATOMIC)
6421 && TREE_CODE (type) == FUNCTION_TYPE)
6423 error_at (loc,
6424 "%<_Atomic%>-qualified function type");
6425 type_quals &= ~TYPE_QUAL_ATOMIC;
6427 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6428 && type_quals)
6429 pedwarn (loc, OPT_Wpedantic,
6430 "ISO C forbids qualified function types");
6431 if (type_quals)
6432 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6433 orig_qual_indirect);
6434 orig_qual_type = NULL_TREE;
6435 size_varies = false;
6437 /* When the pointed-to type involves components of variable size,
6438 care must be taken to ensure that the size evaluation code is
6439 emitted early enough to dominate all the possible later uses
6440 and late enough for the variables on which it depends to have
6441 been assigned.
6443 This is expected to happen automatically when the pointed-to
6444 type has a name/declaration of it's own, but special attention
6445 is required if the type is anonymous.
6447 We handle the NORMAL and FIELD contexts here by attaching an
6448 artificial TYPE_DECL to such pointed-to type. This forces the
6449 sizes evaluation at a safe point and ensures it is not deferred
6450 until e.g. within a deeper conditional context.
6452 We expect nothing to be needed here for PARM or TYPENAME.
6453 Pushing a TYPE_DECL at this point for TYPENAME would actually
6454 be incorrect, as we might be in the middle of an expression
6455 with side effects on the pointed-to type size "arguments" prior
6456 to the pointer declaration point and the fake TYPE_DECL in the
6457 enclosing context would force the size evaluation prior to the
6458 side effects. */
6460 if (!TYPE_NAME (type)
6461 && (decl_context == NORMAL || decl_context == FIELD)
6462 && variably_modified_type_p (type, NULL_TREE))
6464 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6465 DECL_ARTIFICIAL (decl) = 1;
6466 pushdecl (decl);
6467 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6468 TYPE_NAME (type) = decl;
6471 type = c_build_pointer_type (type);
6473 /* Process type qualifiers (such as const or volatile)
6474 that were given inside the `*'. */
6475 type_quals = declarator->u.pointer_quals;
6477 declarator = declarator->declarator;
6478 break;
6480 default:
6481 gcc_unreachable ();
6484 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6486 /* Now TYPE has the actual type, apart from any qualifiers in
6487 TYPE_QUALS. */
6489 /* Warn about address space used for things other than static memory or
6490 pointers. */
6491 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6492 if (!ADDR_SPACE_GENERIC_P (address_space))
6494 if (decl_context == NORMAL)
6496 switch (storage_class)
6498 case csc_auto:
6499 error ("%qs combined with %<auto%> qualifier for %qE",
6500 c_addr_space_name (address_space), name);
6501 break;
6502 case csc_register:
6503 error ("%qs combined with %<register%> qualifier for %qE",
6504 c_addr_space_name (address_space), name);
6505 break;
6506 case csc_none:
6507 if (current_function_scope)
6509 error ("%qs specified for auto variable %qE",
6510 c_addr_space_name (address_space), name);
6511 break;
6513 break;
6514 case csc_static:
6515 case csc_extern:
6516 case csc_typedef:
6517 break;
6518 default:
6519 gcc_unreachable ();
6522 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6524 if (name)
6525 error ("%qs specified for parameter %qE",
6526 c_addr_space_name (address_space), name);
6527 else
6528 error ("%qs specified for unnamed parameter",
6529 c_addr_space_name (address_space));
6531 else if (decl_context == FIELD)
6533 if (name)
6534 error ("%qs specified for structure field %qE",
6535 c_addr_space_name (address_space), name);
6536 else
6537 error ("%qs specified for structure field",
6538 c_addr_space_name (address_space));
6542 /* Check the type and width of a bit-field. */
6543 if (bitfield)
6545 check_bitfield_type_and_width (loc, &type, width, name);
6546 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6547 atomic types are permitted for bit-fields; we have no code to
6548 make bit-field accesses atomic, so disallow them. */
6549 if (type_quals & TYPE_QUAL_ATOMIC)
6551 if (name)
6552 error_at (loc, "bit-field %qE has atomic type", name);
6553 else
6554 error_at (loc, "bit-field has atomic type");
6555 type_quals &= ~TYPE_QUAL_ATOMIC;
6559 /* Reject invalid uses of _Alignas. */
6560 if (declspecs->alignas_p)
6562 if (storage_class == csc_typedef)
6563 error_at (loc, "alignment specified for typedef %qE", name);
6564 else if (storage_class == csc_register)
6565 error_at (loc, "alignment specified for %<register%> object %qE",
6566 name);
6567 else if (decl_context == PARM)
6569 if (name)
6570 error_at (loc, "alignment specified for parameter %qE", name);
6571 else
6572 error_at (loc, "alignment specified for unnamed parameter");
6574 else if (bitfield)
6576 if (name)
6577 error_at (loc, "alignment specified for bit-field %qE", name);
6578 else
6579 error_at (loc, "alignment specified for unnamed bit-field");
6581 else if (TREE_CODE (type) == FUNCTION_TYPE)
6582 error_at (loc, "alignment specified for function %qE", name);
6583 else if (declspecs->align_log != -1 && TYPE_P (type))
6585 alignas_align = 1U << declspecs->align_log;
6586 if (alignas_align < min_align_of_type (type))
6588 if (name)
6589 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6590 "alignment of %qE", name);
6591 else
6592 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6593 "alignment of unnamed field");
6594 alignas_align = 0;
6599 /* If this is declaring a typedef name, return a TYPE_DECL. */
6601 if (storage_class == csc_typedef)
6603 tree decl;
6604 if ((type_quals & TYPE_QUAL_ATOMIC)
6605 && TREE_CODE (type) == FUNCTION_TYPE)
6607 error_at (loc,
6608 "%<_Atomic%>-qualified function type");
6609 type_quals &= ~TYPE_QUAL_ATOMIC;
6611 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6612 && type_quals)
6613 pedwarn (loc, OPT_Wpedantic,
6614 "ISO C forbids qualified function types");
6615 if (type_quals)
6616 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6617 orig_qual_indirect);
6618 decl = build_decl (declarator->id_loc,
6619 TYPE_DECL, declarator->u.id, type);
6620 if (declspecs->explicit_signed_p)
6621 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6622 if (declspecs->inline_p)
6623 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6624 if (declspecs->noreturn_p)
6625 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6627 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6629 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6631 if (b != NULL
6632 && b->decl != NULL_TREE
6633 && (B_IN_CURRENT_SCOPE (b)
6634 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6635 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6637 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6638 ("using %qD as both a typedef and a tag is "
6639 "invalid in C++"), decl)
6640 && b->locus != UNKNOWN_LOCATION)
6641 inform (b->locus, "originally defined here");
6645 return decl;
6648 /* If this is a type name (such as, in a cast or sizeof),
6649 compute the type and return it now. */
6651 if (decl_context == TYPENAME)
6653 /* Note that the grammar rejects storage classes in typenames
6654 and fields. */
6655 gcc_assert (storage_class == csc_none && !threadp
6656 && !declspecs->inline_p && !declspecs->noreturn_p);
6657 if ((type_quals & TYPE_QUAL_ATOMIC)
6658 && TREE_CODE (type) == FUNCTION_TYPE)
6660 error_at (loc,
6661 "%<_Atomic%>-qualified function type");
6662 type_quals &= ~TYPE_QUAL_ATOMIC;
6664 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6665 && type_quals)
6666 pedwarn (loc, OPT_Wpedantic,
6667 "ISO C forbids const or volatile function types");
6668 if (type_quals)
6669 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6670 orig_qual_indirect);
6671 return type;
6674 if (pedantic && decl_context == FIELD
6675 && variably_modified_type_p (type, NULL_TREE))
6677 /* C99 6.7.2.1p8 */
6678 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6679 "have a variably modified type");
6682 /* Aside from typedefs and type names (handle above),
6683 `void' at top level (not within pointer)
6684 is allowed only in public variables.
6685 We don't complain about parms either, but that is because
6686 a better error message can be made later. */
6688 if (VOID_TYPE_P (type) && decl_context != PARM
6689 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6690 && (storage_class == csc_extern
6691 || (current_scope == file_scope
6692 && !(storage_class == csc_static
6693 || storage_class == csc_register)))))
6695 error_at (loc, "variable or field %qE declared void", name);
6696 type = integer_type_node;
6699 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6700 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6703 tree decl;
6705 if (decl_context == PARM)
6707 tree promoted_type;
6708 bool array_parameter_p = false;
6710 /* A parameter declared as an array of T is really a pointer to T.
6711 One declared as a function is really a pointer to a function. */
6713 if (TREE_CODE (type) == ARRAY_TYPE)
6715 /* Transfer const-ness of array into that of type pointed to. */
6716 type = TREE_TYPE (type);
6717 if (orig_qual_type != NULL_TREE)
6719 if (orig_qual_indirect == 0)
6720 orig_qual_type = TREE_TYPE (orig_qual_type);
6721 else
6722 orig_qual_indirect--;
6724 if (type_quals)
6725 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6726 orig_qual_indirect);
6727 type = c_build_pointer_type (type);
6728 type_quals = array_ptr_quals;
6729 if (type_quals)
6730 type = c_build_qualified_type (type, type_quals);
6732 /* We don't yet implement attributes in this context. */
6733 if (array_ptr_attrs != NULL_TREE)
6734 warning_at (loc, OPT_Wattributes,
6735 "attributes in parameter array declarator ignored");
6737 size_varies = false;
6738 array_parameter_p = true;
6740 else if (TREE_CODE (type) == FUNCTION_TYPE)
6742 if (type_quals & TYPE_QUAL_ATOMIC)
6744 error_at (loc,
6745 "%<_Atomic%>-qualified function type");
6746 type_quals &= ~TYPE_QUAL_ATOMIC;
6748 else if (type_quals)
6749 pedwarn (loc, OPT_Wpedantic,
6750 "ISO C forbids qualified function types");
6751 if (type_quals)
6752 type = c_build_qualified_type (type, type_quals);
6753 type = c_build_pointer_type (type);
6754 type_quals = TYPE_UNQUALIFIED;
6756 else if (type_quals)
6757 type = c_build_qualified_type (type, type_quals);
6759 decl = build_decl (declarator->id_loc,
6760 PARM_DECL, declarator->u.id, type);
6761 if (size_varies)
6762 C_DECL_VARIABLE_SIZE (decl) = 1;
6763 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6765 /* Compute the type actually passed in the parmlist,
6766 for the case where there is no prototype.
6767 (For example, shorts and chars are passed as ints.)
6768 When there is a prototype, this is overridden later. */
6770 if (type == error_mark_node)
6771 promoted_type = type;
6772 else
6773 promoted_type = c_type_promotes_to (type);
6775 DECL_ARG_TYPE (decl) = promoted_type;
6776 if (declspecs->inline_p)
6777 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6778 if (declspecs->noreturn_p)
6779 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6781 else if (decl_context == FIELD)
6783 /* Note that the grammar rejects storage classes in typenames
6784 and fields. */
6785 gcc_assert (storage_class == csc_none && !threadp
6786 && !declspecs->inline_p && !declspecs->noreturn_p);
6788 /* Structure field. It may not be a function. */
6790 if (TREE_CODE (type) == FUNCTION_TYPE)
6792 error_at (loc, "field %qE declared as a function", name);
6793 type = build_pointer_type (type);
6795 else if (TREE_CODE (type) != ERROR_MARK
6796 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6798 if (name)
6799 error_at (loc, "field %qE has incomplete type", name);
6800 else
6801 error_at (loc, "unnamed field has incomplete type");
6802 type = error_mark_node;
6804 else if (TREE_CODE (type) == ARRAY_TYPE
6805 && TYPE_DOMAIN (type) == NULL_TREE)
6807 /* We have a flexible array member through a typedef.
6808 Set suitable range. Whether this is a correct position
6809 for a flexible array member will be determined elsewhere. */
6810 if (!in_system_header_at (input_location))
6811 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6812 "support flexible array members");
6813 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6814 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6815 NULL_TREE);
6816 if (orig_qual_indirect == 0)
6817 orig_qual_type = NULL_TREE;
6819 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6820 orig_qual_indirect);
6821 decl = build_decl (declarator->id_loc,
6822 FIELD_DECL, declarator->u.id, type);
6823 DECL_NONADDRESSABLE_P (decl) = bitfield;
6824 if (bitfield && !declarator->u.id)
6826 TREE_NO_WARNING (decl) = 1;
6827 DECL_PADDING_P (decl) = 1;
6830 if (size_varies)
6831 C_DECL_VARIABLE_SIZE (decl) = 1;
6833 else if (TREE_CODE (type) == FUNCTION_TYPE)
6835 if (storage_class == csc_register || threadp)
6837 error_at (loc, "invalid storage class for function %qE", name);
6839 else if (current_scope != file_scope)
6841 /* Function declaration not at file scope. Storage
6842 classes other than `extern' are not allowed, C99
6843 6.7.1p5, and `extern' makes no difference. However,
6844 GCC allows 'auto', perhaps with 'inline', to support
6845 nested functions. */
6846 if (storage_class == csc_auto)
6847 pedwarn (loc, OPT_Wpedantic,
6848 "invalid storage class for function %qE", name);
6849 else if (storage_class == csc_static)
6851 error_at (loc, "invalid storage class for function %qE", name);
6852 if (funcdef_flag)
6853 storage_class = declspecs->storage_class = csc_none;
6854 else
6855 return NULL_TREE;
6859 decl = build_decl (declarator->id_loc,
6860 FUNCTION_DECL, declarator->u.id, type);
6861 decl = build_decl_attribute_variant (decl, decl_attr);
6863 if (type_quals & TYPE_QUAL_ATOMIC)
6865 error_at (loc,
6866 "%<_Atomic%>-qualified function type");
6867 type_quals &= ~TYPE_QUAL_ATOMIC;
6869 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6870 pedwarn (loc, OPT_Wpedantic,
6871 "ISO C forbids qualified function types");
6873 /* Every function declaration is an external reference
6874 (DECL_EXTERNAL) except for those which are not at file
6875 scope and are explicitly declared "auto". This is
6876 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6877 GCC to signify a forward declaration of a nested function. */
6878 if (storage_class == csc_auto && current_scope != file_scope)
6879 DECL_EXTERNAL (decl) = 0;
6880 /* In C99, a function which is declared 'inline' with 'extern'
6881 is not an external reference (which is confusing). It
6882 means that the later definition of the function must be output
6883 in this file, C99 6.7.4p6. In GNU C89, a function declared
6884 'extern inline' is an external reference. */
6885 else if (declspecs->inline_p && storage_class != csc_static)
6886 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6887 == flag_gnu89_inline);
6888 else
6889 DECL_EXTERNAL (decl) = !initialized;
6891 /* Record absence of global scope for `static' or `auto'. */
6892 TREE_PUBLIC (decl)
6893 = !(storage_class == csc_static || storage_class == csc_auto);
6895 /* For a function definition, record the argument information
6896 block where store_parm_decls will look for it. */
6897 if (funcdef_flag)
6898 current_function_arg_info = arg_info;
6900 if (declspecs->default_int_p)
6901 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6903 /* Record presence of `inline' and `_Noreturn', if it is
6904 reasonable. */
6905 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6907 if (declspecs->inline_p)
6908 pedwarn (loc, 0, "cannot inline function %<main%>");
6909 if (declspecs->noreturn_p)
6910 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6912 else
6914 if (declspecs->inline_p)
6915 /* Record that the function is declared `inline'. */
6916 DECL_DECLARED_INLINE_P (decl) = 1;
6917 if (declspecs->noreturn_p)
6919 if (flag_isoc99)
6920 pedwarn_c99 (loc, OPT_Wpedantic,
6921 "ISO C99 does not support %<_Noreturn%>");
6922 else
6923 pedwarn_c99 (loc, OPT_Wpedantic,
6924 "ISO C90 does not support %<_Noreturn%>");
6925 TREE_THIS_VOLATILE (decl) = 1;
6929 else
6931 /* It's a variable. */
6932 /* An uninitialized decl with `extern' is a reference. */
6933 int extern_ref = !initialized && storage_class == csc_extern;
6935 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6936 orig_qual_indirect);
6938 /* C99 6.2.2p7: It is invalid (compile-time undefined
6939 behavior) to create an 'extern' declaration for a
6940 variable if there is a global declaration that is
6941 'static' and the global declaration is not visible.
6942 (If the static declaration _is_ currently visible,
6943 the 'extern' declaration is taken to refer to that decl.) */
6944 if (extern_ref && current_scope != file_scope)
6946 tree global_decl = identifier_global_value (declarator->u.id);
6947 tree visible_decl = lookup_name (declarator->u.id);
6949 if (global_decl
6950 && global_decl != visible_decl
6951 && VAR_P (global_decl)
6952 && !TREE_PUBLIC (global_decl))
6953 error_at (loc, "variable previously declared %<static%> "
6954 "redeclared %<extern%>");
6957 decl = build_decl (declarator->id_loc,
6958 VAR_DECL, declarator->u.id, type);
6959 if (size_varies)
6960 C_DECL_VARIABLE_SIZE (decl) = 1;
6962 if (declspecs->inline_p)
6963 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6964 if (declspecs->noreturn_p)
6965 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6967 /* At file scope, an initialized extern declaration may follow
6968 a static declaration. In that case, DECL_EXTERNAL will be
6969 reset later in start_decl. */
6970 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6972 /* At file scope, the presence of a `static' or `register' storage
6973 class specifier, or the absence of all storage class specifiers
6974 makes this declaration a definition (perhaps tentative). Also,
6975 the absence of `static' makes it public. */
6976 if (current_scope == file_scope)
6978 TREE_PUBLIC (decl) = storage_class != csc_static;
6979 TREE_STATIC (decl) = !extern_ref;
6981 /* Not at file scope, only `static' makes a static definition. */
6982 else
6984 TREE_STATIC (decl) = (storage_class == csc_static);
6985 TREE_PUBLIC (decl) = extern_ref;
6988 if (threadp)
6989 set_decl_tls_model (decl, decl_default_tls_model (decl));
6992 if ((storage_class == csc_extern
6993 || (storage_class == csc_none
6994 && TREE_CODE (type) == FUNCTION_TYPE
6995 && !funcdef_flag))
6996 && variably_modified_type_p (type, NULL_TREE))
6998 /* C99 6.7.5.2p2 */
6999 if (TREE_CODE (type) == FUNCTION_TYPE)
7000 error_at (loc, "non-nested function with variably modified type");
7001 else
7002 error_at (loc, "object with variably modified type must have "
7003 "no linkage");
7006 /* Record `register' declaration for warnings on &
7007 and in case doing stupid register allocation. */
7009 if (storage_class == csc_register)
7011 C_DECL_REGISTER (decl) = 1;
7012 DECL_REGISTER (decl) = 1;
7015 /* Record constancy and volatility. */
7016 c_apply_type_quals_to_decl (type_quals, decl);
7018 /* Apply _Alignas specifiers. */
7019 if (alignas_align)
7021 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7022 DECL_USER_ALIGN (decl) = 1;
7025 /* If a type has volatile components, it should be stored in memory.
7026 Otherwise, the fact that those components are volatile
7027 will be ignored, and would even crash the compiler.
7028 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7029 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7030 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7031 || TREE_CODE (decl) == RESULT_DECL))
7033 /* It is not an error for a structure with volatile fields to
7034 be declared register, but reset DECL_REGISTER since it
7035 cannot actually go in a register. */
7036 int was_reg = C_DECL_REGISTER (decl);
7037 C_DECL_REGISTER (decl) = 0;
7038 DECL_REGISTER (decl) = 0;
7039 c_mark_addressable (decl);
7040 C_DECL_REGISTER (decl) = was_reg;
7043 /* This is the earliest point at which we might know the assembler
7044 name of a variable. Thus, if it's known before this, die horribly. */
7045 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7046 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7048 if (warn_cxx_compat
7049 && VAR_P (decl)
7050 && TREE_PUBLIC (decl)
7051 && TREE_STATIC (decl)
7052 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7053 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7054 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7055 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7056 ("non-local variable %qD with anonymous type is "
7057 "questionable in C++"),
7058 decl);
7060 return decl;
7064 /* Decode the parameter-list info for a function type or function definition.
7065 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7066 if there is an identifier list instead of a parameter decl list).
7067 These two functions are separate because when a function returns
7068 or receives functions then each is called multiple times but the order
7069 of calls is different. The last call to `grokparms' is always the one
7070 that contains the formal parameter names of a function definition.
7072 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7074 FUNCDEF_FLAG is true for a function definition, false for
7075 a mere declaration. A nonempty identifier-list gets an error message
7076 when FUNCDEF_FLAG is false. */
7078 static tree
7079 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7081 tree arg_types = arg_info->types;
7083 if (funcdef_flag && arg_info->had_vla_unspec)
7085 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7086 /* C99 6.7.5.2p4 */
7087 error ("%<[*]%> not allowed in other than function prototype scope");
7090 if (arg_types == NULL_TREE && !funcdef_flag
7091 && !in_system_header_at (input_location))
7092 warning (OPT_Wstrict_prototypes,
7093 "function declaration isn%'t a prototype");
7095 if (arg_types == error_mark_node)
7096 /* Don't set TYPE_ARG_TYPES in this case. */
7097 return NULL_TREE;
7099 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7101 if (!funcdef_flag)
7103 pedwarn (input_location, 0, "parameter names (without types) in "
7104 "function declaration");
7105 arg_info->parms = NULL_TREE;
7107 else
7108 arg_info->parms = arg_info->types;
7110 arg_info->types = NULL_TREE;
7111 return NULL_TREE;
7113 else
7115 tree parm, type, typelt;
7116 unsigned int parmno;
7118 /* If there is a parameter of incomplete type in a definition,
7119 this is an error. In a declaration this is valid, and a
7120 struct or union type may be completed later, before any calls
7121 or definition of the function. In the case where the tag was
7122 first declared within the parameter list, a warning has
7123 already been given. If a parameter has void type, then
7124 however the function cannot be defined or called, so
7125 warn. */
7127 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7128 parm;
7129 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7131 type = TREE_VALUE (typelt);
7132 if (type == error_mark_node)
7133 continue;
7135 if (!COMPLETE_TYPE_P (type))
7137 if (funcdef_flag)
7139 if (DECL_NAME (parm))
7140 error_at (input_location,
7141 "parameter %u (%q+D) has incomplete type",
7142 parmno, parm);
7143 else
7144 error_at (DECL_SOURCE_LOCATION (parm),
7145 "parameter %u has incomplete type",
7146 parmno);
7148 TREE_VALUE (typelt) = error_mark_node;
7149 TREE_TYPE (parm) = error_mark_node;
7150 arg_types = NULL_TREE;
7152 else if (VOID_TYPE_P (type))
7154 if (DECL_NAME (parm))
7155 warning_at (input_location, 0,
7156 "parameter %u (%q+D) has void type",
7157 parmno, parm);
7158 else
7159 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7160 "parameter %u has void type",
7161 parmno);
7165 if (DECL_NAME (parm) && TREE_USED (parm))
7166 warn_if_shadowing (parm);
7168 return arg_types;
7172 /* Allocate and initialize a c_arg_info structure from the parser's
7173 obstack. */
7175 struct c_arg_info *
7176 build_arg_info (void)
7178 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7179 ret->parms = NULL_TREE;
7180 ret->tags = NULL;
7181 ret->types = NULL_TREE;
7182 ret->others = NULL_TREE;
7183 ret->pending_sizes = NULL;
7184 ret->had_vla_unspec = 0;
7185 return ret;
7188 /* Take apart the current scope and return a c_arg_info structure with
7189 info on a parameter list just parsed.
7191 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7193 ELLIPSIS being true means the argument list ended in '...' so don't
7194 append a sentinel (void_list_node) to the end of the type-list.
7196 EXPR is NULL or an expression that needs to be evaluated for the
7197 side effects of array size expressions in the parameters. */
7199 struct c_arg_info *
7200 get_parm_info (bool ellipsis, tree expr)
7202 struct c_binding *b = current_scope->bindings;
7203 struct c_arg_info *arg_info = build_arg_info ();
7205 tree parms = NULL_TREE;
7206 vec<c_arg_tag, va_gc> *tags = NULL;
7207 tree types = NULL_TREE;
7208 tree others = NULL_TREE;
7210 bool gave_void_only_once_err = false;
7212 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7214 /* The bindings in this scope must not get put into a block.
7215 We will take care of deleting the binding nodes. */
7216 current_scope->bindings = 0;
7218 /* This function is only called if there was *something* on the
7219 parameter list. */
7220 gcc_assert (b);
7222 /* A parameter list consisting solely of 'void' indicates that the
7223 function takes no arguments. But if the 'void' is qualified
7224 (by 'const' or 'volatile'), or has a storage class specifier
7225 ('register'), then the behavior is undefined; issue an error.
7226 Typedefs for 'void' are OK (see DR#157). */
7227 if (b->prev == 0 /* one binding */
7228 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7229 && !DECL_NAME (b->decl) /* anonymous */
7230 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7232 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7233 || C_DECL_REGISTER (b->decl))
7234 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7236 /* There cannot be an ellipsis. */
7237 if (ellipsis)
7238 error_at (b->locus, "%<void%> must be the only parameter");
7240 arg_info->types = void_list_node;
7241 return arg_info;
7244 if (!ellipsis)
7245 types = void_list_node;
7247 /* Break up the bindings list into parms, tags, types, and others;
7248 apply sanity checks; purge the name-to-decl bindings. */
7249 while (b)
7251 tree decl = b->decl;
7252 tree type = TREE_TYPE (decl);
7253 c_arg_tag tag;
7254 const char *keyword;
7256 switch (TREE_CODE (decl))
7258 case PARM_DECL:
7259 if (b->id)
7261 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7262 I_SYMBOL_BINDING (b->id) = b->shadowed;
7265 /* Check for forward decls that never got their actual decl. */
7266 if (TREE_ASM_WRITTEN (decl))
7267 error_at (b->locus,
7268 "parameter %q+D has just a forward declaration", decl);
7269 /* Check for (..., void, ...) and issue an error. */
7270 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7272 if (!gave_void_only_once_err)
7274 error_at (b->locus, "%<void%> must be the only parameter");
7275 gave_void_only_once_err = true;
7278 else
7280 /* Valid parameter, add it to the list. */
7281 DECL_CHAIN (decl) = parms;
7282 parms = decl;
7284 /* Since there is a prototype, args are passed in their
7285 declared types. The back end may override this later. */
7286 DECL_ARG_TYPE (decl) = type;
7287 types = tree_cons (0, type, types);
7289 break;
7291 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7292 case UNION_TYPE: keyword = "union"; goto tag;
7293 case RECORD_TYPE: keyword = "struct"; goto tag;
7294 tag:
7295 /* Types may not have tag-names, in which case the type
7296 appears in the bindings list with b->id NULL. */
7297 if (b->id)
7299 gcc_assert (I_TAG_BINDING (b->id) == b);
7300 I_TAG_BINDING (b->id) = b->shadowed;
7303 /* Warn about any struct, union or enum tags defined in a
7304 parameter list. The scope of such types is limited to
7305 the parameter list, which is rarely if ever desirable
7306 (it's impossible to call such a function with type-
7307 correct arguments). An anonymous union parm type is
7308 meaningful as a GNU extension, so don't warn for that. */
7309 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7311 if (b->id)
7312 /* The %s will be one of 'struct', 'union', or 'enum'. */
7313 warning_at (b->locus, 0,
7314 "%<%s %E%> declared inside parameter list"
7315 " will not be visible outside of this definition or"
7316 " declaration", keyword, b->id);
7317 else
7318 /* The %s will be one of 'struct', 'union', or 'enum'. */
7319 warning_at (b->locus, 0,
7320 "anonymous %s declared inside parameter list"
7321 " will not be visible outside of this definition or"
7322 " declaration", keyword);
7325 tag.id = b->id;
7326 tag.type = decl;
7327 vec_safe_push (tags, tag);
7328 break;
7330 case FUNCTION_DECL:
7331 /* FUNCTION_DECLs appear when there is an implicit function
7332 declaration in the parameter list. */
7333 gcc_assert (b->nested || seen_error ());
7334 goto set_shadowed;
7336 case CONST_DECL:
7337 case TYPE_DECL:
7338 /* CONST_DECLs appear here when we have an embedded enum,
7339 and TYPE_DECLs appear here when we have an embedded struct
7340 or union. No warnings for this - we already warned about the
7341 type itself. */
7343 /* When we reinsert this decl in the function body, we need
7344 to reconstruct whether it was marked as nested. */
7345 gcc_assert (!b->nested);
7346 DECL_CHAIN (decl) = others;
7347 others = decl;
7348 /* fall through */
7350 case ERROR_MARK:
7351 set_shadowed:
7352 /* error_mark_node appears here when we have an undeclared
7353 variable. Just throw it away. */
7354 if (b->id)
7356 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7357 I_SYMBOL_BINDING (b->id) = b->shadowed;
7359 break;
7361 /* Other things that might be encountered. */
7362 case LABEL_DECL:
7363 case VAR_DECL:
7364 default:
7365 gcc_unreachable ();
7368 b = free_binding_and_advance (b);
7371 arg_info->parms = parms;
7372 arg_info->tags = tags;
7373 arg_info->types = types;
7374 arg_info->others = others;
7375 arg_info->pending_sizes = expr;
7376 return arg_info;
7379 /* Get the struct, enum or union (CODE says which) with tag NAME.
7380 Define the tag as a forward-reference with location LOC if it is
7381 not defined. Return a c_typespec structure for the type
7382 specifier. */
7384 struct c_typespec
7385 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7387 struct c_typespec ret;
7388 tree ref;
7389 location_t refloc;
7391 ret.expr = NULL_TREE;
7392 ret.expr_const_operands = true;
7394 /* If a cross reference is requested, look up the type
7395 already defined for this tag and return it. */
7397 ref = lookup_tag (code, name, false, &refloc);
7398 /* If this is the right type of tag, return what we found.
7399 (This reference will be shadowed by shadow_tag later if appropriate.)
7400 If this is the wrong type of tag, do not return it. If it was the
7401 wrong type in the same scope, we will have had an error
7402 message already; if in a different scope and declaring
7403 a name, pending_xref_error will give an error message; but if in a
7404 different scope and not declaring a name, this tag should
7405 shadow the previous declaration of a different type of tag, and
7406 this would not work properly if we return the reference found.
7407 (For example, with "struct foo" in an outer scope, "union foo;"
7408 must shadow that tag with a new one of union type.) */
7409 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7410 if (ref && TREE_CODE (ref) == code)
7412 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7413 && loc != UNKNOWN_LOCATION
7414 && warn_cxx_compat)
7416 switch (code)
7418 case ENUMERAL_TYPE:
7419 warning_at (loc, OPT_Wc___compat,
7420 ("enum type defined in struct or union "
7421 "is not visible in C++"));
7422 inform (refloc, "enum type defined here");
7423 break;
7424 case RECORD_TYPE:
7425 warning_at (loc, OPT_Wc___compat,
7426 ("struct defined in struct or union "
7427 "is not visible in C++"));
7428 inform (refloc, "struct defined here");
7429 break;
7430 case UNION_TYPE:
7431 warning_at (loc, OPT_Wc___compat,
7432 ("union defined in struct or union "
7433 "is not visible in C++"));
7434 inform (refloc, "union defined here");
7435 break;
7436 default:
7437 gcc_unreachable();
7441 ret.spec = ref;
7442 return ret;
7445 /* If no such tag is yet defined, create a forward-reference node
7446 and record it as the "definition".
7447 When a real declaration of this type is found,
7448 the forward-reference will be altered into a real type. */
7450 ref = make_node (code);
7451 if (code == ENUMERAL_TYPE)
7453 /* Give the type a default layout like unsigned int
7454 to avoid crashing if it does not get defined. */
7455 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7456 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7457 TYPE_USER_ALIGN (ref) = 0;
7458 TYPE_UNSIGNED (ref) = 1;
7459 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7460 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7461 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7464 pushtag (loc, name, ref);
7466 ret.spec = ref;
7467 return ret;
7470 /* Get the struct, enum or union (CODE says which) with tag NAME.
7471 Define the tag as a forward-reference if it is not defined.
7472 Return a tree for the type. */
7474 tree
7475 xref_tag (enum tree_code code, tree name)
7477 return parser_xref_tag (input_location, code, name).spec;
7480 /* Make sure that the tag NAME is defined *in the current scope*
7481 at least as a forward reference.
7482 LOC is the location of the struct's definition.
7483 CODE says which kind of tag NAME ought to be.
7485 This stores the current value of the file static STRUCT_PARSE_INFO
7486 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7487 new c_struct_parse_info structure. The old value of
7488 STRUCT_PARSE_INFO is restored in finish_struct. */
7490 tree
7491 start_struct (location_t loc, enum tree_code code, tree name,
7492 struct c_struct_parse_info **enclosing_struct_parse_info)
7494 /* If there is already a tag defined at this scope
7495 (as a forward reference), just return it. */
7497 tree ref = NULL_TREE;
7498 location_t refloc = UNKNOWN_LOCATION;
7500 if (name != NULL_TREE)
7501 ref = lookup_tag (code, name, true, &refloc);
7502 if (ref && TREE_CODE (ref) == code)
7504 if (TYPE_STUB_DECL (ref))
7505 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7507 if (TYPE_SIZE (ref))
7509 if (code == UNION_TYPE)
7510 error_at (loc, "redefinition of %<union %E%>", name);
7511 else
7512 error_at (loc, "redefinition of %<struct %E%>", name);
7513 if (refloc != UNKNOWN_LOCATION)
7514 inform (refloc, "originally defined here");
7515 /* Don't create structures using a name already in use. */
7516 ref = NULL_TREE;
7518 else if (C_TYPE_BEING_DEFINED (ref))
7520 if (code == UNION_TYPE)
7521 error_at (loc, "nested redefinition of %<union %E%>", name);
7522 else
7523 error_at (loc, "nested redefinition of %<struct %E%>", name);
7524 /* Don't bother to report "originally defined here" for a
7525 nested redefinition; the original definition should be
7526 obvious. */
7527 /* Don't create structures that contain themselves. */
7528 ref = NULL_TREE;
7532 /* Otherwise create a forward-reference just so the tag is in scope. */
7534 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7536 ref = make_node (code);
7537 pushtag (loc, name, ref);
7540 C_TYPE_BEING_DEFINED (ref) = 1;
7541 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7542 TYPE_PACKED (v) = flag_pack_struct;
7544 *enclosing_struct_parse_info = struct_parse_info;
7545 struct_parse_info = new c_struct_parse_info ();
7547 /* FIXME: This will issue a warning for a use of a type defined
7548 within a statement expr used within sizeof, et. al. This is not
7549 terribly serious as C++ doesn't permit statement exprs within
7550 sizeof anyhow. */
7551 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7552 warning_at (loc, OPT_Wc___compat,
7553 "defining type in %qs expression is invalid in C++",
7554 (in_sizeof
7555 ? "sizeof"
7556 : (in_typeof ? "typeof" : "alignof")));
7558 return ref;
7561 /* Process the specs, declarator and width (NULL if omitted)
7562 of a structure component, returning a FIELD_DECL node.
7563 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7564 DECL_ATTRS is as for grokdeclarator.
7566 LOC is the location of the structure component.
7568 This is done during the parsing of the struct declaration.
7569 The FIELD_DECL nodes are chained together and the lot of them
7570 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7572 tree
7573 grokfield (location_t loc,
7574 struct c_declarator *declarator, struct c_declspecs *declspecs,
7575 tree width, tree *decl_attrs)
7577 tree value;
7579 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7580 && width == NULL_TREE)
7582 /* This is an unnamed decl.
7584 If we have something of the form "union { list } ;" then this
7585 is the anonymous union extension. Similarly for struct.
7587 If this is something of the form "struct foo;", then
7588 If MS or Plan 9 extensions are enabled, this is handled as
7589 an anonymous struct.
7590 Otherwise this is a forward declaration of a structure tag.
7592 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7593 If foo names a structure or union without a tag, then this
7594 is an anonymous struct (this is permitted by C11).
7595 If MS or Plan 9 extensions are enabled and foo names a
7596 structure, then again this is an anonymous struct.
7597 Otherwise this is an error.
7599 Oh what a horrid tangled web we weave. I wonder if MS consciously
7600 took this from Plan 9 or if it was an accident of implementation
7601 that took root before someone noticed the bug... */
7603 tree type = declspecs->type;
7604 bool ok = false;
7606 if (RECORD_OR_UNION_TYPE_P (type)
7607 && (flag_ms_extensions
7608 || flag_plan9_extensions
7609 || !declspecs->typedef_p))
7611 if (flag_ms_extensions || flag_plan9_extensions)
7612 ok = true;
7613 else if (TYPE_NAME (type) == NULL)
7614 ok = true;
7615 else
7616 ok = false;
7618 if (!ok)
7620 pedwarn (loc, 0, "declaration does not declare anything");
7621 return NULL_TREE;
7623 if (flag_isoc99)
7624 pedwarn_c99 (loc, OPT_Wpedantic,
7625 "ISO C99 doesn%'t support unnamed structs/unions");
7626 else
7627 pedwarn_c99 (loc, OPT_Wpedantic,
7628 "ISO C90 doesn%'t support unnamed structs/unions");
7631 value = grokdeclarator (declarator, declspecs, FIELD, false,
7632 width ? &width : NULL, decl_attrs, NULL, NULL,
7633 DEPRECATED_NORMAL);
7635 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7636 DECL_INITIAL (value) = width;
7637 if (width)
7638 SET_DECL_C_BIT_FIELD (value);
7640 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7642 /* If we currently have a binding for this field, set the
7643 in_struct field in the binding, so that we warn about lookups
7644 which find it. */
7645 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7646 if (b != NULL)
7648 /* If the in_struct field is not yet set, push it on a list
7649 to be cleared when this struct is finished. */
7650 if (!b->in_struct)
7652 struct_parse_info->fields.safe_push (b);
7653 b->in_struct = 1;
7658 return value;
7661 /* Subroutine of detect_field_duplicates: return whether X and Y,
7662 which are both fields in the same struct, have duplicate field
7663 names. */
7665 static bool
7666 is_duplicate_field (tree x, tree y)
7668 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7669 return true;
7671 /* When using -fplan9-extensions, an anonymous field whose name is a
7672 typedef can duplicate a field name. */
7673 if (flag_plan9_extensions
7674 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7676 tree xt, xn, yt, yn;
7678 xt = TREE_TYPE (x);
7679 if (DECL_NAME (x) != NULL_TREE)
7680 xn = DECL_NAME (x);
7681 else if (RECORD_OR_UNION_TYPE_P (xt)
7682 && TYPE_NAME (xt) != NULL_TREE
7683 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7684 xn = DECL_NAME (TYPE_NAME (xt));
7685 else
7686 xn = NULL_TREE;
7688 yt = TREE_TYPE (y);
7689 if (DECL_NAME (y) != NULL_TREE)
7690 yn = DECL_NAME (y);
7691 else if (RECORD_OR_UNION_TYPE_P (yt)
7692 && TYPE_NAME (yt) != NULL_TREE
7693 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7694 yn = DECL_NAME (TYPE_NAME (yt));
7695 else
7696 yn = NULL_TREE;
7698 if (xn != NULL_TREE && xn == yn)
7699 return true;
7702 return false;
7705 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7706 to HTAB, giving errors for any duplicates. */
7708 static void
7709 detect_field_duplicates_hash (tree fieldlist,
7710 hash_table<nofree_ptr_hash <tree_node> > *htab)
7712 tree x, y;
7713 tree_node **slot;
7715 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7716 if ((y = DECL_NAME (x)) != NULL_TREE)
7718 slot = htab->find_slot (y, INSERT);
7719 if (*slot)
7721 error ("duplicate member %q+D", x);
7722 DECL_NAME (x) = NULL_TREE;
7724 *slot = y;
7726 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7728 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7730 /* When using -fplan9-extensions, an anonymous field whose
7731 name is a typedef can duplicate a field name. */
7732 if (flag_plan9_extensions
7733 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7734 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7736 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7737 slot = htab->find_slot (xn, INSERT);
7738 if (*slot)
7739 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7740 *slot = xn;
7745 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7746 the list such that this does not present a problem later. */
7748 static void
7749 detect_field_duplicates (tree fieldlist)
7751 tree x, y;
7752 int timeout = 10;
7754 /* If the struct is the list of instance variables of an Objective-C
7755 class, then we need to check all the instance variables of
7756 superclasses when checking for duplicates (since you can't have
7757 an instance variable in a subclass with the same name as an
7758 instance variable in a superclass). We pass on this job to the
7759 Objective-C compiler. objc_detect_field_duplicates() will return
7760 false if we are not checking the list of instance variables and
7761 the C frontend should proceed with the standard field duplicate
7762 checks. If we are checking the list of instance variables, the
7763 ObjC frontend will do the check, emit the errors if needed, and
7764 then return true. */
7765 if (c_dialect_objc ())
7766 if (objc_detect_field_duplicates (false))
7767 return;
7769 /* First, see if there are more than "a few" fields.
7770 This is trivially true if there are zero or one fields. */
7771 if (!fieldlist || !DECL_CHAIN (fieldlist))
7772 return;
7773 x = fieldlist;
7774 do {
7775 timeout--;
7776 if (DECL_NAME (x) == NULL_TREE
7777 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7778 timeout = 0;
7779 x = DECL_CHAIN (x);
7780 } while (timeout > 0 && x);
7782 /* If there were "few" fields and no anonymous structures or unions,
7783 avoid the overhead of allocating a hash table. Instead just do
7784 the nested traversal thing. */
7785 if (timeout > 0)
7787 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7788 /* When using -fplan9-extensions, we can have duplicates
7789 between typedef names and fields. */
7790 if (DECL_NAME (x)
7791 || (flag_plan9_extensions
7792 && DECL_NAME (x) == NULL_TREE
7793 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7794 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7795 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7797 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7798 if (is_duplicate_field (y, x))
7800 error ("duplicate member %q+D", x);
7801 DECL_NAME (x) = NULL_TREE;
7805 else
7807 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7808 detect_field_duplicates_hash (fieldlist, &htab);
7812 /* Finish up struct info used by -Wc++-compat. */
7814 static void
7815 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7816 location_t record_loc)
7818 unsigned int ix;
7819 tree x;
7820 struct c_binding *b;
7822 if (fieldlist == NULL_TREE)
7824 if (code == RECORD_TYPE)
7825 warning_at (record_loc, OPT_Wc___compat,
7826 "empty struct has size 0 in C, size 1 in C++");
7827 else
7828 warning_at (record_loc, OPT_Wc___compat,
7829 "empty union has size 0 in C, size 1 in C++");
7832 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7833 the current struct. We do this now at the end of the struct
7834 because the flag is used to issue visibility warnings, and we
7835 only want to issue those warnings if the type is referenced
7836 outside of the struct declaration. */
7837 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7838 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7840 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7841 typedefs used when declaring fields in this struct. If the name
7842 of any of the fields is also a typedef name then the struct would
7843 not parse in C++, because the C++ lookup rules say that the
7844 typedef name would be looked up in the context of the struct, and
7845 would thus be the field rather than the typedef. */
7846 if (!struct_parse_info->typedefs_seen.is_empty ()
7847 && fieldlist != NULL_TREE)
7849 /* Use a hash_set<tree> using the name of the typedef. We can use
7850 a hash_set<tree> because identifiers are interned. */
7851 hash_set<tree> tset;
7853 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7854 tset.add (DECL_NAME (x));
7856 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7858 if (DECL_NAME (x) != NULL_TREE
7859 && tset.contains (DECL_NAME (x)))
7861 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7862 ("using %qD as both field and typedef name is "
7863 "invalid in C++"),
7865 /* FIXME: It would be nice to report the location where
7866 the typedef name is used. */
7871 /* For each field which has a binding and which was not defined in
7872 an enclosing struct, clear the in_struct field. */
7873 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7874 b->in_struct = 0;
7877 /* Function to help qsort sort FIELD_DECLs by name order. */
7879 static int
7880 field_decl_cmp (const void *x_p, const void *y_p)
7882 const tree *const x = (const tree *) x_p;
7883 const tree *const y = (const tree *) y_p;
7885 if (DECL_NAME (*x) == DECL_NAME (*y))
7886 /* A nontype is "greater" than a type. */
7887 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7888 if (DECL_NAME (*x) == NULL_TREE)
7889 return -1;
7890 if (DECL_NAME (*y) == NULL_TREE)
7891 return 1;
7892 if (DECL_NAME (*x) < DECL_NAME (*y))
7893 return -1;
7894 return 1;
7897 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7898 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7899 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7900 ATTRIBUTES are attributes to be applied to the structure.
7902 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7903 the struct was started. */
7905 tree
7906 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7907 struct c_struct_parse_info *enclosing_struct_parse_info)
7909 tree x;
7910 bool toplevel = file_scope == current_scope;
7912 /* If this type was previously laid out as a forward reference,
7913 make sure we lay it out again. */
7915 TYPE_SIZE (t) = NULL_TREE;
7917 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7919 if (pedantic)
7921 for (x = fieldlist; x; x = DECL_CHAIN (x))
7923 if (DECL_NAME (x) != NULL_TREE)
7924 break;
7925 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7926 break;
7929 if (x == NULL_TREE)
7931 if (TREE_CODE (t) == UNION_TYPE)
7933 if (fieldlist)
7934 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7935 else
7936 pedwarn (loc, OPT_Wpedantic, "union has no members");
7938 else
7940 if (fieldlist)
7941 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7942 else
7943 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7948 /* Install struct as DECL_CONTEXT of each field decl.
7949 Also process specified field sizes, found in the DECL_INITIAL,
7950 storing 0 there after the type has been changed to precision equal
7951 to its width, rather than the precision of the specified standard
7952 type. (Correct layout requires the original type to have been preserved
7953 until now.) */
7955 bool saw_named_field = false;
7956 for (x = fieldlist; x; x = DECL_CHAIN (x))
7958 if (TREE_TYPE (x) == error_mark_node)
7959 continue;
7961 DECL_CONTEXT (x) = t;
7963 /* If any field is const, the structure type is pseudo-const. */
7964 if (TREE_READONLY (x))
7965 C_TYPE_FIELDS_READONLY (t) = 1;
7966 else
7968 /* A field that is pseudo-const makes the structure likewise. */
7969 tree t1 = strip_array_types (TREE_TYPE (x));
7970 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7971 C_TYPE_FIELDS_READONLY (t) = 1;
7974 /* Any field that is volatile means variables of this type must be
7975 treated in some ways as volatile. */
7976 if (TREE_THIS_VOLATILE (x))
7977 C_TYPE_FIELDS_VOLATILE (t) = 1;
7979 /* Any field of nominal variable size implies structure is too. */
7980 if (C_DECL_VARIABLE_SIZE (x))
7981 C_TYPE_VARIABLE_SIZE (t) = 1;
7983 if (DECL_C_BIT_FIELD (x))
7985 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7986 DECL_SIZE (x) = bitsize_int (width);
7987 DECL_BIT_FIELD (x) = 1;
7990 if (TYPE_PACKED (t)
7991 && (DECL_BIT_FIELD (x)
7992 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7993 DECL_PACKED (x) = 1;
7995 /* Detect flexible array member in an invalid context. */
7996 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7997 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7998 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7999 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8001 if (TREE_CODE (t) == UNION_TYPE)
8003 error_at (DECL_SOURCE_LOCATION (x),
8004 "flexible array member in union");
8005 TREE_TYPE (x) = error_mark_node;
8007 else if (DECL_CHAIN (x) != NULL_TREE)
8009 error_at (DECL_SOURCE_LOCATION (x),
8010 "flexible array member not at end of struct");
8011 TREE_TYPE (x) = error_mark_node;
8013 else if (!saw_named_field)
8015 error_at (DECL_SOURCE_LOCATION (x),
8016 "flexible array member in a struct with no named "
8017 "members");
8018 TREE_TYPE (x) = error_mark_node;
8022 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8023 && flexible_array_type_p (TREE_TYPE (x)))
8024 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8025 "invalid use of structure with flexible array member");
8027 if (DECL_NAME (x)
8028 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8029 saw_named_field = true;
8032 detect_field_duplicates (fieldlist);
8034 /* Now we have the nearly final fieldlist. Record it,
8035 then lay out the structure or union (including the fields). */
8037 TYPE_FIELDS (t) = fieldlist;
8039 maybe_apply_pragma_scalar_storage_order (t);
8041 layout_type (t);
8043 if (TYPE_SIZE_UNIT (t)
8044 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8045 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8046 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8047 error ("type %qT is too large", t);
8049 /* Give bit-fields their proper types and rewrite the type of array fields
8050 with scalar component if the enclosing type has reverse storage order. */
8051 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8053 if (TREE_CODE (field) == FIELD_DECL
8054 && DECL_INITIAL (field)
8055 && TREE_TYPE (field) != error_mark_node)
8057 unsigned HOST_WIDE_INT width
8058 = tree_to_uhwi (DECL_INITIAL (field));
8059 tree type = TREE_TYPE (field);
8060 if (width != TYPE_PRECISION (type))
8062 TREE_TYPE (field)
8063 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8064 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8066 DECL_INITIAL (field) = NULL_TREE;
8068 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8069 && TREE_CODE (field) == FIELD_DECL
8070 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8072 tree ftype = TREE_TYPE (field);
8073 tree ctype = strip_array_types (ftype);
8074 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8076 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8077 tree *typep = &fmain_type;
8078 do {
8079 *typep = build_distinct_type_copy (*typep);
8080 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8081 typep = &TREE_TYPE (*typep);
8082 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8083 TREE_TYPE (field)
8084 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8089 /* Now we have the truly final field list.
8090 Store it in this type and in the variants. */
8092 TYPE_FIELDS (t) = fieldlist;
8094 /* If there are lots of fields, sort so we can look through them fast.
8095 We arbitrarily consider 16 or more elts to be "a lot". */
8098 int len = 0;
8100 for (x = fieldlist; x; x = DECL_CHAIN (x))
8102 if (len > 15 || DECL_NAME (x) == NULL)
8103 break;
8104 len += 1;
8107 if (len > 15)
8109 tree *field_array;
8110 struct lang_type *space;
8111 struct sorted_fields_type *space2;
8113 len += list_length (x);
8115 /* Use the same allocation policy here that make_node uses, to
8116 ensure that this lives as long as the rest of the struct decl.
8117 All decls in an inline function need to be saved. */
8119 space = ggc_cleared_alloc<struct lang_type> ();
8120 space2 = (sorted_fields_type *) ggc_internal_alloc
8121 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8123 len = 0;
8124 space->s = space2;
8125 field_array = &space2->elts[0];
8126 for (x = fieldlist; x; x = DECL_CHAIN (x))
8128 field_array[len++] = x;
8130 /* If there is anonymous struct or union, break out of the loop. */
8131 if (DECL_NAME (x) == NULL)
8132 break;
8134 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8135 if (x == NULL)
8137 TYPE_LANG_SPECIFIC (t) = space;
8138 TYPE_LANG_SPECIFIC (t)->s->len = len;
8139 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8140 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8145 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8146 in dwarf2out via rest_of_decl_compilation below and means
8147 something totally different. Since we will be clearing
8148 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8149 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8150 C_TYPE_INCOMPLETE_VARS should use some language specific
8151 node. */
8152 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8153 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8155 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8156 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8157 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8158 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8159 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8160 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8163 /* If this was supposed to be a transparent union, but we can't
8164 make it one, warn and turn off the flag. */
8165 if (TREE_CODE (t) == UNION_TYPE
8166 && TYPE_TRANSPARENT_AGGR (t)
8167 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8169 TYPE_TRANSPARENT_AGGR (t) = 0;
8170 warning_at (loc, 0, "union cannot be made transparent");
8173 /* If this structure or union completes the type of any previous
8174 variable declaration, lay it out and output its rtl. */
8175 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8177 tree decl = TREE_VALUE (x);
8178 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8179 layout_array_type (TREE_TYPE (decl));
8180 if (TREE_CODE (decl) != TYPE_DECL)
8182 layout_decl (decl, 0);
8183 if (c_dialect_objc ())
8184 objc_check_decl (decl);
8185 rest_of_decl_compilation (decl, toplevel, 0);
8189 /* Update type location to the one of the definition, instead of e.g.
8190 a forward declaration. */
8191 if (TYPE_STUB_DECL (t))
8192 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8194 /* Finish debugging output for this type. */
8195 rest_of_type_compilation (t, toplevel);
8197 /* If we're inside a function proper, i.e. not file-scope and not still
8198 parsing parameters, then arrange for the size of a variable sized type
8199 to be bound now. */
8200 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8201 add_stmt (build_stmt (loc,
8202 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8204 if (warn_cxx_compat)
8205 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8207 delete struct_parse_info;
8209 struct_parse_info = enclosing_struct_parse_info;
8211 /* If this struct is defined inside a struct, add it to
8212 struct_types. */
8213 if (warn_cxx_compat
8214 && struct_parse_info != NULL
8215 && !in_sizeof && !in_typeof && !in_alignof)
8216 struct_parse_info->struct_types.safe_push (t);
8218 return t;
8221 static struct {
8222 gt_pointer_operator new_value;
8223 void *cookie;
8224 } resort_data;
8226 /* This routine compares two fields like field_decl_cmp but using the
8227 pointer operator in resort_data. */
8229 static int
8230 resort_field_decl_cmp (const void *x_p, const void *y_p)
8232 const tree *const x = (const tree *) x_p;
8233 const tree *const y = (const tree *) y_p;
8235 if (DECL_NAME (*x) == DECL_NAME (*y))
8236 /* A nontype is "greater" than a type. */
8237 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8238 if (DECL_NAME (*x) == NULL_TREE)
8239 return -1;
8240 if (DECL_NAME (*y) == NULL_TREE)
8241 return 1;
8243 tree d1 = DECL_NAME (*x);
8244 tree d2 = DECL_NAME (*y);
8245 resort_data.new_value (&d1, resort_data.cookie);
8246 resort_data.new_value (&d2, resort_data.cookie);
8247 if (d1 < d2)
8248 return -1;
8250 return 1;
8253 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8255 void
8256 resort_sorted_fields (void *obj,
8257 void * ARG_UNUSED (orig_obj),
8258 gt_pointer_operator new_value,
8259 void *cookie)
8261 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8262 resort_data.new_value = new_value;
8263 resort_data.cookie = cookie;
8264 qsort (&sf->elts[0], sf->len, sizeof (tree),
8265 resort_field_decl_cmp);
8268 /* Lay out the type T, and its element type, and so on. */
8270 static void
8271 layout_array_type (tree t)
8273 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8274 layout_array_type (TREE_TYPE (t));
8275 layout_type (t);
8278 /* Begin compiling the definition of an enumeration type.
8279 NAME is its name (or null if anonymous).
8280 LOC is the enum's location.
8281 Returns the type object, as yet incomplete.
8282 Also records info about it so that build_enumerator
8283 may be used to declare the individual values as they are read. */
8285 tree
8286 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8288 tree enumtype = NULL_TREE;
8289 location_t enumloc = UNKNOWN_LOCATION;
8291 /* If this is the real definition for a previous forward reference,
8292 fill in the contents in the same object that used to be the
8293 forward reference. */
8295 if (name != NULL_TREE)
8296 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8298 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8300 enumtype = make_node (ENUMERAL_TYPE);
8301 pushtag (loc, name, enumtype);
8303 /* Update type location to the one of the definition, instead of e.g.
8304 a forward declaration. */
8305 else if (TYPE_STUB_DECL (enumtype))
8307 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8308 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8311 if (C_TYPE_BEING_DEFINED (enumtype))
8312 error_at (loc, "nested redefinition of %<enum %E%>", name);
8314 C_TYPE_BEING_DEFINED (enumtype) = 1;
8316 if (TYPE_VALUES (enumtype) != NULL_TREE)
8318 /* This enum is a named one that has been declared already. */
8319 error_at (loc, "redeclaration of %<enum %E%>", name);
8320 if (enumloc != UNKNOWN_LOCATION)
8321 inform (enumloc, "originally defined here");
8323 /* Completely replace its old definition.
8324 The old enumerators remain defined, however. */
8325 TYPE_VALUES (enumtype) = NULL_TREE;
8328 the_enum->enum_next_value = integer_zero_node;
8329 the_enum->enum_overflow = 0;
8331 if (flag_short_enums)
8332 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8333 TYPE_PACKED (v) = 1;
8335 /* FIXME: This will issue a warning for a use of a type defined
8336 within sizeof in a statement expr. This is not terribly serious
8337 as C++ doesn't permit statement exprs within sizeof anyhow. */
8338 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8339 warning_at (loc, OPT_Wc___compat,
8340 "defining type in %qs expression is invalid in C++",
8341 (in_sizeof
8342 ? "sizeof"
8343 : (in_typeof ? "typeof" : "alignof")));
8345 return enumtype;
8348 /* After processing and defining all the values of an enumeration type,
8349 install their decls in the enumeration type and finish it off.
8350 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8351 and ATTRIBUTES are the specified attributes.
8352 Returns ENUMTYPE. */
8354 tree
8355 finish_enum (tree enumtype, tree values, tree attributes)
8357 tree pair, tem;
8358 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8359 int precision;
8360 signop sign;
8361 bool toplevel = (file_scope == current_scope);
8362 struct lang_type *lt;
8364 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8366 /* Calculate the maximum value of any enumerator in this type. */
8368 if (values == error_mark_node)
8369 minnode = maxnode = integer_zero_node;
8370 else
8372 minnode = maxnode = TREE_VALUE (values);
8373 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8375 tree value = TREE_VALUE (pair);
8376 if (tree_int_cst_lt (maxnode, value))
8377 maxnode = value;
8378 if (tree_int_cst_lt (value, minnode))
8379 minnode = value;
8383 /* Construct the final type of this enumeration. It is the same
8384 as one of the integral types - the narrowest one that fits, except
8385 that normally we only go as narrow as int - and signed iff any of
8386 the values are negative. */
8387 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8388 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8389 tree_int_cst_min_precision (maxnode, sign));
8391 /* If the precision of the type was specified with an attribute and it
8392 was too small, give an error. Otherwise, use it. */
8393 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8395 if (precision > TYPE_PRECISION (enumtype))
8397 TYPE_PRECISION (enumtype) = 0;
8398 error ("specified mode too small for enumeral values");
8400 else
8401 precision = TYPE_PRECISION (enumtype);
8403 else
8404 TYPE_PRECISION (enumtype) = 0;
8406 if (TYPE_PACKED (enumtype)
8407 || precision > TYPE_PRECISION (integer_type_node)
8408 || TYPE_PRECISION (enumtype))
8410 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8411 if (tem == NULL)
8413 warning (0, "enumeration values exceed range of largest integer");
8414 tem = long_long_integer_type_node;
8417 else
8418 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8420 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8421 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8422 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8423 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8424 TYPE_SIZE (enumtype) = NULL_TREE;
8425 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8427 layout_type (enumtype);
8429 if (values != error_mark_node)
8431 /* Change the type of the enumerators to be the enum type. We
8432 need to do this irrespective of the size of the enum, for
8433 proper type checking. Replace the DECL_INITIALs of the
8434 enumerators, and the value slots of the list, with copies
8435 that have the enum type; they cannot be modified in place
8436 because they may be shared (e.g. integer_zero_node) Finally,
8437 change the purpose slots to point to the names of the decls. */
8438 for (pair = values; pair; pair = TREE_CHAIN (pair))
8440 tree enu = TREE_PURPOSE (pair);
8441 tree ini = DECL_INITIAL (enu);
8443 TREE_TYPE (enu) = enumtype;
8445 /* The ISO C Standard mandates enumerators to have type int,
8446 even though the underlying type of an enum type is
8447 unspecified. However, GCC allows enumerators of any
8448 integer type as an extensions. build_enumerator()
8449 converts any enumerators that fit in an int to type int,
8450 to avoid promotions to unsigned types when comparing
8451 integers with enumerators that fit in the int range.
8452 When -pedantic is given, build_enumerator() would have
8453 already warned about those that don't fit. Here we
8454 convert the rest to the enumerator type. */
8455 if (TREE_TYPE (ini) != integer_type_node)
8456 ini = convert (enumtype, ini);
8458 DECL_INITIAL (enu) = ini;
8459 TREE_PURPOSE (pair) = DECL_NAME (enu);
8460 TREE_VALUE (pair) = ini;
8463 TYPE_VALUES (enumtype) = values;
8466 /* Record the min/max values so that we can warn about bit-field
8467 enumerations that are too small for the values. */
8468 lt = ggc_cleared_alloc<struct lang_type> ();
8469 lt->enum_min = minnode;
8470 lt->enum_max = maxnode;
8471 TYPE_LANG_SPECIFIC (enumtype) = lt;
8473 /* Fix up all variant types of this enum type. */
8474 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8476 if (tem == enumtype)
8477 continue;
8478 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8479 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8480 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8481 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8482 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8483 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8484 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8485 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8486 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8487 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8488 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8491 /* Finish debugging output for this type. */
8492 rest_of_type_compilation (enumtype, toplevel);
8494 /* If this enum is defined inside a struct, add it to
8495 struct_types. */
8496 if (warn_cxx_compat
8497 && struct_parse_info != NULL
8498 && !in_sizeof && !in_typeof && !in_alignof)
8499 struct_parse_info->struct_types.safe_push (enumtype);
8501 return enumtype;
8504 /* Build and install a CONST_DECL for one value of the
8505 current enumeration type (one that was begun with start_enum).
8506 DECL_LOC is the location of the enumerator.
8507 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8508 Return a tree-list containing the CONST_DECL and its value.
8509 Assignment of sequential values by default is handled here. */
8511 tree
8512 build_enumerator (location_t decl_loc, location_t loc,
8513 struct c_enum_contents *the_enum, tree name, tree value)
8515 tree decl, type;
8517 /* Validate and default VALUE. */
8519 if (value != NULL_TREE)
8521 /* Don't issue more errors for error_mark_node (i.e. an
8522 undeclared identifier) - just ignore the value expression. */
8523 if (value == error_mark_node)
8524 value = NULL_TREE;
8525 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8527 error_at (loc, "enumerator value for %qE is not an integer constant",
8528 name);
8529 value = NULL_TREE;
8531 else
8533 if (TREE_CODE (value) != INTEGER_CST)
8535 value = c_fully_fold (value, false, NULL);
8536 if (TREE_CODE (value) == INTEGER_CST)
8537 pedwarn (loc, OPT_Wpedantic,
8538 "enumerator value for %qE is not an integer "
8539 "constant expression", name);
8541 if (TREE_CODE (value) != INTEGER_CST)
8543 error ("enumerator value for %qE is not an integer constant",
8544 name);
8545 value = NULL_TREE;
8547 else
8549 value = default_conversion (value);
8550 constant_expression_warning (value);
8555 /* Default based on previous value. */
8556 /* It should no longer be possible to have NON_LVALUE_EXPR
8557 in the default. */
8558 if (value == NULL_TREE)
8560 value = the_enum->enum_next_value;
8561 if (the_enum->enum_overflow)
8562 error_at (loc, "overflow in enumeration values");
8564 /* Even though the underlying type of an enum is unspecified, the
8565 type of enumeration constants is explicitly defined as int
8566 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8567 an extension. */
8568 else if (!int_fits_type_p (value, integer_type_node))
8569 pedwarn (loc, OPT_Wpedantic,
8570 "ISO C restricts enumerator values to range of %<int%>");
8572 /* The ISO C Standard mandates enumerators to have type int, even
8573 though the underlying type of an enum type is unspecified.
8574 However, GCC allows enumerators of any integer type as an
8575 extensions. Here we convert any enumerators that fit in an int
8576 to type int, to avoid promotions to unsigned types when comparing
8577 integers with enumerators that fit in the int range. When
8578 -pedantic is given, we would have already warned about those that
8579 don't fit. We have to do this here rather than in finish_enum
8580 because this value may be used to define more enumerators. */
8581 if (int_fits_type_p (value, integer_type_node))
8582 value = convert (integer_type_node, value);
8584 /* Set basis for default for next value. */
8585 the_enum->enum_next_value
8586 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8587 PLUS_EXPR, value, integer_one_node, false);
8588 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8590 /* Now create a declaration for the enum value name. */
8592 type = TREE_TYPE (value);
8593 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8594 TYPE_PRECISION (integer_type_node)),
8595 (TYPE_PRECISION (type)
8596 >= TYPE_PRECISION (integer_type_node)
8597 && TYPE_UNSIGNED (type)));
8599 decl = build_decl (decl_loc, CONST_DECL, name, type);
8600 DECL_INITIAL (decl) = convert (type, value);
8601 pushdecl (decl);
8603 return tree_cons (decl, value, NULL_TREE);
8607 /* Create the FUNCTION_DECL for a function definition.
8608 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8609 the declaration; they describe the function's name and the type it returns,
8610 but twisted together in a fashion that parallels the syntax of C.
8612 This function creates a binding context for the function body
8613 as well as setting up the FUNCTION_DECL in current_function_decl.
8615 Returns true on success. If the DECLARATOR is not suitable for a function
8616 (it defines a datum instead), we return false to report a parse error. */
8618 bool
8619 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8620 tree attributes)
8622 tree decl1, old_decl;
8623 tree restype, resdecl;
8624 location_t loc;
8626 current_function_returns_value = 0; /* Assume, until we see it does. */
8627 current_function_returns_null = 0;
8628 current_function_returns_abnormally = 0;
8629 warn_about_return_type = 0;
8630 c_switch_stack = NULL;
8632 /* Indicate no valid break/continue context by setting these variables
8633 to some non-null, non-label value. We'll notice and emit the proper
8634 error message in c_finish_bc_stmt. */
8635 c_break_label = c_cont_label = size_zero_node;
8637 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8638 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8639 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8641 /* If the declarator is not suitable for a function definition,
8642 cause a syntax error. */
8643 if (decl1 == NULL_TREE
8644 || TREE_CODE (decl1) != FUNCTION_DECL)
8645 return false;
8647 loc = DECL_SOURCE_LOCATION (decl1);
8649 c_decl_attributes (&decl1, attributes, 0);
8651 if (DECL_DECLARED_INLINE_P (decl1)
8652 && DECL_UNINLINABLE (decl1)
8653 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8654 warning_at (loc, OPT_Wattributes,
8655 "inline function %qD given attribute noinline",
8656 decl1);
8658 /* Handle gnu_inline attribute. */
8659 if (declspecs->inline_p
8660 && !flag_gnu89_inline
8661 && TREE_CODE (decl1) == FUNCTION_DECL
8662 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8663 || current_function_decl))
8665 if (declspecs->storage_class != csc_static)
8666 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8669 announce_function (decl1);
8671 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8673 error_at (loc, "return type is an incomplete type");
8674 /* Make it return void instead. */
8675 TREE_TYPE (decl1)
8676 = build_function_type (void_type_node,
8677 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8680 if (warn_about_return_type)
8681 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8682 : (warn_return_type ? OPT_Wreturn_type
8683 : OPT_Wimplicit_int),
8684 "return type defaults to %<int%>");
8686 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8687 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8688 DECL_INITIAL (decl1) = error_mark_node;
8690 /* A nested function is not global. */
8691 if (current_function_decl != NULL_TREE)
8692 TREE_PUBLIC (decl1) = 0;
8694 /* If this definition isn't a prototype and we had a prototype declaration
8695 before, copy the arg type info from that prototype. */
8696 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8697 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8698 old_decl = NULL_TREE;
8699 current_function_prototype_locus = UNKNOWN_LOCATION;
8700 current_function_prototype_built_in = false;
8701 current_function_prototype_arg_types = NULL_TREE;
8702 if (!prototype_p (TREE_TYPE (decl1)))
8704 if (old_decl != NULL_TREE
8705 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8706 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8707 TREE_TYPE (TREE_TYPE (old_decl))))
8709 if (stdarg_p (TREE_TYPE (old_decl)))
8711 warning_at (loc, 0, "%q+D defined as variadic function "
8712 "without prototype", decl1);
8713 locate_old_decl (old_decl);
8715 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8716 TREE_TYPE (decl1));
8717 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8718 current_function_prototype_built_in
8719 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8720 current_function_prototype_arg_types
8721 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8723 if (TREE_PUBLIC (decl1))
8725 /* If there is an external prototype declaration of this
8726 function, record its location but do not copy information
8727 to this decl. This may be an invisible declaration
8728 (built-in or in a scope which has finished) or simply
8729 have more refined argument types than any declaration
8730 found above. */
8731 struct c_binding *b;
8732 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8733 if (B_IN_SCOPE (b, external_scope))
8734 break;
8735 if (b)
8737 tree ext_decl, ext_type;
8738 ext_decl = b->decl;
8739 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8740 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8741 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8742 TREE_TYPE (ext_type)))
8744 current_function_prototype_locus
8745 = DECL_SOURCE_LOCATION (ext_decl);
8746 current_function_prototype_built_in
8747 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8748 current_function_prototype_arg_types
8749 = TYPE_ARG_TYPES (ext_type);
8755 /* Optionally warn of old-fashioned def with no previous prototype. */
8756 if (warn_strict_prototypes
8757 && old_decl != error_mark_node
8758 && !prototype_p (TREE_TYPE (decl1))
8759 && C_DECL_ISNT_PROTOTYPE (old_decl))
8760 warning_at (loc, OPT_Wstrict_prototypes,
8761 "function declaration isn%'t a prototype");
8762 /* Optionally warn of any global def with no previous prototype. */
8763 else if (warn_missing_prototypes
8764 && old_decl != error_mark_node
8765 && TREE_PUBLIC (decl1)
8766 && !MAIN_NAME_P (DECL_NAME (decl1))
8767 && C_DECL_ISNT_PROTOTYPE (old_decl)
8768 && !DECL_DECLARED_INLINE_P (decl1))
8769 warning_at (loc, OPT_Wmissing_prototypes,
8770 "no previous prototype for %qD", decl1);
8771 /* Optionally warn of any def with no previous prototype
8772 if the function has already been used. */
8773 else if (warn_missing_prototypes
8774 && old_decl != NULL_TREE
8775 && old_decl != error_mark_node
8776 && TREE_USED (old_decl)
8777 && !prototype_p (TREE_TYPE (old_decl)))
8778 warning_at (loc, OPT_Wmissing_prototypes,
8779 "%qD was used with no prototype before its definition", decl1);
8780 /* Optionally warn of any global def with no previous declaration. */
8781 else if (warn_missing_declarations
8782 && TREE_PUBLIC (decl1)
8783 && old_decl == NULL_TREE
8784 && !MAIN_NAME_P (DECL_NAME (decl1))
8785 && !DECL_DECLARED_INLINE_P (decl1))
8786 warning_at (loc, OPT_Wmissing_declarations,
8787 "no previous declaration for %qD",
8788 decl1);
8789 /* Optionally warn of any def with no previous declaration
8790 if the function has already been used. */
8791 else if (warn_missing_declarations
8792 && old_decl != NULL_TREE
8793 && old_decl != error_mark_node
8794 && TREE_USED (old_decl)
8795 && C_DECL_IMPLICIT (old_decl))
8796 warning_at (loc, OPT_Wmissing_declarations,
8797 "%qD was used with no declaration before its definition", decl1);
8799 /* This function exists in static storage.
8800 (This does not mean `static' in the C sense!) */
8801 TREE_STATIC (decl1) = 1;
8803 /* This is the earliest point at which we might know the assembler
8804 name of the function. Thus, if it's set before this, die horribly. */
8805 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8807 /* If #pragma weak was used, mark the decl weak now. */
8808 if (current_scope == file_scope)
8809 maybe_apply_pragma_weak (decl1);
8811 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8812 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8814 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8815 != integer_type_node)
8816 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8817 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8818 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8819 decl1);
8821 check_main_parameter_types (decl1);
8823 if (!TREE_PUBLIC (decl1))
8824 pedwarn (loc, OPT_Wmain,
8825 "%qD is normally a non-static function", decl1);
8828 /* Record the decl so that the function name is defined.
8829 If we already have a decl for this name, and it is a FUNCTION_DECL,
8830 use the old decl. */
8832 current_function_decl = pushdecl (decl1);
8834 push_scope ();
8835 declare_parm_level ();
8837 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8838 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8839 DECL_ARTIFICIAL (resdecl) = 1;
8840 DECL_IGNORED_P (resdecl) = 1;
8841 DECL_RESULT (current_function_decl) = resdecl;
8843 start_fname_decls ();
8845 return true;
8848 /* Subroutine of store_parm_decls which handles new-style function
8849 definitions (prototype format). The parms already have decls, so we
8850 need only record them as in effect and complain if any redundant
8851 old-style parm decls were written. */
8852 static void
8853 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8855 tree decl;
8856 c_arg_tag *tag;
8857 unsigned ix;
8859 if (current_scope->bindings)
8861 error_at (DECL_SOURCE_LOCATION (fndecl),
8862 "old-style parameter declarations in prototyped "
8863 "function definition");
8865 /* Get rid of the old-style declarations. */
8866 pop_scope ();
8867 push_scope ();
8869 /* Don't issue this warning for nested functions, and don't issue this
8870 warning if we got here because ARG_INFO_TYPES was error_mark_node
8871 (this happens when a function definition has just an ellipsis in
8872 its parameter list). */
8873 else if (!in_system_header_at (input_location)
8874 && !current_function_scope
8875 && arg_info->types != error_mark_node)
8876 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8877 "traditional C rejects ISO C style function definitions");
8879 /* Now make all the parameter declarations visible in the function body.
8880 We can bypass most of the grunt work of pushdecl. */
8881 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8883 DECL_CONTEXT (decl) = current_function_decl;
8884 if (DECL_NAME (decl))
8886 bind (DECL_NAME (decl), decl, current_scope,
8887 /*invisible=*/false, /*nested=*/false,
8888 UNKNOWN_LOCATION);
8889 if (!TREE_USED (decl))
8890 warn_if_shadowing (decl);
8892 else
8893 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8896 /* Record the parameter list in the function declaration. */
8897 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8899 /* Now make all the ancillary declarations visible, likewise. */
8900 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8902 DECL_CONTEXT (decl) = current_function_decl;
8903 if (DECL_NAME (decl))
8904 bind (DECL_NAME (decl), decl, current_scope,
8905 /*invisible=*/false,
8906 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8907 UNKNOWN_LOCATION);
8910 /* And all the tag declarations. */
8911 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8912 if (tag->id)
8913 bind (tag->id, tag->type, current_scope,
8914 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8917 /* Subroutine of store_parm_decls which handles old-style function
8918 definitions (separate parameter list and declarations). */
8920 static void
8921 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8923 struct c_binding *b;
8924 tree parm, decl, last;
8925 tree parmids = arg_info->parms;
8926 hash_set<tree> seen_args;
8928 if (!in_system_header_at (input_location))
8929 warning_at (DECL_SOURCE_LOCATION (fndecl),
8930 OPT_Wold_style_definition, "old-style function definition");
8932 /* Match each formal parameter name with its declaration. Save each
8933 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8934 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8936 if (TREE_VALUE (parm) == NULL_TREE)
8938 error_at (DECL_SOURCE_LOCATION (fndecl),
8939 "parameter name missing from parameter list");
8940 TREE_PURPOSE (parm) = NULL_TREE;
8941 continue;
8944 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8945 if (b && B_IN_CURRENT_SCOPE (b))
8947 decl = b->decl;
8948 /* Skip erroneous parameters. */
8949 if (decl == error_mark_node)
8950 continue;
8951 /* If we got something other than a PARM_DECL it is an error. */
8952 if (TREE_CODE (decl) != PARM_DECL)
8954 error_at (DECL_SOURCE_LOCATION (decl),
8955 "%qD declared as a non-parameter", decl);
8956 continue;
8958 /* If the declaration is already marked, we have a duplicate
8959 name. Complain and ignore the duplicate. */
8960 else if (seen_args.contains (decl))
8962 error_at (DECL_SOURCE_LOCATION (decl),
8963 "multiple parameters named %qD", decl);
8964 TREE_PURPOSE (parm) = NULL_TREE;
8965 continue;
8967 /* If the declaration says "void", complain and turn it into
8968 an int. */
8969 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8971 error_at (DECL_SOURCE_LOCATION (decl),
8972 "parameter %qD declared with void type", decl);
8973 TREE_TYPE (decl) = integer_type_node;
8974 DECL_ARG_TYPE (decl) = integer_type_node;
8975 layout_decl (decl, 0);
8977 warn_if_shadowing (decl);
8979 /* If no declaration found, default to int. */
8980 else
8982 /* FIXME diagnostics: This should be the location of the argument,
8983 not the FNDECL. E.g., for an old-style declaration
8985 int f10(v) { blah; }
8987 We should use the location of the V, not the F10.
8988 Unfortunately, the V is an IDENTIFIER_NODE which has no
8989 location. In the future we need locations for c_arg_info
8990 entries.
8992 See gcc.dg/Wshadow-3.c for an example of this problem. */
8993 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8994 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8995 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8996 pushdecl (decl);
8997 warn_if_shadowing (decl);
8999 if (flag_isoc99)
9000 pedwarn (DECL_SOURCE_LOCATION (decl),
9001 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9002 decl);
9003 else
9004 warning_at (DECL_SOURCE_LOCATION (decl),
9005 OPT_Wmissing_parameter_type,
9006 "type of %qD defaults to %<int%>", decl);
9009 TREE_PURPOSE (parm) = decl;
9010 seen_args.add (decl);
9013 /* Now examine the parms chain for incomplete declarations
9014 and declarations with no corresponding names. */
9016 for (b = current_scope->bindings; b; b = b->prev)
9018 parm = b->decl;
9019 if (TREE_CODE (parm) != PARM_DECL)
9020 continue;
9022 if (TREE_TYPE (parm) != error_mark_node
9023 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9025 error_at (DECL_SOURCE_LOCATION (parm),
9026 "parameter %qD has incomplete type", parm);
9027 TREE_TYPE (parm) = error_mark_node;
9030 if (!seen_args.contains (parm))
9032 error_at (DECL_SOURCE_LOCATION (parm),
9033 "declaration for parameter %qD but no such parameter",
9034 parm);
9036 /* Pretend the parameter was not missing.
9037 This gets us to a standard state and minimizes
9038 further error messages. */
9039 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9043 /* Chain the declarations together in the order of the list of
9044 names. Store that chain in the function decl, replacing the
9045 list of names. Update the current scope to match. */
9046 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9048 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9049 if (TREE_PURPOSE (parm))
9050 break;
9051 if (parm && TREE_PURPOSE (parm))
9053 last = TREE_PURPOSE (parm);
9054 DECL_ARGUMENTS (fndecl) = last;
9056 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9057 if (TREE_PURPOSE (parm))
9059 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9060 last = TREE_PURPOSE (parm);
9062 DECL_CHAIN (last) = NULL_TREE;
9065 /* If there was a previous prototype,
9066 set the DECL_ARG_TYPE of each argument according to
9067 the type previously specified, and report any mismatches. */
9069 if (current_function_prototype_arg_types)
9071 tree type;
9072 for (parm = DECL_ARGUMENTS (fndecl),
9073 type = current_function_prototype_arg_types;
9074 parm || (type != NULL_TREE
9075 && TREE_VALUE (type) != error_mark_node
9076 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9077 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9079 if (parm == NULL_TREE
9080 || type == NULL_TREE
9081 || (TREE_VALUE (type) != error_mark_node
9082 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9084 if (current_function_prototype_built_in)
9085 warning_at (DECL_SOURCE_LOCATION (fndecl),
9086 0, "number of arguments doesn%'t match "
9087 "built-in prototype");
9088 else
9090 /* FIXME diagnostics: This should be the location of
9091 FNDECL, but there is bug when a prototype is
9092 declared inside function context, but defined
9093 outside of it (e.g., gcc.dg/pr15698-2.c). In
9094 which case FNDECL gets the location of the
9095 prototype, not the definition. */
9096 error_at (input_location,
9097 "number of arguments doesn%'t match prototype");
9099 error_at (current_function_prototype_locus,
9100 "prototype declaration");
9102 break;
9104 /* Type for passing arg must be consistent with that
9105 declared for the arg. ISO C says we take the unqualified
9106 type for parameters declared with qualified type. */
9107 if (TREE_TYPE (parm) != error_mark_node
9108 && TREE_VALUE (type) != error_mark_node
9109 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9110 != TYPE_ATOMIC (TREE_VALUE (type)))
9111 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9112 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9114 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9115 == TYPE_ATOMIC (TREE_VALUE (type)))
9116 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9117 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9119 /* Adjust argument to match prototype. E.g. a previous
9120 `int foo(float);' prototype causes
9121 `int foo(x) float x; {...}' to be treated like
9122 `int foo(float x) {...}'. This is particularly
9123 useful for argument types like uid_t. */
9124 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9126 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9127 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9128 && (TYPE_PRECISION (TREE_TYPE (parm))
9129 < TYPE_PRECISION (integer_type_node)))
9130 DECL_ARG_TYPE (parm)
9131 = c_type_promotes_to (TREE_TYPE (parm));
9133 /* ??? Is it possible to get here with a
9134 built-in prototype or will it always have
9135 been diagnosed as conflicting with an
9136 old-style definition and discarded? */
9137 if (current_function_prototype_built_in)
9138 warning_at (DECL_SOURCE_LOCATION (parm),
9139 OPT_Wpedantic, "promoted argument %qD "
9140 "doesn%'t match built-in prototype", parm);
9141 else
9143 pedwarn (DECL_SOURCE_LOCATION (parm),
9144 OPT_Wpedantic, "promoted argument %qD "
9145 "doesn%'t match prototype", parm);
9146 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9147 "prototype declaration");
9150 else
9152 if (current_function_prototype_built_in)
9153 warning_at (DECL_SOURCE_LOCATION (parm),
9154 0, "argument %qD doesn%'t match "
9155 "built-in prototype", parm);
9156 else
9158 error_at (DECL_SOURCE_LOCATION (parm),
9159 "argument %qD doesn%'t match prototype", parm);
9160 error_at (current_function_prototype_locus,
9161 "prototype declaration");
9166 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9169 /* Otherwise, create a prototype that would match. */
9171 else
9173 tree actual = NULL_TREE, last = NULL_TREE, type;
9175 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9177 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9178 if (last)
9179 TREE_CHAIN (last) = type;
9180 else
9181 actual = type;
9182 last = type;
9184 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9185 if (last)
9186 TREE_CHAIN (last) = type;
9187 else
9188 actual = type;
9190 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9191 of the type of this function, but we need to avoid having this
9192 affect the types of other similarly-typed functions, so we must
9193 first force the generation of an identical (but separate) type
9194 node for the relevant function type. The new node we create
9195 will be a variant of the main variant of the original function
9196 type. */
9198 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9200 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9204 /* Store parameter declarations passed in ARG_INFO into the current
9205 function declaration. */
9207 void
9208 store_parm_decls_from (struct c_arg_info *arg_info)
9210 current_function_arg_info = arg_info;
9211 store_parm_decls ();
9214 /* Called by walk_tree to look for and update context-less labels. */
9216 static tree
9217 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9219 if (TREE_CODE (*tp) == LABEL_EXPR
9220 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9222 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9223 *walk_subtrees = 0;
9226 return NULL_TREE;
9229 /* Store the parameter declarations into the current function declaration.
9230 This is called after parsing the parameter declarations, before
9231 digesting the body of the function.
9233 For an old-style definition, construct a prototype out of the old-style
9234 parameter declarations and inject it into the function's type. */
9236 void
9237 store_parm_decls (void)
9239 tree fndecl = current_function_decl;
9240 bool proto;
9242 /* The argument information block for FNDECL. */
9243 struct c_arg_info *arg_info = current_function_arg_info;
9244 current_function_arg_info = 0;
9246 /* True if this definition is written with a prototype. Note:
9247 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9248 list in a function definition as equivalent to (void) -- an
9249 empty argument list specifies the function has no parameters,
9250 but only (void) sets up a prototype for future calls. */
9251 proto = arg_info->types != 0;
9253 if (proto)
9254 store_parm_decls_newstyle (fndecl, arg_info);
9255 else
9256 store_parm_decls_oldstyle (fndecl, arg_info);
9258 /* The next call to push_scope will be a function body. */
9260 next_is_function_body = true;
9262 /* Write a record describing this function definition to the prototypes
9263 file (if requested). */
9265 gen_aux_info_record (fndecl, 1, 0, proto);
9267 /* Initialize the RTL code for the function. */
9268 allocate_struct_function (fndecl, false);
9270 if (warn_unused_local_typedefs)
9271 cfun->language = ggc_cleared_alloc<language_function> ();
9273 /* Begin the statement tree for this function. */
9274 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9276 /* ??? Insert the contents of the pending sizes list into the function
9277 to be evaluated. The only reason left to have this is
9278 void foo(int n, int array[n++])
9279 because we throw away the array type in favor of a pointer type, and
9280 thus won't naturally see the SAVE_EXPR containing the increment. All
9281 other pending sizes would be handled by gimplify_parameters. */
9282 if (arg_info->pending_sizes)
9284 /* In very special circumstances, e.g. for code like
9285 _Atomic int i = 5;
9286 void f (int a[i += 2]) {}
9287 we need to execute the atomic assignment on function entry.
9288 But in this case, it is not just a straight store, it has the
9289 op= form, which means that build_atomic_assign has generated
9290 gotos, labels, etc. Because at that time the function decl
9291 for F has not been created yet, those labels do not have any
9292 function context. But we have the fndecl now, so update the
9293 labels accordingly. gimplify_expr would crash otherwise. */
9294 walk_tree_without_duplicates (&arg_info->pending_sizes,
9295 set_labels_context_r, fndecl);
9296 add_stmt (arg_info->pending_sizes);
9300 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9301 c_finish_omp_declare_simd for function prototypes. No diagnostics
9302 should be done. */
9304 void
9305 temp_store_parm_decls (tree fndecl, tree parms)
9307 push_scope ();
9308 for (tree p = parms; p; p = DECL_CHAIN (p))
9310 DECL_CONTEXT (p) = fndecl;
9311 if (DECL_NAME (p))
9312 bind (DECL_NAME (p), p, current_scope,
9313 /*invisible=*/false, /*nested=*/false,
9314 UNKNOWN_LOCATION);
9318 /* Undo what temp_store_parm_decls did. */
9320 void
9321 temp_pop_parm_decls (void)
9323 /* Clear all bindings in this temporary scope, so that
9324 pop_scope doesn't create a BLOCK. */
9325 struct c_binding *b = current_scope->bindings;
9326 current_scope->bindings = NULL;
9327 for (; b; b = free_binding_and_advance (b))
9329 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9330 || b->decl == error_mark_node);
9331 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9332 I_SYMBOL_BINDING (b->id) = b->shadowed;
9333 if (b->shadowed && b->shadowed->u.type)
9334 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9336 pop_scope ();
9340 /* Finish up a function declaration and compile that function
9341 all the way to assembler language output. Then free the storage
9342 for the function definition.
9344 This is called after parsing the body of the function definition. */
9346 void
9347 finish_function (void)
9349 tree fndecl = current_function_decl;
9351 if (c_dialect_objc ())
9352 objc_finish_function ();
9354 if (TREE_CODE (fndecl) == FUNCTION_DECL
9355 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9357 tree args = DECL_ARGUMENTS (fndecl);
9358 for (; args; args = DECL_CHAIN (args))
9360 tree type = TREE_TYPE (args);
9361 if (INTEGRAL_TYPE_P (type)
9362 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9363 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9367 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9368 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9370 /* Must mark the RESULT_DECL as being in this function. */
9372 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9373 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9375 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9376 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9377 == integer_type_node && flag_isoc99)
9379 /* Hack. We don't want the middle-end to warn that this return
9380 is unreachable, so we mark its location as special. Using
9381 UNKNOWN_LOCATION has the problem that it gets clobbered in
9382 annotate_one_with_locus. A cleaner solution might be to
9383 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9385 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9388 /* Tie off the statement tree for this function. */
9389 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9391 /* If the function has _Cilk_spawn in front of a function call inside it
9392 i.e. it is a spawning function, then add the appropriate Cilk plus
9393 functions inside. */
9394 if (fn_contains_cilk_spawn_p (cfun))
9395 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9397 finish_fname_decls ();
9399 /* Complain if there's just no return statement. */
9400 if (warn_return_type
9401 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9402 && !current_function_returns_value && !current_function_returns_null
9403 /* Don't complain if we are no-return. */
9404 && !current_function_returns_abnormally
9405 /* Don't complain if we are declared noreturn. */
9406 && !TREE_THIS_VOLATILE (fndecl)
9407 /* Don't warn for main(). */
9408 && !MAIN_NAME_P (DECL_NAME (fndecl))
9409 /* Or if they didn't actually specify a return type. */
9410 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9411 /* Normally, with -Wreturn-type, flow will complain, but we might
9412 optimize out static functions. */
9413 && !TREE_PUBLIC (fndecl))
9415 warning (OPT_Wreturn_type,
9416 "no return statement in function returning non-void");
9417 TREE_NO_WARNING (fndecl) = 1;
9420 /* Complain about parameters that are only set, but never otherwise used. */
9421 if (warn_unused_but_set_parameter)
9423 tree decl;
9425 for (decl = DECL_ARGUMENTS (fndecl);
9426 decl;
9427 decl = DECL_CHAIN (decl))
9428 if (TREE_USED (decl)
9429 && TREE_CODE (decl) == PARM_DECL
9430 && !DECL_READ_P (decl)
9431 && DECL_NAME (decl)
9432 && !DECL_ARTIFICIAL (decl)
9433 && !TREE_NO_WARNING (decl))
9434 warning_at (DECL_SOURCE_LOCATION (decl),
9435 OPT_Wunused_but_set_parameter,
9436 "parameter %qD set but not used", decl);
9439 /* Complain about locally defined typedefs that are not used in this
9440 function. */
9441 maybe_warn_unused_local_typedefs ();
9443 /* Possibly warn about unused parameters. */
9444 if (warn_unused_parameter)
9445 do_warn_unused_parameter (fndecl);
9447 /* Store the end of the function, so that we get good line number
9448 info for the epilogue. */
9449 cfun->function_end_locus = input_location;
9451 /* Finalize the ELF visibility for the function. */
9452 c_determine_visibility (fndecl);
9454 /* For GNU C extern inline functions disregard inline limits. */
9455 if (DECL_EXTERNAL (fndecl)
9456 && DECL_DECLARED_INLINE_P (fndecl)
9457 && (flag_gnu89_inline
9458 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9459 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9461 /* Genericize before inlining. Delay genericizing nested functions
9462 until their parent function is genericized. Since finalizing
9463 requires GENERIC, delay that as well. */
9465 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9466 && !undef_nested_function)
9468 if (!decl_function_context (fndecl))
9470 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9471 c_genericize (fndecl);
9473 /* ??? Objc emits functions after finalizing the compilation unit.
9474 This should be cleaned up later and this conditional removed. */
9475 if (symtab->global_info_ready)
9477 cgraph_node::add_new_function (fndecl, false);
9478 return;
9480 cgraph_node::finalize_function (fndecl, false);
9482 else
9484 /* Register this function with cgraph just far enough to get it
9485 added to our parent's nested function list. Handy, since the
9486 C front end doesn't have such a list. */
9487 (void) cgraph_node::get_create (fndecl);
9491 if (!decl_function_context (fndecl))
9492 undef_nested_function = false;
9494 if (cfun->language != NULL)
9496 ggc_free (cfun->language);
9497 cfun->language = NULL;
9500 /* We're leaving the context of this function, so zap cfun.
9501 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9502 tree_rest_of_compilation. */
9503 set_cfun (NULL);
9504 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9505 current_function_decl = NULL;
9508 /* Check the declarations given in a for-loop for satisfying the C99
9509 constraints. If exactly one such decl is found, return it. LOC is
9510 the location of the opening parenthesis of the for loop. The last
9511 parameter allows you to control the "for loop initial declarations
9512 are only allowed in C99 mode". Normally, you should pass
9513 flag_isoc99 as that parameter. But in some cases (Objective-C
9514 foreach loop, for example) we want to run the checks in this
9515 function even if not in C99 mode, so we allow the caller to turn
9516 off the error about not being in C99 mode.
9519 tree
9520 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9522 struct c_binding *b;
9523 tree one_decl = NULL_TREE;
9524 int n_decls = 0;
9526 if (!turn_off_iso_c99_error)
9528 static bool hint = true;
9529 /* If we get here, declarations have been used in a for loop without
9530 the C99 for loop scope. This doesn't make much sense, so don't
9531 allow it. */
9532 error_at (loc, "%<for%> loop initial declarations "
9533 "are only allowed in C99 or C11 mode");
9534 if (hint)
9536 inform (loc,
9537 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9538 "to compile your code");
9539 hint = false;
9541 return NULL_TREE;
9543 /* C99 subclause 6.8.5 paragraph 3:
9545 [#3] The declaration part of a for statement shall only
9546 declare identifiers for objects having storage class auto or
9547 register.
9549 It isn't clear whether, in this sentence, "identifiers" binds to
9550 "shall only declare" or to "objects" - that is, whether all identifiers
9551 declared must be identifiers for objects, or whether the restriction
9552 only applies to those that are. (A question on this in comp.std.c
9553 in November 2000 received no answer.) We implement the strictest
9554 interpretation, to avoid creating an extension which later causes
9555 problems. */
9557 for (b = current_scope->bindings; b; b = b->prev)
9559 tree id = b->id;
9560 tree decl = b->decl;
9562 if (!id)
9563 continue;
9565 switch (TREE_CODE (decl))
9567 case VAR_DECL:
9569 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9570 if (TREE_STATIC (decl))
9571 error_at (decl_loc,
9572 "declaration of static variable %qD in %<for%> loop "
9573 "initial declaration", decl);
9574 else if (DECL_EXTERNAL (decl))
9575 error_at (decl_loc,
9576 "declaration of %<extern%> variable %qD in %<for%> loop "
9577 "initial declaration", decl);
9579 break;
9581 case RECORD_TYPE:
9582 error_at (loc,
9583 "%<struct %E%> declared in %<for%> loop initial "
9584 "declaration", id);
9585 break;
9586 case UNION_TYPE:
9587 error_at (loc,
9588 "%<union %E%> declared in %<for%> loop initial declaration",
9589 id);
9590 break;
9591 case ENUMERAL_TYPE:
9592 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9593 "initial declaration", id);
9594 break;
9595 default:
9596 error_at (loc, "declaration of non-variable "
9597 "%qD in %<for%> loop initial declaration", decl);
9600 n_decls++;
9601 one_decl = decl;
9604 return n_decls == 1 ? one_decl : NULL_TREE;
9607 /* Save and reinitialize the variables
9608 used during compilation of a C function. */
9610 void
9611 c_push_function_context (void)
9613 struct language_function *p = cfun->language;
9614 /* cfun->language might have been already allocated by the use of
9615 -Wunused-local-typedefs. In that case, just re-use it. */
9616 if (p == NULL)
9617 cfun->language = p = ggc_cleared_alloc<language_function> ();
9619 p->base.x_stmt_tree = c_stmt_tree;
9620 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9621 p->x_break_label = c_break_label;
9622 p->x_cont_label = c_cont_label;
9623 p->x_switch_stack = c_switch_stack;
9624 p->arg_info = current_function_arg_info;
9625 p->returns_value = current_function_returns_value;
9626 p->returns_null = current_function_returns_null;
9627 p->returns_abnormally = current_function_returns_abnormally;
9628 p->warn_about_return_type = warn_about_return_type;
9630 push_function_context ();
9633 /* Restore the variables used during compilation of a C function. */
9635 void
9636 c_pop_function_context (void)
9638 struct language_function *p;
9640 pop_function_context ();
9641 p = cfun->language;
9643 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9644 used to store data throughout the life time of the current cfun,
9645 So don't deallocate it. */
9646 if (!warn_unused_local_typedefs)
9647 cfun->language = NULL;
9649 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9650 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9652 /* Stop pointing to the local nodes about to be freed. */
9653 /* But DECL_INITIAL must remain nonzero so we know this
9654 was an actual function definition. */
9655 DECL_INITIAL (current_function_decl) = error_mark_node;
9656 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9659 c_stmt_tree = p->base.x_stmt_tree;
9660 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9661 c_break_label = p->x_break_label;
9662 c_cont_label = p->x_cont_label;
9663 c_switch_stack = p->x_switch_stack;
9664 current_function_arg_info = p->arg_info;
9665 current_function_returns_value = p->returns_value;
9666 current_function_returns_null = p->returns_null;
9667 current_function_returns_abnormally = p->returns_abnormally;
9668 warn_about_return_type = p->warn_about_return_type;
9671 /* The functions below are required for functionality of doing
9672 function at once processing in the C front end. Currently these
9673 functions are not called from anywhere in the C front end, but as
9674 these changes continue, that will change. */
9676 /* Returns the stmt_tree (if any) to which statements are currently
9677 being added. If there is no active statement-tree, NULL is
9678 returned. */
9680 stmt_tree
9681 current_stmt_tree (void)
9683 return &c_stmt_tree;
9686 /* Return the global value of T as a symbol. */
9688 tree
9689 identifier_global_value (tree t)
9691 struct c_binding *b;
9693 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9694 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9695 return b->decl;
9697 return NULL_TREE;
9700 /* In C, the only C-linkage public declaration is at file scope. */
9702 tree
9703 c_linkage_bindings (tree name)
9705 return identifier_global_value (name);
9708 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9709 otherwise the name is found in ridpointers from RID_INDEX. */
9711 void
9712 record_builtin_type (enum rid rid_index, const char *name, tree type)
9714 tree id, decl;
9715 if (name == 0)
9716 id = ridpointers[(int) rid_index];
9717 else
9718 id = get_identifier (name);
9719 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9720 pushdecl (decl);
9721 if (debug_hooks->type_decl)
9722 debug_hooks->type_decl (decl, false);
9725 /* Build the void_list_node (void_type_node having been created). */
9726 tree
9727 build_void_list_node (void)
9729 tree t = build_tree_list (NULL_TREE, void_type_node);
9730 return t;
9733 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9735 struct c_parm *
9736 build_c_parm (struct c_declspecs *specs, tree attrs,
9737 struct c_declarator *declarator,
9738 location_t loc)
9740 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9741 ret->specs = specs;
9742 ret->attrs = attrs;
9743 ret->declarator = declarator;
9744 ret->loc = loc;
9745 return ret;
9748 /* Return a declarator with nested attributes. TARGET is the inner
9749 declarator to which these attributes apply. ATTRS are the
9750 attributes. */
9752 struct c_declarator *
9753 build_attrs_declarator (tree attrs, struct c_declarator *target)
9755 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9756 ret->kind = cdk_attrs;
9757 ret->declarator = target;
9758 ret->u.attrs = attrs;
9759 return ret;
9762 /* Return a declarator for a function with arguments specified by ARGS
9763 and return type specified by TARGET. */
9765 struct c_declarator *
9766 build_function_declarator (struct c_arg_info *args,
9767 struct c_declarator *target)
9769 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9770 ret->kind = cdk_function;
9771 ret->declarator = target;
9772 ret->u.arg_info = args;
9773 return ret;
9776 /* Return a declarator for the identifier IDENT (which may be
9777 NULL_TREE for an abstract declarator). */
9779 struct c_declarator *
9780 build_id_declarator (tree ident)
9782 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9783 ret->kind = cdk_id;
9784 ret->declarator = 0;
9785 ret->u.id = ident;
9786 /* Default value - may get reset to a more precise location. */
9787 ret->id_loc = input_location;
9788 return ret;
9791 /* Return something to represent absolute declarators containing a *.
9792 TARGET is the absolute declarator that the * contains.
9793 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9794 to apply to the pointer type. */
9796 struct c_declarator *
9797 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9798 struct c_declarator *target)
9800 tree attrs;
9801 int quals = 0;
9802 struct c_declarator *itarget = target;
9803 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9804 if (type_quals_attrs)
9806 attrs = type_quals_attrs->attrs;
9807 quals = quals_from_declspecs (type_quals_attrs);
9808 if (attrs != NULL_TREE)
9809 itarget = build_attrs_declarator (attrs, target);
9811 ret->kind = cdk_pointer;
9812 ret->declarator = itarget;
9813 ret->u.pointer_quals = quals;
9814 return ret;
9817 /* Return a pointer to a structure for an empty list of declaration
9818 specifiers. */
9820 struct c_declspecs *
9821 build_null_declspecs (void)
9823 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9824 memset (ret, 0, sizeof *ret);
9825 ret->align_log = -1;
9826 ret->typespec_word = cts_none;
9827 ret->storage_class = csc_none;
9828 ret->expr_const_operands = true;
9829 ret->typespec_kind = ctsk_none;
9830 ret->address_space = ADDR_SPACE_GENERIC;
9831 return ret;
9834 /* Add the address space ADDRSPACE to the declaration specifiers
9835 SPECS, returning SPECS. */
9837 struct c_declspecs *
9838 declspecs_add_addrspace (source_location location,
9839 struct c_declspecs *specs, addr_space_t as)
9841 specs->non_sc_seen_p = true;
9842 specs->declspecs_seen_p = true;
9844 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9845 && specs->address_space != as)
9846 error ("incompatible address space qualifiers %qs and %qs",
9847 c_addr_space_name (as),
9848 c_addr_space_name (specs->address_space));
9849 else
9851 specs->address_space = as;
9852 specs->locations[cdw_address_space] = location;
9854 return specs;
9857 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9858 returning SPECS. */
9860 struct c_declspecs *
9861 declspecs_add_qual (source_location loc,
9862 struct c_declspecs *specs, tree qual)
9864 enum rid i;
9865 bool dupe = false;
9866 specs->non_sc_seen_p = true;
9867 specs->declspecs_seen_p = true;
9868 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9869 && C_IS_RESERVED_WORD (qual));
9870 i = C_RID_CODE (qual);
9871 location_t prev_loc = UNKNOWN_LOCATION;
9872 switch (i)
9874 case RID_CONST:
9875 dupe = specs->const_p;
9876 specs->const_p = true;
9877 prev_loc = specs->locations[cdw_const];
9878 specs->locations[cdw_const] = loc;
9879 break;
9880 case RID_VOLATILE:
9881 dupe = specs->volatile_p;
9882 specs->volatile_p = true;
9883 prev_loc = specs->locations[cdw_volatile];
9884 specs->locations[cdw_volatile] = loc;
9885 break;
9886 case RID_RESTRICT:
9887 dupe = specs->restrict_p;
9888 specs->restrict_p = true;
9889 prev_loc = specs->locations[cdw_restrict];
9890 specs->locations[cdw_restrict] = loc;
9891 break;
9892 case RID_ATOMIC:
9893 dupe = specs->atomic_p;
9894 specs->atomic_p = true;
9895 prev_loc = specs->locations[cdw_atomic];
9896 specs->locations[cdw_atomic] = loc;
9897 break;
9898 default:
9899 gcc_unreachable ();
9901 if (dupe)
9903 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9904 "duplicate %qE declaration specifier", qual);
9905 if (!warned
9906 && warn_duplicate_decl_specifier
9907 && prev_loc >= RESERVED_LOCATION_COUNT
9908 && !from_macro_expansion_at (prev_loc)
9909 && !from_macro_expansion_at (loc))
9910 warning_at (loc, OPT_Wduplicate_decl_specifier,
9911 "duplicate %qE declaration specifier", qual);
9913 return specs;
9916 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9917 returning SPECS. */
9919 struct c_declspecs *
9920 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9921 struct c_typespec spec)
9923 tree type = spec.spec;
9924 specs->non_sc_seen_p = true;
9925 specs->declspecs_seen_p = true;
9926 specs->typespec_kind = spec.kind;
9927 if (TREE_DEPRECATED (type))
9928 specs->deprecated_p = true;
9930 /* Handle type specifier keywords. */
9931 if (TREE_CODE (type) == IDENTIFIER_NODE
9932 && C_IS_RESERVED_WORD (type)
9933 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9935 enum rid i = C_RID_CODE (type);
9936 if (specs->type)
9938 error_at (loc, "two or more data types in declaration specifiers");
9939 return specs;
9941 if ((int) i <= (int) RID_LAST_MODIFIER)
9943 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9944 bool dupe = false;
9945 switch (i)
9947 case RID_LONG:
9948 if (specs->long_long_p)
9950 error_at (loc, "%<long long long%> is too long for GCC");
9951 break;
9953 if (specs->long_p)
9955 if (specs->typespec_word == cts_double)
9957 error_at (loc,
9958 ("both %<long long%> and %<double%> in "
9959 "declaration specifiers"));
9960 break;
9962 pedwarn_c90 (loc, OPT_Wlong_long,
9963 "ISO C90 does not support %<long long%>");
9964 specs->long_long_p = 1;
9965 specs->locations[cdw_long_long] = loc;
9966 break;
9968 if (specs->short_p)
9969 error_at (loc,
9970 ("both %<long%> and %<short%> in "
9971 "declaration specifiers"));
9972 else if (specs->typespec_word == cts_auto_type)
9973 error_at (loc,
9974 ("both %<long%> and %<__auto_type%> in "
9975 "declaration specifiers"));
9976 else if (specs->typespec_word == cts_void)
9977 error_at (loc,
9978 ("both %<long%> and %<void%> in "
9979 "declaration specifiers"));
9980 else if (specs->typespec_word == cts_int_n)
9981 error_at (loc,
9982 ("both %<long%> and %<__int%d%> in "
9983 "declaration specifiers"),
9984 int_n_data[specs->int_n_idx].bitsize);
9985 else if (specs->typespec_word == cts_bool)
9986 error_at (loc,
9987 ("both %<long%> and %<_Bool%> in "
9988 "declaration specifiers"));
9989 else if (specs->typespec_word == cts_char)
9990 error_at (loc,
9991 ("both %<long%> and %<char%> in "
9992 "declaration specifiers"));
9993 else if (specs->typespec_word == cts_float)
9994 error_at (loc,
9995 ("both %<long%> and %<float%> in "
9996 "declaration specifiers"));
9997 else if (specs->typespec_word == cts_floatn_nx)
9998 error_at (loc,
9999 ("both %<long%> and %<_Float%d%s%> in "
10000 "declaration specifiers"),
10001 floatn_nx_types[specs->floatn_nx_idx].n,
10002 (floatn_nx_types[specs->floatn_nx_idx].extended
10003 ? "x"
10004 : ""));
10005 else if (specs->typespec_word == cts_dfloat32)
10006 error_at (loc,
10007 ("both %<long%> and %<_Decimal32%> in "
10008 "declaration specifiers"));
10009 else if (specs->typespec_word == cts_dfloat64)
10010 error_at (loc,
10011 ("both %<long%> and %<_Decimal64%> in "
10012 "declaration specifiers"));
10013 else if (specs->typespec_word == cts_dfloat128)
10014 error_at (loc,
10015 ("both %<long%> and %<_Decimal128%> in "
10016 "declaration specifiers"));
10017 else
10019 specs->long_p = true;
10020 specs->locations[cdw_long] = loc;
10022 break;
10023 case RID_SHORT:
10024 dupe = specs->short_p;
10025 if (specs->long_p)
10026 error_at (loc,
10027 ("both %<long%> and %<short%> in "
10028 "declaration specifiers"));
10029 else if (specs->typespec_word == cts_auto_type)
10030 error_at (loc,
10031 ("both %<short%> and %<__auto_type%> in "
10032 "declaration specifiers"));
10033 else if (specs->typespec_word == cts_void)
10034 error_at (loc,
10035 ("both %<short%> and %<void%> in "
10036 "declaration specifiers"));
10037 else if (specs->typespec_word == cts_int_n)
10038 error_at (loc,
10039 ("both %<short%> and %<__int%d%> in "
10040 "declaration specifiers"),
10041 int_n_data[specs->int_n_idx].bitsize);
10042 else if (specs->typespec_word == cts_bool)
10043 error_at (loc,
10044 ("both %<short%> and %<_Bool%> in "
10045 "declaration specifiers"));
10046 else if (specs->typespec_word == cts_char)
10047 error_at (loc,
10048 ("both %<short%> and %<char%> in "
10049 "declaration specifiers"));
10050 else if (specs->typespec_word == cts_float)
10051 error_at (loc,
10052 ("both %<short%> and %<float%> in "
10053 "declaration specifiers"));
10054 else if (specs->typespec_word == cts_double)
10055 error_at (loc,
10056 ("both %<short%> and %<double%> in "
10057 "declaration specifiers"));
10058 else if (specs->typespec_word == cts_floatn_nx)
10059 error_at (loc,
10060 ("both %<short%> and %<_Float%d%s%> in "
10061 "declaration specifiers"),
10062 floatn_nx_types[specs->floatn_nx_idx].n,
10063 (floatn_nx_types[specs->floatn_nx_idx].extended
10064 ? "x"
10065 : ""));
10066 else if (specs->typespec_word == cts_dfloat32)
10067 error_at (loc,
10068 ("both %<short%> and %<_Decimal32%> in "
10069 "declaration specifiers"));
10070 else if (specs->typespec_word == cts_dfloat64)
10071 error_at (loc,
10072 ("both %<short%> and %<_Decimal64%> in "
10073 "declaration specifiers"));
10074 else if (specs->typespec_word == cts_dfloat128)
10075 error_at (loc,
10076 ("both %<short%> and %<_Decimal128%> in "
10077 "declaration specifiers"));
10078 else
10080 specs->short_p = true;
10081 specs->locations[cdw_short] = loc;
10083 break;
10084 case RID_SIGNED:
10085 dupe = specs->signed_p;
10086 if (specs->unsigned_p)
10087 error_at (loc,
10088 ("both %<signed%> and %<unsigned%> in "
10089 "declaration specifiers"));
10090 else if (specs->typespec_word == cts_auto_type)
10091 error_at (loc,
10092 ("both %<signed%> and %<__auto_type%> in "
10093 "declaration specifiers"));
10094 else if (specs->typespec_word == cts_void)
10095 error_at (loc,
10096 ("both %<signed%> and %<void%> in "
10097 "declaration specifiers"));
10098 else if (specs->typespec_word == cts_bool)
10099 error_at (loc,
10100 ("both %<signed%> and %<_Bool%> in "
10101 "declaration specifiers"));
10102 else if (specs->typespec_word == cts_float)
10103 error_at (loc,
10104 ("both %<signed%> and %<float%> in "
10105 "declaration specifiers"));
10106 else if (specs->typespec_word == cts_double)
10107 error_at (loc,
10108 ("both %<signed%> and %<double%> in "
10109 "declaration specifiers"));
10110 else if (specs->typespec_word == cts_floatn_nx)
10111 error_at (loc,
10112 ("both %<signed%> and %<_Float%d%s%> in "
10113 "declaration specifiers"),
10114 floatn_nx_types[specs->floatn_nx_idx].n,
10115 (floatn_nx_types[specs->floatn_nx_idx].extended
10116 ? "x"
10117 : ""));
10118 else if (specs->typespec_word == cts_dfloat32)
10119 error_at (loc,
10120 ("both %<signed%> and %<_Decimal32%> in "
10121 "declaration specifiers"));
10122 else if (specs->typespec_word == cts_dfloat64)
10123 error_at (loc,
10124 ("both %<signed%> and %<_Decimal64%> in "
10125 "declaration specifiers"));
10126 else if (specs->typespec_word == cts_dfloat128)
10127 error_at (loc,
10128 ("both %<signed%> and %<_Decimal128%> in "
10129 "declaration specifiers"));
10130 else
10132 specs->signed_p = true;
10133 specs->locations[cdw_signed] = loc;
10135 break;
10136 case RID_UNSIGNED:
10137 dupe = specs->unsigned_p;
10138 if (specs->signed_p)
10139 error_at (loc,
10140 ("both %<signed%> and %<unsigned%> in "
10141 "declaration specifiers"));
10142 else if (specs->typespec_word == cts_auto_type)
10143 error_at (loc,
10144 ("both %<unsigned%> and %<__auto_type%> in "
10145 "declaration specifiers"));
10146 else if (specs->typespec_word == cts_void)
10147 error_at (loc,
10148 ("both %<unsigned%> and %<void%> in "
10149 "declaration specifiers"));
10150 else if (specs->typespec_word == cts_bool)
10151 error_at (loc,
10152 ("both %<unsigned%> and %<_Bool%> in "
10153 "declaration specifiers"));
10154 else if (specs->typespec_word == cts_float)
10155 error_at (loc,
10156 ("both %<unsigned%> and %<float%> in "
10157 "declaration specifiers"));
10158 else if (specs->typespec_word == cts_double)
10159 error_at (loc,
10160 ("both %<unsigned%> and %<double%> in "
10161 "declaration specifiers"));
10162 else if (specs->typespec_word == cts_floatn_nx)
10163 error_at (loc,
10164 ("both %<unsigned%> and %<_Float%d%s%> in "
10165 "declaration specifiers"),
10166 floatn_nx_types[specs->floatn_nx_idx].n,
10167 (floatn_nx_types[specs->floatn_nx_idx].extended
10168 ? "x"
10169 : ""));
10170 else if (specs->typespec_word == cts_dfloat32)
10171 error_at (loc,
10172 ("both %<unsigned%> and %<_Decimal32%> in "
10173 "declaration specifiers"));
10174 else if (specs->typespec_word == cts_dfloat64)
10175 error_at (loc,
10176 ("both %<unsigned%> and %<_Decimal64%> in "
10177 "declaration specifiers"));
10178 else if (specs->typespec_word == cts_dfloat128)
10179 error_at (loc,
10180 ("both %<unsigned%> and %<_Decimal128%> in "
10181 "declaration specifiers"));
10182 else
10184 specs->unsigned_p = true;
10185 specs->locations[cdw_unsigned] = loc;
10187 break;
10188 case RID_COMPLEX:
10189 dupe = specs->complex_p;
10190 if (!in_system_header_at (loc))
10191 pedwarn_c90 (loc, OPT_Wpedantic,
10192 "ISO C90 does not support complex types");
10193 if (specs->typespec_word == cts_auto_type)
10194 error_at (loc,
10195 ("both %<complex%> and %<__auto_type%> in "
10196 "declaration specifiers"));
10197 else if (specs->typespec_word == cts_void)
10198 error_at (loc,
10199 ("both %<complex%> and %<void%> in "
10200 "declaration specifiers"));
10201 else if (specs->typespec_word == cts_bool)
10202 error_at (loc,
10203 ("both %<complex%> and %<_Bool%> in "
10204 "declaration specifiers"));
10205 else if (specs->typespec_word == cts_dfloat32)
10206 error_at (loc,
10207 ("both %<complex%> and %<_Decimal32%> in "
10208 "declaration specifiers"));
10209 else if (specs->typespec_word == cts_dfloat64)
10210 error_at (loc,
10211 ("both %<complex%> and %<_Decimal64%> in "
10212 "declaration specifiers"));
10213 else if (specs->typespec_word == cts_dfloat128)
10214 error_at (loc,
10215 ("both %<complex%> and %<_Decimal128%> in "
10216 "declaration specifiers"));
10217 else if (specs->typespec_word == cts_fract)
10218 error_at (loc,
10219 ("both %<complex%> and %<_Fract%> in "
10220 "declaration specifiers"));
10221 else if (specs->typespec_word == cts_accum)
10222 error_at (loc,
10223 ("both %<complex%> and %<_Accum%> in "
10224 "declaration specifiers"));
10225 else if (specs->saturating_p)
10226 error_at (loc,
10227 ("both %<complex%> and %<_Sat%> in "
10228 "declaration specifiers"));
10229 else
10231 specs->complex_p = true;
10232 specs->locations[cdw_complex] = loc;
10234 break;
10235 case RID_SAT:
10236 dupe = specs->saturating_p;
10237 pedwarn (loc, OPT_Wpedantic,
10238 "ISO C does not support saturating types");
10239 if (specs->typespec_word == cts_int_n)
10241 error_at (loc,
10242 ("both %<_Sat%> and %<__int%d%> in "
10243 "declaration specifiers"),
10244 int_n_data[specs->int_n_idx].bitsize);
10246 else if (specs->typespec_word == cts_auto_type)
10247 error_at (loc,
10248 ("both %<_Sat%> and %<__auto_type%> in "
10249 "declaration specifiers"));
10250 else if (specs->typespec_word == cts_void)
10251 error_at (loc,
10252 ("both %<_Sat%> and %<void%> in "
10253 "declaration specifiers"));
10254 else if (specs->typespec_word == cts_bool)
10255 error_at (loc,
10256 ("both %<_Sat%> and %<_Bool%> in "
10257 "declaration specifiers"));
10258 else if (specs->typespec_word == cts_char)
10259 error_at (loc,
10260 ("both %<_Sat%> and %<char%> in "
10261 "declaration specifiers"));
10262 else if (specs->typespec_word == cts_int)
10263 error_at (loc,
10264 ("both %<_Sat%> and %<int%> in "
10265 "declaration specifiers"));
10266 else if (specs->typespec_word == cts_float)
10267 error_at (loc,
10268 ("both %<_Sat%> and %<float%> in "
10269 "declaration specifiers"));
10270 else if (specs->typespec_word == cts_double)
10271 error_at (loc,
10272 ("both %<_Sat%> and %<double%> in "
10273 "declaration specifiers"));
10274 else if (specs->typespec_word == cts_floatn_nx)
10275 error_at (loc,
10276 ("both %<_Sat%> and %<_Float%d%s%> in "
10277 "declaration specifiers"),
10278 floatn_nx_types[specs->floatn_nx_idx].n,
10279 (floatn_nx_types[specs->floatn_nx_idx].extended
10280 ? "x"
10281 : ""));
10282 else if (specs->typespec_word == cts_dfloat32)
10283 error_at (loc,
10284 ("both %<_Sat%> and %<_Decimal32%> in "
10285 "declaration specifiers"));
10286 else if (specs->typespec_word == cts_dfloat64)
10287 error_at (loc,
10288 ("both %<_Sat%> and %<_Decimal64%> in "
10289 "declaration specifiers"));
10290 else if (specs->typespec_word == cts_dfloat128)
10291 error_at (loc,
10292 ("both %<_Sat%> and %<_Decimal128%> in "
10293 "declaration specifiers"));
10294 else if (specs->complex_p)
10295 error_at (loc,
10296 ("both %<_Sat%> and %<complex%> in "
10297 "declaration specifiers"));
10298 else
10300 specs->saturating_p = true;
10301 specs->locations[cdw_saturating] = loc;
10303 break;
10304 default:
10305 gcc_unreachable ();
10308 if (dupe)
10309 error_at (loc, "duplicate %qE", type);
10311 return specs;
10313 else
10315 /* "void", "_Bool", "char", "int", "float", "double",
10316 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10317 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10318 "__auto_type". */
10319 if (specs->typespec_word != cts_none)
10321 error_at (loc,
10322 "two or more data types in declaration specifiers");
10323 return specs;
10325 switch (i)
10327 case RID_AUTO_TYPE:
10328 if (specs->long_p)
10329 error_at (loc,
10330 ("both %<long%> and %<__auto_type%> in "
10331 "declaration specifiers"));
10332 else if (specs->short_p)
10333 error_at (loc,
10334 ("both %<short%> and %<__auto_type%> in "
10335 "declaration specifiers"));
10336 else if (specs->signed_p)
10337 error_at (loc,
10338 ("both %<signed%> and %<__auto_type%> in "
10339 "declaration specifiers"));
10340 else if (specs->unsigned_p)
10341 error_at (loc,
10342 ("both %<unsigned%> and %<__auto_type%> in "
10343 "declaration specifiers"));
10344 else if (specs->complex_p)
10345 error_at (loc,
10346 ("both %<complex%> and %<__auto_type%> in "
10347 "declaration specifiers"));
10348 else if (specs->saturating_p)
10349 error_at (loc,
10350 ("both %<_Sat%> and %<__auto_type%> in "
10351 "declaration specifiers"));
10352 else
10354 specs->typespec_word = cts_auto_type;
10355 specs->locations[cdw_typespec] = loc;
10357 return specs;
10358 case RID_INT_N_0:
10359 case RID_INT_N_1:
10360 case RID_INT_N_2:
10361 case RID_INT_N_3:
10362 specs->int_n_idx = i - RID_INT_N_0;
10363 if (!in_system_header_at (input_location))
10364 pedwarn (loc, OPT_Wpedantic,
10365 "ISO C does not support %<__int%d%> types",
10366 int_n_data[specs->int_n_idx].bitsize);
10368 if (specs->long_p)
10369 error_at (loc,
10370 ("both %<__int%d%> and %<long%> in "
10371 "declaration specifiers"),
10372 int_n_data[specs->int_n_idx].bitsize);
10373 else if (specs->saturating_p)
10374 error_at (loc,
10375 ("both %<_Sat%> and %<__int%d%> in "
10376 "declaration specifiers"),
10377 int_n_data[specs->int_n_idx].bitsize);
10378 else if (specs->short_p)
10379 error_at (loc,
10380 ("both %<__int%d%> and %<short%> in "
10381 "declaration specifiers"),
10382 int_n_data[specs->int_n_idx].bitsize);
10383 else if (! int_n_enabled_p[specs->int_n_idx])
10385 specs->typespec_word = cts_int_n;
10386 error_at (loc,
10387 "%<__int%d%> is not supported on this target",
10388 int_n_data[specs->int_n_idx].bitsize);
10390 else
10392 specs->typespec_word = cts_int_n;
10393 specs->locations[cdw_typespec] = loc;
10395 return specs;
10396 case RID_VOID:
10397 if (specs->long_p)
10398 error_at (loc,
10399 ("both %<long%> and %<void%> in "
10400 "declaration specifiers"));
10401 else if (specs->short_p)
10402 error_at (loc,
10403 ("both %<short%> and %<void%> in "
10404 "declaration specifiers"));
10405 else if (specs->signed_p)
10406 error_at (loc,
10407 ("both %<signed%> and %<void%> in "
10408 "declaration specifiers"));
10409 else if (specs->unsigned_p)
10410 error_at (loc,
10411 ("both %<unsigned%> and %<void%> in "
10412 "declaration specifiers"));
10413 else if (specs->complex_p)
10414 error_at (loc,
10415 ("both %<complex%> and %<void%> in "
10416 "declaration specifiers"));
10417 else if (specs->saturating_p)
10418 error_at (loc,
10419 ("both %<_Sat%> and %<void%> in "
10420 "declaration specifiers"));
10421 else
10423 specs->typespec_word = cts_void;
10424 specs->locations[cdw_typespec] = loc;
10426 return specs;
10427 case RID_BOOL:
10428 if (!in_system_header_at (loc))
10429 pedwarn_c90 (loc, OPT_Wpedantic,
10430 "ISO C90 does not support boolean types");
10431 if (specs->long_p)
10432 error_at (loc,
10433 ("both %<long%> and %<_Bool%> in "
10434 "declaration specifiers"));
10435 else if (specs->short_p)
10436 error_at (loc,
10437 ("both %<short%> and %<_Bool%> in "
10438 "declaration specifiers"));
10439 else if (specs->signed_p)
10440 error_at (loc,
10441 ("both %<signed%> and %<_Bool%> in "
10442 "declaration specifiers"));
10443 else if (specs->unsigned_p)
10444 error_at (loc,
10445 ("both %<unsigned%> and %<_Bool%> in "
10446 "declaration specifiers"));
10447 else if (specs->complex_p)
10448 error_at (loc,
10449 ("both %<complex%> and %<_Bool%> in "
10450 "declaration specifiers"));
10451 else if (specs->saturating_p)
10452 error_at (loc,
10453 ("both %<_Sat%> and %<_Bool%> in "
10454 "declaration specifiers"));
10455 else
10457 specs->typespec_word = cts_bool;
10458 specs->locations[cdw_typespec] = loc;
10460 return specs;
10461 case RID_CHAR:
10462 if (specs->long_p)
10463 error_at (loc,
10464 ("both %<long%> and %<char%> in "
10465 "declaration specifiers"));
10466 else if (specs->short_p)
10467 error_at (loc,
10468 ("both %<short%> and %<char%> in "
10469 "declaration specifiers"));
10470 else if (specs->saturating_p)
10471 error_at (loc,
10472 ("both %<_Sat%> and %<char%> in "
10473 "declaration specifiers"));
10474 else
10476 specs->typespec_word = cts_char;
10477 specs->locations[cdw_typespec] = loc;
10479 return specs;
10480 case RID_INT:
10481 if (specs->saturating_p)
10482 error_at (loc,
10483 ("both %<_Sat%> and %<int%> in "
10484 "declaration specifiers"));
10485 else
10487 specs->typespec_word = cts_int;
10488 specs->locations[cdw_typespec] = loc;
10490 return specs;
10491 case RID_FLOAT:
10492 if (specs->long_p)
10493 error_at (loc,
10494 ("both %<long%> and %<float%> in "
10495 "declaration specifiers"));
10496 else if (specs->short_p)
10497 error_at (loc,
10498 ("both %<short%> and %<float%> in "
10499 "declaration specifiers"));
10500 else if (specs->signed_p)
10501 error_at (loc,
10502 ("both %<signed%> and %<float%> in "
10503 "declaration specifiers"));
10504 else if (specs->unsigned_p)
10505 error_at (loc,
10506 ("both %<unsigned%> and %<float%> in "
10507 "declaration specifiers"));
10508 else if (specs->saturating_p)
10509 error_at (loc,
10510 ("both %<_Sat%> and %<float%> in "
10511 "declaration specifiers"));
10512 else
10514 specs->typespec_word = cts_float;
10515 specs->locations[cdw_typespec] = loc;
10517 return specs;
10518 case RID_DOUBLE:
10519 if (specs->long_long_p)
10520 error_at (loc,
10521 ("both %<long long%> and %<double%> in "
10522 "declaration specifiers"));
10523 else if (specs->short_p)
10524 error_at (loc,
10525 ("both %<short%> and %<double%> in "
10526 "declaration specifiers"));
10527 else if (specs->signed_p)
10528 error_at (loc,
10529 ("both %<signed%> and %<double%> in "
10530 "declaration specifiers"));
10531 else if (specs->unsigned_p)
10532 error_at (loc,
10533 ("both %<unsigned%> and %<double%> in "
10534 "declaration specifiers"));
10535 else if (specs->saturating_p)
10536 error_at (loc,
10537 ("both %<_Sat%> and %<double%> in "
10538 "declaration specifiers"));
10539 else
10541 specs->typespec_word = cts_double;
10542 specs->locations[cdw_typespec] = loc;
10544 return specs;
10545 CASE_RID_FLOATN_NX:
10546 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10547 if (!in_system_header_at (input_location))
10548 pedwarn (loc, OPT_Wpedantic,
10549 "ISO C does not support the %<_Float%d%s%> type",
10550 floatn_nx_types[specs->floatn_nx_idx].n,
10551 (floatn_nx_types[specs->floatn_nx_idx].extended
10552 ? "x"
10553 : ""));
10555 if (specs->long_p)
10556 error_at (loc,
10557 ("both %<long%> and %<_Float%d%s%> in "
10558 "declaration specifiers"),
10559 floatn_nx_types[specs->floatn_nx_idx].n,
10560 (floatn_nx_types[specs->floatn_nx_idx].extended
10561 ? "x"
10562 : ""));
10563 else if (specs->short_p)
10564 error_at (loc,
10565 ("both %<short%> and %<_Float%d%s%> in "
10566 "declaration specifiers"),
10567 floatn_nx_types[specs->floatn_nx_idx].n,
10568 (floatn_nx_types[specs->floatn_nx_idx].extended
10569 ? "x"
10570 : ""));
10571 else if (specs->signed_p)
10572 error_at (loc,
10573 ("both %<signed%> and %<_Float%d%s%> in "
10574 "declaration specifiers"),
10575 floatn_nx_types[specs->floatn_nx_idx].n,
10576 (floatn_nx_types[specs->floatn_nx_idx].extended
10577 ? "x"
10578 : ""));
10579 else if (specs->unsigned_p)
10580 error_at (loc,
10581 ("both %<unsigned%> and %<_Float%d%s%> in "
10582 "declaration specifiers"),
10583 floatn_nx_types[specs->floatn_nx_idx].n,
10584 (floatn_nx_types[specs->floatn_nx_idx].extended
10585 ? "x"
10586 : ""));
10587 else if (specs->saturating_p)
10588 error_at (loc,
10589 ("both %<_Sat%> and %<_Float%d%s%> in "
10590 "declaration specifiers"),
10591 floatn_nx_types[specs->floatn_nx_idx].n,
10592 (floatn_nx_types[specs->floatn_nx_idx].extended
10593 ? "x"
10594 : ""));
10595 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10597 specs->typespec_word = cts_floatn_nx;
10598 error_at (loc,
10599 "%<_Float%d%s%> is not supported on this target",
10600 floatn_nx_types[specs->floatn_nx_idx].n,
10601 (floatn_nx_types[specs->floatn_nx_idx].extended
10602 ? "x"
10603 : ""));
10605 else
10607 specs->typespec_word = cts_floatn_nx;
10608 specs->locations[cdw_typespec] = loc;
10610 return specs;
10611 case RID_DFLOAT32:
10612 case RID_DFLOAT64:
10613 case RID_DFLOAT128:
10615 const char *str;
10616 if (i == RID_DFLOAT32)
10617 str = "_Decimal32";
10618 else if (i == RID_DFLOAT64)
10619 str = "_Decimal64";
10620 else
10621 str = "_Decimal128";
10622 if (specs->long_long_p)
10623 error_at (loc,
10624 ("both %<long long%> and %qs in "
10625 "declaration specifiers"),
10626 str);
10627 if (specs->long_p)
10628 error_at (loc,
10629 ("both %<long%> and %qs in "
10630 "declaration specifiers"),
10631 str);
10632 else if (specs->short_p)
10633 error_at (loc,
10634 ("both %<short%> and %qs in "
10635 "declaration specifiers"),
10636 str);
10637 else if (specs->signed_p)
10638 error_at (loc,
10639 ("both %<signed%> and %qs in "
10640 "declaration specifiers"),
10641 str);
10642 else if (specs->unsigned_p)
10643 error_at (loc,
10644 ("both %<unsigned%> and %qs in "
10645 "declaration specifiers"),
10646 str);
10647 else if (specs->complex_p)
10648 error_at (loc,
10649 ("both %<complex%> and %qs in "
10650 "declaration specifiers"),
10651 str);
10652 else if (specs->saturating_p)
10653 error_at (loc,
10654 ("both %<_Sat%> and %qs in "
10655 "declaration specifiers"),
10656 str);
10657 else if (i == RID_DFLOAT32)
10658 specs->typespec_word = cts_dfloat32;
10659 else if (i == RID_DFLOAT64)
10660 specs->typespec_word = cts_dfloat64;
10661 else
10662 specs->typespec_word = cts_dfloat128;
10663 specs->locations[cdw_typespec] = loc;
10665 if (!targetm.decimal_float_supported_p ())
10666 error_at (loc,
10667 ("decimal floating point not supported "
10668 "for this target"));
10669 pedwarn (loc, OPT_Wpedantic,
10670 "ISO C does not support decimal floating point");
10671 return specs;
10672 case RID_FRACT:
10673 case RID_ACCUM:
10675 const char *str;
10676 if (i == RID_FRACT)
10677 str = "_Fract";
10678 else
10679 str = "_Accum";
10680 if (specs->complex_p)
10681 error_at (loc,
10682 ("both %<complex%> and %qs in "
10683 "declaration specifiers"),
10684 str);
10685 else if (i == RID_FRACT)
10686 specs->typespec_word = cts_fract;
10687 else
10688 specs->typespec_word = cts_accum;
10689 specs->locations[cdw_typespec] = loc;
10691 if (!targetm.fixed_point_supported_p ())
10692 error_at (loc,
10693 "fixed-point types not supported for this target");
10694 pedwarn (loc, OPT_Wpedantic,
10695 "ISO C does not support fixed-point types");
10696 return specs;
10697 default:
10698 /* ObjC reserved word "id", handled below. */
10699 break;
10704 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10705 form of ObjC type, cases such as "int" and "long" being handled
10706 above), a TYPE (struct, union, enum and typeof specifiers) or an
10707 ERROR_MARK. In none of these cases may there have previously
10708 been any type specifiers. */
10709 if (specs->type || specs->typespec_word != cts_none
10710 || specs->long_p || specs->short_p || specs->signed_p
10711 || specs->unsigned_p || specs->complex_p)
10712 error_at (loc, "two or more data types in declaration specifiers");
10713 else if (TREE_CODE (type) == TYPE_DECL)
10715 if (TREE_TYPE (type) == error_mark_node)
10716 ; /* Allow the type to default to int to avoid cascading errors. */
10717 else
10719 specs->type = TREE_TYPE (type);
10720 specs->decl_attr = DECL_ATTRIBUTES (type);
10721 specs->typedef_p = true;
10722 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10723 specs->locations[cdw_typedef] = loc;
10725 /* If this typedef name is defined in a struct, then a C++
10726 lookup would return a different value. */
10727 if (warn_cxx_compat
10728 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10729 warning_at (loc, OPT_Wc___compat,
10730 "C++ lookup of %qD would return a field, not a type",
10731 type);
10733 /* If we are parsing a struct, record that a struct field
10734 used a typedef. */
10735 if (warn_cxx_compat && struct_parse_info != NULL)
10736 struct_parse_info->typedefs_seen.safe_push (type);
10739 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10741 tree t = lookup_name (type);
10742 if (!t || TREE_CODE (t) != TYPE_DECL)
10743 error_at (loc, "%qE fails to be a typedef or built in type", type);
10744 else if (TREE_TYPE (t) == error_mark_node)
10746 else
10748 specs->type = TREE_TYPE (t);
10749 specs->locations[cdw_typespec] = loc;
10752 else
10754 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10756 specs->typedef_p = true;
10757 specs->locations[cdw_typedef] = loc;
10758 if (spec.expr)
10760 if (specs->expr)
10761 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10762 specs->expr, spec.expr);
10763 else
10764 specs->expr = spec.expr;
10765 specs->expr_const_operands &= spec.expr_const_operands;
10768 specs->type = type;
10771 return specs;
10774 /* Add the storage class specifier or function specifier SCSPEC to the
10775 declaration specifiers SPECS, returning SPECS. */
10777 struct c_declspecs *
10778 declspecs_add_scspec (source_location loc,
10779 struct c_declspecs *specs,
10780 tree scspec)
10782 enum rid i;
10783 enum c_storage_class n = csc_none;
10784 bool dupe = false;
10785 specs->declspecs_seen_p = true;
10786 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10787 && C_IS_RESERVED_WORD (scspec));
10788 i = C_RID_CODE (scspec);
10789 if (specs->non_sc_seen_p)
10790 warning (OPT_Wold_style_declaration,
10791 "%qE is not at beginning of declaration", scspec);
10792 switch (i)
10794 case RID_INLINE:
10795 /* C99 permits duplicate inline. Although of doubtful utility,
10796 it seems simplest to permit it in gnu89 mode as well, as
10797 there is also little utility in maintaining this as a
10798 difference between gnu89 and C99 inline. */
10799 dupe = false;
10800 specs->inline_p = true;
10801 specs->locations[cdw_inline] = loc;
10802 break;
10803 case RID_NORETURN:
10804 /* Duplicate _Noreturn is permitted. */
10805 dupe = false;
10806 specs->noreturn_p = true;
10807 specs->locations[cdw_noreturn] = loc;
10808 break;
10809 case RID_THREAD:
10810 dupe = specs->thread_p;
10811 if (specs->storage_class == csc_auto)
10812 error ("%qE used with %<auto%>", scspec);
10813 else if (specs->storage_class == csc_register)
10814 error ("%qE used with %<register%>", scspec);
10815 else if (specs->storage_class == csc_typedef)
10816 error ("%qE used with %<typedef%>", scspec);
10817 else
10819 specs->thread_p = true;
10820 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10821 "__thread") == 0);
10822 /* A diagnostic is not required for the use of this
10823 identifier in the implementation namespace; only diagnose
10824 it for the C11 spelling because of existing code using
10825 the other spelling. */
10826 if (!specs->thread_gnu_p)
10828 if (flag_isoc99)
10829 pedwarn_c99 (loc, OPT_Wpedantic,
10830 "ISO C99 does not support %qE", scspec);
10831 else
10832 pedwarn_c99 (loc, OPT_Wpedantic,
10833 "ISO C90 does not support %qE", scspec);
10835 specs->locations[cdw_thread] = loc;
10837 break;
10838 case RID_AUTO:
10839 n = csc_auto;
10840 break;
10841 case RID_EXTERN:
10842 n = csc_extern;
10843 /* Diagnose "__thread extern". */
10844 if (specs->thread_p && specs->thread_gnu_p)
10845 error ("%<__thread%> before %<extern%>");
10846 break;
10847 case RID_REGISTER:
10848 n = csc_register;
10849 break;
10850 case RID_STATIC:
10851 n = csc_static;
10852 /* Diagnose "__thread static". */
10853 if (specs->thread_p && specs->thread_gnu_p)
10854 error ("%<__thread%> before %<static%>");
10855 break;
10856 case RID_TYPEDEF:
10857 n = csc_typedef;
10858 break;
10859 default:
10860 gcc_unreachable ();
10862 if (n != csc_none && n == specs->storage_class)
10863 dupe = true;
10864 if (dupe)
10866 if (i == RID_THREAD)
10867 error ("duplicate %<_Thread_local%> or %<__thread%>");
10868 else
10869 error ("duplicate %qE", scspec);
10871 if (n != csc_none)
10873 if (specs->storage_class != csc_none && n != specs->storage_class)
10875 error ("multiple storage classes in declaration specifiers");
10877 else
10879 specs->storage_class = n;
10880 specs->locations[cdw_storage_class] = loc;
10881 if (n != csc_extern && n != csc_static && specs->thread_p)
10883 error ("%qs used with %qE",
10884 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10885 scspec);
10886 specs->thread_p = false;
10890 return specs;
10893 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10894 returning SPECS. */
10896 struct c_declspecs *
10897 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10899 specs->attrs = chainon (attrs, specs->attrs);
10900 specs->locations[cdw_attributes] = loc;
10901 specs->declspecs_seen_p = true;
10902 return specs;
10905 /* Add an _Alignas specifier (expression ALIGN, or type whose
10906 alignment is ALIGN) to the declaration specifiers SPECS, returning
10907 SPECS. */
10908 struct c_declspecs *
10909 declspecs_add_alignas (source_location loc,
10910 struct c_declspecs *specs, tree align)
10912 int align_log;
10913 specs->alignas_p = true;
10914 specs->locations[cdw_alignas] = loc;
10915 if (align == error_mark_node)
10916 return specs;
10917 align_log = check_user_alignment (align, true);
10918 if (align_log > specs->align_log)
10919 specs->align_log = align_log;
10920 return specs;
10923 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10924 specifiers with any other type specifier to determine the resulting
10925 type. This is where ISO C checks on complex types are made, since
10926 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10927 double". */
10929 struct c_declspecs *
10930 finish_declspecs (struct c_declspecs *specs)
10932 /* If a type was specified as a whole, we have no modifiers and are
10933 done. */
10934 if (specs->type != NULL_TREE)
10936 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10937 && !specs->signed_p && !specs->unsigned_p
10938 && !specs->complex_p);
10940 /* Set a dummy type. */
10941 if (TREE_CODE (specs->type) == ERROR_MARK)
10942 specs->type = integer_type_node;
10943 return specs;
10946 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10947 has been specified, treat it as "int" unless "_Complex" is
10948 present and there are no other specifiers. If we just have
10949 "_Complex", it is equivalent to "_Complex double", but e.g.
10950 "_Complex short" is equivalent to "_Complex short int". */
10951 if (specs->typespec_word == cts_none)
10953 if (specs->saturating_p)
10955 error_at (specs->locations[cdw_saturating],
10956 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10957 if (!targetm.fixed_point_supported_p ())
10958 error_at (specs->locations[cdw_saturating],
10959 "fixed-point types not supported for this target");
10960 specs->typespec_word = cts_fract;
10962 else if (specs->long_p || specs->short_p
10963 || specs->signed_p || specs->unsigned_p)
10965 specs->typespec_word = cts_int;
10967 else if (specs->complex_p)
10969 specs->typespec_word = cts_double;
10970 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10971 "ISO C does not support plain %<complex%> meaning "
10972 "%<double complex%>");
10974 else
10976 specs->typespec_word = cts_int;
10977 specs->default_int_p = true;
10978 /* We don't diagnose this here because grokdeclarator will
10979 give more specific diagnostics according to whether it is
10980 a function definition. */
10984 /* If "signed" was specified, record this to distinguish "int" and
10985 "signed int" in the case of a bit-field with
10986 -funsigned-bitfields. */
10987 specs->explicit_signed_p = specs->signed_p;
10989 /* Now compute the actual type. */
10990 switch (specs->typespec_word)
10992 case cts_auto_type:
10993 gcc_assert (!specs->long_p && !specs->short_p
10994 && !specs->signed_p && !specs->unsigned_p
10995 && !specs->complex_p);
10996 /* Type to be filled in later. */
10997 break;
10998 case cts_void:
10999 gcc_assert (!specs->long_p && !specs->short_p
11000 && !specs->signed_p && !specs->unsigned_p
11001 && !specs->complex_p);
11002 specs->type = void_type_node;
11003 break;
11004 case cts_bool:
11005 gcc_assert (!specs->long_p && !specs->short_p
11006 && !specs->signed_p && !specs->unsigned_p
11007 && !specs->complex_p);
11008 specs->type = boolean_type_node;
11009 break;
11010 case cts_char:
11011 gcc_assert (!specs->long_p && !specs->short_p);
11012 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11013 if (specs->signed_p)
11014 specs->type = signed_char_type_node;
11015 else if (specs->unsigned_p)
11016 specs->type = unsigned_char_type_node;
11017 else
11018 specs->type = char_type_node;
11019 if (specs->complex_p)
11021 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11022 "ISO C does not support complex integer types");
11023 specs->type = build_complex_type (specs->type);
11025 break;
11026 case cts_int_n:
11027 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11028 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11029 if (! int_n_enabled_p[specs->int_n_idx])
11030 specs->type = integer_type_node;
11031 else
11032 specs->type = (specs->unsigned_p
11033 ? int_n_trees[specs->int_n_idx].unsigned_type
11034 : int_n_trees[specs->int_n_idx].signed_type);
11035 if (specs->complex_p)
11037 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11038 "ISO C does not support complex integer types");
11039 specs->type = build_complex_type (specs->type);
11041 break;
11042 case cts_int:
11043 gcc_assert (!(specs->long_p && specs->short_p));
11044 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11045 if (specs->long_long_p)
11046 specs->type = (specs->unsigned_p
11047 ? long_long_unsigned_type_node
11048 : long_long_integer_type_node);
11049 else if (specs->long_p)
11050 specs->type = (specs->unsigned_p
11051 ? long_unsigned_type_node
11052 : long_integer_type_node);
11053 else if (specs->short_p)
11054 specs->type = (specs->unsigned_p
11055 ? short_unsigned_type_node
11056 : short_integer_type_node);
11057 else
11058 specs->type = (specs->unsigned_p
11059 ? unsigned_type_node
11060 : integer_type_node);
11061 if (specs->complex_p)
11063 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11064 "ISO C does not support complex integer types");
11065 specs->type = build_complex_type (specs->type);
11067 break;
11068 case cts_float:
11069 gcc_assert (!specs->long_p && !specs->short_p
11070 && !specs->signed_p && !specs->unsigned_p);
11071 specs->type = (specs->complex_p
11072 ? complex_float_type_node
11073 : float_type_node);
11074 break;
11075 case cts_double:
11076 gcc_assert (!specs->long_long_p && !specs->short_p
11077 && !specs->signed_p && !specs->unsigned_p);
11078 if (specs->long_p)
11080 specs->type = (specs->complex_p
11081 ? complex_long_double_type_node
11082 : long_double_type_node);
11084 else
11086 specs->type = (specs->complex_p
11087 ? complex_double_type_node
11088 : double_type_node);
11090 break;
11091 case cts_floatn_nx:
11092 gcc_assert (!specs->long_p && !specs->short_p
11093 && !specs->signed_p && !specs->unsigned_p);
11094 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11095 specs->type = integer_type_node;
11096 else if (specs->complex_p)
11097 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11098 else
11099 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11100 break;
11101 case cts_dfloat32:
11102 case cts_dfloat64:
11103 case cts_dfloat128:
11104 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11105 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11106 if (specs->typespec_word == cts_dfloat32)
11107 specs->type = dfloat32_type_node;
11108 else if (specs->typespec_word == cts_dfloat64)
11109 specs->type = dfloat64_type_node;
11110 else
11111 specs->type = dfloat128_type_node;
11112 break;
11113 case cts_fract:
11114 gcc_assert (!specs->complex_p);
11115 if (!targetm.fixed_point_supported_p ())
11116 specs->type = integer_type_node;
11117 else if (specs->saturating_p)
11119 if (specs->long_long_p)
11120 specs->type = specs->unsigned_p
11121 ? sat_unsigned_long_long_fract_type_node
11122 : sat_long_long_fract_type_node;
11123 else if (specs->long_p)
11124 specs->type = specs->unsigned_p
11125 ? sat_unsigned_long_fract_type_node
11126 : sat_long_fract_type_node;
11127 else if (specs->short_p)
11128 specs->type = specs->unsigned_p
11129 ? sat_unsigned_short_fract_type_node
11130 : sat_short_fract_type_node;
11131 else
11132 specs->type = specs->unsigned_p
11133 ? sat_unsigned_fract_type_node
11134 : sat_fract_type_node;
11136 else
11138 if (specs->long_long_p)
11139 specs->type = specs->unsigned_p
11140 ? unsigned_long_long_fract_type_node
11141 : long_long_fract_type_node;
11142 else if (specs->long_p)
11143 specs->type = specs->unsigned_p
11144 ? unsigned_long_fract_type_node
11145 : long_fract_type_node;
11146 else if (specs->short_p)
11147 specs->type = specs->unsigned_p
11148 ? unsigned_short_fract_type_node
11149 : short_fract_type_node;
11150 else
11151 specs->type = specs->unsigned_p
11152 ? unsigned_fract_type_node
11153 : fract_type_node;
11155 break;
11156 case cts_accum:
11157 gcc_assert (!specs->complex_p);
11158 if (!targetm.fixed_point_supported_p ())
11159 specs->type = integer_type_node;
11160 else if (specs->saturating_p)
11162 if (specs->long_long_p)
11163 specs->type = specs->unsigned_p
11164 ? sat_unsigned_long_long_accum_type_node
11165 : sat_long_long_accum_type_node;
11166 else if (specs->long_p)
11167 specs->type = specs->unsigned_p
11168 ? sat_unsigned_long_accum_type_node
11169 : sat_long_accum_type_node;
11170 else if (specs->short_p)
11171 specs->type = specs->unsigned_p
11172 ? sat_unsigned_short_accum_type_node
11173 : sat_short_accum_type_node;
11174 else
11175 specs->type = specs->unsigned_p
11176 ? sat_unsigned_accum_type_node
11177 : sat_accum_type_node;
11179 else
11181 if (specs->long_long_p)
11182 specs->type = specs->unsigned_p
11183 ? unsigned_long_long_accum_type_node
11184 : long_long_accum_type_node;
11185 else if (specs->long_p)
11186 specs->type = specs->unsigned_p
11187 ? unsigned_long_accum_type_node
11188 : long_accum_type_node;
11189 else if (specs->short_p)
11190 specs->type = specs->unsigned_p
11191 ? unsigned_short_accum_type_node
11192 : short_accum_type_node;
11193 else
11194 specs->type = specs->unsigned_p
11195 ? unsigned_accum_type_node
11196 : accum_type_node;
11198 break;
11199 default:
11200 gcc_unreachable ();
11203 return specs;
11206 /* Perform final processing on one file scope's declarations (or the
11207 external scope's declarations), GLOBALS. */
11209 static void
11210 c_write_global_declarations_1 (tree globals)
11212 tree decl;
11213 bool reconsider;
11215 /* Process the decls in the order they were written. */
11216 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11218 /* Check for used but undefined static functions using the C
11219 standard's definition of "used", and set TREE_NO_WARNING so
11220 that check_global_declaration doesn't repeat the check. */
11221 if (TREE_CODE (decl) == FUNCTION_DECL
11222 && DECL_INITIAL (decl) == NULL_TREE
11223 && DECL_EXTERNAL (decl)
11224 && !TREE_PUBLIC (decl))
11226 if (C_DECL_USED (decl))
11228 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11229 TREE_NO_WARNING (decl) = 1;
11231 /* For -Wunused-function warn about unused static prototypes. */
11232 else if (warn_unused_function
11233 && ! DECL_ARTIFICIAL (decl)
11234 && ! TREE_NO_WARNING (decl))
11236 warning (OPT_Wunused_function,
11237 "%q+F declared %<static%> but never defined", decl);
11238 TREE_NO_WARNING (decl) = 1;
11242 wrapup_global_declaration_1 (decl);
11247 reconsider = false;
11248 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11249 reconsider |= wrapup_global_declaration_2 (decl);
11251 while (reconsider);
11254 /* Callback to collect a source_ref from a DECL. */
11256 static void
11257 collect_source_ref_cb (tree decl)
11259 if (!DECL_IS_BUILTIN (decl))
11260 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11263 /* Preserve the external declarations scope across a garbage collect. */
11264 static GTY(()) tree ext_block;
11266 /* Collect all references relevant to SOURCE_FILE. */
11268 static void
11269 collect_all_refs (const char *source_file)
11271 tree t;
11272 unsigned i;
11274 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11275 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11277 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11280 /* Iterate over all global declarations and call CALLBACK. */
11282 static void
11283 for_each_global_decl (void (*callback) (tree decl))
11285 tree t;
11286 tree decls;
11287 tree decl;
11288 unsigned i;
11290 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11292 decls = DECL_INITIAL (t);
11293 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11294 callback (decl);
11297 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11298 callback (decl);
11301 /* Perform any final parser cleanups and generate initial debugging
11302 information. */
11304 void
11305 c_parse_final_cleanups (void)
11307 tree t;
11308 unsigned i;
11310 /* We don't want to do this if generating a PCH. */
11311 if (pch_file)
11312 return;
11314 timevar_stop (TV_PHASE_PARSING);
11315 timevar_start (TV_PHASE_DEFERRED);
11317 /* Do the Objective-C stuff. This is where all the Objective-C
11318 module stuff gets generated (symtab, class/protocol/selector
11319 lists etc). */
11320 if (c_dialect_objc ())
11321 objc_write_global_declarations ();
11323 /* Close the external scope. */
11324 ext_block = pop_scope ();
11325 external_scope = 0;
11326 gcc_assert (!current_scope);
11328 /* Handle -fdump-ada-spec[-slim]. */
11329 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11331 /* Build a table of files to generate specs for */
11332 if (flag_dump_ada_spec_slim)
11333 collect_source_ref (main_input_filename);
11334 else
11335 for_each_global_decl (collect_source_ref_cb);
11337 dump_ada_specs (collect_all_refs, NULL);
11340 /* Process all file scopes in this compilation, and the external_scope,
11341 through wrapup_global_declarations. */
11342 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11343 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11344 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11346 timevar_stop (TV_PHASE_DEFERRED);
11347 timevar_start (TV_PHASE_PARSING);
11349 ext_block = NULL;
11352 /* Register reserved keyword WORD as qualifier for address space AS. */
11354 void
11355 c_register_addr_space (const char *word, addr_space_t as)
11357 int rid = RID_FIRST_ADDR_SPACE + as;
11358 tree id;
11360 /* Address space qualifiers are only supported
11361 in C with GNU extensions enabled. */
11362 if (c_dialect_objc () || flag_no_asm)
11363 return;
11365 id = get_identifier (word);
11366 C_SET_RID_CODE (id, rid);
11367 C_IS_RESERVED_WORD (id) = 1;
11368 ridpointers [rid] = id;
11371 /* Return identifier to look up for omp declare reduction. */
11373 tree
11374 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11376 const char *p = NULL;
11377 switch (reduction_code)
11379 case PLUS_EXPR: p = "+"; break;
11380 case MULT_EXPR: p = "*"; break;
11381 case MINUS_EXPR: p = "-"; break;
11382 case BIT_AND_EXPR: p = "&"; break;
11383 case BIT_XOR_EXPR: p = "^"; break;
11384 case BIT_IOR_EXPR: p = "|"; break;
11385 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11386 case TRUTH_ORIF_EXPR: p = "||"; break;
11387 case MIN_EXPR: p = "min"; break;
11388 case MAX_EXPR: p = "max"; break;
11389 default:
11390 break;
11393 if (p == NULL)
11395 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11396 return error_mark_node;
11397 p = IDENTIFIER_POINTER (reduction_id);
11400 const char prefix[] = "omp declare reduction ";
11401 size_t lenp = sizeof (prefix);
11402 size_t len = strlen (p);
11403 char *name = XALLOCAVEC (char, lenp + len);
11404 memcpy (name, prefix, lenp - 1);
11405 memcpy (name + lenp - 1, p, len + 1);
11406 return get_identifier (name);
11409 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11410 VAR_DECL, bind it into the current scope and return it. */
11412 tree
11413 c_omp_reduction_decl (tree reduction_id)
11415 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11416 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11417 return b->decl;
11419 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11420 reduction_id, integer_type_node);
11421 DECL_ARTIFICIAL (decl) = 1;
11422 DECL_EXTERNAL (decl) = 1;
11423 TREE_STATIC (decl) = 1;
11424 TREE_PUBLIC (decl) = 0;
11425 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11426 return decl;
11429 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11431 tree
11432 c_omp_reduction_lookup (tree reduction_id, tree type)
11434 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11435 while (b)
11437 tree t;
11438 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11439 if (comptypes (TREE_PURPOSE (t), type))
11440 return TREE_VALUE (t);
11441 b = b->shadowed;
11443 return error_mark_node;
11446 /* Helper function called via walk_tree, to diagnose invalid
11447 #pragma omp declare reduction combiners or initializers. */
11449 tree
11450 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11452 tree *vars = (tree *) data;
11453 if (SSA_VAR_P (*tp)
11454 && !DECL_ARTIFICIAL (*tp)
11455 && *tp != vars[0]
11456 && *tp != vars[1])
11458 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11459 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11460 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11461 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11462 *tp);
11463 else
11464 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11465 "to variable %qD which is not %<omp_priv%> nor "
11466 "%<omp_orig%>",
11467 *tp);
11468 return *tp;
11470 return NULL_TREE;
11473 #include "gt-c-c-decl.h"