2016-01-26 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / c / c-decl.c
blob1ec60420db9c758c9f32cdf874ece697c7dc2c8b
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Process declarations and symbol lookup for C front end.
21 Also constructs types; the standard scalar types at initialization,
22 and structure, union, array and enum types when they are declared. */
24 /* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "target.h"
31 #include "function.h"
32 #include "c-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "cgraph.h"
36 #include "intl.h"
37 #include "print-tree.h"
38 #include "stor-layout.h"
39 #include "varasm.h"
40 #include "attribs.h"
41 #include "toplev.h"
42 #include "debug.h"
43 #include "c-family/c-objc.h"
44 #include "c-family/c-pragma.h"
45 #include "c-family/c-ubsan.h"
46 #include "c-lang.h"
47 #include "langhooks.h"
48 #include "tree-iterator.h"
49 #include "dumpfile.h"
50 #include "plugin.h"
51 #include "c-family/c-ada-spec.h"
52 #include "cilk.h"
53 #include "builtins.h"
55 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
56 enum decl_context
57 { NORMAL, /* Ordinary declaration */
58 FUNCDEF, /* Function definition */
59 PARM, /* Declaration of parm before function body */
60 FIELD, /* Declaration inside struct or union */
61 TYPENAME}; /* Typename (inside cast or sizeof) */
63 /* States indicating how grokdeclarator() should handle declspecs marked
64 with __attribute__((deprecated)). An object declared as
65 __attribute__((deprecated)) suppresses warnings of uses of other
66 deprecated items. */
68 enum deprecated_states {
69 DEPRECATED_NORMAL,
70 DEPRECATED_SUPPRESS
74 /* Nonzero if we have seen an invalid cross reference
75 to a struct, union, or enum, but not yet printed the message. */
76 tree pending_invalid_xref;
78 /* File and line to appear in the eventual error message. */
79 location_t pending_invalid_xref_location;
81 /* The file and line that the prototype came from if this is an
82 old-style definition; used for diagnostics in
83 store_parm_decls_oldstyle. */
85 static location_t current_function_prototype_locus;
87 /* Whether this prototype was built-in. */
89 static bool current_function_prototype_built_in;
91 /* The argument type information of this prototype. */
93 static tree current_function_prototype_arg_types;
95 /* The argument information structure for the function currently being
96 defined. */
98 static struct c_arg_info *current_function_arg_info;
100 /* The obstack on which parser and related data structures, which are
101 not live beyond their top-level declaration or definition, are
102 allocated. */
103 struct obstack parser_obstack;
105 /* The current statement tree. */
107 static GTY(()) struct stmt_tree_s c_stmt_tree;
109 /* State saving variables. */
110 tree c_break_label;
111 tree c_cont_label;
113 /* A list of decls to be made automatically visible in each file scope. */
114 static GTY(()) tree visible_builtins;
116 /* Set to 0 at beginning of a function definition, set to 1 if
117 a return statement that specifies a return value is seen. */
119 int current_function_returns_value;
121 /* Set to 0 at beginning of a function definition, set to 1 if
122 a return statement with no argument is seen. */
124 int current_function_returns_null;
126 /* Set to 0 at beginning of a function definition, set to 1 if
127 a call to a noreturn function is seen. */
129 int current_function_returns_abnormally;
131 /* Set to nonzero by `grokdeclarator' for a function
132 whose return type is defaulted, if warnings for this are desired. */
134 static int warn_about_return_type;
136 /* Nonzero when the current toplevel function contains a declaration
137 of a nested function which is never defined. */
139 static bool undef_nested_function;
141 /* If non-zero, implicit "omp declare target" attribute is added into the
142 attribute lists. */
143 int current_omp_declare_target_attribute;
145 /* Each c_binding structure describes one binding of an identifier to
146 a decl. All the decls in a scope - irrespective of namespace - are
147 chained together by the ->prev field, which (as the name implies)
148 runs in reverse order. All the decls in a given namespace bound to
149 a given identifier are chained by the ->shadowed field, which runs
150 from inner to outer scopes.
152 The ->decl field usually points to a DECL node, but there are two
153 exceptions. In the namespace of type tags, the bound entity is a
154 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
155 identifier is encountered, it is bound to error_mark_node to
156 suppress further errors about that identifier in the current
157 function.
159 The ->u.type field stores the type of the declaration in this scope;
160 if NULL, the type is the type of the ->decl field. This is only of
161 relevance for objects with external or internal linkage which may
162 be redeclared in inner scopes, forming composite types that only
163 persist for the duration of those scopes. In the external scope,
164 this stores the composite of all the types declared for this
165 object, visible or not. The ->inner_comp field (used only at file
166 scope) stores whether an incomplete array type at file scope was
167 completed at an inner scope to an array size other than 1.
169 The ->u.label field is used for labels. It points to a structure
170 which stores additional information used for warnings.
172 The depth field is copied from the scope structure that holds this
173 decl. It is used to preserve the proper ordering of the ->shadowed
174 field (see bind()) and also for a handful of special-case checks.
175 Finally, the invisible bit is true for a decl which should be
176 ignored for purposes of normal name lookup, and the nested bit is
177 true for a decl that's been bound a second time in an inner scope;
178 in all such cases, the binding in the outer scope will have its
179 invisible bit true. */
181 struct GTY((chain_next ("%h.prev"))) c_binding {
182 union GTY(()) { /* first so GTY desc can use decl */
183 tree GTY((tag ("0"))) type; /* the type in this scope */
184 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
185 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
186 tree decl; /* the decl bound */
187 tree id; /* the identifier it's bound to */
188 struct c_binding *prev; /* the previous decl in this scope */
189 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
190 unsigned int depth : 28; /* depth of this scope */
191 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
192 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
193 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
194 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
195 location_t locus; /* location for nested bindings */
197 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
198 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
199 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
200 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
202 /* Each C symbol points to three linked lists of c_binding structures.
203 These describe the values of the identifier in the three different
204 namespaces defined by the language. */
206 struct GTY(()) lang_identifier {
207 struct c_common_identifier common_id;
208 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
209 struct c_binding *tag_binding; /* struct/union/enum tags */
210 struct c_binding *label_binding; /* labels */
213 /* Validate c-lang.c's assumptions. */
214 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
215 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
217 /* The binding oracle; see c-tree.h. */
218 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
220 /* This flag is set on an identifier if we have previously asked the
221 binding oracle for this identifier's symbol binding. */
222 #define I_SYMBOL_CHECKED(node) \
223 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
225 static inline struct c_binding* *
226 i_symbol_binding (tree node)
228 struct lang_identifier *lid
229 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
231 if (lid->symbol_binding == NULL
232 && c_binding_oracle != NULL
233 && !I_SYMBOL_CHECKED (node))
235 /* Set the "checked" flag first, to avoid infinite recursion
236 when the binding oracle calls back into gcc. */
237 I_SYMBOL_CHECKED (node) = 1;
238 c_binding_oracle (C_ORACLE_SYMBOL, node);
241 return &lid->symbol_binding;
244 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
246 #define I_SYMBOL_DECL(node) \
247 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
249 /* This flag is set on an identifier if we have previously asked the
250 binding oracle for this identifier's tag binding. */
251 #define I_TAG_CHECKED(node) \
252 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
254 static inline struct c_binding **
255 i_tag_binding (tree node)
257 struct lang_identifier *lid
258 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
260 if (lid->tag_binding == NULL
261 && c_binding_oracle != NULL
262 && !I_TAG_CHECKED (node))
264 /* Set the "checked" flag first, to avoid infinite recursion
265 when the binding oracle calls back into gcc. */
266 I_TAG_CHECKED (node) = 1;
267 c_binding_oracle (C_ORACLE_TAG, node);
270 return &lid->tag_binding;
273 #define I_TAG_BINDING(node) (*i_tag_binding (node))
275 #define I_TAG_DECL(node) \
276 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
278 /* This flag is set on an identifier if we have previously asked the
279 binding oracle for this identifier's label binding. */
280 #define I_LABEL_CHECKED(node) \
281 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
283 static inline struct c_binding **
284 i_label_binding (tree node)
286 struct lang_identifier *lid
287 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
289 if (lid->label_binding == NULL
290 && c_binding_oracle != NULL
291 && !I_LABEL_CHECKED (node))
293 /* Set the "checked" flag first, to avoid infinite recursion
294 when the binding oracle calls back into gcc. */
295 I_LABEL_CHECKED (node) = 1;
296 c_binding_oracle (C_ORACLE_LABEL, node);
299 return &lid->label_binding;
302 #define I_LABEL_BINDING(node) (*i_label_binding (node))
304 #define I_LABEL_DECL(node) \
305 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
307 /* The resulting tree type. */
309 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
310 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
312 union tree_node GTY ((tag ("0"),
313 desc ("tree_node_structure (&%h)")))
314 generic;
315 struct lang_identifier GTY ((tag ("1"))) identifier;
318 /* Track bindings and other things that matter for goto warnings. For
319 efficiency, we do not gather all the decls at the point of
320 definition. Instead, we point into the bindings structure. As
321 scopes are popped, we update these structures and gather the decls
322 that matter at that time. */
324 struct GTY(()) c_spot_bindings {
325 /* The currently open scope which holds bindings defined when the
326 label was defined or the goto statement was found. */
327 struct c_scope *scope;
328 /* The bindings in the scope field which were defined at the point
329 of the label or goto. This lets us look at older or newer
330 bindings in the scope, as appropriate. */
331 struct c_binding *bindings_in_scope;
332 /* The number of statement expressions that have started since this
333 label or goto statement was defined. This is zero if we are at
334 the same statement expression level. It is positive if we are in
335 a statement expression started since this spot. It is negative
336 if this spot was in a statement expression and we have left
337 it. */
338 int stmt_exprs;
339 /* Whether we started in a statement expression but are no longer in
340 it. This is set to true if stmt_exprs ever goes negative. */
341 bool left_stmt_expr;
344 /* This structure is used to keep track of bindings seen when a goto
345 statement is defined. This is only used if we see the goto
346 statement before we see the label. */
348 struct GTY(()) c_goto_bindings {
349 /* The location of the goto statement. */
350 location_t loc;
351 /* The bindings of the goto statement. */
352 struct c_spot_bindings goto_bindings;
355 typedef struct c_goto_bindings *c_goto_bindings_p;
357 /* The additional information we keep track of for a label binding.
358 These fields are updated as scopes are popped. */
360 struct GTY(()) c_label_vars {
361 /* The shadowed c_label_vars, when one label shadows another (which
362 can only happen using a __label__ declaration). */
363 struct c_label_vars *shadowed;
364 /* The bindings when the label was defined. */
365 struct c_spot_bindings label_bindings;
366 /* A list of decls that we care about: decls about which we should
367 warn if a goto branches to this label from later in the function.
368 Decls are added to this list as scopes are popped. We only add
369 the decls that matter. */
370 vec<tree, va_gc> *decls_in_scope;
371 /* A list of goto statements to this label. This is only used for
372 goto statements seen before the label was defined, so that we can
373 issue appropriate warnings for them. */
374 vec<c_goto_bindings_p, va_gc> *gotos;
377 /* Each c_scope structure describes the complete contents of one
378 scope. Four scopes are distinguished specially: the innermost or
379 current scope, the innermost function scope, the file scope (always
380 the second to outermost) and the outermost or external scope.
382 Most declarations are recorded in the current scope.
384 All normal label declarations are recorded in the innermost
385 function scope, as are bindings of undeclared identifiers to
386 error_mark_node. (GCC permits nested functions as an extension,
387 hence the 'innermost' qualifier.) Explicitly declared labels
388 (using the __label__ extension) appear in the current scope.
390 Being in the file scope (current_scope == file_scope) causes
391 special behavior in several places below. Also, under some
392 conditions the Objective-C front end records declarations in the
393 file scope even though that isn't the current scope.
395 All declarations with external linkage are recorded in the external
396 scope, even if they aren't visible there; this models the fact that
397 such declarations are visible to the entire program, and (with a
398 bit of cleverness, see pushdecl) allows diagnosis of some violations
399 of C99 6.2.2p7 and 6.2.7p2:
401 If, within the same translation unit, the same identifier appears
402 with both internal and external linkage, the behavior is
403 undefined.
405 All declarations that refer to the same object or function shall
406 have compatible type; otherwise, the behavior is undefined.
408 Initially only the built-in declarations, which describe compiler
409 intrinsic functions plus a subset of the standard library, are in
410 this scope.
412 The order of the blocks list matters, and it is frequently appended
413 to. To avoid having to walk all the way to the end of the list on
414 each insertion, or reverse the list later, we maintain a pointer to
415 the last list entry. (FIXME: It should be feasible to use a reversed
416 list here.)
418 The bindings list is strictly in reverse order of declarations;
419 pop_scope relies on this. */
422 struct GTY((chain_next ("%h.outer"))) c_scope {
423 /* The scope containing this one. */
424 struct c_scope *outer;
426 /* The next outermost function scope. */
427 struct c_scope *outer_function;
429 /* All bindings in this scope. */
430 struct c_binding *bindings;
432 /* For each scope (except the global one), a chain of BLOCK nodes
433 for all the scopes that were entered and exited one level down. */
434 tree blocks;
435 tree blocks_last;
437 /* The depth of this scope. Used to keep the ->shadowed chain of
438 bindings sorted innermost to outermost. */
439 unsigned int depth : 28;
441 /* True if we are currently filling this scope with parameter
442 declarations. */
443 BOOL_BITFIELD parm_flag : 1;
445 /* True if we saw [*] in this scope. Used to give an error messages
446 if these appears in a function definition. */
447 BOOL_BITFIELD had_vla_unspec : 1;
449 /* True if we already complained about forward parameter decls
450 in this scope. This prevents double warnings on
451 foo (int a; int b; ...) */
452 BOOL_BITFIELD warned_forward_parm_decls : 1;
454 /* True if this is the outermost block scope of a function body.
455 This scope contains the parameters, the local variables declared
456 in the outermost block, and all the labels (except those in
457 nested functions, or declared at block scope with __label__). */
458 BOOL_BITFIELD function_body : 1;
460 /* True means make a BLOCK for this scope no matter what. */
461 BOOL_BITFIELD keep : 1;
463 /* True means that an unsuffixed float constant is _Decimal64. */
464 BOOL_BITFIELD float_const_decimal64 : 1;
466 /* True if this scope has any label bindings. This is used to speed
467 up searching for labels when popping scopes, particularly since
468 labels are normally only found at function scope. */
469 BOOL_BITFIELD has_label_bindings : 1;
471 /* True if we should issue a warning if a goto statement crosses any
472 of the bindings. We still need to check the list of bindings to
473 find the specific ones we need to warn about. This is true if
474 decl_jump_unsafe would return true for any of the bindings. This
475 is used to avoid looping over all the bindings unnecessarily. */
476 BOOL_BITFIELD has_jump_unsafe_decl : 1;
479 /* The scope currently in effect. */
481 static GTY(()) struct c_scope *current_scope;
483 /* The innermost function scope. Ordinary (not explicitly declared)
484 labels, bindings to error_mark_node, and the lazily-created
485 bindings of __func__ and its friends get this scope. */
487 static GTY(()) struct c_scope *current_function_scope;
489 /* The C file scope. This is reset for each input translation unit. */
491 static GTY(()) struct c_scope *file_scope;
493 /* The outermost scope. This is used for all declarations with
494 external linkage, and only these, hence the name. */
496 static GTY(()) struct c_scope *external_scope;
498 /* A chain of c_scope structures awaiting reuse. */
500 static GTY((deletable)) struct c_scope *scope_freelist;
502 /* A chain of c_binding structures awaiting reuse. */
504 static GTY((deletable)) struct c_binding *binding_freelist;
506 /* Append VAR to LIST in scope SCOPE. */
507 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
508 struct c_scope *s_ = (scope); \
509 tree d_ = (decl); \
510 if (s_->list##_last) \
511 BLOCK_CHAIN (s_->list##_last) = d_; \
512 else \
513 s_->list = d_; \
514 s_->list##_last = d_; \
515 } while (0)
517 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
518 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
519 struct c_scope *t_ = (tscope); \
520 struct c_scope *f_ = (fscope); \
521 if (t_->to##_last) \
522 BLOCK_CHAIN (t_->to##_last) = f_->from; \
523 else \
524 t_->to = f_->from; \
525 t_->to##_last = f_->from##_last; \
526 } while (0)
528 /* A c_inline_static structure stores details of a static identifier
529 referenced in a definition of a function that may be an inline
530 definition if no subsequent declaration of that function uses
531 "extern" or does not use "inline". */
533 struct GTY((chain_next ("%h.next"))) c_inline_static {
534 /* The location for a diagnostic. */
535 location_t location;
537 /* The function that may be an inline definition. */
538 tree function;
540 /* The object or function referenced. */
541 tree static_decl;
543 /* What sort of reference this is. */
544 enum c_inline_static_type type;
546 /* The next such structure or NULL. */
547 struct c_inline_static *next;
550 /* List of static identifiers used or referenced in functions that may
551 be inline definitions. */
552 static GTY(()) struct c_inline_static *c_inline_statics;
554 /* True means unconditionally make a BLOCK for the next scope pushed. */
556 static bool keep_next_level_flag;
558 /* True means the next call to push_scope will be the outermost scope
559 of a function body, so do not push a new scope, merely cease
560 expecting parameter decls. */
562 static bool next_is_function_body;
564 /* A vector of pointers to c_binding structures. */
566 typedef struct c_binding *c_binding_ptr;
568 /* Information that we keep for a struct or union while it is being
569 parsed. */
571 struct c_struct_parse_info
573 /* If warn_cxx_compat, a list of types defined within this
574 struct. */
575 vec<tree> struct_types;
576 /* If warn_cxx_compat, a list of field names which have bindings,
577 and which are defined in this struct, but which are not defined
578 in any enclosing struct. This is used to clear the in_struct
579 field of the c_bindings structure. */
580 vec<c_binding_ptr> fields;
581 /* If warn_cxx_compat, a list of typedef names used when defining
582 fields in this struct. */
583 vec<tree> typedefs_seen;
586 /* Information for the struct or union currently being parsed, or
587 NULL if not parsing a struct or union. */
588 static struct c_struct_parse_info *struct_parse_info;
590 /* Forward declarations. */
591 static tree lookup_name_in_scope (tree, struct c_scope *);
592 static tree c_make_fname_decl (location_t, tree, int);
593 static tree grokdeclarator (const struct c_declarator *,
594 struct c_declspecs *,
595 enum decl_context, bool, tree *, tree *, tree *,
596 bool *, enum deprecated_states);
597 static tree grokparms (struct c_arg_info *, bool);
598 static void layout_array_type (tree);
599 static void warn_defaults_to (location_t, int, const char *, ...)
600 ATTRIBUTE_GCC_DIAG(3,4);
602 /* T is a statement. Add it to the statement-tree. This is the
603 C/ObjC version--C++ has a slightly different version of this
604 function. */
606 tree
607 add_stmt (tree t)
609 enum tree_code code = TREE_CODE (t);
611 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
613 if (!EXPR_HAS_LOCATION (t))
614 SET_EXPR_LOCATION (t, input_location);
617 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
618 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
620 /* Add T to the statement-tree. Non-side-effect statements need to be
621 recorded during statement expressions. */
622 if (!building_stmt_list_p ())
623 push_stmt_list ();
624 append_to_statement_list_force (t, &cur_stmt_list);
626 return t;
629 /* Build a pointer type using the default pointer mode. */
631 static tree
632 c_build_pointer_type (tree to_type)
634 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
635 : TYPE_ADDR_SPACE (to_type);
636 machine_mode pointer_mode;
638 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
639 pointer_mode = targetm.addr_space.pointer_mode (as);
640 else
641 pointer_mode = c_default_pointer_mode;
642 return build_pointer_type_for_mode (to_type, pointer_mode, false);
646 /* Return true if we will want to say something if a goto statement
647 crosses DECL. */
649 static bool
650 decl_jump_unsafe (tree decl)
652 if (error_operand_p (decl))
653 return false;
655 /* Always warn about crossing variably modified types. */
656 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
657 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
658 return true;
660 /* Otherwise, only warn if -Wgoto-misses-init and this is an
661 initialized automatic decl. */
662 if (warn_jump_misses_init
663 && VAR_P (decl)
664 && !TREE_STATIC (decl)
665 && DECL_INITIAL (decl) != NULL_TREE)
666 return true;
668 return false;
672 void
673 c_print_identifier (FILE *file, tree node, int indent)
675 void (*save) (enum c_oracle_request, tree identifier);
677 /* Temporarily hide any binding oracle. Without this, calls to
678 debug_tree from the debugger will end up calling into the oracle,
679 making for a confusing debug session. As the oracle isn't needed
680 here for normal operation, it's simplest to suppress it. */
681 save = c_binding_oracle;
682 c_binding_oracle = NULL;
684 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
685 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
686 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
687 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
689 tree rid = ridpointers[C_RID_CODE (node)];
690 indent_to (file, indent + 4);
691 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
692 (void *) rid, IDENTIFIER_POINTER (rid));
695 c_binding_oracle = save;
698 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
699 which may be any of several kinds of DECL or TYPE or error_mark_node,
700 in the scope SCOPE. */
701 static void
702 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
703 bool nested, location_t locus)
705 struct c_binding *b, **here;
707 if (binding_freelist)
709 b = binding_freelist;
710 binding_freelist = b->prev;
712 else
713 b = ggc_alloc<c_binding> ();
715 b->shadowed = 0;
716 b->decl = decl;
717 b->id = name;
718 b->depth = scope->depth;
719 b->invisible = invisible;
720 b->nested = nested;
721 b->inner_comp = 0;
722 b->in_struct = 0;
723 b->locus = locus;
725 b->u.type = NULL;
727 b->prev = scope->bindings;
728 scope->bindings = b;
730 if (decl_jump_unsafe (decl))
731 scope->has_jump_unsafe_decl = 1;
733 if (!name)
734 return;
736 switch (TREE_CODE (decl))
738 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
739 case ENUMERAL_TYPE:
740 case UNION_TYPE:
741 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
742 case VAR_DECL:
743 case FUNCTION_DECL:
744 case TYPE_DECL:
745 case CONST_DECL:
746 case PARM_DECL:
747 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
749 default:
750 gcc_unreachable ();
753 /* Locate the appropriate place in the chain of shadowed decls
754 to insert this binding. Normally, scope == current_scope and
755 this does nothing. */
756 while (*here && (*here)->depth > scope->depth)
757 here = &(*here)->shadowed;
759 b->shadowed = *here;
760 *here = b;
763 /* Clear the binding structure B, stick it on the binding_freelist,
764 and return the former value of b->prev. This is used by pop_scope
765 and get_parm_info to iterate destructively over all the bindings
766 from a given scope. */
767 static struct c_binding *
768 free_binding_and_advance (struct c_binding *b)
770 struct c_binding *prev = b->prev;
772 memset (b, 0, sizeof (struct c_binding));
773 b->prev = binding_freelist;
774 binding_freelist = b;
776 return prev;
779 /* Bind a label. Like bind, but skip fields which aren't used for
780 labels, and add the LABEL_VARS value. */
781 static void
782 bind_label (tree name, tree label, struct c_scope *scope,
783 struct c_label_vars *label_vars)
785 struct c_binding *b;
787 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
788 UNKNOWN_LOCATION);
790 scope->has_label_bindings = true;
792 b = scope->bindings;
793 gcc_assert (b->decl == label);
794 label_vars->shadowed = b->u.label;
795 b->u.label = label_vars;
798 /* Hook called at end of compilation to assume 1 elt
799 for a file-scope tentative array defn that wasn't complete before. */
801 void
802 c_finish_incomplete_decl (tree decl)
804 if (VAR_P (decl))
806 tree type = TREE_TYPE (decl);
807 if (type != error_mark_node
808 && TREE_CODE (type) == ARRAY_TYPE
809 && !DECL_EXTERNAL (decl)
810 && TYPE_DOMAIN (type) == 0)
812 warning_at (DECL_SOURCE_LOCATION (decl),
813 0, "array %q+D assumed to have one element", decl);
815 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
817 relayout_decl (decl);
822 /* Record that inline function FUNC contains a reference (location
823 LOC) to static DECL (file-scope or function-local according to
824 TYPE). */
826 void
827 record_inline_static (location_t loc, tree func, tree decl,
828 enum c_inline_static_type type)
830 c_inline_static *csi = ggc_alloc<c_inline_static> ();
831 csi->location = loc;
832 csi->function = func;
833 csi->static_decl = decl;
834 csi->type = type;
835 csi->next = c_inline_statics;
836 c_inline_statics = csi;
839 /* Check for references to static declarations in inline functions at
840 the end of the translation unit and diagnose them if the functions
841 are still inline definitions. */
843 static void
844 check_inline_statics (void)
846 struct c_inline_static *csi;
847 for (csi = c_inline_statics; csi; csi = csi->next)
849 if (DECL_EXTERNAL (csi->function))
850 switch (csi->type)
852 case csi_internal:
853 pedwarn (csi->location, 0,
854 "%qD is static but used in inline function %qD "
855 "which is not static", csi->static_decl, csi->function);
856 break;
857 case csi_modifiable:
858 pedwarn (csi->location, 0,
859 "%q+D is static but declared in inline function %qD "
860 "which is not static", csi->static_decl, csi->function);
861 break;
862 default:
863 gcc_unreachable ();
866 c_inline_statics = NULL;
869 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
870 for the current state, otherwise set it to uninitialized. */
872 static void
873 set_spot_bindings (struct c_spot_bindings *p, bool defining)
875 if (defining)
877 p->scope = current_scope;
878 p->bindings_in_scope = current_scope->bindings;
880 else
882 p->scope = NULL;
883 p->bindings_in_scope = NULL;
885 p->stmt_exprs = 0;
886 p->left_stmt_expr = false;
889 /* Update spot bindings P as we pop out of SCOPE. Return true if we
890 should push decls for a label. */
892 static bool
893 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
895 if (p->scope != scope)
897 /* This label or goto is defined in some other scope, or it is a
898 label which is not yet defined. There is nothing to
899 update. */
900 return false;
903 /* Adjust the spot bindings to refer to the bindings already defined
904 in the enclosing scope. */
905 p->scope = scope->outer;
906 p->bindings_in_scope = p->scope->bindings;
908 return true;
911 /* The Objective-C front-end often needs to determine the current scope. */
913 void *
914 objc_get_current_scope (void)
916 return current_scope;
919 /* The following function is used only by Objective-C. It needs to live here
920 because it accesses the innards of c_scope. */
922 void
923 objc_mark_locals_volatile (void *enclosing_blk)
925 struct c_scope *scope;
926 struct c_binding *b;
928 for (scope = current_scope;
929 scope && scope != enclosing_blk;
930 scope = scope->outer)
932 for (b = scope->bindings; b; b = b->prev)
933 objc_volatilize_decl (b->decl);
935 /* Do not climb up past the current function. */
936 if (scope->function_body)
937 break;
941 /* Return true if we are in the global binding level. */
943 bool
944 global_bindings_p (void)
946 return current_scope == file_scope;
949 void
950 keep_next_level (void)
952 keep_next_level_flag = true;
955 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
957 void
958 set_float_const_decimal64 (void)
960 current_scope->float_const_decimal64 = true;
963 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
965 void
966 clear_float_const_decimal64 (void)
968 current_scope->float_const_decimal64 = false;
971 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
973 bool
974 float_const_decimal64_p (void)
976 return current_scope->float_const_decimal64;
979 /* Identify this scope as currently being filled with parameters. */
981 void
982 declare_parm_level (void)
984 current_scope->parm_flag = true;
987 void
988 push_scope (void)
990 if (next_is_function_body)
992 /* This is the transition from the parameters to the top level
993 of the function body. These are the same scope
994 (C99 6.2.1p4,6) so we do not push another scope structure.
995 next_is_function_body is set only by store_parm_decls, which
996 in turn is called when and only when we are about to
997 encounter the opening curly brace for the function body.
999 The outermost block of a function always gets a BLOCK node,
1000 because the debugging output routines expect that each
1001 function has at least one BLOCK. */
1002 current_scope->parm_flag = false;
1003 current_scope->function_body = true;
1004 current_scope->keep = true;
1005 current_scope->outer_function = current_function_scope;
1006 current_function_scope = current_scope;
1008 keep_next_level_flag = false;
1009 next_is_function_body = false;
1011 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1012 if (current_scope->outer)
1013 current_scope->float_const_decimal64
1014 = current_scope->outer->float_const_decimal64;
1015 else
1016 current_scope->float_const_decimal64 = false;
1018 else
1020 struct c_scope *scope;
1021 if (scope_freelist)
1023 scope = scope_freelist;
1024 scope_freelist = scope->outer;
1026 else
1027 scope = ggc_cleared_alloc<c_scope> ();
1029 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1030 if (current_scope)
1031 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1032 else
1033 scope->float_const_decimal64 = false;
1035 scope->keep = keep_next_level_flag;
1036 scope->outer = current_scope;
1037 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1039 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1040 possible. */
1041 if (current_scope && scope->depth == 0)
1043 scope->depth--;
1044 sorry ("GCC supports only %u nested scopes", scope->depth);
1047 current_scope = scope;
1048 keep_next_level_flag = false;
1052 /* This is called when we are leaving SCOPE. For each label defined
1053 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1054 These are the decls whose initialization will be skipped by a goto
1055 later in the function. */
1057 static void
1058 update_label_decls (struct c_scope *scope)
1060 struct c_scope *s;
1062 s = scope;
1063 while (s != NULL)
1065 if (s->has_label_bindings)
1067 struct c_binding *b;
1069 for (b = s->bindings; b != NULL; b = b->prev)
1071 struct c_label_vars *label_vars;
1072 struct c_binding *b1;
1073 bool hjud;
1074 unsigned int ix;
1075 struct c_goto_bindings *g;
1077 if (TREE_CODE (b->decl) != LABEL_DECL)
1078 continue;
1079 label_vars = b->u.label;
1081 b1 = label_vars->label_bindings.bindings_in_scope;
1082 if (label_vars->label_bindings.scope == NULL)
1083 hjud = false;
1084 else
1085 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1086 if (update_spot_bindings (scope, &label_vars->label_bindings))
1088 /* This label is defined in this scope. */
1089 if (hjud)
1091 for (; b1 != NULL; b1 = b1->prev)
1093 /* A goto from later in the function to this
1094 label will never see the initialization
1095 of B1, if any. Save it to issue a
1096 warning if needed. */
1097 if (decl_jump_unsafe (b1->decl))
1098 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1103 /* Update the bindings of any goto statements associated
1104 with this label. */
1105 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1106 update_spot_bindings (scope, &g->goto_bindings);
1110 /* Don't search beyond the current function. */
1111 if (s == current_function_scope)
1112 break;
1114 s = s->outer;
1118 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1120 static void
1121 set_type_context (tree type, tree context)
1123 for (type = TYPE_MAIN_VARIANT (type); type;
1124 type = TYPE_NEXT_VARIANT (type))
1125 TYPE_CONTEXT (type) = context;
1128 /* Exit a scope. Restore the state of the identifier-decl mappings
1129 that were in effect when this scope was entered. Return a BLOCK
1130 node containing all the DECLs in this scope that are of interest
1131 to debug info generation. */
1133 tree
1134 pop_scope (void)
1136 struct c_scope *scope = current_scope;
1137 tree block, context, p;
1138 struct c_binding *b;
1140 bool functionbody = scope->function_body;
1141 bool keep = functionbody || scope->keep || scope->bindings;
1143 update_label_decls (scope);
1145 /* If appropriate, create a BLOCK to record the decls for the life
1146 of this function. */
1147 block = 0;
1148 if (keep)
1150 block = make_node (BLOCK);
1151 BLOCK_SUBBLOCKS (block) = scope->blocks;
1152 TREE_USED (block) = 1;
1154 /* In each subblock, record that this is its superior. */
1155 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1156 BLOCK_SUPERCONTEXT (p) = block;
1158 BLOCK_VARS (block) = 0;
1161 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1162 scope must be set so that they point to the appropriate
1163 construct, i.e. either to the current FUNCTION_DECL node, or
1164 else to the BLOCK node we just constructed.
1166 Note that for tagged types whose scope is just the formal
1167 parameter list for some function type specification, we can't
1168 properly set their TYPE_CONTEXTs here, because we don't have a
1169 pointer to the appropriate FUNCTION_TYPE node readily available
1170 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1171 type nodes get set in `grokdeclarator' as soon as we have created
1172 the FUNCTION_TYPE node which will represent the "scope" for these
1173 "parameter list local" tagged types. */
1174 if (scope->function_body)
1175 context = current_function_decl;
1176 else if (scope == file_scope)
1178 tree file_decl = build_translation_unit_decl (NULL_TREE);
1179 context = file_decl;
1180 debug_hooks->register_main_translation_unit (file_decl);
1182 else
1183 context = block;
1185 /* Clear all bindings in this scope. */
1186 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1188 p = b->decl;
1189 switch (TREE_CODE (p))
1191 case LABEL_DECL:
1192 /* Warnings for unused labels, errors for undefined labels. */
1193 if (TREE_USED (p) && !DECL_INITIAL (p))
1195 error ("label %q+D used but not defined", p);
1196 DECL_INITIAL (p) = error_mark_node;
1198 else
1199 warn_for_unused_label (p);
1201 /* Labels go in BLOCK_VARS. */
1202 DECL_CHAIN (p) = BLOCK_VARS (block);
1203 BLOCK_VARS (block) = p;
1204 gcc_assert (I_LABEL_BINDING (b->id) == b);
1205 I_LABEL_BINDING (b->id) = b->shadowed;
1207 /* Also pop back to the shadowed label_vars. */
1208 release_tree_vector (b->u.label->decls_in_scope);
1209 b->u.label = b->u.label->shadowed;
1210 break;
1212 case ENUMERAL_TYPE:
1213 case UNION_TYPE:
1214 case RECORD_TYPE:
1215 set_type_context (p, context);
1217 /* Types may not have tag-names, in which case the type
1218 appears in the bindings list with b->id NULL. */
1219 if (b->id)
1221 gcc_assert (I_TAG_BINDING (b->id) == b);
1222 I_TAG_BINDING (b->id) = b->shadowed;
1224 break;
1226 case FUNCTION_DECL:
1227 /* Propagate TREE_ADDRESSABLE from nested functions to their
1228 containing functions. */
1229 if (!TREE_ASM_WRITTEN (p)
1230 && DECL_INITIAL (p) != 0
1231 && TREE_ADDRESSABLE (p)
1232 && DECL_ABSTRACT_ORIGIN (p) != 0
1233 && DECL_ABSTRACT_ORIGIN (p) != p)
1234 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1235 if (!DECL_EXTERNAL (p)
1236 && !DECL_INITIAL (p)
1237 && scope != file_scope
1238 && scope != external_scope)
1240 error ("nested function %q+D declared but never defined", p);
1241 undef_nested_function = true;
1243 else if (DECL_DECLARED_INLINE_P (p)
1244 && TREE_PUBLIC (p)
1245 && !DECL_INITIAL (p))
1247 /* C99 6.7.4p6: "a function with external linkage... declared
1248 with an inline function specifier ... shall also be defined
1249 in the same translation unit." */
1250 if (!flag_gnu89_inline
1251 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1252 && scope != external_scope)
1253 pedwarn (input_location, 0,
1254 "inline function %q+D declared but never defined", p);
1255 DECL_EXTERNAL (p) = 1;
1258 goto common_symbol;
1260 case VAR_DECL:
1261 /* Warnings for unused variables. */
1262 if ((!TREE_USED (p) || !DECL_READ_P (p))
1263 && !TREE_NO_WARNING (p)
1264 && !DECL_IN_SYSTEM_HEADER (p)
1265 && DECL_NAME (p)
1266 && !DECL_ARTIFICIAL (p)
1267 && scope != file_scope
1268 && scope != external_scope)
1270 if (!TREE_USED (p))
1271 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1272 else if (DECL_CONTEXT (p) == current_function_decl)
1273 warning_at (DECL_SOURCE_LOCATION (p),
1274 OPT_Wunused_but_set_variable,
1275 "variable %qD set but not used", p);
1278 if (b->inner_comp)
1280 error ("type of array %q+D completed incompatibly with"
1281 " implicit initialization", p);
1284 /* Fall through. */
1285 case TYPE_DECL:
1286 case CONST_DECL:
1287 common_symbol:
1288 /* All of these go in BLOCK_VARS, but only if this is the
1289 binding in the home scope. */
1290 if (!b->nested)
1292 DECL_CHAIN (p) = BLOCK_VARS (block);
1293 BLOCK_VARS (block) = p;
1295 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1297 /* For block local externs add a special
1298 DECL_EXTERNAL decl for debug info generation. */
1299 tree extp = copy_node (p);
1301 DECL_EXTERNAL (extp) = 1;
1302 TREE_STATIC (extp) = 0;
1303 TREE_PUBLIC (extp) = 1;
1304 DECL_INITIAL (extp) = NULL_TREE;
1305 DECL_LANG_SPECIFIC (extp) = NULL;
1306 DECL_CONTEXT (extp) = current_function_decl;
1307 if (TREE_CODE (p) == FUNCTION_DECL)
1309 DECL_RESULT (extp) = NULL_TREE;
1310 DECL_SAVED_TREE (extp) = NULL_TREE;
1311 DECL_STRUCT_FUNCTION (extp) = NULL;
1313 if (b->locus != UNKNOWN_LOCATION)
1314 DECL_SOURCE_LOCATION (extp) = b->locus;
1315 DECL_CHAIN (extp) = BLOCK_VARS (block);
1316 BLOCK_VARS (block) = extp;
1318 /* If this is the file scope set DECL_CONTEXT of each decl to
1319 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1320 work. */
1321 if (scope == file_scope)
1323 DECL_CONTEXT (p) = context;
1324 if (TREE_CODE (p) == TYPE_DECL
1325 && TREE_TYPE (p) != error_mark_node)
1326 set_type_context (TREE_TYPE (p), context);
1329 /* Fall through. */
1330 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1331 already been put there by store_parm_decls. Unused-
1332 parameter warnings are handled by function.c.
1333 error_mark_node obviously does not go in BLOCK_VARS and
1334 does not get unused-variable warnings. */
1335 case PARM_DECL:
1336 case ERROR_MARK:
1337 /* It is possible for a decl not to have a name. We get
1338 here with b->id NULL in this case. */
1339 if (b->id)
1341 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1342 I_SYMBOL_BINDING (b->id) = b->shadowed;
1343 if (b->shadowed && b->shadowed->u.type)
1344 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1346 break;
1348 default:
1349 gcc_unreachable ();
1354 /* Dispose of the block that we just made inside some higher level. */
1355 if ((scope->function_body || scope == file_scope) && context)
1357 DECL_INITIAL (context) = block;
1358 BLOCK_SUPERCONTEXT (block) = context;
1360 else if (scope->outer)
1362 if (block)
1363 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1364 /* If we did not make a block for the scope just exited, any
1365 blocks made for inner scopes must be carried forward so they
1366 will later become subblocks of something else. */
1367 else if (scope->blocks)
1368 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1371 /* Pop the current scope, and free the structure for reuse. */
1372 current_scope = scope->outer;
1373 if (scope->function_body)
1374 current_function_scope = scope->outer_function;
1376 memset (scope, 0, sizeof (struct c_scope));
1377 scope->outer = scope_freelist;
1378 scope_freelist = scope;
1380 return block;
1383 void
1384 push_file_scope (void)
1386 tree decl;
1388 if (file_scope)
1389 return;
1391 push_scope ();
1392 file_scope = current_scope;
1394 start_fname_decls ();
1396 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1397 bind (DECL_NAME (decl), decl, file_scope,
1398 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1401 void
1402 pop_file_scope (void)
1404 /* In case there were missing closebraces, get us back to the global
1405 binding level. */
1406 while (current_scope != file_scope)
1407 pop_scope ();
1409 /* __FUNCTION__ is defined at file scope (""). This
1410 call may not be necessary as my tests indicate it
1411 still works without it. */
1412 finish_fname_decls ();
1414 check_inline_statics ();
1416 /* This is the point to write out a PCH if we're doing that.
1417 In that case we do not want to do anything else. */
1418 if (pch_file)
1420 c_common_write_pch ();
1421 return;
1424 /* Pop off the file scope and close this translation unit. */
1425 pop_scope ();
1426 file_scope = 0;
1428 maybe_apply_pending_pragma_weaks ();
1431 /* Adjust the bindings for the start of a statement expression. */
1433 void
1434 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1436 struct c_scope *scope;
1438 for (scope = current_scope; scope != NULL; scope = scope->outer)
1440 struct c_binding *b;
1442 if (!scope->has_label_bindings)
1443 continue;
1445 for (b = scope->bindings; b != NULL; b = b->prev)
1447 struct c_label_vars *label_vars;
1448 unsigned int ix;
1449 struct c_goto_bindings *g;
1451 if (TREE_CODE (b->decl) != LABEL_DECL)
1452 continue;
1453 label_vars = b->u.label;
1454 ++label_vars->label_bindings.stmt_exprs;
1455 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1456 ++g->goto_bindings.stmt_exprs;
1460 if (switch_bindings != NULL)
1461 ++switch_bindings->stmt_exprs;
1464 /* Adjust the bindings for the end of a statement expression. */
1466 void
1467 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1469 struct c_scope *scope;
1471 for (scope = current_scope; scope != NULL; scope = scope->outer)
1473 struct c_binding *b;
1475 if (!scope->has_label_bindings)
1476 continue;
1478 for (b = scope->bindings; b != NULL; b = b->prev)
1480 struct c_label_vars *label_vars;
1481 unsigned int ix;
1482 struct c_goto_bindings *g;
1484 if (TREE_CODE (b->decl) != LABEL_DECL)
1485 continue;
1486 label_vars = b->u.label;
1487 --label_vars->label_bindings.stmt_exprs;
1488 if (label_vars->label_bindings.stmt_exprs < 0)
1490 label_vars->label_bindings.left_stmt_expr = true;
1491 label_vars->label_bindings.stmt_exprs = 0;
1493 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1495 --g->goto_bindings.stmt_exprs;
1496 if (g->goto_bindings.stmt_exprs < 0)
1498 g->goto_bindings.left_stmt_expr = true;
1499 g->goto_bindings.stmt_exprs = 0;
1505 if (switch_bindings != NULL)
1507 --switch_bindings->stmt_exprs;
1508 gcc_assert (switch_bindings->stmt_exprs >= 0);
1512 /* Push a definition or a declaration of struct, union or enum tag "name".
1513 "type" should be the type node.
1514 We assume that the tag "name" is not already defined, and has a location
1515 of LOC.
1517 Note that the definition may really be just a forward reference.
1518 In that case, the TYPE_SIZE will be zero. */
1520 static void
1521 pushtag (location_t loc, tree name, tree type)
1523 /* Record the identifier as the type's name if it has none. */
1524 if (name && !TYPE_NAME (type))
1525 TYPE_NAME (type) = name;
1526 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1528 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1529 tagged type we just added to the current scope. This fake
1530 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1531 to output a representation of a tagged type, and it also gives
1532 us a convenient place to record the "scope start" address for the
1533 tagged type. */
1535 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1536 TYPE_DECL, NULL_TREE, type));
1538 /* An approximation for now, so we can tell this is a function-scope tag.
1539 This will be updated in pop_scope. */
1540 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1542 if (warn_cxx_compat && name != NULL_TREE)
1544 struct c_binding *b = I_SYMBOL_BINDING (name);
1546 if (b != NULL
1547 && b->decl != NULL_TREE
1548 && TREE_CODE (b->decl) == TYPE_DECL
1549 && (B_IN_CURRENT_SCOPE (b)
1550 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1551 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1552 != TYPE_MAIN_VARIANT (type)))
1554 warning_at (loc, OPT_Wc___compat,
1555 ("using %qD as both a typedef and a tag is "
1556 "invalid in C++"),
1557 b->decl);
1558 if (b->locus != UNKNOWN_LOCATION)
1559 inform (b->locus, "originally defined here");
1564 /* An exported interface to pushtag. This is used by the gdb plugin's
1565 binding oracle to introduce a new tag binding. */
1567 void
1568 c_pushtag (location_t loc, tree name, tree type)
1570 pushtag (loc, name, type);
1573 /* An exported interface to bind a declaration. LOC is the location
1574 to use. DECL is the declaration to bind. The decl's name is used
1575 to determine how it is bound. If DECL is a VAR_DECL, then
1576 IS_GLOBAL determines whether the decl is put into the global (file
1577 and external) scope or the current function's scope; if DECL is not
1578 a VAR_DECL then it is always put into the file scope. */
1580 void
1581 c_bind (location_t loc, tree decl, bool is_global)
1583 struct c_scope *scope;
1584 bool nested = false;
1586 if (!VAR_P (decl) || current_function_scope == NULL)
1588 /* Types and functions are always considered to be global. */
1589 scope = file_scope;
1590 DECL_EXTERNAL (decl) = 1;
1591 TREE_PUBLIC (decl) = 1;
1593 else if (is_global)
1595 /* Also bind it into the external scope. */
1596 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1597 nested = true;
1598 scope = file_scope;
1599 DECL_EXTERNAL (decl) = 1;
1600 TREE_PUBLIC (decl) = 1;
1602 else
1604 DECL_CONTEXT (decl) = current_function_decl;
1605 TREE_PUBLIC (decl) = 0;
1606 scope = current_function_scope;
1609 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1612 /* Subroutine of compare_decls. Allow harmless mismatches in return
1613 and argument types provided that the type modes match. This function
1614 return a unified type given a suitable match, and 0 otherwise. */
1616 static tree
1617 match_builtin_function_types (tree newtype, tree oldtype)
1619 tree newrettype, oldrettype;
1620 tree newargs, oldargs;
1621 tree trytype, tryargs;
1623 /* Accept the return type of the new declaration if same modes. */
1624 oldrettype = TREE_TYPE (oldtype);
1625 newrettype = TREE_TYPE (newtype);
1627 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1628 return 0;
1630 oldargs = TYPE_ARG_TYPES (oldtype);
1631 newargs = TYPE_ARG_TYPES (newtype);
1632 tryargs = newargs;
1634 while (oldargs || newargs)
1636 if (!oldargs
1637 || !newargs
1638 || !TREE_VALUE (oldargs)
1639 || !TREE_VALUE (newargs)
1640 || TYPE_MODE (TREE_VALUE (oldargs))
1641 != TYPE_MODE (TREE_VALUE (newargs)))
1642 return 0;
1644 oldargs = TREE_CHAIN (oldargs);
1645 newargs = TREE_CHAIN (newargs);
1648 trytype = build_function_type (newrettype, tryargs);
1650 /* Allow declaration to change transaction_safe attribute. */
1651 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1652 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1653 tree newattrs = TYPE_ATTRIBUTES (newtype);
1654 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1655 if (oldtsafe && !newtsafe)
1656 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1657 else if (newtsafe && !oldtsafe)
1658 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1659 NULL_TREE, oldattrs);
1661 return build_type_attribute_variant (trytype, oldattrs);
1664 /* Subroutine of diagnose_mismatched_decls. Check for function type
1665 mismatch involving an empty arglist vs a nonempty one and give clearer
1666 diagnostics. */
1667 static void
1668 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1669 tree newtype, tree oldtype)
1671 tree t;
1673 if (TREE_CODE (olddecl) != FUNCTION_DECL
1674 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1675 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1676 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1677 return;
1679 t = TYPE_ARG_TYPES (oldtype);
1680 if (t == 0)
1681 t = TYPE_ARG_TYPES (newtype);
1682 for (; t; t = TREE_CHAIN (t))
1684 tree type = TREE_VALUE (t);
1686 if (TREE_CHAIN (t) == 0
1687 && TYPE_MAIN_VARIANT (type) != void_type_node)
1689 inform (input_location, "a parameter list with an ellipsis can%'t match "
1690 "an empty parameter name list declaration");
1691 break;
1694 if (c_type_promotes_to (type) != type)
1696 inform (input_location, "an argument type that has a default promotion can%'t match "
1697 "an empty parameter name list declaration");
1698 break;
1703 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1704 old-style function definition, NEWDECL is a prototype declaration.
1705 Diagnose inconsistencies in the argument list. Returns TRUE if
1706 the prototype is compatible, FALSE if not. */
1707 static bool
1708 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1710 tree newargs, oldargs;
1711 int i;
1713 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1715 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1716 newargs = TYPE_ARG_TYPES (newtype);
1717 i = 1;
1719 for (;;)
1721 tree oldargtype = TREE_VALUE (oldargs);
1722 tree newargtype = TREE_VALUE (newargs);
1724 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1725 return false;
1727 oldargtype = (TYPE_ATOMIC (oldargtype)
1728 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1729 TYPE_QUAL_ATOMIC)
1730 : TYPE_MAIN_VARIANT (oldargtype));
1731 newargtype = (TYPE_ATOMIC (newargtype)
1732 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1733 TYPE_QUAL_ATOMIC)
1734 : TYPE_MAIN_VARIANT (newargtype));
1736 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1737 break;
1739 /* Reaching the end of just one list means the two decls don't
1740 agree on the number of arguments. */
1741 if (END_OF_ARGLIST (oldargtype))
1743 error ("prototype for %q+D declares more arguments "
1744 "than previous old-style definition", newdecl);
1745 return false;
1747 else if (END_OF_ARGLIST (newargtype))
1749 error ("prototype for %q+D declares fewer arguments "
1750 "than previous old-style definition", newdecl);
1751 return false;
1754 /* Type for passing arg must be consistent with that declared
1755 for the arg. */
1756 else if (!comptypes (oldargtype, newargtype))
1758 error ("prototype for %q+D declares argument %d"
1759 " with incompatible type",
1760 newdecl, i);
1761 return false;
1764 oldargs = TREE_CHAIN (oldargs);
1765 newargs = TREE_CHAIN (newargs);
1766 i++;
1769 /* If we get here, no errors were found, but do issue a warning
1770 for this poor-style construct. */
1771 warning (0, "prototype for %q+D follows non-prototype definition",
1772 newdecl);
1773 return true;
1774 #undef END_OF_ARGLIST
1777 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1778 first in a pair of mismatched declarations, using the diagnostic
1779 function DIAG. */
1780 static void
1781 locate_old_decl (tree decl)
1783 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1784 && !C_DECL_DECLARED_BUILTIN (decl))
1786 else if (DECL_INITIAL (decl))
1787 inform (input_location, "previous definition of %q+D was here", decl);
1788 else if (C_DECL_IMPLICIT (decl))
1789 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1790 else
1791 inform (input_location, "previous declaration of %q+D was here", decl);
1794 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1795 Returns true if the caller should proceed to merge the two, false
1796 if OLDDECL should simply be discarded. As a side effect, issues
1797 all necessary diagnostics for invalid or poor-style combinations.
1798 If it returns true, writes the types of NEWDECL and OLDDECL to
1799 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1800 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1802 static bool
1803 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1804 tree *newtypep, tree *oldtypep)
1806 tree newtype, oldtype;
1807 bool pedwarned = false;
1808 bool warned = false;
1809 bool retval = true;
1811 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1812 && DECL_EXTERNAL (DECL))
1814 /* If we have error_mark_node for either decl or type, just discard
1815 the previous decl - we're in an error cascade already. */
1816 if (olddecl == error_mark_node || newdecl == error_mark_node)
1817 return false;
1818 *oldtypep = oldtype = TREE_TYPE (olddecl);
1819 *newtypep = newtype = TREE_TYPE (newdecl);
1820 if (oldtype == error_mark_node || newtype == error_mark_node)
1821 return false;
1823 /* Two different categories of symbol altogether. This is an error
1824 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1825 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1827 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1828 && DECL_BUILT_IN (olddecl)
1829 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1831 error ("%q+D redeclared as different kind of symbol", newdecl);
1832 locate_old_decl (olddecl);
1834 else if (TREE_PUBLIC (newdecl))
1835 warning (0, "built-in function %q+D declared as non-function",
1836 newdecl);
1837 else
1838 warning (OPT_Wshadow, "declaration of %q+D shadows "
1839 "a built-in function", newdecl);
1840 return false;
1843 /* Enumerators have no linkage, so may only be declared once in a
1844 given scope. */
1845 if (TREE_CODE (olddecl) == CONST_DECL)
1847 error ("redeclaration of enumerator %q+D", newdecl);
1848 locate_old_decl (olddecl);
1849 return false;
1852 if (!comptypes (oldtype, newtype))
1854 if (TREE_CODE (olddecl) == FUNCTION_DECL
1855 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1857 /* Accept harmless mismatch in function types.
1858 This is for the ffs and fprintf builtins. */
1859 tree trytype = match_builtin_function_types (newtype, oldtype);
1861 if (trytype && comptypes (newtype, trytype))
1862 *oldtypep = oldtype = trytype;
1863 else
1865 /* If types don't match for a built-in, throw away the
1866 built-in. No point in calling locate_old_decl here, it
1867 won't print anything. */
1868 warning (0, "conflicting types for built-in function %q+D",
1869 newdecl);
1870 return false;
1873 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1874 && DECL_IS_BUILTIN (olddecl))
1876 /* A conflicting function declaration for a predeclared
1877 function that isn't actually built in. Objective C uses
1878 these. The new declaration silently overrides everything
1879 but the volatility (i.e. noreturn) indication. See also
1880 below. FIXME: Make Objective C use normal builtins. */
1881 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1882 return false;
1884 /* Permit void foo (...) to match int foo (...) if the latter is
1885 the definition and implicit int was used. See
1886 c-torture/compile/920625-2.c. */
1887 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1888 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1889 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1890 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1892 pedwarned = pedwarn (input_location, 0,
1893 "conflicting types for %q+D", newdecl);
1894 /* Make sure we keep void as the return type. */
1895 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1896 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1898 /* Permit void foo (...) to match an earlier call to foo (...) with
1899 no declared type (thus, implicitly int). */
1900 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1901 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1902 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1903 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1905 pedwarned = pedwarn (input_location, 0,
1906 "conflicting types for %q+D", newdecl);
1907 /* Make sure we keep void as the return type. */
1908 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1910 else
1912 int new_quals = TYPE_QUALS (newtype);
1913 int old_quals = TYPE_QUALS (oldtype);
1915 if (new_quals != old_quals)
1917 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1918 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1919 if (new_addr != old_addr)
1921 if (ADDR_SPACE_GENERIC_P (new_addr))
1922 error ("conflicting named address spaces (generic vs %s) "
1923 "for %q+D",
1924 c_addr_space_name (old_addr), newdecl);
1925 else if (ADDR_SPACE_GENERIC_P (old_addr))
1926 error ("conflicting named address spaces (%s vs generic) "
1927 "for %q+D",
1928 c_addr_space_name (new_addr), newdecl);
1929 else
1930 error ("conflicting named address spaces (%s vs %s) "
1931 "for %q+D",
1932 c_addr_space_name (new_addr),
1933 c_addr_space_name (old_addr),
1934 newdecl);
1937 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1938 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1939 error ("conflicting type qualifiers for %q+D", newdecl);
1941 else
1942 error ("conflicting types for %q+D", newdecl);
1943 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1944 locate_old_decl (olddecl);
1945 return false;
1949 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1950 but silently ignore the redeclaration if either is in a system
1951 header. (Conflicting redeclarations were handled above.) This
1952 is allowed for C11 if the types are the same, not just
1953 compatible. */
1954 if (TREE_CODE (newdecl) == TYPE_DECL)
1956 bool types_different = false;
1957 int comptypes_result;
1959 comptypes_result
1960 = comptypes_check_different_types (oldtype, newtype, &types_different);
1962 if (comptypes_result != 1 || types_different)
1964 error ("redefinition of typedef %q+D with different type", newdecl);
1965 locate_old_decl (olddecl);
1966 return false;
1969 if (DECL_IN_SYSTEM_HEADER (newdecl)
1970 || DECL_IN_SYSTEM_HEADER (olddecl)
1971 || TREE_NO_WARNING (newdecl)
1972 || TREE_NO_WARNING (olddecl))
1973 return true; /* Allow OLDDECL to continue in use. */
1975 if (variably_modified_type_p (newtype, NULL))
1977 error ("redefinition of typedef %q+D with variably modified type",
1978 newdecl);
1979 locate_old_decl (olddecl);
1981 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1982 "redefinition of typedef %q+D", newdecl))
1983 locate_old_decl (olddecl);
1985 return true;
1988 /* Function declarations can either be 'static' or 'extern' (no
1989 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1990 can never conflict with each other on account of linkage
1991 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1992 gnu89 mode permits two definitions if one is 'extern inline' and
1993 one is not. The non- extern-inline definition supersedes the
1994 extern-inline definition. */
1996 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1998 /* If you declare a built-in function name as static, or
1999 define the built-in with an old-style definition (so we
2000 can't validate the argument list) the built-in definition is
2001 overridden, but optionally warn this was a bad choice of name. */
2002 if (DECL_BUILT_IN (olddecl)
2003 && !C_DECL_DECLARED_BUILTIN (olddecl)
2004 && (!TREE_PUBLIC (newdecl)
2005 || (DECL_INITIAL (newdecl)
2006 && !prototype_p (TREE_TYPE (newdecl)))))
2008 warning (OPT_Wshadow, "declaration of %q+D shadows "
2009 "a built-in function", newdecl);
2010 /* Discard the old built-in function. */
2011 return false;
2014 if (DECL_INITIAL (newdecl))
2016 if (DECL_INITIAL (olddecl))
2018 /* If both decls are in the same TU and the new declaration
2019 isn't overriding an extern inline reject the new decl.
2020 In c99, no overriding is allowed in the same translation
2021 unit. */
2022 if ((!DECL_EXTERN_INLINE (olddecl)
2023 || DECL_EXTERN_INLINE (newdecl)
2024 || (!flag_gnu89_inline
2025 && (!DECL_DECLARED_INLINE_P (olddecl)
2026 || !lookup_attribute ("gnu_inline",
2027 DECL_ATTRIBUTES (olddecl)))
2028 && (!DECL_DECLARED_INLINE_P (newdecl)
2029 || !lookup_attribute ("gnu_inline",
2030 DECL_ATTRIBUTES (newdecl))))
2032 && same_translation_unit_p (newdecl, olddecl))
2034 error ("redefinition of %q+D", newdecl);
2035 locate_old_decl (olddecl);
2036 return false;
2040 /* If we have a prototype after an old-style function definition,
2041 the argument types must be checked specially. */
2042 else if (DECL_INITIAL (olddecl)
2043 && !prototype_p (oldtype) && prototype_p (newtype)
2044 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2045 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2047 locate_old_decl (olddecl);
2048 return false;
2050 /* A non-static declaration (even an "extern") followed by a
2051 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2052 The same is true for a static forward declaration at block
2053 scope followed by a non-static declaration/definition at file
2054 scope. Static followed by non-static at the same scope is
2055 not undefined behavior, and is the most convenient way to get
2056 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2057 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2058 we do diagnose it if -Wtraditional. */
2059 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2061 /* Two exceptions to the rule. If olddecl is an extern
2062 inline, or a predeclared function that isn't actually
2063 built in, newdecl silently overrides olddecl. The latter
2064 occur only in Objective C; see also above. (FIXME: Make
2065 Objective C use normal builtins.) */
2066 if (!DECL_IS_BUILTIN (olddecl)
2067 && !DECL_EXTERN_INLINE (olddecl))
2069 error ("static declaration of %q+D follows "
2070 "non-static declaration", newdecl);
2071 locate_old_decl (olddecl);
2073 return false;
2075 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2077 if (DECL_CONTEXT (olddecl))
2079 error ("non-static declaration of %q+D follows "
2080 "static declaration", newdecl);
2081 locate_old_decl (olddecl);
2082 return false;
2084 else if (warn_traditional)
2086 warned |= warning (OPT_Wtraditional,
2087 "non-static declaration of %q+D "
2088 "follows static declaration", newdecl);
2092 /* Make sure gnu_inline attribute is either not present, or
2093 present on all inline decls. */
2094 if (DECL_DECLARED_INLINE_P (olddecl)
2095 && DECL_DECLARED_INLINE_P (newdecl))
2097 bool newa = lookup_attribute ("gnu_inline",
2098 DECL_ATTRIBUTES (newdecl)) != NULL;
2099 bool olda = lookup_attribute ("gnu_inline",
2100 DECL_ATTRIBUTES (olddecl)) != NULL;
2101 if (newa != olda)
2103 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2104 newa ? newdecl : olddecl);
2105 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2106 "but not here");
2110 else if (VAR_P (newdecl))
2112 /* Only variables can be thread-local, and all declarations must
2113 agree on this property. */
2114 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2116 /* Nothing to check. Since OLDDECL is marked threadprivate
2117 and NEWDECL does not have a thread-local attribute, we
2118 will merge the threadprivate attribute into NEWDECL. */
2121 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2123 if (DECL_THREAD_LOCAL_P (newdecl))
2124 error ("thread-local declaration of %q+D follows "
2125 "non-thread-local declaration", newdecl);
2126 else
2127 error ("non-thread-local declaration of %q+D follows "
2128 "thread-local declaration", newdecl);
2130 locate_old_decl (olddecl);
2131 return false;
2134 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2135 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2137 error ("redefinition of %q+D", newdecl);
2138 locate_old_decl (olddecl);
2139 return false;
2142 /* Objects declared at file scope: if the first declaration had
2143 external linkage (even if it was an external reference) the
2144 second must have external linkage as well, or the behavior is
2145 undefined. If the first declaration had internal linkage, then
2146 the second must too, or else be an external reference (in which
2147 case the composite declaration still has internal linkage).
2148 As for function declarations, we warn about the static-then-
2149 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2150 if (DECL_FILE_SCOPE_P (newdecl)
2151 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2153 if (DECL_EXTERNAL (newdecl))
2155 if (!DECL_FILE_SCOPE_P (olddecl))
2157 error ("extern declaration of %q+D follows "
2158 "declaration with no linkage", newdecl);
2159 locate_old_decl (olddecl);
2160 return false;
2162 else if (warn_traditional)
2164 warned |= warning (OPT_Wtraditional,
2165 "non-static declaration of %q+D "
2166 "follows static declaration", newdecl);
2169 else
2171 if (TREE_PUBLIC (newdecl))
2172 error ("non-static declaration of %q+D follows "
2173 "static declaration", newdecl);
2174 else
2175 error ("static declaration of %q+D follows "
2176 "non-static declaration", newdecl);
2178 locate_old_decl (olddecl);
2179 return false;
2182 /* Two objects with the same name declared at the same block
2183 scope must both be external references (6.7p3). */
2184 else if (!DECL_FILE_SCOPE_P (newdecl))
2186 if (DECL_EXTERNAL (newdecl))
2188 /* Extern with initializer at block scope, which will
2189 already have received an error. */
2191 else if (DECL_EXTERNAL (olddecl))
2193 error ("declaration of %q+D with no linkage follows "
2194 "extern declaration", newdecl);
2195 locate_old_decl (olddecl);
2197 else
2199 error ("redeclaration of %q+D with no linkage", newdecl);
2200 locate_old_decl (olddecl);
2203 return false;
2206 /* C++ does not permit a decl to appear multiple times at file
2207 scope. */
2208 if (warn_cxx_compat
2209 && DECL_FILE_SCOPE_P (newdecl)
2210 && !DECL_EXTERNAL (newdecl)
2211 && !DECL_EXTERNAL (olddecl))
2212 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2213 OPT_Wc___compat,
2214 ("duplicate declaration of %qD is "
2215 "invalid in C++"),
2216 newdecl);
2219 /* warnings */
2220 /* All decls must agree on a visibility. */
2221 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2222 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2223 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2225 warned |= warning (0, "redeclaration of %q+D with different visibility "
2226 "(old visibility preserved)", newdecl);
2229 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2231 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2232 if (DECL_DECLARED_INLINE_P (newdecl)
2233 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2234 warned |= warning (OPT_Wattributes,
2235 "inline declaration of %qD follows "
2236 "declaration with attribute noinline", newdecl);
2237 else if (DECL_DECLARED_INLINE_P (olddecl)
2238 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2239 warned |= warning (OPT_Wattributes,
2240 "declaration of %q+D with attribute "
2241 "noinline follows inline declaration ", newdecl);
2242 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2243 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2244 warned |= warning (OPT_Wattributes,
2245 "declaration of %q+D with attribute "
2246 "%qs follows declaration with attribute %qs",
2247 newdecl, "noinline", "always_inline");
2248 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2249 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2250 warned |= warning (OPT_Wattributes,
2251 "declaration of %q+D with attribute "
2252 "%qs follows declaration with attribute %qs",
2253 newdecl, "always_inline", "noinline");
2254 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2255 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2256 warned |= warning (OPT_Wattributes,
2257 "declaration of %q+D with attribute %qs follows "
2258 "declaration with attribute %qs", newdecl, "cold",
2259 "hot");
2260 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2261 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2262 warned |= warning (OPT_Wattributes,
2263 "declaration of %q+D with attribute %qs follows "
2264 "declaration with attribute %qs", newdecl, "hot",
2265 "cold");
2267 else /* PARM_DECL, VAR_DECL */
2269 /* Redeclaration of a parameter is a constraint violation (this is
2270 not explicitly stated, but follows from C99 6.7p3 [no more than
2271 one declaration of the same identifier with no linkage in the
2272 same scope, except type tags] and 6.2.2p6 [parameters have no
2273 linkage]). We must check for a forward parameter declaration,
2274 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2275 an extension, the mandatory diagnostic for which is handled by
2276 mark_forward_parm_decls. */
2278 if (TREE_CODE (newdecl) == PARM_DECL
2279 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2281 error ("redefinition of parameter %q+D", newdecl);
2282 locate_old_decl (olddecl);
2283 return false;
2287 /* Optional warning for completely redundant decls. */
2288 if (!warned && !pedwarned
2289 && warn_redundant_decls
2290 /* Don't warn about a function declaration followed by a
2291 definition. */
2292 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2293 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2294 /* Don't warn about redundant redeclarations of builtins. */
2295 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2296 && !DECL_BUILT_IN (newdecl)
2297 && DECL_BUILT_IN (olddecl)
2298 && !C_DECL_DECLARED_BUILTIN (olddecl))
2299 /* Don't warn about an extern followed by a definition. */
2300 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2301 /* Don't warn about forward parameter decls. */
2302 && !(TREE_CODE (newdecl) == PARM_DECL
2303 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2304 /* Don't warn about a variable definition following a declaration. */
2305 && !(VAR_P (newdecl)
2306 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2308 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2309 newdecl);
2312 /* Report location of previous decl/defn. */
2313 if (warned || pedwarned)
2314 locate_old_decl (olddecl);
2316 #undef DECL_EXTERN_INLINE
2318 return retval;
2321 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2322 consistent with OLDDECL, but carries new information. Merge the
2323 new information into OLDDECL. This function issues no
2324 diagnostics. */
2326 static void
2327 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2329 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2330 && DECL_INITIAL (newdecl) != 0);
2331 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2332 && prototype_p (TREE_TYPE (newdecl)));
2333 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2334 && prototype_p (TREE_TYPE (olddecl)));
2336 /* For real parm decl following a forward decl, rechain the old decl
2337 in its new location and clear TREE_ASM_WRITTEN (it's not a
2338 forward decl anymore). */
2339 if (TREE_CODE (newdecl) == PARM_DECL
2340 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2342 struct c_binding *b, **here;
2344 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2345 if ((*here)->decl == olddecl)
2346 goto found;
2347 gcc_unreachable ();
2349 found:
2350 b = *here;
2351 *here = b->prev;
2352 b->prev = current_scope->bindings;
2353 current_scope->bindings = b;
2355 TREE_ASM_WRITTEN (olddecl) = 0;
2358 DECL_ATTRIBUTES (newdecl)
2359 = targetm.merge_decl_attributes (olddecl, newdecl);
2361 /* Merge the data types specified in the two decls. */
2362 TREE_TYPE (newdecl)
2363 = TREE_TYPE (olddecl)
2364 = composite_type (newtype, oldtype);
2366 /* Lay the type out, unless already done. */
2367 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2369 if (TREE_TYPE (newdecl) != error_mark_node)
2370 layout_type (TREE_TYPE (newdecl));
2371 if (TREE_CODE (newdecl) != FUNCTION_DECL
2372 && TREE_CODE (newdecl) != TYPE_DECL
2373 && TREE_CODE (newdecl) != CONST_DECL)
2374 layout_decl (newdecl, 0);
2376 else
2378 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2379 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2380 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2381 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2382 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2384 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2385 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2389 /* Keep the old rtl since we can safely use it. */
2390 if (HAS_RTL_P (olddecl))
2391 COPY_DECL_RTL (olddecl, newdecl);
2393 /* Merge the type qualifiers. */
2394 if (TREE_READONLY (newdecl))
2395 TREE_READONLY (olddecl) = 1;
2397 if (TREE_THIS_VOLATILE (newdecl))
2398 TREE_THIS_VOLATILE (olddecl) = 1;
2400 /* Merge deprecatedness. */
2401 if (TREE_DEPRECATED (newdecl))
2402 TREE_DEPRECATED (olddecl) = 1;
2404 /* If a decl is in a system header and the other isn't, keep the one on the
2405 system header. Otherwise, keep source location of definition rather than
2406 declaration and of prototype rather than non-prototype unless that
2407 prototype is built-in. */
2408 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2409 && DECL_IN_SYSTEM_HEADER (olddecl)
2410 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2411 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2412 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2413 && DECL_IN_SYSTEM_HEADER (newdecl)
2414 && !DECL_IN_SYSTEM_HEADER (olddecl))
2415 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2416 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2417 || (old_is_prototype && !new_is_prototype
2418 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2419 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2421 /* Merge the initialization information. */
2422 if (DECL_INITIAL (newdecl) == 0)
2423 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2425 /* Merge the threadprivate attribute. */
2426 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2427 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2429 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2431 /* Copy the assembler name.
2432 Currently, it can only be defined in the prototype. */
2433 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2435 /* Use visibility of whichever declaration had it specified */
2436 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2438 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2439 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2442 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2444 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2445 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2446 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2447 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2448 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2449 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2450 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2451 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2452 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2453 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2454 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2457 /* Merge the storage class information. */
2458 merge_weak (newdecl, olddecl);
2460 /* For functions, static overrides non-static. */
2461 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2463 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2464 /* This is since we don't automatically
2465 copy the attributes of NEWDECL into OLDDECL. */
2466 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2467 /* If this clears `static', clear it in the identifier too. */
2468 if (!TREE_PUBLIC (olddecl))
2469 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2473 /* In c99, 'extern' declaration before (or after) 'inline' means this
2474 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2475 is present. */
2476 if (TREE_CODE (newdecl) == FUNCTION_DECL
2477 && !flag_gnu89_inline
2478 && (DECL_DECLARED_INLINE_P (newdecl)
2479 || DECL_DECLARED_INLINE_P (olddecl))
2480 && (!DECL_DECLARED_INLINE_P (newdecl)
2481 || !DECL_DECLARED_INLINE_P (olddecl)
2482 || !DECL_EXTERNAL (olddecl))
2483 && DECL_EXTERNAL (newdecl)
2484 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2485 && !current_function_decl)
2486 DECL_EXTERNAL (newdecl) = 0;
2488 /* An inline definition following a static declaration is not
2489 DECL_EXTERNAL. */
2490 if (new_is_definition
2491 && (DECL_DECLARED_INLINE_P (newdecl)
2492 || DECL_DECLARED_INLINE_P (olddecl))
2493 && !TREE_PUBLIC (olddecl))
2494 DECL_EXTERNAL (newdecl) = 0;
2496 if (DECL_EXTERNAL (newdecl))
2498 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2499 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2501 /* An extern decl does not override previous storage class. */
2502 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2503 if (!DECL_EXTERNAL (newdecl))
2505 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2506 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2509 else
2511 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2512 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2515 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2517 /* If we're redefining a function previously defined as extern
2518 inline, make sure we emit debug info for the inline before we
2519 throw it away, in case it was inlined into a function that
2520 hasn't been written out yet. */
2521 if (new_is_definition && DECL_INITIAL (olddecl))
2522 /* The new defn must not be inline. */
2523 DECL_UNINLINABLE (newdecl) = 1;
2524 else
2526 /* If either decl says `inline', this fn is inline, unless
2527 its definition was passed already. */
2528 if (DECL_DECLARED_INLINE_P (newdecl)
2529 || DECL_DECLARED_INLINE_P (olddecl))
2530 DECL_DECLARED_INLINE_P (newdecl) = 1;
2532 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2533 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2535 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2536 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2537 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2538 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2541 if (DECL_BUILT_IN (olddecl))
2543 /* If redeclaring a builtin function, it stays built in.
2544 But it gets tagged as having been declared. */
2545 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2546 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2547 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2548 if (new_is_prototype)
2550 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2551 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2553 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2554 switch (fncode)
2556 /* If a compatible prototype of these builtin functions
2557 is seen, assume the runtime implements it with the
2558 expected semantics. */
2559 case BUILT_IN_STPCPY:
2560 if (builtin_decl_explicit_p (fncode))
2561 set_builtin_decl_implicit_p (fncode, true);
2562 break;
2563 default:
2564 if (builtin_decl_explicit_p (fncode))
2565 set_builtin_decl_declared_p (fncode, true);
2566 break;
2570 else
2571 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2572 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2575 /* Preserve function specific target and optimization options */
2576 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2577 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2578 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2579 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2581 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2582 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2583 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2584 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2586 /* Also preserve various other info from the definition. */
2587 if (!new_is_definition)
2589 tree t;
2590 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2591 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2592 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2593 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2594 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2595 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2596 DECL_CONTEXT (t) = newdecl;
2598 /* See if we've got a function to instantiate from. */
2599 if (DECL_SAVED_TREE (olddecl))
2600 DECL_ABSTRACT_ORIGIN (newdecl)
2601 = DECL_ABSTRACT_ORIGIN (olddecl);
2605 /* Merge the USED information. */
2606 if (TREE_USED (olddecl))
2607 TREE_USED (newdecl) = 1;
2608 else if (TREE_USED (newdecl))
2609 TREE_USED (olddecl) = 1;
2610 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2611 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2612 if (DECL_PRESERVE_P (olddecl))
2613 DECL_PRESERVE_P (newdecl) = 1;
2614 else if (DECL_PRESERVE_P (newdecl))
2615 DECL_PRESERVE_P (olddecl) = 1;
2617 /* Merge DECL_COMMON */
2618 if (VAR_P (olddecl) && VAR_P (newdecl)
2619 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2620 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2621 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2623 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2624 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2625 DECL_ARGUMENTS (if appropriate). */
2627 unsigned olddecl_uid = DECL_UID (olddecl);
2628 tree olddecl_context = DECL_CONTEXT (olddecl);
2629 tree olddecl_arguments = NULL;
2630 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2631 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2633 memcpy ((char *) olddecl + sizeof (struct tree_common),
2634 (char *) newdecl + sizeof (struct tree_common),
2635 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2636 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2637 switch (TREE_CODE (olddecl))
2639 case FUNCTION_DECL:
2640 case VAR_DECL:
2642 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2644 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2645 (char *) newdecl + sizeof (struct tree_decl_common),
2646 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2647 olddecl->decl_with_vis.symtab_node = snode;
2649 if ((DECL_EXTERNAL (olddecl)
2650 || TREE_PUBLIC (olddecl)
2651 || TREE_STATIC (olddecl))
2652 && DECL_SECTION_NAME (newdecl) != NULL)
2653 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2655 /* This isn't quite correct for something like
2656 int __thread x attribute ((tls_model ("local-exec")));
2657 extern int __thread x;
2658 as we'll lose the "local-exec" model. */
2659 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2660 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2661 break;
2664 case FIELD_DECL:
2665 case PARM_DECL:
2666 case LABEL_DECL:
2667 case RESULT_DECL:
2668 case CONST_DECL:
2669 case TYPE_DECL:
2670 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2671 (char *) newdecl + sizeof (struct tree_decl_common),
2672 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2673 break;
2675 default:
2677 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2678 (char *) newdecl + sizeof (struct tree_decl_common),
2679 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2681 DECL_UID (olddecl) = olddecl_uid;
2682 DECL_CONTEXT (olddecl) = olddecl_context;
2683 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2684 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2687 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2688 so that encode_section_info has a chance to look at the new decl
2689 flags and attributes. */
2690 if (DECL_RTL_SET_P (olddecl)
2691 && (TREE_CODE (olddecl) == FUNCTION_DECL
2692 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2693 make_decl_rtl (olddecl);
2696 /* Handle when a new declaration NEWDECL has the same name as an old
2697 one OLDDECL in the same binding contour. Prints an error message
2698 if appropriate.
2700 If safely possible, alter OLDDECL to look like NEWDECL, and return
2701 true. Otherwise, return false. */
2703 static bool
2704 duplicate_decls (tree newdecl, tree olddecl)
2706 tree newtype = NULL, oldtype = NULL;
2708 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2710 /* Avoid `unused variable' and other warnings for OLDDECL. */
2711 TREE_NO_WARNING (olddecl) = 1;
2712 return false;
2715 merge_decls (newdecl, olddecl, newtype, oldtype);
2717 /* The NEWDECL will no longer be needed.
2719 Before releasing the node, be sure to remove function from symbol
2720 table that might have been inserted there to record comdat group.
2721 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2722 structure is shared in between NEWDECL and OLDECL. */
2723 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2724 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2725 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2727 struct symtab_node *snode = symtab_node::get (newdecl);
2728 if (snode)
2729 snode->remove ();
2731 ggc_free (newdecl);
2732 return true;
2736 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2737 static void
2738 warn_if_shadowing (tree new_decl)
2740 struct c_binding *b;
2742 /* Shadow warnings wanted? */
2743 if (!warn_shadow
2744 /* No shadow warnings for internally generated vars. */
2745 || DECL_IS_BUILTIN (new_decl)
2746 /* No shadow warnings for vars made for inlining. */
2747 || DECL_FROM_INLINE (new_decl))
2748 return;
2750 /* Is anything being shadowed? Invisible decls do not count. */
2751 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2752 if (b->decl && b->decl != new_decl && !b->invisible
2753 && (b->decl == error_mark_node
2754 || diagnostic_report_warnings_p (global_dc,
2755 DECL_SOURCE_LOCATION (b->decl))))
2757 tree old_decl = b->decl;
2758 bool warned = false;
2760 if (old_decl == error_mark_node)
2762 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2763 "non-variable", new_decl);
2764 break;
2766 else if (TREE_CODE (old_decl) == PARM_DECL)
2767 warned = warning (OPT_Wshadow,
2768 "declaration of %q+D shadows a parameter",
2769 new_decl);
2770 else if (DECL_FILE_SCOPE_P (old_decl))
2772 /* Do not warn if a variable shadows a function, unless
2773 the variable is a function or a pointer-to-function. */
2774 if (TREE_CODE (old_decl) == FUNCTION_DECL
2775 && TREE_CODE (new_decl) != FUNCTION_DECL
2776 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2777 continue;
2779 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2780 "declaration of %qD shadows a global "
2781 "declaration",
2782 new_decl);
2784 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2785 && DECL_BUILT_IN (old_decl))
2787 warning (OPT_Wshadow, "declaration of %q+D shadows "
2788 "a built-in function", new_decl);
2789 break;
2791 else
2792 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2793 "previous local", new_decl);
2795 if (warned)
2796 inform (DECL_SOURCE_LOCATION (old_decl),
2797 "shadowed declaration is here");
2799 break;
2803 /* Record a decl-node X as belonging to the current lexical scope.
2804 Check for errors (such as an incompatible declaration for the same
2805 name already seen in the same scope).
2807 Returns either X or an old decl for the same name.
2808 If an old decl is returned, it may have been smashed
2809 to agree with what X says. */
2811 tree
2812 pushdecl (tree x)
2814 tree name = DECL_NAME (x);
2815 struct c_scope *scope = current_scope;
2816 struct c_binding *b;
2817 bool nested = false;
2818 location_t locus = DECL_SOURCE_LOCATION (x);
2820 /* Must set DECL_CONTEXT for everything not at file scope or
2821 DECL_FILE_SCOPE_P won't work. Local externs don't count
2822 unless they have initializers (which generate code). */
2823 if (current_function_decl
2824 && (!VAR_OR_FUNCTION_DECL_P (x)
2825 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2826 DECL_CONTEXT (x) = current_function_decl;
2828 /* Anonymous decls are just inserted in the scope. */
2829 if (!name)
2831 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2832 locus);
2833 return x;
2836 /* First, see if there is another declaration with the same name in
2837 the current scope. If there is, duplicate_decls may do all the
2838 work for us. If duplicate_decls returns false, that indicates
2839 two incompatible decls in the same scope; we are to silently
2840 replace the old one (duplicate_decls has issued all appropriate
2841 diagnostics). In particular, we should not consider possible
2842 duplicates in the external scope, or shadowing. */
2843 b = I_SYMBOL_BINDING (name);
2844 if (b && B_IN_SCOPE (b, scope))
2846 struct c_binding *b_ext, *b_use;
2847 tree type = TREE_TYPE (x);
2848 tree visdecl = b->decl;
2849 tree vistype = TREE_TYPE (visdecl);
2850 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2851 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2852 b->inner_comp = false;
2853 b_use = b;
2854 b_ext = b;
2855 /* If this is an external linkage declaration, we should check
2856 for compatibility with the type in the external scope before
2857 setting the type at this scope based on the visible
2858 information only. */
2859 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2861 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2862 b_ext = b_ext->shadowed;
2863 if (b_ext)
2865 b_use = b_ext;
2866 if (b_use->u.type)
2867 TREE_TYPE (b_use->decl) = b_use->u.type;
2870 if (duplicate_decls (x, b_use->decl))
2872 if (b_use != b)
2874 /* Save the updated type in the external scope and
2875 restore the proper type for this scope. */
2876 tree thistype;
2877 if (comptypes (vistype, type))
2878 thistype = composite_type (vistype, type);
2879 else
2880 thistype = TREE_TYPE (b_use->decl);
2881 b_use->u.type = TREE_TYPE (b_use->decl);
2882 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2883 && DECL_BUILT_IN (b_use->decl))
2884 thistype
2885 = build_type_attribute_variant (thistype,
2886 TYPE_ATTRIBUTES
2887 (b_use->u.type));
2888 TREE_TYPE (b_use->decl) = thistype;
2890 return b_use->decl;
2892 else
2893 goto skip_external_and_shadow_checks;
2896 /* All declarations with external linkage, and all external
2897 references, go in the external scope, no matter what scope is
2898 current. However, the binding in that scope is ignored for
2899 purposes of normal name lookup. A separate binding structure is
2900 created in the requested scope; this governs the normal
2901 visibility of the symbol.
2903 The binding in the externals scope is used exclusively for
2904 detecting duplicate declarations of the same object, no matter
2905 what scope they are in; this is what we do here. (C99 6.2.7p2:
2906 All declarations that refer to the same object or function shall
2907 have compatible type; otherwise, the behavior is undefined.) */
2908 if (DECL_EXTERNAL (x) || scope == file_scope)
2910 tree type = TREE_TYPE (x);
2911 tree vistype = 0;
2912 tree visdecl = 0;
2913 bool type_saved = false;
2914 if (b && !B_IN_EXTERNAL_SCOPE (b)
2915 && VAR_OR_FUNCTION_DECL_P (b->decl)
2916 && DECL_FILE_SCOPE_P (b->decl))
2918 visdecl = b->decl;
2919 vistype = TREE_TYPE (visdecl);
2921 if (scope != file_scope
2922 && !DECL_IN_SYSTEM_HEADER (x))
2923 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2925 while (b && !B_IN_EXTERNAL_SCOPE (b))
2927 /* If this decl might be modified, save its type. This is
2928 done here rather than when the decl is first bound
2929 because the type may change after first binding, through
2930 being completed or through attributes being added. If we
2931 encounter multiple such decls, only the first should have
2932 its type saved; the others will already have had their
2933 proper types saved and the types will not have changed as
2934 their scopes will not have been re-entered. */
2935 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2937 b->u.type = TREE_TYPE (b->decl);
2938 type_saved = true;
2940 if (B_IN_FILE_SCOPE (b)
2941 && VAR_P (b->decl)
2942 && TREE_STATIC (b->decl)
2943 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2944 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2945 && TREE_CODE (type) == ARRAY_TYPE
2946 && TYPE_DOMAIN (type)
2947 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2948 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2950 /* Array type completed in inner scope, which should be
2951 diagnosed if the completion does not have size 1 and
2952 it does not get completed in the file scope. */
2953 b->inner_comp = true;
2955 b = b->shadowed;
2958 /* If a matching external declaration has been found, set its
2959 type to the composite of all the types of that declaration.
2960 After the consistency checks, it will be reset to the
2961 composite of the visible types only. */
2962 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2963 && b->u.type)
2964 TREE_TYPE (b->decl) = b->u.type;
2966 /* The point of the same_translation_unit_p check here is,
2967 we want to detect a duplicate decl for a construct like
2968 foo() { extern bar(); } ... static bar(); but not if
2969 they are in different translation units. In any case,
2970 the static does not go in the externals scope. */
2971 if (b
2972 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2973 && duplicate_decls (x, b->decl))
2975 tree thistype;
2976 if (vistype)
2978 if (comptypes (vistype, type))
2979 thistype = composite_type (vistype, type);
2980 else
2981 thistype = TREE_TYPE (b->decl);
2983 else
2984 thistype = type;
2985 b->u.type = TREE_TYPE (b->decl);
2986 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2987 thistype
2988 = build_type_attribute_variant (thistype,
2989 TYPE_ATTRIBUTES (b->u.type));
2990 TREE_TYPE (b->decl) = thistype;
2991 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2992 locus);
2993 return b->decl;
2995 else if (TREE_PUBLIC (x))
2997 if (visdecl && !b && duplicate_decls (x, visdecl))
2999 /* An external declaration at block scope referring to a
3000 visible entity with internal linkage. The composite
3001 type will already be correct for this scope, so we
3002 just need to fall through to make the declaration in
3003 this scope. */
3004 nested = true;
3005 x = visdecl;
3007 else
3009 bind (name, x, external_scope, /*invisible=*/true,
3010 /*nested=*/false, locus);
3011 nested = true;
3016 if (TREE_CODE (x) != PARM_DECL)
3017 warn_if_shadowing (x);
3019 skip_external_and_shadow_checks:
3020 if (TREE_CODE (x) == TYPE_DECL)
3022 /* So this is a typedef, set its underlying type. */
3023 set_underlying_type (x);
3025 /* If X is a typedef defined in the current function, record it
3026 for the purpose of implementing the -Wunused-local-typedefs
3027 warning. */
3028 record_locally_defined_typedef (x);
3031 bind (name, x, scope, /*invisible=*/false, nested, locus);
3033 /* If x's type is incomplete because it's based on a
3034 structure or union which has not yet been fully declared,
3035 attach it to that structure or union type, so we can go
3036 back and complete the variable declaration later, if the
3037 structure or union gets fully declared.
3039 If the input is erroneous, we can have error_mark in the type
3040 slot (e.g. "f(void a, ...)") - that doesn't count as an
3041 incomplete type. */
3042 if (TREE_TYPE (x) != error_mark_node
3043 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3045 tree element = TREE_TYPE (x);
3047 while (TREE_CODE (element) == ARRAY_TYPE)
3048 element = TREE_TYPE (element);
3049 element = TYPE_MAIN_VARIANT (element);
3051 if (RECORD_OR_UNION_TYPE_P (element)
3052 && (TREE_CODE (x) != TYPE_DECL
3053 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3054 && !COMPLETE_TYPE_P (element))
3055 C_TYPE_INCOMPLETE_VARS (element)
3056 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3058 return x;
3061 /* Record X as belonging to file scope.
3062 This is used only internally by the Objective-C front end,
3063 and is limited to its needs. duplicate_decls is not called;
3064 if there is any preexisting decl for this identifier, it is an ICE. */
3066 tree
3067 pushdecl_top_level (tree x)
3069 tree name;
3070 bool nested = false;
3071 gcc_assert (VAR_P (x) || TREE_CODE (x) == CONST_DECL);
3073 name = DECL_NAME (x);
3075 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3077 if (TREE_PUBLIC (x))
3079 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3080 UNKNOWN_LOCATION);
3081 nested = true;
3083 if (file_scope)
3084 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3086 return x;
3089 static void
3090 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3092 if (warn_implicit_function_declaration)
3094 bool warned;
3096 if (flag_isoc99)
3097 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3098 "implicit declaration of function %qE", id);
3099 else
3100 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3101 G_("implicit declaration of function %qE"), id);
3102 if (olddecl && warned)
3103 locate_old_decl (olddecl);
3107 /* This function represents mapping of a function code FCODE
3108 to its respective header. */
3110 static const char *
3111 header_for_builtin_fn (enum built_in_function fcode)
3113 switch (fcode)
3115 CASE_FLT_FN (BUILT_IN_ACOS):
3116 CASE_FLT_FN (BUILT_IN_ACOSH):
3117 CASE_FLT_FN (BUILT_IN_ASIN):
3118 CASE_FLT_FN (BUILT_IN_ASINH):
3119 CASE_FLT_FN (BUILT_IN_ATAN):
3120 CASE_FLT_FN (BUILT_IN_ATANH):
3121 CASE_FLT_FN (BUILT_IN_ATAN2):
3122 CASE_FLT_FN (BUILT_IN_CBRT):
3123 CASE_FLT_FN (BUILT_IN_CEIL):
3124 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3125 CASE_FLT_FN (BUILT_IN_COS):
3126 CASE_FLT_FN (BUILT_IN_COSH):
3127 CASE_FLT_FN (BUILT_IN_ERF):
3128 CASE_FLT_FN (BUILT_IN_ERFC):
3129 CASE_FLT_FN (BUILT_IN_EXP):
3130 CASE_FLT_FN (BUILT_IN_EXP2):
3131 CASE_FLT_FN (BUILT_IN_EXPM1):
3132 CASE_FLT_FN (BUILT_IN_FABS):
3133 CASE_FLT_FN (BUILT_IN_FDIM):
3134 CASE_FLT_FN (BUILT_IN_FLOOR):
3135 CASE_FLT_FN (BUILT_IN_FMA):
3136 CASE_FLT_FN (BUILT_IN_FMAX):
3137 CASE_FLT_FN (BUILT_IN_FMIN):
3138 CASE_FLT_FN (BUILT_IN_FMOD):
3139 CASE_FLT_FN (BUILT_IN_FREXP):
3140 CASE_FLT_FN (BUILT_IN_HYPOT):
3141 CASE_FLT_FN (BUILT_IN_ILOGB):
3142 CASE_FLT_FN (BUILT_IN_LDEXP):
3143 CASE_FLT_FN (BUILT_IN_LGAMMA):
3144 CASE_FLT_FN (BUILT_IN_LLRINT):
3145 CASE_FLT_FN (BUILT_IN_LLROUND):
3146 CASE_FLT_FN (BUILT_IN_LOG):
3147 CASE_FLT_FN (BUILT_IN_LOG10):
3148 CASE_FLT_FN (BUILT_IN_LOG1P):
3149 CASE_FLT_FN (BUILT_IN_LOG2):
3150 CASE_FLT_FN (BUILT_IN_LOGB):
3151 CASE_FLT_FN (BUILT_IN_LRINT):
3152 CASE_FLT_FN (BUILT_IN_LROUND):
3153 CASE_FLT_FN (BUILT_IN_MODF):
3154 CASE_FLT_FN (BUILT_IN_NAN):
3155 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3156 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3157 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3158 CASE_FLT_FN (BUILT_IN_POW):
3159 CASE_FLT_FN (BUILT_IN_REMAINDER):
3160 CASE_FLT_FN (BUILT_IN_REMQUO):
3161 CASE_FLT_FN (BUILT_IN_RINT):
3162 CASE_FLT_FN (BUILT_IN_ROUND):
3163 CASE_FLT_FN (BUILT_IN_SCALBLN):
3164 CASE_FLT_FN (BUILT_IN_SCALBN):
3165 CASE_FLT_FN (BUILT_IN_SIN):
3166 CASE_FLT_FN (BUILT_IN_SINH):
3167 CASE_FLT_FN (BUILT_IN_SINCOS):
3168 CASE_FLT_FN (BUILT_IN_SQRT):
3169 CASE_FLT_FN (BUILT_IN_TAN):
3170 CASE_FLT_FN (BUILT_IN_TANH):
3171 CASE_FLT_FN (BUILT_IN_TGAMMA):
3172 CASE_FLT_FN (BUILT_IN_TRUNC):
3173 case BUILT_IN_ISINF:
3174 case BUILT_IN_ISNAN:
3175 return "<math.h>";
3176 CASE_FLT_FN (BUILT_IN_CABS):
3177 CASE_FLT_FN (BUILT_IN_CACOS):
3178 CASE_FLT_FN (BUILT_IN_CACOSH):
3179 CASE_FLT_FN (BUILT_IN_CARG):
3180 CASE_FLT_FN (BUILT_IN_CASIN):
3181 CASE_FLT_FN (BUILT_IN_CASINH):
3182 CASE_FLT_FN (BUILT_IN_CATAN):
3183 CASE_FLT_FN (BUILT_IN_CATANH):
3184 CASE_FLT_FN (BUILT_IN_CCOS):
3185 CASE_FLT_FN (BUILT_IN_CCOSH):
3186 CASE_FLT_FN (BUILT_IN_CEXP):
3187 CASE_FLT_FN (BUILT_IN_CIMAG):
3188 CASE_FLT_FN (BUILT_IN_CLOG):
3189 CASE_FLT_FN (BUILT_IN_CONJ):
3190 CASE_FLT_FN (BUILT_IN_CPOW):
3191 CASE_FLT_FN (BUILT_IN_CPROJ):
3192 CASE_FLT_FN (BUILT_IN_CREAL):
3193 CASE_FLT_FN (BUILT_IN_CSIN):
3194 CASE_FLT_FN (BUILT_IN_CSINH):
3195 CASE_FLT_FN (BUILT_IN_CSQRT):
3196 CASE_FLT_FN (BUILT_IN_CTAN):
3197 CASE_FLT_FN (BUILT_IN_CTANH):
3198 return "<complex.h>";
3199 case BUILT_IN_MEMCHR:
3200 case BUILT_IN_MEMCMP:
3201 case BUILT_IN_MEMCPY:
3202 case BUILT_IN_MEMMOVE:
3203 case BUILT_IN_MEMSET:
3204 case BUILT_IN_STRCAT:
3205 case BUILT_IN_STRCHR:
3206 case BUILT_IN_STRCMP:
3207 case BUILT_IN_STRCPY:
3208 case BUILT_IN_STRCSPN:
3209 case BUILT_IN_STRLEN:
3210 case BUILT_IN_STRNCAT:
3211 case BUILT_IN_STRNCMP:
3212 case BUILT_IN_STRNCPY:
3213 case BUILT_IN_STRPBRK:
3214 case BUILT_IN_STRRCHR:
3215 case BUILT_IN_STRSPN:
3216 case BUILT_IN_STRSTR:
3217 return "<string.h>";
3218 case BUILT_IN_FPRINTF:
3219 case BUILT_IN_PUTC:
3220 case BUILT_IN_FPUTC:
3221 case BUILT_IN_FPUTS:
3222 case BUILT_IN_FSCANF:
3223 case BUILT_IN_FWRITE:
3224 case BUILT_IN_PRINTF:
3225 case BUILT_IN_PUTCHAR:
3226 case BUILT_IN_PUTS:
3227 case BUILT_IN_SCANF:
3228 case BUILT_IN_SNPRINTF:
3229 case BUILT_IN_SPRINTF:
3230 case BUILT_IN_SSCANF:
3231 case BUILT_IN_VFPRINTF:
3232 case BUILT_IN_VFSCANF:
3233 case BUILT_IN_VPRINTF:
3234 case BUILT_IN_VSCANF:
3235 case BUILT_IN_VSNPRINTF:
3236 case BUILT_IN_VSPRINTF:
3237 case BUILT_IN_VSSCANF:
3238 return "<stdio.h>";
3239 case BUILT_IN_ISALNUM:
3240 case BUILT_IN_ISALPHA:
3241 case BUILT_IN_ISBLANK:
3242 case BUILT_IN_ISCNTRL:
3243 case BUILT_IN_ISDIGIT:
3244 case BUILT_IN_ISGRAPH:
3245 case BUILT_IN_ISLOWER:
3246 case BUILT_IN_ISPRINT:
3247 case BUILT_IN_ISPUNCT:
3248 case BUILT_IN_ISSPACE:
3249 case BUILT_IN_ISUPPER:
3250 case BUILT_IN_ISXDIGIT:
3251 case BUILT_IN_TOLOWER:
3252 case BUILT_IN_TOUPPER:
3253 return "<ctype.h>";
3254 case BUILT_IN_ISWALNUM:
3255 case BUILT_IN_ISWALPHA:
3256 case BUILT_IN_ISWBLANK:
3257 case BUILT_IN_ISWCNTRL:
3258 case BUILT_IN_ISWDIGIT:
3259 case BUILT_IN_ISWGRAPH:
3260 case BUILT_IN_ISWLOWER:
3261 case BUILT_IN_ISWPRINT:
3262 case BUILT_IN_ISWPUNCT:
3263 case BUILT_IN_ISWSPACE:
3264 case BUILT_IN_ISWUPPER:
3265 case BUILT_IN_ISWXDIGIT:
3266 case BUILT_IN_TOWLOWER:
3267 case BUILT_IN_TOWUPPER:
3268 return "<wctype.h>";
3269 case BUILT_IN_ABORT:
3270 case BUILT_IN_ABS:
3271 case BUILT_IN_CALLOC:
3272 case BUILT_IN_EXIT:
3273 case BUILT_IN_FREE:
3274 case BUILT_IN_LABS:
3275 case BUILT_IN_LLABS:
3276 case BUILT_IN_MALLOC:
3277 case BUILT_IN_REALLOC:
3278 case BUILT_IN__EXIT2:
3279 case BUILT_IN_ALIGNED_ALLOC:
3280 return "<stdlib.h>";
3281 case BUILT_IN_IMAXABS:
3282 return "<inttypes.h>";
3283 case BUILT_IN_STRFTIME:
3284 return "<time.h>";
3285 default:
3286 return NULL;
3290 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3291 function of type int (). */
3293 tree
3294 implicitly_declare (location_t loc, tree functionid)
3296 struct c_binding *b;
3297 tree decl = 0;
3298 tree asmspec_tree;
3300 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3302 if (B_IN_SCOPE (b, external_scope))
3304 decl = b->decl;
3305 break;
3309 if (decl)
3311 if (decl == error_mark_node)
3312 return decl;
3314 /* FIXME: Objective-C has weird not-really-builtin functions
3315 which are supposed to be visible automatically. They wind up
3316 in the external scope because they're pushed before the file
3317 scope gets created. Catch this here and rebind them into the
3318 file scope. */
3319 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3321 bind (functionid, decl, file_scope,
3322 /*invisible=*/false, /*nested=*/true,
3323 DECL_SOURCE_LOCATION (decl));
3324 return decl;
3326 else
3328 tree newtype = default_function_type;
3329 if (b->u.type)
3330 TREE_TYPE (decl) = b->u.type;
3331 /* Implicit declaration of a function already declared
3332 (somehow) in a different scope, or as a built-in.
3333 If this is the first time this has happened, warn;
3334 then recycle the old declaration but with the new type. */
3335 if (!C_DECL_IMPLICIT (decl))
3337 implicit_decl_warning (loc, functionid, decl);
3338 C_DECL_IMPLICIT (decl) = 1;
3340 if (DECL_BUILT_IN (decl))
3342 newtype = build_type_attribute_variant (newtype,
3343 TYPE_ATTRIBUTES
3344 (TREE_TYPE (decl)));
3345 if (!comptypes (newtype, TREE_TYPE (decl)))
3347 bool warned = warning_at (loc, 0, "incompatible implicit "
3348 "declaration of built-in "
3349 "function %qD", decl);
3350 /* See if we can hint which header to include. */
3351 const char *header
3352 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3353 if (header != NULL && warned)
3354 inform (loc, "include %qs or provide a declaration of %qD",
3355 header, decl);
3356 newtype = TREE_TYPE (decl);
3359 else
3361 if (!comptypes (newtype, TREE_TYPE (decl)))
3363 error_at (loc, "incompatible implicit declaration of "
3364 "function %qD", decl);
3365 locate_old_decl (decl);
3368 b->u.type = TREE_TYPE (decl);
3369 TREE_TYPE (decl) = newtype;
3370 bind (functionid, decl, current_scope,
3371 /*invisible=*/false, /*nested=*/true,
3372 DECL_SOURCE_LOCATION (decl));
3373 return decl;
3377 /* Not seen before. */
3378 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3379 DECL_EXTERNAL (decl) = 1;
3380 TREE_PUBLIC (decl) = 1;
3381 C_DECL_IMPLICIT (decl) = 1;
3382 implicit_decl_warning (loc, functionid, 0);
3383 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3384 if (asmspec_tree)
3385 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3387 /* C89 says implicit declarations are in the innermost block.
3388 So we record the decl in the standard fashion. */
3389 decl = pushdecl (decl);
3391 /* No need to call objc_check_decl here - it's a function type. */
3392 rest_of_decl_compilation (decl, 0, 0);
3394 /* Write a record describing this implicit function declaration
3395 to the prototypes file (if requested). */
3396 gen_aux_info_record (decl, 0, 1, 0);
3398 /* Possibly apply some default attributes to this implicit declaration. */
3399 decl_attributes (&decl, NULL_TREE, 0);
3401 return decl;
3404 /* Issue an error message for a reference to an undeclared variable
3405 ID, including a reference to a builtin outside of function-call
3406 context. Establish a binding of the identifier to error_mark_node
3407 in an appropriate scope, which will suppress further errors for the
3408 same identifier. The error message should be given location LOC. */
3409 void
3410 undeclared_variable (location_t loc, tree id)
3412 static bool already = false;
3413 struct c_scope *scope;
3415 if (current_function_decl == 0)
3417 error_at (loc, "%qE undeclared here (not in a function)", id);
3418 scope = current_scope;
3420 else
3422 if (!objc_diagnose_private_ivar (id))
3423 error_at (loc, "%qE undeclared (first use in this function)", id);
3424 if (!already)
3426 inform (loc, "each undeclared identifier is reported only"
3427 " once for each function it appears in");
3428 already = true;
3431 /* If we are parsing old-style parameter decls, current_function_decl
3432 will be nonnull but current_function_scope will be null. */
3433 scope = current_function_scope ? current_function_scope : current_scope;
3435 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3436 UNKNOWN_LOCATION);
3439 /* Subroutine of lookup_label, declare_label, define_label: construct a
3440 LABEL_DECL with all the proper frills. Also create a struct
3441 c_label_vars initialized for the current scope. */
3443 static tree
3444 make_label (location_t location, tree name, bool defining,
3445 struct c_label_vars **p_label_vars)
3447 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3448 DECL_CONTEXT (label) = current_function_decl;
3449 DECL_MODE (label) = VOIDmode;
3451 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3452 label_vars->shadowed = NULL;
3453 set_spot_bindings (&label_vars->label_bindings, defining);
3454 label_vars->decls_in_scope = make_tree_vector ();
3455 label_vars->gotos = NULL;
3456 *p_label_vars = label_vars;
3458 return label;
3461 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3462 Create one if none exists so far for the current function.
3463 This is called when a label is used in a goto expression or
3464 has its address taken. */
3466 tree
3467 lookup_label (tree name)
3469 tree label;
3470 struct c_label_vars *label_vars;
3472 if (current_function_scope == 0)
3474 error ("label %qE referenced outside of any function", name);
3475 return NULL_TREE;
3478 /* Use a label already defined or ref'd with this name, but not if
3479 it is inherited from a containing function and wasn't declared
3480 using __label__. */
3481 label = I_LABEL_DECL (name);
3482 if (label && (DECL_CONTEXT (label) == current_function_decl
3483 || C_DECLARED_LABEL_FLAG (label)))
3485 /* If the label has only been declared, update its apparent
3486 location to point here, for better diagnostics if it
3487 turns out not to have been defined. */
3488 if (DECL_INITIAL (label) == NULL_TREE)
3489 DECL_SOURCE_LOCATION (label) = input_location;
3490 return label;
3493 /* No label binding for that identifier; make one. */
3494 label = make_label (input_location, name, false, &label_vars);
3496 /* Ordinary labels go in the current function scope. */
3497 bind_label (name, label, current_function_scope, label_vars);
3499 return label;
3502 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3503 to LABEL. */
3505 static void
3506 warn_about_goto (location_t goto_loc, tree label, tree decl)
3508 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3509 error_at (goto_loc,
3510 "jump into scope of identifier with variably modified type");
3511 else
3512 warning_at (goto_loc, OPT_Wjump_misses_init,
3513 "jump skips variable initialization");
3514 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3515 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3518 /* Look up a label because of a goto statement. This is like
3519 lookup_label, but also issues any appropriate warnings. */
3521 tree
3522 lookup_label_for_goto (location_t loc, tree name)
3524 tree label;
3525 struct c_label_vars *label_vars;
3526 unsigned int ix;
3527 tree decl;
3529 label = lookup_label (name);
3530 if (label == NULL_TREE)
3531 return NULL_TREE;
3533 /* If we are jumping to a different function, we can't issue any
3534 useful warnings. */
3535 if (DECL_CONTEXT (label) != current_function_decl)
3537 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3538 return label;
3541 label_vars = I_LABEL_BINDING (name)->u.label;
3543 /* If the label has not yet been defined, then push this goto on a
3544 list for possible later warnings. */
3545 if (label_vars->label_bindings.scope == NULL)
3547 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3549 g->loc = loc;
3550 set_spot_bindings (&g->goto_bindings, true);
3551 vec_safe_push (label_vars->gotos, g);
3552 return label;
3555 /* If there are any decls in label_vars->decls_in_scope, then this
3556 goto has missed the declaration of the decl. This happens for a
3557 case like
3558 int i = 1;
3559 lab:
3561 goto lab;
3562 Issue a warning or error. */
3563 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3564 warn_about_goto (loc, label, decl);
3566 if (label_vars->label_bindings.left_stmt_expr)
3568 error_at (loc, "jump into statement expression");
3569 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3572 return label;
3575 /* Make a label named NAME in the current function, shadowing silently
3576 any that may be inherited from containing functions or containing
3577 scopes. This is called for __label__ declarations. */
3579 tree
3580 declare_label (tree name)
3582 struct c_binding *b = I_LABEL_BINDING (name);
3583 tree label;
3584 struct c_label_vars *label_vars;
3586 /* Check to make sure that the label hasn't already been declared
3587 at this scope */
3588 if (b && B_IN_CURRENT_SCOPE (b))
3590 error ("duplicate label declaration %qE", name);
3591 locate_old_decl (b->decl);
3593 /* Just use the previous declaration. */
3594 return b->decl;
3597 label = make_label (input_location, name, false, &label_vars);
3598 C_DECLARED_LABEL_FLAG (label) = 1;
3600 /* Declared labels go in the current scope. */
3601 bind_label (name, label, current_scope, label_vars);
3603 return label;
3606 /* When we define a label, issue any appropriate warnings if there are
3607 any gotos earlier in the function which jump to this label. */
3609 static void
3610 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3612 unsigned int ix;
3613 struct c_goto_bindings *g;
3615 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3617 struct c_binding *b;
3618 struct c_scope *scope;
3620 /* We have a goto to this label. The goto is going forward. In
3621 g->scope, the goto is going to skip any binding which was
3622 defined after g->bindings_in_scope. */
3623 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3625 for (b = g->goto_bindings.scope->bindings;
3626 b != g->goto_bindings.bindings_in_scope;
3627 b = b->prev)
3629 if (decl_jump_unsafe (b->decl))
3630 warn_about_goto (g->loc, label, b->decl);
3634 /* We also need to warn about decls defined in any scopes
3635 between the scope of the label and the scope of the goto. */
3636 for (scope = label_vars->label_bindings.scope;
3637 scope != g->goto_bindings.scope;
3638 scope = scope->outer)
3640 gcc_assert (scope != NULL);
3641 if (scope->has_jump_unsafe_decl)
3643 if (scope == label_vars->label_bindings.scope)
3644 b = label_vars->label_bindings.bindings_in_scope;
3645 else
3646 b = scope->bindings;
3647 for (; b != NULL; b = b->prev)
3649 if (decl_jump_unsafe (b->decl))
3650 warn_about_goto (g->loc, label, b->decl);
3655 if (g->goto_bindings.stmt_exprs > 0)
3657 error_at (g->loc, "jump into statement expression");
3658 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3659 label);
3663 /* Now that the label is defined, we will issue warnings about
3664 subsequent gotos to this label when we see them. */
3665 vec_safe_truncate (label_vars->gotos, 0);
3666 label_vars->gotos = NULL;
3669 /* Define a label, specifying the location in the source file.
3670 Return the LABEL_DECL node for the label, if the definition is valid.
3671 Otherwise return 0. */
3673 tree
3674 define_label (location_t location, tree name)
3676 /* Find any preexisting label with this name. It is an error
3677 if that label has already been defined in this function, or
3678 if there is a containing function with a declared label with
3679 the same name. */
3680 tree label = I_LABEL_DECL (name);
3682 if (label
3683 && ((DECL_CONTEXT (label) == current_function_decl
3684 && DECL_INITIAL (label) != 0)
3685 || (DECL_CONTEXT (label) != current_function_decl
3686 && C_DECLARED_LABEL_FLAG (label))))
3688 error_at (location, "duplicate label %qD", label);
3689 locate_old_decl (label);
3690 return 0;
3692 else if (label && DECL_CONTEXT (label) == current_function_decl)
3694 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3696 /* The label has been used or declared already in this function,
3697 but not defined. Update its location to point to this
3698 definition. */
3699 DECL_SOURCE_LOCATION (label) = location;
3700 set_spot_bindings (&label_vars->label_bindings, true);
3702 /* Issue warnings as required about any goto statements from
3703 earlier in the function. */
3704 check_earlier_gotos (label, label_vars);
3706 else
3708 struct c_label_vars *label_vars;
3710 /* No label binding for that identifier; make one. */
3711 label = make_label (location, name, true, &label_vars);
3713 /* Ordinary labels go in the current function scope. */
3714 bind_label (name, label, current_function_scope, label_vars);
3717 if (!in_system_header_at (input_location) && lookup_name (name))
3718 warning_at (location, OPT_Wtraditional,
3719 "traditional C lacks a separate namespace "
3720 "for labels, identifier %qE conflicts", name);
3722 /* Mark label as having been defined. */
3723 DECL_INITIAL (label) = error_mark_node;
3724 return label;
3727 /* Get the bindings for a new switch statement. This is used to issue
3728 warnings as appropriate for jumps from the switch to case or
3729 default labels. */
3731 struct c_spot_bindings *
3732 c_get_switch_bindings (void)
3734 struct c_spot_bindings *switch_bindings;
3736 switch_bindings = XNEW (struct c_spot_bindings);
3737 set_spot_bindings (switch_bindings, true);
3738 return switch_bindings;
3741 void
3742 c_release_switch_bindings (struct c_spot_bindings *bindings)
3744 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3745 XDELETE (bindings);
3748 /* This is called at the point of a case or default label to issue
3749 warnings about decls as needed. It returns true if it found an
3750 error, not just a warning. */
3752 bool
3753 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3754 location_t switch_loc, location_t case_loc)
3756 bool saw_error;
3757 struct c_scope *scope;
3759 saw_error = false;
3760 for (scope = current_scope;
3761 scope != switch_bindings->scope;
3762 scope = scope->outer)
3764 struct c_binding *b;
3766 gcc_assert (scope != NULL);
3768 if (!scope->has_jump_unsafe_decl)
3769 continue;
3771 for (b = scope->bindings; b != NULL; b = b->prev)
3773 if (decl_jump_unsafe (b->decl))
3775 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3777 saw_error = true;
3778 error_at (case_loc,
3779 ("switch jumps into scope of identifier with "
3780 "variably modified type"));
3782 else
3783 warning_at (case_loc, OPT_Wjump_misses_init,
3784 "switch jumps over variable initialization");
3785 inform (switch_loc, "switch starts here");
3786 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3787 b->decl);
3792 if (switch_bindings->stmt_exprs > 0)
3794 saw_error = true;
3795 error_at (case_loc, "switch jumps into statement expression");
3796 inform (switch_loc, "switch starts here");
3799 return saw_error;
3802 /* Given NAME, an IDENTIFIER_NODE,
3803 return the structure (or union or enum) definition for that name.
3804 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3805 CODE says which kind of type the caller wants;
3806 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3807 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3808 location where the tag was defined.
3809 If the wrong kind of type is found, an error is reported. */
3811 static tree
3812 lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3813 location_t *ploc)
3815 struct c_binding *b = I_TAG_BINDING (name);
3816 bool thislevel = false;
3818 if (!b || !b->decl)
3819 return NULL_TREE;
3821 /* We only care about whether it's in this level if
3822 thislevel_only was set or it might be a type clash. */
3823 if (thislevel_only || TREE_CODE (b->decl) != code)
3825 /* For our purposes, a tag in the external scope is the same as
3826 a tag in the file scope. (Primarily relevant to Objective-C
3827 and its builtin structure tags, which get pushed before the
3828 file scope is created.) */
3829 if (B_IN_CURRENT_SCOPE (b)
3830 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3831 thislevel = true;
3834 if (thislevel_only && !thislevel)
3835 return NULL_TREE;
3837 if (TREE_CODE (b->decl) != code)
3839 /* Definition isn't the kind we were looking for. */
3840 pending_invalid_xref = name;
3841 pending_invalid_xref_location = input_location;
3843 /* If in the same binding level as a declaration as a tag
3844 of a different type, this must not be allowed to
3845 shadow that tag, so give the error immediately.
3846 (For example, "struct foo; union foo;" is invalid.) */
3847 if (thislevel)
3848 pending_xref_error ();
3851 if (ploc != NULL)
3852 *ploc = b->locus;
3854 return b->decl;
3857 /* Return true if a definition exists for NAME with code CODE. */
3859 bool
3860 tag_exists_p (enum tree_code code, tree name)
3862 struct c_binding *b = I_TAG_BINDING (name);
3864 if (b == NULL || b->decl == NULL_TREE)
3865 return false;
3866 return TREE_CODE (b->decl) == code;
3869 /* Print an error message now
3870 for a recent invalid struct, union or enum cross reference.
3871 We don't print them immediately because they are not invalid
3872 when used in the `struct foo;' construct for shadowing. */
3874 void
3875 pending_xref_error (void)
3877 if (pending_invalid_xref != 0)
3878 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3879 pending_invalid_xref);
3880 pending_invalid_xref = 0;
3884 /* Look up NAME in the current scope and its superiors
3885 in the namespace of variables, functions and typedefs.
3886 Return a ..._DECL node of some kind representing its definition,
3887 or return 0 if it is undefined. */
3889 tree
3890 lookup_name (tree name)
3892 struct c_binding *b = I_SYMBOL_BINDING (name);
3893 if (b && !b->invisible)
3895 maybe_record_typedef_use (b->decl);
3896 return b->decl;
3898 return NULL_TREE;
3901 /* Similar to `lookup_name' but look only at the indicated scope. */
3903 static tree
3904 lookup_name_in_scope (tree name, struct c_scope *scope)
3906 struct c_binding *b;
3908 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3909 if (B_IN_SCOPE (b, scope))
3910 return b->decl;
3911 return NULL_TREE;
3914 /* Create the predefined scalar types of C,
3915 and some nodes representing standard constants (0, 1, (void *) 0).
3916 Initialize the global scope.
3917 Make definitions for built-in primitive functions. */
3919 void
3920 c_init_decl_processing (void)
3922 location_t save_loc = input_location;
3924 /* Initialize reserved words for parser. */
3925 c_parse_init ();
3927 current_function_decl = 0;
3929 gcc_obstack_init (&parser_obstack);
3931 /* Make the externals scope. */
3932 push_scope ();
3933 external_scope = current_scope;
3935 /* Declarations from c_common_nodes_and_builtins must not be associated
3936 with this input file, lest we get differences between using and not
3937 using preprocessed headers. */
3938 input_location = BUILTINS_LOCATION;
3940 c_common_nodes_and_builtins ();
3942 /* In C, comparisons and TRUTH_* expressions have type int. */
3943 truthvalue_type_node = integer_type_node;
3944 truthvalue_true_node = integer_one_node;
3945 truthvalue_false_node = integer_zero_node;
3947 /* Even in C99, which has a real boolean type. */
3948 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3949 boolean_type_node));
3951 input_location = save_loc;
3953 make_fname_decl = c_make_fname_decl;
3954 start_fname_decls ();
3957 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3958 give the decl, NAME is the initialization string and TYPE_DEP
3959 indicates whether NAME depended on the type of the function. As we
3960 don't yet implement delayed emission of static data, we mark the
3961 decl as emitted so it is not placed in the output. Anything using
3962 it must therefore pull out the STRING_CST initializer directly.
3963 FIXME. */
3965 static tree
3966 c_make_fname_decl (location_t loc, tree id, int type_dep)
3968 const char *name = fname_as_string (type_dep);
3969 tree decl, type, init;
3970 size_t length = strlen (name);
3972 type = build_array_type (char_type_node,
3973 build_index_type (size_int (length)));
3974 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3976 decl = build_decl (loc, VAR_DECL, id, type);
3978 TREE_STATIC (decl) = 1;
3979 TREE_READONLY (decl) = 1;
3980 DECL_ARTIFICIAL (decl) = 1;
3982 init = build_string (length + 1, name);
3983 free (CONST_CAST (char *, name));
3984 TREE_TYPE (init) = type;
3985 DECL_INITIAL (decl) = init;
3987 TREE_USED (decl) = 1;
3989 if (current_function_decl
3990 /* For invalid programs like this:
3992 void foo()
3993 const char* p = __FUNCTION__;
3995 the __FUNCTION__ is believed to appear in K&R style function
3996 parameter declarator. In that case we still don't have
3997 function_scope. */
3998 && (!seen_error () || current_function_scope))
4000 DECL_CONTEXT (decl) = current_function_decl;
4001 bind (id, decl, current_function_scope,
4002 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4005 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4007 return decl;
4010 tree
4011 c_builtin_function (tree decl)
4013 tree type = TREE_TYPE (decl);
4014 tree id = DECL_NAME (decl);
4016 const char *name = IDENTIFIER_POINTER (id);
4017 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4019 /* Should never be called on a symbol with a preexisting meaning. */
4020 gcc_assert (!I_SYMBOL_BINDING (id));
4022 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4023 UNKNOWN_LOCATION);
4025 /* Builtins in the implementation namespace are made visible without
4026 needing to be explicitly declared. See push_file_scope. */
4027 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4029 DECL_CHAIN (decl) = visible_builtins;
4030 visible_builtins = decl;
4033 return decl;
4036 tree
4037 c_builtin_function_ext_scope (tree decl)
4039 tree type = TREE_TYPE (decl);
4040 tree id = DECL_NAME (decl);
4042 const char *name = IDENTIFIER_POINTER (id);
4043 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4045 if (external_scope)
4046 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4047 UNKNOWN_LOCATION);
4049 /* Builtins in the implementation namespace are made visible without
4050 needing to be explicitly declared. See push_file_scope. */
4051 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4053 DECL_CHAIN (decl) = visible_builtins;
4054 visible_builtins = decl;
4057 return decl;
4060 /* Called when a declaration is seen that contains no names to declare.
4061 If its type is a reference to a structure, union or enum inherited
4062 from a containing scope, shadow that tag name for the current scope
4063 with a forward reference.
4064 If its type defines a new named structure or union
4065 or defines an enum, it is valid but we need not do anything here.
4066 Otherwise, it is an error. */
4068 void
4069 shadow_tag (const struct c_declspecs *declspecs)
4071 shadow_tag_warned (declspecs, 0);
4074 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4075 but no pedwarn. */
4076 void
4077 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4079 bool found_tag = false;
4081 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4083 tree value = declspecs->type;
4084 enum tree_code code = TREE_CODE (value);
4086 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4087 /* Used to test also that TYPE_SIZE (value) != 0.
4088 That caused warning for `struct foo;' at top level in the file. */
4090 tree name = TYPE_NAME (value);
4091 tree t;
4093 found_tag = true;
4095 if (declspecs->restrict_p)
4097 error ("invalid use of %<restrict%>");
4098 warned = 1;
4101 if (name == 0)
4103 if (warned != 1 && code != ENUMERAL_TYPE)
4104 /* Empty unnamed enum OK */
4106 pedwarn (input_location, 0,
4107 "unnamed struct/union that defines no instances");
4108 warned = 1;
4111 else if (declspecs->typespec_kind != ctsk_tagdef
4112 && declspecs->typespec_kind != ctsk_tagfirstref
4113 && declspecs->storage_class != csc_none)
4115 if (warned != 1)
4116 pedwarn (input_location, 0,
4117 "empty declaration with storage class specifier "
4118 "does not redeclare tag");
4119 warned = 1;
4120 pending_xref_error ();
4122 else if (declspecs->typespec_kind != ctsk_tagdef
4123 && declspecs->typespec_kind != ctsk_tagfirstref
4124 && (declspecs->const_p
4125 || declspecs->volatile_p
4126 || declspecs->atomic_p
4127 || declspecs->restrict_p
4128 || declspecs->address_space))
4130 if (warned != 1)
4131 pedwarn (input_location, 0,
4132 "empty declaration with type qualifier "
4133 "does not redeclare tag");
4134 warned = 1;
4135 pending_xref_error ();
4137 else if (declspecs->typespec_kind != ctsk_tagdef
4138 && declspecs->typespec_kind != ctsk_tagfirstref
4139 && declspecs->alignas_p)
4141 if (warned != 1)
4142 pedwarn (input_location, 0,
4143 "empty declaration with %<_Alignas%> "
4144 "does not redeclare tag");
4145 warned = 1;
4146 pending_xref_error ();
4148 else
4150 pending_invalid_xref = 0;
4151 t = lookup_tag (code, name, true, NULL);
4153 if (t == NULL_TREE)
4155 t = make_node (code);
4156 pushtag (input_location, name, t);
4160 else
4162 if (warned != 1 && !in_system_header_at (input_location))
4164 pedwarn (input_location, 0,
4165 "useless type name in empty declaration");
4166 warned = 1;
4170 else if (warned != 1 && !in_system_header_at (input_location)
4171 && declspecs->typedef_p)
4173 pedwarn (input_location, 0, "useless type name in empty declaration");
4174 warned = 1;
4177 pending_invalid_xref = 0;
4179 if (declspecs->inline_p)
4181 error ("%<inline%> in empty declaration");
4182 warned = 1;
4185 if (declspecs->noreturn_p)
4187 error ("%<_Noreturn%> in empty declaration");
4188 warned = 1;
4191 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4193 error ("%<auto%> in file-scope empty declaration");
4194 warned = 1;
4197 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4199 error ("%<register%> in file-scope empty declaration");
4200 warned = 1;
4203 if (!warned && !in_system_header_at (input_location)
4204 && declspecs->storage_class != csc_none)
4206 warning (0, "useless storage class specifier in empty declaration");
4207 warned = 2;
4210 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4212 warning (0, "useless %qs in empty declaration",
4213 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4214 warned = 2;
4217 if (!warned
4218 && !in_system_header_at (input_location)
4219 && (declspecs->const_p
4220 || declspecs->volatile_p
4221 || declspecs->atomic_p
4222 || declspecs->restrict_p
4223 || declspecs->address_space))
4225 warning (0, "useless type qualifier in empty declaration");
4226 warned = 2;
4229 if (!warned && !in_system_header_at (input_location)
4230 && declspecs->alignas_p)
4232 warning (0, "useless %<_Alignas%> in empty declaration");
4233 warned = 2;
4236 if (warned != 1)
4238 if (!found_tag)
4239 pedwarn (input_location, 0, "empty declaration");
4244 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4245 bits. SPECS represents declaration specifiers that the grammar
4246 only permits to contain type qualifiers and attributes. */
4249 quals_from_declspecs (const struct c_declspecs *specs)
4251 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4252 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4253 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4254 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4255 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4256 gcc_assert (!specs->type
4257 && !specs->decl_attr
4258 && specs->typespec_word == cts_none
4259 && specs->storage_class == csc_none
4260 && !specs->typedef_p
4261 && !specs->explicit_signed_p
4262 && !specs->deprecated_p
4263 && !specs->long_p
4264 && !specs->long_long_p
4265 && !specs->short_p
4266 && !specs->signed_p
4267 && !specs->unsigned_p
4268 && !specs->complex_p
4269 && !specs->inline_p
4270 && !specs->noreturn_p
4271 && !specs->thread_p);
4272 return quals;
4275 /* Construct an array declarator. LOC is the location of the
4276 beginning of the array (usually the opening brace). EXPR is the
4277 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4278 inside the [] (to be applied to the pointer to which a parameter
4279 array is converted). STATIC_P is true if "static" is inside the
4280 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4281 VLA of unspecified length which is nevertheless a complete type,
4282 false otherwise. The field for the contained declarator is left to
4283 be filled in by set_array_declarator_inner. */
4285 struct c_declarator *
4286 build_array_declarator (location_t loc,
4287 tree expr, struct c_declspecs *quals, bool static_p,
4288 bool vla_unspec_p)
4290 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4291 struct c_declarator);
4292 declarator->id_loc = loc;
4293 declarator->kind = cdk_array;
4294 declarator->declarator = 0;
4295 declarator->u.array.dimen = expr;
4296 if (quals)
4298 declarator->u.array.attrs = quals->attrs;
4299 declarator->u.array.quals = quals_from_declspecs (quals);
4301 else
4303 declarator->u.array.attrs = NULL_TREE;
4304 declarator->u.array.quals = 0;
4306 declarator->u.array.static_p = static_p;
4307 declarator->u.array.vla_unspec_p = vla_unspec_p;
4308 if (static_p || quals != NULL)
4309 pedwarn_c90 (loc, OPT_Wpedantic,
4310 "ISO C90 does not support %<static%> or type "
4311 "qualifiers in parameter array declarators");
4312 if (vla_unspec_p)
4313 pedwarn_c90 (loc, OPT_Wpedantic,
4314 "ISO C90 does not support %<[*]%> array declarators");
4315 if (vla_unspec_p)
4317 if (!current_scope->parm_flag)
4319 /* C99 6.7.5.2p4 */
4320 error_at (loc, "%<[*]%> not allowed in other than "
4321 "function prototype scope");
4322 declarator->u.array.vla_unspec_p = false;
4323 return NULL;
4325 current_scope->had_vla_unspec = true;
4327 return declarator;
4330 /* Set the contained declarator of an array declarator. DECL is the
4331 declarator, as constructed by build_array_declarator; INNER is what
4332 appears on the left of the []. */
4334 struct c_declarator *
4335 set_array_declarator_inner (struct c_declarator *decl,
4336 struct c_declarator *inner)
4338 decl->declarator = inner;
4339 return decl;
4342 /* INIT is a constructor that forms DECL's initializer. If the final
4343 element initializes a flexible array field, add the size of that
4344 initializer to DECL's size. */
4346 static void
4347 add_flexible_array_elts_to_size (tree decl, tree init)
4349 tree elt, type;
4351 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4352 return;
4354 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4355 type = TREE_TYPE (elt);
4356 if (TREE_CODE (type) == ARRAY_TYPE
4357 && TYPE_SIZE (type) == NULL_TREE
4358 && TYPE_DOMAIN (type) != NULL_TREE
4359 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4361 complete_array_type (&type, elt, false);
4362 DECL_SIZE (decl)
4363 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4364 DECL_SIZE_UNIT (decl)
4365 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4369 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4370 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4371 before the type name, and set *EXPR_CONST_OPERANDS, if
4372 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4373 appear in a constant expression. */
4375 tree
4376 groktypename (struct c_type_name *type_name, tree *expr,
4377 bool *expr_const_operands)
4379 tree type;
4380 tree attrs = type_name->specs->attrs;
4382 type_name->specs->attrs = NULL_TREE;
4384 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4385 false, NULL, &attrs, expr, expr_const_operands,
4386 DEPRECATED_NORMAL);
4388 /* Apply attributes. */
4389 decl_attributes (&type, attrs, 0);
4391 return type;
4394 /* Wrapper for decl_attributes that adds some implicit attributes
4395 to VAR_DECLs or FUNCTION_DECLs. */
4397 static tree
4398 c_decl_attributes (tree *node, tree attributes, int flags)
4400 /* Add implicit "omp declare target" attribute if requested. */
4401 if (current_omp_declare_target_attribute
4402 && ((VAR_P (*node) && is_global_var (*node))
4403 || TREE_CODE (*node) == FUNCTION_DECL))
4405 if (VAR_P (*node)
4406 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4407 error ("%q+D in declare target directive does not have mappable type",
4408 *node);
4409 else
4410 attributes = tree_cons (get_identifier ("omp declare target"),
4411 NULL_TREE, attributes);
4413 return decl_attributes (node, attributes, flags);
4417 /* Decode a declarator in an ordinary declaration or data definition.
4418 This is called as soon as the type information and variable name
4419 have been parsed, before parsing the initializer if any.
4420 Here we create the ..._DECL node, fill in its type,
4421 and put it on the list of decls for the current context.
4422 The ..._DECL node is returned as the value.
4424 Exception: for arrays where the length is not specified,
4425 the type is left null, to be filled in by `finish_decl'.
4427 Function definitions do not come here; they go to start_function
4428 instead. However, external and forward declarations of functions
4429 do go through here. Structure field declarations are done by
4430 grokfield and not through here. */
4432 tree
4433 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4434 bool initialized, tree attributes)
4436 tree decl;
4437 tree tem;
4438 tree expr = NULL_TREE;
4439 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4441 /* An object declared as __attribute__((deprecated)) suppresses
4442 warnings of uses of other deprecated items. */
4443 if (lookup_attribute ("deprecated", attributes))
4444 deprecated_state = DEPRECATED_SUPPRESS;
4446 decl = grokdeclarator (declarator, declspecs,
4447 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4448 deprecated_state);
4449 if (!decl || decl == error_mark_node)
4450 return NULL_TREE;
4452 if (expr)
4453 add_stmt (fold_convert (void_type_node, expr));
4455 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4456 warning (OPT_Wmain, "%q+D is usually a function", decl);
4458 if (initialized)
4459 /* Is it valid for this decl to have an initializer at all?
4460 If not, set INITIALIZED to zero, which will indirectly
4461 tell 'finish_decl' to ignore the initializer once it is parsed. */
4462 switch (TREE_CODE (decl))
4464 case TYPE_DECL:
4465 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4466 initialized = 0;
4467 break;
4469 case FUNCTION_DECL:
4470 error ("function %qD is initialized like a variable", decl);
4471 initialized = 0;
4472 break;
4474 case PARM_DECL:
4475 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4476 error ("parameter %qD is initialized", decl);
4477 initialized = 0;
4478 break;
4480 default:
4481 /* Don't allow initializations for incomplete types except for
4482 arrays which might be completed by the initialization. */
4484 /* This can happen if the array size is an undefined macro.
4485 We already gave a warning, so we don't need another one. */
4486 if (TREE_TYPE (decl) == error_mark_node)
4487 initialized = 0;
4488 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4490 /* A complete type is ok if size is fixed. */
4492 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4493 || C_DECL_VARIABLE_SIZE (decl))
4495 error ("variable-sized object may not be initialized");
4496 initialized = 0;
4499 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4501 error ("variable %qD has initializer but incomplete type", decl);
4502 initialized = 0;
4504 else if (C_DECL_VARIABLE_SIZE (decl))
4506 /* Although C99 is unclear about whether incomplete arrays
4507 of VLAs themselves count as VLAs, it does not make
4508 sense to permit them to be initialized given that
4509 ordinary VLAs may not be initialized. */
4510 error ("variable-sized object may not be initialized");
4511 initialized = 0;
4515 if (initialized)
4517 if (current_scope == file_scope)
4518 TREE_STATIC (decl) = 1;
4520 /* Tell 'pushdecl' this is an initialized decl
4521 even though we don't yet have the initializer expression.
4522 Also tell 'finish_decl' it may store the real initializer. */
4523 DECL_INITIAL (decl) = error_mark_node;
4526 /* If this is a function declaration, write a record describing it to the
4527 prototypes file (if requested). */
4529 if (TREE_CODE (decl) == FUNCTION_DECL)
4530 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4532 /* ANSI specifies that a tentative definition which is not merged with
4533 a non-tentative definition behaves exactly like a definition with an
4534 initializer equal to zero. (Section 3.7.2)
4536 -fno-common gives strict ANSI behavior, though this tends to break
4537 a large body of code that grew up without this rule.
4539 Thread-local variables are never common, since there's no entrenched
4540 body of code to break, and it allows more efficient variable references
4541 in the presence of dynamic linking. */
4543 if (VAR_P (decl)
4544 && !initialized
4545 && TREE_PUBLIC (decl)
4546 && !DECL_THREAD_LOCAL_P (decl)
4547 && !flag_no_common)
4548 DECL_COMMON (decl) = 1;
4550 /* Set attributes here so if duplicate decl, will have proper attributes. */
4551 c_decl_attributes (&decl, attributes, 0);
4553 /* Handle gnu_inline attribute. */
4554 if (declspecs->inline_p
4555 && !flag_gnu89_inline
4556 && TREE_CODE (decl) == FUNCTION_DECL
4557 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4558 || current_function_decl))
4560 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4562 else if (declspecs->storage_class != csc_static)
4563 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4566 if (TREE_CODE (decl) == FUNCTION_DECL
4567 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4569 struct c_declarator *ce = declarator;
4571 if (ce->kind == cdk_pointer)
4572 ce = declarator->declarator;
4573 if (ce->kind == cdk_function)
4575 tree args = ce->u.arg_info->parms;
4576 for (; args; args = DECL_CHAIN (args))
4578 tree type = TREE_TYPE (args);
4579 if (type && INTEGRAL_TYPE_P (type)
4580 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4581 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4586 if (TREE_CODE (decl) == FUNCTION_DECL
4587 && DECL_DECLARED_INLINE_P (decl)
4588 && DECL_UNINLINABLE (decl)
4589 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4590 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4591 decl);
4593 /* C99 6.7.4p3: An inline definition of a function with external
4594 linkage shall not contain a definition of a modifiable object
4595 with static storage duration... */
4596 if (VAR_P (decl)
4597 && current_scope != file_scope
4598 && TREE_STATIC (decl)
4599 && !TREE_READONLY (decl)
4600 && DECL_DECLARED_INLINE_P (current_function_decl)
4601 && DECL_EXTERNAL (current_function_decl))
4602 record_inline_static (input_location, current_function_decl,
4603 decl, csi_modifiable);
4605 if (c_dialect_objc ()
4606 && VAR_OR_FUNCTION_DECL_P (decl))
4607 objc_check_global_decl (decl);
4609 /* Add this decl to the current scope.
4610 TEM may equal DECL or it may be a previous decl of the same name. */
4611 tem = pushdecl (decl);
4613 if (initialized && DECL_EXTERNAL (tem))
4615 DECL_EXTERNAL (tem) = 0;
4616 TREE_STATIC (tem) = 1;
4619 return tem;
4622 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4623 DECL or the non-array element type if DECL is an uninitialized array.
4624 If that type has a const member, diagnose this. */
4626 static void
4627 diagnose_uninitialized_cst_member (tree decl, tree type)
4629 tree field;
4630 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4632 tree field_type;
4633 if (TREE_CODE (field) != FIELD_DECL)
4634 continue;
4635 field_type = strip_array_types (TREE_TYPE (field));
4637 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4639 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4640 "uninitialized const member in %qT is invalid in C++",
4641 strip_array_types (TREE_TYPE (decl)));
4642 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4645 if (RECORD_OR_UNION_TYPE_P (field_type))
4646 diagnose_uninitialized_cst_member (decl, field_type);
4650 /* Finish processing of a declaration;
4651 install its initial value.
4652 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4653 If the length of an array type is not known before,
4654 it must be determined now, from the initial value, or it is an error.
4656 INIT_LOC is the location of the initial value. */
4658 void
4659 finish_decl (tree decl, location_t init_loc, tree init,
4660 tree origtype, tree asmspec_tree)
4662 tree type;
4663 bool was_incomplete = (DECL_SIZE (decl) == 0);
4664 const char *asmspec = 0;
4666 /* If a name was specified, get the string. */
4667 if (VAR_OR_FUNCTION_DECL_P (decl)
4668 && DECL_FILE_SCOPE_P (decl))
4669 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4670 if (asmspec_tree)
4671 asmspec = TREE_STRING_POINTER (asmspec_tree);
4673 if (VAR_P (decl)
4674 && TREE_STATIC (decl)
4675 && global_bindings_p ())
4676 /* So decl is a global variable. Record the types it uses
4677 so that we can decide later to emit debug info for them. */
4678 record_types_used_by_current_var_decl (decl);
4680 /* If `start_decl' didn't like having an initialization, ignore it now. */
4681 if (init != 0 && DECL_INITIAL (decl) == 0)
4682 init = 0;
4684 /* Don't crash if parm is initialized. */
4685 if (TREE_CODE (decl) == PARM_DECL)
4686 init = 0;
4688 if (init)
4689 store_init_value (init_loc, decl, init, origtype);
4691 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4692 || TREE_CODE (decl) == FIELD_DECL))
4693 objc_check_decl (decl);
4695 type = TREE_TYPE (decl);
4697 /* Deduce size of array from initialization, if not already known. */
4698 if (TREE_CODE (type) == ARRAY_TYPE
4699 && TYPE_DOMAIN (type) == 0
4700 && TREE_CODE (decl) != TYPE_DECL)
4702 bool do_default
4703 = (TREE_STATIC (decl)
4704 /* Even if pedantic, an external linkage array
4705 may have incomplete type at first. */
4706 ? pedantic && !TREE_PUBLIC (decl)
4707 : !DECL_EXTERNAL (decl));
4708 int failure
4709 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4710 do_default);
4712 /* Get the completed type made by complete_array_type. */
4713 type = TREE_TYPE (decl);
4715 switch (failure)
4717 case 1:
4718 error ("initializer fails to determine size of %q+D", decl);
4719 break;
4721 case 2:
4722 if (do_default)
4723 error ("array size missing in %q+D", decl);
4724 /* If a `static' var's size isn't known,
4725 make it extern as well as static, so it does not get
4726 allocated.
4727 If it is not `static', then do not mark extern;
4728 finish_incomplete_decl will give it a default size
4729 and it will get allocated. */
4730 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4731 DECL_EXTERNAL (decl) = 1;
4732 break;
4734 case 3:
4735 error ("zero or negative size array %q+D", decl);
4736 break;
4738 case 0:
4739 /* For global variables, update the copy of the type that
4740 exists in the binding. */
4741 if (TREE_PUBLIC (decl))
4743 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4744 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4745 b_ext = b_ext->shadowed;
4746 if (b_ext)
4748 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4749 b_ext->u.type = composite_type (b_ext->u.type, type);
4750 else
4751 b_ext->u.type = type;
4754 break;
4756 default:
4757 gcc_unreachable ();
4760 if (DECL_INITIAL (decl))
4761 TREE_TYPE (DECL_INITIAL (decl)) = type;
4763 relayout_decl (decl);
4766 if (VAR_P (decl))
4768 if (init && TREE_CODE (init) == CONSTRUCTOR)
4769 add_flexible_array_elts_to_size (decl, init);
4771 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4772 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4773 layout_decl (decl, 0);
4775 if (DECL_SIZE (decl) == 0
4776 /* Don't give an error if we already gave one earlier. */
4777 && TREE_TYPE (decl) != error_mark_node
4778 && (TREE_STATIC (decl)
4779 /* A static variable with an incomplete type
4780 is an error if it is initialized.
4781 Also if it is not file scope.
4782 Otherwise, let it through, but if it is not `extern'
4783 then it may cause an error message later. */
4784 ? (DECL_INITIAL (decl) != 0
4785 || !DECL_FILE_SCOPE_P (decl))
4786 /* An automatic variable with an incomplete type
4787 is an error. */
4788 : !DECL_EXTERNAL (decl)))
4790 error ("storage size of %q+D isn%'t known", decl);
4791 TREE_TYPE (decl) = error_mark_node;
4794 if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
4795 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
4796 && DECL_SIZE (decl) == NULL_TREE
4797 && TREE_STATIC (decl))
4798 incomplete_record_decls.safe_push (decl);
4800 if (is_global_var (decl) && DECL_SIZE (decl) != 0)
4802 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4803 constant_expression_warning (DECL_SIZE (decl));
4804 else
4806 error ("storage size of %q+D isn%'t constant", decl);
4807 TREE_TYPE (decl) = error_mark_node;
4811 if (TREE_USED (type))
4813 TREE_USED (decl) = 1;
4814 DECL_READ_P (decl) = 1;
4818 /* If this is a function and an assembler name is specified, reset DECL_RTL
4819 so we can give it its new name. Also, update builtin_decl if it
4820 was a normal built-in. */
4821 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4823 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4824 set_builtin_user_assembler_name (decl, asmspec);
4825 set_user_assembler_name (decl, asmspec);
4828 /* If #pragma weak was used, mark the decl weak now. */
4829 maybe_apply_pragma_weak (decl);
4831 /* Output the assembler code and/or RTL code for variables and functions,
4832 unless the type is an undefined structure or union.
4833 If not, it will get done when the type is completed. */
4835 if (VAR_OR_FUNCTION_DECL_P (decl))
4837 /* Determine the ELF visibility. */
4838 if (TREE_PUBLIC (decl))
4839 c_determine_visibility (decl);
4841 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4842 if (c_dialect_objc ())
4843 objc_check_decl (decl);
4845 if (asmspec)
4847 /* If this is not a static variable, issue a warning.
4848 It doesn't make any sense to give an ASMSPEC for an
4849 ordinary, non-register local variable. Historically,
4850 GCC has accepted -- but ignored -- the ASMSPEC in
4851 this case. */
4852 if (!DECL_FILE_SCOPE_P (decl)
4853 && VAR_P (decl)
4854 && !C_DECL_REGISTER (decl)
4855 && !TREE_STATIC (decl))
4856 warning (0, "ignoring asm-specifier for non-static local "
4857 "variable %q+D", decl);
4858 else
4859 set_user_assembler_name (decl, asmspec);
4862 if (DECL_FILE_SCOPE_P (decl))
4864 if (DECL_INITIAL (decl) == NULL_TREE
4865 || DECL_INITIAL (decl) == error_mark_node)
4866 /* Don't output anything
4867 when a tentative file-scope definition is seen.
4868 But at end of compilation, do output code for them. */
4869 DECL_DEFER_OUTPUT (decl) = 1;
4870 if (asmspec && C_DECL_REGISTER (decl))
4871 DECL_HARD_REGISTER (decl) = 1;
4872 rest_of_decl_compilation (decl, true, 0);
4874 else
4876 /* In conjunction with an ASMSPEC, the `register'
4877 keyword indicates that we should place the variable
4878 in a particular register. */
4879 if (asmspec && C_DECL_REGISTER (decl))
4881 DECL_HARD_REGISTER (decl) = 1;
4882 /* This cannot be done for a structure with volatile
4883 fields, on which DECL_REGISTER will have been
4884 reset. */
4885 if (!DECL_REGISTER (decl))
4886 error ("cannot put object with volatile field into register");
4889 if (TREE_CODE (decl) != FUNCTION_DECL)
4891 /* If we're building a variable sized type, and we might be
4892 reachable other than via the top of the current binding
4893 level, then create a new BIND_EXPR so that we deallocate
4894 the object at the right time. */
4895 /* Note that DECL_SIZE can be null due to errors. */
4896 if (DECL_SIZE (decl)
4897 && !TREE_CONSTANT (DECL_SIZE (decl))
4898 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4900 tree bind;
4901 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4902 TREE_SIDE_EFFECTS (bind) = 1;
4903 add_stmt (bind);
4904 BIND_EXPR_BODY (bind) = push_stmt_list ();
4906 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4907 DECL_EXPR, decl));
4912 if (!DECL_FILE_SCOPE_P (decl))
4914 /* Recompute the RTL of a local array now
4915 if it used to be an incomplete type. */
4916 if (was_incomplete && !is_global_var (decl))
4918 /* If we used it already as memory, it must stay in memory. */
4919 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4920 /* If it's still incomplete now, no init will save it. */
4921 if (DECL_SIZE (decl) == 0)
4922 DECL_INITIAL (decl) = 0;
4927 if (TREE_CODE (decl) == TYPE_DECL)
4929 if (!DECL_FILE_SCOPE_P (decl)
4930 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4931 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4933 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4936 /* Install a cleanup (aka destructor) if one was given. */
4937 if (VAR_P (decl) && !TREE_STATIC (decl))
4939 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4940 if (attr)
4942 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4943 tree cleanup_decl = lookup_name (cleanup_id);
4944 tree cleanup;
4945 vec<tree, va_gc> *v;
4947 /* Build "cleanup(&decl)" for the destructor. */
4948 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4949 vec_alloc (v, 1);
4950 v->quick_push (cleanup);
4951 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4952 vNULL, cleanup_decl, v, NULL);
4953 vec_free (v);
4955 /* Don't warn about decl unused; the cleanup uses it. */
4956 TREE_USED (decl) = 1;
4957 TREE_USED (cleanup_decl) = 1;
4958 DECL_READ_P (decl) = 1;
4960 push_cleanup (decl, cleanup, false);
4964 if (warn_cxx_compat
4965 && VAR_P (decl)
4966 && !DECL_EXTERNAL (decl)
4967 && DECL_INITIAL (decl) == NULL_TREE)
4969 type = strip_array_types (type);
4970 if (TREE_READONLY (decl))
4971 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4972 "uninitialized const %qD is invalid in C++", decl);
4973 else if (RECORD_OR_UNION_TYPE_P (type)
4974 && C_TYPE_FIELDS_READONLY (type))
4975 diagnose_uninitialized_cst_member (decl, type);
4978 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4981 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4982 EXPR is NULL or a pointer to an expression that needs to be
4983 evaluated for the side effects of array size expressions in the
4984 parameters. */
4986 tree
4987 grokparm (const struct c_parm *parm, tree *expr)
4989 tree attrs = parm->attrs;
4990 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4991 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4993 decl_attributes (&decl, attrs, 0);
4995 return decl;
4998 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4999 and push that on the current scope. EXPR is a pointer to an
5000 expression that needs to be evaluated for the side effects of array
5001 size expressions in the parameters. */
5003 void
5004 push_parm_decl (const struct c_parm *parm, tree *expr)
5006 tree attrs = parm->attrs;
5007 tree decl;
5009 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5010 &attrs, expr, NULL, DEPRECATED_NORMAL);
5011 decl_attributes (&decl, attrs, 0);
5013 decl = pushdecl (decl);
5015 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5018 /* Mark all the parameter declarations to date as forward decls.
5019 Also diagnose use of this extension. */
5021 void
5022 mark_forward_parm_decls (void)
5024 struct c_binding *b;
5026 if (pedantic && !current_scope->warned_forward_parm_decls)
5028 pedwarn (input_location, OPT_Wpedantic,
5029 "ISO C forbids forward parameter declarations");
5030 current_scope->warned_forward_parm_decls = true;
5033 for (b = current_scope->bindings; b; b = b->prev)
5034 if (TREE_CODE (b->decl) == PARM_DECL)
5035 TREE_ASM_WRITTEN (b->decl) = 1;
5038 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5039 literal, which may be an incomplete array type completed by the
5040 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5041 literal. NON_CONST is true if the initializers contain something
5042 that cannot occur in a constant expression. */
5044 tree
5045 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5047 /* We do not use start_decl here because we have a type, not a declarator;
5048 and do not use finish_decl because the decl should be stored inside
5049 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5050 tree decl;
5051 tree complit;
5052 tree stmt;
5054 if (type == error_mark_node
5055 || init == error_mark_node)
5056 return error_mark_node;
5058 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5059 DECL_EXTERNAL (decl) = 0;
5060 TREE_PUBLIC (decl) = 0;
5061 TREE_STATIC (decl) = (current_scope == file_scope);
5062 DECL_CONTEXT (decl) = current_function_decl;
5063 TREE_USED (decl) = 1;
5064 DECL_READ_P (decl) = 1;
5065 TREE_TYPE (decl) = type;
5066 TREE_READONLY (decl) = (TYPE_READONLY (type)
5067 || (TREE_CODE (type) == ARRAY_TYPE
5068 && TYPE_READONLY (TREE_TYPE (type))));
5069 store_init_value (loc, decl, init, NULL_TREE);
5071 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5073 int failure = complete_array_type (&TREE_TYPE (decl),
5074 DECL_INITIAL (decl), true);
5075 /* If complete_array_type returns 3, it means that the
5076 initial value of the compound literal is empty. Allow it. */
5077 gcc_assert (failure == 0 || failure == 3);
5079 type = TREE_TYPE (decl);
5080 TREE_TYPE (DECL_INITIAL (decl)) = type;
5083 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5085 c_incomplete_type_error (NULL_TREE, type);
5086 return error_mark_node;
5089 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5090 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5091 TREE_SIDE_EFFECTS (complit) = 1;
5093 layout_decl (decl, 0);
5095 if (TREE_STATIC (decl))
5097 /* This decl needs a name for the assembler output. */
5098 set_compound_literal_name (decl);
5099 DECL_DEFER_OUTPUT (decl) = 1;
5100 DECL_COMDAT (decl) = 1;
5101 DECL_ARTIFICIAL (decl) = 1;
5102 DECL_IGNORED_P (decl) = 1;
5103 pushdecl (decl);
5104 rest_of_decl_compilation (decl, 1, 0);
5107 if (non_const)
5109 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5110 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5113 return complit;
5116 /* Check the type of a compound literal. Here we just check that it
5117 is valid for C++. */
5119 void
5120 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5122 if (warn_cxx_compat
5123 && (type_name->specs->typespec_kind == ctsk_tagdef
5124 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5125 warning_at (loc, OPT_Wc___compat,
5126 "defining a type in a compound literal is invalid in C++");
5129 /* Determine whether TYPE is a structure with a flexible array member,
5130 or a union containing such a structure (possibly recursively). */
5132 static bool
5133 flexible_array_type_p (tree type)
5135 tree x;
5136 switch (TREE_CODE (type))
5138 case RECORD_TYPE:
5139 x = TYPE_FIELDS (type);
5140 if (x == NULL_TREE)
5141 return false;
5142 while (DECL_CHAIN (x) != NULL_TREE)
5143 x = DECL_CHAIN (x);
5144 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5145 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5146 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5147 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5148 return true;
5149 return false;
5150 case UNION_TYPE:
5151 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5153 if (flexible_array_type_p (TREE_TYPE (x)))
5154 return true;
5156 return false;
5157 default:
5158 return false;
5162 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5163 replacing with appropriate values if they are invalid. */
5165 static void
5166 check_bitfield_type_and_width (location_t loc, tree *type, tree *width,
5167 tree orig_name)
5169 tree type_mv;
5170 unsigned int max_width;
5171 unsigned HOST_WIDE_INT w;
5172 const char *name = (orig_name
5173 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5174 : _("<anonymous>"));
5176 /* Detect and ignore out of range field width and process valid
5177 field widths. */
5178 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5180 error_at (loc, "bit-field %qs width not an integer constant", name);
5181 *width = integer_one_node;
5183 else
5185 if (TREE_CODE (*width) != INTEGER_CST)
5187 *width = c_fully_fold (*width, false, NULL);
5188 if (TREE_CODE (*width) == INTEGER_CST)
5189 pedwarn (loc, OPT_Wpedantic,
5190 "bit-field %qs width not an integer constant expression",
5191 name);
5193 if (TREE_CODE (*width) != INTEGER_CST)
5195 error_at (loc, "bit-field %qs width not an integer constant", name);
5196 *width = integer_one_node;
5198 constant_expression_warning (*width);
5199 if (tree_int_cst_sgn (*width) < 0)
5201 error_at (loc, "negative width in bit-field %qs", name);
5202 *width = integer_one_node;
5204 else if (integer_zerop (*width) && orig_name)
5206 error_at (loc, "zero width for bit-field %qs", name);
5207 *width = integer_one_node;
5211 /* Detect invalid bit-field type. */
5212 if (TREE_CODE (*type) != INTEGER_TYPE
5213 && TREE_CODE (*type) != BOOLEAN_TYPE
5214 && TREE_CODE (*type) != ENUMERAL_TYPE)
5216 error_at (loc, "bit-field %qs has invalid type", name);
5217 *type = unsigned_type_node;
5220 type_mv = TYPE_MAIN_VARIANT (*type);
5221 if (!in_system_header_at (input_location)
5222 && type_mv != integer_type_node
5223 && type_mv != unsigned_type_node
5224 && type_mv != boolean_type_node)
5225 pedwarn_c90 (loc, OPT_Wpedantic,
5226 "type of bit-field %qs is a GCC extension", name);
5228 max_width = TYPE_PRECISION (*type);
5230 if (0 < compare_tree_int (*width, max_width))
5232 error_at (loc, "width of %qs exceeds its type", name);
5233 w = max_width;
5234 *width = build_int_cst (integer_type_node, w);
5236 else
5237 w = tree_to_uhwi (*width);
5239 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5241 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5242 if (!lt
5243 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5244 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5245 warning_at (loc, 0, "%qs is narrower than values of its type", name);
5251 /* Print warning about variable length array if necessary. */
5253 static void
5254 warn_variable_length_array (tree name, tree size)
5256 if (TREE_CONSTANT (size))
5258 if (name)
5259 pedwarn_c90 (input_location, OPT_Wvla,
5260 "ISO C90 forbids array %qE whose size "
5261 "can%'t be evaluated", name);
5262 else
5263 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5264 "whose size can%'t be evaluated");
5266 else
5268 if (name)
5269 pedwarn_c90 (input_location, OPT_Wvla,
5270 "ISO C90 forbids variable length array %qE", name);
5271 else
5272 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5273 "length array");
5277 /* Print warning about defaulting to int if necessary. */
5279 static void
5280 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5282 diagnostic_info diagnostic;
5283 va_list ap;
5284 rich_location richloc (line_table, location);
5286 va_start (ap, gmsgid);
5287 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
5288 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5289 diagnostic.option_index = opt;
5290 report_diagnostic (&diagnostic);
5291 va_end (ap);
5294 /* Given declspecs and a declarator,
5295 determine the name and type of the object declared
5296 and construct a ..._DECL node for it.
5297 (In one case we can return a ..._TYPE node instead.
5298 For invalid input we sometimes return 0.)
5300 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5302 DECL_CONTEXT says which syntactic context this declaration is in:
5303 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5304 FUNCDEF for a function definition. Like NORMAL but a few different
5305 error messages in each case. Return value may be zero meaning
5306 this definition is too screwy to try to parse.
5307 PARM for a parameter declaration (either within a function prototype
5308 or before a function body). Make a PARM_DECL, or return void_type_node.
5309 TYPENAME if for a typename (in a cast or sizeof).
5310 Don't make a DECL node; just return the ..._TYPE node.
5311 FIELD for a struct or union field; make a FIELD_DECL.
5312 INITIALIZED is true if the decl has an initializer.
5313 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5314 representing the width of the bit-field.
5315 DECL_ATTRS points to the list of attributes that should be added to this
5316 decl. Any nested attributes that belong on the decl itself will be
5317 added to this list.
5318 If EXPR is not NULL, any expressions that need to be evaluated as
5319 part of evaluating variably modified types will be stored in *EXPR.
5320 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5321 set to indicate whether operands in *EXPR can be used in constant
5322 expressions.
5323 DEPRECATED_STATE is a deprecated_states value indicating whether
5324 deprecation warnings should be suppressed.
5326 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5327 It may also be so in the PARM case, for a prototype where the
5328 argument type is specified but not the name.
5330 This function is where the complicated C meanings of `static'
5331 and `extern' are interpreted. */
5333 static tree
5334 grokdeclarator (const struct c_declarator *declarator,
5335 struct c_declspecs *declspecs,
5336 enum decl_context decl_context, bool initialized, tree *width,
5337 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5338 enum deprecated_states deprecated_state)
5340 tree type = declspecs->type;
5341 bool threadp = declspecs->thread_p;
5342 enum c_storage_class storage_class = declspecs->storage_class;
5343 int constp;
5344 int restrictp;
5345 int volatilep;
5346 int atomicp;
5347 int type_quals = TYPE_UNQUALIFIED;
5348 tree name = NULL_TREE;
5349 bool funcdef_flag = false;
5350 bool funcdef_syntax = false;
5351 bool size_varies = false;
5352 tree decl_attr = declspecs->decl_attr;
5353 int array_ptr_quals = TYPE_UNQUALIFIED;
5354 tree array_ptr_attrs = NULL_TREE;
5355 int array_parm_static = 0;
5356 bool array_parm_vla_unspec_p = false;
5357 tree returned_attrs = NULL_TREE;
5358 bool bitfield = width != NULL;
5359 tree element_type;
5360 tree orig_qual_type = NULL;
5361 size_t orig_qual_indirect = 0;
5362 struct c_arg_info *arg_info = 0;
5363 addr_space_t as1, as2, address_space;
5364 location_t loc = UNKNOWN_LOCATION;
5365 const char *errmsg;
5366 tree expr_dummy;
5367 bool expr_const_operands_dummy;
5368 enum c_declarator_kind first_non_attr_kind;
5369 unsigned int alignas_align = 0;
5371 if (TREE_CODE (type) == ERROR_MARK)
5372 return error_mark_node;
5373 if (expr == NULL)
5374 expr = &expr_dummy;
5375 if (expr_const_operands == NULL)
5376 expr_const_operands = &expr_const_operands_dummy;
5378 *expr = declspecs->expr;
5379 *expr_const_operands = declspecs->expr_const_operands;
5381 if (decl_context == FUNCDEF)
5382 funcdef_flag = true, decl_context = NORMAL;
5384 /* Look inside a declarator for the name being declared
5385 and get it as an IDENTIFIER_NODE, for an error message. */
5387 const struct c_declarator *decl = declarator;
5389 first_non_attr_kind = cdk_attrs;
5390 while (decl)
5391 switch (decl->kind)
5393 case cdk_array:
5394 loc = decl->id_loc;
5395 /* FALL THRU. */
5397 case cdk_function:
5398 case cdk_pointer:
5399 funcdef_syntax = (decl->kind == cdk_function);
5400 if (first_non_attr_kind == cdk_attrs)
5401 first_non_attr_kind = decl->kind;
5402 decl = decl->declarator;
5403 break;
5405 case cdk_attrs:
5406 decl = decl->declarator;
5407 break;
5409 case cdk_id:
5410 loc = decl->id_loc;
5411 if (decl->u.id)
5412 name = decl->u.id;
5413 if (first_non_attr_kind == cdk_attrs)
5414 first_non_attr_kind = decl->kind;
5415 decl = 0;
5416 break;
5418 default:
5419 gcc_unreachable ();
5421 if (name == 0)
5423 gcc_assert (decl_context == PARM
5424 || decl_context == TYPENAME
5425 || (decl_context == FIELD
5426 && declarator->kind == cdk_id));
5427 gcc_assert (!initialized);
5431 /* A function definition's declarator must have the form of
5432 a function declarator. */
5434 if (funcdef_flag && !funcdef_syntax)
5435 return 0;
5437 /* If this looks like a function definition, make it one,
5438 even if it occurs where parms are expected.
5439 Then store_parm_decls will reject it and not use it as a parm. */
5440 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5441 decl_context = PARM;
5443 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5444 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5446 if ((decl_context == NORMAL || decl_context == FIELD)
5447 && current_scope == file_scope
5448 && variably_modified_type_p (type, NULL_TREE))
5450 if (name)
5451 error_at (loc, "variably modified %qE at file scope", name);
5452 else
5453 error_at (loc, "variably modified field at file scope");
5454 type = integer_type_node;
5457 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5459 /* Diagnose defaulting to "int". */
5461 if (declspecs->default_int_p && !in_system_header_at (input_location))
5463 /* Issue a warning if this is an ISO C 99 program or if
5464 -Wreturn-type and this is a function, or if -Wimplicit;
5465 prefer the former warning since it is more explicit. */
5466 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5467 && funcdef_flag)
5468 warn_about_return_type = 1;
5469 else
5471 if (name)
5472 warn_defaults_to (loc, OPT_Wimplicit_int,
5473 "type defaults to %<int%> in declaration "
5474 "of %qE", name);
5475 else
5476 warn_defaults_to (loc, OPT_Wimplicit_int,
5477 "type defaults to %<int%> in type name");
5481 /* Adjust the type if a bit-field is being declared,
5482 -funsigned-bitfields applied and the type is not explicitly
5483 "signed". */
5484 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5485 && TREE_CODE (type) == INTEGER_TYPE)
5486 type = unsigned_type_for (type);
5488 /* Figure out the type qualifiers for the declaration. There are
5489 two ways a declaration can become qualified. One is something
5490 like `const int i' where the `const' is explicit. Another is
5491 something like `typedef const int CI; CI i' where the type of the
5492 declaration contains the `const'. A third possibility is that
5493 there is a type qualifier on the element type of a typedefed
5494 array type, in which case we should extract that qualifier so
5495 that c_apply_type_quals_to_decl receives the full list of
5496 qualifiers to work with (C90 is not entirely clear about whether
5497 duplicate qualifiers should be diagnosed in this case, but it
5498 seems most appropriate to do so). */
5499 element_type = strip_array_types (type);
5500 constp = declspecs->const_p + TYPE_READONLY (element_type);
5501 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5502 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5503 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5504 as1 = declspecs->address_space;
5505 as2 = TYPE_ADDR_SPACE (element_type);
5506 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5508 if (constp > 1)
5509 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5510 if (restrictp > 1)
5511 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5512 if (volatilep > 1)
5513 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5514 if (atomicp > 1)
5515 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5517 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5518 error_at (loc, "conflicting named address spaces (%s vs %s)",
5519 c_addr_space_name (as1), c_addr_space_name (as2));
5521 if ((TREE_CODE (type) == ARRAY_TYPE
5522 || first_non_attr_kind == cdk_array)
5523 && TYPE_QUALS (element_type))
5525 orig_qual_type = type;
5526 type = TYPE_MAIN_VARIANT (type);
5528 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5529 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5530 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5531 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5532 | ENCODE_QUAL_ADDR_SPACE (address_space));
5533 if (type_quals != TYPE_QUALS (element_type))
5534 orig_qual_type = NULL_TREE;
5536 /* Applying the _Atomic qualifier to an array type (through the use
5537 of typedefs or typeof) must be detected here. If the qualifier
5538 is introduced later, any appearance of applying it to an array is
5539 actually applying it to an element of that array. */
5540 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5541 error_at (loc, "%<_Atomic%>-qualified array type");
5543 /* Warn about storage classes that are invalid for certain
5544 kinds of declarations (parameters, typenames, etc.). */
5546 if (funcdef_flag
5547 && (threadp
5548 || storage_class == csc_auto
5549 || storage_class == csc_register
5550 || storage_class == csc_typedef))
5552 if (storage_class == csc_auto)
5553 pedwarn (loc,
5554 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5555 "function definition declared %<auto%>");
5556 if (storage_class == csc_register)
5557 error_at (loc, "function definition declared %<register%>");
5558 if (storage_class == csc_typedef)
5559 error_at (loc, "function definition declared %<typedef%>");
5560 if (threadp)
5561 error_at (loc, "function definition declared %qs",
5562 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5563 threadp = false;
5564 if (storage_class == csc_auto
5565 || storage_class == csc_register
5566 || storage_class == csc_typedef)
5567 storage_class = csc_none;
5569 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5571 if (decl_context == PARM && storage_class == csc_register)
5573 else
5575 switch (decl_context)
5577 case FIELD:
5578 if (name)
5579 error_at (loc, "storage class specified for structure "
5580 "field %qE", name);
5581 else
5582 error_at (loc, "storage class specified for structure field");
5583 break;
5584 case PARM:
5585 if (name)
5586 error_at (loc, "storage class specified for parameter %qE",
5587 name);
5588 else
5589 error_at (loc, "storage class specified for unnamed parameter");
5590 break;
5591 default:
5592 error_at (loc, "storage class specified for typename");
5593 break;
5595 storage_class = csc_none;
5596 threadp = false;
5599 else if (storage_class == csc_extern
5600 && initialized
5601 && !funcdef_flag)
5603 /* 'extern' with initialization is invalid if not at file scope. */
5604 if (current_scope == file_scope)
5606 /* It is fine to have 'extern const' when compiling at C
5607 and C++ intersection. */
5608 if (!(warn_cxx_compat && constp))
5609 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5610 name);
5612 else
5613 error_at (loc, "%qE has both %<extern%> and initializer", name);
5615 else if (current_scope == file_scope)
5617 if (storage_class == csc_auto)
5618 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5619 name);
5620 if (pedantic && storage_class == csc_register)
5621 pedwarn (input_location, OPT_Wpedantic,
5622 "file-scope declaration of %qE specifies %<register%>", name);
5624 else
5626 if (storage_class == csc_extern && funcdef_flag)
5627 error_at (loc, "nested function %qE declared %<extern%>", name);
5628 else if (threadp && storage_class == csc_none)
5630 error_at (loc, "function-scope %qE implicitly auto and declared "
5631 "%qs", name,
5632 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5633 threadp = false;
5637 /* Now figure out the structure of the declarator proper.
5638 Descend through it, creating more complex types, until we reach
5639 the declared identifier (or NULL_TREE, in an absolute declarator).
5640 At each stage we maintain an unqualified version of the type
5641 together with any qualifiers that should be applied to it with
5642 c_build_qualified_type; this way, array types including
5643 multidimensional array types are first built up in unqualified
5644 form and then the qualified form is created with
5645 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5647 while (declarator && declarator->kind != cdk_id)
5649 if (type == error_mark_node)
5651 declarator = declarator->declarator;
5652 continue;
5655 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5656 a cdk_pointer (for *...),
5657 a cdk_function (for ...(...)),
5658 a cdk_attrs (for nested attributes),
5659 or a cdk_id (for the name being declared
5660 or the place in an absolute declarator
5661 where the name was omitted).
5662 For the last case, we have just exited the loop.
5664 At this point, TYPE is the type of elements of an array,
5665 or for a function to return, or for a pointer to point to.
5666 After this sequence of ifs, TYPE is the type of the
5667 array or function or pointer, and DECLARATOR has had its
5668 outermost layer removed. */
5670 if (array_ptr_quals != TYPE_UNQUALIFIED
5671 || array_ptr_attrs != NULL_TREE
5672 || array_parm_static)
5674 /* Only the innermost declarator (making a parameter be of
5675 array type which is converted to pointer type)
5676 may have static or type qualifiers. */
5677 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5678 array_ptr_quals = TYPE_UNQUALIFIED;
5679 array_ptr_attrs = NULL_TREE;
5680 array_parm_static = 0;
5683 switch (declarator->kind)
5685 case cdk_attrs:
5687 /* A declarator with embedded attributes. */
5688 tree attrs = declarator->u.attrs;
5689 const struct c_declarator *inner_decl;
5690 int attr_flags = 0;
5691 declarator = declarator->declarator;
5692 inner_decl = declarator;
5693 while (inner_decl->kind == cdk_attrs)
5694 inner_decl = inner_decl->declarator;
5695 if (inner_decl->kind == cdk_id)
5696 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5697 else if (inner_decl->kind == cdk_function)
5698 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5699 else if (inner_decl->kind == cdk_array)
5700 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5701 returned_attrs = decl_attributes (&type,
5702 chainon (returned_attrs, attrs),
5703 attr_flags);
5704 break;
5706 case cdk_array:
5708 tree itype = NULL_TREE;
5709 tree size = declarator->u.array.dimen;
5710 /* The index is a signed object `sizetype' bits wide. */
5711 tree index_type = c_common_signed_type (sizetype);
5713 array_ptr_quals = declarator->u.array.quals;
5714 array_ptr_attrs = declarator->u.array.attrs;
5715 array_parm_static = declarator->u.array.static_p;
5716 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5718 declarator = declarator->declarator;
5720 /* Check for some types that there cannot be arrays of. */
5722 if (VOID_TYPE_P (type))
5724 if (name)
5725 error_at (loc, "declaration of %qE as array of voids", name);
5726 else
5727 error_at (loc, "declaration of type name as array of voids");
5728 type = error_mark_node;
5731 if (TREE_CODE (type) == FUNCTION_TYPE)
5733 if (name)
5734 error_at (loc, "declaration of %qE as array of functions",
5735 name);
5736 else
5737 error_at (loc, "declaration of type name as array of "
5738 "functions");
5739 type = error_mark_node;
5742 if (pedantic && !in_system_header_at (input_location)
5743 && flexible_array_type_p (type))
5744 pedwarn (loc, OPT_Wpedantic,
5745 "invalid use of structure with flexible array member");
5747 if (size == error_mark_node)
5748 type = error_mark_node;
5750 if (type == error_mark_node)
5751 continue;
5753 /* If size was specified, set ITYPE to a range-type for
5754 that size. Otherwise, ITYPE remains null. finish_decl
5755 may figure it out from an initial value. */
5757 if (size)
5759 bool size_maybe_const = true;
5760 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5761 && !TREE_OVERFLOW (size));
5762 bool this_size_varies = false;
5764 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5765 lvalue. */
5766 STRIP_TYPE_NOPS (size);
5768 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5770 if (name)
5771 error_at (loc, "size of array %qE has non-integer type",
5772 name);
5773 else
5774 error_at (loc,
5775 "size of unnamed array has non-integer type");
5776 size = integer_one_node;
5779 size = c_fully_fold (size, false, &size_maybe_const);
5781 if (pedantic && size_maybe_const && integer_zerop (size))
5783 if (name)
5784 pedwarn (loc, OPT_Wpedantic,
5785 "ISO C forbids zero-size array %qE", name);
5786 else
5787 pedwarn (loc, OPT_Wpedantic,
5788 "ISO C forbids zero-size array");
5791 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5793 constant_expression_warning (size);
5794 if (tree_int_cst_sgn (size) < 0)
5796 if (name)
5797 error_at (loc, "size of array %qE is negative", name);
5798 else
5799 error_at (loc, "size of unnamed array is negative");
5800 size = integer_one_node;
5802 /* Handle a size folded to an integer constant but
5803 not an integer constant expression. */
5804 if (!size_int_const)
5806 /* If this is a file scope declaration of an
5807 ordinary identifier, this is invalid code;
5808 diagnosing it here and not subsequently
5809 treating the type as variable-length avoids
5810 more confusing diagnostics later. */
5811 if ((decl_context == NORMAL || decl_context == FIELD)
5812 && current_scope == file_scope)
5813 pedwarn (input_location, 0,
5814 "variably modified %qE at file scope",
5815 name);
5816 else
5817 this_size_varies = size_varies = true;
5818 warn_variable_length_array (name, size);
5821 else if ((decl_context == NORMAL || decl_context == FIELD)
5822 && current_scope == file_scope)
5824 error_at (loc, "variably modified %qE at file scope", name);
5825 size = integer_one_node;
5827 else
5829 /* Make sure the array size remains visibly
5830 nonconstant even if it is (eg) a const variable
5831 with known value. */
5832 this_size_varies = size_varies = true;
5833 warn_variable_length_array (name, size);
5834 if (flag_sanitize & SANITIZE_VLA
5835 && decl_context == NORMAL
5836 && do_ubsan_in_current_function ())
5838 /* Evaluate the array size only once. */
5839 size = c_save_expr (size);
5840 size = c_fully_fold (size, false, NULL);
5841 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5842 ubsan_instrument_vla (loc, size),
5843 size);
5847 if (integer_zerop (size) && !this_size_varies)
5849 /* A zero-length array cannot be represented with
5850 an unsigned index type, which is what we'll
5851 get with build_index_type. Create an
5852 open-ended range instead. */
5853 itype = build_range_type (sizetype, size, NULL_TREE);
5855 else
5857 /* Arrange for the SAVE_EXPR on the inside of the
5858 MINUS_EXPR, which allows the -1 to get folded
5859 with the +1 that happens when building TYPE_SIZE. */
5860 if (size_varies)
5861 size = save_expr (size);
5862 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5863 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5864 integer_zero_node, size);
5866 /* Compute the maximum valid index, that is, size
5867 - 1. Do the calculation in index_type, so that
5868 if it is a variable the computations will be
5869 done in the proper mode. */
5870 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5871 convert (index_type, size),
5872 convert (index_type,
5873 size_one_node));
5875 /* The above overflows when size does not fit
5876 in index_type.
5877 ??? While a size of INT_MAX+1 technically shouldn't
5878 cause an overflow (because we subtract 1), handling
5879 this case seems like an unnecessary complication. */
5880 if (TREE_CODE (size) == INTEGER_CST
5881 && !int_fits_type_p (size, index_type))
5883 if (name)
5884 error_at (loc, "size of array %qE is too large",
5885 name);
5886 else
5887 error_at (loc, "size of unnamed array is too large");
5888 type = error_mark_node;
5889 continue;
5892 itype = build_index_type (itype);
5894 if (this_size_varies)
5896 if (*expr)
5897 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5898 *expr, size);
5899 else
5900 *expr = size;
5901 *expr_const_operands &= size_maybe_const;
5904 else if (decl_context == FIELD)
5906 bool flexible_array_member = false;
5907 if (array_parm_vla_unspec_p)
5908 /* Field names can in fact have function prototype
5909 scope so [*] is disallowed here through making
5910 the field variably modified, not through being
5911 something other than a declaration with function
5912 prototype scope. */
5913 size_varies = true;
5914 else
5916 const struct c_declarator *t = declarator;
5917 while (t->kind == cdk_attrs)
5918 t = t->declarator;
5919 flexible_array_member = (t->kind == cdk_id);
5921 if (flexible_array_member
5922 && !in_system_header_at (input_location))
5923 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
5924 "support flexible array members");
5926 /* ISO C99 Flexible array members are effectively
5927 identical to GCC's zero-length array extension. */
5928 if (flexible_array_member || array_parm_vla_unspec_p)
5929 itype = build_range_type (sizetype, size_zero_node,
5930 NULL_TREE);
5932 else if (decl_context == PARM)
5934 if (array_parm_vla_unspec_p)
5936 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5937 size_varies = true;
5940 else if (decl_context == TYPENAME)
5942 if (array_parm_vla_unspec_p)
5944 /* C99 6.7.5.2p4 */
5945 warning (0, "%<[*]%> not in a declaration");
5946 /* We use this to avoid messing up with incomplete
5947 array types of the same type, that would
5948 otherwise be modified below. */
5949 itype = build_range_type (sizetype, size_zero_node,
5950 NULL_TREE);
5951 size_varies = true;
5955 /* Complain about arrays of incomplete types. */
5956 if (!COMPLETE_TYPE_P (type))
5958 error_at (loc, "array type has incomplete element type %qT",
5959 type);
5960 /* See if we can be more helpful. */
5961 if (TREE_CODE (type) == ARRAY_TYPE)
5963 if (name)
5964 inform (loc, "declaration of %qE as multidimensional "
5965 "array must have bounds for all dimensions "
5966 "except the first", name);
5967 else
5968 inform (loc, "declaration of multidimensional array "
5969 "must have bounds for all dimensions except "
5970 "the first");
5972 type = error_mark_node;
5974 else
5975 /* When itype is NULL, a shared incomplete array type is
5976 returned for all array of a given type. Elsewhere we
5977 make sure we don't complete that type before copying
5978 it, but here we want to make sure we don't ever
5979 modify the shared type, so we gcc_assert (itype)
5980 below. */
5982 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5983 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5984 type = build_qualified_type (type,
5985 ENCODE_QUAL_ADDR_SPACE (as));
5987 type = build_array_type (type, itype);
5990 if (type != error_mark_node)
5992 if (size_varies)
5994 /* It is ok to modify type here even if itype is
5995 NULL: if size_varies, we're in a
5996 multi-dimensional array and the inner type has
5997 variable size, so the enclosing shared array type
5998 must too. */
5999 if (size && TREE_CODE (size) == INTEGER_CST)
6000 type
6001 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6002 C_TYPE_VARIABLE_SIZE (type) = 1;
6005 /* The GCC extension for zero-length arrays differs from
6006 ISO flexible array members in that sizeof yields
6007 zero. */
6008 if (size && integer_zerop (size))
6010 gcc_assert (itype);
6011 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6012 TYPE_SIZE (type) = bitsize_zero_node;
6013 TYPE_SIZE_UNIT (type) = size_zero_node;
6014 SET_TYPE_STRUCTURAL_EQUALITY (type);
6016 if (array_parm_vla_unspec_p)
6018 gcc_assert (itype);
6019 /* The type is complete. C99 6.7.5.2p4 */
6020 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6021 TYPE_SIZE (type) = bitsize_zero_node;
6022 TYPE_SIZE_UNIT (type) = size_zero_node;
6023 SET_TYPE_STRUCTURAL_EQUALITY (type);
6026 if (!valid_array_size_p (loc, type, name))
6027 type = error_mark_node;
6030 if (decl_context != PARM
6031 && (array_ptr_quals != TYPE_UNQUALIFIED
6032 || array_ptr_attrs != NULL_TREE
6033 || array_parm_static))
6035 error_at (loc, "static or type qualifiers in non-parameter "
6036 "array declarator");
6037 array_ptr_quals = TYPE_UNQUALIFIED;
6038 array_ptr_attrs = NULL_TREE;
6039 array_parm_static = 0;
6041 orig_qual_indirect++;
6042 break;
6044 case cdk_function:
6046 /* Say it's a definition only for the declarator closest
6047 to the identifier, apart possibly from some
6048 attributes. */
6049 bool really_funcdef = false;
6050 tree arg_types;
6051 orig_qual_type = NULL_TREE;
6052 if (funcdef_flag)
6054 const struct c_declarator *t = declarator->declarator;
6055 while (t->kind == cdk_attrs)
6056 t = t->declarator;
6057 really_funcdef = (t->kind == cdk_id);
6060 /* Declaring a function type. Make sure we have a valid
6061 type for the function to return. */
6062 if (type == error_mark_node)
6063 continue;
6065 size_varies = false;
6067 /* Warn about some types functions can't return. */
6068 if (TREE_CODE (type) == FUNCTION_TYPE)
6070 if (name)
6071 error_at (loc, "%qE declared as function returning a "
6072 "function", name);
6073 else
6074 error_at (loc, "type name declared as function "
6075 "returning a function");
6076 type = integer_type_node;
6078 if (TREE_CODE (type) == ARRAY_TYPE)
6080 if (name)
6081 error_at (loc, "%qE declared as function returning an array",
6082 name);
6083 else
6084 error_at (loc, "type name declared as function returning "
6085 "an array");
6086 type = integer_type_node;
6088 errmsg = targetm.invalid_return_type (type);
6089 if (errmsg)
6091 error (errmsg);
6092 type = integer_type_node;
6095 /* Construct the function type and go to the next
6096 inner layer of declarator. */
6097 arg_info = declarator->u.arg_info;
6098 arg_types = grokparms (arg_info, really_funcdef);
6100 /* Type qualifiers before the return type of the function
6101 qualify the return type, not the function type. */
6102 if (type_quals)
6104 /* Type qualifiers on a function return type are
6105 normally permitted by the standard but have no
6106 effect, so give a warning at -Wreturn-type.
6107 Qualifiers on a void return type are banned on
6108 function definitions in ISO C; GCC used to used
6109 them for noreturn functions. */
6110 if (VOID_TYPE_P (type) && really_funcdef)
6111 pedwarn (loc, 0,
6112 "function definition has qualified void return type");
6113 else
6114 warning_at (loc, OPT_Wignored_qualifiers,
6115 "type qualifiers ignored on function return type");
6117 type = c_build_qualified_type (type, type_quals);
6119 type_quals = TYPE_UNQUALIFIED;
6121 type = build_function_type (type, arg_types);
6122 declarator = declarator->declarator;
6124 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6125 the formal parameter list of this FUNCTION_TYPE to point to
6126 the FUNCTION_TYPE node itself. */
6128 c_arg_tag *tag;
6129 unsigned ix;
6131 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6132 TYPE_CONTEXT (tag->type) = type;
6134 break;
6136 case cdk_pointer:
6138 /* Merge any constancy or volatility into the target type
6139 for the pointer. */
6140 if ((type_quals & TYPE_QUAL_ATOMIC)
6141 && TREE_CODE (type) == FUNCTION_TYPE)
6143 error_at (loc,
6144 "%<_Atomic%>-qualified function type");
6145 type_quals &= ~TYPE_QUAL_ATOMIC;
6147 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6148 && type_quals)
6149 pedwarn (loc, OPT_Wpedantic,
6150 "ISO C forbids qualified function types");
6151 if (type_quals)
6152 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6153 orig_qual_indirect);
6154 orig_qual_type = NULL_TREE;
6155 size_varies = false;
6157 /* When the pointed-to type involves components of variable size,
6158 care must be taken to ensure that the size evaluation code is
6159 emitted early enough to dominate all the possible later uses
6160 and late enough for the variables on which it depends to have
6161 been assigned.
6163 This is expected to happen automatically when the pointed-to
6164 type has a name/declaration of it's own, but special attention
6165 is required if the type is anonymous.
6167 We handle the NORMAL and FIELD contexts here by attaching an
6168 artificial TYPE_DECL to such pointed-to type. This forces the
6169 sizes evaluation at a safe point and ensures it is not deferred
6170 until e.g. within a deeper conditional context.
6172 We expect nothing to be needed here for PARM or TYPENAME.
6173 Pushing a TYPE_DECL at this point for TYPENAME would actually
6174 be incorrect, as we might be in the middle of an expression
6175 with side effects on the pointed-to type size "arguments" prior
6176 to the pointer declaration point and the fake TYPE_DECL in the
6177 enclosing context would force the size evaluation prior to the
6178 side effects. */
6180 if (!TYPE_NAME (type)
6181 && (decl_context == NORMAL || decl_context == FIELD)
6182 && variably_modified_type_p (type, NULL_TREE))
6184 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6185 DECL_ARTIFICIAL (decl) = 1;
6186 pushdecl (decl);
6187 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6188 TYPE_NAME (type) = decl;
6191 type = c_build_pointer_type (type);
6193 /* Process type qualifiers (such as const or volatile)
6194 that were given inside the `*'. */
6195 type_quals = declarator->u.pointer_quals;
6197 declarator = declarator->declarator;
6198 break;
6200 default:
6201 gcc_unreachable ();
6204 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6206 /* Now TYPE has the actual type, apart from any qualifiers in
6207 TYPE_QUALS. */
6209 /* Warn about address space used for things other than static memory or
6210 pointers. */
6211 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6212 if (!ADDR_SPACE_GENERIC_P (address_space))
6214 if (decl_context == NORMAL)
6216 switch (storage_class)
6218 case csc_auto:
6219 error ("%qs combined with %<auto%> qualifier for %qE",
6220 c_addr_space_name (address_space), name);
6221 break;
6222 case csc_register:
6223 error ("%qs combined with %<register%> qualifier for %qE",
6224 c_addr_space_name (address_space), name);
6225 break;
6226 case csc_none:
6227 if (current_function_scope)
6229 error ("%qs specified for auto variable %qE",
6230 c_addr_space_name (address_space), name);
6231 break;
6233 break;
6234 case csc_static:
6235 case csc_extern:
6236 case csc_typedef:
6237 break;
6238 default:
6239 gcc_unreachable ();
6242 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6244 if (name)
6245 error ("%qs specified for parameter %qE",
6246 c_addr_space_name (address_space), name);
6247 else
6248 error ("%qs specified for unnamed parameter",
6249 c_addr_space_name (address_space));
6251 else if (decl_context == FIELD)
6253 if (name)
6254 error ("%qs specified for structure field %qE",
6255 c_addr_space_name (address_space), name);
6256 else
6257 error ("%qs specified for structure field",
6258 c_addr_space_name (address_space));
6262 /* Check the type and width of a bit-field. */
6263 if (bitfield)
6265 check_bitfield_type_and_width (loc, &type, width, name);
6266 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6267 atomic types are permitted for bit-fields; we have no code to
6268 make bit-field accesses atomic, so disallow them. */
6269 if (type_quals & TYPE_QUAL_ATOMIC)
6271 if (name)
6272 error_at (loc, "bit-field %qE has atomic type", name);
6273 else
6274 error_at (loc, "bit-field has atomic type");
6275 type_quals &= ~TYPE_QUAL_ATOMIC;
6279 /* Reject invalid uses of _Alignas. */
6280 if (declspecs->alignas_p)
6282 if (storage_class == csc_typedef)
6283 error_at (loc, "alignment specified for typedef %qE", name);
6284 else if (storage_class == csc_register)
6285 error_at (loc, "alignment specified for %<register%> object %qE",
6286 name);
6287 else if (decl_context == PARM)
6289 if (name)
6290 error_at (loc, "alignment specified for parameter %qE", name);
6291 else
6292 error_at (loc, "alignment specified for unnamed parameter");
6294 else if (bitfield)
6296 if (name)
6297 error_at (loc, "alignment specified for bit-field %qE", name);
6298 else
6299 error_at (loc, "alignment specified for unnamed bit-field");
6301 else if (TREE_CODE (type) == FUNCTION_TYPE)
6302 error_at (loc, "alignment specified for function %qE", name);
6303 else if (declspecs->align_log != -1)
6305 alignas_align = 1U << declspecs->align_log;
6306 if (alignas_align < min_align_of_type (type))
6308 if (name)
6309 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6310 "alignment of %qE", name);
6311 else
6312 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6313 "alignment of unnamed field");
6314 alignas_align = 0;
6319 /* If this is declaring a typedef name, return a TYPE_DECL. */
6321 if (storage_class == csc_typedef)
6323 tree decl;
6324 if ((type_quals & TYPE_QUAL_ATOMIC)
6325 && TREE_CODE (type) == FUNCTION_TYPE)
6327 error_at (loc,
6328 "%<_Atomic%>-qualified function type");
6329 type_quals &= ~TYPE_QUAL_ATOMIC;
6331 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6332 && type_quals)
6333 pedwarn (loc, OPT_Wpedantic,
6334 "ISO C forbids qualified function types");
6335 if (type_quals)
6336 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6337 orig_qual_indirect);
6338 decl = build_decl (declarator->id_loc,
6339 TYPE_DECL, declarator->u.id, type);
6340 if (declspecs->explicit_signed_p)
6341 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6342 if (declspecs->inline_p)
6343 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6344 if (declspecs->noreturn_p)
6345 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6347 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6349 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6351 if (b != NULL
6352 && b->decl != NULL_TREE
6353 && (B_IN_CURRENT_SCOPE (b)
6354 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6355 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6357 warning_at (declarator->id_loc, OPT_Wc___compat,
6358 ("using %qD as both a typedef and a tag is "
6359 "invalid in C++"),
6360 decl);
6361 if (b->locus != UNKNOWN_LOCATION)
6362 inform (b->locus, "originally defined here");
6366 return decl;
6369 /* If this is a type name (such as, in a cast or sizeof),
6370 compute the type and return it now. */
6372 if (decl_context == TYPENAME)
6374 /* Note that the grammar rejects storage classes in typenames
6375 and fields. */
6376 gcc_assert (storage_class == csc_none && !threadp
6377 && !declspecs->inline_p && !declspecs->noreturn_p);
6378 if ((type_quals & TYPE_QUAL_ATOMIC)
6379 && TREE_CODE (type) == FUNCTION_TYPE)
6381 error_at (loc,
6382 "%<_Atomic%>-qualified function type");
6383 type_quals &= ~TYPE_QUAL_ATOMIC;
6385 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6386 && type_quals)
6387 pedwarn (loc, OPT_Wpedantic,
6388 "ISO C forbids const or volatile function types");
6389 if (type_quals)
6390 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6391 orig_qual_indirect);
6392 return type;
6395 if (pedantic && decl_context == FIELD
6396 && variably_modified_type_p (type, NULL_TREE))
6398 /* C99 6.7.2.1p8 */
6399 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6400 "have a variably modified type");
6403 /* Aside from typedefs and type names (handle above),
6404 `void' at top level (not within pointer)
6405 is allowed only in public variables.
6406 We don't complain about parms either, but that is because
6407 a better error message can be made later. */
6409 if (VOID_TYPE_P (type) && decl_context != PARM
6410 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6411 && (storage_class == csc_extern
6412 || (current_scope == file_scope
6413 && !(storage_class == csc_static
6414 || storage_class == csc_register)))))
6416 error_at (loc, "variable or field %qE declared void", name);
6417 type = integer_type_node;
6420 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6421 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6424 tree decl;
6426 if (decl_context == PARM)
6428 tree promoted_type;
6429 bool array_parameter_p = false;
6431 /* A parameter declared as an array of T is really a pointer to T.
6432 One declared as a function is really a pointer to a function. */
6434 if (TREE_CODE (type) == ARRAY_TYPE)
6436 /* Transfer const-ness of array into that of type pointed to. */
6437 type = TREE_TYPE (type);
6438 if (orig_qual_type != NULL_TREE)
6440 if (orig_qual_indirect == 0)
6441 orig_qual_type = TREE_TYPE (orig_qual_type);
6442 else
6443 orig_qual_indirect--;
6445 if (type_quals)
6446 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6447 orig_qual_indirect);
6448 type = c_build_pointer_type (type);
6449 type_quals = array_ptr_quals;
6450 if (type_quals)
6451 type = c_build_qualified_type (type, type_quals);
6453 /* We don't yet implement attributes in this context. */
6454 if (array_ptr_attrs != NULL_TREE)
6455 warning_at (loc, OPT_Wattributes,
6456 "attributes in parameter array declarator ignored");
6458 size_varies = false;
6459 array_parameter_p = true;
6461 else if (TREE_CODE (type) == FUNCTION_TYPE)
6463 if (type_quals & TYPE_QUAL_ATOMIC)
6465 error_at (loc,
6466 "%<_Atomic%>-qualified function type");
6467 type_quals &= ~TYPE_QUAL_ATOMIC;
6469 else if (type_quals)
6470 pedwarn (loc, OPT_Wpedantic,
6471 "ISO C forbids qualified function types");
6472 if (type_quals)
6473 type = c_build_qualified_type (type, type_quals);
6474 type = c_build_pointer_type (type);
6475 type_quals = TYPE_UNQUALIFIED;
6477 else if (type_quals)
6478 type = c_build_qualified_type (type, type_quals);
6480 decl = build_decl (declarator->id_loc,
6481 PARM_DECL, declarator->u.id, type);
6482 if (size_varies)
6483 C_DECL_VARIABLE_SIZE (decl) = 1;
6484 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6486 /* Compute the type actually passed in the parmlist,
6487 for the case where there is no prototype.
6488 (For example, shorts and chars are passed as ints.)
6489 When there is a prototype, this is overridden later. */
6491 if (type == error_mark_node)
6492 promoted_type = type;
6493 else
6494 promoted_type = c_type_promotes_to (type);
6496 DECL_ARG_TYPE (decl) = promoted_type;
6497 if (declspecs->inline_p)
6498 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6499 if (declspecs->noreturn_p)
6500 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6502 else if (decl_context == FIELD)
6504 /* Note that the grammar rejects storage classes in typenames
6505 and fields. */
6506 gcc_assert (storage_class == csc_none && !threadp
6507 && !declspecs->inline_p && !declspecs->noreturn_p);
6509 /* Structure field. It may not be a function. */
6511 if (TREE_CODE (type) == FUNCTION_TYPE)
6513 error_at (loc, "field %qE declared as a function", name);
6514 type = build_pointer_type (type);
6516 else if (TREE_CODE (type) != ERROR_MARK
6517 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6519 if (name)
6520 error_at (loc, "field %qE has incomplete type", name);
6521 else
6522 error_at (loc, "unnamed field has incomplete type");
6523 type = error_mark_node;
6525 else if (TREE_CODE (type) == ARRAY_TYPE
6526 && TYPE_DOMAIN (type) == NULL_TREE)
6528 /* We have a flexible array member through a typedef.
6529 Set suitable range. Whether this is a correct position
6530 for a flexible array member will be determined elsewhere. */
6531 if (!in_system_header_at (input_location))
6532 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6533 "support flexible array members");
6534 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6535 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6536 NULL_TREE);
6538 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6539 orig_qual_indirect);
6540 decl = build_decl (declarator->id_loc,
6541 FIELD_DECL, declarator->u.id, type);
6542 DECL_NONADDRESSABLE_P (decl) = bitfield;
6543 if (bitfield && !declarator->u.id)
6544 TREE_NO_WARNING (decl) = 1;
6546 if (size_varies)
6547 C_DECL_VARIABLE_SIZE (decl) = 1;
6549 else if (TREE_CODE (type) == FUNCTION_TYPE)
6551 if (storage_class == csc_register || threadp)
6553 error_at (loc, "invalid storage class for function %qE", name);
6555 else if (current_scope != file_scope)
6557 /* Function declaration not at file scope. Storage
6558 classes other than `extern' are not allowed, C99
6559 6.7.1p5, and `extern' makes no difference. However,
6560 GCC allows 'auto', perhaps with 'inline', to support
6561 nested functions. */
6562 if (storage_class == csc_auto)
6563 pedwarn (loc, OPT_Wpedantic,
6564 "invalid storage class for function %qE", name);
6565 else if (storage_class == csc_static)
6567 error_at (loc, "invalid storage class for function %qE", name);
6568 if (funcdef_flag)
6569 storage_class = declspecs->storage_class = csc_none;
6570 else
6571 return 0;
6575 decl = build_decl (declarator->id_loc,
6576 FUNCTION_DECL, declarator->u.id, type);
6577 decl = build_decl_attribute_variant (decl, decl_attr);
6579 if (type_quals & TYPE_QUAL_ATOMIC)
6581 error_at (loc,
6582 "%<_Atomic%>-qualified function type");
6583 type_quals &= ~TYPE_QUAL_ATOMIC;
6585 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6586 pedwarn (loc, OPT_Wpedantic,
6587 "ISO C forbids qualified function types");
6589 /* Every function declaration is an external reference
6590 (DECL_EXTERNAL) except for those which are not at file
6591 scope and are explicitly declared "auto". This is
6592 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6593 GCC to signify a forward declaration of a nested function. */
6594 if (storage_class == csc_auto && current_scope != file_scope)
6595 DECL_EXTERNAL (decl) = 0;
6596 /* In C99, a function which is declared 'inline' with 'extern'
6597 is not an external reference (which is confusing). It
6598 means that the later definition of the function must be output
6599 in this file, C99 6.7.4p6. In GNU C89, a function declared
6600 'extern inline' is an external reference. */
6601 else if (declspecs->inline_p && storage_class != csc_static)
6602 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6603 == flag_gnu89_inline);
6604 else
6605 DECL_EXTERNAL (decl) = !initialized;
6607 /* Record absence of global scope for `static' or `auto'. */
6608 TREE_PUBLIC (decl)
6609 = !(storage_class == csc_static || storage_class == csc_auto);
6611 /* For a function definition, record the argument information
6612 block where store_parm_decls will look for it. */
6613 if (funcdef_flag)
6614 current_function_arg_info = arg_info;
6616 if (declspecs->default_int_p)
6617 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6619 /* Record presence of `inline' and `_Noreturn', if it is
6620 reasonable. */
6621 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6623 if (declspecs->inline_p)
6624 pedwarn (loc, 0, "cannot inline function %<main%>");
6625 if (declspecs->noreturn_p)
6626 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6628 else
6630 if (declspecs->inline_p)
6631 /* Record that the function is declared `inline'. */
6632 DECL_DECLARED_INLINE_P (decl) = 1;
6633 if (declspecs->noreturn_p)
6635 if (flag_isoc99)
6636 pedwarn_c99 (loc, OPT_Wpedantic,
6637 "ISO C99 does not support %<_Noreturn%>");
6638 else
6639 pedwarn_c99 (loc, OPT_Wpedantic,
6640 "ISO C90 does not support %<_Noreturn%>");
6641 TREE_THIS_VOLATILE (decl) = 1;
6645 else
6647 /* It's a variable. */
6648 /* An uninitialized decl with `extern' is a reference. */
6649 int extern_ref = !initialized && storage_class == csc_extern;
6651 type = c_build_qualified_type (type, type_quals, orig_qual_type,
6652 orig_qual_indirect);
6654 /* C99 6.2.2p7: It is invalid (compile-time undefined
6655 behavior) to create an 'extern' declaration for a
6656 variable if there is a global declaration that is
6657 'static' and the global declaration is not visible.
6658 (If the static declaration _is_ currently visible,
6659 the 'extern' declaration is taken to refer to that decl.) */
6660 if (extern_ref && current_scope != file_scope)
6662 tree global_decl = identifier_global_value (declarator->u.id);
6663 tree visible_decl = lookup_name (declarator->u.id);
6665 if (global_decl
6666 && global_decl != visible_decl
6667 && VAR_P (global_decl)
6668 && !TREE_PUBLIC (global_decl))
6669 error_at (loc, "variable previously declared %<static%> "
6670 "redeclared %<extern%>");
6673 decl = build_decl (declarator->id_loc,
6674 VAR_DECL, declarator->u.id, type);
6675 if (size_varies)
6676 C_DECL_VARIABLE_SIZE (decl) = 1;
6678 if (declspecs->inline_p)
6679 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6680 if (declspecs->noreturn_p)
6681 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6683 /* At file scope, an initialized extern declaration may follow
6684 a static declaration. In that case, DECL_EXTERNAL will be
6685 reset later in start_decl. */
6686 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6688 /* At file scope, the presence of a `static' or `register' storage
6689 class specifier, or the absence of all storage class specifiers
6690 makes this declaration a definition (perhaps tentative). Also,
6691 the absence of `static' makes it public. */
6692 if (current_scope == file_scope)
6694 TREE_PUBLIC (decl) = storage_class != csc_static;
6695 TREE_STATIC (decl) = !extern_ref;
6697 /* Not at file scope, only `static' makes a static definition. */
6698 else
6700 TREE_STATIC (decl) = (storage_class == csc_static);
6701 TREE_PUBLIC (decl) = extern_ref;
6704 if (threadp)
6705 set_decl_tls_model (decl, decl_default_tls_model (decl));
6708 if ((storage_class == csc_extern
6709 || (storage_class == csc_none
6710 && TREE_CODE (type) == FUNCTION_TYPE
6711 && !funcdef_flag))
6712 && variably_modified_type_p (type, NULL_TREE))
6714 /* C99 6.7.5.2p2 */
6715 if (TREE_CODE (type) == FUNCTION_TYPE)
6716 error_at (loc, "non-nested function with variably modified type");
6717 else
6718 error_at (loc, "object with variably modified type must have "
6719 "no linkage");
6722 /* Record `register' declaration for warnings on &
6723 and in case doing stupid register allocation. */
6725 if (storage_class == csc_register)
6727 C_DECL_REGISTER (decl) = 1;
6728 DECL_REGISTER (decl) = 1;
6731 /* Record constancy and volatility. */
6732 c_apply_type_quals_to_decl (type_quals, decl);
6734 /* Apply _Alignas specifiers. */
6735 if (alignas_align)
6737 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6738 DECL_USER_ALIGN (decl) = 1;
6741 /* If a type has volatile components, it should be stored in memory.
6742 Otherwise, the fact that those components are volatile
6743 will be ignored, and would even crash the compiler.
6744 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6745 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6746 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
6747 || TREE_CODE (decl) == RESULT_DECL))
6749 /* It is not an error for a structure with volatile fields to
6750 be declared register, but reset DECL_REGISTER since it
6751 cannot actually go in a register. */
6752 int was_reg = C_DECL_REGISTER (decl);
6753 C_DECL_REGISTER (decl) = 0;
6754 DECL_REGISTER (decl) = 0;
6755 c_mark_addressable (decl);
6756 C_DECL_REGISTER (decl) = was_reg;
6759 /* This is the earliest point at which we might know the assembler
6760 name of a variable. Thus, if it's known before this, die horribly. */
6761 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6763 if (warn_cxx_compat
6764 && VAR_P (decl)
6765 && TREE_PUBLIC (decl)
6766 && TREE_STATIC (decl)
6767 && (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
6768 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6769 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6770 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6771 ("non-local variable %qD with anonymous type is "
6772 "questionable in C++"),
6773 decl);
6775 return decl;
6779 /* Decode the parameter-list info for a function type or function definition.
6780 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6781 if there is an identifier list instead of a parameter decl list).
6782 These two functions are separate because when a function returns
6783 or receives functions then each is called multiple times but the order
6784 of calls is different. The last call to `grokparms' is always the one
6785 that contains the formal parameter names of a function definition.
6787 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6789 FUNCDEF_FLAG is true for a function definition, false for
6790 a mere declaration. A nonempty identifier-list gets an error message
6791 when FUNCDEF_FLAG is false. */
6793 static tree
6794 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6796 tree arg_types = arg_info->types;
6798 if (funcdef_flag && arg_info->had_vla_unspec)
6800 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6801 /* C99 6.7.5.2p4 */
6802 error ("%<[*]%> not allowed in other than function prototype scope");
6805 if (arg_types == 0 && !funcdef_flag
6806 && !in_system_header_at (input_location))
6807 warning (OPT_Wstrict_prototypes,
6808 "function declaration isn%'t a prototype");
6810 if (arg_types == error_mark_node)
6811 return 0; /* don't set TYPE_ARG_TYPES in this case */
6813 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6815 if (!funcdef_flag)
6817 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6818 arg_info->parms = NULL_TREE;
6820 else
6821 arg_info->parms = arg_info->types;
6823 arg_info->types = 0;
6824 return 0;
6826 else
6828 tree parm, type, typelt;
6829 unsigned int parmno;
6830 const char *errmsg;
6832 /* If there is a parameter of incomplete type in a definition,
6833 this is an error. In a declaration this is valid, and a
6834 struct or union type may be completed later, before any calls
6835 or definition of the function. In the case where the tag was
6836 first declared within the parameter list, a warning has
6837 already been given. If a parameter has void type, then
6838 however the function cannot be defined or called, so
6839 warn. */
6841 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6842 parm;
6843 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6845 type = TREE_VALUE (typelt);
6846 if (type == error_mark_node)
6847 continue;
6849 if (!COMPLETE_TYPE_P (type))
6851 if (funcdef_flag)
6853 if (DECL_NAME (parm))
6854 error_at (input_location,
6855 "parameter %u (%q+D) has incomplete type",
6856 parmno, parm);
6857 else
6858 error_at (DECL_SOURCE_LOCATION (parm),
6859 "parameter %u has incomplete type",
6860 parmno);
6862 TREE_VALUE (typelt) = error_mark_node;
6863 TREE_TYPE (parm) = error_mark_node;
6864 arg_types = NULL_TREE;
6866 else if (VOID_TYPE_P (type))
6868 if (DECL_NAME (parm))
6869 warning_at (input_location, 0,
6870 "parameter %u (%q+D) has void type",
6871 parmno, parm);
6872 else
6873 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6874 "parameter %u has void type",
6875 parmno);
6879 errmsg = targetm.invalid_parameter_type (type);
6880 if (errmsg)
6882 error (errmsg);
6883 TREE_VALUE (typelt) = error_mark_node;
6884 TREE_TYPE (parm) = error_mark_node;
6885 arg_types = NULL_TREE;
6888 if (DECL_NAME (parm) && TREE_USED (parm))
6889 warn_if_shadowing (parm);
6891 return arg_types;
6895 /* Allocate and initialize a c_arg_info structure from the parser's
6896 obstack. */
6898 struct c_arg_info *
6899 build_arg_info (void)
6901 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6902 ret->parms = NULL_TREE;
6903 ret->tags = NULL;
6904 ret->types = NULL_TREE;
6905 ret->others = NULL_TREE;
6906 ret->pending_sizes = NULL;
6907 ret->had_vla_unspec = 0;
6908 return ret;
6911 /* Take apart the current scope and return a c_arg_info structure with
6912 info on a parameter list just parsed.
6914 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6916 ELLIPSIS being true means the argument list ended in '...' so don't
6917 append a sentinel (void_list_node) to the end of the type-list.
6919 EXPR is NULL or an expression that needs to be evaluated for the
6920 side effects of array size expressions in the parameters. */
6922 struct c_arg_info *
6923 get_parm_info (bool ellipsis, tree expr)
6925 struct c_binding *b = current_scope->bindings;
6926 struct c_arg_info *arg_info = build_arg_info ();
6928 tree parms = 0;
6929 vec<c_arg_tag, va_gc> *tags = NULL;
6930 tree types = 0;
6931 tree others = 0;
6933 bool gave_void_only_once_err = false;
6935 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6937 /* The bindings in this scope must not get put into a block.
6938 We will take care of deleting the binding nodes. */
6939 current_scope->bindings = 0;
6941 /* This function is only called if there was *something* on the
6942 parameter list. */
6943 gcc_assert (b);
6945 /* A parameter list consisting solely of 'void' indicates that the
6946 function takes no arguments. But if the 'void' is qualified
6947 (by 'const' or 'volatile'), or has a storage class specifier
6948 ('register'), then the behavior is undefined; issue an error.
6949 Typedefs for 'void' are OK (see DR#157). */
6950 if (b->prev == 0 /* one binding */
6951 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6952 && !DECL_NAME (b->decl) /* anonymous */
6953 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6955 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
6956 || C_DECL_REGISTER (b->decl))
6957 error_at (b->locus, "%<void%> as only parameter may not be qualified");
6959 /* There cannot be an ellipsis. */
6960 if (ellipsis)
6961 error_at (b->locus, "%<void%> must be the only parameter");
6963 arg_info->types = void_list_node;
6964 return arg_info;
6967 if (!ellipsis)
6968 types = void_list_node;
6970 /* Break up the bindings list into parms, tags, types, and others;
6971 apply sanity checks; purge the name-to-decl bindings. */
6972 while (b)
6974 tree decl = b->decl;
6975 tree type = TREE_TYPE (decl);
6976 c_arg_tag tag;
6977 const char *keyword;
6979 switch (TREE_CODE (decl))
6981 case PARM_DECL:
6982 if (b->id)
6984 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6985 I_SYMBOL_BINDING (b->id) = b->shadowed;
6988 /* Check for forward decls that never got their actual decl. */
6989 if (TREE_ASM_WRITTEN (decl))
6990 error_at (b->locus,
6991 "parameter %q+D has just a forward declaration", decl);
6992 /* Check for (..., void, ...) and issue an error. */
6993 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6995 if (!gave_void_only_once_err)
6997 error_at (b->locus, "%<void%> must be the only parameter");
6998 gave_void_only_once_err = true;
7001 else
7003 /* Valid parameter, add it to the list. */
7004 DECL_CHAIN (decl) = parms;
7005 parms = decl;
7007 /* Since there is a prototype, args are passed in their
7008 declared types. The back end may override this later. */
7009 DECL_ARG_TYPE (decl) = type;
7010 types = tree_cons (0, type, types);
7012 break;
7014 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
7015 case UNION_TYPE: keyword = "union"; goto tag;
7016 case RECORD_TYPE: keyword = "struct"; goto tag;
7017 tag:
7018 /* Types may not have tag-names, in which case the type
7019 appears in the bindings list with b->id NULL. */
7020 if (b->id)
7022 gcc_assert (I_TAG_BINDING (b->id) == b);
7023 I_TAG_BINDING (b->id) = b->shadowed;
7026 /* Warn about any struct, union or enum tags defined in a
7027 parameter list. The scope of such types is limited to
7028 the parameter list, which is rarely if ever desirable
7029 (it's impossible to call such a function with type-
7030 correct arguments). An anonymous union parm type is
7031 meaningful as a GNU extension, so don't warn for that. */
7032 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7034 if (b->id)
7035 /* The %s will be one of 'struct', 'union', or 'enum'. */
7036 warning_at (b->locus, 0,
7037 "%<%s %E%> declared inside parameter list"
7038 " will not be visible outside of this definition or"
7039 " declaration", keyword, b->id);
7040 else
7041 /* The %s will be one of 'struct', 'union', or 'enum'. */
7042 warning_at (b->locus, 0,
7043 "anonymous %s declared inside parameter list"
7044 " will not be visible outside of this definition or"
7045 " declaration", keyword);
7048 tag.id = b->id;
7049 tag.type = decl;
7050 vec_safe_push (tags, tag);
7051 break;
7053 case CONST_DECL:
7054 case TYPE_DECL:
7055 case FUNCTION_DECL:
7056 /* CONST_DECLs appear here when we have an embedded enum,
7057 and TYPE_DECLs appear here when we have an embedded struct
7058 or union. No warnings for this - we already warned about the
7059 type itself. FUNCTION_DECLs appear when there is an implicit
7060 function declaration in the parameter list. */
7062 /* When we reinsert this decl in the function body, we need
7063 to reconstruct whether it was marked as nested. */
7064 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7065 ? b->nested
7066 : !b->nested);
7067 DECL_CHAIN (decl) = others;
7068 others = decl;
7069 /* fall through */
7071 case ERROR_MARK:
7072 /* error_mark_node appears here when we have an undeclared
7073 variable. Just throw it away. */
7074 if (b->id)
7076 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7077 I_SYMBOL_BINDING (b->id) = b->shadowed;
7079 break;
7081 /* Other things that might be encountered. */
7082 case LABEL_DECL:
7083 case VAR_DECL:
7084 default:
7085 gcc_unreachable ();
7088 b = free_binding_and_advance (b);
7091 arg_info->parms = parms;
7092 arg_info->tags = tags;
7093 arg_info->types = types;
7094 arg_info->others = others;
7095 arg_info->pending_sizes = expr;
7096 return arg_info;
7099 /* Get the struct, enum or union (CODE says which) with tag NAME.
7100 Define the tag as a forward-reference with location LOC if it is
7101 not defined. Return a c_typespec structure for the type
7102 specifier. */
7104 struct c_typespec
7105 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7107 struct c_typespec ret;
7108 tree ref;
7109 location_t refloc;
7111 ret.expr = NULL_TREE;
7112 ret.expr_const_operands = true;
7114 /* If a cross reference is requested, look up the type
7115 already defined for this tag and return it. */
7117 ref = lookup_tag (code, name, false, &refloc);
7118 /* If this is the right type of tag, return what we found.
7119 (This reference will be shadowed by shadow_tag later if appropriate.)
7120 If this is the wrong type of tag, do not return it. If it was the
7121 wrong type in the same scope, we will have had an error
7122 message already; if in a different scope and declaring
7123 a name, pending_xref_error will give an error message; but if in a
7124 different scope and not declaring a name, this tag should
7125 shadow the previous declaration of a different type of tag, and
7126 this would not work properly if we return the reference found.
7127 (For example, with "struct foo" in an outer scope, "union foo;"
7128 must shadow that tag with a new one of union type.) */
7129 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7130 if (ref && TREE_CODE (ref) == code)
7132 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7133 && loc != UNKNOWN_LOCATION
7134 && warn_cxx_compat)
7136 switch (code)
7138 case ENUMERAL_TYPE:
7139 warning_at (loc, OPT_Wc___compat,
7140 ("enum type defined in struct or union "
7141 "is not visible in C++"));
7142 inform (refloc, "enum type defined here");
7143 break;
7144 case RECORD_TYPE:
7145 warning_at (loc, OPT_Wc___compat,
7146 ("struct defined in struct or union "
7147 "is not visible in C++"));
7148 inform (refloc, "struct defined here");
7149 break;
7150 case UNION_TYPE:
7151 warning_at (loc, OPT_Wc___compat,
7152 ("union defined in struct or union "
7153 "is not visible in C++"));
7154 inform (refloc, "union defined here");
7155 break;
7156 default:
7157 gcc_unreachable();
7161 ret.spec = ref;
7162 return ret;
7165 /* If no such tag is yet defined, create a forward-reference node
7166 and record it as the "definition".
7167 When a real declaration of this type is found,
7168 the forward-reference will be altered into a real type. */
7170 ref = make_node (code);
7171 if (code == ENUMERAL_TYPE)
7173 /* Give the type a default layout like unsigned int
7174 to avoid crashing if it does not get defined. */
7175 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7176 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
7177 TYPE_USER_ALIGN (ref) = 0;
7178 TYPE_UNSIGNED (ref) = 1;
7179 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7180 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7181 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7184 pushtag (loc, name, ref);
7186 ret.spec = ref;
7187 return ret;
7190 /* Get the struct, enum or union (CODE says which) with tag NAME.
7191 Define the tag as a forward-reference if it is not defined.
7192 Return a tree for the type. */
7194 tree
7195 xref_tag (enum tree_code code, tree name)
7197 return parser_xref_tag (input_location, code, name).spec;
7200 /* Make sure that the tag NAME is defined *in the current scope*
7201 at least as a forward reference.
7202 LOC is the location of the struct's definition.
7203 CODE says which kind of tag NAME ought to be.
7205 This stores the current value of the file static STRUCT_PARSE_INFO
7206 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7207 new c_struct_parse_info structure. The old value of
7208 STRUCT_PARSE_INFO is restored in finish_struct. */
7210 tree
7211 start_struct (location_t loc, enum tree_code code, tree name,
7212 struct c_struct_parse_info **enclosing_struct_parse_info)
7214 /* If there is already a tag defined at this scope
7215 (as a forward reference), just return it. */
7217 tree ref = NULL_TREE;
7218 location_t refloc = UNKNOWN_LOCATION;
7220 if (name != NULL_TREE)
7221 ref = lookup_tag (code, name, true, &refloc);
7222 if (ref && TREE_CODE (ref) == code)
7224 if (TYPE_SIZE (ref))
7226 if (code == UNION_TYPE)
7227 error_at (loc, "redefinition of %<union %E%>", name);
7228 else
7229 error_at (loc, "redefinition of %<struct %E%>", name);
7230 if (refloc != UNKNOWN_LOCATION)
7231 inform (refloc, "originally defined here");
7232 /* Don't create structures using a name already in use. */
7233 ref = NULL_TREE;
7235 else if (C_TYPE_BEING_DEFINED (ref))
7237 if (code == UNION_TYPE)
7238 error_at (loc, "nested redefinition of %<union %E%>", name);
7239 else
7240 error_at (loc, "nested redefinition of %<struct %E%>", name);
7241 /* Don't bother to report "originally defined here" for a
7242 nested redefinition; the original definition should be
7243 obvious. */
7244 /* Don't create structures that contain themselves. */
7245 ref = NULL_TREE;
7249 /* Otherwise create a forward-reference just so the tag is in scope. */
7251 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7253 ref = make_node (code);
7254 pushtag (loc, name, ref);
7257 C_TYPE_BEING_DEFINED (ref) = 1;
7258 for (tree v = TYPE_MAIN_VARIANT (ref); v; v = TYPE_NEXT_VARIANT (v))
7259 TYPE_PACKED (v) = flag_pack_struct;
7261 *enclosing_struct_parse_info = struct_parse_info;
7262 struct_parse_info = XNEW (struct c_struct_parse_info);
7263 struct_parse_info->struct_types.create (0);
7264 struct_parse_info->fields.create (0);
7265 struct_parse_info->typedefs_seen.create (0);
7267 /* FIXME: This will issue a warning for a use of a type defined
7268 within a statement expr used within sizeof, et. al. This is not
7269 terribly serious as C++ doesn't permit statement exprs within
7270 sizeof anyhow. */
7271 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7272 warning_at (loc, OPT_Wc___compat,
7273 "defining type in %qs expression is invalid in C++",
7274 (in_sizeof
7275 ? "sizeof"
7276 : (in_typeof ? "typeof" : "alignof")));
7278 return ref;
7281 /* Process the specs, declarator and width (NULL if omitted)
7282 of a structure component, returning a FIELD_DECL node.
7283 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7284 DECL_ATTRS is as for grokdeclarator.
7286 LOC is the location of the structure component.
7288 This is done during the parsing of the struct declaration.
7289 The FIELD_DECL nodes are chained together and the lot of them
7290 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7292 tree
7293 grokfield (location_t loc,
7294 struct c_declarator *declarator, struct c_declspecs *declspecs,
7295 tree width, tree *decl_attrs)
7297 tree value;
7299 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7300 && width == NULL_TREE)
7302 /* This is an unnamed decl.
7304 If we have something of the form "union { list } ;" then this
7305 is the anonymous union extension. Similarly for struct.
7307 If this is something of the form "struct foo;", then
7308 If MS or Plan 9 extensions are enabled, this is handled as
7309 an anonymous struct.
7310 Otherwise this is a forward declaration of a structure tag.
7312 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7313 If foo names a structure or union without a tag, then this
7314 is an anonymous struct (this is permitted by C11).
7315 If MS or Plan 9 extensions are enabled and foo names a
7316 structure, then again this is an anonymous struct.
7317 Otherwise this is an error.
7319 Oh what a horrid tangled web we weave. I wonder if MS consciously
7320 took this from Plan 9 or if it was an accident of implementation
7321 that took root before someone noticed the bug... */
7323 tree type = declspecs->type;
7324 bool type_ok = RECORD_OR_UNION_TYPE_P (type);
7325 bool ok = false;
7327 if (type_ok
7328 && (flag_ms_extensions
7329 || flag_plan9_extensions
7330 || !declspecs->typedef_p))
7332 if (flag_ms_extensions || flag_plan9_extensions)
7333 ok = true;
7334 else if (TYPE_NAME (type) == NULL)
7335 ok = true;
7336 else
7337 ok = false;
7339 if (!ok)
7341 pedwarn (loc, 0, "declaration does not declare anything");
7342 return NULL_TREE;
7344 if (flag_isoc99)
7345 pedwarn_c99 (loc, OPT_Wpedantic,
7346 "ISO C99 doesn%'t support unnamed structs/unions");
7347 else
7348 pedwarn_c99 (loc, OPT_Wpedantic,
7349 "ISO C90 doesn%'t support unnamed structs/unions");
7352 value = grokdeclarator (declarator, declspecs, FIELD, false,
7353 width ? &width : NULL, decl_attrs, NULL, NULL,
7354 DEPRECATED_NORMAL);
7356 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7357 DECL_INITIAL (value) = width;
7359 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7361 /* If we currently have a binding for this field, set the
7362 in_struct field in the binding, so that we warn about lookups
7363 which find it. */
7364 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7365 if (b != NULL)
7367 /* If the in_struct field is not yet set, push it on a list
7368 to be cleared when this struct is finished. */
7369 if (!b->in_struct)
7371 struct_parse_info->fields.safe_push (b);
7372 b->in_struct = 1;
7377 return value;
7380 /* Subroutine of detect_field_duplicates: return whether X and Y,
7381 which are both fields in the same struct, have duplicate field
7382 names. */
7384 static bool
7385 is_duplicate_field (tree x, tree y)
7387 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7388 return true;
7390 /* When using -fplan9-extensions, an anonymous field whose name is a
7391 typedef can duplicate a field name. */
7392 if (flag_plan9_extensions
7393 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7395 tree xt, xn, yt, yn;
7397 xt = TREE_TYPE (x);
7398 if (DECL_NAME (x) != NULL_TREE)
7399 xn = DECL_NAME (x);
7400 else if (RECORD_OR_UNION_TYPE_P (xt)
7401 && TYPE_NAME (xt) != NULL_TREE
7402 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7403 xn = DECL_NAME (TYPE_NAME (xt));
7404 else
7405 xn = NULL_TREE;
7407 yt = TREE_TYPE (y);
7408 if (DECL_NAME (y) != NULL_TREE)
7409 yn = DECL_NAME (y);
7410 else if (RECORD_OR_UNION_TYPE_P (yt)
7411 && TYPE_NAME (yt) != NULL_TREE
7412 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7413 yn = DECL_NAME (TYPE_NAME (yt));
7414 else
7415 yn = NULL_TREE;
7417 if (xn != NULL_TREE && xn == yn)
7418 return true;
7421 return false;
7424 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7425 to HTAB, giving errors for any duplicates. */
7427 static void
7428 detect_field_duplicates_hash (tree fieldlist,
7429 hash_table<nofree_ptr_hash <tree_node> > *htab)
7431 tree x, y;
7432 tree_node **slot;
7434 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7435 if ((y = DECL_NAME (x)) != 0)
7437 slot = htab->find_slot (y, INSERT);
7438 if (*slot)
7440 error ("duplicate member %q+D", x);
7441 DECL_NAME (x) = NULL_TREE;
7443 *slot = y;
7445 else if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7447 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7449 /* When using -fplan9-extensions, an anonymous field whose
7450 name is a typedef can duplicate a field name. */
7451 if (flag_plan9_extensions
7452 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7453 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7455 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7456 slot = htab->find_slot (xn, INSERT);
7457 if (*slot)
7458 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7459 *slot = xn;
7464 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7465 the list such that this does not present a problem later. */
7467 static void
7468 detect_field_duplicates (tree fieldlist)
7470 tree x, y;
7471 int timeout = 10;
7473 /* If the struct is the list of instance variables of an Objective-C
7474 class, then we need to check all the instance variables of
7475 superclasses when checking for duplicates (since you can't have
7476 an instance variable in a subclass with the same name as an
7477 instance variable in a superclass). We pass on this job to the
7478 Objective-C compiler. objc_detect_field_duplicates() will return
7479 false if we are not checking the list of instance variables and
7480 the C frontend should proceed with the standard field duplicate
7481 checks. If we are checking the list of instance variables, the
7482 ObjC frontend will do the check, emit the errors if needed, and
7483 then return true. */
7484 if (c_dialect_objc ())
7485 if (objc_detect_field_duplicates (false))
7486 return;
7488 /* First, see if there are more than "a few" fields.
7489 This is trivially true if there are zero or one fields. */
7490 if (!fieldlist || !DECL_CHAIN (fieldlist))
7491 return;
7492 x = fieldlist;
7493 do {
7494 timeout--;
7495 if (DECL_NAME (x) == NULL_TREE
7496 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7497 timeout = 0;
7498 x = DECL_CHAIN (x);
7499 } while (timeout > 0 && x);
7501 /* If there were "few" fields and no anonymous structures or unions,
7502 avoid the overhead of allocating a hash table. Instead just do
7503 the nested traversal thing. */
7504 if (timeout > 0)
7506 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7507 /* When using -fplan9-extensions, we can have duplicates
7508 between typedef names and fields. */
7509 if (DECL_NAME (x)
7510 || (flag_plan9_extensions
7511 && DECL_NAME (x) == NULL_TREE
7512 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x))
7513 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7514 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7516 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7517 if (is_duplicate_field (y, x))
7519 error ("duplicate member %q+D", x);
7520 DECL_NAME (x) = NULL_TREE;
7524 else
7526 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7527 detect_field_duplicates_hash (fieldlist, &htab);
7531 /* Finish up struct info used by -Wc++-compat. */
7533 static void
7534 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7535 location_t record_loc)
7537 unsigned int ix;
7538 tree x;
7539 struct c_binding *b;
7541 if (fieldlist == NULL_TREE)
7543 if (code == RECORD_TYPE)
7544 warning_at (record_loc, OPT_Wc___compat,
7545 "empty struct has size 0 in C, size 1 in C++");
7546 else
7547 warning_at (record_loc, OPT_Wc___compat,
7548 "empty union has size 0 in C, size 1 in C++");
7551 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7552 the current struct. We do this now at the end of the struct
7553 because the flag is used to issue visibility warnings, and we
7554 only want to issue those warnings if the type is referenced
7555 outside of the struct declaration. */
7556 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7557 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7559 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7560 typedefs used when declaring fields in this struct. If the name
7561 of any of the fields is also a typedef name then the struct would
7562 not parse in C++, because the C++ lookup rules say that the
7563 typedef name would be looked up in the context of the struct, and
7564 would thus be the field rather than the typedef. */
7565 if (!struct_parse_info->typedefs_seen.is_empty ()
7566 && fieldlist != NULL_TREE)
7568 /* Use a hash_set<tree> using the name of the typedef. We can use
7569 a hash_set<tree> because identifiers are interned. */
7570 hash_set<tree> tset;
7572 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7573 tset.add (DECL_NAME (x));
7575 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7577 if (DECL_NAME (x) != NULL_TREE
7578 && tset.contains (DECL_NAME (x)))
7580 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7581 ("using %qD as both field and typedef name is "
7582 "invalid in C++"),
7584 /* FIXME: It would be nice to report the location where
7585 the typedef name is used. */
7590 /* For each field which has a binding and which was not defined in
7591 an enclosing struct, clear the in_struct field. */
7592 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7593 b->in_struct = 0;
7596 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7597 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7598 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7599 ATTRIBUTES are attributes to be applied to the structure.
7601 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7602 the struct was started. */
7604 tree
7605 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7606 struct c_struct_parse_info *enclosing_struct_parse_info)
7608 tree x;
7609 bool toplevel = file_scope == current_scope;
7610 int saw_named_field;
7612 /* If this type was previously laid out as a forward reference,
7613 make sure we lay it out again. */
7615 TYPE_SIZE (t) = 0;
7617 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7619 if (pedantic)
7621 for (x = fieldlist; x; x = DECL_CHAIN (x))
7623 if (DECL_NAME (x) != 0)
7624 break;
7625 if (flag_isoc11 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7626 break;
7629 if (x == 0)
7631 if (TREE_CODE (t) == UNION_TYPE)
7633 if (fieldlist)
7634 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7635 else
7636 pedwarn (loc, OPT_Wpedantic, "union has no members");
7638 else
7640 if (fieldlist)
7641 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7642 else
7643 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7648 /* Install struct as DECL_CONTEXT of each field decl.
7649 Also process specified field sizes, found in the DECL_INITIAL,
7650 storing 0 there after the type has been changed to precision equal
7651 to its width, rather than the precision of the specified standard
7652 type. (Correct layout requires the original type to have been preserved
7653 until now.) */
7655 saw_named_field = 0;
7656 for (x = fieldlist; x; x = DECL_CHAIN (x))
7658 if (TREE_TYPE (x) == error_mark_node)
7659 continue;
7661 DECL_CONTEXT (x) = t;
7663 /* If any field is const, the structure type is pseudo-const. */
7664 if (TREE_READONLY (x))
7665 C_TYPE_FIELDS_READONLY (t) = 1;
7666 else
7668 /* A field that is pseudo-const makes the structure likewise. */
7669 tree t1 = strip_array_types (TREE_TYPE (x));
7670 if (RECORD_OR_UNION_TYPE_P (t1) && C_TYPE_FIELDS_READONLY (t1))
7671 C_TYPE_FIELDS_READONLY (t) = 1;
7674 /* Any field that is volatile means variables of this type must be
7675 treated in some ways as volatile. */
7676 if (TREE_THIS_VOLATILE (x))
7677 C_TYPE_FIELDS_VOLATILE (t) = 1;
7679 /* Any field of nominal variable size implies structure is too. */
7680 if (C_DECL_VARIABLE_SIZE (x))
7681 C_TYPE_VARIABLE_SIZE (t) = 1;
7683 if (DECL_INITIAL (x))
7685 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7686 DECL_SIZE (x) = bitsize_int (width);
7687 DECL_BIT_FIELD (x) = 1;
7688 SET_DECL_C_BIT_FIELD (x);
7691 if (TYPE_PACKED (t)
7692 && (DECL_BIT_FIELD (x)
7693 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7694 DECL_PACKED (x) = 1;
7696 /* Detect flexible array member in an invalid context. */
7697 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7698 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7699 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7700 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7702 if (TREE_CODE (t) == UNION_TYPE)
7704 error_at (DECL_SOURCE_LOCATION (x),
7705 "flexible array member in union");
7706 TREE_TYPE (x) = error_mark_node;
7708 else if (DECL_CHAIN (x) != NULL_TREE)
7710 error_at (DECL_SOURCE_LOCATION (x),
7711 "flexible array member not at end of struct");
7712 TREE_TYPE (x) = error_mark_node;
7714 else if (!saw_named_field)
7716 error_at (DECL_SOURCE_LOCATION (x),
7717 "flexible array member in otherwise empty struct");
7718 TREE_TYPE (x) = error_mark_node;
7722 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7723 && flexible_array_type_p (TREE_TYPE (x)))
7724 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7725 "invalid use of structure with flexible array member");
7727 if (DECL_NAME (x)
7728 || RECORD_OR_UNION_TYPE_P (TREE_TYPE (x)))
7729 saw_named_field = 1;
7732 detect_field_duplicates (fieldlist);
7734 /* Now we have the nearly final fieldlist. Record it,
7735 then lay out the structure or union (including the fields). */
7737 TYPE_FIELDS (t) = fieldlist;
7739 maybe_apply_pragma_scalar_storage_order (t);
7741 layout_type (t);
7743 if (TYPE_SIZE_UNIT (t)
7744 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7745 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7746 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7747 error ("type %qT is too large", t);
7749 /* Give bit-fields their proper types and rewrite the type of array fields
7750 with scalar component if the enclosing type has reverse storage order. */
7751 for (tree field = fieldlist; field; field = DECL_CHAIN (field))
7753 if (TREE_CODE (field) == FIELD_DECL
7754 && DECL_INITIAL (field)
7755 && TREE_TYPE (field) != error_mark_node)
7757 unsigned HOST_WIDE_INT width
7758 = tree_to_uhwi (DECL_INITIAL (field));
7759 tree type = TREE_TYPE (field);
7760 if (width != TYPE_PRECISION (type))
7762 TREE_TYPE (field)
7763 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7764 DECL_MODE (field) = TYPE_MODE (TREE_TYPE (field));
7766 DECL_INITIAL (field) = 0;
7768 else if (TYPE_REVERSE_STORAGE_ORDER (t)
7769 && TREE_CODE (field) == FIELD_DECL
7770 && TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
7772 tree ftype = TREE_TYPE (field);
7773 tree ctype = strip_array_types (ftype);
7774 if (!RECORD_OR_UNION_TYPE_P (ctype) && TYPE_MODE (ctype) != QImode)
7776 tree fmain_type = TYPE_MAIN_VARIANT (ftype);
7777 tree *typep = &fmain_type;
7778 do {
7779 *typep = build_distinct_type_copy (*typep);
7780 TYPE_REVERSE_STORAGE_ORDER (*typep) = 1;
7781 typep = &TREE_TYPE (*typep);
7782 } while (TREE_CODE (*typep) == ARRAY_TYPE);
7783 TREE_TYPE (field)
7784 = c_build_qualified_type (fmain_type, TYPE_QUALS (ftype));
7789 /* Now we have the truly final field list.
7790 Store it in this type and in the variants. */
7792 TYPE_FIELDS (t) = fieldlist;
7794 /* If there are lots of fields, sort so we can look through them fast.
7795 We arbitrarily consider 16 or more elts to be "a lot". */
7798 int len = 0;
7800 for (x = fieldlist; x; x = DECL_CHAIN (x))
7802 if (len > 15 || DECL_NAME (x) == NULL)
7803 break;
7804 len += 1;
7807 if (len > 15)
7809 tree *field_array;
7810 struct lang_type *space;
7811 struct sorted_fields_type *space2;
7813 len += list_length (x);
7815 /* Use the same allocation policy here that make_node uses, to
7816 ensure that this lives as long as the rest of the struct decl.
7817 All decls in an inline function need to be saved. */
7819 space = ggc_cleared_alloc<struct lang_type> ();
7820 space2 = (sorted_fields_type *) ggc_internal_alloc
7821 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7823 len = 0;
7824 space->s = space2;
7825 field_array = &space2->elts[0];
7826 for (x = fieldlist; x; x = DECL_CHAIN (x))
7828 field_array[len++] = x;
7830 /* If there is anonymous struct or union, break out of the loop. */
7831 if (DECL_NAME (x) == NULL)
7832 break;
7834 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7835 if (x == NULL)
7837 TYPE_LANG_SPECIFIC (t) = space;
7838 TYPE_LANG_SPECIFIC (t)->s->len = len;
7839 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7840 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7845 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7847 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7848 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7849 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7850 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7851 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7854 /* If this was supposed to be a transparent union, but we can't
7855 make it one, warn and turn off the flag. */
7856 if (TREE_CODE (t) == UNION_TYPE
7857 && TYPE_TRANSPARENT_AGGR (t)
7858 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7860 TYPE_TRANSPARENT_AGGR (t) = 0;
7861 warning_at (loc, 0, "union cannot be made transparent");
7864 /* If this structure or union completes the type of any previous
7865 variable declaration, lay it out and output its rtl.
7867 Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
7868 in dwarf2out via rest_of_decl_compilation below and means
7869 something totally different. Since we will be clearing
7870 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
7871 clear it ahead of time and avoid problems in dwarf2out. Ideally,
7872 C_TYPE_INCOMPLETE_VARS should use some language specific
7873 node. */
7874 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7875 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7876 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
7878 tree decl = TREE_VALUE (x);
7879 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7880 layout_array_type (TREE_TYPE (decl));
7881 if (TREE_CODE (decl) != TYPE_DECL)
7883 layout_decl (decl, 0);
7884 if (c_dialect_objc ())
7885 objc_check_decl (decl);
7886 rest_of_decl_compilation (decl, toplevel, 0);
7890 /* Update type location to the one of the definition, instead of e.g.
7891 a forward declaration. */
7892 if (TYPE_STUB_DECL (t))
7893 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7895 /* Finish debugging output for this type. */
7896 rest_of_type_compilation (t, toplevel);
7898 /* If we're inside a function proper, i.e. not file-scope and not still
7899 parsing parameters, then arrange for the size of a variable sized type
7900 to be bound now. */
7901 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7902 add_stmt (build_stmt (loc,
7903 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7905 if (warn_cxx_compat)
7906 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
7908 struct_parse_info->struct_types.release ();
7909 struct_parse_info->fields.release ();
7910 struct_parse_info->typedefs_seen.release ();
7911 XDELETE (struct_parse_info);
7913 struct_parse_info = enclosing_struct_parse_info;
7915 /* If this struct is defined inside a struct, add it to
7916 struct_types. */
7917 if (warn_cxx_compat
7918 && struct_parse_info != NULL
7919 && !in_sizeof && !in_typeof && !in_alignof)
7920 struct_parse_info->struct_types.safe_push (t);
7922 return t;
7925 /* Lay out the type T, and its element type, and so on. */
7927 static void
7928 layout_array_type (tree t)
7930 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7931 layout_array_type (TREE_TYPE (t));
7932 layout_type (t);
7935 /* Begin compiling the definition of an enumeration type.
7936 NAME is its name (or null if anonymous).
7937 LOC is the enum's location.
7938 Returns the type object, as yet incomplete.
7939 Also records info about it so that build_enumerator
7940 may be used to declare the individual values as they are read. */
7942 tree
7943 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7945 tree enumtype = NULL_TREE;
7946 location_t enumloc = UNKNOWN_LOCATION;
7948 /* If this is the real definition for a previous forward reference,
7949 fill in the contents in the same object that used to be the
7950 forward reference. */
7952 if (name != NULL_TREE)
7953 enumtype = lookup_tag (ENUMERAL_TYPE, name, true, &enumloc);
7955 if (enumtype == NULL_TREE || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7957 enumtype = make_node (ENUMERAL_TYPE);
7958 pushtag (loc, name, enumtype);
7961 if (C_TYPE_BEING_DEFINED (enumtype))
7962 error_at (loc, "nested redefinition of %<enum %E%>", name);
7964 C_TYPE_BEING_DEFINED (enumtype) = 1;
7966 if (TYPE_VALUES (enumtype) != 0)
7968 /* This enum is a named one that has been declared already. */
7969 error_at (loc, "redeclaration of %<enum %E%>", name);
7970 if (enumloc != UNKNOWN_LOCATION)
7971 inform (enumloc, "originally defined here");
7973 /* Completely replace its old definition.
7974 The old enumerators remain defined, however. */
7975 TYPE_VALUES (enumtype) = 0;
7978 the_enum->enum_next_value = integer_zero_node;
7979 the_enum->enum_overflow = 0;
7981 if (flag_short_enums)
7982 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
7983 TYPE_PACKED (v) = 1;
7985 /* FIXME: This will issue a warning for a use of a type defined
7986 within sizeof in a statement expr. This is not terribly serious
7987 as C++ doesn't permit statement exprs within sizeof anyhow. */
7988 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7989 warning_at (loc, OPT_Wc___compat,
7990 "defining type in %qs expression is invalid in C++",
7991 (in_sizeof
7992 ? "sizeof"
7993 : (in_typeof ? "typeof" : "alignof")));
7995 return enumtype;
7998 /* After processing and defining all the values of an enumeration type,
7999 install their decls in the enumeration type and finish it off.
8000 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
8001 and ATTRIBUTES are the specified attributes.
8002 Returns ENUMTYPE. */
8004 tree
8005 finish_enum (tree enumtype, tree values, tree attributes)
8007 tree pair, tem;
8008 tree minnode = 0, maxnode = 0;
8009 int precision;
8010 signop sign;
8011 bool toplevel = (file_scope == current_scope);
8012 struct lang_type *lt;
8014 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
8016 /* Calculate the maximum value of any enumerator in this type. */
8018 if (values == error_mark_node)
8019 minnode = maxnode = integer_zero_node;
8020 else
8022 minnode = maxnode = TREE_VALUE (values);
8023 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
8025 tree value = TREE_VALUE (pair);
8026 if (tree_int_cst_lt (maxnode, value))
8027 maxnode = value;
8028 if (tree_int_cst_lt (value, minnode))
8029 minnode = value;
8033 /* Construct the final type of this enumeration. It is the same
8034 as one of the integral types - the narrowest one that fits, except
8035 that normally we only go as narrow as int - and signed iff any of
8036 the values are negative. */
8037 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
8038 precision = MAX (tree_int_cst_min_precision (minnode, sign),
8039 tree_int_cst_min_precision (maxnode, sign));
8041 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
8043 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8044 if (tem == NULL)
8046 warning (0, "enumeration values exceed range of largest integer");
8047 tem = long_long_integer_type_node;
8050 else
8051 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8053 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8054 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8055 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8056 TYPE_ALIGN (enumtype) = TYPE_ALIGN (tem);
8057 TYPE_SIZE (enumtype) = 0;
8059 /* If the precision of the type was specified with an attribute and it
8060 was too small, give an error. Otherwise, use it. */
8061 if (TYPE_PRECISION (enumtype)
8062 && lookup_attribute ("mode", attributes))
8064 if (precision > TYPE_PRECISION (enumtype))
8065 error ("specified mode too small for enumeral values");
8067 else
8068 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8070 layout_type (enumtype);
8072 if (values != error_mark_node)
8074 /* Change the type of the enumerators to be the enum type. We
8075 need to do this irrespective of the size of the enum, for
8076 proper type checking. Replace the DECL_INITIALs of the
8077 enumerators, and the value slots of the list, with copies
8078 that have the enum type; they cannot be modified in place
8079 because they may be shared (e.g. integer_zero_node) Finally,
8080 change the purpose slots to point to the names of the decls. */
8081 for (pair = values; pair; pair = TREE_CHAIN (pair))
8083 tree enu = TREE_PURPOSE (pair);
8084 tree ini = DECL_INITIAL (enu);
8086 TREE_TYPE (enu) = enumtype;
8088 /* The ISO C Standard mandates enumerators to have type int,
8089 even though the underlying type of an enum type is
8090 unspecified. However, GCC allows enumerators of any
8091 integer type as an extensions. build_enumerator()
8092 converts any enumerators that fit in an int to type int,
8093 to avoid promotions to unsigned types when comparing
8094 integers with enumerators that fit in the int range.
8095 When -pedantic is given, build_enumerator() would have
8096 already warned about those that don't fit. Here we
8097 convert the rest to the enumerator type. */
8098 if (TREE_TYPE (ini) != integer_type_node)
8099 ini = convert (enumtype, ini);
8101 DECL_INITIAL (enu) = ini;
8102 TREE_PURPOSE (pair) = DECL_NAME (enu);
8103 TREE_VALUE (pair) = ini;
8106 TYPE_VALUES (enumtype) = values;
8109 /* Record the min/max values so that we can warn about bit-field
8110 enumerations that are too small for the values. */
8111 lt = ggc_cleared_alloc<struct lang_type> ();
8112 lt->enum_min = minnode;
8113 lt->enum_max = maxnode;
8114 TYPE_LANG_SPECIFIC (enumtype) = lt;
8116 /* Fix up all variant types of this enum type. */
8117 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8119 if (tem == enumtype)
8120 continue;
8121 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8122 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8123 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8124 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8125 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8126 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8127 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8128 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
8129 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8130 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8131 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8134 /* Finish debugging output for this type. */
8135 rest_of_type_compilation (enumtype, toplevel);
8137 /* If this enum is defined inside a struct, add it to
8138 struct_types. */
8139 if (warn_cxx_compat
8140 && struct_parse_info != NULL
8141 && !in_sizeof && !in_typeof && !in_alignof)
8142 struct_parse_info->struct_types.safe_push (enumtype);
8144 return enumtype;
8147 /* Build and install a CONST_DECL for one value of the
8148 current enumeration type (one that was begun with start_enum).
8149 DECL_LOC is the location of the enumerator.
8150 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8151 Return a tree-list containing the CONST_DECL and its value.
8152 Assignment of sequential values by default is handled here. */
8154 tree
8155 build_enumerator (location_t decl_loc, location_t loc,
8156 struct c_enum_contents *the_enum, tree name, tree value)
8158 tree decl, type;
8160 /* Validate and default VALUE. */
8162 if (value != 0)
8164 /* Don't issue more errors for error_mark_node (i.e. an
8165 undeclared identifier) - just ignore the value expression. */
8166 if (value == error_mark_node)
8167 value = 0;
8168 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8170 error_at (loc, "enumerator value for %qE is not an integer constant",
8171 name);
8172 value = 0;
8174 else
8176 if (TREE_CODE (value) != INTEGER_CST)
8178 value = c_fully_fold (value, false, NULL);
8179 if (TREE_CODE (value) == INTEGER_CST)
8180 pedwarn (loc, OPT_Wpedantic,
8181 "enumerator value for %qE is not an integer "
8182 "constant expression", name);
8184 if (TREE_CODE (value) != INTEGER_CST)
8186 error ("enumerator value for %qE is not an integer constant",
8187 name);
8188 value = 0;
8190 else
8192 value = default_conversion (value);
8193 constant_expression_warning (value);
8198 /* Default based on previous value. */
8199 /* It should no longer be possible to have NON_LVALUE_EXPR
8200 in the default. */
8201 if (value == 0)
8203 value = the_enum->enum_next_value;
8204 if (the_enum->enum_overflow)
8205 error_at (loc, "overflow in enumeration values");
8207 /* Even though the underlying type of an enum is unspecified, the
8208 type of enumeration constants is explicitly defined as int
8209 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8210 an extension. */
8211 else if (!int_fits_type_p (value, integer_type_node))
8212 pedwarn (loc, OPT_Wpedantic,
8213 "ISO C restricts enumerator values to range of %<int%>");
8215 /* The ISO C Standard mandates enumerators to have type int, even
8216 though the underlying type of an enum type is unspecified.
8217 However, GCC allows enumerators of any integer type as an
8218 extensions. Here we convert any enumerators that fit in an int
8219 to type int, to avoid promotions to unsigned types when comparing
8220 integers with enumerators that fit in the int range. When
8221 -pedantic is given, we would have already warned about those that
8222 don't fit. We have to do this here rather than in finish_enum
8223 because this value may be used to define more enumerators. */
8224 if (int_fits_type_p (value, integer_type_node))
8225 value = convert (integer_type_node, value);
8227 /* Set basis for default for next value. */
8228 the_enum->enum_next_value
8229 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8230 PLUS_EXPR, value, integer_one_node, 0);
8231 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8233 /* Now create a declaration for the enum value name. */
8235 type = TREE_TYPE (value);
8236 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8237 TYPE_PRECISION (integer_type_node)),
8238 (TYPE_PRECISION (type)
8239 >= TYPE_PRECISION (integer_type_node)
8240 && TYPE_UNSIGNED (type)));
8242 decl = build_decl (decl_loc, CONST_DECL, name, type);
8243 DECL_INITIAL (decl) = convert (type, value);
8244 pushdecl (decl);
8246 return tree_cons (decl, value, NULL_TREE);
8250 /* Create the FUNCTION_DECL for a function definition.
8251 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8252 the declaration; they describe the function's name and the type it returns,
8253 but twisted together in a fashion that parallels the syntax of C.
8255 This function creates a binding context for the function body
8256 as well as setting up the FUNCTION_DECL in current_function_decl.
8258 Returns 1 on success. If the DECLARATOR is not suitable for a function
8259 (it defines a datum instead), we return 0, which tells
8260 yyparse to report a parse error. */
8263 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8264 tree attributes)
8266 tree decl1, old_decl;
8267 tree restype, resdecl;
8268 location_t loc;
8270 current_function_returns_value = 0; /* Assume, until we see it does. */
8271 current_function_returns_null = 0;
8272 current_function_returns_abnormally = 0;
8273 warn_about_return_type = 0;
8274 c_switch_stack = NULL;
8276 /* Indicate no valid break/continue context by setting these variables
8277 to some non-null, non-label value. We'll notice and emit the proper
8278 error message in c_finish_bc_stmt. */
8279 c_break_label = c_cont_label = size_zero_node;
8281 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8282 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8283 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8285 /* If the declarator is not suitable for a function definition,
8286 cause a syntax error. */
8287 if (decl1 == 0
8288 || TREE_CODE (decl1) != FUNCTION_DECL)
8289 return 0;
8291 loc = DECL_SOURCE_LOCATION (decl1);
8293 c_decl_attributes (&decl1, attributes, 0);
8295 if (DECL_DECLARED_INLINE_P (decl1)
8296 && DECL_UNINLINABLE (decl1)
8297 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8298 warning_at (loc, OPT_Wattributes,
8299 "inline function %qD given attribute noinline",
8300 decl1);
8302 /* Handle gnu_inline attribute. */
8303 if (declspecs->inline_p
8304 && !flag_gnu89_inline
8305 && TREE_CODE (decl1) == FUNCTION_DECL
8306 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8307 || current_function_decl))
8309 if (declspecs->storage_class != csc_static)
8310 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8313 announce_function (decl1);
8315 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8317 error_at (loc, "return type is an incomplete type");
8318 /* Make it return void instead. */
8319 TREE_TYPE (decl1)
8320 = build_function_type (void_type_node,
8321 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8324 if (warn_about_return_type)
8325 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8326 : (warn_return_type ? OPT_Wreturn_type
8327 : OPT_Wimplicit_int),
8328 "return type defaults to %<int%>");
8330 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8331 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8332 DECL_INITIAL (decl1) = error_mark_node;
8334 /* A nested function is not global. */
8335 if (current_function_decl != 0)
8336 TREE_PUBLIC (decl1) = 0;
8338 /* If this definition isn't a prototype and we had a prototype declaration
8339 before, copy the arg type info from that prototype. */
8340 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8341 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8342 old_decl = 0;
8343 current_function_prototype_locus = UNKNOWN_LOCATION;
8344 current_function_prototype_built_in = false;
8345 current_function_prototype_arg_types = NULL_TREE;
8346 if (!prototype_p (TREE_TYPE (decl1)))
8348 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8349 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8350 TREE_TYPE (TREE_TYPE (old_decl))))
8352 if (stdarg_p (TREE_TYPE (old_decl)))
8354 warning_at (loc, 0, "%q+D defined as variadic function "
8355 "without prototype", decl1);
8356 locate_old_decl (old_decl);
8358 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8359 TREE_TYPE (decl1));
8360 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8361 current_function_prototype_built_in
8362 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8363 current_function_prototype_arg_types
8364 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8366 if (TREE_PUBLIC (decl1))
8368 /* If there is an external prototype declaration of this
8369 function, record its location but do not copy information
8370 to this decl. This may be an invisible declaration
8371 (built-in or in a scope which has finished) or simply
8372 have more refined argument types than any declaration
8373 found above. */
8374 struct c_binding *b;
8375 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8376 if (B_IN_SCOPE (b, external_scope))
8377 break;
8378 if (b)
8380 tree ext_decl, ext_type;
8381 ext_decl = b->decl;
8382 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8383 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8384 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8385 TREE_TYPE (ext_type)))
8387 current_function_prototype_locus
8388 = DECL_SOURCE_LOCATION (ext_decl);
8389 current_function_prototype_built_in
8390 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8391 current_function_prototype_arg_types
8392 = TYPE_ARG_TYPES (ext_type);
8398 /* Optionally warn of old-fashioned def with no previous prototype. */
8399 if (warn_strict_prototypes
8400 && old_decl != error_mark_node
8401 && !prototype_p (TREE_TYPE (decl1))
8402 && C_DECL_ISNT_PROTOTYPE (old_decl))
8403 warning_at (loc, OPT_Wstrict_prototypes,
8404 "function declaration isn%'t a prototype");
8405 /* Optionally warn of any global def with no previous prototype. */
8406 else if (warn_missing_prototypes
8407 && old_decl != error_mark_node
8408 && TREE_PUBLIC (decl1)
8409 && !MAIN_NAME_P (DECL_NAME (decl1))
8410 && C_DECL_ISNT_PROTOTYPE (old_decl)
8411 && !DECL_DECLARED_INLINE_P (decl1))
8412 warning_at (loc, OPT_Wmissing_prototypes,
8413 "no previous prototype for %qD", decl1);
8414 /* Optionally warn of any def with no previous prototype
8415 if the function has already been used. */
8416 else if (warn_missing_prototypes
8417 && old_decl != 0
8418 && old_decl != error_mark_node
8419 && TREE_USED (old_decl)
8420 && !prototype_p (TREE_TYPE (old_decl)))
8421 warning_at (loc, OPT_Wmissing_prototypes,
8422 "%qD was used with no prototype before its definition", decl1);
8423 /* Optionally warn of any global def with no previous declaration. */
8424 else if (warn_missing_declarations
8425 && TREE_PUBLIC (decl1)
8426 && old_decl == 0
8427 && !MAIN_NAME_P (DECL_NAME (decl1))
8428 && !DECL_DECLARED_INLINE_P (decl1))
8429 warning_at (loc, OPT_Wmissing_declarations,
8430 "no previous declaration for %qD",
8431 decl1);
8432 /* Optionally warn of any def with no previous declaration
8433 if the function has already been used. */
8434 else if (warn_missing_declarations
8435 && old_decl != 0
8436 && old_decl != error_mark_node
8437 && TREE_USED (old_decl)
8438 && C_DECL_IMPLICIT (old_decl))
8439 warning_at (loc, OPT_Wmissing_declarations,
8440 "%qD was used with no declaration before its definition", decl1);
8442 /* This function exists in static storage.
8443 (This does not mean `static' in the C sense!) */
8444 TREE_STATIC (decl1) = 1;
8446 /* This is the earliest point at which we might know the assembler
8447 name of the function. Thus, if it's set before this, die horribly. */
8448 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8450 /* If #pragma weak was used, mark the decl weak now. */
8451 if (current_scope == file_scope)
8452 maybe_apply_pragma_weak (decl1);
8454 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8455 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8457 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8458 != integer_type_node)
8459 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8460 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8461 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8462 decl1);
8464 check_main_parameter_types (decl1);
8466 if (!TREE_PUBLIC (decl1))
8467 pedwarn (loc, OPT_Wmain,
8468 "%qD is normally a non-static function", decl1);
8471 /* Record the decl so that the function name is defined.
8472 If we already have a decl for this name, and it is a FUNCTION_DECL,
8473 use the old decl. */
8475 current_function_decl = pushdecl (decl1);
8477 push_scope ();
8478 declare_parm_level ();
8480 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8481 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8482 DECL_ARTIFICIAL (resdecl) = 1;
8483 DECL_IGNORED_P (resdecl) = 1;
8484 DECL_RESULT (current_function_decl) = resdecl;
8486 start_fname_decls ();
8488 return 1;
8491 /* Subroutine of store_parm_decls which handles new-style function
8492 definitions (prototype format). The parms already have decls, so we
8493 need only record them as in effect and complain if any redundant
8494 old-style parm decls were written. */
8495 static void
8496 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8498 tree decl;
8499 c_arg_tag *tag;
8500 unsigned ix;
8502 if (current_scope->bindings)
8504 error_at (DECL_SOURCE_LOCATION (fndecl),
8505 "old-style parameter declarations in prototyped "
8506 "function definition");
8508 /* Get rid of the old-style declarations. */
8509 pop_scope ();
8510 push_scope ();
8512 /* Don't issue this warning for nested functions, and don't issue this
8513 warning if we got here because ARG_INFO_TYPES was error_mark_node
8514 (this happens when a function definition has just an ellipsis in
8515 its parameter list). */
8516 else if (!in_system_header_at (input_location)
8517 && !current_function_scope
8518 && arg_info->types != error_mark_node)
8519 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8520 "traditional C rejects ISO C style function definitions");
8522 /* Now make all the parameter declarations visible in the function body.
8523 We can bypass most of the grunt work of pushdecl. */
8524 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8526 DECL_CONTEXT (decl) = current_function_decl;
8527 if (DECL_NAME (decl))
8529 bind (DECL_NAME (decl), decl, current_scope,
8530 /*invisible=*/false, /*nested=*/false,
8531 UNKNOWN_LOCATION);
8532 if (!TREE_USED (decl))
8533 warn_if_shadowing (decl);
8535 else
8536 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8539 /* Record the parameter list in the function declaration. */
8540 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8542 /* Now make all the ancillary declarations visible, likewise. */
8543 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8545 DECL_CONTEXT (decl) = current_function_decl;
8546 if (DECL_NAME (decl))
8547 bind (DECL_NAME (decl), decl, current_scope,
8548 /*invisible=*/false,
8549 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8550 UNKNOWN_LOCATION);
8553 /* And all the tag declarations. */
8554 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8555 if (tag->id)
8556 bind (tag->id, tag->type, current_scope,
8557 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8560 /* Subroutine of store_parm_decls which handles old-style function
8561 definitions (separate parameter list and declarations). */
8563 static void
8564 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8566 struct c_binding *b;
8567 tree parm, decl, last;
8568 tree parmids = arg_info->parms;
8569 hash_set<tree> seen_args;
8571 if (!in_system_header_at (input_location))
8572 warning_at (DECL_SOURCE_LOCATION (fndecl),
8573 OPT_Wold_style_definition, "old-style function definition");
8575 /* Match each formal parameter name with its declaration. Save each
8576 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8577 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8579 if (TREE_VALUE (parm) == 0)
8581 error_at (DECL_SOURCE_LOCATION (fndecl),
8582 "parameter name missing from parameter list");
8583 TREE_PURPOSE (parm) = 0;
8584 continue;
8587 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8588 if (b && B_IN_CURRENT_SCOPE (b))
8590 decl = b->decl;
8591 /* Skip erroneous parameters. */
8592 if (decl == error_mark_node)
8593 continue;
8594 /* If we got something other than a PARM_DECL it is an error. */
8595 if (TREE_CODE (decl) != PARM_DECL)
8596 error_at (DECL_SOURCE_LOCATION (decl),
8597 "%qD declared as a non-parameter", decl);
8598 /* If the declaration is already marked, we have a duplicate
8599 name. Complain and ignore the duplicate. */
8600 else if (seen_args.contains (decl))
8602 error_at (DECL_SOURCE_LOCATION (decl),
8603 "multiple parameters named %qD", decl);
8604 TREE_PURPOSE (parm) = 0;
8605 continue;
8607 /* If the declaration says "void", complain and turn it into
8608 an int. */
8609 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8611 error_at (DECL_SOURCE_LOCATION (decl),
8612 "parameter %qD declared with void type", decl);
8613 TREE_TYPE (decl) = integer_type_node;
8614 DECL_ARG_TYPE (decl) = integer_type_node;
8615 layout_decl (decl, 0);
8617 warn_if_shadowing (decl);
8619 /* If no declaration found, default to int. */
8620 else
8622 /* FIXME diagnostics: This should be the location of the argument,
8623 not the FNDECL. E.g., for an old-style declaration
8625 int f10(v) { blah; }
8627 We should use the location of the V, not the F10.
8628 Unfortunately, the V is an IDENTIFIER_NODE which has no
8629 location. In the future we need locations for c_arg_info
8630 entries.
8632 See gcc.dg/Wshadow-3.c for an example of this problem. */
8633 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8634 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8635 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8636 pushdecl (decl);
8637 warn_if_shadowing (decl);
8639 if (flag_isoc99)
8640 pedwarn (DECL_SOURCE_LOCATION (decl),
8641 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8642 decl);
8643 else
8644 warning_at (DECL_SOURCE_LOCATION (decl),
8645 OPT_Wmissing_parameter_type,
8646 "type of %qD defaults to %<int%>", decl);
8649 TREE_PURPOSE (parm) = decl;
8650 seen_args.add (decl);
8653 /* Now examine the parms chain for incomplete declarations
8654 and declarations with no corresponding names. */
8656 for (b = current_scope->bindings; b; b = b->prev)
8658 parm = b->decl;
8659 if (TREE_CODE (parm) != PARM_DECL)
8660 continue;
8662 if (TREE_TYPE (parm) != error_mark_node
8663 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8665 error_at (DECL_SOURCE_LOCATION (parm),
8666 "parameter %qD has incomplete type", parm);
8667 TREE_TYPE (parm) = error_mark_node;
8670 if (!seen_args.contains (parm))
8672 error_at (DECL_SOURCE_LOCATION (parm),
8673 "declaration for parameter %qD but no such parameter",
8674 parm);
8676 /* Pretend the parameter was not missing.
8677 This gets us to a standard state and minimizes
8678 further error messages. */
8679 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8683 /* Chain the declarations together in the order of the list of
8684 names. Store that chain in the function decl, replacing the
8685 list of names. Update the current scope to match. */
8686 DECL_ARGUMENTS (fndecl) = 0;
8688 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8689 if (TREE_PURPOSE (parm))
8690 break;
8691 if (parm && TREE_PURPOSE (parm))
8693 last = TREE_PURPOSE (parm);
8694 DECL_ARGUMENTS (fndecl) = last;
8696 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8697 if (TREE_PURPOSE (parm))
8699 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8700 last = TREE_PURPOSE (parm);
8702 DECL_CHAIN (last) = 0;
8705 /* If there was a previous prototype,
8706 set the DECL_ARG_TYPE of each argument according to
8707 the type previously specified, and report any mismatches. */
8709 if (current_function_prototype_arg_types)
8711 tree type;
8712 for (parm = DECL_ARGUMENTS (fndecl),
8713 type = current_function_prototype_arg_types;
8714 parm || (type && TREE_VALUE (type) != error_mark_node
8715 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8716 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8718 if (parm == 0 || type == 0
8719 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8721 if (current_function_prototype_built_in)
8722 warning_at (DECL_SOURCE_LOCATION (fndecl),
8723 0, "number of arguments doesn%'t match "
8724 "built-in prototype");
8725 else
8727 /* FIXME diagnostics: This should be the location of
8728 FNDECL, but there is bug when a prototype is
8729 declared inside function context, but defined
8730 outside of it (e.g., gcc.dg/pr15698-2.c). In
8731 which case FNDECL gets the location of the
8732 prototype, not the definition. */
8733 error_at (input_location,
8734 "number of arguments doesn%'t match prototype");
8736 error_at (current_function_prototype_locus,
8737 "prototype declaration");
8739 break;
8741 /* Type for passing arg must be consistent with that
8742 declared for the arg. ISO C says we take the unqualified
8743 type for parameters declared with qualified type. */
8744 if (TREE_TYPE (parm) != error_mark_node
8745 && TREE_TYPE (type) != error_mark_node
8746 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8747 != TYPE_ATOMIC (TREE_VALUE (type)))
8748 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8749 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8751 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8752 == TYPE_ATOMIC (TREE_VALUE (type)))
8753 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8754 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8756 /* Adjust argument to match prototype. E.g. a previous
8757 `int foo(float);' prototype causes
8758 `int foo(x) float x; {...}' to be treated like
8759 `int foo(float x) {...}'. This is particularly
8760 useful for argument types like uid_t. */
8761 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8763 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8764 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8765 && TYPE_PRECISION (TREE_TYPE (parm))
8766 < TYPE_PRECISION (integer_type_node))
8767 DECL_ARG_TYPE (parm)
8768 = c_type_promotes_to (TREE_TYPE (parm));
8770 /* ??? Is it possible to get here with a
8771 built-in prototype or will it always have
8772 been diagnosed as conflicting with an
8773 old-style definition and discarded? */
8774 if (current_function_prototype_built_in)
8775 warning_at (DECL_SOURCE_LOCATION (parm),
8776 OPT_Wpedantic, "promoted argument %qD "
8777 "doesn%'t match built-in prototype", parm);
8778 else
8780 pedwarn (DECL_SOURCE_LOCATION (parm),
8781 OPT_Wpedantic, "promoted argument %qD "
8782 "doesn%'t match prototype", parm);
8783 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8784 "prototype declaration");
8787 else
8789 if (current_function_prototype_built_in)
8790 warning_at (DECL_SOURCE_LOCATION (parm),
8791 0, "argument %qD doesn%'t match "
8792 "built-in prototype", parm);
8793 else
8795 error_at (DECL_SOURCE_LOCATION (parm),
8796 "argument %qD doesn%'t match prototype", parm);
8797 error_at (current_function_prototype_locus,
8798 "prototype declaration");
8803 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8806 /* Otherwise, create a prototype that would match. */
8808 else
8810 tree actual = 0, last = 0, type;
8812 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8814 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8815 if (last)
8816 TREE_CHAIN (last) = type;
8817 else
8818 actual = type;
8819 last = type;
8821 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8822 if (last)
8823 TREE_CHAIN (last) = type;
8824 else
8825 actual = type;
8827 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8828 of the type of this function, but we need to avoid having this
8829 affect the types of other similarly-typed functions, so we must
8830 first force the generation of an identical (but separate) type
8831 node for the relevant function type. The new node we create
8832 will be a variant of the main variant of the original function
8833 type. */
8835 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8837 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8841 /* Store parameter declarations passed in ARG_INFO into the current
8842 function declaration. */
8844 void
8845 store_parm_decls_from (struct c_arg_info *arg_info)
8847 current_function_arg_info = arg_info;
8848 store_parm_decls ();
8851 /* Called by walk_tree to look for and update context-less labels. */
8853 static tree
8854 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
8856 if (TREE_CODE (*tp) == LABEL_EXPR
8857 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
8859 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
8860 *walk_subtrees = 0;
8863 return NULL_TREE;
8866 /* Store the parameter declarations into the current function declaration.
8867 This is called after parsing the parameter declarations, before
8868 digesting the body of the function.
8870 For an old-style definition, construct a prototype out of the old-style
8871 parameter declarations and inject it into the function's type. */
8873 void
8874 store_parm_decls (void)
8876 tree fndecl = current_function_decl;
8877 bool proto;
8879 /* The argument information block for FNDECL. */
8880 struct c_arg_info *arg_info = current_function_arg_info;
8881 current_function_arg_info = 0;
8883 /* True if this definition is written with a prototype. Note:
8884 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8885 list in a function definition as equivalent to (void) -- an
8886 empty argument list specifies the function has no parameters,
8887 but only (void) sets up a prototype for future calls. */
8888 proto = arg_info->types != 0;
8890 if (proto)
8891 store_parm_decls_newstyle (fndecl, arg_info);
8892 else
8893 store_parm_decls_oldstyle (fndecl, arg_info);
8895 /* The next call to push_scope will be a function body. */
8897 next_is_function_body = true;
8899 /* Write a record describing this function definition to the prototypes
8900 file (if requested). */
8902 gen_aux_info_record (fndecl, 1, 0, proto);
8904 /* Initialize the RTL code for the function. */
8905 allocate_struct_function (fndecl, false);
8907 if (warn_unused_local_typedefs)
8908 cfun->language = ggc_cleared_alloc<language_function> ();
8910 /* Begin the statement tree for this function. */
8911 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8913 /* ??? Insert the contents of the pending sizes list into the function
8914 to be evaluated. The only reason left to have this is
8915 void foo(int n, int array[n++])
8916 because we throw away the array type in favor of a pointer type, and
8917 thus won't naturally see the SAVE_EXPR containing the increment. All
8918 other pending sizes would be handled by gimplify_parameters. */
8919 if (arg_info->pending_sizes)
8921 /* In very special circumstances, e.g. for code like
8922 _Atomic int i = 5;
8923 void f (int a[i += 2]) {}
8924 we need to execute the atomic assignment on function entry.
8925 But in this case, it is not just a straight store, it has the
8926 op= form, which means that build_atomic_assign has generated
8927 gotos, labels, etc. Because at that time the function decl
8928 for F has not been created yet, those labels do not have any
8929 function context. But we have the fndecl now, so update the
8930 labels accordingly. gimplify_expr would crash otherwise. */
8931 walk_tree_without_duplicates (&arg_info->pending_sizes,
8932 set_labels_context_r, fndecl);
8933 add_stmt (arg_info->pending_sizes);
8937 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8938 c_finish_omp_declare_simd for function prototypes. No diagnostics
8939 should be done. */
8941 void
8942 temp_store_parm_decls (tree fndecl, tree parms)
8944 push_scope ();
8945 for (tree p = parms; p; p = DECL_CHAIN (p))
8947 DECL_CONTEXT (p) = fndecl;
8948 if (DECL_NAME (p))
8949 bind (DECL_NAME (p), p, current_scope,
8950 /*invisible=*/false, /*nested=*/false,
8951 UNKNOWN_LOCATION);
8955 /* Undo what temp_store_parm_decls did. */
8957 void
8958 temp_pop_parm_decls (void)
8960 /* Clear all bindings in this temporary scope, so that
8961 pop_scope doesn't create a BLOCK. */
8962 struct c_binding *b = current_scope->bindings;
8963 current_scope->bindings = NULL;
8964 for (; b; b = free_binding_and_advance (b))
8966 gcc_assert (TREE_CODE (b->decl) == PARM_DECL
8967 || b->decl == error_mark_node);
8968 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8969 I_SYMBOL_BINDING (b->id) = b->shadowed;
8970 if (b->shadowed && b->shadowed->u.type)
8971 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
8973 pop_scope ();
8977 /* Finish up a function declaration and compile that function
8978 all the way to assembler language output. Then free the storage
8979 for the function definition.
8981 This is called after parsing the body of the function definition. */
8983 void
8984 finish_function (void)
8986 tree fndecl = current_function_decl;
8988 if (c_dialect_objc ())
8989 objc_finish_function ();
8991 if (TREE_CODE (fndecl) == FUNCTION_DECL
8992 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8994 tree args = DECL_ARGUMENTS (fndecl);
8995 for (; args; args = DECL_CHAIN (args))
8997 tree type = TREE_TYPE (args);
8998 if (INTEGRAL_TYPE_P (type)
8999 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9000 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
9004 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
9005 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
9007 /* Must mark the RESULT_DECL as being in this function. */
9009 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
9010 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
9012 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
9013 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
9014 == integer_type_node && flag_isoc99)
9016 /* Hack. We don't want the middle-end to warn that this return
9017 is unreachable, so we mark its location as special. Using
9018 UNKNOWN_LOCATION has the problem that it gets clobbered in
9019 annotate_one_with_locus. A cleaner solution might be to
9020 ensure ! should_carry_locus_p (stmt), but that needs a flag.
9022 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
9025 /* Tie off the statement tree for this function. */
9026 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
9028 /* If the function has _Cilk_spawn in front of a function call inside it
9029 i.e. it is a spawning function, then add the appropriate Cilk plus
9030 functions inside. */
9031 if (fn_contains_cilk_spawn_p (cfun))
9032 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
9034 finish_fname_decls ();
9036 /* Complain if there's just no return statement. */
9037 if (warn_return_type
9038 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
9039 && !current_function_returns_value && !current_function_returns_null
9040 /* Don't complain if we are no-return. */
9041 && !current_function_returns_abnormally
9042 /* Don't complain if we are declared noreturn. */
9043 && !TREE_THIS_VOLATILE (fndecl)
9044 /* Don't warn for main(). */
9045 && !MAIN_NAME_P (DECL_NAME (fndecl))
9046 /* Or if they didn't actually specify a return type. */
9047 && !C_FUNCTION_IMPLICIT_INT (fndecl)
9048 /* Normally, with -Wreturn-type, flow will complain, but we might
9049 optimize out static functions. */
9050 && !TREE_PUBLIC (fndecl))
9052 warning (OPT_Wreturn_type,
9053 "no return statement in function returning non-void");
9054 TREE_NO_WARNING (fndecl) = 1;
9057 /* Complain about parameters that are only set, but never otherwise used. */
9058 if (warn_unused_but_set_parameter)
9060 tree decl;
9062 for (decl = DECL_ARGUMENTS (fndecl);
9063 decl;
9064 decl = DECL_CHAIN (decl))
9065 if (TREE_USED (decl)
9066 && TREE_CODE (decl) == PARM_DECL
9067 && !DECL_READ_P (decl)
9068 && DECL_NAME (decl)
9069 && !DECL_ARTIFICIAL (decl)
9070 && !TREE_NO_WARNING (decl))
9071 warning_at (DECL_SOURCE_LOCATION (decl),
9072 OPT_Wunused_but_set_parameter,
9073 "parameter %qD set but not used", decl);
9076 /* Complain about locally defined typedefs that are not used in this
9077 function. */
9078 maybe_warn_unused_local_typedefs ();
9080 /* Possibly warn about unused parameters. */
9081 if (warn_unused_parameter)
9082 do_warn_unused_parameter (fndecl);
9084 /* Store the end of the function, so that we get good line number
9085 info for the epilogue. */
9086 cfun->function_end_locus = input_location;
9088 /* Finalize the ELF visibility for the function. */
9089 c_determine_visibility (fndecl);
9091 /* For GNU C extern inline functions disregard inline limits. */
9092 if (DECL_EXTERNAL (fndecl)
9093 && DECL_DECLARED_INLINE_P (fndecl))
9094 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9096 /* Genericize before inlining. Delay genericizing nested functions
9097 until their parent function is genericized. Since finalizing
9098 requires GENERIC, delay that as well. */
9100 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9101 && !undef_nested_function)
9103 if (!decl_function_context (fndecl))
9105 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9106 c_genericize (fndecl);
9108 /* ??? Objc emits functions after finalizing the compilation unit.
9109 This should be cleaned up later and this conditional removed. */
9110 if (symtab->global_info_ready)
9112 cgraph_node::add_new_function (fndecl, false);
9113 return;
9115 cgraph_node::finalize_function (fndecl, false);
9117 else
9119 /* Register this function with cgraph just far enough to get it
9120 added to our parent's nested function list. Handy, since the
9121 C front end doesn't have such a list. */
9122 (void) cgraph_node::get_create (fndecl);
9126 if (!decl_function_context (fndecl))
9127 undef_nested_function = false;
9129 if (cfun->language != NULL)
9131 ggc_free (cfun->language);
9132 cfun->language = NULL;
9135 /* We're leaving the context of this function, so zap cfun.
9136 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9137 tree_rest_of_compilation. */
9138 set_cfun (NULL);
9139 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9140 current_function_decl = NULL;
9143 /* Check the declarations given in a for-loop for satisfying the C99
9144 constraints. If exactly one such decl is found, return it. LOC is
9145 the location of the opening parenthesis of the for loop. The last
9146 parameter allows you to control the "for loop initial declarations
9147 are only allowed in C99 mode". Normally, you should pass
9148 flag_isoc99 as that parameter. But in some cases (Objective-C
9149 foreach loop, for example) we want to run the checks in this
9150 function even if not in C99 mode, so we allow the caller to turn
9151 off the error about not being in C99 mode.
9154 tree
9155 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9157 struct c_binding *b;
9158 tree one_decl = NULL_TREE;
9159 int n_decls = 0;
9161 if (!turn_off_iso_c99_error)
9163 static bool hint = true;
9164 /* If we get here, declarations have been used in a for loop without
9165 the C99 for loop scope. This doesn't make much sense, so don't
9166 allow it. */
9167 error_at (loc, "%<for%> loop initial declarations "
9168 "are only allowed in C99 or C11 mode");
9169 if (hint)
9171 inform (loc,
9172 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9173 "to compile your code");
9174 hint = false;
9176 return NULL_TREE;
9178 /* C99 subclause 6.8.5 paragraph 3:
9180 [#3] The declaration part of a for statement shall only
9181 declare identifiers for objects having storage class auto or
9182 register.
9184 It isn't clear whether, in this sentence, "identifiers" binds to
9185 "shall only declare" or to "objects" - that is, whether all identifiers
9186 declared must be identifiers for objects, or whether the restriction
9187 only applies to those that are. (A question on this in comp.std.c
9188 in November 2000 received no answer.) We implement the strictest
9189 interpretation, to avoid creating an extension which later causes
9190 problems. */
9192 for (b = current_scope->bindings; b; b = b->prev)
9194 tree id = b->id;
9195 tree decl = b->decl;
9197 if (!id)
9198 continue;
9200 switch (TREE_CODE (decl))
9202 case VAR_DECL:
9204 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9205 if (TREE_STATIC (decl))
9206 error_at (decl_loc,
9207 "declaration of static variable %qD in %<for%> loop "
9208 "initial declaration", decl);
9209 else if (DECL_EXTERNAL (decl))
9210 error_at (decl_loc,
9211 "declaration of %<extern%> variable %qD in %<for%> loop "
9212 "initial declaration", decl);
9214 break;
9216 case RECORD_TYPE:
9217 error_at (loc,
9218 "%<struct %E%> declared in %<for%> loop initial "
9219 "declaration", id);
9220 break;
9221 case UNION_TYPE:
9222 error_at (loc,
9223 "%<union %E%> declared in %<for%> loop initial declaration",
9224 id);
9225 break;
9226 case ENUMERAL_TYPE:
9227 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9228 "initial declaration", id);
9229 break;
9230 default:
9231 error_at (loc, "declaration of non-variable "
9232 "%qD in %<for%> loop initial declaration", decl);
9235 n_decls++;
9236 one_decl = decl;
9239 return n_decls == 1 ? one_decl : NULL_TREE;
9242 /* Save and reinitialize the variables
9243 used during compilation of a C function. */
9245 void
9246 c_push_function_context (void)
9248 struct language_function *p = cfun->language;
9249 /* cfun->language might have been already allocated by the use of
9250 -Wunused-local-typedefs. In that case, just re-use it. */
9251 if (p == NULL)
9252 cfun->language = p = ggc_cleared_alloc<language_function> ();
9254 p->base.x_stmt_tree = c_stmt_tree;
9255 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9256 p->x_break_label = c_break_label;
9257 p->x_cont_label = c_cont_label;
9258 p->x_switch_stack = c_switch_stack;
9259 p->arg_info = current_function_arg_info;
9260 p->returns_value = current_function_returns_value;
9261 p->returns_null = current_function_returns_null;
9262 p->returns_abnormally = current_function_returns_abnormally;
9263 p->warn_about_return_type = warn_about_return_type;
9265 push_function_context ();
9268 /* Restore the variables used during compilation of a C function. */
9270 void
9271 c_pop_function_context (void)
9273 struct language_function *p;
9275 pop_function_context ();
9276 p = cfun->language;
9278 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9279 used to store data throughout the life time of the current cfun,
9280 So don't deallocate it. */
9281 if (!warn_unused_local_typedefs)
9282 cfun->language = NULL;
9284 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9285 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9287 /* Stop pointing to the local nodes about to be freed. */
9288 /* But DECL_INITIAL must remain nonzero so we know this
9289 was an actual function definition. */
9290 DECL_INITIAL (current_function_decl) = error_mark_node;
9291 DECL_ARGUMENTS (current_function_decl) = 0;
9294 c_stmt_tree = p->base.x_stmt_tree;
9295 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9296 c_break_label = p->x_break_label;
9297 c_cont_label = p->x_cont_label;
9298 c_switch_stack = p->x_switch_stack;
9299 current_function_arg_info = p->arg_info;
9300 current_function_returns_value = p->returns_value;
9301 current_function_returns_null = p->returns_null;
9302 current_function_returns_abnormally = p->returns_abnormally;
9303 warn_about_return_type = p->warn_about_return_type;
9306 /* The functions below are required for functionality of doing
9307 function at once processing in the C front end. Currently these
9308 functions are not called from anywhere in the C front end, but as
9309 these changes continue, that will change. */
9311 /* Returns the stmt_tree (if any) to which statements are currently
9312 being added. If there is no active statement-tree, NULL is
9313 returned. */
9315 stmt_tree
9316 current_stmt_tree (void)
9318 return &c_stmt_tree;
9321 /* Return the global value of T as a symbol. */
9323 tree
9324 identifier_global_value (tree t)
9326 struct c_binding *b;
9328 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9329 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9330 return b->decl;
9332 return 0;
9335 /* In C, the only C-linkage public declaration is at file scope. */
9337 tree
9338 c_linkage_bindings (tree name)
9340 return identifier_global_value (name);
9343 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9344 otherwise the name is found in ridpointers from RID_INDEX. */
9346 void
9347 record_builtin_type (enum rid rid_index, const char *name, tree type)
9349 tree id, decl;
9350 if (name == 0)
9351 id = ridpointers[(int) rid_index];
9352 else
9353 id = get_identifier (name);
9354 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9355 pushdecl (decl);
9356 if (debug_hooks->type_decl)
9357 debug_hooks->type_decl (decl, false);
9360 /* Build the void_list_node (void_type_node having been created). */
9361 tree
9362 build_void_list_node (void)
9364 tree t = build_tree_list (NULL_TREE, void_type_node);
9365 return t;
9368 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9370 struct c_parm *
9371 build_c_parm (struct c_declspecs *specs, tree attrs,
9372 struct c_declarator *declarator)
9374 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9375 ret->specs = specs;
9376 ret->attrs = attrs;
9377 ret->declarator = declarator;
9378 return ret;
9381 /* Return a declarator with nested attributes. TARGET is the inner
9382 declarator to which these attributes apply. ATTRS are the
9383 attributes. */
9385 struct c_declarator *
9386 build_attrs_declarator (tree attrs, struct c_declarator *target)
9388 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9389 ret->kind = cdk_attrs;
9390 ret->declarator = target;
9391 ret->u.attrs = attrs;
9392 return ret;
9395 /* Return a declarator for a function with arguments specified by ARGS
9396 and return type specified by TARGET. */
9398 struct c_declarator *
9399 build_function_declarator (struct c_arg_info *args,
9400 struct c_declarator *target)
9402 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9403 ret->kind = cdk_function;
9404 ret->declarator = target;
9405 ret->u.arg_info = args;
9406 return ret;
9409 /* Return a declarator for the identifier IDENT (which may be
9410 NULL_TREE for an abstract declarator). */
9412 struct c_declarator *
9413 build_id_declarator (tree ident)
9415 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9416 ret->kind = cdk_id;
9417 ret->declarator = 0;
9418 ret->u.id = ident;
9419 /* Default value - may get reset to a more precise location. */
9420 ret->id_loc = input_location;
9421 return ret;
9424 /* Return something to represent absolute declarators containing a *.
9425 TARGET is the absolute declarator that the * contains.
9426 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9427 to apply to the pointer type. */
9429 struct c_declarator *
9430 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9431 struct c_declarator *target)
9433 tree attrs;
9434 int quals = 0;
9435 struct c_declarator *itarget = target;
9436 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9437 if (type_quals_attrs)
9439 attrs = type_quals_attrs->attrs;
9440 quals = quals_from_declspecs (type_quals_attrs);
9441 if (attrs != NULL_TREE)
9442 itarget = build_attrs_declarator (attrs, target);
9444 ret->kind = cdk_pointer;
9445 ret->declarator = itarget;
9446 ret->u.pointer_quals = quals;
9447 return ret;
9450 /* Return a pointer to a structure for an empty list of declaration
9451 specifiers. */
9453 struct c_declspecs *
9454 build_null_declspecs (void)
9456 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9457 memset (&ret->locations, 0, cdw_number_of_elements);
9458 ret->type = 0;
9459 ret->expr = 0;
9460 ret->decl_attr = 0;
9461 ret->attrs = 0;
9462 ret->align_log = -1;
9463 ret->typespec_word = cts_none;
9464 ret->storage_class = csc_none;
9465 ret->expr_const_operands = true;
9466 ret->declspecs_seen_p = false;
9467 ret->typespec_kind = ctsk_none;
9468 ret->non_sc_seen_p = false;
9469 ret->typedef_p = false;
9470 ret->explicit_signed_p = false;
9471 ret->deprecated_p = false;
9472 ret->default_int_p = false;
9473 ret->long_p = false;
9474 ret->long_long_p = false;
9475 ret->short_p = false;
9476 ret->signed_p = false;
9477 ret->unsigned_p = false;
9478 ret->complex_p = false;
9479 ret->inline_p = false;
9480 ret->noreturn_p = false;
9481 ret->thread_p = false;
9482 ret->thread_gnu_p = false;
9483 ret->const_p = false;
9484 ret->volatile_p = false;
9485 ret->atomic_p = false;
9486 ret->restrict_p = false;
9487 ret->saturating_p = false;
9488 ret->alignas_p = false;
9489 ret->address_space = ADDR_SPACE_GENERIC;
9490 return ret;
9493 /* Add the address space ADDRSPACE to the declaration specifiers
9494 SPECS, returning SPECS. */
9496 struct c_declspecs *
9497 declspecs_add_addrspace (source_location location,
9498 struct c_declspecs *specs, addr_space_t as)
9500 specs->non_sc_seen_p = true;
9501 specs->declspecs_seen_p = true;
9503 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9504 && specs->address_space != as)
9505 error ("incompatible address space qualifiers %qs and %qs",
9506 c_addr_space_name (as),
9507 c_addr_space_name (specs->address_space));
9508 else
9510 specs->address_space = as;
9511 specs->locations[cdw_address_space] = location;
9513 return specs;
9516 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9517 returning SPECS. */
9519 struct c_declspecs *
9520 declspecs_add_qual (source_location loc,
9521 struct c_declspecs *specs, tree qual)
9523 enum rid i;
9524 bool dupe = false;
9525 specs->non_sc_seen_p = true;
9526 specs->declspecs_seen_p = true;
9527 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9528 && C_IS_RESERVED_WORD (qual));
9529 i = C_RID_CODE (qual);
9530 switch (i)
9532 case RID_CONST:
9533 dupe = specs->const_p;
9534 specs->const_p = true;
9535 specs->locations[cdw_const] = loc;
9536 break;
9537 case RID_VOLATILE:
9538 dupe = specs->volatile_p;
9539 specs->volatile_p = true;
9540 specs->locations[cdw_volatile] = loc;
9541 break;
9542 case RID_RESTRICT:
9543 dupe = specs->restrict_p;
9544 specs->restrict_p = true;
9545 specs->locations[cdw_restrict] = loc;
9546 break;
9547 case RID_ATOMIC:
9548 dupe = specs->atomic_p;
9549 specs->atomic_p = true;
9550 break;
9551 default:
9552 gcc_unreachable ();
9554 if (dupe)
9555 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9556 return specs;
9559 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9560 returning SPECS. */
9562 struct c_declspecs *
9563 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9564 struct c_typespec spec)
9566 tree type = spec.spec;
9567 specs->non_sc_seen_p = true;
9568 specs->declspecs_seen_p = true;
9569 specs->typespec_kind = spec.kind;
9570 if (TREE_DEPRECATED (type))
9571 specs->deprecated_p = true;
9573 /* Handle type specifier keywords. */
9574 if (TREE_CODE (type) == IDENTIFIER_NODE
9575 && C_IS_RESERVED_WORD (type)
9576 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9578 enum rid i = C_RID_CODE (type);
9579 if (specs->type)
9581 error_at (loc, "two or more data types in declaration specifiers");
9582 return specs;
9584 if ((int) i <= (int) RID_LAST_MODIFIER)
9586 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9587 bool dupe = false;
9588 switch (i)
9590 case RID_LONG:
9591 if (specs->long_long_p)
9593 error_at (loc, "%<long long long%> is too long for GCC");
9594 break;
9596 if (specs->long_p)
9598 if (specs->typespec_word == cts_double)
9600 error_at (loc,
9601 ("both %<long long%> and %<double%> in "
9602 "declaration specifiers"));
9603 break;
9605 pedwarn_c90 (loc, OPT_Wlong_long,
9606 "ISO C90 does not support %<long long%>");
9607 specs->long_long_p = 1;
9608 specs->locations[cdw_long_long] = loc;
9609 break;
9611 if (specs->short_p)
9612 error_at (loc,
9613 ("both %<long%> and %<short%> in "
9614 "declaration specifiers"));
9615 else if (specs->typespec_word == cts_auto_type)
9616 error_at (loc,
9617 ("both %<long%> and %<__auto_type%> in "
9618 "declaration specifiers"));
9619 else if (specs->typespec_word == cts_void)
9620 error_at (loc,
9621 ("both %<long%> and %<void%> in "
9622 "declaration specifiers"));
9623 else if (specs->typespec_word == cts_int_n)
9624 error_at (loc,
9625 ("both %<long%> and %<__int%d%> in "
9626 "declaration specifiers"),
9627 int_n_data[specs->int_n_idx].bitsize);
9628 else if (specs->typespec_word == cts_bool)
9629 error_at (loc,
9630 ("both %<long%> and %<_Bool%> in "
9631 "declaration specifiers"));
9632 else if (specs->typespec_word == cts_char)
9633 error_at (loc,
9634 ("both %<long%> and %<char%> in "
9635 "declaration specifiers"));
9636 else if (specs->typespec_word == cts_float)
9637 error_at (loc,
9638 ("both %<long%> and %<float%> in "
9639 "declaration specifiers"));
9640 else if (specs->typespec_word == cts_dfloat32)
9641 error_at (loc,
9642 ("both %<long%> and %<_Decimal32%> in "
9643 "declaration specifiers"));
9644 else if (specs->typespec_word == cts_dfloat64)
9645 error_at (loc,
9646 ("both %<long%> and %<_Decimal64%> in "
9647 "declaration specifiers"));
9648 else if (specs->typespec_word == cts_dfloat128)
9649 error_at (loc,
9650 ("both %<long%> and %<_Decimal128%> in "
9651 "declaration specifiers"));
9652 else
9654 specs->long_p = true;
9655 specs->locations[cdw_long] = loc;
9657 break;
9658 case RID_SHORT:
9659 dupe = specs->short_p;
9660 if (specs->long_p)
9661 error_at (loc,
9662 ("both %<long%> and %<short%> in "
9663 "declaration specifiers"));
9664 else if (specs->typespec_word == cts_auto_type)
9665 error_at (loc,
9666 ("both %<short%> and %<__auto_type%> in "
9667 "declaration specifiers"));
9668 else if (specs->typespec_word == cts_void)
9669 error_at (loc,
9670 ("both %<short%> and %<void%> in "
9671 "declaration specifiers"));
9672 else if (specs->typespec_word == cts_int_n)
9673 error_at (loc,
9674 ("both %<short%> and %<__int%d%> in "
9675 "declaration specifiers"),
9676 int_n_data[specs->int_n_idx].bitsize);
9677 else if (specs->typespec_word == cts_bool)
9678 error_at (loc,
9679 ("both %<short%> and %<_Bool%> in "
9680 "declaration specifiers"));
9681 else if (specs->typespec_word == cts_char)
9682 error_at (loc,
9683 ("both %<short%> and %<char%> in "
9684 "declaration specifiers"));
9685 else if (specs->typespec_word == cts_float)
9686 error_at (loc,
9687 ("both %<short%> and %<float%> in "
9688 "declaration specifiers"));
9689 else if (specs->typespec_word == cts_double)
9690 error_at (loc,
9691 ("both %<short%> and %<double%> in "
9692 "declaration specifiers"));
9693 else if (specs->typespec_word == cts_dfloat32)
9694 error_at (loc,
9695 ("both %<short%> and %<_Decimal32%> in "
9696 "declaration specifiers"));
9697 else if (specs->typespec_word == cts_dfloat64)
9698 error_at (loc,
9699 ("both %<short%> and %<_Decimal64%> in "
9700 "declaration specifiers"));
9701 else if (specs->typespec_word == cts_dfloat128)
9702 error_at (loc,
9703 ("both %<short%> and %<_Decimal128%> in "
9704 "declaration specifiers"));
9705 else
9707 specs->short_p = true;
9708 specs->locations[cdw_short] = loc;
9710 break;
9711 case RID_SIGNED:
9712 dupe = specs->signed_p;
9713 if (specs->unsigned_p)
9714 error_at (loc,
9715 ("both %<signed%> and %<unsigned%> in "
9716 "declaration specifiers"));
9717 else if (specs->typespec_word == cts_auto_type)
9718 error_at (loc,
9719 ("both %<signed%> and %<__auto_type%> in "
9720 "declaration specifiers"));
9721 else if (specs->typespec_word == cts_void)
9722 error_at (loc,
9723 ("both %<signed%> and %<void%> in "
9724 "declaration specifiers"));
9725 else if (specs->typespec_word == cts_bool)
9726 error_at (loc,
9727 ("both %<signed%> and %<_Bool%> in "
9728 "declaration specifiers"));
9729 else if (specs->typespec_word == cts_float)
9730 error_at (loc,
9731 ("both %<signed%> and %<float%> in "
9732 "declaration specifiers"));
9733 else if (specs->typespec_word == cts_double)
9734 error_at (loc,
9735 ("both %<signed%> and %<double%> in "
9736 "declaration specifiers"));
9737 else if (specs->typespec_word == cts_dfloat32)
9738 error_at (loc,
9739 ("both %<signed%> and %<_Decimal32%> in "
9740 "declaration specifiers"));
9741 else if (specs->typespec_word == cts_dfloat64)
9742 error_at (loc,
9743 ("both %<signed%> and %<_Decimal64%> in "
9744 "declaration specifiers"));
9745 else if (specs->typespec_word == cts_dfloat128)
9746 error_at (loc,
9747 ("both %<signed%> and %<_Decimal128%> in "
9748 "declaration specifiers"));
9749 else
9751 specs->signed_p = true;
9752 specs->locations[cdw_signed] = loc;
9754 break;
9755 case RID_UNSIGNED:
9756 dupe = specs->unsigned_p;
9757 if (specs->signed_p)
9758 error_at (loc,
9759 ("both %<signed%> and %<unsigned%> in "
9760 "declaration specifiers"));
9761 else if (specs->typespec_word == cts_auto_type)
9762 error_at (loc,
9763 ("both %<unsigned%> and %<__auto_type%> in "
9764 "declaration specifiers"));
9765 else if (specs->typespec_word == cts_void)
9766 error_at (loc,
9767 ("both %<unsigned%> and %<void%> in "
9768 "declaration specifiers"));
9769 else if (specs->typespec_word == cts_bool)
9770 error_at (loc,
9771 ("both %<unsigned%> and %<_Bool%> in "
9772 "declaration specifiers"));
9773 else if (specs->typespec_word == cts_float)
9774 error_at (loc,
9775 ("both %<unsigned%> and %<float%> in "
9776 "declaration specifiers"));
9777 else if (specs->typespec_word == cts_double)
9778 error_at (loc,
9779 ("both %<unsigned%> and %<double%> in "
9780 "declaration specifiers"));
9781 else if (specs->typespec_word == cts_dfloat32)
9782 error_at (loc,
9783 ("both %<unsigned%> and %<_Decimal32%> in "
9784 "declaration specifiers"));
9785 else if (specs->typespec_word == cts_dfloat64)
9786 error_at (loc,
9787 ("both %<unsigned%> and %<_Decimal64%> in "
9788 "declaration specifiers"));
9789 else if (specs->typespec_word == cts_dfloat128)
9790 error_at (loc,
9791 ("both %<unsigned%> and %<_Decimal128%> in "
9792 "declaration specifiers"));
9793 else
9795 specs->unsigned_p = true;
9796 specs->locations[cdw_unsigned] = loc;
9798 break;
9799 case RID_COMPLEX:
9800 dupe = specs->complex_p;
9801 if (!in_system_header_at (loc))
9802 pedwarn_c90 (loc, OPT_Wpedantic,
9803 "ISO C90 does not support complex types");
9804 if (specs->typespec_word == cts_auto_type)
9805 error_at (loc,
9806 ("both %<complex%> and %<__auto_type%> in "
9807 "declaration specifiers"));
9808 else if (specs->typespec_word == cts_void)
9809 error_at (loc,
9810 ("both %<complex%> and %<void%> in "
9811 "declaration specifiers"));
9812 else if (specs->typespec_word == cts_bool)
9813 error_at (loc,
9814 ("both %<complex%> and %<_Bool%> in "
9815 "declaration specifiers"));
9816 else if (specs->typespec_word == cts_dfloat32)
9817 error_at (loc,
9818 ("both %<complex%> and %<_Decimal32%> in "
9819 "declaration specifiers"));
9820 else if (specs->typespec_word == cts_dfloat64)
9821 error_at (loc,
9822 ("both %<complex%> and %<_Decimal64%> in "
9823 "declaration specifiers"));
9824 else if (specs->typespec_word == cts_dfloat128)
9825 error_at (loc,
9826 ("both %<complex%> and %<_Decimal128%> in "
9827 "declaration specifiers"));
9828 else if (specs->typespec_word == cts_fract)
9829 error_at (loc,
9830 ("both %<complex%> and %<_Fract%> in "
9831 "declaration specifiers"));
9832 else if (specs->typespec_word == cts_accum)
9833 error_at (loc,
9834 ("both %<complex%> and %<_Accum%> in "
9835 "declaration specifiers"));
9836 else if (specs->saturating_p)
9837 error_at (loc,
9838 ("both %<complex%> and %<_Sat%> in "
9839 "declaration specifiers"));
9840 else
9842 specs->complex_p = true;
9843 specs->locations[cdw_complex] = loc;
9845 break;
9846 case RID_SAT:
9847 dupe = specs->saturating_p;
9848 pedwarn (loc, OPT_Wpedantic,
9849 "ISO C does not support saturating types");
9850 if (specs->typespec_word == cts_int_n)
9852 error_at (loc,
9853 ("both %<_Sat%> and %<__int%d%> in "
9854 "declaration specifiers"),
9855 int_n_data[specs->int_n_idx].bitsize);
9857 else if (specs->typespec_word == cts_auto_type)
9858 error_at (loc,
9859 ("both %<_Sat%> and %<__auto_type%> in "
9860 "declaration specifiers"));
9861 else if (specs->typespec_word == cts_void)
9862 error_at (loc,
9863 ("both %<_Sat%> and %<void%> in "
9864 "declaration specifiers"));
9865 else if (specs->typespec_word == cts_bool)
9866 error_at (loc,
9867 ("both %<_Sat%> and %<_Bool%> in "
9868 "declaration specifiers"));
9869 else if (specs->typespec_word == cts_char)
9870 error_at (loc,
9871 ("both %<_Sat%> and %<char%> in "
9872 "declaration specifiers"));
9873 else if (specs->typespec_word == cts_int)
9874 error_at (loc,
9875 ("both %<_Sat%> and %<int%> in "
9876 "declaration specifiers"));
9877 else if (specs->typespec_word == cts_float)
9878 error_at (loc,
9879 ("both %<_Sat%> and %<float%> in "
9880 "declaration specifiers"));
9881 else if (specs->typespec_word == cts_double)
9882 error_at (loc,
9883 ("both %<_Sat%> and %<double%> in "
9884 "declaration specifiers"));
9885 else if (specs->typespec_word == cts_dfloat32)
9886 error_at (loc,
9887 ("both %<_Sat%> and %<_Decimal32%> in "
9888 "declaration specifiers"));
9889 else if (specs->typespec_word == cts_dfloat64)
9890 error_at (loc,
9891 ("both %<_Sat%> and %<_Decimal64%> in "
9892 "declaration specifiers"));
9893 else if (specs->typespec_word == cts_dfloat128)
9894 error_at (loc,
9895 ("both %<_Sat%> and %<_Decimal128%> in "
9896 "declaration specifiers"));
9897 else if (specs->complex_p)
9898 error_at (loc,
9899 ("both %<_Sat%> and %<complex%> in "
9900 "declaration specifiers"));
9901 else
9903 specs->saturating_p = true;
9904 specs->locations[cdw_saturating] = loc;
9906 break;
9907 default:
9908 gcc_unreachable ();
9911 if (dupe)
9912 error_at (loc, "duplicate %qE", type);
9914 return specs;
9916 else
9918 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9919 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
9920 "__auto_type". */
9921 if (specs->typespec_word != cts_none)
9923 error_at (loc,
9924 "two or more data types in declaration specifiers");
9925 return specs;
9927 switch (i)
9929 case RID_AUTO_TYPE:
9930 if (specs->long_p)
9931 error_at (loc,
9932 ("both %<long%> and %<__auto_type%> in "
9933 "declaration specifiers"));
9934 else if (specs->short_p)
9935 error_at (loc,
9936 ("both %<short%> and %<__auto_type%> in "
9937 "declaration specifiers"));
9938 else if (specs->signed_p)
9939 error_at (loc,
9940 ("both %<signed%> and %<__auto_type%> in "
9941 "declaration specifiers"));
9942 else if (specs->unsigned_p)
9943 error_at (loc,
9944 ("both %<unsigned%> and %<__auto_type%> in "
9945 "declaration specifiers"));
9946 else if (specs->complex_p)
9947 error_at (loc,
9948 ("both %<complex%> and %<__auto_type%> in "
9949 "declaration specifiers"));
9950 else if (specs->saturating_p)
9951 error_at (loc,
9952 ("both %<_Sat%> and %<__auto_type%> in "
9953 "declaration specifiers"));
9954 else
9956 specs->typespec_word = cts_auto_type;
9957 specs->locations[cdw_typespec] = loc;
9959 return specs;
9960 case RID_INT_N_0:
9961 case RID_INT_N_1:
9962 case RID_INT_N_2:
9963 case RID_INT_N_3:
9964 specs->int_n_idx = i - RID_INT_N_0;
9965 if (!in_system_header_at (input_location))
9966 pedwarn (loc, OPT_Wpedantic,
9967 "ISO C does not support %<__int%d%> types",
9968 int_n_data[specs->int_n_idx].bitsize);
9970 if (specs->long_p)
9971 error_at (loc,
9972 ("both %<__int%d%> and %<long%> in "
9973 "declaration specifiers"),
9974 int_n_data[specs->int_n_idx].bitsize);
9975 else if (specs->saturating_p)
9976 error_at (loc,
9977 ("both %<_Sat%> and %<__int%d%> in "
9978 "declaration specifiers"),
9979 int_n_data[specs->int_n_idx].bitsize);
9980 else if (specs->short_p)
9981 error_at (loc,
9982 ("both %<__int%d%> and %<short%> in "
9983 "declaration specifiers"),
9984 int_n_data[specs->int_n_idx].bitsize);
9985 else if (! int_n_enabled_p [specs->int_n_idx])
9986 error_at (loc,
9987 "%<__int%d%> is not supported on this target",
9988 int_n_data[specs->int_n_idx].bitsize);
9989 else
9991 specs->typespec_word = cts_int_n;
9992 specs->locations[cdw_typespec] = loc;
9994 return specs;
9995 case RID_VOID:
9996 if (specs->long_p)
9997 error_at (loc,
9998 ("both %<long%> and %<void%> in "
9999 "declaration specifiers"));
10000 else if (specs->short_p)
10001 error_at (loc,
10002 ("both %<short%> and %<void%> in "
10003 "declaration specifiers"));
10004 else if (specs->signed_p)
10005 error_at (loc,
10006 ("both %<signed%> and %<void%> in "
10007 "declaration specifiers"));
10008 else if (specs->unsigned_p)
10009 error_at (loc,
10010 ("both %<unsigned%> and %<void%> in "
10011 "declaration specifiers"));
10012 else if (specs->complex_p)
10013 error_at (loc,
10014 ("both %<complex%> and %<void%> in "
10015 "declaration specifiers"));
10016 else if (specs->saturating_p)
10017 error_at (loc,
10018 ("both %<_Sat%> and %<void%> in "
10019 "declaration specifiers"));
10020 else
10022 specs->typespec_word = cts_void;
10023 specs->locations[cdw_typespec] = loc;
10025 return specs;
10026 case RID_BOOL:
10027 if (!in_system_header_at (loc))
10028 pedwarn_c90 (loc, OPT_Wpedantic,
10029 "ISO C90 does not support boolean types");
10030 if (specs->long_p)
10031 error_at (loc,
10032 ("both %<long%> and %<_Bool%> in "
10033 "declaration specifiers"));
10034 else if (specs->short_p)
10035 error_at (loc,
10036 ("both %<short%> and %<_Bool%> in "
10037 "declaration specifiers"));
10038 else if (specs->signed_p)
10039 error_at (loc,
10040 ("both %<signed%> and %<_Bool%> in "
10041 "declaration specifiers"));
10042 else if (specs->unsigned_p)
10043 error_at (loc,
10044 ("both %<unsigned%> and %<_Bool%> in "
10045 "declaration specifiers"));
10046 else if (specs->complex_p)
10047 error_at (loc,
10048 ("both %<complex%> and %<_Bool%> in "
10049 "declaration specifiers"));
10050 else if (specs->saturating_p)
10051 error_at (loc,
10052 ("both %<_Sat%> and %<_Bool%> in "
10053 "declaration specifiers"));
10054 else
10056 specs->typespec_word = cts_bool;
10057 specs->locations[cdw_typespec] = loc;
10059 return specs;
10060 case RID_CHAR:
10061 if (specs->long_p)
10062 error_at (loc,
10063 ("both %<long%> and %<char%> in "
10064 "declaration specifiers"));
10065 else if (specs->short_p)
10066 error_at (loc,
10067 ("both %<short%> and %<char%> in "
10068 "declaration specifiers"));
10069 else if (specs->saturating_p)
10070 error_at (loc,
10071 ("both %<_Sat%> and %<char%> in "
10072 "declaration specifiers"));
10073 else
10075 specs->typespec_word = cts_char;
10076 specs->locations[cdw_typespec] = loc;
10078 return specs;
10079 case RID_INT:
10080 if (specs->saturating_p)
10081 error_at (loc,
10082 ("both %<_Sat%> and %<int%> in "
10083 "declaration specifiers"));
10084 else
10086 specs->typespec_word = cts_int;
10087 specs->locations[cdw_typespec] = loc;
10089 return specs;
10090 case RID_FLOAT:
10091 if (specs->long_p)
10092 error_at (loc,
10093 ("both %<long%> and %<float%> in "
10094 "declaration specifiers"));
10095 else if (specs->short_p)
10096 error_at (loc,
10097 ("both %<short%> and %<float%> in "
10098 "declaration specifiers"));
10099 else if (specs->signed_p)
10100 error_at (loc,
10101 ("both %<signed%> and %<float%> in "
10102 "declaration specifiers"));
10103 else if (specs->unsigned_p)
10104 error_at (loc,
10105 ("both %<unsigned%> and %<float%> in "
10106 "declaration specifiers"));
10107 else if (specs->saturating_p)
10108 error_at (loc,
10109 ("both %<_Sat%> and %<float%> in "
10110 "declaration specifiers"));
10111 else
10113 specs->typespec_word = cts_float;
10114 specs->locations[cdw_typespec] = loc;
10116 return specs;
10117 case RID_DOUBLE:
10118 if (specs->long_long_p)
10119 error_at (loc,
10120 ("both %<long long%> and %<double%> in "
10121 "declaration specifiers"));
10122 else if (specs->short_p)
10123 error_at (loc,
10124 ("both %<short%> and %<double%> in "
10125 "declaration specifiers"));
10126 else if (specs->signed_p)
10127 error_at (loc,
10128 ("both %<signed%> and %<double%> in "
10129 "declaration specifiers"));
10130 else if (specs->unsigned_p)
10131 error_at (loc,
10132 ("both %<unsigned%> and %<double%> in "
10133 "declaration specifiers"));
10134 else if (specs->saturating_p)
10135 error_at (loc,
10136 ("both %<_Sat%> and %<double%> in "
10137 "declaration specifiers"));
10138 else
10140 specs->typespec_word = cts_double;
10141 specs->locations[cdw_typespec] = loc;
10143 return specs;
10144 case RID_DFLOAT32:
10145 case RID_DFLOAT64:
10146 case RID_DFLOAT128:
10148 const char *str;
10149 if (i == RID_DFLOAT32)
10150 str = "_Decimal32";
10151 else if (i == RID_DFLOAT64)
10152 str = "_Decimal64";
10153 else
10154 str = "_Decimal128";
10155 if (specs->long_long_p)
10156 error_at (loc,
10157 ("both %<long long%> and %<%s%> in "
10158 "declaration specifiers"),
10159 str);
10160 if (specs->long_p)
10161 error_at (loc,
10162 ("both %<long%> and %<%s%> in "
10163 "declaration specifiers"),
10164 str);
10165 else if (specs->short_p)
10166 error_at (loc,
10167 ("both %<short%> and %<%s%> in "
10168 "declaration specifiers"),
10169 str);
10170 else if (specs->signed_p)
10171 error_at (loc,
10172 ("both %<signed%> and %<%s%> in "
10173 "declaration specifiers"),
10174 str);
10175 else if (specs->unsigned_p)
10176 error_at (loc,
10177 ("both %<unsigned%> and %<%s%> in "
10178 "declaration specifiers"),
10179 str);
10180 else if (specs->complex_p)
10181 error_at (loc,
10182 ("both %<complex%> and %<%s%> in "
10183 "declaration specifiers"),
10184 str);
10185 else if (specs->saturating_p)
10186 error_at (loc,
10187 ("both %<_Sat%> and %<%s%> in "
10188 "declaration specifiers"),
10189 str);
10190 else if (i == RID_DFLOAT32)
10191 specs->typespec_word = cts_dfloat32;
10192 else if (i == RID_DFLOAT64)
10193 specs->typespec_word = cts_dfloat64;
10194 else
10195 specs->typespec_word = cts_dfloat128;
10196 specs->locations[cdw_typespec] = loc;
10198 if (!targetm.decimal_float_supported_p ())
10199 error_at (loc,
10200 ("decimal floating point not supported "
10201 "for this target"));
10202 pedwarn (loc, OPT_Wpedantic,
10203 "ISO C does not support decimal floating point");
10204 return specs;
10205 case RID_FRACT:
10206 case RID_ACCUM:
10208 const char *str;
10209 if (i == RID_FRACT)
10210 str = "_Fract";
10211 else
10212 str = "_Accum";
10213 if (specs->complex_p)
10214 error_at (loc,
10215 ("both %<complex%> and %<%s%> in "
10216 "declaration specifiers"),
10217 str);
10218 else if (i == RID_FRACT)
10219 specs->typespec_word = cts_fract;
10220 else
10221 specs->typespec_word = cts_accum;
10222 specs->locations[cdw_typespec] = loc;
10224 if (!targetm.fixed_point_supported_p ())
10225 error_at (loc,
10226 "fixed-point types not supported for this target");
10227 pedwarn (loc, OPT_Wpedantic,
10228 "ISO C does not support fixed-point types");
10229 return specs;
10230 default:
10231 /* ObjC reserved word "id", handled below. */
10232 break;
10237 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10238 form of ObjC type, cases such as "int" and "long" being handled
10239 above), a TYPE (struct, union, enum and typeof specifiers) or an
10240 ERROR_MARK. In none of these cases may there have previously
10241 been any type specifiers. */
10242 if (specs->type || specs->typespec_word != cts_none
10243 || specs->long_p || specs->short_p || specs->signed_p
10244 || specs->unsigned_p || specs->complex_p)
10245 error_at (loc, "two or more data types in declaration specifiers");
10246 else if (TREE_CODE (type) == TYPE_DECL)
10248 if (TREE_TYPE (type) == error_mark_node)
10249 ; /* Allow the type to default to int to avoid cascading errors. */
10250 else
10252 specs->type = TREE_TYPE (type);
10253 specs->decl_attr = DECL_ATTRIBUTES (type);
10254 specs->typedef_p = true;
10255 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10256 specs->locations[cdw_typedef] = loc;
10258 /* If this typedef name is defined in a struct, then a C++
10259 lookup would return a different value. */
10260 if (warn_cxx_compat
10261 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10262 warning_at (loc, OPT_Wc___compat,
10263 "C++ lookup of %qD would return a field, not a type",
10264 type);
10266 /* If we are parsing a struct, record that a struct field
10267 used a typedef. */
10268 if (warn_cxx_compat && struct_parse_info != NULL)
10269 struct_parse_info->typedefs_seen.safe_push (type);
10272 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10274 tree t = lookup_name (type);
10275 if (!t || TREE_CODE (t) != TYPE_DECL)
10276 error_at (loc, "%qE fails to be a typedef or built in type", type);
10277 else if (TREE_TYPE (t) == error_mark_node)
10279 else
10281 specs->type = TREE_TYPE (t);
10282 specs->locations[cdw_typespec] = loc;
10285 else
10287 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10289 specs->typedef_p = true;
10290 specs->locations[cdw_typedef] = loc;
10291 if (spec.expr)
10293 if (specs->expr)
10294 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10295 specs->expr, spec.expr);
10296 else
10297 specs->expr = spec.expr;
10298 specs->expr_const_operands &= spec.expr_const_operands;
10301 specs->type = type;
10304 return specs;
10307 /* Add the storage class specifier or function specifier SCSPEC to the
10308 declaration specifiers SPECS, returning SPECS. */
10310 struct c_declspecs *
10311 declspecs_add_scspec (source_location loc,
10312 struct c_declspecs *specs,
10313 tree scspec)
10315 enum rid i;
10316 enum c_storage_class n = csc_none;
10317 bool dupe = false;
10318 specs->declspecs_seen_p = true;
10319 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10320 && C_IS_RESERVED_WORD (scspec));
10321 i = C_RID_CODE (scspec);
10322 if (specs->non_sc_seen_p)
10323 warning (OPT_Wold_style_declaration,
10324 "%qE is not at beginning of declaration", scspec);
10325 switch (i)
10327 case RID_INLINE:
10328 /* C99 permits duplicate inline. Although of doubtful utility,
10329 it seems simplest to permit it in gnu89 mode as well, as
10330 there is also little utility in maintaining this as a
10331 difference between gnu89 and C99 inline. */
10332 dupe = false;
10333 specs->inline_p = true;
10334 specs->locations[cdw_inline] = loc;
10335 break;
10336 case RID_NORETURN:
10337 /* Duplicate _Noreturn is permitted. */
10338 dupe = false;
10339 specs->noreturn_p = true;
10340 specs->locations[cdw_noreturn] = loc;
10341 break;
10342 case RID_THREAD:
10343 dupe = specs->thread_p;
10344 if (specs->storage_class == csc_auto)
10345 error ("%qE used with %<auto%>", scspec);
10346 else if (specs->storage_class == csc_register)
10347 error ("%qE used with %<register%>", scspec);
10348 else if (specs->storage_class == csc_typedef)
10349 error ("%qE used with %<typedef%>", scspec);
10350 else
10352 specs->thread_p = true;
10353 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10354 "__thread") == 0);
10355 /* A diagnostic is not required for the use of this
10356 identifier in the implementation namespace; only diagnose
10357 it for the C11 spelling because of existing code using
10358 the other spelling. */
10359 if (!specs->thread_gnu_p)
10361 if (flag_isoc99)
10362 pedwarn_c99 (loc, OPT_Wpedantic,
10363 "ISO C99 does not support %qE", scspec);
10364 else
10365 pedwarn_c99 (loc, OPT_Wpedantic,
10366 "ISO C90 does not support %qE", scspec);
10368 specs->locations[cdw_thread] = loc;
10370 break;
10371 case RID_AUTO:
10372 n = csc_auto;
10373 break;
10374 case RID_EXTERN:
10375 n = csc_extern;
10376 /* Diagnose "__thread extern". */
10377 if (specs->thread_p && specs->thread_gnu_p)
10378 error ("%<__thread%> before %<extern%>");
10379 break;
10380 case RID_REGISTER:
10381 n = csc_register;
10382 break;
10383 case RID_STATIC:
10384 n = csc_static;
10385 /* Diagnose "__thread static". */
10386 if (specs->thread_p && specs->thread_gnu_p)
10387 error ("%<__thread%> before %<static%>");
10388 break;
10389 case RID_TYPEDEF:
10390 n = csc_typedef;
10391 break;
10392 default:
10393 gcc_unreachable ();
10395 if (n != csc_none && n == specs->storage_class)
10396 dupe = true;
10397 if (dupe)
10399 if (i == RID_THREAD)
10400 error ("duplicate %<_Thread_local%> or %<__thread%>");
10401 else
10402 error ("duplicate %qE", scspec);
10404 if (n != csc_none)
10406 if (specs->storage_class != csc_none && n != specs->storage_class)
10408 error ("multiple storage classes in declaration specifiers");
10410 else
10412 specs->storage_class = n;
10413 specs->locations[cdw_storage_class] = loc;
10414 if (n != csc_extern && n != csc_static && specs->thread_p)
10416 error ("%qs used with %qE",
10417 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10418 scspec);
10419 specs->thread_p = false;
10423 return specs;
10426 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10427 returning SPECS. */
10429 struct c_declspecs *
10430 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10432 specs->attrs = chainon (attrs, specs->attrs);
10433 specs->locations[cdw_attributes] = loc;
10434 specs->declspecs_seen_p = true;
10435 return specs;
10438 /* Add an _Alignas specifier (expression ALIGN, or type whose
10439 alignment is ALIGN) to the declaration specifiers SPECS, returning
10440 SPECS. */
10441 struct c_declspecs *
10442 declspecs_add_alignas (source_location loc,
10443 struct c_declspecs *specs, tree align)
10445 int align_log;
10446 specs->alignas_p = true;
10447 specs->locations[cdw_alignas] = loc;
10448 if (align == error_mark_node)
10449 return specs;
10450 align_log = check_user_alignment (align, true);
10451 if (align_log > specs->align_log)
10452 specs->align_log = align_log;
10453 return specs;
10456 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10457 specifiers with any other type specifier to determine the resulting
10458 type. This is where ISO C checks on complex types are made, since
10459 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10460 double". */
10462 struct c_declspecs *
10463 finish_declspecs (struct c_declspecs *specs)
10465 /* If a type was specified as a whole, we have no modifiers and are
10466 done. */
10467 if (specs->type != NULL_TREE)
10469 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10470 && !specs->signed_p && !specs->unsigned_p
10471 && !specs->complex_p);
10473 /* Set a dummy type. */
10474 if (TREE_CODE (specs->type) == ERROR_MARK)
10475 specs->type = integer_type_node;
10476 return specs;
10479 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10480 has been specified, treat it as "int" unless "_Complex" is
10481 present and there are no other specifiers. If we just have
10482 "_Complex", it is equivalent to "_Complex double", but e.g.
10483 "_Complex short" is equivalent to "_Complex short int". */
10484 if (specs->typespec_word == cts_none)
10486 if (specs->saturating_p)
10488 error_at (specs->locations[cdw_saturating],
10489 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10490 if (!targetm.fixed_point_supported_p ())
10491 error_at (specs->locations[cdw_saturating],
10492 "fixed-point types not supported for this target");
10493 specs->typespec_word = cts_fract;
10495 else if (specs->long_p || specs->short_p
10496 || specs->signed_p || specs->unsigned_p)
10498 specs->typespec_word = cts_int;
10500 else if (specs->complex_p)
10502 specs->typespec_word = cts_double;
10503 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10504 "ISO C does not support plain %<complex%> meaning "
10505 "%<double complex%>");
10507 else
10509 specs->typespec_word = cts_int;
10510 specs->default_int_p = true;
10511 /* We don't diagnose this here because grokdeclarator will
10512 give more specific diagnostics according to whether it is
10513 a function definition. */
10517 /* If "signed" was specified, record this to distinguish "int" and
10518 "signed int" in the case of a bit-field with
10519 -funsigned-bitfields. */
10520 specs->explicit_signed_p = specs->signed_p;
10522 /* Now compute the actual type. */
10523 switch (specs->typespec_word)
10525 case cts_auto_type:
10526 gcc_assert (!specs->long_p && !specs->short_p
10527 && !specs->signed_p && !specs->unsigned_p
10528 && !specs->complex_p);
10529 /* Type to be filled in later. */
10530 break;
10531 case cts_void:
10532 gcc_assert (!specs->long_p && !specs->short_p
10533 && !specs->signed_p && !specs->unsigned_p
10534 && !specs->complex_p);
10535 specs->type = void_type_node;
10536 break;
10537 case cts_bool:
10538 gcc_assert (!specs->long_p && !specs->short_p
10539 && !specs->signed_p && !specs->unsigned_p
10540 && !specs->complex_p);
10541 specs->type = boolean_type_node;
10542 break;
10543 case cts_char:
10544 gcc_assert (!specs->long_p && !specs->short_p);
10545 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10546 if (specs->signed_p)
10547 specs->type = signed_char_type_node;
10548 else if (specs->unsigned_p)
10549 specs->type = unsigned_char_type_node;
10550 else
10551 specs->type = char_type_node;
10552 if (specs->complex_p)
10554 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10555 "ISO C does not support complex integer types");
10556 specs->type = build_complex_type (specs->type);
10558 break;
10559 case cts_int_n:
10560 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10561 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10562 specs->type = (specs->unsigned_p
10563 ? int_n_trees[specs->int_n_idx].unsigned_type
10564 : int_n_trees[specs->int_n_idx].signed_type);
10565 if (specs->complex_p)
10567 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10568 "ISO C does not support complex integer types");
10569 specs->type = build_complex_type (specs->type);
10571 break;
10572 case cts_int:
10573 gcc_assert (!(specs->long_p && specs->short_p));
10574 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10575 if (specs->long_long_p)
10576 specs->type = (specs->unsigned_p
10577 ? long_long_unsigned_type_node
10578 : long_long_integer_type_node);
10579 else if (specs->long_p)
10580 specs->type = (specs->unsigned_p
10581 ? long_unsigned_type_node
10582 : long_integer_type_node);
10583 else if (specs->short_p)
10584 specs->type = (specs->unsigned_p
10585 ? short_unsigned_type_node
10586 : short_integer_type_node);
10587 else
10588 specs->type = (specs->unsigned_p
10589 ? unsigned_type_node
10590 : integer_type_node);
10591 if (specs->complex_p)
10593 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10594 "ISO C does not support complex integer types");
10595 specs->type = build_complex_type (specs->type);
10597 break;
10598 case cts_float:
10599 gcc_assert (!specs->long_p && !specs->short_p
10600 && !specs->signed_p && !specs->unsigned_p);
10601 specs->type = (specs->complex_p
10602 ? complex_float_type_node
10603 : float_type_node);
10604 break;
10605 case cts_double:
10606 gcc_assert (!specs->long_long_p && !specs->short_p
10607 && !specs->signed_p && !specs->unsigned_p);
10608 if (specs->long_p)
10610 specs->type = (specs->complex_p
10611 ? complex_long_double_type_node
10612 : long_double_type_node);
10614 else
10616 specs->type = (specs->complex_p
10617 ? complex_double_type_node
10618 : double_type_node);
10620 break;
10621 case cts_dfloat32:
10622 case cts_dfloat64:
10623 case cts_dfloat128:
10624 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10625 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10626 if (specs->typespec_word == cts_dfloat32)
10627 specs->type = dfloat32_type_node;
10628 else if (specs->typespec_word == cts_dfloat64)
10629 specs->type = dfloat64_type_node;
10630 else
10631 specs->type = dfloat128_type_node;
10632 break;
10633 case cts_fract:
10634 gcc_assert (!specs->complex_p);
10635 if (!targetm.fixed_point_supported_p ())
10636 specs->type = integer_type_node;
10637 else if (specs->saturating_p)
10639 if (specs->long_long_p)
10640 specs->type = specs->unsigned_p
10641 ? sat_unsigned_long_long_fract_type_node
10642 : sat_long_long_fract_type_node;
10643 else if (specs->long_p)
10644 specs->type = specs->unsigned_p
10645 ? sat_unsigned_long_fract_type_node
10646 : sat_long_fract_type_node;
10647 else if (specs->short_p)
10648 specs->type = specs->unsigned_p
10649 ? sat_unsigned_short_fract_type_node
10650 : sat_short_fract_type_node;
10651 else
10652 specs->type = specs->unsigned_p
10653 ? sat_unsigned_fract_type_node
10654 : sat_fract_type_node;
10656 else
10658 if (specs->long_long_p)
10659 specs->type = specs->unsigned_p
10660 ? unsigned_long_long_fract_type_node
10661 : long_long_fract_type_node;
10662 else if (specs->long_p)
10663 specs->type = specs->unsigned_p
10664 ? unsigned_long_fract_type_node
10665 : long_fract_type_node;
10666 else if (specs->short_p)
10667 specs->type = specs->unsigned_p
10668 ? unsigned_short_fract_type_node
10669 : short_fract_type_node;
10670 else
10671 specs->type = specs->unsigned_p
10672 ? unsigned_fract_type_node
10673 : fract_type_node;
10675 break;
10676 case cts_accum:
10677 gcc_assert (!specs->complex_p);
10678 if (!targetm.fixed_point_supported_p ())
10679 specs->type = integer_type_node;
10680 else if (specs->saturating_p)
10682 if (specs->long_long_p)
10683 specs->type = specs->unsigned_p
10684 ? sat_unsigned_long_long_accum_type_node
10685 : sat_long_long_accum_type_node;
10686 else if (specs->long_p)
10687 specs->type = specs->unsigned_p
10688 ? sat_unsigned_long_accum_type_node
10689 : sat_long_accum_type_node;
10690 else if (specs->short_p)
10691 specs->type = specs->unsigned_p
10692 ? sat_unsigned_short_accum_type_node
10693 : sat_short_accum_type_node;
10694 else
10695 specs->type = specs->unsigned_p
10696 ? sat_unsigned_accum_type_node
10697 : sat_accum_type_node;
10699 else
10701 if (specs->long_long_p)
10702 specs->type = specs->unsigned_p
10703 ? unsigned_long_long_accum_type_node
10704 : long_long_accum_type_node;
10705 else if (specs->long_p)
10706 specs->type = specs->unsigned_p
10707 ? unsigned_long_accum_type_node
10708 : long_accum_type_node;
10709 else if (specs->short_p)
10710 specs->type = specs->unsigned_p
10711 ? unsigned_short_accum_type_node
10712 : short_accum_type_node;
10713 else
10714 specs->type = specs->unsigned_p
10715 ? unsigned_accum_type_node
10716 : accum_type_node;
10718 break;
10719 default:
10720 gcc_unreachable ();
10723 return specs;
10726 /* Perform final processing on one file scope's declarations (or the
10727 external scope's declarations), GLOBALS. */
10729 static void
10730 c_write_global_declarations_1 (tree globals)
10732 tree decl;
10733 bool reconsider;
10735 /* Process the decls in the order they were written. */
10736 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10738 /* Check for used but undefined static functions using the C
10739 standard's definition of "used", and set TREE_NO_WARNING so
10740 that check_global_declaration doesn't repeat the check. */
10741 if (TREE_CODE (decl) == FUNCTION_DECL
10742 && DECL_INITIAL (decl) == 0
10743 && DECL_EXTERNAL (decl)
10744 && !TREE_PUBLIC (decl)
10745 && C_DECL_USED (decl))
10747 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10748 TREE_NO_WARNING (decl) = 1;
10751 wrapup_global_declaration_1 (decl);
10756 reconsider = false;
10757 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10758 reconsider |= wrapup_global_declaration_2 (decl);
10760 while (reconsider);
10763 /* Callback to collect a source_ref from a DECL. */
10765 static void
10766 collect_source_ref_cb (tree decl)
10768 if (!DECL_IS_BUILTIN (decl))
10769 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10772 /* Preserve the external declarations scope across a garbage collect. */
10773 static GTY(()) tree ext_block;
10775 /* Collect all references relevant to SOURCE_FILE. */
10777 static void
10778 collect_all_refs (const char *source_file)
10780 tree t;
10781 unsigned i;
10783 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10784 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10786 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10789 /* Iterate over all global declarations and call CALLBACK. */
10791 static void
10792 for_each_global_decl (void (*callback) (tree decl))
10794 tree t;
10795 tree decls;
10796 tree decl;
10797 unsigned i;
10799 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10801 decls = DECL_INITIAL (t);
10802 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10803 callback (decl);
10806 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10807 callback (decl);
10810 /* Perform any final parser cleanups and generate initial debugging
10811 information. */
10813 void
10814 c_parse_final_cleanups (void)
10816 tree t;
10817 unsigned i;
10819 /* We don't want to do this if generating a PCH. */
10820 if (pch_file)
10821 return;
10823 timevar_stop (TV_PHASE_PARSING);
10824 timevar_start (TV_PHASE_DEFERRED);
10826 /* Do the Objective-C stuff. This is where all the Objective-C
10827 module stuff gets generated (symtab, class/protocol/selector
10828 lists etc). */
10829 if (c_dialect_objc ())
10830 objc_write_global_declarations ();
10832 /* Close the external scope. */
10833 ext_block = pop_scope ();
10834 external_scope = 0;
10835 gcc_assert (!current_scope);
10837 /* Handle -fdump-ada-spec[-slim]. */
10838 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10840 /* Build a table of files to generate specs for */
10841 if (flag_dump_ada_spec_slim)
10842 collect_source_ref (main_input_filename);
10843 else
10844 for_each_global_decl (collect_source_ref_cb);
10846 dump_ada_specs (collect_all_refs, NULL);
10849 if (ext_block)
10851 tree tmp = BLOCK_VARS (ext_block);
10852 int flags;
10853 FILE * stream = dump_begin (TDI_tu, &flags);
10854 if (stream && tmp)
10856 dump_node (tmp, flags & ~TDF_SLIM, stream);
10857 dump_end (TDI_tu, stream);
10861 /* Process all file scopes in this compilation, and the external_scope,
10862 through wrapup_global_declarations. */
10863 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10864 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10865 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10867 timevar_stop (TV_PHASE_DEFERRED);
10868 timevar_start (TV_PHASE_PARSING);
10870 ext_block = NULL;
10873 /* Register reserved keyword WORD as qualifier for address space AS. */
10875 void
10876 c_register_addr_space (const char *word, addr_space_t as)
10878 int rid = RID_FIRST_ADDR_SPACE + as;
10879 tree id;
10881 /* Address space qualifiers are only supported
10882 in C with GNU extensions enabled. */
10883 if (c_dialect_objc () || flag_no_asm)
10884 return;
10886 id = get_identifier (word);
10887 C_SET_RID_CODE (id, rid);
10888 C_IS_RESERVED_WORD (id) = 1;
10889 ridpointers [rid] = id;
10892 /* Return identifier to look up for omp declare reduction. */
10894 tree
10895 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
10897 const char *p = NULL;
10898 switch (reduction_code)
10900 case PLUS_EXPR: p = "+"; break;
10901 case MULT_EXPR: p = "*"; break;
10902 case MINUS_EXPR: p = "-"; break;
10903 case BIT_AND_EXPR: p = "&"; break;
10904 case BIT_XOR_EXPR: p = "^"; break;
10905 case BIT_IOR_EXPR: p = "|"; break;
10906 case TRUTH_ANDIF_EXPR: p = "&&"; break;
10907 case TRUTH_ORIF_EXPR: p = "||"; break;
10908 case MIN_EXPR: p = "min"; break;
10909 case MAX_EXPR: p = "max"; break;
10910 default:
10911 break;
10914 if (p == NULL)
10916 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
10917 return error_mark_node;
10918 p = IDENTIFIER_POINTER (reduction_id);
10921 const char prefix[] = "omp declare reduction ";
10922 size_t lenp = sizeof (prefix);
10923 size_t len = strlen (p);
10924 char *name = XALLOCAVEC (char, lenp + len);
10925 memcpy (name, prefix, lenp - 1);
10926 memcpy (name + lenp - 1, p, len + 1);
10927 return get_identifier (name);
10930 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10931 VAR_DECL, bind it into the current scope and return it. */
10933 tree
10934 c_omp_reduction_decl (tree reduction_id)
10936 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10937 if (b != NULL && B_IN_CURRENT_SCOPE (b))
10938 return b->decl;
10940 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
10941 reduction_id, integer_type_node);
10942 DECL_ARTIFICIAL (decl) = 1;
10943 DECL_EXTERNAL (decl) = 1;
10944 TREE_STATIC (decl) = 1;
10945 TREE_PUBLIC (decl) = 0;
10946 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
10947 return decl;
10950 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10952 tree
10953 c_omp_reduction_lookup (tree reduction_id, tree type)
10955 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10956 while (b)
10958 tree t;
10959 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
10960 if (comptypes (TREE_PURPOSE (t), type))
10961 return TREE_VALUE (t);
10962 b = b->shadowed;
10964 return error_mark_node;
10967 /* Helper function called via walk_tree, to diagnose invalid
10968 #pragma omp declare reduction combiners or initializers. */
10970 tree
10971 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
10973 tree *vars = (tree *) data;
10974 if (SSA_VAR_P (*tp)
10975 && !DECL_ARTIFICIAL (*tp)
10976 && *tp != vars[0]
10977 && *tp != vars[1])
10979 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
10980 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
10981 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
10982 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10983 *tp);
10984 else
10985 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
10986 "to variable %qD which is not %<omp_priv%> nor "
10987 "%<omp_orig%>",
10988 *tp);
10989 return *tp;
10991 return NULL_TREE;
10994 #include "gt-c-c-decl.h"