1/* Process declarations and variables for C compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
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. */
23
24/* ??? not all decl nodes are given the most useful possible
25 line numbers. For example, the CONST_DECLs for enum values. */
26
27#include "config.h"
28#define INCLUDE_UNIQUE_PTR
29#include "system.h"
30#include "coretypes.h"
31#include "target.h"
32#include "function.h"
33#include "c-tree.h"
34#include "timevar.h"
35#include "stringpool.h"
36#include "cgraph.h"
37#include "intl.h"
38#include "print-tree.h"
39#include "stor-layout.h"
40#include "varasm.h"
41#include "attribs.h"
42#include "toplev.h"
43#include "debug.h"
44#include "c-family/c-objc.h"
45#include "c-family/c-pragma.h"
46#include "c-family/c-ubsan.h"
47#include "c-lang.h"
48#include "langhooks.h"
49#include "tree-iterator.h"
50#include "dumpfile.h"
51#include "plugin.h"
52#include "c-family/c-ada-spec.h"
53#include "builtins.h"
54#include "spellcheck-tree.h"
55#include "gcc-rich-location.h"
56#include "asan.h"
57#include "c-family/name-hint.h"
58#include "c-family/known-headers.h"
59#include "c-family/c-spellcheck.h"
60
61/* In grokdeclarator, distinguish syntactic contexts of declarators. */
62enum decl_context
63{ NORMAL, /* Ordinary declaration */
64 FUNCDEF, /* Function definition */
65 PARM, /* Declaration of parm before function body */
66 FIELD, /* Declaration inside struct or union */
67 TYPENAME}; /* Typename (inside cast or sizeof) */
68
69/* States indicating how grokdeclarator() should handle declspecs marked
70 with __attribute__((deprecated)). An object declared as
71 __attribute__((deprecated)) suppresses warnings of uses of other
72 deprecated items. */
73
74enum deprecated_states {
75 DEPRECATED_NORMAL,
76 DEPRECATED_SUPPRESS
77};
78
79
80/* Nonzero if we have seen an invalid cross reference
81 to a struct, union, or enum, but not yet printed the message. */
82tree pending_invalid_xref;
83
84/* File and line to appear in the eventual error message. */
85location_t pending_invalid_xref_location;
86
87/* The file and line that the prototype came from if this is an
88 old-style definition; used for diagnostics in
89 store_parm_decls_oldstyle. */
90
91static location_t current_function_prototype_locus;
92
93/* Whether this prototype was built-in. */
94
95static bool current_function_prototype_built_in;
96
97/* The argument type information of this prototype. */
98
99static tree current_function_prototype_arg_types;
100
101/* The argument information structure for the function currently being
102 defined. */
103
104static struct c_arg_info *current_function_arg_info;
105
106/* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109struct obstack parser_obstack;
110
111/* The current statement tree. */
112
113static GTY(()) struct stmt_tree_s c_stmt_tree;
114
115/* State saving variables. */
116tree c_break_label;
117tree c_cont_label;
118
119/* A list of decls to be made automatically visible in each file scope. */
120static GTY(()) tree visible_builtins;
121
122/* Set to 0 at beginning of a function definition, set to 1 if
123 a return statement that specifies a return value is seen. */
124
125int current_function_returns_value;
126
127/* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement with no argument is seen. */
129
130int current_function_returns_null;
131
132/* Set to 0 at beginning of a function definition, set to 1 if
133 a call to a noreturn function is seen. */
134
135int current_function_returns_abnormally;
136
137/* Set to nonzero by `grokdeclarator' for a function
138 whose return type is defaulted, if warnings for this are desired. */
139
140static int warn_about_return_type;
141
142/* Nonzero when the current toplevel function contains a declaration
143 of a nested function which is never defined. */
144
145static bool undef_nested_function;
146
147/* If non-zero, implicit "omp declare target" attribute is added into the
148 attribute lists. */
149int current_omp_declare_target_attribute;
150
151/* Each c_binding structure describes one binding of an identifier to
152 a decl. All the decls in a scope - irrespective of namespace - are
153 chained together by the ->prev field, which (as the name implies)
154 runs in reverse order. All the decls in a given namespace bound to
155 a given identifier are chained by the ->shadowed field, which runs
156 from inner to outer scopes.
157
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
163 function.
164
165 The ->u.type field stores the type of the declaration in this scope;
166 if NULL, the type is the type of the ->decl field. This is only of
167 relevance for objects with external or internal linkage which may
168 be redeclared in inner scopes, forming composite types that only
169 persist for the duration of those scopes. In the external scope,
170 this stores the composite of all the types declared for this
171 object, visible or not. The ->inner_comp field (used only at file
172 scope) stores whether an incomplete array type at file scope was
173 completed at an inner scope to an array size other than 1.
174
175 The ->u.label field is used for labels. It points to a structure
176 which stores additional information used for warnings.
177
178 The depth field is copied from the scope structure that holds this
179 decl. It is used to preserve the proper ordering of the ->shadowed
180 field (see bind()) and also for a handful of special-case checks.
181 Finally, the invisible bit is true for a decl which should be
182 ignored for purposes of normal name lookup, and the nested bit is
183 true for a decl that's been bound a second time in an inner scope;
184 in all such cases, the binding in the outer scope will have its
185 invisible bit true. */
186
187struct GTY((chain_next ("%h.prev"))) c_binding {
188 union GTY(()) { /* first so GTY desc can use decl */
189 tree GTY((tag ("0"))) type; /* the type in this scope */
190 struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
191 } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
192 tree decl; /* the decl bound */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
201 location_t locus; /* location for nested bindings */
202};
203#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
204#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
205#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
206#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
207
208/* Each C symbol points to three linked lists of c_binding structures.
209 These describe the values of the identifier in the three different
210 namespaces defined by the language. */
211
212struct GTY(()) lang_identifier {
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
217};
218
219/* Validate c-lang.c's assumptions. */
220extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
222
223/* The binding oracle; see c-tree.h. */
224void (*c_binding_oracle) (enum c_oracle_request, tree identifier);
225
226/* This flag is set on an identifier if we have previously asked the
227 binding oracle for this identifier's symbol binding. */
228#define I_SYMBOL_CHECKED(node) \
229 (TREE_LANG_FLAG_4 (IDENTIFIER_NODE_CHECK (node)))
230
231static inline struct c_binding* *
232i_symbol_binding (tree node)
233{
234 struct lang_identifier *lid
235 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
236
237 if (lid->symbol_binding == NULL
238 && c_binding_oracle != NULL
239 && !I_SYMBOL_CHECKED (node))
240 {
241 /* Set the "checked" flag first, to avoid infinite recursion
242 when the binding oracle calls back into gcc. */
243 I_SYMBOL_CHECKED (node) = 1;
244 c_binding_oracle (C_ORACLE_SYMBOL, node);
245 }
246
247 return &lid->symbol_binding;
248}
249
250#define I_SYMBOL_BINDING(node) (*i_symbol_binding (node))
251
252#define I_SYMBOL_DECL(node) \
253 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
254
255/* This flag is set on an identifier if we have previously asked the
256 binding oracle for this identifier's tag binding. */
257#define I_TAG_CHECKED(node) \
258 (TREE_LANG_FLAG_5 (IDENTIFIER_NODE_CHECK (node)))
259
260static inline struct c_binding **
261i_tag_binding (tree node)
262{
263 struct lang_identifier *lid
264 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
265
266 if (lid->tag_binding == NULL
267 && c_binding_oracle != NULL
268 && !I_TAG_CHECKED (node))
269 {
270 /* Set the "checked" flag first, to avoid infinite recursion
271 when the binding oracle calls back into gcc. */
272 I_TAG_CHECKED (node) = 1;
273 c_binding_oracle (C_ORACLE_TAG, node);
274 }
275
276 return &lid->tag_binding;
277}
278
279#define I_TAG_BINDING(node) (*i_tag_binding (node))
280
281#define I_TAG_DECL(node) \
282 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
283
284/* This flag is set on an identifier if we have previously asked the
285 binding oracle for this identifier's label binding. */
286#define I_LABEL_CHECKED(node) \
287 (TREE_LANG_FLAG_6 (IDENTIFIER_NODE_CHECK (node)))
288
289static inline struct c_binding **
290i_label_binding (tree node)
291{
292 struct lang_identifier *lid
293 = (struct lang_identifier *) IDENTIFIER_NODE_CHECK (node);
294
295 if (lid->label_binding == NULL
296 && c_binding_oracle != NULL
297 && !I_LABEL_CHECKED (node))
298 {
299 /* Set the "checked" flag first, to avoid infinite recursion
300 when the binding oracle calls back into gcc. */
301 I_LABEL_CHECKED (node) = 1;
302 c_binding_oracle (C_ORACLE_LABEL, node);
303 }
304
305 return &lid->label_binding;
306}
307
308#define I_LABEL_BINDING(node) (*i_label_binding (node))
309
310#define I_LABEL_DECL(node) \
311 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
312
313/* The resulting tree type. */
314
315union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
316 chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
317 {
318 union tree_node GTY ((tag ("0"),
319 desc ("tree_node_structure (&%h)")))
320 generic;
321 struct lang_identifier GTY ((tag ("1"))) identifier;
322};
323
324/* Track bindings and other things that matter for goto warnings. For
325 efficiency, we do not gather all the decls at the point of
326 definition. Instead, we point into the bindings structure. As
327 scopes are popped, we update these structures and gather the decls
328 that matter at that time. */
329
330struct GTY(()) c_spot_bindings {
331 /* The currently open scope which holds bindings defined when the
332 label was defined or the goto statement was found. */
333 struct c_scope *scope;
334 /* The bindings in the scope field which were defined at the point
335 of the label or goto. This lets us look at older or newer
336 bindings in the scope, as appropriate. */
337 struct c_binding *bindings_in_scope;
338 /* The number of statement expressions that have started since this
339 label or goto statement was defined. This is zero if we are at
340 the same statement expression level. It is positive if we are in
341 a statement expression started since this spot. It is negative
342 if this spot was in a statement expression and we have left
343 it. */
344 int stmt_exprs;
345 /* Whether we started in a statement expression but are no longer in
346 it. This is set to true if stmt_exprs ever goes negative. */
347 bool left_stmt_expr;
348};
349
350/* This structure is used to keep track of bindings seen when a goto
351 statement is defined. This is only used if we see the goto
352 statement before we see the label. */
353
354struct GTY(()) c_goto_bindings {
355 /* The location of the goto statement. */
356 location_t loc;
357 /* The bindings of the goto statement. */
358 struct c_spot_bindings goto_bindings;
359};
360
361typedef struct c_goto_bindings *c_goto_bindings_p;
362
363/* The additional information we keep track of for a label binding.
364 These fields are updated as scopes are popped. */
365
366struct GTY(()) c_label_vars {
367 /* The shadowed c_label_vars, when one label shadows another (which
368 can only happen using a __label__ declaration). */
369 struct c_label_vars *shadowed;
370 /* The bindings when the label was defined. */
371 struct c_spot_bindings label_bindings;
372 /* A list of decls that we care about: decls about which we should
373 warn if a goto branches to this label from later in the function.
374 Decls are added to this list as scopes are popped. We only add
375 the decls that matter. */
376 vec<tree, va_gc> *decls_in_scope;
377 /* A list of goto statements to this label. This is only used for
378 goto statements seen before the label was defined, so that we can
379 issue appropriate warnings for them. */
380 vec<c_goto_bindings_p, va_gc> *gotos;
381};
382
383/* Each c_scope structure describes the complete contents of one
384 scope. Four scopes are distinguished specially: the innermost or
385 current scope, the innermost function scope, the file scope (always
386 the second to outermost) and the outermost or external scope.
387
388 Most declarations are recorded in the current scope.
389
390 All normal label declarations are recorded in the innermost
391 function scope, as are bindings of undeclared identifiers to
392 error_mark_node. (GCC permits nested functions as an extension,
393 hence the 'innermost' qualifier.) Explicitly declared labels
394 (using the __label__ extension) appear in the current scope.
395
396 Being in the file scope (current_scope == file_scope) causes
397 special behavior in several places below. Also, under some
398 conditions the Objective-C front end records declarations in the
399 file scope even though that isn't the current scope.
400
401 All declarations with external linkage are recorded in the external
402 scope, even if they aren't visible there; this models the fact that
403 such declarations are visible to the entire program, and (with a
404 bit of cleverness, see pushdecl) allows diagnosis of some violations
405 of C99 6.2.2p7 and 6.2.7p2:
406
407 If, within the same translation unit, the same identifier appears
408 with both internal and external linkage, the behavior is
409 undefined.
410
411 All declarations that refer to the same object or function shall
412 have compatible type; otherwise, the behavior is undefined.
413
414 Initially only the built-in declarations, which describe compiler
415 intrinsic functions plus a subset of the standard library, are in
416 this scope.
417
418 The order of the blocks list matters, and it is frequently appended
419 to. To avoid having to walk all the way to the end of the list on
420 each insertion, or reverse the list later, we maintain a pointer to
421 the last list entry. (FIXME: It should be feasible to use a reversed
422 list here.)
423
424 The bindings list is strictly in reverse order of declarations;
425 pop_scope relies on this. */
426
427
428struct GTY((chain_next ("%h.outer"))) c_scope {
429 /* The scope containing this one. */
430 struct c_scope *outer;
431
432 /* The next outermost function scope. */
433 struct c_scope *outer_function;
434
435 /* All bindings in this scope. */
436 struct c_binding *bindings;
437
438 /* For each scope (except the global one), a chain of BLOCK nodes
439 for all the scopes that were entered and exited one level down. */
440 tree blocks;
441 tree blocks_last;
442
443 /* The depth of this scope. Used to keep the ->shadowed chain of
444 bindings sorted innermost to outermost. */
445 unsigned int depth : 28;
446
447 /* True if we are currently filling this scope with parameter
448 declarations. */
449 BOOL_BITFIELD parm_flag : 1;
450
451 /* True if we saw [*] in this scope. Used to give an error messages
452 if these appears in a function definition. */
453 BOOL_BITFIELD had_vla_unspec : 1;
454
455 /* True if we already complained about forward parameter decls
456 in this scope. This prevents double warnings on
457 foo (int a; int b; ...) */
458 BOOL_BITFIELD warned_forward_parm_decls : 1;
459
460 /* True if this is the outermost block scope of a function body.
461 This scope contains the parameters, the local variables declared
462 in the outermost block, and all the labels (except those in
463 nested functions, or declared at block scope with __label__). */
464 BOOL_BITFIELD function_body : 1;
465
466 /* True means make a BLOCK for this scope no matter what. */
467 BOOL_BITFIELD keep : 1;
468
469 /* True means that an unsuffixed float constant is _Decimal64. */
470 BOOL_BITFIELD float_const_decimal64 : 1;
471
472 /* True if this scope has any label bindings. This is used to speed
473 up searching for labels when popping scopes, particularly since
474 labels are normally only found at function scope. */
475 BOOL_BITFIELD has_label_bindings : 1;
476
477 /* True if we should issue a warning if a goto statement crosses any
478 of the bindings. We still need to check the list of bindings to
479 find the specific ones we need to warn about. This is true if
480 decl_jump_unsafe would return true for any of the bindings. This
481 is used to avoid looping over all the bindings unnecessarily. */
482 BOOL_BITFIELD has_jump_unsafe_decl : 1;
483};
484
485/* The scope currently in effect. */
486
487static GTY(()) struct c_scope *current_scope;
488
489/* The innermost function scope. Ordinary (not explicitly declared)
490 labels, bindings to error_mark_node, and the lazily-created
491 bindings of __func__ and its friends get this scope. */
492
493static GTY(()) struct c_scope *current_function_scope;
494
495/* The C file scope. This is reset for each input translation unit. */
496
497static GTY(()) struct c_scope *file_scope;
498
499/* The outermost scope. This is used for all declarations with
500 external linkage, and only these, hence the name. */
501
502static GTY(()) struct c_scope *external_scope;
503
504/* A chain of c_scope structures awaiting reuse. */
505
506static GTY((deletable)) struct c_scope *scope_freelist;
507
508/* A chain of c_binding structures awaiting reuse. */
509
510static GTY((deletable)) struct c_binding *binding_freelist;
511
512/* Append VAR to LIST in scope SCOPE. */
513#define SCOPE_LIST_APPEND(scope, list, decl) do { \
514 struct c_scope *s_ = (scope); \
515 tree d_ = (decl); \
516 if (s_->list##_last) \
517 BLOCK_CHAIN (s_->list##_last) = d_; \
518 else \
519 s_->list = d_; \
520 s_->list##_last = d_; \
521} while (0)
522
523/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
524#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
525 struct c_scope *t_ = (tscope); \
526 struct c_scope *f_ = (fscope); \
527 if (t_->to##_last) \
528 BLOCK_CHAIN (t_->to##_last) = f_->from; \
529 else \
530 t_->to = f_->from; \
531 t_->to##_last = f_->from##_last; \
532} while (0)
533
534/* A c_inline_static structure stores details of a static identifier
535 referenced in a definition of a function that may be an inline
536 definition if no subsequent declaration of that function uses
537 "extern" or does not use "inline". */
538
539struct GTY((chain_next ("%h.next"))) c_inline_static {
540 /* The location for a diagnostic. */
541 location_t location;
542
543 /* The function that may be an inline definition. */
544 tree function;
545
546 /* The object or function referenced. */
547 tree static_decl;
548
549 /* What sort of reference this is. */
550 enum c_inline_static_type type;
551
552 /* The next such structure or NULL. */
553 struct c_inline_static *next;
554};
555
556/* List of static identifiers used or referenced in functions that may
557 be inline definitions. */
558static GTY(()) struct c_inline_static *c_inline_statics;
559
560/* True means unconditionally make a BLOCK for the next scope pushed. */
561
562static bool keep_next_level_flag;
563
564/* True means the next call to push_scope will be the outermost scope
565 of a function body, so do not push a new scope, merely cease
566 expecting parameter decls. */
567
568static bool next_is_function_body;
569
570/* A vector of pointers to c_binding structures. */
571
572typedef struct c_binding *c_binding_ptr;
573
574/* Information that we keep for a struct or union while it is being
575 parsed. */
576
577struct c_struct_parse_info
578{
579 /* If warn_cxx_compat, a list of types defined within this
580 struct. */
581 auto_vec<tree> struct_types;
582 /* If warn_cxx_compat, a list of field names which have bindings,
583 and which are defined in this struct, but which are not defined
584 in any enclosing struct. This is used to clear the in_struct
585 field of the c_bindings structure. */
586 auto_vec<c_binding_ptr> fields;
587 /* If warn_cxx_compat, a list of typedef names used when defining
588 fields in this struct. */
589 auto_vec<tree> typedefs_seen;
590};
591
592/* Information for the struct or union currently being parsed, or
593 NULL if not parsing a struct or union. */
594static struct c_struct_parse_info *struct_parse_info;
595
596/* Forward declarations. */
597static tree lookup_name_in_scope (tree, struct c_scope *);
598static tree c_make_fname_decl (location_t, tree, int);
599static tree grokdeclarator (const struct c_declarator *,
600 struct c_declspecs *,
601 enum decl_context, bool, tree *, tree *, tree *,
602 bool *, enum deprecated_states);
603static tree grokparms (struct c_arg_info *, bool);
604static void layout_array_type (tree);
605static void warn_defaults_to (location_t, int, const char *, ...)
606 ATTRIBUTE_GCC_DIAG(3,4);
607
608/* T is a statement. Add it to the statement-tree. This is the
609 C/ObjC version--C++ has a slightly different version of this
610 function. */
611
612tree
613add_stmt (tree t)
614{
615 enum tree_code code = TREE_CODE (t);
616
617 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
618 {
619 if (!EXPR_HAS_LOCATION (t))
620 SET_EXPR_LOCATION (t, input_location);
621 }
622
623 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
624 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
625
626 /* Add T to the statement-tree. Non-side-effect statements need to be
627 recorded during statement expressions. */
628 if (!building_stmt_list_p ())
629 push_stmt_list ();
630 append_to_statement_list_force (t, &cur_stmt_list);
631
632 return t;
633}
634
635/* Build a pointer type using the default pointer mode. */
636
637static tree
638c_build_pointer_type (tree to_type)
639{
640 addr_space_t as = to_type == error_mark_node? ADDR_SPACE_GENERIC
641 : TYPE_ADDR_SPACE (to_type);
642 machine_mode pointer_mode;
643
644 if (as != ADDR_SPACE_GENERIC || c_default_pointer_mode == VOIDmode)
645 pointer_mode = targetm.addr_space.pointer_mode (as);
646 else
647 pointer_mode = c_default_pointer_mode;
648 return build_pointer_type_for_mode (to_type, pointer_mode, false);
649}
650
651
652/* Return true if we will want to say something if a goto statement
653 crosses DECL. */
654
655static bool
656decl_jump_unsafe (tree decl)
657{
658 if (error_operand_p (decl))
659 return false;
660
661 /* Always warn about crossing variably modified types. */
662 if ((VAR_P (decl) || TREE_CODE (decl) == TYPE_DECL)
663 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
664 return true;
665
666 /* Otherwise, only warn if -Wgoto-misses-init and this is an
667 initialized automatic decl. */
668 if (warn_jump_misses_init
669 && VAR_P (decl)
670 && !TREE_STATIC (decl)
671 && DECL_INITIAL (decl) != NULL_TREE)
672 return true;
673
674 return false;
675}
676
677
678void
679c_print_identifier (FILE *file, tree node, int indent)
680{
681 void (*save) (enum c_oracle_request, tree identifier);
682
683 /* Temporarily hide any binding oracle. Without this, calls to
684 debug_tree from the debugger will end up calling into the oracle,
685 making for a confusing debug session. As the oracle isn't needed
686 here for normal operation, it's simplest to suppress it. */
687 save = c_binding_oracle;
688 c_binding_oracle = NULL;
689
690 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
691 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
692 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
693 if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
694 {
695 tree rid = ridpointers[C_RID_CODE (node)];
696 indent_to (file, indent + 4);
697 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
698 (void *) rid, IDENTIFIER_POINTER (rid));
699 }
700
701 c_binding_oracle = save;
702}
703
704/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
705 which may be any of several kinds of DECL or TYPE or error_mark_node,
706 in the scope SCOPE. */
707static void
708bind (tree name, tree decl, struct c_scope *scope, bool invisible,
709 bool nested, location_t locus)
710{
711 struct c_binding *b, **here;
712
713 if (binding_freelist)
714 {
715 b = binding_freelist;
716 binding_freelist = b->prev;
717 }
718 else
719 b = ggc_alloc<c_binding> ();
720
721 b->shadowed = 0;
722 b->decl = decl;
723 b->id = name;
724 b->depth = scope->depth;
725 b->invisible = invisible;
726 b->nested = nested;
727 b->inner_comp = 0;
728 b->in_struct = 0;
729 b->locus = locus;
730
731 b->u.type = NULL;
732
733 b->prev = scope->bindings;
734 scope->bindings = b;
735
736 if (decl_jump_unsafe (decl))
737 scope->has_jump_unsafe_decl = 1;
738
739 if (!name)
740 return;
741
742 switch (TREE_CODE (decl))
743 {
744 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
745 case ENUMERAL_TYPE:
746 case UNION_TYPE:
747 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
748 case VAR_DECL:
749 case FUNCTION_DECL:
750 case TYPE_DECL:
751 case CONST_DECL:
752 case PARM_DECL:
753 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
754
755 default:
756 gcc_unreachable ();
757 }
758
759 /* Locate the appropriate place in the chain of shadowed decls
760 to insert this binding. Normally, scope == current_scope and
761 this does nothing. */
762 while (*here && (*here)->depth > scope->depth)
763 here = &(*here)->shadowed;
764
765 b->shadowed = *here;
766 *here = b;
767}
768
769/* Clear the binding structure B, stick it on the binding_freelist,
770 and return the former value of b->prev. This is used by pop_scope
771 and get_parm_info to iterate destructively over all the bindings
772 from a given scope. */
773static struct c_binding *
774free_binding_and_advance (struct c_binding *b)
775{
776 struct c_binding *prev = b->prev;
777
778 memset (b, 0, sizeof (struct c_binding));
779 b->prev = binding_freelist;
780 binding_freelist = b;
781
782 return prev;
783}
784
785/* Bind a label. Like bind, but skip fields which aren't used for
786 labels, and add the LABEL_VARS value. */
787static void
788bind_label (tree name, tree label, struct c_scope *scope,
789 struct c_label_vars *label_vars)
790{
791 struct c_binding *b;
792
793 bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
794 UNKNOWN_LOCATION);
795
796 scope->has_label_bindings = true;
797
798 b = scope->bindings;
799 gcc_assert (b->decl == label);
800 label_vars->shadowed = b->u.label;
801 b->u.label = label_vars;
802}
803
804/* Hook called at end of compilation to assume 1 elt
805 for a file-scope tentative array defn that wasn't complete before. */
806
807void
808c_finish_incomplete_decl (tree decl)
809{
810 if (VAR_P (decl))
811 {
812 tree type = TREE_TYPE (decl);
813 if (type != error_mark_node
814 && TREE_CODE (type) == ARRAY_TYPE
815 && !DECL_EXTERNAL (decl)
816 && TYPE_DOMAIN (type) == NULL_TREE)
817 {
818 warning_at (DECL_SOURCE_LOCATION (decl),
819 0, "array %q+D assumed to have one element", decl);
820
821 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
822
823 relayout_decl (decl);
824 }
825 }
826}
827
828/* Record that inline function FUNC contains a reference (location
829 LOC) to static DECL (file-scope or function-local according to
830 TYPE). */
831
832void
833record_inline_static (location_t loc, tree func, tree decl,
834 enum c_inline_static_type type)
835{
836 c_inline_static *csi = ggc_alloc<c_inline_static> ();
837 csi->location = loc;
838 csi->function = func;
839 csi->static_decl = decl;
840 csi->type = type;
841 csi->next = c_inline_statics;
842 c_inline_statics = csi;
843}
844
845/* Check for references to static declarations in inline functions at
846 the end of the translation unit and diagnose them if the functions
847 are still inline definitions. */
848
849static void
850check_inline_statics (void)
851{
852 struct c_inline_static *csi;
853 for (csi = c_inline_statics; csi; csi = csi->next)
854 {
855 if (DECL_EXTERNAL (csi->function))
856 switch (csi->type)
857 {
858 case csi_internal:
859 pedwarn (csi->location, 0,
860 "%qD is static but used in inline function %qD "
861 "which is not static", csi->static_decl, csi->function);
862 break;
863 case csi_modifiable:
864 pedwarn (csi->location, 0,
865 "%q+D is static but declared in inline function %qD "
866 "which is not static", csi->static_decl, csi->function);
867 break;
868 default:
869 gcc_unreachable ();
870 }
871 }
872 c_inline_statics = NULL;
873}
874
875/* Fill in a c_spot_bindings structure. If DEFINING is true, set it
876 for the current state, otherwise set it to uninitialized. */
877
878static void
879set_spot_bindings (struct c_spot_bindings *p, bool defining)
880{
881 if (defining)
882 {
883 p->scope = current_scope;
884 p->bindings_in_scope = current_scope->bindings;
885 }
886 else
887 {
888 p->scope = NULL;
889 p->bindings_in_scope = NULL;
890 }
891 p->stmt_exprs = 0;
892 p->left_stmt_expr = false;
893}
894
895/* Update spot bindings P as we pop out of SCOPE. Return true if we
896 should push decls for a label. */
897
898static bool
899update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
900{
901 if (p->scope != scope)
902 {
903 /* This label or goto is defined in some other scope, or it is a
904 label which is not yet defined. There is nothing to
905 update. */
906 return false;
907 }
908
909 /* Adjust the spot bindings to refer to the bindings already defined
910 in the enclosing scope. */
911 p->scope = scope->outer;
912 p->bindings_in_scope = p->scope->bindings;
913
914 return true;
915}
916
917/* The Objective-C front-end often needs to determine the current scope. */
918
919void *
920objc_get_current_scope (void)
921{
922 return current_scope;
923}
924
925/* The following function is used only by Objective-C. It needs to live here
926 because it accesses the innards of c_scope. */
927
928void
929objc_mark_locals_volatile (void *enclosing_blk)
930{
931 struct c_scope *scope;
932 struct c_binding *b;
933
934 for (scope = current_scope;
935 scope && scope != enclosing_blk;
936 scope = scope->outer)
937 {
938 for (b = scope->bindings; b; b = b->prev)
939 objc_volatilize_decl (b->decl);
940
941 /* Do not climb up past the current function. */
942 if (scope->function_body)
943 break;
944 }
945}
946
947/* Return true if we are in the global binding level. */
948
949bool
950global_bindings_p (void)
951{
952 return current_scope == file_scope;
953}
954
955void
956keep_next_level (void)
957{
958 keep_next_level_flag = true;
959}
960
961/* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
962
963void
964set_float_const_decimal64 (void)
965{
966 current_scope->float_const_decimal64 = true;
967}
968
969/* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
970
971void
972clear_float_const_decimal64 (void)
973{
974 current_scope->float_const_decimal64 = false;
975}
976
977/* Return nonzero if an unsuffixed float constant is _Decimal64. */
978
979bool
980float_const_decimal64_p (void)
981{
982 return current_scope->float_const_decimal64;
983}
984
985/* Identify this scope as currently being filled with parameters. */
986
987void
988declare_parm_level (void)
989{
990 current_scope->parm_flag = true;
991}
992
993void
994push_scope (void)
995{
996 if (next_is_function_body)
997 {
998 /* This is the transition from the parameters to the top level
999 of the function body. These are the same scope
1000 (C99 6.2.1p4,6) so we do not push another scope structure.
1001 next_is_function_body is set only by store_parm_decls, which
1002 in turn is called when and only when we are about to
1003 encounter the opening curly brace for the function body.
1004
1005 The outermost block of a function always gets a BLOCK node,
1006 because the debugging output routines expect that each
1007 function has at least one BLOCK. */
1008 current_scope->parm_flag = false;
1009 current_scope->function_body = true;
1010 current_scope->keep = true;
1011 current_scope->outer_function = current_function_scope;
1012 current_function_scope = current_scope;
1013
1014 keep_next_level_flag = false;
1015 next_is_function_body = false;
1016
1017 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1018 if (current_scope->outer)
1019 current_scope->float_const_decimal64
1020 = current_scope->outer->float_const_decimal64;
1021 else
1022 current_scope->float_const_decimal64 = false;
1023 }
1024 else
1025 {
1026 struct c_scope *scope;
1027 if (scope_freelist)
1028 {
1029 scope = scope_freelist;
1030 scope_freelist = scope->outer;
1031 }
1032 else
1033 scope = ggc_cleared_alloc<c_scope> ();
1034
1035 /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
1036 if (current_scope)
1037 scope->float_const_decimal64 = current_scope->float_const_decimal64;
1038 else
1039 scope->float_const_decimal64 = false;
1040
1041 scope->keep = keep_next_level_flag;
1042 scope->outer = current_scope;
1043 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
1044
1045 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
1046 possible. */
1047 if (current_scope && scope->depth == 0)
1048 {
1049 scope->depth--;
1050 sorry ("GCC supports only %u nested scopes", scope->depth);
1051 }
1052
1053 current_scope = scope;
1054 keep_next_level_flag = false;
1055 }
1056}
1057
1058/* This is called when we are leaving SCOPE. For each label defined
1059 in SCOPE, add any appropriate decls to its decls_in_scope fields.
1060 These are the decls whose initialization will be skipped by a goto
1061 later in the function. */
1062
1063static void
1064update_label_decls (struct c_scope *scope)
1065{
1066 struct c_scope *s;
1067
1068 s = scope;
1069 while (s != NULL)
1070 {
1071 if (s->has_label_bindings)
1072 {
1073 struct c_binding *b;
1074
1075 for (b = s->bindings; b != NULL; b = b->prev)
1076 {
1077 struct c_label_vars *label_vars;
1078 struct c_binding *b1;
1079 bool hjud;
1080 unsigned int ix;
1081 struct c_goto_bindings *g;
1082
1083 if (TREE_CODE (b->decl) != LABEL_DECL)
1084 continue;
1085 label_vars = b->u.label;
1086
1087 b1 = label_vars->label_bindings.bindings_in_scope;
1088 if (label_vars->label_bindings.scope == NULL)
1089 hjud = false;
1090 else
1091 hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
1092 if (update_spot_bindings (scope, &label_vars->label_bindings))
1093 {
1094 /* This label is defined in this scope. */
1095 if (hjud)
1096 {
1097 for (; b1 != NULL; b1 = b1->prev)
1098 {
1099 /* A goto from later in the function to this
1100 label will never see the initialization
1101 of B1, if any. Save it to issue a
1102 warning if needed. */
1103 if (decl_jump_unsafe (b1->decl))
1104 vec_safe_push(label_vars->decls_in_scope, b1->decl);
1105 }
1106 }
1107 }
1108
1109 /* Update the bindings of any goto statements associated
1110 with this label. */
1111 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1112 update_spot_bindings (scope, &g->goto_bindings);
1113 }
1114 }
1115
1116 /* Don't search beyond the current function. */
1117 if (s == current_function_scope)
1118 break;
1119
1120 s = s->outer;
1121 }
1122}
1123
1124/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1125
1126static void
1127set_type_context (tree type, tree context)
1128{
1129 for (type = TYPE_MAIN_VARIANT (type); type;
1130 type = TYPE_NEXT_VARIANT (type))
1131 TYPE_CONTEXT (type) = context;
1132}
1133
1134/* Exit a scope. Restore the state of the identifier-decl mappings
1135 that were in effect when this scope was entered. Return a BLOCK
1136 node containing all the DECLs in this scope that are of interest
1137 to debug info generation. */
1138
1139tree
1140pop_scope (void)
1141{
1142 struct c_scope *scope = current_scope;
1143 tree block, context, p;
1144 struct c_binding *b;
1145
1146 bool functionbody = scope->function_body;
1147 bool keep = functionbody || scope->keep || scope->bindings;
1148
1149 update_label_decls (scope);
1150
1151 /* If appropriate, create a BLOCK to record the decls for the life
1152 of this function. */
1153 block = NULL_TREE;
1154 if (keep)
1155 {
1156 block = make_node (BLOCK);
1157 BLOCK_SUBBLOCKS (block) = scope->blocks;
1158 TREE_USED (block) = 1;
1159
1160 /* In each subblock, record that this is its superior. */
1161 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1162 BLOCK_SUPERCONTEXT (p) = block;
1163
1164 BLOCK_VARS (block) = NULL_TREE;
1165 }
1166
1167 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1168 scope must be set so that they point to the appropriate
1169 construct, i.e. either to the current FUNCTION_DECL node, or
1170 else to the BLOCK node we just constructed.
1171
1172 Note that for tagged types whose scope is just the formal
1173 parameter list for some function type specification, we can't
1174 properly set their TYPE_CONTEXTs here, because we don't have a
1175 pointer to the appropriate FUNCTION_TYPE node readily available
1176 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1177 type nodes get set in `grokdeclarator' as soon as we have created
1178 the FUNCTION_TYPE node which will represent the "scope" for these
1179 "parameter list local" tagged types. */
1180 if (scope->function_body)
1181 context = current_function_decl;
1182 else if (scope == file_scope)
1183 {
1184 tree file_decl
1185 = build_translation_unit_decl (get_identifier (main_input_filename));
1186 context = file_decl;
1187 debug_hooks->register_main_translation_unit (file_decl);
1188 }
1189 else
1190 context = block;
1191
1192 /* Clear all bindings in this scope. */
1193 for (b = scope->bindings; b; b = free_binding_and_advance (b))
1194 {
1195 p = b->decl;
1196 switch (TREE_CODE (p))
1197 {
1198 case LABEL_DECL:
1199 /* Warnings for unused labels, errors for undefined labels. */
1200 if (TREE_USED (p) && !DECL_INITIAL (p))
1201 {
1202 error ("label %q+D used but not defined", p);
1203 DECL_INITIAL (p) = error_mark_node;
1204 }
1205 else
1206 warn_for_unused_label (p);
1207
1208 /* Labels go in BLOCK_VARS. */
1209 DECL_CHAIN (p) = BLOCK_VARS (block);
1210 BLOCK_VARS (block) = p;
1211 gcc_assert (I_LABEL_BINDING (b->id) == b);
1212 I_LABEL_BINDING (b->id) = b->shadowed;
1213
1214 /* Also pop back to the shadowed label_vars. */
1215 release_tree_vector (b->u.label->decls_in_scope);
1216 b->u.label = b->u.label->shadowed;
1217 break;
1218
1219 case ENUMERAL_TYPE:
1220 case UNION_TYPE:
1221 case RECORD_TYPE:
1222 set_type_context (p, context);
1223
1224 /* Types may not have tag-names, in which case the type
1225 appears in the bindings list with b->id NULL. */
1226 if (b->id)
1227 {
1228 gcc_assert (I_TAG_BINDING (b->id) == b);
1229 I_TAG_BINDING (b->id) = b->shadowed;
1230 }
1231 break;
1232
1233 case FUNCTION_DECL:
1234 /* Propagate TREE_ADDRESSABLE from nested functions to their
1235 containing functions. */
1236 if (!TREE_ASM_WRITTEN (p)
1237 && DECL_INITIAL (p) != NULL_TREE
1238 && TREE_ADDRESSABLE (p)
1239 && DECL_ABSTRACT_ORIGIN (p) != NULL_TREE
1240 && DECL_ABSTRACT_ORIGIN (p) != p)
1241 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1242 if (!DECL_EXTERNAL (p)
1243 && !DECL_INITIAL (p)
1244 && scope != file_scope
1245 && scope != external_scope)
1246 {
1247 error ("nested function %q+D declared but never defined", p);
1248 undef_nested_function = true;
1249 }
1250 else if (DECL_DECLARED_INLINE_P (p)
1251 && TREE_PUBLIC (p)
1252 && !DECL_INITIAL (p))
1253 {
1254 /* C99 6.7.4p6: "a function with external linkage... declared
1255 with an inline function specifier ... shall also be defined
1256 in the same translation unit." */
1257 if (!flag_gnu89_inline
1258 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (p))
1259 && scope != external_scope)
1260 pedwarn (input_location, 0,
1261 "inline function %q+D declared but never defined", p);
1262 DECL_EXTERNAL (p) = 1;
1263 }
1264
1265 goto common_symbol;
1266
1267 case VAR_DECL:
1268 /* Warnings for unused variables. */
1269 if ((!TREE_USED (p) || !DECL_READ_P (p))
1270 && !TREE_NO_WARNING (p)
1271 && !DECL_IN_SYSTEM_HEADER (p)
1272 && DECL_NAME (p)
1273 && !DECL_ARTIFICIAL (p)
1274 && scope != file_scope
1275 && scope != external_scope)
1276 {
1277 if (!TREE_USED (p))
1278 warning (OPT_Wunused_variable, "unused variable %q+D", p);
1279 else if (DECL_CONTEXT (p) == current_function_decl)
1280 warning_at (DECL_SOURCE_LOCATION (p),
1281 OPT_Wunused_but_set_variable,
1282 "variable %qD set but not used", p);
1283 }
1284
1285 if (b->inner_comp)
1286 {
1287 error ("type of array %q+D completed incompatibly with"
1288 " implicit initialization", p);
1289 }
1290
1291 /* Fall through. */
1292 case TYPE_DECL:
1293 case CONST_DECL:
1294 common_symbol:
1295 /* All of these go in BLOCK_VARS, but only if this is the
1296 binding in the home scope. */
1297 if (!b->nested)
1298 {
1299 DECL_CHAIN (p) = BLOCK_VARS (block);
1300 BLOCK_VARS (block) = p;
1301 }
1302 else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1303 {
1304 /* For block local externs add a special
1305 DECL_EXTERNAL decl for debug info generation. */
1306 tree extp = copy_node (p);
1307
1308 DECL_EXTERNAL (extp) = 1;
1309 TREE_STATIC (extp) = 0;
1310 TREE_PUBLIC (extp) = 1;
1311 DECL_INITIAL (extp) = NULL_TREE;
1312 DECL_LANG_SPECIFIC (extp) = NULL;
1313 DECL_CONTEXT (extp) = current_function_decl;
1314 if (TREE_CODE (p) == FUNCTION_DECL)
1315 {
1316 DECL_RESULT (extp) = NULL_TREE;
1317 DECL_SAVED_TREE (extp) = NULL_TREE;
1318 DECL_STRUCT_FUNCTION (extp) = NULL;
1319 }
1320 if (b->locus != UNKNOWN_LOCATION)
1321 DECL_SOURCE_LOCATION (extp) = b->locus;
1322 DECL_CHAIN (extp) = BLOCK_VARS (block);
1323 BLOCK_VARS (block) = extp;
1324 }
1325 /* If this is the file scope set DECL_CONTEXT of each decl to
1326 the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1327 work. */
1328 if (scope == file_scope)
1329 {
1330 DECL_CONTEXT (p) = context;
1331 if (TREE_CODE (p) == TYPE_DECL
1332 && TREE_TYPE (p) != error_mark_node)
1333 set_type_context (TREE_TYPE (p), context);
1334 }
1335
1336 gcc_fallthrough ();
1337 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1338 already been put there by store_parm_decls. Unused-
1339 parameter warnings are handled by function.c.
1340 error_mark_node obviously does not go in BLOCK_VARS and
1341 does not get unused-variable warnings. */
1342 case PARM_DECL:
1343 case ERROR_MARK:
1344 /* It is possible for a decl not to have a name. We get
1345 here with b->id NULL in this case. */
1346 if (b->id)
1347 {
1348 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1349 I_SYMBOL_BINDING (b->id) = b->shadowed;
1350 if (b->shadowed && b->shadowed->u.type)
1351 TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1352 }
1353 break;
1354
1355 default:
1356 gcc_unreachable ();
1357 }
1358 }
1359
1360
1361 /* Dispose of the block that we just made inside some higher level. */
1362 if ((scope->function_body || scope == file_scope) && context)
1363 {
1364 DECL_INITIAL (context) = block;
1365 BLOCK_SUPERCONTEXT (block) = context;
1366 }
1367 else if (scope->outer)
1368 {
1369 if (block)
1370 SCOPE_LIST_APPEND (scope->outer, blocks, block);
1371 /* If we did not make a block for the scope just exited, any
1372 blocks made for inner scopes must be carried forward so they
1373 will later become subblocks of something else. */
1374 else if (scope->blocks)
1375 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1376 }
1377
1378 /* Pop the current scope, and free the structure for reuse. */
1379 current_scope = scope->outer;
1380 if (scope->function_body)
1381 current_function_scope = scope->outer_function;
1382
1383 memset (scope, 0, sizeof (struct c_scope));
1384 scope->outer = scope_freelist;
1385 scope_freelist = scope;
1386
1387 return block;
1388}
1389
1390void
1391push_file_scope (void)
1392{
1393 tree decl;
1394
1395 if (file_scope)
1396 return;
1397
1398 push_scope ();
1399 file_scope = current_scope;
1400
1401 start_fname_decls ();
1402
1403 for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1404 bind (DECL_NAME (decl), decl, file_scope,
1405 /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1406}
1407
1408void
1409pop_file_scope (void)
1410{
1411 /* In case there were missing closebraces, get us back to the global
1412 binding level. */
1413 while (current_scope != file_scope)
1414 pop_scope ();
1415
1416 /* __FUNCTION__ is defined at file scope (""). This
1417 call may not be necessary as my tests indicate it
1418 still works without it. */
1419 finish_fname_decls ();
1420
1421 check_inline_statics ();
1422
1423 /* This is the point to write out a PCH if we're doing that.
1424 In that case we do not want to do anything else. */
1425 if (pch_file)
1426 {
1427 c_common_write_pch ();
1428 /* Ensure even the callers don't try to finalize the CU. */
1429 flag_syntax_only = 1;
1430 return;
1431 }
1432
1433 /* Pop off the file scope and close this translation unit. */
1434 pop_scope ();
1435 file_scope = 0;
1436
1437 maybe_apply_pending_pragma_weaks ();
1438}
1439
1440/* Adjust the bindings for the start of a statement expression. */
1441
1442void
1443c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1444{
1445 struct c_scope *scope;
1446
1447 for (scope = current_scope; scope != NULL; scope = scope->outer)
1448 {
1449 struct c_binding *b;
1450
1451 if (!scope->has_label_bindings)
1452 continue;
1453
1454 for (b = scope->bindings; b != NULL; b = b->prev)
1455 {
1456 struct c_label_vars *label_vars;
1457 unsigned int ix;
1458 struct c_goto_bindings *g;
1459
1460 if (TREE_CODE (b->decl) != LABEL_DECL)
1461 continue;
1462 label_vars = b->u.label;
1463 ++label_vars->label_bindings.stmt_exprs;
1464 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1465 ++g->goto_bindings.stmt_exprs;
1466 }
1467 }
1468
1469 if (switch_bindings != NULL)
1470 ++switch_bindings->stmt_exprs;
1471}
1472
1473/* Adjust the bindings for the end of a statement expression. */
1474
1475void
1476c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1477{
1478 struct c_scope *scope;
1479
1480 for (scope = current_scope; scope != NULL; scope = scope->outer)
1481 {
1482 struct c_binding *b;
1483
1484 if (!scope->has_label_bindings)
1485 continue;
1486
1487 for (b = scope->bindings; b != NULL; b = b->prev)
1488 {
1489 struct c_label_vars *label_vars;
1490 unsigned int ix;
1491 struct c_goto_bindings *g;
1492
1493 if (TREE_CODE (b->decl) != LABEL_DECL)
1494 continue;
1495 label_vars = b->u.label;
1496 --label_vars->label_bindings.stmt_exprs;
1497 if (label_vars->label_bindings.stmt_exprs < 0)
1498 {
1499 label_vars->label_bindings.left_stmt_expr = true;
1500 label_vars->label_bindings.stmt_exprs = 0;
1501 }
1502 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
1503 {
1504 --g->goto_bindings.stmt_exprs;
1505 if (g->goto_bindings.stmt_exprs < 0)
1506 {
1507 g->goto_bindings.left_stmt_expr = true;
1508 g->goto_bindings.stmt_exprs = 0;
1509 }
1510 }
1511 }
1512 }
1513
1514 if (switch_bindings != NULL)
1515 {
1516 --switch_bindings->stmt_exprs;
1517 gcc_assert (switch_bindings->stmt_exprs >= 0);
1518 }
1519}
1520
1521/* Push a definition or a declaration of struct, union or enum tag "name".
1522 "type" should be the type node.
1523 We assume that the tag "name" is not already defined, and has a location
1524 of LOC.
1525
1526 Note that the definition may really be just a forward reference.
1527 In that case, the TYPE_SIZE will be zero. */
1528
1529static void
1530pushtag (location_t loc, tree name, tree type)
1531{
1532 /* Record the identifier as the type's name if it has none. */
1533 if (name && !TYPE_NAME (type))
1534 TYPE_NAME (type) = name;
1535 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1536
1537 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1538 tagged type we just added to the current scope. This fake
1539 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1540 to output a representation of a tagged type, and it also gives
1541 us a convenient place to record the "scope start" address for the
1542 tagged type. */
1543
1544 TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1545 TYPE_DECL, NULL_TREE, type));
1546
1547 /* An approximation for now, so we can tell this is a function-scope tag.
1548 This will be updated in pop_scope. */
1549 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1550
1551 if (warn_cxx_compat && name != NULL_TREE)
1552 {
1553 struct c_binding *b = I_SYMBOL_BINDING (name);
1554
1555 if (b != NULL
1556 && b->decl != NULL_TREE
1557 && TREE_CODE (b->decl) == TYPE_DECL
1558 && (B_IN_CURRENT_SCOPE (b)
1559 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1560 && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1561 != TYPE_MAIN_VARIANT (type)))
1562 {
1563 if (warning_at (loc, OPT_Wc___compat,
1564 ("using %qD as both a typedef and a tag is "
1565 "invalid in C++"), b->decl)
1566 && b->locus != UNKNOWN_LOCATION)
1567 inform (b->locus, "originally defined here");
1568 }
1569 }
1570}
1571
1572/* An exported interface to pushtag. This is used by the gdb plugin's
1573 binding oracle to introduce a new tag binding. */
1574
1575void
1576c_pushtag (location_t loc, tree name, tree type)
1577{
1578 pushtag (loc, name, type);
1579}
1580
1581/* An exported interface to bind a declaration. LOC is the location
1582 to use. DECL is the declaration to bind. The decl's name is used
1583 to determine how it is bound. If DECL is a VAR_DECL, then
1584 IS_GLOBAL determines whether the decl is put into the global (file
1585 and external) scope or the current function's scope; if DECL is not
1586 a VAR_DECL then it is always put into the file scope. */
1587
1588void
1589c_bind (location_t loc, tree decl, bool is_global)
1590{
1591 struct c_scope *scope;
1592 bool nested = false;
1593
1594 if (!VAR_P (decl) || current_function_scope == NULL)
1595 {
1596 /* Types and functions are always considered to be global. */
1597 scope = file_scope;
1598 DECL_EXTERNAL (decl) = 1;
1599 TREE_PUBLIC (decl) = 1;
1600 }
1601 else if (is_global)
1602 {
1603 /* Also bind it into the external scope. */
1604 bind (DECL_NAME (decl), decl, external_scope, true, false, loc);
1605 nested = true;
1606 scope = file_scope;
1607 DECL_EXTERNAL (decl) = 1;
1608 TREE_PUBLIC (decl) = 1;
1609 }
1610 else
1611 {
1612 DECL_CONTEXT (decl) = current_function_decl;
1613 TREE_PUBLIC (decl) = 0;
1614 scope = current_function_scope;
1615 }
1616
1617 bind (DECL_NAME (decl), decl, scope, false, nested, loc);
1618}
1619
1620/* Subroutine of compare_decls. Allow harmless mismatches in return
1621 and argument types provided that the type modes match. This function
1622 return a unified type given a suitable match, and 0 otherwise. */
1623
1624static tree
1625match_builtin_function_types (tree newtype, tree oldtype)
1626{
1627 tree newrettype, oldrettype;
1628 tree newargs, oldargs;
1629 tree trytype, tryargs;
1630
1631 /* Accept the return type of the new declaration if same modes. */
1632 oldrettype = TREE_TYPE (oldtype);
1633 newrettype = TREE_TYPE (newtype);
1634
1635 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1636 return NULL_TREE;
1637
1638 oldargs = TYPE_ARG_TYPES (oldtype);
1639 newargs = TYPE_ARG_TYPES (newtype);
1640 tryargs = newargs;
1641
1642 while (oldargs || newargs)
1643 {
1644 if (!oldargs
1645 || !newargs
1646 || !TREE_VALUE (oldargs)
1647 || !TREE_VALUE (newargs)
1648 || TYPE_MODE (TREE_VALUE (oldargs))
1649 != TYPE_MODE (TREE_VALUE (newargs)))
1650 return NULL_TREE;
1651
1652 oldargs = TREE_CHAIN (oldargs);
1653 newargs = TREE_CHAIN (newargs);
1654 }
1655
1656 trytype = build_function_type (newrettype, tryargs);
1657
1658 /* Allow declaration to change transaction_safe attribute. */
1659 tree oldattrs = TYPE_ATTRIBUTES (oldtype);
1660 tree oldtsafe = lookup_attribute ("transaction_safe", oldattrs);
1661 tree newattrs = TYPE_ATTRIBUTES (newtype);
1662 tree newtsafe = lookup_attribute ("transaction_safe", newattrs);
1663 if (oldtsafe && !newtsafe)
1664 oldattrs = remove_attribute ("transaction_safe", oldattrs);
1665 else if (newtsafe && !oldtsafe)
1666 oldattrs = tree_cons (get_identifier ("transaction_safe"),
1667 NULL_TREE, oldattrs);
1668
1669 return build_type_attribute_variant (trytype, oldattrs);
1670}
1671
1672/* Subroutine of diagnose_mismatched_decls. Check for function type
1673 mismatch involving an empty arglist vs a nonempty one and give clearer
1674 diagnostics. */
1675static void
1676diagnose_arglist_conflict (tree newdecl, tree olddecl,
1677 tree newtype, tree oldtype)
1678{
1679 tree t;
1680
1681 if (TREE_CODE (olddecl) != FUNCTION_DECL
1682 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1683 || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == NULL_TREE)
1684 || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == NULL_TREE)))
1685 return;
1686
1687 t = TYPE_ARG_TYPES (oldtype);
1688 if (t == NULL_TREE)
1689 t = TYPE_ARG_TYPES (newtype);
1690 for (; t; t = TREE_CHAIN (t))
1691 {
1692 tree type = TREE_VALUE (t);
1693
1694 if (TREE_CHAIN (t) == NULL_TREE
1695 && TYPE_MAIN_VARIANT (type) != void_type_node)
1696 {
1697 inform (input_location, "a parameter list with an ellipsis can%'t match "
1698 "an empty parameter name list declaration");
1699 break;
1700 }
1701
1702 if (c_type_promotes_to (type) != type)
1703 {
1704 inform (input_location, "an argument type that has a default promotion can%'t match "
1705 "an empty parameter name list declaration");
1706 break;
1707 }
1708 }
1709}
1710
1711/* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1712 old-style function definition, NEWDECL is a prototype declaration.
1713 Diagnose inconsistencies in the argument list. Returns TRUE if
1714 the prototype is compatible, FALSE if not. */
1715static bool
1716validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1717{
1718 tree newargs, oldargs;
1719 int i;
1720
1721#define END_OF_ARGLIST(t) ((t) == void_type_node)
1722
1723 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1724 newargs = TYPE_ARG_TYPES (newtype);
1725 i = 1;
1726
1727 for (;;)
1728 {
1729 tree oldargtype = TREE_VALUE (oldargs);
1730 tree newargtype = TREE_VALUE (newargs);
1731
1732 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1733 return false;
1734
1735 oldargtype = (TYPE_ATOMIC (oldargtype)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (oldargtype),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (oldargtype));
1739 newargtype = (TYPE_ATOMIC (newargtype)
1740 ? c_build_qualified_type (TYPE_MAIN_VARIANT (newargtype),
1741 TYPE_QUAL_ATOMIC)
1742 : TYPE_MAIN_VARIANT (newargtype));
1743
1744 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1745 break;
1746
1747 /* Reaching the end of just one list means the two decls don't
1748 agree on the number of arguments. */
1749 if (END_OF_ARGLIST (oldargtype))
1750 {
1751 error ("prototype for %q+D declares more arguments "
1752 "than previous old-style definition", newdecl);
1753 return false;
1754 }
1755 else if (END_OF_ARGLIST (newargtype))
1756 {
1757 error ("prototype for %q+D declares fewer arguments "
1758 "than previous old-style definition", newdecl);
1759 return false;
1760 }
1761
1762 /* Type for passing arg must be consistent with that declared
1763 for the arg. */
1764 else if (!comptypes (oldargtype, newargtype))
1765 {
1766 error ("prototype for %q+D declares argument %d"
1767 " with incompatible type",
1768 newdecl, i);
1769 return false;
1770 }
1771
1772 oldargs = TREE_CHAIN (oldargs);
1773 newargs = TREE_CHAIN (newargs);
1774 i++;
1775 }
1776
1777 /* If we get here, no errors were found, but do issue a warning
1778 for this poor-style construct. */
1779 warning (0, "prototype for %q+D follows non-prototype definition",
1780 newdecl);
1781 return true;
1782#undef END_OF_ARGLIST
1783}
1784
1785/* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1786 first in a pair of mismatched declarations, using the diagnostic
1787 function DIAG. */
1788static void
1789locate_old_decl (tree decl)
1790{
1791 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)
1792 && !C_DECL_DECLARED_BUILTIN (decl))
1793 ;
1794 else if (DECL_INITIAL (decl))
1795 inform (input_location, "previous definition of %q+D was here", decl);
1796 else if (C_DECL_IMPLICIT (decl))
1797 inform (input_location, "previous implicit declaration of %q+D was here", decl);
1798 else
1799 inform (input_location, "previous declaration of %q+D was here", decl);
1800}
1801
1802/* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1803 Returns true if the caller should proceed to merge the two, false
1804 if OLDDECL should simply be discarded. As a side effect, issues
1805 all necessary diagnostics for invalid or poor-style combinations.
1806 If it returns true, writes the types of NEWDECL and OLDDECL to
1807 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1808 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1809
1810static bool
1811diagnose_mismatched_decls (tree newdecl, tree olddecl,
1812 tree *newtypep, tree *oldtypep)
1813{
1814 tree newtype, oldtype;
1815 bool pedwarned = false;
1816 bool warned = false;
1817 bool retval = true;
1818
1819#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1820 && DECL_EXTERNAL (DECL))
1821
1822 /* If we have error_mark_node for either decl or type, just discard
1823 the previous decl - we're in an error cascade already. */
1824 if (olddecl == error_mark_node || newdecl == error_mark_node)
1825 return false;
1826 *oldtypep = oldtype = TREE_TYPE (olddecl);
1827 *newtypep = newtype = TREE_TYPE (newdecl);
1828 if (oldtype == error_mark_node || newtype == error_mark_node)
1829 return false;
1830
1831 /* Two different categories of symbol altogether. This is an error
1832 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1833 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1834 {
1835 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1836 && DECL_BUILT_IN (olddecl)
1837 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1838 {
1839 error ("%q+D redeclared as different kind of symbol", newdecl);
1840 locate_old_decl (olddecl);
1841 }
1842 else if (TREE_PUBLIC (newdecl))
1843 warning (OPT_Wbuiltin_declaration_mismatch,
1844 "built-in function %q+D declared as non-function",
1845 newdecl);
1846 else
1847 warning (OPT_Wshadow, "declaration of %q+D shadows "
1848 "a built-in function", newdecl);
1849 return false;
1850 }
1851
1852 /* Enumerators have no linkage, so may only be declared once in a
1853 given scope. */
1854 if (TREE_CODE (olddecl) == CONST_DECL)
1855 {
1856 error ("redeclaration of enumerator %q+D", newdecl);
1857 locate_old_decl (olddecl);
1858 return false;
1859 }
1860
1861 if (!comptypes (oldtype, newtype))
1862 {
1863 if (TREE_CODE (olddecl) == FUNCTION_DECL
1864 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1865 {
1866 /* Accept harmless mismatch in function types.
1867 This is for the ffs and fprintf builtins. */
1868 tree trytype = match_builtin_function_types (newtype, oldtype);
1869
1870 if (trytype && comptypes (newtype, trytype))
1871 *oldtypep = oldtype = trytype;
1872 else
1873 {
1874 /* If types don't match for a built-in, throw away the
1875 built-in. No point in calling locate_old_decl here, it
1876 won't print anything. */
1877 warning (OPT_Wbuiltin_declaration_mismatch,
1878 "conflicting types for built-in function %q+D",
1879 newdecl);
1880 return false;
1881 }
1882 }
1883 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1884 && DECL_IS_BUILTIN (olddecl))
1885 {
1886 /* A conflicting function declaration for a predeclared
1887 function that isn't actually built in. Objective C uses
1888 these. The new declaration silently overrides everything
1889 but the volatility (i.e. noreturn) indication. See also
1890 below. FIXME: Make Objective C use normal builtins. */
1891 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1892 return false;
1893 }
1894 /* Permit void foo (...) to match int foo (...) if the latter is
1895 the definition and implicit int was used. See
1896 c-torture/compile/920625-2.c. */
1897 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1898 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1899 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1900 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1901 {
1902 pedwarned = pedwarn (input_location, 0,
1903 "conflicting types for %q+D", newdecl);
1904 /* Make sure we keep void as the return type. */
1905 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1906 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1907 }
1908 /* Permit void foo (...) to match an earlier call to foo (...) with
1909 no declared type (thus, implicitly int). */
1910 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1911 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1912 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1913 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1914 {
1915 pedwarned = pedwarn (input_location, 0,
1916 "conflicting types for %q+D", newdecl);
1917 /* Make sure we keep void as the return type. */
1918 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1919 }
1920 else
1921 {
1922 int new_quals = TYPE_QUALS (newtype);
1923 int old_quals = TYPE_QUALS (oldtype);
1924
1925 if (new_quals != old_quals)
1926 {
1927 addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1928 addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1929 if (new_addr != old_addr)
1930 {
1931 if (ADDR_SPACE_GENERIC_P (new_addr))
1932 error ("conflicting named address spaces (generic vs %s) "
1933 "for %q+D",
1934 c_addr_space_name (old_addr), newdecl);
1935 else if (ADDR_SPACE_GENERIC_P (old_addr))
1936 error ("conflicting named address spaces (%s vs generic) "
1937 "for %q+D",
1938 c_addr_space_name (new_addr), newdecl);
1939 else
1940 error ("conflicting named address spaces (%s vs %s) "
1941 "for %q+D",
1942 c_addr_space_name (new_addr),
1943 c_addr_space_name (old_addr),
1944 newdecl);
1945 }
1946
1947 if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1948 != CLEAR_QUAL_ADDR_SPACE (old_quals))
1949 error ("conflicting type qualifiers for %q+D", newdecl);
1950 }
1951 else
1952 error ("conflicting types for %q+D", newdecl);
1953 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1954 locate_old_decl (olddecl);
1955 return false;
1956 }
1957 }
1958
1959 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1960 but silently ignore the redeclaration if either is in a system
1961 header. (Conflicting redeclarations were handled above.) This
1962 is allowed for C11 if the types are the same, not just
1963 compatible. */
1964 if (TREE_CODE (newdecl) == TYPE_DECL)
1965 {
1966 bool types_different = false;
1967 int comptypes_result;
1968
1969 comptypes_result
1970 = comptypes_check_different_types (oldtype, newtype, &types_different);
1971
1972 if (comptypes_result != 1 || types_different)
1973 {
1974 error ("redefinition of typedef %q+D with different type", newdecl);
1975 locate_old_decl (olddecl);
1976 return false;
1977 }
1978
1979 if (DECL_IN_SYSTEM_HEADER (newdecl)
1980 || DECL_IN_SYSTEM_HEADER (olddecl)
1981 || TREE_NO_WARNING (newdecl)
1982 || TREE_NO_WARNING (olddecl))
1983 return true; /* Allow OLDDECL to continue in use. */
1984
1985 if (variably_modified_type_p (newtype, NULL))
1986 {
1987 error ("redefinition of typedef %q+D with variably modified type",
1988 newdecl);
1989 locate_old_decl (olddecl);
1990 }
1991 else if (pedwarn_c99 (input_location, OPT_Wpedantic,
1992 "redefinition of typedef %q+D", newdecl))
1993 locate_old_decl (olddecl);
1994
1995 return true;
1996 }
1997
1998 /* Function declarations can either be 'static' or 'extern' (no
1999 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
2000 can never conflict with each other on account of linkage
2001 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
2002 gnu89 mode permits two definitions if one is 'extern inline' and
2003 one is not. The non- extern-inline definition supersedes the
2004 extern-inline definition. */
2005
2006 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2007 {
2008 /* If you declare a built-in function name as static, or
2009 define the built-in with an old-style definition (so we
2010 can't validate the argument list) the built-in definition is
2011 overridden, but optionally warn this was a bad choice of name. */
2012 if (DECL_BUILT_IN (olddecl)
2013 && !C_DECL_DECLARED_BUILTIN (olddecl)
2014 && (!TREE_PUBLIC (newdecl)
2015 || (DECL_INITIAL (newdecl)
2016 && !prototype_p (TREE_TYPE (newdecl)))))
2017 {
2018 warning (OPT_Wshadow, "declaration of %q+D shadows "
2019 "a built-in function", newdecl);
2020 /* Discard the old built-in function. */
2021 return false;
2022 }
2023
2024 if (DECL_INITIAL (newdecl))
2025 {
2026 if (DECL_INITIAL (olddecl))
2027 {
2028 /* If both decls are in the same TU and the new declaration
2029 isn't overriding an extern inline reject the new decl.
2030 In c99, no overriding is allowed in the same translation
2031 unit. */
2032 if ((!DECL_EXTERN_INLINE (olddecl)
2033 || DECL_EXTERN_INLINE (newdecl)
2034 || (!flag_gnu89_inline
2035 && (!DECL_DECLARED_INLINE_P (olddecl)
2036 || !lookup_attribute ("gnu_inline",
2037 DECL_ATTRIBUTES (olddecl)))
2038 && (!DECL_DECLARED_INLINE_P (newdecl)
2039 || !lookup_attribute ("gnu_inline",
2040 DECL_ATTRIBUTES (newdecl))))
2041 )
2042 && same_translation_unit_p (newdecl, olddecl))
2043 {
2044 error ("redefinition of %q+D", newdecl);
2045 locate_old_decl (olddecl);
2046 return false;
2047 }
2048 }
2049 }
2050 /* If we have a prototype after an old-style function definition,
2051 the argument types must be checked specially. */
2052 else if (DECL_INITIAL (olddecl)
2053 && !prototype_p (oldtype) && prototype_p (newtype)
2054 && TYPE_ACTUAL_ARG_TYPES (oldtype)
2055 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
2056 {
2057 locate_old_decl (olddecl);
2058 return false;
2059 }
2060 /* A non-static declaration (even an "extern") followed by a
2061 static declaration is undefined behavior per C99 6.2.2p3-5,7.
2062 The same is true for a static forward declaration at block
2063 scope followed by a non-static declaration/definition at file
2064 scope. Static followed by non-static at the same scope is
2065 not undefined behavior, and is the most convenient way to get
2066 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
2067 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
2068 we do diagnose it if -Wtraditional. */
2069 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
2070 {
2071 /* Two exceptions to the rule. If olddecl is an extern
2072 inline, or a predeclared function that isn't actually
2073 built in, newdecl silently overrides olddecl. The latter
2074 occur only in Objective C; see also above. (FIXME: Make
2075 Objective C use normal builtins.) */
2076 if (!DECL_IS_BUILTIN (olddecl)
2077 && !DECL_EXTERN_INLINE (olddecl))
2078 {
2079 error ("static declaration of %q+D follows "
2080 "non-static declaration", newdecl);
2081 locate_old_decl (olddecl);
2082 }
2083 return false;
2084 }
2085 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
2086 {
2087 if (DECL_CONTEXT (olddecl))
2088 {
2089 error ("non-static declaration of %q+D follows "
2090 "static declaration", newdecl);
2091 locate_old_decl (olddecl);
2092 return false;
2093 }
2094 else if (warn_traditional)
2095 {
2096 warned |= warning (OPT_Wtraditional,
2097 "non-static declaration of %q+D "
2098 "follows static declaration", newdecl);
2099 }
2100 }
2101
2102 /* Make sure gnu_inline attribute is either not present, or
2103 present on all inline decls. */
2104 if (DECL_DECLARED_INLINE_P (olddecl)
2105 && DECL_DECLARED_INLINE_P (newdecl))
2106 {
2107 bool newa = lookup_attribute ("gnu_inline",
2108 DECL_ATTRIBUTES (newdecl)) != NULL;
2109 bool olda = lookup_attribute ("gnu_inline",
2110 DECL_ATTRIBUTES (olddecl)) != NULL;
2111 if (newa != olda)
2112 {
2113 error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
2114 newa ? newdecl : olddecl);
2115 error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
2116 "but not here");
2117 }
2118 }
2119 }
2120 else if (VAR_P (newdecl))
2121 {
2122 /* Only variables can be thread-local, and all declarations must
2123 agree on this property. */
2124 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
2125 {
2126 /* Nothing to check. Since OLDDECL is marked threadprivate
2127 and NEWDECL does not have a thread-local attribute, we
2128 will merge the threadprivate attribute into NEWDECL. */
2129 ;
2130 }
2131 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
2132 {
2133 if (DECL_THREAD_LOCAL_P (newdecl))
2134 error ("thread-local declaration of %q+D follows "
2135 "non-thread-local declaration", newdecl);
2136 else
2137 error ("non-thread-local declaration of %q+D follows "
2138 "thread-local declaration", newdecl);
2139
2140 locate_old_decl (olddecl);
2141 return false;
2142 }
2143
2144 /* Multiple initialized definitions are not allowed (6.9p3,5). */
2145 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
2146 {
2147 error ("redefinition of %q+D", newdecl);
2148 locate_old_decl (olddecl);
2149 return false;
2150 }
2151
2152 /* Objects declared at file scope: if the first declaration had
2153 external linkage (even if it was an external reference) the
2154 second must have external linkage as well, or the behavior is
2155 undefined. If the first declaration had internal linkage, then
2156 the second must too, or else be an external reference (in which
2157 case the composite declaration still has internal linkage).
2158 As for function declarations, we warn about the static-then-
2159 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
2160 if (DECL_FILE_SCOPE_P (newdecl)
2161 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
2162 {
2163 if (DECL_EXTERNAL (newdecl))
2164 {
2165 if (!DECL_FILE_SCOPE_P (olddecl))
2166 {
2167 error ("extern declaration of %q+D follows "
2168 "declaration with no linkage", newdecl);
2169 locate_old_decl (olddecl);
2170 return false;
2171 }
2172 else if (warn_traditional)
2173 {
2174 warned |= warning (OPT_Wtraditional,
2175 "non-static declaration of %q+D "
2176 "follows static declaration", newdecl);
2177 }
2178 }
2179 else
2180 {
2181 if (TREE_PUBLIC (newdecl))
2182 error ("non-static declaration of %q+D follows "
2183 "static declaration", newdecl);
2184 else
2185 error ("static declaration of %q+D follows "
2186 "non-static declaration", newdecl);
2187
2188 locate_old_decl (olddecl);
2189 return false;
2190 }
2191 }
2192 /* Two objects with the same name declared at the same block
2193 scope must both be external references (6.7p3). */
2194 else if (!DECL_FILE_SCOPE_P (newdecl))
2195 {
2196 if (DECL_EXTERNAL (newdecl))
2197 {
2198 /* Extern with initializer at block scope, which will
2199 already have received an error. */
2200 }
2201 else if (DECL_EXTERNAL (olddecl))
2202 {
2203 error ("declaration of %q+D with no linkage follows "
2204 "extern declaration", newdecl);
2205 locate_old_decl (olddecl);
2206 }
2207 else
2208 {
2209 error ("redeclaration of %q+D with no linkage", newdecl);
2210 locate_old_decl (olddecl);
2211 }
2212
2213 return false;
2214 }
2215
2216 /* C++ does not permit a decl to appear multiple times at file
2217 scope. */
2218 if (warn_cxx_compat
2219 && DECL_FILE_SCOPE_P (newdecl)
2220 && !DECL_EXTERNAL (newdecl)
2221 && !DECL_EXTERNAL (olddecl))
2222 warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2223 OPT_Wc___compat,
2224 ("duplicate declaration of %qD is "
2225 "invalid in C++"),
2226 newdecl);
2227 }
2228
2229 /* warnings */
2230 /* All decls must agree on a visibility. */
2231 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2232 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2233 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2234 {
2235 warned |= warning (0, "redeclaration of %q+D with different visibility "
2236 "(old visibility preserved)", newdecl);
2237 }
2238
2239 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2240 warned |= diagnose_mismatched_attributes (olddecl, newdecl);
2241 else /* PARM_DECL, VAR_DECL */
2242 {
2243 /* Redeclaration of a parameter is a constraint violation (this is
2244 not explicitly stated, but follows from C99 6.7p3 [no more than
2245 one declaration of the same identifier with no linkage in the
2246 same scope, except type tags] and 6.2.2p6 [parameters have no
2247 linkage]). We must check for a forward parameter declaration,
2248 indicated by TREE_ASM_WRITTEN on the old declaration - this is
2249 an extension, the mandatory diagnostic for which is handled by
2250 mark_forward_parm_decls. */
2251
2252 if (TREE_CODE (newdecl) == PARM_DECL
2253 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2254 {
2255 error ("redefinition of parameter %q+D", newdecl);
2256 locate_old_decl (olddecl);
2257 return false;
2258 }
2259 }
2260
2261 /* Optional warning for completely redundant decls. */
2262 if (!warned && !pedwarned
2263 && warn_redundant_decls
2264 /* Don't warn about a function declaration followed by a
2265 definition. */
2266 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2267 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2268 /* Don't warn about redundant redeclarations of builtins. */
2269 && !(TREE_CODE (newdecl) == FUNCTION_DECL
2270 && !DECL_BUILT_IN (newdecl)
2271 && DECL_BUILT_IN (olddecl)
2272 && !C_DECL_DECLARED_BUILTIN (olddecl))
2273 /* Don't warn about an extern followed by a definition. */
2274 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2275 /* Don't warn about forward parameter decls. */
2276 && !(TREE_CODE (newdecl) == PARM_DECL
2277 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2278 /* Don't warn about a variable definition following a declaration. */
2279 && !(VAR_P (newdecl)
2280 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2281 {
2282 warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2283 newdecl);
2284 }
2285
2286 /* Report location of previous decl/defn. */
2287 if (warned || pedwarned)
2288 locate_old_decl (olddecl);
2289
2290#undef DECL_EXTERN_INLINE
2291
2292 return retval;
2293}
2294
2295/* Subroutine of duplicate_decls. NEWDECL has been found to be
2296 consistent with OLDDECL, but carries new information. Merge the
2297 new information into OLDDECL. This function issues no
2298 diagnostics. */
2299
2300static void
2301merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2302{
2303 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2304 && DECL_INITIAL (newdecl) != NULL_TREE);
2305 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2306 && prototype_p (TREE_TYPE (newdecl)));
2307 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2308 && prototype_p (TREE_TYPE (olddecl)));
2309
2310 /* For real parm decl following a forward decl, rechain the old decl
2311 in its new location and clear TREE_ASM_WRITTEN (it's not a
2312 forward decl anymore). */
2313 if (TREE_CODE (newdecl) == PARM_DECL
2314 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2315 {
2316 struct c_binding *b, **here;
2317
2318 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
2319 if ((*here)->decl == olddecl)
2320 goto found;
2321 gcc_unreachable ();
2322
2323 found:
2324 b = *here;
2325 *here = b->prev;
2326 b->prev = current_scope->bindings;
2327 current_scope->bindings = b;
2328
2329 TREE_ASM_WRITTEN (olddecl) = 0;
2330 }
2331
2332 DECL_ATTRIBUTES (newdecl)
2333 = targetm.merge_decl_attributes (olddecl, newdecl);
2334
2335 /* For typedefs use the old type, as the new type's DECL_NAME points
2336 at newdecl, which will be ggc_freed. */
2337 if (TREE_CODE (newdecl) == TYPE_DECL)
2338 {
2339 /* But NEWTYPE might have an attribute, honor that. */
2340 tree tem = newtype;
2341 newtype = oldtype;
2342
2343 if (TYPE_USER_ALIGN (tem))
2344 {
2345 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2346 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2347 TYPE_USER_ALIGN (newtype) = true;
2348 }
2349
2350 /* And remove the new type from the variants list. */
2351 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2352 {
2353 tree remove = TREE_TYPE (newdecl);
2354 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2355 t = TYPE_NEXT_VARIANT (t))
2356 if (TYPE_NEXT_VARIANT (t) == remove)
2357 {
2358 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2359 break;
2360 }
2361 }
2362 }
2363
2364 /* Merge the data types specified in the two decls. */
2365 TREE_TYPE (newdecl)
2366 = TREE_TYPE (olddecl)
2367 = composite_type (newtype, oldtype);
2368
2369 /* Lay the type out, unless already done. */
2370 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2371 {
2372 if (TREE_TYPE (newdecl) != error_mark_node)
2373 layout_type (TREE_TYPE (newdecl));
2374 if (TREE_CODE (newdecl) != FUNCTION_DECL
2375 && TREE_CODE (newdecl) != TYPE_DECL
2376 && TREE_CODE (newdecl) != CONST_DECL)
2377 layout_decl (newdecl, 0);
2378 }
2379 else
2380 {
2381 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2382 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2383 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2384 SET_DECL_MODE (newdecl, DECL_MODE (olddecl));
2385 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2386 {
2387 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2388 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2389 }
2390 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2391 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2392 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2393 DECL_WARN_IF_NOT_ALIGN (olddecl));
2394 }
2395
2396 /* Keep the old rtl since we can safely use it. */
2397 if (HAS_RTL_P (olddecl))
2398 COPY_DECL_RTL (olddecl, newdecl);
2399
2400 /* Merge the type qualifiers. */
2401 if (TREE_READONLY (newdecl))
2402 TREE_READONLY (olddecl) = 1;
2403
2404 if (TREE_THIS_VOLATILE (newdecl))
2405 TREE_THIS_VOLATILE (olddecl) = 1;
2406
2407 /* Merge deprecatedness. */
2408 if (TREE_DEPRECATED (newdecl))
2409 TREE_DEPRECATED (olddecl) = 1;
2410
2411 /* If a decl is in a system header and the other isn't, keep the one on the
2412 system header. Otherwise, keep source location of definition rather than
2413 declaration and of prototype rather than non-prototype unless that
2414 prototype is built-in. */
2415 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2416 && DECL_IN_SYSTEM_HEADER (olddecl)
2417 && !DECL_IN_SYSTEM_HEADER (newdecl) )
2418 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2419 else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2420 && DECL_IN_SYSTEM_HEADER (newdecl)
2421 && !DECL_IN_SYSTEM_HEADER (olddecl))
2422 DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2423 else if ((DECL_INITIAL (newdecl) == NULL_TREE
2424 && DECL_INITIAL (olddecl) != NULL_TREE)
2425 || (old_is_prototype && !new_is_prototype
2426 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2427 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2428
2429 /* Merge the initialization information. */
2430 if (DECL_INITIAL (newdecl) == NULL_TREE)
2431 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2432
2433 /* Merge the threadprivate attribute. */
2434 if (VAR_P (olddecl) && C_DECL_THREADPRIVATE_P (olddecl))
2435 C_DECL_THREADPRIVATE_P (newdecl) = 1;
2436
2437 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2438 {
2439 /* Copy the assembler name.
2440 Currently, it can only be defined in the prototype. */
2441 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2442
2443 /* Use visibility of whichever declaration had it specified */
2444 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2445 {
2446 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2447 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2448 }
2449
2450 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2451 {
2452 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2453 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2454 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2455 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2456 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2457 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2458 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2459 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2460 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2461 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2462 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2463 }
2464
2465 /* Merge the storage class information. */
2466 merge_weak (newdecl, olddecl);
2467
2468 /* For functions, static overrides non-static. */
2469 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2470 {
2471 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2472 /* This is since we don't automatically
2473 copy the attributes of NEWDECL into OLDDECL. */
2474 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2475 /* If this clears `static', clear it in the identifier too. */
2476 if (!TREE_PUBLIC (olddecl))
2477 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2478 }
2479 }
2480
2481 /* In c99, 'extern' declaration before (or after) 'inline' means this
2482 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2483 is present. */
2484 if (TREE_CODE (newdecl) == FUNCTION_DECL
2485 && !flag_gnu89_inline
2486 && (DECL_DECLARED_INLINE_P (newdecl)
2487 || DECL_DECLARED_INLINE_P (olddecl))
2488 && (!DECL_DECLARED_INLINE_P (newdecl)
2489 || !DECL_DECLARED_INLINE_P (olddecl)
2490 || !DECL_EXTERNAL (olddecl))
2491 && DECL_EXTERNAL (newdecl)
2492 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2493 && !current_function_decl)
2494 DECL_EXTERNAL (newdecl) = 0;
2495
2496 /* An inline definition following a static declaration is not
2497 DECL_EXTERNAL. */
2498 if (new_is_definition
2499 && (DECL_DECLARED_INLINE_P (newdecl)
2500 || DECL_DECLARED_INLINE_P (olddecl))
2501 && !TREE_PUBLIC (olddecl))
2502 DECL_EXTERNAL (newdecl) = 0;
2503
2504 if (DECL_EXTERNAL (newdecl))
2505 {
2506 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2507 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2508
2509 /* An extern decl does not override previous storage class. */
2510 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2511 if (!DECL_EXTERNAL (newdecl))
2512 {
2513 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2514 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2515 }
2516 }
2517 else
2518 {
2519 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2520 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2521 }
2522
2523 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2524 {
2525 /* If we're redefining a function previously defined as extern
2526 inline, make sure we emit debug info for the inline before we
2527 throw it away, in case it was inlined into a function that
2528 hasn't been written out yet. */
2529 if (new_is_definition && DECL_INITIAL (olddecl))
2530 /* The new defn must not be inline. */
2531 DECL_UNINLINABLE (newdecl) = 1;
2532 else
2533 {
2534 /* If either decl says `inline', this fn is inline, unless
2535 its definition was passed already. */
2536 if (DECL_DECLARED_INLINE_P (newdecl)
2537 || DECL_DECLARED_INLINE_P (olddecl))
2538 DECL_DECLARED_INLINE_P (newdecl) = 1;
2539
2540 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2541 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2542
2543 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2544 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2545 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2546 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2547 }
2548
2549 if (DECL_BUILT_IN (olddecl))
2550 {
2551 /* If redeclaring a builtin function, it stays built in.
2552 But it gets tagged as having been declared. */
2553 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2554 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2555 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2556 if (new_is_prototype)
2557 {
2558 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2559 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2560 {
2561 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2562 switch (fncode)
2563 {
2564 /* If a compatible prototype of these builtin functions
2565 is seen, assume the runtime implements it with the
2566 expected semantics. */
2567 case BUILT_IN_STPCPY:
2568 if (builtin_decl_explicit_p (fncode))
2569 set_builtin_decl_implicit_p (fncode, true);
2570 break;
2571 default:
2572 if (builtin_decl_explicit_p (fncode))
2573 set_builtin_decl_declared_p (fncode, true);
2574 break;
2575 }
2576
2577 copy_attributes_to_builtin (newdecl);
2578 }
2579 }
2580 else
2581 C_DECL_BUILTIN_PROTOTYPE (newdecl)
2582 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2583 }
2584
2585 /* Preserve function specific target and optimization options */
2586 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2587 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2588 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2589 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2590
2591 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2592 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2593 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2594 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2595
2596 /* Also preserve various other info from the definition. */
2597 if (!new_is_definition)
2598 {
2599 tree t;
2600 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2601 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2602 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2603 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2604 DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2605 for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2606 DECL_CONTEXT (t) = newdecl;
2607
2608 /* See if we've got a function to instantiate from. */
2609 if (DECL_SAVED_TREE (olddecl))
2610 DECL_ABSTRACT_ORIGIN (newdecl)
2611 = DECL_ABSTRACT_ORIGIN (olddecl);
2612 }
2613 }
2614
2615 /* Merge the USED information. */
2616 if (TREE_USED (olddecl))
2617 TREE_USED (newdecl) = 1;
2618 else if (TREE_USED (newdecl))
2619 TREE_USED (olddecl) = 1;
2620 if (VAR_P (olddecl) || TREE_CODE (olddecl) == PARM_DECL)
2621 DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2622 if (DECL_PRESERVE_P (olddecl))
2623 DECL_PRESERVE_P (newdecl) = 1;
2624 else if (DECL_PRESERVE_P (newdecl))
2625 DECL_PRESERVE_P (olddecl) = 1;
2626
2627 /* Merge DECL_COMMON */
2628 if (VAR_P (olddecl) && VAR_P (newdecl)
2629 && !lookup_attribute ("common", DECL_ATTRIBUTES (newdecl))
2630 && !lookup_attribute ("nocommon", DECL_ATTRIBUTES (newdecl)))
2631 DECL_COMMON (newdecl) = DECL_COMMON (newdecl) && DECL_COMMON (olddecl);
2632
2633 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2634 But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2635 DECL_ARGUMENTS (if appropriate). */
2636 {
2637 unsigned olddecl_uid = DECL_UID (olddecl);
2638 tree olddecl_context = DECL_CONTEXT (olddecl);
2639 tree olddecl_arguments = NULL;
2640 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2641 olddecl_arguments = DECL_ARGUMENTS (olddecl);
2642
2643 memcpy ((char *) olddecl + sizeof (struct tree_common),
2644 (char *) newdecl + sizeof (struct tree_common),
2645 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2646 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2647 switch (TREE_CODE (olddecl))
2648 {
2649 case FUNCTION_DECL:
2650 case VAR_DECL:
2651 {
2652 struct symtab_node *snode = olddecl->decl_with_vis.symtab_node;
2653
2654 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2655 (char *) newdecl + sizeof (struct tree_decl_common),
2656 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2657 olddecl->decl_with_vis.symtab_node = snode;
2658
2659 if ((DECL_EXTERNAL (olddecl)
2660 || TREE_PUBLIC (olddecl)
2661 || TREE_STATIC (olddecl))
2662 && DECL_SECTION_NAME (newdecl) != NULL)
2663 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2664
2665 /* This isn't quite correct for something like
2666 int __thread x attribute ((tls_model ("local-exec")));
2667 extern int __thread x;
2668 as we'll lose the "local-exec" model. */
2669 if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
2670 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2671 break;
2672 }
2673
2674 case FIELD_DECL:
2675 case PARM_DECL:
2676 case LABEL_DECL:
2677 case RESULT_DECL:
2678 case CONST_DECL:
2679 case TYPE_DECL:
2680 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2681 (char *) newdecl + sizeof (struct tree_decl_common),
2682 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2683 break;
2684
2685 default:
2686
2687 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2688 (char *) newdecl + sizeof (struct tree_decl_common),
2689 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2690 }
2691 DECL_UID (olddecl) = olddecl_uid;
2692 DECL_CONTEXT (olddecl) = olddecl_context;
2693 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2694 DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2695 }
2696
2697 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2698 so that encode_section_info has a chance to look at the new decl
2699 flags and attributes. */
2700 if (DECL_RTL_SET_P (olddecl)
2701 && (TREE_CODE (olddecl) == FUNCTION_DECL
2702 || (VAR_P (olddecl) && TREE_STATIC (olddecl))))
2703 make_decl_rtl (olddecl);
2704}
2705
2706/* Handle when a new declaration NEWDECL has the same name as an old
2707 one OLDDECL in the same binding contour. Prints an error message
2708 if appropriate.
2709
2710 If safely possible, alter OLDDECL to look like NEWDECL, and return
2711 true. Otherwise, return false. */
2712
2713static bool
2714duplicate_decls (tree newdecl, tree olddecl)
2715{
2716 tree newtype = NULL, oldtype = NULL;
2717
2718 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2719 {
2720 /* Avoid `unused variable' and other warnings for OLDDECL. */
2721 TREE_NO_WARNING (olddecl) = 1;
2722 return false;
2723 }
2724
2725 merge_decls (newdecl, olddecl, newtype, oldtype);
2726
2727 /* The NEWDECL will no longer be needed.
2728
2729 Before releasing the node, be sure to remove function from symbol
2730 table that might have been inserted there to record comdat group.
2731 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2732 structure is shared in between NEWDECL and OLDECL. */
2733 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2734 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2735 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2736 {
2737 struct symtab_node *snode = symtab_node::get (newdecl);
2738 if (snode)
2739 snode->remove ();
2740 }
2741 ggc_free (newdecl);
2742 return true;
2743}
2744
2745
2746/* Check whether decl-node NEW_DECL shadows an existing declaration. */
2747static void
2748warn_if_shadowing (tree new_decl)
2749{
2750 struct c_binding *b;
2751
2752 /* Shadow warnings wanted? */
2753 if (!(warn_shadow
2754 || warn_shadow_local
2755 || warn_shadow_compatible_local)
2756 /* No shadow warnings for internally generated vars. */
2757 || DECL_IS_BUILTIN (new_decl)
2758 /* No shadow warnings for vars made for inlining. */
2759 || DECL_FROM_INLINE (new_decl))
2760 return;
2761
2762 /* Is anything being shadowed? Invisible decls do not count. */
2763 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2764 if (b->decl && b->decl != new_decl && !b->invisible
2765 && (b->decl == error_mark_node
2766 || diagnostic_report_warnings_p (global_dc,
2767 DECL_SOURCE_LOCATION (b->decl))))
2768 {
2769 tree old_decl = b->decl;
2770 bool warned = false;
2771
2772 if (old_decl == error_mark_node)
2773 {
2774 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2775 "non-variable", new_decl);
2776 break;
2777 }
2778 else if (TREE_CODE (old_decl) == PARM_DECL)
2779 {
2780 enum opt_code warning_code;
2781
2782 /* If '-Wshadow=compatible-local' is specified without other
2783 -Wshadow= flags, we will warn only when the types of the
2784 shadowing variable (i.e. new_decl) and the shadowed variable
2785 (old_decl) are compatible. */
2786 if (warn_shadow)
2787 warning_code = OPT_Wshadow;
2788 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2789 warning_code = OPT_Wshadow_compatible_local;
2790 else
2791 warning_code = OPT_Wshadow_local;
2792 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2793 "declaration of %qD shadows a parameter",
2794 new_decl);
2795 }
2796 else if (DECL_FILE_SCOPE_P (old_decl))
2797 {
2798 /* Do not warn if a variable shadows a function, unless
2799 the variable is a function or a pointer-to-function. */
2800 if (TREE_CODE (old_decl) == FUNCTION_DECL
2801 && TREE_CODE (new_decl) != FUNCTION_DECL
2802 && !FUNCTION_POINTER_TYPE_P (TREE_TYPE (new_decl)))
2803 continue;
2804
2805 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), OPT_Wshadow,
2806 "declaration of %qD shadows a global "
2807 "declaration",
2808 new_decl);
2809 }
2810 else if (TREE_CODE (old_decl) == FUNCTION_DECL
2811 && DECL_BUILT_IN (old_decl))
2812 {
2813 warning (OPT_Wshadow, "declaration of %q+D shadows "
2814 "a built-in function", new_decl);
2815 break;
2816 }
2817 else
2818 {
2819 enum opt_code warning_code;
2820
2821 /* If '-Wshadow=compatible-local' is specified without other
2822 -Wshadow= flags, we will warn only when the types of the
2823 shadowing variable (i.e. new_decl) and the shadowed variable
2824 (old_decl) are compatible. */
2825 if (warn_shadow)
2826 warning_code = OPT_Wshadow;
2827 else if (comptypes (TREE_TYPE (old_decl), TREE_TYPE (new_decl)))
2828 warning_code = OPT_Wshadow_compatible_local;
2829 else
2830 warning_code = OPT_Wshadow_local;
2831 warned = warning_at (DECL_SOURCE_LOCATION (new_decl), warning_code,
2832 "declaration of %qD shadows a previous local",
2833 new_decl);
2834 }
2835
2836 if (warned)
2837 inform (DECL_SOURCE_LOCATION (old_decl),
2838 "shadowed declaration is here");
2839
2840 break;
2841 }
2842}
2843
2844/* Record a decl-node X as belonging to the current lexical scope.
2845 Check for errors (such as an incompatible declaration for the same
2846 name already seen in the same scope).
2847
2848 Returns either X or an old decl for the same name.
2849 If an old decl is returned, it may have been smashed
2850 to agree with what X says. */
2851
2852tree
2853pushdecl (tree x)
2854{
2855 tree name = DECL_NAME (x);
2856 struct c_scope *scope = current_scope;
2857 struct c_binding *b;
2858 bool nested = false;
2859 location_t locus = DECL_SOURCE_LOCATION (x);
2860
2861 /* Must set DECL_CONTEXT for everything not at file scope or
2862 DECL_FILE_SCOPE_P won't work. Local externs don't count
2863 unless they have initializers (which generate code). */
2864 if (current_function_decl
2865 && (!VAR_OR_FUNCTION_DECL_P (x)
2866 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2867 DECL_CONTEXT (x) = current_function_decl;
2868
2869 /* Anonymous decls are just inserted in the scope. */
2870 if (!name)
2871 {
2872 bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2873 locus);
2874 return x;
2875 }
2876
2877 /* First, see if there is another declaration with the same name in
2878 the current scope. If there is, duplicate_decls may do all the
2879 work for us. If duplicate_decls returns false, that indicates
2880 two incompatible decls in the same scope; we are to silently
2881 replace the old one (duplicate_decls has issued all appropriate
2882 diagnostics). In particular, we should not consider possible
2883 duplicates in the external scope, or shadowing. */
2884 b = I_SYMBOL_BINDING (name);
2885 if (b && B_IN_SCOPE (b, scope))
2886 {
2887 struct c_binding *b_ext, *b_use;
2888 tree type = TREE_TYPE (x);
2889 tree visdecl = b->decl;
2890 tree vistype = TREE_TYPE (visdecl);
2891 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2892 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2893 b->inner_comp = false;
2894 b_use = b;
2895 b_ext = b;
2896 /* If this is an external linkage declaration, we should check
2897 for compatibility with the type in the external scope before
2898 setting the type at this scope based on the visible
2899 information only. */
2900 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2901 {
2902 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2903 b_ext = b_ext->shadowed;
2904 if (b_ext)
2905 {
2906 b_use = b_ext;
2907 if (b_use->u.type)
2908 TREE_TYPE (b_use->decl) = b_use->u.type;
2909 }
2910 }
2911 if (duplicate_decls (x, b_use->decl))
2912 {
2913 if (b_use != b)
2914 {
2915 /* Save the updated type in the external scope and
2916 restore the proper type for this scope. */
2917 tree thistype;
2918 if (comptypes (vistype, type))
2919 thistype = composite_type (vistype, type);
2920 else
2921 thistype = TREE_TYPE (b_use->decl);
2922 b_use->u.type = TREE_TYPE (b_use->decl);
2923 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2924 && DECL_BUILT_IN (b_use->decl))
2925 thistype
2926 = build_type_attribute_variant (thistype,
2927 TYPE_ATTRIBUTES
2928 (b_use->u.type));
2929 TREE_TYPE (b_use->decl) = thistype;
2930 }
2931 return b_use->decl;
2932 }
2933 else
2934 goto skip_external_and_shadow_checks;
2935 }
2936
2937 /* All declarations with external linkage, and all external
2938 references, go in the external scope, no matter what scope is
2939 current. However, the binding in that scope is ignored for
2940 purposes of normal name lookup. A separate binding structure is
2941 created in the requested scope; this governs the normal
2942 visibility of the symbol.
2943
2944 The binding in the externals scope is used exclusively for
2945 detecting duplicate declarations of the same object, no matter
2946 what scope they are in; this is what we do here. (C99 6.2.7p2:
2947 All declarations that refer to the same object or function shall
2948 have compatible type; otherwise, the behavior is undefined.) */
2949 if (DECL_EXTERNAL (x) || scope == file_scope)
2950 {
2951 tree type = TREE_TYPE (x);
2952 tree vistype = NULL_TREE;
2953 tree visdecl = NULL_TREE;
2954 bool type_saved = false;
2955 if (b && !B_IN_EXTERNAL_SCOPE (b)
2956 && VAR_OR_FUNCTION_DECL_P (b->decl)
2957 && DECL_FILE_SCOPE_P (b->decl))
2958 {
2959 visdecl = b->decl;
2960 vistype = TREE_TYPE (visdecl);
2961 }
2962 if (scope != file_scope
2963 && !DECL_IN_SYSTEM_HEADER (x))
2964 warning_at (locus, OPT_Wnested_externs,
2965 "nested extern declaration of %qD", x);
2966
2967 while (b && !B_IN_EXTERNAL_SCOPE (b))
2968 {
2969 /* If this decl might be modified, save its type. This is
2970 done here rather than when the decl is first bound
2971 because the type may change after first binding, through
2972 being completed or through attributes being added. If we
2973 encounter multiple such decls, only the first should have
2974 its type saved; the others will already have had their
2975 proper types saved and the types will not have changed as
2976 their scopes will not have been re-entered. */
2977 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2978 {
2979 b->u.type = TREE_TYPE (b->decl);
2980 type_saved = true;
2981 }
2982 if (B_IN_FILE_SCOPE (b)
2983 && VAR_P (b->decl)
2984 && TREE_STATIC (b->decl)
2985 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2986 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2987 && TREE_CODE (type) == ARRAY_TYPE
2988 && TYPE_DOMAIN (type)
2989 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2990 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2991 {
2992 /* Array type completed in inner scope, which should be
2993 diagnosed if the completion does not have size 1 and
2994 it does not get completed in the file scope. */
2995 b->inner_comp = true;
2996 }
2997 b = b->shadowed;
2998 }
2999
3000 /* If a matching external declaration has been found, set its
3001 type to the composite of all the types of that declaration.
3002 After the consistency checks, it will be reset to the
3003 composite of the visible types only. */
3004 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3005 && b->u.type)
3006 TREE_TYPE (b->decl) = b->u.type;
3007
3008 /* The point of the same_translation_unit_p check here is,
3009 we want to detect a duplicate decl for a construct like
3010 foo() { extern bar(); } ... static bar(); but not if
3011 they are in different translation units. In any case,
3012 the static does not go in the externals scope. */
3013 if (b
3014 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
3015 && duplicate_decls (x, b->decl))
3016 {
3017 tree thistype;
3018 if (vistype)
3019 {
3020 if (comptypes (vistype, type))
3021 thistype = composite_type (vistype, type);
3022 else
3023 thistype = TREE_TYPE (b->decl);
3024 }
3025 else
3026 thistype = type;
3027 b->u.type = TREE_TYPE (b->decl);
3028 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
3029 thistype
3030 = build_type_attribute_variant (thistype,
3031 TYPE_ATTRIBUTES (b->u.type));
3032 TREE_TYPE (b->decl) = thistype;
3033 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
3034 locus);
3035 return b->decl;
3036 }
3037 else if (TREE_PUBLIC (x))
3038 {
3039 if (visdecl && !b && duplicate_decls (x, visdecl))
3040 {
3041 /* An external declaration at block scope referring to a
3042 visible entity with internal linkage. The composite
3043 type will already be correct for this scope, so we
3044 just need to fall through to make the declaration in
3045 this scope. */
3046 nested = true;
3047 x = visdecl;
3048 }
3049 else
3050 {
3051 bind (name, x, external_scope, /*invisible=*/true,
3052 /*nested=*/false, locus);
3053 nested = true;
3054 }
3055 }
3056 }
3057
3058 if (TREE_CODE (x) != PARM_DECL)
3059 warn_if_shadowing (x);
3060
3061 skip_external_and_shadow_checks:
3062 if (TREE_CODE (x) == TYPE_DECL)
3063 {
3064 /* So this is a typedef, set its underlying type. */
3065 set_underlying_type (x);
3066
3067 /* If X is a typedef defined in the current function, record it
3068 for the purpose of implementing the -Wunused-local-typedefs
3069 warning. */
3070 record_locally_defined_typedef (x);
3071 }
3072
3073 bind (name, x, scope, /*invisible=*/false, nested, locus);
3074
3075 /* If x's type is incomplete because it's based on a
3076 structure or union which has not yet been fully declared,
3077 attach it to that structure or union type, so we can go
3078 back and complete the variable declaration later, if the
3079 structure or union gets fully declared.
3080
3081 If the input is erroneous, we can have error_mark in the type
3082 slot (e.g. "f(void a, ...)") - that doesn't count as an
3083 incomplete type. */
3084 if (TREE_TYPE (x) != error_mark_node
3085 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
3086 {
3087 tree element = TREE_TYPE (x);
3088
3089 while (TREE_CODE (element) == ARRAY_TYPE)
3090 element = TREE_TYPE (element);
3091 element = TYPE_MAIN_VARIANT (element);
3092
3093 if (RECORD_OR_UNION_TYPE_P (element)
3094 && (TREE_CODE (x) != TYPE_DECL
3095 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
3096 && !COMPLETE_TYPE_P (element))
3097 C_TYPE_INCOMPLETE_VARS (element)
3098 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
3099 }
3100 return x;
3101}
3102
3103
3104/* Issue a warning about implicit function declaration. ID is the function
3105 identifier, OLDDECL is a declaration of the function in a different scope,
3106 or NULL_TREE. */
3107
3108static void
3109implicit_decl_warning (location_t loc, tree id, tree olddecl)
3110{
3111 if (!warn_implicit_function_declaration)
3112 return;
3113
3114 bool warned;
3115 name_hint hint;
3116 if (!olddecl)
3117 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_FUNCTION_NAME, loc);
3118
3119 if (flag_isoc99)
3120 {
3121 if (hint)
3122 {
3123 gcc_rich_location richloc (loc);
3124 richloc.add_fixit_replace (hint.suggestion ());
3125 warned = pedwarn (&richloc, OPT_Wimplicit_function_declaration,
3126 "implicit declaration of function %qE;"
3127 " did you mean %qs?",
3128 id, hint.suggestion ());
3129 }
3130 else
3131 warned = pedwarn (loc, OPT_Wimplicit_function_declaration,
3132 "implicit declaration of function %qE", id);
3133 }
3134 else if (hint)
3135 {
3136 gcc_rich_location richloc (loc);
3137 richloc.add_fixit_replace (hint.suggestion ());
3138 warned = warning_at
3139 (&richloc, OPT_Wimplicit_function_declaration,
3140 G_("implicit declaration of function %qE; did you mean %qs?"),
3141 id, hint.suggestion ());
3142 }
3143 else
3144 warned = warning_at (loc, OPT_Wimplicit_function_declaration,
3145 G_("implicit declaration of function %qE"), id);
3146
3147 if (olddecl && warned)
3148 locate_old_decl (olddecl);
3149
3150 if (!warned)
3151 hint.suppress ();
3152}
3153
3154/* This function represents mapping of a function code FCODE
3155 to its respective header. */
3156
3157static const char *
3158header_for_builtin_fn (enum built_in_function fcode)
3159{
3160 switch (fcode)
3161 {
3162 CASE_FLT_FN (BUILT_IN_ACOS):
3163 CASE_FLT_FN (BUILT_IN_ACOSH):
3164 CASE_FLT_FN (BUILT_IN_ASIN):
3165 CASE_FLT_FN (BUILT_IN_ASINH):
3166 CASE_FLT_FN (BUILT_IN_ATAN):
3167 CASE_FLT_FN (BUILT_IN_ATANH):
3168 CASE_FLT_FN (BUILT_IN_ATAN2):
3169 CASE_FLT_FN (BUILT_IN_CBRT):
3170 CASE_FLT_FN (BUILT_IN_CEIL):
3171 CASE_FLT_FN (BUILT_IN_COPYSIGN):
3172 CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN):
3173 CASE_FLT_FN (BUILT_IN_COS):
3174 CASE_FLT_FN (BUILT_IN_COSH):
3175 CASE_FLT_FN (BUILT_IN_ERF):
3176 CASE_FLT_FN (BUILT_IN_ERFC):
3177 CASE_FLT_FN (BUILT_IN_EXP):
3178 CASE_FLT_FN (BUILT_IN_EXP2):
3179 CASE_FLT_FN (BUILT_IN_EXPM1):
3180 CASE_FLT_FN (BUILT_IN_FABS):
3181 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FABS):
3182 CASE_FLT_FN (BUILT_IN_FDIM):
3183 CASE_FLT_FN (BUILT_IN_FLOOR):
3184 CASE_FLT_FN (BUILT_IN_FMA):
3185 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMA):
3186 CASE_FLT_FN (BUILT_IN_FMAX):
3187 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMAX):
3188 CASE_FLT_FN (BUILT_IN_FMIN):
3189 CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMIN):
3190 CASE_FLT_FN (BUILT_IN_FMOD):
3191 CASE_FLT_FN (BUILT_IN_FREXP):
3192 CASE_FLT_FN (BUILT_IN_HYPOT):
3193 CASE_FLT_FN (BUILT_IN_ILOGB):
3194 CASE_FLT_FN (BUILT_IN_LDEXP):
3195 CASE_FLT_FN (BUILT_IN_LGAMMA):
3196 CASE_FLT_FN (BUILT_IN_LLRINT):
3197 CASE_FLT_FN (BUILT_IN_LLROUND):
3198 CASE_FLT_FN (BUILT_IN_LOG):
3199 CASE_FLT_FN (BUILT_IN_LOG10):
3200 CASE_FLT_FN (BUILT_IN_LOG1P):
3201 CASE_FLT_FN (BUILT_IN_LOG2):
3202 CASE_FLT_FN (BUILT_IN_LOGB):
3203 CASE_FLT_FN (BUILT_IN_LRINT):
3204 CASE_FLT_FN (BUILT_IN_LROUND):
3205 CASE_FLT_FN (BUILT_IN_MODF):
3206 CASE_FLT_FN (BUILT_IN_NAN):
3207 CASE_FLT_FN (BUILT_IN_NEARBYINT):
3208 CASE_FLT_FN (BUILT_IN_NEXTAFTER):
3209 CASE_FLT_FN (BUILT_IN_NEXTTOWARD):
3210 CASE_FLT_FN (BUILT_IN_POW):
3211 CASE_FLT_FN (BUILT_IN_REMAINDER):
3212 CASE_FLT_FN (BUILT_IN_REMQUO):
3213 CASE_FLT_FN (BUILT_IN_RINT):
3214 CASE_FLT_FN (BUILT_IN_ROUND):
3215 CASE_FLT_FN (BUILT_IN_SCALBLN):
3216 CASE_FLT_FN (BUILT_IN_SCALBN):
3217 CASE_FLT_FN (BUILT_IN_SIN):
3218 CASE_FLT_FN (BUILT_IN_SINH):
3219 CASE_FLT_FN (BUILT_IN_SINCOS):
3220 CASE_FLT_FN (BUILT_IN_SQRT):
3221 CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
3222 CASE_FLT_FN (BUILT_IN_TAN):
3223 CASE_FLT_FN (BUILT_IN_TANH):
3224 CASE_FLT_FN (BUILT_IN_TGAMMA):
3225 CASE_FLT_FN (BUILT_IN_TRUNC):
3226 case BUILT_IN_ISINF:
3227 case BUILT_IN_ISNAN:
3228 return "<math.h>";
3229 CASE_FLT_FN (BUILT_IN_CABS):
3230 CASE_FLT_FN (BUILT_IN_CACOS):
3231 CASE_FLT_FN (BUILT_IN_CACOSH):
3232 CASE_FLT_FN (BUILT_IN_CARG):
3233 CASE_FLT_FN (BUILT_IN_CASIN):
3234 CASE_FLT_FN (BUILT_IN_CASINH):
3235 CASE_FLT_FN (BUILT_IN_CATAN):
3236 CASE_FLT_FN (BUILT_IN_CATANH):
3237 CASE_FLT_FN (BUILT_IN_CCOS):
3238 CASE_FLT_FN (BUILT_IN_CCOSH):
3239 CASE_FLT_FN (BUILT_IN_CEXP):
3240 CASE_FLT_FN (BUILT_IN_CIMAG):
3241 CASE_FLT_FN (BUILT_IN_CLOG):
3242 CASE_FLT_FN (BUILT_IN_CONJ):
3243 CASE_FLT_FN (BUILT_IN_CPOW):
3244 CASE_FLT_FN (BUILT_IN_CPROJ):
3245 CASE_FLT_FN (BUILT_IN_CREAL):
3246 CASE_FLT_FN (BUILT_IN_CSIN):
3247 CASE_FLT_FN (BUILT_IN_CSINH):
3248 CASE_FLT_FN (BUILT_IN_CSQRT):
3249 CASE_FLT_FN (BUILT_IN_CTAN):
3250 CASE_FLT_FN (BUILT_IN_CTANH):
3251 return "<complex.h>";
3252 case BUILT_IN_MEMCHR:
3253 case BUILT_IN_MEMCMP:
3254 case BUILT_IN_MEMCPY:
3255 case BUILT_IN_MEMMOVE:
3256 case BUILT_IN_MEMSET:
3257 case BUILT_IN_STRCAT:
3258 case BUILT_IN_STRCHR:
3259 case BUILT_IN_STRCMP:
3260 case BUILT_IN_STRCPY:
3261 case BUILT_IN_STRCSPN:
3262 case BUILT_IN_STRLEN:
3263 case BUILT_IN_STRNCAT:
3264 case BUILT_IN_STRNCMP:
3265 case BUILT_IN_STRNCPY:
3266 case BUILT_IN_STRPBRK:
3267 case BUILT_IN_STRRCHR:
3268 case BUILT_IN_STRSPN:
3269 case BUILT_IN_STRSTR:
3270 return "<string.h>";
3271 case BUILT_IN_FPRINTF:
3272 case BUILT_IN_PUTC:
3273 case BUILT_IN_FPUTC:
3274 case BUILT_IN_FPUTS:
3275 case BUILT_IN_FSCANF:
3276 case BUILT_IN_FWRITE:
3277 case BUILT_IN_PRINTF:
3278 case BUILT_IN_PUTCHAR:
3279 case BUILT_IN_PUTS:
3280 case BUILT_IN_SCANF:
3281 case BUILT_IN_SNPRINTF:
3282 case BUILT_IN_SPRINTF:
3283 case BUILT_IN_SSCANF:
3284 case BUILT_IN_VFPRINTF:
3285 case BUILT_IN_VFSCANF:
3286 case BUILT_IN_VPRINTF:
3287 case BUILT_IN_VSCANF:
3288 case BUILT_IN_VSNPRINTF:
3289 case BUILT_IN_VSPRINTF:
3290 case BUILT_IN_VSSCANF:
3291 return "<stdio.h>";
3292 case BUILT_IN_ISALNUM:
3293 case BUILT_IN_ISALPHA:
3294 case BUILT_IN_ISBLANK:
3295 case BUILT_IN_ISCNTRL:
3296 case BUILT_IN_ISDIGIT:
3297 case BUILT_IN_ISGRAPH:
3298 case BUILT_IN_ISLOWER:
3299 case BUILT_IN_ISPRINT:
3300 case BUILT_IN_ISPUNCT:
3301 case BUILT_IN_ISSPACE:
3302 case BUILT_IN_ISUPPER:
3303 case BUILT_IN_ISXDIGIT:
3304 case BUILT_IN_TOLOWER:
3305 case BUILT_IN_TOUPPER:
3306 return "<ctype.h>";
3307 case BUILT_IN_ISWALNUM:
3308 case BUILT_IN_ISWALPHA:
3309 case BUILT_IN_ISWBLANK:
3310 case BUILT_IN_ISWCNTRL:
3311 case BUILT_IN_ISWDIGIT:
3312 case BUILT_IN_ISWGRAPH:
3313 case BUILT_IN_ISWLOWER:
3314 case BUILT_IN_ISWPRINT:
3315 case BUILT_IN_ISWPUNCT:
3316 case BUILT_IN_ISWSPACE:
3317 case BUILT_IN_ISWUPPER:
3318 case BUILT_IN_ISWXDIGIT:
3319 case BUILT_IN_TOWLOWER:
3320 case BUILT_IN_TOWUPPER:
3321 return "<wctype.h>";
3322 case BUILT_IN_ABORT:
3323 case BUILT_IN_ABS:
3324 case BUILT_IN_CALLOC:
3325 case BUILT_IN_EXIT:
3326 case BUILT_IN_FREE:
3327 case BUILT_IN_LABS:
3328 case BUILT_IN_LLABS:
3329 case BUILT_IN_MALLOC:
3330 case BUILT_IN_REALLOC:
3331 case BUILT_IN__EXIT2:
3332 case BUILT_IN_ALIGNED_ALLOC:
3333 return "<stdlib.h>";
3334 case BUILT_IN_IMAXABS:
3335 return "<inttypes.h>";
3336 case BUILT_IN_STRFTIME:
3337 return "<time.h>";
3338 default:
3339 return NULL;
3340 }
3341}
3342
3343/* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
3344 function of type int (). */
3345
3346tree
3347implicitly_declare (location_t loc, tree functionid)
3348{
3349 struct c_binding *b;
3350 tree decl = NULL_TREE;
3351 tree asmspec_tree;
3352
3353 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
3354 {
3355 if (B_IN_SCOPE (b, external_scope))
3356 {
3357 decl = b->decl;
3358 break;
3359 }
3360 }
3361
3362 if (decl)
3363 {
3364 if (TREE_CODE (decl) != FUNCTION_DECL)
3365 return decl;
3366
3367 /* FIXME: Objective-C has weird not-really-builtin functions
3368 which are supposed to be visible automatically. They wind up
3369 in the external scope because they're pushed before the file
3370 scope gets created. Catch this here and rebind them into the
3371 file scope. */
3372 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
3373 {
3374 bind (functionid, decl, file_scope,
3375 /*invisible=*/false, /*nested=*/true,
3376 DECL_SOURCE_LOCATION (decl));
3377 return decl;
3378 }
3379 else
3380 {
3381 tree newtype = default_function_type;
3382 if (b->u.type)
3383 TREE_TYPE (decl) = b->u.type;
3384 /* Implicit declaration of a function already declared
3385 (somehow) in a different scope, or as a built-in.
3386 If this is the first time this has happened, warn;
3387 then recycle the old declaration but with the new type. */
3388 if (!C_DECL_IMPLICIT (decl))
3389 {
3390 implicit_decl_warning (loc, functionid, decl);
3391 C_DECL_IMPLICIT (decl) = 1;
3392 }
3393 if (DECL_BUILT_IN (decl))
3394 {
3395 newtype = build_type_attribute_variant (newtype,
3396 TYPE_ATTRIBUTES
3397 (TREE_TYPE (decl)));
3398 if (!comptypes (newtype, TREE_TYPE (decl)))
3399 {
3400 bool warned = warning_at (loc, 0, "incompatible implicit "
3401 "declaration of built-in "
3402 "function %qD", decl);
3403 /* See if we can hint which header to include. */
3404 const char *header
3405 = header_for_builtin_fn (DECL_FUNCTION_CODE (decl));
3406 if (header != NULL && warned)
3407 {
3408 rich_location richloc (line_table, loc);
3409 maybe_add_include_fixit (&richloc, header);
3410 inform (&richloc,
3411 "include %qs or provide a declaration of %qD",
3412 header, decl);
3413 }
3414 newtype = TREE_TYPE (decl);
3415 }
3416 }
3417 else
3418 {
3419 if (!comptypes (newtype, TREE_TYPE (decl)))
3420 {
3421 error_at (loc, "incompatible implicit declaration of "
3422 "function %qD", decl);
3423 locate_old_decl (decl);
3424 }
3425 }
3426 b->u.type = TREE_TYPE (decl);
3427 TREE_TYPE (decl) = newtype;
3428 bind (functionid, decl, current_scope,
3429 /*invisible=*/false, /*nested=*/true,
3430 DECL_SOURCE_LOCATION (decl));
3431 return decl;
3432 }
3433 }
3434
3435 /* Not seen before. */
3436 decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
3437 DECL_EXTERNAL (decl) = 1;
3438 TREE_PUBLIC (decl) = 1;
3439 C_DECL_IMPLICIT (decl) = 1;
3440 implicit_decl_warning (loc, functionid, 0);
3441 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
3442 if (asmspec_tree)
3443 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
3444
3445 /* C89 says implicit declarations are in the innermost block.
3446 So we record the decl in the standard fashion. */
3447 decl = pushdecl (decl);
3448
3449 /* No need to call objc_check_decl here - it's a function type. */
3450 rest_of_decl_compilation (decl, 0, 0);
3451
3452 /* Write a record describing this implicit function declaration
3453 to the prototypes file (if requested). */
3454 gen_aux_info_record (decl, 0, 1, 0);
3455
3456 /* Possibly apply some default attributes to this implicit declaration. */
3457 decl_attributes (&decl, NULL_TREE, 0);
3458
3459 return decl;
3460}
3461
3462/* Issue an error message for a reference to an undeclared variable
3463 ID, including a reference to a builtin outside of function-call
3464 context. Establish a binding of the identifier to error_mark_node
3465 in an appropriate scope, which will suppress further errors for the
3466 same identifier. The error message should be given location LOC. */
3467void
3468undeclared_variable (location_t loc, tree id)
3469{
3470 static bool already = false;
3471 struct c_scope *scope;
3472
3473 if (current_function_decl == NULL_TREE)
3474 {
3475 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3476 if (guessed_id)
3477 {
3478 gcc_rich_location richloc (loc);
3479 richloc.add_fixit_replace (guessed_id.suggestion ());
3480 error_at (&richloc,
3481 "%qE undeclared here (not in a function);"
3482 " did you mean %qs?",
3483 id, guessed_id.suggestion ());
3484 }
3485 else
3486 error_at (loc, "%qE undeclared here (not in a function)", id);
3487 scope = current_scope;
3488 }
3489 else
3490 {
3491 if (!objc_diagnose_private_ivar (id))
3492 {
3493 name_hint guessed_id = lookup_name_fuzzy (id, FUZZY_LOOKUP_NAME, loc);
3494 if (guessed_id)
3495 {
3496 gcc_rich_location richloc (loc);
3497 richloc.add_fixit_replace (guessed_id.suggestion ());
3498 error_at (&richloc,
3499 "%qE undeclared (first use in this function);"
3500 " did you mean %qs?",
3501 id, guessed_id.suggestion ());
3502 }
3503 else
3504 error_at (loc, "%qE undeclared (first use in this function)", id);
3505 }
3506 if (!already)
3507 {
3508 inform (loc, "each undeclared identifier is reported only"
3509 " once for each function it appears in");
3510 already = true;
3511 }
3512
3513 /* If we are parsing old-style parameter decls, current_function_decl
3514 will be nonnull but current_function_scope will be null. */
3515 scope = current_function_scope ? current_function_scope : current_scope;
3516 }
3517 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3518 UNKNOWN_LOCATION);
3519}
3520
3521/* Subroutine of lookup_label, declare_label, define_label: construct a
3522 LABEL_DECL with all the proper frills. Also create a struct
3523 c_label_vars initialized for the current scope. */
3524
3525static tree
3526make_label (location_t location, tree name, bool defining,
3527 struct c_label_vars **p_label_vars)
3528{
3529 tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3530 DECL_CONTEXT (label) = current_function_decl;
3531 SET_DECL_MODE (label, VOIDmode);
3532
3533 c_label_vars *label_vars = ggc_alloc<c_label_vars> ();
3534 label_vars->shadowed = NULL;
3535 set_spot_bindings (&label_vars->label_bindings, defining);
3536 label_vars->decls_in_scope = make_tree_vector ();
3537 label_vars->gotos = NULL;
3538 *p_label_vars = label_vars;
3539
3540 return label;
3541}
3542
3543/* Get the LABEL_DECL corresponding to identifier NAME as a label.
3544 Create one if none exists so far for the current function.
3545 This is called when a label is used in a goto expression or
3546 has its address taken. */
3547
3548tree
3549lookup_label (tree name)
3550{
3551 tree label;
3552 struct c_label_vars *label_vars;
3553
3554 if (current_function_scope == 0)
3555 {
3556 error ("label %qE referenced outside of any function", name);
3557 return NULL_TREE;
3558 }
3559
3560 /* Use a label already defined or ref'd with this name, but not if
3561 it is inherited from a containing function and wasn't declared
3562 using __label__. */
3563 label = I_LABEL_DECL (name);
3564 if (label && (DECL_CONTEXT (label) == current_function_decl
3565 || C_DECLARED_LABEL_FLAG (label)))
3566 {
3567 /* If the label has only been declared, update its apparent
3568 location to point here, for better diagnostics if it
3569 turns out not to have been defined. */
3570 if (DECL_INITIAL (label) == NULL_TREE)
3571 DECL_SOURCE_LOCATION (label) = input_location;
3572 return label;
3573 }
3574
3575 /* No label binding for that identifier; make one. */
3576 label = make_label (input_location, name, false, &label_vars);
3577
3578 /* Ordinary labels go in the current function scope. */
3579 bind_label (name, label, current_function_scope, label_vars);
3580
3581 return label;
3582}
3583
3584/* Issue a warning about DECL for a goto statement at GOTO_LOC going
3585 to LABEL. */
3586
3587static void
3588warn_about_goto (location_t goto_loc, tree label, tree decl)
3589{
3590 if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3591 error_at (goto_loc,
3592 "jump into scope of identifier with variably modified type");
3593 else
3594 warning_at (goto_loc, OPT_Wjump_misses_init,
3595 "jump skips variable initialization");
3596 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3597 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3598}
3599
3600/* Look up a label because of a goto statement. This is like
3601 lookup_label, but also issues any appropriate warnings. */
3602
3603tree
3604lookup_label_for_goto (location_t loc, tree name)
3605{
3606 tree label;
3607 struct c_label_vars *label_vars;
3608 unsigned int ix;
3609 tree decl;
3610
3611 label = lookup_label (name);
3612 if (label == NULL_TREE)
3613 return NULL_TREE;
3614
3615 /* If we are jumping to a different function, we can't issue any
3616 useful warnings. */
3617 if (DECL_CONTEXT (label) != current_function_decl)
3618 {
3619 gcc_assert (C_DECLARED_LABEL_FLAG (label));
3620 return label;
3621 }
3622
3623 label_vars = I_LABEL_BINDING (name)->u.label;
3624
3625 /* If the label has not yet been defined, then push this goto on a
3626 list for possible later warnings. */
3627 if (label_vars->label_bindings.scope == NULL)
3628 {
3629 c_goto_bindings *g = ggc_alloc<c_goto_bindings> ();
3630
3631 g->loc = loc;
3632 set_spot_bindings (&g->goto_bindings, true);
3633 vec_safe_push (label_vars->gotos, g);
3634 return label;
3635 }
3636
3637 /* If there are any decls in label_vars->decls_in_scope, then this
3638 goto has missed the declaration of the decl. This happens for a
3639 case like
3640 int i = 1;
3641 lab:
3642 ...
3643 goto lab;
3644 Issue a warning or error. */
3645 FOR_EACH_VEC_SAFE_ELT (label_vars->decls_in_scope, ix, decl)
3646 warn_about_goto (loc, label, decl);
3647
3648 if (label_vars->label_bindings.left_stmt_expr)
3649 {
3650 error_at (loc, "jump into statement expression");
3651 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3652 }
3653
3654 return label;
3655}
3656
3657/* Make a label named NAME in the current function, shadowing silently
3658 any that may be inherited from containing functions or containing
3659 scopes. This is called for __label__ declarations. */
3660
3661tree
3662declare_label (tree name)
3663{
3664 struct c_binding *b = I_LABEL_BINDING (name);
3665 tree label;
3666 struct c_label_vars *label_vars;
3667
3668 /* Check to make sure that the label hasn't already been declared
3669 at this scope */
3670 if (b && B_IN_CURRENT_SCOPE (b))
3671 {
3672 error ("duplicate label declaration %qE", name);
3673 locate_old_decl (b->decl);
3674
3675 /* Just use the previous declaration. */
3676 return b->decl;
3677 }
3678
3679 label = make_label (input_location, name, false, &label_vars);
3680 C_DECLARED_LABEL_FLAG (label) = 1;
3681
3682 /* Declared labels go in the current scope. */
3683 bind_label (name, label, current_scope, label_vars);
3684
3685 return label;
3686}
3687
3688/* When we define a label, issue any appropriate warnings if there are
3689 any gotos earlier in the function which jump to this label. */
3690
3691static void
3692check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3693{
3694 unsigned int ix;
3695 struct c_goto_bindings *g;
3696
3697 FOR_EACH_VEC_SAFE_ELT (label_vars->gotos, ix, g)
3698 {
3699 struct c_binding *b;
3700 struct c_scope *scope;
3701
3702 /* We have a goto to this label. The goto is going forward. In
3703 g->scope, the goto is going to skip any binding which was
3704 defined after g->bindings_in_scope. */
3705 if (g->goto_bindings.scope->has_jump_unsafe_decl)
3706 {
3707 for (b = g->goto_bindings.scope->bindings;
3708 b != g->goto_bindings.bindings_in_scope;
3709 b = b->prev)
3710 {
3711 if (decl_jump_unsafe (b->decl))
3712 warn_about_goto (g->loc, label, b->decl);
3713 }
3714 }
3715
3716 /* We also need to warn about decls defined in any scopes
3717 between the scope of the label and the scope of the goto. */
3718 for (scope = label_vars->label_bindings.scope;
3719 scope != g->goto_bindings.scope;
3720 scope = scope->outer)
3721 {
3722 gcc_assert (scope != NULL);
3723 if (scope->has_jump_unsafe_decl)
3724 {
3725 if (scope == label_vars->label_bindings.scope)
3726 b = label_vars->label_bindings.bindings_in_scope;
3727 else
3728 b = scope->bindings;
3729 for (; b != NULL; b = b->prev)
3730 {
3731 if (decl_jump_unsafe (b->decl))
3732 warn_about_goto (g->loc, label, b->decl);
3733 }
3734 }
3735 }
3736
3737 if (g->goto_bindings.stmt_exprs > 0)
3738 {
3739 error_at (g->loc, "jump into statement expression");
3740 inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3741 label);
3742 }
3743 }
3744
3745 /* Now that the label is defined, we will issue warnings about
3746 subsequent gotos to this label when we see them. */
3747 vec_safe_truncate (label_vars->gotos, 0);
3748 label_vars->gotos = NULL;
3749}
3750
3751/* Define a label, specifying the location in the source file.
3752 Return the LABEL_DECL node for the label, if the definition is valid.
3753 Otherwise return NULL_TREE. */
3754
3755tree
3756define_label (location_t location, tree name)
3757{
3758 /* Find any preexisting label with this name. It is an error
3759 if that label has already been defined in this function, or
3760 if there is a containing function with a declared label with
3761 the same name. */
3762 tree label = I_LABEL_DECL (name);
3763
3764 if (label
3765 && ((DECL_CONTEXT (label) == current_function_decl
3766 && DECL_INITIAL (label) != NULL_TREE)
3767 || (DECL_CONTEXT (label) != current_function_decl
3768 && C_DECLARED_LABEL_FLAG (label))))
3769 {
3770 error_at (location, "duplicate label %qD", label);
3771 locate_old_decl (label);
3772 return NULL_TREE;
3773 }
3774 else if (label && DECL_CONTEXT (label) == current_function_decl)
3775 {
3776 struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3777
3778 /* The label has been used or declared already in this function,
3779 but not defined. Update its location to point to this
3780 definition. */
3781 DECL_SOURCE_LOCATION (label) = location;
3782 set_spot_bindings (&label_vars->label_bindings, true);
3783
3784 /* Issue warnings as required about any goto statements from
3785 earlier in the function. */
3786 check_earlier_gotos (label, label_vars);
3787 }
3788 else
3789 {
3790 struct c_label_vars *label_vars;
3791
3792 /* No label binding for that identifier; make one. */
3793 label = make_label (location, name, true, &label_vars);
3794
3795 /* Ordinary labels go in the current function scope. */
3796 bind_label (name, label, current_function_scope, label_vars);
3797 }
3798
3799 if (!in_system_header_at (input_location) && lookup_name (name))
3800 warning_at (location, OPT_Wtraditional,
3801 "traditional C lacks a separate namespace "
3802 "for labels, identifier %qE conflicts", name);
3803
3804 /* Mark label as having been defined. */
3805 DECL_INITIAL (label) = error_mark_node;
3806 return label;
3807}
3808
3809/* Get the bindings for a new switch statement. This is used to issue
3810 warnings as appropriate for jumps from the switch to case or
3811 default labels. */
3812
3813struct c_spot_bindings *
3814c_get_switch_bindings (void)
3815{
3816 struct c_spot_bindings *switch_bindings;
3817
3818 switch_bindings = XNEW (struct c_spot_bindings);
3819 set_spot_bindings (switch_bindings, true);
3820 return switch_bindings;
3821}
3822
3823void
3824c_release_switch_bindings (struct c_spot_bindings *bindings)
3825{
3826 gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3827 XDELETE (bindings);
3828}
3829
3830/* This is called at the point of a case or default label to issue
3831 warnings about decls as needed. It returns true if it found an
3832 error, not just a warning. */
3833
3834bool
3835c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3836 location_t switch_loc, location_t case_loc)
3837{
3838 bool saw_error;
3839 struct c_scope *scope;
3840
3841 saw_error = false;
3842 for (scope = current_scope;
3843 scope != switch_bindings->scope;
3844 scope = scope->outer)
3845 {
3846 struct c_binding *b;
3847
3848 gcc_assert (scope != NULL);
3849
3850 if (!scope->has_jump_unsafe_decl)
3851 continue;
3852
3853 for (b = scope->bindings; b != NULL; b = b->prev)
3854 {
3855 if (decl_jump_unsafe (b->decl))
3856 {
3857 if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3858 {
3859 saw_error = true;
3860 error_at (case_loc,
3861 ("switch jumps into scope of identifier with "
3862 "variably modified type"));
3863 }
3864 else
3865 warning_at (case_loc, OPT_Wjump_misses_init,
3866 "switch jumps over variable initialization");
3867 inform (switch_loc, "switch starts here");
3868 inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3869 b->decl);
3870 }
3871 }
3872 }
3873
3874 if (switch_bindings->stmt_exprs > 0)
3875 {
3876 saw_error = true;
3877 error_at (case_loc, "switch jumps into statement expression");
3878 inform (switch_loc, "switch starts here");
3879 }
3880
3881 return saw_error;
3882}
3883
3884/* Given NAME, an IDENTIFIER_NODE,
3885 return the structure (or union or enum) definition for that name.
3886 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3887 CODE says which kind of type the caller wants;
3888 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3889 If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3890 location where the tag was defined.
3891 If the wrong kind of type is found, an error is reported. */
3892
3893static tree
3894lookup_tag (enum tree_code code, tree name, bool thislevel_only,
3895 location_t *ploc)
3896{
3897 struct c_binding *b = I_TAG_BINDING (name);
3898 bool thislevel = false;
3899
3900 if (!b || !b->decl)
3901 return NULL_TREE;
3902
3903 /* We only care about whether it's in this level if
3904 thislevel_only was set or it might be a type clash. */
3905 if (thislevel_only || TREE_CODE (b->decl) != code)
3906 {
3907 /* For our purposes, a tag in the external scope is the same as
3908 a tag in the file scope. (Primarily relevant to Objective-C
3909 and its builtin structure tags, which get pushed before the
3910 file scope is created.) */
3911 if (B_IN_CURRENT_SCOPE (b)
3912 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3913 thislevel = true;
3914 }
3915
3916 if (thislevel_only && !thislevel)
3917 return NULL_TREE;
3918
3919 if (TREE_CODE (b->decl) != code)
3920 {
3921 /* Definition isn't the kind we were looking for. */
3922 pending_invalid_xref = name;
3923 pending_invalid_xref_location = input_location;
3924
3925 /* If in the same binding level as a declaration as a tag
3926 of a different type, this must not be allowed to
3927 shadow that tag, so give the error immediately.
3928 (For example, "struct foo; union foo;" is invalid.) */
3929 if (thislevel)
3930 pending_xref_error ();
3931 }
3932
3933 if (ploc != NULL)
3934 *ploc = b->locus;
3935
3936 return b->decl;
3937}
3938
3939/* Return true if a definition exists for NAME with code CODE. */
3940
3941bool
3942tag_exists_p (enum tree_code code, tree name)
3943{
3944 struct c_binding *b = I_TAG_BINDING (name);
3945
3946 if (b == NULL || b->decl == NULL_TREE)
3947 return false;
3948 return TREE_CODE (b->decl) == code;
3949}
3950
3951/* Print an error message now
3952 for a recent invalid struct, union or enum cross reference.
3953 We don't print them immediately because they are not invalid
3954 when used in the `struct foo;' construct for shadowing. */
3955
3956void
3957pending_xref_error (void)
3958{
3959 if (pending_invalid_xref != NULL_TREE)
3960 error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3961 pending_invalid_xref);
3962 pending_invalid_xref = NULL_TREE;
3963}
3964
3965
3966/* Look up NAME in the current scope and its superiors
3967 in the namespace of variables, functions and typedefs.
3968 Return a ..._DECL node of some kind representing its definition,
3969 or return NULL_TREE if it is undefined. */
3970
3971tree
3972lookup_name (tree name)
3973{
3974 struct c_binding *b = I_SYMBOL_BINDING (name);
3975 if (b && !b->invisible)
3976 {
3977 maybe_record_typedef_use (b->decl);
3978 return b->decl;
3979 }
3980 return NULL_TREE;
3981}
3982
3983/* Similar to `lookup_name' but look only at the indicated scope. */
3984
3985static tree
3986lookup_name_in_scope (tree name, struct c_scope *scope)
3987{
3988 struct c_binding *b;
3989
3990 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3991 if (B_IN_SCOPE (b, scope))
3992 return b->decl;
3993 return NULL_TREE;
3994}
3995
3996/* Look for the closest match for NAME within the currently valid
3997 scopes.
3998
3999 This finds the identifier with the lowest Levenshtein distance to
4000 NAME. If there are multiple candidates with equal minimal distance,
4001 the first one found is returned. Scopes are searched from innermost
4002 outwards, and within a scope in reverse order of declaration, thus
4003 benefiting candidates "near" to the current scope.
4004
4005 The function also looks for similar macro names to NAME, since a
4006 misspelled macro name will not be expanded, and hence looks like an
4007 identifier to the C frontend.
4008
4009 It also looks for start_typename keywords, to detect "singed" vs "signed"
4010 typos.
4011
4012 Use LOC for any deferred diagnostics. */
4013
4014name_hint
4015lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc)
4016{
4017 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
4018
4019 /* First, try some well-known names in the C standard library, in case
4020 the user forgot a #include. */
4021 const char *header_hint
4022 = get_c_stdlib_header_for_name (IDENTIFIER_POINTER (name));
4023
4024 if (header_hint)
4025 return name_hint (NULL,
4026 new suggest_missing_header (loc,
4027 IDENTIFIER_POINTER (name),
4028 header_hint));
4029
4030 /* Only suggest names reserved for the implementation if NAME begins
4031 with an underscore. */
4032 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_');
4033
4034 best_match<tree, tree> bm (name);
4035
4036 /* Look within currently valid scopes. */
4037 for (c_scope *scope = current_scope; scope; scope = scope->outer)
4038 for (c_binding *binding = scope->bindings; binding; binding = binding->prev)
4039 {
4040 if (!binding->id || binding->invisible)
4041 continue;
4042 if (binding->decl == error_mark_node)
4043 continue;
4044 /* Don't use bindings from implicitly declared functions,
4045 as they were likely misspellings themselves. */
4046 if (TREE_CODE (binding->decl) == FUNCTION_DECL)
4047 if (C_DECL_IMPLICIT (binding->decl))
4048 continue;
4049 /* Don't suggest names that are reserved for use by the
4050 implementation, unless NAME began with an underscore. */
4051 if (!consider_implementation_names)
4052 {
4053 const char *suggestion_str = IDENTIFIER_POINTER (binding->id);
4054 if (name_reserved_for_implementation_p (suggestion_str))
4055 continue;
4056 }
4057 switch (kind)
4058 {
4059 case FUZZY_LOOKUP_TYPENAME:
4060 if (TREE_CODE (binding->decl) != TYPE_DECL)
4061 continue;
4062 break;
4063
4064 case FUZZY_LOOKUP_FUNCTION_NAME:
4065 if (TREE_CODE (binding->decl) != FUNCTION_DECL)
4066 {
4067 /* Allow function pointers. */
4068 if ((VAR_P (binding->decl)
4069 || TREE_CODE (binding->decl) == PARM_DECL)
4070 && TREE_CODE (TREE_TYPE (binding->decl)) == POINTER_TYPE
4071 && (TREE_CODE (TREE_TYPE (TREE_TYPE (binding->decl)))
4072 == FUNCTION_TYPE))
4073 break;
4074 continue;
4075 }
4076 break;
4077
4078 default:
4079 break;
4080 }
4081 bm.consider (binding->id);
4082 }
4083
4084 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
4085 as:
4086 x = SOME_OTHER_MACRO (y);
4087 then "SOME_OTHER_MACRO" will survive to the frontend and show up
4088 as a misspelled identifier.
4089
4090 Use the best distance so far so that a candidate is only set if
4091 a macro is better than anything so far. This allows early rejection
4092 (without calculating the edit distance) of macro names that must have
4093 distance >= bm.get_best_distance (), and means that we only get a
4094 non-NULL result for best_macro_match if it's better than any of
4095 the identifiers already checked, which avoids needless creation
4096 of identifiers for macro hashnodes. */
4097 best_macro_match bmm (name, bm.get_best_distance (), parse_in);
4098 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate ();
4099 /* If a macro is the closest so far to NAME, use it, creating an
4100 identifier tree node for it. */
4101 if (best_macro)
4102 {
4103 const char *id = (const char *)best_macro->ident.str;
4104 tree macro_as_identifier
4105 = get_identifier_with_length (id, best_macro->ident.len);
4106 bm.set_best_so_far (macro_as_identifier,
4107 bmm.get_best_distance (),
4108 bmm.get_best_candidate_length ());
4109 }
4110
4111 /* Try the "start_typename" keywords to detect
4112 "singed" vs "signed" typos. */
4113 if (kind == FUZZY_LOOKUP_TYPENAME)
4114 {
4115 for (unsigned i = 0; i < num_c_common_reswords; i++)
4116 {
4117 const c_common_resword *resword = &c_common_reswords[i];
4118 if (!c_keyword_starts_typename (resword->rid))
4119 continue;
4120 tree resword_identifier = ridpointers [resword->rid];
4121 if (!resword_identifier)
4122 continue;
4123 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE);
4124 bm.consider (resword_identifier);
4125 }
4126 }
4127
4128 tree best = bm.get_best_meaningful_candidate ();
4129 if (best)
4130 return name_hint (IDENTIFIER_POINTER (best), NULL);
4131 else
4132 return name_hint (NULL, NULL);
4133}
4134
4135
4136/* Create the predefined scalar types of C,
4137 and some nodes representing standard constants (0, 1, (void *) 0).
4138 Initialize the global scope.
4139 Make definitions for built-in primitive functions. */
4140
4141void
4142c_init_decl_processing (void)
4143{
4144 location_t save_loc = input_location;
4145
4146 /* Initialize reserved words for parser. */
4147 c_parse_init ();
4148
4149 current_function_decl = NULL_TREE;
4150
4151 gcc_obstack_init (&parser_obstack);
4152
4153 /* Make the externals scope. */
4154 push_scope ();
4155 external_scope = current_scope;
4156
4157 /* Declarations from c_common_nodes_and_builtins must not be associated
4158 with this input file, lest we get differences between using and not
4159 using preprocessed headers. */
4160 input_location = BUILTINS_LOCATION;
4161
4162 c_common_nodes_and_builtins ();
4163
4164 /* In C, comparisons and TRUTH_* expressions have type int. */
4165 truthvalue_type_node = integer_type_node;
4166 truthvalue_true_node = integer_one_node;
4167 truthvalue_false_node = integer_zero_node;
4168
4169 /* Even in C99, which has a real boolean type. */
4170 pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
4171 boolean_type_node));
4172
4173 input_location = save_loc;
4174
4175 make_fname_decl = c_make_fname_decl;
4176 start_fname_decls ();
4177}
4178
4179/* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
4180 give the decl, NAME is the initialization string and TYPE_DEP
4181 indicates whether NAME depended on the type of the function. As we
4182 don't yet implement delayed emission of static data, we mark the
4183 decl as emitted so it is not placed in the output. Anything using
4184 it must therefore pull out the STRING_CST initializer directly.
4185 FIXME. */
4186
4187static tree
4188c_make_fname_decl (location_t loc, tree id, int type_dep)
4189{
4190 const char *name = fname_as_string (type_dep);
4191 tree decl, type, init;
4192 size_t length = strlen (name);
4193
4194 type = build_array_type (char_type_node,
4195 build_index_type (size_int (length)));
4196 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
4197
4198 decl = build_decl (loc, VAR_DECL, id, type);
4199
4200 TREE_STATIC (decl) = 1;
4201 TREE_READONLY (decl) = 1;
4202 DECL_ARTIFICIAL (decl) = 1;
4203
4204 init = build_string (length + 1, name);
4205 free (CONST_CAST (char *, name));
4206 TREE_TYPE (init) = type;
4207 DECL_INITIAL (decl) = init;
4208
4209 TREE_USED (decl) = 1;
4210
4211 if (current_function_decl
4212 /* For invalid programs like this:
4213
4214 void foo()
4215 const char* p = __FUNCTION__;
4216
4217 the __FUNCTION__ is believed to appear in K&R style function
4218 parameter declarator. In that case we still don't have
4219 function_scope. */
4220 && current_function_scope)
4221 {
4222 DECL_CONTEXT (decl) = current_function_decl;
4223 bind (id, decl, current_function_scope,
4224 /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
4225 }
4226
4227 finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
4228
4229 return decl;
4230}
4231
4232tree
4233c_builtin_function (tree decl)
4234{
4235 tree type = TREE_TYPE (decl);
4236 tree id = DECL_NAME (decl);
4237
4238 const char *name = IDENTIFIER_POINTER (id);
4239 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4240
4241 /* Should never be called on a symbol with a preexisting meaning. */
4242 gcc_assert (!I_SYMBOL_BINDING (id));
4243
4244 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
4245 UNKNOWN_LOCATION);
4246
4247 /* Builtins in the implementation namespace are made visible without
4248 needing to be explicitly declared. See push_file_scope. */
4249 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4250 {
4251 DECL_CHAIN (decl) = visible_builtins;
4252 visible_builtins = decl;
4253 }
4254
4255 return decl;
4256}
4257
4258tree
4259c_builtin_function_ext_scope (tree decl)
4260{
4261 tree type = TREE_TYPE (decl);
4262 tree id = DECL_NAME (decl);
4263
4264 const char *name = IDENTIFIER_POINTER (id);
4265 C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
4266
4267 if (external_scope)
4268 bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
4269 UNKNOWN_LOCATION);
4270
4271 /* Builtins in the implementation namespace are made visible without
4272 needing to be explicitly declared. See push_file_scope. */
4273 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
4274 {
4275 DECL_CHAIN (decl) = visible_builtins;
4276 visible_builtins = decl;
4277 }
4278
4279 return decl;
4280}
4281
4282/* Called when a declaration is seen that contains no names to declare.
4283 If its type is a reference to a structure, union or enum inherited
4284 from a containing scope, shadow that tag name for the current scope
4285 with a forward reference.
4286 If its type defines a new named structure or union
4287 or defines an enum, it is valid but we need not do anything here.
4288 Otherwise, it is an error. */
4289
4290void
4291shadow_tag (const struct c_declspecs *declspecs)
4292{
4293 shadow_tag_warned (declspecs, 0);
4294}
4295
4296/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
4297 but no pedwarn. */
4298void
4299shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
4300{
4301 bool found_tag = false;
4302
4303 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
4304 {
4305 tree value = declspecs->type;
4306 enum tree_code code = TREE_CODE (value);
4307
4308 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
4309 /* Used to test also that TYPE_SIZE (value) != 0.
4310 That caused warning for `struct foo;' at top level in the file. */
4311 {
4312 tree name = TYPE_NAME (value);
4313 tree t;
4314
4315 found_tag = true;
4316
4317 if (declspecs->restrict_p)
4318 {
4319 error ("invalid use of %<restrict%>");
4320 warned = 1;
4321 }
4322
4323 if (name == NULL_TREE)
4324 {
4325 if (warned != 1 && code != ENUMERAL_TYPE)
4326 /* Empty unnamed enum OK */
4327 {
4328 pedwarn (input_location, 0,
4329 "unnamed struct/union that defines no instances");
4330 warned = 1;
4331 }
4332 }
4333 else if (declspecs->typespec_kind != ctsk_tagdef
4334 && declspecs->typespec_kind != ctsk_tagfirstref
4335 && declspecs->storage_class != csc_none)
4336 {
4337 if (warned != 1)
4338 pedwarn (input_location, 0,
4339 "empty declaration with storage class specifier "
4340 "does not redeclare tag");
4341 warned = 1;
4342 pending_xref_error ();
4343 }
4344 else if (declspecs->typespec_kind != ctsk_tagdef
4345 && declspecs->typespec_kind != ctsk_tagfirstref
4346 && (declspecs->const_p
4347 || declspecs->volatile_p
4348 || declspecs->atomic_p
4349 || declspecs->restrict_p
4350 || declspecs->address_space))
4351 {
4352 if (warned != 1)
4353 pedwarn (input_location, 0,
4354 "empty declaration with type qualifier "
4355 "does not redeclare tag");
4356 warned = 1;
4357 pending_xref_error ();
4358 }
4359 else if (declspecs->typespec_kind != ctsk_tagdef
4360 && declspecs->typespec_kind != ctsk_tagfirstref
4361 && declspecs->alignas_p)
4362 {
4363 if (warned != 1)
4364 pedwarn (input_location, 0,
4365 "empty declaration with %<_Alignas%> "
4366 "does not redeclare tag");
4367 warned = 1;
4368 pending_xref_error ();
4369 }
4370 else
4371 {
4372 pending_invalid_xref = NULL_TREE;
4373 t = lookup_tag (code, name, true, NULL);
4374
4375 if (t == NULL_TREE)
4376 {
4377 t = make_node (code);
4378 pushtag (input_location, name, t);
4379 }
4380 }
4381 }
4382 else
4383 {
4384 if (warned != 1 && !in_system_header_at (input_location))
4385 {
4386 pedwarn (input_location, 0,
4387 "useless type name in empty declaration");
4388 warned = 1;
4389 }
4390 }
4391 }
4392 else if (warned != 1 && !in_system_header_at (input_location)
4393 && declspecs->typedef_p)
4394 {
4395 pedwarn (input_location, 0, "useless type name in empty declaration");
4396 warned = 1;
4397 }
4398
4399 pending_invalid_xref = NULL_TREE;
4400
4401 if (declspecs->inline_p)
4402 {
4403 error ("%<inline%> in empty declaration");
4404 warned = 1;
4405 }
4406
4407 if (declspecs->noreturn_p)
4408 {
4409 error ("%<_Noreturn%> in empty declaration");
4410 warned = 1;
4411 }
4412
4413 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
4414 {
4415 error ("%<auto%> in file-scope empty declaration");
4416 warned = 1;
4417 }
4418
4419 if (current_scope == file_scope && declspecs->storage_class == csc_register)
4420 {
4421 error ("%<register%> in file-scope empty declaration");
4422 warned = 1;
4423 }
4424
4425 if (!warned && !in_system_header_at (input_location)
4426 && declspecs->storage_class != csc_none)
4427 {
4428 warning (0, "useless storage class specifier in empty declaration");
4429 warned = 2;
4430 }
4431
4432 if (!warned && !in_system_header_at (input_location) && declspecs->thread_p)
4433 {
4434 warning (0, "useless %qs in empty declaration",
4435 declspecs->thread_gnu_p ? "__thread" : "_Thread_local");
4436 warned = 2;
4437 }
4438
4439 if (!warned
4440 && !in_system_header_at (input_location)
4441 && (declspecs->const_p
4442 || declspecs->volatile_p
4443 || declspecs->atomic_p
4444 || declspecs->restrict_p
4445 || declspecs->address_space))
4446 {
4447 warning (0, "useless type qualifier in empty declaration");
4448 warned = 2;
4449 }
4450
4451 if (!warned && !in_system_header_at (input_location)
4452 && declspecs->alignas_p)
4453 {
4454 warning (0, "useless %<_Alignas%> in empty declaration");
4455 warned = 2;
4456 }
4457
4458 if (warned != 1)
4459 {
4460 if (!found_tag)
4461 pedwarn (input_location, 0, "empty declaration");
4462 }
4463}
4464
4465
4466/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
4467 bits. SPECS represents declaration specifiers that the grammar
4468 only permits to contain type qualifiers and attributes. */
4469
4470int
4471quals_from_declspecs (const struct c_declspecs *specs)
4472{
4473 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
4474 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
4475 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
4476 | (specs->atomic_p ? TYPE_QUAL_ATOMIC : 0)
4477 | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
4478 gcc_assert (!specs->type
4479 && !specs->decl_attr
4480 && specs->typespec_word == cts_none
4481 && specs->storage_class == csc_none
4482 && !specs->typedef_p
4483 && !specs->explicit_signed_p
4484 && !specs->deprecated_p
4485 && !specs->long_p
4486 && !specs->long_long_p
4487 && !specs->short_p
4488 && !specs->signed_p
4489 && !specs->unsigned_p
4490 && !specs->complex_p
4491 && !specs->inline_p
4492 && !specs->noreturn_p
4493 && !specs->thread_p);
4494 return quals;
4495}
4496
4497/* Construct an array declarator. LOC is the location of the
4498 beginning of the array (usually the opening brace). EXPR is the
4499 expression inside [], or NULL_TREE. QUALS are the type qualifiers
4500 inside the [] (to be applied to the pointer to which a parameter
4501 array is converted). STATIC_P is true if "static" is inside the
4502 [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
4503 VLA of unspecified length which is nevertheless a complete type,
4504 false otherwise. The field for the contained declarator is left to
4505 be filled in by set_array_declarator_inner. */
4506
4507struct c_declarator *
4508build_array_declarator (location_t loc,
4509 tree expr, struct c_declspecs *quals, bool static_p,
4510 bool vla_unspec_p)
4511{
4512 struct c_declarator *declarator = XOBNEW (&parser_obstack,
4513 struct c_declarator);
4514 declarator->id_loc = loc;
4515 declarator->kind = cdk_array;
4516 declarator->declarator = 0;
4517 declarator->u.array.dimen = expr;
4518 if (quals)
4519 {
4520 declarator->u.array.attrs = quals->attrs;
4521 declarator->u.array.quals = quals_from_declspecs (quals);
4522 }
4523 else
4524 {
4525 declarator->u.array.attrs = NULL_TREE;
4526 declarator->u.array.quals = 0;
4527 }
4528 declarator->u.array.static_p = static_p;
4529 declarator->u.array.vla_unspec_p = vla_unspec_p;
4530 if (static_p || quals != NULL)
4531 pedwarn_c90 (loc, OPT_Wpedantic,
4532 "ISO C90 does not support %<static%> or type "
4533 "qualifiers in parameter array declarators");
4534 if (vla_unspec_p)
4535 pedwarn_c90 (loc, OPT_Wpedantic,
4536 "ISO C90 does not support %<[*]%> array declarators");
4537 if (vla_unspec_p)
4538 {
4539 if (!current_scope->parm_flag)
4540 {
4541 /* C99 6.7.5.2p4 */
4542 error_at (loc, "%<[*]%> not allowed in other than "
4543 "function prototype scope");
4544 declarator->u.array.vla_unspec_p = false;
4545 return NULL;
4546 }
4547 current_scope->had_vla_unspec = true;
4548 }
4549 return declarator;
4550}
4551
4552/* Set the contained declarator of an array declarator. DECL is the
4553 declarator, as constructed by build_array_declarator; INNER is what
4554 appears on the left of the []. */
4555
4556struct c_declarator *
4557set_array_declarator_inner (struct c_declarator *decl,
4558 struct c_declarator *inner)
4559{
4560 decl->declarator = inner;
4561 return decl;
4562}
4563
4564/* INIT is a constructor that forms DECL's initializer. If the final
4565 element initializes a flexible array field, add the size of that
4566 initializer to DECL's size. */
4567
4568static void
4569add_flexible_array_elts_to_size (tree decl, tree init)
4570{
4571 tree elt, type;
4572
4573 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
4574 return;
4575
4576 elt = CONSTRUCTOR_ELTS (init)->last ().value;
4577 type = TREE_TYPE (elt);
4578 if (TREE_CODE (type) == ARRAY_TYPE
4579 && TYPE_SIZE (type) == NULL_TREE
4580 && TYPE_DOMAIN (type) != NULL_TREE
4581 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
4582 {
4583 complete_array_type (&type, elt, false);
4584 DECL_SIZE (decl)
4585 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
4586 DECL_SIZE_UNIT (decl)
4587 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
4588 }
4589}
4590
4591/* Decode a "typename", such as "int **", returning a ..._TYPE node.
4592 Set *EXPR, if EXPR not NULL, to any expression to be evaluated
4593 before the type name, and set *EXPR_CONST_OPERANDS, if
4594 EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
4595 appear in a constant expression. */
4596
4597tree
4598groktypename (struct c_type_name *type_name, tree *expr,
4599 bool *expr_const_operands)
4600{
4601 tree type;
4602 tree attrs = type_name->specs->attrs;
4603
4604 type_name->specs->attrs = NULL_TREE;
4605
4606 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
4607 false, NULL, &attrs, expr, expr_const_operands,
4608 DEPRECATED_NORMAL);
4609
4610 /* Apply attributes. */
4611 decl_attributes (&type, attrs, 0);
4612
4613 return type;
4614}
4615
4616/* Wrapper for decl_attributes that adds some implicit attributes
4617 to VAR_DECLs or FUNCTION_DECLs. */
4618
4619static tree
4620c_decl_attributes (tree *