PR middle-end/66633
[official-gcc.git] / gcc / c / c-decl.c
blob383f0a766bf9f80e85284b70eecae35c6c7a4229
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2015 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 "tm.h"
31 #include "intl.h"
32 #include "symtab.h"
33 #include "alias.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "print-tree.h"
37 #include "stor-layout.h"
38 #include "varasm.h"
39 #include "attribs.h"
40 #include "stringpool.h"
41 #include "tree-inline.h"
42 #include "flags.h"
43 #include "hard-reg-set.h"
44 #include "function.h"
45 #include "c-tree.h"
46 #include "toplev.h"
47 #include "tm_p.h"
48 #include "cpplib.h"
49 #include "target.h"
50 #include "debug.h"
51 #include "opts.h"
52 #include "timevar.h"
53 #include "c-family/c-common.h"
54 #include "c-family/c-objc.h"
55 #include "c-family/c-pragma.h"
56 #include "c-family/c-ubsan.h"
57 #include "c-lang.h"
58 #include "langhooks.h"
59 #include "tree-iterator.h"
60 #include "diagnostic-core.h"
61 #include "dumpfile.h"
62 #include "cgraph.h"
63 #include "langhooks-def.h"
64 #include "plugin.h"
65 #include "c-family/c-ada-spec.h"
66 #include "cilk.h"
67 #include "builtins.h"
69 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
70 enum decl_context
71 { NORMAL, /* Ordinary declaration */
72 FUNCDEF, /* Function definition */
73 PARM, /* Declaration of parm before function body */
74 FIELD, /* Declaration inside struct or union */
75 TYPENAME}; /* Typename (inside cast or sizeof) */
77 /* States indicating how grokdeclarator() should handle declspecs marked
78 with __attribute__((deprecated)). An object declared as
79 __attribute__((deprecated)) suppresses warnings of uses of other
80 deprecated items. */
82 enum deprecated_states {
83 DEPRECATED_NORMAL,
84 DEPRECATED_SUPPRESS
88 /* Nonzero if we have seen an invalid cross reference
89 to a struct, union, or enum, but not yet printed the message. */
90 tree pending_invalid_xref;
92 /* File and line to appear in the eventual error message. */
93 location_t pending_invalid_xref_location;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types;
109 /* The argument information structure for the function currently being
110 defined. */
112 static struct c_arg_info *current_function_arg_info;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
127 /* A list of decls to be made automatically visible in each file scope. */
128 static GTY(()) tree visible_builtins;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a return statement that specifies a return value is seen. */
133 int current_function_returns_value;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a return statement with no argument is seen. */
138 int current_function_returns_null;
140 /* Set to 0 at beginning of a function definition, set to 1 if
141 a call to a noreturn function is seen. */
143 int current_function_returns_abnormally;
145 /* Set to nonzero by `grokdeclarator' for a function
146 whose return type is defaulted, if warnings for this are desired. */
148 static int warn_about_return_type;
150 /* Nonzero when the current toplevel function contains a declaration
151 of a nested function which is never defined. */
153 static bool undef_nested_function;
155 /* If non-zero, implicit "omp declare target" attribute is added into the
156 attribute lists. */
157 int current_omp_declare_target_attribute;
159 /* Each c_binding structure describes one binding of an identifier to
160 a decl. All the decls in a scope - irrespective of namespace - are
161 chained together by the ->prev field, which (as the name implies)
162 runs in reverse order. All the decls in a given namespace bound to
163 a given identifier are chained by the ->shadowed field, which runs
164 from inner to outer scopes.
166 The ->decl field usually points to a DECL node, but there are two
167 exceptions. In the namespace of type tags, the bound entity is a
168 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
169 identifier is encountered, it is bound to error_mark_node to
170 suppress further errors about that identifier in the current
171 function.
173 The ->u.type field stores the type of the declaration in this scope;
174 if NULL, the type is the type of the ->decl field. This is only of
175 relevance for objects with external or internal linkage which may
176 be redeclared in inner scopes, forming composite types that only
177 persist for the duration of those scopes. In the external scope,
178 this stores the composite of all the types declared for this
179 object, visible or not. The ->inner_comp field (used only at file
180 scope) stores whether an incomplete array type at file scope was
181 completed at an inner scope to an array size other than 1.
183 The ->u.label field is used for labels. It points to a structure
184 which stores additional information used for warnings.
186 The depth field is copied from the scope structure that holds this
187 decl. It is used to preserve the proper ordering of the ->shadowed
188 field (see bind()) and also for a handful of special-case checks.
189 Finally, the invisible bit is true for a decl which should be
190 ignored for purposes of normal name lookup, and the nested bit is
191 true for a decl that's been bound a second time in an inner scope;
192 in all such cases, the binding in the outer scope will have its
193 invisible bit true. */
195 struct GTY((chain_next ("%h.prev"))) c_binding {
196 union GTY(()) { /* first so GTY desc can use decl */
197 tree GTY((tag ("0"))) type; /* the type in this scope */
198 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
199 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
200 tree decl; /* the decl bound */
201 tree id; /* the identifier it's bound to */
202 struct c_binding *prev; /* the previous decl in this scope */
203 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
204 unsigned int depth : 28; /* depth of this scope */
205 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
206 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
207 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
208 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
209 location_t locus; /* location for nested bindings */
211 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
212 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
213 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
214 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
216 /* Each C symbol points to three linked lists of c_binding structures.
217 These describe the values of the identifier in the three different
218 namespaces defined by the language. */
220 struct GTY(()) lang_identifier {
221 struct c_common_identifier common_id;
222 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
223 struct c_binding *tag_binding; /* struct/union/enum tags */
224 struct c_binding *label_binding; /* labels */
227 /* Validate c-lang.c's assumptions. */
228 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
229 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
231 /* The binding oracle; see c-tree.h. */
232 void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
234 /* This flag is set on an identifier if we have previously asked the
235 binding oracle for this identifier's symbol binding. */
236 #define I_SYMBOL_CHECKED(node) \
237 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
239 static inline struct c_binding* *
240 i_symbol_binding (tree node)
242 struct lang_identifier *lid
243 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
245 if (lid->symbol_binding == NULL
246 && c_binding_oracle != NULL
247 && !I_SYMBOL_CHECKED (node))
249 /* Set the "checked" flag first, to avoid infinite recursion
250 when the binding oracle calls back into gcc. */
251 I_SYMBOL_CHECKED (node) = 1;
252 c_binding_oracle (C_ORACLE_SYMBOL, node);
255 return &lid->symbol_binding;
258 #define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
260 #define I_SYMBOL_DECL(node) \
261 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
263 /* This flag is set on an identifier if we have previously asked the
264 binding oracle for this identifier's tag binding. */
265 #define I_TAG_CHECKED(node) \
266 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
268 static inline struct c_binding **
269 i_tag_binding (tree node)
271 struct lang_identifier *lid
272 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
274 if (lid->tag_binding == NULL
275 && c_binding_oracle != NULL
276 && !I_TAG_CHECKED (node))
278 /* Set the "checked" flag first, to avoid infinite recursion
279 when the binding oracle calls back into gcc. */
280 I_TAG_CHECKED (node) = 1;
281 c_binding_oracle (C_ORACLE_TAG, node);
284 return &lid->tag_binding;
287 #define I_TAG_BINDING(node) (*i_tag_binding (node))
289 #define I_TAG_DECL(node) \
290 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
292 /* This flag is set on an identifier if we have previously asked the
293 binding oracle for this identifier's label binding. */
294 #define I_LABEL_CHECKED(node) \
295 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
297 static inline struct c_binding **
298 i_label_binding (tree node)
300 struct lang_identifier *lid
301 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
303 if (lid->label_binding == NULL
304 && c_binding_oracle != NULL
305 && !I_LABEL_CHECKED (node))
307 /* Set the "checked" flag first, to avoid infinite recursion
308 when the binding oracle calls back into gcc. */
309 I_LABEL_CHECKED (node) = 1;
310 c_binding_oracle (C_ORACLE_LABEL, node);
313 return &lid->label_binding;
316 #define I_LABEL_BINDING(node) (*i_label_binding (node))
318 #define I_LABEL_DECL(node) \
319 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
321 /* The resulting tree type. */
323 union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
324 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
326 union tree_node GTY ((tag ("0"),
327 desc ("tree_node_structure (&%h)")))
328 generic;
329 struct lang_identifier GTY ((tag ("1"))) identifier;
332 /* Track bindings and other things that matter for goto warnings. For
333 efficiency, we do not gather all the decls at the point of
334 definition. Instead, we point into the bindings structure. As
335 scopes are popped, we update these structures and gather the decls
336 that matter at that time. */
338 struct GTY(()) c_spot_bindings {
339 /* The currently open scope which holds bindings defined when the
340 label was defined or the goto statement was found. */
341 struct c_scope *scope;
342 /* The bindings in the scope field which were defined at the point
343 of the label or goto. This lets us look at older or newer
344 bindings in the scope, as appropriate. */
345 struct c_binding *bindings_in_scope;
346 /* The number of statement expressions that have started since this
347 label or goto statement was defined. This is zero if we are at
348 the same statement expression level. It is positive if we are in
349 a statement expression started since this spot. It is negative
350 if this spot was in a statement expression and we have left
351 it. */
352 int stmt_exprs;
353 /* Whether we started in a statement expression but are no longer in
354 it. This is set to true if stmt_exprs ever goes negative. */
355 bool left_stmt_expr;
358 /* This structure is used to keep track of bindings seen when a goto
359 statement is defined. This is only used if we see the goto
360 statement before we see the label. */
362 struct GTY(()) c_goto_bindings {
363 /* The location of the goto statement. */
364 location_t loc;
365 /* The bindings of the goto statement. */
366 struct c_spot_bindings goto_bindings;
369 typedef struct c_goto_bindings *c_goto_bindings_p;
371 /* The additional information we keep track of for a label binding.
372 These fields are updated as scopes are popped. */
374 struct GTY(()) c_label_vars {
375 /* The shadowed c_label_vars, when one label shadows another (which
376 can only happen using a __label__ declaration). */
377 struct c_label_vars *shadowed;
378 /* The bindings when the label was defined. */
379 struct c_spot_bindings label_bindings;
380 /* A list of decls that we care about: decls about which we should
381 warn if a goto branches to this label from later in the function.
382 Decls are added to this list as scopes are popped. We only add
383 the decls that matter. */
384 vec<tree, va_gc> *decls_in_scope;
385 /* A list of goto statements to this label. This is only used for
386 goto statements seen before the label was defined, so that we can
387 issue appropriate warnings for them. */
388 vec<c_goto_bindings_p, va_gc> *gotos;
391 /* Each c_scope structure describes the complete contents of one
392 scope. Four scopes are distinguished specially: the innermost or
393 current scope, the innermost function scope, the file scope (always
394 the second to outermost) and the outermost or external scope.
396 Most declarations are recorded in the current scope.
398 All normal label declarations are recorded in the innermost
399 function scope, as are bindings of undeclared identifiers to
400 error_mark_node. (GCC permits nested functions as an extension,
401 hence the 'innermost' qualifier.) Explicitly declared labels
402 (using the __label__ extension) appear in the current scope.
404 Being in the file scope (current_scope == file_scope) causes
405 special behavior in several places below. Also, under some
406 conditions the Objective-C front end records declarations in the
407 file scope even though that isn't the current scope.
409 All declarations with external linkage are recorded in the external
410 scope, even if they aren't visible there; this models the fact that
411 such declarations are visible to the entire program, and (with a
412 bit of cleverness, see pushdecl) allows diagnosis of some violations
413 of C99 6.2.2p7 and 6.2.7p2:
415 If, within the same translation unit, the same identifier appears
416 with both internal and external linkage, the behavior is
417 undefined.
419 All declarations that refer to the same object or function shall
420 have compatible type; otherwise, the behavior is undefined.
422 Initially only the built-in declarations, which describe compiler
423 intrinsic functions plus a subset of the standard library, are in
424 this scope.
426 The order of the blocks list matters, and it is frequently appended
427 to. To avoid having to walk all the way to the end of the list on
428 each insertion, or reverse the list later, we maintain a pointer to
429 the last list entry. (FIXME: It should be feasible to use a reversed
430 list here.)
432 The bindings list is strictly in reverse order of declarations;
433 pop_scope relies on this. */
436 struct GTY((chain_next ("%h.outer"))) c_scope {
437 /* The scope containing this one. */
438 struct c_scope *outer;
440 /* The next outermost function scope. */
441 struct c_scope *outer_function;
443 /* All bindings in this scope. */
444 struct c_binding *bindings;
446 /* For each scope (except the global one), a chain of BLOCK nodes
447 for all the scopes that were entered and exited one level down. */
448 tree blocks;
449 tree blocks_last;
451 /* The depth of this scope. Used to keep the ->shadowed chain of
452 bindings sorted innermost to outermost. */
453 unsigned int depth : 28;
455 /* True if we are currently filling this scope with parameter
456 declarations. */
457 BOOL_BITFIELD parm_flag : 1;
459 /* True if we saw [*] in this scope. Used to give an error messages
460 if these appears in a function definition. */
461 BOOL_BITFIELD had_vla_unspec : 1;
463 /* True if we already complained about forward parameter decls
464 in this scope. This prevents double warnings on
465 foo (int a; int b; ...) */
466 BOOL_BITFIELD warned_forward_parm_decls : 1;
468 /* True if this is the outermost block scope of a function body.
469 This scope contains the parameters, the local variables declared
470 in the outermost block, and all the labels (except those in
471 nested functions, or declared at block scope with __label__). */
472 BOOL_BITFIELD function_body : 1;
474 /* True means make a BLOCK for this scope no matter what. */
475 BOOL_BITFIELD keep : 1;
477 /* True means that an unsuffixed float constant is _Decimal64. */
478 BOOL_BITFIELD float_const_decimal64 : 1;
480 /* True if this scope has any label bindings. This is used to speed
481 up searching for labels when popping scopes, particularly since
482 labels are normally only found at function scope. */
483 BOOL_BITFIELD has_label_bindings : 1;
485 /* True if we should issue a warning if a goto statement crosses any
486 of the bindings. We still need to check the list of bindings to
487 find the specific ones we need to warn about. This is true if
488 decl_jump_unsafe would return true for any of the bindings. This
489 is used to avoid looping over all the bindings unnecessarily. */
490 BOOL_BITFIELD has_jump_unsafe_decl : 1;
493 /* The scope currently in effect. */
495 static GTY(()) struct c_scope *current_scope;
497 /* The innermost function scope. Ordinary (not explicitly declared)
498 labels, bindings to error_mark_node, and the lazily-created
499 bindings of __func__ and its friends get this scope. */
501 static GTY(()) struct c_scope *current_function_scope;
503 /* The C file scope. This is reset for each input translation unit. */
505 static GTY(()) struct c_scope *file_scope;
507 /* The outermost scope. This is used for all declarations with
508 external linkage, and only these, hence the name. */
510 static GTY(()) struct c_scope *external_scope;
512 /* A chain of c_scope structures awaiting reuse. */
514 static GTY((deletable)) struct c_scope *scope_freelist;
516 /* A chain of c_binding structures awaiting reuse. */
518 static GTY((deletable)) struct c_binding *binding_freelist;
520 /* Append VAR to LIST in scope SCOPE. */
521 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
522 struct c_scope *s_ = (scope); \
523 tree d_ = (decl); \
524 if (s_->list##_last) \
525 BLOCK_CHAIN (s_->list##_last) = d_; \
526 else \
527 s_->list = d_; \
528 s_->list##_last = d_; \
529 } while (0)
531 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
532 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
533 struct c_scope *t_ = (tscope); \
534 struct c_scope *f_ = (fscope); \
535 if (t_->to##_last) \
536 BLOCK_CHAIN (t_->to##_last) = f_->from; \
537 else \
538 t_->to = f_->from; \
539 t_->to##_last = f_->from##_last; \
540 } while (0)
542 /* A c_inline_static structure stores details of a static identifier
543 referenced in a definition of a function that may be an inline
544 definition if no subsequent declaration of that function uses
545 "extern" or does not use "inline". */
547 struct GTY((chain_next ("%h.next"))) c_inline_static {
548 /* The location for a diagnostic. */
549 location_t location;
551 /* The function that may be an inline definition. */
552 tree function;
554 /* The object or function referenced. */
555 tree static_decl;
557 /* What sort of reference this is. */
558 enum c_inline_static_type type;
560 /* The next such structure or NULL. */
561 struct c_inline_static *next;
564 /* List of static identifiers used or referenced in functions that may
565 be inline definitions. */
566 static GTY(()) struct c_inline_static *c_inline_statics;
568 /* True means unconditionally make a BLOCK for the next scope pushed. */
570 static bool keep_next_level_flag;
572 /* True means the next call to push_scope will be the outermost scope
573 of a function body, so do not push a new scope, merely cease
574 expecting parameter decls. */
576 static bool next_is_function_body;
578 /* A vector of pointers to c_binding structures. */
580 typedef struct c_binding *c_binding_ptr;
582 /* Information that we keep for a struct or union while it is being
583 parsed. */
585 struct c_struct_parse_info
587 /* If warn_cxx_compat, a list of types defined within this
588 struct. */
589 vec<tree> struct_types;
590 /* If warn_cxx_compat, a list of field names which have bindings,
591 and which are defined in this struct, but which are not defined
592 in any enclosing struct. This is used to clear the in_struct
593 field of the c_bindings structure. */
594 vec<c_binding_ptr> fields;
595 /* If warn_cxx_compat, a list of typedef names used when defining
596 fields in this struct. */
597 vec<tree> typedefs_seen;
600 /* Information for the struct or union currently being parsed, or
601 NULL if not parsing a struct or union. */
602 static struct c_struct_parse_info *struct_parse_info;
604 /* Forward declarations. */
605 static tree lookup_name_in_scope (tree, struct c_scope *);
606 static tree c_make_fname_decl (location_t, tree, int);
607 static tree grokdeclarator (const struct c_declarator *,
608 struct c_declspecs *,
609 enum decl_context, bool, tree *, tree *, tree *,
610 bool *, enum deprecated_states);
611 static tree grokparms (struct c_arg_info *, bool);
612 static void layout_array_type (tree);
613 static void warn_defaults_to (location_t, int, const char *, ...)
614 ATTRIBUTE_GCC_DIAG(3,4);
616 /* T is a statement. Add it to the statement-tree. This is the
617 C/ObjC version--C++ has a slightly different version of this
618 function. */
620 tree
621 add_stmt (tree t)
623 enum tree_code code = TREE_CODE (t);
625 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
627 if (!EXPR_HAS_LOCATION (t))
628 SET_EXPR_LOCATION (t, input_location);
631 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
632 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
634 /* Add T to the statement-tree. Non-side-effect statements need to be
635 recorded during statement expressions. */
636 if (!building_stmt_list_p ())
637 push_stmt_list ();
638 append_to_statement_list_force (t, &cur_stmt_list);
640 return t;
643 /* Build a pointer type using the default pointer mode. */
645 static tree
646 c_build_pointer_type (tree to_type)
648 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
649 : TYPE_ADDR_SPACE (to_type);
650 machine_mode pointer_mode;
652 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
653 pointer_mode = targetm.addr_space.pointer_mode (as);
654 else
655 pointer_mode = c_default_pointer_mode;
656 return build_pointer_type_for_mode (to_type, pointer_mode, false);
660 /* Return true if we will want to say something if a goto statement
661 crosses DECL. */
663 static bool
664 decl_jump_unsafe (tree decl)
666 if (error_operand_p (decl))
667 return false;
669 /* Always warn about crossing variably modified types. */
670 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
671 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
672 return true;
674 /* Otherwise, only warn if -Wgoto-misses-init and this is an
675 initialized automatic decl. */
676 if (warn_jump_misses_init
677 && VAR_P (decl)
678 && !TREE_STATIC (decl)
679 && DECL_INITIAL (decl) != NULL_TREE)
680 return true;
682 return false;
686 void
687 c_print_identifier (FILE *file, tree node, int indent)
689 void (*save) (enum c_oracle_request, tree identifier);
691 /* Temporarily hide any binding oracle. Without this, calls to
692 debug_tree from the debugger will end up calling into the oracle,
693 making for a confusing debug session. As the oracle isn't needed
694 here for normal operation, it's simplest to suppress it. */
695 save = c_binding_oracle;
696 c_binding_oracle = NULL;
698 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
699 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
700 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
701 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
703 tree rid = ridpointers[C_RID_CODE (node)];
704 indent_to (file, indent + 4);
705 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
706 (void *) rid, IDENTIFIER_POINTER (rid));
709 c_binding_oracle = save;
712 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
713 which may be any of several kinds of DECL or TYPE or error_mark_node,
714 in the scope SCOPE. */
715 static void
716 bind (tree name, tree decl, struct c_scope *scope, bool invisible,
717 bool nested, location_t locus)
719 struct c_binding *b, **here;
721 if (binding_freelist)
723 b = binding_freelist;
724 binding_freelist = b->prev;
726 else
727 b = ggc_alloc<c_binding> ();
729 b->shadowed = 0;
730 b->decl = decl;
731 b->id = name;
732 b->depth = scope->depth;
733 b->invisible = invisible;
734 b->nested = nested;
735 b->inner_comp = 0;
736 b->in_struct = 0;
737 b->locus = locus;
739 b->u.type = NULL;
741 b->prev = scope->bindings;
742 scope->bindings = b;
744 if (decl_jump_unsafe (decl))
745 scope->has_jump_unsafe_decl = 1;
747 if (!name)
748 return;
750 switch (TREE_CODE (decl))
752 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
753 case ENUMERAL_TYPE:
754 case UNION_TYPE:
755 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
756 case VAR_DECL:
757 case FUNCTION_DECL:
758 case TYPE_DECL:
759 case CONST_DECL:
760 case PARM_DECL:
761 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
763 default:
764 gcc_unreachable ();
767 /* Locate the appropriate place in the chain of shadowed decls
768 to insert this binding. Normally, scope == current_scope and
769 this does nothing. */
770 while (*here && (*here)->depth > scope->depth)
771 here = &(*here)->shadowed;
773 b->shadowed = *here;
774 *here = b;
777 /* Clear the binding structure B, stick it on the binding_freelist,
778 and return the former value of b->prev. This is used by pop_scope
779 and get_parm_info to iterate destructively over all the bindings
780 from a given scope. */
781 static struct c_binding *
782 free_binding_and_advance (struct c_binding *b)
784 struct c_binding *prev = b->prev;
786 memset (b, 0, sizeof (struct c_binding));
787 b->prev = binding_freelist;
788 binding_freelist = b;
790 return prev;
793 /* Bind a label. Like bind, but skip fields which aren't used for
794 labels, and add the LABEL_VARS value. */
795 static void
796 bind_label (tree name, tree label, struct c_scope *scope,
797 struct c_label_vars *label_vars)
799 struct c_binding *b;
801 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
802 UNKNOWN_LOCATION);
804 scope->has_label_bindings = true;
806 b = scope->bindings;
807 gcc_assert (b->decl == label);
808 label_vars->shadowed = b->u.label;
809 b->u.label = label_vars;
812 /* Hook called at end of compilation to assume 1 elt
813 for a file-scope tentative array defn that wasn't complete before. */
815 void
816 c_finish_incomplete_decl (tree decl)
818 if (VAR_P (decl))
820 tree type = TREE_TYPE (decl);
821 if (type != error_mark_node
822 && TREE_CODE (type) == ARRAY_TYPE
823 && !DECL_EXTERNAL (decl)
824 && TYPE_DOMAIN (type) == 0)
826 warning_at (DECL_SOURCE_LOCATION (decl),
827 0, "array %q+D assumed to have one element", decl);
829 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
831 relayout_decl (decl);
836 /* Record that inline function FUNC contains a reference (location
837 LOC) to static DECL (file-scope or function-local according to
838 TYPE). */
840 void
841 record_inline_static (location_t loc, tree func, tree decl,
842 enum c_inline_static_type type)
844 c_inline_static *csi = ggc_alloc<c_inline_static> ();
845 csi->location = loc;
846 csi->function = func;
847 csi->static_decl = decl;
848 csi->type = type;
849 csi->next = c_inline_statics;
850 c_inline_statics = csi;
853 /* Check for references to static declarations in inline functions at
854 the end of the translation unit and diagnose them if the functions
855 are still inline definitions. */
857 static void
858 check_inline_statics (void)
860 struct c_inline_static *csi;
861 for (csi = c_inline_statics; csi; csi = csi->next)
863 if (DECL_EXTERNAL (csi->function))
864 switch (csi->type)
866 case csi_internal:
867 pedwarn (csi->location, 0,
868 "%qD is static but used in inline function %qD "
869 "which is not static", csi->static_decl, csi->function);
870 break;
871 case csi_modifiable:
872 pedwarn (csi->location, 0,
873 "%q+D is static but declared in inline function %qD "
874 "which is not static", csi->static_decl, csi->function);
875 break;
876 default:
877 gcc_unreachable ();
880 c_inline_statics = NULL;
883 /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
884 for the current state, otherwise set it to uninitialized. */
886 static void
887 set_spot_bindings (struct c_spot_bindings *p, bool defining)
889 if (defining)
891 p->scope = current_scope;
892 p->bindings_in_scope = current_scope->bindings;
894 else
896 p->scope = NULL;
897 p->bindings_in_scope = NULL;
899 p->stmt_exprs = 0;
900 p->left_stmt_expr = false;
903 /* Update spot bindings P as we pop out of SCOPE. Return true if we
904 should push decls for a label. */
906 static bool
907 update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
909 if (p->scope != scope)
911 /* This label or goto is defined in some other scope, or it is a
912 label which is not yet defined. There is nothing to
913 update. */
914 return false;
917 /* Adjust the spot bindings to refer to the bindings already defined
918 in the enclosing scope. */
919 p->scope = scope->outer;
920 p->bindings_in_scope = p->scope->bindings;
922 return true;
925 /* The Objective-C front-end often needs to determine the current scope. */
927 void *
928 objc_get_current_scope (void)
930 return current_scope;
933 /* The following function is used only by Objective-C. It needs to live here
934 because it accesses the innards of c_scope. */
936 void
937 objc_mark_locals_volatile (void *enclosing_blk)
939 struct c_scope *scope;
940 struct c_binding *b;
942 for (scope = current_scope;
943 scope && scope != enclosing_blk;
944 scope = scope->outer)
946 for (b = scope->bindings; b; b = b->prev)
947 objc_volatilize_decl (b->decl);
949 /* Do not climb up past the current function. */
950 if (scope->function_body)
951 break;
955 /* Return true if we are in the global binding level. */
957 bool
958 global_bindings_p (void)
960 return current_scope == file_scope;
963 void
964 keep_next_level (void)
966 keep_next_level_flag = true;
969 /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
971 void
972 set_float_const_decimal64 (void)
974 current_scope->float_const_decimal64 = true;
977 /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
979 void
980 clear_float_const_decimal64 (void)
982 current_scope->float_const_decimal64 = false;
985 /* Return nonzero if an unsuffixed float constant is _Decimal64. */
987 bool
988 float_const_decimal64_p (void)
990 return current_scope->float_const_decimal64;
993 /* Identify this scope as currently being filled with parameters. */
995 void
996 declare_parm_level (void)
998 current_scope->parm_flag = true;
1001 void
1002 push_scope (void)
1004 if (next_is_function_body)
1006 /* This is the transition from the parameters to the top level
1007 of the function body. These are the same scope
1008 (C99 6.2.1p4,6) so we do not push another scope structure.
1009 next_is_function_body is set only by store_parm_decls, which
1010 in turn is called when and only when we are about to
1011 encounter the opening curly brace for the function body.
1013 The outermost block of a function always gets a BLOCK node,
1014 because the debugging output routines expect that each
1015 function has at least one BLOCK. */
1016 current_scope->parm_flag = false;
1017 current_scope->function_body = true;
1018 current_scope->keep = true;
1019 current_scope->outer_function = current_function_scope;
1020 current_function_scope = current_scope;
1022 keep_next_level_flag = false;
1023 next_is_function_body = false;
1025 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1026 if (current_scope->outer)
1027 current_scope->float_const_decimal64
1028 = current_scope->outer->float_const_decimal64;
1029 else
1030 current_scope->float_const_decimal64 = false;
1032 else
1034 struct c_scope *scope;
1035 if (scope_freelist)
1037 scope = scope_freelist;
1038 scope_freelist = scope->outer;
1040 else
1041 scope = ggc_cleared_alloc<c_scope> ();
1043 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1044 if (current_scope)
1045 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1046 else
1047 scope->float_const_decimal64 = false;
1049 scope->keep = keep_next_level_flag;
1050 scope->outer = current_scope;
1051 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1053 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1054 possible. */
1055 if (current_scope && scope->depth == 0)
1057 scope->depth--;
1058 sorry ("GCC supports only %u nested scopes", scope->depth);
1061 current_scope = scope;
1062 keep_next_level_flag = false;
1066 /* This is called when we are leaving SCOPE. For each label defined
1067 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1068 These are the decls whose initialization will be skipped by a goto
1069 later in the function. */
1071 static void
1072 update_label_decls (struct c_scope *scope)
1074 struct c_scope *s;
1076 s = scope;
1077 while (s != NULL)
1079 if (s->has_label_bindings)
1081 struct c_binding *b;
1083 for (b = s->bindings; b != NULL; b = b->prev)
1085 struct c_label_vars *label_vars;
1086 struct c_binding *b1;
1087 bool hjud;
1088 unsigned int ix;
1089 struct c_goto_bindings *g;
1091 if (TREE_CODE (b->decl) != LABEL_DECL)
1092 continue;
1093 label_vars = b->u.label;
1095 b1 = label_vars->label_bindings.bindings_in_scope;
1096 if (label_vars->label_bindings.scope == NULL)
1097 hjud = false;
1098 else
1099 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1100 if (update_spot_bindings (scope, &label_vars->label_bindings))
1102 /* This label is defined in this scope. */
1103 if (hjud)
1105 for (; b1 != NULL; b1 = b1->prev)
1107 /* A goto from later in the function to this
1108 label will never see the initialization
1109 of B1, if any. Save it to issue a
1110 warning if needed. */
1111 if (decl_jump_unsafe (b1->decl))
1112 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1117 /* Update the bindings of any goto statements associated
1118 with this label. */
1119 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1120 update_spot_bindings (scope, &g->goto_bindings);
1124 /* Don't search beyond the current function. */
1125 if (s == current_function_scope)
1126 break;
1128 s = s->outer;
1132 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1134 static void
1135 set_type_context (tree type, tree context)
1137 for (type = TYPE_MAIN_VARIANT (type); type;
1138 type = TYPE_NEXT_VARIANT (type))
1139 TYPE_CONTEXT (type) = context;
1142 /* Exit a scope. Restore the state of the identifier-decl mappings
1143 that were in effect when this scope was entered. Return a BLOCK
1144 node containing all the DECLs in this scope that are of interest
1145 to debug info generation. */
1147 tree
1148 pop_scope (void)
1150 struct c_scope *scope = current_scope;
1151 tree block, context, p;
1152 struct c_binding *b;
1154 bool functionbody = scope->function_body;
1155 bool keep = functionbody || scope->keep || scope->bindings;
1157 update_label_decls (scope);
1159 /* If appropriate, create a BLOCK to record the decls for the life
1160 of this function. */
1161 block = 0;
1162 if (keep)
1164 block = make_node (BLOCK);
1165 BLOCK_SUBBLOCKS (block) = scope->blocks;
1166 TREE_USED (block) = 1;
1168 /* In each subblock, record that this is its superior. */
1169 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1170 BLOCK_SUPERCONTEXT (p) = block;
1172 BLOCK_VARS (block) = 0;
1175 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1176 scope must be set so that they point to the appropriate
1177 construct, i.e. either to the current FUNCTION_DECL node, or
1178 else to the BLOCK node we just constructed.
1180 Note that for tagged types whose scope is just the formal
1181 parameter list for some function type specification, we can't
1182 properly set their TYPE_CONTEXTs here, because we don't have a
1183 pointer to the appropriate FUNCTION_TYPE node readily available
1184 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1185 type nodes get set in `grokdeclarator' as soon as we have created
1186 the FUNCTION_TYPE node which will represent the "scope" for these
1187 "parameter list local" tagged types. */
1188 if (scope->function_body)
1189 context = current_function_decl;
1190 else if (scope == file_scope)
1192 tree file_decl = build_translation_unit_decl (NULL_TREE);
1193 context = file_decl;
1194 debug_hooks->register_main_translation_unit (file_decl);
1196 else
1197 context = block;
1199 /* Clear all bindings in this scope. */
1200 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1202 p = b->decl;
1203 switch (TREE_CODE (p))
1205 case LABEL_DECL:
1206 /* Warnings for unused labels, errors for undefined labels. */
1207 if (TREE_USED (p) && !DECL_INITIAL (p))
1209 error ("label %q+D used but not defined", p);
1210 DECL_INITIAL (p) = error_mark_node;
1212 else
1213 warn_for_unused_label (p);
1215 /* Labels go in BLOCK_VARS. */
1216 DECL_CHAIN (p) = BLOCK_VARS (block);
1217 BLOCK_VARS (block) = p;
1218 gcc_assert (I_LABEL_BINDING (b->id) == b);
1219 I_LABEL_BINDING (b->id) = b->shadowed;
1221 /* Also pop back to the shadowed label_vars. */
1222 release_tree_vector (b->u.label->decls_in_scope);
1223 b->u.label = b->u.label->shadowed;
1224 break;
1226 case ENUMERAL_TYPE:
1227 case UNION_TYPE:
1228 case RECORD_TYPE:
1229 set_type_context (p, context);
1231 /* Types may not have tag-names, in which case the type
1232 appears in the bindings list with b->id NULL. */
1233 if (b->id)
1235 gcc_assert (I_TAG_BINDING (b->id) == b);
1236 I_TAG_BINDING (b->id) = b->shadowed;
1238 break;
1240 case FUNCTION_DECL:
1241 /* Propagate TREE_ADDRESSABLE from nested functions to their
1242 containing functions. */
1243 if (!TREE_ASM_WRITTEN (p)
1244 && DECL_INITIAL (p) != 0
1245 && TREE_ADDRESSABLE (p)
1246 && DECL_ABSTRACT_ORIGIN (p) != 0
1247 && DECL_ABSTRACT_ORIGIN (p) != p)
1248 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1249 if (!DECL_EXTERNAL (p)
1250 && !DECL_INITIAL (p)
1251 && scope != file_scope
1252 && scope != external_scope)
1254 error ("nested function %q+D declared but never defined", p);
1255 undef_nested_function = true;
1257 else if (DECL_DECLARED_INLINE_P (p)
1258 && TREE_PUBLIC (p)
1259 && !DECL_INITIAL (p))
1261 /* C99 6.7.4p6: "a function with external linkage... declared
1262 with an inline function specifier ... shall also be defined
1263 in the same translation unit." */
1264 if (!flag_gnu89_inline
1265 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1266 && scope != external_scope)
1267 pedwarn (input_location, 0,
1268 "inline function %q+D declared but never defined", p);
1269 DECL_EXTERNAL (p) = 1;
1272 goto common_symbol;
1274 case VAR_DECL:
1275 /* Warnings for unused variables. */
1276 if ((!TREE_USED (p) || !DECL_READ_P (p))
1277 && !TREE_NO_WARNING (p)
1278 && !DECL_IN_SYSTEM_HEADER (p)
1279 && DECL_NAME (p)
1280 && !DECL_ARTIFICIAL (p)
1281 && scope != file_scope
1282 && scope != external_scope)
1284 if (!TREE_USED (p))
1285 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1286 else if (DECL_CONTEXT (p) == current_function_decl)
1287 warning_at (DECL_SOURCE_LOCATION (p),
1288 OPT_Wunused_but_set_variable,
1289 "variable %qD set but not used", p);
1292 if (b->inner_comp)
1294 error ("type of array %q+D completed incompatibly with"
1295 " implicit initialization", p);
1298 /* Fall through. */
1299 case TYPE_DECL:
1300 case CONST_DECL:
1301 common_symbol:
1302 /* All of these go in BLOCK_VARS, but only if this is the
1303 binding in the home scope. */
1304 if (!b->nested)
1306 DECL_CHAIN (p) = BLOCK_VARS (block);
1307 BLOCK_VARS (block) = p;
1309 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1311 /* For block local externs add a special
1312 DECL_EXTERNAL decl for debug info generation. */
1313 tree extp = copy_node (p);
1315 DECL_EXTERNAL (extp) = 1;
1316 TREE_STATIC (extp) = 0;
1317 TREE_PUBLIC (extp) = 1;
1318 DECL_INITIAL (extp) = NULL_TREE;
1319 DECL_LANG_SPECIFIC (extp) = NULL;
1320 DECL_CONTEXT (extp) = current_function_decl;
1321 if (TREE_CODE (p) == FUNCTION_DECL)
1323 DECL_RESULT (extp) = NULL_TREE;
1324 DECL_SAVED_TREE (extp) = NULL_TREE;
1325 DECL_STRUCT_FUNCTION (extp) = NULL;
1327 if (b->locus != UNKNOWN_LOCATION)
1328 DECL_SOURCE_LOCATION (extp) = b->locus;
1329 DECL_CHAIN (extp) = BLOCK_VARS (block);
1330 BLOCK_VARS (block) = extp;
1332 /* If this is the file scope set DECL_CONTEXT of each decl to
1333 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1334 work. */
1335 if (scope == file_scope)
1337 DECL_CONTEXT (p) = context;
1338 if (TREE_CODE (p) == TYPE_DECL
1339 && TREE_TYPE (p) != error_mark_node)
1340 set_type_context (TREE_TYPE (p), context);
1343 /* Fall through. */
1344 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1345 already been put there by store_parm_decls. Unused-
1346 parameter warnings are handled by function.c.
1347 error_mark_node obviously does not go in BLOCK_VARS and
1348 does not get unused-variable warnings. */
1349 case PARM_DECL:
1350 case ERROR_MARK:
1351 /* It is possible for a decl not to have a name. We get
1352 here with b->id NULL in this case. */
1353 if (b->id)
1355 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1356 I_SYMBOL_BINDING (b->id) = b->shadowed;
1357 if (b->shadowed && b->shadowed->u.type)
1358 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1360 break;
1362 default:
1363 gcc_unreachable ();
1368 /* Dispose of the block that we just made inside some higher level. */
1369 if ((scope->function_body || scope == file_scope) && context)
1371 DECL_INITIAL (context) = block;
1372 BLOCK_SUPERCONTEXT (block) = context;
1374 else if (scope->outer)
1376 if (block)
1377 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1378 /* If we did not make a block for the scope just exited, any
1379 blocks made for inner scopes must be carried forward so they
1380 will later become subblocks of something else. */
1381 else if (scope->blocks)
1382 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1385 /* Pop the current scope, and free the structure for reuse. */
1386 current_scope = scope->outer;
1387 if (scope->function_body)
1388 current_function_scope = scope->outer_function;
1390 memset (scope, 0, sizeof (struct c_scope));
1391 scope->outer = scope_freelist;
1392 scope_freelist = scope;
1394 return block;
1397 void
1398 push_file_scope (void)
1400 tree decl;
1402 if (file_scope)
1403 return;
1405 push_scope ();
1406 file_scope = current_scope;
1408 start_fname_decls ();
1410 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1411 bind (DECL_NAME (decl), decl, file_scope,
1412 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1415 void
1416 pop_file_scope (void)
1418 /* In case there were missing closebraces, get us back to the global
1419 binding level. */
1420 while (current_scope != file_scope)
1421 pop_scope ();
1423 /* __FUNCTION__ is defined at file scope (""). This
1424 call may not be necessary as my tests indicate it
1425 still works without it. */
1426 finish_fname_decls ();
1428 check_inline_statics ();
1430 /* This is the point to write out a PCH if we're doing that.
1431 In that case we do not want to do anything else. */
1432 if (pch_file)
1434 c_common_write_pch ();
1435 return;
1438 /* Pop off the file scope and close this translation unit. */
1439 pop_scope ();
1440 file_scope = 0;
1442 maybe_apply_pending_pragma_weaks ();
1445 /* Adjust the bindings for the start of a statement expression. */
1447 void
1448 c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1450 struct c_scope *scope;
1452 for (scope = current_scope; scope != NULL; scope = scope->outer)
1454 struct c_binding *b;
1456 if (!scope->has_label_bindings)
1457 continue;
1459 for (b = scope->bindings; b != NULL; b = b->prev)
1461 struct c_label_vars *label_vars;
1462 unsigned int ix;
1463 struct c_goto_bindings *g;
1465 if (TREE_CODE (b->decl) != LABEL_DECL)
1466 continue;
1467 label_vars = b->u.label;
1468 ++label_vars->label_bindings.stmt_exprs;
1469 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1470 ++g->goto_bindings.stmt_exprs;
1474 if (switch_bindings != NULL)
1475 ++switch_bindings->stmt_exprs;
1478 /* Adjust the bindings for the end of a statement expression. */
1480 void
1481 c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1483 struct c_scope *scope;
1485 for (scope = current_scope; scope != NULL; scope = scope->outer)
1487 struct c_binding *b;
1489 if (!scope->has_label_bindings)
1490 continue;
1492 for (b = scope->bindings; b != NULL; b = b->prev)
1494 struct c_label_vars *label_vars;
1495 unsigned int ix;
1496 struct c_goto_bindings *g;
1498 if (TREE_CODE (b->decl) != LABEL_DECL)
1499 continue;
1500 label_vars = b->u.label;
1501 --label_vars->label_bindings.stmt_exprs;
1502 if (label_vars->label_bindings.stmt_exprs < 0)
1504 label_vars->label_bindings.left_stmt_expr = true;
1505 label_vars->label_bindings.stmt_exprs = 0;
1507 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1509 --g->goto_bindings.stmt_exprs;
1510 if (g->goto_bindings.stmt_exprs < 0)
1512 g->goto_bindings.left_stmt_expr = true;
1513 g->goto_bindings.stmt_exprs = 0;
1519 if (switch_bindings != NULL)
1521 --switch_bindings->stmt_exprs;
1522 gcc_assert (switch_bindings->stmt_exprs >= 0);
1526 /* Push a definition or a declaration of struct, union or enum tag "name".
1527 "type" should be the type node.
1528 We assume that the tag "name" is not already defined, and has a location
1529 of LOC.
1531 Note that the definition may really be just a forward reference.
1532 In that case, the TYPE_SIZE will be zero. */
1534 static void
1535 pushtag (location_t loc, tree name, tree type)
1537 /* Record the identifier as the type's name if it has none. */
1538 if (name && !TYPE_NAME (type))
1539 TYPE_NAME (type) = name;
1540 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1542 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1543 tagged type we just added to the current scope. This fake
1544 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1545 to output a representation of a tagged type, and it also gives
1546 us a convenient place to record the "scope start" address for the
1547 tagged type. */
1549 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1550 TYPE_DECL, NULL_TREE, type));
1552 /* An approximation for now, so we can tell this is a function-scope tag.
1553 This will be updated in pop_scope. */
1554 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1556 if (warn_cxx_compat && name != NULL_TREE)
1558 struct c_binding *b = I_SYMBOL_BINDING (name);
1560 if (b != NULL
1561 && b->decl != NULL_TREE
1562 && TREE_CODE (b->decl) == TYPE_DECL
1563 && (B_IN_CURRENT_SCOPE (b)
1564 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1565 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1566 != TYPE_MAIN_VARIANT (type)))
1568 warning_at (loc, OPT_Wc___compat,
1569 ("using %qD as both a typedef and a tag is "
1570 "invalid in C++"),
1571 b->decl);
1572 if (b->locus != UNKNOWN_LOCATION)
1573 inform (b->locus, "originally defined here");
1578 /* An exported interface to pushtag. This is used by the gdb plugin's
1579 binding oracle to introduce a new tag binding. */
1581 void
1582 c_pushtag (location_t loc, tree name, tree type)
1584 pushtag (loc, name, type);
1587 /* An exported interface to bind a declaration. LOC is the location
1588 to use. DECL is the declaration to bind. The decl's name is used
1589 to determine how it is bound. If DECL is a VAR_DECL, then
1590 IS_GLOBAL determines whether the decl is put into the global (file
1591 and external) scope or the current function's scope; if DECL is not
1592 a VAR_DECL then it is always put into the file scope. */
1594 void
1595 c_bind (location_t loc, tree decl, bool is_global)
1597 struct c_scope *scope;
1598 bool nested = false;
1600 if (!VAR_P (decl) || current_function_scope == NULL)
1602 /* Types and functions are always considered to be global. */
1603 scope = file_scope;
1604 DECL_EXTERNAL (decl) = 1;
1605 TREE_PUBLIC (decl) = 1;
1607 else if (is_global)
1609 /* Also bind it into the external scope. */
1610 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1611 nested = true;
1612 scope = file_scope;
1613 DECL_EXTERNAL (decl) = 1;
1614 TREE_PUBLIC (decl) = 1;
1616 else
1618 DECL_CONTEXT (decl) = current_function_decl;
1619 TREE_PUBLIC (decl) = 0;
1620 scope = current_function_scope;
1623 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1626 /* Subroutine of compare_decls. Allow harmless mismatches in return
1627 and argument types provided that the type modes match. This function
1628 return a unified type given a suitable match, and 0 otherwise. */
1630 static tree
1631 match_builtin_function_types (tree newtype, tree oldtype)
1633 tree newrettype, oldrettype;
1634 tree newargs, oldargs;
1635 tree trytype, tryargs;
1637 /* Accept the return type of the new declaration if same modes. */
1638 oldrettype = TREE_TYPE (oldtype);
1639 newrettype = TREE_TYPE (newtype);
1641 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1642 return 0;
1644 oldargs = TYPE_ARG_TYPES (oldtype);
1645 newargs = TYPE_ARG_TYPES (newtype);
1646 tryargs = newargs;
1648 while (oldargs || newargs)
1650 if (!oldargs
1651 || !newargs
1652 || !TREE_VALUE (oldargs)
1653 || !TREE_VALUE (newargs)
1654 || TYPE_MODE (TREE_VALUE (oldargs))
1655 != TYPE_MODE (TREE_VALUE (newargs)))
1656 return 0;
1658 oldargs = TREE_CHAIN (oldargs);
1659 newargs = TREE_CHAIN (newargs);
1662 trytype = build_function_type (newrettype, tryargs);
1663 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1666 /* Subroutine of diagnose_mismatched_decls. Check for function type
1667 mismatch involving an empty arglist vs a nonempty one and give clearer
1668 diagnostics. */
1669 static void
1670 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1671 tree newtype, tree oldtype)
1673 tree t;
1675 if (TREE_CODE (olddecl) != FUNCTION_DECL
1676 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1677 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1678 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1679 return;
1681 t = TYPE_ARG_TYPES (oldtype);
1682 if (t == 0)
1683 t = TYPE_ARG_TYPES (newtype);
1684 for (; t; t = TREE_CHAIN (t))
1686 tree type = TREE_VALUE (t);
1688 if (TREE_CHAIN (t) == 0
1689 && TYPE_MAIN_VARIANT (type) != void_type_node)
1691 inform (input_location, "a parameter list with an ellipsis can%'t match "
1692 "an empty parameter name list declaration");
1693 break;
1696 if (c_type_promotes_to (type) != type)
1698 inform (input_location, "an argument type that has a default promotion can%'t match "
1699 "an empty parameter name list declaration");
1700 break;
1705 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1706 old-style function definition, NEWDECL is a prototype declaration.
1707 Diagnose inconsistencies in the argument list. Returns TRUE if
1708 the prototype is compatible, FALSE if not. */
1709 static bool
1710 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1712 tree newargs, oldargs;
1713 int i;
1715 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1717 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1718 newargs = TYPE_ARG_TYPES (newtype);
1719 i = 1;
1721 for (;;)
1723 tree oldargtype = TREE_VALUE (oldargs);
1724 tree newargtype = TREE_VALUE (newargs);
1726 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1727 return false;
1729 oldargtype = (TYPE_ATOMIC (oldargtype)
1730 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1731 TYPE_QUAL_ATOMIC)
1732 : TYPE_MAIN_VARIANT (oldargtype));
1733 newargtype = (TYPE_ATOMIC (newargtype)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1735 TYPE_QUAL_ATOMIC)
1736 : TYPE_MAIN_VARIANT (newargtype));
1738 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1739 break;
1741 /* Reaching the end of just one list means the two decls don't
1742 agree on the number of arguments. */
1743 if (END_OF_ARGLIST (oldargtype))
1745 error ("prototype for %q+D declares more arguments "
1746 "than previous old-style definition", newdecl);
1747 return false;
1749 else if (END_OF_ARGLIST (newargtype))
1751 error ("prototype for %q+D declares fewer arguments "
1752 "than previous old-style definition", newdecl);
1753 return false;
1756 /* Type for passing arg must be consistent with that declared
1757 for the arg. */
1758 else if (!comptypes (oldargtype, newargtype))
1760 error ("prototype for %q+D declares argument %d"
1761 " with incompatible type",
1762 newdecl, i);
1763 return false;
1766 oldargs = TREE_CHAIN (oldargs);
1767 newargs = TREE_CHAIN (newargs);
1768 i++;
1771 /* If we get here, no errors were found, but do issue a warning
1772 for this poor-style construct. */
1773 warning (0, "prototype for %q+D follows non-prototype definition",
1774 newdecl);
1775 return true;
1776 #undef END_OF_ARGLIST
1779 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1780 first in a pair of mismatched declarations, using the diagnostic
1781 function DIAG. */
1782 static void
1783 locate_old_decl (tree decl)
1785 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1786 && !C_DECL_DECLARED_BUILTIN (decl))
1788 else if (DECL_INITIAL (decl))
1789 inform (input_location, "previous definition of %q+D was here", decl);
1790 else if (C_DECL_IMPLICIT (decl))
1791 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1792 else
1793 inform (input_location, "previous declaration of %q+D was here", decl);
1796 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1797 Returns true if the caller should proceed to merge the two, false
1798 if OLDDECL should simply be discarded. As a side effect, issues
1799 all necessary diagnostics for invalid or poor-style combinations.
1800 If it returns true, writes the types of NEWDECL and OLDDECL to
1801 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1802 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1804 static bool
1805 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1806 tree *newtypep, tree *oldtypep)
1808 tree newtype, oldtype;
1809 bool pedwarned = false;
1810 bool warned = false;
1811 bool retval = true;
1813 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1814 && DECL_EXTERNAL (DECL))
1816 /* If we have error_mark_node for either decl or type, just discard
1817 the previous decl - we're in an error cascade already. */
1818 if (olddecl == error_mark_node || newdecl == error_mark_node)
1819 return false;
1820 *oldtypep = oldtype = TREE_TYPE (olddecl);
1821 *newtypep = newtype = TREE_TYPE (newdecl);
1822 if (oldtype == error_mark_node || newtype == error_mark_node)
1823 return false;
1825 /* Two different categories of symbol altogether. This is an error
1826 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1827 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1829 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1830 && DECL_BUILT_IN (olddecl)
1831 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1833 error ("%q+D redeclared as different kind of symbol", newdecl);
1834 locate_old_decl (olddecl);
1836 else if (TREE_PUBLIC (newdecl))
1837 warning (0, "built-in function %q+D declared as non-function",
1838 newdecl);
1839 else
1840 warning (OPT_Wshadow, "declaration of %q+D shadows "
1841 "a built-in function", newdecl);
1842 return false;
1845 /* Enumerators have no linkage, so may only be declared once in a
1846 given scope. */
1847 if (TREE_CODE (olddecl) == CONST_DECL)
1849 error ("redeclaration of enumerator %q+D", newdecl);
1850 locate_old_decl (olddecl);
1851 return false;
1854 if (!comptypes (oldtype, newtype))
1856 if (TREE_CODE (olddecl) == FUNCTION_DECL
1857 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1859 /* Accept harmless mismatch in function types.
1860 This is for the ffs and fprintf builtins. */
1861 tree trytype = match_builtin_function_types (newtype, oldtype);
1863 if (trytype && comptypes (newtype, trytype))
1864 *oldtypep = oldtype = trytype;
1865 else
1867 /* If types don't match for a built-in, throw away the
1868 built-in. No point in calling locate_old_decl here, it
1869 won't print anything. */
1870 warning (0, "conflicting types for built-in function %q+D",
1871 newdecl);
1872 return false;
1875 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1876 && DECL_IS_BUILTIN (olddecl))
1878 /* A conflicting function declaration for a predeclared
1879 function that isn't actually built in. Objective C uses
1880 these. The new declaration silently overrides everything
1881 but the volatility (i.e. noreturn) indication. See also
1882 below. FIXME: Make Objective C use normal builtins. */
1883 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1884 return false;
1886 /* Permit void foo (...) to match int foo (...) if the latter is
1887 the definition and implicit int was used. See
1888 c-torture/compile/920625-2.c. */
1889 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1890 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1891 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1892 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1894 pedwarned = pedwarn (input_location, 0,
1895 "conflicting types for %q+D", newdecl);
1896 /* Make sure we keep void as the return type. */
1897 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1898 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1900 /* Permit void foo (...) to match an earlier call to foo (...) with
1901 no declared type (thus, implicitly int). */
1902 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1903 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1904 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1905 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1907 pedwarned = pedwarn (input_location, 0,
1908 "conflicting types for %q+D", newdecl);
1909 /* Make sure we keep void as the return type. */
1910 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1912 else
1914 int new_quals = TYPE_QUALS (newtype);
1915 int old_quals = TYPE_QUALS (oldtype);
1917 if (new_quals != old_quals)
1919 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1920 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1921 if (new_addr != old_addr)
1923 if (ADDR_SPACE_GENERIC_P (new_addr))
1924 error ("conflicting named address spaces (generic vs %s) "
1925 "for %q+D",
1926 c_addr_space_name (old_addr), newdecl);
1927 else if (ADDR_SPACE_GENERIC_P (old_addr))
1928 error ("conflicting named address spaces (%s vs generic) "
1929 "for %q+D",
1930 c_addr_space_name (new_addr), newdecl);
1931 else
1932 error ("conflicting named address spaces (%s vs %s) "
1933 "for %q+D",
1934 c_addr_space_name (new_addr),
1935 c_addr_space_name (old_addr),
1936 newdecl);
1939 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1940 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1941 error ("conflicting type qualifiers for %q+D", newdecl);
1943 else
1944 error ("conflicting types for %q+D", newdecl);
1945 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1946 locate_old_decl (olddecl);
1947 return false;
1951 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1952 but silently ignore the redeclaration if either is in a system
1953 header. (Conflicting redeclarations were handled above.) This
1954 is allowed for C11 if the types are the same, not just
1955 compatible. */
1956 if (TREE_CODE (newdecl) == TYPE_DECL)
1958 bool types_different = false;
1959 int comptypes_result;
1961 comptypes_result
1962 = comptypes_check_different_types (oldtype, newtype, &types_different);
1964 if (comptypes_result != 1 || types_different)
1966 error ("redefinition of typedef %q+D with different type", newdecl);
1967 locate_old_decl (olddecl);
1968 return false;
1971 if (DECL_IN_SYSTEM_HEADER (newdecl)
1972 || DECL_IN_SYSTEM_HEADER (olddecl)
1973 || TREE_NO_WARNING (newdecl)
1974 || TREE_NO_WARNING (olddecl))
1975 return true; /* Allow OLDDECL to continue in use. */
1977 if (variably_modified_type_p (newtype, NULL))
1979 error ("redefinition of typedef %q+D with variably modified type",
1980 newdecl);
1981 locate_old_decl (olddecl);
1983 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1984 "redefinition of typedef %q+D", newdecl))
1985 locate_old_decl (olddecl);
1987 return true;
1990 /* Function declarations can either be 'static' or 'extern' (no
1991 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1992 can never conflict with each other on account of linkage
1993 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1994 gnu89 mode permits two definitions if one is 'extern inline' and
1995 one is not. The non- extern-inline definition supersedes the
1996 extern-inline definition. */
1998 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2000 /* If you declare a built-in function name as static, or
2001 define the built-in with an old-style definition (so we
2002 can't validate the argument list) the built-in definition is
2003 overridden, but optionally warn this was a bad choice of name. */
2004 if (DECL_BUILT_IN (olddecl)
2005 && !C_DECL_DECLARED_BUILTIN (olddecl)
2006 && (!TREE_PUBLIC (newdecl)
2007 || (DECL_INITIAL (newdecl)
2008 && !prototype_p (TREE_TYPE (newdecl)))))
2010 warning (OPT_Wshadow, "declaration of %q+D shadows "
2011 "a built-in function", newdecl);
2012 /* Discard the old built-in function. */
2013 return false;
2016 if (DECL_INITIAL (newdecl))
2018 if (DECL_INITIAL (olddecl))
2020 /* If both decls are in the same TU and the new declaration
2021 isn't overriding an extern inline reject the new decl.
2022 In c99, no overriding is allowed in the same translation
2023 unit. */
2024 if ((!DECL_EXTERN_INLINE (olddecl)
2025 || DECL_EXTERN_INLINE (newdecl)
2026 || (!flag_gnu89_inline
2027 && (!DECL_DECLARED_INLINE_P (olddecl)
2028 || !lookup_attribute ("gnu_inline",
2029 DECL_ATTRIBUTES (olddecl)))
2030 && (!DECL_DECLARED_INLINE_P (newdecl)
2031 || !lookup_attribute ("gnu_inline",
2032 DECL_ATTRIBUTES (newdecl))))
2034 && same_translation_unit_p (newdecl, olddecl))
2036 error ("redefinition of %q+D", newdecl);
2037 locate_old_decl (olddecl);
2038 return false;
2042 /* If we have a prototype after an old-style function definition,
2043 the argument types must be checked specially. */
2044 else if (DECL_INITIAL (olddecl)
2045 && !prototype_p (oldtype) && prototype_p (newtype)
2046 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2047 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2049 locate_old_decl (olddecl);
2050 return false;
2052 /* A non-static declaration (even an "extern") followed by a
2053 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2054 The same is true for a static forward declaration at block
2055 scope followed by a non-static declaration/definition at file
2056 scope. Static followed by non-static at the same scope is
2057 not undefined behavior, and is the most convenient way to get
2058 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2059 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2060 we do diagnose it if -Wtraditional. */
2061 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2063 /* Two exceptions to the rule. If olddecl is an extern
2064 inline, or a predeclared function that isn't actually
2065 built in, newdecl silently overrides olddecl. The latter
2066 occur only in Objective C; see also above. (FIXME: Make
2067 Objective C use normal builtins.) */
2068 if (!DECL_IS_BUILTIN (olddecl)
2069 && !DECL_EXTERN_INLINE (olddecl))
2071 error ("static declaration of %q+D follows "
2072 "non-static declaration", newdecl);
2073 locate_old_decl (olddecl);
2075 return false;
2077 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2079 if (DECL_CONTEXT (olddecl))
2081 error ("non-static declaration of %q+D follows "
2082 "static declaration", newdecl);
2083 locate_old_decl (olddecl);
2084 return false;
2086 else if (warn_traditional)
2088 warned |= warning (OPT_Wtraditional,
2089 "non-static declaration of %q+D "
2090 "follows static declaration", newdecl);
2094 /* Make sure gnu_inline attribute is either not present, or
2095 present on all inline decls. */
2096 if (DECL_DECLARED_INLINE_P (olddecl)
2097 && DECL_DECLARED_INLINE_P (newdecl))
2099 bool newa = lookup_attribute ("gnu_inline",
2100 DECL_ATTRIBUTES (newdecl)) != NULL;
2101 bool olda = lookup_attribute ("gnu_inline",
2102 DECL_ATTRIBUTES (olddecl)) != NULL;
2103 if (newa != olda)
2105 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2106 newa ? newdecl : olddecl);
2107 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2108 "but not here");
2112 else if (VAR_P (newdecl))
2114 /* Only variables can be thread-local, and all declarations must
2115 agree on this property. */
2116 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2118 /* Nothing to check. Since OLDDECL is marked threadprivate
2119 and NEWDECL does not have a thread-local attribute, we
2120 will merge the threadprivate attribute into NEWDECL. */
2123 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2125 if (DECL_THREAD_LOCAL_P (newdecl))
2126 error ("thread-local declaration of %q+D follows "
2127 "non-thread-local declaration", newdecl);
2128 else
2129 error ("non-thread-local declaration of %q+D follows "
2130 "thread-local declaration", newdecl);
2132 locate_old_decl (olddecl);
2133 return false;
2136 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2137 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2139 error ("redefinition of %q+D", newdecl);
2140 locate_old_decl (olddecl);
2141 return false;
2144 /* Objects declared at file scope: if the first declaration had
2145 external linkage (even if it was an external reference) the
2146 second must have external linkage as well, or the behavior is
2147 undefined. If the first declaration had internal linkage, then
2148 the second must too, or else be an external reference (in which
2149 case the composite declaration still has internal linkage).
2150 As for function declarations, we warn about the static-then-
2151 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2152 if (DECL_FILE_SCOPE_P (newdecl)
2153 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2155 if (DECL_EXTERNAL (newdecl))
2157 if (!DECL_FILE_SCOPE_P (olddecl))
2159 error ("extern declaration of %q+D follows "
2160 "declaration with no linkage", newdecl);
2161 locate_old_decl (olddecl);
2162 return false;
2164 else if (warn_traditional)
2166 warned |= warning (OPT_Wtraditional,
2167 "non-static declaration of %q+D "
2168 "follows static declaration", newdecl);
2171 else
2173 if (TREE_PUBLIC (newdecl))
2174 error ("non-static declaration of %q+D follows "
2175 "static declaration", newdecl);
2176 else
2177 error ("static declaration of %q+D follows "
2178 "non-static declaration", newdecl);
2180 locate_old_decl (olddecl);
2181 return false;
2184 /* Two objects with the same name declared at the same block
2185 scope must both be external references (6.7p3). */
2186 else if (!DECL_FILE_SCOPE_P (newdecl))
2188 if (DECL_EXTERNAL (newdecl))
2190 /* Extern with initializer at block scope, which will
2191 already have received an error. */
2193 else if (DECL_EXTERNAL (olddecl))
2195 error ("declaration of %q+D with no linkage follows "
2196 "extern declaration", newdecl);
2197 locate_old_decl (olddecl);
2199 else
2201 error ("redeclaration of %q+D with no linkage", newdecl);
2202 locate_old_decl (olddecl);
2205 return false;
2208 /* C++ does not permit a decl to appear multiple times at file
2209 scope. */
2210 if (warn_cxx_compat
2211 && DECL_FILE_SCOPE_P (newdecl)
2212 && !DECL_EXTERNAL (newdecl)
2213 && !DECL_EXTERNAL (olddecl))
2214 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2215 OPT_Wc___compat,
2216 ("duplicate declaration of %qD is "
2217 "invalid in C++"),
2218 newdecl);
2221 /* warnings */
2222 /* All decls must agree on a visibility. */
2223 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2224 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2225 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2227 warned |= warning (0, "redeclaration of %q+D with different visibility "
2228 "(old visibility preserved)", newdecl);
2231 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2233 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2234 if (DECL_DECLARED_INLINE_P (newdecl)
2235 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2236 warned |= warning (OPT_Wattributes,
2237 "inline declaration of %qD follows "
2238 "declaration with attribute noinline", newdecl);
2239 else if (DECL_DECLARED_INLINE_P (olddecl)
2240 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2241 warned |= warning (OPT_Wattributes,
2242 "declaration of %q+D with attribute "
2243 "noinline follows inline declaration ", newdecl);
2244 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
2245 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
2246 warned |= warning (OPT_Wattributes,
2247 "declaration of %q+D with attribute "
2248 "%qs follows declaration with attribute %qs",
2249 newdecl, "noinline", "always_inline");
2250 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
2251 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2252 warned |= warning (OPT_Wattributes,
2253 "declaration of %q+D with attribute "
2254 "%qs follows declaration with attribute %qs",
2255 newdecl, "always_inline", "noinline");
2256 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
2257 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
2258 warned |= warning (OPT_Wattributes,
2259 "declaration of %q+D with attribute %qs follows "
2260 "declaration with attribute %qs", newdecl, "cold",
2261 "hot");
2262 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
2263 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
2264 warned |= warning (OPT_Wattributes,
2265 "declaration of %q+D with attribute %qs follows "
2266 "declaration with attribute %qs", newdecl, "hot",
2267 "cold");
2269 else /* PARM_DECL, VAR_DECL */
2271 /* Redeclaration of a parameter is a constraint violation (this is
2272 not explicitly stated, but follows from C99 6.7p3 [no more than
2273 one declaration of the same identifier with no linkage in the
2274 same scope, except type tags] and 6.2.2p6 [parameters have no
2275 linkage]). We must check for a forward parameter declaration,
2276 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2277 an extension, the mandatory diagnostic for which is handled by
2278 mark_forward_parm_decls. */
2280 if (TREE_CODE (newdecl) == PARM_DECL
2281 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2283 error ("redefinition of parameter %q+D", newdecl);
2284 locate_old_decl (olddecl);
2285 return false;
2289 /* Optional warning for completely redundant decls. */
2290 if (!warned && !pedwarned
2291 && warn_redundant_decls
2292 /* Don't warn about a function declaration followed by a
2293 definition. */
2294 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2295 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2296 /* Don't warn about redundant redeclarations of builtins. */
2297 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2298 && !DECL_BUILT_IN (newdecl)
2299 && DECL_BUILT_IN (olddecl)
2300 && !C_DECL_DECLARED_BUILTIN (olddecl))
2301 /* Don't warn about an extern followed by a definition. */
2302 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2303 /* Don't warn about forward parameter decls. */
2304 && !(TREE_CODE (newdecl) == PARM_DECL
2305 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2306 /* Don't warn about a variable definition following a declaration. */
2307 && !(VAR_P (newdecl)
2308 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2310 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2311 newdecl);
2314 /* Report location of previous decl/defn. */
2315 if (warned || pedwarned)
2316 locate_old_decl (olddecl);
2318 #undef DECL_EXTERN_INLINE
2320 return retval;
2323 /* Subroutine of duplicate_decls. NEWDECL has been found to be
2324 consistent with OLDDECL, but carries new information. Merge the
2325 new information into OLDDECL. This function issues no
2326 diagnostics. */
2328 static void
2329 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2331 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2332 && DECL_INITIAL (newdecl) != 0);
2333 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2334 && prototype_p (TREE_TYPE (newdecl)));
2335 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2336 && prototype_p (TREE_TYPE (olddecl)));
2338 /* For real parm decl following a forward decl, rechain the old decl
2339 in its new location and clear TREE_ASM_WRITTEN (it's not a
2340 forward decl anymore). */
2341 if (TREE_CODE (newdecl) == PARM_DECL
2342 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2344 struct c_binding *b, **here;
2346 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2347 if ((*here)->decl == olddecl)
2348 goto found;
2349 gcc_unreachable ();
2351 found:
2352 b = *here;
2353 *here = b->prev;
2354 b->prev = current_scope->bindings;
2355 current_scope->bindings = b;
2357 TREE_ASM_WRITTEN (olddecl) = 0;
2360 DECL_ATTRIBUTES (newdecl)
2361 = targetm.merge_decl_attributes (olddecl, newdecl);
2363 /* Merge the data types specified in the two decls. */
2364 TREE_TYPE (newdecl)
2365 = TREE_TYPE (olddecl)
2366 = composite_type (newtype, oldtype);
2368 /* Lay the type out, unless already done. */
2369 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2371 if (TREE_TYPE (newdecl) != error_mark_node)
2372 layout_type (TREE_TYPE (newdecl));
2373 if (TREE_CODE (newdecl) != FUNCTION_DECL
2374 && TREE_CODE (newdecl) != TYPE_DECL
2375 && TREE_CODE (newdecl) != CONST_DECL)
2376 layout_decl (newdecl, 0);
2378 else
2380 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2381 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2382 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2383 DECL_MODE (newdecl) = DECL_MODE (olddecl);
2384 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2386 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2387 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2391 /* Keep the old rtl since we can safely use it. */
2392 if (HAS_RTL_P (olddecl))
2393 COPY_DECL_RTL (olddecl, newdecl);
2395 /* Merge the type qualifiers. */
2396 if (TREE_READONLY (newdecl))
2397 TREE_READONLY (olddecl) = 1;
2399 if (TREE_THIS_VOLATILE (newdecl))
2400 TREE_THIS_VOLATILE (olddecl) = 1;
2402 /* Merge deprecatedness. */
2403 if (TREE_DEPRECATED (newdecl))
2404 TREE_DEPRECATED (olddecl) = 1;
2406 /* If a decl is in a system header and the other isn't, keep the one on the
2407 system header. Otherwise, keep source location of definition rather than
2408 declaration and of prototype rather than non-prototype unless that
2409 prototype is built-in. */
2410 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2411 && DECL_IN_SYSTEM_HEADER (olddecl)
2412 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2413 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2414 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2415 && DECL_IN_SYSTEM_HEADER (newdecl)
2416 && !DECL_IN_SYSTEM_HEADER (olddecl))
2417 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2418 else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2419 || (old_is_prototype && !new_is_prototype
2420 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2421 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2423 /* Merge the initialization information. */
2424 if (DECL_INITIAL (newdecl) == 0)
2425 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2427 /* Merge the threadprivate attribute. */
2428 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2429 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2431 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2433 /* Copy the assembler name.
2434 Currently, it can only be defined in the prototype. */
2435 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2437 /* Use visibility of whichever declaration had it specified */
2438 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2440 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2441 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2444 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2446 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2447 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2448 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2449 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2450 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2451 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2452 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2453 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2454 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2455 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2456 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2459 /* Merge the storage class information. */
2460 merge_weak (newdecl, olddecl);
2462 /* For functions, static overrides non-static. */
2463 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2465 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2466 /* This is since we don't automatically
2467 copy the attributes of NEWDECL into OLDDECL. */
2468 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2469 /* If this clears `static', clear it in the identifier too. */
2470 if (!TREE_PUBLIC (olddecl))
2471 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2475 /* In c99, 'extern' declaration before (or after) 'inline' means this
2476 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2477 is present. */
2478 if (TREE_CODE (newdecl) == FUNCTION_DECL
2479 && !flag_gnu89_inline
2480 && (DECL_DECLARED_INLINE_P (newdecl)
2481 || DECL_DECLARED_INLINE_P (olddecl))
2482 && (!DECL_DECLARED_INLINE_P (newdecl)
2483 || !DECL_DECLARED_INLINE_P (olddecl)
2484 || !DECL_EXTERNAL (olddecl))
2485 && DECL_EXTERNAL (newdecl)
2486 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2487 && !current_function_decl)
2488 DECL_EXTERNAL (newdecl) = 0;
2490 /* An inline definition following a static declaration is not
2491 DECL_EXTERNAL. */
2492 if (new_is_definition
2493 && (DECL_DECLARED_INLINE_P (newdecl)
2494 || DECL_DECLARED_INLINE_P (olddecl))
2495 && !TREE_PUBLIC (olddecl))
2496 DECL_EXTERNAL (newdecl) = 0;
2498 if (DECL_EXTERNAL (newdecl))
2500 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2501 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2503 /* An extern decl does not override previous storage class. */
2504 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2505 if (!DECL_EXTERNAL (newdecl))
2507 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2508 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2511 else
2513 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2514 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2517 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2519 /* If we're redefining a function previously defined as extern
2520 inline, make sure we emit debug info for the inline before we
2521 throw it away, in case it was inlined into a function that
2522 hasn't been written out yet. */
2523 if (new_is_definition && DECL_INITIAL (olddecl))
2524 /* The new defn must not be inline. */
2525 DECL_UNINLINABLE (newdecl) = 1;
2526 else
2528 /* If either decl says `inline', this fn is inline, unless
2529 its definition was passed already. */
2530 if (DECL_DECLARED_INLINE_P (newdecl)
2531 || DECL_DECLARED_INLINE_P (olddecl))
2532 DECL_DECLARED_INLINE_P (newdecl) = 1;
2534 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2535 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2537 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2538 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2539 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2540 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2543 if (DECL_BUILT_IN (olddecl))
2545 /* If redeclaring a builtin function, it stays built in.
2546 But it gets tagged as having been declared. */
2547 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2548 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2549 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2550 if (new_is_prototype)
2552 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2553 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2555 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2556 switch (fncode)
2558 /* If a compatible prototype of these builtin functions
2559 is seen, assume the runtime implements it with the
2560 expected semantics. */
2561 case BUILT_IN_STPCPY:
2562 if (builtin_decl_explicit_p (fncode))
2563 set_builtin_decl_implicit_p (fncode, true);
2564 break;
2565 default:
2566 if (builtin_decl_explicit_p (fncode))
2567 set_builtin_decl_declared_p (fncode, true);
2568 break;
2572 else
2573 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2574 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2577 /* Preserve function specific target and optimization options */
2578 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2579 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2580 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2581 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2583 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2584 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2585 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2586 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2588 /* Also preserve various other info from the definition. */
2589 if (!new_is_definition)
2591 tree t;
2592 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2593 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2594 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2595 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2596 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2597 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2598 DECL_CONTEXT (t) = newdecl;
2600 /* See if we've got a function to instantiate from. */
2601 if (DECL_SAVED_TREE (olddecl))
2602 DECL_ABSTRACT_ORIGIN (newdecl)
2603 = DECL_ABSTRACT_ORIGIN (olddecl);
2607 /* Merge the USED information. */
2608 if (TREE_USED (olddecl))
2609 TREE_USED (newdecl) = 1;
2610 else if (TREE_USED (newdecl))
2611 TREE_USED (olddecl) = 1;
2612 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2613 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2614 if (DECL_PRESERVE_P (olddecl))
2615 DECL_PRESERVE_P (newdecl) = 1;
2616 else if (DECL_PRESERVE_P (newdecl))
2617 DECL_PRESERVE_P (olddecl) = 1;
2619 /* Merge DECL_COMMON */
2620 if (VAR_P (olddecl) && VAR_P (newdecl)
2621 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2622 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2623 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2625 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2626 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2627 DECL_ARGUMENTS (if appropriate). */
2629 unsigned olddecl_uid = DECL_UID (olddecl);
2630 tree olddecl_context = DECL_CONTEXT (olddecl);
2631 tree olddecl_arguments = NULL;
2632 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2633 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2635 memcpy ((char *) olddecl + sizeof (struct tree_common),
2636 (char *) newdecl + sizeof (struct tree_common),
2637 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2638 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2639 switch (TREE_CODE (olddecl))
2641 case FUNCTION_DECL:
2642 case VAR_DECL:
2644 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2646 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2647 (char *) newdecl + sizeof (struct tree_decl_common),
2648 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2649 olddecl->decl_with_vis.symtab_node = snode;
2651 if ((DECL_EXTERNAL (olddecl)
2652 || TREE_PUBLIC (olddecl)
2653 || TREE_STATIC (olddecl))
2654 && DECL_SECTION_NAME (newdecl) != NULL)
2655 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2657 /* This isn't quite correct for something like
2658 int __thread x attribute ((tls_model ("local-exec")));
2659 extern int __thread x;
2660 as we'll lose the "local-exec" model. */
2661 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2662 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2663 break;
2666 case FIELD_DECL:
2667 case PARM_DECL:
2668 case LABEL_DECL:
2669 case RESULT_DECL:
2670 case CONST_DECL:
2671 case TYPE_DECL:
2672 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2673 (char *) newdecl + sizeof (struct tree_decl_common),
2674 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2675 break;
2677 default:
2679 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2680 (char *) newdecl + sizeof (struct tree_decl_common),
2681 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2683 DECL_UID (olddecl) = olddecl_uid;
2684 DECL_CONTEXT (olddecl) = olddecl_context;
2685 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2686 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2689 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2690 so that encode_section_info has a chance to look at the new decl
2691 flags and attributes. */
2692 if (DECL_RTL_SET_P (olddecl)
2693 && (TREE_CODE (olddecl) == FUNCTION_DECL
2694 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2695 make_decl_rtl (olddecl);
2698 /* Handle when a new declaration NEWDECL has the same name as an old
2699 one OLDDECL in the same binding contour. Prints an error message
2700 if appropriate.
2702 If safely possible, alter OLDDECL to look like NEWDECL, and return
2703 true. Otherwise, return false. */
2705 static bool
2706 duplicate_decls (tree newdecl, tree olddecl)
2708 tree newtype = NULL, oldtype = NULL;
2710 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2712 /* Avoid `unused variable' and other warnings for OLDDECL. */
2713 TREE_NO_WARNING (olddecl) = 1;
2714 return false;
2717 merge_decls (newdecl, olddecl, newtype, oldtype);
2719 /* The NEWDECL will no longer be needed.
2721 Before releasing the node, be sure to remove function from symbol
2722 table that might have been inserted there to record comdat group.
2723 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2724 structure is shared in between NEWDECL and OLDECL. */
2725 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2726 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2727 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2729 struct symtab_node *snode = symtab_node::get (newdecl);
2730 if (snode)
2731 snode->remove ();
2733 ggc_free (newdecl);
2734 return true;
2738 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2739 static void
2740 warn_if_shadowing (tree new_decl)
2742 struct c_binding *b;
2744 /* Shadow warnings wanted? */
2745 if (!warn_shadow
2746 /* No shadow warnings for internally generated vars. */
2747 || DECL_IS_BUILTIN (new_decl)
2748 /* No shadow warnings for vars made for inlining. */
2749 || DECL_FROM_INLINE (new_decl))
2750 return;
2752 /* Is anything being shadowed? Invisible decls do not count. */
2753 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2754 if (b->decl && b->decl != new_decl && !b->invisible
2755 && (b->decl == error_mark_node
2756 || diagnostic_report_warnings_p (global_dc,
2757 DECL_SOURCE_LOCATION (b->decl))))
2759 tree old_decl = b->decl;
2760 bool warned = false;
2762 if (old_decl == error_mark_node)
2764 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2765 "non-variable", new_decl);
2766 break;
2768 else if (TREE_CODE (old_decl) == PARM_DECL)
2769 warned = warning (OPT_Wshadow,
2770 "declaration of %q+D shadows a parameter",
2771 new_decl);
2772 else if (DECL_FILE_SCOPE_P (old_decl))
2774 /* Do not warn if a variable shadows a function, unless
2775 the variable is a function or a pointer-to-function. */
2776 if (TREE_CODE (old_decl) == FUNCTION_DECL
2777 && TREE_CODE (new_decl) != FUNCTION_DECL
2778 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2779 continue;
2781 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2782 "declaration of %qD shadows a global "
2783 "declaration",
2784 new_decl);
2786 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2787 && DECL_BUILT_IN (old_decl))
2789 warning (OPT_Wshadow, "declaration of %q+D shadows "
2790 "a built-in function", new_decl);
2791 break;
2793 else
2794 warned = warning (OPT_Wshadow, "declaration of %q+D shadows a "
2795 "previous local", new_decl);
2797 if (warned)
2798 inform (DECL_SOURCE_LOCATION (old_decl),
2799 "shadowed declaration is here");
2801 break;
2805 /* Record a decl-node X as belonging to the current lexical scope.
2806 Check for errors (such as an incompatible declaration for the same
2807 name already seen in the same scope).
2809 Returns either X or an old decl for the same name.
2810 If an old decl is returned, it may have been smashed
2811 to agree with what X says. */
2813 tree
2814 pushdecl (tree x)
2816 tree name = DECL_NAME (x);
2817 struct c_scope *scope = current_scope;
2818 struct c_binding *b;
2819 bool nested = false;
2820 location_t locus = DECL_SOURCE_LOCATION (x);
2822 /* Must set DECL_CONTEXT for everything not at file scope or
2823 DECL_FILE_SCOPE_P won't work. Local externs don't count
2824 unless they have initializers (which generate code). */
2825 if (current_function_decl
2826 && (!VAR_OR_FUNCTION_DECL_P (x)
2827 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2828 DECL_CONTEXT (x) = current_function_decl;
2830 /* Anonymous decls are just inserted in the scope. */
2831 if (!name)
2833 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2834 locus);
2835 return x;
2838 /* First, see if there is another declaration with the same name in
2839 the current scope. If there is, duplicate_decls may do all the
2840 work for us. If duplicate_decls returns false, that indicates
2841 two incompatible decls in the same scope; we are to silently
2842 replace the old one (duplicate_decls has issued all appropriate
2843 diagnostics). In particular, we should not consider possible
2844 duplicates in the external scope, or shadowing. */
2845 b = I_SYMBOL_BINDING (name);
2846 if (b && B_IN_SCOPE (b, scope))
2848 struct c_binding *b_ext, *b_use;
2849 tree type = TREE_TYPE (x);
2850 tree visdecl = b->decl;
2851 tree vistype = TREE_TYPE (visdecl);
2852 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2853 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2854 b->inner_comp = false;
2855 b_use = b;
2856 b_ext = b;
2857 /* If this is an external linkage declaration, we should check
2858 for compatibility with the type in the external scope before
2859 setting the type at this scope based on the visible
2860 information only. */
2861 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2863 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2864 b_ext = b_ext->shadowed;
2865 if (b_ext)
2867 b_use = b_ext;
2868 if (b_use->u.type)
2869 TREE_TYPE (b_use->decl) = b_use->u.type;
2872 if (duplicate_decls (x, b_use->decl))
2874 if (b_use != b)
2876 /* Save the updated type in the external scope and
2877 restore the proper type for this scope. */
2878 tree thistype;
2879 if (comptypes (vistype, type))
2880 thistype = composite_type (vistype, type);
2881 else
2882 thistype = TREE_TYPE (b_use->decl);
2883 b_use->u.type = TREE_TYPE (b_use->decl);
2884 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2885 && DECL_BUILT_IN (b_use->decl))
2886 thistype
2887 = build_type_attribute_variant (thistype,
2888 TYPE_ATTRIBUTES
2889 (b_use->u.type));
2890 TREE_TYPE (b_use->decl) = thistype;
2892 return b_use->decl;
2894 else
2895 goto skip_external_and_shadow_checks;
2898 /* All declarations with external linkage, and all external
2899 references, go in the external scope, no matter what scope is
2900 current. However, the binding in that scope is ignored for
2901 purposes of normal name lookup. A separate binding structure is
2902 created in the requested scope; this governs the normal
2903 visibility of the symbol.
2905 The binding in the externals scope is used exclusively for
2906 detecting duplicate declarations of the same object, no matter
2907 what scope they are in; this is what we do here. (C99 6.2.7p2:
2908 All declarations that refer to the same object or function shall
2909 have compatible type; otherwise, the behavior is undefined.) */
2910 if (DECL_EXTERNAL (x) || scope == file_scope)
2912 tree type = TREE_TYPE (x);
2913 tree vistype = 0;
2914 tree visdecl = 0;
2915 bool type_saved = false;
2916 if (b && !B_IN_EXTERNAL_SCOPE (b)
2917 && VAR_OR_FUNCTION_DECL_P (b->decl)
2918 && DECL_FILE_SCOPE_P (b->decl))
2920 visdecl = b->decl;
2921 vistype = TREE_TYPE (visdecl);
2923 if (scope != file_scope
2924 && !DECL_IN_SYSTEM_HEADER (x))
2925 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2927 while (b && !B_IN_EXTERNAL_SCOPE (b))
2929 /* If this decl might be modified, save its type. This is
2930 done here rather than when the decl is first bound
2931 because the type may change after first binding, through
2932 being completed or through attributes being added. If we
2933 encounter multiple such decls, only the first should have
2934 its type saved; the others will already have had their
2935 proper types saved and the types will not have changed as
2936 their scopes will not have been re-entered. */
2937 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2939 b->u.type = TREE_TYPE (b->decl);
2940 type_saved = true;
2942 if (B_IN_FILE_SCOPE (b)
2943 && VAR_P (b->decl)
2944 && TREE_STATIC (b->decl)
2945 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2946 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2947 && TREE_CODE (type) == ARRAY_TYPE
2948 && TYPE_DOMAIN (type)
2949 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2950 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2952 /* Array type completed in inner scope, which should be
2953 diagnosed if the completion does not have size 1 and
2954 it does not get completed in the file scope. */
2955 b->inner_comp = true;
2957 b = b->shadowed;
2960 /* If a matching external declaration has been found, set its
2961 type to the composite of all the types of that declaration.
2962 After the consistency checks, it will be reset to the
2963 composite of the visible types only. */
2964 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2965 && b->u.type)
2966 TREE_TYPE (b->decl) = b->u.type;
2968 /* The point of the same_translation_unit_p check here is,
2969 we want to detect a duplicate decl for a construct like
2970 foo() { extern bar(); } ... static bar(); but not if
2971 they are in different translation units. In any case,
2972 the static does not go in the externals scope. */
2973 if (b
2974 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2975 && duplicate_decls (x, b->decl))
2977 tree thistype;
2978 if (vistype)
2980 if (comptypes (vistype, type))
2981 thistype = composite_type (vistype, type);
2982 else
2983 thistype = TREE_TYPE (b->decl);
2985 else
2986 thistype = type;
2987 b->u.type = TREE_TYPE (b->decl);
2988 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2989 thistype
2990 = build_type_attribute_variant (thistype,
2991 TYPE_ATTRIBUTES (b->u.type));
2992 TREE_TYPE (b->decl) = thistype;
2993 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2994 locus);
2995 return b->decl;
2997 else if (TREE_PUBLIC (x))
2999 if (visdecl && !b && duplicate_decls (x, visdecl))
3001 /* An external declaration at block scope referring to a
3002 visible entity with internal linkage. The composite
3003 type will already be correct for this scope, so we
3004 just need to fall through to make the declaration in
3005 this scope. */
3006 nested = true;
3007 x = visdecl;
3009 else
3011 bind (name, x, external_scope, /*invisible=*/true,
3012 /*nested=*/false, locus);
3013 nested = true;
3018 if (TREE_CODE (x) != PARM_DECL)
3019 warn_if_shadowing (x);
3021 skip_external_and_shadow_checks:
3022 if (TREE_CODE (x) == TYPE_DECL)
3024 /* So this is a typedef, set its underlying type. */
3025 set_underlying_type (x);
3027 /* If X is a typedef defined in the current function, record it
3028 for the purpose of implementing the -Wunused-local-typedefs
3029 warning. */
3030 record_locally_defined_typedef (x);
3033 bind (name, x, scope, /*invisible=*/false, nested, locus);
3035 /* If x's type is incomplete because it's based on a
3036 structure or union which has not yet been fully declared,
3037 attach it to that structure or union type, so we can go
3038 back and complete the variable declaration later, if the
3039 structure or union gets fully declared.
3041 If the input is erroneous, we can have error_mark in the type
3042 slot (e.g. "f(void a, ...)") - that doesn't count as an
3043 incomplete type. */
3044 if (TREE_TYPE (x) != error_mark_node
3045 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3047 tree element = TREE_TYPE (x);
3049 while (TREE_CODE (element) == ARRAY_TYPE)
3050 element = TREE_TYPE (element);
3051 element = TYPE_MAIN_VARIANT (element);
3053 if ((TREE_CODE (element) == RECORD_TYPE
3054 || TREE_CODE (element) == UNION_TYPE)
3055 && (TREE_CODE (x) != TYPE_DECL
3056 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3057 && !COMPLETE_TYPE_P (element))
3058 C_TYPE_INCOMPLETE_VARS (element)
3059 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3061 return x;
3064 /* Record X as belonging to file scope.
3065 This is used only internally by the Objective-C front end,
3066 and is limited to its needs. duplicate_decls is not called;
3067 if there is any preexisting decl for this identifier, it is an ICE. */
3069 tree
3070 pushdecl_top_level (tree x)
3072 tree name;
3073 bool nested = false;
3074 gcc_assert (VAR_P (x) || TREE_CODE (x) == CONST_DECL);
3076 name = DECL_NAME (x);
3078 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
3080 if (TREE_PUBLIC (x))
3082 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
3083 UNKNOWN_LOCATION);
3084 nested = true;
3086 if (file_scope)
3087 bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
3089 return x;
3092 static void
3093 implicit_decl_warning (location_t loc, tree id, tree olddecl)
3095 if (warn_implicit_function_declaration)
3097 bool warned;
3099 if (flag_isoc99)
3100 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3101 "implicit declaration of function %qE", id);
3102 else
3103 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3104 G_("implicit declaration of function %qE"), id);
3105 if (olddecl && warned)
3106 locate_old_decl (olddecl);
3110 /* This function represents mapping of a function code FCODE
3111 to its respective header. */
3113 static const char *
3114 header_for_builtin_fn (enum built_in_function fcode)
3116 switch (fcode)
3118 CASE_FLT_FN (BUILT_IN_ACOS):
3119 CASE_FLT_FN (BUILT_IN_ACOSH):
3120 CASE_FLT_FN (BUILT_IN_ASIN):
3121 CASE_FLT_FN (BUILT_IN_ASINH):
3122 CASE_FLT_FN (BUILT_IN_ATAN):
3123 CASE_FLT_FN (BUILT_IN_ATANH):
3124 CASE_FLT_FN (BUILT_IN_ATAN2):
3125 CASE_FLT_FN (BUILT_IN_CBRT):
3126 CASE_FLT_FN (BUILT_IN_CEIL):
3127 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3128 CASE_FLT_FN (BUILT_IN_COS):
3129 CASE_FLT_FN (BUILT_IN_COSH):
3130 CASE_FLT_FN (BUILT_IN_ERF):
3131 CASE_FLT_FN (BUILT_IN_ERFC):
3132 CASE_FLT_FN (BUILT_IN_EXP):
3133 CASE_FLT_FN (BUILT_IN_EXP2):
3134 CASE_FLT_FN (BUILT_IN_EXPM1):
3135 CASE_FLT_FN (BUILT_IN_FABS):
3136 CASE_FLT_FN (BUILT_IN_FDIM):
3137 CASE_FLT_FN (BUILT_IN_FLOOR):
3138 CASE_FLT_FN (BUILT_IN_FMA):
3139 CASE_FLT_FN (BUILT_IN_FMAX):
3140 CASE_FLT_FN (BUILT_IN_FMIN):
3141 CASE_FLT_FN (BUILT_IN_FMOD):
3142 CASE_FLT_FN (BUILT_IN_FREXP):
3143 CASE_FLT_FN (BUILT_IN_HYPOT):
3144 CASE_FLT_FN (BUILT_IN_ILOGB):
3145 CASE_FLT_FN (BUILT_IN_LDEXP):
3146 CASE_FLT_FN (BUILT_IN_LGAMMA):
3147 CASE_FLT_FN (BUILT_IN_LLRINT):
3148 CASE_FLT_FN (BUILT_IN_LLROUND):
3149 CASE_FLT_FN (BUILT_IN_LOG):
3150 CASE_FLT_FN (BUILT_IN_LOG10):
3151 CASE_FLT_FN (BUILT_IN_LOG1P):
3152 CASE_FLT_FN (BUILT_IN_LOG2):
3153 CASE_FLT_FN (BUILT_IN_LOGB):
3154 CASE_FLT_FN (BUILT_IN_LRINT):
3155 CASE_FLT_FN (BUILT_IN_LROUND):
3156 CASE_FLT_FN (BUILT_IN_MODF):
3157 CASE_FLT_FN (BUILT_IN_NAN):
3158 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3159 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3160 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3161 CASE_FLT_FN (BUILT_IN_POW):
3162 CASE_FLT_FN (BUILT_IN_REMAINDER):
3163 CASE_FLT_FN (BUILT_IN_REMQUO):
3164 CASE_FLT_FN (BUILT_IN_RINT):
3165 CASE_FLT_FN (BUILT_IN_ROUND):
3166 CASE_FLT_FN (BUILT_IN_SCALBLN):
3167 CASE_FLT_FN (BUILT_IN_SCALBN):
3168 CASE_FLT_FN (BUILT_IN_SIN):
3169 CASE_FLT_FN (BUILT_IN_SINH):
3170 CASE_FLT_FN (BUILT_IN_SINCOS):
3171 CASE_FLT_FN (BUILT_IN_SQRT):
3172 CASE_FLT_FN (BUILT_IN_TAN):
3173 CASE_FLT_FN (BUILT_IN_TANH):
3174 CASE_FLT_FN (BUILT_IN_TGAMMA):
3175 CASE_FLT_FN (BUILT_IN_TRUNC):
3176 case BUILT_IN_ISINF:
3177 case BUILT_IN_ISNAN:
3178 return "<math.h>";
3179 CASE_FLT_FN (BUILT_IN_CABS):
3180 CASE_FLT_FN (BUILT_IN_CACOS):
3181 CASE_FLT_FN (BUILT_IN_CACOSH):
3182 CASE_FLT_FN (BUILT_IN_CARG):
3183 CASE_FLT_FN (BUILT_IN_CASIN):
3184 CASE_FLT_FN (BUILT_IN_CASINH):
3185 CASE_FLT_FN (BUILT_IN_CATAN):
3186 CASE_FLT_FN (BUILT_IN_CATANH):
3187 CASE_FLT_FN (BUILT_IN_CCOS):
3188 CASE_FLT_FN (BUILT_IN_CCOSH):
3189 CASE_FLT_FN (BUILT_IN_CEXP):
3190 CASE_FLT_FN (BUILT_IN_CIMAG):
3191 CASE_FLT_FN (BUILT_IN_CLOG):
3192 CASE_FLT_FN (BUILT_IN_CONJ):
3193 CASE_FLT_FN (BUILT_IN_CPOW):
3194 CASE_FLT_FN (BUILT_IN_CPROJ):
3195 CASE_FLT_FN (BUILT_IN_CREAL):
3196 CASE_FLT_FN (BUILT_IN_CSIN):
3197 CASE_FLT_FN (BUILT_IN_CSINH):
3198 CASE_FLT_FN (BUILT_IN_CSQRT):
3199 CASE_FLT_FN (BUILT_IN_CTAN):
3200 CASE_FLT_FN (BUILT_IN_CTANH):
3201 return "<complex.h>";
3202 case BUILT_IN_MEMCHR:
3203 case BUILT_IN_MEMCMP:
3204 case BUILT_IN_MEMCPY:
3205 case BUILT_IN_MEMMOVE:
3206 case BUILT_IN_MEMSET:
3207 case BUILT_IN_STRCAT:
3208 case BUILT_IN_STRCHR:
3209 case BUILT_IN_STRCMP:
3210 case BUILT_IN_STRCPY:
3211 case BUILT_IN_STRCSPN:
3212 case BUILT_IN_STRLEN:
3213 case BUILT_IN_STRNCAT:
3214 case BUILT_IN_STRNCMP:
3215 case BUILT_IN_STRNCPY:
3216 case BUILT_IN_STRPBRK:
3217 case BUILT_IN_STRRCHR:
3218 case BUILT_IN_STRSPN:
3219 case BUILT_IN_STRSTR:
3220 return "<string.h>";
3221 case BUILT_IN_FPRINTF:
3222 case BUILT_IN_PUTC:
3223 case BUILT_IN_FPUTC:
3224 case BUILT_IN_FPUTS:
3225 case BUILT_IN_FSCANF:
3226 case BUILT_IN_FWRITE:
3227 case BUILT_IN_PRINTF:
3228 case BUILT_IN_PUTCHAR:
3229 case BUILT_IN_PUTS:
3230 case BUILT_IN_SCANF:
3231 case BUILT_IN_SNPRINTF:
3232 case BUILT_IN_SPRINTF:
3233 case BUILT_IN_SSCANF:
3234 case BUILT_IN_VFPRINTF:
3235 case BUILT_IN_VFSCANF:
3236 case BUILT_IN_VPRINTF:
3237 case BUILT_IN_VSCANF:
3238 case BUILT_IN_VSNPRINTF:
3239 case BUILT_IN_VSPRINTF:
3240 case BUILT_IN_VSSCANF:
3241 return "<stdio.h>";
3242 case BUILT_IN_ISALNUM:
3243 case BUILT_IN_ISALPHA:
3244 case BUILT_IN_ISBLANK:
3245 case BUILT_IN_ISCNTRL:
3246 case BUILT_IN_ISDIGIT:
3247 case BUILT_IN_ISGRAPH:
3248 case BUILT_IN_ISLOWER:
3249 case BUILT_IN_ISPRINT:
3250 case BUILT_IN_ISPUNCT:
3251 case BUILT_IN_ISSPACE:
3252 case BUILT_IN_ISUPPER:
3253 case BUILT_IN_ISXDIGIT:
3254 case BUILT_IN_TOLOWER:
3255 case BUILT_IN_TOUPPER:
3256 return "<ctype.h>";
3257 case BUILT_IN_ISWALNUM:
3258 case BUILT_IN_ISWALPHA:
3259 case BUILT_IN_ISWBLANK:
3260 case BUILT_IN_ISWCNTRL:
3261 case BUILT_IN_ISWDIGIT:
3262 case BUILT_IN_ISWGRAPH:
3263 case BUILT_IN_ISWLOWER:
3264 case BUILT_IN_ISWPRINT:
3265 case BUILT_IN_ISWPUNCT:
3266 case BUILT_IN_ISWSPACE:
3267 case BUILT_IN_ISWUPPER:
3268 case BUILT_IN_ISWXDIGIT:
3269 case BUILT_IN_TOWLOWER:
3270 case BUILT_IN_TOWUPPER:
3271 return "<wctype.h>";
3272 case BUILT_IN_ABORT:
3273 case BUILT_IN_ABS:
3274 case BUILT_IN_CALLOC:
3275 case BUILT_IN_EXIT:
3276 case BUILT_IN_FREE:
3277 case BUILT_IN_LABS:
3278 case BUILT_IN_LLABS:
3279 case BUILT_IN_MALLOC:
3280 case BUILT_IN_REALLOC:
3281 case BUILT_IN__EXIT2:
3282 case BUILT_IN_ALIGNED_ALLOC:
3283 return "<stdlib.h>";
3284 case BUILT_IN_IMAXABS:
3285 return "<inttypes.h>";
3286 case BUILT_IN_STRFTIME:
3287 return "<time.h>";
3288 default:
3289 return NULL;
3293 /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3294 function of type int (). */
3296 tree
3297 implicitly_declare (location_t loc, tree functionid)
3299 struct c_binding *b;
3300 tree decl = 0;
3301 tree asmspec_tree;
3303 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3305 if (B_IN_SCOPE (b, external_scope))
3307 decl = b->decl;
3308 break;
3312 if (decl)
3314 if (decl == error_mark_node)
3315 return decl;
3317 /* FIXME: Objective-C has weird not-really-builtin functions
3318 which are supposed to be visible automatically. They wind up
3319 in the external scope because they're pushed before the file
3320 scope gets created. Catch this here and rebind them into the
3321 file scope. */
3322 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3324 bind (functionid, decl, file_scope,
3325 /*invisible=*/false, /*nested=*/true,
3326 DECL_SOURCE_LOCATION (decl));
3327 return decl;
3329 else
3331 tree newtype = default_function_type;
3332 if (b->u.type)
3333 TREE_TYPE (decl) = b->u.type;
3334 /* Implicit declaration of a function already declared
3335 (somehow) in a different scope, or as a built-in.
3336 If this is the first time this has happened, warn;
3337 then recycle the old declaration but with the new type. */
3338 if (!C_DECL_IMPLICIT (decl))
3340 implicit_decl_warning (loc, functionid, decl);
3341 C_DECL_IMPLICIT (decl) = 1;
3343 if (DECL_BUILT_IN (decl))
3345 newtype = build_type_attribute_variant (newtype,
3346 TYPE_ATTRIBUTES
3347 (TREE_TYPE (decl)));
3348 if (!comptypes (newtype, TREE_TYPE (decl)))
3350 bool warned = warning_at (loc, 0, "incompatible implicit "
3351 "declaration of built-in "
3352 "function %qD", decl);
3353 /* See if we can hint which header to include. */
3354 const char *header
3355 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3356 if (header != NULL && warned)
3357 inform (loc, "include %qs or provide a declaration of %qD",
3358 header, decl);
3359 newtype = TREE_TYPE (decl);
3362 else
3364 if (!comptypes (newtype, TREE_TYPE (decl)))
3366 error_at (loc, "incompatible implicit declaration of "
3367 "function %qD", decl);
3368 locate_old_decl (decl);
3371 b->u.type = TREE_TYPE (decl);
3372 TREE_TYPE (decl) = newtype;
3373 bind (functionid, decl, current_scope,
3374 /*invisible=*/false, /*nested=*/true,
3375 DECL_SOURCE_LOCATION (decl));
3376 return decl;
3380 /* Not seen before. */
3381 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3382 DECL_EXTERNAL (decl) = 1;
3383 TREE_PUBLIC (decl) = 1;
3384 C_DECL_IMPLICIT (decl) = 1;
3385 implicit_decl_warning (loc, functionid, 0);
3386 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3387 if (asmspec_tree)
3388 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3390 /* C89 says implicit declarations are in the innermost block.
3391 So we record the decl in the standard fashion. */
3392 decl = pushdecl (decl);
3394 /* No need to call objc_check_decl here - it's a function type. */
3395 rest_of_decl_compilation (decl, 0, 0);
3397 /* Write a record describing this implicit function declaration
3398 to the prototypes file (if requested). */
3399 gen_aux_info_record (decl, 0, 1, 0);
3401 /* Possibly apply some default attributes to this implicit declaration. */
3402 decl_attributes (&decl, NULL_TREE, 0);
3404 return decl;
3407 /* Issue an error message for a reference to an undeclared variable
3408 ID, including a reference to a builtin outside of function-call
3409 context. Establish a binding of the identifier to error_mark_node
3410 in an appropriate scope, which will suppress further errors for the
3411 same identifier. The error message should be given location LOC. */
3412 void
3413 undeclared_variable (location_t loc, tree id)
3415 static bool already = false;
3416 struct c_scope *scope;
3418 if (current_function_decl == 0)
3420 error_at (loc, "%qE undeclared here (not in a function)", id);
3421 scope = current_scope;
3423 else
3425 if (!objc_diagnose_private_ivar (id))
3426 error_at (loc, "%qE undeclared (first use in this function)", id);
3427 if (!already)
3429 inform (loc, "each undeclared identifier is reported only"
3430 " once for each function it appears in");
3431 already = true;
3434 /* If we are parsing old-style parameter decls, current_function_decl
3435 will be nonnull but current_function_scope will be null. */
3436 scope = current_function_scope ? current_function_scope : current_scope;
3438 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3439 UNKNOWN_LOCATION);
3442 /* Subroutine of lookup_label, declare_label, define_label: construct a
3443 LABEL_DECL with all the proper frills. Also create a struct
3444 c_label_vars initialized for the current scope. */
3446 static tree
3447 make_label (location_t location, tree name, bool defining,
3448 struct c_label_vars **p_label_vars)
3450 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3451 DECL_CONTEXT (label) = current_function_decl;
3452 DECL_MODE (label) = VOIDmode;
3454 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3455 label_vars->shadowed = NULL;
3456 set_spot_bindings (&label_vars->label_bindings, defining);
3457 label_vars->decls_in_scope = make_tree_vector ();
3458 label_vars->gotos = NULL;
3459 *p_label_vars = label_vars;
3461 return label;
3464 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3465 Create one if none exists so far for the current function.
3466 This is called when a label is used in a goto expression or
3467 has its address taken. */
3469 tree
3470 lookup_label (tree name)
3472 tree label;
3473 struct c_label_vars *label_vars;
3475 if (current_function_scope == 0)
3477 error ("label %qE referenced outside of any function", name);
3478 return 0;
3481 /* Use a label already defined or ref'd with this name, but not if
3482 it is inherited from a containing function and wasn't declared
3483 using __label__. */
3484 label = I_LABEL_DECL (name);
3485 if (label && (DECL_CONTEXT (label) == current_function_decl
3486 || C_DECLARED_LABEL_FLAG (label)))
3488 /* If the label has only been declared, update its apparent
3489 location to point here, for better diagnostics if it
3490 turns out not to have been defined. */
3491 if (DECL_INITIAL (label) == NULL_TREE)
3492 DECL_SOURCE_LOCATION (label) = input_location;
3493 return label;
3496 /* No label binding for that identifier; make one. */
3497 label = make_label (input_location, name, false, &label_vars);
3499 /* Ordinary labels go in the current function scope. */
3500 bind_label (name, label, current_function_scope, label_vars);
3502 return label;
3505 /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3506 to LABEL. */
3508 static void
3509 warn_about_goto (location_t goto_loc, tree label, tree decl)
3511 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3512 error_at (goto_loc,
3513 "jump into scope of identifier with variably modified type");
3514 else
3515 warning_at (goto_loc, OPT_Wjump_misses_init,
3516 "jump skips variable initialization");
3517 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3518 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3521 /* Look up a label because of a goto statement. This is like
3522 lookup_label, but also issues any appropriate warnings. */
3524 tree
3525 lookup_label_for_goto (location_t loc, tree name)
3527 tree label;
3528 struct c_label_vars *label_vars;
3529 unsigned int ix;
3530 tree decl;
3532 label = lookup_label (name);
3533 if (label == NULL_TREE)
3534 return NULL_TREE;
3536 /* If we are jumping to a different function, we can't issue any
3537 useful warnings. */
3538 if (DECL_CONTEXT (label) != current_function_decl)
3540 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3541 return label;
3544 label_vars = I_LABEL_BINDING (name)->u.label;
3546 /* If the label has not yet been defined, then push this goto on a
3547 list for possible later warnings. */
3548 if (label_vars->label_bindings.scope == NULL)
3550 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3552 g->loc = loc;
3553 set_spot_bindings (&g->goto_bindings, true);
3554 vec_safe_push (label_vars->gotos, g);
3555 return label;
3558 /* If there are any decls in label_vars->decls_in_scope, then this
3559 goto has missed the declaration of the decl. This happens for a
3560 case like
3561 int i = 1;
3562 lab:
3564 goto lab;
3565 Issue a warning or error. */
3566 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3567 warn_about_goto (loc, label, decl);
3569 if (label_vars->label_bindings.left_stmt_expr)
3571 error_at (loc, "jump into statement expression");
3572 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3575 return label;
3578 /* Make a label named NAME in the current function, shadowing silently
3579 any that may be inherited from containing functions or containing
3580 scopes. This is called for __label__ declarations. */
3582 tree
3583 declare_label (tree name)
3585 struct c_binding *b = I_LABEL_BINDING (name);
3586 tree label;
3587 struct c_label_vars *label_vars;
3589 /* Check to make sure that the label hasn't already been declared
3590 at this scope */
3591 if (b && B_IN_CURRENT_SCOPE (b))
3593 error ("duplicate label declaration %qE", name);
3594 locate_old_decl (b->decl);
3596 /* Just use the previous declaration. */
3597 return b->decl;
3600 label = make_label (input_location, name, false, &label_vars);
3601 C_DECLARED_LABEL_FLAG (label) = 1;
3603 /* Declared labels go in the current scope. */
3604 bind_label (name, label, current_scope, label_vars);
3606 return label;
3609 /* When we define a label, issue any appropriate warnings if there are
3610 any gotos earlier in the function which jump to this label. */
3612 static void
3613 check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3615 unsigned int ix;
3616 struct c_goto_bindings *g;
3618 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3620 struct c_binding *b;
3621 struct c_scope *scope;
3623 /* We have a goto to this label. The goto is going forward. In
3624 g->scope, the goto is going to skip any binding which was
3625 defined after g->bindings_in_scope. */
3626 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3628 for (b = g->goto_bindings.scope->bindings;
3629 b != g->goto_bindings.bindings_in_scope;
3630 b = b->prev)
3632 if (decl_jump_unsafe (b->decl))
3633 warn_about_goto (g->loc, label, b->decl);
3637 /* We also need to warn about decls defined in any scopes
3638 between the scope of the label and the scope of the goto. */
3639 for (scope = label_vars->label_bindings.scope;
3640 scope != g->goto_bindings.scope;
3641 scope = scope->outer)
3643 gcc_assert (scope != NULL);
3644 if (scope->has_jump_unsafe_decl)
3646 if (scope == label_vars->label_bindings.scope)
3647 b = label_vars->label_bindings.bindings_in_scope;
3648 else
3649 b = scope->bindings;
3650 for (; b != NULL; b = b->prev)
3652 if (decl_jump_unsafe (b->decl))
3653 warn_about_goto (g->loc, label, b->decl);
3658 if (g->goto_bindings.stmt_exprs > 0)
3660 error_at (g->loc, "jump into statement expression");
3661 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3662 label);
3666 /* Now that the label is defined, we will issue warnings about
3667 subsequent gotos to this label when we see them. */
3668 vec_safe_truncate (label_vars->gotos, 0);
3669 label_vars->gotos = NULL;
3672 /* Define a label, specifying the location in the source file.
3673 Return the LABEL_DECL node for the label, if the definition is valid.
3674 Otherwise return 0. */
3676 tree
3677 define_label (location_t location, tree name)
3679 /* Find any preexisting label with this name. It is an error
3680 if that label has already been defined in this function, or
3681 if there is a containing function with a declared label with
3682 the same name. */
3683 tree label = I_LABEL_DECL (name);
3685 if (label
3686 && ((DECL_CONTEXT (label) == current_function_decl
3687 && DECL_INITIAL (label) != 0)
3688 || (DECL_CONTEXT (label) != current_function_decl
3689 && C_DECLARED_LABEL_FLAG (label))))
3691 error_at (location, "duplicate label %qD", label);
3692 locate_old_decl (label);
3693 return 0;
3695 else if (label && DECL_CONTEXT (label) == current_function_decl)
3697 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3699 /* The label has been used or declared already in this function,
3700 but not defined. Update its location to point to this
3701 definition. */
3702 DECL_SOURCE_LOCATION (label) = location;
3703 set_spot_bindings (&label_vars->label_bindings, true);
3705 /* Issue warnings as required about any goto statements from
3706 earlier in the function. */
3707 check_earlier_gotos (label, label_vars);
3709 else
3711 struct c_label_vars *label_vars;
3713 /* No label binding for that identifier; make one. */
3714 label = make_label (location, name, true, &label_vars);
3716 /* Ordinary labels go in the current function scope. */
3717 bind_label (name, label, current_function_scope, label_vars);
3720 if (!in_system_header_at (input_location) && lookup_name (name))
3721 warning_at (location, OPT_Wtraditional,
3722 "traditional C lacks a separate namespace "
3723 "for labels, identifier %qE conflicts", name);
3725 /* Mark label as having been defined. */
3726 DECL_INITIAL (label) = error_mark_node;
3727 return label;
3730 /* Get the bindings for a new switch statement. This is used to issue
3731 warnings as appropriate for jumps from the switch to case or
3732 default labels. */
3734 struct c_spot_bindings *
3735 c_get_switch_bindings (void)
3737 struct c_spot_bindings *switch_bindings;
3739 switch_bindings = XNEW (struct c_spot_bindings);
3740 set_spot_bindings (switch_bindings, true);
3741 return switch_bindings;
3744 void
3745 c_release_switch_bindings (struct c_spot_bindings *bindings)
3747 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3748 XDELETE (bindings);
3751 /* This is called at the point of a case or default label to issue
3752 warnings about decls as needed. It returns true if it found an
3753 error, not just a warning. */
3755 bool
3756 c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3757 location_t switch_loc, location_t case_loc)
3759 bool saw_error;
3760 struct c_scope *scope;
3762 saw_error = false;
3763 for (scope = current_scope;
3764 scope != switch_bindings->scope;
3765 scope = scope->outer)
3767 struct c_binding *b;
3769 gcc_assert (scope != NULL);
3771 if (!scope->has_jump_unsafe_decl)
3772 continue;
3774 for (b = scope->bindings; b != NULL; b = b->prev)
3776 if (decl_jump_unsafe (b->decl))
3778 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3780 saw_error = true;
3781 error_at (case_loc,
3782 ("switch jumps into scope of identifier with "
3783 "variably modified type"));
3785 else
3786 warning_at (case_loc, OPT_Wjump_misses_init,
3787 "switch jumps over variable initialization");
3788 inform (switch_loc, "switch starts here");
3789 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3790 b->decl);
3795 if (switch_bindings->stmt_exprs > 0)
3797 saw_error = true;
3798 error_at (case_loc, "switch jumps into statement expression");
3799 inform (switch_loc, "switch starts here");
3802 return saw_error;
3805 /* Given NAME, an IDENTIFIER_NODE,
3806 return the structure (or union or enum) definition for that name.
3807 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3808 CODE says which kind of type the caller wants;
3809 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3810 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3811 location where the tag was defined.
3812 If the wrong kind of type is found, an error is reported. */
3814 static tree
3815 lookup_tag (enum tree_code code, tree name, int thislevel_only,
3816 location_t *ploc)
3818 struct c_binding *b = I_TAG_BINDING (name);
3819 int thislevel = 0;
3821 if (!b || !b->decl)
3822 return 0;
3824 /* We only care about whether it's in this level if
3825 thislevel_only was set or it might be a type clash. */
3826 if (thislevel_only || TREE_CODE (b->decl) != code)
3828 /* For our purposes, a tag in the external scope is the same as
3829 a tag in the file scope. (Primarily relevant to Objective-C
3830 and its builtin structure tags, which get pushed before the
3831 file scope is created.) */
3832 if (B_IN_CURRENT_SCOPE (b)
3833 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3834 thislevel = 1;
3837 if (thislevel_only && !thislevel)
3838 return 0;
3840 if (TREE_CODE (b->decl) != code)
3842 /* Definition isn't the kind we were looking for. */
3843 pending_invalid_xref = name;
3844 pending_invalid_xref_location = input_location;
3846 /* If in the same binding level as a declaration as a tag
3847 of a different type, this must not be allowed to
3848 shadow that tag, so give the error immediately.
3849 (For example, "struct foo; union foo;" is invalid.) */
3850 if (thislevel)
3851 pending_xref_error ();
3854 if (ploc != NULL)
3855 *ploc = b->locus;
3857 return b->decl;
3860 /* Print an error message now
3861 for a recent invalid struct, union or enum cross reference.
3862 We don't print them immediately because they are not invalid
3863 when used in the `struct foo;' construct for shadowing. */
3865 void
3866 pending_xref_error (void)
3868 if (pending_invalid_xref != 0)
3869 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3870 pending_invalid_xref);
3871 pending_invalid_xref = 0;
3875 /* Look up NAME in the current scope and its superiors
3876 in the namespace of variables, functions and typedefs.
3877 Return a ..._DECL node of some kind representing its definition,
3878 or return 0 if it is undefined. */
3880 tree
3881 lookup_name (tree name)
3883 struct c_binding *b = I_SYMBOL_BINDING (name);
3884 if (b && !b->invisible)
3886 maybe_record_typedef_use (b->decl);
3887 return b->decl;
3889 return 0;
3892 /* Similar to `lookup_name' but look only at the indicated scope. */
3894 static tree
3895 lookup_name_in_scope (tree name, struct c_scope *scope)
3897 struct c_binding *b;
3899 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3900 if (B_IN_SCOPE (b, scope))
3901 return b->decl;
3902 return 0;
3905 /* Create the predefined scalar types of C,
3906 and some nodes representing standard constants (0, 1, (void *) 0).
3907 Initialize the global scope.
3908 Make definitions for built-in primitive functions. */
3910 void
3911 c_init_decl_processing (void)
3913 location_t save_loc = input_location;
3915 /* Initialize reserved words for parser. */
3916 c_parse_init ();
3918 current_function_decl = 0;
3920 gcc_obstack_init (&parser_obstack);
3922 /* Make the externals scope. */
3923 push_scope ();
3924 external_scope = current_scope;
3926 /* Declarations from c_common_nodes_and_builtins must not be associated
3927 with this input file, lest we get differences between using and not
3928 using preprocessed headers. */
3929 input_location = BUILTINS_LOCATION;
3931 c_common_nodes_and_builtins ();
3933 /* In C, comparisons and TRUTH_* expressions have type int. */
3934 truthvalue_type_node = integer_type_node;
3935 truthvalue_true_node = integer_one_node;
3936 truthvalue_false_node = integer_zero_node;
3938 /* Even in C99, which has a real boolean type. */
3939 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3940 boolean_type_node));
3942 input_location = save_loc;
3944 make_fname_decl = c_make_fname_decl;
3945 start_fname_decls ();
3948 /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3949 give the decl, NAME is the initialization string and TYPE_DEP
3950 indicates whether NAME depended on the type of the function. As we
3951 don't yet implement delayed emission of static data, we mark the
3952 decl as emitted so it is not placed in the output. Anything using
3953 it must therefore pull out the STRING_CST initializer directly.
3954 FIXME. */
3956 static tree
3957 c_make_fname_decl (location_t loc, tree id, int type_dep)
3959 const char *name = fname_as_string (type_dep);
3960 tree decl, type, init;
3961 size_t length = strlen (name);
3963 type = build_array_type (char_type_node,
3964 build_index_type (size_int (length)));
3965 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3967 decl = build_decl (loc, VAR_DECL, id, type);
3969 TREE_STATIC (decl) = 1;
3970 TREE_READONLY (decl) = 1;
3971 DECL_ARTIFICIAL (decl) = 1;
3973 init = build_string (length + 1, name);
3974 free (CONST_CAST (char *, name));
3975 TREE_TYPE (init) = type;
3976 DECL_INITIAL (decl) = init;
3978 TREE_USED (decl) = 1;
3980 if (current_function_decl
3981 /* For invalid programs like this:
3983 void foo()
3984 const char* p = __FUNCTION__;
3986 the __FUNCTION__ is believed to appear in K&R style function
3987 parameter declarator. In that case we still don't have
3988 function_scope. */
3989 && (!seen_error () || current_function_scope))
3991 DECL_CONTEXT (decl) = current_function_decl;
3992 bind (id, decl, current_function_scope,
3993 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3996 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3998 return decl;
4001 tree
4002 c_builtin_function (tree decl)
4004 tree type = TREE_TYPE (decl);
4005 tree id = DECL_NAME (decl);
4007 const char *name = IDENTIFIER_POINTER (id);
4008 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4010 /* Should never be called on a symbol with a preexisting meaning. */
4011 gcc_assert (!I_SYMBOL_BINDING (id));
4013 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4014 UNKNOWN_LOCATION);
4016 /* Builtins in the implementation namespace are made visible without
4017 needing to be explicitly declared. See push_file_scope. */
4018 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4020 DECL_CHAIN (decl) = visible_builtins;
4021 visible_builtins = decl;
4024 return decl;
4027 tree
4028 c_builtin_function_ext_scope (tree decl)
4030 tree type = TREE_TYPE (decl);
4031 tree id = DECL_NAME (decl);
4033 const char *name = IDENTIFIER_POINTER (id);
4034 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4036 if (external_scope)
4037 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4038 UNKNOWN_LOCATION);
4040 /* Builtins in the implementation namespace are made visible without
4041 needing to be explicitly declared. See push_file_scope. */
4042 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4044 DECL_CHAIN (decl) = visible_builtins;
4045 visible_builtins = decl;
4048 return decl;
4051 /* Called when a declaration is seen that contains no names to declare.
4052 If its type is a reference to a structure, union or enum inherited
4053 from a containing scope, shadow that tag name for the current scope
4054 with a forward reference.
4055 If its type defines a new named structure or union
4056 or defines an enum, it is valid but we need not do anything here.
4057 Otherwise, it is an error. */
4059 void
4060 shadow_tag (const struct c_declspecs *declspecs)
4062 shadow_tag_warned (declspecs, 0);
4065 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4066 but no pedwarn. */
4067 void
4068 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4070 bool found_tag = false;
4072 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4074 tree value = declspecs->type;
4075 enum tree_code code = TREE_CODE (value);
4077 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4078 /* Used to test also that TYPE_SIZE (value) != 0.
4079 That caused warning for `struct foo;' at top level in the file. */
4081 tree name = TYPE_NAME (value);
4082 tree t;
4084 found_tag = true;
4086 if (declspecs->restrict_p)
4088 error ("invalid use of %<restrict%>");
4089 warned = 1;
4092 if (name == 0)
4094 if (warned != 1 && code != ENUMERAL_TYPE)
4095 /* Empty unnamed enum OK */
4097 pedwarn (input_location, 0,
4098 "unnamed struct/union that defines no instances");
4099 warned = 1;
4102 else if (declspecs->typespec_kind != ctsk_tagdef
4103 && declspecs->typespec_kind != ctsk_tagfirstref
4104 && declspecs->storage_class != csc_none)
4106 if (warned != 1)
4107 pedwarn (input_location, 0,
4108 "empty declaration with storage class specifier "
4109 "does not redeclare tag");
4110 warned = 1;
4111 pending_xref_error ();
4113 else if (declspecs->typespec_kind != ctsk_tagdef
4114 && declspecs->typespec_kind != ctsk_tagfirstref
4115 && (declspecs->const_p
4116 || declspecs->volatile_p
4117 || declspecs->atomic_p
4118 || declspecs->restrict_p
4119 || declspecs->address_space))
4121 if (warned != 1)
4122 pedwarn (input_location, 0,
4123 "empty declaration with type qualifier "
4124 "does not redeclare tag");
4125 warned = 1;
4126 pending_xref_error ();
4128 else if (declspecs->typespec_kind != ctsk_tagdef
4129 && declspecs->typespec_kind != ctsk_tagfirstref
4130 && declspecs->alignas_p)
4132 if (warned != 1)
4133 pedwarn (input_location, 0,
4134 "empty declaration with %<_Alignas%> "
4135 "does not redeclare tag");
4136 warned = 1;
4137 pending_xref_error ();
4139 else
4141 pending_invalid_xref = 0;
4142 t = lookup_tag (code, name, 1, NULL);
4144 if (t == 0)
4146 t = make_node (code);
4147 pushtag (input_location, name, t);
4151 else
4153 if (warned != 1 && !in_system_header_at (input_location))
4155 pedwarn (input_location, 0,
4156 "useless type name in empty declaration");
4157 warned = 1;
4161 else if (warned != 1 && !in_system_header_at (input_location)
4162 && declspecs->typedef_p)
4164 pedwarn (input_location, 0, "useless type name in empty declaration");
4165 warned = 1;
4168 pending_invalid_xref = 0;
4170 if (declspecs->inline_p)
4172 error ("%<inline%> in empty declaration");
4173 warned = 1;
4176 if (declspecs->noreturn_p)
4178 error ("%<_Noreturn%> in empty declaration");
4179 warned = 1;
4182 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4184 error ("%<auto%> in file-scope empty declaration");
4185 warned = 1;
4188 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4190 error ("%<register%> in file-scope empty declaration");
4191 warned = 1;
4194 if (!warned && !in_system_header_at (input_location)
4195 && declspecs->storage_class != csc_none)
4197 warning (0, "useless storage class specifier in empty declaration");
4198 warned = 2;
4201 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4203 warning (0, "useless %qs in empty declaration",
4204 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4205 warned = 2;
4208 if (!warned
4209 && !in_system_header_at (input_location)
4210 && (declspecs->const_p
4211 || declspecs->volatile_p
4212 || declspecs->atomic_p
4213 || declspecs->restrict_p
4214 || declspecs->address_space))
4216 warning (0, "useless type qualifier in empty declaration");
4217 warned = 2;
4220 if (!warned && !in_system_header_at (input_location)
4221 && declspecs->alignas_p)
4223 warning (0, "useless %<_Alignas%> in empty declaration");
4224 warned = 2;
4227 if (warned != 1)
4229 if (!found_tag)
4230 pedwarn (input_location, 0, "empty declaration");
4235 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4236 bits. SPECS represents declaration specifiers that the grammar
4237 only permits to contain type qualifiers and attributes. */
4240 quals_from_declspecs (const struct c_declspecs *specs)
4242 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4243 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4244 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4245 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4246 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4247 gcc_assert (!specs->type
4248 && !specs->decl_attr
4249 && specs->typespec_word == cts_none
4250 && specs->storage_class == csc_none
4251 && !specs->typedef_p
4252 && !specs->explicit_signed_p
4253 && !specs->deprecated_p
4254 && !specs->long_p
4255 && !specs->long_long_p
4256 && !specs->short_p
4257 && !specs->signed_p
4258 && !specs->unsigned_p
4259 && !specs->complex_p
4260 && !specs->inline_p
4261 && !specs->noreturn_p
4262 && !specs->thread_p);
4263 return quals;
4266 /* Construct an array declarator. LOC is the location of the
4267 beginning of the array (usually the opening brace). EXPR is the
4268 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4269 inside the [] (to be applied to the pointer to which a parameter
4270 array is converted). STATIC_P is true if "static" is inside the
4271 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4272 VLA of unspecified length which is nevertheless a complete type,
4273 false otherwise. The field for the contained declarator is left to
4274 be filled in by set_array_declarator_inner. */
4276 struct c_declarator *
4277 build_array_declarator (location_t loc,
4278 tree expr, struct c_declspecs *quals, bool static_p,
4279 bool vla_unspec_p)
4281 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4282 struct c_declarator);
4283 declarator->id_loc = loc;
4284 declarator->kind = cdk_array;
4285 declarator->declarator = 0;
4286 declarator->u.array.dimen = expr;
4287 if (quals)
4289 declarator->u.array.attrs = quals->attrs;
4290 declarator->u.array.quals = quals_from_declspecs (quals);
4292 else
4294 declarator->u.array.attrs = NULL_TREE;
4295 declarator->u.array.quals = 0;
4297 declarator->u.array.static_p = static_p;
4298 declarator->u.array.vla_unspec_p = vla_unspec_p;
4299 if (static_p || quals != NULL)
4300 pedwarn_c90 (loc, OPT_Wpedantic,
4301 "ISO C90 does not support %<static%> or type "
4302 "qualifiers in parameter array declarators");
4303 if (vla_unspec_p)
4304 pedwarn_c90 (loc, OPT_Wpedantic,
4305 "ISO C90 does not support %<[*]%> array declarators");
4306 if (vla_unspec_p)
4308 if (!current_scope->parm_flag)
4310 /* C99 6.7.5.2p4 */
4311 error_at (loc, "%<[*]%> not allowed in other than "
4312 "function prototype scope");
4313 declarator->u.array.vla_unspec_p = false;
4314 return NULL;
4316 current_scope->had_vla_unspec = true;
4318 return declarator;
4321 /* Set the contained declarator of an array declarator. DECL is the
4322 declarator, as constructed by build_array_declarator; INNER is what
4323 appears on the left of the []. */
4325 struct c_declarator *
4326 set_array_declarator_inner (struct c_declarator *decl,
4327 struct c_declarator *inner)
4329 decl->declarator = inner;
4330 return decl;
4333 /* INIT is a constructor that forms DECL's initializer. If the final
4334 element initializes a flexible array field, add the size of that
4335 initializer to DECL's size. */
4337 static void
4338 add_flexible_array_elts_to_size (tree decl, tree init)
4340 tree elt, type;
4342 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4343 return;
4345 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4346 type = TREE_TYPE (elt);
4347 if (TREE_CODE (type) == ARRAY_TYPE
4348 && TYPE_SIZE (type) == NULL_TREE
4349 && TYPE_DOMAIN (type) != NULL_TREE
4350 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4352 complete_array_type (&type, elt, false);
4353 DECL_SIZE (decl)
4354 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4355 DECL_SIZE_UNIT (decl)
4356 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4360 /* Decode a "typename", such as "int **", returning a ..._TYPE node.
4361 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4362 before the type name, and set *EXPR_CONST_OPERANDS, if
4363 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4364 appear in a constant expression. */
4366 tree
4367 groktypename (struct c_type_name *type_name, tree *expr,
4368 bool *expr_const_operands)
4370 tree type;
4371 tree attrs = type_name->specs->attrs;
4373 type_name->specs->attrs = NULL_TREE;
4375 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4376 false, NULL, &attrs, expr, expr_const_operands,
4377 DEPRECATED_NORMAL);
4379 /* Apply attributes. */
4380 decl_attributes (&type, attrs, 0);
4382 return type;
4385 /* Wrapper for decl_attributes that adds some implicit attributes
4386 to VAR_DECLs or FUNCTION_DECLs. */
4388 static tree
4389 c_decl_attributes (tree *node, tree attributes, int flags)
4391 /* Add implicit "omp declare target" attribute if requested. */
4392 if (current_omp_declare_target_attribute
4393 && ((VAR_P (*node) && is_global_var (*node))
4394 || TREE_CODE (*node) == FUNCTION_DECL))
4396 if (VAR_P (*node)
4397 && ((DECL_CONTEXT (*node)
4398 && TREE_CODE (DECL_CONTEXT (*node)) == FUNCTION_DECL)
4399 || (current_function_decl && !DECL_EXTERNAL (*node))))
4400 error ("%q+D in block scope inside of declare target directive",
4401 *node);
4402 else if (VAR_P (*node)
4403 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (*node)))
4404 error ("%q+D in declare target directive does not have mappable type",
4405 *node);
4406 else
4407 attributes = tree_cons (get_identifier ("omp declare target"),
4408 NULL_TREE, attributes);
4410 return decl_attributes (node, attributes, flags);
4414 /* Decode a declarator in an ordinary declaration or data definition.
4415 This is called as soon as the type information and variable name
4416 have been parsed, before parsing the initializer if any.
4417 Here we create the ..._DECL node, fill in its type,
4418 and put it on the list of decls for the current context.
4419 The ..._DECL node is returned as the value.
4421 Exception: for arrays where the length is not specified,
4422 the type is left null, to be filled in by `finish_decl'.
4424 Function definitions do not come here; they go to start_function
4425 instead. However, external and forward declarations of functions
4426 do go through here. Structure field declarations are done by
4427 grokfield and not through here. */
4429 tree
4430 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
4431 bool initialized, tree attributes)
4433 tree decl;
4434 tree tem;
4435 tree expr = NULL_TREE;
4436 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
4438 /* An object declared as __attribute__((deprecated)) suppresses
4439 warnings of uses of other deprecated items. */
4440 if (lookup_attribute ("deprecated", attributes))
4441 deprecated_state = DEPRECATED_SUPPRESS;
4443 decl = grokdeclarator (declarator, declspecs,
4444 NORMAL, initialized, NULL, &attributes, &expr, NULL,
4445 deprecated_state);
4446 if (!decl || decl == error_mark_node)
4447 return NULL_TREE;
4449 if (expr)
4450 add_stmt (fold_convert (void_type_node, expr));
4452 if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4453 warning (OPT_Wmain, "%q+D is usually a function", decl);
4455 if (initialized)
4456 /* Is it valid for this decl to have an initializer at all?
4457 If not, set INITIALIZED to zero, which will indirectly
4458 tell 'finish_decl' to ignore the initializer once it is parsed. */
4459 switch (TREE_CODE (decl))
4461 case TYPE_DECL:
4462 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4463 initialized = 0;
4464 break;
4466 case FUNCTION_DECL:
4467 error ("function %qD is initialized like a variable", decl);
4468 initialized = 0;
4469 break;
4471 case PARM_DECL:
4472 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4473 error ("parameter %qD is initialized", decl);
4474 initialized = 0;
4475 break;
4477 default:
4478 /* Don't allow initializations for incomplete types except for
4479 arrays which might be completed by the initialization. */
4481 /* This can happen if the array size is an undefined macro.
4482 We already gave a warning, so we don't need another one. */
4483 if (TREE_TYPE (decl) == error_mark_node)
4484 initialized = 0;
4485 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4487 /* A complete type is ok if size is fixed. */
4489 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4490 || C_DECL_VARIABLE_SIZE (decl))
4492 error ("variable-sized object may not be initialized");
4493 initialized = 0;
4496 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4498 error ("variable %qD has initializer but incomplete type", decl);
4499 initialized = 0;
4501 else if (C_DECL_VARIABLE_SIZE (decl))
4503 /* Although C99 is unclear about whether incomplete arrays
4504 of VLAs themselves count as VLAs, it does not make
4505 sense to permit them to be initialized given that
4506 ordinary VLAs may not be initialized. */
4507 error ("variable-sized object may not be initialized");
4508 initialized = 0;
4512 if (initialized)
4514 if (current_scope == file_scope)
4515 TREE_STATIC (decl) = 1;
4517 /* Tell 'pushdecl' this is an initialized decl
4518 even though we don't yet have the initializer expression.
4519 Also tell 'finish_decl' it may store the real initializer. */
4520 DECL_INITIAL (decl) = error_mark_node;
4523 /* If this is a function declaration, write a record describing it to the
4524 prototypes file (if requested). */
4526 if (TREE_CODE (decl) == FUNCTION_DECL)
4527 gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4529 /* ANSI specifies that a tentative definition which is not merged with
4530 a non-tentative definition behaves exactly like a definition with an
4531 initializer equal to zero. (Section 3.7.2)
4533 -fno-common gives strict ANSI behavior, though this tends to break
4534 a large body of code that grew up without this rule.
4536 Thread-local variables are never common, since there's no entrenched
4537 body of code to break, and it allows more efficient variable references
4538 in the presence of dynamic linking. */
4540 if (VAR_P (decl)
4541 && !initialized
4542 && TREE_PUBLIC (decl)
4543 && !DECL_THREAD_LOCAL_P (decl)
4544 && !flag_no_common)
4545 DECL_COMMON (decl) = 1;
4547 /* Set attributes here so if duplicate decl, will have proper attributes. */
4548 c_decl_attributes (&decl, attributes, 0);
4550 /* Handle gnu_inline attribute. */
4551 if (declspecs->inline_p
4552 && !flag_gnu89_inline
4553 && TREE_CODE (decl) == FUNCTION_DECL
4554 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4555 || current_function_decl))
4557 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4559 else if (declspecs->storage_class != csc_static)
4560 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4563 if (TREE_CODE (decl) == FUNCTION_DECL
4564 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4566 struct c_declarator *ce = declarator;
4568 if (ce->kind == cdk_pointer)
4569 ce = declarator->declarator;
4570 if (ce->kind == cdk_function)
4572 tree args = ce->u.arg_info->parms;
4573 for (; args; args = DECL_CHAIN (args))
4575 tree type = TREE_TYPE (args);
4576 if (type && INTEGRAL_TYPE_P (type)
4577 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4578 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
4583 if (TREE_CODE (decl) == FUNCTION_DECL
4584 && DECL_DECLARED_INLINE_P (decl)
4585 && DECL_UNINLINABLE (decl)
4586 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4587 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4588 decl);
4590 /* C99 6.7.4p3: An inline definition of a function with external
4591 linkage shall not contain a definition of a modifiable object
4592 with static storage duration... */
4593 if (VAR_P (decl)
4594 && current_scope != file_scope
4595 && TREE_STATIC (decl)
4596 && !TREE_READONLY (decl)
4597 && DECL_DECLARED_INLINE_P (current_function_decl)
4598 && DECL_EXTERNAL (current_function_decl))
4599 record_inline_static (input_location, current_function_decl,
4600 decl, csi_modifiable);
4602 if (c_dialect_objc ()
4603 && VAR_OR_FUNCTION_DECL_P (decl))
4604 objc_check_global_decl (decl);
4606 /* Add this decl to the current scope.
4607 TEM may equal DECL or it may be a previous decl of the same name. */
4608 tem = pushdecl (decl);
4610 if (initialized && DECL_EXTERNAL (tem))
4612 DECL_EXTERNAL (tem) = 0;
4613 TREE_STATIC (tem) = 1;
4616 return tem;
4619 /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4620 DECL or the non-array element type if DECL is an uninitialized array.
4621 If that type has a const member, diagnose this. */
4623 static void
4624 diagnose_uninitialized_cst_member (tree decl, tree type)
4626 tree field;
4627 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4629 tree field_type;
4630 if (TREE_CODE (field) != FIELD_DECL)
4631 continue;
4632 field_type = strip_array_types (TREE_TYPE (field));
4634 if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4636 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4637 "uninitialized const member in %qT is invalid in C++",
4638 strip_array_types (TREE_TYPE (decl)));
4639 inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4642 if (TREE_CODE (field_type) == RECORD_TYPE
4643 || TREE_CODE (field_type) == UNION_TYPE)
4644 diagnose_uninitialized_cst_member (decl, field_type);
4648 /* Finish processing of a declaration;
4649 install its initial value.
4650 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4651 If the length of an array type is not known before,
4652 it must be determined now, from the initial value, or it is an error.
4654 INIT_LOC is the location of the initial value. */
4656 void
4657 finish_decl (tree decl, location_t init_loc, tree init,
4658 tree origtype, tree asmspec_tree)
4660 tree type;
4661 bool was_incomplete = (DECL_SIZE (decl) == 0);
4662 const char *asmspec = 0;
4664 /* If a name was specified, get the string. */
4665 if (VAR_OR_FUNCTION_DECL_P (decl)
4666 && DECL_FILE_SCOPE_P (decl))
4667 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4668 if (asmspec_tree)
4669 asmspec = TREE_STRING_POINTER (asmspec_tree);
4671 if (VAR_P (decl)
4672 && TREE_STATIC (decl)
4673 && global_bindings_p ())
4674 /* So decl is a global variable. Record the types it uses
4675 so that we can decide later to emit debug info for them. */
4676 record_types_used_by_current_var_decl (decl);
4678 /* If `start_decl' didn't like having an initialization, ignore it now. */
4679 if (init != 0 && DECL_INITIAL (decl) == 0)
4680 init = 0;
4682 /* Don't crash if parm is initialized. */
4683 if (TREE_CODE (decl) == PARM_DECL)
4684 init = 0;
4686 if (init)
4687 store_init_value (init_loc, decl, init, origtype);
4689 if (c_dialect_objc () && (VAR_OR_FUNCTION_DECL_P (decl)
4690 || TREE_CODE (decl) == FIELD_DECL))
4691 objc_check_decl (decl);
4693 type = TREE_TYPE (decl);
4695 /* Deduce size of array from initialization, if not already known. */
4696 if (TREE_CODE (type) == ARRAY_TYPE
4697 && TYPE_DOMAIN (type) == 0
4698 && TREE_CODE (decl) != TYPE_DECL)
4700 bool do_default
4701 = (TREE_STATIC (decl)
4702 /* Even if pedantic, an external linkage array
4703 may have incomplete type at first. */
4704 ? pedantic && !TREE_PUBLIC (decl)
4705 : !DECL_EXTERNAL (decl));
4706 int failure
4707 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4708 do_default);
4710 /* Get the completed type made by complete_array_type. */
4711 type = TREE_TYPE (decl);
4713 switch (failure)
4715 case 1:
4716 error ("initializer fails to determine size of %q+D", decl);
4717 break;
4719 case 2:
4720 if (do_default)
4721 error ("array size missing in %q+D", decl);
4722 /* If a `static' var's size isn't known,
4723 make it extern as well as static, so it does not get
4724 allocated.
4725 If it is not `static', then do not mark extern;
4726 finish_incomplete_decl will give it a default size
4727 and it will get allocated. */
4728 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4729 DECL_EXTERNAL (decl) = 1;
4730 break;
4732 case 3:
4733 error ("zero or negative size array %q+D", decl);
4734 break;
4736 case 0:
4737 /* For global variables, update the copy of the type that
4738 exists in the binding. */
4739 if (TREE_PUBLIC (decl))
4741 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4742 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4743 b_ext = b_ext->shadowed;
4744 if (b_ext)
4746 if (b_ext->u.type && comptypes (b_ext->u.type, type))
4747 b_ext->u.type = composite_type (b_ext->u.type, type);
4748 else
4749 b_ext->u.type = type;
4752 break;
4754 default:
4755 gcc_unreachable ();
4758 if (DECL_INITIAL (decl))
4759 TREE_TYPE (DECL_INITIAL (decl)) = type;
4761 relayout_decl (decl);
4764 if (VAR_P (decl))
4766 if (init && TREE_CODE (init) == CONSTRUCTOR)
4767 add_flexible_array_elts_to_size (decl, init);
4769 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4770 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4771 layout_decl (decl, 0);
4773 if (DECL_SIZE (decl) == 0
4774 /* Don't give an error if we already gave one earlier. */
4775 && TREE_TYPE (decl) != error_mark_node
4776 && (TREE_STATIC (decl)
4777 /* A static variable with an incomplete type
4778 is an error if it is initialized.
4779 Also if it is not file scope.
4780 Otherwise, let it through, but if it is not `extern'
4781 then it may cause an error message later. */
4782 ? (DECL_INITIAL (decl) != 0
4783 || !DECL_FILE_SCOPE_P (decl))
4784 /* An automatic variable with an incomplete type
4785 is an error. */
4786 : !DECL_EXTERNAL (decl)))
4788 error ("storage size of %q+D isn%'t known", decl);
4789 TREE_TYPE (decl) = error_mark_node;
4792 if (is_global_var (decl) && DECL_SIZE (decl) != 0)
4794 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4795 constant_expression_warning (DECL_SIZE (decl));
4796 else
4798 error ("storage size of %q+D isn%'t constant", decl);
4799 TREE_TYPE (decl) = error_mark_node;
4803 if (TREE_USED (type))
4805 TREE_USED (decl) = 1;
4806 DECL_READ_P (decl) = 1;
4810 /* If this is a function and an assembler name is specified, reset DECL_RTL
4811 so we can give it its new name. Also, update builtin_decl if it
4812 was a normal built-in. */
4813 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4815 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4816 set_builtin_user_assembler_name (decl, asmspec);
4817 set_user_assembler_name (decl, asmspec);
4820 /* If #pragma weak was used, mark the decl weak now. */
4821 maybe_apply_pragma_weak (decl);
4823 /* Output the assembler code and/or RTL code for variables and functions,
4824 unless the type is an undefined structure or union.
4825 If not, it will get done when the type is completed. */
4827 if (VAR_OR_FUNCTION_DECL_P (decl))
4829 /* Determine the ELF visibility. */
4830 if (TREE_PUBLIC (decl))
4831 c_determine_visibility (decl);
4833 /* This is a no-op in c-lang.c or something real in objc-act.c. */
4834 if (c_dialect_objc ())
4835 objc_check_decl (decl);
4837 if (asmspec)
4839 /* If this is not a static variable, issue a warning.
4840 It doesn't make any sense to give an ASMSPEC for an
4841 ordinary, non-register local variable. Historically,
4842 GCC has accepted -- but ignored -- the ASMSPEC in
4843 this case. */
4844 if (!DECL_FILE_SCOPE_P (decl)
4845 && VAR_P (decl)
4846 && !C_DECL_REGISTER (decl)
4847 && !TREE_STATIC (decl))
4848 warning (0, "ignoring asm-specifier for non-static local "
4849 "variable %q+D", decl);
4850 else
4851 set_user_assembler_name (decl, asmspec);
4854 if (DECL_FILE_SCOPE_P (decl))
4856 if (DECL_INITIAL (decl) == NULL_TREE
4857 || DECL_INITIAL (decl) == error_mark_node)
4858 /* Don't output anything
4859 when a tentative file-scope definition is seen.
4860 But at end of compilation, do output code for them. */
4861 DECL_DEFER_OUTPUT (decl) = 1;
4862 if (asmspec && C_DECL_REGISTER (decl))
4863 DECL_HARD_REGISTER (decl) = 1;
4864 rest_of_decl_compilation (decl, true, 0);
4866 else
4868 /* In conjunction with an ASMSPEC, the `register'
4869 keyword indicates that we should place the variable
4870 in a particular register. */
4871 if (asmspec && C_DECL_REGISTER (decl))
4873 DECL_HARD_REGISTER (decl) = 1;
4874 /* This cannot be done for a structure with volatile
4875 fields, on which DECL_REGISTER will have been
4876 reset. */
4877 if (!DECL_REGISTER (decl))
4878 error ("cannot put object with volatile field into register");
4881 if (TREE_CODE (decl) != FUNCTION_DECL)
4883 /* If we're building a variable sized type, and we might be
4884 reachable other than via the top of the current binding
4885 level, then create a new BIND_EXPR so that we deallocate
4886 the object at the right time. */
4887 /* Note that DECL_SIZE can be null due to errors. */
4888 if (DECL_SIZE (decl)
4889 && !TREE_CONSTANT (DECL_SIZE (decl))
4890 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4892 tree bind;
4893 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4894 TREE_SIDE_EFFECTS (bind) = 1;
4895 add_stmt (bind);
4896 BIND_EXPR_BODY (bind) = push_stmt_list ();
4898 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4899 DECL_EXPR, decl));
4904 if (!DECL_FILE_SCOPE_P (decl))
4906 /* Recompute the RTL of a local array now
4907 if it used to be an incomplete type. */
4908 if (was_incomplete && !is_global_var (decl))
4910 /* If we used it already as memory, it must stay in memory. */
4911 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4912 /* If it's still incomplete now, no init will save it. */
4913 if (DECL_SIZE (decl) == 0)
4914 DECL_INITIAL (decl) = 0;
4919 if (TREE_CODE (decl) == TYPE_DECL)
4921 if (!DECL_FILE_SCOPE_P (decl)
4922 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4923 add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4925 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4928 /* Install a cleanup (aka destructor) if one was given. */
4929 if (VAR_P (decl) && !TREE_STATIC (decl))
4931 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4932 if (attr)
4934 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4935 tree cleanup_decl = lookup_name (cleanup_id);
4936 tree cleanup;
4937 vec<tree, va_gc> *v;
4939 /* Build "cleanup(&decl)" for the destructor. */
4940 cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4941 vec_alloc (v, 1);
4942 v->quick_push (cleanup);
4943 cleanup = c_build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4944 vNULL, cleanup_decl, v, NULL);
4945 vec_free (v);
4947 /* Don't warn about decl unused; the cleanup uses it. */
4948 TREE_USED (decl) = 1;
4949 TREE_USED (cleanup_decl) = 1;
4950 DECL_READ_P (decl) = 1;
4952 push_cleanup (decl, cleanup, false);
4956 if (warn_cxx_compat
4957 && VAR_P (decl)
4958 && !DECL_EXTERNAL (decl)
4959 && DECL_INITIAL (decl) == NULL_TREE)
4961 type = strip_array_types (type);
4962 if (TREE_READONLY (decl))
4963 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4964 "uninitialized const %qD is invalid in C++", decl);
4965 else if ((TREE_CODE (type) == RECORD_TYPE
4966 || TREE_CODE (type) == UNION_TYPE)
4967 && C_TYPE_FIELDS_READONLY (type))
4968 diagnose_uninitialized_cst_member (decl, type);
4971 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4974 /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4975 EXPR is NULL or a pointer to an expression that needs to be
4976 evaluated for the side effects of array size expressions in the
4977 parameters. */
4979 tree
4980 grokparm (const struct c_parm *parm, tree *expr)
4982 tree attrs = parm->attrs;
4983 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4984 NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4986 decl_attributes (&decl, attrs, 0);
4988 return decl;
4991 /* Given a parsed parameter declaration, decode it into a PARM_DECL
4992 and push that on the current scope. EXPR is a pointer to an
4993 expression that needs to be evaluated for the side effects of array
4994 size expressions in the parameters. */
4996 void
4997 push_parm_decl (const struct c_parm *parm, tree *expr)
4999 tree attrs = parm->attrs;
5000 tree decl;
5002 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
5003 &attrs, expr, NULL, DEPRECATED_NORMAL);
5004 decl_attributes (&decl, attrs, 0);
5006 decl = pushdecl (decl);
5008 finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
5011 /* Mark all the parameter declarations to date as forward decls.
5012 Also diagnose use of this extension. */
5014 void
5015 mark_forward_parm_decls (void)
5017 struct c_binding *b;
5019 if (pedantic && !current_scope->warned_forward_parm_decls)
5021 pedwarn (input_location, OPT_Wpedantic,
5022 "ISO C forbids forward parameter declarations");
5023 current_scope->warned_forward_parm_decls = true;
5026 for (b = current_scope->bindings; b; b = b->prev)
5027 if (TREE_CODE (b->decl) == PARM_DECL)
5028 TREE_ASM_WRITTEN (b->decl) = 1;
5031 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
5032 literal, which may be an incomplete array type completed by the
5033 initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
5034 literal. NON_CONST is true if the initializers contain something
5035 that cannot occur in a constant expression. */
5037 tree
5038 build_compound_literal (location_t loc, tree type, tree init, bool non_const)
5040 /* We do not use start_decl here because we have a type, not a declarator;
5041 and do not use finish_decl because the decl should be stored inside
5042 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
5043 tree decl;
5044 tree complit;
5045 tree stmt;
5047 if (type == error_mark_node
5048 || init == error_mark_node)
5049 return error_mark_node;
5051 decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
5052 DECL_EXTERNAL (decl) = 0;
5053 TREE_PUBLIC (decl) = 0;
5054 TREE_STATIC (decl) = (current_scope == file_scope);
5055 DECL_CONTEXT (decl) = current_function_decl;
5056 TREE_USED (decl) = 1;
5057 DECL_READ_P (decl) = 1;
5058 TREE_TYPE (decl) = type;
5059 TREE_READONLY (decl) = (TYPE_READONLY (type)
5060 || (TREE_CODE (type) == ARRAY_TYPE
5061 && TYPE_READONLY (TREE_TYPE (type))));
5062 store_init_value (loc, decl, init, NULL_TREE);
5064 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
5066 int failure = complete_array_type (&TREE_TYPE (decl),
5067 DECL_INITIAL (decl), true);
5068 /* If complete_array_type returns 3, it means that the
5069 initial value of the compound literal is empty. Allow it. */
5070 gcc_assert (failure == 0 || failure == 3);
5072 type = TREE_TYPE (decl);
5073 TREE_TYPE (DECL_INITIAL (decl)) = type;
5076 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
5078 c_incomplete_type_error (NULL_TREE, type);
5079 return error_mark_node;
5082 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
5083 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
5084 TREE_SIDE_EFFECTS (complit) = 1;
5086 layout_decl (decl, 0);
5088 if (TREE_STATIC (decl))
5090 /* This decl needs a name for the assembler output. */
5091 set_compound_literal_name (decl);
5092 DECL_DEFER_OUTPUT (decl) = 1;
5093 DECL_COMDAT (decl) = 1;
5094 DECL_ARTIFICIAL (decl) = 1;
5095 DECL_IGNORED_P (decl) = 1;
5096 pushdecl (decl);
5097 rest_of_decl_compilation (decl, 1, 0);
5100 if (non_const)
5102 complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
5103 C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
5106 return complit;
5109 /* Check the type of a compound literal. Here we just check that it
5110 is valid for C++. */
5112 void
5113 check_compound_literal_type (location_t loc, struct c_type_name *type_name)
5115 if (warn_cxx_compat
5116 && (type_name->specs->typespec_kind == ctsk_tagdef
5117 || type_name->specs->typespec_kind == ctsk_tagfirstref))
5118 warning_at (loc, OPT_Wc___compat,
5119 "defining a type in a compound literal is invalid in C++");
5122 /* Determine whether TYPE is a structure with a flexible array member,
5123 or a union containing such a structure (possibly recursively). */
5125 static bool
5126 flexible_array_type_p (tree type)
5128 tree x;
5129 switch (TREE_CODE (type))
5131 case RECORD_TYPE:
5132 x = TYPE_FIELDS (type);
5133 if (x == NULL_TREE)
5134 return false;
5135 while (DECL_CHAIN (x) != NULL_TREE)
5136 x = DECL_CHAIN (x);
5137 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5138 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5139 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5140 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5141 return true;
5142 return false;
5143 case UNION_TYPE:
5144 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
5146 if (flexible_array_type_p (TREE_TYPE (x)))
5147 return true;
5149 return false;
5150 default:
5151 return false;
5155 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
5156 replacing with appropriate values if they are invalid. */
5157 static void
5158 check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
5160 tree type_mv;
5161 unsigned int max_width;
5162 unsigned HOST_WIDE_INT w;
5163 const char *name = (orig_name
5164 ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
5165 : _("<anonymous>"));
5167 /* Detect and ignore out of range field width and process valid
5168 field widths. */
5169 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
5171 error ("bit-field %qs width not an integer constant", name);
5172 *width = integer_one_node;
5174 else
5176 if (TREE_CODE (*width) != INTEGER_CST)
5178 *width = c_fully_fold (*width, false, NULL);
5179 if (TREE_CODE (*width) == INTEGER_CST)
5180 pedwarn (input_location, OPT_Wpedantic,
5181 "bit-field %qs width not an integer constant expression",
5182 name);
5184 if (TREE_CODE (*width) != INTEGER_CST)
5186 error ("bit-field %qs width not an integer constant", name);
5187 *width = integer_one_node;
5189 constant_expression_warning (*width);
5190 if (tree_int_cst_sgn (*width) < 0)
5192 error ("negative width in bit-field %qs", name);
5193 *width = integer_one_node;
5195 else if (integer_zerop (*width) && orig_name)
5197 error ("zero width for bit-field %qs", name);
5198 *width = integer_one_node;
5202 /* Detect invalid bit-field type. */
5203 if (TREE_CODE (*type) != INTEGER_TYPE
5204 && TREE_CODE (*type) != BOOLEAN_TYPE
5205 && TREE_CODE (*type) != ENUMERAL_TYPE)
5207 error ("bit-field %qs has invalid type", name);
5208 *type = unsigned_type_node;
5211 type_mv = TYPE_MAIN_VARIANT (*type);
5212 if (!in_system_header_at (input_location)
5213 && type_mv != integer_type_node
5214 && type_mv != unsigned_type_node
5215 && type_mv != boolean_type_node)
5216 pedwarn_c90 (input_location, OPT_Wpedantic,
5217 "type of bit-field %qs is a GCC extension", name);
5219 max_width = TYPE_PRECISION (*type);
5221 if (0 < compare_tree_int (*width, max_width))
5223 error ("width of %qs exceeds its type", name);
5224 w = max_width;
5225 *width = build_int_cst (integer_type_node, w);
5227 else
5228 w = tree_to_uhwi (*width);
5230 if (TREE_CODE (*type) == ENUMERAL_TYPE)
5232 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
5233 if (!lt
5234 || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type))
5235 || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type)))
5236 warning (0, "%qs is narrower than values of its type", name);
5242 /* Print warning about variable length array if necessary. */
5244 static void
5245 warn_variable_length_array (tree name, tree size)
5247 if (TREE_CONSTANT (size))
5249 if (name)
5250 pedwarn_c90 (input_location, OPT_Wvla,
5251 "ISO C90 forbids array %qE whose size "
5252 "can%'t be evaluated", name);
5253 else
5254 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids array "
5255 "whose size can%'t be evaluated");
5257 else
5259 if (name)
5260 pedwarn_c90 (input_location, OPT_Wvla,
5261 "ISO C90 forbids variable length array %qE", name);
5262 else
5263 pedwarn_c90 (input_location, OPT_Wvla, "ISO C90 forbids variable "
5264 "length array");
5268 /* Print warning about defaulting to int if necessary. */
5270 static void
5271 warn_defaults_to (location_t location, int opt, const char *gmsgid, ...)
5273 diagnostic_info diagnostic;
5274 va_list ap;
5276 va_start (ap, gmsgid);
5277 diagnostic_set_info (&diagnostic, gmsgid, &ap, location,
5278 flag_isoc99 ? DK_PEDWARN : DK_WARNING);
5279 diagnostic.option_index = opt;
5280 report_diagnostic (&diagnostic);
5281 va_end (ap);
5284 /* Given declspecs and a declarator,
5285 determine the name and type of the object declared
5286 and construct a ..._DECL node for it.
5287 (In one case we can return a ..._TYPE node instead.
5288 For invalid input we sometimes return 0.)
5290 DECLSPECS is a c_declspecs structure for the declaration specifiers.
5292 DECL_CONTEXT says which syntactic context this declaration is in:
5293 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
5294 FUNCDEF for a function definition. Like NORMAL but a few different
5295 error messages in each case. Return value may be zero meaning
5296 this definition is too screwy to try to parse.
5297 PARM for a parameter declaration (either within a function prototype
5298 or before a function body). Make a PARM_DECL, or return void_type_node.
5299 TYPENAME if for a typename (in a cast or sizeof).
5300 Don't make a DECL node; just return the ..._TYPE node.
5301 FIELD for a struct or union field; make a FIELD_DECL.
5302 INITIALIZED is true if the decl has an initializer.
5303 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
5304 representing the width of the bit-field.
5305 DECL_ATTRS points to the list of attributes that should be added to this
5306 decl. Any nested attributes that belong on the decl itself will be
5307 added to this list.
5308 If EXPR is not NULL, any expressions that need to be evaluated as
5309 part of evaluating variably modified types will be stored in *EXPR.
5310 If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
5311 set to indicate whether operands in *EXPR can be used in constant
5312 expressions.
5313 DEPRECATED_STATE is a deprecated_states value indicating whether
5314 deprecation warnings should be suppressed.
5316 In the TYPENAME case, DECLARATOR is really an absolute declarator.
5317 It may also be so in the PARM case, for a prototype where the
5318 argument type is specified but not the name.
5320 This function is where the complicated C meanings of `static'
5321 and `extern' are interpreted. */
5323 static tree
5324 grokdeclarator (const struct c_declarator *declarator,
5325 struct c_declspecs *declspecs,
5326 enum decl_context decl_context, bool initialized, tree *width,
5327 tree *decl_attrs, tree *expr, bool *expr_const_operands,
5328 enum deprecated_states deprecated_state)
5330 tree type = declspecs->type;
5331 bool threadp = declspecs->thread_p;
5332 enum c_storage_class storage_class = declspecs->storage_class;
5333 int constp;
5334 int restrictp;
5335 int volatilep;
5336 int atomicp;
5337 int type_quals = TYPE_UNQUALIFIED;
5338 tree name = NULL_TREE;
5339 bool funcdef_flag = false;
5340 bool funcdef_syntax = false;
5341 bool size_varies = false;
5342 tree decl_attr = declspecs->decl_attr;
5343 int array_ptr_quals = TYPE_UNQUALIFIED;
5344 tree array_ptr_attrs = NULL_TREE;
5345 int array_parm_static = 0;
5346 bool array_parm_vla_unspec_p = false;
5347 tree returned_attrs = NULL_TREE;
5348 bool bitfield = width != NULL;
5349 tree element_type;
5350 struct c_arg_info *arg_info = 0;
5351 addr_space_t as1, as2, address_space;
5352 location_t loc = UNKNOWN_LOCATION;
5353 const char *errmsg;
5354 tree expr_dummy;
5355 bool expr_const_operands_dummy;
5356 enum c_declarator_kind first_non_attr_kind;
5357 unsigned int alignas_align = 0;
5359 if (TREE_CODE (type) == ERROR_MARK)
5360 return error_mark_node;
5361 if (expr == NULL)
5362 expr = &expr_dummy;
5363 if (expr_const_operands == NULL)
5364 expr_const_operands = &expr_const_operands_dummy;
5366 *expr = declspecs->expr;
5367 *expr_const_operands = declspecs->expr_const_operands;
5369 if (decl_context == FUNCDEF)
5370 funcdef_flag = true, decl_context = NORMAL;
5372 /* Look inside a declarator for the name being declared
5373 and get it as an IDENTIFIER_NODE, for an error message. */
5375 const struct c_declarator *decl = declarator;
5377 first_non_attr_kind = cdk_attrs;
5378 while (decl)
5379 switch (decl->kind)
5381 case cdk_array:
5382 loc = decl->id_loc;
5383 /* FALL THRU. */
5385 case cdk_function:
5386 case cdk_pointer:
5387 funcdef_syntax = (decl->kind == cdk_function);
5388 decl = decl->declarator;
5389 if (first_non_attr_kind == cdk_attrs)
5390 first_non_attr_kind = decl->kind;
5391 break;
5393 case cdk_attrs:
5394 decl = decl->declarator;
5395 break;
5397 case cdk_id:
5398 loc = decl->id_loc;
5399 if (decl->u.id)
5400 name = decl->u.id;
5401 if (first_non_attr_kind == cdk_attrs)
5402 first_non_attr_kind = decl->kind;
5403 decl = 0;
5404 break;
5406 default:
5407 gcc_unreachable ();
5409 if (name == 0)
5411 gcc_assert (decl_context == PARM
5412 || decl_context == TYPENAME
5413 || (decl_context == FIELD
5414 && declarator->kind == cdk_id));
5415 gcc_assert (!initialized);
5419 /* A function definition's declarator must have the form of
5420 a function declarator. */
5422 if (funcdef_flag && !funcdef_syntax)
5423 return 0;
5425 /* If this looks like a function definition, make it one,
5426 even if it occurs where parms are expected.
5427 Then store_parm_decls will reject it and not use it as a parm. */
5428 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
5429 decl_context = PARM;
5431 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
5432 warn_deprecated_use (declspecs->type, declspecs->decl_attr);
5434 if ((decl_context == NORMAL || decl_context == FIELD)
5435 && current_scope == file_scope
5436 && variably_modified_type_p (type, NULL_TREE))
5438 if (name)
5439 error_at (loc, "variably modified %qE at file scope", name);
5440 else
5441 error_at (loc, "variably modified field at file scope");
5442 type = integer_type_node;
5445 size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5447 /* Diagnose defaulting to "int". */
5449 if (declspecs->default_int_p && !in_system_header_at (input_location))
5451 /* Issue a warning if this is an ISO C 99 program or if
5452 -Wreturn-type and this is a function, or if -Wimplicit;
5453 prefer the former warning since it is more explicit. */
5454 if ((warn_implicit_int || warn_return_type || flag_isoc99)
5455 && funcdef_flag)
5456 warn_about_return_type = 1;
5457 else
5459 if (name)
5460 warn_defaults_to (loc, OPT_Wimplicit_int,
5461 "type defaults to %<int%> in declaration "
5462 "of %qE", name);
5463 else
5464 warn_defaults_to (loc, OPT_Wimplicit_int,
5465 "type defaults to %<int%> in type name");
5469 /* Adjust the type if a bit-field is being declared,
5470 -funsigned-bitfields applied and the type is not explicitly
5471 "signed". */
5472 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5473 && TREE_CODE (type) == INTEGER_TYPE)
5474 type = unsigned_type_for (type);
5476 /* Figure out the type qualifiers for the declaration. There are
5477 two ways a declaration can become qualified. One is something
5478 like `const int i' where the `const' is explicit. Another is
5479 something like `typedef const int CI; CI i' where the type of the
5480 declaration contains the `const'. A third possibility is that
5481 there is a type qualifier on the element type of a typedefed
5482 array type, in which case we should extract that qualifier so
5483 that c_apply_type_quals_to_decl receives the full list of
5484 qualifiers to work with (C90 is not entirely clear about whether
5485 duplicate qualifiers should be diagnosed in this case, but it
5486 seems most appropriate to do so). */
5487 element_type = strip_array_types (type);
5488 constp = declspecs->const_p + TYPE_READONLY (element_type);
5489 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5490 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5491 atomicp = declspecs->atomic_p + TYPE_ATOMIC (element_type);
5492 as1 = declspecs->address_space;
5493 as2 = TYPE_ADDR_SPACE (element_type);
5494 address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5496 if (constp > 1)
5497 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<const%>");
5498 if (restrictp > 1)
5499 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<restrict%>");
5500 if (volatilep > 1)
5501 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<volatile%>");
5502 if (atomicp > 1)
5503 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %<_Atomic%>");
5505 if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5506 error_at (loc, "conflicting named address spaces (%s vs %s)",
5507 c_addr_space_name (as1), c_addr_space_name (as2));
5509 if ((TREE_CODE (type) == ARRAY_TYPE
5510 || first_non_attr_kind == cdk_array)
5511 && TYPE_QUALS (element_type))
5512 type = TYPE_MAIN_VARIANT (type);
5513 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5514 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5515 | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5516 | (atomicp ? TYPE_QUAL_ATOMIC : 0)
5517 | ENCODE_QUAL_ADDR_SPACE (address_space));
5519 /* Applying the _Atomic qualifier to an array type (through the use
5520 of typedefs or typeof) must be detected here. If the qualifier
5521 is introduced later, any appearance of applying it to an array is
5522 actually applying it to an element of that array. */
5523 if (atomicp && TREE_CODE (type) == ARRAY_TYPE)
5524 error_at (loc, "%<_Atomic%>-qualified array type");
5526 /* Warn about storage classes that are invalid for certain
5527 kinds of declarations (parameters, typenames, etc.). */
5529 if (funcdef_flag
5530 && (threadp
5531 || storage_class == csc_auto
5532 || storage_class == csc_register
5533 || storage_class == csc_typedef))
5535 if (storage_class == csc_auto)
5536 pedwarn (loc,
5537 (current_scope == file_scope) ? 0 : OPT_Wpedantic,
5538 "function definition declared %<auto%>");
5539 if (storage_class == csc_register)
5540 error_at (loc, "function definition declared %<register%>");
5541 if (storage_class == csc_typedef)
5542 error_at (loc, "function definition declared %<typedef%>");
5543 if (threadp)
5544 error_at (loc, "function definition declared %qs",
5545 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5546 threadp = false;
5547 if (storage_class == csc_auto
5548 || storage_class == csc_register
5549 || storage_class == csc_typedef)
5550 storage_class = csc_none;
5552 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5554 if (decl_context == PARM && storage_class == csc_register)
5556 else
5558 switch (decl_context)
5560 case FIELD:
5561 if (name)
5562 error_at (loc, "storage class specified for structure "
5563 "field %qE", name);
5564 else
5565 error_at (loc, "storage class specified for structure field");
5566 break;
5567 case PARM:
5568 if (name)
5569 error_at (loc, "storage class specified for parameter %qE",
5570 name);
5571 else
5572 error_at (loc, "storage class specified for unnamed parameter");
5573 break;
5574 default:
5575 error_at (loc, "storage class specified for typename");
5576 break;
5578 storage_class = csc_none;
5579 threadp = false;
5582 else if (storage_class == csc_extern
5583 && initialized
5584 && !funcdef_flag)
5586 /* 'extern' with initialization is invalid if not at file scope. */
5587 if (current_scope == file_scope)
5589 /* It is fine to have 'extern const' when compiling at C
5590 and C++ intersection. */
5591 if (!(warn_cxx_compat && constp))
5592 warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5593 name);
5595 else
5596 error_at (loc, "%qE has both %<extern%> and initializer", name);
5598 else if (current_scope == file_scope)
5600 if (storage_class == csc_auto)
5601 error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5602 name);
5603 if (pedantic && storage_class == csc_register)
5604 pedwarn (input_location, OPT_Wpedantic,
5605 "file-scope declaration of %qE specifies %<register%>", name);
5607 else
5609 if (storage_class == csc_extern && funcdef_flag)
5610 error_at (loc, "nested function %qE declared %<extern%>", name);
5611 else if (threadp && storage_class == csc_none)
5613 error_at (loc, "function-scope %qE implicitly auto and declared "
5614 "%qs", name,
5615 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
5616 threadp = false;
5620 /* Now figure out the structure of the declarator proper.
5621 Descend through it, creating more complex types, until we reach
5622 the declared identifier (or NULL_TREE, in an absolute declarator).
5623 At each stage we maintain an unqualified version of the type
5624 together with any qualifiers that should be applied to it with
5625 c_build_qualified_type; this way, array types including
5626 multidimensional array types are first built up in unqualified
5627 form and then the qualified form is created with
5628 TYPE_MAIN_VARIANT pointing to the unqualified form. */
5630 while (declarator && declarator->kind != cdk_id)
5632 if (type == error_mark_node)
5634 declarator = declarator->declarator;
5635 continue;
5638 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5639 a cdk_pointer (for *...),
5640 a cdk_function (for ...(...)),
5641 a cdk_attrs (for nested attributes),
5642 or a cdk_id (for the name being declared
5643 or the place in an absolute declarator
5644 where the name was omitted).
5645 For the last case, we have just exited the loop.
5647 At this point, TYPE is the type of elements of an array,
5648 or for a function to return, or for a pointer to point to.
5649 After this sequence of ifs, TYPE is the type of the
5650 array or function or pointer, and DECLARATOR has had its
5651 outermost layer removed. */
5653 if (array_ptr_quals != TYPE_UNQUALIFIED
5654 || array_ptr_attrs != NULL_TREE
5655 || array_parm_static)
5657 /* Only the innermost declarator (making a parameter be of
5658 array type which is converted to pointer type)
5659 may have static or type qualifiers. */
5660 error_at (loc, "static or type qualifiers in non-parameter array declarator");
5661 array_ptr_quals = TYPE_UNQUALIFIED;
5662 array_ptr_attrs = NULL_TREE;
5663 array_parm_static = 0;
5666 switch (declarator->kind)
5668 case cdk_attrs:
5670 /* A declarator with embedded attributes. */
5671 tree attrs = declarator->u.attrs;
5672 const struct c_declarator *inner_decl;
5673 int attr_flags = 0;
5674 declarator = declarator->declarator;
5675 inner_decl = declarator;
5676 while (inner_decl->kind == cdk_attrs)
5677 inner_decl = inner_decl->declarator;
5678 if (inner_decl->kind == cdk_id)
5679 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5680 else if (inner_decl->kind == cdk_function)
5681 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5682 else if (inner_decl->kind == cdk_array)
5683 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5684 returned_attrs = decl_attributes (&type,
5685 chainon (returned_attrs, attrs),
5686 attr_flags);
5687 break;
5689 case cdk_array:
5691 tree itype = NULL_TREE;
5692 tree size = declarator->u.array.dimen;
5693 /* The index is a signed object `sizetype' bits wide. */
5694 tree index_type = c_common_signed_type (sizetype);
5696 array_ptr_quals = declarator->u.array.quals;
5697 array_ptr_attrs = declarator->u.array.attrs;
5698 array_parm_static = declarator->u.array.static_p;
5699 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5701 declarator = declarator->declarator;
5703 /* Check for some types that there cannot be arrays of. */
5705 if (VOID_TYPE_P (type))
5707 if (name)
5708 error_at (loc, "declaration of %qE as array of voids", name);
5709 else
5710 error_at (loc, "declaration of type name as array of voids");
5711 type = error_mark_node;
5714 if (TREE_CODE (type) == FUNCTION_TYPE)
5716 if (name)
5717 error_at (loc, "declaration of %qE as array of functions",
5718 name);
5719 else
5720 error_at (loc, "declaration of type name as array of "
5721 "functions");
5722 type = error_mark_node;
5725 if (pedantic && !in_system_header_at (input_location)
5726 && flexible_array_type_p (type))
5727 pedwarn (loc, OPT_Wpedantic,
5728 "invalid use of structure with flexible array member");
5730 if (size == error_mark_node)
5731 type = error_mark_node;
5733 if (type == error_mark_node)
5734 continue;
5736 /* If size was specified, set ITYPE to a range-type for
5737 that size. Otherwise, ITYPE remains null. finish_decl
5738 may figure it out from an initial value. */
5740 if (size)
5742 bool size_maybe_const = true;
5743 bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5744 && !TREE_OVERFLOW (size));
5745 bool this_size_varies = false;
5747 /* Strip NON_LVALUE_EXPRs since we aren't using as an
5748 lvalue. */
5749 STRIP_TYPE_NOPS (size);
5751 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5753 if (name)
5754 error_at (loc, "size of array %qE has non-integer type",
5755 name);
5756 else
5757 error_at (loc,
5758 "size of unnamed array has non-integer type");
5759 size = integer_one_node;
5762 size = c_fully_fold (size, false, &size_maybe_const);
5764 if (pedantic && size_maybe_const && integer_zerop (size))
5766 if (name)
5767 pedwarn (loc, OPT_Wpedantic,
5768 "ISO C forbids zero-size array %qE", name);
5769 else
5770 pedwarn (loc, OPT_Wpedantic,
5771 "ISO C forbids zero-size array");
5774 if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5776 constant_expression_warning (size);
5777 if (tree_int_cst_sgn (size) < 0)
5779 if (name)
5780 error_at (loc, "size of array %qE is negative", name);
5781 else
5782 error_at (loc, "size of unnamed array is negative");
5783 size = integer_one_node;
5785 /* Handle a size folded to an integer constant but
5786 not an integer constant expression. */
5787 if (!size_int_const)
5789 /* If this is a file scope declaration of an
5790 ordinary identifier, this is invalid code;
5791 diagnosing it here and not subsequently
5792 treating the type as variable-length avoids
5793 more confusing diagnostics later. */
5794 if ((decl_context == NORMAL || decl_context == FIELD)
5795 && current_scope == file_scope)
5796 pedwarn (input_location, 0,
5797 "variably modified %qE at file scope",
5798 name);
5799 else
5800 this_size_varies = size_varies = true;
5801 warn_variable_length_array (name, size);
5804 else if ((decl_context == NORMAL || decl_context == FIELD)
5805 && current_scope == file_scope)
5807 error_at (loc, "variably modified %qE at file scope", name);
5808 size = integer_one_node;
5810 else
5812 /* Make sure the array size remains visibly
5813 nonconstant even if it is (eg) a const variable
5814 with known value. */
5815 this_size_varies = size_varies = true;
5816 warn_variable_length_array (name, size);
5817 if (flag_sanitize & SANITIZE_VLA
5818 && decl_context == NORMAL
5819 && do_ubsan_in_current_function ())
5821 /* Evaluate the array size only once. */
5822 size = c_save_expr (size);
5823 size = c_fully_fold (size, false, NULL);
5824 size = fold_build2 (COMPOUND_EXPR, TREE_TYPE (size),
5825 ubsan_instrument_vla (loc, size),
5826 size);
5830 if (integer_zerop (size) && !this_size_varies)
5832 /* A zero-length array cannot be represented with
5833 an unsigned index type, which is what we'll
5834 get with build_index_type. Create an
5835 open-ended range instead. */
5836 itype = build_range_type (sizetype, size, NULL_TREE);
5838 else
5840 /* Arrange for the SAVE_EXPR on the inside of the
5841 MINUS_EXPR, which allows the -1 to get folded
5842 with the +1 that happens when building TYPE_SIZE. */
5843 if (size_varies)
5844 size = save_expr (size);
5845 if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5846 size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5847 integer_zero_node, size);
5849 /* Compute the maximum valid index, that is, size
5850 - 1. Do the calculation in index_type, so that
5851 if it is a variable the computations will be
5852 done in the proper mode. */
5853 itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5854 convert (index_type, size),
5855 convert (index_type,
5856 size_one_node));
5858 /* The above overflows when size does not fit
5859 in index_type.
5860 ??? While a size of INT_MAX+1 technically shouldn't
5861 cause an overflow (because we subtract 1), handling
5862 this case seems like an unnecessary complication. */
5863 if (TREE_CODE (size) == INTEGER_CST
5864 && !int_fits_type_p (size, index_type))
5866 if (name)
5867 error_at (loc, "size of array %qE is too large",
5868 name);
5869 else
5870 error_at (loc, "size of unnamed array is too large");
5871 type = error_mark_node;
5872 continue;
5875 itype = build_index_type (itype);
5877 if (this_size_varies)
5879 if (*expr)
5880 *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5881 *expr, size);
5882 else
5883 *expr = size;
5884 *expr_const_operands &= size_maybe_const;
5887 else if (decl_context == FIELD)
5889 bool flexible_array_member = false;
5890 if (array_parm_vla_unspec_p)
5891 /* Field names can in fact have function prototype
5892 scope so [*] is disallowed here through making
5893 the field variably modified, not through being
5894 something other than a declaration with function
5895 prototype scope. */
5896 size_varies = true;
5897 else
5899 const struct c_declarator *t = declarator;
5900 while (t->kind == cdk_attrs)
5901 t = t->declarator;
5902 flexible_array_member = (t->kind == cdk_id);
5904 if (flexible_array_member
5905 && !in_system_header_at (input_location))
5906 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
5907 "support flexible array members");
5909 /* ISO C99 Flexible array members are effectively
5910 identical to GCC's zero-length array extension. */
5911 if (flexible_array_member || array_parm_vla_unspec_p)
5912 itype = build_range_type (sizetype, size_zero_node,
5913 NULL_TREE);
5915 else if (decl_context == PARM)
5917 if (array_parm_vla_unspec_p)
5919 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5920 size_varies = true;
5923 else if (decl_context == TYPENAME)
5925 if (array_parm_vla_unspec_p)
5927 /* C99 6.7.5.2p4 */
5928 warning (0, "%<[*]%> not in a declaration");
5929 /* We use this to avoid messing up with incomplete
5930 array types of the same type, that would
5931 otherwise be modified below. */
5932 itype = build_range_type (sizetype, size_zero_node,
5933 NULL_TREE);
5934 size_varies = true;
5938 /* Complain about arrays of incomplete types. */
5939 if (!COMPLETE_TYPE_P (type))
5941 error_at (loc, "array type has incomplete element type %qT",
5942 type);
5943 type = error_mark_node;
5945 else
5946 /* When itype is NULL, a shared incomplete array type is
5947 returned for all array of a given type. Elsewhere we
5948 make sure we don't complete that type before copying
5949 it, but here we want to make sure we don't ever
5950 modify the shared type, so we gcc_assert (itype)
5951 below. */
5953 addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5954 if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5955 type = build_qualified_type (type,
5956 ENCODE_QUAL_ADDR_SPACE (as));
5958 type = build_array_type (type, itype);
5961 if (type != error_mark_node)
5963 if (size_varies)
5965 /* It is ok to modify type here even if itype is
5966 NULL: if size_varies, we're in a
5967 multi-dimensional array and the inner type has
5968 variable size, so the enclosing shared array type
5969 must too. */
5970 if (size && TREE_CODE (size) == INTEGER_CST)
5971 type
5972 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5973 C_TYPE_VARIABLE_SIZE (type) = 1;
5976 /* The GCC extension for zero-length arrays differs from
5977 ISO flexible array members in that sizeof yields
5978 zero. */
5979 if (size && integer_zerop (size))
5981 gcc_assert (itype);
5982 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5983 TYPE_SIZE (type) = bitsize_zero_node;
5984 TYPE_SIZE_UNIT (type) = size_zero_node;
5985 SET_TYPE_STRUCTURAL_EQUALITY (type);
5987 if (array_parm_vla_unspec_p)
5989 gcc_assert (itype);
5990 /* The type is complete. C99 6.7.5.2p4 */
5991 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5992 TYPE_SIZE (type) = bitsize_zero_node;
5993 TYPE_SIZE_UNIT (type) = size_zero_node;
5994 SET_TYPE_STRUCTURAL_EQUALITY (type);
5998 if (decl_context != PARM
5999 && (array_ptr_quals != TYPE_UNQUALIFIED
6000 || array_ptr_attrs != NULL_TREE
6001 || array_parm_static))
6003 error_at (loc, "static or type qualifiers in non-parameter array declarator");
6004 array_ptr_quals = TYPE_UNQUALIFIED;
6005 array_ptr_attrs = NULL_TREE;
6006 array_parm_static = 0;
6008 break;
6010 case cdk_function:
6012 /* Say it's a definition only for the declarator closest
6013 to the identifier, apart possibly from some
6014 attributes. */
6015 bool really_funcdef = false;
6016 tree arg_types;
6017 if (funcdef_flag)
6019 const struct c_declarator *t = declarator->declarator;
6020 while (t->kind == cdk_attrs)
6021 t = t->declarator;
6022 really_funcdef = (t->kind == cdk_id);
6025 /* Declaring a function type. Make sure we have a valid
6026 type for the function to return. */
6027 if (type == error_mark_node)
6028 continue;
6030 size_varies = false;
6032 /* Warn about some types functions can't return. */
6033 if (TREE_CODE (type) == FUNCTION_TYPE)
6035 if (name)
6036 error_at (loc, "%qE declared as function returning a "
6037 "function", name);
6038 else
6039 error_at (loc, "type name declared as function "
6040 "returning a function");
6041 type = integer_type_node;
6043 if (TREE_CODE (type) == ARRAY_TYPE)
6045 if (name)
6046 error_at (loc, "%qE declared as function returning an array",
6047 name);
6048 else
6049 error_at (loc, "type name declared as function returning "
6050 "an array");
6051 type = integer_type_node;
6053 errmsg = targetm.invalid_return_type (type);
6054 if (errmsg)
6056 error (errmsg);
6057 type = integer_type_node;
6060 /* Construct the function type and go to the next
6061 inner layer of declarator. */
6062 arg_info = declarator->u.arg_info;
6063 arg_types = grokparms (arg_info, really_funcdef);
6065 /* Type qualifiers before the return type of the function
6066 qualify the return type, not the function type. */
6067 if (type_quals)
6069 /* Type qualifiers on a function return type are
6070 normally permitted by the standard but have no
6071 effect, so give a warning at -Wreturn-type.
6072 Qualifiers on a void return type are banned on
6073 function definitions in ISO C; GCC used to used
6074 them for noreturn functions. */
6075 if (VOID_TYPE_P (type) && really_funcdef)
6076 pedwarn (loc, 0,
6077 "function definition has qualified void return type");
6078 else
6079 warning_at (loc, OPT_Wignored_qualifiers,
6080 "type qualifiers ignored on function return type");
6082 type = c_build_qualified_type (type, type_quals);
6084 type_quals = TYPE_UNQUALIFIED;
6086 type = build_function_type (type, arg_types);
6087 declarator = declarator->declarator;
6089 /* Set the TYPE_CONTEXTs for each tagged type which is local to
6090 the formal parameter list of this FUNCTION_TYPE to point to
6091 the FUNCTION_TYPE node itself. */
6093 c_arg_tag *tag;
6094 unsigned ix;
6096 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
6097 TYPE_CONTEXT (tag->type) = type;
6099 break;
6101 case cdk_pointer:
6103 /* Merge any constancy or volatility into the target type
6104 for the pointer. */
6105 if ((type_quals & TYPE_QUAL_ATOMIC)
6106 && TREE_CODE (type) == FUNCTION_TYPE)
6108 error_at (loc,
6109 "%<_Atomic%>-qualified function type");
6110 type_quals &= ~TYPE_QUAL_ATOMIC;
6112 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6113 && type_quals)
6114 pedwarn (loc, OPT_Wpedantic,
6115 "ISO C forbids qualified function types");
6116 if (type_quals)
6117 type = c_build_qualified_type (type, type_quals);
6118 size_varies = false;
6120 /* When the pointed-to type involves components of variable size,
6121 care must be taken to ensure that the size evaluation code is
6122 emitted early enough to dominate all the possible later uses
6123 and late enough for the variables on which it depends to have
6124 been assigned.
6126 This is expected to happen automatically when the pointed-to
6127 type has a name/declaration of it's own, but special attention
6128 is required if the type is anonymous.
6130 We handle the NORMAL and FIELD contexts here by attaching an
6131 artificial TYPE_DECL to such pointed-to type. This forces the
6132 sizes evaluation at a safe point and ensures it is not deferred
6133 until e.g. within a deeper conditional context.
6135 We expect nothing to be needed here for PARM or TYPENAME.
6136 Pushing a TYPE_DECL at this point for TYPENAME would actually
6137 be incorrect, as we might be in the middle of an expression
6138 with side effects on the pointed-to type size "arguments" prior
6139 to the pointer declaration point and the fake TYPE_DECL in the
6140 enclosing context would force the size evaluation prior to the
6141 side effects. */
6143 if (!TYPE_NAME (type)
6144 && (decl_context == NORMAL || decl_context == FIELD)
6145 && variably_modified_type_p (type, NULL_TREE))
6147 tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
6148 DECL_ARTIFICIAL (decl) = 1;
6149 pushdecl (decl);
6150 finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6151 TYPE_NAME (type) = decl;
6154 type = c_build_pointer_type (type);
6156 /* Process type qualifiers (such as const or volatile)
6157 that were given inside the `*'. */
6158 type_quals = declarator->u.pointer_quals;
6160 declarator = declarator->declarator;
6161 break;
6163 default:
6164 gcc_unreachable ();
6167 *decl_attrs = chainon (returned_attrs, *decl_attrs);
6169 /* Now TYPE has the actual type, apart from any qualifiers in
6170 TYPE_QUALS. */
6172 /* Warn about address space used for things other than static memory or
6173 pointers. */
6174 address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
6175 if (!ADDR_SPACE_GENERIC_P (address_space))
6177 if (decl_context == NORMAL)
6179 switch (storage_class)
6181 case csc_auto:
6182 error ("%qs combined with %<auto%> qualifier for %qE",
6183 c_addr_space_name (address_space), name);
6184 break;
6185 case csc_register:
6186 error ("%qs combined with %<register%> qualifier for %qE",
6187 c_addr_space_name (address_space), name);
6188 break;
6189 case csc_none:
6190 if (current_function_scope)
6192 error ("%qs specified for auto variable %qE",
6193 c_addr_space_name (address_space), name);
6194 break;
6196 break;
6197 case csc_static:
6198 case csc_extern:
6199 case csc_typedef:
6200 break;
6201 default:
6202 gcc_unreachable ();
6205 else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
6207 if (name)
6208 error ("%qs specified for parameter %qE",
6209 c_addr_space_name (address_space), name);
6210 else
6211 error ("%qs specified for unnamed parameter",
6212 c_addr_space_name (address_space));
6214 else if (decl_context == FIELD)
6216 if (name)
6217 error ("%qs specified for structure field %qE",
6218 c_addr_space_name (address_space), name);
6219 else
6220 error ("%qs specified for structure field",
6221 c_addr_space_name (address_space));
6225 /* Check the type and width of a bit-field. */
6226 if (bitfield)
6228 check_bitfield_type_and_width (&type, width, name);
6229 /* C11 makes it implementation-defined (6.7.2.1#5) whether
6230 atomic types are permitted for bit-fields; we have no code to
6231 make bit-field accesses atomic, so disallow them. */
6232 if (type_quals & TYPE_QUAL_ATOMIC)
6234 if (name)
6235 error ("bit-field %qE has atomic type", name);
6236 else
6237 error ("bit-field has atomic type");
6238 type_quals &= ~TYPE_QUAL_ATOMIC;
6242 /* Reject invalid uses of _Alignas. */
6243 if (declspecs->alignas_p)
6245 if (storage_class == csc_typedef)
6246 error_at (loc, "alignment specified for typedef %qE", name);
6247 else if (storage_class == csc_register)
6248 error_at (loc, "alignment specified for %<register%> object %qE",
6249 name);
6250 else if (decl_context == PARM)
6252 if (name)
6253 error_at (loc, "alignment specified for parameter %qE", name);
6254 else
6255 error_at (loc, "alignment specified for unnamed parameter");
6257 else if (bitfield)
6259 if (name)
6260 error_at (loc, "alignment specified for bit-field %qE", name);
6261 else
6262 error_at (loc, "alignment specified for unnamed bit-field");
6264 else if (TREE_CODE (type) == FUNCTION_TYPE)
6265 error_at (loc, "alignment specified for function %qE", name);
6266 else if (declspecs->align_log != -1)
6268 alignas_align = 1U << declspecs->align_log;
6269 if (alignas_align < min_align_of_type (type))
6271 if (name)
6272 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6273 "alignment of %qE", name);
6274 else
6275 error_at (loc, "%<_Alignas%> specifiers cannot reduce "
6276 "alignment of unnamed field");
6277 alignas_align = 0;
6282 /* Did array size calculations overflow or does the array cover more
6283 than half of the address-space? */
6284 if (TREE_CODE (type) == ARRAY_TYPE
6285 && COMPLETE_TYPE_P (type)
6286 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
6287 && ! valid_constant_size_p (TYPE_SIZE_UNIT (type)))
6289 if (name)
6290 error_at (loc, "size of array %qE is too large", name);
6291 else
6292 error_at (loc, "size of unnamed array is too large");
6293 /* If we proceed with the array type as it is, we'll eventually
6294 crash in tree_to_[su]hwi(). */
6295 type = error_mark_node;
6298 /* If this is declaring a typedef name, return a TYPE_DECL. */
6300 if (storage_class == csc_typedef)
6302 tree decl;
6303 if ((type_quals & TYPE_QUAL_ATOMIC)
6304 && TREE_CODE (type) == FUNCTION_TYPE)
6306 error_at (loc,
6307 "%<_Atomic%>-qualified function type");
6308 type_quals &= ~TYPE_QUAL_ATOMIC;
6310 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6311 && type_quals)
6312 pedwarn (loc, OPT_Wpedantic,
6313 "ISO C forbids qualified function types");
6314 if (type_quals)
6315 type = c_build_qualified_type (type, type_quals);
6316 decl = build_decl (declarator->id_loc,
6317 TYPE_DECL, declarator->u.id, type);
6318 if (declspecs->explicit_signed_p)
6319 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
6320 if (declspecs->inline_p)
6321 pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
6322 if (declspecs->noreturn_p)
6323 pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
6325 if (warn_cxx_compat && declarator->u.id != NULL_TREE)
6327 struct c_binding *b = I_TAG_BINDING (declarator->u.id);
6329 if (b != NULL
6330 && b->decl != NULL_TREE
6331 && (B_IN_CURRENT_SCOPE (b)
6332 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
6333 && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
6335 warning_at (declarator->id_loc, OPT_Wc___compat,
6336 ("using %qD as both a typedef and a tag is "
6337 "invalid in C++"),
6338 decl);
6339 if (b->locus != UNKNOWN_LOCATION)
6340 inform (b->locus, "originally defined here");
6344 return decl;
6347 /* If this is a type name (such as, in a cast or sizeof),
6348 compute the type and return it now. */
6350 if (decl_context == TYPENAME)
6352 /* Note that the grammar rejects storage classes in typenames
6353 and fields. */
6354 gcc_assert (storage_class == csc_none && !threadp
6355 && !declspecs->inline_p && !declspecs->noreturn_p);
6356 if ((type_quals & TYPE_QUAL_ATOMIC)
6357 && TREE_CODE (type) == FUNCTION_TYPE)
6359 error_at (loc,
6360 "%<_Atomic%>-qualified function type");
6361 type_quals &= ~TYPE_QUAL_ATOMIC;
6363 else if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
6364 && type_quals)
6365 pedwarn (loc, OPT_Wpedantic,
6366 "ISO C forbids const or volatile function types");
6367 if (type_quals)
6368 type = c_build_qualified_type (type, type_quals);
6369 return type;
6372 if (pedantic && decl_context == FIELD
6373 && variably_modified_type_p (type, NULL_TREE))
6375 /* C99 6.7.2.1p8 */
6376 pedwarn (loc, OPT_Wpedantic, "a member of a structure or union cannot "
6377 "have a variably modified type");
6380 /* Aside from typedefs and type names (handle above),
6381 `void' at top level (not within pointer)
6382 is allowed only in public variables.
6383 We don't complain about parms either, but that is because
6384 a better error message can be made later. */
6386 if (VOID_TYPE_P (type) && decl_context != PARM
6387 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
6388 && (storage_class == csc_extern
6389 || (current_scope == file_scope
6390 && !(storage_class == csc_static
6391 || storage_class == csc_register)))))
6393 error_at (loc, "variable or field %qE declared void", name);
6394 type = integer_type_node;
6397 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
6398 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
6401 tree decl;
6403 if (decl_context == PARM)
6405 tree promoted_type;
6406 bool array_parameter_p = false;
6408 /* A parameter declared as an array of T is really a pointer to T.
6409 One declared as a function is really a pointer to a function. */
6411 if (TREE_CODE (type) == ARRAY_TYPE)
6413 /* Transfer const-ness of array into that of type pointed to. */
6414 type = TREE_TYPE (type);
6415 if (type_quals)
6416 type = c_build_qualified_type (type, type_quals);
6417 type = c_build_pointer_type (type);
6418 type_quals = array_ptr_quals;
6419 if (type_quals)
6420 type = c_build_qualified_type (type, type_quals);
6422 /* We don't yet implement attributes in this context. */
6423 if (array_ptr_attrs != NULL_TREE)
6424 warning_at (loc, OPT_Wattributes,
6425 "attributes in parameter array declarator ignored");
6427 size_varies = false;
6428 array_parameter_p = true;
6430 else if (TREE_CODE (type) == FUNCTION_TYPE)
6432 if (type_quals & TYPE_QUAL_ATOMIC)
6434 error_at (loc,
6435 "%<_Atomic%>-qualified function type");
6436 type_quals &= ~TYPE_QUAL_ATOMIC;
6438 else if (type_quals)
6439 pedwarn (loc, OPT_Wpedantic,
6440 "ISO C forbids qualified function types");
6441 if (type_quals)
6442 type = c_build_qualified_type (type, type_quals);
6443 type = c_build_pointer_type (type);
6444 type_quals = TYPE_UNQUALIFIED;
6446 else if (type_quals)
6447 type = c_build_qualified_type (type, type_quals);
6449 decl = build_decl (declarator->id_loc,
6450 PARM_DECL, declarator->u.id, type);
6451 if (size_varies)
6452 C_DECL_VARIABLE_SIZE (decl) = 1;
6453 C_ARRAY_PARAMETER (decl) = array_parameter_p;
6455 /* Compute the type actually passed in the parmlist,
6456 for the case where there is no prototype.
6457 (For example, shorts and chars are passed as ints.)
6458 When there is a prototype, this is overridden later. */
6460 if (type == error_mark_node)
6461 promoted_type = type;
6462 else
6463 promoted_type = c_type_promotes_to (type);
6465 DECL_ARG_TYPE (decl) = promoted_type;
6466 if (declspecs->inline_p)
6467 pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
6468 if (declspecs->noreturn_p)
6469 pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
6471 else if (decl_context == FIELD)
6473 /* Note that the grammar rejects storage classes in typenames
6474 and fields. */
6475 gcc_assert (storage_class == csc_none && !threadp
6476 && !declspecs->inline_p && !declspecs->noreturn_p);
6478 /* Structure field. It may not be a function. */
6480 if (TREE_CODE (type) == FUNCTION_TYPE)
6482 error_at (loc, "field %qE declared as a function", name);
6483 type = build_pointer_type (type);
6485 else if (TREE_CODE (type) != ERROR_MARK
6486 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
6488 if (name)
6489 error_at (loc, "field %qE has incomplete type", name);
6490 else
6491 error_at (loc, "unnamed field has incomplete type");
6492 type = error_mark_node;
6494 else if (TREE_CODE (type) == ARRAY_TYPE
6495 && TYPE_DOMAIN (type) == NULL_TREE)
6497 /* We have a flexible array member through a typedef.
6498 Set suitable range. Whether this is a correct position
6499 for a flexible array member will be determined elsewhere. */
6500 if (!in_system_header_at (input_location))
6501 pedwarn_c90 (loc, OPT_Wpedantic, "ISO C90 does not "
6502 "support flexible array members");
6503 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6504 TYPE_DOMAIN (type) = build_range_type (sizetype, size_zero_node,
6505 NULL_TREE);
6507 type = c_build_qualified_type (type, type_quals);
6508 decl = build_decl (declarator->id_loc,
6509 FIELD_DECL, declarator->u.id, type);
6510 DECL_NONADDRESSABLE_P (decl) = bitfield;
6511 if (bitfield && !declarator->u.id)
6512 TREE_NO_WARNING (decl) = 1;
6514 if (size_varies)
6515 C_DECL_VARIABLE_SIZE (decl) = 1;
6517 else if (TREE_CODE (type) == FUNCTION_TYPE)
6519 if (storage_class == csc_register || threadp)
6521 error_at (loc, "invalid storage class for function %qE", name);
6523 else if (current_scope != file_scope)
6525 /* Function declaration not at file scope. Storage
6526 classes other than `extern' are not allowed, C99
6527 6.7.1p5, and `extern' makes no difference. However,
6528 GCC allows 'auto', perhaps with 'inline', to support
6529 nested functions. */
6530 if (storage_class == csc_auto)
6531 pedwarn (loc, OPT_Wpedantic,
6532 "invalid storage class for function %qE", name);
6533 else if (storage_class == csc_static)
6535 error_at (loc, "invalid storage class for function %qE", name);
6536 if (funcdef_flag)
6537 storage_class = declspecs->storage_class = csc_none;
6538 else
6539 return 0;
6543 decl = build_decl (declarator->id_loc,
6544 FUNCTION_DECL, declarator->u.id, type);
6545 decl = build_decl_attribute_variant (decl, decl_attr);
6547 if (type_quals & TYPE_QUAL_ATOMIC)
6549 error_at (loc,
6550 "%<_Atomic%>-qualified function type");
6551 type_quals &= ~TYPE_QUAL_ATOMIC;
6553 else if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6554 pedwarn (loc, OPT_Wpedantic,
6555 "ISO C forbids qualified function types");
6557 /* Every function declaration is an external reference
6558 (DECL_EXTERNAL) except for those which are not at file
6559 scope and are explicitly declared "auto". This is
6560 forbidden by standard C (C99 6.7.1p5) and is interpreted by
6561 GCC to signify a forward declaration of a nested function. */
6562 if (storage_class == csc_auto && current_scope != file_scope)
6563 DECL_EXTERNAL (decl) = 0;
6564 /* In C99, a function which is declared 'inline' with 'extern'
6565 is not an external reference (which is confusing). It
6566 means that the later definition of the function must be output
6567 in this file, C99 6.7.4p6. In GNU C89, a function declared
6568 'extern inline' is an external reference. */
6569 else if (declspecs->inline_p && storage_class != csc_static)
6570 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6571 == flag_gnu89_inline);
6572 else
6573 DECL_EXTERNAL (decl) = !initialized;
6575 /* Record absence of global scope for `static' or `auto'. */
6576 TREE_PUBLIC (decl)
6577 = !(storage_class == csc_static || storage_class == csc_auto);
6579 /* For a function definition, record the argument information
6580 block where store_parm_decls will look for it. */
6581 if (funcdef_flag)
6582 current_function_arg_info = arg_info;
6584 if (declspecs->default_int_p)
6585 C_FUNCTION_IMPLICIT_INT (decl) = 1;
6587 /* Record presence of `inline' and `_Noreturn', if it is
6588 reasonable. */
6589 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6591 if (declspecs->inline_p)
6592 pedwarn (loc, 0, "cannot inline function %<main%>");
6593 if (declspecs->noreturn_p)
6594 pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6596 else
6598 if (declspecs->inline_p)
6599 /* Record that the function is declared `inline'. */
6600 DECL_DECLARED_INLINE_P (decl) = 1;
6601 if (declspecs->noreturn_p)
6603 if (flag_isoc99)
6604 pedwarn_c99 (loc, OPT_Wpedantic,
6605 "ISO C99 does not support %<_Noreturn%>");
6606 else
6607 pedwarn_c99 (loc, OPT_Wpedantic,
6608 "ISO C90 does not support %<_Noreturn%>");
6609 TREE_THIS_VOLATILE (decl) = 1;
6613 else
6615 /* It's a variable. */
6616 /* An uninitialized decl with `extern' is a reference. */
6617 int extern_ref = !initialized && storage_class == csc_extern;
6619 type = c_build_qualified_type (type, type_quals);
6621 /* C99 6.2.2p7: It is invalid (compile-time undefined
6622 behavior) to create an 'extern' declaration for a
6623 variable if there is a global declaration that is
6624 'static' and the global declaration is not visible.
6625 (If the static declaration _is_ currently visible,
6626 the 'extern' declaration is taken to refer to that decl.) */
6627 if (extern_ref && current_scope != file_scope)
6629 tree global_decl = identifier_global_value (declarator->u.id);
6630 tree visible_decl = lookup_name (declarator->u.id);
6632 if (global_decl
6633 && global_decl != visible_decl
6634 && VAR_P (global_decl)
6635 && !TREE_PUBLIC (global_decl))
6636 error_at (loc, "variable previously declared %<static%> "
6637 "redeclared %<extern%>");
6640 decl = build_decl (declarator->id_loc,
6641 VAR_DECL, declarator->u.id, type);
6642 if (size_varies)
6643 C_DECL_VARIABLE_SIZE (decl) = 1;
6645 if (declspecs->inline_p)
6646 pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6647 if (declspecs->noreturn_p)
6648 pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6650 /* At file scope, an initialized extern declaration may follow
6651 a static declaration. In that case, DECL_EXTERNAL will be
6652 reset later in start_decl. */
6653 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6655 /* At file scope, the presence of a `static' or `register' storage
6656 class specifier, or the absence of all storage class specifiers
6657 makes this declaration a definition (perhaps tentative). Also,
6658 the absence of `static' makes it public. */
6659 if (current_scope == file_scope)
6661 TREE_PUBLIC (decl) = storage_class != csc_static;
6662 TREE_STATIC (decl) = !extern_ref;
6664 /* Not at file scope, only `static' makes a static definition. */
6665 else
6667 TREE_STATIC (decl) = (storage_class == csc_static);
6668 TREE_PUBLIC (decl) = extern_ref;
6671 if (threadp)
6672 set_decl_tls_model (decl, decl_default_tls_model (decl));
6675 if ((storage_class == csc_extern
6676 || (storage_class == csc_none
6677 && TREE_CODE (type) == FUNCTION_TYPE
6678 && !funcdef_flag))
6679 && variably_modified_type_p (type, NULL_TREE))
6681 /* C99 6.7.5.2p2 */
6682 if (TREE_CODE (type) == FUNCTION_TYPE)
6683 error_at (loc, "non-nested function with variably modified type");
6684 else
6685 error_at (loc, "object with variably modified type must have "
6686 "no linkage");
6689 /* Record `register' declaration for warnings on &
6690 and in case doing stupid register allocation. */
6692 if (storage_class == csc_register)
6694 C_DECL_REGISTER (decl) = 1;
6695 DECL_REGISTER (decl) = 1;
6698 /* Record constancy and volatility. */
6699 c_apply_type_quals_to_decl (type_quals, decl);
6701 /* Apply _Alignas specifiers. */
6702 if (alignas_align)
6704 DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6705 DECL_USER_ALIGN (decl) = 1;
6708 /* If a type has volatile components, it should be stored in memory.
6709 Otherwise, the fact that those components are volatile
6710 will be ignored, and would even crash the compiler.
6711 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6712 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6713 && (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
6714 || TREE_CODE (decl) == RESULT_DECL))
6716 /* It is not an error for a structure with volatile fields to
6717 be declared register, but reset DECL_REGISTER since it
6718 cannot actually go in a register. */
6719 int was_reg = C_DECL_REGISTER (decl);
6720 C_DECL_REGISTER (decl) = 0;
6721 DECL_REGISTER (decl) = 0;
6722 c_mark_addressable (decl);
6723 C_DECL_REGISTER (decl) = was_reg;
6726 /* This is the earliest point at which we might know the assembler
6727 name of a variable. Thus, if it's known before this, die horribly. */
6728 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6730 if (warn_cxx_compat
6731 && VAR_P (decl)
6732 && TREE_PUBLIC (decl)
6733 && TREE_STATIC (decl)
6734 && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6735 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6736 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6737 && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6738 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6739 ("non-local variable %qD with anonymous type is "
6740 "questionable in C++"),
6741 decl);
6743 return decl;
6747 /* Decode the parameter-list info for a function type or function definition.
6748 The argument is the value returned by `get_parm_info' (or made in c-parse.c
6749 if there is an identifier list instead of a parameter decl list).
6750 These two functions are separate because when a function returns
6751 or receives functions then each is called multiple times but the order
6752 of calls is different. The last call to `grokparms' is always the one
6753 that contains the formal parameter names of a function definition.
6755 Return a list of arg types to use in the FUNCTION_TYPE for this function.
6757 FUNCDEF_FLAG is true for a function definition, false for
6758 a mere declaration. A nonempty identifier-list gets an error message
6759 when FUNCDEF_FLAG is false. */
6761 static tree
6762 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6764 tree arg_types = arg_info->types;
6766 if (funcdef_flag && arg_info->had_vla_unspec)
6768 /* A function definition isn't function prototype scope C99 6.2.1p4. */
6769 /* C99 6.7.5.2p4 */
6770 error ("%<[*]%> not allowed in other than function prototype scope");
6773 if (arg_types == 0 && !funcdef_flag
6774 && !in_system_header_at (input_location))
6775 warning (OPT_Wstrict_prototypes,
6776 "function declaration isn%'t a prototype");
6778 if (arg_types == error_mark_node)
6779 return 0; /* don't set TYPE_ARG_TYPES in this case */
6781 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6783 if (!funcdef_flag)
6785 pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6786 arg_info->parms = NULL_TREE;
6788 else
6789 arg_info->parms = arg_info->types;
6791 arg_info->types = 0;
6792 return 0;
6794 else
6796 tree parm, type, typelt;
6797 unsigned int parmno;
6798 const char *errmsg;
6800 /* If there is a parameter of incomplete type in a definition,
6801 this is an error. In a declaration this is valid, and a
6802 struct or union type may be completed later, before any calls
6803 or definition of the function. In the case where the tag was
6804 first declared within the parameter list, a warning has
6805 already been given. If a parameter has void type, then
6806 however the function cannot be defined or called, so
6807 warn. */
6809 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6810 parm;
6811 parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6813 type = TREE_VALUE (typelt);
6814 if (type == error_mark_node)
6815 continue;
6817 if (!COMPLETE_TYPE_P (type))
6819 if (funcdef_flag)
6821 if (DECL_NAME (parm))
6822 error_at (input_location,
6823 "parameter %u (%q+D) has incomplete type",
6824 parmno, parm);
6825 else
6826 error_at (DECL_SOURCE_LOCATION (parm),
6827 "parameter %u has incomplete type",
6828 parmno);
6830 TREE_VALUE (typelt) = error_mark_node;
6831 TREE_TYPE (parm) = error_mark_node;
6832 arg_types = NULL_TREE;
6834 else if (VOID_TYPE_P (type))
6836 if (DECL_NAME (parm))
6837 warning_at (input_location, 0,
6838 "parameter %u (%q+D) has void type",
6839 parmno, parm);
6840 else
6841 warning_at (DECL_SOURCE_LOCATION (parm), 0,
6842 "parameter %u has void type",
6843 parmno);
6847 errmsg = targetm.invalid_parameter_type (type);
6848 if (errmsg)
6850 error (errmsg);
6851 TREE_VALUE (typelt) = error_mark_node;
6852 TREE_TYPE (parm) = error_mark_node;
6853 arg_types = NULL_TREE;
6856 if (DECL_NAME (parm) && TREE_USED (parm))
6857 warn_if_shadowing (parm);
6859 return arg_types;
6863 /* Allocate and initialize a c_arg_info structure from the parser's
6864 obstack. */
6866 struct c_arg_info *
6867 build_arg_info (void)
6869 struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6870 ret->parms = NULL_TREE;
6871 ret->tags = NULL;
6872 ret->types = NULL_TREE;
6873 ret->others = NULL_TREE;
6874 ret->pending_sizes = NULL;
6875 ret->had_vla_unspec = 0;
6876 return ret;
6879 /* Take apart the current scope and return a c_arg_info structure with
6880 info on a parameter list just parsed.
6882 This structure is later fed to 'grokparms' and 'store_parm_decls'.
6884 ELLIPSIS being true means the argument list ended in '...' so don't
6885 append a sentinel (void_list_node) to the end of the type-list.
6887 EXPR is NULL or an expression that needs to be evaluated for the
6888 side effects of array size expressions in the parameters. */
6890 struct c_arg_info *
6891 get_parm_info (bool ellipsis, tree expr)
6893 struct c_binding *b = current_scope->bindings;
6894 struct c_arg_info *arg_info = build_arg_info ();
6896 tree parms = 0;
6897 vec<c_arg_tag, va_gc> *tags = NULL;
6898 tree types = 0;
6899 tree others = 0;
6901 static bool explained_incomplete_types = false;
6902 bool gave_void_only_once_err = false;
6904 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6906 /* The bindings in this scope must not get put into a block.
6907 We will take care of deleting the binding nodes. */
6908 current_scope->bindings = 0;
6910 /* This function is only called if there was *something* on the
6911 parameter list. */
6912 gcc_assert (b);
6914 /* A parameter list consisting solely of 'void' indicates that the
6915 function takes no arguments. But if the 'void' is qualified
6916 (by 'const' or 'volatile'), or has a storage class specifier
6917 ('register'), then the behavior is undefined; issue an error.
6918 Typedefs for 'void' are OK (see DR#157). */
6919 if (b->prev == 0 /* one binding */
6920 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6921 && !DECL_NAME (b->decl) /* anonymous */
6922 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6924 if (TYPE_QUALS (TREE_TYPE (b->decl)) != TYPE_UNQUALIFIED
6925 || C_DECL_REGISTER (b->decl))
6926 error ("%<void%> as only parameter may not be qualified");
6928 /* There cannot be an ellipsis. */
6929 if (ellipsis)
6930 error ("%<void%> must be the only parameter");
6932 arg_info->types = void_list_node;
6933 return arg_info;
6936 if (!ellipsis)
6937 types = void_list_node;
6939 /* Break up the bindings list into parms, tags, types, and others;
6940 apply sanity checks; purge the name-to-decl bindings. */
6941 while (b)
6943 tree decl = b->decl;
6944 tree type = TREE_TYPE (decl);
6945 c_arg_tag tag;
6946 const char *keyword;
6948 switch (TREE_CODE (decl))
6950 case PARM_DECL:
6951 if (b->id)
6953 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6954 I_SYMBOL_BINDING (b->id) = b->shadowed;
6957 /* Check for forward decls that never got their actual decl. */
6958 if (TREE_ASM_WRITTEN (decl))
6959 error ("parameter %q+D has just a forward declaration", decl);
6960 /* Check for (..., void, ...) and issue an error. */
6961 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6963 if (!gave_void_only_once_err)
6965 error ("%<void%> must be the only parameter");
6966 gave_void_only_once_err = true;
6969 else
6971 /* Valid parameter, add it to the list. */
6972 DECL_CHAIN (decl) = parms;
6973 parms = decl;
6975 /* Since there is a prototype, args are passed in their
6976 declared types. The back end may override this later. */
6977 DECL_ARG_TYPE (decl) = type;
6978 types = tree_cons (0, type, types);
6980 break;
6982 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6983 case UNION_TYPE: keyword = "union"; goto tag;
6984 case RECORD_TYPE: keyword = "struct"; goto tag;
6985 tag:
6986 /* Types may not have tag-names, in which case the type
6987 appears in the bindings list with b->id NULL. */
6988 if (b->id)
6990 gcc_assert (I_TAG_BINDING (b->id) == b);
6991 I_TAG_BINDING (b->id) = b->shadowed;
6994 /* Warn about any struct, union or enum tags defined in a
6995 parameter list. The scope of such types is limited to
6996 the parameter list, which is rarely if ever desirable
6997 (it's impossible to call such a function with type-
6998 correct arguments). An anonymous union parm type is
6999 meaningful as a GNU extension, so don't warn for that. */
7000 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
7002 if (b->id)
7003 /* The %s will be one of 'struct', 'union', or 'enum'. */
7004 warning (0, "%<%s %E%> declared inside parameter list",
7005 keyword, b->id);
7006 else
7007 /* The %s will be one of 'struct', 'union', or 'enum'. */
7008 warning (0, "anonymous %s declared inside parameter list",
7009 keyword);
7011 if (!explained_incomplete_types)
7013 warning (0, "its scope is only this definition or declaration,"
7014 " which is probably not what you want");
7015 explained_incomplete_types = true;
7019 tag.id = b->id;
7020 tag.type = decl;
7021 vec_safe_push (tags, tag);
7022 break;
7024 case CONST_DECL:
7025 case TYPE_DECL:
7026 case FUNCTION_DECL:
7027 /* CONST_DECLs appear here when we have an embedded enum,
7028 and TYPE_DECLs appear here when we have an embedded struct
7029 or union. No warnings for this - we already warned about the
7030 type itself. FUNCTION_DECLs appear when there is an implicit
7031 function declaration in the parameter list. */
7033 /* When we reinsert this decl in the function body, we need
7034 to reconstruct whether it was marked as nested. */
7035 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
7036 ? b->nested
7037 : !b->nested);
7038 DECL_CHAIN (decl) = others;
7039 others = decl;
7040 /* fall through */
7042 case ERROR_MARK:
7043 /* error_mark_node appears here when we have an undeclared
7044 variable. Just throw it away. */
7045 if (b->id)
7047 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
7048 I_SYMBOL_BINDING (b->id) = b->shadowed;
7050 break;
7052 /* Other things that might be encountered. */
7053 case LABEL_DECL:
7054 case VAR_DECL:
7055 default:
7056 gcc_unreachable ();
7059 b = free_binding_and_advance (b);
7062 arg_info->parms = parms;
7063 arg_info->tags = tags;
7064 arg_info->types = types;
7065 arg_info->others = others;
7066 arg_info->pending_sizes = expr;
7067 return arg_info;
7070 /* Get the struct, enum or union (CODE says which) with tag NAME.
7071 Define the tag as a forward-reference with location LOC if it is
7072 not defined. Return a c_typespec structure for the type
7073 specifier. */
7075 struct c_typespec
7076 parser_xref_tag (location_t loc, enum tree_code code, tree name)
7078 struct c_typespec ret;
7079 tree ref;
7080 location_t refloc;
7082 ret.expr = NULL_TREE;
7083 ret.expr_const_operands = true;
7085 /* If a cross reference is requested, look up the type
7086 already defined for this tag and return it. */
7088 ref = lookup_tag (code, name, 0, &refloc);
7089 /* If this is the right type of tag, return what we found.
7090 (This reference will be shadowed by shadow_tag later if appropriate.)
7091 If this is the wrong type of tag, do not return it. If it was the
7092 wrong type in the same scope, we will have had an error
7093 message already; if in a different scope and declaring
7094 a name, pending_xref_error will give an error message; but if in a
7095 different scope and not declaring a name, this tag should
7096 shadow the previous declaration of a different type of tag, and
7097 this would not work properly if we return the reference found.
7098 (For example, with "struct foo" in an outer scope, "union foo;"
7099 must shadow that tag with a new one of union type.) */
7100 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
7101 if (ref && TREE_CODE (ref) == code)
7103 if (C_TYPE_DEFINED_IN_STRUCT (ref)
7104 && loc != UNKNOWN_LOCATION
7105 && warn_cxx_compat)
7107 switch (code)
7109 case ENUMERAL_TYPE:
7110 warning_at (loc, OPT_Wc___compat,
7111 ("enum type defined in struct or union "
7112 "is not visible in C++"));
7113 inform (refloc, "enum type defined here");
7114 break;
7115 case RECORD_TYPE:
7116 warning_at (loc, OPT_Wc___compat,
7117 ("struct defined in struct or union "
7118 "is not visible in C++"));
7119 inform (refloc, "struct defined here");
7120 break;
7121 case UNION_TYPE:
7122 warning_at (loc, OPT_Wc___compat,
7123 ("union defined in struct or union "
7124 "is not visible in C++"));
7125 inform (refloc, "union defined here");
7126 break;
7127 default:
7128 gcc_unreachable();
7132 ret.spec = ref;
7133 return ret;
7136 /* If no such tag is yet defined, create a forward-reference node
7137 and record it as the "definition".
7138 When a real declaration of this type is found,
7139 the forward-reference will be altered into a real type. */
7141 ref = make_node (code);
7142 if (code == ENUMERAL_TYPE)
7144 /* Give the type a default layout like unsigned int
7145 to avoid crashing if it does not get defined. */
7146 SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
7147 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
7148 TYPE_USER_ALIGN (ref) = 0;
7149 TYPE_UNSIGNED (ref) = 1;
7150 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
7151 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
7152 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
7155 pushtag (loc, name, ref);
7157 ret.spec = ref;
7158 return ret;
7161 /* Get the struct, enum or union (CODE says which) with tag NAME.
7162 Define the tag as a forward-reference if it is not defined.
7163 Return a tree for the type. */
7165 tree
7166 xref_tag (enum tree_code code, tree name)
7168 return parser_xref_tag (input_location, code, name).spec;
7171 /* Make sure that the tag NAME is defined *in the current scope*
7172 at least as a forward reference.
7173 LOC is the location of the struct's definition.
7174 CODE says which kind of tag NAME ought to be.
7176 This stores the current value of the file static STRUCT_PARSE_INFO
7177 in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
7178 new c_struct_parse_info structure. The old value of
7179 STRUCT_PARSE_INFO is restored in finish_struct. */
7181 tree
7182 start_struct (location_t loc, enum tree_code code, tree name,
7183 struct c_struct_parse_info **enclosing_struct_parse_info)
7185 /* If there is already a tag defined at this scope
7186 (as a forward reference), just return it. */
7188 tree ref = NULL_TREE;
7189 location_t refloc = UNKNOWN_LOCATION;
7191 if (name != NULL_TREE)
7192 ref = lookup_tag (code, name, 1, &refloc);
7193 if (ref && TREE_CODE (ref) == code)
7195 if (TYPE_SIZE (ref))
7197 if (code == UNION_TYPE)
7198 error_at (loc, "redefinition of %<union %E%>", name);
7199 else
7200 error_at (loc, "redefinition of %<struct %E%>", name);
7201 if (refloc != UNKNOWN_LOCATION)
7202 inform (refloc, "originally defined here");
7203 /* Don't create structures using a name already in use. */
7204 ref = NULL_TREE;
7206 else if (C_TYPE_BEING_DEFINED (ref))
7208 if (code == UNION_TYPE)
7209 error_at (loc, "nested redefinition of %<union %E%>", name);
7210 else
7211 error_at (loc, "nested redefinition of %<struct %E%>", name);
7212 /* Don't bother to report "originally defined here" for a
7213 nested redefinition; the original definition should be
7214 obvious. */
7215 /* Don't create structures that contain themselves. */
7216 ref = NULL_TREE;
7220 /* Otherwise create a forward-reference just so the tag is in scope. */
7222 if (ref == NULL_TREE || TREE_CODE (ref) != code)
7224 ref = make_node (code);
7225 pushtag (loc, name, ref);
7228 C_TYPE_BEING_DEFINED (ref) = 1;
7229 TYPE_PACKED (ref) = flag_pack_struct;
7231 *enclosing_struct_parse_info = struct_parse_info;
7232 struct_parse_info = XNEW (struct c_struct_parse_info);
7233 struct_parse_info->struct_types.create (0);
7234 struct_parse_info->fields.create (0);
7235 struct_parse_info->typedefs_seen.create (0);
7237 /* FIXME: This will issue a warning for a use of a type defined
7238 within a statement expr used within sizeof, et. al. This is not
7239 terribly serious as C++ doesn't permit statement exprs within
7240 sizeof anyhow. */
7241 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7242 warning_at (loc, OPT_Wc___compat,
7243 "defining type in %qs expression is invalid in C++",
7244 (in_sizeof
7245 ? "sizeof"
7246 : (in_typeof ? "typeof" : "alignof")));
7248 return ref;
7251 /* Process the specs, declarator and width (NULL if omitted)
7252 of a structure component, returning a FIELD_DECL node.
7253 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
7254 DECL_ATTRS is as for grokdeclarator.
7256 LOC is the location of the structure component.
7258 This is done during the parsing of the struct declaration.
7259 The FIELD_DECL nodes are chained together and the lot of them
7260 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
7262 tree
7263 grokfield (location_t loc,
7264 struct c_declarator *declarator, struct c_declspecs *declspecs,
7265 tree width, tree *decl_attrs)
7267 tree value;
7269 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
7270 && width == NULL_TREE)
7272 /* This is an unnamed decl.
7274 If we have something of the form "union { list } ;" then this
7275 is the anonymous union extension. Similarly for struct.
7277 If this is something of the form "struct foo;", then
7278 If MS or Plan 9 extensions are enabled, this is handled as
7279 an anonymous struct.
7280 Otherwise this is a forward declaration of a structure tag.
7282 If this is something of the form "foo;" and foo is a TYPE_DECL, then
7283 If foo names a structure or union without a tag, then this
7284 is an anonymous struct (this is permitted by C11).
7285 If MS or Plan 9 extensions are enabled and foo names a
7286 structure, then again this is an anonymous struct.
7287 Otherwise this is an error.
7289 Oh what a horrid tangled web we weave. I wonder if MS consciously
7290 took this from Plan 9 or if it was an accident of implementation
7291 that took root before someone noticed the bug... */
7293 tree type = declspecs->type;
7294 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
7295 || TREE_CODE (type) == UNION_TYPE);
7296 bool ok = false;
7298 if (type_ok
7299 && (flag_ms_extensions
7300 || flag_plan9_extensions
7301 || !declspecs->typedef_p))
7303 if (flag_ms_extensions || flag_plan9_extensions)
7304 ok = true;
7305 else if (TYPE_NAME (type) == NULL)
7306 ok = true;
7307 else
7308 ok = false;
7310 if (!ok)
7312 pedwarn (loc, 0, "declaration does not declare anything");
7313 return NULL_TREE;
7315 if (flag_isoc99)
7316 pedwarn_c99 (loc, OPT_Wpedantic,
7317 "ISO C99 doesn%'t support unnamed structs/unions");
7318 else
7319 pedwarn_c99 (loc, OPT_Wpedantic,
7320 "ISO C90 doesn%'t support unnamed structs/unions");
7323 value = grokdeclarator (declarator, declspecs, FIELD, false,
7324 width ? &width : NULL, decl_attrs, NULL, NULL,
7325 DEPRECATED_NORMAL);
7327 finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
7328 DECL_INITIAL (value) = width;
7330 if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
7332 /* If we currently have a binding for this field, set the
7333 in_struct field in the binding, so that we warn about lookups
7334 which find it. */
7335 struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
7336 if (b != NULL)
7338 /* If the in_struct field is not yet set, push it on a list
7339 to be cleared when this struct is finished. */
7340 if (!b->in_struct)
7342 struct_parse_info->fields.safe_push (b);
7343 b->in_struct = 1;
7348 return value;
7351 /* Subroutine of detect_field_duplicates: return whether X and Y,
7352 which are both fields in the same struct, have duplicate field
7353 names. */
7355 static bool
7356 is_duplicate_field (tree x, tree y)
7358 if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
7359 return true;
7361 /* When using -fplan9-extensions, an anonymous field whose name is a
7362 typedef can duplicate a field name. */
7363 if (flag_plan9_extensions
7364 && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
7366 tree xt, xn, yt, yn;
7368 xt = TREE_TYPE (x);
7369 if (DECL_NAME (x) != NULL_TREE)
7370 xn = DECL_NAME (x);
7371 else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
7372 && TYPE_NAME (xt) != NULL_TREE
7373 && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
7374 xn = DECL_NAME (TYPE_NAME (xt));
7375 else
7376 xn = NULL_TREE;
7378 yt = TREE_TYPE (y);
7379 if (DECL_NAME (y) != NULL_TREE)
7380 yn = DECL_NAME (y);
7381 else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
7382 && TYPE_NAME (yt) != NULL_TREE
7383 && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
7384 yn = DECL_NAME (TYPE_NAME (yt));
7385 else
7386 yn = NULL_TREE;
7388 if (xn != NULL_TREE && xn == yn)
7389 return true;
7392 return false;
7395 /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
7396 to HTAB, giving errors for any duplicates. */
7398 static void
7399 detect_field_duplicates_hash (tree fieldlist,
7400 hash_table<nofree_ptr_hash <tree_node> > *htab)
7402 tree x, y;
7403 tree_node **slot;
7405 for (x = fieldlist; x ; x = DECL_CHAIN (x))
7406 if ((y = DECL_NAME (x)) != 0)
7408 slot = htab->find_slot (y, INSERT);
7409 if (*slot)
7411 error ("duplicate member %q+D", x);
7412 DECL_NAME (x) = NULL_TREE;
7414 *slot = y;
7416 else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7417 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7419 detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
7421 /* When using -fplan9-extensions, an anonymous field whose
7422 name is a typedef can duplicate a field name. */
7423 if (flag_plan9_extensions
7424 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7425 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
7427 tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
7428 slot = htab->find_slot (xn, INSERT);
7429 if (*slot)
7430 error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
7431 *slot = xn;
7436 /* Generate an error for any duplicate field names in FIELDLIST. Munge
7437 the list such that this does not present a problem later. */
7439 static void
7440 detect_field_duplicates (tree fieldlist)
7442 tree x, y;
7443 int timeout = 10;
7445 /* If the struct is the list of instance variables of an Objective-C
7446 class, then we need to check all the instance variables of
7447 superclasses when checking for duplicates (since you can't have
7448 an instance variable in a subclass with the same name as an
7449 instance variable in a superclass). We pass on this job to the
7450 Objective-C compiler. objc_detect_field_duplicates() will return
7451 false if we are not checking the list of instance variables and
7452 the C frontend should proceed with the standard field duplicate
7453 checks. If we are checking the list of instance variables, the
7454 ObjC frontend will do the check, emit the errors if needed, and
7455 then return true. */
7456 if (c_dialect_objc ())
7457 if (objc_detect_field_duplicates (false))
7458 return;
7460 /* First, see if there are more than "a few" fields.
7461 This is trivially true if there are zero or one fields. */
7462 if (!fieldlist || !DECL_CHAIN (fieldlist))
7463 return;
7464 x = fieldlist;
7465 do {
7466 timeout--;
7467 if (DECL_NAME (x) == NULL_TREE
7468 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7469 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7470 timeout = 0;
7471 x = DECL_CHAIN (x);
7472 } while (timeout > 0 && x);
7474 /* If there were "few" fields and no anonymous structures or unions,
7475 avoid the overhead of allocating a hash table. Instead just do
7476 the nested traversal thing. */
7477 if (timeout > 0)
7479 for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
7480 /* When using -fplan9-extensions, we can have duplicates
7481 between typedef names and fields. */
7482 if (DECL_NAME (x)
7483 || (flag_plan9_extensions
7484 && DECL_NAME (x) == NULL_TREE
7485 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7486 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7487 && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
7488 && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
7490 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
7491 if (is_duplicate_field (y, x))
7493 error ("duplicate member %q+D", x);
7494 DECL_NAME (x) = NULL_TREE;
7498 else
7500 hash_table<nofree_ptr_hash <tree_node> > htab (37);
7501 detect_field_duplicates_hash (fieldlist, &htab);
7505 /* Finish up struct info used by -Wc++-compat. */
7507 static void
7508 warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
7509 location_t record_loc)
7511 unsigned int ix;
7512 tree x;
7513 struct c_binding *b;
7515 if (fieldlist == NULL_TREE)
7517 if (code == RECORD_TYPE)
7518 warning_at (record_loc, OPT_Wc___compat,
7519 "empty struct has size 0 in C, size 1 in C++");
7520 else
7521 warning_at (record_loc, OPT_Wc___compat,
7522 "empty union has size 0 in C, size 1 in C++");
7525 /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7526 the current struct. We do this now at the end of the struct
7527 because the flag is used to issue visibility warnings, and we
7528 only want to issue those warnings if the type is referenced
7529 outside of the struct declaration. */
7530 FOR_EACH_VEC_ELT (struct_parse_info->struct_types, ix, x)
7531 C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7533 /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7534 typedefs used when declaring fields in this struct. If the name
7535 of any of the fields is also a typedef name then the struct would
7536 not parse in C++, because the C++ lookup rules say that the
7537 typedef name would be looked up in the context of the struct, and
7538 would thus be the field rather than the typedef. */
7539 if (!struct_parse_info->typedefs_seen.is_empty ()
7540 && fieldlist != NULL_TREE)
7542 /* Use a hash_set<tree> using the name of the typedef. We can use
7543 a hash_set<tree> because identifiers are interned. */
7544 hash_set<tree> tset;
7546 FOR_EACH_VEC_ELT (struct_parse_info->typedefs_seen, ix, x)
7547 tset.add (DECL_NAME (x));
7549 for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7551 if (DECL_NAME (x) != NULL_TREE
7552 && tset.contains (DECL_NAME (x)))
7554 warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7555 ("using %qD as both field and typedef name is "
7556 "invalid in C++"),
7558 /* FIXME: It would be nice to report the location where
7559 the typedef name is used. */
7564 /* For each field which has a binding and which was not defined in
7565 an enclosing struct, clear the in_struct field. */
7566 FOR_EACH_VEC_ELT (struct_parse_info->fields, ix, b)
7567 b->in_struct = 0;
7570 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7571 LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7572 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7573 ATTRIBUTES are attributes to be applied to the structure.
7575 ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7576 the struct was started. */
7578 tree
7579 finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7580 struct c_struct_parse_info *enclosing_struct_parse_info)
7582 tree x;
7583 bool toplevel = file_scope == current_scope;
7584 int saw_named_field;
7586 /* If this type was previously laid out as a forward reference,
7587 make sure we lay it out again. */
7589 TYPE_SIZE (t) = 0;
7591 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7593 if (pedantic)
7595 for (x = fieldlist; x; x = DECL_CHAIN (x))
7597 if (DECL_NAME (x) != 0)
7598 break;
7599 if (flag_isoc11
7600 && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7601 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7602 break;
7605 if (x == 0)
7607 if (TREE_CODE (t) == UNION_TYPE)
7609 if (fieldlist)
7610 pedwarn (loc, OPT_Wpedantic, "union has no named members");
7611 else
7612 pedwarn (loc, OPT_Wpedantic, "union has no members");
7614 else
7616 if (fieldlist)
7617 pedwarn (loc, OPT_Wpedantic, "struct has no named members");
7618 else
7619 pedwarn (loc, OPT_Wpedantic, "struct has no members");
7624 /* Install struct as DECL_CONTEXT of each field decl.
7625 Also process specified field sizes, found in the DECL_INITIAL,
7626 storing 0 there after the type has been changed to precision equal
7627 to its width, rather than the precision of the specified standard
7628 type. (Correct layout requires the original type to have been preserved
7629 until now.) */
7631 saw_named_field = 0;
7632 for (x = fieldlist; x; x = DECL_CHAIN (x))
7634 if (TREE_TYPE (x) == error_mark_node)
7635 continue;
7637 DECL_CONTEXT (x) = t;
7639 /* If any field is const, the structure type is pseudo-const. */
7640 if (TREE_READONLY (x))
7641 C_TYPE_FIELDS_READONLY (t) = 1;
7642 else
7644 /* A field that is pseudo-const makes the structure likewise. */
7645 tree t1 = strip_array_types (TREE_TYPE (x));
7646 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7647 && C_TYPE_FIELDS_READONLY (t1))
7648 C_TYPE_FIELDS_READONLY (t) = 1;
7651 /* Any field that is volatile means variables of this type must be
7652 treated in some ways as volatile. */
7653 if (TREE_THIS_VOLATILE (x))
7654 C_TYPE_FIELDS_VOLATILE (t) = 1;
7656 /* Any field of nominal variable size implies structure is too. */
7657 if (C_DECL_VARIABLE_SIZE (x))
7658 C_TYPE_VARIABLE_SIZE (t) = 1;
7660 if (DECL_INITIAL (x))
7662 unsigned HOST_WIDE_INT width = tree_to_uhwi (DECL_INITIAL (x));
7663 DECL_SIZE (x) = bitsize_int (width);
7664 DECL_BIT_FIELD (x) = 1;
7665 SET_DECL_C_BIT_FIELD (x);
7668 if (TYPE_PACKED (t)
7669 && (DECL_BIT_FIELD (x)
7670 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7671 DECL_PACKED (x) = 1;
7673 /* Detect flexible array member in an invalid context. */
7674 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7675 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7676 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7677 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7679 if (TREE_CODE (t) == UNION_TYPE)
7681 error_at (DECL_SOURCE_LOCATION (x),
7682 "flexible array member in union");
7683 TREE_TYPE (x) = error_mark_node;
7685 else if (DECL_CHAIN (x) != NULL_TREE)
7687 error_at (DECL_SOURCE_LOCATION (x),
7688 "flexible array member not at end of struct");
7689 TREE_TYPE (x) = error_mark_node;
7691 else if (!saw_named_field)
7693 error_at (DECL_SOURCE_LOCATION (x),
7694 "flexible array member in otherwise empty struct");
7695 TREE_TYPE (x) = error_mark_node;
7699 if (pedantic && TREE_CODE (t) == RECORD_TYPE
7700 && flexible_array_type_p (TREE_TYPE (x)))
7701 pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
7702 "invalid use of structure with flexible array member");
7704 if (DECL_NAME (x)
7705 || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7706 || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7707 saw_named_field = 1;
7710 detect_field_duplicates (fieldlist);
7712 /* Now we have the nearly final fieldlist. Record it,
7713 then lay out the structure or union (including the fields). */
7715 TYPE_FIELDS (t) = fieldlist;
7717 layout_type (t);
7719 if (TYPE_SIZE_UNIT (t)
7720 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST
7721 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t))
7722 && !valid_constant_size_p (TYPE_SIZE_UNIT (t)))
7723 error ("type %qT is too large", t);
7725 /* Give bit-fields their proper types. */
7727 tree *fieldlistp = &fieldlist;
7728 while (*fieldlistp)
7729 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7730 && TREE_TYPE (*fieldlistp) != error_mark_node)
7732 unsigned HOST_WIDE_INT width
7733 = tree_to_uhwi (DECL_INITIAL (*fieldlistp));
7734 tree type = TREE_TYPE (*fieldlistp);
7735 if (width != TYPE_PRECISION (type))
7737 TREE_TYPE (*fieldlistp)
7738 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7739 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7741 DECL_INITIAL (*fieldlistp) = 0;
7743 else
7744 fieldlistp = &DECL_CHAIN (*fieldlistp);
7747 /* Now we have the truly final field list.
7748 Store it in this type and in the variants. */
7750 TYPE_FIELDS (t) = fieldlist;
7752 /* If there are lots of fields, sort so we can look through them fast.
7753 We arbitrarily consider 16 or more elts to be "a lot". */
7756 int len = 0;
7758 for (x = fieldlist; x; x = DECL_CHAIN (x))
7760 if (len > 15 || DECL_NAME (x) == NULL)
7761 break;
7762 len += 1;
7765 if (len > 15)
7767 tree *field_array;
7768 struct lang_type *space;
7769 struct sorted_fields_type *space2;
7771 len += list_length (x);
7773 /* Use the same allocation policy here that make_node uses, to
7774 ensure that this lives as long as the rest of the struct decl.
7775 All decls in an inline function need to be saved. */
7777 space = ggc_cleared_alloc<struct lang_type> ();
7778 space2 = (sorted_fields_type *) ggc_internal_alloc
7779 (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7781 len = 0;
7782 space->s = space2;
7783 field_array = &space2->elts[0];
7784 for (x = fieldlist; x; x = DECL_CHAIN (x))
7786 field_array[len++] = x;
7788 /* If there is anonymous struct or union, break out of the loop. */
7789 if (DECL_NAME (x) == NULL)
7790 break;
7792 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7793 if (x == NULL)
7795 TYPE_LANG_SPECIFIC (t) = space;
7796 TYPE_LANG_SPECIFIC (t)->s->len = len;
7797 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7798 qsort (field_array, len, sizeof (tree), field_decl_cmp);
7803 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7805 TYPE_FIELDS (x) = TYPE_FIELDS (t);
7806 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7807 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7808 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7809 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7812 /* If this was supposed to be a transparent union, but we can't
7813 make it one, warn and turn off the flag. */
7814 if (TREE_CODE (t) == UNION_TYPE
7815 && TYPE_TRANSPARENT_AGGR (t)
7816 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7818 TYPE_TRANSPARENT_AGGR (t) = 0;
7819 warning_at (loc, 0, "union cannot be made transparent");
7822 /* If this structure or union completes the type of any previous
7823 variable declaration, lay it out and output its rtl.
7825 Note: C_TYPE_INCOMPLETE_VARS overloads TYPE_VFIELD which is used
7826 in dwarf2out via rest_of_decl_compilation below and means
7827 something totally different. Since we will be clearing
7828 C_TYPE_INCOMPLETE_VARS shortly after we iterate through them,
7829 clear it ahead of time and avoid problems in dwarf2out. Ideally,
7830 C_TYPE_INCOMPLETE_VARS should use some language specific
7831 node. */
7832 tree incomplete_vars = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7833 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7834 for (x = incomplete_vars; x; x = TREE_CHAIN (x))
7836 tree decl = TREE_VALUE (x);
7837 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7838 layout_array_type (TREE_TYPE (decl));
7839 if (TREE_CODE (decl) != TYPE_DECL)
7841 layout_decl (decl, 0);
7842 if (c_dialect_objc ())
7843 objc_check_decl (decl);
7844 rest_of_decl_compilation (decl, toplevel, 0);
7848 /* Update type location to the one of the definition, instead of e.g.
7849 a forward declaration. */
7850 if (TYPE_STUB_DECL (t))
7851 DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7853 /* Finish debugging output for this type. */
7854 rest_of_type_compilation (t, toplevel);
7856 /* If we're inside a function proper, i.e. not file-scope and not still
7857 parsing parameters, then arrange for the size of a variable sized type
7858 to be bound now. */
7859 if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7860 add_stmt (build_stmt (loc,
7861 DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7863 if (warn_cxx_compat)
7864 warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
7866 struct_parse_info->struct_types.release ();
7867 struct_parse_info->fields.release ();
7868 struct_parse_info->typedefs_seen.release ();
7869 XDELETE (struct_parse_info);
7871 struct_parse_info = enclosing_struct_parse_info;
7873 /* If this struct is defined inside a struct, add it to
7874 struct_types. */
7875 if (warn_cxx_compat
7876 && struct_parse_info != NULL
7877 && !in_sizeof && !in_typeof && !in_alignof)
7878 struct_parse_info->struct_types.safe_push (t);
7880 return t;
7883 /* Lay out the type T, and its element type, and so on. */
7885 static void
7886 layout_array_type (tree t)
7888 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7889 layout_array_type (TREE_TYPE (t));
7890 layout_type (t);
7893 /* Begin compiling the definition of an enumeration type.
7894 NAME is its name (or null if anonymous).
7895 LOC is the enum's location.
7896 Returns the type object, as yet incomplete.
7897 Also records info about it so that build_enumerator
7898 may be used to declare the individual values as they are read. */
7900 tree
7901 start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7903 tree enumtype = NULL_TREE;
7904 location_t enumloc = UNKNOWN_LOCATION;
7906 /* If this is the real definition for a previous forward reference,
7907 fill in the contents in the same object that used to be the
7908 forward reference. */
7910 if (name != NULL_TREE)
7911 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7913 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7915 enumtype = make_node (ENUMERAL_TYPE);
7916 pushtag (loc, name, enumtype);
7919 if (C_TYPE_BEING_DEFINED (enumtype))
7920 error_at (loc, "nested redefinition of %<enum %E%>", name);
7922 C_TYPE_BEING_DEFINED (enumtype) = 1;
7924 if (TYPE_VALUES (enumtype) != 0)
7926 /* This enum is a named one that has been declared already. */
7927 error_at (loc, "redeclaration of %<enum %E%>", name);
7928 if (enumloc != UNKNOWN_LOCATION)
7929 inform (enumloc, "originally defined here");
7931 /* Completely replace its old definition.
7932 The old enumerators remain defined, however. */
7933 TYPE_VALUES (enumtype) = 0;
7936 the_enum->enum_next_value = integer_zero_node;
7937 the_enum->enum_overflow = 0;
7939 if (flag_short_enums)
7940 for (tree v = TYPE_MAIN_VARIANT (enumtype); v; v = TYPE_NEXT_VARIANT (v))
7941 TYPE_PACKED (v) = 1;
7943 /* FIXME: This will issue a warning for a use of a type defined
7944 within sizeof in a statement expr. This is not terribly serious
7945 as C++ doesn't permit statement exprs within sizeof anyhow. */
7946 if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7947 warning_at (loc, OPT_Wc___compat,
7948 "defining type in %qs expression is invalid in C++",
7949 (in_sizeof
7950 ? "sizeof"
7951 : (in_typeof ? "typeof" : "alignof")));
7953 return enumtype;
7956 /* After processing and defining all the values of an enumeration type,
7957 install their decls in the enumeration type and finish it off.
7958 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7959 and ATTRIBUTES are the specified attributes.
7960 Returns ENUMTYPE. */
7962 tree
7963 finish_enum (tree enumtype, tree values, tree attributes)
7965 tree pair, tem;
7966 tree minnode = 0, maxnode = 0;
7967 int precision;
7968 signop sign;
7969 bool toplevel = (file_scope == current_scope);
7970 struct lang_type *lt;
7972 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7974 /* Calculate the maximum value of any enumerator in this type. */
7976 if (values == error_mark_node)
7977 minnode = maxnode = integer_zero_node;
7978 else
7980 minnode = maxnode = TREE_VALUE (values);
7981 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7983 tree value = TREE_VALUE (pair);
7984 if (tree_int_cst_lt (maxnode, value))
7985 maxnode = value;
7986 if (tree_int_cst_lt (value, minnode))
7987 minnode = value;
7991 /* Construct the final type of this enumeration. It is the same
7992 as one of the integral types - the narrowest one that fits, except
7993 that normally we only go as narrow as int - and signed iff any of
7994 the values are negative. */
7995 sign = (tree_int_cst_sgn (minnode) >= 0) ? UNSIGNED : SIGNED;
7996 precision = MAX (tree_int_cst_min_precision (minnode, sign),
7997 tree_int_cst_min_precision (maxnode, sign));
7999 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
8001 tem = c_common_type_for_size (precision, sign == UNSIGNED ? 1 : 0);
8002 if (tem == NULL)
8004 warning (0, "enumeration values exceed range of largest integer");
8005 tem = long_long_integer_type_node;
8008 else
8009 tem = sign == UNSIGNED ? unsigned_type_node : integer_type_node;
8011 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
8012 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
8013 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
8014 TYPE_ALIGN (enumtype) = TYPE_ALIGN (tem);
8015 TYPE_SIZE (enumtype) = 0;
8017 /* If the precision of the type was specified with an attribute and it
8018 was too small, give an error. Otherwise, use it. */
8019 if (TYPE_PRECISION (enumtype)
8020 && lookup_attribute ("mode", attributes))
8022 if (precision > TYPE_PRECISION (enumtype))
8023 error ("specified mode too small for enumeral values");
8025 else
8026 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
8028 layout_type (enumtype);
8030 if (values != error_mark_node)
8032 /* Change the type of the enumerators to be the enum type. We
8033 need to do this irrespective of the size of the enum, for
8034 proper type checking. Replace the DECL_INITIALs of the
8035 enumerators, and the value slots of the list, with copies
8036 that have the enum type; they cannot be modified in place
8037 because they may be shared (e.g. integer_zero_node) Finally,
8038 change the purpose slots to point to the names of the decls. */
8039 for (pair = values; pair; pair = TREE_CHAIN (pair))
8041 tree enu = TREE_PURPOSE (pair);
8042 tree ini = DECL_INITIAL (enu);
8044 TREE_TYPE (enu) = enumtype;
8046 /* The ISO C Standard mandates enumerators to have type int,
8047 even though the underlying type of an enum type is
8048 unspecified. However, GCC allows enumerators of any
8049 integer type as an extensions. build_enumerator()
8050 converts any enumerators that fit in an int to type int,
8051 to avoid promotions to unsigned types when comparing
8052 integers with enumerators that fit in the int range.
8053 When -pedantic is given, build_enumerator() would have
8054 already warned about those that don't fit. Here we
8055 convert the rest to the enumerator type. */
8056 if (TREE_TYPE (ini) != integer_type_node)
8057 ini = convert (enumtype, ini);
8059 DECL_INITIAL (enu) = ini;
8060 TREE_PURPOSE (pair) = DECL_NAME (enu);
8061 TREE_VALUE (pair) = ini;
8064 TYPE_VALUES (enumtype) = values;
8067 /* Record the min/max values so that we can warn about bit-field
8068 enumerations that are too small for the values. */
8069 lt = ggc_cleared_alloc<struct lang_type> ();
8070 lt->enum_min = minnode;
8071 lt->enum_max = maxnode;
8072 TYPE_LANG_SPECIFIC (enumtype) = lt;
8074 /* Fix up all variant types of this enum type. */
8075 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
8077 if (tem == enumtype)
8078 continue;
8079 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
8080 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
8081 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
8082 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
8083 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
8084 SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
8085 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
8086 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
8087 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
8088 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
8089 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
8092 /* Finish debugging output for this type. */
8093 rest_of_type_compilation (enumtype, toplevel);
8095 /* If this enum is defined inside a struct, add it to
8096 struct_types. */
8097 if (warn_cxx_compat
8098 && struct_parse_info != NULL
8099 && !in_sizeof && !in_typeof && !in_alignof)
8100 struct_parse_info->struct_types.safe_push (enumtype);
8102 return enumtype;
8105 /* Build and install a CONST_DECL for one value of the
8106 current enumeration type (one that was begun with start_enum).
8107 DECL_LOC is the location of the enumerator.
8108 LOC is the location of the '=' operator if any, DECL_LOC otherwise.
8109 Return a tree-list containing the CONST_DECL and its value.
8110 Assignment of sequential values by default is handled here. */
8112 tree
8113 build_enumerator (location_t decl_loc, location_t loc,
8114 struct c_enum_contents *the_enum, tree name, tree value)
8116 tree decl, type;
8118 /* Validate and default VALUE. */
8120 if (value != 0)
8122 /* Don't issue more errors for error_mark_node (i.e. an
8123 undeclared identifier) - just ignore the value expression. */
8124 if (value == error_mark_node)
8125 value = 0;
8126 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
8128 error_at (loc, "enumerator value for %qE is not an integer constant",
8129 name);
8130 value = 0;
8132 else
8134 if (TREE_CODE (value) != INTEGER_CST)
8136 value = c_fully_fold (value, false, NULL);
8137 if (TREE_CODE (value) == INTEGER_CST)
8138 pedwarn (loc, OPT_Wpedantic,
8139 "enumerator value for %qE is not an integer "
8140 "constant expression", name);
8142 if (TREE_CODE (value) != INTEGER_CST)
8144 error ("enumerator value for %qE is not an integer constant",
8145 name);
8146 value = 0;
8148 else
8150 value = default_conversion (value);
8151 constant_expression_warning (value);
8156 /* Default based on previous value. */
8157 /* It should no longer be possible to have NON_LVALUE_EXPR
8158 in the default. */
8159 if (value == 0)
8161 value = the_enum->enum_next_value;
8162 if (the_enum->enum_overflow)
8163 error_at (loc, "overflow in enumeration values");
8165 /* Even though the underlying type of an enum is unspecified, the
8166 type of enumeration constants is explicitly defined as int
8167 (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
8168 an extension. */
8169 else if (!int_fits_type_p (value, integer_type_node))
8170 pedwarn (loc, OPT_Wpedantic,
8171 "ISO C restricts enumerator values to range of %<int%>");
8173 /* The ISO C Standard mandates enumerators to have type int, even
8174 though the underlying type of an enum type is unspecified.
8175 However, GCC allows enumerators of any integer type as an
8176 extensions. Here we convert any enumerators that fit in an int
8177 to type int, to avoid promotions to unsigned types when comparing
8178 integers with enumerators that fit in the int range. When
8179 -pedantic is given, we would have already warned about those that
8180 don't fit. We have to do this here rather than in finish_enum
8181 because this value may be used to define more enumerators. */
8182 if (int_fits_type_p (value, integer_type_node))
8183 value = convert (integer_type_node, value);
8185 /* Set basis for default for next value. */
8186 the_enum->enum_next_value
8187 = build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
8188 PLUS_EXPR, value, integer_one_node, 0);
8189 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
8191 /* Now create a declaration for the enum value name. */
8193 type = TREE_TYPE (value);
8194 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
8195 TYPE_PRECISION (integer_type_node)),
8196 (TYPE_PRECISION (type)
8197 >= TYPE_PRECISION (integer_type_node)
8198 && TYPE_UNSIGNED (type)));
8200 decl = build_decl (decl_loc, CONST_DECL, name, type);
8201 DECL_INITIAL (decl) = convert (type, value);
8202 pushdecl (decl);
8204 return tree_cons (decl, value, NULL_TREE);
8208 /* Create the FUNCTION_DECL for a function definition.
8209 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
8210 the declaration; they describe the function's name and the type it returns,
8211 but twisted together in a fashion that parallels the syntax of C.
8213 This function creates a binding context for the function body
8214 as well as setting up the FUNCTION_DECL in current_function_decl.
8216 Returns 1 on success. If the DECLARATOR is not suitable for a function
8217 (it defines a datum instead), we return 0, which tells
8218 yyparse to report a parse error. */
8221 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
8222 tree attributes)
8224 tree decl1, old_decl;
8225 tree restype, resdecl;
8226 location_t loc;
8228 current_function_returns_value = 0; /* Assume, until we see it does. */
8229 current_function_returns_null = 0;
8230 current_function_returns_abnormally = 0;
8231 warn_about_return_type = 0;
8232 c_switch_stack = NULL;
8234 /* Indicate no valid break/continue context by setting these variables
8235 to some non-null, non-label value. We'll notice and emit the proper
8236 error message in c_finish_bc_stmt. */
8237 c_break_label = c_cont_label = size_zero_node;
8239 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
8240 &attributes, NULL, NULL, DEPRECATED_NORMAL);
8241 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
8243 /* If the declarator is not suitable for a function definition,
8244 cause a syntax error. */
8245 if (decl1 == 0
8246 || TREE_CODE (decl1) != FUNCTION_DECL)
8247 return 0;
8249 loc = DECL_SOURCE_LOCATION (decl1);
8251 c_decl_attributes (&decl1, attributes, 0);
8253 if (DECL_DECLARED_INLINE_P (decl1)
8254 && DECL_UNINLINABLE (decl1)
8255 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
8256 warning_at (loc, OPT_Wattributes,
8257 "inline function %qD given attribute noinline",
8258 decl1);
8260 /* Handle gnu_inline attribute. */
8261 if (declspecs->inline_p
8262 && !flag_gnu89_inline
8263 && TREE_CODE (decl1) == FUNCTION_DECL
8264 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
8265 || current_function_decl))
8267 if (declspecs->storage_class != csc_static)
8268 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
8271 announce_function (decl1);
8273 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
8275 error_at (loc, "return type is an incomplete type");
8276 /* Make it return void instead. */
8277 TREE_TYPE (decl1)
8278 = build_function_type (void_type_node,
8279 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
8282 if (warn_about_return_type)
8283 warn_defaults_to (loc, flag_isoc99 ? OPT_Wimplicit_int
8284 : (warn_return_type ? OPT_Wreturn_type
8285 : OPT_Wimplicit_int),
8286 "return type defaults to %<int%>");
8288 /* Make the init_value nonzero so pushdecl knows this is not tentative.
8289 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
8290 DECL_INITIAL (decl1) = error_mark_node;
8292 /* A nested function is not global. */
8293 if (current_function_decl != 0)
8294 TREE_PUBLIC (decl1) = 0;
8296 /* If this definition isn't a prototype and we had a prototype declaration
8297 before, copy the arg type info from that prototype. */
8298 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
8299 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
8300 old_decl = 0;
8301 current_function_prototype_locus = UNKNOWN_LOCATION;
8302 current_function_prototype_built_in = false;
8303 current_function_prototype_arg_types = NULL_TREE;
8304 if (!prototype_p (TREE_TYPE (decl1)))
8306 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
8307 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8308 TREE_TYPE (TREE_TYPE (old_decl))))
8310 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
8311 TREE_TYPE (decl1));
8312 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
8313 current_function_prototype_built_in
8314 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
8315 current_function_prototype_arg_types
8316 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
8318 if (TREE_PUBLIC (decl1))
8320 /* If there is an external prototype declaration of this
8321 function, record its location but do not copy information
8322 to this decl. This may be an invisible declaration
8323 (built-in or in a scope which has finished) or simply
8324 have more refined argument types than any declaration
8325 found above. */
8326 struct c_binding *b;
8327 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
8328 if (B_IN_SCOPE (b, external_scope))
8329 break;
8330 if (b)
8332 tree ext_decl, ext_type;
8333 ext_decl = b->decl;
8334 ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
8335 if (TREE_CODE (ext_type) == FUNCTION_TYPE
8336 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
8337 TREE_TYPE (ext_type)))
8339 current_function_prototype_locus
8340 = DECL_SOURCE_LOCATION (ext_decl);
8341 current_function_prototype_built_in
8342 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
8343 current_function_prototype_arg_types
8344 = TYPE_ARG_TYPES (ext_type);
8350 /* Optionally warn of old-fashioned def with no previous prototype. */
8351 if (warn_strict_prototypes
8352 && old_decl != error_mark_node
8353 && !prototype_p (TREE_TYPE (decl1))
8354 && C_DECL_ISNT_PROTOTYPE (old_decl))
8355 warning_at (loc, OPT_Wstrict_prototypes,
8356 "function declaration isn%'t a prototype");
8357 /* Optionally warn of any global def with no previous prototype. */
8358 else if (warn_missing_prototypes
8359 && old_decl != error_mark_node
8360 && TREE_PUBLIC (decl1)
8361 && !MAIN_NAME_P (DECL_NAME (decl1))
8362 && C_DECL_ISNT_PROTOTYPE (old_decl)
8363 && !DECL_DECLARED_INLINE_P (decl1))
8364 warning_at (loc, OPT_Wmissing_prototypes,
8365 "no previous prototype for %qD", decl1);
8366 /* Optionally warn of any def with no previous prototype
8367 if the function has already been used. */
8368 else if (warn_missing_prototypes
8369 && old_decl != 0
8370 && old_decl != error_mark_node
8371 && TREE_USED (old_decl)
8372 && !prototype_p (TREE_TYPE (old_decl)))
8373 warning_at (loc, OPT_Wmissing_prototypes,
8374 "%qD was used with no prototype before its definition", decl1);
8375 /* Optionally warn of any global def with no previous declaration. */
8376 else if (warn_missing_declarations
8377 && TREE_PUBLIC (decl1)
8378 && old_decl == 0
8379 && !MAIN_NAME_P (DECL_NAME (decl1))
8380 && !DECL_DECLARED_INLINE_P (decl1))
8381 warning_at (loc, OPT_Wmissing_declarations,
8382 "no previous declaration for %qD",
8383 decl1);
8384 /* Optionally warn of any def with no previous declaration
8385 if the function has already been used. */
8386 else if (warn_missing_declarations
8387 && old_decl != 0
8388 && old_decl != error_mark_node
8389 && TREE_USED (old_decl)
8390 && C_DECL_IMPLICIT (old_decl))
8391 warning_at (loc, OPT_Wmissing_declarations,
8392 "%qD was used with no declaration before its definition", decl1);
8394 /* This function exists in static storage.
8395 (This does not mean `static' in the C sense!) */
8396 TREE_STATIC (decl1) = 1;
8398 /* This is the earliest point at which we might know the assembler
8399 name of the function. Thus, if it's set before this, die horribly. */
8400 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
8402 /* If #pragma weak was used, mark the decl weak now. */
8403 if (current_scope == file_scope)
8404 maybe_apply_pragma_weak (decl1);
8406 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
8407 if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
8409 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
8410 != integer_type_node)
8411 pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
8412 else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (decl1))))
8413 pedwarn (loc, OPT_Wmain, "%<_Atomic%>-qualified return type of %qD",
8414 decl1);
8416 check_main_parameter_types (decl1);
8418 if (!TREE_PUBLIC (decl1))
8419 pedwarn (loc, OPT_Wmain,
8420 "%qD is normally a non-static function", decl1);
8423 /* Record the decl so that the function name is defined.
8424 If we already have a decl for this name, and it is a FUNCTION_DECL,
8425 use the old decl. */
8427 current_function_decl = pushdecl (decl1);
8429 push_scope ();
8430 declare_parm_level ();
8432 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
8433 resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
8434 DECL_ARTIFICIAL (resdecl) = 1;
8435 DECL_IGNORED_P (resdecl) = 1;
8436 DECL_RESULT (current_function_decl) = resdecl;
8438 start_fname_decls ();
8440 return 1;
8443 /* Subroutine of store_parm_decls which handles new-style function
8444 definitions (prototype format). The parms already have decls, so we
8445 need only record them as in effect and complain if any redundant
8446 old-style parm decls were written. */
8447 static void
8448 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
8450 tree decl;
8451 c_arg_tag *tag;
8452 unsigned ix;
8454 if (current_scope->bindings)
8456 error_at (DECL_SOURCE_LOCATION (fndecl),
8457 "old-style parameter declarations in prototyped "
8458 "function definition");
8460 /* Get rid of the old-style declarations. */
8461 pop_scope ();
8462 push_scope ();
8464 /* Don't issue this warning for nested functions, and don't issue this
8465 warning if we got here because ARG_INFO_TYPES was error_mark_node
8466 (this happens when a function definition has just an ellipsis in
8467 its parameter list). */
8468 else if (!in_system_header_at (input_location)
8469 && !current_function_scope
8470 && arg_info->types != error_mark_node)
8471 warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
8472 "traditional C rejects ISO C style function definitions");
8474 /* Now make all the parameter declarations visible in the function body.
8475 We can bypass most of the grunt work of pushdecl. */
8476 for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
8478 DECL_CONTEXT (decl) = current_function_decl;
8479 if (DECL_NAME (decl))
8481 bind (DECL_NAME (decl), decl, current_scope,
8482 /*invisible=*/false, /*nested=*/false,
8483 UNKNOWN_LOCATION);
8484 if (!TREE_USED (decl))
8485 warn_if_shadowing (decl);
8487 else
8488 error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
8491 /* Record the parameter list in the function declaration. */
8492 DECL_ARGUMENTS (fndecl) = arg_info->parms;
8494 /* Now make all the ancillary declarations visible, likewise. */
8495 for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
8497 DECL_CONTEXT (decl) = current_function_decl;
8498 if (DECL_NAME (decl))
8499 bind (DECL_NAME (decl), decl, current_scope,
8500 /*invisible=*/false,
8501 /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
8502 UNKNOWN_LOCATION);
8505 /* And all the tag declarations. */
8506 FOR_EACH_VEC_SAFE_ELT_REVERSE (arg_info->tags, ix, tag)
8507 if (tag->id)
8508 bind (tag->id, tag->type, current_scope,
8509 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
8512 /* Subroutine of store_parm_decls which handles old-style function
8513 definitions (separate parameter list and declarations). */
8515 static void
8516 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
8518 struct c_binding *b;
8519 tree parm, decl, last;
8520 tree parmids = arg_info->parms;
8521 hash_set<tree> seen_args;
8523 if (!in_system_header_at (input_location))
8524 warning_at (DECL_SOURCE_LOCATION (fndecl),
8525 OPT_Wold_style_definition, "old-style function definition");
8527 /* Match each formal parameter name with its declaration. Save each
8528 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
8529 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8531 if (TREE_VALUE (parm) == 0)
8533 error_at (DECL_SOURCE_LOCATION (fndecl),
8534 "parameter name missing from parameter list");
8535 TREE_PURPOSE (parm) = 0;
8536 continue;
8539 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
8540 if (b && B_IN_CURRENT_SCOPE (b))
8542 decl = b->decl;
8543 /* Skip erroneous parameters. */
8544 if (decl == error_mark_node)
8545 continue;
8546 /* If we got something other than a PARM_DECL it is an error. */
8547 if (TREE_CODE (decl) != PARM_DECL)
8548 error_at (DECL_SOURCE_LOCATION (decl),
8549 "%qD declared as a non-parameter", decl);
8550 /* If the declaration is already marked, we have a duplicate
8551 name. Complain and ignore the duplicate. */
8552 else if (seen_args.contains (decl))
8554 error_at (DECL_SOURCE_LOCATION (decl),
8555 "multiple parameters named %qD", decl);
8556 TREE_PURPOSE (parm) = 0;
8557 continue;
8559 /* If the declaration says "void", complain and turn it into
8560 an int. */
8561 else if (VOID_TYPE_P (TREE_TYPE (decl)))
8563 error_at (DECL_SOURCE_LOCATION (decl),
8564 "parameter %qD declared with void type", decl);
8565 TREE_TYPE (decl) = integer_type_node;
8566 DECL_ARG_TYPE (decl) = integer_type_node;
8567 layout_decl (decl, 0);
8569 warn_if_shadowing (decl);
8571 /* If no declaration found, default to int. */
8572 else
8574 /* FIXME diagnostics: This should be the location of the argument,
8575 not the FNDECL. E.g., for an old-style declaration
8577 int f10(v) { blah; }
8579 We should use the location of the V, not the F10.
8580 Unfortunately, the V is an IDENTIFIER_NODE which has no
8581 location. In the future we need locations for c_arg_info
8582 entries.
8584 See gcc.dg/Wshadow-3.c for an example of this problem. */
8585 decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8586 PARM_DECL, TREE_VALUE (parm), integer_type_node);
8587 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8588 pushdecl (decl);
8589 warn_if_shadowing (decl);
8591 if (flag_isoc99)
8592 pedwarn (DECL_SOURCE_LOCATION (decl),
8593 OPT_Wimplicit_int, "type of %qD defaults to %<int%>",
8594 decl);
8595 else
8596 warning_at (DECL_SOURCE_LOCATION (decl),
8597 OPT_Wmissing_parameter_type,
8598 "type of %qD defaults to %<int%>", decl);
8601 TREE_PURPOSE (parm) = decl;
8602 seen_args.add (decl);
8605 /* Now examine the parms chain for incomplete declarations
8606 and declarations with no corresponding names. */
8608 for (b = current_scope->bindings; b; b = b->prev)
8610 parm = b->decl;
8611 if (TREE_CODE (parm) != PARM_DECL)
8612 continue;
8614 if (TREE_TYPE (parm) != error_mark_node
8615 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8617 error_at (DECL_SOURCE_LOCATION (parm),
8618 "parameter %qD has incomplete type", parm);
8619 TREE_TYPE (parm) = error_mark_node;
8622 if (!seen_args.contains (parm))
8624 error_at (DECL_SOURCE_LOCATION (parm),
8625 "declaration for parameter %qD but no such parameter",
8626 parm);
8628 /* Pretend the parameter was not missing.
8629 This gets us to a standard state and minimizes
8630 further error messages. */
8631 parmids = chainon (parmids, tree_cons (parm, 0, 0));
8635 /* Chain the declarations together in the order of the list of
8636 names. Store that chain in the function decl, replacing the
8637 list of names. Update the current scope to match. */
8638 DECL_ARGUMENTS (fndecl) = 0;
8640 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8641 if (TREE_PURPOSE (parm))
8642 break;
8643 if (parm && TREE_PURPOSE (parm))
8645 last = TREE_PURPOSE (parm);
8646 DECL_ARGUMENTS (fndecl) = last;
8648 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8649 if (TREE_PURPOSE (parm))
8651 DECL_CHAIN (last) = TREE_PURPOSE (parm);
8652 last = TREE_PURPOSE (parm);
8654 DECL_CHAIN (last) = 0;
8657 /* If there was a previous prototype,
8658 set the DECL_ARG_TYPE of each argument according to
8659 the type previously specified, and report any mismatches. */
8661 if (current_function_prototype_arg_types)
8663 tree type;
8664 for (parm = DECL_ARGUMENTS (fndecl),
8665 type = current_function_prototype_arg_types;
8666 parm || (type && TREE_VALUE (type) != error_mark_node
8667 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8668 parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8670 if (parm == 0 || type == 0
8671 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8673 if (current_function_prototype_built_in)
8674 warning_at (DECL_SOURCE_LOCATION (fndecl),
8675 0, "number of arguments doesn%'t match "
8676 "built-in prototype");
8677 else
8679 /* FIXME diagnostics: This should be the location of
8680 FNDECL, but there is bug when a prototype is
8681 declared inside function context, but defined
8682 outside of it (e.g., gcc.dg/pr15698-2.c). In
8683 which case FNDECL gets the location of the
8684 prototype, not the definition. */
8685 error_at (input_location,
8686 "number of arguments doesn%'t match prototype");
8688 error_at (current_function_prototype_locus,
8689 "prototype declaration");
8691 break;
8693 /* Type for passing arg must be consistent with that
8694 declared for the arg. ISO C says we take the unqualified
8695 type for parameters declared with qualified type. */
8696 if (TREE_TYPE (parm) != error_mark_node
8697 && TREE_TYPE (type) != error_mark_node
8698 && ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8699 != TYPE_ATOMIC (TREE_VALUE (type)))
8700 || !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8701 TYPE_MAIN_VARIANT (TREE_VALUE (type)))))
8703 if ((TYPE_ATOMIC (DECL_ARG_TYPE (parm))
8704 == TYPE_ATOMIC (TREE_VALUE (type)))
8705 && (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8706 == TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8708 /* Adjust argument to match prototype. E.g. a previous
8709 `int foo(float);' prototype causes
8710 `int foo(x) float x; {...}' to be treated like
8711 `int foo(float x) {...}'. This is particularly
8712 useful for argument types like uid_t. */
8713 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8715 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8716 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8717 && TYPE_PRECISION (TREE_TYPE (parm))
8718 < TYPE_PRECISION (integer_type_node))
8719 DECL_ARG_TYPE (parm)
8720 = c_type_promotes_to (TREE_TYPE (parm));
8722 /* ??? Is it possible to get here with a
8723 built-in prototype or will it always have
8724 been diagnosed as conflicting with an
8725 old-style definition and discarded? */
8726 if (current_function_prototype_built_in)
8727 warning_at (DECL_SOURCE_LOCATION (parm),
8728 OPT_Wpedantic, "promoted argument %qD "
8729 "doesn%'t match built-in prototype", parm);
8730 else
8732 pedwarn (DECL_SOURCE_LOCATION (parm),
8733 OPT_Wpedantic, "promoted argument %qD "
8734 "doesn%'t match prototype", parm);
8735 pedwarn (current_function_prototype_locus, OPT_Wpedantic,
8736 "prototype declaration");
8739 else
8741 if (current_function_prototype_built_in)
8742 warning_at (DECL_SOURCE_LOCATION (parm),
8743 0, "argument %qD doesn%'t match "
8744 "built-in prototype", parm);
8745 else
8747 error_at (DECL_SOURCE_LOCATION (parm),
8748 "argument %qD doesn%'t match prototype", parm);
8749 error_at (current_function_prototype_locus,
8750 "prototype declaration");
8755 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8758 /* Otherwise, create a prototype that would match. */
8760 else
8762 tree actual = 0, last = 0, type;
8764 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8766 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8767 if (last)
8768 TREE_CHAIN (last) = type;
8769 else
8770 actual = type;
8771 last = type;
8773 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8774 if (last)
8775 TREE_CHAIN (last) = type;
8776 else
8777 actual = type;
8779 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8780 of the type of this function, but we need to avoid having this
8781 affect the types of other similarly-typed functions, so we must
8782 first force the generation of an identical (but separate) type
8783 node for the relevant function type. The new node we create
8784 will be a variant of the main variant of the original function
8785 type. */
8787 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8789 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8793 /* Store parameter declarations passed in ARG_INFO into the current
8794 function declaration. */
8796 void
8797 store_parm_decls_from (struct c_arg_info *arg_info)
8799 current_function_arg_info = arg_info;
8800 store_parm_decls ();
8803 /* Called by walk_tree to look for and update context-less labels. */
8805 static tree
8806 set_labels_context_r (tree *tp, int *walk_subtrees, void *data)
8808 if (TREE_CODE (*tp) == LABEL_EXPR
8809 && DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) == NULL_TREE)
8811 DECL_CONTEXT (LABEL_EXPR_LABEL (*tp)) = static_cast<tree>(data);
8812 *walk_subtrees = 0;
8815 return NULL_TREE;
8818 /* Store the parameter declarations into the current function declaration.
8819 This is called after parsing the parameter declarations, before
8820 digesting the body of the function.
8822 For an old-style definition, construct a prototype out of the old-style
8823 parameter declarations and inject it into the function's type. */
8825 void
8826 store_parm_decls (void)
8828 tree fndecl = current_function_decl;
8829 bool proto;
8831 /* The argument information block for FNDECL. */
8832 struct c_arg_info *arg_info = current_function_arg_info;
8833 current_function_arg_info = 0;
8835 /* True if this definition is written with a prototype. Note:
8836 despite C99 6.7.5.3p14, we can *not* treat an empty argument
8837 list in a function definition as equivalent to (void) -- an
8838 empty argument list specifies the function has no parameters,
8839 but only (void) sets up a prototype for future calls. */
8840 proto = arg_info->types != 0;
8842 if (proto)
8843 store_parm_decls_newstyle (fndecl, arg_info);
8844 else
8845 store_parm_decls_oldstyle (fndecl, arg_info);
8847 /* The next call to push_scope will be a function body. */
8849 next_is_function_body = true;
8851 /* Write a record describing this function definition to the prototypes
8852 file (if requested). */
8854 gen_aux_info_record (fndecl, 1, 0, proto);
8856 /* Initialize the RTL code for the function. */
8857 allocate_struct_function (fndecl, false);
8859 if (warn_unused_local_typedefs)
8860 cfun->language = ggc_cleared_alloc<language_function> ();
8862 /* Begin the statement tree for this function. */
8863 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8865 /* ??? Insert the contents of the pending sizes list into the function
8866 to be evaluated. The only reason left to have this is
8867 void foo(int n, int array[n++])
8868 because we throw away the array type in favor of a pointer type, and
8869 thus won't naturally see the SAVE_EXPR containing the increment. All
8870 other pending sizes would be handled by gimplify_parameters. */
8871 if (arg_info->pending_sizes)
8873 /* In very special circumstances, e.g. for code like
8874 _Atomic int i = 5;
8875 void f (int a[i += 2]) {}
8876 we need to execute the atomic assignment on function entry.
8877 But in this case, it is not just a straight store, it has the
8878 op= form, which means that build_atomic_assign has generated
8879 gotos, labels, etc. Because at that time the function decl
8880 for F has not been created yet, those labels do not have any
8881 function context. But we have the fndecl now, so update the
8882 labels accordingly. gimplify_expr would crash otherwise. */
8883 walk_tree_without_duplicates (&arg_info->pending_sizes,
8884 set_labels_context_r, fndecl);
8885 add_stmt (arg_info->pending_sizes);
8889 /* Store PARM_DECLs in PARMS into scope temporarily. Used for
8890 c_finish_omp_declare_simd for function prototypes. No diagnostics
8891 should be done. */
8893 void
8894 temp_store_parm_decls (tree fndecl, tree parms)
8896 push_scope ();
8897 for (tree p = parms; p; p = DECL_CHAIN (p))
8899 DECL_CONTEXT (p) = fndecl;
8900 if (DECL_NAME (p))
8901 bind (DECL_NAME (p), p, current_scope,
8902 /*invisible=*/false, /*nested=*/false,
8903 UNKNOWN_LOCATION);
8907 /* Undo what temp_store_parm_decls did. */
8909 void
8910 temp_pop_parm_decls (void)
8912 /* Clear all bindings in this temporary scope, so that
8913 pop_scope doesn't create a BLOCK. */
8914 struct c_binding *b = current_scope->bindings;
8915 current_scope->bindings = NULL;
8916 for (; b; b = free_binding_and_advance (b))
8918 gcc_assert (TREE_CODE (b->decl) == PARM_DECL);
8919 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
8920 I_SYMBOL_BINDING (b->id) = b->shadowed;
8921 if (b->shadowed && b->shadowed->u.type)
8922 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
8924 pop_scope ();
8928 /* Finish up a function declaration and compile that function
8929 all the way to assembler language output. Then free the storage
8930 for the function definition.
8932 This is called after parsing the body of the function definition. */
8934 void
8935 finish_function (void)
8937 tree fndecl = current_function_decl;
8939 if (c_dialect_objc ())
8940 objc_finish_function ();
8942 if (TREE_CODE (fndecl) == FUNCTION_DECL
8943 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8945 tree args = DECL_ARGUMENTS (fndecl);
8946 for (; args; args = DECL_CHAIN (args))
8948 tree type = TREE_TYPE (args);
8949 if (INTEGRAL_TYPE_P (type)
8950 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8951 DECL_ARG_TYPE (args) = c_type_promotes_to (type);
8955 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8956 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8958 /* Must mark the RESULT_DECL as being in this function. */
8960 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8961 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8963 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8964 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8965 == integer_type_node && flag_isoc99)
8967 /* Hack. We don't want the middle-end to warn that this return
8968 is unreachable, so we mark its location as special. Using
8969 UNKNOWN_LOCATION has the problem that it gets clobbered in
8970 annotate_one_with_locus. A cleaner solution might be to
8971 ensure ! should_carry_locus_p (stmt), but that needs a flag.
8973 c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8976 /* Tie off the statement tree for this function. */
8977 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8979 /* If the function has _Cilk_spawn in front of a function call inside it
8980 i.e. it is a spawning function, then add the appropriate Cilk plus
8981 functions inside. */
8982 if (fn_contains_cilk_spawn_p (cfun))
8983 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
8985 finish_fname_decls ();
8987 /* Complain if there's just no return statement. */
8988 if (warn_return_type
8989 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8990 && !current_function_returns_value && !current_function_returns_null
8991 /* Don't complain if we are no-return. */
8992 && !current_function_returns_abnormally
8993 /* Don't complain if we are declared noreturn. */
8994 && !TREE_THIS_VOLATILE (fndecl)
8995 /* Don't warn for main(). */
8996 && !MAIN_NAME_P (DECL_NAME (fndecl))
8997 /* Or if they didn't actually specify a return type. */
8998 && !C_FUNCTION_IMPLICIT_INT (fndecl)
8999 /* Normally, with -Wreturn-type, flow will complain, but we might
9000 optimize out static functions. */
9001 && !TREE_PUBLIC (fndecl))
9003 warning (OPT_Wreturn_type,
9004 "no return statement in function returning non-void");
9005 TREE_NO_WARNING (fndecl) = 1;
9008 /* Complain about parameters that are only set, but never otherwise used. */
9009 if (warn_unused_but_set_parameter)
9011 tree decl;
9013 for (decl = DECL_ARGUMENTS (fndecl);
9014 decl;
9015 decl = DECL_CHAIN (decl))
9016 if (TREE_USED (decl)
9017 && TREE_CODE (decl) == PARM_DECL
9018 && !DECL_READ_P (decl)
9019 && DECL_NAME (decl)
9020 && !DECL_ARTIFICIAL (decl)
9021 && !TREE_NO_WARNING (decl))
9022 warning_at (DECL_SOURCE_LOCATION (decl),
9023 OPT_Wunused_but_set_parameter,
9024 "parameter %qD set but not used", decl);
9027 /* Complain about locally defined typedefs that are not used in this
9028 function. */
9029 maybe_warn_unused_local_typedefs ();
9031 /* Possibly warn about unused parameters. */
9032 if (warn_unused_parameter)
9033 do_warn_unused_parameter (fndecl);
9035 /* Store the end of the function, so that we get good line number
9036 info for the epilogue. */
9037 cfun->function_end_locus = input_location;
9039 /* Finalize the ELF visibility for the function. */
9040 c_determine_visibility (fndecl);
9042 /* For GNU C extern inline functions disregard inline limits. */
9043 if (DECL_EXTERNAL (fndecl)
9044 && DECL_DECLARED_INLINE_P (fndecl))
9045 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
9047 /* Genericize before inlining. Delay genericizing nested functions
9048 until their parent function is genericized. Since finalizing
9049 requires GENERIC, delay that as well. */
9051 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
9052 && !undef_nested_function)
9054 if (!decl_function_context (fndecl))
9056 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
9057 c_genericize (fndecl);
9059 /* ??? Objc emits functions after finalizing the compilation unit.
9060 This should be cleaned up later and this conditional removed. */
9061 if (symtab->global_info_ready)
9063 cgraph_node::add_new_function (fndecl, false);
9064 return;
9066 cgraph_node::finalize_function (fndecl, false);
9068 else
9070 /* Register this function with cgraph just far enough to get it
9071 added to our parent's nested function list. Handy, since the
9072 C front end doesn't have such a list. */
9073 (void) cgraph_node::get_create (fndecl);
9077 if (!decl_function_context (fndecl))
9078 undef_nested_function = false;
9080 if (cfun->language != NULL)
9082 ggc_free (cfun->language);
9083 cfun->language = NULL;
9086 /* We're leaving the context of this function, so zap cfun.
9087 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
9088 tree_rest_of_compilation. */
9089 set_cfun (NULL);
9090 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, current_function_decl);
9091 current_function_decl = NULL;
9094 /* Check the declarations given in a for-loop for satisfying the C99
9095 constraints. If exactly one such decl is found, return it. LOC is
9096 the location of the opening parenthesis of the for loop. The last
9097 parameter allows you to control the "for loop initial declarations
9098 are only allowed in C99 mode". Normally, you should pass
9099 flag_isoc99 as that parameter. But in some cases (Objective-C
9100 foreach loop, for example) we want to run the checks in this
9101 function even if not in C99 mode, so we allow the caller to turn
9102 off the error about not being in C99 mode.
9105 tree
9106 check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
9108 struct c_binding *b;
9109 tree one_decl = NULL_TREE;
9110 int n_decls = 0;
9112 if (!turn_off_iso_c99_error)
9114 static bool hint = true;
9115 /* If we get here, declarations have been used in a for loop without
9116 the C99 for loop scope. This doesn't make much sense, so don't
9117 allow it. */
9118 error_at (loc, "%<for%> loop initial declarations "
9119 "are only allowed in C99 or C11 mode");
9120 if (hint)
9122 inform (loc,
9123 "use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 "
9124 "to compile your code");
9125 hint = false;
9127 return NULL_TREE;
9129 /* C99 subclause 6.8.5 paragraph 3:
9131 [#3] The declaration part of a for statement shall only
9132 declare identifiers for objects having storage class auto or
9133 register.
9135 It isn't clear whether, in this sentence, "identifiers" binds to
9136 "shall only declare" or to "objects" - that is, whether all identifiers
9137 declared must be identifiers for objects, or whether the restriction
9138 only applies to those that are. (A question on this in comp.std.c
9139 in November 2000 received no answer.) We implement the strictest
9140 interpretation, to avoid creating an extension which later causes
9141 problems. */
9143 for (b = current_scope->bindings; b; b = b->prev)
9145 tree id = b->id;
9146 tree decl = b->decl;
9148 if (!id)
9149 continue;
9151 switch (TREE_CODE (decl))
9153 case VAR_DECL:
9155 location_t decl_loc = DECL_SOURCE_LOCATION (decl);
9156 if (TREE_STATIC (decl))
9157 error_at (decl_loc,
9158 "declaration of static variable %qD in %<for%> loop "
9159 "initial declaration", decl);
9160 else if (DECL_EXTERNAL (decl))
9161 error_at (decl_loc,
9162 "declaration of %<extern%> variable %qD in %<for%> loop "
9163 "initial declaration", decl);
9165 break;
9167 case RECORD_TYPE:
9168 error_at (loc,
9169 "%<struct %E%> declared in %<for%> loop initial "
9170 "declaration", id);
9171 break;
9172 case UNION_TYPE:
9173 error_at (loc,
9174 "%<union %E%> declared in %<for%> loop initial declaration",
9175 id);
9176 break;
9177 case ENUMERAL_TYPE:
9178 error_at (loc, "%<enum %E%> declared in %<for%> loop "
9179 "initial declaration", id);
9180 break;
9181 default:
9182 error_at (loc, "declaration of non-variable "
9183 "%qD in %<for%> loop initial declaration", decl);
9186 n_decls++;
9187 one_decl = decl;
9190 return n_decls == 1 ? one_decl : NULL_TREE;
9193 /* Save and reinitialize the variables
9194 used during compilation of a C function. */
9196 void
9197 c_push_function_context (void)
9199 struct language_function *p = cfun->language;
9200 /* cfun->language might have been already allocated by the use of
9201 -Wunused-local-typedefs. In that case, just re-use it. */
9202 if (p == NULL)
9203 cfun->language = p = ggc_cleared_alloc<language_function> ();
9205 p->base.x_stmt_tree = c_stmt_tree;
9206 c_stmt_tree.x_cur_stmt_list = vec_safe_copy (c_stmt_tree.x_cur_stmt_list);
9207 p->x_break_label = c_break_label;
9208 p->x_cont_label = c_cont_label;
9209 p->x_switch_stack = c_switch_stack;
9210 p->arg_info = current_function_arg_info;
9211 p->returns_value = current_function_returns_value;
9212 p->returns_null = current_function_returns_null;
9213 p->returns_abnormally = current_function_returns_abnormally;
9214 p->warn_about_return_type = warn_about_return_type;
9216 push_function_context ();
9219 /* Restore the variables used during compilation of a C function. */
9221 void
9222 c_pop_function_context (void)
9224 struct language_function *p;
9226 pop_function_context ();
9227 p = cfun->language;
9229 /* When -Wunused-local-typedefs is in effect, cfun->languages is
9230 used to store data throughout the life time of the current cfun,
9231 So don't deallocate it. */
9232 if (!warn_unused_local_typedefs)
9233 cfun->language = NULL;
9235 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
9236 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
9238 /* Stop pointing to the local nodes about to be freed. */
9239 /* But DECL_INITIAL must remain nonzero so we know this
9240 was an actual function definition. */
9241 DECL_INITIAL (current_function_decl) = error_mark_node;
9242 DECL_ARGUMENTS (current_function_decl) = 0;
9245 c_stmt_tree = p->base.x_stmt_tree;
9246 p->base.x_stmt_tree.x_cur_stmt_list = NULL;
9247 c_break_label = p->x_break_label;
9248 c_cont_label = p->x_cont_label;
9249 c_switch_stack = p->x_switch_stack;
9250 current_function_arg_info = p->arg_info;
9251 current_function_returns_value = p->returns_value;
9252 current_function_returns_null = p->returns_null;
9253 current_function_returns_abnormally = p->returns_abnormally;
9254 warn_about_return_type = p->warn_about_return_type;
9257 /* The functions below are required for functionality of doing
9258 function at once processing in the C front end. Currently these
9259 functions are not called from anywhere in the C front end, but as
9260 these changes continue, that will change. */
9262 /* Returns the stmt_tree (if any) to which statements are currently
9263 being added. If there is no active statement-tree, NULL is
9264 returned. */
9266 stmt_tree
9267 current_stmt_tree (void)
9269 return &c_stmt_tree;
9272 /* Return the global value of T as a symbol. */
9274 tree
9275 identifier_global_value (tree t)
9277 struct c_binding *b;
9279 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
9280 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
9281 return b->decl;
9283 return 0;
9286 /* In C, the only C-linkage public declaration is at file scope. */
9288 tree
9289 c_linkage_bindings (tree name)
9291 return identifier_global_value (name);
9294 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
9295 otherwise the name is found in ridpointers from RID_INDEX. */
9297 void
9298 record_builtin_type (enum rid rid_index, const char *name, tree type)
9300 tree id, decl;
9301 if (name == 0)
9302 id = ridpointers[(int) rid_index];
9303 else
9304 id = get_identifier (name);
9305 decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
9306 pushdecl (decl);
9307 if (debug_hooks->type_decl)
9308 debug_hooks->type_decl (decl, false);
9311 /* Build the void_list_node (void_type_node having been created). */
9312 tree
9313 build_void_list_node (void)
9315 tree t = build_tree_list (NULL_TREE, void_type_node);
9316 return t;
9319 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
9321 struct c_parm *
9322 build_c_parm (struct c_declspecs *specs, tree attrs,
9323 struct c_declarator *declarator)
9325 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
9326 ret->specs = specs;
9327 ret->attrs = attrs;
9328 ret->declarator = declarator;
9329 return ret;
9332 /* Return a declarator with nested attributes. TARGET is the inner
9333 declarator to which these attributes apply. ATTRS are the
9334 attributes. */
9336 struct c_declarator *
9337 build_attrs_declarator (tree attrs, struct c_declarator *target)
9339 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9340 ret->kind = cdk_attrs;
9341 ret->declarator = target;
9342 ret->u.attrs = attrs;
9343 return ret;
9346 /* Return a declarator for a function with arguments specified by ARGS
9347 and return type specified by TARGET. */
9349 struct c_declarator *
9350 build_function_declarator (struct c_arg_info *args,
9351 struct c_declarator *target)
9353 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9354 ret->kind = cdk_function;
9355 ret->declarator = target;
9356 ret->u.arg_info = args;
9357 return ret;
9360 /* Return a declarator for the identifier IDENT (which may be
9361 NULL_TREE for an abstract declarator). */
9363 struct c_declarator *
9364 build_id_declarator (tree ident)
9366 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9367 ret->kind = cdk_id;
9368 ret->declarator = 0;
9369 ret->u.id = ident;
9370 /* Default value - may get reset to a more precise location. */
9371 ret->id_loc = input_location;
9372 return ret;
9375 /* Return something to represent absolute declarators containing a *.
9376 TARGET is the absolute declarator that the * contains.
9377 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
9378 to apply to the pointer type. */
9380 struct c_declarator *
9381 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
9382 struct c_declarator *target)
9384 tree attrs;
9385 int quals = 0;
9386 struct c_declarator *itarget = target;
9387 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
9388 if (type_quals_attrs)
9390 attrs = type_quals_attrs->attrs;
9391 quals = quals_from_declspecs (type_quals_attrs);
9392 if (attrs != NULL_TREE)
9393 itarget = build_attrs_declarator (attrs, target);
9395 ret->kind = cdk_pointer;
9396 ret->declarator = itarget;
9397 ret->u.pointer_quals = quals;
9398 return ret;
9401 /* Return a pointer to a structure for an empty list of declaration
9402 specifiers. */
9404 struct c_declspecs *
9405 build_null_declspecs (void)
9407 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
9408 memset (&ret->locations, 0, cdw_number_of_elements);
9409 ret->type = 0;
9410 ret->expr = 0;
9411 ret->decl_attr = 0;
9412 ret->attrs = 0;
9413 ret->align_log = -1;
9414 ret->typespec_word = cts_none;
9415 ret->storage_class = csc_none;
9416 ret->expr_const_operands = true;
9417 ret->declspecs_seen_p = false;
9418 ret->typespec_kind = ctsk_none;
9419 ret->non_sc_seen_p = false;
9420 ret->typedef_p = false;
9421 ret->explicit_signed_p = false;
9422 ret->deprecated_p = false;
9423 ret->default_int_p = false;
9424 ret->long_p = false;
9425 ret->long_long_p = false;
9426 ret->short_p = false;
9427 ret->signed_p = false;
9428 ret->unsigned_p = false;
9429 ret->complex_p = false;
9430 ret->inline_p = false;
9431 ret->noreturn_p = false;
9432 ret->thread_p = false;
9433 ret->thread_gnu_p = false;
9434 ret->const_p = false;
9435 ret->volatile_p = false;
9436 ret->atomic_p = false;
9437 ret->restrict_p = false;
9438 ret->saturating_p = false;
9439 ret->alignas_p = false;
9440 ret->address_space = ADDR_SPACE_GENERIC;
9441 return ret;
9444 /* Add the address space ADDRSPACE to the declaration specifiers
9445 SPECS, returning SPECS. */
9447 struct c_declspecs *
9448 declspecs_add_addrspace (source_location location,
9449 struct c_declspecs *specs, addr_space_t as)
9451 specs->non_sc_seen_p = true;
9452 specs->declspecs_seen_p = true;
9454 if (!ADDR_SPACE_GENERIC_P (specs->address_space)
9455 && specs->address_space != as)
9456 error ("incompatible address space qualifiers %qs and %qs",
9457 c_addr_space_name (as),
9458 c_addr_space_name (specs->address_space));
9459 else
9461 specs->address_space = as;
9462 specs->locations[cdw_address_space] = location;
9464 return specs;
9467 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
9468 returning SPECS. */
9470 struct c_declspecs *
9471 declspecs_add_qual (source_location loc,
9472 struct c_declspecs *specs, tree qual)
9474 enum rid i;
9475 bool dupe = false;
9476 specs->non_sc_seen_p = true;
9477 specs->declspecs_seen_p = true;
9478 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
9479 && C_IS_RESERVED_WORD (qual));
9480 i = C_RID_CODE (qual);
9481 switch (i)
9483 case RID_CONST:
9484 dupe = specs->const_p;
9485 specs->const_p = true;
9486 specs->locations[cdw_const] = loc;
9487 break;
9488 case RID_VOLATILE:
9489 dupe = specs->volatile_p;
9490 specs->volatile_p = true;
9491 specs->locations[cdw_volatile] = loc;
9492 break;
9493 case RID_RESTRICT:
9494 dupe = specs->restrict_p;
9495 specs->restrict_p = true;
9496 specs->locations[cdw_restrict] = loc;
9497 break;
9498 case RID_ATOMIC:
9499 dupe = specs->atomic_p;
9500 specs->atomic_p = true;
9501 break;
9502 default:
9503 gcc_unreachable ();
9505 if (dupe)
9506 pedwarn_c90 (loc, OPT_Wpedantic, "duplicate %qE", qual);
9507 return specs;
9510 /* Add the type specifier TYPE to the declaration specifiers SPECS,
9511 returning SPECS. */
9513 struct c_declspecs *
9514 declspecs_add_type (location_t loc, struct c_declspecs *specs,
9515 struct c_typespec spec)
9517 tree type = spec.spec;
9518 specs->non_sc_seen_p = true;
9519 specs->declspecs_seen_p = true;
9520 specs->typespec_kind = spec.kind;
9521 if (TREE_DEPRECATED (type))
9522 specs->deprecated_p = true;
9524 /* Handle type specifier keywords. */
9525 if (TREE_CODE (type) == IDENTIFIER_NODE
9526 && C_IS_RESERVED_WORD (type)
9527 && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
9529 enum rid i = C_RID_CODE (type);
9530 if (specs->type)
9532 error_at (loc, "two or more data types in declaration specifiers");
9533 return specs;
9535 if ((int) i <= (int) RID_LAST_MODIFIER)
9537 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
9538 bool dupe = false;
9539 switch (i)
9541 case RID_LONG:
9542 if (specs->long_long_p)
9544 error_at (loc, "%<long long long%> is too long for GCC");
9545 break;
9547 if (specs->long_p)
9549 if (specs->typespec_word == cts_double)
9551 error_at (loc,
9552 ("both %<long long%> and %<double%> in "
9553 "declaration specifiers"));
9554 break;
9556 pedwarn_c90 (loc, OPT_Wlong_long,
9557 "ISO C90 does not support %<long long%>");
9558 specs->long_long_p = 1;
9559 specs->locations[cdw_long_long] = loc;
9560 break;
9562 if (specs->short_p)
9563 error_at (loc,
9564 ("both %<long%> and %<short%> in "
9565 "declaration specifiers"));
9566 else if (specs->typespec_word == cts_auto_type)
9567 error_at (loc,
9568 ("both %<long%> and %<__auto_type%> in "
9569 "declaration specifiers"));
9570 else if (specs->typespec_word == cts_void)
9571 error_at (loc,
9572 ("both %<long%> and %<void%> in "
9573 "declaration specifiers"));
9574 else if (specs->typespec_word == cts_int_n)
9575 error_at (loc,
9576 ("both %<long%> and %<__int%d%> in "
9577 "declaration specifiers"),
9578 int_n_data[specs->int_n_idx].bitsize);
9579 else if (specs->typespec_word == cts_bool)
9580 error_at (loc,
9581 ("both %<long%> and %<_Bool%> in "
9582 "declaration specifiers"));
9583 else if (specs->typespec_word == cts_char)
9584 error_at (loc,
9585 ("both %<long%> and %<char%> in "
9586 "declaration specifiers"));
9587 else if (specs->typespec_word == cts_float)
9588 error_at (loc,
9589 ("both %<long%> and %<float%> in "
9590 "declaration specifiers"));
9591 else if (specs->typespec_word == cts_dfloat32)
9592 error_at (loc,
9593 ("both %<long%> and %<_Decimal32%> in "
9594 "declaration specifiers"));
9595 else if (specs->typespec_word == cts_dfloat64)
9596 error_at (loc,
9597 ("both %<long%> and %<_Decimal64%> in "
9598 "declaration specifiers"));
9599 else if (specs->typespec_word == cts_dfloat128)
9600 error_at (loc,
9601 ("both %<long%> and %<_Decimal128%> in "
9602 "declaration specifiers"));
9603 else
9605 specs->long_p = true;
9606 specs->locations[cdw_long] = loc;
9608 break;
9609 case RID_SHORT:
9610 dupe = specs->short_p;
9611 if (specs->long_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 %<short%> and %<__auto_type%> in "
9618 "declaration specifiers"));
9619 else if (specs->typespec_word == cts_void)
9620 error_at (loc,
9621 ("both %<short%> and %<void%> in "
9622 "declaration specifiers"));
9623 else if (specs->typespec_word == cts_int_n)
9624 error_at (loc,
9625 ("both %<short%> 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 %<short%> and %<_Bool%> in "
9631 "declaration specifiers"));
9632 else if (specs->typespec_word == cts_char)
9633 error_at (loc,
9634 ("both %<short%> and %<char%> in "
9635 "declaration specifiers"));
9636 else if (specs->typespec_word == cts_float)
9637 error_at (loc,
9638 ("both %<short%> and %<float%> in "
9639 "declaration specifiers"));
9640 else if (specs->typespec_word == cts_double)
9641 error_at (loc,
9642 ("both %<short%> and %<double%> in "
9643 "declaration specifiers"));
9644 else if (specs->typespec_word == cts_dfloat32)
9645 error_at (loc,
9646 ("both %<short%> and %<_Decimal32%> in "
9647 "declaration specifiers"));
9648 else if (specs->typespec_word == cts_dfloat64)
9649 error_at (loc,
9650 ("both %<short%> and %<_Decimal64%> in "
9651 "declaration specifiers"));
9652 else if (specs->typespec_word == cts_dfloat128)
9653 error_at (loc,
9654 ("both %<short%> and %<_Decimal128%> in "
9655 "declaration specifiers"));
9656 else
9658 specs->short_p = true;
9659 specs->locations[cdw_short] = loc;
9661 break;
9662 case RID_SIGNED:
9663 dupe = specs->signed_p;
9664 if (specs->unsigned_p)
9665 error_at (loc,
9666 ("both %<signed%> and %<unsigned%> in "
9667 "declaration specifiers"));
9668 else if (specs->typespec_word == cts_auto_type)
9669 error_at (loc,
9670 ("both %<signed%> and %<__auto_type%> in "
9671 "declaration specifiers"));
9672 else if (specs->typespec_word == cts_void)
9673 error_at (loc,
9674 ("both %<signed%> and %<void%> in "
9675 "declaration specifiers"));
9676 else if (specs->typespec_word == cts_bool)
9677 error_at (loc,
9678 ("both %<signed%> and %<_Bool%> in "
9679 "declaration specifiers"));
9680 else if (specs->typespec_word == cts_float)
9681 error_at (loc,
9682 ("both %<signed%> and %<float%> in "
9683 "declaration specifiers"));
9684 else if (specs->typespec_word == cts_double)
9685 error_at (loc,
9686 ("both %<signed%> and %<double%> in "
9687 "declaration specifiers"));
9688 else if (specs->typespec_word == cts_dfloat32)
9689 error_at (loc,
9690 ("both %<signed%> and %<_Decimal32%> in "
9691 "declaration specifiers"));
9692 else if (specs->typespec_word == cts_dfloat64)
9693 error_at (loc,
9694 ("both %<signed%> and %<_Decimal64%> in "
9695 "declaration specifiers"));
9696 else if (specs->typespec_word == cts_dfloat128)
9697 error_at (loc,
9698 ("both %<signed%> and %<_Decimal128%> in "
9699 "declaration specifiers"));
9700 else
9702 specs->signed_p = true;
9703 specs->locations[cdw_signed] = loc;
9705 break;
9706 case RID_UNSIGNED:
9707 dupe = specs->unsigned_p;
9708 if (specs->signed_p)
9709 error_at (loc,
9710 ("both %<signed%> and %<unsigned%> in "
9711 "declaration specifiers"));
9712 else if (specs->typespec_word == cts_auto_type)
9713 error_at (loc,
9714 ("both %<unsigned%> and %<__auto_type%> in "
9715 "declaration specifiers"));
9716 else if (specs->typespec_word == cts_void)
9717 error_at (loc,
9718 ("both %<unsigned%> and %<void%> in "
9719 "declaration specifiers"));
9720 else if (specs->typespec_word == cts_bool)
9721 error_at (loc,
9722 ("both %<unsigned%> and %<_Bool%> in "
9723 "declaration specifiers"));
9724 else if (specs->typespec_word == cts_float)
9725 error_at (loc,
9726 ("both %<unsigned%> and %<float%> in "
9727 "declaration specifiers"));
9728 else if (specs->typespec_word == cts_double)
9729 error_at (loc,
9730 ("both %<unsigned%> and %<double%> in "
9731 "declaration specifiers"));
9732 else if (specs->typespec_word == cts_dfloat32)
9733 error_at (loc,
9734 ("both %<unsigned%> and %<_Decimal32%> in "
9735 "declaration specifiers"));
9736 else if (specs->typespec_word == cts_dfloat64)
9737 error_at (loc,
9738 ("both %<unsigned%> and %<_Decimal64%> in "
9739 "declaration specifiers"));
9740 else if (specs->typespec_word == cts_dfloat128)
9741 error_at (loc,
9742 ("both %<unsigned%> and %<_Decimal128%> in "
9743 "declaration specifiers"));
9744 else
9746 specs->unsigned_p = true;
9747 specs->locations[cdw_unsigned] = loc;
9749 break;
9750 case RID_COMPLEX:
9751 dupe = specs->complex_p;
9752 if (!in_system_header_at (loc))
9753 pedwarn_c90 (loc, OPT_Wpedantic,
9754 "ISO C90 does not support complex types");
9755 if (specs->typespec_word == cts_auto_type)
9756 error_at (loc,
9757 ("both %<complex%> and %<__auto_type%> in "
9758 "declaration specifiers"));
9759 else if (specs->typespec_word == cts_void)
9760 error_at (loc,
9761 ("both %<complex%> and %<void%> in "
9762 "declaration specifiers"));
9763 else if (specs->typespec_word == cts_bool)
9764 error_at (loc,
9765 ("both %<complex%> and %<_Bool%> in "
9766 "declaration specifiers"));
9767 else if (specs->typespec_word == cts_dfloat32)
9768 error_at (loc,
9769 ("both %<complex%> and %<_Decimal32%> in "
9770 "declaration specifiers"));
9771 else if (specs->typespec_word == cts_dfloat64)
9772 error_at (loc,
9773 ("both %<complex%> and %<_Decimal64%> in "
9774 "declaration specifiers"));
9775 else if (specs->typespec_word == cts_dfloat128)
9776 error_at (loc,
9777 ("both %<complex%> and %<_Decimal128%> in "
9778 "declaration specifiers"));
9779 else if (specs->typespec_word == cts_fract)
9780 error_at (loc,
9781 ("both %<complex%> and %<_Fract%> in "
9782 "declaration specifiers"));
9783 else if (specs->typespec_word == cts_accum)
9784 error_at (loc,
9785 ("both %<complex%> and %<_Accum%> in "
9786 "declaration specifiers"));
9787 else if (specs->saturating_p)
9788 error_at (loc,
9789 ("both %<complex%> and %<_Sat%> in "
9790 "declaration specifiers"));
9791 else
9793 specs->complex_p = true;
9794 specs->locations[cdw_complex] = loc;
9796 break;
9797 case RID_SAT:
9798 dupe = specs->saturating_p;
9799 pedwarn (loc, OPT_Wpedantic,
9800 "ISO C does not support saturating types");
9801 if (specs->typespec_word == cts_int_n)
9803 error_at (loc,
9804 ("both %<_Sat%> and %<__int%d%> in "
9805 "declaration specifiers"),
9806 int_n_data[specs->int_n_idx].bitsize);
9808 else if (specs->typespec_word == cts_auto_type)
9809 error_at (loc,
9810 ("both %<_Sat%> and %<__auto_type%> in "
9811 "declaration specifiers"));
9812 else if (specs->typespec_word == cts_void)
9813 error_at (loc,
9814 ("both %<_Sat%> and %<void%> in "
9815 "declaration specifiers"));
9816 else if (specs->typespec_word == cts_bool)
9817 error_at (loc,
9818 ("both %<_Sat%> and %<_Bool%> in "
9819 "declaration specifiers"));
9820 else if (specs->typespec_word == cts_char)
9821 error_at (loc,
9822 ("both %<_Sat%> and %<char%> in "
9823 "declaration specifiers"));
9824 else if (specs->typespec_word == cts_int)
9825 error_at (loc,
9826 ("both %<_Sat%> and %<int%> in "
9827 "declaration specifiers"));
9828 else if (specs->typespec_word == cts_float)
9829 error_at (loc,
9830 ("both %<_Sat%> and %<float%> in "
9831 "declaration specifiers"));
9832 else if (specs->typespec_word == cts_double)
9833 error_at (loc,
9834 ("both %<_Sat%> and %<double%> in "
9835 "declaration specifiers"));
9836 else if (specs->typespec_word == cts_dfloat32)
9837 error_at (loc,
9838 ("both %<_Sat%> and %<_Decimal32%> in "
9839 "declaration specifiers"));
9840 else if (specs->typespec_word == cts_dfloat64)
9841 error_at (loc,
9842 ("both %<_Sat%> and %<_Decimal64%> in "
9843 "declaration specifiers"));
9844 else if (specs->typespec_word == cts_dfloat128)
9845 error_at (loc,
9846 ("both %<_Sat%> and %<_Decimal128%> in "
9847 "declaration specifiers"));
9848 else if (specs->complex_p)
9849 error_at (loc,
9850 ("both %<_Sat%> and %<complex%> in "
9851 "declaration specifiers"));
9852 else
9854 specs->saturating_p = true;
9855 specs->locations[cdw_saturating] = loc;
9857 break;
9858 default:
9859 gcc_unreachable ();
9862 if (dupe)
9863 error_at (loc, "duplicate %qE", type);
9865 return specs;
9867 else
9869 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9870 "__intN", "_Decimal64", "_Decimal128", "_Fract", "_Accum" or
9871 "__auto_type". */
9872 if (specs->typespec_word != cts_none)
9874 error_at (loc,
9875 "two or more data types in declaration specifiers");
9876 return specs;
9878 switch (i)
9880 case RID_AUTO_TYPE:
9881 if (specs->long_p)
9882 error_at (loc,
9883 ("both %<long%> and %<__auto_type%> in "
9884 "declaration specifiers"));
9885 else if (specs->short_p)
9886 error_at (loc,
9887 ("both %<short%> and %<__auto_type%> in "
9888 "declaration specifiers"));
9889 else if (specs->signed_p)
9890 error_at (loc,
9891 ("both %<signed%> and %<__auto_type%> in "
9892 "declaration specifiers"));
9893 else if (specs->unsigned_p)
9894 error_at (loc,
9895 ("both %<unsigned%> and %<__auto_type%> in "
9896 "declaration specifiers"));
9897 else if (specs->complex_p)
9898 error_at (loc,
9899 ("both %<complex%> and %<__auto_type%> in "
9900 "declaration specifiers"));
9901 else if (specs->saturating_p)
9902 error_at (loc,
9903 ("both %<_Sat%> and %<__auto_type%> in "
9904 "declaration specifiers"));
9905 else
9907 specs->typespec_word = cts_auto_type;
9908 specs->locations[cdw_typespec] = loc;
9910 return specs;
9911 case RID_INT_N_0:
9912 case RID_INT_N_1:
9913 case RID_INT_N_2:
9914 case RID_INT_N_3:
9915 specs->int_n_idx = i - RID_INT_N_0;
9916 if (!in_system_header_at (input_location))
9917 pedwarn (loc, OPT_Wpedantic,
9918 "ISO C does not support %<__int%d%> types",
9919 int_n_data[specs->int_n_idx].bitsize);
9921 if (specs->long_p)
9922 error_at (loc,
9923 ("both %<__int%d%> and %<long%> in "
9924 "declaration specifiers"),
9925 int_n_data[specs->int_n_idx].bitsize);
9926 else if (specs->saturating_p)
9927 error_at (loc,
9928 ("both %<_Sat%> and %<__int%d%> in "
9929 "declaration specifiers"),
9930 int_n_data[specs->int_n_idx].bitsize);
9931 else if (specs->short_p)
9932 error_at (loc,
9933 ("both %<__int%d%> and %<short%> in "
9934 "declaration specifiers"),
9935 int_n_data[specs->int_n_idx].bitsize);
9936 else if (! int_n_enabled_p [specs->int_n_idx])
9937 error_at (loc,
9938 "%<__int%d%> is not supported on this target",
9939 int_n_data[specs->int_n_idx].bitsize);
9940 else
9942 specs->typespec_word = cts_int_n;
9943 specs->locations[cdw_typespec] = loc;
9945 return specs;
9946 case RID_VOID:
9947 if (specs->long_p)
9948 error_at (loc,
9949 ("both %<long%> and %<void%> in "
9950 "declaration specifiers"));
9951 else if (specs->short_p)
9952 error_at (loc,
9953 ("both %<short%> and %<void%> in "
9954 "declaration specifiers"));
9955 else if (specs->signed_p)
9956 error_at (loc,
9957 ("both %<signed%> and %<void%> in "
9958 "declaration specifiers"));
9959 else if (specs->unsigned_p)
9960 error_at (loc,
9961 ("both %<unsigned%> and %<void%> in "
9962 "declaration specifiers"));
9963 else if (specs->complex_p)
9964 error_at (loc,
9965 ("both %<complex%> and %<void%> in "
9966 "declaration specifiers"));
9967 else if (specs->saturating_p)
9968 error_at (loc,
9969 ("both %<_Sat%> and %<void%> in "
9970 "declaration specifiers"));
9971 else
9973 specs->typespec_word = cts_void;
9974 specs->locations[cdw_typespec] = loc;
9976 return specs;
9977 case RID_BOOL:
9978 if (!in_system_header_at (loc))
9979 pedwarn_c90 (loc, OPT_Wpedantic,
9980 "ISO C90 does not support boolean types");
9981 if (specs->long_p)
9982 error_at (loc,
9983 ("both %<long%> and %<_Bool%> in "
9984 "declaration specifiers"));
9985 else if (specs->short_p)
9986 error_at (loc,
9987 ("both %<short%> and %<_Bool%> in "
9988 "declaration specifiers"));
9989 else if (specs->signed_p)
9990 error_at (loc,
9991 ("both %<signed%> and %<_Bool%> in "
9992 "declaration specifiers"));
9993 else if (specs->unsigned_p)
9994 error_at (loc,
9995 ("both %<unsigned%> and %<_Bool%> in "
9996 "declaration specifiers"));
9997 else if (specs->complex_p)
9998 error_at (loc,
9999 ("both %<complex%> and %<_Bool%> in "
10000 "declaration specifiers"));
10001 else if (specs->saturating_p)
10002 error_at (loc,
10003 ("both %<_Sat%> and %<_Bool%> in "
10004 "declaration specifiers"));
10005 else
10007 specs->typespec_word = cts_bool;
10008 specs->locations[cdw_typespec] = loc;
10010 return specs;
10011 case RID_CHAR:
10012 if (specs->long_p)
10013 error_at (loc,
10014 ("both %<long%> and %<char%> in "
10015 "declaration specifiers"));
10016 else if (specs->short_p)
10017 error_at (loc,
10018 ("both %<short%> and %<char%> in "
10019 "declaration specifiers"));
10020 else if (specs->saturating_p)
10021 error_at (loc,
10022 ("both %<_Sat%> and %<char%> in "
10023 "declaration specifiers"));
10024 else
10026 specs->typespec_word = cts_char;
10027 specs->locations[cdw_typespec] = loc;
10029 return specs;
10030 case RID_INT:
10031 if (specs->saturating_p)
10032 error_at (loc,
10033 ("both %<_Sat%> and %<int%> in "
10034 "declaration specifiers"));
10035 else
10037 specs->typespec_word = cts_int;
10038 specs->locations[cdw_typespec] = loc;
10040 return specs;
10041 case RID_FLOAT:
10042 if (specs->long_p)
10043 error_at (loc,
10044 ("both %<long%> and %<float%> in "
10045 "declaration specifiers"));
10046 else if (specs->short_p)
10047 error_at (loc,
10048 ("both %<short%> and %<float%> in "
10049 "declaration specifiers"));
10050 else if (specs->signed_p)
10051 error_at (loc,
10052 ("both %<signed%> and %<float%> in "
10053 "declaration specifiers"));
10054 else if (specs->unsigned_p)
10055 error_at (loc,
10056 ("both %<unsigned%> and %<float%> in "
10057 "declaration specifiers"));
10058 else if (specs->saturating_p)
10059 error_at (loc,
10060 ("both %<_Sat%> and %<float%> in "
10061 "declaration specifiers"));
10062 else
10064 specs->typespec_word = cts_float;
10065 specs->locations[cdw_typespec] = loc;
10067 return specs;
10068 case RID_DOUBLE:
10069 if (specs->long_long_p)
10070 error_at (loc,
10071 ("both %<long long%> and %<double%> in "
10072 "declaration specifiers"));
10073 else if (specs->short_p)
10074 error_at (loc,
10075 ("both %<short%> and %<double%> in "
10076 "declaration specifiers"));
10077 else if (specs->signed_p)
10078 error_at (loc,
10079 ("both %<signed%> and %<double%> in "
10080 "declaration specifiers"));
10081 else if (specs->unsigned_p)
10082 error_at (loc,
10083 ("both %<unsigned%> and %<double%> in "
10084 "declaration specifiers"));
10085 else if (specs->saturating_p)
10086 error_at (loc,
10087 ("both %<_Sat%> and %<double%> in "
10088 "declaration specifiers"));
10089 else
10091 specs->typespec_word = cts_double;
10092 specs->locations[cdw_typespec] = loc;
10094 return specs;
10095 case RID_DFLOAT32:
10096 case RID_DFLOAT64:
10097 case RID_DFLOAT128:
10099 const char *str;
10100 if (i == RID_DFLOAT32)
10101 str = "_Decimal32";
10102 else if (i == RID_DFLOAT64)
10103 str = "_Decimal64";
10104 else
10105 str = "_Decimal128";
10106 if (specs->long_long_p)
10107 error_at (loc,
10108 ("both %<long long%> and %<%s%> in "
10109 "declaration specifiers"),
10110 str);
10111 if (specs->long_p)
10112 error_at (loc,
10113 ("both %<long%> and %<%s%> in "
10114 "declaration specifiers"),
10115 str);
10116 else if (specs->short_p)
10117 error_at (loc,
10118 ("both %<short%> and %<%s%> in "
10119 "declaration specifiers"),
10120 str);
10121 else if (specs->signed_p)
10122 error_at (loc,
10123 ("both %<signed%> and %<%s%> in "
10124 "declaration specifiers"),
10125 str);
10126 else if (specs->unsigned_p)
10127 error_at (loc,
10128 ("both %<unsigned%> and %<%s%> in "
10129 "declaration specifiers"),
10130 str);
10131 else if (specs->complex_p)
10132 error_at (loc,
10133 ("both %<complex%> and %<%s%> in "
10134 "declaration specifiers"),
10135 str);
10136 else if (specs->saturating_p)
10137 error_at (loc,
10138 ("both %<_Sat%> and %<%s%> in "
10139 "declaration specifiers"),
10140 str);
10141 else if (i == RID_DFLOAT32)
10142 specs->typespec_word = cts_dfloat32;
10143 else if (i == RID_DFLOAT64)
10144 specs->typespec_word = cts_dfloat64;
10145 else
10146 specs->typespec_word = cts_dfloat128;
10147 specs->locations[cdw_typespec] = loc;
10149 if (!targetm.decimal_float_supported_p ())
10150 error_at (loc,
10151 ("decimal floating point not supported "
10152 "for this target"));
10153 pedwarn (loc, OPT_Wpedantic,
10154 "ISO C does not support decimal floating point");
10155 return specs;
10156 case RID_FRACT:
10157 case RID_ACCUM:
10159 const char *str;
10160 if (i == RID_FRACT)
10161 str = "_Fract";
10162 else
10163 str = "_Accum";
10164 if (specs->complex_p)
10165 error_at (loc,
10166 ("both %<complex%> and %<%s%> in "
10167 "declaration specifiers"),
10168 str);
10169 else if (i == RID_FRACT)
10170 specs->typespec_word = cts_fract;
10171 else
10172 specs->typespec_word = cts_accum;
10173 specs->locations[cdw_typespec] = loc;
10175 if (!targetm.fixed_point_supported_p ())
10176 error_at (loc,
10177 "fixed-point types not supported for this target");
10178 pedwarn (loc, OPT_Wpedantic,
10179 "ISO C does not support fixed-point types");
10180 return specs;
10181 default:
10182 /* ObjC reserved word "id", handled below. */
10183 break;
10188 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
10189 form of ObjC type, cases such as "int" and "long" being handled
10190 above), a TYPE (struct, union, enum and typeof specifiers) or an
10191 ERROR_MARK. In none of these cases may there have previously
10192 been any type specifiers. */
10193 if (specs->type || specs->typespec_word != cts_none
10194 || specs->long_p || specs->short_p || specs->signed_p
10195 || specs->unsigned_p || specs->complex_p)
10196 error_at (loc, "two or more data types in declaration specifiers");
10197 else if (TREE_CODE (type) == TYPE_DECL)
10199 if (TREE_TYPE (type) == error_mark_node)
10200 ; /* Allow the type to default to int to avoid cascading errors. */
10201 else
10203 specs->type = TREE_TYPE (type);
10204 specs->decl_attr = DECL_ATTRIBUTES (type);
10205 specs->typedef_p = true;
10206 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
10207 specs->locations[cdw_typedef] = loc;
10209 /* If this typedef name is defined in a struct, then a C++
10210 lookup would return a different value. */
10211 if (warn_cxx_compat
10212 && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
10213 warning_at (loc, OPT_Wc___compat,
10214 "C++ lookup of %qD would return a field, not a type",
10215 type);
10217 /* If we are parsing a struct, record that a struct field
10218 used a typedef. */
10219 if (warn_cxx_compat && struct_parse_info != NULL)
10220 struct_parse_info->typedefs_seen.safe_push (type);
10223 else if (TREE_CODE (type) == IDENTIFIER_NODE)
10225 tree t = lookup_name (type);
10226 if (!t || TREE_CODE (t) != TYPE_DECL)
10227 error_at (loc, "%qE fails to be a typedef or built in type", type);
10228 else if (TREE_TYPE (t) == error_mark_node)
10230 else
10232 specs->type = TREE_TYPE (t);
10233 specs->locations[cdw_typespec] = loc;
10236 else
10238 if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
10240 specs->typedef_p = true;
10241 specs->locations[cdw_typedef] = loc;
10242 if (spec.expr)
10244 if (specs->expr)
10245 specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
10246 specs->expr, spec.expr);
10247 else
10248 specs->expr = spec.expr;
10249 specs->expr_const_operands &= spec.expr_const_operands;
10252 specs->type = type;
10255 return specs;
10258 /* Add the storage class specifier or function specifier SCSPEC to the
10259 declaration specifiers SPECS, returning SPECS. */
10261 struct c_declspecs *
10262 declspecs_add_scspec (source_location loc,
10263 struct c_declspecs *specs,
10264 tree scspec)
10266 enum rid i;
10267 enum c_storage_class n = csc_none;
10268 bool dupe = false;
10269 specs->declspecs_seen_p = true;
10270 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
10271 && C_IS_RESERVED_WORD (scspec));
10272 i = C_RID_CODE (scspec);
10273 if (specs->non_sc_seen_p)
10274 warning (OPT_Wold_style_declaration,
10275 "%qE is not at beginning of declaration", scspec);
10276 switch (i)
10278 case RID_INLINE:
10279 /* C99 permits duplicate inline. Although of doubtful utility,
10280 it seems simplest to permit it in gnu89 mode as well, as
10281 there is also little utility in maintaining this as a
10282 difference between gnu89 and C99 inline. */
10283 dupe = false;
10284 specs->inline_p = true;
10285 specs->locations[cdw_inline] = loc;
10286 break;
10287 case RID_NORETURN:
10288 /* Duplicate _Noreturn is permitted. */
10289 dupe = false;
10290 specs->noreturn_p = true;
10291 specs->locations[cdw_noreturn] = loc;
10292 break;
10293 case RID_THREAD:
10294 dupe = specs->thread_p;
10295 if (specs->storage_class == csc_auto)
10296 error ("%qE used with %<auto%>", scspec);
10297 else if (specs->storage_class == csc_register)
10298 error ("%qE used with %<register%>", scspec);
10299 else if (specs->storage_class == csc_typedef)
10300 error ("%qE used with %<typedef%>", scspec);
10301 else
10303 specs->thread_p = true;
10304 specs->thread_gnu_p = (strcmp (IDENTIFIER_POINTER (scspec),
10305 "__thread") == 0);
10306 /* A diagnostic is not required for the use of this
10307 identifier in the implementation namespace; only diagnose
10308 it for the C11 spelling because of existing code using
10309 the other spelling. */
10310 if (!specs->thread_gnu_p)
10312 if (flag_isoc99)
10313 pedwarn_c99 (loc, OPT_Wpedantic,
10314 "ISO C99 does not support %qE", scspec);
10315 else
10316 pedwarn_c99 (loc, OPT_Wpedantic,
10317 "ISO C90 does not support %qE", scspec);
10319 specs->locations[cdw_thread] = loc;
10321 break;
10322 case RID_AUTO:
10323 n = csc_auto;
10324 break;
10325 case RID_EXTERN:
10326 n = csc_extern;
10327 /* Diagnose "__thread extern". */
10328 if (specs->thread_p && specs->thread_gnu_p)
10329 error ("%<__thread%> before %<extern%>");
10330 break;
10331 case RID_REGISTER:
10332 n = csc_register;
10333 break;
10334 case RID_STATIC:
10335 n = csc_static;
10336 /* Diagnose "__thread static". */
10337 if (specs->thread_p && specs->thread_gnu_p)
10338 error ("%<__thread%> before %<static%>");
10339 break;
10340 case RID_TYPEDEF:
10341 n = csc_typedef;
10342 break;
10343 default:
10344 gcc_unreachable ();
10346 if (n != csc_none && n == specs->storage_class)
10347 dupe = true;
10348 if (dupe)
10350 if (i == RID_THREAD)
10351 error ("duplicate %<_Thread_local%> or %<__thread%>");
10352 else
10353 error ("duplicate %qE", scspec);
10355 if (n != csc_none)
10357 if (specs->storage_class != csc_none && n != specs->storage_class)
10359 error ("multiple storage classes in declaration specifiers");
10361 else
10363 specs->storage_class = n;
10364 specs->locations[cdw_storage_class] = loc;
10365 if (n != csc_extern && n != csc_static && specs->thread_p)
10367 error ("%qs used with %qE",
10368 specs->thread_gnu_p ? "__thread" : "_Thread_local",
10369 scspec);
10370 specs->thread_p = false;
10374 return specs;
10377 /* Add the attributes ATTRS to the declaration specifiers SPECS,
10378 returning SPECS. */
10380 struct c_declspecs *
10381 declspecs_add_attrs (source_location loc, struct c_declspecs *specs, tree attrs)
10383 specs->attrs = chainon (attrs, specs->attrs);
10384 specs->locations[cdw_attributes] = loc;
10385 specs->declspecs_seen_p = true;
10386 return specs;
10389 /* Add an _Alignas specifier (expression ALIGN, or type whose
10390 alignment is ALIGN) to the declaration specifiers SPECS, returning
10391 SPECS. */
10392 struct c_declspecs *
10393 declspecs_add_alignas (source_location loc,
10394 struct c_declspecs *specs, tree align)
10396 int align_log;
10397 specs->alignas_p = true;
10398 specs->locations[cdw_alignas] = loc;
10399 if (align == error_mark_node)
10400 return specs;
10401 align_log = check_user_alignment (align, true);
10402 if (align_log > specs->align_log)
10403 specs->align_log = align_log;
10404 return specs;
10407 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
10408 specifiers with any other type specifier to determine the resulting
10409 type. This is where ISO C checks on complex types are made, since
10410 "_Complex long" is a prefix of the valid ISO C type "_Complex long
10411 double". */
10413 struct c_declspecs *
10414 finish_declspecs (struct c_declspecs *specs)
10416 /* If a type was specified as a whole, we have no modifiers and are
10417 done. */
10418 if (specs->type != NULL_TREE)
10420 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10421 && !specs->signed_p && !specs->unsigned_p
10422 && !specs->complex_p);
10424 /* Set a dummy type. */
10425 if (TREE_CODE (specs->type) == ERROR_MARK)
10426 specs->type = integer_type_node;
10427 return specs;
10430 /* If none of "void", "_Bool", "char", "int", "float" or "double"
10431 has been specified, treat it as "int" unless "_Complex" is
10432 present and there are no other specifiers. If we just have
10433 "_Complex", it is equivalent to "_Complex double", but e.g.
10434 "_Complex short" is equivalent to "_Complex short int". */
10435 if (specs->typespec_word == cts_none)
10437 if (specs->saturating_p)
10439 error_at (specs->locations[cdw_saturating],
10440 "%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
10441 if (!targetm.fixed_point_supported_p ())
10442 error_at (specs->locations[cdw_saturating],
10443 "fixed-point types not supported for this target");
10444 specs->typespec_word = cts_fract;
10446 else if (specs->long_p || specs->short_p
10447 || specs->signed_p || specs->unsigned_p)
10449 specs->typespec_word = cts_int;
10451 else if (specs->complex_p)
10453 specs->typespec_word = cts_double;
10454 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10455 "ISO C does not support plain %<complex%> meaning "
10456 "%<double complex%>");
10458 else
10460 specs->typespec_word = cts_int;
10461 specs->default_int_p = true;
10462 /* We don't diagnose this here because grokdeclarator will
10463 give more specific diagnostics according to whether it is
10464 a function definition. */
10468 /* If "signed" was specified, record this to distinguish "int" and
10469 "signed int" in the case of a bit-field with
10470 -funsigned-bitfields. */
10471 specs->explicit_signed_p = specs->signed_p;
10473 /* Now compute the actual type. */
10474 switch (specs->typespec_word)
10476 case cts_auto_type:
10477 gcc_assert (!specs->long_p && !specs->short_p
10478 && !specs->signed_p && !specs->unsigned_p
10479 && !specs->complex_p);
10480 /* Type to be filled in later. */
10481 break;
10482 case cts_void:
10483 gcc_assert (!specs->long_p && !specs->short_p
10484 && !specs->signed_p && !specs->unsigned_p
10485 && !specs->complex_p);
10486 specs->type = void_type_node;
10487 break;
10488 case cts_bool:
10489 gcc_assert (!specs->long_p && !specs->short_p
10490 && !specs->signed_p && !specs->unsigned_p
10491 && !specs->complex_p);
10492 specs->type = boolean_type_node;
10493 break;
10494 case cts_char:
10495 gcc_assert (!specs->long_p && !specs->short_p);
10496 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10497 if (specs->signed_p)
10498 specs->type = signed_char_type_node;
10499 else if (specs->unsigned_p)
10500 specs->type = unsigned_char_type_node;
10501 else
10502 specs->type = char_type_node;
10503 if (specs->complex_p)
10505 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10506 "ISO C does not support complex integer types");
10507 specs->type = build_complex_type (specs->type);
10509 break;
10510 case cts_int_n:
10511 gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
10512 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10513 specs->type = (specs->unsigned_p
10514 ? int_n_trees[specs->int_n_idx].unsigned_type
10515 : int_n_trees[specs->int_n_idx].signed_type);
10516 if (specs->complex_p)
10518 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10519 "ISO C does not support complex integer types");
10520 specs->type = build_complex_type (specs->type);
10522 break;
10523 case cts_int:
10524 gcc_assert (!(specs->long_p && specs->short_p));
10525 gcc_assert (!(specs->signed_p && specs->unsigned_p));
10526 if (specs->long_long_p)
10527 specs->type = (specs->unsigned_p
10528 ? long_long_unsigned_type_node
10529 : long_long_integer_type_node);
10530 else if (specs->long_p)
10531 specs->type = (specs->unsigned_p
10532 ? long_unsigned_type_node
10533 : long_integer_type_node);
10534 else if (specs->short_p)
10535 specs->type = (specs->unsigned_p
10536 ? short_unsigned_type_node
10537 : short_integer_type_node);
10538 else
10539 specs->type = (specs->unsigned_p
10540 ? unsigned_type_node
10541 : integer_type_node);
10542 if (specs->complex_p)
10544 pedwarn (specs->locations[cdw_complex], OPT_Wpedantic,
10545 "ISO C does not support complex integer types");
10546 specs->type = build_complex_type (specs->type);
10548 break;
10549 case cts_float:
10550 gcc_assert (!specs->long_p && !specs->short_p
10551 && !specs->signed_p && !specs->unsigned_p);
10552 specs->type = (specs->complex_p
10553 ? complex_float_type_node
10554 : float_type_node);
10555 break;
10556 case cts_double:
10557 gcc_assert (!specs->long_long_p && !specs->short_p
10558 && !specs->signed_p && !specs->unsigned_p);
10559 if (specs->long_p)
10561 specs->type = (specs->complex_p
10562 ? complex_long_double_type_node
10563 : long_double_type_node);
10565 else
10567 specs->type = (specs->complex_p
10568 ? complex_double_type_node
10569 : double_type_node);
10571 break;
10572 case cts_dfloat32:
10573 case cts_dfloat64:
10574 case cts_dfloat128:
10575 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
10576 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
10577 if (specs->typespec_word == cts_dfloat32)
10578 specs->type = dfloat32_type_node;
10579 else if (specs->typespec_word == cts_dfloat64)
10580 specs->type = dfloat64_type_node;
10581 else
10582 specs->type = dfloat128_type_node;
10583 break;
10584 case cts_fract:
10585 gcc_assert (!specs->complex_p);
10586 if (!targetm.fixed_point_supported_p ())
10587 specs->type = integer_type_node;
10588 else if (specs->saturating_p)
10590 if (specs->long_long_p)
10591 specs->type = specs->unsigned_p
10592 ? sat_unsigned_long_long_fract_type_node
10593 : sat_long_long_fract_type_node;
10594 else if (specs->long_p)
10595 specs->type = specs->unsigned_p
10596 ? sat_unsigned_long_fract_type_node
10597 : sat_long_fract_type_node;
10598 else if (specs->short_p)
10599 specs->type = specs->unsigned_p
10600 ? sat_unsigned_short_fract_type_node
10601 : sat_short_fract_type_node;
10602 else
10603 specs->type = specs->unsigned_p
10604 ? sat_unsigned_fract_type_node
10605 : sat_fract_type_node;
10607 else
10609 if (specs->long_long_p)
10610 specs->type = specs->unsigned_p
10611 ? unsigned_long_long_fract_type_node
10612 : long_long_fract_type_node;
10613 else if (specs->long_p)
10614 specs->type = specs->unsigned_p
10615 ? unsigned_long_fract_type_node
10616 : long_fract_type_node;
10617 else if (specs->short_p)
10618 specs->type = specs->unsigned_p
10619 ? unsigned_short_fract_type_node
10620 : short_fract_type_node;
10621 else
10622 specs->type = specs->unsigned_p
10623 ? unsigned_fract_type_node
10624 : fract_type_node;
10626 break;
10627 case cts_accum:
10628 gcc_assert (!specs->complex_p);
10629 if (!targetm.fixed_point_supported_p ())
10630 specs->type = integer_type_node;
10631 else if (specs->saturating_p)
10633 if (specs->long_long_p)
10634 specs->type = specs->unsigned_p
10635 ? sat_unsigned_long_long_accum_type_node
10636 : sat_long_long_accum_type_node;
10637 else if (specs->long_p)
10638 specs->type = specs->unsigned_p
10639 ? sat_unsigned_long_accum_type_node
10640 : sat_long_accum_type_node;
10641 else if (specs->short_p)
10642 specs->type = specs->unsigned_p
10643 ? sat_unsigned_short_accum_type_node
10644 : sat_short_accum_type_node;
10645 else
10646 specs->type = specs->unsigned_p
10647 ? sat_unsigned_accum_type_node
10648 : sat_accum_type_node;
10650 else
10652 if (specs->long_long_p)
10653 specs->type = specs->unsigned_p
10654 ? unsigned_long_long_accum_type_node
10655 : long_long_accum_type_node;
10656 else if (specs->long_p)
10657 specs->type = specs->unsigned_p
10658 ? unsigned_long_accum_type_node
10659 : long_accum_type_node;
10660 else if (specs->short_p)
10661 specs->type = specs->unsigned_p
10662 ? unsigned_short_accum_type_node
10663 : short_accum_type_node;
10664 else
10665 specs->type = specs->unsigned_p
10666 ? unsigned_accum_type_node
10667 : accum_type_node;
10669 break;
10670 default:
10671 gcc_unreachable ();
10674 return specs;
10677 /* Perform final processing on one file scope's declarations (or the
10678 external scope's declarations), GLOBALS. */
10680 static void
10681 c_write_global_declarations_1 (tree globals)
10683 tree decl;
10684 bool reconsider;
10686 /* Process the decls in the order they were written. */
10687 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10689 /* Check for used but undefined static functions using the C
10690 standard's definition of "used", and set TREE_NO_WARNING so
10691 that check_global_declaration doesn't repeat the check. */
10692 if (TREE_CODE (decl) == FUNCTION_DECL
10693 && DECL_INITIAL (decl) == 0
10694 && DECL_EXTERNAL (decl)
10695 && !TREE_PUBLIC (decl)
10696 && C_DECL_USED (decl))
10698 pedwarn (input_location, 0, "%q+F used but never defined", decl);
10699 TREE_NO_WARNING (decl) = 1;
10702 wrapup_global_declaration_1 (decl);
10707 reconsider = false;
10708 for (decl = globals; decl; decl = DECL_CHAIN (decl))
10709 reconsider |= wrapup_global_declaration_2 (decl);
10711 while (reconsider);
10714 /* Callback to collect a source_ref from a DECL. */
10716 static void
10717 collect_source_ref_cb (tree decl)
10719 if (!DECL_IS_BUILTIN (decl))
10720 collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
10723 /* Preserve the external declarations scope across a garbage collect. */
10724 static GTY(()) tree ext_block;
10726 /* Collect all references relevant to SOURCE_FILE. */
10728 static void
10729 collect_all_refs (const char *source_file)
10731 tree t;
10732 unsigned i;
10734 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10735 collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
10737 collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
10740 /* Iterate over all global declarations and call CALLBACK. */
10742 static void
10743 for_each_global_decl (void (*callback) (tree decl))
10745 tree t;
10746 tree decls;
10747 tree decl;
10748 unsigned i;
10750 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10752 decls = DECL_INITIAL (t);
10753 for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
10754 callback (decl);
10757 for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
10758 callback (decl);
10761 /* Perform any final parser cleanups and generate initial debugging
10762 information. */
10764 void
10765 c_parse_final_cleanups (void)
10767 tree t;
10768 unsigned i;
10770 /* We don't want to do this if generating a PCH. */
10771 if (pch_file)
10772 return;
10774 timevar_stop (TV_PHASE_PARSING);
10775 timevar_start (TV_PHASE_DEFERRED);
10777 /* Do the Objective-C stuff. This is where all the Objective-C
10778 module stuff gets generated (symtab, class/protocol/selector
10779 lists etc). */
10780 if (c_dialect_objc ())
10781 objc_write_global_declarations ();
10783 /* Close the external scope. */
10784 ext_block = pop_scope ();
10785 external_scope = 0;
10786 gcc_assert (!current_scope);
10788 /* Handle -fdump-ada-spec[-slim]. */
10789 if (flag_dump_ada_spec || flag_dump_ada_spec_slim)
10791 /* Build a table of files to generate specs for */
10792 if (flag_dump_ada_spec_slim)
10793 collect_source_ref (main_input_filename);
10794 else
10795 for_each_global_decl (collect_source_ref_cb);
10797 dump_ada_specs (collect_all_refs, NULL);
10800 if (ext_block)
10802 tree tmp = BLOCK_VARS (ext_block);
10803 int flags;
10804 FILE * stream = dump_begin (TDI_tu, &flags);
10805 if (stream && tmp)
10807 dump_node (tmp, flags & ~TDF_SLIM, stream);
10808 dump_end (TDI_tu, stream);
10812 /* Process all file scopes in this compilation, and the external_scope,
10813 through wrapup_global_declarations. */
10814 FOR_EACH_VEC_ELT (*all_translation_units, i, t)
10815 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10816 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10818 timevar_stop (TV_PHASE_DEFERRED);
10819 timevar_start (TV_PHASE_PARSING);
10821 ext_block = NULL;
10824 /* Register reserved keyword WORD as qualifier for address space AS. */
10826 void
10827 c_register_addr_space (const char *word, addr_space_t as)
10829 int rid = RID_FIRST_ADDR_SPACE + as;
10830 tree id;
10832 /* Address space qualifiers are only supported
10833 in C with GNU extensions enabled. */
10834 if (c_dialect_objc () || flag_no_asm)
10835 return;
10837 id = get_identifier (word);
10838 C_SET_RID_CODE (id, rid);
10839 C_IS_RESERVED_WORD (id) = 1;
10840 ridpointers [rid] = id;
10843 /* Return identifier to look up for omp declare reduction. */
10845 tree
10846 c_omp_reduction_id (enum tree_code reduction_code, tree reduction_id)
10848 const char *p = NULL;
10849 switch (reduction_code)
10851 case PLUS_EXPR: p = "+"; break;
10852 case MULT_EXPR: p = "*"; break;
10853 case MINUS_EXPR: p = "-"; break;
10854 case BIT_AND_EXPR: p = "&"; break;
10855 case BIT_XOR_EXPR: p = "^"; break;
10856 case BIT_IOR_EXPR: p = "|"; break;
10857 case TRUTH_ANDIF_EXPR: p = "&&"; break;
10858 case TRUTH_ORIF_EXPR: p = "||"; break;
10859 case MIN_EXPR: p = "min"; break;
10860 case MAX_EXPR: p = "max"; break;
10861 default:
10862 break;
10865 if (p == NULL)
10867 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
10868 return error_mark_node;
10869 p = IDENTIFIER_POINTER (reduction_id);
10872 const char prefix[] = "omp declare reduction ";
10873 size_t lenp = sizeof (prefix);
10874 size_t len = strlen (p);
10875 char *name = XALLOCAVEC (char, lenp + len);
10876 memcpy (name, prefix, lenp - 1);
10877 memcpy (name + lenp - 1, p, len + 1);
10878 return get_identifier (name);
10881 /* Lookup REDUCTION_ID in the current scope, or create an artificial
10882 VAR_DECL, bind it into the current scope and return it. */
10884 tree
10885 c_omp_reduction_decl (tree reduction_id)
10887 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10888 if (b != NULL && B_IN_CURRENT_SCOPE (b))
10889 return b->decl;
10891 tree decl = build_decl (BUILTINS_LOCATION, VAR_DECL,
10892 reduction_id, integer_type_node);
10893 DECL_ARTIFICIAL (decl) = 1;
10894 DECL_EXTERNAL (decl) = 1;
10895 TREE_STATIC (decl) = 1;
10896 TREE_PUBLIC (decl) = 0;
10897 bind (reduction_id, decl, current_scope, true, false, BUILTINS_LOCATION);
10898 return decl;
10901 /* Lookup REDUCTION_ID in the first scope where it has entry for TYPE. */
10903 tree
10904 c_omp_reduction_lookup (tree reduction_id, tree type)
10906 struct c_binding *b = I_SYMBOL_BINDING (reduction_id);
10907 while (b)
10909 tree t;
10910 for (t = DECL_INITIAL (b->decl); t; t = TREE_CHAIN (t))
10911 if (comptypes (TREE_PURPOSE (t), type))
10912 return TREE_VALUE (t);
10913 b = b->shadowed;
10915 return error_mark_node;
10918 /* Helper function called via walk_tree, to diagnose invalid
10919 #pragma omp declare reduction combiners or initializers. */
10921 tree
10922 c_check_omp_declare_reduction_r (tree *tp, int *, void *data)
10924 tree *vars = (tree *) data;
10925 if (SSA_VAR_P (*tp)
10926 && !DECL_ARTIFICIAL (*tp)
10927 && *tp != vars[0]
10928 && *tp != vars[1])
10930 location_t loc = DECL_SOURCE_LOCATION (vars[0]);
10931 if (strcmp (IDENTIFIER_POINTER (DECL_NAME (vars[0])), "omp_out") == 0)
10932 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
10933 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
10934 *tp);
10935 else
10936 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
10937 "to variable %qD which is not %<omp_priv%> nor "
10938 "%<omp_orig%>",
10939 *tp);
10940 return *tp;
10942 return NULL_TREE;
10945 #include "gt-c-c-decl.h"