2017-12-01 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c / c-decl.c
blob56c63d80ec4b0407872d493e6704a48bcd4c051e
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #define INCLUDE_UNIQUE_PTR
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
60 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
61 enum decl_context
62 { NORMAL, /* Ordinary declaration */
63 FUNCDEF, /* Function definition */
64 PARM, /* Declaration of parm before function body */
65 FIELD, /* Declaration inside struct or union */
66 TYPENAME}; /* Typename (inside cast or sizeof) */
68 /* States indicating how grokdeclarator() should handle declspecs marked
69 with __attribute__((deprecated)). An object declared as
70 __attribute__((deprecated)) suppresses warnings of uses of other
71 deprecated items. */
73 enum deprecated_states {
74 DEPRECATED_NORMAL,
75 DEPRECATED_SUPPRESS
79 /* Nonzero if we have seen an invalid cross reference
80 to a struct, union, or enum, but not yet printed the message. */
81 tree pending_invalid_xref;
83 /* File and line to appear in the eventual error message. */
84 location_t pending_invalid_xref_location;
86 /* The file and line that the prototype came from if this is an
87 old-style definition; used for diagnostics in
88 store_parm_decls_oldstyle. */
90 static location_t current_function_prototype_locus;
92 /* Whether this prototype was built-in. */
94 static bool current_function_prototype_built_in;
96 /* The argument type information of this prototype. */
98 static tree current_function_prototype_arg_types;
100 /* The argument information structure for the function currently being
101 defined. */
103 static struct c_arg_info *current_function_arg_info;
105 /* The obstack on which parser and related data structures, which are
106 not live beyond their top-level declaration or definition, are
107 allocated. */
108 struct obstack parser_obstack;
110 /* The current statement tree. */
112 static GTY(()) struct stmt_tree_s c_stmt_tree;
114 /* State saving variables. */
115 tree c_break_label;
116 tree c_cont_label;
118 /* A list of decls to be made automatically visible in each file scope. */
119 static GTY(()) tree visible_builtins;
121 /* Set to 0 at beginning of a function definition, set to 1 if
122 a return statement that specifies a return value is seen. */
124 int current_function_returns_value;
126 /* Set to 0 at beginning of a function definition, set to 1 if
127 a return statement with no argument is seen. */
129 int current_function_returns_null;
131 /* Set to 0 at beginning of a function definition, set to 1 if
132 a call to a noreturn function is seen. */
134 int current_function_returns_abnormally;
136 /* Set to nonzero by `grokdeclarator' for a function
137 whose return type is defaulted, if warnings for this are desired. */
139 static int warn_about_return_type;
141 /* Nonzero when the current toplevel function contains a declaration
142 of a nested function which is never defined. */
144 static bool undef_nested_function;
146 /* If non-zero, implicit "omp declare target" attribute is added into the
147 attribute lists. */
148 int current_omp_declare_target_attribute;
150 /* Each c_binding structure describes one binding of an identifier to
151 a decl. All the decls in a scope - irrespective of namespace - are
152 chained together by the ->prev field, which (as the name implies)
153 runs in reverse order. All the decls in a given namespace bound to
154 a given identifier are chained by the ->shadowed field, which runs
155 from inner to outer scopes.
157 The ->decl field usually points to a DECL node, but there are two
158 exceptions. In the namespace of type tags, the bound entity is a
159 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
160 identifier is encountered, it is bound to error_mark_node to
161 suppress further errors about that identifier in the current
162 function.
164 The ->u.type field stores the type of the declaration in this scope;
165 if NULL, the type is the type of the ->decl field. This is only of
166 relevance for objects with external or internal linkage which may
167 be redeclared in inner scopes, forming composite types that only
168 persist for the duration of those scopes. In the external scope,
169 this stores the composite of all the types declared for this
170 object, visible or not. The ->inner_comp field (used only at file
171 scope) stores whether an incomplete array type at file scope was
172 completed at an inner scope to an array size other than 1.
174 The ->u.label field is used for labels. It points to a structure
175 which stores additional information used for warnings.
177 The depth field is copied from the scope structure that holds this
178 decl. It is used to preserve the proper ordering of the ->shadowed
179 field (see bind()) and also for a handful of special-case checks.
180 Finally, the invisible bit is true for a decl which should be
181 ignored for purposes of normal name lookup, and the nested bit is
182 true for a decl that's been bound a second time in an inner scope;
183 in all such cases, the binding in the outer scope will have its
184 invisible bit true. */
186 struct GTY((chain_next ("%h.prev"))) c_binding {
187 union GTY(()) { /* first so GTY desc can use decl */
188 tree GTY((tag ("0"))) type; /* the type in this scope */
189 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
190 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
191 tree decl; /* the decl bound */
192 tree id; /* the identifier it's bound to */
193 struct c_binding *prev; /* the previous decl in this scope */
194 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
195 unsigned int depth : 28; /* depth of this scope */
196 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
197 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
198 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
199 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
200 location_t locus; /* location for nested bindings */
202 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
203 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
204 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
205 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
207 /* Each C symbol points to three linked lists of c_binding structures.
208 These describe the values of the identifier in the three different
209 namespaces defined by the language. */
211 struct GTY(()) lang_identifier {
212 struct c_common_identifier common_id;
213 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
214 struct c_binding *tag_binding; /* struct/union/enum tags */
215 struct c_binding *label_binding; /* labels */
218 /* Validate c-lang.c's assumptions. */
219 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
220 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
222 /* The binding oracle; see c-tree.h. */
223 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
225 /* This flag is set on an identifier if we have previously asked the
226 binding oracle for this identifier's symbol binding. */
227 #define I_SYMBOL_CHECKED(node) \
228 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
230 static inline struct c_binding* *
231 i_symbol_binding (tree node)
233 struct lang_identifier *lid
234 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
236 if (lid->symbol_binding == NULL
237 && c_binding_oracle != NULL
238 && !I_SYMBOL_CHECKED (node))
240 /* Set the "checked" flag first, to avoid infinite recursion
241 when the binding oracle calls back into gcc. */
242 I_SYMBOL_CHECKED (node) = 1;
243 c_binding_oracle (C_ORACLE_SYMBOL, node);
246 return &lid->symbol_binding;
249 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
251 #define I_SYMBOL_DECL(node) \
252 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
254 /* This flag is set on an identifier if we have previously asked the
255 binding oracle for this identifier's tag binding. */
256 #define I_TAG_CHECKED(node) \
257 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
259 static inline struct c_binding **
260 i_tag_binding (tree node)
262 struct lang_identifier *lid
263 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
265 if (lid->tag_binding == NULL
266 && c_binding_oracle != NULL
267 && !I_TAG_CHECKED (node))
269 /* Set the "checked" flag first, to avoid infinite recursion
270 when the binding oracle calls back into gcc. */
271 I_TAG_CHECKED (node) = 1;
272 c_binding_oracle (C_ORACLE_TAG, node);
275 return &lid->tag_binding;
278 #define I_TAG_BINDING(node) (*i_tag_binding (node))
280 #define I_TAG_DECL(node) \
281 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
283 /* This flag is set on an identifier if we have previously asked the
284 binding oracle for this identifier's label binding. */
285 #define I_LABEL_CHECKED(node) \
286 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
288 static inline struct c_binding **
289 i_label_binding (tree node)
291 struct lang_identifier *lid
292 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
294 if (lid->label_binding == NULL
295 && c_binding_oracle != NULL
296 && !I_LABEL_CHECKED (node))
298 /* Set the "checked" flag first, to avoid infinite recursion
299 when the binding oracle calls back into gcc. */
300 I_LABEL_CHECKED (node) = 1;
301 c_binding_oracle (C_ORACLE_LABEL, node);
304 return &lid->label_binding;
307 #define I_LABEL_BINDING(node) (*i_label_binding (node))
309 #define I_LABEL_DECL(node) \
310 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
312 /* The resulting tree type. */
314 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
315 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
317 union tree_node GTY ((tag ("0"),
318 desc ("tree_node_structure (&%h)")))
319 generic;
320 struct lang_identifier GTY ((tag ("1"))) identifier;
323 /* Track bindings and other things that matter for goto warnings. For
324 efficiency, we do not gather all the decls at the point of
325 definition. Instead, we point into the bindings structure. As
326 scopes are popped, we update these structures and gather the decls
327 that matter at that time. */
329 struct GTY(()) c_spot_bindings {
330 /* The currently open scope which holds bindings defined when the
331 label was defined or the goto statement was found. */
332 struct c_scope *scope;
333 /* The bindings in the scope field which were defined at the point
334 of the label or goto. This lets us look at older or newer
335 bindings in the scope, as appropriate. */
336 struct c_binding *bindings_in_scope;
337 /* The number of statement expressions that have started since this
338 label or goto statement was defined. This is zero if we are at
339 the same statement expression level. It is positive if we are in
340 a statement expression started since this spot. It is negative
341 if this spot was in a statement expression and we have left
342 it. */
343 int stmt_exprs;
344 /* Whether we started in a statement expression but are no longer in
345 it. This is set to true if stmt_exprs ever goes negative. */
346 bool left_stmt_expr;
349 /* This structure is used to keep track of bindings seen when a goto
350 statement is defined. This is only used if we see the goto
351 statement before we see the label. */
353 struct GTY(()) c_goto_bindings {
354 /* The location of the goto statement. */
355 location_t loc;
356 /* The bindings of the goto statement. */
357 struct c_spot_bindings goto_bindings;
360 typedef struct c_goto_bindings *c_goto_bindings_p;
362 /* The additional information we keep track of for a label binding.
363 These fields are updated as scopes are popped. */
365 struct GTY(()) c_label_vars {
366 /* The shadowed c_label_vars, when one label shadows another (which
367 can only happen using a __label__ declaration). */
368 struct c_label_vars *shadowed;
369 /* The bindings when the label was defined. */
370 struct c_spot_bindings label_bindings;
371 /* A list of decls that we care about: decls about which we should
372 warn if a goto branches to this label from later in the function.
373 Decls are added to this list as scopes are popped. We only add
374 the decls that matter. */
375 vec<tree, va_gc> *decls_in_scope;
376 /* A list of goto statements to this label. This is only used for
377 goto statements seen before the label was defined, so that we can
378 issue appropriate warnings for them. */
379 vec<c_goto_bindings_p, va_gc> *gotos;
382 /* Each c_scope structure describes the complete contents of one
383 scope. Four scopes are distinguished specially: the innermost or
384 current scope, the innermost function scope, the file scope (always
385 the second to outermost) and the outermost or external scope.
387 Most declarations are recorded in the current scope.
389 All normal label declarations are recorded in the innermost
390 function scope, as are bindings of undeclared identifiers to
391 error_mark_node. (GCC permits nested functions as an extension,
392 hence the 'innermost' qualifier.) Explicitly declared labels
393 (using the __label__ extension) appear in the current scope.
395 Being in the file scope (current_scope == file_scope) causes
396 special behavior in several places below. Also, under some
397 conditions the Objective-C front end records declarations in the
398 file scope even though that isn't the current scope.
400 All declarations with external linkage are recorded in the external
401 scope, even if they aren't visible there; this models the fact that
402 such declarations are visible to the entire program, and (with a
403 bit of cleverness, see pushdecl) allows diagnosis of some violations
404 of C99 6.2.2p7 and 6.2.7p2:
406 If, within the same translation unit, the same identifier appears
407 with both internal and external linkage, the behavior is
408 undefined.
410 All declarations that refer to the same object or function shall
411 have compatible type; otherwise, the behavior is undefined.
413 Initially only the built-in declarations, which describe compiler
414 intrinsic functions plus a subset of the standard library, are in
415 this scope.
417 The order of the blocks list matters, and it is frequently appended
418 to. To avoid having to walk all the way to the end of the list on
419 each insertion, or reverse the list later, we maintain a pointer to
420 the last list entry. (FIXME: It should be feasible to use a reversed
421 list here.)
423 The bindings list is strictly in reverse order of declarations;
424 pop_scope relies on this. */
427 struct GTY((chain_next ("%h.outer"))) c_scope {
428 /* The scope containing this one. */
429 struct c_scope *outer;
431 /* The next outermost function scope. */
432 struct c_scope *outer_function;
434 /* All bindings in this scope. */
435 struct c_binding *bindings;
437 /* For each scope (except the global one), a chain of BLOCK nodes
438 for all the scopes that were entered and exited one level down. */
439 tree blocks;
440 tree blocks_last;
442 /* The depth of this scope. Used to keep the ->shadowed chain of
443 bindings sorted innermost to outermost. */
444 unsigned int depth : 28;
446 /* True if we are currently filling this scope with parameter
447 declarations. */
448 BOOL_BITFIELD parm_flag : 1;
450 /* True if we saw [*] in this scope. Used to give an error messages
451 if these appears in a function definition. */
452 BOOL_BITFIELD had_vla_unspec : 1;
454 /* True if we already complained about forward parameter decls
455 in this scope. This prevents double warnings on
456 foo (int a; int b; ...) */
457 BOOL_BITFIELD warned_forward_parm_decls : 1;
459 /* True if this is the outermost block scope of a function body.
460 This scope contains the parameters, the local variables declared
461 in the outermost block, and all the labels (except those in
462 nested functions, or declared at block scope with __label__). */
463 BOOL_BITFIELD function_body : 1;
465 /* True means make a BLOCK for this scope no matter what. */
466 BOOL_BITFIELD keep : 1;
468 /* True means that an unsuffixed float constant is _Decimal64. */
469 BOOL_BITFIELD float_const_decimal64 : 1;
471 /* True if this scope has any label bindings. This is used to speed
472 up searching for labels when popping scopes, particularly since
473 labels are normally only found at function scope. */
474 BOOL_BITFIELD has_label_bindings : 1;
476 /* True if we should issue a warning if a goto statement crosses any
477 of the bindings. We still need to check the list of bindings to
478 find the specific ones we need to warn about. This is true if
479 decl_jump_unsafe would return true for any of the bindings. This
480 is used to avoid looping over all the bindings unnecessarily. */
481 BOOL_BITFIELD has_jump_unsafe_decl : 1;
484 /* The scope currently in effect. */
486 static GTY(()) struct c_scope *current_scope;
488 /* The innermost function scope. Ordinary (not explicitly declared)
489 labels, bindings to error_mark_node, and the lazily-created
490 bindings of __func__ and its friends get this scope. */
492 static GTY(()) struct c_scope *current_function_scope;
494 /* The C file scope. This is reset for each input translation unit. */
496 static GTY(()) struct c_scope *file_scope;
498 /* The outermost scope. This is used for all declarations with
499 external linkage, and only these, hence the name. */
501 static GTY(()) struct c_scope *external_scope;
503 /* A chain of c_scope structures awaiting reuse. */
505 static GTY((deletable)) struct c_scope *scope_freelist;
507 /* A chain of c_binding structures awaiting reuse. */
509 static GTY((deletable)) struct c_binding *binding_freelist;
511 /* Append VAR to LIST in scope SCOPE. */
512 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
513 struct c_scope *s_ = (scope); \
514 tree d_ = (decl); \
515 if (s_->list##_last) \
516 BLOCK_CHAIN (s_->list##_last) = d_; \
517 else \
518 s_->list = d_; \
519 s_->list##_last = d_; \
520 } while (0)
522 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
523 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
524 struct c_scope *t_ = (tscope); \
525 struct c_scope *f_ = (fscope); \
526 if (t_->to##_last) \
527 BLOCK_CHAIN (t_->to##_last) = f_->from; \
528 else \
529 t_->to = f_->from; \
530 t_->to##_last = f_->from##_last; \
531 } while (0)
533 /* A c_inline_static structure stores details of a static identifier
534 referenced in a definition of a function that may be an inline
535 definition if no subsequent declaration of that function uses
536 "extern" or does not use "inline". */
538 struct GTY((chain_next ("%h.next"))) c_inline_static {
539 /* The location for a diagnostic. */
540 location_t location;
542 /* The function that may be an inline definition. */
543 tree function;
545 /* The object or function referenced. */
546 tree static_decl;
548 /* What sort of reference this is. */
549 enum c_inline_static_type type;
551 /* The next such structure or NULL. */
552 struct c_inline_static *next;
555 /* List of static identifiers used or referenced in functions that may
556 be inline definitions. */
557 static GTY(()) struct c_inline_static *c_inline_statics;
559 /* True means unconditionally make a BLOCK for the next scope pushed. */
561 static bool keep_next_level_flag;
563 /* True means the next call to push_scope will be the outermost scope
564 of a function body, so do not push a new scope, merely cease
565 expecting parameter decls. */
567 static bool next_is_function_body;
569 /* A vector of pointers to c_binding structures. */
571 typedef struct c_binding *c_binding_ptr;
573 /* Information that we keep for a struct or union while it is being
574 parsed. */
576 struct c_struct_parse_info
578 /* If warn_cxx_compat, a list of types defined within this
579 struct. */
580 auto_vec<tree> struct_types;
581 /* If warn_cxx_compat, a list of field names which have bindings,
582 and which are defined in this struct, but which are not defined
583 in any enclosing struct. This is used to clear the in_struct
584 field of the c_bindings structure. */
585 auto_vec<c_binding_ptr> fields;
586 /* If warn_cxx_compat, a list of typedef names used when defining
587 fields in this struct. */
588 auto_vec<tree> typedefs_seen;
591 /* Information for the struct or union currently being parsed, or
592 NULL if not parsing a struct or union. */
593 static struct c_struct_parse_info *struct_parse_info;
595 /* Forward declarations. */
596 static tree lookup_name_in_scope (tree, struct c_scope *);
597 static tree c_make_fname_decl (location_t, tree, int);
598 static tree grokdeclarator (const struct c_declarator *,
599 struct c_declspecs *,
600 enum decl_context, bool, tree *, tree *, tree *,
601 bool *, enum deprecated_states);
602 static tree grokparms (struct c_arg_info *, bool);
603 static void layout_array_type (tree);
604 static void warn_defaults_to (location_t, int, const char *, ...)
605 ATTRIBUTE_GCC_DIAG(3,4);
607 /* T is a statement. Add it to the statement-tree. This is the
608 C/ObjC version--C++ has a slightly different version of this
609 function. */
611 tree
612 add_stmt (tree t)
614 enum tree_code code = TREE_CODE (t);
616 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
618 if (!EXPR_HAS_LOCATION (t))
619 SET_EXPR_LOCATION (t, input_location);
622 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
623 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
625 /* Add T to the statement-tree. Non-side-effect statements need to be
626 recorded during statement expressions. */
627 if (!building_stmt_list_p ())
628 push_stmt_list ();
629 append_to_statement_list_force (t, &cur_stmt_list);
631 return t;
634 /* Build a pointer type using the default pointer mode. */
636 static tree
637 c_build_pointer_type (tree to_type)
639 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
640 : TYPE_ADDR_SPACE (to_type);
641 machine_mode pointer_mode;
643 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
644 pointer_mode = targetm.addr_space.pointer_mode (as);
645 else
646 pointer_mode = c_default_pointer_mode;
647 return build_pointer_type_for_mode (to_type, pointer_mode, false);
651 /* Return true if we will want to say something if a goto statement
652 crosses DECL. */
654 static bool
655 decl_jump_unsafe (tree decl)
657 if (error_operand_p (decl))
658 return false;
660 /* Always warn about crossing variably modified types. */
661 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
662 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
663 return true;
665 /* Otherwise, only warn if -Wgoto-misses-init and this is an
666 initialized automatic decl. */
667 if (warn_jump_misses_init
668 && VAR_P (decl)
669 && !TREE_STATIC (decl)
670 && DECL_INITIAL (decl) != NULL_TREE)
671 return true;
673 return false;
677 void
678 c_print_identifier (FILE *file, tree node, int indent)
680 void (*save) (enum c_oracle_request, tree identifier);
682 /* Temporarily hide any binding oracle. Without this, calls to
683 debug_tree from the debugger will end up calling into the oracle,
684 making for a confusing debug session. As the oracle isn't needed
685 here for normal operation, it's simplest to suppress it. */
686 save = c_binding_oracle;
687 c_binding_oracle = NULL;
689 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
690 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
691 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
692 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
694 tree rid = ridpointers[C_RID_CODE (node)];
695 indent_to (file, indent + 4);
696 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
697 (void *) rid, IDENTIFIER_POINTER (rid));
700 c_binding_oracle = save;
703 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
704 which may be any of several kinds of DECL or TYPE or error_mark_node,
705 in the scope SCOPE. */
706 static void
707 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
708 bool nested, location_t locus)
710 struct c_binding *b, **here;
712 if (binding_freelist)
714 b = binding_freelist;
715 binding_freelist = b->prev;
717 else
718 b = ggc_alloc<c_binding> ();
720 b->shadowed = 0;
721 b->decl = decl;
722 b->id = name;
723 b->depth = scope->depth;
724 b->invisible = invisible;
725 b->nested = nested;
726 b->inner_comp = 0;
727 b->in_struct = 0;
728 b->locus = locus;
730 b->u.type = NULL;
732 b->prev = scope->bindings;
733 scope->bindings = b;
735 if (decl_jump_unsafe (decl))
736 scope->has_jump_unsafe_decl = 1;
738 if (!name)
739 return;
741 switch (TREE_CODE (decl))
743 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
744 case ENUMERAL_TYPE:
745 case UNION_TYPE:
746 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
747 case VAR_DECL:
748 case FUNCTION_DECL:
749 case TYPE_DECL:
750 case CONST_DECL:
751 case PARM_DECL:
752 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
754 default:
755 gcc_unreachable ();
758 /* Locate the appropriate place in the chain of shadowed decls
759 to insert this binding. Normally, scope == current_scope and
760 this does nothing. */
761 while (*here && (*here)->depth > scope->depth)
762 here = &(*here)->shadowed;
764 b->shadowed = *here;
765 *here = b;
768 /* Clear the binding structure B, stick it on the binding_freelist,
769 and return the former value of b->prev. This is used by pop_scope
770 and get_parm_info to iterate destructively over all the bindings
771 from a given scope. */
772 static struct c_binding *
773 free_binding_and_advance (struct c_binding *b)
775 struct c_binding *prev = b->prev;
777 memset (b, 0, sizeof (struct c_binding));
778 b->prev = binding_freelist;
779 binding_freelist = b;
781 return prev;
784 /* Bind a label. Like bind, but skip fields which aren't used for
785 labels, and add the LABEL_VARS value. */
786 static void
787 bind_label (tree name, tree label, struct c_scope *scope,
788 struct c_label_vars *label_vars)
790 struct c_binding *b;
792 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
793 UNKNOWN_LOCATION);
795 scope->has_label_bindings = true;
797 b = scope->bindings;
798 gcc_assert (b->decl == label);
799 label_vars->shadowed = b->u.label;
800 b->u.label = label_vars;
803 /* Hook called at end of compilation to assume 1 elt
804 for a file-scope tentative array defn that wasn't complete before. */
806 void
807 c_finish_incomplete_decl (tree decl)
809 if (VAR_P (decl))
811 tree type = TREE_TYPE (decl);
812 if (type != error_mark_node
813 && TREE_CODE (type) == ARRAY_TYPE
814 && !DECL_EXTERNAL (decl)
815 && TYPE_DOMAIN (type) == NULL_TREE)
817 warning_at (DECL_SOURCE_LOCATION (decl),
818 0, "array %q+D assumed to have one element", decl);
820 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
822 relayout_decl (decl);
827 /* Record that inline function FUNC contains a reference (location
828 LOC) to static DECL (file-scope or function-local according to
829 TYPE). */
831 void
832 record_inline_static (location_t loc, tree func, tree decl,
833 enum c_inline_static_type type)
835 c_inline_static *csi = ggc_alloc<c_inline_static> ();
836 csi->location = loc;
837 csi->function = func;
838 csi->static_decl = decl;
839 csi->type = type;
840 csi->next = c_inline_statics;
841 c_inline_statics = csi;
844 /* Check for references to static declarations in inline functions at
845 the end of the translation unit and diagnose them if the functions
846 are still inline definitions. */
848 static void
849 check_inline_statics (void)
851 struct c_inline_static *csi;
852 for (csi = c_inline_statics; csi; csi = csi->next)
854 if (DECL_EXTERNAL (csi->function))
855 switch (csi->type)
857 case csi_internal:
858 pedwarn (csi->location, 0,
859 "%qD is static but used in inline function %qD "
860 "which is not static", csi->static_decl, csi->function);
861 break;
862 case csi_modifiable:
863 pedwarn (csi->location, 0,
864 "%q+D is static but declared in inline function %qD "
865 "which is not static", csi->static_decl, csi->function);
866 break;
867 default:
868 gcc_unreachable ();
871 c_inline_statics = NULL;
874 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
875 for the current state, otherwise set it to uninitialized. */
877 static void
878 set_spot_bindings (struct c_spot_bindings *p, bool defining)
880 if (defining)
882 p->scope = current_scope;
883 p->bindings_in_scope = current_scope->bindings;
885 else
887 p->scope = NULL;
888 p->bindings_in_scope = NULL;
890 p->stmt_exprs = 0;
891 p->left_stmt_expr = false;
894 /* Update spot bindings P as we pop out of SCOPE. Return true if we
895 should push decls for a label. */
897 static bool
898 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
900 if (p->scope != scope)
902 /* This label or goto is defined in some other scope, or it is a
903 label which is not yet defined. There is nothing to
904 update. */
905 return false;
908 /* Adjust the spot bindings to refer to the bindings already defined
909 in the enclosing scope. */
910 p->scope = scope->outer;
911 p->bindings_in_scope = p->scope->bindings;
913 return true;
916 /* The Objective-C front-end often needs to determine the current scope. */
918 void *
919 objc_get_current_scope (void)
921 return current_scope;
924 /* The following function is used only by Objective-C. It needs to live here
925 because it accesses the innards of c_scope. */
927 void
928 objc_mark_locals_volatile (void *enclosing_blk)
930 struct c_scope *scope;
931 struct c_binding *b;
933 for (scope = current_scope;
934 scope && scope != enclosing_blk;
935 scope = scope->outer)
937 for (b = scope->bindings; b; b = b->prev)
938 objc_volatilize_decl (b->decl);
940 /* Do not climb up past the current function. */
941 if (scope->function_body)
942 break;
946 /* Return true if we are in the global binding level. */
948 bool
949 global_bindings_p (void)
951 return current_scope == file_scope;
954 void
955 keep_next_level (void)
957 keep_next_level_flag = true;
960 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
962 void
963 set_float_const_decimal64 (void)
965 current_scope->float_const_decimal64 = true;
968 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
970 void
971 clear_float_const_decimal64 (void)
973 current_scope->float_const_decimal64 = false;
976 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
978 bool
979 float_const_decimal64_p (void)
981 return current_scope->float_const_decimal64;
984 /* Identify this scope as currently being filled with parameters. */
986 void
987 declare_parm_level (void)
989 current_scope->parm_flag = true;
992 void
993 push_scope (void)
995 if (next_is_function_body)
997 /* This is the transition from the parameters to the top level
998 of the function body. These are the same scope
999 (C99 6.2.1p4,6) so we do not push another scope structure.
1000 next_is_function_body is set only by store_parm_decls, which
1001 in turn is called when and only when we are about to
1002 encounter the opening curly brace for the function body.
1004 The outermost block of a function always gets a BLOCK node,
1005 because the debugging output routines expect that each
1006 function has at least one BLOCK. */
1007 current_scope->parm_flag = false;
1008 current_scope->function_body = true;
1009 current_scope->keep = true;
1010 current_scope->outer_function = current_function_scope;
1011 current_function_scope = current_scope;
1013 keep_next_level_flag = false;
1014 next_is_function_body = false;
1016 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1017 if (current_scope->outer)
1018 current_scope->float_const_decimal64
1019 = current_scope->outer->float_const_decimal64;
1020 else
1021 current_scope->float_const_decimal64 = false;
1023 else
1025 struct c_scope *scope;
1026 if (scope_freelist)
1028 scope = scope_freelist;
1029 scope_freelist = scope->outer;
1031 else
1032 scope = ggc_cleared_alloc<c_scope> ();
1034 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1035 if (current_scope)
1036 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1037 else
1038 scope->float_const_decimal64 = false;
1040 scope->keep = keep_next_level_flag;
1041 scope->outer = current_scope;
1042 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1044 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1045 possible. */
1046 if (current_scope && scope->depth == 0)
1048 scope->depth--;
1049 sorry ("GCC supports only %u nested scopes", scope->depth);
1052 current_scope = scope;
1053 keep_next_level_flag = false;
1057 /* This is called when we are leaving SCOPE. For each label defined
1058 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1059 These are the decls whose initialization will be skipped by a goto
1060 later in the function. */
1062 static void
1063 update_label_decls (struct c_scope *scope)
1065 struct c_scope *s;
1067 s = scope;
1068 while (s != NULL)
1070 if (s->has_label_bindings)
1072 struct c_binding *b;
1074 for (b = s->bindings; b != NULL; b = b->prev)
1076 struct c_label_vars *label_vars;
1077 struct c_binding *b1;
1078 bool hjud;
1079 unsigned int ix;
1080 struct c_goto_bindings *g;
1082 if (TREE_CODE (b->decl) != LABEL_DECL)
1083 continue;
1084 label_vars = b->u.label;
1086 b1 = label_vars->label_bindings.bindings_in_scope;
1087 if (label_vars->label_bindings.scope == NULL)
1088 hjud = false;
1089 else
1090 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1091 if (update_spot_bindings (scope, &label_vars->label_bindings))
1093 /* This label is defined in this scope. */
1094 if (hjud)
1096 for (; b1 != NULL; b1 = b1->prev)
1098 /* A goto from later in the function to this
1099 label will never see the initialization
1100 of B1, if any. Save it to issue a
1101 warning if needed. */
1102 if (decl_jump_unsafe (b1->decl))
1103 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1108 /* Update the bindings of any goto statements associated
1109 with this label. */
1110 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1111 update_spot_bindings (scope, &g->goto_bindings);
1115 /* Don't search beyond the current function. */
1116 if (s == current_function_scope)
1117 break;
1119 s = s->outer;
1123 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1125 static void
1126 set_type_context (tree type, tree context)
1128 for (type = TYPE_MAIN_VARIANT (type); type;
1129 type = TYPE_NEXT_VARIANT (type))
1130 TYPE_CONTEXT (type) = context;
1133 /* Exit a scope. Restore the state of the identifier-decl mappings
1134 that were in effect when this scope was entered. Return a BLOCK
1135 node containing all the DECLs in this scope that are of interest
1136 to debug info generation. */
1138 tree
1139 pop_scope (void)
1141 struct c_scope *scope = current_scope;
1142 tree block, context, p;
1143 struct c_binding *b;
1145 bool functionbody = scope->function_body;
1146 bool keep = functionbody || scope->keep || scope->bindings;
1148 update_label_decls (scope);
1150 /* If appropriate, create a BLOCK to record the decls for the life
1151 of this function. */
1152 block = NULL_TREE;
1153 if (keep)
1155 block = make_node (BLOCK);
1156 BLOCK_SUBBLOCKS (block) = scope->blocks;
1157 TREE_USED (block) = 1;
1159 /* In each subblock, record that this is its superior. */
1160 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1161 BLOCK_SUPERCONTEXT (p) = block;
1163 BLOCK_VARS (block) = NULL_TREE;
1166 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1167 scope must be set so that they point to the appropriate
1168 construct, i.e. either to the current FUNCTION_DECL node, or
1169 else to the BLOCK node we just constructed.
1171 Note that for tagged types whose scope is just the formal
1172 parameter list for some function type specification, we can't
1173 properly set their TYPE_CONTEXTs here, because we don't have a
1174 pointer to the appropriate FUNCTION_TYPE node readily available
1175 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1176 type nodes get set in `grokdeclarator' as soon as we have created
1177 the FUNCTION_TYPE node which will represent the "scope" for these
1178 "parameter list local" tagged types. */
1179 if (scope->function_body)
1180 context = current_function_decl;
1181 else if (scope == file_scope)
1183 tree file_decl
1184 = build_translation_unit_decl (get_identifier (main_input_filename));
1185 context = file_decl;
1186 debug_hooks->register_main_translation_unit (file_decl);
1188 else
1189 context = block;
1191 /* Clear all bindings in this scope. */
1192 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1194 p = b->decl;
1195 switch (TREE_CODE (p))
1197 case LABEL_DECL:
1198 /* Warnings for unused labels, errors for undefined labels. */
1199 if (TREE_USED (p) && !DECL_INITIAL (p))
1201 error ("label %q+D used but not defined", p);
1202 DECL_INITIAL (p) = error_mark_node;
1204 else
1205 warn_for_unused_label (p);
1207 /* Labels go in BLOCK_VARS. */
1208 DECL_CHAIN (p) = BLOCK_VARS (block);
1209 BLOCK_VARS (block) = p;
1210 gcc_assert (I_LABEL_BINDING (b->id) == b);
1211 I_LABEL_BINDING (b->id) = b->shadowed;
1213 /* Also pop back to the shadowed label_vars. */
1214 release_tree_vector (b->u.label->decls_in_scope);
1215 b->u.label = b->u.label->shadowed;
1216 break;
1218 case ENUMERAL_TYPE:
1219 case UNION_TYPE:
1220 case RECORD_TYPE:
1221 set_type_context (p, context);
1223 /* Types may not have tag-names, in which case the type
1224 appears in the bindings list with b->id NULL. */
1225 if (b->id)
1227 gcc_assert (I_TAG_BINDING (b->id) == b);
1228 I_TAG_BINDING (b->id) = b->shadowed;
1230 break;
1232 case FUNCTION_DECL:
1233 /* Propagate TREE_ADDRESSABLE from nested functions to their
1234 containing functions. */
1235 if (!TREE_ASM_WRITTEN (p)
1236 && DECL_INITIAL (p) != NULL_TREE
1237 && TREE_ADDRESSABLE (p)
1238 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1239 && DECL_ABSTRACT_ORIGIN (p) != p)
1240 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1241 if (!DECL_EXTERNAL (p)
1242 && !DECL_INITIAL (p)
1243 && scope != file_scope
1244 && scope != external_scope)
1246 error ("nested function %q+D declared but never defined", p);
1247 undef_nested_function = true;
1249 else if (DECL_DECLARED_INLINE_P (p)
1250 && TREE_PUBLIC (p)
1251 && !DECL_INITIAL (p))
1253 /* C99 6.7.4p6: "a function with external linkage... declared
1254 with an inline function specifier ... shall also be defined
1255 in the same translation unit." */
1256 if (!flag_gnu89_inline
1257 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1258 && scope != external_scope)
1259 pedwarn (input_location, 0,
1260 "inline function %q+D declared but never defined", p);
1261 DECL_EXTERNAL (p) = 1;
1264 goto common_symbol;
1266 case VAR_DECL:
1267 /* Warnings for unused variables. */
1268 if ((!TREE_USED (p) || !DECL_READ_P (p))
1269 && !TREE_NO_WARNING (p)
1270 && !DECL_IN_SYSTEM_HEADER (p)
1271 && DECL_NAME (p)
1272 && !DECL_ARTIFICIAL (p)
1273 && scope != file_scope
1274 && scope != external_scope)
1276 if (!TREE_USED (p))
1277 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1278 else if (DECL_CONTEXT (p) == current_function_decl)
1279 warning_at (DECL_SOURCE_LOCATION (p),
1280 OPT_Wunused_but_set_variable,
1281 "variable %qD set but not used", p);
1284 if (b->inner_comp)
1286 error ("type of array %q+D completed incompatibly with"
1287 " implicit initialization", p);
1290 /* Fall through. */
1291 case TYPE_DECL:
1292 case CONST_DECL:
1293 common_symbol:
1294 /* All of these go in BLOCK_VARS, but only if this is the
1295 binding in the home scope. */
1296 if (!b->nested)
1298 DECL_CHAIN (p) = BLOCK_VARS (block);
1299 BLOCK_VARS (block) = p;
1301 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1303 /* For block local externs add a special
1304 DECL_EXTERNAL decl for debug info generation. */
1305 tree extp = copy_node (p);
1307 DECL_EXTERNAL (extp) = 1;
1308 TREE_STATIC (extp) = 0;
1309 TREE_PUBLIC (extp) = 1;
1310 DECL_INITIAL (extp) = NULL_TREE;
1311 DECL_LANG_SPECIFIC (extp) = NULL;
1312 DECL_CONTEXT (extp) = current_function_decl;
1313 if (TREE_CODE (p) == FUNCTION_DECL)
1315 DECL_RESULT (extp) = NULL_TREE;
1316 DECL_SAVED_TREE (extp) = NULL_TREE;
1317 DECL_STRUCT_FUNCTION (extp) = NULL;
1319 if (b->locus != UNKNOWN_LOCATION)
1320 DECL_SOURCE_LOCATION (extp) = b->locus;
1321 DECL_CHAIN (extp) = BLOCK_VARS (block);
1322 BLOCK_VARS (block) = extp;
1324 /* If this is the file scope set DECL_CONTEXT of each decl to
1325 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1326 work. */
1327 if (scope == file_scope)
1329 DECL_CONTEXT (p) = context;
1330 if (TREE_CODE (p) == TYPE_DECL
1331 && TREE_TYPE (p) != error_mark_node)
1332 set_type_context (TREE_TYPE (p), context);
1335 gcc_fallthrough ();
1336 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1337 already been put there by store_parm_decls. Unused-
1338 parameter warnings are handled by function.c.
1339 error_mark_node obviously does not go in BLOCK_VARS and
1340 does not get unused-variable warnings. */
1341 case PARM_DECL:
1342 case ERROR_MARK:
1343 /* It is possible for a decl not to have a name. We get
1344 here with b->id NULL in this case. */
1345 if (b->id)
1347 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1348 I_SYMBOL_BINDING (b->id) = b->shadowed;
1349 if (b->shadowed && b->shadowed->u.type)
1350 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1352 break;
1354 default:
1355 gcc_unreachable ();
1360 /* Dispose of the block that we just made inside some higher level. */
1361 if ((scope->function_body || scope == file_scope) && context)
1363 DECL_INITIAL (context) = block;
1364 BLOCK_SUPERCONTEXT (block) = context;
1366 else if (scope->outer)
1368 if (block)
1369 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1370 /* If we did not make a block for the scope just exited, any
1371 blocks made for inner scopes must be carried forward so they
1372 will later become subblocks of something else. */
1373 else if (scope->blocks)
1374 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1377 /* Pop the current scope, and free the structure for reuse. */
1378 current_scope = scope->outer;
1379 if (scope->function_body)
1380 current_function_scope = scope->outer_function;
1382 memset (scope, 0, sizeof (struct c_scope));
1383 scope->outer = scope_freelist;
1384 scope_freelist = scope;
1386 return block;
1389 void
1390 push_file_scope (void)
1392 tree decl;
1394 if (file_scope)
1395 return;
1397 push_scope ();
1398 file_scope = current_scope;
1400 start_fname_decls ();
1402 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1403 bind (DECL_NAME (decl), decl, file_scope,
1404 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1407 void
1408 pop_file_scope (void)
1410 /* In case there were missing closebraces, get us back to the global
1411 binding level. */
1412 while (current_scope != file_scope)
1413 pop_scope ();
1415 /* __FUNCTION__ is defined at file scope (""). This
1416 call may not be necessary as my tests indicate it
1417 still works without it. */
1418 finish_fname_decls ();
1420 check_inline_statics ();
1422 /* This is the point to write out a PCH if we're doing that.
1423 In that case we do not want to do anything else. */
1424 if (pch_file)
1426 c_common_write_pch ();
1427 /* Ensure even the callers don't try to finalize the CU. */
1428 flag_syntax_only = 1;
1429 return;
1432 /* Pop off the file scope and close this translation unit. */
1433 pop_scope ();
1434 file_scope = 0;
1436 maybe_apply_pending_pragma_weaks ();
1439 /* Adjust the bindings for the start of a statement expression. */
1441 void
1442 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1444 struct c_scope *scope;
1446 for (scope = current_scope; scope != NULL; scope = scope->outer)
1448 struct c_binding *b;
1450 if (!scope->has_label_bindings)
1451 continue;
1453 for (b = scope->bindings; b != NULL; b = b->prev)
1455 struct c_label_vars *label_vars;
1456 unsigned int ix;
1457 struct c_goto_bindings *g;
1459 if (TREE_CODE (b->decl) != LABEL_DECL)
1460 continue;
1461 label_vars = b->u.label;
1462 ++label_vars->label_bindings.stmt_exprs;
1463 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1464 ++g->goto_bindings.stmt_exprs;
1468 if (switch_bindings != NULL)
1469 ++switch_bindings->stmt_exprs;
1472 /* Adjust the bindings for the end of a statement expression. */
1474 void
1475 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1477 struct c_scope *scope;
1479 for (scope = current_scope; scope != NULL; scope = scope->outer)
1481 struct c_binding *b;
1483 if (!scope->has_label_bindings)
1484 continue;
1486 for (b = scope->bindings; b != NULL; b = b->prev)
1488 struct c_label_vars *label_vars;
1489 unsigned int ix;
1490 struct c_goto_bindings *g;
1492 if (TREE_CODE (b->decl) != LABEL_DECL)
1493 continue;
1494 label_vars = b->u.label;
1495 --label_vars->label_bindings.stmt_exprs;
1496 if (label_vars->label_bindings.stmt_exprs < 0)
1498 label_vars->label_bindings.left_stmt_expr = true;
1499 label_vars->label_bindings.stmt_exprs = 0;
1501 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1503 --g->goto_bindings.stmt_exprs;
1504 if (g->goto_bindings.stmt_exprs < 0)
1506 g->goto_bindings.left_stmt_expr = true;
1507 g->goto_bindings.stmt_exprs = 0;
1513 if (switch_bindings != NULL)
1515 --switch_bindings->stmt_exprs;
1516 gcc_assert (switch_bindings->stmt_exprs >= 0);
1520 /* Push a definition or a declaration of struct, union or enum tag "name".
1521 "type" should be the type node.
1522 We assume that the tag "name" is not already defined, and has a location
1523 of LOC.
1525 Note that the definition may really be just a forward reference.
1526 In that case, the TYPE_SIZE will be zero. */
1528 static void
1529 pushtag (location_t loc, tree name, tree type)
1531 /* Record the identifier as the type's name if it has none. */
1532 if (name && !TYPE_NAME (type))
1533 TYPE_NAME (type) = name;
1534 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1536 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1537 tagged type we just added to the current scope. This fake
1538 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1539 to output a representation of a tagged type, and it also gives
1540 us a convenient place to record the "scope start" address for the
1541 tagged type. */
1543 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1544 TYPE_DECL, NULL_TREE, type));
1546 /* An approximation for now, so we can tell this is a function-scope tag.
1547 This will be updated in pop_scope. */
1548 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1550 if (warn_cxx_compat && name != NULL_TREE)
1552 struct c_binding *b = I_SYMBOL_BINDING (name);
1554 if (b != NULL
1555 && b->decl != NULL_TREE
1556 && TREE_CODE (b->decl) == TYPE_DECL
1557 && (B_IN_CURRENT_SCOPE (b)
1558 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1559 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1560 != TYPE_MAIN_VARIANT (type)))
1562 if (warning_at (loc, OPT_Wc___compat,
1563 ("using %qD as both a typedef and a tag is "
1564 "invalid in C++"), b->decl)
1565 && b->locus != UNKNOWN_LOCATION)
1566 inform (b->locus, "originally defined here");
1571 /* An exported interface to pushtag. This is used by the gdb plugin's
1572 binding oracle to introduce a new tag binding. */
1574 void
1575 c_pushtag (location_t loc, tree name, tree type)
1577 pushtag (loc, name, type);
1580 /* An exported interface to bind a declaration. LOC is the location
1581 to use. DECL is the declaration to bind. The decl's name is used
1582 to determine how it is bound. If DECL is a VAR_DECL, then
1583 IS_GLOBAL determines whether the decl is put into the global (file
1584 and external) scope or the current function's scope; if DECL is not
1585 a VAR_DECL then it is always put into the file scope. */
1587 void
1588 c_bind (location_t loc, tree decl, bool is_global)
1590 struct c_scope *scope;
1591 bool nested = false;
1593 if (!VAR_P (decl) || current_function_scope == NULL)
1595 /* Types and functions are always considered to be global. */
1596 scope = file_scope;
1597 DECL_EXTERNAL (decl) = 1;
1598 TREE_PUBLIC (decl) = 1;
1600 else if (is_global)
1602 /* Also bind it into the external scope. */
1603 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1604 nested = true;
1605 scope = file_scope;
1606 DECL_EXTERNAL (decl) = 1;
1607 TREE_PUBLIC (decl) = 1;
1609 else
1611 DECL_CONTEXT (decl) = current_function_decl;
1612 TREE_PUBLIC (decl) = 0;
1613 scope = current_function_scope;
1616 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1619 /* Subroutine of compare_decls. Allow harmless mismatches in return
1620 and argument types provided that the type modes match. This function
1621 return a unified type given a suitable match, and 0 otherwise. */
1623 static tree
1624 match_builtin_function_types (tree newtype, tree oldtype)
1626 tree newrettype, oldrettype;
1627 tree newargs, oldargs;
1628 tree trytype, tryargs;
1630 /* Accept the return type of the new declaration if same modes. */
1631 oldrettype = TREE_TYPE (oldtype);
1632 newrettype = TREE_TYPE (newtype);
1634 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1635 return NULL_TREE;
1637 oldargs = TYPE_ARG_TYPES (oldtype);
1638 newargs = TYPE_ARG_TYPES (newtype);
1639 tryargs = newargs;
1641 while (oldargs || newargs)
1643 if (!oldargs
1644 || !newargs
1645 || !TREE_VALUE (oldargs)
1646 || !TREE_VALUE (newargs)
1647 || TYPE_MODE (TREE_VALUE (oldargs))
1648 != TYPE_MODE (TREE_VALUE (newargs)))
1649 return NULL_TREE;
1651 oldargs = TREE_CHAIN (oldargs);
1652 newargs = TREE_CHAIN (newargs);
1655 trytype = build_function_type (newrettype, tryargs);
1657 /* Allow declaration to change transaction_safe attribute. */
1658 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1659 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1660 tree newattrs = TYPE_ATTRIBUTES (newtype);
1661 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1662 if (oldtsafe && !newtsafe)
1663 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1664 else if (newtsafe && !oldtsafe)
1665 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1666 NULL_TREE, oldattrs);
1668 return build_type_attribute_variant (trytype, oldattrs);
1671 /* Subroutine of diagnose_mismatched_decls. Check for function type
1672 mismatch involving an empty arglist vs a nonempty one and give clearer
1673 diagnostics. */
1674 static void
1675 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1676 tree newtype, tree oldtype)
1678 tree t;
1680 if (TREE_CODE (olddecl) != FUNCTION_DECL
1681 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1682 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1683 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1684 return;
1686 t = TYPE_ARG_TYPES (oldtype);
1687 if (t == NULL_TREE)
1688 t = TYPE_ARG_TYPES (newtype);
1689 for (; t; t = TREE_CHAIN (t))
1691 tree type = TREE_VALUE (t);
1693 if (TREE_CHAIN (t) == NULL_TREE
1694 && TYPE_MAIN_VARIANT (type) != void_type_node)
1696 inform (input_location, "a parameter list with an ellipsis can%'t match "
1697 "an empty parameter name list declaration");
1698 break;
1701 if (c_type_promotes_to (type) != type)
1703 inform (input_location, "an argument type that has a default promotion can%'t match "
1704 "an empty parameter name list declaration");
1705 break;
1710 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1711 old-style function definition, NEWDECL is a prototype declaration.
1712 Diagnose inconsistencies in the argument list. Returns TRUE if
1713 the prototype is compatible, FALSE if not. */
1714 static bool
1715 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1717 tree newargs, oldargs;
1718 int i;
1720 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1722 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1723 newargs = TYPE_ARG_TYPES (newtype);
1724 i = 1;
1726 for (;;)
1728 tree oldargtype = TREE_VALUE (oldargs);
1729 tree newargtype = TREE_VALUE (newargs);
1731 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1732 return false;
1734 oldargtype = (TYPE_ATOMIC (oldargtype)
1735 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1736 TYPE_QUAL_ATOMIC)
1737 : TYPE_MAIN_VARIANT (oldargtype));
1738 newargtype = (TYPE_ATOMIC (newargtype)
1739 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1740 TYPE_QUAL_ATOMIC)
1741 : TYPE_MAIN_VARIANT (newargtype));
1743 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1744 break;
1746 /* Reaching the end of just one list means the two decls don't
1747 agree on the number of arguments. */
1748 if (END_OF_ARGLIST (oldargtype))
1750 error ("prototype for %q+D declares more arguments "
1751 "than previous old-style definition", newdecl);
1752 return false;
1754 else if (END_OF_ARGLIST (newargtype))
1756 error ("prototype for %q+D declares fewer arguments "
1757 "than previous old-style definition", newdecl);
1758 return false;
1761 /* Type for passing arg must be consistent with that declared
1762 for the arg. */
1763 else if (!comptypes (oldargtype, newargtype))
1765 error ("prototype for %q+D declares argument %d"
1766 " with incompatible type",
1767 newdecl, i);
1768 return false;
1771 oldargs = TREE_CHAIN (oldargs);
1772 newargs = TREE_CHAIN (newargs);
1773 i++;
1776 /* If we get here, no errors were found, but do issue a warning
1777 for this poor-style construct. */
1778 warning (0, "prototype for %q+D follows non-prototype definition",
1779 newdecl);
1780 return true;
1781 #undef END_OF_ARGLIST
1784 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1785 first in a pair of mismatched declarations, using the diagnostic
1786 function DIAG. */
1787 static void
1788 locate_old_decl (tree decl)
1790 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1791 && !C_DECL_DECLARED_BUILTIN (decl))
1793 else if (DECL_INITIAL (decl))
1794 inform (input_location, "previous definition of %q+D was here", decl);
1795 else if (C_DECL_IMPLICIT (decl))
1796 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1797 else
1798 inform (input_location, "previous declaration of %q+D was here", decl);
1801 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1802 Returns true if the caller should proceed to merge the two, false
1803 if OLDDECL should simply be discarded. As a side effect, issues
1804 all necessary diagnostics for invalid or poor-style combinations.
1805 If it returns true, writes the types of NEWDECL and OLDDECL to
1806 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1807 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1809 static bool
1810 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1811 tree *newtypep, tree *oldtypep)
1813 tree newtype, oldtype;
1814 bool pedwarned = false;
1815 bool warned = false;
1816 bool retval = true;
1818 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1819 && DECL_EXTERNAL (DECL))
1821 /* If we have error_mark_node for either decl or type, just discard
1822 the previous decl - we're in an error cascade already. */
1823 if (olddecl == error_mark_node || newdecl == error_mark_node)
1824 return false;
1825 *oldtypep = oldtype = TREE_TYPE (olddecl);
1826 *newtypep = newtype = TREE_TYPE (newdecl);
1827 if (oldtype == error_mark_node || newtype == error_mark_node)
1828 return false;
1830 /* Two different categories of symbol altogether. This is an error
1831 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1832 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1834 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1835 && DECL_BUILT_IN (olddecl)
1836 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1838 error ("%q+D redeclared as different kind of symbol", newdecl);
1839 locate_old_decl (olddecl);
1841 else if (TREE_PUBLIC (newdecl))
1842 warning (OPT_Wbuiltin_declaration_mismatch,
1843 "built-in function %q+D declared as non-function",
1844 newdecl);
1845 else
1846 warning (OPT_Wshadow, "declaration of %q+D shadows "
1847 "a built-in function", newdecl);
1848 return false;
1851 /* Enumerators have no linkage, so may only be declared once in a
1852 given scope. */
1853 if (TREE_CODE (olddecl) == CONST_DECL)
1855 error ("redeclaration of enumerator %q+D", newdecl);
1856 locate_old_decl (olddecl);
1857 return false;
1860 if (!comptypes (oldtype, newtype))
1862 if (TREE_CODE (olddecl) == FUNCTION_DECL
1863 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1865 /* Accept harmless mismatch in function types.
1866 This is for the ffs and fprintf builtins. */
1867 tree trytype = match_builtin_function_types (newtype, oldtype);
1869 if (trytype && comptypes (newtype, trytype))
1870 *oldtypep = oldtype = trytype;
1871 else
1873 /* If types don't match for a built-in, throw away the
1874 built-in. No point in calling locate_old_decl here, it
1875 won't print anything. */
1876 warning (OPT_Wbuiltin_declaration_mismatch,
1877 "conflicting types for built-in function %q+D",
1878 newdecl);
1879 return false;
1882 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1883 && DECL_IS_BUILTIN (olddecl))
1885 /* A conflicting function declaration for a predeclared
1886 function that isn't actually built in. Objective C uses
1887 these. The new declaration silently overrides everything
1888 but the volatility (i.e. noreturn) indication. See also
1889 below. FIXME: Make Objective C use normal builtins. */
1890 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1891 return false;
1893 /* Permit void foo (...) to match int foo (...) if the latter is
1894 the definition and implicit int was used. See
1895 c-torture/compile/920625-2.c. */
1896 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1897 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1898 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1899 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1901 pedwarned = pedwarn (input_location, 0,
1902 "conflicting types for %q+D", newdecl);
1903 /* Make sure we keep void as the return type. */
1904 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1905 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1907 /* Permit void foo (...) to match an earlier call to foo (...) with
1908 no declared type (thus, implicitly int). */
1909 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1910 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1911 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1912 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1914 pedwarned = pedwarn (input_location, 0,
1915 "conflicting types for %q+D", newdecl);
1916 /* Make sure we keep void as the return type. */
1917 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1919 else
1921 int new_quals = TYPE_QUALS (newtype);
1922 int old_quals = TYPE_QUALS (oldtype);
1924 if (new_quals != old_quals)
1926 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1927 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1928 if (new_addr != old_addr)
1930 if (ADDR_SPACE_GENERIC_P (new_addr))
1931 error ("conflicting named address spaces (generic vs %s) "
1932 "for %q+D",
1933 c_addr_space_name (old_addr), newdecl);
1934 else if (ADDR_SPACE_GENERIC_P (old_addr))
1935 error ("conflicting named address spaces (%s vs generic) "
1936 "for %q+D",
1937 c_addr_space_name (new_addr), newdecl);
1938 else
1939 error ("conflicting named address spaces (%s vs %s) "
1940 "for %q+D",
1941 c_addr_space_name (new_addr),
1942 c_addr_space_name (old_addr),
1943 newdecl);
1946 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1947 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1948 error ("conflicting type qualifiers for %q+D", newdecl);
1950 else
1951 error ("conflicting types for %q+D", newdecl);
1952 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1953 locate_old_decl (olddecl);
1954 return false;
1958 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1959 but silently ignore the redeclaration if either is in a system
1960 header. (Conflicting redeclarations were handled above.) This
1961 is allowed for C11 if the types are the same, not just
1962 compatible. */
1963 if (TREE_CODE (newdecl) == TYPE_DECL)
1965 bool types_different = false;
1966 int comptypes_result;
1968 comptypes_result
1969 = comptypes_check_different_types (oldtype, newtype, &types_different);
1971 if (comptypes_result != 1 || types_different)
1973 error ("redefinition of typedef %q+D with different type", newdecl);
1974 locate_old_decl (olddecl);
1975 return false;
1978 if (DECL_IN_SYSTEM_HEADER (newdecl)
1979 || DECL_IN_SYSTEM_HEADER (olddecl)
1980 || TREE_NO_WARNING (newdecl)
1981 || TREE_NO_WARNING (olddecl))
1982 return true; /* Allow OLDDECL to continue in use. */
1984 if (variably_modified_type_p (newtype, NULL))
1986 error ("redefinition of typedef %q+D with variably modified type",
1987 newdecl);
1988 locate_old_decl (olddecl);
1990 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1991 "redefinition of typedef %q+D", newdecl))
1992 locate_old_decl (olddecl);
1994 return true;
1997 /* Function declarations can either be 'static' or 'extern' (no
1998 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1999 can never conflict with each other on account of linkage
2000 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2001 gnu89 mode permits two definitions if one is 'extern inline' and
2002 one is not. The non- extern-inline definition supersedes the
2003 extern-inline definition. */
2005 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2007 /* If you declare a built-in function name as static, or
2008 define the built-in with an old-style definition (so we
2009 can't validate the argument list) the built-in definition is
2010 overridden, but optionally warn this was a bad choice of name. */
2011 if (DECL_BUILT_IN (olddecl)
2012 && !C_DECL_DECLARED_BUILTIN (olddecl)
2013 && (!TREE_PUBLIC (newdecl)
2014 || (DECL_INITIAL (newdecl)
2015 && !prototype_p (TREE_TYPE (newdecl)))))
2017 warning (OPT_Wshadow, "declaration of %q+D shadows "
2018 "a built-in function", newdecl);
2019 /* Discard the old built-in function. */
2020 return false;
2023 if (DECL_INITIAL (newdecl))
2025 if (DECL_INITIAL (olddecl))
2027 /* If both decls are in the same TU and the new declaration
2028 isn't overriding an extern inline reject the new decl.
2029 In c99, no overriding is allowed in the same translation
2030 unit. */
2031 if ((!DECL_EXTERN_INLINE (olddecl)
2032 || DECL_EXTERN_INLINE (newdecl)
2033 || (!flag_gnu89_inline
2034 && (!DECL_DECLARED_INLINE_P (olddecl)
2035 || !lookup_attribute ("gnu_inline",
2036 DECL_ATTRIBUTES (olddecl)))
2037 && (!DECL_DECLARED_INLINE_P (newdecl)
2038 || !lookup_attribute ("gnu_inline",
2039 DECL_ATTRIBUTES (newdecl))))
2041 && same_translation_unit_p (newdecl, olddecl))
2043 error ("redefinition of %q+D", newdecl);
2044 locate_old_decl (olddecl);
2045 return false;
2049 /* If we have a prototype after an old-style function definition,
2050 the argument types must be checked specially. */
2051 else if (DECL_INITIAL (olddecl)
2052 && !prototype_p (oldtype) && prototype_p (newtype)
2053 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2054 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2056 locate_old_decl (olddecl);
2057 return false;
2059 /* A non-static declaration (even an "extern") followed by a
2060 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2061 The same is true for a static forward declaration at block
2062 scope followed by a non-static declaration/definition at file
2063 scope. Static followed by non-static at the same scope is
2064 not undefined behavior, and is the most convenient way to get
2065 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2066 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2067 we do diagnose it if -Wtraditional. */
2068 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2070 /* Two exceptions to the rule. If olddecl is an extern
2071 inline, or a predeclared function that isn't actually
2072 built in, newdecl silently overrides olddecl. The latter
2073 occur only in Objective C; see also above. (FIXME: Make
2074 Objective C use normal builtins.) */
2075 if (!DECL_IS_BUILTIN (olddecl)
2076 && !DECL_EXTERN_INLINE (olddecl))
2078 error ("static declaration of %q+D follows "
2079 "non-static declaration", newdecl);
2080 locate_old_decl (olddecl);
2082 return false;
2084 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2086 if (DECL_CONTEXT (olddecl))
2088 error ("non-static declaration of %q+D follows "
2089 "static declaration", newdecl);
2090 locate_old_decl (olddecl);
2091 return false;
2093 else if (warn_traditional)
2095 warned |= warning (OPT_Wtraditional,
2096 "non-static declaration of %q+D "
2097 "follows static declaration", newdecl);
2101 /* Make sure gnu_inline attribute is either not present, or
2102 present on all inline decls. */
2103 if (DECL_DECLARED_INLINE_P (olddecl)
2104 && DECL_DECLARED_INLINE_P (newdecl))
2106 bool newa = lookup_attribute ("gnu_inline",
2107 DECL_ATTRIBUTES (newdecl)) != NULL;
2108 bool olda = lookup_attribute ("gnu_inline",
2109 DECL_ATTRIBUTES (olddecl)) != NULL;
2110 if (newa != olda)
2112 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2113 newa ? newdecl : olddecl);
2114 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2115 "but not here");
2119 else if (VAR_P (newdecl))
2121 /* Only variables can be thread-local, and all declarations must
2122 agree on this property. */
2123 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2125 /* Nothing to check. Since OLDDECL is marked threadprivate
2126 and NEWDECL does not have a thread-local attribute, we
2127 will merge the threadprivate attribute into NEWDECL. */
2130 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2132 if (DECL_THREAD_LOCAL_P (newdecl))
2133 error ("thread-local declaration of %q+D follows "
2134 "non-thread-local declaration", newdecl);
2135 else
2136 error ("non-thread-local declaration of %q+D follows "
2137 "thread-local declaration", newdecl);
2139 locate_old_decl (olddecl);
2140 return false;
2143 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2144 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2146 error ("redefinition of %q+D", newdecl);
2147 locate_old_decl (olddecl);
2148 return false;
2151 /* Objects declared at file scope: if the first declaration had
2152 external linkage (even if it was an external reference) the
2153 second must have external linkage as well, or the behavior is
2154 undefined. If the first declaration had internal linkage, then
2155 the second must too, or else be an external reference (in which
2156 case the composite declaration still has internal linkage).
2157 As for function declarations, we warn about the static-then-
2158 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2159 if (DECL_FILE_SCOPE_P (newdecl)
2160 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2162 if (DECL_EXTERNAL (newdecl))
2164 if (!DECL_FILE_SCOPE_P (olddecl))
2166 error ("extern declaration of %q+D follows "
2167 "declaration with no linkage", newdecl);
2168 locate_old_decl (olddecl);
2169 return false;
2171 else if (warn_traditional)
2173 warned |= warning (OPT_Wtraditional,
2174 "non-static declaration of %q+D "
2175 "follows static declaration", newdecl);
2178 else
2180 if (TREE_PUBLIC (newdecl))
2181 error ("non-static declaration of %q+D follows "
2182 "static declaration", newdecl);
2183 else
2184 error ("static declaration of %q+D follows "
2185 "non-static declaration", newdecl);
2187 locate_old_decl (olddecl);
2188 return false;
2191 /* Two objects with the same name declared at the same block
2192 scope must both be external references (6.7p3). */
2193 else if (!DECL_FILE_SCOPE_P (newdecl))
2195 if (DECL_EXTERNAL (newdecl))
2197 /* Extern with initializer at block scope, which will
2198 already have received an error. */
2200 else if (DECL_EXTERNAL (olddecl))
2202 error ("declaration of %q+D with no linkage follows "
2203 "extern declaration", newdecl);
2204 locate_old_decl (olddecl);
2206 else
2208 error ("redeclaration of %q+D with no linkage", newdecl);
2209 locate_old_decl (olddecl);
2212 return false;
2215 /* C++ does not permit a decl to appear multiple times at file
2216 scope. */
2217 if (warn_cxx_compat
2218 && DECL_FILE_SCOPE_P (newdecl)
2219 && !DECL_EXTERNAL (newdecl)
2220 && !DECL_EXTERNAL (olddecl))
2221 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2222 OPT_Wc___compat,
2223 ("duplicate declaration of %qD is "
2224 "invalid in C++"),
2225 newdecl);
2228 /* warnings */
2229 /* All decls must agree on a visibility. */
2230 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2231 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2232 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2234 warned |= warning (0, "redeclaration of %q+D with different visibility "
2235 "(old visibility preserved)", newdecl);
2238 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2239 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2240 else /* PARM_DECL, VAR_DECL */
2242 /* Redeclaration of a parameter is a constraint violation (this is
2243 not explicitly stated, but follows from C99 6.7p3 [no more than
2244 one declaration of the same identifier with no linkage in the
2245 same scope, except type tags] and 6.2.2p6 [parameters have no
2246 linkage]). We must check for a forward parameter declaration,
2247 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2248 an extension, the mandatory diagnostic for which is handled by
2249 mark_forward_parm_decls. */
2251 if (TREE_CODE (newdecl) == PARM_DECL
2252 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2254 error ("redefinition of parameter %q+D", newdecl);
2255 locate_old_decl (olddecl);
2256 return false;
2260 /* Optional warning for completely redundant decls. */
2261 if (!warned && !pedwarned
2262 && warn_redundant_decls
2263 /* Don't warn about a function declaration followed by a
2264 definition. */
2265 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2266 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2267 /* Don't warn about redundant redeclarations of builtins. */
2268 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2269 && !DECL_BUILT_IN (newdecl)
2270 && DECL_BUILT_IN (olddecl)
2271 && !C_DECL_DECLARED_BUILTIN (olddecl))
2272 /* Don't warn about an extern followed by a definition. */
2273 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2274 /* Don't warn about forward parameter decls. */
2275 && !(TREE_CODE (newdecl) == PARM_DECL
2276 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2277 /* Don't warn about a variable definition following a declaration. */
2278 && !(VAR_P (newdecl)
2279 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2281 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2282 newdecl);
2285 /* Report location of previous decl/defn. */
2286 if (warned || pedwarned)
2287 locate_old_decl (olddecl);
2289 #undef DECL_EXTERN_INLINE
2291 return retval;
2294 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2295 consistent with OLDDECL, but carries new information. Merge the
2296 new information into OLDDECL. This function issues no
2297 diagnostics. */
2299 static void
2300 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2302 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2303 && DECL_INITIAL (newdecl) != NULL_TREE);
2304 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2305 && prototype_p (TREE_TYPE (newdecl)));
2306 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2307 && prototype_p (TREE_TYPE (olddecl)));
2309 /* For real parm decl following a forward decl, rechain the old decl
2310 in its new location and clear TREE_ASM_WRITTEN (it's not a
2311 forward decl anymore). */
2312 if (TREE_CODE (newdecl) == PARM_DECL
2313 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2315 struct c_binding *b, **here;
2317 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2318 if ((*here)->decl == olddecl)
2319 goto found;
2320 gcc_unreachable ();
2322 found:
2323 b = *here;
2324 *here = b->prev;
2325 b->prev = current_scope->bindings;
2326 current_scope->bindings = b;
2328 TREE_ASM_WRITTEN (olddecl) = 0;
2331 DECL_ATTRIBUTES (newdecl)
2332 = targetm.merge_decl_attributes (olddecl, newdecl);
2334 /* For typedefs use the old type, as the new type's DECL_NAME points
2335 at newdecl, which will be ggc_freed. */
2336 if (TREE_CODE (newdecl) == TYPE_DECL)
2338 /* But NEWTYPE might have an attribute, honor that. */
2339 tree tem = newtype;
2340 newtype = oldtype;
2342 if (TYPE_USER_ALIGN (tem))
2344 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2345 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2346 TYPE_USER_ALIGN (newtype) = true;
2349 /* And remove the new type from the variants list. */
2350 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2352 tree remove = TREE_TYPE (newdecl);
2353 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2354 t = TYPE_NEXT_VARIANT (t))
2355 if (TYPE_NEXT_VARIANT (t) == remove)
2357 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2358 break;
2363 /* Merge the data types specified in the two decls. */
2364 TREE_TYPE (newdecl)
2365 = TREE_TYPE (olddecl)
2366 = composite_type (newtype, oldtype);
2368 /* Lay the type out, unless already done. */
2369 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2371 if (TREE_TYPE (newdecl) != error_mark_node)
2372 layout_type (TREE_TYPE (newdecl));
2373 if (TREE_CODE (newdecl) != FUNCTION_DECL
2374 && TREE_CODE (newdecl) != TYPE_DECL
2375 && TREE_CODE (newdecl) != CONST_DECL)
2376 layout_decl (newdecl, 0);
2378 else
2380 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2381 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2382 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2383 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2384 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2386 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2387 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2389 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2390 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2391 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2392 DECL_WARN_IF_NOT_ALIGN (olddecl));
2395 /* Keep the old rtl since we can safely use it. */
2396 if (HAS_RTL_P (olddecl))
2397 COPY_DECL_RTL (olddecl, newdecl);
2399 /* Merge the type qualifiers. */
2400 if (TREE_READONLY (newdecl))
2401 TREE_READONLY (olddecl) = 1;
2403 if (TREE_THIS_VOLATILE (newdecl))
2404 TREE_THIS_VOLATILE (olddecl) = 1;
2406 /* Merge deprecatedness. */
2407 if (TREE_DEPRECATED (newdecl))
2408 TREE_DEPRECATED (olddecl) = 1;
2410 /* If a decl is in a system header and the other isn't, keep the one on the
2411 system header. Otherwise, keep source location of definition rather than
2412 declaration and of prototype rather than non-prototype unless that
2413 prototype is built-in. */
2414 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2415 && DECL_IN_SYSTEM_HEADER (olddecl)
2416 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2417 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2418 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2419 && DECL_IN_SYSTEM_HEADER (newdecl)
2420 && !DECL_IN_SYSTEM_HEADER (olddecl))
2421 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2422 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2423 && DECL_INITIAL (olddecl) != NULL_TREE)
2424 || (old_is_prototype && !new_is_prototype
2425 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2426 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2428 /* Merge the initialization information. */
2429 if (DECL_INITIAL (newdecl) == NULL_TREE)
2430 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2432 /* Merge the threadprivate attribute. */
2433 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2434 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2436 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2438 /* Copy the assembler name.
2439 Currently, it can only be defined in the prototype. */
2440 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2442 /* Use visibility of whichever declaration had it specified */
2443 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2445 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2446 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2449 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2451 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2452 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2453 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2454 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2455 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2456 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2457 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2458 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2459 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2460 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2461 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2464 /* Merge the storage class information. */
2465 merge_weak (newdecl, olddecl);
2467 /* For functions, static overrides non-static. */
2468 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2470 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2471 /* This is since we don't automatically
2472 copy the attributes of NEWDECL into OLDDECL. */
2473 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2474 /* If this clears `static', clear it in the identifier too. */
2475 if (!TREE_PUBLIC (olddecl))
2476 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2480 /* In c99, 'extern' declaration before (or after) 'inline' means this
2481 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2482 is present. */
2483 if (TREE_CODE (newdecl) == FUNCTION_DECL
2484 && !flag_gnu89_inline
2485 && (DECL_DECLARED_INLINE_P (newdecl)
2486 || DECL_DECLARED_INLINE_P (olddecl))
2487 && (!DECL_DECLARED_INLINE_P (newdecl)
2488 || !DECL_DECLARED_INLINE_P (olddecl)
2489 || !DECL_EXTERNAL (olddecl))
2490 && DECL_EXTERNAL (newdecl)
2491 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2492 && !current_function_decl)
2493 DECL_EXTERNAL (newdecl) = 0;
2495 /* An inline definition following a static declaration is not
2496 DECL_EXTERNAL. */
2497 if (new_is_definition
2498 && (DECL_DECLARED_INLINE_P (newdecl)
2499 || DECL_DECLARED_INLINE_P (olddecl))
2500 && !TREE_PUBLIC (olddecl))
2501 DECL_EXTERNAL (newdecl) = 0;
2503 if (DECL_EXTERNAL (newdecl))
2505 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2506 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2508 /* An extern decl does not override previous storage class. */
2509 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2510 if (!DECL_EXTERNAL (newdecl))
2512 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2513 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2516 else
2518 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2519 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2522 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2524 /* If we're redefining a function previously defined as extern
2525 inline, make sure we emit debug info for the inline before we
2526 throw it away, in case it was inlined into a function that
2527 hasn't been written out yet. */
2528 if (new_is_definition && DECL_INITIAL (olddecl))
2529 /* The new defn must not be inline. */
2530 DECL_UNINLINABLE (newdecl) = 1;
2531 else
2533 /* If either decl says `inline', this fn is inline, unless
2534 its definition was passed already. */
2535 if (DECL_DECLARED_INLINE_P (newdecl)
2536 || DECL_DECLARED_INLINE_P (olddecl))
2537 DECL_DECLARED_INLINE_P (newdecl) = 1;
2539 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2540 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2542 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2543 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2544 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2545 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2548 if (DECL_BUILT_IN (olddecl))
2550 /* If redeclaring a builtin function, it stays built in.
2551 But it gets tagged as having been declared. */
2552 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2553 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2554 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2555 if (new_is_prototype)
2557 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2558 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2560 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2561 switch (fncode)
2563 /* If a compatible prototype of these builtin functions
2564 is seen, assume the runtime implements it with the
2565 expected semantics. */
2566 case BUILT_IN_STPCPY:
2567 if (builtin_decl_explicit_p (fncode))
2568 set_builtin_decl_implicit_p (fncode, true);
2569 break;
2570 default:
2571 if (builtin_decl_explicit_p (fncode))
2572 set_builtin_decl_declared_p (fncode, true);
2573 break;
2576 copy_attributes_to_builtin (newdecl);
2579 else
2580 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2581 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2584 /* Preserve function specific target and optimization options */
2585 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2586 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2587 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2588 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2590 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2591 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2592 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2593 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2595 /* Also preserve various other info from the definition. */
2596 if (!new_is_definition)
2598 tree t;
2599 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2600 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2601 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2602 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2603 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2604 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2605 DECL_CONTEXT (t) = newdecl;
2607 /* See if we've got a function to instantiate from. */
2608 if (DECL_SAVED_TREE (olddecl))
2609 DECL_ABSTRACT_ORIGIN (newdecl)
2610 = DECL_ABSTRACT_ORIGIN (olddecl);
2614 /* Merge the USED information. */
2615 if (TREE_USED (olddecl))
2616 TREE_USED (newdecl) = 1;
2617 else if (TREE_USED (newdecl))
2618 TREE_USED (olddecl) = 1;
2619 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2620 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2621 if (DECL_PRESERVE_P (olddecl))
2622 DECL_PRESERVE_P (newdecl) = 1;
2623 else if (DECL_PRESERVE_P (newdecl))
2624 DECL_PRESERVE_P (olddecl) = 1;
2626 /* Merge DECL_COMMON */
2627 if (VAR_P (olddecl) && VAR_P (newdecl)
2628 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2629 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2630 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2632 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2633 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2634 DECL_ARGUMENTS (if appropriate). */
2636 unsigned olddecl_uid = DECL_UID (olddecl);
2637 tree olddecl_context = DECL_CONTEXT (olddecl);
2638 tree olddecl_arguments = NULL;
2639 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2640 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2642 memcpy ((char *) olddecl + sizeof (struct tree_common),
2643 (char *) newdecl + sizeof (struct tree_common),
2644 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2645 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2646 switch (TREE_CODE (olddecl))
2648 case FUNCTION_DECL:
2649 case VAR_DECL:
2651 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2653 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2654 (char *) newdecl + sizeof (struct tree_decl_common),
2655 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2656 olddecl->decl_with_vis.symtab_node = snode;
2658 if ((DECL_EXTERNAL (olddecl)
2659 || TREE_PUBLIC (olddecl)
2660 || TREE_STATIC (olddecl))
2661 && DECL_SECTION_NAME (newdecl) != NULL)
2662 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2664 /* This isn't quite correct for something like
2665 int __thread x attribute ((tls_model ("local-exec")));
2666 extern int __thread x;
2667 as we'll lose the "local-exec" model. */
2668 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2669 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2670 break;
2673 case FIELD_DECL:
2674 case PARM_DECL:
2675 case LABEL_DECL:
2676 case RESULT_DECL:
2677 case CONST_DECL:
2678 case TYPE_DECL:
2679 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2680 (char *) newdecl + sizeof (struct tree_decl_common),
2681 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2682 break;
2684 default:
2686 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2687 (char *) newdecl + sizeof (struct tree_decl_common),
2688 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2690 DECL_UID (olddecl) = olddecl_uid;
2691 DECL_CONTEXT (olddecl) = olddecl_context;
2692 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2693 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2696 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2697 so that encode_section_info has a chance to look at the new decl
2698 flags and attributes. */
2699 if (DECL_RTL_SET_P (olddecl)
2700 && (TREE_CODE (olddecl) == FUNCTION_DECL
2701 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2702 make_decl_rtl (olddecl);
2705 /* Handle when a new declaration NEWDECL has the same name as an old
2706 one OLDDECL in the same binding contour. Prints an error message
2707 if appropriate.
2709 If safely possible, alter OLDDECL to look like NEWDECL, and return
2710 true. Otherwise, return false. */
2712 static bool
2713 duplicate_decls (tree newdecl, tree olddecl)
2715 tree newtype = NULL, oldtype = NULL;
2717 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2719 /* Avoid `unused variable' and other warnings for OLDDECL. */
2720 TREE_NO_WARNING (olddecl) = 1;
2721 return false;
2724 merge_decls (newdecl, olddecl, newtype, oldtype);
2726 /* The NEWDECL will no longer be needed.
2728 Before releasing the node, be sure to remove function from symbol
2729 table that might have been inserted there to record comdat group.
2730 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2731 structure is shared in between NEWDECL and OLDECL. */
2732 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2733 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2734 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2736 struct symtab_node *snode = symtab_node::get (newdecl);
2737 if (snode)
2738 snode->remove ();
2740 ggc_free (newdecl);
2741 return true;
2745 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2746 static void
2747 warn_if_shadowing (tree new_decl)
2749 struct c_binding *b;
2751 /* Shadow warnings wanted? */
2752 if (!(warn_shadow
2753 || warn_shadow_local
2754 || warn_shadow_compatible_local)
2755 /* No shadow warnings for internally generated vars. */
2756 || DECL_IS_BUILTIN (new_decl)
2757 /* No shadow warnings for vars made for inlining. */
2758 || DECL_FROM_INLINE (new_decl))
2759 return;
2761 /* Is anything being shadowed? Invisible decls do not count. */
2762 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2763 if (b->decl && b->decl != new_decl && !b->invisible
2764 && (b->decl == error_mark_node
2765 || diagnostic_report_warnings_p (global_dc,
2766 DECL_SOURCE_LOCATION (b->decl))))
2768 tree old_decl = b->decl;
2769 bool warned = false;
2771 if (old_decl == error_mark_node)
2773 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2774 "non-variable", new_decl);
2775 break;
2777 else if (TREE_CODE (old_decl) == PARM_DECL)
2779 enum opt_code warning_code;
2781 /* If '-Wshadow=compatible-local' is specified without other
2782 -Wshadow= flags, we will warn only when the types of the
2783 shadowing variable (i.e. new_decl) and the shadowed variable
2784 (old_decl) are compatible. */
2785 if (warn_shadow)
2786 warning_code = OPT_Wshadow;
2787 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2788 warning_code = OPT_Wshadow_compatible_local;
2789 else
2790 warning_code = OPT_Wshadow_local;
2791 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2792 "declaration of %qD shadows a parameter",
2793 new_decl);
2795 else if (DECL_FILE_SCOPE_P (old_decl))
2797 /* Do not warn if a variable shadows a function, unless
2798 the variable is a function or a pointer-to-function. */
2799 if (TREE_CODE (old_decl) == FUNCTION_DECL
2800 && TREE_CODE (new_decl) != FUNCTION_DECL
2801 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2802 continue;
2804 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2805 "declaration of %qD shadows a global "
2806 "declaration",
2807 new_decl);
2809 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2810 && DECL_BUILT_IN (old_decl))
2812 warning (OPT_Wshadow, "declaration of %q+D shadows "
2813 "a built-in function", new_decl);
2814 break;
2816 else
2818 enum opt_code warning_code;
2820 /* If '-Wshadow=compatible-local' is specified without other
2821 -Wshadow= flags, we will warn only when the types of the
2822 shadowing variable (i.e. new_decl) and the shadowed variable
2823 (old_decl) are compatible. */
2824 if (warn_shadow)
2825 warning_code = OPT_Wshadow;
2826 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2827 warning_code = OPT_Wshadow_compatible_local;
2828 else
2829 warning_code = OPT_Wshadow_local;
2830 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2831 "declaration of %qD shadows a previous local",
2832 new_decl);
2835 if (warned)
2836 inform (DECL_SOURCE_LOCATION (old_decl),
2837 "shadowed declaration is here");
2839 break;
2843 /* Record a decl-node X as belonging to the current lexical scope.
2844 Check for errors (such as an incompatible declaration for the same
2845 name already seen in the same scope).
2847 Returns either X or an old decl for the same name.
2848 If an old decl is returned, it may have been smashed
2849 to agree with what X says. */
2851 tree
2852 pushdecl (tree x)
2854 tree name = DECL_NAME (x);
2855 struct c_scope *scope = current_scope;
2856 struct c_binding *b;
2857 bool nested = false;
2858 location_t locus = DECL_SOURCE_LOCATION (x);
2860 /* Must set DECL_CONTEXT for everything not at file scope or
2861 DECL_FILE_SCOPE_P won't work. Local externs don't count
2862 unless they have initializers (which generate code). */
2863 if (current_function_decl
2864 && (!VAR_OR_FUNCTION_DECL_P (x)
2865 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2866 DECL_CONTEXT (x) = current_function_decl;
2868 /* Anonymous decls are just inserted in the scope. */
2869 if (!name)
2871 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2872 locus);
2873 return x;
2876 /* First, see if there is another declaration with the same name in
2877 the current scope. If there is, duplicate_decls may do all the
2878 work for us. If duplicate_decls returns false, that indicates
2879 two incompatible decls in the same scope; we are to silently
2880 replace the old one (duplicate_decls has issued all appropriate
2881 diagnostics). In particular, we should not consider possible
2882 duplicates in the external scope, or shadowing. */
2883 b = I_SYMBOL_BINDING (name);
2884 if (b && B_IN_SCOPE (b, scope))
2886 struct c_binding *b_ext, *b_use;
2887 tree type = TREE_TYPE (x);
2888 tree visdecl = b->decl;
2889 tree vistype = TREE_TYPE (visdecl);
2890 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2891 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2892 b->inner_comp = false;
2893 b_use = b;
2894 b_ext = b;
2895 /* If this is an external linkage declaration, we should check
2896 for compatibility with the type in the external scope before
2897 setting the type at this scope based on the visible
2898 information only. */
2899 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2901 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2902 b_ext = b_ext->shadowed;
2903 if (b_ext)
2905 b_use = b_ext;
2906 if (b_use->u.type)
2907 TREE_TYPE (b_use->decl) = b_use->u.type;
2910 if (duplicate_decls (x, b_use->decl))
2912 if (b_use != b)
2914 /* Save the updated type in the external scope and
2915 restore the proper type for this scope. */
2916 tree thistype;
2917 if (comptypes (vistype, type))
2918 thistype = composite_type (vistype, type);
2919 else
2920 thistype = TREE_TYPE (b_use->decl);
2921 b_use->u.type = TREE_TYPE (b_use->decl);
2922 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2923 && DECL_BUILT_IN (b_use->decl))
2924 thistype
2925 = build_type_attribute_variant (thistype,
2926 TYPE_ATTRIBUTES
2927 (b_use->u.type));
2928 TREE_TYPE (b_use->decl) = thistype;
2930 return b_use->decl;
2932 else
2933 goto skip_external_and_shadow_checks;
2936 /* All declarations with external linkage, and all external
2937 references, go in the external scope, no matter what scope is
2938 current. However, the binding in that scope is ignored for
2939 purposes of normal name lookup. A separate binding structure is
2940 created in the requested scope; this governs the normal
2941 visibility of the symbol.
2943 The binding in the externals scope is used exclusively for
2944 detecting duplicate declarations of the same object, no matter
2945 what scope they are in; this is what we do here. (C99 6.2.7p2:
2946 All declarations that refer to the same object or function shall
2947 have compatible type; otherwise, the behavior is undefined.) */
2948 if (DECL_EXTERNAL (x) || scope == file_scope)
2950 tree type = TREE_TYPE (x);
2951 tree vistype = NULL_TREE;
2952 tree visdecl = NULL_TREE;
2953 bool type_saved = false;
2954 if (b && !B_IN_EXTERNAL_SCOPE (b)
2955 && VAR_OR_FUNCTION_DECL_P (b->decl)
2956 && DECL_FILE_SCOPE_P (b->decl))
2958 visdecl = b->decl;
2959 vistype = TREE_TYPE (visdecl);
2961 if (scope != file_scope
2962 && !DECL_IN_SYSTEM_HEADER (x))
2963 warning_at (locus, OPT_Wnested_externs,
2964 "nested extern declaration of %qD", x);
2966 while (b && !B_IN_EXTERNAL_SCOPE (b))
2968 /* If this decl might be modified, save its type. This is
2969 done here rather than when the decl is first bound
2970 because the type may change after first binding, through
2971 being completed or through attributes being added. If we
2972 encounter multiple such decls, only the first should have
2973 its type saved; the others will already have had their
2974 proper types saved and the types will not have changed as
2975 their scopes will not have been re-entered. */
2976 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2978 b->u.type = TREE_TYPE (b->decl);
2979 type_saved = true;
2981 if (B_IN_FILE_SCOPE (b)
2982 && VAR_P (b->decl)
2983 && TREE_STATIC (b->decl)
2984 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2985 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2986 && TREE_CODE (type) == ARRAY_TYPE
2987 && TYPE_DOMAIN (type)
2988 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2989 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2991 /* Array type completed in inner scope, which should be
2992 diagnosed if the completion does not have size 1 and
2993 it does not get completed in the file scope. */
2994 b->inner_comp = true;
2996 b = b->shadowed;
2999 /* If a matching external declaration has been found, set its
3000 type to the composite of all the types of that declaration.
3001 After the consistency checks, it will be reset to the
3002 composite of the visible types only. */
3003 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3004 && b->u.type)
3005 TREE_TYPE (b->decl) = b->u.type;
3007 /* The point of the same_translation_unit_p check here is,
3008 we want to detect a duplicate decl for a construct like
3009 foo() { extern bar(); } ... static bar(); but not if
3010 they are in different translation units. In any case,
3011 the static does not go in the externals scope. */
3012 if (b
3013 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3014 && duplicate_decls (x, b->decl))
3016 tree thistype;
3017 if (vistype)
3019 if (comptypes (vistype, type))
3020 thistype = composite_type (vistype, type);
3021 else
3022 thistype = TREE_TYPE (b->decl);
3024 else
3025 thistype = type;
3026 b->u.type = TREE_TYPE (b->decl);
3027 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3028 thistype
3029 = build_type_attribute_variant (thistype,
3030 TYPE_ATTRIBUTES (b->u.type));
3031 TREE_TYPE (b->decl) = thistype;
3032 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3033 locus);
3034 return b->decl;
3036 else if (TREE_PUBLIC (x))
3038 if (visdecl && !b && duplicate_decls (x, visdecl))
3040 /* An external declaration at block scope referring to a
3041 visible entity with internal linkage. The composite
3042 type will already be correct for this scope, so we
3043 just need to fall through to make the declaration in
3044 this scope. */
3045 nested = true;
3046 x = visdecl;
3048 else
3050 bind (name, x, external_scope, /*invisible=*/true,
3051 /*nested=*/false, locus);
3052 nested = true;
3057 if (TREE_CODE (x) != PARM_DECL)
3058 warn_if_shadowing (x);
3060 skip_external_and_shadow_checks:
3061 if (TREE_CODE (x) == TYPE_DECL)
3063 /* So this is a typedef, set its underlying type. */
3064 set_underlying_type (x);
3066 /* If X is a typedef defined in the current function, record it
3067 for the purpose of implementing the -Wunused-local-typedefs
3068 warning. */
3069 record_locally_defined_typedef (x);
3072 bind (name, x, scope, /*invisible=*/false, nested, locus);
3074 /* If x's type is incomplete because it's based on a
3075 structure or union which has not yet been fully declared,
3076 attach it to that structure or union type, so we can go
3077 back and complete the variable declaration later, if the
3078 structure or union gets fully declared.
3080 If the input is erroneous, we can have error_mark in the type
3081 slot (e.g. "f(void a, ...)") - that doesn't count as an
3082 incomplete type. */
3083 if (TREE_TYPE (x) != error_mark_node
3084 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3086 tree element = TREE_TYPE (x);
3088 while (TREE_CODE (element) == ARRAY_TYPE)
3089 element = TREE_TYPE (element);
3090 element = TYPE_MAIN_VARIANT (element);
3092 if (RECORD_OR_UNION_TYPE_P (element)
3093 && (TREE_CODE (x) != TYPE_DECL
3094 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3095 && !COMPLETE_TYPE_P (element))
3096 C_TYPE_INCOMPLETE_VARS (element)
3097 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3099 return x;
3103 /* Issue a warning about implicit function declaration. ID is the function
3104 identifier, OLDDECL is a declaration of the function in a different scope,
3105 or NULL_TREE. */
3107 static void
3108 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3110 if (!warn_implicit_function_declaration)
3111 return;
3113 bool warned;
3114 name_hint hint;
3115 if (!olddecl)
3116 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3118 if (flag_isoc99)
3120 if (hint)
3122 gcc_rich_location richloc (loc);
3123 richloc.add_fixit_replace (hint.suggestion ());
3124 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3125 "implicit declaration of function %qE;"
3126 " did you mean %qs?",
3127 id, hint.suggestion ());
3129 else
3130 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3131 "implicit declaration of function %qE", id);
3133 else if (hint)
3135 gcc_rich_location richloc (loc);
3136 richloc.add_fixit_replace (hint.suggestion ());
3137 warned = warning_at
3138 (&richloc, OPT_Wimplicit_function_declaration,
3139 G_("implicit declaration of function %qE; did you mean %qs?"),
3140 id, hint.suggestion ());
3142 else
3143 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3144 G_("implicit declaration of function %qE"), id);
3146 if (olddecl && warned)
3147 locate_old_decl (olddecl);
3149 if (!warned)
3150 hint.suppress ();
3153 /* This function represents mapping of a function code FCODE
3154 to its respective header. */
3156 static const char *
3157 header_for_builtin_fn (enum built_in_function fcode)
3159 switch (fcode)
3161 CASE_FLT_FN (BUILT_IN_ACOS):
3162 CASE_FLT_FN (BUILT_IN_ACOSH):
3163 CASE_FLT_FN (BUILT_IN_ASIN):
3164 CASE_FLT_FN (BUILT_IN_ASINH):
3165 CASE_FLT_FN (BUILT_IN_ATAN):
3166 CASE_FLT_FN (BUILT_IN_ATANH):
3167 CASE_FLT_FN (BUILT_IN_ATAN2):
3168 CASE_FLT_FN (BUILT_IN_CBRT):
3169 CASE_FLT_FN (BUILT_IN_CEIL):
3170 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3171 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3172 CASE_FLT_FN (BUILT_IN_COS):
3173 CASE_FLT_FN (BUILT_IN_COSH):
3174 CASE_FLT_FN (BUILT_IN_ERF):
3175 CASE_FLT_FN (BUILT_IN_ERFC):
3176 CASE_FLT_FN (BUILT_IN_EXP):
3177 CASE_FLT_FN (BUILT_IN_EXP2):
3178 CASE_FLT_FN (BUILT_IN_EXPM1):
3179 CASE_FLT_FN (BUILT_IN_FABS):
3180 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3181 CASE_FLT_FN (BUILT_IN_FDIM):
3182 CASE_FLT_FN (BUILT_IN_FLOOR):
3183 CASE_FLT_FN (BUILT_IN_FMA):
3184 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3185 CASE_FLT_FN (BUILT_IN_FMAX):
3186 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3187 CASE_FLT_FN (BUILT_IN_FMIN):
3188 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3189 CASE_FLT_FN (BUILT_IN_FMOD):
3190 CASE_FLT_FN (BUILT_IN_FREXP):
3191 CASE_FLT_FN (BUILT_IN_HYPOT):
3192 CASE_FLT_FN (BUILT_IN_ILOGB):
3193 CASE_FLT_FN (BUILT_IN_LDEXP):
3194 CASE_FLT_FN (BUILT_IN_LGAMMA):
3195 CASE_FLT_FN (BUILT_IN_LLRINT):
3196 CASE_FLT_FN (BUILT_IN_LLROUND):
3197 CASE_FLT_FN (BUILT_IN_LOG):
3198 CASE_FLT_FN (BUILT_IN_LOG10):
3199 CASE_FLT_FN (BUILT_IN_LOG1P):
3200 CASE_FLT_FN (BUILT_IN_LOG2):
3201 CASE_FLT_FN (BUILT_IN_LOGB):
3202 CASE_FLT_FN (BUILT_IN_LRINT):
3203 CASE_FLT_FN (BUILT_IN_LROUND):
3204 CASE_FLT_FN (BUILT_IN_MODF):
3205 CASE_FLT_FN (BUILT_IN_NAN):
3206 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3207 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3208 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3209 CASE_FLT_FN (BUILT_IN_POW):
3210 CASE_FLT_FN (BUILT_IN_REMAINDER):
3211 CASE_FLT_FN (BUILT_IN_REMQUO):
3212 CASE_FLT_FN (BUILT_IN_RINT):
3213 CASE_FLT_FN (BUILT_IN_ROUND):
3214 CASE_FLT_FN (BUILT_IN_SCALBLN):
3215 CASE_FLT_FN (BUILT_IN_SCALBN):
3216 CASE_FLT_FN (BUILT_IN_SIN):
3217 CASE_FLT_FN (BUILT_IN_SINH):
3218 CASE_FLT_FN (BUILT_IN_SINCOS):
3219 CASE_FLT_FN (BUILT_IN_SQRT):
3220 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3221 CASE_FLT_FN (BUILT_IN_TAN):
3222 CASE_FLT_FN (BUILT_IN_TANH):
3223 CASE_FLT_FN (BUILT_IN_TGAMMA):
3224 CASE_FLT_FN (BUILT_IN_TRUNC):
3225 case BUILT_IN_ISINF:
3226 case BUILT_IN_ISNAN:
3227 return "<math.h>";
3228 CASE_FLT_FN (BUILT_IN_CABS):
3229 CASE_FLT_FN (BUILT_IN_CACOS):
3230 CASE_FLT_FN (BUILT_IN_CACOSH):
3231 CASE_FLT_FN (BUILT_IN_CARG):
3232 CASE_FLT_FN (BUILT_IN_CASIN):
3233 CASE_FLT_FN (BUILT_IN_CASINH):
3234 CASE_FLT_FN (BUILT_IN_CATAN):
3235 CASE_FLT_FN (BUILT_IN_CATANH):
3236 CASE_FLT_FN (BUILT_IN_CCOS):
3237 CASE_FLT_FN (BUILT_IN_CCOSH):
3238 CASE_FLT_FN (BUILT_IN_CEXP):
3239 CASE_FLT_FN (BUILT_IN_CIMAG):
3240 CASE_FLT_FN (BUILT_IN_CLOG):
3241 CASE_FLT_FN (BUILT_IN_CONJ):
3242 CASE_FLT_FN (BUILT_IN_CPOW):
3243 CASE_FLT_FN (BUILT_IN_CPROJ):
3244 CASE_FLT_FN (BUILT_IN_CREAL):
3245 CASE_FLT_FN (BUILT_IN_CSIN):
3246 CASE_FLT_FN (BUILT_IN_CSINH):
3247 CASE_FLT_FN (BUILT_IN_CSQRT):
3248 CASE_FLT_FN (BUILT_IN_CTAN):
3249 CASE_FLT_FN (BUILT_IN_CTANH):
3250 return "<complex.h>";
3251 case BUILT_IN_MEMCHR:
3252 case BUILT_IN_MEMCMP:
3253 case BUILT_IN_MEMCPY:
3254 case BUILT_IN_MEMMOVE:
3255 case BUILT_IN_MEMSET:
3256 case BUILT_IN_STRCAT:
3257 case BUILT_IN_STRCHR:
3258 case BUILT_IN_STRCMP:
3259 case BUILT_IN_STRCPY:
3260 case BUILT_IN_STRCSPN:
3261 case BUILT_IN_STRLEN:
3262 case BUILT_IN_STRNCAT:
3263 case BUILT_IN_STRNCMP:
3264 case BUILT_IN_STRNCPY:
3265 case BUILT_IN_STRPBRK:
3266 case BUILT_IN_STRRCHR:
3267 case BUILT_IN_STRSPN:
3268 case BUILT_IN_STRSTR:
3269 return "<string.h>";
3270 case BUILT_IN_FPRINTF:
3271 case BUILT_IN_PUTC:
3272 case BUILT_IN_FPUTC:
3273 case BUILT_IN_FPUTS:
3274 case BUILT_IN_FSCANF:
3275 case BUILT_IN_FWRITE:
3276 case BUILT_IN_PRINTF:
3277 case BUILT_IN_PUTCHAR:
3278 case BUILT_IN_PUTS:
3279 case BUILT_IN_SCANF:
3280 case BUILT_IN_SNPRINTF:
3281 case BUILT_IN_SPRINTF:
3282 case BUILT_IN_SSCANF:
3283 case BUILT_IN_VFPRINTF:
3284 case BUILT_IN_VFSCANF:
3285 case BUILT_IN_VPRINTF:
3286 case BUILT_IN_VSCANF:
3287 case BUILT_IN_VSNPRINTF:
3288 case BUILT_IN_VSPRINTF:
3289 case BUILT_IN_VSSCANF:
3290 return "<stdio.h>";
3291 case BUILT_IN_ISALNUM:
3292 case BUILT_IN_ISALPHA:
3293 case BUILT_IN_ISBLANK:
3294 case BUILT_IN_ISCNTRL:
3295 case BUILT_IN_ISDIGIT:
3296 case BUILT_IN_ISGRAPH:
3297 case BUILT_IN_ISLOWER:
3298 case BUILT_IN_ISPRINT:
3299 case BUILT_IN_ISPUNCT:
3300 case BUILT_IN_ISSPACE:
3301 case BUILT_IN_ISUPPER:
3302 case BUILT_IN_ISXDIGIT:
3303 case BUILT_IN_TOLOWER:
3304 case BUILT_IN_TOUPPER:
3305 return "<ctype.h>";
3306 case BUILT_IN_ISWALNUM:
3307 case BUILT_IN_ISWALPHA:
3308 case BUILT_IN_ISWBLANK:
3309 case BUILT_IN_ISWCNTRL:
3310 case BUILT_IN_ISWDIGIT:
3311 case BUILT_IN_ISWGRAPH:
3312 case BUILT_IN_ISWLOWER:
3313 case BUILT_IN_ISWPRINT:
3314 case BUILT_IN_ISWPUNCT:
3315 case BUILT_IN_ISWSPACE:
3316 case BUILT_IN_ISWUPPER:
3317 case BUILT_IN_ISWXDIGIT:
3318 case BUILT_IN_TOWLOWER:
3319 case BUILT_IN_TOWUPPER:
3320 return "<wctype.h>";
3321 case BUILT_IN_ABORT:
3322 case BUILT_IN_ABS:
3323 case BUILT_IN_CALLOC:
3324 case BUILT_IN_EXIT:
3325 case BUILT_IN_FREE:
3326 case BUILT_IN_LABS:
3327 case BUILT_IN_LLABS:
3328 case BUILT_IN_MALLOC:
3329 case BUILT_IN_REALLOC:
3330 case BUILT_IN__EXIT2:
3331 case BUILT_IN_ALIGNED_ALLOC:
3332 return "<stdlib.h>";
3333 case BUILT_IN_IMAXABS:
3334 return "<inttypes.h>";
3335 case BUILT_IN_STRFTIME:
3336 return "<time.h>";
3337 default:
3338 return NULL;
3342 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3343 function of type int (). */
3345 tree
3346 implicitly_declare (location_t loc, tree functionid)
3348 struct c_binding *b;
3349 tree decl = NULL_TREE;
3350 tree asmspec_tree;
3352 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3354 if (B_IN_SCOPE (b, external_scope))
3356 decl = b->decl;
3357 break;
3361 if (decl)
3363 if (TREE_CODE (decl) != FUNCTION_DECL)
3364 return decl;
3366 /* FIXME: Objective-C has weird not-really-builtin functions
3367 which are supposed to be visible automatically. They wind up
3368 in the external scope because they're pushed before the file
3369 scope gets created. Catch this here and rebind them into the
3370 file scope. */
3371 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3373 bind (functionid, decl, file_scope,
3374 /*invisible=*/false, /*nested=*/true,
3375 DECL_SOURCE_LOCATION (decl));
3376 return decl;
3378 else
3380 tree newtype = default_function_type;
3381 if (b->u.type)
3382 TREE_TYPE (decl) = b->u.type;
3383 /* Implicit declaration of a function already declared
3384 (somehow) in a different scope, or as a built-in.
3385 If this is the first time this has happened, warn;
3386 then recycle the old declaration but with the new type. */
3387 if (!C_DECL_IMPLICIT (decl))
3389 implicit_decl_warning (loc, functionid, decl);
3390 C_DECL_IMPLICIT (decl) = 1;
3392 if (DECL_BUILT_IN (decl))
3394 newtype = build_type_attribute_variant (newtype,
3395 TYPE_ATTRIBUTES
3396 (TREE_TYPE (decl)));
3397 if (!comptypes (newtype, TREE_TYPE (decl)))
3399 bool warned = warning_at (loc, 0, "incompatible implicit "
3400 "declaration of built-in "
3401 "function %qD", decl);
3402 /* See if we can hint which header to include. */
3403 const char *header
3404 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3405 if (header != NULL && warned)
3407 rich_location richloc (line_table, loc);
3408 maybe_add_include_fixit (&richloc, header);
3409 inform (&richloc,
3410 "include %qs or provide a declaration of %qD",
3411 header, decl);
3413 newtype = TREE_TYPE (decl);
3416 else
3418 if (!comptypes (newtype, TREE_TYPE (decl)))
3420 error_at (loc, "incompatible implicit declaration of "
3421 "function %qD", decl);
3422 locate_old_decl (decl);
3425 b->u.type = TREE_TYPE (decl);
3426 TREE_TYPE (decl) = newtype;
3427 bind (functionid, decl, current_scope,
3428 /*invisible=*/false, /*nested=*/true,
3429 DECL_SOURCE_LOCATION (decl));
3430 return decl;
3434 /* Not seen before. */
3435 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3436 DECL_EXTERNAL (decl) = 1;
3437 TREE_PUBLIC (decl) = 1;
3438 C_DECL_IMPLICIT (decl) = 1;
3439 implicit_decl_warning (loc, functionid, 0);
3440 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3441 if (asmspec_tree)
3442 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3444 /* C89 says implicit declarations are in the innermost block.
3445 So we record the decl in the standard fashion. */
3446 decl = pushdecl (decl);
3448 /* No need to call objc_check_decl here - it's a function type. */
3449 rest_of_decl_compilation (decl, 0, 0);
3451 /* Write a record describing this implicit function declaration
3452 to the prototypes file (if requested). */
3453 gen_aux_info_record (decl, 0, 1, 0);
3455 /* Possibly apply some default attributes to this implicit declaration. */
3456 decl_attributes (&decl, NULL_TREE, 0);
3458 return decl;
3461 /* Issue an error message for a reference to an undeclared variable
3462 ID, including a reference to a builtin outside of function-call
3463 context. Establish a binding of the identifier to error_mark_node
3464 in an appropriate scope, which will suppress further errors for the
3465 same identifier. The error message should be given location LOC. */
3466 void
3467 undeclared_variable (location_t loc, tree id)
3469 static bool already = false;
3470 struct c_scope *scope;
3472 if (current_function_decl == NULL_TREE)
3474 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3475 if (guessed_id)
3477 gcc_rich_location richloc (loc);
3478 richloc.add_fixit_replace (guessed_id.suggestion ());
3479 error_at (&richloc,
3480 "%qE undeclared here (not in a function);"
3481 " did you mean %qs?",
3482 id, guessed_id.suggestion ());
3484 else
3485 error_at (loc, "%qE undeclared here (not in a function)", id);
3486 scope = current_scope;
3488 else
3490 if (!objc_diagnose_private_ivar (id))
3492 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3493 if (guessed_id)
3495 gcc_rich_location richloc (loc);
3496 richloc.add_fixit_replace (guessed_id.suggestion ());
3497 error_at (&richloc,
3498 "%qE undeclared (first use in this function);"
3499 " did you mean %qs?",
3500 id, guessed_id.suggestion ());
3502 else
3503 error_at (loc, "%qE undeclared (first use in this function)", id);
3505 if (!already)
3507 inform (loc, "each undeclared identifier is reported only"
3508 " once for each function it appears in");
3509 already = true;
3512 /* If we are parsing old-style parameter decls, current_function_decl
3513 will be nonnull but current_function_scope will be null. */
3514 scope = current_function_scope ? current_function_scope : current_scope;
3516 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3517 UNKNOWN_LOCATION);
3520 /* Subroutine of lookup_label, declare_label, define_label: construct a
3521 LABEL_DECL with all the proper frills. Also create a struct
3522 c_label_vars initialized for the current scope. */
3524 static tree
3525 make_label (location_t location, tree name, bool defining,
3526 struct c_label_vars **p_label_vars)
3528 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3529 DECL_CONTEXT (label) = current_function_decl;
3530 SET_DECL_MODE (label, VOIDmode);
3532 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3533 label_vars->shadowed = NULL;
3534 set_spot_bindings (&label_vars->label_bindings, defining);
3535 label_vars->decls_in_scope = make_tree_vector ();
3536 label_vars->gotos = NULL;
3537 *p_label_vars = label_vars;
3539 return label;
3542 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3543 Create one if none exists so far for the current function.
3544 This is called when a label is used in a goto expression or
3545 has its address taken. */
3547 tree
3548 lookup_label (tree name)
3550 tree label;
3551 struct c_label_vars *label_vars;
3553 if (current_function_scope == 0)
3555 error ("label %qE referenced outside of any function", name);
3556 return NULL_TREE;
3559 /* Use a label already defined or ref'd with this name, but not if
3560 it is inherited from a containing function and wasn't declared
3561 using __label__. */
3562 label = I_LABEL_DECL (name);
3563 if (label && (DECL_CONTEXT (label) == current_function_decl
3564 || C_DECLARED_LABEL_FLAG (label)))
3566 /* If the label has only been declared, update its apparent
3567 location to point here, for better diagnostics if it
3568 turns out not to have been defined. */
3569 if (DECL_INITIAL (label) == NULL_TREE)
3570 DECL_SOURCE_LOCATION (label) = input_location;
3571 return label;
3574 /* No label binding for that identifier; make one. */
3575 label = make_label (input_location, name, false, &label_vars);
3577 /* Ordinary labels go in the current function scope. */
3578 bind_label (name, label, current_function_scope, label_vars);
3580 return label;
3583 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3584 to LABEL. */
3586 static void
3587 warn_about_goto (location_t goto_loc, tree label, tree decl)
3589 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3590 error_at (goto_loc,
3591 "jump into scope of identifier with variably modified type");
3592 else
3593 warning_at (goto_loc, OPT_Wjump_misses_init,
3594 "jump skips variable initialization");
3595 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3596 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3599 /* Look up a label because of a goto statement. This is like
3600 lookup_label, but also issues any appropriate warnings. */
3602 tree
3603 lookup_label_for_goto (location_t loc, tree name)
3605 tree label;
3606 struct c_label_vars *label_vars;
3607 unsigned int ix;
3608 tree decl;
3610 label = lookup_label (name);
3611 if (label == NULL_TREE)
3612 return NULL_TREE;
3614 /* If we are jumping to a different function, we can't issue any
3615 useful warnings. */
3616 if (DECL_CONTEXT (label) != current_function_decl)
3618 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3619 return label;
3622 label_vars = I_LABEL_BINDING (name)->u.label;
3624 /* If the label has not yet been defined, then push this goto on a
3625 list for possible later warnings. */
3626 if (label_vars->label_bindings.scope == NULL)
3628 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3630 g->loc = loc;
3631 set_spot_bindings (&g->goto_bindings, true);
3632 vec_safe_push (label_vars->gotos, g);
3633 return label;
3636 /* If there are any decls in label_vars->decls_in_scope, then this
3637 goto has missed the declaration of the decl. This happens for a
3638 case like
3639 int i = 1;
3640 lab:
3642 goto lab;
3643 Issue a warning or error. */
3644 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3645 warn_about_goto (loc, label, decl);
3647 if (label_vars->label_bindings.left_stmt_expr)
3649 error_at (loc, "jump into statement expression");
3650 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3653 return label;
3656 /* Make a label named NAME in the current function, shadowing silently
3657 any that may be inherited from containing functions or containing
3658 scopes. This is called for __label__ declarations. */
3660 tree
3661 declare_label (tree name)
3663 struct c_binding *b = I_LABEL_BINDING (name);
3664 tree label;
3665 struct c_label_vars *label_vars;
3667 /* Check to make sure that the label hasn't already been declared
3668 at this scope */
3669 if (b && B_IN_CURRENT_SCOPE (b))
3671 error ("duplicate label declaration %qE", name);
3672 locate_old_decl (b->decl);
3674 /* Just use the previous declaration. */
3675 return b->decl;
3678 label = make_label (input_location, name, false, &label_vars);
3679 C_DECLARED_LABEL_FLAG (label) = 1;
3681 /* Declared labels go in the current scope. */
3682 bind_label (name, label, current_scope, label_vars);
3684 return label;
3687 /* When we define a label, issue any appropriate warnings if there are
3688 any gotos earlier in the function which jump to this label. */
3690 static void
3691 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3693 unsigned int ix;
3694 struct c_goto_bindings *g;
3696 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3698 struct c_binding *b;
3699 struct c_scope *scope;
3701 /* We have a goto to this label. The goto is going forward. In
3702 g->scope, the goto is going to skip any binding which was
3703 defined after g->bindings_in_scope. */
3704 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3706 for (b = g->goto_bindings.scope->bindings;
3707 b != g->goto_bindings.bindings_in_scope;
3708 b = b->prev)
3710 if (decl_jump_unsafe (b->decl))
3711 warn_about_goto (g->loc, label, b->decl);
3715 /* We also need to warn about decls defined in any scopes
3716 between the scope of the label and the scope of the goto. */
3717 for (scope = label_vars->label_bindings.scope;
3718 scope != g->goto_bindings.scope;
3719 scope = scope->outer)
3721 gcc_assert (scope != NULL);
3722 if (scope->has_jump_unsafe_decl)
3724 if (scope == label_vars->label_bindings.scope)
3725 b = label_vars->label_bindings.bindings_in_scope;
3726 else
3727 b = scope->bindings;
3728 for (; b != NULL; b = b->prev)
3730 if (decl_jump_unsafe (b->decl))
3731 warn_about_goto (g->loc, label, b->decl);
3736 if (g->goto_bindings.stmt_exprs > 0)
3738 error_at (g->loc, "jump into statement expression");
3739 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3740 label);
3744 /* Now that the label is defined, we will issue warnings about
3745 subsequent gotos to this label when we see them. */
3746 vec_safe_truncate (label_vars->gotos, 0);
3747 label_vars->gotos = NULL;
3750 /* Define a label, specifying the location in the source file.
3751 Return the LABEL_DECL node for the label, if the definition is valid.
3752 Otherwise return NULL_TREE. */
3754 tree
3755 define_label (location_t location, tree name)
3757 /* Find any preexisting label with this name. It is an error
3758 if that label has already been defined in this function, or
3759 if there is a containing function with a declared label with
3760 the same name. */
3761 tree label = I_LABEL_DECL (name);
3763 if (label
3764 && ((DECL_CONTEXT (label) == current_function_decl
3765 && DECL_INITIAL (label) != NULL_TREE)
3766 || (DECL_CONTEXT (label) != current_function_decl
3767 && C_DECLARED_LABEL_FLAG (label))))
3769 error_at (location, "duplicate label %qD", label);
3770 locate_old_decl (label);
3771 return NULL_TREE;
3773 else if (label && DECL_CONTEXT (label) == current_function_decl)
3775 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3777 /* The label has been used or declared already in this function,
3778 but not defined. Update its location to point to this
3779 definition. */
3780 DECL_SOURCE_LOCATION (label) = location;
3781 set_spot_bindings (&label_vars->label_bindings, true);
3783 /* Issue warnings as required about any goto statements from
3784 earlier in the function. */
3785 check_earlier_gotos (label, label_vars);
3787 else
3789 struct c_label_vars *label_vars;
3791 /* No label binding for that identifier; make one. */
3792 label = make_label (location, name, true, &label_vars);
3794 /* Ordinary labels go in the current function scope. */
3795 bind_label (name, label, current_function_scope, label_vars);
3798 if (!in_system_header_at (input_location) && lookup_name (name))
3799 warning_at (location, OPT_Wtraditional,
3800 "traditional C lacks a separate namespace "
3801 "for labels, identifier %qE conflicts", name);
3803 /* Mark label as having been defined. */
3804 DECL_INITIAL (label) = error_mark_node;
3805 return label;
3808 /* Get the bindings for a new switch statement. This is used to issue
3809 warnings as appropriate for jumps from the switch to case or
3810 default labels. */
3812 struct c_spot_bindings *
3813 c_get_switch_bindings (void)
3815 struct c_spot_bindings *switch_bindings;
3817 switch_bindings = XNEW (struct c_spot_bindings);
3818 set_spot_bindings (switch_bindings, true);
3819 return switch_bindings;
3822 void
3823 c_release_switch_bindings (struct c_spot_bindings *bindings)
3825 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3826 XDELETE (bindings);
3829 /* This is called at the point of a case or default label to issue
3830 warnings about decls as needed. It returns true if it found an
3831 error, not just a warning. */
3833 bool
3834 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3835 location_t switch_loc, location_t case_loc)
3837 bool saw_error;
3838 struct c_scope *scope;
3840 saw_error = false;
3841 for (scope = current_scope;
3842 scope != switch_bindings->scope;
3843 scope = scope->outer)
3845 struct c_binding *b;
3847 gcc_assert (scope != NULL);
3849 if (!scope->has_jump_unsafe_decl)
3850 continue;
3852 for (b = scope->bindings; b != NULL; b = b->prev)
3854 if (decl_jump_unsafe (b->decl))
3856 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3858 saw_error = true;
3859 error_at (case_loc,
3860 ("switch jumps into scope of identifier with "
3861 "variably modified type"));
3863 else
3864 warning_at (case_loc, OPT_Wjump_misses_init,
3865 "switch jumps over variable initialization");
3866 inform (switch_loc, "switch starts here");
3867 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3868 b->decl);
3873 if (switch_bindings->stmt_exprs > 0)
3875 saw_error = true;
3876 error_at (case_loc, "switch jumps into statement expression");
3877 inform (switch_loc, "switch starts here");
3880 return saw_error;
3883 /* Given NAME, an IDENTIFIER_NODE,
3884 return the structure (or union or enum) definition for that name.
3885 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3886 CODE says which kind of type the caller wants;
3887 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3888 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3889 location where the tag was defined.
3890 If the wrong kind of type is found, an error is reported. */
3892 static tree
3893 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3894 location_t *ploc)
3896 struct c_binding *b = I_TAG_BINDING (name);
3897 bool thislevel = false;
3899 if (!b || !b->decl)
3900 return NULL_TREE;
3902 /* We only care about whether it's in this level if
3903 thislevel_only was set or it might be a type clash. */
3904 if (thislevel_only || TREE_CODE (b->decl) != code)
3906 /* For our purposes, a tag in the external scope is the same as
3907 a tag in the file scope. (Primarily relevant to Objective-C
3908 and its builtin structure tags, which get pushed before the
3909 file scope is created.) */
3910 if (B_IN_CURRENT_SCOPE (b)
3911 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3912 thislevel = true;
3915 if (thislevel_only && !thislevel)
3916 return NULL_TREE;
3918 if (TREE_CODE (b->decl) != code)
3920 /* Definition isn't the kind we were looking for. */
3921 pending_invalid_xref = name;
3922 pending_invalid_xref_location = input_location;
3924 /* If in the same binding level as a declaration as a tag
3925 of a different type, this must not be allowed to
3926 shadow that tag, so give the error immediately.
3927 (For example, "struct foo; union foo;" is invalid.) */
3928 if (thislevel)
3929 pending_xref_error ();
3932 if (ploc != NULL)
3933 *ploc = b->locus;
3935 return b->decl;
3938 /* Return true if a definition exists for NAME with code CODE. */
3940 bool
3941 tag_exists_p (enum tree_code code, tree name)
3943 struct c_binding *b = I_TAG_BINDING (name);
3945 if (b == NULL || b->decl == NULL_TREE)
3946 return false;
3947 return TREE_CODE (b->decl) == code;
3950 /* Print an error message now
3951 for a recent invalid struct, union or enum cross reference.
3952 We don't print them immediately because they are not invalid
3953 when used in the `struct foo;' construct for shadowing. */
3955 void
3956 pending_xref_error (void)
3958 if (pending_invalid_xref != NULL_TREE)
3959 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3960 pending_invalid_xref);
3961 pending_invalid_xref = NULL_TREE;
3965 /* Look up NAME in the current scope and its superiors
3966 in the namespace of variables, functions and typedefs.
3967 Return a ..._DECL node of some kind representing its definition,
3968 or return NULL_TREE if it is undefined. */
3970 tree
3971 lookup_name (tree name)
3973 struct c_binding *b = I_SYMBOL_BINDING (name);
3974 if (b && !b->invisible)
3976 maybe_record_typedef_use (b->decl);
3977 return b->decl;
3979 return NULL_TREE;
3982 /* Similar to `lookup_name' but look only at the indicated scope. */
3984 static tree
3985 lookup_name_in_scope (tree name, struct c_scope *scope)
3987 struct c_binding *b;
3989 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3990 if (B_IN_SCOPE (b, scope))
3991 return b->decl;
3992 return NULL_TREE;
3995 /* Look for the closest match for NAME within the currently valid
3996 scopes.
3998 This finds the identifier with the lowest Levenshtein distance to
3999 NAME. If there are multiple candidates with equal minimal distance,
4000 the first one found is returned. Scopes are searched from innermost
4001 outwards, and within a scope in reverse order of declaration, thus
4002 benefiting candidates "near" to the current scope.
4004 The function also looks for similar macro names to NAME, since a
4005 misspelled macro name will not be expanded, and hence looks like an
4006 identifier to the C frontend.
4008 It also looks for start_typename keywords, to detect "singed" vs "signed"
4009 typos.
4011 Use LOC for any deferred diagnostics. */
4013 name_hint
4014 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4016 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4018 /* First, try some well-known names in the C standard library, in case
4019 the user forgot a #include. */
4020 const char *header_hint
4021 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4023 if (header_hint)
4024 return name_hint (NULL,
4025 new suggest_missing_header (loc,
4026 IDENTIFIER_POINTER (name),
4027 header_hint));
4029 best_match<tree, tree> bm (name);
4031 /* Look within currently valid scopes. */
4032 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4033 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4035 if (!binding->id || binding->invisible)
4036 continue;
4037 if (binding->decl == error_mark_node)
4038 continue;
4039 /* Don't use bindings from implicitly declared functions,
4040 as they were likely misspellings themselves. */
4041 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4042 if (C_DECL_IMPLICIT (binding->decl))
4043 continue;
4044 switch (kind)
4046 case FUZZY_LOOKUP_TYPENAME:
4047 if (TREE_CODE (binding->decl) != TYPE_DECL)
4048 continue;
4049 break;
4051 case FUZZY_LOOKUP_FUNCTION_NAME:
4052 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4054 /* Allow function pointers. */
4055 if ((VAR_P (binding->decl)
4056 || TREE_CODE (binding->decl) == PARM_DECL)
4057 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4058 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4059 == FUNCTION_TYPE))
4060 break;
4061 continue;
4063 break;
4065 default:
4066 break;
4068 bm.consider (binding->id);
4071 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4073 x = SOME_OTHER_MACRO (y);
4074 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4075 as a misspelled identifier.
4077 Use the best distance so far so that a candidate is only set if
4078 a macro is better than anything so far. This allows early rejection
4079 (without calculating the edit distance) of macro names that must have
4080 distance >= bm.get_best_distance (), and means that we only get a
4081 non-NULL result for best_macro_match if it's better than any of
4082 the identifiers already checked, which avoids needless creation
4083 of identifiers for macro hashnodes. */
4084 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4085 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4086 /* If a macro is the closest so far to NAME, use it, creating an
4087 identifier tree node for it. */
4088 if (best_macro)
4090 const char *id = (const char *)best_macro->ident.str;
4091 tree macro_as_identifier
4092 = get_identifier_with_length (id, best_macro->ident.len);
4093 bm.set_best_so_far (macro_as_identifier,
4094 bmm.get_best_distance (),
4095 bmm.get_best_candidate_length ());
4098 /* Try the "start_typename" keywords to detect
4099 "singed" vs "signed" typos. */
4100 if (kind == FUZZY_LOOKUP_TYPENAME)
4102 for (unsigned i = 0; i < num_c_common_reswords; i++)
4104 const c_common_resword *resword = &c_common_reswords[i];
4105 if (!c_keyword_starts_typename (resword->rid))
4106 continue;
4107 tree resword_identifier = ridpointers [resword->rid];
4108 if (!resword_identifier)
4109 continue;
4110 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4111 bm.consider (resword_identifier);
4115 tree best = bm.get_best_meaningful_candidate ();
4116 if (best)
4117 return name_hint (IDENTIFIER_POINTER (best), NULL);
4118 else
4119 return name_hint (NULL, NULL);
4123 /* Create the predefined scalar types of C,
4124 and some nodes representing standard constants (0, 1, (void *) 0).
4125 Initialize the global scope.
4126 Make definitions for built-in primitive functions. */
4128 void
4129 c_init_decl_processing (void)
4131 location_t save_loc = input_location;
4133 /* Initialize reserved words for parser. */
4134 c_parse_init ();
4136 current_function_decl = NULL_TREE;
4138 gcc_obstack_init (&parser_obstack);
4140 /* Make the externals scope. */
4141 push_scope ();
4142 external_scope = current_scope;
4144 /* Declarations from c_common_nodes_and_builtins must not be associated
4145 with this input file, lest we get differences between using and not
4146 using preprocessed headers. */
4147 input_location = BUILTINS_LOCATION;
4149 c_common_nodes_and_builtins ();
4151 /* In C, comparisons and TRUTH_* expressions have type int. */
4152 truthvalue_type_node = integer_type_node;
4153 truthvalue_true_node = integer_one_node;
4154 truthvalue_false_node = integer_zero_node;
4156 /* Even in C99, which has a real boolean type. */
4157 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4158 boolean_type_node));
4160 input_location = save_loc;
4162 make_fname_decl = c_make_fname_decl;
4163 start_fname_decls ();
4166 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4167 give the decl, NAME is the initialization string and TYPE_DEP
4168 indicates whether NAME depended on the type of the function. As we
4169 don't yet implement delayed emission of static data, we mark the
4170 decl as emitted so it is not placed in the output. Anything using
4171 it must therefore pull out the STRING_CST initializer directly.
4172 FIXME. */
4174 static tree
4175 c_make_fname_decl (location_t loc, tree id, int type_dep)
4177 const char *name = fname_as_string (type_dep);
4178 tree decl, type, init;
4179 size_t length = strlen (name);
4181 type = build_array_type (char_type_node,
4182 build_index_type (size_int (length)));
4183 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4185 decl = build_decl (loc, VAR_DECL, id, type);
4187 TREE_STATIC (decl) = 1;
4188 TREE_READONLY (decl) = 1;
4189 DECL_ARTIFICIAL (decl) = 1;
4191 init = build_string (length + 1, name);
4192 free (CONST_CAST (char *, name));
4193 TREE_TYPE (init) = type;
4194 DECL_INITIAL (decl) = init;
4196 TREE_USED (decl) = 1;
4198 if (current_function_decl
4199 /* For invalid programs like this:
4201 void foo()
4202 const char* p = __FUNCTION__;
4204 the __FUNCTION__ is believed to appear in K&R style function
4205 parameter declarator. In that case we still don't have
4206 function_scope. */
4207 && current_function_scope)
4209 DECL_CONTEXT (decl) = current_function_decl;
4210 bind (id, decl, current_function_scope,
4211 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4214 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4216 return decl;
4219 tree
4220 c_builtin_function (tree decl)
4222 tree type = TREE_TYPE (decl);
4223 tree id = DECL_NAME (decl);
4225 const char *name = IDENTIFIER_POINTER (id);
4226 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4228 /* Should never be called on a symbol with a preexisting meaning. */
4229 gcc_assert (!I_SYMBOL_BINDING (id));
4231 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4232 UNKNOWN_LOCATION);
4234 /* Builtins in the implementation namespace are made visible without
4235 needing to be explicitly declared. See push_file_scope. */
4236 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4238 DECL_CHAIN (decl) = visible_builtins;
4239 visible_builtins = decl;
4242 return decl;
4245 tree
4246 c_builtin_function_ext_scope (tree decl)
4248 tree type = TREE_TYPE (decl);
4249 tree id = DECL_NAME (decl);
4251 const char *name = IDENTIFIER_POINTER (id);
4252 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4254 if (external_scope)
4255 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4256 UNKNOWN_LOCATION);
4258 /* Builtins in the implementation namespace are made visible without
4259 needing to be explicitly declared. See push_file_scope. */
4260 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4262 DECL_CHAIN (decl) = visible_builtins;
4263 visible_builtins = decl;
4266 return decl;
4269 /* Called when a declaration is seen that contains no names to declare.
4270 If its type is a reference to a structure, union or enum inherited
4271 from a containing scope, shadow that tag name for the current scope
4272 with a forward reference.
4273 If its type defines a new named structure or union
4274 or defines an enum, it is valid but we need not do anything here.
4275 Otherwise, it is an error. */
4277 void
4278 shadow_tag (const struct c_declspecs *declspecs)
4280 shadow_tag_warned (declspecs, 0);
4283 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4284 but no pedwarn. */
4285 void
4286 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4288 bool found_tag = false;
4290 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4292 tree value = declspecs->type;
4293 enum tree_code code = TREE_CODE (value);
4295 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4296 /* Used to test also that TYPE_SIZE (value) != 0.
4297 That caused warning for `struct foo;' at top level in the file. */
4299 tree name = TYPE_NAME (value);
4300 tree t;
4302 found_tag = true;
4304 if (declspecs->restrict_p)
4306 error ("invalid use of %<restrict%>");
4307 warned = 1;
4310 if (name == NULL_TREE)
4312 if (warned != 1 && code != ENUMERAL_TYPE)
4313 /* Empty unnamed enum OK */
4315 pedwarn (input_location, 0,
4316 "unnamed struct/union that defines no instances");
4317 warned = 1;
4320 else if (declspecs->typespec_kind != ctsk_tagdef
4321 && declspecs->typespec_kind != ctsk_tagfirstref
4322 && declspecs->storage_class != csc_none)
4324 if (warned != 1)
4325 pedwarn (input_location, 0,
4326 "empty declaration with storage class specifier "
4327 "does not redeclare tag");
4328 warned = 1;
4329 pending_xref_error ();
4331 else if (declspecs->typespec_kind != ctsk_tagdef
4332 && declspecs->typespec_kind != ctsk_tagfirstref
4333 && (declspecs->const_p
4334 || declspecs->volatile_p
4335 || declspecs->atomic_p
4336 || declspecs->restrict_p
4337 || declspecs->address_space))
4339 if (warned != 1)
4340 pedwarn (input_location, 0,
4341 "empty declaration with type qualifier "
4342 "does not redeclare tag");
4343 warned = 1;
4344 pending_xref_error ();
4346 else if (declspecs->typespec_kind != ctsk_tagdef
4347 && declspecs->typespec_kind != ctsk_tagfirstref
4348 && declspecs->alignas_p)
4350 if (warned != 1)
4351 pedwarn (input_location, 0,
4352 "empty declaration with %<_Alignas%> "
4353 "does not redeclare tag");
4354 warned = 1;
4355 pending_xref_error ();
4357 else
4359 pending_invalid_xref = NULL_TREE;
4360 t = lookup_tag (code, name, true, NULL);
4362 if (t == NULL_TREE)
4364 t = make_node (code);
4365 pushtag (input_location, name, t);
4369 else
4371 if (warned != 1 && !in_system_header_at (input_location))
4373 pedwarn (input_location, 0,
4374 "useless type name in empty declaration");
4375 warned = 1;
4379 else if (warned != 1 && !in_system_header_at (input_location)
4380 && declspecs->typedef_p)
4382 pedwarn (input_location, 0, "useless type name in empty declaration");
4383 warned = 1;
4386 pending_invalid_xref = NULL_TREE;
4388 if (declspecs->inline_p)
4390 error ("%<inline%> in empty declaration");
4391 warned = 1;
4394 if (declspecs->noreturn_p)
4396 error ("%<_Noreturn%> in empty declaration");
4397 warned = 1;
4400 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4402 error ("%<auto%> in file-scope empty declaration");
4403 warned = 1;
4406 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4408 error ("%<register%> in file-scope empty declaration");
4409 warned = 1;
4412 if (!warned && !in_system_header_at (input_location)
4413 && declspecs->storage_class != csc_none)
4415 warning (0, "useless storage class specifier in empty declaration");
4416 warned = 2;
4419 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4421 warning (0, "useless %qs in empty declaration",
4422 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4423 warned = 2;
4426 if (!warned
4427 && !in_system_header_at (input_location)
4428 && (declspecs->const_p
4429 || declspecs->volatile_p
4430 || declspecs->atomic_p
4431 || declspecs->restrict_p
4432 || declspecs->address_space))
4434 warning (0, "useless type qualifier in empty declaration");
4435 warned = 2;
4438 if (!warned && !in_system_header_at (input_location)
4439 && declspecs->alignas_p)
4441 warning (0, "useless %<_Alignas%> in empty declaration");
4442 warned = 2;
4445 if (warned != 1)
4447 if (!found_tag)
4448 pedwarn (input_location, 0, "empty declaration");
4453 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4454 bits. SPECS represents declaration specifiers that the grammar
4455 only permits to contain type qualifiers and attributes. */
4458 quals_from_declspecs (const struct c_declspecs *specs)
4460 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4461 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4462 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4463 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4464 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4465 gcc_assert (!specs->type
4466 && !specs->decl_attr
4467 && specs->typespec_word == cts_none
4468 && specs->storage_class == csc_none
4469 && !specs->typedef_p
4470 && !specs->explicit_signed_p
4471 && !specs->deprecated_p
4472 && !specs->long_p
4473 && !specs->long_long_p
4474 && !specs->short_p
4475 && !specs->signed_p
4476 && !specs->unsigned_p
4477 && !specs->complex_p
4478 && !specs->inline_p
4479 && !specs->noreturn_p
4480 && !specs->thread_p);
4481 return quals;
4484 /* Construct an array declarator. LOC is the location of the
4485 beginning of the array (usually the opening brace). EXPR is the
4486 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4487 inside the [] (to be applied to the pointer to which a parameter
4488 array is converted). STATIC_P is true if "static" is inside the
4489 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4490 VLA of unspecified length which is nevertheless a complete type,
4491 false otherwise. The field for the contained declarator is left to
4492 be filled in by set_array_declarator_inner. */
4494 struct c_declarator *
4495 build_array_declarator (location_t loc,
4496 tree expr, struct c_declspecs *quals, bool static_p,
4497 bool vla_unspec_p)
4499 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4500 struct c_declarator);
4501 declarator->id_loc = loc;
4502 declarator->kind = cdk_array;
4503 declarator->declarator = 0;
4504 declarator->u.array.dimen = expr;
4505 if (quals)
4507 declarator->u.array.attrs = quals->attrs;
4508 declarator->u.array.quals = quals_from_declspecs (quals);
4510 else
4512 declarator->u.array.attrs = NULL_TREE;
4513 declarator->u.array.quals = 0;
4515 declarator->u.array.static_p = static_p;
4516 declarator->u.array.vla_unspec_p = vla_unspec_p;
4517 if (static_p || quals != NULL)
4518 pedwarn_c90 (loc, OPT_Wpedantic,
4519 "ISO C90 does not support %<static%> or type "
4520 "qualifiers in parameter array declarators");
4521 if (vla_unspec_p)
4522 pedwarn_c90 (loc, OPT_Wpedantic,
4523 "ISO C90 does not support %<[*]%> array declarators");
4524 if (vla_unspec_p)
4526 if (!current_scope->parm_flag)
4528 /* C99 6.7.5.2p4 */
4529 error_at (loc, "%<[*]%> not allowed in other than "
4530 "function prototype scope");
4531 declarator->u.array.vla_unspec_p = false;
4532 return NULL;
4534 current_scope->had_vla_unspec = true;
4536 return declarator;
4539 /* Set the contained declarator of an array declarator. DECL is the
4540 declarator, as constructed by build_array_declarator; INNER is what
4541 appears on the left of the []. */
4543 struct c_declarator *
4544 set_array_declarator_inner (struct c_declarator *decl,
4545 struct c_declarator *inner)
4547 decl->declarator = inner;
4548 return decl;
4551 /* INIT is a constructor that forms DECL's initializer. If the final
4552 element initializes a flexible array field, add the size of that
4553 initializer to DECL's size. */
4555 static void
4556 add_flexible_array_elts_to_size (tree decl, tree init)
4558 tree elt, type;
4560 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4561 return;
4563 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4564 type = TREE_TYPE (elt);
4565 if (TREE_CODE (type) == ARRAY_TYPE
4566 && TYPE_SIZE (type) == NULL_TREE
4567 && TYPE_DOMAIN (type) != NULL_TREE
4568 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4570 complete_array_type (&type, elt, false);
4571 DECL_SIZE (decl)
4572 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4573 DECL_SIZE_UNIT (decl)
4574 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4578 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4579 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4580 before the type name, and set *EXPR_CONST_OPERANDS, if
4581 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4582 appear in a constant expression. */
4584 tree
4585 groktypename (struct c_type_name *type_name, tree *expr,
4586 bool *expr_const_operands)
4588 tree type;
4589 tree attrs = type_name->specs->attrs;
4591 type_name->specs->attrs = NULL_TREE;
4593 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4594 false, NULL, &attrs, expr, expr_const_operands,
4595 DEPRECATED_NORMAL);
4597 /* Apply attributes. */
4598 decl_attributes (&type, attrs, 0);
4600 return type;
4603 /* Wrapper for decl_attributes that adds some implicit attributes
4604 to VAR_DECLs or FUNCTION_DECLs. */
4606 static tree
4607 c_decl_attributes (tree *node, tree attributes, int flags)
4609 /* Add implicit "omp declare target" attribute if requested. */
4610 if (current_omp_declare_target_attribute
4611 && ((VAR_P (*node) && is_global_var (*node))
4612 || TREE_CODE (*node) == FUNCTION_DECL))
4614 if (VAR_P (*node)
4615 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4616 error ("%q+D in declare target directive does not have mappable type",
4617 *node);
4618 else
4619 attributes = tree_cons (get_identifier ("omp declare target"),
4620 NULL_TREE, attributes);
4622 return decl_attributes (node, attributes, flags);
4626 /* Decode a declarator in an ordinary declaration or data definition.
4627 This is called as soon as the type information and variable name
4628 have been parsed, before parsing the initializer if any.
4629 Here we create the ..._DECL node, fill in its type,
4630 and put it on the list of decls for the current context.
4631 The ..._DECL node is returned as the value.
4633 Exception: for arrays where the length is not specified,
4634 the type is left null, to be filled in by `finish_decl'.
4636 Function definitions do not come here; they go to start_function
4637 instead. However, external and forward declarations of functions
4638 do go through here. Structure field declarations are done by
4639 grokfield and not through here. */
4641 tree
4642 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4643 bool initialized, tree attributes)
4645 tree decl;
4646 tree tem;
4647 tree expr = NULL_TREE;
4648 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4650 /* An object declared as __attribute__((deprecated)) suppresses
4651 warnings of uses of other deprecated items. */
4652 if (lookup_attribute ("deprecated", attributes))
4653 deprecated_state = DEPRECATED_SUPPRESS;
4655 decl = grokdeclarator (declarator, declspecs,
4656 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4657 deprecated_state);
4658 if (!decl || decl == error_mark_node)
4659 return NULL_TREE;
4661 if (expr)
4662 add_stmt (fold_convert (void_type_node, expr));
4664 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4665 warning (OPT_Wmain, "%q+D is usually a function", decl);
4667 if (initialized)
4668 /* Is it valid for this decl to have an initializer at all?
4669 If not, set INITIALIZED to zero, which will indirectly
4670 tell 'finish_decl' to ignore the initializer once it is parsed. */
4671 switch (TREE_CODE (decl))
4673 case TYPE_DECL:
4674 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4675 initialized = false;
4676 break;
4678 case FUNCTION_DECL:
4679 error ("function %qD is initialized like a variable", decl);
4680 initialized = false;
4681 break;
4683 case PARM_DECL:
4684 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4685 error ("parameter %qD is initialized", decl);
4686 initialized = false;
4687 break;
4689 default:
4690 /* Don't allow initializations for incomplete types except for
4691 arrays which might be completed by the initialization. */
4693 /* This can happen if the array size is an undefined macro.
4694 We already gave a warning, so we don't need another one. */
4695 if (TREE_TYPE (decl) == error_mark_node)
4696 initialized = false;
4697 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4699 /* A complete type is ok if size is fixed. */
4701 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4702 || C_DECL_VARIABLE_SIZE (decl))
4704 error ("variable-sized object may not be initialized");
4705 initialized = false;
4708 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4710 error ("variable %qD has initializer but incomplete type", decl);
4711 initialized = false;
4713 else if (C_DECL_VARIABLE_SIZE (decl))
4715 /* Although C99 is unclear about whether incomplete arrays
4716 of VLAs themselves count as VLAs, it does not make
4717 sense to permit them to be initialized given that
4718 ordinary VLAs may not be initialized. */
4719 error ("variable-sized object may not be initialized");
4720 initialized = false;
4724 if (initialized)
4726 if (current_scope == file_scope)
4727 TREE_STATIC (decl) = 1;
4729 /* Tell 'pushdecl' this is an initialized decl
4730 even though we don't yet have the initializer expression.
4731 Also tell 'finish_decl' it may store the real initializer. */
4732 DECL_INITIAL (decl) = error_mark_node;
4735 /* If this is a function declaration, write a record describing it to the
4736 prototypes file (if requested). */
4738 if (TREE_CODE (decl) == FUNCTION_DECL)
4739 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4741 /* ANSI specifies that a tentative definition which is not merged with
4742 a non-tentative definition behaves exactly like a definition with an
4743 initializer equal to zero. (Section 3.7.2)
4745 -fno-common gives strict ANSI behavior, though this tends to break
4746 a large body of code that grew up without this rule.
4748 Thread-local variables are never common, since there's no entrenched
4749 body of code to break, and it allows more efficient variable references
4750 in the presence of dynamic linking. */
4752 if (VAR_P (decl)
4753 && !initialized
4754 && TREE_PUBLIC (decl)
4755 && !DECL_THREAD_LOCAL_P (decl)
4756 && !flag_no_common)
4757 DECL_COMMON (decl) = 1;
4759 /* Set attributes here so if duplicate decl, will have proper attributes. */
4760 c_decl_attributes (&decl, attributes, 0);
4762 /* Handle gnu_inline attribute. */
4763 if (declspecs->inline_p
4764 && !flag_gnu89_inline
4765 && TREE_CODE (decl) == FUNCTION_DECL
4766 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4767 || current_function_decl))
4769 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4771 else if (declspecs->storage_class != csc_static)
4772 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4775 if (TREE_CODE (decl) == FUNCTION_DECL
4776 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4778 struct c_declarator *ce = declarator;
4780 if (ce->kind == cdk_pointer)
4781 ce = declarator->declarator;
4782 if (ce->kind == cdk_function)
4784 tree args = ce->u.arg_info->parms;
4785 for (; args; args = DECL_CHAIN (args))
4787 tree type = TREE_TYPE (args);
4788 if (type && INTEGRAL_TYPE_P (type)
4789 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4790 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4795 if (TREE_CODE (decl) == FUNCTION_DECL
4796 && DECL_DECLARED_INLINE_P (decl)
4797 && DECL_UNINLINABLE (decl)
4798 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4799 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4800 decl);
4802 /* C99 6.7.4p3: An inline definition of a function with external
4803 linkage shall not contain a definition of a modifiable object
4804 with static storage duration... */
4805 if (VAR_P (decl)
4806 && current_scope != file_scope
4807 && TREE_STATIC (decl)
4808 && !TREE_READONLY (decl)
4809 && DECL_DECLARED_INLINE_P (current_function_decl)
4810 && DECL_EXTERNAL (current_function_decl))
4811 record_inline_static (input_location, current_function_decl,
4812 decl, csi_modifiable);
4814 if (c_dialect_objc ()
4815 && VAR_OR_FUNCTION_DECL_P (decl))
4816 objc_check_global_decl (decl);
4818 /* Add this decl to the current scope.
4819 TEM may equal DECL or it may be a previous decl of the same name. */
4820 tem = pushdecl (decl);
4822 if (initialized && DECL_EXTERNAL (tem))
4824 DECL_EXTERNAL (tem) = 0;
4825 TREE_STATIC (tem) = 1;
4828 return tem;
4831 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4832 DECL or the non-array element type if DECL is an uninitialized array.
4833 If that type has a const member, diagnose this. */
4835 static void
4836 diagnose_uninitialized_cst_member (tree decl, tree type)
4838 tree field;
4839 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4841 tree field_type;
4842 if (TREE_CODE (field) != FIELD_DECL)
4843 continue;
4844 field_type = strip_array_types (TREE_TYPE (field));
4846 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4848 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4849 "uninitialized const member in %qT is invalid in C++",
4850 strip_array_types (TREE_TYPE (decl)));
4851 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4854 if (RECORD_OR_UNION_TYPE_P (field_type))
4855 diagnose_uninitialized_cst_member (decl, field_type);
4859 /* Finish processing of a declaration;
4860 install its initial value.
4861 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4862 If the length of an array type is not known before,
4863 it must be determined now, from the initial value, or it is an error.
4865 INIT_LOC is the location of the initial value. */
4867 void
4868 finish_decl (tree decl, location_t init_loc, tree init,
4869 tree origtype, tree asmspec_tree)
4871 tree type;
4872 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4873 const char *asmspec = 0;
4875 /* If a name was specified, get the string. */
4876 if (VAR_OR_FUNCTION_DECL_P (decl)
4877 && DECL_FILE_SCOPE_P (decl))
4878 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4879 if (asmspec_tree)
4880 asmspec = TREE_STRING_POINTER (asmspec_tree);
4882 if (VAR_P (decl)
4883 && TREE_STATIC (decl)
4884 && global_bindings_p ())
4885 /* So decl is a global variable. Record the types it uses
4886 so that we can decide later to emit debug info for them. */
4887 record_types_used_by_current_var_decl (decl);
4889 /* If `start_decl' didn't like having an initialization, ignore it now. */
4890 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4891 init = NULL_TREE;
4893 /* Don't crash if parm is initialized. */
4894 if (TREE_CODE (decl) == PARM_DECL)
4895 init = NULL_TREE;
4897 if (init)
4898 store_init_value (init_loc, decl, init, origtype);
4900 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4901 || TREE_CODE (decl) == FIELD_DECL))
4902 objc_check_decl (decl);
4904 type = TREE_TYPE (decl);
4906 /* Deduce size of array from initialization, if not already known. */
4907 if (TREE_CODE (type) == ARRAY_TYPE
4908 && TYPE_DOMAIN (type) == NULL_TREE
4909 && TREE_CODE (decl) != TYPE_DECL)
4911 bool do_default
4912 = (TREE_STATIC (decl)
4913 /* Even if pedantic, an external linkage array
4914 may have incomplete type at first. */
4915 ? pedantic && !TREE_PUBLIC (decl)
4916 : !DECL_EXTERNAL (decl));
4917 int failure
4918 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4919 do_default);
4921 /* Get the completed type made by complete_array_type. */
4922 type = TREE_TYPE (decl);
4924 switch (failure)
4926 case 1:
4927 error ("initializer fails to determine size of %q+D", decl);
4928 break;
4930 case 2:
4931 if (do_default)
4932 error ("array size missing in %q+D", decl);
4933 /* If a `static' var's size isn't known,
4934 make it extern as well as static, so it does not get
4935 allocated.
4936 If it is not `static', then do not mark extern;
4937 finish_incomplete_decl will give it a default size
4938 and it will get allocated. */
4939 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4940 DECL_EXTERNAL (decl) = 1;
4941 break;
4943 case 3:
4944 error ("zero or negative size array %q+D", decl);
4945 break;
4947 case 0:
4948 /* For global variables, update the copy of the type that
4949 exists in the binding. */
4950 if (TREE_PUBLIC (decl))
4952 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4953 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4954 b_ext = b_ext->shadowed;
4955 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4957 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4958 b_ext->u.type = composite_type (b_ext->u.type, type);
4959 else
4960 b_ext->u.type = type;
4963 break;
4965 default:
4966 gcc_unreachable ();
4969 if (DECL_INITIAL (decl))
4970 TREE_TYPE (DECL_INITIAL (decl)) = type;
4972 relayout_decl (decl);
4975 if (VAR_P (decl))
4977 if (init && TREE_CODE (init) == CONSTRUCTOR)
4978 add_flexible_array_elts_to_size (decl, init);
4980 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
4981 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4982 layout_decl (decl, 0);
4984 if (DECL_SIZE (decl) == NULL_TREE
4985 /* Don't give an error if we already gave one earlier. */
4986 && TREE_TYPE (decl) != error_mark_node
4987 && (TREE_STATIC (decl)
4988 /* A static variable with an incomplete type
4989 is an error if it is initialized.
4990 Also if it is not file scope.
4991 Otherwise, let it through, but if it is not `extern'
4992 then it may cause an error message later. */
4993 ? (DECL_INITIAL (decl) != NULL_TREE
4994 || !DECL_FILE_SCOPE_P (decl))
4995 /* An automatic variable with an incomplete type
4996 is an error. */
4997 : !DECL_EXTERNAL (decl)))
4999 error ("storage size of %q+D isn%'t known", decl);
5000 TREE_TYPE (decl) = error_mark_node;
5003 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5004 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5005 && DECL_SIZE (decl) == NULL_TREE
5006 && TREE_STATIC (decl))
5007 incomplete_record_decls.safe_push (decl);
5009 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5011 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5012 constant_expression_warning (DECL_SIZE (decl));
5013 else
5015 error ("storage size of %q+D isn%'t constant", decl);
5016 TREE_TYPE (decl) = error_mark_node;
5020 if (TREE_USED (type))
5022 TREE_USED (decl) = 1;
5023 DECL_READ_P (decl) = 1;
5027 /* If this is a function and an assembler name is specified, reset DECL_RTL
5028 so we can give it its new name. Also, update builtin_decl if it
5029 was a normal built-in. */
5030 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5032 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5033 set_builtin_user_assembler_name (decl, asmspec);
5034 set_user_assembler_name (decl, asmspec);
5037 /* If #pragma weak was used, mark the decl weak now. */
5038 maybe_apply_pragma_weak (decl);
5040 /* Output the assembler code and/or RTL code for variables and functions,
5041 unless the type is an undefined structure or union.
5042 If not, it will get done when the type is completed. */
5044 if (VAR_OR_FUNCTION_DECL_P (decl))
5046 /* Determine the ELF visibility. */
5047 if (TREE_PUBLIC (decl))
5048 c_determine_visibility (decl);
5050 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5051 if (c_dialect_objc ())
5052 objc_check_decl (decl);
5054 if (asmspec)
5056 /* If this is not a static variable, issue a warning.
5057 It doesn't make any sense to give an ASMSPEC for an
5058 ordinary, non-register local variable. Historically,
5059 GCC has accepted -- but ignored -- the ASMSPEC in
5060 this case. */
5061 if (!DECL_FILE_SCOPE_P (decl)
5062 && VAR_P (decl)
5063 && !C_DECL_REGISTER (decl)
5064 && !TREE_STATIC (decl))
5065 warning (0, "ignoring asm-specifier for non-static local "
5066 "variable %q+D", decl);
5067 else
5068 set_user_assembler_name (decl, asmspec);
5071 if (DECL_FILE_SCOPE_P (decl))
5073 if (DECL_INITIAL (decl) == NULL_TREE
5074 || DECL_INITIAL (decl) == error_mark_node)
5075 /* Don't output anything
5076 when a tentative file-scope definition is seen.
5077 But at end of compilation, do output code for them. */
5078 DECL_DEFER_OUTPUT (decl) = 1;
5079 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5080 DECL_HARD_REGISTER (decl) = 1;
5081 rest_of_decl_compilation (decl, true, 0);
5083 else
5085 /* In conjunction with an ASMSPEC, the `register'
5086 keyword indicates that we should place the variable
5087 in a particular register. */
5088 if (asmspec && C_DECL_REGISTER (decl))
5090 DECL_HARD_REGISTER (decl) = 1;
5091 /* This cannot be done for a structure with volatile
5092 fields, on which DECL_REGISTER will have been
5093 reset. */
5094 if (!DECL_REGISTER (decl))
5095 error ("cannot put object with volatile field into register");
5098 if (TREE_CODE (decl) != FUNCTION_DECL)
5100 /* If we're building a variable sized type, and we might be
5101 reachable other than via the top of the current binding
5102 level, then create a new BIND_EXPR so that we deallocate
5103 the object at the right time. */
5104 /* Note that DECL_SIZE can be null due to errors. */
5105 if (DECL_SIZE (decl)
5106 && !TREE_CONSTANT (DECL_SIZE (decl))
5107 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5109 tree bind;
5110 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5111 TREE_SIDE_EFFECTS (bind) = 1;
5112 add_stmt (bind);
5113 BIND_EXPR_BODY (bind) = push_stmt_list ();
5115 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5116 DECL_EXPR, decl));
5121 if (!DECL_FILE_SCOPE_P (decl))
5123 /* Recompute the RTL of a local array now
5124 if it used to be an incomplete type. */
5125 if (was_incomplete && !is_global_var (decl))
5127 /* If we used it already as memory, it must stay in memory. */
5128 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5129 /* If it's still incomplete now, no init will save it. */
5130 if (DECL_SIZE (decl) == NULL_TREE)
5131 DECL_INITIAL (decl) = NULL_TREE;
5136 if (TREE_CODE (decl) == TYPE_DECL)
5138 if (!DECL_FILE_SCOPE_P (decl)
5139 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5140 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5142 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5145 /* Install a cleanup (aka destructor) if one was given. */
5146 if (VAR_P (decl) && !TREE_STATIC (decl))
5148 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5149 if (attr)
5151 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5152 tree cleanup_decl = lookup_name (cleanup_id);
5153 tree cleanup;
5154 vec<tree, va_gc> *v;
5156 /* Build "cleanup(&decl)" for the destructor. */
5157 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5158 vec_alloc (v, 1);
5159 v->quick_push (cleanup);
5160 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5161 vNULL, cleanup_decl, v, NULL);
5162 vec_free (v);
5164 /* Don't warn about decl unused; the cleanup uses it. */
5165 TREE_USED (decl) = 1;
5166 TREE_USED (cleanup_decl) = 1;
5167 DECL_READ_P (decl) = 1;
5169 push_cleanup (decl, cleanup, false);
5173 if (warn_cxx_compat
5174 && VAR_P (decl)
5175 && !DECL_EXTERNAL (decl)
5176 && DECL_INITIAL (decl) == NULL_TREE)
5178 type = strip_array_types (type);
5179 if (TREE_READONLY (decl))
5180 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5181 "uninitialized const %qD is invalid in C++", decl);
5182 else if (RECORD_OR_UNION_TYPE_P (type)
5183 && C_TYPE_FIELDS_READONLY (type))
5184 diagnose_uninitialized_cst_member (decl, type);
5187 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5190 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5191 EXPR is NULL or a pointer to an expression that needs to be
5192 evaluated for the side effects of array size expressions in the
5193 parameters. */
5195 tree
5196 grokparm (const struct c_parm *parm, tree *expr)
5198 tree attrs = parm->attrs;
5199 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5200 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5202 decl_attributes (&decl, attrs, 0);
5204 return decl;
5207 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5208 and push that on the current scope. EXPR is a pointer to an
5209 expression that needs to be evaluated for the side effects of array
5210 size expressions in the parameters. */
5212 void
5213 push_parm_decl (const struct c_parm *parm, tree *expr)
5215 tree attrs = parm->attrs;
5216 tree decl;
5218 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5219 &attrs, expr, NULL, DEPRECATED_NORMAL);
5220 if (decl && DECL_P (decl))
5221 DECL_SOURCE_LOCATION (decl) = parm->loc;
5222 decl_attributes (&decl, attrs, 0);
5224 decl = pushdecl (decl);
5226 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5229 /* Mark all the parameter declarations to date as forward decls.
5230 Also diagnose use of this extension. */
5232 void
5233 mark_forward_parm_decls (void)
5235 struct c_binding *b;
5237 if (pedantic && !current_scope->warned_forward_parm_decls)
5239 pedwarn (input_location, OPT_Wpedantic,
5240 "ISO C forbids forward parameter declarations");
5241 current_scope->warned_forward_parm_decls = true;
5244 for (b = current_scope->bindings; b; b = b->prev)
5245 if (TREE_CODE (b->decl) == PARM_DECL)
5246 TREE_ASM_WRITTEN (b->decl) = 1;
5249 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5250 literal, which may be an incomplete array type completed by the
5251 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5252 literal. NON_CONST is true if the initializers contain something
5253 that cannot occur in a constant expression. */
5255 tree
5256 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5258 /* We do not use start_decl here because we have a type, not a declarator;
5259 and do not use finish_decl because the decl should be stored inside
5260 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5261 tree decl;
5262 tree complit;
5263 tree stmt;
5265 if (type == error_mark_node
5266 || init == error_mark_node)
5267 return error_mark_node;
5269 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5270 DECL_EXTERNAL (decl) = 0;
5271 TREE_PUBLIC (decl) = 0;
5272 TREE_STATIC (decl) = (current_scope == file_scope);
5273 DECL_CONTEXT (decl) = current_function_decl;
5274 TREE_USED (decl) = 1;
5275 DECL_READ_P (decl) = 1;
5276 DECL_ARTIFICIAL (decl) = 1;
5277 DECL_IGNORED_P (decl) = 1;
5278 TREE_TYPE (decl) = type;
5279 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5280 store_init_value (loc, decl, init, NULL_TREE);
5282 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5284 int failure = complete_array_type (&TREE_TYPE (decl),
5285 DECL_INITIAL (decl), true);
5286 /* If complete_array_type returns 3, it means that the
5287 initial value of the compound literal is empty. Allow it. */
5288 gcc_assert (failure == 0 || failure == 3);
5290 type = TREE_TYPE (decl);
5291 TREE_TYPE (DECL_INITIAL (decl)) = type;
5294 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5296 c_incomplete_type_error (loc, NULL_TREE, type);
5297 return error_mark_node;
5300 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5301 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5302 TREE_SIDE_EFFECTS (complit) = 1;
5304 layout_decl (decl, 0);
5306 if (TREE_STATIC (decl))
5308 /* This decl needs a name for the assembler output. */
5309 set_compound_literal_name (decl);
5310 DECL_DEFER_OUTPUT (decl) = 1;
5311 DECL_COMDAT (decl) = 1;
5312 pushdecl (decl);
5313 rest_of_decl_compilation (decl, 1, 0);
5316 if (non_const)
5318 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5319 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5322 return complit;
5325 /* Check the type of a compound literal. Here we just check that it
5326 is valid for C++. */
5328 void
5329 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5331 if (warn_cxx_compat
5332 && (type_name->specs->typespec_kind == ctsk_tagdef
5333 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5334 warning_at (loc, OPT_Wc___compat,
5335 "defining a type in a compound literal is invalid in C++");
5338 /* Determine whether TYPE is a structure with a flexible array member,
5339 or a union containing such a structure (possibly recursively). */
5341 static bool
5342 flexible_array_type_p (tree type)
5344 tree x;
5345 switch (TREE_CODE (type))
5347 case RECORD_TYPE:
5348 x = TYPE_FIELDS (type);
5349 if (x == NULL_TREE)
5350 return false;
5351 while (DECL_CHAIN (x) != NULL_TREE)
5352 x = DECL_CHAIN (x);
5353 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5354 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5355 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5356 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5357 return true;
5358 return false;
5359 case UNION_TYPE:
5360 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5362 if (flexible_array_type_p (TREE_TYPE (x)))
5363 return true;
5365 return false;
5366 default:
5367 return false;
5371 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5372 replacing with appropriate values if they are invalid. */
5374 static void
5375 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5376 tree orig_name)
5378 tree type_mv;
5379 unsigned int max_width;
5380 unsigned HOST_WIDE_INT w;
5381 const char *name = (orig_name
5382 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5383 : _("<anonymous>"));
5385 /* Detect and ignore out of range field width and process valid
5386 field widths. */
5387 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5389 error_at (loc, "bit-field %qs width not an integer constant", name);
5390 *width = integer_one_node;
5392 else
5394 if (TREE_CODE (*width) != INTEGER_CST)
5396 *width = c_fully_fold (*width, false, NULL);
5397 if (TREE_CODE (*width) == INTEGER_CST)
5398 pedwarn (loc, OPT_Wpedantic,
5399 "bit-field %qs width not an integer constant expression",
5400 name);
5402 if (TREE_CODE (*width) != INTEGER_CST)
5404 error_at (loc, "bit-field %qs width not an integer constant", name);
5405 *width = integer_one_node;
5407 constant_expression_warning (*width);
5408 if (tree_int_cst_sgn (*width) < 0)
5410 error_at (loc, "negative width in bit-field %qs", name);
5411 *width = integer_one_node;
5413 else if (integer_zerop (*width) && orig_name)
5415 error_at (loc, "zero width for bit-field %qs", name);
5416 *width = integer_one_node;
5420 /* Detect invalid bit-field type. */
5421 if (TREE_CODE (*type) != INTEGER_TYPE
5422 && TREE_CODE (*type) != BOOLEAN_TYPE
5423 && TREE_CODE (*type) != ENUMERAL_TYPE)
5425 error_at (loc, "bit-field %qs has invalid type", name);
5426 *type = unsigned_type_node;
5429 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5431 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5432 name);
5433 *type = unsigned_type_node;
5436 type_mv = TYPE_MAIN_VARIANT (*type);
5437 if (!in_system_header_at (input_location)
5438 && type_mv != integer_type_node
5439 && type_mv != unsigned_type_node
5440 && type_mv != boolean_type_node)
5441 pedwarn_c90 (loc, OPT_Wpedantic,
5442 "type of bit-field %qs is a GCC extension", name);
5444 max_width = TYPE_PRECISION (*type);
5446 if (0 < compare_tree_int (*width, max_width))
5448 error_at (loc, "width of %qs exceeds its type", name);
5449 w = max_width;
5450 *width = build_int_cst (integer_type_node, w);
5452 else
5453 w = tree_to_uhwi (*width);
5455 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5457 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5458 if (!lt
5459 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5460 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5461 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5467 /* Print warning about variable length array if necessary. */
5469 static void
5470 warn_variable_length_array (tree name, tree size)
5472 if (TREE_CONSTANT (size))
5474 if (name)
5475 pedwarn_c90 (input_location, OPT_Wvla,
5476 "ISO C90 forbids array %qE whose size "
5477 "can%'t be evaluated", name);
5478 else
5479 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5480 "whose size can%'t be evaluated");
5482 else
5484 if (name)
5485 pedwarn_c90 (input_location, OPT_Wvla,
5486 "ISO C90 forbids variable length array %qE", name);
5487 else
5488 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5489 "length array");
5493 /* Print warning about defaulting to int if necessary. */
5495 static void
5496 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5498 diagnostic_info diagnostic;
5499 va_list ap;
5500 rich_location richloc (line_table, location);
5502 va_start (ap, gmsgid);
5503 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5504 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5505 diagnostic.option_index = opt;
5506 diagnostic_report_diagnostic (global_dc, &diagnostic);
5507 va_end (ap);
5510 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5511 considering only those c_declspec_words found in LIST, which
5512 must be terminated by cdw_number_of_elements. */
5514 static location_t
5515 smallest_type_quals_location (const location_t *locations,
5516 const c_declspec_word *list)
5518 location_t loc = UNKNOWN_LOCATION;
5519 while (*list != cdw_number_of_elements)
5521 location_t newloc = locations[*list];
5522 if (loc == UNKNOWN_LOCATION
5523 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5524 loc = newloc;
5525 list++;
5528 return loc;
5531 /* Given declspecs and a declarator,
5532 determine the name and type of the object declared
5533 and construct a ..._DECL node for it.
5534 (In one case we can return a ..._TYPE node instead.
5535 For invalid input we sometimes return NULL_TREE.)
5537 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5539 DECL_CONTEXT says which syntactic context this declaration is in:
5540 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5541 FUNCDEF for a function definition. Like NORMAL but a few different
5542 error messages in each case. Return value may be zero meaning
5543 this definition is too screwy to try to parse.
5544 PARM for a parameter declaration (either within a function prototype
5545 or before a function body). Make a PARM_DECL, or return void_type_node.
5546 TYPENAME if for a typename (in a cast or sizeof).
5547 Don't make a DECL node; just return the ..._TYPE node.
5548 FIELD for a struct or union field; make a FIELD_DECL.
5549 INITIALIZED is true if the decl has an initializer.
5550 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5551 representing the width of the bit-field.
5552 DECL_ATTRS points to the list of attributes that should be added to this
5553 decl. Any nested attributes that belong on the decl itself will be
5554 added to this list.
5555 If EXPR is not NULL, any expressions that need to be evaluated as
5556 part of evaluating variably modified types will be stored in *EXPR.
5557 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5558 set to indicate whether operands in *EXPR can be used in constant
5559 expressions.
5560 DEPRECATED_STATE is a deprecated_states value indicating whether
5561 deprecation warnings should be suppressed.
5563 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5564 It may also be so in the PARM case, for a prototype where the
5565 argument type is specified but not the name.
5567 This function is where the complicated C meanings of `static'
5568 and `extern' are interpreted. */
5570 static tree
5571 grokdeclarator (const struct c_declarator *declarator,
5572 struct c_declspecs *declspecs,
5573 enum decl_context decl_context, bool initialized, tree *width,
5574 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5575 enum deprecated_states deprecated_state)
5577 tree type = declspecs->type;
5578 bool threadp = declspecs->thread_p;
5579 enum c_storage_class storage_class = declspecs->storage_class;
5580 int constp;
5581 int restrictp;
5582 int volatilep;
5583 int atomicp;
5584 int type_quals = TYPE_UNQUALIFIED;
5585 tree name = NULL_TREE;
5586 bool funcdef_flag = false;
5587 bool funcdef_syntax = false;
5588 bool size_varies = false;
5589 tree decl_attr = declspecs->decl_attr;
5590 int array_ptr_quals = TYPE_UNQUALIFIED;
5591 tree array_ptr_attrs = NULL_TREE;
5592 bool array_parm_static = false;
5593 bool array_parm_vla_unspec_p = false;
5594 tree returned_attrs = NULL_TREE;
5595 bool bitfield = width != NULL;
5596 tree element_type;
5597 tree orig_qual_type = NULL;
5598 size_t orig_qual_indirect = 0;
5599 struct c_arg_info *arg_info = 0;
5600 addr_space_t as1, as2, address_space;
5601 location_t loc = UNKNOWN_LOCATION;
5602 tree expr_dummy;
5603 bool expr_const_operands_dummy;
5604 enum c_declarator_kind first_non_attr_kind;
5605 unsigned int alignas_align = 0;
5607 if (TREE_CODE (type) == ERROR_MARK)
5608 return error_mark_node;
5609 if (expr == NULL)
5611 expr = &expr_dummy;
5612 expr_dummy = NULL_TREE;
5614 if (expr_const_operands == NULL)
5615 expr_const_operands = &expr_const_operands_dummy;
5617 if (declspecs->expr)
5619 if (*expr)
5620 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5621 declspecs->expr);
5622 else
5623 *expr = declspecs->expr;
5625 *expr_const_operands = declspecs->expr_const_operands;
5627 if (decl_context == FUNCDEF)
5628 funcdef_flag = true, decl_context = NORMAL;
5630 /* Look inside a declarator for the name being declared
5631 and get it as an IDENTIFIER_NODE, for an error message. */
5633 const struct c_declarator *decl = declarator;
5635 first_non_attr_kind = cdk_attrs;
5636 while (decl)
5637 switch (decl->kind)
5639 case cdk_array:
5640 loc = decl->id_loc;
5641 /* FALL THRU. */
5643 case cdk_function:
5644 case cdk_pointer:
5645 funcdef_syntax = (decl->kind == cdk_function);
5646 if (first_non_attr_kind == cdk_attrs)
5647 first_non_attr_kind = decl->kind;
5648 decl = decl->declarator;
5649 break;
5651 case cdk_attrs:
5652 decl = decl->declarator;
5653 break;
5655 case cdk_id:
5656 loc = decl->id_loc;
5657 if (decl->u.id)
5658 name = decl->u.id;
5659 if (first_non_attr_kind == cdk_attrs)
5660 first_non_attr_kind = decl->kind;
5661 decl = 0;
5662 break;
5664 default:
5665 gcc_unreachable ();
5667 if (name == NULL_TREE)
5669 gcc_assert (decl_context == PARM
5670 || decl_context == TYPENAME
5671 || (decl_context == FIELD
5672 && declarator->kind == cdk_id));
5673 gcc_assert (!initialized);
5677 /* A function definition's declarator must have the form of
5678 a function declarator. */
5680 if (funcdef_flag && !funcdef_syntax)
5681 return NULL_TREE;
5683 /* If this looks like a function definition, make it one,
5684 even if it occurs where parms are expected.
5685 Then store_parm_decls will reject it and not use it as a parm. */
5686 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5687 decl_context = PARM;
5689 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5690 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5692 if ((decl_context == NORMAL || decl_context == FIELD)
5693 && current_scope == file_scope
5694 && variably_modified_type_p (type, NULL_TREE))
5696 if (name)
5697 error_at (loc, "variably modified %qE at file scope", name);
5698 else
5699 error_at (loc, "variably modified field at file scope");
5700 type = integer_type_node;
5703 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5705 /* Diagnose defaulting to "int". */
5707 if (declspecs->default_int_p && !in_system_header_at (input_location))
5709 /* Issue a warning if this is an ISO C 99 program or if
5710 -Wreturn-type and this is a function, or if -Wimplicit;
5711 prefer the former warning since it is more explicit. */
5712 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5713 && funcdef_flag)
5714 warn_about_return_type = 1;
5715 else
5717 if (name)
5718 warn_defaults_to (loc, OPT_Wimplicit_int,
5719 "type defaults to %<int%> in declaration "
5720 "of %qE", name);
5721 else
5722 warn_defaults_to (loc, OPT_Wimplicit_int,
5723 "type defaults to %<int%> in type name");
5727 /* Adjust the type if a bit-field is being declared,
5728 -funsigned-bitfields applied and the type is not explicitly
5729 "signed". */
5730 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5731 && TREE_CODE (type) == INTEGER_TYPE)
5732 type = unsigned_type_for (type);
5734 /* Figure out the type qualifiers for the declaration. There are
5735 two ways a declaration can become qualified. One is something
5736 like `const int i' where the `const' is explicit. Another is
5737 something like `typedef const int CI; CI i' where the type of the
5738 declaration contains the `const'. A third possibility is that
5739 there is a type qualifier on the element type of a typedefed
5740 array type, in which case we should extract that qualifier so
5741 that c_apply_type_quals_to_decl receives the full list of
5742 qualifiers to work with (C90 is not entirely clear about whether
5743 duplicate qualifiers should be diagnosed in this case, but it
5744 seems most appropriate to do so). */
5745 element_type = strip_array_types (type);
5746 constp = declspecs->const_p + TYPE_READONLY (element_type);
5747 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5748 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5749 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5750 as1 = declspecs->address_space;
5751 as2 = TYPE_ADDR_SPACE (element_type);
5752 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5754 if (constp > 1)
5755 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5756 if (restrictp > 1)
5757 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5758 if (volatilep > 1)
5759 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5760 if (atomicp > 1)
5761 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5763 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5764 error_at (loc, "conflicting named address spaces (%s vs %s)",
5765 c_addr_space_name (as1), c_addr_space_name (as2));
5767 if ((TREE_CODE (type) == ARRAY_TYPE
5768 || first_non_attr_kind == cdk_array)
5769 && TYPE_QUALS (element_type))
5771 orig_qual_type = type;
5772 type = TYPE_MAIN_VARIANT (type);
5774 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5775 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5776 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5777 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5778 | ENCODE_QUAL_ADDR_SPACE (address_space));
5779 if (type_quals != TYPE_QUALS (element_type))
5780 orig_qual_type = NULL_TREE;
5782 /* Applying the _Atomic qualifier to an array type (through the use
5783 of typedefs or typeof) must be detected here. If the qualifier
5784 is introduced later, any appearance of applying it to an array is
5785 actually applying it to an element of that array. */
5786 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5787 error_at (loc, "%<_Atomic%>-qualified array type");
5789 /* Warn about storage classes that are invalid for certain
5790 kinds of declarations (parameters, typenames, etc.). */
5792 if (funcdef_flag
5793 && (threadp
5794 || storage_class == csc_auto
5795 || storage_class == csc_register
5796 || storage_class == csc_typedef))
5798 if (storage_class == csc_auto)
5799 pedwarn (loc,
5800 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5801 "function definition declared %<auto%>");
5802 if (storage_class == csc_register)
5803 error_at (loc, "function definition declared %<register%>");
5804 if (storage_class == csc_typedef)
5805 error_at (loc, "function definition declared %<typedef%>");
5806 if (threadp)
5807 error_at (loc, "function definition declared %qs",
5808 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5809 threadp = false;
5810 if (storage_class == csc_auto
5811 || storage_class == csc_register
5812 || storage_class == csc_typedef)
5813 storage_class = csc_none;
5815 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5817 if (decl_context == PARM && storage_class == csc_register)
5819 else
5821 switch (decl_context)
5823 case FIELD:
5824 if (name)
5825 error_at (loc, "storage class specified for structure "
5826 "field %qE", name);
5827 else
5828 error_at (loc, "storage class specified for structure field");
5829 break;
5830 case PARM:
5831 if (name)
5832 error_at (loc, "storage class specified for parameter %qE",
5833 name);
5834 else
5835 error_at (loc, "storage class specified for unnamed parameter");
5836 break;
5837 default:
5838 error_at (loc, "storage class specified for typename");
5839 break;
5841 storage_class = csc_none;
5842 threadp = false;
5845 else if (storage_class == csc_extern
5846 && initialized
5847 && !funcdef_flag)
5849 /* 'extern' with initialization is invalid if not at file scope. */
5850 if (current_scope == file_scope)
5852 /* It is fine to have 'extern const' when compiling at C
5853 and C++ intersection. */
5854 if (!(warn_cxx_compat && constp))
5855 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5856 name);
5858 else
5859 error_at (loc, "%qE has both %<extern%> and initializer", name);
5861 else if (current_scope == file_scope)
5863 if (storage_class == csc_auto)
5864 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5865 name);
5866 if (pedantic && storage_class == csc_register)
5867 pedwarn (input_location, OPT_Wpedantic,
5868 "file-scope declaration of %qE specifies %<register%>", name);
5870 else
5872 if (storage_class == csc_extern && funcdef_flag)
5873 error_at (loc, "nested function %qE declared %<extern%>", name);
5874 else if (threadp && storage_class == csc_none)
5876 error_at (loc, "function-scope %qE implicitly auto and declared "
5877 "%qs", name,
5878 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5879 threadp = false;
5883 /* Now figure out the structure of the declarator proper.
5884 Descend through it, creating more complex types, until we reach
5885 the declared identifier (or NULL_TREE, in an absolute declarator).
5886 At each stage we maintain an unqualified version of the type
5887 together with any qualifiers that should be applied to it with
5888 c_build_qualified_type; this way, array types including
5889 multidimensional array types are first built up in unqualified
5890 form and then the qualified form is created with
5891 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5893 while (declarator && declarator->kind != cdk_id)
5895 if (type == error_mark_node)
5897 declarator = declarator->declarator;
5898 continue;
5901 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5902 a cdk_pointer (for *...),
5903 a cdk_function (for ...(...)),
5904 a cdk_attrs (for nested attributes),
5905 or a cdk_id (for the name being declared
5906 or the place in an absolute declarator
5907 where the name was omitted).
5908 For the last case, we have just exited the loop.
5910 At this point, TYPE is the type of elements of an array,
5911 or for a function to return, or for a pointer to point to.
5912 After this sequence of ifs, TYPE is the type of the
5913 array or function or pointer, and DECLARATOR has had its
5914 outermost layer removed. */
5916 if (array_ptr_quals != TYPE_UNQUALIFIED
5917 || array_ptr_attrs != NULL_TREE
5918 || array_parm_static)
5920 /* Only the innermost declarator (making a parameter be of
5921 array type which is converted to pointer type)
5922 may have static or type qualifiers. */
5923 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5924 array_ptr_quals = TYPE_UNQUALIFIED;
5925 array_ptr_attrs = NULL_TREE;
5926 array_parm_static = false;
5929 switch (declarator->kind)
5931 case cdk_attrs:
5933 /* A declarator with embedded attributes. */
5934 tree attrs = declarator->u.attrs;
5935 const struct c_declarator *inner_decl;
5936 int attr_flags = 0;
5937 declarator = declarator->declarator;
5938 inner_decl = declarator;
5939 while (inner_decl->kind == cdk_attrs)
5940 inner_decl = inner_decl->declarator;
5941 if (inner_decl->kind == cdk_id)
5942 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5943 else if (inner_decl->kind == cdk_function)
5944 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5945 else if (inner_decl->kind == cdk_array)
5946 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5947 returned_attrs = decl_attributes (&type,
5948 chainon (returned_attrs, attrs),
5949 attr_flags);
5950 break;
5952 case cdk_array:
5954 tree itype = NULL_TREE;
5955 tree size = declarator->u.array.dimen;
5956 /* The index is a signed object `sizetype' bits wide. */
5957 tree index_type = c_common_signed_type (sizetype);
5959 array_ptr_quals = declarator->u.array.quals;
5960 array_ptr_attrs = declarator->u.array.attrs;
5961 array_parm_static = declarator->u.array.static_p;
5962 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5964 declarator = declarator->declarator;
5966 /* Check for some types that there cannot be arrays of. */
5968 if (VOID_TYPE_P (type))
5970 if (name)
5971 error_at (loc, "declaration of %qE as array of voids", name);
5972 else
5973 error_at (loc, "declaration of type name as array of voids");
5974 type = error_mark_node;
5977 if (TREE_CODE (type) == FUNCTION_TYPE)
5979 if (name)
5980 error_at (loc, "declaration of %qE as array of functions",
5981 name);
5982 else
5983 error_at (loc, "declaration of type name as array of "
5984 "functions");
5985 type = error_mark_node;
5988 if (pedantic && !in_system_header_at (input_location)
5989 && flexible_array_type_p (type))
5990 pedwarn (loc, OPT_Wpedantic,
5991 "invalid use of structure with flexible array member");
5993 if (size == error_mark_node)
5994 type = error_mark_node;
5996 if (type == error_mark_node)
5997 continue;
5999 /* If size was specified, set ITYPE to a range-type for
6000 that size. Otherwise, ITYPE remains null. finish_decl
6001 may figure it out from an initial value. */
6003 if (size)
6005 bool size_maybe_const = true;
6006 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6007 && !TREE_OVERFLOW (size));
6008 bool this_size_varies = false;
6010 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6011 lvalue. */
6012 STRIP_TYPE_NOPS (size);
6014 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6016 if (name)
6017 error_at (loc, "size of array %qE has non-integer type",
6018 name);
6019 else
6020 error_at (loc,
6021 "size of unnamed array has non-integer type");
6022 size = integer_one_node;
6024 /* This can happen with enum forward declaration. */
6025 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6027 if (name)
6028 error_at (loc, "size of array %qE has incomplete type",
6029 name);
6030 else
6031 error_at (loc, "size of unnamed array has incomplete "
6032 "type");
6033 size = integer_one_node;
6036 size = c_fully_fold (size, false, &size_maybe_const);
6038 if (pedantic && size_maybe_const && integer_zerop (size))
6040 if (name)
6041 pedwarn (loc, OPT_Wpedantic,
6042 "ISO C forbids zero-size array %qE", name);
6043 else
6044 pedwarn (loc, OPT_Wpedantic,
6045 "ISO C forbids zero-size array");
6048 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6050 constant_expression_warning (size);
6051 if (tree_int_cst_sgn (size) < 0)
6053 if (name)
6054 error_at (loc, "size of array %qE is negative", name);
6055 else
6056 error_at (loc, "size of unnamed array is negative");
6057 size = integer_one_node;
6059 /* Handle a size folded to an integer constant but
6060 not an integer constant expression. */
6061 if (!size_int_const)
6063 /* If this is a file scope declaration of an
6064 ordinary identifier, this is invalid code;
6065 diagnosing it here and not subsequently
6066 treating the type as variable-length avoids
6067 more confusing diagnostics later. */
6068 if ((decl_context == NORMAL || decl_context == FIELD)
6069 && current_scope == file_scope)
6070 pedwarn (input_location, 0,
6071 "variably modified %qE at file scope",
6072 name);
6073 else
6074 this_size_varies = size_varies = true;
6075 warn_variable_length_array (name, size);
6078 else if ((decl_context == NORMAL || decl_context == FIELD)
6079 && current_scope == file_scope)
6081 error_at (loc, "variably modified %qE at file scope", name);
6082 size = integer_one_node;
6084 else
6086 /* Make sure the array size remains visibly
6087 nonconstant even if it is (eg) a const variable
6088 with known value. */
6089 this_size_varies = size_varies = true;
6090 warn_variable_length_array (name, size);
6091 if (sanitize_flags_p (SANITIZE_VLA)
6092 && current_function_decl != NULL_TREE
6093 && decl_context == NORMAL)
6095 /* Evaluate the array size only once. */
6096 size = save_expr (size);
6097 size = c_fully_fold (size, false, NULL);
6098 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6099 ubsan_instrument_vla (loc, size),
6100 size);
6104 if (integer_zerop (size) && !this_size_varies)
6106 /* A zero-length array cannot be represented with
6107 an unsigned index type, which is what we'll
6108 get with build_index_type. Create an
6109 open-ended range instead. */
6110 itype = build_range_type (sizetype, size, NULL_TREE);
6112 else
6114 /* Arrange for the SAVE_EXPR on the inside of the
6115 MINUS_EXPR, which allows the -1 to get folded
6116 with the +1 that happens when building TYPE_SIZE. */
6117 if (size_varies)
6118 size = save_expr (size);
6119 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6120 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6121 integer_zero_node, size);
6123 /* Compute the maximum valid index, that is, size
6124 - 1. Do the calculation in index_type, so that
6125 if it is a variable the computations will be
6126 done in the proper mode. */
6127 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6128 convert (index_type, size),
6129 convert (index_type,
6130 size_one_node));
6132 /* The above overflows when size does not fit
6133 in index_type.
6134 ??? While a size of INT_MAX+1 technically shouldn't
6135 cause an overflow (because we subtract 1), handling
6136 this case seems like an unnecessary complication. */
6137 if (TREE_CODE (size) == INTEGER_CST
6138 && !int_fits_type_p (size, index_type))
6140 if (name)
6141 error_at (loc, "size of array %qE is too large",
6142 name);
6143 else
6144 error_at (loc, "size of unnamed array is too large");
6145 type = error_mark_node;
6146 continue;
6149 itype = build_index_type (itype);
6151 if (this_size_varies)
6153 if (*expr)
6154 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6155 *expr, size);
6156 else
6157 *expr = size;
6158 *expr_const_operands &= size_maybe_const;
6161 else if (decl_context == FIELD)
6163 bool flexible_array_member = false;
6164 if (array_parm_vla_unspec_p)
6165 /* Field names can in fact have function prototype
6166 scope so [*] is disallowed here through making
6167 the field variably modified, not through being
6168 something other than a declaration with function
6169 prototype scope. */
6170 size_varies = true;
6171 else
6173 const struct c_declarator *t = declarator;
6174 while (t->kind == cdk_attrs)
6175 t = t->declarator;
6176 flexible_array_member = (t->kind == cdk_id);
6178 if (flexible_array_member
6179 && !in_system_header_at (input_location))
6180 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6181 "support flexible array members");
6183 /* ISO C99 Flexible array members are effectively
6184 identical to GCC's zero-length array extension. */
6185 if (flexible_array_member || array_parm_vla_unspec_p)
6186 itype = build_range_type (sizetype, size_zero_node,
6187 NULL_TREE);
6189 else if (decl_context == PARM)
6191 if (array_parm_vla_unspec_p)
6193 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6194 size_varies = true;
6197 else if (decl_context == TYPENAME)
6199 if (array_parm_vla_unspec_p)
6201 /* C99 6.7.5.2p4 */
6202 warning (0, "%<[*]%> not in a declaration");
6203 /* We use this to avoid messing up with incomplete
6204 array types of the same type, that would
6205 otherwise be modified below. */
6206 itype = build_range_type (sizetype, size_zero_node,
6207 NULL_TREE);
6208 size_varies = true;
6212 /* Complain about arrays of incomplete types. */
6213 if (!COMPLETE_TYPE_P (type))
6215 error_at (loc, "array type has incomplete element type %qT",
6216 type);
6217 /* See if we can be more helpful. */
6218 if (TREE_CODE (type) == ARRAY_TYPE)
6220 if (name)
6221 inform (loc, "declaration of %qE as multidimensional "
6222 "array must have bounds for all dimensions "
6223 "except the first", name);
6224 else
6225 inform (loc, "declaration of multidimensional array "
6226 "must have bounds for all dimensions except "
6227 "the first");
6229 type = error_mark_node;
6231 else
6232 /* When itype is NULL, a shared incomplete array type is
6233 returned for all array of a given type. Elsewhere we
6234 make sure we don't complete that type before copying
6235 it, but here we want to make sure we don't ever
6236 modify the shared type, so we gcc_assert (itype)
6237 below. */
6239 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6240 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6241 type = build_qualified_type (type,
6242 ENCODE_QUAL_ADDR_SPACE (as));
6244 type = build_array_type (type, itype);
6247 if (type != error_mark_node)
6249 if (size_varies)
6251 /* It is ok to modify type here even if itype is
6252 NULL: if size_varies, we're in a
6253 multi-dimensional array and the inner type has
6254 variable size, so the enclosing shared array type
6255 must too. */
6256 if (size && TREE_CODE (size) == INTEGER_CST)
6257 type
6258 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6259 C_TYPE_VARIABLE_SIZE (type) = 1;
6262 /* The GCC extension for zero-length arrays differs from
6263 ISO flexible array members in that sizeof yields
6264 zero. */
6265 if (size && integer_zerop (size))
6267 gcc_assert (itype);
6268 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6269 TYPE_SIZE (type) = bitsize_zero_node;
6270 TYPE_SIZE_UNIT (type) = size_zero_node;
6271 SET_TYPE_STRUCTURAL_EQUALITY (type);
6273 if (array_parm_vla_unspec_p)
6275 gcc_assert (itype);
6276 /* The type is complete. C99 6.7.5.2p4 */
6277 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6278 TYPE_SIZE (type) = bitsize_zero_node;
6279 TYPE_SIZE_UNIT (type) = size_zero_node;
6280 SET_TYPE_STRUCTURAL_EQUALITY (type);
6283 if (!valid_array_size_p (loc, type, name))
6284 type = error_mark_node;
6287 if (decl_context != PARM
6288 && (array_ptr_quals != TYPE_UNQUALIFIED
6289 || array_ptr_attrs != NULL_TREE
6290 || array_parm_static))
6292 error_at (loc, "static or type qualifiers in non-parameter "
6293 "array declarator");
6294 array_ptr_quals = TYPE_UNQUALIFIED;
6295 array_ptr_attrs = NULL_TREE;
6296 array_parm_static = false;
6298 orig_qual_indirect++;
6299 break;
6301 case cdk_function:
6303 /* Say it's a definition only for the declarator closest
6304 to the identifier, apart possibly from some
6305 attributes. */
6306 bool really_funcdef = false;
6307 tree arg_types;
6308 orig_qual_type = NULL_TREE;
6309 if (funcdef_flag)
6311 const struct c_declarator *t = declarator->declarator;
6312 while (t->kind == cdk_attrs)
6313 t = t->declarator;
6314 really_funcdef = (t->kind == cdk_id);
6317 /* Declaring a function type. Make sure we have a valid
6318 type for the function to return. */
6319 if (type == error_mark_node)
6320 continue;
6322 size_varies = false;
6324 /* Warn about some types functions can't return. */
6325 if (TREE_CODE (type) == FUNCTION_TYPE)
6327 if (name)
6328 error_at (loc, "%qE declared as function returning a "
6329 "function", name);
6330 else
6331 error_at (loc, "type name declared as function "
6332 "returning a function");
6333 type = integer_type_node;
6335 if (TREE_CODE (type) == ARRAY_TYPE)
6337 if (name)
6338 error_at (loc, "%qE declared as function returning an array",
6339 name);
6340 else
6341 error_at (loc, "type name declared as function returning "
6342 "an array");
6343 type = integer_type_node;
6346 /* Construct the function type and go to the next
6347 inner layer of declarator. */
6348 arg_info = declarator->u.arg_info;
6349 arg_types = grokparms (arg_info, really_funcdef);
6351 /* Type qualifiers before the return type of the function
6352 qualify the return type, not the function type. */
6353 if (type_quals)
6355 const enum c_declspec_word ignored_quals_list[] =
6357 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6358 cdw_atomic, cdw_number_of_elements
6360 location_t specs_loc
6361 = smallest_type_quals_location (declspecs->locations,
6362 ignored_quals_list);
6363 if (specs_loc == UNKNOWN_LOCATION)
6364 specs_loc = declspecs->locations[cdw_typedef];
6365 if (specs_loc == UNKNOWN_LOCATION)
6366 specs_loc = loc;
6368 /* Type qualifiers on a function return type are
6369 normally permitted by the standard but have no
6370 effect, so give a warning at -Wreturn-type.
6371 Qualifiers on a void return type are banned on
6372 function definitions in ISO C; GCC used to used
6373 them for noreturn functions. The resolution of C11
6374 DR#423 means qualifiers (other than _Atomic) are
6375 actually removed from the return type when
6376 determining the function type. */
6377 int quals_used = type_quals;
6378 if (flag_isoc11)
6379 quals_used &= TYPE_QUAL_ATOMIC;
6380 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6381 pedwarn (specs_loc, 0,
6382 "function definition has qualified void return type");
6383 else
6384 warning_at (specs_loc, OPT_Wignored_qualifiers,
6385 "type qualifiers ignored on function return type");
6387 /* Ensure an error for restrict on invalid types; the
6388 DR#423 resolution is not entirely clear about
6389 this. */
6390 if (flag_isoc11
6391 && (type_quals & TYPE_QUAL_RESTRICT)
6392 && (!POINTER_TYPE_P (type)
6393 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6394 error_at (loc, "invalid use of %<restrict%>");
6395 if (quals_used)
6396 type = c_build_qualified_type (type, quals_used);
6398 type_quals = TYPE_UNQUALIFIED;
6400 type = build_function_type (type, arg_types);
6401 declarator = declarator->declarator;
6403 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6404 the formal parameter list of this FUNCTION_TYPE to point to
6405 the FUNCTION_TYPE node itself. */
6407 c_arg_tag *tag;
6408 unsigned ix;
6410 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6411 TYPE_CONTEXT (tag->type) = type;
6413 break;
6415 case cdk_pointer:
6417 /* Merge any constancy or volatility into the target type
6418 for the pointer. */
6419 if ((type_quals & TYPE_QUAL_ATOMIC)
6420 && TREE_CODE (type) == FUNCTION_TYPE)
6422 error_at (loc,
6423 "%<_Atomic%>-qualified function type");
6424 type_quals &= ~TYPE_QUAL_ATOMIC;
6426 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6427 && type_quals)
6428 pedwarn (loc, OPT_Wpedantic,
6429 "ISO C forbids qualified function types");
6430 if (type_quals)
6431 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6432 orig_qual_indirect);
6433 orig_qual_type = NULL_TREE;
6434 size_varies = false;
6436 /* When the pointed-to type involves components of variable size,
6437 care must be taken to ensure that the size evaluation code is
6438 emitted early enough to dominate all the possible later uses
6439 and late enough for the variables on which it depends to have
6440 been assigned.
6442 This is expected to happen automatically when the pointed-to
6443 type has a name/declaration of it's own, but special attention
6444 is required if the type is anonymous.
6446 We handle the NORMAL and FIELD contexts here by attaching an
6447 artificial TYPE_DECL to such pointed-to type. This forces the
6448 sizes evaluation at a safe point and ensures it is not deferred
6449 until e.g. within a deeper conditional context.
6451 We expect nothing to be needed here for PARM or TYPENAME.
6452 Pushing a TYPE_DECL at this point for TYPENAME would actually
6453 be incorrect, as we might be in the middle of an expression
6454 with side effects on the pointed-to type size "arguments" prior
6455 to the pointer declaration point and the fake TYPE_DECL in the
6456 enclosing context would force the size evaluation prior to the
6457 side effects. */
6459 if (!TYPE_NAME (type)
6460 && (decl_context == NORMAL || decl_context == FIELD)
6461 && variably_modified_type_p (type, NULL_TREE))
6463 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6464 DECL_ARTIFICIAL (decl) = 1;
6465 pushdecl (decl);
6466 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6467 TYPE_NAME (type) = decl;
6470 type = c_build_pointer_type (type);
6472 /* Process type qualifiers (such as const or volatile)
6473 that were given inside the `*'. */
6474 type_quals = declarator->u.pointer_quals;
6476 declarator = declarator->declarator;
6477 break;
6479 default:
6480 gcc_unreachable ();
6483 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6485 /* Now TYPE has the actual type, apart from any qualifiers in
6486 TYPE_QUALS. */
6488 /* Warn about address space used for things other than static memory or
6489 pointers. */
6490 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6491 if (!ADDR_SPACE_GENERIC_P (address_space))
6493 if (decl_context == NORMAL)
6495 switch (storage_class)
6497 case csc_auto:
6498 error ("%qs combined with %<auto%> qualifier for %qE",
6499 c_addr_space_name (address_space), name);
6500 break;
6501 case csc_register:
6502 error ("%qs combined with %<register%> qualifier for %qE",
6503 c_addr_space_name (address_space), name);
6504 break;
6505 case csc_none:
6506 if (current_function_scope)
6508 error ("%qs specified for auto variable %qE",
6509 c_addr_space_name (address_space), name);
6510 break;
6512 break;
6513 case csc_static:
6514 case csc_extern:
6515 case csc_typedef:
6516 break;
6517 default:
6518 gcc_unreachable ();
6521 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6523 if (name)
6524 error ("%qs specified for parameter %qE",
6525 c_addr_space_name (address_space), name);
6526 else
6527 error ("%qs specified for unnamed parameter",
6528 c_addr_space_name (address_space));
6530 else if (decl_context == FIELD)
6532 if (name)
6533 error ("%qs specified for structure field %qE",
6534 c_addr_space_name (address_space), name);
6535 else
6536 error ("%qs specified for structure field",
6537 c_addr_space_name (address_space));
6541 /* Check the type and width of a bit-field. */
6542 if (bitfield)
6544 check_bitfield_type_and_width (loc, &type, width, name);
6545 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6546 atomic types are permitted for bit-fields; we have no code to
6547 make bit-field accesses atomic, so disallow them. */
6548 if (type_quals & TYPE_QUAL_ATOMIC)
6550 if (name)
6551 error_at (loc, "bit-field %qE has atomic type", name);
6552 else
6553 error_at (loc, "bit-field has atomic type");
6554 type_quals &= ~TYPE_QUAL_ATOMIC;
6558 /* Reject invalid uses of _Alignas. */
6559 if (declspecs->alignas_p)
6561 if (storage_class == csc_typedef)
6562 error_at (loc, "alignment specified for typedef %qE", name);
6563 else if (storage_class == csc_register)
6564 error_at (loc, "alignment specified for %<register%> object %qE",
6565 name);
6566 else if (decl_context == PARM)
6568 if (name)
6569 error_at (loc, "alignment specified for parameter %qE", name);
6570 else
6571 error_at (loc, "alignment specified for unnamed parameter");
6573 else if (bitfield)
6575 if (name)
6576 error_at (loc, "alignment specified for bit-field %qE", name);
6577 else
6578 error_at (loc, "alignment specified for unnamed bit-field");
6580 else if (TREE_CODE (type) == FUNCTION_TYPE)
6581 error_at (loc, "alignment specified for function %qE", name);
6582 else if (declspecs->align_log != -1 && TYPE_P (type))
6584 alignas_align = 1U << declspecs->align_log;
6585 if (alignas_align < min_align_of_type (type))
6587 if (name)
6588 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6589 "alignment of %qE", name);
6590 else
6591 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6592 "alignment of unnamed field");
6593 alignas_align = 0;
6598 /* If this is declaring a typedef name, return a TYPE_DECL. */
6600 if (storage_class == csc_typedef)
6602 tree decl;
6603 if ((type_quals & TYPE_QUAL_ATOMIC)
6604 && TREE_CODE (type) == FUNCTION_TYPE)
6606 error_at (loc,
6607 "%<_Atomic%>-qualified function type");
6608 type_quals &= ~TYPE_QUAL_ATOMIC;
6610 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6611 && type_quals)
6612 pedwarn (loc, OPT_Wpedantic,
6613 "ISO C forbids qualified function types");
6614 if (type_quals)
6615 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6616 orig_qual_indirect);
6617 decl = build_decl (declarator->id_loc,
6618 TYPE_DECL, declarator->u.id, type);
6619 if (declspecs->explicit_signed_p)
6620 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6621 if (declspecs->inline_p)
6622 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6623 if (declspecs->noreturn_p)
6624 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6626 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6628 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6630 if (b != NULL
6631 && b->decl != NULL_TREE
6632 && (B_IN_CURRENT_SCOPE (b)
6633 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6634 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6636 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6637 ("using %qD as both a typedef and a tag is "
6638 "invalid in C++"), decl)
6639 && b->locus != UNKNOWN_LOCATION)
6640 inform (b->locus, "originally defined here");
6644 return decl;
6647 /* If this is a type name (such as, in a cast or sizeof),
6648 compute the type and return it now. */
6650 if (decl_context == TYPENAME)
6652 /* Note that the grammar rejects storage classes in typenames
6653 and fields. */
6654 gcc_assert (storage_class == csc_none && !threadp
6655 && !declspecs->inline_p && !declspecs->noreturn_p);
6656 if ((type_quals & TYPE_QUAL_ATOMIC)
6657 && TREE_CODE (type) == FUNCTION_TYPE)
6659 error_at (loc,
6660 "%<_Atomic%>-qualified function type");
6661 type_quals &= ~TYPE_QUAL_ATOMIC;
6663 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6664 && type_quals)
6665 pedwarn (loc, OPT_Wpedantic,
6666 "ISO C forbids const or volatile function types");
6667 if (type_quals)
6668 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6669 orig_qual_indirect);
6670 return type;
6673 if (pedantic && decl_context == FIELD
6674 && variably_modified_type_p (type, NULL_TREE))
6676 /* C99 6.7.2.1p8 */
6677 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6678 "have a variably modified type");
6681 /* Aside from typedefs and type names (handle above),
6682 `void' at top level (not within pointer)
6683 is allowed only in public variables.
6684 We don't complain about parms either, but that is because
6685 a better error message can be made later. */
6687 if (VOID_TYPE_P (type) && decl_context != PARM
6688 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6689 && (storage_class == csc_extern
6690 || (current_scope == file_scope
6691 && !(storage_class == csc_static
6692 || storage_class == csc_register)))))
6694 error_at (loc, "variable or field %qE declared void", name);
6695 type = integer_type_node;
6698 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6699 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6702 tree decl;
6704 if (decl_context == PARM)
6706 tree promoted_type;
6707 bool array_parameter_p = false;
6709 /* A parameter declared as an array of T is really a pointer to T.
6710 One declared as a function is really a pointer to a function. */
6712 if (TREE_CODE (type) == ARRAY_TYPE)
6714 /* Transfer const-ness of array into that of type pointed to. */
6715 type = TREE_TYPE (type);
6716 if (orig_qual_type != NULL_TREE)
6718 if (orig_qual_indirect == 0)
6719 orig_qual_type = TREE_TYPE (orig_qual_type);
6720 else
6721 orig_qual_indirect--;
6723 if (type_quals)
6724 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6725 orig_qual_indirect);
6726 type = c_build_pointer_type (type);
6727 type_quals = array_ptr_quals;
6728 if (type_quals)
6729 type = c_build_qualified_type (type, type_quals);
6731 /* We don't yet implement attributes in this context. */
6732 if (array_ptr_attrs != NULL_TREE)
6733 warning_at (loc, OPT_Wattributes,
6734 "attributes in parameter array declarator ignored");
6736 size_varies = false;
6737 array_parameter_p = true;
6739 else if (TREE_CODE (type) == FUNCTION_TYPE)
6741 if (type_quals & TYPE_QUAL_ATOMIC)
6743 error_at (loc,
6744 "%<_Atomic%>-qualified function type");
6745 type_quals &= ~TYPE_QUAL_ATOMIC;
6747 else if (type_quals)
6748 pedwarn (loc, OPT_Wpedantic,
6749 "ISO C forbids qualified function types");
6750 if (type_quals)
6751 type = c_build_qualified_type (type, type_quals);
6752 type = c_build_pointer_type (type);
6753 type_quals = TYPE_UNQUALIFIED;
6755 else if (type_quals)
6756 type = c_build_qualified_type (type, type_quals);
6758 decl = build_decl (declarator->id_loc,
6759 PARM_DECL, declarator->u.id, type);
6760 if (size_varies)
6761 C_DECL_VARIABLE_SIZE (decl) = 1;
6762 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6764 /* Compute the type actually passed in the parmlist,
6765 for the case where there is no prototype.
6766 (For example, shorts and chars are passed as ints.)
6767 When there is a prototype, this is overridden later. */
6769 if (type == error_mark_node)
6770 promoted_type = type;
6771 else
6772 promoted_type = c_type_promotes_to (type);
6774 DECL_ARG_TYPE (decl) = promoted_type;
6775 if (declspecs->inline_p)
6776 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6777 if (declspecs->noreturn_p)
6778 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6780 else if (decl_context == FIELD)
6782 /* Note that the grammar rejects storage classes in typenames
6783 and fields. */
6784 gcc_assert (storage_class == csc_none && !threadp
6785 && !declspecs->inline_p && !declspecs->noreturn_p);
6787 /* Structure field. It may not be a function. */
6789 if (TREE_CODE (type) == FUNCTION_TYPE)
6791 error_at (loc, "field %qE declared as a function", name);
6792 type = build_pointer_type (type);
6794 else if (TREE_CODE (type) != ERROR_MARK
6795 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6797 if (name)
6798 error_at (loc, "field %qE has incomplete type", name);
6799 else
6800 error_at (loc, "unnamed field has incomplete type");
6801 type = error_mark_node;
6803 else if (TREE_CODE (type) == ARRAY_TYPE
6804 && TYPE_DOMAIN (type) == NULL_TREE)
6806 /* We have a flexible array member through a typedef.
6807 Set suitable range. Whether this is a correct position
6808 for a flexible array member will be determined elsewhere. */
6809 if (!in_system_header_at (input_location))
6810 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6811 "support flexible array members");
6812 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6813 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6814 NULL_TREE);
6815 if (orig_qual_indirect == 0)
6816 orig_qual_type = NULL_TREE;
6818 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6819 orig_qual_indirect);
6820 decl = build_decl (declarator->id_loc,
6821 FIELD_DECL, declarator->u.id, type);
6822 DECL_NONADDRESSABLE_P (decl) = bitfield;
6823 if (bitfield && !declarator->u.id)
6825 TREE_NO_WARNING (decl) = 1;
6826 DECL_PADDING_P (decl) = 1;
6829 if (size_varies)
6830 C_DECL_VARIABLE_SIZE (decl) = 1;
6832 else if (TREE_CODE (type) == FUNCTION_TYPE)
6834 if (storage_class == csc_register || threadp)
6836 error_at (loc, "invalid storage class for function %qE", name);
6838 else if (current_scope != file_scope)
6840 /* Function declaration not at file scope. Storage
6841 classes other than `extern' are not allowed, C99
6842 6.7.1p5, and `extern' makes no difference. However,
6843 GCC allows 'auto', perhaps with 'inline', to support
6844 nested functions. */
6845 if (storage_class == csc_auto)
6846 pedwarn (loc, OPT_Wpedantic,
6847 "invalid storage class for function %qE", name);
6848 else if (storage_class == csc_static)
6850 error_at (loc, "invalid storage class for function %qE", name);
6851 if (funcdef_flag)
6852 storage_class = declspecs->storage_class = csc_none;
6853 else
6854 return NULL_TREE;
6858 decl = build_decl (declarator->id_loc,
6859 FUNCTION_DECL, declarator->u.id, type);
6860 decl = build_decl_attribute_variant (decl, decl_attr);
6862 if (type_quals & TYPE_QUAL_ATOMIC)
6864 error_at (loc,
6865 "%<_Atomic%>-qualified function type");
6866 type_quals &= ~TYPE_QUAL_ATOMIC;
6868 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6869 pedwarn (loc, OPT_Wpedantic,
6870 "ISO C forbids qualified function types");
6872 /* Every function declaration is an external reference
6873 (DECL_EXTERNAL) except for those which are not at file
6874 scope and are explicitly declared "auto". This is
6875 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6876 GCC to signify a forward declaration of a nested function. */
6877 if (storage_class == csc_auto && current_scope != file_scope)
6878 DECL_EXTERNAL (decl) = 0;
6879 /* In C99, a function which is declared 'inline' with 'extern'
6880 is not an external reference (which is confusing). It
6881 means that the later definition of the function must be output
6882 in this file, C99 6.7.4p6. In GNU C89, a function declared
6883 'extern inline' is an external reference. */
6884 else if (declspecs->inline_p && storage_class != csc_static)
6885 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6886 == flag_gnu89_inline);
6887 else
6888 DECL_EXTERNAL (decl) = !initialized;
6890 /* Record absence of global scope for `static' or `auto'. */
6891 TREE_PUBLIC (decl)
6892 = !(storage_class == csc_static || storage_class == csc_auto);
6894 /* For a function definition, record the argument information
6895 block where store_parm_decls will look for it. */
6896 if (funcdef_flag)
6897 current_function_arg_info = arg_info;
6899 if (declspecs->default_int_p)
6900 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6902 /* Record presence of `inline' and `_Noreturn', if it is
6903 reasonable. */
6904 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6906 if (declspecs->inline_p)
6907 pedwarn (loc, 0, "cannot inline function %<main%>");
6908 if (declspecs->noreturn_p)
6909 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6911 else
6913 if (declspecs->inline_p)
6914 /* Record that the function is declared `inline'. */
6915 DECL_DECLARED_INLINE_P (decl) = 1;
6916 if (declspecs->noreturn_p)
6918 if (flag_isoc99)
6919 pedwarn_c99 (loc, OPT_Wpedantic,
6920 "ISO C99 does not support %<_Noreturn%>");
6921 else
6922 pedwarn_c99 (loc, OPT_Wpedantic,
6923 "ISO C90 does not support %<_Noreturn%>");
6924 TREE_THIS_VOLATILE (decl) = 1;
6928 else
6930 /* It's a variable. */
6931 /* An uninitialized decl with `extern' is a reference. */
6932 int extern_ref = !initialized && storage_class == csc_extern;
6934 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6935 orig_qual_indirect);
6937 /* C99 6.2.2p7: It is invalid (compile-time undefined
6938 behavior) to create an 'extern' declaration for a
6939 variable if there is a global declaration that is
6940 'static' and the global declaration is not visible.
6941 (If the static declaration _is_ currently visible,
6942 the 'extern' declaration is taken to refer to that decl.) */
6943 if (extern_ref && current_scope != file_scope)
6945 tree global_decl = identifier_global_value (declarator->u.id);
6946 tree visible_decl = lookup_name (declarator->u.id);
6948 if (global_decl
6949 && global_decl != visible_decl
6950 && VAR_P (global_decl)
6951 && !TREE_PUBLIC (global_decl))
6952 error_at (loc, "variable previously declared %<static%> "
6953 "redeclared %<extern%>");
6956 decl = build_decl (declarator->id_loc,
6957 VAR_DECL, declarator->u.id, type);
6958 if (size_varies)
6959 C_DECL_VARIABLE_SIZE (decl) = 1;
6961 if (declspecs->inline_p)
6962 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6963 if (declspecs->noreturn_p)
6964 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6966 /* At file scope, an initialized extern declaration may follow
6967 a static declaration. In that case, DECL_EXTERNAL will be
6968 reset later in start_decl. */
6969 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6971 /* At file scope, the presence of a `static' or `register' storage
6972 class specifier, or the absence of all storage class specifiers
6973 makes this declaration a definition (perhaps tentative). Also,
6974 the absence of `static' makes it public. */
6975 if (current_scope == file_scope)
6977 TREE_PUBLIC (decl) = storage_class != csc_static;
6978 TREE_STATIC (decl) = !extern_ref;
6980 /* Not at file scope, only `static' makes a static definition. */
6981 else
6983 TREE_STATIC (decl) = (storage_class == csc_static);
6984 TREE_PUBLIC (decl) = extern_ref;
6987 if (threadp)
6988 set_decl_tls_model (decl, decl_default_tls_model (decl));
6991 if ((storage_class == csc_extern
6992 || (storage_class == csc_none
6993 && TREE_CODE (type) == FUNCTION_TYPE
6994 && !funcdef_flag))
6995 && variably_modified_type_p (type, NULL_TREE))
6997 /* C99 6.7.5.2p2 */
6998 if (TREE_CODE (type) == FUNCTION_TYPE)
6999 error_at (loc, "non-nested function with variably modified type");
7000 else
7001 error_at (loc, "object with variably modified type must have "
7002 "no linkage");
7005 /* Record `register' declaration for warnings on &
7006 and in case doing stupid register allocation. */
7008 if (storage_class == csc_register)
7010 C_DECL_REGISTER (decl) = 1;
7011 DECL_REGISTER (decl) = 1;
7014 /* Record constancy and volatility. */
7015 c_apply_type_quals_to_decl (type_quals, decl);
7017 /* Apply _Alignas specifiers. */
7018 if (alignas_align)
7020 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7021 DECL_USER_ALIGN (decl) = 1;
7024 /* If a type has volatile components, it should be stored in memory.
7025 Otherwise, the fact that those components are volatile
7026 will be ignored, and would even crash the compiler.
7027 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7028 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7029 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7030 || TREE_CODE (decl) == RESULT_DECL))
7032 /* It is not an error for a structure with volatile fields to
7033 be declared register, but reset DECL_REGISTER since it
7034 cannot actually go in a register. */
7035 int was_reg = C_DECL_REGISTER (decl);
7036 C_DECL_REGISTER (decl) = 0;
7037 DECL_REGISTER (decl) = 0;
7038 c_mark_addressable (decl);
7039 C_DECL_REGISTER (decl) = was_reg;
7042 /* This is the earliest point at which we might know the assembler
7043 name of a variable. Thus, if it's known before this, die horribly. */
7044 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7045 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7047 if (warn_cxx_compat
7048 && VAR_P (decl)
7049 && TREE_PUBLIC (decl)
7050 && TREE_STATIC (decl)
7051 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7052 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7053 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7054 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7055 ("non-local variable %qD with anonymous type is "
7056 "questionable in C++"),
7057 decl);
7059 return decl;
7063 /* Decode the parameter-list info for a function type or function definition.
7064 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7065 if there is an identifier list instead of a parameter decl list).
7066 These two functions are separate because when a function returns
7067 or receives functions then each is called multiple times but the order
7068 of calls is different. The last call to `grokparms' is always the one
7069 that contains the formal parameter names of a function definition.
7071 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7073 FUNCDEF_FLAG is true for a function definition, false for
7074 a mere declaration. A nonempty identifier-list gets an error message
7075 when FUNCDEF_FLAG is false. */
7077 static tree
7078 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7080 tree arg_types = arg_info->types;
7082 if (funcdef_flag && arg_info->had_vla_unspec)
7084 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7085 /* C99 6.7.5.2p4 */
7086 error ("%<[*]%> not allowed in other than function prototype scope");
7089 if (arg_types == NULL_TREE && !funcdef_flag
7090 && !in_system_header_at (input_location))
7091 warning (OPT_Wstrict_prototypes,
7092 "function declaration isn%'t a prototype");
7094 if (arg_types == error_mark_node)
7095 /* Don't set TYPE_ARG_TYPES in this case. */
7096 return NULL_TREE;
7098 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7100 if (!funcdef_flag)
7102 pedwarn (input_location, 0, "parameter names (without types) in "
7103 "function declaration");
7104 arg_info->parms = NULL_TREE;
7106 else
7107 arg_info->parms = arg_info->types;
7109 arg_info->types = NULL_TREE;
7110 return NULL_TREE;
7112 else
7114 tree parm, type, typelt;
7115 unsigned int parmno;
7117 /* If there is a parameter of incomplete type in a definition,
7118 this is an error. In a declaration this is valid, and a
7119 struct or union type may be completed later, before any calls
7120 or definition of the function. In the case where the tag was
7121 first declared within the parameter list, a warning has
7122 already been given. If a parameter has void type, then
7123 however the function cannot be defined or called, so
7124 warn. */
7126 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7127 parm;
7128 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7130 type = TREE_VALUE (typelt);
7131 if (type == error_mark_node)
7132 continue;
7134 if (!COMPLETE_TYPE_P (type))
7136 if (funcdef_flag)
7138 if (DECL_NAME (parm))
7139 error_at (input_location,
7140 "parameter %u (%q+D) has incomplete type",
7141 parmno, parm);
7142 else
7143 error_at (DECL_SOURCE_LOCATION (parm),
7144 "parameter %u has incomplete type",
7145 parmno);
7147 TREE_VALUE (typelt) = error_mark_node;
7148 TREE_TYPE (parm) = error_mark_node;
7149 arg_types = NULL_TREE;
7151 else if (VOID_TYPE_P (type))
7153 if (DECL_NAME (parm))
7154 warning_at (input_location, 0,
7155 "parameter %u (%q+D) has void type",
7156 parmno, parm);
7157 else
7158 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7159 "parameter %u has void type",
7160 parmno);
7164 if (DECL_NAME (parm) && TREE_USED (parm))
7165 warn_if_shadowing (parm);
7167 return arg_types;
7171 /* Allocate and initialize a c_arg_info structure from the parser's
7172 obstack. */
7174 struct c_arg_info *
7175 build_arg_info (void)
7177 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7178 ret->parms = NULL_TREE;
7179 ret->tags = NULL;
7180 ret->types = NULL_TREE;
7181 ret->others = NULL_TREE;
7182 ret->pending_sizes = NULL;
7183 ret->had_vla_unspec = 0;
7184 return ret;
7187 /* Take apart the current scope and return a c_arg_info structure with
7188 info on a parameter list just parsed.
7190 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7192 ELLIPSIS being true means the argument list ended in '...' so don't
7193 append a sentinel (void_list_node) to the end of the type-list.
7195 EXPR is NULL or an expression that needs to be evaluated for the
7196 side effects of array size expressions in the parameters. */
7198 struct c_arg_info *
7199 get_parm_info (bool ellipsis, tree expr)
7201 struct c_binding *b = current_scope->bindings;
7202 struct c_arg_info *arg_info = build_arg_info ();
7204 tree parms = NULL_TREE;
7205 vec<c_arg_tag, va_gc> *tags = NULL;
7206 tree types = NULL_TREE;
7207 tree others = NULL_TREE;
7209 bool gave_void_only_once_err = false;
7211 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7213 /* The bindings in this scope must not get put into a block.
7214 We will take care of deleting the binding nodes. */
7215 current_scope->bindings = 0;
7217 /* This function is only called if there was *something* on the
7218 parameter list. */
7219 gcc_assert (b);
7221 /* A parameter list consisting solely of 'void' indicates that the
7222 function takes no arguments. But if the 'void' is qualified
7223 (by 'const' or 'volatile'), or has a storage class specifier
7224 ('register'), then the behavior is undefined; issue an error.
7225 Typedefs for 'void' are OK (see DR#157). */
7226 if (b->prev == 0 /* one binding */
7227 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7228 && !DECL_NAME (b->decl) /* anonymous */
7229 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7231 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7232 || C_DECL_REGISTER (b->decl))
7233 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7235 /* There cannot be an ellipsis. */
7236 if (ellipsis)
7237 error_at (b->locus, "%<void%> must be the only parameter");
7239 arg_info->types = void_list_node;
7240 return arg_info;
7243 if (!ellipsis)
7244 types = void_list_node;
7246 /* Break up the bindings list into parms, tags, types, and others;
7247 apply sanity checks; purge the name-to-decl bindings. */
7248 while (b)
7250 tree decl = b->decl;
7251 tree type = TREE_TYPE (decl);
7252 c_arg_tag tag;
7253 const char *keyword;
7255 switch (TREE_CODE (decl))
7257 case PARM_DECL:
7258 if (b->id)
7260 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7261 I_SYMBOL_BINDING (b->id) = b->shadowed;
7264 /* Check for forward decls that never got their actual decl. */
7265 if (TREE_ASM_WRITTEN (decl))
7266 error_at (b->locus,
7267 "parameter %q+D has just a forward declaration", decl);
7268 /* Check for (..., void, ...) and issue an error. */
7269 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7271 if (!gave_void_only_once_err)
7273 error_at (b->locus, "%<void%> must be the only parameter");
7274 gave_void_only_once_err = true;
7277 else
7279 /* Valid parameter, add it to the list. */
7280 DECL_CHAIN (decl) = parms;
7281 parms = decl;
7283 /* Since there is a prototype, args are passed in their
7284 declared types. The back end may override this later. */
7285 DECL_ARG_TYPE (decl) = type;
7286 types = tree_cons (0, type, types);
7288 break;
7290 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7291 case UNION_TYPE: keyword = "union"; goto tag;
7292 case RECORD_TYPE: keyword = "struct"; goto tag;
7293 tag:
7294 /* Types may not have tag-names, in which case the type
7295 appears in the bindings list with b->id NULL. */
7296 if (b->id)
7298 gcc_assert (I_TAG_BINDING (b->id) == b);
7299 I_TAG_BINDING (b->id) = b->shadowed;
7302 /* Warn about any struct, union or enum tags defined in a
7303 parameter list. The scope of such types is limited to
7304 the parameter list, which is rarely if ever desirable
7305 (it's impossible to call such a function with type-
7306 correct arguments). An anonymous union parm type is
7307 meaningful as a GNU extension, so don't warn for that. */
7308 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7310 if (b->id)
7311 /* The %s will be one of 'struct', 'union', or 'enum'. */
7312 warning_at (b->locus, 0,
7313 "%<%s %E%> declared inside parameter list"
7314 " will not be visible outside of this definition or"
7315 " declaration", keyword, b->id);
7316 else
7317 /* The %s will be one of 'struct', 'union', or 'enum'. */
7318 warning_at (b->locus, 0,
7319 "anonymous %s declared inside parameter list"
7320 " will not be visible outside of this definition or"
7321 " declaration", keyword);
7324 tag.id = b->id;
7325 tag.type = decl;
7326 vec_safe_push (tags, tag);
7327 break;
7329 case FUNCTION_DECL:
7330 /* FUNCTION_DECLs appear when there is an implicit function
7331 declaration in the parameter list. */
7332 gcc_assert (b->nested || seen_error ());
7333 goto set_shadowed;
7335 case CONST_DECL:
7336 case TYPE_DECL:
7337 /* CONST_DECLs appear here when we have an embedded enum,
7338 and TYPE_DECLs appear here when we have an embedded struct
7339 or union. No warnings for this - we already warned about the
7340 type itself. */
7342 /* When we reinsert this decl in the function body, we need
7343 to reconstruct whether it was marked as nested. */
7344 gcc_assert (!b->nested);
7345 DECL_CHAIN (decl) = others;
7346 others = decl;
7347 /* fall through */
7349 case ERROR_MARK:
7350 set_shadowed:
7351 /* error_mark_node appears here when we have an undeclared
7352 variable. Just throw it away. */
7353 if (b->id)
7355 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7356 I_SYMBOL_BINDING (b->id) = b->shadowed;
7358 break;
7360 /* Other things that might be encountered. */
7361 case LABEL_DECL:
7362 case VAR_DECL:
7363 default:
7364 gcc_unreachable ();
7367 b = free_binding_and_advance (b);
7370 arg_info->parms = parms;
7371 arg_info->tags = tags;
7372 arg_info->types = types;
7373 arg_info->others = others;
7374 arg_info->pending_sizes = expr;
7375 return arg_info;
7378 /* Get the struct, enum or union (CODE says which) with tag NAME.
7379 Define the tag as a forward-reference with location LOC if it is
7380 not defined. Return a c_typespec structure for the type
7381 specifier. */
7383 struct c_typespec
7384 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7386 struct c_typespec ret;
7387 tree ref;
7388 location_t refloc;
7390 ret.expr = NULL_TREE;
7391 ret.expr_const_operands = true;
7393 /* If a cross reference is requested, look up the type
7394 already defined for this tag and return it. */
7396 ref = lookup_tag (code, name, false, &refloc);
7397 /* If this is the right type of tag, return what we found.
7398 (This reference will be shadowed by shadow_tag later if appropriate.)
7399 If this is the wrong type of tag, do not return it. If it was the
7400 wrong type in the same scope, we will have had an error
7401 message already; if in a different scope and declaring
7402 a name, pending_xref_error will give an error message; but if in a
7403 different scope and not declaring a name, this tag should
7404 shadow the previous declaration of a different type of tag, and
7405 this would not work properly if we return the reference found.
7406 (For example, with "struct foo" in an outer scope, "union foo;"
7407 must shadow that tag with a new one of union type.) */
7408 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7409 if (ref && TREE_CODE (ref) == code)
7411 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7412 && loc != UNKNOWN_LOCATION
7413 && warn_cxx_compat)
7415 switch (code)
7417 case ENUMERAL_TYPE:
7418 warning_at (loc, OPT_Wc___compat,
7419 ("enum type defined in struct or union "
7420 "is not visible in C++"));
7421 inform (refloc, "enum type defined here");
7422 break;
7423 case RECORD_TYPE:
7424 warning_at (loc, OPT_Wc___compat,
7425 ("struct defined in struct or union "
7426 "is not visible in C++"));
7427 inform (refloc, "struct defined here");
7428 break;
7429 case UNION_TYPE:
7430 warning_at (loc, OPT_Wc___compat,
7431 ("union defined in struct or union "
7432 "is not visible in C++"));
7433 inform (refloc, "union defined here");
7434 break;
7435 default:
7436 gcc_unreachable();
7440 ret.spec = ref;
7441 return ret;
7444 /* If no such tag is yet defined, create a forward-reference node
7445 and record it as the "definition".
7446 When a real declaration of this type is found,
7447 the forward-reference will be altered into a real type. */
7449 ref = make_node (code);
7450 if (code == ENUMERAL_TYPE)
7452 /* Give the type a default layout like unsigned int
7453 to avoid crashing if it does not get defined. */
7454 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7455 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7456 TYPE_USER_ALIGN (ref) = 0;
7457 TYPE_UNSIGNED (ref) = 1;
7458 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7459 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7460 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7463 pushtag (loc, name, ref);
7465 ret.spec = ref;
7466 return ret;
7469 /* Get the struct, enum or union (CODE says which) with tag NAME.
7470 Define the tag as a forward-reference if it is not defined.
7471 Return a tree for the type. */
7473 tree
7474 xref_tag (enum tree_code code, tree name)
7476 return parser_xref_tag (input_location, code, name).spec;
7479 /* Make sure that the tag NAME is defined *in the current scope*
7480 at least as a forward reference.
7481 LOC is the location of the struct's definition.
7482 CODE says which kind of tag NAME ought to be.
7484 This stores the current value of the file static STRUCT_PARSE_INFO
7485 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7486 new c_struct_parse_info structure. The old value of
7487 STRUCT_PARSE_INFO is restored in finish_struct. */
7489 tree
7490 start_struct (location_t loc, enum tree_code code, tree name,
7491 struct c_struct_parse_info **enclosing_struct_parse_info)
7493 /* If there is already a tag defined at this scope
7494 (as a forward reference), just return it. */
7496 tree ref = NULL_TREE;
7497 location_t refloc = UNKNOWN_LOCATION;
7499 if (name != NULL_TREE)
7500 ref = lookup_tag (code, name, true, &refloc);
7501 if (ref && TREE_CODE (ref) == code)
7503 if (TYPE_STUB_DECL (ref))
7504 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7506 if (TYPE_SIZE (ref))
7508 if (code == UNION_TYPE)
7509 error_at (loc, "redefinition of %<union %E%>", name);
7510 else
7511 error_at (loc, "redefinition of %<struct %E%>", name);
7512 if (refloc != UNKNOWN_LOCATION)
7513 inform (refloc, "originally defined here");
7514 /* Don't create structures using a name already in use. */
7515 ref = NULL_TREE;
7517 else if (C_TYPE_BEING_DEFINED (ref))
7519 if (code == UNION_TYPE)
7520 error_at (loc, "nested redefinition of %<union %E%>", name);
7521 else
7522 error_at (loc, "nested redefinition of %<struct %E%>", name);
7523 /* Don't bother to report "originally defined here" for a
7524 nested redefinition; the original definition should be
7525 obvious. */
7526 /* Don't create structures that contain themselves. */
7527 ref = NULL_TREE;
7531 /* Otherwise create a forward-reference just so the tag is in scope. */
7533 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7535 ref = make_node (code);
7536 pushtag (loc, name, ref);
7539 C_TYPE_BEING_DEFINED (ref) = 1;
7540 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7541 TYPE_PACKED (v) = flag_pack_struct;
7543 *enclosing_struct_parse_info = struct_parse_info;
7544 struct_parse_info = new c_struct_parse_info ();
7546 /* FIXME: This will issue a warning for a use of a type defined
7547 within a statement expr used within sizeof, et. al. This is not
7548 terribly serious as C++ doesn't permit statement exprs within
7549 sizeof anyhow. */
7550 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7551 warning_at (loc, OPT_Wc___compat,
7552 "defining type in %qs expression is invalid in C++",
7553 (in_sizeof
7554 ? "sizeof"
7555 : (in_typeof ? "typeof" : "alignof")));
7557 return ref;
7560 /* Process the specs, declarator and width (NULL if omitted)
7561 of a structure component, returning a FIELD_DECL node.
7562 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7563 DECL_ATTRS is as for grokdeclarator.
7565 LOC is the location of the structure component.
7567 This is done during the parsing of the struct declaration.
7568 The FIELD_DECL nodes are chained together and the lot of them
7569 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7571 tree
7572 grokfield (location_t loc,
7573 struct c_declarator *declarator, struct c_declspecs *declspecs,
7574 tree width, tree *decl_attrs)
7576 tree value;
7578 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7579 && width == NULL_TREE)
7581 /* This is an unnamed decl.
7583 If we have something of the form "union { list } ;" then this
7584 is the anonymous union extension. Similarly for struct.
7586 If this is something of the form "struct foo;", then
7587 If MS or Plan 9 extensions are enabled, this is handled as
7588 an anonymous struct.
7589 Otherwise this is a forward declaration of a structure tag.
7591 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7592 If foo names a structure or union without a tag, then this
7593 is an anonymous struct (this is permitted by C11).
7594 If MS or Plan 9 extensions are enabled and foo names a
7595 structure, then again this is an anonymous struct.
7596 Otherwise this is an error.
7598 Oh what a horrid tangled web we weave. I wonder if MS consciously
7599 took this from Plan 9 or if it was an accident of implementation
7600 that took root before someone noticed the bug... */
7602 tree type = declspecs->type;
7603 bool ok = false;
7605 if (RECORD_OR_UNION_TYPE_P (type)
7606 && (flag_ms_extensions
7607 || flag_plan9_extensions
7608 || !declspecs->typedef_p))
7610 if (flag_ms_extensions || flag_plan9_extensions)
7611 ok = true;
7612 else if (TYPE_NAME (type) == NULL)
7613 ok = true;
7614 else
7615 ok = false;
7617 if (!ok)
7619 pedwarn (loc, 0, "declaration does not declare anything");
7620 return NULL_TREE;
7622 if (flag_isoc99)
7623 pedwarn_c99 (loc, OPT_Wpedantic,
7624 "ISO C99 doesn%'t support unnamed structs/unions");
7625 else
7626 pedwarn_c99 (loc, OPT_Wpedantic,
7627 "ISO C90 doesn%'t support unnamed structs/unions");
7630 value = grokdeclarator (declarator, declspecs, FIELD, false,
7631 width ? &width : NULL, decl_attrs, NULL, NULL,
7632 DEPRECATED_NORMAL);
7634 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7635 DECL_INITIAL (value) = width;
7636 if (width)
7637 SET_DECL_C_BIT_FIELD (value);
7639 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7641 /* If we currently have a binding for this field, set the
7642 in_struct field in the binding, so that we warn about lookups
7643 which find it. */
7644 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7645 if (b != NULL)
7647 /* If the in_struct field is not yet set, push it on a list
7648 to be cleared when this struct is finished. */
7649 if (!b->in_struct)
7651 struct_parse_info->fields.safe_push (b);
7652 b->in_struct = 1;
7657 return value;
7660 /* Subroutine of detect_field_duplicates: return whether X and Y,
7661 which are both fields in the same struct, have duplicate field
7662 names. */
7664 static bool
7665 is_duplicate_field (tree x, tree y)
7667 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7668 return true;
7670 /* When using -fplan9-extensions, an anonymous field whose name is a
7671 typedef can duplicate a field name. */
7672 if (flag_plan9_extensions
7673 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7675 tree xt, xn, yt, yn;
7677 xt = TREE_TYPE (x);
7678 if (DECL_NAME (x) != NULL_TREE)
7679 xn = DECL_NAME (x);
7680 else if (RECORD_OR_UNION_TYPE_P (xt)
7681 && TYPE_NAME (xt) != NULL_TREE
7682 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7683 xn = DECL_NAME (TYPE_NAME (xt));
7684 else
7685 xn = NULL_TREE;
7687 yt = TREE_TYPE (y);
7688 if (DECL_NAME (y) != NULL_TREE)
7689 yn = DECL_NAME (y);
7690 else if (RECORD_OR_UNION_TYPE_P (yt)
7691 && TYPE_NAME (yt) != NULL_TREE
7692 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7693 yn = DECL_NAME (TYPE_NAME (yt));
7694 else
7695 yn = NULL_TREE;
7697 if (xn != NULL_TREE && xn == yn)
7698 return true;
7701 return false;
7704 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7705 to HTAB, giving errors for any duplicates. */
7707 static void
7708 detect_field_duplicates_hash (tree fieldlist,
7709 hash_table<nofree_ptr_hash <tree_node> > *htab)
7711 tree x, y;
7712 tree_node **slot;
7714 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7715 if ((y = DECL_NAME (x)) != NULL_TREE)
7717 slot = htab->find_slot (y, INSERT);
7718 if (*slot)
7720 error ("duplicate member %q+D", x);
7721 DECL_NAME (x) = NULL_TREE;
7723 *slot = y;
7725 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7727 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7729 /* When using -fplan9-extensions, an anonymous field whose
7730 name is a typedef can duplicate a field name. */
7731 if (flag_plan9_extensions
7732 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7733 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7735 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7736 slot = htab->find_slot (xn, INSERT);
7737 if (*slot)
7738 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7739 *slot = xn;
7744 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7745 the list such that this does not present a problem later. */
7747 static void
7748 detect_field_duplicates (tree fieldlist)
7750 tree x, y;
7751 int timeout = 10;
7753 /* If the struct is the list of instance variables of an Objective-C
7754 class, then we need to check all the instance variables of
7755 superclasses when checking for duplicates (since you can't have
7756 an instance variable in a subclass with the same name as an
7757 instance variable in a superclass). We pass on this job to the
7758 Objective-C compiler. objc_detect_field_duplicates() will return
7759 false if we are not checking the list of instance variables and
7760 the C frontend should proceed with the standard field duplicate
7761 checks. If we are checking the list of instance variables, the
7762 ObjC frontend will do the check, emit the errors if needed, and
7763 then return true. */
7764 if (c_dialect_objc ())
7765 if (objc_detect_field_duplicates (false))
7766 return;
7768 /* First, see if there are more than "a few" fields.
7769 This is trivially true if there are zero or one fields. */
7770 if (!fieldlist || !DECL_CHAIN (fieldlist))
7771 return;
7772 x = fieldlist;
7773 do {
7774 timeout--;
7775 if (DECL_NAME (x) == NULL_TREE
7776 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7777 timeout = 0;
7778 x = DECL_CHAIN (x);
7779 } while (timeout > 0 && x);
7781 /* If there were "few" fields and no anonymous structures or unions,
7782 avoid the overhead of allocating a hash table. Instead just do
7783 the nested traversal thing. */
7784 if (timeout > 0)
7786 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7787 /* When using -fplan9-extensions, we can have duplicates
7788 between typedef names and fields. */
7789 if (DECL_NAME (x)
7790 || (flag_plan9_extensions
7791 && DECL_NAME (x) == NULL_TREE
7792 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7793 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7794 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7796 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7797 if (is_duplicate_field (y, x))
7799 error ("duplicate member %q+D", x);
7800 DECL_NAME (x) = NULL_TREE;
7804 else
7806 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7807 detect_field_duplicates_hash (fieldlist, &htab);
7811 /* Finish up struct info used by -Wc++-compat. */
7813 static void
7814 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7815 location_t record_loc)
7817 unsigned int ix;
7818 tree x;
7819 struct c_binding *b;
7821 if (fieldlist == NULL_TREE)
7823 if (code == RECORD_TYPE)
7824 warning_at (record_loc, OPT_Wc___compat,
7825 "empty struct has size 0 in C, size 1 in C++");
7826 else
7827 warning_at (record_loc, OPT_Wc___compat,
7828 "empty union has size 0 in C, size 1 in C++");
7831 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7832 the current struct. We do this now at the end of the struct
7833 because the flag is used to issue visibility warnings, and we
7834 only want to issue those warnings if the type is referenced
7835 outside of the struct declaration. */
7836 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7837 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7839 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7840 typedefs used when declaring fields in this struct. If the name
7841 of any of the fields is also a typedef name then the struct would
7842 not parse in C++, because the C++ lookup rules say that the
7843 typedef name would be looked up in the context of the struct, and
7844 would thus be the field rather than the typedef. */
7845 if (!struct_parse_info->typedefs_seen.is_empty ()
7846 && fieldlist != NULL_TREE)
7848 /* Use a hash_set<tree> using the name of the typedef. We can use
7849 a hash_set<tree> because identifiers are interned. */
7850 hash_set<tree> tset;
7852 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7853 tset.add (DECL_NAME (x));
7855 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7857 if (DECL_NAME (x) != NULL_TREE
7858 && tset.contains (DECL_NAME (x)))
7860 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7861 ("using %qD as both field and typedef name is "
7862 "invalid in C++"),
7864 /* FIXME: It would be nice to report the location where
7865 the typedef name is used. */
7870 /* For each field which has a binding and which was not defined in
7871 an enclosing struct, clear the in_struct field. */
7872 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7873 b->in_struct = 0;
7876 /* Function to help qsort sort FIELD_DECLs by name order. */
7878 static int
7879 field_decl_cmp (const void *x_p, const void *y_p)
7881 const tree *const x = (const tree *) x_p;
7882 const tree *const y = (const tree *) y_p;
7884 if (DECL_NAME (*x) == DECL_NAME (*y))
7885 /* A nontype is "greater" than a type. */
7886 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7887 if (DECL_NAME (*x) == NULL_TREE)
7888 return -1;
7889 if (DECL_NAME (*y) == NULL_TREE)
7890 return 1;
7891 if (DECL_NAME (*x) < DECL_NAME (*y))
7892 return -1;
7893 return 1;
7896 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7897 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7898 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7899 ATTRIBUTES are attributes to be applied to the structure.
7901 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7902 the struct was started. */
7904 tree
7905 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7906 struct c_struct_parse_info *enclosing_struct_parse_info)
7908 tree x;
7909 bool toplevel = file_scope == current_scope;
7911 /* If this type was previously laid out as a forward reference,
7912 make sure we lay it out again. */
7914 TYPE_SIZE (t) = NULL_TREE;
7916 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7918 if (pedantic)
7920 for (x = fieldlist; x; x = DECL_CHAIN (x))
7922 if (DECL_NAME (x) != NULL_TREE)
7923 break;
7924 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7925 break;
7928 if (x == NULL_TREE)
7930 if (TREE_CODE (t) == UNION_TYPE)
7932 if (fieldlist)
7933 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7934 else
7935 pedwarn (loc, OPT_Wpedantic, "union has no members");
7937 else
7939 if (fieldlist)
7940 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7941 else
7942 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7947 /* Install struct as DECL_CONTEXT of each field decl.
7948 Also process specified field sizes, found in the DECL_INITIAL,
7949 storing 0 there after the type has been changed to precision equal
7950 to its width, rather than the precision of the specified standard
7951 type. (Correct layout requires the original type to have been preserved
7952 until now.) */
7954 bool saw_named_field = false;
7955 for (x = fieldlist; x; x = DECL_CHAIN (x))
7957 if (TREE_TYPE (x) == error_mark_node)
7958 continue;
7960 DECL_CONTEXT (x) = t;
7962 /* If any field is const, the structure type is pseudo-const. */
7963 if (TREE_READONLY (x))
7964 C_TYPE_FIELDS_READONLY (t) = 1;
7965 else
7967 /* A field that is pseudo-const makes the structure likewise. */
7968 tree t1 = strip_array_types (TREE_TYPE (x));
7969 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7970 C_TYPE_FIELDS_READONLY (t) = 1;
7973 /* Any field that is volatile means variables of this type must be
7974 treated in some ways as volatile. */
7975 if (TREE_THIS_VOLATILE (x))
7976 C_TYPE_FIELDS_VOLATILE (t) = 1;
7978 /* Any field of nominal variable size implies structure is too. */
7979 if (C_DECL_VARIABLE_SIZE (x))
7980 C_TYPE_VARIABLE_SIZE (t) = 1;
7982 if (DECL_C_BIT_FIELD (x))
7984 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7985 DECL_SIZE (x) = bitsize_int (width);
7986 DECL_BIT_FIELD (x) = 1;
7989 if (TYPE_PACKED (t)
7990 && (DECL_BIT_FIELD (x)
7991 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7992 DECL_PACKED (x) = 1;
7994 /* Detect flexible array member in an invalid context. */
7995 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7996 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7997 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7998 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8000 if (TREE_CODE (t) == UNION_TYPE)
8002 error_at (DECL_SOURCE_LOCATION (x),
8003 "flexible array member in union");
8004 TREE_TYPE (x) = error_mark_node;
8006 else if (DECL_CHAIN (x) != NULL_TREE)
8008 error_at (DECL_SOURCE_LOCATION (x),
8009 "flexible array member not at end of struct");
8010 TREE_TYPE (x) = error_mark_node;
8012 else if (!saw_named_field)
8014 error_at (DECL_SOURCE_LOCATION (x),
8015 "flexible array member in a struct with no named "
8016 "members");
8017 TREE_TYPE (x) = error_mark_node;
8021 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8022 && flexible_array_type_p (TREE_TYPE (x)))
8023 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8024 "invalid use of structure with flexible array member");
8026 if (DECL_NAME (x)
8027 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8028 saw_named_field = true;
8031 detect_field_duplicates (fieldlist);
8033 /* Now we have the nearly final fieldlist. Record it,
8034 then lay out the structure or union (including the fields). */
8036 TYPE_FIELDS (t) = fieldlist;
8038 maybe_apply_pragma_scalar_storage_order (t);
8040 layout_type (t);
8042 if (TYPE_SIZE_UNIT (t)
8043 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8044 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8045 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8046 error ("type %qT is too large", t);
8048 /* Give bit-fields their proper types and rewrite the type of array fields
8049 with scalar component if the enclosing type has reverse storage order. */
8050 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8052 if (TREE_CODE (field) == FIELD_DECL
8053 && DECL_INITIAL (field)
8054 && TREE_TYPE (field) != error_mark_node)
8056 unsigned HOST_WIDE_INT width
8057 = tree_to_uhwi (DECL_INITIAL (field));
8058 tree type = TREE_TYPE (field);
8059 if (width != TYPE_PRECISION (type))
8061 TREE_TYPE (field)
8062 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8063 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8065 DECL_INITIAL (field) = NULL_TREE;
8067 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8068 && TREE_CODE (field) == FIELD_DECL
8069 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8071 tree ftype = TREE_TYPE (field);
8072 tree ctype = strip_array_types (ftype);
8073 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8075 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8076 tree *typep = &fmain_type;
8077 do {
8078 *typep = build_distinct_type_copy (*typep);
8079 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8080 typep = &TREE_TYPE (*typep);
8081 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8082 TREE_TYPE (field)
8083 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8088 /* Now we have the truly final field list.
8089 Store it in this type and in the variants. */
8091 TYPE_FIELDS (t) = fieldlist;
8093 /* If there are lots of fields, sort so we can look through them fast.
8094 We arbitrarily consider 16 or more elts to be "a lot". */
8097 int len = 0;
8099 for (x = fieldlist; x; x = DECL_CHAIN (x))
8101 if (len > 15 || DECL_NAME (x) == NULL)
8102 break;
8103 len += 1;
8106 if (len > 15)
8108 tree *field_array;
8109 struct lang_type *space;
8110 struct sorted_fields_type *space2;
8112 len += list_length (x);
8114 /* Use the same allocation policy here that make_node uses, to
8115 ensure that this lives as long as the rest of the struct decl.
8116 All decls in an inline function need to be saved. */
8118 space = ggc_cleared_alloc<struct lang_type> ();
8119 space2 = (sorted_fields_type *) ggc_internal_alloc
8120 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8122 len = 0;
8123 space->s = space2;
8124 field_array = &space2->elts[0];
8125 for (x = fieldlist; x; x = DECL_CHAIN (x))
8127 field_array[len++] = x;
8129 /* If there is anonymous struct or union, break out of the loop. */
8130 if (DECL_NAME (x) == NULL)
8131 break;
8133 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8134 if (x == NULL)
8136 TYPE_LANG_SPECIFIC (t) = space;
8137 TYPE_LANG_SPECIFIC (t)->s->len = len;
8138 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8139 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8144 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8145 in dwarf2out via rest_of_decl_compilation below and means
8146 something totally different. Since we will be clearing
8147 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8148 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8149 C_TYPE_INCOMPLETE_VARS should use some language specific
8150 node. */
8151 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8152 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8154 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8155 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8156 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8157 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8158 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8159 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8162 /* If this was supposed to be a transparent union, but we can't
8163 make it one, warn and turn off the flag. */
8164 if (TREE_CODE (t) == UNION_TYPE
8165 && TYPE_TRANSPARENT_AGGR (t)
8166 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8168 TYPE_TRANSPARENT_AGGR (t) = 0;
8169 warning_at (loc, 0, "union cannot be made transparent");
8172 /* If this structure or union completes the type of any previous
8173 variable declaration, lay it out and output its rtl. */
8174 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8176 tree decl = TREE_VALUE (x);
8177 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8178 layout_array_type (TREE_TYPE (decl));
8179 if (TREE_CODE (decl) != TYPE_DECL)
8181 layout_decl (decl, 0);
8182 if (c_dialect_objc ())
8183 objc_check_decl (decl);
8184 rest_of_decl_compilation (decl, toplevel, 0);
8188 /* Update type location to the one of the definition, instead of e.g.
8189 a forward declaration. */
8190 if (TYPE_STUB_DECL (t))
8191 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8193 /* Finish debugging output for this type. */
8194 rest_of_type_compilation (t, toplevel);
8196 /* If we're inside a function proper, i.e. not file-scope and not still
8197 parsing parameters, then arrange for the size of a variable sized type
8198 to be bound now. */
8199 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8200 add_stmt (build_stmt (loc,
8201 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8203 if (warn_cxx_compat)
8204 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8206 delete struct_parse_info;
8208 struct_parse_info = enclosing_struct_parse_info;
8210 /* If this struct is defined inside a struct, add it to
8211 struct_types. */
8212 if (warn_cxx_compat
8213 && struct_parse_info != NULL
8214 && !in_sizeof && !in_typeof && !in_alignof)
8215 struct_parse_info->struct_types.safe_push (t);
8217 return t;
8220 static struct {
8221 gt_pointer_operator new_value;
8222 void *cookie;
8223 } resort_data;
8225 /* This routine compares two fields like field_decl_cmp but using the
8226 pointer operator in resort_data. */
8228 static int
8229 resort_field_decl_cmp (const void *x_p, const void *y_p)
8231 const tree *const x = (const tree *) x_p;
8232 const tree *const y = (const tree *) y_p;
8234 if (DECL_NAME (*x) == DECL_NAME (*y))
8235 /* A nontype is "greater" than a type. */
8236 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8237 if (DECL_NAME (*x) == NULL_TREE)
8238 return -1;
8239 if (DECL_NAME (*y) == NULL_TREE)
8240 return 1;
8242 tree d1 = DECL_NAME (*x);
8243 tree d2 = DECL_NAME (*y);
8244 resort_data.new_value (&d1, resort_data.cookie);
8245 resort_data.new_value (&d2, resort_data.cookie);
8246 if (d1 < d2)
8247 return -1;
8249 return 1;
8252 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8254 void
8255 resort_sorted_fields (void *obj,
8256 void * ARG_UNUSED (orig_obj),
8257 gt_pointer_operator new_value,
8258 void *cookie)
8260 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8261 resort_data.new_value = new_value;
8262 resort_data.cookie = cookie;
8263 qsort (&sf->elts[0], sf->len, sizeof (tree),
8264 resort_field_decl_cmp);
8267 /* Lay out the type T, and its element type, and so on. */
8269 static void
8270 layout_array_type (tree t)
8272 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8273 layout_array_type (TREE_TYPE (t));
8274 layout_type (t);
8277 /* Begin compiling the definition of an enumeration type.
8278 NAME is its name (or null if anonymous).
8279 LOC is the enum's location.
8280 Returns the type object, as yet incomplete.
8281 Also records info about it so that build_enumerator
8282 may be used to declare the individual values as they are read. */
8284 tree
8285 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8287 tree enumtype = NULL_TREE;
8288 location_t enumloc = UNKNOWN_LOCATION;
8290 /* If this is the real definition for a previous forward reference,
8291 fill in the contents in the same object that used to be the
8292 forward reference. */
8294 if (name != NULL_TREE)
8295 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8297 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8299 enumtype = make_node (ENUMERAL_TYPE);
8300 pushtag (loc, name, enumtype);
8302 /* Update type location to the one of the definition, instead of e.g.
8303 a forward declaration. */
8304 else if (TYPE_STUB_DECL (enumtype))
8306 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8307 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8310 if (C_TYPE_BEING_DEFINED (enumtype))
8311 error_at (loc, "nested redefinition of %<enum %E%>", name);
8313 C_TYPE_BEING_DEFINED (enumtype) = 1;
8315 if (TYPE_VALUES (enumtype) != NULL_TREE)
8317 /* This enum is a named one that has been declared already. */
8318 error_at (loc, "redeclaration of %<enum %E%>", name);
8319 if (enumloc != UNKNOWN_LOCATION)
8320 inform (enumloc, "originally defined here");
8322 /* Completely replace its old definition.
8323 The old enumerators remain defined, however. */
8324 TYPE_VALUES (enumtype) = NULL_TREE;
8327 the_enum->enum_next_value = integer_zero_node;
8328 the_enum->enum_overflow = 0;
8330 if (flag_short_enums)
8331 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8332 TYPE_PACKED (v) = 1;
8334 /* FIXME: This will issue a warning for a use of a type defined
8335 within sizeof in a statement expr. This is not terribly serious
8336 as C++ doesn't permit statement exprs within sizeof anyhow. */
8337 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8338 warning_at (loc, OPT_Wc___compat,
8339 "defining type in %qs expression is invalid in C++",
8340 (in_sizeof
8341 ? "sizeof"
8342 : (in_typeof ? "typeof" : "alignof")));
8344 return enumtype;
8347 /* After processing and defining all the values of an enumeration type,
8348 install their decls in the enumeration type and finish it off.
8349 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8350 and ATTRIBUTES are the specified attributes.
8351 Returns ENUMTYPE. */
8353 tree
8354 finish_enum (tree enumtype, tree values, tree attributes)
8356 tree pair, tem;
8357 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8358 int precision;
8359 signop sign;
8360 bool toplevel = (file_scope == current_scope);
8361 struct lang_type *lt;
8363 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8365 /* Calculate the maximum value of any enumerator in this type. */
8367 if (values == error_mark_node)
8368 minnode = maxnode = integer_zero_node;
8369 else
8371 minnode = maxnode = TREE_VALUE (values);
8372 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8374 tree value = TREE_VALUE (pair);
8375 if (tree_int_cst_lt (maxnode, value))
8376 maxnode = value;
8377 if (tree_int_cst_lt (value, minnode))
8378 minnode = value;
8382 /* Construct the final type of this enumeration. It is the same
8383 as one of the integral types - the narrowest one that fits, except
8384 that normally we only go as narrow as int - and signed iff any of
8385 the values are negative. */
8386 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8387 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8388 tree_int_cst_min_precision (maxnode, sign));
8390 /* If the precision of the type was specified with an attribute and it
8391 was too small, give an error. Otherwise, use it. */
8392 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8394 if (precision > TYPE_PRECISION (enumtype))
8396 TYPE_PRECISION (enumtype) = 0;
8397 error ("specified mode too small for enumeral values");
8399 else
8400 precision = TYPE_PRECISION (enumtype);
8402 else
8403 TYPE_PRECISION (enumtype) = 0;
8405 if (TYPE_PACKED (enumtype)
8406 || precision > TYPE_PRECISION (integer_type_node)
8407 || TYPE_PRECISION (enumtype))
8409 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8410 if (tem == NULL)
8412 warning (0, "enumeration values exceed range of largest integer");
8413 tem = long_long_integer_type_node;
8416 else
8417 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8419 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8420 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8421 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8422 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8423 TYPE_SIZE (enumtype) = NULL_TREE;
8424 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8426 layout_type (enumtype);
8428 if (values != error_mark_node)
8430 /* Change the type of the enumerators to be the enum type. We
8431 need to do this irrespective of the size of the enum, for
8432 proper type checking. Replace the DECL_INITIALs of the
8433 enumerators, and the value slots of the list, with copies
8434 that have the enum type; they cannot be modified in place
8435 because they may be shared (e.g. integer_zero_node) Finally,
8436 change the purpose slots to point to the names of the decls. */
8437 for (pair = values; pair; pair = TREE_CHAIN (pair))
8439 tree enu = TREE_PURPOSE (pair);
8440 tree ini = DECL_INITIAL (enu);
8442 TREE_TYPE (enu) = enumtype;
8444 /* The ISO C Standard mandates enumerators to have type int,
8445 even though the underlying type of an enum type is
8446 unspecified. However, GCC allows enumerators of any
8447 integer type as an extensions. build_enumerator()
8448 converts any enumerators that fit in an int to type int,
8449 to avoid promotions to unsigned types when comparing
8450 integers with enumerators that fit in the int range.
8451 When -pedantic is given, build_enumerator() would have
8452 already warned about those that don't fit. Here we
8453 convert the rest to the enumerator type. */
8454 if (TREE_TYPE (ini) != integer_type_node)
8455 ini = convert (enumtype, ini);
8457 DECL_INITIAL (enu) = ini;
8458 TREE_PURPOSE (pair) = DECL_NAME (enu);
8459 TREE_VALUE (pair) = ini;
8462 TYPE_VALUES (enumtype) = values;
8465 /* Record the min/max values so that we can warn about bit-field
8466 enumerations that are too small for the values. */
8467 lt = ggc_cleared_alloc<struct lang_type> ();
8468 lt->enum_min = minnode;
8469 lt->enum_max = maxnode;
8470 TYPE_LANG_SPECIFIC (enumtype) = lt;
8472 /* Fix up all variant types of this enum type. */
8473 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8475 if (tem == enumtype)
8476 continue;
8477 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8478 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8479 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8480 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8481 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8482 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8483 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8484 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8485 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8486 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8487 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8490 /* Finish debugging output for this type. */
8491 rest_of_type_compilation (enumtype, toplevel);
8493 /* If this enum is defined inside a struct, add it to
8494 struct_types. */
8495 if (warn_cxx_compat
8496 && struct_parse_info != NULL
8497 && !in_sizeof && !in_typeof && !in_alignof)
8498 struct_parse_info->struct_types.safe_push (enumtype);
8500 return enumtype;
8503 /* Build and install a CONST_DECL for one value of the
8504 current enumeration type (one that was begun with start_enum).
8505 DECL_LOC is the location of the enumerator.
8506 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8507 Return a tree-list containing the CONST_DECL and its value.
8508 Assignment of sequential values by default is handled here. */
8510 tree
8511 build_enumerator (location_t decl_loc, location_t loc,
8512 struct c_enum_contents *the_enum, tree name, tree value)
8514 tree decl, type;
8516 /* Validate and default VALUE. */
8518 if (value != NULL_TREE)
8520 /* Don't issue more errors for error_mark_node (i.e. an
8521 undeclared identifier) - just ignore the value expression. */
8522 if (value == error_mark_node)
8523 value = NULL_TREE;
8524 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8526 error_at (loc, "enumerator value for %qE is not an integer constant",
8527 name);
8528 value = NULL_TREE;
8530 else
8532 if (TREE_CODE (value) != INTEGER_CST)
8534 value = c_fully_fold (value, false, NULL);
8535 if (TREE_CODE (value) == INTEGER_CST)
8536 pedwarn (loc, OPT_Wpedantic,
8537 "enumerator value for %qE is not an integer "
8538 "constant expression", name);
8540 if (TREE_CODE (value) != INTEGER_CST)
8542 error ("enumerator value for %qE is not an integer constant",
8543 name);
8544 value = NULL_TREE;
8546 else
8548 value = default_conversion (value);
8549 constant_expression_warning (value);
8554 /* Default based on previous value. */
8555 /* It should no longer be possible to have NON_LVALUE_EXPR
8556 in the default. */
8557 if (value == NULL_TREE)
8559 value = the_enum->enum_next_value;
8560 if (the_enum->enum_overflow)
8561 error_at (loc, "overflow in enumeration values");
8563 /* Even though the underlying type of an enum is unspecified, the
8564 type of enumeration constants is explicitly defined as int
8565 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8566 an extension. */
8567 else if (!int_fits_type_p (value, integer_type_node))
8568 pedwarn (loc, OPT_Wpedantic,
8569 "ISO C restricts enumerator values to range of %<int%>");
8571 /* The ISO C Standard mandates enumerators to have type int, even
8572 though the underlying type of an enum type is unspecified.
8573 However, GCC allows enumerators of any integer type as an
8574 extensions. Here we convert any enumerators that fit in an int
8575 to type int, to avoid promotions to unsigned types when comparing
8576 integers with enumerators that fit in the int range. When
8577 -pedantic is given, we would have already warned about those that
8578 don't fit. We have to do this here rather than in finish_enum
8579 because this value may be used to define more enumerators. */
8580 if (int_fits_type_p (value, integer_type_node))
8581 value = convert (integer_type_node, value);
8583 /* Set basis for default for next value. */
8584 the_enum->enum_next_value
8585 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8586 PLUS_EXPR, value, integer_one_node, false);
8587 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8589 /* Now create a declaration for the enum value name. */
8591 type = TREE_TYPE (value);
8592 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8593 TYPE_PRECISION (integer_type_node)),
8594 (TYPE_PRECISION (type)
8595 >= TYPE_PRECISION (integer_type_node)
8596 && TYPE_UNSIGNED (type)));
8598 decl = build_decl (decl_loc, CONST_DECL, name, type);
8599 DECL_INITIAL (decl) = convert (type, value);
8600 pushdecl (decl);
8602 return tree_cons (decl, value, NULL_TREE);
8606 /* Create the FUNCTION_DECL for a function definition.
8607 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8608 the declaration; they describe the function's name and the type it returns,
8609 but twisted together in a fashion that parallels the syntax of C.
8611 This function creates a binding context for the function body
8612 as well as setting up the FUNCTION_DECL in current_function_decl.
8614 Returns true on success. If the DECLARATOR is not suitable for a function
8615 (it defines a datum instead), we return false to report a parse error. */
8617 bool
8618 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8619 tree attributes)
8621 tree decl1, old_decl;
8622 tree restype, resdecl;
8623 location_t loc;
8625 current_function_returns_value = 0; /* Assume, until we see it does. */
8626 current_function_returns_null = 0;
8627 current_function_returns_abnormally = 0;
8628 warn_about_return_type = 0;
8629 c_switch_stack = NULL;
8631 /* Indicate no valid break/continue context by setting these variables
8632 to some non-null, non-label value. We'll notice and emit the proper
8633 error message in c_finish_bc_stmt. */
8634 c_break_label = c_cont_label = size_zero_node;
8636 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8637 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8638 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8640 /* If the declarator is not suitable for a function definition,
8641 cause a syntax error. */
8642 if (decl1 == NULL_TREE
8643 || TREE_CODE (decl1) != FUNCTION_DECL)
8644 return false;
8646 loc = DECL_SOURCE_LOCATION (decl1);
8648 c_decl_attributes (&decl1, attributes, 0);
8650 if (DECL_DECLARED_INLINE_P (decl1)
8651 && DECL_UNINLINABLE (decl1)
8652 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8653 warning_at (loc, OPT_Wattributes,
8654 "inline function %qD given attribute noinline",
8655 decl1);
8657 /* Handle gnu_inline attribute. */
8658 if (declspecs->inline_p
8659 && !flag_gnu89_inline
8660 && TREE_CODE (decl1) == FUNCTION_DECL
8661 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8662 || current_function_decl))
8664 if (declspecs->storage_class != csc_static)
8665 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8668 announce_function (decl1);
8670 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8672 error_at (loc, "return type is an incomplete type");
8673 /* Make it return void instead. */
8674 TREE_TYPE (decl1)
8675 = build_function_type (void_type_node,
8676 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8679 if (warn_about_return_type)
8680 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8681 : (warn_return_type ? OPT_Wreturn_type
8682 : OPT_Wimplicit_int),
8683 "return type defaults to %<int%>");
8685 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8686 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8687 DECL_INITIAL (decl1) = error_mark_node;
8689 /* A nested function is not global. */
8690 if (current_function_decl != NULL_TREE)
8691 TREE_PUBLIC (decl1) = 0;
8693 /* If this definition isn't a prototype and we had a prototype declaration
8694 before, copy the arg type info from that prototype. */
8695 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8696 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8697 old_decl = NULL_TREE;
8698 current_function_prototype_locus = UNKNOWN_LOCATION;
8699 current_function_prototype_built_in = false;
8700 current_function_prototype_arg_types = NULL_TREE;
8701 if (!prototype_p (TREE_TYPE (decl1)))
8703 if (old_decl != NULL_TREE
8704 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8705 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8706 TREE_TYPE (TREE_TYPE (old_decl))))
8708 if (stdarg_p (TREE_TYPE (old_decl)))
8710 warning_at (loc, 0, "%q+D defined as variadic function "
8711 "without prototype", decl1);
8712 locate_old_decl (old_decl);
8714 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8715 TREE_TYPE (decl1));
8716 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8717 current_function_prototype_built_in
8718 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8719 current_function_prototype_arg_types
8720 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8722 if (TREE_PUBLIC (decl1))
8724 /* If there is an external prototype declaration of this
8725 function, record its location but do not copy information
8726 to this decl. This may be an invisible declaration
8727 (built-in or in a scope which has finished) or simply
8728 have more refined argument types than any declaration
8729 found above. */
8730 struct c_binding *b;
8731 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8732 if (B_IN_SCOPE (b, external_scope))
8733 break;
8734 if (b)
8736 tree ext_decl, ext_type;
8737 ext_decl = b->decl;
8738 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8739 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8740 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8741 TREE_TYPE (ext_type)))
8743 current_function_prototype_locus
8744 = DECL_SOURCE_LOCATION (ext_decl);
8745 current_function_prototype_built_in
8746 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8747 current_function_prototype_arg_types
8748 = TYPE_ARG_TYPES (ext_type);
8754 /* Optionally warn of old-fashioned def with no previous prototype. */
8755 if (warn_strict_prototypes
8756 && old_decl != error_mark_node
8757 && !prototype_p (TREE_TYPE (decl1))
8758 && C_DECL_ISNT_PROTOTYPE (old_decl))
8759 warning_at (loc, OPT_Wstrict_prototypes,
8760 "function declaration isn%'t a prototype");
8761 /* Optionally warn of any global def with no previous prototype. */
8762 else if (warn_missing_prototypes
8763 && old_decl != error_mark_node
8764 && TREE_PUBLIC (decl1)
8765 && !MAIN_NAME_P (DECL_NAME (decl1))
8766 && C_DECL_ISNT_PROTOTYPE (old_decl)
8767 && !DECL_DECLARED_INLINE_P (decl1))
8768 warning_at (loc, OPT_Wmissing_prototypes,
8769 "no previous prototype for %qD", decl1);
8770 /* Optionally warn of any def with no previous prototype
8771 if the function has already been used. */
8772 else if (warn_missing_prototypes
8773 && old_decl != NULL_TREE
8774 && old_decl != error_mark_node
8775 && TREE_USED (old_decl)
8776 && !prototype_p (TREE_TYPE (old_decl)))
8777 warning_at (loc, OPT_Wmissing_prototypes,
8778 "%qD was used with no prototype before its definition", decl1);
8779 /* Optionally warn of any global def with no previous declaration. */
8780 else if (warn_missing_declarations
8781 && TREE_PUBLIC (decl1)
8782 && old_decl == NULL_TREE
8783 && !MAIN_NAME_P (DECL_NAME (decl1))
8784 && !DECL_DECLARED_INLINE_P (decl1))
8785 warning_at (loc, OPT_Wmissing_declarations,
8786 "no previous declaration for %qD",
8787 decl1);
8788 /* Optionally warn of any def with no previous declaration
8789 if the function has already been used. */
8790 else if (warn_missing_declarations
8791 && old_decl != NULL_TREE
8792 && old_decl != error_mark_node
8793 && TREE_USED (old_decl)
8794 && C_DECL_IMPLICIT (old_decl))
8795 warning_at (loc, OPT_Wmissing_declarations,
8796 "%qD was used with no declaration before its definition", decl1);
8798 /* This function exists in static storage.
8799 (This does not mean `static' in the C sense!) */
8800 TREE_STATIC (decl1) = 1;
8802 /* This is the earliest point at which we might know the assembler
8803 name of the function. Thus, if it's set before this, die horribly. */
8804 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8806 /* If #pragma weak was used, mark the decl weak now. */
8807 if (current_scope == file_scope)
8808 maybe_apply_pragma_weak (decl1);
8810 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8811 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8813 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8814 != integer_type_node)
8815 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8816 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8817 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8818 decl1);
8820 check_main_parameter_types (decl1);
8822 if (!TREE_PUBLIC (decl1))
8823 pedwarn (loc, OPT_Wmain,
8824 "%qD is normally a non-static function", decl1);
8827 /* Record the decl so that the function name is defined.
8828 If we already have a decl for this name, and it is a FUNCTION_DECL,
8829 use the old decl. */
8831 current_function_decl = pushdecl (decl1);
8833 push_scope ();
8834 declare_parm_level ();
8836 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8837 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8838 DECL_ARTIFICIAL (resdecl) = 1;
8839 DECL_IGNORED_P (resdecl) = 1;
8840 DECL_RESULT (current_function_decl) = resdecl;
8842 start_fname_decls ();
8844 return true;
8847 /* Subroutine of store_parm_decls which handles new-style function
8848 definitions (prototype format). The parms already have decls, so we
8849 need only record them as in effect and complain if any redundant
8850 old-style parm decls were written. */
8851 static void
8852 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8854 tree decl;
8855 c_arg_tag *tag;
8856 unsigned ix;
8858 if (current_scope->bindings)
8860 error_at (DECL_SOURCE_LOCATION (fndecl),
8861 "old-style parameter declarations in prototyped "
8862 "function definition");
8864 /* Get rid of the old-style declarations. */
8865 pop_scope ();
8866 push_scope ();
8868 /* Don't issue this warning for nested functions, and don't issue this
8869 warning if we got here because ARG_INFO_TYPES was error_mark_node
8870 (this happens when a function definition has just an ellipsis in
8871 its parameter list). */
8872 else if (!in_system_header_at (input_location)
8873 && !current_function_scope
8874 && arg_info->types != error_mark_node)
8875 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8876 "traditional C rejects ISO C style function definitions");
8878 /* Now make all the parameter declarations visible in the function body.
8879 We can bypass most of the grunt work of pushdecl. */
8880 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8882 DECL_CONTEXT (decl) = current_function_decl;
8883 if (DECL_NAME (decl))
8885 bind (DECL_NAME (decl), decl, current_scope,
8886 /*invisible=*/false, /*nested=*/false,
8887 UNKNOWN_LOCATION);
8888 if (!TREE_USED (decl))
8889 warn_if_shadowing (decl);
8891 else
8892 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8895 /* Record the parameter list in the function declaration. */
8896 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8898 /* Now make all the ancillary declarations visible, likewise. */
8899 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8901 DECL_CONTEXT (decl) = current_function_decl;
8902 if (DECL_NAME (decl))
8903 bind (DECL_NAME (decl), decl, current_scope,
8904 /*invisible=*/false,
8905 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8906 UNKNOWN_LOCATION);
8909 /* And all the tag declarations. */
8910 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8911 if (tag->id)
8912 bind (tag->id, tag->type, current_scope,
8913 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8916 /* Subroutine of store_parm_decls which handles old-style function
8917 definitions (separate parameter list and declarations). */
8919 static void
8920 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8922 struct c_binding *b;
8923 tree parm, decl, last;
8924 tree parmids = arg_info->parms;
8925 hash_set<tree> seen_args;
8927 if (!in_system_header_at (input_location))
8928 warning_at (DECL_SOURCE_LOCATION (fndecl),
8929 OPT_Wold_style_definition, "old-style function definition");
8931 /* Match each formal parameter name with its declaration. Save each
8932 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8933 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8935 if (TREE_VALUE (parm) == NULL_TREE)
8937 error_at (DECL_SOURCE_LOCATION (fndecl),
8938 "parameter name missing from parameter list");
8939 TREE_PURPOSE (parm) = NULL_TREE;
8940 continue;
8943 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8944 if (b && B_IN_CURRENT_SCOPE (b))
8946 decl = b->decl;
8947 /* Skip erroneous parameters. */
8948 if (decl == error_mark_node)
8949 continue;
8950 /* If we got something other than a PARM_DECL it is an error. */
8951 if (TREE_CODE (decl) != PARM_DECL)
8953 error_at (DECL_SOURCE_LOCATION (decl),
8954 "%qD declared as a non-parameter", decl);
8955 continue;
8957 /* If the declaration is already marked, we have a duplicate
8958 name. Complain and ignore the duplicate. */
8959 else if (seen_args.contains (decl))
8961 error_at (DECL_SOURCE_LOCATION (decl),
8962 "multiple parameters named %qD", decl);
8963 TREE_PURPOSE (parm) = NULL_TREE;
8964 continue;
8966 /* If the declaration says "void", complain and turn it into
8967 an int. */
8968 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8970 error_at (DECL_SOURCE_LOCATION (decl),
8971 "parameter %qD declared with void type", decl);
8972 TREE_TYPE (decl) = integer_type_node;
8973 DECL_ARG_TYPE (decl) = integer_type_node;
8974 layout_decl (decl, 0);
8976 warn_if_shadowing (decl);
8978 /* If no declaration found, default to int. */
8979 else
8981 /* FIXME diagnostics: This should be the location of the argument,
8982 not the FNDECL. E.g., for an old-style declaration
8984 int f10(v) { blah; }
8986 We should use the location of the V, not the F10.
8987 Unfortunately, the V is an IDENTIFIER_NODE which has no
8988 location. In the future we need locations for c_arg_info
8989 entries.
8991 See gcc.dg/Wshadow-3.c for an example of this problem. */
8992 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8993 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8994 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8995 pushdecl (decl);
8996 warn_if_shadowing (decl);
8998 if (flag_isoc99)
8999 pedwarn (DECL_SOURCE_LOCATION (decl),
9000 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9001 decl);
9002 else
9003 warning_at (DECL_SOURCE_LOCATION (decl),
9004 OPT_Wmissing_parameter_type,
9005 "type of %qD defaults to %<int%>", decl);
9008 TREE_PURPOSE (parm) = decl;
9009 seen_args.add (decl);
9012 /* Now examine the parms chain for incomplete declarations
9013 and declarations with no corresponding names. */
9015 for (b = current_scope->bindings; b; b = b->prev)
9017 parm = b->decl;
9018 if (TREE_CODE (parm) != PARM_DECL)
9019 continue;
9021 if (TREE_TYPE (parm) != error_mark_node
9022 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9024 error_at (DECL_SOURCE_LOCATION (parm),
9025 "parameter %qD has incomplete type", parm);
9026 TREE_TYPE (parm) = error_mark_node;
9029 if (!seen_args.contains (parm))
9031 error_at (DECL_SOURCE_LOCATION (parm),
9032 "declaration for parameter %qD but no such parameter",
9033 parm);
9035 /* Pretend the parameter was not missing.
9036 This gets us to a standard state and minimizes
9037 further error messages. */
9038 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9042 /* Chain the declarations together in the order of the list of
9043 names. Store that chain in the function decl, replacing the
9044 list of names. Update the current scope to match. */
9045 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9047 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9048 if (TREE_PURPOSE (parm))
9049 break;
9050 if (parm && TREE_PURPOSE (parm))
9052 last = TREE_PURPOSE (parm);
9053 DECL_ARGUMENTS (fndecl) = last;
9055 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9056 if (TREE_PURPOSE (parm))
9058 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9059 last = TREE_PURPOSE (parm);
9061 DECL_CHAIN (last) = NULL_TREE;
9064 /* If there was a previous prototype,
9065 set the DECL_ARG_TYPE of each argument according to
9066 the type previously specified, and report any mismatches. */
9068 if (current_function_prototype_arg_types)
9070 tree type;
9071 for (parm = DECL_ARGUMENTS (fndecl),
9072 type = current_function_prototype_arg_types;
9073 parm || (type != NULL_TREE
9074 && TREE_VALUE (type) != error_mark_node
9075 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9076 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9078 if (parm == NULL_TREE
9079 || type == NULL_TREE
9080 || (TREE_VALUE (type) != error_mark_node
9081 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9083 if (current_function_prototype_built_in)
9084 warning_at (DECL_SOURCE_LOCATION (fndecl),
9085 0, "number of arguments doesn%'t match "
9086 "built-in prototype");
9087 else
9089 /* FIXME diagnostics: This should be the location of
9090 FNDECL, but there is bug when a prototype is
9091 declared inside function context, but defined
9092 outside of it (e.g., gcc.dg/pr15698-2.c). In
9093 which case FNDECL gets the location of the
9094 prototype, not the definition. */
9095 error_at (input_location,
9096 "number of arguments doesn%'t match prototype");
9098 error_at (current_function_prototype_locus,
9099 "prototype declaration");
9101 break;
9103 /* Type for passing arg must be consistent with that
9104 declared for the arg. ISO C says we take the unqualified
9105 type for parameters declared with qualified type. */
9106 if (TREE_TYPE (parm) != error_mark_node
9107 && TREE_VALUE (type) != error_mark_node
9108 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9109 != TYPE_ATOMIC (TREE_VALUE (type)))
9110 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9111 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9113 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9114 == TYPE_ATOMIC (TREE_VALUE (type)))
9115 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9116 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9118 /* Adjust argument to match prototype. E.g. a previous
9119 `int foo(float);' prototype causes
9120 `int foo(x) float x; {...}' to be treated like
9121 `int foo(float x) {...}'. This is particularly
9122 useful for argument types like uid_t. */
9123 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9125 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9126 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9127 && (TYPE_PRECISION (TREE_TYPE (parm))
9128 < TYPE_PRECISION (integer_type_node)))
9129 DECL_ARG_TYPE (parm)
9130 = c_type_promotes_to (TREE_TYPE (parm));
9132 /* ??? Is it possible to get here with a
9133 built-in prototype or will it always have
9134 been diagnosed as conflicting with an
9135 old-style definition and discarded? */
9136 if (current_function_prototype_built_in)
9137 warning_at (DECL_SOURCE_LOCATION (parm),
9138 OPT_Wpedantic, "promoted argument %qD "
9139 "doesn%'t match built-in prototype", parm);
9140 else
9142 pedwarn (DECL_SOURCE_LOCATION (parm),
9143 OPT_Wpedantic, "promoted argument %qD "
9144 "doesn%'t match prototype", parm);
9145 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9146 "prototype declaration");
9149 else
9151 if (current_function_prototype_built_in)
9152 warning_at (DECL_SOURCE_LOCATION (parm),
9153 0, "argument %qD doesn%'t match "
9154 "built-in prototype", parm);
9155 else
9157 error_at (DECL_SOURCE_LOCATION (parm),
9158 "argument %qD doesn%'t match prototype", parm);
9159 error_at (current_function_prototype_locus,
9160 "prototype declaration");
9165 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9168 /* Otherwise, create a prototype that would match. */
9170 else
9172 tree actual = NULL_TREE, last = NULL_TREE, type;
9174 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9176 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9177 if (last)
9178 TREE_CHAIN (last) = type;
9179 else
9180 actual = type;
9181 last = type;
9183 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9184 if (last)
9185 TREE_CHAIN (last) = type;
9186 else
9187 actual = type;
9189 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9190 of the type of this function, but we need to avoid having this
9191 affect the types of other similarly-typed functions, so we must
9192 first force the generation of an identical (but separate) type
9193 node for the relevant function type. The new node we create
9194 will be a variant of the main variant of the original function
9195 type. */
9197 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9199 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9203 /* Store parameter declarations passed in ARG_INFO into the current
9204 function declaration. */
9206 void
9207 store_parm_decls_from (struct c_arg_info *arg_info)
9209 current_function_arg_info = arg_info;
9210 store_parm_decls ();
9213 /* Called by walk_tree to look for and update context-less labels. */
9215 static tree
9216 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9218 if (TREE_CODE (*tp) == LABEL_EXPR
9219 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9221 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9222 *walk_subtrees = 0;
9225 return NULL_TREE;
9228 /* Store the parameter declarations into the current function declaration.
9229 This is called after parsing the parameter declarations, before
9230 digesting the body of the function.
9232 For an old-style definition, construct a prototype out of the old-style
9233 parameter declarations and inject it into the function's type. */
9235 void
9236 store_parm_decls (void)
9238 tree fndecl = current_function_decl;
9239 bool proto;
9241 /* The argument information block for FNDECL. */
9242 struct c_arg_info *arg_info = current_function_arg_info;
9243 current_function_arg_info = 0;
9245 /* True if this definition is written with a prototype. Note:
9246 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9247 list in a function definition as equivalent to (void) -- an
9248 empty argument list specifies the function has no parameters,
9249 but only (void) sets up a prototype for future calls. */
9250 proto = arg_info->types != 0;
9252 if (proto)
9253 store_parm_decls_newstyle (fndecl, arg_info);
9254 else
9255 store_parm_decls_oldstyle (fndecl, arg_info);
9257 /* The next call to push_scope will be a function body. */
9259 next_is_function_body = true;
9261 /* Write a record describing this function definition to the prototypes
9262 file (if requested). */
9264 gen_aux_info_record (fndecl, 1, 0, proto);
9266 /* Initialize the RTL code for the function. */
9267 allocate_struct_function (fndecl, false);
9269 if (warn_unused_local_typedefs)
9270 cfun->language = ggc_cleared_alloc<language_function> ();
9272 /* Begin the statement tree for this function. */
9273 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9275 /* ??? Insert the contents of the pending sizes list into the function
9276 to be evaluated. The only reason left to have this is
9277 void foo(int n, int array[n++])
9278 because we throw away the array type in favor of a pointer type, and
9279 thus won't naturally see the SAVE_EXPR containing the increment. All
9280 other pending sizes would be handled by gimplify_parameters. */
9281 if (arg_info->pending_sizes)
9283 /* In very special circumstances, e.g. for code like
9284 _Atomic int i = 5;
9285 void f (int a[i += 2]) {}
9286 we need to execute the atomic assignment on function entry.
9287 But in this case, it is not just a straight store, it has the
9288 op= form, which means that build_atomic_assign has generated
9289 gotos, labels, etc. Because at that time the function decl
9290 for F has not been created yet, those labels do not have any
9291 function context. But we have the fndecl now, so update the
9292 labels accordingly. gimplify_expr would crash otherwise. */
9293 walk_tree_without_duplicates (&arg_info->pending_sizes,
9294 set_labels_context_r, fndecl);
9295 add_stmt (arg_info->pending_sizes);
9299 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9300 c_finish_omp_declare_simd for function prototypes. No diagnostics
9301 should be done. */
9303 void
9304 temp_store_parm_decls (tree fndecl, tree parms)
9306 push_scope ();
9307 for (tree p = parms; p; p = DECL_CHAIN (p))
9309 DECL_CONTEXT (p) = fndecl;
9310 if (DECL_NAME (p))
9311 bind (DECL_NAME (p), p, current_scope,
9312 /*invisible=*/false, /*nested=*/false,
9313 UNKNOWN_LOCATION);
9317 /* Undo what temp_store_parm_decls did. */
9319 void
9320 temp_pop_parm_decls (void)
9322 /* Clear all bindings in this temporary scope, so that
9323 pop_scope doesn't create a BLOCK. */
9324 struct c_binding *b = current_scope->bindings;
9325 current_scope->bindings = NULL;
9326 for (; b; b = free_binding_and_advance (b))
9328 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9329 || b->decl == error_mark_node);
9330 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9331 I_SYMBOL_BINDING (b->id) = b->shadowed;
9332 if (b->shadowed && b->shadowed->u.type)
9333 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9335 pop_scope ();
9339 /* Finish up a function declaration and compile that function
9340 all the way to assembler language output. Then free the storage
9341 for the function definition.
9343 This is called after parsing the body of the function definition. */
9345 void
9346 finish_function (void)
9348 tree fndecl = current_function_decl;
9350 if (c_dialect_objc ())
9351 objc_finish_function ();
9353 if (TREE_CODE (fndecl) == FUNCTION_DECL
9354 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9356 tree args = DECL_ARGUMENTS (fndecl);
9357 for (; args; args = DECL_CHAIN (args))
9359 tree type = TREE_TYPE (args);
9360 if (INTEGRAL_TYPE_P (type)
9361 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9362 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9366 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9367 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9369 /* Must mark the RESULT_DECL as being in this function. */
9371 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9372 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9374 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9375 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9376 == integer_type_node && flag_isoc99)
9378 /* Hack. We don't want the middle-end to warn that this return
9379 is unreachable, so we mark its location as special. Using
9380 UNKNOWN_LOCATION has the problem that it gets clobbered in
9381 annotate_one_with_locus. A cleaner solution might be to
9382 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9384 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9387 /* Tie off the statement tree for this function. */
9388 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9390 finish_fname_decls ();
9392 /* Complain if there's just no return statement. */
9393 if (warn_return_type
9394 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9395 && !current_function_returns_value && !current_function_returns_null
9396 /* Don't complain if we are no-return. */
9397 && !current_function_returns_abnormally
9398 /* Don't complain if we are declared noreturn. */
9399 && !TREE_THIS_VOLATILE (fndecl)
9400 /* Don't warn for main(). */
9401 && !MAIN_NAME_P (DECL_NAME (fndecl))
9402 /* Or if they didn't actually specify a return type. */
9403 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9404 /* Normally, with -Wreturn-type, flow will complain, but we might
9405 optimize out static functions. */
9406 && !TREE_PUBLIC (fndecl))
9408 warning (OPT_Wreturn_type,
9409 "no return statement in function returning non-void");
9410 TREE_NO_WARNING (fndecl) = 1;
9413 /* Complain about parameters that are only set, but never otherwise used. */
9414 if (warn_unused_but_set_parameter)
9416 tree decl;
9418 for (decl = DECL_ARGUMENTS (fndecl);
9419 decl;
9420 decl = DECL_CHAIN (decl))
9421 if (TREE_USED (decl)
9422 && TREE_CODE (decl) == PARM_DECL
9423 && !DECL_READ_P (decl)
9424 && DECL_NAME (decl)
9425 && !DECL_ARTIFICIAL (decl)
9426 && !TREE_NO_WARNING (decl))
9427 warning_at (DECL_SOURCE_LOCATION (decl),
9428 OPT_Wunused_but_set_parameter,
9429 "parameter %qD set but not used", decl);
9432 /* Complain about locally defined typedefs that are not used in this
9433 function. */
9434 maybe_warn_unused_local_typedefs ();
9436 /* Possibly warn about unused parameters. */
9437 if (warn_unused_parameter)
9438 do_warn_unused_parameter (fndecl);
9440 /* Store the end of the function, so that we get good line number
9441 info for the epilogue. */
9442 cfun->function_end_locus = input_location;
9444 /* Finalize the ELF visibility for the function. */
9445 c_determine_visibility (fndecl);
9447 /* For GNU C extern inline functions disregard inline limits. */
9448 if (DECL_EXTERNAL (fndecl)
9449 && DECL_DECLARED_INLINE_P (fndecl)
9450 && (flag_gnu89_inline
9451 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9452 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9454 /* Genericize before inlining. Delay genericizing nested functions
9455 until their parent function is genericized. Since finalizing
9456 requires GENERIC, delay that as well. */
9458 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9459 && !undef_nested_function)
9461 if (!decl_function_context (fndecl))
9463 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9464 c_genericize (fndecl);
9466 /* ??? Objc emits functions after finalizing the compilation unit.
9467 This should be cleaned up later and this conditional removed. */
9468 if (symtab->global_info_ready)
9470 cgraph_node::add_new_function (fndecl, false);
9471 return;
9473 cgraph_node::finalize_function (fndecl, false);
9475 else
9477 /* Register this function with cgraph just far enough to get it
9478 added to our parent's nested function list. Handy, since the
9479 C front end doesn't have such a list. */
9480 (void) cgraph_node::get_create (fndecl);
9484 if (!decl_function_context (fndecl))
9485 undef_nested_function = false;
9487 if (cfun->language != NULL)
9489 ggc_free (cfun->language);
9490 cfun->language = NULL;
9493 /* We're leaving the context of this function, so zap cfun.
9494 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9495 tree_rest_of_compilation. */
9496 set_cfun (NULL);
9497 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9498 current_function_decl = NULL;
9501 /* Check the declarations given in a for-loop for satisfying the C99
9502 constraints. If exactly one such decl is found, return it. LOC is
9503 the location of the opening parenthesis of the for loop. The last
9504 parameter allows you to control the "for loop initial declarations
9505 are only allowed in C99 mode". Normally, you should pass
9506 flag_isoc99 as that parameter. But in some cases (Objective-C
9507 foreach loop, for example) we want to run the checks in this
9508 function even if not in C99 mode, so we allow the caller to turn
9509 off the error about not being in C99 mode.
9512 tree
9513 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9515 struct c_binding *b;
9516 tree one_decl = NULL_TREE;
9517 int n_decls = 0;
9519 if (!turn_off_iso_c99_error)
9521 static bool hint = true;
9522 /* If we get here, declarations have been used in a for loop without
9523 the C99 for loop scope. This doesn't make much sense, so don't
9524 allow it. */
9525 error_at (loc, "%<for%> loop initial declarations "
9526 "are only allowed in C99 or C11 mode");
9527 if (hint)
9529 inform (loc,
9530 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9531 "to compile your code");
9532 hint = false;
9534 return NULL_TREE;
9536 /* C99 subclause 6.8.5 paragraph 3:
9538 [#3] The declaration part of a for statement shall only
9539 declare identifiers for objects having storage class auto or
9540 register.
9542 It isn't clear whether, in this sentence, "identifiers" binds to
9543 "shall only declare" or to "objects" - that is, whether all identifiers
9544 declared must be identifiers for objects, or whether the restriction
9545 only applies to those that are. (A question on this in comp.std.c
9546 in November 2000 received no answer.) We implement the strictest
9547 interpretation, to avoid creating an extension which later causes
9548 problems. */
9550 for (b = current_scope->bindings; b; b = b->prev)
9552 tree id = b->id;
9553 tree decl = b->decl;
9555 if (!id)
9556 continue;
9558 switch (TREE_CODE (decl))
9560 case VAR_DECL:
9562 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9563 if (TREE_STATIC (decl))
9564 error_at (decl_loc,
9565 "declaration of static variable %qD in %<for%> loop "
9566 "initial declaration", decl);
9567 else if (DECL_EXTERNAL (decl))
9568 error_at (decl_loc,
9569 "declaration of %<extern%> variable %qD in %<for%> loop "
9570 "initial declaration", decl);
9572 break;
9574 case RECORD_TYPE:
9575 error_at (loc,
9576 "%<struct %E%> declared in %<for%> loop initial "
9577 "declaration", id);
9578 break;
9579 case UNION_TYPE:
9580 error_at (loc,
9581 "%<union %E%> declared in %<for%> loop initial declaration",
9582 id);
9583 break;
9584 case ENUMERAL_TYPE:
9585 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9586 "initial declaration", id);
9587 break;
9588 default:
9589 error_at (loc, "declaration of non-variable "
9590 "%qD in %<for%> loop initial declaration", decl);
9593 n_decls++;
9594 one_decl = decl;
9597 return n_decls == 1 ? one_decl : NULL_TREE;
9600 /* Save and reinitialize the variables
9601 used during compilation of a C function. */
9603 void
9604 c_push_function_context (void)
9606 struct language_function *p = cfun->language;
9607 /* cfun->language might have been already allocated by the use of
9608 -Wunused-local-typedefs. In that case, just re-use it. */
9609 if (p == NULL)
9610 cfun->language = p = ggc_cleared_alloc<language_function> ();
9612 p->base.x_stmt_tree = c_stmt_tree;
9613 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9614 p->x_break_label = c_break_label;
9615 p->x_cont_label = c_cont_label;
9616 p->x_switch_stack = c_switch_stack;
9617 p->arg_info = current_function_arg_info;
9618 p->returns_value = current_function_returns_value;
9619 p->returns_null = current_function_returns_null;
9620 p->returns_abnormally = current_function_returns_abnormally;
9621 p->warn_about_return_type = warn_about_return_type;
9623 push_function_context ();
9626 /* Restore the variables used during compilation of a C function. */
9628 void
9629 c_pop_function_context (void)
9631 struct language_function *p;
9633 pop_function_context ();
9634 p = cfun->language;
9636 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9637 used to store data throughout the life time of the current cfun,
9638 So don't deallocate it. */
9639 if (!warn_unused_local_typedefs)
9640 cfun->language = NULL;
9642 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9643 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9645 /* Stop pointing to the local nodes about to be freed. */
9646 /* But DECL_INITIAL must remain nonzero so we know this
9647 was an actual function definition. */
9648 DECL_INITIAL (current_function_decl) = error_mark_node;
9649 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9652 c_stmt_tree = p->base.x_stmt_tree;
9653 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9654 c_break_label = p->x_break_label;
9655 c_cont_label = p->x_cont_label;
9656 c_switch_stack = p->x_switch_stack;
9657 current_function_arg_info = p->arg_info;
9658 current_function_returns_value = p->returns_value;
9659 current_function_returns_null = p->returns_null;
9660 current_function_returns_abnormally = p->returns_abnormally;
9661 warn_about_return_type = p->warn_about_return_type;
9664 /* The functions below are required for functionality of doing
9665 function at once processing in the C front end. Currently these
9666 functions are not called from anywhere in the C front end, but as
9667 these changes continue, that will change. */
9669 /* Returns the stmt_tree (if any) to which statements are currently
9670 being added. If there is no active statement-tree, NULL is
9671 returned. */
9673 stmt_tree
9674 current_stmt_tree (void)
9676 return &c_stmt_tree;
9679 /* Return the global value of T as a symbol. */
9681 tree
9682 identifier_global_value (tree t)
9684 struct c_binding *b;
9686 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9687 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9688 return b->decl;
9690 return NULL_TREE;
9693 /* In C, the only C-linkage public declaration is at file scope. */
9695 tree
9696 c_linkage_bindings (tree name)
9698 return identifier_global_value (name);
9701 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9702 otherwise the name is found in ridpointers from RID_INDEX. */
9704 void
9705 record_builtin_type (enum rid rid_index, const char *name, tree type)
9707 tree id, decl;
9708 if (name == 0)
9709 id = ridpointers[(int) rid_index];
9710 else
9711 id = get_identifier (name);
9712 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9713 pushdecl (decl);
9714 if (debug_hooks->type_decl)
9715 debug_hooks->type_decl (decl, false);
9718 /* Build the void_list_node (void_type_node having been created). */
9719 tree
9720 build_void_list_node (void)
9722 tree t = build_tree_list (NULL_TREE, void_type_node);
9723 return t;
9726 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9728 struct c_parm *
9729 build_c_parm (struct c_declspecs *specs, tree attrs,
9730 struct c_declarator *declarator,
9731 location_t loc)
9733 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9734 ret->specs = specs;
9735 ret->attrs = attrs;
9736 ret->declarator = declarator;
9737 ret->loc = loc;
9738 return ret;
9741 /* Return a declarator with nested attributes. TARGET is the inner
9742 declarator to which these attributes apply. ATTRS are the
9743 attributes. */
9745 struct c_declarator *
9746 build_attrs_declarator (tree attrs, struct c_declarator *target)
9748 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9749 ret->kind = cdk_attrs;
9750 ret->declarator = target;
9751 ret->u.attrs = attrs;
9752 return ret;
9755 /* Return a declarator for a function with arguments specified by ARGS
9756 and return type specified by TARGET. */
9758 struct c_declarator *
9759 build_function_declarator (struct c_arg_info *args,
9760 struct c_declarator *target)
9762 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9763 ret->kind = cdk_function;
9764 ret->declarator = target;
9765 ret->u.arg_info = args;
9766 return ret;
9769 /* Return a declarator for the identifier IDENT (which may be
9770 NULL_TREE for an abstract declarator). */
9772 struct c_declarator *
9773 build_id_declarator (tree ident)
9775 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9776 ret->kind = cdk_id;
9777 ret->declarator = 0;
9778 ret->u.id = ident;
9779 /* Default value - may get reset to a more precise location. */
9780 ret->id_loc = input_location;
9781 return ret;
9784 /* Return something to represent absolute declarators containing a *.
9785 TARGET is the absolute declarator that the * contains.
9786 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9787 to apply to the pointer type. */
9789 struct c_declarator *
9790 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9791 struct c_declarator *target)
9793 tree attrs;
9794 int quals = 0;
9795 struct c_declarator *itarget = target;
9796 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9797 if (type_quals_attrs)
9799 attrs = type_quals_attrs->attrs;
9800 quals = quals_from_declspecs (type_quals_attrs);
9801 if (attrs != NULL_TREE)
9802 itarget = build_attrs_declarator (attrs, target);
9804 ret->kind = cdk_pointer;
9805 ret->declarator = itarget;
9806 ret->u.pointer_quals = quals;
9807 return ret;
9810 /* Return a pointer to a structure for an empty list of declaration
9811 specifiers. */
9813 struct c_declspecs *
9814 build_null_declspecs (void)
9816 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9817 memset (ret, 0, sizeof *ret);
9818 ret->align_log = -1;
9819 ret->typespec_word = cts_none;
9820 ret->storage_class = csc_none;
9821 ret->expr_const_operands = true;
9822 ret->typespec_kind = ctsk_none;
9823 ret->address_space = ADDR_SPACE_GENERIC;
9824 return ret;
9827 /* Add the address space ADDRSPACE to the declaration specifiers
9828 SPECS, returning SPECS. */
9830 struct c_declspecs *
9831 declspecs_add_addrspace (source_location location,
9832 struct c_declspecs *specs, addr_space_t as)
9834 specs->non_sc_seen_p = true;
9835 specs->declspecs_seen_p = true;
9837 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9838 && specs->address_space != as)
9839 error ("incompatible address space qualifiers %qs and %qs",
9840 c_addr_space_name (as),
9841 c_addr_space_name (specs->address_space));
9842 else
9844 specs->address_space = as;
9845 specs->locations[cdw_address_space] = location;
9847 return specs;
9850 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9851 returning SPECS. */
9853 struct c_declspecs *
9854 declspecs_add_qual (source_location loc,
9855 struct c_declspecs *specs, tree qual)
9857 enum rid i;
9858 bool dupe = false;
9859 specs->non_sc_seen_p = true;
9860 specs->declspecs_seen_p = true;
9861 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9862 && C_IS_RESERVED_WORD (qual));
9863 i = C_RID_CODE (qual);
9864 location_t prev_loc = UNKNOWN_LOCATION;
9865 switch (i)
9867 case RID_CONST:
9868 dupe = specs->const_p;
9869 specs->const_p = true;
9870 prev_loc = specs->locations[cdw_const];
9871 specs->locations[cdw_const] = loc;
9872 break;
9873 case RID_VOLATILE:
9874 dupe = specs->volatile_p;
9875 specs->volatile_p = true;
9876 prev_loc = specs->locations[cdw_volatile];
9877 specs->locations[cdw_volatile] = loc;
9878 break;
9879 case RID_RESTRICT:
9880 dupe = specs->restrict_p;
9881 specs->restrict_p = true;
9882 prev_loc = specs->locations[cdw_restrict];
9883 specs->locations[cdw_restrict] = loc;
9884 break;
9885 case RID_ATOMIC:
9886 dupe = specs->atomic_p;
9887 specs->atomic_p = true;
9888 prev_loc = specs->locations[cdw_atomic];
9889 specs->locations[cdw_atomic] = loc;
9890 break;
9891 default:
9892 gcc_unreachable ();
9894 if (dupe)
9896 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9897 "duplicate %qE declaration specifier", qual);
9898 if (!warned
9899 && warn_duplicate_decl_specifier
9900 && prev_loc >= RESERVED_LOCATION_COUNT
9901 && !from_macro_expansion_at (prev_loc)
9902 && !from_macro_expansion_at (loc))
9903 warning_at (loc, OPT_Wduplicate_decl_specifier,
9904 "duplicate %qE declaration specifier", qual);
9906 return specs;
9909 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9910 returning SPECS. */
9912 struct c_declspecs *
9913 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9914 struct c_typespec spec)
9916 tree type = spec.spec;
9917 specs->non_sc_seen_p = true;
9918 specs->declspecs_seen_p = true;
9919 specs->typespec_kind = spec.kind;
9920 if (TREE_DEPRECATED (type))
9921 specs->deprecated_p = true;
9923 /* Handle type specifier keywords. */
9924 if (TREE_CODE (type) == IDENTIFIER_NODE
9925 && C_IS_RESERVED_WORD (type)
9926 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9928 enum rid i = C_RID_CODE (type);
9929 if (specs->type)
9931 error_at (loc, "two or more data types in declaration specifiers");
9932 return specs;
9934 if ((int) i <= (int) RID_LAST_MODIFIER)
9936 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9937 bool dupe = false;
9938 switch (i)
9940 case RID_LONG:
9941 if (specs->long_long_p)
9943 error_at (loc, "%<long long long%> is too long for GCC");
9944 break;
9946 if (specs->long_p)
9948 if (specs->typespec_word == cts_double)
9950 error_at (loc,
9951 ("both %<long long%> and %<double%> in "
9952 "declaration specifiers"));
9953 break;
9955 pedwarn_c90 (loc, OPT_Wlong_long,
9956 "ISO C90 does not support %<long long%>");
9957 specs->long_long_p = 1;
9958 specs->locations[cdw_long_long] = loc;
9959 break;
9961 if (specs->short_p)
9962 error_at (loc,
9963 ("both %<long%> and %<short%> in "
9964 "declaration specifiers"));
9965 else if (specs->typespec_word == cts_auto_type)
9966 error_at (loc,
9967 ("both %<long%> and %<__auto_type%> in "
9968 "declaration specifiers"));
9969 else if (specs->typespec_word == cts_void)
9970 error_at (loc,
9971 ("both %<long%> and %<void%> in "
9972 "declaration specifiers"));
9973 else if (specs->typespec_word == cts_int_n)
9974 error_at (loc,
9975 ("both %<long%> and %<__int%d%> in "
9976 "declaration specifiers"),
9977 int_n_data[specs->int_n_idx].bitsize);
9978 else if (specs->typespec_word == cts_bool)
9979 error_at (loc,
9980 ("both %<long%> and %<_Bool%> in "
9981 "declaration specifiers"));
9982 else if (specs->typespec_word == cts_char)
9983 error_at (loc,
9984 ("both %<long%> and %<char%> in "
9985 "declaration specifiers"));
9986 else if (specs->typespec_word == cts_float)
9987 error_at (loc,
9988 ("both %<long%> and %<float%> in "
9989 "declaration specifiers"));
9990 else if (specs->typespec_word == cts_floatn_nx)
9991 error_at (loc,
9992 ("both %<long%> and %<_Float%d%s%> in "
9993 "declaration specifiers"),
9994 floatn_nx_types[specs->floatn_nx_idx].n,
9995 (floatn_nx_types[specs->floatn_nx_idx].extended
9996 ? "x"
9997 : ""));
9998 else if (specs->typespec_word == cts_dfloat32)
9999 error_at (loc,
10000 ("both %<long%> and %<_Decimal32%> in "
10001 "declaration specifiers"));
10002 else if (specs->typespec_word == cts_dfloat64)
10003 error_at (loc,
10004 ("both %<long%> and %<_Decimal64%> in "
10005 "declaration specifiers"));
10006 else if (specs->typespec_word == cts_dfloat128)
10007 error_at (loc,
10008 ("both %<long%> and %<_Decimal128%> in "
10009 "declaration specifiers"));
10010 else
10012 specs->long_p = true;
10013 specs->locations[cdw_long] = loc;
10015 break;
10016 case RID_SHORT:
10017 dupe = specs->short_p;
10018 if (specs->long_p)
10019 error_at (loc,
10020 ("both %<long%> and %<short%> in "
10021 "declaration specifiers"));
10022 else if (specs->typespec_word == cts_auto_type)
10023 error_at (loc,
10024 ("both %<short%> and %<__auto_type%> in "
10025 "declaration specifiers"));
10026 else if (specs->typespec_word == cts_void)
10027 error_at (loc,
10028 ("both %<short%> and %<void%> in "
10029 "declaration specifiers"));
10030 else if (specs->typespec_word == cts_int_n)
10031 error_at (loc,
10032 ("both %<short%> and %<__int%d%> in "
10033 "declaration specifiers"),
10034 int_n_data[specs->int_n_idx].bitsize);
10035 else if (specs->typespec_word == cts_bool)
10036 error_at (loc,
10037 ("both %<short%> and %<_Bool%> in "
10038 "declaration specifiers"));
10039 else if (specs->typespec_word == cts_char)
10040 error_at (loc,
10041 ("both %<short%> and %<char%> in "
10042 "declaration specifiers"));
10043 else if (specs->typespec_word == cts_float)
10044 error_at (loc,
10045 ("both %<short%> and %<float%> in "
10046 "declaration specifiers"));
10047 else if (specs->typespec_word == cts_double)
10048 error_at (loc,
10049 ("both %<short%> and %<double%> in "
10050 "declaration specifiers"));
10051 else if (specs->typespec_word == cts_floatn_nx)
10052 error_at (loc,
10053 ("both %<short%> and %<_Float%d%s%> in "
10054 "declaration specifiers"),
10055 floatn_nx_types[specs->floatn_nx_idx].n,
10056 (floatn_nx_types[specs->floatn_nx_idx].extended
10057 ? "x"
10058 : ""));
10059 else if (specs->typespec_word == cts_dfloat32)
10060 error_at (loc,
10061 ("both %<short%> and %<_Decimal32%> in "
10062 "declaration specifiers"));
10063 else if (specs->typespec_word == cts_dfloat64)
10064 error_at (loc,
10065 ("both %<short%> and %<_Decimal64%> in "
10066 "declaration specifiers"));
10067 else if (specs->typespec_word == cts_dfloat128)
10068 error_at (loc,
10069 ("both %<short%> and %<_Decimal128%> in "
10070 "declaration specifiers"));
10071 else
10073 specs->short_p = true;
10074 specs->locations[cdw_short] = loc;
10076 break;
10077 case RID_SIGNED:
10078 dupe = specs->signed_p;
10079 if (specs->unsigned_p)
10080 error_at (loc,
10081 ("both %<signed%> and %<unsigned%> in "
10082 "declaration specifiers"));
10083 else if (specs->typespec_word == cts_auto_type)
10084 error_at (loc,
10085 ("both %<signed%> and %<__auto_type%> in "
10086 "declaration specifiers"));
10087 else if (specs->typespec_word == cts_void)
10088 error_at (loc,
10089 ("both %<signed%> and %<void%> in "
10090 "declaration specifiers"));
10091 else if (specs->typespec_word == cts_bool)
10092 error_at (loc,
10093 ("both %<signed%> and %<_Bool%> in "
10094 "declaration specifiers"));
10095 else if (specs->typespec_word == cts_float)
10096 error_at (loc,
10097 ("both %<signed%> and %<float%> in "
10098 "declaration specifiers"));
10099 else if (specs->typespec_word == cts_double)
10100 error_at (loc,
10101 ("both %<signed%> and %<double%> in "
10102 "declaration specifiers"));
10103 else if (specs->typespec_word == cts_floatn_nx)
10104 error_at (loc,
10105 ("both %<signed%> and %<_Float%d%s%> in "
10106 "declaration specifiers"),
10107 floatn_nx_types[specs->floatn_nx_idx].n,
10108 (floatn_nx_types[specs->floatn_nx_idx].extended
10109 ? "x"
10110 : ""));
10111 else if (specs->typespec_word == cts_dfloat32)
10112 error_at (loc,
10113 ("both %<signed%> and %<_Decimal32%> in "
10114 "declaration specifiers"));
10115 else if (specs->typespec_word == cts_dfloat64)
10116 error_at (loc,
10117 ("both %<signed%> and %<_Decimal64%> in "
10118 "declaration specifiers"));
10119 else if (specs->typespec_word == cts_dfloat128)
10120 error_at (loc,
10121 ("both %<signed%> and %<_Decimal128%> in "
10122 "declaration specifiers"));
10123 else
10125 specs->signed_p = true;
10126 specs->locations[cdw_signed] = loc;
10128 break;
10129 case RID_UNSIGNED:
10130 dupe = specs->unsigned_p;
10131 if (specs->signed_p)
10132 error_at (loc,
10133 ("both %<signed%> and %<unsigned%> in "
10134 "declaration specifiers"));
10135 else if (specs->typespec_word == cts_auto_type)
10136 error_at (loc,
10137 ("both %<unsigned%> and %<__auto_type%> in "
10138 "declaration specifiers"));
10139 else if (specs->typespec_word == cts_void)
10140 error_at (loc,
10141 ("both %<unsigned%> and %<void%> in "
10142 "declaration specifiers"));
10143 else if (specs->typespec_word == cts_bool)
10144 error_at (loc,
10145 ("both %<unsigned%> and %<_Bool%> in "
10146 "declaration specifiers"));
10147 else if (specs->typespec_word == cts_float)
10148 error_at (loc,
10149 ("both %<unsigned%> and %<float%> in "
10150 "declaration specifiers"));
10151 else if (specs->typespec_word == cts_double)
10152 error_at (loc,
10153 ("both %<unsigned%> and %<double%> in "
10154 "declaration specifiers"));
10155 else if (specs->typespec_word == cts_floatn_nx)
10156 error_at (loc,
10157 ("both %<unsigned%> and %<_Float%d%s%> in "
10158 "declaration specifiers"),
10159 floatn_nx_types[specs->floatn_nx_idx].n,
10160 (floatn_nx_types[specs->floatn_nx_idx].extended
10161 ? "x"
10162 : ""));
10163 else if (specs->typespec_word == cts_dfloat32)
10164 error_at (loc,
10165 ("both %<unsigned%> and %<_Decimal32%> in "
10166 "declaration specifiers"));
10167 else if (specs->typespec_word == cts_dfloat64)
10168 error_at (loc,
10169 ("both %<unsigned%> and %<_Decimal64%> in "
10170 "declaration specifiers"));
10171 else if (specs->typespec_word == cts_dfloat128)
10172 error_at (loc,
10173 ("both %<unsigned%> and %<_Decimal128%> in "
10174 "declaration specifiers"));
10175 else
10177 specs->unsigned_p = true;
10178 specs->locations[cdw_unsigned] = loc;
10180 break;
10181 case RID_COMPLEX:
10182 dupe = specs->complex_p;
10183 if (!in_system_header_at (loc))
10184 pedwarn_c90 (loc, OPT_Wpedantic,
10185 "ISO C90 does not support complex types");
10186 if (specs->typespec_word == cts_auto_type)
10187 error_at (loc,
10188 ("both %<complex%> and %<__auto_type%> in "
10189 "declaration specifiers"));
10190 else if (specs->typespec_word == cts_void)
10191 error_at (loc,
10192 ("both %<complex%> and %<void%> in "
10193 "declaration specifiers"));
10194 else if (specs->typespec_word == cts_bool)
10195 error_at (loc,
10196 ("both %<complex%> and %<_Bool%> in "
10197 "declaration specifiers"));
10198 else if (specs->typespec_word == cts_dfloat32)
10199 error_at (loc,
10200 ("both %<complex%> and %<_Decimal32%> in "
10201 "declaration specifiers"));
10202 else if (specs->typespec_word == cts_dfloat64)
10203 error_at (loc,
10204 ("both %<complex%> and %<_Decimal64%> in "
10205 "declaration specifiers"));
10206 else if (specs->typespec_word == cts_dfloat128)
10207 error_at (loc,
10208 ("both %<complex%> and %<_Decimal128%> in "
10209 "declaration specifiers"));
10210 else if (specs->typespec_word == cts_fract)
10211 error_at (loc,
10212 ("both %<complex%> and %<_Fract%> in "
10213 "declaration specifiers"));
10214 else if (specs->typespec_word == cts_accum)
10215 error_at (loc,
10216 ("both %<complex%> and %<_Accum%> in "
10217 "declaration specifiers"));
10218 else if (specs->saturating_p)
10219 error_at (loc,
10220 ("both %<complex%> and %<_Sat%> in "
10221 "declaration specifiers"));
10222 else
10224 specs->complex_p = true;
10225 specs->locations[cdw_complex] = loc;
10227 break;
10228 case RID_SAT:
10229 dupe = specs->saturating_p;
10230 pedwarn (loc, OPT_Wpedantic,
10231 "ISO C does not support saturating types");
10232 if (specs->typespec_word == cts_int_n)
10234 error_at (loc,
10235 ("both %<_Sat%> and %<__int%d%> in "
10236 "declaration specifiers"),
10237 int_n_data[specs->int_n_idx].bitsize);
10239 else if (specs->typespec_word == cts_auto_type)
10240 error_at (loc,
10241 ("both %<_Sat%> and %<__auto_type%> in "
10242 "declaration specifiers"));
10243 else if (specs->typespec_word == cts_void)
10244 error_at (loc,
10245 ("both %<_Sat%> and %<void%> in "
10246 "declaration specifiers"));
10247 else if (specs->typespec_word == cts_bool)
10248 error_at (loc,
10249 ("both %<_Sat%> and %<_Bool%> in "
10250 "declaration specifiers"));
10251 else if (specs->typespec_word == cts_char)
10252 error_at (loc,
10253 ("both %<_Sat%> and %<char%> in "
10254 "declaration specifiers"));
10255 else if (specs->typespec_word == cts_int)
10256 error_at (loc,
10257 ("both %<_Sat%> and %<int%> in "
10258 "declaration specifiers"));
10259 else if (specs->typespec_word == cts_float)
10260 error_at (loc,
10261 ("both %<_Sat%> and %<float%> in "
10262 "declaration specifiers"));
10263 else if (specs->typespec_word == cts_double)
10264 error_at (loc,
10265 ("both %<_Sat%> and %<double%> in "
10266 "declaration specifiers"));
10267 else if (specs->typespec_word == cts_floatn_nx)
10268 error_at (loc,
10269 ("both %<_Sat%> and %<_Float%d%s%> in "
10270 "declaration specifiers"),
10271 floatn_nx_types[specs->floatn_nx_idx].n,
10272 (floatn_nx_types[specs->floatn_nx_idx].extended
10273 ? "x"
10274 : ""));
10275 else if (specs->typespec_word == cts_dfloat32)
10276 error_at (loc,
10277 ("both %<_Sat%> and %<_Decimal32%> in "
10278 "declaration specifiers"));
10279 else if (specs->typespec_word == cts_dfloat64)
10280 error_at (loc,
10281 ("both %<_Sat%> and %<_Decimal64%> in "
10282 "declaration specifiers"));
10283 else if (specs->typespec_word == cts_dfloat128)
10284 error_at (loc,
10285 ("both %<_Sat%> and %<_Decimal128%> in "
10286 "declaration specifiers"));
10287 else if (specs->complex_p)
10288 error_at (loc,
10289 ("both %<_Sat%> and %<complex%> in "
10290 "declaration specifiers"));
10291 else
10293 specs->saturating_p = true;
10294 specs->locations[cdw_saturating] = loc;
10296 break;
10297 default:
10298 gcc_unreachable ();
10301 if (dupe)
10302 error_at (loc, "duplicate %qE", type);
10304 return specs;
10306 else
10308 /* "void", "_Bool", "char", "int", "float", "double",
10309 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10310 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10311 "__auto_type". */
10312 if (specs->typespec_word != cts_none)
10314 error_at (loc,
10315 "two or more data types in declaration specifiers");
10316 return specs;
10318 switch (i)
10320 case RID_AUTO_TYPE:
10321 if (specs->long_p)
10322 error_at (loc,
10323 ("both %<long%> and %<__auto_type%> in "
10324 "declaration specifiers"));
10325 else if (specs->short_p)
10326 error_at (loc,
10327 ("both %<short%> and %<__auto_type%> in "
10328 "declaration specifiers"));
10329 else if (specs->signed_p)
10330 error_at (loc,
10331 ("both %<signed%> and %<__auto_type%> in "
10332 "declaration specifiers"));
10333 else if (specs->unsigned_p)
10334 error_at (loc,
10335 ("both %<unsigned%> and %<__auto_type%> in "
10336 "declaration specifiers"));
10337 else if (specs->complex_p)
10338 error_at (loc,
10339 ("both %<complex%> and %<__auto_type%> in "
10340 "declaration specifiers"));
10341 else if (specs->saturating_p)
10342 error_at (loc,
10343 ("both %<_Sat%> and %<__auto_type%> in "
10344 "declaration specifiers"));
10345 else
10347 specs->typespec_word = cts_auto_type;
10348 specs->locations[cdw_typespec] = loc;
10350 return specs;
10351 case RID_INT_N_0:
10352 case RID_INT_N_1:
10353 case RID_INT_N_2:
10354 case RID_INT_N_3:
10355 specs->int_n_idx = i - RID_INT_N_0;
10356 if (!in_system_header_at (input_location))
10357 pedwarn (loc, OPT_Wpedantic,
10358 "ISO C does not support %<__int%d%> types",
10359 int_n_data[specs->int_n_idx].bitsize);
10361 if (specs->long_p)
10362 error_at (loc,
10363 ("both %<__int%d%> and %<long%> in "
10364 "declaration specifiers"),
10365 int_n_data[specs->int_n_idx].bitsize);
10366 else if (specs->saturating_p)
10367 error_at (loc,
10368 ("both %<_Sat%> and %<__int%d%> in "
10369 "declaration specifiers"),
10370 int_n_data[specs->int_n_idx].bitsize);
10371 else if (specs->short_p)
10372 error_at (loc,
10373 ("both %<__int%d%> and %<short%> in "
10374 "declaration specifiers"),
10375 int_n_data[specs->int_n_idx].bitsize);
10376 else if (! int_n_enabled_p[specs->int_n_idx])
10378 specs->typespec_word = cts_int_n;
10379 error_at (loc,
10380 "%<__int%d%> is not supported on this target",
10381 int_n_data[specs->int_n_idx].bitsize);
10383 else
10385 specs->typespec_word = cts_int_n;
10386 specs->locations[cdw_typespec] = loc;
10388 return specs;
10389 case RID_VOID:
10390 if (specs->long_p)
10391 error_at (loc,
10392 ("both %<long%> and %<void%> in "
10393 "declaration specifiers"));
10394 else if (specs->short_p)
10395 error_at (loc,
10396 ("both %<short%> and %<void%> in "
10397 "declaration specifiers"));
10398 else if (specs->signed_p)
10399 error_at (loc,
10400 ("both %<signed%> and %<void%> in "
10401 "declaration specifiers"));
10402 else if (specs->unsigned_p)
10403 error_at (loc,
10404 ("both %<unsigned%> and %<void%> in "
10405 "declaration specifiers"));
10406 else if (specs->complex_p)
10407 error_at (loc,
10408 ("both %<complex%> and %<void%> in "
10409 "declaration specifiers"));
10410 else if (specs->saturating_p)
10411 error_at (loc,
10412 ("both %<_Sat%> and %<void%> in "
10413 "declaration specifiers"));
10414 else
10416 specs->typespec_word = cts_void;
10417 specs->locations[cdw_typespec] = loc;
10419 return specs;
10420 case RID_BOOL:
10421 if (!in_system_header_at (loc))
10422 pedwarn_c90 (loc, OPT_Wpedantic,
10423 "ISO C90 does not support boolean types");
10424 if (specs->long_p)
10425 error_at (loc,
10426 ("both %<long%> and %<_Bool%> in "
10427 "declaration specifiers"));
10428 else if (specs->short_p)
10429 error_at (loc,
10430 ("both %<short%> and %<_Bool%> in "
10431 "declaration specifiers"));
10432 else if (specs->signed_p)
10433 error_at (loc,
10434 ("both %<signed%> and %<_Bool%> in "
10435 "declaration specifiers"));
10436 else if (specs->unsigned_p)
10437 error_at (loc,
10438 ("both %<unsigned%> and %<_Bool%> in "
10439 "declaration specifiers"));
10440 else if (specs->complex_p)
10441 error_at (loc,
10442 ("both %<complex%> and %<_Bool%> in "
10443 "declaration specifiers"));
10444 else if (specs->saturating_p)
10445 error_at (loc,
10446 ("both %<_Sat%> and %<_Bool%> in "
10447 "declaration specifiers"));
10448 else
10450 specs->typespec_word = cts_bool;
10451 specs->locations[cdw_typespec] = loc;
10453 return specs;
10454 case RID_CHAR:
10455 if (specs->long_p)
10456 error_at (loc,
10457 ("both %<long%> and %<char%> in "
10458 "declaration specifiers"));
10459 else if (specs->short_p)
10460 error_at (loc,
10461 ("both %<short%> and %<char%> in "
10462 "declaration specifiers"));
10463 else if (specs->saturating_p)
10464 error_at (loc,
10465 ("both %<_Sat%> and %<char%> in "
10466 "declaration specifiers"));
10467 else
10469 specs->typespec_word = cts_char;
10470 specs->locations[cdw_typespec] = loc;
10472 return specs;
10473 case RID_INT:
10474 if (specs->saturating_p)
10475 error_at (loc,
10476 ("both %<_Sat%> and %<int%> in "
10477 "declaration specifiers"));
10478 else
10480 specs->typespec_word = cts_int;
10481 specs->locations[cdw_typespec] = loc;
10483 return specs;
10484 case RID_FLOAT:
10485 if (specs->long_p)
10486 error_at (loc,
10487 ("both %<long%> and %<float%> in "
10488 "declaration specifiers"));
10489 else if (specs->short_p)
10490 error_at (loc,
10491 ("both %<short%> and %<float%> in "
10492 "declaration specifiers"));
10493 else if (specs->signed_p)
10494 error_at (loc,
10495 ("both %<signed%> and %<float%> in "
10496 "declaration specifiers"));
10497 else if (specs->unsigned_p)
10498 error_at (loc,
10499 ("both %<unsigned%> and %<float%> in "
10500 "declaration specifiers"));
10501 else if (specs->saturating_p)
10502 error_at (loc,
10503 ("both %<_Sat%> and %<float%> in "
10504 "declaration specifiers"));
10505 else
10507 specs->typespec_word = cts_float;
10508 specs->locations[cdw_typespec] = loc;
10510 return specs;
10511 case RID_DOUBLE:
10512 if (specs->long_long_p)
10513 error_at (loc,
10514 ("both %<long long%> and %<double%> in "
10515 "declaration specifiers"));
10516 else if (specs->short_p)
10517 error_at (loc,
10518 ("both %<short%> and %<double%> in "
10519 "declaration specifiers"));
10520 else if (specs->signed_p)
10521 error_at (loc,
10522 ("both %<signed%> and %<double%> in "
10523 "declaration specifiers"));
10524 else if (specs->unsigned_p)
10525 error_at (loc,
10526 ("both %<unsigned%> and %<double%> in "
10527 "declaration specifiers"));
10528 else if (specs->saturating_p)
10529 error_at (loc,
10530 ("both %<_Sat%> and %<double%> in "
10531 "declaration specifiers"));
10532 else
10534 specs->typespec_word = cts_double;
10535 specs->locations[cdw_typespec] = loc;
10537 return specs;
10538 CASE_RID_FLOATN_NX:
10539 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10540 if (!in_system_header_at (input_location))
10541 pedwarn (loc, OPT_Wpedantic,
10542 "ISO C does not support the %<_Float%d%s%> type",
10543 floatn_nx_types[specs->floatn_nx_idx].n,
10544 (floatn_nx_types[specs->floatn_nx_idx].extended
10545 ? "x"
10546 : ""));
10548 if (specs->long_p)
10549 error_at (loc,
10550 ("both %<long%> and %<_Float%d%s%> in "
10551 "declaration specifiers"),
10552 floatn_nx_types[specs->floatn_nx_idx].n,
10553 (floatn_nx_types[specs->floatn_nx_idx].extended
10554 ? "x"
10555 : ""));
10556 else if (specs->short_p)
10557 error_at (loc,
10558 ("both %<short%> and %<_Float%d%s%> in "
10559 "declaration specifiers"),
10560 floatn_nx_types[specs->floatn_nx_idx].n,
10561 (floatn_nx_types[specs->floatn_nx_idx].extended
10562 ? "x"
10563 : ""));
10564 else if (specs->signed_p)
10565 error_at (loc,
10566 ("both %<signed%> and %<_Float%d%s%> in "
10567 "declaration specifiers"),
10568 floatn_nx_types[specs->floatn_nx_idx].n,
10569 (floatn_nx_types[specs->floatn_nx_idx].extended
10570 ? "x"
10571 : ""));
10572 else if (specs->unsigned_p)
10573 error_at (loc,
10574 ("both %<unsigned%> and %<_Float%d%s%> in "
10575 "declaration specifiers"),
10576 floatn_nx_types[specs->floatn_nx_idx].n,
10577 (floatn_nx_types[specs->floatn_nx_idx].extended
10578 ? "x"
10579 : ""));
10580 else if (specs->saturating_p)
10581 error_at (loc,
10582 ("both %<_Sat%> and %<_Float%d%s%> in "
10583 "declaration specifiers"),
10584 floatn_nx_types[specs->floatn_nx_idx].n,
10585 (floatn_nx_types[specs->floatn_nx_idx].extended
10586 ? "x"
10587 : ""));
10588 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10590 specs->typespec_word = cts_floatn_nx;
10591 error_at (loc,
10592 "%<_Float%d%s%> is not supported on this target",
10593 floatn_nx_types[specs->floatn_nx_idx].n,
10594 (floatn_nx_types[specs->floatn_nx_idx].extended
10595 ? "x"
10596 : ""));
10598 else
10600 specs->typespec_word = cts_floatn_nx;
10601 specs->locations[cdw_typespec] = loc;
10603 return specs;
10604 case RID_DFLOAT32:
10605 case RID_DFLOAT64:
10606 case RID_DFLOAT128:
10608 const char *str;
10609 if (i == RID_DFLOAT32)
10610 str = "_Decimal32";
10611 else if (i == RID_DFLOAT64)
10612 str = "_Decimal64";
10613 else
10614 str = "_Decimal128";
10615 if (specs->long_long_p)
10616 error_at (loc,
10617 ("both %<long long%> and %qs in "
10618 "declaration specifiers"),
10619 str);
10620 if (specs->long_p)
10621 error_at (loc,
10622 ("both %<long%> and %qs in "
10623 "declaration specifiers"),
10624 str);
10625 else if (specs->short_p)
10626 error_at (loc,
10627 ("both %<short%> and %qs in "
10628 "declaration specifiers"),
10629 str);
10630 else if (specs->signed_p)
10631 error_at (loc,
10632 ("both %<signed%> and %qs in "
10633 "declaration specifiers"),
10634 str);
10635 else if (specs->unsigned_p)
10636 error_at (loc,
10637 ("both %<unsigned%> and %qs in "
10638 "declaration specifiers"),
10639 str);
10640 else if (specs->complex_p)
10641 error_at (loc,
10642 ("both %<complex%> and %qs in "
10643 "declaration specifiers"),
10644 str);
10645 else if (specs->saturating_p)
10646 error_at (loc,
10647 ("both %<_Sat%> and %qs in "
10648 "declaration specifiers"),
10649 str);
10650 else if (i == RID_DFLOAT32)
10651 specs->typespec_word = cts_dfloat32;
10652 else if (i == RID_DFLOAT64)
10653 specs->typespec_word = cts_dfloat64;
10654 else
10655 specs->typespec_word = cts_dfloat128;
10656 specs->locations[cdw_typespec] = loc;
10658 if (!targetm.decimal_float_supported_p ())
10659 error_at (loc,
10660 ("decimal floating point not supported "
10661 "for this target"));
10662 pedwarn (loc, OPT_Wpedantic,
10663 "ISO C does not support decimal floating point");
10664 return specs;
10665 case RID_FRACT:
10666 case RID_ACCUM:
10668 const char *str;
10669 if (i == RID_FRACT)
10670 str = "_Fract";
10671 else
10672 str = "_Accum";
10673 if (specs->complex_p)
10674 error_at (loc,
10675 ("both %<complex%> and %qs in "
10676 "declaration specifiers"),
10677 str);
10678 else if (i == RID_FRACT)
10679 specs->typespec_word = cts_fract;
10680 else
10681 specs->typespec_word = cts_accum;
10682 specs->locations[cdw_typespec] = loc;
10684 if (!targetm.fixed_point_supported_p ())
10685 error_at (loc,
10686 "fixed-point types not supported for this target");
10687 pedwarn (loc, OPT_Wpedantic,
10688 "ISO C does not support fixed-point types");
10689 return specs;
10690 default:
10691 /* ObjC reserved word "id", handled below. */
10692 break;
10697 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10698 form of ObjC type, cases such as "int" and "long" being handled
10699 above), a TYPE (struct, union, enum and typeof specifiers) or an
10700 ERROR_MARK. In none of these cases may there have previously
10701 been any type specifiers. */
10702 if (specs->type || specs->typespec_word != cts_none
10703 || specs->long_p || specs->short_p || specs->signed_p
10704 || specs->unsigned_p || specs->complex_p)
10705 error_at (loc, "two or more data types in declaration specifiers");
10706 else if (TREE_CODE (type) == TYPE_DECL)
10708 if (TREE_TYPE (type) == error_mark_node)
10709 ; /* Allow the type to default to int to avoid cascading errors. */
10710 else
10712 specs->type = TREE_TYPE (type);
10713 specs->decl_attr = DECL_ATTRIBUTES (type);
10714 specs->typedef_p = true;
10715 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10716 specs->locations[cdw_typedef] = loc;
10718 /* If this typedef name is defined in a struct, then a C++
10719 lookup would return a different value. */
10720 if (warn_cxx_compat
10721 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10722 warning_at (loc, OPT_Wc___compat,
10723 "C++ lookup of %qD would return a field, not a type",
10724 type);
10726 /* If we are parsing a struct, record that a struct field
10727 used a typedef. */
10728 if (warn_cxx_compat && struct_parse_info != NULL)
10729 struct_parse_info->typedefs_seen.safe_push (type);
10732 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10734 tree t = lookup_name (type);
10735 if (!t || TREE_CODE (t) != TYPE_DECL)
10736 error_at (loc, "%qE fails to be a typedef or built in type", type);
10737 else if (TREE_TYPE (t) == error_mark_node)
10739 else
10741 specs->type = TREE_TYPE (t);
10742 specs->locations[cdw_typespec] = loc;
10745 else
10747 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10749 specs->typedef_p = true;
10750 specs->locations[cdw_typedef] = loc;
10751 if (spec.expr)
10753 if (specs->expr)
10754 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10755 specs->expr, spec.expr);
10756 else
10757 specs->expr = spec.expr;
10758 specs->expr_const_operands &= spec.expr_const_operands;
10761 specs->type = type;
10764 return specs;
10767 /* Add the storage class specifier or function specifier SCSPEC to the
10768 declaration specifiers SPECS, returning SPECS. */
10770 struct c_declspecs *
10771 declspecs_add_scspec (source_location loc,
10772 struct c_declspecs *specs,
10773 tree scspec)
10775 enum rid i;
10776 enum c_storage_class n = csc_none;
10777 bool dupe = false;
10778 specs->declspecs_seen_p = true;
10779 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10780 && C_IS_RESERVED_WORD (scspec));
10781 i = C_RID_CODE (scspec);
10782 if (specs->non_sc_seen_p)
10783 warning (OPT_Wold_style_declaration,
10784 "%qE is not at beginning of declaration", scspec);
10785 switch (i)
10787 case RID_INLINE:
10788 /* C99 permits duplicate inline. Although of doubtful utility,
10789 it seems simplest to permit it in gnu89 mode as well, as
10790 there is also little utility in maintaining this as a
10791 difference between gnu89 and C99 inline. */
10792 dupe = false;
10793 specs->inline_p = true;
10794 specs->locations[cdw_inline] = loc;
10795 break;
10796 case RID_NORETURN:
10797 /* Duplicate _Noreturn is permitted. */
10798 dupe = false;
10799 specs->noreturn_p = true;
10800 specs->locations[cdw_noreturn] = loc;
10801 break;
10802 case RID_THREAD:
10803 dupe = specs->thread_p;
10804 if (specs->storage_class == csc_auto)
10805 error ("%qE used with %<auto%>", scspec);
10806 else if (specs->storage_class == csc_register)
10807 error ("%qE used with %<register%>", scspec);
10808 else if (specs->storage_class == csc_typedef)
10809 error ("%qE used with %<typedef%>", scspec);
10810 else
10812 specs->thread_p = true;
10813 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10814 "__thread") == 0);
10815 /* A diagnostic is not required for the use of this
10816 identifier in the implementation namespace; only diagnose
10817 it for the C11 spelling because of existing code using
10818 the other spelling. */
10819 if (!specs->thread_gnu_p)
10821 if (flag_isoc99)
10822 pedwarn_c99 (loc, OPT_Wpedantic,
10823 "ISO C99 does not support %qE", scspec);
10824 else
10825 pedwarn_c99 (loc, OPT_Wpedantic,
10826 "ISO C90 does not support %qE", scspec);
10828 specs->locations[cdw_thread] = loc;
10830 break;
10831 case RID_AUTO:
10832 n = csc_auto;
10833 break;
10834 case RID_EXTERN:
10835 n = csc_extern;
10836 /* Diagnose "__thread extern". */
10837 if (specs->thread_p && specs->thread_gnu_p)
10838 error ("%<__thread%> before %<extern%>");
10839 break;
10840 case RID_REGISTER:
10841 n = csc_register;
10842 break;
10843 case RID_STATIC:
10844 n = csc_static;
10845 /* Diagnose "__thread static". */
10846 if (specs->thread_p && specs->thread_gnu_p)
10847 error ("%<__thread%> before %<static%>");
10848 break;
10849 case RID_TYPEDEF:
10850 n = csc_typedef;
10851 break;
10852 default:
10853 gcc_unreachable ();
10855 if (n != csc_none && n == specs->storage_class)
10856 dupe = true;
10857 if (dupe)
10859 if (i == RID_THREAD)
10860 error ("duplicate %<_Thread_local%> or %<__thread%>");
10861 else
10862 error ("duplicate %qE", scspec);
10864 if (n != csc_none)
10866 if (specs->storage_class != csc_none && n != specs->storage_class)
10868 error ("multiple storage classes in declaration specifiers");
10870 else
10872 specs->storage_class = n;
10873 specs->locations[cdw_storage_class] = loc;
10874 if (n != csc_extern && n != csc_static && specs->thread_p)
10876 error ("%qs used with %qE",
10877 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10878 scspec);
10879 specs->thread_p = false;
10883 return specs;
10886 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10887 returning SPECS. */
10889 struct c_declspecs *
10890 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10892 specs->attrs = chainon (attrs, specs->attrs);
10893 specs->locations[cdw_attributes] = loc;
10894 specs->declspecs_seen_p = true;
10895 return specs;
10898 /* Add an _Alignas specifier (expression ALIGN, or type whose
10899 alignment is ALIGN) to the declaration specifiers SPECS, returning
10900 SPECS. */
10901 struct c_declspecs *
10902 declspecs_add_alignas (source_location loc,
10903 struct c_declspecs *specs, tree align)
10905 int align_log;
10906 specs->alignas_p = true;
10907 specs->locations[cdw_alignas] = loc;
10908 if (align == error_mark_node)
10909 return specs;
10910 align_log = check_user_alignment (align, true);
10911 if (align_log > specs->align_log)
10912 specs->align_log = align_log;
10913 return specs;
10916 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10917 specifiers with any other type specifier to determine the resulting
10918 type. This is where ISO C checks on complex types are made, since
10919 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10920 double". */
10922 struct c_declspecs *
10923 finish_declspecs (struct c_declspecs *specs)
10925 /* If a type was specified as a whole, we have no modifiers and are
10926 done. */
10927 if (specs->type != NULL_TREE)
10929 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10930 && !specs->signed_p && !specs->unsigned_p
10931 && !specs->complex_p);
10933 /* Set a dummy type. */
10934 if (TREE_CODE (specs->type) == ERROR_MARK)
10935 specs->type = integer_type_node;
10936 return specs;
10939 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10940 has been specified, treat it as "int" unless "_Complex" is
10941 present and there are no other specifiers. If we just have
10942 "_Complex", it is equivalent to "_Complex double", but e.g.
10943 "_Complex short" is equivalent to "_Complex short int". */
10944 if (specs->typespec_word == cts_none)
10946 if (specs->saturating_p)
10948 error_at (specs->locations[cdw_saturating],
10949 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10950 if (!targetm.fixed_point_supported_p ())
10951 error_at (specs->locations[cdw_saturating],
10952 "fixed-point types not supported for this target");
10953 specs->typespec_word = cts_fract;
10955 else if (specs->long_p || specs->short_p
10956 || specs->signed_p || specs->unsigned_p)
10958 specs->typespec_word = cts_int;
10960 else if (specs->complex_p)
10962 specs->typespec_word = cts_double;
10963 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10964 "ISO C does not support plain %<complex%> meaning "
10965 "%<double complex%>");
10967 else
10969 specs->typespec_word = cts_int;
10970 specs->default_int_p = true;
10971 /* We don't diagnose this here because grokdeclarator will
10972 give more specific diagnostics according to whether it is
10973 a function definition. */
10977 /* If "signed" was specified, record this to distinguish "int" and
10978 "signed int" in the case of a bit-field with
10979 -funsigned-bitfields. */
10980 specs->explicit_signed_p = specs->signed_p;
10982 /* Now compute the actual type. */
10983 switch (specs->typespec_word)
10985 case cts_auto_type:
10986 gcc_assert (!specs->long_p && !specs->short_p
10987 && !specs->signed_p && !specs->unsigned_p
10988 && !specs->complex_p);
10989 /* Type to be filled in later. */
10990 break;
10991 case cts_void:
10992 gcc_assert (!specs->long_p && !specs->short_p
10993 && !specs->signed_p && !specs->unsigned_p
10994 && !specs->complex_p);
10995 specs->type = void_type_node;
10996 break;
10997 case cts_bool:
10998 gcc_assert (!specs->long_p && !specs->short_p
10999 && !specs->signed_p && !specs->unsigned_p
11000 && !specs->complex_p);
11001 specs->type = boolean_type_node;
11002 break;
11003 case cts_char:
11004 gcc_assert (!specs->long_p && !specs->short_p);
11005 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11006 if (specs->signed_p)
11007 specs->type = signed_char_type_node;
11008 else if (specs->unsigned_p)
11009 specs->type = unsigned_char_type_node;
11010 else
11011 specs->type = char_type_node;
11012 if (specs->complex_p)
11014 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11015 "ISO C does not support complex integer types");
11016 specs->type = build_complex_type (specs->type);
11018 break;
11019 case cts_int_n:
11020 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11021 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11022 if (! int_n_enabled_p[specs->int_n_idx])
11023 specs->type = integer_type_node;
11024 else
11025 specs->type = (specs->unsigned_p
11026 ? int_n_trees[specs->int_n_idx].unsigned_type
11027 : int_n_trees[specs->int_n_idx].signed_type);
11028 if (specs->complex_p)
11030 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11031 "ISO C does not support complex integer types");
11032 specs->type = build_complex_type (specs->type);
11034 break;
11035 case cts_int:
11036 gcc_assert (!(specs->long_p && specs->short_p));
11037 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11038 if (specs->long_long_p)
11039 specs->type = (specs->unsigned_p
11040 ? long_long_unsigned_type_node
11041 : long_long_integer_type_node);
11042 else if (specs->long_p)
11043 specs->type = (specs->unsigned_p
11044 ? long_unsigned_type_node
11045 : long_integer_type_node);
11046 else if (specs->short_p)
11047 specs->type = (specs->unsigned_p
11048 ? short_unsigned_type_node
11049 : short_integer_type_node);
11050 else
11051 specs->type = (specs->unsigned_p
11052 ? unsigned_type_node
11053 : integer_type_node);
11054 if (specs->complex_p)
11056 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11057 "ISO C does not support complex integer types");
11058 specs->type = build_complex_type (specs->type);
11060 break;
11061 case cts_float:
11062 gcc_assert (!specs->long_p && !specs->short_p
11063 && !specs->signed_p && !specs->unsigned_p);
11064 specs->type = (specs->complex_p
11065 ? complex_float_type_node
11066 : float_type_node);
11067 break;
11068 case cts_double:
11069 gcc_assert (!specs->long_long_p && !specs->short_p
11070 && !specs->signed_p && !specs->unsigned_p);
11071 if (specs->long_p)
11073 specs->type = (specs->complex_p
11074 ? complex_long_double_type_node
11075 : long_double_type_node);
11077 else
11079 specs->type = (specs->complex_p
11080 ? complex_double_type_node
11081 : double_type_node);
11083 break;
11084 case cts_floatn_nx:
11085 gcc_assert (!specs->long_p && !specs->short_p
11086 && !specs->signed_p && !specs->unsigned_p);
11087 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11088 specs->type = integer_type_node;
11089 else if (specs->complex_p)
11090 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11091 else
11092 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11093 break;
11094 case cts_dfloat32:
11095 case cts_dfloat64:
11096 case cts_dfloat128:
11097 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11098 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11099 if (specs->typespec_word == cts_dfloat32)
11100 specs->type = dfloat32_type_node;
11101 else if (specs->typespec_word == cts_dfloat64)
11102 specs->type = dfloat64_type_node;
11103 else
11104 specs->type = dfloat128_type_node;
11105 break;
11106 case cts_fract:
11107 gcc_assert (!specs->complex_p);
11108 if (!targetm.fixed_point_supported_p ())
11109 specs->type = integer_type_node;
11110 else if (specs->saturating_p)
11112 if (specs->long_long_p)
11113 specs->type = specs->unsigned_p
11114 ? sat_unsigned_long_long_fract_type_node
11115 : sat_long_long_fract_type_node;
11116 else if (specs->long_p)
11117 specs->type = specs->unsigned_p
11118 ? sat_unsigned_long_fract_type_node
11119 : sat_long_fract_type_node;
11120 else if (specs->short_p)
11121 specs->type = specs->unsigned_p
11122 ? sat_unsigned_short_fract_type_node
11123 : sat_short_fract_type_node;
11124 else
11125 specs->type = specs->unsigned_p
11126 ? sat_unsigned_fract_type_node
11127 : sat_fract_type_node;
11129 else
11131 if (specs->long_long_p)
11132 specs->type = specs->unsigned_p
11133 ? unsigned_long_long_fract_type_node
11134 : long_long_fract_type_node;
11135 else if (specs->long_p)
11136 specs->type = specs->unsigned_p
11137 ? unsigned_long_fract_type_node
11138 : long_fract_type_node;
11139 else if (specs->short_p)
11140 specs->type = specs->unsigned_p
11141 ? unsigned_short_fract_type_node
11142 : short_fract_type_node;
11143 else
11144 specs->type = specs->unsigned_p
11145 ? unsigned_fract_type_node
11146 : fract_type_node;
11148 break;
11149 case cts_accum:
11150 gcc_assert (!specs->complex_p);
11151 if (!targetm.fixed_point_supported_p ())
11152 specs->type = integer_type_node;
11153 else if (specs->saturating_p)
11155 if (specs->long_long_p)
11156 specs->type = specs->unsigned_p
11157 ? sat_unsigned_long_long_accum_type_node
11158 : sat_long_long_accum_type_node;
11159 else if (specs->long_p)
11160 specs->type = specs->unsigned_p
11161 ? sat_unsigned_long_accum_type_node
11162 : sat_long_accum_type_node;
11163 else if (specs->short_p)
11164 specs->type = specs->unsigned_p
11165 ? sat_unsigned_short_accum_type_node
11166 : sat_short_accum_type_node;
11167 else
11168 specs->type = specs->unsigned_p
11169 ? sat_unsigned_accum_type_node
11170 : sat_accum_type_node;
11172 else
11174 if (specs->long_long_p)
11175 specs->type = specs->unsigned_p
11176 ? unsigned_long_long_accum_type_node
11177 : long_long_accum_type_node;
11178 else if (specs->long_p)
11179 specs->type = specs->unsigned_p
11180 ? unsigned_long_accum_type_node
11181 : long_accum_type_node;
11182 else if (specs->short_p)
11183 specs->type = specs->unsigned_p
11184 ? unsigned_short_accum_type_node
11185 : short_accum_type_node;
11186 else
11187 specs->type = specs->unsigned_p
11188 ? unsigned_accum_type_node
11189 : accum_type_node;
11191 break;
11192 default:
11193 gcc_unreachable ();
11196 return specs;
11199 /* Perform final processing on one file scope's declarations (or the
11200 external scope's declarations), GLOBALS. */
11202 static void
11203 c_write_global_declarations_1 (tree globals)
11205 tree decl;
11206 bool reconsider;
11208 /* Process the decls in the order they were written. */
11209 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11211 /* Check for used but undefined static functions using the C
11212 standard's definition of "used", and set TREE_NO_WARNING so
11213 that check_global_declaration doesn't repeat the check. */
11214 if (TREE_CODE (decl) == FUNCTION_DECL
11215 && DECL_INITIAL (decl) == NULL_TREE
11216 && DECL_EXTERNAL (decl)
11217 && !TREE_PUBLIC (decl))
11219 if (C_DECL_USED (decl))
11221 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11222 TREE_NO_WARNING (decl) = 1;
11224 /* For -Wunused-function warn about unused static prototypes. */
11225 else if (warn_unused_function
11226 && ! DECL_ARTIFICIAL (decl)
11227 && ! TREE_NO_WARNING (decl))
11229 warning (OPT_Wunused_function,
11230 "%q+F declared %<static%> but never defined", decl);
11231 TREE_NO_WARNING (decl) = 1;
11235 wrapup_global_declaration_1 (decl);
11240 reconsider = false;
11241 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11242 reconsider |= wrapup_global_declaration_2 (decl);
11244 while (reconsider);
11247 /* Callback to collect a source_ref from a DECL. */
11249 static void
11250 collect_source_ref_cb (tree decl)
11252 if (!DECL_IS_BUILTIN (decl))
11253 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11256 /* Preserve the external declarations scope across a garbage collect. */
11257 static GTY(()) tree ext_block;
11259 /* Collect all references relevant to SOURCE_FILE. */
11261 static void
11262 collect_all_refs (const char *source_file)
11264 tree t;
11265 unsigned i;
11267 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11268 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11270 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11273 /* Iterate over all global declarations and call CALLBACK. */
11275 static void
11276 for_each_global_decl (void (*callback) (tree decl))
11278 tree t;
11279 tree decls;
11280 tree decl;
11281 unsigned i;
11283 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11285 decls = DECL_INITIAL (t);
11286 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11287 callback (decl);
11290 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11291 callback (decl);
11294 /* Perform any final parser cleanups and generate initial debugging
11295 information. */
11297 void
11298 c_parse_final_cleanups (void)
11300 tree t;
11301 unsigned i;
11303 /* We don't want to do this if generating a PCH. */
11304 if (pch_file)
11305 return;
11307 timevar_stop (TV_PHASE_PARSING);
11308 timevar_start (TV_PHASE_DEFERRED);
11310 /* Do the Objective-C stuff. This is where all the Objective-C
11311 module stuff gets generated (symtab, class/protocol/selector
11312 lists etc). */
11313 if (c_dialect_objc ())
11314 objc_write_global_declarations ();
11316 /* Close the external scope. */
11317 ext_block = pop_scope ();
11318 external_scope = 0;
11319 gcc_assert (!current_scope);
11321 /* Handle -fdump-ada-spec[-slim]. */
11322 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11324 /* Build a table of files to generate specs for */
11325 if (flag_dump_ada_spec_slim)
11326 collect_source_ref (main_input_filename);
11327 else
11328 for_each_global_decl (collect_source_ref_cb);
11330 dump_ada_specs (collect_all_refs, NULL);
11333 /* Process all file scopes in this compilation, and the external_scope,
11334 through wrapup_global_declarations. */
11335 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11336 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11337 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11339 timevar_stop (TV_PHASE_DEFERRED);
11340 timevar_start (TV_PHASE_PARSING);
11342 ext_block = NULL;
11345 /* Register reserved keyword WORD as qualifier for address space AS. */
11347 void
11348 c_register_addr_space (const char *word, addr_space_t as)
11350 int rid = RID_FIRST_ADDR_SPACE + as;
11351 tree id;
11353 /* Address space qualifiers are only supported
11354 in C with GNU extensions enabled. */
11355 if (c_dialect_objc () || flag_no_asm)
11356 return;
11358 id = get_identifier (word);
11359 C_SET_RID_CODE (id, rid);
11360 C_IS_RESERVED_WORD (id) = 1;
11361 ridpointers [rid] = id;
11364 /* Return identifier to look up for omp declare reduction. */
11366 tree
11367 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11369 const char *p = NULL;
11370 switch (reduction_code)
11372 case PLUS_EXPR: p = "+"; break;
11373 case MULT_EXPR: p = "*"; break;
11374 case MINUS_EXPR: p = "-"; break;
11375 case BIT_AND_EXPR: p = "&"; break;
11376 case BIT_XOR_EXPR: p = "^"; break;
11377 case BIT_IOR_EXPR: p = "|"; break;
11378 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11379 case TRUTH_ORIF_EXPR: p = "||"; break;
11380 case MIN_EXPR: p = "min"; break;
11381 case MAX_EXPR: p = "max"; break;
11382 default:
11383 break;
11386 if (p == NULL)
11388 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11389 return error_mark_node;
11390 p = IDENTIFIER_POINTER (reduction_id);
11393 const char prefix[] = "omp declare reduction ";
11394 size_t lenp = sizeof (prefix);
11395 size_t len = strlen (p);
11396 char *name = XALLOCAVEC (char, lenp + len);
11397 memcpy (name, prefix, lenp - 1);
11398 memcpy (name + lenp - 1, p, len + 1);
11399 return get_identifier (name);
11402 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11403 VAR_DECL, bind it into the current scope and return it. */
11405 tree
11406 c_omp_reduction_decl (tree reduction_id)
11408 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11409 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11410 return b->decl;
11412 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11413 reduction_id, integer_type_node);
11414 DECL_ARTIFICIAL (decl) = 1;
11415 DECL_EXTERNAL (decl) = 1;
11416 TREE_STATIC (decl) = 1;
11417 TREE_PUBLIC (decl) = 0;
11418 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11419 return decl;
11422 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11424 tree
11425 c_omp_reduction_lookup (tree reduction_id, tree type)
11427 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11428 while (b)
11430 tree t;
11431 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11432 if (comptypes (TREE_PURPOSE (t), type))
11433 return TREE_VALUE (t);
11434 b = b->shadowed;
11436 return error_mark_node;
11439 /* Helper function called via walk_tree, to diagnose invalid
11440 #pragma omp declare reduction combiners or initializers. */
11442 tree
11443 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11445 tree *vars = (tree *) data;
11446 if (SSA_VAR_P (*tp)
11447 && !DECL_ARTIFICIAL (*tp)
11448 && *tp != vars[0]
11449 && *tp != vars[1])
11451 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11452 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11453 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11454 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11455 *tp);
11456 else
11457 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11458 "to variable %qD which is not %<omp_priv%> nor "
11459 "%<omp_orig%>",
11460 *tp);
11461 return *tp;
11463 return NULL_TREE;
11466 #include "gt-c-c-decl.h"