builtins.def: (_Float<N> and _Float<N>X BUILT_IN_CEIL): Add _Float<N> and _Float...
[official-gcc.git] / gcc / c / c-decl.c
blobd3294c390721c73bb53b40f873bd875e22e06177
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #define INCLUDE_UNIQUE_PTR
29 #include "system.h"
30 #include "coretypes.h"
31 #include "target.h"
32 #include "function.h"
33 #include "c-tree.h"
34 #include "timevar.h"
35 #include "stringpool.h"
36 #include "cgraph.h"
37 #include "intl.h"
38 #include "print-tree.h"
39 #include "stor-layout.h"
40 #include "varasm.h"
41 #include "attribs.h"
42 #include "toplev.h"
43 #include "debug.h"
44 #include "c-family/c-objc.h"
45 #include "c-family/c-pragma.h"
46 #include "c-family/c-ubsan.h"
47 #include "c-lang.h"
48 #include "langhooks.h"
49 #include "tree-iterator.h"
50 #include "dumpfile.h"
51 #include "plugin.h"
52 #include "c-family/c-ada-spec.h"
53 #include "builtins.h"
54 #include "spellcheck-tree.h"
55 #include "gcc-rich-location.h"
56 #include "asan.h"
57 #include "c-family/name-hint.h"
58 #include "c-family/known-headers.h"
59 #include "c-family/c-spellcheck.h"
61 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
62 enum decl_context
63 { NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
69 /* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
72 deprecated items. */
74 enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
80 /* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82 tree pending_invalid_xref;
84 /* File and line to appear in the eventual error message. */
85 location_t pending_invalid_xref_location;
87 /* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
91 static location_t current_function_prototype_locus;
93 /* Whether this prototype was built-in. */
95 static bool current_function_prototype_built_in;
97 /* The argument type information of this prototype. */
99 static tree current_function_prototype_arg_types;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* A list of decls to be made automatically visible in each file scope. */
120 static GTY(()) tree visible_builtins;
122 /* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
125 int current_function_returns_value;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
130 int current_function_returns_null;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
135 int current_function_returns_abnormally;
137 /* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
140 static int warn_about_return_type;
142 /* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
145 static bool undef_nested_function;
147 /* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149 int current_omp_declare_target_attribute;
151 /* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
187 struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
203 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
208 /* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
212 struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The binding oracle; see c-tree.h. */
224 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
226 /* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228 #define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
231 static inline struct c_binding* *
232 i_symbol_binding (tree node)
234 struct lang_identifier *lid
235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
237 if (lid->symbol_binding == NULL
238 && c_binding_oracle != NULL
239 && !I_SYMBOL_CHECKED (node))
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL, node);
247 return &lid->symbol_binding;
250 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
252 #define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
255 /* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257 #define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
260 static inline struct c_binding **
261 i_tag_binding (tree node)
263 struct lang_identifier *lid
264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
266 if (lid->tag_binding == NULL
267 && c_binding_oracle != NULL
268 && !I_TAG_CHECKED (node))
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node) = 1;
273 c_binding_oracle (C_ORACLE_TAG, node);
276 return &lid->tag_binding;
279 #define I_TAG_BINDING(node) (*i_tag_binding (node))
281 #define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
284 /* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286 #define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
289 static inline struct c_binding **
290 i_label_binding (tree node)
292 struct lang_identifier *lid
293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
295 if (lid->label_binding == NULL
296 && c_binding_oracle != NULL
297 && !I_LABEL_CHECKED (node))
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node) = 1;
302 c_binding_oracle (C_ORACLE_LABEL, node);
305 return &lid->label_binding;
308 #define I_LABEL_BINDING(node) (*i_label_binding (node))
310 #define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
313 /* The resulting tree type. */
315 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
318 union tree_node GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
320 generic;
321 struct lang_identifier GTY ((tag ("1"))) identifier;
324 /* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
330 struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
350 /* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
354 struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
361 typedef struct c_goto_bindings *c_goto_bindings_p;
363 /* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
366 struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
383 /* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
388 Most declarations are recorded in the current scope.
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
409 undefined.
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
416 this scope.
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
422 list here.)
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
428 struct GTY((chain_next ("%h.outer"))) c_scope {
429 /* The scope containing this one. */
430 struct c_scope *outer;
432 /* The next outermost function scope. */
433 struct c_scope *outer_function;
435 /* All bindings in this scope. */
436 struct c_binding *bindings;
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
440 tree blocks;
441 tree blocks_last;
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth : 28;
447 /* True if we are currently filling this scope with parameter
448 declarations. */
449 BOOL_BITFIELD parm_flag : 1;
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec : 1;
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls : 1;
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body : 1;
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep : 1;
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64 : 1;
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings : 1;
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl : 1;
485 /* The scope currently in effect. */
487 static GTY(()) struct c_scope *current_scope;
489 /* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
493 static GTY(()) struct c_scope *current_function_scope;
495 /* The C file scope. This is reset for each input translation unit. */
497 static GTY(()) struct c_scope *file_scope;
499 /* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
502 static GTY(()) struct c_scope *external_scope;
504 /* A chain of c_scope structures awaiting reuse. */
506 static GTY((deletable)) struct c_scope *scope_freelist;
508 /* A chain of c_binding structures awaiting reuse. */
510 static GTY((deletable)) struct c_binding *binding_freelist;
512 /* Append VAR to LIST in scope SCOPE. */
513 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
515 tree d_ = (decl); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
518 else \
519 s_->list = d_; \
520 s_->list##_last = d_; \
521 } while (0)
523 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
527 if (t_->to##_last) \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
529 else \
530 t_->to = f_->from; \
531 t_->to##_last = f_->from##_last; \
532 } while (0)
534 /* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
539 struct GTY((chain_next ("%h.next"))) c_inline_static {
540 /* The location for a diagnostic. */
541 location_t location;
543 /* The function that may be an inline definition. */
544 tree function;
546 /* The object or function referenced. */
547 tree static_decl;
549 /* What sort of reference this is. */
550 enum c_inline_static_type type;
552 /* The next such structure or NULL. */
553 struct c_inline_static *next;
556 /* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558 static GTY(()) struct c_inline_static *c_inline_statics;
560 /* True means unconditionally make a BLOCK for the next scope pushed. */
562 static bool keep_next_level_flag;
564 /* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
568 static bool next_is_function_body;
570 /* A vector of pointers to c_binding structures. */
572 typedef struct c_binding *c_binding_ptr;
574 /* Information that we keep for a struct or union while it is being
575 parsed. */
577 struct c_struct_parse_info
579 /* If warn_cxx_compat, a list of types defined within this
580 struct. */
581 auto_vec<tree> struct_types;
582 /* If warn_cxx_compat, a list of field names which have bindings,
583 and which are defined in this struct, but which are not defined
584 in any enclosing struct. This is used to clear the in_struct
585 field of the c_bindings structure. */
586 auto_vec<c_binding_ptr> fields;
587 /* If warn_cxx_compat, a list of typedef names used when defining
588 fields in this struct. */
589 auto_vec<tree> typedefs_seen;
592 /* Information for the struct or union currently being parsed, or
593 NULL if not parsing a struct or union. */
594 static struct c_struct_parse_info *struct_parse_info;
596 /* Forward declarations. */
597 static tree lookup_name_in_scope (tree, struct c_scope *);
598 static tree c_make_fname_decl (location_t, tree, int);
599 static tree grokdeclarator (const struct c_declarator *,
600 struct c_declspecs *,
601 enum decl_context, bool, tree *, tree *, tree *,
602 bool *, enum deprecated_states);
603 static tree grokparms (struct c_arg_info *, bool);
604 static void layout_array_type (tree);
605 static void warn_defaults_to (location_t, int, const char *, ...)
606 ATTRIBUTE_GCC_DIAG(3,4);
608 /* T is a statement. Add it to the statement-tree. This is the
609 C/ObjC version--C++ has a slightly different version of this
610 function. */
612 tree
613 add_stmt (tree t)
615 enum tree_code code = TREE_CODE (t);
617 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
619 if (!EXPR_HAS_LOCATION (t))
620 SET_EXPR_LOCATION (t, input_location);
623 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
624 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
626 /* Add T to the statement-tree. Non-side-effect statements need to be
627 recorded during statement expressions. */
628 if (!building_stmt_list_p ())
629 push_stmt_list ();
630 append_to_statement_list_force (t, &cur_stmt_list);
632 return t;
635 /* Build a pointer type using the default pointer mode. */
637 static tree
638 c_build_pointer_type (tree to_type)
640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
641 : TYPE_ADDR_SPACE (to_type);
642 machine_mode pointer_mode;
644 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
645 pointer_mode = targetm.addr_space.pointer_mode (as);
646 else
647 pointer_mode = c_default_pointer_mode;
648 return build_pointer_type_for_mode (to_type, pointer_mode, false);
652 /* Return true if we will want to say something if a goto statement
653 crosses DECL. */
655 static bool
656 decl_jump_unsafe (tree decl)
658 if (error_operand_p (decl))
659 return false;
661 /* Always warn about crossing variably modified types. */
662 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
663 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
664 return true;
666 /* Otherwise, only warn if -Wgoto-misses-init and this is an
667 initialized automatic decl. */
668 if (warn_jump_misses_init
669 && VAR_P (decl)
670 && !TREE_STATIC (decl)
671 && DECL_INITIAL (decl) != NULL_TREE)
672 return true;
674 return false;
678 void
679 c_print_identifier (FILE *file, tree node, int indent)
681 void (*save) (enum c_oracle_request, tree identifier);
683 /* Temporarily hide any binding oracle. Without this, calls to
684 debug_tree from the debugger will end up calling into the oracle,
685 making for a confusing debug session. As the oracle isn't needed
686 here for normal operation, it's simplest to suppress it. */
687 save = c_binding_oracle;
688 c_binding_oracle = NULL;
690 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
691 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
692 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
693 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
695 tree rid = ridpointers[C_RID_CODE (node)];
696 indent_to (file, indent + 4);
697 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
698 (void *) rid, IDENTIFIER_POINTER (rid));
701 c_binding_oracle = save;
704 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
705 which may be any of several kinds of DECL or TYPE or error_mark_node,
706 in the scope SCOPE. */
707 static void
708 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
709 bool nested, location_t locus)
711 struct c_binding *b, **here;
713 if (binding_freelist)
715 b = binding_freelist;
716 binding_freelist = b->prev;
718 else
719 b = ggc_alloc<c_binding> ();
721 b->shadowed = 0;
722 b->decl = decl;
723 b->id = name;
724 b->depth = scope->depth;
725 b->invisible = invisible;
726 b->nested = nested;
727 b->inner_comp = 0;
728 b->in_struct = 0;
729 b->locus = locus;
731 b->u.type = NULL;
733 b->prev = scope->bindings;
734 scope->bindings = b;
736 if (decl_jump_unsafe (decl))
737 scope->has_jump_unsafe_decl = 1;
739 if (!name)
740 return;
742 switch (TREE_CODE (decl))
744 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
745 case ENUMERAL_TYPE:
746 case UNION_TYPE:
747 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
748 case VAR_DECL:
749 case FUNCTION_DECL:
750 case TYPE_DECL:
751 case CONST_DECL:
752 case PARM_DECL:
753 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
755 default:
756 gcc_unreachable ();
759 /* Locate the appropriate place in the chain of shadowed decls
760 to insert this binding. Normally, scope == current_scope and
761 this does nothing. */
762 while (*here && (*here)->depth > scope->depth)
763 here = &(*here)->shadowed;
765 b->shadowed = *here;
766 *here = b;
769 /* Clear the binding structure B, stick it on the binding_freelist,
770 and return the former value of b->prev. This is used by pop_scope
771 and get_parm_info to iterate destructively over all the bindings
772 from a given scope. */
773 static struct c_binding *
774 free_binding_and_advance (struct c_binding *b)
776 struct c_binding *prev = b->prev;
778 memset (b, 0, sizeof (struct c_binding));
779 b->prev = binding_freelist;
780 binding_freelist = b;
782 return prev;
785 /* Bind a label. Like bind, but skip fields which aren't used for
786 labels, and add the LABEL_VARS value. */
787 static void
788 bind_label (tree name, tree label, struct c_scope *scope,
789 struct c_label_vars *label_vars)
791 struct c_binding *b;
793 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
794 UNKNOWN_LOCATION);
796 scope->has_label_bindings = true;
798 b = scope->bindings;
799 gcc_assert (b->decl == label);
800 label_vars->shadowed = b->u.label;
801 b->u.label = label_vars;
804 /* Hook called at end of compilation to assume 1 elt
805 for a file-scope tentative array defn that wasn't complete before. */
807 void
808 c_finish_incomplete_decl (tree decl)
810 if (VAR_P (decl))
812 tree type = TREE_TYPE (decl);
813 if (type != error_mark_node
814 && TREE_CODE (type) == ARRAY_TYPE
815 && !DECL_EXTERNAL (decl)
816 && TYPE_DOMAIN (type) == NULL_TREE)
818 warning_at (DECL_SOURCE_LOCATION (decl),
819 0, "array %q+D assumed to have one element", decl);
821 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
823 relayout_decl (decl);
828 /* Record that inline function FUNC contains a reference (location
829 LOC) to static DECL (file-scope or function-local according to
830 TYPE). */
832 void
833 record_inline_static (location_t loc, tree func, tree decl,
834 enum c_inline_static_type type)
836 c_inline_static *csi = ggc_alloc<c_inline_static> ();
837 csi->location = loc;
838 csi->function = func;
839 csi->static_decl = decl;
840 csi->type = type;
841 csi->next = c_inline_statics;
842 c_inline_statics = csi;
845 /* Check for references to static declarations in inline functions at
846 the end of the translation unit and diagnose them if the functions
847 are still inline definitions. */
849 static void
850 check_inline_statics (void)
852 struct c_inline_static *csi;
853 for (csi = c_inline_statics; csi; csi = csi->next)
855 if (DECL_EXTERNAL (csi->function))
856 switch (csi->type)
858 case csi_internal:
859 pedwarn (csi->location, 0,
860 "%qD is static but used in inline function %qD "
861 "which is not static", csi->static_decl, csi->function);
862 break;
863 case csi_modifiable:
864 pedwarn (csi->location, 0,
865 "%q+D is static but declared in inline function %qD "
866 "which is not static", csi->static_decl, csi->function);
867 break;
868 default:
869 gcc_unreachable ();
872 c_inline_statics = NULL;
875 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
876 for the current state, otherwise set it to uninitialized. */
878 static void
879 set_spot_bindings (struct c_spot_bindings *p, bool defining)
881 if (defining)
883 p->scope = current_scope;
884 p->bindings_in_scope = current_scope->bindings;
886 else
888 p->scope = NULL;
889 p->bindings_in_scope = NULL;
891 p->stmt_exprs = 0;
892 p->left_stmt_expr = false;
895 /* Update spot bindings P as we pop out of SCOPE. Return true if we
896 should push decls for a label. */
898 static bool
899 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
901 if (p->scope != scope)
903 /* This label or goto is defined in some other scope, or it is a
904 label which is not yet defined. There is nothing to
905 update. */
906 return false;
909 /* Adjust the spot bindings to refer to the bindings already defined
910 in the enclosing scope. */
911 p->scope = scope->outer;
912 p->bindings_in_scope = p->scope->bindings;
914 return true;
917 /* The Objective-C front-end often needs to determine the current scope. */
919 void *
920 objc_get_current_scope (void)
922 return current_scope;
925 /* The following function is used only by Objective-C. It needs to live here
926 because it accesses the innards of c_scope. */
928 void
929 objc_mark_locals_volatile (void *enclosing_blk)
931 struct c_scope *scope;
932 struct c_binding *b;
934 for (scope = current_scope;
935 scope && scope != enclosing_blk;
936 scope = scope->outer)
938 for (b = scope->bindings; b; b = b->prev)
939 objc_volatilize_decl (b->decl);
941 /* Do not climb up past the current function. */
942 if (scope->function_body)
943 break;
947 /* Return true if we are in the global binding level. */
949 bool
950 global_bindings_p (void)
952 return current_scope == file_scope;
955 void
956 keep_next_level (void)
958 keep_next_level_flag = true;
961 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
963 void
964 set_float_const_decimal64 (void)
966 current_scope->float_const_decimal64 = true;
969 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
971 void
972 clear_float_const_decimal64 (void)
974 current_scope->float_const_decimal64 = false;
977 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
979 bool
980 float_const_decimal64_p (void)
982 return current_scope->float_const_decimal64;
985 /* Identify this scope as currently being filled with parameters. */
987 void
988 declare_parm_level (void)
990 current_scope->parm_flag = true;
993 void
994 push_scope (void)
996 if (next_is_function_body)
998 /* This is the transition from the parameters to the top level
999 of the function body. These are the same scope
1000 (C99 6.2.1p4,6) so we do not push another scope structure.
1001 next_is_function_body is set only by store_parm_decls, which
1002 in turn is called when and only when we are about to
1003 encounter the opening curly brace for the function body.
1005 The outermost block of a function always gets a BLOCK node,
1006 because the debugging output routines expect that each
1007 function has at least one BLOCK. */
1008 current_scope->parm_flag = false;
1009 current_scope->function_body = true;
1010 current_scope->keep = true;
1011 current_scope->outer_function = current_function_scope;
1012 current_function_scope = current_scope;
1014 keep_next_level_flag = false;
1015 next_is_function_body = false;
1017 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1018 if (current_scope->outer)
1019 current_scope->float_const_decimal64
1020 = current_scope->outer->float_const_decimal64;
1021 else
1022 current_scope->float_const_decimal64 = false;
1024 else
1026 struct c_scope *scope;
1027 if (scope_freelist)
1029 scope = scope_freelist;
1030 scope_freelist = scope->outer;
1032 else
1033 scope = ggc_cleared_alloc<c_scope> ();
1035 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1036 if (current_scope)
1037 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1038 else
1039 scope->float_const_decimal64 = false;
1041 scope->keep = keep_next_level_flag;
1042 scope->outer = current_scope;
1043 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1045 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1046 possible. */
1047 if (current_scope && scope->depth == 0)
1049 scope->depth--;
1050 sorry ("GCC supports only %u nested scopes", scope->depth);
1053 current_scope = scope;
1054 keep_next_level_flag = false;
1058 /* This is called when we are leaving SCOPE. For each label defined
1059 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1060 These are the decls whose initialization will be skipped by a goto
1061 later in the function. */
1063 static void
1064 update_label_decls (struct c_scope *scope)
1066 struct c_scope *s;
1068 s = scope;
1069 while (s != NULL)
1071 if (s->has_label_bindings)
1073 struct c_binding *b;
1075 for (b = s->bindings; b != NULL; b = b->prev)
1077 struct c_label_vars *label_vars;
1078 struct c_binding *b1;
1079 bool hjud;
1080 unsigned int ix;
1081 struct c_goto_bindings *g;
1083 if (TREE_CODE (b->decl) != LABEL_DECL)
1084 continue;
1085 label_vars = b->u.label;
1087 b1 = label_vars->label_bindings.bindings_in_scope;
1088 if (label_vars->label_bindings.scope == NULL)
1089 hjud = false;
1090 else
1091 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1092 if (update_spot_bindings (scope, &label_vars->label_bindings))
1094 /* This label is defined in this scope. */
1095 if (hjud)
1097 for (; b1 != NULL; b1 = b1->prev)
1099 /* A goto from later in the function to this
1100 label will never see the initialization
1101 of B1, if any. Save it to issue a
1102 warning if needed. */
1103 if (decl_jump_unsafe (b1->decl))
1104 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1109 /* Update the bindings of any goto statements associated
1110 with this label. */
1111 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1112 update_spot_bindings (scope, &g->goto_bindings);
1116 /* Don't search beyond the current function. */
1117 if (s == current_function_scope)
1118 break;
1120 s = s->outer;
1124 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1126 static void
1127 set_type_context (tree type, tree context)
1129 for (type = TYPE_MAIN_VARIANT (type); type;
1130 type = TYPE_NEXT_VARIANT (type))
1131 TYPE_CONTEXT (type) = context;
1134 /* Exit a scope. Restore the state of the identifier-decl mappings
1135 that were in effect when this scope was entered. Return a BLOCK
1136 node containing all the DECLs in this scope that are of interest
1137 to debug info generation. */
1139 tree
1140 pop_scope (void)
1142 struct c_scope *scope = current_scope;
1143 tree block, context, p;
1144 struct c_binding *b;
1146 bool functionbody = scope->function_body;
1147 bool keep = functionbody || scope->keep || scope->bindings;
1149 update_label_decls (scope);
1151 /* If appropriate, create a BLOCK to record the decls for the life
1152 of this function. */
1153 block = NULL_TREE;
1154 if (keep)
1156 block = make_node (BLOCK);
1157 BLOCK_SUBBLOCKS (block) = scope->blocks;
1158 TREE_USED (block) = 1;
1160 /* In each subblock, record that this is its superior. */
1161 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1162 BLOCK_SUPERCONTEXT (p) = block;
1164 BLOCK_VARS (block) = NULL_TREE;
1167 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1168 scope must be set so that they point to the appropriate
1169 construct, i.e. either to the current FUNCTION_DECL node, or
1170 else to the BLOCK node we just constructed.
1172 Note that for tagged types whose scope is just the formal
1173 parameter list for some function type specification, we can't
1174 properly set their TYPE_CONTEXTs here, because we don't have a
1175 pointer to the appropriate FUNCTION_TYPE node readily available
1176 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1177 type nodes get set in `grokdeclarator' as soon as we have created
1178 the FUNCTION_TYPE node which will represent the "scope" for these
1179 "parameter list local" tagged types. */
1180 if (scope->function_body)
1181 context = current_function_decl;
1182 else if (scope == file_scope)
1184 tree file_decl
1185 = build_translation_unit_decl (get_identifier (main_input_filename));
1186 context = file_decl;
1187 debug_hooks->register_main_translation_unit (file_decl);
1189 else
1190 context = block;
1192 /* Clear all bindings in this scope. */
1193 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1195 p = b->decl;
1196 switch (TREE_CODE (p))
1198 case LABEL_DECL:
1199 /* Warnings for unused labels, errors for undefined labels. */
1200 if (TREE_USED (p) && !DECL_INITIAL (p))
1202 error ("label %q+D used but not defined", p);
1203 DECL_INITIAL (p) = error_mark_node;
1205 else
1206 warn_for_unused_label (p);
1208 /* Labels go in BLOCK_VARS. */
1209 DECL_CHAIN (p) = BLOCK_VARS (block);
1210 BLOCK_VARS (block) = p;
1211 gcc_assert (I_LABEL_BINDING (b->id) == b);
1212 I_LABEL_BINDING (b->id) = b->shadowed;
1214 /* Also pop back to the shadowed label_vars. */
1215 release_tree_vector (b->u.label->decls_in_scope);
1216 b->u.label = b->u.label->shadowed;
1217 break;
1219 case ENUMERAL_TYPE:
1220 case UNION_TYPE:
1221 case RECORD_TYPE:
1222 set_type_context (p, context);
1224 /* Types may not have tag-names, in which case the type
1225 appears in the bindings list with b->id NULL. */
1226 if (b->id)
1228 gcc_assert (I_TAG_BINDING (b->id) == b);
1229 I_TAG_BINDING (b->id) = b->shadowed;
1231 break;
1233 case FUNCTION_DECL:
1234 /* Propagate TREE_ADDRESSABLE from nested functions to their
1235 containing functions. */
1236 if (!TREE_ASM_WRITTEN (p)
1237 && DECL_INITIAL (p) != NULL_TREE
1238 && TREE_ADDRESSABLE (p)
1239 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1240 && DECL_ABSTRACT_ORIGIN (p) != p)
1241 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1242 if (!DECL_EXTERNAL (p)
1243 && !DECL_INITIAL (p)
1244 && scope != file_scope
1245 && scope != external_scope)
1247 error ("nested function %q+D declared but never defined", p);
1248 undef_nested_function = true;
1250 else if (DECL_DECLARED_INLINE_P (p)
1251 && TREE_PUBLIC (p)
1252 && !DECL_INITIAL (p))
1254 /* C99 6.7.4p6: "a function with external linkage... declared
1255 with an inline function specifier ... shall also be defined
1256 in the same translation unit." */
1257 if (!flag_gnu89_inline
1258 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1259 && scope != external_scope)
1260 pedwarn (input_location, 0,
1261 "inline function %q+D declared but never defined", p);
1262 DECL_EXTERNAL (p) = 1;
1265 goto common_symbol;
1267 case VAR_DECL:
1268 /* Warnings for unused variables. */
1269 if ((!TREE_USED (p) || !DECL_READ_P (p))
1270 && !TREE_NO_WARNING (p)
1271 && !DECL_IN_SYSTEM_HEADER (p)
1272 && DECL_NAME (p)
1273 && !DECL_ARTIFICIAL (p)
1274 && scope != file_scope
1275 && scope != external_scope)
1277 if (!TREE_USED (p))
1278 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1279 else if (DECL_CONTEXT (p) == current_function_decl)
1280 warning_at (DECL_SOURCE_LOCATION (p),
1281 OPT_Wunused_but_set_variable,
1282 "variable %qD set but not used", p);
1285 if (b->inner_comp)
1287 error ("type of array %q+D completed incompatibly with"
1288 " implicit initialization", p);
1291 /* Fall through. */
1292 case TYPE_DECL:
1293 case CONST_DECL:
1294 common_symbol:
1295 /* All of these go in BLOCK_VARS, but only if this is the
1296 binding in the home scope. */
1297 if (!b->nested)
1299 DECL_CHAIN (p) = BLOCK_VARS (block);
1300 BLOCK_VARS (block) = p;
1302 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1304 /* For block local externs add a special
1305 DECL_EXTERNAL decl for debug info generation. */
1306 tree extp = copy_node (p);
1308 DECL_EXTERNAL (extp) = 1;
1309 TREE_STATIC (extp) = 0;
1310 TREE_PUBLIC (extp) = 1;
1311 DECL_INITIAL (extp) = NULL_TREE;
1312 DECL_LANG_SPECIFIC (extp) = NULL;
1313 DECL_CONTEXT (extp) = current_function_decl;
1314 if (TREE_CODE (p) == FUNCTION_DECL)
1316 DECL_RESULT (extp) = NULL_TREE;
1317 DECL_SAVED_TREE (extp) = NULL_TREE;
1318 DECL_STRUCT_FUNCTION (extp) = NULL;
1320 if (b->locus != UNKNOWN_LOCATION)
1321 DECL_SOURCE_LOCATION (extp) = b->locus;
1322 DECL_CHAIN (extp) = BLOCK_VARS (block);
1323 BLOCK_VARS (block) = extp;
1325 /* If this is the file scope set DECL_CONTEXT of each decl to
1326 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1327 work. */
1328 if (scope == file_scope)
1330 DECL_CONTEXT (p) = context;
1331 if (TREE_CODE (p) == TYPE_DECL
1332 && TREE_TYPE (p) != error_mark_node)
1333 set_type_context (TREE_TYPE (p), context);
1336 gcc_fallthrough ();
1337 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1338 already been put there by store_parm_decls. Unused-
1339 parameter warnings are handled by function.c.
1340 error_mark_node obviously does not go in BLOCK_VARS and
1341 does not get unused-variable warnings. */
1342 case PARM_DECL:
1343 case ERROR_MARK:
1344 /* It is possible for a decl not to have a name. We get
1345 here with b->id NULL in this case. */
1346 if (b->id)
1348 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1349 I_SYMBOL_BINDING (b->id) = b->shadowed;
1350 if (b->shadowed && b->shadowed->u.type)
1351 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1353 break;
1355 default:
1356 gcc_unreachable ();
1361 /* Dispose of the block that we just made inside some higher level. */
1362 if ((scope->function_body || scope == file_scope) && context)
1364 DECL_INITIAL (context) = block;
1365 BLOCK_SUPERCONTEXT (block) = context;
1367 else if (scope->outer)
1369 if (block)
1370 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1371 /* If we did not make a block for the scope just exited, any
1372 blocks made for inner scopes must be carried forward so they
1373 will later become subblocks of something else. */
1374 else if (scope->blocks)
1375 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1378 /* Pop the current scope, and free the structure for reuse. */
1379 current_scope = scope->outer;
1380 if (scope->function_body)
1381 current_function_scope = scope->outer_function;
1383 memset (scope, 0, sizeof (struct c_scope));
1384 scope->outer = scope_freelist;
1385 scope_freelist = scope;
1387 return block;
1390 void
1391 push_file_scope (void)
1393 tree decl;
1395 if (file_scope)
1396 return;
1398 push_scope ();
1399 file_scope = current_scope;
1401 start_fname_decls ();
1403 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1404 bind (DECL_NAME (decl), decl, file_scope,
1405 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1408 void
1409 pop_file_scope (void)
1411 /* In case there were missing closebraces, get us back to the global
1412 binding level. */
1413 while (current_scope != file_scope)
1414 pop_scope ();
1416 /* __FUNCTION__ is defined at file scope (""). This
1417 call may not be necessary as my tests indicate it
1418 still works without it. */
1419 finish_fname_decls ();
1421 check_inline_statics ();
1423 /* This is the point to write out a PCH if we're doing that.
1424 In that case we do not want to do anything else. */
1425 if (pch_file)
1427 c_common_write_pch ();
1428 /* Ensure even the callers don't try to finalize the CU. */
1429 flag_syntax_only = 1;
1430 return;
1433 /* Pop off the file scope and close this translation unit. */
1434 pop_scope ();
1435 file_scope = 0;
1437 maybe_apply_pending_pragma_weaks ();
1440 /* Adjust the bindings for the start of a statement expression. */
1442 void
1443 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1445 struct c_scope *scope;
1447 for (scope = current_scope; scope != NULL; scope = scope->outer)
1449 struct c_binding *b;
1451 if (!scope->has_label_bindings)
1452 continue;
1454 for (b = scope->bindings; b != NULL; b = b->prev)
1456 struct c_label_vars *label_vars;
1457 unsigned int ix;
1458 struct c_goto_bindings *g;
1460 if (TREE_CODE (b->decl) != LABEL_DECL)
1461 continue;
1462 label_vars = b->u.label;
1463 ++label_vars->label_bindings.stmt_exprs;
1464 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1465 ++g->goto_bindings.stmt_exprs;
1469 if (switch_bindings != NULL)
1470 ++switch_bindings->stmt_exprs;
1473 /* Adjust the bindings for the end of a statement expression. */
1475 void
1476 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1478 struct c_scope *scope;
1480 for (scope = current_scope; scope != NULL; scope = scope->outer)
1482 struct c_binding *b;
1484 if (!scope->has_label_bindings)
1485 continue;
1487 for (b = scope->bindings; b != NULL; b = b->prev)
1489 struct c_label_vars *label_vars;
1490 unsigned int ix;
1491 struct c_goto_bindings *g;
1493 if (TREE_CODE (b->decl) != LABEL_DECL)
1494 continue;
1495 label_vars = b->u.label;
1496 --label_vars->label_bindings.stmt_exprs;
1497 if (label_vars->label_bindings.stmt_exprs < 0)
1499 label_vars->label_bindings.left_stmt_expr = true;
1500 label_vars->label_bindings.stmt_exprs = 0;
1502 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1504 --g->goto_bindings.stmt_exprs;
1505 if (g->goto_bindings.stmt_exprs < 0)
1507 g->goto_bindings.left_stmt_expr = true;
1508 g->goto_bindings.stmt_exprs = 0;
1514 if (switch_bindings != NULL)
1516 --switch_bindings->stmt_exprs;
1517 gcc_assert (switch_bindings->stmt_exprs >= 0);
1521 /* Push a definition or a declaration of struct, union or enum tag "name".
1522 "type" should be the type node.
1523 We assume that the tag "name" is not already defined, and has a location
1524 of LOC.
1526 Note that the definition may really be just a forward reference.
1527 In that case, the TYPE_SIZE will be zero. */
1529 static void
1530 pushtag (location_t loc, tree name, tree type)
1532 /* Record the identifier as the type's name if it has none. */
1533 if (name && !TYPE_NAME (type))
1534 TYPE_NAME (type) = name;
1535 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1537 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1538 tagged type we just added to the current scope. This fake
1539 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1540 to output a representation of a tagged type, and it also gives
1541 us a convenient place to record the "scope start" address for the
1542 tagged type. */
1544 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1545 TYPE_DECL, NULL_TREE, type));
1547 /* An approximation for now, so we can tell this is a function-scope tag.
1548 This will be updated in pop_scope. */
1549 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1551 if (warn_cxx_compat && name != NULL_TREE)
1553 struct c_binding *b = I_SYMBOL_BINDING (name);
1555 if (b != NULL
1556 && b->decl != NULL_TREE
1557 && TREE_CODE (b->decl) == TYPE_DECL
1558 && (B_IN_CURRENT_SCOPE (b)
1559 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1560 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1561 != TYPE_MAIN_VARIANT (type)))
1563 if (warning_at (loc, OPT_Wc___compat,
1564 ("using %qD as both a typedef and a tag is "
1565 "invalid in C++"), b->decl)
1566 && b->locus != UNKNOWN_LOCATION)
1567 inform (b->locus, "originally defined here");
1572 /* An exported interface to pushtag. This is used by the gdb plugin's
1573 binding oracle to introduce a new tag binding. */
1575 void
1576 c_pushtag (location_t loc, tree name, tree type)
1578 pushtag (loc, name, type);
1581 /* An exported interface to bind a declaration. LOC is the location
1582 to use. DECL is the declaration to bind. The decl's name is used
1583 to determine how it is bound. If DECL is a VAR_DECL, then
1584 IS_GLOBAL determines whether the decl is put into the global (file
1585 and external) scope or the current function's scope; if DECL is not
1586 a VAR_DECL then it is always put into the file scope. */
1588 void
1589 c_bind (location_t loc, tree decl, bool is_global)
1591 struct c_scope *scope;
1592 bool nested = false;
1594 if (!VAR_P (decl) || current_function_scope == NULL)
1596 /* Types and functions are always considered to be global. */
1597 scope = file_scope;
1598 DECL_EXTERNAL (decl) = 1;
1599 TREE_PUBLIC (decl) = 1;
1601 else if (is_global)
1603 /* Also bind it into the external scope. */
1604 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1605 nested = true;
1606 scope = file_scope;
1607 DECL_EXTERNAL (decl) = 1;
1608 TREE_PUBLIC (decl) = 1;
1610 else
1612 DECL_CONTEXT (decl) = current_function_decl;
1613 TREE_PUBLIC (decl) = 0;
1614 scope = current_function_scope;
1617 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1620 /* Subroutine of compare_decls. Allow harmless mismatches in return
1621 and argument types provided that the type modes match. This function
1622 return a unified type given a suitable match, and 0 otherwise. */
1624 static tree
1625 match_builtin_function_types (tree newtype, tree oldtype)
1627 tree newrettype, oldrettype;
1628 tree newargs, oldargs;
1629 tree trytype, tryargs;
1631 /* Accept the return type of the new declaration if same modes. */
1632 oldrettype = TREE_TYPE (oldtype);
1633 newrettype = TREE_TYPE (newtype);
1635 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1636 return NULL_TREE;
1638 oldargs = TYPE_ARG_TYPES (oldtype);
1639 newargs = TYPE_ARG_TYPES (newtype);
1640 tryargs = newargs;
1642 while (oldargs || newargs)
1644 if (!oldargs
1645 || !newargs
1646 || !TREE_VALUE (oldargs)
1647 || !TREE_VALUE (newargs)
1648 || TYPE_MODE (TREE_VALUE (oldargs))
1649 != TYPE_MODE (TREE_VALUE (newargs)))
1650 return NULL_TREE;
1652 oldargs = TREE_CHAIN (oldargs);
1653 newargs = TREE_CHAIN (newargs);
1656 trytype = build_function_type (newrettype, tryargs);
1658 /* Allow declaration to change transaction_safe attribute. */
1659 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1660 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1661 tree newattrs = TYPE_ATTRIBUTES (newtype);
1662 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1663 if (oldtsafe && !newtsafe)
1664 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1665 else if (newtsafe && !oldtsafe)
1666 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1667 NULL_TREE, oldattrs);
1669 return build_type_attribute_variant (trytype, oldattrs);
1672 /* Subroutine of diagnose_mismatched_decls. Check for function type
1673 mismatch involving an empty arglist vs a nonempty one and give clearer
1674 diagnostics. */
1675 static void
1676 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1677 tree newtype, tree oldtype)
1679 tree t;
1681 if (TREE_CODE (olddecl) != FUNCTION_DECL
1682 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1683 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1684 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1685 return;
1687 t = TYPE_ARG_TYPES (oldtype);
1688 if (t == NULL_TREE)
1689 t = TYPE_ARG_TYPES (newtype);
1690 for (; t; t = TREE_CHAIN (t))
1692 tree type = TREE_VALUE (t);
1694 if (TREE_CHAIN (t) == NULL_TREE
1695 && TYPE_MAIN_VARIANT (type) != void_type_node)
1697 inform (input_location, "a parameter list with an ellipsis can%'t match "
1698 "an empty parameter name list declaration");
1699 break;
1702 if (c_type_promotes_to (type) != type)
1704 inform (input_location, "an argument type that has a default promotion can%'t match "
1705 "an empty parameter name list declaration");
1706 break;
1711 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1712 old-style function definition, NEWDECL is a prototype declaration.
1713 Diagnose inconsistencies in the argument list. Returns TRUE if
1714 the prototype is compatible, FALSE if not. */
1715 static bool
1716 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1718 tree newargs, oldargs;
1719 int i;
1721 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1723 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1724 newargs = TYPE_ARG_TYPES (newtype);
1725 i = 1;
1727 for (;;)
1729 tree oldargtype = TREE_VALUE (oldargs);
1730 tree newargtype = TREE_VALUE (newargs);
1732 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1733 return false;
1735 oldargtype = (TYPE_ATOMIC (oldargtype)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (oldargtype));
1739 newargtype = (TYPE_ATOMIC (newargtype)
1740 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1741 TYPE_QUAL_ATOMIC)
1742 : TYPE_MAIN_VARIANT (newargtype));
1744 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1745 break;
1747 /* Reaching the end of just one list means the two decls don't
1748 agree on the number of arguments. */
1749 if (END_OF_ARGLIST (oldargtype))
1751 error ("prototype for %q+D declares more arguments "
1752 "than previous old-style definition", newdecl);
1753 return false;
1755 else if (END_OF_ARGLIST (newargtype))
1757 error ("prototype for %q+D declares fewer arguments "
1758 "than previous old-style definition", newdecl);
1759 return false;
1762 /* Type for passing arg must be consistent with that declared
1763 for the arg. */
1764 else if (!comptypes (oldargtype, newargtype))
1766 error ("prototype for %q+D declares argument %d"
1767 " with incompatible type",
1768 newdecl, i);
1769 return false;
1772 oldargs = TREE_CHAIN (oldargs);
1773 newargs = TREE_CHAIN (newargs);
1774 i++;
1777 /* If we get here, no errors were found, but do issue a warning
1778 for this poor-style construct. */
1779 warning (0, "prototype for %q+D follows non-prototype definition",
1780 newdecl);
1781 return true;
1782 #undef END_OF_ARGLIST
1785 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1786 first in a pair of mismatched declarations, using the diagnostic
1787 function DIAG. */
1788 static void
1789 locate_old_decl (tree decl)
1791 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1792 && !C_DECL_DECLARED_BUILTIN (decl))
1794 else if (DECL_INITIAL (decl))
1795 inform (input_location, "previous definition of %q+D was here", decl);
1796 else if (C_DECL_IMPLICIT (decl))
1797 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1798 else
1799 inform (input_location, "previous declaration of %q+D was here", decl);
1802 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1803 Returns true if the caller should proceed to merge the two, false
1804 if OLDDECL should simply be discarded. As a side effect, issues
1805 all necessary diagnostics for invalid or poor-style combinations.
1806 If it returns true, writes the types of NEWDECL and OLDDECL to
1807 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1808 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1810 static bool
1811 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1812 tree *newtypep, tree *oldtypep)
1814 tree newtype, oldtype;
1815 bool pedwarned = false;
1816 bool warned = false;
1817 bool retval = true;
1819 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1820 && DECL_EXTERNAL (DECL))
1822 /* If we have error_mark_node for either decl or type, just discard
1823 the previous decl - we're in an error cascade already. */
1824 if (olddecl == error_mark_node || newdecl == error_mark_node)
1825 return false;
1826 *oldtypep = oldtype = TREE_TYPE (olddecl);
1827 *newtypep = newtype = TREE_TYPE (newdecl);
1828 if (oldtype == error_mark_node || newtype == error_mark_node)
1829 return false;
1831 /* Two different categories of symbol altogether. This is an error
1832 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1833 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1835 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1836 && DECL_BUILT_IN (olddecl)
1837 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1839 error ("%q+D redeclared as different kind of symbol", newdecl);
1840 locate_old_decl (olddecl);
1842 else if (TREE_PUBLIC (newdecl))
1843 warning (OPT_Wbuiltin_declaration_mismatch,
1844 "built-in function %q+D declared as non-function",
1845 newdecl);
1846 else
1847 warning (OPT_Wshadow, "declaration of %q+D shadows "
1848 "a built-in function", newdecl);
1849 return false;
1852 /* Enumerators have no linkage, so may only be declared once in a
1853 given scope. */
1854 if (TREE_CODE (olddecl) == CONST_DECL)
1856 error ("redeclaration of enumerator %q+D", newdecl);
1857 locate_old_decl (olddecl);
1858 return false;
1861 if (!comptypes (oldtype, newtype))
1863 if (TREE_CODE (olddecl) == FUNCTION_DECL
1864 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1866 /* Accept harmless mismatch in function types.
1867 This is for the ffs and fprintf builtins. */
1868 tree trytype = match_builtin_function_types (newtype, oldtype);
1870 if (trytype && comptypes (newtype, trytype))
1871 *oldtypep = oldtype = trytype;
1872 else
1874 /* If types don't match for a built-in, throw away the
1875 built-in. No point in calling locate_old_decl here, it
1876 won't print anything. */
1877 warning (OPT_Wbuiltin_declaration_mismatch,
1878 "conflicting types for built-in function %q+D",
1879 newdecl);
1880 return false;
1883 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1884 && DECL_IS_BUILTIN (olddecl))
1886 /* A conflicting function declaration for a predeclared
1887 function that isn't actually built in. Objective C uses
1888 these. The new declaration silently overrides everything
1889 but the volatility (i.e. noreturn) indication. See also
1890 below. FIXME: Make Objective C use normal builtins. */
1891 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1892 return false;
1894 /* Permit void foo (...) to match int foo (...) if the latter is
1895 the definition and implicit int was used. See
1896 c-torture/compile/920625-2.c. */
1897 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1898 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1899 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1900 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1902 pedwarned = pedwarn (input_location, 0,
1903 "conflicting types for %q+D", newdecl);
1904 /* Make sure we keep void as the return type. */
1905 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1906 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1908 /* Permit void foo (...) to match an earlier call to foo (...) with
1909 no declared type (thus, implicitly int). */
1910 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1911 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1912 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1913 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1915 pedwarned = pedwarn (input_location, 0,
1916 "conflicting types for %q+D", newdecl);
1917 /* Make sure we keep void as the return type. */
1918 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1920 else
1922 int new_quals = TYPE_QUALS (newtype);
1923 int old_quals = TYPE_QUALS (oldtype);
1925 if (new_quals != old_quals)
1927 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1928 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1929 if (new_addr != old_addr)
1931 if (ADDR_SPACE_GENERIC_P (new_addr))
1932 error ("conflicting named address spaces (generic vs %s) "
1933 "for %q+D",
1934 c_addr_space_name (old_addr), newdecl);
1935 else if (ADDR_SPACE_GENERIC_P (old_addr))
1936 error ("conflicting named address spaces (%s vs generic) "
1937 "for %q+D",
1938 c_addr_space_name (new_addr), newdecl);
1939 else
1940 error ("conflicting named address spaces (%s vs %s) "
1941 "for %q+D",
1942 c_addr_space_name (new_addr),
1943 c_addr_space_name (old_addr),
1944 newdecl);
1947 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1948 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1949 error ("conflicting type qualifiers for %q+D", newdecl);
1951 else
1952 error ("conflicting types for %q+D", newdecl);
1953 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1954 locate_old_decl (olddecl);
1955 return false;
1959 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1960 but silently ignore the redeclaration if either is in a system
1961 header. (Conflicting redeclarations were handled above.) This
1962 is allowed for C11 if the types are the same, not just
1963 compatible. */
1964 if (TREE_CODE (newdecl) == TYPE_DECL)
1966 bool types_different = false;
1967 int comptypes_result;
1969 comptypes_result
1970 = comptypes_check_different_types (oldtype, newtype, &types_different);
1972 if (comptypes_result != 1 || types_different)
1974 error ("redefinition of typedef %q+D with different type", newdecl);
1975 locate_old_decl (olddecl);
1976 return false;
1979 if (DECL_IN_SYSTEM_HEADER (newdecl)
1980 || DECL_IN_SYSTEM_HEADER (olddecl)
1981 || TREE_NO_WARNING (newdecl)
1982 || TREE_NO_WARNING (olddecl))
1983 return true; /* Allow OLDDECL to continue in use. */
1985 if (variably_modified_type_p (newtype, NULL))
1987 error ("redefinition of typedef %q+D with variably modified type",
1988 newdecl);
1989 locate_old_decl (olddecl);
1991 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1992 "redefinition of typedef %q+D", newdecl))
1993 locate_old_decl (olddecl);
1995 return true;
1998 /* Function declarations can either be 'static' or 'extern' (no
1999 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2000 can never conflict with each other on account of linkage
2001 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2002 gnu89 mode permits two definitions if one is 'extern inline' and
2003 one is not. The non- extern-inline definition supersedes the
2004 extern-inline definition. */
2006 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2008 /* If you declare a built-in function name as static, or
2009 define the built-in with an old-style definition (so we
2010 can't validate the argument list) the built-in definition is
2011 overridden, but optionally warn this was a bad choice of name. */
2012 if (DECL_BUILT_IN (olddecl)
2013 && !C_DECL_DECLARED_BUILTIN (olddecl)
2014 && (!TREE_PUBLIC (newdecl)
2015 || (DECL_INITIAL (newdecl)
2016 && !prototype_p (TREE_TYPE (newdecl)))))
2018 warning (OPT_Wshadow, "declaration of %q+D shadows "
2019 "a built-in function", newdecl);
2020 /* Discard the old built-in function. */
2021 return false;
2024 if (DECL_INITIAL (newdecl))
2026 if (DECL_INITIAL (olddecl))
2028 /* If both decls are in the same TU and the new declaration
2029 isn't overriding an extern inline reject the new decl.
2030 In c99, no overriding is allowed in the same translation
2031 unit. */
2032 if ((!DECL_EXTERN_INLINE (olddecl)
2033 || DECL_EXTERN_INLINE (newdecl)
2034 || (!flag_gnu89_inline
2035 && (!DECL_DECLARED_INLINE_P (olddecl)
2036 || !lookup_attribute ("gnu_inline",
2037 DECL_ATTRIBUTES (olddecl)))
2038 && (!DECL_DECLARED_INLINE_P (newdecl)
2039 || !lookup_attribute ("gnu_inline",
2040 DECL_ATTRIBUTES (newdecl))))
2042 && same_translation_unit_p (newdecl, olddecl))
2044 error ("redefinition of %q+D", newdecl);
2045 locate_old_decl (olddecl);
2046 return false;
2050 /* If we have a prototype after an old-style function definition,
2051 the argument types must be checked specially. */
2052 else if (DECL_INITIAL (olddecl)
2053 && !prototype_p (oldtype) && prototype_p (newtype)
2054 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2055 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2057 locate_old_decl (olddecl);
2058 return false;
2060 /* A non-static declaration (even an "extern") followed by a
2061 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2062 The same is true for a static forward declaration at block
2063 scope followed by a non-static declaration/definition at file
2064 scope. Static followed by non-static at the same scope is
2065 not undefined behavior, and is the most convenient way to get
2066 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2067 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2068 we do diagnose it if -Wtraditional. */
2069 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2071 /* Two exceptions to the rule. If olddecl is an extern
2072 inline, or a predeclared function that isn't actually
2073 built in, newdecl silently overrides olddecl. The latter
2074 occur only in Objective C; see also above. (FIXME: Make
2075 Objective C use normal builtins.) */
2076 if (!DECL_IS_BUILTIN (olddecl)
2077 && !DECL_EXTERN_INLINE (olddecl))
2079 error ("static declaration of %q+D follows "
2080 "non-static declaration", newdecl);
2081 locate_old_decl (olddecl);
2083 return false;
2085 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2087 if (DECL_CONTEXT (olddecl))
2089 error ("non-static declaration of %q+D follows "
2090 "static declaration", newdecl);
2091 locate_old_decl (olddecl);
2092 return false;
2094 else if (warn_traditional)
2096 warned |= warning (OPT_Wtraditional,
2097 "non-static declaration of %q+D "
2098 "follows static declaration", newdecl);
2102 /* Make sure gnu_inline attribute is either not present, or
2103 present on all inline decls. */
2104 if (DECL_DECLARED_INLINE_P (olddecl)
2105 && DECL_DECLARED_INLINE_P (newdecl))
2107 bool newa = lookup_attribute ("gnu_inline",
2108 DECL_ATTRIBUTES (newdecl)) != NULL;
2109 bool olda = lookup_attribute ("gnu_inline",
2110 DECL_ATTRIBUTES (olddecl)) != NULL;
2111 if (newa != olda)
2113 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2114 newa ? newdecl : olddecl);
2115 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2116 "but not here");
2120 else if (VAR_P (newdecl))
2122 /* Only variables can be thread-local, and all declarations must
2123 agree on this property. */
2124 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2126 /* Nothing to check. Since OLDDECL is marked threadprivate
2127 and NEWDECL does not have a thread-local attribute, we
2128 will merge the threadprivate attribute into NEWDECL. */
2131 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2133 if (DECL_THREAD_LOCAL_P (newdecl))
2134 error ("thread-local declaration of %q+D follows "
2135 "non-thread-local declaration", newdecl);
2136 else
2137 error ("non-thread-local declaration of %q+D follows "
2138 "thread-local declaration", newdecl);
2140 locate_old_decl (olddecl);
2141 return false;
2144 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2145 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2147 error ("redefinition of %q+D", newdecl);
2148 locate_old_decl (olddecl);
2149 return false;
2152 /* Objects declared at file scope: if the first declaration had
2153 external linkage (even if it was an external reference) the
2154 second must have external linkage as well, or the behavior is
2155 undefined. If the first declaration had internal linkage, then
2156 the second must too, or else be an external reference (in which
2157 case the composite declaration still has internal linkage).
2158 As for function declarations, we warn about the static-then-
2159 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2160 if (DECL_FILE_SCOPE_P (newdecl)
2161 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2163 if (DECL_EXTERNAL (newdecl))
2165 if (!DECL_FILE_SCOPE_P (olddecl))
2167 error ("extern declaration of %q+D follows "
2168 "declaration with no linkage", newdecl);
2169 locate_old_decl (olddecl);
2170 return false;
2172 else if (warn_traditional)
2174 warned |= warning (OPT_Wtraditional,
2175 "non-static declaration of %q+D "
2176 "follows static declaration", newdecl);
2179 else
2181 if (TREE_PUBLIC (newdecl))
2182 error ("non-static declaration of %q+D follows "
2183 "static declaration", newdecl);
2184 else
2185 error ("static declaration of %q+D follows "
2186 "non-static declaration", newdecl);
2188 locate_old_decl (olddecl);
2189 return false;
2192 /* Two objects with the same name declared at the same block
2193 scope must both be external references (6.7p3). */
2194 else if (!DECL_FILE_SCOPE_P (newdecl))
2196 if (DECL_EXTERNAL (newdecl))
2198 /* Extern with initializer at block scope, which will
2199 already have received an error. */
2201 else if (DECL_EXTERNAL (olddecl))
2203 error ("declaration of %q+D with no linkage follows "
2204 "extern declaration", newdecl);
2205 locate_old_decl (olddecl);
2207 else
2209 error ("redeclaration of %q+D with no linkage", newdecl);
2210 locate_old_decl (olddecl);
2213 return false;
2216 /* C++ does not permit a decl to appear multiple times at file
2217 scope. */
2218 if (warn_cxx_compat
2219 && DECL_FILE_SCOPE_P (newdecl)
2220 && !DECL_EXTERNAL (newdecl)
2221 && !DECL_EXTERNAL (olddecl))
2222 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2223 OPT_Wc___compat,
2224 ("duplicate declaration of %qD is "
2225 "invalid in C++"),
2226 newdecl);
2229 /* warnings */
2230 /* All decls must agree on a visibility. */
2231 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2232 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2233 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2235 warned |= warning (0, "redeclaration of %q+D with different visibility "
2236 "(old visibility preserved)", newdecl);
2239 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2240 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2241 else /* PARM_DECL, VAR_DECL */
2243 /* Redeclaration of a parameter is a constraint violation (this is
2244 not explicitly stated, but follows from C99 6.7p3 [no more than
2245 one declaration of the same identifier with no linkage in the
2246 same scope, except type tags] and 6.2.2p6 [parameters have no
2247 linkage]). We must check for a forward parameter declaration,
2248 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2249 an extension, the mandatory diagnostic for which is handled by
2250 mark_forward_parm_decls. */
2252 if (TREE_CODE (newdecl) == PARM_DECL
2253 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2255 error ("redefinition of parameter %q+D", newdecl);
2256 locate_old_decl (olddecl);
2257 return false;
2261 /* Optional warning for completely redundant decls. */
2262 if (!warned && !pedwarned
2263 && warn_redundant_decls
2264 /* Don't warn about a function declaration followed by a
2265 definition. */
2266 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2267 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2268 /* Don't warn about redundant redeclarations of builtins. */
2269 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2270 && !DECL_BUILT_IN (newdecl)
2271 && DECL_BUILT_IN (olddecl)
2272 && !C_DECL_DECLARED_BUILTIN (olddecl))
2273 /* Don't warn about an extern followed by a definition. */
2274 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2275 /* Don't warn about forward parameter decls. */
2276 && !(TREE_CODE (newdecl) == PARM_DECL
2277 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2278 /* Don't warn about a variable definition following a declaration. */
2279 && !(VAR_P (newdecl)
2280 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2282 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2283 newdecl);
2286 /* Report location of previous decl/defn. */
2287 if (warned || pedwarned)
2288 locate_old_decl (olddecl);
2290 #undef DECL_EXTERN_INLINE
2292 return retval;
2295 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2296 consistent with OLDDECL, but carries new information. Merge the
2297 new information into OLDDECL. This function issues no
2298 diagnostics. */
2300 static void
2301 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2303 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2304 && DECL_INITIAL (newdecl) != NULL_TREE);
2305 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2306 && prototype_p (TREE_TYPE (newdecl)));
2307 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2308 && prototype_p (TREE_TYPE (olddecl)));
2310 /* For real parm decl following a forward decl, rechain the old decl
2311 in its new location and clear TREE_ASM_WRITTEN (it's not a
2312 forward decl anymore). */
2313 if (TREE_CODE (newdecl) == PARM_DECL
2314 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2316 struct c_binding *b, **here;
2318 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2319 if ((*here)->decl == olddecl)
2320 goto found;
2321 gcc_unreachable ();
2323 found:
2324 b = *here;
2325 *here = b->prev;
2326 b->prev = current_scope->bindings;
2327 current_scope->bindings = b;
2329 TREE_ASM_WRITTEN (olddecl) = 0;
2332 DECL_ATTRIBUTES (newdecl)
2333 = targetm.merge_decl_attributes (olddecl, newdecl);
2335 /* For typedefs use the old type, as the new type's DECL_NAME points
2336 at newdecl, which will be ggc_freed. */
2337 if (TREE_CODE (newdecl) == TYPE_DECL)
2339 /* But NEWTYPE might have an attribute, honor that. */
2340 tree tem = newtype;
2341 newtype = oldtype;
2343 if (TYPE_USER_ALIGN (tem))
2345 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2346 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2347 TYPE_USER_ALIGN (newtype) = true;
2350 /* And remove the new type from the variants list. */
2351 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2353 tree remove = TREE_TYPE (newdecl);
2354 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2355 t = TYPE_NEXT_VARIANT (t))
2356 if (TYPE_NEXT_VARIANT (t) == remove)
2358 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2359 break;
2364 /* Merge the data types specified in the two decls. */
2365 TREE_TYPE (newdecl)
2366 = TREE_TYPE (olddecl)
2367 = composite_type (newtype, oldtype);
2369 /* Lay the type out, unless already done. */
2370 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2372 if (TREE_TYPE (newdecl) != error_mark_node)
2373 layout_type (TREE_TYPE (newdecl));
2374 if (TREE_CODE (newdecl) != FUNCTION_DECL
2375 && TREE_CODE (newdecl) != TYPE_DECL
2376 && TREE_CODE (newdecl) != CONST_DECL)
2377 layout_decl (newdecl, 0);
2379 else
2381 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2382 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2383 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2384 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2385 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2387 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2388 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2390 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2391 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2392 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2393 DECL_WARN_IF_NOT_ALIGN (olddecl));
2396 /* Keep the old rtl since we can safely use it. */
2397 if (HAS_RTL_P (olddecl))
2398 COPY_DECL_RTL (olddecl, newdecl);
2400 /* Merge the type qualifiers. */
2401 if (TREE_READONLY (newdecl))
2402 TREE_READONLY (olddecl) = 1;
2404 if (TREE_THIS_VOLATILE (newdecl))
2405 TREE_THIS_VOLATILE (olddecl) = 1;
2407 /* Merge deprecatedness. */
2408 if (TREE_DEPRECATED (newdecl))
2409 TREE_DEPRECATED (olddecl) = 1;
2411 /* If a decl is in a system header and the other isn't, keep the one on the
2412 system header. Otherwise, keep source location of definition rather than
2413 declaration and of prototype rather than non-prototype unless that
2414 prototype is built-in. */
2415 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2416 && DECL_IN_SYSTEM_HEADER (olddecl)
2417 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2418 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2419 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2420 && DECL_IN_SYSTEM_HEADER (newdecl)
2421 && !DECL_IN_SYSTEM_HEADER (olddecl))
2422 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2423 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2424 && DECL_INITIAL (olddecl) != NULL_TREE)
2425 || (old_is_prototype && !new_is_prototype
2426 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2427 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2429 /* Merge the initialization information. */
2430 if (DECL_INITIAL (newdecl) == NULL_TREE)
2431 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2433 /* Merge the threadprivate attribute. */
2434 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2435 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2437 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2439 /* Copy the assembler name.
2440 Currently, it can only be defined in the prototype. */
2441 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2443 /* Use visibility of whichever declaration had it specified */
2444 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2446 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2447 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2450 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2452 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2453 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2454 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2455 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2456 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2457 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2458 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2459 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2460 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2461 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2462 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2465 /* Merge the storage class information. */
2466 merge_weak (newdecl, olddecl);
2468 /* For functions, static overrides non-static. */
2469 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2471 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2472 /* This is since we don't automatically
2473 copy the attributes of NEWDECL into OLDDECL. */
2474 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2475 /* If this clears `static', clear it in the identifier too. */
2476 if (!TREE_PUBLIC (olddecl))
2477 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2481 /* In c99, 'extern' declaration before (or after) 'inline' means this
2482 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2483 is present. */
2484 if (TREE_CODE (newdecl) == FUNCTION_DECL
2485 && !flag_gnu89_inline
2486 && (DECL_DECLARED_INLINE_P (newdecl)
2487 || DECL_DECLARED_INLINE_P (olddecl))
2488 && (!DECL_DECLARED_INLINE_P (newdecl)
2489 || !DECL_DECLARED_INLINE_P (olddecl)
2490 || !DECL_EXTERNAL (olddecl))
2491 && DECL_EXTERNAL (newdecl)
2492 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2493 && !current_function_decl)
2494 DECL_EXTERNAL (newdecl) = 0;
2496 /* An inline definition following a static declaration is not
2497 DECL_EXTERNAL. */
2498 if (new_is_definition
2499 && (DECL_DECLARED_INLINE_P (newdecl)
2500 || DECL_DECLARED_INLINE_P (olddecl))
2501 && !TREE_PUBLIC (olddecl))
2502 DECL_EXTERNAL (newdecl) = 0;
2504 if (DECL_EXTERNAL (newdecl))
2506 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2507 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2509 /* An extern decl does not override previous storage class. */
2510 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2511 if (!DECL_EXTERNAL (newdecl))
2513 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2514 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2517 else
2519 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2520 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2523 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2525 /* If we're redefining a function previously defined as extern
2526 inline, make sure we emit debug info for the inline before we
2527 throw it away, in case it was inlined into a function that
2528 hasn't been written out yet. */
2529 if (new_is_definition && DECL_INITIAL (olddecl))
2530 /* The new defn must not be inline. */
2531 DECL_UNINLINABLE (newdecl) = 1;
2532 else
2534 /* If either decl says `inline', this fn is inline, unless
2535 its definition was passed already. */
2536 if (DECL_DECLARED_INLINE_P (newdecl)
2537 || DECL_DECLARED_INLINE_P (olddecl))
2538 DECL_DECLARED_INLINE_P (newdecl) = 1;
2540 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2541 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2543 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2544 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2545 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2546 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2549 if (DECL_BUILT_IN (olddecl))
2551 /* If redeclaring a builtin function, it stays built in.
2552 But it gets tagged as having been declared. */
2553 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2554 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2555 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2556 if (new_is_prototype)
2558 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2559 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2561 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2562 switch (fncode)
2564 /* If a compatible prototype of these builtin functions
2565 is seen, assume the runtime implements it with the
2566 expected semantics. */
2567 case BUILT_IN_STPCPY:
2568 if (builtin_decl_explicit_p (fncode))
2569 set_builtin_decl_implicit_p (fncode, true);
2570 break;
2571 default:
2572 if (builtin_decl_explicit_p (fncode))
2573 set_builtin_decl_declared_p (fncode, true);
2574 break;
2577 copy_attributes_to_builtin (newdecl);
2580 else
2581 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2582 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2585 /* Preserve function specific target and optimization options */
2586 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2587 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2588 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2589 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2591 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2592 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2593 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2594 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2596 /* Also preserve various other info from the definition. */
2597 if (!new_is_definition)
2599 tree t;
2600 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2601 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2602 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2603 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2604 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2605 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2606 DECL_CONTEXT (t) = newdecl;
2608 /* See if we've got a function to instantiate from. */
2609 if (DECL_SAVED_TREE (olddecl))
2610 DECL_ABSTRACT_ORIGIN (newdecl)
2611 = DECL_ABSTRACT_ORIGIN (olddecl);
2615 /* Merge the USED information. */
2616 if (TREE_USED (olddecl))
2617 TREE_USED (newdecl) = 1;
2618 else if (TREE_USED (newdecl))
2619 TREE_USED (olddecl) = 1;
2620 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2621 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2622 if (DECL_PRESERVE_P (olddecl))
2623 DECL_PRESERVE_P (newdecl) = 1;
2624 else if (DECL_PRESERVE_P (newdecl))
2625 DECL_PRESERVE_P (olddecl) = 1;
2627 /* Merge DECL_COMMON */
2628 if (VAR_P (olddecl) && VAR_P (newdecl)
2629 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2630 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2631 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2633 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2634 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2635 DECL_ARGUMENTS (if appropriate). */
2637 unsigned olddecl_uid = DECL_UID (olddecl);
2638 tree olddecl_context = DECL_CONTEXT (olddecl);
2639 tree olddecl_arguments = NULL;
2640 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2641 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2643 memcpy ((char *) olddecl + sizeof (struct tree_common),
2644 (char *) newdecl + sizeof (struct tree_common),
2645 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2646 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2647 switch (TREE_CODE (olddecl))
2649 case FUNCTION_DECL:
2650 case VAR_DECL:
2652 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2654 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2655 (char *) newdecl + sizeof (struct tree_decl_common),
2656 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2657 olddecl->decl_with_vis.symtab_node = snode;
2659 if ((DECL_EXTERNAL (olddecl)
2660 || TREE_PUBLIC (olddecl)
2661 || TREE_STATIC (olddecl))
2662 && DECL_SECTION_NAME (newdecl) != NULL)
2663 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2665 /* This isn't quite correct for something like
2666 int __thread x attribute ((tls_model ("local-exec")));
2667 extern int __thread x;
2668 as we'll lose the "local-exec" model. */
2669 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2670 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2671 break;
2674 case FIELD_DECL:
2675 case PARM_DECL:
2676 case LABEL_DECL:
2677 case RESULT_DECL:
2678 case CONST_DECL:
2679 case TYPE_DECL:
2680 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2681 (char *) newdecl + sizeof (struct tree_decl_common),
2682 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2683 break;
2685 default:
2687 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2688 (char *) newdecl + sizeof (struct tree_decl_common),
2689 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2691 DECL_UID (olddecl) = olddecl_uid;
2692 DECL_CONTEXT (olddecl) = olddecl_context;
2693 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2694 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2697 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2698 so that encode_section_info has a chance to look at the new decl
2699 flags and attributes. */
2700 if (DECL_RTL_SET_P (olddecl)
2701 && (TREE_CODE (olddecl) == FUNCTION_DECL
2702 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2703 make_decl_rtl (olddecl);
2706 /* Handle when a new declaration NEWDECL has the same name as an old
2707 one OLDDECL in the same binding contour. Prints an error message
2708 if appropriate.
2710 If safely possible, alter OLDDECL to look like NEWDECL, and return
2711 true. Otherwise, return false. */
2713 static bool
2714 duplicate_decls (tree newdecl, tree olddecl)
2716 tree newtype = NULL, oldtype = NULL;
2718 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2720 /* Avoid `unused variable' and other warnings for OLDDECL. */
2721 TREE_NO_WARNING (olddecl) = 1;
2722 return false;
2725 merge_decls (newdecl, olddecl, newtype, oldtype);
2727 /* The NEWDECL will no longer be needed.
2729 Before releasing the node, be sure to remove function from symbol
2730 table that might have been inserted there to record comdat group.
2731 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2732 structure is shared in between NEWDECL and OLDECL. */
2733 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2734 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2735 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2737 struct symtab_node *snode = symtab_node::get (newdecl);
2738 if (snode)
2739 snode->remove ();
2741 ggc_free (newdecl);
2742 return true;
2746 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2747 static void
2748 warn_if_shadowing (tree new_decl)
2750 struct c_binding *b;
2752 /* Shadow warnings wanted? */
2753 if (!(warn_shadow
2754 || warn_shadow_local
2755 || warn_shadow_compatible_local)
2756 /* No shadow warnings for internally generated vars. */
2757 || DECL_IS_BUILTIN (new_decl)
2758 /* No shadow warnings for vars made for inlining. */
2759 || DECL_FROM_INLINE (new_decl))
2760 return;
2762 /* Is anything being shadowed? Invisible decls do not count. */
2763 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2764 if (b->decl && b->decl != new_decl && !b->invisible
2765 && (b->decl == error_mark_node
2766 || diagnostic_report_warnings_p (global_dc,
2767 DECL_SOURCE_LOCATION (b->decl))))
2769 tree old_decl = b->decl;
2770 bool warned = false;
2772 if (old_decl == error_mark_node)
2774 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2775 "non-variable", new_decl);
2776 break;
2778 else if (TREE_CODE (old_decl) == PARM_DECL)
2780 enum opt_code warning_code;
2782 /* If '-Wshadow=compatible-local' is specified without other
2783 -Wshadow= flags, we will warn only when the types of the
2784 shadowing variable (i.e. new_decl) and the shadowed variable
2785 (old_decl) are compatible. */
2786 if (warn_shadow)
2787 warning_code = OPT_Wshadow;
2788 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2789 warning_code = OPT_Wshadow_compatible_local;
2790 else
2791 warning_code = OPT_Wshadow_local;
2792 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2793 "declaration of %qD shadows a parameter",
2794 new_decl);
2796 else if (DECL_FILE_SCOPE_P (old_decl))
2798 /* Do not warn if a variable shadows a function, unless
2799 the variable is a function or a pointer-to-function. */
2800 if (TREE_CODE (old_decl) == FUNCTION_DECL
2801 && TREE_CODE (new_decl) != FUNCTION_DECL
2802 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2803 continue;
2805 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2806 "declaration of %qD shadows a global "
2807 "declaration",
2808 new_decl);
2810 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2811 && DECL_BUILT_IN (old_decl))
2813 warning (OPT_Wshadow, "declaration of %q+D shadows "
2814 "a built-in function", new_decl);
2815 break;
2817 else
2819 enum opt_code warning_code;
2821 /* If '-Wshadow=compatible-local' is specified without other
2822 -Wshadow= flags, we will warn only when the types of the
2823 shadowing variable (i.e. new_decl) and the shadowed variable
2824 (old_decl) are compatible. */
2825 if (warn_shadow)
2826 warning_code = OPT_Wshadow;
2827 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2828 warning_code = OPT_Wshadow_compatible_local;
2829 else
2830 warning_code = OPT_Wshadow_local;
2831 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2832 "declaration of %qD shadows a previous local",
2833 new_decl);
2836 if (warned)
2837 inform (DECL_SOURCE_LOCATION (old_decl),
2838 "shadowed declaration is here");
2840 break;
2844 /* Record a decl-node X as belonging to the current lexical scope.
2845 Check for errors (such as an incompatible declaration for the same
2846 name already seen in the same scope).
2848 Returns either X or an old decl for the same name.
2849 If an old decl is returned, it may have been smashed
2850 to agree with what X says. */
2852 tree
2853 pushdecl (tree x)
2855 tree name = DECL_NAME (x);
2856 struct c_scope *scope = current_scope;
2857 struct c_binding *b;
2858 bool nested = false;
2859 location_t locus = DECL_SOURCE_LOCATION (x);
2861 /* Must set DECL_CONTEXT for everything not at file scope or
2862 DECL_FILE_SCOPE_P won't work. Local externs don't count
2863 unless they have initializers (which generate code). */
2864 if (current_function_decl
2865 && (!VAR_OR_FUNCTION_DECL_P (x)
2866 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2867 DECL_CONTEXT (x) = current_function_decl;
2869 /* Anonymous decls are just inserted in the scope. */
2870 if (!name)
2872 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2873 locus);
2874 return x;
2877 /* First, see if there is another declaration with the same name in
2878 the current scope. If there is, duplicate_decls may do all the
2879 work for us. If duplicate_decls returns false, that indicates
2880 two incompatible decls in the same scope; we are to silently
2881 replace the old one (duplicate_decls has issued all appropriate
2882 diagnostics). In particular, we should not consider possible
2883 duplicates in the external scope, or shadowing. */
2884 b = I_SYMBOL_BINDING (name);
2885 if (b && B_IN_SCOPE (b, scope))
2887 struct c_binding *b_ext, *b_use;
2888 tree type = TREE_TYPE (x);
2889 tree visdecl = b->decl;
2890 tree vistype = TREE_TYPE (visdecl);
2891 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2892 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2893 b->inner_comp = false;
2894 b_use = b;
2895 b_ext = b;
2896 /* If this is an external linkage declaration, we should check
2897 for compatibility with the type in the external scope before
2898 setting the type at this scope based on the visible
2899 information only. */
2900 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2902 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2903 b_ext = b_ext->shadowed;
2904 if (b_ext)
2906 b_use = b_ext;
2907 if (b_use->u.type)
2908 TREE_TYPE (b_use->decl) = b_use->u.type;
2911 if (duplicate_decls (x, b_use->decl))
2913 if (b_use != b)
2915 /* Save the updated type in the external scope and
2916 restore the proper type for this scope. */
2917 tree thistype;
2918 if (comptypes (vistype, type))
2919 thistype = composite_type (vistype, type);
2920 else
2921 thistype = TREE_TYPE (b_use->decl);
2922 b_use->u.type = TREE_TYPE (b_use->decl);
2923 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2924 && DECL_BUILT_IN (b_use->decl))
2925 thistype
2926 = build_type_attribute_variant (thistype,
2927 TYPE_ATTRIBUTES
2928 (b_use->u.type));
2929 TREE_TYPE (b_use->decl) = thistype;
2931 return b_use->decl;
2933 else
2934 goto skip_external_and_shadow_checks;
2937 /* All declarations with external linkage, and all external
2938 references, go in the external scope, no matter what scope is
2939 current. However, the binding in that scope is ignored for
2940 purposes of normal name lookup. A separate binding structure is
2941 created in the requested scope; this governs the normal
2942 visibility of the symbol.
2944 The binding in the externals scope is used exclusively for
2945 detecting duplicate declarations of the same object, no matter
2946 what scope they are in; this is what we do here. (C99 6.2.7p2:
2947 All declarations that refer to the same object or function shall
2948 have compatible type; otherwise, the behavior is undefined.) */
2949 if (DECL_EXTERNAL (x) || scope == file_scope)
2951 tree type = TREE_TYPE (x);
2952 tree vistype = NULL_TREE;
2953 tree visdecl = NULL_TREE;
2954 bool type_saved = false;
2955 if (b && !B_IN_EXTERNAL_SCOPE (b)
2956 && VAR_OR_FUNCTION_DECL_P (b->decl)
2957 && DECL_FILE_SCOPE_P (b->decl))
2959 visdecl = b->decl;
2960 vistype = TREE_TYPE (visdecl);
2962 if (scope != file_scope
2963 && !DECL_IN_SYSTEM_HEADER (x))
2964 warning_at (locus, OPT_Wnested_externs,
2965 "nested extern declaration of %qD", x);
2967 while (b && !B_IN_EXTERNAL_SCOPE (b))
2969 /* If this decl might be modified, save its type. This is
2970 done here rather than when the decl is first bound
2971 because the type may change after first binding, through
2972 being completed or through attributes being added. If we
2973 encounter multiple such decls, only the first should have
2974 its type saved; the others will already have had their
2975 proper types saved and the types will not have changed as
2976 their scopes will not have been re-entered. */
2977 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2979 b->u.type = TREE_TYPE (b->decl);
2980 type_saved = true;
2982 if (B_IN_FILE_SCOPE (b)
2983 && VAR_P (b->decl)
2984 && TREE_STATIC (b->decl)
2985 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2986 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2987 && TREE_CODE (type) == ARRAY_TYPE
2988 && TYPE_DOMAIN (type)
2989 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2990 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2992 /* Array type completed in inner scope, which should be
2993 diagnosed if the completion does not have size 1 and
2994 it does not get completed in the file scope. */
2995 b->inner_comp = true;
2997 b = b->shadowed;
3000 /* If a matching external declaration has been found, set its
3001 type to the composite of all the types of that declaration.
3002 After the consistency checks, it will be reset to the
3003 composite of the visible types only. */
3004 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3005 && b->u.type)
3006 TREE_TYPE (b->decl) = b->u.type;
3008 /* The point of the same_translation_unit_p check here is,
3009 we want to detect a duplicate decl for a construct like
3010 foo() { extern bar(); } ... static bar(); but not if
3011 they are in different translation units. In any case,
3012 the static does not go in the externals scope. */
3013 if (b
3014 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3015 && duplicate_decls (x, b->decl))
3017 tree thistype;
3018 if (vistype)
3020 if (comptypes (vistype, type))
3021 thistype = composite_type (vistype, type);
3022 else
3023 thistype = TREE_TYPE (b->decl);
3025 else
3026 thistype = type;
3027 b->u.type = TREE_TYPE (b->decl);
3028 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3029 thistype
3030 = build_type_attribute_variant (thistype,
3031 TYPE_ATTRIBUTES (b->u.type));
3032 TREE_TYPE (b->decl) = thistype;
3033 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3034 locus);
3035 return b->decl;
3037 else if (TREE_PUBLIC (x))
3039 if (visdecl && !b && duplicate_decls (x, visdecl))
3041 /* An external declaration at block scope referring to a
3042 visible entity with internal linkage. The composite
3043 type will already be correct for this scope, so we
3044 just need to fall through to make the declaration in
3045 this scope. */
3046 nested = true;
3047 x = visdecl;
3049 else
3051 bind (name, x, external_scope, /*invisible=*/true,
3052 /*nested=*/false, locus);
3053 nested = true;
3058 if (TREE_CODE (x) != PARM_DECL)
3059 warn_if_shadowing (x);
3061 skip_external_and_shadow_checks:
3062 if (TREE_CODE (x) == TYPE_DECL)
3064 /* So this is a typedef, set its underlying type. */
3065 set_underlying_type (x);
3067 /* If X is a typedef defined in the current function, record it
3068 for the purpose of implementing the -Wunused-local-typedefs
3069 warning. */
3070 record_locally_defined_typedef (x);
3073 bind (name, x, scope, /*invisible=*/false, nested, locus);
3075 /* If x's type is incomplete because it's based on a
3076 structure or union which has not yet been fully declared,
3077 attach it to that structure or union type, so we can go
3078 back and complete the variable declaration later, if the
3079 structure or union gets fully declared.
3081 If the input is erroneous, we can have error_mark in the type
3082 slot (e.g. "f(void a, ...)") - that doesn't count as an
3083 incomplete type. */
3084 if (TREE_TYPE (x) != error_mark_node
3085 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3087 tree element = TREE_TYPE (x);
3089 while (TREE_CODE (element) == ARRAY_TYPE)
3090 element = TREE_TYPE (element);
3091 element = TYPE_MAIN_VARIANT (element);
3093 if (RECORD_OR_UNION_TYPE_P (element)
3094 && (TREE_CODE (x) != TYPE_DECL
3095 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3096 && !COMPLETE_TYPE_P (element))
3097 C_TYPE_INCOMPLETE_VARS (element)
3098 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3100 return x;
3104 /* Issue a warning about implicit function declaration. ID is the function
3105 identifier, OLDDECL is a declaration of the function in a different scope,
3106 or NULL_TREE. */
3108 static void
3109 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3111 if (!warn_implicit_function_declaration)
3112 return;
3114 bool warned;
3115 name_hint hint;
3116 if (!olddecl)
3117 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3119 if (flag_isoc99)
3121 if (hint)
3123 gcc_rich_location richloc (loc);
3124 richloc.add_fixit_replace (hint.suggestion ());
3125 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3126 "implicit declaration of function %qE;"
3127 " did you mean %qs?",
3128 id, hint.suggestion ());
3130 else
3131 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3132 "implicit declaration of function %qE", id);
3134 else if (hint)
3136 gcc_rich_location richloc (loc);
3137 richloc.add_fixit_replace (hint.suggestion ());
3138 warned = warning_at
3139 (&richloc, OPT_Wimplicit_function_declaration,
3140 G_("implicit declaration of function %qE; did you mean %qs?"),
3141 id, hint.suggestion ());
3143 else
3144 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3145 G_("implicit declaration of function %qE"), id);
3147 if (olddecl && warned)
3148 locate_old_decl (olddecl);
3150 if (!warned)
3151 hint.suppress ();
3154 /* This function represents mapping of a function code FCODE
3155 to its respective header. */
3157 static const char *
3158 header_for_builtin_fn (enum built_in_function fcode)
3160 switch (fcode)
3162 CASE_FLT_FN (BUILT_IN_ACOS):
3163 CASE_FLT_FN (BUILT_IN_ACOSH):
3164 CASE_FLT_FN (BUILT_IN_ASIN):
3165 CASE_FLT_FN (BUILT_IN_ASINH):
3166 CASE_FLT_FN (BUILT_IN_ATAN):
3167 CASE_FLT_FN (BUILT_IN_ATANH):
3168 CASE_FLT_FN (BUILT_IN_ATAN2):
3169 CASE_FLT_FN (BUILT_IN_CBRT):
3170 CASE_FLT_FN (BUILT_IN_CEIL):
3171 CASE_FLT_FN_FLOATN_NX (BUILT_IN_CEIL):
3172 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3173 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3174 CASE_FLT_FN (BUILT_IN_COS):
3175 CASE_FLT_FN (BUILT_IN_COSH):
3176 CASE_FLT_FN (BUILT_IN_ERF):
3177 CASE_FLT_FN (BUILT_IN_ERFC):
3178 CASE_FLT_FN (BUILT_IN_EXP):
3179 CASE_FLT_FN (BUILT_IN_EXP2):
3180 CASE_FLT_FN (BUILT_IN_EXPM1):
3181 CASE_FLT_FN (BUILT_IN_FABS):
3182 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3183 CASE_FLT_FN (BUILT_IN_FDIM):
3184 CASE_FLT_FN (BUILT_IN_FLOOR):
3185 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FLOOR):
3186 CASE_FLT_FN (BUILT_IN_FMA):
3187 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3188 CASE_FLT_FN (BUILT_IN_FMAX):
3189 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3190 CASE_FLT_FN (BUILT_IN_FMIN):
3191 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3192 CASE_FLT_FN (BUILT_IN_FMOD):
3193 CASE_FLT_FN (BUILT_IN_FREXP):
3194 CASE_FLT_FN (BUILT_IN_HYPOT):
3195 CASE_FLT_FN (BUILT_IN_ILOGB):
3196 CASE_FLT_FN (BUILT_IN_LDEXP):
3197 CASE_FLT_FN (BUILT_IN_LGAMMA):
3198 CASE_FLT_FN (BUILT_IN_LLRINT):
3199 CASE_FLT_FN (BUILT_IN_LLROUND):
3200 CASE_FLT_FN (BUILT_IN_LOG):
3201 CASE_FLT_FN (BUILT_IN_LOG10):
3202 CASE_FLT_FN (BUILT_IN_LOG1P):
3203 CASE_FLT_FN (BUILT_IN_LOG2):
3204 CASE_FLT_FN (BUILT_IN_LOGB):
3205 CASE_FLT_FN (BUILT_IN_LRINT):
3206 CASE_FLT_FN (BUILT_IN_LROUND):
3207 CASE_FLT_FN (BUILT_IN_MODF):
3208 CASE_FLT_FN (BUILT_IN_NAN):
3209 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3210 CASE_FLT_FN_FLOATN_NX (BUILT_IN_NEARBYINT):
3211 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3212 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3213 CASE_FLT_FN (BUILT_IN_POW):
3214 CASE_FLT_FN (BUILT_IN_REMAINDER):
3215 CASE_FLT_FN (BUILT_IN_REMQUO):
3216 CASE_FLT_FN (BUILT_IN_RINT):
3217 CASE_FLT_FN_FLOATN_NX (BUILT_IN_RINT):
3218 CASE_FLT_FN (BUILT_IN_ROUND):
3219 CASE_FLT_FN_FLOATN_NX (BUILT_IN_ROUND):
3220 CASE_FLT_FN (BUILT_IN_SCALBLN):
3221 CASE_FLT_FN (BUILT_IN_SCALBN):
3222 CASE_FLT_FN (BUILT_IN_SIN):
3223 CASE_FLT_FN (BUILT_IN_SINH):
3224 CASE_FLT_FN (BUILT_IN_SINCOS):
3225 CASE_FLT_FN (BUILT_IN_SQRT):
3226 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3227 CASE_FLT_FN (BUILT_IN_TAN):
3228 CASE_FLT_FN (BUILT_IN_TANH):
3229 CASE_FLT_FN (BUILT_IN_TGAMMA):
3230 CASE_FLT_FN (BUILT_IN_TRUNC):
3231 CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
3232 case BUILT_IN_ISINF:
3233 case BUILT_IN_ISNAN:
3234 return "<math.h>";
3235 CASE_FLT_FN (BUILT_IN_CABS):
3236 CASE_FLT_FN (BUILT_IN_CACOS):
3237 CASE_FLT_FN (BUILT_IN_CACOSH):
3238 CASE_FLT_FN (BUILT_IN_CARG):
3239 CASE_FLT_FN (BUILT_IN_CASIN):
3240 CASE_FLT_FN (BUILT_IN_CASINH):
3241 CASE_FLT_FN (BUILT_IN_CATAN):
3242 CASE_FLT_FN (BUILT_IN_CATANH):
3243 CASE_FLT_FN (BUILT_IN_CCOS):
3244 CASE_FLT_FN (BUILT_IN_CCOSH):
3245 CASE_FLT_FN (BUILT_IN_CEXP):
3246 CASE_FLT_FN (BUILT_IN_CIMAG):
3247 CASE_FLT_FN (BUILT_IN_CLOG):
3248 CASE_FLT_FN (BUILT_IN_CONJ):
3249 CASE_FLT_FN (BUILT_IN_CPOW):
3250 CASE_FLT_FN (BUILT_IN_CPROJ):
3251 CASE_FLT_FN (BUILT_IN_CREAL):
3252 CASE_FLT_FN (BUILT_IN_CSIN):
3253 CASE_FLT_FN (BUILT_IN_CSINH):
3254 CASE_FLT_FN (BUILT_IN_CSQRT):
3255 CASE_FLT_FN (BUILT_IN_CTAN):
3256 CASE_FLT_FN (BUILT_IN_CTANH):
3257 return "<complex.h>";
3258 case BUILT_IN_MEMCHR:
3259 case BUILT_IN_MEMCMP:
3260 case BUILT_IN_MEMCPY:
3261 case BUILT_IN_MEMMOVE:
3262 case BUILT_IN_MEMSET:
3263 case BUILT_IN_STRCAT:
3264 case BUILT_IN_STRCHR:
3265 case BUILT_IN_STRCMP:
3266 case BUILT_IN_STRCPY:
3267 case BUILT_IN_STRCSPN:
3268 case BUILT_IN_STRLEN:
3269 case BUILT_IN_STRNCAT:
3270 case BUILT_IN_STRNCMP:
3271 case BUILT_IN_STRNCPY:
3272 case BUILT_IN_STRPBRK:
3273 case BUILT_IN_STRRCHR:
3274 case BUILT_IN_STRSPN:
3275 case BUILT_IN_STRSTR:
3276 return "<string.h>";
3277 case BUILT_IN_FPRINTF:
3278 case BUILT_IN_PUTC:
3279 case BUILT_IN_FPUTC:
3280 case BUILT_IN_FPUTS:
3281 case BUILT_IN_FSCANF:
3282 case BUILT_IN_FWRITE:
3283 case BUILT_IN_PRINTF:
3284 case BUILT_IN_PUTCHAR:
3285 case BUILT_IN_PUTS:
3286 case BUILT_IN_SCANF:
3287 case BUILT_IN_SNPRINTF:
3288 case BUILT_IN_SPRINTF:
3289 case BUILT_IN_SSCANF:
3290 case BUILT_IN_VFPRINTF:
3291 case BUILT_IN_VFSCANF:
3292 case BUILT_IN_VPRINTF:
3293 case BUILT_IN_VSCANF:
3294 case BUILT_IN_VSNPRINTF:
3295 case BUILT_IN_VSPRINTF:
3296 case BUILT_IN_VSSCANF:
3297 return "<stdio.h>";
3298 case BUILT_IN_ISALNUM:
3299 case BUILT_IN_ISALPHA:
3300 case BUILT_IN_ISBLANK:
3301 case BUILT_IN_ISCNTRL:
3302 case BUILT_IN_ISDIGIT:
3303 case BUILT_IN_ISGRAPH:
3304 case BUILT_IN_ISLOWER:
3305 case BUILT_IN_ISPRINT:
3306 case BUILT_IN_ISPUNCT:
3307 case BUILT_IN_ISSPACE:
3308 case BUILT_IN_ISUPPER:
3309 case BUILT_IN_ISXDIGIT:
3310 case BUILT_IN_TOLOWER:
3311 case BUILT_IN_TOUPPER:
3312 return "<ctype.h>";
3313 case BUILT_IN_ISWALNUM:
3314 case BUILT_IN_ISWALPHA:
3315 case BUILT_IN_ISWBLANK:
3316 case BUILT_IN_ISWCNTRL:
3317 case BUILT_IN_ISWDIGIT:
3318 case BUILT_IN_ISWGRAPH:
3319 case BUILT_IN_ISWLOWER:
3320 case BUILT_IN_ISWPRINT:
3321 case BUILT_IN_ISWPUNCT:
3322 case BUILT_IN_ISWSPACE:
3323 case BUILT_IN_ISWUPPER:
3324 case BUILT_IN_ISWXDIGIT:
3325 case BUILT_IN_TOWLOWER:
3326 case BUILT_IN_TOWUPPER:
3327 return "<wctype.h>";
3328 case BUILT_IN_ABORT:
3329 case BUILT_IN_ABS:
3330 case BUILT_IN_CALLOC:
3331 case BUILT_IN_EXIT:
3332 case BUILT_IN_FREE:
3333 case BUILT_IN_LABS:
3334 case BUILT_IN_LLABS:
3335 case BUILT_IN_MALLOC:
3336 case BUILT_IN_REALLOC:
3337 case BUILT_IN__EXIT2:
3338 case BUILT_IN_ALIGNED_ALLOC:
3339 return "<stdlib.h>";
3340 case BUILT_IN_IMAXABS:
3341 return "<inttypes.h>";
3342 case BUILT_IN_STRFTIME:
3343 return "<time.h>";
3344 default:
3345 return NULL;
3349 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3350 function of type int (). */
3352 tree
3353 implicitly_declare (location_t loc, tree functionid)
3355 struct c_binding *b;
3356 tree decl = NULL_TREE;
3357 tree asmspec_tree;
3359 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3361 if (B_IN_SCOPE (b, external_scope))
3363 decl = b->decl;
3364 break;
3368 if (decl)
3370 if (TREE_CODE (decl) != FUNCTION_DECL)
3371 return decl;
3373 /* FIXME: Objective-C has weird not-really-builtin functions
3374 which are supposed to be visible automatically. They wind up
3375 in the external scope because they're pushed before the file
3376 scope gets created. Catch this here and rebind them into the
3377 file scope. */
3378 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3380 bind (functionid, decl, file_scope,
3381 /*invisible=*/false, /*nested=*/true,
3382 DECL_SOURCE_LOCATION (decl));
3383 return decl;
3385 else
3387 tree newtype = default_function_type;
3388 if (b->u.type)
3389 TREE_TYPE (decl) = b->u.type;
3390 /* Implicit declaration of a function already declared
3391 (somehow) in a different scope, or as a built-in.
3392 If this is the first time this has happened, warn;
3393 then recycle the old declaration but with the new type. */
3394 if (!C_DECL_IMPLICIT (decl))
3396 implicit_decl_warning (loc, functionid, decl);
3397 C_DECL_IMPLICIT (decl) = 1;
3399 if (DECL_BUILT_IN (decl))
3401 newtype = build_type_attribute_variant (newtype,
3402 TYPE_ATTRIBUTES
3403 (TREE_TYPE (decl)));
3404 if (!comptypes (newtype, TREE_TYPE (decl)))
3406 bool warned = warning_at (loc, 0, "incompatible implicit "
3407 "declaration of built-in "
3408 "function %qD", decl);
3409 /* See if we can hint which header to include. */
3410 const char *header
3411 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3412 if (header != NULL && warned)
3414 rich_location richloc (line_table, loc);
3415 maybe_add_include_fixit (&richloc, header);
3416 inform (&richloc,
3417 "include %qs or provide a declaration of %qD",
3418 header, decl);
3420 newtype = TREE_TYPE (decl);
3423 else
3425 if (!comptypes (newtype, TREE_TYPE (decl)))
3427 error_at (loc, "incompatible implicit declaration of "
3428 "function %qD", decl);
3429 locate_old_decl (decl);
3432 b->u.type = TREE_TYPE (decl);
3433 TREE_TYPE (decl) = newtype;
3434 bind (functionid, decl, current_scope,
3435 /*invisible=*/false, /*nested=*/true,
3436 DECL_SOURCE_LOCATION (decl));
3437 return decl;
3441 /* Not seen before. */
3442 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3443 DECL_EXTERNAL (decl) = 1;
3444 TREE_PUBLIC (decl) = 1;
3445 C_DECL_IMPLICIT (decl) = 1;
3446 implicit_decl_warning (loc, functionid, 0);
3447 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3448 if (asmspec_tree)
3449 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3451 /* C89 says implicit declarations are in the innermost block.
3452 So we record the decl in the standard fashion. */
3453 decl = pushdecl (decl);
3455 /* No need to call objc_check_decl here - it's a function type. */
3456 rest_of_decl_compilation (decl, 0, 0);
3458 /* Write a record describing this implicit function declaration
3459 to the prototypes file (if requested). */
3460 gen_aux_info_record (decl, 0, 1, 0);
3462 /* Possibly apply some default attributes to this implicit declaration. */
3463 decl_attributes (&decl, NULL_TREE, 0);
3465 return decl;
3468 /* Issue an error message for a reference to an undeclared variable
3469 ID, including a reference to a builtin outside of function-call
3470 context. Establish a binding of the identifier to error_mark_node
3471 in an appropriate scope, which will suppress further errors for the
3472 same identifier. The error message should be given location LOC. */
3473 void
3474 undeclared_variable (location_t loc, tree id)
3476 static bool already = false;
3477 struct c_scope *scope;
3479 if (current_function_decl == NULL_TREE)
3481 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3482 if (guessed_id)
3484 gcc_rich_location richloc (loc);
3485 richloc.add_fixit_replace (guessed_id.suggestion ());
3486 error_at (&richloc,
3487 "%qE undeclared here (not in a function);"
3488 " did you mean %qs?",
3489 id, guessed_id.suggestion ());
3491 else
3492 error_at (loc, "%qE undeclared here (not in a function)", id);
3493 scope = current_scope;
3495 else
3497 if (!objc_diagnose_private_ivar (id))
3499 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3500 if (guessed_id)
3502 gcc_rich_location richloc (loc);
3503 richloc.add_fixit_replace (guessed_id.suggestion ());
3504 error_at (&richloc,
3505 "%qE undeclared (first use in this function);"
3506 " did you mean %qs?",
3507 id, guessed_id.suggestion ());
3509 else
3510 error_at (loc, "%qE undeclared (first use in this function)", id);
3512 if (!already)
3514 inform (loc, "each undeclared identifier is reported only"
3515 " once for each function it appears in");
3516 already = true;
3519 /* If we are parsing old-style parameter decls, current_function_decl
3520 will be nonnull but current_function_scope will be null. */
3521 scope = current_function_scope ? current_function_scope : current_scope;
3523 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3524 UNKNOWN_LOCATION);
3527 /* Subroutine of lookup_label, declare_label, define_label: construct a
3528 LABEL_DECL with all the proper frills. Also create a struct
3529 c_label_vars initialized for the current scope. */
3531 static tree
3532 make_label (location_t location, tree name, bool defining,
3533 struct c_label_vars **p_label_vars)
3535 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3536 DECL_CONTEXT (label) = current_function_decl;
3537 SET_DECL_MODE (label, VOIDmode);
3539 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3540 label_vars->shadowed = NULL;
3541 set_spot_bindings (&label_vars->label_bindings, defining);
3542 label_vars->decls_in_scope = make_tree_vector ();
3543 label_vars->gotos = NULL;
3544 *p_label_vars = label_vars;
3546 return label;
3549 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3550 Create one if none exists so far for the current function.
3551 This is called when a label is used in a goto expression or
3552 has its address taken. */
3554 tree
3555 lookup_label (tree name)
3557 tree label;
3558 struct c_label_vars *label_vars;
3560 if (current_function_scope == 0)
3562 error ("label %qE referenced outside of any function", name);
3563 return NULL_TREE;
3566 /* Use a label already defined or ref'd with this name, but not if
3567 it is inherited from a containing function and wasn't declared
3568 using __label__. */
3569 label = I_LABEL_DECL (name);
3570 if (label && (DECL_CONTEXT (label) == current_function_decl
3571 || C_DECLARED_LABEL_FLAG (label)))
3573 /* If the label has only been declared, update its apparent
3574 location to point here, for better diagnostics if it
3575 turns out not to have been defined. */
3576 if (DECL_INITIAL (label) == NULL_TREE)
3577 DECL_SOURCE_LOCATION (label) = input_location;
3578 return label;
3581 /* No label binding for that identifier; make one. */
3582 label = make_label (input_location, name, false, &label_vars);
3584 /* Ordinary labels go in the current function scope. */
3585 bind_label (name, label, current_function_scope, label_vars);
3587 return label;
3590 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3591 to LABEL. */
3593 static void
3594 warn_about_goto (location_t goto_loc, tree label, tree decl)
3596 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3597 error_at (goto_loc,
3598 "jump into scope of identifier with variably modified type");
3599 else
3600 warning_at (goto_loc, OPT_Wjump_misses_init,
3601 "jump skips variable initialization");
3602 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3603 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3606 /* Look up a label because of a goto statement. This is like
3607 lookup_label, but also issues any appropriate warnings. */
3609 tree
3610 lookup_label_for_goto (location_t loc, tree name)
3612 tree label;
3613 struct c_label_vars *label_vars;
3614 unsigned int ix;
3615 tree decl;
3617 label = lookup_label (name);
3618 if (label == NULL_TREE)
3619 return NULL_TREE;
3621 /* If we are jumping to a different function, we can't issue any
3622 useful warnings. */
3623 if (DECL_CONTEXT (label) != current_function_decl)
3625 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3626 return label;
3629 label_vars = I_LABEL_BINDING (name)->u.label;
3631 /* If the label has not yet been defined, then push this goto on a
3632 list for possible later warnings. */
3633 if (label_vars->label_bindings.scope == NULL)
3635 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3637 g->loc = loc;
3638 set_spot_bindings (&g->goto_bindings, true);
3639 vec_safe_push (label_vars->gotos, g);
3640 return label;
3643 /* If there are any decls in label_vars->decls_in_scope, then this
3644 goto has missed the declaration of the decl. This happens for a
3645 case like
3646 int i = 1;
3647 lab:
3649 goto lab;
3650 Issue a warning or error. */
3651 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3652 warn_about_goto (loc, label, decl);
3654 if (label_vars->label_bindings.left_stmt_expr)
3656 error_at (loc, "jump into statement expression");
3657 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3660 return label;
3663 /* Make a label named NAME in the current function, shadowing silently
3664 any that may be inherited from containing functions or containing
3665 scopes. This is called for __label__ declarations. */
3667 tree
3668 declare_label (tree name)
3670 struct c_binding *b = I_LABEL_BINDING (name);
3671 tree label;
3672 struct c_label_vars *label_vars;
3674 /* Check to make sure that the label hasn't already been declared
3675 at this scope */
3676 if (b && B_IN_CURRENT_SCOPE (b))
3678 error ("duplicate label declaration %qE", name);
3679 locate_old_decl (b->decl);
3681 /* Just use the previous declaration. */
3682 return b->decl;
3685 label = make_label (input_location, name, false, &label_vars);
3686 C_DECLARED_LABEL_FLAG (label) = 1;
3688 /* Declared labels go in the current scope. */
3689 bind_label (name, label, current_scope, label_vars);
3691 return label;
3694 /* When we define a label, issue any appropriate warnings if there are
3695 any gotos earlier in the function which jump to this label. */
3697 static void
3698 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3700 unsigned int ix;
3701 struct c_goto_bindings *g;
3703 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3705 struct c_binding *b;
3706 struct c_scope *scope;
3708 /* We have a goto to this label. The goto is going forward. In
3709 g->scope, the goto is going to skip any binding which was
3710 defined after g->bindings_in_scope. */
3711 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3713 for (b = g->goto_bindings.scope->bindings;
3714 b != g->goto_bindings.bindings_in_scope;
3715 b = b->prev)
3717 if (decl_jump_unsafe (b->decl))
3718 warn_about_goto (g->loc, label, b->decl);
3722 /* We also need to warn about decls defined in any scopes
3723 between the scope of the label and the scope of the goto. */
3724 for (scope = label_vars->label_bindings.scope;
3725 scope != g->goto_bindings.scope;
3726 scope = scope->outer)
3728 gcc_assert (scope != NULL);
3729 if (scope->has_jump_unsafe_decl)
3731 if (scope == label_vars->label_bindings.scope)
3732 b = label_vars->label_bindings.bindings_in_scope;
3733 else
3734 b = scope->bindings;
3735 for (; b != NULL; b = b->prev)
3737 if (decl_jump_unsafe (b->decl))
3738 warn_about_goto (g->loc, label, b->decl);
3743 if (g->goto_bindings.stmt_exprs > 0)
3745 error_at (g->loc, "jump into statement expression");
3746 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3747 label);
3751 /* Now that the label is defined, we will issue warnings about
3752 subsequent gotos to this label when we see them. */
3753 vec_safe_truncate (label_vars->gotos, 0);
3754 label_vars->gotos = NULL;
3757 /* Define a label, specifying the location in the source file.
3758 Return the LABEL_DECL node for the label, if the definition is valid.
3759 Otherwise return NULL_TREE. */
3761 tree
3762 define_label (location_t location, tree name)
3764 /* Find any preexisting label with this name. It is an error
3765 if that label has already been defined in this function, or
3766 if there is a containing function with a declared label with
3767 the same name. */
3768 tree label = I_LABEL_DECL (name);
3770 if (label
3771 && ((DECL_CONTEXT (label) == current_function_decl
3772 && DECL_INITIAL (label) != NULL_TREE)
3773 || (DECL_CONTEXT (label) != current_function_decl
3774 && C_DECLARED_LABEL_FLAG (label))))
3776 error_at (location, "duplicate label %qD", label);
3777 locate_old_decl (label);
3778 return NULL_TREE;
3780 else if (label && DECL_CONTEXT (label) == current_function_decl)
3782 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3784 /* The label has been used or declared already in this function,
3785 but not defined. Update its location to point to this
3786 definition. */
3787 DECL_SOURCE_LOCATION (label) = location;
3788 set_spot_bindings (&label_vars->label_bindings, true);
3790 /* Issue warnings as required about any goto statements from
3791 earlier in the function. */
3792 check_earlier_gotos (label, label_vars);
3794 else
3796 struct c_label_vars *label_vars;
3798 /* No label binding for that identifier; make one. */
3799 label = make_label (location, name, true, &label_vars);
3801 /* Ordinary labels go in the current function scope. */
3802 bind_label (name, label, current_function_scope, label_vars);
3805 if (!in_system_header_at (input_location) && lookup_name (name))
3806 warning_at (location, OPT_Wtraditional,
3807 "traditional C lacks a separate namespace "
3808 "for labels, identifier %qE conflicts", name);
3810 /* Mark label as having been defined. */
3811 DECL_INITIAL (label) = error_mark_node;
3812 return label;
3815 /* Get the bindings for a new switch statement. This is used to issue
3816 warnings as appropriate for jumps from the switch to case or
3817 default labels. */
3819 struct c_spot_bindings *
3820 c_get_switch_bindings (void)
3822 struct c_spot_bindings *switch_bindings;
3824 switch_bindings = XNEW (struct c_spot_bindings);
3825 set_spot_bindings (switch_bindings, true);
3826 return switch_bindings;
3829 void
3830 c_release_switch_bindings (struct c_spot_bindings *bindings)
3832 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3833 XDELETE (bindings);
3836 /* This is called at the point of a case or default label to issue
3837 warnings about decls as needed. It returns true if it found an
3838 error, not just a warning. */
3840 bool
3841 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3842 location_t switch_loc, location_t case_loc)
3844 bool saw_error;
3845 struct c_scope *scope;
3847 saw_error = false;
3848 for (scope = current_scope;
3849 scope != switch_bindings->scope;
3850 scope = scope->outer)
3852 struct c_binding *b;
3854 gcc_assert (scope != NULL);
3856 if (!scope->has_jump_unsafe_decl)
3857 continue;
3859 for (b = scope->bindings; b != NULL; b = b->prev)
3861 if (decl_jump_unsafe (b->decl))
3863 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3865 saw_error = true;
3866 error_at (case_loc,
3867 ("switch jumps into scope of identifier with "
3868 "variably modified type"));
3870 else
3871 warning_at (case_loc, OPT_Wjump_misses_init,
3872 "switch jumps over variable initialization");
3873 inform (switch_loc, "switch starts here");
3874 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3875 b->decl);
3880 if (switch_bindings->stmt_exprs > 0)
3882 saw_error = true;
3883 error_at (case_loc, "switch jumps into statement expression");
3884 inform (switch_loc, "switch starts here");
3887 return saw_error;
3890 /* Given NAME, an IDENTIFIER_NODE,
3891 return the structure (or union or enum) definition for that name.
3892 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3893 CODE says which kind of type the caller wants;
3894 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3895 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3896 location where the tag was defined.
3897 If the wrong kind of type is found, an error is reported. */
3899 static tree
3900 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3901 location_t *ploc)
3903 struct c_binding *b = I_TAG_BINDING (name);
3904 bool thislevel = false;
3906 if (!b || !b->decl)
3907 return NULL_TREE;
3909 /* We only care about whether it's in this level if
3910 thislevel_only was set or it might be a type clash. */
3911 if (thislevel_only || TREE_CODE (b->decl) != code)
3913 /* For our purposes, a tag in the external scope is the same as
3914 a tag in the file scope. (Primarily relevant to Objective-C
3915 and its builtin structure tags, which get pushed before the
3916 file scope is created.) */
3917 if (B_IN_CURRENT_SCOPE (b)
3918 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3919 thislevel = true;
3922 if (thislevel_only && !thislevel)
3923 return NULL_TREE;
3925 if (TREE_CODE (b->decl) != code)
3927 /* Definition isn't the kind we were looking for. */
3928 pending_invalid_xref = name;
3929 pending_invalid_xref_location = input_location;
3931 /* If in the same binding level as a declaration as a tag
3932 of a different type, this must not be allowed to
3933 shadow that tag, so give the error immediately.
3934 (For example, "struct foo; union foo;" is invalid.) */
3935 if (thislevel)
3936 pending_xref_error ();
3939 if (ploc != NULL)
3940 *ploc = b->locus;
3942 return b->decl;
3945 /* Return true if a definition exists for NAME with code CODE. */
3947 bool
3948 tag_exists_p (enum tree_code code, tree name)
3950 struct c_binding *b = I_TAG_BINDING (name);
3952 if (b == NULL || b->decl == NULL_TREE)
3953 return false;
3954 return TREE_CODE (b->decl) == code;
3957 /* Print an error message now
3958 for a recent invalid struct, union or enum cross reference.
3959 We don't print them immediately because they are not invalid
3960 when used in the `struct foo;' construct for shadowing. */
3962 void
3963 pending_xref_error (void)
3965 if (pending_invalid_xref != NULL_TREE)
3966 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3967 pending_invalid_xref);
3968 pending_invalid_xref = NULL_TREE;
3972 /* Look up NAME in the current scope and its superiors
3973 in the namespace of variables, functions and typedefs.
3974 Return a ..._DECL node of some kind representing its definition,
3975 or return NULL_TREE if it is undefined. */
3977 tree
3978 lookup_name (tree name)
3980 struct c_binding *b = I_SYMBOL_BINDING (name);
3981 if (b && !b->invisible)
3983 maybe_record_typedef_use (b->decl);
3984 return b->decl;
3986 return NULL_TREE;
3989 /* Similar to `lookup_name' but look only at the indicated scope. */
3991 static tree
3992 lookup_name_in_scope (tree name, struct c_scope *scope)
3994 struct c_binding *b;
3996 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3997 if (B_IN_SCOPE (b, scope))
3998 return b->decl;
3999 return NULL_TREE;
4002 /* Look for the closest match for NAME within the currently valid
4003 scopes.
4005 This finds the identifier with the lowest Levenshtein distance to
4006 NAME. If there are multiple candidates with equal minimal distance,
4007 the first one found is returned. Scopes are searched from innermost
4008 outwards, and within a scope in reverse order of declaration, thus
4009 benefiting candidates "near" to the current scope.
4011 The function also looks for similar macro names to NAME, since a
4012 misspelled macro name will not be expanded, and hence looks like an
4013 identifier to the C frontend.
4015 It also looks for start_typename keywords, to detect "singed" vs "signed"
4016 typos.
4018 Use LOC for any deferred diagnostics. */
4020 name_hint
4021 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4023 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4025 /* First, try some well-known names in the C standard library, in case
4026 the user forgot a #include. */
4027 const char *header_hint
4028 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4030 if (header_hint)
4031 return name_hint (NULL,
4032 new suggest_missing_header (loc,
4033 IDENTIFIER_POINTER (name),
4034 header_hint));
4036 /* Only suggest names reserved for the implementation if NAME begins
4037 with an underscore. */
4038 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4040 best_match<tree, tree> bm (name);
4042 /* Look within currently valid scopes. */
4043 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4044 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4046 if (!binding->id || binding->invisible)
4047 continue;
4048 if (binding->decl == error_mark_node)
4049 continue;
4050 /* Don't use bindings from implicitly declared functions,
4051 as they were likely misspellings themselves. */
4052 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4053 if (C_DECL_IMPLICIT (binding->decl))
4054 continue;
4055 /* Don't suggest names that are reserved for use by the
4056 implementation, unless NAME began with an underscore. */
4057 if (!consider_implementation_names)
4059 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4060 if (name_reserved_for_implementation_p (suggestion_str))
4061 continue;
4063 switch (kind)
4065 case FUZZY_LOOKUP_TYPENAME:
4066 if (TREE_CODE (binding->decl) != TYPE_DECL)
4067 continue;
4068 break;
4070 case FUZZY_LOOKUP_FUNCTION_NAME:
4071 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4073 /* Allow function pointers. */
4074 if ((VAR_P (binding->decl)
4075 || TREE_CODE (binding->decl) == PARM_DECL)
4076 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4077 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4078 == FUNCTION_TYPE))
4079 break;
4080 continue;
4082 break;
4084 default:
4085 break;
4087 bm.consider (binding->id);
4090 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4092 x = SOME_OTHER_MACRO (y);
4093 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4094 as a misspelled identifier.
4096 Use the best distance so far so that a candidate is only set if
4097 a macro is better than anything so far. This allows early rejection
4098 (without calculating the edit distance) of macro names that must have
4099 distance >= bm.get_best_distance (), and means that we only get a
4100 non-NULL result for best_macro_match if it's better than any of
4101 the identifiers already checked, which avoids needless creation
4102 of identifiers for macro hashnodes. */
4103 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4104 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4105 /* If a macro is the closest so far to NAME, use it, creating an
4106 identifier tree node for it. */
4107 if (best_macro)
4109 const char *id = (const char *)best_macro->ident.str;
4110 tree macro_as_identifier
4111 = get_identifier_with_length (id, best_macro->ident.len);
4112 bm.set_best_so_far (macro_as_identifier,
4113 bmm.get_best_distance (),
4114 bmm.get_best_candidate_length ());
4117 /* Try the "start_typename" keywords to detect
4118 "singed" vs "signed" typos. */
4119 if (kind == FUZZY_LOOKUP_TYPENAME)
4121 for (unsigned i = 0; i < num_c_common_reswords; i++)
4123 const c_common_resword *resword = &c_common_reswords[i];
4124 if (!c_keyword_starts_typename (resword->rid))
4125 continue;
4126 tree resword_identifier = ridpointers [resword->rid];
4127 if (!resword_identifier)
4128 continue;
4129 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4130 bm.consider (resword_identifier);
4134 tree best = bm.get_best_meaningful_candidate ();
4135 if (best)
4136 return name_hint (IDENTIFIER_POINTER (best), NULL);
4137 else
4138 return name_hint (NULL, NULL);
4142 /* Create the predefined scalar types of C,
4143 and some nodes representing standard constants (0, 1, (void *) 0).
4144 Initialize the global scope.
4145 Make definitions for built-in primitive functions. */
4147 void
4148 c_init_decl_processing (void)
4150 location_t save_loc = input_location;
4152 /* Initialize reserved words for parser. */
4153 c_parse_init ();
4155 current_function_decl = NULL_TREE;
4157 gcc_obstack_init (&parser_obstack);
4159 /* Make the externals scope. */
4160 push_scope ();
4161 external_scope = current_scope;
4163 /* Declarations from c_common_nodes_and_builtins must not be associated
4164 with this input file, lest we get differences between using and not
4165 using preprocessed headers. */
4166 input_location = BUILTINS_LOCATION;
4168 c_common_nodes_and_builtins ();
4170 /* In C, comparisons and TRUTH_* expressions have type int. */
4171 truthvalue_type_node = integer_type_node;
4172 truthvalue_true_node = integer_one_node;
4173 truthvalue_false_node = integer_zero_node;
4175 /* Even in C99, which has a real boolean type. */
4176 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4177 boolean_type_node));
4179 input_location = save_loc;
4181 make_fname_decl = c_make_fname_decl;
4182 start_fname_decls ();
4185 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4186 give the decl, NAME is the initialization string and TYPE_DEP
4187 indicates whether NAME depended on the type of the function. As we
4188 don't yet implement delayed emission of static data, we mark the
4189 decl as emitted so it is not placed in the output. Anything using
4190 it must therefore pull out the STRING_CST initializer directly.
4191 FIXME. */
4193 static tree
4194 c_make_fname_decl (location_t loc, tree id, int type_dep)
4196 const char *name = fname_as_string (type_dep);
4197 tree decl, type, init;
4198 size_t length = strlen (name);
4200 type = build_array_type (char_type_node,
4201 build_index_type (size_int (length)));
4202 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4204 decl = build_decl (loc, VAR_DECL, id, type);
4206 TREE_STATIC (decl) = 1;
4207 TREE_READONLY (decl) = 1;
4208 DECL_ARTIFICIAL (decl) = 1;
4210 init = build_string (length + 1, name);
4211 free (CONST_CAST (char *, name));
4212 TREE_TYPE (init) = type;
4213 DECL_INITIAL (decl) = init;
4215 TREE_USED (decl) = 1;
4217 if (current_function_decl
4218 /* For invalid programs like this:
4220 void foo()
4221 const char* p = __FUNCTION__;
4223 the __FUNCTION__ is believed to appear in K&R style function
4224 parameter declarator. In that case we still don't have
4225 function_scope. */
4226 && current_function_scope)
4228 DECL_CONTEXT (decl) = current_function_decl;
4229 bind (id, decl, current_function_scope,
4230 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4233 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4235 return decl;
4238 tree
4239 c_builtin_function (tree decl)
4241 tree type = TREE_TYPE (decl);
4242 tree id = DECL_NAME (decl);
4244 const char *name = IDENTIFIER_POINTER (id);
4245 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4247 /* Should never be called on a symbol with a preexisting meaning. */
4248 gcc_assert (!I_SYMBOL_BINDING (id));
4250 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4251 UNKNOWN_LOCATION);
4253 /* Builtins in the implementation namespace are made visible without
4254 needing to be explicitly declared. See push_file_scope. */
4255 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4257 DECL_CHAIN (decl) = visible_builtins;
4258 visible_builtins = decl;
4261 return decl;
4264 tree
4265 c_builtin_function_ext_scope (tree decl)
4267 tree type = TREE_TYPE (decl);
4268 tree id = DECL_NAME (decl);
4270 const char *name = IDENTIFIER_POINTER (id);
4271 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4273 if (external_scope)
4274 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4275 UNKNOWN_LOCATION);
4277 /* Builtins in the implementation namespace are made visible without
4278 needing to be explicitly declared. See push_file_scope. */
4279 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4281 DECL_CHAIN (decl) = visible_builtins;
4282 visible_builtins = decl;
4285 return decl;
4288 /* Called when a declaration is seen that contains no names to declare.
4289 If its type is a reference to a structure, union or enum inherited
4290 from a containing scope, shadow that tag name for the current scope
4291 with a forward reference.
4292 If its type defines a new named structure or union
4293 or defines an enum, it is valid but we need not do anything here.
4294 Otherwise, it is an error. */
4296 void
4297 shadow_tag (const struct c_declspecs *declspecs)
4299 shadow_tag_warned (declspecs, 0);
4302 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4303 but no pedwarn. */
4304 void
4305 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4307 bool found_tag = false;
4309 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4311 tree value = declspecs->type;
4312 enum tree_code code = TREE_CODE (value);
4314 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4315 /* Used to test also that TYPE_SIZE (value) != 0.
4316 That caused warning for `struct foo;' at top level in the file. */
4318 tree name = TYPE_NAME (value);
4319 tree t;
4321 found_tag = true;
4323 if (declspecs->restrict_p)
4325 error ("invalid use of %<restrict%>");
4326 warned = 1;
4329 if (name == NULL_TREE)
4331 if (warned != 1 && code != ENUMERAL_TYPE)
4332 /* Empty unnamed enum OK */
4334 pedwarn (input_location, 0,
4335 "unnamed struct/union that defines no instances");
4336 warned = 1;
4339 else if (declspecs->typespec_kind != ctsk_tagdef
4340 && declspecs->typespec_kind != ctsk_tagfirstref
4341 && declspecs->storage_class != csc_none)
4343 if (warned != 1)
4344 pedwarn (input_location, 0,
4345 "empty declaration with storage class specifier "
4346 "does not redeclare tag");
4347 warned = 1;
4348 pending_xref_error ();
4350 else if (declspecs->typespec_kind != ctsk_tagdef
4351 && declspecs->typespec_kind != ctsk_tagfirstref
4352 && (declspecs->const_p
4353 || declspecs->volatile_p
4354 || declspecs->atomic_p
4355 || declspecs->restrict_p
4356 || declspecs->address_space))
4358 if (warned != 1)
4359 pedwarn (input_location, 0,
4360 "empty declaration with type qualifier "
4361 "does not redeclare tag");
4362 warned = 1;
4363 pending_xref_error ();
4365 else if (declspecs->typespec_kind != ctsk_tagdef
4366 && declspecs->typespec_kind != ctsk_tagfirstref
4367 && declspecs->alignas_p)
4369 if (warned != 1)
4370 pedwarn (input_location, 0,
4371 "empty declaration with %<_Alignas%> "
4372 "does not redeclare tag");
4373 warned = 1;
4374 pending_xref_error ();
4376 else
4378 pending_invalid_xref = NULL_TREE;
4379 t = lookup_tag (code, name, true, NULL);
4381 if (t == NULL_TREE)
4383 t = make_node (code);
4384 pushtag (input_location, name, t);
4388 else
4390 if (warned != 1 && !in_system_header_at (input_location))
4392 pedwarn (input_location, 0,
4393 "useless type name in empty declaration");
4394 warned = 1;
4398 else if (warned != 1 && !in_system_header_at (input_location)
4399 && declspecs->typedef_p)
4401 pedwarn (input_location, 0, "useless type name in empty declaration");
4402 warned = 1;
4405 pending_invalid_xref = NULL_TREE;
4407 if (declspecs->inline_p)
4409 error ("%<inline%> in empty declaration");
4410 warned = 1;
4413 if (declspecs->noreturn_p)
4415 error ("%<_Noreturn%> in empty declaration");
4416 warned = 1;
4419 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4421 error ("%<auto%> in file-scope empty declaration");
4422 warned = 1;
4425 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4427 error ("%<register%> in file-scope empty declaration");
4428 warned = 1;
4431 if (!warned && !in_system_header_at (input_location)
4432 && declspecs->storage_class != csc_none)
4434 warning (0, "useless storage class specifier in empty declaration");
4435 warned = 2;
4438 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4440 warning (0, "useless %qs in empty declaration",
4441 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4442 warned = 2;
4445 if (!warned
4446 && !in_system_header_at (input_location)
4447 && (declspecs->const_p
4448 || declspecs->volatile_p
4449 || declspecs->atomic_p
4450 || declspecs->restrict_p
4451 || declspecs->address_space))
4453 warning (0, "useless type qualifier in empty declaration");
4454 warned = 2;
4457 if (!warned && !in_system_header_at (input_location)
4458 && declspecs->alignas_p)
4460 warning (0, "useless %<_Alignas%> in empty declaration");
4461 warned = 2;
4464 if (warned != 1)
4466 if (!found_tag)
4467 pedwarn (input_location, 0, "empty declaration");
4472 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4473 bits. SPECS represents declaration specifiers that the grammar
4474 only permits to contain type qualifiers and attributes. */
4477 quals_from_declspecs (const struct c_declspecs *specs)
4479 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4480 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4481 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4482 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4483 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4484 gcc_assert (!specs->type
4485 && !specs->decl_attr
4486 && specs->typespec_word == cts_none
4487 && specs->storage_class == csc_none
4488 && !specs->typedef_p
4489 && !specs->explicit_signed_p
4490 && !specs->deprecated_p
4491 && !specs->long_p
4492 && !specs->long_long_p
4493 && !specs->short_p
4494 && !specs->signed_p
4495 && !specs->unsigned_p
4496 && !specs->complex_p
4497 && !specs->inline_p
4498 && !specs->noreturn_p
4499 && !specs->thread_p);
4500 return quals;
4503 /* Construct an array declarator. LOC is the location of the
4504 beginning of the array (usually the opening brace). EXPR is the
4505 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4506 inside the [] (to be applied to the pointer to which a parameter
4507 array is converted). STATIC_P is true if "static" is inside the
4508 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4509 VLA of unspecified length which is nevertheless a complete type,
4510 false otherwise. The field for the contained declarator is left to
4511 be filled in by set_array_declarator_inner. */
4513 struct c_declarator *
4514 build_array_declarator (location_t loc,
4515 tree expr, struct c_declspecs *quals, bool static_p,
4516 bool vla_unspec_p)
4518 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4519 struct c_declarator);
4520 declarator->id_loc = loc;
4521 declarator->kind = cdk_array;
4522 declarator->declarator = 0;
4523 declarator->u.array.dimen = expr;
4524 if (quals)
4526 declarator->u.array.attrs = quals->attrs;
4527 declarator->u.array.quals = quals_from_declspecs (quals);
4529 else
4531 declarator->u.array.attrs = NULL_TREE;
4532 declarator->u.array.quals = 0;
4534 declarator->u.array.static_p = static_p;
4535 declarator->u.array.vla_unspec_p = vla_unspec_p;
4536 if (static_p || quals != NULL)
4537 pedwarn_c90 (loc, OPT_Wpedantic,
4538 "ISO C90 does not support %<static%> or type "
4539 "qualifiers in parameter array declarators");
4540 if (vla_unspec_p)
4541 pedwarn_c90 (loc, OPT_Wpedantic,
4542 "ISO C90 does not support %<[*]%> array declarators");
4543 if (vla_unspec_p)
4545 if (!current_scope->parm_flag)
4547 /* C99 6.7.5.2p4 */
4548 error_at (loc, "%<[*]%> not allowed in other than "
4549 "function prototype scope");
4550 declarator->u.array.vla_unspec_p = false;
4551 return NULL;
4553 current_scope->had_vla_unspec = true;
4555 return declarator;
4558 /* Set the contained declarator of an array declarator. DECL is the
4559 declarator, as constructed by build_array_declarator; INNER is what
4560 appears on the left of the []. */
4562 struct c_declarator *
4563 set_array_declarator_inner (struct c_declarator *decl,
4564 struct c_declarator *inner)
4566 decl->declarator = inner;
4567 return decl;
4570 /* INIT is a constructor that forms DECL's initializer. If the final
4571 element initializes a flexible array field, add the size of that
4572 initializer to DECL's size. */
4574 static void
4575 add_flexible_array_elts_to_size (tree decl, tree init)
4577 tree elt, type;
4579 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4580 return;
4582 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4583 type = TREE_TYPE (elt);
4584 if (TREE_CODE (type) == ARRAY_TYPE
4585 && TYPE_SIZE (type) == NULL_TREE
4586 && TYPE_DOMAIN (type) != NULL_TREE
4587 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4589 complete_array_type (&type, elt, false);
4590 DECL_SIZE (decl)
4591 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4592 DECL_SIZE_UNIT (decl)
4593 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4597 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4598 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4599 before the type name, and set *EXPR_CONST_OPERANDS, if
4600 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4601 appear in a constant expression. */
4603 tree
4604 groktypename (struct c_type_name *type_name, tree *expr,
4605 bool *expr_const_operands)
4607 tree type;
4608 tree attrs = type_name->specs->attrs;
4610 type_name->specs->attrs = NULL_TREE;
4612 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4613 false, NULL, &attrs, expr, expr_const_operands,
4614 DEPRECATED_NORMAL);
4616 /* Apply attributes. */
4617 decl_attributes (&type, attrs, 0);
4619 return type;
4622 /* Wrapper for decl_attributes that adds some implicit attributes
4623 to VAR_DECLs or FUNCTION_DECLs. */
4625 static tree
4626 c_decl_attributes (tree *node, tree attributes, int flags)
4628 /* Add implicit "omp declare target" attribute if requested. */
4629 if (current_omp_declare_target_attribute
4630 && ((VAR_P (*node) && is_global_var (*node))
4631 || TREE_CODE (*node) == FUNCTION_DECL))
4633 if (VAR_P (*node)
4634 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4635 error ("%q+D in declare target directive does not have mappable type",
4636 *node);
4637 else
4638 attributes = tree_cons (get_identifier ("omp declare target"),
4639 NULL_TREE, attributes);
4642 /* Look up the current declaration with all the attributes merged
4643 so far so that attributes on the current declaration that's
4644 about to be pushed that conflict with the former can be detected,
4645 diagnosed, and rejected as appropriate. */
4646 tree last_decl = lookup_name (DECL_NAME (*node));
4647 if (!last_decl)
4648 last_decl = lookup_name_in_scope (DECL_NAME (*node), external_scope);
4650 return decl_attributes (node, attributes, flags, last_decl);
4654 /* Decode a declarator in an ordinary declaration or data definition.
4655 This is called as soon as the type information and variable name
4656 have been parsed, before parsing the initializer if any.
4657 Here we create the ..._DECL node, fill in its type,
4658 and put it on the list of decls for the current context.
4659 The ..._DECL node is returned as the value.
4661 Exception: for arrays where the length is not specified,
4662 the type is left null, to be filled in by `finish_decl'.
4664 Function definitions do not come here; they go to start_function
4665 instead. However, external and forward declarations of functions
4666 do go through here. Structure field declarations are done by
4667 grokfield and not through here. */
4669 tree
4670 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4671 bool initialized, tree attributes)
4673 tree decl;
4674 tree tem;
4675 tree expr = NULL_TREE;
4676 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4678 /* An object declared as __attribute__((deprecated)) suppresses
4679 warnings of uses of other deprecated items. */
4680 if (lookup_attribute ("deprecated", attributes))
4681 deprecated_state = DEPRECATED_SUPPRESS;
4683 decl = grokdeclarator (declarator, declspecs,
4684 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4685 deprecated_state);
4686 if (!decl || decl == error_mark_node)
4687 return NULL_TREE;
4689 if (expr)
4690 add_stmt (fold_convert (void_type_node, expr));
4692 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4693 warning (OPT_Wmain, "%q+D is usually a function", decl);
4695 if (initialized)
4696 /* Is it valid for this decl to have an initializer at all?
4697 If not, set INITIALIZED to zero, which will indirectly
4698 tell 'finish_decl' to ignore the initializer once it is parsed. */
4699 switch (TREE_CODE (decl))
4701 case TYPE_DECL:
4702 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4703 initialized = false;
4704 break;
4706 case FUNCTION_DECL:
4707 error ("function %qD is initialized like a variable", decl);
4708 initialized = false;
4709 break;
4711 case PARM_DECL:
4712 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4713 error ("parameter %qD is initialized", decl);
4714 initialized = false;
4715 break;
4717 default:
4718 /* Don't allow initializations for incomplete types except for
4719 arrays which might be completed by the initialization. */
4721 /* This can happen if the array size is an undefined macro.
4722 We already gave a warning, so we don't need another one. */
4723 if (TREE_TYPE (decl) == error_mark_node)
4724 initialized = false;
4725 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4727 /* A complete type is ok if size is fixed. */
4729 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4730 || C_DECL_VARIABLE_SIZE (decl))
4732 error ("variable-sized object may not be initialized");
4733 initialized = false;
4736 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4738 error ("variable %qD has initializer but incomplete type", decl);
4739 initialized = false;
4741 else if (C_DECL_VARIABLE_SIZE (decl))
4743 /* Although C99 is unclear about whether incomplete arrays
4744 of VLAs themselves count as VLAs, it does not make
4745 sense to permit them to be initialized given that
4746 ordinary VLAs may not be initialized. */
4747 error ("variable-sized object may not be initialized");
4748 initialized = false;
4752 if (initialized)
4754 if (current_scope == file_scope)
4755 TREE_STATIC (decl) = 1;
4757 /* Tell 'pushdecl' this is an initialized decl
4758 even though we don't yet have the initializer expression.
4759 Also tell 'finish_decl' it may store the real initializer. */
4760 DECL_INITIAL (decl) = error_mark_node;
4763 /* If this is a function declaration, write a record describing it to the
4764 prototypes file (if requested). */
4766 if (TREE_CODE (decl) == FUNCTION_DECL)
4767 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4769 /* ANSI specifies that a tentative definition which is not merged with
4770 a non-tentative definition behaves exactly like a definition with an
4771 initializer equal to zero. (Section 3.7.2)
4773 -fno-common gives strict ANSI behavior, though this tends to break
4774 a large body of code that grew up without this rule.
4776 Thread-local variables are never common, since there's no entrenched
4777 body of code to break, and it allows more efficient variable references
4778 in the presence of dynamic linking. */
4780 if (VAR_P (decl)
4781 && !initialized
4782 && TREE_PUBLIC (decl)
4783 && !DECL_THREAD_LOCAL_P (decl)
4784 && !flag_no_common)
4785 DECL_COMMON (decl) = 1;
4787 /* Set attributes here so if duplicate decl, will have proper attributes. */
4788 c_decl_attributes (&decl, attributes, 0);
4790 /* Handle gnu_inline attribute. */
4791 if (declspecs->inline_p
4792 && !flag_gnu89_inline
4793 && TREE_CODE (decl) == FUNCTION_DECL
4794 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4795 || current_function_decl))
4797 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4799 else if (declspecs->storage_class != csc_static)
4800 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4803 if (TREE_CODE (decl) == FUNCTION_DECL
4804 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4806 struct c_declarator *ce = declarator;
4808 if (ce->kind == cdk_pointer)
4809 ce = declarator->declarator;
4810 if (ce->kind == cdk_function)
4812 tree args = ce->u.arg_info->parms;
4813 for (; args; args = DECL_CHAIN (args))
4815 tree type = TREE_TYPE (args);
4816 if (type && INTEGRAL_TYPE_P (type)
4817 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4818 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4823 if (TREE_CODE (decl) == FUNCTION_DECL
4824 && DECL_DECLARED_INLINE_P (decl)
4825 && DECL_UNINLINABLE (decl)
4826 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4827 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4828 decl);
4830 /* C99 6.7.4p3: An inline definition of a function with external
4831 linkage shall not contain a definition of a modifiable object
4832 with static storage duration... */
4833 if (VAR_P (decl)
4834 && current_scope != file_scope
4835 && TREE_STATIC (decl)
4836 && !TREE_READONLY (decl)
4837 && DECL_DECLARED_INLINE_P (current_function_decl)
4838 && DECL_EXTERNAL (current_function_decl))
4839 record_inline_static (input_location, current_function_decl,
4840 decl, csi_modifiable);
4842 if (c_dialect_objc ()
4843 && VAR_OR_FUNCTION_DECL_P (decl))
4844 objc_check_global_decl (decl);
4846 /* Add this decl to the current scope.
4847 TEM may equal DECL or it may be a previous decl of the same name. */
4848 tem = pushdecl (decl);
4850 if (initialized && DECL_EXTERNAL (tem))
4852 DECL_EXTERNAL (tem) = 0;
4853 TREE_STATIC (tem) = 1;
4856 return tem;
4859 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4860 DECL or the non-array element type if DECL is an uninitialized array.
4861 If that type has a const member, diagnose this. */
4863 static void
4864 diagnose_uninitialized_cst_member (tree decl, tree type)
4866 tree field;
4867 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4869 tree field_type;
4870 if (TREE_CODE (field) != FIELD_DECL)
4871 continue;
4872 field_type = strip_array_types (TREE_TYPE (field));
4874 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4876 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4877 "uninitialized const member in %qT is invalid in C++",
4878 strip_array_types (TREE_TYPE (decl)));
4879 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4882 if (RECORD_OR_UNION_TYPE_P (field_type))
4883 diagnose_uninitialized_cst_member (decl, field_type);
4887 /* Finish processing of a declaration;
4888 install its initial value.
4889 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4890 If the length of an array type is not known before,
4891 it must be determined now, from the initial value, or it is an error.
4893 INIT_LOC is the location of the initial value. */
4895 void
4896 finish_decl (tree decl, location_t init_loc, tree init,
4897 tree origtype, tree asmspec_tree)
4899 tree type;
4900 bool was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
4901 const char *asmspec = 0;
4903 /* If a name was specified, get the string. */
4904 if (VAR_OR_FUNCTION_DECL_P (decl)
4905 && DECL_FILE_SCOPE_P (decl))
4906 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4907 if (asmspec_tree)
4908 asmspec = TREE_STRING_POINTER (asmspec_tree);
4910 if (VAR_P (decl)
4911 && TREE_STATIC (decl)
4912 && global_bindings_p ())
4913 /* So decl is a global variable. Record the types it uses
4914 so that we can decide later to emit debug info for them. */
4915 record_types_used_by_current_var_decl (decl);
4917 /* If `start_decl' didn't like having an initialization, ignore it now. */
4918 if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
4919 init = NULL_TREE;
4921 /* Don't crash if parm is initialized. */
4922 if (TREE_CODE (decl) == PARM_DECL)
4923 init = NULL_TREE;
4925 if (init)
4926 store_init_value (init_loc, decl, init, origtype);
4928 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4929 || TREE_CODE (decl) == FIELD_DECL))
4930 objc_check_decl (decl);
4932 type = TREE_TYPE (decl);
4934 /* Deduce size of array from initialization, if not already known. */
4935 if (TREE_CODE (type) == ARRAY_TYPE
4936 && TYPE_DOMAIN (type) == NULL_TREE
4937 && TREE_CODE (decl) != TYPE_DECL)
4939 bool do_default
4940 = (TREE_STATIC (decl)
4941 /* Even if pedantic, an external linkage array
4942 may have incomplete type at first. */
4943 ? pedantic && !TREE_PUBLIC (decl)
4944 : !DECL_EXTERNAL (decl));
4945 int failure
4946 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4947 do_default);
4949 /* Get the completed type made by complete_array_type. */
4950 type = TREE_TYPE (decl);
4952 switch (failure)
4954 case 1:
4955 error ("initializer fails to determine size of %q+D", decl);
4956 break;
4958 case 2:
4959 if (do_default)
4960 error ("array size missing in %q+D", decl);
4961 /* If a `static' var's size isn't known,
4962 make it extern as well as static, so it does not get
4963 allocated.
4964 If it is not `static', then do not mark extern;
4965 finish_incomplete_decl will give it a default size
4966 and it will get allocated. */
4967 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4968 DECL_EXTERNAL (decl) = 1;
4969 break;
4971 case 3:
4972 error ("zero or negative size array %q+D", decl);
4973 break;
4975 case 0:
4976 /* For global variables, update the copy of the type that
4977 exists in the binding. */
4978 if (TREE_PUBLIC (decl))
4980 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4981 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4982 b_ext = b_ext->shadowed;
4983 if (b_ext && TREE_CODE (decl) == TREE_CODE (b_ext->decl))
4985 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4986 b_ext->u.type = composite_type (b_ext->u.type, type);
4987 else
4988 b_ext->u.type = type;
4991 break;
4993 default:
4994 gcc_unreachable ();
4997 if (DECL_INITIAL (decl))
4998 TREE_TYPE (DECL_INITIAL (decl)) = type;
5000 relayout_decl (decl);
5003 if (VAR_P (decl))
5005 if (init && TREE_CODE (init) == CONSTRUCTOR)
5006 add_flexible_array_elts_to_size (decl, init);
5008 if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node
5009 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
5010 layout_decl (decl, 0);
5012 if (DECL_SIZE (decl) == NULL_TREE
5013 /* Don't give an error if we already gave one earlier. */
5014 && TREE_TYPE (decl) != error_mark_node
5015 && (TREE_STATIC (decl)
5016 /* A static variable with an incomplete type
5017 is an error if it is initialized.
5018 Also if it is not file scope.
5019 Otherwise, let it through, but if it is not `extern'
5020 then it may cause an error message later. */
5021 ? (DECL_INITIAL (decl) != NULL_TREE
5022 || !DECL_FILE_SCOPE_P (decl))
5023 /* An automatic variable with an incomplete type
5024 is an error. */
5025 : !DECL_EXTERNAL (decl)))
5027 error ("storage size of %q+D isn%'t known", decl);
5028 TREE_TYPE (decl) = error_mark_node;
5031 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
5032 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
5033 && DECL_SIZE (decl) == NULL_TREE
5034 && TREE_STATIC (decl))
5035 incomplete_record_decls.safe_push (decl);
5037 if (is_global_var (decl) && DECL_SIZE (decl) != NULL_TREE)
5039 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5040 constant_expression_warning (DECL_SIZE (decl));
5041 else
5043 error ("storage size of %q+D isn%'t constant", decl);
5044 TREE_TYPE (decl) = error_mark_node;
5048 if (TREE_USED (type))
5050 TREE_USED (decl) = 1;
5051 DECL_READ_P (decl) = 1;
5055 /* If this is a function and an assembler name is specified, reset DECL_RTL
5056 so we can give it its new name. Also, update builtin_decl if it
5057 was a normal built-in. */
5058 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
5060 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
5061 set_builtin_user_assembler_name (decl, asmspec);
5062 set_user_assembler_name (decl, asmspec);
5065 /* If #pragma weak was used, mark the decl weak now. */
5066 maybe_apply_pragma_weak (decl);
5068 /* Output the assembler code and/or RTL code for variables and functions,
5069 unless the type is an undefined structure or union.
5070 If not, it will get done when the type is completed. */
5072 if (VAR_OR_FUNCTION_DECL_P (decl))
5074 /* Determine the ELF visibility. */
5075 if (TREE_PUBLIC (decl))
5076 c_determine_visibility (decl);
5078 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5079 if (c_dialect_objc ())
5080 objc_check_decl (decl);
5082 if (asmspec)
5084 /* If this is not a static variable, issue a warning.
5085 It doesn't make any sense to give an ASMSPEC for an
5086 ordinary, non-register local variable. Historically,
5087 GCC has accepted -- but ignored -- the ASMSPEC in
5088 this case. */
5089 if (!DECL_FILE_SCOPE_P (decl)
5090 && VAR_P (decl)
5091 && !C_DECL_REGISTER (decl)
5092 && !TREE_STATIC (decl))
5093 warning (0, "ignoring asm-specifier for non-static local "
5094 "variable %q+D", decl);
5095 else
5096 set_user_assembler_name (decl, asmspec);
5099 if (DECL_FILE_SCOPE_P (decl))
5101 if (DECL_INITIAL (decl) == NULL_TREE
5102 || DECL_INITIAL (decl) == error_mark_node)
5103 /* Don't output anything
5104 when a tentative file-scope definition is seen.
5105 But at end of compilation, do output code for them. */
5106 DECL_DEFER_OUTPUT (decl) = 1;
5107 if (asmspec && VAR_P (decl) && C_DECL_REGISTER (decl))
5108 DECL_HARD_REGISTER (decl) = 1;
5109 rest_of_decl_compilation (decl, true, 0);
5111 else
5113 /* In conjunction with an ASMSPEC, the `register'
5114 keyword indicates that we should place the variable
5115 in a particular register. */
5116 if (asmspec && C_DECL_REGISTER (decl))
5118 DECL_HARD_REGISTER (decl) = 1;
5119 /* This cannot be done for a structure with volatile
5120 fields, on which DECL_REGISTER will have been
5121 reset. */
5122 if (!DECL_REGISTER (decl))
5123 error ("cannot put object with volatile field into register");
5126 if (TREE_CODE (decl) != FUNCTION_DECL)
5128 /* If we're building a variable sized type, and we might be
5129 reachable other than via the top of the current binding
5130 level, then create a new BIND_EXPR so that we deallocate
5131 the object at the right time. */
5132 /* Note that DECL_SIZE can be null due to errors. */
5133 if (DECL_SIZE (decl)
5134 && !TREE_CONSTANT (DECL_SIZE (decl))
5135 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
5137 tree bind;
5138 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
5139 TREE_SIDE_EFFECTS (bind) = 1;
5140 add_stmt (bind);
5141 BIND_EXPR_BODY (bind) = push_stmt_list ();
5143 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
5144 DECL_EXPR, decl));
5149 if (!DECL_FILE_SCOPE_P (decl))
5151 /* Recompute the RTL of a local array now
5152 if it used to be an incomplete type. */
5153 if (was_incomplete && !is_global_var (decl))
5155 /* If we used it already as memory, it must stay in memory. */
5156 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5157 /* If it's still incomplete now, no init will save it. */
5158 if (DECL_SIZE (decl) == NULL_TREE)
5159 DECL_INITIAL (decl) = NULL_TREE;
5164 if (TREE_CODE (decl) == TYPE_DECL)
5166 if (!DECL_FILE_SCOPE_P (decl)
5167 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
5168 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
5170 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
5173 /* Install a cleanup (aka destructor) if one was given. */
5174 if (VAR_P (decl) && !TREE_STATIC (decl))
5176 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
5177 if (attr)
5179 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
5180 tree cleanup_decl = lookup_name (cleanup_id);
5181 tree cleanup;
5182 vec<tree, va_gc> *v;
5184 /* Build "cleanup(&decl)" for the destructor. */
5185 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, false);
5186 vec_alloc (v, 1);
5187 v->quick_push (cleanup);
5188 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
5189 vNULL, cleanup_decl, v, NULL);
5190 vec_free (v);
5192 /* Don't warn about decl unused; the cleanup uses it. */
5193 TREE_USED (decl) = 1;
5194 TREE_USED (cleanup_decl) = 1;
5195 DECL_READ_P (decl) = 1;
5197 push_cleanup (decl, cleanup, false);
5201 if (warn_cxx_compat
5202 && VAR_P (decl)
5203 && !DECL_EXTERNAL (decl)
5204 && DECL_INITIAL (decl) == NULL_TREE)
5206 type = strip_array_types (type);
5207 if (TREE_READONLY (decl))
5208 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
5209 "uninitialized const %qD is invalid in C++", decl);
5210 else if (RECORD_OR_UNION_TYPE_P (type)
5211 && C_TYPE_FIELDS_READONLY (type))
5212 diagnose_uninitialized_cst_member (decl, type);
5215 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
5218 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
5219 EXPR is NULL or a pointer to an expression that needs to be
5220 evaluated for the side effects of array size expressions in the
5221 parameters. */
5223 tree
5224 grokparm (const struct c_parm *parm, tree *expr)
5226 tree attrs = parm->attrs;
5227 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
5228 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
5230 decl_attributes (&decl, attrs, 0);
5232 return decl;
5235 /* Given a parsed parameter declaration, decode it into a PARM_DECL
5236 and push that on the current scope. EXPR is a pointer to an
5237 expression that needs to be evaluated for the side effects of array
5238 size expressions in the parameters. */
5240 void
5241 push_parm_decl (const struct c_parm *parm, tree *expr)
5243 tree attrs = parm->attrs;
5244 tree decl;
5246 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5247 &attrs, expr, NULL, DEPRECATED_NORMAL);
5248 if (decl && DECL_P (decl))
5249 DECL_SOURCE_LOCATION (decl) = parm->loc;
5250 decl_attributes (&decl, attrs, 0);
5252 decl = pushdecl (decl);
5254 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5257 /* Mark all the parameter declarations to date as forward decls.
5258 Also diagnose use of this extension. */
5260 void
5261 mark_forward_parm_decls (void)
5263 struct c_binding *b;
5265 if (pedantic && !current_scope->warned_forward_parm_decls)
5267 pedwarn (input_location, OPT_Wpedantic,
5268 "ISO C forbids forward parameter declarations");
5269 current_scope->warned_forward_parm_decls = true;
5272 for (b = current_scope->bindings; b; b = b->prev)
5273 if (TREE_CODE (b->decl) == PARM_DECL)
5274 TREE_ASM_WRITTEN (b->decl) = 1;
5277 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5278 literal, which may be an incomplete array type completed by the
5279 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5280 literal. NON_CONST is true if the initializers contain something
5281 that cannot occur in a constant expression. If ALIGNAS_ALIGN is nonzero,
5282 it is the (valid) alignment for this compound literal, as specified
5283 with _Alignas. */
5285 tree
5286 build_compound_literal (location_t loc, tree type, tree init, bool non_const,
5287 unsigned int alignas_align)
5289 /* We do not use start_decl here because we have a type, not a declarator;
5290 and do not use finish_decl because the decl should be stored inside
5291 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5292 tree decl;
5293 tree complit;
5294 tree stmt;
5296 if (type == error_mark_node
5297 || init == error_mark_node)
5298 return error_mark_node;
5300 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5301 DECL_EXTERNAL (decl) = 0;
5302 TREE_PUBLIC (decl) = 0;
5303 TREE_STATIC (decl) = (current_scope == file_scope);
5304 DECL_CONTEXT (decl) = current_function_decl;
5305 TREE_USED (decl) = 1;
5306 DECL_READ_P (decl) = 1;
5307 DECL_ARTIFICIAL (decl) = 1;
5308 DECL_IGNORED_P (decl) = 1;
5309 TREE_TYPE (decl) = type;
5310 c_apply_type_quals_to_decl (TYPE_QUALS (strip_array_types (type)), decl);
5311 if (alignas_align)
5313 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
5314 DECL_USER_ALIGN (decl) = 1;
5316 store_init_value (loc, decl, init, NULL_TREE);
5318 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5320 int failure = complete_array_type (&TREE_TYPE (decl),
5321 DECL_INITIAL (decl), true);
5322 /* If complete_array_type returns 3, it means that the
5323 initial value of the compound literal is empty. Allow it. */
5324 gcc_assert (failure == 0 || failure == 3);
5326 type = TREE_TYPE (decl);
5327 TREE_TYPE (DECL_INITIAL (decl)) = type;
5330 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5332 c_incomplete_type_error (loc, NULL_TREE, type);
5333 return error_mark_node;
5336 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5337 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5338 TREE_SIDE_EFFECTS (complit) = 1;
5340 layout_decl (decl, 0);
5342 if (TREE_STATIC (decl))
5344 /* This decl needs a name for the assembler output. */
5345 set_compound_literal_name (decl);
5346 DECL_DEFER_OUTPUT (decl) = 1;
5347 DECL_COMDAT (decl) = 1;
5348 pushdecl (decl);
5349 rest_of_decl_compilation (decl, 1, 0);
5352 if (non_const)
5354 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5355 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5358 return complit;
5361 /* Check the type of a compound literal. Here we just check that it
5362 is valid for C++. */
5364 void
5365 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5367 if (warn_cxx_compat
5368 && (type_name->specs->typespec_kind == ctsk_tagdef
5369 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5370 warning_at (loc, OPT_Wc___compat,
5371 "defining a type in a compound literal is invalid in C++");
5374 /* Determine whether TYPE is a structure with a flexible array member,
5375 or a union containing such a structure (possibly recursively). */
5377 static bool
5378 flexible_array_type_p (tree type)
5380 tree x;
5381 switch (TREE_CODE (type))
5383 case RECORD_TYPE:
5384 x = TYPE_FIELDS (type);
5385 if (x == NULL_TREE)
5386 return false;
5387 while (DECL_CHAIN (x) != NULL_TREE)
5388 x = DECL_CHAIN (x);
5389 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5390 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5391 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5392 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5393 return true;
5394 return false;
5395 case UNION_TYPE:
5396 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5398 if (flexible_array_type_p (TREE_TYPE (x)))
5399 return true;
5401 return false;
5402 default:
5403 return false;
5407 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5408 replacing with appropriate values if they are invalid. */
5410 static void
5411 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5412 tree orig_name)
5414 tree type_mv;
5415 unsigned int max_width;
5416 unsigned HOST_WIDE_INT w;
5417 const char *name = (orig_name
5418 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5419 : _("<anonymous>"));
5421 /* Detect and ignore out of range field width and process valid
5422 field widths. */
5423 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5425 error_at (loc, "bit-field %qs width not an integer constant", name);
5426 *width = integer_one_node;
5428 else
5430 if (TREE_CODE (*width) != INTEGER_CST)
5432 *width = c_fully_fold (*width, false, NULL);
5433 if (TREE_CODE (*width) == INTEGER_CST)
5434 pedwarn (loc, OPT_Wpedantic,
5435 "bit-field %qs width not an integer constant expression",
5436 name);
5438 if (TREE_CODE (*width) != INTEGER_CST)
5440 error_at (loc, "bit-field %qs width not an integer constant", name);
5441 *width = integer_one_node;
5443 constant_expression_warning (*width);
5444 if (tree_int_cst_sgn (*width) < 0)
5446 error_at (loc, "negative width in bit-field %qs", name);
5447 *width = integer_one_node;
5449 else if (integer_zerop (*width) && orig_name)
5451 error_at (loc, "zero width for bit-field %qs", name);
5452 *width = integer_one_node;
5456 /* Detect invalid bit-field type. */
5457 if (TREE_CODE (*type) != INTEGER_TYPE
5458 && TREE_CODE (*type) != BOOLEAN_TYPE
5459 && TREE_CODE (*type) != ENUMERAL_TYPE)
5461 error_at (loc, "bit-field %qs has invalid type", name);
5462 *type = unsigned_type_node;
5465 if (TYPE_WARN_IF_NOT_ALIGN (*type))
5467 error_at (loc, "cannot declare bit-field %qs with %<warn_if_not_aligned%> type",
5468 name);
5469 *type = unsigned_type_node;
5472 type_mv = TYPE_MAIN_VARIANT (*type);
5473 if (!in_system_header_at (input_location)
5474 && type_mv != integer_type_node
5475 && type_mv != unsigned_type_node
5476 && type_mv != boolean_type_node)
5477 pedwarn_c90 (loc, OPT_Wpedantic,
5478 "type of bit-field %qs is a GCC extension", name);
5480 max_width = TYPE_PRECISION (*type);
5482 if (compare_tree_int (*width, max_width) > 0)
5484 error_at (loc, "width of %qs exceeds its type", name);
5485 w = max_width;
5486 *width = build_int_cst (integer_type_node, w);
5488 else
5489 w = tree_to_uhwi (*width);
5491 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5493 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5494 if (!lt
5495 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5496 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5497 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5503 /* Print warning about variable length array if necessary. */
5505 static void
5506 warn_variable_length_array (tree name, tree size)
5508 if (TREE_CONSTANT (size))
5510 if (name)
5511 pedwarn_c90 (input_location, OPT_Wvla,
5512 "ISO C90 forbids array %qE whose size "
5513 "can%'t be evaluated", name);
5514 else
5515 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5516 "whose size can%'t be evaluated");
5518 else
5520 if (name)
5521 pedwarn_c90 (input_location, OPT_Wvla,
5522 "ISO C90 forbids variable length array %qE", name);
5523 else
5524 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5525 "length array");
5529 /* Print warning about defaulting to int if necessary. */
5531 static void
5532 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5534 diagnostic_info diagnostic;
5535 va_list ap;
5536 rich_location richloc (line_table, location);
5538 va_start (ap, gmsgid);
5539 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5540 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5541 diagnostic.option_index = opt;
5542 diagnostic_report_diagnostic (global_dc, &diagnostic);
5543 va_end (ap);
5546 /* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS,
5547 considering only those c_declspec_words found in LIST, which
5548 must be terminated by cdw_number_of_elements. */
5550 static location_t
5551 smallest_type_quals_location (const location_t *locations,
5552 const c_declspec_word *list)
5554 location_t loc = UNKNOWN_LOCATION;
5555 while (*list != cdw_number_of_elements)
5557 location_t newloc = locations[*list];
5558 if (loc == UNKNOWN_LOCATION
5559 || (newloc != UNKNOWN_LOCATION && newloc < loc))
5560 loc = newloc;
5561 list++;
5564 return loc;
5567 /* Given declspecs and a declarator,
5568 determine the name and type of the object declared
5569 and construct a ..._DECL node for it.
5570 (In one case we can return a ..._TYPE node instead.
5571 For invalid input we sometimes return NULL_TREE.)
5573 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5575 DECL_CONTEXT says which syntactic context this declaration is in:
5576 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5577 FUNCDEF for a function definition. Like NORMAL but a few different
5578 error messages in each case. Return value may be zero meaning
5579 this definition is too screwy to try to parse.
5580 PARM for a parameter declaration (either within a function prototype
5581 or before a function body). Make a PARM_DECL, or return void_type_node.
5582 TYPENAME if for a typename (in a cast or sizeof).
5583 Don't make a DECL node; just return the ..._TYPE node.
5584 FIELD for a struct or union field; make a FIELD_DECL.
5585 INITIALIZED is true if the decl has an initializer.
5586 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5587 representing the width of the bit-field.
5588 DECL_ATTRS points to the list of attributes that should be added to this
5589 decl. Any nested attributes that belong on the decl itself will be
5590 added to this list.
5591 If EXPR is not NULL, any expressions that need to be evaluated as
5592 part of evaluating variably modified types will be stored in *EXPR.
5593 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5594 set to indicate whether operands in *EXPR can be used in constant
5595 expressions.
5596 DEPRECATED_STATE is a deprecated_states value indicating whether
5597 deprecation warnings should be suppressed.
5599 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5600 It may also be so in the PARM case, for a prototype where the
5601 argument type is specified but not the name.
5603 This function is where the complicated C meanings of `static'
5604 and `extern' are interpreted. */
5606 static tree
5607 grokdeclarator (const struct c_declarator *declarator,
5608 struct c_declspecs *declspecs,
5609 enum decl_context decl_context, bool initialized, tree *width,
5610 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5611 enum deprecated_states deprecated_state)
5613 tree type = declspecs->type;
5614 bool threadp = declspecs->thread_p;
5615 enum c_storage_class storage_class = declspecs->storage_class;
5616 int constp;
5617 int restrictp;
5618 int volatilep;
5619 int atomicp;
5620 int type_quals = TYPE_UNQUALIFIED;
5621 tree name = NULL_TREE;
5622 bool funcdef_flag = false;
5623 bool funcdef_syntax = false;
5624 bool size_varies = false;
5625 tree decl_attr = declspecs->decl_attr;
5626 int array_ptr_quals = TYPE_UNQUALIFIED;
5627 tree array_ptr_attrs = NULL_TREE;
5628 bool array_parm_static = false;
5629 bool array_parm_vla_unspec_p = false;
5630 tree returned_attrs = NULL_TREE;
5631 bool bitfield = width != NULL;
5632 tree element_type;
5633 tree orig_qual_type = NULL;
5634 size_t orig_qual_indirect = 0;
5635 struct c_arg_info *arg_info = 0;
5636 addr_space_t as1, as2, address_space;
5637 location_t loc = UNKNOWN_LOCATION;
5638 tree expr_dummy;
5639 bool expr_const_operands_dummy;
5640 enum c_declarator_kind first_non_attr_kind;
5641 unsigned int alignas_align = 0;
5643 if (TREE_CODE (type) == ERROR_MARK)
5644 return error_mark_node;
5645 if (expr == NULL)
5647 expr = &expr_dummy;
5648 expr_dummy = NULL_TREE;
5650 if (expr_const_operands == NULL)
5651 expr_const_operands = &expr_const_operands_dummy;
5653 if (declspecs->expr)
5655 if (*expr)
5656 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (declspecs->expr), *expr,
5657 declspecs->expr);
5658 else
5659 *expr = declspecs->expr;
5661 *expr_const_operands = declspecs->expr_const_operands;
5663 if (decl_context == FUNCDEF)
5664 funcdef_flag = true, decl_context = NORMAL;
5666 /* Look inside a declarator for the name being declared
5667 and get it as an IDENTIFIER_NODE, for an error message. */
5669 const struct c_declarator *decl = declarator;
5671 first_non_attr_kind = cdk_attrs;
5672 while (decl)
5673 switch (decl->kind)
5675 case cdk_array:
5676 loc = decl->id_loc;
5677 /* FALL THRU. */
5679 case cdk_function:
5680 case cdk_pointer:
5681 funcdef_syntax = (decl->kind == cdk_function);
5682 if (first_non_attr_kind == cdk_attrs)
5683 first_non_attr_kind = decl->kind;
5684 decl = decl->declarator;
5685 break;
5687 case cdk_attrs:
5688 decl = decl->declarator;
5689 break;
5691 case cdk_id:
5692 loc = decl->id_loc;
5693 if (decl->u.id)
5694 name = decl->u.id;
5695 if (first_non_attr_kind == cdk_attrs)
5696 first_non_attr_kind = decl->kind;
5697 decl = 0;
5698 break;
5700 default:
5701 gcc_unreachable ();
5703 if (name == NULL_TREE)
5705 gcc_assert (decl_context == PARM
5706 || decl_context == TYPENAME
5707 || (decl_context == FIELD
5708 && declarator->kind == cdk_id));
5709 gcc_assert (!initialized);
5713 /* A function definition's declarator must have the form of
5714 a function declarator. */
5716 if (funcdef_flag && !funcdef_syntax)
5717 return NULL_TREE;
5719 /* If this looks like a function definition, make it one,
5720 even if it occurs where parms are expected.
5721 Then store_parm_decls will reject it and not use it as a parm. */
5722 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5723 decl_context = PARM;
5725 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5726 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5728 if ((decl_context == NORMAL || decl_context == FIELD)
5729 && current_scope == file_scope
5730 && variably_modified_type_p (type, NULL_TREE))
5732 if (name)
5733 error_at (loc, "variably modified %qE at file scope", name);
5734 else
5735 error_at (loc, "variably modified field at file scope");
5736 type = integer_type_node;
5739 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5741 /* Diagnose defaulting to "int". */
5743 if (declspecs->default_int_p && !in_system_header_at (input_location))
5745 /* Issue a warning if this is an ISO C 99 program or if
5746 -Wreturn-type and this is a function, or if -Wimplicit;
5747 prefer the former warning since it is more explicit. */
5748 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5749 && funcdef_flag)
5750 warn_about_return_type = 1;
5751 else
5753 if (name)
5754 warn_defaults_to (loc, OPT_Wimplicit_int,
5755 "type defaults to %<int%> in declaration "
5756 "of %qE", name);
5757 else
5758 warn_defaults_to (loc, OPT_Wimplicit_int,
5759 "type defaults to %<int%> in type name");
5763 /* Adjust the type if a bit-field is being declared,
5764 -funsigned-bitfields applied and the type is not explicitly
5765 "signed". */
5766 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5767 && TREE_CODE (type) == INTEGER_TYPE)
5768 type = unsigned_type_for (type);
5770 /* Figure out the type qualifiers for the declaration. There are
5771 two ways a declaration can become qualified. One is something
5772 like `const int i' where the `const' is explicit. Another is
5773 something like `typedef const int CI; CI i' where the type of the
5774 declaration contains the `const'. A third possibility is that
5775 there is a type qualifier on the element type of a typedefed
5776 array type, in which case we should extract that qualifier so
5777 that c_apply_type_quals_to_decl receives the full list of
5778 qualifiers to work with (C90 is not entirely clear about whether
5779 duplicate qualifiers should be diagnosed in this case, but it
5780 seems most appropriate to do so). */
5781 element_type = strip_array_types (type);
5782 constp = declspecs->const_p + TYPE_READONLY (element_type);
5783 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5784 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5785 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5786 as1 = declspecs->address_space;
5787 as2 = TYPE_ADDR_SPACE (element_type);
5788 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5790 if (constp > 1)
5791 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5792 if (restrictp > 1)
5793 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5794 if (volatilep > 1)
5795 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5796 if (atomicp > 1)
5797 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5799 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5800 error_at (loc, "conflicting named address spaces (%s vs %s)",
5801 c_addr_space_name (as1), c_addr_space_name (as2));
5803 if ((TREE_CODE (type) == ARRAY_TYPE
5804 || first_non_attr_kind == cdk_array)
5805 && TYPE_QUALS (element_type))
5807 orig_qual_type = type;
5808 type = TYPE_MAIN_VARIANT (type);
5810 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5811 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5812 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5813 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5814 | ENCODE_QUAL_ADDR_SPACE (address_space));
5815 if (type_quals != TYPE_QUALS (element_type))
5816 orig_qual_type = NULL_TREE;
5818 /* Applying the _Atomic qualifier to an array type (through the use
5819 of typedefs or typeof) must be detected here. If the qualifier
5820 is introduced later, any appearance of applying it to an array is
5821 actually applying it to an element of that array. */
5822 if (declspecs->atomic_p && TREE_CODE (type) == ARRAY_TYPE)
5823 error_at (loc, "%<_Atomic%>-qualified array type");
5825 /* Warn about storage classes that are invalid for certain
5826 kinds of declarations (parameters, typenames, etc.). */
5828 if (funcdef_flag
5829 && (threadp
5830 || storage_class == csc_auto
5831 || storage_class == csc_register
5832 || storage_class == csc_typedef))
5834 if (storage_class == csc_auto)
5835 pedwarn (loc,
5836 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5837 "function definition declared %<auto%>");
5838 if (storage_class == csc_register)
5839 error_at (loc, "function definition declared %<register%>");
5840 if (storage_class == csc_typedef)
5841 error_at (loc, "function definition declared %<typedef%>");
5842 if (threadp)
5843 error_at (loc, "function definition declared %qs",
5844 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5845 threadp = false;
5846 if (storage_class == csc_auto
5847 || storage_class == csc_register
5848 || storage_class == csc_typedef)
5849 storage_class = csc_none;
5851 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5853 if (decl_context == PARM && storage_class == csc_register)
5855 else
5857 switch (decl_context)
5859 case FIELD:
5860 if (name)
5861 error_at (loc, "storage class specified for structure "
5862 "field %qE", name);
5863 else
5864 error_at (loc, "storage class specified for structure field");
5865 break;
5866 case PARM:
5867 if (name)
5868 error_at (loc, "storage class specified for parameter %qE",
5869 name);
5870 else
5871 error_at (loc, "storage class specified for unnamed parameter");
5872 break;
5873 default:
5874 error_at (loc, "storage class specified for typename");
5875 break;
5877 storage_class = csc_none;
5878 threadp = false;
5881 else if (storage_class == csc_extern
5882 && initialized
5883 && !funcdef_flag)
5885 /* 'extern' with initialization is invalid if not at file scope. */
5886 if (current_scope == file_scope)
5888 /* It is fine to have 'extern const' when compiling at C
5889 and C++ intersection. */
5890 if (!(warn_cxx_compat && constp))
5891 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5892 name);
5894 else
5895 error_at (loc, "%qE has both %<extern%> and initializer", name);
5897 else if (current_scope == file_scope)
5899 if (storage_class == csc_auto)
5900 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5901 name);
5902 if (pedantic && storage_class == csc_register)
5903 pedwarn (input_location, OPT_Wpedantic,
5904 "file-scope declaration of %qE specifies %<register%>", name);
5906 else
5908 if (storage_class == csc_extern && funcdef_flag)
5909 error_at (loc, "nested function %qE declared %<extern%>", name);
5910 else if (threadp && storage_class == csc_none)
5912 error_at (loc, "function-scope %qE implicitly auto and declared "
5913 "%qs", name,
5914 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5915 threadp = false;
5919 /* Now figure out the structure of the declarator proper.
5920 Descend through it, creating more complex types, until we reach
5921 the declared identifier (or NULL_TREE, in an absolute declarator).
5922 At each stage we maintain an unqualified version of the type
5923 together with any qualifiers that should be applied to it with
5924 c_build_qualified_type; this way, array types including
5925 multidimensional array types are first built up in unqualified
5926 form and then the qualified form is created with
5927 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5929 while (declarator && declarator->kind != cdk_id)
5931 if (type == error_mark_node)
5933 declarator = declarator->declarator;
5934 continue;
5937 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5938 a cdk_pointer (for *...),
5939 a cdk_function (for ...(...)),
5940 a cdk_attrs (for nested attributes),
5941 or a cdk_id (for the name being declared
5942 or the place in an absolute declarator
5943 where the name was omitted).
5944 For the last case, we have just exited the loop.
5946 At this point, TYPE is the type of elements of an array,
5947 or for a function to return, or for a pointer to point to.
5948 After this sequence of ifs, TYPE is the type of the
5949 array or function or pointer, and DECLARATOR has had its
5950 outermost layer removed. */
5952 if (array_ptr_quals != TYPE_UNQUALIFIED
5953 || array_ptr_attrs != NULL_TREE
5954 || array_parm_static)
5956 /* Only the innermost declarator (making a parameter be of
5957 array type which is converted to pointer type)
5958 may have static or type qualifiers. */
5959 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5960 array_ptr_quals = TYPE_UNQUALIFIED;
5961 array_ptr_attrs = NULL_TREE;
5962 array_parm_static = false;
5965 switch (declarator->kind)
5967 case cdk_attrs:
5969 /* A declarator with embedded attributes. */
5970 tree attrs = declarator->u.attrs;
5971 const struct c_declarator *inner_decl;
5972 int attr_flags = 0;
5973 declarator = declarator->declarator;
5974 inner_decl = declarator;
5975 while (inner_decl->kind == cdk_attrs)
5976 inner_decl = inner_decl->declarator;
5977 if (inner_decl->kind == cdk_id)
5978 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5979 else if (inner_decl->kind == cdk_function)
5980 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5981 else if (inner_decl->kind == cdk_array)
5982 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5983 returned_attrs = decl_attributes (&type,
5984 chainon (returned_attrs, attrs),
5985 attr_flags);
5986 break;
5988 case cdk_array:
5990 tree itype = NULL_TREE;
5991 tree size = declarator->u.array.dimen;
5992 /* The index is a signed object `sizetype' bits wide. */
5993 tree index_type = c_common_signed_type (sizetype);
5995 array_ptr_quals = declarator->u.array.quals;
5996 array_ptr_attrs = declarator->u.array.attrs;
5997 array_parm_static = declarator->u.array.static_p;
5998 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
6000 declarator = declarator->declarator;
6002 /* Check for some types that there cannot be arrays of. */
6004 if (VOID_TYPE_P (type))
6006 if (name)
6007 error_at (loc, "declaration of %qE as array of voids", name);
6008 else
6009 error_at (loc, "declaration of type name as array of voids");
6010 type = error_mark_node;
6013 if (TREE_CODE (type) == FUNCTION_TYPE)
6015 if (name)
6016 error_at (loc, "declaration of %qE as array of functions",
6017 name);
6018 else
6019 error_at (loc, "declaration of type name as array of "
6020 "functions");
6021 type = error_mark_node;
6024 if (pedantic && !in_system_header_at (input_location)
6025 && flexible_array_type_p (type))
6026 pedwarn (loc, OPT_Wpedantic,
6027 "invalid use of structure with flexible array member");
6029 if (size == error_mark_node)
6030 type = error_mark_node;
6032 if (type == error_mark_node)
6033 continue;
6035 /* If size was specified, set ITYPE to a range-type for
6036 that size. Otherwise, ITYPE remains null. finish_decl
6037 may figure it out from an initial value. */
6039 if (size)
6041 bool size_maybe_const = true;
6042 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
6043 && !TREE_OVERFLOW (size));
6044 bool this_size_varies = false;
6046 /* Strip NON_LVALUE_EXPRs since we aren't using as an
6047 lvalue. */
6048 STRIP_TYPE_NOPS (size);
6050 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
6052 if (name)
6053 error_at (loc, "size of array %qE has non-integer type",
6054 name);
6055 else
6056 error_at (loc,
6057 "size of unnamed array has non-integer type");
6058 size = integer_one_node;
6060 /* This can happen with enum forward declaration. */
6061 else if (!COMPLETE_TYPE_P (TREE_TYPE (size)))
6063 if (name)
6064 error_at (loc, "size of array %qE has incomplete type",
6065 name);
6066 else
6067 error_at (loc, "size of unnamed array has incomplete "
6068 "type");
6069 size = integer_one_node;
6072 size = c_fully_fold (size, false, &size_maybe_const);
6074 if (pedantic && size_maybe_const && integer_zerop (size))
6076 if (name)
6077 pedwarn (loc, OPT_Wpedantic,
6078 "ISO C forbids zero-size array %qE", name);
6079 else
6080 pedwarn (loc, OPT_Wpedantic,
6081 "ISO C forbids zero-size array");
6084 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
6086 constant_expression_warning (size);
6087 if (tree_int_cst_sgn (size) < 0)
6089 if (name)
6090 error_at (loc, "size of array %qE is negative", name);
6091 else
6092 error_at (loc, "size of unnamed array is negative");
6093 size = integer_one_node;
6095 /* Handle a size folded to an integer constant but
6096 not an integer constant expression. */
6097 if (!size_int_const)
6099 /* If this is a file scope declaration of an
6100 ordinary identifier, this is invalid code;
6101 diagnosing it here and not subsequently
6102 treating the type as variable-length avoids
6103 more confusing diagnostics later. */
6104 if ((decl_context == NORMAL || decl_context == FIELD)
6105 && current_scope == file_scope)
6106 pedwarn (input_location, 0,
6107 "variably modified %qE at file scope",
6108 name);
6109 else
6110 this_size_varies = size_varies = true;
6111 warn_variable_length_array (name, size);
6114 else if ((decl_context == NORMAL || decl_context == FIELD)
6115 && current_scope == file_scope)
6117 error_at (loc, "variably modified %qE at file scope", name);
6118 size = integer_one_node;
6120 else
6122 /* Make sure the array size remains visibly
6123 nonconstant even if it is (eg) a const variable
6124 with known value. */
6125 this_size_varies = size_varies = true;
6126 warn_variable_length_array (name, size);
6127 if (sanitize_flags_p (SANITIZE_VLA)
6128 && current_function_decl != NULL_TREE
6129 && decl_context == NORMAL)
6131 /* Evaluate the array size only once. */
6132 size = save_expr (size);
6133 size = c_fully_fold (size, false, NULL);
6134 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
6135 ubsan_instrument_vla (loc, size),
6136 size);
6140 if (integer_zerop (size) && !this_size_varies)
6142 /* A zero-length array cannot be represented with
6143 an unsigned index type, which is what we'll
6144 get with build_index_type. Create an
6145 open-ended range instead. */
6146 itype = build_range_type (sizetype, size, NULL_TREE);
6148 else
6150 /* Arrange for the SAVE_EXPR on the inside of the
6151 MINUS_EXPR, which allows the -1 to get folded
6152 with the +1 that happens when building TYPE_SIZE. */
6153 if (size_varies)
6154 size = save_expr (size);
6155 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
6156 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6157 integer_zero_node, size);
6159 /* Compute the maximum valid index, that is, size
6160 - 1. Do the calculation in index_type, so that
6161 if it is a variable the computations will be
6162 done in the proper mode. */
6163 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
6164 convert (index_type, size),
6165 convert (index_type,
6166 size_one_node));
6168 /* The above overflows when size does not fit
6169 in index_type.
6170 ??? While a size of INT_MAX+1 technically shouldn't
6171 cause an overflow (because we subtract 1), handling
6172 this case seems like an unnecessary complication. */
6173 if (TREE_CODE (size) == INTEGER_CST
6174 && !int_fits_type_p (size, index_type))
6176 if (name)
6177 error_at (loc, "size of array %qE is too large",
6178 name);
6179 else
6180 error_at (loc, "size of unnamed array is too large");
6181 type = error_mark_node;
6182 continue;
6185 itype = build_index_type (itype);
6187 if (this_size_varies)
6189 if (*expr)
6190 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
6191 *expr, size);
6192 else
6193 *expr = size;
6194 *expr_const_operands &= size_maybe_const;
6197 else if (decl_context == FIELD)
6199 bool flexible_array_member = false;
6200 if (array_parm_vla_unspec_p)
6201 /* Field names can in fact have function prototype
6202 scope so [*] is disallowed here through making
6203 the field variably modified, not through being
6204 something other than a declaration with function
6205 prototype scope. */
6206 size_varies = true;
6207 else
6209 const struct c_declarator *t = declarator;
6210 while (t->kind == cdk_attrs)
6211 t = t->declarator;
6212 flexible_array_member = (t->kind == cdk_id);
6214 if (flexible_array_member
6215 && !in_system_header_at (input_location))
6216 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6217 "support flexible array members");
6219 /* ISO C99 Flexible array members are effectively
6220 identical to GCC's zero-length array extension. */
6221 if (flexible_array_member || array_parm_vla_unspec_p)
6222 itype = build_range_type (sizetype, size_zero_node,
6223 NULL_TREE);
6225 else if (decl_context == PARM)
6227 if (array_parm_vla_unspec_p)
6229 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
6230 size_varies = true;
6233 else if (decl_context == TYPENAME)
6235 if (array_parm_vla_unspec_p)
6237 /* C99 6.7.5.2p4 */
6238 warning (0, "%<[*]%> not in a declaration");
6239 /* We use this to avoid messing up with incomplete
6240 array types of the same type, that would
6241 otherwise be modified below. */
6242 itype = build_range_type (sizetype, size_zero_node,
6243 NULL_TREE);
6244 size_varies = true;
6248 /* Complain about arrays of incomplete types. */
6249 if (!COMPLETE_TYPE_P (type))
6251 error_at (loc, "array type has incomplete element type %qT",
6252 type);
6253 /* See if we can be more helpful. */
6254 if (TREE_CODE (type) == ARRAY_TYPE)
6256 if (name)
6257 inform (loc, "declaration of %qE as multidimensional "
6258 "array must have bounds for all dimensions "
6259 "except the first", name);
6260 else
6261 inform (loc, "declaration of multidimensional array "
6262 "must have bounds for all dimensions except "
6263 "the first");
6265 type = error_mark_node;
6267 else
6268 /* When itype is NULL, a shared incomplete array type is
6269 returned for all array of a given type. Elsewhere we
6270 make sure we don't complete that type before copying
6271 it, but here we want to make sure we don't ever
6272 modify the shared type, so we gcc_assert (itype)
6273 below. */
6275 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
6276 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
6277 type = build_qualified_type (type,
6278 ENCODE_QUAL_ADDR_SPACE (as));
6280 type = build_array_type (type, itype);
6283 if (type != error_mark_node)
6285 if (size_varies)
6287 /* It is ok to modify type here even if itype is
6288 NULL: if size_varies, we're in a
6289 multi-dimensional array and the inner type has
6290 variable size, so the enclosing shared array type
6291 must too. */
6292 if (size && TREE_CODE (size) == INTEGER_CST)
6293 type
6294 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6295 C_TYPE_VARIABLE_SIZE (type) = 1;
6298 /* The GCC extension for zero-length arrays differs from
6299 ISO flexible array members in that sizeof yields
6300 zero. */
6301 if (size && integer_zerop (size))
6303 gcc_assert (itype);
6304 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6305 TYPE_SIZE (type) = bitsize_zero_node;
6306 TYPE_SIZE_UNIT (type) = size_zero_node;
6307 SET_TYPE_STRUCTURAL_EQUALITY (type);
6309 if (array_parm_vla_unspec_p)
6311 gcc_assert (itype);
6312 /* The type is complete. C99 6.7.5.2p4 */
6313 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6314 TYPE_SIZE (type) = bitsize_zero_node;
6315 TYPE_SIZE_UNIT (type) = size_zero_node;
6316 SET_TYPE_STRUCTURAL_EQUALITY (type);
6319 if (!valid_array_size_p (loc, type, name))
6320 type = error_mark_node;
6323 if (decl_context != PARM
6324 && (array_ptr_quals != TYPE_UNQUALIFIED
6325 || array_ptr_attrs != NULL_TREE
6326 || array_parm_static))
6328 error_at (loc, "static or type qualifiers in non-parameter "
6329 "array declarator");
6330 array_ptr_quals = TYPE_UNQUALIFIED;
6331 array_ptr_attrs = NULL_TREE;
6332 array_parm_static = false;
6334 orig_qual_indirect++;
6335 break;
6337 case cdk_function:
6339 /* Say it's a definition only for the declarator closest
6340 to the identifier, apart possibly from some
6341 attributes. */
6342 bool really_funcdef = false;
6343 tree arg_types;
6344 orig_qual_type = NULL_TREE;
6345 if (funcdef_flag)
6347 const struct c_declarator *t = declarator->declarator;
6348 while (t->kind == cdk_attrs)
6349 t = t->declarator;
6350 really_funcdef = (t->kind == cdk_id);
6353 /* Declaring a function type. Make sure we have a valid
6354 type for the function to return. */
6355 if (type == error_mark_node)
6356 continue;
6358 size_varies = false;
6360 /* Warn about some types functions can't return. */
6361 if (TREE_CODE (type) == FUNCTION_TYPE)
6363 if (name)
6364 error_at (loc, "%qE declared as function returning a "
6365 "function", name);
6366 else
6367 error_at (loc, "type name declared as function "
6368 "returning a function");
6369 type = integer_type_node;
6371 if (TREE_CODE (type) == ARRAY_TYPE)
6373 if (name)
6374 error_at (loc, "%qE declared as function returning an array",
6375 name);
6376 else
6377 error_at (loc, "type name declared as function returning "
6378 "an array");
6379 type = integer_type_node;
6382 /* Construct the function type and go to the next
6383 inner layer of declarator. */
6384 arg_info = declarator->u.arg_info;
6385 arg_types = grokparms (arg_info, really_funcdef);
6387 /* Type qualifiers before the return type of the function
6388 qualify the return type, not the function type. */
6389 if (type_quals)
6391 const enum c_declspec_word ignored_quals_list[] =
6393 cdw_const, cdw_volatile, cdw_restrict, cdw_address_space,
6394 cdw_atomic, cdw_number_of_elements
6396 location_t specs_loc
6397 = smallest_type_quals_location (declspecs->locations,
6398 ignored_quals_list);
6399 if (specs_loc == UNKNOWN_LOCATION)
6400 specs_loc = declspecs->locations[cdw_typedef];
6401 if (specs_loc == UNKNOWN_LOCATION)
6402 specs_loc = loc;
6404 /* Type qualifiers on a function return type are
6405 normally permitted by the standard but have no
6406 effect, so give a warning at -Wreturn-type.
6407 Qualifiers on a void return type are banned on
6408 function definitions in ISO C; GCC used to used
6409 them for noreturn functions. The resolution of C11
6410 DR#423 means qualifiers (other than _Atomic) are
6411 actually removed from the return type when
6412 determining the function type. */
6413 int quals_used = type_quals;
6414 if (flag_isoc11)
6415 quals_used &= TYPE_QUAL_ATOMIC;
6416 if (quals_used && VOID_TYPE_P (type) && really_funcdef)
6417 pedwarn (specs_loc, 0,
6418 "function definition has qualified void return type");
6419 else
6420 warning_at (specs_loc, OPT_Wignored_qualifiers,
6421 "type qualifiers ignored on function return type");
6423 /* Ensure an error for restrict on invalid types; the
6424 DR#423 resolution is not entirely clear about
6425 this. */
6426 if (flag_isoc11
6427 && (type_quals & TYPE_QUAL_RESTRICT)
6428 && (!POINTER_TYPE_P (type)
6429 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
6430 error_at (loc, "invalid use of %<restrict%>");
6431 if (quals_used)
6432 type = c_build_qualified_type (type, quals_used);
6434 type_quals = TYPE_UNQUALIFIED;
6436 type = build_function_type (type, arg_types);
6437 declarator = declarator->declarator;
6439 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6440 the formal parameter list of this FUNCTION_TYPE to point to
6441 the FUNCTION_TYPE node itself. */
6443 c_arg_tag *tag;
6444 unsigned ix;
6446 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6447 TYPE_CONTEXT (tag->type) = type;
6449 break;
6451 case cdk_pointer:
6453 /* Merge any constancy or volatility into the target type
6454 for the pointer. */
6455 if ((type_quals & TYPE_QUAL_ATOMIC)
6456 && TREE_CODE (type) == FUNCTION_TYPE)
6458 error_at (loc,
6459 "%<_Atomic%>-qualified function type");
6460 type_quals &= ~TYPE_QUAL_ATOMIC;
6462 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6463 && type_quals)
6464 pedwarn (loc, OPT_Wpedantic,
6465 "ISO C forbids qualified function types");
6466 if (type_quals)
6467 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6468 orig_qual_indirect);
6469 orig_qual_type = NULL_TREE;
6470 size_varies = false;
6472 /* When the pointed-to type involves components of variable size,
6473 care must be taken to ensure that the size evaluation code is
6474 emitted early enough to dominate all the possible later uses
6475 and late enough for the variables on which it depends to have
6476 been assigned.
6478 This is expected to happen automatically when the pointed-to
6479 type has a name/declaration of it's own, but special attention
6480 is required if the type is anonymous.
6482 We handle the NORMAL and FIELD contexts here by attaching an
6483 artificial TYPE_DECL to such pointed-to type. This forces the
6484 sizes evaluation at a safe point and ensures it is not deferred
6485 until e.g. within a deeper conditional context.
6487 We expect nothing to be needed here for PARM or TYPENAME.
6488 Pushing a TYPE_DECL at this point for TYPENAME would actually
6489 be incorrect, as we might be in the middle of an expression
6490 with side effects on the pointed-to type size "arguments" prior
6491 to the pointer declaration point and the fake TYPE_DECL in the
6492 enclosing context would force the size evaluation prior to the
6493 side effects. */
6495 if (!TYPE_NAME (type)
6496 && (decl_context == NORMAL || decl_context == FIELD)
6497 && variably_modified_type_p (type, NULL_TREE))
6499 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6500 DECL_ARTIFICIAL (decl) = 1;
6501 pushdecl (decl);
6502 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6503 TYPE_NAME (type) = decl;
6506 type = c_build_pointer_type (type);
6508 /* Process type qualifiers (such as const or volatile)
6509 that were given inside the `*'. */
6510 type_quals = declarator->u.pointer_quals;
6512 declarator = declarator->declarator;
6513 break;
6515 default:
6516 gcc_unreachable ();
6519 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6521 /* Now TYPE has the actual type, apart from any qualifiers in
6522 TYPE_QUALS. */
6524 /* Warn about address space used for things other than static memory or
6525 pointers. */
6526 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6527 if (!ADDR_SPACE_GENERIC_P (address_space))
6529 if (decl_context == NORMAL)
6531 switch (storage_class)
6533 case csc_auto:
6534 error ("%qs combined with %<auto%> qualifier for %qE",
6535 c_addr_space_name (address_space), name);
6536 break;
6537 case csc_register:
6538 error ("%qs combined with %<register%> qualifier for %qE",
6539 c_addr_space_name (address_space), name);
6540 break;
6541 case csc_none:
6542 if (current_function_scope)
6544 error ("%qs specified for auto variable %qE",
6545 c_addr_space_name (address_space), name);
6546 break;
6548 break;
6549 case csc_static:
6550 case csc_extern:
6551 case csc_typedef:
6552 break;
6553 default:
6554 gcc_unreachable ();
6557 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6559 if (name)
6560 error ("%qs specified for parameter %qE",
6561 c_addr_space_name (address_space), name);
6562 else
6563 error ("%qs specified for unnamed parameter",
6564 c_addr_space_name (address_space));
6566 else if (decl_context == FIELD)
6568 if (name)
6569 error ("%qs specified for structure field %qE",
6570 c_addr_space_name (address_space), name);
6571 else
6572 error ("%qs specified for structure field",
6573 c_addr_space_name (address_space));
6577 /* Check the type and width of a bit-field. */
6578 if (bitfield)
6580 check_bitfield_type_and_width (loc, &type, width, name);
6581 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6582 atomic types are permitted for bit-fields; we have no code to
6583 make bit-field accesses atomic, so disallow them. */
6584 if (type_quals & TYPE_QUAL_ATOMIC)
6586 if (name)
6587 error_at (loc, "bit-field %qE has atomic type", name);
6588 else
6589 error_at (loc, "bit-field has atomic type");
6590 type_quals &= ~TYPE_QUAL_ATOMIC;
6594 /* Reject invalid uses of _Alignas. */
6595 if (declspecs->alignas_p)
6597 if (storage_class == csc_typedef)
6598 error_at (loc, "alignment specified for typedef %qE", name);
6599 else if (storage_class == csc_register)
6600 error_at (loc, "alignment specified for %<register%> object %qE",
6601 name);
6602 else if (decl_context == PARM)
6604 if (name)
6605 error_at (loc, "alignment specified for parameter %qE", name);
6606 else
6607 error_at (loc, "alignment specified for unnamed parameter");
6609 else if (bitfield)
6611 if (name)
6612 error_at (loc, "alignment specified for bit-field %qE", name);
6613 else
6614 error_at (loc, "alignment specified for unnamed bit-field");
6616 else if (TREE_CODE (type) == FUNCTION_TYPE)
6617 error_at (loc, "alignment specified for function %qE", name);
6618 else if (declspecs->align_log != -1 && TYPE_P (type))
6620 alignas_align = 1U << declspecs->align_log;
6621 if (alignas_align < min_align_of_type (type))
6623 if (name)
6624 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6625 "alignment of %qE", name);
6626 else
6627 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6628 "alignment of unnamed field");
6629 alignas_align = 0;
6634 /* If this is declaring a typedef name, return a TYPE_DECL. */
6636 if (storage_class == csc_typedef)
6638 tree decl;
6639 if ((type_quals & TYPE_QUAL_ATOMIC)
6640 && TREE_CODE (type) == FUNCTION_TYPE)
6642 error_at (loc,
6643 "%<_Atomic%>-qualified function type");
6644 type_quals &= ~TYPE_QUAL_ATOMIC;
6646 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6647 && type_quals)
6648 pedwarn (loc, OPT_Wpedantic,
6649 "ISO C forbids qualified function types");
6650 if (type_quals)
6651 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6652 orig_qual_indirect);
6653 decl = build_decl (declarator->id_loc,
6654 TYPE_DECL, declarator->u.id, type);
6655 if (declspecs->explicit_signed_p)
6656 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6657 if (declspecs->inline_p)
6658 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6659 if (declspecs->noreturn_p)
6660 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6662 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6664 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6666 if (b != NULL
6667 && b->decl != NULL_TREE
6668 && (B_IN_CURRENT_SCOPE (b)
6669 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6670 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6672 if (warning_at (declarator->id_loc, OPT_Wc___compat,
6673 ("using %qD as both a typedef and a tag is "
6674 "invalid in C++"), decl)
6675 && b->locus != UNKNOWN_LOCATION)
6676 inform (b->locus, "originally defined here");
6680 return decl;
6683 /* If this is a type name (such as, in a cast or sizeof),
6684 compute the type and return it now. */
6686 if (decl_context == TYPENAME)
6688 /* Note that the grammar rejects storage classes in typenames
6689 and fields. */
6690 gcc_assert (storage_class == csc_none && !threadp
6691 && !declspecs->inline_p && !declspecs->noreturn_p);
6692 if ((type_quals & TYPE_QUAL_ATOMIC)
6693 && TREE_CODE (type) == FUNCTION_TYPE)
6695 error_at (loc,
6696 "%<_Atomic%>-qualified function type");
6697 type_quals &= ~TYPE_QUAL_ATOMIC;
6699 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6700 && type_quals)
6701 pedwarn (loc, OPT_Wpedantic,
6702 "ISO C forbids const or volatile function types");
6703 if (type_quals)
6704 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6705 orig_qual_indirect);
6706 return type;
6709 if (pedantic && decl_context == FIELD
6710 && variably_modified_type_p (type, NULL_TREE))
6712 /* C99 6.7.2.1p8 */
6713 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6714 "have a variably modified type");
6717 /* Aside from typedefs and type names (handle above),
6718 `void' at top level (not within pointer)
6719 is allowed only in public variables.
6720 We don't complain about parms either, but that is because
6721 a better error message can be made later. */
6723 if (VOID_TYPE_P (type) && decl_context != PARM
6724 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6725 && (storage_class == csc_extern
6726 || (current_scope == file_scope
6727 && !(storage_class == csc_static
6728 || storage_class == csc_register)))))
6730 error_at (loc, "variable or field %qE declared void", name);
6731 type = integer_type_node;
6734 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6735 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6738 tree decl;
6740 if (decl_context == PARM)
6742 tree promoted_type;
6743 bool array_parameter_p = false;
6745 /* A parameter declared as an array of T is really a pointer to T.
6746 One declared as a function is really a pointer to a function. */
6748 if (TREE_CODE (type) == ARRAY_TYPE)
6750 /* Transfer const-ness of array into that of type pointed to. */
6751 type = TREE_TYPE (type);
6752 if (orig_qual_type != NULL_TREE)
6754 if (orig_qual_indirect == 0)
6755 orig_qual_type = TREE_TYPE (orig_qual_type);
6756 else
6757 orig_qual_indirect--;
6759 if (type_quals)
6760 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6761 orig_qual_indirect);
6762 type = c_build_pointer_type (type);
6763 type_quals = array_ptr_quals;
6764 if (type_quals)
6765 type = c_build_qualified_type (type, type_quals);
6767 /* We don't yet implement attributes in this context. */
6768 if (array_ptr_attrs != NULL_TREE)
6769 warning_at (loc, OPT_Wattributes,
6770 "attributes in parameter array declarator ignored");
6772 size_varies = false;
6773 array_parameter_p = true;
6775 else if (TREE_CODE (type) == FUNCTION_TYPE)
6777 if (type_quals & TYPE_QUAL_ATOMIC)
6779 error_at (loc,
6780 "%<_Atomic%>-qualified function type");
6781 type_quals &= ~TYPE_QUAL_ATOMIC;
6783 else if (type_quals)
6784 pedwarn (loc, OPT_Wpedantic,
6785 "ISO C forbids qualified function types");
6786 if (type_quals)
6787 type = c_build_qualified_type (type, type_quals);
6788 type = c_build_pointer_type (type);
6789 type_quals = TYPE_UNQUALIFIED;
6791 else if (type_quals)
6792 type = c_build_qualified_type (type, type_quals);
6794 decl = build_decl (declarator->id_loc,
6795 PARM_DECL, declarator->u.id, type);
6796 if (size_varies)
6797 C_DECL_VARIABLE_SIZE (decl) = 1;
6798 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6800 /* Compute the type actually passed in the parmlist,
6801 for the case where there is no prototype.
6802 (For example, shorts and chars are passed as ints.)
6803 When there is a prototype, this is overridden later. */
6805 if (type == error_mark_node)
6806 promoted_type = type;
6807 else
6808 promoted_type = c_type_promotes_to (type);
6810 DECL_ARG_TYPE (decl) = promoted_type;
6811 if (declspecs->inline_p)
6812 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6813 if (declspecs->noreturn_p)
6814 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6816 else if (decl_context == FIELD)
6818 /* Note that the grammar rejects storage classes in typenames
6819 and fields. */
6820 gcc_assert (storage_class == csc_none && !threadp
6821 && !declspecs->inline_p && !declspecs->noreturn_p);
6823 /* Structure field. It may not be a function. */
6825 if (TREE_CODE (type) == FUNCTION_TYPE)
6827 error_at (loc, "field %qE declared as a function", name);
6828 type = build_pointer_type (type);
6830 else if (TREE_CODE (type) != ERROR_MARK
6831 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6833 if (name)
6834 error_at (loc, "field %qE has incomplete type", name);
6835 else
6836 error_at (loc, "unnamed field has incomplete type");
6837 type = error_mark_node;
6839 else if (TREE_CODE (type) == ARRAY_TYPE
6840 && TYPE_DOMAIN (type) == NULL_TREE)
6842 /* We have a flexible array member through a typedef.
6843 Set suitable range. Whether this is a correct position
6844 for a flexible array member will be determined elsewhere. */
6845 if (!in_system_header_at (input_location))
6846 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6847 "support flexible array members");
6848 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6849 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6850 NULL_TREE);
6851 if (orig_qual_indirect == 0)
6852 orig_qual_type = NULL_TREE;
6854 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6855 orig_qual_indirect);
6856 decl = build_decl (declarator->id_loc,
6857 FIELD_DECL, declarator->u.id, type);
6858 DECL_NONADDRESSABLE_P (decl) = bitfield;
6859 if (bitfield && !declarator->u.id)
6861 TREE_NO_WARNING (decl) = 1;
6862 DECL_PADDING_P (decl) = 1;
6865 if (size_varies)
6866 C_DECL_VARIABLE_SIZE (decl) = 1;
6868 else if (TREE_CODE (type) == FUNCTION_TYPE)
6870 if (storage_class == csc_register || threadp)
6872 error_at (loc, "invalid storage class for function %qE", name);
6874 else if (current_scope != file_scope)
6876 /* Function declaration not at file scope. Storage
6877 classes other than `extern' are not allowed, C99
6878 6.7.1p5, and `extern' makes no difference. However,
6879 GCC allows 'auto', perhaps with 'inline', to support
6880 nested functions. */
6881 if (storage_class == csc_auto)
6882 pedwarn (loc, OPT_Wpedantic,
6883 "invalid storage class for function %qE", name);
6884 else if (storage_class == csc_static)
6886 error_at (loc, "invalid storage class for function %qE", name);
6887 if (funcdef_flag)
6888 storage_class = declspecs->storage_class = csc_none;
6889 else
6890 return NULL_TREE;
6894 decl = build_decl (declarator->id_loc,
6895 FUNCTION_DECL, declarator->u.id, type);
6896 decl = build_decl_attribute_variant (decl, decl_attr);
6898 if (type_quals & TYPE_QUAL_ATOMIC)
6900 error_at (loc,
6901 "%<_Atomic%>-qualified function type");
6902 type_quals &= ~TYPE_QUAL_ATOMIC;
6904 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6905 pedwarn (loc, OPT_Wpedantic,
6906 "ISO C forbids qualified function types");
6908 /* Every function declaration is an external reference
6909 (DECL_EXTERNAL) except for those which are not at file
6910 scope and are explicitly declared "auto". This is
6911 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6912 GCC to signify a forward declaration of a nested function. */
6913 if (storage_class == csc_auto && current_scope != file_scope)
6914 DECL_EXTERNAL (decl) = 0;
6915 /* In C99, a function which is declared 'inline' with 'extern'
6916 is not an external reference (which is confusing). It
6917 means that the later definition of the function must be output
6918 in this file, C99 6.7.4p6. In GNU C89, a function declared
6919 'extern inline' is an external reference. */
6920 else if (declspecs->inline_p && storage_class != csc_static)
6921 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6922 == flag_gnu89_inline);
6923 else
6924 DECL_EXTERNAL (decl) = !initialized;
6926 /* Record absence of global scope for `static' or `auto'. */
6927 TREE_PUBLIC (decl)
6928 = !(storage_class == csc_static || storage_class == csc_auto);
6930 /* For a function definition, record the argument information
6931 block where store_parm_decls will look for it. */
6932 if (funcdef_flag)
6933 current_function_arg_info = arg_info;
6935 if (declspecs->default_int_p)
6936 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6938 /* Record presence of `inline' and `_Noreturn', if it is
6939 reasonable. */
6940 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6942 if (declspecs->inline_p)
6943 pedwarn (loc, 0, "cannot inline function %<main%>");
6944 if (declspecs->noreturn_p)
6945 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6947 else
6949 if (declspecs->inline_p)
6950 /* Record that the function is declared `inline'. */
6951 DECL_DECLARED_INLINE_P (decl) = 1;
6952 if (declspecs->noreturn_p)
6954 if (flag_isoc99)
6955 pedwarn_c99 (loc, OPT_Wpedantic,
6956 "ISO C99 does not support %<_Noreturn%>");
6957 else
6958 pedwarn_c99 (loc, OPT_Wpedantic,
6959 "ISO C90 does not support %<_Noreturn%>");
6960 TREE_THIS_VOLATILE (decl) = 1;
6964 else
6966 /* It's a variable. */
6967 /* An uninitialized decl with `extern' is a reference. */
6968 int extern_ref = !initialized && storage_class == csc_extern;
6970 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6971 orig_qual_indirect);
6973 /* C99 6.2.2p7: It is invalid (compile-time undefined
6974 behavior) to create an 'extern' declaration for a
6975 variable if there is a global declaration that is
6976 'static' and the global declaration is not visible.
6977 (If the static declaration _is_ currently visible,
6978 the 'extern' declaration is taken to refer to that decl.) */
6979 if (extern_ref && current_scope != file_scope)
6981 tree global_decl = identifier_global_value (declarator->u.id);
6982 tree visible_decl = lookup_name (declarator->u.id);
6984 if (global_decl
6985 && global_decl != visible_decl
6986 && VAR_P (global_decl)
6987 && !TREE_PUBLIC (global_decl))
6988 error_at (loc, "variable previously declared %<static%> "
6989 "redeclared %<extern%>");
6992 decl = build_decl (declarator->id_loc,
6993 VAR_DECL, declarator->u.id, type);
6994 if (size_varies)
6995 C_DECL_VARIABLE_SIZE (decl) = 1;
6997 if (declspecs->inline_p)
6998 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6999 if (declspecs->noreturn_p)
7000 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
7002 /* At file scope, an initialized extern declaration may follow
7003 a static declaration. In that case, DECL_EXTERNAL will be
7004 reset later in start_decl. */
7005 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
7007 /* At file scope, the presence of a `static' or `register' storage
7008 class specifier, or the absence of all storage class specifiers
7009 makes this declaration a definition (perhaps tentative). Also,
7010 the absence of `static' makes it public. */
7011 if (current_scope == file_scope)
7013 TREE_PUBLIC (decl) = storage_class != csc_static;
7014 TREE_STATIC (decl) = !extern_ref;
7016 /* Not at file scope, only `static' makes a static definition. */
7017 else
7019 TREE_STATIC (decl) = (storage_class == csc_static);
7020 TREE_PUBLIC (decl) = extern_ref;
7023 if (threadp)
7024 set_decl_tls_model (decl, decl_default_tls_model (decl));
7027 if ((storage_class == csc_extern
7028 || (storage_class == csc_none
7029 && TREE_CODE (type) == FUNCTION_TYPE
7030 && !funcdef_flag))
7031 && variably_modified_type_p (type, NULL_TREE))
7033 /* C99 6.7.5.2p2 */
7034 if (TREE_CODE (type) == FUNCTION_TYPE)
7035 error_at (loc, "non-nested function with variably modified type");
7036 else
7037 error_at (loc, "object with variably modified type must have "
7038 "no linkage");
7041 /* Record `register' declaration for warnings on &
7042 and in case doing stupid register allocation. */
7044 if (storage_class == csc_register)
7046 C_DECL_REGISTER (decl) = 1;
7047 DECL_REGISTER (decl) = 1;
7050 /* Record constancy and volatility. */
7051 c_apply_type_quals_to_decl (type_quals, decl);
7053 /* Apply _Alignas specifiers. */
7054 if (alignas_align)
7056 SET_DECL_ALIGN (decl, alignas_align * BITS_PER_UNIT);
7057 DECL_USER_ALIGN (decl) = 1;
7060 /* If a type has volatile components, it should be stored in memory.
7061 Otherwise, the fact that those components are volatile
7062 will be ignored, and would even crash the compiler.
7063 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
7064 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
7065 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
7066 || TREE_CODE (decl) == RESULT_DECL))
7068 /* It is not an error for a structure with volatile fields to
7069 be declared register, but reset DECL_REGISTER since it
7070 cannot actually go in a register. */
7071 int was_reg = C_DECL_REGISTER (decl);
7072 C_DECL_REGISTER (decl) = 0;
7073 DECL_REGISTER (decl) = 0;
7074 c_mark_addressable (decl);
7075 C_DECL_REGISTER (decl) = was_reg;
7078 /* This is the earliest point at which we might know the assembler
7079 name of a variable. Thus, if it's known before this, die horribly. */
7080 gcc_assert (!HAS_DECL_ASSEMBLER_NAME_P (decl)
7081 || !DECL_ASSEMBLER_NAME_SET_P (decl));
7083 if (warn_cxx_compat
7084 && VAR_P (decl)
7085 && TREE_PUBLIC (decl)
7086 && TREE_STATIC (decl)
7087 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
7088 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
7089 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
7090 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
7091 ("non-local variable %qD with anonymous type is "
7092 "questionable in C++"),
7093 decl);
7095 return decl;
7099 /* Decode the parameter-list info for a function type or function definition.
7100 The argument is the value returned by `get_parm_info' (or made in c-parse.c
7101 if there is an identifier list instead of a parameter decl list).
7102 These two functions are separate because when a function returns
7103 or receives functions then each is called multiple times but the order
7104 of calls is different. The last call to `grokparms' is always the one
7105 that contains the formal parameter names of a function definition.
7107 Return a list of arg types to use in the FUNCTION_TYPE for this function.
7109 FUNCDEF_FLAG is true for a function definition, false for
7110 a mere declaration. A nonempty identifier-list gets an error message
7111 when FUNCDEF_FLAG is false. */
7113 static tree
7114 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
7116 tree arg_types = arg_info->types;
7118 if (funcdef_flag && arg_info->had_vla_unspec)
7120 /* A function definition isn't function prototype scope C99 6.2.1p4. */
7121 /* C99 6.7.5.2p4 */
7122 error ("%<[*]%> not allowed in other than function prototype scope");
7125 if (arg_types == NULL_TREE && !funcdef_flag
7126 && !in_system_header_at (input_location))
7127 warning (OPT_Wstrict_prototypes,
7128 "function declaration isn%'t a prototype");
7130 if (arg_types == error_mark_node)
7131 /* Don't set TYPE_ARG_TYPES in this case. */
7132 return NULL_TREE;
7134 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
7136 if (!funcdef_flag)
7138 pedwarn (input_location, 0, "parameter names (without types) in "
7139 "function declaration");
7140 arg_info->parms = NULL_TREE;
7142 else
7143 arg_info->parms = arg_info->types;
7145 arg_info->types = NULL_TREE;
7146 return NULL_TREE;
7148 else
7150 tree parm, type, typelt;
7151 unsigned int parmno;
7153 /* If there is a parameter of incomplete type in a definition,
7154 this is an error. In a declaration this is valid, and a
7155 struct or union type may be completed later, before any calls
7156 or definition of the function. In the case where the tag was
7157 first declared within the parameter list, a warning has
7158 already been given. If a parameter has void type, then
7159 however the function cannot be defined or called, so
7160 warn. */
7162 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
7163 parm;
7164 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
7166 type = TREE_VALUE (typelt);
7167 if (type == error_mark_node)
7168 continue;
7170 if (!COMPLETE_TYPE_P (type))
7172 if (funcdef_flag)
7174 if (DECL_NAME (parm))
7175 error_at (input_location,
7176 "parameter %u (%q+D) has incomplete type",
7177 parmno, parm);
7178 else
7179 error_at (DECL_SOURCE_LOCATION (parm),
7180 "parameter %u has incomplete type",
7181 parmno);
7183 TREE_VALUE (typelt) = error_mark_node;
7184 TREE_TYPE (parm) = error_mark_node;
7185 arg_types = NULL_TREE;
7187 else if (VOID_TYPE_P (type))
7189 if (DECL_NAME (parm))
7190 warning_at (input_location, 0,
7191 "parameter %u (%q+D) has void type",
7192 parmno, parm);
7193 else
7194 warning_at (DECL_SOURCE_LOCATION (parm), 0,
7195 "parameter %u has void type",
7196 parmno);
7200 if (DECL_NAME (parm) && TREE_USED (parm))
7201 warn_if_shadowing (parm);
7203 return arg_types;
7207 /* Allocate and initialize a c_arg_info structure from the parser's
7208 obstack. */
7210 struct c_arg_info *
7211 build_arg_info (void)
7213 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
7214 ret->parms = NULL_TREE;
7215 ret->tags = NULL;
7216 ret->types = NULL_TREE;
7217 ret->others = NULL_TREE;
7218 ret->pending_sizes = NULL;
7219 ret->had_vla_unspec = 0;
7220 return ret;
7223 /* Take apart the current scope and return a c_arg_info structure with
7224 info on a parameter list just parsed.
7226 This structure is later fed to 'grokparms' and 'store_parm_decls'.
7228 ELLIPSIS being true means the argument list ended in '...' so don't
7229 append a sentinel (void_list_node) to the end of the type-list.
7231 EXPR is NULL or an expression that needs to be evaluated for the
7232 side effects of array size expressions in the parameters. */
7234 struct c_arg_info *
7235 get_parm_info (bool ellipsis, tree expr)
7237 struct c_binding *b = current_scope->bindings;
7238 struct c_arg_info *arg_info = build_arg_info ();
7240 tree parms = NULL_TREE;
7241 vec<c_arg_tag, va_gc> *tags = NULL;
7242 tree types = NULL_TREE;
7243 tree others = NULL_TREE;
7245 bool gave_void_only_once_err = false;
7247 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
7249 /* The bindings in this scope must not get put into a block.
7250 We will take care of deleting the binding nodes. */
7251 current_scope->bindings = 0;
7253 /* This function is only called if there was *something* on the
7254 parameter list. */
7255 gcc_assert (b);
7257 /* A parameter list consisting solely of 'void' indicates that the
7258 function takes no arguments. But if the 'void' is qualified
7259 (by 'const' or 'volatile'), or has a storage class specifier
7260 ('register'), then the behavior is undefined; issue an error.
7261 Typedefs for 'void' are OK (see DR#157). */
7262 if (b->prev == 0 /* one binding */
7263 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
7264 && !DECL_NAME (b->decl) /* anonymous */
7265 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
7267 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
7268 || C_DECL_REGISTER (b->decl))
7269 error_at (b->locus, "%<void%> as only parameter may not be qualified");
7271 /* There cannot be an ellipsis. */
7272 if (ellipsis)
7273 error_at (b->locus, "%<void%> must be the only parameter");
7275 arg_info->types = void_list_node;
7276 return arg_info;
7279 if (!ellipsis)
7280 types = void_list_node;
7282 /* Break up the bindings list into parms, tags, types, and others;
7283 apply sanity checks; purge the name-to-decl bindings. */
7284 while (b)
7286 tree decl = b->decl;
7287 tree type = TREE_TYPE (decl);
7288 c_arg_tag tag;
7289 const char *keyword;
7291 switch (TREE_CODE (decl))
7293 case PARM_DECL:
7294 if (b->id)
7296 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7297 I_SYMBOL_BINDING (b->id) = b->shadowed;
7300 /* Check for forward decls that never got their actual decl. */
7301 if (TREE_ASM_WRITTEN (decl))
7302 error_at (b->locus,
7303 "parameter %q+D has just a forward declaration", decl);
7304 /* Check for (..., void, ...) and issue an error. */
7305 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
7307 if (!gave_void_only_once_err)
7309 error_at (b->locus, "%<void%> must be the only parameter");
7310 gave_void_only_once_err = true;
7313 else
7315 /* Valid parameter, add it to the list. */
7316 DECL_CHAIN (decl) = parms;
7317 parms = decl;
7319 /* Since there is a prototype, args are passed in their
7320 declared types. The back end may override this later. */
7321 DECL_ARG_TYPE (decl) = type;
7322 types = tree_cons (0, type, types);
7324 break;
7326 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7327 case UNION_TYPE: keyword = "union"; goto tag;
7328 case RECORD_TYPE: keyword = "struct"; goto tag;
7329 tag:
7330 /* Types may not have tag-names, in which case the type
7331 appears in the bindings list with b->id NULL. */
7332 if (b->id)
7334 gcc_assert (I_TAG_BINDING (b->id) == b);
7335 I_TAG_BINDING (b->id) = b->shadowed;
7338 /* Warn about any struct, union or enum tags defined in a
7339 parameter list. The scope of such types is limited to
7340 the parameter list, which is rarely if ever desirable
7341 (it's impossible to call such a function with type-
7342 correct arguments). An anonymous union parm type is
7343 meaningful as a GNU extension, so don't warn for that. */
7344 if (TREE_CODE (decl) != UNION_TYPE || b->id != NULL_TREE)
7346 if (b->id)
7347 /* The %s will be one of 'struct', 'union', or 'enum'. */
7348 warning_at (b->locus, 0,
7349 "%<%s %E%> declared inside parameter list"
7350 " will not be visible outside of this definition or"
7351 " declaration", keyword, b->id);
7352 else
7353 /* The %s will be one of 'struct', 'union', or 'enum'. */
7354 warning_at (b->locus, 0,
7355 "anonymous %s declared inside parameter list"
7356 " will not be visible outside of this definition or"
7357 " declaration", keyword);
7360 tag.id = b->id;
7361 tag.type = decl;
7362 vec_safe_push (tags, tag);
7363 break;
7365 case FUNCTION_DECL:
7366 /* FUNCTION_DECLs appear when there is an implicit function
7367 declaration in the parameter list. */
7368 gcc_assert (b->nested || seen_error ());
7369 goto set_shadowed;
7371 case CONST_DECL:
7372 case TYPE_DECL:
7373 /* CONST_DECLs appear here when we have an embedded enum,
7374 and TYPE_DECLs appear here when we have an embedded struct
7375 or union. No warnings for this - we already warned about the
7376 type itself. */
7378 /* When we reinsert this decl in the function body, we need
7379 to reconstruct whether it was marked as nested. */
7380 gcc_assert (!b->nested);
7381 DECL_CHAIN (decl) = others;
7382 others = decl;
7383 /* fall through */
7385 case ERROR_MARK:
7386 set_shadowed:
7387 /* error_mark_node appears here when we have an undeclared
7388 variable. Just throw it away. */
7389 if (b->id)
7391 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7392 I_SYMBOL_BINDING (b->id) = b->shadowed;
7394 break;
7396 /* Other things that might be encountered. */
7397 case LABEL_DECL:
7398 case VAR_DECL:
7399 default:
7400 gcc_unreachable ();
7403 b = free_binding_and_advance (b);
7406 arg_info->parms = parms;
7407 arg_info->tags = tags;
7408 arg_info->types = types;
7409 arg_info->others = others;
7410 arg_info->pending_sizes = expr;
7411 return arg_info;
7414 /* Get the struct, enum or union (CODE says which) with tag NAME.
7415 Define the tag as a forward-reference with location LOC if it is
7416 not defined. Return a c_typespec structure for the type
7417 specifier. */
7419 struct c_typespec
7420 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7422 struct c_typespec ret;
7423 tree ref;
7424 location_t refloc;
7426 ret.expr = NULL_TREE;
7427 ret.expr_const_operands = true;
7429 /* If a cross reference is requested, look up the type
7430 already defined for this tag and return it. */
7432 ref = lookup_tag (code, name, false, &refloc);
7433 /* If this is the right type of tag, return what we found.
7434 (This reference will be shadowed by shadow_tag later if appropriate.)
7435 If this is the wrong type of tag, do not return it. If it was the
7436 wrong type in the same scope, we will have had an error
7437 message already; if in a different scope and declaring
7438 a name, pending_xref_error will give an error message; but if in a
7439 different scope and not declaring a name, this tag should
7440 shadow the previous declaration of a different type of tag, and
7441 this would not work properly if we return the reference found.
7442 (For example, with "struct foo" in an outer scope, "union foo;"
7443 must shadow that tag with a new one of union type.) */
7444 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7445 if (ref && TREE_CODE (ref) == code)
7447 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7448 && loc != UNKNOWN_LOCATION
7449 && warn_cxx_compat)
7451 switch (code)
7453 case ENUMERAL_TYPE:
7454 warning_at (loc, OPT_Wc___compat,
7455 ("enum type defined in struct or union "
7456 "is not visible in C++"));
7457 inform (refloc, "enum type defined here");
7458 break;
7459 case RECORD_TYPE:
7460 warning_at (loc, OPT_Wc___compat,
7461 ("struct defined in struct or union "
7462 "is not visible in C++"));
7463 inform (refloc, "struct defined here");
7464 break;
7465 case UNION_TYPE:
7466 warning_at (loc, OPT_Wc___compat,
7467 ("union defined in struct or union "
7468 "is not visible in C++"));
7469 inform (refloc, "union defined here");
7470 break;
7471 default:
7472 gcc_unreachable();
7476 ret.spec = ref;
7477 return ret;
7480 /* If no such tag is yet defined, create a forward-reference node
7481 and record it as the "definition".
7482 When a real declaration of this type is found,
7483 the forward-reference will be altered into a real type. */
7485 ref = make_node (code);
7486 if (code == ENUMERAL_TYPE)
7488 /* Give the type a default layout like unsigned int
7489 to avoid crashing if it does not get defined. */
7490 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7491 SET_TYPE_ALIGN (ref, TYPE_ALIGN (unsigned_type_node));
7492 TYPE_USER_ALIGN (ref) = 0;
7493 TYPE_UNSIGNED (ref) = 1;
7494 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7495 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7496 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7499 pushtag (loc, name, ref);
7501 ret.spec = ref;
7502 return ret;
7505 /* Get the struct, enum or union (CODE says which) with tag NAME.
7506 Define the tag as a forward-reference if it is not defined.
7507 Return a tree for the type. */
7509 tree
7510 xref_tag (enum tree_code code, tree name)
7512 return parser_xref_tag (input_location, code, name).spec;
7515 /* Make sure that the tag NAME is defined *in the current scope*
7516 at least as a forward reference.
7517 LOC is the location of the struct's definition.
7518 CODE says which kind of tag NAME ought to be.
7520 This stores the current value of the file static STRUCT_PARSE_INFO
7521 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7522 new c_struct_parse_info structure. The old value of
7523 STRUCT_PARSE_INFO is restored in finish_struct. */
7525 tree
7526 start_struct (location_t loc, enum tree_code code, tree name,
7527 struct c_struct_parse_info **enclosing_struct_parse_info)
7529 /* If there is already a tag defined at this scope
7530 (as a forward reference), just return it. */
7532 tree ref = NULL_TREE;
7533 location_t refloc = UNKNOWN_LOCATION;
7535 if (name != NULL_TREE)
7536 ref = lookup_tag (code, name, true, &refloc);
7537 if (ref && TREE_CODE (ref) == code)
7539 if (TYPE_STUB_DECL (ref))
7540 refloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (ref));
7542 if (TYPE_SIZE (ref))
7544 if (code == UNION_TYPE)
7545 error_at (loc, "redefinition of %<union %E%>", name);
7546 else
7547 error_at (loc, "redefinition of %<struct %E%>", name);
7548 if (refloc != UNKNOWN_LOCATION)
7549 inform (refloc, "originally defined here");
7550 /* Don't create structures using a name already in use. */
7551 ref = NULL_TREE;
7553 else if (C_TYPE_BEING_DEFINED (ref))
7555 if (code == UNION_TYPE)
7556 error_at (loc, "nested redefinition of %<union %E%>", name);
7557 else
7558 error_at (loc, "nested redefinition of %<struct %E%>", name);
7559 /* Don't bother to report "originally defined here" for a
7560 nested redefinition; the original definition should be
7561 obvious. */
7562 /* Don't create structures that contain themselves. */
7563 ref = NULL_TREE;
7567 /* Otherwise create a forward-reference just so the tag is in scope. */
7569 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7571 ref = make_node (code);
7572 pushtag (loc, name, ref);
7575 C_TYPE_BEING_DEFINED (ref) = 1;
7576 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7577 TYPE_PACKED (v) = flag_pack_struct;
7579 *enclosing_struct_parse_info = struct_parse_info;
7580 struct_parse_info = new c_struct_parse_info ();
7582 /* FIXME: This will issue a warning for a use of a type defined
7583 within a statement expr used within sizeof, et. al. This is not
7584 terribly serious as C++ doesn't permit statement exprs within
7585 sizeof anyhow. */
7586 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7587 warning_at (loc, OPT_Wc___compat,
7588 "defining type in %qs expression is invalid in C++",
7589 (in_sizeof
7590 ? "sizeof"
7591 : (in_typeof ? "typeof" : "alignof")));
7593 return ref;
7596 /* Process the specs, declarator and width (NULL if omitted)
7597 of a structure component, returning a FIELD_DECL node.
7598 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7599 DECL_ATTRS is as for grokdeclarator.
7601 LOC is the location of the structure component.
7603 This is done during the parsing of the struct declaration.
7604 The FIELD_DECL nodes are chained together and the lot of them
7605 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7607 tree
7608 grokfield (location_t loc,
7609 struct c_declarator *declarator, struct c_declspecs *declspecs,
7610 tree width, tree *decl_attrs)
7612 tree value;
7614 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7615 && width == NULL_TREE)
7617 /* This is an unnamed decl.
7619 If we have something of the form "union { list } ;" then this
7620 is the anonymous union extension. Similarly for struct.
7622 If this is something of the form "struct foo;", then
7623 If MS or Plan 9 extensions are enabled, this is handled as
7624 an anonymous struct.
7625 Otherwise this is a forward declaration of a structure tag.
7627 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7628 If foo names a structure or union without a tag, then this
7629 is an anonymous struct (this is permitted by C11).
7630 If MS or Plan 9 extensions are enabled and foo names a
7631 structure, then again this is an anonymous struct.
7632 Otherwise this is an error.
7634 Oh what a horrid tangled web we weave. I wonder if MS consciously
7635 took this from Plan 9 or if it was an accident of implementation
7636 that took root before someone noticed the bug... */
7638 tree type = declspecs->type;
7639 bool ok = false;
7641 if (RECORD_OR_UNION_TYPE_P (type)
7642 && (flag_ms_extensions
7643 || flag_plan9_extensions
7644 || !declspecs->typedef_p))
7646 if (flag_ms_extensions || flag_plan9_extensions)
7647 ok = true;
7648 else if (TYPE_NAME (type) == NULL)
7649 ok = true;
7650 else
7651 ok = false;
7653 if (!ok)
7655 pedwarn (loc, 0, "declaration does not declare anything");
7656 return NULL_TREE;
7658 if (flag_isoc99)
7659 pedwarn_c99 (loc, OPT_Wpedantic,
7660 "ISO C99 doesn%'t support unnamed structs/unions");
7661 else
7662 pedwarn_c99 (loc, OPT_Wpedantic,
7663 "ISO C90 doesn%'t support unnamed structs/unions");
7666 value = grokdeclarator (declarator, declspecs, FIELD, false,
7667 width ? &width : NULL, decl_attrs, NULL, NULL,
7668 DEPRECATED_NORMAL);
7670 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7671 DECL_INITIAL (value) = width;
7672 if (width)
7673 SET_DECL_C_BIT_FIELD (value);
7675 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7677 /* If we currently have a binding for this field, set the
7678 in_struct field in the binding, so that we warn about lookups
7679 which find it. */
7680 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7681 if (b != NULL)
7683 /* If the in_struct field is not yet set, push it on a list
7684 to be cleared when this struct is finished. */
7685 if (!b->in_struct)
7687 struct_parse_info->fields.safe_push (b);
7688 b->in_struct = 1;
7693 return value;
7696 /* Subroutine of detect_field_duplicates: return whether X and Y,
7697 which are both fields in the same struct, have duplicate field
7698 names. */
7700 static bool
7701 is_duplicate_field (tree x, tree y)
7703 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7704 return true;
7706 /* When using -fplan9-extensions, an anonymous field whose name is a
7707 typedef can duplicate a field name. */
7708 if (flag_plan9_extensions
7709 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7711 tree xt, xn, yt, yn;
7713 xt = TREE_TYPE (x);
7714 if (DECL_NAME (x) != NULL_TREE)
7715 xn = DECL_NAME (x);
7716 else if (RECORD_OR_UNION_TYPE_P (xt)
7717 && TYPE_NAME (xt) != NULL_TREE
7718 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7719 xn = DECL_NAME (TYPE_NAME (xt));
7720 else
7721 xn = NULL_TREE;
7723 yt = TREE_TYPE (y);
7724 if (DECL_NAME (y) != NULL_TREE)
7725 yn = DECL_NAME (y);
7726 else if (RECORD_OR_UNION_TYPE_P (yt)
7727 && TYPE_NAME (yt) != NULL_TREE
7728 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7729 yn = DECL_NAME (TYPE_NAME (yt));
7730 else
7731 yn = NULL_TREE;
7733 if (xn != NULL_TREE && xn == yn)
7734 return true;
7737 return false;
7740 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7741 to HTAB, giving errors for any duplicates. */
7743 static void
7744 detect_field_duplicates_hash (tree fieldlist,
7745 hash_table<nofree_ptr_hash <tree_node> > *htab)
7747 tree x, y;
7748 tree_node **slot;
7750 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7751 if ((y = DECL_NAME (x)) != NULL_TREE)
7753 slot = htab->find_slot (y, INSERT);
7754 if (*slot)
7756 error ("duplicate member %q+D", x);
7757 DECL_NAME (x) = NULL_TREE;
7759 *slot = y;
7761 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7763 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7765 /* When using -fplan9-extensions, an anonymous field whose
7766 name is a typedef can duplicate a field name. */
7767 if (flag_plan9_extensions
7768 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7769 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7771 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7772 slot = htab->find_slot (xn, INSERT);
7773 if (*slot)
7774 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7775 *slot = xn;
7780 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7781 the list such that this does not present a problem later. */
7783 static void
7784 detect_field_duplicates (tree fieldlist)
7786 tree x, y;
7787 int timeout = 10;
7789 /* If the struct is the list of instance variables of an Objective-C
7790 class, then we need to check all the instance variables of
7791 superclasses when checking for duplicates (since you can't have
7792 an instance variable in a subclass with the same name as an
7793 instance variable in a superclass). We pass on this job to the
7794 Objective-C compiler. objc_detect_field_duplicates() will return
7795 false if we are not checking the list of instance variables and
7796 the C frontend should proceed with the standard field duplicate
7797 checks. If we are checking the list of instance variables, the
7798 ObjC frontend will do the check, emit the errors if needed, and
7799 then return true. */
7800 if (c_dialect_objc ())
7801 if (objc_detect_field_duplicates (false))
7802 return;
7804 /* First, see if there are more than "a few" fields.
7805 This is trivially true if there are zero or one fields. */
7806 if (!fieldlist || !DECL_CHAIN (fieldlist))
7807 return;
7808 x = fieldlist;
7809 do {
7810 timeout--;
7811 if (DECL_NAME (x) == NULL_TREE
7812 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7813 timeout = 0;
7814 x = DECL_CHAIN (x);
7815 } while (timeout > 0 && x);
7817 /* If there were "few" fields and no anonymous structures or unions,
7818 avoid the overhead of allocating a hash table. Instead just do
7819 the nested traversal thing. */
7820 if (timeout > 0)
7822 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7823 /* When using -fplan9-extensions, we can have duplicates
7824 between typedef names and fields. */
7825 if (DECL_NAME (x)
7826 || (flag_plan9_extensions
7827 && DECL_NAME (x) == NULL_TREE
7828 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7829 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7830 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7832 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7833 if (is_duplicate_field (y, x))
7835 error ("duplicate member %q+D", x);
7836 DECL_NAME (x) = NULL_TREE;
7840 else
7842 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7843 detect_field_duplicates_hash (fieldlist, &htab);
7847 /* Finish up struct info used by -Wc++-compat. */
7849 static void
7850 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7851 location_t record_loc)
7853 unsigned int ix;
7854 tree x;
7855 struct c_binding *b;
7857 if (fieldlist == NULL_TREE)
7859 if (code == RECORD_TYPE)
7860 warning_at (record_loc, OPT_Wc___compat,
7861 "empty struct has size 0 in C, size 1 in C++");
7862 else
7863 warning_at (record_loc, OPT_Wc___compat,
7864 "empty union has size 0 in C, size 1 in C++");
7867 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7868 the current struct. We do this now at the end of the struct
7869 because the flag is used to issue visibility warnings, and we
7870 only want to issue those warnings if the type is referenced
7871 outside of the struct declaration. */
7872 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7873 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7875 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7876 typedefs used when declaring fields in this struct. If the name
7877 of any of the fields is also a typedef name then the struct would
7878 not parse in C++, because the C++ lookup rules say that the
7879 typedef name would be looked up in the context of the struct, and
7880 would thus be the field rather than the typedef. */
7881 if (!struct_parse_info->typedefs_seen.is_empty ()
7882 && fieldlist != NULL_TREE)
7884 /* Use a hash_set<tree> using the name of the typedef. We can use
7885 a hash_set<tree> because identifiers are interned. */
7886 hash_set<tree> tset;
7888 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7889 tset.add (DECL_NAME (x));
7891 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7893 if (DECL_NAME (x) != NULL_TREE
7894 && tset.contains (DECL_NAME (x)))
7896 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7897 ("using %qD as both field and typedef name is "
7898 "invalid in C++"),
7900 /* FIXME: It would be nice to report the location where
7901 the typedef name is used. */
7906 /* For each field which has a binding and which was not defined in
7907 an enclosing struct, clear the in_struct field. */
7908 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7909 b->in_struct = 0;
7912 /* Function to help qsort sort FIELD_DECLs by name order. */
7914 static int
7915 field_decl_cmp (const void *x_p, const void *y_p)
7917 const tree *const x = (const tree *) x_p;
7918 const tree *const y = (const tree *) y_p;
7920 if (DECL_NAME (*x) == DECL_NAME (*y))
7921 /* A nontype is "greater" than a type. */
7922 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
7923 if (DECL_NAME (*x) == NULL_TREE)
7924 return -1;
7925 if (DECL_NAME (*y) == NULL_TREE)
7926 return 1;
7927 if (DECL_NAME (*x) < DECL_NAME (*y))
7928 return -1;
7929 return 1;
7932 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7933 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7934 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7935 ATTRIBUTES are attributes to be applied to the structure.
7937 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7938 the struct was started. */
7940 tree
7941 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7942 struct c_struct_parse_info *enclosing_struct_parse_info)
7944 tree x;
7945 bool toplevel = file_scope == current_scope;
7947 /* If this type was previously laid out as a forward reference,
7948 make sure we lay it out again. */
7950 TYPE_SIZE (t) = NULL_TREE;
7952 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7954 if (pedantic)
7956 for (x = fieldlist; x; x = DECL_CHAIN (x))
7958 if (DECL_NAME (x) != NULL_TREE)
7959 break;
7960 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7961 break;
7964 if (x == NULL_TREE)
7966 if (TREE_CODE (t) == UNION_TYPE)
7968 if (fieldlist)
7969 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7970 else
7971 pedwarn (loc, OPT_Wpedantic, "union has no members");
7973 else
7975 if (fieldlist)
7976 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7977 else
7978 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7983 /* Install struct as DECL_CONTEXT of each field decl.
7984 Also process specified field sizes, found in the DECL_INITIAL,
7985 storing 0 there after the type has been changed to precision equal
7986 to its width, rather than the precision of the specified standard
7987 type. (Correct layout requires the original type to have been preserved
7988 until now.) */
7990 bool saw_named_field = false;
7991 for (x = fieldlist; x; x = DECL_CHAIN (x))
7993 if (TREE_TYPE (x) == error_mark_node)
7994 continue;
7996 DECL_CONTEXT (x) = t;
7998 /* If any field is const, the structure type is pseudo-const. */
7999 if (TREE_READONLY (x))
8000 C_TYPE_FIELDS_READONLY (t) = 1;
8001 else
8003 /* A field that is pseudo-const makes the structure likewise. */
8004 tree t1 = strip_array_types (TREE_TYPE (x));
8005 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
8006 C_TYPE_FIELDS_READONLY (t) = 1;
8009 /* Any field that is volatile means variables of this type must be
8010 treated in some ways as volatile. */
8011 if (TREE_THIS_VOLATILE (x))
8012 C_TYPE_FIELDS_VOLATILE (t) = 1;
8014 /* Any field of nominal variable size implies structure is too. */
8015 if (C_DECL_VARIABLE_SIZE (x))
8016 C_TYPE_VARIABLE_SIZE (t) = 1;
8018 if (DECL_C_BIT_FIELD (x))
8020 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
8021 DECL_SIZE (x) = bitsize_int (width);
8022 DECL_BIT_FIELD (x) = 1;
8025 if (TYPE_PACKED (t)
8026 && (DECL_BIT_FIELD (x)
8027 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
8028 DECL_PACKED (x) = 1;
8030 /* Detect flexible array member in an invalid context. */
8031 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
8032 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
8033 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
8034 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
8036 if (TREE_CODE (t) == UNION_TYPE)
8038 error_at (DECL_SOURCE_LOCATION (x),
8039 "flexible array member in union");
8040 TREE_TYPE (x) = error_mark_node;
8042 else if (DECL_CHAIN (x) != NULL_TREE)
8044 error_at (DECL_SOURCE_LOCATION (x),
8045 "flexible array member not at end of struct");
8046 TREE_TYPE (x) = error_mark_node;
8048 else if (!saw_named_field)
8050 error_at (DECL_SOURCE_LOCATION (x),
8051 "flexible array member in a struct with no named "
8052 "members");
8053 TREE_TYPE (x) = error_mark_node;
8057 if (pedantic && TREE_CODE (t) == RECORD_TYPE
8058 && flexible_array_type_p (TREE_TYPE (x)))
8059 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
8060 "invalid use of structure with flexible array member");
8062 if (DECL_NAME (x)
8063 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
8064 saw_named_field = true;
8067 detect_field_duplicates (fieldlist);
8069 /* Now we have the nearly final fieldlist. Record it,
8070 then lay out the structure or union (including the fields). */
8072 TYPE_FIELDS (t) = fieldlist;
8074 maybe_apply_pragma_scalar_storage_order (t);
8076 layout_type (t);
8078 if (TYPE_SIZE_UNIT (t)
8079 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
8080 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
8081 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
8082 error ("type %qT is too large", t);
8084 /* Give bit-fields their proper types and rewrite the type of array fields
8085 with scalar component if the enclosing type has reverse storage order. */
8086 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
8088 if (TREE_CODE (field) == FIELD_DECL
8089 && DECL_INITIAL (field)
8090 && TREE_TYPE (field) != error_mark_node)
8092 unsigned HOST_WIDE_INT width
8093 = tree_to_uhwi (DECL_INITIAL (field));
8094 tree type = TREE_TYPE (field);
8095 if (width != TYPE_PRECISION (type))
8097 TREE_TYPE (field)
8098 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
8099 SET_DECL_MODE (field, TYPE_MODE (TREE_TYPE (field)));
8101 DECL_INITIAL (field) = NULL_TREE;
8103 else if (TYPE_REVERSE_STORAGE_ORDER (t)
8104 && TREE_CODE (field) == FIELD_DECL
8105 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
8107 tree ftype = TREE_TYPE (field);
8108 tree ctype = strip_array_types (ftype);
8109 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
8111 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
8112 tree *typep = &fmain_type;
8113 do {
8114 *typep = build_distinct_type_copy (*typep);
8115 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
8116 typep = &TREE_TYPE (*typep);
8117 } while (TREE_CODE (*typep) == ARRAY_TYPE);
8118 TREE_TYPE (field)
8119 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
8124 /* Now we have the truly final field list.
8125 Store it in this type and in the variants. */
8127 TYPE_FIELDS (t) = fieldlist;
8129 /* If there are lots of fields, sort so we can look through them fast.
8130 We arbitrarily consider 16 or more elts to be "a lot". */
8133 int len = 0;
8135 for (x = fieldlist; x; x = DECL_CHAIN (x))
8137 if (len > 15 || DECL_NAME (x) == NULL)
8138 break;
8139 len += 1;
8142 if (len > 15)
8144 tree *field_array;
8145 struct lang_type *space;
8146 struct sorted_fields_type *space2;
8148 len += list_length (x);
8150 /* Use the same allocation policy here that make_node uses, to
8151 ensure that this lives as long as the rest of the struct decl.
8152 All decls in an inline function need to be saved. */
8154 space = ggc_cleared_alloc<struct lang_type> ();
8155 space2 = (sorted_fields_type *) ggc_internal_alloc
8156 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
8158 len = 0;
8159 space->s = space2;
8160 field_array = &space2->elts[0];
8161 for (x = fieldlist; x; x = DECL_CHAIN (x))
8163 field_array[len++] = x;
8165 /* If there is anonymous struct or union, break out of the loop. */
8166 if (DECL_NAME (x) == NULL)
8167 break;
8169 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
8170 if (x == NULL)
8172 TYPE_LANG_SPECIFIC (t) = space;
8173 TYPE_LANG_SPECIFIC (t)->s->len = len;
8174 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
8175 qsort (field_array, len, sizeof (tree), field_decl_cmp);
8180 /* Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
8181 in dwarf2out via rest_of_decl_compilation below and means
8182 something totally different. Since we will be clearing
8183 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
8184 clear it ahead of time and avoid problems in dwarf2out. Ideally,
8185 C_TYPE_INCOMPLETE_VARS should use some language specific
8186 node. */
8187 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
8188 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
8190 TYPE_FIELDS (x) = TYPE_FIELDS (t);
8191 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
8192 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
8193 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
8194 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
8195 C_TYPE_INCOMPLETE_VARS (x) = NULL_TREE;
8198 /* If this was supposed to be a transparent union, but we can't
8199 make it one, warn and turn off the flag. */
8200 if (TREE_CODE (t) == UNION_TYPE
8201 && TYPE_TRANSPARENT_AGGR (t)
8202 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
8204 TYPE_TRANSPARENT_AGGR (t) = 0;
8205 warning_at (loc, 0, "union cannot be made transparent");
8208 /* Update type location to the one of the definition, instead of e.g.
8209 a forward declaration. */
8210 if (TYPE_STUB_DECL (t))
8211 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
8213 /* Finish debugging output for this type. */
8214 rest_of_type_compilation (t, toplevel);
8216 /* If this structure or union completes the type of any previous
8217 variable declaration, lay it out and output its rtl. */
8218 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
8220 tree decl = TREE_VALUE (x);
8221 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
8222 layout_array_type (TREE_TYPE (decl));
8223 if (TREE_CODE (decl) != TYPE_DECL)
8225 layout_decl (decl, 0);
8226 if (c_dialect_objc ())
8227 objc_check_decl (decl);
8228 rest_of_decl_compilation (decl, toplevel, 0);
8232 /* If we're inside a function proper, i.e. not file-scope and not still
8233 parsing parameters, then arrange for the size of a variable sized type
8234 to be bound now. */
8235 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
8236 add_stmt (build_stmt (loc,
8237 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
8239 if (warn_cxx_compat)
8240 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
8242 delete struct_parse_info;
8244 struct_parse_info = enclosing_struct_parse_info;
8246 /* If this struct is defined inside a struct, add it to
8247 struct_types. */
8248 if (warn_cxx_compat
8249 && struct_parse_info != NULL
8250 && !in_sizeof && !in_typeof && !in_alignof)
8251 struct_parse_info->struct_types.safe_push (t);
8253 return t;
8256 static struct {
8257 gt_pointer_operator new_value;
8258 void *cookie;
8259 } resort_data;
8261 /* This routine compares two fields like field_decl_cmp but using the
8262 pointer operator in resort_data. */
8264 static int
8265 resort_field_decl_cmp (const void *x_p, const void *y_p)
8267 const tree *const x = (const tree *) x_p;
8268 const tree *const y = (const tree *) y_p;
8270 if (DECL_NAME (*x) == DECL_NAME (*y))
8271 /* A nontype is "greater" than a type. */
8272 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
8273 if (DECL_NAME (*x) == NULL_TREE)
8274 return -1;
8275 if (DECL_NAME (*y) == NULL_TREE)
8276 return 1;
8278 tree d1 = DECL_NAME (*x);
8279 tree d2 = DECL_NAME (*y);
8280 resort_data.new_value (&d1, resort_data.cookie);
8281 resort_data.new_value (&d2, resort_data.cookie);
8282 if (d1 < d2)
8283 return -1;
8285 return 1;
8288 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
8290 void
8291 resort_sorted_fields (void *obj,
8292 void * ARG_UNUSED (orig_obj),
8293 gt_pointer_operator new_value,
8294 void *cookie)
8296 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
8297 resort_data.new_value = new_value;
8298 resort_data.cookie = cookie;
8299 qsort (&sf->elts[0], sf->len, sizeof (tree),
8300 resort_field_decl_cmp);
8303 /* Lay out the type T, and its element type, and so on. */
8305 static void
8306 layout_array_type (tree t)
8308 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8309 layout_array_type (TREE_TYPE (t));
8310 layout_type (t);
8313 /* Begin compiling the definition of an enumeration type.
8314 NAME is its name (or null if anonymous).
8315 LOC is the enum's location.
8316 Returns the type object, as yet incomplete.
8317 Also records info about it so that build_enumerator
8318 may be used to declare the individual values as they are read. */
8320 tree
8321 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
8323 tree enumtype = NULL_TREE;
8324 location_t enumloc = UNKNOWN_LOCATION;
8326 /* If this is the real definition for a previous forward reference,
8327 fill in the contents in the same object that used to be the
8328 forward reference. */
8330 if (name != NULL_TREE)
8331 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
8333 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
8335 enumtype = make_node (ENUMERAL_TYPE);
8336 pushtag (loc, name, enumtype);
8338 /* Update type location to the one of the definition, instead of e.g.
8339 a forward declaration. */
8340 else if (TYPE_STUB_DECL (enumtype))
8342 enumloc = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype));
8343 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (enumtype)) = loc;
8346 if (C_TYPE_BEING_DEFINED (enumtype))
8347 error_at (loc, "nested redefinition of %<enum %E%>", name);
8349 C_TYPE_BEING_DEFINED (enumtype) = 1;
8351 if (TYPE_VALUES (enumtype) != NULL_TREE)
8353 /* This enum is a named one that has been declared already. */
8354 error_at (loc, "redeclaration of %<enum %E%>", name);
8355 if (enumloc != UNKNOWN_LOCATION)
8356 inform (enumloc, "originally defined here");
8358 /* Completely replace its old definition.
8359 The old enumerators remain defined, however. */
8360 TYPE_VALUES (enumtype) = NULL_TREE;
8363 the_enum->enum_next_value = integer_zero_node;
8364 the_enum->enum_overflow = 0;
8366 if (flag_short_enums)
8367 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
8368 TYPE_PACKED (v) = 1;
8370 /* FIXME: This will issue a warning for a use of a type defined
8371 within sizeof in a statement expr. This is not terribly serious
8372 as C++ doesn't permit statement exprs within sizeof anyhow. */
8373 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
8374 warning_at (loc, OPT_Wc___compat,
8375 "defining type in %qs expression is invalid in C++",
8376 (in_sizeof
8377 ? "sizeof"
8378 : (in_typeof ? "typeof" : "alignof")));
8380 return enumtype;
8383 /* After processing and defining all the values of an enumeration type,
8384 install their decls in the enumeration type and finish it off.
8385 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8386 and ATTRIBUTES are the specified attributes.
8387 Returns ENUMTYPE. */
8389 tree
8390 finish_enum (tree enumtype, tree values, tree attributes)
8392 tree pair, tem;
8393 tree minnode = NULL_TREE, maxnode = NULL_TREE;
8394 int precision;
8395 signop sign;
8396 bool toplevel = (file_scope == current_scope);
8397 struct lang_type *lt;
8399 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8401 /* Calculate the maximum value of any enumerator in this type. */
8403 if (values == error_mark_node)
8404 minnode = maxnode = integer_zero_node;
8405 else
8407 minnode = maxnode = TREE_VALUE (values);
8408 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8410 tree value = TREE_VALUE (pair);
8411 if (tree_int_cst_lt (maxnode, value))
8412 maxnode = value;
8413 if (tree_int_cst_lt (value, minnode))
8414 minnode = value;
8418 /* Construct the final type of this enumeration. It is the same
8419 as one of the integral types - the narrowest one that fits, except
8420 that normally we only go as narrow as int - and signed iff any of
8421 the values are negative. */
8422 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8423 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8424 tree_int_cst_min_precision (maxnode, sign));
8426 /* If the precision of the type was specified with an attribute and it
8427 was too small, give an error. Otherwise, use it. */
8428 if (TYPE_PRECISION (enumtype) && lookup_attribute ("mode", attributes))
8430 if (precision > TYPE_PRECISION (enumtype))
8432 TYPE_PRECISION (enumtype) = 0;
8433 error ("specified mode too small for enumeral values");
8435 else
8436 precision = TYPE_PRECISION (enumtype);
8438 else
8439 TYPE_PRECISION (enumtype) = 0;
8441 if (TYPE_PACKED (enumtype)
8442 || precision > TYPE_PRECISION (integer_type_node)
8443 || TYPE_PRECISION (enumtype))
8445 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8446 if (tem == NULL)
8448 warning (0, "enumeration values exceed range of largest integer");
8449 tem = long_long_integer_type_node;
8452 else
8453 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8455 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8456 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8457 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8458 SET_TYPE_ALIGN (enumtype, TYPE_ALIGN (tem));
8459 TYPE_SIZE (enumtype) = NULL_TREE;
8460 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8462 layout_type (enumtype);
8464 if (values != error_mark_node)
8466 /* Change the type of the enumerators to be the enum type. We
8467 need to do this irrespective of the size of the enum, for
8468 proper type checking. Replace the DECL_INITIALs of the
8469 enumerators, and the value slots of the list, with copies
8470 that have the enum type; they cannot be modified in place
8471 because they may be shared (e.g. integer_zero_node) Finally,
8472 change the purpose slots to point to the names of the decls. */
8473 for (pair = values; pair; pair = TREE_CHAIN (pair))
8475 tree enu = TREE_PURPOSE (pair);
8476 tree ini = DECL_INITIAL (enu);
8478 TREE_TYPE (enu) = enumtype;
8480 /* The ISO C Standard mandates enumerators to have type int,
8481 even though the underlying type of an enum type is
8482 unspecified. However, GCC allows enumerators of any
8483 integer type as an extensions. build_enumerator()
8484 converts any enumerators that fit in an int to type int,
8485 to avoid promotions to unsigned types when comparing
8486 integers with enumerators that fit in the int range.
8487 When -pedantic is given, build_enumerator() would have
8488 already warned about those that don't fit. Here we
8489 convert the rest to the enumerator type. */
8490 if (TREE_TYPE (ini) != integer_type_node)
8491 ini = convert (enumtype, ini);
8493 DECL_INITIAL (enu) = ini;
8494 TREE_PURPOSE (pair) = DECL_NAME (enu);
8495 TREE_VALUE (pair) = ini;
8498 TYPE_VALUES (enumtype) = values;
8501 /* Record the min/max values so that we can warn about bit-field
8502 enumerations that are too small for the values. */
8503 lt = ggc_cleared_alloc<struct lang_type> ();
8504 lt->enum_min = minnode;
8505 lt->enum_max = maxnode;
8506 TYPE_LANG_SPECIFIC (enumtype) = lt;
8508 /* Fix up all variant types of this enum type. */
8509 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8511 if (tem == enumtype)
8512 continue;
8513 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8514 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8515 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8516 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8517 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8518 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8519 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8520 SET_TYPE_ALIGN (tem, TYPE_ALIGN (enumtype));
8521 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8522 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8523 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8526 /* Finish debugging output for this type. */
8527 rest_of_type_compilation (enumtype, toplevel);
8529 /* If this enum is defined inside a struct, add it to
8530 struct_types. */
8531 if (warn_cxx_compat
8532 && struct_parse_info != NULL
8533 && !in_sizeof && !in_typeof && !in_alignof)
8534 struct_parse_info->struct_types.safe_push (enumtype);
8536 return enumtype;
8539 /* Build and install a CONST_DECL for one value of the
8540 current enumeration type (one that was begun with start_enum).
8541 DECL_LOC is the location of the enumerator.
8542 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8543 Return a tree-list containing the CONST_DECL and its value.
8544 Assignment of sequential values by default is handled here. */
8546 tree
8547 build_enumerator (location_t decl_loc, location_t loc,
8548 struct c_enum_contents *the_enum, tree name, tree value)
8550 tree decl, type;
8552 /* Validate and default VALUE. */
8554 if (value != NULL_TREE)
8556 /* Don't issue more errors for error_mark_node (i.e. an
8557 undeclared identifier) - just ignore the value expression. */
8558 if (value == error_mark_node)
8559 value = NULL_TREE;
8560 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8562 error_at (loc, "enumerator value for %qE is not an integer constant",
8563 name);
8564 value = NULL_TREE;
8566 else
8568 if (TREE_CODE (value) != INTEGER_CST)
8570 value = c_fully_fold (value, false, NULL);
8571 if (TREE_CODE (value) == INTEGER_CST)
8572 pedwarn (loc, OPT_Wpedantic,
8573 "enumerator value for %qE is not an integer "
8574 "constant expression", name);
8576 if (TREE_CODE (value) != INTEGER_CST)
8578 error ("enumerator value for %qE is not an integer constant",
8579 name);
8580 value = NULL_TREE;
8582 else
8584 value = default_conversion (value);
8585 constant_expression_warning (value);
8590 /* Default based on previous value. */
8591 /* It should no longer be possible to have NON_LVALUE_EXPR
8592 in the default. */
8593 if (value == NULL_TREE)
8595 value = the_enum->enum_next_value;
8596 if (the_enum->enum_overflow)
8597 error_at (loc, "overflow in enumeration values");
8599 /* Even though the underlying type of an enum is unspecified, the
8600 type of enumeration constants is explicitly defined as int
8601 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8602 an extension. */
8603 else if (!int_fits_type_p (value, integer_type_node))
8604 pedwarn (loc, OPT_Wpedantic,
8605 "ISO C restricts enumerator values to range of %<int%>");
8607 /* The ISO C Standard mandates enumerators to have type int, even
8608 though the underlying type of an enum type is unspecified.
8609 However, GCC allows enumerators of any integer type as an
8610 extensions. Here we convert any enumerators that fit in an int
8611 to type int, to avoid promotions to unsigned types when comparing
8612 integers with enumerators that fit in the int range. When
8613 -pedantic is given, we would have already warned about those that
8614 don't fit. We have to do this here rather than in finish_enum
8615 because this value may be used to define more enumerators. */
8616 if (int_fits_type_p (value, integer_type_node))
8617 value = convert (integer_type_node, value);
8619 /* Set basis for default for next value. */
8620 the_enum->enum_next_value
8621 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8622 PLUS_EXPR, value, integer_one_node, false);
8623 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8625 /* Now create a declaration for the enum value name. */
8627 type = TREE_TYPE (value);
8628 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8629 TYPE_PRECISION (integer_type_node)),
8630 (TYPE_PRECISION (type)
8631 >= TYPE_PRECISION (integer_type_node)
8632 && TYPE_UNSIGNED (type)));
8634 decl = build_decl (decl_loc, CONST_DECL, name, type);
8635 DECL_INITIAL (decl) = convert (type, value);
8636 pushdecl (decl);
8638 return tree_cons (decl, value, NULL_TREE);
8642 /* Create the FUNCTION_DECL for a function definition.
8643 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8644 the declaration; they describe the function's name and the type it returns,
8645 but twisted together in a fashion that parallels the syntax of C.
8647 This function creates a binding context for the function body
8648 as well as setting up the FUNCTION_DECL in current_function_decl.
8650 Returns true on success. If the DECLARATOR is not suitable for a function
8651 (it defines a datum instead), we return false to report a parse error. */
8653 bool
8654 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8655 tree attributes)
8657 tree decl1, old_decl;
8658 tree restype, resdecl;
8659 location_t loc;
8661 current_function_returns_value = 0; /* Assume, until we see it does. */
8662 current_function_returns_null = 0;
8663 current_function_returns_abnormally = 0;
8664 warn_about_return_type = 0;
8665 c_switch_stack = NULL;
8667 /* Indicate no valid break/continue context by setting these variables
8668 to some non-null, non-label value. We'll notice and emit the proper
8669 error message in c_finish_bc_stmt. */
8670 c_break_label = c_cont_label = size_zero_node;
8672 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8673 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8674 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8676 /* If the declarator is not suitable for a function definition,
8677 cause a syntax error. */
8678 if (decl1 == NULL_TREE
8679 || TREE_CODE (decl1) != FUNCTION_DECL)
8680 return false;
8682 loc = DECL_SOURCE_LOCATION (decl1);
8684 c_decl_attributes (&decl1, attributes, 0);
8686 if (DECL_DECLARED_INLINE_P (decl1)
8687 && DECL_UNINLINABLE (decl1)
8688 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8689 warning_at (loc, OPT_Wattributes,
8690 "inline function %qD given attribute noinline",
8691 decl1);
8693 /* Handle gnu_inline attribute. */
8694 if (declspecs->inline_p
8695 && !flag_gnu89_inline
8696 && TREE_CODE (decl1) == FUNCTION_DECL
8697 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8698 || current_function_decl))
8700 if (declspecs->storage_class != csc_static)
8701 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8704 announce_function (decl1);
8706 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8708 error_at (loc, "return type is an incomplete type");
8709 /* Make it return void instead. */
8710 TREE_TYPE (decl1)
8711 = build_function_type (void_type_node,
8712 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8715 if (warn_about_return_type)
8716 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8717 : (warn_return_type ? OPT_Wreturn_type
8718 : OPT_Wimplicit_int),
8719 "return type defaults to %<int%>");
8721 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8722 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8723 DECL_INITIAL (decl1) = error_mark_node;
8725 /* A nested function is not global. */
8726 if (current_function_decl != NULL_TREE)
8727 TREE_PUBLIC (decl1) = 0;
8729 /* If this definition isn't a prototype and we had a prototype declaration
8730 before, copy the arg type info from that prototype. */
8731 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8732 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8733 old_decl = NULL_TREE;
8734 current_function_prototype_locus = UNKNOWN_LOCATION;
8735 current_function_prototype_built_in = false;
8736 current_function_prototype_arg_types = NULL_TREE;
8737 if (!prototype_p (TREE_TYPE (decl1)))
8739 if (old_decl != NULL_TREE
8740 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8741 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8742 TREE_TYPE (TREE_TYPE (old_decl))))
8744 if (stdarg_p (TREE_TYPE (old_decl)))
8746 warning_at (loc, 0, "%q+D defined as variadic function "
8747 "without prototype", decl1);
8748 locate_old_decl (old_decl);
8750 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8751 TREE_TYPE (decl1));
8752 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8753 current_function_prototype_built_in
8754 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8755 current_function_prototype_arg_types
8756 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8758 if (TREE_PUBLIC (decl1))
8760 /* If there is an external prototype declaration of this
8761 function, record its location but do not copy information
8762 to this decl. This may be an invisible declaration
8763 (built-in or in a scope which has finished) or simply
8764 have more refined argument types than any declaration
8765 found above. */
8766 struct c_binding *b;
8767 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8768 if (B_IN_SCOPE (b, external_scope))
8769 break;
8770 if (b)
8772 tree ext_decl, ext_type;
8773 ext_decl = b->decl;
8774 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8775 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8776 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8777 TREE_TYPE (ext_type)))
8779 current_function_prototype_locus
8780 = DECL_SOURCE_LOCATION (ext_decl);
8781 current_function_prototype_built_in
8782 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8783 current_function_prototype_arg_types
8784 = TYPE_ARG_TYPES (ext_type);
8790 /* Optionally warn of old-fashioned def with no previous prototype. */
8791 if (warn_strict_prototypes
8792 && old_decl != error_mark_node
8793 && !prototype_p (TREE_TYPE (decl1))
8794 && C_DECL_ISNT_PROTOTYPE (old_decl))
8795 warning_at (loc, OPT_Wstrict_prototypes,
8796 "function declaration isn%'t a prototype");
8797 /* Optionally warn of any global def with no previous prototype. */
8798 else if (warn_missing_prototypes
8799 && old_decl != error_mark_node
8800 && TREE_PUBLIC (decl1)
8801 && !MAIN_NAME_P (DECL_NAME (decl1))
8802 && C_DECL_ISNT_PROTOTYPE (old_decl)
8803 && !DECL_DECLARED_INLINE_P (decl1))
8804 warning_at (loc, OPT_Wmissing_prototypes,
8805 "no previous prototype for %qD", decl1);
8806 /* Optionally warn of any def with no previous prototype
8807 if the function has already been used. */
8808 else if (warn_missing_prototypes
8809 && old_decl != NULL_TREE
8810 && old_decl != error_mark_node
8811 && TREE_USED (old_decl)
8812 && !prototype_p (TREE_TYPE (old_decl)))
8813 warning_at (loc, OPT_Wmissing_prototypes,
8814 "%qD was used with no prototype before its definition", decl1);
8815 /* Optionally warn of any global def with no previous declaration. */
8816 else if (warn_missing_declarations
8817 && TREE_PUBLIC (decl1)
8818 && old_decl == NULL_TREE
8819 && !MAIN_NAME_P (DECL_NAME (decl1))
8820 && !DECL_DECLARED_INLINE_P (decl1))
8821 warning_at (loc, OPT_Wmissing_declarations,
8822 "no previous declaration for %qD",
8823 decl1);
8824 /* Optionally warn of any def with no previous declaration
8825 if the function has already been used. */
8826 else if (warn_missing_declarations
8827 && old_decl != NULL_TREE
8828 && old_decl != error_mark_node
8829 && TREE_USED (old_decl)
8830 && C_DECL_IMPLICIT (old_decl))
8831 warning_at (loc, OPT_Wmissing_declarations,
8832 "%qD was used with no declaration before its definition", decl1);
8834 /* This function exists in static storage.
8835 (This does not mean `static' in the C sense!) */
8836 TREE_STATIC (decl1) = 1;
8838 /* This is the earliest point at which we might know the assembler
8839 name of the function. Thus, if it's set before this, die horribly. */
8840 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8842 /* If #pragma weak was used, mark the decl weak now. */
8843 if (current_scope == file_scope)
8844 maybe_apply_pragma_weak (decl1);
8846 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8847 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8849 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8850 != integer_type_node)
8851 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8852 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8853 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8854 decl1);
8856 check_main_parameter_types (decl1);
8858 if (!TREE_PUBLIC (decl1))
8859 pedwarn (loc, OPT_Wmain,
8860 "%qD is normally a non-static function", decl1);
8863 /* Record the decl so that the function name is defined.
8864 If we already have a decl for this name, and it is a FUNCTION_DECL,
8865 use the old decl. */
8867 current_function_decl = pushdecl (decl1);
8869 push_scope ();
8870 declare_parm_level ();
8872 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8873 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8874 DECL_ARTIFICIAL (resdecl) = 1;
8875 DECL_IGNORED_P (resdecl) = 1;
8876 DECL_RESULT (current_function_decl) = resdecl;
8878 start_fname_decls ();
8880 return true;
8883 /* Subroutine of store_parm_decls which handles new-style function
8884 definitions (prototype format). The parms already have decls, so we
8885 need only record them as in effect and complain if any redundant
8886 old-style parm decls were written. */
8887 static void
8888 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8890 tree decl;
8891 c_arg_tag *tag;
8892 unsigned ix;
8894 if (current_scope->bindings)
8896 error_at (DECL_SOURCE_LOCATION (fndecl),
8897 "old-style parameter declarations in prototyped "
8898 "function definition");
8900 /* Get rid of the old-style declarations. */
8901 pop_scope ();
8902 push_scope ();
8904 /* Don't issue this warning for nested functions, and don't issue this
8905 warning if we got here because ARG_INFO_TYPES was error_mark_node
8906 (this happens when a function definition has just an ellipsis in
8907 its parameter list). */
8908 else if (!in_system_header_at (input_location)
8909 && !current_function_scope
8910 && arg_info->types != error_mark_node)
8911 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8912 "traditional C rejects ISO C style function definitions");
8914 /* Now make all the parameter declarations visible in the function body.
8915 We can bypass most of the grunt work of pushdecl. */
8916 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8918 DECL_CONTEXT (decl) = current_function_decl;
8919 if (DECL_NAME (decl))
8921 bind (DECL_NAME (decl), decl, current_scope,
8922 /*invisible=*/false, /*nested=*/false,
8923 UNKNOWN_LOCATION);
8924 if (!TREE_USED (decl))
8925 warn_if_shadowing (decl);
8927 else
8928 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8931 /* Record the parameter list in the function declaration. */
8932 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8934 /* Now make all the ancillary declarations visible, likewise. */
8935 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8937 DECL_CONTEXT (decl) = current_function_decl;
8938 if (DECL_NAME (decl))
8939 bind (DECL_NAME (decl), decl, current_scope,
8940 /*invisible=*/false,
8941 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8942 UNKNOWN_LOCATION);
8945 /* And all the tag declarations. */
8946 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8947 if (tag->id)
8948 bind (tag->id, tag->type, current_scope,
8949 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8952 /* Subroutine of store_parm_decls which handles old-style function
8953 definitions (separate parameter list and declarations). */
8955 static void
8956 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8958 struct c_binding *b;
8959 tree parm, decl, last;
8960 tree parmids = arg_info->parms;
8961 hash_set<tree> seen_args;
8963 if (!in_system_header_at (input_location))
8964 warning_at (DECL_SOURCE_LOCATION (fndecl),
8965 OPT_Wold_style_definition, "old-style function definition");
8967 /* Match each formal parameter name with its declaration. Save each
8968 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8969 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8971 if (TREE_VALUE (parm) == NULL_TREE)
8973 error_at (DECL_SOURCE_LOCATION (fndecl),
8974 "parameter name missing from parameter list");
8975 TREE_PURPOSE (parm) = NULL_TREE;
8976 continue;
8979 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8980 if (b && B_IN_CURRENT_SCOPE (b))
8982 decl = b->decl;
8983 /* Skip erroneous parameters. */
8984 if (decl == error_mark_node)
8985 continue;
8986 /* If we got something other than a PARM_DECL it is an error. */
8987 if (TREE_CODE (decl) != PARM_DECL)
8989 error_at (DECL_SOURCE_LOCATION (decl),
8990 "%qD declared as a non-parameter", decl);
8991 continue;
8993 /* If the declaration is already marked, we have a duplicate
8994 name. Complain and ignore the duplicate. */
8995 else if (seen_args.contains (decl))
8997 error_at (DECL_SOURCE_LOCATION (decl),
8998 "multiple parameters named %qD", decl);
8999 TREE_PURPOSE (parm) = NULL_TREE;
9000 continue;
9002 /* If the declaration says "void", complain and turn it into
9003 an int. */
9004 else if (VOID_TYPE_P (TREE_TYPE (decl)))
9006 error_at (DECL_SOURCE_LOCATION (decl),
9007 "parameter %qD declared with void type", decl);
9008 TREE_TYPE (decl) = integer_type_node;
9009 DECL_ARG_TYPE (decl) = integer_type_node;
9010 layout_decl (decl, 0);
9012 warn_if_shadowing (decl);
9014 /* If no declaration found, default to int. */
9015 else
9017 /* FIXME diagnostics: This should be the location of the argument,
9018 not the FNDECL. E.g., for an old-style declaration
9020 int f10(v) { blah; }
9022 We should use the location of the V, not the F10.
9023 Unfortunately, the V is an IDENTIFIER_NODE which has no
9024 location. In the future we need locations for c_arg_info
9025 entries.
9027 See gcc.dg/Wshadow-3.c for an example of this problem. */
9028 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
9029 PARM_DECL, TREE_VALUE (parm), integer_type_node);
9030 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
9031 pushdecl (decl);
9032 warn_if_shadowing (decl);
9034 if (flag_isoc99)
9035 pedwarn (DECL_SOURCE_LOCATION (decl),
9036 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
9037 decl);
9038 else
9039 warning_at (DECL_SOURCE_LOCATION (decl),
9040 OPT_Wmissing_parameter_type,
9041 "type of %qD defaults to %<int%>", decl);
9044 TREE_PURPOSE (parm) = decl;
9045 seen_args.add (decl);
9048 /* Now examine the parms chain for incomplete declarations
9049 and declarations with no corresponding names. */
9051 for (b = current_scope->bindings; b; b = b->prev)
9053 parm = b->decl;
9054 if (TREE_CODE (parm) != PARM_DECL)
9055 continue;
9057 if (TREE_TYPE (parm) != error_mark_node
9058 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
9060 error_at (DECL_SOURCE_LOCATION (parm),
9061 "parameter %qD has incomplete type", parm);
9062 TREE_TYPE (parm) = error_mark_node;
9065 if (!seen_args.contains (parm))
9067 error_at (DECL_SOURCE_LOCATION (parm),
9068 "declaration for parameter %qD but no such parameter",
9069 parm);
9071 /* Pretend the parameter was not missing.
9072 This gets us to a standard state and minimizes
9073 further error messages. */
9074 parmids = chainon (parmids, tree_cons (parm, 0, 0));
9078 /* Chain the declarations together in the order of the list of
9079 names. Store that chain in the function decl, replacing the
9080 list of names. Update the current scope to match. */
9081 DECL_ARGUMENTS (fndecl) = NULL_TREE;
9083 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
9084 if (TREE_PURPOSE (parm))
9085 break;
9086 if (parm && TREE_PURPOSE (parm))
9088 last = TREE_PURPOSE (parm);
9089 DECL_ARGUMENTS (fndecl) = last;
9091 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
9092 if (TREE_PURPOSE (parm))
9094 DECL_CHAIN (last) = TREE_PURPOSE (parm);
9095 last = TREE_PURPOSE (parm);
9097 DECL_CHAIN (last) = NULL_TREE;
9100 /* If there was a previous prototype,
9101 set the DECL_ARG_TYPE of each argument according to
9102 the type previously specified, and report any mismatches. */
9104 if (current_function_prototype_arg_types)
9106 tree type;
9107 for (parm = DECL_ARGUMENTS (fndecl),
9108 type = current_function_prototype_arg_types;
9109 parm || (type != NULL_TREE
9110 && TREE_VALUE (type) != error_mark_node
9111 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node);
9112 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
9114 if (parm == NULL_TREE
9115 || type == NULL_TREE
9116 || (TREE_VALUE (type) != error_mark_node
9117 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node))
9119 if (current_function_prototype_built_in)
9120 warning_at (DECL_SOURCE_LOCATION (fndecl),
9121 0, "number of arguments doesn%'t match "
9122 "built-in prototype");
9123 else
9125 /* FIXME diagnostics: This should be the location of
9126 FNDECL, but there is bug when a prototype is
9127 declared inside function context, but defined
9128 outside of it (e.g., gcc.dg/pr15698-2.c). In
9129 which case FNDECL gets the location of the
9130 prototype, not the definition. */
9131 error_at (input_location,
9132 "number of arguments doesn%'t match prototype");
9134 error_at (current_function_prototype_locus,
9135 "prototype declaration");
9137 break;
9139 /* Type for passing arg must be consistent with that
9140 declared for the arg. ISO C says we take the unqualified
9141 type for parameters declared with qualified type. */
9142 if (TREE_TYPE (parm) != error_mark_node
9143 && TREE_VALUE (type) != error_mark_node
9144 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9145 != TYPE_ATOMIC (TREE_VALUE (type)))
9146 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
9147 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
9149 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
9150 == TYPE_ATOMIC (TREE_VALUE (type)))
9151 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
9152 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
9154 /* Adjust argument to match prototype. E.g. a previous
9155 `int foo(float);' prototype causes
9156 `int foo(x) float x; {...}' to be treated like
9157 `int foo(float x) {...}'. This is particularly
9158 useful for argument types like uid_t. */
9159 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
9161 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
9162 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
9163 && (TYPE_PRECISION (TREE_TYPE (parm))
9164 < TYPE_PRECISION (integer_type_node)))
9165 DECL_ARG_TYPE (parm)
9166 = c_type_promotes_to (TREE_TYPE (parm));
9168 /* ??? Is it possible to get here with a
9169 built-in prototype or will it always have
9170 been diagnosed as conflicting with an
9171 old-style definition and discarded? */
9172 if (current_function_prototype_built_in)
9173 warning_at (DECL_SOURCE_LOCATION (parm),
9174 OPT_Wpedantic, "promoted argument %qD "
9175 "doesn%'t match built-in prototype", parm);
9176 else
9178 pedwarn (DECL_SOURCE_LOCATION (parm),
9179 OPT_Wpedantic, "promoted argument %qD "
9180 "doesn%'t match prototype", parm);
9181 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
9182 "prototype declaration");
9185 else
9187 if (current_function_prototype_built_in)
9188 warning_at (DECL_SOURCE_LOCATION (parm),
9189 0, "argument %qD doesn%'t match "
9190 "built-in prototype", parm);
9191 else
9193 error_at (DECL_SOURCE_LOCATION (parm),
9194 "argument %qD doesn%'t match prototype", parm);
9195 error_at (current_function_prototype_locus,
9196 "prototype declaration");
9201 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = NULL_TREE;
9204 /* Otherwise, create a prototype that would match. */
9206 else
9208 tree actual = NULL_TREE, last = NULL_TREE, type;
9210 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
9212 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
9213 if (last)
9214 TREE_CHAIN (last) = type;
9215 else
9216 actual = type;
9217 last = type;
9219 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
9220 if (last)
9221 TREE_CHAIN (last) = type;
9222 else
9223 actual = type;
9225 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
9226 of the type of this function, but we need to avoid having this
9227 affect the types of other similarly-typed functions, so we must
9228 first force the generation of an identical (but separate) type
9229 node for the relevant function type. The new node we create
9230 will be a variant of the main variant of the original function
9231 type. */
9233 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
9235 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
9239 /* Store parameter declarations passed in ARG_INFO into the current
9240 function declaration. */
9242 void
9243 store_parm_decls_from (struct c_arg_info *arg_info)
9245 current_function_arg_info = arg_info;
9246 store_parm_decls ();
9249 /* Called by walk_tree to look for and update context-less labels. */
9251 static tree
9252 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
9254 if (TREE_CODE (*tp) == LABEL_EXPR
9255 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
9257 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
9258 *walk_subtrees = 0;
9261 return NULL_TREE;
9264 /* Store the parameter declarations into the current function declaration.
9265 This is called after parsing the parameter declarations, before
9266 digesting the body of the function.
9268 For an old-style definition, construct a prototype out of the old-style
9269 parameter declarations and inject it into the function's type. */
9271 void
9272 store_parm_decls (void)
9274 tree fndecl = current_function_decl;
9275 bool proto;
9277 /* The argument information block for FNDECL. */
9278 struct c_arg_info *arg_info = current_function_arg_info;
9279 current_function_arg_info = 0;
9281 /* True if this definition is written with a prototype. Note:
9282 despite C99 6.7.5.3p14, we can *not* treat an empty argument
9283 list in a function definition as equivalent to (void) -- an
9284 empty argument list specifies the function has no parameters,
9285 but only (void) sets up a prototype for future calls. */
9286 proto = arg_info->types != 0;
9288 if (proto)
9289 store_parm_decls_newstyle (fndecl, arg_info);
9290 else
9291 store_parm_decls_oldstyle (fndecl, arg_info);
9293 /* The next call to push_scope will be a function body. */
9295 next_is_function_body = true;
9297 /* Write a record describing this function definition to the prototypes
9298 file (if requested). */
9300 gen_aux_info_record (fndecl, 1, 0, proto);
9302 /* Initialize the RTL code for the function. */
9303 allocate_struct_function (fndecl, false);
9305 if (warn_unused_local_typedefs)
9306 cfun->language = ggc_cleared_alloc<language_function> ();
9308 /* Begin the statement tree for this function. */
9309 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
9311 /* ??? Insert the contents of the pending sizes list into the function
9312 to be evaluated. The only reason left to have this is
9313 void foo(int n, int array[n++])
9314 because we throw away the array type in favor of a pointer type, and
9315 thus won't naturally see the SAVE_EXPR containing the increment. All
9316 other pending sizes would be handled by gimplify_parameters. */
9317 if (arg_info->pending_sizes)
9319 /* In very special circumstances, e.g. for code like
9320 _Atomic int i = 5;
9321 void f (int a[i += 2]) {}
9322 we need to execute the atomic assignment on function entry.
9323 But in this case, it is not just a straight store, it has the
9324 op= form, which means that build_atomic_assign has generated
9325 gotos, labels, etc. Because at that time the function decl
9326 for F has not been created yet, those labels do not have any
9327 function context. But we have the fndecl now, so update the
9328 labels accordingly. gimplify_expr would crash otherwise. */
9329 walk_tree_without_duplicates (&arg_info->pending_sizes,
9330 set_labels_context_r, fndecl);
9331 add_stmt (arg_info->pending_sizes);
9335 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
9336 c_finish_omp_declare_simd for function prototypes. No diagnostics
9337 should be done. */
9339 void
9340 temp_store_parm_decls (tree fndecl, tree parms)
9342 push_scope ();
9343 for (tree p = parms; p; p = DECL_CHAIN (p))
9345 DECL_CONTEXT (p) = fndecl;
9346 if (DECL_NAME (p))
9347 bind (DECL_NAME (p), p, current_scope,
9348 /*invisible=*/false, /*nested=*/false,
9349 UNKNOWN_LOCATION);
9353 /* Undo what temp_store_parm_decls did. */
9355 void
9356 temp_pop_parm_decls (void)
9358 /* Clear all bindings in this temporary scope, so that
9359 pop_scope doesn't create a BLOCK. */
9360 struct c_binding *b = current_scope->bindings;
9361 current_scope->bindings = NULL;
9362 for (; b; b = free_binding_and_advance (b))
9364 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
9365 || b->decl == error_mark_node);
9366 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
9367 I_SYMBOL_BINDING (b->id) = b->shadowed;
9368 if (b->shadowed && b->shadowed->u.type)
9369 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
9371 pop_scope ();
9375 /* Finish up a function declaration and compile that function
9376 all the way to assembler language output. Then free the storage
9377 for the function definition.
9379 This is called after parsing the body of the function definition. */
9381 void
9382 finish_function (void)
9384 tree fndecl = current_function_decl;
9386 if (c_dialect_objc ())
9387 objc_finish_function ();
9389 if (TREE_CODE (fndecl) == FUNCTION_DECL
9390 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
9392 tree args = DECL_ARGUMENTS (fndecl);
9393 for (; args; args = DECL_CHAIN (args))
9395 tree type = TREE_TYPE (args);
9396 if (INTEGRAL_TYPE_P (type)
9397 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9398 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9402 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9403 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9405 /* Must mark the RESULT_DECL as being in this function. */
9407 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9408 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9410 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9411 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9412 == integer_type_node && flag_isoc99)
9414 /* Hack. We don't want the middle-end to warn that this return
9415 is unreachable, so we mark its location as special. Using
9416 UNKNOWN_LOCATION has the problem that it gets clobbered in
9417 annotate_one_with_locus. A cleaner solution might be to
9418 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9420 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9423 /* Tie off the statement tree for this function. */
9424 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9426 finish_fname_decls ();
9428 /* Complain if there's just no return statement. */
9429 if (warn_return_type
9430 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9431 && !current_function_returns_value && !current_function_returns_null
9432 /* Don't complain if we are no-return. */
9433 && !current_function_returns_abnormally
9434 /* Don't complain if we are declared noreturn. */
9435 && !TREE_THIS_VOLATILE (fndecl)
9436 /* Don't warn for main(). */
9437 && !MAIN_NAME_P (DECL_NAME (fndecl))
9438 /* Or if they didn't actually specify a return type. */
9439 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9440 /* Normally, with -Wreturn-type, flow will complain, but we might
9441 optimize out static functions. */
9442 && !TREE_PUBLIC (fndecl))
9444 warning (OPT_Wreturn_type,
9445 "no return statement in function returning non-void");
9446 TREE_NO_WARNING (fndecl) = 1;
9449 /* Complain about parameters that are only set, but never otherwise used. */
9450 if (warn_unused_but_set_parameter)
9452 tree decl;
9454 for (decl = DECL_ARGUMENTS (fndecl);
9455 decl;
9456 decl = DECL_CHAIN (decl))
9457 if (TREE_USED (decl)
9458 && TREE_CODE (decl) == PARM_DECL
9459 && !DECL_READ_P (decl)
9460 && DECL_NAME (decl)
9461 && !DECL_ARTIFICIAL (decl)
9462 && !TREE_NO_WARNING (decl))
9463 warning_at (DECL_SOURCE_LOCATION (decl),
9464 OPT_Wunused_but_set_parameter,
9465 "parameter %qD set but not used", decl);
9468 /* Complain about locally defined typedefs that are not used in this
9469 function. */
9470 maybe_warn_unused_local_typedefs ();
9472 /* Possibly warn about unused parameters. */
9473 if (warn_unused_parameter)
9474 do_warn_unused_parameter (fndecl);
9476 /* Store the end of the function, so that we get good line number
9477 info for the epilogue. */
9478 cfun->function_end_locus = input_location;
9480 /* Finalize the ELF visibility for the function. */
9481 c_determine_visibility (fndecl);
9483 /* For GNU C extern inline functions disregard inline limits. */
9484 if (DECL_EXTERNAL (fndecl)
9485 && DECL_DECLARED_INLINE_P (fndecl)
9486 && (flag_gnu89_inline
9487 || lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (fndecl))))
9488 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9490 /* Genericize before inlining. Delay genericizing nested functions
9491 until their parent function is genericized. Since finalizing
9492 requires GENERIC, delay that as well. */
9494 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9495 && !undef_nested_function)
9497 if (!decl_function_context (fndecl))
9499 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9500 c_genericize (fndecl);
9502 /* ??? Objc emits functions after finalizing the compilation unit.
9503 This should be cleaned up later and this conditional removed. */
9504 if (symtab->global_info_ready)
9506 cgraph_node::add_new_function (fndecl, false);
9507 return;
9509 cgraph_node::finalize_function (fndecl, false);
9511 else
9513 /* Register this function with cgraph just far enough to get it
9514 added to our parent's nested function list. Handy, since the
9515 C front end doesn't have such a list. */
9516 (void) cgraph_node::get_create (fndecl);
9520 if (!decl_function_context (fndecl))
9521 undef_nested_function = false;
9523 if (cfun->language != NULL)
9525 ggc_free (cfun->language);
9526 cfun->language = NULL;
9529 /* We're leaving the context of this function, so zap cfun.
9530 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9531 tree_rest_of_compilation. */
9532 set_cfun (NULL);
9533 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9534 current_function_decl = NULL;
9537 /* Check the declarations given in a for-loop for satisfying the C99
9538 constraints. If exactly one such decl is found, return it. LOC is
9539 the location of the opening parenthesis of the for loop. The last
9540 parameter allows you to control the "for loop initial declarations
9541 are only allowed in C99 mode". Normally, you should pass
9542 flag_isoc99 as that parameter. But in some cases (Objective-C
9543 foreach loop, for example) we want to run the checks in this
9544 function even if not in C99 mode, so we allow the caller to turn
9545 off the error about not being in C99 mode.
9548 tree
9549 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9551 struct c_binding *b;
9552 tree one_decl = NULL_TREE;
9553 int n_decls = 0;
9555 if (!turn_off_iso_c99_error)
9557 static bool hint = true;
9558 /* If we get here, declarations have been used in a for loop without
9559 the C99 for loop scope. This doesn't make much sense, so don't
9560 allow it. */
9561 error_at (loc, "%<for%> loop initial declarations "
9562 "are only allowed in C99 or C11 mode");
9563 if (hint)
9565 inform (loc,
9566 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9567 "to compile your code");
9568 hint = false;
9570 return NULL_TREE;
9572 /* C99 subclause 6.8.5 paragraph 3:
9574 [#3] The declaration part of a for statement shall only
9575 declare identifiers for objects having storage class auto or
9576 register.
9578 It isn't clear whether, in this sentence, "identifiers" binds to
9579 "shall only declare" or to "objects" - that is, whether all identifiers
9580 declared must be identifiers for objects, or whether the restriction
9581 only applies to those that are. (A question on this in comp.std.c
9582 in November 2000 received no answer.) We implement the strictest
9583 interpretation, to avoid creating an extension which later causes
9584 problems. */
9586 for (b = current_scope->bindings; b; b = b->prev)
9588 tree id = b->id;
9589 tree decl = b->decl;
9591 if (!id)
9592 continue;
9594 switch (TREE_CODE (decl))
9596 case VAR_DECL:
9598 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9599 if (TREE_STATIC (decl))
9600 error_at (decl_loc,
9601 "declaration of static variable %qD in %<for%> loop "
9602 "initial declaration", decl);
9603 else if (DECL_EXTERNAL (decl))
9604 error_at (decl_loc,
9605 "declaration of %<extern%> variable %qD in %<for%> loop "
9606 "initial declaration", decl);
9608 break;
9610 case RECORD_TYPE:
9611 error_at (loc,
9612 "%<struct %E%> declared in %<for%> loop initial "
9613 "declaration", id);
9614 break;
9615 case UNION_TYPE:
9616 error_at (loc,
9617 "%<union %E%> declared in %<for%> loop initial declaration",
9618 id);
9619 break;
9620 case ENUMERAL_TYPE:
9621 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9622 "initial declaration", id);
9623 break;
9624 default:
9625 error_at (loc, "declaration of non-variable "
9626 "%qD in %<for%> loop initial declaration", decl);
9629 n_decls++;
9630 one_decl = decl;
9633 return n_decls == 1 ? one_decl : NULL_TREE;
9636 /* Save and reinitialize the variables
9637 used during compilation of a C function. */
9639 void
9640 c_push_function_context (void)
9642 struct language_function *p = cfun->language;
9643 /* cfun->language might have been already allocated by the use of
9644 -Wunused-local-typedefs. In that case, just re-use it. */
9645 if (p == NULL)
9646 cfun->language = p = ggc_cleared_alloc<language_function> ();
9648 p->base.x_stmt_tree = c_stmt_tree;
9649 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9650 p->x_break_label = c_break_label;
9651 p->x_cont_label = c_cont_label;
9652 p->x_switch_stack = c_switch_stack;
9653 p->arg_info = current_function_arg_info;
9654 p->returns_value = current_function_returns_value;
9655 p->returns_null = current_function_returns_null;
9656 p->returns_abnormally = current_function_returns_abnormally;
9657 p->warn_about_return_type = warn_about_return_type;
9659 push_function_context ();
9662 /* Restore the variables used during compilation of a C function. */
9664 void
9665 c_pop_function_context (void)
9667 struct language_function *p;
9669 pop_function_context ();
9670 p = cfun->language;
9672 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9673 used to store data throughout the life time of the current cfun,
9674 So don't deallocate it. */
9675 if (!warn_unused_local_typedefs)
9676 cfun->language = NULL;
9678 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9679 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9681 /* Stop pointing to the local nodes about to be freed. */
9682 /* But DECL_INITIAL must remain nonzero so we know this
9683 was an actual function definition. */
9684 DECL_INITIAL (current_function_decl) = error_mark_node;
9685 DECL_ARGUMENTS (current_function_decl) = NULL_TREE;
9688 c_stmt_tree = p->base.x_stmt_tree;
9689 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9690 c_break_label = p->x_break_label;
9691 c_cont_label = p->x_cont_label;
9692 c_switch_stack = p->x_switch_stack;
9693 current_function_arg_info = p->arg_info;
9694 current_function_returns_value = p->returns_value;
9695 current_function_returns_null = p->returns_null;
9696 current_function_returns_abnormally = p->returns_abnormally;
9697 warn_about_return_type = p->warn_about_return_type;
9700 /* The functions below are required for functionality of doing
9701 function at once processing in the C front end. Currently these
9702 functions are not called from anywhere in the C front end, but as
9703 these changes continue, that will change. */
9705 /* Returns the stmt_tree (if any) to which statements are currently
9706 being added. If there is no active statement-tree, NULL is
9707 returned. */
9709 stmt_tree
9710 current_stmt_tree (void)
9712 return &c_stmt_tree;
9715 /* Return the global value of T as a symbol. */
9717 tree
9718 identifier_global_value (tree t)
9720 struct c_binding *b;
9722 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9723 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9724 return b->decl;
9726 return NULL_TREE;
9729 /* In C, the only C-linkage public declaration is at file scope. */
9731 tree
9732 c_linkage_bindings (tree name)
9734 return identifier_global_value (name);
9737 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9738 otherwise the name is found in ridpointers from RID_INDEX. */
9740 void
9741 record_builtin_type (enum rid rid_index, const char *name, tree type)
9743 tree id, decl;
9744 if (name == 0)
9745 id = ridpointers[(int) rid_index];
9746 else
9747 id = get_identifier (name);
9748 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9749 pushdecl (decl);
9750 if (debug_hooks->type_decl)
9751 debug_hooks->type_decl (decl, false);
9754 /* Build the void_list_node (void_type_node having been created). */
9755 tree
9756 build_void_list_node (void)
9758 tree t = build_tree_list (NULL_TREE, void_type_node);
9759 return t;
9762 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9764 struct c_parm *
9765 build_c_parm (struct c_declspecs *specs, tree attrs,
9766 struct c_declarator *declarator,
9767 location_t loc)
9769 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9770 ret->specs = specs;
9771 ret->attrs = attrs;
9772 ret->declarator = declarator;
9773 ret->loc = loc;
9774 return ret;
9777 /* Return a declarator with nested attributes. TARGET is the inner
9778 declarator to which these attributes apply. ATTRS are the
9779 attributes. */
9781 struct c_declarator *
9782 build_attrs_declarator (tree attrs, struct c_declarator *target)
9784 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9785 ret->kind = cdk_attrs;
9786 ret->declarator = target;
9787 ret->u.attrs = attrs;
9788 return ret;
9791 /* Return a declarator for a function with arguments specified by ARGS
9792 and return type specified by TARGET. */
9794 struct c_declarator *
9795 build_function_declarator (struct c_arg_info *args,
9796 struct c_declarator *target)
9798 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9799 ret->kind = cdk_function;
9800 ret->declarator = target;
9801 ret->u.arg_info = args;
9802 return ret;
9805 /* Return a declarator for the identifier IDENT (which may be
9806 NULL_TREE for an abstract declarator). */
9808 struct c_declarator *
9809 build_id_declarator (tree ident)
9811 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9812 ret->kind = cdk_id;
9813 ret->declarator = 0;
9814 ret->u.id = ident;
9815 /* Default value - may get reset to a more precise location. */
9816 ret->id_loc = input_location;
9817 return ret;
9820 /* Return something to represent absolute declarators containing a *.
9821 TARGET is the absolute declarator that the * contains.
9822 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9823 to apply to the pointer type. */
9825 struct c_declarator *
9826 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9827 struct c_declarator *target)
9829 tree attrs;
9830 int quals = 0;
9831 struct c_declarator *itarget = target;
9832 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9833 if (type_quals_attrs)
9835 attrs = type_quals_attrs->attrs;
9836 quals = quals_from_declspecs (type_quals_attrs);
9837 if (attrs != NULL_TREE)
9838 itarget = build_attrs_declarator (attrs, target);
9840 ret->kind = cdk_pointer;
9841 ret->declarator = itarget;
9842 ret->u.pointer_quals = quals;
9843 return ret;
9846 /* Return a pointer to a structure for an empty list of declaration
9847 specifiers. */
9849 struct c_declspecs *
9850 build_null_declspecs (void)
9852 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9853 memset (ret, 0, sizeof *ret);
9854 ret->align_log = -1;
9855 ret->typespec_word = cts_none;
9856 ret->storage_class = csc_none;
9857 ret->expr_const_operands = true;
9858 ret->typespec_kind = ctsk_none;
9859 ret->address_space = ADDR_SPACE_GENERIC;
9860 return ret;
9863 /* Add the address space ADDRSPACE to the declaration specifiers
9864 SPECS, returning SPECS. */
9866 struct c_declspecs *
9867 declspecs_add_addrspace (source_location location,
9868 struct c_declspecs *specs, addr_space_t as)
9870 specs->non_sc_seen_p = true;
9871 specs->declspecs_seen_p = true;
9873 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9874 && specs->address_space != as)
9875 error ("incompatible address space qualifiers %qs and %qs",
9876 c_addr_space_name (as),
9877 c_addr_space_name (specs->address_space));
9878 else
9880 specs->address_space = as;
9881 specs->locations[cdw_address_space] = location;
9883 return specs;
9886 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9887 returning SPECS. */
9889 struct c_declspecs *
9890 declspecs_add_qual (source_location loc,
9891 struct c_declspecs *specs, tree qual)
9893 enum rid i;
9894 bool dupe = false;
9895 specs->non_sc_seen_p = true;
9896 specs->declspecs_seen_p = true;
9897 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9898 && C_IS_RESERVED_WORD (qual));
9899 i = C_RID_CODE (qual);
9900 location_t prev_loc = UNKNOWN_LOCATION;
9901 switch (i)
9903 case RID_CONST:
9904 dupe = specs->const_p;
9905 specs->const_p = true;
9906 prev_loc = specs->locations[cdw_const];
9907 specs->locations[cdw_const] = loc;
9908 break;
9909 case RID_VOLATILE:
9910 dupe = specs->volatile_p;
9911 specs->volatile_p = true;
9912 prev_loc = specs->locations[cdw_volatile];
9913 specs->locations[cdw_volatile] = loc;
9914 break;
9915 case RID_RESTRICT:
9916 dupe = specs->restrict_p;
9917 specs->restrict_p = true;
9918 prev_loc = specs->locations[cdw_restrict];
9919 specs->locations[cdw_restrict] = loc;
9920 break;
9921 case RID_ATOMIC:
9922 dupe = specs->atomic_p;
9923 specs->atomic_p = true;
9924 prev_loc = specs->locations[cdw_atomic];
9925 specs->locations[cdw_atomic] = loc;
9926 break;
9927 default:
9928 gcc_unreachable ();
9930 if (dupe)
9932 bool warned = pedwarn_c90 (loc, OPT_Wpedantic,
9933 "duplicate %qE declaration specifier", qual);
9934 if (!warned
9935 && warn_duplicate_decl_specifier
9936 && prev_loc >= RESERVED_LOCATION_COUNT
9937 && !from_macro_expansion_at (prev_loc)
9938 && !from_macro_expansion_at (loc))
9939 warning_at (loc, OPT_Wduplicate_decl_specifier,
9940 "duplicate %qE declaration specifier", qual);
9942 return specs;
9945 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9946 returning SPECS. */
9948 struct c_declspecs *
9949 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9950 struct c_typespec spec)
9952 tree type = spec.spec;
9953 specs->non_sc_seen_p = true;
9954 specs->declspecs_seen_p = true;
9955 specs->typespec_kind = spec.kind;
9956 if (TREE_DEPRECATED (type))
9957 specs->deprecated_p = true;
9959 /* Handle type specifier keywords. */
9960 if (TREE_CODE (type) == IDENTIFIER_NODE
9961 && C_IS_RESERVED_WORD (type)
9962 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9964 enum rid i = C_RID_CODE (type);
9965 if (specs->type)
9967 error_at (loc, "two or more data types in declaration specifiers");
9968 return specs;
9970 if ((int) i <= (int) RID_LAST_MODIFIER)
9972 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9973 bool dupe = false;
9974 switch (i)
9976 case RID_LONG:
9977 if (specs->long_long_p)
9979 error_at (loc, "%<long long long%> is too long for GCC");
9980 break;
9982 if (specs->long_p)
9984 if (specs->typespec_word == cts_double)
9986 error_at (loc,
9987 ("both %<long long%> and %<double%> in "
9988 "declaration specifiers"));
9989 break;
9991 pedwarn_c90 (loc, OPT_Wlong_long,
9992 "ISO C90 does not support %<long long%>");
9993 specs->long_long_p = 1;
9994 specs->locations[cdw_long_long] = loc;
9995 break;
9997 if (specs->short_p)
9998 error_at (loc,
9999 ("both %<long%> and %<short%> in "
10000 "declaration specifiers"));
10001 else if (specs->typespec_word == cts_auto_type)
10002 error_at (loc,
10003 ("both %<long%> and %<__auto_type%> in "
10004 "declaration specifiers"));
10005 else if (specs->typespec_word == cts_void)
10006 error_at (loc,
10007 ("both %<long%> and %<void%> in "
10008 "declaration specifiers"));
10009 else if (specs->typespec_word == cts_int_n)
10010 error_at (loc,
10011 ("both %<long%> and %<__int%d%> in "
10012 "declaration specifiers"),
10013 int_n_data[specs->int_n_idx].bitsize);
10014 else if (specs->typespec_word == cts_bool)
10015 error_at (loc,
10016 ("both %<long%> and %<_Bool%> in "
10017 "declaration specifiers"));
10018 else if (specs->typespec_word == cts_char)
10019 error_at (loc,
10020 ("both %<long%> and %<char%> in "
10021 "declaration specifiers"));
10022 else if (specs->typespec_word == cts_float)
10023 error_at (loc,
10024 ("both %<long%> and %<float%> in "
10025 "declaration specifiers"));
10026 else if (specs->typespec_word == cts_floatn_nx)
10027 error_at (loc,
10028 ("both %<long%> and %<_Float%d%s%> in "
10029 "declaration specifiers"),
10030 floatn_nx_types[specs->floatn_nx_idx].n,
10031 (floatn_nx_types[specs->floatn_nx_idx].extended
10032 ? "x"
10033 : ""));
10034 else if (specs->typespec_word == cts_dfloat32)
10035 error_at (loc,
10036 ("both %<long%> and %<_Decimal32%> in "
10037 "declaration specifiers"));
10038 else if (specs->typespec_word == cts_dfloat64)
10039 error_at (loc,
10040 ("both %<long%> and %<_Decimal64%> in "
10041 "declaration specifiers"));
10042 else if (specs->typespec_word == cts_dfloat128)
10043 error_at (loc,
10044 ("both %<long%> and %<_Decimal128%> in "
10045 "declaration specifiers"));
10046 else
10048 specs->long_p = true;
10049 specs->locations[cdw_long] = loc;
10051 break;
10052 case RID_SHORT:
10053 dupe = specs->short_p;
10054 if (specs->long_p)
10055 error_at (loc,
10056 ("both %<long%> and %<short%> in "
10057 "declaration specifiers"));
10058 else if (specs->typespec_word == cts_auto_type)
10059 error_at (loc,
10060 ("both %<short%> and %<__auto_type%> in "
10061 "declaration specifiers"));
10062 else if (specs->typespec_word == cts_void)
10063 error_at (loc,
10064 ("both %<short%> and %<void%> in "
10065 "declaration specifiers"));
10066 else if (specs->typespec_word == cts_int_n)
10067 error_at (loc,
10068 ("both %<short%> and %<__int%d%> in "
10069 "declaration specifiers"),
10070 int_n_data[specs->int_n_idx].bitsize);
10071 else if (specs->typespec_word == cts_bool)
10072 error_at (loc,
10073 ("both %<short%> and %<_Bool%> in "
10074 "declaration specifiers"));
10075 else if (specs->typespec_word == cts_char)
10076 error_at (loc,
10077 ("both %<short%> and %<char%> in "
10078 "declaration specifiers"));
10079 else if (specs->typespec_word == cts_float)
10080 error_at (loc,
10081 ("both %<short%> and %<float%> in "
10082 "declaration specifiers"));
10083 else if (specs->typespec_word == cts_double)
10084 error_at (loc,
10085 ("both %<short%> and %<double%> in "
10086 "declaration specifiers"));
10087 else if (specs->typespec_word == cts_floatn_nx)
10088 error_at (loc,
10089 ("both %<short%> and %<_Float%d%s%> in "
10090 "declaration specifiers"),
10091 floatn_nx_types[specs->floatn_nx_idx].n,
10092 (floatn_nx_types[specs->floatn_nx_idx].extended
10093 ? "x"
10094 : ""));
10095 else if (specs->typespec_word == cts_dfloat32)
10096 error_at (loc,
10097 ("both %<short%> and %<_Decimal32%> in "
10098 "declaration specifiers"));
10099 else if (specs->typespec_word == cts_dfloat64)
10100 error_at (loc,
10101 ("both %<short%> and %<_Decimal64%> in "
10102 "declaration specifiers"));
10103 else if (specs->typespec_word == cts_dfloat128)
10104 error_at (loc,
10105 ("both %<short%> and %<_Decimal128%> in "
10106 "declaration specifiers"));
10107 else
10109 specs->short_p = true;
10110 specs->locations[cdw_short] = loc;
10112 break;
10113 case RID_SIGNED:
10114 dupe = specs->signed_p;
10115 if (specs->unsigned_p)
10116 error_at (loc,
10117 ("both %<signed%> and %<unsigned%> in "
10118 "declaration specifiers"));
10119 else if (specs->typespec_word == cts_auto_type)
10120 error_at (loc,
10121 ("both %<signed%> and %<__auto_type%> in "
10122 "declaration specifiers"));
10123 else if (specs->typespec_word == cts_void)
10124 error_at (loc,
10125 ("both %<signed%> and %<void%> in "
10126 "declaration specifiers"));
10127 else if (specs->typespec_word == cts_bool)
10128 error_at (loc,
10129 ("both %<signed%> and %<_Bool%> in "
10130 "declaration specifiers"));
10131 else if (specs->typespec_word == cts_float)
10132 error_at (loc,
10133 ("both %<signed%> and %<float%> in "
10134 "declaration specifiers"));
10135 else if (specs->typespec_word == cts_double)
10136 error_at (loc,
10137 ("both %<signed%> and %<double%> in "
10138 "declaration specifiers"));
10139 else if (specs->typespec_word == cts_floatn_nx)
10140 error_at (loc,
10141 ("both %<signed%> and %<_Float%d%s%> in "
10142 "declaration specifiers"),
10143 floatn_nx_types[specs->floatn_nx_idx].n,
10144 (floatn_nx_types[specs->floatn_nx_idx].extended
10145 ? "x"
10146 : ""));
10147 else if (specs->typespec_word == cts_dfloat32)
10148 error_at (loc,
10149 ("both %<signed%> and %<_Decimal32%> in "
10150 "declaration specifiers"));
10151 else if (specs->typespec_word == cts_dfloat64)
10152 error_at (loc,
10153 ("both %<signed%> and %<_Decimal64%> in "
10154 "declaration specifiers"));
10155 else if (specs->typespec_word == cts_dfloat128)
10156 error_at (loc,
10157 ("both %<signed%> and %<_Decimal128%> in "
10158 "declaration specifiers"));
10159 else
10161 specs->signed_p = true;
10162 specs->locations[cdw_signed] = loc;
10164 break;
10165 case RID_UNSIGNED:
10166 dupe = specs->unsigned_p;
10167 if (specs->signed_p)
10168 error_at (loc,
10169 ("both %<signed%> and %<unsigned%> in "
10170 "declaration specifiers"));
10171 else if (specs->typespec_word == cts_auto_type)
10172 error_at (loc,
10173 ("both %<unsigned%> and %<__auto_type%> in "
10174 "declaration specifiers"));
10175 else if (specs->typespec_word == cts_void)
10176 error_at (loc,
10177 ("both %<unsigned%> and %<void%> in "
10178 "declaration specifiers"));
10179 else if (specs->typespec_word == cts_bool)
10180 error_at (loc,
10181 ("both %<unsigned%> and %<_Bool%> in "
10182 "declaration specifiers"));
10183 else if (specs->typespec_word == cts_float)
10184 error_at (loc,
10185 ("both %<unsigned%> and %<float%> in "
10186 "declaration specifiers"));
10187 else if (specs->typespec_word == cts_double)
10188 error_at (loc,
10189 ("both %<unsigned%> and %<double%> in "
10190 "declaration specifiers"));
10191 else if (specs->typespec_word == cts_floatn_nx)
10192 error_at (loc,
10193 ("both %<unsigned%> and %<_Float%d%s%> in "
10194 "declaration specifiers"),
10195 floatn_nx_types[specs->floatn_nx_idx].n,
10196 (floatn_nx_types[specs->floatn_nx_idx].extended
10197 ? "x"
10198 : ""));
10199 else if (specs->typespec_word == cts_dfloat32)
10200 error_at (loc,
10201 ("both %<unsigned%> and %<_Decimal32%> in "
10202 "declaration specifiers"));
10203 else if (specs->typespec_word == cts_dfloat64)
10204 error_at (loc,
10205 ("both %<unsigned%> and %<_Decimal64%> in "
10206 "declaration specifiers"));
10207 else if (specs->typespec_word == cts_dfloat128)
10208 error_at (loc,
10209 ("both %<unsigned%> and %<_Decimal128%> in "
10210 "declaration specifiers"));
10211 else
10213 specs->unsigned_p = true;
10214 specs->locations[cdw_unsigned] = loc;
10216 break;
10217 case RID_COMPLEX:
10218 dupe = specs->complex_p;
10219 if (!in_system_header_at (loc))
10220 pedwarn_c90 (loc, OPT_Wpedantic,
10221 "ISO C90 does not support complex types");
10222 if (specs->typespec_word == cts_auto_type)
10223 error_at (loc,
10224 ("both %<complex%> and %<__auto_type%> in "
10225 "declaration specifiers"));
10226 else if (specs->typespec_word == cts_void)
10227 error_at (loc,
10228 ("both %<complex%> and %<void%> in "
10229 "declaration specifiers"));
10230 else if (specs->typespec_word == cts_bool)
10231 error_at (loc,
10232 ("both %<complex%> and %<_Bool%> in "
10233 "declaration specifiers"));
10234 else if (specs->typespec_word == cts_dfloat32)
10235 error_at (loc,
10236 ("both %<complex%> and %<_Decimal32%> in "
10237 "declaration specifiers"));
10238 else if (specs->typespec_word == cts_dfloat64)
10239 error_at (loc,
10240 ("both %<complex%> and %<_Decimal64%> in "
10241 "declaration specifiers"));
10242 else if (specs->typespec_word == cts_dfloat128)
10243 error_at (loc,
10244 ("both %<complex%> and %<_Decimal128%> in "
10245 "declaration specifiers"));
10246 else if (specs->typespec_word == cts_fract)
10247 error_at (loc,
10248 ("both %<complex%> and %<_Fract%> in "
10249 "declaration specifiers"));
10250 else if (specs->typespec_word == cts_accum)
10251 error_at (loc,
10252 ("both %<complex%> and %<_Accum%> in "
10253 "declaration specifiers"));
10254 else if (specs->saturating_p)
10255 error_at (loc,
10256 ("both %<complex%> and %<_Sat%> in "
10257 "declaration specifiers"));
10258 else
10260 specs->complex_p = true;
10261 specs->locations[cdw_complex] = loc;
10263 break;
10264 case RID_SAT:
10265 dupe = specs->saturating_p;
10266 pedwarn (loc, OPT_Wpedantic,
10267 "ISO C does not support saturating types");
10268 if (specs->typespec_word == cts_int_n)
10270 error_at (loc,
10271 ("both %<_Sat%> and %<__int%d%> in "
10272 "declaration specifiers"),
10273 int_n_data[specs->int_n_idx].bitsize);
10275 else if (specs->typespec_word == cts_auto_type)
10276 error_at (loc,
10277 ("both %<_Sat%> and %<__auto_type%> in "
10278 "declaration specifiers"));
10279 else if (specs->typespec_word == cts_void)
10280 error_at (loc,
10281 ("both %<_Sat%> and %<void%> in "
10282 "declaration specifiers"));
10283 else if (specs->typespec_word == cts_bool)
10284 error_at (loc,
10285 ("both %<_Sat%> and %<_Bool%> in "
10286 "declaration specifiers"));
10287 else if (specs->typespec_word == cts_char)
10288 error_at (loc,
10289 ("both %<_Sat%> and %<char%> in "
10290 "declaration specifiers"));
10291 else if (specs->typespec_word == cts_int)
10292 error_at (loc,
10293 ("both %<_Sat%> and %<int%> in "
10294 "declaration specifiers"));
10295 else if (specs->typespec_word == cts_float)
10296 error_at (loc,
10297 ("both %<_Sat%> and %<float%> in "
10298 "declaration specifiers"));
10299 else if (specs->typespec_word == cts_double)
10300 error_at (loc,
10301 ("both %<_Sat%> and %<double%> in "
10302 "declaration specifiers"));
10303 else if (specs->typespec_word == cts_floatn_nx)
10304 error_at (loc,
10305 ("both %<_Sat%> and %<_Float%d%s%> in "
10306 "declaration specifiers"),
10307 floatn_nx_types[specs->floatn_nx_idx].n,
10308 (floatn_nx_types[specs->floatn_nx_idx].extended
10309 ? "x"
10310 : ""));
10311 else if (specs->typespec_word == cts_dfloat32)
10312 error_at (loc,
10313 ("both %<_Sat%> and %<_Decimal32%> in "
10314 "declaration specifiers"));
10315 else if (specs->typespec_word == cts_dfloat64)
10316 error_at (loc,
10317 ("both %<_Sat%> and %<_Decimal64%> in "
10318 "declaration specifiers"));
10319 else if (specs->typespec_word == cts_dfloat128)
10320 error_at (loc,
10321 ("both %<_Sat%> and %<_Decimal128%> in "
10322 "declaration specifiers"));
10323 else if (specs->complex_p)
10324 error_at (loc,
10325 ("both %<_Sat%> and %<complex%> in "
10326 "declaration specifiers"));
10327 else
10329 specs->saturating_p = true;
10330 specs->locations[cdw_saturating] = loc;
10332 break;
10333 default:
10334 gcc_unreachable ();
10337 if (dupe)
10338 error_at (loc, "duplicate %qE", type);
10340 return specs;
10342 else
10344 /* "void", "_Bool", "char", "int", "float", "double",
10345 "_FloatN", "_FloatNx", "_Decimal32", "__intN",
10346 "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
10347 "__auto_type". */
10348 if (specs->typespec_word != cts_none)
10350 error_at (loc,
10351 "two or more data types in declaration specifiers");
10352 return specs;
10354 switch (i)
10356 case RID_AUTO_TYPE:
10357 if (specs->long_p)
10358 error_at (loc,
10359 ("both %<long%> and %<__auto_type%> in "
10360 "declaration specifiers"));
10361 else if (specs->short_p)
10362 error_at (loc,
10363 ("both %<short%> and %<__auto_type%> in "
10364 "declaration specifiers"));
10365 else if (specs->signed_p)
10366 error_at (loc,
10367 ("both %<signed%> and %<__auto_type%> in "
10368 "declaration specifiers"));
10369 else if (specs->unsigned_p)
10370 error_at (loc,
10371 ("both %<unsigned%> and %<__auto_type%> in "
10372 "declaration specifiers"));
10373 else if (specs->complex_p)
10374 error_at (loc,
10375 ("both %<complex%> and %<__auto_type%> in "
10376 "declaration specifiers"));
10377 else if (specs->saturating_p)
10378 error_at (loc,
10379 ("both %<_Sat%> and %<__auto_type%> in "
10380 "declaration specifiers"));
10381 else
10383 specs->typespec_word = cts_auto_type;
10384 specs->locations[cdw_typespec] = loc;
10386 return specs;
10387 case RID_INT_N_0:
10388 case RID_INT_N_1:
10389 case RID_INT_N_2:
10390 case RID_INT_N_3:
10391 specs->int_n_idx = i - RID_INT_N_0;
10392 if (!in_system_header_at (input_location))
10393 pedwarn (loc, OPT_Wpedantic,
10394 "ISO C does not support %<__int%d%> types",
10395 int_n_data[specs->int_n_idx].bitsize);
10397 if (specs->long_p)
10398 error_at (loc,
10399 ("both %<__int%d%> and %<long%> in "
10400 "declaration specifiers"),
10401 int_n_data[specs->int_n_idx].bitsize);
10402 else if (specs->saturating_p)
10403 error_at (loc,
10404 ("both %<_Sat%> and %<__int%d%> in "
10405 "declaration specifiers"),
10406 int_n_data[specs->int_n_idx].bitsize);
10407 else if (specs->short_p)
10408 error_at (loc,
10409 ("both %<__int%d%> and %<short%> in "
10410 "declaration specifiers"),
10411 int_n_data[specs->int_n_idx].bitsize);
10412 else if (! int_n_enabled_p[specs->int_n_idx])
10414 specs->typespec_word = cts_int_n;
10415 error_at (loc,
10416 "%<__int%d%> is not supported on this target",
10417 int_n_data[specs->int_n_idx].bitsize);
10419 else
10421 specs->typespec_word = cts_int_n;
10422 specs->locations[cdw_typespec] = loc;
10424 return specs;
10425 case RID_VOID:
10426 if (specs->long_p)
10427 error_at (loc,
10428 ("both %<long%> and %<void%> in "
10429 "declaration specifiers"));
10430 else if (specs->short_p)
10431 error_at (loc,
10432 ("both %<short%> and %<void%> in "
10433 "declaration specifiers"));
10434 else if (specs->signed_p)
10435 error_at (loc,
10436 ("both %<signed%> and %<void%> in "
10437 "declaration specifiers"));
10438 else if (specs->unsigned_p)
10439 error_at (loc,
10440 ("both %<unsigned%> and %<void%> in "
10441 "declaration specifiers"));
10442 else if (specs->complex_p)
10443 error_at (loc,
10444 ("both %<complex%> and %<void%> in "
10445 "declaration specifiers"));
10446 else if (specs->saturating_p)
10447 error_at (loc,
10448 ("both %<_Sat%> and %<void%> in "
10449 "declaration specifiers"));
10450 else
10452 specs->typespec_word = cts_void;
10453 specs->locations[cdw_typespec] = loc;
10455 return specs;
10456 case RID_BOOL:
10457 if (!in_system_header_at (loc))
10458 pedwarn_c90 (loc, OPT_Wpedantic,
10459 "ISO C90 does not support boolean types");
10460 if (specs->long_p)
10461 error_at (loc,
10462 ("both %<long%> and %<_Bool%> in "
10463 "declaration specifiers"));
10464 else if (specs->short_p)
10465 error_at (loc,
10466 ("both %<short%> and %<_Bool%> in "
10467 "declaration specifiers"));
10468 else if (specs->signed_p)
10469 error_at (loc,
10470 ("both %<signed%> and %<_Bool%> in "
10471 "declaration specifiers"));
10472 else if (specs->unsigned_p)
10473 error_at (loc,
10474 ("both %<unsigned%> and %<_Bool%> in "
10475 "declaration specifiers"));
10476 else if (specs->complex_p)
10477 error_at (loc,
10478 ("both %<complex%> and %<_Bool%> in "
10479 "declaration specifiers"));
10480 else if (specs->saturating_p)
10481 error_at (loc,
10482 ("both %<_Sat%> and %<_Bool%> in "
10483 "declaration specifiers"));
10484 else
10486 specs->typespec_word = cts_bool;
10487 specs->locations[cdw_typespec] = loc;
10489 return specs;
10490 case RID_CHAR:
10491 if (specs->long_p)
10492 error_at (loc,
10493 ("both %<long%> and %<char%> in "
10494 "declaration specifiers"));
10495 else if (specs->short_p)
10496 error_at (loc,
10497 ("both %<short%> and %<char%> in "
10498 "declaration specifiers"));
10499 else if (specs->saturating_p)
10500 error_at (loc,
10501 ("both %<_Sat%> and %<char%> in "
10502 "declaration specifiers"));
10503 else
10505 specs->typespec_word = cts_char;
10506 specs->locations[cdw_typespec] = loc;
10508 return specs;
10509 case RID_INT:
10510 if (specs->saturating_p)
10511 error_at (loc,
10512 ("both %<_Sat%> and %<int%> in "
10513 "declaration specifiers"));
10514 else
10516 specs->typespec_word = cts_int;
10517 specs->locations[cdw_typespec] = loc;
10519 return specs;
10520 case RID_FLOAT:
10521 if (specs->long_p)
10522 error_at (loc,
10523 ("both %<long%> and %<float%> in "
10524 "declaration specifiers"));
10525 else if (specs->short_p)
10526 error_at (loc,
10527 ("both %<short%> and %<float%> in "
10528 "declaration specifiers"));
10529 else if (specs->signed_p)
10530 error_at (loc,
10531 ("both %<signed%> and %<float%> in "
10532 "declaration specifiers"));
10533 else if (specs->unsigned_p)
10534 error_at (loc,
10535 ("both %<unsigned%> and %<float%> in "
10536 "declaration specifiers"));
10537 else if (specs->saturating_p)
10538 error_at (loc,
10539 ("both %<_Sat%> and %<float%> in "
10540 "declaration specifiers"));
10541 else
10543 specs->typespec_word = cts_float;
10544 specs->locations[cdw_typespec] = loc;
10546 return specs;
10547 case RID_DOUBLE:
10548 if (specs->long_long_p)
10549 error_at (loc,
10550 ("both %<long long%> and %<double%> in "
10551 "declaration specifiers"));
10552 else if (specs->short_p)
10553 error_at (loc,
10554 ("both %<short%> and %<double%> in "
10555 "declaration specifiers"));
10556 else if (specs->signed_p)
10557 error_at (loc,
10558 ("both %<signed%> and %<double%> in "
10559 "declaration specifiers"));
10560 else if (specs->unsigned_p)
10561 error_at (loc,
10562 ("both %<unsigned%> and %<double%> in "
10563 "declaration specifiers"));
10564 else if (specs->saturating_p)
10565 error_at (loc,
10566 ("both %<_Sat%> and %<double%> in "
10567 "declaration specifiers"));
10568 else
10570 specs->typespec_word = cts_double;
10571 specs->locations[cdw_typespec] = loc;
10573 return specs;
10574 CASE_RID_FLOATN_NX:
10575 specs->floatn_nx_idx = i - RID_FLOATN_NX_FIRST;
10576 if (!in_system_header_at (input_location))
10577 pedwarn (loc, OPT_Wpedantic,
10578 "ISO C does not support the %<_Float%d%s%> type",
10579 floatn_nx_types[specs->floatn_nx_idx].n,
10580 (floatn_nx_types[specs->floatn_nx_idx].extended
10581 ? "x"
10582 : ""));
10584 if (specs->long_p)
10585 error_at (loc,
10586 ("both %<long%> and %<_Float%d%s%> in "
10587 "declaration specifiers"),
10588 floatn_nx_types[specs->floatn_nx_idx].n,
10589 (floatn_nx_types[specs->floatn_nx_idx].extended
10590 ? "x"
10591 : ""));
10592 else if (specs->short_p)
10593 error_at (loc,
10594 ("both %<short%> and %<_Float%d%s%> in "
10595 "declaration specifiers"),
10596 floatn_nx_types[specs->floatn_nx_idx].n,
10597 (floatn_nx_types[specs->floatn_nx_idx].extended
10598 ? "x"
10599 : ""));
10600 else if (specs->signed_p)
10601 error_at (loc,
10602 ("both %<signed%> and %<_Float%d%s%> in "
10603 "declaration specifiers"),
10604 floatn_nx_types[specs->floatn_nx_idx].n,
10605 (floatn_nx_types[specs->floatn_nx_idx].extended
10606 ? "x"
10607 : ""));
10608 else if (specs->unsigned_p)
10609 error_at (loc,
10610 ("both %<unsigned%> and %<_Float%d%s%> in "
10611 "declaration specifiers"),
10612 floatn_nx_types[specs->floatn_nx_idx].n,
10613 (floatn_nx_types[specs->floatn_nx_idx].extended
10614 ? "x"
10615 : ""));
10616 else if (specs->saturating_p)
10617 error_at (loc,
10618 ("both %<_Sat%> and %<_Float%d%s%> in "
10619 "declaration specifiers"),
10620 floatn_nx_types[specs->floatn_nx_idx].n,
10621 (floatn_nx_types[specs->floatn_nx_idx].extended
10622 ? "x"
10623 : ""));
10624 else if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
10626 specs->typespec_word = cts_floatn_nx;
10627 error_at (loc,
10628 "%<_Float%d%s%> is not supported on this target",
10629 floatn_nx_types[specs->floatn_nx_idx].n,
10630 (floatn_nx_types[specs->floatn_nx_idx].extended
10631 ? "x"
10632 : ""));
10634 else
10636 specs->typespec_word = cts_floatn_nx;
10637 specs->locations[cdw_typespec] = loc;
10639 return specs;
10640 case RID_DFLOAT32:
10641 case RID_DFLOAT64:
10642 case RID_DFLOAT128:
10644 const char *str;
10645 if (i == RID_DFLOAT32)
10646 str = "_Decimal32";
10647 else if (i == RID_DFLOAT64)
10648 str = "_Decimal64";
10649 else
10650 str = "_Decimal128";
10651 if (specs->long_long_p)
10652 error_at (loc,
10653 ("both %<long long%> and %qs in "
10654 "declaration specifiers"),
10655 str);
10656 if (specs->long_p)
10657 error_at (loc,
10658 ("both %<long%> and %qs in "
10659 "declaration specifiers"),
10660 str);
10661 else if (specs->short_p)
10662 error_at (loc,
10663 ("both %<short%> and %qs in "
10664 "declaration specifiers"),
10665 str);
10666 else if (specs->signed_p)
10667 error_at (loc,
10668 ("both %<signed%> and %qs in "
10669 "declaration specifiers"),
10670 str);
10671 else if (specs->unsigned_p)
10672 error_at (loc,
10673 ("both %<unsigned%> and %qs in "
10674 "declaration specifiers"),
10675 str);
10676 else if (specs->complex_p)
10677 error_at (loc,
10678 ("both %<complex%> and %qs in "
10679 "declaration specifiers"),
10680 str);
10681 else if (specs->saturating_p)
10682 error_at (loc,
10683 ("both %<_Sat%> and %qs in "
10684 "declaration specifiers"),
10685 str);
10686 else if (i == RID_DFLOAT32)
10687 specs->typespec_word = cts_dfloat32;
10688 else if (i == RID_DFLOAT64)
10689 specs->typespec_word = cts_dfloat64;
10690 else
10691 specs->typespec_word = cts_dfloat128;
10692 specs->locations[cdw_typespec] = loc;
10694 if (!targetm.decimal_float_supported_p ())
10695 error_at (loc,
10696 ("decimal floating point not supported "
10697 "for this target"));
10698 pedwarn (loc, OPT_Wpedantic,
10699 "ISO C does not support decimal floating point");
10700 return specs;
10701 case RID_FRACT:
10702 case RID_ACCUM:
10704 const char *str;
10705 if (i == RID_FRACT)
10706 str = "_Fract";
10707 else
10708 str = "_Accum";
10709 if (specs->complex_p)
10710 error_at (loc,
10711 ("both %<complex%> and %qs in "
10712 "declaration specifiers"),
10713 str);
10714 else if (i == RID_FRACT)
10715 specs->typespec_word = cts_fract;
10716 else
10717 specs->typespec_word = cts_accum;
10718 specs->locations[cdw_typespec] = loc;
10720 if (!targetm.fixed_point_supported_p ())
10721 error_at (loc,
10722 "fixed-point types not supported for this target");
10723 pedwarn (loc, OPT_Wpedantic,
10724 "ISO C does not support fixed-point types");
10725 return specs;
10726 default:
10727 /* ObjC reserved word "id", handled below. */
10728 break;
10733 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10734 form of ObjC type, cases such as "int" and "long" being handled
10735 above), a TYPE (struct, union, enum and typeof specifiers) or an
10736 ERROR_MARK. In none of these cases may there have previously
10737 been any type specifiers. */
10738 if (specs->type || specs->typespec_word != cts_none
10739 || specs->long_p || specs->short_p || specs->signed_p
10740 || specs->unsigned_p || specs->complex_p)
10741 error_at (loc, "two or more data types in declaration specifiers");
10742 else if (TREE_CODE (type) == TYPE_DECL)
10744 if (TREE_TYPE (type) == error_mark_node)
10745 ; /* Allow the type to default to int to avoid cascading errors. */
10746 else
10748 specs->type = TREE_TYPE (type);
10749 specs->decl_attr = DECL_ATTRIBUTES (type);
10750 specs->typedef_p = true;
10751 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10752 specs->locations[cdw_typedef] = loc;
10754 /* If this typedef name is defined in a struct, then a C++
10755 lookup would return a different value. */
10756 if (warn_cxx_compat
10757 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10758 warning_at (loc, OPT_Wc___compat,
10759 "C++ lookup of %qD would return a field, not a type",
10760 type);
10762 /* If we are parsing a struct, record that a struct field
10763 used a typedef. */
10764 if (warn_cxx_compat && struct_parse_info != NULL)
10765 struct_parse_info->typedefs_seen.safe_push (type);
10768 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10770 tree t = lookup_name (type);
10771 if (!t || TREE_CODE (t) != TYPE_DECL)
10772 error_at (loc, "%qE fails to be a typedef or built in type", type);
10773 else if (TREE_TYPE (t) == error_mark_node)
10775 else
10777 specs->type = TREE_TYPE (t);
10778 specs->locations[cdw_typespec] = loc;
10781 else
10783 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10785 specs->typedef_p = true;
10786 specs->locations[cdw_typedef] = loc;
10787 if (spec.expr)
10789 if (specs->expr)
10790 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10791 specs->expr, spec.expr);
10792 else
10793 specs->expr = spec.expr;
10794 specs->expr_const_operands &= spec.expr_const_operands;
10797 specs->type = type;
10800 return specs;
10803 /* Add the storage class specifier or function specifier SCSPEC to the
10804 declaration specifiers SPECS, returning SPECS. */
10806 struct c_declspecs *
10807 declspecs_add_scspec (source_location loc,
10808 struct c_declspecs *specs,
10809 tree scspec)
10811 enum rid i;
10812 enum c_storage_class n = csc_none;
10813 bool dupe = false;
10814 specs->declspecs_seen_p = true;
10815 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10816 && C_IS_RESERVED_WORD (scspec));
10817 i = C_RID_CODE (scspec);
10818 if (specs->non_sc_seen_p)
10819 warning (OPT_Wold_style_declaration,
10820 "%qE is not at beginning of declaration", scspec);
10821 switch (i)
10823 case RID_INLINE:
10824 /* C99 permits duplicate inline. Although of doubtful utility,
10825 it seems simplest to permit it in gnu89 mode as well, as
10826 there is also little utility in maintaining this as a
10827 difference between gnu89 and C99 inline. */
10828 dupe = false;
10829 specs->inline_p = true;
10830 specs->locations[cdw_inline] = loc;
10831 break;
10832 case RID_NORETURN:
10833 /* Duplicate _Noreturn is permitted. */
10834 dupe = false;
10835 specs->noreturn_p = true;
10836 specs->locations[cdw_noreturn] = loc;
10837 break;
10838 case RID_THREAD:
10839 dupe = specs->thread_p;
10840 if (specs->storage_class == csc_auto)
10841 error ("%qE used with %<auto%>", scspec);
10842 else if (specs->storage_class == csc_register)
10843 error ("%qE used with %<register%>", scspec);
10844 else if (specs->storage_class == csc_typedef)
10845 error ("%qE used with %<typedef%>", scspec);
10846 else
10848 specs->thread_p = true;
10849 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10850 "__thread") == 0);
10851 /* A diagnostic is not required for the use of this
10852 identifier in the implementation namespace; only diagnose
10853 it for the C11 spelling because of existing code using
10854 the other spelling. */
10855 if (!specs->thread_gnu_p)
10857 if (flag_isoc99)
10858 pedwarn_c99 (loc, OPT_Wpedantic,
10859 "ISO C99 does not support %qE", scspec);
10860 else
10861 pedwarn_c99 (loc, OPT_Wpedantic,
10862 "ISO C90 does not support %qE", scspec);
10864 specs->locations[cdw_thread] = loc;
10866 break;
10867 case RID_AUTO:
10868 n = csc_auto;
10869 break;
10870 case RID_EXTERN:
10871 n = csc_extern;
10872 /* Diagnose "__thread extern". */
10873 if (specs->thread_p && specs->thread_gnu_p)
10874 error ("%<__thread%> before %<extern%>");
10875 break;
10876 case RID_REGISTER:
10877 n = csc_register;
10878 break;
10879 case RID_STATIC:
10880 n = csc_static;
10881 /* Diagnose "__thread static". */
10882 if (specs->thread_p && specs->thread_gnu_p)
10883 error ("%<__thread%> before %<static%>");
10884 break;
10885 case RID_TYPEDEF:
10886 n = csc_typedef;
10887 break;
10888 default:
10889 gcc_unreachable ();
10891 if (n != csc_none && n == specs->storage_class)
10892 dupe = true;
10893 if (dupe)
10895 if (i == RID_THREAD)
10896 error ("duplicate %<_Thread_local%> or %<__thread%>");
10897 else
10898 error ("duplicate %qE", scspec);
10900 if (n != csc_none)
10902 if (specs->storage_class != csc_none && n != specs->storage_class)
10904 error ("multiple storage classes in declaration specifiers");
10906 else
10908 specs->storage_class = n;
10909 specs->locations[cdw_storage_class] = loc;
10910 if (n != csc_extern && n != csc_static && specs->thread_p)
10912 error ("%qs used with %qE",
10913 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10914 scspec);
10915 specs->thread_p = false;
10919 return specs;
10922 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10923 returning SPECS. */
10925 struct c_declspecs *
10926 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10928 specs->attrs = chainon (attrs, specs->attrs);
10929 specs->locations[cdw_attributes] = loc;
10930 specs->declspecs_seen_p = true;
10931 return specs;
10934 /* Add an _Alignas specifier (expression ALIGN, or type whose
10935 alignment is ALIGN) to the declaration specifiers SPECS, returning
10936 SPECS. */
10937 struct c_declspecs *
10938 declspecs_add_alignas (source_location loc,
10939 struct c_declspecs *specs, tree align)
10941 int align_log;
10942 specs->alignas_p = true;
10943 specs->locations[cdw_alignas] = loc;
10944 if (align == error_mark_node)
10945 return specs;
10946 align_log = check_user_alignment (align, true);
10947 if (align_log > specs->align_log)
10948 specs->align_log = align_log;
10949 return specs;
10952 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10953 specifiers with any other type specifier to determine the resulting
10954 type. This is where ISO C checks on complex types are made, since
10955 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10956 double". */
10958 struct c_declspecs *
10959 finish_declspecs (struct c_declspecs *specs)
10961 /* If a type was specified as a whole, we have no modifiers and are
10962 done. */
10963 if (specs->type != NULL_TREE)
10965 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10966 && !specs->signed_p && !specs->unsigned_p
10967 && !specs->complex_p);
10969 /* Set a dummy type. */
10970 if (TREE_CODE (specs->type) == ERROR_MARK)
10971 specs->type = integer_type_node;
10972 return specs;
10975 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10976 has been specified, treat it as "int" unless "_Complex" is
10977 present and there are no other specifiers. If we just have
10978 "_Complex", it is equivalent to "_Complex double", but e.g.
10979 "_Complex short" is equivalent to "_Complex short int". */
10980 if (specs->typespec_word == cts_none)
10982 if (specs->saturating_p)
10984 error_at (specs->locations[cdw_saturating],
10985 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10986 if (!targetm.fixed_point_supported_p ())
10987 error_at (specs->locations[cdw_saturating],
10988 "fixed-point types not supported for this target");
10989 specs->typespec_word = cts_fract;
10991 else if (specs->long_p || specs->short_p
10992 || specs->signed_p || specs->unsigned_p)
10994 specs->typespec_word = cts_int;
10996 else if (specs->complex_p)
10998 specs->typespec_word = cts_double;
10999 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11000 "ISO C does not support plain %<complex%> meaning "
11001 "%<double complex%>");
11003 else
11005 specs->typespec_word = cts_int;
11006 specs->default_int_p = true;
11007 /* We don't diagnose this here because grokdeclarator will
11008 give more specific diagnostics according to whether it is
11009 a function definition. */
11013 /* If "signed" was specified, record this to distinguish "int" and
11014 "signed int" in the case of a bit-field with
11015 -funsigned-bitfields. */
11016 specs->explicit_signed_p = specs->signed_p;
11018 /* Now compute the actual type. */
11019 switch (specs->typespec_word)
11021 case cts_auto_type:
11022 gcc_assert (!specs->long_p && !specs->short_p
11023 && !specs->signed_p && !specs->unsigned_p
11024 && !specs->complex_p);
11025 /* Type to be filled in later. */
11026 break;
11027 case cts_void:
11028 gcc_assert (!specs->long_p && !specs->short_p
11029 && !specs->signed_p && !specs->unsigned_p
11030 && !specs->complex_p);
11031 specs->type = void_type_node;
11032 break;
11033 case cts_bool:
11034 gcc_assert (!specs->long_p && !specs->short_p
11035 && !specs->signed_p && !specs->unsigned_p
11036 && !specs->complex_p);
11037 specs->type = boolean_type_node;
11038 break;
11039 case cts_char:
11040 gcc_assert (!specs->long_p && !specs->short_p);
11041 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11042 if (specs->signed_p)
11043 specs->type = signed_char_type_node;
11044 else if (specs->unsigned_p)
11045 specs->type = unsigned_char_type_node;
11046 else
11047 specs->type = char_type_node;
11048 if (specs->complex_p)
11050 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11051 "ISO C does not support complex integer types");
11052 specs->type = build_complex_type (specs->type);
11054 break;
11055 case cts_int_n:
11056 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
11057 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11058 if (! int_n_enabled_p[specs->int_n_idx])
11059 specs->type = integer_type_node;
11060 else
11061 specs->type = (specs->unsigned_p
11062 ? int_n_trees[specs->int_n_idx].unsigned_type
11063 : int_n_trees[specs->int_n_idx].signed_type);
11064 if (specs->complex_p)
11066 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11067 "ISO C does not support complex integer types");
11068 specs->type = build_complex_type (specs->type);
11070 break;
11071 case cts_int:
11072 gcc_assert (!(specs->long_p && specs->short_p));
11073 gcc_assert (!(specs->signed_p && specs->unsigned_p));
11074 if (specs->long_long_p)
11075 specs->type = (specs->unsigned_p
11076 ? long_long_unsigned_type_node
11077 : long_long_integer_type_node);
11078 else if (specs->long_p)
11079 specs->type = (specs->unsigned_p
11080 ? long_unsigned_type_node
11081 : long_integer_type_node);
11082 else if (specs->short_p)
11083 specs->type = (specs->unsigned_p
11084 ? short_unsigned_type_node
11085 : short_integer_type_node);
11086 else
11087 specs->type = (specs->unsigned_p
11088 ? unsigned_type_node
11089 : integer_type_node);
11090 if (specs->complex_p)
11092 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
11093 "ISO C does not support complex integer types");
11094 specs->type = build_complex_type (specs->type);
11096 break;
11097 case cts_float:
11098 gcc_assert (!specs->long_p && !specs->short_p
11099 && !specs->signed_p && !specs->unsigned_p);
11100 specs->type = (specs->complex_p
11101 ? complex_float_type_node
11102 : float_type_node);
11103 break;
11104 case cts_double:
11105 gcc_assert (!specs->long_long_p && !specs->short_p
11106 && !specs->signed_p && !specs->unsigned_p);
11107 if (specs->long_p)
11109 specs->type = (specs->complex_p
11110 ? complex_long_double_type_node
11111 : long_double_type_node);
11113 else
11115 specs->type = (specs->complex_p
11116 ? complex_double_type_node
11117 : double_type_node);
11119 break;
11120 case cts_floatn_nx:
11121 gcc_assert (!specs->long_p && !specs->short_p
11122 && !specs->signed_p && !specs->unsigned_p);
11123 if (FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx) == NULL_TREE)
11124 specs->type = integer_type_node;
11125 else if (specs->complex_p)
11126 specs->type = COMPLEX_FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11127 else
11128 specs->type = FLOATN_NX_TYPE_NODE (specs->floatn_nx_idx);
11129 break;
11130 case cts_dfloat32:
11131 case cts_dfloat64:
11132 case cts_dfloat128:
11133 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
11134 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
11135 if (specs->typespec_word == cts_dfloat32)
11136 specs->type = dfloat32_type_node;
11137 else if (specs->typespec_word == cts_dfloat64)
11138 specs->type = dfloat64_type_node;
11139 else
11140 specs->type = dfloat128_type_node;
11141 break;
11142 case cts_fract:
11143 gcc_assert (!specs->complex_p);
11144 if (!targetm.fixed_point_supported_p ())
11145 specs->type = integer_type_node;
11146 else if (specs->saturating_p)
11148 if (specs->long_long_p)
11149 specs->type = specs->unsigned_p
11150 ? sat_unsigned_long_long_fract_type_node
11151 : sat_long_long_fract_type_node;
11152 else if (specs->long_p)
11153 specs->type = specs->unsigned_p
11154 ? sat_unsigned_long_fract_type_node
11155 : sat_long_fract_type_node;
11156 else if (specs->short_p)
11157 specs->type = specs->unsigned_p
11158 ? sat_unsigned_short_fract_type_node
11159 : sat_short_fract_type_node;
11160 else
11161 specs->type = specs->unsigned_p
11162 ? sat_unsigned_fract_type_node
11163 : sat_fract_type_node;
11165 else
11167 if (specs->long_long_p)
11168 specs->type = specs->unsigned_p
11169 ? unsigned_long_long_fract_type_node
11170 : long_long_fract_type_node;
11171 else if (specs->long_p)
11172 specs->type = specs->unsigned_p
11173 ? unsigned_long_fract_type_node
11174 : long_fract_type_node;
11175 else if (specs->short_p)
11176 specs->type = specs->unsigned_p
11177 ? unsigned_short_fract_type_node
11178 : short_fract_type_node;
11179 else
11180 specs->type = specs->unsigned_p
11181 ? unsigned_fract_type_node
11182 : fract_type_node;
11184 break;
11185 case cts_accum:
11186 gcc_assert (!specs->complex_p);
11187 if (!targetm.fixed_point_supported_p ())
11188 specs->type = integer_type_node;
11189 else if (specs->saturating_p)
11191 if (specs->long_long_p)
11192 specs->type = specs->unsigned_p
11193 ? sat_unsigned_long_long_accum_type_node
11194 : sat_long_long_accum_type_node;
11195 else if (specs->long_p)
11196 specs->type = specs->unsigned_p
11197 ? sat_unsigned_long_accum_type_node
11198 : sat_long_accum_type_node;
11199 else if (specs->short_p)
11200 specs->type = specs->unsigned_p
11201 ? sat_unsigned_short_accum_type_node
11202 : sat_short_accum_type_node;
11203 else
11204 specs->type = specs->unsigned_p
11205 ? sat_unsigned_accum_type_node
11206 : sat_accum_type_node;
11208 else
11210 if (specs->long_long_p)
11211 specs->type = specs->unsigned_p
11212 ? unsigned_long_long_accum_type_node
11213 : long_long_accum_type_node;
11214 else if (specs->long_p)
11215 specs->type = specs->unsigned_p
11216 ? unsigned_long_accum_type_node
11217 : long_accum_type_node;
11218 else if (specs->short_p)
11219 specs->type = specs->unsigned_p
11220 ? unsigned_short_accum_type_node
11221 : short_accum_type_node;
11222 else
11223 specs->type = specs->unsigned_p
11224 ? unsigned_accum_type_node
11225 : accum_type_node;
11227 break;
11228 default:
11229 gcc_unreachable ();
11232 return specs;
11235 /* Perform final processing on one file scope's declarations (or the
11236 external scope's declarations), GLOBALS. */
11238 static void
11239 c_write_global_declarations_1 (tree globals)
11241 tree decl;
11242 bool reconsider;
11244 /* Process the decls in the order they were written. */
11245 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11247 /* Check for used but undefined static functions using the C
11248 standard's definition of "used", and set TREE_NO_WARNING so
11249 that check_global_declaration doesn't repeat the check. */
11250 if (TREE_CODE (decl) == FUNCTION_DECL
11251 && DECL_INITIAL (decl) == NULL_TREE
11252 && DECL_EXTERNAL (decl)
11253 && !TREE_PUBLIC (decl))
11255 if (C_DECL_USED (decl))
11257 pedwarn (input_location, 0, "%q+F used but never defined", decl);
11258 TREE_NO_WARNING (decl) = 1;
11260 /* For -Wunused-function warn about unused static prototypes. */
11261 else if (warn_unused_function
11262 && ! DECL_ARTIFICIAL (decl)
11263 && ! TREE_NO_WARNING (decl))
11265 warning (OPT_Wunused_function,
11266 "%q+F declared %<static%> but never defined", decl);
11267 TREE_NO_WARNING (decl) = 1;
11271 wrapup_global_declaration_1 (decl);
11276 reconsider = false;
11277 for (decl = globals; decl; decl = DECL_CHAIN (decl))
11278 reconsider |= wrapup_global_declaration_2 (decl);
11280 while (reconsider);
11283 /* Callback to collect a source_ref from a DECL. */
11285 static void
11286 collect_source_ref_cb (tree decl)
11288 if (!DECL_IS_BUILTIN (decl))
11289 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
11292 /* Preserve the external declarations scope across a garbage collect. */
11293 static GTY(()) tree ext_block;
11295 /* Collect all references relevant to SOURCE_FILE. */
11297 static void
11298 collect_all_refs (const char *source_file)
11300 tree t;
11301 unsigned i;
11303 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11304 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
11306 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
11309 /* Iterate over all global declarations and call CALLBACK. */
11311 static void
11312 for_each_global_decl (void (*callback) (tree decl))
11314 tree t;
11315 tree decls;
11316 tree decl;
11317 unsigned i;
11319 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11321 decls = DECL_INITIAL (t);
11322 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
11323 callback (decl);
11326 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
11327 callback (decl);
11330 /* Perform any final parser cleanups and generate initial debugging
11331 information. */
11333 void
11334 c_parse_final_cleanups (void)
11336 tree t;
11337 unsigned i;
11339 /* We don't want to do this if generating a PCH. */
11340 if (pch_file)
11341 return;
11343 timevar_stop (TV_PHASE_PARSING);
11344 timevar_start (TV_PHASE_DEFERRED);
11346 /* Do the Objective-C stuff. This is where all the Objective-C
11347 module stuff gets generated (symtab, class/protocol/selector
11348 lists etc). */
11349 if (c_dialect_objc ())
11350 objc_write_global_declarations ();
11352 /* Close the external scope. */
11353 ext_block = pop_scope ();
11354 external_scope = 0;
11355 gcc_assert (!current_scope);
11357 /* Handle -fdump-ada-spec[-slim]. */
11358 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
11360 /* Build a table of files to generate specs for */
11361 if (flag_dump_ada_spec_slim)
11362 collect_source_ref (main_input_filename);
11363 else
11364 for_each_global_decl (collect_source_ref_cb);
11366 dump_ada_specs (collect_all_refs, NULL);
11369 /* Process all file scopes in this compilation, and the external_scope,
11370 through wrapup_global_declarations. */
11371 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
11372 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
11373 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
11375 timevar_stop (TV_PHASE_DEFERRED);
11376 timevar_start (TV_PHASE_PARSING);
11378 ext_block = NULL;
11381 /* Register reserved keyword WORD as qualifier for address space AS. */
11383 void
11384 c_register_addr_space (const char *word, addr_space_t as)
11386 int rid = RID_FIRST_ADDR_SPACE + as;
11387 tree id;
11389 /* Address space qualifiers are only supported
11390 in C with GNU extensions enabled. */
11391 if (c_dialect_objc () || flag_no_asm)
11392 return;
11394 id = get_identifier (word);
11395 C_SET_RID_CODE (id, rid);
11396 C_IS_RESERVED_WORD (id) = 1;
11397 ridpointers [rid] = id;
11400 /* Return identifier to look up for omp declare reduction. */
11402 tree
11403 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
11405 const char *p = NULL;
11406 switch (reduction_code)
11408 case PLUS_EXPR: p = "+"; break;
11409 case MULT_EXPR: p = "*"; break;
11410 case MINUS_EXPR: p = "-"; break;
11411 case BIT_AND_EXPR: p = "&"; break;
11412 case BIT_XOR_EXPR: p = "^"; break;
11413 case BIT_IOR_EXPR: p = "|"; break;
11414 case TRUTH_ANDIF_EXPR: p = "&&"; break;
11415 case TRUTH_ORIF_EXPR: p = "||"; break;
11416 case MIN_EXPR: p = "min"; break;
11417 case MAX_EXPR: p = "max"; break;
11418 default:
11419 break;
11422 if (p == NULL)
11424 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
11425 return error_mark_node;
11426 p = IDENTIFIER_POINTER (reduction_id);
11429 const char prefix[] = "omp declare reduction ";
11430 size_t lenp = sizeof (prefix);
11431 size_t len = strlen (p);
11432 char *name = XALLOCAVEC (char, lenp + len);
11433 memcpy (name, prefix, lenp - 1);
11434 memcpy (name + lenp - 1, p, len + 1);
11435 return get_identifier (name);
11438 /* Lookup REDUCTION_ID in the current scope, or create an artificial
11439 VAR_DECL, bind it into the current scope and return it. */
11441 tree
11442 c_omp_reduction_decl (tree reduction_id)
11444 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11445 if (b != NULL && B_IN_CURRENT_SCOPE (b))
11446 return b->decl;
11448 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
11449 reduction_id, integer_type_node);
11450 DECL_ARTIFICIAL (decl) = 1;
11451 DECL_EXTERNAL (decl) = 1;
11452 TREE_STATIC (decl) = 1;
11453 TREE_PUBLIC (decl) = 0;
11454 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
11455 return decl;
11458 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
11460 tree
11461 c_omp_reduction_lookup (tree reduction_id, tree type)
11463 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
11464 while (b)
11466 tree t;
11467 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
11468 if (comptypes (TREE_PURPOSE (t), type))
11469 return TREE_VALUE (t);
11470 b = b->shadowed;
11472 return error_mark_node;
11475 /* Helper function called via walk_tree, to diagnose invalid
11476 #pragma omp declare reduction combiners or initializers. */
11478 tree
11479 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
11481 tree *vars = (tree *) data;
11482 if (SSA_VAR_P (*tp)
11483 && !DECL_ARTIFICIAL (*tp)
11484 && *tp != vars[0]
11485 && *tp != vars[1])
11487 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
11488 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
11489 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
11490 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
11491 *tp);
11492 else
11493 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
11494 "to variable %qD which is not %<omp_priv%> nor "
11495 "%<omp_orig%>",
11496 *tp);
11497 return *tp;
11499 return NULL_TREE;
11502 #include "gt-c-c-decl.h"